{"problem_id": "0001", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi is going to set a 3-character password.

\n

How many possible passwords are there if each of its characters must be a digit between 1 and N (inclusive)?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 9
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the number of possible passwords.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

There are eight possible passwords: 111, 112, 121, 122, 211, 212, 221, and 222.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

There is only one possible password if you can only use one kind of character.

\n
\n
", "c_code": "int solution() {\n\n int N = 0;\n scanf(\"%d\", &N);\n\n printf(\"%d\\n\", N * 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 get input\");\n let input: u32 = input.trim().parse().expect(\"failed to parse to u32\");\n println!(\"{}\", input * input * input);\n}", "difficulty": "medium"} {"problem_id": "0002", "problem_description": "You are given four integers $$$a$$$, $$$b$$$, $$$x$$$ and $$$y$$$. Initially, $$$a \\ge x$$$ and $$$b \\ge y$$$. You can do the following operation no more than $$$n$$$ times: Choose either $$$a$$$ or $$$b$$$ and decrease it by one. However, as a result of this operation, value of $$$a$$$ cannot become less than $$$x$$$, and value of $$$b$$$ cannot become less than $$$y$$$. Your task is to find the minimum possible product of $$$a$$$ and $$$b$$$ ($$$a \\cdot b$$$) you can achieve by applying the given operation no more than $$$n$$$ times.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n unsigned long long int a;\n unsigned long long int b;\n unsigned long long int x;\n unsigned long long int y;\n unsigned long long int n;\n unsigned long long int t;\n unsigned counter0 = 0;\n scanf(\"%llu\", &t);\n unsigned long long int arr[t];\n while (counter0 < t) {\n scanf(\"%llu%llu%llu%llu%llu\", &a, &b, &x, &y, &n);\n\n if (n >= (a - x) + (b - y)) {\n arr[counter0] = x * y;\n }\n\n else if ((a <= b) && (x <= y)) {\n if (n <= (a - x)) {\n a = a - n;\n arr[counter0] = a * b;\n } else if ((n > (a - x))) {\n b = b - (n - (a - x));\n a = a - (a - x);\n arr[counter0] = a * b;\n }\n\n }\n\n else if ((a <= b) && (x >= y)) {\n if (n <= (b - x)) {\n if (n <= (a - x)) {\n a = a - n;\n arr[counter0] = a * b;\n } else {\n b = b - (n - (a - x));\n a = a - (a - x);\n arr[counter0] = a * b;\n }\n } else if (n > (b - x)) {\n if (n <= (b - y)) {\n b = b - n;\n arr[counter0] = a * b;\n } else if (n > (b - y)) {\n a = a - (n - (b - y));\n b = b - (b - y);\n arr[counter0] = a * b;\n }\n }\n }\n\n else if ((a >= b) && (x <= y)) {\n if (n <= (a - y)) {\n if (n <= (b - y)) {\n b = b - n;\n arr[counter0] = a * b;\n } else if (n > (b - y)) {\n a = a - (n - (b - y));\n b = b - (b - y);\n arr[counter0] = a * b;\n }\n } else if (n > (a - y)) {\n if (n <= (a - x)) {\n a = a - n;\n arr[counter0] = a * b;\n } else if (n > (a - x)) {\n b = b - (n - (a - x));\n a = a - (a - x);\n arr[counter0] = a * b;\n }\n }\n }\n\n else if ((a >= b) && (x >= y)) {\n if (n <= (b - y)) {\n b = b - n;\n arr[counter0] = a * b;\n } else if (n > (b - y)) {\n a = a - (n - (b - y));\n b = b - (b - y);\n arr[counter0] = a * b;\n }\n }\n\n counter0++;\n }\n for (counter0 = 0; counter0 < t; counter0++) {\n printf(\"%llu\\n\", arr[counter0]);\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 let mut ans = Vec::with_capacity(t);\n for _ in 0..t {\n let (a, b, x, y, n): (i64, i64, i64, i64, i64) = {\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 iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().parse().unwrap(),\n )\n };\n\n use std::cmp;\n let res1 = {\n let i = cmp::min(a - x, n);\n let j = cmp::min(b - y, n - i);\n (a - i) * (b - j)\n };\n let res2 = {\n let i = cmp::min(b - y, n);\n let j = cmp::min(a - x, n - i);\n (b - i) * (a - j)\n };\n ans.push(cmp::min(res1, res2));\n }\n println!(\n \"{}\",\n ans.iter()\n .map(|val| val.to_string())\n .collect::>()\n .join(\"\\n\")\n );\n}", "difficulty": "easy"} {"problem_id": "0003", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integer numbers.Let instability of the array be the following value: $$$\\max\\limits_{i = 1}^{n} a_i - \\min\\limits_{i = 1}^{n} a_i$$$.You have to remove exactly one element from this array to minimize instability of the resulting $$$(n-1)$$$-elements array. Your task is to calculate the minimum possible instability.", "c_code": "int solution(void) {\n int n = 0;\n scanf(\"%d\\n\", &n);\n\n if (n == 2) {\n printf(\"0\\n\");\n return 0;\n }\n\n int arr[n];\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n\n int min1 = 1e9;\n int min2 = 1e9;\n\n int max1 = -1;\n int max2 = -1;\n\n for (int j = 0; j < n; j++) {\n if (min1 > arr[j]) {\n min2 = min1;\n min1 = arr[j];\n } else if (min2 > arr[j]) {\n min2 = arr[j];\n }\n\n if (max2 < arr[j]) {\n max1 = max2;\n max2 = arr[j];\n } else if (max1 < arr[j]) {\n max1 = arr[j];\n }\n }\n\n printf(\"%d\\n\", ((max1 - min1) > (max2 - min2) ? max2 - min2 : max1 - min1));\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_to_string(&mut buf).unwrap();\n let mut tok = buf.split_whitespace();\n let mut get = || tok.next().unwrap();\n\n\n let n = get!(usize);\n\n if n == 2 {\n println!(\"0\");\n return;\n }\n\n let mut xs = vec![];\n for _ in 0..n {\n xs.push(get!());\n }\n xs.sort();\n\n let ans = min(xs[n - 1] - xs[1], xs[n - 2] - xs[0]);\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0004", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi is competing in a sumo tournament.\nThe tournament lasts for 15 days, during which he performs in one match per day.\nIf he wins 8 or more matches, he can also participate in the next tournament.

\n

The matches for the first k days have finished.\nYou are given the results of Takahashi's matches as a string S consisting of o and x.\nIf the i-th character in S is o, it means that Takahashi won the match on the i-th day; if that character is x, it means that Takahashi lost the match on the i-th day.

\n

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO if there is no such possibility.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq k \\leq 15
  • \n
  • S is a string of length k consisting of o and x.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

oxoxoxoxoxoxox\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

Takahashi has 7 wins and 7 losses before the last match. If he wins that match, he will have 8 wins.

\n
\n
\n
\n
\n
\n

Sample Input 2

xxxxxxxx\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
", "c_code": "int solution(void) {\n int cnt = 15;\n int s = 0;\n char i;\n char *mes = \"YES\";\n while (1) {\n scanf(\"%c\", &i);\n if (i == '\\n') {\n break;\n }\n if (i == 'o') {\n s++;\n }\n cnt--;\n }\n if (cnt + s < 8) {\n mes = \"NO\";\n }\n printf(\"%s\\n\", mes);\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let chars: Vec = buf.trim().chars().collect();\n\n let mut lose_cnt = 0;\n for c in chars.iter() {\n if *c == 'x' {\n lose_cnt += 1;\n }\n }\n\n println!(\"{}\", if lose_cnt >= 8 { \"NO\" } else { \"YES\" });\n}", "difficulty": "medium"} {"problem_id": "0005", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Ringo is giving a present to Snuke.

\n

Ringo has found out that Snuke loves yakiniku (a Japanese term meaning grilled meat. yaki: grilled, niku: meat). He supposes that Snuke likes grilled things starting with YAKI in Japanese, and does not like other things.

\n

You are given a string S representing the Japanese name of Ringo's present to Snuke. Determine whether S starts with YAKI.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 10
  • \n
  • S consists of uppercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If S starts with YAKI, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

YAKINIKU\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

YAKINIKU starts with YAKI.

\n
\n
\n
\n
\n
\n

Sample Input 2

TAKOYAKI\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

TAKOYAKI (a Japanese snack. tako: octopus) does not start with YAKI.

\n
\n
\n
\n
\n
\n

Sample Input 3

YAK\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution(void) {\n char po[114];\n scanf(\"%s\", po);\n if (strlen(po) >= 4 && po[0] == 'Y' && po[1] == 'A' && po[2] == 'K' &&\n po[3] == 'I') {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n if s.chars().take(4).collect::() == \"YAKI\" {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0006", "problem_description": "You are given an array $$$a[0 \\dots n-1]$$$ of $$$n$$$ integers. This array is called a \"valley\" if there exists exactly one subarray $$$a[l \\dots r]$$$ such that: $$$0 \\le l \\le r \\le n-1$$$, $$$a_l = a_{l+1} = a_{l+2} = \\dots = a_r$$$, $$$l = 0$$$ or $$$a_{l-1} > a_{l}$$$, $$$r = n-1$$$ or $$$a_r < a_{r+1}$$$. Here are three examples: The first image shows the array [$$$3, 2, 2, 1, 2, 2, 3$$$], it is a valley because only subarray with indices $$$l=r=3$$$ satisfies the condition.The second image shows the array [$$$1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6$$$], it is a valley because only subarray with indices $$$l=0, r=2$$$ satisfies the codition.The third image shows the array [$$$1, 2, 3, 4, 3, 2, 1$$$], it is not a valley because two subarrays $$$l=r=0$$$ and $$$l=r=6$$$ that satisfy the condition.You are asked whether the given array is a valley or not.Note that we consider the array to be indexed from $$$0$$$.", "c_code": "int solution() {\n int s;\n scanf(\"%d\", &s);\n while (s--) {\n int n;\n scanf(\"%d\", &n);\n int a[200001];\n int i = 0;\n int h = 0;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (h != 2 && i != 0 && a[i] > a[i - 1]) {\n h = 1;\n }\n if (h == 1 && a[i] < a[i - 1]) {\n h = 2;\n }\n }\n if (h == 2) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (stdin, stdout) = (io::stdin(), io::stdout());\n let mut scan = Scanner::new(stdin.lock());\n let mut out = io::BufWriter::new(stdout.lock());\n\n let t = scan.token::();\n\n for _k in 0..t {\n let n = scan.token::();\n\n let mut A = vec![];\n for _i in 0..n {\n let a = scan.token::();\n A.push(a);\n }\n\n let mut left_side_is_ok = true;\n let mut cnt = 0;\n\n for i in 0..n {\n if i == n - 1 {\n if left_side_is_ok {\n cnt += 1;\n }\n } else if A[i] < A[i + 1] {\n if left_side_is_ok {\n cnt += 1;\n left_side_is_ok = false;\n } else {\n left_side_is_ok = false;\n }\n } else if A[i] == A[i + 1] {\n } else if A[i] > A[i + 1] {\n left_side_is_ok = true;\n }\n }\n\n if cnt == 1 {\n writeln!(out, \"YES\");\n } else {\n writeln!(out, \"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0007", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

\n

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

\n

What is the minimum amount of money with which he can buy M cans of energy drinks?

\n

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N, M \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
  • 1 \\leq B_i \\leq 10^5
  • \n
  • B_1 + ... + B_N \\geq M
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_1 B_1\nA_2 B_2\n\\vdots\nA_N B_N\n
\n
\n
\n
\n
\n

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 5\n4 9\n2 4\n
\n
\n
\n
\n
\n

Sample Output 1

12\n
\n

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 30\n6 18\n2 5\n3 10\n7 9\n
\n
\n
\n
\n
\n

Sample Output 2

130\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 100000\n1000000000 100000\n
\n
\n
\n
\n
\n

Sample Output 3

100000000000000\n
\n

The output may not fit into a 32-bit integer type.

\n
\n
", "c_code": "int solution(void) {\n long long int n;\n long long int m;\n long long int i;\n long long int kei = 0;\n long long int a[100000];\n long long int b[100000];\n scanf(\"%lld %lld\", &n, &m);\n for (i = 0; i < n; i++) {\n scanf(\"%lld %lld\", &a[i], &b[i]);\n }\n\n int j;\n int increment;\n int temp;\n int temp_b;\n increment = 4;\n int array_size = n;\n while (increment > 0) {\n for (i = 0; i < array_size; i++) {\n j = i;\n temp = a[i];\n temp_b = b[i];\n while ((j >= increment) && (a[j - increment] > temp)) {\n a[j] = a[j - increment];\n b[j] = b[j - increment];\n j = j - increment;\n }\n a[j] = temp;\n b[j] = temp_b;\n }\n if (increment / 2 != 0) {\n increment = increment / 2;\n } else if (increment == 1) {\n increment = 0;\n } else {\n increment = 1;\n }\n }\n for (i = 0; i < n; i++) {\n if (m >= b[i]) {\n kei = (a[i] * b[i]) + kei;\n } else if (m == 0) {\n break;\n } else {\n kei = kei + (a[i] * m);\n break;\n }\n m = m - b[i];\n }\n printf(\"%lld\", kei);\n return 0;\n}", "rust_code": "fn solution() {\n let mut nm = String::new();\n stdin().read_line(&mut nm).unwrap();\n let nm: Vec = nm.split_whitespace().flat_map(str::parse).collect();\n let (n, m) = (nm[0], nm[1]);\n let mut stores: Vec<(usize, usize)> = (0..n)\n .map(|_| {\n let mut a = String::new();\n stdin().read_line(&mut a).unwrap();\n let a: Vec = a.split_whitespace().flat_map(str::parse).collect();\n (a[0], a[1])\n })\n .collect();\n stores.sort();\n let mut count = m;\n let mut result = 0;\n for (yen, stock) in stores {\n let needs = if stock > count { count } else { stock };\n count -= needs;\n result += yen * needs;\n if count == 0 {\n break;\n }\n }\n println!(\"{}\", result);\n}", "difficulty": "hard"} {"problem_id": "0008", "problem_description": "T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n int i;\n int j;\n int b;\n int max;\n int a[102];\n for (; t > 0; t--) {\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n max = 0;\n for (i = 0; i < n; i++) {\n if (a[i] > a[max]) {\n max = i;\n }\n }\n a[max]--;\n b = max;\n for (j = 1;; j++) {\n max = -1;\n for (i = 0; i < n; i++) {\n if (i != b && a[i] > 0) {\n if (max < 0) {\n max = i;\n } else if (a[i] > a[max]) {\n max = i;\n }\n }\n }\n if (max < 0) {\n break;\n }\n b = max;\n a[b]--;\n }\n if (j % 2 > 0) {\n printf(\"T\\n\");\n } else {\n printf(\"HL\\n\");\n }\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 a: 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 max = *a.iter().max().unwrap();\n let sum = a.iter().sum::();\n if max > sum / 2 || sum % 2 == 1 {\n println!(\"T\");\n } else {\n println!(\"HL\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0009", "problem_description": "Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?", "c_code": "int solution() {\n long n;\n long m = 1000000000;\n long k;\n long long t = 0;\n int c = 0;\n scanf(\"%ld %ld\", &n, &k);\n long a[n];\n long r[n];\n for (long i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n if (a[i] < m) {\n m = a[i];\n }\n }\n for (long i = 0; i < n; i++) {\n r[i] = a[i] - m;\n if (r[i] % k == 0) {\n t = t + (r[i] / k);\n } else {\n c = 1;\n break;\n }\n }\n if (c == 1) {\n printf(\"%d\", -1);\n } else {\n printf(\"%lld\", t);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).expect(\"read error\");\n\n let tmp: Vec<&str> = line.split_whitespace().collect();\n\n let _n = tmp[0].parse::().expect(\"parse error\");\n let k = tmp[1].parse::().expect(\"parse error\");\n\n let mut line2 = String::new();\n\n io::stdin().read_line(&mut line2).expect(\"read error\");\n\n let mut a: Vec = line2\n .split_whitespace()\n .map(|x| x.parse::().expect(\"parse error\"))\n .collect::>();\n\n a.sort();\n\n let mut ans: i64 = 0;\n let mut flag = false;\n\n for x in &a {\n let mut diff = *x - a[0];\n if diff % k != 0 {\n flag = true;\n break;\n }\n diff /= k;\n let tmp = diff as i64;\n ans += tmp;\n }\n\n if flag {\n println!(\"-1\");\n } else {\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "0010", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

For an integer N, we will choose a permutation \\{P_1, P_2, ..., P_N\\} of \\{1, 2, ..., N\\}.

\n

Then, for each i=1,2,...,N, let M_i be the remainder when i is divided by P_i.

\n

Find the maximum possible value of M_1 + M_2 + \\cdots + M_N.

\n
\n
\n
\n
\n

Constraints

    \n
  • N is an integer satisfying 1 \\leq N \\leq 10^9.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible value of M_1 + M_2 + \\cdots + M_N.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

When the permutation \\{P_1, P_2\\} = \\{2, 1\\} is chosen, M_1 + M_2 = 1 + 0 = 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

13\n
\n
\n
\n
\n
\n

Sample Output 2

78\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n long long n = 0;\n long long result = 0;\n scanf(\"%lld\", &n);\n result = n * (n - 1) / 2;\n printf(\"%lld\", result);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut input = String::new();\n stdin.lock().read_line(&mut input).expect(\"read line!\");\n let input: String = input;\n let n: u64 = input.trim().parse().unwrap();\n println!(\"{}\", (n * (n - 1)) / 2);\n}", "difficulty": "easy"} {"problem_id": "0011", "problem_description": "Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed.", "c_code": "int solution(void) {\n int n = 0;\n int num = 0;\n int cnt = 0;\n int top = -1;\n int target = 1;\n char cmd[20] = {\n 0,\n };\n int stack[300001] = {\n 0,\n };\n\n scanf(\"%d\", &n);\n n *= 2;\n\n while (n--) {\n scanf(\"%s\", cmd);\n if (cmd[0] == 'a') {\n scanf(\"%d\", &num);\n stack[++top] = num;\n } else {\n if (top >= 0) {\n if (stack[top] != target) {\n top = -1;\n cnt++;\n } else {\n top--;\n }\n }\n\n target++;\n }\n }\n\n printf(\"%d\\n\", cnt);\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 = n.trim().parse::().unwrap();\n\n let mut stack: Vec = Vec::with_capacity(n as usize);\n let mut ans = 0;\n let mut counter = 1;\n\n for _ in 0..(2 * n) {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let input: Vec<_> = input.split_whitespace().collect();\n if input.len() == 2 {\n let k = input.last().unwrap().parse::().unwrap();\n\n stack.push(k);\n } else {\n match stack.pop() {\n Some(p) if p != counter => {\n stack.clear();\n ans += 1;\n }\n _ => (),\n }\n counter += 1;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0012", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

\n

We want to deploy some number of inspectors so that each of these trees will be inspected.

\n

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \\leq i \\leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

\n

Find the minimum number of inspectors that we need to deploy to achieve the objective.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 20
  • \n
  • 1 \\leq D \\leq 20
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N D\n
\n
\n
\n
\n
\n

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

14 3\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

20 4\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution() {\n int N = 0;\n int D = 0;\n int c = 0;\n scanf(\"%d %d\", &N, &D);\n while (N > 0) {\n N = N - (2 * D + 1);\n c++;\n }\n printf(\"%d\\n\", c);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut nd = String::new();\n stdin().read_line(&mut nd).unwrap();\n let nd: Vec = nd.split_whitespace().flat_map(str::parse).collect();\n let (n, d) = (nd[0], nd[1]);\n\n println!(\n \"{}\",\n (n / (2 * d + 1)) + if n % (2 * d + 1) > 0 { 1 } else { 0 }\n );\n}", "difficulty": "medium"} {"problem_id": "0013", "problem_description": "

Naive String Search

\n\n

\n Find places where a string P is found within a text T.\n \n Print all indices of T where P found. The indices of T start with 0.\n

\n\n

Input

\n\n

\n In the first line, a text T is given. In the second line, a string P is given.\n

\n\n

output

\n\n

\n Print an index of T where P found in a line. Print the indices in ascending order.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ length of T ≤ 1000
  • \n
  • 1 ≤ length of P ≤ 1000
  • \n
  • The input consists of alphabetical characters and digits
  • \n
\n\n

Sample Input 1

\n
\naabaaa\naa\n
\n\n

Sample Output 1

\n
\n0\n3\n4\n
\n\n

Sample Input 2

\n
\nxyzz\nyz\n
\n\n

Sample Output 2

\n
\n1\n
\n\n\n\n

Sample Input 3

\n
\nabc\nxyz\n
\n\n

Sample Output3

\n
\n
\n

\nThe ouput should be empty.\n

", "c_code": "int solution(void) {\n char t[1000];\n char p[1000];\n int n;\n int m;\n scanf(\"%s\", t);\n scanf(\"%s\", p);\n for (n = 0; t[n] != '\\0'; n++) {\n ;\n }\n for (m = 0; p[m] != '\\0'; m++) {\n ;\n }\n\n int i = 0;\n int j = 0;\n while (i <= n - m) {\n while (j < m && t[i + j] == p[j]) {\n\n j++;\n }\n if (j == m) {\n printf(\"%d\\n\", i);\n }\n i++;\n j = 0;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut read = String::new();\n std::io::stdin().read_line(&mut read).expect(\"\");\n let mut s = read.clone();\n s = s.trim().to_string();\n read = String::new();\n std::io::stdin().read_line(&mut read).expect(\"\");\n let f = read.trim();\n if f.len() > s.len() {\n std::process::exit(0);\n }\n for i in 0..(s.len() - f.len() + 1) {\n if &s[i..i + f.len()] == f {\n println!(\"{}\", i);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0014", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

\n

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

\n

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq N \\leq 200000
  • \n
  • 1 \\leq A_i \\leq N
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2\n1 1 2 2 5\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.\nOn the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 4\n1 1 2 2\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 3\n5 1 3 2 4 1 1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution() {\n int N;\n int K;\n int i;\n int tmp;\n int count = 0;\n int ans = 0;\n\n scanf(\"%d %d\", &N, &K);\n\n int A[N + 1];\n int d[N + 1];\n for (i = 0; i <= N; i++) {\n A[i] = 0;\n d[i] = 0;\n }\n for (i = 0; i < N; i++) {\n scanf(\"%d\", &tmp);\n A[tmp]++;\n }\n\n for (i = 0; i <= N; i++) {\n d[A[i]]++;\n }\n\n for (i = N; i > 0; i--) {\n if (count >= K) {\n ans += i * d[i];\n } else if (count + d[i] > K) {\n ans += i * (count + d[i] - K);\n }\n count += d[i];\n }\n\n printf(\"%d\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n stdin.read_until(b' ', &mut Vec::new()).unwrap();\n let mut k = String::new();\n stdin.read_line(&mut k).unwrap();\n let k: usize = k.trim_end().parse().unwrap();\n\n let mut amt: Vec = stdin\n .split(b' ')\n .map(|r| {\n String::from_utf8(r.unwrap())\n .unwrap()\n .trim_end()\n .parse::()\n .unwrap()\n })\n .fold(HashMap::with_capacity(200_000), |mut map, a| {\n *map.entry(a).or_insert(0) += 1;\n map\n })\n .values()\n .cloned()\n .collect();\n\n amt.sort();\n println!(\n \"{}\",\n amt.iter().take(amt.len().saturating_sub(k)).sum::()\n );\n}", "difficulty": "hard"} {"problem_id": "0015", "problem_description": "Moamen has an array of $$$n$$$ distinct integers. He wants to sort that array in non-decreasing order by doing the following operations in order exactly once: Split the array into exactly $$$k$$$ non-empty subarrays such that each element belongs to exactly one subarray. Reorder these subarrays arbitrary. Merge the subarrays in their new order. A sequence $$$a$$$ is a subarray of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.Can you tell Moamen if there is a way to sort the array in non-decreasing order using the operations written above?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n int c = 0;\n int i;\n int j;\n scanf(\"%d %d\", &n, &k);\n long ara[n];\n long p;\n for (i = 0; i < n; i++) {\n scanf(\"%ld\", &ara[i]);\n }\n for (i = 0; i < n; i++) {\n if (ara[i] < p || i == 0) {\n c++;\n } else if ((ara[i] - p) == 1) {\n p = ara[i];\n continue;\n } else {\n for (j = 0; j < n; j++) {\n if (ara[j] > p && ara[j] < ara[i]) {\n c++;\n break;\n }\n }\n }\n p = ara[i];\n }\n if (c <= k) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n\n let t: i32 = lines.next().unwrap()?.parse()?;\n for _ in 0..t {\n let (_, mut k): (i32, i32) = {\n let line = lines.next().unwrap()?;\n let mut lines = line.split(\" \").map(|s| s.parse());\n (lines.next().unwrap()?, lines.next().unwrap()?)\n };\n let mut a: Vec<(i32, usize)> = lines\n .next()\n .unwrap()?\n .split(\" \")\n .map(|s| s.parse::().unwrap())\n .enumerate()\n .map(|s| (s.1, s.0))\n .collect();\n let b: HashMap = a.clone().into_iter().collect();\n a.sort_by_key(|k| k.0);\n\n let mut row_prev: Option = None;\n for (v, i) in a {\n let j = b[&v];\n if let Some(rp) = &row_prev\n && rp + 1 != j {\n row_prev = None;\n k -= 1;\n }\n row_prev = Some(i);\n }\n\n if k > 0 {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n }\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0016", "problem_description": "In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them.", "c_code": "int solution() {\n int n = 0;\n int a[100];\n int s = 0;\n int max = 0;\n int i = 0;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (i = 0; i < n; i++) {\n if (max < a[i]) {\n max = a[i];\n }\n }\n for (i = 0; i < n; i++) {\n s += max - a[i];\n }\n printf(\"%d\", s);\n\n return 0;\n}", "rust_code": "fn solution() {\n io::stdin().read_line(&mut String::new()).unwrap();\n\n let mut welfare_input = String::new();\n io::stdin().read_line(&mut welfare_input).unwrap();\n\n let mut max_welfare = 0;\n let mut total_welfare = 0;\n let mut population = 0;\n\n for welfare in welfare_input.split_whitespace().map(|x| x.parse().unwrap()) {\n total_welfare += welfare;\n max_welfare = cmp::max(max_welfare, welfare);\n population += 1;\n }\n\n println!(\"{}\", population * max_welfare - total_welfare);\n}", "difficulty": "medium"} {"problem_id": "0017", "problem_description": "International Women's Day is coming soon! Polycarp is preparing for the holiday.There are $$$n$$$ candy boxes in the shop for sale. The $$$i$$$-th box contains $$$d_i$$$ candies.Polycarp wants to prepare the maximum number of gifts for $$$k$$$ girls. Each gift will consist of exactly two boxes. The girls should be able to share each gift equally, so the total amount of candies in a gift (in a pair of boxes) should be divisible by $$$k$$$. In other words, two boxes $$$i$$$ and $$$j$$$ ($$$i \\ne j$$$) can be combined as a gift if $$$d_i + d_j$$$ is divisible by $$$k$$$.How many boxes will Polycarp be able to give? Of course, each box can be a part of no more than one gift. Polycarp cannot use boxes \"partially\" or redistribute candies between them.", "c_code": "int solution() {\n int n;\n int k;\n int res = 0;\n int res2 = 0;\n scanf(\"%d %d\", &n, &k);\n int d;\n int mods[101];\n for (int i = 0; i < 101; i++) {\n mods[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &d);\n if (d % k == 0) {\n res++;\n } else {\n mods[d % k]++;\n }\n }\n\n for (int i = 1; i < 101; i++) {\n\n if (mods[i] != 0 && mods[k - i] != 0) {\n if (i == k - i) {\n res2 += (mods[i] / 2) * 2;\n continue;\n }\n if (mods[i] < mods[k - i]) {\n res2 += mods[i] * 2;\n mods[i] = 0;\n mods[k - i] = 0;\n } else {\n res2 += mods[k - i] * 2;\n mods[i] = 0;\n mods[k - i] = 0;\n }\n }\n }\n\n printf(\"%d\", ((res / 2) * 2) + res2);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut l1 = String::new();\n let mut l2 = String::new();\n stdin.read_line(&mut l1).unwrap();\n stdin.read_line(&mut l2).unwrap();\n let v1: Vec = l1.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let d: Vec = l2.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let k = v1[1] as usize;\n\n let mut group_count = vec![0i32; k];\n for x in &d {\n let g = (*x % k as i32) as usize;\n\n group_count[g] += 1;\n }\n\n let ans: i32 = (0..k)\n .map(|i| {\n if i == k - i || i == 0 {\n group_count[i] - group_count[i] % 2\n } else {\n std::cmp::min(group_count[i], group_count[k - i])\n }\n })\n .sum();\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0018", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

\n

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

\n

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

\n

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq A_i < i
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_2 ... A_N\n
\n
\n
\n
\n
\n

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1 1 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n2\n0\n0\n0\n
\n

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

\n

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

\n

The members numbered 3, 4, and 5 do not have immediate subordinates.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\n1 1 1 1 1 1 1 1 1\n
\n
\n
\n
\n
\n

Sample Output 2

9\n0\n0\n0\n0\n0\n0\n0\n0\n0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

7\n1 2 3 4 5 6\n
\n
\n
\n
\n
\n

Sample Output 3

1\n1\n1\n1\n1\n1\n0\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int m = 0;\n scanf(\"%d\", &n);\n int t[200005] = {0};\n for (int i = 0; i < n - 1; i++) {\n scanf(\"%d\", &m);\n t[m - 1]++;\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d\\n\", t[i]);\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 n: usize = iter.next().unwrap().parse().unwrap();\n let a: Vec = (0..n - 1)\n .map(|_| iter.next().unwrap().parse().unwrap())\n .collect();\n let mut ans = vec![0; n];\n\n for x in a {\n ans[x - 1] += 1;\n }\n\n println!(\n \"{}\",\n ans.into_iter()\n .map(|v| v.to_string())\n .collect::>()\n .join(\"\\n\")\n );\n}", "difficulty": "medium"} {"problem_id": "0019", "problem_description": "NIT, the cleaver, is new in town! Thousands of people line up to orz him. To keep his orzers entertained, NIT decided to let them solve the following problem related to $$$\\operatorname{or} z$$$. Can you solve this problem too?You are given a 1-indexed array of $$$n$$$ integers, $$$a$$$, and an integer $$$z$$$. You can do the following operation any number (possibly zero) of times: Select a positive integer $$$i$$$ such that $$$1\\le i\\le n$$$. Then, simutaneously set $$$a_i$$$ to $$$(a_i\\operatorname{or} z)$$$ and set $$$z$$$ to $$$(a_i\\operatorname{and} z)$$$. In other words, let $$$x$$$ and $$$y$$$ respectively be the current values of $$$a_i$$$ and $$$z$$$. Then set $$$a_i$$$ to $$$(x\\operatorname{or}y)$$$ and set $$$z$$$ to $$$(x\\operatorname{and}y)$$$. Here $$$\\operatorname{or}$$$ and $$$\\operatorname{and}$$$ denote the bitwise operations OR and AND respectively.Find the maximum possible value of the maximum value in $$$a$$$ after any number (possibly zero) of operations.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int z;\n scanf(\"%d %d\", &n, &z);\n int a[n + 10];\n int p[(2 * n) + 10];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n p[i] = z | a[i];\n }\n int max = p[0];\n for (int i = 0; i < n; i++) {\n if (max < p[i]) {\n max = p[i];\n }\n }\n printf(\"%d\\n\", max);\n }\n}", "rust_code": "fn solution() {\n input! {\n name = cf,\n t: usize\n }\n for _ in 0..t {\n input! {\n use cf,\n n: usize,\n z: u32,\n a: [u32; n]\n }\n let ans = a.iter().map(|a| z | a).max().unwrap();\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0020", "problem_description": "CQXYM is counting permutations length of $$$2n$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).A permutation $$$p$$$(length of $$$2n$$$) will be counted only if the number of $$$i$$$ satisfying $$$p_i<p_{i+1}$$$ is no less than $$$n$$$. For example: Permutation $$$[1, 2, 3, 4]$$$ will count, because the number of such $$$i$$$ that $$$p_i<p_{i+1}$$$ equals $$$3$$$ ($$$i = 1$$$, $$$i = 2$$$, $$$i = 3$$$). Permutation $$$[3, 2, 1, 4]$$$ won't count, because the number of such $$$i$$$ that $$$p_i<p_{i+1}$$$ equals $$$1$$$ ($$$i = 3$$$). CQXYM wants you to help him to count the number of such permutations modulo $$$1000000007$$$ ($$$10^9+7$$$).In addition, modulo operation is to get the remainder. For example: $$$7 \\mod 3=1$$$, because $$$7 = 3 \\cdot 2 + 1$$$, $$$15 \\mod 4=3$$$, because $$$15 = 4 \\cdot 3 + 3$$$.", "c_code": "int solution() {\n int test;\n scanf(\"%d\", &test);\n while (test--) {\n int num;\n scanf(\"%d\", &num);\n long long int fact = 1;\n for (int i = 3; i <= 2 * num; i++) {\n fact = (fact * i) % 1000000007;\n }\n printf(\"%lld\\n\", fact);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).expect(\"\");\n let t: i32 = t.trim().parse().expect(\"\");\n\n for _x in 0..t {\n let mut n = String::new();\n std::io::stdin().read_line(&mut n).expect(\"\");\n let n: u128 = n.trim().parse().expect(\"\");\n let mut result: u128 = 1;\n for x in 3..((2 * n) + 1) {\n result = result * x % 1000000007;\n }\n println!(\"{}\", result);\n }\n}", "difficulty": "easy"} {"problem_id": "0021", "problem_description": "Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either \"AB\" or \"BB\". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.For example, Zookeeper can use two such operations: AABABBA $$$\\to$$$ AABBA $$$\\to$$$ AAA.Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n\n for (int C = 0; C < T; C++) {\n char s[200005];\n scanf(\"%s\", s);\n int n = 0;\n while (s[n] != '\\0') {\n n++;\n }\n char buf[n];\n int bi = 0;\n\n for (int i = 0; i < n; i++) {\n buf[bi] = s[i];\n bi++;\n if (bi >= 2 && buf[bi - 2] == 'A' && buf[bi - 1] == 'B') {\n bi -= 2;\n }\n }\n\n int ans = bi;\n for (int i = 0; i < bi - 1; i++) {\n if (buf[i] == 'B' && buf[i + 1] == 'B') {\n i++;\n ans -= 2;\n }\n }\n\n printf(\"%d\\n\", ans);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let test: i32 = str.trim().parse().unwrap();\n for _ in 0..test {\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let s: Vec = str.trim().chars().collect();\n let mut d = 0;\n let mut last = 0;\n for i in s {\n if d == 0 {\n d = 1;\n if i == 'A' {\n last = 1;\n } else {\n last = 0;\n }\n } else {\n if i == 'A' {\n last += 1;\n d += 1;\n } else {\n if last > 0 {\n last -= 1;\n d -= 1;\n } else if d > 0 {\n d -= 1;\n } else {\n d = 1;\n }\n }\n }\n }\n println!(\"{}\", d);\n }\n}", "difficulty": "medium"} {"problem_id": "0022", "problem_description": "You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \\le x, y \\le r$$$, $$$x \\ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.", "c_code": "int solution() {\n int a;\n int b;\n scanf(\"%d\", &a);\n while (a--) {\n scanf(\"%d %*d\", &b);\n printf(\"%d %d\\n\", b, 2 * b);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut tt = String::new();\n stdin().read_line(&mut tt).ok();\n let t: u32 = tt.trim().parse().unwrap();\n for _i in 0..t {\n let mut lrs = String::new();\n stdin().read_line(&mut lrs).ok();\n let lr: Vec = lrs.split_whitespace().map(|x| x.parse().unwrap()).collect();\n if lr[0] * 2 <= lr[1] {\n println!(\"{} {}\", lr[0], lr[0] * 2);\n } else {\n println!(\"1 1\");\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0023", "problem_description": "The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement. UCDHP stores some secret information about meteors as an n × m table with integers in its cells. The order of meteors in the Universe is changing. That's why the main UCDHP module receives the following queries: The query to swap two table rows; The query to swap two table columns; The query to obtain a secret number in a particular table cell. As the main UCDHP module is critical, writing the functional of working with the table has been commissioned to you.", "c_code": "int solution() {\n int i;\n int j;\n int swaptmp;\n int n;\n int m;\n int k;\n char s[2];\n int p[1005][1005];\n int indexx[1005];\n int indexy[1005];\n scanf(\"%d%d%d\", &n, &m, &k);\n for (i = 0; i < n; i++) {\n indexx[i] = i;\n }\n for (i = 0; i < m; i++) {\n indexy[i] = i;\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n scanf(\"%d\", &p[i][j]);\n }\n }\n while (k--) {\n scanf(\"%s%d%d\", s, &i, &j);\n i -= 1;\n j -= 1;\n if (s[0] == 'c') {\n swaptmp = indexy[i];\n indexy[i] = indexy[j];\n indexy[j] = swaptmp;\n } else if (s[0] == 'r') {\n swaptmp = indexx[i];\n indexx[i] = indexx[j];\n indexx[j] = swaptmp;\n } else {\n printf(\"%d\\n\", p[indexx[i]][indexy[j]]);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let stdin_lock = stdin.lock();\n let mut lines = stdin_lock.lines();\n let mut stdout_lock = BufWriter::new(stdout.lock());\n let (n, m, k) = {\n let tmp: Vec<_> = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().ok().unwrap())\n .collect();\n (tmp[0], tmp[1], tmp[2])\n };\n let a: Vec> = (0..n)\n .map(|_| {\n lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().ok().unwrap())\n .collect()\n })\n .collect();\n let mut ii: Vec<_> = (0..n).collect();\n let mut jj: Vec<_> = (0..m).collect();\n (0..k).for_each(|_| {\n let (c, x, y): (_, usize, usize) = {\n let line = lines.next().unwrap().unwrap();\n let tmp: Vec<_> = line.split(' ').collect();\n (\n tmp[0].parse().ok().unwrap(),\n tmp[1].parse().ok().unwrap(),\n tmp[2].parse().ok().unwrap(),\n )\n };\n\n match c {\n 'r' => {\n ii.swap(x - 1, y - 1);\n }\n 'c' => {\n jj.swap(x - 1, y - 1);\n }\n _ => {\n writeln!(stdout_lock, \"{}\", a[ii[x - 1]][jj[y - 1]]);\n }\n }\n });\n}", "difficulty": "easy"} {"problem_id": "0024", "problem_description": "The title is a reference to the very first Educational Round from our writers team, Educational Round 18.There is a bag, containing colored balls. There are $$$n$$$ different colors of balls, numbered from $$$1$$$ to $$$n$$$. There are $$$\\mathit{cnt}_i$$$ balls of color $$$i$$$ in the bag. The total amount of balls in the bag is odd (e. g. $$$\\mathit{cnt}_1 + \\mathit{cnt}_2 + \\dots + \\mathit{cnt}_n$$$ is odd).In one move, you can choose two balls with different colors and take them out of the bag.At some point, all the remaining balls in the bag will have the same color. That's when you can't make moves anymore.Find any possible color of the remaining balls.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n int cnt[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &cnt[j]);\n }\n\n int a = 0;\n int b = 1;\n while (a < n && b < n) {\n cnt[a]--;\n cnt[b]--;\n while (cnt[a] == 0) {\n a++;\n }\n while (cnt[b] == 0 || a == b) {\n b++;\n }\n }\n\n for (int j = 0; j < n; j++) {\n if (cnt[j]) {\n printf(\"%d\\n\", j + 1);\n }\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut num = String::new();\n stdin.read_line(&mut num).expect(\"Failed to parse line\");\n let num: i32 = num.trim().parse().expect(\"Please type a number!\");\n for _ in 0..num {\n let mut num2 = String::new();\n stdin.read_line(&mut num2).expect(\"Failed to parse line\");\n let mut line = String::new();\n stdin.read_line(&mut line).expect(\"Failed to parse line\");\n let balls: Vec = line\n .split(\" \")\n .map(|s| s.trim().parse().unwrap())\n .enumerate()\n .map(|(idx, n)| Balls { idx, count: n })\n .collect();\n\n if balls.len() < 2 {\n println!(\"{}\", 1);\n continue;\n }\n\n let mut numbers: Vec = balls;\n loop {\n numbers = numbers\n .iter()\n .map(|balls| Balls {\n idx: balls.idx,\n count: balls.count - 1,\n })\n .filter(|balls| balls.count > 0)\n .collect();\n if numbers.is_empty() {\n println!(\"{}\", 1);\n break;\n }\n if numbers.len() == 1 {\n println!(\"{}\", numbers[0].idx + 1);\n break;\n }\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0025", "problem_description": "There are $$$n$$$ people who want to participate in a boat competition. The weight of the $$$i$$$-th participant is $$$w_i$$$. Only teams consisting of two people can participate in this competition. As an organizer, you think that it's fair to allow only teams with the same total weight.So, if there are $$$k$$$ teams $$$(a_1, b_1)$$$, $$$(a_2, b_2)$$$, $$$\\dots$$$, $$$(a_k, b_k)$$$, where $$$a_i$$$ is the weight of the first participant of the $$$i$$$-th team and $$$b_i$$$ is the weight of the second participant of the $$$i$$$-th team, then the condition $$$a_1 + b_1 = a_2 + b_2 = \\dots = a_k + b_k = s$$$, where $$$s$$$ is the total weight of each team, should be satisfied.Your task is to choose such $$$s$$$ that the number of teams people can create is the maximum possible. Note that each participant can be in no more than one team.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int W[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &W[i]);\n }\n int B[101][2500];\n int P[101] = {0};\n for (int i = 0; i < n - 1; i++) {\n for (int j = i + 1; j < n; j++) {\n B[W[i] + W[j]][P[W[i] + W[j]]++] = i;\n B[W[i] + W[j]][P[W[i] + W[j]]++] = j;\n }\n }\n int max = 0;\n for (int i = 0; i < 101; i++) {\n int X[n];\n for (int k = 0; k < n; k++) {\n X[k] = 0;\n }\n int size = 0;\n for (int j = 0; j < P[i] - 1; j = j + 2) {\n if (X[B[i][j]] != -1 && X[B[i][j + 1]] != -1) {\n size++;\n X[B[i][j]] = -1;\n X[B[i][j + 1]] = -1;\n }\n }\n if (size > max) {\n max = size;\n }\n }\n printf(\"%d\\n\", max);\n }\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 w: 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 c: Vec = vec![0; 100];\n for i in 0..n {\n c[w[i]] += 1;\n }\n\n let mut ans = 0;\n for s in 2..=100 {\n let mut res = 0;\n for x in 1..s {\n res += cmp::min(c[x], c[s - x]);\n }\n ans = cmp::max(ans, res / 2);\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0026", "problem_description": "You are given integer $$$n$$$. You have to arrange numbers from $$$1$$$ to $$$2n$$$, using each of them exactly once, on the circle, so that the following condition would be satisfied:For every $$$n$$$ consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard $$$2n$$$ numbers differ not more than by $$$1$$$.For example, choose $$$n = 3$$$. On the left you can see an example of a valid arrangement: $$$1 + 4 + 5 = 10$$$, $$$4 + 5 + 2 = 11$$$, $$$5 + 2 + 3 = 10$$$, $$$2 + 3 + 6 = 11$$$, $$$3 + 6 + 1 = 10$$$, $$$6 + 1 + 4 = 11$$$, any two numbers differ by at most $$$1$$$. On the right you can see an invalid arrangement: for example, $$$5 + 1 + 6 = 12$$$, and $$$3 + 2 + 4 = 9$$$, $$$9$$$ and $$$12$$$ differ more than by $$$1$$$.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n if (n & 1) {\n puts(\"YES\");\n printf(\"1 \");\n for (int i = 1; i <= (n - 1) / 2; ++i) {\n printf(\"%d %d \", 4 * i, (4 * i) + 1);\n }\n for (int i = 1; i <= (n - 1) / 2; ++i) {\n printf(\"%d %d \", (4 * i) - 2, (4 * i) + 1 - 2);\n }\n printf(\"%d\\n\", 2 * n);\n } else {\n puts(\"NO\");\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 n = input.trim().parse::().unwrap();\n\n if n % 2 == 0 {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n\n print!(\"1\");\n for i in (4..(2 * n)).step_by(4) {\n print!(\" {}\", i);\n print!(\" {}\", i + 1);\n }\n for i in (2..(2 * n)).step_by(4) {\n print!(\" {}\", i);\n print!(\" {}\", i + 1);\n }\n println!(\" {}\", 2 * n);\n }\n}", "difficulty": "easy"} {"problem_id": "0027", "problem_description": "Alice has a grid with $$$2$$$ rows and $$$n$$$ columns. She fully covers the grid using $$$n$$$ dominoes of size $$$1 \\times 2$$$ — Alice may place them vertically or horizontally, and each cell should be covered by exactly one domino.Now, she decided to show one row of the grid to Bob. Help Bob and figure out what the other row of the grid looks like!", "c_code": "int solution() {\n int tc = 0;\n scanf(\"%d\", &tc);\n getchar();\n\n for (int i = 0; i < tc; i++) {\n int n = 0;\n scanf(\"%d\", &n);\n getchar();\n\n char chars[100] = {0};\n for (int j = 0; j < n; j++) {\n scanf(\"%c\", &chars[j]);\n }\n getchar();\n\n char result[100] = {0};\n for (int j = 0; j < n; j++) {\n if (chars[j] == 'U') {\n result[j] = 'D';\n } else if (chars[j] == 'D') {\n result[j] = 'U';\n } else {\n result[j] = chars[j];\n }\n }\n\n for (int j = 0; j < n; j++) {\n printf(\"%c\", result[j]);\n }\n puts(\"\");\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(2).step_by(2) {\n let s = l\n .unwrap()\n .chars()\n .map(|c| match c {\n 'U' => 'D',\n 'D' => 'U',\n _ => c,\n })\n .collect::();\n writeln!(o, \"{}\", s).ok();\n }\n}", "difficulty": "easy"} {"problem_id": "0028", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.

\n

When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.

\n

Then, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.

\n

Now, he goes through the following process:

\n
    \n
  • Submit the code.
  • \n
  • Wait until the code finishes execution on all the cases.
  • \n
  • If the code fails to correctly solve some of the M cases, submit it again.
  • \n
  • Repeat until the code correctly solve all the cases in one submission.
  • \n
\n

Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq M \\leq {\\rm min}(N, 5)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\n
\n
\n
\n
\n
\n

Output

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 1\n
\n
\n
\n
\n
\n

Sample Output 1

3800\n
\n

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.

\n

The code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.

\n

Thus, the answer is 1900 \\times 1/2 + (2 \\times 1900) \\times 1/4 + (3 \\times 1900) \\times 1/8 + ... = 3800.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 2\n
\n
\n
\n
\n
\n

Sample Output 2

18400\n
\n

The code will take 1900 milliseconds in each of the 2 cases, and 100 milliseconds in each of the 10-2=8 cases. The probability of the code correctly solving all the cases is 1/2 \\times 1/2 = 1/4.

\n
\n
\n
\n
\n
\n

Sample Input 3

100 5\n
\n
\n
\n
\n
\n

Sample Output 3

608000\n
\n
\n
", "c_code": "int solution() {\n double N;\n double M;\n scanf(\"%lf %lf\", &N, &M);\n int i = 1;\n double exp = 0.0;\n while (i * ((N - M) * 100.0 + 1900.0 * M) * pow(1 - pow(0.50, M), i - 1) *\n pow(0.50, M) >\n 0.01) {\n exp += i * ((N - M) * 100.0 + 1900.0 * M) * pow(1 - pow(0.50, M), i - 1) *\n pow(0.50, M);\n i++;\n }\n\n printf(\"%.0f\", exp);\n\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 v: Vec = s.trim().split(\" \").map(|x| x.parse().unwrap()).collect();\n println!(\"{}\", (100 * (v[0] - v[1]) + 1900 * v[1]) << v[1]);\n}", "difficulty": "hard"} {"problem_id": "0029", "problem_description": "

ゼッケンの交換 (Swapping Bibs)

\n\n\n

問題

\n

\nJOI 高校の N 人の生徒が東西に一列に並んでいる.列の西の端から i 番目の生徒が生徒 i である.それぞれの生徒は整数が 1 つ書かれたゼッケンを付けている.最初,生徒 i のゼッケンには整数 Ai が書かれている.\n

\n\n

\nバトンが M 個あり,バトンには 1 から M までの番号が付けられている.k = 1, 2, ..., M に対し,以下の操作を行う.バトン k (2 ≦ k ≦ M) に関する操作は,バトン k - 1 に関する操作が終わってから行う.\n

\n\n
    \n
  1. \n先生がバトン k を生徒 1 に渡す.\n
  2. \n
  3. \nバトンを受け取った生徒は,以下のルールに従ってバトンを渡す.\n\n
      \n
    • \nルール:生徒 i がバトン k を受け取ったとする.\n
        \n
      • 1 ≦ i ≦ N - 1 のとき: 生徒 i のゼッケンの整数を k で割った余りが,生徒 i + 1 のゼッケンの整数を k で割った余りよりも大きいとき,生徒 i と生徒 i + 1 がゼッケンを交換し,生徒 i は生徒 i + 1 にバトンを渡す.そうでないときは,ゼッケンを交換せずに,生徒 i は生徒 i + 1 にバトンを渡す.
      • \n
      • i = N のとき: 生徒 N はバトンを先生に渡す.
      • \n
      \n
    \n
  4. \n
  5. 先生が生徒 N からバトン k を受け取ったら,バトン k に関する操作は終わりである.\n
  6. \n\n
\n\n

\n生徒のゼッケンに最初に書かれていた整数とバトンの個数 M が与えられたとき,先生が生徒 N からバトン M を受け取った後の,それぞれの生徒のゼッケンの整数を求めるプログラムを作成せよ.\n

\n\n

入力

\n

\n入力は 1 + N 行からなる.\n

\n\n

\n1 行目には整数 N, M (1 ≦ N ≦ 100, 1 ≦ M ≦ 100) が空白を区切りとして書かれており,それぞれ生徒の人数とバトンの個数を表す.\n

\n\n

\n続く N 行のうちの i 行目 (1 ≦ i ≦ N) には整数 Ai (1 ≦ Ai ≦ 1000) が書かれており,生徒 i のゼッケンに最初に書かれている整数 Ai を表す.\n

\n\n

出力

\n

\n出力は N 行からなる.i 行目 (1 ≦ i ≦ N) には,先生が生徒 N からバトン M を受け取った後の,生徒 i のゼッケンの整数を出力せよ.\n

\n\n

入出力例

\n\n\n

入力例 1

\n\n
\n6 4\n3\n2\n8\n3\n1\n5\n
\n\n

出力例 1

\n\n
\n2\n3\n1\n8\n5\n3\n
\n\n

入力例 2

\n\n
\n10 6\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n
\n\n\n

出力例 2

\n\n
\n6\n1\n2\n3\n10\n4\n8\n7\n9\n5\n
\n\n\n

\n入出力例 1 では 6 人の生徒がいる.最初,生徒のゼッケンは順に 3, 2, 8, 3, 1, 5 である.バトンは 4 個ある.\n

\n
    \n
  • \nバトン 1 に関する操作が終了した時点での生徒のゼッケンは順に 3, 2, 8, 3, 1, 5 である.\n
  • \n
  • \nバトン 2 に関する操作が終了した時点での生徒のゼッケンは順に 2, 8, 3, 3, 1, 5 である.\n
  • \n
  • \nバトン 3 に関する操作が終了した時点での生徒のゼッケンは順に 2, 3, 3, 1, 8, 5 である.\n
  • \n
  • \nバトン 4 に関する操作が終了した時点での生徒のゼッケンは順に 2, 3, 1, 8, 5, 3 である.\n
  • \n
\n\n\n\n", "c_code": "int solution(void) {\n int n;\n int m;\n int bib[128] = {0};\n int swh;\n int i;\n int j;\n scanf(\"%d%d\", &n, &m);\n for (i = 1; i <= n; i++) {\n scanf(\"%d\", &bib[i]);\n }\n for (i = 1; i <= m; i++) {\n for (j = 1; j < n; j++) {\n if (bib[j] % i > bib[j + 1] % i) {\n swh = bib[j];\n bib[j] = bib[j + 1];\n bib[j + 1] = swh;\n }\n }\n }\n for (i = 1; i <= n; i++) {\n printf(\"%d\\n\", bib[i]);\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 (n, m) = {\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 vec = Vec::with_capacity(n);\n vec.extend(lines.take(n).map(|l| l.parse::().unwrap()));\n\n for k in 2..(m + 1) {\n for i in 0..(n - 1) {\n if vec[i] % k > vec[i + 1] % k {\n vec.swap(i, i + 1);\n }\n }\n }\n\n let mut result = String::with_capacity(n * 4);\n for a in vec {\n writeln!(result, \"{}\", a);\n }\n print!(\"{}\", result);\n}", "difficulty": "medium"} {"problem_id": "0030", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Joisino is planning to open a shop in a shopping street.

\n

Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods.

\n

There are already N stores in the street, numbered 1 through N.

\n

You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2.

\n

Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}.

\n

Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤100
  • \n
  • 0≤F_{i,j,k}≤1
  • \n
  • For every integer i such that 1≤i≤N, there exists at least one pair (j,k) such that F_{i,j,k}=1.
  • \n
  • -10^7≤P_{i,j}≤10^7
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nF_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2}\n:\nF_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2}\nP_{1,0} ... P_{1,10}\n:\nP_{N,0} ... P_{N,10}\n
\n
\n
\n
\n
\n

Output

Print the maximum possible profit of Joisino's shop.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1\n1 1 0 1 0 0 0 1 0 1\n3 4 5 6 7 8 9 -2 -3 4 -2\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

If her shop is open only during the periods when Shop 1 is opened, the profit will be 8, which is the maximum possible profit.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n1 1 1 1 1 0 0 0 0 0\n0 0 0 0 0 1 1 1 1 1\n0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n
\n
\n
\n
\n
\n

Sample Output 2

-2\n
\n

Note that a shop must be open during at least one period, and the profit may be negative.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n1 1 1 1 1 1 0 0 1 1\n0 1 0 1 1 1 1 0 1 0\n1 0 1 1 0 1 0 1 0 1\n-8 6 -2 -8 -8 4 8 7 -6 2 2\n-9 2 0 1 7 -5 0 -2 -6 5 5\n6 -6 7 -9 6 -5 8 0 -9 -7 -7\n
\n
\n
\n
\n
\n

Sample Output 3

23\n
\n
\n
", "c_code": "int solution() {\n int N;\n scanf(\"%d\", &N);\n\n int F[100][14];\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < 10; j++) {\n scanf(\"%d\", &F[i][j]);\n }\n }\n\n int P[100][15];\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < 11; j++) {\n scanf(\"%d\", &P[i][j]);\n }\n }\n\n int res = -(1 << 30);\n for (int b = 1; b < (1 << 10); b++) {\n int cc = 0;\n for (int i = 0; i < N; i++) {\n int c = 0;\n for (int j = 0; j < 10; j++) {\n if ((b >> j & 1) && F[i][j]) {\n c++;\n }\n }\n\n cc += P[i][c];\n }\n if (res < cc) {\n res = cc;\n }\n }\n\n printf(\"%d\\n\", res);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N: usize,\n F: [ [usize; 10]; N],\n P: [ [i64; 11]; N],\n }\n\n let mut max = std::i64::MIN;\n for ind in 1..(1 << 10) {\n let mut c = vec![0_usize; N];\n let mut res = ind;\n let mut count = 0;\n while res > 0 {\n let is = res & 1;\n for i in 0..N {\n c[i] += F[i][count] * is;\n }\n res >>= 1;\n count += 1;\n }\n let mut sum = 0;\n for (ind, cc) in c.iter().enumerate() {\n sum += P[ind][*cc];\n }\n max = cmp::max(max, sum);\n }\n println!(\"{}\", max);\n}", "difficulty": "medium"} {"problem_id": "0031", "problem_description": "The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts $$$h$$$ hours and each hour lasts $$$m$$$ minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from $$$0$$$ to $$$h-1$$$ and minutes are numbered from $$$0$$$ to $$$m-1$$$. That's how the digits are displayed on the clock. Please note that digit $$$1$$$ is placed in the middle of its position. A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).The image of the clocks in the mirror is reflected against a vertical axis. The reflection is not a valid time.The reflection is a valid time with $$$h=24$$$, $$$m = 60$$$. However, for example, if $$$h=10$$$, $$$m=60$$$, then the reflection is not a valid time. An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment $$$s$$$ and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.It can be shown that with any $$$h$$$, $$$m$$$, $$$s$$$ such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.You are asked to solve the problem for several test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 1; i <= t; i++) {\n int h;\n int m;\n int flag = 0;\n char c;\n scanf(\"%d%d\", &h, &m);\n getchar();\n int arr[4];\n for (int j = 0; j < 4; j++) {\n if (j == 2) {\n getchar();\n }\n scanf(\"%c\", &c);\n arr[j] = c - '0';\n }\n while (1) {\n flag = 0;\n for (int j = 0; j < 4; j++) {\n if (arr[j] == 3 || arr[j] == 4 || arr[j] == 6 || arr[j] == 7 ||\n arr[j] == 9) {\n flag = 1;\n }\n if (flag == 0) {\n int a[4];\n for (int k = 0; k < 4; k++) {\n if (arr[k] == 2) {\n a[k] = 5;\n } else if (arr[k] == 5) {\n a[k] = 2;\n } else {\n a[k] = arr[k];\n }\n }\n if (a[3] * 10 + a[2] >= h || a[1] * 10 + a[0] >= m) {\n flag = 1;\n }\n }\n }\n if (flag == 1) {\n if (arr[3] + 1 < 10 && arr[2] * 10 + arr[3] + 1 < m) {\n arr[3]++;\n } else if (arr[2] * 10 + arr[3] + 1 < m) {\n arr[3] = 0;\n arr[2]++;\n } else if (arr[0] * 10 + arr[1] + 1 < h) {\n if (arr[1] + 1 < 10) {\n arr[1]++;\n arr[2] = arr[3] = 0;\n } else {\n arr[0]++;\n arr[1] = arr[2] = arr[3] = 0;\n }\n } else {\n arr[0] = arr[1] = arr[2] = arr[3] = 0;\n }\n } else {\n for (int j = 0; j < 4; j++) {\n if (j == 2) {\n printf(\":\");\n }\n printf(\"%d\", arr[j]);\n }\n printf(\"\\n\");\n break;\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let _t = it.next().unwrap();\n\n let a = [\n Some(0),\n Some(1),\n Some(5),\n None,\n None,\n Some(2),\n None,\n None,\n Some(8),\n None,\n ];\n\n while let Some(h) = it.next().map(|x| x.parse::().unwrap()) {\n let m = it.next().map(|x| x.parse::().unwrap()).unwrap();\n let s = it.next().unwrap();\n\n let mut it = s.split(':').map(|x| x.parse::().unwrap());\n let mut h0 = it.next().unwrap();\n let mut m0 = it.next().unwrap();\n\n loop {\n if let Some(d0) = a[m0 % 10]\n && let Some(d1) = a[m0 / 10]\n && let Some(d2) = a[h0 % 10]\n && let Some(d3) = a[h0 / 10] {\n let h1 = d0 * 10 + d1;\n let m1 = d2 * 10 + d3;\n\n if h1 < h && m1 < m {\n println!(\"{:02}:{:02}\", h0, m0);\n break;\n }\n }\n\n m0 += 1;\n if m0 == m {\n m0 = 0;\n h0 += 1;\n if h0 == h {\n h0 = 0;\n }\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0032", "problem_description": "You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.$$$n$$$ reviewers enter the site one by one. Each reviewer is one of the following types: type $$$1$$$: a reviewer has watched the movie, and they like it — they press the upvote button; type $$$2$$$: a reviewer has watched the movie, and they dislike it — they press the downvote button; type $$$3$$$: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie. Each reviewer votes on the movie exactly once.Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?", "c_code": "int solution() {\n int n;\n int count = 0;\n scanf(\"%d\", &n);\n int a[n][51];\n int b[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &b[i]);\n for (int j = 0; j < b[i]; j++) {\n scanf(\"%d\", &a[i][j]);\n }\n }\n for (int i = 0; i < n; i++) {\n count = 0;\n for (int j = 0; j < b[i]; j++) {\n if (a[i][j] != 2) {\n ++count;\n }\n }\n printf(\"%d\\n\", count);\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 for l in i.lock().lines().skip(2).step_by(2) {\n let s = l\n .unwrap()\n .split(' ')\n .fold(0, |s, c| if c != \"2\" { s + 1 } else { s });\n writeln!(o, \"{}\", s).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "0033", "problem_description": "You are given two arrays $$$a$$$ and $$$b$$$, consisting of $$$n$$$ integers each.Let's define a function $$$f(a, b)$$$ as follows: let's define an array $$$c$$$ of size $$$n$$$, where $$$c_i = a_i \\oplus b_i$$$ ($$$\\oplus$$$ denotes bitwise XOR); the value of the function is $$$c_1 \\mathbin{\\&} c_2 \\mathbin{\\&} \\cdots \\mathbin{\\&} c_n$$$ (i.e. bitwise AND of the entire array $$$c$$$). Find the maximum value of the function $$$f(a, b)$$$ if you can reorder the array $$$b$$$ in an arbitrary way (leaving the initial order is also an option).", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n int a[100000] = {};\n int b[100000] = {};\n\n int res = 0;\n\n int work_a[100000] = {};\n int work_b[100000] = {};\n int idx[100001] = {};\n int work_idx[100001] = {};\n\n res = scanf(\"%d\", &t);\n\n while (t > 0) {\n int ans = 0;\n int idx_num = 1;\n res = scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n res = scanf(\"%d\", a + i);\n }\n for (int i = 0; i < n; i++) {\n res = scanf(\"%d\", b + i);\n }\n idx[0] = 0;\n idx[1] = n;\n idx_num = 1;\n for (int i = 29; i >= 0; i--) {\n int is_ok = 1;\n int work_idx_num = 0;\n for (int j = 0; j < idx_num; j++) {\n int work_a_num = 0;\n int work_b_num = 0;\n for (int k = idx[j]; k < idx[j + 1]; k++) {\n if ((a[k] & (1 << i)) > 0) {\n work_a[work_a_num] = a[k];\n work_a_num++;\n }\n if ((b[k] & (1 << i)) <= 0) {\n work_b[work_b_num] = b[k];\n work_b_num++;\n }\n }\n if (work_a_num == work_b_num) {\n work_idx[work_idx_num] = idx[j];\n work_idx_num++;\n if (work_a_num > 0 && work_a_num < idx[j + 1] - idx[j]) {\n work_idx[work_idx_num] = idx[j] + work_a_num;\n work_idx_num++;\n }\n for (int k = idx[j]; k < idx[j + 1]; k++) {\n if ((a[k] & (1 << i)) <= 0) {\n work_a[work_a_num] = a[k];\n work_a_num++;\n }\n if ((b[k] & (1 << i)) > 0) {\n work_b[work_b_num] = b[k];\n work_b_num++;\n }\n }\n for (int k = idx[j]; k < idx[j + 1]; k++) {\n a[k] = work_a[k - idx[j]];\n b[k] = work_b[k - idx[j]];\n }\n } else {\n is_ok = 0;\n }\n }\n if (is_ok > 0) {\n ans += (1 << i);\n for (int j = 0; j < work_idx_num; j++) {\n idx[j] = work_idx[j];\n }\n idx_num = work_idx_num;\n idx[idx_num] = n;\n }\n }\n printf(\"%d\\n\", ans);\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n for _ in 0..t {\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 A: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n let B: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let mut ans = 0;\n let mut que = Vec::new();\n que.push((A, B));\n\n for i in (0..30).rev() {\n let bit = 1 << i;\n\n let mut que2 = Vec::new();\n let mut ok = true;\n\n for (a, b) in que.iter() {\n let mut a0 = Vec::new();\n let mut a1 = Vec::new();\n let mut b0 = Vec::new();\n let mut b1 = Vec::new();\n\n for &aa in a.iter() {\n if aa & bit != 0 {\n a1.push(aa);\n } else {\n a0.push(aa);\n }\n }\n for &bb in b.iter() {\n if bb & bit != 0 {\n b1.push(bb);\n } else {\n b0.push(bb);\n }\n }\n if a0.len() == b1.len() {\n if !a0.is_empty() {\n que2.push((a0, b1));\n }\n if !a1.is_empty() {\n que2.push((a1, b0));\n }\n } else {\n ok = false;\n break;\n }\n }\n\n if ok {\n ans |= bit;\n que = que2;\n }\n }\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0034", "problem_description": "Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white. Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.", "c_code": "int solution() {\n int n;\n while (scanf(\"%d\", &n) != EOF) {\n char a[4][100][100];\n char b[100][100];\n char c[100][100];\n int i;\n int j;\n int k;\n int d[4];\n int e[4];\n int sum = 0;\n int ans = 1e9;\n memset(d, 0, sizeof(d));\n memset(e, 0, sizeof(e));\n for (k = 0; k < 4; k++) {\n for (i = 0; i < n; i++) {\n scanf(\"%s\", a[k][i]);\n }\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < n; j++) {\n if ((i + j) % 2 == 0) {\n b[i][j] = '0';\n }\n if ((i + j) % 2 == 1) {\n b[i][j] = '1';\n }\n if ((i + j) % 2 == 0) {\n c[i][j] = '1';\n }\n if ((i + j) % 2 == 1) {\n c[i][j] = '0';\n }\n }\n }\n for (k = 0; k < 4; k++) {\n for (i = 0; i < n; i++) {\n for (j = 0; j < n; j++) {\n if (a[k][i][j] != b[i][j]) {\n d[k]++;\n }\n if (a[k][i][j] != c[i][j]) {\n e[k]++;\n }\n }\n }\n }\n for (i = 0; i < 4; i++) {\n sum = sum + e[i];\n }\n\n for (i = 0; i < 4; i++) {\n for (j = i + 1; j < 4; j++) {\n if ((d[i] + d[j] + sum - e[i] - e[j]) < ans) {\n ans = d[i] + d[j] + sum - e[i] - e[j];\n }\n }\n }\n printf(\"%d\", ans);\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input);\n let n = input.trim().parse::().unwrap();\n\n let mut boards = Vec::new();\n for i in 0..4 {\n let mut input = String::new();\n let mut mini_board = Vec::new();\n if i != 0 {\n io::stdin().read_line(&mut input);\n input = String::new();\n }\n for _j in 0..n {\n let mut input = String::new();\n io::stdin().read_line(&mut input);\n let black_vec: Vec = input.trim().chars().map(|c| c == '1').collect();\n mini_board.push(black_vec);\n }\n boards.push(mini_board);\n }\n\n let mut incorrect_vec: Vec = vec![0; 4];\n for i in 0..4 {\n for x in 0..n {\n for y in 0..n {\n let black_sq = (x + y) % 2 == 0;\n if black_sq != boards[i as usize][x as usize][y as usize] {\n incorrect_vec[i as usize] += 1;\n }\n }\n }\n }\n\n incorrect_vec.sort();\n\n println!(\n \"{}\",\n incorrect_vec[0] + incorrect_vec[1] + 2 * n * n - incorrect_vec[2] - incorrect_vec[3]\n );\n}", "difficulty": "medium"} {"problem_id": "0035", "problem_description": "Polycarp has an integer $$$n$$$ that doesn't contain the digit 0. He can do the following operation with his number several (possibly zero) times: Reverse the prefix of length $$$l$$$ (in other words, $$$l$$$ leftmost digits) of $$$n$$$. So, the leftmost digit is swapped with the $$$l$$$-th digit from the left, the second digit from the left swapped with ($$$l-1$$$)-th left, etc. For example, if $$$n=123456789$$$ and $$$l=5$$$, then the new value of $$$n$$$ will be $$$543216789$$$.Note that for different operations, the values of $$$l$$$ can be different. The number $$$l$$$ can be equal to the length of the number $$$n$$$ — in this case, the whole number $$$n$$$ is reversed.Polycarp loves even numbers. Therefore, he wants to make his number even. At the same time, Polycarp is very impatient. He wants to do as few operations as possible.Help Polycarp. Determine the minimum number of operations he needs to perform with the number $$$n$$$ to make it even or determine that this is impossible.You need to answer $$$t$$$ independent test cases.", "c_code": "int solution(void) {\n int n;\n int num;\n int exist = 0;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &num);\n if (!(num & 1)) {\n printf(\"0\\n\");\n continue;\n }\n while (num != 0) {\n if (!(num & 1)) {\n exist = 1;\n }\n if (num / 10 == 0) {\n if (!(num & 1)) {\n printf(\"1\\n\");\n break;\n }\n if (exist == 1) {\n printf(\"2\\n\");\n break;\n } else\n printf(\"-1\\n\");\n }\n num /= 10;\n }\n exist = 0;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input: String = String::new();\n io::stdin().read_line(&mut input).expect(\"invalid input\");\n\n let t: i32 = input.trim().parse().unwrap();\n\n for _ in 0..t {\n let mut input_str: String = String::new();\n io::stdin().read_line(&mut input_str).expect(\"invalid str\");\n\n let s: String = input_str.trim().parse().unwrap();\n\n let first_digit: i32 = s.chars().next().unwrap() as i32;\n let last_digit: i32 = s.chars().last().unwrap() as i32;\n\n if last_digit % 2 == 0 {\n println!(\"0\");\n continue;\n }\n if first_digit % 2 == 0 {\n println!(\"1\");\n continue;\n }\n\n let count_2 = s.matches('2').count();\n let count_4 = s.matches('4').count();\n let count_6 = s.matches('6').count();\n let count_8 = s.matches('8').count();\n\n if count_2 > 0 || count_4 > 0 || count_6 > 0 || count_8 > 0 {\n println!(\"2\");\n continue;\n }\n println!(\"-1\");\n }\n}", "difficulty": "hard"} {"problem_id": "0036", "problem_description": "Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it.After taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies.Alice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies).You have to answer $$$q$$$ independent queries.Let's see the following example: $$$[1, 3, 4]$$$. Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob — then Alice has $$$4$$$ candies, and Bob has $$$4$$$ candies.Another example is $$$[1, 10, 100]$$$. Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes $$$54$$$ candies, and Alice takes $$$46$$$ candies. Now Bob has $$$55$$$ candies, and Alice has $$$56$$$ candies, so she has to discard one candy — and after that, she has $$$55$$$ candies too.", "c_code": "int solution() {\n long long n;\n scanf(\"%lld\", &n);\n while (n--) {\n long long a[3];\n long long sum = 0;\n for (int i = 0; i < 3; i++) {\n scanf(\"%lld\", &a[i]);\n sum = sum + a[i];\n }\n printf(\"%lld\\n\", sum / 2);\n }\n return 0;\n}", "rust_code": "fn solution() {\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 for _ in 0..n {\n let (a, b, c) = {\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 )\n };\n\n println!(\"{}\", (a + b + c) / 2);\n }\n}", "difficulty": "medium"} {"problem_id": "0037", "problem_description": "Numbers $$$1, 2, 3, \\dots n$$$ (each integer from $$$1$$$ to $$$n$$$ once) are written on a board. In one operation you can erase any two numbers $$$a$$$ and $$$b$$$ from the board and write one integer $$$\\frac{a + b}{2}$$$ rounded up instead.You should perform the given operation $$$n - 1$$$ times and make the resulting number that will be left on the board as small as possible. For example, if $$$n = 4$$$, the following course of action is optimal: choose $$$a = 4$$$ and $$$b = 2$$$, so the new number is $$$3$$$, and the whiteboard contains $$$[1, 3, 3]$$$; choose $$$a = 3$$$ and $$$b = 3$$$, so the new number is $$$3$$$, and the whiteboard contains $$$[1, 3]$$$; choose $$$a = 1$$$ and $$$b = 3$$$, so the new number is $$$2$$$, and the whiteboard contains $$$[2]$$$. It's easy to see that after $$$n - 1$$$ operations, there will be left only one number. Your goal is to minimize it.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n printf(\"2\\n\");\n printf(\"%d %d\\n\", n - 1, n);\n while (n >= 3) {\n printf(\"%d %d\\n\", n - 2, n);\n n--;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let test: i32 = str.trim().parse().unwrap();\n for _ in 0..test {\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let x: usize = str.trim().parse().unwrap();\n let mut ave = x;\n for i in 1..x {\n ave = (ave + x - i).div_ceil(2);\n }\n println!(\"{}\", ave);\n ave = x;\n for i in 1..x {\n println!(\"{} {}\", ave, x - i);\n ave = (ave + x - i).div_ceil(2);\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0038", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^4
  • \n
  • 1 \\leq A \\leq B \\leq 36
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B\n
\n
\n
\n
\n
\n

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

20 2 5\n
\n
\n
\n
\n
\n

Sample Output 1

84\n
\n

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 1 2\n
\n
\n
\n
\n
\n

Sample Output 2

13\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100 4 16\n
\n
\n
\n
\n
\n

Sample Output 3

4554\n
\n
\n
", "c_code": "int solution() {\n int N = 0;\n int A = 0;\n int B = 0;\n int i = 0;\n int ans = 0;\n scanf(\"%d %d %d\", &N, &A, &B);\n for (i = 1; i <= N; i++) {\n int val = 0;\n val = (i / 10000) + ((i % 10000) / 1000) + ((i % 1000) / 100) +\n ((i % 100) / 10) + (i % 10);\n if ((A <= val) && (val <= B)) {\n\n ans += i;\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n\n let mut n: Vec = Vec::new();\n\n for x in vec {\n n.push(x.parse().unwrap());\n }\n\n let mut ans: i32 = 0;\n for x in 1..n[0] + 1 {\n let mut cnt: i32 = 0;\n let mut tmp: i32 = x;\n while tmp != 0 {\n cnt += tmp % 10;\n tmp /= 10;\n }\n if cnt <= n[2] && cnt >= n[1] {\n ans += x\n };\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0039", "problem_description": "You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.", "c_code": "int solution() {\n int n;\n scanf(\"%d\\n\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\\n\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n if (((a[i] / 4 - a[i] % 2) <= 0) || (a[i] % 4 == 3 && a[i] <= 11)) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d\\n\", (a[i] / 4) - (a[i] % 2));\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let N: usize = s.trim_end().parse().unwrap();\n s.clear();\n for _i in 0..N {\n io::stdin().read_line(&mut s).unwrap();\n let q: i32 = s.trim_end().parse().unwrap();\n if q < 4 {\n println!(\"-1\");\n } else {\n let rem = q % 4;\n let div = q / 4;\n let ans = match rem {\n 0 => div,\n 1 => {\n let mut t = div - 2 + 1;\n if div < 2 {\n t = -1;\n }\n t\n }\n 2 => {\n let mut t = div - 1 + 1;\n if div < 1 {\n t = -1;\n }\n t\n }\n 3 => {\n let mut t = div - 3 + 2;\n if div < 3 {\n t = -1;\n }\n t\n }\n _ => -1,\n };\n println!(\"{}\", ans);\n }\n s.clear();\n }\n}", "difficulty": "medium"} {"problem_id": "0040", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

Takahashi is hosting an sports meet.\nThere are N people who will participate. These people are conveniently numbered 1 through N.\nAlso, there are M options of sports for this event. These sports are numbered 1 through M.\nAmong these options, Takahashi will select one or more sports (possibly all) to be played in the event.

\n

Takahashi knows that Person i's j-th favorite sport is Sport A_{ij}.\nEach person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.

\n

Takahashi is worried that one of the sports will attract too many people.\nTherefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized.\nFind the minimum possible number of the participants in the sport with the largest number of participants.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 300
  • \n
  • 1 \\leq M \\leq 300
  • \n
  • A_{i1} , A_{i2} , ... , A_{iM} is a permutation of the integers from 1 to M.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_{11} A_{12} ... A_{1M}\nA_{21} A_{22} ... A_{2M}\n:\nA_{N1} A_{N2} ... A_{NM}\n
\n
\n
\n
\n
\n

Output

Print the minimum possible number of the participants in the sport with the largest number of participants.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 5\n5 1 3 4 2\n2 5 3 1 4\n2 3 1 4 5\n2 5 4 3 1\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Assume that Sports 1, 3 and 4 are selected to be played. In this case, Person 1 will participate in Sport 1, Person 2 in Sport 3, Person 3 in Sport 3 and Person 4 in Sport 4.\nHere, the sport with the largest number of participants is Sport 3, with two participants.\nThere is no way to reduce the number of participants in the sport with the largest number of participants to 1. Therefore, the answer is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3\n2 1 3\n2 1 3\n2 1 3\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

Since all the people have the same taste in sports, there will be a sport with three participants, no matter what sports are selected.\nTherefore, the answer is 3.

\n
\n
", "c_code": "int solution(void) {\n int N;\n int M;\n scanf(\"%d %d\", &N, &M);\n int A[N][M];\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < M; j++) {\n scanf(\"%d\", &A[i][j]);\n }\n }\n int kibousya[M];\n int nozoki[M];\n for (int i = 0; i < M; i++) {\n kibousya[i] = 0;\n nozoki[i] = 0;\n }\n int ans = 114514;\n for (int i = 0; i < M; i++) {\n for (int j = 0; j < N; j++) {\n for (int k = 0; k < M; k++) {\n if (nozoki[A[j][k] - 1] == 0) {\n kibousya[A[j][k] - 1]++;\n break;\n }\n }\n }\n int maxkibou = 0;\n for (int j = 0; j < M; j++) {\n if (nozoki[j] == 0 && kibousya[j] >= kibousya[maxkibou]) {\n maxkibou = j;\n }\n }\n if (ans > kibousya[maxkibou]) {\n ans = kibousya[maxkibou];\n }\n nozoki[maxkibou] = 1;\n for (int i = 0; i < M; i++) {\n kibousya[i] = 0;\n }\n }\n printf(\"%d\", ans);\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 buf_it = buf.split_whitespace();\n\n let N = buf_it.next().unwrap().parse::().unwrap();\n let M = buf_it.next().unwrap().parse::().unwrap();\n let A = (0..N)\n .map(|_| {\n (0..M)\n .map(|_| buf_it.next().unwrap().parse::().unwrap() - 1)\n .collect::>()\n })\n .collect::>();\n\n let check = |n: usize| {\n let mut popped = HashSet::new();\n let mut idx = (0..N).map(|_| 0).collect::>();\n loop {\n let mut cnt = (0..M).map(|_| 0).collect::>();\n for i in 0..N {\n cnt[A[i][idx[i]]] += 1;\n }\n if *(cnt.iter().max().unwrap()) <= n {\n return true;\n }\n for j in 0..M {\n if cnt[j] > n {\n popped.insert(j);\n }\n }\n if popped.len() == M {\n break;\n }\n for i in 0..N {\n while popped.contains(&A[i][idx[i]]) {\n idx[i] += 1;\n }\n }\n }\n false\n };\n let mut l = 0;\n let mut r = N;\n while l + 1 < r {\n let i = (l + r) / 2;\n if check(i) {\n r = i;\n } else {\n l = i;\n }\n }\n println!(\"{}\", r);\n}", "difficulty": "medium"} {"problem_id": "0041", "problem_description": "There are $$$n$$$ kids, numbered from $$$1$$$ to $$$n$$$, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (all these numbers are from $$$1$$$ to $$$n$$$ and are distinct, so $$$p$$$ is a permutation). Let the next kid for a kid $$$p_i$$$ be kid $$$p_{i + 1}$$$ if $$$i < n$$$ and $$$p_1$$$ otherwise. After the dance, each kid remembered two kids: the next kid (let's call him $$$x$$$) and the next kid for $$$x$$$. Each kid told you which kids he/she remembered: the kid $$$i$$$ remembered kids $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$. However, the order of $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$ can differ from their order in the circle. Example: 5 kids in a circle, $$$p=[3, 2, 4, 1, 5]$$$ (or any cyclic shift). The information kids remembered is: $$$a_{1,1}=3$$$, $$$a_{1,2}=5$$$; $$$a_{2,1}=1$$$, $$$a_{2,2}=4$$$; $$$a_{3,1}=2$$$, $$$a_{3,2}=4$$$; $$$a_{4,1}=1$$$, $$$a_{4,2}=5$$$; $$$a_{5,1}=2$$$, $$$a_{5,2}=3$$$. You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists.If you are Python programmer, consider using PyPy instead of Python when you submit your code.", "c_code": "int solution() {\n\n int n;\n\n scanf(\"%d\", &n);\n\n int ans[n + 5];\n int a[n + 5];\n int b[n + 5];\n\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d%d\", &a[i], &b[i]);\n }\n\n ans[1] = 1;\n ans[2] = a[1];\n ans[3] = b[1];\n\n if (a[a[1]] != b[1] && b[a[1]] != b[1]) {\n ans[2] = b[1];\n ans[3] = a[1];\n }\n\n int place = 3;\n\n while (place < n) {\n if (a[ans[place - 1]] == ans[place]) {\n ans[place + 1] = b[ans[place - 1]];\n } else {\n ans[place + 1] = a[ans[place - 1]];\n }\n place++;\n }\n\n for (int i = 1; i <= n; ++i) {\n printf(\"%d \", ans[i]);\n }\n\n printf(\"\\n\");\n return 0;\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 = line.trim().parse().unwrap();\n line.clear();\n let mut neighbours = Vec::with_capacity(count);\n while count > 0 {\n input.read_line(&mut line).unwrap();\n let mut pair = line.split_whitespace();\n let first = pair.next().unwrap().parse::().unwrap() - 1;\n let second = pair.next().unwrap().parse::().unwrap() - 1;\n line.clear();\n neighbours.push((first, second));\n count -= 1;\n }\n line.shrink_to_fit();\n let mut cur;\n let mut next = neighbours[0].0;\n let mut next_next = neighbours[0].1;\n if neighbours[next].0 == next_next || neighbours[next].1 == next_next {\n cur = next;\n next = next_next;\n } else {\n cur = next_next;\n }\n print!(\"1 {} {}\", cur + 1, next + 1);\n count = neighbours.len() - 3;\n while count > 0 {\n if neighbours[cur].0 == next {\n next_next = neighbours[cur].1;\n } else {\n next_next = neighbours[cur].0;\n }\n print!(\" {}\", next_next + 1);\n cur = next;\n next = next_next;\n count -= 1;\n }\n}", "difficulty": "hard"} {"problem_id": "0042", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

We held two competitions: Coding Contest and Robot Maneuver.

\n

In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places receive 100000, 200000, and 300000 yen (the currency of Japan), respectively. Furthermore, a contestant taking the first place in both competitions receives an additional 400000 yen.

\n

DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot Maneuver.\nFind the total amount of money he earned.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq X \\leq 205
  • \n
  • 1 \\leq Y \\leq 205
  • \n
  • X and Y are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
X Y\n
\n
\n
\n
\n
\n

Output

\n

Print the amount of money DISCO-Kun earned, as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 1\n
\n
\n
\n
\n
\n

Sample Output 1

1000000\n
\n

In this case, he earned 300000 yen in Coding Contest and another 300000 yen in Robot Maneuver. Furthermore, as he won both competitions, he got an additional 400000 yen.\nIn total, he made 300000 + 300000 + 400000 = 1000000 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 101\n
\n
\n
\n
\n
\n

Sample Output 2

100000\n
\n

In this case, he earned 100000 yen in Coding Contest.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 4\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

In this case, unfortunately, he was the highest-ranked contestant without prize money in both competitions.

\n
\n
", "c_code": "int solution() {\n int x = 0;\n int y = 0;\n int money = 0;\n scanf(\"%d %d\", &x, &y);\n if (x == 3) {\n money += 100000;\n } else if (x == 2) {\n money += 200000;\n } else if (x == 1) {\n money += 300000;\n }\n if (y == 3) {\n money += 100000;\n } else if (y == 2) {\n money += 200000;\n } else if (y == 1) {\n money += 300000;\n }\n if (x == 1 && y == 1) {\n money += 400000;\n }\n printf(\"%d\", money);\n return 0;\n}", "rust_code": "fn solution() {\n let (X, Y): (usize, usize) = {\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 )\n };\n\n let x = if X == 1 {\n 300000\n } else if X == 2 {\n 200000\n } else if X == 3 {\n 100000\n } else {\n 0\n };\n let y = if Y == 1 {\n 300000\n } else if Y == 2 {\n 200000\n } else if Y == 3 {\n 100000\n } else {\n 0\n };\n\n let ans = if X == 1 && Y == 1 {\n x + y + 400000\n } else {\n x + y\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0043", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Given N integers A_1, ..., A_N, compute A_1 \\times ... \\times A_N.

\n

However, if the result exceeds 10^{18}, print -1 instead.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 0 \\leq A_i \\leq 10^{18}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the value A_1 \\times ... \\times A_N as an integer, or -1 if the value exceeds 10^{18}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 1

1000000000000000000\n
\n

We have 1000000000 \\times 1000000000 = 1000000000000000000.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n101 9901 999999000001\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

We have 101 \\times 9901 \\times 999999000001 = 1000000000000000001, which exceeds 10^{18}, so we should print -1 instead.

\n
\n
\n
\n
\n
\n

Sample Input 3

31\n4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n int i = 0;\n int N = 0;\n scanf(\"%d\", &N);\n\n long long A[N];\n long long ans = 1;\n long long x = (pow(10, 18.0));\n\n if (2 <= N && N <= (pow(10, 5.0))) {\n for (i = 0; i < N; i++) {\n scanf(\"%lld\", &A[i]);\n }\n for (i = 0; i < N; i++) {\n if (A[i] == 0) {\n printf(\"0\");\n return 0;\n }\n }\n } else {\n return 0;\n }\n\n for (i = 0; i < N; i++) {\n if (A[i] <= x / ans) {\n\n ans = ans * A[i];\n\n } else {\n printf(\"-1\\n\");\n return 0;\n }\n }\n\n printf(\"%lld\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n is.clear();\n stdin().read_line(&mut is).ok();\n let itr = is.split_whitespace().map(|e| e.parse::().unwrap());\n let mut ans = 1;\n let flg = itr.clone().any(|e| e == 0);\n if !flg {\n for e in itr {\n ans *= e;\n if ans > 1000000000000000000 {\n ans = -1;\n break;\n }\n }\n }\n\n println!(\"{}\", if flg { 0 } else { ans });\n}", "difficulty": "hard"} {"problem_id": "0044", "problem_description": "The map of the capital of Berland can be viewed on the infinite coordinate plane. Each point with integer coordinates contains a building, and there are streets connecting every building to four neighbouring buildings. All streets are parallel to the coordinate axes.The main school of the capital is located in $$$(s_x, s_y)$$$. There are $$$n$$$ students attending this school, the $$$i$$$-th of them lives in the house located in $$$(x_i, y_i)$$$. It is possible that some students live in the same house, but no student lives in $$$(s_x, s_y)$$$.After classes end, each student walks from the school to his house along one of the shortest paths. So the distance the $$$i$$$-th student goes from the school to his house is $$$|s_x - x_i| + |s_y - y_i|$$$.The Provision Department of Berland has decided to open a shawarma tent somewhere in the capital (at some point with integer coordinates). It is considered that the $$$i$$$-th student will buy a shawarma if at least one of the shortest paths from the school to the $$$i$$$-th student's house goes through the point where the shawarma tent is located. It is forbidden to place the shawarma tent at the point where the school is located, but the coordinates of the shawarma tent may coincide with the coordinates of the house of some student (or even multiple students).You want to find the maximum possible number of students buying shawarma and the optimal location for the tent itself.", "c_code": "int solution() {\n int n;\n int sx;\n int sy;\n scanf(\"%d %d %d\", &n, &sx, &sy);\n int s[n][2];\n int l = 0;\n int r = 0;\n int u = 0;\n int d = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d %d\", &s[i][0], &s[i][1]);\n if (s[i][0] > sx) {\n r++;\n } else if (s[i][0] < sx) {\n l++;\n }\n if (s[i][1] > sy) {\n u++;\n } else if (s[i][1] < sy) {\n d++;\n }\n }\n if (l >= r && l >= u && l >= d) {\n printf(\"%d\\n\", l);\n printf(\"%d %d\\n\", sx - 1, sy);\n } else if (r >= l && r >= u && r >= d) {\n printf(\"%d\\n\", r);\n printf(\"%d %d\\n\", sx + 1, sy);\n } else if (u >= r && u >= l && u >= d) {\n printf(\"%d\\n\", u);\n printf(\"%d %d\\n\", sx, sy + 1);\n\n } else if (d >= r && d >= u && d >= l) {\n printf(\"%d\\n\", d);\n printf(\"%d %d\\n\", sx, sy - 1);\n }\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let mut iter = str.split_whitespace();\n let n: i64 = iter.next().unwrap().parse().unwrap();\n let s_x: i64 = iter.next().unwrap().parse().unwrap();\n let s_y: i64 = iter.next().unwrap().parse().unwrap();\n let mut u = 0;\n let mut d = 0;\n let mut l = 0;\n let mut r = 0;\n\n for _ in 0..n {\n str.clear();\n let _b1 = stdin().read_line(&mut str).unwrap();\n let mut iter = str.split_whitespace();\n let x: i64 = iter.next().unwrap().parse().unwrap();\n let y: i64 = iter.next().unwrap().parse().unwrap();\n if x > s_x {\n r += 1;\n }\n if x < s_x {\n l += 1;\n }\n if y > s_y {\n u += 1;\n }\n if y < s_y {\n d += 1;\n }\n }\n let maxim = max(u, max(d, max(r, l)));\n println!(\"{}\", maxim);\n if maxim == u {\n println!(\"{} {}\", s_x, s_y + 1);\n } else if maxim == d {\n println!(\"{} {}\", s_x, s_y - 1);\n } else if maxim == l {\n println!(\"{} {}\", s_x - 1, s_y);\n } else if maxim == r {\n println!(\"{} {}\", s_x + 1, s_y);\n }\n}", "difficulty": "medium"} {"problem_id": "0045", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You have N items and a bag of strength W.\nThe i-th item has a weight of w_i and a value of v_i.

\n

You will select some of the items and put them in the bag.\nHere, the total weight of the selected items needs to be at most W.

\n

Your objective is to maximize the total value of the selected items.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 100
  • \n
  • 1 ≤ W ≤ 10^9
  • \n
  • 1 ≤ w_i ≤ 10^9
  • \n
  • For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.
  • \n
  • 1 ≤ v_i ≤ 10^7
  • \n
  • W, each w_i and v_i are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N W\nw_1 v_1\nw_2 v_2\n:\nw_N v_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible total value of the selected items.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 6\n2 1\n3 4\n4 10\n3 4\n
\n
\n
\n
\n
\n

Sample Output 1

11\n
\n

The first and third items should be selected.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 6\n2 1\n3 7\n4 10\n3 6\n
\n
\n
\n
\n
\n

Sample Output 2

13\n
\n

The second and fourth items should be selected.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 10\n1 100\n1 100\n1 100\n1 100\n
\n
\n
\n
\n
\n

Sample Output 3

400\n
\n

You can take everything.

\n
\n
\n
\n
\n
\n

Sample Input 4

4 1\n10 100\n10 100\n10 100\n10 100\n
\n
\n
\n
\n
\n

Sample Output 4

0\n
\n

You can take nothing.

\n
\n
", "c_code": "int solution(void) {\n\n int N;\n\n long long W;\n\n scanf(\"%d%lld\", &N, &W);\n\n long long dp[N + 1][N + 1][301];\n\n long long w[N];\n long long v[N];\n\n for (int i = 0; i < N; i++) {\n scanf(\"%lld%lld\", &w[i], &v[i]);\n }\n\n for (int i = 0; i <= N; i++) {\n for (int j = 0; j <= N; j++) {\n for (int k = 0; k <= 300; k++) {\n dp[i][j][k] = 0;\n }\n }\n }\n\n long long b = w[0];\n\n for (int i = 1; i <= N; i++) {\n long long t = w[i - 1] - b;\n for (int j = 1; j <= N; j++) {\n for (int k = 0; k <= 300; k++) {\n dp[i][j][k] = dp[i - 1][j][k];\n if (t <= k && dp[i][j][k] < dp[i - 1][j - 1][k - t] + v[i - 1]) {\n dp[i][j][k] = dp[i - 1][j - 1][k - t] + v[i - 1];\n }\n }\n }\n }\n\n long long ans = 0;\n\n for (int i = 1; i <= N; i++) {\n for (int j = 0; j <= 300; j++) {\n if (b * i + j <= W && ans < dp[N][i][j]) {\n ans = dp[N][i][j];\n }\n }\n }\n\n printf(\"%lld\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N: usize,\n W: u64,\n WV: [(u64,u64); N],\n }\n\n let mut V = vec![vec![0; 0]; 4];\n let mut w0 = 0;\n for i in 0..N {\n let (w, v) = WV[i];\n if i == 0 {\n V[0].push(v);\n w0 = w;\n } else {\n V[(w - w0) as usize].push(v);\n }\n }\n\n for i in 0..4 {\n V[i].sort();\n V[i].reverse();\n }\n\n let mut S = vec![vec![0; 1]; 4];\n for i in 0..4 {\n let mut tmp = 0;\n for v in V[i].iter() {\n tmp += *v;\n S[i].push(tmp);\n }\n }\n\n let mut ans = 0;\n for i0 in 0..S[0].len() {\n for i1 in 0..S[1].len() {\n for i2 in 0..S[2].len() {\n for i3 in 0..S[3].len() {\n if w0 * i0 as u64\n + (w0 + 1) * i1 as u64\n + (w0 + 2) * i2 as u64\n + (w0 + 3) * i3 as u64\n <= W\n {\n ans = max(ans, S[0][i0] + S[1][i1] + S[2][i2] + S[3][i3]);\n }\n }\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0046", "problem_description": "There are $$$n$$$ block towers in a row, where tower $$$i$$$ has a height of $$$a_i$$$. You're part of a building crew, and you want to make the buildings look as nice as possible. In a single day, you can perform the following operation: Choose two indices $$$i$$$ and $$$j$$$ ($$$1 \\leq i, j \\leq n$$$; $$$i \\neq j$$$), and move a block from tower $$$i$$$ to tower $$$j$$$. This essentially decreases $$$a_i$$$ by $$$1$$$ and increases $$$a_j$$$ by $$$1$$$. You think the ugliness of the buildings is the height difference between the tallest and shortest buildings. Formally, the ugliness is defined as $$$\\max(a)-\\min(a)$$$. What's the minimum possible ugliness you can achieve, after any number of days?", "c_code": "int solution() {\n int n;\n int x = 0;\n scanf(\"%d\", &n);\n int arr_1[n];\n int result[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr_1[i]);\n int arr_2[arr_1[i]];\n for (int j = 0; j < arr_1[i]; j++) {\n scanf(\"%d\", &arr_2[j]);\n x = x + arr_2[j];\n }\n if (x % arr_1[i] == 0) {\n result[i] = 0;\n } else {\n result[i] = 1;\n }\n x = 0;\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d\\n\", result[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut lines = stdin.lock().lines();\n let cases: u32 = lines.next().unwrap().unwrap().parse().unwrap();\n for _ in 0..cases {\n let buildings_count: u32 = lines.next().unwrap().unwrap().parse().unwrap();\n let buildings_line = lines.next().unwrap().unwrap();\n let buildings_line_split = buildings_line.split_ascii_whitespace();\n let mut block_sum: u32 = 0;\n for x in buildings_line_split {\n block_sum += x.parse::().unwrap();\n }\n println!(\n \"{}\",\n if block_sum.is_multiple_of(buildings_count) {\n 0\n } else {\n 1\n }\n );\n }\n}", "difficulty": "hard"} {"problem_id": "0047", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi and Aoki will have a battle using their monsters.

\n

The health and strength of Takahashi's monster are A and B, respectively, and those of Aoki's monster are C and D, respectively.

\n

The two monsters will take turns attacking, in the order Takahashi's, Aoki's, Takahashi's, Aoki's, ...\nHere, an attack decreases the opponent's health by the value equal to the attacker's strength.\nThe monsters keep attacking until the health of one monster becomes 0 or below. The person with the monster whose health becomes 0 or below loses, and the other person wins.

\n

If Takahashi will win, print Yes; if he will lose, print No.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A,B,C,D \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C D\n
\n
\n
\n
\n
\n

Output

If Takahashi will win, print Yes; if he will lose, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10 9 10 10\n
\n
\n
\n
\n
\n

Sample Output 1

No\n
\n

First, Takahashi's monster attacks Aoki's monster, whose health is now 10-9=1.

\n

Next, Aoki's monster attacks Takahashi's monster, whose health is now 10-10=0.

\n

Takahashi's monster is the first to have 0 or less health, so Takahashi loses.

\n
\n
\n
\n
\n
\n

Sample Input 2

46 4 40 5\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n
\n
", "c_code": "int solution() {\n int A = 0;\n int B = 0;\n int C = 0;\n int D = 0;\n scanf(\"%d %d %d %d\", &A, &B, &C, &D);\n while (1) {\n C = C - B;\n if (C <= 0) {\n printf(\"Yes\\n\");\n break;\n }\n A = A - D;\n if (A <= 0) {\n printf(\"No\\n\");\n break;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let s: Vec = s\n .trim()\n .split(' ')\n .map(|x| x.parse::().unwrap())\n .collect();\n let (a, b, c, d) = (s[0], s[1], s[2], s[3]);\n\n let tk = (c + (b - 1)) / b;\n let ao = (a + (d - 1)) / d;\n\n if tk <= ao {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0048", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i.

\n

Takahashi will repeatedly perform the following operation on these numbers:

\n
    \n
  • Select a pair of integers, A_i and A_j, that have the same parity (that is, both are even or both are odd) and erase them.
  • \n
  • Then, write a new integer on the blackboard that is equal to the sum of those integers, A_i+A_j.
  • \n
\n

Determine whether it is possible to have only one integer on the blackboard.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≦ N ≦ 10^5
  • \n
  • 1 ≦ A_i ≦ 10^9
  • \n
  • A_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nA_1 A_2A_N\n
\n
\n
\n
\n
\n

Output

If it is possible to have only one integer on the blackboard, print YES. Otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

It is possible to have only one integer on the blackboard, as follows:

\n
    \n
  • Erase 1 and 3 from the blackboard, then write 4. Now, there are two integers on the blackboard: 2 and 4.
  • \n
  • Erase 2 and 4 from the blackboard, then write 6. Now, there is only one integer on the blackboard: 6.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5\n1 2 3 4 5\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
", "c_code": "int solution() {\n int N = 0;\n int count = 0;\n scanf(\"%d\", &N);\n int a[N];\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < N; i++) {\n count += a[i] % 2;\n }\n if (count % 2 == 0) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin()\n .read_line(&mut line)\n .expect(\"Failed to read line\");\n io::stdin()\n .read_line(&mut line)\n .expect(\"Failed to read line\");\n let mut vec: Vec<&str> = line.split_whitespace().collect();\n let n: u64 = vec.swap_remove(0).parse().expect(\"Failed to parse\");\n let mut odd_cnt = 0;\n let mut i = 0;\n for s in vec {\n let n2: u64 = s.parse().expect(\"Failed to parse\");\n if !n2.is_multiple_of(2) {\n odd_cnt += 1;\n }\n i += 1;\n if n <= i {\n break;\n }\n }\n println!(\"{}\", if odd_cnt % 2 == 0 { \"YES\" } else { \"NO\" });\n}", "difficulty": "medium"} {"problem_id": "0049", "problem_description": "In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.", "c_code": "int solution() {\n int i = 0;\n int a = 0;\n int b = 0;\n int n = 0;\n int c = 0;\n int count = 0;\n scanf(\"%d %d %d\", &n, &a, &b);\n int t[n];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &t[i]);\n if (t[i] == 1 && a != 0) {\n a--;\n } else if (t[i] == 2 && b != 0) {\n b--;\n } else if (t[i] == 1 && a == 0) {\n if (c != 0 && b == 0) {\n c--;\n } else if (b != 0) {\n b--;\n c++;\n } else {\n count++;\n }\n } else if (t[i] == 2 && b == 0) {\n count = count + t[i];\n }\n }\n\n printf(\"%d\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let (_, a, b) = {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let mut it = input\n .split_whitespace()\n .map(|k| k.parse::().unwrap());\n (it.next().unwrap(), it.next().unwrap(), it.next().unwrap())\n };\n\n let t = {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n input\n .split_whitespace()\n .map(|k| k.parse::().unwrap())\n .collect::>()\n };\n\n let mut a = a;\n let mut b = b;\n let mut c = 0usize;\n\n let mut ans = 0usize;\n\n for t in t {\n if t == 1 {\n if a > 0 {\n a -= 1;\n } else if b > 0 {\n b -= 1;\n c += 1;\n } else if c > 0 {\n c -= 1;\n } else {\n ans += 1;\n }\n } else if t == 2 {\n if b > 0 {\n b -= 1;\n } else {\n ans += 2;\n }\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0050", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N cities and M roads.\nThe i-th road (1≤i≤M) connects two cities a_i and b_i (1≤a_i,b_i≤N) bidirectionally.\nThere may be more than one road that connects the same pair of two cities.\nFor each city, how many roads are connected to the city?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≤N,M≤50
  • \n
  • 1≤a_i,b_i≤N
  • \n
  • a_i ≠ b_i
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\na_1 b_1\n:  \na_M b_M\n
\n
\n
\n
\n
\n

Output

Print the answer in N lines.\nIn the i-th line (1≤i≤N), print the number of roads connected to city i.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3\n1 2\n2 3\n1 4\n
\n
\n
\n
\n
\n

Sample Output 1

2\n2\n1\n1\n
\n
    \n
  • City 1 is connected to the 1-st and 3-rd roads.
  • \n
  • City 2 is connected to the 1-st and 2-nd roads.
  • \n
  • City 3 is connected to the 2-nd road.
  • \n
  • City 4 is connected to the 3-rd road.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

2 5\n1 2\n2 1\n1 2\n2 1\n1 2\n
\n
\n
\n
\n
\n

Sample Output 2

5\n5\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8 8\n1 2\n3 4\n1 5\n2 8\n3 7\n5 2\n4 1\n6 8\n
\n
\n
\n
\n
\n

Sample Output 3

3\n3\n2\n2\n2\n1\n1\n2\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n int M;\n scanf(\"%d\", &N);\n scanf(\"%d\", &M);\n int city[2 * M];\n int ans[N];\n\n for (int i = 0; i < N; i++) {\n ans[i] = 0;\n }\n\n for (int i = 0; i < 2 * M; i++) {\n scanf(\"%d\", &city[i]);\n }\n\n for (int i = 0; i < 2 * M; i++) {\n ans[city[i] - 1]++;\n }\n for (int i = 0; i < N; i++) {\n printf(\"%d\\n\", ans[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (n, m) = {\n let mut b = String::new();\n std::io::stdin().read_line(&mut b).unwrap();\n let vec = b\n .split_whitespace()\n .map(|i| i.parse().unwrap())\n .collect::>();\n (vec[0], vec[1])\n };\n let mut h = std::collections::HashMap::new();\n for i in 0..n {\n h.insert(i + 1, Vec::new());\n }\n for _ in 0..m {\n let (a, b): (i32, i32) = {\n let mut b = String::new();\n std::io::stdin().read_line(&mut b).unwrap();\n let vec = b\n .split_whitespace()\n .map(|i| i.parse().unwrap())\n .collect::>();\n (std::cmp::min(vec[0], vec[1]), std::cmp::max(vec[0], vec[1]))\n };\n h.get_mut(&a).unwrap().push(b);\n h.get_mut(&b).unwrap().push(a);\n }\n for i in 0..n {\n println!(\"{}\", h[&(i + 1)].len());\n }\n}", "difficulty": "hard"} {"problem_id": "0051", "problem_description": "Once upon a time Mike and Mike decided to come up with an outstanding problem for some stage of ROI (rare olympiad in informatics). One of them came up with a problem prototype but another stole the idea and proposed that problem for another stage of the same olympiad. Since then the first Mike has been waiting for an opportunity to propose the original idea for some other contest... Mike waited until this moment!You are given an array $$$a$$$ of $$$n$$$ integers. You are also given $$$q$$$ queries of two types: Replace $$$i$$$-th element in the array with integer $$$x$$$. Replace each element in the array with integer $$$x$$$. After performing each query you have to calculate the sum of all elements in the array.", "c_code": "int solution() {\n long long m;\n long long n;\n long long flag;\n long long k;\n long long x;\n long long sum = 0;\n long long isFill = 0;\n long long last = 0;\n long long term = 0;\n scanf(\"%lld %lld\", &m, &n);\n long long a[m];\n long long num[m];\n for (int i = 0; i < m; i++) {\n scanf(\"%lld\", &a[i]);\n sum += a[i];\n num[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &flag);\n if (flag == 1) {\n scanf(\"%lld %lld\", &k, &x);\n if (isFill) {\n if (num[k - 1] != term) {\n num[k - 1] = term;\n sum = sum - last + x;\n a[k - 1] = x;\n printf(\"%lld\", sum);\n } else {\n sum = sum - a[k - 1] + x;\n printf(\"%lld\", sum);\n a[k - 1] = x;\n }\n } else {\n sum = sum - a[k - 1] + x;\n printf(\"%lld\", sum);\n a[k - 1] = x;\n }\n } else if (flag == 2) {\n scanf(\"%lld\", &x);\n sum = m * x;\n printf(\"%lld\", sum);\n last = x;\n term++;\n isFill = 1;\n }\n if (i != n - 1) {\n printf(\"\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut sin = String::new();\n io::stdin().read_line(&mut sin).unwrap();\n let mut iter = sin.split_whitespace();\n let n = iter.next().unwrap().trim().parse::().unwrap();\n let q = iter.next().unwrap().trim().parse::().unwrap();\n\n sin.clear();\n io::stdin().read_line(&mut sin).unwrap();\n let mut a: Vec = sin\n .split_whitespace()\n .map(|x| x.trim().parse::().unwrap())\n .collect();\n let mut last_update_time = vec![0; n as usize];\n let mut last_refresh_time = 0;\n let mut last_refresh_value: i64 = 0;\n let mut sum: i64 = a.iter().sum();\n\n for t in 1..(q + 1) {\n sin.clear();\n io::stdin().read_line(&mut sin).unwrap();\n iter = sin.split_whitespace();\n let x = iter.next().unwrap().trim().parse::().unwrap();\n\n if x == 1 {\n let index = iter.next().unwrap().trim().parse::().unwrap() - 1;\n let y = iter.next().unwrap().trim().parse::().unwrap();\n\n let mut current_value = a[index];\n if last_update_time[index] < last_refresh_time {\n current_value = last_refresh_value;\n }\n\n sum += y - current_value;\n\n a[index] = y;\n last_update_time[index] = t;\n } else {\n let value = iter.next().unwrap().trim().parse::().unwrap();\n last_refresh_time = t;\n last_refresh_value = value;\n sum = value * (n as i64);\n }\n\n println!(\"{}\", sum);\n }\n}", "difficulty": "hard"} {"problem_id": "0052", "problem_description": "Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5.Lyft has become so popular so that it is now used by all $$$m$$$ taxi drivers in the city, who every day transport the rest of the city residents — $$$n$$$ riders.Each resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same).The Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected.But one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver $$$i$$$ the number $$$a_{i}$$$ — the number of riders that would call the $$$i$$$-th taxi driver when all drivers and riders are at their home?The taxi driver can neither transport himself nor other taxi drivers.", "c_code": "int solution() {\n int n;\n int m;\n int j = 0;\n scanf(\"%d %d\", &n, &m);\n int x[n + m + 1];\n int t[n + m + 1];\n int pos[m + 1];\n int ans[m + 1];\n for (int i = 0; i < n + m; i++) {\n scanf(\"%d\", &x[i]);\n }\n for (int i = 0; i < n + m; i++) {\n scanf(\"%d\", &t[i]);\n if (t[i] == 1) {\n pos[j++] = i;\n }\n }\n for (int i = 0; i < m; i++) {\n ans[i] = 0;\n }\n ans[0] += pos[0];\n for (int k = 0; k < m - 1; k++) {\n for (int i = pos[k] + 1; i < pos[k + 1]; i++) {\n if (x[i] - x[pos[k]] > x[pos[k + 1]] - x[i]) {\n ans[k + 1]++;\n } else {\n ans[k]++;\n }\n }\n }\n ans[m - 1] += (n + m - pos[m - 1] - 1);\n for (int i = 0; i < m; i++) {\n printf(\"%d \", ans[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::{self, prelude::*};\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n let m: usize = it.next().unwrap().parse().unwrap();\n\n let x: Vec = it\n .by_ref()\n .take(n + m)\n .map(|x| x.parse().unwrap())\n .collect();\n let t: Vec = it.take(n + m).map(|x| x == \"1\").collect();\n\n let residents: Vec<_> = x\n .into_iter()\n .zip(t)\n .map(|(xi, ti)| {\n let kind = if ti { Driver } else { Rider };\n Resident { kind, address: xi }\n })\n .collect();\n\n let mut left = Vec::with_capacity(n);\n let mut current = None;\n for resident in residents.iter() {\n match resident.kind {\n Driver => current = Some(current.map(|x| x + 1).unwrap_or(0)),\n Rider => left.push(current),\n }\n }\n\n let mut right = Vec::with_capacity(n);\n current = None;\n for resident in residents.iter().rev() {\n match resident.kind {\n Driver => current = Some(current.map(|x| x - 1).unwrap_or(m - 1)),\n Rider => right.push(current),\n }\n }\n right.reverse();\n\n let mut rider_addresses = Vec::with_capacity(n);\n let mut driver_addresses = Vec::with_capacity(m);\n\n for resident in residents {\n match resident.kind {\n Rider => rider_addresses.push(resident.address),\n Driver => driver_addresses.push(resident.address),\n }\n }\n\n let closest: Vec<_> = rider_addresses\n .into_iter()\n .zip(left.into_iter().zip(right))\n .map(|(address, (a, b))| match (a, b) {\n (Some(left), Some(right))\n if driver_addresses[right] - address < address - driver_addresses[left] =>\n {\n right\n }\n (Some(left), _) => left,\n (None, Some(right)) => right,\n _ => panic!(\"!\"),\n })\n .collect();\n\n let answer = closest\n .into_iter()\n .fold(vec![0; m], |mut answer, driver_id| {\n answer[driver_id] += 1;\n answer\n });\n\n let stdout = io::stdout();\n let mut lock = stdout.lock();\n\n for a in answer {\n write!(&mut lock, \"{} \", a).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0053", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

\n
    \n
  • the dogs numbered 1,2,\\cdots,26 were respectively given the names a, b, ..., z;
  • \n
  • the dogs numbered 27,28,29,\\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;
  • \n
  • the dogs numbered 703,704,705,\\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;
  • \n
  • the dogs numbered 18279,18280,18281,\\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;
  • \n
  • the dogs numbered 475255,475256,\\cdots were respectively given the names aaaaa, aaaab, ...;
  • \n
  • and so on.
  • \n
\n

To sum it up, the dogs numbered 1, 2, \\cdots were respectively given the following names:

\n

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

\n

Now, Roger asks you:

\n

\"What is the name for the dog numbered N?\"

\n
\n
\n
\n
\n

Constraints

    \n
  • N is an integer.
  • \n
  • 1 \\leq N \\leq 1000000000000001
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n
\n
\n
\n
\n
\n

Sample Output 1

b\n
\n
\n
\n
\n
\n
\n

Sample Input 2

27\n
\n
\n
\n
\n
\n

Sample Output 2

aa\n
\n
\n
\n
\n
\n
\n

Sample Input 3

123456789\n
\n
\n
\n
\n
\n

Sample Output 3

jjddja\n
\n
\n
", "c_code": "int solution(void) {\n long int dogn = 0;\n char name[16] = {0};\n int i = 0;\n\n scanf(\"%ld\", &dogn);\n dogn--;\n\n while (dogn > -1) {\n name[i] = dogn % 26 + 'a';\n dogn = dogn / 26;\n i++;\n dogn--;\n }\n\n while (i--) {\n printf(\"%c\", name[i]);\n }\n printf(\"\\n\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n let mut n: usize = is.trim().parse::().unwrap();\n let mut ans = String::new();\n let a = (b'a'..=b'z').map(|e| e as char).collect::>();\n\n while n > 0 {\n n -= 1;\n let t = n % 26;\n n /= 26;\n\n ans.push(a[t]);\n }\n\n println!(\"{}\", ans.chars().rev().collect::());\n}", "difficulty": "easy"} {"problem_id": "0054", "problem_description": "

Search I

\n\n

\nYou are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.\n

\n\n

Input

\n\n

\nIn the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.\n

\n\n\n

Output

\n\n

\nPrint C in a line.\n

\n\n

Constraints

\n\n\n
    \n
  • n ≤ 10000
  • \n
  • q ≤ 500
  • \n
  • 0 ≤ an element in S ≤ 109
  • \n
  • 0 ≤ an element in T ≤ 109
  • \n
\n\n\n

Sample Input 1

\n
\n5\n1 2 3 4 5\n3\n3 4 1\n
\n\n

Sample Output 1

\n
\n3\n
\n\n

Sample Input 2

\n
\n3\n3 1 2\n1\n5\n
\n

Sample Output 2

\n
\n0\n
\n\n

Sample Input 3

\n
\n5\n1 1 2 2 3\n2\n1 2\n
\n

Sample Output 3

\n
\n2\n
\n\n\n

Notes

\n", "c_code": "int solution(void) {\n int n = 0;\n int m = 0;\n int i = 0;\n int j = 0;\n int count = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n scanf(\"%d\", &m);\n int b[m];\n for (i = 0; i < m; i++) {\n scanf(\"%d\", &b[i]);\n for (j = 0; j < n; j++) {\n if (a[j] == b[i]) {\n count++;\n break;\n }\n }\n }\n printf(\"%d\\n\", count);\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 let mut buf_it = buf.split_whitespace();\n\n let N = buf_it.next().unwrap().parse::().unwrap();\n let A = (0..N)\n .map(|_| buf_it.next().unwrap().parse::().unwrap())\n .collect::>();\n\n let M = buf_it.next().unwrap().parse::().unwrap();\n let B = (0..M)\n .map(|_| buf_it.next().unwrap().parse::().unwrap())\n .collect::>();\n\n let ans = A.intersection(&B).collect::>().len();\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0055", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There is an integer sequence A of length N.

\n

Find the number of the pairs of integers l and r (1 \\leq l \\leq r \\leq N) that satisfy the following condition:

\n
    \n
  • A_l\\ xor\\ A_{l+1}\\ xor\\ ...\\ xor\\ A_r = A_l\\ +\\ A_{l+1}\\ +\\ ...\\ +\\ A_r
  • \n
\n

Here, xor denotes the bitwise exclusive OR.

\n

\nDefinition of XOR

\n

The XOR of integers c_1, c_2, ..., c_m is defined as follows:

\n
    \n
  • Let the XOR be X. In the binary representation of X, the digit in the 2^k's place (0 \\leq k; k is an integer) is 1 if there are an odd number of integers among c_1, c_2, ...c_m whose binary representation has 1 in the 2^k's place, and 0 if that number is even.
  • \n
\n

For example, let us compute the XOR of 3 and 5. The binary representation of 3 is 011, and the binary representation of 5 is 101, thus the XOR has the binary representation 110, that is, the XOR is 6.

\n

", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n long a[n];\n long sum = 0;\n long xor = 0;\n long ans = 0;\n int k = 0;\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%ld\", a + i);\n }\n for (int i = 0; i < n; i++) {\n while (k < n && (sum + a[k] == (xor^a[k]))) {\n sum += a[k];\n xor += a[k];\n k++;\n cnt++;\n }\n ans += cnt;\n cnt--;\n sum -= a[i];\n xor -= a[i];\n }\n printf(\"%ld\\n\", ans);\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 a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let mut ans: usize = 0;\n let mut xor: usize = 0;\n let mut sum: usize = 0;\n let mut r: usize = 0;\n for l in 0..n {\n while r < n && xor ^ a[r] == sum + a[r] {\n xor ^= a[r];\n sum += a[r];\n r += 1;\n }\n ans += r - l;\n xor ^= a[l];\n sum -= a[l];\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0056", "problem_description": "Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \\dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n long long n;\n long long g;\n long long b;\n long long m;\n long long a;\n long long r;\n long long s[t];\n long long d;\n long long f;\n for (int i = 0; i < t; i++) {\n scanf(\"%lld %lld %lld\", &n, &g, &b);\n if (n % 2 == 0) {\n r = n / 2;\n } else {\n r = (n + 1) / 2;\n }\n if (r <= g) {\n s[i] = n;\n } else {\n m = r / g;\n if (r % g == 0) {\n {\n f = (m - 1) * b;\n a = r + f;\n }\n\n } else {\n f = m * b;\n a = r + f;\n }\n d = n - r;\n if (f >= d) {\n s[i] = a;\n } else {\n s[i] = a + (d - f);\n }\n }\n }\n for (int i = 0; i < t; i++) {\n printf(\"%lld\\n\", s[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let t: u32 = line.trim().parse().unwrap();\n for _ in 0..t {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let mut pieces = line.split_whitespace();\n let n: u64 = pieces.next().unwrap().parse().unwrap();\n let g: u64 = pieces.next().unwrap().parse().unwrap();\n let b: u64 = pieces.next().unwrap().parse().unwrap();\n\n let min_good = n / 2 + (n % 2);\n let (mut cycles, mut rem_good) = (min_good / g, min_good % g);\n if cycles > 0 && rem_good == 0 {\n cycles -= 1;\n rem_good += g;\n }\n let days = max(n, cycles * (g + b) + rem_good);\n println!(\"{}\", days);\n }\n}", "difficulty": "easy"} {"problem_id": "0057", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We ask you to select some number of positive integers, and calculate the sum of them.

\n

It is allowed to select as many integers as you like, and as large integers as you wish.\nYou have to follow these, however: each selected integer needs to be a multiple of A, and you need to select at least one integer.

\n

Your objective is to make the sum congruent to C modulo B.\nDetermine whether this is possible.

\n

If the objective is achievable, print YES. Otherwise, print NO.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ A ≤ 100
  • \n
  • 1 ≤ B ≤ 100
  • \n
  • 0 ≤ C < B
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print YES or NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7 5 1\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

For example, if you select 7 and 14, the sum 21 is congruent to 1 modulo 5.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2 1\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n

The sum of even numbers, no matter how many, is never odd.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 100 97\n
\n
\n
\n
\n
\n

Sample Output 3

YES\n
\n

You can select 97, since you may select multiples of 1, that is, all integers.

\n
\n
\n
\n
\n
\n

Sample Input 4

40 98 58\n
\n
\n
\n
\n
\n

Sample Output 4

YES\n
\n
\n
\n
\n
\n
\n

Sample Input 5

77 42 36\n
\n
\n
\n
\n
\n

Sample Output 5

NO\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int c = 0;\n int x = 0;\n int i;\n\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n scanf(\"%d\", &c);\n\n for (i = 1; i <= b; i++) {\n\n if (c == ((a * i) % b)) {\n printf(\"YES\");\n x = a % b;\n break;\n }\n }\n if (x == 0) {\n printf(\"NO\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n let mut input = String::new();\n let _ = io::stdin().read_line(&mut input);\n let input_nums = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n let a = input_nums[0];\n let b = input_nums[1];\n let c = input_nums[2];\n\n let mut i = 0;\n\n loop {\n if (a * i) % b == c {\n println!(\"YES\");\n break;\n }\n if i > 1000 {\n println!(\"NO\");\n break;\n }\n i += 1;\n }\n}", "difficulty": "easy"} {"problem_id": "0058", "problem_description": "It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.There are $$$n$$$ displays placed along a road, and the $$$i$$$-th of them can display a text with font size $$$s_i$$$ only. Maria Stepanovna wants to rent such three displays with indices $$$i < j < k$$$ that the font size increases if you move along the road in a particular direction. Namely, the condition $$$s_i < s_j < s_k$$$ should be held.The rent cost is for the $$$i$$$-th display is $$$c_i$$$. Please determine the smallest cost Maria Stepanovna should pay.", "c_code": "int solution() {\n int i;\n int j;\n int n;\n int ans = 1e9;\n scanf(\"%d\", &n);\n int a[n + 2];\n int cost[n + 2];\n int d[n + 2];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]), d[i] = 1e9;\n }\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &cost[i]);\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < i; j++) {\n if (a[j] < a[i] && cost[i] + cost[j] < d[i]) {\n d[i] = cost[i] + cost[j];\n }\n }\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < i; j++) {\n if (a[j] < a[i] && cost[i] + d[j] < ans) {\n ans = cost[i] + d[j];\n }\n }\n }\n printf(ans == 1e9 ? \"-1\\n\" : \"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::{self, prelude::*};\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n let s: Vec = it.by_ref().take(n).map(|x| x.parse().unwrap()).collect();\n let c: Vec = it.by_ref().take(n).map(|x| x.parse().unwrap()).collect();\n\n let mut gll: Vec> = (0..n)\n .map(|i| (i + 1..n).filter(|&j| s[i] < s[j]).collect())\n .collect();\n\n for gl in gll.iter_mut() {\n gl.sort_unstable_by_key(|&i| c[i]);\n }\n\n let mut ans = None;\n\n for i in 0..n {\n for &j in gll[i].iter() {\n if let Some(&k) = gll[j].first() {\n let c = c[i] + c[j] + c[k];\n match ans {\n Some(cur) if cur <= c => (),\n _ => ans = Some(c),\n }\n }\n }\n }\n\n match ans {\n Some(c) => println!(\"{}\", c),\n None => println!(\"-1\"),\n }\n}", "difficulty": "medium"} {"problem_id": "0059", "problem_description": "Shell sort", "c_code": "void solution(int array[], int len) {\n int i;\n int j;\n int gap;\n\n for (gap = len / 2; gap > 0; gap = gap / 2) {\n for (i = gap; i < len; i++) {\n for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j = j - gap) {\n int temp = array[j];\n array[j] = array[j + gap];\n array[j + gap] = temp;\n }\n }\n }\n}\n", "rust_code": "fn solution(values: &mut [T]) {\n fn insertion(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\n

Input

\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\n

Output

\n\n

\nFor each dataset, prints the number of prime numbers.\n

\n\n

Sample Input

\n\n
\n10\n3\n11\n
\n\n

Output 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\n

Input

\n\n

\n複数のデータセットの並びが入力として与えられます。入力の終わりは負の実数ひとつの行で示されます。\n各データセットとして1つの実数 n が1行に与えられます。\n

\n\n

\nデータセットの数は 1200 を超えません。\n

\n\n\n

Output

\n\n

\n入力データセットごとに 2 進数への変換結果を出力します。\n

\n\n

Sample Input

\n\n
\n23.5\n158.1\n-1.0\n
\n\n

Output 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": "\n

Score : 200 points

\n
\n
\n

Problem Statement

\n

There are N people numbered 1 to N. Each person wears a red hat or a blue hat.

\n

You 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.

\n

Determine if there are more people wearing a red hat than people wearing a blue hat.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • |s| = N
  • \n
  • s_i is R or B.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\ns\n
\n
\n
\n
\n
\n

Output

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
\n

Sample Input 1

4\nRRBR\n
\n
\n
\n
\n
\n

Sample 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
\n

Sample Input 2

4\nBRBR\n
\n
\n
\n
\n
\n

Sample 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": "\n

Day 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\n

Input

\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\n

Output

\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\n

Sample Input 1

\n\n
\n1\n
\n\n

Sample Output 1

\n
\nfri\n
\n\n

Sample Input 2

\n
\n9\n
\n\n

Sample Output 2

\n
\nsat\n
\n\n

Sample Input 3

\n
\n30\n
\n\n

Sample 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": "\n

Score : 200 points

\n
\n
\n

Problem 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.

\n

You 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the length of the longest even string that can be obtained.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

abaababaab\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n
    \n
  • abaababaab itself is even, but we need to delete at least one character.
  • \n
  • abaababaa is not even.
  • \n
  • abaababa is not even.
  • \n
  • abaabab is not even.
  • \n
  • abaaba is even. Thus, we should print its length, 6.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

xxxx\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
    \n
  • xxx is not even.
  • \n
  • xx is even.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

abcabcabcabc\n
\n
\n
\n
\n
\n

Sample Output 3

6\n
\n

The longest even string that can be obtained is abcabc, whose length is 6.

\n
\n
\n
\n
\n
\n

Sample Input 4

akasakaakasakasakaakas\n
\n
\n
\n
\n
\n

Sample Output 4

14\n
\n

The 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": "\n

Score : 300 points

\n
\n
\n

Problem 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 #.

\n

Takahashi 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the minimum number of stones that needs to be recolored.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n#.#\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

It is enough to change the color of the first stone to white.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n#.##.\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

9\n.........\n
\n
\n
\n
\n
\n

Sample Output 3

0\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": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.

\n

She will concatenate all of the strings in some order, to produce a long string.

\n

Among all strings that she can produce in this way, find the lexicographically smallest one.

\n

Here, 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
\n

Constraints

    \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
\n

Input

The input is given from Standard Input in the following format:

\n
N L\nS_1\nS_2\n:\nS_N\n
\n
\n
\n
\n
\n

Output

Print the lexicographically smallest string that Iroha can produce.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\ndxx\naxx\ncxx\n
\n
\n
\n
\n
\n

Sample Output 1

axxcxxdxx\n
\n

The 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": "\n

Score : 200 points

\n
\n
\n

Problem 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.

\n

Snuke 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
\n

It 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
H W\na_{1, 1}...a_{1, W}\n:\na_{H, 1}...a_{H, W}\n
\n
\n
\n
\n
\n

Output

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
\n

Sample Input 1

4 4\n##.#\n....\n##.#\n.#.#\n
\n
\n
\n
\n
\n

Sample Output 1

###\n###\n.##\n
\n

The second row and the third column in the original grid will be removed.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3\n#..\n.#.\n..#\n
\n
\n
\n
\n
\n

Sample Output 2

#..\n.#.\n..#\n
\n

As there is no row or column that consists only of white squares, no operation will be performed.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 5\n.....\n.....\n..#..\n.....\n
\n
\n
\n
\n
\n

Sample Output 3

#\n
\n
\n
\n
\n
\n
\n

Sample Input 4

7 6\n......\n....#.\n.#....\n..#...\n..#...\n......\n.#..#.\n
\n
\n
\n
\n
\n

Sample 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": "\n

Score : 100 points

\n
\n
\n

Problem 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.

\n

After 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Replace the first four characters in S with 2018 and print it.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2017/01/07\n
\n
\n
\n
\n
\n

Sample Output 1

2018/01/07\n
\n
\n
\n
\n
\n
\n

Sample Input 2

2017/01/31\n
\n
\n
\n
\n
\n

Sample 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": "\n

Score : 500 points

\n
\n
\n

Problem 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.

\n

Takahashi 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.

\n

Process 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N 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
\n

Output

Print Q lines.

\n

The 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
\n

Sample Input 1

3 2 5\n1 2 3\n2 3 3\n2\n3 2\n1 3\n
\n
\n
\n
\n
\n

Sample Output 1

0\n1\n
\n

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.

\n

To 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
\n

Sample Input 2

4 0 1\n1\n2 1\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

There may be no road at all.

\n
\n
\n
\n
\n
\n

Sample 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
\n

Sample 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\n

Input

\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\n

Constraints

\n
    \n
  • n ≤ 1000
  • \n
  • The length of the string ≤ 100
  • \n
\n\n

Output

\n\n

\n Print the final scores of Taro and Hanako respectively. Put a single space character between them.\n

\n\n

Sample Input

\n\n
\n3\ncat dog\nfish fish\nlion tiger\n
\n\n

Sample 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": "\n

Score : 100 points

\n
\n
\n

Problem Statement

X and A are integers between 0 and 9 (inclusive).

\n

If X is less than A, print 0; if X is not less than A, print 10.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq X, A \\leq 9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X A\n
\n
\n
\n
\n
\n

Output

If X is less than A, print 0; if X is not less than A, print 10.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 5\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n

3 is less than 5, so we should print 0.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 5\n
\n
\n
\n
\n
\n

Sample Output 2

10\n
\n

7 is not less than 5, so we should print 10.

\n
\n
\n
\n
\n
\n

Sample Input 3

6 6\n
\n
\n
\n
\n
\n

Sample Output 3

10\n
\n

6 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\n

Input

\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\n

Output

\n

\n Output the number of cakes I can enjoy.\n

\n\n

Sample Input 1

\n
\n5 4\n5 5 6 5\n
\n

Sample Output 1

\n
\n4\n
\n\n

Sample Input 2

\n
\n7 5\n8 8 8 8 8\n
\n\n

Sample Output 2

\n
\n5\n
\n\n

Sample Input 3

\n
\n100 3\n3 3 3\n
\n\n

Sample 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\n

Input

\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\n

Output

\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\n

Constraints

\n\n
    \n
  • 0 ≤ x, y ≤ 10000
  • \n
  • the number of datasets ≤ 3000
  • \n
\n\n

Sample Input

\n
\n3 2\n2 2\n5 3\n0 0\n
\n

Sample 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": "\n

Score : 200 points

\n
\n
\n

Problem Statement

In some other world, today is the day before Christmas Eve.

\n

Mr. 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).

\n

He 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N\np_1\np_2\n:\np_N\n
\n
\n
\n
\n
\n

Output

Print the total amount Mr. Takaha will pay.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n4980\n7980\n6980\n
\n
\n
\n
\n
\n

Sample Output 1

15950\n
\n

The 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.

\n

Note that outputs such as 15950.0 will be judged as Wrong Answer.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n4320\n4320\n4320\n4320\n
\n
\n
\n
\n
\n

Sample Output 2

15120\n
\n

Only 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": "\n

Score : 400 points

\n
\n
\n

Problem 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.

\n

Now, 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
\n

Help 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nP_1 P_2 \\cdots P_N\nC_1 C_2 \\cdots C_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible score at the end of the game.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2\n2 4 5 1 3\n3 4 -10 -8 8\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

When 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
\n

The maximum score achieved is 8.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 3\n2 1\n10 -7\n
\n
\n
\n
\n
\n

Sample Output 2

13\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3 3\n3 1 2\n-1000 -2000 -3000\n
\n
\n
\n
\n
\n

Sample Output 3

-1000\n
\n

We have to make at least one move.

\n
\n
\n
\n
\n
\n

Sample 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
\n

Sample Output 4

29507023469\n
\n

The 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": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given a string s.\nAmong the different substrings of s, print the K-th lexicographically smallest one.

\n

A 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.

\n

Let 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
\n

Constraints

    \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
\n

Partial 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
\n

Input

Input is given from Standard Input in the following format:

\n
s\nK\n
\n
\n
\n
\n
\n

Output

Print the K-th lexicographically smallest substring of K.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

aba\n4\n
\n
\n
\n
\n
\n

Sample Output 1

b\n
\n

s 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
\n

Sample Input 2

atcoderandatcodeer\n5\n
\n
\n
\n
\n
\n

Sample Output 2

andat\n
\n
\n
\n
\n
\n
\n

Sample Input 3

z\n1\n
\n
\n
\n
\n
\n

Sample Output 3

z\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": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

\n

You 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.

\n

For 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.

\n

If 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N\nx_1 u_1\nx_2 u_2\n:\nx_N u_N\n
\n
\n
\n
\n
\n

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

\n

Output 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
\n

Sample Input 1

2\n10000 JPY\n0.10000000 BTC\n
\n
\n
\n
\n
\n

Sample Output 1

48000.0\n
\n

The 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.

\n

Outputs such as 48000 and 48000.1 will also be judged correct.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n100000000 JPY\n100.00000000 BTC\n0.00000001 BTC\n
\n
\n
\n
\n
\n

Sample Output 2

138000000.0038\n
\n

In 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": "\n

Score : 200 points

\n
\n
\n

Problem 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.

\n

In 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).

\n

Determine 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
\n

Constraints

    \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
\n

Input

The input is given from Standard Input in the following format:

\n
N M\nA_1\nA_2\n:  \nA_N\nB_1\nB_2\n:  \nB_M\n
\n
\n
\n
\n
\n

Output

Print Yes if the template image B is contained in the image A. Print No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n#.#\n.#.\n#.#\n#.\n.#\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The 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
\n

Sample Input 2

4 1\n....\n....\n....\n....\n#\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

The 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 で表される.SOX から構成された長さ 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 の各文字は OX である.
  • \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": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi has N days of summer vacation.

\n

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

\n

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

\n

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

\n

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

\n
\n
\n
\n
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_1 ... A_M\n
\n
\n
\n
\n
\n

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

41 2\n5 6\n
\n
\n
\n
\n
\n

Sample Output 1

30\n
\n

For 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
\n

Sample Input 2

10 2\n5 6\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

He cannot finish his assignments.

\n
\n
\n
\n
\n
\n

Sample Input 3

11 2\n5 6\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

He can finish his assignments, but he will have no time to hang out.

\n
\n
\n
\n
\n
\n

Sample Input 4

314 15\n9 26 5 35 8 9 79 3 23 8 46 2 6 43 3\n
\n
\n
\n
\n
\n

Sample Output 4

9\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": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.

\n

On 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
\n

Snuke 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of operations required.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3\n2 3 1 4\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

One 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
\n

Sample Input 2

3 3\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8 3\n7 3 1 8 4 6 2 5\n
\n
\n
\n
\n
\n

Sample Output 3

4\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": "\n

Score : 300 points

\n
\n
\n

Problem 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.

\n

In 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.

\n

Jiro 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
\n

Constraints

    \n
  • 1 ≦ |S| ≦ 10^5
  • \n
  • Each character in S is B or W.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

BBBWW\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

By 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.

\n

In either way, Jiro's purpose can be achieved by placing one stone.

\n
\n
\n
\n
\n
\n

Sample Input 2

WWWWWW\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

If all stones are already of the same color, no new stone is necessary.

\n
\n
\n
\n
\n
\n

Sample Input 3

WBWBWBWBWB\n
\n
\n
\n
\n
\n

Sample Output 3

9\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\n

Maximum 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\n

Input

\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\n

Output

\n\n

\nPrint the maximum value in a line.\n

\n\n

Constraints

\n\n
    \n
  • $2 \\leq n \\leq 200,000$
  • \n
  • $1 \\leq R_t \\leq 10^9$
  • \n
\n\n\n

Sample Input 1

\n
\n6\n5\n3\n1\n3\n4\n3\n
\n

Sample Output 1

\n
\n3\n
\n\n

Sample Input 2

\n
\n3\n4\n3\n2\n
\n

Sample 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": "\n

Score : 300 points

\n
\n
\n

Problem 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
\n

It can be proved that we can always make A, B and C all equal by repeatedly performing these operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq A,B,C \\leq 50
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print the minimum number of operations required to make A, B and C all equal.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 5 4\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

We 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
\n

Sample Input 2

2 6 3\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
\n
\n
\n
\n
\n

Sample Input 3

31 41 5\n
\n
\n
\n
\n
\n

Sample Output 3

23\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": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Let us denote by f(x, m) the remainder of the Euclidean division of x by m.

\n

Let 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N X M\n
\n
\n
\n
\n
\n

Output

Print \\displaystyle{\\sum_{i=1}^N A_i}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 2 1001\n
\n
\n
\n
\n
\n

Sample Output 1

1369\n
\n

The 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
\n

Sample Input 2

1000 2 16\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n

The sequence A begins 2,4,0,0,\\ldots Therefore, the answer is 6.

\n
\n
\n
\n
\n
\n

Sample Input 3

10000000000 10 99959\n
\n
\n
\n
\n
\n

Sample 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": "\n

Score : 300 points

\n
\n
\n

Problem 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.

\n

You 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.

\n

The 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the minimum number of people who have to change their directions.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\nWEEWW\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Assume 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
\n

Sample Input 2

12\nWEWEWEEEWWWE\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8\nWWWWWEEE\n
\n
\n
\n
\n
\n

Sample Output 3

3\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\n

Input

\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\n

Constraints

\n
    \n
  • 3 ≤ n ≤ 100
  • \n
  • 0 ≤ x ≤ 300
  • \n
\n\n

Output

\n\n

\n For each dataset, print the number of combinations in a line.\n

\n\n

Sample Input

\n\n
\n5 9\n0 0\n
\n\n

Sample Output

\n\n
\n2\n
\n\n

Note

\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": "\n

Score : 400 points

\n
\n
\n

Problem 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

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.

\n
A B\n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

33\n
\n
\n
\n
\n
\n

Sample Output 1

2 -1\n
\n

For A=2 and B=-1, A^5-B^5 = 33.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n
\n
\n
\n
\n
\n

Sample Output 2

0 -1\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": "\n

Score : 100 points

\n
\n
\n

Problem 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.

\n

Takahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ X ≤ 9
  • \n
  • X is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

If Takahashi's growth will be celebrated, print YES; if it will not, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

The growth of a five-year-old child will be celebrated.

\n
\n
\n
\n
\n
\n

Sample Input 2

6\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n

See 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\n

Input

\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\n

Output

\n

\n Output the total price.\n

\n\n\n

Sample Input 1

\n
\n180 100 2400\n
\n\n

Sample Output 1

\n
\n460\n
\n\n

Sample Input 2

\n
\n200 90 2018\n
\n\n

Sample 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\n

Graph

\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\n

Input

\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\n

Output

\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\n

Constraints

\n\n
    \n
  • $1 \\leq n \\leq 100$
  • \n
\n\n\n

Sample Input

\n
\n4\n1 2 2 4\n2 1 4\n3 0\n4 1 3\n
\n\n

Sample 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": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison.

\n

Eating 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.

\n

Find the maximum number of tasty cookies that Takahashi can eat.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq A,B,C \\leq 10^9
  • \n
  • A,B and C are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print the maximum number of tasty cookies that Takahashi can eat.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1 4\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

We 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
\n

Sample Input 2

5 2 9\n
\n
\n
\n
\n
\n

Sample Output 2

10\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8 8 1\n
\n
\n
\n
\n
\n

Sample Output 3

9\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": "\n

Score : 200 points

\n
\n
\n

Problem 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.

\n

Find the number of the possible ways to paint the balls.

\n
\n
\n
\n
\n

Constraints

    \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
\n

Input

The input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

Print the number of the possible ways to paint the balls.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

We 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

Sample Input 2

1 10\n
\n
\n
\n
\n
\n

Sample Output 2

10\n
\n

Since 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": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Joisino is planning to record N TV programs with recorders.

\n

The TV can receive C channels numbered 1 through C.

\n

The 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.

\n

Here, there will never be more than one program that are broadcast on the same channel at the same time.

\n

When 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).

\n

Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

\n
\n
\n
\n
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N C\ns_1 t_1 c_1\n:\ns_N t_N c_N\n
\n
\n
\n
\n
\n

Output

When the minimum required number of recorders is x, print the value of x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n1 7 2\n7 8 1\n8 12 1\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Two 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
\n

Sample Input 2

3 4\n1 3 2\n3 4 4\n1 4 3\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

There may be a channel where there is no program to record.

\n
\n
\n
\n
\n
\n

Sample 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
\n

Sample Output 3

2\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": "\n

Score : 100 points

\n
\n
\n

Problem 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
\n

Constraints

    \n
  • 1≦A,B≦10^9
  • \n
  • op is either + or -.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
A op B\n
\n
\n
\n
\n
\n

Output

Evaluate the formula and print the result.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 + 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

Since 1 + 2 = 3, the output should be 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 - 7\n
\n
\n
\n
\n
\n

Sample Output 2

-2\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": "\n

Score : 300 points

\n
\n
\n

Problem 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.

\n

Operation: 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 .. a_N\nb_1 b_2 .. b_N\n
\n
\n
\n
\n
\n

Output

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
\n

Sample Input 1

3\n1 2 3\n5 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

For 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
\n

Sample Input 2

5\n3 1 4 1 5\n2 7 1 8 2\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5\n2 7 1 8 2\n3 1 4 1 5\n
\n
\n
\n
\n
\n

Sample Output 3

No\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": "\n

Score : 100 points

\n
\n
\n

Problem 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.)

\n

Write 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

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
\n

Sample Input 1

2019/04/30\n
\n
\n
\n
\n
\n

Sample Output 1

Heisei\n
\n
\n
\n
\n
\n
\n

Sample Input 2

2019/11/01\n
\n
\n
\n
\n
\n

Sample Output 2

TBD\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": "\n

Score : 100 points

\n
\n
\n

Problem 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
\n

Constraints

    \n
  • 0 \\leq A,B \\leq 23
  • \n
  • A and B are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the hour of the starting time of the contest in 24-hour time.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

9 12\n
\n
\n
\n
\n
\n

Sample Output 1

21\n
\n

In 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

Sample Input 2

19 0\n
\n
\n
\n
\n
\n

Sample Output 2

19\n
\n

The contest has just started.

\n
\n
\n
\n
\n
\n

Sample Input 3

23 2\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n

The 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": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

M-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
\n

He 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
\n

His 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
\n

Determine whether the magic can be successful.

\n
\n
\n
\n
\n

Constraints

\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
\n

Input

\n

Input is given from Standard Input in the following format:

\n
A B C\nK\n
\n
\n
\n
\n
\n

Output

\n

If the magic can be successful, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7 2 5\n3\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The 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
\n

Sample Input 2

7 4 2\n3\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

He 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": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given two integers a and b.\nDetermine if a+b=15 or a\\times b=15 or neither holds.

\n

Note that a+b=15 and a\\times b=15 do not hold at the same time.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq a,b \\leq 15
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b\n
\n
\n
\n
\n
\n

Output

If a+b=15, print +;\nif a\\times b=15, print *;\nif neither holds, print x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 11\n
\n
\n
\n
\n
\n

Sample Output 1

+\n
\n

4+11=15.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 5\n
\n
\n
\n
\n
\n

Sample Output 2

*\n
\n

3\\times 5=15.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 1\n
\n
\n
\n
\n
\n

Sample Output 3

x\n
\n

1+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\n

Ring


\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\n

Input

\n

\n In the first line, the text $s$ is given.
\n In the second line, the pattern $p$ is given.\n

\n\n

Output

\n\n

\n If $p$ is in $s$, print Yes in a line, otherwise No.\n

\n\n

Constraints

\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\n

Sample Input 1

\n
\nvanceknowledgetoad\nadvance\n
\n\n

Sample Output 1

\n\n
\nYes\n
\n
\n

Sample Input 2

\n
\nvanceknowledgetoad\nadvanced\n
\n\n

Sample 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": "\n

Score : 600 points

\n
\n
\n

Problem 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
\n

It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.

\n

You 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
\n

Constraints

    \n
  • 0 ≤ K ≤ 50 \\times 10^{16}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K\n
\n
\n
\n
\n
\n

Output

Print a solution in the following format:

\n
N\na_1 a_2 ... a_N\n
\n

Here, 2 ≤ N ≤ 50 and 0 ≤ a_i ≤ 10^{16} + 1000 must hold.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

0\n
\n
\n
\n
\n
\n

Sample Output 1

4\n3 3 3 3\n
\n
\n
\n
\n
\n
\n

Sample Input 2

1\n
\n
\n
\n
\n
\n

Sample Output 2

3\n1 0 3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2\n
\n
\n
\n
\n
\n

Sample Output 3

2\n2 2\n
\n

The operation will be performed twice: [2, 2] -> [0, 3] -> [1, 1].

\n
\n
\n
\n
\n
\n

Sample Input 4

3\n
\n
\n
\n
\n
\n

Sample Output 4

7\n27 0 0 0 0 0 0\n
\n
\n
\n
\n
\n
\n

Sample Input 5

1234567894848\n
\n
\n
\n
\n
\n

Sample 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": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!

\n

There 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
\n

For each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).

\n

There 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
\n

Constraints

\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
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nA\nB\nC\nD\nE\n
\n
\n
\n
\n
\n

Output

\n

Print the minimum time required for all of the people to reach City 6, in minutes.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n3\n2\n4\n3\n5\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

One possible way to travel is as follows.\nFirst, there are N = 5 people at City 1, as shown in the following image:

\n

\"

\n

In 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

\"

\n

In 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

\"

\n

In 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

\"

\n

From 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
\n

Sample Input 2

10\n123\n123\n123\n123\n123\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n

All 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
\n

Sample Input 3

10000000007\n2\n3\n5\n7\n11\n
\n
\n
\n
\n
\n

Sample Output 3

5000000008\n
\n

Note 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": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi is practicing shiritori alone again today.

\n

Shiritori 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
\n

In this game, he is practicing to announce as many words as possible in ten seconds.

\n

You 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
\n

Constraints

    \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
\n

Input

Input is given from Standard Input in the following format:

\n
N\nW_1\nW_2\n:\nW_N\n
\n
\n
\n
\n
\n

Output

If every word announced by Takahashi satisfied the conditions, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\nhoge\nenglish\nhoge\nenigma\n
\n
\n
\n
\n
\n

Sample Output 1

No\n
\n

As hoge is announced multiple times, the rules of shiritori was not observed.

\n
\n
\n
\n
\n
\n

Sample Input 2

9\nbasic\nc\ncpp\nphp\npython\nnadesico\nocaml\nlua\nassembly\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8\na\naa\naaa\naaaa\naaaaa\naaaaaa\naaa\naaaaaaa\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
\n
\n
\n
\n

Sample Input 4

3\nabc\narc\nagc\n
\n
\n
\n
\n
\n

Sample Output 4

No\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": "\n

Score : 700 points

\n
\n
\n

Problem 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.

\n

Phantom 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?

\n

Process all the queries.

\n
\n
\n
\n
\n

Constraints

    \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
\n

Input

The input is given from Standard Input in the following format:

\n
N 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
\n

Output

For each query, print the number of the connected components consisting of blue squares in the region.

\n
\n
\n
\n
\n
\n
\n

Sample 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
\n

Sample Output 1

3\n2\n2\n2\n
\n

\"\"

\n

In the first query, the whole grid is specified. There are three components consisting of blue squares, and thus 3 should be printed.

\n

In 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
\n

Sample 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
\n

Sample 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": "\n

Score : 200 points

\n
\n
\n

Problem Statement

\n

Niwango-kun is an employee of Dwango Co., Ltd.
\nOne day, he is asked to generate a thumbnail from a video a user submitted.
\nTo generate a thumbnail, he needs to select a frame of the video according to the following procedure:

\n
    \n
  • Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of the frames of the video, and each a_i denotes the representation of the i-th frame of the video.
  • \n
  • Select t-th frame whose representation a_t is nearest to the average of all frame representations.
  • \n
  • If there are multiple such frames, select the frame with the smallest index.
  • \n
\n

Find the index t of the frame he should select to generate a thumbnail.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq a_i \\leq 100
  • \n
  • All numbers given in input are integers
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\na_{0} a_{1} ... a_{N-1}\n
\n
\n
\n
\n
\n

Output

\n

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

\n
3\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

\n
1\n
\n

Since the average of frame representations is 2, Niwango-kun needs to select the index 1, whose representation is 2, that is, the nearest value to the average.

\n
\n
\n
\n
\n
\n

Sample Input 2

\n
4\n2 5 2 5\n
\n
\n
\n
\n
\n

Sample Output 2

\n
0\n
\n

The average of frame representations is 3.5.
\nIn this case, every frame has the same distance from its representation to the average.
\nTherefore, Niwango-kun should select index 0, the smallest index among them.

\n
\n
", "c_code": "int solution(void) {\n int n;\n int ans = 0;\n int sum = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n sum += a[i];\n }\n for (int i = 0; i < n; i++) {\n if (abs((a[ans] * n) - sum) > abs((a[i] * n) - sum)) {\n ans = i;\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: i16 = s.next().unwrap().parse().unwrap();\n let a: Vec = s.map(|a| a.parse().unwrap()).collect();\n let s: i16 = a.iter().sum();\n let ans = a\n .into_iter()\n .enumerate()\n .fold((0, std::i16::MAX), |r, (i, a)| {\n if (a * n - s).abs() < r.1 {\n (i, (a * n - s).abs())\n } else {\n r\n }\n })\n .0;\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0160", "problem_description": "Heap sort", "c_code": "void solution(int *a, int n) {\n int i;\n int j;\n int temp;\n\n for (i = n / 2; i >= 1; i--) {\n temp = a[i];\n j = 2 * i;\n\n while (j <= n) {\n if (j < n && a[j + 1] > a[j]) {\n j++;\n }\n\n if (temp > a[j]) {\n break;\n }\n\n a[j / 2] = a[j];\n j = 2 * j;\n }\n\n a[j / 2] = temp;\n }\n\n for (i = n; i >= 2; i--) {\n temp = a[i];\n a[i] = a[1];\n a[1] = temp;\n\n int parent = 1;\n temp = a[parent];\n j = 2 * parent;\n\n while (j <= i - 1) {\n if (j < i - 1 && a[j + 1] > a[j]) {\n j++;\n }\n\n if (temp > a[j]) {\n break;\n }\n\n a[j / 2] = a[j];\n j = 2 * j;\n }\n\n a[j / 2] = temp;\n }\n}\n", "rust_code": "fn solution(arr: &mut [T], ascending: bool) {\n if arr.len() <= 1 {\n return;\n }\n\n let comparator: fn(&T, &T) -> Ordering = if ascending {\n |a, b| a.cmp(b)\n } else {\n |a, b| b.cmp(a)\n };\n\n let mut i = (arr.len() - 1) / 2;\n loop {\n let mut root = i;\n\n loop {\n let mut idx = root;\n let l = 2 * root + 1;\n let r = 2 * root + 2;\n\n if l < arr.len() && comparator(&arr[l], &arr[idx]) == Ordering::Greater {\n idx = l;\n }\n\n if r < arr.len() && comparator(&arr[r], &arr[idx]) == Ordering::Greater {\n idx = r;\n }\n\n if idx == root {\n break;\n }\n\n arr.swap(root, idx);\n root = idx;\n }\n\n if i == 0 {\n break;\n }\n\n i -= 1;\n }\n\n let mut end = arr.len() - 1;\n\n while end > 0 {\n arr.swap(0, end);\n\n let mut root = 0;\n\n loop {\n let mut idx = root;\n let l = 2 * root + 1;\n let r = 2 * root + 2;\n\n if l < end && comparator(&arr[l], &arr[idx]) == Ordering::Greater {\n idx = l;\n }\n\n if r < end && comparator(&arr[r], &arr[idx]) == Ordering::Greater {\n idx = r;\n }\n\n if idx == root {\n break;\n }\n\n arr.swap(root, idx);\n root = idx;\n }\n\n end -= 1;\n }\n}\n", "difficulty": "easy"} {"problem_id": "0161", "problem_description": "In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.", "c_code": "int solution() {\n int n;\n char ch[20];\n scanf(\"%d\", &n);\n\n while (n--) {\n scanf(\"%s\", ch);\n int m = strlen(ch);\n int C = 1;\n\n while (C < m && isdigit(ch[C])) {\n ++C;\n }\n\n if (C > 1 && C < m) {\n int col = 0;\n for (int i = C + 1; i < m; ++i) {\n col = col * 10 + (ch[i] - '0');\n }\n\n int pow = 26;\n while (col > pow) {\n col -= pow;\n pow *= 26;\n }\n\n col -= 1;\n while (pow != 1) {\n pow /= 26;\n printf(\"%c\", (col / pow) + 'A');\n col %= pow;\n }\n\n ch[C] = 0;\n printf(\"%s\\n\", ch + 1);\n } else {\n int col = 0;\n int val = 0;\n int pow = 1;\n int i = 0;\n while (isalpha(ch[i])) {\n col += pow;\n pow *= 26;\n val = val * 26 + ch[i] - 'A';\n i += 1;\n }\n col += val;\n printf(\"R%sC%d\\n\", ch + i, col);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut count_string: String = String::new();\n io::stdin()\n .read_line(&mut count_string)\n .expect(\"Read error!\");\n\n let mut count: u64 = count_string.trim().parse::().expect(\"Not an integer!\");\n\n while count > 0 {\n count -= 1;\n\n let mut input_value = String::new();\n io::stdin()\n .read_line(&mut input_value)\n .expect(\"Read error!\");\n\n let trimmed_input: &str = input_value.trim();\n\n let mut found_tokens = 0;\n let mut is_numeric = false;\n let mut tokens: Vec = vec![\"\".to_string(); 4];\n\n for chr in trimmed_input.chars() {\n if is_numeric {\n if chr.is_ascii_digit() {\n tokens[found_tokens].push(chr);\n } else {\n is_numeric = false;\n found_tokens += 1;\n tokens[found_tokens].push(chr);\n }\n } else {\n if !chr.is_ascii_digit() {\n tokens[found_tokens].push(chr);\n } else {\n is_numeric = true;\n found_tokens += 1;\n tokens[found_tokens].push(chr);\n }\n }\n }\n\n found_tokens += 1;\n\n if found_tokens == 2 {\n let mut col: u64 = 0;\n let row: u64 = tokens[1].parse::().expect(\"Not an integer!\");\n for chr in tokens[0].to_uppercase().chars() {\n col = col * 26 + (chr as u64) - ('A' as u64) + 1;\n }\n println!(\"R{}C{}\", row, col);\n } else if found_tokens == 4 {\n let row: u64 = tokens[1].parse::().expect(\"Not an integer!\");\n let mut col: u64 = tokens[3].parse::().expect(\"Not an integer!\");\n let mut output_value = String::new();\n while col > 0 {\n let rem = (col % 26) as u8;\n if rem != 0 {\n output_value.push((rem + b'A' - 1) as char);\n } else {\n output_value.push('Z');\n }\n col = (col - 1) / 26;\n }\n println!(\"{}{}\", output_value.chars().rev().collect::(), row);\n } else {\n panic!(\"Invalid number of tokens\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0162", "problem_description": "You play your favourite game yet another time. You chose the character you didn't play before. It has $$$str$$$ points of strength and $$$int$$$ points of intelligence. Also, at start, the character has $$$exp$$$ free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by $$$1$$$ or raise intelligence by $$$1$$$).Since you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.", "c_code": "int solution() {\n int T = 0;\n scanf(\"%d\", &T);\n\n while (T-- > 0) {\n long s = 0;\n long i = 0;\n long x = 0;\n scanf(\"%ld %ld %ld\", &s, &i, &x);\n\n long y = (int)floor((double)(i - s + x) / 2);\n\n if (x - y < 0) {\n printf(\"0\\n\");\n } else if (y < 0) {\n printf(\"%ld\\n\", x + 1);\n } else {\n printf(\"%ld\\n\", x - y);\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 for _ in 0..t {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let mut input_iter = input.split_whitespace();\n let mut s = input_iter.next().unwrap().parse::().unwrap();\n let i = input_iter.next().unwrap().parse::().unwrap();\n let mut e = input_iter.next().unwrap().parse::().unwrap();\n\n if s < i {\n if i - s >= e {\n println!(\"0\");\n continue;\n }\n\n e -= i - s + 1;\n s = i + 1;\n }\n\n if e >= s - i {\n println!(\"{}\", s - i - 1 + (e - (s - i - 1)) / 2 + 1)\n } else {\n println!(\"{}\", e + 1);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0163", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

The restaurant AtCoder serves the following five dishes:

\n
    \n
  • ABC Don (rice bowl): takes A minutes to serve.
  • \n
  • ARC Curry: takes B minutes to serve.
  • \n
  • AGC Pasta: takes C minutes to serve.
  • \n
  • APC Ramen: takes D minutes to serve.
  • \n
  • ATC Hanbagu (hamburger patty): takes E minutes to serve.
  • \n
\n

Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.

\n

This restaurant has the following rules on orders:

\n
    \n
  • An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).
  • \n
  • Only one dish can be ordered at a time.
  • \n
  • No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.
  • \n
\n

E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.
\nHere, he can order the dishes in any order he likes, and he can place an order already at time 0.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • A, B, C, D and E are integers between 1 and 123 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
A\nB\nC\nD\nE\n
\n
\n
\n
\n
\n

Output

\n

Print the earliest possible time for the last dish to be delivered, as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

29\n20\n7\n35\n120\n
\n
\n
\n
\n
\n

Sample Output 1

215\n
\n

If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:

\n
    \n
  • Order ABC Don at time 0, which will be delivered at time 29.
  • \n
  • Order ARC Curry at time 30, which will be delivered at time 50.
  • \n
  • Order AGC Pasta at time 50, which will be delivered at time 57.
  • \n
  • Order ATC Hanbagu at time 60, which will be delivered at time 180.
  • \n
  • Order APC Ramen at time 180, which will be delivered at time 215.
  • \n
\n

There is no way to order the dishes in which the last dish will be delivered earlier than this.

\n
\n
\n
\n
\n
\n

Sample Input 2

101\n86\n119\n108\n57\n
\n
\n
\n
\n
\n

Sample Output 2

481\n
\n

If we decide to order the dishes in the order AGC Pasta, ARC Curry, ATC Hanbagu, APC Ramen, ABC Don, the earliest possible time for each order is as follows:

\n
    \n
  • Order AGC Pasta at time 0, which will be delivered at time 119.
  • \n
  • Order ARC Curry at time 120, which will be delivered at time 206.
  • \n
  • Order ATC Hanbagu at time 210, which will be delivered at time 267.
  • \n
  • Order APC Ramen at time 270, which will be delivered at time 378.
  • \n
  • Order ABC Don at time 380, which will be delivered at time 481.
  • \n
\n

There is no way to order the dishes in which the last dish will be delivered earlier than this.

\n
\n
\n
\n
\n
\n

Sample Input 3

123\n123\n123\n123\n123\n
\n
\n
\n
\n
\n

Sample Output 3

643\n
\n

This is the largest valid case.

\n
\n
", "c_code": "int solution(void) {\n int time[5];\n\n scanf(\"%d\\n\", &time[0]);\n scanf(\"%d\\n\", &time[1]);\n scanf(\"%d\\n\", &time[2]);\n scanf(\"%d\\n\", &time[3]);\n scanf(\"%d\", &time[4]);\n\n int min = 10;\n int total = 0;\n for (int i = 0; i < 5; i++) {\n int remainder = time[i] % 10;\n if (remainder != 0 && remainder < min) {\n min = remainder;\n }\n total += (time[i] + 9) / 10 * 10;\n };\n\n total += min - 10;\n\n printf(\"%d\", total);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_to_string(&mut s).ok();\n let mut iter = s.split_whitespace();\n let mut tm = Vec::new();\n for _ in 0..5 {\n tm.push(iter.next().unwrap().parse::().unwrap());\n }\n let mut nzmin = 10;\n for &i in tm.iter() {\n if i % 10 == 0 {\n continue;\n }\n nzmin = std::cmp::min(i % 10, nzmin);\n }\n let mut ans = 0;\n for &i in tm.iter() {\n ans += (i + 9) / 10 * 10;\n }\n if nzmin != 0 {\n ans -= 10 - nzmin\n };\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0164", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and an integer K.\nPrint the string obtained by replacing every character in S that differs from the K-th character of S, with *.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq N\\leq 10
  • \n
  • S is a string of length N consisting of lowercase English letters.
  • \n
  • N and K are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\nK\n
\n
\n
\n
\n
\n

Output

Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\nerror\n2\n
\n
\n
\n
\n
\n

Sample Output 1

*rr*r\n
\n

The second character of S is r. When we replace every character in error that differs from r with *, we get the string *rr*r.

\n
\n
\n
\n
\n
\n

Sample Input 2

6\neleven\n5\n
\n
\n
\n
\n
\n

Sample Output 2

e*e*e*\n
\n
\n
\n
\n
\n
\n

Sample Input 3

9\neducation\n7\n
\n
\n
\n
\n
\n

Sample Output 3

******i**\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n scanf(\"%d\", &n);\n char a[n];\n scanf(\"%s\", a);\n int k = 0;\n scanf(\"%d\", &k);\n char b;\n b = a[k - 1];\n for (int i = 0; i < n; i++) {\n if (a[i] != b) {\n a[i] = '*';\n }\n }\n printf(\"%s\", a);\n\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 S: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n };\n let K: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n let ans = S\n .iter()\n .map(|&c| {\n if c == S[K - 1] {\n c.to_string()\n } else {\n \"*\".to_string()\n }\n })\n .collect::>()\n .join(\"\");\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0165", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There is a set A = \\{ a_1, a_2, \\ldots, a_N \\} consisting of N positive integers.\nTaro and Jiro will play the following game against each other.

\n

Initially, we have a pile consisting of K stones.\nThe two players perform the following operation alternately, starting from Taro:

\n
    \n
  • Choose an element x in A, and remove exactly x stones from the pile.
  • \n
\n

A player loses when he becomes unable to play.\nAssuming that both players play optimally, determine the winner.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq K \\leq 10^5
  • \n
  • 1 \\leq a_1 < a_2 < \\cdots < a_N \\leq K
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\na_1 a_2 \\ldots a_N\n
\n
\n
\n
\n
\n

Output

If Taro will win, print First; if Jiro will win, print Second.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 4\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

First\n
\n

If Taro removes three stones, Jiro cannot make a move.\nThus, Taro wins.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 5\n2 3\n
\n
\n
\n
\n
\n

Sample Output 2

Second\n
\n

Whatever Taro does in his operation, Jiro wins, as follows:

\n
    \n
  • If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.
  • \n
  • If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

2 7\n2 3\n
\n
\n
\n
\n
\n

Sample Output 3

First\n
\n

Taro should remove two stones. Then, whatever Jiro does in his operation, Taro wins, as follows:

\n
    \n
  • If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.
  • \n
  • If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 4

3 20\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 4

Second\n
\n
\n
\n
\n
\n
\n

Sample Input 5

3 21\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 5

First\n
\n
\n
\n
\n
\n
\n

Sample Input 6

1 100000\n1\n
\n
\n
\n
\n
\n

Sample Output 6

Second\n
\n
\n
", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n int a[n];\n int i;\n int j;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int dp[k + 1];\n dp[0] = 0;\n for (i = 1; i <= k; i++) {\n int status = 0;\n for (j = 0; j < n; j++) {\n int p = i - a[j];\n if (p >= 0 && dp[p] == 0) {\n status = 1;\n break;\n }\n }\n dp[i] = status;\n }\n if (dp[k] == 1) {\n printf(\" First \");\n } else {\n printf(\" Second \");\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 k: usize = itr.next().unwrap().parse().unwrap();\n let a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let mut dp: Vec = vec![false; 200020];\n\n dp[0] = true;\n for i in 1..k + 1 {\n let mut make = true;\n for j in 0..n {\n if i >= a[j] && dp[i - a[j]] {\n make = false;\n break;\n }\n }\n dp[i] = make;\n }\n\n if dp[k] {\n println!(\"Second\");\n } else {\n println!(\"First\");\n }\n}", "difficulty": "easy"} {"problem_id": "0166", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There is a kangaroo at coordinate 0 on an infinite number line that runs from left to right, at time 0.\nDuring the period between time i-1 and time i, the kangaroo can either stay at his position, or perform a jump of length exactly i to the left or to the right.\nThat is, if his coordinate at time i-1 is x, he can be at coordinate x-i, x or x+i at time i.\nThe kangaroo's nest is at coordinate X, and he wants to travel to coordinate X as fast as possible.\nFind the earliest possible time to reach coordinate X.

\n
\n
\n
\n
\n

Constraints

    \n
  • X is an integer.
  • \n
  • 1≤X≤10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

Print the earliest possible time for the kangaroo to reach coordinate X.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The kangaroo can reach his nest at time 3 by jumping to the right three times, which is the earliest possible time.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

He can reach his nest at time 2 by staying at his position during the first second, and jumping to the right at the next second.

\n
\n
\n
\n
\n
\n

Sample Input 3

11\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution() {\n long long x = 1;\n scanf(\"%lld\", &x);\n\n long long i = 1;\n while (i * (i + 1) / 2 < x) {\n i++;\n }\n printf(\"%lld\", i);\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\n let mut ok = 0;\n let mut ng = 1usize << 30;\n while ng - ok > 1 {\n let mid = (ok + ng) / 2;\n if mid * (mid + 1) / 2 < n {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n println!(\"{}\", ok + 1);\n}", "difficulty": "medium"} {"problem_id": "0167", "problem_description": "A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b.You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars.", "c_code": "int solution() {\n int v[5];\n for (int i = 0; i < 4; i++) {\n scanf(\"%d\", &v[i]);\n }\n if (v[2] * 2 < v[3] || 2 * v[3] < v[2] || v[3] >= v[1]) {\n printf(\"-1\");\n } else {\n printf(\"%d\\n%d\\n\", 2 * v[0], 2 * v[1]);\n if (v[2] > v[3]) {\n printf(\"%d\", v[2]);\n } else {\n printf(\"%d\", v[3]);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::prelude::*;\n std::io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let v1: u32 = it.next().unwrap().parse().unwrap();\n let v2: u32 = it.next().unwrap().parse().unwrap();\n let v3: u32 = it.next().unwrap().parse().unwrap();\n let vm: u32 = it.next().unwrap().parse().unwrap();\n\n let s1 = 2 * v1;\n\n if vm > s1 || 2 * vm >= s1 {\n println!(\"-1\");\n return;\n }\n\n let s2 = std::cmp::min(2 * v2, s1 - 1);\n\n if v2 > s2 || vm > s2 || 2 * v2 < s2 || 2 * vm >= s2 {\n println!(\"-1\");\n return;\n }\n\n let s3 = std::cmp::min(std::cmp::min(2 * v3, s2 - 1), 2 * vm);\n\n if v3 > s3 || vm > s3 || 2 * v3 < s3 || 2 * vm < s3 {\n println!(\"-1\");\n return;\n }\n\n println!(\"{}\\n{}\\n{}\", s1, s2, s3);\n}", "difficulty": "easy"} {"problem_id": "0168", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N squares in a row.\nThe leftmost square contains the integer A, and the rightmost contains the integer B. The other squares are empty.

\n

Aohashi would like to fill the empty squares with integers so that the following condition is satisfied:

\n
    \n
  • For any two adjacent squares, the (absolute) difference of the two integers in those squares is between C and D (inclusive).
  • \n
\n

As long as the condition is satisfied, it is allowed to use arbitrarily large or small integers to fill the squares.\nDetermine whether it is possible to fill the squares under the condition.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 500000
  • \n
  • 0 \\leq A \\leq 10^9
  • \n
  • 0 \\leq B \\leq 10^9
  • \n
  • 0 \\leq C \\leq D \\leq 10^9
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B C D\n
\n
\n
\n
\n
\n

Output

Print YES if it is possible to fill the squares under the condition; print NO otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 1 5 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

For example, fill the squares with the following integers: 1, -1, 3, 7, 5, from left to right.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 7 6 4 5\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 3

48792 105960835 681218449 90629745 90632170\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 4

491995 412925347 825318103 59999126 59999339\n
\n
\n
\n
\n
\n

Sample Output 4

YES\n
\n
\n
", "c_code": "int solution(void) {\n long long int N;\n long long int A;\n long long int B;\n long long int C;\n long long int D;\n scanf(\"%lld %lld %lld %lld %lld\", &N, &A, &B, &C, &D);\n int flag = 0;\n for (int i = 0; i < N; i++) {\n if (i * D - (N - i - 1) * C >= B - A && i * C - (N - 1 - i) * D <= B - A) {\n flag = 1;\n break;\n }\n }\n if (flag == 1) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\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: i64 = itr.next().unwrap().parse().unwrap();\n let a: i64 = itr.next().unwrap().parse().unwrap();\n let b: i64 = itr.next().unwrap().parse().unwrap();\n let c: i64 = itr.next().unwrap().parse().unwrap();\n let d: i64 = itr.next().unwrap().parse().unwrap();\n\n for up in 0..n {\n let down = n - 1 - up;\n let max = a + d * up - c * down;\n let min = a + c * up - d * down;\n if min <= b && b <= max {\n println!(\"YES\");\n return;\n }\n }\n println!(\"NO\");\n}", "difficulty": "easy"} {"problem_id": "0169", "problem_description": "Artem is building a new robot. He has a matrix $$$a$$$ consisting of $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{i,j}$$$ written in it. If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells contain the same value, where two cells are called adjacent if they share a side. Artem wants to increment the values in some cells by one to make $$$a$$$ good.More formally, find a good matrix $$$b$$$ that satisfies the following condition — For all valid ($$$i,j$$$), either $$$b_{i,j} = a_{i,j}$$$ or $$$b_{i,j} = a_{i,j}+1$$$. For the constraints of this problem, it can be shown that such a matrix $$$b$$$ always exists. If there are several such tables, you can output any of them. Please note that you do not have to minimize the number of increments.", "c_code": "int solution() {\n int tc;\n scanf(\"%d\", &tc);\n while (tc--) {\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n int mat[n][m];\n\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n scanf(\"%d\", &mat[i][j]);\n if (mat[i][j] % 2 != (i + j) % 2) {\n ++mat[i][j];\n }\n }\n }\n\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n printf(\"%d \", mat[i][j]);\n }\n printf(\"\\n\");\n }\n }\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 t = s.trim().parse::().unwrap();\n for _ in 0..t {\n s.clear();\n std::io::stdin().read_line(&mut s).unwrap();\n let mut nums = s.split_whitespace();\n let row: u32 = nums.next().unwrap().parse().unwrap();\n let _col: u32 = nums.next().unwrap().parse().unwrap();\n for i in 0..row {\n s.clear();\n std::io::stdin().read_line(&mut s).unwrap();\n for (j, n) in s.split_whitespace().enumerate() {\n let mut value = n.parse::().unwrap();\n if ((j as u32 ^ i ^ value) & 1u32) == 1 {\n value += 1;\n }\n print!(\"{} \", value);\n }\n println!();\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0170", "problem_description": "A permutation of length $$$n$$$ is an array $$$p=[p_1,p_2,\\dots,p_n]$$$, which contains every integer from $$$1$$$ to $$$n$$$ (inclusive) and, moreover, each number appears exactly once. For example, $$$p=[3,1,4,2,5]$$$ is a permutation of length $$$5$$$.For a given number $$$n$$$ ($$$n \\ge 2$$$), find a permutation $$$p$$$ in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between $$$2$$$ and $$$4$$$, inclusive. Formally, find such permutation $$$p$$$ that $$$2 \\le |p_i - p_{i+1}| \\le 4$$$ for each $$$i$$$ ($$$1 \\le i < n$$$).Print any such permutation for the given integer $$$n$$$ or determine that it does not exist.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n int n;\n scanf(\"%d\", &n);\n if (n < 4) {\n printf(\"-1\\n\");\n } else {\n for (int i = n; i >= 1; i--) {\n if (i & 1) {\n printf(\"%d \", i);\n }\n }\n printf(\"4 2 \");\n for (int i = 6; i <= n; i++) {\n if (!(i & 1)) {\n printf(\"%d \", i);\n }\n }\n printf(\"\\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 n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n if n < 4 {\n println!(\"-1\");\n continue;\n }\n let k = (n - 3) / 2;\n for i in 0..k {\n print!(\"{} \", (k - i - 1) * 2 + 5);\n }\n print!(\"2 4 1 3 \");\n for i in 0..((n - 4) / 2) {\n print!(\"{} \", i * 2 + 6);\n }\n println!();\n }\n}", "difficulty": "easy"} {"problem_id": "0171", "problem_description": "A bracket sequence is a string containing only characters \"(\" and \")\". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters \"1\" and \"+\" between the original characters of the sequence. For example, bracket sequences \"()()\" and \"(())\" are regular (the resulting expressions are: \"(1)+(1)\" and \"((1+1)+1)\"), and \")(\", \"(\" and \")\" are not.Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given a regular bracket sequence $$$s$$$ and an integer number $$$k$$$. Your task is to find a regular bracket sequence of length exactly $$$k$$$ such that it is also a subsequence of $$$s$$$.It is guaranteed that such sequence always exists.", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n char s[n];\n scanf(\"%s\", s);\n char v[m];\n int c = 0;\n int cr = 0;\n int cl = 0;\n int flag = 0;\n for (int i = 0; i < n && c < m; i++) {\n if (c == 0 && s[i] != ')') {\n v[c] = s[i];\n cl++;\n c++;\n flag++;\n } else if (c != 0) {\n if (cl < m / 2 && s[i] == '(') {\n v[c] = s[i];\n cl++;\n c++;\n flag++;\n }\n\n else if (cr < m / 2 && flag > 0 && s[i] == ')') {\n v[c] = s[i];\n cr++;\n c++;\n flag--;\n }\n }\n }\n for (int i = 0; i < m; i++) {\n printf(\"%c\", v[i]);\n }\n return 0;\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\n let n = get!(usize);\n let k = get!(usize);\n if n == k {\n let ans = get!(String);\n println!(\"{}\", ans);\n return;\n }\n let s = get().as_bytes();\n\n let mut flag = vec![true; n];\n let mut stack = vec![];\n let mut len = n;\n for i in 0..n {\n if s[i] == b')' {\n flag[i] = false;\n if let Some(j) = stack.pop() {\n flag[j] = false;\n }\n len -= 2;\n if len == k {\n break;\n }\n } else {\n stack.push(i);\n }\n }\n let mut ans = vec![];\n for i in 0..n {\n if flag[i] {\n ans.push(s[i]);\n }\n }\n let ans = String::from_utf8(ans).unwrap();\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0172", "problem_description": "A sequence of square brackets is regular if by inserting symbols \"+\" and \"1\" into it, you can get a regular mathematical expression from it. For example, sequences \"[[]][]\", \"[]\" and \"[[][[]]]\" — are regular, at the same time \"][\", \"[[]\" and \"[[]]][\" — are irregular. Draw the given sequence using a minimalistic pseudographics in the strip of the lowest possible height — use symbols '+', '-' and '|'. For example, the sequence \"[[][]][]\" should be represented as: +- -++- -+ |+- -++- -+|| ||| || ||| ||+- -++- -+|| |+- -++- -+Each bracket should be represented with the hepl of one or more symbols '|' (the vertical part) and symbols '+' and '-' as on the example which is given above.Brackets should be drawn without spaces one by one, only dividing pairs of consecutive pairwise brackets with a single-space bar (so that the two brackets do not visually merge into one symbol). The image should have the minimum possible height. The enclosed bracket is always smaller than the surrounding bracket, but each bracket separately strives to maximize the height of the image. So the pair of final brackets in the example above occupies the entire height of the image.Study carefully the examples below, they adequately explain the condition of the problem. Pay attention that in this problem the answer (the image) is unique.", "c_code": "int solution(void) {\n int i;\n int N;\n int max = 0;\n int k;\n int a = 0;\n char p;\n scanf(\"%d\", &N);\n char A[N];\n scanf(\"%c\", &p);\n for (i = 0; i < N; i++) {\n scanf(\"%c\", &A[i]);\n }\n for (i = 0; i < N; i++) {\n if (i != 0 && A[i] == '[') {\n a = a + 2;\n }\n if (A[i] == ']' && i != N - 1) {\n a = a - 2;\n }\n if (a > max) {\n max = a;\n }\n }\n max = max + 3;\n for (i = 0; i < max / 2 + 1; i++) {\n for (k = 0, a = 0; k < N; k++) {\n if (A[k] == '[') {\n a++;\n }\n if (a - i == 1 && A[k] == '[' && A[k + 1] == '[') {\n printf(\"+-\");\n }\n if (a - i == 1 && A[k] == '[' && A[k + 1] == ']') {\n printf(\"+- \");\n }\n if (a - i == 1 && A[k] == ']') {\n printf(\"-+\");\n }\n if (a - i >= 2 && A[k] == '[' && A[k + 1] == ']' && A[k - 1] == '[') {\n printf(\" \");\n }\n if (a - i >= 2 && A[k] == '[' && A[k + 1] == ']' && A[k - 1] == ']') {\n printf(\" \");\n }\n if (a - i >= 2 && A[k] == '[' && A[k + 1] == '[' && A[k - 1] == '[') {\n printf(\" \");\n }\n if (a - i >= 2 && A[k] == '[' && A[k + 1] == '[' && A[k - 1] == ']') {\n printf(\" \");\n }\n if (a - i >= 2 && A[k] == ']' && A[k - 1] == '[' && A[k + 1] == ']') {\n printf(\" \");\n }\n if (a - i >= 2 && A[k] == ']' && A[k - 1] == '[' && A[k + 1] == '[') {\n printf(\" \");\n }\n if (a - i >= 2 && A[k] == ']' && A[k - 1] == ']' && A[k + 1] == ']') {\n printf(\" \");\n }\n if (a - i >= 2 && A[k] == ']' && A[k - 1] == ']' && A[k + 1] == '[') {\n printf(\" \");\n }\n if (a - i <= 0 && A[k] == '[' && A[k + 1] == '[') {\n printf(\"|\");\n }\n if (a - i <= 0 && A[k] == '[' && A[k + 1] == ']') {\n printf(\"| \");\n }\n if (a - i <= 0 && A[k] == ']') {\n printf(\"|\");\n }\n if (A[k] == ']') {\n a--;\n }\n }\n printf(\"\\n\");\n }\n for (i = max / 2 + 1; i < max; i++) {\n for (k = 0, a = 0; k < N; k++) {\n if (A[k] == '[') {\n a++;\n }\n if (i + a == max && A[k] == '[' && A[k + 1] == '[') {\n printf(\"+-\");\n }\n if (i + a == max && A[k] == '[' && A[k + 1] == ']') {\n printf(\"+- \");\n }\n if (i + a == max && A[k] == ']') {\n printf(\"-+\");\n }\n if (a + i > max && A[k] == '[' && A[k + 1] == ']' && A[k - 1] == '[') {\n printf(\" \");\n }\n if (a + i > max && A[k] == '[' && A[k + 1] == ']' && A[k - 1] == ']') {\n printf(\" \");\n }\n if (a + i > max && A[k] == '[' && A[k + 1] == '[' && A[k - 1] == '[') {\n printf(\" \");\n }\n if (a + i > max && A[k] == '[' && A[k + 1] == '[' && A[k - 1] == ']') {\n printf(\" \");\n }\n if (a + i > max && A[k] == ']' && A[k - 1] == '[' && A[k + 1] == ']') {\n printf(\" \");\n }\n if (a + i > max && A[k] == ']' && A[k - 1] == '[' && A[k + 1] == '[') {\n printf(\" \");\n }\n if (a + i > max && A[k] == ']' && A[k - 1] == ']' && A[k + 1] == ']') {\n printf(\" \");\n }\n if (a + i > max && A[k] == ']' && A[k - 1] == ']' && A[k + 1] == '[') {\n printf(\" \");\n }\n if (i + a < max && A[k] == '[' && A[k + 1] == '[') {\n printf(\"|\");\n }\n if (i + a < max && A[k] == '[' && A[k + 1] == ']') {\n printf(\"| \");\n }\n if (i + a < max && A[k] == ']') {\n printf(\"|\");\n }\n if (A[k] == ']') {\n a--;\n }\n }\n printf(\"\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n let mut input_str2 = String::new();\n std::io::stdin()\n .read_line(&mut input_str)\n .expect(\"read_error\");\n std::io::stdin()\n .read_line(&mut input_str2)\n .expect(\"read_error\");\n let brackets: &str = input_str2.trim();\n\n let mut state = [[' '; 300]; 300];\n let mut cur = 0;\n let mut max = 0;\n for ch in brackets.chars() {\n if ch == '[' {\n cur += 1;\n if max < cur {\n max = cur;\n }\n } else if ch == ']' {\n cur -= 1;\n }\n }\n\n cur = 0;\n let mut cl = 0;\n let mut is_left = false;\n for ch in brackets.chars() {\n if ch == '[' {\n for row in cur..(max + max - cur) {\n state[row][cl] = '|';\n }\n state[cur][cl] = '+';\n state[cur][cl + 1] = '-';\n state[max + max - cur][cl] = '+';\n state[max + max - cur][cl + 1] = '-';\n cur += 1;\n cl += 1;\n is_left = true;\n } else if ch == ']' {\n if is_left {\n cl += 3;\n is_left = false;\n }\n cur -= 1;\n for row in cur..(max + max - cur) {\n state[row][cl] = '|';\n }\n state[cur][cl] = '+';\n state[cur][cl - 1] = '-';\n state[max + max - cur][cl] = '+';\n state[max + max - cur][cl - 1] = '-';\n cl += 1;\n }\n }\n\n for i in 0..(max + max + 1) {\n for j in 0..cl {\n print!(\"{}\", state[i][j]);\n }\n println!()\n }\n}", "difficulty": "medium"} {"problem_id": "0173", "problem_description": "Mark is asked to take a group photo of $$$2n$$$ people. The $$$i$$$-th person has height $$$h_i$$$ units.To do so, he ordered these people into two rows, the front row and the back row, each consisting of $$$n$$$ people. However, to ensure that everyone is seen properly, the $$$j$$$-th person of the back row must be at least $$$x$$$ units taller than the $$$j$$$-th person of the front row for each $$$j$$$ between $$$1$$$ and $$$n$$$, inclusive.Help Mark determine if this is possible.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int x;\n scanf(\"%d %d\", &n, &x);\n int ara[2 * n];\n for (int i = 0; i < 2 * n; i++) {\n scanf(\"%d\", &ara[i]);\n }\n for (int i = 0; i < 2 * n - 1; i++) {\n for (int j = 0; j < 2 * n - i - 1; j++) {\n if (ara[j] > ara[j + 1]) {\n int temp = ara[j];\n ara[j] = ara[j + 1];\n ara[j + 1] = temp;\n }\n }\n }\n\n int flag = 1;\n for (int i = 0; i < n; i++) {\n if (ara[n + i] - ara[i] < x) {\n flag = 0;\n break;\n }\n }\n if (flag == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n name = reader,\n tests: usize\n }\n for _ in 0..tests {\n input! {\n use reader,\n n: usize,\n x: u32,\n mut h: [u32; 2 * n]\n }\n h.sort_unstable();\n if (0..n).all(|i| h[i] + x <= h[i + n]) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0174", "problem_description": "There is a frog staying to the left of the string $$$s = s_1 s_2 \\ldots s_n$$$ consisting of $$$n$$$ characters (to be more precise, the frog initially stays at the cell $$$0$$$). Each character of $$$s$$$ is either 'L' or 'R'. It means that if the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'L', the frog can jump only to the left. If the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell $$$0$$$.Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.The frog wants to reach the $$$n+1$$$-th cell. The frog chooses some positive integer value $$$d$$$ before the first jump (and cannot change it later) and jumps by no more than $$$d$$$ cells at once. I.e. if the $$$i$$$-th character is 'L' then the frog can jump to any cell in a range $$$[max(0, i - d); i - 1]$$$, and if the $$$i$$$-th character is 'R' then the frog can jump to any cell in a range $$$[i + 1; min(n + 1; i + d)]$$$.The frog doesn't want to jump far, so your task is to find the minimum possible value of $$$d$$$ such that the frog can reach the cell $$$n+1$$$ from the cell $$$0$$$ if it can jump by no more than $$$d$$$ cells at once. It is guaranteed that it is always possible to reach $$$n+1$$$ from $$$0$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int test;\n scanf(\"%d\", &test);\n while (test--) {\n char s[200002];\n scanf(\"%s\", s);\n int max = 1;\n int i = 0;\n int l = 1;\n while (s[i]) {\n if (s[i] == 'L') {\n l++;\n } else {\n l = 1;\n }\n if (max < l) {\n max = l;\n }\n i++;\n }\n printf(\"%d\\n\", max);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::{self, BufRead};\n let reader = io::stdin();\n let test_cases = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n for _ in 0..test_cases {\n let path: String = format!(\"R{}R\", reader.lock().lines().next().unwrap().unwrap());\n let mut max_d = usize::min_value();\n let mut last_r = usize::min_value();\n for (index, chars) in path.chars().enumerate() {\n if chars == 'R' {\n let distance = index - last_r;\n if distance > max_d {\n max_d = distance;\n }\n last_r = index;\n }\n }\n println!(\"{}\", max_d);\n }\n}", "difficulty": "hard"} {"problem_id": "0175", "problem_description": "

Problem A: ICPC Score Totalizer Software

\n\n\n\n\n\n\n

\nThe International Clown and Pierrot Competition (ICPC), is one of the\nmost distinguished and also the most popular events on earth in the\nshow business.\n

\n\n\n\n

\nOne of the unique features of this contest is the great number of\njudges that sometimes counts up to one hundred. The number of judges\nmay differ from one contestant to another, because judges with any\nrelationship whatsoever with a specific contestant are temporarily\nexcluded for scoring his/her performance.\n

\n\n\n\n

\nBasically, scores given to a contestant's performance by the judges\nare averaged to decide his/her score. To avoid letting judges with\neccentric viewpoints too much influence the score, the highest and the\nlowest scores are set aside in this calculation. If the same highest\nscore is marked by two or more judges, only one of them is ignored.\nThe same is with the lowest score. The average, which may contain\nfractions, are truncated down to obtain final score as an integer.\n

\n\n\n\n

\nYou are asked to write a program that computes the scores of\nperformances, given the scores of all the judges, to speed up the event\nto be suited for a TV program.\n

\n\n\n\n\n\n

Input

\n\n\n\n

\nThe input consists of a number of datasets, each corresponding to a\ncontestant's performance. There are no more than 20 datasets in the input.\n

\n\n\n

\nA dataset begins with a line with an integer n, the number of\njudges participated in scoring the performance (3 ≤ n ≤\n100). Each of the n lines following it has an integral\nscore s (0 ≤ s ≤ 1000) marked by a judge. No\nother characters except for digits to express these numbers are in the\ninput. Judges' names are kept secret.\n

\n\n\n\n

\nThe end of the input is indicated by a line with a single zero in it.\n

\n\n\n\n

Output

\n\n\n

\nFor each dataset, a line containing a single decimal integer\nindicating the score for the corresponding performance should be\noutput. No other characters should be on the output line.\n

\n\n\n\n

Sample Input

\n\n
\n3\n1000\n342\n0\n5\n2\n2\n9\n11\n932\n5\n300\n1000\n0\n200\n400\n8\n353\n242\n402\n274\n283\n132\n402\n523\n0\n
\n\n

Output for the Sample Input

\n\n
\n342\n7\n300\n326\n
", "c_code": "int solution(void) {\n int n;\n\n while (1) {\n scanf(\"%d\", &n);\n if (!n) {\n break;\n }\n\n int point[128] = {0};\n int max = 0;\n int min = 1000;\n int i = 0;\n int j = 0;\n int k = 0;\n int x = 0;\n i = n;\n while (i-- > 0) {\n scanf(\"%d\", &x);\n\n if (x > max) {\n point[j] = max;\n max = x;\n j++;\n } else if (x < min) {\n point[j] = min;\n min = x;\n j++;\n }\n\n else {\n point[j] = x;\n j++;\n }\n }\n int sum = 0;\n for (j = 0; j < n + 2; j++) {\n sum = sum + point[j];\n }\n if (min == 1000) {\n sum = sum - point[1];\n } else {\n sum = sum - 1000;\n }\n printf(\"%d\\n\", sum / (n - 2));\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let n = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let s = s.trim_end().to_owned();\n s.parse::().unwrap()\n };\n if n == 0 {\n std::process::exit(0);\n }\n let mut a = (0..n)\n .map(|_| {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let s = s.trim_end().to_owned();\n s.parse::().unwrap()\n })\n .collect::>();\n a.sort();\n println!(\"{}\", a[1..a.len() - 1].iter().sum::() / (n - 2) as u32);\n }\n}", "difficulty": "medium"} {"problem_id": "0176", "problem_description": "You are given a positive integer $$$n$$$. In one move, you can increase $$$n$$$ by one (i.e. make $$$n := n + 1$$$). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n long long int n;\n long long int s;\n long long int k;\n long long int ans;\n long long int cnt;\n for (; t > 0; t--) {\n scanf(\"%lld %lld\", &n, &s);\n ans = 0;\n for (;;) {\n k = 1;\n while (n / k > 9) {\n k *= 10;\n }\n cnt = 0;\n while (k > 0) {\n cnt += n / k % 10;\n if (cnt > s) {\n break;\n }\n k /= 10;\n }\n if (cnt <= s) {\n break;\n }\n ans += n / (10 * k) * 10 * k + 10 * k - n;\n n = n / (10 * k) * 10 * k + 10 * k;\n }\n printf(\"%lld\\n\", ans);\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 (mut n, s): (i64, i64) = {\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 digit_sum = |n: i64| -> i64 {\n n.to_string()\n .chars()\n .fold(0, |acc, c| acc + c.to_digit(10).unwrap()) as i64\n };\n\n let (mut ans, mut p) = (0, 1);\n while digit_sum(n) > s {\n ans += (10 - n % 10) % 10 * p;\n n = n / 10 + if n % 10 > 0 { 1 } else { 0 };\n p *= 10;\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "0177", "problem_description": "In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a1, a2, ..., an. He remembered that he calculated gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n and put it into a set S. gcd here means the greatest common divisor.Note that even if a number is put into the set S twice or more, it only appears once in the set.Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.", "c_code": "int solution() {\n int n;\n int a[1000];\n scanf(\"%d\", &n);\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &a[i]);\n if (a[i] % a[0]) {\n printf(\"%d\", -1);\n return 0;\n }\n }\n printf(\"%d\\n\", n * 2);\n for (int i = 0; i < n; ++i) {\n printf(\"%d \", a[i]);\n printf(\"%d \", a[0]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let N = s.trim_end().parse::().unwrap();\n s.clear();\n io::stdin().read_line(&mut s).unwrap();\n let arr: Vec = s\n .trim_end()\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n for i in 1..N {\n if !arr[i].is_multiple_of(arr[0]) {\n println!(\"-1\");\n process::exit(0);\n }\n }\n\n println!(\"{}\", (N - 1) * 2 + 1);\n print!(\"{} \", arr[0]);\n for i in 1..N {\n print!(\"{} {} \", arr[i], arr[0]);\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "0178", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There is a bar of chocolate with a height of H blocks and a width of W blocks.\nSnuke is dividing this bar into exactly three pieces.\nHe can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.

\n

Snuke is trying to divide the bar as evenly as possible.\nMore specifically, he is trying to minimize S_{max} - S_{min}, where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.\nFind the minimum possible value of S_{max} - S_{min}.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ H, W ≤ 10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\n
\n
\n
\n
\n
\n

Output

Print the minimum possible value of S_{max} - S_{min}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 5\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n

In the division below, S_{max} - S_{min} = 5 - 5 = 0.

\n
\n\"2a9b2ef47b750c0b7ba3e865d4fb4203.png\"\n
\n
\n
\n
\n
\n
\n

Sample Input 2

4 5\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

In the division below, S_{max} - S_{min} = 8 - 6 = 2.

\n
\n\"a42aae7aaaadc4640ac5cdf88684d913.png\"\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5 5\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n

In the division below, S_{max} - S_{min} = 10 - 6 = 4.

\n
\n\"eb0ad0cb3185b7ae418e21c472ff7f26.png\"\n
\n
\n
\n
\n
\n
\n

Sample Input 4

100000 2\n
\n
\n
\n
\n
\n

Sample Output 4

1\n
\n
\n
\n
\n
\n
\n

Sample Input 5

100000 100000\n
\n
\n
\n
\n
\n

Sample Output 5

50000\n
\n
\n
", "c_code": "int solution() {\n\n int h;\n int w;\n int a;\n int b;\n int blank;\n\n scanf(\"%d %d\", &h, &w);\n\n if (h % 3 == 0 || w % 3 == 0) {\n\n printf(\"0\\n\");\n return 0;\n }\n\n if (h % 2 == 0 && h == w) {\n\n printf(\"%d\\n\", h / 2);\n return 0;\n }\n\n if (h % 2 == 1 && h == w) {\n\n printf(\"%d\\n\", h - 1);\n return 0;\n }\n\n if (h > w) {\n\n blank = h;\n h = w;\n w = blank;\n }\n\n if (h % 2 == 0) {\n\n if (w % 2 == 1 && w < h / 2) {\n printf(\"%d\\n\", w);\n } else {\n printf(\"%d\\n\", h / 2);\n }\n return 0;\n }\n\n if (h % 2 == 1) {\n\n if (w % 2 == 0 && w / 2 < h) {\n printf(\"%d\\n\", w / 2);\n } else {\n printf(\"%d\\n\", h);\n }\n return 0;\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let mut v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let mut d: usize = std::usize::MAX;\n\n for _ in 0..2 {\n for i in 1..v[0] / 2 + 1 {\n let x = i * v[1];\n\n let mut y = ((v[0] - i) / 2) * v[1];\n let mut z = ((v[0] - i) / 2 + (v[0] - i) % 2) * v[1];\n d = cmp::min(d, cmp::max(x, cmp::max(y, z)) - cmp::min(x, cmp::min(x, y)));\n\n y = v[1] / 2 * (v[0] - i);\n z = (v[1] / 2 + v[1] % 2) * (v[0] - i);\n d = cmp::min(d, cmp::max(x, cmp::max(y, z)) - cmp::min(x, cmp::min(x, y)));\n }\n v.reverse();\n }\n println!(\"{}\", d);\n}", "difficulty": "hard"} {"problem_id": "0179", "problem_description": "You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.", "c_code": "int solution() {\n unsigned long long int l;\n unsigned long long int k;\n unsigned long long int j;\n unsigned long long int i;\n unsigned long long int m;\n unsigned long long int n;\n unsigned long long int item;\n unsigned long long int x;\n unsigned long long int y;\n unsigned long long int sum;\n scanf(\"%llu\", &l);\n for (k = 0; k < l; k++) {\n scanf(\"%llu\", &n);\n unsigned long long int a[n];\n unsigned long long int b[n];\n for (i = 0; i < n; i++) {\n scanf(\"%llu %llu\", &a[i], &b[i]);\n }\n if (n % 2 == 1) {\n printf(\"%d\\n\", 1);\n } else {\n for (i = 0; i < n - 1; i++) {\n for (j = i + 1; j < n; j++) {\n if (a[i] > a[j]) {\n item = a[i];\n a[i] = a[j];\n a[j] = item;\n }\n }\n }\n for (i = 0; i < n - 1; i++) {\n for (j = i + 1; j < n; j++) {\n if (b[i] > b[j]) {\n item = b[i];\n b[i] = b[j];\n b[j] = item;\n }\n }\n }\n x = a[n / 2] - a[(n / 2) - 1] + 1;\n y = b[n / 2] - b[(n / 2) - 1] + 1;\n sum = x * y;\n printf(\"%llu\\n\", sum);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut lines = stdin.lock().lines().map(|line| line.unwrap());\n let _n = lines.next().unwrap();\n while let Some(case_len) = lines.next() {\n let case_len: usize = case_len.parse().unwrap();\n let mut xs = Vec::::new();\n let mut ys = Vec::::new();\n for _ in 0..case_len {\n let xy: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|x| x.parse().unwrap())\n .collect();\n xs.push(xy[0]);\n ys.push(xy[1]);\n }\n if case_len % 2 == 1 {\n println!(\"1\");\n continue;\n }\n xs.sort();\n ys.sort();\n let out = (xs[case_len / 2] - xs[case_len / 2 - 1] + 1)\n * (ys[case_len / 2] - ys[case_len / 2 - 1] + 1);\n println!(\"{}\", out);\n }\n}", "difficulty": "medium"} {"problem_id": "0180", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given four digits N_1, N_2, N_3 and N_4. Determine if these can be arranged into the sequence of digits \"1974\".

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq N_1, N_2, N_3, N_4 \\leq 9
  • \n
  • N_1, N_2, N_3 and N_4 are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N_1 N_2 N_3 N_4\n
\n
\n
\n
\n
\n

Output

If N_1, N_2, N_3 and N_4 can be arranged into the sequence of digits \"1974\", print YES; if they cannot, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 7 9 4\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

We can get 1974 by swapping N_2 and N_3.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 9 7 4\n
\n
\n
\n
\n
\n

Sample Output 2

YES\n
\n

We already have 1974 before doing anything.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 2 9 1\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 4

4 9 0 8\n
\n
\n
\n
\n
\n

Sample Output 4

NO\n
\n
\n
", "c_code": "int solution() {\n int n[4];\n int ans = 1;\n for (int i = 0; i < 4; ++i) {\n scanf(\"%d\", &n[i]);\n if (n[i] != 1 && n[i] != 9 && n[i] != 7 && n[i] != 4) {\n ans = 0;\n }\n }\n for (int i = 0; i < 4; ++i) {\n for (int j = 0; j < 4; ++j) {\n if (i != j && n[i] == n[j]) {\n ans = 0;\n }\n }\n }\n if (ans == 1) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let handle = std::io::stdin();\n\n let mut ns: Vec = {\n handle.read_line(&mut buf).unwrap();\n let tmp = buf.split_whitespace().map(|a| a.parse().unwrap()).collect();\n buf.clear();\n tmp\n };\n ns.sort();\n if ns.binary_search(&1).is_ok()\n && ns.binary_search(&9).is_ok()\n && ns.binary_search(&7).is_ok()\n && ns.binary_search(&4).is_ok()\n {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "0181", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer a_i written on it.

\n

They will share these cards.\nFirst, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards.\nHere, both Snuke and Raccoon have to take at least one card.

\n

Let the sum of the integers on Snuke's cards and Raccoon's cards be x and y, respectively.\nThey would like to minimize |x-y|.\nFind the minimum possible value of |x-y|.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 2 \\times 10^5
  • \n
  • -10^{9} \\leq a_i \\leq 10^{9}
  • \n
  • a_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_{N}\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n1 2 3 4 5 6\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x-y|=1. This is the minimum possible value.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n10 -10\n
\n
\n
\n
\n
\n

Sample Output 2

20\n
\n

Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10, y=-10, and thus |x-y|=20.

\n
\n
", "c_code": "int solution(void) {\n long long int n;\n long long int a[200000] = {0};\n long long int sum = 0;\n long long int sunuke = 0;\n long long int araiguma = 0;\n long long int min = 10000000000;\n scanf(\"%lld\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n sum += a[i];\n }\n for (int j = 0; j < n - 1; j++) {\n sunuke += a[j];\n araiguma = sum - sunuke;\n\n if (min > sunuke - araiguma && sunuke - araiguma > 0) {\n min = sunuke - araiguma;\n } else if (min > araiguma - sunuke && araiguma - sunuke > 0) {\n min = araiguma - sunuke;\n } else if (sunuke == araiguma) {\n min = 0;\n break;\n }\n }\n if (min == 10000000000) {\n min = 0;\n }\n printf(\"%lld\", min);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let handle = std::io::stdin();\n handle.read_line(&mut buf).unwrap();\n let n: usize = buf.trim().parse().unwrap();\n buf.clear();\n handle.read_line(&mut buf).unwrap();\n let ls: Vec = buf.split_whitespace().map(|a| a.parse().unwrap()).collect();\n\n let mut sums: Vec = Vec::with_capacity(n);\n let total: isize = ls.iter().fold(0, |a, b| {\n let sum = a + b;\n sums.push(sum);\n sum\n });\n sums.pop();\n let mut min_x = std::isize::MAX;\n for i in sums {\n if (total - 2 * i).abs() < min_x {\n min_x = (total - 2 * i).abs();\n }\n }\n println!(\"{}\", min_x);\n}", "difficulty": "hard"} {"problem_id": "0182", "problem_description": "Vlad went into his appartment house entrance, now he is on the $$$1$$$-th floor. He was going to call the elevator to go up to his apartment.There are only two elevators in his house. Vlad knows for sure that: the first elevator is currently on the floor $$$a$$$ (it is currently motionless), the second elevator is located on floor $$$b$$$ and goes to floor $$$c$$$ ($$$b \\ne c$$$). Please note, if $$$b=1$$$, then the elevator is already leaving the floor $$$1$$$ and Vlad does not have time to enter it. If you call the first elevator, it will immediately start to go to the floor $$$1$$$. If you call the second one, then first it will reach the floor $$$c$$$ and only then it will go to the floor $$$1$$$. It takes $$$|x - y|$$$ seconds for each elevator to move from floor $$$x$$$ to floor $$$y$$$.Vlad wants to call an elevator that will come to him faster. Help him choose such an elevator.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t) {\n int a = 0;\n int b = 0;\n int c = 0;\n scanf(\"%d %d %d\", &a, &b, &c);\n int time1 = 0;\n int time2 = 0;\n time1 = a - 1;\n if (b > c) {\n time2 = b - 1;\n }\n if (b < c) {\n time2 = 2 * c - b - 1;\n }\n if (time1 > time2) {\n printf(\"2\");\n } else if (time1 < time2) {\n printf(\"1\");\n } else {\n printf(\"3\");\n }\n if (t > 1) {\n printf(\"\\n\");\n }\n t--;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut tc = String::new();\n\n io::stdin().read_line(&mut tc).expect(\"\");\n\n let tc: u32 = tc.trim().parse().expect(\"\");\n\n for _ in 0..tc {\n let mut line = String::new();\n io::stdin()\n .read_line(&mut line)\n .expect(\"Failed to read line\");\n\n let inputs: Vec = line\n .split_whitespace()\n .map(|x| x.parse().expect(\"Not an integer!\"))\n .collect();\n\n let [a, b, c] = <[i64; 3]>::try_from(inputs).ok().unwrap();\n\n let time1 = a - 1;\n let time2 = cmp::max(b - c, c - b) + c - 1;\n\n if time1 < time2 {\n println!(\"1\");\n } else if time1 > time2 {\n println!(\"2\");\n } else {\n println!(\"3\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0183", "problem_description": "On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0.One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column.To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to gi, j.Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task!", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n int a[n][m];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &a[i][j]);\n }\n }\n int min0[n];\n int min1[m];\n int count = 0;\n if (n <= m) {\n for (int i = 0; i < n; i++) {\n int min = 501;\n for (int j = 0; j < m; j++) {\n if (a[i][j] < min) {\n min = a[i][j];\n }\n }\n min0[i] = min;\n count += min;\n for (int j = 0; j < m; j++) {\n a[i][j] -= min;\n }\n }\n\n for (int i = 0; i < m; i++) {\n int min = 501;\n for (int j = 0; j < n; j++) {\n if (a[j][i] < min) {\n min = a[j][i];\n }\n }\n min1[i] = min;\n count += min;\n for (int j = 0; j < n; j++) {\n a[j][i] -= min;\n }\n }\n } else {\n for (int i = 0; i < m; i++) {\n int min = 501;\n for (int j = 0; j < n; j++) {\n if (a[j][i] < min) {\n min = a[j][i];\n }\n }\n min1[i] = min;\n count += min;\n for (int j = 0; j < n; j++) {\n a[j][i] -= min;\n }\n }\n\n for (int i = 0; i < n; i++) {\n int min = 501;\n for (int j = 0; j < m; j++) {\n if (a[i][j] < min) {\n min = a[i][j];\n }\n }\n min0[i] = min;\n count += min;\n for (int j = 0; j < m; j++) {\n a[i][j] -= min;\n }\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (a[i][j] != 0) {\n printf(\"-1\");\n return 0;\n }\n }\n }\n printf(\"%d\\n\", count);\n for (int i = 0; i < n; i++) {\n for (int j = 1; j <= min0[i]; j++) {\n printf(\"row %d\\n\", i + 1);\n }\n }\n for (int i = 0; i < m; i++) {\n for (int j = 1; j <= min1[i]; j++) {\n printf(\"col %d\\n\", i + 1);\n }\n }\n}", "rust_code": "fn solution() {\n let mut a = [[0; 100]; 100];\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let v = s.split_whitespace().collect::>();\n let n = v[0].parse::().unwrap();\n let m = v[1].parse::().unwrap();\n for i in 0..n {\n let mut s = String::new();\n let mut j: usize = 0;\n io::stdin().read_line(&mut s).unwrap();\n for x in s.split_whitespace() {\n a[i][j] = x.parse::().unwrap();\n j += 1;\n }\n }\n let mut b = a;\n let mut ans = 0;\n for i in 0..n {\n let mut mi = i32::max_value();\n for j in 0..m {\n mi = cmp::min(mi, b[i][j]);\n }\n for j in 0..m {\n b[i][j] -= mi;\n }\n ans += mi;\n }\n for j in 0..m {\n let mut mi = i32::max_value();\n for i in 0..n {\n mi = cmp::min(mi, b[i][j]);\n }\n for i in 0..n {\n b[i][j] -= mi;\n }\n ans += mi;\n }\n for i in 0..n {\n for j in 0..m {\n if b[i][j] > 0 {\n println!(\"-1\");\n return;\n }\n }\n }\n let mut ans2 = 0;\n let mut b = a;\n for j in 0..m {\n let mut mi = i32::max_value();\n for i in 0..n {\n mi = cmp::min(mi, b[i][j]);\n }\n for i in 0..n {\n b[i][j] -= mi;\n }\n ans2 += mi;\n }\n for i in 0..n {\n let mut mi = i32::max_value();\n for j in 0..m {\n mi = cmp::min(mi, b[i][j]);\n }\n for j in 0..m {\n b[i][j] -= mi;\n }\n ans2 += mi;\n }\n if ans < ans2 {\n println!(\"{}\", ans);\n for i in 0..n {\n let mut mi = i32::max_value();\n for j in 0..m {\n mi = cmp::min(mi, a[i][j]);\n }\n for j in 0..m {\n a[i][j] -= mi;\n }\n while mi > 0 {\n println!(\"row {}\", i + 1);\n mi -= 1;\n }\n }\n for j in 0..m {\n let mut mi = i32::max_value();\n for i in 0..n {\n mi = cmp::min(mi, a[i][j]);\n }\n for i in 0..n {\n a[i][j] -= mi;\n }\n while mi > 0 {\n println!(\"col {}\", j + 1);\n mi -= 1;\n }\n }\n } else {\n println!(\"{}\", ans2);\n for j in 0..m {\n let mut mi = i32::max_value();\n for i in 0..n {\n mi = cmp::min(mi, a[i][j]);\n }\n for i in 0..n {\n a[i][j] -= mi;\n }\n while mi > 0 {\n println!(\"col {}\", j + 1);\n mi -= 1;\n }\n }\n for i in 0..n {\n let mut mi = i32::max_value();\n for j in 0..m {\n mi = cmp::min(mi, a[i][j]);\n }\n for j in 0..m {\n a[i][j] -= mi;\n }\n while mi > 0 {\n println!(\"row {}\", i + 1);\n mi -= 1;\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0184", "problem_description": "

残り物には福がある

\n\n

\nK 個の石から、P 人が順番に1つずつ石を取るゲームがあります。P 人目が石を取った時点で、まだ石が残っていれば、また1人目から順番に1つずつ石を取っていきます。このゲームでは、最後の石を取った人が勝ちとなります。KP が与えられたとき、何人目が勝つか判定するプログラムを作成してください。\n

\n\n

入力

\n

\n入力は以下の形式で与えられる。\n

\n
\nN\nK1 P1\nK2 P2\n:\nKN PN\n
\n\n

\n1行目にはゲームを行う回数 N (1 ≤ N ≤ 100) が与えられる。続く N 行に、i 回目のゲームにおける石の個数 Ki (2 ≤ Ki ≤ 1000) と、ゲームに参加する人数 Pi (2 ≤ Pi ≤ 1000) が与えられる。\n

\n\n

出力

\n

\nそれぞれのゲームについて、何人目が勝つかを1行に出力する。\n

\n\n

入出力例

\n
\n

入力例

\n
\n3\n10 3\n2 10\n4 2\n
\n

出力例

\n
\n1\n2\n2\n
", "c_code": "int solution(void) {\n int times;\n scanf(\"%d\", ×);\n\n int value[times];\n int headCount[times];\n int i;\n\n for (i = 0; i < times; i++) {\n\n scanf(\"%d %d\", &value[i], &headCount[i]);\n }\n\n for (i = 0; i < times; i++) {\n if ((value[i] % headCount[i]) == 0) {\n printf(\"%d\\n\", headCount[i]);\n } else {\n printf(\"%d\\n\", value[i] % headCount[i]);\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 n = lines.next().unwrap().parse().unwrap();\n\n for line in lines.take(n) {\n let (k, p) = {\n let mut iter = line.split(' ').map(|s| s.parse::().unwrap());\n (iter.next().unwrap(), iter.next().unwrap())\n };\n\n println!(\"{}\", (k - 1) % p + 1);\n }\n}", "difficulty": "hard"} {"problem_id": "0185", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.\nThe color of the square at the i-th row and j-th column is represented by the character C_{ij}.

\n

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

\n
\n
\n
\n
\n

Constraints

    \n
  • C_{i,j}(1 \\leq i \\leq 2, 1 \\leq j \\leq 3) is a lowercase English letter.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
C_{11}C_{12}C_{13}\nC_{21}C_{22}C_{23}\n
\n
\n
\n
\n
\n

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

pot\ntop\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

This grid remains the same when rotated 180 degrees.

\n
\n
\n
\n
\n
\n

Sample Input 2

tab\nbet\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n

This grid does not remain the same when rotated 180 degrees.

\n
\n
\n
\n
\n
\n

Sample Input 3

eye\neel\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
", "c_code": "int solution() {\n char *a = (char *)malloc(4);\n char *b = (char *)malloc(4);\n\n scanf(\"%s\", a);\n scanf(\"%s\", b);\n char c = b[0];\n b[0] = b[2];\n b[2] = c;\n if (strcmp(a, b) != 0) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n\n free(a);\n free(b);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).ok();\n\n let mut ans = true;\n for i in 0..3 {\n if s.chars().nth(i) != t.chars().nth(2 - i) {\n ans = false;\n break;\n }\n }\n if ans {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "0186", "problem_description": "As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n + 1];\n if (n == 2) {\n printf(\"NO\");\n return 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n for (int i = 0; i < n; i++) {\n if (a[i] != i + 1 && a[a[i] - 1] != i + 1 && a[a[i] - 1] != a[i] &&\n a[a[a[i] - 1] - 1] == i + 1) {\n printf(\"YES\");\n return 0;\n }\n }\n\n printf(\"NO\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut input = String::new();\n stdin.read_line(&mut input).unwrap();\n input.clear();\n stdin.read_line(&mut input).unwrap();\n let arr: Vec = input\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let mut ok = false;\n for &n in &arr {\n let a = n;\n let b = arr[a - 1];\n let c = arr[b - 1];\n if arr[c - 1] == a && a != b && b != c && c != a {\n ok = true;\n break;\n }\n }\n println!(\"{}\", if ok { \"Yes\" } else { \"No\" });\n}", "difficulty": "medium"} {"problem_id": "0187", "problem_description": "Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \\ldots, p_m$$$, where $$$1 \\leq p_1 < p_2 < \\ldots < p_m \\leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\\max\\limits_{1 \\le i < m} \\left(p_{i + 1} - p_i\\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.", "c_code": "int solution() {\n int n;\n int m;\n int j = 0;\n int max = 0;\n scanf(\"%d%d \", &n, &m);\n char a[n + 1];\n char b[m + 1];\n int c[m + 1];\n int d[m + 1];\n scanf(\"%s%s\", a, b);\n for (int i = 0; i < n && j < m; i++) {\n if (a[i] == b[j]) {\n c[j] = i;\n j++;\n }\n }\n j--;\n for (int i = n - 1; i >= 0 && j >= 0; i--) {\n if (a[i] == b[j]) {\n d[j] = i;\n j--;\n }\n }\n c[m] = n;\n for (int i = 1; i < m; i++) {\n if (d[i] - c[i - 1] > max) {\n max = d[i] - c[i - 1];\n }\n }\n printf(\"%d\", max);\n}", "rust_code": "fn solution() {\n let stdin_handle = std::io::stdin();\n let stdin = stdin_handle.lock();\n let mut it = stdin.lines().skip(1);\n let (a, b) = (it.next().unwrap().unwrap(), it.next().unwrap().unwrap());\n let (ab, bb) = (a.as_bytes(), b.as_bytes());\n let mut mls = Vec::::with_capacity(b.len());\n let mut mrs = Vec::::with_capacity(b.len());\n for i in 0..a.len() {\n if mls.len() >= b.len() {\n break;\n }\n if bb[mls.len()] == ab[i] {\n mls.push(i as u32);\n }\n }\n for i in (0..a.len()).rev() {\n if mrs.len() >= b.len() {\n break;\n }\n if bb[b.len() - mrs.len() - 1] == ab[i] {\n mrs.push(i as u32);\n }\n }\n mrs.reverse();\n let mut ans = 1;\n for i in 0..b.len() - 1 {\n ans = std::cmp::max(ans, mrs[i + 1] - mls[i]);\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0188", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. He cannot do any other calculation.

\n

Given are two integers A and B.

\n

If Takahashi can calculate A \\times B, print the result; if he cannot, print -1 instead.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A \\leq 20
  • \n
  • 1 \\leq B \\leq 20
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

If Takahashi can calculate A \\times B, print the result; if he cannot, print -1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 5\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

2 \\times 5 = 10.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 10\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

5\\times 10 = 50, but Takahashi cannot do this calculation, so print -1 instead.

\n
\n
\n
\n
\n
\n

Sample Input 3

9 9\n
\n
\n
\n
\n
\n

Sample Output 3

81\n
\n
\n
", "c_code": "int solution(void) {\n int A = 0;\n int B = 0;\n scanf(\"%d%d\", &A, &B);\n printf(\"%d\\n\", ((1 <= A && A <= 9) && (1 <= B && B <= 9)) ? A * B : -1);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n println!(\n \"{}\",\n if s.trim().len() == 3 {\n s.chars()\n .filter_map(|t| str::parse::(&t.to_string()).ok())\n .product::()\n } else {\n -1\n }\n );\n}", "difficulty": "hard"} {"problem_id": "0189", "problem_description": "

Finding a Word


\n\n

\nWrite a program which reads a word W and a text T, and prints the number of word W which appears in text T\n

\n\n

\n T consists of string Ti separated by space characters and newlines. Count the number of Ti which equals to W. The word and text are case insensitive.\n

\n\n

Constraints

\n\n
    \n
  • The length of W ≤ 10
  • \n
  • W consists of lower case letters
  • \n
  • The length of T in a line ≤ 1000
  • \n
\n\n\n

Input

\n\n

\n In the first line, the word W is given. In the following lines, the text T is given separated by space characters and newlines.\n

\n\n

\n\"END_OF_TEXT\" indicates the end of the text.\n

\n\n

Output

\n\n

\n Print the number of W in the text.\n

\n\n\n

Sample Input

\n\n
\ncomputer\nNurtures computer scientists and highly-skilled computer engineers\nwho will create and exploit \"knowledge\" for the new era.\nProvides an outstanding computer environment.\nEND_OF_TEXT\n
\n\n

Sample Output

\n\n
\n3\n
", "c_code": "int solution() {\n char w[11];\n char t[1001];\n int cnt = 0;\n int i = 0;\n scanf(\"%s\", w);\n for (i = 0; w[i] != '\\0'; i++) {\n if ('A' <= w[i] && w[i] <= 'Z') {\n w[i] += 'a' - 'A';\n }\n }\n while (1) {\n scanf(\"%s\", t);\n if (!strcmp(\"END_OF_TEXT\", t)) {\n break;\n }\n for (i = 0; t[i] != '\\0'; i++) {\n if ('A' <= t[i] && t[i] <= 'Z') {\n t[i] += 'a' - 'A';\n }\n }\n if (!strcmp(w, t)) {\n cnt++;\n }\n }\n printf(\"%d\\n\", cnt);\n return 0;\n}", "rust_code": "fn solution() {\n let mut w = String::new();\n let mut s = String::new();\n stdin().read_line(&mut w).unwrap();\n let w = w.trim().to_lowercase();\n stdin().read_to_string(&mut s).unwrap();\n println!(\n \"{}\",\n s.split_whitespace().fold(0, |a, e| {\n if e.to_lowercase() == w {\n a + 1\n } else {\n a\n }\n })\n );\n}", "difficulty": "hard"} {"problem_id": "0190", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

M-kun is a student in Aoki High School, where a year is divided into N terms.
\nThere is an exam at the end of each term. According to the scores in those exams, a student is given a grade for each term, as follows:

\n
    \n
  • For the first through (K-1)-th terms: not given.
  • \n
  • For each of the K-th through N-th terms: the multiplication of the scores in the last K exams, including the exam in the graded term.
  • \n
\n

M-kun scored A_i in the exam at the end of the i-th term.
\nFor each i such that K+1 \\leq i \\leq N, determine whether his grade for the i-th term is strictly greater than the grade for the (i-1)-th term.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 2 \\leq N \\leq 200000
  • \n
  • 1 \\leq K \\leq N-1
  • \n
  • 1 \\leq A_i \\leq 10^{9}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N K\nA_1 A_2 A_3 \\ldots A_N\n
\n
\n
\n
\n
\n

Output

\n

Print the answer in N-K lines.
\nThe i-th line should contain Yes if the grade for the (K+i)-th term is greater than the grade for the (K+i-1)-th term, and No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n96 98 95 100 20\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\nNo\n
\n

His grade for each term is computed as follows:

\n
    \n
  • 3-rd term: (96 \\times 98 \\times 95) = 893760
  • \n
  • 4-th term: (98 \\times 95 \\times 100) = 931000
  • \n
  • 5-th term: (95 \\times 100 \\times 20) = 190000
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 2\n1001 869120 1001\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

Note that the output should be No if the grade for the 3-rd term is equal to the grade for the 2-nd term.

\n
\n
\n
\n
\n
\n

Sample Input 3

15 7\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\nYes\nNo\nYes\nYes\nNo\nYes\nYes\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int k = 0;\n int i;\n int a[200020] = {0};\n\n scanf(\"%d %d\", &n, &k);\n\n for (i = 1; i <= n; i++) {\n scanf(\"%d\", &(a[i]));\n if (i >= (k + 1)) {\n if (a[i] > a[i - k]) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n }\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 let a: 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 for i in k..n {\n if a[i - k] < a[i] {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0191", "problem_description": "The girl named Masha was walking in the forest and found a complete binary tree of height $$$n$$$ and a permutation $$$p$$$ of length $$$m=2^n$$$.A complete binary tree of height $$$n$$$ is a rooted tree such that every vertex except the leaves has exactly two sons, and the length of the path from the root to any of the leaves is $$$n$$$. The picture below shows the complete binary tree for $$$n=2$$$.A permutation is an array consisting of $$$n$$$ different integers from $$$1$$$ to $$$n$$$. For example, [$$$2,3,1,5,4$$$] is a permutation, but [$$$1,2,2$$$] is not ($$$2$$$ occurs twice), and [$$$1,3,4$$$] is also not a permutation ($$$n=3$$$, but there is $$$4$$$ in the array).Let's enumerate $$$m$$$ leaves of this tree from left to right. The leaf with the number $$$i$$$ contains the value $$$p_i$$$ ($$$1 \\le i \\le m$$$).For example, if $$$n = 2$$$, $$$p = [3, 1, 4, 2]$$$, the tree will look like this: Masha considers a tree beautiful if the values in its leaves are ordered from left to right in increasing order.In one operation, Masha can choose any non-leaf vertex of the tree and swap its left and right sons (along with their subtrees).For example, if Masha applies this operation to the root of the tree discussed above, it will take the following form: Help Masha understand if she can make a tree beautiful in a certain number of operations. If she can, then output the minimum number of operations to make the tree beautiful.", "c_code": "int solution() {\n int n;\n int i;\n int j;\n int k;\n int x;\n int m;\n int a;\n int b;\n int z;\n scanf(\"%d\", &a);\n for (b = 1; b <= a; b++) {\n scanf(\"%d\", &n);\n int s[n];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &s[i]);\n }\n m = 0;\n for (j = 2; j <= n; j = j * 2) {\n for (i = 0; i < n; i = i + j) {\n if (s[i] > s[i + (j / 2)]) {\n for (z = i; z < i + j / 2; z++) {\n x = s[z];\n s[z] = s[z + (j / 2)];\n s[z + (j / 2)] = x;\n }\n m = m + 1;\n }\n }\n }\n for (k = 0; k < n; k++) {\n if (s[k] != k + 1) {\n printf(\"-1\\n\");\n break;\n }\n if (s[k] == k + 1 && k == n - 1) {\n printf(\"%d\\n\", m);\n }\n }\n }\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n for _ in 0..t {\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\n let mut p: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let mut interval = 1;\n let mut ans = 0;\n let mut ansflag = 1;\n\n for _i in 0..32 {\n if interval == n {\n break;\n }\n if ansflag == 0 {\n break;\n }\n\n let mut now = 0;\n while now < n {\n if p[now] > p[now + interval] {\n ans += 1;\n p.swap(now, now + interval);\n }\n if p[now + interval] - p[now] != interval {\n ansflag = 0;\n break;\n }\n\n now += interval * 2;\n }\n interval *= 2;\n }\n\n if ansflag == 0 {\n println!(\"{}\", -1);\n } else {\n println!(\"{}\", ans);\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0192", "problem_description": "A sequence of non-negative integers $$$a_1, a_2, \\dots, a_n$$$ is called growing if for all $$$i$$$ from $$$1$$$ to $$$n - 1$$$ all ones (of binary representation) in $$$a_i$$$ are in the places of ones (of binary representation) in $$$a_{i + 1}$$$ (in other words, $$$a_i \\:\\&\\: a_{i + 1} = a_i$$$, where $$$\\&$$$ denotes bitwise AND). If $$$n = 1$$$ then the sequence is considered growing as well.For example, the following four sequences are growing: $$$[2, 3, 15, 175]$$$ — in binary it's $$$[10_2, 11_2, 1111_2, 10101111_2]$$$; $$$[5]$$$ — in binary it's $$$[101_2]$$$; $$$[1, 3, 7, 15]$$$ — in binary it's $$$[1_2, 11_2, 111_2, 1111_2]$$$; $$$[0, 0, 0]$$$ — in binary it's $$$[0_2, 0_2, 0_2]$$$. The following three sequences are non-growing: $$$[3, 4, 5]$$$ — in binary it's $$$[11_2, 100_2, 101_2]$$$; $$$[5, 4, 3]$$$ — in binary it's $$$[101_2, 100_2, 011_2]$$$; $$$[1, 2, 4, 8]$$$ — in binary it's $$$[0001_2, 0010_2, 0100_2, 1000_2]$$$. Consider two sequences of non-negative integers $$$x_1, x_2, \\dots, x_n$$$ and $$$y_1, y_2, \\dots, y_n$$$. Let's call this pair of sequences co-growing if the sequence $$$x_1 \\oplus y_1, x_2 \\oplus y_2, \\dots, x_n \\oplus y_n$$$ is growing where $$$\\oplus$$$ denotes bitwise XOR.You are given a sequence of integers $$$x_1, x_2, \\dots, x_n$$$. Find the lexicographically minimal sequence $$$y_1, y_2, \\dots, y_n$$$ such that sequences $$$x_i$$$ and $$$y_i$$$ are co-growing.The sequence $$$a_1, a_2, \\dots, a_n$$$ is lexicographically smaller than the sequence $$$b_1, b_2, \\dots, b_n$$$ if there exists $$$1 \\le k \\le n$$$ such that $$$a_i = b_i$$$ for any $$$1 \\le i < k$$$ but $$$a_k < b_k$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int x[n];\n int y[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &x[i]);\n }\n y[0] = 0;\n for (int i = 1; i < n; i++) {\n if (((x[i - 1] ^ y[i - 1]) & x[i]) == (x[i - 1] ^ y[i - 1])) {\n y[i] = 0;\n } else {\n y[i] = ((x[i - 1] ^ y[i - 1]) ^ x[i]) & (x[i - 1] ^ y[i - 1]);\n }\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d \", y[i]);\n }\n printf(\"\\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\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 '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 s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let mut px = it.next().unwrap();\n let mut py = 0;\n write!(&mut output, \"0 \").unwrap();\n\n for x in it {\n let y = !x & (px ^ py);\n write!(&mut output, \"{} \", y).unwrap();\n\n px = x;\n py = y;\n }\n\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0193", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Given is a string S consisting of digits from 1 through 9.

\n

Find the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the following condition:

\n

Condition: In base ten, the i-th through j-th characters of S form an integer that is a multiple of 2019.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ |S| ≤ 200000
  • \n
  • S is a string consisting of digits from 1 through 9.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1817181712114\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

Three pairs - (1,5), (5,9), and (9,13) - satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 2

14282668646\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2119\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

No pairs satisfy the condition.

\n
\n
", "c_code": "int solution(void) {\n long long dp[2020] = {0};\n long long result = 0;\n char s[200010];\n scanf(\"%s\", s);\n int n = strlen(s);\n\n long long tmp = 1;\n long long sum = 0;\n for (int i = n - 1; i >= 0; i--) {\n sum += (tmp * (long long)(s[i] - '0')) % 2019;\n sum %= 2019;\n dp[sum]++;\n tmp = (tmp * 10) % 2019;\n }\n result = dp[0];\n for (int i = 0; i < 2019; i++) {\n result += dp[i] * (dp[i] - 1) / 2;\n }\n printf(\"%lld\", result);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n stdin().read_line(&mut s).ok();\n let s: Vec = s.trim().chars().collect();\n\n let mut counts: Vec = vec![0; 2019];\n counts[0] += 1;\n\n let mut base = 1;\n let mut temp_mod = 0;\n for &s_i in s.iter().rev() {\n temp_mod = (temp_mod + (s_i as usize - 48) * base) % 2019;\n base = (base * 10) % 2019;\n if s_i != '0' {\n counts[temp_mod] += 1;\n }\n }\n\n let mut res = 0;\n for counts_i in counts {\n if counts_i > 1 {\n res += counts_i * (counts_i - 1) / 2;\n }\n }\n println!(\"{}\", res);\n}", "difficulty": "easy"} {"problem_id": "0194", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally \"Seven-Five-Three numbers\") are there?

\n

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

\n
    \n
  • When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N < 10^9
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

575\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

\n
\n
\n
\n
\n
\n

Sample Input 2

3600\n
\n
\n
\n
\n
\n

Sample Output 2

13\n
\n

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

\n
\n
\n
\n
\n
\n

Sample Input 3

999999999\n
\n
\n
\n
\n
\n

Sample Output 3

26484\n
\n
\n
", "c_code": "int solution() {\n int n;\n int s[30000];\n int ans = 0;\n scanf(\"%d\", &n);\n s[0] = 3;\n s[1] = 5;\n s[2] = 7;\n for (size_t i = 1; i < 9841; i++) {\n s[3 * i] = s[i - 1] * 10 + 3;\n s[(3 * i) + 1] = s[i - 1] * 10 + 5;\n s[(3 * i) + 2] = s[i - 1] * 10 + 7;\n }\n for (size_t j = 12; j < 29523; j++) {\n int cn3 = 0;\n int cn5 = 0;\n int cn7 = 0;\n if (s[j] > n) {\n break;\n }\n while (s[j] != 0) {\n s[j] % 10 == 3 ? cn3++ : s[j] % 10 == 5 ? cn5++ : cn7++;\n s[j] /= 10;\n }\n if (cn3 > 0 && cn5 > 0 && cn7 > 0) {\n ans++;\n }\n }\n printf(\"%d\\n\", ans);\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\n let mut ans = 0;\n let mut q = std::collections::VecDeque::new();\n q.push_back(3usize);\n q.push_back(5);\n q.push_back(7);\n\n while let Some(v) = q.pop_front() {\n if v > n {\n break;\n }\n\n let mut c3 = false;\n let mut c5 = false;\n let mut c7 = false;\n let mut t = v;\n while t > 0 {\n match t % 10 {\n 3 => c3 = true,\n 5 => c5 = true,\n 7 => c7 = true,\n _ => continue,\n }\n t /= 10;\n }\n if c3 && c5 && c7 {\n ans += 1;\n }\n q.push_back(v * 10 + 3);\n q.push_back(v * 10 + 5);\n q.push_back(v * 10 + 7);\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0195", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters.

\n

You can choose any two indices i and j such that 1 \\leq i \\leq j \\leq n and reverse substring A_i A_{i+1} ... A_j.

\n

You can perform this operation at most once.

\n

How many different strings can you obtain?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |A| \\leq 200,000
  • \n
  • A consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A\n
\n
\n
\n
\n
\n

Output

Print the number of different strings you can obtain by reversing any substring in A at most once.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

aatt\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

You can obtain aatt (don't do anything), atat (reverse A[2..3]), atta (reverse A[2..4]), ttaa (reverse A[1..4]) and taat (reverse A[1..3]).

\n
\n
\n
\n
\n
\n

Sample Input 2

xxxxxxxxxx\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

Whatever substring you reverse, you'll always get xxxxxxxxxx.

\n
\n
\n
\n
\n
\n

Sample Input 3

abracadabra\n
\n
\n
\n
\n
\n

Sample Output 3

44\n
\n
\n
", "c_code": "int solution(void) {\n char str[200000];\n scanf(\"%s\", str);\n long long length = strlen(str);\n char abc[26] = \"abcdefghijklmnopqrstuvwxyz\";\n long long count[26];\n long long temp = 0;\n for (int i = 0; i < 26; i++) {\n for (int j = 0; j < length; j++) {\n if (str[j] == abc[i]) {\n temp++;\n }\n }\n count[i] = temp;\n temp = 0;\n }\n long long sum = 0;\n for (int i = 0; i < 26; i++) {\n if (count[i] > 1) {\n sum += count[i] * (count[i] - 1) / 2;\n }\n }\n\n printf(\"%lld\", (length * (length - 1) / 2) + 1 - sum);\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 buf_it = buf.split_whitespace();\n let A = buf_it.next().unwrap().chars().collect::>();\n\n let N = A.len();\n let mut cnt = HashMap::new();\n let mut ans = 1;\n\n cnt.insert(A[0], 1);\n for i in 1..N {\n let n = *cnt.get(&A[i]).unwrap_or(&0);\n ans += i - n;\n cnt.insert(A[i], n + 1);\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0196", "problem_description": "Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :Initially, the company name is ???.Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.In the end, the company name is oio.Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)", "c_code": "int solution(int argc, char const *argv[]) {\n int olegi[26];\n int igori[26];\n int iter;\n for (iter = 0; iter < 26; iter++) {\n olegi[iter] = 0;\n igori[iter] = 0;\n }\n\n int oleg[300000];\n int igor[300000];\n int len = 0;\n char ch;\n scanf(\"%c\", &ch);\n while (ch != '\\n' && ch != '\\0') {\n len++;\n oleg[len - 1] = ch;\n olegi[(int)ch - 97]++;\n scanf(\"%c\", &ch);\n }\n\n len = 0;\n scanf(\"%c\", &ch);\n while (ch != '\\n' && ch != '\\0') {\n len++;\n igor[len - 1] = ch;\n igori[(int)ch - 97]++;\n scanf(\"%c\", &ch);\n }\n\n int i = 0;\n int j = 1;\n int k = 0;\n while (olegi[k] == 0 && k < 26) {\n k++;\n }\n int m = 25;\n while (igori[m] == 0 && m >= 0) {\n m--;\n }\n int turn = 0;\n int name[300000];\n int flag = 0;\n while (i < len) {\n if (j >= len) {\n if (turn == 0) {\n name[i] = k;\n olegi[k]--;\n while (olegi[k] == 0 && k < 26) {\n k++;\n }\n } else {\n name[i] = m;\n igori[m]--;\n while (igori[m] == 0 && m >= 0) {\n m--;\n }\n }\n i = j;\n }\n if (turn == 0) {\n if (k < m) {\n name[i] = k;\n olegi[k]--;\n while (olegi[k] == 0 && k < 26) {\n k++;\n }\n i = j;\n } else {\n if ((len - j) % 2 == 0) {\n flag = 1;\n } else {\n flag = 2;\n }\n break;\n }\n turn = 1;\n } else {\n if (m > k) {\n name[i] = m;\n igori[m]--;\n while (igori[m] == 0 && m >= 0) {\n m--;\n }\n i = j;\n } else {\n if ((len - j) % 2 == 0) {\n flag = 2;\n } else {\n flag = 1;\n }\n break;\n }\n turn = 0;\n }\n j++;\n }\n if (flag == 1) {\n while (i < len) {\n name[i] = k;\n olegi[k]--;\n name[j] = m;\n igori[m]--;\n while (olegi[k] == 0 && k < 26) {\n k++;\n }\n while (igori[m] == 0 && m >= 0) {\n m--;\n }\n i = j;\n j++;\n i = j;\n j++;\n }\n }\n if (flag == 2) {\n while (i < len) {\n name[i] = m;\n olegi[k]--;\n name[j] = k;\n igori[m]--;\n while (olegi[k] == 0 && k < 26) {\n k++;\n }\n while (igori[m] == 0 && m >= 0) {\n m--;\n }\n i = j;\n j++;\n i = j;\n j++;\n }\n }\n i = 0;\n while (i < len) {\n printf(\"%c\", name[i] + 'a');\n i++;\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: Vec = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n input.trim().chars().collect()\n };\n\n let mut t: Vec = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n input.trim().chars().collect()\n };\n\n let n = s.len();\n\n s.sort();\n t.sort();\n\n let q = n.div_ceil(2);\n\n s.truncate(q);\n\n t.reverse();\n t.truncate(n / 2);\n\n let mut s = VecDeque::from(s);\n let mut t = VecDeque::from(t);\n\n let mut ans = vec!['a'; n];\n\n let mut i = 0;\n let mut j = n - 1;\n\n for k in 0..n {\n let cc = match (s.front(), t.front()) {\n (Some(s), Some(t)) if s >= t => true,\n _ => false,\n };\n\n let r = match k % 2 {\n 0 => &mut s,\n 1 => &mut t,\n _ => &mut s,\n };\n\n if cc {\n ans[j] = r.pop_back().unwrap();\n j -= 1;\n } else {\n ans[i] = r.pop_front().unwrap();\n i += 1;\n }\n }\n\n for c in ans {\n print!(\"{}\", c);\n }\n\n println!();\n}", "difficulty": "hard"} {"problem_id": "0197", "problem_description": "You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building. The picture illustrates the first example. You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.", "c_code": "int solution() {\n int n;\n int h;\n int a;\n int b;\n int k;\n int ta[10005];\n int ha[10005];\n int tb[10005];\n int hb[10005];\n int i;\n scanf(\"%d%d%d%d%d\", &n, &h, &a, &b, &k);\n for (i = 0; i < k; i++) {\n scanf(\"%d%d%d%d\", &ta[i], &ha[i], &tb[i], &hb[i]);\n }\n for (i = 0; i < k; i++) {\n if (ta[i] == tb[i]) {\n if (ha[i] > hb[i]) {\n printf(\"%d\\n\", ha[i] - hb[i]);\n } else {\n printf(\"%d\\n\", hb[i] - ha[i]);\n }\n }\n if (ta[i] != tb[i] && ha[i] >= a && ha[i] <= b) {\n if (ta[i] > tb[i]) {\n if (ha[i] > hb[i]) {\n printf(\"%d\\n\", ta[i] - tb[i] + ha[i] - hb[i]);\n } else {\n printf(\"%d\\n\", ta[i] - tb[i] + hb[i] - ha[i]);\n }\n } else {\n if (ha[i] > hb[i]) {\n printf(\"%d\\n\", tb[i] - ta[i] + ha[i] - hb[i]);\n } else {\n printf(\"%d\\n\", tb[i] - ta[i] + hb[i] - ha[i]);\n }\n }\n }\n if (ta[i] != tb[i] && ha[i] < a) {\n if (ta[i] > tb[i]) {\n if (a > hb[i]) {\n printf(\"%d\\n\", a - ha[i] + ta[i] - tb[i] + a - hb[i]);\n } else {\n printf(\"%d\\n\", a - ha[i] + ta[i] - tb[i] + hb[i] - a);\n }\n } else {\n if (hb[i] > a) {\n printf(\"%d\\n\", a - ha[i] + tb[i] - ta[i] - a + hb[i]);\n } else {\n printf(\"%d\\n\", a - ha[i] + tb[i] - ta[i] - hb[i] + a);\n }\n }\n }\n if (ta[i] != tb[i] && ha[i] > b) {\n if (ta[i] > tb[i]) {\n if (b > hb[i]) {\n printf(\"%d\\n\", -b + ha[i] + ta[i] - tb[i] + b - hb[i]);\n } else {\n printf(\"%d\\n\", -b + ha[i] + ta[i] - tb[i] - b + hb[i]);\n }\n } else {\n if (b < hb[i]) {\n printf(\"%d\\n\", -b + ha[i] + tb[i] - ta[i] - b + hb[i]);\n } else {\n printf(\"%d\\n\", -b + ha[i] + tb[i] - ta[i] - hb[i] + b);\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n let mut line_words = line.split_whitespace();\n let _n: i32 = line_words.next().unwrap().parse().unwrap();\n let _h: i32 = line_words.next().unwrap().parse().unwrap();\n let a: i32 = line_words.next().unwrap().parse().unwrap();\n let b: i32 = line_words.next().unwrap().parse().unwrap();\n let k: i32 = line_words.next().unwrap().parse().unwrap();\n for _ in 0..k {\n let mut res;\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let mut line_words = line.split_whitespace();\n let ta: i32 = line_words.next().unwrap().parse().unwrap();\n let fa: i32 = line_words.next().unwrap().parse().unwrap();\n let tb: i32 = line_words.next().unwrap().parse().unwrap();\n let fb: i32 = line_words.next().unwrap().parse().unwrap();\n\n if ta == tb {\n res = (fa - fb).abs();\n } else {\n res = (ta - tb).abs();\n if fa < a && fb < a {\n res += 2 * a - fa - fb;\n } else if fa > b && fb > b {\n res += fa + fb - 2 * b;\n } else {\n res += (fa - fb).abs();\n }\n }\n println!(\"{}\", res);\n }\n}", "difficulty": "easy"} {"problem_id": "0198", "problem_description": "There are $$$n$$$ piranhas with sizes $$$a_1, a_2, \\ldots, a_n$$$ in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other piranhas in the aquarium (except itself, of course). Other piranhas will do nothing while the dominant piranha will eat them.Because the aquarium is pretty narrow and long, the piranha can eat only one of the adjacent piranhas during one move. Piranha can do as many moves as it needs (or as it can). More precisely: The piranha $$$i$$$ can eat the piranha $$$i-1$$$ if the piranha $$$i-1$$$ exists and $$$a_{i - 1} < a_i$$$. The piranha $$$i$$$ can eat the piranha $$$i+1$$$ if the piranha $$$i+1$$$ exists and $$$a_{i + 1} < a_i$$$. When the piranha $$$i$$$ eats some piranha, its size increases by one ($$$a_i$$$ becomes $$$a_i + 1$$$).Your task is to find any dominant piranha in the aquarium or determine if there are no such piranhas.Note that you have to find any (exactly one) dominant piranha, you don't have to find all of them.For example, if $$$a = [5, 3, 4, 4, 5]$$$, then the third piranha can be dominant. Consider the sequence of its moves: The piranha eats the second piranha and $$$a$$$ becomes $$$[5, \\underline{5}, 4, 5]$$$ (the underlined piranha is our candidate). The piranha eats the third piranha and $$$a$$$ becomes $$$[5, \\underline{6}, 5]$$$. The piranha eats the first piranha and $$$a$$$ becomes $$$[\\underline{7}, 5]$$$. The piranha eats the second piranha and $$$a$$$ becomes $$$[\\underline{8}]$$$. You have to answer $$$t$$$ independent test cases.", "c_code": "int solution(void) {\n int t;\n scanf(\"%i\", &t);\n\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%i\", &n);\n\n int size[n];\n int max = 0;\n\n for (int j = 0; j < n; j++) {\n scanf(\"%i\", &size[j]);\n\n if (size[j] > max) {\n max = size[j];\n }\n }\n\n int yes = 0;\n for (int j = 0; j < n; j++) {\n if (size[j] == max) {\n if ((j == 0 && size[j] > size[j + 1]) ||\n (j == n - 1 && size[j] > size[j - 1]) ||\n ((j != 0 && j != n - 1) &&\n (size[j] > size[j - 1] || size[j] > size[j + 1]))) {\n printf(\"%i\\n\", j + 1);\n yes++;\n break;\n }\n }\n }\n\n if (yes == 0) {\n printf(\"-1\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = \"\".split_ascii_whitespace();\n let mut read = || loop {\n if let Some(word) = input.next() {\n break word;\n }\n input = {\n let mut input = \"\".to_owned();\n io::stdin().read_line(&mut input).unwrap();\n if input.is_empty() {\n panic!(\"reached EOF\");\n }\n Box::leak(input.into_boxed_str()).split_ascii_whitespace()\n };\n };\n\n\n let t = read!(usize);\n for _ in 0..t {\n let n = read!(usize);\n let mut a = vec![0; n];\n for i in 0..n {\n a[i] = read!(usize);\n }\n\n let mut r = n + 1;\n let mut m = 0;\n for i in 0..n {\n let c = a[i];\n if c < m {\n continue;\n }\n m = c;\n if i > 0 && a[i - 1] < c {\n r = i + 1;\n } else if i < n - 1 && a[i + 1] < c {\n r = i + 1;\n }\n }\n\n println!(\"{}\", if r > n { -1 } else { r as i64 });\n }\n}", "difficulty": "medium"} {"problem_id": "0199", "problem_description": "Alexey is travelling on a train. Unfortunately, due to the bad weather, the train moves slower that it should!Alexey took the train at the railroad terminal. Let's say that the train starts from the terminal at the moment $$$0$$$. Also, let's say that the train will visit $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$ along its way, and that Alexey destination is the station $$$n$$$.Alexey learned from the train schedule $$$n$$$ integer pairs $$$(a_i, b_i)$$$ where $$$a_i$$$ is the expected time of train's arrival at the $$$i$$$-th station and $$$b_i$$$ is the expected time of departure.Also, using all information he has, Alexey was able to calculate $$$n$$$ integers $$$tm_1, tm_2, \\dots, tm_n$$$ where $$$tm_i$$$ is the extra time the train need to travel from the station $$$i - 1$$$ to the station $$$i$$$. Formally, the train needs exactly $$$a_i - b_{i-1} + tm_i$$$ time to travel from station $$$i - 1$$$ to station $$$i$$$ (if $$$i = 1$$$ then $$$b_0$$$ is the moment the train leave the terminal, and it's equal to $$$0$$$).The train leaves the station $$$i$$$, if both conditions are met: it's on the station for at least $$$\\left\\lceil \\frac{b_i - a_i}{2} \\right\\rceil$$$ units of time (division with ceiling); current time $$$\\ge b_i$$$. Since Alexey spent all his energy on prediction of time delays, help him to calculate the time of arrival at the station $$$n$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int b[n];\n int tm[n];\n int ta = 0;\n int td = 0;\n for (int j = 0; j < n; j++) {\n scanf(\"%d%d\", &a[j], &b[j]);\n }\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &tm[j]);\n }\n for (int j = 0; j < n; j++) {\n if (j == 0) {\n ta = a[0] + tm[0];\n\n } else {\n ta = td + tm[j] + (a[j] - b[j - 1]);\n }\n int x = (b[j] - a[j]);\n if (x % 2 == 1) {\n x = x / 2 + 1;\n } else {\n x = x / 2;\n }\n if (b[j] >= (ta + x)) {\n td = b[j];\n\n } else {\n td = ta + x;\n }\n }\n printf(\"%d\\n\", ta);\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 _ in 0..t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let n = it.next().unwrap();\n\n let mut z = Vec::with_capacity(n);\n\n for _ in 0..n {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let a = it.next().unwrap();\n let b = it.next().unwrap();\n\n z.push((a, b, 0));\n }\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n for (p, x) in z.iter_mut().zip(it) {\n p.2 = x;\n }\n\n let mut sch = 0;\n let mut act = 0;\n let mut arr = 0;\n for (a, b, tm) in z {\n arr = act + a - sch + tm;\n let l = std::cmp::max(b, arr + (b - a).div_ceil(2));\n\n act = l;\n sch = b;\n }\n\n let ans = arr;\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0200", "problem_description": "You are given a permutation $$$a$$$ consisting of $$$n$$$ numbers $$$1$$$, $$$2$$$, ..., $$$n$$$ (a permutation is an array in which each element from $$$1$$$ to $$$n$$$ occurs exactly once).You can perform the following operation: choose some subarray (contiguous subsegment) of $$$a$$$ and rearrange the elements in it in any way you want. But this operation cannot be applied to the whole array.For example, if $$$a = [2, 1, 4, 5, 3]$$$ and we want to apply the operation to the subarray $$$a[2, 4]$$$ (the subarray containing all elements from the $$$2$$$-nd to the $$$4$$$-th), then after the operation, the array can become $$$a = [2, 5, 1, 4, 3]$$$ or, for example, $$$a = [2, 1, 5, 4, 3]$$$.Your task is to calculate the minimum number of operations described above to sort the permutation $$$a$$$ in ascending order.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n\n int arr[n];\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", arr + i);\n }\n\n int r = 0;\n\n for (int i = 0; i < n; i++) {\n if (arr[i] != i + 1) {\n r = -1;\n break;\n }\n }\n\n if (r < 0) {\n if (arr[0] == 1 || arr[n - 1] == n) {\n printf(\"%d\\n\", 1);\n } else if (arr[0] == n && arr[n - 1] == 1) {\n printf(\"%d\\n\", 3);\n } else {\n printf(\"%d\\n\", 2);\n }\n } else {\n printf(\"%d\\n\", 0);\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(|x| x.unwrap());\n\n let t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let n = lines.next().unwrap().parse::().unwrap();\n\n let s = lines.next().unwrap();\n let a = s\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect::>();\n\n let ans = if (0..n - 1).all(|i| a[i] < a[i + 1]) {\n 0\n } else if a[0] == 1 || a[n - 1] == n {\n 1\n } else if a[0] == n && a[n - 1] == 1 {\n 3\n } else {\n 2\n };\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0201", "problem_description": "Recall that a permutation of length $$$n$$$ is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once.For a fixed positive integer $$$d$$$, let's define the cost of the permutation $$$p$$$ of length $$$n$$$ as the number of indices $$$i$$$ $$$(1 \\le i < n)$$$ such that $$$p_i \\cdot d = p_{i + 1}$$$.For example, if $$$d = 3$$$ and $$$p = [5, 2, 6, 7, 1, 3, 4]$$$, then the cost of such a permutation is $$$2$$$, because $$$p_2 \\cdot 3 = p_3$$$ and $$$p_5 \\cdot 3 = p_6$$$.Your task is the following one: for a given value $$$n$$$, find the permutation of length $$$n$$$ and the value $$$d$$$ with maximum possible cost (over all ways to choose the permutation and $$$d$$$). If there are multiple answers, then print any of them.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n = 0;\n scanf(\"%d\", &n);\n printf(\"2\\n\");\n int a[n + 1];\n memset(a, 0, sizeof(a) + 1);\n int res[n + 1];\n int index = 0;\n int next = 1;\n while (1) {\n int j = next;\n int isfirst = 1;\n int ok = 1;\n while (j <= n) {\n if (a[j] == 0) {\n if (isfirst) {\n isfirst = 0;\n next = j;\n }\n a[j] = 1;\n res[index++] = j;\n j = j * 2;\n ok = 0;\n } else {\n j++;\n }\n }\n if (ok) {\n break;\n }\n }\n for (int j = 0; j < n; j++) {\n printf(\"%d \", res[j]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n input! {\n name = reader,\n tests: usize\n }\n for _ in 0..tests {\n input! {\n use reader,\n n: usize\n }\n println!(\"2\");\n let mut a = vec![];\n for mut i in 1..=n {\n if i % 2 == 1 {\n while i <= n {\n a.push(i);\n i *= 2;\n }\n }\n }\n for i in a {\n print!(\"{} \", i);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0202", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi has N blue cards and M red cards.\nA string is written on each card. The string written on the i-th blue card is s_i, and the string written on the i-th red card is t_i.

\n

Takahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn 1 yen (the currency of Japan); each time he finds a red card with that string, he will lose 1 yen.

\n

Here, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces atcoder, he will not earn money even if there are blue cards with atcoderr, atcode, btcoder, and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.)

\n

At most how much can he earn on balance?

\n

Note that the same string may be written on multiple cards.

\n
\n
\n
\n
\n

Constraints

    \n
  • N and M are integers.
  • \n
  • 1 \\leq N, M \\leq 100
  • \n
  • s_1, s_2, ..., s_N, t_1, t_2, ..., t_M are all strings of lengths between 1 and 10 (inclusive) consisting of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\ns_1\ns_2\n:\ns_N\nM\nt_1\nt_2\n:\nt_M\n
\n
\n
\n
\n
\n

Output

If Takahashi can earn at most X yen on balance, print X.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\napple\norange\napple\n1\ngrape\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

He can earn 2 yen by announcing apple.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\napple\norange\napple\n5\napple\napple\napple\napple\napple\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

If he announces apple, he will lose 3 yen. If he announces orange, he can earn 1 yen.

\n
\n
\n
\n
\n
\n

Sample Input 3

1\nvoldemort\n10\nvoldemort\nvoldemort\nvoldemort\nvoldemort\nvoldemort\nvoldemort\nvoldemort\nvoldemort\nvoldemort\nvoldemort\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

If he announces voldemort, he will lose 9 yen. If he announces orange, for example, he can avoid losing a yen.

\n
\n
\n
\n
\n
\n

Sample Input 4

6\nred\nred\nblue\nyellow\nyellow\nred\n5\nred\nred\nyellow\ngreen\nblue\n
\n
\n
\n
\n
\n

Sample Output 4

1\n
\n
\n
", "c_code": "int solution(void) {\n\n static int n;\n static int m;\n static int ans = 0;\n static int tmp = 0;\n static char s[120][15];\n static char t[120][15];\n\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n\n scanf(\"%s\", s[i]);\n }\n\n scanf(\"%d\", &m);\n\n for (int i = 0; i < m; i++) {\n\n scanf(\"%s\", t[i]);\n }\n\n for (int i = 0; i < n; i++) {\n\n tmp = 0;\n\n for (int j = 0; j < n; j++) {\n\n if (strcmp(s[i], s[j]) == 0) {\n\n tmp++;\n }\n }\n\n for (int j = 0; j < m; j++) {\n\n if (strcmp(s[i], t[j]) == 0) {\n\n tmp--;\n }\n }\n\n if (ans < tmp) {\n\n ans = tmp;\n }\n }\n\n printf(\"%d\\n\", ans);\n\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: usize = s.trim().parse().unwrap();\n let mut h: HashMap = HashMap::new();\n for _ in 0..n {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let x = match h.get(&s) {\n Some(x) => *x,\n None => 0,\n };\n h.insert(s, x + 1);\n }\n\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let m: usize = s.trim().parse().unwrap();\n for _ in 0..m {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let x = match h.get(&s) {\n Some(x) => *x,\n None => 0,\n };\n h.insert(s, std::cmp::max(-1, x - 1));\n }\n\n println!(\"{:?}\", std::cmp::max(h.values().max().unwrap(), &0));\n}", "difficulty": "hard"} {"problem_id": "0203", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

On a two-dimensional plane, there are N red points and N blue points.\nThe coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i).

\n

A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.

\n

At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1 \\leq N \\leq 100
  • \n
  • 0 \\leq a_i, b_i, c_i, d_i < 2N
  • \n
  • a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different.
  • \n
  • b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 b_1\na_2 b_2\n:\na_N b_N\nc_1 d_1\nc_2 d_2\n:\nc_N d_N\n
\n
\n
\n
\n
\n

Output

Print the maximum number of friendly pairs.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2 0\n3 1\n1 3\n4 2\n0 4\n5 5\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

For example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5).

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n0 0\n1 1\n5 2\n2 3\n3 4\n4 5\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

For example, you can pair (0, 0) and (2, 3), then (1, 1) and (3, 4).

\n
\n
\n
\n
\n
\n

Sample Input 3

2\n2 2\n3 3\n0 0\n1 1\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

It is possible that no pair can be formed.

\n
\n
\n
\n
\n
\n

Sample Input 4

5\n0 0\n7 3\n2 2\n4 8\n1 6\n8 5\n6 9\n5 4\n9 1\n3 7\n
\n
\n
\n
\n
\n

Sample Output 4

5\n
\n
\n
\n
\n
\n
\n

Sample Input 5

5\n0 0\n1 1\n5 5\n6 6\n7 7\n2 2\n3 3\n4 4\n8 8\n9 9\n
\n
\n
\n
\n
\n

Sample Output 5

4\n
\n
\n
", "c_code": "int solution() {\n int n;\n int ab[100][2];\n int cd[100][2];\n int i;\n int j;\n int ans = 0;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d%d\", &ab[i][0], &ab[i][1]);\n }\n for (i = 0; i < n; i++) {\n scanf(\"%d%d\", &cd[i][0], &cd[i][1]);\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < n; j++) {\n if (ab[i][1] > ab[j][1]) {\n int a = ab[i][0];\n int b = ab[i][1];\n ab[i][0] = ab[j][0];\n ab[i][1] = ab[j][1];\n ab[j][0] = a;\n ab[j][1] = b;\n }\n }\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < n; j++) {\n if (cd[i][0] < cd[j][0]) {\n int a = cd[i][0];\n int b = cd[i][1];\n cd[i][0] = cd[j][0];\n cd[i][1] = cd[j][1];\n cd[j][0] = a;\n cd[j][1] = b;\n }\n }\n }\n int memo[100] = {0};\n\n for (i = 0; i < n; i++) {\n for (j = 0; j < n; j++) {\n if (memo[j] == 0 && cd[i][0] >= ab[j][0] && cd[i][1] >= ab[j][1]) {\n ans++;\n memo[j] = 1;\n break;\n }\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let mut r: Vec<(i16, i16)> = (0..n)\n .map(|_| {\n (\n s.next().unwrap().parse().unwrap(),\n s.next().unwrap().parse().unwrap(),\n )\n })\n .collect();\n let mut b: Vec<(i16, i16)> = (0..n)\n .map(|_| {\n (\n s.next().unwrap().parse().unwrap(),\n s.next().unwrap().parse().unwrap(),\n )\n })\n .collect();\n r.sort();\n b.sort();\n let mut ans = 0;\n for (x, y) in b {\n let mut maxy = -1;\n let mut j = 0;\n for (i, r) in r.iter().take_while(|r| r.0 < x).enumerate() {\n if r.1 < y && r.1 > maxy {\n maxy = r.1;\n j = i;\n }\n }\n if maxy >= 0 {\n r[j].1 = std::i16::MAX;\n ans += 1;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0204", "problem_description": "Timur likes his name. As a spelling of his name, he allows any permutation of the letters of the name. For example, the following strings are valid spellings of his name: Timur, miurT, Trumi, mriTu. Note that the correct spelling must have uppercased T and lowercased other letters.Today he wrote string $$$s$$$ of length $$$n$$$ consisting only of uppercase or lowercase Latin letters. He asks you to check if $$$s$$$ is the correct spelling of his name.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char str[101];\n int n;\n scanf(\"%d\", &n);\n int cnt1 = 0;\n int cnt2 = 0;\n int cnt3 = 0;\n int cnt4 = 0;\n int cnt5 = 0;\n int cnt6 = 0;\n scanf(\"%s\", str);\n for (int i = 0; i < n; i++) {\n if (str[i] == 'T') {\n cnt1++;\n } else if (str[i] == 'i') {\n cnt2++;\n } else if (str[i] == 'm') {\n cnt3++;\n } else if (str[i] == 'u') {\n cnt4++;\n } else if (str[i] == 'r') {\n cnt5++;\n } else {\n cnt6++;\n }\n }\n\n if ((cnt1 + cnt2 + cnt3 + cnt4 + cnt5 + cnt6) == 5 && (cnt1 == 1) &&\n (cnt2 == 1) && (cnt3 == 1) && (cnt4 == 1) && (cnt6 == 0) &&\n (cnt5 == 1)) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut number_of_names = String::new();\n io::stdin()\n .read_line(&mut number_of_names)\n .expect(\"Не получилось прочитать строку\");\n let number_of_names: usize = number_of_names\n .trim()\n .parse()\n .expect(\"Пожалуйста, наберите число!\");\n\n let mut names_vec: Vec = Vec::new();\n let mut names_length_vec: Vec = Vec::new();\n for _i in 0usize..number_of_names {\n let mut length_of_name = String::new();\n io::stdin()\n .read_line(&mut length_of_name)\n .expect(\"Не получилось прочитать строку\");\n let length_of_name: usize = length_of_name\n .trim()\n .parse()\n .expect(\"Пожалуйста, наберите число!\");\n names_length_vec.push(length_of_name);\n\n let mut name = String::new();\n io::stdin()\n .read_line(&mut name)\n .expect(\"Не получилось прочитать строку\");\n names_vec.push(name);\n }\n\n for _i in 0usize..number_of_names {\n let timur_name: String = String::from(\"Timur\");\n if names_length_vec[_i] != 5 {\n println!(\"NO\")\n } else {\n for character in timur_name.chars() {\n names_vec[_i] = names_vec[_i].replacen(character, \"\", 1);\n }\n if names_vec[_i].len() == 2 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0205", "problem_description": "There is a game called \"I Wanna Be the Guy\", consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other?", "c_code": "int solution() {\n int a = 0;\n int d = 0;\n int b = 0;\n int c[300] = {0};\n\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n for (int i = 0; i < b; i++) {\n scanf(\"%d\", &c[i]);\n }\n scanf(\"%d\", &d);\n for (int i = b; i < b + d; i++) {\n scanf(\"%d\", &c[i]);\n }\n int flag = 0;\n for (int i = 1; i <= a; i++) {\n for (int j = 0; j < b + d; j++) {\n if (i == c[j]) {\n flag++;\n break;\n }\n }\n }\n\n if (flag >= a || c[0] == 25) {\n printf(\"I become the guy.\");\n } else {\n printf(\"Oh, my keyboard!\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut b = String::new();\n let _ = stdin().read_line(&mut b);\n let n = b.trim_end().parse::().unwrap();\n\n b.clear();\n let _ = stdin().read_line(&mut b);\n let mut x = b\n .clone()\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n x.remove(0);\n\n b.clear();\n let _ = stdin().read_line(&mut b);\n let mut y = b\n .clone()\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n y.remove(0);\n\n let mut s = HashSet::new();\n for i in 1..n + 1 {\n s.insert(i);\n }\n for i in &x {\n s.remove(i);\n }\n for i in &y {\n s.remove(i);\n }\n if s.is_empty() {\n println!(\"I become the guy.\");\n } else {\n println!(\"Oh, my keyboard!\");\n }\n}", "difficulty": "easy"} {"problem_id": "0206", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

You are given a string s consisting of A, B and C.

\n

Snuke wants to perform the following operation on s as many times as possible:

\n
    \n
  • Choose a contiguous substring of s that reads ABC and replace it with BCA.
  • \n
\n

Find the maximum possible number of operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |s| \\leq 200000
  • \n
  • Each character of s is A, B and C.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Find the maximum possible number of operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

ABCABC\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

You can perform the operations three times as follows: ABCABCBCAABCBCABCABCBCAA. This is the maximum result.

\n
\n
\n
\n
\n
\n

Sample Input 2

C\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

ABCACCBABCBCAABCB\n
\n
\n
\n
\n
\n

Sample Output 3

6\n
\n
\n
", "c_code": "int solution() {\n\n char str[200000];\n scanf(\"%s\", str);\n\n long int times = 0;\n long int add = 0;\n\n for (long int i = 0; i < 200000; i++) {\n\n if (str[i] == 'A') {\n add += 1;\n }\n\n if (str[i] == 'B' && str[i + 1] == 'C') {\n times += add;\n }\n\n if (str[i] == 'B' && str[i + 1] == 'B') {\n add = 0;\n }\n\n if (str[i] == 'C' && str[i + 1] == 'C') {\n add = 0;\n }\n\n if (str[i] == 'A' && str[i + 1] == 'C') {\n add = 0;\n }\n\n if (str[i] == 'B' && str[i + 1] == 'A') {\n add = 0;\n }\n }\n\n printf(\"%ld\", times);\n\n return 0;\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 if s.len() < 3 {\n println!(\"{}\", 0);\n return;\n }\n\n let mut ans = 0_i64;\n let mut i = 0;\n let mut cnt = 0;\n while i < s.len() - 1 {\n if s[i] == b'A' {\n cnt += 1;\n i += 1;\n } else if cnt > 0 && s[i] == b'B' && s[i + 1] == b'C' {\n ans += cnt;\n i += 2;\n } else {\n cnt = 0;\n i += 1;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0207", "problem_description": "Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?", "c_code": "int solution() {\n int nkids = 0;\n int fhight = 0;\n int swidth = 0;\n int hkids[1000] = {0};\n\n scanf(\"%d\", &nkids);\n\n scanf(\"%d\", &fhight);\n\n for (int i = 1; i <= nkids; i++) {\n scanf(\"%d\", &hkids[i]);\n }\n for (int i = 1; i <= nkids; i++) {\n if ((hkids[i] <= fhight) && (hkids[i] >= 1)) {\n swidth += 1;\n } else if ((hkids[i] > fhight) && (hkids[i] <= (2 * fhight))) {\n swidth += 2;\n } else if (hkids[i] > (2 * fhight)) {\n printf(\"he is very high and this is wrong\");\n break;\n }\n }\n printf(\"%d\", swidth);\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\n let h = 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 println!(\n \"{:?}\",\n v.iter()\n .fold(0, |total, &i| if i > h { total + 2 } else { total + 1 })\n );\n}", "difficulty": "hard"} {"problem_id": "0208", "problem_description": "Quick sort", "c_code": "void solution(int arr[], int lo, int hi) {\n while (lo < hi) {\n\n int pivot = hi;\n\n int i = lo;\n int j = hi - 1;\n\n while (1) {\n while (arr[i] < arr[pivot]) {\n i++;\n }\n\n while (j > lo && arr[j] > arr[pivot]) {\n j--;\n }\n\n if (i >= j) {\n break;\n }\n if (arr[i] == arr[j]) {\n i++;\n j--;\n } else {\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n }\n }\n\n int temp = arr[i];\n arr[i] = arr[pivot];\n arr[pivot] = temp;\n\n int pivotIndex = i;\n\n if ((pivotIndex - lo) < (hi - pivotIndex)) {\n if (pivotIndex > lo) {\n quickSort(arr, lo, pivotIndex - 1);\n }\n\n lo = pivotIndex + 1;\n } else {\n quickSort(arr, pivotIndex + 1, hi);\n\n hi = pivotIndex - 1;\n }\n }\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n if arr.len() <= 1 {\n return;\n }\n\n fn sort(arr: &mut [T], mut lo: usize, mut hi: usize) {\n while lo < hi {\n let pivot = hi;\n let mut i = lo;\n let mut j = hi - 1;\n\n loop {\n while arr[i] < arr[pivot] {\n i += 1;\n }\n\n while j > 0 && arr[j] > arr[pivot] {\n j -= 1;\n }\n\n if j == 0 || i >= j {\n break;\n } else if arr[i] == arr[j] {\n i += 1;\n j -= 1;\n } else {\n arr.swap(i, j);\n }\n }\n\n arr.swap(i, pivot);\n let pivot_idx = i;\n\n if pivot_idx - lo < hi - pivot_idx {\n if pivot_idx > 0 {\n sort(arr, lo, pivot_idx - 1);\n }\n lo = pivot_idx + 1;\n } else {\n sort(arr, pivot_idx + 1, hi);\n hi = pivot_idx.saturating_sub(1);\n }\n }\n }\n\n sort(arr, 0, arr.len() - 1);\n}\n", "difficulty": "easy"} {"problem_id": "0209", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

2N players are running a competitive table tennis training on N tables numbered from 1 to N.

\n

The training consists of rounds.\nIn each round, the players form N pairs, one pair per table.\nIn each pair, competitors play a match against each other.\nAs a result, one of them wins and the other one loses.

\n

The winner of the match on table X plays on table X-1 in the next round,\nexcept for the winner of the match on table 1 who stays at table 1.

\n

Similarly, the loser of the match on table X plays on table X+1 in the next round,\nexcept for the loser of the match on table N who stays at table N.

\n

Two friends are playing their first round matches on distinct tables A and B.\nLet's assume that the friends are strong enough to win or lose any match at will.\nWhat is the smallest number of rounds after which the friends can get to play a match against each other?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^{18}
  • \n
  • 1 \\leq A < B \\leq N
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B\n
\n
\n
\n
\n
\n

Output

Print the smallest number of rounds after which the friends can get to play a match against each other.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

If the first friend loses their match and the second friend wins their match, they will both move to table 3 and play each other in the next round.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 2 3\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

If both friends win two matches in a row, they will both move to table 1.

\n
\n
", "c_code": "int solution(void) {\n long long int n = 0;\n long long int a = 0;\n long long int b = 0;\n long long int ans = 0;\n\n scanf(\"%lld %lld %lld\", &n, &a, &b);\n\n if (a > b) {\n ans = a;\n a = b;\n b = ans;\n }\n\n if ((b - a) % 2 == 0) {\n ans = (b - a) / 2;\n } else if ((a + b - 1) <= n) {\n ans = (a + b - 1) / 2;\n } else {\n ans = (n - a + n - b + 1) / 2;\n }\n\n printf(\"%lld\\n\", ans);\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 a: usize = itr.next().unwrap().parse().unwrap();\n let b: usize = itr.next().unwrap().parse().unwrap();\n\n let c = b - a;\n if c.is_multiple_of(2) {\n println!(\"{}\", c / 2);\n } else {\n let d = a + (b - a - 1) / 2;\n let e = (n - b) + 1 + (b - a - 1) / 2;\n println!(\"{}\", min(d, e));\n }\n}", "difficulty": "easy"} {"problem_id": "0210", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Takahashi participated in a contest on AtCoder.

\n

The contest had N problems.

\n

Takahashi made M submissions during the contest.

\n

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

\n

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

\n

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

\n

Find the numbers of Takahashi's correct answers and penalties.

\n
\n
\n
\n
\n

Constraints

    \n
  • N, M, and p_i are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 0 \\leq M \\leq 10^5
  • \n
  • 1 \\leq p_i \\leq N
  • \n
  • S_i is AC or WA.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\np_1 S_1\n:\np_M S_M\n
\n
\n
\n
\n
\n

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 5\n1 WA\n1 AC\n2 WA\n2 AC\n2 WA\n
\n
\n
\n
\n
\n

Sample Output 1

2 2\n
\n

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

\n

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

\n

Thus, he has two correct answers and two penalties.

\n
\n
\n
\n
\n
\n

Sample Input 2

100000 3\n7777 AC\n7777 AC\n7777 AC\n
\n
\n
\n
\n
\n

Sample Output 2

1 0\n
\n

Note that it is pointless to get an AC more than once on the same problem.

\n
\n
\n
\n
\n
\n

Sample Input 3

6 0\n
\n
\n
\n
\n
\n

Sample Output 3

0 0\n
\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n int ac[100001] = {0};\n int wa[100001] = {0};\n scanf(\"%d%d\", &n, &m);\n int p[m];\n char s[m][3];\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &p[i]);\n scanf(\"%s\", s[i]);\n if (s[i][0] == 'A' && ac[p[i]] == 0) {\n ac[p[i]] = 1;\n } else if (s[i][0] == 'W' && ac[p[i]] == 0) {\n wa[p[i]]++;\n }\n }\n int ansa = 0;\n int answ = 0;\n for (int i = 0; i <= n; i++) {\n if (ac[i]) {\n ansa += ac[i];\n answ += wa[i];\n }\n }\n printf(\"%d %d\", ansa, answ);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n\n let mut letter = buffer.split_whitespace();\n let n = letter.next().unwrap().to_string().parse::().unwrap();\n let m = letter.next().unwrap().to_string().parse::().unwrap();\n\n let mut sub = vec![(0, 0); n];\n for _ in 0..m {\n let mut res = String::new();\n io::stdin().read_line(&mut res).unwrap();\n\n let mut res = res.split_whitespace();\n let num = res.next().unwrap().to_string().parse::().unwrap() - 1;\n let ans = res.next().unwrap().to_string();\n\n if &ans == \"AC\" {\n sub[num].0 += 1;\n } else if sub[num].0 == 0 {\n sub[num].1 += 1;\n }\n }\n\n let acc = sub\n .iter()\n .filter(|&&(a, _r)| a > 0)\n .collect::>()\n .len();\n let pen: usize = sub.iter().map(|&(a, r)| if a > 0 { r } else { 0 }).sum();\n\n println!(\"{} {}\", acc, pen);\n}", "difficulty": "medium"} {"problem_id": "0211", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

\n

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

\n

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

\n

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0≤A,B,C,D≤9
  • \n
  • All input values are integers.
  • \n
  • It is guaranteed that there is a solution.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
ABCD\n
\n
\n
\n
\n
\n

Output

Print the formula you made, including the part =7.

\n

Use the signs + and -.

\n

Do not print a space between a digit and a sign.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1222\n
\n
\n
\n
\n
\n

Sample Output 1

1+2+2+2=7\n
\n

This is the only valid solution.

\n
\n
\n
\n
\n
\n

Sample Input 2

0290\n
\n
\n
\n
\n
\n

Sample Output 2

0-2+9+0=7\n
\n

0 - 2 + 9 - 0 = 7 is also a valid solution.

\n
\n
\n
\n
\n
\n

Sample Input 3

3242\n
\n
\n
\n
\n
\n

Sample Output 3

3+2+4-2=7\n
\n
\n
", "c_code": "int solution(void) {\n char X[10];\n\n scanf(\"%s\", X);\n\n X[4] = X[2];\n X[6] = X[3];\n X[2] = X[1];\n X[7] = '=';\n X[8] = '7';\n X[9] = '\\0';\n\n if (X[0] + X[2] + X[4] + X[6] - 4 * '0' == 7) {\n X[1] = '+';\n X[3] = '+';\n X[5] = '+';\n } else if (X[0] + X[2] + X[4] - X[6] - 2 * '0' == 7) {\n X[1] = '+';\n X[3] = '+';\n X[5] = '-';\n } else if (X[0] + X[2] - X[4] - X[6] == 7) {\n X[1] = '+';\n X[3] = '-';\n X[5] = '-';\n } else if (X[0] - X[2] - X[4] - X[6] + 2 * '0' == 7) {\n X[1] = '-';\n X[3] = '-';\n X[5] = '-';\n } else if (X[0] - X[2] - X[4] + X[6] == 7) {\n X[1] = '-';\n X[3] = '-';\n X[5] = '+';\n } else if (X[0] - X[2] + X[4] + X[6] - 2 * '0' == 7) {\n X[1] = '-';\n X[3] = '+';\n X[5] = '+';\n } else if (X[0] + X[2] - X[4] + X[6] - 2 * '0' == 7) {\n X[1] = '+';\n X[3] = '-';\n X[5] = '+';\n } else if (X[0] - X[2] + X[4] - X[6] == 7) {\n X[1] = '-';\n X[3] = '+';\n X[5] = '-';\n }\n\n printf(\"%s\\n\", X);\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut reader = std::io::BufReader::new(stdin.lock());\n let mut s = String::new();\n reader.read_line(&mut s).unwrap();\n let s = s.trim();\n let abcd: Vec = s.chars().map(|x| x.to_string().parse().unwrap()).collect();\n let a = abcd[0];\n let b = abcd[1];\n let c = abcd[2];\n let d = abcd[3];\n\n let abcd = [\n [a, b, c, d],\n [a, -b, c, d],\n [a, b, -c, d],\n [a, b, c, -d],\n [a, -b, -c, d],\n [a, b, -c, -d],\n [a, -b, c, -d],\n [a, -b, -c, -d],\n ];\n let ans = abcd.iter().find(|x| x.iter().sum::() == 7).unwrap();\n println!(\"{}{:+}{:+}{:+}=7\", ans[0], ans[1], ans[2], ans[3]);\n}", "difficulty": "medium"} {"problem_id": "0212", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

\n

Takahashi says: \"there are X animals in total in the garden, and they have Y legs in total.\" Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq X \\leq 100
  • \n
  • 1 \\leq Y \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X Y\n
\n
\n
\n
\n
\n

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 8\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The statement \"there are 3 animals in total in the garden, and they have 8 legs in total\" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 100\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

There is no combination of numbers of cranes and turtles in which this statement is correct.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 2\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n

We also consider the case in which there are only cranes or only turtles.

\n
\n
", "c_code": "int solution(void) {\n\n int x[2];\n\n scanf(\"%d\", &x[0]);\n scanf(\"%d\", &x[1]);\n\n int sum = 0;\n int i = 0;\n int flag = 0;\n\n for (i = 0; i <= x[0]; i++) {\n sum = (x[0] - i) * 2 + i * 4;\n\n if (sum == x[1]) {\n flag = 1;\n break;\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 mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let nums: Vec = buf\n .split_whitespace()\n .filter_map(|x| x.parse::().ok())\n .collect();\n let x = nums[0];\n let y = nums[1];\n if y % 2 == 1 {\n println!(\"No\");\n } else if 2 * x <= y && y <= 4 * x {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0213", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

N people are waiting in a single line in front of the Takahashi Store. The cash on hand of the i-th person from the front of the line is a positive integer A_i.

\n

Mr. Takahashi, the shop owner, has decided on the following scheme: He picks a product, sets a positive integer P indicating its price, and shows this product to customers in order, starting from the front of the line. This step is repeated as described below.

\n

At each step, when a product is shown to a customer, if price P is equal to or less than the cash held by that customer at the time, the customer buys the product and Mr. Takahashi ends the current step. That is, the cash held by the first customer in line with cash equal to or greater than P decreases by P, and the next step begins.

\n

Mr. Takahashi can set the value of positive integer P independently at each step.

\n

He would like to sell as many products as possible. However, if a customer were to end up with 0 cash on hand after a purchase, that person would not have the fare to go home. Customers not being able to go home would be a problem for Mr. Takahashi, so he does not want anyone to end up with 0 cash.

\n

Help out Mr. Takahashi by writing a program that determines the maximum number of products he can sell, when the initial cash in possession of each customer is given.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ | N | ≦ 100000
  • \n
  • 1 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)
  • \n
  • All inputs are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Inputs are provided from Standard Inputs in the following form.

\n
N\nA_1\n:\nA_N\n
\n
\n
\n
\n
\n

Output

Output an integer representing the maximum number of products Mr. Takahashi can sell.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n3\n2\n5\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

As values of P, select in order 1, 4, 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

15\n3\n1\n4\n1\n5\n9\n2\n6\n5\n3\n5\n8\n9\n7\n9\n
\n
\n
\n
\n
\n

Sample Output 2

18\n
\n
\n
", "c_code": "int solution() {\n long ans = 0;\n int N;\n scanf(\"%d\", &N);\n long A[N];\n int i = 0;\n for (i = 0; i < N; i++) {\n scanf(\"%ld\", &A[i]);\n }\n ans += A[0] - 1;\n long r = 2;\n for (i = 1; i < N; i++) {\n if (A[i] != r) {\n ans += (A[i] - 1) / r;\n } else {\n r++;\n }\n }\n printf(\"%ld\\n\", ans);\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 A: 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, _) = A.iter().fold((0, 1), |(res, h), &a| {\n (res + (a - 1) / h, if h == a || h == 1 { h + 1 } else { h })\n });\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0214", "problem_description": "Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \\ldots, a_{n+1}$$$. For each $$$i = 1, 2, \\ldots, n+1$$$ it is guaranteed that $$$0\\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n long long int arr[n][2];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &arr[i][0]);\n scanf(\"%lld\", &arr[i][1]);\n }\n\n long long int ans[n];\n\n for (int i = 0; i < n; i++) {\n ans[i] = (arr[i][1] / (arr[i][0] * arr[i][0]));\n }\n\n for (int i = 0; i < n; i++) {\n printf(\"%lld\\n\", ans[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut count = 0u64;\n let mut t = String::new();\n io::stdin().read_line(&mut t).expect(\"error reading line\");\n let t: u64 = t.trim().parse().expect(\"enter a number\");\n loop {\n count += 1;\n\n let mut line = String::new();\n io::stdin()\n .read_line(&mut line)\n .expect(\"error reading line\");\n let mut iter = line.split_whitespace();\n let n: u64 = iter.next().unwrap().parse().unwrap();\n let s: u64 = iter.next().unwrap().parse().unwrap();\n println!(\"{}\", s / (n * n));\n if count == t {\n break;\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0215", "problem_description": "You are given four integer values $$$a$$$, $$$b$$$, $$$c$$$ and $$$m$$$.Check if there exists a string that contains: $$$a$$$ letters 'A'; $$$b$$$ letters 'B'; $$$c$$$ letters 'C'; no other letters; exactly $$$m$$$ pairs of adjacent equal letters (exactly $$$m$$$ such positions $$$i$$$ that the $$$i$$$-th letter is equal to the $$$(i+1)$$$-th one).", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int A[3];\n int m;\n for (int i = 0; i < 3; i++) {\n scanf(\"%d\", &A[i]);\n }\n scanf(\"%d\", &m);\n for (int i = 0; i < 3; i++) {\n for (int j = i + 1; j < 3; j++) {\n if (A[j] > A[i]) {\n int temp = A[j];\n A[j] = A[i];\n A[i] = temp;\n }\n }\n }\n int max_m = A[0] + A[1] + A[2] - 3;\n int min_m = A[0] - A[1] - A[2] - 1;\n if (m >= min_m && m <= max_m) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n let mut buffer = String::new();\n reader.read_line(&mut buffer).unwrap();\n let mut t: i32 = buffer.trim().parse().unwrap();\n while t > 0 {\n let mut buffer = String::new();\n reader.read_line(&mut buffer).unwrap();\n let [mut a, mut b, mut c, m] = <[i32; 4]>::try_from(\n buffer\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect::>(),\n )\n .unwrap();\n if a > b {\n std::mem::swap(&mut a, &mut b);\n }\n if b > c {\n std::mem::swap(&mut b, &mut c);\n }\n let min_v = std::cmp::max(0, c - (a + b + 1));\n let max_v = (a - 1) + (b - 1) + (c - 1);\n if m >= min_v && m <= max_v {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n t -= 1;\n }\n}", "difficulty": "easy"} {"problem_id": "0216", "problem_description": "

Simple Calculator

\n\n

\nWrite a program which reads two integers a, b and an operator op, and then prints the value of a op b.\n

\n\n

\nThe operator op is '+', '-', '*' or '/' (sum, difference, product or quotient). The division should truncate any fractional part.\n

\n\n\n

Input

\n\n

\nThe input consists of multiple datasets. Each dataset is given in the following format.\n

\n\n
\na op b\n
\n\n

\nThe input ends with a dataset where op = '?'. Your program should not process for this dataset.\n

\n\n

Output

\n\n

\nFor each dataset, print the value in a line.\n

\n\n

Constraints

\n\n
    \n
  • 0 ≤ a, b ≤ 20000
  • \n
  • No divisions by zero are given.
  • \n
\n\n

Sample Input 1

\n
\n1 + 2\n56 - 18\n13 * 2\n100 / 10\n27 + 81\n0 ? 0\n
\n

Sample Output 1

\n
\n3\n38\n26\n10\n108\n
", "c_code": "int solution(void) {\n int a[90];\n int b[90];\n int ans[90];\n int i = 0;\n int Count = 0;\n char op[90];\n\n while (1) {\n scanf(\"%d %c %d\", &a[i], &op[i], &b[i]);\n if (op[i] == '?') {\n break;\n }\n i++;\n Count++;\n }\n\n for (i = 0; i <= Count; i++) {\n if (op[i] == '+') {\n ans[i] = a[i] + b[i];\n } else if (op[i] == '-') {\n ans[i] = a[i] - b[i];\n } else if (op[i] == '*') {\n ans[i] = a[i] * b[i];\n } else if (op[i] == '/') {\n ans[i] = a[i] / b[i];\n }\n }\n for (i = 0; i < Count; i++) {\n printf(\"%d\\n\", ans[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n for line in stdin.lock().lines() {\n let uline = line.unwrap();\n let vec: Vec<&str> = uline.split_whitespace().collect();\n let a: i32 = vec[0].parse().unwrap_or(0);\n let o: &str = vec[1];\n let b: i32 = vec[2].parse().unwrap_or(0);\n if o == \"?\" {\n break;\n } else if o == \"+\" {\n println!(\"{}\", a + b);\n } else if o == \"-\" {\n println!(\"{}\", a - b);\n } else if o == \"*\" {\n println!(\"{}\", a * b);\n } else if o == \"/\" {\n println!(\"{}\", a / b);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0217", "problem_description": "$$$n$$$ heroes fight against each other in the Arena. Initially, the $$$i$$$-th hero has level $$$a_i$$$.Each minute, a fight between two different heroes occurs. These heroes can be chosen arbitrarily (it's even possible that it is the same two heroes that were fighting during the last minute).When two heroes of equal levels fight, nobody wins the fight. When two heroes of different levels fight, the one with the higher level wins, and his level increases by $$$1$$$.The winner of the tournament is the first hero that wins in at least $$$100^{500}$$$ fights (note that it's possible that the tournament lasts forever if no hero wins this number of fights, then there is no winner). A possible winner is a hero such that there exists a sequence of fights that this hero becomes the winner of the tournament.Calculate the number of possible winners among $$$n$$$ heroes.", "c_code": "int solution() {\n int T = 0;\n scanf(\"%d\", &T);\n int a[500];\n\n for (int i = 0; i < T; i++) {\n int n = 0;\n scanf(\"%d\", &n);\n int min = 100;\n int counter = 0;\n for (int j = 0; j < n; j++) {\n int num = 0;\n scanf(\"%d\", &num);\n if (min > num) {\n min = num;\n counter = 1;\n } else if (min == num) {\n counter++;\n }\n }\n a[i] = n - counter;\n }\n\n for (int i = 0; i < T; i++) {\n printf(\"%d\\n\", a[i]);\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 for l in i.lock().lines().skip(2).step_by(2) {\n let mut v = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>();\n v.sort_unstable();\n let r = v.iter().filter(|&n| n > &v[0]).count();\n writeln!(o, \"{}\", r).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "0218", "problem_description": "Suppose you have a special $$$x$$$-$$$y$$$-counter. This counter can store some value as a decimal number; at first, the counter has value $$$0$$$. The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $$$x$$$ or $$$y$$$ to its value. So all sequences this counter generates are starting from $$$0$$$. For example, a $$$4$$$-$$$2$$$-counter can act as follows: it prints $$$0$$$, and adds $$$4$$$ to its value, so the current value is $$$4$$$, and the output is $$$0$$$; it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$8$$$, and the output is $$$04$$$; it prints $$$8$$$, and adds $$$4$$$ to its value, so the current value is $$$12$$$, and the output is $$$048$$$; it prints $$$2$$$, and adds $$$2$$$ to its value, so the current value is $$$14$$$, and the output is $$$0482$$$; it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$18$$$, and the output is $$$04824$$$. This is only one of the possible outputs; for example, the same counter could generate $$$0246802468024$$$ as the output, if we chose to add $$$2$$$ during each step.You wrote down a printed sequence from one of such $$$x$$$-$$$y$$$-counters. But the sequence was corrupted and several elements from the sequence could be erased.Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $$$s$$$ — the remaining data of the sequence. For all $$$0 \\le x, y < 10$$$, calculate the minimum number of digits you have to insert in the string $$$s$$$ to make it a possible output of the $$$x$$$-$$$y$$$-counter. Note that you can't change the order of digits in string $$$s$$$ or erase any of them; only insertions are allowed.", "c_code": "int solution() {\n char s[2000006];\n scanf(\"%s\", s);\n long long int n = strlen(s);\n int i;\n int j;\n int k;\n int l;\n int q;\n long long int ans;\n long long int p[10][10];\n for (i = 0; i < 10; i++) {\n for (j = 0; j < 10; j++) {\n for (k = 0; k < 10; k++) {\n for (l = 0; l < 10; l++) {\n p[k][l] = 1e9 + 10000;\n }\n }\n for (k = 0; k < 10; k++) {\n p[k][(k + i) % 10] = p[k][(k + j) % 10] = 1;\n }\n for (q = 0; q < 200; q++) {\n for (k = 0; k < 10; k++) {\n for (l = 0; l < 10; l++) {\n if (p[k][(l + i) % 10] > p[k][l] + 1) {\n p[k][(l + i) % 10] = p[k][l] + 1;\n }\n if (p[k][(l + j) % 10] > p[k][l] + 1) {\n p[k][(l + j) % 10] = p[k][l] + 1;\n }\n }\n }\n }\n ans = 0;\n for (k = 1; k < n; k++) {\n ans += p[s[k - 1] - '0'][s[k] - '0'];\n }\n if (ans > 1e9) {\n ans = n - 2;\n }\n printf(\"%lld\", ans - n + 1);\n if (j != 9) {\n printf(\" \");\n } else {\n printf(\"\\n\");\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let line: Vec = line.trim().as_bytes().iter().map(|&it| it - b'0').collect();\n\n let mut from_to = [[0; 10]; 10];\n for i in 0..line.len() - 1 {\n let a = line[i] as usize;\n let b = line[i + 1] as usize;\n from_to[a][b] += 1;\n }\n\n for i in 0..10 {\n for j in 0..10 {\n let mut steps = [[2_000_000_000; 10]; 10];\n\n for a in 0..10 {\n for x in 0..10 {\n for y in 0..10 {\n if x + y == 0 {\n continue;\n }\n let b = (a + i * x + j * y) % 10;\n steps[a][b] = steps[a][b].min(x + y - 1);\n }\n }\n }\n\n let mut answer = 0;\n 'l: for a in 0..10 {\n for b in 0..10 {\n if steps[a][b] == 2_000_000_000 && from_to[a][b] != 0 {\n answer = -1;\n break 'l;\n }\n answer += (steps[a][b] * from_to[a][b]) as isize;\n }\n }\n print!(\"{} \", answer);\n }\n println!();\n }\n}", "difficulty": "hard"} {"problem_id": "0219", "problem_description": "An integer array $$$a_1, a_2, \\ldots, a_n$$$ is being transformed into an array of lowercase English letters using the following prodecure:While there is at least one number in the array: Choose any number $$$x$$$ from the array $$$a$$$, and any letter of the English alphabet $$$y$$$. Replace all occurrences of number $$$x$$$ with the letter $$$y$$$. For example, if we initially had an array $$$a = [2, 3, 2, 4, 1]$$$, then we could transform it the following way: Choose the number $$$2$$$ and the letter c. After that $$$a = [c, 3, c, 4, 1]$$$. Choose the number $$$3$$$ and the letter a. After that $$$a = [c, a, c, 4, 1]$$$. Choose the number $$$4$$$ and the letter t. After that $$$a = [c, a, c, t, 1]$$$. Choose the number $$$1$$$ and the letter a. After that $$$a = [c, a, c, t, a]$$$. After the transformation all letters are united into a string, in our example we get the string \"cacta\".Having the array $$$a$$$ and the string $$$s$$$ determine if the string $$$s$$$ could be got from the array $$$a$$$ after the described transformation?", "c_code": "int solution(void) {\n\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n }\n char h[n + 1];\n scanf(\"%s\", h);\n int f = 1;\n\n for (int m = 0; m < n; m++) {\n\n for (int l = 0; l < n + 1; l++) {\n if (a[m] == a[l] && h[m] != h[l]) {\n\n f = 0;\n break;\n }\n f = 1;\n }\n if (f == 0) {\n break;\n }\n }\n if (f == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).expect(\"Error\");\n let t: i32 = t.trim().parse().unwrap();\n\n let mut solutions = Vec::new();\n 'outer: for _ in 0..t {\n let mut n = String::new();\n io::stdin().read_line(&mut n).expect(\"Erorr\");\n let _: i32 = n.trim().parse().unwrap();\n\n let mut a = String::new();\n io::stdin().read_line(&mut a).expect(\"Error\");\n let a: Vec = a\n .trim()\n .split(' ')\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"Error\");\n\n let mut pairs: Vec<(i32, char)> = Vec::new();\n 'inner: for (i, c) in s.trim().chars().enumerate() {\n for (vi, vc) in &pairs {\n if *vi == a[i] {\n if c == *vc {\n continue 'inner;\n } else {\n solutions.push(false);\n continue 'outer;\n }\n }\n }\n pairs.push((a[i], c));\n }\n solutions.push(true);\n }\n\n for sol in solutions {\n if sol {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0220", "problem_description": "You are given a positive integer $$$n$$$.The weight of a permutation $$$p_1, p_2, \\ldots, p_n$$$ is the number of indices $$$1\\le i\\le n$$$ such that $$$i$$$ divides $$$p_i$$$. Find a permutation $$$p_1,p_2,\\dots, p_n$$$ with the minimum possible weight (among all permutations of length $$$n$$$).A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n int x = 0;\n scanf(\"%d\", &x);\n if (x % 2 == 0) {\n for (int k = 0; k < x; k++) {\n if (k % 2 == 0) {\n printf(\"%d\\n\", k + 2);\n } else {\n printf(\"%d\\n\", k);\n }\n }\n } else {\n for (int k = 0; k < (x - 2); k++) {\n if (k % 2 == 0) {\n printf(\"%d\\n\", k + 2);\n } else {\n printf(\"%d\\n\", k);\n }\n }\n if (x > 1) {\n printf(\"%d\\n%d\\n\", x, x - 2);\n } else {\n printf(\"%d\\n\", 1);\n }\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() -> Result<(), std::io::Error> {\n let stdin = std::io::stdin();\n let mut lines = stdin.lock().lines();\n let t = lines.next().unwrap()?.parse::().unwrap();\n for _ in 0..t {\n let n = lines.next().unwrap()?.parse::().unwrap();\n let res: Vec<_> = if n % 2 == 0 {\n (1..=n)\n .map(|i| if i % 2 == 0 { i - 1 } else { i + 1 })\n .map(|i| i.to_string())\n .collect()\n } else {\n [\"1\".to_owned()]\n .into_iter()\n .chain(\n (2..=n)\n .map(|i| if i % 2 == 0 { i + 1 } else { i - 1 })\n .map(|i| i.to_string()),\n )\n .collect()\n };\n println!(\"{}\", res.join(\" \"));\n }\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0221", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

\n

The variable x_i was initially assigned a value of i.

\n

Snuke chose one of these variables and assigned it 0.

\n

You are given the values of the five variables after this assignment.

\n

Find out which variable Snuke assigned 0.

\n
\n
\n
\n
\n

Constraints

    \n
  • The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
x_1 x_2 x_3 x_4 x_5\n
\n
\n
\n
\n
\n

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

0 2 3 4 5\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

In this case, Snuke assigned 0 to x_1, so we should print 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 2 0 4 5\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
", "c_code": "int solution(void) {\n\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int e = 0;\n int ans = 0;\n int er = 0;\n\n scanf(\"%d%d%d%d%d\", &a, &b, &c, &d, &e);\n\n if (a == 0 && b == 2 && c == 3 && d == 4 && e == 5) {\n ans = 1;\n er = 1;\n }\n if (a == 1 && b == 0 && c == 3 && d == 4 && e == 5) {\n ans = 2;\n er = 1;\n }\n if (a == 1 && b == 2 && c == 0 && d == 4 && e == 5) {\n ans = 3;\n er = 1;\n }\n if (a == 1 && b == 2 && c == 3 && d == 0 && e == 5) {\n ans = 4;\n er = 1;\n }\n if (a == 1 && b == 2 && c == 3 && d == 4 && e == 0) {\n ans = 5;\n er = 1;\n }\n\n if (ans > 0) {\n printf(\"%d\", ans);\n }\n if (er == 0) {\n printf(\"error\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let item = buf.split_whitespace().filter_map(|x| x.parse::().ok());\n for (ix, v) in item.enumerate() {\n if v == 0 {\n println!(\"{}\", ix + 1);\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0222", "problem_description": "Mihai plans to watch a movie. He only likes palindromic movies, so he wants to skip some (possibly zero) scenes to make the remaining parts of the movie palindromic.You are given a list $$$s$$$ of $$$n$$$ non-empty strings of length at most $$$3$$$, representing the scenes of Mihai's movie.A subsequence of $$$s$$$ is called awesome if it is non-empty and the concatenation of the strings in the subsequence, in order, is a palindrome.Can you help Mihai check if there is at least one awesome subsequence of $$$s$$$?A palindrome is a string that reads the same backward as forward, for example strings \"z\", \"aaa\", \"aba\", \"abccba\" are palindromes, but strings \"codeforces\", \"reality\", \"ab\" are not.A sequence $$$a$$$ is a non-empty subsequence of a non-empty sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero, but not all) elements.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n while (n--) {\n int m;\n scanf(\"%d\", &m);\n int a[m + 5][7];\n int flag = 0;\n int same = 1;\n for (int u = 0; u < m; u++) {\n char str[5];\n scanf(\"%s\", str);\n int l = strlen(str);\n a[u][4] = l;\n\n for (int j = 0; j < l; j++) {\n a[u][j] = str[j] - 'a' + 1;\n }\n\n a[u][l] = -1;\n if (l == 1) {\n flag = 1;\n }\n if (l == 2 && a[u][0] == a[u][1]) {\n flag = 1;\n }\n if (l == 3 && a[u][0] == a[u][2]) {\n flag = 1;\n }\n\n if (flag == 0) {\n int v;\n int q;\n for (v = 0, q = l - 1; v < q; v++, q--) {\n if (a[u][v] != a[u][q]) {\n break;\n }\n }\n if (v >= q) {\n flag = 1;\n }\n }\n }\n\n if (flag == 0) {\n for (int i = 0; i < m - 1; i++) {\n if (flag == 1) {\n break;\n }\n int y1 = a[i][4] - 1;\n if (i >= 1 && a[i][0] == a[i - 1][0] && a[i][4] == a[i - 1][4] &&\n a[i][y1] == a[i - 1][y1]) {\n continue;\n }\n for (int j = i + 1; j < m; j++) {\n int lt = a[j][4] - 1;\n if (a[i][0] == a[j][lt]) {\n int s1 = 0;\n int s2 = lt;\n while (a[i][s1] == a[j][s2] && a[i][s1] != -1 && s2 >= 0) {\n s1++;\n s2--;\n }\n if (a[i][s1] == -1 || s2 < 0) {\n flag = 1;\n break;\n }\n }\n }\n if (flag == 1) {\n break;\n }\n }\n }\n if (flag == 0) {\n printf(\"NO\\n\");\n }\n if (flag == 1) {\n printf(\"YES\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let stdout = stdout();\n\n let stdin = stdin.lock();\n let stdout = stdout.lock();\n let mut stdout = BufWriter::new(stdout);\n\n let stdin = &mut stdin.lines().map(Result::unwrap);\n\n let nn = stdin.next().unwrap().parse().unwrap();\n let chr = |c| ((c - b'a') & 31) as usize;\n\n for _ii in 0..nn {\n let mut pending3 = [[[0u8; 32]; 32]; 32];\n let mut pending2 = [[0u8; 32]; 32];\n\n let n: usize = stdin.next().unwrap().parse().unwrap();\n let mut data = stdin.take(n).map(String::into_bytes);\n let res = data.any(|s| match s[..] {\n [_] => true,\n [a, b] | [a, _, b] if a == b => true,\n [a, b] => {\n let (a, b) = (chr(a), chr(b));\n pending2[b][a] = 3;\n pending2[a][b] != 0\n }\n [a, b, c] => {\n let (a, b, c) = (chr(a), chr(b), chr(c));\n pending3[c][b][a] = 1;\n pending2[b][a] |= 2;\n ((pending2[b][c] | pending3[a][b][c]) & 1) != 0\n }\n _ => false,\n });\n\n data.for_each(drop);\n writeln!(stdout, \"{}\", if res { \"YES\" } else { \"NO\" }).unwrap();\n }\n\n stdout.flush().unwrap();\n}", "difficulty": "medium"} {"problem_id": "0223", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a string S consisting of lowercase English letters.\nFind the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.\nIf every lowercase English letter occurs in S, print None instead.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 10^5 (|S| is the length of string S.)
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the lexicographically smallest lowercase English letter that does not occur in S.\nIf every lowercase English letter occurs in S, print None instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

atcoderregularcontest\n
\n
\n
\n
\n
\n

Sample Output 1

b\n
\n

The string atcoderregularcontest contains a, but does not contain b.

\n
\n
\n
\n
\n
\n

Sample Input 2

abcdefghijklmnopqrstuvwxyz\n
\n
\n
\n
\n
\n

Sample Output 2

None\n
\n

This string contains every lowercase English letter.

\n
\n
\n
\n
\n
\n

Sample Input 3

fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg\n
\n
\n
\n
\n
\n

Sample Output 3

d\n
\n
\n
", "c_code": "int solution() {\n\n char s[100000];\n scanf(\"%s\", s);\n\n unsigned int allbit = 0;\n unsigned int bit = 0;\n for (unsigned int i = 0; s[i] != '\\0'; i++) {\n bit = 1;\n bit = bit << (s[i] - 0x61);\n allbit = allbit | bit;\n }\n\n unsigned int expected_all = 0;\n for (unsigned int i = 0; i < 26; i++) {\n bit = 1;\n bit = bit << i;\n expected_all = expected_all | bit;\n }\n\n if (allbit == expected_all) {\n printf(\"None\\n\");\n return 0;\n }\n\n for (int i = 0; i < 26; i++) {\n if ((~allbit >> i) & 0x1) {\n printf(\"%c\\n\", i + 0x61);\n break;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let s = buf.trim().chars().collect::>();\n let mut set = HashSet::new();\n for i in s {\n set.insert(i);\n }\n let mut a = b'a';\n let mut flag = true;\n while a <= b'z' {\n if set.insert(a as char) {\n break;\n }\n if a == b'z' {\n flag = false;\n }\n a += 1;\n }\n if flag {\n println!(\"{}\", (a as char));\n } else {\n println!(\"None\");\n }\n}", "difficulty": "medium"} {"problem_id": "0224", "problem_description": "Anna is a girl so brave that she is loved by everyone in the city and citizens love her cookies. She is planning to hold a party with cookies. Now she has $$$a$$$ vanilla cookies and $$$b$$$ chocolate cookies for the party.She invited $$$n$$$ guests of the first type and $$$m$$$ guests of the second type to the party. They will come to the party in some order. After coming to the party, each guest will choose the type of cookie (vanilla or chocolate) to eat. There is a difference in the way how they choose that type:If there are $$$v$$$ vanilla cookies and $$$c$$$ chocolate cookies at the moment, when the guest comes, then if the guest of the first type: if $$$v>c$$$ the guest selects a vanilla cookie. Otherwise, the guest selects a chocolate cookie. if the guest of the second type: if $$$v>c$$$ the guest selects a chocolate cookie. Otherwise, the guest selects a vanilla cookie. After that: If there is at least one cookie of the selected type, the guest eats one. Otherwise (there are no cookies of the selected type), the guest gets angry and returns to home. Anna wants to know if there exists some order of guests, such that no one guest gets angry. Your task is to answer her question.", "c_code": "int solution() {\n unsigned long long int a;\n unsigned long long int b;\n unsigned long long int n;\n unsigned long long int m;\n int t;\n\n scanf(\"%d\", &t);\n\n while (t) {\n scanf(\"%llu%llu%llu%llu\", &a, &b, &n, &m);\n if ((a + b) < (n + m) || (a <= b && a < m) || (a >= b && b < m)) {\n printf(\"No\\n\");\n } else {\n printf(\"Yes\\n\");\n }\n t--;\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (a, b, n, m) = (xs[0], xs[1], xs[2], xs[3]);\n let c = min(a, b);\n let ok = m <= c && m + n <= a + b;\n println!(\"{}\", if ok { \"Yes\" } else { \"No\" });\n }\n}", "difficulty": "easy"} {"problem_id": "0225", "problem_description": "For a collection of integers $$$S$$$, define $$$\\operatorname{mex}(S)$$$ as the smallest non-negative integer that does not appear in $$$S$$$.NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.The universe can be represented as a 1-indexed array $$$a$$$ of length $$$n$$$. When NIT snaps his fingers, he does the following operation on the array: He selects positive integers $$$l$$$ and $$$r$$$ such that $$$1\\le l\\le r\\le n$$$. Let $$$w=\\operatorname{mex}(\\{a_l,a_{l+1},\\dots,a_r\\})$$$. Then, for all $$$l\\le i\\le r$$$, set $$$a_i$$$ to $$$w$$$. We say the universe is destroyed if and only if for all $$$1\\le i\\le n$$$, $$$a_i=0$$$ holds.Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to $$$0$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n int x = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] > 0 && x == 0) {\n x = 1;\n } else if (arr[i] == 0 && x == 1) {\n x = 2;\n } else if (arr[i] > 0 && x == 2) {\n x = 3;\n }\n }\n if (x == 0) {\n printf(\"0\\n\");\n } else if (x == 3) {\n printf(\"2\\n\");\n } else {\n printf(\"1\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n name = reader,\n tests: usize\n }\n for _ in 0..tests {\n input! {\n use reader,\n n: usize,\n a: [u32; n]\n }\n let mut a = VecDeque::from(a);\n while let Some(0) = a.front() {\n a.pop_front();\n }\n while let Some(0) = a.back() {\n a.pop_back();\n }\n let found0 = a.iter().position(|&x| x == 0).is_some();\n let ans = if a.is_empty() {\n 0\n } else if found0 {\n 2\n } else {\n 1\n };\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0226", "problem_description": "You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \\dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \\ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \\le B$$$ for all $$$i$$$ and $$$\\sum\\limits_{i=0}^{n}{a_i}$$$ is maximum possible.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n\n for (int i = 0; i < t; i++) {\n long long int sum = 0;\n long int n = 0;\n long long int B = 0;\n long long int x = 0;\n long long int y = 0;\n scanf(\"%ld%lld%lld%lld\", &n, &B, &x, &y);\n long long int a[n + 1];\n a[0] = 0;\n for (long int j = 1; j <= n; j++) {\n if ((a[j - 1] + x) <= B) {\n a[j] = a[j - 1] + x;\n } else {\n a[j] = a[j - 1] - y;\n }\n }\n for (long int j = 0; j <= n; j++) {\n sum += a[j];\n }\n printf(\"%lld\\n\", sum);\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n let stdout = io::stdout();\n let stdout_handle = stdout.lock();\n let mut out_buffer = BufWriter::with_capacity(65536, stdout_handle);\n\n let mut line = String::new();\n stdin.read_line(&mut line).unwrap();\n let t: usize = line.trim().parse().unwrap();\n\n for _t in 0..t {\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let mut values = line.split_ascii_whitespace();\n let length: u64 = values.next().unwrap().parse().unwrap();\n let max_value: i64 = values.next().unwrap().parse().unwrap();\n let add: i64 = values.next().unwrap().parse().unwrap();\n let sub: i64 = values.next().unwrap().parse().unwrap();\n\n let mut max_sum: i64 = 0;\n let mut current_value: i64 = 0;\n for _i in 0..length {\n if current_value + add <= max_value {\n current_value += add;\n } else {\n current_value -= sub;\n }\n max_sum += current_value;\n }\n\n writeln!(&mut out_buffer, \"{}\", max_sum).unwrap();\n }\n out_buffer.flush().unwrap();\n}", "difficulty": "hard"} {"problem_id": "0227", "problem_description": "You are given an array of $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$. After you watched the amazing film \"Everything Everywhere All At Once\", you came up with the following operation.In one operation, you choose $$$n-1$$$ elements of the array and replace each of them with their arithmetic mean (which doesn't have to be an integer). For example, from the array $$$[1, 2, 3, 1]$$$ we can get the array $$$[2, 2, 2, 1]$$$, if we choose the first three elements, or we can get the array $$$[\\frac{4}{3}, \\frac{4}{3}, 3, \\frac{4}{3}]$$$, if we choose all elements except the third.Is it possible to make all elements of the array equal by performing a finite number of such operations?", "c_code": "int solution() {\n\n int t = 0;\n scanf(\"%d\", &t);\n\n float arr[50];\n int n = 0;\n\n int sum = 0;\n int checker = 0;\n int results[200];\n\n for (int i = 0; i < t; i++) {\n\n sum = 0;\n checker = 0;\n\n n = 0;\n scanf(\"%d\", &n);\n\n for (int j = 0; j < n; j++) {\n scanf(\"%f\", &arr[j]);\n }\n\n for (int j = 0; j < n; j++) {\n sum += arr[j];\n }\n\n for (int j = 0; j < n; j++) {\n\n if ((sum - arr[j]) / (n - 1) == arr[j]) {\n checker = 1;\n }\n }\n\n if (checker == 1) {\n results[i] = 1;\n } else {\n results[i] = 0;\n }\n }\n\n for (int m = 0; m < t; m++) {\n\n if (results[m] == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n\n return (0);\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin().read_to_string(&mut content);\n\n let mut lines = content.lines();\n let num_tests: i32 = lines.next().unwrap().parse().unwrap();\n for _ in 0..num_tests {\n let n: i32 = lines.next().unwrap().parse().unwrap();\n let a: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|s| s.parse().unwrap())\n .collect();\n let sum: i32 = a.iter().sum();\n if sum % n != 0 || !a.contains(&(sum / n)) {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0228", "problem_description": "Two players play a game.Initially there are $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, then players alternate turns.The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it.You want to know what number will be left on the board after $$$n - 1$$$ turns if both players make optimal moves.", "c_code": "int solution() {\n\n int cases;\n scanf(\"%d\", &cases);\n int arr[cases];\n for (int i = 0; i < cases; i++) {\n scanf(\"%d\", &arr[i]);\n }\n for (int j = 0; j < cases; j++) {\n for (int k = 1; k < cases - j; k++) {\n if (arr[k] > arr[k - 1]) {\n int temp = arr[k];\n arr[k] = arr[k - 1];\n arr[k - 1] = temp;\n }\n }\n }\n if (cases % 2 == 0) {\n printf(\"%d\\n\", arr[cases / 2]);\n } else {\n printf(\"%d\\n\", arr[(cases - 1) / 2]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"input : read line failed\");\n\n let amount = input\n .trim()\n .parse::()\n .expect(\"amount: parse to usize failed\");\n input.clear();\n\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"input : read line failed\");\n\n let mut vec = input\n .split_whitespace()\n .map(|item| item.parse::().expect(\"item: parse i32 failed\"))\n .collect::>();\n\n vec.sort();\n let index = if amount % 2 == 0 {\n amount / 2 - 1\n } else {\n amount / 2\n };\n let outcome = vec[index];\n\n println!(\"{}\", &outcome);\n}", "difficulty": "medium"} {"problem_id": "0229", "problem_description": "\n

Score: 400 points

\n
\n
\n

Problem Statement

\n

AtCoder Inc. has decided to lock the door of its office with a 3-digit PIN code.

\n

The company has an N-digit lucky number, S. Takahashi, the president, will erase N-3 digits from S and concatenate the remaining 3 digits without changing the order to set the PIN code.

\n

How many different PIN codes can he set this way?

\n

Both the lucky number and the PIN code may begin with a 0.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 4 \\leq N \\leq 30000
  • \n
  • S is a string of length N consisting of digits.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

\n

Print the number of different PIN codes Takahashi can set.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n0224\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

Takahashi has the following options:

\n
    \n
  • Erase the first digit of S and set 224.
  • \n
  • Erase the second digit of S and set 024.
  • \n
  • Erase the third digit of S and set 024.
  • \n
  • Erase the fourth digit of S and set 022.
  • \n
\n

Thus, he can set three different PIN codes: 022, 024, and 224.

\n
\n
\n
\n
\n
\n

Sample Input 2

6\n123123\n
\n
\n
\n
\n
\n

Sample Output 2

17\n
\n
\n
\n
\n
\n
\n

Sample Input 3

19\n3141592653589793238\n
\n
\n
\n
\n
\n

Sample Output 3

329\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n char s[n + 1];\n scanf(\"%s\", s);\n int ans = 0;\n for (int i = 0; i < 1000; i++) {\n int first = i / 100;\n int sec = (i - i / 100 * 100) / 10;\n int ff = 0;\n int fs = 0;\n int tert = i % 10;\n int ft = 0;\n int mark = 0;\n while (ft == 0 && mark < n) {\n if (ff == 0 && s[mark] - '0' == first) {\n ff = 1;\n } else if (ff == 1 && fs == 0 && s[mark] - '0' == sec) {\n fs = 1;\n } else if (ff == 1 && fs == 1 && ft == 0 && s[mark] - '0' == tert) {\n ft = 1;\n }\n mark++;\n }\n if (ft == 1) {\n ans++;\n }\n }\n printf(\"%d\", ans);\n\n return 0;\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 _n = it.next().unwrap().parse::().unwrap();\n let s = it.next().unwrap().as_bytes();\n let mut res = 0;\n for i in 0..1000 {\n let mut m: u32 = i;\n let mut cnt = 0;\n for &c in s {\n if c == b'0' + (m % 10) as u8 {\n cnt += 1;\n m /= 10;\n }\n }\n if cnt >= 3 {\n res += 1;\n }\n }\n println!(\"{}\", res);\n}", "difficulty": "medium"} {"problem_id": "0230", "problem_description": "The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, $$$n$$$ is always even, and in C2, $$$n$$$ is always odd.You are given a regular polygon with $$$2 \\cdot n$$$ vertices (it's convex and has equal sides and equal angles) and all its sides have length $$$1$$$. Let's name it as $$$2n$$$-gon.Your task is to find the square of the minimum size such that you can embed $$$2n$$$-gon in the square. Embedding $$$2n$$$-gon in the square means that you need to place $$$2n$$$-gon in the square in such way that each point which lies inside or on a border of $$$2n$$$-gon should also lie inside or on a border of the square.You can rotate $$$2n$$$-gon and/or the square.", "c_code": "int solution() {\n int n;\n int t;\n double x;\n\n scanf(\"%d\", &t);\n\n while (t--) {\n scanf(\"%d\", &n);\n\n n = n * 2;\n x = 360 / (1.0 * n);\n x = (180 - x);\n x = x / 2;\n\n x = (x * acos(-1)) / 180;\n double y = tan(x);\n\n printf(\"%.9f\\n\", y);\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 x: f64 = lines.next().unwrap().unwrap().parse().unwrap();\n let ans = (std::f64::consts::FRAC_PI_2 * (1.0 - 1.0 / x)).tan();\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0231", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

We have N voting papers. The i-th vote (1 \\leq i \\leq N) has the string S_i written on it.

\n

Print all strings that are written on the most number of votes, in lexicographical order.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • S_i (1 \\leq i \\leq N) are strings consisting of lowercase English letters.
  • \n
  • The length of S_i (1 \\leq i \\leq N) is between 1 and 10 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nS_1\n:\nS_N\n
\n
\n
\n
\n
\n

Output

\n

Print all strings in question in lexicographical order.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7\nbeat\nvet\nbeet\nbed\nvet\nbet\nbeet\n
\n
\n
\n
\n
\n

Sample Output 1

beet\nvet\n
\n

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

\n
\n
\n
\n
\n
\n

Sample Input 2

8\nbuffalo\nbuffalo\nbuffalo\nbuffalo\nbuffalo\nbuffalo\nbuffalo\nbuffalo\n
\n
\n
\n
\n
\n

Sample Output 2

buffalo\n
\n
\n
\n
\n
\n
\n

Sample Input 3

7\nbass\nbass\nkick\nkick\nbass\nkick\nkick\n
\n
\n
\n
\n
\n

Sample Output 3

kick\n
\n
\n
\n
\n
\n
\n

Sample Input 4

4\nushi\ntapu\nnichia\nkun\n
\n
\n
\n
\n
\n

Sample Output 4

kun\nnichia\ntapu\nushi\n
\n
\n
", "c_code": "int solution() {\n int n;\n int i;\n int j;\n int k;\n int l;\n int o;\n int p;\n int b;\n int r;\n int c[200005][5];\n int nn = 0;\n int f;\n int m = 1;\n int d[200005][2];\n long h[200005];\n char s[200005][12];\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%s\", &s[nn]);\n h[nn] = 0;\n for (j = 0; j < 10; j++) {\n if (s[nn][j] == 0) {\n break;\n }\n h[nn] |= ((long)s[nn][j] - 'a' + 1) << (9 - j) * 5;\n }\n if (nn == 0) {\n c[0][0] = -1;\n c[0][1] = -1;\n c[0][2] = 1;\n c[0][3] = 0;\n c[0][4] = -1;\n r = 0;\n nn = 1;\n } else {\n j = r;\n f = 0;\n while (j != -1) {\n k = j;\n if (h[j] == h[nn]) {\n c[j][2]++;\n if (m < c[j][2]) {\n m = c[j][2];\n }\n f = 1;\n break;\n }\n j = c[j][h[j] < h[nn]];\n }\n if (f == 0) {\n c[nn][0] = -1;\n c[nn][1] = -1;\n c[nn][2] = 1;\n c[nn][3] = 1;\n c[nn][4] = k;\n c[k][h[k] < h[nn]] = nn;\n j = nn;\n while (c[j][3]) {\n k = c[j][4];\n if (c[k][3]) {\n l = c[k][4];\n p = c[l][c[l][0] == k];\n if (p != -1) {\n if (c[p][3]) {\n c[k][3] = 0;\n c[p][3] = 0;\n c[l][3] = 1;\n c[r][3] = 0;\n j = l;\n continue;\n }\n }\n if ((c[k][0] == j) ^ (c[l][0] == k)) {\n c[l][c[l][0] != k] = j;\n c[j][4] = l;\n o = (c[k][0] == j);\n c[k][o ^ 1] = c[j][o];\n c[c[j][o]][4] = k;\n c[j][o] = k;\n c[k][4] = j;\n o = j;\n j = k;\n k = o;\n }\n o = c[l][4];\n if (o != -1) {\n c[o][c[o][0] != l] = k;\n } else {\n r = k;\n }\n c[k][4] = o;\n o = (c[l][0] == k);\n c[l][o ^ 1] = c[k][o];\n c[c[k][o]][4] = l;\n c[k][o] = l;\n c[l][4] = k;\n c[k][3] = 0;\n c[l][3] = 1;\n }\n break;\n }\n c[r][3] = 0;\n nn++;\n }\n }\n }\n i = 0;\n d[0][0] = r;\n d[0][1] = 0;\n while (i != -1) {\n f = 0;\n switch (d[i][1]) {\n case 0:\n d[i + 1][0] = c[d[i][0]][0];\n if (d[i + 1][0] != -1) {\n f = 1;\n }\n break;\n case 1:\n if (c[d[i][0]][2] == m) {\n printf(\"%s\\n\", &s[d[i][0]]);\n }\n break;\n case 2:\n d[i + 1][0] = c[d[i][0]][1];\n if (d[i + 1][0] != -1) {\n f = 1;\n }\n break;\n case 3:\n i--;\n break;\n }\n if (f) {\n i++;\n d[i][1] = 0;\n } else if (i >= 0) {\n d[i][1]++;\n }\n }\n return (0);\n}", "rust_code": "fn solution() {\n let mut vote_count = String::new();\n io::stdin().read_line(&mut vote_count).unwrap();\n vote_count.pop();\n let vote_count = u32::from_str(&vote_count).unwrap();\n\n let mut votes = HashMap::new();\n for _ in 0..vote_count {\n let mut vote = String::new();\n io::stdin().read_line(&mut vote).unwrap();\n vote.pop();\n let value = if let Some(cnt) = votes.get(&vote) {\n cnt + 1\n } else {\n 1\n };\n votes.insert(vote, value);\n }\n\n let mut max_value = 0;\n let mut max_votes: Vec = Vec::new();\n for (key, value) in votes.iter() {\n if *value > max_value {\n max_value = *value;\n max_votes.clear();\n }\n if *value == max_value {\n max_votes.push(key.to_string());\n }\n }\n\n max_votes.sort();\n for vote in max_votes.iter() {\n println!(\"{}\", vote);\n }\n}", "difficulty": "hard"} {"problem_id": "0232", "problem_description": "Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on $$$n - 2$$$ intermediate planets. Formally: we number all the planets from $$$1$$$ to $$$n$$$. $$$1$$$ is Earth, $$$n$$$ is Mars. Natasha will make exactly $$$n$$$ flights: $$$1 \\to 2 \\to \\ldots n \\to 1$$$.Flight from $$$x$$$ to $$$y$$$ consists of two phases: take-off from planet $$$x$$$ and landing to planet $$$y$$$. This way, the overall itinerary of the trip will be: the $$$1$$$-st planet $$$\\to$$$ take-off from the $$$1$$$-st planet $$$\\to$$$ landing to the $$$2$$$-nd planet $$$\\to$$$ $$$2$$$-nd planet $$$\\to$$$ take-off from the $$$2$$$-nd planet $$$\\to$$$ $$$\\ldots$$$ $$$\\to$$$ landing to the $$$n$$$-th planet $$$\\to$$$ the $$$n$$$-th planet $$$\\to$$$ take-off from the $$$n$$$-th planet $$$\\to$$$ landing to the $$$1$$$-st planet $$$\\to$$$ the $$$1$$$-st planet.The mass of the rocket together with all the useful cargo (but without fuel) is $$$m$$$ tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that $$$1$$$ ton of fuel can lift off $$$a_i$$$ tons of rocket from the $$$i$$$-th planet or to land $$$b_i$$$ tons of rocket onto the $$$i$$$-th planet. For example, if the weight of rocket is $$$9$$$ tons, weight of fuel is $$$3$$$ tons and take-off coefficient is $$$8$$$ ($$$a_i = 8$$$), then $$$1.5$$$ tons of fuel will be burnt (since $$$1.5 \\cdot 8 = 9 + 3$$$). The new weight of fuel after take-off will be $$$1.5$$$ tons. Please note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well.Help Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn't need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly.", "c_code": "int solution(void) {\n\n long int n;\n long int m;\n scanf(\"%ld %ld\", &n, &m);\n long int a[n];\n long int b[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n if (a[i] == 1) {\n printf(\"-1\");\n return 0;\n }\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%ld\", &b[i]);\n if (b[i] == 1) {\n printf(\"-1\");\n return 0;\n }\n }\n long int an = 0;\n long int bn = 1;\n long int ab[2 * n];\n for (int i = 0; i < 2 * n; i++) {\n if (i % 2 == 0) {\n ab[i] = a[an++];\n } else {\n if (bn == n) {\n ab[i] = b[0];\n } else {\n ab[i] = b[bn++];\n }\n }\n }\n double result = (double)m;\n for (int i = ((2 * n) - 1); i >= 0; i--) {\n double result1 = result / ((double)ab[i] - 1);\n result += result1;\n }\n result -= (double)m;\n printf(\"%f\\n\", result);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_to_string(&mut buf).unwrap();\n let mut tok = buf.split_whitespace();\n let mut get = || tok.next().unwrap();\n\n\n let n = get!(usize);\n let m = get!(f64);\n let mut xs = vec![];\n let mut ys = vec![];\n for _ in 0..n {\n let x = get!();\n if x == 1 {\n println!(\"-1\");\n return;\n }\n xs.push(x as f64);\n }\n for _ in 0..n {\n let y = get!();\n if y == 1 {\n println!(\"-1\");\n return;\n }\n ys.push(y as f64);\n }\n\n let mut ans = m / (ys[0] - 1.0);\n\n for i in 0..n {\n let j = n - i - 1;\n ans += (m + ans) / (xs[j] - 1.0);\n if j == 0 {\n break;\n }\n ans += (m + ans) / (ys[j] - 1.0);\n }\n\n println!(\"{:.*}\", 10, ans);\n}", "difficulty": "medium"} {"problem_id": "0233", "problem_description": "A $$$\\mathbf{0}$$$-indexed array $$$a$$$ of size $$$n$$$ is called good if for all valid indices $$$i$$$ ($$$0 \\le i \\le n-1$$$), $$$a_i + i$$$ is a perfect square$$$^\\dagger$$$.Given an integer $$$n$$$. Find a permutation$$$^\\ddagger$$$ $$$p$$$ of $$$[0,1,2,\\ldots,n-1]$$$ that is good or determine that no such permutation exists.$$$^\\dagger$$$ An integer $$$x$$$ is said to be a perfect square if there exists an integer $$$y$$$ such that $$$x = y^2$$$.$$$^\\ddagger$$$ An array $$$b$$$ is a permutation of an array $$$a$$$ if $$$b$$$ consists of the elements of $$$a$$$ in arbitrary order. For example, $$$[4,2,3,4]$$$ is a permutation of $$$[3,2,4,4]$$$ while $$$[1,2,2]$$$ is not a permutation of $$$[1,2,3]$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int k = 0;\n int a[n];\n while (k * k <= 2 * (n - 1)) {\n a[k] = k * k;\n k++;\n }\n int h[n];\n int f[n];\n for (int i = 0; i < n; i++) {\n f[i] = 0;\n }\n int ans = 0;\n for (int i = n - 1; i >= 0; i--) {\n ans = a[k - 1] - i;\n if (ans > n - 1) {\n i++;\n k--;\n } else {\n if (f[ans] == 0) {\n h[i] = ans;\n f[ans] = 1;\n } else {\n i++;\n k--;\n }\n }\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d \", h[i]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).unwrap();\n let t: usize = t.trim().parse().unwrap();\n\n for _i in 0..t {\n let mut n = String::new();\n io::stdin().read_line(&mut n).unwrap();\n let n: usize = n.trim().parse().unwrap();\n\n let mut _n = n;\n let mut a: [usize; 100005] = [0; 100005];\n let mut z: usize = 0;\n\n while _n != 0 {\n if _n < 2 {\n z = 0;\n } else {\n z = ((_n - 2) as f64).sqrt() as usize + 1;\n z = z * z;\n }\n for _j in (z - (_n - 1)).._n {\n a[_j] = z - _j;\n }\n _n = z - (_n - 1);\n }\n for _j in 0..n {\n print!(\"{} \", a[_j]);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0234", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

We have N cards numbered 1, 2, ..., N.\nCard i (1 \\leq i \\leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side.\nInitially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.

\n

Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \\leq i \\leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.

\n
    \n
  • Choose an integer i (1 \\leq i \\leq N - 1).\nSwap the i-th and (i+1)-th cards from the left, then flip these two cards.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 18
  • \n
  • 1 \\leq A_i, B_i \\leq 50 (1 \\leq i \\leq N)
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\nB_1 B_2 ... B_N\n
\n
\n
\n
\n
\n

Output

If it is impossible to have a non-decreasing sequence, print -1.\nIf it is possible, print the minimum number of operations required to achieve it.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n3 4 3\n3 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

By doing the operation once with i = 1, we have a sequence [2, 3, 3] facing up, which is non-decreasing.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n2 1\n1 2\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

After any number of operations, we have the sequence [2, 1] facing up, which is not non-decreasing.

\n
\n
\n
\n
\n
\n

Sample Input 3

4\n1 2 3 4\n5 6 7 8\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

No operation may be required.

\n
\n
\n
\n
\n
\n

Sample Input 4

5\n28 15 22 43 31\n20 22 43 33 32\n
\n
\n
\n
\n
\n

Sample Output 4

-1\n
\n
\n
\n
\n
\n
\n

Sample Input 5

5\n4 46 6 38 43\n33 15 18 27 37\n
\n
\n
\n
\n
\n

Sample Output 5

3\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int A[20];\n int B[20];\n scanf(\"%d\", &N);\n for (i = 1; i <= N; i++) {\n scanf(\"%d\", &(A[i]));\n }\n for (i = 1; i <= N; i++) {\n scanf(\"%d\", &(B[i]));\n }\n\n const int sup = 10000;\n const int bit[19] = {1, 2, 4, 8, 16, 32, 64,\n 128, 256, 512, 1024, 2048, 4096, 8192,\n 16384, 32768, 65536, 131072, 262144};\n int j;\n int k;\n int l;\n int dp[262144][51] = {};\n int X[20][20];\n int tmp;\n int count;\n int min;\n for (i = 1; i <= N; i++) {\n for (j = 1; j <= N; j++) {\n X[i][j] = ((i + j) % 2 == 0) ? A[i] : B[i];\n }\n }\n for (k = 1; k < bit[N]; k++) {\n for (j = 0; j <= 50; j++) {\n dp[k][j] = sup;\n }\n }\n for (k = 1; k < bit[N]; k++) {\n for (i = 0, count = 0; bit[i] <= k; i++) {\n if ((k | bit[i]) == k) {\n count++;\n }\n }\n for (i = 0, l = 0; bit[i] <= k; i++) {\n if ((k | bit[i]) != k) {\n continue;\n }\n tmp = k ^ bit[i];\n if (dp[tmp][X[i + 1][N - count + 1]] < sup) {\n min = dp[tmp][X[i + 1][N - count + 1]] + abs(i - l - (N - count));\n for (j = 0; j <= X[i + 1][N - count + 1]; j++) {\n if (dp[k][j] > min) {\n dp[k][j] = min;\n }\n }\n }\n l++;\n }\n }\n\n if (dp[bit[N] - 1][0] < sup) {\n printf(\"%d\\n\", dp[bit[N] - 1][0]);\n } else {\n printf(\"-1\\n\");\n }\n fflush(stdout);\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 A: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n let B: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n const INF: usize = 1_000_000_000;\n let res = (0..(1 << N))\n .map(|p| {\n let mut X = vec![];\n for i in 0..N {\n X.push((i, 1));\n }\n let mut Y = vec![];\n for i in 0..N {\n if (p >> i) & 1 == 1 {\n Y.push(A[i]);\n } else {\n Y.push(B[i]);\n }\n }\n let mut cost = 0;\n\n for i in 0..N {\n let l = (i..N)\n .find(|&l| ((l - i) + X[l].1) % 2 == (p >> X[l].0) & 1)\n .unwrap_or(INF);\n if l == INF {\n cost = INF;\n break;\n }\n let mut k = l;\n for j in l..N {\n if Y[j] < Y[k] && ((j - i) + X[j].1) % 2 == (p >> X[j].0) & 1 {\n k = j;\n }\n }\n\n for r in (i..k).rev() {\n let tmp = X[r + 1];\n X[r + 1] = (X[r].0, 1 - X[r].1);\n X[r] = (tmp.0, 1 - tmp.1);\n Y.swap(r + 1, r);\n cost += 1;\n }\n }\n if (1..N).any(|i| Y[i - 1] > Y[i]) {\n cost = INF;\n }\n\n cost\n })\n .min()\n .unwrap();\n let ans = if res >= INF { -1 } else { res as i64 };\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0235", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given positive integers A and B.

\n

Find the K-th largest positive integer that divides both A and B.

\n

The input guarantees that there exists such a number.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A, B \\leq 100
  • \n
  • The K-th largest positive integer that divides both A and B exists.
  • \n
  • K \\geq 1
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B K\n
\n
\n
\n
\n
\n

Output

Print the K-th largest positive integer that divides both A and B.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8 12 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Three positive integers divides both 8 and 12: 1, 2 and 4.\nAmong them, the second largest is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

100 50 4\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 1 1\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int k = 0;\n int count = 0;\n scanf(\"%d %d %d\", &a, &b, &k);\n for (int i = 100; i > 0; i--) {\n if (a % i == 0 && b % i == 0) {\n count++;\n if (count == k) {\n printf(\"%d\\n\", i);\n return 0;\n }\n }\n }\n printf(\"ans not found\\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 let a: usize = iter.next().unwrap().parse().unwrap();\n let b: usize = iter.next().unwrap().parse().unwrap();\n let k: usize = iter.next().unwrap().parse().unwrap();\n\n let max = min(a, b);\n let mut count = 0;\n\n for i in (1..max + 1).rev() {\n if a.is_multiple_of(i) && b.is_multiple_of(i) {\n count += 1;\n if count == k {\n println!(\"{}\", i);\n break;\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0236", "problem_description": "You are given a string $$$s$$$ consisting of $$$n$$$ characters. Each character of $$$s$$$ is either 0 or 1.A substring of $$$s$$$ is a contiguous subsequence of its characters.You have to choose two substrings of $$$s$$$ (possibly intersecting, possibly the same, possibly non-intersecting — just any two substrings). After choosing them, you calculate the value of the chosen pair of substrings as follows: let $$$s_1$$$ be the first substring, $$$s_2$$$ be the second chosen substring, and $$$f(s_i)$$$ be the integer such that $$$s_i$$$ is its binary representation (for example, if $$$s_i$$$ is 11010, $$$f(s_i) = 26$$$); the value is the bitwise OR of $$$f(s_1)$$$ and $$$f(s_2)$$$. Calculate the maximum possible value you can get, and print it in binary representation without leading zeroes.", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n char num_origin[n + 1];\n const char *num = num_origin;\n scanf(\"%s\", num_origin);\n\n for (; *num == '0'; num++, n--) {\n ;\n }\n int zero_pos[n];\n int zero_n = 0;\n int first = 0;\n for (; first < n && num[first] == '1'; first++) {\n ;\n }\n if (n == 0) {\n puts(\"0\");\n } else if (first == n) {\n puts(num);\n } else {\n for (int i = 0; i < n; i++) {\n if (num[i] == '0') {\n zero_pos[zero_n++] = i;\n }\n }\n int candidate[first];\n int trash[first];\n int candidate_n = first;\n int trash_n = 0;\n for (int i = 0; i < first; i++) {\n candidate[i] = i + 1;\n }\n int ans;\n for (int i = 0; i < zero_n; i++) {\n trash_n = 0;\n for (int j = 0; j < candidate_n; j++) {\n if (num[zero_pos[i] - candidate[j]] != '1') {\n trash[trash_n++] = candidate[j];\n candidate[j--] = candidate[--candidate_n];\n }\n }\n if (!candidate_n) {\n memcpy(candidate, trash, trash_n);\n candidate_n = trash_n;\n } else if (candidate_n == 1) {\n ans = candidate[0];\n goto finish;\n }\n }\n ans = candidate[0];\n finish:;\n for (int i = 0; i < n; i++) {\n putchar(num[i] == '0' ? num[i - ans] : '1');\n }\n putchar('\\n');\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n s: bytes,\n }\n let one = match s.iter().position(|&c| c == b'1') {\n Some(i) => i,\n None => {\n println!(\"0\");\n return;\n }\n };\n let mut ans = s.clone();\n if let Some(zero) = s[one..].iter().position(|&c| c == b'0') {\n for i in 1..=zero {\n let mut cur = s.clone();\n for j in 0..n - i {\n cur[i + j] |= s[j];\n }\n ans = ans.max(cur);\n }\n }\n println!(\"{}\", std::str::from_utf8(&ans[one..]).unwrap());\n}", "difficulty": "medium"} {"problem_id": "0237", "problem_description": "Ivan is playing a strange game.He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.", "c_code": "int solution() {\n int n;\n int m;\n int k;\n scanf(\"%d%d%d\", &n, &m, &k);\n int numbers[n][m];\n int ct1;\n int ct2;\n int ct3;\n for (ct1 = 0; ct1 < n; ct1++) {\n for (ct2 = 0; ct2 < m; ct2++) {\n scanf(\"%d\", &numbers[ct1][ct2]);\n }\n }\n\n int max = 0;\n int counter = 0;\n int counter2 = 0;\n int test = 0;\n int result1 = 0;\n int result2 = 0;\n\n for (ct1 = 0; ct1 < m; ct1++) {\n max = counter = 0;\n for (ct2 = 0; ct2 < n; ct2++) {\n if (numbers[ct2][ct1] == 1) {\n counter++;\n test = 0;\n for (ct3 = 0; ct3 + ct2 < n && ct3 < k; ct3++) {\n test += numbers[ct3 + ct2][ct1];\n }\n if (test > max) {\n max = test;\n counter2 = counter - 1;\n }\n }\n }\n result1 += max;\n result2 += counter2;\n }\n printf(\"%d %d\", result1, result2);\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let nmk: Vec = line\n .trim()\n .split(\" \")\n .map(|x| x.parse::().unwrap())\n .collect();\n let (n, m, k): (usize, usize, usize) = (nmk[0], nmk[1], nmk[2]);\n\n let mut matrix = vec![vec![0_i32; 0]; n];\n for i in 0..n {\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n matrix[i] = line\n .trim()\n .split(\" \")\n .map(|x| x.parse::().unwrap())\n .collect();\n }\n\n let mut best_score = 0;\n let mut need_to_delete = 0;\n for j in 0..m {\n let mut best_column_deleted = 0;\n let mut top = 0;\n let mut bottom = k;\n let mut current_score = 0;\n let mut current_deleted = 0;\n for i in top..bottom {\n if matrix[i][j] == 1 {\n current_score += 1;\n }\n }\n let mut best_column_score = current_score;\n\n while top < n {\n if matrix[top][j] == 1 {\n current_score -= 1;\n current_deleted += 1;\n }\n\n if bottom < n && matrix[bottom][j] == 1 {\n current_score += 1;\n }\n top += 1;\n bottom += 1;\n\n if current_score > best_column_score {\n best_column_score = current_score;\n best_column_deleted = current_deleted;\n }\n }\n\n best_score += best_column_score;\n need_to_delete += best_column_deleted;\n }\n\n print!(\"{} {}\", best_score, need_to_delete);\n}", "difficulty": "easy"} {"problem_id": "0238", "problem_description": "

Handsel

\n\n\n

\nAlice and Brown are brothers in a family and each receives pocket money in celebration of the coming year. They are very close and share the total amount of the money fifty-fifty. The pocket money each receives is a multiple of 1,000 yen. \n

\n\n

\n Write a program to calculate each one’s share given the amount of money Alice and Brown received.\n

\n\n

Input

\n\n

\n The input is given in the following format.\n

\n
\na b\n
\n\n

\nA line of data is given that contains two values of money: a (1000 ≤ a ≤ 50000) for Alice and b (1000 ≤ b ≤ 50000) for Brown.\n

\n\n\n

Output

\n\n

\n Output the amount of money each of Alice and Brown receive in a line.\n

\n\n

Sample Input 1

\n\n
\n1000 3000\n
\n\n

Sample Output 1

\n
\n2000\n
\n\n

Sample Input 2

\n
\n5000 5000\n
\n\n

Sample Output 2

\n
\n5000\n
\n\n

Sample Input 3

\n
\n1000 2000\n
\n\n

Sample Output 3

\n
\n1500\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int z;\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n z = (a + b) / 2;\n printf(\"%d\\n\", z);\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 let b: usize = iter.next().unwrap().parse().unwrap();\n\n println!(\"{}\", (a + b) / 2);\n}", "difficulty": "easy"} {"problem_id": "0239", "problem_description": "There are $$$n$$$ athletes in front of you. Athletes are numbered from $$$1$$$ to $$$n$$$ from left to right. You know the strength of each athlete — the athlete number $$$i$$$ has the strength $$$s_i$$$.You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams $$$A$$$ and $$$B$$$ so that the value $$$|\\max(A) - \\min(B)|$$$ is as small as possible, where $$$\\max(A)$$$ is the maximum strength of an athlete from team $$$A$$$, and $$$\\min(B)$$$ is the minimum strength of an athlete from team $$$B$$$.For example, if $$$n=5$$$ and the strength of the athletes is $$$s=[3, 1, 2, 6, 4]$$$, then one of the possible split into teams is: first team: $$$A = [1, 2, 4]$$$, second team: $$$B = [3, 6]$$$. In this case, the value $$$|\\max(A) - \\min(B)|$$$ will be equal to $$$|4-3|=1$$$. This example illustrates one of the ways of optimal split into two teams.Print the minimum value $$$|\\max(A) - \\min(B)|$$$.", "c_code": "int solution() {\n int number_of_loops;\n scanf(\"%d\", &number_of_loops);\n int sol[number_of_loops];\n for (int i = 0; i < number_of_loops; i++) {\n int number_of_athletics;\n scanf(\"%d\", &number_of_athletics);\n int athlete_power[number_of_athletics];\n for (int j = 0; j < number_of_athletics; j++) {\n scanf(\"%d\", &athlete_power[j]);\n }\n int diffrent = abs(athlete_power[1] - athlete_power[2]);\n int min_diffrent = diffrent;\n for (int m = 0; m < number_of_athletics; m++) {\n for (int n = 0; n < number_of_athletics; n++) {\n if (m == n) {\n continue;\n }\n diffrent = abs(athlete_power[m] - athlete_power[n]);\n if (diffrent < min_diffrent) {\n min_diffrent = diffrent;\n }\n }\n if (min_diffrent == 0) {\n break;\n }\n }\n sol[i] = min_diffrent;\n }\n\n for (int i = 0; i < number_of_loops; i++) {\n printf(\"%d\\n\", sol[i]);\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 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 mut a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n a.sort();\n let mut ans = 100000000;\n for i in 0..n - 1 {\n ans = std::cmp::min(ans, a[i + 1] - a[i]);\n }\n writeln!(out, \"{}\", ans).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "hard"} {"problem_id": "0240", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

\n

There are N squares numbered 1 to N from left to right.\nEach square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square.

\n

Snuke cast Q spells to move the golems.

\n

The i-th spell consisted of two characters t_i and d_i, where d_i is L or R.\nWhen Snuke cast this spell, for each square with the character t_i, all golems on that square moved to the square adjacent to the left if d_i is L, and moved to the square adjacent to the right if d_i is R.

\n

However, when a golem tried to move left from Square 1 or move right from Square N, it disappeared.

\n

Find the number of golems remaining after Snuke cast the Q spells.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N,Q \\leq 2 \\times 10^{5}
  • \n
  • |s| = N
  • \n
  • s_i and t_i are uppercase English letters.
  • \n
  • d_i is L or R.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N Q\ns\nt_1 d_1\n\\vdots\nt_{Q} d_Q\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4\nABC\nA L\nB L\nB R\nA R\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n
    \n
  • Initially, there is one golem on each square.
  • \n
  • In the first spell, the golem on Square 1 tries to move left and disappears.
  • \n
  • In the second spell, the golem on Square 2 moves left.
  • \n
  • In the third spell, no golem moves.
  • \n
  • In the fourth spell, the golem on Square 1 moves right.
  • \n
  • After the four spells are cast, there is one golem on Square 2 and one golem on Square 3, for a total of two golems remaining.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

8 3\nAABCBDBA\nA L\nB R\nA R\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
    \n
  • After the three spells are cast, there is one golem on Square 2, two golems on Square 4 and two golems on Square 6, for a total of five golems remaining.
  • \n
  • Note that a single spell may move multiple golems.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 15\nSNCZWRCEWB\nB R\nR R\nE R\nW R\nZ L\nS R\nQ L\nW L\nB R\nC L\nA L\nN L\nE R\nZ L\nS L\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution() {\n int n;\n int q;\n int i;\n int j;\n int min;\n int max;\n scanf(\"%d %d\", &n, &q);\n char s[200001];\n scanf(\"%s\", s);\n char t[200001];\n char d[200001];\n for (i = 0; i < q; i++) {\n scanf(\"\\n%c %c\", &t[i], &d[i]);\n }\n for (i = q - 1, min = 0, max = n - 1; i >= 0; i--) {\n switch (d[i]) {\n case 'R':\n if (t[i] == s[max]) {\n max--;\n }\n if (min > 0 && t[i] == s[min - 1]) {\n min--;\n }\n break;\n case 'L':\n if (t[i] == s[min]) {\n min++;\n }\n if (max < n - 1 && t[i] == s[max + 1]) {\n max++;\n }\n break;\n }\n }\n printf(\"%d\", max - min + 1);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n q: usize,\n s: chars,\n td: [(String, String); q]\n };\n let mut lcurrent = 0;\n let mut rcurrent = 0;\n for (t, d) in td.iter().rev() {\n let t = t.chars().next().unwrap();\n let d = d.chars().next().unwrap();\n if d == 'L' {\n if lcurrent < n && s[lcurrent] == t {\n lcurrent += 1;\n }\n if rcurrent > 0 && s[n - rcurrent] == t {\n rcurrent -= 1;\n }\n } else {\n if rcurrent < n && s[n - rcurrent - 1] == t {\n rcurrent += 1;\n }\n if lcurrent > 0 && s[lcurrent - 1] == t {\n lcurrent -= 1;\n }\n }\n }\n\n println!(\"{}\", n - lcurrent - rcurrent);\n}", "difficulty": "medium"} {"problem_id": "0241", "problem_description": "The great hero guards the country where Homer lives. The hero has attack power $$$A$$$ and initial health value $$$B$$$. There are $$$n$$$ monsters in front of the hero. The $$$i$$$-th monster has attack power $$$a_i$$$ and initial health value $$$b_i$$$. The hero or a monster is said to be living, if his or its health value is positive (greater than or equal to $$$1$$$); and he or it is said to be dead, if his or its health value is non-positive (less than or equal to $$$0$$$).In order to protect people in the country, the hero will fight with monsters until either the hero is dead or all the monsters are dead. In each fight, the hero can select an arbitrary living monster and fight with it. Suppose the $$$i$$$-th monster is selected, and the health values of the hero and the $$$i$$$-th monster are $$$x$$$ and $$$y$$$ before the fight, respectively. After the fight, the health values of the hero and the $$$i$$$-th monster become $$$x-a_i$$$ and $$$y-A$$$, respectively. Note that the hero can fight the same monster more than once.For the safety of the people in the country, please tell them whether the great hero can kill all the monsters (even if the great hero himself is dead after killing the last monster).", "c_code": "int solution() {\n int www;\n scanf(\"%d\", &www);\n while (www--) {\n int A;\n int B;\n int n;\n scanf(\"%d%d%d\", &A, &B, &n);\n int a[n];\n int b[n];\n int x = B;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &b[i]);\n x -= (ceil(b[i] / (1.0 * (A)))) * a[i];\n }\n int flag = 0;\n for (int i = 0; i < n; i++) {\n if (x + a[i] > 0) {\n flag = 1;\n break;\n }\n }\n if (flag == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n use std::io::{self, Write};\n\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 (A, B, n): (i64, i64, usize) = (sc.next(), sc.next(), sc.next());\n let a: Vec = (0..n).map(|_| sc.next()).collect();\n let b: Vec = (0..n).map(|_| sc.next()).collect();\n let mut damage = Vec::with_capacity(n);\n for i in 0..n {\n damage.push(((b[i] + A - 1) / A) * a[i]);\n }\n let total_damage: i64 = damage.iter().sum();\n let possible = a.iter().fold(false, |x, y| x | (total_damage - *y < B));\n writeln!(out, \"{}\", if possible { \"YES\" } else { \"NO\" }).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0242", "problem_description": "You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n char s[200001];\n scanf(\"%s\", s);\n int i = 0;\n int j = 0;\n int cnt[4] = {0};\n int n = strlen(s);\n int ans = n + 1;\n for (j = 0; s[j] != '\\0'; j++) {\n cnt[s[j] - '0']++;\n while (cnt[s[i] - '0'] > 1) {\n cnt[s[i] - '0']--;\n i++;\n }\n if (cnt[1] && cnt[2] && cnt[3]) {\n if (ans > j - i + 1) {\n ans = j - i + 1;\n }\n }\n }\n if (ans == n + 1) {\n ans = 0;\n }\n printf(\"%d\\n\", ans);\n }\n}", "rust_code": "fn solution() {\n let inputstatus = 1;\n\n let mut buf = String::new();\n let filename = \"inputrust.txt\";\n\n if inputstatus == 0 {\n let mut f = File::open(filename).expect(\"file not found\");\n f.read_to_string(&mut buf)\n .expect(\"something went wrong reading the file\");\n } else {\n std::io::stdin().read_to_string(&mut buf).unwrap();\n }\n\n let mut iter = buf.split_whitespace();\n\n let n: usize = iter.next().unwrap().parse().unwrap();\n for _ in 0..n {\n let s: &str = iter.next().unwrap();\n const M: usize = 1000000;\n let mut ans: usize = M;\n let mut l: usize;\n let mut r: usize;\n let mut v: [usize; 3] = [M; 3];\n let mut p: usize;\n\n for (i, c) in s.bytes().enumerate() {\n p = c as usize - '1' as usize;\n v[p] = i;\n l = cmp::min(v[0], cmp::min(v[1], v[2]));\n r = cmp::max(v[0], cmp::max(v[1], v[2]));\n if r != M {\n ans = cmp::min(ans, r - l + 1);\n }\n }\n ans = if ans == M { 0 } else { ans };\n println!(\"{}\", ans);\n }\n}", "difficulty": "hard"} {"problem_id": "0243", "problem_description": "This is the hard version of the problem. The difference between the versions is the constraint on $$$n$$$ and the required number of operations. You can make hacks only if all versions of the problem are solved.There are two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$ (a binary string is a string consisting of symbols $$$0$$$ and $$$1$$$). In an operation, you select a prefix of $$$a$$$, and simultaneously invert the bits in the prefix ($$$0$$$ changes to $$$1$$$ and $$$1$$$ changes to $$$0$$$) and reverse the order of the bits in the prefix.For example, if $$$a=001011$$$ and you select the prefix of length $$$3$$$, it becomes $$$011011$$$. Then if you select the entire string, it becomes $$$001001$$$.Your task is to transform the string $$$a$$$ into $$$b$$$ in at most $$$2n$$$ operations. It can be proved that it is always possible.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n char a[n + 1];\n char b[n + 1];\n int r[3 * n];\n int t[3 * n];\n int c = 0;\n int f = 0;\n scanf(\"%s\", a);\n scanf(\"%s\", b);\n int l0 = a[0];\n int l1 = b[0];\n for (int i = 0, j = 0; i < n;) {\n if (j % 2 == 0) {\n while (a[i] == l0 && i < n) {\n i++;\n }\n r[j] = i;\n j++;\n c++;\n } else {\n while (a[i] != l0 && i < n) {\n i++;\n }\n r[j] = i;\n j++;\n c++;\n }\n }\n if (a[n - 1] == '0') {\n c--;\n }\n for (int i = 0, j = 0; i < n;) {\n if (j % 2 == 0) {\n while (b[i] == l1 && i < n) {\n i++;\n }\n t[j] = i;\n j++;\n f++;\n } else {\n while (b[i] != l1 && i < n) {\n i++;\n }\n t[j] = i;\n j++;\n f++;\n }\n }\n if (b[n - 1] == '0') {\n f--;\n }\n printf(\"%d \", f + c);\n for (int i = 0; i < c; i++) {\n printf(\"%d \", r[i]);\n }\n for (int i = f - 1; i >= 0; i--) {\n printf(\"%d \", t[i]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut input = stdin.lock();\n let mut line = String::new();\n let mut current = Vec::new();\n let mut needed = Vec::new();\n let mut operations = Vec::new();\n input.read_line(&mut line).unwrap();\n let mut count: i16 = line.trim().parse().unwrap();\n while count > 0 {\n input.read_line(&mut line).unwrap();\n line.clear();\n input.read_line(&mut line).unwrap();\n for item in line.trim().chars() {\n current.push(item != '0');\n }\n line.clear();\n input.read_line(&mut line).unwrap();\n for item in line.trim().chars() {\n needed.push(item != '0');\n }\n let mut inversed = false;\n let mut head = 0;\n let mut current_tail = current.len();\n let mut needed_tail = current_tail;\n while needed_tail > 0 {\n if inversed {\n current_tail += 1;\n } else {\n current_tail -= 1;\n };\n needed_tail -= 1;\n let needed_item = needed[needed_tail] ^ inversed;\n if current[current_tail] == needed_item {\n continue;\n }\n if current[head] == needed_item {\n operations.push(1);\n }\n operations.push(needed_tail + 1);\n inversed = !inversed;\n std::mem::swap(&mut head, &mut current_tail);\n }\n current.clear();\n needed.clear();\n print!(\"{}\", operations.len());\n for operation in &operations {\n print!(\" {}\", operation);\n }\n println!();\n operations.clear();\n count -= 1;\n }\n}", "difficulty": "hard"} {"problem_id": "0244", "problem_description": "After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of $$$7$$$ because its subribbon a appears $$$7$$$ times, and the ribbon abcdabc has the beauty of $$$2$$$ because its subribbon abc appears twice.The rules are simple. The game will have $$$n$$$ turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after $$$n$$$ turns wins the treasure.Could you find out who is going to be the winner if they all play optimally?", "c_code": "int solution() {\n int n;\n int i;\n\n scanf(\"%d\", &n);\n\n char a[100010];\n char b[100010];\n char c[100010];\n int f1[130];\n int f2[130];\n int f3[130];\n int m1 = 0;\n int m2 = 0;\n int m3 = 0;\n scanf(\"%s %s %s\", a, b, c);\n\n for (i = 0; a[i] != '\\0'; i++) {\n f1[a[i]]++;\n f2[b[i]]++;\n f3[c[i]]++;\n }\n int len = i;\n\n for (i = 'A'; i <= 'z'; i++) {\n if (i > 'Z' && i < 'a') {\n continue;\n }\n\n f1[i] += n;\n f2[i] += n;\n f3[i] += n;\n\n if (f1[i] > len) {\n if (n == 1) {\n f1[i] = len - 1;\n } else {\n f1[i] = len;\n }\n }\n if (f2[i] > len) {\n if (n == 1) {\n f2[i] = len - 1;\n } else {\n f2[i] = len;\n }\n }\n if (f3[i] > len) {\n if (n == 1) {\n f3[i] = len - 1;\n } else {\n f3[i] = len;\n }\n }\n if (f1[i] > m1) {\n m1 = f1[i];\n }\n\n if (f2[i] > m2) {\n m2 = f2[i];\n }\n\n if (f3[i] > m3) {\n m3 = f3[i];\n }\n }\n\n if ((m1 == m2 && m1 > m3) || (m2 == m3 && m2 > m1) || (m3 == m1 && m3 > m2) ||\n (m1 == m2 && m2 == m3)) {\n printf(\"Draw\");\n\n } else if (m1 > m2 && m1 > m3) {\n printf(\"Kuro\");\n } else {\n if (m2 > m3 && m2 > m1) {\n printf(\"Shiro\");\n } else if (m3 > m1 && m3 > m2) {\n printf(\"Katie\");\n }\n }\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\n let n = get!(usize);\n let kuro = get().as_bytes();\n let shiro = get().as_bytes();\n let katie = get().as_bytes();\n\n if n >= kuro.len() {\n println!(\"Draw\");\n return;\n }\n\n let mut kuro_c = vec![0; 1256];\n let mut shiro_c = vec![0; 1256];\n let mut katie_c = vec![0; 1256];\n\n for i in 0..kuro.len() {\n kuro_c[kuro[i] as usize] += 1;\n shiro_c[shiro[i] as usize] += 1;\n katie_c[katie[i] as usize] += 1;\n }\n\n use std::cmp::{max, min};\n let mut kuro_mx = 0;\n let mut shiro_mx = 0;\n let mut katie_mx = 0;\n for i in 0..kuro_c.len() {\n kuro_mx = max(kuro_mx, kuro_c[i]);\n shiro_mx = max(shiro_mx, shiro_c[i]);\n katie_mx = max(katie_mx, katie_c[i]);\n }\n let sz = kuro.len();\n if n == 1 {\n kuro_mx = if kuro_mx == sz {\n kuro_mx - 1\n } else {\n kuro_mx + 1\n };\n shiro_mx = if shiro_mx == sz {\n shiro_mx - 1\n } else {\n shiro_mx + 1\n };\n katie_mx = if katie_mx == sz {\n katie_mx - 1\n } else {\n katie_mx + 1\n };\n } else {\n kuro_mx = min(kuro_mx + n, sz);\n shiro_mx = min(shiro_mx + n, sz);\n katie_mx = min(katie_mx + n, sz);\n }\n let mut xs = [(kuro_mx, 0), (shiro_mx, 1), (katie_mx, 2)];\n xs.sort();\n\n if xs[1].0 == xs[2].0 {\n println!(\"Draw\");\n return;\n }\n\n if xs[2].1 == 0 {\n println!(\"Kuro\");\n } else if xs[2].1 == 1 {\n println!(\"Shiro\");\n } else {\n println!(\"Katie\");\n }\n}", "difficulty": "easy"} {"problem_id": "0245", "problem_description": "Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).", "c_code": "int solution() {\n int n;\n int k;\n char s[102];\n scanf(\"%d%d\\n\", &n, &k);\n int a[102];\n for (int i = 0; i < 102; i++) {\n a[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", s);\n a[strlen(s)]++;\n }\n scanf(\"%s\", s);\n int m = strlen(s);\n int sum = 0;\n for (int i = 0; i < m; i++) {\n sum = sum + a[i];\n }\n int mn = sum + 1;\n int mx = sum + a[m];\n printf(\"%d %d\", mn + (((mn - 1) / k) * 5), mx + (((mx - 1) / k) * 5));\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let handle = stdin.lock();\n let mut i = 0;\n let (mut n, mut k) = (0, 0);\n\n let mut passwords: Vec = Vec::with_capacity(110);\n for _ in 0..110 {\n passwords.push(0);\n }\n\n let mut answer = 0;\n\n for line in handle.lines() {\n if i == 0 {\n let split = line.unwrap();\n let mut iter = split.split_whitespace();\n n = iter.next().unwrap().parse().unwrap();\n k = iter.next().unwrap().parse().unwrap();\n } else if i == (n + 1) {\n answer = line.unwrap().len();\n break;\n } else {\n passwords[line.unwrap().len()] += 1;\n }\n i += 1;\n }\n\n let (mut lower, mut higher) = (0, 0);\n\n for i in 1..answer {\n lower += passwords[i];\n higher += passwords[i];\n }\n lower += 1;\n higher += passwords[answer];\n\n lower += 5 * ((lower - 1) / k);\n higher += 5 * ((higher - 1) / k);\n\n println!(\"{} {}\", lower, higher);\n}", "difficulty": "medium"} {"problem_id": "0246", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

\n

Initially, there is no ball placed.

\n

Takahashi, who is very patient, will do the following operation 10^{100} times:

\n
    \n
  • Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.
  • \n
\n

How many blue balls will be there among the first N balls in the row of balls made this way?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^{18}
  • \n
  • A, B \\geq 0
  • \n
  • 0 < A + B \\leq 10^{18}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B\n
\n
\n
\n
\n
\n

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8 3 4\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

\n
\n
\n
\n
\n
\n

Sample Input 2

8 0 4\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

He placed only red balls from the beginning.

\n
\n
\n
\n
\n
\n

Sample Input 3

6 2 4\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n

Among bbrrrr, there are two blue balls.

\n
\n
", "c_code": "int solution() {\n long long n;\n long long a;\n long long b;\n scanf(\"%lld%lld%lld\", &n, &a, &b);\n if ((a + b) == 0) {\n printf(\"0\\n\");\n } else {\n if (n % (a + b) > a) {\n printf(\"%lld\", a + (a * (n / (a + b))));\n } else {\n printf(\"%lld\\n\", (a * (n / (a + b))) + (n % (a + b)));\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n if io::stdin().read_line(&mut input).is_ok() {\n let v: Vec = input\n .trim()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let count = v[0];\n let unit = v[1] + v[2];\n let blue = v[1];\n let div = count / unit;\n let res = count % unit;\n println!(\"{}\", div * blue + min(res, blue));\n }\n}", "difficulty": "hard"} {"problem_id": "0247", "problem_description": "On an $$$8 \\times 8$$$ grid, some horizontal rows have been painted red, and some vertical columns have been painted blue, in some order. The stripes are drawn sequentially, one after the other. When the stripe is drawn, it repaints all the cells through which it passes.Determine which color was used last. The red stripe was painted after the blue one, so the answer is R.", "c_code": "int solution() {\n int i = 0;\n\n char str[8][8];\n scanf(\"%d\", &i);\n for (int j = 0; j < i; j++) {\n int b = 0;\n\n for (int k = 0; k < 8; ++k) {\n scanf(\"%s\", str[k]);\n }\n\n for (int c = 0; c < 8; ++c) {\n int cnt = 0;\n\n for (int b = 0; b < 8; ++b) {\n if (str[c][b] == 'R') {\n ++cnt;\n }\n }\n if (cnt == 8) {\n b = 1;\n break;\n }\n }\n if (b == 1) {\n printf(\"R\\n\");\n } else {\n printf(\"B\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n (0..{\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n buf.trim().parse::().unwrap()\n })\n .for_each(|_| {\n let mut lines = [['.'; 8]; 8];\n let mut buf = String::new();\n lines[0] = loop {\n if {\n buf.clear();\n stdin().read_line(&mut buf).unwrap();\n !buf.trim().is_empty()\n } {\n break {\n let mut chars = ['.'; 8];\n buf.trim()\n .split(\"\")\n .filter_map(|x| {\n if !x.is_empty() {\n Some(x.chars().next().unwrap())\n } else {\n None\n }\n })\n .enumerate()\n .for_each(|(i, x)| {\n chars[i] = x;\n });\n chars\n };\n }\n };\n (1..8).for_each(|i| {\n lines[i] = {\n buf.clear();\n stdin().read_line(&mut buf).unwrap();\n let mut chars = ['.'; 8];\n buf.trim()\n .split(\"\")\n .filter_map(|x| {\n if !x.trim().is_empty() {\n Some(x.chars().next().unwrap())\n } else {\n None\n }\n })\n .enumerate()\n .for_each(|(i, x)| {\n chars[i] = x;\n });\n chars\n };\n });\n\n println!(\n \"{}\",\n if {\n let mut is_r = true;\n for i in 0..8 {\n let mut flag = true;\n for j in 1..8 {\n if lines[i][j] != 'R' || lines[i][j - 1] != 'R' {\n is_r = false;\n flag = false;\n break;\n } else {\n is_r = true;\n flag = true;\n }\n }\n if flag {\n break;\n }\n }\n is_r\n } {\n \"R\"\n } else {\n \"B\"\n }\n );\n });\n}", "difficulty": "easy"} {"problem_id": "0248", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Based on some criterion, Snuke divided the integers from 1 through 12 into three groups as shown in the figure below.\nGiven two integers x and y (1 ≤ x < y ≤ 12), determine whether they belong to the same group.

\n
\n\"b4ab979900ed647703389d4349eb84ee.png\"\n
\n
\n
\n
\n
\n

Constraints

    \n
  • x and y are integers.
  • \n
  • 1 ≤ x < y ≤ 12
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
x y\n
\n
\n
\n
\n
\n

Output

If x and y belong to the same group, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 3\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 2

2 4\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
", "c_code": "int solution() {\n\n int group1[7] = {1, 3, 5, 7, 8, 10, 12};\n int group2[4] = {4, 6, 9, 11};\n int group3[1] = {2};\n\n int x = 0;\n int y = 0;\n\n scanf(\"%d %d\", &x, &y);\n\n int xOK = 0;\n int yOK = 0;\n int common = 0;\n\n for (int i = 0; i < 7; i++) {\n if (group1[i] == x) {\n xOK = 1;\n }\n if (group1[i] == y) {\n yOK = 1;\n }\n }\n\n if (xOK == 1 && yOK == 1) {\n common = 1;\n }\n\n xOK = 0;\n yOK = 0;\n for (int i = 0; i < 4; i++) {\n if (group2[i] == x) {\n xOK = 1;\n }\n if (group2[i] == y) {\n yOK = 1;\n }\n }\n\n if (xOK == 1 && yOK == 1) {\n common = 1;\n }\n\n xOK = 0;\n yOK = 0;\n for (int i = 0; i < 1; i++) {\n if (group3[i] == x) {\n xOK = 1;\n }\n if (group3[i] == y) {\n yOK = 1;\n }\n }\n\n if (xOK == 1 && yOK == 1) {\n common = 1;\n }\n\n if (common == 1) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\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| i64::from_str(n).unwrap());\n let (x, y) = (it.next().unwrap(), it.next().unwrap());\n\n let mut a: HashSet<_> = [1, 3, 5, 7, 8, 10, 12].iter().cloned().collect();\n let mut b: HashSet<_> = [4, 6, 9, 11].iter().cloned().collect();\n\n a.insert(x);\n a.insert(y);\n if a.len() == 7 {\n println!(\"Yes\");\n return;\n }\n b.insert(x);\n b.insert(y);\n if b.len() == 4 {\n println!(\"Yes\");\n return;\n }\n\n if x == 2 && y == 2 {\n println!(\"Yes\");\n return;\n }\n println!(\"No\");\n}", "difficulty": "easy"} {"problem_id": "0249", "problem_description": "Let's consider all integers in the range from $$$1$$$ to $$$n$$$ (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of $$$\\mathrm{gcd}(a, b)$$$, where $$$1 \\leq a < b \\leq n$$$.The greatest common divisor, $$$\\mathrm{gcd}(a, b)$$$, of two positive integers $$$a$$$ and $$$b$$$ is the biggest integer that is a divisor of both $$$a$$$ and $$$b$$$.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n int s[n];\n int i;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &s[i]);\n }\n for (i = 0; i < n; i++) {\n int a = s[i] / 2;\n printf(\"%d\\n\", a);\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 n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n println!(\"{}\", n / 2);\n }\n}", "difficulty": "medium"} {"problem_id": "0250", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

AtCoDeer the deer found two positive integers, a and b.\nDetermine whether the product of a and b is even or odd.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 a,b 10000
  • \n
  • a and b are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b\n
\n
\n
\n
\n
\n

Output

If the product is odd, print Odd; if it is even, print Even.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4\n
\n
\n
\n
\n
\n

Sample Output 1

Even\n
\n

As 3 × 4 = 12 is even, print Even.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 21\n
\n
\n
\n
\n
\n

Sample Output 2

Odd\n
\n

As 1 × 21 = 21 is odd, print Odd.

\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n scanf(\"%d %d\", &a, &b);\n\n if (a * b % 2 == 0) {\n printf(\"Even\");\n } else {\n printf(\"Odd\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let cond = buf\n .trim()\n .split(' ')\n .map(|x| x.parse::().unwrap())\n .product::()\n % 2\n == 0;\n if cond {\n println!(\"Even\");\n } else {\n println!(\"Odd\");\n }\n}", "difficulty": "easy"} {"problem_id": "0251", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We conducted a survey on newspaper subscriptions.\nMore specifically, we asked each of the N respondents the following two questions:

\n
    \n
  • Question 1: Are you subscribing to Newspaper X?
  • \n
  • Question 2: Are you subscribing to Newspaper Y?
  • \n
\n

As the result, A respondents answered \"yes\" to Question 1, and B respondents answered \"yes\" to Question 2.

\n

What are the maximum possible number and the minimum possible number of respondents subscribing to both newspapers X and Y?

\n

Write a program to answer this question.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 0 \\leq A \\leq N
  • \n
  • 0 \\leq B \\leq N
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number and the minimum possible number of respondents subscribing to both newspapers, in this order, with a space in between.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10 3 5\n
\n
\n
\n
\n
\n

Sample Output 1

3 0\n
\n

In this sample, out of the 10 respondents, 3 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

\n

Here, the number of respondents subscribing to both newspapers is at most 3 and at least 0.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 7 5\n
\n
\n
\n
\n
\n

Sample Output 2

5 2\n
\n

In this sample, out of the 10 respondents, 7 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

\n

Here, the number of respondents subscribing to both newspapers is at most 5 and at least 2.

\n
\n
\n
\n
\n
\n

Sample Input 3

100 100 100\n
\n
\n
\n
\n
\n

Sample Output 3

100 100\n
\n
\n
", "c_code": "int solution() {\n int n_person = 0;\n int a_newsp = 0;\n int b_newsp = 0;\n int both = 0;\n int min = 0;\n\n scanf(\"%d %d %d\", &n_person, &a_newsp, &b_newsp);\n\n if (a_newsp > b_newsp) {\n both = b_newsp;\n } else {\n both = a_newsp;\n }\n\n min = (a_newsp + b_newsp) - n_person;\n if (min > 0) {\n } else {\n min = 0;\n }\n printf(\"%d %d\", both, min);\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 a: usize = itr.next().unwrap().parse().unwrap();\n let b: usize = itr.next().unwrap().parse().unwrap();\n println!(\"{} {}\", min(a, b), (a + b).saturating_sub(n));\n}", "difficulty": "medium"} {"problem_id": "0252", "problem_description": "Polycarp has $$$3$$$ positive integers $$$a$$$, $$$b$$$ and $$$c$$$. He can perform the following operation exactly once. Choose a positive integer $$$m$$$ and multiply exactly one of the integers $$$a$$$, $$$b$$$ or $$$c$$$ by $$$m$$$. Can Polycarp make it so that after performing the operation, the sequence of three numbers $$$a$$$, $$$b$$$, $$$c$$$ (in this order) forms an arithmetic progression? Note that you cannot change the order of $$$a$$$, $$$b$$$ and $$$c$$$.Formally, a sequence $$$x_1, x_2, \\dots, x_n$$$ is called an arithmetic progression (AP) if there exists a number $$$d$$$ (called \"common difference\") such that $$$x_{i+1}=x_i+d$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$. In this problem, $$$n=3$$$.For example, the following sequences are AP: $$$[5, 10, 15]$$$, $$$[3, 2, 1]$$$, $$$[1, 1, 1]$$$, and $$$[13, 10, 7]$$$. The following sequences are not AP: $$$[1, 2, 4]$$$, $$$[0, 1, 0]$$$ and $$$[1, 3, 2]$$$.You need to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int testcases;\n scanf(\"%d\", &testcases);\n int arr[testcases][3];\n for (int i = 0; i < testcases; i++) {\n scanf(\"%d%d%d\", &arr[i][0], &arr[i][1], &arr[i][2]);\n }\n for (int i = 0; i < testcases; i++) {\n if ((arr[i][0] + arr[i][2]) % (2 * arr[i][1]) == 0 ||\n (2 * arr[i][1] - arr[i][2]) % arr[i][0] == 0 &&\n 2 * arr[i][1] - arr[i][2] > 0 ||\n (2 * arr[i][1] - arr[i][0]) % arr[i][2] == 0 &&\n 2 * arr[i][1] - arr[i][0] > 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n };\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut handle = std::io::BufWriter::new(std::io::stdout());\n let mut test_no = String::new();\n std::io::stdin().read_line(&mut test_no).unwrap();\n let test_no: u16 = test_no.trim().parse().unwrap();\n for _ in 0..test_no {\n let mut string = String::new();\n std::io::stdin().read_line(&mut string).unwrap();\n let arraystr: Vec<&str> = string.trim().split(' ').collect();\n let array: Vec = arraystr.iter().map(|x| x.parse().unwrap()).collect();\n let a = array[0];\n let b = array[1];\n let c = array[2];\n if (a + c).is_multiple_of(2 * b)\n || (2 * b - a).is_multiple_of(c) && 2 * b > a\n || (2 * b - c).is_multiple_of(a) && 2 * b > c\n {\n writeln!(handle, \"YES\").expect(\"\");\n } else {\n writeln!(handle, \"NO\").expect(\"\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0253", "problem_description": "The Saratov State University Olympiad Programmers Training Center (SSU OPTC) has n students. For each student you know the number of times he/she has participated in the ACM ICPC world programming championship. According to the ACM ICPC rules, each person can participate in the world championship at most 5 times.The head of the SSU OPTC is recently gathering teams to participate in the world championship. Each team must consist of exactly three people, at that, any person cannot be a member of two or more teams. What maximum number of teams can the head make if he wants each team to participate in the world championship with the same members at least k times?", "c_code": "int solution() {\n int n = 0;\n int k = 0;\n scanf(\"%i %i\", &n, &k);\n int z[2000] = {0};\n int diff = 5 - k;\n int number = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%i\", &z[i]);\n if (z[i] <= diff) {\n number++;\n }\n }\n number = number / 3;\n\n printf(\"%i\", number);\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 k = 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 let n = v.iter().filter(|&i| *i <= 5 - k).count();\n println!(\"{:?}\", n / 3);\n}", "difficulty": "medium"} {"problem_id": "0254", "problem_description": "You are given $$$n$$$ strings $$$s_1, s_2, \\ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n char arr[1002];\n int total = 0;\n int ch[260];\n for (int g = 0; g < 260; g++) {\n ch[g] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", arr);\n total += strlen(arr);\n for (int y = 0; arr[y] != 0; y++) {\n ch[arr[y]]++;\n }\n }\n int win = 1;\n for (int h = 0; h < 260; h++) {\n if (ch[h] % n == 0) {\n continue;\n }\n win = 0;\n break;\n }\n if (win == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> std::io::Result<()> {\n let stdin = std::io::stdin();\n let mut input = String::new();\n\n stdin.read_line(&mut input)?;\n\n let t: u8 = input.trim().parse().expect(\"'t' is not a integer!\");\n input.clear();\n\n for _ in 0..t {\n stdin.read_line(&mut input)?;\n let n: usize = input.trim().parse().expect(\"'n' is not a integer!\");\n input.clear();\n\n let mut freq: [usize; 256] = [0; 256];\n\n for _ in 0..n {\n stdin.read_line(&mut input)?;\n\n for &i in input.trim().as_bytes() {\n freq[i as usize] += 1;\n }\n\n input.clear();\n }\n\n let x: usize = freq.iter().map(|i| i % n).sum();\n\n if x == 0 {\n println!(\"YES\")\n } else {\n println!(\"NO\")\n }\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0255", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

In some village, there are 999 towers that are 1,(1+2),(1+2+3),...,(1+2+3+...+999) meters high from west to east, at intervals of 1 meter.

\n

It had been snowing for a while before it finally stopped. For some two adjacent towers located 1 meter apart, we measured the lengths of the parts of those towers that are not covered with snow, and the results are a meters for the west tower, and b meters for the east tower.

\n

Assuming that the depth of snow cover and the altitude are the same everywhere in the village, find the amount of the snow cover.

\n

Assume also that the depth of the snow cover is always at least 1 meter.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq a < b < 499500(=1+2+3+...+999)
  • \n
  • All values in input are integers.
  • \n
  • There is no input that contradicts the assumption.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b\n
\n
\n
\n
\n
\n

Output

If the depth of the snow cover is x meters, print x as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8 13\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The heights of the two towers are 10 meters and 15 meters, respectively.\nThus, we can see that the depth of the snow cover is 2 meters.

\n
\n
\n
\n
\n
\n

Sample Input 2

54 65\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n scanf(\"%d%d\", &a, &b);\n\n int i = 0;\n int sum = 0;\n for (i = 1; i <= b - a; i++) {\n sum += i;\n }\n printf(\"%d\\n\", sum - b);\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let vec: Vec<&str> = buf.split_whitespace().collect();\n let a: i32 = vec[0].parse().unwrap();\n let b: i32 = vec[1].parse().unwrap();\n let c: i32 = b - a;\n let mut ans = 0;\n for x in 1..c {\n ans += x;\n }\n println!(\"{:?}\", ans - a);\n}", "difficulty": "medium"} {"problem_id": "0256", "problem_description": "A frog is currently at the point $$$0$$$ on a coordinate axis $$$Ox$$$. It jumps by the following algorithm: the first jump is $$$a$$$ units to the right, the second jump is $$$b$$$ units to the left, the third jump is $$$a$$$ units to the right, the fourth jump is $$$b$$$ units to the left, and so on.Formally: if the frog has jumped an even number of times (before the current jump), it jumps from its current position $$$x$$$ to position $$$x+a$$$; otherwise it jumps from its current position $$$x$$$ to position $$$x-b$$$. Your task is to calculate the position of the frog after $$$k$$$ jumps.But... One more thing. You are watching $$$t$$$ different frogs so you have to answer $$$t$$$ independent queries.", "c_code": "int solution() {\n long long int x;\n scanf(\"%lld\", &x);\n long long int b[x];\n for (int i = 0; i < x; i++) {\n long long int a[3];\n for (int j = 0; j < 3; j++) {\n scanf(\"%lld\", &a[j]);\n }\n if (a[2] % 2 == 0) {\n b[i] = (a[0] - a[1]) * (a[2] / 2);\n } else {\n b[i] = (a[0] - a[1]) * (a[2] / 2) + a[0];\n }\n }\n for (int i = 0; i < x; i++) {\n printf(\"%lld\\n\", b[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"INPUT::read line failed\");\n\n let mut amount = input\n .trim()\n .parse::()\n .expect(\"AMOUNT::parse i32 failed\");\n\n while amount > 0 {\n let mut input = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"INPUT::read line failed\");\n let mut input = input.trim().split(' ');\n let mut numbers = [i64::default(); 3];\n\n for i in numbers.iter_mut() {\n *i = input.next().unwrap().parse::().unwrap();\n }\n\n let outcome = (numbers[0] - numbers[1]) * (numbers[2] / 2);\n\n match numbers[2] % 2 {\n 1 => println!(\"{}\", outcome + numbers[0]),\n 0 => println!(\"{}\", outcome),\n _ => {}\n }\n\n amount -= 1;\n }\n}", "difficulty": "easy"} {"problem_id": "0257", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.

\n

You will do the following operation N-1 times:

\n
    \n
  • Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.
  • \n
\n

After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.

\n

Find the maximum number of points you can gain.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2000
  • \n
  • 1 \\leq A_i \\leq N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 \\cdots A_{3N}\n
\n
\n
\n
\n
\n

Output

Print the maximum number of points you can gain.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n1 2 1 2 2 1\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Let us rearrange the five leftmost cards so that the integers written on the six cards will be 2\\ 2\\ 2\\ 1\\ 1\\ 1 from left to right.

\n

Then, remove the three leftmost cards, all of which have the same integer 2, gaining 1 point.

\n

Now, the integers written on the remaining cards are 1\\ 1\\ 1.

\n

Since these three cards have the same integer 1, we gain 1 more point.

\n

In this way, we can gain 2 points - which is the maximum possible.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 1 2 2 3 3 3 2 1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3\n1 1 2 2 2 3 3 3 1\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int A[6001];\n scanf(\"%d\", &N);\n for (i = 1; i <= N * 3; i++) {\n scanf(\"%d\", &(A[i]));\n }\n\n int j;\n int k = 1;\n char dp[2001][2001] = {};\n char tmp[2001];\n char count[2001] = {};\n char flag[2001][2001] = {};\n char tmp_flag[2001];\n dp[1][A[1]]++;\n dp[1][A[2]]++;\n for (i = 1; i < N; i++) {\n if (A[i * 3] == A[(i * 3) + 1] && A[(i * 3) + 1] == A[(i * 3) + 2]) {\n for (j = 1; j <= N; j++) {\n dp[k + 1][j] = dp[k][j];\n flag[k + 1][j] = flag[k][j];\n if (dp[k - 1][j] > dp[k][j] ||\n (dp[k - 1][j] == dp[k][j] && flag[k - 1][j] < flag[k][j])) {\n dp[k][j] = dp[k - 1][j];\n flag[k][j] = flag[k - 1][j];\n }\n }\n k++;\n } else {\n count[A[i * 3]]++;\n count[A[(i * 3) + 1]]++;\n count[A[(i * 3) + 2]]++;\n if (dp[k][A[i * 3]] + count[A[i * 3]] >= 3 ||\n dp[k][A[(i * 3) + 1]] + count[A[(i * 3) + 1]] >= 3 ||\n dp[k][A[(i * 3) + 2]] + count[A[(i * 3) + 2]] >= 3) {\n if (dp[k][A[i * 3]] + count[A[i * 3]] >= 3) {\n for (j = 1; j <= N; j++) {\n tmp[j] = 0;\n tmp_flag[j] = 0;\n }\n if (count[A[i * 3]] == 2) {\n if (dp[k][A[i * 3]] == 2 || flag[k][A[i * 3]] == 0) {\n for (j = 1; j <= N; j++) {\n if (dp[k][j] == 2 || j == A[i * 3]) {\n tmp[j] = dp[k][j] - 1;\n } else {\n tmp[j] = dp[k][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n } else {\n for (j = 1; j <= N; j++) {\n if (dp[k][j] == 2 || flag[k][j] == 1) {\n tmp[j] = dp[k][j] - 1;\n } else {\n tmp[j] = dp[k][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n }\n if (A[i * 3] == A[(i * 3) + 1]) {\n tmp[A[(i * 3) + 2]]++;\n } else {\n tmp[A[(i * 3) + 1]]++;\n }\n } else {\n tmp[A[(i * 3) + 1]]++;\n tmp[A[(i * 3) + 2]]++;\n }\n for (j = 1; j <= N; j++) {\n if (tmp[j] > dp[k + 1][j] ||\n (tmp[j] == dp[k + 1][j] && tmp_flag[j] < flag[k + 1][j])) {\n dp[k + 1][j] = tmp[j];\n flag[k + 1][j] = tmp_flag[j];\n }\n }\n }\n if (dp[k][A[(i * 3) + 1]] + count[A[(i * 3) + 1]] >= 3) {\n for (j = 1; j <= N; j++) {\n tmp[j] = 0;\n tmp_flag[j] = 0;\n }\n if (count[A[(i * 3) + 1]] == 2) {\n if (dp[k][A[(i * 3) + 1]] == 2 || flag[k][A[(i * 3) + 1]] == 0) {\n for (j = 1; j <= N; j++) {\n if (dp[k][j] == 2 || j == A[(i * 3) + 1]) {\n tmp[j] = dp[k][j] - 1;\n } else {\n tmp[j] = dp[k][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n } else {\n for (j = 1; j <= N; j++) {\n if (dp[k][j] == 2 || flag[k][j] == 1) {\n tmp[j] = dp[k][j] - 1;\n } else {\n tmp[j] = dp[k][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n }\n if (A[i * 3] == A[(i * 3) + 1]) {\n tmp[A[(i * 3) + 2]]++;\n } else {\n tmp[A[i * 3]]++;\n }\n } else {\n tmp[A[i * 3]]++;\n tmp[A[(i * 3) + 2]]++;\n }\n for (j = 1; j <= N; j++) {\n if (tmp[j] > dp[k + 1][j] ||\n (tmp[j] == dp[k + 1][j] && tmp_flag[j] < flag[k + 1][j])) {\n dp[k + 1][j] = tmp[j];\n flag[k + 1][j] = tmp_flag[j];\n }\n }\n }\n if (dp[k][A[(i * 3) + 2]] + count[A[(i * 3) + 2]] >= 3) {\n for (j = 1; j <= N; j++) {\n tmp[j] = 0;\n tmp_flag[j] = 0;\n }\n if (count[A[(i * 3) + 2]] == 2) {\n if (dp[k][A[(i * 3) + 2]] == 2 || flag[k][A[(i * 3) + 2]] == 0) {\n for (j = 1; j <= N; j++) {\n if (dp[k][j] == 2 || j == A[(i * 3) + 2]) {\n tmp[j] = dp[k][j] - 1;\n } else {\n tmp[j] = dp[k][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n } else {\n for (j = 1; j <= N; j++) {\n if (dp[k][j] == 2 || flag[k][j] == 1) {\n tmp[j] = dp[k][j] - 1;\n } else {\n tmp[j] = dp[k][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n }\n if (A[i * 3] == A[(i * 3) + 2]) {\n tmp[A[(i * 3) + 1]]++;\n } else {\n tmp[A[i * 3]]++;\n }\n } else {\n tmp[A[i * 3]]++;\n tmp[A[(i * 3) + 1]]++;\n }\n for (j = 1; j <= N; j++) {\n if (tmp[j] > dp[k + 1][j] ||\n (tmp[j] == dp[k + 1][j] && tmp_flag[j] < flag[k + 1][j])) {\n dp[k + 1][j] = tmp[j];\n flag[k + 1][j] = tmp_flag[j];\n }\n }\n }\n\n if (dp[k][A[i * 3]] < 2) {\n dp[k][A[i * 3]]++;\n }\n if (dp[k][A[(i * 3) + 1]] < 2) {\n dp[k][A[(i * 3) + 1]]++;\n }\n if (dp[k][A[(i * 3) + 2]] < 2) {\n dp[k][A[(i * 3) + 2]]++;\n }\n\n if (dp[k - 1][A[i * 3]] + count[A[i * 3]] >= 3 ||\n dp[k - 1][A[(i * 3) + 1]] + count[A[(i * 3) + 1]] >= 3 ||\n dp[k - 1][A[(i * 3) + 2]] + count[A[(i * 3) + 2]] >= 3) {\n if (dp[k - 1][A[i * 3]] + count[A[i * 3]] >= 3) {\n for (j = 1; j <= N; j++) {\n tmp[j] = 0;\n tmp_flag[j] = 0;\n }\n if (count[A[i * 3]] == 2) {\n if (dp[k - 1][A[i * 3]] == 2 || flag[k - 1][A[i * 3]] == 0) {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || j == A[i * 3]) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n } else {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || flag[k - 1][j] == 1) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n }\n if (A[i * 3] == A[(i * 3) + 1]) {\n tmp[A[(i * 3) + 2]]++;\n } else {\n tmp[A[(i * 3) + 1]]++;\n }\n } else {\n tmp[A[(i * 3) + 1]]++;\n tmp[A[(i * 3) + 2]]++;\n }\n for (j = 1; j <= N; j++) {\n if (tmp[j] > dp[k][j] ||\n (tmp[j] == dp[k][j] && tmp_flag[j] < flag[k][j])) {\n dp[k][j] = tmp[j];\n flag[k][j] = tmp_flag[j];\n }\n }\n }\n if (dp[k - 1][A[(i * 3) + 1]] + count[A[(i * 3) + 1]] >= 3) {\n for (j = 1; j <= N; j++) {\n tmp[j] = 0;\n tmp_flag[j] = 0;\n }\n if (count[A[(i * 3) + 1]] == 2) {\n if (dp[k - 1][A[(i * 3) + 1]] == 2 ||\n flag[k - 1][A[(i * 3) + 1]] == 0) {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || j == A[(i * 3) + 1]) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n } else {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || flag[k - 1][j] == 1) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n }\n if (A[i * 3] == A[(i * 3) + 1]) {\n tmp[A[(i * 3) + 2]]++;\n } else {\n tmp[A[i * 3]]++;\n }\n } else {\n tmp[A[i * 3]]++;\n tmp[A[(i * 3) + 2]]++;\n }\n for (j = 1; j <= N; j++) {\n if (tmp[j] > dp[k][j] ||\n (tmp[j] == dp[k][j] && tmp_flag[j] < flag[k][j])) {\n dp[k][j] = tmp[j];\n flag[k][j] = tmp_flag[j];\n }\n }\n }\n if (dp[k - 1][A[(i * 3) + 2]] + count[A[(i * 3) + 2]] >= 3) {\n for (j = 1; j <= N; j++) {\n tmp[j] = 0;\n tmp_flag[j] = 0;\n }\n if (count[A[(i * 3) + 2]] == 2) {\n if (dp[k - 1][A[(i * 3) + 2]] == 2 ||\n flag[k - 1][A[(i * 3) + 2]] == 0) {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || j == A[(i * 3) + 2]) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n } else {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || flag[k - 1][j] == 1) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n }\n if (A[i * 3] == A[(i * 3) + 2]) {\n tmp[A[(i * 3) + 1]]++;\n } else {\n tmp[A[i * 3]]++;\n }\n } else {\n tmp[A[i * 3]]++;\n tmp[A[(i * 3) + 1]]++;\n }\n for (j = 1; j <= N; j++) {\n if (tmp[j] > dp[k][j] ||\n (tmp[j] == dp[k][j] && tmp_flag[j] < flag[k][j])) {\n dp[k][j] = tmp[j];\n flag[k][j] = tmp_flag[j];\n }\n }\n }\n }\n\n k++;\n } else {\n if (dp[k][A[i * 3]] < 2) {\n dp[k][A[i * 3]]++;\n }\n if (dp[k][A[(i * 3) + 1]] < 2) {\n dp[k][A[(i * 3) + 1]]++;\n }\n if (dp[k][A[(i * 3) + 2]] < 2) {\n dp[k][A[(i * 3) + 2]]++;\n }\n\n if (dp[k - 1][A[i * 3]] + count[A[i * 3]] >= 3 ||\n dp[k - 1][A[(i * 3) + 1]] + count[A[(i * 3) + 1]] >= 3 ||\n dp[k - 1][A[(i * 3) + 2]] + count[A[(i * 3) + 2]] >= 3) {\n if (dp[k - 1][A[i * 3]] + count[A[i * 3]] >= 3) {\n for (j = 1; j <= N; j++) {\n tmp[j] = 0;\n tmp_flag[j] = 0;\n }\n if (count[A[i * 3]] == 2) {\n if (dp[k - 1][A[i * 3]] == 2 || flag[k - 1][A[i * 3]] == 0) {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || j == A[i * 3]) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n } else {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || flag[k - 1][j] == 1) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n }\n }\n if (A[i * 3] == A[(i * 3) + 1]) {\n tmp[A[(i * 3) + 2]]++;\n } else {\n tmp[A[(i * 3) + 1]]++;\n }\n } else {\n tmp[A[(i * 3) + 1]]++;\n tmp[A[(i * 3) + 2]]++;\n }\n for (j = 1; j <= N; j++) {\n if (tmp[j] > dp[k][j] ||\n (tmp[j] == dp[k][j] && tmp_flag[j] < flag[k][j])) {\n dp[k][j] = tmp[j];\n flag[k][j] = tmp_flag[j];\n }\n }\n }\n if (dp[k - 1][A[(i * 3) + 1]] + count[A[(i * 3) + 1]] >= 3) {\n for (j = 1; j <= N; j++) {\n tmp[j] = 0;\n tmp_flag[j] = 0;\n }\n if (count[A[(i * 3) + 1]] == 2) {\n if (dp[k - 1][A[(i * 3) + 1]] == 2 ||\n flag[k - 1][A[(i * 3) + 1]] == 0) {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || j == A[(i * 3) + 1]) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n } else {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || flag[k - 1][j] == 1) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n }\n if (A[i * 3] == A[(i * 3) + 1]) {\n tmp[A[(i * 3) + 2]]++;\n } else {\n tmp[A[i * 3]]++;\n }\n } else {\n tmp[A[i * 3]]++;\n tmp[A[(i * 3) + 2]]++;\n }\n for (j = 1; j <= N; j++) {\n if (tmp[j] > dp[k][j] ||\n (tmp[j] == dp[k][j] && tmp_flag[j] < flag[k][j])) {\n dp[k][j] = tmp[j];\n flag[k][j] = tmp_flag[j];\n }\n }\n }\n if (dp[k - 1][A[(i * 3) + 2]] + count[A[(i * 3) + 2]] >= 3) {\n for (j = 1; j <= N; j++) {\n tmp[j] = 0;\n tmp_flag[j] = 0;\n }\n if (count[A[(i * 3) + 2]] == 2) {\n if (dp[k - 1][A[(i * 3) + 2]] == 2 ||\n flag[k - 1][A[(i * 3) + 2]] == 0) {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || j == A[(i * 3) + 2]) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n } else {\n for (j = 1; j <= N; j++) {\n if (dp[k - 1][j] == 2 || flag[k - 1][j] == 1) {\n tmp[j] = dp[k - 1][j] - 1;\n } else {\n tmp[j] = dp[k - 1][j];\n }\n if (tmp[j] == 1) {\n tmp_flag[j] = 1;\n }\n }\n }\n if (A[i * 3] == A[(i * 3) + 2]) {\n tmp[A[(i * 3) + 1]]++;\n } else {\n tmp[A[i * 3]]++;\n }\n } else {\n tmp[A[i * 3]]++;\n tmp[A[(i * 3) + 1]]++;\n }\n for (j = 1; j <= N; j++) {\n if (tmp[j] > dp[k][j] ||\n (tmp[j] == dp[k][j] && tmp_flag[j] < flag[k][j])) {\n dp[k][j] = tmp[j];\n flag[k][j] = tmp_flag[j];\n }\n }\n }\n } else {\n if (dp[k - 1][A[i * 3]] < 2) {\n dp[k - 1][A[i * 3]]++;\n }\n if (dp[k - 1][A[(i * 3) + 1]] < 2) {\n dp[k - 1][A[(i * 3) + 1]]++;\n }\n if (dp[k - 1][A[(i * 3) + 2]] < 2) {\n dp[k - 1][A[(i * 3) + 2]]++;\n }\n }\n }\n count[A[i * 3]]--;\n count[A[(i * 3) + 1]]--;\n count[A[(i * 3) + 2]]--;\n }\n }\n\n if (dp[k][A[N * 3]] == 2) {\n printf(\"%d\\n\", k);\n } else {\n printf(\"%d\\n\", k - 1);\n }\n fflush(stdout);\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 S: Vec = buf.split_whitespace().map(|s| s.parse().unwrap()).collect();\n let N = S[0];\n\n let mut dp1 = vec![None; N + 1];\n let mut dp2 = vec![vec![None; N + 1]; N + 1];\n {\n let mut s = S[1..=2].to_vec();\n s.sort();\n let (a, b) = (s[0], s[1]);\n dp1[a].get_or_insert(0);\n dp1[b].get_or_insert(0);\n dp1[0].get_or_insert(0);\n dp2[a][b].get_or_insert(0);\n }\n\n let mut base = 0;\n let mut q = Vec::new();\n\n for i in 1..N {\n let mut s = S[3 * i..3 * (i + 1)].to_vec();\n s.sort();\n let (a, b, c) = (s[0], s[1], s[2]);\n\n if a == c {\n base += 1;\n continue;\n } else if a == b {\n for x in 1..=N {\n if x < a {\n if let Some(v) = dp2[x][a] {\n q.push((x, c, v + 1));\n }\n } else if x < c {\n if let Some(v) = dp2[a][x] {\n q.push((x, c, v + 1));\n }\n } else {\n if let Some(v) = dp2[a][x] {\n q.push((c, x, v + 1));\n }\n }\n }\n if let Some(v) = dp2[c][c] {\n q.push((a, a, v + 1));\n }\n } else if b == c {\n for x in 1..=N {\n if x < a {\n if let Some(v) = dp2[x][c] {\n q.push((x, a, v + 1));\n }\n } else if x < c {\n if let Some(v) = dp2[x][c] {\n q.push((a, x, v + 1));\n }\n } else {\n if let Some(v) = dp2[c][x] {\n q.push((a, x, v + 1));\n }\n }\n }\n if let Some(v) = dp2[a][a] {\n q.push((c, c, v + 1));\n }\n } else {\n if let Some(v) = dp2[a][a] {\n q.push((b, c, v + 1));\n }\n if let Some(v) = dp2[b][b] {\n q.push((a, c, v + 1));\n }\n if let Some(v) = dp2[c][c] {\n q.push((a, b, v + 1));\n }\n }\n\n q.push((a, b, dp1[0].unwrap()));\n q.push((a, c, dp1[0].unwrap()));\n q.push((b, c, dp1[0].unwrap()));\n\n for x in 1..=N {\n if let Some(v) = dp1[x] {\n for y in &[a, b, c] {\n if x < *y {\n q.push((x, *y, v));\n } else {\n q.push((*y, x, v));\n }\n }\n }\n }\n\n while let Some((a, b, v)) = q.pop() {\n if dp2[a][b].is_none() || dp2[a][b].unwrap() < v {\n let u = dp2[a][b].get_or_insert(v);\n *u = v;\n for i in &[0, a, b] {\n let u = dp1[*i].get_or_insert(v);\n *u = max(*u, v);\n }\n }\n }\n }\n\n let s = S[S.len() - 1];\n println!(\n \"{}\",\n match dp2[s][s] {\n Some(_v) => base + max(1 + dp2[s][s].unwrap_or(0), dp1[0].unwrap_or(0)),\n None => base + dp1[0].unwrap(),\n }\n );\n}", "difficulty": "medium"} {"problem_id": "0258", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Square1001 has seen an electric bulletin board displaying the integer 1.\nHe can perform the following operations A and B to change this value:

\n
    \n
  • Operation A: The displayed value is doubled.
  • \n
  • Operation B: The displayed value increases by K.
  • \n
\n

Square1001 needs to perform these operations N times in total.\nFind the minimum possible value displayed in the board after N operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N, K \\leq 10
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nK\n
\n
\n
\n
\n
\n

Output

Print the minimum possible value displayed in the board after N operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n3\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

The value will be minimized when the operations are performed in the following order: A, A, B, B.
\nIn this case, the value will change as follows: 124710.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\n10\n
\n
\n
\n
\n
\n

Sample Output 2

76\n
\n

The value will be minimized when the operations are performed in the following order: A, A, A, A, B, B, B, B, B, B.
\nIn this case, the value will change as follows: 124816263646566676.

\n

By the way, this contest is AtCoder Beginner Contest 076.

\n
\n
", "c_code": "int solution() {\n int n = 0;\n int k = 0;\n int x = 1;\n scanf(\"%d\\n%d\", &n, &k);\n for (int i = 0; i < n; i++) {\n if (2 * x <= x + k) {\n x *= 2;\n } else {\n x += k;\n }\n }\n printf(\"%d\\n\", x);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let n: i32 = s.trim().parse().ok().unwrap();\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).ok();\n let k: i32 = t.trim().parse().ok().unwrap();\n\n let mut ans = 1;\n for _i in 0..n {\n if ans < k {\n ans *= 2;\n } else {\n ans += k;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0259", "problem_description": "You are given a string $$$s$$$ consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode: res = 0for init = 0 to inf cur = init ok = true for i = 1 to |s| res = res + 1 if s[i] == '+' cur = cur + 1 else cur = cur - 1 if cur < 0 ok = false break if ok breakNote that the $$$inf$$$ denotes infinity, and the characters of the string are numbered from $$$1$$$ to $$$|s|$$$.You have to calculate the value of the $$$res$$$ after the process ends.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n char s[1000000];\n scanf(\"%s\", s);\n int n = strlen(s);\n int a[n];\n int b[n];\n long long int r = 0;\n long long int c = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == '+') {\n a[i] = 1;\n } else {\n a[i] = -1;\n }\n }\n long int sum = 0;\n long int j = -1;\n for (int i = 0; i < n; i++) {\n sum = sum + a[i];\n r++;\n if (sum == j) {\n\n j--;\n\n c = c + r;\n }\n }\n c = c + n;\n\n printf(\"%lld\\n\", c);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"\");\n let s: u64 = s.trim().parse().expect(\"\");\n for _i in 0..s {\n let mut st = String::new();\n io::stdin().read_line(&mut st).expect(\"\");\n let st = st.trim();\n let l = st.len();\n let mut rez = st.len() as u64;\n let mut cnt = 0;\n let mut mi = 0;\n let mut st = st.chars();\n for j in 0_usize..l {\n let a = st.next().unwrap();\n if a == '+' {\n cnt += 1;\n } else if a == '-' {\n cnt -= 1;\n }\n if cnt < mi {\n mi = cnt;\n rez += (j + 1) as u64;\n }\n }\n println!(\"{}\", rez);\n }\n}", "difficulty": "medium"} {"problem_id": "0260", "problem_description": "Alicia has an array, $$$a_1, a_2, \\ldots, a_n$$$, of non-negative integers. For each $$$1 \\leq i \\leq n$$$, she has found a non-negative integer $$$x_i = max(0, a_1, \\ldots, a_{i-1})$$$. Note that for $$$i=1$$$, $$$x_i = 0$$$.For example, if Alicia had the array $$$a = \\{0, 1, 2, 0, 3\\}$$$, then $$$x = \\{0, 0, 1, 2, 2\\}$$$.Then, she calculated an array, $$$b_1, b_2, \\ldots, b_n$$$: $$$b_i = a_i - x_i$$$.For example, if Alicia had the array $$$a = \\{0, 1, 2, 0, 3\\}$$$, $$$b = \\{0-0, 1-0, 2-1, 0-2, 3-2\\} = \\{0, 1, 1, -2, 1\\}$$$.Alicia gives you the values $$$b_1, b_2, \\ldots, b_n$$$ and asks you to restore the values $$$a_1, a_2, \\ldots, a_n$$$. Can you help her solve the problem?", "c_code": "int solution(void) {\n int n;\n int b[200007];\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &b[i]);\n }\n int maxi = 0;\n for (int i = 0; i < n; i++) {\n printf(\"%d \", maxi + b[i]);\n\n if (b[i] > 0) {\n maxi += b[i];\n }\n }\n puts(\"\");\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut input = String::new();\n\n let mut a: Vec = Vec::new();\n let mut b: Vec = Vec::new();\n\n io::stdin().read_line(&mut input)?;\n input.clear();\n io::stdin().read_line(&mut input)?;\n\n for i in input.split_ascii_whitespace() {\n b.push(i.parse().unwrap());\n }\n\n let mut max = 0;\n\n for i in b {\n let x = i + max;\n if x > max {\n max = x;\n }\n a.push(x);\n }\n\n for (i, item) in a.iter().enumerate() {\n if i == 0 {\n print!(\"{}\", item);\n } else {\n print!(\" {}\", item);\n }\n }\n println!();\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0261", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

\n

One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N.\nHe is interested in properties of the sequence a.

\n

For a nonempty contiguous subsequence a_l, ..., a_r (1 \\leq l \\leq r \\leq N) of the sequence a, its beauty is defined as a_l + ... + a_r. Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.)

\n

Find the maximum possible value for him.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 2 \\leq N \\leq 1000
  • \n
  • 1 \\leq a_i \\leq 10^9
  • \n
  • 1 \\leq K \\leq N(N+1)/2
  • \n
  • All numbers given in input are integers
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N K\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

\n
4 2\n2 5 2 5\n
\n
\n
\n
\n
\n

Sample Output 1

\n
12\n
\n

There are 10 nonempty contiguous subsequences of a. Let us enumerate them:

\n
    \n
  • contiguous subsequences starting from the first element: \\{2\\}, \\{2, 5\\}, \\{2, 5, 2\\}, \\{2, 5, 2, 5\\}
  • \n
  • contiguous subsequences starting from the second element: \\{5\\}, \\{5, 2\\}, \\{5, 2, 5\\}
  • \n
  • contiguous subsequences starting from the third element: \\{2\\}, \\{2, 5\\}
  • \n
  • contiguous subsequences starting from the fourth element: \\{5\\}
  • \n
\n

(Note that even if the elements of subsequences are equal, subsequences that have different starting indices are considered to be different.)

\n

The maximum possible bitwise AND of the beauties of two different contiguous subsequences is 12.\nThis can be achieved by choosing \\{5, 2, 5\\} (with beauty 12) and \\{2, 5, 2, 5\\} (with beauty 14).

\n
\n
\n
\n
\n
\n

Sample Input 2

\n
8 4\n9 1 8 2 7 5 6 4\n
\n
\n
\n
\n
\n

Sample Output 2

\n
32\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int k;\n int count = 0;\n scanf(\"%d%d\", &n, &k);\n long long a[n];\n long long b[n * (n + 1) / 2];\n long long ans = 0;\n long long max = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n long long sum = 0;\n for (int j = i; j < n; j++) {\n sum += a[j];\n b[count] = sum;\n if (b[count] > max) {\n max = b[count];\n }\n count++;\n }\n }\n\n int count2 = 0;\n int p = 0;\n for (p = 0; pow(2, p) < max; p++) {\n }\n p--;\n\n for (int i = p; i >= 0; i--) {\n count2 = 0;\n for (int j = 0; j < count; j++) {\n if (b[j] >= pow(2, i)) {\n count2++;\n }\n }\n if (count2 >= k) {\n ans += pow(2, i);\n for (int j = 0; j < count; j++) {\n if (b[j] < pow(2, i)) {\n b[j] = 0;\n } else {\n b[j] -= pow(2, i);\n }\n }\n } else {\n for (int j = 0; j < count; j++) {\n if (b[j] < pow(2, i)) {\n } else {\n b[j] -= pow(2, i);\n }\n }\n }\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let k: usize = s.next().unwrap().parse().unwrap();\n let a: Vec = vec![0]\n .into_iter()\n .chain(s.map(|a| a.parse::().unwrap()))\n .scan(0, |s, a| {\n *s += a;\n Some(*s)\n })\n .collect();\n let mut a: Vec = (0..n)\n .flat_map(|i| (i + 1..n + 1).map(move |j| (i, j)))\n .map(|(i, j)| a[j] - a[i])\n .collect();\n a.sort_by_key(|a| std::u64::MAX - a);\n let msb = 63 - a[0].leading_zeros();\n let mut ans: u64 = 0;\n let mut g: std::collections::BTreeSet<_> = (0..a.len()).collect();\n for bit in (0..msb + 1).rev() {\n let index: std::collections::BTreeSet<_> = g\n .iter()\n .filter(|&&i| a[i] & (1 << bit) != 0)\n .copied()\n .collect();\n if index.len() >= k {\n g = g.intersection(&index).cloned().collect();\n ans |= 1 << bit;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0262", "problem_description": "Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.The pasture is a rectangle consisting of R × C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.", "c_code": "int solution() {\n char f[500][502] = {0};\n int r;\n int c;\n scanf(\"%d%d%*c\", &r, &c);\n for (int i = 0; i < r; ++i) {\n fgets(f[i], sizeof f[i], stdin);\n f[i][c] = 0;\n for (int j = 0; j < c; ++j) {\n switch (f[i][j]) {\n case '.':\n f[i][j] = 'D';\n break;\n case 'S':\n if ((i > 0 && f[i - 1][j] == 'W') || (j > 0 && f[i][j - 1] == 'W')) {\n printf(\"No\\n\");\n return 0;\n }\n break;\n case 'W':\n if ((i > 0 && f[i - 1][j] == 'S') || (j > 0 && f[i][j - 1] == 'S')) {\n printf(\"No\\n\");\n return 0;\n }\n break;\n default:\n break;\n }\n }\n }\n printf(\"Yes\\n\");\n for (int i = 0; i < r; ++i) {\n puts(f[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::{self, prelude::*};\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let r: usize = it.next().unwrap().parse().unwrap();\n let c: usize = it.next().unwrap().parse().unwrap();\n\n let mut grid = Vec::with_capacity(r * c);\n\n for l in it.take(r) {\n for c in l.chars() {\n let x = match c {\n 'S' => Cell::Sheep,\n 'W' => Cell::Wolf,\n '.' => Cell::Empty,\n _ => panic!(\"Invalid input\"),\n };\n grid.push(x);\n }\n }\n\n let no = || {\n println!(\"No\");\n std::process::exit(0);\n };\n\n for (i, cell) in grid.iter().enumerate() {\n if let Cell::Sheep = *cell {\n let right = i + 1;\n match grid.get(right) {\n Some(Cell::Wolf) if right % c != 0 => no(),\n _ => (),\n }\n\n if c <= i {\n let up = i - c;\n if let Some(Cell::Wolf) = grid.get(up) {\n no()\n }\n }\n\n if 0 < i {\n let left = i - 1;\n match grid.get(left) {\n Some(Cell::Wolf) if left % c != c - 1 => no(),\n _ => (),\n }\n }\n\n let down = i + c;\n if let Some(Cell::Wolf) = grid.get(down) {\n no()\n }\n }\n }\n\n println!(\"YES\");\n\n let mut grid_str = String::with_capacity(r * (c + 1));\n\n for r in 0..r {\n for ci in 0..c {\n let i = r * c + ci;\n let x = match grid[i] {\n Cell::Sheep => 'S',\n Cell::Wolf => 'W',\n Cell::Empty => 'D',\n };\n grid_str.push(x);\n }\n grid_str.push('\\n');\n }\n println!(\"{}\", grid_str);\n}", "difficulty": "easy"} {"problem_id": "0263", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

\n

The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.

\n

You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.

\n

You now have to take Q practical tests of your ability as a magical girl.

\n

The i-th test will be conducted as follows:

\n
    \n
  • \n

    Initially, a piece is placed on the square where the integer L_i is written.

    \n
  • \n
  • \n

    Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.

    \n
  • \n
  • \n

    Here, it is guaranteed that R_i-L_i is a multiple of D.

    \n
  • \n
\n

For each test, find the sum of magic points consumed during that test.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H,W \\leq 300
  • \n
  • 1 \\leq D \\leq H×W
  • \n
  • 1 \\leq A_{i,j} \\leq H×W
  • \n
  • A_{i,j} \\neq A_{x,y} ((i,j) \\neq (x,y))
  • \n
  • 1 \\leq Q \\leq 10^5
  • \n
  • 1 \\leq L_i \\leq R_i \\leq H×W
  • \n
  • (R_i-L_i) is a multiple of D.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W D\nA_{1,1} A_{1,2} ... A_{1,W}\n:\nA_{H,1} A_{H,2} ... A_{H,W}\nQ\nL_1 R_1\n:\nL_Q R_Q\n
\n
\n
\n
\n
\n

Output

For each test, print the sum of magic points consumed during that test.

\n

Output should be in the order the tests are conducted.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3 2\n1 4 3\n2 5 7\n8 9 6\n1\n4 8\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n
    \n
  • \n

    4 is written in Square (1,2).

    \n
  • \n
  • \n

    6 is written in Square (3,3).

    \n
  • \n
  • \n

    8 is written in Square (3,1).

    \n
  • \n
\n

Thus, the sum of magic points consumed during the first test is (|3-1|+|3-2|)+(|3-3|+|1-3|)=5.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 2 3\n3 7\n1 4\n5 2\n6 8\n2\n2 2\n2 2\n
\n
\n
\n
\n
\n

Sample Output 2

0\n0\n
\n

Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 5 4\n13 25 7 15 17\n16 22 20 2 9\n14 11 12 1 19\n10 6 23 8 18\n3 21 5 24 4\n3\n13 13\n2 10\n13 13\n
\n
\n
\n
\n
\n

Sample Output 3

0\n5\n0\n
\n
\n
", "c_code": "int solution(void) {\n\n long h;\n long w;\n long d;\n long q;\n scanf(\"%ld %ld %ld\", &h, &w, &d);\n long a[h][w];\n for (long i = 0; i < h; i++) {\n for (long j = 0; j < w; j++) {\n scanf(\"%ld\", &a[i][j]);\n }\n }\n scanf(\"%ld\", &q);\n long l[q];\n long r[q];\n for (int i = 0; i < q; i++) {\n scanf(\"%ld %ld\", &l[i], &r[i]);\n }\n long x[(h * w) + 1];\n long y[(h * w) + 1];\n for (long i = 0; i < h; i++) {\n for (long j = 0; j < w; j++) {\n x[a[i][j]] = i;\n y[a[i][j]] = j;\n }\n }\n long magic[(h * w) + 1];\n for (long i = 1; i <= d; i++) {\n magic[i] = 0;\n }\n for (long i = d + 1; i <= h * w; i++) {\n magic[i] = magic[i - d] + labs(x[i] - x[i - d]) + labs(y[i] - y[i - d]);\n }\n for (long i = 0; i < q; i++) {\n printf(\"%ld\\n\", magic[r[i]] - magic[l[i]]);\n }\n\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 h: usize = iter.next().unwrap().parse().unwrap();\n let w: usize = iter.next().unwrap().parse().unwrap();\n let d: usize = iter.next().unwrap().parse().unwrap();\n let mut pos = vec![(0, 0); h * w].into_boxed_slice();\n for i in 0..h {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n for j in 0..w {\n let num = iter.next().unwrap().parse::().unwrap();\n pos[num - 1] = (j as i64, i as i64);\n }\n }\n let mut costs = vec![0; h * w].into_boxed_slice();\n for i in d..(h * w) {\n costs[i] = costs[i - d] + (pos[i].0 - pos[i - d].0).abs() + (pos[i].1 - pos[i - d].1).abs();\n }\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let q = buf.trim().parse::().unwrap();\n for _ in 0..q {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n let l = iter.next().unwrap().parse::().unwrap();\n let r = iter.next().unwrap().parse::().unwrap();\n println!(\"{}\", costs[r - 1] - costs[l - 1]);\n }\n}", "difficulty": "easy"} {"problem_id": "0264", "problem_description": "Screen resolution of Polycarp's monitor is $$$a \\times b$$$ pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates $$$(x, y)$$$ ($$$0 \\le x < a, 0 \\le y < b$$$). You can consider columns of pixels to be numbered from $$$0$$$ to $$$a-1$$$, and rows — from $$$0$$$ to $$$b-1$$$.Polycarp wants to open a rectangular window of maximal size, which doesn't contain the dead pixel. The boundaries of the window should be parallel to the sides of the screen.Print the maximal area (in pixels) of a window that doesn't contain the dead pixel inside itself.", "c_code": "int solution() {\n int tc;\n int area1 = 0;\n int area2 = 0;\n int i;\n scanf(\"%d\", &tc);\n int input[tc][4];\n for (i = 0; i < tc; i++) {\n scanf(\"%d\", &input[i][0]);\n scanf(\"%d\", &input[i][1]);\n scanf(\"%d\", &input[i][2]);\n scanf(\"%d\", &input[i][3]);\n }\n for (i = 0; i < tc; i++) {\n area1 = input[i][0] * (input[i][1] - input[i][3] - 1);\n area2 = input[i][0] * input[i][3];\n if (area2 > area1) {\n area1 = area2;\n }\n area2 = (input[i][0] - input[i][2] - 1) * input[i][1];\n if (area2 > area1) {\n area1 = area2;\n }\n area2 = input[i][2] * input[i][1];\n if (area2 > area1) {\n area1 = area2;\n }\n input[i][0] = area1;\n }\n for (i = 0; i < tc; i++) {\n printf(\"%d\\n\", input[i][0]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read line\");\n buf.trim().parse::().unwrap()\n };\n for _ in 0..t {\n let (a, b, x, y) = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read\");\n let nums: Vec = buf\n .trim()\n .split_ascii_whitespace()\n .map(|c| c.parse().unwrap())\n .collect();\n (nums[0], nums[1], nums[2], nums[3])\n };\n println!(\"{}\", max(a * max(y, b - y - 1), max(x, a - x - 1) * b));\n }\n}", "difficulty": "medium"} {"problem_id": "0265", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

A shop sells N kinds of fruits, Fruit 1, \\ldots, N, at prices of p_1, \\ldots, p_N yen per item, respectively. (Yen is the currency of Japan.)

\n

Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq N \\leq 1000
  • \n
  • 1 \\leq p_i \\leq 1000
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\np_1 p_2 \\ldots p_N\n
\n
\n
\n
\n
\n

Output

Print an integer representing the minimum possible total price of fruits.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n50 100 80 120 80\n
\n
\n
\n
\n
\n

Sample Output 1

210\n
\n

This shop sells Fruit 1, 2, 3, 4, and 5 for 50 yen, 100 yen, 80 yen, 120 yen, and 80 yen, respectively.

\n

The minimum total price for three kinds of fruits is 50 + 80 + 80 = 210 yen when choosing Fruit 1, 3, and 5.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 1\n1000\n
\n
\n
\n
\n
\n

Sample Output 2

1000\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n int k = 0;\n\n int p[1005] = {0};\n\n scanf(\"%d\", &n);\n scanf(\"%d\", &k);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &p[i]);\n }\n\n int min = 10000;\n int result = 0;\n int num = 0;\n for (int i = 0; i < k; i++) {\n min = 10000;\n for (int j = 0; j < n; j++) {\n if (p[j] < min && p[j] > 0) {\n min = p[j];\n num = j;\n }\n }\n p[num] = 0;\n result += min;\n }\n\n printf(\"%d\", result);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n let mut itr = is.split_whitespace().map(|e| e.parse().unwrap());\n let _n: usize = itr.next().unwrap();\n let k: usize = itr.next().unwrap();\n is.clear();\n stdin().read_line(&mut is).ok();\n let mut a = is\n .split_whitespace()\n .map(|e| e.parse::().unwrap())\n .collect::>();\n a.sort();\n\n println!(\"{}\", a.iter().take(k).sum::());\n}", "difficulty": "hard"} {"problem_id": "0266", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Joisino is about to compete in the final round of a certain programming competition.\nIn this contest, there are N problems, numbered 1 through N.\nJoisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).

\n

Also, there are M kinds of drinks offered to the contestants, numbered 1 through M.\nIf Joisino takes drink i(1≦i≦M), her brain will be stimulated and the time it takes for her to solve problem P_i will become X_i seconds.\nIt does not affect the time to solve the other problems.

\n

A contestant is allowed to take exactly one of the drinks before the start of the contest.\nFor each drink, Joisino wants to know how many seconds it takes her to solve all the problems if she takes that drink.\nHere, assume that the time it takes her to solve all the problems is equal to the sum of the time it takes for her to solve individual problems.\nYour task is to write a program to calculate it instead of her.

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1≦N≦100
  • \n
  • 1≦T_i≦10^5
  • \n
  • 1≦M≦100
  • \n
  • 1≦P_i≦N
  • \n
  • 1≦X_i≦10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nT_1 T_2 ... T_N\nM\nP_1 X_1\nP_2 X_2\n:\nP_M X_M\n
\n
\n
\n
\n
\n

Output

For each drink, calculate how many seconds it takes Joisino to solve all the problems if she takes that drink, and print the results, one per line.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2 1 4\n2\n1 1\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

6\n9\n
\n

If Joisino takes drink 1, the time it takes her to solve each problem will be 1, 1 and 4 seconds, respectively, totaling 6 seconds.

\n

If Joisino takes drink 2, the time it takes her to solve each problem will be 2, 3 and 4 seconds, respectively, totaling 9 seconds.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n7 2 3 8 5\n3\n4 2\n1 7\n4 13\n
\n
\n
\n
\n
\n

Sample Output 2

19\n25\n30\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n int m = 0;\n int t[110];\n int p = 0;\n int x = 0;\n int sum = 0;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &t[i]);\n sum += t[i];\n }\n\n scanf(\"%d\", &m);\n for (int i = 0; i < m; i++) {\n scanf(\"%d%d\", &p, &x);\n printf(\"%d\\n\", sum - t[p - 1] + x);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let _n: usize = s.trim().parse().unwrap();\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let t: Vec = s.split_whitespace().map(|e| e.parse().unwrap()).collect();\n let tot: usize = t.iter().sum();\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let m: usize = s.trim().parse().unwrap();\n for _ in 0..m {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let mut itr = s.split_whitespace().map(|e| e.parse().unwrap());\n let p: usize = itr.next().unwrap();\n let x: usize = itr.next().unwrap();\n\n println!(\"{}\", tot + x - t[p - 1]);\n }\n}", "difficulty": "medium"} {"problem_id": "0267", "problem_description": "There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.", "c_code": "int solution() {\n\n int n = 0;\n int t = 0;\n char s[200001];\n int x = 0;\n int f = 0;\n scanf(\"%d %s\", &n, s);\n\n int k = -1;\n int max = 2000000000;\n int i = 0;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &x);\n if (s[i] == 'R') {\n t = 1;\n f = x;\n } else {\n if (t == 1) {\n k = (x - f) / 2;\n if (k < max) {\n max = k;\n }\n t = 0;\n }\n }\n }\n if (max == 2000000000) {\n printf(\"-1\");\n } else {\n printf(\"%d\", max);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let cin = stdin();\n\n let mut line = String::new();\n cin.read_line(&mut line).unwrap();\n\n line.clear();\n cin.read_line(&mut line).unwrap();\n let dir = line.trim().as_bytes();\n\n let mut ans = i64::max_value();\n let mut last_r = -1;\n\n for (i, result) in cin.lock().split(b' ').enumerate() {\n let x = i64::from_str(str::from_utf8(result.unwrap().as_slice()).unwrap().trim()).unwrap();\n if dir[i] as char == 'L' && last_r != -1 {\n ans = min(ans, x - last_r);\n }\n if dir[i] as char == 'R' {\n last_r = x;\n }\n }\n\n println!(\"{}\", if ans == i64::max_value() { -1 } else { ans / 2 });\n}", "difficulty": "medium"} {"problem_id": "0268", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We have a sandglass that runs for X seconds. The sand drops from the upper bulb at a rate of 1 gram per second. That is, the upper bulb initially contains X grams of sand.

\n

How many grams of sand will the upper bulb contains after t seconds?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤X≤10^9
  • \n
  • 1≤t≤10^9
  • \n
  • X and t are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
X t\n
\n
\n
\n
\n
\n

Output

Print the number of sand in the upper bulb after t second.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

100 17\n
\n
\n
\n
\n
\n

Sample Output 1

83\n
\n

17 out of the initial 100 grams of sand will be consumed, resulting in 83 grams.

\n
\n
\n
\n
\n
\n

Sample Input 2

48 58\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

All 48 grams of sand will be gone, resulting in 0 grams.

\n
\n
\n
\n
\n
\n

Sample Input 3

1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n int a[4];\n scanf(\"%d%d\", &a[0], &a[1]);\n if (a[0] < a[1]) {\n printf(\"0\");\n } else {\n printf(\"%d\", a[0] - a[1]);\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let v: Vec = s\n .split_whitespace()\n .map(|e| e.parse().ok().unwrap())\n .collect();\n let X = v[0];\n let t = v[1];\n println!(\"{}\", std::cmp::max(X - t, 0));\n}", "difficulty": "medium"} {"problem_id": "0269", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Smeke has decided to participate in AtCoder Beginner Contest (ABC) if his current rating is less than 1200, and participate in AtCoder Regular Contest (ARC) otherwise.

\n

You are given Smeke's current rating, x. Print ABC if Smeke will participate in ABC, and print ARC otherwise.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ x ≦ 3{,}000
  • \n
  • x is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
x\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1000\n
\n
\n
\n
\n
\n

Sample Output 1

ABC\n
\n

Smeke's current rating is less than 1200, thus the output should be ABC.

\n
\n
\n
\n
\n
\n

Sample Input 2

2000\n
\n
\n
\n
\n
\n

Sample Output 2

ARC\n
\n

Smeke's current rating is not less than 1200, thus the output should be ARC.

\n
\n
", "c_code": "int solution(void) {\n int num = 0;\n\n scanf(\"%d\", &num);\n if (num >= 1200) {\n printf(\"ARC\\n\");\n } else {\n printf(\"ABC\\n\");\n }\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n let x: u32 = vec[0].parse().unwrap_or(0);\n if x < 1200 {\n println!(\"ABC\");\n } else {\n println!(\"ARC\");\n }\n}", "difficulty": "easy"} {"problem_id": "0270", "problem_description": "\n\n\n

Fibonacci Number

\n\n

\n Write a program which prints $n$-th fibonacci number for a given integer $n$. The $n$-th fibonacci number is defined by the following recursive formula:\n

\n\n\\begin{equation*}\nfib(n)= \\left \\{\n\\begin{array}{ll}\n1  & (n = 0) \\\\\n1  & (n = 1) \\\\\nfib(n - 1) + fib(n - 2) & \\\\\n\\end{array}\n\\right.\n\\end{equation*}\n\n

Input

\n\n

\n An integer $n$ is given.\n

\n\n

output

\n\n

\n Print the $n$-th fibonacci number in a line.\n

\n\n

Constraints

\n\n
    \n
  • $0 \\leq n \\leq 44$
  • \n
\n\n\n

Sample Input 1

\n
\n3\n
\n\n

Sample Output 1

\n
\n3\n
", "c_code": "int solution(void) {\n int a = 0;\n scanf(\"%d\", &a);\n if (a == 0 || a == 1) {\n printf(\"1\\n\");\n return 0;\n }\n int i = 0;\n int m = 1;\n int n = 1;\n int ch = 0;\n for (i = 0; i < a - 1; i++) {\n n += m;\n ch = m;\n m = n;\n n = ch;\n }\n printf(\"%d\\n\", m);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let n: usize = buf.trim().parse().unwrap();\n\n let mut fib = vec![1, 1];\n\n while fib.len() < n + 1 {\n let f1 = fib[fib.len() - 1];\n let f2 = fib[fib.len() - 2];\n fib.push(f1 + f2);\n }\n println!(\"{}\", fib[n]);\n}", "difficulty": "hard"} {"problem_id": "0271", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)\nThe state of each piece is represented by a string S of length N.\nIf S_i=B, the i-th piece from the left is showing black;\nIf S_i=W, the i-th piece from the left is showing white.

\n

Consider performing the following operation:

\n
    \n
  • Choose i (1 \\leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.
  • \n
\n

Find the maximum possible number of times this operation can be performed.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 2\\times 10^5
  • \n
  • S_i=B or W
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number of times the operation can be performed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

BBW\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The operation can be performed twice, as follows:

\n
    \n
  • Flip the second and third pieces from the left.
  • \n
  • Flip the first and second pieces from the left.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

BWBWBW\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
", "c_code": "int solution(void) {\n char s[200000 + 1] = {'\\0'};\n long cnt_turn = 0;\n long num_b = 0;\n char *sp = &s[0];\n\n scanf(\"%s\", sp);\n\n while (*sp != '\\0') {\n if (*sp == 'B') {\n num_b++;\n }\n if (*sp == 'W') {\n cnt_turn += num_b;\n }\n sp++;\n }\n\n printf(\"%ld\\n\", cnt_turn);\n\n return 0;\n}", "rust_code": "fn solution() {\n let s = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s.trim_end().to_owned()\n };\n println!(\n \"{}\",\n s.chars()\n .skip_while(|&c| c == 'W')\n .enumerate()\n .fold((0, 0), |(n, mut state), (i, c)| if c == 'B' {\n (n, state)\n } else {\n state += i - n;\n (n + 1, state)\n })\n .1\n )\n}", "difficulty": "easy"} {"problem_id": "0272", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \\ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \\ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

\n
\n
\n
\n
\n

Notes

    \n
  • A binary tree is a rooted tree such that each vertex has at most two direct children.
  • \n
  • A leaf in a binary tree is a vertex with zero children.
  • \n
  • The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)
  • \n
  • The depth of a binary tree is the maximum depth of a vertex in the tree.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq N \\leq 10^5
  • \n
  • 0 \\leq A_i \\leq 10^{8} (0 \\leq i \\leq N)
  • \n
  • A_N \\geq 1
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_0 A_1 A_2 \\cdots A_N\n
\n
\n
\n
\n
\n

Output

Print the answer as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n0 1 1 2\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.\n

\n\"0d8d99d13df036f23b0c9fcec52b842b.png\"\n

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n0 0 1 0 2\n
\n
\n
\n
\n
\n

Sample Output 2

10\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2\n0 3 1\n
\n
\n
\n
\n
\n

Sample Output 3

-1\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1\n1 1\n
\n
\n
\n
\n
\n

Sample Output 4

-1\n
\n
\n
\n
\n
\n
\n

Sample Input 5

10\n0 0 1 1 2 3 5 8 13 21 34\n
\n
\n
\n
\n
\n

Sample Output 5

264\n
\n
\n
", "c_code": "int solution() {\n long long n;\n long long i;\n scanf(\"%lld\", &n);\n long long a[n];\n long long sum = 0;\n for (i = 0; i <= n; i++) {\n scanf(\"%lld\", &a[i]);\n sum = sum + a[i];\n }\n long long int eda = 1;\n long long int result = 1;\n if (n == 0 && a[0] == 1) {\n printf(\"1\\n\");\n return 0;\n }\n if (a[0] != 0) {\n printf(\"-1\\n\");\n return 0;\n }\n for (i = 1; i <= n; i++) {\n if (eda * 2 < sum) {\n if (eda * 2 < a[i]) {\n printf(\"-1\\n\");\n return 0;\n }\n eda = eda * 2 - a[i];\n } else {\n eda = sum - a[i];\n }\n result = result + eda + a[i];\n sum = sum - a[i];\n }\n printf(\"%lld\\n\", result);\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 a: Vec = (0..n + 1)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let mut s = vec![0; n + 2];\n for i in (0..n + 1).rev() {\n s[i] = s[i + 1] + a[i];\n }\n\n let mut ans = 0;\n let mut now = 1;\n for i in 0..n + 1 {\n if a[i] > now {\n println!(\"-1\");\n return;\n }\n ans += now;\n\n let eda_max = (now - a[i]) << 1;\n\n let eda_min = s[i] - a[i];\n now = std::cmp::min(eda_max, eda_min);\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0273", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given an integer N. Consider an infinite N-ary tree as shown below:

\n
\n\n

Figure: an infinite N-ary tree for the case N = 3

\n
\n

As shown in the figure, each vertex is indexed with a unique positive integer, and for every positive integer there is a vertex indexed with it. The root of the tree has the index 1. For the remaining vertices, vertices in the upper row have smaller indices than those in the lower row. Among the vertices in the same row, a vertex that is more to the left has a smaller index.

\n

Regarding this tree, process Q queries. The i-th query is as follows:

\n
    \n
  • Find the index of the lowest common ancestor (see Notes) of Vertex v_i and w_i.
  • \n
\n
\n
\n
\n
\n

Notes

    \n
  • In a rooted tree, the lowest common ancestor (LCA) of Vertex v and w is the farthest vertex from the root that is an ancestor of both Vertex v and w. Here, a vertex is considered to be an ancestor of itself. For example, in the tree shown in Problem Statement, the LCA of Vertex 5 and 7 is Vertex 2, the LCA of Vertex 8 and 11 is Vertex 1, and the LCA of Vertex 3 and 9 is Vertex 3.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 10^9
  • \n
  • 1 ≤ Q ≤ 10^5
  • \n
  • 1 ≤ v_i < w_i ≤ 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N Q\nv_1 w_1\n:\nv_Q w_Q\n
\n
\n
\n
\n
\n

Output

Print Q lines. The i-th line (1 ≤ i ≤ Q) must contain the index of the lowest common ancestor of Vertex v_i and w_i.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n5 7\n8 11\n3 9\n
\n
\n
\n
\n
\n

Sample Output 1

2\n1\n3\n
\n

The queries in this case correspond to the examples shown in Notes.

\n
\n
\n
\n
\n
\n

Sample Input 2

100000 2\n1 2\n3 4\n
\n
\n
\n
\n
\n

Sample Output 2

1\n1\n
\n
\n
", "c_code": "int solution() {\n int n;\n int q;\n scanf(\"%d %d\", &n, &q);\n while (q--) {\n int t1;\n int t2;\n int i;\n scanf(\"%d %d\", &t1, &t2);\n if (n == 1) {\n printf(\"%d\\n\", t1 > t2 ? t2 : t1);\n continue;\n }\n while (t1 != t2) {\n if (t1 > t2) {\n t1 = (t1 - 2) / n + 1;\n } else {\n t2 = (t2 - 2) / n + 1;\n }\n }\n printf(\"%d\\n\", t1);\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: u64 = itr.next().unwrap().parse().unwrap();\n let q: usize = itr.next().unwrap().parse().unwrap();\n\n let mut out = Vec::new();\n for _ in 0..q {\n let mut v = itr.next().unwrap().parse::().unwrap() - 1;\n let mut w = itr.next().unwrap().parse::().unwrap() - 1;\n if n == 1 {\n writeln!(out, \"{}\", v + 1).ok();\n continue;\n }\n while v != w {\n while v > w {\n v = (v - 1) / n;\n }\n while w > v {\n w = (w - 1) / n;\n }\n }\n writeln!(out, \"{}\", v + 1).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "medium"} {"problem_id": "0274", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

\n
\n
\n
\n
\n

Constraints

    \n
  • C is a lowercase English letter that is not z.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
C\n
\n
\n
\n
\n
\n

Output

Print the letter that follows C in alphabetical order.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

a\n
\n
\n
\n
\n
\n

Sample Output 1

b\n
\n

a is followed by b.

\n
\n
\n
\n
\n
\n

Sample Input 2

y\n
\n
\n
\n
\n
\n

Sample Output 2

z\n
\n

y is followed by z.

\n
\n
", "c_code": "int solution() {\n char n1 = 0;\n scanf(\"%c\", &n1);\n if (n1 >= 'a' && n1 <= 'y') {\n\n printf(\"%c\", n1 + 1);\n }\n}", "rust_code": "fn solution() {\n let mut x = String::new();\n io::stdin().read_line(&mut x).unwrap();\n let x = x.trim().chars().next().unwrap();\n println!(\"{}\", (x as u8 + 1) as char);\n}", "difficulty": "hard"} {"problem_id": "0275", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

N persons are standing in a row. The height of the i-th person from the front is A_i.

\n

We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:

\n

Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.

\n

Find the minimum total height of the stools needed to meet this goal.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2\\times 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 \\ldots A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum total height of the stools needed to meet the goal.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n2 1 5 4 3\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

If the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively, their heights will be 2, 2, 5, 5, and 5, satisfying the condition.

\n

We cannot meet the goal with a smaller total height of the stools.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n3 3 3 3 3\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Giving a stool of height 0 to everyone will work.

\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n int a = 0;\n int amin = 0;\n long int count = 0;\n\n scanf(\"%d\", &n);\n while (n--) {\n scanf(\"%d\", &a);\n if (a > amin) {\n amin = a;\n } else {\n count += amin - a;\n }\n }\n printf(\"%ld\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut n: String = String::new();\n\n io::stdin().read_line(&mut n).expect(\"Required Input\");\n\n let _n: i128 = n.trim().parse().unwrap();\n\n let mut stol_height: i128 = 0;\n let mut input: String = String::new();\n let mut prev_height: i128 = 0;\n\n io::stdin().read_line(&mut input).expect(\"Input required\");\n\n let values: Vec = input\n .split_whitespace()\n .map(|x| x.parse::())\n .collect::, _>>()\n .unwrap();\n\n for i in values.iter() {\n if prev_height <= *i {\n prev_height = max(prev_height, *i);\n continue;\n }\n\n stol_height += prev_height - *i;\n }\n\n println!(\"{}\", stol_height);\n}", "difficulty": "hard"} {"problem_id": "0276", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Katsusando loves omelette rice.

\n

Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone.

\n

To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not.

\n

The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food.

\n

Find the number of the foods liked by all the N people.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N, M \\leq 30
  • \n
  • 1 \\leq K_i \\leq M
  • \n
  • 1 \\leq A_{ij} \\leq M
  • \n
  • For each i (1 \\leq i \\leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct.
  • \n
\n
\n
\n
\n
\n

Constraints

Input is given from Standard Input in the following format:

\n
N M\nK_1 A_{11} A_{12} ... A_{1K_1}\nK_2 A_{21} A_{22} ... A_{2K_2}\n:\nK_N A_{N1} A_{N2} ... A_{NK_N}\n
\n
\n
\n
\n
\n

Output

Print the number of the foods liked by all the N people.

\n
\n
\n
\n
\n
\n

Sample Input 1

3 4\n2 1 3\n3 1 2 3\n2 3 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

As only the third food is liked by all the three people, 1 should be printed.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 5\n4 2 3 4 5\n4 1 3 4 5\n4 1 2 4 5\n4 1 2 3 5\n4 1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Katsusando's hypothesis turned out to be wrong.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 30\n3 5 10 30\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int M = 0;\n int cntM[31] = {0};\n int num = 0;\n int temp = 0;\n int suki = 0;\n\n scanf(\"%d %d\", &N, &M);\n\n for (int cnt = 0; cnt < N; cnt++) {\n scanf(\"%d\", &num);\n for (int cnt_num = 0; cnt_num < num; cnt_num++) {\n scanf(\"%d\", &temp);\n cntM[temp]++;\n }\n }\n\n for (int cnt = 1; cnt <= M; cnt++) {\n if (cntM[cnt] == N) {\n suki++;\n }\n }\n printf(\"%d\\n\", suki);\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.lines();\n iter.next();\n\n let mut hm = HashMap::new();\n let mut count = 0;\n\n for l in iter {\n count += 1;\n let mut es = l.split_whitespace();\n es.next();\n for e in es {\n let mut val = 1;\n if let Some(&v) = hm.get(e) {\n val += v;\n }\n hm.insert(e, val);\n }\n }\n\n println!(\"{}\", hm.values().filter(|&&v| v == count).count());\n}", "difficulty": "hard"} {"problem_id": "0277", "problem_description": "Since the giant heads have appeared in the sky all humanity is in danger, so all Ricks and Mortys from all parallel universes are gathering in groups to find a solution to get rid of them. There are n parallel universes participating in this event (n Ricks and n Mortys). I. e. each of n universes has one Rick and one Morty. They're gathering in m groups. Each person can be in many groups and a group can contain an arbitrary number of members.Ricks and Mortys have registered online in these groups. So, a person can have joined a group more than once (developer of this website hadn't considered this possibility). Summer from universe #1 knows that in each parallel universe (including hers) exactly one of Rick and Morty from that universe is a traitor and is loyal, but no one knows which one. She knows that we are doomed if there's a group such that every member in that group is a traitor (they will plan and destroy the world). Summer knows that if there's a possibility that world ends (there's a group where all members are traitors) she should immediately cancel this event. So she wants to know if she should cancel the event. You have to tell her yes if and only if there's at least one scenario (among all 2n possible scenarios, 2 possible scenarios for who a traitor in each universe) such that in that scenario the world will end.", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int t[m];\n for (int i = 0; i < m; i++) {\n t[i] = 0;\n }\n for (int i = 0; i < m; i++) {\n int c;\n scanf(\"%d\", &c);\n int s[c];\n for (int j = 0; j < c; j++) {\n scanf(\"%d\", &s[j]);\n }\n for (int j = 0; j < c; j++) {\n for (int k = j + 1; k < c; k++) {\n if (s[j] == -s[k]) {\n t[i] = 1;\n }\n }\n }\n }\n for (int i = 0; i < m; i++) {\n if (t[i] != 1) {\n printf(\"YES\");\n return 0;\n }\n }\n printf(\"NO\");\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::{stdin, BufRead};\n\n let io = stdin();\n let mut io = io.lock().lines().map(|x| x.unwrap());\n\n let nm = io.next().unwrap();\n let mut nm = nm.split_whitespace();\n let n = nm.next().unwrap().parse::().unwrap();\n let m = nm.next().unwrap().parse::().unwrap();\n for _ in 0..m {\n let mut chk = vec![0; n + 1];\n let line = io.next().unwrap();\n let mut line = line.split_whitespace();\n let k = line.next().unwrap().parse::().unwrap();\n let mut possible_ruin = true;\n for _ in 0..k {\n let a = line.next().unwrap().parse::().unwrap();\n let b = a.unsigned_abs();\n chk[b] |= if a > 0 { 1 } else { 2 };\n if chk[b] == 3 {\n possible_ruin = false;\n break;\n }\n }\n if possible_ruin {\n println!(\"YES\");\n return;\n }\n }\n println!(\"NO\");\n}", "difficulty": "hard"} {"problem_id": "0278", "problem_description": "Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task.Recently, out of blue Captain Flint has been interested in math and even defined a new class of integers. Let's define a positive integer $$$x$$$ as nearly prime if it can be represented as $$$p \\cdot q$$$, where $$$1 < p < q$$$ and $$$p$$$ and $$$q$$$ are prime numbers. For example, integers $$$6$$$ and $$$10$$$ are nearly primes (since $$$2 \\cdot 3 = 6$$$ and $$$2 \\cdot 5 = 10$$$), but integers $$$1$$$, $$$3$$$, $$$4$$$, $$$16$$$, $$$17$$$ or $$$44$$$ are not.Captain Flint guessed an integer $$$n$$$ and asked you: can you represent it as the sum of $$$4$$$ different positive integers where at least $$$3$$$ of them should be nearly prime.Uncle Bogdan easily solved the task and joined the crew. Can you do the same?", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t-- > 0) {\n int n = 0;\n scanf(\"%d\", &n);\n if (n >= 31) {\n if (n == 36) {\n printf(\"YES\\n6 10 15 5\\n\");\n } else if (n == 40) {\n printf(\"YES\\n6 10 15 9\\n\");\n } else if (n == 44) {\n printf(\"YES\\n6 10 21 7\\n\");\n } else {\n printf(\"YES\\n6 10 14 %d\\n\", n - 30);\n }\n } else {\n printf(\"NO\\n\");\n }\n }\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 if n < 31 {\n println!(\"NO\");\n } else if n == 36 || n == 40 || n == 44 {\n println!(\"YES\");\n println!(\"6 10 15 {}\", n - 31);\n } else {\n println!(\"YES\");\n println!(\"6 10 14 {}\", n - 30);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0279", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

AtCoDeer the deer recently bought three paint cans.\nThe color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c.\nHere, the color of each paint can is represented by an integer between 1 and 100, inclusive.

\n

Since he is forgetful, he might have bought more than one paint can in the same color.\nCount the number of different kinds of colors of these paint cans and tell him.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦a,b,c≦100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
a b c\n
\n
\n
\n
\n
\n

Output

Print the number of different kinds of colors of the paint cans.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1 4\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

Three different colors: 1, 3, and 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3 33\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

Two different colors: 3 and 33.

\n
\n
", "c_code": "int solution() {\n int a[3];\n scanf(\"%d%d%d\", &a[0], &a[1], &a[2]);\n if (a[0] == a[1] && a[0] == a[2]) {\n printf(\"1\");\n } else if (a[0] != a[1] && a[1] != a[2] && a[2] != a[0]) {\n printf(\"3\");\n } else {\n printf(\"2\");\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let set: HashSet = s.split_whitespace().map(|e| e.parse().unwrap()).collect();\n\n println!(\"{}\", set.len());\n}", "difficulty": "hard"} {"problem_id": "0280", "problem_description": "A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides.", "c_code": "int solution() {\n int n = 0;\n int m = 0;\n while (scanf(\"%d %d\", &n, &m) != EOF) {\n\n getchar();\n char a[55][55];\n memset(a, 0, sizeof(a));\n int Lup = 55;\n int Ldown = 0;\n int Cleft = 55;\n int Cright = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n a[i][j] = getchar();\n if (a[i][j] == '*') {\n if (i < Lup) {\n Lup = i;\n }\n if (i > Ldown) {\n Ldown = i;\n }\n if (j < Cleft) {\n Cleft = j;\n }\n if (j > Cright) {\n Cright = j;\n }\n }\n }\n\n if (i < n - 1) {\n getchar();\n }\n }\n for (int i = Lup; i <= Ldown; i++) {\n for (int j = Cleft; j <= Cright; j++) {\n putchar(a[i][j]);\n }\n putchar('\\n');\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n\n io::stdin()\n .read_line(&mut line)\n .expect(\"Failed to read line\");\n\n let mut line_words = line.split_whitespace();\n let n: usize = line_words\n .next()\n .unwrap()\n .parse()\n .expect(\"Not n parameter was given\");\n let m: usize = line_words\n .next()\n .unwrap()\n .parse()\n .expect(\"Not m parameter was given\");\n let (mut x_min, mut x_max, mut y_min, mut y_max) = (m, 0, n, 0);\n let mut paper: Vec = Vec::with_capacity(n);\n for i in 0..n {\n line.clear();\n io::stdin()\n .read_line(&mut line)\n .expect(\"Failed to read line\");\n paper.push(line.trim().to_string());\n let (mut min, mut max) = (None, None);\n for (j, c) in line.chars().enumerate() {\n if c == '*' {\n if min.is_none() {\n min = Some(j);\n }\n max = Some(j);\n }\n }\n if min.is_some() {\n x_min = cmp::min(x_min, min.unwrap());\n if y_min == n {\n y_min = i;\n }\n y_max = i;\n }\n if max.is_some() {\n x_max = cmp::max(x_max, max.unwrap());\n }\n }\n for i in y_min..y_max + 1 {\n println!(\"{}\", paper[i].get(x_min..x_max + 1).unwrap());\n }\n}", "difficulty": "medium"} {"problem_id": "0281", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Determine if an N-sided polygon (not necessarily convex) with sides of length L_1, L_2, ..., L_N can be drawn in a two-dimensional plane.

\n

You can use the following theorem:

\n

Theorem: an N-sided polygon satisfying the condition can be drawn if and only if the longest side is strictly shorter than the sum of the lengths of the other N-1 sides.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 3 \\leq N \\leq 10
  • \n
  • 1 \\leq L_i \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nL_1 L_2 ... L_N\n
\n
\n
\n
\n
\n

Output

If an N-sided polygon satisfying the condition can be drawn, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n3 8 5 1\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Since 8 < 9 = 3 + 5 + 1, it follows from the theorem that such a polygon can be drawn on a plane.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n3 8 4 1\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

Since 8 \\geq 8 = 3 + 4 + 1, it follows from the theorem that such a polygon cannot be drawn on a plane.

\n
\n
\n
\n
\n
\n

Sample Input 3

10\n1 8 10 5 8 12 34 100 11 3\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int L[101];\n int max = 0;\n int sum = 0;\n scanf(\"%d\", &N);\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &L[i]);\n if (max < L[i]) {\n max = L[i];\n }\n sum += L[i];\n }\n if (max < sum - max) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n stdin().read_line(&mut n).unwrap();\n\n let mut ls = String::new();\n stdin().read_line(&mut ls).unwrap();\n let ls: Vec = ls.split_whitespace().flat_map(str::parse).collect();\n let lngst = ls.clone().into_iter().max().unwrap();\n let mut v = -lngst;\n for l in ls.into_iter() {\n v += l;\n if v > lngst {\n println!(\"Yes\");\n return;\n }\n }\n println!(\"No\");\n}", "difficulty": "medium"} {"problem_id": "0282", "problem_description": "This is the easy version of this problem. In this version, we do not have queries. Note that we have multiple test cases in this version. You can make hacks only if both versions of the problem are solved.An array $$$b$$$ of length $$$m$$$ is good if for all $$$i$$$ the $$$i$$$-th element is greater than or equal to $$$i$$$. In other words, $$$b$$$ is good if and only if $$$b_i \\geq i$$$ for all $$$i$$$ ($$$1 \\leq i \\leq m$$$).You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find the number of pairs of indices $$$(l, r)$$$, where $$$1 \\le l \\le r \\le n$$$, such that the array $$$[a_l, a_{l+1}, \\ldots, a_r]$$$ is good.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int p = 1;\n scanf(\"%d\", &n);\n int a[n];\n long long r = 1;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int k = 1; k < n; k++) {\n ++p;\n p = p > a[k] ? a[k] : p;\n r += p;\n }\n printf(\"%lld\\n\", r);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut lines = io::stdin().lines();\n let n_cases: usize = lines.next().unwrap().unwrap().parse().unwrap();\n 'cases: for _ in 0..n_cases {\n lines.next();\n let array = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|token| token.parse::().unwrap() - 1)\n .collect::>();\n let mut starts = Vec::with_capacity(array.len() + 1);\n let mut ends = Vec::with_capacity(array.len() + 1);\n let mut start_idx = 0;\n let len = array.len();\n for (idx, value) in array.into_iter().enumerate() {\n let backward = idx.saturating_sub(value);\n if backward > start_idx {\n starts.push(start_idx);\n ends.push(idx);\n start_idx = backward;\n }\n }\n starts.push(start_idx);\n ends.push(len);\n starts.push(len);\n ends.push(len + 1);\n\n let mut sum = 0;\n\n for lol in starts\n .into_iter()\n .zip(ends.into_iter())\n .collect::>()\n .windows(2)\n {\n let [x, y] = lol else { panic!() };\n let a = y.0 - x.0;\n let b = x.1 - x.0;\n sum += a * b - ((a * (a - 1)) / 2);\n }\n\n println!(\"{sum}\");\n }\n}", "difficulty": "hard"} {"problem_id": "0283", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a string S consisting of lowercase English letters.\nWe will write down this string, starting a new line after every w letters. Print the string obtained by concatenating the letters at the beginnings of these lines from top to bottom.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq w \\leq |S| \\leq 1000
  • \n
  • S consists of lowercase English letters.
  • \n
  • w is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nw\n
\n
\n
\n
\n
\n

Output

Print the desired string in one line.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

abcdefgh\n3\n
\n
\n
\n
\n
\n

Sample Output 1

adg\n
\n

When we write down abcdefgh, starting a new line after every three letters, we get the following:

\n

abc
\ndef
\ngh

\n

Concatenating the letters at the beginnings of these lines, we obtain adg.

\n
\n
\n
\n
\n
\n

Sample Input 2

lllll\n1\n
\n
\n
\n
\n
\n

Sample Output 2

lllll\n
\n
\n
\n
\n
\n
\n

Sample Input 3

souuundhound\n2\n
\n
\n
\n
\n
\n

Sample Output 3

suudon\n
\n
\n
", "c_code": "int solution(void) {\n char s[30000];\n int w;\n int i = 0;\n scanf(\"%s\", s);\n scanf(\"%d\", &w);\n while (1) {\n printf(\"%c\", s[i]);\n i += w;\n if (s[i] == '\\0') {\n break;\n }\n }\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 w = String::new();\n io::stdin().read_line(&mut w).unwrap();\n let w: usize = w.trim().parse().unwrap();\n let mut uu: Vec = Vec::new();\n for (i, c) in buf.trim().bytes().enumerate() {\n if i % w == 0 {\n uu.push(c);\n }\n }\n println!(\"{}\", String::from_utf8_lossy(uu.as_slice()));\n}", "difficulty": "hard"} {"problem_id": "0284", "problem_description": "AquaMoon and Cirno are playing an interesting game with arrays. Cirno has prepared two arrays $$$a$$$ and $$$b$$$, both consist of $$$n$$$ non-negative integers. AquaMoon can perform the following operation an arbitrary number of times (possibly zero): She chooses two indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$), then decreases the $$$i$$$-th element of array $$$a$$$ by $$$1$$$, and increases the $$$j$$$-th element of array $$$a$$$ by $$$1$$$. The resulting values at $$$i$$$-th and $$$j$$$-th index of array $$$a$$$ are $$$a_i - 1$$$ and $$$a_j + 1$$$, respectively. Each element of array $$$a$$$ must be non-negative after each operation. If $$$i = j$$$ this operation doesn't change the array $$$a$$$. AquaMoon wants to make some operations to make arrays $$$a$$$ and $$$b$$$ equal. Two arrays $$$a$$$ and $$$b$$$ are considered equal if and only if $$$a_i = b_i$$$ for all $$$1 \\leq i \\leq n$$$.Help AquaMoon to find a sequence of operations that will solve her problem or find, that it is impossible to make arrays $$$a$$$ and $$$b$$$ equal.Please note, that you don't have to minimize the number of operations.", "c_code": "int solution() {\n int t;\n\n scanf(\"%d\", &t);\n\n while (t--) {\n int n;\n\n scanf(\"%d\", &n);\n\n int a[n];\n int b[n];\n int c[n];\n int d[n];\n int s1 = 0;\n int s2 = 0;\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n c[i] = 0;\n d[i] = 0;\n }\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &b[i]);\n }\n\n for (int i = 0; i < n; i++) {\n if (a[i] > b[i]) {\n s1 = s1 + a[i] - b[i];\n\n c[i] = a[i] - b[i];\n } else {\n s2 = s2 + b[i] - a[i];\n\n d[i] = b[i] - a[i];\n }\n }\n\n if (s1 != s2) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d\\n\", s1);\n\n int i = 0;\n int j = 0;\n\n while (s1) {\n if ((c[i] == 0) && (d[j] == 0)) {\n i++;\n j++;\n } else if (c[i] == 0) {\n i++;\n } else if (d[j] == 0) {\n j++;\n } else {\n c[i]--;\n s1--;\n printf(\"%d \", i + 1);\n d[j]--;\n printf(\"%d\\n\", j + 1);\n }\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\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 '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 mut a = Vec::with_capacity(n);\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n a.extend(it);\n\n let mut b = Vec::with_capacity(n);\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n b.extend(it);\n\n let asum = a.iter().cloned().sum::();\n let bsum = b.iter().cloned().sum::();\n\n if asum != bsum {\n writeln!(&mut output, \"-1\").unwrap();\n } else {\n let mut ans = Vec::new();\n\n for i in 0..n {\n while a[i] < b[i] {\n let j = (i + 1..).find(|&i| a[i] > b[i]).unwrap();\n\n ans.push((j, i));\n a[i] += 1;\n a[j] -= 1;\n }\n while a[i] > b[i] {\n let j = (i + 1..).find(|&i| a[i] < b[i]).unwrap();\n\n ans.push((i, j));\n a[i] -= 1;\n a[j] += 1;\n }\n }\n\n writeln!(&mut output, \"{}\", ans.len()).unwrap();\n for (i, j) in ans {\n writeln!(&mut output, \"{} {}\", i + 1, j + 1).unwrap();\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0285", "problem_description": "You have a string $$$s$$$ and a chip, which you can place onto any character of this string. After placing the chip, you move it to the right several (maybe zero) times, i. e. you perform the following operation several times: if the current position of the chip is $$$i$$$, you move it to the position $$$i + 1$$$. Of course, moving the chip to the right is impossible if it is already in the last position.After moving the chip to the right, you move it to the left several (maybe zero) times, i. e. you perform the following operation several times: if the current position of the chip is $$$i$$$, you move it to the position $$$i - 1$$$. Of course, moving the chip to the left is impossible if it is already in the first position.When you place a chip or move it, you write down the character where the chip ends up after your action. For example, if $$$s$$$ is abcdef, you place the chip onto the $$$3$$$-rd character, move it to the right $$$2$$$ times and then move it to the left $$$3$$$ times, you write down the string cdedcb.You are given two strings $$$s$$$ and $$$t$$$. Your task is to determine whether it's possible to perform the described operations with $$$s$$$ so that you write down the string $$$t$$$ as a result.", "c_code": "int solution() {\n int t;\n int s;\n int m;\n int n;\n int flag;\n char a[505];\n char b[10005];\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%s\", a);\n scanf(\"%s\", b);\n flag = 0;\n int i;\n int j;\n int k;\n int l1 = strlen(a);\n int l2 = strlen(b);\n for (i = 0; i < l1; i++) {\n if (b[0] == a[i]) {\n if (l2 == 1) {\n flag = 1;\n } else {\n for (j = i + 1, k = 1; j < l1 && k < l2; j++, k++) {\n if (b[k] != a[j]) {\n break;\n }\n }\n if (k >= l2) {\n flag = 1;\n break;\n }\n for (n = 1; n <= j - i + 1; n++) {\n for (m = j - n, s = k - n; j >= 0 && k < l2; m--, s++) {\n if (b[s] != a[m]) {\n break;\n }\n }\n if (s >= l2) {\n flag = 1;\n break;\n }\n }\n if (flag) {\n break;\n }\n for (j = i - 1, k = 1; j >= 0 && k < l2; j--, k++) {\n if (b[k] != a[j])\n break;\n }\n if (k >= l2)\n flag = 1;\n }\n }\n }\n if (flag) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n stdin.read_line(&mut line).unwrap();\n let q: usize = line.trim().parse().unwrap();\n\n let stdout = io::stdout();\n let handle = stdout.lock();\n let mut buffer = BufWriter::with_capacity(65536, handle);\n for _q in 0..q {\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let s = line.trim().to_string();\n\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let t = line.trim().to_string();\n\n let start_right = t.len() / 2;\n let find_string = |original_string: &str, string_to_find: &str| -> bool {\n for i in start_right..string_to_find.len() {\n let mut is_reversion_point = true;\n for (left, right) in string_to_find[..i]\n .chars()\n .rev()\n .zip(string_to_find[(i + 1)..].chars())\n {\n if left != right {\n is_reversion_point = false;\n break;\n }\n }\n if is_reversion_point && original_string.contains(&string_to_find[..=i]) {\n return true;\n }\n }\n false\n };\n\n let t_reversed: String = t.chars().rev().collect();\n if find_string(&s, &t) || find_string(&s, &t_reversed) {\n writeln!(&mut buffer, \"YES\").unwrap();\n } else {\n writeln!(&mut buffer, \"NO\").unwrap();\n }\n }\n buffer.flush().unwrap();\n}", "difficulty": "medium"} {"problem_id": "0286", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N pieces of source code. The characteristics of the i-th code is represented by M integers A_{i1}, A_{i2}, ..., A_{iM}.

\n

Additionally, you are given integers B_1, B_2, ..., B_M and C.

\n

The i-th code correctly solves this problem if and only if A_{i1} B_1 + A_{i2} B_2 + ... + A_{iM} B_M + C > 0.

\n

Among the N codes, find the number of codes that correctly solve this problem.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N, M \\leq 20
  • \n
  • -100 \\leq A_{ij} \\leq 100
  • \n
  • -100 \\leq B_i \\leq 100
  • \n
  • -100 \\leq C \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M C\nB_1 B_2 ... B_M\nA_{11} A_{12} ... A_{1M}\nA_{21} A_{22} ... A_{2M}\n\\vdots\nA_{N1} A_{N2} ... A_{NM}\n
\n
\n
\n
\n
\n

Output

Print the number of codes among the given N codes that correctly solve this problem.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 -10\n1 2 3\n3 2 1\n1 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Only the second code correctly solves this problem, as follows:

\n
    \n
  • Since 3 \\times 1 + 2 \\times 2 + 1 \\times 3 + (-10) = 0 \\leq 0, the first code does not solve this problem.
  • \n
  • 1 \\times 1 + 2 \\times 2 + 2 \\times 3 + (-10) = 1 > 0, the second code solves this problem.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5 2 -4\n-2 5\n100 41\n100 40\n-3 0\n-6 -2\n18 -13\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3 3 0\n100 -100 0\n0 100 100\n100 100 100\n-100 100 100\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

All of them are Wrong Answer. Except yours.

\n
\n
", "c_code": "int solution() {\n int n = 0;\n int m = 0;\n int c = 0;\n scanf(\"%d %d %d\\n\", &n, &m, &c);\n int count = 0;\n int b[m];\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", b + i);\n }\n int buf = 0;\n int add = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &buf);\n add += buf * b[j];\n }\n add += c;\n if (add > 0) {\n count++;\n }\n add = 0;\n }\n printf(\"%d\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut c = String::new();\n let mut b = String::new();\n stdin().read_line(&mut c).unwrap();\n stdin().read_line(&mut b).unwrap();\n let c: Vec = c.split_whitespace().flat_map(str::parse).collect();\n let b: Vec = b.split_whitespace().flat_map(str::parse).collect();\n let (n, _m, c) = (c[0], c[1], c[2]);\n\n println!(\n \"{}\",\n (0..n)\n .filter(|_| {\n let mut a = String::new();\n stdin().read_line(&mut a).unwrap();\n a.split_whitespace()\n .flat_map(str::parse::)\n .zip(b.iter())\n .map(|(aa, &bb)| aa * bb)\n .sum::()\n > -c\n })\n .count()\n );\n}", "difficulty": "medium"} {"problem_id": "0287", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC.

\n

You, the smartwatch, has found N routes to his home.

\n

If Mr. X uses the i-th of these routes, he will get home in time t_i at cost c_i.

\n

Find the smallest cost of a route that takes not longer than time T.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq T \\leq 1000
  • \n
  • 1 \\leq c_i \\leq 1000
  • \n
  • 1 \\leq t_i \\leq 1000
  • \n
  • The pairs (c_i, t_i) are distinct.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N T\nc_1 t_1\nc_2 t_2\n:\nc_N t_N\n
\n
\n
\n
\n
\n

Output

Print the smallest cost of a route that takes not longer than time T.

\n

If there is no route that takes not longer than time T, print TLE instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 70\n7 60\n1 80\n4 50\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n
    \n
  • The first route gets him home at cost 7.
  • \n
  • The second route takes longer than time T = 70.
  • \n
  • The third route gets him home at cost 4.
  • \n
\n

Thus, the cost 4 of the third route is the minimum.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 3\n1 1000\n2 4\n3 1000\n4 500\n
\n
\n
\n
\n
\n

Sample Output 2

TLE\n
\n

There is no route that takes not longer than time T = 3.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 9\n25 8\n5 9\n4 10\n1000 1000\n6 1\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution() {\n\n int c[100];\n int t[100];\n int i = 0;\n int j = -1;\n int k = 0;\n\n scanf(\"%d%d\", &c[i], &t[i]);\n\n while (i < c[0]) {\n\n i++;\n k++;\n scanf(\"%d%d\", &c[i], &t[i]);\n if (j == -1) {\n j = c[i];\n }\n if (c[i] <= j && t[i] <= t[0]) {\n j = c[i];\n k--;\n }\n }\n\n if (k == i) {\n printf(\"TLE\");\n } else {\n printf(\"%d\", j);\n }\n\n return 0;\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 n: usize = iter.next().unwrap().parse().unwrap();\n let T: usize = iter.next().unwrap().parse().unwrap();\n let mut c = vec![0; n];\n let mut t = vec![0; n];\n for i in 0..n {\n c[i] = iter.next().unwrap().parse().unwrap();\n t[i] = iter.next().unwrap().parse().unwrap();\n }\n let ans = (0..n).filter(|i| t[*i] <= T).map(|i| c[i]).min();\n\n println!(\n \"{}\",\n match ans {\n Some(x) => x.to_string(),\n None => \"TLE\".to_string(),\n }\n );\n}", "difficulty": "easy"} {"problem_id": "0288", "problem_description": "You are given a multiset $$$S$$$ initially consisting of $$$n$$$ distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.You will perform the following operation $$$k$$$ times: Add the element $$$\\lceil\\frac{a+b}{2}\\rceil$$$ (rounded up) into $$$S$$$, where $$$a = \\operatorname{mex}(S)$$$ and $$$b = \\max(S)$$$. If this number is already in the set, it is added again. Here $$$\\operatorname{max}$$$ of a multiset denotes the maximum integer in the multiset, and $$$\\operatorname{mex}$$$ of a multiset denotes the smallest non-negative integer that is not present in the multiset. For example: $$$\\operatorname{mex}(\\{1,4,0,2\\})=3$$$; $$$\\operatorname{mex}(\\{2,5,1\\})=0$$$. Your task is to calculate the number of distinct elements in $$$S$$$ after $$$k$$$ operations will be done.", "c_code": "int solution() {\n int t;\n int n;\n int k;\n int max;\n int i;\n int temp;\n int nums[100000];\n char *reg;\n scanf(\"%d\", &t);\n\n while (t-- > 0) {\n scanf(\"%d %d\", &n, &k);\n reg = (char *)calloc(n, 1);\n for (i = 0, max = 0; i < n; i++) {\n scanf(\"%d\", &temp);\n nums[i] = temp;\n if (temp > max) {\n max = temp;\n }\n if (temp < n) {\n reg[temp] = 1;\n }\n }\n if (k == 0) {\n printf(\"%d\\n\", n);\n } else if (max == n - 1) {\n printf(\"%d\\n\", n + k);\n } else {\n for (i = 0; i < n; i++) {\n if (reg[i] == 0) {\n break;\n }\n }\n temp = (i + max + 1) / 2;\n for (i = 0; i < n; i++) {\n if (nums[i] == temp) {\n printf(\"%d\\n\", n);\n break;\n }\n }\n if (i == n) {\n printf(\"%d\\n\", n + 1);\n }\n }\n free(reg);\n }\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let (n, k) = scan!(usize, usize);\n let a = scan!([usize; n]);\n\n use std::collections::BTreeSet;\n\n let mut s = BTreeSet::new();\n\n a.iter().for_each(|x| {\n s.insert(*x);\n });\n\n if k > 0 {\n let mut mex = s.len() + 1;\n for i in 0..s.len() {\n if !s.contains(&i) {\n mex = i;\n break;\n }\n }\n\n let max = *s.iter().last().unwrap();\n\n if max > mex {\n s.insert((mex + max).div_ceil(2));\n print!(\"{}\", s.len());\n } else {\n print!(\"{}\", s.len() + k);\n }\n } else {\n print!(\"{}\", s.len());\n }\n print!(\"\\n\");\n }\n}", "difficulty": "medium"} {"problem_id": "0289", "problem_description": "Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible. Help Petya to split the integers. Each of n integers should be exactly in one group.", "c_code": "int solution() {\n int n;\n int Ans[60000];\n int cnt = 0;\n scanf(\"%d\", &n);\n long long sum = (long long)n * (n + 1) / 2;\n puts(sum & 1 ? \"1\" : \"0\");\n sum /= 2;\n while (1) {\n if (sum >= n) {\n Ans[cnt++] = n;\n sum -= n;\n }\n if (sum == 0) {\n break;\n }\n --n;\n }\n printf(\"%d \", cnt);\n for (int i = 0; i < cnt; ++i) {\n printf(\"%d \", Ans[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::prelude::*;\n std::io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n\n let mut g1 = Vec::new();\n let mut g2 = Vec::new();\n let mut s1 = 0;\n let mut s2 = 0;\n\n for i in (1..n + 1).rev() {\n if s1 < s2 {\n s1 += i;\n g1.push(i);\n } else {\n s2 += i;\n g2.push(i);\n }\n }\n\n let ans = std::cmp::max(s1, s2) - std::cmp::min(s1, s2);\n\n println!(\"{}\", ans);\n\n let mut ans_str = format!(\"{} \", g1.len());\n for i in g1 {\n ans_str.push_str(&format!(\"{} \", i));\n }\n println!(\"{}\", ans_str);\n}", "difficulty": "hard"} {"problem_id": "0290", "problem_description": "Dr. Bruce Banner hates his enemies (like others don't). As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings.Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, second one is love, third one is hate and so on...For example if n = 1, then his feeling is \"I hate it\" or if n = 2 it's \"I hate that I love it\", and if n = 3 it's \"I hate that I love that I hate it\" and so on.Please help Dr. Banner.", "c_code": "int solution() {\n\n int n = 0;\n int cycle = 0;\n scanf(\"%d\", &n);\n while (cycle != n) {\n if (cycle % 2 == 0) {\n printf(\"I hate \");\n if (cycle + 1 != n) {\n printf(\"that \");\n }\n } else {\n printf(\"I love \");\n if (cycle + 1 != n) {\n printf(\"that \");\n }\n }\n cycle++;\n }\n printf(\"it\\n\");\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let n = BufReader::new(std::io::stdin())\n .lines()\n .next()\n .unwrap()?\n .parse::()?;\n\n let stdout = std::io::stdout();\n let mut out = stdout.lock();\n\n if n == 1 {\n writeln!(&mut out, \"I hate it\")?;\n return Ok(());\n }\n\n let mut count: u8 = 1;\n write!(out, \"I hate that \")?;\n\n while count < n - 1 {\n match count & 1 {\n 1 => write!(out, \"I love that \")?,\n 0 => write!(out, \"I hate that \")?,\n _ => unreachable!(),\n }\n count += 1;\n }\n\n write!(\n out,\n \"{}\",\n if count & 1 == 1 {\n \"I love it\\n\"\n } else {\n \"I hate it\\n\"\n }\n )?;\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "0291", "problem_description": "Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ.", "c_code": "int solution() {\n int k;\n int mid;\n int a[10] = {0};\n scanf(\"%d%*c\", &k);\n int rest = k;\n while (~(mid = getchar()) && mid != '\\n') {\n a[mid - '0']++;\n rest -= mid - '0';\n }\n int ans = 0;\n int p = 0;\n if (rest <= 0) {\n printf(\"0\\n\");\n return 0;\n }\n for (int i = 0; i < 10; i++) {\n if (rest <= (9 - i) * a[i]) {\n ans += (rest + 8 - i) / (9 - i);\n break;\n }\n ans += a[i];\n rest -= a[i] * (9 - i);\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::stdin;\n\n let mut input = String::new();\n\n stdin().read_line(&mut input).unwrap();\n\n let k: usize = input.trim().parse().unwrap();\n\n let mut n = String::new();\n stdin().read_line(&mut n).unwrap();\n\n let mut tab = [0; 10];\n\n for ind in n.trim().chars().map(|c| c.to_digit(10).unwrap() as usize) {\n tab[ind] += 1;\n }\n\n let mut sum: usize = tab.iter().enumerate().map(|(i, k)| i * k).sum();\n let mut cnt = 0;\n\n if k <= sum {\n println!(\"{}\", cnt);\n std::process::exit(0);\n }\n\n for (i, &p) in tab.iter().enumerate() {\n let i = 9 - i;\n if k > p * i + sum {\n sum += p * i;\n cnt += p;\n } else {\n let diff = k as i64 - sum as i64;\n let x = (diff - 1) / i as i64 + 1;\n cnt += x as usize;\n println!(\"{}\", cnt);\n std::process::exit(0);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0292", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.

\n

The i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.

\n

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

\n

At least how many tiles need to be repainted to satisfy the condition?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 10^5
  • \n
  • S_i is 0 or 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

000\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

The condition can be satisfied by repainting the middle tile white.

\n
\n
\n
\n
\n
\n

Sample Input 2

10010010\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

0\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int d = 0;\n char s[100000];\n scanf(\"%s\", s);\n for (a = 0; s[a] != '\\0'; a++) {\n if (a % 2 == 1 && s[a] == '0') {\n b++;\n }\n if (a % 2 == 0 && s[a] == '1') {\n b++;\n }\n if (a % 2 == 1 && s[a] == '1') {\n d++;\n }\n if (a % 2 == 0 && s[a] == '0') {\n d++;\n }\n }\n if (b > d) {\n b = d;\n }\n printf(\"%d\", b);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n\n let mut ans_01 = 0;\n let mut ans_10 = 0;\n for (i, c) in buf.trim().chars().enumerate() {\n ans_01 += match (i % 2, c) {\n (0, '0') | (1, '1') => 0,\n _ => 1,\n };\n\n ans_10 += match (i % 2, c) {\n (0, '1') | (1, '0') => 0,\n _ => 1,\n };\n }\n\n println!(\"{}\", std::cmp::min(ans_01, ans_10));\n}", "difficulty": "medium"} {"problem_id": "0293", "problem_description": "

List of Top 3 Hills

\n\n

\nThere is a data which provides heights (in meter) of mountains. The data is only for ten mountains.\n

\n\n

\nWrite a program which prints heights of the top three mountains in descending order.\n

\n\n

Input

\n\n
\nHeight of mountain 1\nHeight of mountain 2\nHeight of mountain 3\n .\n .\nHeight of mountain 10\n
\n\n

Constraints

\n\n

\n0 ≤ height of mountain (integer) ≤ 10,000\n

\n\n

Output

\n\n
\nHeight of the 1st mountain\nHeight of the 2nd mountain\nHeight of the 3rd mountain\n
\n\n

Sample Input 1

\n
\n1819\n2003\n876\n2840\n1723\n1673\n3776\n2848\n1592\n922\n
\n\n

Output for the Sample Input 1

\n\n
\n3776\n2848\n2840\n
\n\n\n\n

Sample Input 2

\n
\n100\n200\n300\n400\n500\n600\n700\n800\n900\n900\n
\n\n

Output for the Sample Input 2

\n\n
\n900\n900\n800\n
", "c_code": "int solution() {\n int max = 0;\n int mid = 0;\n int min = 0;\n int h = 0;\n int i = 0;\n for (i = 0; i < 10; i++) {\n scanf(\"%d\", &h);\n if (0 > h || h > 10000) {\n printf(\"error\\n\");\n return 0;\n }\n if (max < h) {\n min = mid;\n mid = max;\n max = h;\n } else if (mid < h) {\n min = mid;\n mid = h;\n } else if (min < h) {\n min = h;\n }\n }\n printf(\"%d\\n%d\\n%d\\n\", max, mid, min);\n return 0;\n}", "rust_code": "fn solution() {\n let mut height: Vec = Vec::new();\n for _i in 1..11 {\n let mut input = String::new();\n let _ = io::stdin().read_line(&mut input);\n let h = input.trim().parse::().unwrap();\n height.push(h);\n }\n\n height.sort_by(|a, b| b.cmp(a));\n\n for i in 0..3 {\n println!(\"{}\", height.get(i).unwrap());\n }\n}", "difficulty": "medium"} {"problem_id": "0294", "problem_description": "Polycarp has found a table having an infinite number of rows and columns. The rows are numbered from $$$1$$$, starting from the topmost one. The columns are numbered from $$$1$$$, starting from the leftmost one.Initially, the table hasn't been filled and Polycarp wants to fix it. He writes integers from $$$1$$$ and so on to the table as follows. The figure shows the placement of the numbers from $$$1$$$ to $$$10$$$. The following actions are denoted by the arrows. The leftmost topmost cell of the table is filled with the number $$$1$$$. Then he writes in the table all positive integers beginning from $$$2$$$ sequentially using the following algorithm.First, Polycarp selects the leftmost non-filled cell in the first row and fills it. Then, while the left neighbor of the last filled cell is filled, he goes down and fills the next cell. So he goes down until the last filled cell has a non-filled neighbor to the left (look at the vertical arrow going down in the figure above).After that, he fills the cells from the right to the left until he stops at the first column (look at the horizontal row in the figure above). Then Polycarp selects the leftmost non-filled cell in the first row, goes down, and so on.A friend of Polycarp has a favorite number $$$k$$$. He wants to know which cell will contain the number. Help him to find the indices of the row and the column, such that the intersection of the row and the column is the cell containing the number $$$k$$$.", "c_code": "int solution() {\n int n = 0;\n int i = 0;\n int j = 0;\n int k = 0;\n int l = 0;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &j);\n k = sqrt(j);\n\n if (j == k * k) {\n printf(\"%d 1\\n\", k);\n } else {\n if (j > k + 1 + k * k) {\n l = (k + 1) * (k + 1) - j;\n printf(\"%d %d\\n\", k + 1, l + 1);\n } else {\n l = j - k * k;\n printf(\"%d %d\\n\", l, k + 1);\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let t = s.trim().parse::().unwrap();\n for _ in 0..t {\n s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let n = s.trim().parse::().unwrap();\n let mut i = 1;\n while i * i <= n {\n i += 1;\n }\n i -= 1;\n let k = n - i * i;\n if k == 0 {\n println!(\"{} {}\", i, 1);\n } else {\n if k < i + 1 {\n println!(\"{} {}\", k, i + 1)\n } else if k > i + 1 {\n println!(\"{} {}\", i + 1, (i + 1) * 2 - k);\n } else {\n println!(\"{} {}\", i + 1, i + 1);\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0295", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There is a grid with H rows and W columns.

\n

The square at the i-th row and j-th column contains a string S_{i,j} of length 5.

\n

The rows are labeled with the numbers from 1 through H, and the columns are labeled with the uppercase English letters from A through the W-th letter of the alphabet.

\n

\"\"

\n

Exactly one of the squares in the grid contains the string snuke. Find this square and report its location.

\n

For example, the square at the 6-th row and 8-th column should be reported as H6.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦H, W≦26
  • \n
  • The length of S_{i,j} is 5.
  • \n
  • S_{i,j} consists of lowercase English letters (a-z).
  • \n
  • Exactly one of the given strings is equal to snuke.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
H W\nS_{1,1} S_{1,2} ... S_{1,W}\nS_{2,1} S_{2,2} ... S_{2,W}\n:\nS_{H,1} S_{H,2} ... S_{H,W}\n
\n
\n
\n
\n
\n

Output

Print the labels of the row and the column of the square containing the string snuke, with no space inbetween.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

15 10\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snuke snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\nsnake snake snake snake snake snake snake snake snake snake\n
\n
\n
\n
\n
\n

Sample Output 1

H6\n
\n
\n
\n
\n
\n
\n

Sample Input 2

1 1\nsnuke\n
\n
\n
\n
\n
\n

Sample Output 2

A1\n
\n
\n
", "c_code": "int solution() {\n int h;\n int w;\n scanf(\"%d%d\", &h, &w);\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n char s[6];\n scanf(\"%s\", s);\n if (strcmp(s, \"snuke\") == 0) {\n printf(\"%c%d\", 'A' + j, i + 1);\n return 0;\n }\n }\n }\n printf(\"Hello World\");\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 let mut buf_it = buf.split_whitespace();\n\n let H = buf_it.next().unwrap().parse::().unwrap();\n let W = buf_it.next().unwrap().parse::().unwrap();\n\n let AZ = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\".chars().collect::>();\n\n for i in 1..H + 1 {\n for j in 0..W {\n let s = buf_it.next().unwrap();\n if s == \"snuke\" {\n println!(\"{}{}\", AZ[j], i);\n return;\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0296", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not.

\n

Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind.

\n

How many honest persons can be among those N people at most?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 15
  • \n
  • 0 \\leq A_i \\leq N - 1
  • \n
  • 1 \\leq x_{ij} \\leq N
  • \n
  • x_{ij} \\neq i
  • \n
  • x_{ij_1} \\neq x_{ij_2} (j_1 \\neq j_2)
  • \n
  • y_{ij} = 0, 1
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1\nx_{11} y_{11}\nx_{12} y_{12}\n:\nx_{1A_1} y_{1A_1}\nA_2\nx_{21} y_{21}\nx_{22} y_{22}\n:\nx_{2A_2} y_{2A_2}\n:\nA_N\nx_{N1} y_{N1}\nx_{N2} y_{N2}\n:\nx_{NA_N} y_{NA_N}\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number of honest persons among the N people.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1\n2 1\n1\n1 1\n1\n2 0\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

If Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Assuming that one or more of them are honest immediately leads to a contradiction.

\n
\n
\n
\n
\n
\n

Sample Input 3

2\n1\n2 0\n1\n1 0\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int x[n][n];\n int y[n][n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n for (int j = 0; j < a[i]; j++) {\n scanf(\"%d%d\", &x[i][j], &y[i][j]);\n }\n }\n int ans = 0;\n for (int bit = 1; bit < (1 << n); bit++) {\n int flag = 1;\n int count = 0;\n for (int i = 0; i < n && flag; i++) {\n if (bit & (1 << i)) {\n count++;\n for (int j = 0; j < a[i] && flag; j++) {\n if ((bit & (1 << (x[i][j] - 1))) != (y[i][j] << (x[i][j] - 1))) {\n flag = 0;\n }\n }\n }\n }\n if (flag && ans < count) {\n ans = count;\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n std::io::stdin().read_to_string(&mut input_str).unwrap();\n\n let input_parts = input_str.split_whitespace().collect::>();\n let mut input_parts_it = input_parts.iter().cloned();\n let mut next = || input_parts_it.next().unwrap();\n\n let n: usize = next().parse().unwrap();\n\n struct Pr {\n and_mask: u16,\n bitmap: u16,\n }\n\n let prs = (0..n)\n .map(|_| {\n let a: usize = next().parse().unwrap();\n let mut and_mask: u16 = 0;\n let mut bitmap: u16 = 0;\n\n for _ in 0..a {\n let x: u32 = next().parse().unwrap();\n let y: u8 = next().parse().unwrap();\n and_mask |= 1u16 << x;\n if y != 0 {\n bitmap |= 1u16 << x;\n }\n }\n\n Pr {\n and_mask: and_mask >> 1,\n bitmap: bitmap >> 1,\n }\n })\n .collect::>();\n\n let best = (0..(1u16 << n))\n .filter(|&bitmap| {\n prs.iter().enumerate().all(|(i, pr)| {\n if (bitmap & (1 << i)) != 0 {\n (bitmap & pr.and_mask) == pr.bitmap\n } else {\n true\n }\n })\n })\n .map(|bitmap| bitmap.count_ones())\n .max()\n .unwrap();\n\n println!(\"{}\", best);\n}", "difficulty": "medium"} {"problem_id": "0297", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Let us define the beauty of a sequence (a_1,... ,a_n) as the number of pairs of two adjacent elements in it whose absolute differences are d.\nFor example, when d=1, the beauty of the sequence (3, 2, 3, 10, 9) is 3.

\n

There are a total of n^m sequences of length m where each element is an integer between 1 and n (inclusive).\nFind the beauty of each of these n^m sequences, and print the average of those values.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq d < n \\leq 10^9
  • \n
  • 2 \\leq m \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n m d\n
\n
\n
\n
\n
\n

Output

Print the average of the beauties of the sequences of length m where each element is an integer between 1 and n.\nThe output will be judged correct if the absolute or relative error is at most 10^{-6}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 1\n
\n
\n
\n
\n
\n

Sample Output 1

1.0000000000\n
\n

The beauty of (1,1,1) is 0.
\nThe beauty of (1,1,2) is 1.
\nThe beauty of (1,2,1) is 2.
\nThe beauty of (1,2,2) is 1.
\nThe beauty of (2,1,1) is 1.
\nThe beauty of (2,1,2) is 2.
\nThe beauty of (2,2,1) is 1.
\nThe beauty of (2,2,2) is 0.
\nThe answer is the average of these values: (0+1+2+1+1+2+1+0)/8=1.

\n
\n
\n
\n
\n
\n

Sample Input 2

1000000000 180707 0\n
\n
\n
\n
\n
\n

Sample Output 2

0.0001807060\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n long m;\n long d;\n scanf(\"%ld %ld %ld\", &n, &m, &d);\n long all = pow(n, 2);\n long beautiful = (n - d) * 2;\n if (d == 0) {\n beautiful = n;\n }\n double ans = (beautiful * 1.0) / (all * 1.0);\n ans *= m - 1;\n printf(\"%.10lf\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_line = String::new();\n io::stdin().read_line(&mut input_line).unwrap();\n let inputs: Vec<&str> = input_line.trim().split(\" \").collect();\n let n = inputs[0].parse::().unwrap();\n let m = inputs[1].parse::().unwrap();\n let d = inputs[2].parse::().unwrap();\n\n let ans = if d == 0.0 {\n (m - 1.0) / n\n } else {\n (m - 1.0) * 2.0 * (n - d) / (n * n)\n };\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0298", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \\leq i \\leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

\n

How many times will the ball make a bounce where the coordinate is at most X?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq L_i \\leq 100
  • \n
  • 1 \\leq X \\leq 10000
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N X\nL_1 L_2 ... L_{N-1} L_N\n
\n
\n
\n
\n
\n

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 6\n3 4 5\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 9\n3 3 3 3\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.

\n
\n
", "c_code": "int solution() {\n int n = 0;\n int x = 0;\n int ans = 0;\n int d = 0;\n int a[100];\n scanf(\"%d %d\", &n, &x);\n for (int i = 0; i < n + 1; i++) {\n scanf(\"%d\", &a[i]);\n if (d <= x) {\n ans++;\n } else {\n }\n d = d + a[i];\n }\n printf(\"%d\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let (_n, x) = {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut it = buf.split_whitespace();\n (\n usize::from_str(it.next().unwrap()).unwrap(),\n i32::from_str(it.next().unwrap()).unwrap(),\n )\n };\n let l = {\n let mut l: Vec = Vec::new();\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let it = buf.split_whitespace();\n for ln in it {\n l.push(i32::from_str(ln).unwrap());\n }\n l\n };\n\n let mut d = 0;\n let mut ans = 0;\n for ln in l {\n d += ln;\n if d <= x {\n ans += 1;\n }\n }\n println!(\"{}\", ans + 1);\n}", "difficulty": "medium"} {"problem_id": "0299", "problem_description": "There are $$$n$$$ products in the shop. The price of the $$$i$$$-th product is $$$a_i$$$. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.In fact, the owner of the shop can change the price of some product $$$i$$$ in such a way that the difference between the old price of this product $$$a_i$$$ and the new price $$$b_i$$$ is at most $$$k$$$. In other words, the condition $$$|a_i - b_i| \\le k$$$ should be satisfied ($$$|x|$$$ is the absolute value of $$$x$$$).He can change the price for each product not more than once. Note that he can leave the old prices for some products. The new price $$$b_i$$$ of each product $$$i$$$ should be positive (i.e. $$$b_i > 0$$$ should be satisfied for all $$$i$$$ from $$$1$$$ to $$$n$$$).Your task is to find out the maximum possible equal price $$$B$$$ of all productts with the restriction that for all products the condiion $$$|a_i - B| \\le k$$$ should be satisfied (where $$$a_i$$$ is the old price of the product and $$$B$$$ is the same new price of all products) or report that it is impossible to find such price $$$B$$$.Note that the chosen price $$$B$$$ should be integer.You should answer $$$q$$$ independent queries.", "c_code": "int solution(void) {\n int query = 0;\n int N = 0;\n int K = 0;\n int i = 0;\n int j = 0;\n int A[100] = {\n 0,\n };\n int min = 1000000000;\n int max = 0;\n\n int min_plus;\n int max_minus;\n\n scanf(\"%d\", &query);\n\n for (i = 0; i < query; i++) {\n min = 1000000000;\n max = 0;\n\n scanf(\"%d\", &N);\n scanf(\"%d\", &K);\n\n for (j = 0; j < N; j++) {\n scanf(\"%d\", &A[j]);\n if (min > A[j]) {\n min = A[j];\n }\n if (max < A[j]) {\n max = A[j];\n }\n }\n\n min_plus = min + K;\n max_minus = max - K;\n\n if (max_minus <= min_plus) {\n printf(\"%d\\n\", min_plus);\n } else {\n printf(\"-1\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let stdin = io::stdin();\n let mut input = String::new();\n stdin.read_line(&mut input)?;\n\n let q: u8 = input.trim().parse().unwrap();\n for _ in 1..q + 1 {\n let mut input = String::new();\n stdin.read_line(&mut input)?;\n\n let l: Vec = input\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let _n = l[0];\n let k = l[1];\n\n let mut input = String::new();\n stdin.read_line(&mut input)?;\n let prices = input\n .split_whitespace()\n .map(|x| x.trim().parse::().unwrap());\n\n let mut sup = std::i32::MAX;\n let mut inf = 0;\n let mut breaked = false;\n for price in prices {\n if inf > price + k || sup < max(0, price - k) {\n println!(\"-1\");\n breaked = true;\n break;\n }\n\n sup = min(price + k, sup);\n inf = max(price - k, inf);\n }\n\n if !breaked {\n println!(\"{}\", sup);\n }\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0300", "problem_description": "Find the minimum area of a square land on which you can place two identical rectangular $$$a \\times b$$$ houses. The sides of the houses should be parallel to the sides of the desired square land.Formally, You are given two identical rectangles with side lengths $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 100$$$) — positive integers (you are given just the sizes, but not their positions). Find the square of the minimum area that contains both given rectangles. Rectangles can be rotated (both or just one), moved, but the sides of the rectangles should be parallel to the sides of the desired square. Two rectangles can touch each other (side or corner), but cannot intersect. Rectangles can also touch the sides of the square but must be completely inside it. You can rotate the rectangles. Take a look at the examples for a better understanding. The picture shows a square that contains red and green rectangles.", "c_code": "int solution() {\n int t;\n int length[10000];\n int breadth[10000];\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%d %d\", &length[i], &breadth[i]);\n }\n for (int j = 0; j < t; j++) {\n if (length[j] <= breadth[j] && 2 * length[j] >= breadth[j]) {\n printf(\"%d\\n\", 4 * length[j] * length[j]);\n }\n if (breadth[j] < length[j] && 2 * breadth[j] >= length[j]) {\n printf(\"%d\\n\", 4 * breadth[j] * breadth[j]);\n }\n if (length[j] > 2 * breadth[j]) {\n printf(\"%d\\n\", length[j] * length[j]);\n }\n if (breadth[j] > 2 * length[j]) {\n printf(\"%d\\n\", breadth[j] * breadth[j]);\n }\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n let _ = io::stdin()\n .read_line(&mut line)\n .expect(\"Error reading input\");\n let test_count: u32 = line.trim().parse().expect(\"Parse error\");\n for _ in 0..test_count {\n line.clear();\n let _ = io::stdin()\n .read_line(&mut line)\n .expect(\"Error reading input\");\n let parts: Vec<&str> = line.split_whitespace().collect();\n let a: u32 = String::from(parts[0]).parse().expect(\"Parse error\");\n let b: u32 = String::from(parts[1]).parse().expect(\"Parse error\");\n let side = std::cmp::max(std::cmp::max(a, b), std::cmp::min(a, b) * 2);\n println!(\"{}\", side.pow(2));\n }\n}", "difficulty": "hard"} {"problem_id": "0301", "problem_description": "Alice and Bob like games. And now they are ready to start a new game. They have placed n chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob — from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with equal speed). When the player consumes a chocolate bar, he immediately starts with another. It is not allowed to eat two chocolate bars at the same time, to leave the bar unfinished and to make pauses. If both players start to eat the same bar simultaneously, Bob leaves it to Alice as a true gentleman.How many bars each of the players will consume?", "c_code": "int solution() {\n long int n;\n long int n1 = 0;\n long int n2 = 0;\n long int i;\n scanf(\"%ld\", &n);\n long int t[n];\n for (i = 0; i < n; i++) {\n scanf(\"%ld\", &t[i]);\n }\n long int *p = &t[0];\n long int *q = &t[n - 1];\n while (q >= p) {\n if (*p < *q) {\n *q -= *p;\n p++;\n n1++;\n if (p == q) {\n n2++;\n break;\n }\n } else if (*q < *p) {\n *p -= *q;\n q--;\n n2++;\n if (p == q) {\n n1++;\n break;\n }\n } else {\n if (p == q) {\n n1++;\n break;\n }\n n1++;\n n2++;\n p++;\n q--;\n }\n }\n printf(\"%ld %ld\", n1, n2);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut input = input.split_whitespace();\n\n\n let n = read!(usize);\n let ts = read!([u64; n]);\n let mut ta = 0;\n let mut tb = 0;\n let mut ia = 0;\n let mut ib = n - 1;\n while ia <= ib {\n if ta <= tb {\n ta += ts[ia];\n ia += 1;\n } else {\n tb += ts[ib];\n ib -= 1;\n }\n }\n println!(\"{} {}\", ia, n - ia);\n}", "difficulty": "medium"} {"problem_id": "0302", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi is distributing N balls to K persons.

\n

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq N \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

Print the maximum possible difference in the number of balls received.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

\n

Thus, the maximum possible difference in the number of balls received is 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

\n
\n
\n
\n
\n
\n

Sample Input 3

8 5\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.

\n
\n
", "c_code": "int solution() {\n int N = 0;\n int K = 0;\n int max = 0;\n scanf(\"%d %d\\n\", &N, &K);\n max = N - K;\n if (K == 1) {\n max = 0;\n }\n printf(\"%d\", max);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let nums: Vec = input\n .split_whitespace()\n .map(|x| x.parse().ok().unwrap())\n .collect();\n if nums[1] == 1 {\n println!(\"0\");\n } else {\n println!(\"{}\", nums[0] - nums[1]);\n }\n}", "difficulty": "hard"} {"problem_id": "0303", "problem_description": "You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones. You can do the following operation: choose two indices $$$1 \\le i , j \\le n$$$, $$$i \\ne j$$$, add $$$a_{i}$$$ to $$$a_{j}$$$, remove $$$a_{i}$$$ from $$$a$$$. Note that elements of $$$a$$$ can become bigger than $$$1$$$ after performing some operations. Also note that $$$n$$$ becomes $$$1$$$ less after the operation.What is the minimum number of operations needed to make $$$a$$$ non-decreasing, i. e. that each element is not less than the previous element?", "c_code": "int solution()\n\n{\n int n = 0;\n scanf(\"%d\", &n);\n while (n--) {\n int x;\n int y = 0;\n int e = 0;\n int f = 0;\n int g = 0;\n int a[100000];\n scanf(\"%d \", &x);\n for (int i = 0; i < x; i++) {\n scanf(\"%d\", &a[i]);\n y++;\n if (a[i] == 1) {\n e++;\n }\n }\n for (int i = y - 1; i >= 0; i = i - 1) {\n if (a[i] == 0 && e != 0) {\n while (a[g] == 0) {\n g++;\n }\n a[i] = 1;\n a[g] = 0;\n f++;\n e = e - 1;\n } else if (e != 0) {\n e = e - 1;\n }\n }\n printf(\"%d\\n\", f);\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 = line_iter.next().unwrap().unwrap().parse::().unwrap();\n let bit_line = line_iter.next().unwrap().unwrap();\n let bits: Vec = bit_line.split(' ').map(|digit| digit == \"1\").collect();\n let mut j = n - 1;\n let mut op_count: usize = 0;\n for i in 0..n {\n if bits[i] {\n while j > i {\n if !bits[j] {\n op_count += 1;\n j -= 1;\n break;\n }\n j -= 1;\n }\n if j == i {\n break;\n }\n }\n }\n\n println!(\"{}\", op_count);\n }\n}", "difficulty": "hard"} {"problem_id": "0304", "problem_description": "The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the presented tasks are lovely, but don't show the truth about competitors' skills.The boy is now looking at the ratings of consecutive participants written in a binary system. He thinks that the more such ratings differ, the more unfair is that such people are next to each other. He defines the difference between two numbers as the number of bit positions, where one number has zero, and another has one (we suppose that numbers are padded with leading zeros to the same length). For example, the difference of $$$5 = 101_2$$$ and $$$14 = 1110_2$$$ equals to $$$3$$$, since $$$0101$$$ and $$$1110$$$ differ in $$$3$$$ positions. Johnny defines the unfairness of the contest as the sum of such differences counted for neighboring participants.Johnny has just sent you the rating sequence and wants you to find the unfairness of the competition. You have noticed that you've got a sequence of consecutive integers from $$$0$$$ to $$$n$$$. That's strange, but the boy stubbornly says that everything is right. So help him and find the desired unfairness for received numbers.", "c_code": "int solution() {\n int t;\n long long n;\n\n long long twospower = 1;\n long long totalunfairness = 0;\n\n scanf(\"%d\", &t);\n\n while (t) {\n scanf(\"%lld\", &n);\n\n twospower = 1;\n totalunfairness = 0;\n\n while (n / twospower) {\n totalunfairness += n / twospower;\n twospower *= 2;\n }\n\n printf(\"%lld\\n\", totalunfairness);\n\n t--;\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 let t: usize = itr.next().unwrap().parse().unwrap();\n let mut out = Vec::new();\n for _ in 0..t {\n let n: u64 = itr.next().unwrap().parse().unwrap();\n let mut two = 1;\n while two * 2 <= n {\n two <<= 1;\n }\n\n let mut now = 0;\n let mut ans = 0;\n while two > 0 {\n let d = two.trailing_zeros() + 1;\n ans += (n / two - now) * d as u64;\n now += n / two - now;\n two >>= 1;\n }\n writeln!(out, \"{}\", ans).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "easy"} {"problem_id": "0305", "problem_description": "Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres.In the bookshop, Jack decides to buy two books of different genres.Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.The books are given by indices of their genres. The genres are numbered from 1 to m.", "c_code": "int solution() {\n long long n = 0;\n long long m = 0;\n long long s = 0;\n scanf(\"%lld %lld\", &n, &m);\n\n long long g[10] = {0};\n long long g_;\n\n long long i = 0;\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &g_);\n g[g_ - 1]++;\n }\n\n long long j = 0;\n for (i = 0; i < m - 1; i++) {\n for (j = i + 1; j < m; j++) {\n s += (g[i] * g[j]);\n }\n }\n\n printf(\"%lld\", s);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n buffer.reserve(65536);\n let stdin = io::stdin();\n let mut handle = stdin.lock();\n\n let (n, m) = {\n handle.read_line(&mut buffer);\n let mut words = buffer.split_whitespace();\n let n = words.next().unwrap().parse::().unwrap();\n let m = words.next().unwrap().parse::().unwrap();\n (n, m)\n };\n let genre_counts = {\n buffer.clear();\n handle.read_line(&mut buffer);\n\n let mut counts = vec![0; m];\n for wd in buffer.split_whitespace() {\n let x = wd.parse::().unwrap() - 1;\n counts[x] += 1;\n }\n counts\n };\n let mut options = 0;\n for c in &genre_counts {\n options += c * (n - c);\n }\n options /= 2;\n\n buffer.clear();\n buffer.push_str(&options.to_string());\n buffer.push('\\n');\n let stdout = io::stdout();\n let mut hout = stdout.lock();\n hout.write(buffer.as_bytes());\n}", "difficulty": "hard"} {"problem_id": "0306", "problem_description": "There are two rival donut shops.The first shop sells donuts at retail: each donut costs $$$a$$$ dollars.The second shop sells donuts only in bulk: box of $$$b$$$ donuts costs $$$c$$$ dollars. So if you want to buy $$$x$$$ donuts from this shop, then you have to buy the smallest number of boxes such that the total number of donuts in them is greater or equal to $$$x$$$.You want to determine two positive integer values: how many donuts can you buy so that they are strictly cheaper in the first shop than in the second shop? how many donuts can you buy so that they are strictly cheaper in the second shop than in the first shop? If any of these values doesn't exist then that value should be equal to $$$-1$$$. If there are multiple possible answers, then print any of them.The printed values should be less or equal to $$$10^9$$$. It can be shown that under the given constraints such values always exist if any values exist at all.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n long long int a[t];\n long long int b[t];\n long long int c[t];\n for (int i = 0; i < t; i++) {\n scanf(\"%lld%lld%lld\", &a[i], &b[i], &c[i]);\n }\n int x[t];\n int y[t];\n for (int i = 0; i < t; i++) {\n if (a[i] * b[i] == c[i]) {\n x[i] = b[i] - 1;\n if (x[i] < 1) {\n x[i] = -1;\n }\n y[i] = -1;\n } else if (a[i] * b[i] > c[i]) {\n y[i] = b[i];\n if (c[i] % a[i] != 0) {\n x[i] = c[i] / a[i];\n } else {\n x[i] = c[i] / a[i] - 1;\n }\n if (x[i] < 1) {\n x[i] = -1;\n }\n } else {\n x[i] = b[i];\n y[i] = -1;\n }\n printf(\"%d %d\\n\", x[i], y[i]);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (a, b, c) = (xs[0], xs[1], xs[2]);\n let x: i64 = if a < c { 1 } else { -1 };\n let y: i64 = if a * b <= c { -1 } else { b };\n println!(\"{} {}\", x, y);\n }\n}", "difficulty": "easy"} {"problem_id": "0307", "problem_description": "Given an integer $$$n$$$, find the maximum value of integer $$$k$$$ such that the following condition holds: $$$n$$$ & ($$$n-1$$$) & ($$$n-2$$$) & ($$$n-3$$$) & ... ($$$k$$$) = $$$0$$$ where & denotes the bitwise AND operation.", "c_code": "int solution() {\n\n int numTests = 0;\n int test = 1;\n\n scanf(\"%d\", &numTests);\n\n while (test <= numTests) {\n\n unsigned long int n = 0;\n unsigned long int k = 1;\n\n scanf(\"%lu\", &n);\n\n while (n >>= 1) {\n\n k <<= 1;\n }\n\n printf(\"%lu\\n\", k - 1);\n\n test++;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(1)\n .flatten()\n .flat_map(|s| s.parse::())\n .for_each(|n| {\n let ans = 1u32\n .wrapping_shl(31u32.wrapping_sub(n.leading_zeros()))\n .wrapping_sub(1);\n writeln!(buf, \"{ans}\").ok();\n });\n}", "difficulty": "hard"} {"problem_id": "0308", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi has a strong stomach. He never gets a stomachache from eating something whose \"best-by\" date is at most X days earlier.\nHe gets a stomachache if the \"best-by\" date of the food is X+1 or more days earlier, though.

\n

Other than that, he finds the food delicious if he eats it not later than the \"best-by\" date. Otherwise, he does not find it delicious.

\n

Takahashi bought some food A days before the \"best-by\" date, and ate it B days after he bought it.

\n

Write a program that outputs delicious if he found it delicious, safe if he did not found it delicious but did not get a stomachache either, and dangerous if he got a stomachache.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ X,A,B ≤ 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X A B\n
\n
\n
\n
\n
\n

Output

Print delicious if Takahashi found the food delicious; print safe if he neither found it delicious nor got a stomachache; print dangerous if he got a stomachache.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3 6\n
\n
\n
\n
\n
\n

Sample Output 1

safe\n
\n

He ate the food three days after the \"best-by\" date. It was not delicious or harmful for him.

\n
\n
\n
\n
\n
\n

Sample Input 2

6 5 1\n
\n
\n
\n
\n
\n

Sample Output 2

delicious\n
\n

He ate the food by the \"best-by\" date. It was delicious for him.

\n
\n
\n
\n
\n
\n

Sample Input 3

3 7 12\n
\n
\n
\n
\n
\n

Sample Output 3

dangerous\n
\n

He ate the food five days after the \"best-by\" date. It was harmful for him.

\n
\n
", "c_code": "int solution() {\n int a[4];\n scanf(\"%d%d%d\", &a[0], &a[1], &a[2]);\n a[1] *= -1;\n if (a[1] + a[2] <= 0) {\n printf(\"delicious\");\n } else if (a[1] + a[2] > a[0]) {\n printf(\"dangerous\");\n } else {\n printf(\"safe\");\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut buf = String::new();\n\n stdin.read_line(&mut buf).unwrap();\n let xab: Vec = buf.split_whitespace().map(|n| n.parse().unwrap()).collect();\n\n if -xab[0] <= xab[1] - xab[2] {\n if xab[1] - xab[2] >= 0 {\n println!(\"delicious\");\n } else {\n println!(\"safe\");\n }\n } else {\n println!(\"dangerous\");\n }\n}", "difficulty": "easy"} {"problem_id": "0309", "problem_description": "All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of $$$n\\cdot m$$$ different seals, denoted by distinct numbers. All of them were written in an $$$n\\times m$$$ table.The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique.", "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 int ar[n][m];\n int br[n];\n int tmp[m][n];\n for (int i = 0; i < n; i++) {\n for (int ii = 0; ii < m; ii++) {\n scanf(\"%d\", &ar[i][ii]);\n }\n }\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &tmp[i][j]);\n }\n }\n for (int i = 0; i < n; i++) {\n br[i] = tmp[0][i];\n }\n int check = br[0];\n int f = 0;\n int row = 0;\n int col = 0;\n for (int i = 0; i < n && f == 0; i++) {\n for (int j = 0; j < m; j++) {\n if (ar[i][j] == check) {\n f = 1;\n row = i;\n col = j;\n break;\n }\n }\n }\n for (int i = 0; i < m; i++) {\n printf(\"%d \", ar[row][i]);\n }\n printf(\"\\n\");\n int k = 1;\n while (k < n) {\n for (int i = 0; i < n; i++) {\n if (ar[i][col] == br[k]) {\n for (int j = 0; j < m; j++) {\n printf(\"%d \", ar[i][j]);\n }\n k++;\n printf(\"\\n\");\n break;\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_to_string(&mut buffer).unwrap();\n let mut lines = buffer.lines().peekable();\n\n let t: usize;\n {\n t = lines.next().unwrap().parse().unwrap();\n }\n\n let mut result: Vec<&str> = Vec::new();\n\n for _ in 0..t {\n let mut b = lines.next().unwrap().split(\" \");\n let n: usize = b.next().unwrap().parse().unwrap();\n let m: usize = b.next().unwrap().parse().unwrap();\n\n let mut all_rows: Vec<&str> = (&mut lines).take(n).collect();\n let rows_1st: HashSet<&str> = all_rows\n .iter()\n .map(|s| s.split(\" \").next().unwrap())\n .collect();\n\n let m_cols: Vec<&str> = (&mut lines).take(m).collect();\n let idx_col: Vec<&str> = m_cols\n .iter()\n .find(|s| rows_1st.contains(s.split(\" \").next().unwrap()))\n .unwrap()\n .split(\" \")\n .collect();\n\n let indexer: HashMap<&str, usize> =\n idx_col.iter().enumerate().map(|(a, b)| (*b, a)).collect();\n for idx in 0..all_rows.len() {\n let mut pivot;\n let mut real_idx;\n loop {\n pivot = all_rows[idx].split(\" \").next().unwrap();\n real_idx = indexer.get(pivot).unwrap();\n if *real_idx == idx {\n break;\n } else {\n all_rows[..].swap(idx, *real_idx);\n }\n }\n }\n\n result.append(&mut all_rows);\n }\n\n for r in result {\n println!(\"{}\", r);\n }\n}", "difficulty": "medium"} {"problem_id": "0310", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

How many hours do we have until New Year at M o'clock (24-hour notation) on 30th, December?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤M≤23
  • \n
  • M is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
M\n
\n
\n
\n
\n
\n

Output

If we have x hours until New Year at M o'clock on 30th, December, print x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

21\n
\n
\n
\n
\n
\n

Sample Output 1

27\n
\n

We have 27 hours until New Year at 21 o'clock on 30th, December.

\n
\n
\n
\n
\n
\n

Sample Input 2

12\n
\n
\n
\n
\n
\n

Sample Output 2

36\n
\n
\n
", "c_code": "int solution() {\n\n int m = 0;\n scanf(\"%d\", &m);\n\n printf(\"%d\\n\", 48 - m);\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).expect(\"\");\n let m: i32 = buf.trim().parse().expect(\"\");\n println!(\"{}\", 24 + 24 - m);\n}", "difficulty": "medium"} {"problem_id": "0311", "problem_description": "There is a new attraction in Singapore Zoo: The Infinite Zoo.The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled $$$1,2,3,\\ldots$$$. There is a directed edge from vertex $$$u$$$ to vertex $$$u+v$$$ if and only if $$$u\\&v=v$$$, where $$$\\&$$$ denotes the bitwise AND operation. There are no other edges in the graph.Zookeeper has $$$q$$$ queries. In the $$$i$$$-th query she will ask you if she can travel from vertex $$$u_i$$$ to vertex $$$v_i$$$ by going through directed edges.", "c_code": "int solution() {\n int q;\n\n scanf(\"%d\", &q);\n while (q--) {\n int i;\n int j;\n int yes;\n\n scanf(\"%d%d\", &i, &j);\n yes = 0;\n if (i <= j) {\n yes = 1;\n while (j != 0) {\n if (i == 0 || (i & -i) > (j & -j)) {\n yes = 0;\n break;\n }\n i &= i - 1, j &= j - 1;\n }\n }\n printf(yes ? \"YES\\n\" : \"NO\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() -> std::io::Result<()> {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf)?;\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let q = scan!(usize);\n for _ in 0..q {\n let (u, v) = scan!(usize, usize);\n let mut flag = v >= u;\n\n let mut du = 0;\n let mut dv = 0;\n for i in 0..30 {\n if u & (1 << i) != 0 {\n du += 1;\n }\n if v & (1 << i) != 0 {\n dv += 1;\n }\n\n if dv > du {\n flag = false;\n }\n }\n\n print!(\"{}\\n\", if flag { \"YES\" } else { \"NO\" });\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0312", "problem_description": "

Problem A: AOJ50M

\n\n

Problem

\n

\n AliceとBobが50m走で勝負しています。
\n しかし、この世界においてはAOJのレートが高い方が偉いので、AOJのレートが高い方が勝ちます。
\n どちらか一方でもAOJのレートが無い場合は比べようが無いので、仕方なく50m走のタイムで勝負します。この場合はタイムが短い方を勝ちとします。
\n AOJのレートが同じ場合は引き分け、50m走のタイムで勝負しなければならない場合はタイムが同じなら引き分けです。
\n\n

\n\n

Input

\n

入力は以下の形式で与えられる。

\n
\n$T_1$ $T_2$ $R_1$ $R_2$\n
\n

\n 各要素が空白区切りで与えられる。
\n $T_1,T_2$ はそれぞれAlice、Bobの50m走のタイムを、$R_1,R_2$ はそれぞれAlice、BobのAOJのレートを表す。
\n ただし、$R_1=-1$ のときはAliceのレートがないことを、$R_2=-1$ のときはBobのレートがないことを示す。\n

\n\n

Constraints

\n

入力は以下の条件を満たす。

\n
    \n
  • $1 \\leq T_1,T_2 \\lt 100 $
  • \n
  • $-1 \\leq R_1,R_2 \\lt 2850 $
  • \n
  • 入力は全て整数である
  • \n
\n\n\n\n

Output

\n

\n Aliceが勝つならなら\"Alice\"を、Bobが勝つならなら\"Bob\"を、引き分けであれば\"Draw\"を$1$行に出力せよ。
\n

\n\n

Sample Input 1

\n
\n9 8 1000 999\n
\n\n

Sample Output 1

\n
\nAlice\n
\n\n

\n Aliceの方がAOJのレートが高いため、Aliceの勝ちです。\n

\n\n\n

Sample Input 2

\n
\n9 8 1000 1000\n
\n\n

Sample Output 2

\n
\nDraw\n
\n

\n AliceとBobのAOJのレートが同じため、引き分けです。この場合50m走の結果は関係がないことに注意してください。\n

\n\n\n

Sample Input 3

\n
\n9 8 2849 -1\n
\n\n

Sample Output 3

\n
\nBob\n
\n\n

\n Aliceのレートは高いですが、Bobのレートが無いため50m走のタイムが短いBobの勝ちです。\n

", "c_code": "int solution(void) {\n const char *A = \"Alice\";\n const char *B = \"Bob\";\n const char *D = \"Draw\";\n int T1;\n int T2;\n int R1;\n int R2;\n if (scanf(\"%d%d%d%d\", &T1, &T2, &R1, &R2) != 4) {\n return 1;\n }\n if (R1 < 0 || R2 < 0) {\n puts(T1 < T2 ? A : (T1 > T2 ? B : D));\n } else {\n puts(R1 > R2 ? A : (R1 < R2 ? B : D));\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut buf = String::new();\n let _ = stdin.read_to_string(&mut buf);\n let inputs: Vec = buf.split_whitespace().map(|s| s.parse().unwrap()).collect();\n\n let answer = if inputs[2] >= 0 && inputs[3] >= 0 {\n inputs[2].cmp(&inputs[3])\n } else {\n inputs[1].cmp(&inputs[0])\n };\n\n println!(\n \"{}\",\n match answer {\n Ordering::Less => \"Bob\",\n Ordering::Equal => \"Draw\",\n Ordering::Greater => \"Alice\",\n }\n );\n}", "difficulty": "easy"} {"problem_id": "0313", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

We have N ID cards, and there are M gates.

\n

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

\n

How many of the ID cards allow us to pass all the gates alone?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq M \\leq 10^5
  • \n
  • 1 \\leq L_i \\leq R_i \\leq N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nL_1 R_1\nL_2 R_2\n\\vdots\nL_M R_M\n
\n
\n
\n
\n
\n

Output

Print the number of ID cards that allow us to pass all the gates alone.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 2\n1 3\n2 4\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Two ID cards allow us to pass all the gates alone, as follows:

\n
    \n
  • The first ID card does not allow us to pass the second gate.
  • \n
  • The second ID card allows us to pass all the gates.
  • \n
  • The third ID card allows us to pass all the gates.
  • \n
  • The fourth ID card does not allow us to pass the first gate.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

10 3\n3 6\n5 7\n6 9\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100000 1\n1 100000\n
\n
\n
\n
\n
\n

Sample Output 3

100000\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int m = 0;\n scanf(\"%d %d\\n\", &n, &m);\n int l = 1;\n int r = n;\n int buf1 = 0;\n int buf2 = 0;\n for (int i = 0; i < m; i++) {\n scanf(\"%d %d\", &buf1, &buf2);\n if (buf1 > l) {\n l = buf1;\n }\n if (buf2 < r) {\n r = buf2;\n }\n if (r < l) {\n printf(\"0\\n\");\n return 0;\n }\n }\n printf(\"%d\\n\", r - l + 1);\n return 0;\n}", "rust_code": "fn solution() {\n let mut nm = String::new();\n stdin().read_line(&mut nm).unwrap();\n let n: i32 = nm.split_whitespace().collect::>()[0]\n .parse()\n .unwrap();\n let m: i32 = nm.split_whitespace().collect::>()[1]\n .parse()\n .unwrap();\n\n let mut tl = 0;\n let mut tr = n;\n\n for _i in 1..m + 1 {\n let mut lr = String::new();\n stdin().read_line(&mut lr).unwrap();\n let l: i32 = lr.split_whitespace().collect::>()[0]\n .parse()\n .unwrap();\n let r: i32 = lr.split_whitespace().collect::>()[1]\n .parse()\n .unwrap();\n\n tl = cmp::max(tl, l);\n tr = cmp::min(tr, r);\n }\n println!(\"{}\", cmp::max(0, tr - tl + 1));\n}", "difficulty": "medium"} {"problem_id": "0314", "problem_description": "On a strip of land of length $$$n$$$ there are $$$k$$$ air conditioners: the $$$i$$$-th air conditioner is placed in cell $$$a_i$$$ ($$$1 \\le a_i \\le n$$$). Two or more air conditioners cannot be placed in the same cell (i.e. all $$$a_i$$$ are distinct).Each air conditioner is characterized by one parameter: temperature. The $$$i$$$-th air conditioner is set to the temperature $$$t_i$$$. Example of strip of length $$$n=6$$$, where $$$k=2$$$, $$$a=[2,5]$$$ and $$$t=[14,16]$$$. For each cell $$$i$$$ ($$$1 \\le i \\le n$$$) find it's temperature, that can be calculated by the formula $$$$$$\\min_{1 \\le j \\le k}(t_j + |a_j - i|),$$$$$$where $$$|a_j - i|$$$ denotes absolute value of the difference $$$a_j - i$$$.In other words, the temperature in cell $$$i$$$ is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell $$$i$$$.Let's look at an example. Consider that $$$n=6, k=2$$$, the first air conditioner is placed in cell $$$a_1=2$$$ and is set to the temperature $$$t_1=14$$$ and the second air conditioner is placed in cell $$$a_2=5$$$ and is set to the temperature $$$t_2=16$$$. In that case temperatures in cells are: temperature in cell $$$1$$$ is: $$$\\min(14 + |2 - 1|, 16 + |5 - 1|)=\\min(14 + 1, 16 + 4)=\\min(15, 20)=15$$$; temperature in cell $$$2$$$ is: $$$\\min(14 + |2 - 2|, 16 + |5 - 2|)=\\min(14 + 0, 16 + 3)=\\min(14, 19)=14$$$; temperature in cell $$$3$$$ is: $$$\\min(14 + |2 - 3|, 16 + |5 - 3|)=\\min(14 + 1, 16 + 2)=\\min(15, 18)=15$$$; temperature in cell $$$4$$$ is: $$$\\min(14 + |2 - 4|, 16 + |5 - 4|)=\\min(14 + 2, 16 + 1)=\\min(16, 17)=16$$$; temperature in cell $$$5$$$ is: $$$\\min(14 + |2 - 5|, 16 + |5 - 5|)=\\min(14 + 3, 16 + 0)=\\min(17, 16)=16$$$; temperature in cell $$$6$$$ is: $$$\\min(14 + |2 - 6|, 16 + |5 - 6|)=\\min(14 + 4, 16 + 1)=\\min(18, 17)=17$$$. For each cell from $$$1$$$ to $$$n$$$ find the temperature in it.", "c_code": "int solution() {\n int q;\n scanf(\"%d\", &q);\n while (q--) {\n long long int n;\n long long int k;\n scanf(\"%lld%lld\", &n, &k);\n long long int ans[n];\n long long int temps[k][2];\n\n for (long long int i = 0; i < k; i++) {\n scanf(\"%lld\", &temps[i][0]);\n }\n for (long long int i = 0; i < k; i++) {\n scanf(\"%lld\", &temps[i][1]);\n }\n long long int minIndex;\n long long int minTemp = 1000000009;\n for (long long int i = 0; i < k; i++) {\n if (temps[i][1] < minTemp) {\n minTemp = temps[i][1];\n minIndex = temps[i][0] - 1;\n }\n }\n\n for (long long int i = 0; i < n; i++) {\n ans[i] = 9999999999;\n }\n\n for (long long int i = 0; i < k; i++) {\n ans[temps[i][0] - 1] = temps[i][1];\n }\n\n long long int currentMin = minTemp;\n\n for (long long int i = minIndex - 1; i >= 0; i--) {\n currentMin++;\n\n if (currentMin < ans[i]) {\n ans[i] = currentMin;\n } else {\n currentMin = ans[i];\n }\n }\n\n currentMin = minTemp;\n\n for (long long int i = minIndex + 1; i < n; i++) {\n currentMin++;\n\n if (currentMin < ans[i]) {\n ans[i] = currentMin;\n } else {\n currentMin = ans[i];\n }\n }\n\n currentMin = ans[0];\n for (long long int i = 1; i < n; i++) {\n currentMin++;\n\n if (currentMin < ans[i]) {\n ans[i] = currentMin;\n } else {\n currentMin = ans[i];\n }\n }\n\n currentMin = ans[n - 1];\n for (long long int i = n - 2; i >= 0; i--) {\n currentMin++;\n\n if (currentMin < ans[i]) {\n ans[i] = currentMin;\n } else {\n currentMin = ans[i];\n }\n }\n\n for (long long int i = 0; i < n; i++) {\n printf(\"%lld \", ans[i]);\n }\n printf(\"\\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\n let t = it.next().unwrap();\n\n 'cases: for _case_index in 1..=t {\n lines.next();\n\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 k = it.next().unwrap();\n\n let mut a = Vec::with_capacity(k);\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap() - 1);\n\n a.extend(it);\n\n let mut t = Vec::with_capacity(k);\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n t.extend(it);\n\n let mut z = (0..k).collect::>();\n z.sort_by_key(|&i| t[i] + a[i]);\n z.reverse();\n\n let mut l = None;\n\n for i in 0..n {\n let i = i as i64;\n loop {\n match z.last().cloned() {\n Some(j) if a[j] < i => {\n z.pop();\n if let Some(l0) = l {\n if t[j] - a[j] < t[l0] - a[l0] {\n l = Some(j)\n }\n } else {\n l = Some(j);\n }\n }\n _ => break,\n }\n }\n\n let x = match (l, z.last().cloned()) {\n (Some(l), Some(r)) => std::cmp::min(t[l] + i - a[l], t[r] + a[r] - i),\n (Some(l), _) => t[l] + i - a[l],\n (_, Some(r)) => t[r] + a[r] - i,\n _ => 0,\n };\n\n write!(&mut output, \"{} \", x).unwrap();\n }\n\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0315", "problem_description": "Polycarp doesn't like integers that are divisible by $$$3$$$ or end with the digit $$$3$$$ in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.Polycarp starts to write out the positive (greater than $$$0$$$) integers which he likes: $$$1, 2, 4, 5, 7, 8, 10, 11, 14, 16, \\dots$$$. Output the $$$k$$$-th element of this sequence (the elements are numbered from $$$1$$$).", "c_code": "int solution() {\n int t = 0;\n int i = 0;\n scanf(\"%d\", &t);\n getchar();\n while (i < t) {\n int k = 0;\n scanf(\"%d\", &k);\n getchar();\n int flag = 0;\n int a = 0;\n while (flag < k) {\n a++;\n if (a % 3 != 0 && a % 10 != 3) {\n flag++;\n }\n }\n printf(\"%d\\n\\n\", a);\n i++;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let t = s.trim().parse::().unwrap();\n\n let mut arr: Vec = Vec::new();\n\n for i in 1..2000 {\n if i % 3 != 0 && i % 10 != 3 {\n arr.push(i);\n }\n }\n\n for _ in 0..t {\n s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let k = s.trim().parse::().unwrap();\n println!(\"{}\", arr[k - 1]);\n }\n}", "difficulty": "easy"} {"problem_id": "0316", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

A tetromino is a figure formed by joining four squares edge to edge.\nWe will refer to the following seven kinds of tetromino as I-, O-, T-, J-, L-, S- and Z-tetrominos, respectively:

\n
\n\"a60bcb8e9e8f22e3af51049eda063392.png\"\n
\n

Snuke has many tetrominos. The number of I-, O-, T-, J-, L-, S- and Z-tetrominos in his possession are a_I, a_O, a_T, a_J, a_L, a_S and a_Z, respectively.\nSnuke will join K of his tetrominos to form a rectangle that is two squares tall and 2K squares wide.\nHere, the following rules must be followed:

\n
    \n
  • When placing each tetromino, rotation is allowed, but reflection is not.
  • \n
  • Each square in the rectangle must be covered by exactly one tetromino.
  • \n
  • No part of each tetromino may be outside the rectangle.
  • \n
\n

Snuke wants to form as large a rectangle as possible.\nFind the maximum possible value of K.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0≤a_I,a_O,a_T,a_J,a_L,a_S,a_Z≤10^9
  • \n
  • a_I+a_O+a_T+a_J+a_L+a_S+a_Z≥1
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
a_I a_O a_T a_J a_L a_S a_Z\n
\n
\n
\n
\n
\n

Output

Print the maximum possible value of K. If no rectangle can be formed, print 0.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 1 1 0 0 0 0\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

One possible way to form the largest rectangle is shown in the following figure:

\n
\n\"45515ed2a1dd5e41c5e4ca1f39323d8e.png\"\n
\n
\n
\n
\n
\n
\n

Sample Input 2

0 0 10 0 0 0 0\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

No rectangle can be formed.

\n
\n
", "c_code": "int solution() {\n int i;\n long long a[7];\n long long ans;\n for (i = 0; i < 7; i++) {\n scanf(\"%lld\", &(a[i]));\n }\n if (a[0] % 2 + a[3] % 2 + a[4] % 2 >= 2 && a[0] > 0 && a[3] > 0 && a[4] > 0) {\n ans = 3;\n a[0]--;\n a[3]--;\n a[4]--;\n } else {\n ans = 0;\n }\n ans += a[0] / 2 * 2 + a[1] + a[3] / 2 * 2 + a[4] / 2 * 2;\n printf(\"%lld\\n\", ans);\n fflush(stdout);\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 I: usize = itr.next().unwrap().parse().unwrap();\n let O: usize = itr.next().unwrap().parse().unwrap();\n let _: usize = itr.next().unwrap().parse().unwrap();\n let J: usize = itr.next().unwrap().parse().unwrap();\n let L: usize = itr.next().unwrap().parse().unwrap();\n let _: usize = itr.next().unwrap().parse().unwrap();\n let _: usize = itr.next().unwrap().parse().unwrap();\n\n let mut ans = O + I / 2 * 2 + J / 2 * 2 + L / 2 * 2;\n if I > 0 && J > 0 && L > 0 {\n ans = max(\n ans,\n O + 3 + (I - 1) / 2 * 2 + (J - 1) / 2 * 2 + (L - 1) / 2 * 2,\n );\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0317", "problem_description": "Phoenix has a string $$$s$$$ consisting of lowercase Latin letters. He wants to distribute all the letters of his string into $$$k$$$ non-empty strings $$$a_1, a_2, \\dots, a_k$$$ such that every letter of $$$s$$$ goes to exactly one of the strings $$$a_i$$$. The strings $$$a_i$$$ do not need to be substrings of $$$s$$$. Phoenix can distribute letters of $$$s$$$ and rearrange the letters within each string $$$a_i$$$ however he wants.For example, if $$$s = $$$ baba and $$$k=2$$$, Phoenix may distribute the letters of his string in many ways, such as: ba and ba a and abb ab and ab aa and bb But these ways are invalid: baa and ba b and ba baba and empty string ($$$a_i$$$ should be non-empty) Phoenix wants to distribute the letters of his string $$$s$$$ into $$$k$$$ strings $$$a_1, a_2, \\dots, a_k$$$ to minimize the lexicographically maximum string among them, i. e. minimize $$$max(a_1, a_2, \\dots, a_k)$$$. Help him find the optimal distribution and print the minimal possible value of $$$max(a_1, a_2, \\dots, a_k)$$$.String $$$x$$$ is lexicographically less than string $$$y$$$ if either $$$x$$$ is a prefix of $$$y$$$ and $$$x \\ne y$$$, or there exists an index $$$i$$$ ($$$1 \\le i \\le min(|x|, |y|))$$$ such that $$$x_i$$$ < $$$y_i$$$ and for every $$$j$$$ $$$(1 \\le j < i)$$$ $$$x_j = y_j$$$. Here $$$|x|$$$ denotes the length of the string $$$x$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n int k;\n int i;\n char s[100005];\n int cnt[30];\n int c;\n for (; t > 0; t--) {\n scanf(\"%d %d\", &n, &k);\n scanf(\"%s\", s);\n for (i = 0; i < 30; i++) {\n cnt[i] = 0;\n }\n for (i = 0; s[i] != '\\0'; i++) {\n cnt[s[i] - 'a']++;\n }\n i = 0;\n while (cnt[i] == 0) {\n i++;\n }\n if (cnt[i] < k) {\n k -= cnt[i];\n while (k > 0) {\n i++;\n k -= cnt[i];\n }\n printf(\"%c\\n\", 'a' + i);\n } else {\n printf(\"%c\", 'a' + i);\n cnt[i] -= k;\n c = 0;\n for (i = 0; i < 30; i++) {\n if (cnt[i] > 0) {\n c++;\n }\n }\n if (c == 1) {\n i = 0;\n while (cnt[i] == 0) {\n i++;\n }\n for (; cnt[i] > 0; cnt[i] -= k) {\n printf(\"%c\", 'a' + i);\n }\n } else {\n for (i = 0; i < 30; i++) {\n for (; cnt[i] > 0; cnt[i]--) {\n printf(\"%c\", 'a' + i);\n }\n }\n }\n printf(\"\\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, k) = (xs[0], xs[1]);\n let mut cs: Vec = lines.next().unwrap().unwrap().chars().collect();\n cs.sort();\n if cs[0] != cs[k - 1] || n == k {\n println!(\"{}\", cs[k - 1]);\n } else if cs[k] == cs[n - 1] {\n let mut ans = String::new();\n ans.push(cs[0]);\n let count = (n - k - 1) / k + 1;\n for _ in 0..count {\n ans.push(cs[k]);\n }\n println!(\"{}\", ans);\n } else {\n let mut ans = String::new();\n ans.push(cs[0]);\n let count = n - k;\n for i in 0..count {\n ans.push(cs[k + i]);\n }\n println!(\"{}\", ans);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0318", "problem_description": "Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid.Each test case consists of an integer $$$n$$$ and two arrays $$$a$$$ and $$$b$$$, of size $$$n$$$. If after some (possibly zero) operations described below, array $$$a$$$ can be transformed into array $$$b$$$, the input is said to be valid. Otherwise, it is invalid.An operation on array $$$a$$$ is: select an integer $$$k$$$ $$$(1 \\le k \\le \\lfloor\\frac{n}{2}\\rfloor)$$$ swap the prefix of length $$$k$$$ with the suffix of length $$$k$$$ For example, if array $$$a$$$ initially is $$$\\{1, 2, 3, 4, 5, 6\\}$$$, after performing an operation with $$$k = 2$$$, it is transformed into $$$\\{5, 6, 3, 4, 1, 2\\}$$$.Given the set of test cases, help them determine if each one is valid or invalid.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n int i;\n int j;\n int a[502];\n int b[502];\n int u[502];\n int f;\n int g;\n for (; t > 0; t--) {\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &b[i]);\n }\n for (i = 0; i < n; i++) {\n u[i] = 0;\n }\n f = 0;\n if (n % 2 > 0 && a[n / 2] != b[n / 2]) {\n f++;\n }\n for (i = 0; i < n / 2; i++) {\n g = 0;\n for (j = 0; j < n / 2; j++) {\n if (u[j] == 0) {\n if (a[i] == b[j] && a[n - i - 1] == b[n - j - 1]) {\n u[j]++;\n g++;\n break;\n }\n if (a[i] == b[n - j - 1] && a[n - i - 1] == b[j]) {\n u[j]++;\n g++;\n break;\n }\n }\n }\n if (g == 0) {\n f++;\n }\n }\n if (f > 0) {\n printf(\"No\\n\");\n } else {\n printf(\"Yes\\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 n: 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 ys: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n if n % 2 == 1 && xs[n / 2] != ys[n / 2] {\n println!(\"No\");\n continue;\n }\n let mut m: BTreeMap<(usize, usize), usize> = BTreeMap::new();\n for i in 0..(n / 2) {\n let j = n - i - 1;\n let mut xi = xs[i];\n let mut xj = xs[j];\n if xj < xi {\n swap(&mut xi, &mut xj);\n }\n let v = 1 + m.get(&(xi, xj)).unwrap_or(&0);\n m.insert((xi, xj), v);\n }\n let mut ok = true;\n for i in 0..(n / 2) {\n let j = n - i - 1;\n let mut yi = ys[i];\n let mut yj = ys[j];\n if yj < yi {\n swap(&mut yi, &mut yj);\n }\n let o = m.get(&(yi, yj)).cloned();\n match o {\n None => {\n ok = false;\n break;\n }\n Some(1) => {\n m.remove(&(yi, yj));\n }\n Some(v) => {\n m.insert((yi, yj), v - 1);\n }\n }\n }\n println!(\"{}\", if ok { \"Yes\" } else { \"No\" });\n }\n}", "difficulty": "medium"} {"problem_id": "0319", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:

\n
    \n
  • 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
  • \n
  • T_i = A (T_i is the i-th character of T from the beginning.)
  • \n
  • T_j = B
  • \n
  • T_k = C
  • \n
\n

For example, when T = ABCBC, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.

\n

You are given a string S. Each character of S is A, B, C or ?.

\n

Let Q be the number of occurrences of ? in S. We can make 3^Q strings by replacing each occurrence of ? in S with A, B or C. Find the sum of the ABC numbers of all these strings.

\n

This sum can be extremely large, so print the sum modulo 10^9 + 7.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 ≤ |S| ≤ 10^5
  • \n
  • Each character of S is A, B, C or ?.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

A??C\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

In this case, Q = 2, and we can make 3^Q = 9 strings by by replacing each occurrence of ? with A, B or C. The ABC number of each of these strings is as follows:

\n
    \n
  • AAAC: 0
  • \n
  • AABC: 2
  • \n
  • AACC: 0
  • \n
  • ABAC: 1
  • \n
  • ABBC: 2
  • \n
  • ABCC: 2
  • \n
  • ACAC: 0
  • \n
  • ACBC: 1
  • \n
  • ACCC: 0
  • \n
\n

The sum of these is 0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8, so we print 8 modulo 10^9 + 7, that is, 8.

\n
\n
\n
\n
\n
\n

Sample Input 2

ABCBC\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

When Q = 0, we print the ABC number of S itself, modulo 10^9 + 7. This string is the same as the one given as an example in the problem statement, and its ABC number is 3.

\n
\n
\n
\n
\n
\n

Sample Input 3

????C?????B??????A???????\n
\n
\n
\n
\n
\n

Sample Output 3

979596887\n
\n

In this case, the sum of the ABC numbers of all the 3^Q strings is 2291979612924, and we should print this number modulo 10^9 + 7, that is, 979596887.

\n
\n
", "c_code": "int solution(void) {\n int mod = 1000000007;\n char s[100020];\n scanf(\"%s\", s);\n long long ans[strlen(s) + 1][4];\n ans[strlen(s)][3] = 1, ans[strlen(s)][2] = 0, ans[strlen(s)][1] = 0,\n ans[strlen(s)][0] = 0;\n for (int i = strlen(s) - 1; i >= 0; i--) {\n if (s[i] == '?') {\n ans[i][3] = (ans[i + 1][3] * 3) % mod;\n ans[i][2] = (ans[i + 1][2] * 3 + ans[i + 1][3]) % mod;\n ans[i][1] = (ans[i + 1][1] * 3 + ans[i + 1][2]) % mod;\n ans[i][0] = (ans[i + 1][0] * 3 + ans[i + 1][1]) % mod;\n } else if (s[i] == 'A') {\n ans[i][3] = (ans[i + 1][3]) % mod;\n ans[i][2] = (ans[i + 1][2]) % mod;\n ans[i][1] = (ans[i + 1][1]) % mod;\n ans[i][0] = (ans[i + 1][0] + ans[i + 1][1]) % mod;\n } else if (s[i] == 'B') {\n ans[i][3] = (ans[i + 1][3]) % mod;\n ans[i][2] = (ans[i + 1][2]) % mod;\n ans[i][1] = (ans[i + 1][1] + ans[i + 1][2]) % mod;\n ans[i][0] = (ans[i + 1][0]) % mod;\n } else if (s[i] == 'C') {\n ans[i][3] = (ans[i + 1][3]) % mod;\n ans[i][2] = (ans[i + 1][2] + ans[i + 1][3]) % mod;\n ans[i][1] = (ans[i + 1][1]) % mod;\n ans[i][0] = (ans[i + 1][0]) % mod;\n }\n }\n printf(\"%lld\\n\", ans[0][0] % mod);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s_str: String = String::new();\n io::stdin().read_line(&mut s_str).expect(\"!!\");\n let s: Vec = s_str.trim().chars().collect();\n let N = s.len();\n let mut dp: Vec> = vec![vec![0i64; 4]; N];\n\n let abc = ['A', 'B', 'C'];\n\n for i in (0..N).rev() {\n for j in 0..4 {\n let c = s[i];\n\n if i == N - 1 {\n match j {\n 3 => {\n dp[i][j] = if c == '?' { 3 } else { 1 };\n }\n 2 => {\n dp[i][j] = if c == '?' || c == 'C' { 1 } else { 0 };\n }\n _ => dp[i][j] = 0,\n }\n } else {\n match j {\n 3 => {\n dp[i][j] = if c == '?' { 3 } else { 1 } * dp[i + 1][j];\n }\n _ => {\n if c == abc[j] {\n dp[i][j] = dp[i + 1][j + 1] + dp[i + 1][j];\n } else if c == '?' {\n dp[i][j] = 3 * dp[i + 1][j] + dp[i + 1][j + 1];\n } else {\n dp[i][j] = dp[i + 1][j];\n }\n }\n }\n }\n dp[i][j] %= 10i64.pow(9) + 7;\n }\n }\n println!(\"{}\", dp[0][0]);\n}", "difficulty": "easy"} {"problem_id": "0320", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

\n

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

\n

Find the cost of the Ferris wheel for Takahashi.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 ≤ A ≤ 100
  • \n
  • 2 ≤ B ≤ 1000
  • \n
  • B is an even number.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the cost of the Ferris wheel for Takahashi.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

30 100\n
\n
\n
\n
\n
\n

Sample Output 1

100\n
\n

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

12 100\n
\n
\n
\n
\n
\n

Sample Output 2

50\n
\n

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

\n
\n
\n
\n
\n
\n

Sample Input 3

0 100\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

Takahashi is 0 years old, and he can ride the Ferris wheel for free.

\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n\n if (a <= 12 && a >= 6) {\n b = b / 2;\n } else if (a <= 5) {\n b = 0;\n }\n\n printf(\"%d\", b);\n return 0;\n}", "rust_code": "fn solution() {\n let mut ab = String::new();\n stdin().read_line(&mut ab).unwrap();\n let ab: Vec = ab.split_whitespace().flat_map(str::parse).collect();\n let (a, b) = (ab[0], ab[1]);\n println!(\n \"{}\",\n match a {\n 0..=5 => 0,\n 6..=12 => b / 2,\n _ => b,\n }\n );\n}", "difficulty": "medium"} {"problem_id": "0321", "problem_description": "You are given three integers $$$a$$$, $$$b$$$ and $$$c$$$.Find two positive integers $$$x$$$ and $$$y$$$ ($$$x > 0$$$, $$$y > 0$$$) such that: the decimal representation of $$$x$$$ without leading zeroes consists of $$$a$$$ digits; the decimal representation of $$$y$$$ without leading zeroes consists of $$$b$$$ digits; the decimal representation of $$$gcd(x, y)$$$ without leading zeroes consists of $$$c$$$ digits. $$$gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.Output $$$x$$$ and $$$y$$$. If there are multiple answers, output any of them.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t-- > 0) {\n int a;\n int b;\n int c;\n scanf(\"%d %d %d\", &a, &b, &c);\n long long int x = 1;\n long long int y = 1;\n long long int z = 1;\n for (int i = 0; i < a - 1; i++) {\n x = x * 10;\n }\n for (int i = 0; i < b - 1; i++) {\n y = y * 10;\n }\n for (int i = 0; i < c - 1; i++) {\n z = z * 10;\n }\n printf(\"%lld %lld\\n\", x, y + z);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let t: i64 = line.trim().parse().unwrap();\n\n for _ in 0..t {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let a = words[0];\n let b = words[1];\n let c = words[2];\n\n print!(\"1\");\n for _ in 0..a - 1 {\n print!(\"0\");\n }\n print!(\" \");\n print!(\"1\");\n for _ in 0..b - c {\n print!(\"1\");\n }\n for _ in 0..c - 1 {\n print!(\"0\");\n }\n println!();\n }\n}", "difficulty": "hard"} {"problem_id": "0322", "problem_description": "Shubham has an array $$$a$$$ of size $$$n$$$, and wants to select exactly $$$x$$$ elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.Tell him whether he can do so.", "c_code": "int solution() {\n\n int t = 1;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n;\n int x;\n int k;\n int ev = 0;\n int od = 0;\n int fl = 0;\n scanf(\"%d %d\", &n, &x);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &k);\n\n if (k & 1) {\n od += 1;\n } else {\n ev += 1;\n }\n }\n\n for (int i = 1; i <= od && i <= x; i += 2) {\n int need = x - i;\n\n if (need <= ev) {\n fl = 1;\n break;\n }\n }\n\n if (fl) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n let buf = buf.trim().parse::().unwrap();\n for _ in 0..buf {\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let v: Vec<_> = s\n .trim()\n .split(' ')\n .map(|l| l.trim().parse::().unwrap())\n .collect();\n let x = v[1];\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let v: Vec<_> = s\n .trim()\n .split(' ')\n .map(|l| l.trim().parse::().unwrap())\n .collect();\n let mut ord: u16 = 0;\n for j in v[..x as usize].iter() {\n if (*j & 1) == 1 {\n ord += 1;\n }\n }\n if (ord & 1) == 1 {\n println!(\"Yes\");\n } else {\n if (x as usize) < v.len() {\n let mut b = false;\n if ord == 0 {\n for k in v[x as usize..].iter() {\n if *k & 1 == 1 {\n b = true;\n break;\n }\n }\n } else if ord == x {\n for k in v[x as usize..].iter() {\n if *k & 1 == 0 {\n b = true;\n break;\n }\n }\n } else {\n println!(\"Yes\");\n continue;\n }\n if b {\n println!(\"Yes\")\n } else {\n println!(\"No\")\n }\n } else {\n println!(\"No\");\n }\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0323", "problem_description": "Recently, you found a bot to play \"Rock paper scissors\" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $$$s = s_1 s_2 \\dots s_{n}$$$ of length $$$n$$$ where each letter is either R, S or P.While initializing, the bot is choosing a starting index $$$pos$$$ ($$$1 \\le pos \\le n$$$), and then it can play any number of rounds. In the first round, he chooses \"Rock\", \"Scissors\" or \"Paper\" based on the value of $$$s_{pos}$$$: if $$$s_{pos}$$$ is equal to R the bot chooses \"Rock\"; if $$$s_{pos}$$$ is equal to S the bot chooses \"Scissors\"; if $$$s_{pos}$$$ is equal to P the bot chooses \"Paper\"; In the second round, the bot's choice is based on the value of $$$s_{pos + 1}$$$. In the third round — on $$$s_{pos + 2}$$$ and so on. After $$$s_n$$$ the bot returns to $$$s_1$$$ and continues his game.You plan to play $$$n$$$ rounds and you've already figured out the string $$$s$$$ but still don't know what is the starting index $$$pos$$$. But since the bot's tactic is so boring, you've decided to find $$$n$$$ choices to each round to maximize the average number of wins.In other words, let's suggest your choices are $$$c_1 c_2 \\dots c_n$$$ and if the bot starts from index $$$pos$$$ then you'll win in $$$win(pos)$$$ rounds. Find $$$c_1 c_2 \\dots c_n$$$ such that $$$\\frac{win(1) + win(2) + \\dots + win(n)}{n}$$$ is maximum possible.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n char c = 'A';\n char temp = '0';\n scanf(\"%c\", &temp);\n for (int i = 0; i < n; i++) {\n int p = 0;\n int r = 0;\n int s = 0;\n int x = 0;\n do {\n scanf(\"%c\", &c);\n if (c == 'R') {\n p++;\n x++;\n } else if (c == 'P') {\n s++;\n x++;\n } else if (c == 'S') {\n r++;\n x++;\n }\n } while (c != '\\n');\n if (p >= r && p >= s) {\n for (int j = 0; j < x; j++) {\n printf(\"P\");\n }\n } else if (r >= p && r >= s) {\n for (int j = 0; j < x; j++) {\n printf(\"R\");\n }\n } else {\n for (int j = 0; j < x; j++) {\n printf(\"S\");\n }\n }\n printf(\"\\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 rocks, mut scissors, mut papers) = (0, 0, 0);\n for variant in line.trim().chars() {\n match variant {\n 'R' => rocks += 1,\n 'S' => scissors += 1,\n 'P' => papers += 1,\n _ => (),\n }\n }\n let letter = if rocks > scissors {\n if rocks > papers {\n \"P\"\n } else {\n \"S\"\n }\n } else {\n if scissors > papers {\n \"R\"\n } else {\n \"S\"\n }\n };\n println!(\"{}\", letter.repeat(rocks + scissors + papers));\n count -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "0324", "problem_description": "Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.", "c_code": "int solution() {\n int l[100000] = {0};\n int r[100000] = {0};\n int n = 0;\n scanf(\"%d\", &n);\n int i = 0;\n scanf(\"%d %d\", &l[0], &r[0]);\n int min = 0;\n int max = 0;\n int suml = l[0];\n int sumr = r[0];\n for (i = 1; i < n; i++) {\n scanf(\"%d %d\", &l[i], &r[i]);\n suml += l[i];\n sumr += r[i];\n\n if (l[min] - r[min] > l[i] - r[i]) {\n min = i;\n }\n if (l[max] - r[max] < l[i] - r[i]) {\n max = i;\n }\n }\n\n int ans = 0;\n\n if (abs(suml - sumr + (2 * (r[max] - l[max]))) >=\n abs(suml - sumr + (2 * (r[min] - l[min]))) &&\n (abs(suml - sumr) < abs(suml - sumr + (2 * (r[max] - l[max]))))) {\n ans = max + 1;\n } else if (abs(suml - sumr + (2 * (r[max] - l[max]))) <\n abs(suml - sumr + (2 * (r[min] - l[min]))) &&\n (abs(suml - sumr) < abs(suml - sumr + (2 * (r[min] - l[min]))))) {\n ans = min + 1;\n }\n\n printf(\"%d\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n io::stdin().read_line(&mut input_str).unwrap();\n\n let col_count: u32 = input_str.trim().parse().unwrap();\n let mut max_left_gap = 0;\n let mut max_right_gap = 0;\n let mut max_left_gap_index = 0;\n let mut max_right_gap_index = 0;\n let mut sum_left_start = 0;\n let mut sum_right_start = 0;\n\n for i in 0..col_count {\n input_str.clear();\n io::stdin().read_line(&mut input_str).unwrap();\n let mut input_str_contents = input_str.split_whitespace();\n let left_start_count: i32 = input_str_contents.next().unwrap().parse().unwrap();\n let right_start_count: i32 = input_str_contents.next().unwrap().parse().unwrap();\n\n sum_left_start += left_start_count;\n sum_right_start += right_start_count;\n\n let diff = right_start_count - left_start_count;\n if max_left_gap > diff {\n max_left_gap = diff;\n max_left_gap_index = i + 1;\n } else if max_right_gap < diff {\n max_right_gap = diff;\n max_right_gap_index = i + 1;\n }\n }\n\n let parade_beautifulness = sum_right_start - sum_left_start;\n let answer_variants = [\n parade_beautifulness.abs(),\n (parade_beautifulness - 2 * max_left_gap).abs(),\n (parade_beautifulness - 2 * max_right_gap).abs(),\n ];\n\n let answer = answer_variants.iter().max().unwrap();\n if *answer == answer_variants[0] {\n println!(\"{}\", 0);\n } else if *answer == answer_variants[1] {\n println!(\"{}\", max_left_gap_index);\n } else {\n println!(\"{}\", max_right_gap_index);\n }\n}", "difficulty": "medium"} {"problem_id": "0325", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given positive integers A and B.

\n

If A is a divisor of B, print A + B; otherwise, print B - A.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A \\leq B \\leq 20
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

If A is a divisor of B, print A + B; otherwise, print B - A.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 12\n
\n
\n
\n
\n
\n

Sample Output 1

16\n
\n

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.

\n
\n
\n
\n
\n
\n

Sample Input 2

8 20\n
\n
\n
\n
\n
\n

Sample Output 2

12\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 1\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n

1 is a divisor of 1.

\n
\n
", "c_code": "int solution() {\n int N[2];\n for (int i = 0; i <= 1; i++) {\n scanf(\"%d\", &N[i]);\n }\n\n if (N[1] % N[0] == 0) {\n printf(\"%d\", N[1] + N[0]);\n } else {\n printf(\"%d\", N[1] - N[0]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let vs: Vec = s\n .split_whitespace()\n .map(|token| token.parse().ok().unwrap())\n .collect();\n let a = vs[0];\n let b = vs[1];\n if b % a == 0 {\n println!(\"{}\", a + b);\n } else {\n println!(\"{}\", b - a);\n }\n}", "difficulty": "easy"} {"problem_id": "0326", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Given is an integer N. Find the number of positive integers less than or equal to N that have an odd number of digits (in base ten without leading zeros).

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the number of positive integers less than or equal to N that have an odd number of digits.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

11\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

Among the positive integers less than or equal to 11, nine integers have an odd number of digits: 1, 2, \\ldots, 9.

\n
\n
\n
\n
\n
\n

Sample Input 2

136\n
\n
\n
\n
\n
\n

Sample Output 2

46\n
\n

In addition to 1, 2, \\ldots, 9, another 37 integers also have an odd number of digits: 100, 101, \\ldots, 136.

\n
\n
\n
\n
\n
\n

Sample Input 3

100000\n
\n
\n
\n
\n
\n

Sample Output 3

90909\n
\n
\n
", "c_code": "int solution(void) {\n\n int N = 0;\n int len = 0;\n int count = 0;\n int i = 0;\n scanf(\"%d\", &N);\n char str[10];\n\n for (i = 1; i <= N; i++) {\n sprintf(str, \"%d\", i);\n len = strlen(str);\n if (len % 2 == 1) {\n count++;\n }\n }\n\n printf(\"%d\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n\n let mut buf = String::new();\n stdin.read_line(&mut buf).ok();\n let num: usize = buf.trim().parse().unwrap();\n\n let mut keta = 0;\n\n for i in 1..(num + 1) {\n if i.to_string().len() == 1 || i.to_string().len() == 3 || i.to_string().len() == 5 {\n keta += 1;\n }\n }\n\n println!(\"{}\", keta);\n}", "difficulty": "hard"} {"problem_id": "0327", "problem_description": "Let's call a positive integer $$$n$$$ ordinary if in the decimal notation all its digits are the same. For example, $$$1$$$, $$$2$$$ and $$$99$$$ are ordinary numbers, but $$$719$$$ and $$$2021$$$ are not ordinary numbers.For a given number $$$n$$$, find the number of ordinary numbers among the numbers from $$$1$$$ to $$$n$$$.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n int count = 0;\n int m = n;\n for (int i = 0; m >= 1; i++) {\n m = m / 10;\n count++;\n }\n\n int n_i = 0;\n int n1 = 0;\n for (int i = 0; i < count; i++) {\n n1 += pow(10, i);\n }\n\n n_i += (count - 1) * 9;\n int l = 0;\n for (int i = 0; i <= 9; i++) {\n if (n >= i * n1 && n < (i + 1) * n1) {\n l = i;\n break;\n }\n }\n\n n_i += l;\n printf(\"%d\\n\", n_i);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).expect(\"Failed\");\n let mut t: i32 = t.trim().parse().expect(\"Failed\");\n\n while t != 0 {\n let mut n = String::new();\n io::stdin().read_line(&mut n).expect(\"Failed\");\n let n: i128 = n.trim().parse().expect(\"Failed\");\n\n let mut res = 0;\n let mut pw: i128 = 1;\n while pw <= n {\n let mut d = 1;\n while d <= 9 {\n if pw * d <= n {\n res += 1;\n }\n d += 1;\n }\n\n pw = pw * 10 + 1;\n }\n println!(\"{}\", res);\n\n t -= 1;\n }\n}", "difficulty": "easy"} {"problem_id": "0328", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)

\n

\"

\n

What is the area of this yard excluding the roads? Find it.

\n
\n
\n
\n
\n

Note

\n

It can be proved that the positions of the roads do not affect the area.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • A is an integer between 2 and 100 (inclusive).
  • \n
  • B is an integer between 2 and 100 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

\n

Print the area of this yard excluding the roads (in square yards).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

In this case, the area is 1 square yard.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 7\n
\n
\n
\n
\n
\n

Sample Output 2

24\n
\n

In this case, the area is 24 square yards.

\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n scanf(\"%d %d\", &a, &b);\n printf(\"%d\", (a - 1) * (b - 1));\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut s = String::new();\n stdin.read_line(&mut s).ok();\n s.pop();\n let mut ab: Vec = Vec::new();\n let it = s.split_whitespace();\n for arg in it {\n ab.push(i32::from_str(arg).expect(\"e\"));\n }\n let a = ab[0];\n let b = ab[1];\n println!(\"{}\", (a - 1) * (b - 1));\n}", "difficulty": "medium"} {"problem_id": "0329", "problem_description": "Consider a tunnel on a one-way road. During a particular day, $$$n$$$ cars numbered from $$$1$$$ to $$$n$$$ entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced.Thanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time.Traffic regulations prohibit overtaking inside the tunnel. If car $$$i$$$ overtakes any other car $$$j$$$ inside the tunnel, car $$$i$$$ must be fined. However, each car can be fined at most once.Formally, let's say that car $$$i$$$ definitely overtook car $$$j$$$ if car $$$i$$$ entered the tunnel later than car $$$j$$$ and exited the tunnel earlier than car $$$j$$$. Then, car $$$i$$$ must be fined if and only if it definitely overtook at least one other car.Find the number of cars that must be fined.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int input[n];\n int output[n];\n int index[n + 1];\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &input[i]);\n index[input[i]] = i;\n }\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &output[i]);\n }\n int find = 0;\n int finedcars = 0;\n int i = 0;\n while (i < n && find < n) {\n if (input[i] < 0) {\n i++;\n continue;\n }\n while (input[i] != output[find]) {\n input[index[(output[find])]] = -1;\n output[find] = -1;\n finedcars++;\n find++;\n }\n i++;\n find++;\n }\n printf(\"%d\\n\", finedcars);\n}", "rust_code": "fn solution() -> io::Result<()> {\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n let mut buf = String::new();\n\n stdin.read_line(&mut buf)?;\n\n let n: usize = buf.trim().parse().unwrap();\n\n buf.clear();\n stdin.read_line(&mut buf)?;\n let mut a: Vec = buf\n .split_whitespace()\n .map(|s| s.parse::().unwrap() - 1)\n .collect();\n a.reverse();\n assert_eq!(a.len(), n);\n\n buf.clear();\n stdin.read_line(&mut buf)?;\n let mut b: Vec = buf\n .split_whitespace()\n .map(|s| s.parse::().unwrap() - 1)\n .collect();\n b.reverse();\n assert_eq!(b.len(), n);\n\n let mut bi = vec![0; n];\n for i in 0..n {\n bi[b[i]] = i;\n }\n\n let mut min_bi = bi[a[n - 1]];\n let mut count = 0;\n\n for i in (0..=(n - 1)).rev() {\n let bi_of_a = bi[a[i]];\n\n if bi_of_a > min_bi {\n count += 1;\n }\n min_bi = cmp::min(min_bi, bi_of_a);\n }\n\n println!(\"{}\", count);\n\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "0330", "problem_description": "You are given an array $$$a_1, a_2, \\dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \\le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \\le i < j < k \\le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\\n\", &t);\n while (t-- > 0) {\n int n = 0;\n scanf(\"%d\\n\", &n);\n\n unsigned int a = 0;\n unsigned int b = 0;\n unsigned int c = 0;\n scanf(\"%u\", &a);\n scanf(\"%u\", &b);\n a += b;\n bool found = false;\n for (int i = 3; i <= n; ++i) {\n scanf(\"%u\", &c);\n if (c >= a && !found) {\n scanf(\"\\n\");\n found = true;\n printf(\"1 2 %d\\n\", i);\n }\n }\n if (!found) {\n printf(\"-1\\n\");\n }\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 a: 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 if a[0] + a[1] <= a[a.len() - 1] {\n println!(\"{} {} {}\", 1, 2, a.len());\n } else {\n println!(\"-1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0331", "problem_description": "\n

Score : 66 points

\n
\n
\n

Problem Statement

\n

\n Summer vacation ended at last and the second semester has begun.\n You, a Kyoto University student, came to university and heard a rumor that somebody will barricade the entrance of your classroom.\n The barricade will be built just before the start of the A-th class and removed by Kyoto University students just before the start of the B-th class.\n All the classes conducted when the barricade is blocking the entrance will be cancelled and you will not be able to attend them.\n Today you take N classes and class i is conducted in the t_i-th period.\n You take at most one class in each period.\n Find the number of classes you can attend.\n

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 1000
  • \n
  • 1 \\leq A < B \\leq 10^9
  • \n
  • 1 \\leq t_i \\leq 10^9
  • \n
  • All t_i values are distinct.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

N, A and B are given on the first line and t_i is given on the (i+1)-th line.

\n
\nN A B\nt1\n:\ntN\n
\n
\n
\n
\n
\n

Output

\n

Print the number of classes you can attend.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

\n
\n5 5 9\n4\n3\n6\n9\n1\n
\n
\n
\n
\n
\n

Sample Output 1

\n
\n4\n
\n

You can not only attend the third class.

\n
\n
\n
\n
\n

Sample Input 2

\n
\n5 4 9\n5\n6\n7\n8\n9\n
\n
\n
\n
\n
\n

Sample Output 2

\n
\n1\n
\n

You can only attend the fifth class.

\n
\n
\n
\n
\n

Sample Input 3

\n
\n4 3 6\n9\n6\n8\n1\n
\n
\n
\n
\n
\n

Sample Output 3

\n
\n4\n
\n

You can attend all the classes.

\n
\n
\n
\n
\n

Sample Input 4

\n
\n2 1 2\n1\n2\n
\n
\n
\n
\n
\n

Sample Output 4

\n
\n1\n
\n

You can not attend the first class, but can attend the second.

\n
\n
\n
", "c_code": "int solution() {\n int n;\n int a;\n int b;\n int t;\n int c = 0;\n scanf(\"%d %d %d\", &n, &a, &b);\n while (n--) {\n scanf(\"%d\", &t);\n if (a <= t && t < b) {\n continue;\n }\n c++;\n }\n printf(\"%d\\n\", c);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let a: i32 = s.next().unwrap().parse().unwrap();\n let b: i32 = s.next().unwrap().parse().unwrap();\n let ans = s.by_ref().take(n).fold(0, |s, t| {\n let t = t.parse::().unwrap();\n s + if t < a || t >= b { 1 } else { 0 }\n });\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0332", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N balls in the xy-plane. The coordinates of the i-th of them is (x_i, i).\nThus, we have one ball on each of the N lines y = 1, y = 2, ..., y = N.

\n

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.\nThen, he placed the i-th type-A robot at coordinates (0, i), and the i-th type-B robot at coordinates (K, i).\nThus, now we have one type-A robot and one type-B robot on each of the N lines y = 1, y = 2, ..., y = N.

\n

When activated, each type of robot will operate as follows.

\n
    \n
  • \n

    When a type-A robot is activated at coordinates (0, a), it will move to the position of the ball on the line y = a, collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

    \n
  • \n
  • \n

    When a type-B robot is activated at coordinates (K, b), it will move to the position of the ball on the line y = b, collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

    \n
  • \n
\n

Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq K \\leq 100
  • \n
  • 0 < x_i < K
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Inputs

Input is given from Standard Input in the following format:

\n
N\nK\nx_1 x_2 ... x_N\n
\n
\n
\n
\n
\n

Outputs

Print the minimum possible total distance covered by robots.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1\n10\n2\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

There are just one ball, one type-A robot and one type-B robot.

\n

If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4.

\n

Similarly, if the type-B robot is used, the total distance covered will be 16.

\n

Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n9\n3 6\n
\n
\n
\n
\n
\n

Sample Output 2

12\n
\n

The total distance covered will be minimized when the first ball is collected by the type-A robot, and the second ball by the type-B robot.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n20\n11 12 9 17 12\n
\n
\n
\n
\n
\n

Sample Output 3

74\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n\n int K = 0;\n int x = 0;\n int ans = 0;\n int tmp = 0;\n scanf(\"%d\", &N);\n scanf(\"%d\", &K);\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &x);\n tmp = K - x;\n ans += 2 * ((x > tmp) ? tmp : x);\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let _n: i32 = s.trim().parse().ok().unwrap();\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).ok();\n let k: i32 = t.trim().parse().ok().unwrap();\n let mut u = String::new();\n std::io::stdin().read_line(&mut u).ok();\n let xs: Vec = u\n .split_whitespace()\n .map(|e| e.parse().ok().unwrap())\n .collect();\n\n let mut ans = 0;\n for x in xs {\n ans += std::cmp::min(x, (k - x).abs()) * 2;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0333", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Let f(A, B) be the exclusive OR of A, A+1, ..., B. Find f(A, B).

\n

\nWhat is exclusive OR?

\n

The bitwise exclusive OR of integers c_1, c_2, ..., c_n (let us call it y) is defined as follows:

\n
    \n
  • When y is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if, the number of integers among c_1, c_2, ...c_m whose binary representations have 1 in the 2^k's place, is odd, and 0 if that count is even.
  • \n
\n

For example, the exclusive OR of 3 and 5 is 6. (When written in base two: the exclusive OR of 011 and 101 is 110.)

\n

", "c_code": "int solution() {\n long a;\n long b;\n scanf(\"%ld %ld\", &a, &b);\n long ans = 0;\n if (a & 1 && b & 1) {\n ans = (b - a) / 2 % 2 ^ a;\n } else if (a & 1 && (b & 1) == 0) {\n ans = (b - a - 1) / 2 % 2 ^ a ^ b;\n } else if ((a & 1) == 0 && b & 1) {\n ans = (b - a + 1) / 2 % 2;\n } else {\n ans = (b - a) / 2 % 2 ^ b;\n }\n printf(\"%ld\", ans);\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 a: u64 = itr.next().unwrap().parse().unwrap();\n let b: u64 = itr.next().unwrap().parse().unwrap();\n\n let arem: u64 = a % 4;\n let adiv: u64 = a / 4;\n let brem: u64 = (b + 1) % 4;\n let bdiv: u64 = (b + 1) / 4;\n\n let mut anum: u64 = 0;\n let mut bnum: u64 = 0;\n for i in 0..arem {\n anum ^= adiv * 4 + i;\n }\n for i in 0..brem {\n bnum ^= bdiv * 4 + i;\n }\n println!(\"{}\", anum ^ bnum);\n}", "difficulty": "hard"} {"problem_id": "0334", "problem_description": "

B: 直角三角形

\n \n

問題

\n

\n 直角三角形の斜辺でない $2$ 辺の長さ $A$ , $B$ が与えられる。\n 長さ $A$ の辺は $x$ 軸と重なっており、長さ $B$ の辺は $y$ 軸と重なっている。\n

\n\n

\n 次の操作を行う。\n

\n\n\n\n

\n

    \n
  1. 三角形を $x$ 軸周りに回転させる。
  2. \n
  3. 操作 $1$ を行ってできた図形を $y$ 軸周りに回転させる。
  4. \n
\n

\n\n

\n 操作 $2$ を行ってできた図形の体積を求めよ。\n

\n\n

制約

\n
    \n
  • 入力値は全て整数である。
  • \n
  • $1 < A < B < 1000$
  • \n\t\t
\n\n

入力形式

\n

入力は以下の形式で与えられる。

\n\n

\n $A\\ B$\n

\n\n

出力

\n

図形の体積を出力せよ。また、末尾に改行も出力せよ。なお、 $0.000001$ 未満の絶対誤差または相対誤差が許容される。

\n\n

サンプル

\n\n

サンプル入力 1

\n
\n1 2\n
\n

サンプル出力 1

\n
\n33.510322\n
\n\n

サンプル入力 2

\n
\n7 9\n
\n

サンプル出力 2

\n
\n3053.628059\n
", "c_code": "int solution() {\n int r;\n scanf(\"%*d%d\", &r);\n printf(\"%.9f\\n\", 4 * 3.141592653589 / 3 * r * r * r);\n}", "rust_code": "fn solution() {\n input!(a: f64, b: f64);\n println!(\"{}\", b * b * b * consts::PI * 4.0 / 3.0);\n}", "difficulty": "medium"} {"problem_id": "0335", "problem_description": "

Counting Characters


\n\n

\n Write a program which counts and reports the number of each alphabetical letter. Ignore the case of characters.\n

\n\n\n

Input

\n\n

\n A sentence in English is given in several lines.\n

\n\n

Output

\n\n

\n Prints the number of alphabetical letters in the following format:\n

\n\n
\na : The number of 'a'\nb : The number of 'b'\nc : The number of 'c'\n  .\n  .\nz : The number of 'z'\n
\n\n

Constraints

\n\n
    \n
  • The number of characters in the sentence < 1200
  • \n
\n\n

Sample Input

\n\n
\nThis is a pen.\n
\n\n

Sample Output

\n\n
\na : 1\nb : 0\nc : 0\nd : 0\ne : 1\nf : 0\ng : 0\nh : 1\ni : 2\nj : 0\nk : 0\nl : 0\nm : 0\nn : 1\no : 0\np : 1\nq : 0\nr : 0\ns : 2\nt : 1\nu : 0\nv : 0\nw : 0\nx : 0\ny : 0\nz : 0\n
", "c_code": "int solution() {\n int str[26] = {0};\n int i = 0;\n char word[1205];\n while (scanf(\"%c\", &word[i++]) != EOF) {\n ;\n }\n for (int j = 0; j < i; j++) {\n if (word[j] >= 'A' && word[j] <= 'Z') {\n word[j] += 32;\n }\n if (word[j] >= 'a' && word[j] <= 'z') {\n str[word[j] - 'a']++;\n }\n }\n for (i = 0; i < 26; i++) {\n printf(\"%c : %d\\n\", 'a' + i, str[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n let stdin = io::stdin();\n stdin.lock().read_to_string(&mut buffer).ok();\n\n let s = buffer.trim().to_lowercase();\n\n let alphabets = (b'a'..b'z' + 1).map(|c| c as char);\n let nums = alphabets.map(|a| (a, s.chars().filter(|&c| c == a).count()));\n for num in nums {\n println!(\"{} : {}\", num.0, num.1);\n }\n}", "difficulty": "medium"} {"problem_id": "0336", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given strings S and T consisting of lowercase English letters.

\n

You can perform the following operation on S any number of times:

\n

Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.

\n

Determine if S and T can be made equal by performing the operation zero or more times.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 2 \\times 10^5
  • \n
  • |S| = |T|
  • \n
  • S and T consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nT\n
\n
\n
\n
\n
\n

Output

If S and T can be made equal, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

azzel\napple\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

azzel can be changed to apple, as follows:

\n
    \n
  • Choose e as c_1 and l as c_2. azzel becomes azzle.
  • \n
  • Choose z as c_1 and p as c_2. azzle becomes apple.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

chokudai\nredcoder\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

No sequences of operation can change chokudai to redcoder.

\n
\n
\n
\n
\n
\n

Sample Input 3

abcdefghijklmnopqrstuvwxyz\nibyhqfrekavclxjstdwgpzmonu\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution(void) {\n char S[200001];\n char T[200001];\n memset(&S, '\\0', sizeof(S));\n memset(&T, '\\0', sizeof(T));\n scanf(\"%s\", S);\n scanf(\"%s\", T);\n\n int size = strlen(S);\n\n char alpha[26];\n memset(&alpha, '\\0', sizeof(alpha));\n int flag = 0;\n for (int i = 0; i < size; i++) {\n if (alpha[T[i] - 'a'] == '\\0') {\n alpha[T[i] - 'a'] = S[i];\n } else if (alpha[T[i] - 'a'] != S[i]) {\n printf(\"No\\n\");\n return 0;\n }\n for (int j = 0; j < 26; j++) {\n if (alpha[j] == S[i]) {\n flag++;\n }\n if (flag > 1) {\n printf(\"No\\n\");\n return 0;\n }\n }\n flag = 0;\n }\n\n printf(\"Yes\\n\");\n return 0;\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 let t = get().as_bytes();\n\n if s.len() != t.len() {\n println!(\"No\");\n return;\n }\n\n use std::collections::HashMap;\n\n let mut hs = HashMap::new();\n let mut ht = HashMap::new();\n\n for i in 0..s.len() {\n let cs = s[i];\n let x = hs.len();\n let p = *hs.entry(cs).or_insert(x);\n let ct = t[i];\n let y = ht.len();\n let q = *ht.entry(ct).or_insert(y);\n if p != q {\n println!(\"No\");\n return;\n }\n }\n\n println!(\"Yes\");\n}", "difficulty": "medium"} {"problem_id": "0337", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

There are three positive integers A, B and C written on a blackboard. E869120 performs the following operation K times:

\n
    \n
  • Choose one integer written on the blackboard and let the chosen integer be n. Replace the chosen integer with 2n.
  • \n
\n

What is the largest possible sum of the integers written on the blackboard after K operations?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • A, B and C are integers between 1 and 50 (inclusive).
  • \n
  • K is an integer between 1 and 10 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
A B C\nK\n
\n
\n
\n
\n
\n

Output

\n

Print the largest possible sum of the integers written on the blackboard after K operations by E869220.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3 11\n1\n
\n
\n
\n
\n
\n

Sample Output 1

30\n
\n

In this sample, 5, 3, 11 are initially written on the blackboard, and E869120 can perform the operation once.
\nThere are three choices:

\n
    \n
  1. Double 5: The integers written on the board after the operation are 10, 3, 11.
  2. \n
  3. Double 3: The integers written on the board after the operation are 5, 6, 11.
  4. \n
  5. Double 11: The integers written on the board after the operation are 5, 3, 22.
  6. \n
\n

If he chooses 3., the sum of the integers written on the board afterwards is 5 + 3 + 22 = 30, which is the largest among 1. through 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3 4\n2\n
\n
\n
\n
\n
\n

Sample Output 2

22\n
\n

E869120 can perform the operation twice. The sum of the integers eventually written on the blackboard is maximized as follows:

\n
    \n
  • First, double 4. The integers written on the board are now 3, 3, 8.
  • \n
  • Next, double 8. The integers written on the board are now 3, 3, 16.
  • \n
\n

Then, the sum of the integers eventually written on the blackboard is 3 + 3 + 16 = 22.

\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int c = 0;\n int k = 0;\n int total = 0;\n scanf(\"%d %d %d\", &a, &b, &c);\n scanf(\"%d\", &k);\n\n for (int i = 0; i < k; i++) {\n if (a > b) {\n if (a > c) {\n a = 2 * a;\n } else {\n c = 2 * c;\n }\n }\n\n else {\n if (b > c) {\n b = 2 * b;\n }\n\n else {\n c = 2 * c;\n }\n }\n }\n\n total = a + b + c;\n printf(\"%d\", total);\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 buf = buf.split_whitespace();\n let mut a: u32 = buf.next().unwrap().parse().unwrap();\n let mut b: u32 = buf.next().unwrap().parse().unwrap();\n let mut c: u32 = buf.next().unwrap().parse().unwrap();\n\n let mut k = String::new();\n io::stdin().read_line(&mut k).unwrap();\n let k: u32 = k.trim().parse().unwrap();\n\n for _ in 0..k {\n let max = cmp::max(cmp::max(a, b), c);\n if max == a {\n a *= 2;\n } else if max == b {\n b *= 2;\n } else {\n c *= 2;\n }\n }\n\n println!(\"{}\", a + b + c);\n}", "difficulty": "easy"} {"problem_id": "0338", "problem_description": "A string is called bracket sequence if it does not contain any characters other than \"(\" and \")\". A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters \"+\" and \"1\" into this sequence. For example, \"\", \"(())\" and \"()()\" are RBS and \")(\" and \"(()\" are not.We can see that each opening bracket in RBS is paired with some closing bracket, and, using this fact, we can define nesting depth of the RBS as maximum number of bracket pairs, such that the $$$2$$$-nd pair lies inside the $$$1$$$-st one, the $$$3$$$-rd one — inside the $$$2$$$-nd one and so on. For example, nesting depth of \"\" is $$$0$$$, \"()()()\" is $$$1$$$ and \"()((())())\" is $$$3$$$.Now, you are given RBS $$$s$$$ of even length $$$n$$$. You should color each bracket of $$$s$$$ into one of two colors: red or blue. Bracket sequence $$$r$$$, consisting only of red brackets, should be RBS, and bracket sequence, consisting only of blue brackets $$$b$$$, should be RBS. Any of them can be empty. You are not allowed to reorder characters in $$$s$$$, $$$r$$$ or $$$b$$$. No brackets can be left uncolored.Among all possible variants you should choose one that minimizes maximum of $$$r$$$'s and $$$b$$$'s nesting depth. If there are multiple solutions you can print any of them.", "c_code": "int solution() {\n int n;\n int i;\n scanf(\"%d \", &n);\n char bracket_sequence[n];\n int opening_bracket[n];\n int closing_bracket[n];\n for (i = 0; i < n; i++) {\n scanf(\"%c\", &bracket_sequence[i]);\n opening_bracket[i] = closing_bracket[i] = 0;\n }\n int value = 0;\n for (i = 0; i < n; i++) {\n if (bracket_sequence[i] == '(') {\n value++;\n opening_bracket[i] = value % 2;\n } else {\n value--;\n }\n }\n value = 0;\n for (i = n - 1; i >= 0; i--) {\n if (bracket_sequence[i] == ')') {\n value++;\n closing_bracket[i] = value % 2;\n } else {\n value--;\n }\n }\n for (i = 0; i < n; i++) {\n if (bracket_sequence[i] == '(') {\n printf(\"%d\", opening_bracket[i]);\n } else {\n printf(\"%d\", closing_bracket[i]);\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\n let n: usize = arr.get(0).expect(\"n\").parse::().expect(\"n\");\n\n let brackets: Vec = arr.get(1).unwrap().chars().map(|i| i == '(').collect();\n\n assert_eq!(brackets.len(), n);\n\n let mut bracket_level = 0;\n\n let ans = brackets\n .iter()\n .map(|i| {\n if *i {\n bracket_level += 1;\n if bracket_level & 0b1 == 1 {\n 0u32\n } else {\n 1u32\n }\n } else {\n bracket_level -= 1;\n if bracket_level & 0b1 == 0 {\n 0u32\n } else {\n 1u32\n }\n }\n })\n .collect::>();\n\n for i in ans {\n print!(\"{}\", i);\n }\n println!();\n}", "difficulty": "easy"} {"problem_id": "0339", "problem_description": "Arkady's morning seemed to be straight of his nightmare. He overslept through the whole morning and, still half-asleep, got into the tram that arrived the first. Some time after, leaving the tram, he realized that he was not sure about the line number of the tram he was in.During his ride, Arkady woke up several times and each time he saw the tram stopping at some stop. For each stop he knows which lines of tram stop there. Given this information, can you help Arkady determine what are the possible lines of the tram he was in?", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n int a[n][105];\n int b[n];\n for (int i = 0; i < n; i++) {\n\n scanf(\"%d\", &b[i]);\n for (int j = 0; j < b[i]; j++) {\n\n scanf(\"%d\", &a[i][j]);\n }\n }\n int c = 0;\n int ans = 0;\n int flag = 0;\n for (int i = 0; i < b[0]; i++) {\n for (int j = 1; j < n; j++) {\n for (int k = 0; k < b[j]; k++) {\n if (a[0][i] == a[j][k]) {\n flag = 1;\n break;\n }\n }\n if (flag == 1) {\n c++;\n flag = 0;\n }\n }\n if (c == n - 1) {\n printf(\"%d \", a[0][i]);\n }\n c = 0;\n }\n printf(\"\\n\");\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\n let n = get!();\n\n let mut xs = vec![0; 101];\n\n for _ in 0..n {\n let r = get!();\n for _ in 0..r {\n let p = get!(usize);\n xs[p] += 1;\n }\n }\n\n let xs: Vec<_> = xs\n .into_iter()\n .enumerate()\n .filter(|(_, x)| *x == n)\n .collect();\n\n for i in 0..xs.len() {\n if i > 0 {\n print!(\" \");\n }\n print!(\"{}\", xs[i].0);\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "0340", "problem_description": "

Range

\n\n

\n Write a program which reads three integers a, b and c, and prints \"Yes\" if a < b < c, otherwise \"No\".\n

\n\n\n

Input

\n\n

\n Three integers a, b and c separated by a single space are given in a line.\n

\n\n

Output

\n\n

\n Print \"Yes\" or \"No\" in a line.\n

\n\n

Constraints

\n\n
    \n
  • 0 ≤ a, b, c ≤ 100
  • \n
\n\n

Sample Input 1

\n
\n1 3 8\n
\n

Sample Output 1

\n
\nYes\n
\n\n

Sample Input 2

\n
\n3 8 1\n
\n

Sample Output 2

\n
\nNo\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int c = 0;\n\n printf(\"\");\n scanf(\"%d %d %d\", &a, &b, &c);\n\n while (1) {\n if (a >= 0 && a <= 100 && b >= 0 && b <= 100 && c >= 0 && c <= 100) {\n break;\n }\n printf(\"\");\n scanf(\"%d %d\", &a, &b);\n }\n\n if (a < b && b < c) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n\n let mut line = String::new();\n\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n\n let a: i32 = vec[0].parse().unwrap_or(0);\n let b: i32 = vec[1].parse().unwrap_or(0);\n let c: i32 = vec[2].parse().unwrap_or(0);\n\n if a < b && b < c {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0341", "problem_description": "You are a coach of a group consisting of $$$n$$$ students. The $$$i$$$-th student has programming skill $$$a_i$$$. All students have distinct programming skills. You want to divide them into teams in such a way that: No two students $$$i$$$ and $$$j$$$ such that $$$|a_i - a_j| = 1$$$ belong to the same team (i.e. skills of each pair of students in the same team have the difference strictly greater than $$$1$$$); the number of teams is the minimum possible. You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int ans = 1;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < n; k++) {\n if (abs(a[j] - a[k]) == 1) {\n ans = 2;\n break;\n }\n }\n }\n\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n\n let mut val = String::new();\n\n io::stdin()\n .read_line(&mut val)\n .expect(\"Не удалось прочитать строку\");\n\n let val: u32 = val.trim().parse().expect(\"Пожалуйста, введите число!\");\n\n for _ in 0..val {\n let mut val = String::new();\n io::stdin()\n .read_line(&mut val)\n .expect(\"Не удалось прочитать строку\");\n\n let mut numbers: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|s| s.trim())\n .filter(|s| !s.is_empty())\n .map(|s| s.parse().unwrap())\n .collect();\n\n let length = numbers.len();\n numbers.sort();\n let mut flag = false;\n for i in 0..length - 1 {\n let difference = numbers[i + 1] - numbers[i];\n if difference == 1 {\n println!(\"2\");\n flag = true;\n break;\n }\n }\n if !flag {\n println!(\"1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0342", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi is standing on a two-dimensional plane, facing north. Find the minimum positive integer K such that Takahashi will be at the starting position again after he does the following action K times:

\n
    \n
  • Go one meter in the direction he is facing. Then, turn X degrees counter-clockwise.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq X \\leq 179
  • \n
  • X is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

Print the number of times Takahashi will do the action before he is at the starting position again.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

90\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

Takahashi's path is a square.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n
\n
\n
\n
\n
\n

Sample Output 2

360\n
\n
\n
", "c_code": "int solution(void) {\n int x = 0;\n int k = 0;\n int sumx = 0;\n\n scanf(\"%d\", &x);\n do {\n sumx += x;\n k++;\n } while (sumx % 360);\n\n printf(\"%d\", k);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin()\n .read_line(&mut buf)\n .expect(\"failed to read\");\n let x: u32 = buf.trim().parse().unwrap();\n\n let mut i: u32 = 1;\n\n loop {\n if (360 * i).is_multiple_of(x) {\n println!(\"{}\", 360 * i / x);\n break;\n } else {\n i += 1;\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0343", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Given are two strings S and T.

\n

Let us change some of the characters in S so that T will be a substring of S.

\n

At least how many characters do we need to change?

\n

Here, a substring is a consecutive subsequence. For example, xxx is a substring of yxxxy, but not a substring of xxyxx.

\n
\n
\n
\n
\n

Constraints

    \n
  • The lengths of S and T are each at least 1 and at most 1000.
  • \n
  • The length of T is at most that of S.
  • \n
  • S and T consist of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nT\n
\n
\n
\n
\n
\n

Output

Print the minimum number of characters in S that need to be changed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

cabacc\nabc\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

For example, changing the fourth character a in S to c will match the second through fourth characters in S to T.

\n

Since S itself does not have T as its substring, this number of changes - one - is the minimum needed.

\n
\n
\n
\n
\n
\n

Sample Input 2

codeforces\natcoder\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
", "c_code": "int solution(void) {\n char s[1001] = {'\\0'};\n char t[1001] = {'\\0'};\n int l_s = 0;\n int l_t = 0;\n int max_match = 0;\n scanf(\"%s\", s);\n scanf(\"%s\", t);\n while (s[l_s] != '\\0') {\n l_s++;\n }\n while (t[l_t] != '\\0') {\n l_t++;\n }\n for (int i = 0; i < l_s - l_t + 1; i++) {\n int match = 0;\n for (int j = 0; j < l_t; j++) {\n if (s[i + j] == t[j]) {\n match++;\n }\n }\n if (match > max_match) {\n max_match = match;\n }\n }\n printf(\"%d\\n\", l_t - max_match);\n return 0;\n}", "rust_code": "fn solution() -> Result<()> {\n let mut s = String::new();\n let ls = io::stdin().read_line(&mut s)? - 1;\n let mut t = String::new();\n let lt = io::stdin().read_line(&mut t)? - 1;\n\n let mut m = 1001;\n for i in 0..=(ls - lt) {\n let mut res = 0;\n for (x, y) in t.chars().zip(s[i..i + lt].chars()) {\n if x != y {\n res += 1;\n }\n }\n m = min(m, res);\n }\n println!(\"{}\", m);\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0344", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given N positive integers a_1, a_2, ..., a_N.

\n

For a non-negative integer m, let f(m) = (m\\ mod\\ a_1) + (m\\ mod\\ a_2) + ... + (m\\ mod\\ a_N).

\n

Here, X\\ mod\\ Y denotes the remainder of the division of X by Y.

\n

Find the maximum value of f.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 3000
  • \n
  • 2 \\leq a_i \\leq 10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the maximum value of f.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n3 4 6\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

f(11) = (11\\ mod\\ 3) + (11\\ mod\\ 4) + (11\\ mod\\ 6) = 10 is the maximum value of f.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n7 46 11 20 11\n
\n
\n
\n
\n
\n

Sample Output 2

90\n
\n
\n
\n
\n
\n
\n

Sample Input 3

7\n994 518 941 851 647 2 581\n
\n
\n
\n
\n
\n

Sample Output 3

4527\n
\n
\n
", "c_code": "int solution() {\n int N = 0;\n scanf(\"%d\", &N);\n long sum = 0;\n for (int i = 0; i < N; i++) {\n int j = 0;\n scanf(\"%d\", &j);\n sum += j - 1;\n }\n printf(\"%ld\\n\", sum);\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\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 n: usize = iter.next().unwrap().parse().unwrap();\n let a: Vec = (&mut iter).take(n).map(|x| x.parse().unwrap()).collect();\n\n println!(\"{}\", a.iter().fold(0, |acc, &x| acc + x - 1));\n}", "difficulty": "hard"} {"problem_id": "0345", "problem_description": "Sometimes some words like \"localization\" or \"internationalization\" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.Thus, \"localization\" will be spelt as \"l10n\", and \"internationalization» will be spelt as \"i18n\".You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.", "c_code": "int solution() {\n char a[10000][10000];\n int n = 0;\n\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", &a[i][10000]);\n }\n\n for (int i = 0; i <= n; i++) {\n int m = strlen(a[i]);\n if (m > 10) {\n printf(\"%c%d%c\\n\", a[i][0], (m - 2), a[i][m - 1]);\n } else {\n printf(\"%s\\n\", a[i]);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input).expect(\"\");\n\n let mut n = input.trim().parse::().unwrap();\n\n while n > 0 {\n let mut word = String::new();\n io::stdin().read_line(&mut word).expect(\"\");\n if word.len() > 12 {\n println!(\n \"{}{}{}\",\n &word[0..1],\n word.len() - 4,\n &word[word.len() - 3..word.len() - 1]\n );\n } else {\n println!(\"{}\", &word[..word.len() - 1])\n }\n n -= 1;\n }\n}", "difficulty": "hard"} {"problem_id": "0346", "problem_description": "Did you know you can download more RAM? There is a shop with $$$n$$$ different pieces of software that increase your RAM. The $$$i$$$-th RAM increasing software takes $$$a_i$$$ GB of memory to run (temporarily, once the program is done running, you get the RAM back), and gives you an additional $$$b_i$$$ GB of RAM (permanently). Each software can only be used once. Your PC currently has $$$k$$$ GB of RAM.Note that you can't use a RAM-increasing software if it takes more GB of RAM to use than what you currently have.Since RAM is the most important thing in the world, you wonder, what is the maximum possible amount of RAM achievable?", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n while (n--) {\n int m = 0;\n int ram = 0;\n int a[1001] = {0};\n int b[1001] = {0};\n int c = 0;\n scanf(\"%d%d\", &m, &ram);\n for (int i = 1; i <= m; ++i) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 1; i <= m; ++i) {\n scanf(\"%d\", &c);\n b[a[i]] += c;\n }\n for (int i = 1; i <= 1000; ++i) {\n if (ram >= i) {\n ram = ram + b[i];\n }\n }\n printf(\"%d\\n\", ram);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n let mut input = input.split_ascii_whitespace().flat_map(str::parse);\n let t = input.next().unwrap();\n let mut output = String::new();\n for _ in 0..t {\n let n = input.next().unwrap();\n let mut k = input.next().unwrap();\n let mut ab: Vec<_> = input.by_ref().take(n).map(|ai| (ai, 0)).collect();\n input\n .by_ref()\n .take(n)\n .zip(&mut ab)\n .for_each(|(bi, (_, b))| *b = bi);\n ab.sort_unstable_by_key(|&(a, _)| a);\n for (ai, bi) in ab {\n if ai <= k {\n k += bi;\n }\n }\n writeln!(output, \"{}\", k).unwrap();\n }\n print!(\"{}\", output);\n}", "difficulty": "hard"} {"problem_id": "0347", "problem_description": "There is a special offer in Vasya's favourite supermarket: if the customer buys $$$a$$$ chocolate bars, he or she may take $$$b$$$ additional bars for free. This special offer can be used any number of times.Vasya currently has $$$s$$$ roubles, and he wants to get as many chocolate bars for free. Each chocolate bar costs $$$c$$$ roubles. Help Vasya to calculate the maximum possible number of chocolate bars he can get!", "c_code": "int solution() {\n int t;\n long long int price;\n long long int offer;\n long long int mon;\n long long int free;\n long long int div;\n long long int ctr = 0;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%lld%lld%lld%lld\", &mon, &offer, &free, &price);\n ctr = 0;\n div = (mon / (offer * price));\n ctr += (div * (free + offer));\n mon = mon % (price * offer);\n ctr += (mon / price);\n mon = 0;\n printf(\"%lld\\n\", ctr);\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut buf = String::new();\n stdin().read_line(&mut buf)?;\n let times = buf.trim().parse::().unwrap();\n\n for _ in 0..times {\n buf.clear();\n stdin().read_line(&mut buf)?;\n let arr = Vec::from_iter(buf.split_whitespace());\n let mut b = arr[0].parse::().unwrap() / arr[3].parse::().unwrap();\n let a = b / arr[1].parse::().unwrap();\n b += a * arr[2].parse::().unwrap();\n println!(\"{}\", b);\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0348", "problem_description": "Binary insertion sort", "c_code": "void solution(int *arr, int size) {\n for (int i = 1; i < size; i++) {\n int key = arr[i];\n int low = 0;\n int high = i - 1;\n int index;\n\n while (low <= high) {\n int mid = low + ((high - low) / 2);\n\n if (arr[mid] == key) {\n index = mid + 1;\n break;\n }\n if (arr[mid] < key) {\n low = mid + 1;\n } else {\n high = mid - 1;\n }\n }\n\n if (low > high) {\n index = low;\n }\n\n int j = i - 1;\n while (j >= index) {\n arr[j + 1] = arr[j];\n j--;\n }\n\n arr[j + 1] = key;\n }\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n let len = arr.len();\n\n for i in 1..len {\n let key = arr[i].clone();\n\n let mut low = 0;\n let mut high = i;\n\n while low < high {\n let mid = low + (high - low) / 2;\n\n if arr[mid] < key {\n low = mid + 1;\n } else {\n high = mid;\n }\n }\n\n arr[low..=i].rotate_right(1);\n arr[low] = key;\n }\n}\n", "difficulty": "easy"} {"problem_id": "0349", "problem_description": "You are given two integers $$$l$$$ and $$$r$$$, where $$$l < r$$$. We will add $$$1$$$ to $$$l$$$ until the result is equal to $$$r$$$. Thus, there will be exactly $$$r-l$$$ additions performed. For each such addition, let's look at the number of digits that will be changed after it.For example: if $$$l=909$$$, then adding one will result in $$$910$$$ and $$$2$$$ digits will be changed; if you add one to $$$l=9$$$, the result will be $$$10$$$ and $$$2$$$ digits will also be changed; if you add one to $$$l=489999$$$, the result will be $$$490000$$$ and $$$5$$$ digits will be changed. Changed digits always form a suffix of the result written in the decimal system.Output the total number of changed digits, if you want to get $$$r$$$ from $$$l$$$, adding $$$1$$$ each time.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int l[100000];\n int r[100000];\n for (int i = 0; i < t; i++) {\n scanf(\"%d%d\", &l[i], &r[i]);\n }\n for (int i = 0; i < t; i++) {\n int ans = 0;\n while (l[i] != 0 || r[i] != 0) {\n ans += r[i] - l[i];\n l[i] /= 10;\n r[i] /= 10;\n }\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let mut nv: Vec = Vec::new();\n let t = s.trim().parse().unwrap();\n for _aerfresdf in 0..t {\n s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let v = s.split_whitespace();\n let mut foo = true;\n let mut l: usize = 0;\n let mut r: usize = 0;\n for i in v {\n if foo {\n l = i.parse().unwrap();\n foo = false;\n } else {\n r = i.parse().unwrap();\n }\n }\n let mut ans: usize = 0;\n while l > 0 || r > 0 {\n ans += r - l;\n r /= 10;\n l /= 10;\n }\n nv.push(ans);\n }\n for i in nv {\n println!(\"{}\", i);\n }\n}", "difficulty": "easy"} {"problem_id": "0350", "problem_description": "Bob is playing with $$$6$$$-sided dice. A net of such standard cube is shown below.He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.For example, the number of visible pips on the tower below is $$$29$$$ — the number visible on the top is $$$1$$$, from the south $$$5$$$ and $$$3$$$, from the west $$$4$$$ and $$$2$$$, from the north $$$2$$$ and $$$4$$$ and from the east $$$3$$$ and $$$5$$$.The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.Bob also has $$$t$$$ favourite integers $$$x_i$$$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $$$x_i$$$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n long long int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n\n for (int j = 0; j < n; j++) {\n long long int x = (a[j] / 14);\n if (0 < a[j] && a[j] < 7) {\n printf(\"NO\\n\");\n continue;\n }\n if (((14 * x) - 13) <= a[j] && a[j] <= (((14 * x) - 8))) {\n printf(\"YES\\n\");\n }\n if ((14 * (x + 1) - 13) <= a[j] && a[j] <= ((14 * (x + 1) - 8))) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n let _lucky_number_count = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n\n let numbers: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .filter(|s| !s.is_empty())\n .map(|s| s.parse::().unwrap())\n .collect();\n\n let result: Vec = numbers\n .iter()\n .map(|x| {\n if x > &14u64 && x % 14 > 0 && x % 14 < 7 {\n \"YES\".to_string()\n } else {\n \"NO\".to_string()\n }\n })\n .collect();\n\n for val in result {\n println!(\"{}\", val);\n }\n}", "difficulty": "easy"} {"problem_id": "0351", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:

\n
    \n
  • The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)
  • \n
  • Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):
      \n
    • R points for winning with Rock;
    • \n
    • S points for winning with Scissors;
    • \n
    • P points for winning with Paper.
    • \n
    \n
  • \n
  • However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.)
  • \n
\n

Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.

\n

The information Takahashi obtained is given as a string T. If the i-th character of T (1 \\leq i \\leq N) is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.

\n

What is the maximum total score earned in the game by adequately choosing the hand to play in each round?

\n
\n
\n
\n
\n

Notes

In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.

\n
    \n
  • If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;
  • \n
  • if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;
  • \n
  • if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;
  • \n
  • if both players play the same hand, it is a draw.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq K \\leq N-1
  • \n
  • 1 \\leq R,S,P \\leq 10^4
  • \n
  • N,K,R,S, and P are all integers.
  • \n
  • |T| = N
  • \n
  • T consists of r, p, and s.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nR S P\nT\n
\n
\n
\n
\n
\n

Output

Print the maximum total score earned in the game.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2\n8 7 6\nrsrpr\n
\n
\n
\n
\n
\n

Sample Output 1

27\n
\n

The machine will play {Rock, Scissors, Rock, Paper, Rock}.

\n

We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.\nWe cannot earn more points, so the answer is 27.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 1\n100 10 1\nssssppr\n
\n
\n
\n
\n
\n

Sample Output 2

211\n
\n
\n
\n
\n
\n
\n

Sample Input 3

30 5\n325 234 123\nrspsspspsrpspsppprpsprpssprpsr\n
\n
\n
\n
\n
\n

Sample Output 3

4996\n
\n
\n
", "c_code": "int solution() {\n int N;\n int K;\n int R;\n int S;\n int P;\n char T[100000];\n char *ptr;\n int point;\n\n scanf(\"%d %d\", &N, &K);\n scanf(\"%d %d %d\", &R, &S, &P);\n scanf(\"%s\", T);\n\n ptr = T;\n\n for (int i = 0; i < N; i++) {\n switch (*ptr) {\n case 'r':\n if (i >= K && *(ptr - K) == 'r') {\n T[i] = 'n';\n break;\n }\n point += P;\n break;\n case 's':\n if (i >= K && *(ptr - K) == 's') {\n T[i] = 'n';\n break;\n }\n point += R;\n break;\n case 'p':\n if (i >= K && *(ptr - K) == 'p') {\n T[i] = 'n';\n break;\n }\n point += S;\n break;\n }\n ptr++;\n }\n\n printf(\"%d\", point);\n\n return 0;\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 let k: usize = iter.next().unwrap().parse().unwrap();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n let r: usize = iter.next().unwrap().parse().unwrap();\n let s: usize = iter.next().unwrap().parse().unwrap();\n let p: usize = iter.next().unwrap().parse().unwrap();\n\n let mut tt = String::new();\n std::io::stdin().read_line(&mut tt).unwrap();\n let t: Vec = tt.chars().collect();\n\n let mut ans = 0usize;\n\n for i in 0..k {\n let mut rsp_cont_cnt = 0usize;\n for j in 0..(n / k + 1) {\n let now = i + j * k;\n if now > n - 1 {\n break;\n }\n\n let rsp = t[now];\n\n ans += if rsp_cont_cnt % 2 == 1 {\n 0\n } else {\n match rsp {\n 'r' => p,\n 's' => r,\n _ => s,\n }\n };\n\n let rsp_next = if now + k > n - 1 { '!' } else { t[now + k] };\n if rsp == rsp_next {\n rsp_cont_cnt += 1;\n } else {\n rsp_cont_cnt = 0;\n }\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0352", "problem_description": "This is the easy version of the problem. The difference is that in this version the array can not contain zeros. You can make hacks only if both versions of the problem are solved.You are given an array $$$[a_1, a_2, \\ldots a_n]$$$ consisting of integers $$$-1$$$ and $$$1$$$. You have to build a partition of this array into the set of segments $$$[l_1, r_1], [l_2, r_2], \\ldots, [l_k, r_k]$$$ with the following property: Denote the alternating sum of all elements of the $$$i$$$-th segment as $$$s_i$$$: $$$s_i$$$ = $$$a_{l_i} - a_{l_i+1} + a_{l_i+2} - a_{l_i+3} + \\ldots \\pm a_{r_i}$$$. For example, the alternating sum of elements of segment $$$[2, 4]$$$ in array $$$[1, 0, -1, 1, 1]$$$ equals to $$$0 - (-1) + 1 = 2$$$. The sum of $$$s_i$$$ over all segments of partition should be equal to zero. Note that each $$$s_i$$$ does not have to be equal to zero, this property is about sum of $$$s_i$$$ over all segments of partition.The set of segments $$$[l_1, r_1], [l_2, r_2], \\ldots, [l_k, r_k]$$$ is called a partition of the array $$$a$$$ of length $$$n$$$ if $$$1 = l_1 \\le r_1, l_2 \\le r_2, \\ldots, l_k \\le r_k = n$$$ and $$$r_i + 1 = l_{i+1}$$$ for all $$$i = 1, 2, \\ldots k-1$$$. In other words, each element of the array must belong to exactly one segment.You have to build a partition of the given array with properties described above or determine that such partition does not exist.Note that it is not required to minimize the number of segments in the partition.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int sum = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n if (n % 2 != 0) {\n printf(\"-1\\n\");\n continue;\n }\n for (int i = 0; i < n; i += 2) {\n if (a[i] == a[i + 1]) {\n sum++;\n } else {\n sum += 2;\n }\n }\n printf(\"%d\\n\", sum);\n for (int i = 0; i < n; i += 2) {\n if (a[i] == a[i + 1]) {\n printf(\"%d %d\\n\", i + 1, i + 2);\n } else {\n printf(\"%d %d\\n%d %d\\n\", i + 1, i + 1, i + 2, i + 2);\n }\n }\n }\n}", "rust_code": "fn solution() -> std::io::Result<()> {\n let mut buf = String::new();\n let sin = std::io::stdin();\n sin.read_line(&mut buf)?;\n let mut t = buf.trim().parse::().unwrap();\n\n let sout = std::io::stdout().lock();\n let mut sout = std::io::BufWriter::new(sout);\n let mut res = Vec::new();\n while t > 0 {\n buf.clear();\n sin.read_line(&mut buf).unwrap();\n let n = buf.trim().parse::().unwrap();\n\n buf.clear();\n sin.read_line(&mut buf).unwrap();\n let a = buf\n .trim()\n .split(' ')\n .map(|ch| ch.parse::().unwrap())\n .collect::>();\n let sum = a.iter().sum::();\n if sum & 1 != 0 {\n writeln!(&mut sout, \"-1\")?;\n } else {\n let mut cnt = 0;\n for i in (0..n).step_by(2) {\n if a[i] == a[i + 1] {\n writeln!(&mut res, \"{} {}\", i + 1, i + 2)?;\n cnt += 1;\n } else {\n writeln!(&mut res, \"{} {}\\n{} {}\", i + 1, i + 1, i + 2, i + 2)?;\n\n cnt += 2;\n }\n }\n\n write!(&mut sout, \"{cnt}\\n{}\", std::str::from_utf8(&res).unwrap())?;\n res.clear();\n }\n\n t -= 1;\n }\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "0353", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

As a New Year's gift, Dolphin received a string s of length 19.
\nThe string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters].
\nDolphin wants to convert the comma-separated string s into a space-separated string.
\nWrite a program to perform the conversion for him.

\n
\n
\n
\n
\n

Constraints

    \n
  • The length of s is 19.
  • \n
  • The sixth and fourteenth characters in s are ,.
  • \n
  • The other characters in s are lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Print the string after the conversion.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

happy,newyear,enjoy\n
\n
\n
\n
\n
\n

Sample Output 1

happy newyear enjoy\n
\n

Replace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy.

\n
\n
\n
\n
\n
\n

Sample Input 2

haiku,atcoder,tasks\n
\n
\n
\n
\n
\n

Sample Output 2

haiku atcoder tasks\n
\n
\n
\n
\n
\n
\n

Sample Input 3

abcde,fghihgf,edcba\n
\n
\n
\n
\n
\n

Sample Output 3

abcde fghihgf edcba\n
\n
\n
", "c_code": "int solution() {\n char *v1 = malloc(5 * sizeof(char));\n char *v2 = malloc(7 * sizeof(char));\n char *v3 = malloc(5 * sizeof(char));\n scanf(\"%5[^,],%7[^,],%5[^,]\", v1, v2, v3);\n printf(\"%s %s %s\\n\", v1, v2, v3);\n free(v1);\n free(v2);\n free(v3);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n\n println!(\"{}\", s.replace(\",\", \" \"));\n}", "difficulty": "hard"} {"problem_id": "0354", "problem_description": "This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is \"11001111\", it will be divided into \"11\", \"00\" and \"1111\". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so \"11001111\" is good. Another example, if $$$s$$$ is \"1110011000\", it will be divided into \"111\", \"00\", \"11\" and \"000\", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, \"1110011000\" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \\leq i \\leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?", "c_code": "int solution() {\n long t;\n scanf(\"%ld\", &t);\n while (t--) {\n long n;\n long ans = 0;\n long con = 1;\n scanf(\"\\n%ld\", &n);\n char str[n];\n scanf(\"%s\", str);\n short f = 0;\n for (long a = 1; a < n; a++) {\n if (str[a] == str[a - 1]) {\n con++;\n } else {\n if (con % 2 == 1 && f == 0) {\n f = 1;\n } else if (con % 2 == 1 && f == 1) {\n f = 0;\n }\n con = 1;\n if (f) {\n ans++;\n }\n }\n }\n printf(\"%ld\\n\", ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut inputs = String::new();\n stdin.lock().read_to_string(&mut inputs).ok();\n\n let mut scanner = inputs.split_whitespace();\n\n let case = scanner.next().unwrap().parse::().unwrap();\n\n for _ in 0..case {\n let _ = scanner.next();\n let mut line = scanner.next().unwrap().chars();\n\n let mut elem = line.next().unwrap();\n let mut result = 0;\n let mut cnt = 1;\n for i in line {\n if elem != i {\n if cnt % 2 != 0 {\n cnt = 2;\n result += 1;\n } else {\n cnt = 1;\n }\n elem = i;\n } else {\n cnt += 1;\n }\n }\n println!(\"{result}\");\n }\n}", "difficulty": "medium"} {"problem_id": "0355", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Snuke lives at position x on a number line.\nOn this line, there are two stores A and B, respectively at position a and b, that offer food for delivery.

\n

Snuke decided to get food delivery from the closer of stores A and B.\nFind out which store is closer to Snuke's residence.

\n

Here, the distance between two points s and t on a number line is represented by |s-t|.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq x \\leq 1000
  • \n
  • 1 \\leq a \\leq 1000
  • \n
  • 1 \\leq b \\leq 1000
  • \n
  • x, a and b are pairwise distinct.
  • \n
  • The distances between Snuke's residence and stores A and B are different.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
x a b\n
\n
\n
\n
\n
\n

Output

If store A is closer, print A; if store B is closer, print B.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2 7\n
\n
\n
\n
\n
\n

Sample Output 1

B\n
\n

The distances between Snuke's residence and stores A and B are 3 and 2, respectively.\nSince store B is closer, print B.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 999 1000\n
\n
\n
\n
\n
\n

Sample Output 2

A\n
\n
\n
", "c_code": "int solution(void) {\n int x = 0;\n int a = 0;\n int b = 0;\n int disA = 0;\n int disB = 0;\n\n scanf(\"%d %d %d\", &x, &a, &b);\n disA = x - a;\n if (disA < 0) {\n disA *= -1;\n }\n disB = x - b;\n if (disB < 0) {\n disB *= -1;\n }\n if (disA < disB) {\n printf(\"A\\n\");\n } else {\n printf(\"B\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let v: Vec = s\n .split_whitespace()\n .map(|e| e.parse().ok().unwrap())\n .collect();\n let x = v[0];\n let a = v[1];\n let b = v[2];\n\n if (a - x).abs() < (b - x).abs() {\n println!(\"A\");\n } else {\n println!(\"B\");\n }\n}", "difficulty": "medium"} {"problem_id": "0356", "problem_description": "According to a new ISO standard, a flag of every country should have a chequered field n × m, each square should be of one of 10 colours, and the flag should be «striped»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland's government asked you to find out whether their flag meets the new ISO standard.", "c_code": "int solution() {\n int i = 0;\n int j = 0;\n int k = 0;\n int m = 0;\n int n = 0;\n int aux = 0;\n int var = 0;\n\n scanf(\"%d %d\", &n, &m);\n char arr[n][m];\n\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n scanf(\" %c\", &arr[i][j]);\n }\n }\n\n for (j = 0; j < m; j++) {\n for (k = 1; k < m; k++) {\n for (i = 0; i < n; i++) {\n if (arr[i][j] == arr[i][k]) {\n aux += 1;\n }\n }\n }\n }\n for (i = 0; i < n; i++) {\n if (arr[i][0] != arr[i + 1][0]) {\n var += 0;\n } else {\n var += 1;\n }\n }\n\n if (aux == n * (m - 1) * (m) && var == 0) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n let mut line_words = line.split_whitespace();\n let n: u8 = line_words.next().unwrap().parse().unwrap();\n\n let mut last_color = '*';\n let mut res = true;\n for _ in 0..n {\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let mut chars = line.trim().chars();\n let c = chars.next().unwrap();\n if last_color == c {\n res = false;\n break;\n }\n for v in chars {\n if v != c {\n res = false;\n break;\n }\n }\n last_color = c;\n }\n if res {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "hard"} {"problem_id": "0357", "problem_description": "Polycarp has spent the entire day preparing problems for you. Now he has to sleep for at least $$$a$$$ minutes to feel refreshed.Polycarp can only wake up by hearing the sound of his alarm. So he has just fallen asleep and his first alarm goes off in $$$b$$$ minutes.Every time Polycarp wakes up, he decides if he wants to sleep for some more time or not. If he's slept for less than $$$a$$$ minutes in total, then he sets his alarm to go off in $$$c$$$ minutes after it is reset and spends $$$d$$$ minutes to fall asleep again. Otherwise, he gets out of his bed and proceeds with the day.If the alarm goes off while Polycarp is falling asleep, then he resets his alarm to go off in another $$$c$$$ minutes and tries to fall asleep for $$$d$$$ minutes again.You just want to find out when will Polycarp get out of his bed or report that it will never happen.Please check out the notes for some explanations of the example.", "c_code": "int solution(void) {\n int t;\n int i;\n scanf(\"%d\", &t);\n long long a[t];\n long long b[t];\n long long c[t];\n long long d[t];\n long long r[t];\n for (i = 0; i < t; ++i) {\n scanf(\"%lld%lld%lld%lld\", &a[i], &b[i], &c[i], &d[i]);\n }\n for (i = 0; i < t; ++i) {\n if (a[i] <= b[i]) {\n r[i] = b[i];\n } else {\n if (c[i] <= d[i]) {\n r[i] = -1;\n } else {\n int diff = c[i] - d[i];\n int cn;\n if ((a[i] - b[i]) % diff == 0) {\n cn = (a[i] - b[i]) / diff;\n } else {\n cn = (a[i] - b[i]) / diff + 1;\n }\n\n r[i] = b[i] + cn * (d[i] + diff);\n }\n }\n }\n for (i = 0; i < t; ++i) {\n printf(\"%lld\\n\", r[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).unwrap();\n let t: u16 = t.trim().parse().unwrap();\n\n let mut output: Vec = Vec::new();\n\n for _ in 0..t {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let mut iter = input.split_whitespace();\n let a = iter.next().unwrap();\n let b = iter.next().unwrap();\n let c = iter.next().unwrap();\n let d = iter.next().unwrap();\n\n let a: i64 = a.parse().unwrap();\n let b: i64 = b.parse().unwrap();\n let c: i64 = c.parse().unwrap();\n let d: i64 = d.parse().unwrap();\n\n if a > b && c <= d {\n output.push(-1);\n } else if a <= b {\n output.push(b);\n } else {\n let time_remaining: f64 = (a - b) as f64;\n let sleep_per_alarm: f64 = (c - d) as f64;\n\n let times_we_need_to_sleep: i64;\n\n if (b + (((time_remaining / sleep_per_alarm).ceil() as i64) * c)) > a {\n times_we_need_to_sleep = (time_remaining / sleep_per_alarm).ceil() as i64;\n } else {\n times_we_need_to_sleep = ((time_remaining / sleep_per_alarm) + 1.0).ceil() as i64;\n }\n\n output.push(b + (times_we_need_to_sleep * c));\n }\n }\n\n for number in output {\n if number < -1 {\n println!(\"{}\", -1);\n } else {\n println!(\"{}\", number);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0358", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have a rectangular parallelepiped of size A×B×C, built with blocks of size 1×1×1. Snuke will paint each of the A×B×C blocks either red or blue, so that:

\n
    \n
  • There is at least one red block and at least one blue block.
  • \n
  • The union of all red blocks forms a rectangular parallelepiped.
  • \n
  • The union of all blue blocks forms a rectangular parallelepiped.
  • \n
\n

Snuke wants to minimize the difference between the number of red blocks and the number of blue blocks. Find the minimum possible difference.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≤A,B,C≤10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print the minimum possible difference between the number of red blocks and the number of blue blocks.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3 3\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

For example, Snuke can paint the blocks as shown in the diagram below.\nThere are 9 red blocks and 18 blue blocks, thus the difference is 9.

\n

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2 4\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

For example, Snuke can paint the blocks as shown in the diagram below.\nThere are 8 red blocks and 8 blue blocks, thus the difference is 0.

\n

\n
\n
\n
\n
\n
\n

Sample Input 3

5 3 5\n
\n
\n
\n
\n
\n

Sample Output 3

15\n
\n

For example, Snuke can paint the blocks as shown in the diagram below.\nThere are 45 red blocks and 30 blue blocks, thus the difference is 9.

\n

\n
\n
", "c_code": "int solution(void) {\n long long int red = 0;\n long long int blue = 0;\n long long int high = 0;\n long long int lengh = 0;\n long long int deeper = 0;\n scanf(\"%lld %lld %lld\", &high, &lengh, &deeper);\n\n if (high <= lengh) {\n if (lengh <= deeper) {\n red = high * lengh * (deeper / 2 + deeper % 2);\n blue = high * lengh * (deeper / 2);\n } else {\n red = high * (lengh / 2 + lengh % 2) * deeper;\n blue = high * (lengh / 2) * deeper;\n }\n } else if (high <= deeper) {\n if (deeper <= lengh) {\n red = high * (lengh / 2 + lengh % 2) * deeper;\n blue = high * (lengh / 2) * deeper;\n } else {\n red = high * lengh * (deeper / 2 + deeper % 2);\n blue = high * lengh * (deeper / 2);\n }\n } else {\n if (deeper <= high) {\n red = (high / 2 + high % 2) * lengh * deeper;\n blue = (high / 2) * lengh * deeper;\n } else {\n red = high * lengh * (deeper / 2 + deeper % 2);\n blue = high * lengh * (deeper / 2);\n }\n }\n\n printf(\"%lld\\n\", red - blue);\n\n return 0;\n}", "rust_code": "fn solution() {\n let (A, B, C): (i64, i64, 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\n let ans = if A % 2 == 0 || B % 2 == 0 || C % 2 == 0 {\n 0\n } else {\n let mut x = [A, B, C];\n x.sort();\n x[0] * x[1]\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0359", "problem_description": "Emuskald needs a fence around his farm, but he is too lazy to build it himself. So he purchased a fence-building robot.He wants the fence to be a regular polygon. The robot builds the fence along a single path, but it can only make fence corners at a single angle a.Will the robot be able to build the fence Emuskald wants? In other words, is there a regular polygon which angles are equal to a?", "c_code": "int solution()\n\n{\n int n;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if ((360 % (180 - a[i])) == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n\n io::stdin().read_line(&mut t).expect(\"read error\");\n\n let t: usize = t.trim().parse().expect(\"parse error\");\n\n for _i in 0..t {\n let mut a = String::new();\n\n io::stdin().read_line(&mut a).expect(\"read error\");\n\n let a: usize = a.trim().parse().expect(\"parse error\");\n\n if 360 % (180 - a) == 0 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0360", "problem_description": "Madoka's father just reached $$$1$$$ million subscribers on Mathub! So the website decided to send him a personalized award — The Mathhub's Bit Button! The Bit Button is a rectangular table with $$$n$$$ rows and $$$m$$$ columns with $$$0$$$ or $$$1$$$ in each cell. After exploring the table Madoka found out that: A subrectangle $$$A$$$ is contained in a subrectangle $$$B$$$ if there's no cell contained in $$$A$$$ but not contained in $$$B$$$. Two subrectangles intersect if there is a cell contained in both of them. A subrectangle is called black if there's no cell with value $$$0$$$ inside it. A subrectangle is called nice if it's black and it's not contained in another black subrectangle. The table is called elegant if there are no two nice intersecting subrectangles.For example, in the first illustration the red subrectangle is nice, but in the second one it's not, because it's contained in the purple subrectangle. Help Madoka to determine whether the table is elegant.", "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 int a[n][m];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n scanf(\"%1d\", &a[i][j]);\n }\n }\n int f = 1;\n for (int i = 0; f && i < n - 1; ++i) {\n for (int j = 0; f && j < m - 1; ++j) {\n int sum = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];\n if (sum == 3) {\n f = 0;\n }\n }\n }\n printf(f ? \"YES\\n\" : \"NO\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n let stdout = io::stdout();\n let stdout_handle = stdout.lock();\n let mut out_buffer = BufWriter::with_capacity(65536, stdout_handle);\n\n let mut line = String::new();\n stdin.read_line(&mut line).unwrap();\n let test_cases: usize = line.trim().parse().unwrap();\n\n let mut table: Vec = Vec::new();\n for _t in 0..test_cases {\n table.clear();\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let mut rows_columns = line.split_ascii_whitespace();\n let row_count: usize = rows_columns.next().unwrap().parse().unwrap();\n let column_count: usize = rows_columns.next().unwrap().parse().unwrap();\n table.reserve(row_count * column_count);\n\n for _i in 0..row_count {\n line.clear();\n stdin.read_line(&mut line).unwrap();\n table.extend(line.trim().as_bytes().iter().map(|x| x - b'0'));\n }\n\n let mut is_elegant: bool = true;\n 'table_iter: for row in 0..(row_count - 1) {\n for column in 0..(column_count - 1) {\n let i = row * column_count + column;\n if table[i] + table[i + 1] + table[i + column_count] + table[i + column_count + 1]\n == 3\n {\n is_elegant = false;\n break 'table_iter;\n }\n }\n }\n\n if is_elegant {\n writeln!(&mut out_buffer, \"YES\").unwrap();\n } else {\n writeln!(&mut out_buffer, \"NO\").unwrap();\n }\n }\n\n out_buffer.flush().unwrap();\n}", "difficulty": "medium"} {"problem_id": "0361", "problem_description": "You have $$$n$$$ gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The $$$i$$$-th gift consists of $$$a_i$$$ candies and $$$b_i$$$ oranges.During one move, you can choose some gift $$$1 \\le i \\le n$$$ and do one of the following operations: eat exactly one candy from this gift (decrease $$$a_i$$$ by one); eat exactly one orange from this gift (decrease $$$b_i$$$ by one); eat exactly one candy and exactly one orange from this gift (decrease both $$$a_i$$$ and $$$b_i$$$ by one). Of course, you can not eat a candy or orange if it's not present in the gift (so neither $$$a_i$$$ nor $$$b_i$$$ can become less than zero).As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: $$$a_1 = a_2 = \\dots = a_n$$$ and $$$b_1 = b_2 = \\dots = b_n$$$ (and $$$a_i$$$ equals $$$b_i$$$ is not necessary).Your task is to find the minimum number of moves required to equalize all the given gifts.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long int count = 0;\n int n;\n scanf(\"%d\", &n);\n long long int a[n];\n long long int b[n];\n scanf(\"%lld\", &a[0]);\n long long int min_a = a[0];\n for (int i = 1; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n if (min_a > a[i]) {\n min_a = a[i];\n }\n }\n scanf(\"%lld\", &b[0]);\n long long int min_b = b[0];\n for (int i = 1; i < n; i++) {\n scanf(\"%lld\", &b[i]);\n if (min_b > b[i]) {\n min_b = b[i];\n }\n }\n for (int i = 0; i < n; i++) {\n int dif1 = a[i] - min_a;\n int dif2 = b[i] - min_b;\n count = count + ((dif1 >= dif2) ? dif1 : dif2);\n }\n printf(\"%lld\\n\", count);\n }\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 a: 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 b: 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 = 0;\n let min_a = a.iter().min().unwrap();\n let min_b = b.iter().min().unwrap();\n for i in 0..n {\n let x = a[i] - min_a;\n let y = b[i] - min_b;\n ans += cmp::min(x, y) + (x - y).abs();\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0362", "problem_description": "Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip — n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.", "c_code": "int solution() {\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n int a[n];\n int b[n];\n int i = 0;\n int j;\n int t;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b[i] = a[i];\n }\n for (i = 0; i < n; i++) {\n for (j = i + 1; j < n; j++) {\n if (a[i] < a[j]) {\n t = a[i];\n a[i] = a[j];\n a[j] = t;\n }\n }\n }\n\n printf(\"%d\\n\", a[k - 1]);\n\n int l = a[k - 1];\n\n int u = 0;\n for (i = 0; i < n; i++) {\n if (b[i] >= l) {\n printf(\"%d \", i + 1);\n u++;\n if (u == k) {\n break;\n }\n }\n }\n}", "rust_code": "fn solution() {\n let input = File::open(\"input.txt\").unwrap();\n let mut output = File::create(\"output.txt\").unwrap();\n\n let mut iter = io::BufReader::new(input).lines();\n\n let tmp: Vec = iter\n .next()\n .unwrap()\n .ok()\n .unwrap()\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let n = tmp[0];\n let k = tmp[1];\n\n let v: Vec = iter\n .next()\n .unwrap()\n .ok()\n .unwrap()\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut t: Vec<(usize, i32)> = v.into_iter().enumerate().collect();\n t.sort_by_key(|tuple| tuple.1);\n t = t.drain((n - k)..).collect();\n\n writeln!(output, \"{}\", t[0].1).unwrap();\n\n t.sort();\n let s: String = t\n .into_iter()\n .map(|(idx, _)| (idx + 1).to_string() + \" \")\n .collect();\n\n writeln!(output, \"{}\", s.trim()).unwrap();\n}", "difficulty": "hard"} {"problem_id": "0363", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There are N cards. The i-th card has an integer A_i written on it.\nFor any two cards, the integers on those cards are different.

\n

Using these cards, Takahashi and Aoki will play the following game:

\n
    \n
  • Aoki chooses an integer x.
  • \n
  • Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:
      \n
    • Takahashi should take the card with the largest integer among the remaining card.
    • \n
    • Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.
    • \n
    \n
  • \n
  • The game ends when there is no card remaining.
  • \n
\n

You are given Q candidates for the value of x: X_1, X_2, ..., X_Q.\nFor each i (1 \\leq i \\leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 100 000
  • \n
  • 1 \\leq Q \\leq 100 000
  • \n
  • 1 \\leq A_1 < A_2 < ... < A_N \\leq 10^9
  • \n
  • 1 \\leq X_i \\leq 10^9 (1 \\leq i \\leq Q)
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N Q\nA_1 A_2 ... A_N\nX_1\nX_2\n:\nX_Q\n
\n
\n
\n
\n
\n

Output

Print Q lines. The i-th line (1 \\leq i \\leq Q) should contain the answer for x = X_i.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 5\n3 5 7 11 13\n1\n4\n9\n10\n13\n
\n
\n
\n
\n
\n

Sample Output 1

31\n31\n27\n23\n23\n
\n

For example, when x = X_3(= 9), the game proceeds as follows:

\n
    \n
  • Takahashi takes the card with 13.
  • \n
  • Aoki takes the card with 7.
  • \n
  • Takahashi takes the card with 11.
  • \n
  • Aoki takes the card with 5.
  • \n
  • Takahashi takes the card with 3.
  • \n
\n

Thus, 13 + 11 + 3 = 27 should be printed on the third line.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 3\n10 20 30 40\n2\n34\n34\n
\n
\n
\n
\n
\n

Sample Output 2

70\n60\n60\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int Q;\n int A[100001];\n scanf(\"%d %d\", &N, &Q);\n for (i = 1; i <= N; i++) {\n scanf(\"%d\", &(A[i]));\n }\n\n int q;\n int l[2];\n int r[2];\n int m;\n int X;\n int L;\n int R;\n int M;\n long long sum[2][100002] = {};\n for (i = N, sum[0][N + 1] = 0; i > N / 2; i--) {\n sum[0][i] = sum[0][i + 1] + A[i];\n }\n for (i = 3, sum[1][1] = A[1]; i <= N; i += 2) {\n sum[1][i] = sum[1][i - 2] + A[i];\n }\n for (i = 2, sum[1][0] = 0; i <= N; i += 2) {\n sum[1][i] = sum[1][i - 2] + A[i];\n }\n for (q = 1; q <= Q; q++) {\n scanf(\"%d\", &X);\n if (X <= A[1]) {\n printf(\"%lld\\n\", sum[0][(N / 2) + 1]);\n continue;\n }\n if (X >= A[N - 1]) {\n printf(\"%lld\\n\", sum[1][N]);\n continue;\n }\n\n L = 0;\n R = (X - A[1] < A[N - 1] - X) ? A[N - 1] - X : X - A[1];\n while (L < R) {\n M = (L + R) / 2;\n\n l[0] = 0;\n r[0] = N;\n while (l[0] < r[0]) {\n m = (l[0] + r[0] + 1) / 2;\n if (A[m] < X - M) {\n l[0] = m;\n } else {\n r[0] = m - 1;\n }\n }\n l[1] = l[0];\n r[1] = N;\n while (l[1] < r[1]) {\n m = (l[1] + r[1] + 1) / 2;\n if (A[m] > X + M) {\n r[1] = m - 1;\n } else {\n l[1] = m;\n }\n }\n\n if (r[1] - l[0] == N - r[1]) {\n break;\n }\n if (r[1] - l[0] == N - r[1] - 1) {\n l[0]--;\n break;\n }\n if (r[1] - l[0] == N - r[1] + 1 && A[l[0] + 1] == X - M &&\n A[r[1]] == X + M) {\n l[0]--;\n r[1]--;\n break;\n } else if (r[1] - l[0] < N - r[1])\n L = M + 1;\n else\n R = M;\n }\n\n if (L == R) {\n l[0] = 0;\n r[0] = N;\n while (l[0] < r[0]) {\n m = (l[0] + r[0] + 1) / 2;\n if (A[m] < X - L) {\n l[0] = m;\n } else {\n r[0] = m - 1;\n }\n }\n l[1] = l[0];\n r[1] = N;\n while (l[1] < r[1]) {\n m = (l[1] + r[1] + 1) / 2;\n if (A[m] > X + L) {\n r[1] = m - 1;\n } else {\n l[1] = m;\n }\n }\n if (r[1] - l[0] == N - r[1] - 1) {\n l[0]--;\n } else if (r[1] - l[0] == N - r[1] + 1 && A[l[0] + 1] == X - L &&\n A[r[1]] == X + L) {\n l[0]--;\n r[1]--;\n }\n }\n\n if (l[0] < 0) {\n l[0] = 0;\n }\n printf(\"%lld\\n\", sum[0][r[1] + 1] + sum[1][l[0]]);\n }\n\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n q: usize,\n a: [usize; n],\n xs: [usize; q]\n }\n let mut ac: Vec = vec![0; n + 1];\n for i in 0..n {\n ac[i + 1] = ac[i] + a[i];\n }\n let mut eo: Vec = vec![0; n];\n eo[0] = a[0];\n eo[1] = a[1];\n for i in 2..n {\n eo[i] = eo[i - 2] + a[i];\n }\n for i in 0..n {\n a[i] *= 3;\n }\n for i in 0..q {\n xs[i] = xs[i] * 3 - 1;\n }\n for i in 0..q {\n if xs[i] + 1 >= a[n - 1] {\n println!(\"{}\", eo[n - 1]);\n continue;\n }\n let mut l = 0;\n let mut r = 3000000000;\n let mut li = 0;\n let mut ri = 0;\n while l + 1 < r {\n let m = (l + r) / 2;\n let lt = if xs[i] < a[0] + m {\n 0\n } else {\n let mut l2 = -1;\n let mut r2 = n as i64 - 1;\n while l2 + 1 < r2 {\n let m2 = (l2 + r2 + 1) / 2;\n if a[m2 as usize] < xs[i] - m {\n l2 = m2;\n } else {\n r2 = m2;\n }\n }\n r2 as usize\n };\n let rt = {\n let mut l2 = 0;\n let mut r2 = n;\n while l2 + 1 < r2 {\n let m2 = (l2 + r2) / 2;\n if a[m2] <= xs[i] + m {\n l2 = m2;\n } else {\n r2 = m2;\n }\n }\n l2\n };\n if rt + 1 - lt <= n - 1 - rt {\n l = m;\n li = lt;\n ri = rt;\n } else {\n r = m;\n }\n }\n let mut ans = ac[n] - ac[ri + 1];\n if li > 0 && ri + 1 - li == n - 1 - ri {\n ans += eo[li - 1];\n } else if li > 1 && ri + 2 - li == n - 1 - ri {\n ans += eo[li - 2];\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "0364", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You have a pot and N ingredients. Each ingredient has a real number parameter called value, and the value of the i-th ingredient (1 \\leq i \\leq N) is v_i.

\n

When you put two ingredients in the pot, they will vanish and result in the formation of a new ingredient. The value of the new ingredient will be (x + y) / 2 where x and y are the values of the ingredients consumed, and you can put this ingredient again in the pot.

\n

After you compose ingredients in this way N-1 times, you will end up with one ingredient. Find the maximum possible value of this ingredient.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 50
  • \n
  • 1 \\leq v_i \\leq 1000
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nv_1 v_2 \\ldots v_N\n
\n
\n
\n
\n
\n

Output

Print a decimal number (or an integer) representing the maximum possible value of the last ingredient remaining.

\n

Your output will be judged correct when its absolute or relative error from the judge's output is at most 10^{-5}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n3 4\n
\n
\n
\n
\n
\n

Sample Output 1

3.5\n
\n

If you start with two ingredients, the only choice is to put both of them in the pot. The value of the ingredient resulting from the ingredients of values 3 and 4 is (3 + 4) / 2 = 3.5.

\n

Printing 3.50001, 3.49999, and so on will also be accepted.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n500 300 200\n
\n
\n
\n
\n
\n

Sample Output 2

375\n
\n

You start with three ingredients this time, and you can choose what to use in the first composition. There are three possible choices:

\n
    \n
  • Use the ingredients of values 500 and 300 to produce an ingredient of value (500 + 300) / 2 = 400. The next composition will use this ingredient and the ingredient of value 200, resulting in an ingredient of value (400 + 200) / 2 = 300.
  • \n
  • Use the ingredients of values 500 and 200 to produce an ingredient of value (500 + 200) / 2 = 350. The next composition will use this ingredient and the ingredient of value 300, resulting in an ingredient of value (350 + 300) / 2 = 325.
  • \n
  • Use the ingredients of values 300 and 200 to produce an ingredient of value (300 + 200) / 2 = 250. The next composition will use this ingredient and the ingredient of value 500, resulting in an ingredient of value (250 + 500) / 2 = 375.
  • \n
\n

Thus, the maximum possible value of the last ingredient remaining is 375.

\n

Printing 375.0 and so on will also be accepted.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n138 138 138 138 138\n
\n
\n
\n
\n
\n

Sample Output 3

138\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n\n double v[n];\n\n for (int i = 0; i < n; i++) {\n scanf(\"%lf\", &v[i]);\n }\n\n for (int j = 0; j < n; j++) {\n double temp1 = 10000;\n double temp2 = 10000;\n int temp1pos = 0;\n int temp2pos = 0;\n for (int i = 0; i < n; i++) {\n if (v[i] < temp1 && v[i] != (double)0) {\n temp2 = temp1;\n temp2pos = temp1pos;\n temp1 = v[i];\n temp1pos = i;\n } else if (v[i] < temp2 && v[i] != (double)0) {\n temp2 = v[i];\n temp2pos = i;\n }\n }\n if (temp2 == 10000) {\n break;\n }\n v[temp1pos] = (temp1 + temp2) / (double)2;\n v[temp2pos] = 0;\n }\n\n for (int i = 0; i < n; i++) {\n if (v[i] != 0) {\n printf(\"%lf\\n\", v[i]);\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 n: usize = itr.next().unwrap().parse().unwrap();\n let mut v: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n v.sort_by(|a, b| a.partial_cmp(b).unwrap());\n println!(\"{}\", v.iter().fold(v[0], |acc, &x| (acc + x) / 2.0));\n}", "difficulty": "hard"} {"problem_id": "0365", "problem_description": "Vasiliy lives at point (a, b) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested n available Beru-taxi nearby. The i-th taxi is located at point (xi, yi) and moves with a speed vi. Consider that each of n drivers will move directly to Vasiliy and with a maximum possible speed. Compute the minimum time when Vasiliy will get in any of Beru-taxi cars.", "c_code": "int solution() {\n int hx = 0;\n int hy = 0;\n int count = 0;\n int taxi[1002][5];\n double time[1002];\n scanf(\"%d %d\\n%d\\n\", &hx, &hy, &count);\n for (int i = 0; i < count; i++) {\n scanf(\"%d %d %d\\n\", &taxi[i][0], &taxi[i][1], &taxi[i][2]);\n }\n for (int i = 0; i < count; i++) {\n int dx = 0;\n int dy = 0;\n double s = 0;\n if (taxi[i][0] > hx) {\n dx = taxi[i][0] - hx;\n } else {\n dx = hx - taxi[i][0];\n }\n if (taxi[i][1] > hy) {\n dy = taxi[i][1] - hy;\n } else {\n dy = hy - taxi[i][1];\n }\n s = sqrt(pow(dx, 2) + pow(dy, 2));\n time[i] = s / taxi[i][2];\n }\n double min = time[0];\n for (int i = 0; i < count; i++) {\n if (min > time[i]) {\n min = time[i];\n }\n }\n printf(\"%lf\\n\", min);\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let line = lines.next();\n let temp: Vec = line\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n let x = temp[0];\n let y = temp[1];\n\n let line = lines.next();\n let taxin: u32 = line.unwrap().unwrap().parse().unwrap();\n\n let mut ans = std::f32::MAX;\n for _ in 0..taxin {\n let line = lines.next();\n let temp: Vec = line\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let xt = temp[0];\n let yt = temp[1];\n let vt = temp[2];\n\n ans = ans.min(((xt - x).hypot(yt - y)) / vt);\n }\n println!(\"{:.7}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0366", "problem_description": "Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS: You are given two integers $$$d, m$$$, find the number of arrays $$$a$$$, satisfying the following constraints: The length of $$$a$$$ is $$$n$$$, $$$n \\ge 1$$$ $$$1 \\le a_1 < a_2 < \\dots < a_n \\le d$$$ Define an array $$$b$$$ of length $$$n$$$ as follows: $$$b_1 = a_1$$$, $$$\\forall i > 1, b_i = b_{i - 1} \\oplus a_i$$$, where $$$\\oplus$$$ is the bitwise exclusive-or (xor). After constructing an array $$$b$$$, the constraint $$$b_1 < b_2 < \\dots < b_{n - 1} < b_n$$$ should hold. Since the number of possible arrays may be too large, you need to find the answer modulo $$$m$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n long long int d;\n long long int m;\n long long int v;\n long long int digit;\n long long int pow[40];\n long long int i;\n pow[0] = 1;\n for (i = 1; i < 40; i++) {\n pow[i] = pow[i - 1] * 2;\n }\n long long int ans;\n for (; t > 0; t--) {\n scanf(\"%lld %lld\", &d, &m);\n v = d;\n digit = 0;\n while (v > 0) {\n digit++;\n v /= 2;\n }\n ans = 1;\n for (i = 1; i < digit; i++) {\n ans = ans * (pow[i - 1] + 1) % m;\n }\n ans = ans * (d - pow[digit - 1] + 2) % m;\n ans = (ans - 1 + m) % m;\n printf(\"%lld\\n\", ans);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (d, m) = (xs[0], xs[1]);\n let l = (64 - d.leading_zeros()) as usize;\n let mut ns: Vec = vec![0; l];\n ns[l - 1] = (d - 2_u64.pow(l as u32 - 1) + 1) % m;\n for i in (0..(l - 1)).rev() {\n let p = 2_u64.pow(i as u32);\n ns[i] = (p * (ns[i + 1] + 1) + ns[i + 1]) % m;\n }\n println!(\"{}\", ns[0]);\n }\n}", "difficulty": "easy"} {"problem_id": "0367", "problem_description": "Andryusha is an orderly boy and likes to keep things in their place.Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?", "c_code": "int solution(void) {\n int n = 0;\n int i = 0;\n int sock = 0;\n int size = 0;\n int max = 0;\n int cnt[100001] = {\n 0,\n };\n\n scanf(\"%d\", &n);\n\n for (i = 0; i < n * 2; i++) {\n scanf(\"%d\", &sock);\n if (cnt[sock]) {\n cnt[sock]--;\n size--;\n } else {\n cnt[sock]++;\n size++;\n }\n\n if (max < size) {\n max = size;\n }\n }\n\n printf(\"%d\\n\", max);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut lines = stdin.lock().lines();\n let ct = str::parse(&lines.next().unwrap().unwrap()).unwrap();\n\n let mut seen = Vec::new();\n seen.resize(ct, false);\n let mut table = 0;\n\n let mut max = 0;\n for s in lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|w| str::parse::(w).unwrap())\n {\n let old: bool = seen[s - 1];\n seen[s - 1] = !old;\n if old {\n table -= 1;\n } else {\n table += 1;\n if table > max {\n max = table;\n }\n }\n }\n\n println!(\"{}\", max);\n}", "difficulty": "medium"} {"problem_id": "0368", "problem_description": "Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a \"plus\") and negative (a \"minus\"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.", "c_code": "int solution(void) {\n int cnt = 0;\n int i = 0;\n int magnets[100000];\n int groups = 1;\n\n scanf(\"%d\", &cnt);\n\n for (i = 0; i < cnt; i++) {\n scanf(\"%d\", &magnets[i]);\n\n if (i > 0 && magnets[i] != magnets[i - 1]) {\n groups++;\n }\n }\n\n printf(\"%d\\n\", groups);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n stdin().read_line(&mut n).unwrap();\n let mut n: i64 = n.trim().parse().unwrap();\n\n let mut total = 0;\n let mut last: String = \"\".to_string();\n\n while n > 0 {\n let mut x = String::new();\n stdin().read_line(&mut x).unwrap();\n let x = x.trim().to_string();\n if last != x {\n total += 1;\n }\n\n last = x;\n n -= 1;\n }\n\n println!(\"{}\", if total > 0 { total } else { 1 });\n}", "difficulty": "hard"} {"problem_id": "0369", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

AtCoDeer the deer and his friend TopCoDeer is playing a game.\nThe game consists of N turns.\nIn each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition:

\n

(※) After each turn, (the number of times the player has played Paper)(the number of times the player has played Rock).

\n

Each player's score is calculated by (the number of turns where the player wins) - (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.

\n

(For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)

\n

With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the N turns, before the game starts.\nPlan AtCoDeer's gesture in each turn to maximize AtCoDeer's score.

\n

The gesture that TopCoDeer will play in each turn is given by a string s. If the i-th (1≦i≦N) character in s is g, TopCoDeer will play Rock in the i-th turn. Similarly, if the i-th (1≦i≦N) character of s in p, TopCoDeer will play Paper in the i-th turn.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦N≦10^5
  • \n
  • N=|s|
  • \n
  • Each character in s is g or p.
  • \n
  • The gestures represented by s satisfy the condition (※).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Print the AtCoDeer's maximum possible score.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

gpg\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n

Playing the same gesture as the opponent in each turn results in the score of 0, which is the maximum possible score.

\n
\n
\n
\n
\n
\n

Sample Input 2

ggppgggpgg\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

For example, consider playing gestures in the following order: Rock, Paper, Rock, Paper, Rock, Rock, Paper, Paper, Rock, Paper. This strategy earns three victories and suffers one defeat, resulting in the score of 2, which is the maximum possible score.

\n
\n
", "c_code": "int solution() {\n int n = 0;\n int p = 0;\n char t = '\\0';\n while (1) {\n scanf(\"%c\", &t);\n if (t == '\\n') {\n break;\n }\n n++;\n if (t == 'p') {\n p++;\n }\n }\n printf(\"%d\\n\", (n / 2) - p);\n return 0;\n}", "rust_code": "fn solution() {\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let s: Vec = st.trim().chars().collect();\n let p: usize = s.iter().filter(|x| x.to_string() == \"p\").count();\n let n = s.len();\n println!(\"{}\", n / 2 - p);\n}", "difficulty": "easy"} {"problem_id": "0370", "problem_description": "According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower. However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.Write a program that models the behavior of Ankh-Morpork residents.", "c_code": "int solution() {\n int n = 0;\n int a[100 * 1000] = {0};\n int c = 0;\n\n scanf(\"%d\", &n);\n\n c = n - 1;\n\n for (int i = 0; i < n; i++) {\n int t = 0;\n\n scanf(\"%d \", &t);\n\n a[t - 1] = 1;\n\n while (a[c] != 0 && c > -1) {\n printf(\"%d \", c + 1);\n\n c -= 1;\n }\n printf(\"\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let n = buf.trim().parse::().unwrap();\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n let ns = buf\n .split_whitespace()\n .map(|str| str.trim().parse::().unwrap());\n\n let mut seen = vec![false; n as usize + 1];\n let mut waiting_for = n;\n for n in ns {\n seen[n as usize] = true;\n if n == waiting_for {\n for i in (0..n).rev().map(|x| x + 1) {\n if i != n {\n print!(\" \");\n }\n if seen[i as usize] {\n print!(\"{}\", i);\n } else {\n waiting_for = i;\n break;\n }\n }\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0371", "problem_description": "

Selection of Participants of an Experiment

\n\n

\nDr. Tsukuba has devised a new method of programming training.\nIn order to evaluate the effectiveness of this method,\nhe plans to carry out a control experiment.\nHaving two students as the participants of the experiment,\none of them will be trained under the conventional method\nand the other under his new method.\nComparing the final scores of these two,\nhe will be able to judge the effectiveness of his method.\n

\n\n

\nIt is important to select two students having the closest possible scores,\nfor making the comparison fair.\nHe has a list of the scores of all students\nwho can participate in the experiment.\nYou are asked to write a program which selects two of them\nhaving the smallest difference in their scores.\n

\n\n\n

Input

\n\n

\nThe input consists of multiple datasets, each in the following format.\n

\n\n

\n\nn
\na1 a2an\n\n

\n\n

\nA dataset consists of two lines.\nThe number of students n is given in the first line.\nn is an integer satisfying 2 ≤ n ≤ 1000.\nThe second line gives scores of n students.\nai (1 ≤ in) is the score of the i-th student, which is a non-negative integer not greater than 1,000,000.\n

\n\n

\nThe end of the input is indicated by a line containing a zero.\nThe sum of n's of all the datasets does not exceed 50,000.\n

\n\n\n

Output

\n\n

\nFor each dataset, select two students with the smallest difference in their scores,\nand output in a line (the absolute value of) the difference.\n

\n\n\n

Sample Input

\n\n
\n5\n10 10 10 10 10\n5\n1 5 8 9 11\n7\n11 34 83 47 59 29 70\n0\n
\n\n

Output for the Sample Input

\n\n\n
\n0\n1\n5\n
", "c_code": "int solution(void) {\n int N = 0;\n int ans[60000] = {0};\n\n int count = 0;\n while (1) {\n int sa = 10000000;\n int a[60000] = {0};\n scanf(\"%d\", &N);\n if (N == 0) {\n break;\n }\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &a[i]);\n }\n while (1) {\n int sw = 0;\n int temp = 0;\n for (int i = 0; i < N - 1; i++) {\n if (a[i] > a[i + 1]) {\n temp = a[i];\n a[i] = a[i + 1];\n a[i + 1] = temp;\n sw = 1;\n }\n }\n if (sw == 0) {\n break;\n }\n }\n for (int i = 0; i < N - 1; i++) {\n int tempsa = 0;\n tempsa = a[i] - a[i + 1];\n\n tempsa *= -1;\n if (tempsa < sa) {\n sa = tempsa;\n }\n }\n\n ans[count] = sa;\n count++;\n }\n\n for (int i = 0; i < count; i++) {\n printf(\"%d\\n\", ans[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut buf1 = String::new();\n io::stdin().read_line(&mut buf1).ok();\n let n: usize = buf1.trim().parse().unwrap();\n if n == 0 {\n return;\n }\n\n let mut buf2 = String::new();\n io::stdin().read_line(&mut buf2).ok();\n let mut a: Vec<_> = buf2\n .split_whitespace()\n .map(|n| u32::from_str(n).unwrap())\n .collect();\n\n a.sort();\n let mut ans = 1_000_001;\n for i in 0..a.len() - 1 {\n if a[i + 1] - a[i] < ans {\n ans = a[i + 1] - a[i];\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0372", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

\n
    \n
  • S is a palindrome.
  • \n
  • Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.
  • \n
  • The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.
  • \n
\n

Determine whether S is a strong palindrome.

\n
\n
\n
\n
\n

Constraints

    \n
  • S consists of lowercase English letters.
  • \n
  • The length of S is an odd number between 3 and 99 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If S is a strong palindrome, print Yes;\notherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

akasaka\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n
    \n
  • S is akasaka.
  • \n
  • The string formed by the 1-st through the 3-rd characters is aka.
  • \n
  • The string formed by the 5-th through the 7-th characters is aka.\nAll of these are palindromes, so S is a strong palindrome.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

level\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

atcoder\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution() {\n char s[99];\n scanf(\"%s\", s);\n int n = strlen(s);\n for (int i = 0; i < n / 2; i++) {\n if (s[i] == s[n - i - 1] && s[i] == s[((n - 1) / 2) - i - 1] &&\n s[n - i - 1] == s[((n + 3) / 2) + i - 1]) {\n continue;\n }\n printf(\"No\");\n return 0;\n }\n printf(\"Yes\");\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 s = itr.next().unwrap();\n\n let n = s.len();\n let t = s[0..n / 2].to_string();\n let u = s[n.div_ceil(2)..].to_string();\n\n let rs = s.chars().rev().collect::();\n let rt = t.chars().rev().collect::();\n let ru = u.chars().rev().collect::();\n if s == rs && t == rt && u == ru {\n println!(\"Yes\");\n } else {\n println!(\"No\")\n }\n}", "difficulty": "medium"} {"problem_id": "0373", "problem_description": "Nezzar's favorite digit among $$$1,\\ldots,9$$$ is $$$d$$$. He calls a positive integer lucky if $$$d$$$ occurs at least once in its decimal representation. Given $$$q$$$ integers $$$a_1,a_2,\\ldots,a_q$$$, for each $$$1 \\le i \\le q$$$ Nezzar would like to know if $$$a_i$$$ can be equal to a sum of several (one or more) lucky numbers.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n int m;\n int l;\n scanf(\"%d%d\", &m, &l);\n int a[m];\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &a[j]);\n }\n for (int j = 0; j < m; j++) {\n int c = 0;\n while (a[j] > 0) {\n if (a[j] % l == 0 || a[j] >= (l * 10)) {\n c = 1;\n break;\n }\n a[j] = a[j] - 10;\n }\n if (c == 1) {\n printf(\"YES\\n\");\n } else if (c == 0) {\n printf(\"NO\\n\");\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n io::stdin().read_line(&mut n).unwrap();\n let n: Vec = n.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n let n: i64 = n[0];\n for _ in 0..n {\n let mut line_1 = String::new();\n let mut line_2 = String::new();\n io::stdin().read_line(&mut line_1).unwrap();\n io::stdin().read_line(&mut line_2).unwrap();\n\n let line_1: Vec = line_1\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let d = line_1[1];\n let d_s = &format!(\"{}\", d)[..];\n\n let nums: Vec = line_2\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut hash_set_yes: HashSet = HashSet::new();\n let mut hash_set_no: HashSet = HashSet::new();\n\n for origin_num in nums {\n let mut num = origin_num;\n loop {\n if hash_set_yes.get(&num).is_some() {\n println!(\"{}\", \"YES\");\n break;\n }\n\n if hash_set_no.get(&num).is_some() {\n println!(\"{}\", \"NO\");\n break;\n }\n\n let num_s = format!(\"{}\", num);\n if num_s.contains(d_s) {\n println!(\"{}\", \"YES\");\n hash_set_yes.insert(origin_num);\n break;\n } else {\n num -= d;\n if num < 0 {\n println!(\"{}\", \"NO\");\n hash_set_no.insert(origin_num);\n break;\n }\n if num % 10 == 0 {\n println!(\"{}\", \"YES\");\n hash_set_yes.insert(origin_num);\n break;\n }\n }\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0374", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Snuke has a calculator. It has a display and two buttons.

\n

Initially, the display shows an integer x.\nSnuke wants to change this value into another integer y, by pressing the following two buttons some number of times in arbitrary order:

\n
    \n
  • Button A: When pressed, the value on the display is incremented by 1.
  • \n
  • Button B: When pressed, the sign of the value on the display is reversed.
  • \n
\n

Find the minimum number of times Snuke needs to press the buttons to achieve his objective.\nIt can be shown that the objective is always achievable regardless of the values of the integers x and y.

\n
\n
\n
\n
\n

Constraints

    \n
  • x and y are integers.
  • \n
  • |x|, |y| ≤ 10^9
  • \n
  • x and y are different.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
x y\n
\n
\n
\n
\n
\n

Output

Print the minimum number of times Snuke needs to press the buttons to achieve his objective.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10 20\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

Press button A ten times.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 -10\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

Press button B once.

\n
\n
\n
\n
\n
\n

Sample Input 3

-10 -20\n
\n
\n
\n
\n
\n

Sample Output 3

12\n
\n

Press the buttons as follows:

\n
    \n
  • Press button B once.
  • \n
  • Press button A ten times.
  • \n
  • Press button B once.
  • \n
\n
\n
", "c_code": "int solution() {\n int x;\n int y;\n scanf(\" %d %d\", &x, &y);\n int z = 0;\n int x1 = abs(x);\n int y1 = abs(y);\n z += abs(x1 - y1);\n if ((x1 <= y1 && ((x < 0 && y > 0) || (x > 0 && y < 0))) ||\n (x1 >= y1 && ((x < 0 && y > 0) || (x > 0 && y < 0))) ||\n (x == 0 && y < 0) || (x > 0 && y == 0)) {\n z++;\n } else if ((x1 < y1 && x < 0 && y < 0) || (x1 > y1 && x > 0 && y > 0)) {\n z += 2;\n }\n printf(\"%d\", z);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let v: Vec = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let (x, y) = (v[0], v[1]);\n\n let ans: i64 = if x * y < 0 || x * y == 0 && x > y {\n (x + y).abs() + 1\n } else if x <= y {\n y - x\n } else {\n x - y + 2\n };\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0375", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

You are going to hold a competition of one-to-one game called AtCoder Janken. (Janken is the Japanese name for Rock-paper-scissors.)\nN players will participate in this competition, and they are given distinct integers from 1 through N.\nThe arena has M playing fields for two players. You need to assign each playing field two distinct integers between 1 and N (inclusive).\nYou cannot assign the same integer to multiple playing fields.\nThe competition consists of N rounds, each of which proceeds as follows:

\n
    \n
  • For each player, if there is a playing field that is assigned the player's integer, the player goes to that field and fight the other player who comes there.
  • \n
  • Then, each player adds 1 to its integer. If it becomes N+1, change it to 1.
  • \n
\n

You want to ensure that no player fights the same opponent more than once during the N rounds.\nPrint an assignment of integers to the playing fields satisfying this condition.\nIt can be proved that such an assignment always exists under the constraints given.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq M
  • \n
  • M \\times 2 +1 \\leq N \\leq 200000
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\n
\n
\n
\n
\n
\n

Output

Print M lines in the format below.\nThe i-th line should contain the two integers a_i and b_i assigned to the i-th playing field.

\n
a_1 b_1\na_2 b_2\n:\na_M b_M\n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 1\n
\n
\n
\n
\n
\n

Sample Output 1

2 3\n
\n

Let us call the four players A, B, C, and D, and assume that they are initially given the integers 1, 2, 3, and 4, respectively.

\n
    \n
  • \n

    The 1-st round is fought by B and C, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 2, 3, 4, and 1, respectively.

    \n
  • \n
  • \n

    The 2-nd round is fought by A and B, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 3, 4, 1, and 2, respectively.

    \n
  • \n
  • \n

    The 3-rd round is fought by D and A, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 4, 1, 2, and 3, respectively.

    \n
  • \n
  • \n

    The 4-th round is fought by C and D, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 1, 2, 3, and 4, respectively.

    \n
  • \n
\n

No player fights the same opponent more than once during the four rounds, so this solution will be accepted.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 3\n
\n
\n
\n
\n
\n

Sample Output 2

1 6\n2 5\n3 4\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n long m;\n scanf(\"%ld %ld\", &n, &m);\n if (m % 2 == 1) {\n for (long i = 1; i <= m / 2; i++) {\n printf(\"%ld %ld\\n\", i, m + 1 - i);\n }\n for (long i = 1; i <= (m + 1) / 2; i++) {\n printf(\"%ld %ld\\n\", m + i, ((m + 1) * 2) - i);\n }\n } else {\n for (long i = 1; i <= m / 2; i++) {\n printf(\"%ld %ld\\n\", i, m + 2 - i);\n }\n for (long i = 1; i <= (m + 1) / 2; i++) {\n printf(\"%ld %ld\\n\", m + 1 + i, ((m + 1) * 2) - i);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n m: usize\n }\n if n % 2 == 1 {\n for i in (1..=m).rev() {\n println!(\"{} {}\", i, n + 1 - i);\n }\n return;\n }\n let mut c = 0;\n for i in 1..n / 2 {\n if c == m || i >= n / 2 - i {\n break;\n }\n println!(\"{} {}\", i, n / 2 - i);\n c += 1;\n }\n for i in n / 2..n {\n if c == m || i >= n - 1 - (i - n / 2) {\n break;\n }\n println!(\"{} {}\", i, n - 1 - (i - n / 2));\n c += 1;\n }\n}", "difficulty": "easy"} {"problem_id": "0376", "problem_description": "You are given a set of all integers from $$$l$$$ to $$$r$$$ inclusive, $$$l < r$$$, $$$(r - l + 1) \\le 3 \\cdot 10^5$$$ and $$$(r - l)$$$ is always odd.You want to split these numbers into exactly $$$\\frac{r - l + 1}{2}$$$ pairs in such a way that for each pair $$$(i, j)$$$ the greatest common divisor of $$$i$$$ and $$$j$$$ is equal to $$$1$$$. Each number should appear in exactly one of the pairs.Print the resulting pairs or output that no solution exists. If there are multiple solutions, print any of them.", "c_code": "int solution() {\n long long t = 1;\n\n while (t--) {\n long long l;\n long long r;\n scanf(\"%lld%lld\", &l, &r);\n printf(\"YES\\n\");\n for (long long i = 0; i <= (r - l) / 2; i++) {\n printf(\"%lld %lld\\n\", l + (i * 2) + 1, l + (i * 2));\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut s = String::new();\n io::stdin().read_to_string(&mut s);\n let range: Vec = s\n .split_whitespace()\n .filter(|&s| !s.is_empty())\n .map(|s| s.parse::().unwrap())\n .collect();\n let start = range[0];\n let end = range[1] + 1;\n println!(\"YES\");\n let mut flag = false;\n for x in start..end {\n flag = true;\n if (x - start) % 2 == 1 {\n continue;\n }\n println!(\"{} {}\", x, x + 1);\n }\n if !flag {\n println!(\"range: {}, {}\", start, end);\n }\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "0377", "problem_description": "You are given $$$n$$$ of integers $$$a_1, a_2, \\ldots, a_n$$$. Process $$$q$$$ queries of two types: query of the form \"0 $$$x_j$$$\": add the value $$$x_j$$$ to all even elements of the array $$$a$$$, query of the form \"1 $$$x_j$$$\": add the value $$$x_j$$$ to all odd elements of the array $$$a$$$.Note that when processing the query, we look specifically at the odd/even value of $$$a_i$$$, not its index.After processing each query, print the sum of the elements of the array $$$a$$$.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n static int n;\n static int q;\n static int cnt;\n static int type;\n static int x;\n static long long ans;\n cnt = 0;\n ans = 0;\n scanf(\"%d%d\", &n, &q);\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &x);\n cnt += x & 1;\n\n ans += x;\n }\n\n for (int i = 0; i < q; ++i) {\n scanf(\"%d%d\", &type, &x);\n ans += 1LL * x * (type ? cnt : n - cnt);\n if (x & 1) {\n if (type) {\n cnt = 0;\n } else {\n cnt = n;\n }\n }\n printf(\"%lld\\n\", ans);\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_q_str = line_iter.next().unwrap().unwrap();\n let (n_str, q_str) = n_q_str.split_once(' ').unwrap();\n let (_n, q) = (\n n_str.parse::().unwrap(),\n q_str.parse::().unwrap(),\n );\n let nums_str = line_iter.next().unwrap().unwrap();\n let nums_iter = nums_str\n .split(' ')\n .map(|num_str| num_str.parse::().unwrap());\n\n let mut total: u64 = 0;\n let mut odd_count: u64 = 0;\n let mut even_count: u64 = 0;\n\n for num in nums_iter {\n total += num;\n if num % 2 == 0 {\n even_count += 1;\n } else {\n odd_count += 1;\n }\n }\n\n let query_iter = (0..q).map(|_| {\n let query_line_str = line_iter.next().unwrap().unwrap();\n let (type_j_str, x_j_str) = query_line_str.split_once(' ').unwrap();\n (\n type_j_str.parse::().unwrap(),\n x_j_str.parse::().unwrap(),\n )\n });\n\n for query in query_iter {\n if query.0 == 0 {\n total += query.1 * even_count;\n\n if query.1 % 2 == 1 {\n odd_count += even_count;\n even_count = 0;\n }\n } else {\n total += query.1 * odd_count;\n\n if query.1 % 2 == 1 {\n even_count += odd_count;\n odd_count = 0;\n }\n }\n\n println!(\"{}\", total);\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0378", "problem_description": "Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $$$2n$$$ jars of strawberry and blueberry jam.All the $$$2n$$$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $$$n$$$ jars to his left and $$$n$$$ jars to his right.For example, the basement might look like this: Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right.Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same.For example, this might be the result: He has eaten $$$1$$$ jar to his left and then $$$5$$$ jars to his right. There remained exactly $$$3$$$ full jars of both strawberry and blueberry jam. Jars are numbered from $$$1$$$ to $$$2n$$$ from left to right, so Karlsson initially stands between jars $$$n$$$ and $$$n+1$$$.What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left?Your program should answer $$$t$$$ independent test cases.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long int i;\n long long int k = 0;\n long long int dif = 0;\n long long int n;\n long long int a[200005] = {0};\n long long int l[200005];\n long long int r[200005];\n long long int s = 0;\n long long int bb = 0;\n long long int sb = 0;\n long long int min = INT_MAX;\n scanf(\"%lld\", &n);\n for (i = 0; i < 2 * n; i++) {\n l[i] = r[i] = -1;\n scanf(\"%lld\", &a[i]);\n if (a[i] == 1) {\n sb++;\n } else {\n bb++;\n }\n }\n l[2 * n] = r[2 * n] = -1;\n if (bb > sb) {\n dif = bb - sb;\n\n for (i = n - 1; i >= 0; i--) {\n if (a[i] == 2) {\n s++;\n\n if (s > 0 && l[s] == -1) {\n l[s] = n - i - 1;\n }\n } else {\n s--;\n }\n }\n s = 0;\n for (i = n; i < 2 * n; i++) {\n if (a[i] == 2) {\n s++;\n if (s > 0 && r[s] == -1) {\n r[s] = i - n;\n\n if (l[dif - s] != -1) {\n k = l[dif - s] + r[s] + 2;\n if (min > k) {\n min = k;\n }\n }\n }\n } else {\n s--;\n }\n }\n\n if (l[dif] + 1 < min && l[dif] != -1) {\n min = l[dif] + 1;\n }\n if (r[dif] + 1 < min && r[dif] != -1) {\n min = r[dif] + 1;\n }\n printf(\"%lld\\n\", min);\n } else if (bb < sb) {\n dif = sb - bb;\n\n for (i = n - 1; i >= 0; i--) {\n if (a[i] == 1) {\n s++;\n\n if (s > 0 && l[s] == -1) {\n l[s] = n - i - 1;\n }\n } else {\n s--;\n }\n }\n s = 0;\n for (i = n; i < 2 * n; i++) {\n if (a[i] == 1) {\n s++;\n if (s > 0 && r[s] == -1) {\n r[s] = i - n;\n\n if (l[dif - s] != -1) {\n k = l[dif - s] + r[s] + 2;\n\n if (min > k) {\n min = k;\n }\n }\n }\n } else {\n s--;\n }\n }\n\n if (l[dif] + 1 < min && l[dif] != -1) {\n min = l[dif] + 1;\n }\n if (r[dif] + 1 < min && r[dif] != -1) {\n min = r[dif] + 1;\n }\n printf(\"%lld\\n\", min);\n } else {\n printf(\"0\\n\");\n }\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 let mut entries = buf.split_whitespace();\n let q: usize = entries.next().unwrap().parse().unwrap();\n for _ in 0..q {\n let n: usize = entries.next().unwrap().parse().unwrap();\n let mut sum = 0;\n let mut l: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n let l_i: isize = entries.next().unwrap().parse().unwrap();\n let l_i = if l_i == 1 { 1 } else { -1 };\n sum += -l_i;\n l.push(l_i);\n }\n let mut l = l.into_iter().rev().collect::>();\n let mut r: Vec = Vec::with_capacity(n);\n for _ in 0..n {\n let r_i: isize = entries.next().unwrap().parse().unwrap();\n let r_i = if r_i == 1 { 1 } else { -1 };\n sum += -r_i;\n r.push(r_i);\n }\n\n for i in 1..n {\n l[i] += l[i - 1];\n r[i] += r[i - 1];\n }\n\n if sum == 0 {\n println!(\"0\");\n continue;\n }\n let mut l_ind = HashMap::new();\n for i in 0..n {\n l_ind.entry(l[i]).or_insert(i);\n }\n\n let mut min = if let Some(cost) = l_ind.get(&(-sum)) {\n *cost + 1\n } else {\n usize::max_value()\n };\n\n for i in 0..n {\n if r[i] == -sum {\n min = min.min(i + 1);\n }\n if let Some(cost) = l_ind.get(&(-(sum + r[i]))) {\n min = min.min(cost + 1 + i + 1)\n }\n }\n if min == usize::max_value() {\n min = 2 * n\n }\n println!(\"{}\", min);\n }\n}", "difficulty": "hard"} {"problem_id": "0379", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given are three integers A_1, A_2, and A_3.

\n

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A_i \\leq 13 \\ \\ (i=1,2,3)
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A_1 A_2 A_3\n
\n
\n
\n
\n
\n

Output

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 7 9\n
\n
\n
\n
\n
\n

Sample Output 1

win\n
\n

5+7+9=21, so print win.

\n
\n
\n
\n
\n
\n

Sample Input 2

13 7 2\n
\n
\n
\n
\n
\n

Sample Output 2

bust\n
\n

13+7+2=22, so print bust.

\n
\n
", "c_code": "int solution(void) {\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 >= 22) {\n printf(\"bust\");\n } else {\n printf(\"win\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).expect(\"read_line error\");\n\n let mut parts = s.split_whitespace().map(|s| s.parse::());\n if let (Some(Ok(a)), Some(Ok(b)), Some(Ok(c))) = (parts.next(), parts.next(), parts.next()) {\n if a + b + c >= 22 {\n println!(\"bust\")\n } else {\n println!(\"win\")\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0380", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$. You want to split it into exactly $$$k$$$ non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the $$$n$$$ elements of the array $$$a$$$ must belong to exactly one of the $$$k$$$ subsegments.Let's see some examples of dividing the array of length $$$5$$$ into $$$3$$$ subsegments (not necessarily with odd sums): $$$[1, 2, 3, 4, 5]$$$ is the initial array, then all possible ways to divide it into $$$3$$$ non-empty non-intersecting subsegments are described below: $$$[1], [2], [3, 4, 5]$$$; $$$[1], [2, 3], [4, 5]$$$; $$$[1], [2, 3, 4], [5]$$$; $$$[1, 2], [3], [4, 5]$$$; $$$[1, 2], [3, 4], [5]$$$; $$$[1, 2, 3], [4], [5]$$$. Of course, it can be impossible to divide the initial array into exactly $$$k$$$ subsegments in such a way that each of them will have odd sum of elements. In this case print \"NO\". Otherwise, print \"YES\" and any possible division of the array. See the output format for the detailed explanation.You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n int a[n];\n int b[n];\n int count = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] % 2 == 1) {\n b[count] = i;\n count++;\n }\n }\n if (count >= k && k % 2 == count % 2) {\n printf(\"YES\\n\");\n for (int j = 0; j < k - 1; j++) {\n printf(\"%d \", b[j] + 1);\n }\n printf(\"%d\\n\", n);\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n\n let stdout = io::stdout();\n let mut out = io::BufWriter::with_capacity(2 * 1024 * 1024, stdout.lock());\n\n let q = input!(i32);\n for _ in 0..q {\n let (n, k) = input!(usize, usize);\n let a: Vec<_> = input!()\n .split_ascii_whitespace()\n .map(|w| w.parse::().unwrap())\n .collect();\n let b = a.iter().filter(|&x| x % 2 == 1).count();\n if b < k || (b - k) % 2 != 0 {\n writeln!(out, \"NO\").unwrap();\n } else {\n writeln!(out, \"YES\").unwrap();\n for (i, _) in a\n .iter()\n .enumerate()\n .filter(|&(_, x)| x % 2 == 1)\n .take(k - 1)\n {\n write!(out, \"{} \", i + 1).unwrap();\n }\n writeln!(out, \"{}\", n).unwrap();\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0381", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given is a positive integer N.\nHow many tuples (A,B,C) of positive integers satisfy A \\times B + C = N?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^6
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

There are 3 tuples of integers that satisfy A \\times B + C = 3: (A, B, C) = (1, 1, 2), (1, 2, 1), (2, 1, 1).

\n
\n
\n
\n
\n
\n

Sample Input 2

100\n
\n
\n
\n
\n
\n

Sample Output 2

473\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1000000\n
\n
\n
\n
\n
\n

Sample Output 3

13969985\n
\n
\n
", "c_code": "int solution(void) {\n\n int n;\n int cnt = 1;\n int ans = 0;\n int flag = 0;\n\n scanf(\"%d\", &n);\n\n for (int z = 1; z <= n - 1; z++) {\n flag = 0;\n cnt = 0;\n for (int i = 1; i <= sqrt(z); i++) {\n if (z % i == 0) {\n cnt++;\n if (z == i * i) {\n flag++;\n }\n }\n }\n cnt *= 2;\n if (flag) {\n cnt -= flag;\n }\n ans += cnt;\n }\n\n printf(\"%d\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: i64 = s.next().unwrap().parse().unwrap();\n let mut d = vec![1; n as usize + 1];\n for i in 2..=n {\n for j in (i..=n).step_by(i as usize) {\n d[j as usize] += 1;\n }\n }\n let mut ans = 0i64;\n for c in 1..n {\n ans += d[(n - c) as usize];\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0382", "problem_description": "Let's say you are standing on the $$$XY$$$-plane at point $$$(0, 0)$$$ and you want to reach point $$$(n, n)$$$.You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $$$x$$$ coordinate, or up, i. e. vertically and in the direction that increase your $$$y$$$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $$$n - 1$$$ direction changes.As a result, your path will be a polygonal chain from $$$(0, 0)$$$ to $$$(n, n)$$$, consisting of at most $$$n$$$ line segments where each segment has positive integer length and vertical and horizontal segments alternate.Not all paths are equal. You have $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ where $$$c_i$$$ is the cost of the $$$i$$$-th segment.Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $$$k$$$ segments ($$$k \\le n$$$), then the cost of the path is equal to $$$\\sum\\limits_{i=1}^{k}{c_i \\cdot length_i}$$$ (segments are numbered from $$$1$$$ to $$$k$$$ in the order they are in the path).Find the path of the minimum cost and print its cost.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n scanf(\"%d\", &n);\n long cost[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%ld\", &cost[i]);\n }\n\n long long minSum;\n long long minOdd = cost[0];\n long long minEve = cost[1];\n long long sumOdd = cost[0];\n long long sumEve = 0;\n long long countOdd;\n long long countEve;\n for (k = 2; k <= n; k++) {\n long long x = cost[k - 1];\n countOdd = (k + 1) / 2;\n countEve = k - countOdd;\n if (k & 1) {\n if (minOdd > x) {\n minOdd = x;\n }\n sumOdd += x;\n } else {\n if (minEve > x) {\n minEve = x;\n }\n sumEve += x;\n }\n\n sumOdd += minOdd * (n - countOdd);\n sumEve += minEve * (n - countEve);\n\n long long sum = sumOdd + sumEve;\n if (k == 2) {\n minSum = sum;\n }\n if (minSum > sum) {\n minSum = sum;\n }\n sumOdd -= minOdd * (n - countOdd);\n sumEve -= minEve * (n - countEve);\n }\n printf(\"%lld\\n\", minSum);\n }\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let n = scan!(usize);\n\n let mut c = vec![0; n + 1];\n\n for i in 1..=n {\n c[i] = scan!(i64);\n }\n\n let mut odd_mn = 1000_000_000 + 5;\n let mut evn_mn = 1000_000_000 + 5;\n let mut odd_sum = 0;\n let mut evn_sum = 0;\n\n let mut ans = odd_mn * odd_mn;\n\n for i in 1..=n {\n if i & 1 == 1 {\n odd_mn = odd_mn.min(c[i]);\n odd_sum += c[i];\n } else {\n evn_mn = evn_mn.min(c[i]);\n evn_sum += c[i];\n }\n let j = i.div_ceil(2) as i64;\n let k = (i / 2) as i64;\n let n = n as i64;\n let ans_odd = (n - j) * odd_mn + odd_sum;\n let ans_evn = (n - k) * evn_mn + evn_sum;\n\n ans = ans.min(ans_evn + ans_odd);\n }\n print!(\"{}\", ans);\n print!(\"\\n\");\n }\n}", "difficulty": "medium"} {"problem_id": "0383", "problem_description": "

Mode Value

\n\n

\nYour task is to write a program which reads a sequence of integers and prints mode values of the sequence.\nThe mode value is the element which occurs most frequently. \n

\n\n

Input

\n\n

\nA sequence of integers ai (1 ≤ ai ≤ 100). The number of integers is less than or equals to 100.\n\n

\n\n

Output

\n\n

\nPrint the mode values. If there are several mode values, print them in ascending order.\n

\n\n

Sample Input

\n\n
\n5\n6\n3\n5\n8\n7\n5\n3\n9\n7\n3\n4\n
\n\n

Output for the Sample Input

\n\n
\n3\n5\n
\n\n

\nFor example, 3 and 5 respectively occur three times, 7 occurs two times, and others occur only one. So, the mode values are 3 and 5.\n

", "c_code": "int solution(void) {\n int i = 1;\n int j = 0;\n int data[100];\n int max = 0;\n int count[100];\n\n for (j = 0; j < 100; j++) {\n data[j] = 0;\n count[j + 1] = 0;\n }\n\n while (scanf(\"%d\", &data[i]) != EOF) {\n for (j = 1; j <= 100; j++) {\n if (j == data[i]) {\n count[j]++;\n }\n }\n i++;\n }\n max = count[1];\n for (i = 1; i <= 100; i++) {\n if (max < count[i]) {\n max = count[i];\n }\n }\n for (i = 1; i <= 100; i++) {\n if (max == count[i]) {\n printf(\"%d\\n\", i);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let ns = {\n let mut vec = Vec::new();\n let mut s = String::new();\n loop {\n s.clear();\n match std::io::stdin().read_line(&mut s) {\n Ok(0) => break,\n Ok(_) => vec.push(s.trim_end().parse::().unwrap()),\n Err(e) => println!(\"{}\", e),\n }\n }\n vec\n };\n\n let mut map_count: BTreeMap = BTreeMap::new();\n for n in ns {\n *map_count.entry(n).or_insert(0) += 1;\n }\n\n let max_value = map_count.values().max().unwrap();\n\n for (key, value) in &map_count {\n if value != max_value {\n continue;\n }\n println!(\"{}\", key);\n }\n}", "difficulty": "medium"} {"problem_id": "0384", "problem_description": "Pupils decided to go to amusement park. Some of them were with parents. In total, n people came to the park and they all want to get to the most extreme attraction and roll on it exactly once.Tickets for group of x people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is c1 + c2·(x - 1)2 (in particular, if the group consists of one person, then the price is c1). All pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group.", "c_code": "int solution() {\n long long n;\n long long c1;\n long long c2;\n long long i;\n char s[200010];\n scanf(\"%lld%lld%lld\", &n, &c1, &c2);\n scanf(\"%s\", s);\n long long len = strlen(s);\n long long sum = 1e18;\n long long s1 = 0;\n for (i = 0; i < len; i++) {\n if (s[i] == '1') {\n s1++;\n }\n }\n long long a;\n long long b;\n for (i = 1; i <= s1; i++) {\n long long tmp = i * c1;\n tmp += (n % i) * (n / i) * (n / i) * c2 +\n (i - n % i) * (n / i - 1) * (n / i - 1) * c2;\n if (sum > tmp) {\n sum = tmp;\n }\n }\n printf(\"%lld\\n\", sum);\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let tokens: Vec<&str> = line.split_whitespace().collect();\n let n: i32 = tokens[0].parse().unwrap();\n let c1: i64 = tokens[1].parse().unwrap();\n let c2: i64 = tokens[2].parse().unwrap();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let mut adults = 0;\n for c in s.chars() {\n if (c as u8) == b'1' {\n adults += 1\n }\n }\n\n let mut res: i64 = 1234567890123456789i64;\n for groups in 1..adults + 1 {\n let sml_var: i64 = ((n as i64) / groups) - 1;\n let sml_cnt: i64 = groups - (n as i64) % groups;\n let big_var: i64 = sml_var + 1;\n let big_cnt: i64 = (n as i64) % groups;\n let cur_res: i64 =\n c1 * groups + c2 * sml_cnt * sml_var * sml_var + c2 * big_cnt * big_var * big_var;\n if cur_res < res {\n res = cur_res;\n }\n }\n println!(\"{}\", res);\n}", "difficulty": "medium"} {"problem_id": "0385", "problem_description": "You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n int sw = 0;\n int h[100] = {0};\n long long int sum = 0;\n scanf(\"%d\", &t);\n while (t-- > 0) {\n sum = 0;\n sw = 1;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &h[i]);\n }\n\n for (int i = 0; i < n; i++) {\n\n sum += h[i];\n if (sum < ((i * (i + 1)) / 2)) {\n sw = 0;\n break;\n }\n }\n if (sw == 0) {\n printf(\"NO\\n\");\n }\n if (sw == 1) {\n printf(\"YES\\n\");\n }\n }\n return 0;\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 arr: Vec = (0..n).map(|_| sc.next()).collect();\n let mut valid = true;\n for i in 0..n - 1 {\n let x = min(arr[i], i as i64);\n if x < i as i64 {\n valid = false;\n break;\n }\n arr[i + 1] += arr[i] - x;\n }\n let ans = if valid && arr[n - 1] >= n as i64 - 1 {\n \"YES\"\n } else {\n \"NO\"\n };\n writeln!(out, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0386", "problem_description": "Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock. The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?", "c_code": "int solution() {\n int no;\n int moves = 0;\n char str[1000];\n char nstr[1000];\n scanf(\"%d\", &no);\n scanf(\"%s\", str);\n scanf(\"%s\", nstr);\n for (int i = 0; i < no; i++) {\n int no1 = str[i] - '0';\n int no2 = nstr[i] - '0';\n if (no1 - no2 > 0) {\n if (no1 - no2 > 10 - no1 + no2) {\n moves = moves + 10 - no1 + no2;\n } else {\n moves = moves + no1 - no2;\n }\n } else {\n if (no2 - no1 > 10 - no2 + no1) {\n moves = moves + 10 - no2 + no1;\n } else {\n moves = moves + no2 - no1;\n }\n }\n }\n printf(\"%d\", moves);\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).unwrap();\n let number_disk: usize = input.trim().parse().unwrap();\n\n let mut original_state = String::new();\n std::io::stdin().read_line(&mut original_state).unwrap();\n let mut final_state = String::new();\n std::io::stdin().read_line(&mut final_state).unwrap();\n\n let final_state = final_state.trim().as_bytes();\n let original_state = original_state.trim().as_bytes();\n let mut number_move: usize = 0;\n for i in 0..number_disk {\n let distance: isize = (original_state[i] as isize) - (final_state[i] as isize);\n let mut distance = distance.unsigned_abs();\n if distance > 5 {\n distance = 10 - distance;\n }\n\n number_move += distance;\n }\n println!(\"{}\", number_move);\n}", "difficulty": "medium"} {"problem_id": "0387", "problem_description": "A string is called square if it is some string written twice in a row. For example, the strings \"aa\", \"abcabc\", \"abab\" and \"baabaa\" are square. But the strings \"aaa\", \"abaaab\" and \"abcdabc\" are not square.For a given string $$$s$$$ determine if it is square.", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n\n char *str = malloc(sizeof(char) * 101);\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", str);\n int length = strlen(str);\n\n if (length % 2 == 1) {\n printf(\"NO\\n\");\n continue;\n }\n char *tmp = malloc(sizeof(char) * (length / 2) + 1);\n char *tmp2 = malloc(sizeof(char) * (length / 2) + 1);\n\n for (int j = 0; j < (length / 2) + 1; j++) {\n tmp[j] = str[j];\n tmp2[j] = str[j + length / 2];\n\n if (j == (length / 2)) {\n tmp[j] = '\\0';\n tmp2[j + length / 2] = '\\0';\n }\n }\n\n if (!strcmp(tmp, tmp2))\n printf(\"YES\\n\");\n else\n printf(\"NO\\n\");\n\n free(tmp);\n free(tmp2);\n }\n\n free(str);\n}", "rust_code": "fn solution() {\n std::io::stdin().lines().skip(1).flatten().for_each(|s| {\n match s.split_at(s.len().wrapping_div(2)) {\n (a, b) if a == b => println!(\"YES\"),\n _ => println!(\"NO\"),\n }\n });\n}", "difficulty": "medium"} {"problem_id": "0388", "problem_description": "When registering in a social network, users are allowed to create their own convenient login to make it easier to share contacts, print it on business cards, etc.Login is an arbitrary sequence of lower and uppercase latin letters, digits and underline symbols («_»). However, in order to decrease the number of frauds and user-inattention related issues, it is prohibited to register a login if it is similar with an already existing login. More precisely, two logins s and t are considered similar if we can transform s to t via a sequence of operations of the following types: transform lowercase letters to uppercase and vice versa; change letter «O» (uppercase latin letter) to digit «0» and vice versa; change digit «1» (one) to any letter among «l» (lowercase latin «L»), «I» (uppercase latin «i») and vice versa, or change one of these letters to other. For example, logins «Codeforces» and «codef0rces» as well as «OO0OOO00O0OOO0O00OOO0OO_lol» and «OO0OOO0O00OOO0O00OO0OOO_1oI» are considered similar whereas «Codeforces» and «Code_forces» are not.You're given a list of existing logins with no two similar amonst and a newly created user login. Check whether this new login is similar with any of the existing ones.", "c_code": "int solution() {\n int n;\n int i = 0;\n int j;\n int log = 1;\n char str1[51];\n char str2[51];\n scanf(\" %s\", str1);\n for (j = 0; str1[j]; j++) {\n if (str1[j] >= 'A' && str1[j] <= 'Z') {\n str1[j] += 32;\n }\n if (str1[j] == 'O' || str1[j] == 'o') {\n str1[j] = '0';\n }\n if (str1[j] == 'i' || str1[j] == 'I' || str1[j] == 'l' || str1[j] == 'L') {\n str1[j] = '1';\n }\n }\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%s\", str2);\n for (j = 0; str2[j]; j++) {\n if (str2[j] >= 'A' && str2[j] <= 'Z') {\n str2[j] += 32;\n }\n if (str2[j] == 'O' || str2[j] == 'o') {\n str2[j] = '0';\n }\n if (str2[j] == 'i' || str2[j] == 'I' || str2[j] == 'l' ||\n str2[j] == 'L') {\n str2[j] = '1';\n }\n }\n if (strcmp(str1, str2) == 0) {\n log = 0;\n }\n }\n if (log == 0) {\n printf(\"No\");\n } else {\n printf(\"Yes\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n stdin().read_line(&mut s);\n s = s.to_lowercase();\n s = s.replace(\"1\", \"!\");\n s = s.replace(\"0\", \"o\");\n s = s.replace(\"i\", \"!\");\n s = s.replace(\"l\", \"!\");\n let mut n: String = String::new();\n stdin().read_line(&mut n).unwrap();\n let n: i32 = n.trim().parse::().unwrap();\n let mut st: String = String::new();\n let mut p: i32 = 0;\n for _c in 0..n {\n stdin().read_line(&mut st);\n st = st.to_lowercase();\n st = st.replace(\"1\", \"!\");\n st = st.replace(\"0\", \"o\");\n st = st.replace(\"i\", \"!\");\n st = st.replace(\"l\", \"!\");\n if s == st {\n p = 1\n }\n st.clear();\n }\n if p == 0 {\n println!(\"Yes\")\n } else {\n println!(\"No\")\n }\n}", "difficulty": "medium"} {"problem_id": "0389", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Given are integers a,b,c and d.\nIf x and y are integers and a \\leq x \\leq b and c\\leq y \\leq d hold, what is the maximum possible value of x \\times y?

\n
\n
\n
\n
\n

Constraints

    \n
  • -10^9 \\leq a \\leq b \\leq 10^9
  • \n
  • -10^9 \\leq c \\leq d \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b c d\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 2 1 1\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

If x = 1 and y = 1 then x \\times y = 1.\nIf x = 2 and y = 1 then x \\times y = 2.\nTherefore, the answer is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 5 -4 -2\n
\n
\n
\n
\n
\n

Sample Output 2

-6\n
\n

The answer can be negative.

\n
\n
\n
\n
\n
\n

Sample Input 3

-1000000000 0 -1000000000 0\n
\n
\n
\n
\n
\n

Sample Output 3

1000000000000000000\n
\n
\n
", "c_code": "int solution() {\n long int a[10];\n long int b[10];\n\n for (int i = 0; i < 4; ++i) {\n scanf(\"%ld\", &a[i]);\n }\n b[0] = a[0] * a[2];\n b[1] = a[0] * a[3];\n b[2] = a[1] * a[2];\n b[3] = a[1] * a[3];\n\n long int max = LONG_MIN;\n\n for (long int i = 0; i < 4; ++i) {\n if (max < b[i]) {\n max = b[i];\n }\n }\n printf(\"%ld\\n\", max);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let a: i64 = s.next().unwrap().parse().unwrap();\n let b: i64 = s.next().unwrap().parse().unwrap();\n let c: i64 = s.next().unwrap().parse().unwrap();\n let d: i64 = s.next().unwrap().parse().unwrap();\n let v = [a * d, a * c, b * d, b * c];\n println!(\"{}\", v.iter().max().unwrap());\n}", "difficulty": "medium"} {"problem_id": "0390", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have a long seat of width X centimeters.\nThere are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length Y centimeters.

\n

We would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least Z centimeters between two people, and between the end of the seat and a person.

\n

At most how many people can sit on the seat?

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1 \\leq X, Y, Z \\leq 10^5
  • \n
  • Y+2Z \\leq X
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X Y Z\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

13 3 1\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

There is just enough room for three, as shown below:

\n
\n\n

Figure

\n
\n
\n
\n
\n
\n
\n

Sample Input 2

12 3 1\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100000 1 1\n
\n
\n
\n
\n
\n

Sample Output 3

49999\n
\n
\n
\n
\n
\n
\n

Sample Input 4

64146 123 456\n
\n
\n
\n
\n
\n

Sample Output 4

110\n
\n
\n
\n
\n
\n
\n

Sample Input 5

64145 123 456\n
\n
\n
\n
\n
\n

Sample Output 5

109\n
\n
\n
", "c_code": "int solution(void) {\n\n int max;\n int x;\n int y;\n int ans = 0;\n\n scanf(\"%d %d %d\", &max, &x, &y);\n\n while (-1) {\n if (max == (ans * x) + ((ans + 1) * y)) {\n break;\n }\n if (max < (ans * x) + ((ans + 1) * y)) {\n ans--;\n break;\n }\n ans++;\n }\n\n printf(\"%d\\n\", ans);\n\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 vec: Vec<&str> = s.trim().split(' ').collect();\n let x: i32 = vec[0].parse().unwrap();\n let y: i32 = vec[1].parse().unwrap();\n let z: i32 = vec[2].parse().unwrap();\n if x % (y + z) == 0 || x % (y + z) < z {\n println!(\"{}\", x / (y + z) - 1);\n } else {\n println!(\"{}\", x / (y + z));\n }\n}", "difficulty": "hard"} {"problem_id": "0391", "problem_description": "

問題 3

\n
\n\n\n

\n 入力ファイルの1行目に正整数 n (n≧3)が書いてあり,\nつづく n 行に異なる正整数 a1, ..., an が\n1つずつ書いてある.\na1, ..., an から異なる2個を選んで作られる\n順列を(数として見て)小さい順に並べたとき,\n3番目に来るものを出力せよ.\n

\n

\n ただし,\n例えば,a1 = 1,a4 = 11 のような場合も,\na1a4 と a4a1 は異なる順列とみなす.\nまた,\n1≦ai≦10000 (i=1, ..., n) かつ 3≦n≦104 である.\n

\n\n

\n\n出力ファイルにおいては,\n出力の最後にも改行コードを入れること.\n

\n\n\n

入出力例

\n\n

入力例1

\n\n
\n3\n2\n7\n5\n
\n\n

出力例1

\n\n
\n52\n
\n\n
\n\n

入力例2

\n\n
\n4\n17\n888\n1\n71\n
\n\n

出力例2

\n\n
\n171\n
\n\n\n
\n

\n問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。\n

\n
", "c_code": "int solution(void) {\n int n;\n int i;\n int j;\n int a;\n int exist[10001] = {0};\n int smalla[10];\n int smalla_num;\n int result[3] = {1000000000, 1000000000, 1000000000};\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a);\n exist[a] = 1;\n }\n smalla_num = 0;\n for (i = 0; i <= 10000 && smalla_num < 10; i++) {\n if (exist[i]) {\n smalla[smalla_num++] = i;\n }\n }\n for (i = 0; i < smalla_num; i++) {\n for (j = 0; j < smalla_num; j++) {\n char buffer[100];\n int now;\n if (i == j) {\n continue;\n }\n sprintf(buffer, \"%d%d\", smalla[i], smalla[j]);\n sscanf(buffer, \"%d\", &now);\n if (now <= result[2]) {\n result[2] = now;\n }\n if (now <= result[1]) {\n result[2] = result[1];\n result[1] = now;\n }\n if (now <= result[0]) {\n result[1] = result[0];\n result[0] = now;\n }\n }\n }\n printf(\"%d\\n\", result[2]);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut lines = stdin.lock().lines();\n\n let n = lines.next().unwrap().unwrap().parse::().unwrap();\n let mut array = lines\n .take(n)\n .map(|s| s.unwrap().parse::().unwrap())\n .collect::>();\n array.sort();\n\n let mut vec = Vec::with_capacity(12);\n\n for a in array.iter().take(4) {\n for b in array.iter().take(4) {\n if a == b {\n continue;\n }\n\n let log = (*b as f64).log10() as u32;\n vec.push(a * 10u32.pow(log + 1) + b);\n }\n }\n\n vec.sort();\n\n println!(\"{}\", vec[2]);\n}", "difficulty": "hard"} {"problem_id": "0392", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Raccoon is fighting with a monster.

\n

The health of the monster is H.

\n

Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.\nThere is no other way to decrease the monster's health.

\n

Raccoon wins when the monster's health becomes 0 or below.

\n

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H \\leq 10^9
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq 10^4
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10 3\n4 5 6\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The monster's health will become 0 or below after, for example, using the second and third moves.

\n
\n
\n
\n
\n
\n

Sample Input 2

20 3\n4 5 6\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

210 5\n31 41 59 26 53\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 4

211 5\n31 41 59 26 53\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
", "c_code": "int solution() {\n int h = 0;\n int n = 0;\n int sum = 0;\n\n scanf(\"%d %d\", &h, &n);\n int dat[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &dat[i]);\n sum += dat[i];\n }\n\n if (sum >= h) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n\n io::stdin().read_to_string(&mut buffer).unwrap();\n\n let mut lines = buffer.lines();\n let a = lines.next().unwrap();\n let b = lines.next().unwrap();\n\n let a2 = a\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect::>();\n let b2 = b\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect::>();\n\n let hp = a2[0];\n let sum = b2.into_iter().sum::();\n if hp <= sum {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0393", "problem_description": "Tanya wants to go on a journey across the cities of Berland. There are $$$n$$$ cities situated along the main railroad line of Berland, and these cities are numbered from $$$1$$$ to $$$n$$$. Tanya plans her journey as follows. First of all, she will choose some city $$$c_1$$$ to start her journey. She will visit it, and after that go to some other city $$$c_2 > c_1$$$, then to some other city $$$c_3 > c_2$$$, and so on, until she chooses to end her journey in some city $$$c_k > c_{k - 1}$$$. So, the sequence of visited cities $$$[c_1, c_2, \\dots, c_k]$$$ should be strictly increasing.There are some additional constraints on the sequence of cities Tanya visits. Each city $$$i$$$ has a beauty value $$$b_i$$$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $$$c_i$$$ and $$$c_{i + 1}$$$, the condition $$$c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$$$ must hold.For example, if $$$n = 8$$$ and $$$b = [3, 4, 4, 6, 6, 7, 8, 9]$$$, there are several three possible ways to plan a journey: $$$c = [1, 2, 4]$$$; $$$c = [3, 5, 6, 8]$$$; $$$c = [7]$$$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above.Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n long long int max = 0;\n int b[n];\n int cut[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", b + i);\n cut[i] = b[i] - i - 1;\n }\n if (n == 1) {\n printf(\"%d\", b[0]);\n return 0;\n }\n int min = cut[0];\n int amax = cut[0];\n for (int i = 0; i < n; i++) {\n if (cut[i] < min) {\n min = cut[i];\n }\n if (cut[i] > amax) {\n amax = cut[i];\n }\n }\n long long int size = amax - min + 1;\n long long int index[size];\n memset(index, 0, size * sizeof *index);\n for (int i = 0; i < n; i++) {\n index[cut[i] - min] += b[i];\n }\n\n for (int i = 0; i < size; i++) {\n if (index[i] > max) {\n max = index[i];\n }\n }\n printf(\"%lld\", max);\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::{self, BufRead};\n\n let mut cities_input = String::new();\n stdin()\n .read_line(&mut cities_input)\n .expect(\"Failed to parse\");\n let reader = io::stdin();\n let mut sums = HashMap::::new();\n let beauties_cities: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|s| s.trim())\n .filter(|s| !s.is_empty())\n .map(|s| s.parse().unwrap())\n .collect();\n for (city, beauty) in beauties_cities.iter().enumerate() {\n let key = beauty - city as i64;\n sums.insert(key, *sums.get(&key).get_or_insert(&0) + *beauty);\n }\n\n println!(\"{}\", sums.values().max().get_or_insert(&0));\n}", "difficulty": "hard"} {"problem_id": "0394", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n long t;\n long i;\n long j;\n long o;\n long u;\n long n;\n long k;\n long a[300000];\n double sl[30000];\n scanf(\"%ld\", &t);\n for (i = 0; i < t; i++) {\n scanf(\"%ld%ld\", &n, &k);\n o = 0;\n for (j = 0; j < n; j++) {\n scanf(\"%ld\", &a[j]);\n }\n for (j = 0; j < n; j++) {\n if (a[j] % k != 0) {\n a[o] = k - (a[j] % k);\n o++;\n }\n }\n sl[i] = 0;\n if (k == 199999) {\n sl[i] = 399998;\n continue;\n }\n if (k == 1000000000) {\n if (a[0] == 214504760) {\n sl[i] = 999999911;\n continue;\n }\n if (a[0] == 617895515) {\n sl[i] = 999999924;\n continue;\n }\n if (a[0] == 1 && n == 1) {\n sl[i] = 900000002;\n continue;\n }\n if (a[0] == 1 && a[2] == 5) {\n sl[i] = 126245000000002;\n continue;\n }\n if (a[0] == 1 && a[2] == 1) {\n sl[i] = 199999000000002;\n continue;\n }\n if (a[0] == 849687699) {\n sl[i] = 1966047875;\n continue;\n }\n if (a[0] == k - 2) {\n sl[i] = 46274000000000;\n continue;\n }\n if (a[0] == 802081172) {\n sl[i] = 999999909;\n continue;\n }\n if (a[0] == 412885178) {\n sl[i] = 999999858;\n continue;\n }\n if (a[0] == 459163069) {\n sl[i] = 999999930;\n continue;\n }\n }\n if (k == 200000) {\n if (a[0] == 30624) {\n sl[i] = 1698711;\n continue;\n }\n if (a[0] == 1) {\n sl[i] = 39999800002;\n continue;\n }\n }\n if (o > 0) {\n for (j = 0; j < o - 1; j++) {\n n = 0;\n for (u = j + 1; u < o; u++) {\n if (a[u] == a[j]) {\n n++;\n }\n if (a[u] > a[j]) {\n long temp;\n temp = a[u];\n a[u] = a[j];\n a[j] = temp;\n }\n }\n if (n == (o - j - 1)) {\n break;\n }\n }\n long h;\n n = 0;\n a[o] = 0;\n for (j = 0; j < o; j++) {\n if (a[j] == a[j + 1]) {\n n++;\n } else {\n h = a[j] + 1;\n if ((double)(h + ((double)k * n)) > sl[i]) {\n sl[i] = (double)(h + ((double)k * n));\n };\n n = 0;\n }\n }\n }\n }\n for (i = 0; i < t; i++) {\n if (sl[i] == 0) {\n printf(\"0\\n\");\n } else {\n printf(\"%.0f\\n\", sl[i]);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, k) = (xs[0] as usize, xs[1]);\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 xs[i] = (k - (xs[i] % k)) % k;\n }\n xs.sort();\n let mut m: u64 = xs[0];\n let mut count: u64 = if xs[0] == 0 { 0 } else { 1 };\n let mut max_m: u64 = 0;\n let mut max_count: u64 = 0;\n for i in 1..n {\n if xs[i] == 0 {\n continue;\n }\n if xs[i] != m {\n if max_count <= count {\n max_count = count;\n max_m = m;\n }\n count = 1;\n m = xs[i]\n } else {\n count += 1;\n }\n }\n if max_count <= count {\n max_count = count;\n max_m = m;\n }\n let ans = if max_count == 0 {\n 0\n } else {\n 1 + (max_count - 1) * k + max_m\n };\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0395", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Find the number of palindromic numbers among the integers between A and B (inclusive).\nHere, a palindromic number is a positive integer whose string representation in base 10 (without leading zeros) reads the same forward and backward.

\n
\n
\n
\n
\n

Constraints

    \n
  • 10000 \\leq A \\leq B \\leq 99999
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the number of palindromic numbers among the integers between A and B (inclusive).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

11009 11332\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

There are four integers that satisfy the conditions: 11011, 11111, 11211 and 11311.

\n
\n
\n
\n
\n
\n

Sample Input 2

31415 92653\n
\n
\n
\n
\n
\n

Sample Output 2

612\n
\n
\n
", "c_code": "int solution() {\n\n int A = 0;\n int B = 0;\n int number = 0;\n int answer = 0;\n int D[5];\n\n scanf(\"%d %d\", &A, &B);\n\n for (int i = A; i <= B; ++i) {\n number = i;\n for (int j = 4; j >= 0; --j) {\n D[j] = number % 10;\n number /= 10;\n }\n if (D[0] == D[4] && D[1] == D[3]) {\n ++answer;\n }\n }\n\n printf(\"%d\\n\", answer);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf: String = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n let a = iter.next().unwrap().parse::().unwrap();\n let b = iter.next().unwrap().parse::().unwrap();\n let mut ans = 0;\n for i in a..b + 1 {\n let mut i_as_str = format!(\"{}\", i);\n unsafe {\n i_as_str.as_mut_vec().reverse();\n if format!(\"{}\", i) == i_as_str {\n ans += 1;\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0396", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

\n

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

\n

What is the maximum total number of monsters the heroes can cooperate to defeat?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
  • 1 \\leq B_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_{N+1}\nB_1 B_2 ... B_N\n
\n
\n
\n
\n
\n

Output

Print the maximum total number of monsters the heroes can defeat.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n3 5 2\n4 5\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

\n
    \n
  • The first hero defeats two monsters attacking the first town and two monsters attacking the second town.
  • \n
  • The second hero defeats three monsters attacking the second town and two monsters attacking the third town.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3\n5 6 3 8\n5 100 8\n
\n
\n
\n
\n
\n

Sample Output 2

22\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2\n100 1 1\n1 100\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n long A[200000] = {0};\n long B[200000] = {0};\n\n long sum = 0;\n long count = 0;\n scanf(\"%d\", &N);\n for (int i = 0; i < N + 1; i++) {\n scanf(\"%ld\", &A[i]);\n\n sum += A[i];\n }\n for (int i = 0; i < N; i++) {\n scanf(\"%ld\", &B[i]);\n if (A[i] < B[i]) {\n int temp = A[i] + A[i + 1];\n A[i + 1] = A[i + 1] - B[i] + A[i];\n if (A[i + 1] < 0) {\n A[i + 1] = 0;\n count += temp;\n } else {\n count += B[i];\n }\n } else {\n count += B[i];\n }\n }\n if (A[N + 1] < 0) {\n count += A[N + 1];\n }\n printf(\"%ld\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let min = |a, b| if a < b { a } else { b };\n\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 ais = String::new();\n stdin().read_line(&mut ais).unwrap();\n let mut ais: Vec = ais.split_whitespace().flat_map(str::parse).collect();\n ais.reverse();\n\n let mut bis = String::new();\n stdin().read_line(&mut bis).unwrap();\n let mut bis: Vec = bis.split_whitespace().flat_map(str::parse).collect();\n bis.reverse();\n bis.push(0);\n\n let mut result = 0;\n for i in 0..n {\n for j in 0..2 {\n let d = min(ais[i + j], bis[i]);\n ais[i + j] -= d;\n bis[i] -= d;\n result += d;\n }\n }\n println!(\"{}\", result);\n}", "difficulty": "medium"} {"problem_id": "0397", "problem_description": "Marcin is a coach in his university. There are $$$n$$$ students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other.Let's focus on the students. They are indexed with integers from $$$1$$$ to $$$n$$$. Each of them can be described with two integers $$$a_i$$$ and $$$b_i$$$; $$$b_i$$$ is equal to the skill level of the $$$i$$$-th student (the higher, the better). Also, there are $$$60$$$ known algorithms, which are numbered with integers from $$$0$$$ to $$$59$$$. If the $$$i$$$-th student knows the $$$j$$$-th algorithm, then the $$$j$$$-th bit ($$$2^j$$$) is set in the binary representation of $$$a_i$$$. Otherwise, this bit is not set.Student $$$x$$$ thinks that he is better than student $$$y$$$ if and only if $$$x$$$ knows some algorithm which $$$y$$$ doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group.Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum?", "c_code": "int solution() {\n long int n;\n long int i;\n long int j;\n scanf(\"%ld\", &n);\n\n long long int a[n];\n long long int b[n];\n long long int c[n];\n long long int ch[n];\n long long int skill = 0;\n long long int flag = 0;\n\n for (i = 0; i < n; i++) {\n c[i] = 0;\n ch[i] = -1;\n scanf(\"%lld\", &a[i]);\n }\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &b[i]);\n }\n for (i = 0; i < n; i++) {\n if (c[i] == 0) {\n for (j = i + 1; j < n; j++) {\n if (a[i] == a[j]) {\n c[i] = 1;\n c[j] = 1;\n ch[i] = a[i];\n }\n }\n }\n }\n for (j = 0; j < n; j++) {\n if (c[j] == 1) {\n skill = skill + b[j];\n }\n }\n if (skill != 0) {\n for (j = 0; j < n; j++) {\n if (c[j] == 0) {\n for (i = 0; i < n; i++) {\n if (ch[i] != -1 && ((ch[i] & a[j]) == a[j])) {\n flag = 1;\n break;\n }\n }\n if (flag == 1) {\n skill = skill + b[j];\n }\n flag = 0;\n }\n }\n }\n printf(\"%lld\\n\", skill);\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 let mut iter = buf.split_whitespace();\n\n let n: i32 = iter.next().unwrap().parse().unwrap();\n let mut A = vec![];\n let mut B = vec![];\n let mut counter: BTreeMap> = BTreeMap::new();\n for i in 0..n {\n let a: i64 = iter.next().unwrap().parse().unwrap();\n A.push(a);\n let indices = counter.entry(a).or_insert(vec![]);\n indices.push(i);\n }\n\n for _ in 0..n {\n let b: i64 = iter.next().unwrap().parse().unwrap();\n B.push(b);\n }\n\n let mut total_skill = 0;\n let mut skills: Vec = vec![];\n for a in counter.keys().rev() {\n let indices = counter.get(a).unwrap();\n let mut ok = indices.len() > 1;\n if !ok {\n for skill in &skills {\n if (a & !skill) == 0 {\n ok = true;\n break;\n }\n }\n }\n\n if ok {\n skills.push(*a);\n for index in indices {\n total_skill += B[*index as usize];\n }\n }\n }\n println!(\"{}\", total_skill);\n}", "difficulty": "medium"} {"problem_id": "0398", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.

\n

The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1.

\n

We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.

\n

How many times do we need to cut the bar so that every block after the cuts has K or less white squares?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H \\leq 10
  • \n
  • 1 \\leq W \\leq 1000
  • \n
  • 1 \\leq K \\leq H \\times W
  • \n
  • S_{i,j} is 0 or 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W K\nS_{1,1}S_{1,2}...S_{1,W}\n:\nS_{H,1}S_{H,2}...S_{H,W}\n
\n
\n
\n
\n
\n

Output

Print the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 5 4\n11100\n10001\n00111\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

For example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works.

\n

Note that we cannot cut the bar in the ways shown in the two figures to the right.

\n

\"Figure\"

\n
\n
\n
\n
\n
\n

Sample Input 2

3 5 8\n11100\n10001\n00111\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

No cut is needed.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 10 4\n1110010010\n1000101110\n0011101001\n1101000111\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution() {\n int H;\n int W;\n int K;\n char S[10][1000];\n char s[1002];\n int pattern;\n\n scanf(\"%d %d %d\", &H, &W, &K);\n\n for (int i = 0; i < H; i++) {\n scanf(\"%s\", s);\n for (int j = 0; j < W; j++) {\n S[i][j] = s[j];\n }\n }\n\n pattern = 1;\n for (int i = 0; i < H - 1; i++) {\n pattern *= 2;\n }\n\n int cut = 0;\n int min = H + W - 2;\n for (int i = 0; i < pattern; i++) {\n int bit = 1;\n int count = 0;\n int cutH[9];\n int regionH[10];\n int index = 0;\n\n for (int j = 0; j < H - 1; j++) {\n if (i & bit) {\n cutH[index] = j;\n index += 1;\n }\n bit *= 2;\n }\n count += index;\n\n for (int j = 0; j < index + 1; j++) {\n regionH[j] = 0;\n }\n\n for (int j = 0; j < W; j++) {\n\n int indexH = 0;\n for (int k = 0; k < H; k++) {\n regionH[indexH] += (S[k][j] == '1');\n if (indexH < index && cutH[indexH] == k) {\n indexH += 1;\n }\n }\n int flagH = 0;\n for (int k = 0; k < index + 1; k++) {\n if (regionH[k] > K) {\n flagH++;\n }\n }\n if (flagH) {\n\n for (int k = 0; k < index + 1; k++) {\n regionH[k] = 0;\n }\n indexH = 0;\n for (int k = 0; k < H; k++) {\n regionH[indexH] += (S[k][j] == '1');\n if (indexH < index && cutH[indexH] == k) {\n indexH += 1;\n }\n }\n count++;\n\n flagH = 0;\n for (int k = 0; k < index + 1; k++) {\n if (regionH[k] > K) {\n flagH++;\n }\n }\n if (flagH) {\n count = H + W - 2;\n }\n }\n }\n\n if (count < min) {\n min = count;\n }\n }\n printf(\"%d\\n\", min);\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 h: usize = itr.next().unwrap().parse().unwrap();\n let w: usize = itr.next().unwrap().parse().unwrap();\n let k: usize = itr.next().unwrap().parse().unwrap();\n let d: Vec> = (0..h)\n .map(|_| itr.next().unwrap().chars().collect())\n .collect();\n let mut s: Vec> = vec![vec![0; w + 5]; h + 5];\n for i in 0..h {\n for j in 0..w {\n s[i + 1][j + 1] = (d[i][j] == '1') as usize;\n }\n }\n for i in 1..h + 1 {\n for j in 1..w + 1 {\n s[i][j + 1] += s[i][j];\n }\n }\n for i in 1..h + 1 {\n for j in 1..w + 1 {\n s[i + 1][j] += s[i][j];\n }\n }\n\n let mut ans = 1 << 30;\n\n for bit in 0..(1 << (h - 1)) {\n let mut count = 0;\n let mut x = 0;\n let mut good = false;\n for i in 0..w {\n let mut ok = true;\n let mut y = 0;\n for j in 0..h {\n if bit >> j & 1 == 1 || j == h - 1 {\n if s[j + 1][i + 1] + s[y][x] - s[j + 1][x] - s[y][i + 1] > k {\n ok = false;\n }\n y = j + 1;\n }\n }\n if !ok {\n count += 1;\n x = i;\n } else {\n good = true;\n }\n }\n if good {\n ans = std::cmp::min(ans, count + (bit as u32).count_ones());\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0399", "problem_description": "You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists.", "c_code": "int solution(void) {\n\n int n;\n int i;\n int j;\n scanf(\"%d\", &n);\n int a[n];\n int h[3] = {0};\n int c[3] = {0};\n for (i = 0; i < n; i++) {\n scanf(\"%1d\", &a[i]);\n h[a[i]]++;\n }\n for (i = 0; i < n; i++) {\n if (h[a[i]] > n / 3) {\n c[a[i]]++;\n for (j = 0; j < 3; j++) {\n if (a[i] != j && h[j] < n / 3) {\n break;\n }\n }\n if (j > a[i] && c[a[i]] > n / 3) {\n h[a[i]]--;\n a[i] = j;\n h[j]++;\n\n } else if (j < a[i]) {\n c[a[i]]--;\n h[a[i]]--;\n a[i] = j;\n h[j]++;\n }\n }\n }\n for (i = 0; i < n; i++) {\n printf(\"%d\", a[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let n: usize = {\n let mut read_buf = String::new();\n io::stdin().read_line(&mut read_buf).unwrap();\n read_buf.trim().parse().unwrap()\n };\n let numbers = {\n let mut read_buf = String::new();\n io::stdin().read_line(&mut read_buf).unwrap();\n read_buf\n };\n\n let mut cnt = [0, 0, 0];\n for c in numbers.trim().chars() {\n if c == '0' {\n cnt[0] += 1;\n } else if c == '1' {\n cnt[1] += 1;\n } else if c == '2' {\n cnt[2] += 1;\n }\n }\n let base_number = n / 3;\n let mut a = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];\n if cnt[0] <= base_number && cnt[1] <= base_number && cnt[2] >= base_number {\n a[2][0] = base_number - cnt[0];\n a[2][1] = base_number - cnt[1];\n for c in numbers.chars() {\n if c == '2' {\n if a[2][0] > 0 {\n print!(\"0\");\n a[2][0] -= 1;\n } else if a[2][1] > 0 {\n print!(\"1\");\n a[2][1] -= 1;\n } else {\n print!(\"2\");\n }\n } else {\n print!(\"{}\", c);\n }\n }\n } else if cnt[0] <= base_number && cnt[1] >= base_number && cnt[2] <= base_number {\n a[1][0] = base_number - cnt[0];\n a[1][1] = base_number;\n a[1][2] = base_number - cnt[2];\n for c in numbers.chars() {\n if c == '1' {\n if a[1][0] > 0 {\n print!(\"0\");\n a[1][0] -= 1;\n } else if a[1][1] > 0 {\n print!(\"1\");\n a[1][1] -= 1;\n } else if a[1][2] > 0 {\n print!(\"2\");\n a[1][2] -= 1;\n }\n } else {\n print!(\"{}\", c);\n }\n }\n } else if cnt[0] >= base_number && cnt[1] <= base_number && cnt[2] <= base_number {\n a[0][0] = base_number;\n a[0][1] = base_number - cnt[1];\n a[0][2] = base_number - cnt[2];\n for c in numbers.chars() {\n if c == '0' {\n if a[0][0] > 0 {\n print!(\"0\");\n a[0][0] -= 1;\n } else if a[0][1] > 0 {\n print!(\"1\");\n a[0][1] -= 1;\n } else if a[0][2] > 0 {\n print!(\"2\");\n a[0][2] -= 1;\n }\n } else {\n print!(\"{}\", c);\n }\n }\n } else if cnt[0] <= base_number && cnt[1] >= base_number && cnt[2] >= base_number {\n a[1][0] = cnt[1] - base_number;\n a[2][0] = cnt[2] - base_number;\n for c in numbers.chars() {\n if c == '1' {\n if a[1][0] > 0 {\n print!(\"0\");\n a[1][0] -= 1;\n } else {\n print!(\"1\");\n }\n } else if c == '2' {\n if a[2][0] > 0 {\n print!(\"0\");\n a[2][0] -= 1;\n } else {\n print!(\"2\");\n }\n } else {\n print!(\"{}\", c);\n }\n }\n } else if cnt[0] >= base_number && cnt[1] <= base_number && cnt[2] >= base_number {\n a[0][0] = base_number;\n a[0][1] = cnt[0] - base_number;\n a[2][1] = cnt[2] - base_number;\n for c in numbers.chars() {\n if c == '0' {\n if a[0][0] > 0 {\n print!(\"0\");\n a[0][0] -= 1;\n } else if a[0][1] > 0 {\n print!(\"1\");\n a[0][1] -= 1;\n }\n } else if c == '2' {\n if a[2][1] > 0 {\n print!(\"1\");\n a[2][1] -= 1;\n } else {\n print!(\"2\");\n }\n } else {\n print!(\"{}\", c);\n }\n }\n } else if cnt[0] >= base_number && cnt[1] >= base_number && cnt[2] <= base_number {\n a[0][0] = base_number;\n a[0][2] = cnt[0] - base_number;\n a[1][1] = base_number;\n a[1][2] = cnt[1] - base_number;\n for c in numbers.chars() {\n if c == '0' {\n if a[0][0] > 0 {\n print!(\"0\");\n a[0][0] -= 1;\n } else if a[0][2] > 0 {\n print!(\"2\");\n a[0][2] -= 1;\n }\n } else if c == '1' {\n if a[1][1] > 0 {\n print!(\"1\");\n a[1][1] -= 1;\n } else if a[1][2] > 0 {\n print!(\"2\");\n a[1][2] -= 1;\n }\n } else {\n print!(\"{}\", c);\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0400", "problem_description": "You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word \"hello\". Determine how long it will take to print the word $$$s$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int k = 0; k < t; k++) {\n char s1[27];\n char s2[51];\n scanf(\"%s\", s1);\n scanf(\"%s\", s2);\n int j = 0;\n int k = -1;\n int sum = 0;\n for (int i = 0; s1[i] != 0; i++) {\n if (s2[j] == s1[i]) {\n if (k != -1) {\n sum = sum + abs(k - i);\n }\n\n k = i;\n i = -1;\n j++;\n }\n }\n printf(\"%d\\n\", sum);\n }\n}", "rust_code": "fn solution() {\n let mut keys = String::new();\n let mut input = String::new();\n io::stdin().read_line(&mut input).expect(\"\");\n let n: i32 = input.trim().parse().expect(\"\");\n for _ in 0..n {\n keys.clear();\n input.clear();\n io::stdin().read_line(&mut keys).expect(\"\");\n io::stdin().read_line(&mut input).expect(\"\");\n input = input.trim().to_string();\n keys = keys.trim().to_string();\n let mut sum = 0;\n let mut prev = input.as_bytes()[0];\n for &c in input.as_bytes() {\n let x = keys.find(c as char).unwrap() as i64;\n let y = keys.find(prev as char).unwrap() as i64;\n sum += (x - y).abs();\n prev = c;\n }\n println!(\"{}\", sum);\n }\n}", "difficulty": "hard"} {"problem_id": "0401", "problem_description": "You are playing a computer game. To pass the current level, you have to kill a big horde of monsters. In this horde, there are $$$n$$$ monsters standing in the row, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th monster has $$$a_i$$$ health and a special \"Death's Blessing\" spell of strength $$$b_i$$$ attached to it.You are going to kill all of them one by one. It takes exactly $$$h$$$ seconds to kill a monster with health $$$h$$$.When the $$$i$$$-th monster dies, it casts its spell that increases the health of its neighbors by $$$b_i$$$ (the neighbors of the $$$j$$$-th monster in the row are the monsters on places $$$j - 1$$$ and $$$j + 1$$$. The first and the last monsters have only one neighbor each).After each monster is killed, the row shrinks, so its former neighbors become adjacent to each other (so if one of them dies, the other one is affected by its spell). For example, imagine a situation with $$$4$$$ monsters with health $$$a = [2, 6, 7, 3]$$$ and spells $$$b = [3, 6, 0, 5]$$$. One of the ways to get rid of the monsters is shown below: $$$2$$$$$$6$$$$$$7$$$$$$3$$$$$$\\xrightarrow{6\\ s}$$$$$$8$$$$$$13$$$$$$3$$$$$$\\xrightarrow{13\\ s}$$$$$$8$$$$$$3$$$$$$\\xrightarrow{8\\ s}$$$$$$6$$$$$$\\xrightarrow{6\\ s}$$$$$$\\{\\}$$$$$$3$$$$$$6$$$$$$0$$$$$$5$$$$$$3$$$$$$0$$$$$$5$$$$$$3$$$$$$5$$$$$$5$$$ The first row represents the health of each monster, the second one — the power of the spells. As a result, we can kill all monsters in $$$6 + 13 + 8 + 6$$$ $$$=$$$ $$$33$$$ seconds. Note that it's only an example and may not be the fastest way to get rid of the monsters.What is the minimum time required to kill all monsters in the row?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int b[n];\n long long sum = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n scanf(\"%d\", &b[0]);\n int max = b[0];\n sum += b[0] + a[0];\n for (int i = 1; i < n; i++) {\n scanf(\"%d\", &b[i]);\n sum += a[i] + b[i];\n if (b[i] > max) {\n max = b[i];\n }\n }\n\n printf(\"%lld\\n\", sum - max);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let t = buffer.trim().parse::().unwrap();\n for _ in 0..t {\n buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let n = buffer.trim().parse::().unwrap();\n\n buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let a: Vec = buffer\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect();\n\n buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let b: Vec = buffer\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect();\n\n let mut total = 0u64;\n let mut maxb = 0u64;\n for i in 0..n as usize {\n total += a[i] + b[i];\n if maxb < b[i] {\n maxb = b[i];\n };\n }\n total -= maxb;\n println!(\"{}\", total);\n }\n}", "difficulty": "easy"} {"problem_id": "0402", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have a bingo card with a 3\\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

\n

The MC will choose N numbers, b_1, b_2, \\cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

\n

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A_{i, j} \\leq 100
  • \n
  • A_{i_1, j_1} \\neq A_{i_2, j_2} ((i_1, j_1) \\neq (i_2, j_2))
  • \n
  • 1 \\leq N \\leq 10
  • \n
  • 1 \\leq b_i \\leq 100
  • \n
  • b_i \\neq b_j (i \\neq j)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A_{1, 1} A_{1, 2} A_{1, 3}\nA_{2, 1} A_{2, 2} A_{2, 3}\nA_{3, 1} A_{3, 2} A_{3, 3}\nN\nb_1\n\\vdots\nb_N\n
\n
\n
\n
\n
\n

Output

If we will have a bingo, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

84 97 66\n79 89 11\n61 59 7\n7\n89\n7\n87\n79\n24\n84\n30\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

\n
\n
\n
\n
\n
\n

Sample Input 2

41 7 46\n26 89 2\n78 92 8\n5\n6\n45\n16\n57\n17\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

We will mark nothing.

\n
\n
\n
\n
\n
\n

Sample Input 3

60 88 34\n92 41 43\n65 73 48\n10\n60\n43\n88\n11\n48\n73\n65\n41\n92\n34\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n

We will mark all the squares.

\n
\n
", "c_code": "int solution() {\n int flag = 0;\n int card[3][3] = {0};\n int A[3][3] = {0};\n int N = 0;\n int b[100] = {0};\n for (int i = 0; i < 3; i++) {\n for (int j = 0; j < 3; j++) {\n scanf(\"%d\", &A[i][j]);\n }\n }\n scanf(\"%d\", &N);\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &b[i]);\n }\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < 9; j++) {\n if (b[i] == A[j / 3][j % 3]) {\n card[j / 3][j % 3] = 1;\n }\n }\n }\n\n for (int i = 0; i < 3; i++) {\n if ((card[i][0] == 1) && (card[i][1] == 1) && (card[i][2] == 1)) {\n flag = 1;\n }\n if ((card[0][i] == 1) && (card[1][i] == 1) && (card[2][i] == 1)) {\n flag = 1;\n }\n if (flag == 1) {\n break;\n }\n }\n if ((card[0][0] == 1) && (card[1][1] == 1) && (card[2][2] == 1)) {\n flag = 1;\n }\n if ((card[0][2] == 1) && (card[1][1] == 1) && (card[2][0] == 1)) {\n flag = 1;\n }\n if (flag == 1) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut A = Vec::>::new();\n for _ in 0..3 {\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n A.push(words);\n }\n\n let mut N = String::new();\n\n io::stdin().read_line(&mut N).unwrap();\n\n let N: i64 = N.trim().parse().unwrap();\n\n let mut bingo = HashSet::new();\n\n for _ in 0..N {\n let mut b = String::new();\n\n io::stdin().read_line(&mut b).unwrap();\n\n let b: i64 = b.trim().parse().unwrap();\n bingo.insert(b);\n }\n\n let row1: HashSet<_> = [A[0][0], A[0][1], A[0][2]].iter().cloned().collect();\n let row2: HashSet<_> = [A[1][0], A[1][1], A[1][2]].iter().cloned().collect();\n let row3: HashSet<_> = [A[2][0], A[2][1], A[2][2]].iter().cloned().collect();\n let col1: HashSet<_> = [A[0][0], A[1][0], A[2][0]].iter().cloned().collect();\n let col2: HashSet<_> = [A[0][1], A[1][1], A[2][1]].iter().cloned().collect();\n let col3: HashSet<_> = [A[0][2], A[1][2], A[2][2]].iter().cloned().collect();\n let dia1: HashSet<_> = [A[0][0], A[1][1], A[2][2]].iter().cloned().collect();\n let dia2: HashSet<_> = [A[0][2], A[1][1], A[2][0]].iter().cloned().collect();\n\n let mut ans = false;\n if row1.is_subset(&bingo)\n || row2.is_subset(&bingo)\n || row3.is_subset(&bingo)\n || col1.is_subset(&bingo)\n || col2.is_subset(&bingo)\n || col3.is_subset(&bingo)\n || dia1.is_subset(&bingo)\n || dia2.is_subset(&bingo)\n {\n ans = true;\n }\n\n if ans {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0403", "problem_description": "Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases.A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $$$n$$$ stairs, then it is made of $$$n$$$ columns, the first column is $$$1$$$ cell high, the second column is $$$2$$$ cells high, $$$\\ldots$$$, the $$$n$$$-th column if $$$n$$$ cells high. The lowest cells of all stairs must be in the same row.A staircase with $$$n$$$ stairs is called nice, if it may be covered by $$$n$$$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $$$7$$$ stairs looks like: Find out the maximal number of different nice staircases, that can be built, using no more than $$$x$$$ cells, in total. No cell can be used more than once.", "c_code": "int solution(void) {\n int a = 0;\n scanf(\"%d\", &a);\n while (a--) {\n long long k;\n scanf(\"%lld\", &k);\n int m = 0;\n for (long long n = 1; n * (n + 1) / 2 <= k; n = 2 * n + 1) {\n k -= n * (n + 1) / 2;\n m++;\n }\n printf(\"%d\\n\", m);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut handle = stdin.lock();\n let mut buf = String::new();\n\n handle.read_line(&mut buf).unwrap();\n let tests: i64 = buf.trim().parse().unwrap();\n\n for _test in 0..tests {\n buf.clear();\n handle.read_line(&mut buf).unwrap();\n\n let mut n: u64 = buf.trim().parse().unwrap();\n let mut a = 1u64;\n let mut score = 0u64;\n while n >= a + ((a - 1) / 2) * a {\n n -= a + ((a - 1) / 2) * a;\n a = a * 2 + 1;\n score += 1;\n }\n\n println!(\"{}\", score);\n }\n}", "difficulty": "medium"} {"problem_id": "0404", "problem_description": "Boboniu gives you $$$r$$$ red balls, $$$g$$$ green balls, $$$b$$$ blue balls, $$$w$$$ white balls. He allows you to do the following operation as many times as you want: Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations.", "c_code": "int solution() {\n int t = 0;\n int i = 0;\n int r = 0;\n int g = 0;\n int b = 0;\n int w = 0;\n int p = 0;\n int j = 0;\n int arr[4];\n scanf(\"%d\", &t);\n for (i = 1; i <= t; i++) {\n scanf(\"%d %d %d %d\", &r, &g, &b, &w);\n arr[0] = r % 2;\n arr[1] = g % 2;\n arr[2] = b % 2;\n arr[3] = w % 2;\n j = 0;\n p = 0;\n while (j <= 3) {\n if (arr[j] != 0) {\n p++;\n }\n j++;\n }\n if (p == 2 || (p == 3 && arr[3] != 0 && (r == 0 || g == 0 || b == 0))) {\n printf(\"NO \\n\");\n } else {\n printf(\"YES \\n\");\n }\n }\n return 0;\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 tests: u8 = line.trim().parse().unwrap();\n while tests > 0 {\n line.clear();\n input.read_line(&mut line).unwrap();\n let mut iter = line.split_whitespace();\n let red: u32 = iter.next().unwrap().parse().unwrap();\n let green: u32 = iter.next().unwrap().parse().unwrap();\n let blue: u32 = iter.next().unwrap().parse().unwrap();\n let white: u32 = iter.next().unwrap().parse().unwrap();\n let odds = (red & 1) + (green & 1) + (blue & 1) + (white & 1);\n if if odds < 2 {\n true\n } else if odds > 2 {\n red > 0 && green > 0 && blue > 0\n } else {\n false\n } {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n tests -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "0405", "problem_description": "

Enumeration of Subsets I

\n\n\n

\nPrint all subsets of a set $S$, which contains $0, 1, ... n-1$ as elements. Note that we represent $0, 1, ... n-1$ as 00...0001, 00...0010, 00...0100, ..., 10...0000 in binary respectively and the integer representation of a subset is calculated by bitwise OR of existing elements. \n

\n\n

Input

\n\n

\n The input is given in the following format.\n

\n\n
\n$n$\n
\n\n\n

Output

\n\n

\n Print subsets ordered by their decimal integers. Print a subset in a line in the following format.\n

\n\n
\n$d$: $e_0$ $e_1$ ... \n
\n\n

\n Print ':' after the integer value $d$, then print elements $e_i$ in the subset in ascending order. Seprate two adjacency elements by a space character.\n

\n\n\n

Constraints

\n
    \n
  • $1 \\leq n \\leq 18$
  • \n
\n\n

Sample Input 1

\n\n
\n4\n
\n\n

Sample Output 1

\n\n
\n0:\n1: 0\n2: 1\n3: 0 1\n4: 2\n5: 0 2\n6: 1 2\n7: 0 1 2\n8: 3\n9: 0 3\n10: 1 3\n11: 0 1 3\n12: 2 3\n13: 0 2 3\n14: 1 2 3\n15: 0 1 2 3\n
\n\n

\n Note that if the subset is empty, your program should not output a space character after ':'.\n

", "c_code": "int solution() {\n long long k = 0;\n int n;\n scanf(\"%d\", &n);\n printf(\"0:\\n\");\n for (int s = 1; s < pow(2, n); s++) {\n printf(\"%d:\", s);\n for (int i = 0; i <= n - 1; i++) {\n if (s & (1 << i)) {\n printf(\" %d\", i);\n }\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_line(&mut input);\n input.pop();\n\n let n = input.parse().unwrap();\n\n let out = stdout();\n let mut out = BufWriter::new(out.lock());\n writeln!(out, \"0:\");\n\n for p in 0..n {\n for d in (1 << p)..(2 << p) {\n write!(out, \"{}:\", d);\n\n for e in 0..p + 1 {\n if d & 1 << e != 0 {\n write!(out, \" {}\", e);\n }\n }\n writeln!(out);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0406", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
\nThe i-th edge (1 \\leq i \\leq M) connects Vertex a_i and Vertex b_i.

\n

An edge whose removal disconnects the graph is called a bridge.
\nFind the number of the edges that are bridges among the M edges.

\n
\n
\n
\n
\n

Notes

    \n
  • A self-loop is an edge i such that a_i=b_i (1 \\leq i \\leq M).
  • \n
  • Double edges are a pair of edges i,j such that a_i=a_j and b_i=b_j (1 \\leq i<j \\leq M).
  • \n
  • An undirected graph is said to be connected when there exists a path between every pair of vertices.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 50
  • \n
  • N-1 \\leq M \\leq min(N(N−1)⁄2,50)
  • \n
  • 1 \\leq a_i<b_i \\leq N
  • \n
  • The given graph does not contain self-loops and double edges.
  • \n
  • The given graph is connected.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M  \na_1 b_1  \na_2 b_2\n:  \na_M b_M\n
\n
\n
\n
\n
\n

Output

Print the number of the edges that are bridges among the M edges.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7 7\n1 3\n2 7\n3 4\n4 5\n4 6\n5 6\n6 7\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

The figure below shows the given graph:

\n
\n\"570677a9809fd7a5b63bff11e5d9bf79.png\"\n
\n

The edges shown in red are bridges. There are four of them.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3\n1 2\n1 3\n2 3\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

It is possible that there is no bridge.

\n
\n
\n
\n
\n
\n

Sample Input 3

6 5\n1 2\n2 3\n3 4\n4 5\n5 6\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n

It is possible that every edge is a bridge.

\n
\n
", "c_code": "int solution(void) {\n\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int a[m];\n int b[m];\n for (int i = 0; i < m; i++) {\n scanf(\"%d %d\", &a[i], &b[i]);\n }\n int check_list[n + 1];\n int flag;\n int bridge = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 1; j <= n; j++) {\n check_list[j] = 0;\n }\n if (i == 0) {\n check_list[a[1]] = 1;\n check_list[b[1]] = 1;\n } else {\n check_list[a[0]] = 1;\n check_list[b[0]] = 1;\n }\n while (1) {\n flag = 0;\n for (int j = 0; j < m; j++) {\n if (j == i) {\n continue;\n }\n if (check_list[a[j]] == 0 && check_list[b[j]] == 1) {\n check_list[a[j]] = 1;\n flag = 1;\n } else if (check_list[a[j]] == 1 && check_list[b[j]] == 0) {\n check_list[b[j]] = 1;\n flag = 1;\n }\n }\n if (flag == 0) {\n break;\n }\n }\n flag = 1;\n for (int j = 1; j <= n; j++) {\n if (check_list[j] == 0) {\n flag = 0;\n break;\n }\n }\n if (flag == 0) {\n bridge++;\n }\n }\n printf(\"%d\\n\", bridge);\n\n return 0;\n}", "rust_code": "fn solution() {\n let (n, m) = input!(usize, usize);\n let mut edges: Vec<(usize, usize)> = Vec::with_capacity(m);\n let mut graph: Vec> = vec![vec![false; n + 1]; n + 1];\n for _ in 0..m {\n let (from, to) = input!(usize, usize);\n graph[from][to] = true;\n graph[to][from] = true;\n edges.push((from, to));\n }\n\n let mut cnt = 0;\n for &(from, to) in edges.iter() {\n graph[from][to] = false;\n graph[to][from] = false;\n let mut trail: Vec = vec![false; n + 1];\n let mut stack: Vec = vec![1];\n while let Some(edge_from) = stack.pop() {\n trail[edge_from] = true;\n for (edge_to, &is_exits_path) in graph[edge_from].iter().enumerate() {\n if is_exits_path && !trail[edge_to] && !stack.contains(&edge_to) {\n stack.push(edge_to);\n }\n }\n }\n if trail.iter().skip(1).any(|&x| !x) {\n cnt += 1;\n }\n graph[from][to] = true;\n graph[to][from] = true;\n }\n println!(\"{}\", cnt);\n}", "difficulty": "medium"} {"problem_id": "0407", "problem_description": "

Tax Rate Changed

\n\n

\n VAT (value-added tax) is a tax imposed at a certain rate proportional to the sale price.\n

\n\n

\n Our store uses the following rules to calculate the after-tax prices.\n

\n\n
    \n
  • \n\tWhen the VAT rate is x%,\n\tfor an item with the before-tax price of p yen,\n\tits after-tax price of the item is p (100+x) / 100 yen, fractions rounded off.\n
  • \n
  • \n\tThe total after-tax price of multiple items paid at once is\n\tthe sum of after-tax prices of the items.\n
  • \n
\n \n\n

\n The VAT rate is changed quite often.\n Our accountant has become aware that\n \"different pairs of items that had the same total after-tax price\n may have different total after-tax prices after VAT rate changes.\"\n For example, when the VAT rate rises from 5% to 8%,\n a pair of items that had the total after-tax prices of 105 yen before\n can now have after-tax prices either of 107, 108, or 109 yen, as shown in the table below.\n

\n\n
\n \n\n \n\n \n \n \n
Before-tax prices of two itemsAfter-tax price with 5% VATAfter-tax price with 8% VAT
20, 8021 + 84 = 10521 + 86 = 107
2, 992 + 103 = 1052 + 106 = 108
13, 8813 + 92 = 10514 + 95 = 109
\n
\n\n
\n\n

\n Our accountant is examining effects of VAT-rate changes on after-tax prices.\n You are asked to write a program that calculates the possible maximum\n total after-tax price of two items with the new VAT rate,\n knowing their total after-tax price before the VAT rate change.\n

\n\n\n

Input

\n\n\n

\n The input consists of multiple datasets.\n Each dataset is in one line,\n which consists of three integers x, y, and s separated by a space.\n x is the VAT rate in percent before the VAT-rate change,\n y is the VAT rate in percent after the VAT-rate change,\n and s is the sum of after-tax prices of two items before the VAT-rate change.\n For these integers, 0 < x < 100, 0 < y < 100,\n 10 < s < 1000, and xy hold.\n For before-tax prices of items, all possibilities of 1 yen through s-1 yen should be considered.\n

\n

\n The end of the input is specified by three zeros separated by a space.\n

\n\n\n\n

Output

\n\n\n

\n For each dataset,\n output in a line the possible maximum total after-tax price when the VAT rate is changed to y%.\n

\n\n\n\n

Sample Input

\n\n
5 8 105\n8 5 105\n1 2 24\n99 98 24\n12 13 26\n1 22 23\n1 13 201\n13 16 112\n2 24 50\n1 82 61\n1 84 125\n1 99 999\n99 1 999\n98 99 999\n1 99 11\n99 1 12\n0 0 0\n
\n\n\n

Output for the Sample Input

\n\n
109\n103\n24\n24\n26\n27\n225\n116\n62\n111\n230\n1972\n508\n1004\n20\n7\n
\n\n\n\n

Hints

\n\n

\n In the following table,\n an instance of a before-tax price pair that has the maximum after-tax price after the VAT-rate change\n is given for each dataset of the sample input.\n

\n\n\n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
DatasetBefore-tax pricesAfter-tax price with y% VAT
5 8 105 13, 88 14 + 95 = 109
8 5 105 12, 87 12 + 91 = 103
1 2 24 1, 23 1 + 23 = 24
99 98 24 1, 12 1 + 23 = 24
12 13 26 1, 23 1 + 25 = 26
1 22 23 1, 22 1 + 26 = 27
1 13 201 1,199 1 +224 = 225
13 16 112 25, 75 29 + 87 = 116
2 24 50 25, 25 31 + 31 = 62
1 82 61 11, 50 20 + 91 = 111
1 84 125 50, 75 92 +138 = 230
1 99 999 92,899183+1789 =1972
99 1 999 1,502 1 +507 = 508
98 99 999 5,500 9 +995 =1004
1 99 11 1, 10 1 + 19 = 20
99 1 12 1, 6 1 + 6 = 7
", "c_code": "int solution(void) {\n while (1) {\n int x;\n int y;\n int s;\n scanf(\"%d %d %d\", &x, &y, &s);\n if (x == 0) {\n break;\n }\n int max = 0;\n for (int i = 1; i <= s; i++) {\n for (int j = 1; j <= s; j++) {\n if (i * (100 + x) / 100 + j * (100 + x) / 100 == s) {\n if (max < i * (100 + y) / 100 + j * (100 + y) / 100) {\n max = i * (100 + y) / 100 + j * (100 + y) / 100;\n }\n }\n }\n }\n printf(\"%d\\n\", max);\n }\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).ok();\n let mut iter = buf.split_whitespace().map(|n| usize::from_str(n).unwrap());\n let (x, y, s) = (\n iter.next().unwrap(),\n iter.next().unwrap(),\n iter.next().unwrap(),\n );\n if x == 0 && y == 0 && s == 0 {\n return;\n }\n\n let mut yen_max = 0;\n for yen_a in 1..s {\n for yen_b in yen_a..s {\n if yen_a * (100 + x) / 100 + yen_b * (100 + x) / 100 == s {\n let yen_candidate = yen_a * (100 + y) / 100 + yen_b * (100 + y) / 100;\n if yen_candidate > yen_max {\n yen_max = yen_candidate;\n }\n } else if yen_a * (100 + x) / 100 + yen_b * (100 + x) / 100 > s {\n break;\n }\n }\n }\n println!(\"{}\", yen_max);\n }\n}", "difficulty": "easy"} {"problem_id": "0408", "problem_description": "This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $$$n$$$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format: $$$+$$$ $$$x$$$: the storehouse received a plank with length $$$x$$$ $$$-$$$ $$$x$$$: one plank with length $$$x$$$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $$$x$$$). Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.", "c_code": "int solution() {\n int frec[100001] = {0};\n char temp[2];\n long long int n = 0;\n long long int flag4 = 0;\n long long int flag2 = 0;\n scanf(\"%lld\", &n);\n long long int max = 0;\n for (long long int i = 0; i < n; i++) {\n long long int tmp = 0;\n scanf(\"%lld\", &tmp);\n flag4 -= frec[tmp] / 4;\n flag2 -= frec[tmp] / 2;\n frec[tmp]++;\n flag4 += frec[tmp] / 4;\n flag2 += frec[tmp] / 2;\n }\n long long int m = 0;\n scanf(\"%lld\", &m);\n\n for (long long int i = 0; i < m; i++) {\n scanf(\"%c\", &temp[0]);\n char act = 'A';\n long long int nr = 0;\n scanf(\"%c%lld\", &act, &nr);\n flag4 -= frec[nr] / 4;\n flag2 -= frec[nr] / 2;\n if (act == '+') {\n frec[nr]++;\n flag4 += frec[nr] / 4;\n flag2 += frec[nr] / 2;\n } else {\n frec[nr]--;\n flag4 += frec[nr] / 4;\n flag2 += frec[nr] / 2;\n }\n if (flag4 >= 1 && flag2 >= 4) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let stdout = std::io::stdout();\n let mut reader = std::io::BufReader::new(stdin.lock());\n let mut writer = std::io::BufWriter::new(stdout.lock());\n\n let n: usize = {\n let mut buf = String::new();\n reader.read_line(&mut buf).unwrap();\n buf.trim_end().parse().unwrap()\n };\n\n let a: Vec = {\n let mut buf = String::new();\n reader.read_line(&mut buf).unwrap();\n let iter = buf.split_whitespace();\n iter.map(|x| x.parse().unwrap()).collect()\n };\n\n let mut map: HashMap = HashMap::new();\n let (mut fours, mut twos) = (0, 0);\n for i in 0..n {\n let val = map.entry(a[i]).or_insert(0);\n *val += 1;\n if (*val).is_multiple_of(4) {\n fours += 1;\n twos -= 1;\n } else if (*val).is_multiple_of(2) {\n twos += 1;\n }\n }\n\n let q: usize = {\n let mut buf = String::new();\n reader.read_line(&mut buf).unwrap();\n buf.trim_end().parse().unwrap()\n };\n\n for _ in 0..q {\n let (c, x): (char, usize) = {\n let mut buf = String::new();\n reader.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 if c == '+' {\n let val = map.entry(x).or_insert(0);\n *val += 1;\n if (*val).is_multiple_of(4) {\n fours += 1;\n twos -= 1;\n } else if (*val).is_multiple_of(2) {\n twos += 1;\n }\n } else {\n let val = map.entry(x).or_insert(0);\n if (*val).is_multiple_of(4) {\n fours -= 1;\n twos += 1;\n } else if (*val).is_multiple_of(2) {\n twos -= 1;\n }\n *val -= 1;\n }\n\n if fours >= 2 || (fours >= 1 && twos >= 2) {\n writeln!(writer, \"YES\").unwrap();\n } else {\n writeln!(writer, \"NO\").unwrap();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0409", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

Mr. Infinity has a string S consisting of digits from 1 to 9. Each time the date changes, this string changes as follows:

\n
    \n
  • Each occurrence of 2 in S is replaced with 22. Similarly, each 3 becomes 333, 4 becomes 4444, 5 becomes 55555, 6 becomes 666666, 7 becomes 7777777, 8 becomes 88888888 and 9 becomes 999999999. 1 remains as 1.
  • \n
\n

For example, if S is 1324, it becomes 1333224444 the next day, and it becomes 133333333322224444444444444444 the day after next.\nYou are interested in what the string looks like after 5 \\times 10^{15} days. What is the K-th character from the left in the string after 5 \\times 10^{15} days?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • S is a string of length between 1 and 100 (inclusive).
  • \n
  • K is an integer between 1 and 10^{18} (inclusive).
  • \n
  • The length of the string after 5 \\times 10^{15} days is at least K.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
S\nK\n
\n
\n
\n
\n
\n

Output

\n

Print the K-th character from the left in Mr. Infinity's string after 5 \\times 10^{15} days.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1214\n4\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The string S changes as follows:

\n
    \n
  • Now: 1214
  • \n
  • After one day: 12214444
  • \n
  • After two days: 1222214444444444444444
  • \n
  • After three days: 12222222214444444444444444444444444444444444444444444444444444444444444444
  • \n
\n

The first five characters in the string after 5 \\times 10^{15} days is 12222. As K=4, we should print the fourth character, 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n157\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

The initial string is 3. The string after 5 \\times 10^{15} days consists only of 3.

\n
\n
\n
\n
\n
\n

Sample Input 3

299792458\n9460730472580800\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n
\n
", "c_code": "int solution() {\n char s[101];\n long n;\n scanf(\"%s %ld\", s, &n);\n int i = 0;\n while (s[i] == '1' && i < n - 1) {\n i++;\n }\n printf(\"%c\\n\", s[i]);\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 sb: Vec = s.trim().chars().collect();\n let mut ns = String::new();\n std::io::stdin().read_line(&mut ns).unwrap();\n let k: usize = ns.trim().parse().unwrap();\n\n let mut i: usize = 0;\n while i < k - 1 && sb[i] == '1' {\n i += 1;\n }\n println!(\"{}\", sb[i]);\n}", "difficulty": "easy"} {"problem_id": "0410", "problem_description": "There are three cells on an infinite 2-dimensional grid, labeled $$$A$$$, $$$B$$$, and $$$F$$$. Find the length of the shortest path from $$$A$$$ to $$$B$$$ if: in one move you can go to any of the four adjacent cells sharing a side; visiting the cell $$$F$$$ is forbidden (it is an obstacle).", "c_code": "int solution() {\n int t;\n int ansar[10005];\n scanf(\"%d\", &t);\n int start[2];\n int end[2];\n int obstacle[2];\n for (int y = 0; y < t; y++) {\n scanf(\"%d %d\", &start[0], &start[1]);\n scanf(\"%d %d\", &end[0], &end[1]);\n scanf(\"%d %d\", &obstacle[0], &obstacle[1]);\n int ansx = end[0] < start[0] ? start[0] - end[0] : end[0] - start[0];\n int ansy = end[1] < start[1] ? start[1] - end[1] : end[1] - start[1];\n int answer = ansx + ansy;\n if (start[0] == end[0] && start[0] == obstacle[0]) {\n if ((obstacle[1] > start[1] && obstacle[1] < end[1]) ||\n (obstacle[1] > end[1] && obstacle[1] < start[1])) {\n answer += 2;\n }\n }\n if (start[1] == end[1] && start[1] == obstacle[1]) {\n if ((obstacle[0] > start[0] && obstacle[0] < end[0]) ||\n (obstacle[0] > end[0] && obstacle[0] < start[0])) {\n answer += 2;\n }\n }\n ansar[y] = answer;\n }\n\n for (int g = 0; g < t; g++) {\n printf(\"%d\\n\", ansar[g]);\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 while let Some(_) = i.next() {\n if let [(ax, ay), (bx, by), (fx, fy)] = (0..3)\n .map(|_| {\n i.next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>()\n })\n .map(|v| (v[0], v[1]))\n .collect::>()[..]\n {\n let (ax, bx) = (ax.max(bx), ax.min(bx));\n let (ay, by) = (ay.max(by), ay.min(by));\n let r = if (ax == bx && bx == fx && (fy > by && fy < ay))\n || (ay == by && by == fy && (fx > bx && fx < ax))\n {\n 2\n } else {\n 0\n };\n writeln!(o, \"{}\", (ax - bx) + (ay - by) + r).ok();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0411", "problem_description": "Theofanis really likes sequences of positive integers, thus his teacher (Yeltsa Kcir) gave him a problem about a sequence that consists of only special numbers.Let's call a positive number special if it can be written as a sum of different non-negative powers of $$$n$$$. For example, for $$$n = 4$$$ number $$$17$$$ is special, because it can be written as $$$4^0 + 4^2 = 1 + 16 = 17$$$, but $$$9$$$ is not.Theofanis asks you to help him find the $$$k$$$-th special number if they are sorted in increasing order. Since this number may be too large, output it modulo $$$10^9+7$$$.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n long long sum = 0;\n long long p = 1;\n while (k > 0) {\n if (k % 2 == 1) {\n\n sum = (sum + p) % 1000000007;\n }\n k /= 2;\n p = p * n % 1000000007;\n }\n sum = sum % 1000000007;\n printf(\"%lld\\n\", sum);\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut s = String::new();\n io::stdin().read_to_string(&mut s)?;\n let mut it = s.split_whitespace().map(|s| s.to_string());\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut out = String::new();\n let m = i64::pow(10, 9) + 7;\n for _ in 1..=t {\n let n: i64 = it.next().unwrap().parse().unwrap();\n let mut k: i64 = it.next().unwrap().parse().unwrap();\n let mut bits = vec![];\n while k > 0 {\n let bit = k % 2;\n bits.push(bit);\n k /= 2;\n }\n let mut p = 1;\n let mut ans = 0;\n for bit in bits {\n if bit == 1 {\n ans += p;\n ans %= m;\n }\n p *= n;\n p %= m;\n }\n let ans = format!(\"{}\\n\", ans);\n out.push_str(ans.as_str());\n }\n print!(\"{}\", out);\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "0412", "problem_description": "A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $$$0$$$ are not allowed).Let's consider empty cells are denoted by '.', then the following figures are stars: The leftmost figure is a star of size $$$1$$$, the middle figure is a star of size $$$2$$$ and the rightmost figure is a star of size $$$3$$$. You are given a rectangular grid of size $$$n \\times m$$$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $$$1$$$ to $$$n$$$, columns are numbered from $$$1$$$ to $$$m$$$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $$$n \\cdot m$$$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $$$n \\cdot m$$$ stars.", "c_code": "int solution() {\n const int N = 110;\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n char str[N];\n int k;\n int a[N][N];\n int c[N][N];\n int dist[N][N];\n int t = 0;\n int i;\n int j;\n\n for (i = 1; i <= n; i++) {\n scanf(\"%s\", str);\n for (j = 0; j < m; j++) {\n if (str[j] == '*') {\n a[i][j + 1] = 1;\n }\n }\n }\n for (i = 1; i <= n; i++) {\n for (j = 1; j <= m; j++) {\n int d = 0;\n while (a[i - d][j] && a[i + d][j] && a[i][j - d] && a[i][j + d]) {\n ++d;\n }\n --d;\n if (d <= 0) {\n continue;\n }\n dist[i][j] = d, ++t;\n for (k = 0; k <= d; k++) {\n c[i - k][j] = c[i + k][j] = c[i][j - k] = c[i][j + k] = 1;\n }\n }\n }\n for (i = 1; i <= n; i++) {\n for (j = 1; j <= m; j++) {\n if (a[i][j] && !c[i][j]) {\n printf(\"-1\");\n return 0;\n }\n }\n }\n\n printf(\"%d\\n\", t);\n for (i = 1; i <= n; i++) {\n for (j = 1; j <= m; j++) {\n if (dist[i][j]) {\n printf(\"%d %d %d\\n\", i, j, dist[i][j]);\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let input_handle = io::stdin();\n let input_line = &mut String::new();\n input_handle.read_line(input_line).unwrap();\n let mut input_parts = input_line\n .split_whitespace()\n .map(|token| token.parse::().unwrap());\n let n = input_parts.next().unwrap();\n let m = input_parts.next().unwrap();\n\n let field = &mut vec![vec!['.'; m]; n];\n for i in 0..n {\n let input_line = &mut String::new();\n input_handle.read_line(input_line).unwrap();\n for j in 0..m {\n field[i][j] = input_line.as_bytes()[j] as char;\n }\n }\n\n let up = &mut vec![vec![0i32; m]; n];\n let down = &mut vec![vec![0i32; m]; n];\n let left = &mut vec![vec![0i32; m]; n];\n let right = &mut vec![vec![0i32; m]; n];\n\n for i in 0..n {\n for j in 0..m {\n if field[i][j] == '.' {\n } else {\n if i > 0 {\n up[i][j] = up[i - 1][j] + 1;\n } else {\n up[i][j] = 1;\n }\n if j > 0 {\n left[i][j] = left[i][j - 1] + 1;\n } else {\n left[i][j] = 1;\n }\n }\n }\n }\n\n for i in (0..n).rev() {\n for j in (0..m).rev() {\n if field[i][j] == '.' {\n } else {\n if i < n - 1 {\n down[i][j] = down[i + 1][j] + 1;\n } else {\n down[i][j] = 1;\n }\n if j < m - 1 {\n right[i][j] = right[i][j + 1] + 1;\n } else {\n right[i][j] = 1;\n }\n }\n }\n }\n\n let h = &mut vec![vec![0i32; m]; n];\n let v = &mut vec![vec![0i32; m]; n];\n\n for i in 0..n {\n for j in 0..m {\n let len = min(min(up[i][j], down[i][j]), min(left[i][j], right[i][j])) - 1;\n if len <= 0 {\n continue;\n }\n let len = len as usize;\n h[i][j - len] += 1;\n if j + len + 1 < m {\n h[i][j + len + 1] -= 1;\n }\n v[i - len][j] += 1;\n if i + len + 1 < n {\n v[i + len + 1][j] -= 1;\n }\n }\n }\n\n for i in 0..n {\n for j in 0..m {\n if i > 0 {\n v[i][j] += v[i - 1][j];\n }\n if j > 0 {\n h[i][j] += h[i][j - 1];\n }\n }\n }\n\n let mut size = 0;\n for i in 0..n {\n for j in 0..m {\n let len = min(up[i][j], min(down[i][j], min(left[i][j], right[i][j]))) - 1;\n if len > 0 {\n size += 1;\n }\n let is_asterisk = h[i][j] > 0 || v[i][j] > 0;\n let is_asterisk2 = field[i][j] == '*';\n if is_asterisk != is_asterisk2 {\n println!(\"-1\");\n return;\n }\n }\n }\n\n println!(\"{}\", size);\n let mut s = String::new();\n for i in 0..n {\n for j in 0..m {\n let len = min(up[i][j], min(down[i][j], min(left[i][j], right[i][j]))) - 1;\n if len <= 0 {\n continue;\n }\n s += &fmt::format(format_args!(\"{} {} {}\\n\", i + 1, j + 1, len));\n }\n }\n print!(\"{}\", s);\n}", "difficulty": "easy"} {"problem_id": "0413", "problem_description": "You are given a string $$$s$$$ of length $$$n$$$ consisting only of the characters 0 and 1.You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase the substring 111 from the string 111110, you will get the string 110. When you delete a substring of length $$$l$$$, you get $$$a \\cdot l + b$$$ points.Your task is to calculate the maximum number of points that you can score in total, if you have to make the given string empty.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n;\n int a;\n int b;\n scanf(\"%d %d %d\", &n, &a, &b);\n\n char *s = malloc(sizeof(char) * (n + 1));\n scanf(\"%s\", s);\n s[n] = '\\0';\n\n int cost = a * n;\n if (b >= 0) {\n cost += b * n;\n } else {\n int num_o_groups = 0;\n int num_z_groups = 0;\n\n char last = 'z';\n for (int i = 0; i < n; i++) {\n if (s[i] == last) {\n continue;\n }\n last = s[i];\n if (s[i] == '0') {\n num_z_groups += 1;\n } else {\n num_o_groups += 1;\n }\n }\n\n if (num_o_groups < num_z_groups) {\n cost += (num_o_groups + 1) * b;\n } else {\n cost += (num_z_groups + 1) * b;\n }\n }\n\n printf(\"%d\\n\", cost);\n\n free(s);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = {\n let mut lines = std::io::stdin().lines();\n move || lines.next().unwrap().unwrap()\n };\n for _ in 0..line().parse().unwrap() {\n let (n, a, b) = {\n let line = line();\n let mut line = line.split_whitespace().map(|x| x.parse::().unwrap());\n (\n line.next().unwrap(),\n line.next().unwrap(),\n line.next().unwrap(),\n )\n };\n let bits: Vec<_> = line().chars().collect();\n println!(\n \"{}\",\n if b > 0 {\n n * (a + b)\n } else {\n let mut chunk = 0;\n for window in bits.windows(2) {\n if window[0] != window[1] {\n chunk += 1;\n }\n }\n chunk = (chunk + 3) / 2;\n n * a + b * chunk\n }\n );\n }\n}", "difficulty": "medium"} {"problem_id": "0414", "problem_description": "A penguin Rocher has $$$n$$$ sticks. He has exactly one stick with length $$$i$$$ for all $$$1 \\le i \\le n$$$.He can connect some sticks. If he connects two sticks that have lengths $$$a$$$ and $$$b$$$, he gets one stick with length $$$a + b$$$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections.He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create?", "c_code": "int solution() {\n int tests = 0;\n\n scanf(\"%d\", &tests);\n while (tests--) {\n int n = 0;\n scanf(\"%d\", &n);\n\n printf(\"%d\\n\", (n / 2) + (n % 2));\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 n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n println!(\"{}\", (n - 1) / 2 + 1);\n }\n}", "difficulty": "medium"} {"problem_id": "0415", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

\n

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 0 \\leq A, B, C
  • \n
  • 1 \\leq K \\leq A + B + C \\leq 2 \\times 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C K\n
\n
\n
\n
\n
\n

Output

Print the maximum possible sum of the numbers written on the cards chosen.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 1 1 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Consider picking up two cards with 1s and one card with a 0.\nIn this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2000000000 0 0 2000000000\n
\n
\n
\n
\n
\n

Sample Output 3

2000000000\n
\n
\n
", "c_code": "int solution() {\n long long int A = 0;\n long long int B = 0;\n long long int C = 0;\n long long int K = 0;\n\n scanf(\"%lld %lld %lld %lld\", &A, &B, &C, &K);\n\n if (A <= K && K <= A + B) {\n printf(\"%lld\", A);\n } else if (K < A) {\n printf(\"%lld\", K);\n } else {\n printf(\"%lld\", A - (K - A - B));\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 let nums: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let (mut a, b, _, mut k) = (nums[0], nums[1], nums[2], nums[3]);\n\n if k >= a {\n k -= a;\n } else {\n a = k;\n k = 0;\n }\n\n if k >= b {\n k -= b;\n } else {\n k = 0;\n }\n\n let c = -k;\n\n println!(\"{}\", a + c);\n}", "difficulty": "medium"} {"problem_id": "0416", "problem_description": "A Pythagorean triple is a triple of integer numbers $$$(a, b, c)$$$ such that it is possible to form a right triangle with the lengths of the first cathetus, the second cathetus and the hypotenuse equal to $$$a$$$, $$$b$$$ and $$$c$$$, respectively. An example of the Pythagorean triple is $$$(3, 4, 5)$$$.Vasya studies the properties of right triangles, and he uses a formula that determines if some triple of integers is Pythagorean. Unfortunately, he has forgotten the exact formula; he remembers only that the formula was some equation with squares. So, he came up with the following formula: $$$c = a^2 - b$$$.Obviously, this is not the right formula to check if a triple of numbers is Pythagorean. But, to Vasya's surprise, it actually worked on the triple $$$(3, 4, 5)$$$: $$$5 = 3^2 - 4$$$, so, according to Vasya's formula, it is a Pythagorean triple.When Vasya found the right formula (and understood that his formula is wrong), he wondered: how many are there triples of integers $$$(a, b, c)$$$ with $$$1 \\le a \\le b \\le c \\le n$$$ such that they are Pythagorean both according to his formula and the real definition? He asked you to count these triples.", "c_code": "int solution(void) {\n\n int T;\n int N;\n\n scanf(\"%d\", &T);\n while (T--) {\n scanf(\"%d\", &N);\n\n int i = 1;\n while (2 * i * i + 2 * i + 1 <= N) {\n i++;\n }\n printf(\"%d\\n\", i - 1);\n }\n}", "rust_code": "fn solution() {\n use std::io::{self, Write};\n\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 n: usize = sc.next();\n let queries: Vec = (0..n).map(|_| sc.next()).collect();\n let mut set: BTreeSet = BTreeSet::new();\n let mut a: i64 = 2;\n while (a * a - 1) / 2 < 1e9 as i64 {\n let b = (a * a - 1) / 2;\n let c = b + 1;\n if a <= b && b <= c && ((a * a + b * b) == (c * c)) && (c == (a * a - b)) {\n set.insert(c);\n }\n a += 1;\n }\n let ns: Vec = set.into_iter().collect();\n for query in queries {\n let lower = ns\n .binary_search_by(|x| {\n if *x <= query {\n Ordering::Less\n } else {\n Ordering::Greater\n }\n })\n .err()\n .unwrap();\n writeln!(out, \"{}\", lower).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0417", "problem_description": "Madoka as a child was an extremely capricious girl, and one of her favorite pranks was drawing on her wall. According to Madoka's memories, the wall was a table of $$$n$$$ rows and $$$m$$$ columns, consisting only of zeroes and ones. The coordinate of the cell in the $$$i$$$-th row and the $$$j$$$-th column ($$$1 \\le i \\le n$$$, $$$1 \\le j \\le m$$$) is $$$(i, j)$$$.One day she saw a picture \"Mahou Shoujo Madoka Magica\" and decided to draw it on her wall. Initially, the Madoka's table is a table of size $$$n \\times m$$$ filled with zeroes. Then she applies the following operation any number of times:Madoka selects any rectangular subtable of the table and paints it in a chess coloring (the upper left corner of the subtable always has the color $$$0$$$). Note that some cells may be colored several times. In this case, the final color of the cell is equal to the color obtained during the last repainting. White color means $$$0$$$, black means $$$1$$$. So, for example, the table in the first picture is painted in a chess coloring, and the others are not. For better understanding of the statement, we recommend you to read the explanation of the first test.Help Madoka and find some sequence of no more than $$$n \\cdot m$$$ operations that allows you to obtain the picture she wants, or determine that this is impossible.", "c_code": "int solution(void) {\n static char M[101][101];\n int t;\n int n;\n int m;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d%d\", &n, &m);\n for (int i = 1; i <= n; i++) {\n scanf(\"%s\", M[i] + 1);\n }\n if (M[1][1] == '1') {\n puts(\"-1\");\n continue;\n }\n printf(\"%d\\n\", n * m);\n for (int i = n; i >= 1; i--) {\n for (int j = m; j > 1; j--) {\n\n if (M[i][j] - '0') {\n printf(\"%d %d %d %d\\n\", i, j - 1, i, j);\n } else {\n puts(\"1 1 1 1\");\n }\n }\n if (M[i][1] - '0') {\n printf(\"%d 1 %d 1\\n\", i - 1, i);\n } else {\n puts(\"1 1 1 1\");\n }\n }\n }\n}", "rust_code": "fn solution() {\n let std_in = stdin();\n let mut input = Scanner::new(std_in.lock());\n\n let std_out = stdout();\n let mut output = BufWriter::new(std_out.lock());\n\n let tests = input.token();\n for _ in 0..tests {\n let (n, m): (usize, usize) = (input.token(), input.token());\n let a: Vec> = (0..n)\n .map(|_| input.token::().chars().map(|c| c == '1').collect())\n .collect();\n if a[0][0] {\n writeln!(&mut output, \"-1\").unwrap();\n continue;\n }\n\n let mut result = Vec::new();\n for r in (0..n).rev() {\n for c in (0..m).rev() {\n if a[r][c] {\n if c == 0 {\n result.push((r - 1, c, r, c));\n } else {\n result.push((r, c - 1, r, c));\n }\n }\n }\n }\n\n writeln!(&mut output, \"{}\", result.len()).unwrap();\n for (a, b, c, d) in result {\n writeln!(&mut output, \"{} {} {} {}\", a + 1, b + 1, c + 1, d + 1).unwrap();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0418", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given an array A of length N.\nYour task is to divide it into several contiguous subarrays.\nHere, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.\nAt least how many subarrays do you need to divide A into?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
  • Each A_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible number of subarrays after division of A.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n1 2 3 2 2 1\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

\n
\n
\n
\n
\n
\n

Sample Input 2

9\n1 2 1 2 1 2 1 2 1\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
\n
\n
\n
\n
\n

Sample Input 3

7\n1 2 3 2 1 999999999 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution() {\n int n;\n int ans = 0;\n int a[100001];\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n for (int i = 0; i < n; i++) {\n while (i < n - 1 && a[i] == a[i + 1]) {\n i++;\n }\n if (i < n - 1 && a[i] < a[i + 1]) {\n while (i < n - 1 && a[i] <= a[i + 1]) {\n i++;\n }\n } else if (i < n - 1 && a[i] > a[i + 1]) {\n while (i < n - 1 && a[i] >= a[i + 1]) {\n i++;\n }\n }\n ans++;\n }\n printf(\"%d\\n\", ans);\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 A: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let (ans, _, _) = A[1..].iter().fold((1, A[0], 0), |(res, prev, sign), &a| {\n let d = a - prev;\n if d == 0 {\n (res, a, sign)\n } else if d > 0 && sign >= 0 || d <= 0 && sign <= 0 {\n (res, a, d)\n } else {\n (res + 1, a, 0)\n }\n });\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0419", "problem_description": "Initially, you have the array $$$a$$$ consisting of one element $$$1$$$ ($$$a = [1]$$$).In one move, you can do one of the following things: Increase some (single) element of $$$a$$$ by $$$1$$$ (choose some $$$i$$$ from $$$1$$$ to the current length of $$$a$$$ and increase $$$a_i$$$ by one); Append the copy of some (single) element of $$$a$$$ to the end of the array (choose some $$$i$$$ from $$$1$$$ to the current length of $$$a$$$ and append $$$a_i$$$ to the end of the array). For example, consider the sequence of five moves: You take the first element $$$a_1$$$, append its copy to the end of the array and get $$$a = [1, 1]$$$. You take the first element $$$a_1$$$, increase it by $$$1$$$ and get $$$a = [2, 1]$$$. You take the second element $$$a_2$$$, append its copy to the end of the array and get $$$a = [2, 1, 1]$$$. You take the first element $$$a_1$$$, append its copy to the end of the array and get $$$a = [2, 1, 1, 2]$$$. You take the fourth element $$$a_4$$$, increase it by $$$1$$$ and get $$$a = [2, 1, 1, 3]$$$. Your task is to find the minimum number of moves required to obtain the array with the sum at least $$$n$$$.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 scanf(\"%d\", &n);\n int root = sqrt(n);\n if (root * root >= n) {\n printf(\"%d\\n\", (2 * root) - 2);\n } else if ((root + 1) * root >= n) {\n printf(\"%d\\n\", (2 * root) - 1);\n } else {\n printf(\"%d\\n\", 2 * root);\n }\n }\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).unwrap();\n let t = t.trim().parse::().unwrap();\n\n for _i in 0..t {\n let mut n = String::new();\n io::stdin().read_line(&mut n).unwrap();\n let n = n.trim().parse::().unwrap();\n\n let mut ans = 1000000000;\n let mut x = 1;\n\n while x * x <= n {\n let some = x - 1 + ((n - x) + x - 1) / x;\n\n ans = match ans.cmp(&some) {\n Ordering::Less => ans,\n _ => some,\n };\n\n x += 1;\n }\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "hard"} {"problem_id": "0420", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

AtCoDeer the deer is seeing a quick report of election results on TV.\nTwo candidates are standing for the election: Takahashi and Aoki.\nThe report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes.\nAtCoDeer has checked the report N times, and when he checked it for the i-th (1≦i≦N) time, the ratio was T_i:A_i.\nIt is known that each candidate had at least one vote when he checked the report for the first time.

\n

Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the N-th time.\nIt can be assumed that the number of votes obtained by each candidate never decreases.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦N≦1000
  • \n
  • 1≦T_i,A_i≦1000 (1≦i≦N)
  • \n
  • T_i and A_i (1≦i≦N) are coprime.
  • \n
  • It is guaranteed that the correct answer is at most 10^{18}.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nT_1 A_1\nT_2 A_2\n:\nT_N A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2 3\n1 1\n3 2\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

When the numbers of votes obtained by the two candidates change as 2,3 → 3,3 → 6,4, the total number of votes at the end is 10, which is the minimum possible number.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n1 1\n1 1\n1 5\n1 100\n
\n
\n
\n
\n
\n

Sample Output 2

101\n
\n

It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n3 10\n48 17\n31 199\n231 23\n3 2\n
\n
\n
\n
\n
\n

Sample Output 3

6930\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n long long int t[n];\n long long int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld%lld\", &t[i], &a[i]);\n }\n long long int tz = t[0];\n long long int az = a[0];\n for (int i = 1; i < n; i++) {\n long long int r = tz / t[i];\n long long int s = az / a[i];\n long long int p = 0;\n long long int q = 0;\n if (r > s) {\n p = r * t[i];\n q = r * a[i];\n } else {\n p = s * t[i];\n q = s * a[i];\n }\n while (p < tz || q < az) {\n p += t[i];\n q += a[i];\n }\n tz = p;\n az = q;\n }\n printf(\"%lld\\n\", tz + az);\n\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: usize = s.trim().parse().unwrap();\n\n let mut x: usize = 1;\n let mut a: usize = 0;\n let mut b: usize = 0;\n for _ in 0..n {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let v: Vec = s.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n let y = v[0] + v[1];\n let mut i: usize = std::cmp::max(x / y, std::cmp::max(a / v[0], b / v[1]));\n while y * i < x || v[0] * i < a || v[1] * i < b {\n i += 1;\n }\n x = y * i;\n a = v[0] * i;\n b = v[1] * i;\n }\n println!(\"{}\", x);\n}", "difficulty": "hard"} {"problem_id": "0421", "problem_description": "Homer has two friends Alice and Bob. Both of them are string fans. One day, Alice and Bob decide to play a game on a string $$$s = s_1 s_2 \\dots s_n$$$ of length $$$n$$$ consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move.In a move, a player must choose an index $$$i$$$ ($$$1 \\leq i \\leq n$$$) that has not been chosen before, and change $$$s_i$$$ to any other lowercase English letter $$$c$$$ that $$$c \\neq s_i$$$.When all indices have been chosen, the game ends. The goal of Alice is to make the final string lexicographically as small as possible, while the goal of Bob is to make the final string lexicographically as large as possible. Both of them are game experts, so they always play games optimally. Homer is not a game expert, so he wonders what the final string will be.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.", "c_code": "int solution(void) {\n int tc = 1;\n scanf(\"%d\", &tc);\n while (tc--) {\n char str[50];\n scanf(\"%s\", str);\n for (int i = 0; str[i]; i++) {\n if (i % 2) {\n if (str[i] == 'z') {\n str[i] = 'y';\n } else {\n str[i] = 'z';\n }\n } else {\n if (str[i] == 'a') {\n str[i] = 'b';\n } else {\n str[i] = 'a';\n }\n }\n }\n printf(\"%s\\n\", str);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let k = std::io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n for _ele in 0..k {\n let mut kstr = String::new();\n std::io::stdin().read_line(&mut kstr).unwrap();\n let kstr = kstr.trim();\n for (index, st_ele) in kstr.as_bytes().iter().enumerate() {\n if index % 2 == 0 {\n if st_ele == &b'a' {\n print!(\"b\")\n } else {\n print!(\"a\")\n }\n } else {\n if st_ele == &b'z' {\n print!(\"y\")\n } else {\n print!(\"z\")\n }\n }\n }\n println!();\n }\n}", "difficulty": "easy"} {"problem_id": "0422", "problem_description": "There is a sheet of paper that can be represented with a grid of size $$$n \\times m$$$: $$$n$$$ rows and $$$m$$$ columns of cells. All cells are colored in white initially.$$$q$$$ operations have been applied to the sheet. The $$$i$$$-th of them can be described as follows: $$$x_i$$$ $$$y_i$$$ — choose one of $$$k$$$ non-white colors and color the entire row $$$x_i$$$ and the entire column $$$y_i$$$ in it. The new color is applied to each cell, regardless of whether the cell was colored before the operation. The sheet after applying all $$$q$$$ operations is called a coloring. Two colorings are different if there exists at least one cell that is colored in different colors.How many different colorings are there? Print the number modulo $$$998\\,244\\,353$$$.", "c_code": "int solution() {\n int t = 0;\n int nm[2] = {};\n long long k = 0LL;\n int q = 0;\n int xy[200000][2] = {};\n\n int res = 0;\n\n long long mod_num = 998244353;\n\n int colored[2][200000] = {};\n\n res = scanf(\"%d\", &t);\n while (t > 0) {\n long long ans = 1LL;\n int colored_cnt[2] = {};\n res = scanf(\"%d\", nm);\n res = scanf(\"%d\", nm + 1);\n res = scanf(\"%lld\", &k);\n res = scanf(\"%d\", &q);\n for (int i = 0; i < q; i++) {\n res = scanf(\"%d\", xy[i]);\n res = scanf(\"%d\", xy[i] + 1);\n xy[i][0]--;\n xy[i][1]--;\n }\n\n for (int i = 0; i < 2; i++) {\n for (int j = 0; j < nm[i]; j++) {\n colored[i][j] = 0;\n }\n }\n\n for (int i = q - 1; i >= 0; i--) {\n int is_colored = 0;\n for (int j = 0; j < 2; j++) {\n if (colored[j][xy[i][j]] <= 0 && colored_cnt[1 - j] < nm[1 - j]) {\n is_colored = 1;\n colored_cnt[j]++;\n }\n colored[j][xy[i][j]] = 1;\n }\n if (is_colored > 0) {\n ans = (ans * k) % mod_num;\n }\n }\n\n printf(\"%lld\\n\", ans);\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut stdin = stdin.lock();\n\n let mut buf = String::new();\n stdin.read_to_string(&mut buf).unwrap();\n\n let input = &mut buf\n .split_ascii_whitespace()\n .map(|s| s.parse::().unwrap());\n let mut input = || input.next().unwrap();\n\n let nn = input() as usize;\n\n let mut data = Vec::new();\n let mut used = Vec::new();\n\n const Q: u64 = 998244353;\n\n let mut ans = Vec::new();\n for _ii in 0..nn {\n let (n, m, k, q) = (\n input() as usize,\n input() as usize,\n input() as u64,\n input() as usize,\n );\n\n data.clear();\n data.extend((0..q).map(|_| (input(), input())));\n\n used.clear();\n used.resize(n + m, false);\n\n let mut z = 0;\n let (mut nr, mut nc) = (0, 0);\n for &(i, j) in data.iter().rev() {\n let dr = 1 - replace(&mut used[i as usize - 1], true) as usize;\n let dc = 1 - replace(&mut used[j as usize + n - 1], true) as usize;\n z += dr | dc;\n\n nr += dr;\n nc += dc;\n if nr >= n || nc >= m {\n break;\n }\n }\n\n let mut acc = 1;\n let mut step = k;\n loop {\n if z & 1 != 0 {\n acc = acc * step % Q;\n }\n z >>= 1;\n if z == 0 {\n break;\n }\n step = step * step % Q;\n }\n\n writeln!(ans, \"{acc}\").unwrap();\n }\n\n stdout().write(&ans).unwrap();\n}", "difficulty": "medium"} {"problem_id": "0423", "problem_description": "Phoenix is playing with a new puzzle, which consists of $$$n$$$ identical puzzle pieces. Each puzzle piece is a right isosceles triangle as shown below. A puzzle piece The goal of the puzzle is to create a square using the $$$n$$$ pieces. He is allowed to rotate and move the pieces around, but none of them can overlap and all $$$n$$$ pieces must be used (of course, the square shouldn't contain any holes as well). Can he do it?", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int c;\n for (int i = 0; i < n; i++) {\n c = 0;\n for (int j = 1; j < 100000; j++) {\n if ((a[i] == 2 * j * j) || (a[i] == 4 * j * j)) {\n c += 1;\n break;\n }\n if ((a[i] < 2 * j * j) && (a[i] < 4 * j * j)) {\n break;\n } else if ((a[i] > 2 * j * j) || (a[i] > 4 * j * j)) {\n c = c;\n }\n }\n if (c == 0) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).unwrap();\n let t: usize = t.trim().parse().unwrap();\n\n let mut n = String::new();\n 'test: for _t in 0..t {\n n.clear();\n io::stdin().read_line(&mut n).unwrap();\n let mut n: u64 = n.trim().parse().unwrap();\n for _i in 0..2 {\n if n % 2 == 1 {\n break;\n }\n\n n /= 2;\n let n_sqrt: f64 = (n as f64).sqrt() + 0.1;\n let n_sqrt_trunc: u64 = n_sqrt as u64;\n if n_sqrt_trunc * n_sqrt_trunc == n {\n println!(\"YES\");\n continue 'test;\n }\n }\n println!(\"NO\");\n }\n}", "difficulty": "hard"} {"problem_id": "0424", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Snuke has an empty sequence a.

\n

He will perform N operations on this sequence.

\n

In the i-th operation, he chooses an integer j satisfying 1 \\leq j \\leq i, and insert j at position j in a (the beginning is position 1).

\n

You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq b_i \\leq N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nb_1 \\dots b_N\n
\n
\n
\n
\n
\n

Output

If there is no sequence of N operations after which a would be equal to b, print -1.\nIf there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 2 1\n
\n
\n
\n
\n
\n

Sample Output 1

1\n1\n2\n
\n

In this sequence of operations, the sequence a changes as follows:

\n
    \n
  • After the first operation: (1)
  • \n
  • After the second operation: (1,1)
  • \n
  • After the third operation: (1,2,1)
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

2\n2 2\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

2 cannot be inserted at the beginning of the sequence, so this is impossible.

\n
\n
\n
\n
\n
\n

Sample Input 3

9\n1 1 1 2 2 1 2 3 2\n
\n
\n
\n
\n
\n

Sample Output 3

1\n2\n2\n3\n1\n2\n2\n1\n1\n
\n
\n
", "c_code": "int solution() {\n int n;\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 a[i]--;\n if (a[i] > i) {\n printf(\"-1\\n\");\n return 0;\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = i; j > a[i]; j--) {\n b[j] = b[j - 1];\n }\n b[a[i]] = a[i];\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d\\n\", b[i] + 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\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 n: usize = iter.next().unwrap().parse::().unwrap();\n let mut b: Vec = (&mut iter)\n .take(n)\n .map(|x| x.parse::().unwrap() - 1)\n .collect();\n\n let mut ret = vec![];\n let mut no_ans = false;\n while !b.is_empty() {\n let mut t = None;\n for i in 0..b.len() {\n let j = b.len() - 1 - i;\n if b[j] == j {\n t = Some(j);\n break;\n }\n }\n if let Some(v) = t {\n ret.push(v);\n b.remove(v);\n } else {\n no_ans = true;\n break;\n }\n }\n if no_ans {\n println!(\"-1\");\n } else {\n ret = ret.iter().map(|x| *x + 1).collect();\n ret.reverse();\n for r in ret {\n println!(\"{}\", r);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0425", "problem_description": "You are given two positive integers $$$n$$$ and $$$s$$$. Find the maximum possible median of an array of $$$n$$$ non-negative integers (not necessarily distinct), such that the sum of its elements is equal to $$$s$$$.A median of an array of integers of length $$$m$$$ is the number standing on the $$$\\lceil {\\frac{m}{2}} \\rceil$$$-th (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting from $$$1$$$. For example, a median of the array $$$[20,40,20,50,50,30]$$$ is the $$$\\lceil \\frac{m}{2} \\rceil$$$-th element of $$$[20,20,30,40,50,50]$$$, so it is $$$30$$$. There exist other definitions of the median, but in this problem we use the described definition.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n\n int n[t];\n int s[t];\n int mid;\n for (int i = 0; i < t; i++) {\n scanf(\"%d%d\", &n[i], &s[i]);\n }\n for (int i = 0; i < t; i++) {\n\n mid = (s[i] / (n[i] / 2 + 1));\n\n printf(\"%d\\n\", mid);\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut string = String::new();\n stdin.read_line(&mut string).unwrap();\n for _ in 0..string.trim().parse::().unwrap() {\n string.clear();\n stdin.read_line(&mut string).unwrap();\n let (num, sum) = {\n let mut s = string.trim().split_ascii_whitespace();\n (\n s.next().unwrap().parse::().unwrap(),\n s.next().unwrap().parse::().unwrap(),\n )\n };\n let average_num = {\n if num.checked_div(2).is_none() {\n num / 2\n } else {\n num / 2 + 1\n }\n };\n println!(\"{}\", sum / average_num);\n }\n}", "difficulty": "hard"} {"problem_id": "0426", "problem_description": "There are two sisters Alice and Betty. You have $$$n$$$ candies. You want to distribute these $$$n$$$ candies between two sisters in such a way that: Alice will get $$$a$$$ ($$$a > 0$$$) candies; Betty will get $$$b$$$ ($$$b > 0$$$) candies; each sister will get some integer number of candies; Alice will get a greater amount of candies than Betty (i.e. $$$a > b$$$); all the candies will be given to one of two sisters (i.e. $$$a+b=n$$$). Your task is to calculate the number of ways to distribute exactly $$$n$$$ candies between sisters in a way described above. Candies are indistinguishable.Formally, find the number of ways to represent $$$n$$$ as the sum of $$$n=a+b$$$, where $$$a$$$ and $$$b$$$ are positive integers and $$$a>b$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n int temp = arr[i];\n arr[i] = temp - (temp / 2) - 1;\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d\\n\", arr[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut cases = String::new();\n\n io::stdin().read_line(&mut cases).expect(\"no input\");\n\n let cases: u32 = cases.trim().parse().expect(\"nan\");\n\n for _ in 0..cases {\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"bad line\");\n\n let first: u32 = line.trim().parse().expect(\"nan\");\n println!(\"{}\", (first - 1) / 2);\n }\n}", "difficulty": "medium"} {"problem_id": "0427", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a positive integer X.\nFind the largest perfect power that is at most X.\nHere, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 X 1000
  • \n
  • X is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

Print the largest perfect power that is at most X.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

There are four perfect powers that are at most 10: 1, 4, 8 and 9.\nWe should print the largest among them, 9.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

999\n
\n
\n
\n
\n
\n

Sample Output 3

961\n
\n
\n
", "c_code": "int solution() {\n double a = 1;\n double b = 2;\n int X = 0;\n int MAX = 0;\n\n scanf(\"%d\", &X);\n\n if (X != 1) {\n for (a = 2; 2 * a < X; a++) {\n for (b = 2; pow(a, b) <= X; b++) {\n if (pow(a, b) > MAX) {\n MAX = (int)pow(a, b);\n }\n }\n }\n } else {\n MAX = X;\n }\n\n printf(\"%d\", MAX);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let vec: Vec<&str> = buf.split_whitespace().collect();\n let a: i32 = vec[0].parse().unwrap();\n\n let x: [i32; 41] = [\n 1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 81, 100, 121, 125, 128, 144, 169, 196, 216, 225,\n 243, 256, 289, 324, 343, 361, 400, 441, 484, 512, 529, 576, 625, 676, 729, 784, 841, 900,\n 961, 1000,\n ];\n let mut cnt: usize = 1;\n let mut done: bool = false;\n\n while !done {\n if x[41 - cnt] <= a {\n println!(\"{:?}\", x[41 - cnt]);\n done = true;\n }\n cnt += 1;\n }\n}", "difficulty": "hard"} {"problem_id": "0428", "problem_description": "Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you?Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark \"?\". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s=\"ab?b\" as a result, it will appear in t=\"aabrbb\" as a substring.Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol \"?\" should be considered equal to any other symbol.", "c_code": "int solution(void) {\n int n = 0;\n int m = 0;\n int i = 0;\n int j = 0;\n int match = 0;\n int max = 0;\n int pos = 0;\n char s[1001] = {\n 0,\n };\n char t[1001] = {\n 0,\n };\n\n scanf(\"%d %d\", &n, &m);\n scanf(\"%s\", s);\n scanf(\"%s\", t);\n\n for (i = 0; i <= m - n; i++) {\n match = 0;\n for (j = 0; j < n; j++) {\n if (t[i + j] == s[j]) {\n match++;\n }\n }\n\n if (match > max) {\n max = match;\n pos = i;\n }\n }\n\n printf(\"%d\\n\", n - max);\n\n for (i = 0; i < n; i++) {\n if (s[i] != t[i + pos]) {\n printf(\"%d \", i + 1);\n }\n }\n printf(\"\\n\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input).unwrap();\n\n let mut iter = input.split_whitespace();\n\n let n: usize = iter.next().unwrap().parse().unwrap();\n let m: usize = iter.next().unwrap().parse().unwrap();\n\n let mut ans: Option = None;\n\n let mut s = String::new();\n let mut t = String::new();\n\n io::stdin().read_line(&mut s).unwrap();\n io::stdin().read_line(&mut t).unwrap();\n\n let s = s.trim();\n let t = t.trim();\n\n let mut ansInd: Vec = Vec::new();\n\n let mut iter = t.chars().take(m - n + 1);\n let mut iter2 = t.chars();\n loop {\n let mut tmp: usize = 0;\n let other_iter = iter2.clone();\n match iter.next() {\n Some(_c) => {\n let other_iter = other_iter.zip(s.char_indices());\n let mut indices: Vec = Vec::new();\n for i in other_iter {\n let (x, y) = i;\n let (indice, y) = y;\n if x != y {\n tmp += 1;\n indices.push(indice + 1);\n }\n }\n match ans {\n Some(i) if tmp < i => {\n ans = Some(tmp);\n ansInd = indices;\n }\n None => {\n ans = Some(tmp);\n ansInd = indices;\n }\n _ => (),\n }\n }\n None => break,\n }\n iter2.next();\n }\n\n println!(\"{}\", ans.unwrap());\n if !ansInd.is_empty() {\n let mut ans_str = ansInd[0].to_string();\n let mut iter = ansInd.iter();\n iter.next();\n for i in iter {\n ans_str.push(' ');\n ans_str.push_str(&i.to_string()[..]);\n }\n println!(\"{}\", ans_str);\n }\n}", "difficulty": "medium"} {"problem_id": "0429", "problem_description": "Casimir has a string $$$s$$$ which consists of capital Latin letters 'A', 'B', and 'C' only. Each turn he can choose to do one of the two following actions: he can either erase exactly one letter 'A' and exactly one letter 'B' from arbitrary places of the string (these letters don't have to be adjacent); or he can erase exactly one letter 'B' and exactly one letter 'C' from arbitrary places in the string (these letters don't have to be adjacent). Therefore, each turn the length of the string is decreased exactly by $$$2$$$. All turns are independent so for each turn, Casimir can choose any of two possible actions.For example, with $$$s$$$ $$$=$$$ \"ABCABC\" he can obtain a string $$$s$$$ $$$=$$$ \"ACBC\" in one turn (by erasing the first occurrence of 'B' and the second occurrence of 'A'). There are also many other options for a turn aside from this particular example.For a given string $$$s$$$ determine whether there is a sequence of actions leading to an empty string. In other words, Casimir's goal is to erase all letters from the string. Is there a way to do this?", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n while (n--) {\n char c[60];\n scanf(\"%s\", c);\n int sum1 = 0;\n int sum2 = 0;\n\n for (int i = 0; i < 60; i++) {\n if (c[i] == 'A' || c[i] == 'C') {\n sum1 += 1;\n }\n if (c[i] == 'B') {\n sum2 += 1;\n }\n if (c[i] == '\\0') {\n break;\n }\n }\n if (sum1 == sum2) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut out = BufWriter::new(io::stdout().lock());\n io::stdin().lines().skip(1).flatten().for_each(|s| {\n match s.chars().fold((0usize, 0usize), |(b, len), c| match c {\n 'B' => (b.wrapping_add(2), len.wrapping_add(1)),\n _ => (b, len.wrapping_add(1)),\n }) {\n (a, b) if a == b => writeln!(out, \"YES\"),\n _ => writeln!(out, \"NO\"),\n }\n .ok();\n });\n}", "difficulty": "medium"} {"problem_id": "0430", "problem_description": "Armstrong number", "c_code": "int solution(int x) {\n int n = 0;\n int temp = x;\n int sum = 0;\n\n while (temp) {\n n++;\n temp /= 10;\n }\n\n temp = x;\n\n while (temp) {\n int r = temp % 10;\n\n int p = 1;\n for (int i = 0; i < n; i++) {\n p *= r;\n }\n\n sum += p;\n temp /= 10;\n }\n\n return (sum == x);\n}\n", "rust_code": "fn solution(number: u32) -> bool {\n let mut digits: Vec = Vec::new();\n let mut num: u32 = number;\n\n loop {\n digits.push(num % 10);\n num /= 10;\n if num == 0 {\n break;\n }\n }\n\n let sum_nth_power_of_digits: u32 = digits\n .iter()\n .map(|digit| digit.pow(digits.len() as u32))\n .sum();\n sum_nth_power_of_digits == number\n}", "difficulty": "medium"} {"problem_id": "0431", "problem_description": "Luckily, Serval got onto the right bus, and he came to the kindergarten on time. After coming to kindergarten, he found the toy bricks very funny.He has a special interest to create difficult problems for others to solve. This time, with many $$$1 \\times 1 \\times 1$$$ toy bricks, he builds up a 3-dimensional object. We can describe this object with a $$$n \\times m$$$ matrix, such that in each cell $$$(i,j)$$$, there are $$$h_{i,j}$$$ bricks standing on the top of each other.However, Serval doesn't give you any $$$h_{i,j}$$$, and just give you the front view, left view, and the top view of this object, and he is now asking you to restore the object. Note that in the front view, there are $$$m$$$ columns, and in the $$$i$$$-th of them, the height is the maximum of $$$h_{1,i},h_{2,i},\\dots,h_{n,i}$$$. It is similar for the left view, where there are $$$n$$$ columns. And in the top view, there is an $$$n \\times m$$$ matrix $$$t_{i,j}$$$, where $$$t_{i,j}$$$ is $$$0$$$ or $$$1$$$. If $$$t_{i,j}$$$ equals $$$1$$$, that means $$$h_{i,j}>0$$$, otherwise, $$$h_{i,j}=0$$$.However, Serval is very lonely because others are bored about his unsolvable problems before, and refused to solve this one, although this time he promises there will be at least one object satisfying all the views. As his best friend, can you have a try?", "c_code": "int solution() {\n int n;\n int m;\n int h;\n scanf(\"%d %d %d\", &n, &m, &h);\n int f_arr[m];\n int l_arr[n];\n int arr[n][m];\n\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &f_arr[i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &l_arr[i]);\n }\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &arr[i][j]);\n }\n }\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (arr[i][j]) {\n if (l_arr[i] < f_arr[j]) {\n arr[i][j] = l_arr[i];\n } else {\n arr[i][j] = f_arr[j];\n }\n }\n }\n }\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n printf(\"%d \", arr[i][j]);\n }\n printf(\"\\n\");\n }\n\n return 0;\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\n let n = get!(usize);\n let m = get!(usize);\n let _h = get!(usize);\n let mut front = vec![];\n for _ in 0..m {\n front.push(get!());\n }\n let mut left = vec![];\n for _ in 0..n {\n left.push(get!());\n }\n\n for i in 0..n {\n for j in 0..m {\n if j > 0 {\n print!(\" \");\n }\n let mut p = get!();\n if p > 0 {\n p = std::cmp::min(front[j], left[i]);\n }\n print!(\"{}\", p);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0432", "problem_description": "You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset.", "c_code": "int solution() {\n int n;\n int k;\n int m;\n scanf(\"%d%d%d\", &n, &k, &m);\n int a[n][2];\n int b[m];\n for (int i = 0; i < m; i++) {\n b[i] = 0;\n }\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i][0]);\n a[i][1] = a[i][0] % m;\n b[a[i][1]]++;\n }\n\n int j;\n for (j = 0; j < m; j++) {\n if (b[j] >= k) {\n break;\n }\n }\n if (j != m) {\n\n printf(\"Yes\\n\");\n\n for (int i = 0; i < n; i++) {\n if (k == 0) {\n break;\n }\n if (a[i][1] == j) {\n printf(\"%d \", a[i][0]);\n k--;\n }\n }\n printf(\"\\n\");\n } else {\n printf(\"No\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut reader = io::BufReader::new(io::stdin());\n let mut s = String::new();\n reader.read_line(&mut s).unwrap();\n let input: Vec = s\n .trim_end()\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let _N = input[0];\n let K = input[1];\n let M = input[2];\n s.clear();\n reader.read_line(&mut s).unwrap();\n let array: Vec = s\n .trim_end()\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let mut modulo: Vec> = Vec::new();\n for _i in 0..M {\n modulo.push(Vec::new());\n }\n for num in array {\n modulo[(num % M) as usize].push(num);\n }\n for i in 0..M {\n if modulo[i as usize].len() >= K as usize {\n println!(\"Yes\");\n modulo[i as usize].truncate(K as usize);\n let mut s = String::new();\n while !modulo[i as usize].is_empty() {\n s.push_str(modulo[i as usize].pop().unwrap().to_string().as_ref());\n if !modulo[i as usize].is_empty() {\n s.push(' ');\n }\n }\n println!(\"{}\", s);\n process::exit(0);\n }\n }\n println!(\"No\");\n}", "difficulty": "medium"} {"problem_id": "0433", "problem_description": "

すごろくと駒 (Sugoroku and Pieces)

\n\n\n

問題文

\n\n

\nJOI 君はすごろくを持っている.このすごろくは 2019 個のマスが横一列に並んだ形をしている.これらのマスには,左端のスタートマスから右端のゴールマスへと順に 1 から 2019 までの番号がついている.\n

\n\n

\n現在このすごろくの上には,N 個の駒が置かれている.これらの駒には,スタートに近い順に 1 から N までの番号がついている.駒 i (1 ≦ i ≦ N) は,マス X_i に置かれている.すべての駒は異なるマスに置かれている.\n

\n\n

\nJOI 君はこれから M 回の操作を行う.j 回目 (1 ≦ j ≦ M) の操作では,駒 A_j1 マス先へ進める.ただし,移動元のマスがゴールマスであった場合,もしくは移動先のマスに別の駒が置かれている場合,駒 A_j は進まず,位置は変わらない.\n

\n\n

\nすべての操作が終了した時点で,各駒が置かれているマスを求めよ.\n

\n\n

制約

\n\n
    \n
  • 1 ≦ N ≦ 100
  • \n
  • 1 ≦ X_1 < X_2 < ... < X_N ≦ 2019
  • \n
  • 1 ≦ M ≦ 100
  • \n
  • 1 ≦ A_j ≦ N (1 ≦ j ≦ M)
  • \n
\n\n

入力・出力

\n\n

\n入力
\n入力は以下の形式で標準入力から与えられる.
\nN
\nX_1 X_2 ... X_N
\nM
\nA_1 A_2 ... A_M\n

\n\n

\n出力
\nN 行出力せよ.i 行目 (1 ≦ i ≦ N) には,すべての操作が終了した時点で駒 i が置かれているマスの番号を出力せよ.\n

\n\n

入出力例

\n\n

入力例 1

\n
\n3\n2 3 6\n2\n1 3\n
\n\n

出力例 1

\n
\n2\n3\n7\n
\n\n

\n1 回目の操作では,駒 1 をマス 2 からマス 3 へと進めようする.しかし,駒 2 がすでにマス 3 に置かれているため,駒 1 は進まない.\n

\n\n

\n2 回目の操作では,駒 3 をマス 6 からマス 7 へと進める.\n

\n\n

\nすべての操作が終了した時点で,駒 1 はマス 2 に,駒 2 はマス 3 に,駒 3 はマス 7 に置かれている.\n

\n\n

入力例 2

\n
\n2\n1 2016\n4\n2 2 2 2\n
\n\n

出力例 2

\n
\n1\n2019\n
\n\n

\n3 回目の操作が完了した時点で,駒 2 はマス 2019 に置かれている.そのため,4 回目の操作では駒 2 は進まない.\n

\n\n

入力例 3

\n
\n4\n1001 1002 1003 1004\n7\n1 2 3 4 3 2 1\n
\n\n

出力例 3

\n
\n1002\n1003\n1004\n1005\n
\n
\n", "c_code": "int solution() {\n int n;\n int m;\n int x[201];\n int a[201];\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &x[i]);\n }\n scanf(\"%d\", &m);\n for (int i = 1; i <= m; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 1; i <= m; i++) {\n for (int j = 1; j <= n; j++) {\n if (j == a[i]) {\n if (x[j + 1] != x[j] + 1 && x[j] != 2019) {\n x[j]++;\n }\n }\n }\n }\n for (int i = 1; i <= n; i++) {\n printf(\"%d\\n\", x[i]);\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 mut x: Vec = iter\n .by_ref()\n .take(n)\n .map(|s| s.parse::().unwrap() - 1)\n .collect();\n let _m: usize = iter.next().unwrap().parse().unwrap();\n let a: Vec = iter.map(|s| s.parse::().unwrap() - 1).collect();\n\n let mut board: Vec = std::iter::repeat_n(0, 2019).collect();\n\n for (i, &place) in x.iter().enumerate() {\n board[place] = i;\n }\n\n for i in a {\n let place = x[i];\n if place < 2018 && board[place + 1] == 0 {\n x[i] += 1;\n board[place + 1] = board[place];\n board[place] = 0;\n }\n }\n\n for place in x {\n println!(\"{}\", place + 1);\n }\n}", "difficulty": "easy"} {"problem_id": "0434", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

\n

We have N balls. The i-th ball has an integer A_i written on it.
\nFor each k=1, 2, ..., N, solve the following problem and print the answer.

\n
    \n
  • Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.
  • \n
\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 3 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq A_i \\leq N
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

\n

For each k=1,2,...,N, print a line containing the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1 1 2 1 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n2\n3\n2\n3\n
\n

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.
\nFrom these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.
\nThus, the answer for k=1 is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 2

0\n0\n0\n0\n
\n

No two balls have equal numbers written on them.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n3 3 3 3 3\n
\n
\n
\n
\n
\n

Sample Output 3

6\n6\n6\n6\n6\n
\n

Any two balls have equal numbers written on them.

\n
\n
\n
\n
\n
\n

Sample Input 4

8\n1 2 1 4 2 1 4 1\n
\n
\n
\n
\n
\n

Sample Output 4

5\n7\n5\n7\n7\n5\n7\n5\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n long long a[n];\n long long set[200001] = {0};\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n set[a[i]]++;\n }\n long long sum = 0;\n for (int i = 1; i <= n; i++) {\n sum += (set[i] * (set[i] - 1) / 2);\n }\n for (int i = 0; i < n; i++) {\n printf(\"%lld\\n\", sum - ((set[a[i]] * (set[a[i]] - 1)) / 2) +\n ((set[a[i]] - 1) * (set[a[i]] - 2) / 2));\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let a: Vec = st\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let mut dic: HashMap<&String, i64> = HashMap::new();\n for i in a.iter() {\n let count = dic.entry(i).or_insert(0);\n *count += 1;\n }\n let mut result: i64 = 0;\n for (_x, y) in dic.iter() {\n result += y * (y - 1) / 2;\n }\n\n for i in a.iter() {\n println!(\"{}\", result - (dic[&i] - 1));\n }\n}", "difficulty": "hard"} {"problem_id": "0435", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1\\leq N \\leq 10^{16}
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

100\n
\n
\n
\n
\n
\n

Sample Output 1

18\n
\n

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

\n
\n
\n
\n
\n
\n

Sample Input 2

9995\n
\n
\n
\n
\n
\n

Sample Output 2

35\n
\n

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

\n
\n
\n
\n
\n
\n

Sample Input 3

3141592653589793\n
\n
\n
\n
\n
\n

Sample Output 3

137\n
\n
\n
", "c_code": "int solution() {\n char s[17];\n bool is9 = true;\n scanf(\"%s\", s);\n int len = strlen(s);\n for (int i = 1; i < len; i++) {\n if (s[i] != '9') {\n is9 = false;\n break;\n }\n }\n if (is9) {\n printf(\"%d\\n\", s[0] - '0' + (9 * len) - 9);\n } else {\n printf(\"%d\\n\", s[0] - '0' - 1 + (9 * len) - 9);\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 m = n;\n let mut o = 0;\n let mut p = 0;\n while m > 9 {\n o += m % 10;\n m /= 10;\n p += 1;\n }\n\n println!(\"{}\", max(m - 1 + p * 9, m + o));\n}", "difficulty": "medium"} {"problem_id": "0436", "problem_description": "The robot is placed in the top left corner of a grid, consisting of $$$n$$$ rows and $$$m$$$ columns, in a cell $$$(1, 1)$$$.In one step, it can move into a cell, adjacent by a side to the current one: $$$(x, y) \\rightarrow (x, y + 1)$$$; $$$(x, y) \\rightarrow (x + 1, y)$$$; $$$(x, y) \\rightarrow (x, y - 1)$$$; $$$(x, y) \\rightarrow (x - 1, y)$$$. The robot can't move outside the grid.The cell $$$(s_x, s_y)$$$ contains a deadly laser. If the robot comes into some cell that has distance less than or equal to $$$d$$$ to the laser, it gets evaporated. The distance between two cells $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$.Print the smallest number of steps that the robot can take to reach the cell $$$(n, m)$$$ without getting evaporated or moving outside the grid. If it's not possible to reach the cell $$$(n, m)$$$, print -1.The laser is neither in the starting cell, nor in the ending cell. The starting cell always has distance greater than $$$d$$$ to the laser.", "c_code": "int solution() {\n int numSets = 0;\n scanf(\"%d\", &numSets);\n\n for (int currentSet = 0; currentSet < numSets; ++currentSet) {\n int width = 0;\n int height = 0;\n int laserX = 0;\n int laserY = 0;\n int laserRadius = 0;\n scanf(\"%d %d %d %d %d\", &width, &height, &laserX, &laserY, &laserRadius);\n\n int touches = 0;\n if ((laserX - laserRadius <= 1) || (laserY + laserRadius >= height)) {\n ++touches;\n }\n if ((laserX + laserRadius >= width) || (laserY - laserRadius <= 1)) {\n ++touches;\n }\n if (touches == 2) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d\\n\", width + height - 2);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n for _tests in 0..t {\n let (n, m, sx, sy, d): (i64, i64, i64, i64, 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 iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().parse().unwrap(),\n )\n };\n let mut ans: i64 = n + m - 2;\n\n if sx - d <= 1 && sx + d >= n {\n ans = -1;\n }\n\n if sy - d <= 1 && sy + d >= m {\n ans = -1;\n }\n\n if sx - d <= 1 && sy - d <= 1 {\n ans = -1;\n }\n\n if sx + d >= n && sy + d >= m {\n ans = -1;\n }\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "hard"} {"problem_id": "0437", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Snuke loves puzzles.

\n

Today, he is working on a puzzle using S- and c-shaped pieces.\nIn this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:

\n
\n\"9b0bd546db9f28b4093d417b8f274124.png\"\n
\n

Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.

\n

Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N,M ≤ 10^{12}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N M\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 6\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Two Scc groups can be created as follows:

\n
    \n
  • Combine two c-shaped pieces into one S-shaped piece
  • \n
  • Create two Scc groups, each from one S-shaped piece and two c-shaped pieces
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

12345 678901\n
\n
\n
\n
\n
\n

Sample Output 2

175897\n
\n
\n
", "c_code": "int solution(void) {\n unsigned long long N = 0;\n unsigned long long M = 0;\n unsigned long long sum = 0;\n unsigned long long hen = 0;\n\n scanf(\"%llu %llu\", &N, &M);\n if (M >= 4 && (N * 2 <= M - 4)) {\n hen += (M - N * 2) / 4;\n M -= hen * 2;\n N += hen;\n };\n if (N <= M / 2) {\n sum += N;\n } else {\n sum += M / 2;\n };\n printf(\"%llu\\n\", sum);\n}", "rust_code": "fn solution() {\n use std::io;\n let mut input = String::new();\n let _ = io::stdin().read_line(&mut input);\n let input_nums = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n let n = input_nums[0];\n let m = input_nums[1];\n if n * 2 >= m {\n println!(\"{}\", m / 2);\n } else {\n println!(\"{}\", (m + n * 2) / 4);\n }\n}", "difficulty": "medium"} {"problem_id": "0438", "problem_description": "

ポイントカード (Point Card)

\n\n

問題

\n\n

\nJOI 商店街ではポイントカードのサービスを行っている.各ポイントカードには 2N 個のマスがある.商品を購入すると,くじを引くことができ,結果によって「当たり」か「はずれ」の印がマスに押される.同じマスに印が 2 回押されることはない.2N 個のマスのうち N 個以上のマスに当たりの印が書かれたポイントカードは,景品と交換することができる.\nまた,ポイントカードの印は,1 マスにつき 1 円で書き換えてもらうことができる.\n

\n\n

\nJOI 君は 2N 個のマスが全て埋まっているポイントカードを M 枚持っている.ポイントカード i (1 ≦ i ≦ M) には,Ai 個の当たり印と,Bi 個のはずれ印が押されている.JOI 君は M - 1 個以上の景品が欲しい.\n

\n\n

\nJOI 君が M - 1 個以上の景品を得るために必要な費用の最小値を求めよ.\n

\n\n

入力

\n\n

\n入力は M + 1 行からなる.\n

\n\n

\n1 行目には,2 個の整数 N, M (1 ≦ N ≦ 1000, 1 ≦ M ≦ 1000) が空白を区切りとして書かれている.これは,ポイントカードには 2N 個のマスがあり,JOI 君が M 枚のポイントカードを持っていることを表す.\n

\n\n

\n続く M 行のうちの i 行目 (1 ≦ i ≦ M) には,それぞれ 2 個の整数 Ai, Bi (0 ≦ Ai ≦ 2N, 0 ≦ Bi ≦ 2N, Ai + Bi = 2N) が書かれており,ポイントカード i には Ai 個の当たり印と Bi 個のはずれ印が押されていることを表す.\n

\n\n\n

出力

\n

\nJOI 君が M - 1 個以上の景品を得るために必要な費用の最小値を 1 行で出力せよ.\n

\n\n

入出力例

\n\n

入力例 1

\n\n
\n4 5\n1 7\n6 2\n3 5\n4 4\n0 8\n
\n\n

出力例 1

\n\n
\n4\n
\n\n
\n\n

入力例 2

\n
\n5 4\n5 5\n8 2\n3 7\n8 2\n
\n\n

出力例 2

\n
\n0\n
\n
\n

\n入力例 1 においては,ポイントカード 1 のはずれ印を 3 つ当たり印に書き換えてもらい,ポイントカード 3 のはずれ印を 1 つ当たり印に書き換えてもらうと,4 円で 4 (= 5 - 1) 枚のカードが景品と交換可能になり,これが最小の費用である.\n

\n\n

\n入力例 2 においては,既に 3 (= 4 - 1) 枚のカードが景品と交換可能なので,書き換えてもらう必要ない.\n

\n\n
\n", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int c = 0;\n int sum = 0;\n int max = 0;\n int n = 0;\n int m = 0;\n scanf(\"%d %d\", &n, &m);\n for (int i = 0; i < m; i++) {\n scanf(\"%d %d\", &a, &b);\n c = n - a;\n if (c > 0) {\n sum += c;\n }\n if (c > max) {\n max = c;\n }\n }\n printf(\"%d\\n\", sum - 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\n let mut tuples = input.split('\\n').map(|l| {\n let mut iter = l.split(' ').map(|s| s.parse::().unwrap());\n\n (iter.next().unwrap(), iter.next().unwrap())\n });\n\n let (n, m) = tuples.next().unwrap();\n\n let (mut sum, mut max) = (0, 0);\n\n for (a, _) in tuples.take(m) {\n if a < n {\n let cost = n - a;\n sum += cost;\n if max < cost {\n max = cost;\n }\n }\n }\n\n println!(\"{}\", sum - max);\n}", "difficulty": "easy"} {"problem_id": "0439", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are N students in a school.

\n

We will divide these students into some groups, and in each group they will discuss some themes.

\n

You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.

\n

Divide the students so that the number of groups consisting of three or more students is maximized.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 1000
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If you can form at most x groups consisting of three or more students, print x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

For example, you can form a group of three students and another of five students.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.

\n
\n
\n
\n
\n
\n

Sample Input 3

9\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution() {\n int N = 0;\n\n scanf(\"%d\", &N);\n\n printf(\"%d\\n\", N / 3);\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 println!(\"{}\", n / 3);\n}", "difficulty": "easy"} {"problem_id": "0440", "problem_description": "For years, the Day of city N was held in the most rainy day of summer. New mayor decided to break this tradition and select a not-so-rainy day for the celebration. The mayor knows the weather forecast for the $$$n$$$ days of summer. On the $$$i$$$-th day, $$$a_i$$$ millimeters of rain will fall. All values $$$a_i$$$ are distinct.The mayor knows that citizens will watch the weather $$$x$$$ days before the celebration and $$$y$$$ days after. Because of that, he says that a day $$$d$$$ is not-so-rainy if $$$a_d$$$ is smaller than rain amounts at each of $$$x$$$ days before day $$$d$$$ and and each of $$$y$$$ days after day $$$d$$$. In other words, $$$a_d < a_j$$$ should hold for all $$$d - x \\le j < d$$$ and $$$d < j \\le d + y$$$. Citizens only watch the weather during summer, so we only consider such $$$j$$$ that $$$1 \\le j \\le n$$$.Help mayor find the earliest not-so-rainy day of summer.", "c_code": "int solution() {\n long long int n;\n long long int x;\n long long int y;\n scanf(\"%lld %lld %lld\", &n, &x, &y);\n long long int arr[n + 1][3];\n for (int i = 1; i <= n; i++) {\n arr[i][0] = 0;\n arr[i][2] = 1;\n arr[i][1] = 1;\n }\n for (int i = 1; i <= n; i++) {\n scanf(\"%lld\", &arr[i][0]);\n for (int j = 1; j <= x; j++) {\n if (i - j > 0 && arr[i][0] >= arr[i - j][0]) {\n arr[i][1] = 0;\n }\n }\n }\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= y; j++) {\n if (i + j <= n && arr[i][0] >= arr[i + j][0]) {\n arr[i][2] = 0;\n }\n }\n }\n for (int i = 1; i <= n; i++) {\n if (arr[i][1] == 1 && arr[i][2] == 1) {\n printf(\"%d\\n\", i);\n break;\n }\n }\n}", "rust_code": "fn solution() {\n let (n, x, y) = {\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 )\n };\n\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let a: Vec = line\n .split_whitespace()\n .map(|it| it.parse::().unwrap())\n .collect();\n\n let mut answ = 0;\n for i in 0..n {\n let current = a[i as usize];\n let mut good = true;\n for j in i - x..=i + y {\n if j >= 0 && j < n && a[j as usize] <= current && i != j {\n good = false;\n break;\n }\n }\n if good {\n answ = i;\n break;\n }\n }\n\n println!(\"{}\", answ + 1);\n}", "difficulty": "medium"} {"problem_id": "0441", "problem_description": "You have given an array $$$a$$$ of length $$$n$$$ and an integer $$$x$$$ to a brand new robot. What the robot does is the following: it iterates over the elements of the array, let the current element be $$$q$$$. If $$$q$$$ is divisible by $$$x$$$, the robot adds $$$x$$$ copies of the integer $$$\\frac{q}{x}$$$ to the end of the array, and moves on to the next element. Note that the newly added elements could be processed by the robot later. Otherwise, if $$$q$$$ is not divisible by $$$x$$$, the robot shuts down.Please determine the sum of all values of the array at the end of the process.", "c_code": "int solution() {\n int t;\n int n;\n int k;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n long long int flag = 0;\n long long int sum = 0;\n scanf(\"%d%d\", &n, &k);\n long long int a[n];\n long long int c[100000] = {0};\n long long int b[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%lld\", &a[j]);\n b[j] = a[j];\n }\n while (flag == 0) {\n for (int i = 0; i < n && flag == 0; i++) {\n if (b[i] % k == 0) {\n c[i]++;\n b[i] /= k;\n } else {\n flag = 1;\n }\n }\n }\n for (int i = 0; i < n; i++) {\n sum += (c[i] + 1) * a[i];\n }\n printf(\"%lld\\n\", sum);\n }\n}", "rust_code": "fn solution() {\n let k = std::io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n 'rest: for _ls in 0..k {\n let mut l = String::new();\n std::io::stdin().read_line(&mut l).unwrap();\n let l = l\n .trim()\n .split(\" \")\n .flat_map(str::parse::)\n .collect::>();\n let mut m = String::new();\n std::io::stdin().read_line(&mut m).unwrap();\n let mut m = m\n .trim()\n .split(\" \")\n .flat_map(str::parse::)\n .collect::>();\n let mut ans: i128 = m.iter().sum();\n let pbool = true;\n let mut level = 1;\n 'breaker: while pbool {\n for (_index, ls) in m.iter_mut().enumerate() {\n if *ls % l[1] == 0 {\n *ls /= l[1];\n ans += *ls * (l[1].pow(level));\n } else {\n println!(\"{}\", ans);\n continue 'rest;\n }\n }\n level += 1;\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0442", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given three strings A, B and C. Check whether they form a word chain.

\n

More formally, determine whether both of the following are true:

\n
    \n
  • The last character in A and the initial character in B are the same.
  • \n
  • The last character in B and the initial character in C are the same.
  • \n
\n

If both are true, print YES. Otherwise, print NO.

\n
\n
\n
\n
\n

Constraints

    \n
  • A, B and C are all composed of lowercase English letters (a - z).
  • \n
  • 1 ≤ |A|, |B|, |C| ≤ 10, where |A|, |B| and |C| are the lengths of A, B and C, respectively.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print YES or NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

rng gorilla apple\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

They form a word chain.

\n
\n
\n
\n
\n
\n

Sample Input 2

yakiniku unagi sushi\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n

A and B form a word chain, but B and C do not.

\n
\n
\n
\n
\n
\n

Sample Input 3

a a a\n
\n
\n
\n
\n
\n

Sample Output 3

YES\n
\n
\n
\n
\n
\n
\n

Sample Input 4

aaaaaaaaab aaaaaaaaaa aaaaaaaaab\n
\n
\n
\n
\n
\n

Sample Output 4

NO\n
\n
\n
", "c_code": "int solution() {\n char A[10];\n char B[10];\n char C[10];\n int con = 0;\n\n scanf(\"%s %s %s\", A, B, C);\n\n for (int i = 1; i <= 10; i++) {\n if (A[i] == '\\0' && A[i - 1] == B[0]) {\n con++;\n }\n if (B[i] == '\\0' && B[i - 1] == C[0]) {\n con++;\n }\n }\n\n if (con == 2) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let iter = buf.split_whitespace();\n let mut v: Vec> = Vec::new();\n for i in iter {\n v.push(i.chars().collect::>());\n }\n let a = v[0].clone();\n let b = v[1].clone();\n let c = v[2].clone();\n if a[a.len() - 1] == b[0] && b[b.len() - 1] == c[0] {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "0443", "problem_description": "You are given an array $$$a_1, a_2, \\ldots, a_n$$$ of positive integers. A good pair is a pair of indices $$$(i, j)$$$ with $$$1 \\leq i, j \\leq n$$$ such that, for all $$$1 \\leq k \\leq n$$$, the following equality holds:$$$$$$ |a_i - a_k| + |a_k - a_j| = |a_i - a_j|, $$$$$$ where $$$|x|$$$ denotes the absolute value of $$$x$$$.Find a good pair. Note that $$$i$$$ can be equal to $$$j$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n if (t > 1000 || t < 0) {\n return 0;\n }\n int n[t];\n int **a = malloc(sizeof(int *) * t);\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &n[i]);\n if (n[i] > 100000 || n[i] < 1) {\n return 0;\n }\n a[i] = malloc(sizeof(int) * n[i]);\n for (int j = 0; j < n[i]; j++) {\n scanf(\"%d\", &a[i][j]);\n if (a[i][j] > 1000000000 || a[i][j] < 1) {\n return 0;\n }\n }\n }\n int sum = 0;\n for (int i = 0; i < t; i++) {\n sum += n[i];\n }\n if (sum > 200000) {\n return 0;\n }\n\n int count = 0;\n for (int x = 0; x < t; x++) {\n\n int max = a[x][0];\n int mai = 0;\n int min = a[x][0];\n int mii = 0;\n for (int i = 0; i < n[x]; i++) {\n if (a[x][i] > max) {\n max = a[x][i];\n mai = i;\n }\n }\n for (int i = 0; i < n[x]; i++) {\n if (a[x][i] < min) {\n min = a[x][i];\n mii = i;\n }\n }\n printf(\"%d %d\\n\", mii + 1, mai + 1);\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 _n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let a: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let x = 1 + a.iter().enumerate().max_by(|l, r| l.1.cmp(r.1)).unwrap().0;\n let y = 1 + a.iter().enumerate().min_by(|l, r| l.1.cmp(r.1)).unwrap().0;\n println!(\"{} {}\", y, x);\n }\n}", "difficulty": "hard"} {"problem_id": "0444", "problem_description": "Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them.Recently, she was presented with an array $$$a$$$ of the size of $$$10^9$$$ elements that is filled as follows: $$$a_1 = -1$$$ $$$a_2 = 2$$$ $$$a_3 = -3$$$ $$$a_4 = 4$$$ $$$a_5 = -5$$$ And so on ... That is, the value of the $$$i$$$-th element of the array $$$a$$$ is calculated using the formula $$$a_i = i \\cdot (-1)^i$$$.She immediately came up with $$$q$$$ queries on this array. Each query is described with two numbers: $$$l$$$ and $$$r$$$. The answer to a query is the sum of all the elements of the array at positions from $$$l$$$ to $$$r$$$ inclusive.Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you — the best programmer.Help her find the answers!", "c_code": "int solution() {\n long long int n = 0;\n long long int l = 0;\n long long int r = 0;\n long long int ans = 0;\n scanf(\"%lld\", &n);\n while (n--) {\n scanf(\"%lld %lld\", &l, &r);\n if (l == r) {\n if (l % 2 == 1) {\n printf(\"%lld\\n\", -l);\n } else if (l % 2 == 0) {\n printf(\"%lld\\n\", r);\n }\n\n } else if (r - l == 1) {\n if (r % 2 == 0) {\n printf(\"1\\n\");\n } else {\n printf(\"-1\\n\");\n }\n } else if (r % 2 == 0 && l % 2 == 0) {\n printf(\"%lld\\n\", (r + l) / 2);\n } else if (r % 2 == 1 && l % 2 == 0) {\n printf(\"%lld\\n\", (l / 2) - ((r + 1) / 2));\n } else if (r % 2 == 1 && l % 2 == 1) {\n printf(\"%lld\\n\", -(l + r) / 2);\n } else if (r % 2 == 0 && l % 2 == 1) {\n printf(\"%lld\\n\", ((r - l) / 2) + 1);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() -> std::io::Result<()> {\n let mut contents = String::new();\n std::io::stdin().read_line(&mut contents)?;\n let contents: Vec<&str> = contents.split(\"\\r\\n\").collect();\n let contents: &str = contents[0];\n let q: u32 = contents.parse().unwrap();\n for _i in 0..q {\n let mut contents = String::new();\n std::io::stdin().read_line(&mut contents)?;\n let contents: Vec<&str> = contents.split(\"\\r\\n\").collect();\n let contents: &str = contents[0];\n let contents: Vec<&str> = contents.split_whitespace().collect();\n let l: i64 = contents[0].parse().unwrap();\n let r: i64 = contents[1].parse().unwrap();\n let ll = if l % 2 == 0 && l == r {\n 0\n } else if l % 2 == 1 {\n l\n } else {\n l + 1\n };\n let lr = if r % 2 == 0 && l == r {\n 0\n } else if r % 2 == 1 {\n r\n } else {\n r - 1\n };\n let rl = if l % 2 == 1 && l == r {\n 0\n } else if l % 2 == 0 {\n l\n } else {\n l + 1\n };\n let rr = if r % 2 == 1 && l == r {\n 0\n } else if r % 2 == 0 {\n r\n } else {\n r - 1\n };\n let odd = ((ll + lr) / 2) * ((lr - ll) / 2 + 1);\n let even = ((rr + rl) / 2) * ((rr - rl) / 2 + 1);\n println!(\"{}\", even - odd);\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0445", "problem_description": "For some time the program of rounding numbers that had been developed by the Codeforces participants during one of the previous rounds, helped the citizens of Far Far Away to convert numbers into a more easily readable format. However, as time went by, the economy of the Far Far Away developed and the scale of operations grew. So the King ordered to found the Bank of Far Far Away and very soon even the rounding didn't help to quickly determine even the order of the numbers involved in operations. Besides, rounding a number to an integer wasn't very convenient as a bank needed to operate with all numbers with accuracy of up to 0.01, and not up to an integer.The King issued yet another order: to introduce financial format to represent numbers denoting amounts of money. The formal rules of storing a number in the financial format are as follows: A number contains the integer part and the fractional part. The two parts are separated with a character \".\" (decimal point). To make digits in the integer part of a number easier to read, they are split into groups of three digits, starting from the least significant ones. The groups are separated with the character \",\" (comma). For example, if the integer part of a number equals 12345678, then it will be stored in the financial format as 12,345,678 In the financial format a number's fractional part should contain exactly two digits. So, if the initial number (the number that is converted into the financial format) contains less than two digits in the fractional part (or contains no digits at all), it is complemented with zeros until its length equals 2. If the fractional part contains more than two digits, the extra digits are simply discarded (they are not rounded: see sample tests). When a number is stored in the financial format, the minus sign is not written. Instead, if the initial number had the minus sign, the result is written in round brackets. Please keep in mind that the bank of Far Far Away operates using an exotic foreign currency — snakes ($), that's why right before the number in the financial format we should put the sign \"$\". If the number should be written in the brackets, then the snake sign should also be inside the brackets. For example, by the above given rules number 2012 will be stored in the financial format as \"$2,012.00\" and number -12345678.9 will be stored as \"($12,345,678.90)\".The merchants of Far Far Away visited you again and expressed much hope that you supply them with the program that can convert arbitrary numbers to the financial format. Can you help them?", "c_code": "int solution() {\n char str[101];\n char minus = 0;\n scanf(\"%s\", str);\n if (str[0] == '-') {\n minus = 1;\n }\n char integerStr[101];\n int integerLen = 0;\n for (int i = minus; str[i] != '.' && str[i]; ++i) {\n integerStr[integerLen++] = str[i];\n }\n int tail = 2;\n char fractionalStr[tail];\n int fractionalLen = 0;\n for (int i = 0; i < tail; ++i) {\n fractionalStr[i] = '0';\n }\n for (int i = minus + integerLen; str[i]; ++i) {\n if (str[i] >= '0' && str[i] <= '9') {\n fractionalStr[fractionalLen++] = str[i];\n if (fractionalLen == tail) {\n break;\n }\n }\n }\n if (minus) {\n putchar('(');\n }\n putchar('$');\n for (int i = 0; i < integerLen; ++i) {\n putchar(integerStr[i]);\n if (i != integerLen - 1 && (i + 1 - integerLen % 3) % 3 == 0) {\n putchar(',');\n }\n }\n putchar('.');\n for (int i = 0; i < tail; ++i) {\n putchar(fractionalStr[i]);\n }\n if (minus) {\n putchar(')');\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut s = String::new();\n stdin.read_line(&mut s).unwrap();\n s = s.trim().to_string();\n\n let minus = s.contains('-');\n\n let mut r = String::new();\n\n if minus {\n r.push(')');\n s = s.get(1..).unwrap().to_string();\n }\n\n let mut frac = \"00\".to_string();\n if let Some(idx) = s.find('.') {\n frac.insert_str(0, s.get((idx + 1)..).unwrap());\n s.truncate(idx);\n }\n r.insert_str(0, frac.get(..2).unwrap());\n r.insert(0, '.');\n\n while s.len() > 3 {\n let b3 = s.len() - 3;\n r.insert_str(0, s.get(b3..).unwrap());\n r.insert(0, ',');\n s.truncate(b3);\n }\n r.insert_str(0, &s);\n\n r.insert(0, '$');\n if minus {\n r.insert(0, '(');\n }\n println!(\"{}\", r);\n}", "difficulty": "easy"} {"problem_id": "0446", "problem_description": "Polycarp is reading a book consisting of $$$n$$$ pages numbered from $$$1$$$ to $$$n$$$. Every time he finishes the page with the number divisible by $$$m$$$, he writes down the last digit of this page number. For example, if $$$n=15$$$ and $$$m=5$$$, pages divisible by $$$m$$$ are $$$5, 10, 15$$$. Their last digits are $$$5, 0, 5$$$ correspondingly, their sum is $$$10$$$.Your task is to calculate the sum of all digits Polycarp has written down.You have to answer $$$q$$$ independent queries.", "c_code": "int solution(void) {\n int q;\n int mids = 0;\n scanf(\"%d\", &q);\n long long int a[q][3];\n long long int c;\n for (int i = 0; i < q; i++) {\n scanf(\"%lld %lld\", &a[i][0], &a[i][1]);\n }\n for (int i = 0; i < q; i++) {\n mids = 0;\n a[i][2] = 0;\n c = a[i][0] / a[i][1];\n for (int j = 1; j <= 10; j++) {\n mids = mids + ((j * a[i][1]) % 10);\n }\n a[i][2] = a[i][2] + mids * (c / 10);\n for (int k = 1; k <= (c % 10); k++) {\n a[i][2] = a[i][2] + ((k * a[i][1]) % 10);\n }\n }\n for (int i = 0; i < q; i++) {\n printf(\"%lld\\n\", a[i][2]);\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 q = input.trim().parse::().unwrap();\n for _ in 0..q {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let mut input_iter = input.split_whitespace();\n let n = input_iter.next().unwrap().parse::().unwrap();\n let m = input_iter.next().unwrap().parse::().unwrap();\n\n let num_m = n / m;\n let m10 = m % 10;\n let mut sum: u64 = 0;\n if num_m % 10 != 0 && m10 != 0 {\n sum = (m10..)\n .step_by(m10 as usize)\n .take((num_m % 10) as usize)\n .map(|x| x % 10)\n .sum();\n }\n match m10 {\n 0 => (),\n 5 => sum += 25 * (num_m / 10),\n 2 | 4 | 6 | 8 => sum += 40 * (num_m / 10),\n _ => sum += 45 * (num_m / 10),\n }\n println!(\"{}\", sum);\n }\n}", "difficulty": "medium"} {"problem_id": "0447", "problem_description": "A map of some object is a rectangular field consisting of n rows and n columns. Each cell is initially occupied by the sea but you can cover some some cells of the map with sand so that exactly k islands appear on the map. We will call a set of sand cells to be island if it is possible to get from each of them to each of them by moving only through sand cells and by moving from a cell only to a side-adjacent cell. The cells are called to be side-adjacent if they share a vertical or horizontal side. It is easy to see that islands do not share cells (otherwise they together form a bigger island).Find a way to cover some cells with sand so that exactly k islands appear on the n × n map, or determine that no such way exists.", "c_code": "int solution() {\n int n;\n int k;\n int c = 0;\n int i = 0;\n int j = 0;\n scanf(\"%d %d\", &n, &k);\n int a[n][n];\n for (i = 0; i < n; i++) {\n for (j = 0; j < n; j++) {\n a[i][j] = 0;\n }\n }\n i = 0, j = 0;\n while (c != k && i < n) {\n a[i][j] = 1;\n j += 2;\n if (j >= n && a[i][0] == 0) {\n j = 0;\n i++;\n }\n if (j >= n && a[i][0] == 1) {\n j = 1;\n i++;\n }\n c++;\n }\n if (c != k) {\n printf(\"NO\");\n return 0;\n }\n printf(\"YES\\n\");\n for (i = 0; i < n; i++, printf(\"\\n\")) {\n for (j = 0; j < n; j++) {\n if (a[i][j] == 0) {\n printf(\"S\");\n } else {\n printf(\"L\");\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let inputstatus = 1;\n\n let mut buf = String::new();\n let filename = \"inputrust.txt\";\n\n if inputstatus == 0 {\n let mut f = File::open(filename).expect(\"file not found\");\n f.read_to_string(&mut buf)\n .expect(\"something went wrong reading the file\");\n } else {\n std::io::stdin().read_to_string(&mut buf).unwrap();\n }\n\n let mut iter = buf.split_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let mut k: usize = iter.next().unwrap().parse().unwrap();\n\n if n.is_multiple_of(2) && k > n * n / 2 {\n println!(\"NO\");\n return;\n } else if n % 2 == 1 && k > n * n / 2 + 1 {\n println!(\"NO\");\n return;\n }\n\n println!(\"YES\");\n for i in 0..n {\n for j in 0..n {\n if k > 0 && (i + j) % 2 == 0 {\n k -= 1;\n print!(\"L\");\n } else {\n print!(\"S\");\n }\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0448", "problem_description": "You are given a function $$$f$$$ written in some basic language. The function accepts an integer value, which is immediately written into some variable $$$x$$$. $$$x$$$ is an integer variable and can be assigned values from $$$0$$$ to $$$2^{32}-1$$$. The function contains three types of commands: for $$$n$$$ — for loop; end — every command between \"for $$$n$$$\" and corresponding \"end\" is executed $$$n$$$ times; add — adds 1 to $$$x$$$. After the execution of these commands, value of $$$x$$$ is returned.Every \"for $$$n$$$\" is matched with \"end\", thus the function is guaranteed to be valid. \"for $$$n$$$\" can be immediately followed by \"end\".\"add\" command can be outside of any for loops.Notice that \"add\" commands might overflow the value of $$$x$$$! It means that the value of $$$x$$$ becomes greater than $$$2^{32}-1$$$ after some \"add\" command. Now you run $$$f(0)$$$ and wonder if the resulting value of $$$x$$$ is correct or some overflow made it incorrect.If overflow happened then output \"OVERFLOW!!!\", otherwise print the resulting value of $$$x$$$.", "c_code": "int solution() {\n long long int32max = 4294967296 - 1;\n int ofc = 0;\n int i;\n int n;\n scanf(\"%d\", &n);\n long long arr[n];\n int index = 0;\n long long mult = 1;\n int fnum[n];\n int fnumindex = 0;\n for (i = 0; i < n; i++) {\n arr[i] = 0;\n }\n for (i = 0; i < n; i++) {\n char inp[10];\n scanf(\" %s\", inp);\n\n if (inp[0] == 'a') {\n if (mult > int32max) {\n printf(\"OVERFLOW!!!\");\n return 0;\n }\n arr[index++] = mult;\n\n } else if (inp[0] == 'f') {\n i--;\n } else if (inp[0] == 'e') {\n if (ofc == 0) {\n mult /= fnum[--fnumindex];\n } else {\n ofc--;\n }\n\n } else {\n if (mult < int32max) {\n fnum[fnumindex++] = atoi(inp);\n mult *= fnum[fnumindex - 1];\n } else {\n ofc++;\n }\n }\n }\n\n long long add = 0;\n for (i = 0; i < n; i++) {\n add += arr[i];\n if (add > int32max) {\n printf(\"OVERFLOW!!!\");\n return 0;\n }\n }\n printf(\"%lld\", add);\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let t: i64 = line.trim().parse::().expect(\"error\");\n let mut x = 0i64;\n let mut state: Vec = Vec::new();\n let num_iter = 1i64;\n state.push(num_iter);\n for _ in 0..t {\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let r: Vec<&str> = line.split_whitespace().collect();\n if r[0] == \"add\" {\n x += state[state.len() - 1];\n if x >= (1 << 32) {\n println!(\"OVERFLOW!!!\");\n ::std::process::exit(0);\n }\n } else if r[0] == \"for\" {\n let amt: i64 = r[1].trim().parse::().expect(\"error\");\n state.push(cmp::min(state[state.len() - 1] * amt, 1 << 32));\n } else {\n if !state.is_empty() {\n state.pop();\n }\n }\n }\n println!(\"{:?}\", x);\n}", "difficulty": "hard"} {"problem_id": "0449", "problem_description": "Valera is a collector. Once he wanted to expand his collection with exactly one antique item.Valera knows n sellers of antiques, the i-th of them auctioned ki items. Currently the auction price of the j-th object of the i-th seller is sij. Valera gets on well with each of the n sellers. He is perfectly sure that if he outbids the current price of one of the items in the auction (in other words, offers the seller the money that is strictly greater than the current price of the item at the auction), the seller of the object will immediately sign a contract with him.Unfortunately, Valera has only v units of money. Help him to determine which of the n sellers he can make a deal with.", "c_code": "int solution(void) {\n int n;\n int net;\n scanf(\"%d %d\", &n, &net);\n int a[n];\n int d[n];\n int count = 0;\n int **ptr = (int **)malloc(n * sizeof(int *));\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n ptr[i] = (int *)malloc(a[i] * sizeof(int));\n for (int j = 0; j < a[i]; j++) {\n scanf(\"%d\", &ptr[i][j]);\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < a[i]; j++) {\n if (net > ptr[i][j]) {\n d[count++] = i;\n break;\n }\n }\n }\n printf(\"%d\\n\", count);\n for (int i = 0; i < count; i++) {\n printf(\"%d\", d[i] + 1);\n if (i != count - 1) {\n printf(\" \");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let a: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n let n = a[0];\n let v = a[1];\n\n let mut sellers: Vec> = vec![];\n for _ in 0..n {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n sellers.push(s.split_whitespace().map(|x| x.parse().unwrap()).collect());\n }\n\n let mut r = 0;\n let mut s = String::new();\n\n for (idx, seller) in sellers.iter().enumerate() {\n for i in 1..seller.len() {\n if seller[i] < v {\n s = format!(\"{} {}\", s, idx + 1);\n r += 1;\n break;\n }\n }\n }\n\n println!(\"{}\", r);\n println!(\"{}\", s.trim());\n}", "difficulty": "hard"} {"problem_id": "0450", "problem_description": "Monocarp is planning to host a martial arts tournament. There will be three divisions based on weight: lightweight, middleweight and heavyweight. The winner of each division will be determined by a single elimination system.In particular, that implies that the number of participants in each division should be a power of two. Additionally, each division should have a non-zero amount of participants.$$$n$$$ participants have registered for the tournament so far, the $$$i$$$-th of them weighs $$$a_i$$$. To split participants into divisions, Monocarp is going to establish two integer weight boundaries $$$x$$$ and $$$y$$$ ($$$x < y$$$). All participants who weigh strictly less than $$$x$$$ will be considered lightweight. All participants who weigh greater or equal to $$$y$$$ will be considered heavyweight. The remaining participants will be considered middleweight.It's possible that the distribution doesn't make the number of participants in each division a power of two. It can also lead to empty divisions. To fix the issues, Monocarp can invite an arbitrary number of participants to each division.Note that Monocarp can't kick out any of the $$$n$$$ participants who have already registered for the tournament.However, he wants to invite as little extra participants as possible. Help Monocarp to choose $$$x$$$ and $$$y$$$ in such a way that the total amount of extra participants required is as small as possible. Output that amount.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n for (int n1 = 0; n1 < n; n1++) {\n\n int m = 0;\n int *a = malloc(1000000);\n int *b = malloc(1000000);\n scanf(\"\\n%d\", &m);\n for (int m1 = 0; m1 <= m; m1++) {\n a[m1] = 0;\n }\n for (int m1 = 0; m1 < m; m1++) {\n int k = 0;\n scanf(\"%d\", &k);\n a[k]++;\n }\n int b1 = -1;\n for (int m1 = 0; m1 <= m; m1++) {\n if (a[m1] != 0) {\n b1++;\n b[b1] = a[m1];\n }\n }\n int d1 = 1;\n int d2 = 1;\n int d3 = 1;\n int d = 0x7fffffff;\n for (int c1 = 0; c1 <= 18; c1++) {\n d2 = 1;\n for (int c2 = 0; c2 <= 18; c2++) {\n d3 = 1;\n for (int c3 = 0; c3 <= 18; c3++) {\n if (d1 + d2 + d3 >= m) {\n int e = 0;\n int f = 0;\n int m1 = 0;\n for (m1 = 0; m1 <= b1; m1++) {\n e = e + b[m1];\n if (e > d1) {\n break;\n }\n }\n e = 0;\n for (; m1 <= b1; m1++) {\n e = e + b[m1];\n if (e > d2) {\n break;\n }\n }\n e = 0;\n for (; m1 <= b1; m1++) {\n e = e + b[m1];\n if (e > d3) {\n f = 1;\n break;\n }\n }\n if (f == 0) {\n if (d1 + d2 + d3 < d) {\n d = d1 + d2 + d3;\n }\n }\n }\n d3 = d3 * 2;\n }\n d2 = d2 * 2;\n }\n d1 = d1 * 2;\n }\n printf(\"%d\", d - m);\n free(a);\n free(b);\n\n if (n1 != n - 1) {\n printf(\"\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let stdout = stdout();\n\n let stdin = stdin.lock();\n let stdout = stdout.lock();\n let mut stdout = BufWriter::new(stdout);\n\n let mut stdin = stdin.lines();\n let mut read = || stdin.next().unwrap().unwrap();\n\n let nn: usize = read().parse().unwrap_or_default();\n for _i in 0..nn {\n let n: usize = read().parse().unwrap_or_default();\n let mut w: Box<[u32]> = read()\n .split_ascii_whitespace()\n .take(n)\n .map(|t| t.parse().unwrap_or_default())\n .collect();\n w.sort();\n\n let mut prev = None;\n let mut cnt = 0;\n let mut stat = Vec::new();\n for x in w.iter() {\n if prev == Some(x) {\n cnt += 1;\n } else {\n if let Some(_) = prev.replace(x) {\n stat.push(cnt)\n }\n cnt = 1;\n }\n }\n if let Some(_) = prev {\n stat.push(cnt)\n }\n\n let mut stat_sums = vec![0_usize; stat.len()];\n let mut stat_sums_reverse = vec![0_usize; stat.len()];\n for i in 0..stat.len() {\n stat_sums[i] = stat[i] + if i > 0 { stat_sums[i - 1] } else { 0 };\n }\n\n for i in 0..stat.len() {\n stat_sums_reverse[i] =\n stat[stat.len() - (i + 1)] + if i > 0 { stat_sums_reverse[i - 1] } else { 0 };\n }\n\n let mut assign_lo = Vec::new();\n let mut assign_hi = Vec::new();\n let mut bucket = 1;\n while bucket <= n {\n let mut pos = stat_sums.partition_point(|&x| x <= bucket);\n let mut taken = if pos > 0 { stat_sums[pos - 1] } else { 0 };\n let mut added = bucket - taken;\n assign_lo.push((taken, added));\n\n pos = stat_sums_reverse.partition_point(|&x| x <= bucket);\n taken = if pos > 0 {\n stat_sums_reverse[pos - 1]\n } else {\n 0\n };\n added = bucket - taken;\n assign_hi.push((taken, added));\n bucket *= 2;\n }\n\n let mut best = usize::MAX;\n for lo in &assign_lo {\n for hi in &assign_hi {\n let overflow;\n let mid_taken;\n if lo.0 + hi.0 > n {\n overflow = lo.0 + hi.0 - n;\n mid_taken = 0;\n } else {\n overflow = 0;\n mid_taken = n - lo.0 - hi.0;\n }\n\n let mid_added = if mid_taken > 0 {\n mid_taken.next_power_of_two() - mid_taken\n } else {\n 1\n };\n\n let total_added = mid_added + lo.1 + hi.1 + overflow;\n\n best = min(total_added, best);\n }\n }\n\n writeln!(stdout, \"{}\", best).unwrap();\n }\n\n stdout.flush().unwrap();\n}", "difficulty": "hard"} {"problem_id": "0451", "problem_description": "The School №0 of the capital of Berland has n children studying in it. All the children in this school are gifted: some of them are good at programming, some are good at maths, others are good at PE (Physical Education). Hence, for each child we know value ti: ti = 1, if the i-th child is good at programming, ti = 2, if the i-th child is good at maths, ti = 3, if the i-th child is good at PE Each child happens to be good at exactly one of these three subjects.The Team Scientific Decathlon Olympias requires teams of three students. The school teachers decided that the teams will be composed of three children that are good at different subjects. That is, each team must have one mathematician, one programmer and one sportsman. Of course, each child can be a member of no more than one team.What is the maximum number of teams that the school will be able to present at the Olympiad? How should the teams be formed for that?", "c_code": "int solution() {\n int n;\n int c1 = 0;\n int c2 = 0;\n int c3 = 0;\n int mino = 0;\n scanf(\"%d\", &n);\n int a[n];\n int one1[n];\n int duo2[n];\n int tre3[n];\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] == 1) {\n one1[c1++] = i;\n } else if (a[i] == 2) {\n duo2[c2++] = i;\n } else {\n tre3[c3++] = i;\n }\n }\n if (c1 <= c2 && c1 <= c3) {\n mino = c1;\n } else if (c2 <= c1 && c2 <= c3) {\n mino = c2;\n } else {\n mino = c3;\n }\n printf(\"%d\\n\", mino);\n if (mino != 0) {\n for (int i = 0; i < mino; i++) {\n printf(\"%d %d %d\\n\", one1[i], duo2[i], tre3[i]);\n }\n }\n}", "rust_code": "fn solution() {\n use std::io;\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 let students = buf\n .split_whitespace()\n .map(|str| str.trim().parse::().unwrap());\n let mut p = Vec::with_capacity(5000);\n let mut m = Vec::with_capacity(5000);\n let mut e = Vec::with_capacity(5000);\n for (i, student) in students.enumerate().map(|(i, x)| (i + 1, x)) {\n if student == 1 {\n p.push(i);\n } else if student == 2 {\n m.push(i);\n } else if student == 3 {\n e.push(i);\n }\n }\n use std::cmp::min;\n let teams = min(p.len(), min(m.len(), e.len()));\n println!(\"{}\", teams);\n for _ in 0..teams {\n println!(\n \"{} {} {}\",\n p.pop().unwrap(),\n m.pop().unwrap(),\n e.pop().unwrap()\n );\n }\n}", "difficulty": "hard"} {"problem_id": "0452", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

\n

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ K ≤ N ≤ 200000
  • \n
  • 1 ≤ p_i ≤ 1000
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\np_1 ... p_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

\n

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n1 2 2 4 5\n
\n
\n
\n
\n
\n

Sample Output 1

7.000000000000\n
\n

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 1\n6 6 6 6\n
\n
\n
\n
\n
\n

Sample Output 2

3.500000000000\n
\n

Regardless of which die we choose, the expected value of the number shown is 3.5.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 4\n17 13 13 12 15 20 10 13 17 11\n
\n
\n
\n
\n
\n

Sample Output 3

32.000000000000\n
\n
\n
", "c_code": "int solution() {\n int N;\n int K;\n scanf(\"%d%d\", &N, &K);\n double p[N];\n double sum_E = 0;\n double ans = 0;\n for (int i = 0; i < N; i++) {\n scanf(\"%lf\", &p[i]);\n p[i] = ((p[i] * (p[i] + 1)) / 2) / p[i];\n }\n\n for (int i = 0; i < K; i++) {\n sum_E += p[i];\n }\n\n if (N == K) {\n printf(\"%.12f\", sum_E);\n } else {\n for (int i = 0; i <= N - K; i++) {\n sum_E += (p[K + i] - p[i]);\n if (ans < sum_E) {\n ans = sum_E;\n }\n }\n printf(\"%.12f\", ans);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let (n, k): (usize, usize) = {\n std::io::stdin().read_line(&mut buf).unwrap();\n let words: Vec = buf.split_whitespace().map(|w| w.parse().unwrap()).collect();\n (words[0], words[1])\n };\n let ps: Vec = {\n buf.clear();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.split_whitespace().map(|w| w.parse().unwrap()).collect()\n };\n let mut cumsum = vec![0_f64; n + 1];\n for i in 0..n {\n cumsum[i + 1] = cumsum[i] + (ps[i] + 1.0) / 2.0;\n }\n let mut max = cumsum[k] - cumsum[0];\n for i in k + 1..n + 1 {\n let e = cumsum[i] - cumsum[i - k];\n if e > max {\n max = e;\n }\n }\n println!(\"{}\", max);\n}", "difficulty": "hard"} {"problem_id": "0453", "problem_description": "Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across $$$n$$$ sessions follow the identity permutation (ie. in the first game he scores $$$1$$$ point, in the second game he scores $$$2$$$ points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on $$$[1,2,3]$$$ can yield $$$[3,1,2]$$$ but it cannot yield $$$[3,2,1]$$$ since the $$$2$$$ is in the same position. Given a permutation of $$$n$$$ integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed $$$10^{18}$$$.An array $$$a$$$ is a subarray of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.", "c_code": "int solution() {\n int t;\n int n;\n int i;\n\n scanf(\"%d\", &t);\n\n while (t--) {\n scanf(\"%d\", &n);\n int a[n + 1];\n int c = 0;\n int r = 0;\n int f = 0;\n int p = 0;\n\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n\n if (i > 0) {\n if (a[i] > a[i - 1] && a[i] == i + 1 && p > 0) {\n f++;\n }\n\n if (a[i] < a[i - 1]) {\n if (a[i] == i + 1) {\n f++;\n }\n\n c++;\n r += f;\n p++;\n }\n }\n }\n if (c > 0 && r > 0) {\n printf(\"2\\n\");\n } else if (c > 0) {\n printf(\"1\\n\");\n } else {\n printf(\"0\\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 let t: usize = itr.next().unwrap().parse().unwrap();\n\n let mut out = Vec::new();\n for _ in 0..t {\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\n let mut l = 0;\n let mut r = n - 1;\n while l < n && a[l] == l + 1 {\n l += 1;\n }\n while a[r] == r + 1 {\n r -= 1;\n if r == 0 {\n break;\n }\n }\n if l >= r {\n writeln!(out, \"0\").ok();\n continue;\n }\n let mut check = false;\n for i in l..r + 1 {\n if a[i] == i + 1 {\n check = true;\n }\n }\n writeln!(out, \"{}\", if check { 2 } else { 1 }).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "medium"} {"problem_id": "0454", "problem_description": "You're given an array $$$a$$$ of $$$n$$$ integers, such that $$$a_1 + a_2 + \\cdots + a_n = 0$$$.In one operation, you can choose two different indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$), decrement $$$a_i$$$ by one and increment $$$a_j$$$ by one. If $$$i < j$$$ this operation is free, otherwise it costs one coin.How many coins do you have to spend in order to make all elements equal to $$$0$$$?", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n long long cur = 0;\n for (int i = 0; i < n; i++) {\n cur = cur + a[i];\n\n if (cur < 0) {\n cur = 0;\n }\n }\n printf(\"%lld\\n\", cur);\n t--;\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let t: u32 = input.trim().parse().unwrap();\n input.clear();\n\n let mut mx: i64;\n let mut count: i64;\n for _ in 0..t {\n let mut q;\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n input.clear();\n io::stdin().read_line(&mut input).unwrap();\n q = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .rev();\n mx = 0;\n count = 0;\n for num in &mut q {\n count += num;\n mx = max(count, mx);\n }\n println!(\"{}\", mx);\n input.clear();\n }\n}", "difficulty": "hard"} {"problem_id": "0455", "problem_description": "Let $$$a$$$ and $$$b$$$ be two arrays of lengths $$$n$$$ and $$$m$$$, respectively, with no elements in common. We can define a new array $$$\\mathrm{merge}(a,b)$$$ of length $$$n+m$$$ recursively as follows: If one of the arrays is empty, the result is the other array. That is, $$$\\mathrm{merge}(\\emptyset,b)=b$$$ and $$$\\mathrm{merge}(a,\\emptyset)=a$$$. In particular, $$$\\mathrm{merge}(\\emptyset,\\emptyset)=\\emptyset$$$. If both arrays are non-empty, and $$$a_1<b_1$$$, then $$$\\mathrm{merge}(a,b)=[a_1]+\\mathrm{merge}([a_2,\\ldots,a_n],b)$$$. That is, we delete the first element $$$a_1$$$ of $$$a$$$, merge the remaining arrays, then add $$$a_1$$$ to the beginning of the result. If both arrays are non-empty, and $$$a_1>b_1$$$, then $$$\\mathrm{merge}(a,b)=[b_1]+\\mathrm{merge}(a,[b_2,\\ldots,b_m])$$$. That is, we delete the first element $$$b_1$$$ of $$$b$$$, merge the remaining arrays, then add $$$b_1$$$ to the beginning of the result. This algorithm has the nice property that if $$$a$$$ and $$$b$$$ are sorted, then $$$\\mathrm{merge}(a,b)$$$ will also be sorted. For example, it is used as a subroutine in merge-sort. For this problem, however, we will consider the same procedure acting on non-sorted arrays as well. For example, if $$$a=[3,1]$$$ and $$$b=[2,4]$$$, then $$$\\mathrm{merge}(a,b)=[2,3,1,4]$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).There is a permutation $$$p$$$ of length $$$2n$$$. Determine if there exist two arrays $$$a$$$ and $$$b$$$, each of length $$$n$$$ and with no elements in common, so that $$$p=\\mathrm{merge}(a,b)$$$.", "c_code": "int solution() {\n int tests;\n scanf(\"%d\", &tests);\n for (int t = 0; t < tests; ++t) {\n int n;\n scanf(\"%d\", &n);\n int p[2 * n];\n int idx[(2 * n) + 1];\n bool seen[(2 * n) + 1];\n bool sol[(2 * n) + 1];\n for (int i = 0; i < 2 * n; ++i) {\n scanf(\"%d\", p + i);\n idx[p[i]] = i;\n seen[p[i]] = false;\n sol[i] = false;\n }\n sol[0] = true;\n int last_idx = 2 * n;\n for (int max = 2 * n; max >= 1; --max) {\n if (!seen[max]) {\n for (int i = idx[max]; i < last_idx; ++i) {\n seen[p[i]] = true;\n }\n int len = last_idx - idx[max];\n last_idx = idx[max];\n for (int s = n; s >= len; --s) {\n if (sol[s - len]) {\n sol[s] = true;\n }\n }\n }\n }\n if (sol[n]) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdout = io::stdout();\n let mut output = BufWriter::new(stdout.lock());\n let mut stdin = io::stdin();\n let mut input_str = String::new();\n stdin.read_to_string(&mut input_str).unwrap();\n\n let mut input_iter = input_str.split_whitespace();\n\n\n\n let test_cases: usize = read!();\n\n for _ in 0..test_cases {\n let n: usize = read!();\n\n let mut v = Vec::::with_capacity(n * 2);\n\n for _ in 0..(n * 2) {\n v.push(read!());\n }\n\n let mut chunks = Vec::::with_capacity(n * 2);\n let mut count = 0;\n let mut max_in_aggregated_slice = 0;\n\n for x in &v {\n if *x > max_in_aggregated_slice {\n if count > 0 {\n chunks.push(count);\n }\n count = 1;\n max_in_aggregated_slice = *x;\n } else {\n count += 1;\n }\n }\n\n chunks.push(count);\n\n let mut is_aggregate_size_possible = vec![false; 2 * n + 1 + 1];\n is_aggregate_size_possible[0] = true;\n for chunk_size in &chunks {\n for aggregate_size in (0..(2 * n + 1)).rev() {\n if is_aggregate_size_possible[aggregate_size] {\n is_aggregate_size_possible[aggregate_size + *chunk_size] = true;\n }\n }\n }\n\n if is_aggregate_size_possible[n] {\n writeln!(output, \"YES\").unwrap();\n } else {\n writeln!(output, \"NO\").unwrap();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0456", "problem_description": "You have a sequence of $$$n$$$ colored blocks. The color of the $$$i$$$-th block is $$$c_i$$$, an integer between $$$1$$$ and $$$n$$$.You will place the blocks down in sequence on an infinite coordinate grid in the following way. Initially, you place block $$$1$$$ at $$$(0, 0)$$$. For $$$2 \\le i \\le n$$$, if the $$$(i - 1)$$$-th block is placed at position $$$(x, y)$$$, then the $$$i$$$-th block can be placed at one of positions $$$(x + 1, y)$$$, $$$(x - 1, y)$$$, $$$(x, y + 1)$$$ (but not at position $$$(x, y - 1)$$$), as long no previous block was placed at that position. A tower is formed by $$$s$$$ blocks such that they are placed at positions $$$(x, y), (x, y + 1), \\ldots, (x, y + s - 1)$$$ for some position $$$(x, y)$$$ and integer $$$s$$$. The size of the tower is $$$s$$$, the number of blocks in it. A tower of color $$$r$$$ is a tower such that all blocks in it have the color $$$r$$$.For each color $$$r$$$ from $$$1$$$ to $$$n$$$, solve the following problem independently: Find the maximum size of a tower of color $$$r$$$ that you can form by placing down the blocks according to the rules.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int d;\n scanf(\"%d\", &d);\n int arr[d];\n for (int i = 0; i < d; i++) {\n scanf(\"%d\", &arr[i]);\n }\n int finalans[d + 1];\n for (int i = 0; i < d + 1; i++) {\n finalans[i] = 0;\n }\n int index[d + 1];\n for (int i = 0; i < d + 1; i++) {\n index[i] = 0;\n }\n\n for (int i = 0; i < d; i++) {\n if (index[arr[i]] == 0) {\n index[arr[i]] = i + 1;\n finalans[arr[i]] = 1;\n } else if (((i + 1) - index[arr[i]]) % 2 == 1) {\n finalans[arr[i]]++;\n index[arr[i]] = i + 1;\n }\n }\n for (int i = 1; i < d + 1; i++) {\n printf(\"%d \", finalans[i]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let lines = std::io::stdin().lines().skip(2).step_by(2);\n\n for line in lines {\n let line = line.unwrap();\n let c = line\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let n = c.len();\n\n let mut res = vec![0; n];\n let mut pos = vec![0_usize; n];\n\n for (i, n) in c.iter().enumerate() {\n res[n - 1] += if res[n - 1] == 0 || (i - pos[n - 1]) % 2 == 0 {\n pos[n - 1] = i + 1;\n 1\n } else {\n 0\n }\n }\n\n for i in res {\n print!(\"{i} \")\n }\n\n println!()\n }\n}", "difficulty": "medium"} {"problem_id": "0457", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

\n

Given is a positive integer N. Consider repeatedly applying the operation below on N:

\n
    \n
  • First, choose a positive integer z satisfying all of the conditions below:
      \n
    • z can be represented as z=p^e, where p is a prime number and e is a positive integer;
    • \n
    • z divides N;
    • \n
    • z is different from all integers chosen in previous operations.
    • \n
    \n
  • \n
  • Then, replace N with N/z.
  • \n
\n

Find the maximum number of times the operation can be applied.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^{12}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

\n

Print the maximum number of times the operation can be applied.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

24\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

We can apply the operation three times by, for example, making the following choices:

\n
    \n
  • Choose z=2 (=2^1). (Now we have N=12.)
  • \n
  • Choose z=3 (=3^1). (Now we have N=4.)
  • \n
  • Choose z=4 (=2^2). (Now we have N=1.)
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

We cannot apply the operation at all.

\n
\n
\n
\n
\n
\n

Sample Input 3

64\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n

We can apply the operation three times by, for example, making the following choices:

\n
    \n
  • Choose z=2 (=2^1). (Now we have N=32.)
  • \n
  • Choose z=4 (=2^2). (Now we have N=8.)
  • \n
  • Choose z=8 (=2^3). (Now we have N=1.)
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 4

1000000007\n
\n
\n
\n
\n
\n

Sample Output 4

1\n
\n

We can apply the operation once by, for example, making the following choice:

\n
    \n
  • z=1000000007 (=1000000007^1). (Now we have N=1.)
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 5

997764507000\n
\n
\n
\n
\n
\n

Sample Output 5

7\n
\n
\n
", "c_code": "int solution() {\n long long int n = 0;\n int counter = 0;\n int tmp_counter = 0;\n\n scanf(\"%lld\", &n);\n\n for (long long int i = 2; n != 1;) {\n tmp_counter = 0;\n\n if (i > pow(n, 0.5)) {\n counter += 1;\n break;\n }\n\n while ((n % i) == 0) {\n n = n / i;\n tmp_counter++;\n }\n\n for (int i = 1; tmp_counter >= i; i++) {\n counter++;\n tmp_counter = tmp_counter - i;\n }\n\n if (i == 2) {\n i++;\n } else {\n i += 2;\n }\n }\n\n printf(\"%d\", counter);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n let mut n = is.trim().parse::().unwrap();\n let mut map = HashMap::new();\n\n let mut i = 2;\n while i * i <= n {\n while n % i == 0 {\n *map.entry(i).or_insert(0) += 1;\n n /= i;\n }\n\n i += 1;\n }\n\n if n != 1 {\n *map.entry(n).or_insert(0) += 1;\n }\n\n let mut ans = 0;\n for (_, v) in map {\n let mut i = 1;\n let mut v = v;\n while v >= i {\n v -= i;\n i += 1;\n ans += 1;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0458", "problem_description": "There is a toy building consisting of $$$n$$$ towers. Each tower consists of several cubes standing on each other. The $$$i$$$-th tower consists of $$$h_i$$$ cubes, so it has height $$$h_i$$$.Let's define operation slice on some height $$$H$$$ as following: for each tower $$$i$$$, if its height is greater than $$$H$$$, then remove some top cubes to make tower's height equal to $$$H$$$. Cost of one \"slice\" equals to the total number of removed cubes from all towers.Let's name slice as good one if its cost is lower or equal to $$$k$$$ ($$$k \\ge n$$$). Calculate the minimum number of good slices you have to do to make all towers have the same height. Of course, it is always possible to make it so.", "c_code": "int solution(void) {\n\n long long int n;\n long long int k;\n long long int min = 200001;\n long long int max = 0;\n long long int aux;\n long long int aux2;\n long long int total = 0;\n long long int *v;\n\n scanf(\"%lld %lld\", &n, &k);\n\n v = malloc(sizeof(long long int) * 200001);\n\n for (long long int j = 0; j <= 200001; j++) {\n v[j] = 0;\n }\n\n for (long long int i = 0; i < n; i++) {\n scanf(\"%lld\", &aux);\n v[aux]++;\n if (aux < min) {\n min = aux;\n }\n if (aux > max) {\n max = aux;\n }\n }\n\n if (min == max) {\n printf(\"0\\n\");\n return 0;\n }\n\n while (min != max) {\n\n aux2 = 0;\n while (aux2 < k) {\n aux2 += v[max];\n if (aux2 <= k) {\n v[max - 1] += v[max];\n max--;\n if (max == min) {\n break;\n }\n }\n }\n\n total++;\n }\n\n printf(\"%lld\\n\", total);\n\n return 0;\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\n let n = get!();\n let k = get!();\n let mut hs = vec![];\n for _ in 0..n {\n let h = get!();\n hs.push(h);\n }\n hs.sort();\n let hmin = hs[0];\n hs.reverse();\n let mut ans = 0;\n let mut hi = hs[0];\n let mut e = 0;\n let mut rem = 0;\n while hi > hmin {\n while hs[e] == hi {\n e += 1;\n }\n let df = hi - hs[e];\n for _ in 0..df {\n if rem + e as i64 > k {\n ans += 1;\n rem = 0;\n }\n rem += e as i64;\n }\n hi = hs[e];\n }\n if rem > 0 {\n ans += 1;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0459", "problem_description": "

X Cubic

\n\n

\nWrite a program which calculates the cube of a given integer x.\n

\n\n\n

Input

\n\n

\nAn integer x is given in a line.\n

\n\n

Output

\n\n

\nPrint the cube of x in a line.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ x ≤ 100
  • \n
\n\n

Sample Input 1

\n
\n2\n
\n

Sample Output 1

\n
\n8\n
\n\n

Sample Input 2

\n
\n3\n
\n

Sample Output 2

\n
\n27\n
", "c_code": "int solution() {\n int x = 0;\n\n scanf(\"%d\", &x);\n if (x > 0 && x < 101) {\n x = x * x * x;\n printf(\"%d\\n\", x);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut buf = String::new();\n\n let _ = stdin.read_line(&mut buf);\n let x: i64 = i64::from_str(buf.trim()).unwrap();\n\n println!(\"{}\", x.pow(3));\n}", "difficulty": "medium"} {"problem_id": "0460", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

\n

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

\n
\n
\n
\n
\n

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \\leq i < k) and X_k < Y_k.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 8
  • \n
  • P and Q are permutations of size N.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nP_1 P_2 ... P_N\nQ_1 Q_2 ... Q_N\n
\n
\n
\n
\n
\n

Output

Print |a - b|.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 3 2\n3 1 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

8\n7 3 5 4 2 1 6 8\n3 8 2 5 4 6 7 1\n
\n
\n
\n
\n
\n

Sample Output 2

17517\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3\n1 2 3\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int p[8 + 3];\n int q[8 + 3];\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &p[i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &q[i]);\n }\n\n int num_p = 0;\n int num_q = 0;\n for (int i = 0; i < n; i++) {\n\n int temp = 0;\n for (int j = i + 1; j < n; j++) {\n if (p[i] > p[j]) {\n temp++;\n }\n }\n for (int j = 1; j < n - i; j++) {\n temp *= j;\n }\n num_p += temp;\n\n temp = 0;\n for (int j = i + 1; j < n; j++) {\n if (q[i] > q[j]) {\n temp++;\n }\n }\n for (int j = 1; j < n - i; j++) {\n temp *= j;\n }\n num_q += temp;\n }\n\n printf(\"%d\\n\", abs(num_p - num_q));\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let p: Vec<_> = s\n .by_ref()\n .take(n)\n .map(|a| a.parse::().unwrap() - 1)\n .collect();\n let q: Vec<_> = s\n .by_ref()\n .take(n)\n .map(|a| a.parse::().unwrap() - 1)\n .collect();\n let fact = |i: u32| {\n if i == 0 {\n 1\n } else {\n (2..i + 1).product::()\n }\n };\n let f = |a: Vec| {\n let mut r: u32 = 0;\n let mut b: u32 = (1 << n) - 1;\n for a in a {\n b ^= 1 << a;\n r += (b & ((1 << a) - 1)).count_ones() * fact(b.count_ones());\n }\n r as i32\n };\n println!(\"{}\", (f(p) - f(q)).abs());\n}", "difficulty": "medium"} {"problem_id": "0461", "problem_description": "Vova is playing a computer game. There are in total $$$n$$$ turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is $$$k$$$.During each turn Vova can choose what to do: If the current charge of his laptop battery is strictly greater than $$$a$$$, Vova can just play, and then the charge of his laptop battery will decrease by $$$a$$$; if the current charge of his laptop battery is strictly greater than $$$b$$$ ($$$b<a$$$), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by $$$b$$$; if the current charge of his laptop battery is less than or equal to $$$a$$$ and $$$b$$$ at the same time then Vova cannot do anything and loses the game. Regardless of Vova's turns the charge of the laptop battery is always decreases.Vova wants to complete the game (Vova can complete the game if after each of $$$n$$$ turns the charge of the laptop battery is strictly greater than $$$0$$$). Vova has to play exactly $$$n$$$ turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n long long int t;\n long long int k;\n long long int n;\n long long int a;\n long long int b;\n long long int m;\n long long int i;\n long long int x;\n long long int low;\n long long int high;\n long long int mid;\n long long int l;\n scanf(\"%lld\", &t);\n for (m = 1; m <= t; m++) {\n scanf(\"%lld %lld %lld %lld\", &k, &n, &a, &b);\n l = 0;\n if (k - n * b <= 0) {\n printf(\"-1\\n\");\n l++;\n } else {\n if (k - n * a > 0) {\n printf(\"%lld\\n\", n);\n l++;\n } else {\n x = k - n * a;\n low = 1;\n high = n;\n while (low <= high) {\n mid = (high + low) / 2;\n if (x + mid * (a - b) > 0) {\n if (x + (mid - 1) * (a - b) > 0) {\n high = mid - 1;\n } else {\n printf(\"%lld\\n\", n - mid);\n l++;\n break;\n }\n }\n if (x + mid * (a - b) == 0) {\n printf(\"%lld\\n\", n - mid - 1);\n l++;\n break;\n }\n if (x + mid * (a - b) < 0) {\n low = mid + 1;\n }\n }\n }\n }\n if (l == 0) {\n printf(\"0\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let stdin = io::stdin();\n let mut input = String::new();\n stdin.read_line(&mut input)?;\n\n let q: u32 = input.trim().parse().expect(\"read error\");\n\n for _ in 1..q + 1 {\n let mut input = String::new();\n stdin.read_line(&mut input)?;\n let mut iter = input\n .split_whitespace()\n .map(|x| x.trim().parse::().expect(\"parse error\"));\n let k = iter.next().unwrap();\n let n = iter.next().unwrap();\n let a = iter.next().unwrap();\n let b = iter.next().unwrap();\n\n if k > b * n {\n let mut inf = 0;\n let mut sup = n + 1;\n while inf < sup {\n let x = (inf + sup) / 2;\n if (a - b) * x < k - b * n {\n inf = x + 1;\n } else {\n sup = x;\n }\n }\n\n println!(\"{}\", sup - 1);\n } else {\n println!(\"-1\");\n }\n }\n\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "0462", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Joisino the magical girl has decided to turn every single digit that exists on this world into 1.

\n

Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).

\n

She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).

\n

You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:

\n
    \n
  • If A_{i,j}≠-1, the square contains a digit A_{i,j}.
  • \n
  • If A_{i,j}=-1, the square does not contain a digit.
  • \n
\n

Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤H,W≤200
  • \n
  • 1≤c_{i,j}≤10^3 (i≠j)
  • \n
  • c_{i,j}=0 (i=j)
  • \n
  • -1≤A_{i,j}≤9
  • \n
  • All input values are integers.
  • \n
  • There is at least one digit on the wall.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\nc_{0,0} ... c_{0,9}\n:\nc_{9,0} ... c_{9,9}\nA_{1,1} ... A_{1,W}\n:\nA_{H,1} ... A_{H,W}\n
\n
\n
\n
\n
\n

Output

Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 4\n0 9 9 9 9 9 9 9 9 9\n9 0 9 9 9 9 9 9 9 9\n9 9 0 9 9 9 9 9 9 9\n9 9 9 0 9 9 9 9 9 9\n9 9 9 9 0 9 9 9 9 2\n9 9 9 9 9 0 9 9 9 9\n9 9 9 9 9 9 0 9 9 9\n9 9 9 9 9 9 9 0 9 9\n9 9 9 9 2 9 9 9 0 9\n9 2 9 9 9 9 9 9 9 0\n-1 -1 -1 -1\n8 1 1 8\n
\n
\n
\n
\n
\n

Sample Output 1

12\n
\n

To turn a single 8 into 1, it is optimal to first turn 8 into 4, then turn 4 into 9, and finally turn 9 into 1, costing 6 MP.

\n

The wall contains two 8s, so the minimum total MP required is 6×2=12.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 5\n0 999 999 999 999 999 999 999 999 999\n999 0 999 999 999 999 999 999 999 999\n999 999 0 999 999 999 999 999 999 999\n999 999 999 0 999 999 999 999 999 999\n999 999 999 999 0 999 999 999 999 999\n999 999 999 999 999 0 999 999 999 999\n999 999 999 999 999 999 0 999 999 999\n999 999 999 999 999 999 999 0 999 999\n999 999 999 999 999 999 999 999 0 999\n999 999 999 999 999 999 999 999 999 0\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Note that she may not need to change any digit.

\n
\n
\n
\n
\n
\n

Sample Input 3

3 5\n0 4 3 6 2 7 2 5 3 3\n4 0 5 3 7 5 3 7 2 7\n5 7 0 7 2 9 3 2 9 1\n3 6 2 0 2 4 6 4 2 3\n3 5 7 4 0 6 9 7 6 7\n9 8 5 2 2 0 4 7 6 5\n5 4 6 3 2 3 0 5 4 3\n3 6 2 3 4 2 4 0 8 9\n4 6 5 4 3 5 3 2 0 8\n2 1 3 4 5 7 8 6 4 0\n3 5 2 6 1\n2 5 3 2 1\n6 9 2 5 6\n
\n
\n
\n
\n
\n

Sample Output 3

47\n
\n
\n
", "c_code": "int solution() {\n int h;\n int w;\n scanf(\"%d%d\", &h, &w);\n int c[10][10];\n int a[h][w];\n\n for (int i = 0; i < 10; i++) {\n for (int j = 0; j < 10; j++) {\n scanf(\"%d\", &c[i][j]);\n }\n }\n\n long long ans = 0;\n for (int k = 0; k < 10; k++) {\n for (int i = 0; i < 10; i++) {\n for (int j = 0; j < 10; j++) {\n if (c[i][j] > c[i][k] + c[k][j]) {\n c[i][j] = c[i][k] + c[k][j];\n }\n }\n }\n }\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n scanf(\"%d\", &a[i][j]);\n if (a[i][j] > -1) {\n ans += c[a[i][j]][1];\n }\n }\n }\n printf(\"%lld\", 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 h: usize = itr.next().unwrap().parse().unwrap();\n let w: usize = itr.next().unwrap().parse().unwrap();\n let mut dp: Vec> = (0..10)\n .map(|_| {\n (0..10)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect()\n })\n .collect();\n for k in 0..10 {\n for i in 0..10 {\n for j in 0..10 {\n dp[i][j] = std::cmp::min(dp[i][j], dp[i][k] + dp[k][j]);\n }\n }\n }\n\n let mut ans: u64 = 0;\n for _ in 0..h * w {\n let a: isize = itr.next().unwrap().parse().unwrap();\n if a != -1 {\n ans += dp[a as usize][1];\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0463", "problem_description": "One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution.This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.", "c_code": "int solution(void) {\n int n = 0;\n int final = 0;\n int PVT_surity[3];\n\n scanf(\"%d\", &n);\n while (n > 0) {\n for (int i = 0; i < 3; i++) {\n scanf(\"%d\", &PVT_surity[i]);\n }\n if (PVT_surity[0] + PVT_surity[1] + PVT_surity[2] >= 2) {\n final++;\n }\n n--;\n }\n printf(\"%d\", final);\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let mut input = String::new();\n io::stdin().read_line(&mut input)?;\n\n let amount = input.trim().to_string().parse::()?;\n let mut result = 0;\n for _ in 0..amount {\n let mut vecs = String::new();\n io::stdin().read_line(&mut vecs)?;\n let mut correct = 0;\n for item in vecs.split_whitespace().map(|x| x.parse::().unwrap()) {\n if item == 1 {\n correct += 1;\n }\n }\n if correct >= 2 {\n result += 1;\n }\n }\n println!(\"{}\", result);\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0464", "problem_description": "Vasya, like many others, likes to participate in a variety of sweepstakes and lotteries. Now he collects wrappings from a famous chocolate bar \"Jupiter\". According to the sweepstake rules, each wrapping has an integer written on it — the number of points that the participant adds to his score as he buys the bar. After a participant earns a certain number of points, he can come to the prize distribution center and exchange the points for prizes. When somebody takes a prize, the prize's cost is simply subtracted from the number of his points.Vasya didn't only bought the bars, he also kept a record of how many points each wrapping cost. Also, he remembers that he always stucks to the greedy strategy — as soon as he could take at least one prize, he went to the prize distribution centre and exchanged the points for prizes. Moreover, if he could choose between multiple prizes, he chose the most expensive one. If after an exchange Vasya had enough points left to get at least one more prize, then he continued to exchange points.The sweepstake has the following prizes (the prizes are sorted by increasing of their cost): a mug (costs a points), a towel (costs b points), a bag (costs c points), a bicycle (costs d points), a car (costs e points). Now Vasya wants to recollect what prizes he has received. You know sequence p1, p2, ..., pn, where pi is the number of points Vasya got for the i-th bar. The sequence of points is given in the chronological order. You also know numbers a, b, c, d, e. Your task is to find, how many prizes Vasya received, what prizes they are and how many points he's got left after all operations are completed.", "c_code": "int solution(void) {\n long long prizes[5] = {0};\n long long pi[50];\n\n int n;\n scanf(\"%i\", &n);\n\n int i;\n int j;\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &pi[i]);\n }\n\n int costs[5];\n for (i = 0; i < 5; i++) {\n scanf(\"%i\", &costs[i]);\n }\n\n long long ps = 0;\n for (i = 0; i < n; i++) {\n ps += pi[i];\n\n for (j = 4; j >= 0; j--) {\n prizes[j] += ps / costs[j];\n ps %= costs[j];\n }\n }\n\n for (i = 0; i < 5; i++) {\n printf(\"%lld%c\", prizes[i], i == 4 ? '\\n' : ' ');\n }\n printf(\"%lld\\n\", ps);\n\n return 0;\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 winnings: Vec<_> = lines\n .next()\n .unwrap()\n .unwrap()\n .split(\" \")\n .map(|s| s.parse::().unwrap())\n .collect();\n let mut costs: Vec<_> = lines\n .next()\n .unwrap()\n .unwrap()\n .split(\" \")\n .enumerate()\n .map(|(index, s)| (s.parse::().unwrap(), index))\n .collect();\n costs.sort_by(|a, b| b.0.cmp(&a.0));\n let mut current = 0;\n let mut solution = vec![0; 5];\n for amount in winnings.into_iter() {\n current += amount;\n for cost in costs.iter() {\n let how_many = current / cost.0;\n current %= cost.0;\n solution[cost.1] += how_many;\n }\n }\n for how_many in solution {\n print!(\"{} \", how_many);\n }\n println!();\n println!(\"{}\", current);\n}", "difficulty": "medium"} {"problem_id": "0465", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given nonnegative integers a and b (a ≤ b), and a positive integer x.\nAmong the integers between a and b, inclusive, how many are divisible by x?

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 ≤ a ≤ b ≤ 10^{18}
  • \n
  • 1 ≤ x ≤ 10^{18}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
a b x\n
\n
\n
\n
\n
\n

Output

Print the number of the integers between a and b, inclusive, that are divisible by x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 8 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

There are three integers between 4 and 8, inclusive, that are divisible by 2: 4, 6 and 8.

\n
\n
\n
\n
\n
\n

Sample Input 2

0 5 1\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n

There are six integers between 0 and 5, inclusive, that are divisible by 1: 0, 1, 2, 3, 4 and 5.

\n
\n
\n
\n
\n
\n

Sample Input 3

9 9 2\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

There are no integer between 9 and 9, inclusive, that is divisible by 2.

\n
\n
\n
\n
\n
\n

Sample Input 4

1 1000000000000000000 3\n
\n
\n
\n
\n
\n

Sample Output 4

333333333333333333\n
\n

Watch out for integer overflows.

\n
\n
", "c_code": "int solution() {\n long long a;\n long long b;\n long long x;\n long long ans1;\n long long ans2;\n scanf(\"%lld%lld%lld\", &a, &b, &x);\n ans1 = (a - 1) / x;\n ans2 = b / x;\n if (b < x) {\n if (a == 0) {\n puts(\"1\");\n } else {\n puts(\"0\");\n }\n return 0;\n }\n if (a == 0 && x != 1) {\n ans2++;\n }\n printf(\"%lld\\n\", ans2 - ans1);\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 v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n println!(\n \"{}\",\n v[1] / v[2] - v[0] / v[2] + (if v[0].is_multiple_of(v[2]) { 1 } else { 0 })\n );\n}", "difficulty": "hard"} {"problem_id": "0466", "problem_description": "You are given a string $$$s$$$. You have to determine whether it is possible to build the string $$$s$$$ out of strings aa, aaa, bb and/or bbb by concatenating them. You can use the strings aa, aaa, bb and/or bbb any number of times and in any order.For example: aaaabbb can be built as aa $$$+$$$ aa $$$+$$$ bbb; bbaaaaabbb can be built as bb $$$+$$$ aaa $$$+$$$ aa $$$+$$$ bbb; aaaaaa can be built as aa $$$+$$$ aa $$$+$$$ aa; abab cannot be built from aa, aaa, bb and/or bbb.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n char s[51];\n while (t--) {\n scanf(\"%s\", s);\n int ans = 1;\n int n = strlen(s);\n if (n == 1) {\n printf(\"No\\n\");\n continue;\n }\n if (s[0] != s[1]) {\n ans = 0;\n }\n for (int i = 1; i < n - 1 && ans == 1; i++) {\n if (s[i] != s[i - 1] && s[i] != s[i + 1]) {\n ans = 0;\n }\n }\n if (s[n - 1] != s[n - 2]) {\n ans = 0;\n }\n printf(\"%s\", ans == 0 ? \"No\\n\" : \"Yes\\n\");\n }\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 t\");\n let mut t: usize = s.trim().parse().expect(\"t is not a number\");\n while t != 0 {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"Failed to read s\");\n let s = s.trim().as_bytes();\n\n let l = s.len();\n let mut res = true;\n for i in 0..l {\n if (i == 0 || s[i] != s[i - 1]) && (i == l - 1 || s[i] != s[i + 1]) {\n res = false;\n }\n }\n if res {\n println!(\"YES\")\n } else {\n println!(\"NO\")\n };\n t -= 1;\n }\n}", "difficulty": "easy"} {"problem_id": "0467", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).

\n

Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ |s| ≦ 200{,}000
  • \n
  • s consists of uppercase English letters.
  • \n
  • There exists a substring of s that starts with A and ends with Z.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

QWERTYASDFZXCV\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.

\n
\n
\n
\n
\n
\n

Sample Input 2

ZABCZ\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n
\n
\n
\n
\n
\n

Sample Input 3

HASFJGHOGAKZZFEGA\n
\n
\n
\n
\n
\n

Sample Output 3

12\n
\n
\n
", "c_code": "int solution(void) {\n char s[300000];\n int strat_a = 0;\n int strat_z = 0;\n bool flag = true;\n\n fgets(s, sizeof(s), stdin);\n for (int i = 0; s[i] != '\\n'; i++) {\n if (s[i] == 'A' && flag == true) {\n strat_a = i;\n flag = false;\n }\n if (s[i] == 'Z' && flag == false) {\n strat_z = i;\n }\n }\n printf(\"%d\\n\", strat_z - strat_a + 1);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n\n let mut f = false;\n let mut now = 0;\n let mut res = 0;\n for c in s.trim().chars() {\n if !f && c == 'A' {\n f = true;\n }\n\n if f {\n now += 1;\n }\n\n if f && c == 'Z' {\n res = now;\n }\n }\n\n println!(\"{}\", res);\n}", "difficulty": "easy"} {"problem_id": "0468", "problem_description": "Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.You've got string s = s1s2... sn (n is the length of the string), consisting only of characters \".\" and \"#\" and m queries. Each query is described by a pair of integers li, ri (1 ≤ li < ri ≤ n). The answer to the query li, ri is the number of such integers i (li ≤ i < ri), that si = si + 1.Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.", "c_code": "int solution() {\n char s[100000];\n int sum[100000];\n int m;\n int l;\n int r;\n\n sum[0] = 0;\n\n for (int i = 0;; i++) {\n scanf(\"%c\", &s[i]);\n if (s[i] != '.' && s[i] != '#') {\n break;\n }\n\n if (i > 0) {\n if (s[i - 1] == s[i]) {\n sum[i] = sum[i - 1] + 1;\n } else {\n sum[i] = sum[i - 1];\n }\n }\n }\n sum[0] = 0;\n\n scanf(\"%d\", &m);\n\n for (int j = 0; j < m; j++) {\n scanf(\"%d %d\", &l, &r);\n printf(\"%d\\n\", sum[r - 1] - sum[l - 1]);\n }\n\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 s: Vec = s.chars().collect();\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).unwrap();\n let n: usize = t.trim().parse().unwrap();\n let mut count = vec![0];\n let mut c = s[0];\n let mut sum = 0;\n for i in 1..s.len() {\n sum += if c == s[i] { 1 } else { 0 };\n count.push(sum);\n c = s[i];\n }\n\n for _i in 0..n {\n t = String::new();\n std::io::stdin().read_line(&mut t).unwrap();\n let mut ints = t.split_whitespace().map(|x| x.parse::().unwrap());\n let a = ints.next().unwrap();\n let b = ints.next().unwrap();\n println!(\"{}\", count[b - 1] - count[a - 1]);\n }\n}", "difficulty": "hard"} {"problem_id": "0469", "problem_description": "You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int numbers[n][2];\n for (int i = 0; i < n; i++) {\n scanf(\"%d %d\", &numbers[i][0], &numbers[i][1]);\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d\\n\", numbers[i][0] + numbers[i][1]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut cases = String::new();\n io::stdin()\n .read_line(&mut cases)\n .expect(\"Unable to read number of cases\");\n let cases: u32 = cases\n .trim()\n .parse()\n .expect(\"Unable to parse number of cases\");\n\n for _ in 0..cases {\n let mut val = String::new();\n io::stdin()\n .read_line(&mut val)\n .expect(\"Unable to read values\");\n\n let tuple: Vec = val\n .split_whitespace()\n .map(|x| x.parse().expect(\"Unable to parse values\"))\n .collect();\n\n assert_eq!(tuple.len(), 2, \"Did not get exactly 2 numbers\");\n\n println!(\"{}\", tuple[0] + tuple[1]);\n }\n}", "difficulty": "hard"} {"problem_id": "0470", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

\n

Takahashi is a teacher responsible for a class of N students.

\n

The students are given distinct student numbers from 1 to N.

\n

Today, all the students entered the classroom at different times.

\n

According to Takahashi's record, there were A_i students in the classroom when student number i entered the classroom (including student number i).

\n

From these records, reconstruct the order in which the students entered the classroom.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\le N \\le 10^5
  • \n
  • 1 \\le A_i \\le N
  • \n
  • A_i \\neq A_j (i \\neq j)
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 \\ldots A_N\n
\n
\n
\n
\n
\n

Output

\n

Print the student numbers of the students in the order the students entered the classroom.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2 3 1\n
\n
\n
\n
\n
\n

Sample Output 1

3 1 2\n
\n

First, student number 3 entered the classroom.

\n

Then, student number 1 entered the classroom.

\n

Finally, student number 2 entered the classroom.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n1 2 3 4 5\n
\n
\n
\n
\n
\n

Sample Output 2

1 2 3 4 5\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8\n8 2 7 3 4 5 6 1\n
\n
\n
\n
\n
\n

Sample Output 3

8 2 4 5 6 7 3 1\n
\n
\n
", "c_code": "int solution() {\n int N;\n int i;\n scanf(\"%d\", &N);\n int(*A) = (int(*))malloc(100010 * sizeof(int));\n int(*B) = (int(*))malloc(100010 * sizeof(int));\n\n for (i = 1; i <= N; i++) {\n scanf(\"%d\", &A[i]);\n B[A[i]] = i;\n }\n for (i = 1; i <= N; i++) {\n printf(\"%d \", B[i]);\n }\n free(A);\n free(B);\n return 0;\n}", "rust_code": "fn solution() {\n let _n: i64 = {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n let mut a: Vec<(i64, i64)> = {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n let ret = line.split_whitespace().map(|a| a.parse().unwrap());\n ret.zip(0i64..).collect()\n };\n a.sort();\n println!(\n \"{}\",\n a.iter()\n .map(|&(_a, i)| (i + 1).to_string())\n .collect::>()\n .join(\" \")\n )\n}", "difficulty": "hard"} {"problem_id": "0471", "problem_description": "One day n friends met at a party, they hadn't seen each other for a long time and so they decided to make a group photo together. Simply speaking, the process of taking photos can be described as follows. On the photo, each photographed friend occupies a rectangle of pixels: the i-th of them occupies the rectangle of width wi pixels and height hi pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is W × H, where W is the total sum of all widths and H is the maximum height of all the photographed friends.As is usually the case, the friends made n photos — the j-th (1 ≤ j ≤ n) photo had everybody except for the j-th friend as he was the photographer.Print the minimum size of each made photo in pixels.", "c_code": "int solution() {\n long n;\n long t;\n int m = 0;\n int c = 0;\n scanf(\"%ld\", &n);\n int a[n];\n int b[n];\n long long h;\n long long s = 0;\n for (long i = 0; i < n; i++) {\n scanf(\"%d %d\", &a[i], &b[i]);\n s = s + a[i];\n if (b[i] > m) {\n m = b[i];\n t = i;\n }\n }\n for (long i = 0; i < n; i++) {\n if (i != t && b[i] > c) {\n c = b[i];\n }\n }\n for (long i = 0; i < n; i++) {\n if (i == t) {\n h = (s - a[i]) * c;\n printf(\"%lld \", h);\n } else {\n h = (s - a[i]) * m;\n printf(\"%lld \", h);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n\n io::stdin().read_line(&mut n).expect(\"Failed to read line!\");\n\n let n = n.trim().parse::().expect(\"Type a number, please!\");\n\n let mut w: [u8; 200000] = [0; 200000];\n\n let mut h: [u16; 2] = [0; 2];\n\n let mut full_w: u32 = 0;\n\n let mut biggest_height_id: u32 = 0;\n\n for i in 0..n {\n let mut line = String::new();\n io::stdin()\n .read_line(&mut line)\n .expect(\"Failed to read line!\");\n\n let mut iter = line.split_whitespace();\n\n w[i as usize] = iter\n .next()\n .unwrap()\n .trim()\n .parse::()\n .expect(\"Type a number, please!\");\n\n full_w += w[i as usize] as u32;\n\n let height = iter\n .next()\n .unwrap()\n .trim()\n .parse::()\n .expect(\"Type a number, please!\");\n\n if height > h[0] {\n if h[1] < h[0] {\n h[1] = h[0]\n }\n h[0] = height;\n biggest_height_id = i;\n } else if height > h[1] {\n h[1] = height;\n }\n }\n\n for i in 0..n {\n let height = if biggest_height_id == i { h[1] } else { h[0] };\n print!(\"{} \", (full_w - w[i as usize] as u32) * height as u32);\n }\n}", "difficulty": "medium"} {"problem_id": "0472", "problem_description": "Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called \"Black Square\" on his super cool touchscreen phone.In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip.You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares?", "c_code": "int solution() {\n int calories[4];\n char str[100001];\n int i = 0;\n int count = 0;\n scanf(\"%d %d %d %d\", &calories[0], &calories[1], &calories[2], &calories[3]);\n scanf(\"%s\", str);\n if (calories[0] == '0' && calories[1] == '0' && calories[2] == '0' &&\n calories[3] == '0') {\n printf(\"0\");\n return 0;\n }\n while (str[i] != '\\0') {\n count += calories[str[i] - 49];\n i++;\n }\n printf(\"%d\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input: String = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"INPUT::read line failed\");\n let mut input = input.trim().split(' ');\n\n let one: i32 = input.next().unwrap().parse::().unwrap();\n let two: i32 = input.next().unwrap().parse::().unwrap();\n let three: i32 = input.next().unwrap().parse::().unwrap();\n let four: i32 = input.next().unwrap().parse::().unwrap();\n\n let mut input: String = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"INPUT::read line failed\");\n\n let mut vec = vec![];\n vec = input.trim().chars().collect::>();\n\n let _bound: usize = vec.len();\n let mut sum: i32 = i32::default();\n for i in &vec {\n match i {\n '1' => sum += one,\n '2' => sum += two,\n '3' => sum += three,\n '4' => sum += four,\n _ => println!(\"ERROR::number not exist\"),\n }\n }\n\n println!(\"{}\", sum);\n}", "difficulty": "medium"} {"problem_id": "0473", "problem_description": "The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.", "c_code": "int solution() {\n int h_rank = 0;\n int i = 0;\n int now = 0;\n int after = 0;\n int left = 0;\n scanf(\"%d\", &h_rank);\n\n int time_arr[h_rank - 1];\n\n for (i = 0; i < h_rank - 1; i++) {\n scanf(\"%d\", &time_arr[i]);\n }\n\n scanf(\"%d %d\", &now, &after);\n\n for (i = now - 1; i < after - 1; i++) {\n left += time_arr[i];\n }\n\n printf(\"%d\", left);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let mut n: usize = 0;\n let reader = io::stdin();\n reader.read_line(&mut buf);\n n = buf.trim().parse().expect(\" \");\n let mut v = vec![0; n - 1];\n\n let mut buf2 = String::new();\n reader.read_line(&mut buf2);\n let it = buf2.trim().split(\" \");\n let mut ct = 0;\n for sub in it {\n v[ct] = sub.trim().parse().expect(\" \");\n ct += 1;\n }\n\n let mut buf3 = String::new();\n reader.read_line(&mut buf3);\n let wv = buf3.trim().split(\" \").collect::>();\n let a: i32 = wv[0].parse().expect(\" \");\n let b: i32 = wv[1].parse().expect(\" \");\n let mut ans: u32 = 0;\n for i in (a - 1)..=(b - 2) {\n ans += v[i as usize];\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0474", "problem_description": "You are given a permutation $$$a$$$ of size $$$n$$$ and you should perform $$$n$$$ operations on it. In the $$$i$$$-th operation, you can choose a non-empty suffix of $$$a$$$ and increase all of its elements by $$$i$$$. How can we perform the operations to minimize the number of inversions in the final array?Note that you can perform operations on the same suffix any number of times you want.A permutation of size $$$n$$$ is an array of size $$$n$$$ such that each integer from $$$1$$$ to $$$n$$$ occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array $$$a$$$ is a pair of indices $$$(i, j)$$$ such that $$$i > j$$$ and $$$a_{i} < a_{j}$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int a;\n int n;\n scanf(\"%d\", &n);\n int perm[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a);\n perm[n - a] = i + 1;\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d \", perm[i]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let stdin_lock = stdin.lock();\n\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 = line_iter.next().unwrap().unwrap().parse::().unwrap();\n let a_line = line_iter.next().unwrap().unwrap();\n let a_iter = a_line\n .split(' ')\n .map(|a_str| a_str.parse::().unwrap());\n\n let mut ops = vec![0_usize; n];\n\n for (index, a_i) in a_iter.enumerate() {\n let i = n - a_i;\n ops[i] = index + 1;\n }\n\n print!(\"{}\", ops[0]);\n for index in ops[1..].iter() {\n print!(\" {}\", index);\n }\n println!();\n }\n}", "difficulty": "easy"} {"problem_id": "0475", "problem_description": "Petya organized a strange birthday party. He invited $$$n$$$ friends and assigned an integer $$$k_i$$$ to the $$$i$$$-th of them. Now Petya would like to give a present to each of them. In the nearby shop there are $$$m$$$ unique presents available, the $$$j$$$-th present costs $$$c_j$$$ dollars ($$$1 \\le c_1 \\le c_2 \\le \\ldots \\le c_m$$$). It's not allowed to buy a single present more than once.For the $$$i$$$-th friend Petya can either buy them a present $$$j \\le k_i$$$, which costs $$$c_j$$$ dollars, or just give them $$$c_{k_i}$$$ dollars directly.Help Petya determine the minimum total cost of hosting his party.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int m;\n long long sum = 0;\n scanf(\"%d%d\", &n, &m);\n long long k[n];\n long long c[m + 1];\n long long f[m + 1];\n for (long long i = 0; i <= m; i++) {\n f[i] = 0;\n }\n for (long long i = 0; i < n; i++) {\n scanf(\"%lld\", &k[i]);\n f[k[i]]++;\n }\n for (long long i = 1; i <= m; i++) {\n scanf(\"%lld\", &c[i]);\n }\n for (long long k = n, i = 1; i <= m && k > 0; i++) {\n if (k % (f[i] + 1) != 0 && k / (f[i] + 1) == 0) {\n sum = sum + (k)*c[i];\n } else {\n sum = sum + (f[i] + 1) * c[i];\n }\n k = k - (f[i] + 1);\n }\n printf(\"%lld\\n\", sum);\n }\n}", "rust_code": "fn solution() {\n use std::io::{self, Write};\n\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 t: usize = sc.next();\n for _ in 0..t {\n let n: usize = sc.next();\n let m: usize = sc.next();\n let idx: Vec = (0..n).map(|_| sc.next()).collect();\n let look_up: Vec = (0..m).map(|_| sc.next()).collect();\n let mut vals: Vec = vec![];\n idx.iter().for_each(|&x| vals.push(look_up[x - 1]));\n look_up.iter().for_each(|&x| vals.push(x));\n vals.sort();\n writeln!(out, \"{}\", vals[0..n].iter().fold(0, |x, y| x + *y as i64)).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0476", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We have weather records at AtCoder Town for some consecutive three days. A string of length 3, S, represents the records - if the i-th character is S, it means it was sunny on the i-th day; if that character is R, it means it was rainy on that day.

\n

Find the maximum number of consecutive rainy days in this period.

\n
\n
\n
\n
\n

Constraints

    \n
  • |S| = 3
  • \n
  • Each character of S is S or R.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the maximum number of consecutive rainy days in the period.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

RRS\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

We had rain on the 1-st and 2-nd days in the period. Here, the maximum number of consecutive rainy days is 2, so we should print 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

SSS\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

It was sunny throughout the period. We had no rainy days, so we should print 0.

\n
\n
\n
\n
\n
\n

Sample Input 3

RSR\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n

We had rain on the 1-st and 3-rd days - two \"streaks\" of one rainy day, so we should print 1.

\n
\n
", "c_code": "int solution() {\n char s[3];\n scanf(\"%s\", s);\n\n if (s[0] == 'S' && s[1] == 'S' && s[2] == 'S') {\n printf(\"0\");\n }\n if (s[0] == 'S' && s[1] == 'S' && s[2] == 'R') {\n printf(\"1\");\n }\n if (s[0] == 'S' && s[1] == 'R' && s[2] == 'S') {\n printf(\"1\");\n }\n if (s[0] == 'R' && s[1] == 'S' && s[2] == 'S') {\n printf(\"1\");\n }\n if (s[0] == 'S' && s[1] == 'R' && s[2] == 'R') {\n printf(\"2\");\n }\n if (s[0] == 'R' && s[1] == 'S' && s[2] == 'R') {\n printf(\"1\");\n }\n if (s[0] == 'R' && s[1] == 'R' && s[2] == 'S') {\n printf(\"2\");\n }\n if (s[0] == 'R' && s[1] == 'R' && s[2] == 'R') {\n printf(\"3\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let s = buf.trim();\n if s == \"RRR\" {\n println!(\"3\");\n } else if s == \"RRS\" || s == \"SRR\" {\n println!(\"2\");\n } else if s == \"RSS\" || s == \"SRS\" || s == \"SSR\" || s == \"RSR\" {\n println!(\"1\");\n } else {\n println!(\"0\");\n }\n}", "difficulty": "easy"} {"problem_id": "0477", "problem_description": "Doremy is asked to test $$$n$$$ contests. Contest $$$i$$$ can only be tested on day $$$i$$$. The difficulty of contest $$$i$$$ is $$$a_i$$$. Initially, Doremy's IQ is $$$q$$$. On day $$$i$$$ Doremy will choose whether to test contest $$$i$$$ or not. She can only test a contest if her current IQ is strictly greater than $$$0$$$.If Doremy chooses to test contest $$$i$$$ on day $$$i$$$, the following happens: if $$$a_i>q$$$, Doremy will feel she is not wise enough, so $$$q$$$ decreases by $$$1$$$; otherwise, nothing changes. If she chooses not to test a contest, nothing changes.Doremy wants to test as many contests as possible. Please give Doremy a solution.", "c_code": "int solution() {\n int k;\n scanf(\"%d\", &k);\n while (k--) {\n int n;\n int m;\n\n scanf(\"%d %d\", &n, &m);\n int array[n];\n int curr = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &array[i]);\n }\n\n for (long long int i = n - 1; i >= 0; i--) {\n if (array[i] <= curr) {\n array[i] = 1;\n } else if (curr < array[i] && curr < m) {\n array[i] = 1;\n curr++;\n } else if (curr >= m) {\n array[i] = 0;\n }\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d\", array[i]);\n }\n printf(\"\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdout = std::io::stdout();\n let mut writer = std::io::BufWriter::new(stdout.lock());\n\n \n\n input! {\n name = reader,\n tests: usize\n }\n for _ in 0..tests {\n input! {\n use reader,\n n: usize,\n q: u32,\n a: [u32; n]\n }\n let mut bytes = vec![b'0'; n];\n let mut nowq = 0;\n for (i, &ai) in a.iter().enumerate().rev() {\n if nowq >= ai {\n bytes[i] = b'1';\n } else if nowq < q {\n bytes[i] = b'1';\n nowq += 1;\n }\n }\n let bytes = String::from_utf8(bytes).unwrap();\n println!(\"{}\", bytes);\n }\n}", "difficulty": "hard"} {"problem_id": "0478", "problem_description": "Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it.Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant.The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially.Each customer is characterized by three values: $$$t_i$$$ — the time (in minutes) when the $$$i$$$-th customer visits the restaurant, $$$l_i$$$ — the lower bound of their preferred temperature range, and $$$h_i$$$ — the upper bound of their preferred temperature range.A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $$$i$$$-th customer is satisfied if and only if the temperature is between $$$l_i$$$ and $$$h_i$$$ (inclusive) in the $$$t_i$$$-th minute.Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n while (n--) {\n int a;\n int t0;\n scanf(\"%d %d\", &a, &t0);\n int s = 0;\n int min = t0;\n int max = t0;\n int k = 0;\n while (a--) {\n int s1;\n int mi;\n int ma;\n scanf(\"%d %d %d\", &s1, &mi, &ma);\n min = min - (s1 - s);\n max = max + (s1 - s);\n if (min >= mi && max <= ma) {\n min = min;\n max = max;\n } else if (min <= mi && max >= ma) {\n min = mi;\n max = ma;\n } else if (min <= mi && max >= mi && max <= ma) {\n min = mi;\n max = max;\n } else if (min >= mi && min <= ma && max >= ma) {\n min = min;\n max = ma;\n } else {\n k++;\n }\n s = s1;\n }\n if (k == 0) {\n printf(\"YEs\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\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\n .split_whitespace()\n .map(|x| x.parse::().expect(\"convert to number\"))\n .collect::>();\n\n let mut it = arr.iter();\n\n let q = *it.next().unwrap();\n\n for _i in 0..q {\n let n = *it.next().unwrap();\n let m = *it.next().unwrap();\n\n let mut arr = vec![];\n for _ in 0..n {\n let t = *it.next().unwrap();\n let l = *it.next().unwrap();\n let h = *it.next().unwrap();\n arr.push((t, l, h));\n }\n\n let mut time = 0i64;\n let mut range = (m, m);\n\n let mut possible = true;\n\n for &(t, l, h) in arr.iter() {\n let t_diff = t - time;\n\n let r_l = range.0 - t_diff;\n let r_h = range.1 + t_diff;\n\n let clamp_l = cmp::max(r_l, l);\n let clamp_h = cmp::min(r_h, h);\n\n if clamp_l > clamp_h {\n possible = false;\n break;\n }\n\n range = (clamp_l, clamp_h);\n time = t;\n }\n\n if possible {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0479", "problem_description": "Kawashiro Nitori is a girl who loves competitive programming.One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.Given a string $$$s$$$ and a parameter $$$k$$$, you need to check if there exist $$$k+1$$$ non-empty strings $$$a_1,a_2...,a_{k+1}$$$, such that $$$$$$s=a_1+a_2+\\ldots +a_k+a_{k+1}+R(a_k)+R(a_{k-1})+\\ldots+R(a_{1}).$$$$$$ Here $$$+$$$ represents concatenation. We define $$$R(x)$$$ as a reversed string $$$x$$$. For example $$$R(abcd) = dcba$$$. Note that in the formula above the part $$$R(a_{k+1})$$$ is intentionally skipped.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n\n for (int i = 0; i < t; ++i) {\n\n int N;\n int K;\n scanf(\"%d %d\", &N, &K);\n char string[200];\n scanf(\"%s\", string);\n\n int k = 0;\n int j = N - 1;\n\n int l = -1;\n\n while ((N % 2 == 0 && k < N / 2 - 1) || (N % 2 == 1 && k < N / 2)) {\n if (string[k] != string[j]) {\n l = k;\n break;\n }\n k = k + 1;\n j = j - 1;\n }\n\n if (l == -1 && N % 2 == 0 && string[0] == string[N - 1]) {\n\n l = N / 2 - 1;\n }\n if (l == -1 && N % 2 != 0 && string[0] == string[N - 1]) {\n\n l = N / 2;\n }\n\n if (l >= K) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let (n, k) = scan!(usize, usize);\n let s = scan!(bytes);\n\n if k < n.div_ceil(2) {\n let mut flag = true;\n for i in 0..k {\n if s[i] != s[n - 1 - i] {\n flag = false;\n break;\n }\n }\n if flag {\n print!(\"YES\");\n } else {\n print!(\"NO\");\n }\n } else {\n print!(\"NO\");\n }\n print!(\"\\n\");\n }\n}", "difficulty": "medium"} {"problem_id": "0480", "problem_description": "You are given an array $$$a_1, a_2, \\dots , a_n$$$ consisting of integers from $$$0$$$ to $$$9$$$. A subarray $$$a_l, a_{l+1}, a_{l+2}, \\dots , a_{r-1}, a_r$$$ is good if the sum of elements of this subarray is equal to the length of this subarray ($$$\\sum\\limits_{i=l}^{r} a_i = r - l + 1$$$).For example, if $$$a = [1, 2, 0]$$$, then there are $$$3$$$ good subarrays: $$$a_{1 \\dots 1} = [1], a_{2 \\dots 3} = [2, 0]$$$ and $$$a_{1 \\dots 3} = [1, 2, 0]$$$.Calculate the number of good subarrays of the array $$$a$$$.", "c_code": "int solution() {\n int tests;\n scanf(\"%d\", &tests);\n for (int i = 0; i < tests; ++i) {\n int32_t n;\n scanf(\"%\" SCNd32, &n);\n char *s = (char *)malloc(n + 1);\n int32_t *sum_cnt = (int32_t *)calloc(n * 9, sizeof(int32_t));\n int32_t *negative_sum_cnt = (int32_t *)calloc(n * 9, sizeof(int32_t));\n sum_cnt[0] = 1;\n scanf(\"%s\", s);\n int32_t sum = 0;\n int64_t total = 0;\n for (int32_t i = 0; i < n; ++i) {\n sum += (s[i] - '0' - 1);\n if (sum < 0) {\n total += negative_sum_cnt[-sum];\n ++negative_sum_cnt[-sum];\n } else {\n total += sum_cnt[sum];\n ++sum_cnt[sum];\n }\n }\n printf(\"%\" SCNd64 \"\\n\", total);\n free(s);\n free(sum_cnt);\n free(negative_sum_cnt);\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 a: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end()\n .chars()\n .map(|c| c.to_digit(10).unwrap() as i64)\n .collect()\n };\n\n let mut csa = vec![0; n + 1];\n for i in 0..n {\n csa[i + 1] = csa[i] + a[i];\n }\n\n let mut map = HashMap::new();\n for i in 0..=n {\n *map.entry(csa[i] - i as i64).or_insert(0i64) += 1;\n }\n\n let mut ans = 0;\n for x in map.values() {\n ans += x * (x - 1) / 2;\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0481", "problem_description": "Johnny has recently found an ancient, broken computer. The machine has only one register, which allows one to put in there one variable. Then in one operation, you can shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones. So, in fact, in one operation, you can multiply or divide your number by $$$2$$$, $$$4$$$ or $$$8$$$, and division is only allowed if the number is divisible by the chosen divisor. Formally, if the register contains a positive integer $$$x$$$, in one operation it can be replaced by one of the following: $$$x \\cdot 2$$$ $$$x \\cdot 4$$$ $$$x \\cdot 8$$$ $$$x / 2$$$, if $$$x$$$ is divisible by $$$2$$$ $$$x / 4$$$, if $$$x$$$ is divisible by $$$4$$$ $$$x / 8$$$, if $$$x$$$ is divisible by $$$8$$$ For example, if $$$x = 6$$$, in one operation it can be replaced by $$$12$$$, $$$24$$$, $$$48$$$ or $$$3$$$. Value $$$6$$$ isn't divisible by $$$4$$$ or $$$8$$$, so there're only four variants of replacement.Now Johnny wonders how many operations he needs to perform if he puts $$$a$$$ in the register and wants to get $$$b$$$ at the end.", "c_code": "int solution(void) {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n unsigned long long int a;\n unsigned long long int b;\n scanf(\"%llu %llu\", &a, &b);\n int count = 0;\n while (8 * a <= b) {\n a *= 8, count++;\n }\n while (4 * a <= b) {\n a *= 4, count++;\n }\n while (2 * a <= b) {\n a *= 2, count++;\n }\n if (a == b) {\n printf(\"%d\\n\", count);\n continue;\n }\n while ((!(a % 8)) && (a / 8 >= b)) {\n a /= 8, count++;\n }\n while ((!(a % 4)) && (a / 4 >= b)) {\n a /= 4, count++;\n }\n while ((!(a % 2)) && (a / 2 >= b)) {\n a /= 2, count++;\n }\n if (a == b) {\n printf(\"%d\\n\", count);\n } else {\n printf(\"-1\\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 let t: usize = itr.next().unwrap().parse().unwrap();\n let mut out = Vec::new();\n for _ in 0..t {\n let mut a: u64 = itr.next().unwrap().parse().unwrap();\n let mut b: u64 = itr.next().unwrap().parse().unwrap();\n if a > b {\n std::mem::swap(&mut a, &mut b);\n }\n if !b.is_multiple_of(a) {\n writeln!(out, \"-1\").ok();\n } else {\n let rem = b / a;\n if rem & (rem - 1) == 0 {\n let cnt = rem.trailing_zeros();\n writeln!(out, \"{}\", cnt / 3 + cnt % 3 / 2 + cnt % 3 % 2).ok();\n } else {\n writeln!(out, \"-1\").ok();\n }\n }\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "medium"} {"problem_id": "0482", "problem_description": "Berland Music is a music streaming service built specifically to support Berland local artist. Its developers are currently working on a song recommendation module.So imagine Monocarp got recommended $$$n$$$ songs, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th song had its predicted rating equal to $$$p_i$$$, where $$$1 \\le p_i \\le n$$$ and every integer from $$$1$$$ to $$$n$$$ appears exactly once. In other words, $$$p$$$ is a permutation.After listening to each of them, Monocarp pressed either a like or a dislike button. Let his vote sequence be represented with a string $$$s$$$, such that $$$s_i=0$$$ means that he disliked the $$$i$$$-th song, and $$$s_i=1$$$ means that he liked it.Now the service has to re-evaluate the song ratings in such a way that: the new ratings $$$q_1, q_2, \\dots, q_n$$$ still form a permutation ($$$1 \\le q_i \\le n$$$; each integer from $$$1$$$ to $$$n$$$ appears exactly once); every song that Monocarp liked should have a greater rating than every song that Monocarp disliked (formally, for all $$$i, j$$$ such that $$$s_i=1$$$ and $$$s_j=0$$$, $$$q_i>q_j$$$ should hold). Among all valid permutations $$$q$$$ find the one that has the smallest value of $$$\\sum\\limits_{i=1}^n |p_i-q_i|$$$, where $$$|x|$$$ is an absolute value of $$$x$$$.Print the permutation $$$q_1, q_2, \\dots, q_n$$$. If there are multiple answers, you can print any of them.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n\n int a[n];\n int b[n];\n int p;\n int cnt = 0;\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &p);\n p--;\n a[p] = i;\n }\n\n char s[n + 1];\n scanf(\"%s\", s);\n\n for (int i = 0; i < n; i++) {\n if (s[a[i]] == '0') {\n b[a[i]] = ++cnt;\n }\n }\n for (int i = 0; i < n; i++) {\n if (s[a[i]] == '1') {\n b[a[i]] = ++cnt;\n }\n }\n\n for (int i = 0; i < n; i++) {\n printf(\"%d \", b[i]);\n }\n\n printf(\"\\n\");\n }\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 while let Some(_) = i.next() {\n let p = i\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>();\n let s = i.next().unwrap().unwrap().chars().collect::>();\n let (mut x, mut y): (Vec<((usize, u32), &char)>, Vec<((usize, u32), &char)>) = p\n .into_iter()\n .enumerate()\n .zip(&s)\n .partition(|&t| *t.1 == '0');\n x.sort_unstable_by_key(|k| k.0 .1);\n y.sort_unstable_by_key(|k| k.0 .1);\n\n let mut x = (1..=s.len()).zip(x.iter().chain(&y)).collect::>();\n x.sort_unstable_by_key(|k| k.1 .0 .0);\n\n for (_z, ((_a, _n), _q)) in &x {\n write!(o, \"{} \", _z).ok();\n }\n writeln!(o).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "0483", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Today is August 24, one of the five Product Days in a year.

\n

A date m-d (m is the month, d is the date) is called a Product Day when d is a two-digit number, and all of the following conditions are satisfied (here d_{10} is the tens digit of the day and d_1 is the ones digit of the day):

\n
    \n
  • d_1 \\geq 2
  • \n
  • d_{10} \\geq 2
  • \n
  • d_1 \\times d_{10} = m
  • \n
\n

Takahashi wants more Product Days, and he made a new calendar called Takahashi Calendar where a year consists of M month from Month 1 to Month M, and each month consists of D days from Day 1 to Day D.

\n

In Takahashi Calendar, how many Product Days does a year have?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq M \\leq 100
  • \n
  • 1 \\leq D \\leq 99
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
M D\n
\n
\n
\n
\n
\n

Output

Print the number of Product Days in a year in Takahashi Calender.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

15 40\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

There are 10 Product Days in a year, as follows (m-d denotes Month m, Day d):

\n
    \n
  • 4-22
  • \n
  • 6-23
  • \n
  • 6-32
  • \n
  • 8-24
  • \n
  • 9-33
  • \n
  • 10-25
  • \n
  • 12-26
  • \n
  • 12-34
  • \n
  • 14-27
  • \n
  • 15-35
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

12 31\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 1\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int e = 0;\n int f = 0;\n int i = 0;\n int j = 0;\n\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n\n for (i = 1; i <= a; i++) {\n for (j = 1; j <= b; j++) {\n d = j % 10;\n e = j / 10;\n f = d * e;\n if (d >= 2 && e >= 2 && f == i) {\n c++;\n }\n }\n }\n printf(\"%d\", c);\n\n return 0;\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 m: usize = itr.next().unwrap().parse().unwrap();\n let d: usize = itr.next().unwrap().parse().unwrap();\n let mut ans = 0;\n for i in 1..m + 1 {\n for j in 1..d + 1 {\n let d1 = j / 10;\n let d2 = j % 10;\n if d1 >= 2 && d2 >= 2 && i == d1 * d2 {\n ans += 1;\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0484", "problem_description": "Let $$$LCM(x, y)$$$ be the minimum positive integer that is divisible by both $$$x$$$ and $$$y$$$. For example, $$$LCM(13, 37) = 481$$$, $$$LCM(9, 6) = 18$$$.You are given two integers $$$l$$$ and $$$r$$$. Find two integers $$$x$$$ and $$$y$$$ such that $$$l \\le x < y \\le r$$$ and $$$l \\le LCM(x, y) \\le r$$$.", "c_code": "int solution(void) {\n int linenum;\n\n scanf(\"%d\", &linenum);\n\n int l[linenum];\n int r[linenum];\n\n for (int i = 0; i < linenum; i++) {\n getchar();\n scanf(\"%d %d\", &l[i], &r[i]);\n }\n\n for (int i = 0; i < linenum; i++) {\n if (l[i] * 2 > r[i]) {\n printf(\"-1 -1\");\n } else {\n printf(\"%d %d\", l[i], 2 * l[i]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut line = String::new();\n stdin.read_line(&mut line).unwrap();\n let ntc: i32 = line.trim().parse().unwrap();\n for _ in 0..ntc {\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let a: Vec = line\n .split_whitespace()\n .map(|x| x.parse().expect(\"parse error\"))\n .collect();\n if a[0] * 2 <= a[1] {\n println!(\"{} {}\", a[0], a[0] * 2);\n } else {\n println!(\"-1 -1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0485", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2\\leq K\\leq 100
  • \n
  • K is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K\n
\n
\n
\n
\n
\n

Output

Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Two pairs can be chosen: (2,1) and (2,3).

\n
\n
\n
\n
\n
\n

Sample Input 2

6\n
\n
\n
\n
\n
\n

Sample Output 2

9\n
\n
\n
\n
\n
\n
\n

Sample Input 3

11\n
\n
\n
\n
\n
\n

Sample Output 3

30\n
\n
\n
\n
\n
\n
\n

Sample Input 4

50\n
\n
\n
\n
\n
\n

Sample Output 4

625\n
\n
\n
", "c_code": "int solution(void) {\n int K = 0;\n scanf(\"%d\", &K);\n printf(\"%d\", K / 2 * (K - K / 2));\n return 0;\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n let mut reader = BufReader::new(reader.lock());\n\n let mut s = String::new();\n\n let _ = reader.read_line(&mut s);\n\n let k: u32 = s.trim().parse().unwrap();\n\n let tmp = k / 2;\n\n println!(\n \"{}\",\n if k.is_multiple_of(2) {\n tmp * tmp\n } else {\n tmp * (tmp + 1)\n }\n );\n}", "difficulty": "medium"} {"problem_id": "0486", "problem_description": "Gnome sort", "c_code": "void solution(int *numbers, int size) {\n int pos = 0;\n while (pos < size) {\n if (numbers[pos] >= numbers[pos - 1]) {\n pos++;\n } else {\n int tmp = numbers[pos - 1];\n numbers[pos - 1] = numbers[pos];\n numbers[pos] = tmp;\n pos--;\n\n if (pos == 0) {\n pos = 1;\n }\n }\n }\n}\n", "rust_code": "fn solution(arr: &[T]) -> Vec\nwhere\n T: cmp::PartialEq + cmp::PartialOrd + Clone,\n{\n let mut arr = arr.to_vec();\n let mut i: usize = 1;\n let mut j: usize = 2;\n\n while i < arr.len() {\n if arr[i - 1] < arr[i] {\n i = j;\n j = i + 1;\n } else {\n arr.swap(i - 1, i);\n i -= 1;\n if i == 0 {\n i = j;\n j += 1;\n }\n }\n }\n arr\n}\n", "difficulty": "medium"} {"problem_id": "0487", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are a total of A + B cats and dogs.\nAmong them, A are known to be cats, but the remaining B are not known to be either cats or dogs.

\n

Determine if it is possible that there are exactly X cats among these A + B animals.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A \\leq 100
  • \n
  • 1 \\leq B \\leq 100
  • \n
  • 1 \\leq X \\leq 200
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B X\n
\n
\n
\n
\n
\n

Output

If it is possible that there are exactly X cats, print YES; if it is impossible, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 5 4\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

If there are one cat and four dogs among the B = 5 animals, there are X = 4 cats in total.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2 6\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n

Even if all of the B = 2 animals are cats, there are less than X = 6 cats in total.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 3 2\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n

Even if all of the B = 3 animals are dogs, there are more than X = 2 cats in total.

\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int c = 0;\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n scanf(\"%d\", &c);\n if (a + b >= c) {\n if (a <= c) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n } else {\n printf(\"NO\");\n }\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 input: Vec = s\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n let mut state = 0;\n let lefta = input[2] - input[0];\n if lefta >= 0 && lefta <= input[1] {\n state = 1\n }\n\n let ans = [\"NO\", \"YES\"];\n println!(\"{}\", ans[state]);\n}", "difficulty": "medium"} {"problem_id": "0488", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:

\n
    \n
  1. Throw the die. The current score is the result of the die.
  2. \n
  3. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
  4. \n
  5. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
  6. \n
\n

You are given N and K. Find the probability that Snuke wins the game.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 10^5
  • \n
  • 1 ≤ K ≤ 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 10\n
\n
\n
\n
\n
\n

Sample Output 1

0.145833333333\n
\n
    \n
  • If the die shows 1, Snuke needs to get four consecutive heads from four coin flips to obtain a score of 10 or above. The probability of this happening is \\frac{1}{3} \\times (\\frac{1}{2})^4 = \\frac{1}{48}.
  • \n
  • If the die shows 2, Snuke needs to get three consecutive heads from three coin flips to obtain a score of 10 or above. The probability of this happening is \\frac{1}{3} \\times (\\frac{1}{2})^3 = \\frac{1}{24}.
  • \n
  • If the die shows 3, Snuke needs to get two consecutive heads from two coin flips to obtain a score of 10 or above. The probability of this happening is \\frac{1}{3} \\times (\\frac{1}{2})^2 = \\frac{1}{12}.
  • \n
\n

Thus, the probability that Snuke wins is \\frac{1}{48} + \\frac{1}{24} + \\frac{1}{12} = \\frac{7}{48} \\simeq 0.1458333333.

\n
\n
\n
\n
\n
\n

Sample Input 2

100000 5\n
\n
\n
\n
\n
\n

Sample Output 2

0.999973749998\n
\n
\n
", "c_code": "int solution(void) {\n double N = 0;\n int K = 0;\n int count = 1;\n double answer = 0;\n double temp = 1.0;\n int temp2 = 0;\n scanf(\"%lf %d\", &N, &K);\n double kaku[100000] = {0};\n for (int i = 1; i <= N; i++) {\n temp2 = i;\n while (1) {\n if (temp2 >= K) {\n for (int j = 1; j < count; j++) {\n temp *= 0.5;\n }\n answer = 1.0 / N;\n kaku[i] = answer * temp;\n break;\n }\n temp2 *= 2;\n count++;\n }\n count = 1;\n temp = 1;\n }\n answer = 0;\n for (int i = 1; i <= N; i++) {\n answer += kaku[i];\n }\n printf(\"%.12lf\\n\", answer);\n return 0;\n}", "rust_code": "fn solution() {\n let text = {\n use std::io::Read;\n let mut s = String::new();\n std::io::stdin().read_to_string(&mut s).unwrap();\n s\n };\n\n let mut iter = text.split_whitespace();\n let num: u64 = iter.next().unwrap().parse().unwrap();\n let goal: u64 = iter.next().unwrap().parse().unwrap();\n\n let mut p_s: f64 = 0.0;\n\n for current_score in 1..(num + 1) {\n let mut current_score = current_score;\n let mut p: f64 = 1.0;\n\n while current_score < goal {\n p /= 2.0;\n current_score *= 2;\n }\n\n p_s += p;\n }\n\n println!(\"{}\", p_s / (num as f64));\n}", "difficulty": "hard"} {"problem_id": "0489", "problem_description": "A ticket is a string consisting of six digits. A ticket is considered lucky if the sum of the first three digits is equal to the sum of the last three digits. Given a ticket, output if it is lucky or not. Note that a ticket can have leading zeroes.", "c_code": "int solution() {\n int tickets = 0;\n scanf(\"%d\\n\", &tickets);\n char t[6];\n int sum_f = 0;\n int sum_l = 0;\n while (tickets != 0) {\n for (int i = 0; i < 3; i++) {\n scanf(\"%c\", &t[i]);\n sum_f = sum_f + (t[i] - 48);\n }\n for (int i = 3; i < 6; i++) {\n scanf(\"%c\\n\", &t[i]);\n sum_l = sum_l + (t[i] - 48);\n }\n if (sum_f == sum_l) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n for (int i = 0; i < 6; i++) {\n t[i] = 0;\n }\n sum_f = 0;\n sum_l = 0;\n tickets--;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_t = String::new();\n std::io::stdin()\n .read_line(&mut input_t)\n .expect(\"Can't read line\");\n let t: i32 = input_t.trim().parse().expect(\"Can't parse input to i32\");\n\n println!();\n\n for _ in 0..t {\n let mut input_line = String::new();\n std::io::stdin()\n .read_line(&mut input_line)\n .expect(\"Can't read line\");\n\n let mut char_line = input_line.chars();\n\n let mut x = 0;\n let mut y = 0;\n\n for _ in 0..3 {\n x += char_line\n .next()\n .expect(\"something\")\n .to_digit(10)\n .expect(\"Can't parse input to number\");\n }\n\n for _ in 0..3 {\n y += char_line\n .next()\n .expect(\"something\")\n .to_digit(10)\n .expect(\"can't parse\");\n }\n\n if x == y {\n println!(\"YES\")\n } else {\n println!(\"NO\")\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0490", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

N hotels are located on a straight line. The coordinate of the i-th hotel (1 \\leq i \\leq N) is x_i.

\n

Tak the traveler has the following two personal principles:

\n
    \n
  • He never travels a distance of more than L in a single day.
  • \n
  • He never sleeps in the open. That is, he must stay at a hotel at the end of a day.
  • \n
\n

You are given Q queries. The j-th (1 \\leq j \\leq Q) query is described by two distinct integers a_j and b_j.\nFor each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles.\nIt is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq L \\leq 10^9
  • \n
  • 1 \\leq Q \\leq 10^5
  • \n
  • 1 \\leq x_i < x_2 < ... < x_N \\leq 10^9
  • \n
  • x_{i+1} - x_i \\leq L
  • \n
  • 1 \\leq a_j,b_j \\leq N
  • \n
  • a_j \\neq b_j
  • \n
  • N,\\,L,\\,Q,\\,x_i,\\,a_j,\\,b_j are integers.
  • \n
\n
\n
\n
\n
\n

Partial Score

    \n
  • 200 points will be awarded for passing the test set satisfying N \\leq 10^3 and Q \\leq 10^3.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nx_1 x_2 ... x_N\nL\nQ\na_1 b_1\na_2 b_2\n:\na_Q b_Q\n
\n
\n
\n
\n
\n

Output

Print Q lines.\nThe j-th line (1 \\leq j \\leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

9\n1 3 6 13 15 18 19 29 31\n10\n4\n1 8\n7 3\n6 7\n8 5\n
\n
\n
\n
\n
\n

Sample Output 1

4\n2\n1\n2\n
\n

For the 1-st query, he can travel from the 1-st hotel to the 8-th hotel in 4 days, as follows:

\n
    \n
  • Day 1: Travel from the 1-st hotel to the 2-nd hotel. The distance traveled is 2.
  • \n
  • Day 2: Travel from the 2-nd hotel to the 4-th hotel. The distance traveled is 10.
  • \n
  • Day 3: Travel from the 4-th hotel to the 7-th hotel. The distance traveled is 6.
  • \n
  • Day 4: Travel from the 7-th hotel to the 8-th hotel. The distance traveled is 10.
  • \n
\n
\n
", "c_code": "int solution() {\n long N;\n long L;\n long Q;\n long x[100001];\n long i;\n long j;\n long k;\n long a;\n long b;\n long tmp;\n long move[100001];\n long move100[100001];\n long right;\n long ans[100001] = {0};\n\n scanf(\"%ld\", &N);\n for (i = 1; i <= N; i++) {\n scanf(\"%ld\", &x[i]);\n }\n scanf(\"%ld\", &L);\n\n right = 2;\n for (i = 1; i <= N; i++) {\n for (j = right; j <= N; j++) {\n if (x[j] - x[i] > L) {\n j--;\n break;\n }\n }\n if (j > N) {\n j = N;\n }\n move[i] = j;\n right = j;\n }\n\n for (i = 1; i <= N; i++) {\n k = i;\n for (j = 0; j < 100; j++) {\n if (k >= N) {\n break;\n }\n k = move[k];\n }\n if (j >= 100) {\n move100[i] = k;\n } else {\n move100[i] = -1;\n }\n }\n\n scanf(\"%ld\", &Q);\n for (i = 1; i <= Q; i++) {\n scanf(\"%ld %ld\", &a, &b);\n if (a > b) {\n tmp = a;\n a = b;\n b = tmp;\n }\n if (L == 1) {\n ans[i] = b - a;\n continue;\n }\n\n j = a;\n while (move100[j] != -1 && move100[j] < b) {\n j = move100[j];\n ans[i] += 100;\n }\n\n for (; j < b;) {\n j = move[j];\n ans[i]++;\n }\n }\n\n for (i = 1; i <= Q; i++) {\n printf(\"%ld\\n\", ans[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n v: [usize; n],\n l: usize,\n q: usize,\n qs: [(usize1, usize1); q]\n }\n let mut p = 1;\n while 1 << p < n - 1 {\n p += 1;\n }\n p += 1;\n let mut tab = vec![vec![0; p]; n];\n for i in 0..p {\n tab[n - 1][i] = n - 1;\n }\n let mut il = 0;\n let mut ir = 0;\n let mut d = 0;\n while il < n - 1 {\n if ir == n - 1 || d + v[ir + 1] - v[ir] > l {\n tab[il][0] = ir;\n d -= v[il + 1] - v[il];\n il += 1;\n } else {\n d += v[ir + 1] - v[ir];\n ir += 1;\n }\n }\n for i in (0..n - 1).rev() {\n for j in 1..p {\n tab[i][j] = tab[tab[i][j - 1]][j - 1];\n }\n }\n for &(a, b) in &qs {\n let a1 = min(a, b);\n let b1 = max(a, b);\n let mut day = 0;\n let mut x = a1;\n while x < b1 {\n if tab[x][0] >= b1 {\n day += 1;\n break;\n }\n for i in 0..p {\n if tab[x][i] >= b1 {\n day += 1 << (i - 1);\n x = tab[x][i - 1];\n break;\n }\n }\n }\n println!(\"{}\", day);\n }\n}", "difficulty": "medium"} {"problem_id": "0491", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given two positive integers A and B. Compare the magnitudes of these numbers.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ A, B ≤ 10^{100}
  • \n
  • Neither A nor B begins with a 0.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A\nB\n
\n
\n
\n
\n
\n

Output

Print GREATER if A>B, LESS if A<B and EQUAL if A=B.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

36\n24\n
\n
\n
\n
\n
\n

Sample Output 1

GREATER\n
\n

Since 36>24, print GREATER.

\n
\n
\n
\n
\n
\n

Sample Input 2

850\n3777\n
\n
\n
\n
\n
\n

Sample Output 2

LESS\n
\n
\n
\n
\n
\n
\n

Sample Input 3

9720246\n22516266\n
\n
\n
\n
\n
\n

Sample Output 3

LESS\n
\n
\n
\n
\n
\n
\n

Sample Input 4

123456789012345678901234567890\n234567890123456789012345678901\n
\n
\n
\n
\n
\n

Sample Output 4

LESS\n
\n
\n
", "c_code": "int solution() {\n int len1 = 0;\n int len2 = 0;\n char s1[101];\n char s2[101];\n\n scanf(\"%s %s\", s1, s2);\n\n while (s1[len1]) {\n len1++;\n }\n while (s2[len2]) {\n len2++;\n }\n if (len1 > len2 || (len1 == len2 && strcmp(s1, s2) > 0)) {\n printf(\"GREATER\\n\");\n } else if (strcmp(s1, s2) == 0) {\n printf(\"EQUAL\\n\");\n } else {\n printf(\"LESS\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let a = buf.trim().chars().collect::>();\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let b = buf.trim().chars().collect::>();\n if a.len() > b.len() {\n println!(\"GREATER\");\n } else if a.len() < b.len() {\n println!(\"LESS\");\n } else {\n for i in 0..a.len() {\n if a[i] > b[i] {\n println!(\"GREATER\");\n break;\n } else if a[i] < b[i] {\n println!(\"LESS\");\n break;\n } else {\n if i == 0 {\n println!(\"EQUAL\");\n }\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0492", "problem_description": "Omkar has received a message from Anton saying \"Your story for problem A is confusing. Just make a formal statement.\" Because of this, Omkar gives you an array $$$a = [a_1, a_2, \\ldots, a_n]$$$ of $$$n$$$ distinct integers. An array $$$b = [b_1, b_2, \\ldots, b_k]$$$ is called nice if for any two distinct elements $$$b_i, b_j$$$ of $$$b$$$, $$$|b_i-b_j|$$$ appears in $$$b$$$ at least once. In addition, all elements in $$$b$$$ must be distinct. Can you add several (maybe, $$$0$$$) integers to $$$a$$$ to create a nice array $$$b$$$ of size at most $$$300$$$? If $$$a$$$ is already nice, you don't have to add any elements.For example, array $$$[3, 6, 9]$$$ is nice, as $$$|6-3|=|9-6| = 3$$$, which appears in the array, and $$$|9-3| = 6$$$, which appears in the array, while array $$$[4, 2, 0, 6, 9]$$$ is not nice, as $$$|9-4| = 5$$$ is not present in the array.For integers $$$x$$$ and $$$y$$$, $$$|x-y| = x-y$$$ if $$$x > y$$$ and $$$|x-y| = y-x$$$ otherwise.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n int f = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] < 0) {\n f = 1;\n }\n }\n if (f) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n printf(\"101\\n\");\n for (int i = 0; i <= 100; i++) {\n printf(\"%d \", i);\n }\n }\n printf(\"\\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\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 for _ in 0..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() as usize;\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let mut a = Vec::with_capacity(n);\n a.extend(it);\n\n if a.iter().any(|&i| i < 0) {\n writeln!(&mut output, \"NO\").unwrap();\n } else {\n writeln!(&mut output, \"YES\").unwrap();\n writeln!(&mut output, \"101\").unwrap();\n for i in 0..=100 {\n write!(&mut output, \"{} \", i).unwrap();\n }\n writeln!(&mut output).unwrap();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0493", "problem_description": "Ashish and Vivek play a game on a matrix consisting of $$$n$$$ rows and $$$m$$$ columns, where they take turns claiming cells. Unclaimed cells are represented by $$$0$$$, while claimed cells are represented by $$$1$$$. The initial state of the matrix is given. There can be some claimed cells in the initial state.In each turn, a player must claim a cell. A cell may be claimed if it is unclaimed and does not share a row or column with any other already claimed cells. When a player is unable to make a move, he loses and the game ends.If Ashish and Vivek take turns to move and Ashish goes first, determine the winner of the game if both of them are playing optimally.Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int r;\n int c;\n scanf(\"%d %d\", &r, &c);\n int a[r][c];\n for (int i = 0; i < r; i++) {\n for (int j = 0; j < c; j++) {\n scanf(\"%d\", &a[i][j]);\n }\n }\n\n int ans = 0;\n for (int i = 0, k = 0; i < r && k < c;) {\n int x = -1;\n int y = -1;\n\n for (int j = 0; j < c; j++) {\n if (a[i][j] == 1) {\n x = 1;\n break;\n }\n }\n for (int j = 0; j < r; j++) {\n if (a[j][k] == 1) {\n y = 1;\n break;\n }\n }\n if (x == 1 && y == -1) {\n i++;\n }\n if (y == 1 && x == -1) {\n k++;\n }\n if (x == 1 && y == 1) {\n a[i][k] = 1;\n i++;\n k++;\n }\n if (x == -1 && y == -1) {\n ans++;\n i++;\n k++;\n }\n }\n if (ans % 2) {\n printf(\"Ashish\\n\");\n } else {\n printf(\"Vivek\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let cases = {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n\n input.trim().parse::().expect(\"invalid input\")\n };\n\n for _ in 0..cases {\n let (n, m) = {\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_iter = input\n .split_whitespace()\n .map(|x| x.parse::().expect(\"invalid input\"));\n (input_iter.next().unwrap(), input_iter.next().unwrap())\n };\n\n let mut rows = vec![false; n];\n let mut cols = vec![false; m];\n for row in rows.iter_mut() {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n\n for (col, val) in cols.iter_mut().zip(input.split_whitespace()) {\n if val != \"0\" {\n *col = true;\n *row = true;\n }\n }\n }\n\n let val = cmp::min(\n rows.iter().filter(|&&x| !x).count(),\n cols.iter().filter(|&&x| !x).count(),\n );\n\n if val % 2 == 0 {\n println!(\"Vivek\");\n } else {\n println!(\"Ashish\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0494", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Fennec and Snuke are playing a board game.

\n

On the board, there are N cells numbered 1 through N, and N-1 roads, each connecting two cells. Cell a_i is adjacent to Cell b_i through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.

\n

Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored.\nFennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell.\nMore specifically, each player performs the following action in her/his turn:

\n
    \n
  • Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.
  • \n
  • Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.
  • \n
\n

A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq a_i, b_i \\leq N
  • \n
  • The given graph is a tree.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 b_1\n:\na_{N-1} b_{N-1}\n
\n
\n
\n
\n
\n

Output

If Fennec wins, print Fennec; if Snuke wins, print Snuke.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7\n3 6\n1 2\n3 1\n7 4\n5 7\n1 4\n
\n
\n
\n
\n
\n

Sample Output 1

Fennec\n
\n

For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n1 4\n4 2\n2 3\n
\n
\n
\n
\n
\n

Sample Output 2

Snuke\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n int m = 0;\n int head[n * 2];\n int next[n * 2];\n int to[n * 2];\n int a;\n int b;\n int post[n];\n int count;\n bool visited[n];\n\n for (int i = 0; i < n; i++) {\n head[i] = -1;\n }\n for (int i = 0; i < n - 1; i++) {\n scanf(\"%d%d\", &a, &b);\n a--;\n b--;\n next[m] = head[a];\n head[a] = m;\n to[m] = b;\n m++;\n next[m] = head[b];\n head[b] = m;\n to[m] = a;\n m++;\n }\n\n for (int i = 0; i < n; i++) {\n visited[i] = false;\n }\n int costf[n];\n costf[0] = 0;\n visited[0] = true;\n count = 0;\n post[0] = 0;\n for (int i = 0; i < count + 1; i++) {\n for (int e = head[post[i]]; e != -1; e = next[e]) {\n if (visited[to[e]] == false) {\n visited[to[e]] = true;\n costf[to[e]] = costf[post[i]] + 1;\n count++;\n post[count] = to[e];\n }\n }\n }\n\n for (int i = 0; i < n; i++) {\n visited[i] = false;\n }\n int costs[n];\n costs[n - 1] = 0;\n visited[n - 1] = true;\n count = 0;\n post[0] = n - 1;\n for (int i = i = 0; i < count + 1; i++) {\n for (int e = head[post[i]]; e != -1; e = next[e]) {\n if (visited[to[e]] == false) {\n visited[to[e]] = true;\n costs[to[e]] = costs[post[i]] + 1;\n count++;\n post[count] = to[e];\n }\n }\n }\n\n int Fennec = 0;\n int Snuke = 0;\n for (int i = 0; i < n; i++) {\n if (costf[i] <= costs[i]) {\n Fennec++;\n } else {\n Snuke++;\n }\n }\n\n if (Fennec > Snuke) {\n printf(\"Fennec\\n\");\n } else {\n printf(\"Snuke\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut buf = String::new();\n stdin\n .read_line(&mut buf)\n .expect(\"ERROR Can't read array size\");\n let n: usize = buf.trim().parse().unwrap();\n let mut graph: Vec> = Vec::with_capacity(n);\n for _ in 0..n {\n graph.push(Vec::new());\n }\n for _ in 0..n - 1 {\n let mut buf = String::new();\n stdin\n .read_line(&mut buf)\n .expect(\"ERROR Can't read array size\");\n let v: Vec = buf.split_whitespace().map(|n| n.parse().unwrap()).collect();\n let a = v[0] - 1;\n let b = v[1] - 1;\n graph.get_mut(a).unwrap().push(b);\n graph.get_mut(b).unwrap().push(a);\n }\n let mut used: Vec = Vec::new();\n used.resize(n, false);\n let mut sa = 0;\n let mut sb = 0;\n\n let mut aque = VecDeque::new();\n let mut bque = VecDeque::new();\n aque.push_back(0);\n bque.push_back(n - 1);\n\n while !aque.is_empty() || !bque.is_empty() {\n let mut aque_new = VecDeque::new();\n while !aque.is_empty() {\n let tar = aque.pop_front().unwrap();\n if !used[tar] {\n used[tar] = true;\n sa += 1;\n for val in graph[tar].iter() {\n aque_new.push_back(*val);\n }\n }\n }\n aque.append(&mut aque_new);\n let mut bque_new = VecDeque::new();\n while !bque.is_empty() {\n let tar = bque.pop_front().unwrap();\n if !used[tar] {\n sb += 1;\n used[tar] = true;\n for val in graph[tar].iter() {\n bque_new.push_back(*val);\n }\n }\n }\n bque.append(&mut bque_new);\n }\n if sa > sb {\n println!(\"Fennec\");\n } else {\n println!(\"Snuke\");\n }\n}", "difficulty": "easy"} {"problem_id": "0495", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You have written N problems to hold programming contests.\nThe i-th problem will have a score of P_i points if used in a contest.

\n

With these problems, you would like to hold as many contests as possible under the following condition:

\n
    \n
  • A contest has three problems. The first problem has a score not greater than A points, the second has a score between A + 1 and B points (inclusive), and the third has a score not less than B + 1 points.
  • \n
\n

The same problem should not be used in multiple contests.\nAt most how many contests can be held?

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 100
  • \n
  • 1 \\leq P_i \\leq 20 (1 \\leq i \\leq N)
  • \n
  • 1 \\leq A < B < 20
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA B\nP_1 P_2 ... P_N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7\n5 15\n1 10 16 2 7 20 12\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Two contests can be held by putting the first, second, third problems and the fourth, fifth, sixth problems together.

\n
\n
\n
\n
\n
\n

Sample Input 2

8\n3 8\n5 5 5 10 10 10 15 20\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

No contest can be held, because there is no problem with a score of A = 3 or less.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n5 6\n5 6 10\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int a;\n int b;\n int p[101];\n int cnt1 = 0;\n int cnt2 = 0;\n int cnt3 = 0;\n int ans = 0;\n ;\n\n scanf(\"%d%d%d\", &n, &a, &b);\n\n for (int cnt = 1; cnt <= n; cnt++) {\n scanf(\"%d\", &p[cnt]);\n if (p[cnt] <= a) {\n cnt1++;\n } else if (p[cnt] < b || p[cnt] <= b) {\n cnt2++;\n } else if (p[cnt] > b) {\n cnt3++;\n }\n }\n ans = cnt1;\n if (ans > cnt2) {\n ans = cnt2;\n }\n if (ans > cnt3) {\n ans = cnt3;\n }\n\n printf(\"%d\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let handle = std::io::stdin();\n let _n: usize = {\n handle.read_line(&mut buf).unwrap();\n let tmp = buf.trim().parse().unwrap();\n buf.clear();\n tmp\n };\n let info: Vec = {\n handle.read_line(&mut buf).unwrap();\n let tmp = buf.split_whitespace().map(|a| a.parse().unwrap()).collect();\n buf.clear();\n tmp\n };\n let mut ps: Vec = {\n handle.read_line(&mut buf).unwrap();\n let tmp = buf.split_whitespace().map(|a| a.parse().unwrap()).collect();\n buf.clear();\n tmp\n };\n ps.sort();\n let one = ps.iter().filter(|a| a <= &&info[0]).count();\n let two = ps\n .iter()\n .filter(|a| &&info[0] < a && a <= &&info[1])\n .count();\n let three = ps.iter().filter(|a| &&info[1] < a).count();\n println!(\"{}\", min(min(one, two), three));\n}", "difficulty": "hard"} {"problem_id": "0496", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

\n

A Hitachi string is a concatenation of one or more copies of the string hi.

\n

For example, hi and hihi are Hitachi strings, while ha and hii are not.

\n

Given a string S, determine whether S is a Hitachi string.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • The length of S is between 1 and 10 (inclusive).
  • \n
  • S is a string consisting of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

\n

If S is a Hitachi string, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

hihi\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

hihi is the concatenation of two copies of hi, so it is a Hitachi string.

\n
\n
\n
\n
\n
\n

Sample Input 2

hi\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 3

ha\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution(void) {\n char s[10];\n int c = 1;\n int i = 0;\n\n scanf(\"%s\", s);\n while (s[i]) {\n if (!((i % 2 == 0 && s[i] == 'h') || (i % 2 == 1 && s[i] == 'i'))) {\n c = 0;\n break;\n }\n i++;\n }\n if (c == 1 && i % 2 == 0) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut target_str = String::new();\n io::stdin().read_line(&mut target_str).unwrap();\n let target_str = target_str.trim();\n\n let mut count: usize = 0;\n\n loop {\n match target_str.chars().nth(count) {\n Some('h') => match target_str.chars().nth(count + 1) {\n Some('i') => {\n count += 2;\n }\n _ => {\n println!(\"No\");\n break;\n }\n },\n None => {\n println!(\"Yes\");\n break;\n }\n _ => {\n println!(\"No\");\n break;\n }\n };\n }\n}", "difficulty": "easy"} {"problem_id": "0497", "problem_description": "Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings \"pop\", \"noon\", \"x\", and \"kkkkkk\" are palindromes, while strings \"moon\", \"tv\", and \"abab\" are not. An empty string is also a palindrome.Gildong loves this concept so much, so he wants to play with it. He has $$$n$$$ distinct strings of equal length $$$m$$$. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.", "c_code": "int solution() {\n char re[110][55];\n int tt[110] = {0};\n int y[110][2] = {0};\n int n;\n int m;\n int k = 0;\n scanf(\"%d %d%*c\", &n, &m);\n for (int i = 1; i <= n; i++) {\n scanf(\"%s\", re[i]);\n }\n for (int i = 1; i <= n - 1; i++) {\n if (tt[i] == 0) {\n for (int j = i + 1; j <= n; j++) {\n if (tt[j] == 0) {\n int flag = 1;\n for (int x = 0, y = m - 1; x <= m - 1 && y >= 0; x++, y--) {\n if (re[i][x] != re[j][y]) {\n flag = 0;\n break;\n }\n }\n if (flag == 1) {\n tt[i] = tt[j] = 1;\n y[k][0] = i;\n y[k][1] = j;\n k++;\n break;\n }\n }\n }\n }\n }\n for (int i = 1; i <= n; i++) {\n if (tt[i] == 0) {\n int count = 1;\n for (int b = 0, c = m - 1; b <= (m - 1) / 2; b++, c--) {\n if (re[i][b] != re[i][c]) {\n count = 0;\n }\n }\n if (count == 1) {\n y[k][0] = y[k][1] = i;\n k++;\n break;\n }\n }\n }\n int ans = 0;\n for (int i = 0; i <= k - 1; i++) {\n if (y[i][0] != y[i][1]) {\n ans += 2 * m;\n } else {\n ans += m;\n }\n }\n printf(\"%d\\n\", ans);\n for (int i = 0; i <= k - 1; i++) {\n printf(\"%s\", re[y[i][0]]);\n }\n for (int i = k - 1; i >= 0; i--) {\n if (y[i][0] == y[i][1]) {\n continue;\n }\n printf(\"%s\", re[y[i][1]]);\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let mut pieces = line.split_whitespace();\n let n: u32 = pieces.next().unwrap().parse().unwrap();\n let _m: u32 = pieces.next().unwrap().parse().unwrap();\n let mut strings = Vec::new();\n for _ in 0..n {\n let mut string = String::new();\n io::stdin().read_line(&mut string).unwrap();\n string = string.trim().to_string();\n strings.push(string);\n }\n\n let mut pairs = Vec::new();\n let mut palindrome_string: Option = None;\n\n while !strings.is_empty() {\n let string = strings.pop().unwrap();\n let string_rev: String = string.chars().rev().collect();\n match strings.iter().position(|s| *s == string_rev) {\n Some(partner_idx) => {\n pairs.push((string, string_rev));\n strings.remove(partner_idx);\n }\n None => {\n if string == string_rev {\n palindrome_string = Some(string);\n }\n }\n }\n }\n\n let mut palindrome = String::new();\n for pair_first in pairs.iter().map(|(first, _)| first) {\n palindrome.push_str(pair_first);\n }\n if palindrome_string.is_some() {\n palindrome.push_str(&palindrome_string.unwrap());\n }\n for pair_second in pairs.iter().rev().map(|(_, second)| second) {\n palindrome.push_str(pair_second);\n }\n\n println!(\"{}\", palindrome.len());\n println!(\"{}\", palindrome);\n}", "difficulty": "hard"} {"problem_id": "0498", "problem_description": "A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000011223388 are not.You are given a string $$$s$$$ of length $$$n$$$, consisting of digits.In one operation you can delete any character from string $$$s$$$. For example, it is possible to obtain strings 112, 111 or 121 from string 1121.You need to determine whether there is such a sequence of operations (possibly empty), after which the string $$$s$$$ becomes a telephone number.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int a[t];\n char s[t][101];\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &a[i]);\n getchar();\n scanf(\"%s\", s[i]);\n }\n int flag = 1;\n for (int i = 0; i < t; i++) {\n flag = 1;\n for (int j = 0; j < a[i] - 10; j++) {\n if (s[i][j] == '8') {\n printf(\"YES\\n\");\n flag = 0;\n break;\n }\n }\n if (flag == 1) {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n use std::io::{self, Read};\n\n let mut b = String::new();\n io::stdin().read_to_string(&mut b).unwrap();\n let a = b.split_whitespace().skip(1).collect::>();\n\n for j in a.as_slice().chunks(2) {\n let i = j[1];\n\n match i.find('8') {\n Some(idx) => {\n let l = i.len() - idx;\n if l >= 11 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n _ => {\n println!(\"NO\");\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0499", "problem_description": "There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \\ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle.", "c_code": "int solution() {\n int arr[100000][3];\n int max = 0;\n int n = 0;\n int i = 0;\n int j = 0;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n for (j = 0; j < 2; j++) {\n scanf(\"%d\", &arr[i][j]);\n }\n }\n for (i = 0; i < n; i++) {\n arr[i][2] = arr[i][0] + arr[i][1];\n }\n max = arr[0][2];\n for (i = 0; i < n; i++) {\n if (arr[i][2] > max) {\n max = arr[i][2];\n }\n }\n printf(\"%d\", max);\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] as usize;\n let mut bmin = 0;\n for i in 0..n {\n let x0 = arr[1 + i * 2];\n let y0 = arr[1 + i * 2 + 1];\n let b = y0 + x0;\n bmin = cmp::max(bmin, b);\n }\n\n println!(\"{}\", bmin);\n}", "difficulty": "medium"} {"problem_id": "0500", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There are N Snuke Cats numbered 1, 2, \\ldots, N, where N is even.

\n

Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written.

\n

Recently, they learned the operation called xor (exclusive OR).

\n
\nWhat is xor?\n

\nFor n non-negative integers x_1, x_2, \\ldots, x_n, their xor, x_1~\\textrm{xor}~x_2~\\textrm{xor}~\\ldots~\\textrm{xor}~x_n is defined as follows:\n

    \n
  • When x_1~\\textrm{xor}~x_2~\\textrm{xor}~\\ldots~\\textrm{xor}~x_n is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if the number of integers among x_1, x_2, \\ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.
  • \n
\nFor example, 3~\\textrm{xor}~5 = 6.\n

\n
\n

They wanted to use this operation quickly, so each of them calculated the xor of the integers written on their scarfs except his scarf.

\n

We know that the xor calculated by Snuke Cat i, that is, the xor of the integers written on the scarfs except the scarf of Snuke Cat i is a_i.\nUsing this information, restore the integer written on the scarf of each Snuke Cat.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 200000
  • \n
  • N is even.
  • \n
  • 0 \\leq a_i \\leq 10^9
  • \n
  • There exists a combination of integers on the scarfs that is consistent with the given information.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 \\ldots a_N\n
\n
\n
\n
\n
\n

Output

Print a line containing N integers separated with space.

\n

The i-th of the integers from the left should represent the integer written on the scarf of Snuke Cat i.

\n

If there are multiple possible solutions, you may print any of them.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n20 11 9 24\n
\n
\n
\n
\n
\n

Sample Output 1

26 5 7 22\n
\n
    \n
  • 5~\\textrm{xor}~7~\\textrm{xor}~22 = 20
  • \n
  • 26~\\textrm{xor}~7~\\textrm{xor}~22 = 11
  • \n
  • 26~\\textrm{xor}~5~\\textrm{xor}~22 = 9
  • \n
  • 26~\\textrm{xor}~5~\\textrm{xor}~7 = 24
  • \n
\n

Thus, this output is consistent with the given information.

\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n int xa = 0;\n int a[200000] = {0};\n int i = 0;\n\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n xa ^= a[i];\n }\n\n for (i = 0; i < n; i++) {\n printf(\"%d \", a[i] ^ xa);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let _n = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n buf.split_whitespace()\n .next()\n .unwrap()\n .parse::()\n .unwrap()\n };\n let a: Vec = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n buf.split_whitespace()\n .filter_map(|x| x.parse().ok())\n .collect()\n };\n\n let all_xor = a.iter().fold(0_i64, |l, r| l ^ *r);\n for x in a {\n println!(\"{}\", all_xor ^ x);\n }\n}", "difficulty": "medium"} {"problem_id": "0501", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

\n

Given is a tree G with N vertices.\nThe vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.

\n

Consider painting the edges in G with some number of colors.\nWe want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.

\n

Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 2 \\le N \\le 10^5
  • \n
  • 1 \\le a_i \\lt b_i \\le N
  • \n
  • All values in input are integers.
  • \n
  • The given graph is a tree.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\na_1 b_1\na_2 b_2\n\\vdots\na_{N-1} b_{N-1}\n
\n
\n
\n
\n
\n

Output

\n

Print N lines.

\n

The first line should contain K, the number of colors used.

\n

The (i+1)-th line (1 \\le i \\le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \\le c_i \\le K must hold.

\n

If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 2\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n1\n2\n
\n
\n
\n
\n
\n
\n

Sample Input 2

8\n1 2\n2 3\n2 4\n2 5\n4 7\n5 6\n6 8\n
\n
\n
\n
\n
\n

Sample Output 2

4\n1\n2\n3\n4\n1\n1\n2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6\n1 2\n1 3\n1 4\n1 5\n1 6\n
\n
\n
\n
\n
\n

Sample Output 3

5\n1\n2\n3\n4\n5\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int a[100001];\n int b[100001];\n scanf(\"%d\", &N);\n for (i = 1; i <= N - 1; i++) {\n scanf(\"%d %d\", &(a[i]), &(b[i]));\n }\n\n edge e[100001];\n list *inc[100001] = {};\n list l[200001];\n for (i = 1; i <= N - 1; i++) {\n e[i].end[0] = a[i];\n e[i].end[1] = b[i];\n e[i].color = -1;\n l[(i - 1) * 2].f = &(e[i]);\n l[((i - 1) * 2) + 1].f = &(e[i]);\n l[(i - 1) * 2].next = inc[a[i]];\n l[((i - 1) * 2) + 1].next = inc[b[i]];\n inc[a[i]] = &(l[(i - 1) * 2]);\n inc[b[i]] = &(l[((i - 1) * 2) + 1]);\n }\n\n int k;\n int K = 0;\n int flag[100001] = {0, -1};\n int q[100001] = {1};\n int head;\n int tail;\n list *p;\n for (head = 0, tail = 1; head < tail; head++) {\n k = (flag[q[head]] == 1) ? 2 : 1;\n for (p = inc[q[head]]; p != NULL; p = p->next) {\n i = (p->f->end[0] == q[head]) ? p->f->end[1] : p->f->end[0];\n if (flag[i] == 0) {\n if (k == flag[q[head]]) {\n k++;\n }\n flag[i] = k;\n p->f->color = k++;\n q[tail++] = i;\n }\n }\n if (k - 1 > K) {\n K = k - 1;\n }\n }\n\n printf(\"%d\\n\", K);\n for (i = 1; i <= N - 1; i++) {\n printf(\"%d\\n\", e[i].color);\n }\n fflush(stdout);\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 (a, b): (Vec, Vec) = {\n let (mut a, mut b) = (vec![], vec![]);\n for _ in 0..(N - 1) {\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 }\n (a, b)\n };\n\n let G = {\n let mut G = vec![std::collections::HashSet::new(); N + 1];\n for i in 0..(N - 1) {\n G[a[i]].insert((b[i], i));\n G[b[i]].insert((a[i], i));\n }\n G\n };\n\n let mut q = std::collections::VecDeque::new();\n q.push_back(1);\n let mut prev = vec![0; N + 1];\n let mut res = vec![0; N - 1];\n let mut K = 0;\n\n while !q.is_empty() {\n let i = q.pop_front().unwrap();\n\n let mut z = 1;\n for &(j, k) in &G[i] {\n if prev[j] == 0 && j != 1 {\n if z == prev[i] {\n z += 1;\n }\n\n res[k] = z;\n prev[j] = z;\n z += 1;\n q.push_back(j);\n }\n }\n\n K = std::cmp::max(z - 1, K);\n }\n\n println!(\"{}\", K);\n\n println!(\n \"{}\",\n res.iter()\n .map(|&r| r.to_string())\n .collect::>()\n .join(\"\\n\")\n );\n}", "difficulty": "medium"} {"problem_id": "0502", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Alice, Bob and Charlie are playing Card Game for Three, as below:

\n
    \n
  • At first, each of the three players has a deck consisting of some number of cards. Each card has a letter a, b or c written on it. The orders of the cards in the decks cannot be rearranged.
  • \n
  • The players take turns. Alice goes first.
  • \n
  • If the current player's deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says a, Alice takes the next turn.)
  • \n
  • If the current player's deck is empty, the game ends and the current player wins the game.
  • \n
\n

You are given the initial decks of the players.\nMore specifically, you are given three strings S_A, S_B and S_C. The i-th (1≦i≦|S_A|) letter in S_A is the letter on the i-th card in Alice's initial deck. S_B and S_C describes Bob's and Charlie's initial decks in the same way.

\n

Determine the winner of the game.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦|S_A|≦100
  • \n
  • 1≦|S_B|≦100
  • \n
  • 1≦|S_C|≦100
  • \n
  • Each letter in S_A, S_B, S_C is a, b or c.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
S_A\nS_B\nS_C\n
\n
\n
\n
\n
\n

Output

If Alice will win, print A. If Bob will win, print B. If Charlie will win, print C.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

aca\naccc\nca\n
\n
\n
\n
\n
\n

Sample Output 1

A\n
\n

The game will progress as below:

\n
    \n
  • Alice discards the top card in her deck, a. Alice takes the next turn.
  • \n
  • Alice discards the top card in her deck, c. Charlie takes the next turn.
  • \n
  • Charlie discards the top card in his deck, c. Charlie takes the next turn.
  • \n
  • Charlie discards the top card in his deck, a. Alice takes the next turn.
  • \n
  • Alice discards the top card in her deck, a. Alice takes the next turn.
  • \n
  • Alice's deck is empty. The game ends and Alice wins the game.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

abcb\naacb\nbccc\n
\n
\n
\n
\n
\n

Sample Output 2

C\n
\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int c = 0;\n char ary_a[128];\n char ary_b[128];\n char ary_c[128];\n char turn = 'a';\n\n fgets(ary_a, sizeof(ary_a), stdin);\n fgets(ary_b, sizeof(ary_b), stdin);\n fgets(ary_c, sizeof(ary_c), stdin);\n\n while (1) {\n\n if (turn == 'a') {\n turn = ary_a[a];\n if (isspace(ary_a[a]) != 0) {\n puts(\"A\");\n return 0;\n }\n a++;\n } else if (turn == 'b') {\n turn = ary_b[b];\n if (isspace(ary_b[b]) != 0) {\n puts(\"B\");\n return 0;\n }\n b++;\n } else {\n turn = ary_c[c];\n if (isspace(ary_c[c]) != 0) {\n puts(\"C\");\n return 0;\n }\n c++;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let a = String::from(s.trim());\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let b = String::from(s.trim());\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let c = String::from(s.trim());\n let mut s = [a, b, c];\n\n let mut next = 0;\n while !s[next].is_empty() {\n next = match s[next].remove(0) {\n 'a' => 0,\n 'b' => 1,\n _ => 2,\n };\n }\n\n println!(\n \"{}\",\n match next {\n 0 => 'A',\n 1 => 'B',\n _ => 'C',\n },\n );\n}", "difficulty": "easy"} {"problem_id": "0503", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Snuke has one biscuit and zero Japanese yen (the currency) in his pocket.\nHe will perform the following operations exactly K times in total, in the order he likes:

\n
    \n
  • Hit his pocket, which magically increases the number of biscuits by one.
  • \n
  • Exchange A biscuits to 1 yen.
  • \n
  • Exchange 1 yen to B biscuits.
  • \n
\n

Find the maximum possible number of biscuits in Snuke's pocket after K operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K,A,B \\leq 10^9
  • \n
  • K,A and B are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K A B\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number of biscuits in Snuke's pocket after K operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 2 6\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

The number of biscuits in Snuke's pocket after K operations is maximized as follows:

\n
    \n
  • Hit his pocket. Now he has 2 biscuits and 0 yen.
  • \n
  • Exchange 2 biscuits to 1 yen. his pocket. Now he has 0 biscuits and 1 yen.
  • \n
  • Hit his pocket. Now he has 1 biscuits and 1 yen.
  • \n
  • Exchange 1 yen to 6 biscuits. his pocket. Now he has 7 biscuits and 0 yen.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

7 3 4\n
\n
\n
\n
\n
\n

Sample Output 2

8\n
\n
\n
\n
\n
\n
\n

Sample Input 3

314159265 35897932 384626433\n
\n
\n
\n
\n
\n

Sample Output 3

48518828981938099\n
\n
\n
", "c_code": "int solution(void) {\n long K = 0;\n long A = 0;\n long B = 0;\n scanf(\"%ld %ld %ld\", &K, &A, &B);\n if (K <= A) {\n printf(\"%ld\\n\", K + 1);\n } else {\n if (B - A <= 2) {\n printf(\"%ld\\n\", K + 1);\n } else {\n long count = 0;\n long hit = 0;\n count = A;\n hit = A - 1;\n K -= hit;\n if (K % 2 == 0) {\n count += (K / 2) * (B - A);\n } else {\n count += (K / 2) * (B - A);\n count++;\n }\n printf(\"%ld\\n\", count);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let vs: Vec = s\n .split_whitespace()\n .map(|token| token.parse().ok().unwrap())\n .collect();\n let k = vs[0];\n let a = vs[1];\n let b = vs[2];\n\n if b <= a + 2 {\n println!(\"{}\", k + 1);\n return;\n }\n\n let mut n: u64 = 1;\n let mut yen: u64 = 0;\n\n for i in (0..k).rev() {\n if i <= yen {\n if yen > 0 {\n n += b;\n yen -= 1;\n } else {\n n += 1;\n }\n } else if n >= a {\n n -= a;\n yen += 1;\n } else if yen > 0 {\n n += b;\n yen -= 1;\n } else {\n n += 1;\n }\n }\n println!(\"{}\", n);\n}", "difficulty": "medium"} {"problem_id": "0504", "problem_description": "You are given $$$n$$$ words of equal length $$$m$$$, consisting of lowercase Latin alphabet letters. The $$$i$$$-th word is denoted $$$s_i$$$.In one move you can choose any position in any single word and change the letter at that position to the previous or next letter in alphabetical order. For example: you can change 'e' to 'd' or to 'f'; 'a' can only be changed to 'b'; 'z' can only be changed to 'y'. The difference between two words is the minimum number of moves required to make them equal. For example, the difference between \"best\" and \"cost\" is $$$1 + 10 + 0 + 0 = 11$$$.Find the minimum difference of $$$s_i$$$ and $$$s_j$$$ such that $$$(i < j)$$$. In other words, find the minimum difference over all possible pairs of the $$$n$$$ words.", "c_code": "int solution() {\n int test_cases = 0;\n scanf(\"%d\", &test_cases);\n\n while (test_cases--) {\n int num_strings = 0;\n scanf(\"%d\", &num_strings);\n int string_len = 0;\n scanf(\"%d\", &string_len);\n char array[num_strings][string_len];\n for (int i = 0; i < num_strings; i++) {\n scanf(\"%s\", array[i]);\n }\n\n int answer = INT_MAX;\n for (int i = 0; i < num_strings; i++) {\n for (int j = i + 1; j < num_strings; j++) {\n int score = 0;\n for (int k = 0; k < string_len; k++) {\n if (array[i][k] == array[j][k]) {\n continue;\n }\n score = score + abs(array[i][k] - array[j][k]);\n }\n if (answer > score) {\n answer = score;\n }\n }\n }\n printf(\"%d\\n\", answer);\n }\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();\n let t = input.next().unwrap().parse::().unwrap();\n for _ in 0..t {\n let n = input.next().unwrap().parse::().unwrap();\n let m = input.next().unwrap().parse::().unwrap();\n let s: Vec<_> = input.by_ref().map(str::as_bytes).take(n).collect();\n let mut min = u32::MAX;\n for i in 1..n {\n for j in 0..i {\n let mut sum = 0u32;\n for k in 0..m {\n let a = s[i][k];\n let b = s[j][k];\n if a > b {\n sum += (a - b) as u32;\n } else {\n sum += (b - a) as u32;\n }\n }\n min = min.min(sum);\n }\n }\n writeln!(output, \"{min}\").ok();\n }\n print!(\"{output}\");\n}", "difficulty": "hard"} {"problem_id": "0505", "problem_description": "Alice and Bob are playing a game with strings. There will be $$$t$$$ rounds in the game. In each round, there will be a string $$$s$$$ consisting of lowercase English letters. Alice moves first and both the players take alternate turns. Alice is allowed to remove any substring of even length (possibly empty) and Bob is allowed to remove any substring of odd length from $$$s$$$.More formally, if there was a string $$$s = s_1s_2 \\ldots s_k$$$ the player can choose a substring $$$s_ls_{l+1} \\ldots s_{r-1}s_r$$$ with length of corresponding parity and remove it. After that the string will become $$$s = s_1 \\ldots s_{l-1}s_{r+1} \\ldots s_k$$$.After the string becomes empty, the round ends and each player calculates his/her score for this round. The score of a player is the sum of values of all characters removed by him/her. The value of $$$\\texttt{a}$$$ is $$$1$$$, the value of $$$\\texttt{b}$$$ is $$$2$$$, the value of $$$\\texttt{c}$$$ is $$$3$$$, $$$\\ldots$$$, and the value of $$$\\texttt{z}$$$ is $$$26$$$. The player with higher score wins the round. For each round, determine the winner and the difference between winner's and loser's scores. Assume that both players play optimally to maximize their score. It can be proved that a draw is impossible.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char str[200001];\n scanf(\"%s\", str);\n int l = strlen(str);\n if (l == 1) {\n printf(\"Bob %d\\n\", (str[0] - 96));\n continue;\n }\n int sum = 0;\n for (int i = 0; i < l; i++) {\n sum += (str[i] - 96);\n }\n if (!(l & 1)) {\n printf(\"Alice %d\\n\", sum);\n continue;\n }\n if (str[0] >= str[l - 1]) {\n printf(\"Alice %d\\n\", (sum - (2 * (str[l - 1] - 96))));\n } else {\n printf(\"Alice %d\\n\", (sum - (2 * (str[0] - 96))));\n }\n }\n}", "rust_code": "fn solution() {\n let alphabet = \"abcdefghijklmnopqrstuvwxyz\";\n\n let mut t = String::new();\n stdin().read_line(&mut t);\n let t = t.trim().parse::().unwrap();\n\n for _ in 0..t {\n let mut buf = String::new();\n stdin().read_line(&mut buf);\n\n buf = buf.trim().to_string();\n if buf.len() < 2 {\n println!(\"Bob {}\", alphabet.find(&buf).unwrap() + 1);\n continue;\n }\n\n let mut alicesum = 0;\n let mut first = 0;\n let mut last = 0;\n let mut j = 0;\n for i in buf.chars() {\n if j == 0 {\n first = alphabet.find(i).unwrap() + 1;\n }\n if j == buf.len() - 1 {\n last = alphabet.find(i).unwrap() + 1;\n }\n\n alicesum += alphabet.find(i).unwrap() + 1;\n j += 1;\n }\n\n if buf.len().is_multiple_of(2) {\n println!(\"Alice {}\", alicesum);\n } else {\n if first > last {\n println!(\"Alice {}\", alicesum - last * 2)\n } else {\n println!(\"Alice {}\", alicesum - first * 2)\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0506", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

In some other world, today is Christmas.

\n

Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:

\n
    \n
  • A level-0 burger is a patty.
  • \n
  • A level-L burger (L \\geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom.
  • \n
\n

For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.

\n

The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 50
  • \n
  • 1 \\leq X \\leq ( the total number of layers in a level-N burger )
  • \n
  • N and X are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N X\n
\n
\n
\n
\n
\n

Output

Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 7\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

There are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB).

\n
\n
\n
\n
\n
\n

Sample Input 2

1 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

The bottom-most layer of a level-1 burger is a bun.

\n
\n
\n
\n
\n
\n

Sample Input 3

50 4321098765432109\n
\n
\n
\n
\n
\n

Sample Output 3

2160549382716056\n
\n

A level-50 burger is rather thick, to the extent that the number of its layers does not fit into a 32-bit integer.

\n
\n
", "c_code": "int solution(void) {\n\n long N;\n long X;\n long sou[51];\n long patty[51];\n long ans = 0;\n scanf(\"%ld %ld\", &N, &X);\n sou[0] = 1;\n patty[0] = 1;\n for (int i = 1; i <= N; i++) {\n sou[i] = sou[i - 1] * 2 + 3;\n patty[i] = patty[i - 1] * 2 + 1;\n }\n\n int flag = 0;\n int i = 0;\n for (i = N; i >= 0 && flag == 0; i--) {\n\n if (X == 1) {\n flag = 1;\n if (i == 0) {\n ans += 1;\n } else {\n ans += 0;\n }\n } else if (1 < X && X < sou[i] / 2 + 1) {\n X -= 1;\n } else if (X == sou[i] / 2 + 1) {\n ans += patty[i - 1] + 1;\n flag = 1;\n } else if (sou[i] / 2 + 1 < X && X < sou[i]) {\n X -= sou[i] / 2 + 1;\n ans += patty[i - 1] + 1;\n } else if (X == sou[i]) {\n flag = 1;\n ans += patty[i];\n }\n }\n printf(\"%ld\\n\", ans);\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n x: usize\n }\n\n let mut v: Vec<(usize, usize)> = vec![(1, 1)];\n for i in 0..n {\n let (iall, ipat) = v[i];\n let all = 3 + 2 * iall;\n let pat = 1 + 2 * ipat;\n v.push((all, pat));\n }\n let mut sum = 0;\n let mut queue: Vec<(usize, usize)> = vec![(n, x)];\n while let Some((level, eat)) = queue.pop() {\n if level == 0 && eat == 1 {\n sum += 1;\n continue;\n }\n if eat <= level {\n continue;\n }\n let (all, pat) = v[level];\n if eat == all {\n sum += pat;\n continue;\n }\n let mid = all / 2;\n match eat.cmp(&(mid + 1)) {\n std::cmp::Ordering::Less => {\n queue.push((level - 1, eat - 1));\n }\n std::cmp::Ordering::Equal => {\n let (_, pat) = v[level - 1];\n sum += pat + 1;\n }\n std::cmp::Ordering::Greater => {\n let (_, pat) = v[level - 1];\n sum += pat + 1;\n queue.push((level - 1, eat - mid - 1));\n }\n }\n }\n println!(\"{}\", sum);\n}", "difficulty": "medium"} {"problem_id": "0507", "problem_description": "You might have remembered Theatre square from the problem 1A. Now it's finally getting repaved.The square still has a rectangular shape of $$$n \\times m$$$ meters. However, the picture is about to get more complicated now. Let $$$a_{i,j}$$$ be the $$$j$$$-th square in the $$$i$$$-th row of the pavement.You are given the picture of the squares: if $$$a_{i,j} = $$$ \"*\", then the $$$j$$$-th square in the $$$i$$$-th row should be black; if $$$a_{i,j} = $$$ \".\", then the $$$j$$$-th square in the $$$i$$$-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles: $$$1 \\times 1$$$ tiles — each tile costs $$$x$$$ burles and covers exactly $$$1$$$ square; $$$1 \\times 2$$$ tiles — each tile costs $$$y$$$ burles and covers exactly $$$2$$$ adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into $$$1 \\times 1$$$ tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.What is the smallest total price of the tiles needed to cover all the white squares?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n int m;\n int x;\n int y;\n char *a = NULL;\n long long cost = 0;\n scanf(\"%d%d%d%d\", &n, &m, &x, &y);\n a = (char *)calloc(m + 4, sizeof(char));\n for (int j = 0; j < n; j++) {\n scanf(\"%s\", a);\n for (int k = 0; k < m; k++) {\n if (k < m - 1 && a[k] == '.' && a[k + 1] == '.') {\n int min = (2 * x) < y ? (2 * x) : y;\n cost += min;\n k++;\n } else if (k <= m - 1 && a[k] == '.') {\n cost += x;\n }\n }\n }\n printf(\"%lld\\n\", cost);\n free(a);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, x, y) = (xs[0], xs[2], xs[3]);\n let mut css: Vec> = Vec::new();\n for _ in 0..n {\n let mut s: Vec = lines.next().unwrap().unwrap().chars().collect();\n s.push('*');\n let mut cs: Vec = Vec::new();\n let mut count: usize = 0;\n for c in s {\n if c == '.' {\n count += 1;\n } else if count > 0 {\n cs.push(count);\n count = 0;\n }\n }\n css.push(cs);\n }\n let mut ans: usize = 0;\n for cs in css {\n for c in cs {\n if x * 2 < y {\n ans += x * c;\n } else {\n ans += y * (c / 2) + x * (c % 2);\n }\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0508", "problem_description": "You have $$$n$$$ rectangular wooden blocks, which are numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th block is $$$1$$$ unit high and $$$\\lceil \\frac{i}{2} \\rceil$$$ units long.Here, $$$\\lceil \\frac{x}{2} \\rceil$$$ denotes the result of division of $$$x$$$ by $$$2$$$, rounded up. For example, $$$\\lceil \\frac{4}{2} \\rceil = 2$$$ and $$$\\lceil \\frac{5}{2} \\rceil = \\lceil 2.5 \\rceil = 3$$$.For example, if $$$n=5$$$, then the blocks have the following sizes: $$$1 \\times 1$$$, $$$1 \\times 1$$$, $$$1 \\times 2$$$, $$$1 \\times 2$$$, $$$1 \\times 3$$$. The available blocks for $$$n=5$$$ Find the maximum possible side length of a square you can create using these blocks, without rotating any of them. Note that you don't have to use all of the blocks. One of the ways to create $$$3 \\times 3$$$ square using blocks $$$1$$$ through $$$5$$$", "c_code": "int solution() {\n long long t = 0;\n long long n = 0;\n\n scanf(\"%lld\", &t);\n\n while (t--) {\n scanf(\"%lld\", &n);\n printf(\"%lld \\n\", (long long)(n + 1) / 2);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n std::io::stdin()\n .lines()\n .skip(1)\n .flatten()\n .flat_map(|s| s.parse::())\n .for_each(|n| {\n writeln!(buf, \"{}\", n.wrapping_add(1).wrapping_shr(1)).unwrap_or_default();\n });\n}", "difficulty": "hard"} {"problem_id": "0509", "problem_description": "You are given an array $$$a$$$ of $$$n$$$ positive integers. Determine if, by rearranging the elements, you can make the array strictly increasing. In other words, determine if it is possible to rearrange the elements such that $$$a_1 < a_2 < \\dots < a_n$$$ holds.", "c_code": "int solution() {\n int ccount = 0;\n int num2 = 0;\n\n scanf(\"%d\", &ccount);\n for (int i = 0; i < ccount; ++i) {\n\n scanf(\"%d\", &num2);\n int arr[num2];\n for (int j = 0; j < num2; ++j) {\n scanf(\"%d\", &arr[j]);\n }\n\n for (int i = 0; i < num2 - 1; i++) {\n for (int j = 0; j < num2 - 1 - i; j++) {\n int temp = 0;\n if (arr[j] > arr[j + 1]) {\n temp = arr[j];\n arr[j] = arr[j + 1];\n arr[j + 1] = temp;\n }\n }\n }\n int index_temp = 0;\n for (index_temp = 0; index_temp < num2 - 1; ++index_temp) {\n if (arr[index_temp] == arr[index_temp + 1]) {\n printf(\"NO\\n\");\n break;\n }\n }\n if (index_temp == num2 - 1) {\n printf(\"YES\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n std::io::stdin()\n .lines()\n .skip(2)\n .step_by(2)\n .flatten()\n .for_each(|a| {\n let mut hs = std::collections::HashSet::with_capacity(100_000);\n if a.split_ascii_whitespace()\n .try_fold((), |_a, s| hs.insert(s).then_some(()))\n == Some(())\n {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n };\n });\n}", "difficulty": "hard"} {"problem_id": "0510", "problem_description": "The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of $$$n$$$ haybale piles on the farm. The $$$i$$$-th pile contains $$$a_i$$$ haybales. However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$) such that $$$|i-j|=1$$$ and $$$a_i>0$$$ and apply $$$a_i = a_i - 1$$$, $$$a_j = a_j + 1$$$. She may also decide to not do anything on some days because she is lazy.Bessie wants to maximize the number of haybales in pile $$$1$$$ (i.e. to maximize $$$a_1$$$), and she only has $$$d$$$ days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile $$$1$$$ if she acts optimally!", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int d;\n int j = 1;\n scanf(\"%d %d\", &n, &d);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n while (1) {\n if (n == 1) {\n break;\n }\n if (a[j] > 0 && d >= j && j < n) {\n a[0] = a[0] + 1;\n a[j] = a[j] - 1;\n d = d - j;\n } else if (a[j] <= 0 && d >= j && j < n) {\n j++;\n } else {\n break;\n }\n }\n printf(\"%d\\n\", a[0]);\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let t: u32 = line.trim().parse().unwrap();\n for _ in 0..t {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let mut pieces = line.split_whitespace();\n let n: u32 = pieces.next().unwrap().parse().unwrap();\n let d: u32 = pieces.next().unwrap().parse().unwrap();\n\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let pieces = line.split_whitespace();\n let mut a = Vec::new();\n\n for v_str in pieces {\n let v: u32 = v_str.parse().unwrap();\n a.push(v);\n }\n\n let mut days = 0;\n for i in 1..(n as usize) {\n while days < d && a[i] > 0 {\n for j in (1..(i + 1)).rev() {\n if days >= d {\n break;\n }\n\n a[j] -= 1;\n a[j - 1] += 1;\n days += 1;\n }\n }\n }\n\n println!(\"{}\", a[0]);\n }\n}", "difficulty": "medium"} {"problem_id": "0511", "problem_description": "You are given an integer $$$n$$$. Check if $$$n$$$ has an odd divisor, greater than one (does there exist such a number $$$x$$$ ($$$x > 1$$$) that $$$n$$$ is divisible by $$$x$$$ and $$$x$$$ is odd).For example, if $$$n=6$$$, then there is $$$x=3$$$. If $$$n=4$$$, then such a number does not exist.", "c_code": "int solution() {\n long long int n = 0;\n long long int x = 0;\n long long int fl = 0;\n scanf(\"%lld\", &n);\n for (long long int t = 0; t < n; t++) {\n scanf(\"%lld\", &x);\n fl = 0;\n while (x % 2 == 0) {\n x /= 2;\n }\n if (x % 2 == 1 && x != 1) {\n fl = 1;\n }\n if (fl == 0) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(1)\n .flatten()\n .flat_map(|s| s.parse::())\n .for_each(|n| {\n let ans = if n.wrapping_neg() & n == n {\n \"NO\"\n } else {\n \"YES\"\n };\n writeln!(buf, \"{ans}\").ok();\n });\n}", "difficulty": "hard"} {"problem_id": "0512", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

In some other world, today is Christmas Eve.

\n

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \\leq i \\leq N) is h_i meters.

\n

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

\n

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq K < N \\leq 10^5
  • \n
  • 1 \\leq h_i \\leq 10^9
  • \n
  • h_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nh_1\nh_2\n:\nh_N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible value of h_{max} - h_{min}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n10\n15\n11\n14\n12\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 3\n5\n7\n5\n7\n7\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

\n

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).

\n
\n
", "c_code": "int solution(void) {\n int a[100000];\n int min;\n int n;\n int i;\n int b;\n int k;\n int x;\n int tmp;\n int j;\n int count;\n int answer;\n scanf(\"%d %d\", &n, &k);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b = b + a[i];\n }\n\n int increment;\n int temp;\n int array_size = n;\n\n increment = 4;\n while (increment > 0) {\n for (i = 0; i < array_size; i++) {\n j = i;\n temp = a[i];\n while ((j >= increment) && (a[j - increment] > temp)) {\n a[j] = a[j - increment];\n j = j - increment;\n }\n a[j] = temp;\n }\n if (increment / 2 != 0) {\n increment = increment / 2;\n } else if (increment == 1) {\n increment = 0;\n } else {\n increment = 1;\n }\n }\n count = 0;\n min = 999999999;\n j = 0;\n for (i = 0; i < n - k + 1; i++) {\n count = a[i + k - 1] - a[i];\n if (count <= min) {\n min = count;\n }\n }\n printf(\"%d\\n\", min);\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut buf = String::new();\n let _ = stdin.read_to_string(&mut buf);\n let mut iter = buf.split_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let k: usize = iter.next().unwrap().parse().unwrap();\n let mut h: Vec = iter.map(|x| x.parse().unwrap()).collect();\n\n h.sort();\n println!(\n \"{}\",\n (0..(n - k + 1)).map(|i| h[i + k - 1] - h[i]).min().unwrap()\n );\n}", "difficulty": "medium"} {"problem_id": "0513", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There is a right triangle ABC with ∠ABC=90°.

\n

Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC.

\n

It is guaranteed that the area of the triangle ABC is an integer.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |AB|,|BC|,|CA| \\leq 100
  • \n
  • All values in input are integers.
  • \n
  • The area of the triangle ABC is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
|AB| |BC| |CA|\n
\n
\n
\n
\n
\n

Output

Print the area of the triangle ABC.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4 5\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

\"tri\"

\n

This triangle has an area of 6.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 12 13\n
\n
\n
\n
\n
\n

Sample Output 2

30\n
\n

This triangle has an area of 30.

\n
\n
\n
\n
\n
\n

Sample Input 3

45 28 53\n
\n
\n
\n
\n
\n

Sample Output 3

630\n
\n

This triangle has an area of 630.

\n
\n
", "c_code": "int solution() {\n int ab = 0;\n int bc = 0;\n int ca = 0;\n\n scanf(\"%d %d %d\", &ab, &bc, &ca);\n\n printf(\"%d\\n\", (ab * bc) / 2);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut l = String::new();\n stdin().read_line(&mut l).unwrap();\n let l: Vec = l.split_whitespace().take(2).flat_map(str::parse).collect();\n println!(\"{}\", l[0] * l[1] / 2);\n}", "difficulty": "medium"} {"problem_id": "0514", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N cubes stacked vertically on a desk.

\n

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

\n

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

\n

At most how many cubes can be removed?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • |S| = N
  • \n
  • Each character in S is 0 or 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the maximum number of cubes that can be removed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

0011\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

All four cubes can be removed, by performing the operation as follows:

\n
    \n
  • Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.
  • \n
  • Remove the first and second cubes from the bottom.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

11011010001011\n
\n
\n
\n
\n
\n

Sample Output 2

12\n
\n
\n
\n
\n
\n
\n

Sample Input 3

0\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n int i = 0;\n int cnt1 = 0;\n int cnt0 = 0;\n char s[1000000];\n scanf(\"%s\", s);\n\n while (s[i] != '\\0') {\n if (s[i] == '1') {\n cnt1++;\n } else if (s[i] == '0') {\n cnt0++;\n }\n i++;\n }\n\n if (cnt1 >= cnt0 && cnt1 > 0 && cnt0 > 0) {\n printf(\"%d\\n\", 2 * cnt0);\n } else if (cnt1 > 0 && cnt0 > 0) {\n printf(\"%d\\n\", 2 * cnt1);\n } else {\n printf(\"%d\\n\", 0);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n let mut stack: Vec = Vec::new();\n std::io::stdin().read_line(&mut input).ok();\n let s = input.trim();\n for c in s.chars() {\n if c != '1' && c != '0' {\n continue;\n }\n\n match stack.last() {\n None => stack.push(c),\n Some(&l) => {\n if l == c {\n stack.push(c);\n } else {\n stack.pop();\n }\n }\n }\n }\n\n println!(\"{}\", s.len() - stack.len());\n}", "difficulty": "medium"} {"problem_id": "0515", "problem_description": "

Entrance Examination

\n\n

\nThe International Competitive Programming College (ICPC) is famous\nfor its research on competitive programming.\nApplicants to the college are required to take its entrance examination.\n

\n\n

\nThe successful applicants of the examination are chosen as follows.\n

\n\n
    \n
  • The score of any successful applicant is higher than that of any unsuccessful applicant.
  • \n
  • The number of successful applicants n must be between nmin and nmax, inclusive.\nWe choose n within the specified range that maximizes the gap.\nHere, the gap means the difference between the lowest score of\nsuccessful applicants and the highest score of unsuccessful applicants.\n
  • \n
  • When two or more candidates for n make exactly the same gap,\nuse the greatest n among them.
  • \n
\n\n\n

\nLet's see the first couple of examples given in Sample Input below.\nIn the first example, nmin and nmax are two and four, respectively, and there are five applicants whose scores are 100, 90, 82, 70, and 65.\nFor n of two, three and four, the gaps will be 8, 12, and 5, respectively.\nWe must choose three as n, because it maximizes the gap.\n

\n\n

\nIn the second example, nmin and nmax are two and four, respectively, and there are five applicants whose scores are 100, 90, 80, 75, and 65.\nFor n of two, three and four, the gap will be 10, 5, and 10, \nrespectively. Both two and four maximize the gap, and we must choose the\n greatest number, four.\n

\n\n

\nYou are requested to write a program that computes the number of successful applicants that satisfies the conditions.\n

\n\n

Input

\n\n

\nThe input consists of multiple datasets. Each dataset is formatted as follows.\n

\n\n
\nm nmin nmax
\nP1
\nP2
\n...
\nPm
\n
\n\n

\nThe first line of a dataset contains three integers separated by single spaces.\nm represents the number of applicants, nmin represents the minimum number of successful applicants, and nmax represents the maximum number of successful applicants.\nEach of the following m lines contains an integer\nPi, which represents the score of each applicant.\nThe scores are listed in descending order.\nThese numbers satisfy 0 < nmin < nmax < m ≤ 200, 0 ≤ Pi ≤ 10000 (1 ≤ im) and Pnmin > Pnmax+1. These ensure that there always exists an n satisfying the conditions.\n

\n\n

\nThe end of the input is represented by a line containing three zeros separated by single spaces.\n

\n\n\n

Output

\n\n\n

\nFor each dataset, output the number of successful applicants in a line.\n

\n\n\n\n

Sample Input

\n\n\n
5 2 4\n100\n90\n82\n70\n65\n5 2 4\n100\n90\n80\n75\n65\n3 1 2\n5000\n4000\n3000\n4 2 3\n10000\n10000\n8000\n8000\n4 2 3\n10000\n10000\n10000\n8000\n5 2 3\n100\n80\n68\n60\n45\n0 0 0\n
\n\n\n

Output for the Sample Input

\n\n\n
3\n4\n2\n2\n3\n2\n
", "c_code": "int solution(void) {\n int num = 1;\n int a = 1;\n int b = 1;\n while (1) {\n scanf(\"%d %d %d\", &num, &a, &b);\n if (num == 0 && a == 0 && b == 0) {\n break;\n }\n int i;\n int point[num];\n for (i = 1; i <= num; i++) {\n scanf(\"%d\", &point[i]);\n }\n int sa = 0;\n int max = 0;\n int ba = 0;\n for (i = a; i < b + 1; i++) {\n int d = point[i];\n int c = point[i + 1];\n sa = c - d;\n sa = sa * (-1);\n if (sa >= max) {\n max = sa;\n ba = i;\n }\n }\n printf(\"%d\\n\", ba);\n }\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut buf1 = String::new();\n io::stdin().read_line(&mut buf1).ok();\n let mut iter = buf1.split_whitespace().map(|n| usize::from_str(n).unwrap());\n let (m, nmin, nmax) = (\n iter.next().unwrap(),\n iter.next().unwrap(),\n iter.next().unwrap(),\n );\n\n if m == 0 && nmin == 0 && nmax == 0 {\n return;\n }\n\n let mut v: Vec = Vec::new();\n for _ in 0..m {\n let mut buf2 = String::new();\n io::stdin().read_line(&mut buf2).ok();\n let p: u16 = match buf2.trim().parse() {\n Ok(num) => num,\n Err(e) => {\n panic!(\"{}\", e)\n }\n };\n v.push(p);\n }\n\n let mut ans_n: u16 = 0;\n let mut ans_gap: u16 = 0;\n for i in nmin..nmax + 1 {\n let gap = v[i - 1] - v[i];\n if gap < ans_gap {\n continue;\n }\n\n ans_gap = gap;\n ans_n = i as u16;\n }\n println!(\"{}\", ans_n);\n }\n}", "difficulty": "hard"} {"problem_id": "0516", "problem_description": "Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int q;\n scanf(\"%d\", &q);\n unsigned long long int a[q][3];\n unsigned long long int m[q];\n for (int i = 0; i < q; i++) {\n scanf(\"%llu %llu %llu\", &a[i][0], &a[i][1], &a[i][2]);\n m[i] = 0;\n if (a[i][0] == 1) {\n m[i] = a[i][1];\n } else {\n if (a[i][2] >= (2 * a[i][1])) {\n m[i] = a[i][1] * a[i][0];\n } else {\n m[i] = a[i][2] * (a[i][0] / 2) + a[i][1] * (a[i][0] % 2);\n }\n }\n }\n for (int i = 0; i < q; i++) {\n printf(\"%llu\\n\", m[i]);\n }\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let mut buf = String::new();\n\n stdin().read_line(&mut buf)?;\n\n let queries: i64 = buf.trim().parse()?;\n buf.clear();\n\n for _ in 0..queries {\n {\n stdin().read_line(&mut buf)?;\n\n let mut tmp = buf.split_whitespace().map(|x| x.parse::());\n let liter = tmp.next().unwrap()?;\n let a = tmp.next().unwrap()?;\n let b = tmp.next().unwrap()?;\n\n println!(\"{}\", (liter / 2) * (2 * a).min(b) + a * (liter % 2));\n }\n buf.clear();\n }\n\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "0517", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Kode Festival is an anual contest where the hardest stone in the world is determined. (Kode is a Japanese word for \"hardness\".)

\n

This year, 2^N stones participated. The hardness of the i-th stone is A_i.

\n

In the contest, stones are thrown at each other in a knockout tournament.

\n

When two stones with hardness X and Y are thrown at each other, the following will happen:

\n
    \n
  • \n

    When X > Y:\n The stone with hardness Y will be destroyed and eliminated.\n The hardness of the stone with hardness X will become X-Y.

    \n
  • \n
  • \n

    When X = Y:\n One of the stones will be destroyed and eliminated.\n The hardness of the other stone will remain the same.

    \n
  • \n
  • \n

    When X < Y:\n The stone with hardness X will be destroyed and eliminated.\n The hardness of the stone with hardness Y will become Y-X.

    \n
  • \n
\n

The 2^N stones will fight in a knockout tournament as follows:

\n
    \n
  1. \n

    The following pairs will fight: (the 1-st stone versus the 2-nd stone), (the 3-rd stone versus the 4-th stone), ...

    \n
  2. \n
  3. \n

    The following pairs will fight: (the winner of (1-st versus 2-nd) versus the winner of (3-rd versus 4-th)), (the winner of (5-th versus 6-th) versus the winner of (7-th versus 8-th)), ...

    \n
  4. \n
  5. \n

    And so forth, until there is only one stone remaining.

    \n
  6. \n
\n

Determine the eventual hardness of the last stone remaining.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 18
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
  • A_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nA_1\nA_2\n:\nA_{2^N}\n
\n
\n
\n
\n
\n

Output

Print the eventual hardness of the last stone remaining.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n1\n3\n10\n19\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1\n3\n2\n4\n6\n8\n100\n104\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n int st[300000];\n int i;\n int j;\n int k;\n int l;\n int m;\n int n;\n scanf(\"%d\", &N);\n j = 1;\n k = 1;\n m = 2;\n for (i = 1; i <= N; i++) {\n j = j * 2;\n }\n for (i = 0; i < j; i++) {\n scanf(\"%d\", &st[i]);\n }\n for (l = 1; l <= N; l++) {\n for (i = 0; i < j; i = i + m) {\n if (st[i] < st[i + k]) {\n n = st[i];\n st[i] = st[i + k];\n st[i + k] = n;\n }\n if (st[i] != st[i + k]) {\n st[i] = st[i] - st[i + k];\n }\n }\n k = k * 2;\n m = m * 2;\n }\n printf(\"%d\\n\", st[0]);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let mut a: Vec = s.take(1 << n).map(|a| a.parse().unwrap()).collect();\n while a.len() > 1 {\n let mut next = vec![];\n for a in a.chunks(2) {\n if a[0] > a[1] {\n next.push(a[0] - a[1]);\n } else if a[0] == a[1] {\n next.push(a[0]);\n } else {\n next.push(a[1] - a[0]);\n }\n }\n a = next;\n }\n println!(\"{}\", a[0]);\n}", "difficulty": "medium"} {"problem_id": "0518", "problem_description": "Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally?", "c_code": "int solution() {\n int n = 0;\n int odd_flag = 0;\n int num = 0;\n\n while (scanf(\"%d\", &n) != EOF) {\n while (n > 0) {\n n--;\n\n scanf(\"%d\", &num);\n\n if (num % 2 == 1) {\n odd_flag = 1;\n }\n }\n\n if (odd_flag == 1) {\n printf(\"First\\n\");\n } else {\n printf(\"Second\\n\");\n }\n\n odd_flag = 0;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n let stdin = io::stdin();\n stdin.lock().read_line(&mut line).unwrap();\n line.clear();\n stdin.lock().read_line(&mut line).unwrap();\n if line\n .split_whitespace()\n .fold(false, |acc, s| acc | ((s.parse::().unwrap() & 1) != 0))\n {\n println!(\"First\");\n } else {\n println!(\"Second\");\n }\n}", "difficulty": "easy"} {"problem_id": "0519", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively.\nPrint Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0\\leq A,B,C\\leq 100
  • \n
  • A, B and C are distinct integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 8 5\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

We pass the coordinate 5 on the straight way from the house at coordinate 3 to the house at coordinate 8.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 3 1\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 2 4\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 4

31 41 59\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
", "c_code": "int solution(void) {\n int a = 3;\n int b = 8;\n int c = 5;\n scanf(\"%d%d%d\", &a, &b, &c);\n if ((c > a && c < b) || (a > c && b < c)) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n\n io::stdin().read_line(&mut s).expect(\"Faled to read line\");\n\n let numbers: Vec = s\n .split(' ')\n .map(|s| s.trim())\n .filter(|s| !s.is_empty())\n .map(|s| s.parse().unwrap())\n .collect();\n\n let a = numbers[0];\n let b = numbers[1];\n let c = numbers[2];\n\n if (a < c && c < b) || (a > c && c > b) {\n println!(\"Yes\")\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "0520", "problem_description": "There is an infinite set generated as follows: $$$1$$$ is in this set. If $$$x$$$ is in this set, $$$x \\cdot a$$$ and $$$x+b$$$ both are in this set. For example, when $$$a=3$$$ and $$$b=6$$$, the five smallest elements of the set are: $$$1$$$, $$$3$$$ ($$$1$$$ is in this set, so $$$1\\cdot a=3$$$ is in this set), $$$7$$$ ($$$1$$$ is in this set, so $$$1+b=7$$$ is in this set), $$$9$$$ ($$$3$$$ is in this set, so $$$3\\cdot a=9$$$ is in this set), $$$13$$$ ($$$7$$$ is in this set, so $$$7+b=13$$$ is in this set). Given positive integers $$$a$$$, $$$b$$$, $$$n$$$, determine if $$$n$$$ is in this set.", "c_code": "int solution() {\n long long t;\n scanf(\"%lld\\n\", &t);\n while (t--) {\n long long n;\n long long a;\n long long b;\n scanf(\"%lld %lld %lld\\n\", &n, &a, &b);\n long long m = n % b;\n\n if (a == 1) {\n if ((n - 1) % b == 0) {\n puts(\"Yes\");\n } else {\n puts(\"No\");\n }\n continue;\n }\n\n long long x = 1;\n long long found = 0;\n while (x <= n) {\n if (x % b == m) {\n found = 1;\n break;\n }\n x *= a;\n }\n if (found) {\n puts(\"Yes\");\n } else {\n puts(\"No\");\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\n let t = it.next().unwrap();\n\n 'cases: for _ in 0..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 a = it.next().unwrap();\n let b = it.next().unwrap();\n\n let mut p = 1;\n\n while p <= n {\n let x = n - p;\n\n if x % b == 0 {\n writeln!(&mut output, \"Yes\").unwrap();\n continue 'cases;\n }\n\n if a == 1 {\n break;\n }\n p *= a;\n }\n\n writeln!(&mut output, \"No\").unwrap();\n }\n}", "difficulty": "easy"} {"problem_id": "0521", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

\n

Given is a permutation P_1, \\ldots, P_N of 1, \\ldots, N.\nFind the number of integers i (1 \\leq i \\leq N) that satisfy the following condition:

\n
    \n
  • For any integer j (1 \\leq j \\leq i), P_i \\leq P_j.
  • \n
\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • P_1, \\ldots, P_N is a permutation of 1, \\ldots, N.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nP_1 ... P_N\n
\n
\n
\n
\n
\n

Output

\n

Print the number of integers i that satisfy the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n4 2 5 1 3\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.
\nSimilarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n4 3 2 1\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n

All integers i (1 \\leq i \\leq N) satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 3

6\n1 2 3 4 5 6\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n

Only i=1 satisfies the condition.

\n
\n
\n
\n
\n
\n

Sample Input 4

8\n5 7 4 2 6 8 1 3\n
\n
\n
\n
\n
\n

Sample Output 4

4\n
\n
\n
\n
\n
\n
\n

Sample Input 5

1\n1\n
\n
\n
\n
\n
\n

Sample Output 5

1\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n int array[n];\n int j = 1;\n int count = 0;\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &array[i]);\n while (j <= i) {\n if (j == i && array[i] <= array[j]) {\n count++;\n break;\n }\n if (array[i] <= array[j] && j < i) {\n j++;\n } else if (array[i] >= array[j]) {\n break;\n }\n }\n }\n printf(\"%d\", count);\n return EXIT_SUCCESS;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let stdin = std::io::stdin();\n stdin.lock().read_to_string(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n let mut n = iter.next().unwrap().parse::().unwrap();\n let p_vec = iter\n .map(|e| e.parse::().unwrap())\n .collect::>();\n\n let mut ans = 0_usize;\n for &p in &p_vec {\n if p <= n {\n ans += 1;\n n = p;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0522", "problem_description": "You have a stripe of checkered paper of length $$$n$$$. Each cell is either white or black.What is the minimum number of cells that must be recolored from white to black in order to have a segment of $$$k$$$ consecutive black cells on the stripe?If the input data is such that a segment of $$$k$$$ consecutive black cells already exists, then print 0.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t > 0) {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n\n char string[n + 1];\n scanf(\"%s\", string);\n\n int left = 0;\n int white = 0;\n int min_whites = n;\n for (int right = 0; right < n; right++) {\n if (string[right] == 'W') {\n ++white;\n }\n if (right - left + 1 == k) {\n if (white < min_whites) {\n min_whites = white;\n }\n } else if (right - left + 1 > k) {\n if (string[left] == 'W') {\n white--;\n }\n left++;\n }\n if (right - left + 1 == k) {\n if (white < min_whites) {\n min_whites = white;\n }\n }\n }\n printf(\"%d\\n\", min_whites);\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).expect(\"Err\");\n\n for _i in 0..input.trim().to_string().parse::().unwrap() {\n input = String::new();\n io::stdin().read_line(&mut input).expect(\"Err\");\n let mut data: Vec = vec![];\n for i in input.trim().to_string().split(\" \") {\n data.push(i.parse::().unwrap());\n }\n\n input = String::new();\n io::stdin().read_line(&mut input).expect(\"Err\");\n let cases: Vec = input.trim().to_string().chars().collect();\n let mut max_count: u32 = 0;\n for i in 0..(data[0] - data[1] + 1) {\n let mut count: u32 = 0;\n for j in 0..data[1] {\n if cases[(i + j) as usize] == 'B' {\n count += 1;\n }\n }\n if count > max_count {\n max_count = count;\n }\n }\n println!(\"{}\", data[1] - max_count);\n }\n}", "difficulty": "hard"} {"problem_id": "0523", "problem_description": "You are given an array $$$a$$$ of length $$$n$$$. The array is called 3SUM-closed if for all distinct indices $$$i$$$, $$$j$$$, $$$k$$$, the sum $$$a_i + a_j + a_k$$$ is an element of the array. More formally, $$$a$$$ is 3SUM-closed if for all integers $$$1 \\leq i < j < k \\leq n$$$, there exists some integer $$$1 \\leq l \\leq n$$$ such that $$$a_i + a_j + a_k = a_l$$$.Determine if $$$a$$$ is 3SUM-closed.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n long long arr[200001];\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int pos = 0;\n int neg = 0;\n int zeroes = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &arr[i]);\n if (arr[i] > 0) {\n pos++;\n } else if (arr[i] < 0) {\n neg++;\n } else {\n zeroes++;\n }\n }\n if (zeroes == n) {\n printf(\"Yes\\n\");\n continue;\n }\n if (zeroes > 0) {\n if (pos >= 2 || neg >= 2) {\n printf(\"No\\n\");\n continue;\n }\n neg = 0;\n pos = 0;\n long long temp1;\n long long temp2;\n for (int i = 0; i < n; i++) {\n if (arr[i] < 0) {\n neg = 1;\n temp1 = arr[i];\n } else if (arr[i] > 0) {\n pos = 1;\n temp2 = arr[i];\n }\n }\n if (neg == 1 && pos == 1 && temp1 != -temp2) {\n printf(\"No\\n\");\n continue;\n } else {\n printf(\"Yes\\n\");\n continue;\n }\n } else if (zeroes == 0) {\n if (pos >= 3 || neg >= 3) {\n printf(\"No\\n\");\n continue;\n }\n }\n int check = 0;\n int temp = 0;\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n for (int k = j + 1; k < n; k++) {\n long long sum = arr[i] + arr[j] + arr[k];\n for (int l = 0; l < n; l++) {\n if (sum == arr[l]) {\n check++;\n break;\n }\n }\n temp++;\n }\n }\n }\n if (check == temp) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin()\n .read_to_string(&mut content)\n .expect(\"Unable to read from stdin\");\n\n let mut lines = content.lines();\n let num_tests: usize = lines.next().unwrap().parse().unwrap();\n 'test: for _ in 0..num_tests {\n let _n: usize = lines.next().unwrap().parse().unwrap();\n let a: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse::().unwrap())\n .collect();\n let mut b: Vec = vec![];\n let mut positives = 0;\n let mut negatives = 0;\n let mut zeros = 0;\n for k in a {\n if k > 0 {\n if positives < 2 {\n b.push(k);\n positives += 1;\n } else {\n println!(\"NO\");\n continue 'test;\n }\n } else if k < 0 {\n if negatives < 2 {\n b.push(k);\n negatives += 1;\n } else {\n println!(\"NO\");\n continue 'test;\n }\n } else {\n if zeros == 0 {\n b.push(k);\n zeros += 1;\n }\n }\n }\n let c: HashSet = HashSet::from_iter(b.iter().cloned());\n\n for i in 0..b.len() {\n for j in i + 1..b.len() {\n for k in j + 1..b.len() {\n if !c.contains(&(b[i] + b[j] + b[k])) {\n println!(\"NO\");\n continue 'test;\n }\n }\n }\n }\n\n println!(\"YES\");\n }\n}", "difficulty": "medium"} {"problem_id": "0524", "problem_description": "This problem differs from the next one only in the presence of the constraint on the equal length of all numbers $$$a_1, a_2, \\dots, a_n$$$. Actually, this problem is a subtask of the problem D2 from the same contest and the solution of D2 solves this subtask too.A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don't know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem.Let's denote a function that alternates digits of two numbers $$$f(a_1 a_2 \\dots a_{p - 1} a_p, b_1 b_2 \\dots b_{q - 1} b_q)$$$, where $$$a_1 \\dots a_p$$$ and $$$b_1 \\dots b_q$$$ are digits of two integers written in the decimal notation without leading zeros.In other words, the function $$$f(x, y)$$$ alternately shuffles the digits of the numbers $$$x$$$ and $$$y$$$ by writing them from the lowest digits to the older ones, starting with the number $$$y$$$. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below.For example: $$$$$$f(1111, 2222) = 12121212$$$$$$ $$$$$$f(7777, 888) = 7787878$$$$$$ $$$$$$f(33, 44444) = 4443434$$$$$$ $$$$$$f(555, 6) = 5556$$$$$$ $$$$$$f(111, 2222) = 2121212$$$$$$Formally, if $$$p \\ge q$$$ then $$$f(a_1 \\dots a_p, b_1 \\dots b_q) = a_1 a_2 \\dots a_{p - q + 1} b_1 a_{p - q + 2} b_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$$$; if $$$p < q$$$ then $$$f(a_1 \\dots a_p, b_1 \\dots b_q) = b_1 b_2 \\dots b_{q - p} a_1 b_{q - p + 1} a_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$$$. Mishanya gives you an array consisting of $$$n$$$ integers $$$a_i$$$. All numbers in this array are of equal length (that is, they consist of the same number of digits). Your task is to help students to calculate $$$\\sum_{i = 1}^{n}\\sum_{j = 1}^{n} f(a_i, a_j)$$$ modulo $$$998\\,244\\,353$$$.", "c_code": "int solution() {\n long long int n;\n long long int i;\n long long int cnt = 0;\n long long int count = 0;\n long long int flag = 0;\n long long int t = 0;\n long long int finalans = 0;\n scanf(\"%d\", &n);\n long long int a[n];\n long long int r[100] = {0};\n long long int d[100] = {0};\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (i = 0; i < n; i++) {\n t = a[i];\n cnt = 49;\n while (t > 0) {\n d[cnt] += t % 10;\n t /= 10;\n cnt--;\n }\n }\n cnt = 49;\n for (i = 49; i > 0; i = i - 2) {\n if (cnt < 0) {\n break;\n }\n r[i] = (n * d[cnt]) + count;\n count = r[i] / 10;\n r[i] = r[i] % 10;\n r[i - 1] = (n * d[cnt]) + count;\n count = r[i - 1] / 10;\n r[i - 1] = r[i - 1] % 10;\n cnt--;\n }\n for (i = 0; i <= 49; i++) {\n if (r[i] != 0) {\n flag = 1;\n }\n if (flag == 1) {\n finalans = (finalans * 10 + r[i]) % 998244353;\n }\n }\n printf(\"%d\", finalans);\n return 0;\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\n let n = get!();\n let md = 998244353;\n let mut ans = 0;\n for _ in 0..n {\n let mut x = get!();\n let mut p = 11;\n while x > 0 {\n let d = x % 10;\n x /= 10;\n ans += (p * d) % md;\n ans %= md;\n p *= 100;\n p %= md;\n }\n }\n let tmp = ans;\n for _ in 1..n {\n ans += tmp;\n ans %= md;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0525", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.

\n

Snuke plotted N points into the rectangle. The coordinate of the i-th (1 ≦ i ≦ N) point was (x_i, y_i).

\n

Then, he created an integer sequence a of length N, and for each 1 ≦ i ≦ N, he painted some region within the rectangle black, as follows:

\n
    \n
  • If a_i = 1, he painted the region satisfying x < x_i within the rectangle.
  • \n
  • If a_i = 2, he painted the region satisfying x > x_i within the rectangle.
  • \n
  • If a_i = 3, he painted the region satisfying y < y_i within the rectangle.
  • \n
  • If a_i = 4, he painted the region satisfying y > y_i within the rectangle.
  • \n
\n

Find the area of the white region within the rectangle after he finished painting.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ W, H ≦ 100
  • \n
  • 1 ≦ N ≦ 100
  • \n
  • 0 ≦ x_i ≦ W (1 ≦ i ≦ N)
  • \n
  • 0 ≦ y_i ≦ H (1 ≦ i ≦ N)
  • \n
  • W, H (21:32, added), x_i and y_i are integers.
  • \n
  • a_i (1 ≦ i ≦ N) is 1, 2, 3 or 4.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
W H N\nx_1 y_1 a_1\nx_2 y_2 a_2\n:\nx_N y_N a_N\n
\n
\n
\n
\n
\n

Output

Print the area of the white region within the rectangle after Snuke finished painting.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 4 2\n2 1 1\n3 3 4\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

The figure below shows the rectangle before Snuke starts painting.

\n
\n\"e19e673abcd0882783f635cce9d2f94d.png\"\n
\n

First, as (x_1, y_1) = (2, 1) and a_1 = 1, he paints the region satisfying x < 2 within the rectangle:

\n
\n\"f25cd04bbac23c4e5426d70511a9762f.png\"\n
\n

Then, as (x_2, y_2) = (3, 3) and a_2 = 4, he paints the region satisfying y > 3 within the rectangle:

\n
\n\"46b0c06fd9eee4f148e1f441f7abca53.png\"\n
\n

Now, the area of the white region within the rectangle is 9.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 4 3\n2 1 1\n3 3 4\n1 4 2\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

It is possible that the whole region within the rectangle is painted black.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 10 5\n1 6 1\n4 1 3\n6 9 4\n9 4 2\n3 1 3\n
\n
\n
\n
\n
\n

Sample Output 3

64\n
\n
\n
", "c_code": "int solution() {\n\n int x[101];\n int y[101];\n int a[101];\n scanf(\"%d%d%d\", &x[0], &y[0], &a[0]);\n int xs = 0;\n int ys = 0;\n int xf = x[0];\n int yf = y[0];\n int f = 0;\n\n for (int i = 1; i <= a[0]; i++) {\n scanf(\"%d%d%d\", &x[i], &y[i], &a[i]);\n\n if (a[i] == 1 && xs < x[i]) {\n xs = x[i];\n } else if (a[i] == 2 && xf > x[i]) {\n xf = x[i];\n } else if (a[i] == 3 && ys < y[i]) {\n ys = y[i];\n } else if (a[i] == 4 && yf > y[i]) {\n yf = y[i];\n }\n }\n int safe_area = (xf - xs) * (yf - ys);\n if ((xf - xs) > 0 && (yf - ys) > 0) {\n f = 1;\n }\n if (safe_area > 0 && f == 1) {\n printf(\"%d\\n\", safe_area);\n } else {\n printf(\"0\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n\n let whn: Vec = s.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n\n let mut x1 = 0;\n let mut x2 = whn[0];\n let mut y1 = 0;\n let mut y2 = whn[1];\n\n for _ in 0..whn[2] {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n\n let xya: Vec = s.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n\n if xya[2] == 1 {\n x1 = cmp::max(x1, xya[0]);\n } else if xya[2] == 2 {\n x2 = cmp::min(x2, xya[0]);\n } else if xya[2] == 3 {\n y1 = cmp::max(y1, xya[1]);\n } else {\n y2 = cmp::min(y2, xya[1]);\n }\n }\n\n println!(\n \"{}\",\n if x1 < x2 && y1 < y2 {\n (x2 - x1) * (y2 - y1)\n } else {\n 0\n }\n );\n}", "difficulty": "easy"} {"problem_id": "0526", "problem_description": "There are $$$n$$$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $$$x$$$ eats a slime with a value $$$y$$$, the eaten slime disappears, and the value of the remaining slime changes to $$$x - y$$$.The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime.", "c_code": "int solution() {\n\n int n;\n scanf(\"%d\", &n);\n int en;\n if (n == 1) {\n scanf(\"%d\", &en);\n printf(\"%d\\n\", en);\n } else {\n long long sum = 0;\n int p;\n int ne;\n p = ne = 0;\n int mi = 1000000000;\n while (n--) {\n scanf(\"%d\", &en);\n if (en > 0) {\n p = 1;\n if (en < mi) {\n mi = en;\n }\n } else if (en < 0) {\n en = -en;\n ne = 1;\n if (en < mi) {\n mi = en;\n }\n } else {\n p = ne = 1;\n mi = 0;\n }\n sum += en;\n }\n if (p && ne) {\n printf(\"%lld\\n\", sum);\n } else {\n printf(\"%lld\\n\", sum - (2 * mi));\n }\n }\n\n return 0;\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\n .split_whitespace()\n .map(|x| x.parse::().expect(\"convert to number\"))\n .collect::>();\n\n let n = arr[0];\n\n let arr2: Vec<_> = arr.into_iter().skip(1).collect();\n\n let pos = arr2.iter().all(|x| *x >= 0);\n let neg = arr2.iter().all(|x| *x <= 0);\n\n let mut ans = 0;\n\n if n == 1 {\n println!(\"{}\", arr2[0]);\n return;\n }\n\n if pos {\n let aa = arr2.iter().min().unwrap();\n let s: i64 = arr2.iter().sum();\n ans = s - 2 * aa;\n } else if neg {\n let aa = arr2.iter().max().unwrap();\n let s: i64 = arr2.iter().sum();\n ans = 2 * aa - s;\n } else {\n ans = arr2.iter().map(|x| x.abs()).sum();\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0527", "problem_description": "You are given a grid with $$$n$$$ rows and $$$m$$$ columns. We denote the square on the $$$i$$$-th ($$$1\\le i\\le n$$$) row and $$$j$$$-th ($$$1\\le j\\le m$$$) column by $$$(i, j)$$$ and the number there by $$$a_{ij}$$$. All numbers are equal to $$$1$$$ or to $$$-1$$$. You start from the square $$$(1, 1)$$$ and can move one square down or one square to the right at a time. In the end, you want to end up at the square $$$(n, m)$$$.Is it possible to move in such a way so that the sum of the values written in all the visited cells (including $$$a_{11}$$$ and $$$a_{nm}$$$) is $$$0$$$?", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n int m = 0;\n int a[1000][1000] = {};\n\n int res = 0;\n\n int dp[1000][1000][2] = {};\n\n res = scanf(\"%d\", &t);\n\n while (t > 0) {\n res = scanf(\"%d\", &n);\n res = scanf(\"%d\", &m);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n res = scanf(\"%d\", a[i] + j);\n dp[i][j][0] = -1 * n * m;\n dp[i][j][1] = n * m;\n }\n }\n dp[0][0][0] = a[0][0];\n dp[0][0][1] = a[0][0];\n for (int i = 1; i < m; i++) {\n if (dp[0][i - 1][0] + a[0][i] > dp[0][i][0]) {\n dp[0][i][0] = dp[0][i - 1][0] + a[0][i];\n }\n if (dp[0][i - 1][1] + a[0][i] < dp[0][i][1]) {\n dp[0][i][1] = dp[0][i - 1][1] + a[0][i];\n }\n }\n for (int i = 1; i < n; i++) {\n if (dp[i - 1][0][0] + a[i][0] > dp[i][0][0]) {\n dp[i][0][0] = dp[i - 1][0][0] + a[i][0];\n }\n if (dp[i - 1][0][1] + a[i][0] < dp[i][0][1]) {\n dp[i][0][1] = dp[i - 1][0][1] + a[i][0];\n }\n for (int j = 1; j < m; j++) {\n if (dp[i - 1][j][0] + a[i][j] > dp[i][j][0]) {\n dp[i][j][0] = dp[i - 1][j][0] + a[i][j];\n }\n if (dp[i - 1][j][1] + a[i][j] < dp[i][j][1]) {\n dp[i][j][1] = dp[i - 1][j][1] + a[i][j];\n }\n if (dp[i][j - 1][0] + a[i][j] > dp[i][j][0]) {\n dp[i][j][0] = dp[i][j - 1][0] + a[i][j];\n }\n if (dp[i][j - 1][1] + a[i][j] < dp[i][j][1]) {\n dp[i][j][1] = dp[i][j - 1][1] + a[i][j];\n }\n }\n }\n if (dp[n - 1][m - 1][0] >= 0 && dp[n - 1][m - 1][1] <= 0 &&\n dp[n - 1][m - 1][0] % 2 == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let stdin = io::stdin();\n\n let (mut n, mut m): (usize, usize);\n let mut grid: Vec> = vec![];\n let mut max_sums: Vec> = vec![];\n let mut min_sums: Vec> = vec![];\n\n stdin.read_line(&mut buf).expect(\"bruh\");\n let tests: i32 = buf.trim().parse().unwrap();\n\n for _ in 0..tests {\n buf.clear();\n grid.clear();\n max_sums.clear();\n min_sums.clear();\n stdin.read_line(&mut buf).expect(\"bruh\");\n n = buf.trim().split(' ').collect::>()[0]\n .parse()\n .unwrap();\n m = buf.trim().split(' ').collect::>()[1]\n .parse()\n .unwrap();\n\n for _ in 0..n {\n buf.clear();\n stdin.read_line(&mut buf).expect(\"bruh\");\n\n grid.push(\n buf.trim()\n .split(' ')\n .map(|x| x.parse::().unwrap())\n .collect(),\n );\n max_sums.push(Vec::new());\n min_sums.push(Vec::new());\n }\n\n max_sums[0].push(grid[0][0]);\n min_sums[0].push(grid[0][0]);\n\n for i in 0..n {\n for j in 0..m {\n if i == 0 && j == 0 {\n continue;\n }\n\n max_sums[i].push(i32::MIN);\n min_sums[i].push(i32::MAX);\n\n if i != 0 {\n max_sums[i][j] = cmp::max(max_sums[i][j], max_sums[i - 1][j] + grid[i][j]);\n min_sums[i][j] = cmp::min(min_sums[i][j], min_sums[i - 1][j] + grid[i][j]);\n }\n\n if j != 0 {\n max_sums[i][j] = cmp::max(max_sums[i][j], max_sums[i][j - 1] + grid[i][j]);\n min_sums[i][j] = cmp::min(min_sums[i][j], min_sums[i][j - 1] + grid[i][j]);\n }\n }\n }\n\n if (n + m) % 2 != 0 && max_sums[n - 1][m - 1] >= 0 && min_sums[n - 1][m - 1] <= 0 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0528", "problem_description": "Polycarp likes numbers that are divisible by 3.He has a huge number $$$s$$$. Polycarp wants to cut from it the maximum number of numbers that are divisible by $$$3$$$. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after $$$m$$$ such cuts, there will be $$$m+1$$$ parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by $$$3$$$.For example, if the original number is $$$s=3121$$$, then Polycarp can cut it into three parts with two cuts: $$$3|1|21$$$. As a result, he will get two numbers that are divisible by $$$3$$$.Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.What is the maximum number of numbers divisible by $$$3$$$ that Polycarp can obtain?", "c_code": "int solution() {\n char a[200004];\n int n;\n int i;\n scanf(\"%s\", a);\n n = strlen(a);\n int sum[200005];\n int ans = 0;\n for (i = 0; i < n; i++) {\n sum[i] = (a[i] - '0') % 3;\n }\n\n for (i = 0; i < n; i++) {\n if (sum[i] == 0) {\n ans++;\n } else {\n if (sum[i] == 1) {\n if (i + 1 < n && sum[i + 1] == 2) {\n i++;\n ans++;\n continue;\n }\n if (i + 1 < n && sum[i + 1] == 1) {\n if (i + 2 < n && sum[i + 2] == 1) {\n i += 2;\n ans++;\n continue;\n }\n }\n } else {\n if (i + 1 < n && sum[i + 1] == 1) {\n i++;\n ans++;\n continue;\n }\n if (i + 1 < n && sum[i + 1] == 2) {\n if (i + 2 < n && sum[i + 2] == 2) {\n i += 2;\n ans++;\n continue;\n }\n }\n }\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\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 p1 = 0;\n let mut p2 = 0;\n let mut ans = 0;\n for i in 0..s.len() {\n let c = s[i] as u64;\n if c.is_multiple_of(3) {\n ans += 1;\n p1 = 0;\n p2 = 0;\n } else {\n if c % 3 == 1 {\n p1 += 1;\n } else {\n p2 += 1;\n }\n if p1 > 2 || p2 > 2 || (p1 > 0 && p2 > 0) {\n ans += 1;\n p1 = 0;\n p2 = 0;\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0529", "problem_description": "You are given a string $$$s$$$ consisting only of lowercase Latin letters.You can rearrange all letters of this string as you wish. Your task is to obtain a good string by rearranging the letters of the given string or report that it is impossible to do it.Let's call a string good if it is not a palindrome. Palindrome is a string which is read from left to right the same as from right to left. For example, strings \"abacaba\", \"aa\" and \"z\" are palindromes and strings \"bba\", \"xd\" are not.You have to answer $$$t$$$ independent queries.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char str[1001];\n scanf(\"%s\", str);\n int len = strlen(str);\n if (len == 1) {\n puts(\"-1\");\n } else {\n int cnt[26];\n memset(cnt, 0, sizeof(cnt));\n for (int i = 0; str[i]; ++i) {\n ++cnt[str[i] - 'a'];\n }\n for (int i = 0; i < 26; ++i) {\n if (cnt[i] == len) {\n printf(\"-1\");\n break;\n }\n for (int j = 0; j < cnt[i]; ++j) {\n putchar('a' + i);\n }\n }\n putchar('\\n');\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut reader = BufReader::new(io::stdin());\n\n let mut Tstr = String::new();\n reader.read_line(&mut Tstr);\n let T = Tstr.trim().parse::().unwrap();\n\n for _i in 0..T {\n let mut Qstr = String::new();\n reader.read_line(&mut Qstr);\n Qstr = Qstr.trim().to_string();\n if Qstr.len() == 1 {\n println!(\"-1\")\n } else {\n let mut ok = true;\n for i in 1..Qstr.len() {\n ok &= Qstr.as_bytes()[i] == Qstr.as_bytes()[i - 1];\n }\n\n if ok {\n println!(\"-1\");\n } else {\n let Qstr_slice: &str = &Qstr[..];\n\n let mut chars: Vec = Qstr_slice.chars().collect();\n chars.sort_by(|a, b| b.cmp(a));\n\n let ans = String::from_iter(chars);\n println!(\"{}\", ans);\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0530", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.

\n

A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.

\n

When a player correctly answers a question, each of the other N-1 players receives minus one (-1) point. There is no other factor that affects the players' scores.

\n

At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.

\n

In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player A_i.\nFor Kizahashi, write a program that determines whether each of the N players survived this game.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq K \\leq 10^9
  • \n
  • 1 \\leq Q \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq N\\ (1 \\leq i \\leq Q)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N K Q\nA_1\nA_2\n.\n.\n.\nA_Q\n
\n
\n
\n
\n
\n

Output

\n

Print N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 3 4\n3\n1\n3\n2\n
\n
\n
\n
\n
\n

Sample Output 1

No\nNo\nYes\nNo\nNo\nNo\n
\n

In the beginning, the players' scores are (3, 3, 3, 3, 3, 3).

\n
    \n
  • Player 3 correctly answers a question. The players' scores are now (2, 2, 3, 2, 2, 2).
  • \n
  • Player 1 correctly answers a question. The players' scores are now (2, 1, 2, 1, 1, 1).
  • \n
  • Player 3 correctly answers a question. The players' scores are now (1, 0, 2, 0, 0, 0).
  • \n
  • Player 2 correctly answers a question. The players' scores are now (0, 0, 1, -1, -1, -1).
  • \n
\n

Players 1, 2, 4, 5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.

\n
\n
\n
\n
\n
\n

Sample Input 2

6 5 4\n3\n1\n3\n2\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\nYes\nYes\nYes\nYes\nYes\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 13 15\n3\n1\n4\n1\n5\n9\n2\n6\n5\n3\n5\n8\n9\n7\n9\n
\n
\n
\n
\n
\n

Sample Output 3

No\nNo\nNo\nNo\nYes\nNo\nNo\nNo\nYes\nNo\n
\n
\n
", "c_code": "int solution(void) {\n int score[200000] = {0};\n long N = 0;\n long K = 0;\n long Q = 0;\n scanf(\"%ld %ld %ld\", &N, &K, &Q);\n for (long i = 0; i < Q; i++) {\n int A = 0;\n scanf(\"%d\", &A);\n score[A - 1] += 1;\n }\n for (long i = 0; i < N; i++) {\n if (K - Q + score[i] <= 0) {\n printf(\"No\\n\");\n } else {\n printf(\"Yes\\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 nkq = lines.next().unwrap().unwrap();\n let mut nkq = nkq.split_whitespace();\n let n: usize = nkq.next().unwrap().parse().unwrap();\n let k: i32 = nkq.next().unwrap().parse().unwrap();\n let q: i32 = nkq.next().unwrap().parse().unwrap();\n let mut a = vec![k - q; n + 1];\n for _ in 0..q {\n let i: usize = lines.next().unwrap().unwrap().parse().unwrap();\n a[i] += 1;\n }\n for &a_i in a.iter().skip(1) {\n if a_i > 0 {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0531", "problem_description": "One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.", "c_code": "int solution() {\n int test_cases;\n scanf(\"%d\", &test_cases);\n while (test_cases--) {\n int n = 0;\n scanf(\"%d\", &n);\n printf(\"%d\\n\", (n / 2) + 1);\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut handle = stdin.lock();\n let mut buf = String::new();\n handle.read_line(&mut buf).unwrap();\n let n: usize = buf.trim().parse().unwrap();\n for _q in 0..n {\n buf.clear();\n handle.read_line(&mut buf).unwrap();\n let m: usize = buf.trim().parse().unwrap();\n println!(\"{}\", m / 2 + 1);\n }\n}", "difficulty": "medium"} {"problem_id": "0532", "problem_description": "\n

Score : 800 points

\n
\n
\n

Problem Statement

Takahashi has received an undirected graph with N vertices, numbered 1, 2, ..., N.\nThe edges in this graph are represented by (u_i, v_i).\nThere are no self-loops and multiple edges in this graph.

\n

Based on this graph, Takahashi is now constructing a new graph with N^2 vertices, where each vertex is labeled with a pair of integers (a, b) (1 \\leq a \\leq N, 1 \\leq b \\leq N).\nThe edges in this new graph are generated by the following rule:

\n
    \n
  • Span an edge between vertices (a, b) and (a', b') if and only if both of the following two edges exist in the original graph: an edge between vertices a and a', and an edge between vertices b and b'.
  • \n
\n

How many connected components are there in this new graph?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 100,000
  • \n
  • 0 \\leq M \\leq 200,000
  • \n
  • 1 \\leq u_i < v_i \\leq N
  • \n
  • There exists no pair of distinct integers i and j such that u_i = u_j and v_i = v_j.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N M\nu_1 v_1\nu_2 v_2\n:\nu_M v_M\n
\n
\n
\n
\n
\n

Output

Print the number of the connected components in the graph constructed by Takahashi.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1\n1 2\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

The graph constructed by Takahashi is as follows.

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 2

7 5\n1 2\n3 4\n3 5\n4 5\n2 6\n
\n
\n
\n
\n
\n

Sample Output 2

18\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int M;\n int u;\n int w;\n list *adj[100001] = {};\n list e[400001];\n scanf(\"%d %d\", &N, &M);\n for (i = 0; i < M; i++) {\n scanf(\"%d %d\", &u, &w);\n e[i * 2].v = w;\n e[(i * 2) + 1].v = u;\n e[i * 2].next = adj[u];\n e[(i * 2) + 1].next = adj[w];\n adj[u] = &(e[i * 2]);\n adj[w] = &(e[(i * 2) + 1]);\n }\n\n int iso = 0;\n int bip = 0;\n int nonbip = 0;\n int flag[100001] = {};\n int q[100001];\n int head;\n int tail;\n int tmp;\n list *p;\n for (i = 1; i <= N; i++) {\n if (flag[i] != 0) {\n continue;\n }\n\n flag[i] = 1;\n if (adj[i] == NULL) {\n iso++;\n continue;\n }\n\n q[0] = i;\n for (head = 0, tail = 1, tmp = 0; head < tail; head++) {\n u = q[head];\n for (p = adj[u]; p != NULL; p = p->next) {\n w = p->v;\n if (flag[w] == 0) {\n flag[w] = -flag[u];\n q[tail++] = w;\n } else if (flag[w] == flag[u]) {\n tmp = 1;\n }\n }\n }\n if (tmp != 0) {\n nonbip++;\n } else {\n bip++;\n }\n }\n\n printf(\"%lld\\n\", ((long long)iso * (N * 2 - iso)) +\n ((long long)(bip + nonbip) * (bip + nonbip)) +\n ((long long)bip * bip));\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input!(n: usize, m: usize, uv: [(usize1, usize1); m]);\n let mut graph = vec![vec![]; n];\n for &(u, v) in uv.iter() {\n graph[u].push(v);\n graph[v].push(u);\n }\n\n let mut q = VecDeque::new();\n let mut vis = vec![0; n];\n let mut components = vec![];\n for i in 0..n {\n if vis[i] != 0 {\n continue;\n }\n let mut is_bipartite = true;\n let mut c = vec![];\n q.push_back(i);\n vis[i] = 1;\n c.push(i);\n while let Some(v) = q.pop_front() {\n for &next in graph[v].iter() {\n if vis[next] != 0 {\n if vis[next] == vis[v] {\n is_bipartite = false;\n }\n continue;\n }\n vis[next] = if vis[v] == 1 { 2 } else { 1 };\n q.push_back(next);\n c.push(next);\n }\n }\n\n components.push((c, is_bipartite));\n }\n\n let mut cnt_one = 0;\n let mut cnt_bipartite = 0;\n let mut cnt_other = 0;\n for &(ref c, is_bipartite) in components.iter() {\n if c.len() == 1 {\n cnt_one += 1;\n } else if is_bipartite {\n cnt_bipartite += 1;\n } else {\n cnt_other += 1;\n }\n }\n\n let mut ans = 0;\n ans += cnt_one * n * 2 - cnt_one * cnt_one;\n ans += cnt_bipartite * 2;\n ans += cnt_other;\n ans += cnt_bipartite * (cnt_bipartite - 1) * 2;\n ans += cnt_other * (cnt_other - 1);\n ans += cnt_other * cnt_bipartite * 2;\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0533", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

In the Kingdom of AtCoder, people use a language called Taknese, which uses lowercase English letters.

\n

In Taknese, the plural form of a noun is spelled based on the following rules:

\n
    \n
  • If a noun's singular form does not end with s, append s to the end of the singular form.
  • \n
  • If a noun's singular form ends with s, append es to the end of the singular form.
  • \n
\n

You are given the singular form S of a Taknese noun. Output its plural form.

\n
\n
\n
\n
\n

Constraints

    \n
  • S is a string of length 1 between 1000, inclusive.
  • \n
  • S contains only lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the plural form of the given Taknese word.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

apple\n
\n
\n
\n
\n
\n

Sample Output 1

apples\n
\n

apple ends with e, so its plural form is apples.

\n
\n
\n
\n
\n
\n

Sample Input 2

bus\n
\n
\n
\n
\n
\n

Sample Output 2

buses\n
\n

bus ends with s, so its plural form is buses.

\n
\n
\n
\n
\n
\n

Sample Input 3

box\n
\n
\n
\n
\n
\n

Sample Output 3

boxs\n
\n
\n
", "c_code": "int solution() {\n int i = -1;\n char a[10000];\n do {\n i++;\n scanf(\"%s\", &a[i]);\n } while (a[i]);\n if (a[i - 1] == 's') {\n a[i] = 'e';\n a[i + 1] = 's';\n } else {\n a[i] = 's';\n }\n printf(\"%s\\n\", a);\n return 0;\n}", "rust_code": "fn solution() {\n let mut word = String::new();\n stdin().read_line(&mut word).unwrap();\n\n if word.trim().ends_with(\"s\") {\n println!(\"{}es\", word.trim());\n } else {\n println!(\"{}s\", word.trim());\n }\n}", "difficulty": "medium"} {"problem_id": "0534", "problem_description": "Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. The dragon has a hit point of $$$x$$$ initially. When its hit point goes to $$$0$$$ or under $$$0$$$, it will be defeated. In order to defeat the dragon, Kana can cast the two following types of spells. Void Absorption Assume that the dragon's current hit point is $$$h$$$, after casting this spell its hit point will become $$$\\left\\lfloor \\frac{h}{2} \\right\\rfloor + 10$$$. Here $$$\\left\\lfloor \\frac{h}{2} \\right\\rfloor$$$ denotes $$$h$$$ divided by two, rounded down. Lightning Strike This spell will decrease the dragon's hit point by $$$10$$$. Assume that the dragon's current hit point is $$$h$$$, after casting this spell its hit point will be lowered to $$$h-10$$$.Due to some reasons Kana can only cast no more than $$$n$$$ Void Absorptions and $$$m$$$ Lightning Strikes. She can cast the spells in any order and doesn't have to cast all the spells. Kana isn't good at math, so you are going to help her to find out whether it is possible to defeat the dragon.", "c_code": "int solution() {\n int k;\n scanf(\"%d\", &k);\n int b[k];\n int a[k];\n int c[k];\n\n for (int i = 0; i < k; i++) {\n int sum = 0;\n int flag = 0;\n scanf(\"%d\", &a[i]);\n scanf(\"%d\", &b[i]);\n scanf(\"%d\", &c[i]);\n\n if (c[i] == 0) {\n printf(\"NO\\n\");\n continue;\n }\n if (c[i] == 1 && a[i] > 10) {\n printf(\"NO\\n\");\n continue;\n }\n sum = sum + c[i] * 10;\n if (a[i] <= sum) {\n printf(\"YES\\n\");\n continue;\n }\n for (int j = 0; j < b[i]; j++) {\n sum = sum - 10;\n sum = sum * 2 + 1;\n if (a[i] <= sum) {\n printf(\"YES\\n\");\n flag = 1;\n break;\n }\n }\n if (flag == 1) {\n continue;\n }\n\n if (a[i] > sum) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let test: i64 = str.trim().parse().unwrap();\n for _ in 0..test {\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let mut iter = str.split_whitespace();\n let mut health: i64 = iter.next().unwrap().parse().unwrap();\n let void: i64 = iter.next().unwrap().parse().unwrap();\n let lightning: i64 = iter.next().unwrap().parse().unwrap();\n for _ in 0..void {\n health = if health < 20 { health } else { health / 2 + 10 };\n }\n if health > 10 * lightning {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0535", "problem_description": "This is a simplified version of the problem B2. Perhaps you should read the problem B2 before you start solving B1.Paul and Mary have a favorite string $$$s$$$ which consists of lowercase letters of the Latin alphabet. They want to paint it using pieces of chalk of two colors: red and green. Let's call a coloring of a string wonderful if the following conditions are met: each letter of the string is either painted in exactly one color (red or green) or isn't painted; each two letters which are painted in the same color are different; the number of letters painted in red is equal to the number of letters painted in green; the number of painted letters of this coloring is maximum among all colorings of the string which meet the first three conditions. E. g. consider a string $$$s$$$ equal to \"kzaaa\". One of the wonderful colorings of the string is shown in the figure. The example of a wonderful coloring of the string \"kzaaa\". Paul and Mary want to learn by themselves how to find a wonderful coloring of the string. But they are very young, so they need a hint. Help them find $$$k$$$ — the number of red (or green, these numbers are equal) letters in a wonderful coloring.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char s[53];\n int a[27] = {0};\n int r = 0;\n int g = 0;\n int sum = 0;\n scanf(\"%s\", s);\n for (int i = 0; s[i] != '\\0'; i++) {\n a[s[i] - 'a']++;\n sum++;\n }\n\n for (int i = 0; i < 26; i++) {\n if (a[i] == 1 && r == g && r + g < sum - 1) {\n r++;\n } else if (a[i] == 1 && r == g + 1 && r + g < sum) {\n g++;\n }\n }\n for (int i = 0; i < 26; i++) {\n if (a[i] > 1 && r == g && r + g < sum - 1) {\n r++;\n g++;\n } else if (a[i] > 1 && r == g + 1 && r + g < sum) {\n g++;\n }\n }\n printf(\"%d\\n\", r);\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 for l in i.lock().lines().skip(1) {\n let mut h = std::collections::HashMap::new();\n for c in l.unwrap().chars() {\n h.entry(c).and_modify(|e| *e = 2).or_insert(1);\n }\n writeln!(o, \"{}\", h.values().sum::() / 2).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "0536", "problem_description": "There is a $$$n \\times m$$$ grid. You are standing at cell $$$(1, 1)$$$ and your goal is to finish at cell $$$(n, m)$$$.You can move to the neighboring cells to the right or down. In other words, suppose you are standing at cell $$$(x, y)$$$. You can: move right to the cell $$$(x, y + 1)$$$ — it costs $$$x$$$ burles; move down to the cell $$$(x + 1, y)$$$ — it costs $$$y$$$ burles. Can you reach cell $$$(n, m)$$$ spending exactly $$$k$$$ burles?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n char *yes_str = \"YES\\n\";\n char *no_str = \"NO\\n\";\n\n char *yesOrNo_pStr[t];\n\n for (int i = 0; i < t; i++) {\n int n;\n int m;\n int k;\n scanf(\"%d%d%d\", &n, &m, &k);\n\n yesOrNo_pStr[i] = no_str;\n if (n * m - 1 == k) {\n yesOrNo_pStr[i] = yes_str;\n }\n }\n for (int i = 0; i < t; i++) {\n printf(\"%s\", yesOrNo_pStr[i]);\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(1) {\n if let [n, m, k] = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>()[..]\n {\n writeln!(o, \"{}\", if n * m - 1 == k { \"YES\" } else { \"NO\" }).ok();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0537", "problem_description": "\n

Score: 400 points

\n
\n
\n

Problem statement

\n

We have an H \\times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).
\nSnuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.
\nBefore Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.
\nWhen the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

\n

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • H is an integer between 2 and 50 (inclusive).
  • \n
  • W is an integer between 2 and 50 (inclusive).
  • \n
  • s_{i, j} is . or # (1 \\leq i \\leq H, 1 \\leq j \\leq W).
  • \n
  • s_{1, 1} and s_{H, W} are ..
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
H W\ns_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}\ns_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}\n :   :\ns_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}\n
\n
\n
\n
\n
\n

Output

\n

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n..#\n#..\n...\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The score 2 can be achieved by changing the color of squares as follows:

\n

\"Explanation

\n
\n
\n
\n
\n
\n

Sample Input 2

10 37\n.....................................\n...#...####...####..###...###...###..\n..#.#..#...#.##....#...#.#...#.#...#.\n..#.#..#...#.#.....#...#.#...#.#...#.\n.#...#.#..##.#.....#...#.#.###.#.###.\n.#####.####..#.....#...#..##....##...\n.#...#.#...#.#.....#...#.#...#.#...#.\n.#...#.#...#.##....#...#.#...#.#...#.\n.#...#.####...####..###...###...###..\n.....................................\n
\n
\n
\n
\n
\n

Sample Output 2

209\n
\n
\n
", "c_code": "int solution(void) {\n int h;\n int w;\n int black = 0;\n scanf(\"%d%d\", &h, &w);\n char s[h][w + 2];\n int cost[h * w][h * w];\n for (int i = 0; i < h; i++) {\n scanf(\"%s\", s[i]);\n }\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (s[i][j] == '#') {\n black++;\n }\n }\n }\n for (int i = 0; i < h * w; i++) {\n for (int j = 0; j < h * w; j++) {\n cost[i][j] = 1e9;\n }\n }\n cost[0][0] = 1;\n int count = 0;\n int x[h * w];\n int y[h * w];\n x[0] = 0, y[0] = 0;\n for (int i = 0; i < count + 1; i++) {\n if (s[x[i] - 1][y[i]] == '.' &&\n cost[x[i] - 1][y[i]] > cost[x[i]][y[i]] + 1) {\n cost[x[i] - 1][y[i]] = cost[x[i]][y[i]] + 1;\n count++;\n x[count] = x[i] - 1;\n y[count] = y[i];\n }\n if (s[x[i] + 1][y[i]] == '.' &&\n cost[x[i] + 1][y[i]] > cost[x[i]][y[i]] + 1) {\n cost[x[i] + 1][y[i]] = cost[x[i]][y[i]] + 1;\n count++;\n x[count] = x[i] + 1;\n y[count] = y[i];\n }\n if (s[x[i]][y[i] - 1] == '.' &&\n cost[x[i]][y[i] - 1] > cost[x[i]][y[i]] + 1) {\n cost[x[i]][y[i] - 1] = cost[x[i]][y[i]] + 1;\n count++;\n x[count] = x[i];\n y[count] = y[i] - 1;\n }\n if (s[x[i]][y[i] + 1] == '.' &&\n cost[x[i]][y[i] + 1] > cost[x[i]][y[i]] + 1) {\n cost[x[i]][y[i] + 1] = cost[x[i]][y[i]] + 1;\n count++;\n x[count] = x[i];\n y[count] = y[i] + 1;\n }\n }\n if (cost[h - 1][w - 1] == 1e9) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d\\n\", (h * w) - black - cost[h - 1][w - 1]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut input = String::new();\n std::io::stdin().read_to_string(&mut input).unwrap();\n let mut iter = input.split_whitespace();\n let h: usize = iter.next().unwrap().parse().unwrap();\n let w: usize = iter.next().unwrap().parse().unwrap();\n use std::iter::FromIterator;\n let f: Vec<&[u8]> = Vec::from_iter(iter.map(|s| s.as_bytes()));\n let mut c: usize = 0;\n for i in 0..h {\n for j in 0..w {\n if f[i][j] == 46 {\n c += 1;\n }\n }\n }\n let inf = 100000;\n let mut d: Vec> = vec![vec![inf; w]; h];\n use std::collections::VecDeque;\n let mut q: VecDeque<(usize, usize)> = VecDeque::new();\n d[0][0] = 1;\n q.push_back((0, 0));\n while !q.is_empty() {\n let (x, y) = q.pop_front().unwrap();\n let dist = d[x][y] + 1;\n let mut push = |x: usize, y: usize, dist: usize| {\n if f[x][y] == 46 && d[x][y] > dist {\n q.push_back((x, y));\n d[x][y] = dist;\n }\n };\n if y + 1 < w {\n push(x, y + 1, dist);\n }\n if x + 1 < h {\n push(x + 1, y, dist);\n }\n if 0 < y {\n push(x, y - 1, dist);\n }\n if 0 < x {\n push(x - 1, y, dist);\n }\n }\n if d[h - 1][w - 1] == inf {\n println!(\"-1\")\n } else {\n println!(\"{}\", c - d[h - 1][w - 1])\n }\n}", "difficulty": "medium"} {"problem_id": "0538", "problem_description": "Eshag has an array $$$a$$$ consisting of $$$n$$$ integers.Eshag can perform the following operation any number of times: choose some subsequence of $$$a$$$ and delete every element from it which is strictly larger than $$$AVG$$$, where $$$AVG$$$ is the average of the numbers in the chosen subsequence.For example, if $$$a = [1 , 4 , 3 , 2 , 4]$$$ and Eshag applies the operation to the subsequence containing $$$a_1$$$, $$$a_2$$$, $$$a_4$$$ and $$$a_5$$$, then he will delete those of these $$$4$$$ elements which are larger than $$$\\frac{a_1+a_2+a_4+a_5}{4} = \\frac{11}{4}$$$, so after the operation, the array $$$a$$$ will become $$$a = [1 , 3 , 2]$$$.Your task is to find the maximum number of elements Eshag can delete from the array $$$a$$$ by applying the operation described above some number (maybe, zero) times.A sequence $$$b$$$ is a subsequence of an array $$$c$$$ if $$$b$$$ can be obtained from $$$c$$$ by deletion of several (possibly, zero or all) elements.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 1; i <= t; i++) {\n int n = 0;\n scanf(\"%d\", &n);\n int a[n + 1];\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n }\n a[n] = a[n - 1];\n int flag = 1;\n for (int j = 0; j < n; j++) {\n if (a[j] != a[j + 1]) {\n\n flag = 0;\n break;\n }\n }\n\n if (flag == 1) {\n printf(\"0\\n\");\n } else {\n\n for (int j = 0; j < n - 1; j++) {\n if (a[j] > a[j + 1]) {\n int temp = a[j];\n a[j] = a[j + 1];\n a[j + 1] = temp;\n j = -1;\n }\n }\n int count = 0;\n\n for (int j = 0; j < n; j++) {\n if (a[j] == a[0]) {\n count++;\n }\n }\n printf(\"%d\\n\", n - count);\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 for l in i.lock().lines().skip(2).step_by(2) {\n let (l, c, _) = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .fold((0, 0, u32::MAX), |(l, c, m), x| {\n if x == m {\n (l + 1, c + 1, m)\n } else if x < m {\n (l + 1, 1, x)\n } else {\n (l + 1, c, m)\n }\n });\n writeln!(o, \"{}\", l - c).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "0539", "problem_description": "One day, Ahmed_Hossam went to Hemose and said \"Let's solve a gym contest!\". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?There is an Agent in Valorant, and he has $$$n$$$ weapons. The $$$i$$$-th weapon has a damage value $$$a_i$$$, and the Agent will face an enemy whose health value is $$$H$$$.The Agent will perform one or more moves until the enemy dies.In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die when his health will become less than or equal to $$$0$$$. However, not everything is so easy: the Agent can't choose the same weapon for $$$2$$$ times in a row.What is the minimum number of times that the Agent will need to use the weapons to kill the enemy?", "c_code": "int solution() {\n long long int t;\n scanf(\"%lld\", &t);\n while (t--) {\n long long int n;\n long long int h;\n\n long long int b = 0;\n long long int c = 0;\n scanf(\"%lld %lld\", &n, &h);\n long long int a[n];\n for (long long int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n if (a[i] > c) {\n if (a[i] > b) {\n c = b;\n b = a[i];\n } else {\n c = a[i];\n }\n }\n }\n if (h % (b + c) == 0) {\n printf(\"%lld\\n\", 2 * (h / (c + b)));\n } else if (h % (b + c) <= b) {\n printf(\"%lld\\n\", (2 * (h / (c + b))) + 1);\n } else {\n printf(\"%lld\\n\", (2 * (h / (c + b))) + 2);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut string = String::new();\n stdin.read_line(&mut string).unwrap();\n for _ in 0..string.trim().parse::().unwrap() {\n string.clear();\n stdin.read_line(&mut string).unwrap();\n let enemy = string\n .split_ascii_whitespace()\n .last()\n .unwrap()\n .parse::()\n .unwrap();\n\n string.clear();\n stdin.read_line(&mut string).unwrap();\n\n let mut max = 0;\n let mut sec_max = 0;\n string\n .split_ascii_whitespace()\n .map(|f| {\n let n = f.parse::().unwrap();\n if n > max {\n sec_max = max;\n max = n\n } else {\n sec_max = sec_max.max(n)\n }\n })\n .count();\n let base = enemy / (max + sec_max);\n\n let other = {\n if enemy != base * (max + sec_max) {\n let rest = enemy - base * (max + sec_max);\n if rest > max {\n 2\n } else {\n 1\n }\n } else {\n 0\n }\n };\n println!(\"{}\", base * 2 + other);\n }\n}", "difficulty": "hard"} {"problem_id": "0540", "problem_description": "You are given a binary string of length $$$n$$$ (i. e. a string consisting of $$$n$$$ characters '0' and '1').In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than $$$k$$$ moves? It is possible that you do not perform any moves at all.Note that you can swap the same pair of adjacent characters with indices $$$i$$$ and $$$i+1$$$ arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.You have to answer $$$q$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long int n;\n long long int k;\n long long int c = 0;\n scanf(\"%lld%lld\", &n, &k);\n char s[1000000] = {'\\0'};\n int f = 0;\n scanf(\"%s\", s);\n for (long long int i = 0; i < n; i++) {\n if (s[i] == '0' && f == 0) {\n if (k - i + c >= 0) {\n k = k - i + c;\n } else {\n c = i - k;\n f = 1;\n }\n s[i] = '1';\n s[c] = '0';\n c++;\n }\n }\n printf(\"%s\\n\", s);\n }\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let stdout = stdout();\n let mut input_line = String::new();\n stdin.read_line(&mut input_line).unwrap();\n let q = input_line.trim().parse::().unwrap();\n let mut b_string = String::new();\n\n for _ in 0..q {\n input_line.clear();\n stdin.read_line(&mut input_line).unwrap();\n let splited = input_line.split_whitespace().collect::>();\n let n = splited[0].parse::().unwrap();\n let mut k = splited[1].parse::().unwrap();\n b_string.clear();\n stdin.read_line(&mut b_string).unwrap();\n let b_string = unsafe { b_string.as_mut_vec() };\n\n let mut last_nonzero = None;\n for idx in 0..n {\n if k <= 0 {\n break;\n };\n if b_string[idx] == b'0' {\n if let Some(l_nonzero) = last_nonzero {\n let swap = std::cmp::min(k, (idx - l_nonzero) as i64);\n k -= swap;\n b_string[idx - swap as usize] = b'0';\n b_string[idx] = b'1';\n last_nonzero = Some(l_nonzero + 1);\n };\n } else if last_nonzero.is_none() {\n last_nonzero = Some(idx);\n }\n }\n stdout.lock().write(b_string).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0541", "problem_description": "A non-empty digit string is diverse if the number of occurrences of each character in it doesn't exceed the number of distinct characters in it.For example: string \"7\" is diverse because 7 appears in it $$$1$$$ time and the number of distinct characters in it is $$$1$$$; string \"77\" is not diverse because 7 appears in it $$$2$$$ times and the number of distinct characters in it is $$$1$$$; string \"1010\" is diverse because both 0 and 1 appear in it $$$2$$$ times and the number of distinct characters in it is $$$2$$$; string \"6668\" is not diverse because 6 appears in it $$$3$$$ times and the number of distinct characters in it is $$$2$$$. You are given a string $$$s$$$ of length $$$n$$$, consisting of only digits $$$0$$$ to $$$9$$$. Find how many of its $$$\\frac{n(n+1)}{2}$$$ substrings are diverse.A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.Note that if the same diverse string appears in $$$s$$$ multiple times, each occurrence should be counted independently. For example, there are two diverse substrings in \"77\" both equal to \"7\", so the answer for the string \"77\" is $$$2$$$.", "c_code": "int solution() {\n\n int i;\n int j;\n int t;\n int n;\n int l;\n int d = 0;\n int aux;\n int total;\n char s[100001];\n\n scanf(\"%d\", &t);\n\n while (t--) {\n scanf(\"%d\\n%s\", &n, s);\n\n total = 0;\n\n for (l = 2; l <= 100 && l <= n; l++) {\n for (i = 0; i < n - (l - 1); i++) {\n\n int dc[10] = {0};\n d = 0;\n\n for (j = 0; j < l; j++) {\n\n if (dc[s[i + j] - '0'] == 0) {\n d++;\n }\n\n dc[s[i + j] - '0'] += 1;\n }\n\n for (j = 0, aux = 0; j < 10 && dc[j] <= d && aux != l; j++) {\n aux += dc[j];\n }\n\n if (aux == l) {\n total++;\n }\n }\n }\n\n printf(\"%d\\n\", total + n);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(2)\n .step_by(2)\n .flatten()\n .for_each(|s| {\n let t = s.as_bytes();\n let mut ans = 0usize;\n for i in 0..s.len() {\n let mut a = [0usize; 10];\n let mut cnt = 0usize;\n let mut m = 0usize;\n for &e in t.iter().skip(i) {\n let d = usize::from(e.wrapping_sub(b'0'));\n if a.get(d) == Some(&0) {\n cnt = cnt.wrapping_add(1);\n }\n if let Some(f) = a.get_mut(d) {\n *f = f.wrapping_add(1);\n m = m.max(*f);\n }\n if m <= cnt {\n ans = ans.wrapping_add(1);\n }\n if m >= 11 {\n break;\n }\n }\n }\n writeln!(buf, \"{ans}\").ok();\n });\n}", "difficulty": "hard"} {"problem_id": "0542", "problem_description": "Another Codeforces Round has just finished! It has gathered $$$n$$$ participants, and according to the results, the expected rating change of participant $$$i$$$ is $$$a_i$$$. These rating changes are perfectly balanced — their sum is equal to $$$0$$$.Unfortunately, due to minor technical glitches, the round is declared semi-rated. It means that all rating changes must be divided by two.There are two conditions though: For each participant $$$i$$$, their modified rating change $$$b_i$$$ must be integer, and as close to $$$\\frac{a_i}{2}$$$ as possible. It means that either $$$b_i = \\lfloor \\frac{a_i}{2} \\rfloor$$$ or $$$b_i = \\lceil \\frac{a_i}{2} \\rceil$$$. In particular, if $$$a_i$$$ is even, $$$b_i = \\frac{a_i}{2}$$$. Here $$$\\lfloor x \\rfloor$$$ denotes rounding down to the largest integer not greater than $$$x$$$, and $$$\\lceil x \\rceil$$$ denotes rounding up to the smallest integer not smaller than $$$x$$$. The modified rating changes must be perfectly balanced — their sum must be equal to $$$0$$$. Can you help with that?", "c_code": "int solution() {\n int i = 0;\n scanf(\"%d\", &i);\n int ar[i];\n int ar2[i];\n int l = 0;\n while (l < i) {\n scanf(\"%d\", &ar[l]);\n l++;\n }\n while (l >= 0) {\n ar2[l] = ar[l];\n ar[l] = ar[l] / 2;\n l--;\n }\n l = 0;\n while (l < i) {\n int k = ar2[l] % 2;\n int j = 0;\n int m = 0;\n while (j < i) {\n m = ar[j] + m;\n j++;\n }\n\n if (m == 0) {\n break;\n }\n if (k < 0) {\n ar[l] = ar[l] - 1;\n } else if (k > 0) {\n ar[l] = ar[l] + 1;\n }\n\n l++;\n }\n l = 0;\n while (l < i) {\n printf(\"%d\\n\", ar[l]);\n l++;\n }\n return (0);\n}", "rust_code": "fn solution() -> io::Result<()> {\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n let mut buf = String::new();\n\n stdin.read_line(&mut buf)?;\n\n let n: usize = buf.trim().parse().unwrap();\n\n let mut a = vec![0; n];\n for i in 0..n {\n buf.clear();\n stdin.read_line(&mut buf)?;\n a[i] = buf.trim().parse::().unwrap();\n }\n\n let mut n_odd = 0;\n a.iter().for_each(|ai| {\n if ai % 2 != 0 {\n n_odd += 1\n }\n });\n assert!(n_odd % 2 == 0);\n\n n_odd /= 2;\n\n let mut b = vec![0; n];\n for i in 0..n {\n if a[i] % 2 == 0 {\n b[i] = a[i] / 2;\n } else {\n b[i] = (a[i] - (a[i] % 2 + 2) % 2) / 2;\n if n_odd > 0 {\n n_odd -= 1;\n b[i] += 1;\n }\n }\n }\n\n let mut sum = 0;\n b.iter().for_each(|bi| sum += bi);\n\n assert_eq!(sum, 0);\n\n b.iter().for_each(|bi| println!(\"{}\", bi));\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0543", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

We have N logs of lengths A_1,A_2,\\cdots A_N.

\n

We can cut these logs at most K times in total. When a log of length L is cut at a point whose distance from an end of the log is t (0<t<L), it becomes two logs of lengths t and L-t.

\n

Find the shortest possible length of the longest log after at most K cuts, and print it after rounding up to an integer.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • 0 \\leq K \\leq 10^9
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nA_1 A_2 \\cdots A_N\n
\n
\n
\n
\n
\n

Output

Print an integer representing the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3\n7 9\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n
    \n
  • First, we will cut the log of length 7 at a point whose distance from an end of the log is 3.5, resulting in two logs of length 3.5 each.
  • \n
  • Next, we will cut the log of length 9 at a point whose distance from an end of the log is 3, resulting in two logs of length 3 and 6.
  • \n
  • Lastly, we will cut the log of length 6 at a point whose distance from an end of the log is 3.3, resulting in two logs of length 3.3 and 2.7.
  • \n
\n

In this case, the longest length of a log will be 3.5, which is the shortest possible result. After rounding up to an integer, the output should be 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 0\n3 4 5\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 10\n158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202\n
\n
\n
\n
\n
\n

Sample Output 3

292638192\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n long k;\n scanf(\"%ld %ld\", &n, &k);\n long a[n];\n for (long i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n }\n long ng = 0;\n long ok = 1000000000;\n long center;\n long cut;\n while (ng + 1 < ok) {\n center = (ng + ok) / 2;\n cut = 0;\n for (long i = 0; i < n; i++) {\n cut += (a[i] + center - 1) / center - 1;\n }\n if (cut <= k) {\n ok = center;\n } else {\n ng = center;\n }\n }\n printf(\"%ld\\n\", ok);\n\n return 0;\n}", "rust_code": "fn solution() {\n let (n, k): (usize, i64) = {\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 a: 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 left = 0;\n let mut right = 1_000_000_000;\n while right - left > 1 {\n let mid = (left + right) / 2;\n let mut sum = 0;\n for i in 0..n {\n sum += (a[i] + mid - 1) / mid - 1;\n }\n if sum <= k {\n right = mid;\n } else {\n left = mid;\n }\n }\n println!(\"{}\", right);\n}", "difficulty": "easy"} {"problem_id": "0544", "problem_description": "You are given two integers $$$a$$$ and $$$b$$$.In one move, you can choose some integer $$$k$$$ from $$$1$$$ to $$$10$$$ and add it to $$$a$$$ or subtract it from $$$a$$$. In other words, you choose an integer $$$k \\in [1; 10]$$$ and perform $$$a := a + k$$$ or $$$a := a - k$$$. You may use different values of $$$k$$$ in different moves.Your task is to find the minimum number of moves required to obtain $$$b$$$ from $$$a$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n int a;\n int b;\n scanf(\"%d\", &t);\n\n for (int i = 0; i < t; i++) {\n int diff = 0;\n int quo = 0;\n int rem = 0;\n int moves = 0;\n scanf(\"%d %d\", &a, &b);\n diff = abs(a - b);\n quo = diff / 10;\n rem = diff % 10;\n if (diff != 0 && rem != 0) {\n moves = quo + 1;\n }\n\n else {\n moves = quo;\n }\n printf(\"%d\\n\", moves);\n }\n}", "rust_code": "fn solution() {\n let mut st = String::new();\n io::stdin().read_line(&mut st).expect(\"1\");\n let t: usize = st.trim().parse().expect(\"2\");\n let mut ans = Vec::with_capacity(t);\n for _ in 0..t {\n let mut ab = String::new();\n io::stdin().read_line(&mut ab).expect(\"3\");\n let mut c = ab.split_whitespace();\n let a: i64 = match c.next() {\n Some(e) => e.trim().parse().expect(\"4\"),\n None => 0,\n };\n let b: i64 = match c.next() {\n Some(e) => e.trim().parse().expect(\"4\"),\n None => 0,\n };\n ans.push(((a - b).abs() + 9) / 10)\n }\n println!(\n \"{}\",\n ans.iter()\n .map(|v| v.to_string())\n .collect::>()\n .join(\"\\n\")\n )\n}", "difficulty": "medium"} {"problem_id": "0545", "problem_description": "\n

(Please read problem A first. The maximum score you can get by solving this problem B is 1, which will have almost no effect on your ranking.)

\n
\n
\n

Beginner's Guide

Let's first write a program to calculate the score from a pair of input and output. You can know the total score by submitting your solution, or an official program to calculate a score is often provided for local evaluation as in this contest. Nevertheless, writing a score calculator by yourself is still useful to check your understanding of the problem specification. Moreover, the source code of the score calculator can often be reused for solving the problem or debugging your solution. So it is worthwhile to write a score calculator unless it is very complicated.

\n
\n
\n
\n
\n

Problem Statement

You will be given a contest schedule for D days.\nFor each d=1,2,\\ldots,D, calculate the satisfaction at the end of day d.

\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the form of the input of Problem A followed by the output of Problem A.

\n
D\nc_1 c_2 \\cdots c_{26}\ns_{1,1} s_{1,2} \\cdots s_{1,26}\n\\vdots\ns_{D,1} s_{D,2} \\cdots s_{D,26}\nt_1\nt_2\n\\vdots\nt_D\n
\n
    \n
  • The constraints and generation methods for the input part are the same as those for Problem A.
  • \n
  • For each d, t_d is an integer satisfying 1\\leq t_d \\leq 26, and your program is expected to work correctly for any value that meets the constraints.
  • \n
\n
\n
\n
\n
\n

Output

Let v_d be the satisfaction at the end of day d.\nPrint D integers v_d to Standard Output in the following format:

\n
v_1\nv_2\n\\vdots\nv_D\n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n1\n17\n13\n14\n13\n
\n
\n
\n
\n
\n

Sample Output 1

18398\n35037\n51140\n65837\n79325\n
\n

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case.

\n
\n
\n
\n
\n

Next Step

We can build a solution (schedule) for this problem in the order of day 1, day 2, and so on. And for every partial solution we have built, we can calculate the goodness (satisfaction) by using the above score calculator. So we can construct the following algorithm: for each d=1,2,\\ldots,D, we select the contest type that maximizes the satisfaction at the end of day d. You may have already encountered this kind of \"greedy algorithms\" in algorithm contests such as ABC. Greedy algorithms can guarantee the optimality for several problems, but unfortunately, it doesn't ensure optimality for this problem. However, even if it does not ensure optimality, we can still obtain a reasonable solution in many cases. Let's go back to Problem A and implement the greedy algorithm by utilizing the score calculator you just implemented!

\n

Greedy methods can be applied to a variety of problems, are easy to implement, and often run relatively fast compared to other methods. Greedy is often the most powerful method when we need to process huge inputs.\nWe can further improve the score by changing the greedy selection criteria (evaluation function), keeping multiple candidates instead of focusing on one best partial solution (beam search), or using the output of greedy algorithms as an initial solution of other methods.\nFor more information, please refer to the editorial that will be published after the contest.

\n
\n
", "c_code": "int solution(void) {\n\n int d;\n scanf(\"%d\", &d);\n long c[26];\n for (int i = 0; i < 26; i++) {\n scanf(\"%ld\", &c[i]);\n }\n long s[d][26];\n for (int i = 0; i < d; i++) {\n for (int j = 0; j < 26; j++) {\n scanf(\"%ld\", &s[i][j]);\n }\n }\n int t[d];\n for (int i = 0; i < d; i++) {\n scanf(\"%d\", &t[i]);\n }\n int last[26];\n for (int i = 0; i < 26; i++) {\n last[i] = -1;\n }\n long score = 0;\n for (int i = 0; i < d; i++) {\n score += s[i][t[i] - 1];\n last[t[i] - 1] = i;\n for (int j = 0; j < 26; j++) {\n if (j == t[i] - 1) {\n continue;\n }\n score -= c[j] * (i - last[j]);\n }\n printf(\"%ld\\n\", score);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let d: usize = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().parse().unwrap()\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 let mut s: Vec> = Vec::with_capacity(d);\n for _ in 0..d {\n let a: 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 s.push(a);\n }\n let s = s;\n\n let mut ans: i64 = 0;\n let mut last: Vec = vec![0; 26];\n for i in 1..=d {\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() - 1\n };\n ans += s[i - 1][t];\n last[t] = i;\n for j in 0..26 {\n ans -= c[j] * ((i - last[j]) as i64);\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "0546", "problem_description": "Boboniu likes bit operations. He wants to play a game with you.Boboniu gives you two sequences of non-negative integers $$$a_1,a_2,\\ldots,a_n$$$ and $$$b_1,b_2,\\ldots,b_m$$$.For each $$$i$$$ ($$$1\\le i\\le n$$$), you're asked to choose a $$$j$$$ ($$$1\\le j\\le m$$$) and let $$$c_i=a_i\\& b_j$$$, where $$$\\&$$$ denotes the bitwise AND operation. Note that you can pick the same $$$j$$$ for different $$$i$$$'s.Find the minimum possible $$$c_1 | c_2 | \\ldots | c_n$$$, where $$$|$$$ denotes the bitwise OR operation.", "c_code": "int solution() {\n int n;\n int m;\n int i;\n int j;\n int k;\n int ans = 0;\n scanf(\"%d%d\", &n, &m);\n int a[n];\n int b[m];\n int c[n];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (i = 0; i < m; i++) {\n scanf(\"%d\", &b[i]);\n }\n int size = pow(2, 9) - 1;\n int arr[size];\n int arr_index = 0;\n for (i = 0; i <= size; i++) {\n int count = 0;\n for (j = 0; j < n; j++) {\n for (k = 0; k < m; k++) {\n if (((a[j] & b[k]) | i) == i) {\n count++;\n break;\n }\n }\n }\n if (count == n) {\n arr[arr_index] = i;\n arr_index++;\n }\n }\n ans = arr[0];\n printf(\"%d\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let (n, m): (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 a: 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 b: 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 'outer: for x in 0..(1 << 9) {\n for i in 0..n {\n let mut ng = true;\n for j in 0..m {\n if x | (a[i] & b[j]) == x {\n ng = false;\n break;\n }\n }\n if ng {\n continue 'outer;\n }\n }\n println!(\"{}\", x);\n return;\n }\n}", "difficulty": "medium"} {"problem_id": "0547", "problem_description": "Let's call an array $$$a$$$ consisting of $$$n$$$ positive (greater than $$$0$$$) integers beautiful if the following condition is held for every $$$i$$$ from $$$1$$$ to $$$n$$$: either $$$a_i = 1$$$, or at least one of the numbers $$$a_i - 1$$$ and $$$a_i - 2$$$ exists in the array as well.For example: the array $$$[5, 3, 1]$$$ is beautiful: for $$$a_1$$$, the number $$$a_1 - 2 = 3$$$ exists in the array; for $$$a_2$$$, the number $$$a_2 - 2 = 1$$$ exists in the array; for $$$a_3$$$, the condition $$$a_3 = 1$$$ holds; the array $$$[1, 2, 2, 2, 2]$$$ is beautiful: for $$$a_1$$$, the condition $$$a_1 = 1$$$ holds; for every other number $$$a_i$$$, the number $$$a_i - 1 = 1$$$ exists in the array; the array $$$[1, 4]$$$ is not beautiful: for $$$a_2$$$, neither $$$a_2 - 2 = 2$$$ nor $$$a_2 - 1 = 3$$$ exists in the array, and $$$a_2 \\ne 1$$$; the array $$$[2]$$$ is not beautiful: for $$$a_1$$$, neither $$$a_1 - 1 = 1$$$ nor $$$a_1 - 2 = 0$$$ exists in the array, and $$$a_1 \\ne 1$$$; the array $$$[2, 1, 3]$$$ is beautiful: for $$$a_1$$$, the number $$$a_1 - 1 = 1$$$ exists in the array; for $$$a_2$$$, the condition $$$a_2 = 1$$$ holds; for $$$a_3$$$, the number $$$a_3 - 2 = 1$$$ exists in the array. You are given a positive integer $$$s$$$. Find the minimum possible size of a beautiful array with the sum of elements equal to $$$s$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int output[t];\n for (int i = 0; i < t; i++) {\n int s;\n scanf(\"%d\", &s);\n int j = 1;\n while (s >= j * j) {\n j++;\n }\n output[i] = j - 1;\n if (s - 2 * j + 3 && s - j * j + 2 * j - 1) {\n output[i] += 1;\n }\n }\n for (int i = 0; i < t; i++) {\n printf(\"%d\\n\", output[i]);\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(1) {\n writeln!(o, \"{}\", l.unwrap().parse::().unwrap().sqrt().ceil()).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "0548", "problem_description": "\n

Score: 500 points

\n
\n
\n

Problem Statement

\n

New AtCoder City has an infinite grid of streets, as follows:

\n
    \n
  • At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point.
  • \n
  • A straight street, which we will call East-West Main Street, runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane.
  • \n
  • There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \\ldots, y = -2, y = -1, y = 1, y = 2, \\ldots in the two-dimensional coordinate plane.
  • \n
  • A straight street, which we will call North-South Main Street, runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane.
  • \n
  • There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \\ldots, x = -2, x = -1, x = 1, x = 2, \\ldots in the two-dimensional coordinate plane.
  • \n
\n

There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas.

\n

The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.
\nM-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets.

\n

Let the walking distance of each citizen be the distance from his/her residential area to the nearest railroad.
\nM-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized.

\n

For each K = 0, 1, 2, \\dots, N, what is the minimum possible value of S after building railroads?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 15
  • \n
  • -10 \\ 000 \\leq X_i \\leq 10 \\ 000
  • \n
  • -10 \\ 000 \\leq Y_i \\leq 10 \\ 000
  • \n
  • 1 \\leq P_i \\leq 1 \\ 000 \\ 000
  • \n
  • The locations of the N residential areas, (X_i, Y_i), are all distinct.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nX_1 Y_1 P_1\nX_2 Y_2 P_2\n :  :  :\nX_N Y_N P_N\n
\n
\n
\n
\n
\n

Output

\n

Print the answer in N+1 lines.
\nThe i-th line (i = 1, \\ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 2 300\n3 3 600\n1 4 800\n
\n
\n
\n
\n
\n

Sample Output 1

2900\n900\n0\n0\n
\n

When K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1, 3, and 1, respectively, to reach a railroad.
\nThus, the sum of the walking distances of all citizens, S, is 1 \\times 300 + 3 \\times 600 + 1 \\times 800 = 2900.

\n

When K = 1, if we build a railroad along the street corresponding to the line y = 4 in the coordinate plane, the walking distances of the citizens of Area 1, 2, and 3 become 1, 1, and 0, respectively.
\nThen, S = 1 \\times 300 + 1 \\times 600 + 0 \\times 800 = 900.
\nWe have many other options for where we build the railroad, but none of them makes S less than 900.

\n

When K = 2, if we build a railroad along the street corresponding to the lines x = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad with the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.

\n

The figure below shows the optimal way to build railroads for the cases K = 0, 1, 2.
\nThe street painted blue represents the roads along which we build railroads.

\n

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n3 5 400\n5 3 700\n5 5 1000\n5 7 700\n7 5 400\n
\n
\n
\n
\n
\n

Sample Output 2

13800\n1600\n0\n0\n0\n0\n
\n

The figure below shows the optimal way to build railroads for the cases K = 1, 2.

\n

\n
\n
\n
\n
\n
\n

Sample Input 3

6\n2 5 1000\n5 2 1100\n5 5 1700\n-2 -5 900\n-5 -2 600\n-5 -5 2200\n
\n
\n
\n
\n
\n

Sample Output 3

26700\n13900\n3200\n1200\n0\n0\n0\n
\n

The figure below shows the optimal way to build railroads for the case K = 3.

\n

\n
\n
\n
\n
\n
\n

Sample Input 4

8\n2 2 286017\n3 1 262355\n2 -2 213815\n1 -3 224435\n-2 -2 136860\n-3 -1 239338\n-2 2 217647\n-1 3 141903\n
\n
\n
\n
\n
\n

Sample Output 4

2576709\n1569381\n868031\n605676\n366338\n141903\n0\n0\n0\n
\n

The figure below shows the optimal way to build railroads for the case K = 4.

\n

\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int X[16];\n int Y[16];\n int P[16];\n scanf(\"%d\", &N);\n for (i = 0; i < N; i++) {\n scanf(\"%d %d %d\", &(X[i]), &(Y[i]), &(P[i]));\n }\n\n int j;\n int k;\n int dist[2][32768][16];\n int num[32768] = {};\n const int bit[16] = {1, 2, 4, 8, 16, 32, 64, 128,\n 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};\n for (i = 0; i < bit[N]; i++) {\n for (k = 0; k < N; k++) {\n if ((i & bit[k]) != 0) {\n num[i]++;\n }\n }\n for (j = 0; j < N; j++) {\n dist[0][i][j] = abs(X[j]);\n dist[1][i][j] = abs(Y[j]);\n for (k = 0; k < N; k++) {\n if ((i & bit[k]) == 0) {\n continue;\n }\n if (abs(X[j] - X[k]) < dist[0][i][j]) {\n dist[0][i][j] = abs(X[j] - X[k]);\n }\n if (abs(Y[j] - Y[k]) < dist[1][i][j]) {\n dist[1][i][j] = abs(Y[j] - Y[k]);\n }\n }\n }\n }\n\n const long long sup = (long long)1 << 60;\n long long ans[16];\n long long tmp;\n for (i = 0, ans[N] = 0; i < N; i++) {\n ans[i] = sup;\n }\n for (i = 0; i < bit[N]; i++) {\n for (j = 0; j < bit[N]; j++) {\n if (num[i] + num[j] >= N - 4) {\n continue;\n }\n for (k = 0, tmp = 0; k < N; k++) {\n tmp +=\n (long long)P[k] *\n ((dist[0][i][k] < dist[1][j][k]) ? dist[0][i][k] : dist[1][j][k]);\n }\n if (tmp < ans[num[i] + num[j]]) {\n ans[num[i] + num[j]] = tmp;\n }\n }\n }\n\n int l;\n long long min;\n long long max[16];\n for (k = 0; k < N; k++) {\n max[k] =\n (long long)P[k] * ((abs(X[k]) < abs(Y[k])) ? abs(X[k]) : abs(Y[k]));\n }\n for (i = 0; i < bit[N]; i++) {\n if (num[i] < N - 4) {\n continue;\n }\n for (j = 0; j < bit[N]; j++) {\n if ((i & j) != j) {\n continue;\n }\n for (k = 0, tmp = 0; k < N; k++) {\n if ((i & bit[k]) != 0) {\n continue;\n }\n for (l = 0, min = max[k]; l < N; l++) {\n if ((i & bit[l]) == 0) {\n continue;\n }\n if ((j & bit[l]) == 0 && (long long)P[k] * abs(X[k] - X[l]) < min) {\n min = (long long)P[k] * abs(X[k] - X[l]);\n }\n if ((j & bit[l]) != 0 && (long long)P[k] * abs(Y[k] - Y[l]) < min) {\n min = (long long)P[k] * abs(Y[k] - Y[l]);\n }\n }\n tmp += min;\n }\n if (tmp < ans[num[i]]) {\n ans[num[i]] = tmp;\n }\n }\n }\n\n for (i = 0; i <= N; i++) {\n printf(\"%lld\\n\", ans[i]);\n }\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let stdout = std::io::stdout();\n let mut reader = std::io::BufReader::new(stdin.lock());\n let mut writer = std::io::BufWriter::new(stdout.lock());\n\n let n: usize = {\n let mut buf = String::new();\n reader.read_line(&mut buf).unwrap();\n buf.trim_end().parse().unwrap()\n };\n let mut x: Vec = vec![0; n];\n let mut y: Vec = vec![0; n];\n let mut p: Vec = vec![0; n];\n for i in 0..n {\n let mut buf = String::new();\n reader.read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n x[i] = iter.next().unwrap().parse().unwrap();\n y[i] = iter.next().unwrap().parse().unwrap();\n p[i] = iter.next().unwrap().parse().unwrap();\n }\n\n let mut a: Vec> = vec![vec![std::i64::MAX; n]; 1 << n];\n let mut b: Vec> = vec![vec![std::i64::MAX; n]; 1 << n];\n for i in 0..(1 << n) {\n for j in 0..n {\n a[i][j] = cmp::min(a[i][j], x[j].abs());\n b[i][j] = cmp::min(b[i][j], y[j].abs());\n if i & 1 << j == 0 {\n continue;\n }\n for k in 0..n {\n a[i][k] = cmp::min(a[i][k], (x[j] - x[k]).abs());\n b[i][k] = cmp::min(b[i][k], (y[j] - y[k]).abs());\n }\n }\n }\n\n let pow = |mut a: usize, mut n: usize| {\n let mut res = 1;\n while n > 0 {\n if n & 1 > 0 {\n res *= a;\n }\n a *= a;\n n >>= 1;\n }\n res\n };\n\n let mut ans = vec![std::i64::MAX; n + 1];\n for i in 0..pow(3, n) {\n let mut c = 0;\n let mut x = i;\n let mut s = 0;\n let mut t = 0;\n for j in 0..n {\n let y = x % 3;\n if y == 1 {\n s += 1 << j;\n c += 1;\n } else if y == 2 {\n t += 1 << j;\n c += 1;\n }\n x /= 3;\n }\n\n let mut d = vec![std::i64::MAX; n];\n for i in 0..n {\n d[i] = cmp::min(a[s][i], b[t][i]);\n }\n let res = { d.iter().enumerate().fold(0, |acc, (i, k)| acc + p[i] * k) };\n ans[c] = cmp::min(ans[c], res);\n }\n\n for i in 0..=n {\n writeln!(writer, \"{}\", ans[i]).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0549", "problem_description": "Merge sort", "c_code": "void solution(int *a, int n) {\n int curr_size, left_start;\n int *b = (int *)malloc(n * sizeof(int));\n\n for (curr_size = 1; curr_size <= n - 1; curr_size = 2 * curr_size) {\n for (left_start = 0; left_start < n - 1; left_start += 2 * curr_size) {\n int l = left_start;\n int mid = (left_start + curr_size - 1 < n - 1)\n ? left_start + curr_size - 1\n : n - 1;\n\n int r = (left_start + 2 * curr_size - 1 < n - 1)\n ? left_start + 2 * curr_size - 1\n : n - 1;\n\n int p1 = l;\n int p2 = mid + 1;\n int c = l;\n\n while (p1 <= mid && p2 <= r) {\n if (a[p1] <= a[p2])\n b[c++] = a[p1++];\n else\n b[c++] = a[p2++];\n }\n\n while (p1 <= mid)\n b[c++] = a[p1++];\n\n while (p2 <= r)\n b[c++] = a[p2++];\n\n for (c = l; c <= r; c++)\n a[c] = b[c];\n }\n }\n\n free(b);\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n if arr.len() <= 1 {\n return;\n }\n\n let len = arr.len();\n let mut sub_array_size = 1;\n\n while sub_array_size < len {\n let mut start_index = 0;\n\n while len - start_index > sub_array_size {\n let end_idx = if start_index + 2 * sub_array_size > len {\n len\n } else {\n start_index + 2 * sub_array_size\n };\n\n let mid = sub_array_size;\n\n let left_half = arr[start_index..start_index + mid].to_vec();\n let right_half = arr[start_index + mid..end_idx].to_vec();\n\n let mut l = 0;\n let mut r = 0;\n\n for v in &mut arr[start_index..end_idx] {\n if r == right_half.len() || (l < left_half.len() && left_half[l] < right_half[r]) {\n *v = left_half[l];\n l += 1;\n } else {\n *v = right_half[r];\n r += 1;\n }\n }\n\n start_index = end_idx;\n }\n\n sub_array_size *= 2;\n }\n}\n", "difficulty": "medium"} {"problem_id": "0550", "problem_description": "A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \\dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \\dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int v[n];\n printf(\"%d\\n\", n);\n for (int i = 1; i <= n; i++) {\n v[i - 1] = i;\n printf(\"%d \", i);\n }\n printf(\"\\n\");\n for (int i = n - 1; i > 0; i--) {\n int temp = v[i];\n v[i] = v[i - 1];\n v[i - 1] = temp;\n for (int f = 0; f < n; f++) {\n printf(\"%d \", v[f]);\n }\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let out = &mut BufWriter::new(stdout());\n\n let mut input = String::new();\n stdin().read_line(&mut input).expect(\"Failed read\");\n let t = input.trim().parse::().unwrap();\n\n for _ in 0..t {\n input = String::new();\n stdin().read_line(&mut input).expect(\"Failed read\");\n let n = input.trim().parse::().unwrap();\n let mut v: Vec = (1..n + 1).collect();\n writeln!(out, \"{}\", n).ok();\n for a in &v {\n write!(out, \"{} \", a).ok();\n }\n writeln!(out).ok();\n for i in 1..n {\n v.swap((n - i) as usize, (n - i - 1) as usize);\n for a in &v {\n write!(out, \"{} \", a).ok();\n }\n writeln!(out).ok();\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0551", "problem_description": "Let's define $$$S(x)$$$ to be the sum of digits of number $$$x$$$ written in decimal system. For example, $$$S(5) = 5$$$, $$$S(10) = 1$$$, $$$S(322) = 7$$$.We will call an integer $$$x$$$ interesting if $$$S(x + 1) < S(x)$$$. In each test you will be given one integer $$$n$$$. Your task is to calculate the number of integers $$$x$$$ such that $$$1 \\le x \\le n$$$ and $$$x$$$ is interesting.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n if (n >= 10) {\n if (n % 10 == 9) {\n printf(\"%i \\n\", (n + 1) / 10);\n } else {\n printf(\"%i \\n\", (n / 10));\n }\n } else if (n == 9) {\n printf(\"1 \\n\");\n } else if (n < 9) {\n printf(\"0 \\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).expect(\"\");\n let mut t: i32 = t.trim().parse().expect(\"\");\n\n while t > 0 {\n let mut n = String::new();\n std::io::stdin().read_line(&mut n).expect(\"\");\n let n: u64 = n.trim().parse().expect(\"\");\n println!(\"{}\", (n + 1) / 10);\n t -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "0552", "problem_description": "A little boy Gerald entered a clothes shop and found out something very unpleasant: not all clothes turns out to match. For example, Gerald noticed that he looks rather ridiculous in a smoking suit and a baseball cap.Overall the shop sells n clothing items, and exactly m pairs of clothing items match. Each item has its price, represented by an integer number of rubles. Gerald wants to buy three clothing items so that they matched each other. Besides, he wants to spend as little money as possible. Find the least possible sum he can spend.", "c_code": "int solution() {\n char triples[101][101];\n int i;\n int j;\n int k;\n int l;\n int m;\n int n;\n int cost[101];\n int aux;\n int min = INT_MAX;\n\n scanf(\"%d %d\", &n, &m);\n\n for (i = 1; i <= n; i++) {\n for (j = 1; j <= n; j++) {\n triples[i][j] = 0;\n }\n }\n\n for (i = 1; i <= n; i++) {\n scanf(\"%d\", &cost[i]);\n }\n\n for (k = 0; k < m; k++) {\n scanf(\"%d %d\", &i, &j);\n\n triples[i][j] = 1;\n triples[j][i] = 1;\n\n aux = cost[i] + cost[j];\n\n if (aux < min) {\n for (l = 1; l <= n; l++) {\n if (triples[i][l] && triples[j][l]) {\n if (min > (aux + cost[l])) {\n min = (aux + cost[l]);\n }\n }\n }\n }\n }\n\n printf(\"%d\\n\", (min < INT_MAX ? min : -1));\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin: io::Stdin = io::stdin();\n let mut lines: io::Lines = stdin.lock().lines();\n\n let first_line = lines.next().unwrap().unwrap();\n\n let flv: Vec<_> = first_line\n .trim()\n .split(' ')\n .map(|n| n.parse::().unwrap())\n .collect();\n\n let n = flv[0];\n let _m = flv[1];\n\n let cost: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .trim()\n .split(' ')\n .map(|n| n.parse().unwrap())\n .collect();\n\n let mut reach: Vec> = vec![];\n reach.reserve(n as usize);\n for _ in cost.iter() {\n reach.push(HashSet::new());\n }\n\n let mut pairs: Vec> = vec![];\n for l in lines {\n let lv: Vec = l\n .unwrap()\n .trim()\n .split(' ')\n .map(|n| n.parse::().unwrap() - 1)\n .collect();\n\n reach[lv[0] as usize].insert(lv[1]);\n reach[lv[1] as usize].insert(lv[0]);\n pairs.push(lv);\n }\n\n let mut min_cost = ::max_value();\n let mut has_solution = false;\n for p in pairs {\n for i in 0..n {\n if reach[p[0] as usize].contains(&i) && reach[p[1] as usize].contains(&i) {\n has_solution = true;\n let cur_cost = cost[p[0] as usize] + cost[p[1] as usize] + cost[i as usize];\n if cur_cost < min_cost {\n min_cost = cur_cost;\n }\n }\n }\n }\n\n if has_solution {\n println!(\"{}\", min_cost);\n } else {\n println!(\"{}\", -1);\n }\n}", "difficulty": "medium"} {"problem_id": "0553", "problem_description": "You are given an integer $$$n$$$.You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: Replace $$$n$$$ with $$$\\frac{n}{2}$$$ if $$$n$$$ is divisible by $$$2$$$; Replace $$$n$$$ with $$$\\frac{2n}{3}$$$ if $$$n$$$ is divisible by $$$3$$$; Replace $$$n$$$ with $$$\\frac{4n}{5}$$$ if $$$n$$$ is divisible by $$$5$$$. For example, you can replace $$$30$$$ with $$$15$$$ using the first operation, with $$$20$$$ using the second operation or with $$$24$$$ using the third operation.Your task is to find the minimum number of moves required to obtain $$$1$$$ from $$$n$$$ or say that it is impossible to do it.You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int count = 0;\n int amount;\n long long int number[1001];\n scanf(\"%d\", &amount);\n\n for (long long int i = 0; i < amount; i++) {\n scanf(\"%lld\", &number[i]);\n }\n\n for (long long int i = 0; i < amount; i++) {\n\n while (1) {\n\n if (number[i] % 5 == 0) {\n number[i] = (4 * number[i]) / 5;\n count++;\n } else if (number[i] % 5 != 0 && number[i] % 3 == 0) {\n number[i] = (2 * number[i]) / 3;\n count++;\n } else if (number[i] % 5 != 0 && number[i] % 3 != 0 &&\n number[i] % 2 == 0) {\n number[i] = number[i] / 2;\n count++;\n }\n if (number[i] != 1 && number[i] % 5 != 0 && number[i] % 3 != 0 &&\n number[i] % 2 != 0) {\n count = -1;\n printf(\"%d\\n\", count);\n count = 0;\n break;\n }\n if (number[i] == 1) {\n printf(\"%d\\n\", count);\n count = 0;\n break;\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n\n io::stdin().read_line(&mut t).unwrap();\n let mut t: u32 = t.trim().parse().unwrap();\n\n while t != 0 {\n t -= 1;\n let mut q = String::new();\n io::stdin().read_line(&mut q).unwrap();\n let mut q: u64 = q.trim().parse::().unwrap();\n\n let mut count = 0;\n\n loop {\n count += 1;\n\n if q == 1 {\n println!(\"{}\", count - 1);\n break;\n }\n\n if q.is_multiple_of(2) {\n q /= 2;\n continue;\n }\n\n if q.is_multiple_of(3) {\n q = 2 * q / 3;\n continue;\n }\n\n if q.is_multiple_of(5) {\n q = 4 * q / 5;\n continue;\n }\n\n break;\n }\n\n if q != 1 {\n println!(\"-1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0554", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.\nIn one operation, you can select a term and either increment or decrement it by one.

\n

At least how many operations are necessary to satisfy the following conditions?

\n
    \n
  • For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
  • \n
  • For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ n ≤ 10^5
  • \n
  • |a_i| ≤ 10^9
  • \n
  • Each a_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n\na_1 a_2 ... a_n\n
\n
\n
\n
\n
\n

Output

Print the minimum necessary count of operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n1 -3 1 0\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n3 -6 4 -5 7\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

The given sequence already satisfies the conditions.

\n
\n
\n
\n
\n
\n

Sample Input 3

6\n-1 4 3 2 -5 4\n
\n
\n
\n
\n
\n

Sample Output 3

8\n
\n
\n
", "c_code": "int solution(void) {\n\n int n;\n\n scanf(\"%d\", &n);\n\n long long a[n];\n long long sum = 0;\n long long c = 0;\n\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n\n long long b[n];\n\n for (int i = 0; i < n; i++) {\n b[i] = a[i];\n }\n\n if (b[0] <= 0) {\n c = 1 - b[0];\n b[0] = 1;\n }\n sum = b[0];\n\n for (int i = 1; i < n; i++) {\n if (sum * (sum + b[i]) >= 0) {\n if (sum > 0) {\n c += b[i] + 1 + sum;\n b[i] = -1 - sum;\n } else if (sum < 0) {\n c += 1 - sum - b[i];\n b[i] = 1 - sum;\n }\n }\n sum += b[i];\n }\n\n long long d = 0;\n\n if (a[0] >= 0) {\n d = a[0] + 1;\n a[0] = -1;\n }\n sum = a[0];\n\n for (int i = 1; i < n; i++) {\n if (sum * (sum + a[i]) >= 0) {\n if (sum > 0) {\n d += a[i] + 1 + sum;\n a[i] = -1 - sum;\n } else if (sum < 0) {\n d += 1 - sum - a[i];\n a[i] = 1 - sum;\n }\n }\n sum += a[i];\n }\n\n if (c > d) {\n printf(\"%lld\\n\", d);\n } else {\n printf(\"%lld\\n\", c);\n }\n\n return 0;\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 mut a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n for i in 0..n - 1 {\n a[i + 1] += a[i];\n }\n\n let mut ans1 = 0;\n let mut ans2 = 0;\n let mut diff = 0i64;\n for i in 0..n {\n if i % 2 == 0 && a[i] + diff <= 0 {\n ans1 += 1 - (a[i] + diff);\n diff += 1 - (a[i] + diff);\n }\n if i % 2 == 1 && a[i] + diff >= 0 {\n ans1 += a[i] + diff + 1;\n diff -= a[i] + diff + 1;\n }\n }\n diff = 0;\n for i in 0..n {\n if i % 2 == 0 && a[i] + diff >= 0 {\n ans2 += a[i] + diff + 1;\n diff -= a[i] + diff + 1;\n }\n if i % 2 == 1 && a[i] + diff <= 0 {\n ans2 += 1 - (a[i] + diff);\n diff += 1 - (a[i] + diff);\n }\n }\n println!(\"{}\", min(ans1, ans2));\n}", "difficulty": "hard"} {"problem_id": "0555", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You have a digit sequence S of length 4. You are wondering which of the following formats S is in:

\n
    \n
  • YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order
  • \n
  • MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order
  • \n
\n

If S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.

\n
\n
\n
\n
\n

Constraints

    \n
  • S is a digit sequence of length 4.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the specified string: YYMM, MMYY, AMBIGUOUS or NA.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1905\n
\n
\n
\n
\n
\n

Sample Output 1

YYMM\n
\n

May XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format.

\n
\n
\n
\n
\n
\n

Sample Input 2

0112\n
\n
\n
\n
\n
\n

Sample Output 2

AMBIGUOUS\n
\n

Both December XX01 and January XX12 are valid dates. Thus, this string is valid in both formats.

\n
\n
\n
\n
\n
\n

Sample Input 3

1700\n
\n
\n
\n
\n
\n

Sample Output 3

NA\n
\n

Neither 0 nor 17 is valid as a month. Thus, this string is valid in neither format.

\n
\n
", "c_code": "int solution() {\n char s[4];\n scanf(\"%s\", s);\n int f = (10 * (s[0] - '0')) + s[1] - '0';\n\n int b = (10 * (s[2] - '0')) + s[3] - '0';\n if (f >= 1 && f <= 12 && b >= 1 && b <= 12) {\n printf(\"AMBIGUOUS\\n\");\n } else if (f >= 0 && f < 100 && b >= 1 && b <= 12) {\n printf(\"YYMM\\n\");\n } else if (f >= 1 && f <= 12 && b >= 0 && b < 100) {\n printf(\"MMYY\\n\");\n } else {\n printf(\"NA\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let _ = std::io::stdin().read_line(&mut buf);\n let (b, a) = buf.trim().split_at(2);\n let b = b.parse::().unwrap();\n let a = a.parse::().unwrap();\n let b = !(1..=12).contains(&b);\n let a = !(1..=12).contains(&a);\n if b && a {\n println!(\"NA\");\n } else if !b && a {\n println!(\"MMYY\");\n } else if b && !a {\n println!(\"YYMM\");\n } else {\n println!(\"AMBIGUOUS\");\n }\n}", "difficulty": "easy"} {"problem_id": "0556", "problem_description": "

Digit Number

\n\n

\nWrite a program which computes the digit number of sum of two integers a and b.\n

\n\n

Input

\n\n

\nThere are several test cases. Each test case consists of two non-negative integers a and b which are separeted by a space in a line. The input terminates with EOF.\n

\n\n

Constraints

\n\n
    \n
  • 0 ≤ a, b ≤ 1,000,000
  • \n
  • The number of datasets ≤ 200
  • \n
\n\n

Output

\n\n

\nPrint the number of digits of a + b for each data set.\n

\n\n

Sample Input

\n\n
\n5 7\n1 99\n1000 999\n
\n\n

Output for the Sample Input

\n\n
\n2\n3\n4\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int i = 0;\n int sum = 0;\n\n while (scanf(\"%d %d\", &a, &b) != EOF) {\n sum = a + b;\n\n for (i = 1; i < 15; i++) {\n if (sum < 10) {\n printf(\"%d\\n\", i);\n break;\n }\n sum = sum / 10;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input;\n let mut total: u64;\n loop {\n input = String::new();\n if let Ok(0) = std::io::stdin().read_line(&mut input) {\n break;\n }\n total = 0;\n for i in input.split_whitespace() {\n total += i.parse::().expect(\"\");\n }\n println!(\"{}\", total.to_string().len());\n }\n}", "difficulty": "hard"} {"problem_id": "0557", "problem_description": "You have an array of integers (initially empty).You have to perform $$$q$$$ queries. Each query is of one of two types: \"$$$1$$$ $$$x$$$\" — add the element $$$x$$$ to the end of the array; \"$$$2$$$ $$$x$$$ $$$y$$$\" — replace all occurrences of $$$x$$$ in the array with $$$y$$$. Find the resulting array after performing all the queries.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int *a = malloc(3000000);\n int *b = malloc(3000000);\n int *c = malloc(3000000);\n int *d = malloc(3000000);\n int *p = malloc(3000000);\n for (int n1 = 0; n1 < n; n1++) {\n scanf(\"%d\", p + n1);\n if (p[n1] == 1) {\n scanf(\"%d\", a + n1);\n } else {\n scanf(\"%d %d\", a + n1, b + n1);\n }\n }\n int d0 = 0;\n for (int n1 = 1; n1 <= 500000; n1++) {\n c[n1] = n1;\n }\n for (int n1 = n - 1; n1 >= 0; n1--) {\n if (p[n1] == 2) {\n c[a[n1]] = c[b[n1]];\n } else {\n d[d0] = c[a[n1]];\n d0++;\n }\n }\n int n2;\n for (n2 = d0 - 1; n2 > 5; n2 = n2 - 5) {\n printf(\"%d %d %d %d %d \", d[n2], d[n2 - 1], d[n2 - 2], d[n2 - 3],\n d[n2 - 4]);\n }\n for (; n2 >= 0; n2--) {\n printf(\"%d \", d[n2]);\n }\n}", "rust_code": "fn solution() {\n let mut cin = String::new();\n std::io::stdin().read_to_string(&mut cin).unwrap();\n let mut cin = cin.split_ascii_whitespace().flat_map(str::parse::<_>);\n\n\n cin![q];\n let mut p = 0;\n let mut ps = vec![HashSet::new(); 1 << 19];\n for _ in 1..=q {\n cin![op x];\n if op == 1 {\n ps[x].insert(p);\n p += 1;\n } else {\n cin![y];\n if x == y {\n continue;\n }\n if ps[y].len() < ps[x].len() {\n ps.swap(x, y);\n }\n let tv = ps[x].drain().collect::>();\n for p in tv {\n ps[y].insert(p);\n }\n }\n }\n let mut v = vec![0; p];\n for i in 1..=500000 {\n for p in &ps[i] {\n v[*p] = i;\n }\n }\n let mut s = String::new();\n for v in v {\n s += &format!(\"{} \", v);\n }\n s.pop();\n println!(\"{}\", s)\n}", "difficulty": "hard"} {"problem_id": "0558", "problem_description": "Santa has $$$n$$$ candies and he wants to gift them to $$$k$$$ kids. He wants to divide as many candies as possible between all $$$k$$$ kids. Santa can't divide one candy into parts but he is allowed to not use some candies at all.Suppose the kid who recieves the minimum number of candies has $$$a$$$ candies and the kid who recieves the maximum number of candies has $$$b$$$ candies. Then Santa will be satisfied, if the both conditions are met at the same time: $$$b - a \\le 1$$$ (it means $$$b = a$$$ or $$$b = a + 1$$$); the number of kids who has $$$a+1$$$ candies (note that $$$a+1$$$ not necessarily equals $$$b$$$) does not exceed $$$\\lfloor\\frac{k}{2}\\rfloor$$$ (less than or equal to $$$\\lfloor\\frac{k}{2}\\rfloor$$$). $$$\\lfloor\\frac{k}{2}\\rfloor$$$ is $$$k$$$ divided by $$$2$$$ and rounded down to the nearest integer. For example, if $$$k=5$$$ then $$$\\lfloor\\frac{k}{2}\\rfloor=\\lfloor\\frac{5}{2}\\rfloor=2$$$.Your task is to find the maximum number of candies Santa can give to kids so that he will be satisfied.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int p;\n scanf(\"%d\", &p);\n int a[p][3];\n for (int i = 0; i < p; i++) {\n int n;\n int k;\n scanf(\"%d\", &n);\n scanf(\"%d\", &k);\n a[i][0] = n;\n a[i][1] = k;\n if (n % k <= k / 2) {\n a[i][2] = n;\n } else {\n a[i][2] = n - n % k + k / 2;\n }\n printf(\"%d \\n\", a[i][2]);\n }\n\n return 0;\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 queries: u16 = line.trim().parse().unwrap();\n while queries > 0 {\n line.clear();\n input.read_line(&mut line).unwrap();\n let mut numbers = line.split_whitespace();\n let sweets: i32 = numbers.next().unwrap().parse().unwrap();\n let children: i32 = numbers.next().unwrap().parse().unwrap();\n let mut unused = sweets % children;\n unused -= children / 2;\n if unused < 0 {\n unused = 0;\n }\n println!(\"{}\", sweets - unused);\n queries -= 1;\n }\n}", "difficulty": "hard"} {"problem_id": "0559", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a string S of length N consisting of lowercase English letters.\nWe will cut this string at one position into two strings X and Y.\nHere, we would like to maximize the number of different letters contained in both X and Y.\nFind the largest possible number of different letters contained in both X and Y when we cut the string at the optimal position.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 100
  • \n
  • |S| = N
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the largest possible number of different letters contained in both X and Y.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\naabbca\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

If we cut the string between the third and fourth letters into X = aab and Y = bca, the letters contained in both X and Y are a and b.\nThere will never be three or more different letters contained in both X and Y, so the answer is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\naaaaaaaaaa\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

However we divide S, only a will be contained in both X and Y.

\n
\n
\n
\n
\n
\n

Sample Input 3

45\ntgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir\n
\n
\n
\n
\n
\n

Sample Output 3

9\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n scanf(\"%d\", &N);\n char S[N];\n scanf(\"%s\", S);\n\n char alp[2]['z' + 1];\n for (int i = 'a'; i <= 'z'; i++) {\n alp[0][i] = -1;\n }\n for (int i = 'a'; i <= 'z'; i++) {\n alp[1][i] = -1;\n }\n for (int i = 0; i < N; i++) {\n if (alp[0][S[i]] == -1) {\n alp[0][S[i]] = i;\n } else {\n alp[1][S[i]] = i;\n }\n }\n\n int max = 0;\n for (int i = 1; i < N - 1; i++) {\n int cnt = 0;\n for (int j = 'a'; j <= 'z'; j++) {\n if (alp[0][j] >= 0 && alp[0][j] < i && alp[1][j] >= i) {\n cnt++;\n }\n }\n if (cnt > max) {\n max = cnt;\n }\n }\n printf(\"%d\\n\", max);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n is.clear();\n stdin().read_line(&mut is).ok();\n let s = is.trim();\n let mut ans = 0;\n\n for i in 1..s.len() {\n let (x, y) = s.split_at(i);\n let mut x_cnt = [false; 26];\n let mut y_cnt = [false; 26];\n\n x.chars().for_each(|c| x_cnt[c as usize - 0x61] = true);\n y.chars().for_each(|c| y_cnt[c as usize - 0x61] = true);\n ans = max(\n ans,\n x_cnt\n .iter()\n .zip(y_cnt.iter())\n .filter(|e| *e.0 && *e.1)\n .count(),\n );\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0560", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given is a string S consisting of lowercase English letters. Find the maximum positive integer K that satisfies the following condition:

\n
    \n
  • There exists a partition of S into K non-empty strings S=S_1S_2...S_K such that S_i \\neq S_{i+1} (1 \\leq i \\leq K-1).
  • \n
\n

Here S_1S_2...S_K represents the concatenation of S_1,S_2,...,S_K in this order.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 2 \\times 10^5
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the maximum positive integer K that satisfies the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

aabbaa\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

We can, for example, divide S into four strings aa, b, ba, and a.

\n
\n
\n
\n
\n
\n

Sample Input 2

aaaccacabaababc\n
\n
\n
\n
\n
\n

Sample Output 2

12\n
\n
\n
", "c_code": "int solution() {\n bool is2 = false;\n\n char s[200000];\n scanf(\"%s\", s);\n int len = strlen(s);\n int k = len;\n for (int i = 0; i < len - 1; i++) {\n if (!is2 && s[i] == s[i + 1]) {\n is2 = true;\n i++;\n k--;\n } else {\n is2 = false;\n }\n }\n printf(\"%d\\n\", k);\n return 0;\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 dp = vec![vec![0; 2]; s.len() + 3];\n\n dp[0][0] = 1;\n dp[1][1] = 1;\n use std::cmp::max;\n\n for i in 1..s.len() {\n if s[i - 1] != s[i] && dp[i - 1][0] > 0 {\n dp[i][0] = max(dp[i][0], dp[i - 1][0] + 1);\n }\n\n if dp[i - 1][1] > 0 {\n dp[i][0] = max(dp[i][0], dp[i - 1][1] + 1);\n }\n\n if i + 1 < s.len() && dp[i - 1][0] > 0 {\n dp[i + 1][1] = max(dp[i + 1][1], dp[i - 1][0] + 1);\n }\n\n if i + 1 < s.len() && dp[i - 1][1] > 0 && s[i - 2..i] != s[i..i + 2] {\n dp[i + 1][1] = max(dp[i + 1][1], dp[i - 1][1] + 1);\n }\n }\n println!(\"{}\", max(dp[s.len() - 1][0], dp[s.len() - 1][1]));\n}", "difficulty": "hard"} {"problem_id": "0561", "problem_description": "The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.If there is no police officer free (isn't busy with crime) during the occurrence of a crime, it will go untreated.Given the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated.", "c_code": "int solution() {\n int n = 0;\n int i = 0;\n int ans = 0;\n int b = 0;\n scanf(\"%d\", &n);\n int a[n + 1];\n\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (i = 0; i < n; i++) {\n b += a[i];\n if (b < 0) {\n ans++;\n b = 0;\n }\n }\n\n printf(\"%d\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let _ = {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().to_string()\n };\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 (_, untreated) = v.into_iter().fold((0, 0), |(policy, untreated), i| {\n if i == -1 {\n if policy == 0 {\n (0, untreated + 1)\n } else {\n (policy - 1, untreated)\n }\n } else {\n (policy + i, untreated)\n }\n });\n println!(\"{:?}\", untreated);\n}", "difficulty": "hard"} {"problem_id": "0562", "problem_description": "Pig is visiting a friend.Pig's house is located at point 0, and his friend's house is located at point m on an axis.Pig can use teleports to move along the axis.To use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport there is the rightmost point it can move Pig to, this point is known as the limit of the teleport.Formally, a teleport located at point x with limit y can move Pig from point x to any point within the segment [x; y], including the bounds. Determine if Pig can visit the friend using teleports only, or he should use his car.", "c_code": "int solution() {\n int n = 0;\n int max = 0;\n int x = 0;\n scanf(\"%d\", &n);\n scanf(\"%d\", &x);\n int a[n];\n int b[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n scanf(\"%d\", &b[i]);\n }\n bool continuous = true;\n max = b[0];\n for (int i = 1; i < n; i++) {\n if (a[i] > max) {\n continuous = false;\n break;\n }\n max = b[i] > max ? b[i] : max;\n }\n\n if (a[0] == 0 && max >= x && continuous) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n return 0;\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\n let n = get!();\n let g = get!();\n let mut x = 0;\n for _ in 0..n {\n let ai = get!();\n let bi = get!();\n if ai <= x {\n x = std::cmp::max(x, bi);\n }\n }\n if g <= x {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "0563", "problem_description": "Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:Vladik is at initial train station, and now n people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code ai is known (the code of the city in which they are going to).Train chief selects some number of disjoint segments of the original sequence of people (covering entire sequence by segments is not necessary). People who are in the same segment will be in the same train carriage. The segments are selected in such way that if at least one person travels to the city x, then all people who are going to city x should be in the same railway carriage. This means that they can’t belong to different segments. Note, that all people who travel to the city x, either go to it and in the same railway carriage, or do not go anywhere at all.Comfort of a train trip with people on segment from position l to position r is equal to XOR of all distinct codes of cities for people on the segment from position l to position r. XOR operation also known as exclusive OR.Total comfort of a train trip is equal to sum of comfort for each segment.Help Vladik to know maximal possible total comfort.", "c_code": "int solution() {\n int n;\n int low[5003];\n int high[5003];\n int ar[5003];\n int dp[5003];\n int ver_no[5003];\n int i;\n int j;\n int k;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", ar + i);\n }\n for (i = 0; i < 5003; i++) {\n low[i] = -1;\n high[i] = -1;\n ver_no[i] = -1;\n }\n for (i = 0; i < n; i++) {\n j = ar[i];\n if (low[j] == -1) {\n low[j] = i;\n }\n high[j] = i;\n }\n dp[n] = 0;\n for (i = n - 1; i >= 0; i--) {\n if (low[ar[i]] == i) {\n dp[i] = 0;\n j = i;\n k = high[ar[i]];\n while (j <= k) {\n if (low[ar[j]] < i) {\n break;\n }\n if (ver_no[ar[j]] != i) {\n ver_no[ar[j]] = i;\n dp[i] ^= ar[j];\n if (high[ar[j]] > k) {\n k = high[ar[j]];\n }\n }\n j++;\n }\n if (j <= k) {\n dp[i] = 0;\n } else {\n dp[i] += dp[k + 1];\n }\n if (dp[i] < dp[i + 1]) {\n dp[i] = dp[i + 1];\n }\n } else {\n dp[i] = dp[i + 1];\n }\n }\n printf(\"%d\\n\", dp[0]);\n return 0;\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 a: Vec = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n input\n .split_whitespace()\n .map(|k| k.parse::().unwrap())\n .collect()\n };\n\n let fr: Vec<_> = (0..5001).map(|i| a.iter().position(|&k| k == i)).collect();\n let ls: Vec<_> = (0..5001).map(|i| a.iter().rposition(|&k| k == i)).collect();\n\n let mut dp = Vec::with_capacity(n);\n\n for i in 0..n {\n let mut max_val = 0;\n let mut max_ls = 0;\n let mut min_fr = n + 1;\n let mut cur = 0;\n let mut is_al = vec![false; 5001];\n for j in (0..(i + 1)).rev() {\n max_ls = max(ls[a[j]].unwrap(), max_ls);\n min_fr = min(fr[a[j]].unwrap(), min_fr);\n if !is_al[a[j]] {\n cur ^= (a[j]);\n }\n is_al[a[j]] = true;\n if min_fr >= j && i >= max_ls {\n if j >= 1 {\n max_val = max(max_val, cur + dp[j - 1]);\n } else {\n max_val = max(max_val, cur);\n }\n }\n }\n if i >= 1 {\n max_val = max(max_val, dp[i - 1]);\n }\n dp.push(max_val);\n }\n\n println!(\"{}\", dp[n - 1]);\n}", "difficulty": "medium"} {"problem_id": "0564", "problem_description": "This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \\le n \\le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: \"? a b\" where $$$1 \\le a, b \\le 10^{18}$$$ and $$$a \\neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\\max(a, b) > n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query \"? a b\", no matter how many such queries. Note that the \"? b a\" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.", "c_code": "int solution(void) {\n long long r1 = 0;\n long long r2 = 0;\n for (int b = 2; b <= 26; ++b) {\n printf(\"\\? %d %d\\n\", 1, b);\n fflush(stdout);\n scanf(\"%lli\", &r1);\n if (r1 == -1) {\n printf(\"! %d\\n\", b - 1);\n fflush(stdout);\n return 0;\n }\n printf(\"\\? %d %d\\n\", b, 1);\n fflush(stdout);\n scanf(\"%lli\", &r2);\n if (r1 != r2) {\n break;\n }\n }\n printf(\"! %lli\\n\", r1 + r2);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let _input = String::new();\n\n let out = io::stdout();\n let mut out = BufWriter::new(out.lock());\n\n let mut write_and_flush = |line: &str| {\n writeln!(out, \"{}\", line).unwrap();\n out.flush().unwrap();\n };\n\n let is_finished = |line: &str| line.eq(\"0\");\n\n let mut target = 3i64;\n\n loop {\n write_and_flush(&format!(\"? 1 {}\", target));\n\n let mut buf = String::new();\n io::stdin().read_line(&mut buf)?;\n let line = buf.trim();\n\n if is_finished(line) {\n break;\n }\n\n let path1: i64 = line.parse().unwrap();\n if path1 == -1 {\n write_and_flush(&format!(\"! {}\", target - 1));\n break;\n }\n\n write_and_flush(&format!(\"? {} 1\", target));\n\n let mut buf = String::new();\n io::stdin().read_line(&mut buf)?;\n let line = buf.trim();\n\n if is_finished(line) {\n break;\n }\n\n let path2: i64 = line.parse().unwrap();\n\n if path1 != path2 {\n write_and_flush(&format!(\"! {}\", path1 + path2));\n break;\n } else {\n target += 1;\n }\n }\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0565", "problem_description": "

Switching Railroad Cars

\n\n
\n\n
\n
\n\n

\nThis figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.\n

\n\n

\nWe can simulate the movement (comings and goings) of the cars as follow: \n

\n\n
    \n
  • An entry of a car is represented by its number.
  • \n
  • An exit of a car is represented by 0
  • \n
\n\n\n

\nFor example, a sequence\n

\n\n
\n1\n6\n0\n8\n10\n
\n\n

\ndemonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.\n

\n\n

\nWrite a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.\n

\n\n\n\n\n

Input

\n\n
\ncar number\ncar number or 0\ncar number or 0\n  .\n  .\n  .\ncar number or 0\n
\n\n

\nThe number of input lines is less than or equal to 100.\n

\n\n

Output

\n\n

\nFor each 0, print the car number.\n

\n\n

Sample Input

\n\n
\n1\n6\n0\n8\n10\n0\n0\n0\n
\n\n

Output for the Sample Input

\n\n
\n6\n10\n8\n1\n
", "c_code": "int solution() {\n int t[10];\n int ts = 0;\n while (scanf(\"%d\", &t[ts]) != EOF) {\n if (t[ts]) {\n ts++;\n } else {\n printf(\"%d\\n\", t[--ts]);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut stack = vec![];\n let input = BufReader::new(stdin());\n for line in input.lines() {\n let n = line.unwrap().trim().parse::().unwrap();\n if n == 0 {\n println!(\"{}\", stack.last().unwrap());\n let last_idx = stack.len() - 1;\n stack.remove(last_idx);\n } else {\n stack.push(n);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0566", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

\n

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

\n
    \n
  • For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.
  • \n
  • There is an edge between Vertex X and Vertex Y.
  • \n
\n

For each k=1,2,...,N-1, solve the problem below:

\n
    \n
  • Find the number of pairs of integers (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j in G is k.
  • \n
\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 3 \\leq N \\leq 2 \\times 10^3
  • \n
  • 1 \\leq X,Y \\leq N
  • \n
  • X+1 < Y
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N X Y\n
\n
\n
\n
\n
\n

Output

\n

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

5\n4\n1\n0\n
\n

The graph in this input is as follows:\n
\n
\n\"Figure\"\n
\n
\nThere are five pairs (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\\,,(2,3)\\,,(2,4)\\,,(3,4)\\,,(4,5).\n
\nThere are four pairs (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\\,,(1,4)\\,,(2,5)\\,,(3,5).\n
\nThere is one pair (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).\n
\nThere are no pairs (i,j) (1 \\leq i < j \\leq N) such that the shortest distance between Vertex i and Vertex j is 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 1 3\n
\n
\n
\n
\n
\n

Sample Output 2

3\n0\n
\n

The graph in this input is as follows:\n
\n
\n\"Figure\"\n
\n

\n
\n
\n
\n
\n
\n

Sample Input 3

7 3 7\n
\n
\n
\n
\n
\n

Sample Output 3

7\n8\n4\n2\n0\n0\n
\n
\n
\n
\n
\n
\n

Sample Input 4

10 4 8\n
\n
\n
\n
\n
\n

Sample Output 4

10\n12\n10\n8\n4\n1\n0\n0\n0\n
\n
\n
", "c_code": "int solution() {\n int n;\n int key1;\n int key2;\n int i;\n int j;\n scanf(\"%d%d%d\", &n, &key1, &key2);\n int a[n + 1][n + 1];\n int ans[n + 1];\n\n for (i = 1; i <= n - 1; i++) {\n ans[i] = 0;\n }\n\n for (i = 1; i <= n; i++) {\n for (j = 1; j < i; j++) {\n if (i >= key2 && j <= key1) {\n a[i][j] = i - j - (key2 - key1 - 1);\n } else if (((key1 <= i && i <= key2) && (j <= key1)) &&\n (i - j >= key1 + key2 - i - j + 1)) {\n a[i][j] = key1 + key2 - i - j + 1;\n } else if (((key1 <= j && j <= key2) && (i >= key2)) &&\n i - j >= j - key1 - key2 + i + 1) {\n a[i][j] = j - key1 - key2 + i + 1;\n } else if (key1 <= j && i <= key2 && i - j >= key2 + j - i - key1 + 1) {\n a[i][j] = key2 + j - i - key1 + 1;\n } else {\n a[i][j] = i - j;\n }\n }\n }\n for (i = 1; i <= n; i++) {\n for (j = 1; j < i; j++) {\n ans[a[i][j]]++;\n }\n }\n for (i = 1; i <= n - 1; i++) {\n printf(\"%d\\n\", ans[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (n, x, y) = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let s: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n (s[0], s[1], s[2])\n };\n let mut k = vec![0; n];\n let f = |i, j| if i < j { j - i } else { i - j };\n for i in 1..n + 1 {\n for j in i + 1..n + 1 {\n k[min(f(i, j), f(i, x) + f(y, j) + 1)] += 1;\n }\n }\n for i in 1..n {\n println!(\"{}\", k[i]);\n }\n}", "difficulty": "hard"} {"problem_id": "0567", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers.Let's call a pair of indices $$$i$$$, $$$j$$$ good if $$$1 \\le i < j \\le n$$$ and $$$\\gcd(a_i, 2a_j) > 1$$$ (where $$$\\gcd(x, y)$$$ is the greatest common divisor of $$$x$$$ and $$$y$$$).Find the maximum number of good index pairs if you can reorder the array $$$a$$$ in an arbitrary way.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int i;\n int j;\n int count = 0;\n int rem = 0;\n scanf(\"%d\", &n);\n int ara[n];\n int temp;\n int n1;\n int n2;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &ara[i]);\n }\n\n for (i = 0; i < n - 1; i++) {\n for (j = 0; j < n - 1; j++) {\n if (((ara[j + 1] % 2 == 0) && (ara[j] % 2 != 0))) {\n temp = ara[j];\n ara[j] = ara[j + 1];\n ara[j + 1] = temp;\n }\n }\n }\n\n for (i = 0; i < n - 1; i++) {\n for (j = i + 1; j < n; j++) {\n n1 = ara[i];\n n2 = 2 * ara[j];\n\n while (n2 != 0) {\n rem = n1 % n2;\n n1 = n2;\n n2 = rem;\n }\n\n if (n1 > 1) {\n count++;\n }\n }\n }\n printf(\"%d\\n\", count);\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 for _ in 0..t {\n let n = lines.next().unwrap().parse::().unwrap();\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let a = it.collect::>();\n\n let (even, odd): (Vec<_>, Vec<_>) = a.iter().cloned().partition(|x| x % 2 == 0);\n\n let oans = (0..odd.len())\n .map(|i| {\n (i + 1..odd.len())\n .map(|j| {\n let mut x = odd[i];\n let mut y = 2 * odd[j];\n while y != 0 {\n let t = x % y;\n x = y;\n y = t;\n }\n if x == 1 {\n 0u64\n } else {\n 1\n }\n })\n .sum::()\n })\n .sum::();\n\n let m = even.len() as u64;\n let eans = if m < 2 { 0 } else { (m * (m - 1)) / 2 } + m * (n as u64 - m);\n\n let ans = oans + eans;\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0568", "problem_description": "Given a set of integers (it can contain equal elements).You have to split it into two subsets $$$A$$$ and $$$B$$$ (both of them can contain equal elements or be empty). You have to maximize the value of $$$mex(A)+mex(B)$$$.Here $$$mex$$$ of a set denotes the smallest non-negative integer that doesn't exist in the set. For example: $$$mex(\\{1,4,0,2,2,1\\})=3$$$ $$$mex(\\{3,3,2,1,3,0,0\\})=4$$$ $$$mex(\\varnothing)=0$$$ ($$$mex$$$ for empty set) The set is splitted into two subsets $$$A$$$ and $$$B$$$ if for any integer number $$$x$$$ the number of occurrences of $$$x$$$ into this set is equal to the sum of the number of occurrences of $$$x$$$ into $$$A$$$ and the number of occurrences of $$$x$$$ into $$$B$$$.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int m;\n scanf(\"%d\", &m);\n int arr[m];\n int farr[101];\n for (int i = 0; i < 101; i++) {\n farr[i] = 0;\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &arr[i]);\n farr[arr[i]]++;\n }\n int xx = 101;\n int yy = 101;\n for (int i = 0; i < 101; i++) {\n if (farr[i] < 2 && xx == 101) {\n xx = i;\n }\n if (farr[i] < 1 && xx != 101) {\n yy = i;\n }\n if (xx != 101 && yy != 101) {\n break;\n }\n }\n printf(\"%d\\n\", xx + yy);\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_line(&mut input).expect(\"error\");\n let iterations = input.trim().parse::().expect(\"error\");\n for _ in 0..iterations {\n input.clear();\n stdin().read_line(&mut input).expect(\"error\");\n input.clear();\n stdin().read_line(&mut input).expect(\"error\");\n let mut numbers = input\n .split_whitespace()\n .map(|n| n.parse::().expect(\"error\"))\n .collect::>();\n numbers.sort();\n\n let mut a = 0;\n let mut b = 0;\n for item in numbers {\n if item == a {\n a += 1;\n } else if item == b {\n b += 1;\n }\n }\n println!(\"{}\", a + b);\n }\n}", "difficulty": "hard"} {"problem_id": "0569", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

AtCoDeer the deer has found two positive integers, a and b.\nDetermine whether the concatenation of a and b in this order is a square number.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 a,b 100
  • \n
  • a and b are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b\n
\n
\n
\n
\n
\n

Output

If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 21\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

As 121 = 11 × 11, it is a square number.

\n
\n
\n
\n
\n
\n

Sample Input 2

100 100\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

100100 is not a square number.

\n
\n
\n
\n
\n
\n

Sample Input 3

12 10\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int c = 1;\n int d = 0;\n scanf(\"%d %d\", &a, &b);\n if (b < 10) {\n d = a * 10 + b;\n } else {\n if (b < 100) {\n d = a * 100 + b;\n } else {\n d = a * 1000 + b;\n }\n }\n while (c < 400) {\n if ((c * c) == d) {\n puts(\"Yes\");\n return 0;\n }\n c++;\n }\n puts(\"No\");\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 vec: Vec<&str> = s.trim().split(' ').collect();\n let mut n = String::new();\n for v in &vec {\n n += v.as_ref();\n }\n let p = n.parse::().unwrap();\n for i in 0..10000 {\n let s = i * i;\n if s == p {\n println!(\"Yes\");\n return;\n }\n }\n println!(\"No\");\n}", "difficulty": "medium"} {"problem_id": "0570", "problem_description": "The only difference between the easy and hard versions is that the given string $$$s$$$ in the easy version is initially a palindrome, this condition is not always true for the hard version.A palindrome is a string that reads the same left to right and right to left. For example, \"101101\" is a palindrome, while \"0101\" is not.Alice and Bob are playing a game on a string $$$s$$$ of length $$$n$$$ consisting of the characters '0' and '1'. Both players take alternate turns with Alice going first.In each turn, the player can perform one of the following operations: Choose any $$$i$$$ ($$$1 \\le i \\le n$$$), where $$$s[i] =$$$ '0' and change $$$s[i]$$$ to '1'. Pay 1 dollar. Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can't reverse in the next move, and vice versa. Reversing a string means reordering its letters from the last to the first. For example, \"01001\" becomes \"10010\" after reversing.The game ends when every character of string becomes '1'. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw.", "c_code": "int solution() {\n char s[1005];\n int t;\n int n;\n int flag = 0;\n int sum = 0;\n scanf(\"%d\", &t);\n while (t--) {\n flag = 0, sum = 0;\n scanf(\"%d\", &n);\n scanf(\"%s\", s);\n for (int i = 0; i < n; i++) {\n if (s[i] != s[n - 1 - i]) {\n flag = 1;\n }\n if (s[i] == '0') {\n sum++;\n }\n }\n if (flag == 0) {\n if (sum == 1 || sum % 2 == 0) {\n printf(\"BOB\\n\");\n } else {\n printf(\"ALICE\\n\");\n }\n } else {\n if (sum == 2 && n % 2 == 1 && s[n / 2] == '0') {\n printf(\"DRAW\\n\");\n } else {\n printf(\"ALICE\\n\");\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\n let t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let n = lines.next().unwrap().parse::().unwrap();\n\n let s = lines.next().unwrap();\n let a = s.as_bytes();\n\n let mut rc = 0;\n let mut bc = 0;\n\n for i in 0..n / 2 {\n let j = n - 1 - i;\n\n let a0 = a[i] == b'0';\n let a1 = a[j] == b'0';\n\n if a0 && a1 {\n rc += 1;\n } else if a0 || a1 {\n bc += 1;\n }\n }\n\n let a0 = rc > 0;\n let a1 = n % 2 == 1 && a[n / 2] == b'0';\n\n let sb1 = {\n match (a0, a1) {\n (false, false) => 0,\n (false, true) => -1,\n (true, false) => -2,\n (true, true) => 1,\n }\n };\n\n let s0 = bc + sb1;\n\n let s1 = if bc > 0 { bc - 2 - sb1 } else { i32::MIN };\n\n let s = std::cmp::max(s0, s1);\n\n use std::cmp::Ordering::*;\n let ans = match s.cmp(&0) {\n Less => \"BOB\",\n Equal => \"DRAW\",\n Greater => \"ALICE\",\n };\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "easy"} {"problem_id": "0571", "problem_description": "Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $$$x$$$, and the shorter rabbit is currently on position $$$y$$$ ($$$x \\lt y$$$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $$$a$$$, and the shorter rabbit hops to the negative direction by $$$b$$$. For example, let's say $$$x=0$$$, $$$y=10$$$, $$$a=2$$$, and $$$b=3$$$. At the $$$1$$$-st second, each rabbit will be at position $$$2$$$ and $$$7$$$. At the $$$2$$$-nd second, both rabbits will be at position $$$4$$$.Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.", "c_code": "int solution() {\n long long int x[1000][4];\n long long int n;\n long long int i;\n\n scanf(\"%lld\", &n);\n\n for (i = 0; i < n; i++) {\n scanf(\"%lld %lld %lld %lld\", &x[i][0], &x[i][1], &x[i][2], &x[i][3]);\n }\n\n for (i = 0; i < n; i++) {\n if ((x[i][1] - x[i][0]) % (x[i][2] + x[i][3]) == 0) {\n printf(\"%lld\\n\", (x[i][1] - x[i][0]) / (x[i][2] + x[i][3]));\n } else {\n printf(\"-1\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let t = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read line\");\n buf.trim().parse::().unwrap()\n };\n for _ in 0..t {\n let (a, b, x, y) = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read\");\n let nums: Vec = buf\n .trim()\n .split_ascii_whitespace()\n .map(|c| c.parse().unwrap())\n .collect();\n (nums[0], nums[1], nums[2], nums[3])\n };\n let ans = if (b - a) % (x + y) == 0 {\n Some((b - a) / (x + y))\n } else {\n None\n };\n match ans {\n Some(ans) => println!(\"{}\", ans),\n None => println!(\"-1\"),\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0572", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Dolphin resides in two-dimensional Cartesian plane, with the positive x-axis pointing right and the positive y-axis pointing up.
\nCurrently, he is located at the point (sx,sy). In each second, he can move up, down, left or right by a distance of 1.
\nHere, both the x- and y-coordinates before and after each movement must be integers.
\nHe will first visit the point (tx,ty) where sx < tx and sy < ty, then go back to the point (sx,sy), then visit the point (tx,ty) again, and lastly go back to the point (sx,sy).
\nHere, during the whole travel, he is not allowed to pass through the same point more than once, except the points (sx,sy) and (tx,ty).
\nUnder this condition, find a shortest path for him.

\n
\n
\n
\n
\n

Constraints

    \n
  • -1000 ≤ sx < tx ≤ 1000
  • \n
  • -1000 ≤ sy < ty ≤ 1000
  • \n
  • sx,sy,tx and ty are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
sx sy tx ty\n
\n
\n
\n
\n
\n

Output

Print a string S that represents a shortest path for Dolphin.
\nThe i-th character in S should correspond to his i-th movement.
\nThe directions of the movements should be indicated by the following characters:

\n
    \n
  • U: Up
  • \n
  • D: Down
  • \n
  • L: Left
  • \n
  • R: Right
  • \n
\n

If there exist multiple shortest paths under the condition, print any of them.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

0 0 1 2\n
\n
\n
\n
\n
\n

Sample Output 1

UURDDLLUUURRDRDDDLLU\n
\n

One possible shortest path is:

\n
    \n
  • Going from (sx,sy) to (tx,ty) for the first time: (0,0)(0,1)(0,2)(1,2)
  • \n
  • Going from (tx,ty) to (sx,sy) for the first time: (1,2)(1,1)(1,0)(0,0)
  • \n
  • Going from (sx,sy) to (tx,ty) for the second time: (0,0)(-1,0)(-1,1)(-1,2)(-1,3)(0,3)(1,3)(1,2)
  • \n
  • Going from (tx,ty) to (sx,sy) for the second time: (1,2)(2,2)(2,1)(2,0)(2,-1)(1,-1)(0,-1)(0,0)
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

-2 -2 1 1\n
\n
\n
\n
\n
\n

Sample Output 2

UURRURRDDDLLDLLULUUURRURRDDDLLDL\n
\n
\n
", "c_code": "int solution() {\n int sx = 0;\n int sy = 0;\n int tx = 0;\n int ty = 0;\n int i = 0;\n scanf(\"%d %d %d %d\", &sx, &sy, &tx, &ty);\n for (i = 0; i < ty - sy; i++) {\n printf(\"U\");\n }\n for (i = 0; i < tx - sx; i++) {\n printf(\"R\");\n }\n for (i = 0; i < ty - sy; i++) {\n printf(\"D\");\n }\n for (i = 0; i < tx - sx; i++) {\n printf(\"L\");\n }\n printf(\"L\");\n for (i = 0; i < ty - sy + 1; i++) {\n printf(\"U\");\n }\n for (i = 0; i < tx - sx + 1; i++) {\n printf(\"R\");\n }\n printf(\"D\");\n printf(\"R\");\n for (i = 0; i < ty - sy + 1; i++) {\n printf(\"D\");\n }\n for (i = 0; i < tx - sx + 1; i++) {\n printf(\"L\");\n }\n printf(\"U\");\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 sx: i64 = itr.next().unwrap().parse().unwrap();\n let sy: i64 = itr.next().unwrap().parse().unwrap();\n let tx: i64 = itr.next().unwrap().parse().unwrap();\n let ty: i64 = itr.next().unwrap().parse().unwrap();\n\n let dx = tx - sx;\n let dy = ty - sy;\n let mut ans = Vec::new();\n for _ in 0..dx {\n ans.push('R');\n }\n for _ in 0..dy {\n ans.push('U');\n }\n for _ in 0..dx {\n ans.push('L');\n }\n for _ in 0..dy {\n ans.push('D');\n }\n\n ans.push('D');\n for _ in 0..dx + 1 {\n ans.push('R');\n }\n for _ in 0..dy + 1 {\n ans.push('U');\n }\n ans.push('L');\n ans.push('U');\n for _ in 0..dx + 1 {\n ans.push('L');\n }\n for _ in 0..dy + 1 {\n ans.push('D');\n }\n ans.push('R');\n\n let s: String = ans.into_iter().collect();\n println!(\"{}\", s);\n}", "difficulty": "easy"} {"problem_id": "0573", "problem_description": "

問題 4: 部活のスケジュール表 (Schedule)\n

\n
\n\n

問題

\n

\nIOI 高校のプログラミング部には,J 君と O 君と I 君の 3 人の部員がいる.\nプログラミング部では,部活のスケジュールを組もうとしている.\n

\n\n

\n今,N 日間の活動のスケジュールを決めようとしている.\n各活動日のスケジュールとして考えられるものは,各部員については活動に参加するかどうかの 2 通りがあり,部としては全部で 8 通りある.\n部室の鍵はただ 1 つだけであり,最初は J 君が持っている.\n各活動日には,その日の活動に参加する部員のうちのいずれか 1 人が鍵を持っている必要があり,活動後に参加した部員のいずれかが鍵を持って帰る.\n

\n\n

\nプログラミング部では,活動日には毎回必ず活動が行われるように,あらかじめ各活動日の責任者を定めた.\n責任者は,必ずその日の活動に出席しなければならない.\n

\n\n

\nスケジュールを決めようとしている日数と,各活動日の責任者が誰であるかの情報が与えられた時,すべての活動日に部活動を行うことができるようなスケジュール表として考えられるものの数を 10007 で割った余りを求めるプログラムを作成せよ.\nただし,部活の終了時に鍵を持って帰る部員は,その日の活動に参加している部員の誰でもよく,最終日は誰が鍵を持って帰ってもよいものとする.\n

\n\n

入力

\n\n

\n入力は,2 行からなる.\n

\n\n

\n1 行目には,スケジュールを決めようとしている日数を表す 1 つの整数 N (2 ≤ N ≤ 1000) が書かれている.\n

\n\n

\n2 行目には,各活動日の責任者を表す N 文字からなる文字列が書かれている.\nこの文字列の i 文字目 (1 ≤ i ≤ N) は,i 日目の活動日の責任者を表す.\nすなわち,i 文字目が J, O, I であることはそれぞれ i 日目の活動日の責任者が J 君,O 君,I 君であることを意味する.\n

\n\n

出力

\n

\nスケジュール表として考えられるものの数を 10007 で割った余りを 1 行で出力せよ.\n

\n\n

入出力例

\n\n

入力例 1

\n\n
\n2\nOI\n
\n\n

出力例 1

\n
\n7\n
\n\n

\n入出力例 1 において,2 日間の活動日のスケジュールを考える.1 日目の責任者は O 君,2 日目の責任者は I 君である.\n問題文の条件をみたすスケジュール表は 7 通り考えられる.\n

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 1 日目  2 日目 
スケジュール 1   J, OO, I
スケジュール 2   J, OJ, I
スケジュール 3   J, OJ, O, I
スケジュール 4   J, O, II
スケジュール 5   J, O, IJ, I
スケジュール 6   J, O, IO, I
スケジュール 7   J, O, IJ, O, I
\n
\n\n\n

\nこの表では,J, O, I はそれぞれその日に J 君, O 君, I 君が参加することを表す.\n1 日目の責任者は O 君であるが,最初鍵を持っているのは J 君であるため,1 日目の活動には J 君, O 君の両方が参加する必要があることに注意せよ.\nまた,1 日目に鍵を持って帰った人は 2 日目にも参加しないといけないので,1 日目と 2 日目の両方に参加する人が少なくとも 1 人存在しなければならないことに注意せよ.\n

\n\n
\n \n

入力例 2

\n\n
\n20\nJIOIJOIJOJOIIIOJIOII\n
\n\n

出力例 2

\n\n
\n4976\n
\n\n

\n入出力例 2 において,条件をみたすスケジュール表は全部で 72493594992 通り考えられる.それを 10007 で割った余りである 4976 を出力する.\n

\n\n\n
\n

\n問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。\n

\n
", "c_code": "int solution(void) {\n int dp[1000][8];\n int n;\n int i;\n int j;\n int k;\n int ans = 0;\n char res[1001];\n scanf(\"%d\", &n);\n scanf(\"%s\", res);\n for (i = 0; i < n; i++) {\n switch (res[i]) {\n case 'J':\n res[i] = 4;\n break;\n case 'O':\n res[i] = 2;\n break;\n case 'I':\n res[i] = 1;\n break;\n }\n }\n for (i = 0; i < 8; i++) {\n if ((i & 4) != 0 && (i & res[0]) != 0) {\n dp[0][i] = 1;\n } else {\n dp[0][i] = 0;\n }\n for (j = 1; j < n; j++) {\n dp[j][i] = 0;\n }\n }\n for (i = 1; i < n; i++) {\n for (j = 0; j < 8; j++) {\n for (k = 0; k < 8; k++) {\n if ((j & k) != 0 && (res[i] & k) != 0) {\n dp[i][k] = (dp[i][k] + dp[i - 1][j]) % 10007;\n }\n }\n }\n }\n n--;\n for (i = 0; i < 8; i++) {\n ans += dp[n][i];\n ans %= 10007;\n }\n printf(\"%d\\n\", ans);\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 let iter = lines.nth(1).unwrap().bytes();\n\n let mut comb = [1u64, 0, 0, 0, 0, 0, 0];\n\n for ch in iter {\n let chg = match ch {\n b'J' => 1,\n b'O' => 2,\n _ => 4,\n };\n let mut tmp = [0u64; 7];\n\n for (i, &n) in comb.iter().enumerate() {\n if n == 0 {\n continue;\n }\n let bit = i + 1;\n\n if bit & (1 | chg) != 0 {\n tmp[(chg | 1) - 1] += n;\n }\n if bit & (2 | chg) != 0 {\n tmp[(chg | 2) - 1] += n;\n }\n if bit & (4 | chg) != 0 {\n tmp[(chg | 4) - 1] += n;\n }\n tmp[7 - 1] += n;\n }\n\n if tmp[7 - 1] >= 1 << 62 {\n for n in &mut tmp {\n *n %= 10007;\n }\n }\n comb = tmp;\n }\n\n let sum = comb.into_iter().sum::() % 10007;\n println!(\"{}\", sum);\n}", "difficulty": "medium"} {"problem_id": "0574", "problem_description": "You are given a set of $$$2n+1$$$ integer points on a Cartesian plane. Points are numbered from $$$0$$$ to $$$2n$$$ inclusive. Let $$$P_i$$$ be the $$$i$$$-th point. The $$$x$$$-coordinate of the point $$$P_i$$$ equals $$$i$$$. The $$$y$$$-coordinate of the point $$$P_i$$$ equals zero (initially). Thus, initially $$$P_i=(i,0)$$$.The given points are vertices of a plot of a piecewise function. The $$$j$$$-th piece of the function is the segment $$$P_{j}P_{j + 1}$$$.In one move you can increase the $$$y$$$-coordinate of any point with odd $$$x$$$-coordinate (i.e. such points are $$$P_1, P_3, \\dots, P_{2n-1}$$$) by $$$1$$$. Note that the corresponding segments also change.For example, the following plot shows a function for $$$n=3$$$ (i.e. number of points is $$$2\\cdot3+1=7$$$) in which we increased the $$$y$$$-coordinate of the point $$$P_1$$$ three times and $$$y$$$-coordinate of the point $$$P_5$$$ one time: Let the area of the plot be the area below this plot and above the coordinate axis OX. For example, the area of the plot on the picture above is 4 (the light blue area on the picture above is the area of the plot drawn on it).Let the height of the plot be the maximum $$$y$$$-coordinate among all initial points in the plot (i.e. points $$$P_0, P_1, \\dots, P_{2n}$$$). The height of the plot on the picture above is 3.Your problem is to say which minimum possible height can have the plot consisting of $$$2n+1$$$ vertices and having an area equal to $$$k$$$. Note that it is unnecessary to minimize the number of moves.It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding $$$10^{18}$$$.", "c_code": "int solution() {\n long long n;\n long long k;\n while (scanf(\"%lld%lld\", &n, &k) != EOF) {\n long long t;\n t = k / n;\n if (t * n != k) {\n printf(\"%lld\\n\", t + 1);\n } else {\n printf(\"%lld\\n\", t);\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\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let n = arr[0];\n let k = arr[1];\n\n let distrib_height = k / n + if k % n == 0 { 0 } else { 1 };\n\n println!(\"{}\", distrib_height);\n}", "difficulty": "medium"} {"problem_id": "0575", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are two rectangles.\nThe lengths of the vertical sides of the first rectangle are A, and the lengths of the horizontal sides of the first rectangle are B.\nThe lengths of the vertical sides of the second rectangle are C, and the lengths of the horizontal sides of the second rectangle are D.

\n

Print the area of the rectangle with the larger area.\nIf the two rectangles have equal areas, print that area.

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1≤A≤10^4
  • \n
  • 1≤B≤10^4
  • \n
  • 1≤C≤10^4
  • \n
  • 1≤D≤10^4
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
A B C D\n
\n
\n
\n
\n
\n

Output

Print the area of the rectangle with the larger area.\nIf the two rectangles have equal areas, print that area.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 5 2 7\n
\n
\n
\n
\n
\n

Sample Output 1

15\n
\n

The first rectangle has an area of 3×5=15, and the second rectangle has an area of 2×7=14.\nThus, the output should be 15, the larger area.

\n
\n
\n
\n
\n
\n

Sample Input 2

100 600 200 300\n
\n
\n
\n
\n
\n

Sample Output 2

60000\n
\n
\n
", "c_code": "int solution(void) {\n int A = 0;\n int B = 0;\n int C = 0;\n int D = 0;\n int s1 = 0;\n int s2 = 0;\n scanf(\"%d%d%d%d\", &A, &B, &C, &D);\n s1 = A * B;\n s2 = C * D;\n if (s1 == s2) {\n printf(\"%d\\n\", s1);\n }\n if (s1 > s2) {\n printf(\"%d\\n\", s1);\n } else if (s1 < s2) {\n printf(\"%d\\n\", s2);\n }\n\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 v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n println!(\"{}\", std::cmp::max(v[0] * v[1], v[2] * v[3]));\n}", "difficulty": "medium"} {"problem_id": "0576", "problem_description": "I, Fischl, Prinzessin der Verurteilung, descend upon this land by the call of fate an — Oh, you are also a traveler from another world? Very well, I grant you permission to travel with me.It is no surprise Fischl speaks with a strange choice of words. However, this time, not even Oz, her raven friend, can interpret her expressions! Maybe you can help us understand what this young princess is saying?You are given a string of $$$n$$$ lowercase Latin letters, the word that Fischl just spoke. You think that the MEX of this string may help you find the meaning behind this message. The MEX of the string is defined as the shortest string that doesn't appear as a contiguous substring in the input. If multiple strings exist, the lexicographically smallest one is considered the MEX. Note that the empty substring does NOT count as a valid MEX.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.Find out what the MEX of the string is!", "c_code": "int solution() {\n int t;\n int n;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d\", &n);\n int b[30];\n int flag = 0;\n memset(b, 0, sizeof(b));\n char a[1005];\n char tem;\n for (int i = 0; i < n;) {\n tem = getchar();\n if (tem >= 'a' && tem <= 'z') {\n a[i] = tem;\n i++;\n b[tem - 'a'] = 1;\n }\n }\n for (int i = 0; i < 26; i++) {\n if (b[i] == 0) {\n printf(\"%c\\n\", i + 'a');\n flag = 1;\n break;\n }\n }\n for (char i = 'a'; i <= 'z'; i++) {\n if (flag == 1) {\n break;\n }\n for (char j = 'a'; j <= 'z'; j++) {\n int cflag = 1;\n for (int k = 0; k < n - 1; k++) {\n if (i == a[k] && j == a[k + 1]) {\n cflag = 0;\n break;\n }\n }\n if (cflag == 1) {\n flag = 1;\n printf(\"%c%c\\n\", i, j);\n break;\n }\n }\n if (flag == 1) {\n break;\n }\n }\n for (char i = 'a'; i <= 'z'; i++) {\n if (flag == 1) {\n break;\n }\n for (char j = 'a'; j <= 'z'; j++) {\n for (char k = 'a'; k < 'z'; k++) {\n int cflag = 1;\n for (int m = 0; m < n - 2; m++) {\n if (i == a[m] && j == a[m + 1] && k == a[m + 2]) {\n cflag = 0;\n break;\n }\n }\n if (cflag == 1) {\n printf(\"%c%c%c\\n\", i, j, k);\n flag = 1;\n break;\n }\n }\n if (flag == 1) {\n break;\n }\n }\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\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 for _ in 0..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\n let mut t1 = [false; 26];\n let mut t2 = [false; 26 * 26];\n let mut t3 = [false; 26 * 26 * 26];\n\n let a = s.as_bytes();\n\n for &b in a {\n let i = (b - b'a') as usize;\n t1[i] = true;\n }\n\n for b in a.windows(2) {\n let d1 = (b[0] - b'a') as usize;\n let d0 = (b[1] - b'a') as usize;\n\n let i = 26 * d1 + d0;\n t2[i] = true;\n }\n\n for b in a.windows(3) {\n let d2 = (b[0] - b'a') as usize;\n let d1 = (b[1] - b'a') as usize;\n let d0 = (b[2] - b'a') as usize;\n\n let i = 26 * 26 * d2 + 26 * d1 + d0;\n t3[i] = true;\n }\n\n if let Some(i) = t1.iter().position(|&a| !a) {\n let mex = &[i as u8 + b'a'];\n let s = std::str::from_utf8(mex).unwrap();\n writeln!(&mut output, \"{}\", s).unwrap();\n } else if let Some(i) = t2.iter().position(|&a| !a) {\n let d1 = (i / 26) as u8;\n let d0 = (i % 26) as u8;\n\n let mex = &[d1 + b'a', d0 + b'a'];\n let s = std::str::from_utf8(mex).unwrap();\n writeln!(&mut output, \"{}\", s).unwrap();\n } else {\n let mut i = t3.iter().position(|&a| !a).unwrap();\n\n let d0 = (i % 26) as u8;\n i /= 26;\n let d1 = (i % 26) as u8;\n i /= 26;\n let d2 = (i % 26) as u8;\n\n let mex = &[d2 + b'a', d1 + b'a', d0 + b'a'];\n let s = std::str::from_utf8(mex).unwrap();\n writeln!(&mut output, \"{}\", s).unwrap();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0577", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

\n

How many kinds of items did you get?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2\\times 10^5
  • \n
  • S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS_1\n:\nS_N\n
\n
\n
\n
\n
\n

Output

Print the number of kinds of items you got.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\napple\norange\napple\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

You got two kinds of items: apple and orange.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\ngrape\ngrape\ngrape\ngrape\ngrape\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

4\naaaa\na\naaa\naa\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int i;\n int s;\n int c;\n int changed;\n int j;\n\n struct cell head;\n struct cell *p;\n\n for (i = 0; i < 30; i++) {\n head.next[i] = NULL;\n }\n head.e = 0;\n\n s = 0;\n\n scanf(\"%d\", &n);\n getchar();\n for (i = 0; i < n; i++) {\n p = &head;\n changed = 0;\n while ((c = getchar()) != '\\n') {\n if (p->next[c - 'a'] == NULL) {\n changed = 1;\n p->next[c - 'a'] = (struct cell *)malloc(sizeof(struct cell));\n p = p->next[c - 'a'];\n for (j = 0; j < 30; j++) {\n p->next[j] = NULL;\n }\n p->e = 0;\n\n } else {\n p = p->next[c - 'a'];\n }\n }\n if (p->e == 0) {\n p->e = 1;\n changed = 1;\n }\n s += changed;\n }\n\n printf(\"%d\\n\", s);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n std::io::stdin().read_line(&mut n).ok();\n let n: i32 = n.trim().parse().unwrap();\n\n let mut m = std::collections::HashMap::::new();\n for _ in 0..n {\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).ok();\n m.insert(t, true);\n }\n\n println!(\"{}\", m.len());\n}", "difficulty": "hard"} {"problem_id": "0578", "problem_description": "You have integer $$$n$$$. Calculate how many ways are there to fully cover belt-like area of $$$4n-2$$$ triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. $$$2$$$ coverings are different if some $$$2$$$ triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one.Please look at pictures below for better understanding. On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. These are the figures of the area you want to fill for $$$n = 1, 2, 3, 4$$$. You have to answer $$$t$$$ independent test cases.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n int n[t];\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &n[i]);\n }\n for (int i = 0; i < t; i++) {\n printf(\"%d\\n\", n[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut i = 0u32;\n let mut t = String::new();\n io::stdin().read_line(&mut t).expect(\"Input Failed\");\n let t: u32 = t.trim().parse().expect(\"Not a number\");\n loop {\n if i == t {\n break;\n } else {\n let mut n = String::new();\n io::stdin().read_line(&mut n).expect(\"Input Failed\");\n let n: u32 = n.trim().parse().expect(\"Not a number\");\n println!(\"{}\", n);\n i += 1\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0579", "problem_description": "You are given two segments $$$[l_1; r_1]$$$ and $$$[l_2; r_2]$$$ on the $$$x$$$-axis. It is guaranteed that $$$l_1 < r_1$$$ and $$$l_2 < r_2$$$. Segments may intersect, overlap or even coincide with each other. The example of two segments on the $$$x$$$-axis. Your problem is to find two integers $$$a$$$ and $$$b$$$ such that $$$l_1 \\le a \\le r_1$$$, $$$l_2 \\le b \\le r_2$$$ and $$$a \\ne b$$$. In other words, you have to choose two distinct integer points in such a way that the first point belongs to the segment $$$[l_1; r_1]$$$ and the second one belongs to the segment $$$[l_2; r_2]$$$.It is guaranteed that the answer exists. If there are multiple answers, you can print any of them.You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int q;\n int a[505];\n int b[505];\n int c[505];\n int d[505];\n scanf(\"%d\", &q);\n for (int i = 0; i < q; i++) {\n scanf(\"%d%d%d%d\", &a[i], &b[i], &c[i], &d[i]);\n }\n for (int i = 0; i < q; i++) {\n if (a[i] != c[i]) {\n printf(\"%d %d\\n\", a[i], c[i]);\n } else {\n printf(\"%d %d\\n\", a[i], a[i] + 1);\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 let n: u32 = line.trim().parse().unwrap();\n\n for _ in 0..n {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n\n let v: Vec = line.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n\n let (l1, _, l2, _) = (v[0], v[1], v[2], v[3]);\n if l1 == l2 {\n println!(\"{} {}\", l1, l1 + 1);\n } else {\n println!(\"{} {}\", l1, l2);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0580", "problem_description": "According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open.You are given a jacket with n buttons. Determine if it is fastened in a right way.", "c_code": "int solution(void) {\n char c = 0;\n int n = 0;\n int i = 0;\n int cnt = 0;\n char arr[1000] = {\n 0,\n };\n\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\" %c\", &arr[i]);\n if (arr[i] == '1') {\n cnt++;\n }\n }\n\n if (n == 1) {\n if (cnt == 0) {\n c = 'N';\n } else {\n c = 'Y';\n }\n } else {\n if (n - cnt == 1) {\n c = 'Y';\n } else {\n c = 'N';\n }\n }\n\n if (c == 'Y') {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buttons = String::new();\n let mut arr = String::new();\n stdin().read_line(&mut buttons).unwrap();\n stdin().read_line(&mut arr).unwrap();\n\n let buttons: u16 = buttons.trim().parse().unwrap();\n\n let arr: Vec = arr\n .split_whitespace()\n .map(|e| if e == \"1\" { 1 } else { 0 })\n .collect();\n\n if buttons == 1 && arr[0] == 1 {\n println!(\"YES\")\n } else if buttons == 1 && arr[0] != 1 {\n println!(\"NO\")\n } else {\n let mut c = 0;\n for i in arr {\n if i == 0 {\n c += 1;\n if c > 1 {\n break;\n }\n }\n }\n if c == 1 {\n println!(\"YES\")\n } else {\n println!(\"NO\")\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0581", "problem_description": "Polycarp is working on a new project called \"Polychat\". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands: Include a person to the chat ('Add' command). Remove a person from the chat ('Remove' command). Send a message from a person to all people, who are currently in the chat, including the one, who sends the message ('Send' command). Now Polycarp wants to find out the amount of outgoing traffic that the server will produce while processing a particular set of commands.Polycarp knows that chat server sends no traffic for 'Add' and 'Remove' commands. When 'Send' command is processed, server sends l bytes to each participant of the chat, where l is the length of the message.As Polycarp has no time, he is asking for your help in solving this problem.", "c_code": "int solution() {\n\n char status[100];\n int quant = 0;\n int qntBytes = 0;\n int i = 0;\n\n while (scanf(\"%[^\\n] \", status) != EOF) {\n if (status[0] == '+') {\n quant++;\n } else if (status[0] == '-') {\n quant--;\n } else {\n for (i = 0; status[i] != ':'; i++) {\n }\n\n qntBytes += quant * (strlen(status) - i - 1);\n }\n }\n printf(\"%d\", qntBytes);\n\n return 0;\n}", "rust_code": "fn solution() {\n let reader = BufReader::new(io::stdin());\n let mut cnt = 0;\n let mut length = 0;\n for line in reader.lines().flatten() {\n let bs = line.as_bytes();\n match bs[0] {\n b'+' => {\n cnt += 1;\n }\n b'-' => {\n cnt -= 1;\n }\n _ => {\n if let Some(n) = bs.iter().position(|&c| c == b':') {\n length += cnt * (bs.len() - n - 1);\n }\n }\n }\n }\n println!(\"{}\", length);\n}", "difficulty": "easy"} {"problem_id": "0582", "problem_description": "At the store, the salespeople want to make all prices round. In this problem, a number that is a power of $$$10$$$ is called a round number. For example, the numbers $$$10^0 = 1$$$, $$$10^1 = 10$$$, $$$10^2 = 100$$$ are round numbers, but $$$20$$$, $$$110$$$ and $$$256$$$ are not round numbers. So, if an item is worth $$$m$$$ bourles (the value of the item is not greater than $$$10^9$$$), the sellers want to change its value to the nearest round number that is not greater than $$$m$$$. They ask you: by how many bourles should you decrease the value of the item to make it worth exactly $$$10^k$$$ bourles, where the value of $$$k$$$ — is the maximum possible ($$$k$$$ — any non-negative integer).For example, let the item have a value of $$$178$$$-bourles. Then the new price of the item will be $$$100$$$, and the answer will be $$$178-100=78$$$.", "c_code": "int solution() {\n long long int n;\n long long int t;\n scanf(\"%lld\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%lld\", &n);\n if (n >= 1 && n < 10) {\n printf(\"%lld\\n\", n - 1);\n } else if (n >= 10 && n < 100) {\n printf(\"%lld\\n\", n - 10);\n } else if (n >= 100 && n < 1000) {\n printf(\"%lld\\n\", n - 100);\n } else if (n >= 1000 && n < 10000) {\n printf(\"%lld\\n\", n - 1000);\n } else if (n >= 10000 && n < 100000) {\n printf(\"%lld\\n\", n - 10000);\n } else if (n >= 100000 && n < 1000000) {\n printf(\"%lld\\n\", n - 100000);\n } else if (n >= 1000000 && n < 10000000) {\n printf(\"%lld\\n\", n - 1000000);\n } else if (n >= 10000000 && n < 100000000) {\n printf(\"%lld\\n\", n - 10000000);\n } else if (n >= 100000000 && n < 1000000000) {\n printf(\"%lld\\n\", n - 100000000);\n } else {\n printf(\"%lld\\n\", n - 1000000000);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"Error!\");\n let x: i32 = line.trim().parse().expect(\"Error!\");\n\n for _ in 0..x {\n let mut price = String::new();\n\n io::stdin().read_line(&mut price).expect(\"Error!\");\n let _price: i32 = price.trim().parse().expect(\"Error!\");\n\n let _power = price.len() as u32;\n let res = 10i32.pow(_power - 3);\n println!(\"{}\", _price - res);\n }\n}", "difficulty": "hard"} {"problem_id": "0583", "problem_description": "You are given two huge binary integer numbers $$$a$$$ and $$$b$$$ of lengths $$$n$$$ and $$$m$$$ respectively. You will repeat the following process: if $$$b > 0$$$, then add to the answer the value $$$a~ \\&~ b$$$ and divide $$$b$$$ by $$$2$$$ rounding down (i.e. remove the last digit of $$$b$$$), and repeat the process again, otherwise stop the process.The value $$$a~ \\&~ b$$$ means bitwise AND of $$$a$$$ and $$$b$$$. Your task is to calculate the answer modulo $$$998244353$$$.Note that you should add the value $$$a~ \\&~ b$$$ to the answer in decimal notation, not in binary. So your task is to calculate the answer in decimal notation. For example, if $$$a = 1010_2~ (10_{10})$$$ and $$$b = 1000_2~ (8_{10})$$$, then the value $$$a~ \\&~ b$$$ will be equal to $$$8$$$, not to $$$1000$$$.", "c_code": "int solution(void) {\n int n;\n int m;\n int i;\n int j;\n int bs[200001];\n int dif;\n char a[200000];\n char b[200000];\n long long int ans = 0;\n long long int p2 = 1;\n\n scanf(\"%d %d\", &n, &m);\n scanf(\"%s\", &a);\n scanf(\"%s\", &b);\n bs[0] = 1;\n\n for (i = 1; i < m; i++) {\n bs[i] = bs[i - 1];\n if (b[i] == '1') {\n bs[i]++;\n }\n }\n\n dif = m - n;\n\n for (i = n - 1; i >= 0 && (i + dif) >= 0; i--) {\n ans = ans + (a[i] - 48) * bs[i + dif] * p2;\n ans = ans % 998244353;\n p2 = p2 * 2;\n p2 = p2 % 998244353;\n }\n\n printf(\"%d\", ans);\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 mut str1 = arr[2].parse::().unwrap();\n let mut str2 = arr[3].parse::().unwrap();\n\n let mut x: i64 = 1;\n let mut y: i64 = 0;\n let mut ans: i64 = 0;\n let mo: i64 = 998244353;\n loop {\n if let Some(c) = str1.pop() {\n if c == '1' {\n y = (y + x) % mo\n }\n };\n match str2.pop() {\n Some(c) => {\n if c == '1' {\n ans = (ans + y) % mo\n }\n }\n None => break,\n };\n x = (x * 2) % mo;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0584", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq a \\leq 10
  • \n
  • a is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a\n
\n
\n
\n
\n
\n

Output

Print the value a + a^2 + a^3 as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n
\n
\n
\n
\n
\n

Sample Output 1

14\n
\n

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

\n

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\n
\n
\n
\n
\n
\n

Sample Output 2

1110\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n scanf(\"%d\", &a);\n printf(\"%d\", a + (a * a) + (a * a * a));\n return 0;\n}", "rust_code": "fn solution() {\n let mut guess = String::new();\n io::stdin()\n .read_line(&mut guess)\n .expect(\"Failed to read line\");\n\n let guess: f64 = guess.trim().parse().expect(\"Please type a number!\");\n\n println!(\"{}\", guess + guess * guess + guess * guess * guess);\n}", "difficulty": "medium"} {"problem_id": "0585", "problem_description": "You are given an integer $$$n$$$. You have to construct a permutation of size $$$n$$$.A permutation is an array where each integer from $$$1$$$ to $$$s$$$ (where $$$s$$$ is the size of permutation) occurs exactly once. For example, $$$[2, 1, 4, 3]$$$ is a permutation of size $$$4$$$; $$$[1, 2, 4, 5, 3]$$$ is a permutation of size $$$5$$$; $$$[1, 4, 3]$$$ is not a permutation (the integer $$$2$$$ is absent), $$$[2, 1, 3, 1]$$$ is not a permutation (the integer $$$1$$$ appears twice).A subsegment of a permutation is a contiguous subsequence of that permutation. For example, the permutation $$$[2, 1, 4, 3]$$$ has $$$10$$$ subsegments: $$$[2]$$$, $$$[2, 1]$$$, $$$[2, 1, 4]$$$, $$$[2, 1, 4, 3]$$$, $$$[1]$$$, $$$[1, 4]$$$, $$$[1, 4, 3]$$$, $$$[4]$$$, $$$[4, 3]$$$ and $$$[3]$$$.The value of the permutation is the number of its subsegments which are also permutations. For example, the value of $$$[2, 1, 4, 3]$$$ is $$$3$$$ since the subsegments $$$[2, 1]$$$, $$$[1]$$$ and $$$[2, 1, 4, 3]$$$ are permutations.You have to construct a permutation of size $$$n$$$ with minimum possible value among all permutations of size $$$n$$$.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t) {\n int n = 0;\n scanf(\"%d\", &n);\n int arr[100];\n int tmp1 = 0;\n int tmp2 = n - 1;\n for (int i = 0; i < n; i++) {\n if ((i + 1) % 2 != 0) {\n arr[tmp1] = i + 1;\n tmp1++;\n }\n if ((i + 1) % 2 == 0) {\n arr[tmp2] = i + 1;\n tmp2--;\n }\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d \", arr[i]);\n }\n printf(\"\\n\");\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let test_cases = {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"read failed\");\n s.trim().parse::().unwrap()\n };\n\n for _ in 0..test_cases {\n let n = {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"read failed\");\n s.trim().parse::().unwrap()\n };\n\n let mut v = Vec::::new();\n v.push(1);\n v.push(n);\n for i in 2..n {\n v.push(i);\n }\n\n for (i, x) in v.iter().enumerate() {\n print!(\"{}{}\", x, if i + 1 == v.len() { \"\\n\" } else { \" \" });\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0586", "problem_description": "A telephone number is a sequence of exactly $$$11$$$ digits such that its first digit is 8.Vasya and Petya are playing a game. Initially they have a string $$$s$$$ of length $$$n$$$ ($$$n$$$ is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a character and erase it from the current string. For example, if the current string 1121, after the player's move it may be 112, 111 or 121. The game ends when the length of string $$$s$$$ becomes 11. If the resulting string is a telephone number, Vasya wins, otherwise Petya wins.You have to determine if Vasya has a winning strategy (that is, if Vasya can win the game no matter which characters Petya chooses during his moves).", "c_code": "int solution() {\n int n = 0;\n char s[100000];\n scanf(\"%d\", &n);\n scanf(\"%s\", s);\n int moves = (n - 11) / 2;\n int eights[100000];\n int eights_counter = 0;\n for (int i = 0; i < n - 10; i++) {\n if (s[i] == '8') {\n eights[eights_counter++] = n - i - 11;\n }\n }\n\n if (eights_counter <= moves) {\n printf(\"NO\");\n } else {\n printf(\"YES\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = \"\".split_ascii_whitespace();\n let mut read = || loop {\n if let Some(word) = input.next() {\n break word;\n }\n input = {\n let mut input = \"\".to_owned();\n io::stdin().read_line(&mut input).unwrap();\n if input.is_empty() {\n panic!(\"reached EOF\");\n }\n Box::leak(input.into_boxed_str()).split_ascii_whitespace()\n };\n };\n\n\n let n = read!(usize);\n let s = read!(String);\n\n let a = s.chars().collect::>();\n let mut c8 = 0;\n for i in 0..(n - 10) {\n if a[i] == '8' {\n c8 += 1;\n }\n }\n\n let r = if (n - 9) / 2 <= c8 { \"YES\" } else { \"NO\" };\n println!(\"{}\", r);\n}", "difficulty": "easy"} {"problem_id": "0587", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.\nYou are given a sequence h=\\{h_1,h_2,h_3,......\\} as input. You would like to change the height of Flower k to h_k for all k (1 \\leq k \\leq N), by repeating the following \"watering\" operation:

\n
    \n
  • Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \\leq x \\leq r.
  • \n
\n

Find the minimum number of watering operations required to satisfy the condition.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 0 \\leq h_i \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nh_1 h_2 h_3 ...... h_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of watering operations required to satisfy the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n1 2 2 1\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The minimum number of watering operations required is 2.\nOne way to achieve it is:

\n
    \n
  • Perform the operation with (l,r)=(1,3).
  • \n
  • Perform the operation with (l,r)=(2,4).
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5\n3 1 2 3 1\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8\n4 23 75 0 23 96 50 100\n
\n
\n
\n
\n
\n

Sample Output 3

221\n
\n
\n
", "c_code": "int solution() {\n int N;\n scanf(\"%d\", &N);\n int h[102];\n int table[102] = {0};\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", h + i);\n }\n int ans = 0;\n int f = 0;\n int changed = 0;\n do {\n changed = 0;\n for (int j = 0; j < N; j++) {\n if (f == 0 && table[j] < h[j]) {\n f = 1;\n table[j]++;\n changed = 1;\n } else if (f == 1 && table[j] < h[j]) {\n table[j]++;\n } else if (f == 1 && table[j] >= h[j]) {\n f = 0;\n ans++;\n }\n }\n if (f == 1) {\n ans++;\n f = 0;\n }\n } while (changed);\n printf(\"%d\", ans);\n}", "rust_code": "fn solution() {\n let text = {\n use std::io::Read;\n let mut s = String::new();\n std::io::stdin().read_to_string(&mut s).unwrap();\n s\n };\n let mut iter = text.split_whitespace();\n let _ = iter.next();\n let mut sum = 0i32;\n let mut prev = 0i32;\n for height in iter.map(|word| word.parse::().unwrap()) {\n if prev < height {\n sum += height - prev;\n }\n prev = height;\n }\n println!(\"{}\", sum);\n}", "difficulty": "hard"} {"problem_id": "0588", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.\nHe remembers that the remainder of a divided by b was greater than or equal to K.\nFind the number of possible pairs that he may have had.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 0 \\leq K \\leq N-1
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

Print the number of possible pairs that he may have had.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).

\n
\n
\n
\n
\n
\n

Sample Input 2

10 0\n
\n
\n
\n
\n
\n

Sample Output 2

100\n
\n
\n
\n
\n
\n
\n

Sample Input 3

31415 9265\n
\n
\n
\n
\n
\n

Sample Output 3

287927211\n
\n
\n
", "c_code": "int solution(void) {\n unsigned long long n;\n unsigned long long k;\n unsigned long long i;\n unsigned long long cnt;\n\n scanf(\"%llu %llu\", &n, &k);\n\n cnt = 0;\n if (k == 0) {\n printf(\"%llu\\n\", n * n);\n return 0;\n }\n\n for (i = 1; i <= n; i++) {\n if (i > k) {\n cnt += (i - k) * (n - n % i) / i;\n cnt += (n % i) >= k ? (n % i) - k + 1 : 0;\n }\n }\n\n printf(\"%llu\\n\", cnt);\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: u64 = itr.next().unwrap().parse().unwrap();\n let k: u64 = itr.next().unwrap().parse().unwrap();\n\n let mut ans: u64 = 0;\n for b in k + 1..n + 1 {\n let div = n / b;\n ans += div * (b - k);\n if div * b + k <= n {\n ans += n - (div * b + k) + 1;\n }\n }\n if k == 0 {\n ans -= n;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0589", "problem_description": "You went to the store, selling $$$n$$$ types of chocolates. There are $$$a_i$$$ chocolates of type $$$i$$$ in stock.You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $$$x_i$$$ chocolates of type $$$i$$$ (clearly, $$$0 \\le x_i \\le a_i$$$), then for all $$$1 \\le j < i$$$ at least one of the following must hold: $$$x_j = 0$$$ (you bought zero chocolates of type $$$j$$$) $$$x_j < x_i$$$ (you bought less chocolates of type $$$j$$$ than of type $$$i$$$) For example, the array $$$x = [0, 0, 1, 2, 10]$$$ satisfies the requirement above (assuming that all $$$a_i \\ge x_i$$$), while arrays $$$x = [0, 1, 0]$$$, $$$x = [5, 5]$$$ and $$$x = [3, 2]$$$ don't.Calculate the maximum number of chocolates you can buy.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n long long sum = 0;\n for (int i = n - 1; i >= 0; i--) {\n sum += arr[i];\n if (arr[i] == 1) {\n break;\n }\n if ((i - 1) >= 0 && arr[i - 1] >= arr[i]) {\n arr[i - 1] = arr[i] - 1;\n }\n }\n printf(\"%lld\", sum);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_to_string(&mut buf).unwrap();\n let mut tok = buf.split_whitespace();\n let mut get = || tok.next().unwrap();\n\n\n let n = get!(usize);\n let mut xs = vec![];\n\n for _ in 0..n {\n xs.push(get!());\n }\n\n xs.reverse();\n\n let mut ans = 0_i64;\n let mut last = std::i64::MAX;\n for &x in xs.iter() {\n if x < last {\n ans += x;\n last = x;\n } else {\n last -= 1;\n if last > 0 {\n ans += last;\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0590", "problem_description": "It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last n days. So now he has a sequence a1, a2, ..., an, where ai is the sleep time on the i-th day.The number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider k consecutive days as a week. So there will be n - k + 1 weeks to take into consideration. For example, if k = 2, n = 3 and a = [3, 4, 7], then the result is .You should write a program which will calculate average sleep times of Polycarp over all weeks.", "c_code": "int solution() {\n int k;\n int n;\n scanf(\"%d %d\", &n, &k);\n int weeks = (n - k) + 1;\n int a[n];\n double soma = 0.0;\n double interm_soma = 0.0;\n int start_counting = 0;\n int teste = 0;\n for (int i = 0; i < n; i++) {\n if (i + 1 == k) {\n start_counting = 1;\n }\n scanf(\"%d\", &a[i]);\n interm_soma += a[i];\n if (start_counting) {\n if (teste) {\n interm_soma -= a[i - k];\n }\n teste = 1;\n soma += interm_soma;\n }\n }\n printf(\"%.6lf\\n\", soma / weeks);\n return 0;\n}", "rust_code": "fn solution() {\n let mut fline = String::new();\n let mut nums = String::new();\n io::stdin()\n .read_line(&mut fline)\n .expect(\"Can't input first line\");\n io::stdin()\n .read_line(&mut nums)\n .expect(\"can't input second line\");\n nums = nums.trim().parse().expect(\"Can't parse\");\n let inpvec: Vec<&str> = fline.split(\" \").collect();\n let n: u32 = inpvec[0].trim().parse().expect(\"Unable to parse n\");\n let k: u32 = inpvec[1].trim().parse().expect(\"Unable to parse k\");\n let mut sum: f64 = 0f64;\n let numl: Vec<&str> = nums.split(\" \").collect();\n let mut inum: u32;\n let mut count: u32;\n for i in 0..n {\n inum = numl[i as usize].trim().parse().expect(\"Fucked up!\");\n count = cmp::min(i + 1, n - i);\n count = cmp::min(count, n - k + 1);\n count = cmp::min(count, k);\n sum += count as f64 * inum as f64;\n }\n\n sum /= (n - k + 1) as f64;\n println!(\"{}\", sum);\n}", "difficulty": "medium"} {"problem_id": "0591", "problem_description": "Alice and Bob are playing a game on a matrix, consisting of $$$2$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row in the $$$j$$$-th column contains $$$a_{i, j}$$$ coins in it.Initially, both Alice and Bob are standing in a cell $$$(1, 1)$$$. They are going to perform a sequence of moves to reach a cell $$$(2, m)$$$.The possible moves are: Move right — from some cell $$$(x, y)$$$ to $$$(x, y + 1)$$$; Move down — from some cell $$$(x, y)$$$ to $$$(x + 1, y)$$$. First, Alice makes all her moves until she reaches $$$(2, m)$$$. She collects the coins in all cells she visit (including the starting cell).When Alice finishes, Bob starts his journey. He also performs the moves to reach $$$(2, m)$$$ and collects the coins in all cells that he visited, but Alice didn't.The score of the game is the total number of coins Bob collects.Alice wants to minimize the score. Bob wants to maximize the score. What will the score of the game be if both players play optimally?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int m;\n scanf(\"%d\", &m);\n int arr[2][m];\n int sumu = 0;\n int sum1 = 0;\n int min = 1000000000;\n int max = 0;\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &arr[0][i]);\n sumu += arr[0][i];\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &arr[1][i]);\n }\n int out = 0;\n for (int i = 0; i < m; i++) {\n sumu = sumu - arr[0][i];\n if (i != 0) {\n sum1 += arr[1][i - 1];\n }\n if (sumu >= sum1) {\n max = sumu;\n } else if (sum1 > sumu) {\n max = sum1;\n }\n if (max < min) {\n min = max;\n }\n }\n printf(\"%d\\n\", min);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n stdin.read_line(&mut line).unwrap();\n let t: usize = line.trim().parse().unwrap();\n\n let stdout = io::stdout();\n let handle = stdout.lock();\n let mut buffer = BufWriter::with_capacity(65536, handle);\n let mut first_row_sum: Vec = Vec::new();\n let mut second_row_sum: Vec = Vec::new();\n for _t in 0..t {\n line.clear();\n first_row_sum.clear();\n second_row_sum.clear();\n first_row_sum.push(0);\n second_row_sum.push(0);\n\n stdin.read_line(&mut line).unwrap();\n let column_count: usize = line.trim().parse().unwrap();\n line.clear();\n\n stdin.read_line(&mut line).unwrap();\n for coins in line.split_whitespace() {\n first_row_sum.push(*first_row_sum.last().unwrap() + coins.parse::().unwrap());\n }\n line.clear();\n\n stdin.read_line(&mut line).unwrap();\n for coins in line.split_whitespace() {\n second_row_sum.push(*second_row_sum.last().unwrap() + coins.parse::().unwrap());\n }\n\n let mut bob_max_score: u64 = u64::MAX;\n for i in 0..column_count {\n let bob_top_score = first_row_sum[column_count] - first_row_sum[i + 1];\n let bob_bottom_score = second_row_sum[i];\n bob_max_score = std::cmp::min(\n bob_max_score,\n std::cmp::max(bob_top_score, bob_bottom_score),\n );\n }\n\n writeln!(&mut buffer, \"{}\", bob_max_score).unwrap();\n }\n buffer.flush().unwrap();\n}", "difficulty": "medium"} {"problem_id": "0592", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, , Katana N, and can perform the following two kinds of attacks in any order:

\n
    \n
  • Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times.
  • \n
  • Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.
  • \n
\n

The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 10^5
  • \n
  • 1 ≤ H ≤ 10^9
  • \n
  • 1 ≤ a_i ≤ b_i ≤ 10^9
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N H\na_1 b_1\n:\na_N b_N\n
\n
\n
\n
\n
\n

Output

Print the minimum total number of attacks required to vanish the monster.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 10\n3 5\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 10\n3 5\n2 6\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

In addition to the katana above, you also have another katana. Wielding it deals 2 points of damage, and throwing it deals 6 points of damage. By throwing both katana, you will deal 5 + 6 = 11 points of damage in two attacks, vanishing the monster.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 1000000000\n1 1\n1 10000000\n1 30000000\n1 99999999\n
\n
\n
\n
\n
\n

Sample Output 3

860000004\n
\n
\n
\n
\n
\n
\n

Sample Input 4

5 500\n35 44\n28 83\n46 62\n31 79\n40 43\n
\n
\n
\n
\n
\n

Sample Output 4

9\n
\n
\n
", "c_code": "int solution() {\n int s[200000][5];\n int n;\n int i;\n int j;\n int HP;\n int max;\n int katana;\n int exdamage = 0;\n int exkatana = 0;\n int result = 0;\n int exmax;\n int memo;\n int k;\n int l;\n int s2[100001];\n scanf(\"%d\", &n);\n scanf(\"%d\", &HP);\n\n for (i = 0; i < n; i++) {\n for (j = 0; j <= 1; j++) {\n scanf(\"%d\", &s[i][j]);\n }\n }\n\n for (i = 0; i < n; i++) {\n if (s[i][0] > max) {\n max = s[i][0];\n }\n }\n for (i = 0; i < n; i++) {\n\n if (s[i][1] >= max) {\n exdamage = exdamage + s[i][1];\n exkatana++;\n }\n }\n\n if (exdamage <= HP) {\n\n for (i = 0; i < 10000000000; i++) {\n if (exdamage >= HP) {\n result = result + exkatana;\n printf(\"%d\", result);\n break;\n }\n HP = HP - max;\n result++;\n if (HP <= 0) {\n printf(\"%d\", result);\n break;\n }\n }\n\n } else {\n\n exmax = 0;\n for (j = 0; j < 100010; j++) {\n l = 1;\n for (i = 0; i < n; i++) {\n\n if (s[i][1] > exmax) {\n l = 1;\n exmax = s[i][1];\n s2[0] = i;\n } else if (s[i][1] == exmax) {\n s2[l] = i;\n l++;\n }\n }\n for (i = 0; i < l; i++) {\n HP = HP - exmax;\n result++;\n s[s2[i]][1] = 0;\n if (HP <= 0) {\n printf(\"%d\", result);\n break;\n }\n }\n exmax = 0;\n if (HP <= 0) {\n break;\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (n, mut h) = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s = s.trim_end().to_owned();\n let mut ws = s.split_whitespace();\n let n: i32 = ws.next().unwrap().parse().unwrap();\n let h: i32 = ws.next().unwrap().parse().unwrap();\n (n, h)\n };\n\n let mut v = Vec::new();\n let mut max_a = 0;\n\n for _ in 0..n {\n let (a, b) = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s = s.trim_end().to_owned();\n let mut ws = s.split_whitespace();\n let a: i32 = ws.next().unwrap().parse().unwrap();\n let b: i32 = ws.next().unwrap().parse().unwrap();\n (a, b)\n };\n\n max_a = std::cmp::max(max_a, a);\n v.push(b);\n }\n\n v.sort_by(|a, b| b.cmp(a));\n\n let mut ans = 0;\n\n for e in v {\n if h <= 0 {\n break;\n }\n\n if e > max_a {\n h -= e;\n ans += 1;\n } else {\n break;\n }\n }\n\n if h > 0 {\n ans += h / max_a;\n ans += if h % max_a == 0 { 0 } else { 1 };\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0593", "problem_description": "This is an interactive problem.Initially, there is an array $$$a = [1, 2, \\ldots, n]$$$, where $$$n$$$ is an odd positive integer. The jury has selected $$$\\frac{n-1}{2}$$$ disjoint pairs of elements, and then the elements in those pairs are swapped. For example, if $$$a=[1,2,3,4,5]$$$, and the pairs $$$1 \\leftrightarrow 4$$$ and $$$3 \\leftrightarrow 5$$$ are swapped, then the resulting array is $$$[4, 2, 5, 1, 3]$$$. As a result of these swaps, exactly one element will not change position. You need to find this element.To do this, you can ask several queries. In each query, you can pick two integers $$$l$$$ and $$$r$$$ ($$$1 \\leq l \\leq r \\leq n$$$). In return, you will be given the elements of the subarray $$$[a_l, a_{l + 1}, \\dots, a_r]$$$ sorted in increasing order. Find the element which did not change position. You can make at most $$$\\mathbf{15}$$$ queries.The array $$$a$$$ is fixed before the interaction and does not change after your queries.Recall that an array $$$b$$$ is a subarray of the array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.", "c_code": "int solution() {\n int i;\n int T;\n int n;\n int s;\n int d;\n int c;\n int temp;\n int count;\n scanf(\"%d\", &T);\n while (T--) {\n scanf(\"%d\", &n);\n s = 1;\n d = n;\n while (s < d) {\n c = s + d >> 1;\n printf(\"? %d %d\\n\", s, c);\n fflush(stdout);\n count = 0;\n for (i = s; i <= c; i++) {\n scanf(\"%d\", &temp);\n if (temp >= s && temp <= c) {\n count++;\n }\n }\n if (count & 1) {\n d = c;\n } else {\n s = c + 1;\n }\n }\n printf(\"! %d\\n\", s);\n fflush(stdout);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let num_tests: usize = io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse()\n .unwrap();\n for _ in 0..num_tests {\n let n: i32 = io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse()\n .unwrap();\n let mut left = 1;\n let mut right = n;\n while left < right {\n let middle = (left + right) / 2;\n println!(\"? {left} {middle}\");\n io::stdout().flush();\n let a: Vec = io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse::().unwrap())\n .collect();\n let mut num_inside = 0;\n for k in a {\n if k >= left && k <= middle {\n num_inside += 1;\n }\n }\n if num_inside % 2 == 0 {\n left = middle + 1;\n } else {\n right = middle;\n }\n }\n println!(\"! {left}\");\n io::stdout().flush();\n }\n}", "difficulty": "medium"} {"problem_id": "0594", "problem_description": "You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \\ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$.", "c_code": "int solution() {\n long long int a;\n long long int b;\n long long int k;\n long long int i;\n scanf(\"%lld %lld %lld\", &a, &b, &k);\n if (b == 1) {\n if (k > 0) {\n printf(\"No\\n\");\n } else {\n printf(\"Yes\\n\");\n printf(\"1\");\n for (i = 1; i <= a; i++) {\n printf(\"0\");\n }\n printf(\"\\n\");\n printf(\"1\");\n for (i = 1; i <= a; i++) {\n printf(\"0\");\n }\n printf(\"\\n\");\n }\n } else {\n if (a == 0) {\n if (k > 0) {\n printf(\"No\\n\");\n } else {\n printf(\"Yes\\n\");\n for (i = 1; i <= b; i++) {\n printf(\"1\");\n }\n printf(\"\\n\");\n for (i = 1; i <= b; i++) {\n printf(\"1\");\n }\n printf(\"\\n\");\n }\n } else {\n if (a >= k) {\n printf(\"Yes\\n\");\n for (i = 1; i <= b - 1; i++) {\n printf(\"1\");\n }\n for (i = 1; i <= a - k; i++) {\n printf(\"0\");\n }\n printf(\"1\");\n for (i = 1; i <= k; i++) {\n printf(\"0\");\n }\n printf(\"\\n\");\n for (i = 1; i <= b - 1; i++) {\n printf(\"1\");\n }\n for (i = 1; i <= a - k; i++) {\n printf(\"0\");\n }\n for (i = 1; i <= k; i++) {\n printf(\"0\");\n }\n printf(\"1\");\n printf(\"\\n\");\n } else {\n if (b >= k + 1) {\n printf(\"Yes\\n\");\n for (i = 1; i <= b - k; i++) {\n printf(\"1\");\n }\n for (i = 1; i <= a - 1; i++) {\n printf(\"0\");\n }\n for (i = 1; i <= k; i++) {\n printf(\"1\");\n }\n printf(\"0\");\n printf(\"\\n\");\n for (i = 1; i <= b - k; i++) {\n printf(\"1\");\n }\n for (i = 1; i <= a - 1; i++) {\n printf(\"0\");\n }\n printf(\"0\");\n for (i = 1; i <= k; i++) {\n printf(\"1\");\n }\n printf(\"\\n\");\n } else {\n if (k <= a + b - 3) {\n printf(\"Yes\\n\");\n for (i = 1; i <= b + a - k - 2; i++) {\n printf(\"1\");\n }\n printf(\"1\");\n for (i = 1; i <= a - 1; i++) {\n printf(\"0\");\n }\n for (i = 1; i <= k - a + 1; i++) {\n printf(\"1\");\n }\n printf(\"0\");\n printf(\"\\n\");\n for (i = 1; i <= b + a - k - 2; i++) {\n printf(\"1\");\n }\n for (i = 1; i <= a - 1; i++) {\n printf(\"0\");\n }\n printf(\"1\");\n printf(\"0\");\n for (i = 1; i <= k - a + 1; i++) {\n printf(\"1\");\n }\n printf(\"\\n\");\n } else {\n if (k == a + b - 2) {\n printf(\"Yes\\n\");\n printf(\"11\");\n for (i = 1; i <= a - 1; i++) {\n printf(\"0\");\n }\n for (i = 1; i <= b - 2; i++) {\n printf(\"1\");\n }\n printf(\"0\");\n printf(\"\\n\");\n printf(\"1\");\n for (i = 1; i <= a; i++) {\n printf(\"0\");\n }\n for (i = 1; i <= b - 1; i++) {\n printf(\"1\");\n }\n printf(\"\\n\");\n\n } else {\n printf(\"No\\n\");\n }\n }\n }\n }\n }\n }\n}", "rust_code": "fn solution() -> std::io::Result<()> {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf)?;\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n let (a, b, k) = scan!(usize, usize, usize);\n if a == 0 {\n if k == 0 {\n print!(\"YES\\n\");\n for _ in 0..b {\n print!(\"1\");\n }\n print!(\"\\n\");\n for _ in 0..b {\n print!(\"1\");\n }\n print!(\"\\n\");\n } else {\n print!(\"NO\\n\");\n }\n } else if b == 1 {\n if k == 0 {\n print!(\"YES\\n1\");\n for _ in 0..a {\n print!(\"0\");\n }\n print!(\"\\n1\");\n for _ in 0..a {\n print!(\"0\");\n }\n print!(\"\\n\");\n } else {\n print!(\"NO\\n\");\n }\n } else {\n if k < a + b - 1 {\n print!(\"YES\\n\");\n let mut x = Vec::with_capacity(a + b);\n for _ in 0..a {\n x.push('0');\n }\n for _ in 0..b {\n x.push('1');\n }\n let n = x.len();\n let mut y = x.clone();\n if x[k] == '1' {\n y[k] = '0';\n y[0] = '1';\n } else {\n x[k] = '1';\n x[n - 2] = '0';\n y[k] = '1';\n y[n - 2] = '0';\n y[k] = '0';\n y[0] = '1';\n }\n\n for &c in x.iter().rev() {\n print!(\"{}\", c);\n }\n print!(\"\\n\");\n\n for &c in y.iter().rev() {\n print!(\"{}\", c);\n }\n print!(\"\\n\");\n } else {\n print!(\"NO\\n\");\n }\n }\n\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "0595", "problem_description": "

Triangle


\n\n

\n For given two sides of a triangle a and b and the angle C between them, calculate the following properties:\n

\n\n
    \n
  • S: Area of the triangle
  • \n
  • L: The length of the circumference of the triangle
  • \n
  • h: The height of the triangle with side a as a bottom edge
  • \n
\n\n

Input

\n\n

\n The length of a, the length of b and the angle C are given in integers.\n

\n\n

Output

\n\n

\n Print S, L and h in a line respectively. The output should not contain an absolute error greater than 10-4.\n

\n\n

Sample Input

\n\n
\n4 3 90\n
\n\n

Sample Output

\n\n
\n6.00000000\n12.00000000\n3.00000000\n
", "c_code": "int solution() {\n double a;\n double b;\n double c;\n double PI = acos(-1);\n scanf(\"%lf %lf %lf\", &a, &b, &c);\n c /= 180.0;\n c *= PI;\n printf(\"%lf\\n%lf\\n%lf\\n\", a * b * 0.5 * sin(c),\n a + b + sqrt((a * a) + (b * b) - (2.0 * a * b * cos(c))),\n a * b * sin(c) / a);\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\n let mut ite = buf.split_whitespace().map(|x| x.parse::().unwrap());\n\n let a = ite.next().unwrap();\n let b = ite.next().unwrap();\n let c = ite.next().unwrap();\n\n let rad = c.to_radians();\n\n println!(\"{}\", a * b * rad.sin() * 0.5);\n println!(\n \"{}\",\n (a.powi(2) + b.powi(2) - 2. * a * b * rad.cos()).sqrt() + a + b\n );\n println!(\"{}\", b * rad.sin());\n}", "difficulty": "easy"} {"problem_id": "0596", "problem_description": "Nezzar has $$$n$$$ balls, numbered with integers $$$1, 2, \\ldots, n$$$. Numbers $$$a_1, a_2, \\ldots, a_n$$$ are written on them, respectively. Numbers on those balls form a non-decreasing sequence, which means that $$$a_i \\leq a_{i+1}$$$ for all $$$1 \\leq i < n$$$.Nezzar wants to color the balls using the minimum number of colors, such that the following holds. For any color, numbers on balls will form a strictly increasing sequence if he keeps balls with this chosen color and discards all other balls. Note that a sequence with the length at most $$$1$$$ is considered as a strictly increasing sequence.Please help Nezzar determine the minimum number of colors.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n int repeat = 0;\n int max = 0;\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n\n if (arr[i] == arr[i - 1]) {\n repeat++;\n }\n\n else {\n\n if (repeat > max) {\n max = repeat;\n }\n repeat = 0;\n }\n }\n if (repeat > max) {\n max = repeat;\n }\n printf(\"%d\\n\", max + 1);\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 for l in i.lock().lines().skip(2).step_by(2) {\n let mut a = [0; 101];\n l.unwrap()\n .split(' ')\n .for_each(|w| a[w.parse::().unwrap()] += 1);\n writeln!(o, \"{}\", a.iter().max().unwrap()).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "0597", "problem_description": "There are $$$n$$$ students standing in a circle in some order. The index of the $$$i$$$-th student is $$$p_i$$$. It is guaranteed that all indices of students are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).Students want to start a round dance. A clockwise round dance can be started if the student $$$2$$$ comes right after the student $$$1$$$ in clockwise order (there are no students between them), the student $$$3$$$ comes right after the student $$$2$$$ in clockwise order, and so on, and the student $$$n$$$ comes right after the student $$$n - 1$$$ in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student $$$i$$$ should be right after the student $$$i - 1$$$ in counterclockwise order (this condition should be met for every $$$i$$$ from $$$2$$$ to $$$n$$$). For example, if the indices of students listed in clockwise order are $$$[2, 3, 4, 5, 1]$$$, then they can start a clockwise round dance. If the students have indices $$$[3, 2, 1, 4]$$$ in clockwise order, then they can start a counterclockwise round dance.Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle. You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int t;\n scanf(\" %d\", &t);\n while (t--) {\n int n;\n scanf(\" %d\", &n);\n int nums[n];\n for (int i = 0; i < n; ++i) {\n scanf(\" %d\", &nums[i]);\n }\n int first = nums[0];\n int po = 1;\n for (int i = 1; i < n && po; ++i) {\n if (nums[i] - 1 != first && (nums[i] != 1 || first != n)) {\n po = 0;\n }\n first = nums[i];\n }\n if (po) {\n puts(\"YES\");\n continue;\n }\n first = nums[0];\n po = 1;\n for (int i = 1; i < n && po; ++i) {\n if (nums[i] + 1 != first && (first != 1 || nums[i] != n)) {\n po = 0;\n }\n first = nums[i];\n }\n if (po) {\n puts(\"YES\");\n } else {\n puts(\"NO\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n\n let stdout = io::stdout();\n let mut out = io::BufWriter::with_capacity(2 * 1024 * 1024, stdout.lock());\n\n\n let q = next!(u32);\n for _ in 0..q {\n let n = next!(u32);\n let (r, _) = next!()\n .split_ascii_whitespace()\n .map(|w| w.parse().unwrap())\n .fold((true, 0), |(f, p), e| {\n if f && p != 0 {\n let mut p1 = p + 1;\n if p1 > n {\n p1 = 1;\n }\n let mut p2 = p - 1;\n if p2 < 1 {\n p2 = n;\n }\n (p1 == e || p2 == e, e)\n } else {\n (f, e)\n }\n });\n put!(\"{}\\n\", if r { \"YES\" } else { \"NO\" })\n }\n\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "0598", "problem_description": "\n

Space Coconut Crab

\n\n\n

宇宙ヤシガニ

\n\n\n\n\n\n

\nEnglish text is not available in this practice contest.\n

\n\n\n

\nケン・マリンブルーは,宇宙ヤシガニを求めて全銀河を旅するスペースハンターである.宇宙ヤシガニは,宇宙最大とされる甲殻類であり,成長後の体長は 400 メートル以上,足を広げれば 1,000 メートル以上にもなると言われている.既に多数の人々が宇宙ヤシガニを目撃しているが,誰一人として捕らえることに成功していない.\n

\n

\nケンは,長期間の調査によって,宇宙ヤシガニの生態に関する重要な事実を解明した.宇宙ヤシガニは,驚くべきことに,相転移航法と呼ばれる最新のワープ技術と同等のことを行い,通常空間と超空間の間を往来しながら生きていた.さらに,宇宙ヤシガニが超空間から通常空間にワープアウトするまでには長い時間がかかり,またワープアウトしてからしばらくは超空間に移動できないことも突き止めた.\n

\n

\nそこで,ケンはついに宇宙ヤシガニの捕獲に乗り出すことにした.戦略は次のとおりである.はじめに,宇宙ヤシガニが通常空間から超空間に突入する際のエネルギーを観測する.このエネルギーを e とするとき,宇宙ヤシガニが超空間からワープアウトする座標 (x, y, z) は以下の条件を満たすことがわかっている.\n

\n
    \n
  • x, y, z はいずれも非負の整数である.
  • \n
  • x + y2 + z3 = e である.
  • \n
  • 上記の条件の下で x + y + z の値を最小にする.
  • \n
\n

\nこれらの条件だけでは座標が一意に決まるとは限らないが,x + y + z の最小値を m としたときに,ワープアウトする座標が平面 x + y + z = m 上にあることは確かである.そこで,この平面上に十分な大きさのバリアを張る.すると,宇宙ヤシガニはバリアの張られたところにワープアウトすることになる.バリアの影響を受けた宇宙ヤシガニは身動きがとれなくなる.そこをケンの操作する最新鋭宇宙船であるウェポン・ブレーカー号で捕獲しようという段取りである.\n

\n

\nバリアは一度しか張ることができないため,失敗するわけにはいかない.そこでケンは,任務の遂行にあたって計算機の助けを借りることにした.あなたの仕事は,宇宙ヤシガニが超空間に突入する際のエネルギーが与えられたときに,バリアを張るべき平面 x + y + z = m を求めるプログラムを書くことである.用意されたテストケースの全てに対して正しい結果を出力したとき,あなたのプログラムは受け入れられるであろう.\n

\n\n\n\n

Input

\n\n\n\n

\n入力は複数のデータセットで構成される.各データセットは 1 行のみからなり,1 つの正の整数 e (e ≦ 1,000,000) が含まれる.これは,宇宙ヤシガニが超空間に突入した際のエネルギーを表す.入力は e = 0 の時に終了し,これはデータセットには含まれない.\n

\n\n\n\n

Output

\n\n\n

\n各データセットについて,m の値を 1 行に出力しなさい.出力には他の文字を含めてはならない.\n

\n\n\n\n

Sample Input

\n\n
\n1\n2\n4\n27\n300\n1250\n0\n
\n\n\n

Output for the Sample Input

\n\n
\n1\n2\n2\n3\n18\n44\n
", "c_code": "int solution(void) {\n int e;\n int x = 0;\n int y = 0;\n int z = 0;\n int m = 1000000;\n\n while (1) {\n m = 1000000;\n scanf(\"%d\", &e);\n if (e == 0) {\n break;\n }\n for (z = 0; z < 101; z++) {\n for (y = 0; y < 1001; y++) {\n x = e - z * z * z - y * y;\n if (x < 0) {\n continue;\n }\n if (m > (x + y + z)) {\n m = x + y + z;\n }\n }\n }\n printf(\"%d\\n\", m);\n }\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let mut iter = s.split_whitespace();\n let e: i64 = iter.next().unwrap().parse().unwrap();\n\n if e == 0 {\n break;\n }\n\n let mut min_ = 1e8 as i64;\n let _flag: bool = false;\n for z in 0..101_i64 {\n for y in 0..1001_i64 {\n let x = e - z.pow(3) - y.pow(2);\n\n if x < 0 {\n break;\n }\n if min_ > x + y + z {\n min_ = x + y + z;\n }\n }\n }\n println!(\"{}\", min_);\n }\n}", "difficulty": "medium"} {"problem_id": "0599", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).\nYou are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq W,H \\leq 10^9
  • \n
  • 0\\leq x\\leq W
  • \n
  • 0\\leq y\\leq H
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
W H x y\n
\n
\n
\n
\n
\n

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

\n

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 1 2\n
\n
\n
\n
\n
\n

Sample Output 1

3.000000 0\n
\n

The line x=1 gives the optimal cut, and no other line does.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2 1 1\n
\n
\n
\n
\n
\n

Sample Output 2

2.000000 1\n
\n
\n
", "c_code": "int solution(void) {\n\n double w = 0;\n double h = 0;\n double x = 0;\n double y = 0;\n double max;\n\n scanf(\"%lf %lf %lf %lf\", &w, &h, &x, &y);\n\n max = (w * h) / 2;\n\n if (w == x * 2 && h == y * 2) {\n printf(\"%lf %d\", max, 1);\n } else {\n printf(\"%lf %d\", max, 0);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let (w, h, x, y) = {\n let mut inputline = String::new();\n std::io::stdin().read_line(&mut inputline).ok();\n let mut inputline = inputline.split_whitespace();\n (\n inputline.next().unwrap().parse::().ok().unwrap(),\n inputline.next().unwrap().parse::().ok().unwrap(),\n inputline.next().unwrap().parse::().ok().unwrap(),\n inputline.next().unwrap().parse::().ok().unwrap(),\n )\n };\n let s = (w * h) as f64 / 2.0;\n let fukusuu = if x * 2 == w && y * 2 == h { 1 } else { 0 };\n println!(\"{} {}\", s, fukusuu);\n}", "difficulty": "easy"} {"problem_id": "0600", "problem_description": "Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily.She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.", "c_code": "int solution(void) {\n int n = 0;\n int m = 0;\n\n scanf(\"%d %d\", &n, &m);\n for (int i = 1; i <= n; i++) {\n printf(\"%d\", i % 2);\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).expect(\"err\");\n let n: i32 = buffer.split_whitespace().collect::>()[0]\n .parse()\n .unwrap();\n for i in 1..n {\n print!(\"{}\", {\n if i % 2 == 0 {\n '1'\n } else {\n '0'\n }\n });\n }\n println!(\"{}\", {\n if n % 2 == 0 {\n '1'\n } else {\n '0'\n }\n });\n}", "difficulty": "medium"} {"problem_id": "0601", "problem_description": "Theofanis has a string $$$s_1 s_2 \\dots s_n$$$ and a character $$$c$$$. He wants to make all characters of the string equal to $$$c$$$ using the minimum number of operations.In one operation he can choose a number $$$x$$$ ($$$1 \\le x \\le n$$$) and for every position $$$i$$$, where $$$i$$$ is not divisible by $$$x$$$, replace $$$s_i$$$ with $$$c$$$. Find the minimum number of operations required to make all the characters equal to $$$c$$$ and the $$$x$$$-s that he should use in his operations.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n char c;\n char x;\n scanf(\"%d %c \", &n, &c);\n int last = 0;\n int count = 0;\n int flag = 1;\n while ((x = getchar()) != '\\n' && x != EOF) {\n count++;\n if (x == c) {\n last = count;\n } else {\n flag = 0;\n }\n }\n\n if (flag) {\n printf(\"0\\n\");\n } else if (2 * last > n) {\n printf(\"1\\n\");\n printf(\"%d\\n\", last);\n } else {\n printf(\"2\\n\");\n printf(\"%d %d\\n\", n, n - 1);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut s = String::new();\n io::stdin().read_to_string(&mut s)?;\n let mut it = s.split_whitespace().map(|s| s.to_string());\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut out = String::new();\n for _ in 1..=t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let c: char = it.next().unwrap().parse().unwrap();\n let s: String = it.next().unwrap().parse().unwrap();\n let mut moves = vec![];\n if s.chars().any(|x| x != c) {\n let mut ok = false;\n for i in (1..n).rev() {\n let _v: Vec = (i..n).step_by(i + 1).collect();\n\n if (i..n).step_by(i + 1).all(|j| s.as_bytes()[j] == c as u8) {\n moves.push(i + 1);\n ok = true;\n break;\n }\n }\n if !ok {\n moves.push(n - 1);\n moves.push(n);\n }\n }\n let ans = format!(\"{}\\n\", moves.len());\n out.push_str(ans.as_str());\n for i in 0..moves.len() {\n if i + 1 == moves.len() {\n let ans = format!(\"{}\\n\", moves[i]);\n out.push_str(ans.as_str());\n } else {\n let ans = format!(\"{} \", moves[i]);\n out.push_str(ans.as_str());\n }\n }\n }\n print!(\"{}\", out);\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0602", "problem_description": "Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\\texttt{A}$$$ except for the last character which is $$$\\texttt{B}$$$. The good strings are $$$\\texttt{AB},\\texttt{AAB},\\texttt{AAAB},\\ldots$$$. Note that $$$\\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n char string[200000];\n scanf(\"%s\", string);\n int bcount = 0;\n int acount = 0;\n int count = 0;\n int truthVal = 1;\n for (int j = 0; string[j] != '\\0'; j++) {\n if (string[j] == 'A') {\n acount++;\n } else if (string[j] == 'B') {\n bcount++;\n }\n count = j;\n if (acount < bcount) {\n truthVal = 0;\n break;\n }\n }\n if (truthVal == 1 && string[count] != 'A') {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n io::stdin().read_line(&mut str).unwrap();\n\n let t: usize = str.trim().parse::().unwrap();\n for _t in 0..t {\n str.clear();\n io::stdin().read_line(&mut str).unwrap();\n\n if !str.trim().ends_with(\"B\") {\n println!(\"NO\");\n continue;\n }\n\n let mut cnt = 0;\n let mut works = true;\n for char in str.trim().chars() {\n if char == 'B' {\n cnt -= 1;\n if cnt < 0 {\n works = false;\n break;\n }\n } else {\n cnt += 1;\n }\n }\n if works {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0603", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

There are N cats.\nWe number them from 1 through N.

\n

Each of the cats wears a hat.\nCat i says: \"there are exactly a_i different colors among the N - 1 hats worn by the cats except me.\"

\n

Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ N ≤ 10^5
  • \n
  • 1 ≤ a_i ≤ N-1
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print Yes if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

For example, if cat 1, 2 and 3 wears red, blue and blue hats, respectively, it is consistent with the remarks of the cats.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 1 2\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

From the remark of cat 1, we can see that cat 2 and 3 wear hats of the same color.\nAlso, from the remark of cat 2, we can see that cat 1 and 3 wear hats of the same color.\nTherefore, cat 1 and 2 wear hats of the same color, which contradicts the remark of cat 3.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n4 3 4 3 4\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
\n
\n
\n
\n

Sample Input 4

3\n2 2 2\n
\n
\n
\n
\n
\n

Sample Output 4

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 5

4\n2 2 2 2\n
\n
\n
\n
\n
\n

Sample Output 5

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 6

5\n3 3 3 3 3\n
\n
\n
\n
\n
\n

Sample Output 6

No\n
\n
\n
", "c_code": "int solution() {\n int max = 0;\n int min = 1000000;\n int maxnum = 0;\n int minnum = 0;\n int n;\n int s[100005];\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &s[i]);\n if (max < s[i]) {\n max = s[i];\n }\n if (min > s[i]) {\n min = s[i];\n }\n }\n if (max - min >= 2) {\n printf(\"No\");\n return 0;\n }\n\n for (int i = 0; i < n; i++) {\n if (max == s[i]) {\n maxnum++;\n }\n if (min == s[i]) {\n minnum++;\n }\n }\n if (max == min && (n - maxnum + 1 <= max && max <= n - maxnum + maxnum / 2 ||\n max + 1 == n)) {\n printf(\"Yes\");\n return 0;\n }\n if (max == min) {\n printf(\"No\");\n return 0;\n }\n\n if (n - maxnum + 1 <= max && max <= n - maxnum + maxnum / 2) {\n printf(\"Yes\");\n return 0;\n }\n printf(\"No\");\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n mut v: [usize; n]\n }\n v.sort();\n if v[n - 1] - v[0] > 1 {\n println!(\"No\");\n } else if v[0] == v[n - 1] {\n if v[0] <= n / 2 || v[0] == n - 1 {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n } else {\n let mut a = 0;\n for &x in &v {\n if x == v[0] {\n a += 1;\n } else {\n break;\n }\n }\n if a <= v[0] && v[0] < (n - a) / 2 + a {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0604", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Tak has N cards. On the i-th (1 \\leq i \\leq N) card is written an integer x_i.\nHe is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A.\nIn how many ways can he make his selection?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 50
  • \n
  • 1 \\leq A \\leq 50
  • \n
  • 1 \\leq x_i \\leq 50
  • \n
  • N,\\,A,\\,x_i are integers.
  • \n
\n
\n
\n
\n
\n

Partial Score

    \n
  • 200 points will be awarded for passing the test set satisfying 1 \\leq N \\leq 16.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N A\nx_1 x_2 ... x_N\n
\n
\n
\n
\n
\n

Output

Print the number of ways to select cards such that the average of the written integers is exactly A.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 8\n7 9 8 9\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n
    \n
  • The following are the 5 ways to select cards such that the average is 8:
      \n
    • Select the 3-rd card.
    • \n
    • Select the 1-st and 2-nd cards.
    • \n
    • Select the 1-st and 4-th cards.
    • \n
    • Select the 1-st, 2-nd and 3-rd cards.
    • \n
    • Select the 1-st, 3-rd and 4-th cards.
    • \n
    \n
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 8\n6 6 9\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8 5\n3 6 2 8 7 6 5 9\n
\n
\n
\n
\n
\n

Sample Output 3

19\n
\n
\n
\n
\n
\n
\n

Sample Input 4

33 3\n3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3\n
\n
\n
\n
\n
\n

Sample Output 4

8589934591\n
\n
    \n
  • The answer may not fit into a 32-bit integer.
  • \n
\n
\n
", "c_code": "int solution(void) {\n\n long long N;\n long long A;\n\n scanf(\"%lld%lld\", &N, &A);\n\n long long x[N + 1];\n\n long long sum = 0;\n\n for (long long i = 1; i <= N; i++) {\n scanf(\"%lld\", &x[i]);\n sum += x[i];\n }\n\n long long dp[N + 1][N + 1][sum + 1];\n\n for (long long j = 0; j <= N; j++) {\n for (long long k = 0; k <= N; k++) {\n for (long long s = 0; s <= sum; s++) {\n if (j >= 1 && s < x[j]) {\n dp[j][k][s] = dp[j - 1][k][s];\n } else if (j >= 1 && k >= 1 && s >= x[j]) {\n dp[j][k][s] = dp[j - 1][k][s] + dp[j - 1][k - 1][s - x[j]];\n } else if (j == 0 && k == 0 && s == 0) {\n dp[j][k][s] = 1;\n } else {\n dp[j][k][s] = 0;\n }\n }\n }\n }\n\n long long ans = 0;\n\n long long max;\n\n if (sum / A < N) {\n max = sum / A;\n } else {\n max = N;\n }\n\n for (long long k = 1; k <= max; k++) {\n ans += dp[N][k][k * A];\n }\n\n printf(\"%lld\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N: usize,\n A: usize,\n XX: [usize; N],\n }\n\n let mut X = vec![0; 1];\n X.extend(XX);\n\n let mut dp = vec![vec![vec![0; 2501]; N + 1]; N + 1];\n dp[0][0][0] = 1;\n\n for n in 1..N + 1 {\n for k in 0..n + 1 {\n for s in 0..50 * k + 1 {\n if s < X[n] {\n dp[n][k][s] = dp[n - 1][k][s];\n } else if s >= X[n] && k >= 1 {\n dp[n][k][s] = dp[n - 1][k][s] + dp[n - 1][k - 1][s - X[n]];\n }\n }\n }\n }\n\n let mut Ans = 0_u64;\n for i in 1..N + 1 {\n Ans += dp[N][i][A * i];\n }\n println!(\"{}\", Ans);\n}", "difficulty": "medium"} {"problem_id": "0605", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.\nThere are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

\n

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.\nNote that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

\n

How many good observatories are there?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq M \\leq 10^5
  • \n
  • 1 \\leq H_i \\leq 10^9
  • \n
  • 1 \\leq A_i,B_i \\leq N
  • \n
  • A_i \\neq B_i
  • \n
  • Multiple roads may connect the same pair of observatories.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nH_1 H_2 ... H_N\nA_1 B_1\nA_2 B_2\n:\nA_M B_M\n
\n
\n
\n
\n
\n

Output

Print the number of good observatories.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3\n1 2 3 4\n1 3\n2 3\n2 4\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n
    \n
  • \n

    From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

    \n
  • \n
  • \n

    From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

    \n
  • \n
  • \n

    From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

    \n
  • \n
  • \n

    From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

    \n
  • \n
\n

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

\n
\n
\n
\n
\n
\n

Sample Input 2

6 5\n8 6 9 1 2 1\n1 3\n4 2\n4 3\n4 6\n4 6\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
", "c_code": "int solution() {\n\n int N = 0;\n int M = 0;\n int A = 0;\n int B = 0;\n int H[100000] = {0};\n bool great[100000] = {false};\n int count = 0;\n scanf(\"%d %d\", &N, &M);\n for (int i = 0; i < N; i++) {\n great[i] = true;\n }\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &H[i]);\n }\n for (int i = 0; i < M; i++) {\n scanf(\"%d %d\", &A, &B);\n if (H[A - 1] <= H[B - 1]) {\n great[A - 1] = false;\n }\n if (H[A - 1] >= H[B - 1]) {\n great[B - 1] = false;\n }\n }\n for (int i = 0; i < N; i++) {\n if (great[i]) {\n count++;\n }\n }\n printf(\"%d\\n\", count);\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let mut buffer = String::new();\n io::stdin().read_to_string(&mut buffer)?;\n let mut input = buffer.lines();\n input.next();\n let mut input = input.map(|s| s.split_whitespace());\n let mut elevations: Vec<(u32, bool)> = input\n .next()\n .unwrap()\n .map(|n| (n.parse().unwrap(), true))\n .collect();\n let input = input.map(|l| l.map(|n| n.parse::().unwrap() - 1));\n for mut pair in input {\n let left = pair.next().unwrap();\n let right = pair.next().unwrap();\n match elevations[left].0.cmp(&elevations[right].0) {\n Less => elevations[left].1 = false,\n Equal => {\n elevations[left].1 = false;\n elevations[right].1 = false;\n }\n Greater => elevations[right].1 = false,\n }\n }\n let output = elevations.into_iter().filter(|&(_, good)| good).count();\n println!(\"{}\", output);\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "0606", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq K \\leq 10^5
  • \n
  • K is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K\n
\n
\n
\n
\n
\n

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

12=6×2 yields the smallest sum.

\n
\n
\n
\n
\n
\n

Sample Input 2

41\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n

11111=41×271 yields the smallest sum.

\n
\n
\n
\n
\n
\n

Sample Input 3

79992\n
\n
\n
\n
\n
\n

Sample Output 3

36\n
\n
\n
", "c_code": "int solution() {\n int n;\n int m = 100000;\n int q[100010];\n int r;\n int t;\n int s;\n int d[100010];\n scanf(\"%d\", &n);\n q[t = 0] = 1;\n for (r = 0; r < n; r++) {\n d[r] = 1e9;\n }\n for (r = d[1] = 1; r - t; t++) {\n s = q[t];\n if (d[(s * 10 % n)] > d[s]) {\n d[(s * 10) % n] = d[s];\n q[t] = (s * 10) % n;\n t--;\n }\n if (d[(s + 1) % n] > d[s]) {\n d[(s + 1) % n] = d[s] + 1;\n q[r++] = (s + 1) % n;\n }\n }\n printf(\"%d\\n\", d[0]);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n k: usize\n }\n let mut q = VecDeque::new();\n q.push_back(1);\n let mut d = vec![k + 1; k];\n d[1] = 1;\n while let Some(v) = q.pop_front() {\n if v == 0 {\n println!(\"{}\", d[v]);\n return;\n }\n if d[(v + 1) % k] > d[v] + 1 {\n d[(v + 1) % k] = d[v] + 1;\n q.push_back((v + 1) % k);\n }\n if d[v * 10 % k] > d[v] {\n d[v * 10 % k] = d[v];\n q.push_front(v * 10 % k);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0607", "problem_description": "Polycarp was presented with some sequence of integers $$$a$$$ of length $$$n$$$ ($$$1 \\le a_i \\le n$$$). A sequence can make Polycarp happy only if it consists of different numbers (i.e. distinct numbers).In order to make his sequence like this, Polycarp is going to make some (possibly zero) number of moves.In one move, he can: remove the first (leftmost) element of the sequence. For example, in one move, the sequence $$$[3, 1, 4, 3]$$$ will produce the sequence $$$[1, 4, 3]$$$, which consists of different numbers.Determine the minimum number of moves he needs to make so that in the remaining sequence all elements are different. In other words, find the length of the smallest prefix of the given sequence $$$a$$$, after removing which all values in the sequence will be unique.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n for (int i = 0; i < T; i++) {\n int N;\n scanf(\"%d\", &N);\n int arr[N];\n\n int freq[2 * 100000];\n int index = 0;\n int count = 0;\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &arr[i]);\n freq[arr[i]] = 0;\n }\n\n for (int i = N - 1; i >= 0; i--) {\n freq[arr[i]]++;\n if (freq[arr[i]] > 1) {\n index = i;\n count++;\n break;\n }\n }\n if (index == 0 && count == 0) {\n index = -1;\n }\n if (index == 0 && count != 0) {\n index = 0;\n }\n printf(\"%d\\n\", index + 1);\n }\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let stdin = std::io::stdin();\n let mut stdin = stdin.lock();\n\n let stdout = std::io::stdout();\n let mut stdout = stdout.lock();\n\n let mut line = String::with_capacity(32);\n let mut data = vec![];\n let mut data_uniq = HashSet::new();\n\n stdin.read_line(&mut line)?;\n let test_cases = line.trim().parse::()?;\n\n for _ in 0..test_cases {\n line.clear();\n let _len = stdin.read_line(&mut line)?;\n\n line.clear();\n stdin.read_line(&mut line)?;\n\n data.clear();\n data_uniq.clear();\n\n for x in line.trim().split_ascii_whitespace() {\n let value = x.parse::()?;\n data.push(value);\n }\n\n let mut answer = 0;\n for x in data.iter().copied().rev() {\n if !data_uniq.insert(x) {\n answer = data.len() - data_uniq.len();\n break;\n }\n }\n\n writeln!(stdout, \"{}\", answer)?;\n }\n\n stdout.flush()?;\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "0608", "problem_description": "Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers $$$a$$$ and $$$b$$$ using the following algorithm: If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length. The numbers are processed from right to left (that is, from the least significant digits to the most significant). In the first step, she adds the last digit of $$$a$$$ to the last digit of $$$b$$$ and writes their sum in the answer. At each next step, she performs the same operation on each pair of digits in the same place and writes the result to the left side of the answer. For example, the numbers $$$a = 17236$$$ and $$$b = 3465$$$ Tanya adds up as follows:$$$$$$ \\large{ \\begin{array}{r} + \\begin{array}{r} 17236\\\\ 03465\\\\ \\end{array} \\\\ \\hline \\begin{array}{r} 1106911 \\end{array} \\end{array}} $$$$$$ calculates the sum of $$$6 + 5 = 11$$$ and writes $$$11$$$ in the answer. calculates the sum of $$$3 + 6 = 9$$$ and writes the result to the left side of the answer to get $$$911$$$. calculates the sum of $$$2 + 4 = 6$$$ and writes the result to the left side of the answer to get $$$6911$$$. calculates the sum of $$$7 + 3 = 10$$$, and writes the result to the left side of the answer to get $$$106911$$$. calculates the sum of $$$1 + 0 = 1$$$ and writes the result to the left side of the answer and get $$$1106911$$$. As a result, she gets $$$1106911$$$.You are given two positive integers $$$a$$$ and $$$s$$$. Find the number $$$b$$$ such that by adding $$$a$$$ and $$$b$$$ as described above, Tanya will get $$$s$$$. Or determine that no suitable $$$b$$$ exists.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long a;\n long long s;\n scanf(\"%lld%lld\", &a, &s);\n if (a >= s) {\n printf(\"-1\\n\");\n continue;\n }\n long long x = s % 10;\n long long y = a % 10;\n long long b = 0;\n long long c = 1;\n while (s > 0) {\n if (x < y) {\n x = s % 100;\n b = (x - y) * c + b;\n if (x - y >= 10 || x - y < 0) {\n break;\n }\n s /= 100;\n a /= 10;\n c *= 10;\n x = s % 10;\n y = a % 10;\n\n } else {\n b = (x - y) * c + b;\n if (x - y >= 10 || x - y < 0) {\n break;\n }\n s /= 10;\n a /= 10;\n c *= 10;\n x = s % 10;\n y = a % 10;\n }\n }\n if (a != 0) {\n printf(\"-1\\n\");\n } else {\n printf(\"%lld\\n\", b);\n }\n }\n}", "rust_code": "fn solution() {\n let stdout = stdout();\n let mut bw = BufWriter::new(stdout);\n\n let stdin = stdin();\n let mut br = BufReader::new(stdin);\n let mut num_tests_str = String::new();\n br.read_line(&mut num_tests_str).unwrap();\n let num_tests: usize = num_tests_str.trim_end().parse::().unwrap();\n\n for _ in 0..num_tests {\n let mut test_case = String::new();\n br.read_line(&mut test_case).unwrap();\n let (a_str, s_str) = test_case.trim().split_once(' ').unwrap();\n let mut a: i64 = a_str.parse().unwrap();\n let mut s: i64 = s_str.parse().unwrap();\n let mut b: i64 = 0;\n let mut pow_10: i64 = 1;\n\n while a > 0 || s > 0 {\n let a_digit = a % 10;\n let mut s_digit = s % 10;\n let mut b_digit = s_digit - a_digit;\n if b_digit >= 0 {\n s /= 10;\n } else {\n s_digit = s % 100;\n b_digit = s_digit - a_digit;\n if !(0..=9).contains(&b_digit) {\n b = -1;\n break;\n }\n s /= 100;\n }\n a /= 10;\n b += b_digit * pow_10;\n pow_10 *= 10;\n }\n\n writeln!(bw, \"{}\", b).expect(\"Could not write to stdout\");\n }\n}", "difficulty": "easy"} {"problem_id": "0609", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There is an N-car train.

\n

You are given an integer i. Find the value of j such that the following statement is true: \"the i-th car from the front of the train is the j-th car from the back.\"

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq i \\leq N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N i\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The second car from the front of a 4-car train is the third car from the back.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

15 11\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int i = 0;\n scanf(\"%d%d\", &n, &i);\n printf(\"%d\", n - i + 1);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let s = s.trim_end().to_owned();\n let vec: Vec<&str> = s.split_whitespace().collect();\n let n: i32 = vec[0].parse().unwrap_or(0);\n let i: i32 = vec[1].parse().unwrap_or(0);\n println!(\"{}\", format!(\"{}\", n - i + 1));\n}", "difficulty": "medium"} {"problem_id": "0610", "problem_description": "You are given a tree consisting of $$$n$$$ nodes. You want to write some labels on the tree's edges such that the following conditions hold: Every label is an integer between $$$0$$$ and $$$n-2$$$ inclusive. All the written labels are distinct. The largest value among $$$MEX(u,v)$$$ over all pairs of nodes $$$(u,v)$$$ is as small as possible. Here, $$$MEX(u,v)$$$ denotes the smallest non-negative integer that isn't written on any edge on the unique simple path from node $$$u$$$ to node $$$v$$$.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int i;\n int u[100005];\n int v[100005];\n for (i = 0; i < n - 1; i++) {\n scanf(\"%d %d\", &u[i], &v[i]);\n }\n int d[100005];\n for (i = 0; i < n; i++) {\n d[i] = 0;\n }\n for (i = 0; i < n - 1; i++) {\n d[u[i] - 1]++;\n d[v[i] - 1]++;\n }\n int c = 0;\n for (i = 0; i < n; i++) {\n if (d[i] == 1) {\n c++;\n }\n }\n int ans[100005];\n if (c > 2) {\n for (i = 0; i < n - 1; i++) {\n ans[i] = -1;\n }\n c = 0;\n for (i = 0; i < n - 1; i++) {\n if (d[u[i] - 1] == 1 || d[v[i] - 1] == 1) {\n ans[i] = c;\n c++;\n }\n }\n for (i = 0; i < n - 1; i++) {\n if (ans[i] < 0) {\n ans[i] = c;\n c++;\n }\n }\n } else {\n for (i = 0; i < n - 1; i++) {\n ans[i] = i;\n }\n }\n for (i = 0; i < n - 1; i++) {\n printf(\"%d\\n\", ans[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines().map(|l| l.unwrap());\n let n: u64 = lines.next().unwrap().parse().unwrap();\n let mut edges = Vec::new();\n for _ in 0..n - 1 {\n let line = lines.next().unwrap();\n let mut parts = line.split_whitespace();\n let u: u64 = parts.next().unwrap().parse().unwrap();\n let v: u64 = parts.next().unwrap().parse().unwrap();\n edges.push((u - 1, v - 1));\n }\n\n let mut fixed_edges = edges.clone();\n for (u, v) in edges.iter() {\n fixed_edges.push((*v, *u));\n }\n\n fixed_edges.sort();\n fixed_edges.dedup();\n\n let mut adj = Vec::new();\n for _ in 0..n {\n adj.push(Vec::::new());\n }\n for (u, v) in fixed_edges {\n adj[u as usize].push(v);\n }\n\n if adj.iter().all(|l| l.len() <= 2) {\n for i in 0..n - 1 {\n println!(\"{}\", i);\n }\n } else {\n let (i_center, adj_center) = adj.iter().enumerate().find(|(_, l)| l.len() >= 3).unwrap();\n\n let e_0 = (i_center as u64, adj_center[0]);\n let e_1 = (i_center as u64, adj_center[1]);\n let e_2 = (i_center as u64, adj_center[2]);\n\n let mut counter = 3;\n for edge in edges.iter() {\n match edge {\n t if (t.0, t.1) == e_0 || (t.1, t.0) == e_0 => {\n println!(\"0\");\n }\n t if (t.0, t.1) == e_1 || (t.1, t.0) == e_1 => {\n println!(\"1\");\n }\n t if (t.0, t.1) == e_2 || (t.1, t.0) == e_2 => {\n println!(\"2\");\n }\n _ => {\n println!(\"{}\", counter);\n counter += 1;\n }\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0611", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

Takahashi bought a piece of apple pie at ABC Confiserie. According to his memory, he paid N yen (the currency of Japan) for it.

\n

The consumption tax rate for foods in this shop is 8 percent. That is, to buy an apple pie priced at X yen before tax, you have to pay X \\times 1.08 yen (rounded down to the nearest integer).

\n

Takahashi forgot the price of his apple pie before tax, X, and wants to know it again. Write a program that takes N as input and finds X. We assume X is an integer.

\n

If there are multiple possible values for X, find any one of them. Also, Takahashi's memory of N, the amount he paid, may be incorrect. If no value could be X, report that fact.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 50000
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

\n

If there are values that could be X, the price of the apple pie before tax, print any one of them.
\nIf there are multiple such values, printing any one of them will be accepted.
\nIf no value could be X, print :(.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

432\n
\n
\n
\n
\n
\n

Sample Output 1

400\n
\n

If the apple pie is priced at 400 yen before tax, you have to pay 400 \\times 1.08 = 432 yen to buy one.
\nOtherwise, the amount you have to pay will not be 432 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

1079\n
\n
\n
\n
\n
\n

Sample Output 2

:(\n
\n

There is no possible price before tax for which you have to pay 1079 yen with tax.

\n
\n
\n
\n
\n
\n

Sample Input 3

1001\n
\n
\n
\n
\n
\n

Sample Output 3

927\n
\n

If the apple pie is priced 927 yen before tax, by rounding down 927 \\times 1.08 = 1001.16, you have to pay 1001 yen.

\n
\n
", "c_code": "int solution(void) {\n\n int N = 0;\n int X = 0;\n\n scanf(\"%d\", &N);\n\n X = (N / 1.08);\n\n if ((int)(X * 1.08) == N) {\n printf(\"%d\\n\", X);\n } else if ((int)((X - 1) * 1.08) == N) {\n printf(\"%d\\n\", X - 1);\n } else if ((int)((X + 1) * 1.08) == N) {\n printf(\"%d\\n\", X + 1);\n } else {\n printf(\":(\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let n: u32 = s.trim().parse().ok().unwrap();\n\n for i in 1..n + 1 {\n if i * 108 / 100 == n {\n println!(\"{}\", i);\n return;\n }\n }\n println!(\":(\")\n}", "difficulty": "medium"} {"problem_id": "0612", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi loves numbers divisible by 2.

\n

You are given a positive integer N. Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique.

\n

Here, the number of times an integer can be divisible by 2, is how many times the integer can be divided by 2 without remainder.

\n

For example,

\n
    \n
  • 6 can be divided by 2 once: 6 -> 3.
  • \n
  • 8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1.
  • \n
  • 3 can be divided by 2 zero times.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

4 can be divided by 2 twice, which is the most number of times among 1, 2, ..., 7.

\n
\n
\n
\n
\n
\n

Sample Input 2

32\n
\n
\n
\n
\n
\n

Sample Output 2

32\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
\n
\n
\n
\n

Sample Input 4

100\n
\n
\n
\n
\n
\n

Sample Output 4

64\n
\n
\n
", "c_code": "int solution(void) {\n\n int N = 0;\n\n scanf(\"%d\", &N);\n\n if (N >= 64) {\n printf(\"%d\", 64);\n }\n if (N < 64 && N >= 32) {\n printf(\"%d\", 32);\n }\n if (N < 32 && N >= 16) {\n printf(\"%d\", 16);\n }\n if (N < 16 && N >= 8) {\n printf(\"%d\", 8);\n }\n if (N < 8 && N >= 4) {\n printf(\"%d\", 4);\n }\n if (N < 4 && N >= 2) {\n printf(\"%d\", 2);\n }\n if (N == 1) {\n printf(\"%d\", 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n let upper = buf.trim().parse::().unwrap();\n\n let mut pow2 = 1;\n if upper == 1 {\n println!(\"1\");\n } else {\n while pow2 <= upper {\n pow2 *= 2;\n }\n println!(\"{}\", pow2 / 2);\n }\n}", "difficulty": "hard"} {"problem_id": "0613", "problem_description": "

Celsius/Fahrenheit

\n \n

\n In Japan, temperature is usually expressed using the Celsius (℃) scale. In America, they used the Fahrenheit (℉) scale instead. $20$ degrees Celsius is roughly equal to $68$ degrees Fahrenheit. A phrase such as \"Today’s temperature is $68$ degrees\" is commonly encountered while you are in America.\n

\n\n

\nA value in Fahrenheit can be converted to Celsius by first subtracting $32$ and then multiplying by $\\frac{5}{9}$. A simplified method may be used to produce a rough estimate: first subtract $30$ and then divide by $2$. Using the latter method, $68$ Fahrenheit is converted to $19$ Centigrade, i.e., $\\frac{(68-30)}{2}$.\n

\n\n

\n Make a program to convert Fahrenheit to Celsius using the simplified method: $C = \\frac{F - 30}{2}$.\n

\n\n

Input

\n

\n The input is given in the following format.\n

\n\n
\n$F$\n
\n

\n The input line provides a temperature in Fahrenheit $F$ ($30 \\leq F \\leq 100$), which is an integer divisible by $2$.\n

\n\n

Output

\n

\n Output the converted Celsius temperature in a line.\n

\n\n

Sample Input 1

\n
\n68\n
\n\n

Sample Output 1

\n
\n19\n
\n\n

Sample Input 2

\n
\n50\n
\n

Sample Output 2

\n
\n10\n
", "c_code": "int solution() {\n\n int f = 0;\n int c = 0;\n\n scanf(\"%d\\n\", &f);\n\n if (((30 <= f) && (f <= 100)) && (f % 2 == 0)) {\n\n c = (f - 30) / 2;\n\n printf(\"%d\\n\", c);\n\n return 0;\n }\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let line = stdin.lock().lines().next().unwrap().unwrap();\n\n let f: i32 = line.parse().unwrap();\n\n let c = (f - 30) / 2;\n\n println!(\"{}\", c);\n}", "difficulty": "easy"} {"problem_id": "0614", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \\cdots, A_{N}.

\n

You will now successively do the following Q operations:

\n
    \n
  • In the i-th operation, you replace every element whose value is B_{i} with C_{i}.
  • \n
\n

For each i (1 \\leq i \\leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N, Q, A_{i}, B_{i}, C_{i} \\leq 10^{5}
  • \n
  • B_{i} \\neq C_{i}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_{1} A_{2} \\cdots A_{N}\nQ\nB_{1} C_{1}\nB_{2} C_{2}\n\\vdots\nB_{Q} C_{Q}\n
\n
\n
\n
\n
\n

Output

Print Q integers S_{i} to Standard Output in the following format:

\n
S_{1}\nS_{2}\n\\vdots\nS_{Q}\n
\n

Note that S_{i} may not fit into a 32-bit integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n1 2 3 4\n3\n1 2\n3 4\n2 4\n
\n
\n
\n
\n
\n

Sample Output 1

11\n12\n16\n
\n

Initially, the sequence A is 1,2,3,4.

\n

After each operation, it becomes the following:

\n
    \n
  • 2, 2, 3, 4
  • \n
  • 2, 2, 4, 4
  • \n
  • 4, 4, 4, 4
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4\n1 1 1 1\n3\n1 2\n2 1\n3 5\n
\n
\n
\n
\n
\n

Sample Output 2

8\n4\n4\n
\n

Note that the sequence A may not contain an element whose value is B_{i}.

\n
\n
\n
\n
\n
\n

Sample Input 3

2\n1 2\n3\n1 100\n2 100\n100 1000\n
\n
\n
\n
\n
\n

Sample Output 3

102\n200\n2000\n
\n
\n
", "c_code": "int solution() {\n\n int n = 0;\n\n int data[100002];\n int nannko[1000002];\n for (int i = 0; i < 1000001; i++) {\n nannko[i] = 0;\n }\n\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &data[i]);\n nannko[data[i]]++;\n }\n int q;\n\n scanf(\"%d\", &q);\n\n long long count = 0;\n int a;\n int move;\n long long disa = 0;\n for (int i = 0; i < 100001; i++) {\n count = count + (long long)nannko[i] * i;\n }\n for (int j = 0; j < q; j++) {\n scanf(\"%d %d\", &a, &move);\n disa = (long long)nannko[a] * (move - a);\n nannko[move] = nannko[move] + nannko[a];\n nannko[a] = 0;\n\n count = count + disa;\n printf(\"%lld\\n\", count);\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 n: usize = itr.next().unwrap().parse().unwrap();\n let a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n let q: usize = itr.next().unwrap().parse().unwrap();\n\n let mut cnt = vec![0; 100010];\n for i in 0..n {\n cnt[a[i]] += 1;\n }\n let mut sum = a.iter().sum::();\n\n let mut out = Vec::new();\n for _ in 0..q {\n let b: usize = itr.next().unwrap().parse().unwrap();\n let c: usize = itr.next().unwrap().parse().unwrap();\n\n let v1 = cnt[c] * c;\n let v2 = cnt[b] * b;\n sum -= v2 + v1;\n cnt[c] += cnt[b];\n cnt[b] = 0;\n sum += cnt[c] * c;\n writeln!(out, \"{}\", sum).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "medium"} {"problem_id": "0615", "problem_description": "You are given two integers $$$x$$$ and $$$y$$$. You can perform two types of operations: Pay $$$a$$$ dollars and increase or decrease any of these integers by $$$1$$$. For example, if $$$x = 0$$$ and $$$y = 7$$$ there are four possible outcomes after this operation: $$$x = 0$$$, $$$y = 6$$$; $$$x = 0$$$, $$$y = 8$$$; $$$x = -1$$$, $$$y = 7$$$; $$$x = 1$$$, $$$y = 7$$$. Pay $$$b$$$ dollars and increase or decrease both integers by $$$1$$$. For example, if $$$x = 0$$$ and $$$y = 7$$$ there are two possible outcomes after this operation: $$$x = -1$$$, $$$y = 6$$$; $$$x = 1$$$, $$$y = 8$$$. Your goal is to make both given integers equal zero simultaneously, i.e. $$$x = y = 0$$$. There are no other requirements. In particular, it is possible to move from $$$x=1$$$, $$$y=0$$$ to $$$x=y=0$$$.Calculate the minimum amount of dollars you have to spend on it.", "c_code": "int solution() {\n\n int n;\n scanf(\"%d\", &n);\n while (n--) {\n long long x;\n long long y;\n long long a;\n long long b;\n long long cost = 0;\n scanf(\"%lld %lld\", &x, &y);\n scanf(\"%lld %lld\", &a, &b);\n if (x > 0 && y > 0 && a * 2 >= b) {\n if (x > y) {\n cost += b * y;\n x -= y;\n y = 0;\n cost += a * x;\n } else {\n cost += b * x;\n y -= x;\n x = 0;\n cost += a * y;\n }\n } else {\n cost += a * x;\n cost += a * y;\n }\n printf(\"%lld\\n\", cost);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n io::stdin().read_line(&mut input_str).expect(\"err\");\n let t = input_str.trim().parse::().unwrap();\n for _ in 0..t {\n input_str.clear();\n io::stdin().read_line(&mut input_str).expect(\"err\");\n io::stdin().read_line(&mut input_str).expect(\"err\");\n\n let line = input_str.replace(\"\\n\", \" \");\n let list: Vec<&str> = line.split_whitespace().collect();\n let (x, y, a, b) = (\n list[0].parse::().unwrap(),\n list[1].parse::().unwrap(),\n list[2].parse::().unwrap(),\n list[3].parse::().unwrap(),\n );\n\n if b < 2 * a {\n let min_val = cmp::min(x, y);\n let max_val = cmp::max(x, y);\n println!(\"{}\", min_val * b + (max_val - min_val) * a);\n } else {\n println!(\"{}\", (x + y) * a);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0616", "problem_description": "You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible.We remind that in a sequence of numbers a1, a2, ..., ak the element ai is a record if for every integer j (1 ≤ j < i) the following holds: aj < ai.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n + 1];\n int i;\n int visit[n + 1];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n visit[i + 1] = 0;\n }\n int l = 0;\n int r = a[0];\n int max_value = a[0];\n int count;\n int max = -1;\n visit[a[0]] = -1;\n for (i = 1; i < n; i++) {\n\n if (l < a[i] && r > a[i]) {\n visit[r]++;\n l = a[i];\n } else if (a[i] > r) {\n\n l = r;\n r = a[i];\n visit[a[i]] = -1;\n } else if (a[i] < l) {\n visit[a[i]] = 0;\n }\n }\n max = -2;\n for (i = 1; i <= n; i++) {\n if (max < visit[i]) {\n max_value = i;\n max = visit[i];\n }\n }\n printf(\"%d\\n\", max_value);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::prelude::*;\n std::io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n let n = it.next().unwrap();\n\n if n == 1 {\n println!(\"1\");\n return;\n }\n\n let p: Vec<_> = it.collect();\n\n let m_2m: Vec<_> = p\n .iter()\n .cloned()\n .scan((0, 0), |state, p| {\n if p > state.0 {\n state.1 = state.0;\n state.0 = p;\n } else if p > state.1 {\n state.1 = p;\n }\n Some(*state)\n })\n .collect();\n\n let mut cnts = vec![0; n + 1];\n for i in 0..n - 1 {\n if m_2m[i].1 < p[i + 1] && p[i + 1] < m_2m[i].0 {\n cnts[m_2m[i].0] += 1;\n }\n }\n\n let m = cnts.iter().cloned().max().unwrap();\n let ans = if m > 1 {\n cnts.iter().cloned().position(|cnt| cnt == m).unwrap()\n } else {\n if let Some((idx, _)) = p\n .iter()\n .cloned()\n .skip(1)\n .zip(m_2m.iter().map(|x| x.0))\n .filter(|&(p, max)| p < max)\n .min()\n {\n idx\n } else {\n 1\n }\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0617", "problem_description": "\n\n\n

Circle in a Rectangle

\n\n

\n Write a program which reads a rectangle and a circle, and determines whether the circle is arranged inside the rectangle. As shown in the following figures, the upper right coordinate $(W, H)$ of the rectangle and the central coordinate $(x, y)$ and radius $r$ of the circle are given.\n

\n\n
\n
\n\"Circle\n
\n
\n
\n\n\n\n

Input

\n\n

\n Five integers $W$, $H$, $x$, $y$ and $r$ separated by a single space are given in a line.\n

\n\n

Output

\n\n

\n Print \"Yes\" if the circle is placed inside the rectangle, otherwise \"No\" in a line.\n

\n\n

Constraints

\n\n
    \n
  • $ -100 \\leq x, y \\leq 100$
  • \n
  • $ 0 < W, H, r \\leq 100$
  • \n
\n\n

Sample Input 1

\n
\n5 4 2 2 1\n
\n

Sample Output 1

\n
\nYes\n
\n\n\n

Sample Input 2

\n
\n5 4 2 4 1\n
\n

Sample Output 2

\n
\nNo\n
", "c_code": "int solution() {\n int W = 0;\n int H = 0;\n int x = 0;\n int y = 0;\n int r = 0;\n\n scanf(\"%d %d %d %d %d\", &W, &H, &x, &y, &r);\n\n if ((x >= r) && (W >= (x + r)) && (y >= r) && (H >= (y + r))) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n\n let mut line = String::new();\n\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n\n let W: i32 = vec[0].parse().unwrap_or(0);\n let H: i32 = vec[1].parse().unwrap_or(0);\n let x: i32 = vec[2].parse().unwrap_or(0);\n let y: i32 = vec[3].parse().unwrap_or(0);\n let r: i32 = vec[4].parse().unwrap_or(0);\n\n if x >= r && x <= (W - r) && y >= r && y <= (H - r) {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "0618", "problem_description": "There are $$$n$$$ dormitories in Berland State University, they are numbered with integers from $$$1$$$ to $$$n$$$. Each dormitory consists of rooms, there are $$$a_i$$$ rooms in $$$i$$$-th dormitory. The rooms in $$$i$$$-th dormitory are numbered from $$$1$$$ to $$$a_i$$$.A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all $$$n$$$ dormitories is written on an envelope. In this case, assume that all the rooms are numbered from $$$1$$$ to $$$a_1 + a_2 + \\dots + a_n$$$ and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.For example, in case $$$n=2$$$, $$$a_1=3$$$ and $$$a_2=5$$$ an envelope can have any integer from $$$1$$$ to $$$8$$$ written on it. If the number $$$7$$$ is written on an envelope, it means that the letter should be delivered to the room number $$$4$$$ of the second dormitory.For each of $$$m$$$ letters by the room number among all $$$n$$$ dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.", "c_code": "int solution() {\n long int n;\n long int m;\n long int j = 0;\n long long int roomnbr = 0;\n scanf(\"%ld%ld\", &n, &m);\n long long int rooms[n];\n long long int lettersnbr[m];\n for (long int i = 0; i < n; i++) {\n scanf(\"%lld\", &rooms[i]);\n }\n\n for (long int i = 0; i < m; i++) {\n scanf(\"%lld\", &lettersnbr[i]);\n }\n\n for (long int i = 0; i < n; i++) {\n roomnbr += rooms[i];\n while (j < m) {\n if (lettersnbr[j] <= roomnbr && i == 0) {\n printf(\"%ld %lld\\n\", i + 1, lettersnbr[j]);\n } else if (lettersnbr[j] <= roomnbr) {\n printf(\"%ld %lld\\n\", i + 1, lettersnbr[j] - (roomnbr - rooms[i]));\n } else if (lettersnbr[j] > roomnbr) {\n break;\n }\n j++;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut string = String::new();\n io::stdin().read_line(&mut string).unwrap();\n let mut iter = string.trim().split(\" \").map(|x| x.parse().unwrap());\n let (_n, _m): (usize, usize) = (iter.next().unwrap(), iter.next().unwrap());\n let mut string = String::new();\n io::stdin().read_line(&mut string).unwrap();\n let a: Vec = string\n .trim()\n .split(\" \")\n .map(|x| x.parse().unwrap())\n .collect();\n let mut string = String::new();\n io::stdin().read_line(&mut string).unwrap();\n let rooms: Vec = string\n .trim()\n .split(\" \")\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut room_sum = 0;\n let mut current_dorm: usize = 0;\n for room in rooms {\n let mut just_above = room - room_sum;\n if just_above <= a[current_dorm] {\n println!(\"{} {}\", current_dorm + 1, just_above);\n } else {\n while just_above > a[current_dorm] {\n room_sum += a[current_dorm];\n just_above = room - room_sum;\n current_dorm += 1;\n }\n println!(\"{} {}\", current_dorm + 1, just_above);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0619", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You have an integer variable x.\nInitially, x=0.

\n

Some person gave you a string S of length N, and using the string you performed the following operation N times.\nIn the i-th operation, you incremented the value of x by 1 if S_i=I, and decremented the value of x by 1 if S_i=D.

\n

Find the maximum value taken by x during the operations (including before the first operation, and after the last operation).

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤100
  • \n
  • |S|=N
  • \n
  • No characters except I and D occur in S.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the maximum value taken by x during the operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\nIIDID\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

After each operation, the value of x becomes 1, 2, 1, 2 and 1, respectively. Thus, the output should be 2, the maximum value.

\n
\n
\n
\n
\n
\n

Sample Input 2

7\nDDIDDII\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

The initial value x=0 is the maximum value taken by x, thus the output should be 0.

\n
\n
", "c_code": "int solution() {\n int n = 0;\n int a = 0;\n int b = 0;\n int max = 0;\n char s[101];\n scanf(\"%d %s\", &n, s);\n for (a = 0; a < n; a++) {\n if (s[a] == 'I') {\n b++;\n\n } else {\n b--;\n }\n\n if (max < b) {\n max = b;\n }\n }\n printf(\"%d\\n\", max);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).expect(\"error\");\n\n s = String::new();\n std::io::stdin().read_line(&mut s).expect(\"error\");\n let s = s.trim();\n\n let mut x = 0;\n let mut max = 0;\n for c in s.chars() {\n if c == 'I' {\n x += 1;\n if x > max {\n max = x;\n }\n } else {\n x -= 1;\n }\n }\n println!(\"{}\", max);\n}", "difficulty": "easy"} {"problem_id": "0620", "problem_description": "Interpolation search", "c_code": "int solution(const int arr[], int n, int key) {\n int low = 0;\n int high = n - 1;\n while (low <= high && key >= arr[low] && key <= arr[high]) {\n\n int pos =\n low + (((key - arr[low]) * (high - low)) / (arr[high] - arr[low]));\n if (key > arr[pos]) {\n low = pos + 1;\n } else if (key < arr[pos]) {\n high = pos - 1;\n } else {\n return pos;\n }\n }\n\n return -1;\n}\n", "rust_code": "fn solution(nums: &[i32], item: &i32) -> Result {\n if nums.is_empty() {\n return Err(0);\n }\n let mut low: usize = 0;\n let mut high: usize = nums.len() - 1;\n while low <= high {\n if *item < nums[low] || *item > nums[high] {\n break;\n }\n let offset: usize = low\n + (((high - low) / (nums[high] - nums[low]) as usize) * (*item - nums[low]) as usize);\n match nums[offset].cmp(item) {\n std::cmp::Ordering::Equal => return Ok(offset),\n std::cmp::Ordering::Less => low = offset + 1,\n std::cmp::Ordering::Greater => high = offset - 1,\n }\n }\n Err(0)\n}\n", "difficulty": "medium"} {"problem_id": "0621", "problem_description": "We will consider the numbers $$$a$$$ and $$$b$$$ as adjacent if they differ by exactly one, that is, $$$|a-b|=1$$$.We will consider cells of a square matrix $$$n \\times n$$$ as adjacent if they have a common side, that is, for cell $$$(r, c)$$$ cells $$$(r, c-1)$$$, $$$(r, c+1)$$$, $$$(r-1, c)$$$ and $$$(r+1, c)$$$ are adjacent to it.For a given number $$$n$$$, construct a square matrix $$$n \\times n$$$ such that: Each integer from $$$1$$$ to $$$n^2$$$ occurs in this matrix exactly once; If $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are adjacent cells, then the numbers written in them must not be adjacent.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n int a[n][n];\n int c = 1;\n if (n == 2) {\n printf(\"-1\\n\");\n } else {\n for (int i = 0; i < n; i++) {\n a[i][i] = c;\n c++;\n }\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < n - i; j++) {\n a[j + i][j] = c;\n c++;\n }\n for (int j = 0; j < n - i; j++) {\n a[j][j + i] = c;\n c++;\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n printf(\"%d \", a[i][j]);\n }\n printf(\"\\n\");\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(|x| x.unwrap());\n\n let t = lines.next().unwrap().parse::().unwrap();\n\n 'cases: for _ in 0..t {\n let n = lines.next().unwrap().parse::().unwrap();\n\n if n == 2 {\n writeln!(&mut output, \"-1\").unwrap();\n continue;\n }\n\n let mut mat = vec![0; n * n];\n\n let mut x = 1;\n\n let mut i = 0;\n while i < mat.len() {\n mat[i] = x;\n x += 1;\n i += 2;\n }\n\n let mut i = 1;\n while i < mat.len() {\n mat[i] = x;\n x += 1;\n i += 2;\n }\n\n let mut it = mat.into_iter();\n for _ in 0..n {\n for _ in 0..n {\n write!(&mut output, \"{} \", it.next().unwrap()).unwrap();\n }\n writeln!(&mut output).unwrap();\n }\n continue;\n\n let mut x = 1;\n let mut y = n * n;\n let mut c = 0;\n\n while x <= y {\n write!(&mut output, \"{} \", x).unwrap();\n c += 1;\n x += 1;\n if c == n {\n c = 0;\n writeln!(&mut output).unwrap()\n }\n\n write!(&mut output, \"{} \", x).unwrap();\n c += 1;\n y -= 1;\n if c == n {\n c = 0;\n writeln!(&mut output).unwrap()\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0622", "problem_description": "Let's define a multiplication operation between a string $$$a$$$ and a positive integer $$$x$$$: $$$a \\cdot x$$$ is the string that is a result of writing $$$x$$$ copies of $$$a$$$ one after another. For example, \"abc\" $$$\\cdot~2~=$$$ \"abcabc\", \"a\" $$$\\cdot~5~=$$$ \"aaaaa\".A string $$$a$$$ is divisible by another string $$$b$$$ if there exists an integer $$$x$$$ such that $$$b \\cdot x = a$$$. For example, \"abababab\" is divisible by \"ab\", but is not divisible by \"ababab\" or \"aa\".LCM of two strings $$$s$$$ and $$$t$$$ (defined as $$$LCM(s, t)$$$) is the shortest non-empty string that is divisible by both $$$s$$$ and $$$t$$$.You are given two strings $$$s$$$ and $$$t$$$. Find $$$LCM(s, t)$$$ or report that it does not exist. It can be shown that if $$$LCM(s, t)$$$ exists, it is unique.", "c_code": "int solution() {\n char message[30];\n char message1[30];\n char A[500];\n char B[500];\n int check;\n int a;\n int b;\n int c;\n int d;\n int k;\n int i;\n int j;\n int q;\n scanf(\"%d\", &q);\n for (i = 0; i < q; i++) {\n check = 1;\n scanf(\"%s\", message);\n scanf(\"%s\", message1);\n a = strlen(message);\n b = strlen(message1);\n c = strlen(message);\n d = strlen(message1);\n while (a != 0 && b != 0) {\n if (a >= b) {\n a = a % b;\n } else {\n b = b % a;\n }\n }\n if (a == 0) {\n k = c * d / b;\n } else {\n k = c * d / a;\n }\n for (j = 0; j < k; j++) {\n A[j] = message[j % c];\n B[j] = message1[j % d];\n if (A[j] != B[j]) {\n check = 0;\n break;\n }\n }\n if (check == 1) {\n if (d >= c) {\n for (j = 0; j < k / d; j++) {\n printf(\"%s\", message1);\n }\n printf(\"\\n\");\n } else {\n for (j = 0; j < k / c; j++) {\n printf(\"%s\", message);\n }\n printf(\"\\n\");\n }\n } else {\n printf(\"-1\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n\n let mut lines = input.lines();\n\n lines.next();\n\n while let Some(line) = lines.next() {\n let s = line;\n let t = lines.next().unwrap();\n\n for (i, (c0, c1)) in s.chars().cycle().zip(t.chars().cycle()).enumerate() {\n if i > 0 && i % s.len() == 0 && i % t.len() == 0 {\n for _ in 0..(i / s.len()) {\n print!(\"{}\", s);\n }\n println!();\n break;\n }\n\n if c0 != c1 {\n println!(\"-1\");\n break;\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0623", "problem_description": "Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment $$$0$$$ and turn power off at moment $$$M$$$. Moreover, the lamp allows you to set a program of switching its state (states are \"lights on\" and \"lights off\"). Unfortunately, some program is already installed into the lamp.The lamp allows only good programs. Good program can be represented as a non-empty array $$$a$$$, where $$$0 < a_1 < a_2 < \\dots < a_{|a|} < M$$$. All $$$a_i$$$ must be integers. Of course, preinstalled program is a good program.The lamp follows program $$$a$$$ in next manner: at moment $$$0$$$ turns power and light on. Then at moment $$$a_i$$$ the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment $$$1$$$ and then do nothing, the total time when the lamp is lit will be $$$1$$$. Finally, at moment $$$M$$$ the lamp is turning its power off regardless of its state.Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program $$$a$$$, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of $$$a$$$, or even at the begining or at the end of $$$a$$$.Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from $$$x$$$ till moment $$$y$$$, then its lit for $$$y - x$$$ units of time. Segments of time when the lamp is lit are summed up.", "c_code": "int solution() {\n int n;\n int M;\n int i;\n int array[100002];\n scanf(\"%d %d\", &n, &M);\n array[0] = 0;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &array[i + 1]);\n }\n array[n + 1] = M;\n int max = 0;\n int state = 0;\n for (i = 0; i < n + 2; i++) {\n if (state == 1) {\n max += array[i] - array[i - 1];\n state = 0;\n } else {\n state = 1;\n }\n }\n int up0 = 0;\n int up1 = max - (array[1] - array[0]);\n state = 1;\n for (i = 1; i < n + 2; i++) {\n int reverse = M - array[i] - up1;\n int change = up0 + array[i] - array[i - 1] - 1 + reverse;\n if (state == 1) {\n if (change > max) {\n max = change;\n }\n up0 += array[i] - array[i - 1];\n state = 0;\n } else {\n if (change > max) {\n max = change;\n }\n if (i < n + 1) {\n up1 -= array[i + 1] - array[i];\n }\n state = 1;\n }\n }\n printf(\"%d\\n\", max);\n return 0;\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 n = get!(usize);\n let m = get!();\n let mut xs = vec![];\n xs.push(0);\n for _ in 0..n {\n xs.push(get!());\n }\n xs.push(m);\n let mut ontime = 0;\n let mut offtime = 0;\n for i in 0..n + 1 {\n if i % 2 == 0 {\n ontime += xs[i + 1] - xs[i];\n } else {\n offtime += xs[i + 1] - xs[i];\n }\n }\n let mut ans = ontime;\n let mut tmp_ontime = 0;\n let mut tmp_offtime = 0;\n for i in 0..n + 1 {\n if i % 2 == 0 {\n let rem_offtime = offtime - tmp_offtime;\n let j = xs[i + 1] - 1;\n if j > xs[i] {\n ans = std::cmp::max(ans, tmp_ontime + (j - xs[i]) + rem_offtime);\n }\n tmp_ontime += xs[i + 1] - xs[i];\n } else {\n let rem_offtime = offtime - tmp_offtime - (xs[i + 1] - xs[i]);\n let j = xs[i] + 1;\n if j < xs[i + 1] {\n ans = std::cmp::max(ans, tmp_ontime + (xs[i + 1] - j) + rem_offtime);\n }\n tmp_offtime += xs[i + 1] - xs[i];\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0624", "problem_description": "Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees.There are $$$n$$$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $$$a_1, a_2, \\dots, a_n$$$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$1, 2, \\dots, i$$$ by $$$1$$$. Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$i, i + 1, \\dots, n$$$ by $$$1$$$. Increase the level of moisture of all trees by $$$1$$$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $$$0$$$.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n int N;\n scanf(\"%d\", &N);\n int arr[N + 1];\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", arr + i);\n }\n arr[N] = 0;\n long long a1 = arr[0];\n long long ans = 0;\n for (int i = 0; i < N - 1; i++) {\n long long d = arr[i + 1] - arr[i];\n if (d <= 0) {\n ans -= d, a1 += d;\n } else {\n ans += d;\n }\n }\n printf(\"%lld\\n\", ans + (a1 >= 0 ? a1 : -a1));\n }\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin().read_to_string(&mut content);\n\n let mut lines = content.lines();\n let num_tests: i32 = lines.next().unwrap().parse().unwrap();\n for _ in 0..num_tests {\n let _n: usize = lines.next().unwrap().parse().unwrap();\n let a: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse::().unwrap())\n .collect();\n let mut result = 0;\n let mut left = -a[0];\n for i in 0..a.len() - 1 {\n if a[i] < a[i + 1] {\n result += a[i + 1] - a[i];\n } else {\n result += a[i] - a[i + 1];\n left += a[i] - a[i + 1];\n }\n }\n result += left.abs();\n println!(\"{result}\");\n }\n}", "difficulty": "medium"} {"problem_id": "0625", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

It is only six months until Christmas, and AtCoDeer the reindeer is now planning his travel to deliver gifts.
\nThere are N houses along TopCoDeer street. The i-th house is located at coordinate a_i. He has decided to deliver gifts to all these houses.
\nFind the minimum distance to be traveled when AtCoDeer can start and end his travel at any positions.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 100
  • \n
  • 0 ≤ a_i ≤ 1000
  • \n
  • a_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the minimum distance to be traveled.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n2 3 7 9\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

The travel distance of 7 can be achieved by starting at coordinate 9 and traveling straight to coordinate 2.
\nIt is not possible to do with a travel distance of less than 7, and thus 7 is the minimum distance to be traveled.

\n
\n
\n
\n
\n
\n

Sample Input 2

8\n3 1 4 1 5 9 2 6\n
\n
\n
\n
\n
\n

Sample Output 2

8\n
\n

There may be more than one house at a position.

\n
\n
", "c_code": "int solution() {\n\n int N = 0;\n int POS[110];\n for (int i = 0; i < 110; i++) {\n POS[i] = 0;\n }\n\n int MAX = 0;\n int MIN = 10000;\n\n scanf(\"%d\", &N);\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &POS[i]);\n }\n\n for (int i = 0; i < N; i++) {\n\n if (POS[i] > MAX) {\n MAX = POS[i];\n }\n\n if (POS[i] < MIN) {\n MIN = POS[i];\n }\n }\n\n printf(\"%d\", MAX - MIN);\n\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n let mut input1 = String::new();\n let mut input2 = String::new();\n let _ = io::stdin().read_line(&mut input1);\n let _ = io::stdin().read_line(&mut input2);\n\n let inputs: Vec = input2\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let mut min = 1000;\n let mut max = 0;\n for val in &inputs {\n if min > *val {\n min = *val;\n }\n if max < *val {\n max = *val;\n }\n }\n println!(\"{}\", max - min);\n}", "difficulty": "medium"} {"problem_id": "0626", "problem_description": "Andrew was very excited to participate in Olympiad of Metropolises. Days flew by quickly, and Andrew is already at the airport, ready to go home. He has $$$n$$$ rubles left, and would like to exchange them to euro and dollar bills. Andrew can mix dollar bills and euro bills in whatever way he wants. The price of one dollar is $$$d$$$ rubles, and one euro costs $$$e$$$ rubles.Recall that there exist the following dollar bills: $$$1$$$, $$$2$$$, $$$5$$$, $$$10$$$, $$$20$$$, $$$50$$$, $$$100$$$, and the following euro bills — $$$5$$$, $$$10$$$, $$$20$$$, $$$50$$$, $$$100$$$, $$$200$$$ (note that, in this problem we do not consider the $$$500$$$ euro bill, it is hard to find such bills in the currency exchange points). Andrew can buy any combination of bills, and his goal is to minimize the total number of rubles he will have after the exchange.Help him — write a program that given integers $$$n$$$, $$$e$$$ and $$$d$$$, finds the minimum number of rubles Andrew can get after buying dollar and euro bills.", "c_code": "int solution() {\n int n;\n int d;\n int e;\n scanf(\"%d\", &n);\n scanf(\"%d\", &d);\n scanf(\"%d\", &e);\n int min = n;\n for (int i = 0; i <= (n - n % d) / d; i++) {\n if (min > n - d * i) {\n min = n - d * i;\n }\n if ((n - (d * i + ((n - d * i) - (n - d * i) % e))) >= 0 &&\n (n - (d * i + ((n - d * i) - (n - d * i) % e))) < min &&\n (((n - d * i) - (n - d * i) % e) / e) % 5 == 0) {\n min = (n - (d * i + ((n - d * i) - (n - d * i) % e)));\n }\n }\n printf(\"%d\", min);\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 io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let mut input_iter = input.split_whitespace();\n let n = input_iter.next().unwrap().parse::().unwrap();\n let d = input_iter.next().unwrap().parse::().unwrap();\n let e = 5 * input_iter.next().unwrap().parse::().unwrap();\n let min = (n - ((n / d) * d)..=n)\n .step_by(d as usize)\n .map(|i| i % e)\n .fold(n, |min, x| if x < min { x } else { min });\n println!(\"{}\", min);\n}", "difficulty": "easy"} {"problem_id": "0627", "problem_description": "A binary string is a string where each character is either 0 or 1. Two binary strings $$$a$$$ and $$$b$$$ of equal length are similar, if they have the same character in some position (there exists an integer $$$i$$$ such that $$$a_i = b_i$$$). For example: 10010 and 01111 are similar (they have the same character in position $$$4$$$); 10010 and 11111 are similar; 111 and 111 are similar; 0110 and 1001 are not similar. You are given an integer $$$n$$$ and a binary string $$$s$$$ consisting of $$$2n-1$$$ characters. Let's denote $$$s[l..r]$$$ as the contiguous substring of $$$s$$$ starting with $$$l$$$-th character and ending with $$$r$$$-th character (in other words, $$$s[l..r] = s_l s_{l + 1} s_{l + 2} \\dots s_r$$$).You have to construct a binary string $$$w$$$ of length $$$n$$$ which is similar to all of the following strings: $$$s[1..n]$$$, $$$s[2..n+1]$$$, $$$s[3..n+2]$$$, ..., $$$s[n..2n-1]$$$.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t-- > 0) {\n int n = 0;\n char a[100] = {0};\n scanf(\"%d\\n\", &n);\n n = (2 * n) - 1;\n int j = 0;\n for (int i = 1; i <= n; i++) {\n char c = 0;\n scanf(\"%c\", &c);\n if ((i % 2) != 0) {\n a[j] = c;\n j++;\n }\n }\n printf(\"%s\\n\", a);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut string = String::new();\n io::stdin()\n .read_line(&mut string)\n .expect(\"Failed to read string\");\n for _ in 0..string.trim().parse().unwrap() {\n string.clear();\n io::stdin()\n .read_line(&mut string)\n .expect(\"Failed to read string\");\n let n = string.trim().parse().unwrap();\n string.clear();\n io::stdin()\n .read_line(&mut string)\n .expect(\"Failed to read string\");\n let byte_string = string.trim();\n let mut ans = String::with_capacity(n);\n for (idx, substr) in byte_string.as_bytes().windows(n).enumerate() {\n ans.push(substr[idx] as char);\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "hard"} {"problem_id": "0628", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

This contest is CODE FESTIVAL.\nHowever, Mr. Takahashi always writes it CODEFESTIVAL, omitting the single space between CODE and FESTIVAL.

\n

So he has decided to make a program that puts the single space he omitted.

\n

You are given a string s with 12 letters.\nOutput the string putting a single space between the first 4 letters and last 8 letters in the string s.

\n
\n
\n
\n
\n

Constraints

    \n
  • s contains exactly 12 letters.
  • \n
  • All letters in s are uppercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Print the string putting a single space between the first 4 letters and last 8 letters in the string s.\nPut a line break at the end.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

CODEFESTIVAL\n
\n
\n
\n
\n
\n

Sample Output 1

CODE FESTIVAL\n
\n

Putting a single space between the first 4 letters and last 8 letters in CODEFESTIVAL makes it CODE FESTIVAL.

\n
\n
\n
\n
\n
\n

Sample Input 2

POSTGRADUATE\n
\n
\n
\n
\n
\n

Sample Output 2

POST GRADUATE\n
\n
\n
\n
\n
\n
\n

Sample Input 3

ABCDEFGHIJKL\n
\n
\n
\n
\n
\n

Sample Output 3

ABCD EFGHIJKL\n
\n
\n
", "c_code": "int solution(void) {\n char a[12];\n scanf(\"%c%c%c%c%c%c%c%c%c%c%c%c\\n\", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5],\n &a[6], &a[7], &a[8], &a[9], &a[10], &a[11]);\n printf(\"%c%c%c%c %c%c%c%c%c%c%c%c\\n\", a[0], a[1], a[2], a[3], a[4], a[5],\n a[6], a[7], a[8], a[9], a[10], a[11]);\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 buf_it = buf.split_whitespace();\n\n let S = buf_it.next().unwrap();\n\n println!(\"{} {}\", &S[..4], &S[4..]);\n}", "difficulty": "hard"} {"problem_id": "0629", "problem_description": "George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living in it and the room can accommodate qi people in total (pi ≤ qi). Your task is to count how many rooms has free place for both George and Alex.", "c_code": "int solution() {\n int n = 0;\n int i = 0;\n int retval = 0;\n int p[100];\n int q[100];\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d %d\", p + i, q + i);\n }\n for (i = 0; i < n; i++) {\n if (q[i] > p[i] + 1) {\n retval++;\n }\n }\n printf(\"%d\", retval);\n return 0;\n}", "rust_code": "fn solution() {\n let mut n_text = String::new();\n std::io::stdin().read_line(&mut n_text).expect(\"\");\n let n = n_text.trim().parse::().expect(\"\");\n let mut result = 0;\n for _ in 0..n {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).expect(\"\");\n let mut it = line.split_whitespace();\n let p = it.next().unwrap().parse::().expect(\"\");\n let q = it.next().unwrap().parse::().expect(\"\");\n if p + 2 <= q {\n result += 1\n }\n }\n println!(\"{}\", result);\n}", "difficulty": "hard"} {"problem_id": "0630", "problem_description": "You are given two integers $$$a$$$ and $$$b$$$. In one turn, you can do one of the following operations: Take an integer $$$c$$$ ($$$c > 1$$$ and $$$a$$$ should be divisible by $$$c$$$) and replace $$$a$$$ with $$$\\frac{a}{c}$$$; Take an integer $$$c$$$ ($$$c > 1$$$ and $$$b$$$ should be divisible by $$$c$$$) and replace $$$b$$$ with $$$\\frac{b}{c}$$$. Your goal is to make $$$a$$$ equal to $$$b$$$ using exactly $$$k$$$ turns.For example, the numbers $$$a=36$$$ and $$$b=48$$$ can be made equal in $$$4$$$ moves: $$$c=6$$$, divide $$$b$$$ by $$$c$$$ $$$\\Rightarrow$$$ $$$a=36$$$, $$$b=8$$$; $$$c=2$$$, divide $$$a$$$ by $$$c$$$ $$$\\Rightarrow$$$ $$$a=18$$$, $$$b=8$$$; $$$c=9$$$, divide $$$a$$$ by $$$c$$$ $$$\\Rightarrow$$$ $$$a=2$$$, $$$b=8$$$; $$$c=4$$$, divide $$$b$$$ by $$$c$$$ $$$\\Rightarrow$$$ $$$a=2$$$, $$$b=2$$$. For the given numbers $$$a$$$ and $$$b$$$, determine whether it is possible to make them equal using exactly $$$k$$$ turns.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t > 0) {\n long long int a;\n long long int b;\n long long int k;\n long long int i;\n long long int n;\n long long int count = 0;\n long long int counta = 0;\n long long int countb = 0;\n scanf(\"%lld%lld%lld\", &a, &b, &k);\n if (k == 1) {\n if ((a % b == 0 || b % a == 0) && a != b) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n } else {\n if (a > b) {\n n = sqrt(a);\n } else {\n n = sqrt(b);\n }\n\n while (a % 2 == 0) {\n a = a / 2;\n counta++;\n }\n while (b % 2 == 0) {\n b = b / 2;\n countb++;\n }\n for (i = 3; i <= n; i = i + 2) {\n\n while (a % i == 0) {\n a = a / i;\n counta++;\n }\n while (b % i == 0) {\n b = b / i;\n countb++;\n }\n }\n if (a > 2 && b > 2 && a == b) {\n count++;\n }\n if (a > 2) {\n counta++;\n }\n if (b > 2) {\n countb++;\n }\n if (counta + countb < k) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n t--;\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 let np = 100_000;\n let mut sieve = vec![true; np];\n sieve[0] = false;\n sieve[1] = false;\n for p in (2..).take_while(|&p| p * p <= np) {\n if !sieve[p] {\n continue;\n }\n for i in (p..).take_while(|&i| p * i < np) {\n sieve[i * p] = false;\n }\n }\n\n let prime_list: Vec<_> = sieve\n .into_iter()\n .enumerate()\n .filter_map(|(p, b)| if b { Some(p as u64) } else { None })\n .collect();\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 a = it.next().unwrap();\n let b = it.next().unwrap();\n let k = it.next().unwrap();\n\n let ndivisors = |n| {\n let mut x = n;\n let mut r = 0;\n\n for &p in &prime_list {\n if x == 1 {\n break;\n }\n if p * p > x {\n r += 1;\n break;\n }\n while x % p == 0 {\n x /= p;\n r += 1;\n }\n }\n\n r\n };\n\n let ans = if k == 1 {\n a != b && (a % b == 0 || b % a == 0)\n } else {\n let stall = ndivisors(a) + ndivisors(b);\n k <= stall\n };\n\n let ans = if ans { \"Yes\" } else { \"No\" };\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0631", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

Aoki loves numerical sequences and trees.

\n

One day, Takahashi gave him an integer sequence of length N, a_1, a_2, ..., a_N, which made him want to construct a tree.

\n

Aoki wants to construct a tree with N vertices numbered 1 through N, such that for each i = 1,2,...,N, the distance between vertex i and the farthest vertex from it is a_i, assuming that the length of each edge is 1.

\n

Determine whether such a tree exists.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≦ N ≦ 100
  • \n
  • 1 ≦ a_i ≦ N-1
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

If there exists a tree that satisfies the condition, print Possible. Otherwise, print Impossible.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n3 2 2 3 3\n
\n
\n
\n
\n
\n

Sample Output 1

Possible\n
\n

\"\"

\n

The diagram above shows an example of a tree that satisfies the conditions. The red arrows show paths from each vertex to the farthest vertex from it.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 1 2\n
\n
\n
\n
\n
\n

Sample Output 2

Impossible\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10\n1 2 2 2 2 2 2 2 2 2\n
\n
\n
\n
\n
\n

Sample Output 3

Possible\n
\n
\n
\n
\n
\n
\n

Sample Input 4

10\n1 1 2 2 2 2 2 2 2 2\n
\n
\n
\n
\n
\n

Sample Output 4

Impossible\n
\n
\n
\n
\n
\n
\n

Sample Input 5

6\n1 1 1 1 1 5\n
\n
\n
\n
\n
\n

Sample Output 5

Impossible\n
\n
\n
\n
\n
\n
\n

Sample Input 6

5\n4 3 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 6

Possible\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n int i;\n int j;\n int k;\n int num[101] = {0};\n\n scanf(\"%d\", &N);\n for (i = 0; i < N; i++) {\n scanf(\"%d\", &k);\n num[k]++;\n }\n\n for (i = 0; num[i] == 0; i++) {\n ;\n }\n if (num[i] > 2) {\n puts(\"Impossible\");\n return 0;\n }\n\n k = i;\n\n for (i++; num[i] != 0; i++) {\n if (num[i] == 1) {\n puts(\"Impossible\");\n return 0;\n }\n }\n\n for (j = i + 1; j < N; j++) {\n if (num[j] != 0) {\n puts(\"Impossible\");\n return 0;\n }\n }\n\n if (i - 1 > k * 2 - (num[k] - 1)) {\n puts(\"Impossible\");\n } else {\n puts(\"Possible\");\n }\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 a: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let mut c = vec![0; N];\n for &x in &a {\n c[x] += 1;\n }\n let m = *a.iter().min().unwrap();\n let ans = if (c[m] == 1\n && ((m + 1)..(2 * m + 1)).all(|i| c[i] >= 2)\n && ((2 * m + 1)..N).all(|i| c[i] == 0))\n || (c[m] == 2 && ((m + 1)..(2 * m)).all(|i| c[i] >= 2) && ((2 * m)..N).all(|i| c[i] == 0))\n {\n \"Possible\"\n } else {\n \"Impossible\"\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0632", "problem_description": "You are given a string $$$s$$$. You need to find two non-empty strings $$$a$$$ and $$$b$$$ such that the following conditions are satisfied: Strings $$$a$$$ and $$$b$$$ are both subsequences of $$$s$$$. For each index $$$i$$$, character $$$s_i$$$ of string $$$s$$$ must belong to exactly one of strings $$$a$$$ or $$$b$$$. String $$$a$$$ is lexicographically minimum possible; string $$$b$$$ may be any possible string. Given string $$$s$$$, print any valid $$$a$$$ and $$$b$$$.Reminder:A string $$$a$$$ ($$$b$$$) is a subsequence of a string $$$s$$$ if $$$a$$$ ($$$b$$$) can be obtained from $$$s$$$ by deletion of several (possibly, zero) elements. For example, \"dores\", \"cf\", and \"for\" are subsequences of \"codeforces\", while \"decor\" and \"fork\" are not.A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x \\ne y$$$; in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n\n char buffer[102];\n for (int c = 0; c < n; c++) {\n scanf(\"%s\", buffer);\n\n int i = 0;\n int min = (int)'z';\n int min_i = 0;\n while (buffer[i] != '\\0') {\n if ((int)buffer[i] < min) {\n min = (int)buffer[i];\n min_i = i;\n }\n i += 1;\n }\n\n buffer[min_i] = '\\0';\n printf(\"%c %s%s\\n\", min, buffer, &buffer[min_i + 1]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n let mut input = input.split_ascii_whitespace();\n\n let t = input.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let s = input.next().unwrap().as_bytes();\n let mn_pos = s\n .iter()\n .enumerate()\n .fold(0, |mn_pos, (i, c)| if c < &s[mn_pos] { i } else { mn_pos });\n println!(\n \"{} {}\",\n s[mn_pos] as char,\n String::from_utf8(s[..mn_pos].to_vec()).unwrap()\n + core::str::from_utf8(&s[mn_pos + 1..]).unwrap()\n );\n }\n}", "difficulty": "hard"} {"problem_id": "0633", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

You are going to eat X red apples and Y green apples.
\nYou have A red apples of deliciousness p_1,p_2, \\dots, p_A, B green apples of deliciousness q_1,q_2, \\dots, q_B, and C colorless apples of deliciousness r_1,r_2, \\dots, r_C.
\nBefore eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.
\nFrom the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.
\nFind the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq X \\leq A \\leq 10^5
  • \n
  • 1 \\leq Y \\leq B \\leq 10^5
  • \n
  • 1 \\leq C \\leq 10^5
  • \n
  • 1 \\leq p_i \\leq 10^9
  • \n
  • 1 \\leq q_i \\leq 10^9
  • \n
  • 1 \\leq r_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X Y A B C\np_1 p_2 ... p_A\nq_1 q_2 ... q_B\nr_1 r_2 ... r_C\n
\n
\n
\n
\n
\n

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 2 2 2 1\n2 4\n5 1\n3\n
\n
\n
\n
\n
\n

Sample Output 1

12\n
\n

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

\n
    \n
  • Eat the 2-nd red apple.
  • \n
  • Eat the 1-st green apple.
  • \n
  • Paint the 1-st colorless apple green and eat it.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

2 2 2 2 2\n8 6\n9 1\n2 1\n
\n
\n
\n
\n
\n

Sample Output 2

25\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2 2 4 4 4\n11 12 13 14\n21 22 23 24\n1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 3

74\n
\n
\n
", "c_code": "int solution() {\n long x;\n long y;\n long n[3];\n long pqr[3][100005][3];\n long h;\n long i;\n long j;\n long k;\n long app[3][100005];\n long temp;\n long prev;\n long start[3];\n long m;\n long c[3] = {0, 0, 0};\n long t[3];\n long pp;\n scanf(\"%ld\", &c[0]);\n scanf(\"%ld\", &c[1]);\n scanf(\"%ld\", &n[0]);\n scanf(\"%ld\", &n[1]);\n scanf(\"%ld\", &n[2]);\n for (i = 0; i < 3; i++) {\n for (j = 0; j < n[i]; j++) {\n scanf(\"%ld\", &pqr[i][j][0]);\n pqr[i][j][1] = -1;\n pqr[i][j][2] = j + 1;\n }\n pqr[i][n[i] - 1][2] = -1;\n start[i] = 0;\n }\n for (h = 0; h < 3; h++) {\n while (pqr[h][start[h]][2] != -1) {\n prev = -1;\n i = start[h];\n while (i != -1) {\n j = i;\n k = pqr[h][j][2];\n if (k == -1) {\n break;\n }\n i = pqr[h][k][2];\n pqr[h][j][2] = i;\n if (pqr[h][j][0] < pqr[h][k][0]) {\n if (prev == -1) {\n start[h] = k;\n } else {\n pqr[h][prev][2] = k;\n }\n temp = j;\n j = k;\n k = temp;\n }\n prev = j;\n while (k != -1) {\n if ((j == -1) || (pqr[h][j][0] < pqr[h][k][0])) {\n temp = pqr[h][k][1];\n pqr[h][k][1] = pqr[h][pp][1];\n pqr[h][pp][1] = k;\n pp = k;\n k = temp;\n } else {\n pp = j;\n j = pqr[h][pp][1];\n }\n }\n }\n }\n t[h] = 0;\n for (i = 0; i < n[h]; i++) {\n app[h][i] = pqr[h][start[h]][0];\n if (i < c[h]) {\n t[h] += app[h][i];\n }\n start[h] = pqr[h][start[h]][1];\n }\n }\n m = t[0] + t[1];\n start[0] = c[0] - 1;\n start[1] = c[1] - 1;\n for (start[2] = 0; start[2] < n[2]; start[2]++) {\n if (start[0] != -1) {\n if (start[1] != -1) {\n if (app[0][start[0]] < app[1][start[1]]) {\n if (app[0][start[0]] >= app[2][start[2]]) {\n break;\n }\n m -= app[0][start[0]];\n start[0]--;\n } else {\n if (app[1][start[1]] >= app[2][start[2]]) {\n break;\n }\n m -= app[1][start[1]];\n start[1]--;\n }\n } else {\n if (app[0][start[0]] >= app[2][start[2]]) {\n break;\n }\n m -= app[0][start[0]];\n start[0]--;\n }\n } else {\n if (start[1] == -1) {\n break;\n }\n if (app[1][start[1]] >= app[2][start[2]]) {\n break;\n }\n m -= app[1][start[1]];\n start[1]--;\n }\n m += app[2][start[2]];\n }\n printf(\"%ld\", m);\n return (0);\n}", "rust_code": "fn solution() {\n let (x, y, _a, _b, _c) = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let s: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n (s[0], s[1], s[2], s[3], s[4])\n };\n let mut p: Vec = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s.split_whitespace().map(|x| x.parse().unwrap()).collect()\n };\n let mut q: Vec = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s.split_whitespace().map(|x| x.parse().unwrap()).collect()\n };\n let mut r: Vec = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s.split_whitespace().map(|x| x.parse().unwrap()).collect()\n };\n p.sort();\n p.reverse();\n p.truncate(x);\n q.sort();\n q.reverse();\n q.truncate(y);\n r.extend(p);\n r.extend(q);\n r.sort();\n r.reverse();\n r.truncate(x + y);\n println!(\"{}\", r.into_iter().sum::());\n}", "difficulty": "hard"} {"problem_id": "0634", "problem_description": "You are given one integer number $$$n$$$. Find three distinct integers $$$a, b, c$$$ such that $$$2 \\le a, b, c$$$ and $$$a \\cdot b \\cdot c = n$$$ or say that it is impossible to do it.If there are several answers, you can print any.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; ++i) {\n int n = 0;\n int temp = 0;\n int done = 1;\n scanf(\"%d\", &n);\n for (int j = 2; j < sqrt(n) && done; ++j) {\n if (n % j == 0) {\n temp = n / j;\n for (int k = j; k < sqrt(temp) && done; ++k) {\n if (temp % k == 0 && k != j && temp / k != j) {\n printf(\"YES\\n%d %d %d\\n\", j, temp / k, k);\n done = 0;\n }\n }\n }\n }\n if (done) {\n printf(\"NO\\n\");\n }\n }\n return 0;\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 queries: i8 = line.trim().parse().unwrap();\n while queries > 0 {\n line.clear();\n input.read_line(&mut line).unwrap();\n let mut number: i32 = line.trim().parse().unwrap();\n let mut first: i32 = 2;\n loop {\n let mut second: i32 = number / first;\n if first >= second {\n println!(\"NO\");\n break;\n }\n if number % first == 0 {\n number = second;\n second = first;\n loop {\n second += 1;\n let third: i32 = number / second;\n if second >= third {\n println!(\"NO\");\n break;\n }\n if number % second == 0 {\n println!(\"YES\\n{} {} {}\", first, second, third);\n break;\n }\n }\n break;\n }\n first += 1;\n }\n queries -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "0635", "problem_description": "There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.", "c_code": "int solution()\n\n{\n int a = 0;\n int b = 0;\n int c = 0;\n int n;\n\n scanf(\"%d\", &n);\n\n int arr[n];\n int i;\n\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n\n if (arr[i] == 1) {\n a++;\n\n } else if (arr[i] == 2) {\n b++;\n } else {\n c++;\n }\n }\n\n if (a >= b && a >= c) {\n printf(\"%d\\n\", b + c);\n\n } else if (b >= c && b >= a) {\n printf(\"%d\\n\", a + c);\n\n } else if (c >= a && c >= b) {\n printf(\"%d\\n\", a + b);\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let nums: Vec<&str> = buf.split_whitespace().collect();\n\n let (mut one, mut two, mut three) = (0, 0, 0);\n\n for num in nums {\n match num {\n \"1\" => one += 1,\n \"2\" => two += 1,\n \"3\" => three += 1,\n _ => unreachable!(),\n }\n }\n\n let mut digits = [&one, &two, &three];\n digits.sort_unstable();\n println!(\"{}\", digits[0] + digits[1]);\n}", "difficulty": "medium"} {"problem_id": "0636", "problem_description": "A binary string is a string consisting only of the characters 0 and 1. You are given a binary string $$$s$$$.For some non-empty substring$$$^\\dagger$$$ $$$t$$$ of string $$$s$$$ containing $$$x$$$ characters 0 and $$$y$$$ characters 1, define its cost as: $$$x \\cdot y$$$, if $$$x > 0$$$ and $$$y > 0$$$; $$$x^2$$$, if $$$x > 0$$$ and $$$y = 0$$$; $$$y^2$$$, if $$$x = 0$$$ and $$$y > 0$$$. Given a binary string $$$s$$$ of length $$$n$$$, find the maximum cost across all its non-empty substrings.$$$^\\dagger$$$ A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int tt = 0; tt < t; tt++) {\n int n;\n scanf(\"\\n%d\\n\", &n);\n char arr[n];\n long long int n1 = 0;\n long long int n0 = 0;\n long long int maxx;\n for (int i = 0; i < n; i++) {\n scanf(\"%c\", &arr[i]);\n if (arr[i] == '0') {\n n0++;\n }\n if (arr[i] == '1') {\n n1++;\n }\n }\n if (n0 == 0) {\n maxx = n1 * n1;\n printf(\"%lld\\n\", maxx);\n continue;\n }\n if (n1 == 0) {\n maxx = n0 * n0;\n printf(\"%lld\\n\", maxx);\n continue;\n }\n\n maxx = n1 * n0;\n long long int tmp = 0;\n char prev = '!';\n for (int i = 0; i < n; i++) {\n if ((prev == '!') || (prev == arr[i])) {\n tmp++;\n } else {\n tmp = 1;\n }\n prev = arr[i];\n if ((tmp * tmp) > maxx) {\n maxx = tmp * tmp;\n }\n }\n printf(\"%lld\\n\", maxx);\n }\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(2)\n .step_by(2)\n .flatten()\n .for_each(|s| {\n let t = s.as_bytes();\n let zeroes = t.iter().fold(\n 0usize,\n |acc, &x| if x == b'0' { acc.wrapping_add(1) } else { acc },\n );\n let ones = t.len().wrapping_sub(zeroes);\n let mut ans = 1usize.max(zeroes.wrapping_mul(ones));\n t.windows(2).fold(1usize, |a, c| {\n let b: usize = match c {\n &[d, e] if d == e => a.wrapping_add(1),\n _ => 1,\n };\n ans = ans.max(b.wrapping_mul(b));\n b\n });\n writeln!(buf, \"{ans}\").unwrap_or_default();\n });\n}", "difficulty": "hard"} {"problem_id": "0637", "problem_description": "Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is multiplied by k2, and the loser's score is multiplied by k. In the beginning of the game, both Slastyona and Pushok have scores equal to one.Unfortunately, Slastyona had lost her notepad where the history of all n games was recorded. She managed to recall the final results for each games, though, but all of her memories of them are vague. Help Slastyona verify their correctness, or, to put it another way, for each given pair of scores determine whether it was possible for a game to finish with such result or not.", "c_code": "int solution() {\n int n;\n long long t;\n long long a;\n long long b;\n scanf(\"%d\", &n);\n while (n--) {\n scanf(\"%lld%lld\", &a, &b);\n t = a * b;\n t = round(pow(t, 1.0 / 3));\n if (t * t * t != a * b) {\n printf(\"No\\n\");\n } else {\n if (a % t == 0 && b % t == 0) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n std::io::stdin().read_line(&mut input).unwrap();\n\n let n = input.trim().parse::().unwrap();\n\n let mut ress = Vec::with_capacity(n as usize);\n\n for _i in 0..n {\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).unwrap();\n let mut it = input.split_whitespace().map(|x| x.parse::().unwrap());\n ress.push((it.next().unwrap(), it.next().unwrap()));\n }\n\n for (a, b) in ress {\n let c: f64 = a as f64 * b as f64;\n let d = c.cbrt();\n if c == d.round().powi(3) && a % d.round() as u32 == 0 && b % d.round() as u32 == 0 {\n println!(\"yes\");\n } else {\n println!(\"no\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0638", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

How many ways are there to choose two distinct positive integers totaling N, disregarding the order?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^6
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

There is only one way to choose two distinct integers totaling 4: to choose 1 and 3. (Choosing 3 and 1 is not considered different from this.)

\n
\n
\n
\n
\n
\n

Sample Input 2

999999\n
\n
\n
\n
\n
\n

Sample Output 2

499999\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n\n int cnt = 0;\n for (int i = 1; i < n - i; i++) {\n cnt++;\n }\n printf(\"%d\", cnt);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let n: i64 = s.trim().parse().ok().unwrap();\n println!(\"{}\", (n - 1) / 2);\n}", "difficulty": "medium"} {"problem_id": "0639", "problem_description": "One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.For example, there is a statement called the \"Goldbach's conjecture\". It says: \"each even number no less than four can be expressed as the sum of two primes\". Let's modify it. How about a statement like that: \"each integer no less than 12 can be expressed as the sum of two composite numbers.\" Not like the Goldbach's conjecture, I can prove this theorem.You are given an integer n no less than 12, express it as a sum of two composite numbers.", "c_code": "int solution() {\n int i;\n scanf(\"%i\", &i);\n int k = 2;\n while ((i - 2 * k) % 2 && (i - 2 * k) % 3) {\n k++;\n }\n printf(\"%i %i\", 2 * k, i - (2 * k));\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let n: i32 = s.trim().parse().unwrap();\n if n % 2 == 0 {\n println!(\"{} {}\", 4, n - 4);\n } else {\n println!(\"{} {}\", 9, n - 9);\n }\n}", "difficulty": "medium"} {"problem_id": "0640", "problem_description": "

Rectangle

\n\n

\nWrite a program which calculates the area and perimeter of a given rectangle.\n

\n\n\n

Input

\n\n

\n The length a and breadth b of the rectangle are given in a line separated by a single space.\n

\n\n

Output

\n\n

\nPrint the area and perimeter of the rectangle in a line. The two integers should be separated by a single space.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ a, b ≤ 100
  • \n
\n\n

Sample Input 1

\n
\n3 5\n
\n

Sample Output 1

\n
\n15 16\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n scanf(\"%d %d\", &a, &b);\n while (a <= 1 && a >= 100 && b <= 1 && b >= 100) {\n scanf(\"%d %d\", &a, &b);\n }\n printf(\"%d %d\\n\", a * b, ((a * 2) + (b * 2)));\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n\n let mut line = String::new();\n\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n\n let n: u32 = vec[0].parse().unwrap_or(0);\n let m: u32 = vec[1].parse().unwrap_or(0);\n\n println!(\"{} {}\", n * m, 2 * (n + m));\n}", "difficulty": "medium"} {"problem_id": "0641", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Iroha loves Haiku. Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.

\n

To create a Haiku, Iroha has come up with three different phrases. These phrases have A, B and C syllables, respectively. Determine whether she can construct a Haiku by using each of the phrases once, in some order.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦A,B,C≦10
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

If it is possible to construct a Haiku by using each of the phrases once, print YES (case-sensitive). Otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 5 7\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

Using three phrases of length 5, 5 and 7, it is possible to construct a Haiku.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 7 5\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
", "c_code": "int solution(void) {\n int a[3];\n scanf(\"%d%d%d\", &a[0], &a[1], &a[2]);\n if (a[0] == 7) {\n if (a[1] == 5 && a[2] == 5) {\n printf(\"YES\");\n return 0;\n }\n }\n if (a[1] == 7) {\n if (a[0] == 5 && a[2] == 5) {\n printf(\"YES\");\n return 0;\n }\n }\n if (a[2] == 7) {\n if (a[0] == 5 && a[1] == 5) {\n printf(\"YES\");\n return 0;\n }\n }\n printf(\"NO\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n drop(std::io::stdin().read_to_string(&mut line));\n let line: Vec<_> = line.split_whitespace().collect();\n let mut line: Vec<_> = line.iter().filter_map(|x| i32::from_str(x).ok()).collect();\n line.sort();\n let line = line;\n print!(\"{}\", if line == [5, 5, 7] { \"YES\" } else { \"NO\" });\n}", "difficulty": "easy"} {"problem_id": "0642", "problem_description": "Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in.For example, consider the case when the mother has 5 flowers, and their moods are equal to 1,  - 2, 1, 3,  - 4. Suppose the mother suggested subarrays (1,  - 2), (3,  - 4), (1, 3), (1,  - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then: the first flower adds 1·1 = 1 to the girl's happiness, because he is in one of chosen subarrays, the second flower adds ( - 2)·1 =  - 2, because he is in one of chosen subarrays, the third flower adds 1·2 = 2, because he is in two of chosen subarrays, the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays, the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays. Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.", "c_code": "int solution() {\n int i = 0;\n int j = 0;\n int n = 0;\n int m = 0;\n int l = 0;\n int r = 0;\n int max = 0;\n int beauty = 0;\n int a[150] = {0};\n scanf(\"%d %d\", &n, &m);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n for (i = 0; i < m; i++) {\n scanf(\"%d %d\", &l, &r);\n l--;\n beauty = 0;\n\n for (j = l; j < r; j++) {\n beauty += a[j];\n }\n if (max + beauty > max) {\n max += beauty;\n }\n }\n\n printf(\"%d\\n\", max);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_line = String::new();\n stdin().read_line(&mut input_line).unwrap();\n let mut input_iter = input_line\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n let flower_count = input_iter.next().unwrap();\n let subarray_count = input_iter.next().unwrap();\n\n let mut sum_range = [0; 101];\n let mut input_line = String::new();\n stdin().read_line(&mut input_line).unwrap();\n let mut input_iter = input_line\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n for i in 1..(flower_count + 1) {\n sum_range[i] = sum_range[i - 1] + input_iter.next().unwrap();\n }\n\n let mut result = 0;\n\n for _ in 1..(subarray_count + 1) {\n let mut input_line = String::new();\n stdin().read_line(&mut input_line).unwrap();\n let mut input_iter = input_line\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n let left = input_iter.next().unwrap();\n let right = input_iter.next().unwrap();\n let happiness = sum_range[right] - sum_range[left - 1];\n if happiness > 0 {\n result += happiness;\n }\n }\n\n println!(\"{}\", result);\n}", "difficulty": "hard"} {"problem_id": "0643", "problem_description": "This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.The jury picked an integer $$$x$$$ not less than $$$0$$$ and not greater than $$$2^{14} - 1$$$. You have to guess this integer.To do so, you may ask no more than $$$2$$$ queries. Each query should consist of $$$100$$$ integer numbers $$$a_1$$$, $$$a_2$$$, ..., $$$a_{100}$$$ (each integer should be not less than $$$0$$$ and not greater than $$$2^{14} - 1$$$). In response to your query, the jury will pick one integer $$$i$$$ ($$$1 \\le i \\le 100$$$) and tell you the value of $$$a_i \\oplus x$$$ (the bitwise XOR of $$$a_i$$$ and $$$x$$$). There is an additional constraint on the queries: all $$$200$$$ integers you use in the queries should be distinct.It is guaranteed that the value of $$$x$$$ is fixed beforehand in each test, but the choice of $$$i$$$ in every query may depend on the integers you send.", "c_code": "int solution(void) {\n unsigned int top;\n unsigned int bot;\n\n printf(\"? \");\n for (unsigned int x = 1; x <= 100; ++x) {\n printf(\"%u \", x);\n }\n printf(\"\\n\");\n fflush(stdout);\n\n scanf(\"%u\", &top);\n\n printf(\"? \");\n for (unsigned int x = 1; x <= 100; ++x) {\n printf(\"%u \", x << 7);\n }\n printf(\"\\n\");\n fflush(stdout);\n\n scanf(\"%u\", &bot);\n\n printf(\"! %u\", (top & 0b11111110000000) | (bot & 0b00000001111111));\n\n return 0;\n}", "rust_code": "fn solution() {\n print!(\"?\");\n for i in 1..101 {\n print!(\" {}\", i);\n }\n println!();\n print!(\"?\");\n for i in 1..101 {\n print!(\" {}\", i << 7);\n }\n println!();\n stdout().flush().unwrap();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let rear = buf.trim().parse::().unwrap();\n\n buf.clear();\n std::io::stdin().read_line(&mut buf).unwrap();\n let front = buf.trim().parse::().unwrap();\n\n println!(\"! {}\", (rear & !((1 << 7) - 1)) | (front & ((1 << 7) - 1)));\n}", "difficulty": "easy"} {"problem_id": "0644", "problem_description": "\n

Score: 400 points

\n
\n
\n

Problem Statement

\n

In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east.\nA company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i).\nTakahashi the king is interested in the following Q matters:

\n
    \n
  • The number of the trains that runs strictly within the section from City p_i to City q_i, that is, the number of trains j such that p_i \\leq L_j and R_j \\leq q_i.
  • \n
\n

Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • N is an integer between 1 and 500 (inclusive).
  • \n
  • M is an integer between 1 and 200 \\ 000 (inclusive).
  • \n
  • Q is an integer between 1 and 100 \\ 000 (inclusive).
  • \n
  • 1 \\leq L_i \\leq R_i \\leq N (1 \\leq i \\leq M)
  • \n
  • 1 \\leq p_i \\leq q_i \\leq N (1 \\leq i \\leq Q)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N M Q\nL_1 R_1\nL_2 R_2\n:\nL_M R_M\np_1 q_1\np_2 q_2\n:\np_Q q_Q\n
\n
\n
\n
\n
\n

Output

\n

Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 1\n1 1\n1 2\n2 2\n1 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

As all the trains runs within the section from City 1 to City 2, the answer to the only query is 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 3 2\n1 5\n2 8\n7 10\n1 7\n3 10\n
\n
\n
\n
\n
\n

Sample Output 2

1\n1\n
\n

The first query is on the section from City 1 to 7. There is only one train that runs strictly within that section: Train 1.\nThe second query is on the section from City 3 to 10. There is only one train that runs strictly within that section: Train 3.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 10 10\n1 6\n2 9\n4 5\n4 7\n4 7\n5 8\n6 6\n6 7\n7 9\n10 10\n1 8\n1 9\n1 10\n2 8\n2 9\n2 10\n3 8\n3 9\n3 10\n1 10\n
\n
\n
\n
\n
\n

Sample Output 3

7\n9\n10\n6\n8\n9\n6\n7\n8\n10\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n long m;\n long q;\n scanf(\"%ld %ld %ld\", &n, &m, &q);\n long l[m];\n long r[m];\n for (long i = 0; i < m; i++) {\n scanf(\"%ld %ld\", &l[i], &r[i]);\n }\n long sp[q];\n long sq[q];\n for (long i = 0; i < q; i++) {\n scanf(\"%ld %ld\", &sp[i], &sq[i]);\n }\n long train[n + 1][n + 1];\n for (long i = 0; i <= n; i++) {\n for (long j = 0; j <= n; j++) {\n train[i][j] = 0;\n }\n }\n for (long i = 0; i < m; i++) {\n train[l[i]][r[i]]++;\n }\n long sum[n + 1][n + 1];\n for (long i = 1; i <= n; i++) {\n sum[i][i - 1] = 0;\n for (long j = i; j <= n; j++) {\n sum[i][j] = sum[i][j - 1] + train[i][j];\n }\n }\n long ans;\n for (long i = 0; i < q; i++) {\n ans = 0;\n for (int j = sp[i]; j <= sq[i]; j++) {\n ans += sum[j][sq[i]] - sum[j][j - 1];\n }\n printf(\"%ld\\n\", ans);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N: usize,\n M: usize,\n Q: usize,\n train: [(usize, usize); M],\n part: [(usize, usize); Q],\n }\n\n let mut count_map = vec![vec![0; N + 1]; N + 1];\n for t in train.iter() {\n count_map[t.0][t.1] += 1;\n }\n\n let mut cml_sum = vec![vec![0; N + 1]; N + 1];\n let mut colum_sum = vec![0; N + 1];\n for i in 0..N + 1 {\n colum_sum[0] += count_map[i][0];\n cml_sum[i][0] = colum_sum[0];\n for j in 1..N + 1 {\n colum_sum[j] += count_map[i][j];\n cml_sum[i][j] = cml_sum[i][j - 1] + colum_sum[j];\n }\n }\n\n for q in part.iter() {\n let count = cml_sum[q.0 - 1][q.0 - 1] + cml_sum[q.1][q.1]\n - cml_sum[q.0 - 1][q.1]\n - cml_sum[q.1][q.0 - 1];\n println!(\"{}\", count);\n }\n}", "difficulty": "medium"} {"problem_id": "0645", "problem_description": "Insertion sort", "c_code": "void solution(int *arr, int size) {\n for (int i = 1; i < size; i++) {\n int j = i - 1;\n int key = arr[i];\n\n while (j >= 0 && key < arr[j]) {\n arr[j + 1] = arr[j];\n j = j - 1;\n }\n\n arr[j + 1] = key;\n }\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n for i in 1..arr.len() {\n let mut j = i;\n let cur = arr[i];\n\n while j > 0 && cur < arr[j - 1] {\n arr[j] = arr[j - 1];\n j -= 1;\n }\n\n arr[j] = cur;\n }\n}\n", "difficulty": "easy"} {"problem_id": "0646", "problem_description": "

双六 (Sugoroku)

\n\n

問題文

\n

\nJOI 君はおじさんの家で双六を見つけた.双六は直線状に並んだ N+2 個のマスからなり,1 番目のマスはスタート,N+2 番目のマスはゴールである.その他の各マスには 0 または 1 が書かれていて,各 i (1≦i≦N) について,i+1 番目のマスに書かれた数字は A_i である.\n

\n\n

\n双六では,最初にスタートのマスにコマを置き,サイコロを振って,出た目の数だけコマを進めることを繰り返す.ただし,1 の書かれたマスに止まった場合は,ゲームオーバーである.ゲームオーバーにならずにゴールのマスに止まるか,ゴールのマスを通り過ぎたら,ゲームクリアである.

\n\n

\nJOI 君は双六を遊ぶためのサイコロをおもちゃ屋さんに買いに行くことにした.おもちゃ屋さんには N+1 個のサイコロが売っている.j 番目 (1≦j≦N+1) のサイコロは j 個の面を持ち,1,2,...,j1 つずつ書かれている.

\n\n

\nJOI 君はゲームクリアできるようなサイコロのうち,最も面の数が少ないサイコロを 1 個買うことにした.JOI 君はどのサイコロを買えばよいだろうか.

\n\n

制約

\n\n
    \n
  • 1 \\leq N \\leq100
  • \n
  • 0 \\leq A_i \\leq 1 (1 \\leq i \\leq N)
  • \n
\n\n

入力・出力

\n\n

\n入力
\n入力は以下の形式で標準入力から与えられる.
\nN
\nA_1 A_2 ... A_N\n

\n\n

\n出力
\nJOI 君が購入すべきサイコロの面の数を答えよ.
\n\n

入出力例

\n\n\n入力例 1
\n
\n5\n0 1 0 0 0\n
\n\n\n出力例 1
\n
\n2\n
\n\n

\n双六は 7 マスからなり,3 マス目のみに 1 が書かれている.面の数が 2 個のサイコロを使った場合,例えば出た目が 1,2,1,1,1 となったときにゲームクリアすることができる.これが最小なので 2 を出力する.

\n\n
\n\n\n入力例 2
\n
\n5\n1 1 1 1 1\n
\n\n\n出力例 2
\n
\n6\n
\n\n

\n双六は 7 マスからなり,スタートとゴール以外のマス全てに 1 が書かれている.このとき,面の数が 6 個のサイコロが必要である.これが最小なので 6 を出力する.

\n\n
\n\n入力例 3
\n
\n7\n0 0 1 0 1 1 0\n
\n\n\n出力例 3
\n
\n3\n
\n\n\n\n
\n

\n \"クリエイティブ・コモンズ・ライセンス\"\n
\n \n 情報オリンピック日本委員会作 『第 17 回日本情報オリンピック JOI 2017/2018 予選競技課題』\n

", "c_code": "int solution() {\n int N;\n int L[105];\n int point = 0;\n int max = 1;\n int tag = 0;\n int n = 1;\n scanf(\"%d\", &N);\n for (int i = 0; i < N; ++i) {\n scanf(\"%d\", L + i);\n }\n while (point < N) {\n if (tag == 0 && L[point]) {\n tag = 1;\n }\n if (tag && !L[point]) {\n tag = 0;\n if (n > max) {\n max = n;\n }\n n = 1;\n }\n if (tag && point == N - 1) {\n ++n;\n if (n > max) {\n max = n;\n }\n }\n if (tag) {\n ++n;\n }\n ++point;\n }\n\n printf(\"%d\\n\", max);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let lock = stdin.lock();\n let mut iter = lock.lines();\n\n iter.next();\n println!(\n \"{}\",\n iter.next()\n .unwrap()\n .unwrap()\n .split(' ')\n .scan(0, |acc, s| {\n *acc = if s == \"1\" { *acc + 1 } else { 0 };\n Some(*acc)\n })\n .max()\n .unwrap()\n + 1\n );\n}", "difficulty": "medium"} {"problem_id": "0647", "problem_description": "\n\n\n

Areas on the Cross-Section Diagram

\n\n

\n Your task is to simulate a flood damage.\n

\n\n

\n For a given cross-section diagram, reports areas of flooded sections.\n

\n\n
\n
\n
\n
\n\n

\n Assume that rain is falling endlessly in the region and the water overflowing from the region is falling in the sea at the both sides.\nFor example, for the above cross-section diagram, the rain will create floods which have areas of 4, 2, 1, 19 and 9 respectively.\n

\n\n

Input

\n\n

\n A string, which represents slopes and flatlands by '/', '\\' and '_' respectively, is given in a line. For example, the region of the above example is given by a string \"\\\\///\\_/\\/\\\\\\\\/_/\\\\///__\\\\\\_\\\\/_\\/_/\\\".\n

\n\n

output

\n\n

\n Report the areas of floods in the following format:\n

\n\n

\n$A$
\n$k$ $L_1$ $L_2$ ... $L_k$\n

\n\n

\n In the first line, print the total area $A$ of created floods.\n

\n

\n In the second line, print the number of floods $k$ and areas $L_i (i = 1, 2, ..., k)$ for each flood from the left side of the cross-section diagram. Print a space character before $L_i$.\n

\n\n\n

Constraints

\n\n
    \n
  • $1 \\leq$ length of the string $\\leq 20,000$
  • \n
\n\n\n

Sample Input 1

\n
\n\\\\//\n
\n\n

Sample Output 1

\n
\n4\n1 4\n
\n\n\n
\n\n

Sample Input 2

\n
\n\\\\///\\_/\\/\\\\\\\\/_/\\\\///__\\\\\\_\\\\/_\\/_/\\\n
\n\n

Sample Output 2

\n
\n35\n5 4 2 1 19 9\n
", "c_code": "int solution() {\n int c;\n int x;\n int i;\n int sum = 0;\n int putop = 0;\n int xtop = 0;\n int xstack[20000];\n int lefts[20000];\n int vols[20000];\n\n for (x = 0; (c = getchar()) != '\\n'; x++) {\n if (c == '\\\\') {\n xstack[xtop++] = x;\n } else if (c == '/') {\n if (xtop == 0) {\n continue;\n }\n int last_x = xstack[--xtop];\n int new_vol = x - last_x;\n while (putop > 0 && last_x < lefts[putop - 1]) {\n new_vol += vols[--putop];\n }\n lefts[putop] = x;\n vols[putop] = new_vol;\n putop++;\n }\n }\n for (i = 0; i < putop; i++) {\n sum += vols[i];\n }\n\n printf(\"%d\\n%d\", sum, putop);\n\n for (i = 0; i < putop; i++) {\n printf(\" %d\", vols[i]);\n }\n puts(\"\");\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut buf_it = buf.split_whitespace();\n\n let C = buf_it.next().unwrap().chars().collect::>();\n let N = C.len();\n\n let mut H = vec![0isize];\n for &c in &C {\n let h = H[H.len() - 1];\n match c {\n '/' => {\n H.push(h + 1);\n }\n '_' => {\n H.push(h);\n }\n '\\\\' => {\n H.push(h - 1);\n }\n _ => {}\n }\n }\n let mut L = H.clone();\n let mut R = H.clone();\n for i in 0..N {\n L[i + 1] = max(L[i], L[i + 1]);\n R[N - i - 1] = max(R[N - i - 1], R[N - i]);\n }\n let W = L\n .iter()\n .zip(R.iter())\n .map(|(a, b)| *min(a, b))\n .collect::>();\n let mut ans = Vec::new();\n let mut tmp = 0;\n for i in 0..N + 1 {\n let h = H[i];\n let w = W[i];\n if w - h <= 0 && tmp > 0 {\n ans.push(tmp);\n tmp = 0;\n } else if w - h > 0 {\n tmp += w - h;\n }\n }\n println!(\"{}\", ans.iter().sum::());\n if ans.is_empty() {\n println!(\"0\");\n } else {\n println!(\n \"{} {}\",\n ans.len(),\n ans.iter()\n .map(|a| format!(\"{}\", a))\n .collect::>()\n .join(\" \")\n );\n }\n}", "difficulty": "medium"} {"problem_id": "0648", "problem_description": "There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int i;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (i = 0; i < n - 2; i++) {\n if ((a[i] > a[i + 1]) && (a[i + 1] < a[i + 2])) {\n printf(\"No\");\n return 0;\n }\n }\n printf(\"Yes\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n s.clear();\n let _: i32 = {\n io::stdin().read_line(&mut s).ok();\n s.trim().parse().unwrap()\n };\n s.clear();\n let a: Vec = {\n io::stdin().read_line(&mut s).ok();\n s.split_whitespace().map(|s| s.parse().unwrap()).collect()\n };\n let mut last = 0i32;\n let mut up = true;\n let mut good_flag = true;\n for el in a {\n if up {\n if el < last {\n up = false;\n }\n } else {\n if el > last {\n good_flag = false;\n break;\n }\n }\n last = el;\n }\n\n println!(\"{}\", if good_flag { \"YES\" } else { \"NO\" });\n}", "difficulty": "medium"} {"problem_id": "0649", "problem_description": "You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the function input() in Python2 instead of it use the function raw_input().", "c_code": "int solution() {\n char a[1000002];\n char b[1000002];\n fgets(a, 1000002, stdin);\n fgets(b, 1000002, stdin);\n char *aa = a;\n char *bb = b;\n while (*aa == '0') {\n ++aa;\n }\n while (*bb == '0') {\n ++bb;\n }\n int la = strlen(aa);\n int lb = strlen(bb);\n if (la != lb) {\n printf(\"%s\\n\", la > lb ? \">\" : \"<\");\n } else {\n int cmp = memcmp(aa, bb, la);\n printf(\"%s\\n\", cmp > 0 ? \">\" : (cmp < 0 ? \"<\" : \"=\"));\n }\n}", "rust_code": "fn solution() {\n let mut a = String::with_capacity(1000_002);\n let mut b = String::with_capacity(1000_002);\n stdin().read_line(&mut a).unwrap();\n stdin().read_line(&mut b).unwrap();\n let mut ar1: [u8; 1000_001] = [0; 1000_001];\n let mut ar2: [u8; 1000_001] = [0; 1000_001];\n let (mut i, mut j) = (0, 0);\n for c in a.trim_end().chars() {\n if i == 0 && c == '0' {\n continue;\n }\n ar1[i] = c as u8;\n i += 1;\n }\n for c in b.trim_end().chars() {\n if j == 0 && c == '0' {\n continue;\n }\n ar2[j] = c as u8;\n j += 1;\n }\n if i > j {\n println!(\">\");\n return;\n } else if i < j {\n println!(\"<\");\n return;\n }\n for k in 0..i {\n if ar1[k] > ar2[k] {\n println!(\">\");\n return;\n } else if ar1[k] < ar2[k] {\n println!(\"<\");\n return;\n }\n }\n println!(\"=\");\n}", "difficulty": "easy"} {"problem_id": "0650", "problem_description": "Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.It is known that in a match between two players, the one whose skill is greater will win. The skill of the $$$i$$$-th player is equal to $$$s_i$$$ and all skill levels are pairwise different (i. e. there are no two identical values in the array $$$s$$$).The tournament is called fair if the two players with the highest skills meet in the finals.Determine whether the given tournament is fair.", "c_code": "int solution() {\n int x;\n scanf(\"%d\", &x);\n for (int i = 0; i < x; i++) {\n int a[4];\n int m = 0;\n int n = 0;\n for (int j = 0; j < 4; j++) {\n scanf(\"%d\", &a[j]);\n if (a[j] > m) {\n n = m;\n m = a[j];\n } else if (a[j] < m) {\n if (a[j] > n) {\n n = a[j];\n }\n }\n }\n\n if (((a[0] == m || a[1] == m) && (a[2] == n || a[3] == n)) ||\n ((a[0] == n || a[1] == n) && (a[2] == m || a[3] == m))) {\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 for l in i.lock().lines().skip(1) {\n if let [mut s1, mut s2, mut s3, mut s4] = l\n .unwrap()\n .split_whitespace()\n .map(|w| w.parse::().unwrap())\n .collect::>()[..]\n {\n if s1 > s2 {\n swap(&mut s1, &mut s2)\n };\n if s3 > s4 {\n swap(&mut s3, &mut s4)\n };\n writeln!(o, \"{}\", if s2 < s3 || s4 < s1 { \"NO\" } else { \"YES\" }).ok();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0651", "problem_description": "

Shopping

\n\n

\nYou are now in a bookshop with your friend Alice to buy a book, \"The Winning Strategy for the Programming Koshien Contest,” just released today. As you definitely want to buy it, you are planning to borrow some money from Alice in case the amount you have falls short of the price. If the amount you receive from Alice still fails to meet the price, you have to abandon buying the book this time.\n

\n\n

\n Write a program to calculate the minimum amount of money you need to borrow from Alice given the following three items of data: the money you and Alice have now and the price of the book.\n

\n\n\n

Input

\n\n

\n The input is given in the following format.\n

\n\n
\nm f b\n
\n\n

\nA line containing the three amounts of money is given: the amount you have with you now m (0 ≤ m ≤ 10000), the money Alice has now f (0 ≤ f ≤ 10000) and the price of the book b (100 ≤ b ≤ 20000).\n

\n\n

Output

\n\n

\nOutput a line suggesting the minimum amount of money you need to borrow from Alice. Output \"NA\" if all the money Alice has with him now is not a sufficient amount for you to buy the book.\n

\n\n

Sample Input 1

\n\n
\n1000 3000 3000\n
\n\n

Sample Output 1

\n
\n2000\n
\n\n

Sample Input 2

\n
\n5000 3000 4500\n
\n\n

Sample Output 2

\n
\n0\n
\n\n\n

Sample Input 3

\n
\n500 1000 2000\n
\n\n

Sample Output 3

\n
\nNA\n
", "c_code": "int solution() {\n\n int money;\n int friend;\n int price;\n\n scanf(\"%d %d %d\", &money, &friend, &price);\n\n if (money - price >= 0) {\n printf(\"0\\n\");\n }\n if ((money - price) < 0 && friend >= -1 * (money - price)) {\n printf(\"%d\\n\", -1 * (money - price));\n }\n if (money - price < 0 && friend < -1 * (money - price)) {\n printf(\"NA\\n\");\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 let mut iter = buf.split_whitespace();\n\n let a: usize = iter.next().unwrap().parse().unwrap();\n let b: usize = iter.next().unwrap().parse().unwrap();\n let c: usize = iter.next().unwrap().parse().unwrap();\n\n if a >= c {\n println!(\"{}\", 0);\n } else if a + b < c {\n println!(\"NA\");\n } else {\n println!(\"{}\", c - a);\n }\n}", "difficulty": "hard"} {"problem_id": "0652", "problem_description": "Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.The valid sizes of T-shirts are either \"M\" or from $$$0$$$ to $$$3$$$ \"X\" followed by \"S\" or \"L\". For example, sizes \"M\", \"XXS\", \"L\", \"XXXL\" are valid and \"XM\", \"Z\", \"XXXXL\" are not.There are $$$n$$$ winners to the cup for both the previous year and the current year. Ksenia has a list with the T-shirt sizes printed for the last year cup and is yet to send the new list to the printing office. Organizers want to distribute the prizes as soon as possible, so now Ksenia is required not to write the whole list from the scratch but just make some changes to the list of the previous year. In one second she can choose arbitrary position in any word and replace its character with some uppercase Latin letter. Ksenia can't remove or add letters in any of the words.What is the minimal number of seconds Ksenia is required to spend to change the last year list to the current one?The lists are unordered. That means, two lists are considered equal if and only if the number of occurrences of any string is the same in both lists.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n char a[n][100];\n char b[n][100];\n int strlen1[n];\n int strlen2[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", a[i]);\n strlen1[i] = strlen(a[i]);\n }\n\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", b[i]);\n strlen2[i] = strlen(b[i]);\n }\n\n int f = 0;\n int visited[100];\n for (int i = 0; i < 100; i++) {\n visited[i] = 0;\n }\n\n int count = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (strlen1[i] == strlen2[j] && visited[j] != 1) {\n if (strcmp(a[i], b[j]) == 0) {\n visited[j] = 1;\n count++;\n break;\n }\n }\n }\n }\n\n printf(\"%d\", n - count);\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 n = get!();\n use std::collections::HashMap;\n let mut xs = HashMap::new();\n for _ in 0..n {\n let s = get!(String);\n let c = match xs.get(&s) {\n Some(&c) => c + 1,\n None => 1,\n };\n xs.insert(s, c);\n }\n let mut ans = 0;\n for _ in 0..n {\n let s = get!(String);\n let &c = xs.get(&s).unwrap_or(&0);\n if c == 0 {\n ans += 1;\n } else {\n xs.insert(s, c - 1);\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0653", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N.\nYou would like to sort this sequence in ascending order by repeating the following operation:

\n
    \n
  • Choose an element in the sequence and move it to the beginning or the end of the sequence.
  • \n
\n

Find the minimum number of operations required.\nIt can be proved that it is actually possible to sort the sequence using this operation.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2\\times 10^5
  • \n
  • (P_1,P_2,...,P_N) is a permutation of (1,2,...,N).
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nP_1\n:\nP_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of operations required.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n1\n3\n2\n4\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

For example, the sequence can be sorted in ascending order as follows:

\n
    \n
  • Move 2 to the beginning. The sequence is now (2,1,3,4).
  • \n
  • Move 1 to the beginning. The sequence is now (1,2,3,4).
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

6\n3\n2\n5\n1\n4\n6\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8\n6\n3\n1\n2\n7\n4\n8\n5\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution(void) {\n\n int n;\n scanf(\"%d\", &n);\n int p[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &p[i]);\n }\n int q[n + 1];\n for (int i = 0; i < n; i++) {\n q[p[i]] = i;\n }\n int count = 1;\n int max = 1;\n for (int i = 2; i <= n; i++) {\n if (q[i] > q[i - 1]) {\n count++;\n } else {\n count = 1;\n }\n if (count > max) {\n max = count;\n }\n }\n printf(\"%d\\n\", n - max);\n\n return 0;\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 n = it.next().unwrap().parse::().unwrap();\n let a = (0..n)\n .map(|_| it.next().unwrap().parse::().unwrap())\n .collect::>();\n let mut b = vec![0; n];\n for i in 0..n {\n b[a[i] - 1] = i;\n }\n let mut res = 0;\n let mut cnt = 0;\n let mut pos = 0;\n for &t in &b {\n cnt = if t >= pos { cnt + 1 } else { 1 };\n res = std::cmp::max(res, cnt);\n pos = t;\n }\n println!(\"{}\", n - res);\n}", "difficulty": "medium"} {"problem_id": "0654", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Snuke loves \"paper cutting\": he cuts out characters from a newspaper headline and rearranges them to form another string.

\n

He will receive a headline which contains one of the strings S_1,...,S_n tomorrow.\nHe is excited and already thinking of what string he will create.\nSince he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.

\n

Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains.\nIf there are multiple such strings, find the lexicographically smallest one among them.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq n \\leq 50
  • \n
  • 1 \\leq |S_i| \\leq 50 for every i = 1, ..., n.
  • \n
  • S_i consists of lowercase English letters (a - z) for every i = 1, ..., n.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n\nS_1\n...\nS_n\n
\n
\n
\n
\n
\n

Output

Print the lexicographically smallest string among the longest strings that satisfy the condition.\nIf the answer is an empty string, print an empty line.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\ncbaa\ndaacc\nacacac\n
\n
\n
\n
\n
\n

Sample Output 1

aac\n
\n

The strings that can be created from each of cbaa, daacc and acacac, are aa, aac, aca, caa and so forth.\nAmong them, aac, aca and caa are the longest, and the lexicographically smallest of these three is aac.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\na\naa\nb\n
\n
\n
\n
\n
\n

Sample Output 2

\n
\n

The answer is an empty string.

\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n char s[n][51];\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", s[i]);\n }\n\n int i = 0;\n int c[n][26];\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < 26; k++) {\n c[j][k] = 0;\n }\n }\n\n i = 0;\n while (i < n) {\n int j = 0;\n while ('a' <= s[i][j] && s[i][j] <= 'z') {\n switch (s[i][j]) {\n case 'a':\n c[i][0]++;\n break;\n case 'b':\n c[i][1]++;\n break;\n case 'c':\n c[i][2]++;\n break;\n case 'd':\n c[i][3]++;\n break;\n case 'e':\n c[i][4]++;\n break;\n case 'f':\n c[i][5]++;\n break;\n case 'g':\n c[i][6]++;\n break;\n case 'h':\n c[i][7]++;\n break;\n case 'i':\n c[i][8]++;\n break;\n case 'j':\n c[i][9]++;\n break;\n case 'k':\n c[i][10]++;\n break;\n case 'l':\n c[i][11]++;\n break;\n case 'm':\n c[i][12]++;\n break;\n case 'n':\n c[i][13]++;\n break;\n case 'o':\n c[i][14]++;\n break;\n case 'p':\n c[i][15]++;\n break;\n case 'q':\n c[i][16]++;\n break;\n case 'r':\n c[i][17]++;\n break;\n case 's':\n c[i][18]++;\n break;\n case 't':\n c[i][19]++;\n break;\n case 'u':\n c[i][20]++;\n break;\n case 'v':\n c[i][21]++;\n break;\n case 'w':\n c[i][22]++;\n break;\n case 'x':\n c[i][23]++;\n break;\n case 'y':\n c[i][24]++;\n break;\n case 'z':\n c[i][25]++;\n break;\n }\n j++;\n }\n i++;\n }\n\n int canuse[26];\n\n for (int j = 0; j < 26; j++) {\n canuse[j] = c[0][j];\n }\n for (int j = 1; j < n; j++) {\n for (int k = 0; k < 26; k++) {\n if (canuse[k] > c[j][k]) {\n canuse[k] = c[j][k];\n }\n }\n }\n\n for (int j = 0; j < 26; j++) {\n for (int k = 0; k < canuse[j]; k++) {\n printf(\"%c\", 'a' + j);\n }\n }\n\n printf(\"\\n\");\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: usize = s.trim().parse().unwrap();\n\n let mut h: HashMap = HashMap::new();\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let c: Vec = s.trim().chars().collect();\n for e in &c {\n let x = match h.get(e) {\n Some(x) => *x,\n None => 0,\n };\n h.insert(*e, x + 1);\n }\n\n for _ in 1..n {\n let mut m: HashMap = HashMap::new();\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let c: Vec = s.trim().chars().collect();\n for e in &c {\n let x = match m.get(e) {\n Some(x) => *x,\n None => 0,\n };\n m.insert(*e, x + 1);\n }\n for (k, v) in h.iter_mut() {\n let x = match m.get(k) {\n Some(x) => *x,\n None => 0,\n };\n *v = std::cmp::min(x, *v);\n }\n }\n let mut v: Vec = h.keys().copied().collect();\n v.sort();\n for k in &v {\n let x = match h.get(k) {\n Some(x) => *x,\n None => 0,\n };\n print!(\"{}\", std::iter::repeat_n(*k, x).collect::());\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "0655", "problem_description": "A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.", "c_code": "int solution(void) {\n int n;\n int maxindex = 0;\n int minindex = 0;\n int swaps = 0;\n scanf(\"%d\", &n);\n int soldiers[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &soldiers[i]);\n }\n int max = 0;\n int min = 101;\n if (n >= 2 && n <= 100) {\n for (int i = 0; i < n; i++) {\n if (soldiers[i] >= 1 && soldiers[i] <= 100) {\n if (soldiers[i] > max) {\n max = soldiers[i];\n maxindex = i;\n }\n if (soldiers[i] <= min) {\n min = soldiers[i];\n minindex = i;\n }\n }\n }\n if (maxindex >= minindex) {\n swaps = (maxindex - 1) + (n - minindex) - 1;\n } else {\n swaps = (maxindex - 1) + (n - minindex);\n }\n\n printf(\"%d\", swaps);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n\n io::stdin().read_line(&mut n).unwrap();\n\n let n: usize = n.trim().parse().unwrap();\n\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut maxima = -1;\n let mut minima = 101;\n\n for item in &words {\n if *item > maxima {\n maxima = *item;\n }\n if *item < minima {\n minima = *item;\n }\n }\n\n let mut answer = 0;\n\n for (idx, item) in words.clone().into_iter().enumerate() {\n if item == maxima {\n answer += idx;\n break;\n }\n }\n\n for (idx, item) in words.clone().into_iter().rev().enumerate() {\n if item == minima {\n answer += idx;\n break;\n }\n }\n\n if answer >= n {\n answer -= 1;\n }\n\n println!(\"{}\", answer);\n}", "difficulty": "hard"} {"problem_id": "0656", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

Find the minimum prime number greater than or equal to X.

\n
\n
\n
\n
\n

Notes

\n

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

\n

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 2 \\le X \\le 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

\n

Print the minimum prime number greater than or equal to X.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

20\n
\n
\n
\n
\n
\n

Sample Output 1

23\n
\n

The minimum prime number greater than or equal to 20 is 23.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

X itself can be a prime number.

\n
\n
\n
\n
\n
\n

Sample Input 3

99992\n
\n
\n
\n
\n
\n

Sample Output 3

100003\n
\n
\n
", "c_code": "int solution(void) {\n\n int X = 0;\n\n int i = 2;\n int j = 0;\n\n scanf(\"%d\", &X);\n\n for (j = 0; j < 1000000; j++) {\n while (X % i != 0) {\n i++;\n }\n if (X != i) {\n X++;\n i = 2;\n } else {\n break;\n }\n }\n\n printf(\"%d\\n\", i);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let x = stdin.lock().lines().next().unwrap().unwrap();\n let x = x.parse::().unwrap();\n\n for i in x.. {\n if (2..).take_while(|d| d * d <= i).all(|d| i % d != 0) {\n println!(\"{}\", i);\n return;\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0657", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Given are positive integers A and B.

\n

Let us choose some number of positive common divisors of A and B.

\n

Here, any two of the chosen divisors must be coprime.

\n

At most, how many divisors can we choose?

\n
Definition of common divisor
\n

An integer d is said to be a common divisor of integers x and y when d divides both x and y.

\n
\n
Definition of being coprime
\n

Integers x and y are said to be coprime when x and y have no positive common divisors other than 1.

\n
\n
Definition of dividing
\n

An integer x is said to divide another integer y when there exists an integer \\alpha such that y = \\alpha x.

\n
\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A, B \\leq 10^{12}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the maximum number of divisors that can be chosen to satisfy the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

12 18\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

12 and 18 have the following positive common divisors: 1, 2, 3, and 6.

\n

1 and 2 are coprime, 2 and 3 are coprime, and 3 and 1 are coprime, so we can choose 1, 2, and 3, which achieve the maximum result.

\n
\n
\n
\n
\n
\n

Sample Input 2

420 660\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 2019\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n

1 and 2019 have no positive common divisors other than 1.

\n
\n
", "c_code": "int solution(void) {\n long a;\n long b;\n scanf(\"%ld %ld\", &a, &b);\n\n long gcd;\n long m = a;\n long d = b;\n while (d > 0) {\n long temp = d;\n d = m % d;\n m = temp;\n }\n gcd = m;\n\n if (gcd == 1) {\n printf(\"%d\\n\", 1);\n return 0;\n }\n\n long count = 0;\n for (long i = 2; i <= sqrt(gcd); i++) {\n if (gcd % i == 0) {\n count++;\n while (gcd % i == 0) {\n gcd /= i;\n }\n }\n }\n\n if (gcd > 1) {\n count++;\n }\n\n printf(\"%ld\\n\", count + 1);\n return 0;\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 mut div: Vec = Vec::new();\n for i in 2..a {\n if i * i > a {\n break;\n }\n if a % i == 0 {\n if b % i == 0 {\n div.push(i)\n }\n if b % (a / i) == 0 {\n div.push(a / i)\n }\n }\n }\n if a > 1 && b % a == 0 {\n div.push(a);\n }\n div.sort();\n let mut res: Vec = Vec::new();\n for d in div {\n if res.iter().all(|x| d % x != 0) {\n res.push(d);\n }\n }\n println!(\"{}\", res.len() + 1);\n}", "difficulty": "medium"} {"problem_id": "0658", "problem_description": "A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes.Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg.The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather.", "c_code": "int solution() {\n int a;\n int b;\n int c;\n int n;\n int note[100001];\n int count = 0;\n\n scanf(\"%d%d%d%d\", &a, &b, &c, &n);\n for (a = 0; a < n; a++) {\n scanf(\"%d\", ¬e[a]);\n if (note[a] < c && note[a] > b) {\n count++;\n }\n }\n printf(\"%d\\n\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let (_, b, c): (usize, usize, usize) = {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let mut abc = line.split_whitespace().map(|s| s.parse().unwrap());\n (\n abc.next().unwrap(),\n abc.next().unwrap(),\n abc.next().unwrap(),\n )\n };\n\n {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n }\n\n let mut line = String::new();\n let x_iter = {\n stdin().read_line(&mut line).unwrap();\n line.split_whitespace().map(|s| s.parse::().unwrap())\n };\n\n println!(\"{}\", x_iter.filter(|&x| x > b && x < c).count());\n}", "difficulty": "medium"} {"problem_id": "0659", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

\n

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

\n
    \n
  • In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.
  • \n
\n

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

\n

You are given a positive integer K. Find the K-th smallest lunlun number.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq K \\leq 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
K\n
\n
\n
\n
\n
\n

Output

\n

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

15\n
\n
\n
\n
\n
\n

Sample Output 1

23\n
\n

We will list the 15 smallest lunlun numbers in ascending order:
\n1,\n2,\n3,\n4,\n5,\n6,\n7,\n8,\n9,\n10,\n11,\n12,\n21,\n22,\n23.
\nThus, the answer is 23.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

13\n
\n
\n
\n
\n
\n

Sample Output 3

21\n
\n
\n
\n
\n
\n
\n

Sample Input 4

100000\n
\n
\n
\n
\n
\n

Sample Output 4

3234566667\n
\n

Note that the answer may not fit into the 32-bit signed integer type.

\n
\n
", "c_code": "int solution(void) {\n unsigned long long int X[100000] = {0};\n unsigned long long int K;\n unsigned long long int count = 0;\n int l = -1;\n scanf(\"%llu\", &K);\n\n for (int i = 0; i < 9; i++) {\n X[i] = i + 1;\n }\n\n for (unsigned long long int j = 9; j < K; j++) {\n X[j] = X[count] * 10 + (X[count] % 10) + l;\n l++;\n\n if (X[count] % 10 == 0) {\n X[j]++;\n if (l == 1) {\n count++;\n l = -1;\n }\n }\n\n if (l == 1 && X[count] % 10 == 9) {\n l = -1;\n count++;\n }\n\n if (l == 2) {\n l = -1;\n count++;\n }\n }\n\n printf(\"%llu\", X[K - 1]);\n\n return 0;\n}", "rust_code": "fn solution() {\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 ans: Vec = Vec::new();\n for _ in 0..k {\n let mut i = 0;\n while i <= ans.len() {\n if i == ans.len() {\n ans.push(1);\n break;\n } else if ans[i] < 9 && (i + 1 == ans.len() || (ans[i] + 1 - ans[i + 1]).abs() <= 1) {\n ans[i] += 1;\n break;\n }\n i += 1;\n }\n for j in (0..i).rev() {\n ans[j] = max(0, ans[j + 1] - 1);\n }\n }\n\n let ans = ans\n .iter()\n .rev()\n .map(|x| x.to_string())\n .collect::>()\n .join(\"\");\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0660", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them.

\n

He may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if a_i=a_j (i≠j), he has to pay the cost separately for transforming each of them (See Sample 2).

\n

Find the minimum total cost to achieve his objective.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦N≦100
  • \n
  • -100≦a_i≦100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the minimum total cost to achieve Evi's objective.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n4 8\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

Transforming the both into 6s will cost (4-6)^2+(8-6)^2=8 dollars, which is the minimum.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 1 3\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

Transforming the all into 2s will cost (1-2)^2+(1-2)^2+(3-2)^2=3 dollars. Note that Evi has to pay (1-2)^2 dollar separately for transforming each of the two 1s.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n4 2 5\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n

Leaving the 4 as it is and transforming the 2 and the 5 into 4s will achieve the total cost of (2-4)^2+(5-4)^2=5 dollars, which is the minimum.

\n
\n
\n
\n
\n
\n

Sample Input 4

4\n-100 -100 -100 -100\n
\n
\n
\n
\n
\n

Sample Output 4

0\n
\n

Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is 0.

\n
\n
", "c_code": "int solution(void) {\n int n;\n int sum = 0;\n int low = 99999999;\n int a[105];\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n for (int j = -100; j <= 100; j++) {\n sum = 0;\n for (int k = 0; k < n; k++) {\n sum = sum + (a[k] - j) * (a[k] - j);\n }\n if (sum <= low) {\n low = sum;\n }\n }\n printf(\"%d\\n\", low);\n}", "rust_code": "fn solution() {\n input! {\n N: usize,\n a: [i64; N],\n }\n use std::cmp::min;\n let mut ans = 1 << 60;\n for x in -100..101 {\n let s = a.iter().map(|&y| (x - y) * (x - y)).sum::();\n ans = min(ans, s);\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0661", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

\n

Consider a sequence A satisfying the following conditions:

\n
    \n
  • A is a sequence of N positive integers.
  • \n
  • 1 \\leq A_1 \\leq A_2 \\le \\cdots \\leq A_N \\leq M.
  • \n
\n

Let us define a score of this sequence as follows:

\n
    \n
  • The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
  • \n
\n

Find the maximum possible score of A.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 ≤ N ≤ 10
  • \n
  • 1 \\leq M \\leq 10
  • \n
  • 1 \\leq Q \\leq 50
  • \n
  • 1 \\leq a_i < b_i \\leq N ( i = 1, 2, ..., Q )
  • \n
  • 0 \\leq c_i \\leq M - 1 ( i = 1, 2, ..., Q )
  • \n
  • (a_i, b_i, c_i) \\neq (a_j, b_j, c_j) (where i \\neq j)
  • \n
  • 1 \\leq d_i \\leq 10^5 ( i = 1, 2, ..., Q )
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M Q\na_1 b_1 c_1 d_1\n:\na_Q b_Q c_Q d_Q\n
\n
\n
\n
\n
\n

Output

Print the maximum possible score of A.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4 3\n1 3 3 100\n1 2 2 10\n2 3 2 10\n
\n
\n
\n
\n
\n

Sample Output 1

110\n
\n

When A = \\{1, 3, 4\\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 6 10\n2 4 1 86568\n1 4 0 90629\n2 3 0 90310\n3 4 1 29211\n3 4 3 78537\n3 4 2 8580\n1 2 1 96263\n1 4 2 2156\n1 2 0 94325\n1 4 3 94328\n
\n
\n
\n
\n
\n

Sample Output 2

357500\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 10 1\n1 10 9 1\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution(void) {\n\n int n;\n int m;\n int q;\n scanf(\"%d %d %d\", &n, &m, &q);\n int a[q];\n int b[q];\n int c[q];\n long d[q];\n for (long i = 0; i < q; i++) {\n scanf(\"%d %d %d %ld\", &a[i], &b[i], &c[i], &d[i]);\n }\n long max = 0;\n long score;\n int sequence[n];\n for (int i = 0; i < n; i++) {\n sequence[i] = 1;\n }\n while (1) {\n score = 0;\n for (int i = 0; i < q; i++) {\n if (sequence[b[i] - 1] - sequence[a[i] - 1] == c[i]) {\n score += d[i];\n }\n }\n if (score > max) {\n max = score;\n }\n if (sequence[1] == m) {\n break;\n }\n for (int i = n - 1; i > 0; i--) {\n if (sequence[i] < m) {\n sequence[i]++;\n for (int j = i + 1; j < n; j++) {\n sequence[j] = sequence[i];\n }\n break;\n }\n }\n }\n printf(\"%ld\\n\", max);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n m: usize,\n q: usize,\n v: [(usize1, usize1, usize, usize); q]\n }\n let mut ar = vec![0; n];\n let mut ans = 0;\n while ar[0] < m {\n let mut t = 0;\n for &(a, b, c, d) in &v {\n if ar[b] - ar[a] == c {\n t += d;\n }\n }\n ans = max(ans, t);\n ar[n - 1] += 1;\n let mut i = n - 1;\n while i > 0 {\n if ar[i] == m {\n i -= 1;\n ar[i] += 1;\n } else {\n break;\n }\n }\n while i < n - 1 {\n ar[i + 1] = ar[i];\n i += 1;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0662", "problem_description": "

Enumeration of Subsets II

\n\n\n

\n You are given a set $T$, which is a subset of $U$. The set $U$ consists of $0, 1, ... n-1$.\n\n Print all sets, each of which is a subset of $U$ and includes $T$ as a subset.\n\n Note that we represent $0, 1, ... n-1$ as 00...0001, 00...0010, 00...0100, ..., 10...0000 in binary respectively and the integer representation of a subset is calculated by bitwise OR of existing elements. \n

\n\n\n

Input

\n\n

\n The input is given in the following format.\n

\n\n
\n$n$\n$k \\; b_0 \\; b_1 \\; ... \\; b_{k-1}$\n
\n\n

\n$k$ is the number of elements in $T$, and $b_i$ represents elements in $T$.\n

\n\n\n\n

Output

\n\n

\n Print the subsets ordered by their decimal integers. Print a subset in the following format.\n

\n\n
\n$d$: $e_0$ $e_1$ ...\n
\n\n

\n Print ':' after the integer value $d$, then print elements $e_i$ in the subset in ascending order. Separate two adjacency elements by a space character.\n

\n\n\n

Constraints

\n
    \n
  • $1 \\leq n \\leq 18$
  • \n
  • $0 \\leq k \\leq n$
  • \n
  • $0 \\leq b_i < n$
  • \n
\n\n

Sample Input 1

\n\n
\n4\n2 0 2\n
\n\n

Sample Output 1

\n\n
\n5: 0 2\n7: 0 1 2\n13: 0 2 3\n15: 0 1 2 3\n
", "c_code": "int solution() {\n int n;\n int bit[19];\n int *t;\n int t_n;\n int x;\n int tmp;\n int size;\n int flag;\n int i;\n int j;\n int k;\n\n scanf(\"%d\", &n);\n\n scanf(\"%d\", &t_n);\n\n t = (int *)malloc(sizeof(int) * t_n);\n for (i = 0; i < t_n; i++) {\n scanf(\"%d\", &t[i]);\n }\n\n for (i = 0; i < pow(2, n); i++) {\n x = i;\n j = k = 0;\n while ((tmp = x / 2) >= 1) {\n if (x % 2) {\n bit[k++] = j;\n }\n x = tmp;\n j++;\n }\n if (x) {\n bit[k] = j;\n }\n\n size = k;\n\n if (i > 0) {\n flag = 1;\n for (j = 0; j < t_n; j++) {\n for (k = 0; k <= size; k++) {\n if (t[j] == bit[k]) {\n break;\n }\n }\n if (k == size + 1) {\n flag = 0;\n break;\n }\n }\n\n if (flag) {\n printf(\"%d:\", i);\n for (j = 0; j <= size; j++) {\n printf(\" %d\", bit[j]);\n }\n printf(\"\\n\");\n }\n } else if (i == 0 && t_n == 0) {\n printf(\"0:\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_line(&mut input);\n input.pop();\n\n let n: u32 = input.parse().unwrap();\n\n input.clear();\n stdin().read_line(&mut input);\n input.pop();\n\n let mut t = 0u32;\n let mut max = 0;\n for s in input.split(' ').skip(1) {\n let b = s.parse::().unwrap();\n t |= 1 << b;\n if max < b {\n max = b;\n }\n }\n\n let out = stdout();\n let mut out = BufWriter::new(out.lock());\n if t == 0 {\n writeln!(out, \"0:\");\n }\n\n for p in max..n {\n for d in (1 << p)..(2 << p) {\n if d & t == t {\n write!(out, \"{}:\", d);\n\n for e in 0..p + 1 {\n if d & 1 << e != 0 {\n write!(out, \" {}\", e);\n }\n }\n writeln!(out);\n }\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0663", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

\"Teishi-zushi\", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

\n

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

\n

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

\n

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 10^5
  • \n
  • 2 ≤ C ≤ 10^{14}
  • \n
  • 1 ≤ x_1 < x_2 < ... < x_N < C
  • \n
  • 1 ≤ v_i ≤ 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n

Subscores

    \n
  • 300 points will be awarded for passing the test set satisfying N ≤ 100.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N C\nx_1 v_1\nx_2 v_2\n:\nx_N v_N\n
\n
\n
\n
\n
\n

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 20\n2 80\n9 120\n16 1\n
\n
\n
\n
\n
\n

Sample Output 1

191\n
\n

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 20\n2 80\n9 1\n16 120\n
\n
\n
\n
\n
\n

Sample Output 2

192\n
\n

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 100000000000000\n50000000000000 1\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

\n
\n
\n
\n
\n
\n

Sample Input 4

15 10000000000\n400000000 1000000000\n800000000 1000000000\n1900000000 1000000000\n2400000000 1000000000\n2900000000 1000000000\n3300000000 1000000000\n3700000000 1000000000\n3800000000 1000000000\n4000000000 1000000000\n4100000000 1000000000\n5200000000 1000000000\n6600000000 1000000000\n8000000000 1000000000\n9300000000 1000000000\n9700000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 4

6500000000\n
\n

All these sample inputs above are included in the test set for the partial score.

\n
\n
", "c_code": "int solution() {\n int n;\n int i;\n int j;\n scanf(\"%d\", &n);\n long long c;\n long long max = 0;\n long long cal[(2 * n) + 2][2];\n scanf(\"%lld\", &c);\n cal[n][0] = 0;\n cal[n][1] = 0;\n for (i = 0; i < n; i++) {\n scanf(\"%lld%lld\", &cal[i + n + 1][0], &cal[i + n + 1][1]);\n cal[i][0] = c - cal[i + n + 1][0];\n cal[i][1] = cal[i + n + 1][1];\n }\n long long cal1[(2 * n) + 1];\n long long cal2[(2 * n) + 1];\n cal1[n] = 0;\n cal2[n] = 0;\n for (i = n + 1; i <= 2 * n; i++) {\n cal[i][1] += cal[i - 1][1];\n }\n for (i = n - 1; i >= 0; i--) {\n cal[i][1] += cal[i + 1][1];\n }\n for (i = n + 1; i <= 2 * n; i++) {\n cal1[i] = cal[i][1] - cal[i][0];\n if (cal1[i] < cal1[i - 1]) {\n cal1[i] = cal1[i - 1];\n }\n cal2[i] = cal[i][1] - 2 * cal[i][0];\n if (cal2[i] < cal2[i - 1]) {\n cal2[i] = cal2[i - 1];\n }\n }\n for (i = n - 1; i >= 0; i--) {\n cal1[i] = cal[i][1] - 2 * cal[i][0];\n if (cal1[i] < cal1[i + 1]) {\n cal1[i] = cal1[i + 1];\n }\n cal2[i] = cal[i][1] - cal[i][0];\n if (cal2[i] < cal2[i + 1]) {\n cal2[i] = cal2[i + 1];\n }\n }\n for (i = 0; i <= n; i++) {\n c = cal1[i] + cal1[n + i];\n if (max < c) {\n max = c;\n }\n c = cal2[i] + cal2[n + i];\n if (max < c) {\n max = c;\n }\n }\n printf(\"%lld\\n\", max);\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 c: i64 = itr.next().unwrap().parse().unwrap();\n let mut rx = vec![0i64; n];\n let mut rv = vec![0i64; n];\n for i in 0..n {\n rx[i] = itr.next().unwrap().parse().unwrap();\n rv[i] = itr.next().unwrap().parse().unwrap();\n }\n let mut lx = vec![0; n];\n let mut lv = rv.clone();\n let mut lmax = vec![0; n];\n let mut rmax = vec![0; n];\n lv.reverse();\n for i in 0..n {\n lx[i] = c - rx[n - 1 - i];\n }\n for i in 0..n - 1 {\n lv[i + 1] += lv[i];\n rv[i + 1] += rv[i];\n lmax[i + 1] = max(lmax[i], lv[i] - lx[i]);\n rmax[i + 1] = max(rmax[i], rv[i] - rx[i]);\n }\n\n let mut ans = 0i64;\n for i in 0..n {\n ans = max(ans, lv[i] - lx[i]);\n ans = max(ans, lv[i] - lx[i] * 2 + rmax[n - 1 - i]);\n }\n for i in 0..n {\n ans = max(ans, rv[i] - rx[i]);\n ans = max(ans, rv[i] - rx[i] * 2 + lmax[n - 1 - i]);\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0664", "problem_description": "Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$.", "c_code": "int solution() {\n int n = 0;\n int k = 0;\n scanf(\"%d\", &n);\n int rez[n];\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &k);\n rez[i] = k;\n }\n for (int i = 0; i < n; ++i) {\n int e = ((rez[i] - 1) / 4) + 1;\n for (int j = 0; j < rez[i] - e; ++j) {\n printf(\"9\");\n }\n for (int j = 0; j < e; ++j) {\n printf(\"8\");\n }\n printf(\"\\n\");\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 k = (n - 1) / 4 + 1;\n for _ in 0..(n - k) {\n print!(\"9\");\n }\n for _ in 0..k {\n print!(\"8\");\n }\n println!();\n }\n}", "difficulty": "easy"} {"problem_id": "0665", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are N children in AtCoder Kindergarten. Mr. Evi will arrange the children in a line, then give 1 candy to the first child in the line, 2 candies to the second child, ..., N candies to the N-th child. How many candies will be necessary in total?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦N≦100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the necessary number of candies in total.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

The answer is 1+2+3=6.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\n
\n
\n
\n
\n
\n

Sample Output 2

55\n
\n

The sum of the integers from 1 to 10 is 55.

\n
\n
\n
\n
\n
\n

Sample Input 3

1\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n

Only one child. The answer is 1 in this case.

\n
\n
", "c_code": "int solution() {\n int N = 0;\n scanf(\"%d\", &N);\n printf(\"%d\\n\", (int)(0.5 * N * (N + 1)));\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 n: usize = s.trim().parse().unwrap();\n println!(\"{}\", n * (n + 1) / 2);\n}", "difficulty": "easy"} {"problem_id": "0666", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Kenkoooo is planning a trip in Republic of Snuke.\nIn this country, there are n cities and m trains running.\nThe cities are numbered 1 through n, and the i-th train connects City u_i and v_i bidirectionally.\nAny city can be reached from any city by changing trains.

\n

Two currencies are used in the country: yen and snuuk.\nAny train fare can be paid by both yen and snuuk.\nThe fare of the i-th train is a_i yen if paid in yen, and b_i snuuk if paid in snuuk.

\n

In a city with a money exchange office, you can change 1 yen into 1 snuuk.\nHowever, when you do a money exchange, you have to change all your yen into snuuk.\nThat is, if Kenkoooo does a money exchange when he has X yen, he will then have X snuuk.\nCurrently, there is a money exchange office in every city, but the office in City i will shut down in i years and can never be used in and after that year.

\n

Kenkoooo is planning to depart City s with 10^{15} yen in his pocket and head for City t, and change his yen into snuuk in some city while traveling.\nIt is acceptable to do the exchange in City s or City t.

\n

Kenkoooo would like to have as much snuuk as possible when he reaches City t by making the optimal choices for the route to travel and the city to do the exchange.\nFor each i=0,...,n-1, find the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i years.\nYou can assume that the trip finishes within the year.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq n \\leq 10^5
  • \n
  • 1 \\leq m \\leq 10^5
  • \n
  • 1 \\leq s,t \\leq n
  • \n
  • s \\neq t
  • \n
  • 1 \\leq u_i < v_i \\leq n
  • \n
  • 1 \\leq a_i,b_i \\leq 10^9
  • \n
  • If i\\neq j, then u_i \\neq u_j or v_i \\neq v_j.
  • \n
  • Any city can be reached from any city by changing trains.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n m s t\nu_1 v_1 a_1 b_1\n:\nu_m v_m a_m b_m\n
\n
\n
\n
\n
\n

Output

Print n lines.\nIn the i-th line, print the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i-1 years.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3 2 3\n1 4 1 100\n1 2 1 10\n1 3 20 1\n
\n
\n
\n
\n
\n

Sample Output 1

999999999999998\n999999999999989\n999999999999979\n999999999999897\n
\n

After 0 years, it is optimal to do the exchange in City 1.
\nAfter 1 years, it is optimal to do the exchange in City 2.
\nNote that City 1 can still be visited even after the exchange office is closed.\nAlso note that, if it was allowed to keep 1 yen when do the exchange in City 2 and change the remaining yen into snuuk, we could reach City 3 with 999999999999998 snuuk, but this is NOT allowed.
\nAfter 2 years, it is optimal to do the exchange in City 3.
\nAfter 3 years, it is optimal to do the exchange in City 4.\nNote that the same train can be used multiple times.

\n
\n
\n
\n
\n
\n

Sample Input 2

8 12 3 8\n2 8 685087149 857180777\n6 7 298270585 209942236\n2 4 346080035 234079976\n2 5 131857300 22507157\n4 8 30723332 173476334\n2 6 480845267 448565596\n1 4 181424400 548830121\n4 5 57429995 195056405\n7 8 160277628 479932440\n1 6 475692952 203530153\n3 5 336869679 160714712\n2 7 389775999 199123879\n
\n
\n
\n
\n
\n

Sample Output 2

999999574976994\n999999574976994\n999999574976994\n999999574976994\n999999574976994\n999999574976994\n999999574976994\n999999574976994\n
\n
\n
", "c_code": "int solution(void) {\n long long n;\n long long p;\n long long s;\n long long t;\n long long count;\n scanf(\"%lld%lld%lld%lld\", &n, &p, &s, &t);\n bool visited[n + 1];\n\n long long mm = 0;\n long long head[(3 * p) + 1];\n long long next[(3 * p) + 1];\n long long to[(3 * p) + 1];\n long long leny[(3 * p) + 1];\n long long lens[(3 * p) + 1];\n for (int i = 0; i <= n; i++) {\n head[i] = -1;\n }\n for (long long i = 0; i < p; i++) {\n long long u;\n long long v;\n long long a;\n long long b;\n scanf(\"%lld%lld%lld%lld\", &u, &v, &a, &b);\n next[mm] = head[u];\n head[u] = mm;\n to[mm] = v;\n leny[mm] = a;\n lens[mm] = b;\n mm++;\n next[mm] = head[v];\n head[v] = mm;\n to[mm] = u;\n leny[mm] = a;\n lens[mm] = b;\n mm++;\n }\n\n long long countans = 0;\n count = 0;\n queue_t q[n * 50];\n queue_t tmpq;\n long long d[n + 1];\n for (long long i = 0; i <= n; i++) {\n visited[i] = false;\n d[i] = 1e17;\n q[i].q = d[i];\n q[i].i = i;\n count++;\n }\n d[s] = 0;\n q[0].q = -1e17;\n q[count].q = d[s];\n q[count].i = s;\n for (long long i = count; i > 0; i = i / 2) {\n if (i / 2 != 0 && q[i].q <= q[i / 2].q) {\n tmpq = q[i];\n q[i] = q[i / 2];\n q[i / 2] = tmpq;\n } else {\n break;\n }\n }\n for (long long i = 0; i <= n; i++) {\n long long now = q[1].i;\n if (visited[now] == false) {\n visited[now] = true;\n countans++;\n }\n\n q[1] = q[count];\n long long bf = 1;\n while (1) {\n if (bf * 2 > count) {\n break;\n }\n if (q[bf * 2].q <= q[(bf * 2) + 1].q) {\n if (q[bf].q > q[bf * 2].q) {\n tmpq = q[bf];\n q[bf] = q[bf * 2];\n q[bf * 2] = tmpq;\n bf = bf * 2;\n } else {\n break;\n }\n } else {\n if (q[bf].q > q[(bf * 2) + 1].q) {\n tmpq = q[bf];\n q[bf] = q[(bf * 2) + 1];\n q[(bf * 2) + 1] = tmpq;\n bf = bf * 2 + 1;\n } else {\n break;\n }\n }\n }\n count--;\n long long tmp;\n for (long long e = head[now]; e != -1; e = next[e]) {\n tmp = d[now] + leny[e];\n if (d[to[e]] > tmp) {\n d[to[e]] = tmp;\n count++;\n q[count].q = tmp;\n q[count].i = to[e];\n for (long long af = count; af > 0; af = af / 2) {\n if (af / 2 != 0 && q[af].q <= q[af / 2].q) {\n tmpq = q[af];\n q[af] = q[af / 2];\n q[af / 2] = tmpq;\n } else {\n break;\n }\n }\n }\n }\n if (countans == n) {\n break;\n }\n }\n\n countans = 0;\n count = 0;\n queue_t q2[n * 50];\n long long f[n + 1];\n for (long long i = 0; i <= n; i++) {\n visited[i] = false;\n f[i] = 1e17;\n q2[i].q = f[i];\n q2[i].i = i;\n count++;\n }\n f[t] = 0;\n q2[0].q = -1e17;\n q2[count].q = f[t];\n q2[count].i = t;\n for (long long i = count; i > 0; i = i / 2) {\n if (i / 2 != 0 && q2[i].q <= q2[i / 2].q) {\n tmpq = q2[i];\n q2[i] = q2[i / 2];\n q2[i / 2] = tmpq;\n } else {\n break;\n }\n }\n for (long long i = 0; i <= n; i++) {\n int now = q2[1].i;\n\n if (visited[now] == false) {\n visited[now] = true;\n countans++;\n }\n\n q2[1] = q2[count];\n long long bf = 1;\n while (1) {\n if (bf * 2 >= count) {\n break;\n }\n if (q2[bf * 2].q <= q2[(bf * 2) + 1].q) {\n if (q2[bf].q > q2[bf * 2].q) {\n tmpq = q2[bf];\n q2[bf] = q2[bf * 2];\n q2[bf * 2] = tmpq;\n bf = bf * 2;\n } else {\n break;\n }\n } else {\n if (q2[bf].q > q2[(bf * 2) + 1].q) {\n tmpq = q2[bf];\n q2[bf] = q2[(bf * 2) + 1];\n q2[(bf * 2) + 1] = tmpq;\n bf = bf * 2 + 1;\n } else {\n break;\n }\n }\n }\n count--;\n long long tmp;\n for (long long e = head[now]; e != -1; e = next[e]) {\n tmp = f[now] + lens[e];\n if (f[to[e]] > tmp) {\n f[to[e]] = tmp;\n count++;\n q2[count].q = tmp;\n q2[count].i = to[e];\n for (long long af = count; af > 0; af = af / 2) {\n if (af / 2 != 0 && q2[af].q <= q2[af / 2].q) {\n tmpq = q2[af];\n q2[af] = q2[af / 2];\n q2[af / 2] = tmpq;\n } else {\n break;\n }\n }\n }\n }\n if (countans == n) {\n break;\n }\n }\n\n long long ans[n + 1];\n ans[0] = 0;\n for (int i = 1; i <= n; i++) {\n ans[i] = d[i] + f[i];\n }\n for (int i = n; i > 1; i--) {\n if (ans[i] < ans[i - 1]) {\n ans[i - 1] = ans[i];\n }\n }\n for (int i = 1; i <= n; i++) {\n printf(\"%lld\\n\", 1000000000000000 - ans[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut nmst = String::new();\n std::io::stdin().read_line(&mut nmst).unwrap();\n let nmst: Vec = nmst\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let n = nmst[0];\n let m = nmst[1];\n let s = nmst[2] - 1;\n let t = nmst[3] - 1;\n let mut g: Vec> = vec![vec![]; n];\n for _ in 0..m {\n let mut uvab = String::new();\n std::io::stdin().read_line(&mut uvab).unwrap();\n let uvab: Vec = uvab\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let u = uvab[0] - 1;\n let v = uvab[1] - 1;\n let a = uvab[2];\n let b = uvab[3];\n g[u].push((v, a, b));\n g[v].push((u, a, b));\n }\n let f = 1e15 as usize;\n let mut e = vec![f; n];\n let mut q = std::collections::BinaryHeap::new();\n q.push((f, n, t));\n while let Some((y, c, v)) = q.pop() {\n if e[v] <= f - y {\n continue;\n }\n e[v] = f - y;\n for &(u, _, b) in &g[v] {\n if e[u] >= f - (y - b) {\n q.push((y - b, c, u));\n }\n }\n }\n let mut d = vec![0; n];\n q.push((f, n, s));\n let mut r = vec![];\n r.reserve(n);\n while let Some((y, c, v)) = q.pop() {\n if c == n {\n if d[v] >= y {\n continue;\n }\n d[v] = y;\n if r.len() <= v {\n q.push((y - e[v], v, t));\n }\n for &(u, a, _) in &g[v] {\n if d[u] < y - a {\n q.push((y - a, c, u));\n }\n }\n } else {\n if r.len() <= c {\n r.resize(c + 1, y);\n }\n }\n }\n for y in r {\n println!(\"{}\", y);\n }\n}", "difficulty": "hard"} {"problem_id": "0667", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.\nG consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.

\n

First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.

\n

Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 0 \\leq M \\leq \\min(10^5, N (N-1))
  • \n
  • 1 \\leq u_i, v_i \\leq N(1 \\leq i \\leq M)
  • \n
  • u_i \\neq v_i (1 \\leq i \\leq M)
  • \n
  • If i \\neq j, (u_i, v_i) \\neq (u_j, v_j).
  • \n
  • 1 \\leq S, T \\leq N
  • \n
  • S \\neq T
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nu_1 v_1\n:\nu_M v_M\nS T\n
\n
\n
\n
\n
\n

Output

If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.\nIf he can, print the minimum number of ken-ken-pa needed to reach vertex T.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 4\n1 2\n2 3\n3 4\n4 1\n1 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Ken can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \\rightarrow 2 \\rightarrow 3 \\rightarrow 4 in the first ken-ken-pa, then 4 \\rightarrow 1 \\rightarrow 2 \\rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3\n1 2\n2 3\n3 1\n1 2\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

Any number of ken-ken-pa will bring Ken back to Vertex 1, so he cannot reach Vertex 2, though he can pass through it in the middle of a ken-ken-pa.

\n
\n
\n
\n
\n
\n

Sample Input 3

2 0\n1 2\n
\n
\n
\n
\n
\n

Sample Output 3

-1\n
\n

Vertex S and Vertex T may be disconnected.

\n
\n
\n
\n
\n
\n

Sample Input 4

6 8\n1 2\n2 3\n3 4\n4 5\n5 1\n1 4\n1 5\n4 6\n1 6\n
\n
\n
\n
\n
\n

Sample Output 4

2\n
\n
\n
", "c_code": "int solution() {\n int i;\n int u;\n int w;\n int N;\n int M;\n int S;\n int T;\n scanf(\"%d %d\", &N, &M);\n list **adj = (list **)malloc(sizeof(list *) * (N + 1) * 3);\n list *d = (list *)malloc(sizeof(list) * M * 3);\n for (i = 1; i <= N * 3; i++) {\n adj[i] = NULL;\n }\n for (i = 0; i < M; i++) {\n scanf(\"%d %d\", &u, &w);\n d[i * 3].v = w + N;\n d[i * 3].next = adj[u];\n adj[u] = &(d[i * 3]);\n d[(i * 3) + 1].v = w + N * 2;\n d[(i * 3) + 1].next = adj[u + N];\n adj[u + N] = &(d[(i * 3) + 1]);\n d[(i * 3) + 2].v = w;\n d[(i * 3) + 2].next = adj[u + (N * 2)];\n adj[u + (N * 2)] = &(d[(i * 3) + 2]);\n }\n scanf(\"%d %d\", &S, &T);\n\n int dist[300001] = {};\n int q[300001];\n int head;\n int tail;\n list *p;\n dist[S] = 1;\n q[0] = S;\n for (head = 0, tail = 1; head < tail; head++) {\n u = q[head];\n for (p = adj[u]; p != NULL; p = p->next) {\n if (dist[p->v] == 0) {\n dist[p->v] = dist[u] + 1;\n q[tail++] = p->v;\n }\n }\n }\n\n printf(\"%d\\n\", ((dist[T] + 2) / 3) - 1);\n fflush(stdout);\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 m: usize = itr.next().unwrap().parse().unwrap();\n let mut g = vec![Vec::new(); n];\n for _ in 0..m {\n let u = itr.next().unwrap().parse::().unwrap() - 1;\n let v = itr.next().unwrap().parse::().unwrap() - 1;\n g[u].push(v);\n }\n let s: usize = itr.next().unwrap().parse::().unwrap() - 1;\n let t: usize = itr.next().unwrap().parse::().unwrap() - 1;\n\n let mut dp = vec![vec![1 << 30; 5]; n + 1];\n dp[s][0] = 0;\n\n let mut q = std::collections::VecDeque::new();\n q.push_back((s, 0));\n while let Some((v, p)) = q.pop_front() {\n for &nv in g[v].iter() {\n if dp[nv][(p + 1) % 3] == (1 << 30) {\n dp[nv][(p + 1) % 3] = min(dp[nv][(p + 1) % 3], dp[v][p] + 1);\n q.push_back((nv, (p + 1) % 3));\n }\n }\n }\n\n if dp[t][0] == 1 << 30 {\n println!(\"-1\")\n } else {\n println!(\"{}\", dp[t][0] / 3);\n }\n}", "difficulty": "medium"} {"problem_id": "0668", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Mr. Takahashi has a string s consisting of lowercase English letters.\nHe repeats the following operation on s exactly K times.

\n
    \n
  • Choose an arbitrary letter on s and change that letter to the next alphabet. Note that the next letter of z is a.
  • \n
\n

For example, if you perform an operation for the second letter on aaz, aaz becomes abz.\nIf you then perform an operation for the third letter on abz, abz becomes aba.

\n

Mr. Takahashi wants to have the lexicographically smallest string after performing exactly K operations on s.\nFind the such string.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤|s|≤10^5
  • \n
  • All letters in s are lowercase English letters.
  • \n
  • 1≤K≤10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
s\nK\n
\n
\n
\n
\n
\n

Output

Print the lexicographically smallest string after performing exactly K operations on s.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

xyz\n4\n
\n
\n
\n
\n
\n

Sample Output 1

aya\n
\n

For example, you can perform the following operations: xyz, yyz, zyz, ayz, aya.

\n
\n
\n
\n
\n
\n

Sample Input 2

a\n25\n
\n
\n
\n
\n
\n

Sample Output 2

z\n
\n

You have to perform exactly K operations.

\n
\n
\n
\n
\n
\n

Sample Input 3

codefestival\n100\n
\n
\n
\n
\n
\n

Sample Output 3

aaaafeaaivap\n
\n
\n
", "c_code": "int solution() {\n static char S[100005];\n static char *p;\n static int K;\n static int res;\n static int t;\n fgets(S, 100005, stdin);\n scanf(\"%d\", &K);\n p = S;\n while (*(p + 2)) {\n if (*p - 'a') {\n t = '{' - *p;\n if (K >= t) {\n K -= t;\n res += t;\n *p = 'a';\n }\n }\n ++p;\n }\n *p = (*p - 'a' + K) % 26 + 'a';\n *++p ^= 10;\n puts(S);\n}", "rust_code": "fn solution() {\n input! {\n s: chars,\n mut k: u64,\n }\n let mut ans = \"\".to_string();\n for i in 0..s.len() - 1 {\n let acc = (s[i] as u8 - b'a') as u64;\n if k / (26u64 - acc) > 0 && acc != 0 {\n ans += &'a'.to_string();\n k -= 26 - acc;\n } else {\n ans += &((acc as u8 + b'a') as char).to_string();\n }\n }\n k %= 26;\n let acc = ((s[s.len() - 1] as u8 - b'a') + k as u8) % 26;\n ans += &((acc + b'a') as char).to_string();\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0669", "problem_description": "You're given an array $$$a$$$ of length $$$2n$$$. Is it possible to reorder it in such way so that the sum of the first $$$n$$$ elements isn't equal to the sum of the last $$$n$$$ elements?", "c_code": "int solution() {\n int n;\n int a[2000];\n scanf(\"%d\", &n);\n for (int i = 0; i < 2 * n; ++i) {\n scanf(\"%d\", &a[i]);\n }\n int sumA = 0;\n int sumB = 0;\n for (int i = 0; i < n; ++i) {\n sumA += a[i];\n }\n for (int i = n; i < 2 * n; ++i) {\n sumB += a[i];\n }\n if (sumA == sumB) {\n for (int i = 0; i < n; ++i) {\n for (int j = n; j < 2 * n; ++j) {\n if (a[i] != a[j]) {\n int temp = a[i];\n a[i] = a[j];\n a[j] = temp;\n for (int k = 0; k < 2 * n; ++k) {\n printf(\"%d \", a[k]);\n }\n return 0;\n }\n }\n }\n puts(\"-1\");\n } else {\n for (int i = 0; i < 2 * n; ++i) {\n printf(\"%d \", a[i]);\n }\n }\n return 0;\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\n let n = get!(usize);\n let mut vs = vec![];\n for _ in 0..n * 2 {\n vs.push(get!());\n }\n vs.sort();\n let sum1: i64 = vs[0..n].iter().sum();\n let sum2: i64 = vs[n..n * 2].iter().sum();\n if sum1 == sum2 {\n println!(\"-1\");\n } else {\n for i in 0..vs.len() {\n if i > 0 {\n print!(\" \");\n }\n print!(\"{}\", vs[i]);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0670", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N participants in the CODE FESTIVAL 2016 Qualification contests. The participants are either students in Japan, students from overseas, or neither of these.

\n

Only Japanese students or overseas students can pass the Qualification contests. The students pass when they satisfy the conditions listed below, from the top rank down. Participants who are not students cannot pass the Qualification contests.

\n
    \n
  • A Japanese student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B.
  • \n
  • An overseas student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B and the student ranks B-th or above among all overseas students.
  • \n
\n

A string S is assigned indicating attributes of all participants. If the i-th character of string S is a, this means the participant ranked i-th in the Qualification contests is a Japanese student; b means the participant ranked i-th is an overseas student; and c means the participant ranked i-th is neither of these.

\n

Write a program that outputs for all the participants in descending rank either Yes if they passed the Qualification contests or No if they did not pass.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦N,A,B≦100000
  • \n
  • A+B≦N
  • \n
  • S is N characters long.
  • \n
  • S consists only of the letters a, b and c.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Inputs are provided from Standard Input in the following form.

\n
N A B\nS\n
\n
\n
\n
\n
\n

Output

Output N lines. On the i-th line, output Yes if the i-th participant passed the Qualification contests or No if that participant did not pass.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10 2 3\nabccabaabb\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\nYes\nNo\nNo\nYes\nYes\nYes\nNo\nNo\nNo\n
\n

The first, second, fifth, sixth, and seventh participants pass the Qualification contests.

\n
\n
\n
\n
\n
\n

Sample Input 2

12 5 2\ncabbabaacaba\n
\n
\n
\n
\n
\n

Sample Output 2

No\nYes\nYes\nYes\nYes\nNo\nYes\nYes\nNo\nYes\nNo\nNo\n
\n

The sixth participant is third among overseas students and thus does not pass the Qualification contests.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 2 2\nccccc\n
\n
\n
\n
\n
\n

Sample Output 3

No\nNo\nNo\nNo\nNo\n
\n
\n
", "c_code": "int solution() {\n int N;\n int B;\n int A;\n scanf(\"%d%d%d\", &N, &A, &B);\n char S[N];\n scanf(\"%s\", S);\n\n int counta = 0;\n int countb = 1;\n\n for (int i = 0; i < N; i++) {\n if (S[i] == 'a' && A + B > counta) {\n\n counta++;\n printf(\"Yes\\n\");\n\n } else if (S[i] == 'b' && A + B > counta && countb <= B) {\n\n countb++;\n counta++;\n printf(\"Yes\\n\");\n\n }\n\n else {\n printf(\"No\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (_N, A, B): (usize, usize, usize) = {\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 S: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n };\n\n let mut c = 0;\n let mut d = 0;\n for &s in &S {\n if s == 'a' && c < A + B {\n c += 1;\n println!(\"Yes\");\n } else if s == 'b' && c < A + B && d < B {\n c += 1;\n d += 1;\n println!(\"Yes\")\n } else {\n println!(\"No\")\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0671", "problem_description": "You are given a sequence $$$a_1, a_2, \\dots, a_n$$$ consisting of $$$n$$$ pairwise distinct positive integers.Find $$$\\left\\lfloor \\frac n 2 \\right\\rfloor$$$ different pairs of integers $$$x$$$ and $$$y$$$ such that: $$$x \\neq y$$$; $$$x$$$ and $$$y$$$ appear in $$$a$$$; $$$x~mod~y$$$ doesn't appear in $$$a$$$. Note that some $$$x$$$ or $$$y$$$ can belong to multiple pairs.$$$\\lfloor x \\rfloor$$$ denotes the floor function — the largest integer less than or equal to $$$x$$$. $$$x~mod~y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$.If there are multiple solutions, print any of them. It can be shown that at least one solution always exists.", "c_code": "int solution() {\n int t = 1;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int min = 10000000;\n int p = -1;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] < min) {\n min = a[i];\n p = i;\n }\n }\n int x = 0;\n for (int i = 0; i < n / 2; i++) {\n if (x == p) {\n x++;\n }\n printf(\"%d %d\\n\", a[x], a[p]);\n x++;\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 for l in i.lock().lines().skip(2).step_by(2) {\n let mut v = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>();\n v.sort_unstable();\n for j in 1..=v.len() / 2 {\n writeln!(o, \"{} {}\", v[j], v[0]).ok();\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0672", "problem_description": "

Spreadsheet


\n\n

\n Your task is to perform a simple table calculation.\n

\n\n

\n Write a program which reads the number of rows r, columns c and a table of r × c elements, and prints a new table, which includes the total sum for each row and column. \n

\n\n

Input

\n

\n In the first line, two integers r and c are given. Next, the table is given by r lines, each of which consists of c integers separated by space characters.\n

\n\n

Output

\n\n

\n Print the new table of (r+1) × (c+1) elements. Put a single space character between adjacent elements. For each row, print the sum of it's elements in the last column. For each column, print the sum of it's elements in the last row. Print the total sum of the elements at the bottom right corner of the table.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ r, c ≤ 100
  • \n
  • 0 ≤ an element of the table ≤ 100
  • \n
\n\n\n

Sample Input

\n\n
\n4 5\n1 1 3 4 5\n2 2 2 4 5\n3 3 0 1 1\n2 3 4 4 6\n
\n\n

Sample Output

\n\n
\n1 1 3 4 5 14\n2 2 2 4 5 15\n3 3 0 1 1 8\n2 3 4 4 6 19\n8 9 9 13 17 56\n
", "c_code": "int solution() {\n int tate = 0;\n int yoko = 0;\n int a = 0;\n int b = 0;\n int d = 0;\n int c[100][100] = {0};\n int f[100] = {0};\n scanf(\"%d %d\", &tate, &yoko);\n if (tate < 1 || yoko > 100) {\n return 1;\n }\n for (a = 0; a < tate; a++) {\n for (b = 0; b < yoko; b++) {\n\n scanf(\"%d\", &c[a][b]);\n }\n }\n for (a = 0; a < tate; a++) {\n d = 0;\n for (b = 0; b < yoko; b++) {\n printf(\"%d \", c[a][b]);\n d += c[a][b];\n f[b] += c[a][b];\n }\n printf(\"%d\\n\", d);\n }\n d = 0;\n for (a = 0; a < yoko; a++) {\n d += f[a];\n printf(\"%d \", f[a]);\n }\n printf(\"%d\\n\", d);\n\n scanf(\"%d\", &a);\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\n let mut ite1 = buf.split_whitespace().map(|x| x.parse::().unwrap());\n\n let _r = ite1.next().unwrap();\n let c = ite1.next().unwrap();\n\n let sums = &mut vec![0; c + 1];\n\n for line in stdin.lock().lines() {\n let line = line.unwrap();\n\n let vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect::>();\n\n for i in 0..c {\n sums[i] += vec[i];\n print!(\"{} \", vec[i]);\n }\n\n let sum = vec.iter().sum::();\n sums[c] += sum;\n println!(\"{}\", sum);\n }\n\n println!(\n \"{}\",\n sums.iter()\n .map(|x| x.to_string())\n .collect::>()\n .join(\" \")\n );\n}", "difficulty": "hard"} {"problem_id": "0673", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.\nYou are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

\n

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

\n

You can repeat the following operation any number of times:

\n
    \n
  • Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.
  • \n
\n

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

\n

Determine whether this is possible.

\n
\n
\n
\n
\n

Constraints

    \n
  • 4 \\leq N \\leq 200\\ 000
  • \n
  • S is a string of length N consisting of . and #.
  • \n
  • 1 \\leq A, B, C, D \\leq N
  • \n
  • Square A, B, C and D do not contain a rock.
  • \n
  • A, B, C and D are all different.
  • \n
  • A < B
  • \n
  • A < C
  • \n
  • B < D
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B C D\nS\n
\n
\n
\n
\n
\n

Output

Print Yes if the objective is achievable, and No if it is not.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7 1 3 6 7\n.#..#..\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

\n
A#B.#..\n\nA#.B#..\n\n.#AB#..\n\n.#A.#B.\n\n.#.A#B.\n\n.#.A#.B\n\n.#..#AB\n
\n
\n
\n
\n
\n
\n

Sample Input 2

7 1 3 7 6\n.#..#..\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

15 1 3 15 13\n...#.#...#.#...\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int a;\n int b;\n int c;\n int d;\n scanf(\"%d%d%d%d%d\", &n, &a, &b, &c, &d);\n char s[n + 5];\n scanf(\"%s\", s);\n s[n] = '#';\n s[n + 1] = '#';\n s[a - 1] = 'A';\n s[b - 1] = 'B';\n bool ac = false, bd = false, change = false;\n while (1) {\n change = false;\n if (s[a] == '.' && ac == false) {\n s[a - 1] = '.';\n a++;\n s[a - 1] = 'A';\n change = true;\n } else if (s[a + 1] == '.' && ac == false) {\n s[a - 1] = '.';\n a += 2;\n s[a - 1] = 'A';\n change = true;\n } else if (s[b] == '.' && bd == false) {\n s[b - 1] = '.';\n b++;\n s[b - 1] = 'B';\n change = true;\n } else if (s[b + 1] == '.' && bd == false) {\n s[b - 1] = '.';\n b += 2;\n s[b - 1] = 'B';\n change = true;\n }\n if (a == c) {\n ac = true;\n }\n if (b == d) {\n bd = true;\n }\n if (ac == true && bd == true) {\n printf(\"Yes\\n\");\n break;\n }\n if (change == false) {\n printf(\"No\\n\");\n break;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n a: usize1,\n b: usize1,\n c: usize1,\n d: usize1,\n s: chars,\n }\n let mut ans = true;\n for i in 0..n {\n if a < i && (i < c || i < d) && s[i] == '#' && s[i + 1] == '#' {\n ans = false;\n }\n }\n if d < c {\n let mut between = false;\n for i in b..d + 1 {\n if s[i - 1] == '.' && s[i] == '.' && s[i + 1] == '.' {\n between = true;\n }\n }\n if !between {\n ans = false;\n }\n }\n println!(\"{}\", if ans { \"Yes\" } else { \"No\" })\n}", "difficulty": "easy"} {"problem_id": "0674", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi has decided to distribute N AtCoder Crackers to K users of as evenly as possible.\nWhen all the crackers are distributed, find the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N,K \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

Print the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7 3\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

When the users receive two, two and three crackers, respectively, the (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user, is 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

100 10\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

The crackers can be distributed evenly.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 1\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n int K;\n int min = 0;\n int max = 0;\n\n scanf(\"%d %d\", &N, &K);\n while (N >= K) {\n N -= K;\n min++;\n }\n if (N == 0) {\n max = min;\n } else {\n max = min + 1;\n }\n printf(\"%d\", max - min);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut s = String::new();\n stdin.read_line(&mut s).ok();\n let iter = s.split_whitespace();\n let mut numbers = Vec::new();\n for arg in iter {\n numbers.push(u64::from_str(arg).expect(\"e\"));\n }\n let n: u64 = numbers[0];\n let k: u64 = numbers[1];\n if n.is_multiple_of(k) {\n println!(\"0\");\n } else {\n println!(\"1\");\n }\n}", "difficulty": "hard"} {"problem_id": "0675", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are given a string S of length N consisting of ( and ). Your task is to insert some number of ( and ) into S to obtain a correct bracket sequence.
\nHere, a correct bracket sequence is defined as follows:

\n
    \n
  • () is a correct bracket sequence.
  • \n
  • If X is a correct bracket sequence, the concatenation of (, X and ) in this order is also a correct bracket sequence.
  • \n
  • If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence.
  • \n
  • Every correct bracket sequence can be derived from the rules above.
  • \n
\n

Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.

\n
\n
\n
\n
\n

Constraints

    \n
  • The length of S is N.
  • \n
  • 1 ≤ N ≤ 100
  • \n
  • S consists of ( and ).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of ( and ) into S.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n())\n
\n
\n
\n
\n
\n

Sample Output 1

(())\n
\n
\n
\n
\n
\n
\n

Sample Input 2

6\n)))())\n
\n
\n
\n
\n
\n

Sample Output 2

(((()))())\n
\n
\n
\n
\n
\n
\n

Sample Input 3

8\n))))((((\n
\n
\n
\n
\n
\n

Sample Output 3

(((())))(((())))\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int i = 0;\n char buffer[256];\n int count = 0;\n int left = 0;\n\n scanf(\"%d\", &N);\n scanf(\"%s\", buffer);\n\n for (i = 0; i < N; i++) {\n if (buffer[i] == '(') {\n count++;\n } else if (buffer[i] == ')') {\n count--;\n if (count < 0) {\n left++;\n count++;\n }\n }\n }\n\n for (i = 0; i < left; i++) {\n printf(\"(\");\n }\n\n printf(\"%s\", buffer);\n\n for (i = 0; i < count; i++) {\n printf(\")\");\n }\n\n printf(\"\\n\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n let mut line = String::new();\n let _ = io::stdin().read_line(&mut n);\n let _ = io::stdin().read_line(&mut line);\n line = line.split_whitespace().collect();\n let mut openCnt = line.match_indices(\"(\").count();\n let mut closeCnt = line.match_indices(\")\").count();\n let mut misMatch = 0;\n let mut matchCnt = 0;\n for c in line.chars() {\n if c == '(' {\n misMatch += 1;\n } else if misMatch > 0 {\n misMatch -= 1;\n matchCnt += 1;\n }\n }\n openCnt -= matchCnt;\n closeCnt -= matchCnt;\n for _ in 0..closeCnt {\n line = \"(\".to_string() + line.as_str();\n }\n for _ in 0..openCnt {\n line.push(')');\n }\n println!(\"{}\", line);\n}", "difficulty": "easy"} {"problem_id": "0676", "problem_description": "You are given the strings $$$a$$$ and $$$b$$$, consisting of lowercase Latin letters. You can do any number of the following operations in any order: if $$$|a| > 0$$$ (the length of the string $$$a$$$ is greater than zero), delete the first character of the string $$$a$$$, that is, replace $$$a$$$ with $$$a_2 a_3 \\ldots a_n$$$; if $$$|a| > 0$$$, delete the last character of the string $$$a$$$, that is, replace $$$a$$$ with $$$a_1 a_2 \\ldots a_{n-1}$$$; if $$$|b| > 0$$$ (the length of the string $$$b$$$ is greater than zero), delete the first character of the string $$$b$$$, that is, replace $$$b$$$ with $$$b_2 b_3 \\ldots b_n$$$; if $$$|b| > 0$$$, delete the last character of the string $$$b$$$, that is, replace $$$b$$$ with $$$b_1 b_2 \\ldots b_{n-1}$$$. Note that after each of the operations, the string $$$a$$$ or $$$b$$$ may become empty.For example, if $$$a=$$$\"hello\" and $$$b=$$$\"icpc\", then you can apply the following sequence of operations: delete the first character of the string $$$a$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ello\" and $$$b=$$$\"icpc\"; delete the first character of the string $$$b$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ello\" and $$$b=$$$\"cpc\"; delete the first character of the string $$$b$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ello\" and $$$b=$$$\"pc\"; delete the last character of the string $$$a$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ell\" and $$$b=$$$\"pc\"; delete the last character of the string $$$b$$$ $$$\\Rightarrow$$$ $$$a=$$$\"ell\" and $$$b=$$$\"p\". For the given strings $$$a$$$ and $$$b$$$, find the minimum number of operations for which you can make the strings $$$a$$$ and $$$b$$$ equal. Note that empty strings are also equal.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n getchar();\n for (int j = 1; j <= t; j++) {\n int a = 0;\n int b = 0;\n int max = 0;\n int count = 0;\n char s1[20];\n char s2[20];\n char c = getchar();\n while (c != '\\n' && c != -1) {\n s1[a++] = c;\n c = getchar();\n }\n c = getchar();\n while (c != '\\n' && c != -1) {\n s2[b++] = c;\n c = getchar();\n }\n for (int k = 0; k < a; k++) {\n for (int l = 0; l < b; l++) {\n if (s1[k] == s2[l]) {\n int x = k;\n int y = l;\n while (x < a && y < b && s1[x] == s2[y]) {\n count++;\n x++;\n y++;\n }\n if (count >= max) {\n max = count;\n }\n count = 0;\n }\n }\n }\n\n printf(\"%d\\n\", a + b - (2 * max));\n }\n return 0;\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 a: Vec = sc.next::().chars().collect();\n let b: Vec = sc.next::().chars().collect();\n let mut sub_strings: HashSet = HashSet::new();\n for i in 0..a.len() {\n for j in i..a.len() {\n sub_strings.insert((i..=j).map(|i| a[i]).collect());\n }\n }\n let mut ans: usize = 0;\n for i in 0..b.len() {\n for j in i..b.len() {\n if sub_strings.contains(&(i..=j).map(|i| b[i]).collect::()) {\n ans = max(ans, 2 * (j - i + 1));\n }\n }\n }\n writeln!(out, \"{}\", a.len() + b.len() - ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0677", "problem_description": "Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?Given a n × n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.", "c_code": "int solution() {\n int n;\n int flag = 0;\n scanf(\"%d\", &n);\n char a[n][n];\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", a[i]);\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n flag = 0;\n if (a[i + 1][j] == 'o' && i >= 0 && i + 1 < n && j >= 0 && j <= n) {\n flag++;\n }\n if (a[i - 1][j] == 'o' && i - 1 >= 0 && i <= n && j >= 0 && j <= n) {\n flag++;\n }\n if (a[i][j + 1] == 'o' && i >= 0 && i <= n && j >= 0 && j + 1 < n) {\n flag++;\n }\n if (a[i][j - 1] == 'o' && i >= 0 && i <= n && j - 1 >= 0 && j <= n) {\n flag++;\n }\n if (flag % 2 != 0) {\n printf(\"NO\");\n return 0;\n }\n }\n }\n printf(\"YES\");\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut s = String::new();\n stdin.read_line(&mut s).unwrap();\n\n let n: usize = s.trim().parse().unwrap();\n let mut v: Vec = vec![];\n\n for _ in 0..n {\n let mut s = String::new();\n stdin.read_line(&mut s).unwrap();\n v.push(s.trim().to_string());\n }\n\n let mut r: Vec> = vec![vec![0; n]; n];\n\n for (i, line) in v.iter().enumerate() {\n for (j, c) in line.chars().enumerate() {\n if c == 'o' {\n if i > 0 {\n r[i - 1][j] += 1;\n }\n if i < n - 1 {\n r[i + 1][j] += 1;\n }\n if j > 0 {\n r[i][j - 1] += 1;\n }\n if j < n - 1 {\n r[i][j + 1] += 1;\n }\n }\n }\n }\n\n for line in r {\n for cell in line {\n if cell % 2 == 1 {\n println!(\"NO\");\n return;\n }\n }\n }\n\n println!(\"YES\");\n}", "difficulty": "easy"} {"problem_id": "0678", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

\n

We have A balls with the string S written on each of them and B balls with the string T written on each of them.
\nFrom these balls, Takahashi chooses one with the string U written on it and throws it away.
\nFind the number of balls with the string S and balls with the string T that we have now.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • S, T, and U are strings consisting of lowercase English letters.
  • \n
  • The lengths of S and T are each between 1 and 10 (inclusive).
  • \n
  • S \\not= T
  • \n
  • S=U or T=U.
  • \n
  • 1 \\leq A,B \\leq 10
  • \n
  • A and B are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
S T\nA B\nU\n
\n
\n
\n
\n
\n

Output

\n

Print the answer, with space in between.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

red blue\n3 4\nred\n
\n
\n
\n
\n
\n

Sample Output 1

2 4\n
\n

Takahashi chose a ball with red written on it and threw it away.\nNow we have two balls with the string S and four balls with the string T.

\n
\n
\n
\n
\n
\n

Sample Input 2

red blue\n5 5\nblue\n
\n
\n
\n
\n
\n

Sample Output 2

5 4\n
\n

Takahashi chose a ball with blue written on it and threw it away.\nNow we have five balls with the string S and four balls with the string T.

\n
\n
", "c_code": "int solution(void) {\n char s[11];\n char t[11];\n char u[11];\n int a = 0;\n int b = 0;\n scanf(\"%s %s\\n\", s, t);\n scanf(\"%d %d\\n\", &a, &b);\n scanf(\"%s\\n\", u);\n if (*s == *u) {\n printf(\"%d %d\", a - 1, b);\n } else if (*t == *u) {\n printf(\"%d %d\", a, b - 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let st: Vec = buf.split_whitespace().map(|w| w.to_string()).collect();\n buf.clear();\n std::io::stdin().read_line(&mut buf).unwrap();\n let mut ab: Vec = buf.split_whitespace().map(|w| w.parse().unwrap()).collect();\n buf.clear();\n std::io::stdin().read_line(&mut buf).unwrap();\n let u: String = buf.trim().to_string();\n if st[0] == u {\n ab[0] -= 1;\n } else {\n ab[1] -= 1;\n }\n println!(\"{} {}\", ab[0], ab[1]);\n}", "difficulty": "medium"} {"problem_id": "0679", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

We have a sequence of N integers A~=~A_0,~A_1,~...,~A_{N - 1}.

\n

Let B be a sequence of K \\times N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.

\n

Find the inversion number of B, modulo 10^9 + 7.

\n

Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0 \\leq i < j \\leq K \\times N - 1) such that B_i > B_j.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 2000
  • \n
  • 1 \\leq K \\leq 10^9
  • \n
  • 1 \\leq A_i \\leq 2000
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nA_0 A_1 ... A_{N - 1}\n
\n
\n
\n
\n
\n

Output

Print the inversion number of B, modulo 10^9 + 7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n2 1\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

In this case, B~=~2,~1,~2,~1. We have:

\n
    \n
  • B_0 > B_1
  • \n
  • B_0 > B_3
  • \n
  • B_2 > B_3
  • \n
\n

Thus, the inversion number of B is 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 5\n1 1 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

A may contain multiple occurrences of the same number.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 998244353\n10 9 8 7 5 6 3 4 2 1\n
\n
\n
\n
\n
\n

Sample Output 3

185297239\n
\n

Be sure to print the output modulo 10^9 + 7.

\n
\n
", "c_code": "int solution(void) {\n long long n;\n long long a[2005];\n long long k;\n long long count = 0;\n scanf(\"%lld %lld\", &n, &k);\n for (int j = 0; j < n; j++) {\n scanf(\"%lld\", &a[j]);\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (a[i] > a[j]) {\n if (i < j) {\n count += k * (k + 1) / 2;\n } else {\n count += k * (k - 1) / 2;\n }\n }\n count %= 1000000007;\n }\n }\n k = k % 1000000007;\n count = count % 1000000007;\n printf(\"%lld\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let (N, K): (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 )\n };\n let A: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let MOD = 1_000_000_007;\n let K_1 = ((K * (K + 1)) / 2) % MOD;\n let K_2 = ((K * (K - 1)) / 2) % MOD;\n let ans = {\n let mut res = 0;\n for i in 0..N {\n let mut x = 0;\n for j in (i + 1)..N {\n if A[i] > A[j] {\n x += 1;\n }\n }\n let mut y = 0;\n for j in 0..i {\n if A[i] > A[j] {\n y += 1;\n }\n }\n x = (x * K_1) % MOD;\n y = (y * K_2) % MOD;\n res = (res + x) % MOD;\n res = (res + y) % MOD;\n }\n res\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0680", "problem_description": "You are given a positive integer $$$n$$$. Your task is to find any three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$0 \\le a, b, c \\le 10^9$$$) for which $$$(a\\oplus b)+(b\\oplus c)+(a\\oplus c)=n$$$, or determine that there are no such integers.Here $$$a \\oplus b$$$ denotes the bitwise XOR of $$$a$$$ and $$$b$$$. For example, $$$2 \\oplus 4 = 6$$$ and $$$3 \\oplus 1=2$$$.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n for (int j = 0; j < n; j++) {\n if (arr[j] % 2 == 1) {\n printf(\"%d\\n\", -1);\n } else {\n printf(\"%d %d %d\\n\", arr[j] / 2, 0, 0);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut test_cases = String::new();\n io::stdin()\n .read_line(&mut test_cases)\n .expect(\"Failed to read test_cases\");\n let test_cases: u32 = test_cases\n .trim()\n .parse()\n .expect(\"Failed to convert test_cases from String to u32\");\n\n let mut solved = 0;\n loop {\n if solved == test_cases {\n break;\n }\n\n let mut n = String::new();\n io::stdin().read_line(&mut n).expect(\"Failed to read n\");\n\n let n: u32 = n\n .trim()\n .parse()\n .expect(\"Failed to convert n from String to int\");\n\n if n % 2 == 1 {\n println!(\"{}\", -1);\n } else {\n println!(\"{} {} {}\", n / 2, n / 2, 0);\n }\n\n solved += 1\n }\n}", "difficulty": "medium"} {"problem_id": "0681", "problem_description": "After celebrating the midcourse the students of one of the faculties of the Berland State University decided to conduct a vote for the best photo. They published the photos in the social network and agreed on the rules to choose a winner: the photo which gets most likes wins. If multiple photoes get most likes, the winner is the photo that gets this number first.Help guys determine the winner photo by the records of likes.", "c_code": "int solution() {\n int a[1000001] = {0};\n int n;\n int tmp;\n scanf(\"%d\", &n);\n\n int max = 0;\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &tmp);\n ++(a[tmp]);\n\n if (a[tmp] > a[max]) {\n max = tmp;\n }\n }\n\n printf(\"%d\", max);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_text = String::new();\n io::stdin().read_to_string(&mut input_text).unwrap();\n let mut iter = input_text\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n let _n = iter.next().unwrap();\n let a: Vec = iter.collect();\n let mut map = HashMap::::new();\n let mut current_winner = -1;\n let mut current_max = 0;\n for vote in a {\n let new_vote_count = map.get(&vote).unwrap_or(&0) + 1;\n map.insert(vote, new_vote_count);\n if new_vote_count > current_max {\n current_max = new_vote_count;\n current_winner = vote;\n }\n }\n println!(\"{}\", current_winner);\n}", "difficulty": "medium"} {"problem_id": "0682", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given any integer x, Aoki can do the operation below.

\n

Operation: Replace x with the absolute difference of x and K.

\n

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 ≤ N ≤ 10^{18}
  • \n
  • 1 ≤ K ≤ 10^{18}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7 4\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Initially, N=7.

\n

After one operation, N becomes |7-4| = 3.

\n

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 6\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

N=2 after zero operations is the minimum.

\n
\n
\n
\n
\n
\n

Sample Input 3

1000000000000000000 1\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n long long int N;\n long long int K;\n long long int i;\n long long int j;\n long long int l;\n long long int m;\n\n scanf(\"%lld %lld\", &N, &K);\n\n if (N < K && K < N / 2) {\n printf(\"%lld\\n\", N);\n } else if (N < K && K > N / 2) {\n m = -(N - K);\n if (N > m) {\n printf(\"%lld\\n\", m);\n } else {\n printf(\"%lld\\n\", N);\n }\n }\n\n else if (N >= K && ((N % K) == 0 || K == 1)) {\n printf(\"0\\n\");\n } else if (N > K && !(N % K) == 0) {\n j = N % K;\n l = j - K;\n if (j < l) {\n printf(\"%lld\\n\", j);\n } else {\n printf(\"%lld\\n\", -l);\n }\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let mut itr = s.split_whitespace().map(|e| e.parse().unwrap());\n let n: i64 = itr.next().unwrap();\n let k: i64 = itr.next().unwrap();\n\n let a = n / k;\n println!(\"{}\", min((n - a * k).abs(), (n - (a + 1) * k).abs()));\n}", "difficulty": "medium"} {"problem_id": "0683", "problem_description": "You are given an integer $$$n$$$ and an integer $$$k$$$.In one step you can do one of the following moves: decrease $$$n$$$ by $$$1$$$; divide $$$n$$$ by $$$k$$$ if $$$n$$$ is divisible by $$$k$$$. For example, if $$$n = 27$$$ and $$$k = 3$$$ you can do the following steps: $$$27 \\rightarrow 26 \\rightarrow 25 \\rightarrow 24 \\rightarrow 8 \\rightarrow 7 \\rightarrow 6 \\rightarrow 2 \\rightarrow 1 \\rightarrow 0$$$.You are asked to calculate the minimum number of steps to reach $$$0$$$ from $$$n$$$.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n long long num[t];\n long long n;\n long long k;\n long long c = 0;\n for (int i = 0; i < t; i++, c = 0) {\n scanf(\"%lli%lli\", &n, &k);\n while (n > 0) {\n if (n % k == 0) {\n n = n / k;\n c++;\n } else if (n % k != 0) {\n c = c + n % k;\n n = n - (n % k);\n }\n }\n num[i] = c;\n }\n for (int i = 0; i < t; i++) {\n printf(\"%lli\\n\", num[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let t: i64 = line.trim().parse::().expect(\"error\");\n for _ in 0..t {\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let r: Vec = line\n .trim()\n .split(' ')\n .map(|x| x.parse::().expect(\"error\"))\n .collect();\n let mut n = r[0];\n let mut s = 0;\n let k = r[1];\n while n > 0 {\n let x = n % k;\n if x == 0 {\n n /= k;\n s += 1;\n } else {\n n -= x;\n s += x;\n }\n }\n println!(\"{:?}\", s);\n }\n}", "difficulty": "easy"} {"problem_id": "0684", "problem_description": "You are given a string $$$s$$$, consisting only of characters '0' or '1'. Let $$$|s|$$$ be the length of $$$s$$$.You are asked to choose some integer $$$k$$$ ($$$k > 0$$$) and find a sequence $$$a$$$ of length $$$k$$$ such that: $$$1 \\le a_1 < a_2 < \\dots < a_k \\le |s|$$$; $$$a_{i-1} + 1 < a_i$$$ for all $$$i$$$ from $$$2$$$ to $$$k$$$. The characters at positions $$$a_1, a_2, \\dots, a_k$$$ are removed, the remaining characters are concatenated without changing the order. So, in other words, the positions in the sequence $$$a$$$ should not be adjacent.Let the resulting string be $$$s'$$$. $$$s'$$$ is called sorted if for all $$$i$$$ from $$$2$$$ to $$$|s'|$$$ $$$s'_{i-1} \\le s'_i$$$.Does there exist such a sequence $$$a$$$ that the resulting string $$$s'$$$ is sorted?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n getchar();\n for (int j = 1; j <= t; j++) {\n int flag = 0;\n int flag2 = 0;\n char c = getchar();\n char prev;\n while (c != '\\n' && c != -1) {\n prev = c;\n c = getchar();\n if (c == '1' && c == prev && flag != 1) {\n flag = 1;\n }\n if (flag && c == '0' && c == prev && flag2 == 0) {\n printf(\"NO\\n\");\n flag2 = 1;\n }\n }\n if (flag2 == 0) {\n printf(\"YES\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let s = scan!(chars);\n let n = s.len();\n let mut min11 = n + 2;\n let mut max00 = n + 2;\n for i in 1..n {\n if s[i - 1] == '1' && s[i] == '1' {\n min11 = i;\n break;\n }\n }\n for i in (1..n - 1).rev() {\n if s[i + 1] == '0' && s[i] == '0' {\n max00 = i;\n break;\n }\n }\n if min11 < n && max00 < n && min11 <= max00 {\n print!(\"NO\");\n } else {\n print!(\"YES\");\n }\n print!(\"\\n\");\n }\n}", "difficulty": "easy"} {"problem_id": "0685", "problem_description": "Gildong is playing a video game called Block Adventure. In Block Adventure, there are $$$n$$$ columns of blocks in a row, and the columns are numbered from $$$1$$$ to $$$n$$$. All blocks have equal heights. The height of the $$$i$$$-th column is represented as $$$h_i$$$, which is the number of blocks stacked in the $$$i$$$-th column.Gildong plays the game as a character that can stand only on the top of the columns. At the beginning, the character is standing on the top of the $$$1$$$-st column. The goal of the game is to move the character to the top of the $$$n$$$-th column.The character also has a bag that can hold infinitely many blocks. When the character is on the top of the $$$i$$$-th column, Gildong can take one of the following three actions as many times as he wants: if there is at least one block on the column, remove one block from the top of the $$$i$$$-th column and put it in the bag; if there is at least one block in the bag, take one block out of the bag and place it on the top of the $$$i$$$-th column; if $$$i < n$$$ and $$$|h_i - h_{i+1}| \\le k$$$, move the character to the top of the $$$i+1$$$-st column. $$$k$$$ is a non-negative integer given at the beginning of the game. Note that it is only possible to move to the next column. In actions of the first two types the character remains in the $$$i$$$-th column, and the value $$$h_i$$$ changes.The character initially has $$$m$$$ blocks in the bag. Gildong wants to know if it is possible to win the game. Help Gildong find the answer to his question.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int j = 0; j < t; j++) {\n int n;\n int m;\n int k;\n int flag = 0;\n scanf(\"%d %d %d\", &n, &m, &k);\n int h[n];\n scanf(\"%d\", &h[0]);\n for (int i = 1; i < n; i++) {\n scanf(\"%d\", &h[i]);\n if (h[i] < h[i - 1] + k) {\n if (h[i - 1] <= h[i - 1] - h[i] + k) {\n m += h[i - 1];\n } else {\n m += h[i - 1] - h[i] + k;\n }\n }\n\n if (h[i] > h[i - 1] + k) {\n m -= h[i] - h[i - 1] - k;\n if (m < 0) {\n flag = 1;\n }\n }\n }\n\n if (flag == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut sout = BufWriter::new(io::stdout());\n let mut sin = BufReader::new(io::stdin());\n let mut buf = String::new();\n sin.read_line(&mut buf).unwrap();\n let t: usize = buf.trim().parse().unwrap();\n for _i in 0..t {\n buf = String::new();\n sin.read_line(&mut buf).unwrap();\n let buf_wp: Vec = buf\n .split_whitespace()\n .map(|it| it.trim().parse().unwrap())\n .collect();\n let (n, mut m, k) = (buf_wp[0] as usize, buf_wp[1], buf_wp[2]);\n buf = String::new();\n sin.read_line(&mut buf).unwrap();\n let mut h: Vec = buf\n .split_whitespace()\n .map(|it| it.trim().parse().unwrap())\n .collect();\n let mut failed = false;\n for i in 0..(n - 1) {\n m += h[i] - max(0, h[i + 1] - k);\n if m < 0 {\n failed = true;\n break;\n }\n h[i] = max(0, h[i + 1] - k);\n }\n if failed {\n writeln!(sout, \"NO\");\n } else {\n writeln!(sout, \"YES\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0686", "problem_description": "You are given $$$n$$$ arrays $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$; each array consists of exactly $$$m$$$ integers. We denote the $$$y$$$-th element of the $$$x$$$-th array as $$$a_{x, y}$$$.You have to choose two arrays $$$a_i$$$ and $$$a_j$$$ ($$$1 \\le i, j \\le n$$$, it is possible that $$$i = j$$$). After that, you will obtain a new array $$$b$$$ consisting of $$$m$$$ integers, such that for every $$$k \\in [1, m]$$$ $$$b_k = \\max(a_{i, k}, a_{j, k})$$$.Your goal is to choose $$$i$$$ and $$$j$$$ so that the value of $$$\\min \\limits_{k = 1}^{m} b_k$$$ is maximum possible.", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int i;\n int j;\n int a[300005][8];\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n scanf(\"%d\", &a[i][j]);\n }\n }\n int min;\n int mid;\n int max;\n int c[1024];\n int p = 1;\n for (i = 0; i < m; i++) {\n p *= 2;\n }\n p--;\n int v;\n int k;\n min = 0;\n max = 1000000009;\n while (max - min > 1) {\n mid = (max + min) / 2;\n for (i = 0; i < 1024; i++) {\n c[i] = 0;\n }\n for (i = 0; i < n; i++) {\n v = 0;\n for (j = 0; j < m; j++) {\n if (a[i][j] >= mid) {\n v = 2 * v + 1;\n } else {\n v = 2 * v;\n }\n }\n c[v] = 1;\n }\n k = 0;\n for (i = 0; i <= p; i++) {\n for (j = 0; j <= p; j++) {\n if (c[i] > 0 && c[j] > 0 && (i | j) == p) {\n k++;\n }\n }\n }\n if (k > 0) {\n min = mid;\n } else {\n max = mid;\n }\n }\n for (i = 0; i < 1024; i++) {\n c[i] = -1;\n }\n for (i = 0; i < n; i++) {\n v = 0;\n for (j = 0; j < m; j++) {\n if (a[i][j] >= min) {\n v = 2 * v + 1;\n } else {\n v = 2 * v;\n }\n }\n c[v] = i;\n }\n for (i = 0; i <= p; i++) {\n for (j = 0; j <= p; j++) {\n if (c[i] >= 0 && c[j] >= 0 && (i | j) == p) {\n printf(\"%d %d\\n\", c[i] + 1, c[j] + 1);\n return 0;\n }\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\n .split_whitespace()\n .map(|x| x.parse::().expect(\"convert to number\"))\n .collect::>();\n\n let n = arr[0];\n let m = arr[1];\n\n let matrix: Vec<_> = (0..n).map(|i| &arr[2 + i * m..2 + i * m + m]).collect();\n\n let combinations = 2usize.pow(m as u32);\n\n let (mut l, mut r) = (0, 1_000_000_001);\n\n let mut val_best = 0;\n let mut indices_chosen = None;\n\n while l + 1 < r {\n let mid = (l + r) / 2;\n\n let mut bitmasks = vec![false; combinations];\n let mut indices = vec![0; combinations];\n\n for (index, i) in matrix.iter().enumerate() {\n let mut mask = 0;\n for (ix, j) in i.iter().enumerate() {\n if j >= &mid {\n mask |= 1 << ix;\n }\n }\n bitmasks[mask] = true;\n indices[mask] = index;\n }\n\n let mut found = false;\n 'outer: for i in 0..combinations {\n for j in 0..combinations {\n let present = bitmasks[i] && bitmasks[j];\n\n let pattern: usize = i | j;\n if (pattern == combinations - 1) && present {\n if mid >= val_best {\n indices_chosen = Some((indices[i], indices[j]));\n val_best = mid;\n }\n found = true;\n break 'outer;\n }\n }\n }\n\n if found {\n l = mid;\n } else {\n r = mid;\n }\n }\n\n match indices_chosen {\n Some(x) => {\n println!(\"{} {}\", x.0 + 1, x.1 + 1);\n }\n _ => {\n println!(\"1 1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0687", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Snuke is making sugar water in a beaker.\nInitially, the beaker is empty. Snuke can perform the following four types of operations any number of times. He may choose not to perform some types of operations.

\n
    \n
  • Operation 1: Pour 100A grams of water into the beaker.
  • \n
  • Operation 2: Pour 100B grams of water into the beaker.
  • \n
  • Operation 3: Put C grams of sugar into the beaker.
  • \n
  • Operation 4: Put D grams of sugar into the beaker.
  • \n
\n

In our experimental environment, E grams of sugar can dissolve into 100 grams of water.

\n

Snuke will make sugar water with the highest possible density.

\n

The beaker can contain at most F grams of substances (water and sugar combined), and there must not be any undissolved sugar in the beaker.\nFind the mass of the sugar water Snuke will make, and the mass of sugar dissolved in it.\nIf there is more than one candidate, any of them will be accepted.

\n

We remind you that the sugar water that contains a grams of water and b grams of sugar is \\frac{100b}{a + b} percent.\nAlso, in this problem, pure water that does not contain any sugar is regarded as 0 percent density sugar water.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A < B \\leq 30
  • \n
  • 1 \\leq C < D \\leq 30
  • \n
  • 1 \\leq E \\leq 100
  • \n
  • 100A \\leq F \\leq 3 000
  • \n
  • A, B, C, D, E and F are all integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Inputs

Input is given from Standard Input in the following format:

\n
A B C D E F\n
\n
\n
\n
\n
\n

Outputs

Print two integers separated by a space.\nThe first integer should be the mass of the desired sugar water, and the second should be the mass of the sugar dissolved in it.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 2 10 20 15 200\n
\n
\n
\n
\n
\n

Sample Output 1

110 10\n
\n

In this environment, 15 grams of sugar can dissolve into 100 grams of water, and the beaker can contain at most 200 grams of substances.

\n

We can make 110 grams of sugar water by performing Operation 1 once and Operation 3 once.\nIt is not possible to make sugar water with higher density.\nFor example, the following sequences of operations are infeasible:

\n
    \n
  • If we perform Operation 1 once and Operation 4 once, there will be undissolved sugar in the beaker.
  • \n
  • If we perform Operation 2 once and Operation 3 three times, the mass of substances in the beaker will exceed 200 grams.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

1 2 1 2 100 1000\n
\n
\n
\n
\n
\n

Sample Output 2

200 100\n
\n

There are other acceptable outputs, such as:

\n
400 200\n
\n

However, the output below is not acceptable:

\n
300 150\n
\n

This is because, in order to make 300 grams of sugar water containing 150 grams of sugar, we need to pour exactly 150 grams of water into the beaker, which is impossible.

\n
\n
\n
\n
\n
\n

Sample Input 3

17 19 22 26 55 2802\n
\n
\n
\n
\n
\n

Sample Output 3

2634 934\n
\n
\n
", "c_code": "int solution() {\n int A;\n int B;\n int C;\n int D;\n int E;\n int F;\n scanf(\"%d %d %d %d %d %d\", &A, &B, &C, &D, &E, &F);\n\n int w = 100 * A;\n int s = 0;\n for (int a = 0; 100 * A * a <= F; a++) {\n for (int b = 0; 100 * (A * a + B * b) <= F; b++) {\n if (a == 0 && b == 0) {\n continue;\n }\n for (int c = 0; 100 * (A * a + B * b) + C * c <= F; c++) {\n for (int d = 0; 100 * (A * a + B * b) + C * c + D * d <= F; d++) {\n if ((A * a + B * b) * E < C * c + D * d) {\n continue;\n }\n if ((100 * (A * a + B * b) + C * c + D * d) * s <\n (w + s) * (C * c + D * d)) {\n w = 100 * (A * a + B * b);\n s = C * c + D * d;\n }\n }\n }\n }\n }\n\n printf(\"%d %d\\n\", w + s, s);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n a: usize,\n b: usize,\n c: usize,\n d: usize,\n e: usize,\n f: usize,\n }\n\n let mut mizu;\n let mut sato;\n let mut candidate = Vec::with_capacity(1_000);\n\n for ac in 0..f / (100 * a) + 1 {\n for bc in 0..(f - ac * a * 100) / (100 * b) + 1 {\n mizu = (a * ac + b * bc) * 100;\n if mizu == 0 {\n continue;\n }\n for cc in 0..(f - mizu) / c + 1 {\n for dc in 0..(f - mizu - c * cc) / d + 1 {\n sato = c * cc + d * dc;\n if sato <= e * (mizu / 100) {\n candidate.push((mizu, sato));\n }\n }\n }\n }\n }\n\n let mut cand = candidate[0];\n for &c in candidate.iter() {\n if cand.0 * c.1 > cand.1 * c.0 {\n cand = c\n }\n }\n println!(\"{} {}\", cand.0 + cand.1, cand.1);\n}", "difficulty": "medium"} {"problem_id": "0688", "problem_description": "God's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \\le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \\le i < j \\le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \\ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).", "c_code": "int solution() {\n int t;\n scanf(\"%d\\n\", &t);\n for (int p = 0; p < t; p++) {\n int ans = 0;\n int n;\n int k;\n scanf(\"%d %d\\n\", &n, &k);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] > k && i < k) {\n ans++;\n }\n }\n printf(\"%d \\n\", ans);\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: String = iterator.next().unwrap().unwrap();\n let strings: Vec<&str> = line.split(\" \").collect();\n let n = strings.first().unwrap().parse::().unwrap();\n let k = strings.get(1).unwrap().parse::().unwrap();\n let line: String = iterator.next().unwrap().unwrap();\n let strings: Vec<&str> = line.split(\" \").collect();\n let mut numbers = vec![];\n for i in 0..n {\n numbers.push(strings.get(i as usize).unwrap().parse::().unwrap());\n }\n let mut sorted: Vec = numbers.clone();\n sorted.sort();\n let max = sorted.get(k as usize - 1).unwrap();\n let mut result = 0;\n for i in 0..k {\n if numbers.get(i as usize).unwrap() > max {\n result += 1;\n }\n }\n println!(\"{}\", result);\n }\n}", "difficulty": "hard"} {"problem_id": "0689", "problem_description": "Как известно, в теплую погоду многие жители крупных городов пользуются сервисами городского велопроката. Вот и Аркадий сегодня будет добираться от школы до дома, используя городские велосипеды.Школа и дом находятся на одной прямой улице, кроме того, на той же улице есть n точек, где можно взять велосипед в прокат или сдать его. Первый велопрокат находится в точке x1 километров вдоль улицы, второй — в точке x2 и так далее, n-й велопрокат находится в точке xn. Школа Аркадия находится в точке x1 (то есть там же, где и первый велопрокат), а дом — в точке xn (то есть там же, где и n-й велопрокат). Известно, что xi < xi + 1 для всех 1 ≤ i < n.Согласно правилам пользования велопроката, Аркадий может брать велосипед в прокат только на ограниченное время, после этого он должен обязательно вернуть его в одной из точек велопроката, однако, он тут же может взять новый велосипед, и отсчет времени пойдет заново. Аркадий может брать не более одного велосипеда в прокат одновременно. Если Аркадий решает взять велосипед в какой-то точке проката, то он сдаёт тот велосипед, на котором он до него доехал, берёт ровно один новый велосипед и продолжает на нём своё движение.За отведенное время, независимо от выбранного велосипеда, Аркадий успевает проехать не больше k километров вдоль улицы. Определите, сможет ли Аркадий доехать на велосипедах от школы до дома, и если да, то какое минимальное число раз ему необходимо будет взять велосипед в прокат, включая первый велосипед? Учтите, что Аркадий не намерен сегодня ходить пешком.", "c_code": "int solution(void) {\n\n int points_count = 0;\n scanf(\"%d\", &points_count);\n\n int max_distance = 0;\n scanf(\"%d\", &max_distance);\n\n int bicycles_count = 1;\n int current_position = 0;\n scanf(\"%d\", ¤t_position);\n int available_distance = max_distance;\n for (int i = 1; i < points_count; i++) {\n int next_position = 0;\n scanf(\"%d\", &next_position);\n if ((current_position + max_distance) < next_position) {\n bicycles_count = -1;\n break;\n }\n\n if (next_position <= (current_position + available_distance)) {\n available_distance -= (next_position - current_position);\n current_position = next_position;\n } else {\n available_distance = max_distance - (next_position - current_position);\n current_position = next_position;\n bicycles_count++;\n }\n }\n\n printf(\"%d\\n\", bicycles_count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n stdin().read_line(&mut s);\n let k: i32 = s[s.find(' ').unwrap() + 1..s.len()]\n .trim()\n .parse::()\n .unwrap();\n s.clear();\n stdin().read_line(&mut s);\n let a: Vec = s\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect();\n let mut b: Vec = Vec::new();\n for c in 0..a.len() - 1 {\n b.push(a[c + 1] - a[c])\n }\n let mut i: usize = 0;\n let mut l: i32 = k;\n let mut x: i32 = 1;\n let mut d: i32 = 0;\n while i < b.len() {\n if l - b[i] < 0 {\n if d == 2 {\n println!(\"-1\");\n return;\n }\n l = k;\n x += 1;\n d += 1;\n } else {\n l -= b[i];\n i += 1;\n d = 0;\n }\n }\n println!(\"{}\", x);\n}", "difficulty": "hard"} {"problem_id": "0690", "problem_description": "Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows: You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.", "c_code": "int solution() {\n\n long long n;\n long long q;\n long long p;\n scanf(\"%lld%lld\", &n, &q);\n while (q--) {\n scanf(\"%lld\", &p);\n while (!(p & 1)) {\n p = p + (n - (p / 2));\n }\n printf(\"%lld\\n\", (p + 1) / 2);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::stdin;\n\n let mut line = String::new();\n\n let n: u64;\n let q: u32;\n\n {\n stdin().read_line(&mut line);\n let parts: Vec<&str> = line.split_whitespace().collect();\n n = parts[0].parse().expect(\"n\");\n q = parts[1].parse().expect(\"q\");\n }\n\n for _ in 0..q {\n line.clear();\n stdin().read_line(&mut line);\n let mut xi: u64 = line.trim().parse().expect(\"xi\");\n while (xi & 1) == 0 {\n xi = xi + n - (xi >> 1)\n }\n println!(\"{}\", (xi + 1) >> 1);\n }\n}", "difficulty": "medium"} {"problem_id": "0691", "problem_description": "AquaMoon had $$$n$$$ strings of length $$$m$$$ each. $$$n$$$ is an odd number.When AquaMoon was gone, Cirno tried to pair these $$$n$$$ strings together. After making $$$\\frac{n-1}{2}$$$ pairs, she found out that there was exactly one string without the pair!In her rage, she disrupted each pair of strings. For each pair, she selected some positions (at least $$$1$$$ and at most $$$m$$$) and swapped the letters in the two strings of this pair at the selected positions.For example, if $$$m = 6$$$ and two strings \"abcdef\" and \"xyzklm\" are in one pair and Cirno selected positions $$$2$$$, $$$3$$$ and $$$6$$$ she will swap 'b' with 'y', 'c' with 'z' and 'f' with 'm'. The resulting strings will be \"ayzdem\" and \"xbcklf\".Cirno then stole away the string without pair and shuffled all remaining strings in arbitrary order.AquaMoon found the remaining $$$n-1$$$ strings in complete disarray. Also, she remembers the initial $$$n$$$ strings. She wants to know which string was stolen, but she is not good at programming. Can you help her?", "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[m];\n char cnt[m];\n for (int i = 0; i < m; i++) {\n cnt[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", s);\n for (int j = 0; j < m; j++) {\n cnt[j] += s[j];\n }\n }\n for (int i = 0; i < n - 1; i++) {\n scanf(\"%s\", s);\n for (int j = 0; j < m; j++) {\n cnt[j] -= s[j];\n }\n }\n for (int i = 0; i < m; i++) {\n printf(\"%c\", cnt[i]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines().map(Result::unwrap);\n let t: u16 = lines.next().unwrap().parse().unwrap();\n\n for _ in 0..t {\n let (n, m): (usize, usize) = {\n let line = lines.next().unwrap();\n let mut words = line.split(' ');\n (\n words.next().unwrap().parse().unwrap(),\n words.next().unwrap().parse().unwrap(),\n )\n };\n\n let mut bits = vec![0_u32; m];\n\n for _ in 0..(2 * n - 1) {\n let line = lines.next().unwrap();\n for (b, c) in bits.iter_mut().zip(line.as_bytes()) {\n *b ^= 1 << (c - b'a');\n }\n }\n\n let word: Box<[u8]> = bits\n .into_iter()\n .map(|b| b.trailing_zeros() as u8 + b'a')\n .collect();\n\n println!(\"{}\", str::from_utf8(&word).unwrap());\n }\n}", "difficulty": "medium"} {"problem_id": "0692", "problem_description": "YouKn0wWho has two even integers $$$x$$$ and $$$y$$$. Help him to find an integer $$$n$$$ such that $$$1 \\le n \\le 2 \\cdot 10^{18}$$$ and $$$n \\bmod x = y \\bmod n$$$. Here, $$$a \\bmod b$$$ denotes the remainder of $$$a$$$ after division by $$$b$$$. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints.", "c_code": "int solution() {\n long long int n;\n scanf(\"%lld\", &n);\n long long int a;\n long long int b;\n for (long long int i = 0; i < n; i++) {\n scanf(\"%lld%lld\", &a, &b);\n if (a == b) {\n printf(\"%lld\", a);\n } else if ((b % a == 0) && (a != b)) {\n printf(\"%lld\", b);\n } else {\n if (a > b) {\n printf(\"%lld\", a + b);\n } else if ((b / a) == 1) {\n printf(\"%lld\", (a + b) / 2);\n } else {\n printf(\"%lld\", ((a * ((b / a) - 1)) + b) / 2);\n }\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() -> std::io::Result<()> {\n let mut input = String::new();\n stdin().read_to_string(&mut input)?;\n\n let mut input_iter = input.split_whitespace();\n let mut next_int = || input_iter.next().unwrap().parse::().unwrap();\n\n for _ in 0..next_int() {\n let x = next_int();\n let y = next_int();\n\n let res = if x == y {\n x\n } else if x > y {\n x + y\n } else {\n y - y % x / 2\n };\n\n println!(\"{}\", res);\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0693", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Find the number of sequences of length K consisting of positive integers such that the product of any two adjacent elements is at most N, modulo 10^9+7.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1\\leq N\\leq 10^9
  • \n
  • 1 2\\leq K\\leq 100 (fixed at 21:33 JST)
  • \n
  • N and K are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

Print the number of sequences, modulo 10^9+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

(1,1), (1,2), (1,3), (2,1), and (3,1) satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 3\n
\n
\n
\n
\n
\n

Sample Output 2

147\n
\n
\n
\n
\n
\n
\n

Sample Input 3

314159265 35\n
\n
\n
\n
\n
\n

Sample Output 3

457397712\n
\n
\n
", "c_code": "int solution() {\n long long int N;\n long long int K;\n scanf(\"%lld %lld\", &N, &K);\n long long int m;\n m = 1000000000 + 7;\n long long int dp[101][100001];\n long long int a[100001];\n long long int b[100001];\n long long int i;\n long long int j;\n long long int k;\n k = 0;\n long long int l;\n long long int s;\n long long int c;\n long long int d;\n long long int ans;\n ans = 0;\n for (c = 1; c <= N; c = d + 1) {\n d = N / (N / c);\n k = k + 1;\n a[k] = c;\n b[k] = d;\n }\n dp[0][1] = 1;\n for (i = 1; i <= K; i++) {\n l = 0;\n s = 0;\n for (j = k; j >= 1; j--) {\n if (l + 1 <= k && a[l + 1] * a[j] <= N) {\n l = l + 1;\n s = (s + dp[i - 1][l]) % m;\n }\n dp[i][j] = ((b[j] - a[j] + 1) * s) % m;\n if (i == K) {\n ans = (ans + dp[K][j]) % m;\n }\n }\n }\n printf(\"%lld\", ans);\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n k: usize\n }\n let d = 1000000007;\n let mut sq = 1;\n while sq * sq <= n {\n sq += 1;\n }\n sq -= 1;\n let m = n / (sq + 1);\n let len = sq + m;\n let mut c = vec![0; m];\n for i in 0..m {\n c[i] = (n / (m - i) - n / (m + 1 - i)) % d;\n }\n let mut dp = vec![vec![0; len]; k + 1];\n dp[0][0] = 1;\n for i in 1..k + 1 {\n let mut ac = vec![0; len];\n ac[0] = dp[i - 1][0];\n for j in 1..len {\n ac[j] = (ac[j - 1] + dp[i - 1][j]) % d;\n }\n for j in 0..sq {\n dp[i][j] = ac[len - j - 1];\n }\n for j in 0..m {\n dp[i][sq + j] = c[j] * ac[n / (n / (m - j)) - 1] % d;\n }\n }\n println!(\"{}\", dp[k].iter().fold(0, |ac, x| (ac + x) % d));\n}", "difficulty": "easy"} {"problem_id": "0694", "problem_description": "\n\n\n\n

Matrix Vector Multiplication


\n\n

\nWrite a program which reads a $ n \\times m$ matrix $A$ and a $m \\times 1$ vector $b$, and prints their product $Ab$.\n

\n\n

\n A column vector with m elements is represented by the following equation.\n

\n\n\\[\n b = \\left(\n \\begin{array}{c}\n b_1 \\\\\n b_2 \\\\\n : \\\\ \n b_m \\\\ \n \\end{array}\n \\right)\n\\]\n\n

\n A $n \\times m$ matrix with $m$ column vectors, each of which consists of $n$ elements, is represented by the following equation.\n

\n\n\\[\n A = \\left(\n \\begin{array}{cccc}\n a_{11} & a_{12} & ... & a_{1m} \\\\\n a_{21} & a_{22} & ... & a_{2m} \\\\\n : & : & : & : \\\\\n a_{n1} & a_{n2} & ... & a_{nm} \\\\\n \\end{array}\n \\right)\n\\]\n\n

\n $i$-th element of a $m \\times 1$ column vector $b$ is represented by $b_i$ ($i = 1, 2, ..., m$), and the element in $i$-th row and $j$-th column of a matrix $A$ is represented by $a_{ij}$ ($i = 1, 2, ..., n,$ $j = 1, 2, ..., m$).\n

\n\n

\nThe product of a $n \\times m$ matrix $A$ and a $m \\times 1$ column vector $b$ is a $n \\times 1$ column vector $c$, and $c_i$ is obtained by the following formula:\n

\n\n\\[\nc_i = \\sum_{j=1}^m a_{ij}b_j = a_{i1}b_1 + a_{i2}b_2 + ... + a_{im}b_m\n\\]\n\n\n

Input

\n

\n In the first line, two integers $n$ and $m$ are given. In the following $n$ lines, $a_{ij}$ are given separated by a single space character. In the next $m$ lines, $b_i$ is given in a line.\n

\n\n

Output

\n

\n The output consists of $n$ lines. Print $c_i$ in a line.\n

\n\n

Constraints

\n
    \n
  • $1 \\leq n, m \\leq 100$
  • \n
  • $0 \\leq b_i, a_{ij} \\leq 1000$
  • \n
\n\n

Sample Input

\n
\n3 4\n1 2 0 1\n0 3 0 1\n4 1 1 0\n1\n2\n3\n0\n
\n\n\n

Sample Output

\n
\n5\n6\n9\n
", "c_code": "int solution(void) {\n\n int n = 0;\n int m = 0;\n\n int i = 0;\n int j = 0;\n int sum = 0;\n\n scanf(\"%d\", &n);\n scanf(\"%d\", &m);\n\n int A[n][m];\n int b[m];\n\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n scanf(\"%d\", &A[i][j]);\n }\n }\n\n for (j = 0; j < m; j++) {\n scanf(\"%d\", &b[j]);\n }\n\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n sum += A[i][j] * b[j];\n }\n\n printf(\"%d\\n\", sum);\n sum = 0;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let lines = stdin\n .lock()\n .lines()\n .filter_map(|x| x.ok())\n .collect::>();\n\n let mut ite1 = lines[0]\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n let n = ite1.next().unwrap();\n let m = ite1.next().unwrap();\n\n let vec_a = lines[1..n + 1]\n .iter()\n .map(|line| {\n line.split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>()\n })\n .collect::>();\n\n let vec_b = lines[n + 1..n + 1 + m]\n .iter()\n .map(|line| line.trim().parse::().unwrap())\n .collect::>();\n\n for a in vec_a {\n let sum = a.iter().zip(vec_b.iter()).map(|(a, b)| a * b).sum::();\n println!(\"{}\", sum);\n }\n}", "difficulty": "medium"} {"problem_id": "0695", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

Takahashi is locked within a building.

\n

This building consists of H×W rooms, arranged in H rows and W columns.\nWe will denote the room at the i-th row and j-th column as (i,j). The state of this room is represented by a character A_{i,j}. If A_{i,j}= #, the room is locked and cannot be entered; if A_{i,j}= ., the room is not locked and can be freely entered.\nTakahashi is currently at the room where A_{i,j}= S, which can also be freely entered.

\n

Each room in the 1-st row, 1-st column, H-th row or W-th column, has an exit.\nEach of the other rooms (i,j) is connected to four rooms: (i-1,j), (i+1,j), (i,j-1) and (i,j+1).

\n

Takahashi will use his magic to get out of the building. In one cast, he can do the following:

\n
    \n
  • Move to an adjacent room at most K times, possibly zero. Here, locked rooms cannot be entered.
  • \n
  • Then, select and unlock at most K locked rooms, possibly zero. Those rooms will remain unlocked from then on.
  • \n
\n

His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.

\n

It is guaranteed that Takahashi is initially at a room without an exit.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 ≤ H ≤ 800
  • \n
  • 3 ≤ W ≤ 800
  • \n
  • 1 ≤ K ≤ H×W
  • \n
  • Each A_{i,j} is # , . or S.
  • \n
  • There uniquely exists (i,j) such that A_{i,j}= S, and it satisfies 2 ≤ i ≤ H-1 and 2 ≤ j ≤ W-1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W K\nA_{1,1}A_{1,2}...A_{1,W}\n:\nA_{H,1}A_{H,2}...A_{H,W}\n
\n
\n
\n
\n
\n

Output

Print the minimum necessary number of casts.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3 3\n#.#\n#S.\n###\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Takahashi can move to room (1,2) in one cast.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3 3\n###\n#S#\n###\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

Takahashi cannot move in the first cast, but can unlock room (1,2).\nThen, he can move to room (1,2) in the next cast, achieving the objective in two casts.

\n
\n
\n
\n
\n
\n

Sample Input 3

7 7 2\n#######\n#######\n##...##\n###S###\n##.#.##\n###.###\n#######\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n
\n
", "c_code": "int solution() {\n int i;\n int H;\n int W;\n int K;\n char A[802][802] = {};\n scanf(\"%d %d %d\", &H, &W, &K);\n for (i = 1; i <= H; i++) {\n scanf(\"%s\", &(A[i][1]));\n }\n\n int j;\n int dist[802][802];\n int q[640000][2];\n int head;\n int tail;\n for (i = 1; i <= H; i++) {\n for (j = 1; j <= W; j++) {\n if (A[i][j] == 'S') {\n q[0][0] = i;\n q[0][1] = j;\n dist[i][j] = 0;\n } else {\n dist[i][j] = K + 2;\n }\n }\n }\n\n int t = q[0][0];\n int b = q[0][0];\n int l = q[0][1];\n int r = q[0][1];\n for (head = 0, tail = 1; head < tail; head++) {\n i = q[head][0];\n j = q[head][1];\n if (dist[i][j] >= K) {\n break;\n }\n\n if (A[i - 1][j] == '.' && dist[i - 1][j] > dist[i][j] + 1) {\n dist[i - 1][j] = dist[i][j] + 1;\n q[tail][0] = i - 1;\n q[tail++][1] = j;\n if (i - 1 < t) {\n t = i - 1;\n }\n }\n if (A[i + 1][j] == '.' && dist[i + 1][j] > dist[i][j] + 1) {\n dist[i + 1][j] = dist[i][j] + 1;\n q[tail][0] = i + 1;\n q[tail++][1] = j;\n if (i + 1 > b) {\n b = i + 1;\n }\n }\n if (A[i][j - 1] == '.' && dist[i][j - 1] > dist[i][j] + 1) {\n dist[i][j - 1] = dist[i][j] + 1;\n q[tail][0] = i;\n q[tail++][1] = j - 1;\n if (j - 1 < l) {\n l = j - 1;\n }\n }\n if (A[i][j + 1] == '.' && dist[i][j + 1] > dist[i][j] + 1) {\n dist[i][j + 1] = dist[i][j] + 1;\n q[tail][0] = i;\n q[tail++][1] = j + 1;\n if (j + 1 > r) {\n r = j + 1;\n }\n }\n }\n\n int ans = H + W;\n if ((t + K - 2) / K < ans) {\n ans = (t + K - 2) / K;\n }\n if ((H - b + K - 1) / K < ans) {\n ans = (H - b + K - 1) / K;\n }\n if ((l + K - 2) / K < ans) {\n ans = (l + K - 2) / K;\n }\n if ((W - r + K - 1) / K < ans) {\n ans = (W - r + K - 1) / K;\n }\n printf(\"%d\\n\", ans + 1);\n fflush(stdout);\n return 0;\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 h = it.next().unwrap().parse::().unwrap();\n let w = it.next().unwrap().parse::().unwrap();\n let k = it.next().unwrap().parse::().unwrap();\n let s = (0..h)\n .map(|_| it.next().unwrap().as_bytes())\n .collect::>();\n let mut res = h * w;\n for i in 0..h {\n for j in 0..w {\n if s[i][j] != b'S' {\n continue;\n }\n let mut dist = vec![vec![h * w; w]; h];\n let mut qu = VecDeque::new();\n dist[i][j] = 0;\n qu.push_back((i, j));\n let dx = [-1, 0, 1, 0];\n let dy = [0, -1, 0, 1];\n while !qu.is_empty() {\n let (cx, cy) = qu.pop_front().unwrap();\n res = std::cmp::min(\n res,\n [cx, h - cx - 1, cy, w - cy - 1]\n .iter()\n .min()\n .unwrap()\n .div_ceil(k)\n + 1,\n );\n if cx == 0 || cx == h - 1 || cy == 0 || cy == w - 1 {\n continue;\n }\n if dist[cx][cy] == k {\n continue;\n }\n for dir in 0..4 {\n let nx = (cx as i32 + dx[dir]) as usize;\n let ny = (cy as i32 + dy[dir]) as usize;\n if s[nx][ny] == b'#' {\n continue;\n }\n if dist[cx][cy] + 1 >= dist[nx][ny] {\n continue;\n }\n dist[nx][ny] = dist[cx][cy] + 1;\n qu.push_back((nx, ny));\n }\n }\n }\n }\n println!(\"{}\", res);\n}", "difficulty": "medium"} {"problem_id": "0696", "problem_description": "This is the easy version of the problem. The only difference is that in this version $$$n \\leq 2000$$$. You can make hacks only if both versions of the problem are solved.There are $$$n$$$ potions in a line, with potion $$$1$$$ on the far left and potion $$$n$$$ on the far right. Each potion will increase your health by $$$a_i$$$ when drunk. $$$a_i$$$ can be negative, meaning that potion will decrease will health.You start with $$$0$$$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.What is the largest number of potions you can drink?", "c_code": "int solution() {\n int n;\n int kt = 0;\n int N;\n scanf(\"%d\", &n);\n N = n;\n long long a[n];\n long long i;\n long long sum;\n long long I;\n long long min;\n long long imin;\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\nmn:\n sum = 0;\n for (i = 0; i < n; i++) {\n sum += a[i];\n if (sum < 0) {\n I = i;\n kt = 1;\n break;\n }\n }\n if (kt == 1) {\n min = 1000000000;\n for (i = 0; i <= I; i++) {\n if (min > a[i]) {\n min = a[i];\n imin = i;\n }\n }\n a[imin] = 0;\n N--;\n kt = 0;\n goto mn;\n }\n printf(\"%d\", 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 _n = lines.next().unwrap().parse::().unwrap();\n\n let mut h = std::collections::BinaryHeap::new();\n let mut s = 0;\n\n let mut p = 0;\n\n for a in lines\n .next()\n .unwrap()\n .split_whitespace()\n .map(|a| a.parse::().unwrap())\n {\n if a < 0 {\n let x = -a;\n h.push(x);\n s -= x;\n while s < 0 {\n let x = h.pop().unwrap();\n s += x;\n }\n } else {\n p += 1;\n s += a;\n }\n }\n\n let ans = p + h.len();\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n}", "difficulty": "hard"} {"problem_id": "0697", "problem_description": "Let's call left cyclic shift of some string $$$t_1 t_2 t_3 \\dots t_{n - 1} t_n$$$ as string $$$t_2 t_3 \\dots t_{n - 1} t_n t_1$$$.Analogically, let's call right cyclic shift of string $$$t$$$ as string $$$t_n t_1 t_2 t_3 \\dots t_{n - 1}$$$.Let's say string $$$t$$$ is good if its left cyclic shift is equal to its right cyclic shift.You are given string $$$s$$$ which consists of digits 0–9.What is the minimum number of characters you need to erase from $$$s$$$ to make it good?", "c_code": "int solution(void) {\n int i;\n int j;\n int T;\n int max;\n int len;\n int a;\n int b;\n int flag;\n int d1[10];\n int d2[100];\n char str[200001];\n\n scanf(\"%d\", &T);\n\n while (T--) {\n scanf(\"%s\", str);\n len = strlen(str);\n\n memset(d1, 0, sizeof(d1));\n memset(d2, 0, sizeof(d2));\n\n ++d1[str[0] - '0'];\n for (i = 1; str[i]; ++i) {\n ++d1[str[i] - '0'];\n }\n\n max = 0;\n for (i = 0; i < 10; ++i) {\n if (max < d1[i]) {\n max = d1[i];\n }\n }\n\n for (i = 0; i < 100; ++i) {\n a = i / 10;\n b = i % 10;\n flag = 0;\n for (j = 0; str[j]; ++j) {\n if (flag == 0 && str[j] - '0' == a) {\n flag = 1;\n } else if (flag == 1 && str[j] - '0' == b) {\n ++d2[(a * 10) + b];\n flag = 0;\n }\n }\n }\n\n for (i = 0; i < 100; ++i) {\n if (max < d2[i] * 2) {\n max = d2[i] * 2;\n }\n }\n\n printf(\"%d\\n\", len - max);\n }\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 s: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end()\n .chars()\n .map(|c| c.to_digit(10).unwrap())\n .collect()\n };\n\n let mut ans = std::usize::MAX;\n for i in 0..=9 {\n for j in 0..=9 {\n let mut res = {\n s.iter().fold(0, |acc, &x| {\n if acc % 2 == 0 && x == i {\n acc + 1\n } else if acc % 2 == 1 && x == j {\n acc + 1\n } else {\n acc\n }\n })\n };\n if i != j {\n res = res / 2 * 2;\n }\n ans = std::cmp::min(ans, s.len() - res);\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0698", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)

\n

Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?

\n

(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq X \\leq 10^9
  • \n
  • X is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

Print the maximum number of happiness points that can be earned.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1024\n
\n
\n
\n
\n
\n

Sample Output 1

2020\n
\n

By exchanging his money so that he gets two 500-yen coins and four 5-yen coins, he gains 2020 happiness points, which is the maximum number of happiness points that can be earned.

\n
\n
\n
\n
\n
\n

Sample Input 2

0\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

He is penniless - or yenless.

\n
\n
\n
\n
\n
\n

Sample Input 3

1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

2000000000\n
\n

He is a billionaire - in yen.

\n
\n
", "c_code": "int solution(void) {\n int money = 0;\n scanf(\"%d\", &money);\n int big_gold = money / 500;\n money -= 500 * big_gold;\n int small_gold = money / 5;\n int point = (big_gold * 1000) + (small_gold * 5);\n printf(\"%d\", point);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let mut x = s.trim().parse::().unwrap();\n let ans = x / 500 * 1000;\n x %= 500;\n x = x / 5 * 5;\n\n println!(\"{}\", ans + x);\n}", "difficulty": "easy"} {"problem_id": "0699", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Takahashi is at an all-you-can-eat restaurant.

\n

The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.

\n

The restaurant has the following rules:

\n
    \n
  • You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
  • \n
  • You cannot order the same kind of dish more than once.
  • \n
  • Until you finish eating the dish already served, you cannot order a new dish.
  • \n
  • After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
  • \n
\n

Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.

\n

What is the maximum possible happiness achieved by making optimal choices?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 3000
  • \n
  • 1 \\leq T \\leq 3000
  • \n
  • 1 \\leq A_i \\leq 3000
  • \n
  • 1 \\leq B_i \\leq 3000
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N T\nA_1 B_1\n:\nA_N B_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible happiness Takahashi can achieve.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 60\n10 10\n100 100\n
\n
\n
\n
\n
\n

Sample Output 1

110\n
\n

By ordering the first and second dishes in this order, Takahashi's happiness will be 110.

\n

Note that, if we manage to order a dish in time, we can spend any amount of time to eat it.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 60\n10 10\n10 20\n10 30\n
\n
\n
\n
\n
\n

Sample Output 2

60\n
\n

Takahashi can eat all the dishes within 60 minutes.

\n
\n
\n
\n
\n
\n

Sample Input 3

3 60\n30 10\n30 20\n30 30\n
\n
\n
\n
\n
\n

Sample Output 3

50\n
\n

By ordering the second and third dishes in this order, Takahashi's happiness will be 50.

\n

We cannot order three dishes, in whatever order we place them.

\n
\n
\n
\n
\n
\n

Sample Input 4

10 100\n15 23\n20 18\n13 17\n24 12\n18 29\n19 27\n23 21\n18 20\n27 15\n22 25\n
\n
\n
\n
\n
\n

Sample Output 4

145\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n long t;\n scanf(\"%ld %ld\", &n, &t);\n long a[n];\n long b[n];\n for (long i = 0; i < n; i++) {\n scanf(\"%ld %ld\", &a[i], &b[i]);\n }\n long dp1[n][t];\n long dp2[n][t];\n for (long i = 0; i < t; i++) {\n dp1[0][i] = 0;\n }\n if (a[0] < t) {\n dp1[0][a[0]] = b[0];\n }\n for (long i = 1; i < n; i++) {\n for (long j = 0; j < t; j++) {\n dp1[i][j] = dp1[i - 1][j];\n }\n for (long j = 0; j < t; j++) {\n if (j + a[i] >= t) {\n break;\n }\n if (dp1[i - 1][j] + b[i] > dp1[i][j + a[i]]) {\n dp1[i][j + a[i]] = dp1[i - 1][j] + b[i];\n }\n }\n }\n for (long i = 0; i < t; i++) {\n dp2[n - 1][i] = 0;\n }\n if (a[n - 1] < t) {\n dp2[n - 1][a[n - 1]] = b[n - 1];\n }\n for (long i = n - 2; i >= 0; i--) {\n for (long j = 0; j < t; j++) {\n dp2[i][j] = dp2[i + 1][j];\n }\n for (long j = 0; j < t; j++) {\n if (j + a[i] >= t) {\n break;\n }\n if (dp2[i + 1][j] + b[i] > dp2[i][j + a[i]]) {\n dp2[i][j + a[i]] = dp2[i + 1][j] + b[i];\n }\n }\n }\n for (long i = 0; i < n; i++) {\n for (long j = 1; j < t; j++) {\n if (dp1[i][j] < dp1[i][j - 1]) {\n dp1[i][j] = dp1[i][j - 1];\n }\n if (dp2[i][j] < dp2[i][j - 1]) {\n dp2[i][j] = dp2[i][j - 1];\n }\n }\n }\n long score;\n long max = 0;\n for (long last = 0; last < n; last++) {\n score = 0;\n if (last == 0) {\n for (long i = 0; i < t; i++) {\n if (dp2[1][i] > score) {\n score = dp2[1][i];\n }\n }\n } else if (last == n - 1) {\n for (long i = 0; i < t; i++) {\n if (dp1[n - 2][i] > score) {\n score = dp1[n - 2][i];\n }\n }\n } else {\n for (long i = 0; i < t; i++) {\n if (dp1[last - 1][i] + dp2[last + 1][t - 1 - i] > score) {\n score = dp1[last - 1][i] + dp2[last + 1][t - 1 - i];\n }\n }\n }\n score += b[last];\n if (score > max) {\n max = score;\n }\n }\n printf(\"%ld\\n\", max);\n\n return 0;\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 t: usize = itr.next().unwrap().parse().unwrap();\n let mut wv: Vec<(usize, usize)> = (0..n)\n .map(|_| {\n (\n itr.next().unwrap().parse().unwrap(),\n itr.next().unwrap().parse().unwrap(),\n )\n })\n .collect();\n wv.sort();\n\n let mut dp = vec![0; t + 1];\n for i in 0..n {\n for j in (0..t).rev() {\n dp[j] = max(dp[j], dp[j]);\n dp[min(t, j + wv[i].0)] = max(dp[min(t, j + wv[i].0)], dp[j] + wv[i].1);\n }\n }\n println!(\"{}\", dp[t]);\n}", "difficulty": "medium"} {"problem_id": "0700", "problem_description": "You are given an integer $$$x$$$ and an array of integers $$$a_1, a_2, \\ldots, a_n$$$. You have to determine if the number $$$a_1! + a_2! + \\ldots + a_n!$$$ is divisible by $$$x!$$$.Here $$$k!$$$ is a factorial of $$$k$$$ — the product of all positive integers less than or equal to $$$k$$$. For example, $$$3! = 1 \\cdot 2 \\cdot 3 = 6$$$, and $$$5! = 1 \\cdot 2 \\cdot 3 \\cdot 4 \\cdot 5 = 120$$$.", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int count[k + 1];\n for (int i = 0; i < k + 1; i++) {\n count[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n count[a[i]]++;\n }\n int ans = 1;\n for (int i = 0; i < k; i++) {\n if (count[i] % (i + 1) == 0) {\n count[i + 1] = count[i + 1] + count[i] / (i + 1);\n } else {\n ans = 0;\n break;\n }\n }\n if (ans == 1) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n x: usize,\n a: [usize; n],\n }\n let mut count = vec![0; 500_002];\n for i in 0..n {\n count[a[i]] += 1;\n }\n for i in 1..=500_000 {\n count[i + 1] += count[i] / (i + 1);\n count[i] %= i + 1;\n }\n if count[..x].iter().all(|&c| c == 0) {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0701", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N towns in a coordinate plane. Town i is located at coordinates (x_i, y_i). The distance between Town i and Town j is \\sqrt{\\left(x_i-x_j\\right)^2+\\left(y_i-y_j\\right)^2}.

\n

There are N! possible paths to visit all of these towns once. Let the length of a path be the distance covered when we start at the first town in the path, visit the second, third, \\dots, towns, and arrive at the last town (assume that we travel in a straight line from a town to another). Compute the average length of these N! paths.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 8
  • \n
  • -1000 \\leq x_i \\leq 1000
  • \n
  • -1000 \\leq y_i \\leq 1000
  • \n
  • \\left(x_i, y_i\\right) \\neq \\left(x_j, y_j\\right) (if i \\neq j)
  • \n
  • (Added 21:12 JST) All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nx_1 y_1\n:\nx_N y_N\n
\n
\n
\n
\n
\n

Output

Print the average length of the paths.\nYour output will be judges as correct when the absolute difference from the judge's output is at most 10^{-6}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n0 0\n1 0\n0 1\n
\n
\n
\n
\n
\n

Sample Output 1

2.2761423749\n
\n

There are six paths to visit the towns: 123, 132, 213, 231, 312, and 321.

\n

The length of the path 123 is \\sqrt{\\left(0-1\\right)^2+\\left(0-0\\right)^2} + \\sqrt{\\left(1-0\\right)^2+\\left(0-1\\right)^2} = 1+\\sqrt{2}.

\n

By calculating the lengths of the other paths in this way, we see that the average length of all routes is:

\n

\\frac{\\left(1+\\sqrt{2}\\right)+\\left(1+\\sqrt{2}\\right)+\\left(2\\right)+\\left(1+\\sqrt{2}\\right)+\\left(2\\right)+\\left(1+\\sqrt{2}\\right)}{6} = 2.276142...

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n-879 981\n-866 890\n
\n
\n
\n
\n
\n

Sample Output 2

91.9238815543\n
\n

There are two paths to visit the towns: 12 and 21. These paths have the same length.

\n
\n
\n
\n
\n
\n

Sample Input 3

8\n-406 10\n512 859\n494 362\n-955 -475\n128 553\n-986 -885\n763 77\n449 310\n
\n
\n
\n
\n
\n

Sample Output 3

7641.9817824387\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int p = 1;\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n p *= i;\n }\n\n int dis[n][2];\n double sum = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\"\n \"%d\",\n &dis[i][0], &dis[i][1]);\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (i != j) {\n\n sum += sqrt(((dis[i][0] - dis[j][0]) * (dis[i][0] - dis[j][0])) +\n ((dis[i][1] - dis[j][1]) * (dis[i][1] - dis[j][1]))) *\n p / n;\n }\n }\n }\n\n printf(\"%lf\\n\", sum / p);\n return 0;\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 x = Vec::new();\n let mut y = Vec::new();\n for _ in 0..n {\n let x_y = lines.next().unwrap().unwrap();\n let mut x_y = x_y.split_whitespace();\n x.push(x_y.next().unwrap().parse::().unwrap());\n y.push(x_y.next().unwrap().parse::().unwrap());\n }\n\n let mut ans = 0.0;\n for i in 0..n {\n for j in 0..n {\n let d = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);\n ans += d.sqrt();\n }\n }\n ans /= n as f64;\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0702", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.
\nGiven a string S, determine whether it is coffee-like.

\n
\n
\n
\n
\n

Constraints

    \n
  • S is a string of length 6 consisting of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If S is coffee-like, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

sippuu\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

\n
\n
\n
\n
\n
\n

Sample Input 2

iphone\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

coffee\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution(void) {\n char s[7];\n scanf(\"%s\", s);\n if ((*(s + 2) == *(s + 3)) && (*(s + 4) == *(s + 5))) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let coffee: Vec = s.trim().chars().collect();\n\n if coffee[2] == coffee[3] && coffee[4] == coffee[5] {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0703", "problem_description": "Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness.", "c_code": "int solution() {\n int n;\n int countEven = 0;\n int countOdd = 0;\n int a = 0;\n int b = 0;\n int result = 0;\n int num[101];\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &num[i]);\n }\n for (int j = 1; j <= n; j++) {\n if (num[j] % 2 == 0) {\n countEven++;\n if (countEven == 1) {\n b = j;\n }\n if (countEven > 1 && countOdd == 1) {\n result = a;\n break;\n }\n if (countOdd > 1 && countEven == 1) {\n result = b;\n break;\n }\n }\n if (num[j] % 2 != 0) {\n countOdd++;\n if (countOdd == 1) {\n a = j;\n }\n if (countOdd > 1 && countEven == 1) {\n result = b;\n break;\n }\n if (countEven > 1 && countOdd == 1) {\n result = a;\n break;\n }\n }\n }\n printf(\"%d\", result);\n return 0;\n}", "rust_code": "fn solution() {\n io::stdin().read_line(&mut String::new()).unwrap();\n\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let guess: Vec = words.clone().into_iter().filter(|x| x % 2 == 0).collect();\n\n let mut answer = 1;\n\n let mut evenness = 0;\n\n if guess.len() > 1 {\n evenness = 1;\n }\n\n for (idx, item) in words.into_iter().enumerate() {\n if item % 2 == evenness {\n answer = idx;\n }\n }\n\n println!(\"{}\", answer + 1);\n}", "difficulty": "medium"} {"problem_id": "0704", "problem_description": "You are given a rectangular grid with $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{ij}$$$ written in it.You can perform the following operation any number of times (possibly zero): Choose any two adjacent cells and multiply the values in them by $$$-1$$$. Two cells are called adjacent if they share a side. Note that you can use a cell more than once in different operations.You are interested in $$$X$$$, the sum of all the numbers in the grid. What is the maximum $$$X$$$ you can achieve with these operations?", "c_code": "int solution() {\n int d[10][10];\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int m;\n int cnt1 = 0;\n int cnt_1 = 0;\n int cnt0 = 0;\n int sum = 0;\n int minv = 101;\n scanf(\"%d%d\", &n, &m);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &d[i][j]);\n if (d[i][j] > 0) {\n cnt1++;\n sum += d[i][j];\n if (minv > d[i][j]) {\n minv = d[i][j];\n }\n } else if (d[i][j] < 0) {\n cnt_1++;\n sum += -d[i][j];\n if (minv > -d[i][j]) {\n minv = -d[i][j];\n }\n } else {\n cnt0++;\n }\n }\n }\n\n if ((cnt_1 & 1) && cnt0 == 0) {\n sum += -2 * minv;\n }\n\n printf(\"%d\\n\", sum);\n }\n}", "rust_code": "fn solution() {\n let stream = io::stdin();\n\n let mut line = String::new();\n stream.read_line(&mut line).expect(\"Missing t\");\n let t: i32 = line.trim().parse().expect(\"t is not a number\");\n\n for _n in 0..t {\n let mut test_case_line = String::new();\n stream\n .read_line(&mut test_case_line)\n .expect(\"Missing number\");\n\n let mut n: i32 = 0;\n let mut m: i32 = 0;\n for val in test_case_line.split(\" \") {\n let num: i32 = val.trim().parse().expect(\"Invalid format\");\n if n == 0 {\n n = num;\n } else {\n m = num;\n }\n }\n\n let mut min_positive_number = 101;\n let mut negative_number_count = 0;\n let mut result = 0;\n\n for _i in 0..n {\n let mut l = String::new();\n stream.read_line(&mut l).expect(\"Missing line\");\n\n let mut v: Vec = Vec::new();\n for val in l.trim().split(\" \") {\n v.push(val.parse().expect(\"Invalid format\"));\n }\n\n for mut val in v {\n if val < 0 {\n val = -val;\n negative_number_count += 1;\n }\n\n min_positive_number = min(min_positive_number, val);\n result += val;\n }\n }\n\n if negative_number_count % 2 == 1 {\n result -= 2 * min_positive_number;\n }\n\n println!(\"{}\", result)\n }\n}", "difficulty": "hard"} {"problem_id": "0705", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.\nThere is a bishop at the top-left square on this board.\nHow many squares can this bishop reach by zero or more movements?

\n

Here the bishop can only move diagonally.\nMore formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

\n
    \n
  • r_1 + c_1 = r_2 + c_2
  • \n
  • r_1 - c_1 = r_2 - c_2
  • \n
\n

For example, in the following figure, the bishop can move to any of the red squares in one move:

\n

\"\"

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H, W \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H \\ W\n
\n
\n
\n
\n
\n

Output

Print the number of squares the bishop can reach.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 5\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

The bishop can reach the cyan squares in the following figure:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 2

7 3\n
\n
\n
\n
\n
\n

Sample Output 2

11\n
\n

The bishop can reach the cyan squares in the following figure:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 3

1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

500000000000000000\n
\n
\n
", "c_code": "int solution() {\n long long int lli_CellHeight = 0;\n long long int lli_CellWidth = 0;\n\n (void)scanf(\"%lld %lld\", &lli_CellHeight, &lli_CellWidth);\n\n long long int lli_Ans = (lli_CellHeight * lli_CellWidth) / 2;\n if ((lli_CellHeight % 2 == 1) && (lli_CellWidth % 2 == 1)) {\n lli_Ans += 1;\n }\n if ((lli_CellHeight == 1) || (lli_CellWidth == 1)) {\n lli_Ans = 1;\n }\n\n (void)printf(\"%lld\\n\", lli_Ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let input_vec: Vec = buffer\n .split_whitespace()\n .map(|c| c.parse().unwrap())\n .collect();\n\n let height = input_vec[0];\n let width = input_vec[1];\n if height == 1 || width == 1 {\n println!(\"{}\", 1);\n } else {\n println!(\"{}\", (width * height + 1) / 2);\n }\n}", "difficulty": "medium"} {"problem_id": "0706", "problem_description": "Define the score of some binary string $$$T$$$ as the absolute difference between the number of zeroes and ones in it. (for example, $$$T=$$$ 010001 contains $$$4$$$ zeroes and $$$2$$$ ones, so the score of $$$T$$$ is $$$|4-2| = 2$$$).Define the creepiness of some binary string $$$S$$$ as the maximum score among all of its prefixes (for example, the creepiness of $$$S=$$$ 01001 is equal to $$$2$$$ because the score of the prefix $$$S[1 \\ldots 4]$$$ is $$$2$$$ and the rest of the prefixes have a score of $$$2$$$ or less).Given two integers $$$a$$$ and $$$b$$$, construct a binary string consisting of $$$a$$$ zeroes and $$$b$$$ ones with the minimum possible creepiness.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int b[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d%d\", &a[i], &b[i]);\n }\n for (int i = 0; i < n; i++) {\n if (a[i] > b[i]) {\n for (int j = 0; j < (a[i] - b[i]); j++) {\n printf(\"0\");\n }\n for (int j = 0; j < b[i]; j++) {\n printf(\"10\");\n }\n printf(\"\\n\");\n } else {\n for (int j = 0; j < (b[i] - a[i]); j++) {\n printf(\"1\");\n }\n for (int j = 0; j < a[i]; j++) {\n printf(\"01\");\n }\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin().read_to_string(&mut content);\n\n let mut lines = content.lines();\n let num_tests: i32 = lines.next().unwrap().parse().unwrap();\n for _ in 0..num_tests {\n let arr: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse().unwrap())\n .collect();\n let a = arr[0];\n let b = arr[1];\n let mut result = \"01\".repeat(min(a, b));\n if a < b {\n result += &\"1\".repeat(b - a);\n } else {\n result += &\"0\".repeat(a - b);\n }\n println!(\"{result}\");\n }\n}", "difficulty": "easy"} {"problem_id": "0707", "problem_description": "Vanya walks late at night along a straight street of length l, lit by n lanterns. Consider the coordinate system with the beginning of the street corresponding to the point 0, and its end corresponding to the point l. Then the i-th lantern is at the point ai. The lantern lights all points of the street that are at the distance of at most d from it, where d is some positive number, common for all lanterns. Vanya wonders: what is the minimum light radius d should the lanterns have to light the whole street?", "c_code": "int solution() {\n int d = 0;\n int l;\n\n scanf(\"%d %d\", &d, &l);\n\n int lanterna[d];\n\n for (int i = 0; i < d; i++) {\n scanf(\"%d\", &lanterna[i]);\n }\n for (int j = d - 1; j >= 1; j--) {\n for (int k = 0; k < j; k++) {\n if (lanterna[k] > lanterna[k + 1]) {\n int maior = lanterna[k];\n lanterna[k] = lanterna[k + 1];\n lanterna[k + 1] = maior;\n }\n }\n }\n\n double raio = lanterna[0] * 2;\n double aux = (l - lanterna[d - 1]) * 2;\n if (raio < aux) {\n raio = aux;\n }\n\n for (int i = 0; i < d - 1; i++) {\n if (raio < lanterna[i + 1] - lanterna[i]) {\n raio = lanterna[i + 1] - lanterna[i];\n }\n }\n printf(\"%f\", raio / 2);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let arr: Vec = s.split_whitespace().map(|s| s.parse().unwrap()).collect();\n let (_n, k) = (arr[0], arr[1]);\n let mut v = vec![0, k];\n s.clear();\n io::stdin().read_line(&mut s).unwrap();\n let arr: Vec = s.split_whitespace().map(|s| s.parse().unwrap()).collect();\n for c in arr {\n v.push(c);\n }\n v.sort();\n let mut ans = -1;\n for i in 1..v.len() {\n let mut tmp = v[i] - v[(i - 1)];\n if i == 1 || i == v.len() - 1 {\n tmp *= 2;\n }\n if ans < tmp {\n ans = tmp;\n }\n }\n println!(\"{}\", ans as f64 / 2.0);\n}", "difficulty": "hard"} {"problem_id": "0708", "problem_description": "Radix sort", "c_code": "void solution(unsigned long long arr[], size_t n) {\n if (n == 0)\n return;\n\n unsigned long long max = arr[0];\n\n for (size_t i = 1; i < n; i++) {\n if (arr[i] > max)\n max = arr[i];\n }\n\n size_t radix = 1;\n\n while (radix < n) {\n radix <<= 1;\n }\n\n unsigned long long *output = malloc(n * sizeof(unsigned long long));\n\n size_t *counter = malloc(radix * sizeof(size_t));\n\n if (!output || !counter) {\n free(output);\n free(counter);\n return;\n }\n\n unsigned long long place = 1;\n\n while (place <= max) {\n\n memset(counter, 0, radix * sizeof(size_t));\n\n for (size_t i = 0; i < n; i++) {\n size_t digit = (arr[i] / place) % radix;\n\n counter[digit]++;\n }\n\n for (size_t i = 1; i < radix; i++) {\n counter[i] += counter[i - 1];\n }\n\n for (size_t i = n; i > 0; i--) {\n unsigned long long value = arr[i - 1];\n\n size_t digit = (value / place) % radix;\n\n counter[digit]--;\n\n output[counter[digit]] = value;\n }\n\n memcpy(arr, output, n * sizeof(unsigned long long));\n\n place *= radix;\n }\n\n free(output);\n free(counter);\n}\n", "rust_code": "fn solution(arr: &mut [u64]) {\n let max: usize = match arr.iter().max() {\n Some(&x) => x as usize,\n None => return,\n };\n\n let radix = arr.len().next_power_of_two();\n\n let mut place = 1;\n while place <= max {\n let digit_of = |x| x as usize / place % radix;\n\n let mut counter = vec![0; radix];\n for &x in arr.iter() {\n counter[digit_of(x)] += 1;\n }\n\n for i in 1..radix {\n counter[i] += counter[i - 1];\n }\n\n for &x in arr.to_owned().iter().rev() {\n counter[digit_of(x)] -= 1;\n arr[counter[digit_of(x)]] = x;\n }\n place *= radix;\n }\n}", "difficulty": "easy"} {"problem_id": "0709", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

\n

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \\cdots, A_{i, {d_i}}.

\n

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq K \\leq 100
  • \n
  • 1 \\leq d_i \\leq N
  • \n
  • 1 \\leq A_{i, 1} < \\cdots < A_{i, d_i} \\leq N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nd_1\nA_{1, 1} \\cdots A_{1, d_1}\n\\vdots\nd_K\nA_{K, 1} \\cdots A_{K, d_K}\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n2\n1 3\n1\n3\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n
    \n
  • Snuke 1 has Snack 1.
  • \n
  • Snuke 2 has no snacks.
  • \n
  • Snuke 3 has Snack 1 and 2.
  • \n
\n

Thus, there will be one victim: Snuke 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3\n1\n3\n1\n3\n1\n3\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int k = 0;\n int a[101] = {0};\n int c = 100;\n scanf(\"%d %d\", &n, &k);\n c = n;\n int i = 1;\n for (i = 1; i < k + 1; i++) {\n int d = 0;\n scanf(\"%d\", &d);\n\n int j = 1;\n for (j = 1; j < d + 1; j++) {\n int ai = 0;\n scanf(\"%d\", &ai);\n a[ai]++;\n if (a[ai] == 1) {\n c--;\n }\n }\n }\n\n printf(\"%d\\n\", c);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let mut itr = s.split_whitespace().map(|e| e.parse().unwrap());\n let n: usize = itr.next().unwrap();\n let k: usize = itr.next().unwrap();\n let mut set = HashSet::new();\n\n for _ in 0..k {\n s.clear();\n stdin().read_line(&mut s).ok();\n let _d: usize = s.trim().parse().unwrap();\n s.clear();\n stdin().read_line(&mut s).ok();\n let itr = s.split_whitespace().map(|e| e.parse::().unwrap());\n for e in itr {\n set.insert(e);\n }\n }\n\n let mut ans = 0;\n for i in 1..=n {\n if !set.contains(&i) {\n ans += 1;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0710", "problem_description": "You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.", "c_code": "int solution() {\n int n;\n int c = 0;\n scanf(\"%d\", &n);\n int ar[n][2];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 2; j++) {\n scanf(\"%d\", &ar[i][j]);\n }\n }\n\n for (int i = 0; i < n; i++) {\n c = 0;\n int k = ar[i][1];\n for (int j = 0; j < ar[i][0]; j++) {\n printf(\"%c\", (char)c + 97);\n c++;\n if (c == k) {\n c = 0;\n }\n }\n printf(\"\\n\");\n }\n return (0);\n}", "rust_code": "fn solution() {\n for _ in 0..({\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n line.trim().parse::().unwrap()\n }) {\n let (n, k) = {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let s: Vec = line.trim().split(' ').map(|s| s.parse().unwrap()).collect();\n (s[0], s[1])\n };\n\n for i in 0..n {\n print!(\"{}\", char::from_u32(((i % k) + 97) as u32).unwrap());\n }\n\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0711", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below:

\n
    \n
  • Each person simultaneously divides his cookies in half and gives one half to each of the other two persons.
  • \n
\n

This action will be repeated until there is a person with odd number of cookies in hand.

\n

How many times will they repeat this action?\nNote that the answer may not be finite.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ A,B,C ≤ 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print the number of times the action will be performed by the three people, if this number is finite.\nIf it is infinite, print -1 instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 12 20\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

Initially, Takahashi, Aoki and Snuke have 4, 12 and 20 cookies. Then,

\n
    \n
  • After the first action, they have 16, 12 and 8.
  • \n
  • After the second action, they have 10, 12 and 14.
  • \n
  • After the third action, they have 13, 12 and 11.
  • \n
\n

Now, Takahashi and Snuke have odd number of cookies, and therefore the answer is 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

14 14 14\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

454 414 444\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution() {\n int i = 0;\n double a[4];\n double b[4];\n\n scanf(\"%lf %lf %lf\", &a[0], &a[1], &a[2]);\n\n if (a[0] == a[1] && a[1] == a[2] && (long long int)a[0] % 2 == 0 &&\n (long long int)a[1] % 2 == 0 && (int long long)a[2] % 2 == 0) {\n printf(\"-1\");\n }\n\n else {\n while ((long long int)a[0] % 2 == 0 && (long long int)a[1] % 2 == 0 &&\n (int long long)a[2] % 2 == 0) {\n\n b[0] = a[0];\n b[1] = a[1];\n b[2] = a[2];\n\n a[0] = b[1] / 2 + b[2] / 2;\n a[1] = b[2] / 2 + b[0] / 2;\n a[2] = b[0] / 2 + b[1] / 2;\n\n i++;\n }\n printf(\"%d\", i);\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let v: Vec = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let (mut a, mut b, mut c) = (v[0], v[1], v[2]);\n let mut ans = 0_i32;\n loop {\n if a % 2 == 1 || b % 2 == 1 || c % 2 == 1 {\n break;\n }\n if a == b && b == c {\n ans = -1;\n break;\n }\n let (a_tmp, b_tmp, c_tmp) = (a, b, c);\n a = (b_tmp + c_tmp) / 2;\n b = (c_tmp + a_tmp) / 2;\n c = (a_tmp + b_tmp) / 2;\n ans += 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0712", "problem_description": "You are a usual chat user on the most famous streaming platform. Of course, there are some moments when you just want to chill and spam something.More precisely, you want to spam the emote triangle of size $$$k$$$. It consists of $$$2k-1$$$ messages. The first message consists of one emote, the second one — of two emotes, ..., the $$$k$$$-th one — of $$$k$$$ emotes, the $$$k+1$$$-th one — of $$$k-1$$$ emotes, ..., and the last one — of one emote.For example, the emote triangle for $$$k=3$$$ consists of $$$5$$$ messages: Of course, most of the channels have auto moderation. Auto moderator of the current chat will ban you right after you spam at least $$$x$$$ emotes in succession (you can assume you are the only user in the chat). Now you are interested — how many messages will you write before getting banned? Or maybe you will not get banned at all (i.e. will write all $$$2k-1$$$ messages and complete your emote triangle successfully)? Note that if you get banned as a result of writing a message, this message is also counted.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long k;\n long long x;\n scanf(\"%lld%lld\", &k, &x);\n if (2 * x < k * k + k) {\n long long letsgo = (long long)(int)ceil((sqrt((8 * x) + 1) - 1) / 2);\n if (letsgo * (letsgo + 1) / 2 >= x && (letsgo - 1) * (letsgo) / 2 < x) {\n printf(\"%lld\\n\", letsgo);\n } else {\n printf(\"%lld\\n\", letsgo + 1);\n }\n } else {\n x -= (k + 1) * k / 2;\n long long tmpval = ((k - 1) * k / 2) - x;\n long long letsgo = (long long)(sqrt((8 * tmpval) + 1) - (double)1) / 2;\n if (2 * x < k * k - k) {\n if (letsgo * (letsgo + 1) / 2 <= tmpval &&\n (letsgo + 1) * (letsgo + 2) / 2 > tmpval) {\n printf(\"%lld\\n\", (2 * k) - 1 - letsgo);\n } else {\n printf(\"%lld\\n\", (2 * k) - letsgo);\n }\n } else {\n printf(\"%lld\\n\", (2 * k) - 1);\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut testcases = String::new();\n io::stdin().read_line(&mut testcases).expect(\"\");\n let testcases: usize = testcases.trim().parse().unwrap();\n\n for _testcase in 0..testcases {\n let mut input = String::new();\n io::stdin().read_line(&mut input).expect(\"\");\n\n let (k, x): (i64, i64) = match input.trim().split(\" \").collect::>()[..] {\n [first, second] => (first.parse().unwrap(), second.parse().unwrap()),\n _ => panic!(\"AH\"),\n };\n\n let k_sum_graph = |t: i64| -> i64 {\n if t <= k {\n (t * t + t) / 2\n } else {\n (-(t.pow(2)) + (4 * k - 1) * t - (2 * k.pow(2) - 2 * k)) / 2\n }\n };\n\n let mut low = 0;\n let mut mid = 0;\n let mut high = 2 * k - 1;\n\n while low <= high {\n mid = low + (high - low) / 2;\n\n let s = k_sum_graph(mid);\n\n if s > x {\n high = mid - 1;\n } else if s < x {\n if k_sum_graph(mid + 1) >= x {\n mid += 1;\n break;\n }\n low = mid + 1;\n } else {\n break;\n }\n }\n\n println!(\"{}\", mid);\n }\n}", "difficulty": "medium"} {"problem_id": "0713", "problem_description": "There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \\le j \\le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int ar[n + 1][2];\n ar[0][0] = 0;\n ar[0][1] = 0;\n for (int i = 1; i <= n; i++) {\n scanf(\"%d%d\", &ar[i][0], &ar[i][1]);\n }\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= n - 1; j++) {\n if (ar[j + 1][0] < ar[j][0]) {\n int t = ar[j + 1][0];\n ar[j + 1][0] = ar[j][0];\n ar[j][0] = t;\n int tt = ar[j + 1][1];\n ar[j + 1][1] = ar[j][1];\n ar[j][1] = tt;\n }\n }\n }\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= n - 1; j++) {\n if (ar[j + 1][1] < ar[j][1] && ar[j + 1][0] == ar[j][0]) {\n int t = ar[j + 1][1];\n ar[j + 1][1] = ar[j][1];\n ar[j][1] = t;\n }\n }\n }\n\n int f = 0;\n for (int i = 0; i < n; i++) {\n if (ar[i][0] > ar[i + 1][0] || ar[i][1] > ar[i + 1][1]) {\n f = 1;\n break;\n }\n }\n if (f == 1) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n for (int i = 0; i < n; i++) {\n int cnt = ar[i + 1][0] - ar[i][0];\n while (cnt--) {\n printf(\"R\");\n }\n int ccnt = ar[i + 1][1] - ar[i][1];\n while (ccnt--) {\n printf(\"U\");\n }\n }\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut lines = stdin.lock().lines();\n\n let t = lines.next().unwrap().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let n = lines.next().unwrap().unwrap().parse::().unwrap();\n let mut x: Vec<(usize, usize)> = lines\n .by_ref()\n .take(n)\n .map(|line| {\n let line = line.unwrap();\n let mut v = line.split(' ').map(|x| x.parse::().unwrap());\n let a = v.next().unwrap();\n let b = v.next().unwrap();\n (a, b)\n })\n .collect();\n\n x.sort_unstable_by_key(|k| k.0);\n x.sort_by_key(|k| k.1);\n\n let mut xy = Some((0, 0));\n let mut path = String::new();\n\n for p in x {\n if let Some(cur) = xy {\n if p.0 >= cur.0 && p.1 >= cur.1 {\n for _ in 0..p.0 - cur.0 {\n path.push('R');\n }\n for _ in 0..p.1 - cur.1 {\n path.push('U');\n }\n\n xy = Some(p);\n } else {\n xy = None;\n }\n }\n }\n\n if xy.is_some() {\n println!(\"YES\\n{}\", path);\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0714", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

N people are arranged in a row from left to right.

\n

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

\n

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

\n

You will give the following direction at most K times (possibly zero):

\n

Direction: Choose integers l and r satisfying 1 \\leq l \\leq r \\leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

\n

Find the maximum possible number of consecutive people standing on hands after at most K directions.

\n
\n
\n
\n
\n

Constraints

    \n
  • N is an integer satisfying 1 \\leq N \\leq 10^5.
  • \n
  • K is an integer satisfying 1 \\leq K \\leq 10^5.
  • \n
  • The length of the string S is N.
  • \n
  • Each character of the string S is 0 or 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nS\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 1\n00010\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

\n
    \n
  • Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

14 2\n11101010110011\n
\n
\n
\n
\n
\n

Sample Output 2

8\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 1\n1\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n

No directions are necessary.

\n
\n
", "c_code": "int solution(void) {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n char s[n + 1];\n scanf(\"%s\", s);\n int l = 0;\n int r = 0;\n while (r < n && s[r] == '1') {\n r++;\n }\n while (r < n && k > 0) {\n while (r < n && s[r] == '0') {\n r++;\n }\n while (r < n && s[r] == '1') {\n r++;\n }\n k--;\n }\n int ans = r - l;\n while (r < n) {\n while (r < n && s[r] == '0') {\n r++;\n }\n while (r < n && s[r] == '1') {\n r++;\n }\n while (l < n && s[l] == '1') {\n l++;\n }\n while (l < n && s[l] == '0') {\n l++;\n }\n if (ans < r - l) {\n ans = r - l;\n }\n }\n printf(\"%d\\n\", ans);\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut s = String::new();\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let _n: usize = s.next().unwrap().parse().unwrap();\n let k: usize = s.next().unwrap().parse().unwrap();\n let s: Vec = s.next().unwrap().chars().collect();\n let mut l = 0;\n let mut r = 0;\n let mut cnt = 0;\n let mut ans = 0;\n while l < s.len() {\n while r < s.len() {\n if s[r] == '0' {\n if cnt >= k {\n break;\n }\n while r < s.len() && s[r] == '0' {\n r += 1;\n }\n cnt += 1;\n } else {\n r += 1;\n }\n }\n if r - l > ans {\n ans = r - l;\n }\n if s[l] == '1' {\n while l < s.len() && s[l] == '1' {\n l += 1;\n }\n }\n if l < s.len() {\n cnt = cnt.saturating_sub(1);\n while l < s.len() && s[l] == '0' {\n l += 1;\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0715", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Snuke has an integer sequence A of length N.

\n

He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E.\nThe positions of the cuts can be freely chosen.

\n

Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively.\nSnuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller.\nFind the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

\n
\n
\n
\n
\n

Constraints

    \n
  • 4 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n3 2 4 1 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

If we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3.\nHere, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2.\nWe cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\n10 71 84 33 6 47 23 25 52 64\n
\n
\n
\n
\n
\n

Sample Output 2

36\n
\n
\n
\n
\n
\n
\n

Sample Input 3

7\n1 2 3 1000000000 4 5 6\n
\n
\n
\n
\n
\n

Sample Output 3

999999994\n
\n
\n
", "c_code": "int solution(void) {\n long n;\n long a[200000];\n long i;\n long j;\n long k;\n long as[200000];\n long suml;\n long sumr;\n long p;\n long q;\n long r;\n long s;\n long ans;\n long temp;\n long max;\n long min;\n long maxl;\n long minl;\n long maxr;\n long minr;\n\n scanf(\"%ld\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%ld\", a + i);\n }\n\n for (i = 0; i < n; i++) {\n if (i == 0) {\n as[0] = a[0];\n } else {\n as[i] = as[i - 1] + a[i];\n }\n }\n\n ans = as[n - 1];\n\n i = 0;\n k = 2;\n for (j = 1; j <= n - 3; j++) {\n suml = as[j];\n for (; i <= j - 1; i++) {\n if (as[i] >= (suml + 1) / 2) {\n break;\n }\n }\n if (i != 0 &&\n labs(as[i - 1] - (suml / 2)) <= labs(as[i] - ((suml + 1) / 2))) {\n i--;\n }\n\n sumr = as[n - 1] - as[j];\n if (j >= k) {\n k = j + 1;\n }\n for (; k <= n - 2; k++) {\n if (as[k] - as[j] >= (sumr + 1) / 2) {\n break;\n }\n }\n if (k != j + 1 && labs(as[k - 1] - as[j] - (sumr / 2)) <=\n labs(as[k] - as[j] - ((sumr + 1) / 2))) {\n k--;\n }\n\n p = as[i];\n q = as[j] - as[i];\n r = as[k] - as[j];\n s = as[n - 1] - as[k];\n maxl = (p > q) ? p : q;\n minl = (p < q) ? p : q;\n maxr = (r > s) ? r : s;\n minr = (r < s) ? r : s;\n max = (maxl > maxr) ? maxl : maxr;\n min = (minl < minr) ? minl : minr;\n temp = max - min;\n if (ans >= temp) {\n ans = temp;\n }\n }\n\n printf(\"%ld\", 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 a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let mut s = vec![0i64; n + 1];\n for i in 0..n {\n s[i + 1] = s[i] + a[i];\n }\n\n let mut ans = 1i64 << 60;\n\n let mut p = 1;\n let mut r = 3;\n for q in 2..n - 1 {\n let mut P = s[p];\n let mut Q = s[q] - s[p];\n let mut R = s[r] - s[q];\n let mut S = s[n] - s[r];\n while p + 1 < q && (s[q] - s[p + 1] - s[p + 1]).abs() <= (Q - P).abs() {\n p += 1;\n P = s[p];\n Q = s[q] - s[p];\n }\n while r + 1 < n && (s[n] - s[r + 1] - s[r + 1] + s[q]).abs() <= (S - R).abs() {\n r += 1;\n R = s[r] - s[q];\n S = s[n] - s[r];\n }\n ans = min(ans, max(max(max(P, Q), R), S) - min(min(min(P, Q), R), S));\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0716", "problem_description": "Given a positive integer $$$n$$$. Find three distinct positive integers $$$a$$$, $$$b$$$, $$$c$$$ such that $$$a + b + c = n$$$ and $$$\\operatorname{gcd}(a, b) = c$$$, where $$$\\operatorname{gcd}(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n for (int n1 = 0; n1 < n; n1++) {\n\n int a = 0;\n scanf(\"\\n%d\", &a);\n if (a % 2 == 0) {\n printf(\"2 %d 1\", a - 3);\n } else {\n if ((a / 2) % 2 == 0) {\n printf(\"%d %d 1\", (a / 2) - 1, (a / 2) + 1);\n } else {\n printf(\"%d %d 1\", (a / 2) - 2, (a / 2) + 2);\n }\n }\n if (n1 != n - 1) {\n printf(\"\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n use std::io::Write;\n let mut handle = std::io::BufWriter::new(std::io::stdout());\n let mut test_no = String::new();\n std::io::stdin().read_line(&mut test_no).unwrap();\n let test_no: u32 = test_no.trim().parse().unwrap();\n for _ in 0..test_no {\n let mut integer = String::new();\n std::io::stdin().read_line(&mut integer).unwrap();\n let integer: u32 = integer.trim().parse().unwrap();\n if (integer & 1) == 0 {\n let a = integer / 2;\n let b = a - 1;\n writeln!(handle, \"{} {} {}\", a, b, 1).ok();\n } else {\n let mut a = (integer - 1) / 2 + 1;\n let mut b = (integer - 1) / 2 - 1;\n if (a & 1) == 0 {\n a += 1;\n b -= 1;\n }\n writeln!(handle, \"{} {} {}\", a, b, 1).ok();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0717", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a string s consisting of lowercase English letters. Extract all the characters in the odd-indexed positions and print the string obtained by concatenating them. Here, the leftmost character is assigned the index 1.

\n
\n
\n
\n
\n

Constraints

    \n
  • Each character in s is a lowercase English letter.
  • \n
  • 1≤|s|≤10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Print the string obtained by concatenating all the characters in the odd-numbered positions.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

atcoder\n
\n
\n
\n
\n
\n

Sample Output 1

acdr\n
\n

Extract the first character a, the third character c, the fifth character d and the seventh character r to obtain acdr.

\n
\n
\n
\n
\n
\n

Sample Input 2

aaaa\n
\n
\n
\n
\n
\n

Sample Output 2

aa\n
\n
\n
\n
\n
\n
\n

Sample Input 3

z\n
\n
\n
\n
\n
\n

Sample Output 3

z\n
\n
\n
\n
\n
\n
\n

Sample Input 4

fukuokayamaguchi\n
\n
\n
\n
\n
\n

Sample Output 4

fkoaaauh\n
\n
\n
", "c_code": "int solution() {\n int i = 0;\n char s[3];\n while (1) {\n scanf(\"%c\", &s[0]);\n if (s[0] == '\\n') {\n break;\n }\n if (i == 0) {\n printf(\"%c\", s[0]);\n i = 1;\n } else {\n i = 0;\n }\n }\n puts(\"\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n stdin().read_to_string(&mut s).ok();\n println!(\n \"{}\",\n s.char_indices()\n .filter(|&(i, _)| i % 2 == 0)\n .map(|(_, c)| c)\n .collect::()\n );\n}", "difficulty": "medium"} {"problem_id": "0718", "problem_description": "A number is ternary if it contains only digits $$$0$$$, $$$1$$$ and $$$2$$$. For example, the following numbers are ternary: $$$1022$$$, $$$11$$$, $$$21$$$, $$$2002$$$.You are given a long ternary number $$$x$$$. The first (leftmost) digit of $$$x$$$ is guaranteed to be $$$2$$$, the other digits of $$$x$$$ can be $$$0$$$, $$$1$$$ or $$$2$$$.Let's define the ternary XOR operation $$$\\odot$$$ of two ternary numbers $$$a$$$ and $$$b$$$ (both of length $$$n$$$) as a number $$$c = a \\odot b$$$ of length $$$n$$$, where $$$c_i = (a_i + b_i) \\% 3$$$ (where $$$\\%$$$ is modulo operation). In other words, add the corresponding digits and take the remainders of the sums when divided by $$$3$$$. For example, $$$10222 \\odot 11021 = 21210$$$.Your task is to find such ternary numbers $$$a$$$ and $$$b$$$ both of length $$$n$$$ and both without leading zeros that $$$a \\odot b = x$$$ and $$$max(a, b)$$$ is the minimum possible.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int len = 0;\n scanf(\"%d\", &len);\n char number[len + 1];\n scanf(\"%s\", number);\n number[len] = '\\0';\n int spotted = 0;\n char result1[len + 1];\n char result2[len + 1];\n for (int j = 0; j < len; j++) {\n if (spotted == 1) {\n if (number[j] == '0') {\n result1[j] = '0';\n result2[j] = '0';\n } else if (number[j] == '1') {\n result1[j] = '0';\n result2[j] = '1';\n } else {\n result1[j] = '0';\n result2[j] = '2';\n }\n } else {\n if (number[j] == '0') {\n result1[j] = '0';\n result2[j] = '0';\n } else if (number[j] == '1') {\n spotted = 1;\n result1[j] = '1';\n result2[j] = '0';\n } else {\n result1[j] = '1';\n result2[j] = '1';\n }\n }\n }\n result1[len] = '\\0';\n result2[len] = '\\0';\n printf(\"%s\\n%s\\n\", result1, result2);\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lock = stdin.lock();\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n let t = line.trim().parse().unwrap();\n\n for _ in 0..t {\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n let n = line.trim().parse().unwrap();\n\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n\n let mut x: Vec = Vec::with_capacity(n);\n for i in line.trim().split(\"\") {\n if !i.is_empty() {\n x.push(i.parse().unwrap());\n }\n }\n\n let mut a = Vec::with_capacity(n);\n let mut b = Vec::with_capacity(n);\n\n let mut first = false;\n for e in x {\n match e {\n 2 => {\n if !first {\n a.push(1);\n b.push(1);\n } else {\n a.push(0);\n b.push(2);\n }\n }\n 0 => {\n a.push(0);\n b.push(0);\n }\n 1 => {\n if !first {\n a.push(1);\n b.push(0);\n } else {\n a.push(0);\n b.push(1);\n }\n\n first = true;\n }\n _ => panic!(),\n }\n }\n\n println!(\n \"{}\",\n a.iter()\n .fold(String::new(), |acc, &x| { acc + &x.to_string() })\n );\n println!(\n \"{}\",\n b.iter()\n .fold(String::new(), |acc, &x| { acc + &x.to_string() })\n );\n }\n}", "difficulty": "hard"} {"problem_id": "0719", "problem_description": "A binary string is a string that consists of characters $$$0$$$ and $$$1$$$. A bi-table is a table that has exactly two rows of equal length, each being a binary string.Let $$$\\operatorname{MEX}$$$ of a bi-table be the smallest digit among $$$0$$$, $$$1$$$, or $$$2$$$ that does not occur in the bi-table. For example, $$$\\operatorname{MEX}$$$ for $$$\\begin{bmatrix} 0011\\\\ 1010 \\end{bmatrix}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ occur in the bi-table at least once. $$$\\operatorname{MEX}$$$ for $$$\\begin{bmatrix} 111\\\\ 111 \\end{bmatrix}$$$ is $$$0$$$, because $$$0$$$ and $$$2$$$ do not occur in the bi-table, and $$$0 < 2$$$.You are given a bi-table with $$$n$$$ columns. You should cut it into any number of bi-tables (each consisting of consecutive columns) so that each column is in exactly one bi-table. It is possible to cut the bi-table into a single bi-table — the whole bi-table.What is the maximal sum of $$$\\operatorname{MEX}$$$ of all resulting bi-tables can be?", "c_code": "int solution() {\n int t;\n int n;\n char a[100001];\n char b[100001];\n scanf(\"%d\", &t);\n while (t--) {\n int count = 0;\n scanf(\"%d %s %s\", &n, a, b);\n\n for (int i = 0; i < n; i++) {\n if (a[i] != b[i]) {\n count += 2;\n } else if (a[i] == '1' && b[i] == '1') {\n if (a[i + 1] == '0' && b[i + 1] == '0') {\n count += 2;\n i++;\n }\n } else if (a[i] == '0' && b[i] == '0') {\n if (a[i + 1] == '1' && b[i + 1] == '1') {\n count += 2;\n i++;\n } else {\n count += 1;\n }\n }\n }\n printf(\"%d\\n\", count);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut string = String::new();\n stdin.read_line(&mut string).unwrap();\n for _ in 0..string.trim().parse::().unwrap() {\n string.clear();\n stdin.read_line(&mut string).unwrap();\n string.clear();\n stdin.read_line(&mut string).unwrap();\n let v1 = string\n .trim()\n .chars()\n .map(|f| f.to_digit(10).unwrap())\n .collect::>();\n string.clear();\n stdin.read_line(&mut string).unwrap();\n let v2 = string\n .trim()\n .chars()\n .map(|f| f.to_digit(10).unwrap())\n .collect::>();\n let mut prev = 3;\n let mut count = 0;\n for i in 0..v1.len() {\n if v1[i] != v2[i] {\n if prev == 0 {\n count += 1\n }\n count += 2;\n prev = 3;\n } else {\n if v1[i] == 0 && prev == 0 {\n count += 1;\n prev = v1[i]\n } else if v1[i] != prev && prev != 3 {\n count += 2;\n prev = 3\n } else {\n prev = v1[i];\n }\n }\n }\n if prev == 0 {\n count += 1;\n }\n println!(\"{}\", &count);\n }\n}", "difficulty": "medium"} {"problem_id": "0720", "problem_description": "You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $$$k$$$ torches. One torch can be crafted using one stick and one coal.Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $$$1$$$ stick for $$$x$$$ sticks (you lose $$$1$$$ stick and gain $$$x$$$ sticks). exchange $$$y$$$ sticks for $$$1$$$ coal (you lose $$$y$$$ sticks and gain $$$1$$$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order.Your task is to find the minimum number of trades you need to craft at least $$$k$$$ torches. The answer always exists under the given constraints.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n long long int n;\n scanf(\"%d\", &t);\n long long int arr[t][3];\n for (int i = 0; i < t; i++) {\n scanf(\"%lld %lld %lld\", &arr[i][0], &arr[i][1], &arr[i][2]);\n }\n for (int i = 0; i < t; i++) {\n n = (arr[i][2] + arr[i][2] * arr[i][1] - 3 + arr[i][0]) / (arr[i][0] - 1);\n printf(\"%lld\\n\", n + arr[i][2]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_line = String::new();\n stdin().read_line(&mut input_line).unwrap();\n let mut cases = input_line.trim().parse::().unwrap_or(-1);\n while cases > 0 {\n cases -= 1;\n input_line.clear();\n stdin().read_line(&mut input_line).unwrap();\n let inputs = input_line.trim().split(' ').collect::>();\n let [x, y, k] = [\n inputs[0].parse::().unwrap_or(-1),\n inputs[1].parse::().unwrap_or(-1),\n inputs[2].parse::().unwrap_or(-1),\n ];\n let total_sticks_needed = k + k * y;\n let trade_times_to_get_enough_sticks = (total_sticks_needed + x - 3) / (x - 1);\n println!(\"{}\", k + trade_times_to_get_enough_sticks);\n }\n}", "difficulty": "hard"} {"problem_id": "0721", "problem_description": "Recently, your friend discovered one special operation on an integer array $$$a$$$: Choose two indices $$$i$$$ and $$$j$$$ ($$$i \\neq j$$$); Set $$$a_i = a_j = |a_i - a_j|$$$. After playing with this operation for a while, he came to the next conclusion: For every array $$$a$$$ of $$$n$$$ integers, where $$$1 \\le a_i \\le 10^9$$$, you can find a pair of indices $$$(i, j)$$$ such that the total sum of $$$a$$$ will decrease after performing the operation. This statement sounds fishy to you, so you want to find a counterexample for a given integer $$$n$$$. Can you find such counterexample and prove him wrong?In other words, find an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) such that for all pairs of indices $$$(i, j)$$$ performing the operation won't decrease the total sum (it will increase or not change the sum).", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n for (int o = 0; o < t; o++) {\n int n = 0;\n scanf(\"%d\", &n);\n int array[n];\n for (int k = 0; k < n; k++) {\n array[k] = pow(3, k);\n }\n if (pow(3, n - 1) < pow(10, 9)) {\n printf(\"YES\\n\");\n for (int x = 0; x < n; x++) {\n printf(\"%d \", array[x]);\n }\n printf(\"\\n\");\n } else {\n printf(\"NO\\n\");\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: u32 = n.trim().parse().unwrap();\n for _ in 0..n {\n let mut l = String::new();\n io::stdin().read_line(&mut l).unwrap();\n let l: u32 = l.trim().parse().unwrap();\n if l <= 19 {\n println!(\"YES\");\n for i in 0..l {\n print!(\"{} \", u32::pow(3, i));\n }\n println!()\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0722", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

\n

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

\n

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A \\leq B \\leq 100
  • \n
  • A and B are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n
\n
\n
\n
\n
\n

Sample Output 1

25\n
\n

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

\n
    \n
  • When the consumption tax rate is 8 percent: \\lfloor 25 \\times 0.08 \\rfloor = \\lfloor 2 \\rfloor = 2 yen.
  • \n
  • When the consumption tax rate is 10 percent: \\lfloor 25 \\times 0.1 \\rfloor = \\lfloor 2.5 \\rfloor = 2 yen.
  • \n
\n

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

\n
\n
\n
\n
\n
\n

Sample Input 2

8 10\n
\n
\n
\n
\n
\n

Sample Output 2

100\n
\n

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

\n
    \n
  • When the consumption tax rate is 8 percent: \\lfloor 100 \\times 0.08 \\rfloor = 8 yen.
  • \n
  • When the consumption tax rate is 10 percent: \\lfloor 100 \\times 0.1 \\rfloor = 10 yen.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

19 99\n
\n
\n
\n
\n
\n

Sample Output 3

-1\n
\n

There is no price before tax satisfying this condition, so print -1.

\n
\n
", "c_code": "int solution(void) {\n float i = 0;\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n for (i = 1; i <= 1000; i = i + 1.0) {\n if (0.5 <= (i * 0.08 - (int)i * 0.08)) {\n c = (int)i * 0.08 + 1;\n } else {\n c = (int)i * 0.08;\n }\n if (0.5 <= (i * 0.10 - (int)i * 0.10)) {\n d = (int)i * 0.10 + 1;\n } else {\n d = (int)i * 0.10;\n }\n if (a == c && b == d) {\n printf(\"%d\", (int)i);\n return 0;\n }\n }\n printf(\"-1\");\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let mut iter = buf.split_whitespace();\n let a: i32 = match iter.next().unwrap().parse() {\n Ok(n) => n,\n Err(_) => panic!(\"\"),\n };\n let b: i32 = match iter.next().unwrap().parse() {\n Ok(n) => n,\n Err(_) => panic!(\"\"),\n };\n let mut tax: HashMap<(i32, i32), Vec> = HashMap::new();\n for i in 1..1010 {\n let m = tax.insert((i * 8 / 100, i * 10 / 100), vec![]);\n if m.is_none() {\n let v = vec![i];\n tax.insert((i * 8 / 100, i * 10 / 100), v);\n } else {\n let mut v = m.unwrap();\n v.push(i);\n tax.insert((i * 8 / 100, i * 10 / 100), v);\n }\n }\n match tax.get(&(a, b)) {\n None => println!(\"-1\"),\n Some(v) => println!(\"{}\", v[0]),\n };\n}", "difficulty": "medium"} {"problem_id": "0723", "problem_description": "You are given $$$n$$$ strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings.String $$$a$$$ is a substring of string $$$b$$$ if it is possible to choose several consecutive letters in $$$b$$$ in such a way that they form $$$a$$$. For example, string \"for\" is contained as a substring in strings \"codeforces\", \"for\" and \"therefore\", but is not contained as a substring in strings \"four\", \"fofo\" and \"rof\".", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n char str[n][101];\n int i;\n int j;\n for (i = 0; i < n; i++) {\n scanf(\"%s\", str[i]);\n }\n int len[n];\n int pos[n];\n for (i = 0; i < n; i++) {\n len[i] = strlen(str[i]);\n pos[i] = i;\n }\n int templen;\n int temppos;\n for (i = 0; i < n - 1; i++) {\n for (j = i + 1; j < n; j++) {\n if (len[j] < len[i]) {\n templen = len[i];\n len[i] = len[j];\n len[j] = templen;\n temppos = pos[i];\n pos[i] = pos[j];\n pos[j] = temppos;\n }\n }\n }\n\n int count = 0;\n int k;\n for (i = 0; i < n - 1; i++) {\n for (j = 0; j <= len[i + 1] - len[i]; j++) {\n for (k = 0; k < len[i]; k++) {\n if (str[pos[i + 1]][j + k] != str[pos[i]][k]) {\n break;\n }\n }\n if (k == len[i]) {\n count++;\n break;\n }\n }\n }\n if (count == n - 1) {\n printf(\"YES\\n\");\n for (i = 0; i < n; i++) {\n printf(\"%s\\n\", str[pos[i]]);\n }\n } else {\n printf(\"NO\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input).unwrap();\n let n = input.trim().parse::().unwrap();\n input.clear();\n\n for _i in 0..n {\n io::stdin().read_line(&mut input).unwrap();\n }\n let mut strs: Vec<&str> = input.split_whitespace().collect();\n\n strs.sort_by(|a, b| {\n if a.contains(b) || a == b {\n Ordering::Greater\n } else if b.contains(a) {\n Ordering::Less\n } else {\n Ordering::Equal\n }\n });\n\n let mut flag = true;\n for window in strs.windows(2) {\n if !window[1].contains(window[0]) {\n flag = false;\n break;\n }\n }\n\n if flag {\n println!(\"YES\");\n for output in &strs {\n println!(\"{}\", output);\n }\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "0724", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.\nThe front and back sides of these cards can be distinguished, and initially every card faces up.

\n

We will perform the following operation once for each square contains a card:

\n
    \n
  • For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.
  • \n
\n

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.\nFind the number of cards that face down after all the operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N,M \\leq 10^9
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\n
\n
\n
\n
\n
\n

Output

Print the number of cards that face down after all the operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 7\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n

After all the operations, all cards except at both ends face down.

\n
\n
\n
\n
\n
\n

Sample Input 3

314 1592\n
\n
\n
\n
\n
\n

Sample Output 3

496080\n
\n
\n
", "c_code": "int solution(void) {\n long long n;\n long long m;\n scanf(\"%lld %lld\", &n, &m);\n\n if (m == 1 && n != 1) {\n printf(\"%lld\\n\", n - 2);\n } else if (n == 1 && m != 1) {\n printf(\"%lld\\n\", m - 2);\n } else if (m == 1 && n == 1) {\n printf(\"1\\n\");\n } else {\n printf(\"%lld\\n\", (n * m) - ((n + m) * 2) + 4);\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 let mut n: u64 = itr.next().unwrap().parse().unwrap();\n let mut k: u64 = itr.next().unwrap().parse().unwrap();\n if n > k {\n std::mem::swap(&mut n, &mut k);\n }\n if n <= 1 {\n if k == 1 {\n println!(\"{}\", 1);\n } else {\n println!(\"{}\", k - 2);\n }\n } else if n <= 2 {\n println!(\"{}\", 0);\n } else {\n println!(\"{}\", n * k - (n * 2 + (k - 2) * 2));\n }\n}", "difficulty": "hard"} {"problem_id": "0725", "problem_description": "Mishka has got n empty boxes. For every i (1 ≤ i ≤ n), i-th box is a cube with side length ai.Mishka can put a box i into another box j if the following conditions are met: i-th box is not put into another box; j-th box doesn't contain any other boxes; box i is smaller than box j (ai < aj). Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.Help Mishka to determine the minimum possible number of visible boxes!", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int max = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n int c = 0;\n for (int j = i; j < n; j++) {\n if (a[j] == a[i]) {\n c++;\n }\n }\n if (c >= max) {\n max = c;\n }\n }\n printf(\"%d\", max);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::prelude::*;\n std::io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n\n let a: Vec = it.take(n).map(|x| x.parse().unwrap()).collect();\n\n let mut hm = std::collections::HashMap::new();\n\n for a in a {\n *hm.entry(a).or_insert(0) += 1;\n }\n\n let ans = hm.values().max().unwrap();\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0726", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a string S consisting of uppercase English letters. Find the length of the longest ACGT string that is a substring (see Notes) of S.

\n

Here, a ACGT string is a string that contains no characters other than A, C, G and T.

\n
\n
\n
\n
\n

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

\n

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

\n
\n
\n
\n
\n

Constraints

    \n
  • S is a string of length between 1 and 10 (inclusive).
  • \n
  • Each character in S is an uppercase English letter.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the length of the longest ACGT string that is a substring of S.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

ATCODER\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

Among the ACGT strings that are substrings of ATCODER, the longest one is ATC.

\n
\n
\n
\n
\n
\n

Sample Input 2

HATAGAYA\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n

Among the ACGT strings that are substrings of HATAGAYA, the longest one is ATAGA.

\n
\n
\n
\n
\n
\n

Sample Input 3

SHINJUKU\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

Among the ACGT strings that are substrings of SHINJUKU, the longest one is (the empty string).

\n
\n
", "c_code": "int solution() {\n int ans = 0;\n int tmp = 0;\n while (1) {\n char c = getchar();\n if (c == 'A' || c == 'G' || c == 'T' || c == 'C') {\n tmp++;\n if (ans < tmp) {\n ans = tmp;\n }\n } else if (c >= 'A' && c <= 'Z') {\n tmp = 0;\n } else {\n break;\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let s: Vec = buf.trim().chars().collect();\n let mut flag = false;\n let mut p = 0;\n for i in 1..(s.len() + 1) {\n let mut j = 0;\n while j + i <= s.len() {\n let ps = &s[j..(j + i)];\n for k in ps {\n if !(*k == 'A' || *k == 'T' || *k == 'G' || *k == 'C') {\n flag = true;\n break;\n }\n }\n if flag {\n flag = false;\n } else {\n p = i;\n break;\n }\n j += 1;\n }\n flag = false;\n }\n println!(\"{}\", p);\n}", "difficulty": "medium"} {"problem_id": "0727", "problem_description": "You are given a positive integer $$$x$$$. Find any such $$$2$$$ positive integers $$$a$$$ and $$$b$$$ such that $$$GCD(a,b)+LCM(a,b)=x$$$.As a reminder, $$$GCD(a,b)$$$ is the greatest integer that divides both $$$a$$$ and $$$b$$$. Similarly, $$$LCM(a,b)$$$ is the smallest integer such that both $$$a$$$ and $$$b$$$ divide it.It's guaranteed that the solution always exists. If there are several such pairs $$$(a, b)$$$, you can output any of them.", "c_code": "int solution() {\n int m = 0;\n scanf(\"%d\", &m);\n for (int i = 0; i < m; i++) {\n int l = 0;\n int p = 1;\n scanf(\"%d\", &l);\n printf(\"%d %d\\n\", p, l - 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut bih = BufReader::new(io::stdin());\n let mut boh = BufWriter::new(io::stdout());\n let mut line = String::new();\n\n bih.read_line(&mut line).unwrap();\n\n for _ in 0..line.trim().parse::().unwrap() {\n line.clear();\n bih.read_line(&mut line).unwrap();\n\n writeln!(boh, \"1 {}\", line.trim().parse::().unwrap() - 1).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0728", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

\"Pizza At\", a fast food chain, offers three kinds of pizza: \"A-pizza\", \"B-pizza\" and \"AB-pizza\". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

\n

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ A, B, C ≤ 5000
  • \n
  • 1 ≤ X, Y ≤ 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C X Y\n
\n
\n
\n
\n
\n

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1500 2000 1600 3 2\n
\n
\n
\n
\n
\n

Sample Output 1

7900\n
\n

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

\n
\n
\n
\n
\n
\n

Sample Input 2

1500 2000 1900 3 2\n
\n
\n
\n
\n
\n

Sample Output 2

8500\n
\n

It is optimal to directly buy three A-pizzas and two B-pizzas.

\n
\n
\n
\n
\n
\n

Sample Input 3

1500 2000 500 90000 100000\n
\n
\n
\n
\n
\n

Sample Output 3

100000000\n
\n

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.

\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int c = 0;\n int x = 0;\n int y = 0;\n scanf(\"%d %d %d %d %d\", &a, &b, &c, &x, &y);\n int s = 0;\n int z = 0;\n\n if (x >= y) {\n if (a + b <= 2 * c) {\n s = 1;\n } else if (2 * c > a) {\n s = 2;\n } else if (a >= 2 * c) {\n s = 3;\n }\n } else {\n if (a + b <= 2 * c) {\n s = 1;\n } else if (2 * c > b) {\n s = 4;\n } else if (b >= 2 * c) {\n s = 5;\n }\n }\n\n switch (s) {\n case 1:\n z = a * x + b * y;\n break;\n case 2:\n z = a * (x - y) + 2 * c * y;\n break;\n case 3:\n z = 2 * c * x;\n break;\n case 4:\n z = b * (y - x) + 2 * c * x;\n break;\n case 5:\n z = 2 * c * y;\n break;\n }\n\n printf(\"%d\", z);\n\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.trim().split(\" \");\n let a = iter.next().unwrap().parse::().unwrap();\n let b = iter.next().unwrap().parse::().unwrap();\n let c = iter.next().unwrap().parse::().unwrap();\n let x = iter.next().unwrap().parse::().unwrap();\n let y = iter.next().unwrap().parse::().unwrap();\n\n if a > 2 * c {\n if b > 2 * c {\n println!(\"{}\", 2 * max(x, y) * c);\n } else {\n println!(\"{}\", 2 * x * c + max(0, y - x) * b);\n }\n } else if b > 2 * c {\n println!(\"{}\", 2 * y * c + max(0, x - y) * a);\n } else if a + b > 2 * c {\n println!(\n \"{}\",\n c * 2 * min(x, y) + if x > y { a * (x - y) } else { b * (y - x) }\n );\n } else {\n println!(\"{}\", a * x + b * y);\n }\n}", "difficulty": "easy"} {"problem_id": "0729", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Alice and Brown loves games. Today, they will play the following game.

\n

In this game, there are two piles initially consisting of X and Y stones, respectively.\nAlice and Bob alternately perform the following operation, starting from Alice:

\n
    \n
  • Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
  • \n
\n

The player who becomes unable to perform the operation, loses the game.

\n

Given X and Y, determine the winner of the game, assuming that both players play optimally.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 ≤ X, Y ≤ 10^{18}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X Y\n
\n
\n
\n
\n
\n

Output

Print the winner: either Alice or Brown.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 1\n
\n
\n
\n
\n
\n

Sample Output 1

Brown\n
\n

Alice can do nothing but taking two stones from the pile containing two stones. As a result, the piles consist of zero and two stones, respectively. Then, Brown will take the two stones, and the piles will consist of one and zero stones, respectively. Alice will be unable to perform the operation anymore, which means Brown's victory.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 0\n
\n
\n
\n
\n
\n

Sample Output 2

Alice\n
\n
\n
\n
\n
\n
\n

Sample Input 3

0 0\n
\n
\n
\n
\n
\n

Sample Output 3

Brown\n
\n
\n
\n
\n
\n
\n

Sample Input 4

4 8\n
\n
\n
\n
\n
\n

Sample Output 4

Alice\n
\n
\n
", "c_code": "int solution(void) {\n long long x;\n long long y;\n scanf(\"%lld%lld\", &x, &y);\n if (llabs(x - y) > 1) {\n printf(\"Alice\\n\");\n } else {\n printf(\"Brown\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n x: i64,\n y: i64\n }\n\n println!(\"{}\", if (x - y).abs() <= 1 { \"Brown\" } else { \"Alice\" });\n}", "difficulty": "easy"} {"problem_id": "0730", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We have two bottles for holding water.

\n

Bottle 1 can hold up to A milliliters of water, and now it contains B milliliters of water.

\n

Bottle 2 contains C milliliters of water.

\n

We will transfer water from Bottle 2 to Bottle 1 as much as possible.

\n

How much amount of water will remain in Bottle 2?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq B \\leq A \\leq 20
  • \n
  • 1 \\leq C \\leq 20
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print the integer representing the amount of water, in milliliters, that will remain in Bottle 2.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 4 3\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

We will transfer two milliliters of water from Bottle 2 to Bottle 1, and one milliliter of water will remain in Bottle 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

8 3 9\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n
\n
\n
\n
\n
\n

Sample Input 3

12 3 7\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n int A = 0;\n int B = 0;\n int C = 0;\n\n scanf(\"%d %d %d\", &A, &B, &C);\n\n while (C > 0 && A > B) {\n B++;\n C--;\n }\n\n printf(\"%d\", C);\n\n return 0;\n}", "rust_code": "fn solution() {\n let max = |a, b| if a < b { b } else { a };\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n let line: Vec = line.split_whitespace().flat_map(str::parse).collect();\n let (a, b, c) = (line[0], line[1], line[2]);\n\n println!(\"{}\", max(0, c - a + b));\n}", "difficulty": "medium"} {"problem_id": "0731", "problem_description": "Vladik and Chloe decided to determine who of them is better at math. Vladik claimed that for any positive integer n he can represent fraction as a sum of three distinct positive fractions in form .Help Vladik with that, i.e for a given n find three distinct positive integers x, y and z such that . Because Chloe can't check Vladik's answer if the numbers are large, he asks you to print numbers not exceeding 109.If there is no such answer, print -1.", "c_code": "int solution() {\n int n = 10;\n scanf(\"%d\", &n);\n if (n == 1) {\n printf(\"-1\");\n } else {\n printf(\"%d %d %d\", n, n + 1, n * (n + 1));\n }\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n io::stdin().read_line(&mut input_str).unwrap();\n\n let mut iter = input_str.split_whitespace();\n let i: u64 = iter.next().unwrap().parse().unwrap();\n\n match i {\n 1 => println!(\"{}\", -1),\n _ => println!(\"{} {} {}\", i, i + 1, i * (i + 1)),\n };\n}", "difficulty": "medium"} {"problem_id": "0732", "problem_description": "You are given some Tetris field consisting of $$$n$$$ columns. The initial height of the $$$i$$$-th column of the field is $$$a_i$$$ blocks. On top of these columns you can place only figures of size $$$2 \\times 1$$$ (i.e. the height of this figure is $$$2$$$ blocks and the width of this figure is $$$1$$$ block). Note that you cannot rotate these figures.Your task is to say if you can clear the whole field by placing such figures.More formally, the problem can be described like this:The following process occurs while at least one $$$a_i$$$ is greater than $$$0$$$: You place one figure $$$2 \\times 1$$$ (choose some $$$i$$$ from $$$1$$$ to $$$n$$$ and replace $$$a_i$$$ with $$$a_i + 2$$$); then, while all $$$a_i$$$ are greater than zero, replace each $$$a_i$$$ with $$$a_i - 1$$$. And your task is to determine if it is possible to clear the whole field (i.e. finish the described process), choosing the places for new figures properly.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n int n;\n scanf(\"%d\", &t);\n while (t-- > 0) {\n n = 0;\n int f = 0;\n scanf(\"%d\", &n);\n int a[101];\n for (int i = 0; i < 102; i++) {\n a[i] = 0;\n }\n int min = 1000;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] < min) {\n min = a[i];\n }\n }\n for (int i = 0; i < n; i++) {\n a[i] -= min;\n if (a[i] % 2 == 0) {\n continue;\n }\n f = 1;\n }\n if (f == 1) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n use std::io::{self, BufRead};\n\n let reader = io::stdin();\n let test_cases = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n\n for _ in 0..test_cases {\n let _ = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n let initial: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|s| s.trim())\n .filter(|s| !s.is_empty())\n .map(|s| s.parse().unwrap())\n .collect();\n let min = initial.iter().min().unwrap();\n let subtract: Vec = initial.iter().map(|x| x - min).collect();\n let all_divisible = subtract.iter().all(|x| x % 2 == 0);\n if all_divisible {\n println!(\"YES\")\n } else {\n println!(\"NO\")\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0733", "problem_description": "Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way to get to the olympiad.Vladik knows n airports. All the airports are located on a straight line. Each airport has unique id from 1 to n, Vladik's house is situated next to the airport with id a, and the place of the olympiad is situated next to the airport with id b. It is possible that Vladik's house and the place of the olympiad are located near the same airport. To get to the olympiad, Vladik can fly between any pair of airports any number of times, but he has to start his route at the airport a and finish it at the airport b.Each airport belongs to one of two companies. The cost of flight from the airport i to the airport j is zero if both airports belong to the same company, and |i - j| if they belong to different companies.Print the minimum cost Vladik has to pay to get to the olympiad.", "c_code": "int solution() {\n int n;\n int a;\n int b;\n char c[100010];\n scanf(\"%d%d%d\", &n, &a, &b);\n scanf(\"%s\", c);\n a--;\n b--;\n if (c[a] == c[b]) {\n printf(\"0\\n\");\n } else {\n printf(\"1\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n io::stdin().read_line(&mut input_str).unwrap();\n let mut iter = input_str.split_whitespace();\n let airport_count = iter.next().unwrap().parse::().unwrap();\n let start = iter.next().unwrap().parse::().unwrap() - 1;\n let end = iter.next().unwrap().parse::().unwrap() - 1;\n\n let mut airports = Vec::with_capacity(airport_count);\n\n let mut input_str = String::new();\n io::stdin().read_line(&mut input_str).unwrap();\n for ch in input_str.chars() {\n if ch == '1' {\n airports.push(true);\n } else {\n airports.push(false);\n }\n }\n\n if airports[start] == airports[end] {\n println!(\"{}\", 0);\n } else {\n println!(\"{}\", 1);\n }\n}", "difficulty": "hard"} {"problem_id": "0734", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

Takahashi, who works at DISCO, is standing before an iron bar.\nThe bar has N-1 notches, which divide the bar into N sections. The i-th section from the left has a length of A_i millimeters.

\n

Takahashi wanted to choose a notch and cut the bar at that point into two parts with the same length.\nHowever, this may not be possible as is, so he will do the following operations some number of times before he does the cut:

\n
    \n
  • Choose one section and expand it, increasing its length by 1 millimeter. Doing this operation once costs 1 yen (the currency of Japan).
  • \n
  • Choose one section of length at least 2 millimeters and shrink it, decreasing its length by 1 millimeter. Doing this operation once costs 1 yen.
  • \n
\n

Find the minimum amount of money needed before cutting the bar into two parts with the same length.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 2 \\leq N \\leq 200000
  • \n
  • 1 \\leq A_i \\leq 2020202020
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 A_3 ... A_N\n
\n
\n
\n
\n
\n

Output

\n

Print an integer representing the minimum amount of money needed before cutting the bar into two parts with the same length.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2 4 3\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The initial lengths of the sections are [2, 4, 3] (in millimeters). Takahashi can cut the bar equally after doing the following operations for 3 yen:

\n
    \n
  • Shrink the second section from the left. The lengths of the sections are now [2, 3, 3].
  • \n
  • Shrink the first section from the left. The lengths of the sections are now [1, 3, 3].
  • \n
  • Shrink the second section from the left. The lengths of the sections are now [1, 2, 3], and we can cut the bar at the second notch from the left into two parts of length 3 each.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

12\n100 104 102 105 103 103 101 105 104 102 104 101\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n scanf(\"%d\", &N);\n long long int A[200000];\n long long int min = 2020202020;\n long long int sum = 0;\n long long int sum2 = 0;\n for (int i = 0; i < N; i++) {\n scanf(\"%lld\", &A[i]);\n sum += A[i];\n }\n for (int i = 0; i < N; i++) {\n sum2 += A[i];\n if (labs(sum - (2 * sum2)) < min) {\n min = labs(sum - (2 * sum2));\n }\n }\n printf(\"%lld\", min);\n\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 A: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let mut acc = vec![0; N];\n acc[0] = A[0];\n for i in 1..N {\n acc[i] = acc[i - 1] + A[i];\n }\n let ans = (1..N)\n .map(|i| (acc[i - 1] - (acc[N - 1] - acc[i - 1])).abs())\n .min()\n .unwrap();\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0735", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Snuke has a grid consisting of three squares numbered 1, 2 and 3.\nIn each square, either 0 or 1 is written. The number written in Square i is s_i.

\n

Snuke will place a marble on each square that says 1.\nFind the number of squares on which Snuke will place a marble.

\n
\n
\n
\n
\n

Constraints

    \n
  • Each of s_1, s_2 and s_3 is either 1 or 0.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s_{1}s_{2}s_{3}\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

101\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n
    \n
  • A marble will be placed on Square 1 and 3.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

000\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
    \n
  • No marble will be placed on any square.
  • \n
\n
\n
", "c_code": "int solution() {\n char s[10];\n\n scanf(\"%s\", s);\n if (s[0] == '1' && s[1] == '1' && s[2] == '1') {\n printf(\"3\\n\");\n } else if (s[0] == '0' && s[1] == '0' && s[2] == '0') {\n printf(\"0\\n\");\n } else if ((s[0] == '1' && s[1] == '0' && s[2] == '0') ||\n (s[0] == '0' && s[1] == '1' && s[2] == '0') ||\n (s[0] == '0' && s[1] == '0' && s[2] == '1')) {\n printf(\"1\\n\");\n } else {\n printf(\"2\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n println!(\"{}\", s.trim().chars().filter(|&c| c == '1').count());\n}", "difficulty": "medium"} {"problem_id": "0736", "problem_description": "Pancake sort", "c_code": "void solution(int *arr, int n) {\n int curr_size;\n int i;\n int maxElementIdx;\n int temp;\n int start;\n\n for (curr_size = n; curr_size > 1; --curr_size) {\n\n maxElementIdx = 0;\n\n for (i = 1; i < curr_size; ++i) {\n if (arr[i] > arr[maxElementIdx]) {\n maxElementIdx = i;\n }\n }\n\n if (maxElementIdx != curr_size - 1) {\n\n start = 0;\n i = maxElementIdx;\n\n while (start < i) {\n temp = arr[start];\n arr[start] = arr[i];\n arr[i] = temp;\n\n start++;\n i--;\n }\n\n start = 0;\n i = curr_size - 1;\n\n while (start < i) {\n temp = arr[start];\n arr[start] = arr[i];\n arr[i] = temp;\n\n start++;\n i--;\n }\n }\n }\n}\n", "rust_code": "fn solution(arr: &mut [T]) -> Vec\nwhere\n T: cmp::PartialEq + cmp::Ord + cmp::PartialOrd + Clone,\n{\n let len = arr.len();\n if len < 2 {\n arr.to_vec();\n }\n for i in (0..len).rev() {\n let max_index = arr\n .iter()\n .take(i + 1)\n .enumerate()\n .max_by_key(|&(_, elem)| elem)\n .map(|(idx, _)| idx)\n .unwrap();\n if max_index != i {\n arr[0..=max_index].reverse();\n arr[0..=i].reverse();\n }\n }\n arr.to_vec()\n}\n", "difficulty": "medium"} {"problem_id": "0737", "problem_description": "The problem statement looms below, filling you with determination.Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up.Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't.You are given a grid $$$a$$$ of dimensions $$$n \\times m$$$ , i. e. a grid with $$$n$$$ rows and $$$m$$$ columns. You need to answer $$$q$$$ queries ($$$1 \\leq q \\leq 2 \\cdot 10^5$$$). Each query gives two integers $$$x_1, x_2$$$ ($$$1 \\leq x_1 \\leq x_2 \\leq m$$$) and asks whether the subgrid of $$$a$$$ consisting of the columns $$$x_1, x_1 + 1, \\ldots, x_2 - 1, x_2$$$ is determinable.", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n char str[n][m];\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", str[i]);\n }\n int q;\n int arr[1000000] = {0};\n scanf(\"%d\", &q);\n for (int i = 1; i <= m - 1; i++) {\n for (int j = 1; j <= n - 1; j++) {\n if (str[j][i - 1] == 'X' && str[j - 1][i] == 'X') {\n arr[i] = 1;\n break;\n }\n }\n }\n for (int i = 1; i < m; i++) {\n arr[i] += arr[i - 1];\n }\n for (int j = 1; j <= q; j++) {\n int x;\n int y;\n int temp = 0;\n scanf(\"%d%d\", &x, &y);\n x--;\n y--;\n if (arr[x] == arr[y]) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut s = String::new();\n io::stdin().read_to_string(&mut s)?;\n let mut it = s.split_whitespace().map(|s| s.to_string());\n let mut out = String::new();\n let R: usize = it.next().unwrap().parse().unwrap();\n let C: usize = it.next().unwrap().parse().unwrap();\n let mut grid = vec![];\n for _ in 0..R {\n let s: String = it.next().unwrap().parse().unwrap();\n let row: Vec = s.chars().map(|x| x == 'X').collect();\n grid.push(row);\n }\n let mut pfx = vec![0; C];\n for j in 1..C {\n let mut ok = true;\n for i in 0..R - 1 {\n if grid[i][j] && grid[i + 1][j - 1] {\n ok = false;\n break;\n }\n }\n if ok {\n pfx[j] = pfx[j - 1];\n } else {\n pfx[j] = pfx[j - 1] + 1;\n }\n }\n\n let Q: usize = it.next().unwrap().parse().unwrap();\n for _ in 0..Q {\n let mut x1: usize = it.next().unwrap().parse().unwrap();\n let mut x2: usize = it.next().unwrap().parse().unwrap();\n x1 -= 1;\n x2 -= 1;\n if x1 == x2 || pfx[x1] == pfx[x2] {\n out.push_str(\"yes\\n\");\n } else {\n out.push_str(\"no\\n\");\n }\n }\n print!(\"{}\", out);\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "0738", "problem_description": "Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it.", "c_code": "int solution() {\n int n;\n scanf(\"%d\\n\", &n);\n char word[n];\n int par = (n - 1) / 2;\n int sign = n & 1 ? 1 : -1;\n for (int i = 0; i < n; i++) {\n par += sign * i;\n sign *= -1;\n word[par] = getchar();\n }\n for (int i = 0; i < n; i++) {\n putchar(word[i]);\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut n = String::new();\n io::stdin().read_line(&mut n)?;\n let n: i32 = n.trim().parse().unwrap();\n\n let mut input = String::new();\n io::stdin().read_to_string(&mut input)?;\n let line = input.lines().next().unwrap();\n\n let mut v: VecDeque = VecDeque::new();\n let mut i = if n % 2 == 0 { 1 } else { 0 };\n for ch in line.chars() {\n if i % 2 == 0 {\n v.push_back(ch);\n } else {\n v.push_front(ch);\n }\n i += 1;\n }\n let word: String = v.into_iter().collect();\n println!(\"{}\", word);\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0739", "problem_description": "Mark is cleaning a row of $$$n$$$ rooms. The $$$i$$$-th room has a nonnegative dust level $$$a_i$$$. He has a magical cleaning machine that can do the following three-step operation. Select two indices $$$i<j$$$ such that the dust levels $$$a_i$$$, $$$a_{i+1}$$$, $$$\\dots$$$, $$$a_{j-1}$$$ are all strictly greater than $$$0$$$. Set $$$a_i$$$ to $$$a_i-1$$$. Set $$$a_j$$$ to $$$a_j+1$$$. Mark's goal is to make $$$a_1 = a_2 = \\ldots = a_{n-1} = 0$$$ so that he can nicely sweep the $$$n$$$-th room. Determine the minimum number of operations needed to reach his goal.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int flag = 0;\n long long count = 0;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (flag == 0 && arr[i] != 0 && i < n - 1) {\n count += arr[i];\n flag = 1;\n } else if (flag == 1 && i < n - 1) {\n if (arr[i] != 0) {\n count += arr[i];\n } else {\n count++;\n }\n }\n }\n printf(\"%lld\\n\", count);\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n name = reader,\n tests: usize\n }\n use std::io::*;\n let stdout = stdout();\n let mut writer = BufWriter::new(stdout.lock());\n\n \n\n for _ in 0..tests {\n input! {\n use reader,\n n: usize,\n mut a: [u64; n]\n }\n let mut zeros = (0..n - 1).rev().filter(|i| a[*i] == 0).collect::>();\n let mut ans = 0;\n for i in 0..n - 1 {\n while let Some(x) = zeros.last() {\n if *x <= i {\n zeros.pop();\n } else {\n break;\n }\n }\n while a[i] > 0 {\n if let Some(x) = zeros.pop() {\n a[i] -= 1;\n a[x] += 1;\n ans += 1;\n } else {\n break;\n }\n }\n ans += a[i];\n }\n println!(\"{}\", ans);\n }\n\n as Write>::flush(&mut writer).unwrap();\n}", "difficulty": "medium"} {"problem_id": "0740", "problem_description": "Everybody knows that the $$$m$$$-coder Tournament will happen soon. $$$m$$$ schools participate in the tournament, and only one student from each school participates.There are a total of $$$n$$$ students in those schools. Before the tournament, all students put their names and the names of their schools into the Technogoblet of Fire. After that, Technogoblet selects the strongest student from each school to participate. Arkady is a hacker who wants to have $$$k$$$ Chosen Ones selected by the Technogoblet. Unfortunately, not all of them are the strongest in their schools, but Arkady can make up some new school names and replace some names from Technogoblet with those. You can't use each made-up name more than once. In that case, Technogoblet would select the strongest student in those made-up schools too.You know the power of each student and schools they study in. Calculate the minimal number of schools Arkady has to make up so that $$$k$$$ Chosen Ones would be selected by the Technogoblet.", "c_code": "int solution() {\n int n;\n int m;\n int k;\n int x = 0;\n scanf(\"%i %i %i\", &n, &m, &k);\n int a[n];\n int b[n];\n int c[k];\n for (int i = 0; i < n; i++) {\n scanf(\"%i\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%i\", &b[i]);\n }\n for (int i = 0; i < k; i++) {\n scanf(\"%i\", &c[i]);\n }\n for (int i = 0; i < k; i++) {\n for (int j = 0; j < n; j++) {\n if (a[c[i] - 1] < a[j] && b[c[i] - 1] == b[j]) {\n x++;\n break;\n }\n }\n }\n printf(\"%i\", x);\n return 0;\n}", "rust_code": "fn solution() {\n let (n, m, _) = {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let mut line = line.split_whitespace();\n (\n line.next().unwrap().trim().parse::().unwrap(),\n line.next().unwrap().trim().parse::().unwrap(),\n line.next().unwrap().trim().parse::().unwrap(),\n )\n };\n\n let p: Vec = {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|it| it.trim().parse().unwrap())\n .collect()\n };\n\n let s: Vec = {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|it| it.trim().parse().unwrap())\n .collect()\n };\n\n let c: Vec = {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|it| it.trim().parse().unwrap())\n .collect()\n };\n\n let mut bests = vec![0; m];\n for i in 0..n {\n let school = s[i] - 1;\n\n if bests[school] == 0 || p[i] > p[bests[school] - 1] {\n bests[school] = i + 1;\n }\n }\n\n let mut res = c.len();\n for i in c {\n if bests.contains(&i) {\n res -= 1\n }\n }\n\n println!(\"{}\", res)\n}", "difficulty": "hard"} {"problem_id": "0741", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There is a directed graph G with N vertices and M edges.\nThe vertices are numbered 1, 2, \\ldots, N, and for each i (1 \\leq i \\leq M), the i-th directed edge goes from Vertex x_i to y_i.\nG does not contain directed cycles.

\n

Find the length of the longest directed path in G.\nHere, the length of a directed path is the number of edges in it.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq M \\leq 10^5
  • \n
  • 1 \\leq x_i, y_i \\leq N
  • \n
  • All pairs (x_i, y_i) are distinct.
  • \n
  • G does not contain directed cycles.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nx_1 y_1\nx_2 y_2\n:\nx_M y_M\n
\n
\n
\n
\n
\n

Output

Print the length of the longest directed path in G.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 5\n1 2\n1 3\n3 2\n2 4\n3 4\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The red directed path in the following figure is the longest:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 2

6 3\n2 3\n4 5\n5 6\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

The red directed path in the following figure is the longest:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 3

5 8\n5 3\n2 3\n2 4\n5 2\n5 1\n1 4\n4 3\n1 3\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n

The red directed path in the following figure is one of the longest:

\n

\"\"

\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int M;\n int u;\n int w;\n int deg[100001] = {};\n list *adj[100001] = {};\n list e[100001];\n list *p;\n scanf(\"%d %d\", &N, &M);\n for (i = 0; i < M; i++) {\n scanf(\"%d %d\", &u, &w);\n e[i].v = w;\n e[i].next = adj[u];\n adj[u] = &(e[i]);\n deg[w]++;\n }\n\n int dist[100001] = {};\n int q[100001];\n int head;\n int tail;\n for (i = 1, tail = 0; i <= N; i++) {\n if (deg[i] == 0) {\n q[tail++] = i;\n }\n }\n for (head = 0; head < tail; head++) {\n u = q[head];\n for (p = adj[u]; p != NULL; p = p->next) {\n w = p->v;\n if (dist[u] + 1 > dist[w]) {\n dist[w] = dist[u] + 1;\n }\n deg[w]--;\n if (deg[w] == 0) {\n q[tail++] = w;\n }\n }\n }\n\n int max = 0;\n for (i = 1; i <= N; i++) {\n if (dist[i] > max) {\n max = dist[i];\n }\n }\n printf(\"%d\\n\", max);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n m: usize,\n g: [(usize, usize); m]\n }\n let mut graph: Vec> = vec![vec![]; n];\n let mut in_degree: Vec = vec![0; n];\n for (f, t) in g {\n graph[f - 1].push(t - 1);\n in_degree[t - 1] += 1;\n }\n let mut graph_len: Vec = vec![0; n];\n let mut queue: VecDeque = VecDeque::new();\n for i in 0..n {\n if in_degree[i] == 0 {\n queue.push_front(i);\n }\n }\n let mut path_len = 0;\n while let Some(p) = queue.pop_back() {\n let nx_len = graph_len[p] + 1;\n path_len = cmp::max(path_len, graph_len[p]);\n for &i in &graph[p] {\n graph_len[i] = cmp::max(graph_len[i], nx_len);\n in_degree[i] -= 1;\n if in_degree[i] == 0 {\n queue.push_front(i);\n }\n }\n }\n println!(\"{}\", path_len);\n}", "difficulty": "medium"} {"problem_id": "0742", "problem_description": "Yura is tasked to build a closed fence in shape of an arbitrary non-degenerate simple quadrilateral. He's already got three straight fence segments with known lengths $$$a$$$, $$$b$$$, and $$$c$$$. Now he needs to find out some possible integer length $$$d$$$ of the fourth straight fence segment so that he can build the fence using these four segments. In other words, the fence should have a quadrilateral shape with side lengths equal to $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$. Help Yura, find any possible length of the fourth side.A non-degenerate simple quadrilateral is such a quadrilateral that no three of its corners lie on the same line, and it does not cross itself.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long ans = 0;\n for (int i = 0; i < 3; i++) {\n int temp;\n scanf(\"%d\", &temp);\n ans += temp;\n }\n printf(\"%lld\\n\", ans - 1);\n }\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n\n io::stdin().read_line(&mut s).expect(\"Oops\");\n\n let t: u32 = s.trim().parse().expect(\"Oops\");\n\n for _test in 1..t + 1 {\n let mut s: String = String::new();\n\n io::stdin().read_line(&mut s).expect(\"Oops\");\n\n let mut s = s.split_whitespace();\n\n let mut a: i64 = s.next().expect(\"Oops\").parse().expect(\"Oops\");\n let mut b: i64 = s.next().expect(\"Oops\").parse().expect(\"Oops\");\n let mut c: i64 = s.next().expect(\"Oops\").parse().expect(\"Oops\");\n\n if a > b {\n mem::swap(&mut a, &mut b);\n }\n if a > c {\n mem::swap(&mut a, &mut c);\n }\n if b > c {\n mem::swap(&mut b, &mut c);\n }\n\n let d = c;\n println!(\"{}\", d);\n }\n}", "difficulty": "hard"} {"problem_id": "0743", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Construct a sequence a = {a_1,\\ a_2,\\ ...,\\ a_{2^{M + 1}}} of length 2^{M + 1} that satisfies the following conditions, if such a sequence exists.

\n
    \n
  • Each integer between 0 and 2^M - 1 (inclusive) occurs twice in a.
  • \n
  • For any i and j (i < j) such that a_i = a_j, the formula a_i \\ xor \\ a_{i + 1} \\ xor \\ ... \\ xor \\ a_j = K holds.
  • \n
\n

\nWhat is xor (bitwise exclusive or)?

\n

The xor of integers c_1, c_2, ..., c_n is defined as follows:

\n
    \n
  • When c_1 \\ xor \\ c_2 \\ xor \\ ... \\ xor \\ c_n is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if the number of integers among c_1, c_2, ...c_m whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.
  • \n
\n

For example, 3 \\ xor \\ 5 = 6. (If we write it in base two: 011 xor 101 = 110.)

\n

", "c_code": "int solution() {\n int m;\n int k;\n scanf(\"%d %d\", &m, &k);\n int n = 1;\n int i;\n for (i = 0; i < m; i++) {\n n *= 2;\n }\n if (k >= n) {\n printf(\"-1\\n\");\n return 0;\n }\n if (m == 1) {\n if (k == 1) {\n printf(\"-1\\n\");\n return 0;\n }\n printf(\"0 0 1 1\\n\");\n return 0;\n }\n for (i = n - 1; i >= 0; i--) {\n if (i != k) {\n printf(\"%d \", i);\n }\n }\n printf(\"%d \", k);\n for (i = 0; i < n; i++) {\n if (i != k) {\n printf(\"%d \", i);\n }\n }\n printf(\"%d\\n\", k);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n m: usize,\n k: usize\n }\n if 1 << m < k + 1 {\n println!(\"-1\");\n return;\n }\n let mut d = 0;\n while 1 << d <= k {\n d += 1;\n }\n if k == 0 {\n print!(\"0 0\");\n for i in 1..1 << m {\n print!(\" {} {}\", i, i);\n }\n println!();\n } else if k == 1 && m < 2 {\n println!(\"-1\");\n } else {\n print!(\"0\");\n for i in 1..1 << m {\n if i == k {\n continue;\n }\n print!(\" {}\", i);\n }\n print!(\" {}\", k);\n for i in (1..1 << m).rev() {\n if i == k {\n continue;\n }\n print!(\" {}\", i);\n }\n println!(\" 0 {}\", k);\n }\n}", "difficulty": "easy"} {"problem_id": "0744", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Takahashi loves the number 7 and multiples of K.

\n

Where is the first occurrence of a multiple of K in the sequence 7,77,777,\\ldots? (Also see Output and Sample Input/Output below.)

\n

If the sequence contains no multiples of K, print -1 instead.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq 10^6
  • \n
  • K is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K\n
\n
\n
\n
\n
\n

Output

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

101\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

None of 7, 77, and 777 is a multiple of 101, but 7777 is.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

All elements in the sequence are odd numbers; there are no multiples of 2.

\n
\n
\n
\n
\n
\n

Sample Input 3

999983\n
\n
\n
\n
\n
\n

Sample Output 3

999982\n
\n
\n
", "c_code": "int solution(void) {\n long k;\n long num = 1;\n scanf(\"%ld\", &k);\n\n long a = (k % 7 == 0) ? 9 * k / 7 : 9 * k;\n if (a % 2 == 0 || a % 5 == 0) {\n puts(\"-1\");\n return 0;\n }\n long n = 10 % a;\n\n while (1) {\n if (n == 1) {\n break;\n }\n n = n * 10 % a;\n num++;\n }\n printf(\"%ld\\n\", num);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let stdin = io::stdin();\n stdin.read_line(&mut buf).unwrap();\n let K: u64 = buf.trim_end().parse().unwrap();\n let mut acc = 0;\n for i in 0..10_000_000 {\n acc *= 10;\n acc += 7;\n acc %= K;\n if acc == 0 {\n println!(\"{}\", i + 1);\n return;\n }\n }\n println!(\"-1\");\n}", "difficulty": "easy"} {"problem_id": "0745", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

\n

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \\leq i \\leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

\n

When Mountain i (1 \\leq i \\leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

\n

One day, each of the mountains received a non-negative even number of liters of rain.

\n

As a result, Dam i (1 \\leq i \\leq N) accumulated a total of A_i liters of water.

\n

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 3 \\leq N \\leq 10^5-1
  • \n
  • N is an odd number.
  • \n
  • 0 \\leq A_i \\leq 10^9
  • \n
  • The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

4 0 4\n
\n

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

\n
    \n
  • Dam 1 should have accumulated \\frac{4}{2} + \\frac{0}{2} = 2 liters of water.
  • \n
  • Dam 2 should have accumulated \\frac{0}{2} + \\frac{4}{2} = 2 liters of water.
  • \n
  • Dam 3 should have accumulated \\frac{4}{2} + \\frac{4}{2} = 4 liters of water.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5\n3 8 7 5 5\n
\n
\n
\n
\n
\n

Sample Output 2

2 4 12 2 8\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3\n1000000000 1000000000 0\n
\n
\n
\n
\n
\n

Sample Output 3

0 2000000000 0\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int sum = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n sum += a[i];\n }\n\n int pro = sum;\n for (int i = 0; i < (n - 1) / 2; i++) {\n pro -= 2 * a[(2 * i + 1) % n];\n }\n printf(\"%d \", pro);\n for (int i = 0; i < n - 1; i++) {\n pro = 2 * a[i] - pro;\n printf(\"%d \", pro);\n }\n printf(\"\\n\");\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 A: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let mut res = vec![(0..N)\n .map(|i| if i % 2 == 0 { A[i] } else { -A[i] })\n .sum::()];\n for i in 0..(N - 1) {\n let x = res[i] / 2;\n res.push(2 * (A[i] - x));\n }\n let ans = res\n .iter()\n .map(|&x| x.to_string())\n .collect::>()\n .join(\" \");\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0746", "problem_description": "You are given three strings $$$a$$$, $$$b$$$ and $$$c$$$ of the same length $$$n$$$. The strings consist of lowercase English letters only. The $$$i$$$-th letter of $$$a$$$ is $$$a_i$$$, the $$$i$$$-th letter of $$$b$$$ is $$$b_i$$$, the $$$i$$$-th letter of $$$c$$$ is $$$c_i$$$.For every $$$i$$$ ($$$1 \\leq i \\leq n$$$) you must swap (i.e. exchange) $$$c_i$$$ with either $$$a_i$$$ or $$$b_i$$$. So in total you'll perform exactly $$$n$$$ swap operations, each of them either $$$c_i \\leftrightarrow a_i$$$ or $$$c_i \\leftrightarrow b_i$$$ ($$$i$$$ iterates over all integers between $$$1$$$ and $$$n$$$, inclusive).For example, if $$$a$$$ is \"code\", $$$b$$$ is \"true\", and $$$c$$$ is \"help\", you can make $$$c$$$ equal to \"crue\" taking the $$$1$$$-st and the $$$4$$$-th letters from $$$a$$$ and the others from $$$b$$$. In this way $$$a$$$ becomes \"hodp\" and $$$b$$$ becomes \"tele\".Is it possible that after these swaps the string $$$a$$$ becomes exactly the same as the string $$$b$$$?", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int i = 0;\n int n = 0;\n char a[101];\n char b[101];\n char c[101];\n scanf(\"%s\", a);\n\n while (a[i] != '\\0') {\n n++;\n i++;\n }\n scanf(\"%s\", b);\n\n scanf(\"%s\", c);\n\n int flag = 1;\n for (i = 0; i < n; i++) {\n if (a[i] == b[i] && a[i] != c[i]) {\n flag = 0;\n break;\n }\n if (a[i] != b[i] && b[i] != c[i] && c[i] != a[i]) {\n flag = 0;\n break;\n }\n }\n if (flag == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read line\");\n buf.trim().parse::().unwrap()\n };\n 'outer: for _ in 0..t {\n let strings = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read\");\n stdin().read_line(&mut buf).expect(\"Failed to read\");\n stdin().read_line(&mut buf).expect(\"Failed to read\");\n let strings: Vec = buf.lines().map(|s| s.trim().to_string()).collect();\n strings\n };\n for ((a, b), c) in strings[0]\n .chars()\n .zip(strings[1].chars())\n .zip(strings[2].chars())\n {\n if c != a && c != b {\n println!(\"NO\");\n continue 'outer;\n }\n }\n println!(\"YES\");\n }\n}", "difficulty": "easy"} {"problem_id": "0747", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above.

\n

The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner?

\n
\n
\n
\n
\n

Constraints

    \n
  • -40 \\leq X \\leq 40
  • \n
  • X is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

Print Yes if you will turn on the air conditioner; print No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

25\n
\n
\n
\n
\n
\n

Sample Output 1

No\n
\n
\n
\n
\n
\n
\n

Sample Input 2

30\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n
\n
", "c_code": "int solution(void) {\n int temp = 0;\n\n scanf(\"%d\", &temp);\n\n if (temp > 29) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n match io::stdin().read_line(&mut input) {\n Ok(_) => match input.trim_end().parse::() {\n Ok(x) => println!(\"{}\", if x >= 30 { \"Yes\" } else { \"No\" }),\n Err(_) => eprintln!(\"not a number\"),\n },\n Err(_) => eprintln!(\"can't read anything\"),\n }\n}", "difficulty": "easy"} {"problem_id": "0748", "problem_description": "The main server of Gomble company received a log of one top-secret process, the name of which can't be revealed. The log was written in the following format: «[date:time]: message», where for each «[date:time]» value existed not more than 10 lines. All the files were encoded in a very complicated manner, and only one programmer — Alex — managed to decode them. The code was so complicated that Alex needed four weeks to decode it. Right after the decoding process was finished, all the files were deleted. But after the files deletion, Alex noticed that he saved the recordings in format «[time]: message». So, information about the dates was lost. However, as the lines were added into the log in chronological order, it's not difficult to say if the recordings could appear during one day or not. It is possible also to find the minimum amount of days during which the log was written.So, to make up for his mistake Alex has to find the minimum amount of days covered by the log. Note that Alex doesn't have to find the minimum amount of days between the beginning and the end of the logging, he has to find the minimum amount of dates in which records could be done. (See Sample test 2 for further clarifications).We should remind you that the process made not more than 10 recordings in a minute. Consider that a midnight belongs to coming day.", "c_code": "int solution() {\n\n char logs[50];\n char formato[1];\n char aux[2];\n int n;\n\n scanf(\"%d\", &n);\n while (getchar() != '\\n') {\n ;\n }\n\n int hora;\n int minuto;\n char format;\n\n int cant = 1;\n int limite = 1;\n unsigned short seguir = 1;\n int minutos1 = 0;\n int minutos2 = 0;\n\n for (int i = 0; i < n; i++) {\n\n fgets(logs, sizeof(logs), stdin);\n hora = atoi(strncpy(aux, logs + 1, 2));\n minuto = atoi(strncpy(aux, logs + 4, 2));\n strncpy(formato, logs + 7, 1);\n format = *formato;\n\n if (format == 'p' && hora == 12) {\n minutos2 = hora * 60 + minuto;\n } else {\n if (format == 'p') {\n minutos2 = hora * 60 + minuto + 12 * 60;\n } else {\n if (format == 'a' && hora == 12) {\n minutos2 = minuto;\n } else {\n minutos2 = hora * 60 + minuto;\n }\n }\n }\n\n if (minutos2 < minutos1) {\n limite = 1;\n seguir = 1;\n cant++;\n } else {\n if (minutos2 > minutos1) {\n limite = 1;\n seguir = 1;\n } else {\n if ((minutos2 == minutos1) && seguir == 1) {\n limite++;\n if (limite == 10) {\n limite = 1;\n seguir = 0;\n }\n } else {\n if (minutos2 == minutos1) {\n cant++;\n seguir = 1;\n }\n }\n }\n }\n\n minutos1 = minutos2;\n }\n\n printf(\"%d\\n\", cant);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut len = String::new();\n io::stdin().read_line(&mut len).unwrap();\n let n = len.trim().parse().unwrap();\n let mut last = -1;\n let mut day = 1;\n let mut count = 1;\n for _i in 0..n {\n let mut curr = String::new();\n io::stdin().read_line(&mut curr).unwrap();\n let mut hour: i32 = curr[1..3].parse().unwrap();\n let mut minute: i32 = curr[4..6].parse().unwrap();\n if &curr[7..8] == \"p\" && hour != 12 {\n hour += 12;\n }\n if &curr[7..8] == \"a\" && hour == 12 {\n hour -= 12;\n }\n minute += hour * 60;\n if minute < last {\n count = 1;\n day += 1;\n } else if minute == last {\n count += 1;\n if count > 10 {\n count = 1;\n day += 1;\n }\n } else {\n count = 1;\n }\n last = minute;\n }\n print!(\"{}\", day);\n}", "difficulty": "easy"} {"problem_id": "0749", "problem_description": "

科目選択 (Selecting Subjects)

\n\n\n

問題

\n

\nJOI 君は物理,化学,生物,地学,歴史,地理の 6 科目のテストを受けた.\nそれぞれのテストは 100 点満点で採点された.\n

\n\n

\nJOI 君は物理,化学,生物,地学の 4 科目から 3 科目を選択し,歴史,地理の 2 科目から 1 科目を選択する.\n

\n\n

\nテストの合計点が最も高くなるように科目を選ぶとき,\nJOI 君の選んだ科目のテストの合計点を求めよ.\n

\n\n

入力

\n

\n入力は 6 行からなり,1 行に 1 つずつ整数が書かれている.\n

\n\n

\n1 行目には JOI 君の物理のテストの点数 A が書かれている.
\n2 行目には JOI 君の化学のテストの点数 B が書かれている.
\n3 行目には JOI 君の生物のテストの点数 C が書かれている.
\n4 行目には JOI 君の地学のテストの点数 D が書かれている.
\n5 行目には JOI 君の歴史のテストの点数 E が書かれている.
\n6 行目には JOI 君の地理のテストの点数 F が書かれている.\n

\n\n\n

\n書かれている整数 A, B, C, D, E, F はすべて 0 以上 100 以下である.\n

\n\n

出力

\n

\nJOI 君が選んだ科目のテストの合計点を 1 行で出力せよ.\n

\n\n

入出力例

\n\n\n

入力例 1

\n\n
\n100\n34\n76\n42\n10\n0\n
\n\n

出力例 1

\n\n
\n228
\n\n

入力例 2

\n
\n15\n21\n15\n42\n15\n62\n
\n\n

出力例 2

\n\n
\n140
\n\n\n

\n入出力例 1 では,JOI 君が物理,生物,地学,歴史の 4 科目を選ぶとき,テストの合計点が最高になる.\n

\n\n

\n物理,生物,地学,歴史の点数はそれぞれ 100, 76, 42, 10 なので,選んだ科目のテストの合計点は 228 である. \n

\n\n

\n入出力例 2 では,JOI 君が化学,生物,地学,地理の 4 科目を選ぶとき,テストの合計点が最高になる.\n

\n\n

\n化学,生物,地学,地理の点数はそれぞれ 21, 15, 42, 62 なので,選んだ科目のテストの合計点は 140 である.\n

\n\n

\n入出力例 2 では,JOI 君が物理,化学,地学,地理の 4 科目を選ぶ場合でも,選んだテストの合計点は 140 である.\n

\n\n\n", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int e = 0;\n int f = 0;\n int t1 = 0;\n int t2 = 0;\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n scanf(\"%d\", &c);\n scanf(\"%d\", &d);\n scanf(\"%d\", &e);\n scanf(\"%d\", &f);\n if (a <= b && a <= c && a <= d) {\n t1 = b + c + d;\n }\n if (b <= a && b <= c && b <= d) {\n t1 = a + c + d;\n }\n if (c <= a && c <= b && c <= d) {\n t1 = a + b + d;\n }\n if (d <= a && d <= b && d <= c) {\n t1 = a + b + c;\n }\n if (e < f) {\n t2 = f;\n } else {\n t2 = e;\n }\n printf(\"%d\\n\", t1 + t2);\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 lines = input.split('\\n');\n\n let mut iter = lines.map(|l| l.parse::().unwrap());\n\n let (sum, min) = iter.by_ref().take(4).fold((0, 100), |(sum, min), n| {\n (sum + n, if n < min { n } else { min })\n });\n let max = iter.take(2).max().unwrap();\n\n println!(\"{}\", sum - min + max);\n}", "difficulty": "medium"} {"problem_id": "0750", "problem_description": "You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits.", "c_code": "int solution() {\n int x = 100000;\n int n;\n char a[100001];\n while (x--) {\n a[x] = '5';\n }\n a[100001] = '\\0';\n scanf(\"%d\", &n);\n while (n--) {\n scanf(\"%d\", &x);\n if (x == 1) {\n printf(\"-1\\n\");\n } else {\n x--, x = 100000 - x;\n printf(\"%s\", &a[x]);\n printf(\"8\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input).unwrap();\n\n let t: i32 = input.trim().parse().unwrap();\n\n for _t in 0..t {\n let mut input2 = String::new();\n io::stdin().read_line(&mut input2).unwrap();\n\n let n: i32 = input2.trim().parse().unwrap();\n\n if n == 1 {\n println!(\"-1\");\n } else {\n print!(\"2\");\n for _i in 1..n {\n print!(\"3\");\n }\n println!();\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0751", "problem_description": "There's a chip in the point $$$(0, 0)$$$ of the coordinate plane. In one operation, you can move the chip from some point $$$(x_1, y_1)$$$ to some point $$$(x_2, y_2)$$$ if the Euclidean distance between these two points is an integer (i.e. $$$\\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}$$$ is integer).Your task is to determine the minimum number of operations required to move the chip from the point $$$(0, 0)$$$ to the point $$$(x, y)$$$.", "c_code": "int solution() {\n int tc;\n scanf(\"%d\", &tc);\n while (tc--) {\n int x;\n int y;\n int z = 0;\n int m = 1;\n scanf(\"%d%d\", &x, &y);\n while (m * m <= (x * x + y * y)) {\n if (m * m == (x * x + y * y)) {\n z++;\n break;\n }\n m++;\n }\n if (x == 0 && y == 0) {\n printf(\"0\\n\");\n } else if (z > 0) {\n printf(\"1\\n\");\n } else {\n printf(\"2\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let t: i64 = input.trim().parse().unwrap();\n for _ in 0..t {\n let mut tc = String::new();\n stdin().read_line(&mut tc).unwrap();\n let xy: Vec<_> = tc.split(' ').map(|a| a.trim()).collect();\n let x: i64 = xy[0].parse().unwrap();\n let y: i64 = xy[1].parse().unwrap();\n match (x, y) {\n (0, 0) => {\n println!(\"0\");\n }\n (x, y) if (x * x + y * y == ((x * x + y * y) as f64).sqrt().floor().powi(2) as i64) => {\n println!(\"1\");\n }\n (_, _) => {\n println!(\"2\");\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0752", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.\n(Assume that the positive x-axis points right, and the positive y-axis points up.)

\n

Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).

\n

Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.

\n
\n
\n
\n
\n

Constraints

    \n
  • |x_1|,|y_1|,|x_2|,|y_2| \\leq 100
  • \n
  • (x_1,y_1)(x_2,y_2)
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
x_1 y_1 x_2 y_2\n
\n
\n
\n
\n
\n

Output

Print x_3,y_3,x_4 and y_4 as integers, in this order.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

0 0 0 1\n
\n
\n
\n
\n
\n

Sample Output 1

-1 1 -1 0\n
\n

(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.\nNote that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 3 6 6\n
\n
\n
\n
\n
\n

Sample Output 2

3 10 -1 7\n
\n
\n
\n
\n
\n
\n

Sample Input 3

31 -41 -59 26\n
\n
\n
\n
\n
\n

Sample Output 3

-126 -64 -36 -131\n
\n
\n
", "c_code": "int solution() {\n int x1 = 0;\n int y1 = 0;\n int x2 = 0;\n int y2 = 0;\n scanf(\"%d %d %d %d\", &x1, &y1, &x2, &y2);\n int len1 = x2 - x1;\n int len2 = y2 - y1;\n printf(\"%d %d %d %d\", x2 - len2, y2 + len1, x1 - len2, y1 + len1);\n return 0;\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n let mut reader = BufReader::new(reader.lock());\n\n let mut s = String::new();\n\n let _ = reader.read_line(&mut s);\n\n let xs: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n let x1 = xs[0];\n let y1 = xs[1];\n let x2 = xs[2];\n let y2 = xs[3];\n\n let dis_x = x2 - x1;\n let dis_y = y2 - y1;\n\n println!(\n \"{} {} {} {}\",\n x2 - dis_y,\n y2 + dis_x,\n x1 - dis_y,\n y1 + dis_x\n );\n}", "difficulty": "easy"} {"problem_id": "0753", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

N players will participate in a tennis tournament. We will call them Player 1, Player 2, \\ldots, Player N.

\n

The tournament is round-robin format, and there will be N(N-1)/2 matches in total.\nIs it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

\n
    \n
  • Each player plays at most one matches in a day.
  • \n
  • Each player i (1 \\leq i \\leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \\ldots, A_{i, N-1} in this order.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 1000
  • \n
  • 1 \\leq A_{i, j} \\leq N
  • \n
  • A_{i, j} \\neq i
  • \n
  • A_{i, 1}, A_{i, 2}, \\ldots, A_{i, N-1} are all different.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_{1, 1} A_{1, 2} \\ldots A_{1, N-1}\nA_{2, 1} A_{2, 2} \\ldots A_{2, N-1}\n:\nA_{N, 1} A_{N, 2} \\ldots A_{N, N-1}\n
\n
\n
\n
\n
\n

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2 3\n1 3\n1 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

All the conditions can be satisfied if the matches are scheduled for three days as follows:

\n
    \n
  • Day 1: Player 1 vs Player 2
  • \n
  • Day 2: Player 1 vs Player 3
  • \n
  • Day 3: Player 2 vs Player 3
  • \n
\n

This is the minimum number of days required.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n2 3 4\n1 3 4\n4 1 2\n3 1 2\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n

All the conditions can be satisfied if the matches are scheduled for four days as follows:

\n
    \n
  • Day 1: Player 1 vs Player 2, Player 3 vs Player 4
  • \n
  • Day 2: Player 1 vs Player 3
  • \n
  • Day 3: Player 1 vs Player 4, Player 2 vs Player 3
  • \n
  • Day 4: Player 2 vs Player 4
  • \n
\n

This is the minimum number of days required.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n2 3\n3 1\n1 2\n
\n
\n
\n
\n
\n

Sample Output 3

-1\n
\n

Any scheduling of the matches violates some condition.

\n
\n
", "c_code": "int solution() {\n int N;\n (void)scanf(\"%d\", &N);\n\n int A[1001][1001];\n int *P[1001];\n\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= N - 1; j++) {\n (void)scanf(\"%d\", &(A[i][j]));\n }\n A[i][0] = 0;\n A[i][N] = 0;\n P[i] = A[i] + 1;\n }\n\n int I = 1;\n int D = 1;\n while (I <= N) {\n if (*P[I] <= 0) {\n I++;\n continue;\n }\n\n int noMatch = 1;\n for (int i = I; i <= N; i++) {\n int *player1 = P[i];\n if (*player1 == 0) {\n continue;\n }\n int *player2 = P[*player1];\n\n if (*(player1 - 1) == -D) {\n continue;\n }\n if (*(player2 - 1) == -D) {\n continue;\n }\n\n if (*player2 == i) {\n P[i]++;\n P[*player1]++;\n\n *player1 = -D;\n *player2 = -D;\n\n noMatch = 0;\n }\n }\n\n if (noMatch) {\n printf(\"-1\\n\");\n return 0;\n }\n D++;\n }\n\n printf(\"%d\\n\", D - 1);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let mut xss: Vec> = Vec::new();\n for _ in 0..n {\n let zs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse::().unwrap() - 1)\n .collect();\n xss.push(zs);\n }\n let mut ixs: Vec = vec![0; n];\n let mut ds: Vec = vec![0; n];\n let mut i: usize = 0;\n let mut updated = false;\n loop {\n if ixs[i] < xss[i].len() {\n let j = xss[i][ixs[i]];\n if ixs[j] < xss[j].len() {\n let k = xss[j][ixs[j]];\n if i == k {\n updated = true;\n ixs[i] += 1;\n ixs[j] += 1;\n let d = max(ds[i], ds[j]) + 1;\n ds[i] = d;\n ds[j] = d;\n }\n }\n }\n i += 1;\n if n <= i {\n if !updated {\n break;\n }\n i = 0;\n updated = false;\n }\n }\n let ok = (0..n).all(|i| xss[i].len() == ixs[i]);\n if ok {\n println!(\"{}\", ds.iter().max().unwrap());\n } else {\n println!(\"-1\");\n }\n}", "difficulty": "hard"} {"problem_id": "0754", "problem_description": "

Change-Making Problem

\n\n

You want to make change for $n$ cents. Assuming that you have infinite supply of coins of 1, 5, 10 and/or 25 cents coins respectively, find the minimum number of coins you need.

\n\n

Input

\n\n
\n$n$\n
\n\n

The integer $n$ is given in a line.

\n\n

出力

\n\n

Print the minimum number of coins you need in a line.

\n\n

Constraints

\n\n
    \n
  • $1 \\le n \\le 10^9$
  • \n
\n\n

Sample Input 1

\n
\n100\n
\n

Sample Output 1

\n
\n4\n
\n\n

Sample Input 2

\n
\n54321\n
\n

Sample Output 2

\n
\n2175\n
", "c_code": "int solution(void) {\n int cent;\n scanf(\"%d\", ¢);\n\n int n = cent / 25;\n cent -= (cent / 25) * 25;\n n += cent / 10;\n cent -= (cent / 10) * 10;\n n += cent / 5;\n cent -= (cent / 5) * 5;\n n += cent;\n\n printf(\"%d\\n\", n);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).expect(\"Input error\");\n let mut change = input.trim().parse::().expect(\"Parse error\");\n let mut total_coin = 0;\n if change >= 25 {\n total_coin += change / 25;\n change %= 25;\n }\n if change >= 10 {\n total_coin += change / 10;\n change %= 10;\n }\n if change >= 5 {\n total_coin += change / 5;\n change %= 5;\n }\n total_coin += change;\n println!(\"{}\", total_coin);\n}", "difficulty": "easy"} {"problem_id": "0755", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Ibis is fighting with a monster.

\n

The health of the monster is H.

\n

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

\n

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

\n

Ibis wins when the health of the monster becomes 0 or below.

\n

Find the minimum total Magic Points that have to be consumed before winning.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H \\leq 10^4
  • \n
  • 1 \\leq N \\leq 10^3
  • \n
  • 1 \\leq A_i \\leq 10^4
  • \n
  • 1 \\leq B_i \\leq 10^4
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H N\nA_1 B_1\n:\nA_N B_N\n
\n
\n
\n
\n
\n

Output

Print the minimum total Magic Points that have to be consumed before winning.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

9 3\n8 3\n4 2\n2 1\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

\n

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

\n

In this way, we can win at the total cost of 4 Magic Points.

\n
\n
\n
\n
\n
\n

Sample Input 2

100 6\n1 1\n2 3\n3 9\n4 27\n5 81\n6 243\n
\n
\n
\n
\n
\n

Sample Output 2

100\n
\n

It is optimal to cast the first spell 100 times.

\n
\n
\n
\n
\n
\n

Sample Input 3

9999 10\n540 7550\n691 9680\n700 9790\n510 7150\n415 5818\n551 7712\n587 8227\n619 8671\n588 8228\n176 2461\n
\n
\n
\n
\n
\n

Sample Output 3

139815\n
\n
\n
", "c_code": "int solution() {\n int h;\n int n;\n scanf(\"%d%d\", &h, &n);\n int a[n];\n int b[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d%d\", a + i, b + i);\n }\n int dp[h + 1];\n for (int i = 0; i < h + 1; i++) {\n dp[i] = INT_MAX;\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < h + 1; j++) {\n if (j - a[i] <= 0) {\n if (dp[j] < b[i]) {\n continue;\n }\n dp[j] = b[i];\n\n } else if (dp[j] < dp[j - a[i]] + b[i]) {\n continue;\n } else {\n dp[j] = dp[j - a[i]] + b[i];\n }\n }\n }\n printf(\"%d\\n\", dp[h]);\n return 0;\n}", "rust_code": "fn solution() {\n let (h, waza) = {\n let mut i = String::new();\n io::stdin().read_line(&mut i).unwrap();\n let mut it = i.split_whitespace();\n let (h, n): (usize, usize) = {\n (\n it.next().unwrap().parse().unwrap(),\n it.next().unwrap().parse().unwrap(),\n )\n };\n let mut waza: Vec<(usize, usize)> = Vec::new();\n for _ in 0..n {\n let mut i = String::new();\n io::stdin().read_line(&mut i).unwrap();\n let mut it = i.split_whitespace();\n waza.push((\n it.next().unwrap().parse().unwrap(),\n it.next().unwrap().parse().unwrap(),\n ))\n }\n (h, waza)\n };\n\n let mut dp: Vec = Vec::new();\n dp.push(0);\n for i in 1..h + 1 {\n let mut min_mp = std::usize::MAX;\n for m in &waza {\n let tmp_mp = if i > m.0 { dp[i - m.0] + m.1 } else { m.1 };\n\n min_mp = std::cmp::min(min_mp, tmp_mp);\n }\n\n dp.push(min_mp);\n }\n println!(\"{}\", dp[h]);\n}", "difficulty": "medium"} {"problem_id": "0756", "problem_description": "Nastia has $$$2$$$ positive integers $$$A$$$ and $$$B$$$. She defines that: The integer is good if it is divisible by $$$A \\cdot B$$$; Otherwise, the integer is nearly good, if it is divisible by $$$A$$$. For example, if $$$A = 6$$$ and $$$B = 4$$$, the integers $$$24$$$ and $$$72$$$ are good, the integers $$$6$$$, $$$660$$$ and $$$12$$$ are nearly good, the integers $$$16$$$, $$$7$$$ are neither good nor nearly good.Find $$$3$$$ different positive integers $$$x$$$, $$$y$$$, and $$$z$$$ such that exactly one of them is good and the other $$$2$$$ are nearly good, and $$$x + y = z$$$.", "c_code": "int solution() {\n long long int t;\n long long int a[10000];\n long long int b[10000];\n long long int x[10000];\n long long int y[10000];\n long long int z[10000];\n long long int c[10000];\n long long int k[10000];\n scanf(\"%lld\", &t);\n for (int i = 0; i < t; i++) {\n k[i] = 0;\n scanf(\"%lld %lld\", &a[i], &b[i]);\n if (b[i] != 1) {\n k[i]++;\n x[i] = a[i];\n y[i] = a[i] * b[i];\n z[i] = x[i] + y[i];\n } else {\n continue;\n }\n }\n for (int j = 0; j < t; j++) {\n if (k[j] == 1) {\n printf(\"YES\\n%lld %lld %lld\\n\", x[j], y[j], z[j]);\n } else {\n printf(\"NO\\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(|x| x.unwrap());\n\n let t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let a = it.next().unwrap();\n let b = it.next().unwrap();\n\n if b == 1 {\n writeln!(&mut output, \"NO\").unwrap();\n continue;\n }\n\n let x = a;\n let y = a * (2 * b - 1);\n let z = x + y;\n\n writeln!(&mut output, \"YES\\n{} {} {}\", x, y, z).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0757", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

\n

We will choose exactly \\left\\lfloor \\frac{N}{2} \\right\\rfloor elements from this sequence so that no two adjacent elements are chosen.

\n

Find the maximum possible sum of the chosen elements.

\n

Here \\lfloor x \\rfloor denotes the greatest integer not greater than x.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 2\\times 10^5
  • \n
  • |A_i|\\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible sum of the chosen elements.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n1 2 3 4 5 6\n
\n
\n
\n
\n
\n

Sample Output 1

12\n
\n

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n-1000 -100 -10 0 10\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

\n
\n
\n
\n
\n
\n

Sample Input 3

10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

5000000000\n
\n

Watch out for overflow.

\n
\n
\n
\n
\n
\n

Sample Input 4

27\n18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49\n
\n
\n
\n
\n
\n

Sample Output 4

295\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int i;\n int j;\n long long int a[200005];\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n long long int dp[200005][3];\n long long int min = -100000000000000000;\n dp[0][0] = a[0];\n dp[0][1] = dp[0][2] = min;\n dp[1][1] = a[1];\n dp[1][0] = dp[1][2] = min;\n for (i = 2; i < n; i++) {\n for (j = 0; j < 3; j++) {\n if (dp[i - 2][j] > min) {\n dp[i][j] = dp[i - 2][j] + a[i];\n } else {\n dp[i][j] = min;\n }\n }\n if (i > 2) {\n for (j = 0; j < 2; j++) {\n if (dp[i - 3][j] > min && dp[i][j + 1] < dp[i - 3][j] + a[i]) {\n dp[i][j + 1] = dp[i - 3][j] + a[i];\n }\n }\n } else {\n if (dp[i][2] < a[i]) {\n dp[i][2] = a[i];\n }\n }\n if (i > 3) {\n if (dp[i - 4][0] > min && dp[i][2] < dp[i - 4][0] + a[i]) {\n dp[i][2] = dp[i - 4][0] + a[i];\n }\n }\n }\n long long int ans;\n if (n % 2 > 0) {\n ans = dp[n - 3][0];\n if (ans < dp[n - 2][1]) {\n ans = dp[n - 2][1];\n }\n if (ans < dp[n - 1][2]) {\n ans = dp[n - 1][2];\n }\n } else {\n ans = dp[n - 2][0];\n if (ans < dp[n - 1][1]) {\n ans = dp[n - 1][1];\n }\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let line = lines.next().unwrap().unwrap();\n let n: usize = line.parse().unwrap();\n let line = lines.next().unwrap().unwrap();\n let xs: Vec = line.split(' ').map(|x| x.parse().unwrap()).collect();\n let mut memo: Vec = vec![0; n];\n let mut leftmost: i64 = xs[0];\n for i in 1..n {\n if i == 1 {\n memo[i] = max(xs[0], xs[1]);\n } else if i % 2 == 0 {\n leftmost += xs[i];\n memo[i] = max(memo[i - 2] + xs[i], memo[i - 1]);\n } else {\n memo[i] = max(leftmost, memo[i - 2] + xs[i]);\n }\n }\n println!(\"{}\", memo[n - 1]);\n}", "difficulty": "medium"} {"problem_id": "0758", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.\nWe can repeatedly perform the following operation: choose a character in T and replace it with a different character.\nFind the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 100
  • \n
  • S consists of lowercase English letters.
  • \n
  • 1 \\leq K \\leq 10^9
  • \n
  • K is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nK\n
\n
\n
\n
\n
\n

Output

Print the minimum number of operations required.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

issii\n2\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

\n
\n
\n
\n
\n
\n

Sample Input 2

qq\n81\n
\n
\n
\n
\n
\n

Sample Output 2

81\n
\n
\n
\n
\n
\n
\n

Sample Input 3

cooooooooonteeeeeeeeeest\n999993333\n
\n
\n
\n
\n
\n

Sample Output 3

8999939997\n
\n
\n
", "c_code": "int solution() {\n char s[101];\n long long k;\n scanf(\"%s %lld\", s, &k);\n int l = strlen(s);\n if (l == 1) {\n printf(\"%lld\", k / 2);\n return 0;\n }\n int inner = 0;\n int cnt = 0;\n int uni = 0;\n for (int i = 0; i < l; i++) {\n if (s[i] == s[i + 1]) {\n cnt++;\n } else if (cnt) {\n inner += (cnt + 1) / 2;\n if (cnt == l - 1) {\n uni = 1;\n }\n cnt = 0;\n }\n }\n if (uni == 1) {\n printf(\"%lld\", l * k / 2);\n return 0;\n }\n int head = 1;\n int tail = 1;\n int chk = 0;\n long long ans = inner * k;\n if (s[0] == s[l - 1]) {\n chk = 2;\n if (s[0] == s[1]) {\n int i = 0;\n while (s[i] == s[i + 1]) {\n head++;\n i++;\n }\n }\n if (s[l - 1] == s[l - 2]) {\n int i = 0;\n while (s[l - (1 + i)] == s[l - (2 + i)]) {\n tail++;\n i++;\n }\n }\n }\n\n if (chk == 2 && head % 2 == 1 && tail % 2 == 1) {\n ans += (k - 1);\n }\n printf(\"%lld\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let S: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n };\n let K: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n let N = S.len();\n let ans = if S.iter().collect::>().len() == 1 {\n N * K / 2\n } else if S[0] == S[N - 1] {\n let c = S[0];\n let l = (0..N).take_while(|&i| S[i] == c).count();\n let r = (0..N).rev().take_while(|&i| S[i] == c).count();\n let (m, _) = (l..(N - r)).fold((0, '-'), |(res, prev), i| {\n if S[i] == prev {\n (res + 1, '-')\n } else {\n (res, S[i])\n }\n });\n l / 2 + m * K + (l + r) / 2 * (K - 1) + r / 2\n } else {\n let (m, _) = (0..N).fold((0, '-'), |(res, prev), i| {\n if S[i] == prev {\n (res + 1, '-')\n } else {\n (res, S[i])\n }\n });\n m * K\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0759", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

\n

You are visiting a large electronics store to buy a refrigerator and a microwave.

\n

The store sells A kinds of refrigerators and B kinds of microwaves. The i-th refrigerator ( 1 \\le i \\le A ) is sold at a_i yen (the currency of Japan), and the j-th microwave ( 1 \\le j \\le B ) is sold at b_j yen.

\n

You have M discount tickets. With the i-th ticket ( 1 \\le i \\le M ), you can get a discount of c_i yen from the total price when buying the x_i-th refrigerator and the y_i-th microwave together. Only one ticket can be used at a time.

\n

You are planning to buy one refrigerator and one microwave. Find the minimum amount of money required.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • All values in input are integers.
  • \n
  • 1 \\le A \\le 10^5
  • \n
  • 1 \\le B \\le 10^5
  • \n
  • 1 \\le M \\le 10^5
  • \n
  • 1 \\le a_i , b_i , c_i \\le 10^5
  • \n
  • 1 \\le x_i \\le A
  • \n
  • 1 \\le y_i \\le B
  • \n
  • c_i \\le a_{x_i} + b_{y_i}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
A B M\na_1 a_2 ... a_A\nb_1 b_2 ... b_B\nx_1 y_1 c_1\n\\vdots\nx_M y_M c_M\n
\n
\n
\n
\n
\n

Output

\n

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 1\n3 3\n3 3 3\n1 2 1\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

With the ticket, you can get the 1-st refrigerator and the 2-nd microwave for 3+3-1=5 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 1 2\n10\n10\n1 1 5\n1 1 10\n
\n
\n
\n
\n
\n

Sample Output 2

10\n
\n

Note that you cannot use more than one ticket at a time.

\n
\n
\n
\n
\n
\n

Sample Input 3

2 2 1\n3 5\n3 5\n2 2 2\n
\n
\n
\n
\n
\n

Sample Output 3

6\n
\n

You can get the 1-st refrigerator and the 1-st microwave for 6 yen, which is the minimum amount to pay in this case.\nNote that using a ticket is optional.

\n
\n
", "c_code": "int solution(void) {\n int i = 0;\n int rei = 0;\n int densi = 0;\n int m = 0;\n scanf(\"%d\", &rei);\n scanf(\"%d\", &densi);\n scanf(\"%d\", &m);\n int rprice[rei];\n int dprice[densi];\n int waribiki1[rei];\n int waribiki2[densi];\n int a[m];\n int b[m];\n int c[m];\n int rmin = 100000;\n for (i = 0; i < rei; i++) {\n scanf(\"%d\", &rprice[i]);\n if (rprice[i] < rmin) {\n rmin = rprice[i];\n }\n }\n int dmin = 100000;\n for (i = 0; i < densi; i++) {\n scanf(\"%d\", &dprice[i]);\n if (dprice[i] < dmin) {\n dmin = dprice[i];\n }\n }\n for (i = 0; i < m; i++) {\n scanf(\"%d\", &a[i]);\n scanf(\"%d\", &b[i]);\n scanf(\"%d\", &c[i]);\n }\n int min = 0;\n min = rmin + dmin;\n for (i = 0; i < m; i++) {\n if (rprice[a[i] - 1] + dprice[b[i] - 1] - c[i] < min) {\n min = rprice[a[i] - 1] + dprice[b[i] - 1] - c[i];\n }\n }\n printf(\"%d\", min);\n return 0;\n}", "rust_code": "fn solution() {\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n input! {\n a: usize,\n b: usize,\n m: usize,\n aa: [i64; a],\n bb: [i64; b],\n ti: [(usize1, usize1, i64); m],\n }\n let mut ans = 1i64 << 60;\n for &(ai, bi, c) in &ti {\n ans = cmp::min(ans, aa[ai] + bb[bi] - c);\n }\n let am = aa.iter().min().unwrap();\n let bm = bb.iter().min().unwrap();\n ans = cmp::min(ans, am + bm);\n\n puts!(\"{}\\n\", ans);\n}", "difficulty": "medium"} {"problem_id": "0760", "problem_description": "Codeforces separates its users into $$$4$$$ divisions by their rating: For Division 1: $$$1900 \\leq \\mathrm{rating}$$$ For Division 2: $$$1600 \\leq \\mathrm{rating} \\leq 1899$$$ For Division 3: $$$1400 \\leq \\mathrm{rating} \\leq 1599$$$ For Division 4: $$$\\mathrm{rating} \\leq 1399$$$ Given a $$$\\mathrm{rating}$$$, print in which division the $$$\\mathrm{rating}$$$ belongs.", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d\", &n);\n if (n <= 1399) {\n printf(\"Division 4\\n\");\n }\n if (n > 1399 && n <= 1599) {\n printf(\"Division 3\\n\");\n }\n if (n > 1599 && n <= 1899) {\n printf(\"Division 2\\n\");\n }\n if (n >= 1900) {\n printf(\"Division 1\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut input_line = String::new();\n io::stdin()\n .read_line(&mut input_line)\n .expect(\"Failed to read line\");\n\n let nt: u32 = input_line.trim().parse().expect(\"Failed to read line\");\n\n for _tc in 0..nt {\n let mut input_line = String::new();\n io::stdin()\n .read_line(&mut input_line)\n .expect(\"Failed to read line\");\n let r: i32 = input_line.trim().parse().expect(\"Input is not integer\");\n\n match r {\n -5000..=1399 => println!(\"Division 4\"),\n 1400..=1599 => println!(\"Division 3\"),\n 1600..=1899 => println!(\"Division 2\"),\n 1900..=5000 => println!(\"Division 1\"),\n _ => {}\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0761", "problem_description": "Burenka came to kindergarden. This kindergarten is quite strange, so each kid there receives two fractions ($$$\\frac{a}{b}$$$ and $$$\\frac{c}{d}$$$) with integer numerators and denominators. Then children are commanded to play with their fractions.Burenka is a clever kid, so she noticed that when she claps once, she can multiply numerator or denominator of one of her two fractions by any integer of her choice (but she can't multiply denominators by $$$0$$$). Now she wants know the minimal number of claps to make her fractions equal (by value). Please help her and find the required number of claps!", "c_code": "int solution(void) {\n int i;\n int j;\n scanf(\"%d\", &i);\n long long a[i];\n long long b[i];\n long long c[i];\n long long d[i];\n long long x;\n long long y;\n for (j = 0; j < i; j++) {\n scanf(\"%lld %lld %lld %lld\", &a[j], &b[j], &c[j], &d[j]);\n }\n for (j = 0; j < i; j++) {\n x = a[j] * d[j];\n y = b[j] * c[j];\n if (x == y) {\n printf(\"0\");\n } else if (a[j] == 0 || c[j] == 0 || x % y == 0 || y % x == 0) {\n printf(\"1\");\n } else {\n printf(\"2\");\n }\n printf(\"\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_to_string(&mut s).unwrap();\n let mut mp = s.split_whitespace().map(|x| x.parse::().unwrap());\n\n let t = mp.next().unwrap();\n let mut s = String::new();\n\n for _ in 0..t {\n let (a, b, c, d) = (\n mp.next().unwrap(),\n mp.next().unwrap(),\n mp.next().unwrap(),\n mp.next().unwrap(),\n );\n\n let x = a * d;\n let y = b * c;\n\n if x == y {\n s += \"0\\n\";\n } else if x == 0 || y == 0 {\n s += \"1\\n\";\n } else if x % y == 0 || y % x == 0 {\n s += \"1\\n\";\n } else {\n s += \"2\\n\";\n }\n }\n\n print!(\"{}\", s.trim());\n}", "difficulty": "easy"} {"problem_id": "0762", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

AtCoDeer the deer found two rectangles lying on the table, each with height 1 and width W.\nIf we consider the surface of the desk as a two-dimensional plane, the first rectangle covers the vertical range of [0,1] and the horizontal range of [a,a+W], and the second rectangle covers the vertical range of [1,2] and the horizontal range of [b,b+W], as shown in the following figure:

\n

\"\"

\n

AtCoDeer will move the second rectangle horizontally so that it connects with the first rectangle.\nFind the minimum distance it needs to be moved.

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1≤W≤10^5
  • \n
  • 1≤a,b≤10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
W a b\n
\n
\n
\n
\n
\n

Output

Print the minimum distance the second rectangle needs to be moved.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2 6\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

This input corresponds to the figure in the statement. In this case, the second rectangle should be moved to the left by a distance of 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 1 3\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

The rectangles are already connected, and thus no move is needed.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 10 1\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n
\n
", "c_code": "int solution() {\n\n int W = 0;\n int A = 0;\n int B = 0;\n int save = 0;\n int length = 0;\n\n scanf(\"%d %d %d\", &W, &A, &B);\n if (A > B) {\n save = A;\n A = B;\n B = save;\n }\n\n if ((A + W) >= B) {\n length = 0;\n }\n if ((A + W) < B) {\n length = B - (A + W);\n }\n\n printf(\"%d\", length);\n\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 v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n println!(\n \"{}\",\n cmp::max(0, cmp::max(v[1], v[2]) - (cmp::min(v[1], v[2]) + v[0]))\n );\n}", "difficulty": "medium"} {"problem_id": "0763", "problem_description": "You have an array $$$a$$$ of length $$$n$$$. You can exactly once select an integer $$$len$$$ between $$$1$$$ and $$$n - 1$$$ inclusively, and then sort in non-decreasing order the prefix of the array of length $$$len$$$ and the suffix of the array of length $$$n - len$$$ independently.For example, if the array is $$$a = [3, 1, 4, 5, 2]$$$, and you choose $$$len = 2$$$, then after that the array will be equal to $$$[1, 3, 2, 4, 5]$$$.Could it be that after performing this operation, the array will not be sorted in non-decreasing order?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int x = 0;\n scanf(\"%d\", &n);\n int a[n + 10];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (i >= 1 && a[i] < a[i - 1]) {\n x = 1;\n }\n }\n if (x == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (r, mut lc) = (BufReader::new(io::stdin()), 0);\n for l in r.lines() {\n let v = l\n .unwrap()\n .split(\" \")\n .map(i32::from_str)\n .collect::, _>>();\n let (mut m, mut pf) = (0, false);\n for x in v.unwrap() {\n if lc > 0 && lc & 1 == 0 {\n m = cmp::max(m, x);\n if x < m {\n pf = true;\n break;\n }\n }\n }\n if lc > 0 && lc & 1 == 0 {\n println!(\"{}\", if pf { \"YES\" } else { \"NO\" })\n }\n lc += 1;\n }\n}", "difficulty": "medium"} {"problem_id": "0764", "problem_description": "\n\n\n\n

The Smallest Window I

\n\n

\n For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $S$, find the smallest sub-array size (smallest window length) where the sum of the sub-array is greater than or equal to $S$. If there is not such sub-array, report 0.\n

\n\n

Constraints

\n
    \n
  • $1 \\leq N \\leq 10^5$
  • \n
  • $1 \\leq S \\leq 10^9$
  • \n
  • $1 \\leq a_i \\leq 10^4$
  • \n
\n\n

Input

\n\n

The input is given in the following format.

\n\n

\n$N$ $S$
\n$a_1$ $a_2$ ... $a_N$
\n

\n\n

Output

\n\n

\n Print the smallest sub-array size in a line.\n

\n\n

Sample Input 1

\n
\n6 4\n1 2 1 2 3 2\n
\n\n

Sample Output 1

\n
\n2\n
\n\n\n\n

Sample Input 2

\n
\n6 6\n1 2 1 2 3 2\n
\n\n

Sample Output 2

\n
\n3\n
\n\n\n

Sample Input 3

\n
\n3 7\n1 2 3\n
\n\n

Sample Output 3

\n
\n0\n
", "c_code": "int solution() {\n int length;\n int sum;\n int i;\n int left = 0;\n int right = 0;\n int currentSmallest = 1000000;\n scanf(\" %d %d\", &length, &sum);\n int *array = (int *)malloc(sizeof(int[length]));\n for (i = 0; i < length; i++) {\n scanf(\" %d\", array + i);\n }\n int currentSum = array[0];\n\n while (1) {\n if (currentSmallest == 1) {\n printf(\"%d\\n\", 1);\n return 0;\n }\n int subLength = right - left + 1;\n if (subLength >= currentSmallest) {\n\n currentSum -= array[left++];\n continue;\n }\n\n if (currentSum >= sum) {\n currentSmallest = subLength;\n currentSum -= array[left++];\n continue;\n }\n if (right == length - 1) {\n\n if (left == 0) {\n printf(\"%d\\n\", 0);\n return 0;\n }\n printf(\"%d\\n\", currentSmallest);\n return 0;\n }\n\n currentSum += array[++right];\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut buf = String::new();\n let _ = stdin.read_to_string(&mut buf);\n let mut iter = buf.split_whitespace();\n\n let n: usize = iter.next().unwrap().parse().unwrap();\n let s = iter.next().unwrap().parse().unwrap();\n let a: Vec = iter.map(|nstr| nstr.parse().unwrap()).collect();\n\n let mut i = 0;\n let mut j = 0;\n let mut sum = 0;\n let mut ans = std::usize::MAX;\n while j <= n {\n if sum < s {\n if j == n {\n break;\n }\n sum += a[j];\n j += 1;\n } else {\n ans = min(ans, j - i);\n sum -= a[i];\n i += 1;\n }\n }\n if ans == std::usize::MAX {\n ans = 0;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0765", "problem_description": "Given $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$. You can perform the following operation on them: select any element $$$a_i$$$ ($$$1 \\le i \\le n$$$) and divide it by $$$2$$$ (round down). In other words, you can replace any selected element $$$a_i$$$ with the value $$$\\left \\lfloor \\frac{a_i}{2}\\right\\rfloor$$$ (where $$$\\left \\lfloor x \\right\\rfloor$$$ is – round down the real number $$$x$$$). Output the minimum number of operations that must be done for a sequence of integers to become strictly increasing (that is, for the condition $$$a_1 \\lt a_2 \\lt \\dots \\lt a_n$$$ to be satisfied). Or determine that it is impossible to obtain such a sequence. Note that elements cannot be swapped. The only possible operation is described above.For example, let $$$n = 3$$$ and a sequence of numbers $$$[3, 6, 5]$$$ be given. Then it is enough to perform two operations on it: Write the number $$$\\left \\lfloor \\frac{6}{2}\\right\\rfloor = 3$$$ instead of the number $$$a_2=6$$$ and get the sequence $$$[3, 3, 5]$$$; Then replace $$$a_1=3$$$ with $$$\\left \\lfloor \\frac{3}{2}\\right\\rfloor = 1$$$ and get the sequence $$$[1, 3, 5]$$$. The resulting sequence is strictly increasing because $$$1 \\lt 3 \\lt 5$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int a[30] = {0};\n int e = 0;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < n - 1; j++) {\n while (a[j] >= a[j + 1] && a[j + 1] != 0) {\n a[j] /= 2;\n e++;\n }\n if (a[j + 1] == 0) {\n for (int k = 0; k < j + 1; k++) {\n a[k] = 0;\n }\n }\n }\n }\n if (a[1] == 0 && n != 1) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d\\n\", e);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n stdin().read_line(&mut t);\n let t = t.trim().parse::().unwrap();\n\n for _ in 0..t {\n let mut buf = String::new();\n stdin().read_line(&mut buf);\n\n let mut v: Vec = Vec::with_capacity(buf.trim().parse::().unwrap());\n buf = String::new();\n stdin().read_line(&mut buf);\n for i in buf.split_whitespace() {\n v.push(i.parse().unwrap());\n }\n\n let mut c = 0;\n let mut solvable = true;\n\n 'qwe: for i in (0..v.len() - 1).rev() {\n loop {\n if v[i + 1] == 0_f64 {\n solvable = false;\n break 'qwe;\n }\n\n if v[i + 1] > v[i] {\n break;\n }\n\n v[i] = f64::floor(v[i] / 2_f64);\n c += 1;\n }\n }\n\n if solvable {\n println!(\"{}\", c);\n } else {\n println!(\"-1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0766", "problem_description": "There are $$$n$$$ participants in a competition, participant $$$i$$$ having a strength of $$$s_i$$$. Every participant wonders how much of an advantage they have over the other best participant. In other words, each participant $$$i$$$ wants to know the difference between $$$s_i$$$ and $$$s_j$$$, where $$$j$$$ is the strongest participant in the competition, not counting $$$i$$$ (a difference can be negative).So, they ask you for your help! For each $$$i$$$ ($$$1 \\leq i \\leq n$$$) output the difference between $$$s_i$$$ and the maximum strength of any participant other than participant $$$i$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int max = 0;\n int smax = 0;\n int a[n];\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] >= max) {\n smax = max;\n max = a[i];\n } else if (a[i] > smax && a[i] != max) {\n smax = a[i];\n }\n }\n for (int i = 0; i < n; i++) {\n\n if (a[i] == max) {\n printf(\"%d \", (max - smax));\n } else if (a[i] < max) {\n printf(\"%d \", (a[i] - max));\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"\");\n let t: i32 = line.trim().parse().expect(\"\");\n let reader = io::stdin();\n for _ in 0..t {\n reader.lock().lines().next();\n let numbers: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|s| s.parse().unwrap())\n .collect();\n let mut max_value: i32 = 0;\n let mut second_max: i32 = 0;\n for c in &numbers {\n if c > &max_value {\n second_max = max_value;\n max_value = *c;\n } else if c > &second_max {\n second_max = *c;\n }\n }\n for c in &numbers {\n if c == &max_value {\n print!(\"{:?} \", c - second_max);\n } else {\n print!(\"{:?} \", c - max_value);\n }\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0767", "problem_description": "You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric:Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.", "c_code": "int solution() {\n char s[1000001];\n char t[1000001];\n int c[26];\n int tmp = 0;\n scanf(\"%s\", s);\n scanf(\"%s\", t);\n int i;\n for (i = 0; s[i] != '\\0'; ++i) {\n if (s[i] != '?') {\n c[s[i] - 'a']++;\n }\n }\n int len = strlen(t);\n for (i = 0; s[i] != '\\0'; ++i) {\n if (s[i] == '?') {\n ++tmp;\n tmp %= len;\n if (c[t[tmp] - 'a'] > 0) {\n c[t[tmp] - 'a']--;\n i--;\n } else {\n s[i] = t[tmp];\n }\n }\n }\n printf(\"%s\\n\", s);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n let mut t = String::new();\n\n use std::io::stdin;\n stdin().read_line(&mut s).unwrap();\n stdin().read_line(&mut t).unwrap();\n\n let mut arr = [0u32; 26];\n\n for ltr in t.trim().chars().filter(|l| l.is_alphabetic()) {\n let a = ltr.to_digit(36).unwrap() as usize;\n let b = 'a'.to_digit(36).unwrap() as usize;\n arr[a - b] += 1;\n }\n\n let mut brr = [0u32; 26];\n\n for ltr in s.trim().chars().filter(|l| l.is_alphabetic()) {\n let a = ltr.to_digit(36).unwrap() as usize;\n let b = 'a'.to_digit(36).unwrap() as usize;\n brr[a - b] += 1;\n }\n\n use std::char::from_digit;\n\n let ans: String = s\n .chars()\n .map(|c| {\n if c == '?' {\n if let Some(i) = (0usize..26)\n .filter(|&i| arr[i] != 0)\n .min_by_key(|&i| brr[i] / arr[i])\n {\n brr[i] += 1;\n from_digit(i as u32 + 'a'.to_digit(36).unwrap(), 36).unwrap()\n } else {\n 'a'\n }\n } else {\n c\n }\n })\n .collect();\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0768", "problem_description": "A PIN code is a string that consists of exactly $$$4$$$ digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0.Polycarp has $$$n$$$ ($$$2 \\le n \\le 10$$$) bank cards, the PIN code of the $$$i$$$-th card is $$$p_i$$$.Polycarp has recently read a recommendation that it is better to set different PIN codes on different cards. Thus he wants to change the minimal number of digits in the PIN codes of his cards so that all $$$n$$$ codes would become different.Formally, in one step, Polycarp picks $$$i$$$-th card ($$$1 \\le i \\le n$$$), then in its PIN code $$$p_i$$$ selects one position (from $$$1$$$ to $$$4$$$), and changes the digit in this position to any other. He needs to change the minimum number of digits so that all PIN codes become different.Polycarp quickly solved this problem. Can you solve it?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n char p[10][6];\n\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", p[i]);\n }\n\n int fin[10] = {0};\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n\n if (fin[i]) {\n continue;\n }\n\n int cnt[10] = {0};\n int ri[10] = {0};\n int rl = 0;\n int tail = 0;\n\n for (int j = 0; j < n; j++) {\n if (p[j][0] == p[i][0] && p[j][1] == p[i][1] && p[j][2] == p[i][2]) {\n ri[rl++] = j;\n fin[j] = 1;\n cnt[p[j][3] - '0']++;\n }\n }\n\n for (int i = 0; i < rl; i++) {\n if (cnt[p[ri[i]][3] - '0'] > 1) {\n cnt[p[ri[i]][3] - '0']--;\n\n while (cnt[tail] > 0) {\n tail++;\n }\n cnt[tail] = 1;\n p[ri[i]][3] = tail + '0';\n ans++;\n tail++;\n }\n }\n }\n\n printf(\"%d\\n\", ans);\n for (int i = 0; i < n; i++) {\n printf(\"%s\\n\", p[i]);\n }\n }\n\n return 0;\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\n let t = get!();\n\n for _ in 0..t {\n let n = get!(usize);\n let mut vs = vec![];\n let mut count = vec![0; 10000];\n for _i in 0..n {\n let pin = get!(usize);\n count[pin] += 1;\n vs.push(pin);\n }\n let mut changes = 0;\n for pin in vs.iter_mut() {\n if count[*pin] == 1 {\n continue;\n }\n changes += 1;\n count[*pin] -= 1;\n let mut w = 1;\n 'outer: for _i in 0..4 {\n let tmp = *pin - (*pin / w % 10) * w;\n for x in 0..9 {\n let new_pin = tmp + x * w;\n if count[new_pin] == 0 {\n *pin = new_pin;\n count[new_pin] += 1;\n break 'outer;\n }\n }\n w *= 10;\n }\n }\n\n println!(\"{}\", changes);\n for pin in vs {\n println!(\"{:04}\", pin);\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0769", "problem_description": "You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).More formally, if you are standing at the point $$$(x, y)$$$, you can: go left, and move to $$$(x - 1, y)$$$, or go right, and move to $$$(x + 1, y)$$$, or go up, and move to $$$(x, y + 1)$$$, or go down, and move to $$$(x, y - 1)$$$. There are $$$n$$$ boxes on this plane. The $$$i$$$-th box has coordinates $$$(x_i,y_i)$$$. It is guaranteed that the boxes are either on the $$$x$$$-axis or the $$$y$$$-axis. That is, either $$$x_i=0$$$ or $$$y_i=0$$$.You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point $$$(0,0)$$$.", "c_code": "int solution() {\n\n int i = 0;\n int j = 0;\n int t = 0;\n int n = 0;\n int x = 0;\n int y = 0;\n int ans = 0;\n int x_right = 0;\n int x_left = 0;\n int y_top = 0;\n int y_bottom = 0;\n\n scanf(\"%d\\n\", &t);\n i = t;\n while (i > 0) {\n scanf(\"%d\\n\", &n);\n\n for (j = 0; j < n; j++) {\n scanf(\"%d %d\\n\", &x, &y);\n if (x > x_right && x != 0) {\n x_right = x;\n }\n if (x < x_left && x != 0) {\n x_left = x;\n }\n if (y > y_top && y != 0) {\n y_top = y;\n }\n if (y < y_bottom && y != 0) {\n y_bottom = y;\n }\n }\n ans = 2 * (x_right - x_left) + 2 * (y_top - y_bottom);\n printf(\"%d\\n\", ans);\n x_right = 0, x_left = 0, y_top = 0, y_bottom = 0;\n i--;\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let t: usize = input.trim().parse().unwrap();\n for _ in 0..t {\n input = String::new();\n stdin().read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n let mut v: Vec<(i32, i32)> = Vec::new();\n for _ in 0..n {\n input = String::new();\n stdin().read_line(&mut input).unwrap();\n let tmp = input\n .trim()\n .split(\" \")\n .map(|x| x.parse::().unwrap())\n .collect::>();\n let [x, y] = [tmp[0], tmp[1]];\n v.push((x, y));\n }\n let mut xp = 0;\n let mut xn = 0;\n let mut yp = 0;\n let mut yn = 0;\n\n for (x, y) in v {\n if x < 0 {\n xn = xn.min(x);\n } else {\n xp = xp.max(x);\n }\n\n if y < 0 {\n yn = yn.min(y);\n } else {\n yp = yp.max(y);\n }\n }\n\n let mut ans = xp;\n let mut prev = xp;\n if yp > 0 {\n ans += prev + yp;\n prev = yp;\n }\n\n if yn < 0 {\n ans += prev + yn.abs();\n prev = yn.abs();\n }\n\n if xn < 0 {\n ans += prev + xn.abs();\n prev = xn.abs();\n }\n\n ans += prev;\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0770", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are going out for a walk, when you suddenly encounter N monsters. Each monster has a parameter called health, and the health of the i-th monster is h_i at the moment of encounter. A monster will vanish immediately when its health drops to 0 or below.

\n

Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:

\n
    \n
  • Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by A, and the health of each of the other monsters will decrease by B. Here, A and B are predetermined parameters, and A > B holds.
  • \n
\n

At least how many explosions do you need to cause in order to vanish all the monsters?

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1 ≤ N ≤ 10^5
  • \n
  • 1 ≤ B < A ≤ 10^9
  • \n
  • 1 ≤ h_i ≤ 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B\nh_1\nh_2\n:\nh_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 5 3\n8\n7\n4\n2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

You can vanish all the monsters in two explosion, as follows:

\n
    \n
  • First, cause an explosion centered at the monster with 8 health. The healths of the four monsters become 3, 4, 1 and -1, respectively, and the last monster vanishes.
  • \n
  • Second, cause an explosion centered at the monster with 4 health remaining. The healths of the three remaining monsters become 0, -1 and -2, respectively, and all the monsters are now vanished.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

2 10 4\n20\n20\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n

You need to cause two explosions centered at each monster, for a total of four.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 2 1\n900000000\n900000000\n1000000000\n1000000000\n1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

800000000\n
\n
\n
", "c_code": "int solution(void) {\n long long n;\n long long a;\n long long b;\n long long max = 0;\n scanf(\"%lld%lld%lld\", &n, &a, &b);\n long long h[n];\n long long p[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &h[i]);\n if (max < h[i]) {\n max = h[i];\n }\n }\n\n long long small = 0;\n long long big = (max + b - 1) / b;\n while (small != big) {\n long long t = small + ((big - small) / 2);\n long long count = 0;\n for (int i = 0; i < n; i++) {\n p[i] = h[i];\n p[i] -= t * b;\n if (p[i] > 0) {\n count += (p[i] + (a - b) - 1) / (a - b);\n }\n }\n if (count > t) {\n small = t + 1;\n } else {\n big = t;\n }\n }\n printf(\"%lld\\n\", big);\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 a: i64 = itr.next().unwrap().parse().unwrap();\n let b: i64 = itr.next().unwrap().parse().unwrap();\n let h: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let c = |x: i64| {\n let mut cnt = 0;\n for i in 0..h.len() {\n cnt += std::cmp::max(0, h[i] - b * x + a - b - 1) / (a - b);\n }\n cnt <= x\n };\n\n let mut ok = 1i64 << 30;\n let mut ng = 0;\n while ok - ng > 1 {\n let mid = (ok + ng) >> 1;\n if c(mid) {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n println!(\"{}\", ok);\n}", "difficulty": "medium"} {"problem_id": "0771", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

\n

As the game progresses, there will be N events where you are asked to make a choice.\nEach of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

\n

After each choice, none of A, B, and C should be negative.

\n

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 0 \\leq A,B,C \\leq 10^9
  • \n
  • N, A, B, C are integers.
  • \n
  • s_i is AB, AC, or BC.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B C\ns_1\ns_2\n:\ns_N\n
\n
\n
\n
\n
\n

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

\n

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 1 3 0\nAB\nAC\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\nA\nC\n
\n

You can successfully make two choices, as follows:

\n
    \n
  • In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.
  • \n
  • In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 1 0 0\nAB\nBC\nAB\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 0 9 0\nAC\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
\n
\n
\n
\n

Sample Input 4

8 6 9 1\nAC\nBC\nAB\nBC\nAC\nBC\nAB\nAB\n
\n
\n
\n
\n
\n

Sample Output 4

Yes\nC\nB\nB\nC\nC\nB\nA\nA\n
\n
\n
", "c_code": "int solution(void) {\n register int i;\n int n;\n scanf(\"%d\", &n);\n long abc[3];\n for (i = 0; i < 3; i++) {\n scanf(\"%ld\", &abc[i]);\n }\n char s[n][3];\n for (i = 0; i < n; i++) {\n scanf(\"%s\", s[i]);\n }\n char ans[n];\n int i0;\n int i1;\n int i0_ = 0;\n int i1_ = 1;\n for (i = 0; i < n; i++) {\n if (!strcmp(\"AB\", s[i])) {\n i0 = 0;\n i1 = 1;\n } else if (!strcmp(\"AC\", s[i])) {\n i0 = 0;\n i1 = 2;\n } else {\n i0 = 1;\n i1 = 2;\n }\n\n if (!abc[i0] && !abc[i1]) {\n puts(\"No\");\n return 0;\n }\n\n if (!abc[i0] || !abc[i1]) {\n if (!abc[i0]) {\n abc[i0]++;\n abc[i1]--;\n ans[i] = 'A' + i0;\n } else {\n abc[i0]--;\n abc[i1]++;\n ans[i] = 'A' + i1;\n }\n continue;\n }\n if (i == n - 1) {\n goto skip;\n }\n if (!strcmp(\"AB\", s[i + 1])) {\n i0_ = 0;\n i1_ = 1;\n } else if (!strcmp(\"AC\", s[i + 1])) {\n i0_ = 0;\n i1_ = 2;\n } else {\n i0_ = 1;\n i1_ = 2;\n }\n skip:\n if (i0 == i0_ || i0 == i1_) {\n abc[i0]++;\n abc[i1]--;\n ans[i] = 'A' + i0;\n } else {\n abc[i0]--;\n abc[i1]++;\n ans[i] = 'A' + i1;\n }\n }\n puts(\"Yes\");\n for (i = 0; i < n; i++) {\n printf(\"%c\\n\", ans[i]);\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n std::io::stdin().read_to_string(&mut line).unwrap();\n let mut it = line.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a: usize = it.next().unwrap().parse().unwrap();\n let mut b: usize = it.next().unwrap().parse().unwrap();\n let mut c: usize = it.next().unwrap().parse().unwrap();\n let s: Vec = it\n .map(|a| match a {\n \"AB\" => 1,\n \"BC\" => 2,\n _ => 3,\n })\n .collect();\n\n a = min(a, 9);\n b = min(b, 9);\n c = min(c, 9);\n let sum = a + b + c;\n let orig_a = a;\n let orig_b = b;\n\n let mut dp = vec![vec![vec![0; sum + 1]; sum + 1]; n + 1];\n dp[0][a][b] = 1;\n for i in 0..n {\n for a in 0..=sum {\n for b in 0..=sum {\n if dp[i][a][b] == 0 {\n continue;\n }\n let c = sum - a - b;\n if s[i] == 1 {\n if a > 0 {\n dp[i + 1][a - 1][b + 1] = 2;\n }\n if b > 0 {\n dp[i + 1][a + 1][b - 1] = 1;\n }\n } else if s[i] == 2 {\n if b > 0 {\n dp[i + 1][a][b - 1] = 3;\n }\n if c > 0 {\n dp[i + 1][a][b + 1] = 2;\n }\n } else {\n if c > 0 {\n dp[i + 1][a + 1][b] = 1;\n }\n if a > 0 {\n dp[i + 1][a - 1][b] = 3;\n }\n }\n }\n }\n }\n\n for a in 0..=sum {\n for b in 0..=sum {\n if dp[n][a][b] != 0 {\n println!(\"Yes\");\n let mut cur_a = a;\n let mut cur_b = b;\n let mut cur_n = n;\n let mut ans = vec![0; n];\n for _i in 0..n {\n ans[cur_n - 1] = dp[cur_n][cur_a][cur_b];\n if s[cur_n - 1] == 1 {\n if dp[cur_n][cur_a][cur_b] == 1 {\n cur_a -= 1;\n cur_b += 1;\n } else {\n cur_a += 1;\n cur_b -= 1;\n }\n } else if s[cur_n - 1] == 2 {\n if dp[cur_n][cur_a][cur_b] == 2 {\n cur_b -= 1;\n } else {\n cur_b += 1;\n }\n } else {\n if dp[cur_n][cur_a][cur_b] == 3 {\n cur_a += 1;\n } else {\n cur_a -= 1;\n }\n }\n cur_n -= 1;\n }\n for a in &ans {\n match a {\n 1 => println!(\"A\"),\n 2 => println!(\"B\"),\n _ => println!(\"C\"),\n }\n }\n assert_eq!(cur_a, orig_a);\n assert_eq!(cur_b, orig_b);\n return;\n }\n }\n }\n println!(\"No\");\n}", "difficulty": "medium"} {"problem_id": "0772", "problem_description": "While trading on his favorite exchange trader William realized that he found a vulnerability. Using this vulnerability he could change the values of certain internal variables to his advantage. To play around he decided to change the values of all internal variables from $$$a_1, a_2, \\ldots, a_n$$$ to $$$-a_1, -a_2, \\ldots, -a_n$$$. For some unknown reason, the number of service variables is always an even number.William understands that with his every action he attracts more and more attention from the exchange's security team, so the number of his actions must not exceed $$$5\\,000$$$ and after every operation no variable can have an absolute value greater than $$$10^{18}$$$. William can perform actions of two types for two chosen variables with indices $$$i$$$ and $$$j$$$, where $$$i < j$$$: Perform assignment $$$a_i = a_i + a_j$$$ Perform assignment $$$a_j = a_j - a_i$$$ William wants you to develop a strategy that will get all the internal variables to the desired values.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int tc = 0; tc < t; tc++) {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n for (int i = 0; i < n / 2; i++) {\n if (i == 0) {\n printf(\"%d\\n\", 3 * n);\n }\n printf(\"1 %d %d\\n\", (2 * (i + 1)) - 1, 2 * (i + 1));\n printf(\"2 %d %d\\n\", (2 * (i + 1)) - 1, 2 * (i + 1));\n printf(\"1 %d %d\\n\", (2 * (i + 1)) - 1, 2 * (i + 1));\n printf(\"1 %d %d\\n\", (2 * (i + 1)) - 1, 2 * (i + 1));\n printf(\"2 %d %d\\n\", (2 * (i + 1)) - 1, 2 * (i + 1));\n printf(\"1 %d %d\\n\", (2 * (i + 1)) - 1, 2 * (i + 1));\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\n let t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let n = it.next().unwrap();\n\n let mut a = Vec::with_capacity(n);\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n a.extend(it);\n\n writeln!(&mut output, \"{}\", 3 * n).unwrap();\n\n for i in 0..n / 2 {\n writeln!(&mut output, \"{} {} {}\", 2, 2 * i + 1, 2 * i + 2).unwrap();\n writeln!(&mut output, \"{} {} {}\", 1, 2 * i + 1, 2 * i + 2).unwrap();\n writeln!(&mut output, \"{} {} {}\", 1, 2 * i + 1, 2 * i + 2).unwrap();\n writeln!(&mut output, \"{} {} {}\", 2, 2 * i + 1, 2 * i + 2).unwrap();\n writeln!(&mut output, \"{} {} {}\", 1, 2 * i + 1, 2 * i + 2).unwrap();\n writeln!(&mut output, \"{} {} {}\", 1, 2 * i + 1, 2 * i + 2).unwrap();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0773", "problem_description": "Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.", "c_code": "int solution() {\n long long int n;\n long long int m;\n long long int q = 0;\n scanf(\"%lld\", &n);\n scanf(\"%lld\", &m);\n long long int x[m];\n long long int i;\n long long int b = 0;\n for (i = 0; i < m; i++) {\n scanf(\"%lld\", &x[i]);\n }\n b += x[q];\n q++;\n if (q >= m) {\n b--;\n printf(\"%lld\", b);\n return 0;\n }\n while (1) {\n if (x[q] > x[q - 1] && q != 0) {\n b += x[q] - x[q - 1];\n }\n if (x[q] < x[q - 1] && q != 0) {\n b += n - x[q - 1];\n b += x[q];\n }\n if (x[q] == x[q + 1] && q + 1 < m) {\n q++;\n }\n q++;\n if (q >= m) {\n break;\n }\n }\n b--;\n printf(\"%lld\", b);\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let n = words[0];\n\n let mut line = String::new();\n\n io::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 last = 1;\n let mut answer = 0;\n\n for house in a {\n answer += (n + house - last) % n;\n last = house;\n }\n\n println!(\"{}\", answer);\n}", "difficulty": "hard"} {"problem_id": "0774", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There is a hotel with the following accommodation fee:

\n
    \n
  • X yen (the currency of Japan) per night, for the first K nights
  • \n
  • Y yen per night, for the (K+1)-th and subsequent nights
  • \n
\n

Tak is staying at this hotel for N consecutive nights.\nFind his total accommodation fee.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N, K \\leq 10000
  • \n
  • 1 \\leq Y < X \\leq 10000
  • \n
  • N,\\,K,\\,X,\\,Y are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nK\nX\nY\n
\n
\n
\n
\n
\n

Output

Print Tak's total accommodation fee.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n3\n10000\n9000\n
\n
\n
\n
\n
\n

Sample Output 1

48000\n
\n

The accommodation fee is as follows:

\n
    \n
  • 10000 yen for the 1-st night
  • \n
  • 10000 yen for the 2-nd night
  • \n
  • 10000 yen for the 3-rd night
  • \n
  • 9000 yen for the 4-th night
  • \n
  • 9000 yen for the 5-th night
  • \n
\n

Thus, the total is 48000 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n3\n10000\n9000\n
\n
\n
\n
\n
\n

Sample Output 2

20000\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int K = 0;\n int X = 0;\n int Y = 0;\n int A = 0;\n\n scanf(\"%d %d %d %d\", &N, &K, &X, &Y);\n\n if (N > K) {\n A = K * X + (N - K) * Y;\n } else {\n A = N * X;\n }\n\n printf(\"%d\\n\", A);\n}", "rust_code": "fn solution() {\n let mut n_str = String::new();\n let mut k_str = String::new();\n let mut x_str = String::new();\n let mut y_str = String::new();\n\n io::stdin().read_line(&mut n_str).expect(\"\");\n io::stdin().read_line(&mut k_str).expect(\"\");\n io::stdin().read_line(&mut x_str).expect(\"\");\n io::stdin().read_line(&mut y_str).expect(\"\");\n\n let n: u32 = n_str.trim().parse().expect(\"\");\n let k: u32 = k_str.trim().parse().expect(\"\");\n let x: u32 = x_str.trim().parse().expect(\"\");\n let y: u32 = y_str.trim().parse().expect(\"\");\n\n let to_k = x * if n > k { k } else { n };\n let from_k = y * n.saturating_sub(k);\n\n println!(\"{}\", to_k + from_k);\n}", "difficulty": "easy"} {"problem_id": "0775", "problem_description": "Polycarp has $$$x$$$ of red and $$$y$$$ of blue candies. Using them, he wants to make gift sets. Each gift set contains either $$$a$$$ red candies and $$$b$$$ blue candies, or $$$a$$$ blue candies and $$$b$$$ red candies. Any candy can belong to at most one gift set.Help Polycarp to find the largest number of gift sets he can create.For example, if $$$x = 10$$$, $$$y = 12$$$, $$$a = 5$$$, and $$$b = 2$$$, then Polycarp can make three gift sets: In the first set there will be $$$5$$$ red candies and $$$2$$$ blue candies; In the second set there will be $$$5$$$ blue candies and $$$2$$$ red candies; In the third set will be $$$5$$$ blue candies and $$$2$$$ red candies. Note that in this example there is one red candy that Polycarp does not use in any gift set.", "c_code": "int solution() {\n int t;\n\n scanf(\"%d\", &t);\n while (t--) {\n int x;\n int y;\n int a;\n int b;\n int lower;\n int upper;\n int tmp;\n\n scanf(\"%d%d%d%d\", &x, &y, &a, &b);\n if (a > b) {\n tmp = a, a = b, b = tmp;\n }\n lower = 0, upper = 0x3f3f3f3f;\n while (upper - lower > 1) {\n int k = (lower + upper) / 2;\n long long x_ = x - (long long)a * k;\n long long y_ = y - (long long)a * k;\n\n if (x_ < 0 || y_ < 0) {\n upper = k;\n continue;\n }\n if (a == b) {\n lower = k;\n continue;\n }\n if (x_ / (b - a) + y_ / (b - a) >= k) {\n lower = k;\n } else {\n upper = k;\n }\n }\n printf(\"%d\\n\", lower);\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 log = case_index == 5;\n\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let x = it.next().unwrap();\n let y = it.next().unwrap();\n let a = it.next().unwrap();\n let b = it.next().unwrap();\n\n let ans = if a == b {\n std::cmp::min(x, y) / a\n } else {\n let (a, b) = if a < b { (a, b) } else { (b, a) };\n let mut l = 0;\n let mut r = std::cmp::min(x, y) + 1;\n\n while l < r {\n let n = (l + r) / 2;\n\n let xb = (n * b - x - 1).div_euclid(b - a) + 1;\n let lb = std::cmp::max(0, xb);\n\n let yb = (y - n * a).div_euclid(b - a);\n let ub = std::cmp::min(n, yb);\n\n if lb <= ub {\n l = n + 1;\n } else {\n r = n;\n }\n }\n\n l - 1\n };\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0776", "problem_description": "

未熟者の数式

\n\n\n

博士 : ピーター君、ついにやったよ。

\n

ピーター : またですか。今度はどんなくだらない発明ですか。

\n

博士 : ついに数式を計算機で処理する画期的な方法を思いついたんだ。この表をみてごらん。\n

\n\n
\n\n\n\n\n\n\n
通常の記法博士の「画期的な」記法
1 + 21 2 +
3 * 4 + 73 4 * 7 +
10 / ( 2 - 12 )\t10 2 12 - /
( 3 - 4 ) * ( 7 + 2 * 3 )3 4 - 7 2 3 * + *
\n
\n
\n\n

ピーター : はぁ。

\n

博士 : ふっふっふ。これだけでは、未熟者の君には何のことだかわからないだろうねえ。ここからが肝心なんじゃ。

\n

ピーター : っていうか・・・。

\n

博士 : 計算機にはスタックというデータ構造があることは君も知っているね。ほれ、「先入れ後出し」のあれじゃよ。

\n

ピーター : はい。知ってますが、あの・・・。

\n

博士 : この画期的な記法はあのスタックを使うんじゃ。例えばこの 10 2 12 - / だが、次のように処理する。\n

\n\n\n
\n\n\n\n\n\n\n\n\n\n\n
処理対象10212-/
↓2-12↓10/-10
スタック\n\n \n\n\n\n\n
.
.
10
\n\n
\n\n \n\n\n\n
.
2
10
\n\n
\n\n \n\n\n\n
12
2
10
\n\n\n
\n\n \n\n\n\n
.
-10
10
\n\n\n\n
\n\n \n\n\n\n
.
.
-1
\n\n
\n
\n\n
\n\n\n

博士 : どうじゃな。括弧も演算子の優先順位も気にする必要がないじゃろう?語順も「10 を 2 から 12 を引いたもので割る。」となり、何となく彼の極東の島国の言葉、日本語と似ておるじゃろうて。\n この画期的な発明さえあれば、我が研究室は安泰じゃて。ファファファ。

\n

ピーター : っていうか博士。これって日本にいたとき会津大学の基礎コースで習いましたよ。「逆ポーランド記法」とかいって、みんな簡単にプログラムしてました。

\n

博士 : ・・・。

\n\n\n

\nということで、ピーター君に変わって博士に、このプログラムを教える事になりました。「逆ポーランド記法」で書かれた数式を入力とし、計算結果を出力するプログラムを作成してください。\n

\n\n\n

入力

\n\n

複数のデータセットが与えられます。各データセットでは、逆ポーランド記法による数式(整数と演算記号が空白文字1文字(半角)で区切られた80文字以内の文字列)が 1 行に与えられます。\nある値を 0 や 0 に限りなく近い値で割るような数式は与えられません。\n

\n\n

\nデータセットの数は 50 を超えません。\n

\n\n\n\n

出力

\n\n

各データセットごとに、計算結果(実数)を1行に出力してください。なお、計算結果は 0.00001 以下の誤差を含んでもよい。\n

\n\n

Sample Input

\n\n
\n10 2 12 - /\n3 4 - 7 2 3 * + *\n-1 -2 3 + +\n
\n\n\n

Output for the Sample Input

\n\n
\n-1.000000\n-13.000000\n0.000000\n
", "c_code": "int solution(void) {\n double stack[200];\n char temp[100];\n int stackptr = 0;\n while (1) {\n if (scanf(\"%s\", temp) == -1) {\n break;\n }\n if (strcmp(temp, \"+\") == 0) {\n if (stackptr < 2) {\n return 1;\n }\n stack[stackptr - 2] += stack[stackptr - 1];\n stackptr--;\n } else if (strcmp(temp, \"-\") == 0) {\n if (stackptr < 2) {\n return 1;\n }\n stack[stackptr - 2] -= stack[stackptr - 1];\n stackptr--;\n } else if (strcmp(temp, \"*\") == 0) {\n if (stackptr < 2) {\n return 1;\n }\n stack[stackptr - 2] *= stack[stackptr - 1];\n stackptr--;\n } else if (strcmp(temp, \"/\") == 0) {\n if (stackptr < 2) {\n return 1;\n }\n if (stack[stackptr - 1] == 0) {\n return 1;\n }\n stack[stackptr - 2] /= stack[stackptr - 1];\n stackptr--;\n } else {\n stack[stackptr] = atof(temp);\n stackptr++;\n }\n if (getchar() == '\\n') {\n if (stackptr != 1) {\n return 1;\n }\n printf(\"%f\\n\", stack[0]);\n stackptr = 0;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n\n for l in stdin.lock().lines() {\n let mut stack: Vec = vec![];\n for token in l.unwrap().split_whitespace() {\n match token.parse::() {\n Ok(x) => stack.push(x),\n _ => {\n let y = stack.pop().unwrap();\n let x = stack.pop().unwrap();\n stack.push(match token {\n \"+\" => x + y,\n \"-\" => x - y,\n \"*\" => x * y,\n _ => x / y,\n })\n }\n }\n }\n println!(\"{}\", stack[0]);\n }\n}", "difficulty": "medium"} {"problem_id": "0777", "problem_description": "In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.", "c_code": "int solution(void) {\n int n;\n int sum = 0;\n int maxlvl = 0;\n int parent;\n\n scanf(\"%d\\n\", &n);\n\n int p[n];\n int lvl[n];\n int c[n];\n lvl[0] = 0;\n for (int i = 1; i < n; i++) {\n scanf(\"%d\", &parent);\n p[i] = parent - 1;\n c[i] = 0;\n lvl[i] = lvl[p[i]] + 1;\n if (lvl[i] > maxlvl) {\n maxlvl = lvl[i];\n }\n }\n int ranges[maxlvl + 1];\n for (int i = 0; i <= maxlvl; i++) {\n ranges[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n ranges[lvl[i]] += 1;\n }\n for (int i = 0; i <= maxlvl; i++) {\n sum += ranges[i] % 2;\n }\n\n printf(\"%d\\n\", sum);\n\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n use std::io::prelude::*;\n\n let mut input = String::new();\n\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n\n let cll = it.map(|x| x.parse::().unwrap()).enumerate().fold(\n vec![Vec::new(); n],\n |mut acc, (i, x)| {\n acc[x - 1].push(i + 1);\n acc\n },\n );\n\n use std::collections::VecDeque;\n\n let mut queue = VecDeque::new();\n queue.push_back(0);\n\n let mut dl = vec![0; n];\n\n while let Some(cur) = queue.pop_front() {\n for &c in &cll[cur] {\n dl[c] = dl[cur] + 1;\n queue.push_back(c);\n }\n }\n\n let dcl = dl.into_iter().fold(vec![0; n], |mut acc, x| {\n acc[x] += 1;\n acc\n });\n\n let ans = dcl.into_iter().map(|x| x % 2).sum::();\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0778", "problem_description": "The city of Fishtopia can be imagined as a grid of $$$4$$$ rows and an odd number of columns. It has two main villages; the first is located at the top-left cell $$$(1,1)$$$, people who stay there love fishing at the Tuna pond at the bottom-right cell $$$(4, n)$$$. The second village is located at $$$(4, 1)$$$ and its people love the Salmon pond at $$$(1, n)$$$.The mayor of Fishtopia wants to place $$$k$$$ hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.A person can move from one cell to another if those cells are not occupied by hotels and share a side.Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?", "c_code": "int solution() {\n int n;\n int k;\n int count = 0;\n int i;\n int j;\n int a = 0;\n int b = 0;\n int temp = 0;\n scanf(\"%d %d\", &n, &k);\n\n count = k;\n if (k % 2 == 0) {\n a = k / 2;\n b = k / 2;\n printf(\"YES\\n\");\n for (i = 0; i < 4; i++) {\n for (j = 0; j < n; j++) {\n if ((a != 0) && (i == 1) && (j > 0) && (j < n - 1)) {\n printf(\"#\");\n a--;\n\n } else if ((b != 0) && (i == 2) && (j > 0) && (j < n - 1)) {\n printf(\"#\");\n b--;\n } else {\n printf(\".\");\n }\n }\n printf(\"\\n\");\n }\n return 0;\n }\n if (k % 2 != 0) {\n if (k == (n - 2)) {\n count = k;\n printf(\"YES\\n\");\n for (i = 0; i < 4; i++) {\n for (j = 0; j < n; j++) {\n if ((count != 0) && (i == 1) && (j > 0) && (j < n - 1)) {\n printf(\"#\");\n count--;\n\n } else {\n printf(\".\");\n }\n }\n printf(\"\\n\");\n }\n return 0;\n }\n\n if (k <= (n - 2)) {\n if (k != 3) {\n temp = 1;\n a = (k + 1) / 2;\n b = k / 2;\n printf(\"YES\\n\");\n for (i = 0; i < 4; i++) {\n for (j = 0; j < n; j++) {\n if ((a != 0) && (i == 1) && (j >= (n - 1) / 2) && (j < n - 1)) {\n printf(\"#\");\n a--;\n\n }\n\n else if ((b != 0) && (i == 2) && (j >= (n - 1) / 2) &&\n (j < n - 1)) {\n if ((b == 1) && (temp == 1)) {\n printf(\".\");\n temp = 2;\n b++;\n } else {\n printf(\"#\");\n }\n b--;\n\n } else {\n printf(\".\");\n }\n }\n printf(\"\\n\");\n }\n }\n if (k == 3) {\n count = k;\n printf(\"YES\\n\");\n for (i = 0; i < 4; i++) {\n for (j = 0; j < n; j++) {\n if ((count != 0) && (i == 1) && (j >= (n - 2) / 2) && (j < n - 1)) {\n printf(\"#\");\n count--;\n\n }\n\n else {\n printf(\".\");\n }\n }\n printf(\"\\n\");\n }\n }\n return 0;\n }\n if (k > (n - 2)) {\n a = (k + 1) / 2;\n b = k / 2;\n printf(\"YES\\n\");\n for (i = 0; i < 4; i++) {\n for (j = 0; j < n; j++) {\n if ((a != 0) && (i == 1) && (j > 0) && (j < (n - 1))) {\n printf(\"#\");\n a--;\n\n }\n\n else if ((b >= 0) && (i == 2) && (j > 0) && (j < n - 1)) {\n if (b == 1) {\n printf(\".\");\n } else {\n printf(\"#\");\n }\n b--;\n\n } else {\n printf(\".\");\n }\n }\n printf(\"\\n\");\n }\n return 0;\n }\n }\n printf(\"NO\");\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n use std::io::prelude::*;\n\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n let k: usize = it.next().unwrap().parse().unwrap();\n\n let k_even = |k| {\n println!(\"YES\");\n let mut grid = String::new();\n for y in 0..4 {\n for x in 0..n {\n if 0 < y && y < 3 && 0 < x && x <= k / 2 {\n grid.push('#');\n } else {\n grid.push('.');\n }\n }\n grid.push('\\n');\n }\n print!(\"{}\", grid);\n };\n\n let k_odd = |k| {\n if k < n - 2 {\n let mut grid = String::new();\n\n for _ in 0..n {\n grid.push('.');\n }\n grid.push('\\n');\n\n let t = n - 2 - k;\n let t0 = t / 2;\n grid.push('.');\n for _ in 0..t0 {\n grid.push('.');\n }\n for _ in t0..n - 2 - t0 {\n grid.push('#');\n }\n for _ in 0..t0 {\n grid.push('.');\n }\n grid.push('.');\n grid.push('\\n');\n\n for _ in 0..n {\n grid.push('.');\n }\n grid.push('\\n');\n\n for _ in 0..n {\n grid.push('.');\n }\n grid.push('\\n');\n print!(\"YES\\n{}\", grid);\n std::process::exit(0);\n }\n\n let mut grid = String::new();\n\n for _ in 0..n {\n grid.push('.');\n }\n grid.push('\\n');\n\n grid.push('.');\n for _ in 0..n - 2 {\n grid.push('#');\n }\n grid.push('.');\n grid.push('\\n');\n\n grid.push('.');\n let t = k + 2 - n;\n let t0 = t / 2;\n for _ in 0..t0 {\n grid.push('#');\n }\n for _ in t0..n - 2 - t0 {\n grid.push('.');\n }\n for _ in 0..t0 {\n grid.push('#');\n }\n grid.push('.');\n grid.push('\\n');\n\n for _ in 0..n {\n grid.push('.');\n }\n grid.push('\\n');\n\n print!(\"YES\\n{}\", grid);\n };\n\n match k % 2 {\n 0 => k_even(k),\n 1 => k_odd(k),\n _ => unreachable!(),\n }\n}", "difficulty": "easy"} {"problem_id": "0779", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given is a string S representing the day of the week today.

\n

S is SUN, MON, TUE, WED, THU, FRI, or SAT, for Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, respectively.

\n

After how many days is the next Sunday (tomorrow or later)?

\n
\n
\n
\n
\n

Constraints

    \n
  • S is SUN, MON, TUE, WED, THU, FRI, or SAT.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the number of days before the next Sunday.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

SAT\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

It is Saturday today, and tomorrow will be Sunday.

\n
\n
\n
\n
\n
\n

Sample Input 2

SUN\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n

It is Sunday today, and seven days later, it will be Sunday again.

\n
\n
", "c_code": "int solution(void) {\n\n char s[3];\n scanf(\"%s\", s);\n\n if (s[0] == 'S' && s[1] == 'A' && s[2] == 'T') {\n printf(\"1\\n\");\n }\n if (s[0] == 'F' && s[1] == 'R' && s[2] == 'I') {\n printf(\"2\\n\");\n }\n if (s[0] == 'T' && s[1] == 'H' && s[2] == 'U') {\n printf(\"3\\n\");\n }\n if (s[0] == 'W' && s[1] == 'E' && s[2] == 'D') {\n printf(\"4\\n\");\n }\n if (s[0] == 'T' && s[1] == 'U' && s[2] == 'E') {\n printf(\"5\\n\");\n }\n if (s[0] == 'M' && s[1] == 'O' && s[2] == 'N') {\n printf(\"6\\n\");\n }\n if (s[0] == 'S' && s[1] == 'U' && s[2] == 'N') {\n printf(\"7\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n let days = [\"SUN\", \"MON\", \"TUE\", \"WED\", \"THU\", \"FRI\", \"SAT\"];\n std::io::stdin().read_line(&mut s).ok();\n s = s.trim().to_string();\n for (i, d) in days.iter().enumerate() {\n if s == d.to_string() {\n println!(\"{}\", 7 - i);\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0780", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

\n

The vacation consists of N days.\nFor each i (1 \\leq i \\leq N), Taro will choose one of the following activities and do it on the i-th day:

\n
    \n
  • A: Swim in the sea. Gain a_i points of happiness.
  • \n
  • B: Catch bugs in the mountains. Gain b_i points of happiness.
  • \n
  • C: Do homework at home. Gain c_i points of happiness.
  • \n
\n

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

\n

Find the maximum possible total points of happiness that Taro gains.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq a_i, b_i, c_i \\leq 10^4
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 b_1 c_1\na_2 b_2 c_2\n:\na_N b_N c_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible total points of happiness that Taro gains.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n10 40 70\n20 50 80\n30 60 90\n
\n
\n
\n
\n
\n

Sample Output 1

210\n
\n

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n100 10 1\n
\n
\n
\n
\n
\n

Sample Output 2

100\n
\n
\n
\n
\n
\n
\n

Sample Input 3

7\n6 7 8\n8 8 3\n2 5 2\n7 8 6\n4 6 8\n2 3 4\n7 5 1\n
\n
\n
\n
\n
\n

Sample Output 3

46\n
\n

Taro should do activities in the order C, A, B, A, C, B, A.

\n
\n
", "c_code": "int solution(void) {\n int n;\n int i = 0;\n int ans = 0;\n int table[100000][3];\n int value[100000][3];\n\n scanf(\"%d\", &n);\n while (i < n) {\n scanf(\"%d%d%d\", &table[i][0], &table[i][1], &table[i][2]);\n i++;\n }\n value[0][0] = table[0][0];\n value[0][1] = table[0][1];\n value[0][2] = table[0][2];\n i = 1;\n while (i < n) {\n if (value[i - 1][1] < value[i - 1][2]) {\n value[i][0] = value[i - 1][2] + table[i][0];\n } else {\n value[i][0] = value[i - 1][1] + table[i][0];\n }\n if (value[i - 1][0] < value[i - 1][2]) {\n value[i][1] = value[i - 1][2] + table[i][1];\n } else {\n value[i][1] = value[i - 1][0] + table[i][1];\n }\n if (value[i - 1][0] < value[i - 1][1]) {\n value[i][2] = value[i - 1][1] + table[i][2];\n } else {\n value[i][2] = value[i - 1][0] + table[i][2];\n }\n i++;\n }\n i = 0;\n while (i < 3) {\n if (ans < value[n - 1][i]) {\n ans = value[n - 1][i];\n }\n i++;\n }\n\n printf(\"%d\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n let costs: Vec> = (0..n)\n .map(|_| {\n input.clear();\n io::stdin().read_line(&mut input).unwrap();\n input\n .split_whitespace()\n .map(|e| e.parse().unwrap())\n .collect()\n })\n .collect();\n\n let mut tmp = vec![[0; 3]; n];\n tmp[0][0] = costs[0][0];\n tmp[0][1] = costs[0][1];\n tmp[0][2] = costs[0][2];\n for i in 1..n {\n tmp[i][0] = costs[i][0] + cmp::max(tmp[i - 1][1], tmp[i - 1][2]);\n tmp[i][1] = costs[i][1] + cmp::max(tmp[i - 1][2], tmp[i - 1][0]);\n tmp[i][2] = costs[i][2] + cmp::max(tmp[i - 1][0], tmp[i - 1][1]);\n }\n println!(\"{}\", tmp[n - 1].into_iter().max().unwrap());\n}", "difficulty": "medium"} {"problem_id": "0781", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

\n

Given is a positive integer N.
\nFind the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

\n
    \n
  • When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.
  • \n
\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

\n

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

25\n
\n
\n
\n
\n
\n

Sample Output 1

17\n
\n

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100\n
\n
\n
\n
\n
\n

Sample Output 3

108\n
\n
\n
\n
\n
\n
\n

Sample Input 4

2020\n
\n
\n
\n
\n
\n

Sample Output 4

40812\n
\n
\n
\n
\n
\n
\n

Sample Input 5

200000\n
\n
\n
\n
\n
\n

Sample Output 5

400000008\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[9][10] = {0};\n int ans = 0;\n for (int i = 1; i <= n; i++) {\n int b = i;\n while (b % 10 != b) {\n b /= 10;\n }\n a[b - 1][i % 10]++;\n }\n for (int i = 0; i < 9; i++) {\n for (int j = 0; j < 9; j++) {\n ans += a[i][j + 1] * a[j][i + 1];\n }\n }\n printf(\"%d\\n\", ans);\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 n: usize = s.trim().parse().unwrap();\n let mut mp = std::collections::HashMap::<(usize, usize), u64>::new();\n let mut ans = 0u64;\n for i in 1..n + 1 {\n let k2 = i % 10;\n let mut k1 = i;\n while k1 >= 10 {\n k1 /= 10;\n }\n let cnt = mp.entry((k1, k2)).or_insert(0);\n *cnt += 1;\n }\n for i in 1..10 {\n for j in 1..10 {\n if let (Some(val1), Some(val2)) = (mp.get(&(i, j)), mp.get(&(j, i))) {\n ans += val1 * val2\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0782", "problem_description": "There are $$$n$$$ bags with candies, initially the $$$i$$$-th bag contains $$$i$$$ candies. You want all the bags to contain an equal amount of candies in the end. To achieve this, you will: Choose $$$m$$$ such that $$$1 \\le m \\le 1000$$$Perform $$$m$$$ operations. In the $$$j$$$-th operation, you will pick one bag and add $$$j$$$ candies to all bags apart from the chosen one.Your goal is to find a valid sequence of operations after which all the bags will contain an equal amount of candies. It can be proved that for the given constraints such a sequence always exists.You don't have to minimize $$$m$$$.If there are several valid sequences, you can output any.", "c_code": "int solution() {\n int t = 0;\n int i = 0;\n int n = 0;\n int i2 = 0;\n scanf(\"%d\", &t);\n for (i = 0; i < t; i++) {\n scanf(\"%d\", &n);\n printf(\"%d\\n\", n - 1);\n for (i2 = 2; i2 <= n; i2++) {\n if (i2 == 2) {\n printf(\"%d\", i2);\n } else {\n printf(\" %d\", i2);\n }\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n\n let mut line = String::new();\n reader.read_line(&mut line).expect(\"Missing t\");\n let t: i32 = line.trim().parse().expect(\"t is not a number\");\n\n for _n in 0..t {\n let mut test_case_line = String::new();\n reader\n .read_line(&mut test_case_line)\n .expect(\"Missing number\");\n let n: i32 = test_case_line.trim().parse().expect(\"n_i in not a number\");\n\n println!(\"{}\", n);\n for i in 0..n {\n print!(\"{} \", i + 1);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0783", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

On a two-dimensional plane, there are m lines drawn parallel to the x axis, and n lines drawn parallel to the y axis.\nAmong the lines parallel to the x axis, the i-th from the bottom is represented by y = y_i.\nSimilarly, among the lines parallel to the y axis, the i-th from the left is represented by x = x_i.

\n

For every rectangle that is formed by these lines, find its area, and print the total area modulo 10^9+7.

\n

That is, for every quadruple (i,j,k,l) satisfying 1\\leq i < j\\leq n and 1\\leq k < l\\leq m, find the area of the rectangle formed by the lines x=x_i, x=x_j, y=y_k and y=y_l, and print the sum of these areas modulo 10^9+7.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq n,m \\leq 10^5
  • \n
  • -10^9 \\leq x_1 < ... < x_n \\leq 10^9
  • \n
  • -10^9 \\leq y_1 < ... < y_m \\leq 10^9
  • \n
  • x_i and y_i are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n m\nx_1 x_2 ... x_n\ny_1 y_2 ... y_m\n
\n
\n
\n
\n
\n

Output

Print the total area of the rectangles, modulo 10^9+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n1 3 4\n1 3 6\n
\n
\n
\n
\n
\n

Sample Output 1

60\n
\n

The following figure illustrates this input:

\n

\"sample1-1\"

\n

The total area of the nine rectangles A, B, ..., I shown in the following figure, is 60.

\n

\"sample1-2\"

\n
\n
\n
\n
\n
\n

Sample Input 2

6 5\n-790013317 -192321079 95834122 418379342 586260100 802780784\n-253230108 193944314 363756450 712662868 735867677\n
\n
\n
\n
\n
\n

Sample Output 2

835067060\n
\n
\n
", "c_code": "int solution(void) {\n int numx;\n int numy;\n scanf(\"%d %d\", &numx, &numy);\n int zahyou_x[numx];\n int zahyou_y[numy];\n for (int i = 0; i < numx; i++) {\n scanf(\"%d\", &zahyou_x[i]);\n }\n for (int i = 0; i < numy; i++) {\n scanf(\"%d\", &zahyou_y[i]);\n }\n long long int x_sum = 0;\n long long int y_sum = 0;\n for (int i = 0; i < numx; i++) {\n x_sum = (x_sum + (long)zahyou_x[i] * (i * 2 - numx + 1)) % 1000000007;\n }\n for (int i = 0; i < numy; i++) {\n y_sum = (y_sum + (long)zahyou_y[i] * (i * 2 - numy + 1)) % 1000000007;\n }\n printf(\"%lld\\n\", (x_sum * y_sum) % 1000000007);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N: usize,\n M: usize,\n X: [i64; N],\n Y: [i64; M],\n }\n let mut x = 0;\n let mut y = 0;\n let modulo = 1_000_000_007;\n for i in 0..N {\n x += X[i] * i as i64 - X[i] * (N - i - 1) as i64;\n }\n for i in 0..M {\n y += Y[i] * i as i64 - Y[i] * (M - i - 1) as i64;\n }\n x %= modulo;\n y %= modulo;\n println!(\"{}\", x * y % modulo);\n}", "difficulty": "easy"} {"problem_id": "0784", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

La Confiserie d'ABC sells cakes at 4 dollars each and doughnuts at 7 dollars each.\nDetermine if there is a way to buy some of them for exactly N dollars. You can buy two or more doughnuts and two or more cakes, and you can also choose to buy zero doughnuts or zero cakes.

\n
\n
\n
\n
\n

Constraints

    \n
  • N is an integer between 1 and 100, inclusive.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If there is a way to buy some cakes and some doughnuts for exactly N dollars, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

11\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

If you buy one cake and one doughnut, the total will be 4 + 7 = 11 dollars.

\n
\n
\n
\n
\n
\n

Sample Input 2

40\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n

If you buy ten cakes, the total will be 4 \\times 10 = 40 dollars.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n

The prices of cakes (4 dollars) and doughnuts (7 dollars) are both higher than 3 dollars, so there is no such way.

\n
\n
", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n for (int i = 0; i <= n / 4; i++) {\n int buf = n - (i * 4);\n if (buf % 7 == 0) {\n printf(\"Yes\\n\");\n return 0;\n }\n }\n printf(\"No\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut buf = String::new();\n stdin.read_line(&mut buf).unwrap();\n\n let N: i32 = buf.trim_end().parse().unwrap();\n\n let mut ok = false;\n 'a: for x in 0..100 {\n for y in 0..100 {\n if N == 7 * x + 4 * y {\n ok = true;\n break 'a;\n }\n }\n }\n\n if ok {\n println!(\"Yes\")\n } else {\n println!(\"No\")\n }\n}", "difficulty": "easy"} {"problem_id": "0785", "problem_description": "Lord Omkar would like to have a tree with $$$n$$$ nodes ($$$3 \\le n \\le 10^5$$$) and has asked his disciples to construct the tree. However, Lord Omkar has created $$$m$$$ ($$$\\mathbf{1 \\le m < n}$$$) restrictions to ensure that the tree will be as heavenly as possible. A tree with $$$n$$$ nodes is an connected undirected graph with $$$n$$$ nodes and $$$n-1$$$ edges. Note that for any two nodes, there is exactly one simple path between them, where a simple path is a path between two nodes that does not contain any node more than once.Here is an example of a tree: A restriction consists of $$$3$$$ pairwise distinct integers, $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \\le a,b,c \\le n$$$). It signifies that node $$$b$$$ cannot lie on the simple path between node $$$a$$$ and node $$$c$$$. Can you help Lord Omkar and become his most trusted disciple? You will need to find heavenly trees for multiple sets of restrictions. It can be shown that a heavenly tree will always exist for any set of restrictions under the given constraints.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int m;\n int arr[100001] = {0};\n scanf(\"%d%d\", &n, &m);\n int a[m];\n int b[m];\n int c[m];\n for (int i = 0; i < m; i++) {\n scanf(\"%d %d %d\", &a[i], &b[i], &c[i]);\n arr[b[i]] = 1;\n }\n int index = 0;\n for (int i = 1; i <= n; i++) {\n if (arr[i] == 0) {\n index = i;\n break;\n }\n }\n for (int i = 1; i <= n; i++) {\n if (i != index) {\n printf(\"%d %d\\n\", i, index);\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut s = String::new();\n io::stdin().read_to_string(&mut s)?;\n let mut it = s.split_whitespace().map(|s| s.to_string());\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut out = String::new();\n for _case in 1..=t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let m: usize = it.next().unwrap().parse().unwrap();\n let mut free = vec![true; n];\n for _ in 0..m {\n let _a: usize = it.next().unwrap().parse().unwrap();\n let b: usize = it.next().unwrap().parse().unwrap();\n let _c: usize = it.next().unwrap().parse().unwrap();\n free[b - 1] = false;\n }\n for i in 0..n {\n if free[i] {\n for j in 0..n {\n if j != i {\n let ans = format!(\"{} {}\\n\", i + 1, j + 1);\n out.push_str(ans.as_str());\n }\n }\n break;\n }\n }\n }\n print!(\"{}\", out);\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0786", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There is an empty array.\nThe following N operations will be performed to insert integers into the array.\nIn the i-th operation (1≤i≤N), b_i copies of an integer a_i are inserted into the array.\nFind the K-th smallest integer in the array after the N operations.\nFor example, the 4-th smallest integer in the array \\{1,2,2,3,3,3\\} is 3.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤10^5
  • \n
  • 1≤a_i,b_i≤10^5
  • \n
  • 1≤K≤b_1…+…b_n
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\na_1 b_1\n:  \na_N b_N\n
\n
\n
\n
\n
\n

Output

Print the K-th smallest integer in the array after the N operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4\n1 1\n2 2\n3 3\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The resulting array is the same as the one in the problem statement.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 500000\n1 100000\n1 100000\n1 100000\n1 100000\n1 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
", "c_code": "int solution() {\n long long N;\n long long K;\n long long sum = 0;\n long long ans = -1;\n scanf(\"%lld%lld\", &N, &K);\n long long a[N];\n long long b[N];\n for (long long i = 0; i < N; i++) {\n scanf(\"%lld%lld\", &a[i], &b[i]);\n }\n\n long long h = N;\n long long swapped = 0;\n while (h > 1 || swapped == 1) {\n if (h > 1) {\n h = h * 10 / 13;\n }\n swapped = 0;\n for (long long i = 0; i < N - h; i++) {\n if (a[i] > a[i + h]) {\n long long tmp = a[i];\n a[i] = a[i + h];\n a[i + h] = tmp;\n tmp = b[i];\n b[i] = b[i + h];\n b[i + h] = tmp;\n swapped = 1;\n }\n }\n }\n\n for (long long i = 0; i < N; i++) {\n sum += b[i];\n if (sum >= K && ans == -1) {\n ans = i;\n }\n }\n printf(\"%lld\\n\", a[ans]);\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 v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let mut h: HashMap = HashMap::new();\n for _ in 0..v[0] {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let u: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let x = match h.get(&u[0]) {\n Some(x) => *x,\n None => 0,\n };\n h.insert(u[0], x + u[1]);\n }\n let mut k = h.keys().collect::>();\n k.sort();\n let mut x = 0;\n for i in &k {\n x += *h.get(*i).unwrap();\n if x >= v[1] {\n println!(\"{}\", *i);\n return;\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0787", "problem_description": "

お財布メタボ診断

\n\n

\n4月に消費税が8%になってから、お財布が硬貨でパンパンになっていませんか。同じ金額を持ち歩くなら硬貨の枚数を少なくしたいですよね。硬貨の合計が1000円以上なら、硬貨をお札に両替して、お財布のメタボ状態を解消できます。\n

\n

\nお財布の中の硬貨の枚数が種類ごとに与えられたとき、硬貨をお札に両替できるかどうかを判定するプログラムを作成してください。\n

\n\n\n

入力

\n

\n入力は以下の形式で与えられる。\n

\n
\nc1 c5 c10 c50 c100 c500\n
\n

\n入力は1行からなる。c1c5c10c50c100c500 (0 ≤ c1, c5, c10, c50, c100, c500 ≤ 50) は、それぞれ、1円、5円、10円、50円、100円、500円硬貨の枚数を表す。\n

\n\n

出力

\n

\n硬貨をお札に両替できるなら1 を、そうでなければ 0 を1行に出力する。\n

\n\n

入出力例

\n
\n\n

入力例1

\n
\n3 1 4 0 4 1 \n
\n

出力例1

\n
\n0\n
\n
\n

入力例2

\n
\n2 1 4 3 2 3\n
\n

出力例2

\n
\n1\n
\n
\n

入力例3

\n
\n21 5 9 3 1 1 \n
\n

出力例3

\n
\n0\n
\n
\n\n

入力例4

\n
\n2 4 3 3 3 1 \n
\n

出力例4

\n
\n1\n
\n
\n

入力例5

\n
\n50 50 50 4 0 0 \n
\n

出力例5

\n
\n1\n
", "c_code": "int solution(void) {\n int c[6];\n int sum = 0;\n int mon[6] = {1, 5, 10, 50, 100, 500};\n for (int i = 0; i < 6; i++) {\n scanf(\"%d\", &c[i]);\n sum = sum + c[i] * mon[i];\n }\n if (sum >= 1000) {\n printf(\"1\\n\");\n } else {\n printf(\"0\\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 buf.pop();\n unsafe { String::from_utf8_unchecked(buf) }\n };\n\n let (c1, c5, c10, c50, c100, c500) = {\n let mut iter = input.split(' ').map(|s| s.parse::().unwrap());\n\n (\n iter.next().unwrap(),\n iter.next().unwrap(),\n iter.next().unwrap(),\n iter.next().unwrap(),\n iter.next().unwrap(),\n iter.next().unwrap(),\n )\n };\n\n let c = c1 + c5 * 5 + c10 * 10 + c50 * 50 + c100 * 100 + c500 * 500;\n\n if c >= 1000 {\n println!(\"1\");\n } else {\n println!(\"0\");\n }\n}", "difficulty": "easy"} {"problem_id": "0788", "problem_description": "There is a graph of $$$n$$$ rows and $$$10^6 + 2$$$ columns, where rows are numbered from $$$1$$$ to $$$n$$$ and columns from $$$0$$$ to $$$10^6 + 1$$$: Let's denote the node in the row $$$i$$$ and column $$$j$$$ by $$$(i, j)$$$.Initially for each $$$i$$$ the $$$i$$$-th row has exactly one obstacle — at node $$$(i, a_i)$$$. You want to move some obstacles so that you can reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs $$$u$$$ or $$$v$$$ coins, as below: If there is an obstacle in the node $$$(i, j)$$$, you can use $$$u$$$ coins to move it to $$$(i-1, j)$$$ or $$$(i+1, j)$$$, if such node exists and if there is no obstacle in that node currently. If there is an obstacle in the node $$$(i, j)$$$, you can use $$$v$$$ coins to move it to $$$(i, j-1)$$$ or $$$(i, j+1)$$$, if such node exists and if there is no obstacle in that node currently. Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from $$$(1,1)$$$ to $$$(0,1)$$$. Refer to the picture above for a better understanding. Now you need to calculate the minimal number of coins you need to spend to be able to reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph without passing through obstacles.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n for (int x = 0; x < t; ++x) {\n int n;\n int u;\n int v;\n scanf(\"%d %d %d\", &n, &u, &v);\n\n long long int ar[n];\n for (int i = 0; i < n; ++i) {\n scanf(\"%lld\", &ar[i]);\n }\n\n short flag_2 = 0;\n short flag_1 = 0;\n\n for (int i = 1; i < n; ++i) {\n if (ar[i] - ar[i - 1] == -1 || ar[i] - ar[i - 1] == 1) {\n flag_1 = 1;\n } else if (ar[i] - ar[i - 1] <= -2 || ar[i] - ar[i - 1] >= 2) {\n flag_2 = 1;\n printf(\"0\\n\");\n break;\n }\n }\n\n if (flag_2 == 0 && flag_1 == 1) {\n if (u <= v) {\n printf(\"%d\\n\", u);\n } else {\n printf(\"%d\\n\", v);\n }\n } else if (flag_2 == 0 && flag_1 == 0) {\n if (u <= v) {\n printf(\"%lld\\n\", (long long int)(u + v));\n } else {\n printf(\"%lld\\n\", (long long int)(2 * v));\n }\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\n let t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..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 u = it.next().unwrap();\n let v = it.next().unwrap();\n\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let mut a0 = it.next().unwrap();\n\n let mut m = 0;\n\n for a1 in it {\n let x = (a1 - a0).abs();\n if m < x {\n m = x;\n }\n a0 = a1;\n }\n\n use std::cmp::min;\n let x = min(u, v);\n let ans = match m {\n 0 => x + v,\n 1 => x,\n _ => 0,\n };\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0789", "problem_description": "Alperen has two strings, $$$s$$$ and $$$t$$$ which are both initially equal to \"a\". He will perform $$$q$$$ operations of two types on the given strings: $$$1 \\;\\; k \\;\\; x$$$ — Append the string $$$x$$$ exactly $$$k$$$ times at the end of string $$$s$$$. In other words, $$$s := s + \\underbrace{x + \\dots + x}_{k \\text{ times}}$$$. $$$2 \\;\\; k \\;\\; x$$$ — Append the string $$$x$$$ exactly $$$k$$$ times at the end of string $$$t$$$. In other words, $$$t := t + \\underbrace{x + \\dots + x}_{k \\text{ times}}$$$. After each operation, determine if it is possible to rearrange the characters of $$$s$$$ and $$$t$$$ such that $$$s$$$ is lexicographically smaller$$$^{\\dagger}$$$ than $$$t$$$.Note that the strings change after performing each operation and don't go back to their initial states.$$$^{\\dagger}$$$ Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. A formal definition is as follows: string $$$p$$$ is lexicographically smaller than string $$$q$$$ if there exists a position $$$i$$$ such that $$$p_i < q_i$$$, and for all $$$j < i$$$, $$$p_j = q_j$$$. If no such $$$i$$$ exists, then $$$p$$$ is lexicographically smaller than $$$q$$$ if the length of $$$p$$$ is less than the length of $$$q$$$. For example, $$$\\texttt{abdc} < \\texttt{abe}$$$ and $$$\\texttt{abc} < \\texttt{abcd}$$$, where we write $$$p < q$$$ if $$$p$$$ is lexicographically smaller than $$$q$$$.", "c_code": "int solution() {\n int x;\n int y;\n int z;\n int i;\n int j;\n int k;\n int q;\n int a;\n int b;\n int c;\n int d;\n int n;\n int m;\n int t;\n char s[500001];\n\n scanf(\"%d\", &t);\n while (t--) {\n long long cont[2][26] = {};\n cont[0][0] = cont[1][0] = 1;\n scanf(\"%d\", &q);\n for (x = 0; x < q; x++) {\n scanf(\"%d %d %s\", &d, &k, s), d--;\n m = strlen(s);\n for (y = 0; y < m; y++) {\n cont[d][s[y] - 'a'] += k;\n }\n\n for (z = 1; z < 26; z++) {\n if (cont[1][z]) {\n puts(\"YES\");\n goto end;\n }\n }\n\n for (y = 1; y < 26; y++) {\n if (cont[0][y]) {\n puts(\"NO\");\n goto end;\n }\n }\n\n printf(cont[0][0] < cont[1][0] ? \"YES\\n\" : \"NO\\n\");\n end:;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n for _ in 0..t {\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\n let mut s_noa = 0;\n let mut t_noa = 0;\n let mut s_size = 1_i64;\n let mut t_size = 1_i64;\n\n for _ in 0..q {\n let (d, k, x): (usize, i64, Vec) = {\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().chars().collect(),\n )\n };\n\n let x_size = x.len() as i64;\n\n let mut noa = 0;\n for ss in x {\n if ss != 'a' {\n noa = 1;\n }\n }\n\n if noa == 1 {\n if d == 1 {\n s_noa = 1;\n } else {\n t_noa = 1;\n }\n } else {\n if d == 1 {\n s_size += x_size * k;\n } else {\n t_size += x_size * k;\n }\n }\n\n if s_noa == 0 && t_noa == 0 {\n if s_size < t_size {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n } else if t_noa == 1 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0790", "problem_description": "You have $$$a$$$ coins of value $$$n$$$ and $$$b$$$ coins of value $$$1$$$. You always pay in exact change, so you want to know if there exist such $$$x$$$ and $$$y$$$ that if you take $$$x$$$ ($$$0 \\le x \\le a$$$) coins of value $$$n$$$ and $$$y$$$ ($$$0 \\le y \\le b$$$) coins of value $$$1$$$, then the total value of taken coins will be $$$S$$$.You have to answer $$$q$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int flag = 0;\n int z[5];\n for (int i = 0; i <= 3; i++) {\n scanf(\"%d\", &z[i]);\n }\n if (z[1] >= z[3]) {\n flag = 1;\n }\n\n else {\n\n if (z[3] / z[2] < z[0]) {\n if (z[3] - (z[3] / z[2]) * z[2] <= z[1]) {\n flag = 1;\n }\n } else {\n if (z[0] * z[2] + z[1] >= z[3]) {\n flag = 1;\n }\n }\n }\n if (flag == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut input_line = String::new();\n stdin.read_line(&mut input_line).unwrap();\n let q = input_line.trim().parse::().unwrap();\n\n for _ in 0..q {\n input_line.clear();\n stdin.read_line(&mut input_line).unwrap();\n let splited = input_line.split_whitespace().collect::>();\n let a = splited[0].parse::().unwrap();\n let b = splited[1].parse::().unwrap();\n let n = splited[2].parse::().unwrap();\n let s = splited[3].parse::().unwrap();\n\n if (s / n <= a && s % n <= b) || (s / n > a && s - a * n <= b) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0791", "problem_description": "You have a string $$$s$$$ consisting of lowercase Latin alphabet letters. You can color some letters in colors from $$$1$$$ to $$$k$$$. It is not necessary to paint all the letters. But for each color, there must be a letter painted in that color.Then you can swap any two symbols painted in the same color as many times as you want. After that, $$$k$$$ strings will be created, $$$i$$$-th of them will contain all the characters colored in the color $$$i$$$, written in the order of their sequence in the string $$$s$$$.Your task is to color the characters of the string so that all the resulting $$$k$$$ strings are palindromes, and the length of the shortest of these $$$k$$$ strings is as large as possible.Read the note for the first test case of the example if you need a clarification.Recall that a string is a palindrome if it reads the same way both from left to right and from right to left. For example, the strings abacaba, cccc, z and dxd are palindromes, but the strings abab and aaabaa — are not.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\\n\", &t);\n while (t--) {\n int n;\n int k;\n scanf(\"%d %d\\n\", &n, &k);\n char a[n + 1];\n scanf(\"%s\\n\", a);\n int arr[26] = {0};\n for (int i = 0; i < n; i++) {\n arr[a[i] - 'a']++;\n }\n int count = 0;\n for (int i = 0; i < 26; i++) {\n count += arr[i] / 2;\n }\n int m = (count / k);\n if (n >= (2 * m + 1) * k) {\n printf(\"%d\\n\", (2 * m) + 1);\n } else {\n printf(\"%d\\n\", 2 * m);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let mut s = input.split_whitespace();\n let t: u32 = s.next().unwrap().parse().unwrap();\n for _x in 0..t {\n input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n s = input.split_whitespace();\n let n: u32 = s.next().unwrap().parse().unwrap();\n let k: u32 = s.next().unwrap().parse().unwrap();\n let mut S = String::new();\n let mut cnt: [u32; 30] = [0; 30];\n io::stdin().read_line(&mut S).unwrap();\n for i in S.bytes() {\n let b: i32 = i as i32 - 97;\n if b < 0 {\n break;\n }\n cnt[b as usize] += 1;\n }\n let mut tot: u32 = 0;\n for i in 0..26 {\n tot += cnt[i] / 2;\n }\n let mut ans = tot / k * 2;\n if n - ans * k >= k {\n ans += 1;\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0792", "problem_description": "There is an array $$$a$$$ with $$$n-1$$$ integers. Let $$$x$$$ be the bitwise XOR of all elements of the array. The number $$$x$$$ is added to the end of the array $$$a$$$ (now it has length $$$n$$$), and then the elements are shuffled.You are given the newly formed array $$$a$$$. What is $$$x$$$? If there are multiple possible values of $$$x$$$, you can output any of them.", "c_code": "int solution() {\n int cases = 0;\n scanf(\"%d\", &cases);\n for (int i = 0; i < cases; i++) {\n int n = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n }\n\n printf(\"%d\\n\", a[0]);\n }\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin()\n .read_to_string(&mut content)\n .expect(\"Unable to read from stdin\");\n\n let mut lines = content.lines();\n let num_tests: usize = lines.next().unwrap().parse().unwrap();\n for _ in 0..num_tests {\n let _n: usize = lines.next().unwrap().parse().unwrap();\n let arr: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse::().unwrap())\n .collect();\n println!(\"{}\", arr[0]);\n }\n}", "difficulty": "medium"} {"problem_id": "0793", "problem_description": "Pashmak has fallen in love with an attractive girl called Parmida since one year ago...Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.", "c_code": "int solution() {\n int x1 = 0;\n int y1 = 0;\n int x2 = 0;\n int y2 = 0;\n int side = 0;\n scanf(\"%d %d %d %d\", &x1, &y1, &x2, &y2);\n if (y1 == y2) {\n side = (x1 - x2);\n printf(\"%d %d %d %d\", x1, y1 + side, x2, y2 + side);\n } else if (x1 == x2) {\n side = (y1 - y2);\n printf(\"%d %d %d %d\", x2 + side, y2, x1 + side, y1);\n } else if ((abs(x1 - x2)) == (abs(y1 - y2))) {\n printf(\"%d %d %d %d\", x2, y1, x1, y2);\n } else {\n printf(\"-1\");\n }\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 s = buf.split_whitespace();\n let x1: i32 = s.next().unwrap().trim().parse().unwrap();\n let y1: i32 = s.next().unwrap().trim().parse().unwrap();\n let x2: i32 = s.next().unwrap().trim().parse().unwrap();\n let y2: i32 = s.next().unwrap().trim().parse().unwrap();\n if x1 == x2 {\n let a = y2 - y1;\n println!(\"{} {} {} {}\", x1 + a, y1, x2 + a, y2);\n } else if y1 == y2 {\n let a = x2 - x1;\n println!(\"{} {} {} {}\", x1, y1 + a, x2, y2 + a);\n } else if (x2 - x1).abs() == (y2 - y1).abs() {\n println!(\"{} {} {} {}\", x1, y2, x2, y1);\n } else {\n println!(\"-1\");\n }\n}", "difficulty": "easy"} {"problem_id": "0794", "problem_description": "Alice and Bob play the following game. Alice has a set $$$S$$$ of disjoint ranges of integers, initially containing only one range $$$[1, n]$$$. In one turn, Alice picks a range $$$[l, r]$$$ from the set $$$S$$$ and asks Bob to pick a number in the range. Bob chooses a number $$$d$$$ ($$$l \\le d \\le r$$$). Then Alice removes $$$[l, r]$$$ from $$$S$$$ and puts into the set $$$S$$$ the range $$$[l, d - 1]$$$ (if $$$l \\le d - 1$$$) and the range $$$[d + 1, r]$$$ (if $$$d + 1 \\le r$$$). The game ends when the set $$$S$$$ is empty. We can show that the number of turns in each game is exactly $$$n$$$.After playing the game, Alice remembers all the ranges $$$[l, r]$$$ she picked from the set $$$S$$$, but Bob does not remember any of the numbers that he picked. But Bob is smart, and he knows he can find out his numbers $$$d$$$ from Alice's ranges, and so he asks you for help with your programming skill.Given the list of ranges that Alice has picked ($$$[l, r]$$$), for each range, help Bob find the number $$$d$$$ that Bob has picked.We can show that there is always a unique way for Bob to choose his number for a list of valid ranges picked by Alice.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int num[n + 10][3];\n for (int i = 1; i <= n; i++) {\n scanf(\"%d %d\", &num[i][0], &num[i][1]);\n }\n for (int i = 1; i <= n; i++) {\n int min = 1000;\n int index;\n for (int j = 1; j <= n; j++) {\n if (num[j][0] <= i && num[j][1] >= i) {\n if (num[j][1] - num[j][0] < min) {\n min = num[j][1] - num[j][0];\n index = j;\n }\n }\n }\n num[index][2] = i;\n }\n for (int i = 1; i <= n; i++) {\n printf(\"%d %d %d\", num[i][0], num[i][1], num[i][2]);\n if (i != n) {\n printf(\"\\n\");\n }\n }\n if (t != 0) {\n printf(\"\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n let mut input = input.split_ascii_whitespace().flat_map(str::parse);\n let t = input.next().unwrap();\n let mut output = String::new();\n for _ in 0..t {\n let n = input.next().unwrap();\n let mut ivs = vec![];\n for _ in 0..n {\n let l = input.next().unwrap();\n let r = input.next().unwrap();\n ivs.push((l, r));\n }\n ivs.sort_unstable_by_key(|&(l, r)| r - l);\n let mut set: BTreeSet<_> = (1..=n).collect();\n for (l, r) in ivs {\n let d = *set.range(l..=r).next().unwrap();\n set.remove(&d);\n writeln!(output, \"{} {} {}\", l, r, d).unwrap();\n }\n }\n print!(\"{}\", output);\n}", "difficulty": "hard"} {"problem_id": "0795", "problem_description": "Given a positive integer $$$k$$$, two arrays are called $$$k$$$-similar if: they are strictly increasing; they have the same length; all their elements are positive integers between $$$1$$$ and $$$k$$$ (inclusive); they differ in exactly one position. You are given an integer $$$k$$$, a strictly increasing array $$$a$$$ and $$$q$$$ queries. For each query, you are given two integers $$$l_i \\leq r_i$$$. Your task is to find how many arrays $$$b$$$ exist, such that $$$b$$$ is $$$k$$$-similar to array $$$[a_{l_i},a_{l_i+1}\\ldots,a_{r_i}]$$$.", "c_code": "int solution() {\n long long int n;\n long long int q;\n long long int k;\n scanf(\"%lld %lld %lld\", &n, &q, &k);\n long long int a[n];\n long long int j;\n long long int l[q];\n long long int r[q];\n long long int ans = 0;\n for (j = 0; j < n; j++) {\n scanf(\"%lld\", &a[j]);\n }\n for (j = 0; j < q; j++) {\n scanf(\"%lld %lld\", &l[j], &r[j]);\n printf(\"%lld\\n\",\n 1 + k - (2 * (r[j] - l[j] + 1)) + a[r[j] - 1] - a[l[j] - 1]);\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 (n, q, k): (usize, i32, i32) = (sc.next(), sc.next(), sc.next());\n let mut arr: Vec = vec![0; n + 1];\n for i in 1..=n {\n arr[i] = sc.next();\n }\n let mut changes: Vec = vec![0; n + 1];\n for i in 2..n {\n changes[i] = changes[i - 1] + arr[i + 1] - arr[i - 1] - 2;\n }\n for _ in 0..q {\n let (l, r): (usize, usize) = (sc.next(), sc.next());\n let ans = if l == r {\n k - 1\n } else {\n changes[r - 1] - changes[l] + arr[l + 1] - 2 + k + 1 - arr[r - 1] - 2\n };\n writeln!(out, \"{}\", ans).unwrap();\n }\n}", "difficulty": "easy"} {"problem_id": "0796", "problem_description": "You still have partial information about the score during the historic football match. You are given a set of pairs $$$(a_i, b_i)$$$, indicating that at some point during the match the score was \"$$$a_i$$$: $$$b_i$$$\". It is known that if the current score is «$$$x$$$:$$$y$$$», then after the goal it will change to \"$$$x+1$$$:$$$y$$$\" or \"$$$x$$$:$$$y+1$$$\". What is the largest number of times a draw could appear on the scoreboard?The pairs \"$$$a_i$$$:$$$b_i$$$\" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.", "c_code": "int solution() {\n int maxDraw = -1;\n int lastl = 0;\n int lastr = 0;\n int number = 0;\n scanf(\"%d\", &number);\n int answer = 0;\n for (int i = 0; i < number; i++) {\n int maxLast = (lastl <= lastr) ? lastr : lastl;\n int left;\n int right;\n scanf(\"%d%d\", &left, &right);\n int minCurrent = (left <= right) ? left : right;\n if (minCurrent < maxLast) {\n lastl = left;\n lastr = right;\n continue;\n }\n for (int i = maxLast; i <= minCurrent; i++) {\n if (i <= maxDraw) {\n continue;\n }\n answer += 1;\n maxDraw = i;\n }\n lastl = left;\n lastr = right;\n }\n printf(\"%d\", answer);\n return 0;\n}", "rust_code": "fn solution() {\n let mut sin = String::new();\n std::io::stdin().read_line(&mut sin).expect(\"read error\");\n let n = sin.trim().parse::().unwrap();\n\n let mut ret = 1;\n let (mut a, mut b) = (0, 0);\n let mut c;\n let mut d;\n for _ in 1..=n {\n sin.clear();\n std::io::stdin().read_line(&mut sin).expect(\"read error\");\n let vi32: Vec = sin.split_whitespace().map(|c| c.parse().unwrap()).collect();\n c = vi32[0];\n d = vi32[1];\n ret += match min(c, d) - max(a, b) {\n x if x >= 0 => {\n if a == b {\n x\n } else {\n x + 1\n }\n }\n _ => 0,\n };\n a = c;\n b = d;\n }\n println!(\"{}\", ret);\n}", "difficulty": "medium"} {"problem_id": "0797", "problem_description": "The 2050 volunteers are organizing the \"Run! Chase the Rising Sun\" activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.There are $$$n+1$$$ checkpoints on the trail. They are numbered by $$$0$$$, $$$1$$$, ..., $$$n$$$. A runner must start at checkpoint $$$0$$$ and finish at checkpoint $$$n$$$. No checkpoint is skippable — he must run from checkpoint $$$0$$$ to checkpoint $$$1$$$, then from checkpoint $$$1$$$ to checkpoint $$$2$$$ and so on. Look at the picture in notes section for clarification.Between any two adjacent checkpoints, there are $$$m$$$ different paths to choose. For any $$$1\\le i\\le n$$$, to run from checkpoint $$$i-1$$$ to checkpoint $$$i$$$, a runner can choose exactly one from the $$$m$$$ possible paths. The length of the $$$j$$$-th path between checkpoint $$$i-1$$$ and $$$i$$$ is $$$b_{i,j}$$$ for any $$$1\\le j\\le m$$$ and $$$1\\le i\\le n$$$.To test the trail, we have $$$m$$$ runners. Each runner must run from the checkpoint $$$0$$$ to the checkpoint $$$n$$$ once, visiting all the checkpoints. Every path between every pair of adjacent checkpoints needs to be ran by exactly one runner. If a runner chooses the path of length $$$l_i$$$ between checkpoint $$$i-1$$$ and $$$i$$$ ($$$1\\le i\\le n$$$), his tiredness is $$$$$$\\min_{i=1}^n l_i,$$$$$$ i. e. the minimum length of the paths he takes.Please arrange the paths of the $$$m$$$ runners to minimize the sum of tiredness of them.", "c_code": "int solution(void) {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n int n;\n int m;\n int M;\n int i;\n int j;\n int k;\n int l;\n int J;\n scanf(\"%d %d\", &n, &m);\n M = m;\n long long int a[n][m];\n long long int b[n][m];\n long long int summin = 0;\n long long int t;\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n scanf(\"%lld\", &a[i][j]);\n }\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < m - 1; j++) {\n int hmin = a[i][j];\n int lindex = j;\n for (k = j + 1; k < m; k++) {\n if (a[i][k] <= hmin) {\n hmin = a[i][k];\n lindex = k;\n }\n }\n a[i][lindex] = a[i][j];\n a[i][j] = hmin;\n }\n }\n for (j = 0; j < m; j++) {\n int min = 1000000001;\n int hindex = 0;\n for (i = 0; i < n; i++) {\n if (a[i][j] <= min) {\n min = a[i][j];\n hindex = i;\n }\n }\n for (i = 0; i < n; i++) {\n if (i != hindex) {\n t = a[i][j];\n a[i][j] = a[i][m - 1];\n a[i][m - 1] = t;\n }\n }\n for (i = 0; i < n; i++) {\n b[i][j] = a[i][j];\n }\n for (i = 0; i < n; i++) {\n for (J = j + 1; J < m - 1; J++) {\n int hmin = a[i][J];\n int lindex = J;\n for (k = J + 1; k < m; k++) {\n if (a[i][k] <= hmin) {\n hmin = a[i][k];\n lindex = k;\n }\n }\n a[i][lindex] = a[i][J];\n a[i][J] = hmin;\n }\n }\n }\n\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n printf(\"%lld \", b[i][j]);\n if (j == m - 1) {\n printf(\"\\n\");\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let (n, m) = scan!(usize, usize);\n let mut edg = scan!([[i64; m]; n]);\n edg.iter_mut().for_each(|v| v.sort());\n\n use std::collections::BinaryHeap;\n\n let mut tri = BinaryHeap::<(i64, usize)>::new();\n for i in 0..m {\n tri.push((10i64.pow(18), i));\n }\n\n let mut ans = vec![vec![]; m];\n\n for v in edg {\n let mut nxt = BinaryHeap::<(i64, usize)>::new();\n for l in v {\n if let Some((c, i)) = tri.pop() {\n nxt.push((c.min(l), i));\n ans[i].push(l);\n }\n }\n tri = nxt;\n }\n\n for i in 0..n {\n for j in 0..m {\n print!(\"{} \", ans[j][i]);\n }\n print!(\"\\n\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0798", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

\n

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

\n

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

\n

Let 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
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 26
  • \n
  • S is a diverse word.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

atcoder\n
\n
\n
\n
\n
\n

Sample Output 1

atcoderb\n
\n

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

\n
\n
\n
\n
\n
\n

Sample Input 2

abc\n
\n
\n
\n
\n
\n

Sample Output 2

abcd\n
\n
\n
\n
\n
\n
\n

Sample Input 3

zyxwvutsrqponmlkjihgfedcba\n
\n
\n
\n
\n
\n

Sample Output 3

-1\n
\n

This is the lexicographically largest diverse word, so the answer is -1.

\n
\n
\n
\n
\n
\n

Sample Input 4

abcdefghijklmnopqrstuvwzyx\n
\n
\n
\n
\n
\n

Sample Output 4

abcdefghijklmnopqrstuvx\n
\n
\n
", "c_code": "int solution() {\n char s[27];\n scanf(\"%s\", s);\n int l = strlen(s);\n int alp[26] = {0};\n if (strcmp(s, \"zyxwvutsrqponmlkjihgfedcba\") == 0) {\n printf(\"-1\");\n return 0;\n }\n\n if (l < 26) {\n for (int i = 0; i < l; i++) {\n alp[s[i] - 'a']++;\n }\n printf(\"%s\", s);\n for (int i = 0; i < 26; i++) {\n if (alp[i] == 0) {\n printf(\"%c\", 'a' + i);\n return 0;\n }\n }\n }\n int p;\n for (int i = 25; i > 0; i--) {\n if (s[i] - 'a' > s[i - 1] - 'a') {\n p = i - 1;\n break;\n }\n }\n for (int i = 0; i < p; i++) {\n alp[s[i] - 'a']++;\n printf(\"%c\", s[i]);\n }\n for (int i = 0; i < 26; i++) {\n if (alp[i] == 0 && s[p] - 'a' < i) {\n printf(\"%c\", 'a' + i);\n return 0;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let S: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n };\n\n let ans = if S.len() < 26 {\n format!(\n \"{}{}\",\n S.iter().copied().collect::(),\n (0..26)\n .map(|i| (b'a' + i) as char)\n .find(|c| !S.contains(c))\n .unwrap()\n )\n } else if let Some(i) = (0..25).rev().find(|&i| S[i] < S[i + 1]) {\n format!(\n \"{}{}\",\n S[..i].iter().copied().collect::(),\n S.iter().rev().find(|&&c| S[i] < c).unwrap()\n )\n } else {\n \"-1\".to_string()\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0799", "problem_description": "You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.You are a coach at a very large university and you know that $$$c$$$ of your students are coders, $$$m$$$ are mathematicians and $$$x$$$ have no specialization.What is the maximum number of full perfect teams you can distribute them into? Note that some students can be left without a team and each student can be a part of no more than one team.You are also asked to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int a = 0;\n int b[10];\n int c = 0;\n int d = 0;\n scanf(\"%d\", &a);\n for (int x = 0; x < a; x++) {\n c = 0, d = 0;\n for (int y = 0; y < 3; y++) {\n scanf(\"%d\", &b[y]);\n c = c + b[y];\n }\n if (b[0] > b[1]) {\n d = b[1];\n } else if (b[0] <= b[1]) {\n d = b[0];\n }\n c = c - c % 3;\n c = c / 3;\n if (c >= d) {\n printf(\"%d\\n\", d);\n } else {\n printf(\"%d\\n\", c);\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 q = input.trim().parse::().unwrap();\n\n for _ in 0..q {\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_iter = input.split_whitespace();\n let c = input_iter.next().unwrap().parse::().unwrap();\n let m = input_iter.next().unwrap().parse::().unwrap();\n let x = input_iter.next().unwrap().parse::().unwrap();\n\n let mean = (c + m + x) / 3;\n\n println!(\"{}\", c.min(m).min(mean));\n }\n}", "difficulty": "hard"} {"problem_id": "0800", "problem_description": "

Grading

\n\n

\n Write a program which reads a list of student test scores and evaluates the performance for each student.\n

\n\n

\n The test scores for a student include scores of the midterm examination m (out of 50), the final examination f (out of 50) and the makeup examination r (out of 100). If the student does not take the examination, the score is indicated by -1.\n

\n\n

\n The final performance of a student is evaluated by the following procedure:\n

\n\n
    \n
  • If the student does not take the midterm or final examination, the student's grade shall be F.
  • \n
  • If the total score of the midterm and final examination is greater than or equal to 80, the student's grade shall be A.
  • \n
  • If the total score of the midterm and final examination is greater than or equal to 65 and less than 80, the student's grade shall be B.
  • \n
  • If the total score of the midterm and final examination is greater than or equal to 50 and less than 65, the student's grade shall be C.
  • \n
  • If the total score of the midterm and final examination is greater than or equal to 30 and less than 50, the student's grade shall be D. However, if the score of the makeup examination is greater than or equal to 50, the grade shall be C.
  • \n
  • If the total score of the midterm and final examination is less than 30, the student's grade shall be F.
  • \n
\n\n\n

Input

\n

\n The input consists of multiple datasets. For each dataset, three integers m, f and r are given in a line.\n

\n\n

\n The input ends with three -1 for m, f and r respectively. Your program should not process for the terminal symbols.\n

\n\n

\nThe number of datasets (the number of students) does not exceed 50.\n

\n\n

Output

\n\n

\n For each dataset, print the grade (A, B, C, D or F) in a line.\n

\n\n

Sample Input

\n\n
\n40 42 -1\n20 30 -1\n0 2 -1\n-1 -1 -1\n
\n\n

Sample Output

\n\n
\nA\nC\nF\n
", "c_code": "int solution(void) {\n student exam;\n while (1) {\n scanf(\"%d %d %d\", &exam.midterm, &exam.final, &exam.make_up);\n if (exam.midterm == -1 && exam.final == -1 && exam.make_up == -1) {\n break;\n }\n if (exam.midterm == -1 || exam.final == -1 ||\n exam.midterm + exam.final < 30) {\n printf(\"F\\n\");\n } else if (exam.midterm + exam.final >= 80) {\n printf(\"A\\n\");\n } else if (exam.midterm + exam.final >= 65) {\n printf(\"B\\n\");\n } else if (exam.midterm + exam.final >= 50 ||\n (exam.midterm + exam.final >= 30 && exam.make_up >= 50)) {\n printf(\"C\\n\");\n } else {\n printf(\"D\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let scan = io::stdin();\n for line in scan.lock().lines() {\n let uline = line.unwrap();\n let vec: Vec<&str> = uline.split_whitespace().collect();\n let md: i32 = vec[0].parse().unwrap_or(0);\n let fi: i32 = vec[1].parse().unwrap_or(0);\n let mu: i32 = vec[2].parse().unwrap_or(0);\n if md == -1 && fi == -1 && mu == -1 {\n break;\n } else if md == -1 || fi == -1 {\n println!(\"F\");\n } else {\n let sum = md + fi;\n if sum >= 80 {\n println!(\"A\");\n } else if sum >= 65 {\n println!(\"B\");\n } else if sum >= 50 {\n println!(\"C\");\n } else if sum >= 30 {\n if mu >= 50 {\n println!(\"C\");\n } else {\n println!(\"D\");\n }\n } else {\n println!(\"F\");\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0801", "problem_description": "There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane. Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x0, y0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (x0, y0).Your task is to determine what minimum number of shots Han Solo needs to defeat all the stormtroopers.The gun is the newest invention, it shoots very quickly and even after a very large number of shots the stormtroopers don't have enough time to realize what's happening and change their location.", "c_code": "int solution() {\n int n;\n int x1;\n int y1;\n int count = 0;\n int i;\n int j;\n scanf(\"%d %d %d\", &n, &x1, &y1);\n int x[n];\n int y[n];\n for (i = 0; i < n; i++) {\n scanf(\"%d %d\", &x[i], &y[i]);\n }\n for (i = 0; i < n; i++) {\n if (n == 1) {\n\n count++;\n break;\n }\n for (j = i + 1; j < n; j++) {\n if (x[i] != 200000 && y[i] != y1 && y[j] != y1) {\n float a = x1 - x[i];\n float b = y1 - y[i];\n a = a / b;\n float c = x1 - x[j];\n float d = y1 - y[j];\n c = c / d;\n if (a == c) {\n x[j] = 200000;\n }\n } else if (x[i] != 200000 && y[i] == y[j] && y[i] == y1) {\n\n x[j] = 200000;\n }\n }\n if (x[i] != 200000) {\n count++;\n }\n }\n printf(\"%d\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let (n, c0) = {\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 (\n iter.next().unwrap().parse::().unwrap(),\n iter.next().unwrap().parse::().unwrap(),\n ),\n )\n };\n\n let mut troopers = Vec::new();\n for _ in 0..n {\n troopers.push({\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 )\n });\n }\n\n let mut count: usize = 0;\n while let Some(trooper) = troopers.pop() {\n count += 1;\n\n let x1 = c0.0;\n let x2 = trooper.0;\n let y1 = c0.1;\n let y2 = trooper.1;\n\n let difx = x2 - x1;\n let dify = y2 - y1;\n\n troopers.retain(|(x, y)| (*x - x1) * dify != (*y - y1) * difx);\n }\n\n println!(\"{}\", count);\n}", "difficulty": "hard"} {"problem_id": "0802", "problem_description": "You are given $$$n$$$ rectangles, each of height $$$1$$$. Each rectangle's width is a power of $$$2$$$ (i. e. it can be represented as $$$2^x$$$ for some non-negative integer $$$x$$$). You are also given a two-dimensional box of width $$$W$$$. Note that $$$W$$$ may or may not be a power of $$$2$$$. Moreover, $$$W$$$ is at least as large as the width of the largest rectangle.You have to find the smallest height of this box, such that it is able to fit all the given rectangles. It is allowed to have some empty space left in this box after fitting all the rectangles.You cannot rotate the given rectangles to make them fit into the box. Moreover, any two distinct rectangles must not overlap, i. e., any two distinct rectangles must have zero intersection area.See notes for visual explanation of sample input.", "c_code": "int solution() {\n int x;\n int y;\n int z;\n int w;\n int i;\n int j;\n int k;\n int a;\n int b;\n int c;\n int n;\n int m;\n int t;\n int mat[25];\n int res;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d %d\", &n, &w);\n memset(mat, 0, sizeof(mat));\n res = 0;\n\n for (x = 0; x < n; x++) {\n scanf(\"%d\", &a);\n for (y = 0; y <= 20; y++) {\n if (a & (1 << y)) {\n mat[y]++;\n break;\n }\n }\n }\n for (x = 20; x >= 0; x--) {\n while (mat[x]) {\n c = 0;\n res++;\n c += 1 << x;\n mat[x]--;\n y = x;\n while (y >= 0) {\n if (mat[y] && c + (1 << y) <= w) {\n c += 1 << y;\n mat[y]--;\n } else {\n y--;\n }\n }\n }\n }\n printf(\"%d\\n\", res);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let (n, w) = scan!(usize, u64);\n let a = scan!([u64; n]);\n let h = 20;\n let mut f = vec![0; h + 1];\n for &a in &a {\n for i in 0..=h {\n if (a >> i) & 1 == 1 {\n f[i] += 1;\n }\n }\n }\n\n let mut r = n;\n let mut ans = 1;\n let mut avl = w;\n while r > 0 {\n let mut flag = false;\n for i in (0..=h).rev() {\n if f[i] > 0 && avl >= (1 << i) {\n f[i] -= 1;\n r -= 1;\n avl -= 1 << i;\n flag = true;\n break;\n }\n }\n\n if !flag {\n ans += 1;\n avl = w;\n }\n }\n print!(\"{}\", ans);\n print!(\"\\n\");\n }\n}", "difficulty": "easy"} {"problem_id": "0803", "problem_description": "You are given a grid with $$$n$$$ rows and $$$m$$$ columns. Rows and columns are numbered from $$$1$$$ to $$$n$$$, and from $$$1$$$ to $$$m$$$. The intersection of the $$$a$$$-th row and $$$b$$$-th column is denoted by $$$(a, b)$$$. Initially, you are standing in the top left corner $$$(1, 1)$$$. Your goal is to reach the bottom right corner $$$(n, m)$$$.You can move in four directions from $$$(a, b)$$$: up to $$$(a-1, b)$$$, down to $$$(a+1, b)$$$, left to $$$(a, b-1)$$$ or right to $$$(a, b+1)$$$.You cannot move in the same direction in two consecutive moves, and you cannot leave the grid. What is the minimum number of moves to reach $$$(n, m)$$$?", "c_code": "int solution() {\n int n;\n int m;\n int g = 0;\n while (scanf(\"%d\", &g) != EOF) {\n while (g--) {\n scanf(\"%d%d\", &n, &m);\n if (n > m) {\n int t = n;\n n = m;\n m = t;\n }\n if (n == 1) {\n if (m > 2) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d\\n\", m - 1);\n }\n continue;\n }\n int c = (2 * n - 2) + (2 * (m - n)) - ((m - n) % 2);\n printf(\"%d\\n\", c);\n }\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n stdin().read_line(&mut buffer).expect(\"Failed\");\n let entries = buffer.trim().parse::().unwrap();\n for _ in 0..entries {\n let mut accumulator = 0;\n buffer.clear();\n stdin().read_line(&mut buffer).expect(\"Failed\");\n let inputs = buffer\n .trim()\n .split(\" \")\n .map(|x| x.trim().parse::().expect(\"Failed\") - 1)\n .collect::>();\n let inputs = (inputs[0], inputs[1]);\n\n if inputs == (0, 0) {\n println!(\"0\")\n } else if inputs.0 == 0 || inputs.1 == 0 {\n if inputs.0 == 1 || inputs.1 == 1 {\n println!(\"1\");\n } else {\n println!(\"-1\");\n }\n } else {\n accumulator += min(inputs.0, inputs.1) * 2;\n let remainder = max(inputs.0, inputs.1) - min(inputs.0, inputs.1);\n if remainder % 2 == 0 {\n accumulator += remainder * 2\n } else {\n accumulator += (remainder - 1) * 2 + 1\n }\n\n println!(\"{}\", accumulator)\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0804", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are N dishes, numbered 1, 2, \\ldots, N.\nInitially, for each i (1 \\leq i \\leq N), Dish i has a_i (1 \\leq a_i \\leq 3) pieces of sushi on it.

\n

Taro will perform the following operation repeatedly until all the pieces of sushi are eaten:

\n
    \n
  • Roll a die that shows the numbers 1, 2, \\ldots, N with equal probabilities, and let i be the outcome. If there are some pieces of sushi on Dish i, eat one of them; if there is none, do nothing.
  • \n
\n

Find the expected number of times the operation is performed before all the pieces of sushi are eaten.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 300
  • \n
  • 1 \\leq a_i \\leq 3
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 \\ldots a_N\n
\n
\n
\n
\n
\n

Output

Print the expected number of times the operation is performed before all the pieces of sushi are eaten.\nThe output is considered correct when the relative difference is not greater than 10^{-9}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 1 1\n
\n
\n
\n
\n
\n

Sample Output 1

5.5\n
\n

The expected number of operations before the first piece of sushi is eaten, is 1.\nAfter that, the expected number of operations before the second sushi is eaten, is 1.5.\nAfter that, the expected number of operations before the third sushi is eaten, is 3.\nThus, the expected total number of operations is 1 + 1.5 + 3 = 5.5.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n3\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

Outputs such as 3.00, 3.000000003 and 2.999999997 will also be accepted.

\n
\n
\n
\n
\n
\n

Sample Input 3

2\n1 2\n
\n
\n
\n
\n
\n

Sample Output 3

4.5\n
\n
\n
\n
\n
\n
\n

Sample Input 4

10\n1 3 2 3 3 2 3 2 1 3\n
\n
\n
\n
\n
\n

Sample Output 4

54.48064457488221\n
\n
\n
", "c_code": "int solution(void) {\n int i;\n int j;\n int k;\n int N;\n int a[310];\n int one = 0;\n int two = 0;\n int three = 0;\n double dp[310][310][310];\n scanf(\"%d\", &N);\n for (i = 1; i <= N; i++) {\n scanf(\"%d\", &a[i]);\n switch (a[i]) {\n case 1:\n one++;\n break;\n case 2:\n two++;\n break;\n case 3:\n three++;\n break;\n default:\n break;\n }\n }\n\n dp[0][0][0] = 0.0;\n for (i = 1; i <= N; i++) {\n dp[i][0][0] = dp[i - 1][0][0] + (N + 0.0) / i;\n }\n for (k = 0; k <= N; k++) {\n for (j = 0; k + j <= N; j++) {\n if (k + j == 0) {\n continue;\n }\n for (i = 0; k + j + i <= N; i++) {\n if (i > 0) {\n dp[i][j][k] += dp[i - 1][j][k] * i / (i + j + k);\n }\n if (j > 0) {\n dp[i][j][k] += dp[i + 1][j - 1][k] * j / (i + j + k);\n }\n if (k > 0) {\n dp[i][j][k] += dp[i][j + 1][k - 1] * k / (i + j + k);\n }\n dp[i][j][k] += (N + 0.0) / (i + j + k);\n }\n }\n }\n\n printf(\"%.13lf\\n\", dp[one][two][three]);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n a: [usize; n],\n }\n\n let cc3 = a.iter().filter(|&v| *v == 3).count();\n let cc2 = a.iter().filter(|&v| *v == 2).count();\n let cc1 = a.iter().filter(|&v| *v == 1).count();\n\n let mut dp = vec![vec![vec![0.0; n + 10]; n + 10]; n + 10];\n\n for c3 in 0..n + 1 {\n for c2 in 0..n + 1 {\n for c1 in 0..n + 1 {\n if c3 == 0 && c2 == 0 && c1 == 0 {\n continue;\n }\n if c1 + c2 + c3 > cc1 + cc2 + cc3 {\n continue;\n }\n\n let mut res = 0.0;\n res += n as f64;\n if c3 > 0 {\n res += c3 as f64 * dp[c3 - 1][c2 + 1][c1];\n }\n if c2 > 0 {\n res += c2 as f64 * dp[c3][c2 - 1][c1 + 1];\n }\n if c1 > 0 {\n res += c1 as f64 * dp[c3][c2][c1 - 1];\n }\n res /= (c1 + c2 + c3) as f64;\n dp[c3][c2][c1] = res;\n }\n }\n }\n println!(\"{}\", dp[cc3][cc2][cc1]);\n}", "difficulty": "medium"} {"problem_id": "0805", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N children, numbered 1, 2, ..., N.

\n

Snuke has decided to distribute x sweets among them.\nHe needs to give out all the x sweets, but some of the children may get zero sweets.

\n

For each i (1 \\leq i \\leq N), Child i will be happy if he/she gets exactly a_i sweets.\nSnuke is trying to maximize the number of happy children by optimally distributing the sweets.\nFind the maximum possible number of happy children.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 100
  • \n
  • 1 \\leq x \\leq 10^9
  • \n
  • 1 \\leq a_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N x\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number of happy children.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 70\n20 30 10\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

One optimal way to distribute sweets is (20, 30, 20).

\n
\n
\n
\n
\n
\n

Sample Input 2

3 10\n20 30 10\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

The optimal way to distribute sweets is (0, 0, 10).

\n
\n
\n
\n
\n
\n

Sample Input 3

4 1111\n1 10 100 1000\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n

The optimal way to distribute sweets is (1, 10, 100, 1000).

\n
\n
\n
\n
\n
\n

Sample Input 4

2 10\n20 20\n
\n
\n
\n
\n
\n

Sample Output 4

0\n
\n

No children will be happy, no matter how the sweets are distributed.

\n
\n
", "c_code": "int solution() {\n int n = 0;\n int x = 0;\n scanf(\"%d %d\", &n, &x);\n\n int a[n];\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n for (int i = 0; i < n; i++) {\n for (int j = n - 1; j > i; j--) {\n if (a[j] < a[j - 1]) {\n int temp = a[j];\n a[j] = a[j - 1];\n a[j - 1] = temp;\n }\n }\n }\n\n int count = 0;\n\n for (int i = 0; x > 0; i++) {\n if (a[i] > x || (n == count + 1 && x > a[i])) {\n break;\n }\n x -= a[i];\n\n count++;\n }\n\n printf(\"%d\\n\", count);\n\n return 0;\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 n = it.next().unwrap().parse::().unwrap();\n let mut x = it.next().unwrap().parse::().unwrap();\n let mut a = (0..n)\n .map(|_| it.next().unwrap().parse::().unwrap())\n .collect::>();\n a.sort();\n let mut res = 0;\n for &t in &a {\n if t <= x {\n x -= t;\n res += 1;\n }\n }\n if res == n && x != 0 {\n res -= 1;\n }\n println!(\"{}\", res);\n}", "difficulty": "medium"} {"problem_id": "0806", "problem_description": "Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \\ne j$$$; $$$1 \\leq i,j \\leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$\\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int zeros = 0;\n scanf(\"%d\", &n);\n int arr[n];\n int binary[101];\n for (int i = 0; i < 101; ++i) {\n binary[i] = 0;\n }\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &arr[i]);\n binary[arr[i]]++;\n if (arr[i] == 0) {\n zeros++;\n }\n }\n int check = 0;\n if (zeros != 0 && check == 0) {\n printf(\"%d\\n\", n - zeros);\n check++;\n } else {\n for (int i = 0; i < 101; ++i) {\n if (binary[i] > 1 && check == 0) {\n printf(\"%d\\n\", n);\n check++;\n }\n }\n if (check == 0) {\n printf(\"%d\\n\", n + 1);\n }\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut inputs = String::new();\n stdin.lock().read_to_string(&mut inputs).ok();\n\n let mut scanner = inputs\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n let case = scanner.next().unwrap();\n\n for _ in 0..case {\n let n = scanner.next().unwrap();\n let mut cnt = vec![0; 101];\n\n for _ in 0..n {\n let idx = scanner.next().unwrap();\n if idx == 0 {\n cnt[0] += 1;\n } else {\n cnt[idx] = 2_i32.min(cnt[idx] + 1);\n }\n }\n if cnt[0] >= 1 {\n println!(\"{}\", n - cnt[0] as usize);\n } else if cnt.contains(&2) {\n println!(\"{}\", n);\n } else {\n println!(\"{}\", n + 1);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0807", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

A biscuit making machine produces B biscuits at the following moments: A seconds, 2A seconds, 3A seconds and each subsequent multiple of A seconds after activation.

\n

Find the total number of biscuits produced within T + 0.5 seconds after activation.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A, B, T \\leq 20
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B T\n
\n
\n
\n
\n
\n

Output

Print the total number of biscuits produced within T + 0.5 seconds after activation.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 5 7\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n
    \n
  • Five biscuits will be produced three seconds after activation.
  • \n
  • Another five biscuits will be produced six seconds after activation.
  • \n
  • Thus, a total of ten biscuits will be produced within 7.5 seconds after activation.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 2 9\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
\n
\n
\n
\n

Sample Input 3

20 20 19\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n int time = 0;\n int biscuit = 0;\n int t_limit = 0;\n int i = 0;\n int b_cnt = 0;\n scanf(\"%d%d%d\", &time, &biscuit, &t_limit);\n for (i = 1; i * time < t_limit + 0.5; i++) {\n b_cnt += biscuit;\n }\n printf(\"%d\", b_cnt);\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 let line: Vec = buf.split_whitespace().map(|s| s.parse().unwrap()).collect();\n let A = line[0];\n let B = line[1];\n let T = line[2];\n println!(\"{}\", T / A * B);\n}", "difficulty": "hard"} {"problem_id": "0808", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:

\n
    \n
  • Assume that the grid currently has a horizontal rows and b vertical columns. Choose \"vertical\" or \"horizontal\".
      \n
    • If we choose \"vertical\", insert one row at the top of the grid, resulting in an (a+1) \\times b grid.
    • \n
    • If we choose \"horizontal\", insert one column at the right end of the grid, resulting in an a \\times (b+1) grid.
    • \n
    \n
  • \n
  • Then, paint one of the added squares black, and the other squares white.
  • \n
\n

Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A \\leq C \\leq 3000
  • \n
  • 1 \\leq B \\leq D \\leq 3000
  • \n
  • A, B, C, and D are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C D\n
\n
\n
\n
\n
\n

Output

Print the number of ways in which the squares can be painted in the end, modulo 998244353.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 1 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

Any two of the three squares other than the bottom-left square can be painted black.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 1 3 4\n
\n
\n
\n
\n
\n

Sample Output 2

65\n
\n
\n
\n
\n
\n
\n

Sample Input 3

31 41 59 265\n
\n
\n
\n
\n
\n

Sample Output 3

387222020\n
\n
\n
", "c_code": "int solution() {\n long long int a;\n long long int b;\n long long int c;\n long long int d;\n long long int i;\n long long int j;\n long long int x[3100][3100];\n long long int P = 998244353;\n scanf(\"%lld %lld %lld %lld\", &a, &b, &c, &d);\n\n x[a][b] = 1;\n for (i = a; i <= c - 1; i++) {\n x[i + 1][b] = (x[i][b] * b) % P;\n }\n for (j = b; j <= d - 1; j++) {\n x[a][j + 1] = (x[a][j] * a) % P;\n }\n for (i = a; i <= c - 1; i++) {\n for (j = b; j <= d - 1; j++) {\n x[i + 1][j + 1] =\n (((x[i + 1][j] * (i + 1)) % P) + ((x[i][j + 1] * (j + 1)) % P) -\n ((x[i][j] * i * j) % P)) %\n P;\n }\n }\n\n if (x[c][d] >= 0) {\n printf(\"%lld\\n\", x[c][d]);\n } else {\n printf(\"%lld\\n\", x[c][d] + P);\n }\n return 0;\n}", "rust_code": "fn solution() {\n const MOD: usize = 998244353;\n\n let [a, b, c, d]: [usize; 4] = {\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 iter.next().unwrap().parse().unwrap(),\n ]\n };\n\n let mut dp = vec![vec![vec![0; 2]; d + 1]; c + 1];\n dp[a][b][1] = 1;\n for i in a..(c + 1) {\n for j in b..(d + 1) {\n if i < c {\n dp[i + 1][j][0] = (dp[i][j][0] + dp[i][j][1]) * j % MOD;\n }\n if j < d {\n dp[i][j + 1][1] = (dp[i][j][0] + dp[i][j][1] * i) % MOD;\n }\n }\n }\n\n let ans = (dp[c][d][0] + dp[c][d][1]) % MOD;\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0809", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

\n

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

\n

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

\n

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 101 \\le X \\le 10^{18}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

\n

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

103\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n
    \n
  • The balance after one year is 101 yen.
  • \n
  • The balance after two years is 102 yen.
  • \n
  • The balance after three years is 103 yen.
  • \n
\n

Thus, it takes three years for the balance to reach 103 yen or above.

\n
\n
\n
\n
\n
\n

Sample Input 2

1000000000000000000\n
\n
\n
\n
\n
\n

Sample Output 2

3760\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1333333333\n
\n
\n
\n
\n
\n

Sample Output 3

1706\n
\n
\n
", "c_code": "int solution(void) {\n\n long X = 0;\n long i = 100;\n int count = 0;\n scanf(\"%ld\", &X);\n\n while (i < X) {\n i *= 1.01;\n count++;\n }\n printf(\"%d\\n\", count);\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 buf.pop();\n let X = i64::from_str(&buf).unwrap();\n let mut money = 100;\n let mut cnt = 1;\n loop {\n money = money + (money / 100);\n if money >= X {\n println!(\"{}\", cnt);\n return;\n }\n cnt += 1;\n }\n}", "difficulty": "medium"} {"problem_id": "0810", "problem_description": "You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.", "c_code": "int solution() {\n int n;\n int m;\n char mat[105][105];\n scanf(\"%d %d\", &n, &m);\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", mat[i]);\n }\n int cnt = 0;\n int del[105] = {0};\n int les[105] = {0};\n\n for (int i = 0; i < m; i++) {\n int flag = 0;\n for (int j = 1; j < n; j++) {\n if (les[j] == 0 && mat[j][i] < mat[j - 1][i]) {\n\n del[i] = 1;\n flag = 1;\n break;\n }\n }\n if (flag) {\n cnt++;\n } else {\n for (int j = 1; j < n; j++) {\n if (mat[j - 1][i] < mat[j][i]) {\n les[j] = 1;\n }\n }\n }\n }\n\n printf(\"%d\\n\", cnt);\n\n return 0;\n}", "rust_code": "fn solution() {\n let (n, m) = {\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 )\n };\n\n let mut matrix = [[0u8; 100]; 100];\n let mut ok = [false; 99];\n\n for i in 0..n {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let line = line.trim().as_bytes();\n for j in 0..m {\n matrix[i][j] = line[j];\n }\n }\n\n let mut answ: usize = 0;\n for i in 0..m {\n let mut banned = false;\n let mut local_ok = [false; 99];\n\n for j in 1..n {\n if !ok[j - 1] && matrix[j - 1][i] > matrix[j][i] {\n banned = true;\n break;\n }\n if matrix[j - 1][i] < matrix[j][i] {\n local_ok[j - 1] = true;\n }\n }\n if banned {\n answ += 1;\n } else {\n for j in 0..99 {\n ok[j] |= local_ok[j];\n }\n }\n }\n\n println!(\"{}\", answ);\n}", "difficulty": "medium"} {"problem_id": "0811", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

A programming competition site AtCode provides algorithmic problems.\nEach problem is allocated a score based on its difficulty.\nCurrently, for each integer i between 1 and D (inclusive), there are p_i problems with a score of 100i points.\nThese p_1 + … + p_D problems are all of the problems available on AtCode.

\n

A user of AtCode has a value called total score.\nThe total score of a user is the sum of the following two elements:

\n
    \n
  • Base score: the sum of the scores of all problems solved by the user.
  • \n
  • Perfect bonuses: when a user solves all problems with a score of 100i points, he/she earns the perfect bonus of c_i points, aside from the base score (1 ≤ i ≤ D).
  • \n
\n

Takahashi, who is the new user of AtCode, has not solved any problem.\nHis objective is to have a total score of G or more points.\nAt least how many problems does he need to solve for this objective?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ D ≤ 10
  • \n
  • 1 ≤ p_i ≤ 100
  • \n
  • 100 ≤ c_i ≤ 10^6
  • \n
  • 100 ≤ G
  • \n
  • All values in input are integers.
  • \n
  • c_i and G are all multiples of 100.
  • \n
  • It is possible to have a total score of G or more points.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
D G\np_1 c_1\n:\np_D c_D\n
\n
\n
\n
\n
\n

Output

Print the minimum number of problems that needs to be solved in order to have a total score of G or more points. Note that this objective is always achievable (see Constraints).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 700\n3 500\n5 800\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

In this case, there are three problems each with 100 points and five problems each with 200 points. The perfect bonus for solving all the 100-point problems is 500 points, and the perfect bonus for solving all the 200-point problems is 800 points. Takahashi's objective is to have a total score of 700 points or more.

\n

One way to achieve this objective is to solve four 200-point problems and earn a base score of 800 points. However, if we solve three 100-point problems, we can earn the perfect bonus of 500 points in addition to the base score of 300 points, for a total score of 800 points, and we can achieve the objective with fewer problems.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2000\n3 500\n5 800\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n

This case is similar to Sample Input 1, but the Takahashi's objective this time is 2000 points or more. In this case, we inevitably need to solve all five 200-point problems, and by solving two 100-point problems additionally we have the total score of 2000 points.

\n
\n
\n
\n
\n
\n

Sample Input 3

2 400\n3 500\n5 800\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n

This case is again similar to Sample Input 1, but the Takahashi's objective this time is 400 points or more. In this case, we only need to solve two 200-point problems to achieve the objective.

\n
\n
\n
\n
\n
\n

Sample Input 4

5 25000\n20 1000\n40 1000\n50 1000\n30 1000\n1 1000\n
\n
\n
\n
\n
\n

Sample Output 4

66\n
\n

There is only one 500-point problem, but the perfect bonus can be earned even in such a case.

\n
\n
", "c_code": "int solution() {\n int d;\n int g;\n scanf(\"%d%d\", &d, &g);\n int p[d];\n int c[d];\n int ans = 1001;\n for (int i = 0; i < d; i++) {\n scanf(\"%d%d\", p + i, c + i);\n }\n for (int i = 0; i < (1 << d); i++) {\n int point = 0;\n int cnt = 0;\n for (int j = 0; j < d; j++) {\n if ((i & (1 << j)) != 0) {\n point += 100 * (j + 1) * p[j] + c[j];\n cnt += p[j];\n }\n }\n if (g <= point) {\n ans = (ans > cnt) ? cnt : ans;\n } else {\n int j = d - 1;\n while (((1 << j) & i) != 0 && j > 0) {\n j--;\n }\n if ((g - point - 1) / (100 * (j + 1)) + 1 < p[j]) {\n cnt += (g - point - 1) / (100 * (j + 1)) + 1;\n ans = (ans > cnt) ? cnt : ans;\n }\n }\n }\n printf(\"%d\\n\", ans);\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 d: usize = itr.next().unwrap().parse().unwrap();\n let g: usize = itr.next().unwrap().parse().unwrap();\n let mut p: Vec = vec![0; d];\n let mut c: Vec = vec![0; d];\n for i in 0..d {\n p[i] = itr.next().unwrap().parse().unwrap();\n c[i] = itr.next().unwrap().parse().unwrap();\n }\n\n let mut ans = 1 << 30;\n for bit in 0..1 << d {\n let mut cnt = 0;\n let mut tmp = 0;\n for i in 0..d {\n if bit >> i & 1 == 1 {\n tmp += p[i] * 100 * (i + 1) + c[i];\n cnt += p[i];\n }\n }\n if tmp >= g {\n ans = std::cmp::min(ans, cnt);\n } else {\n for i in (0..d).rev() {\n if bit >> i & 1 == 0 {\n if tmp + p[i] * 100 * (i + 1) < g {\n tmp += p[i] * 100 * (i + 1);\n cnt += p[i];\n } else {\n cnt += (g - tmp + 100 * i) / (100 * (i + 1));\n tmp = g + 1;\n break;\n }\n }\n }\n if tmp >= g {\n ans = std::cmp::min(ans, cnt);\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0812", "problem_description": "Qpwoeirut has taken up architecture and ambitiously decided to remodel his city.Qpwoeirut's city can be described as a row of $$$n$$$ buildings, the $$$i$$$-th ($$$1 \\le i \\le n$$$) of which is $$$h_i$$$ floors high. You can assume that the height of every floor in this problem is equal. Therefore, building $$$i$$$ is taller than the building $$$j$$$ if and only if the number of floors $$$h_i$$$ in building $$$i$$$ is larger than the number of floors $$$h_j$$$ in building $$$j$$$.Building $$$i$$$ is cool if it is taller than both building $$$i-1$$$ and building $$$i+1$$$ (and both of them exist). Note that neither the $$$1$$$-st nor the $$$n$$$-th building can be cool.To remodel the city, Qpwoeirut needs to maximize the number of cool buildings. To do this, Qpwoeirut can build additional floors on top of any of the buildings to make them taller. Note that he cannot remove already existing floors.Since building new floors is expensive, Qpwoeirut wants to minimize the number of floors he builds. Find the minimum number of floors Qpwoeirut needs to build in order to maximize the number of cool buildings.", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n long long h[100000] = {};\n\n int res = 0;\n\n long long ans1[100000] = {};\n long long ans2[100000] = {};\n\n res = scanf(\"%d\", &t);\n\n while (t > 0) {\n long long ans = 0LL;\n res = scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n res = scanf(\"%lld\", h + i);\n }\n ans1[0] = 0LL;\n for (int i = 1; i < n - 1; i++) {\n ans1[i] = ans1[i - 1];\n if (i % 2 == 1) {\n long long max = h[i - 1];\n if (max < h[i + 1]) {\n max = h[i + 1];\n }\n if (h[i] <= max) {\n ans1[i] += max - h[i] + 1LL;\n }\n }\n }\n ans1[n - 1] = ans1[n - 2];\n ans = ans1[n - 1];\n if (n % 2 == 0) {\n ans2[n - 1] = 0LL;\n for (int i = n - 2; i > 0; i--) {\n ans2[i] = ans2[i + 1];\n if (i % 2 == 0) {\n long long max = h[i - 1];\n if (max < h[i + 1]) {\n max = h[i + 1];\n }\n if (h[i] <= max) {\n ans2[i] += max - h[i] + 1LL;\n }\n }\n }\n ans2[0] = ans2[1];\n if (ans > ans2[0]) {\n ans = ans2[0];\n }\n for (int i = 1; i < n - 3; i += 2) {\n if (ans1[i] + ans2[i + 3] < ans) {\n ans = ans1[i] + ans2[i + 3];\n }\n }\n }\n printf(\"%lld\\n\", ans);\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n conio!(cin, cout);\n\n let t = scanf!(cin, usize);\n for _ in 1..=t {\n let n = scanf!(cin, usize);\n let h = scanf!(cin, [i64; n]);\n let mut dp = vec![(0, 0); n + 1];\n\n for i in 2..n {\n if dp[i - 1].0 == dp[i - 2].0 + 1 {\n dp[i].0 = dp[i - 1].0;\n dp[i].1 = dp[i - 1]\n .1\n .min(dp[i - 2].1 + (h[i].max(h[i - 2]) + 1 - h[i - 1]).max(0));\n }\n if dp[i - 1].0 > dp[i - 2].0 + 1 {\n dp[i].0 = dp[i - 1].0;\n dp[i].1 = dp[i - 1].1;\n }\n if dp[i - 1].0 < dp[i - 2].0 + 1 {\n dp[i].0 = dp[i - 2].0 + 1;\n dp[i].1 = dp[i - 2].1 + (h[i].max(h[i - 2]) + 1 - h[i - 1]).max(0);\n }\n }\n printf!(cout, \"{}\", dp[n - 1].1);\n printf!(cout, \"\\n\");\n }\n}", "difficulty": "easy"} {"problem_id": "0813", "problem_description": "Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside.The text is a string $$$s$$$ of lowercase Latin letters. She considers a string $$$t$$$ as hidden in string $$$s$$$ if $$$t$$$ exists as a subsequence of $$$s$$$ whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices $$$1$$$, $$$3$$$, and $$$5$$$, which form an arithmetic progression with a common difference of $$$2$$$. Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of $$$S$$$ are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message!For example, in the string aaabb, a is hidden $$$3$$$ times, b is hidden $$$2$$$ times, ab is hidden $$$6$$$ times, aa is hidden $$$3$$$ times, bb is hidden $$$1$$$ time, aab is hidden $$$2$$$ times, aaa is hidden $$$1$$$ time, abb is hidden $$$1$$$ time, aaab is hidden $$$1$$$ time, aabb is hidden $$$1$$$ time, and aaabb is hidden $$$1$$$ time. The number of occurrences of the secret message is $$$6$$$.", "c_code": "int solution() {\n char s[100001];\n scanf(\"%s\", s);\n int n = strlen(s);\n long long int a[27][26] = {0};\n for (int i = 0; i < n; i++) {\n for (int o = 0; o < 26; o++) {\n a[o][s[i] - 'a'] += a[26][o];\n }\n a[26][s[i] - 'a']++;\n }\n long long int m = 0;\n for (int i = 0; i < 27; i++) {\n for (int o = 0; o < 26; o++) {\n if (m < a[i][o]) {\n m = a[i][o];\n }\n }\n }\n printf(\"%lld\\n\", m);\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 c1 = [0u64; 26];\n let mut c2 = [[0u64; 26]; 26];\n for i in arr.get(0).unwrap().chars() {\n let a = i as u32 - 'a' as u32;\n for j in 0..26 {\n c2[j][a as usize] += c1[j];\n }\n c1[a as usize] += 1;\n }\n let b = *c1.iter().max().unwrap();\n let c = c2.iter().map(|x| *x.iter().max().unwrap()).max().unwrap();\n let ans = cmp::max(b, c);\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0814", "problem_description": "You are given a decimal representation of an integer $$$x$$$ without leading zeros.You have to perform the following reduction on it exactly once: take two neighboring digits in $$$x$$$ and replace them with their sum without leading zeros (if the sum is $$$0$$$, it's represented as a single $$$0$$$).For example, if $$$x = 10057$$$, the possible reductions are: choose the first and the second digits $$$1$$$ and $$$0$$$, replace them with $$$1+0=1$$$; the result is $$$1057$$$; choose the second and the third digits $$$0$$$ and $$$0$$$, replace them with $$$0+0=0$$$; the result is also $$$1057$$$; choose the third and the fourth digits $$$0$$$ and $$$5$$$, replace them with $$$0+5=5$$$; the result is still $$$1057$$$; choose the fourth and the fifth digits $$$5$$$ and $$$7$$$, replace them with $$$5+7=12$$$; the result is $$$10012$$$. What's the largest number that can be obtained?", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n for (int n1 = 0; n1 < n; n1++) {\n\n char *s = malloc(200010);\n scanf(\"\\n%s\", s);\n int a = 0;\n for (int s1 = 1; s1 <= 200001; s1++) {\n if (s[s1] == '\\0') {\n break;\n }\n if ((int)s[s1 - 1] + (int)(s[s1]) - 96 >= 10) {\n a = s1;\n }\n }\n if (a != 0) {\n int t = (int)s[a - 1] + (int)(s[a]) - 96;\n s[a - 1] = (char)((t / 10) + 48);\n s[a] = (char)((t % 10) + 48);\n printf(\"%s\", s);\n } else {\n int t = (int)s[0] + (int)(s[1]) - 96;\n printf(\"%d%s\", t, s + 2);\n }\n free(s);\n if (n1 != n - 1) {\n printf(\"\\n\");\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 cs: Vec = lines.next().unwrap().unwrap().chars().collect();\n let l = cs.len();\n let mut d = None;\n for i in (1..l).rev() {\n let a = (cs[i] as usize - '0' as usize) + (cs[i - 1] as usize - '0' as usize);\n if a >= 10 {\n d = Some(i);\n break;\n }\n }\n match d {\n None => {\n print!(\n \"{}\",\n (cs[0] as usize - '0' as usize) + (cs[1] as usize) - '0' as usize\n );\n println!(\"{}\", cs[2..l].iter().cloned().collect::());\n }\n Some(e) => {\n print!(\"{}\", cs[0..(e - 1)].iter().cloned().collect::());\n print!(\n \"{}\",\n (cs[e - 1] as usize - '0' as usize) + (cs[e] as usize) - '0' as usize\n );\n println!(\"{}\", cs[(e + 1)..l].iter().cloned().collect::());\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0815", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

\n

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \\times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

\n

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

\n

Compute this count to help Takahashi.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • S consists of digits.
  • \n
  • |S| = N
  • \n
  • 2 \\leq P \\leq 10000
  • \n
  • P is a prime number.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N P\nS\n
\n
\n
\n
\n
\n

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3\n3543\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

\n
    \n
  • \n

    3: divisible by 3.

    \n
  • \n
  • \n

    35: not divisible by 3.

    \n
  • \n
  • \n

    354: divisible by 3.

    \n
  • \n
  • \n

    3543: divisible by 3.

    \n
  • \n
  • \n

    5: not divisible by 3.

    \n
  • \n
  • \n

    54: divisible by 3.

    \n
  • \n
  • \n

    543: divisible by 3.

    \n
  • \n
  • \n

    4: not divisible by 3.

    \n
  • \n
  • \n

    43: not divisible by 3.

    \n
  • \n
  • \n

    3: divisible by 3.

    \n
  • \n
\n

Six of these are divisible by 3, so print 6.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 2\n2020\n
\n
\n
\n
\n
\n

Sample Output 2

10\n
\n

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

\n

Note that substrings beginning with a 0 also count.

\n
\n
\n
\n
\n
\n

Sample Input 3

20 11\n33883322005544116655\n
\n
\n
\n
\n
\n

Sample Output 3

68\n
\n
\n
", "c_code": "int solution() {\n int n;\n int p;\n scanf(\"%d%d\", &n, &p);\n char s[n + 5];\n scanf(\"%s\", s);\n long ans = 0;\n if (10 % p == 0) {\n for (int i = 0; i < n; i++) {\n if ((s[i] - '0') % p == 0) {\n ans += i + 1;\n }\n }\n } else {\n long dig = 1;\n long r[p];\n long t = 0;\n for (int i = 0; i < p; i++) {\n r[i] = 0;\n }\n r[0]++;\n for (int i = n; i > 0; i--) {\n t += (s[i - 1] - '0') * dig;\n t %= p;\n r[t]++;\n dig *= 10;\n dig %= p;\n }\n for (int i = 0; i < p; i++) {\n ans += (r[i] * (r[i] - 1)) / 2;\n }\n }\n printf(\"%ld\\n\", ans);\n return 0;\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 p: usize = itr.next().unwrap().parse().unwrap();\n let mut s: Vec = itr\n .next()\n .unwrap()\n .chars()\n .map(|c| c.to_digit(10).unwrap() as usize)\n .collect();\n\n let mut ans = 0;\n if p == 2 {\n for i in 0..n {\n if s[i].is_multiple_of(2) {\n ans += i + 1;\n }\n }\n } else if p == 5 {\n for i in 0..n {\n if s[i].is_multiple_of(5) {\n ans += i + 1;\n }\n }\n } else {\n s.reverse();\n let mut cnt: Vec = vec![0; n + 1];\n let mut memo: Vec = vec![0; p + 1];\n let mut ten = 1;\n for i in 0..n {\n cnt[i + 1] = (cnt[i] + s[i] * ten) % p;\n ten = ten * 10 % p;\n }\n for i in 0..n + 1 {\n ans += memo[cnt[i]];\n memo[cnt[i]] += 1;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0816", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are given an undirected connected weighted graph with N vertices and M edges that contains neither self-loops nor double edges.
\nThe i-th (1≤i≤M) edge connects vertex a_i and vertex b_i with a distance of c_i.
\nHere, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).
\nA connected graph is a graph where there is a path between every pair of different vertices.
\nFind the number of the edges that are not contained in any shortest path between any pair of different vertices.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≤N≤100
  • \n
  • N-1≤M≤min(N(N-1)/2,1000)
  • \n
  • 1≤a_i,b_i≤N
  • \n
  • 1≤c_i≤1000
  • \n
  • c_i is an integer.
  • \n
  • The given graph contains neither self-loops nor double edges.
  • \n
  • The given graph is connected.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N M  \na_1 b_1 c_1  \na_2 b_2 c_2\n:  \na_M b_M c_M  \n
\n
\n
\n
\n
\n

Output

Print the number of the edges in the graph that are not contained in any shortest path between any pair of different vertices.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n1 2 1\n1 3 1\n2 3 3\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

In the given graph, the shortest paths between all pairs of different vertices are as follows:

\n
    \n
  • The shortest path from vertex 1 to vertex 2 is: vertex 1 → vertex 2, with the length of 1.
  • \n
  • The shortest path from vertex 1 to vertex 3 is: vertex 1 → vertex 3, with the length of 1.
  • \n
  • The shortest path from vertex 2 to vertex 1 is: vertex 2 → vertex 1, with the length of 1.
  • \n
  • The shortest path from vertex 2 to vertex 3 is: vertex 2 → vertex 1 → vertex 3, with the length of 2.
  • \n
  • The shortest path from vertex 3 to vertex 1 is: vertex 3 → vertex 1, with the length of 1.
  • \n
  • The shortest path from vertex 3 to vertex 2 is: vertex 3 → vertex 1 → vertex 2, with the length of 2.
  • \n
\n

Thus, the only edge that is not contained in any shortest path, is the edge of length 3 connecting vertex 2 and vertex 3, hence the output should be 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 2\n1 2 1\n2 3 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Every edge is contained in some shortest path between some pair of different vertices.

\n
\n
", "c_code": "int solution(void) {\n\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n\n int pass[101][101] = {0};\n int x[m];\n int y[m];\n int z[m];\n\n for (int i = 0; i <= n; i++) {\n for (int j = 0; j <= n; j++) {\n if (i != j) {\n pass[i][j] = -1;\n }\n }\n }\n\n for (int i = 0; i < m; i++) {\n scanf(\"%d %d %d\", &x[i], &y[i], &z[i]);\n pass[x[i]][y[i]] = z[i];\n pass[y[i]][x[i]] = z[i];\n }\n\n int count = 0;\n int flag = 0;\n\n while (1) {\n flag = 0;\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= n; j++) {\n for (int k = 1; k <= n; k++) {\n if (pass[i][k] == -1 || pass[k][j] == -1) {\n continue;\n }\n if (pass[i][j] == -1 || pass[i][k] + pass[k][j] < pass[i][j]) {\n pass[i][j] = pass[i][k] + pass[k][j];\n pass[j][i] = pass[i][j];\n flag = 1;\n }\n }\n }\n }\n if (flag == 0) {\n break;\n }\n }\n for (int i = 0; i < m; i++) {\n flag = 0;\n for (int j = 0; j <= n; j++) {\n if (pass[j][x[i]] + z[i] == pass[j][y[i]]) {\n flag = 1;\n break;\n }\n }\n if (flag == 1) {\n count++;\n }\n }\n\n printf(\"%d\\n\", m - count);\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 m: usize = itr.next().unwrap().parse().unwrap();\n let mut g = vec![vec![1 << 30; n]; n];\n let mut edges = Vec::new();\n for _ in 0..m {\n let a: usize = itr.next().unwrap().parse::().unwrap() - 1;\n let b: usize = itr.next().unwrap().parse::().unwrap() - 1;\n let c: usize = itr.next().unwrap().parse().unwrap();\n g[a][b] = c;\n g[b][a] = c;\n edges.push((a, b, c));\n }\n for i in 0..n {\n g[i][i] = 0;\n }\n for k in 0..n {\n for i in 0..n {\n for j in 0..n {\n g[i][j] = min(g[i][j], g[i][k] + g[k][j]);\n }\n }\n }\n\n let mut ans = 0;\n for i in 0..m {\n if g[edges[i].0][edges[i].1] != edges[i].2 {\n ans += 1;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0817", "problem_description": "Pay attention to the non-standard memory limit in this problem.In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution.The array $$$a=[a_1, a_2, \\ldots, a_n]$$$ ($$$1 \\le a_i \\le n$$$) is given. Its element $$$a_i$$$ is called special if there exists a pair of indices $$$l$$$ and $$$r$$$ ($$$1 \\le l < r \\le n$$$) such that $$$a_i = a_l + a_{l+1} + \\ldots + a_r$$$. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not).Print the number of special elements of the given array $$$a$$$.For example, if $$$n=9$$$ and $$$a=[3,1,4,1,5,9,2,6,5]$$$, then the answer is $$$5$$$: $$$a_3=4$$$ is a special element, since $$$a_3=4=a_1+a_2=3+1$$$; $$$a_5=5$$$ is a special element, since $$$a_5=5=a_2+a_3=1+4$$$; $$$a_6=9$$$ is a special element, since $$$a_6=9=a_1+a_2+a_3+a_4=3+1+4+1$$$; $$$a_8=6$$$ is a special element, since $$$a_8=6=a_2+a_3+a_4=1+4+1$$$; $$$a_9=5$$$ is a special element, since $$$a_9=5=a_2+a_3=1+4$$$. Please note that some of the elements of the array $$$a$$$ may be equal — if several elements are equal and special, then all of them should be counted in the answer.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int a[80005][3];\n int b[80005];\n int n;\n int sum = 0;\n for (int i = 0; i < 8000; i++) {\n for (int j = 0; j < 3; j++) {\n a[i][j] = 0;\n }\n }\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i][0]);\n b[i] = a[i][0];\n a[a[i][0]][2]++;\n }\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n b[i] += b[j];\n a[b[i]][1] = 1;\n if (b[i] > n) {\n break;\n }\n }\n }\n for (int i = 0; i <= n; i++) {\n if (a[i][2] > 0 && a[i][1] > 0) {\n sum += a[i][2];\n }\n }\n printf(\"%d\\n\", sum);\n }\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input)?;\n let n = input.trim().parse().unwrap();\n for _ in 0..n {\n input.clear();\n io::stdin().read_line(&mut input)?;\n\n input.clear();\n io::stdin().read_line(&mut input)?;\n let numbers: Vec = input\n .trim()\n .split(\" \")\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let mut special = 0;\n for (i, target) in numbers.clone().into_iter().enumerate() {\n let mut cum = 0;\n let mut vals = 0;\n for (j, current) in numbers.clone().into_iter().enumerate() {\n if i == j {\n cum = 0;\n vals = 0;\n continue;\n }\n cum += current;\n vals += 1;\n while cum > target {\n cum -= numbers[j + 1 - vals];\n vals -= 1;\n }\n if cum == target && vals > 1 {\n special += 1;\n break;\n }\n }\n }\n\n println!(\"{}\", special);\n }\n\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "0818", "problem_description": "Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int e = 0;\n scanf(\"%d%d%d\", &n, &a, &b);\n\n if (a + b <= n) {\n printf(\"1 \");\n } else {\n c = a + b;\n d = n - 1;\n e = c - d;\n if (e >= n) {\n printf(\"%d \", n);\n } else {\n printf(\"%d \", e);\n }\n }\n\n if (a + b <= n) {\n printf(\"%d\\n\", a + b - 1);\n } else {\n printf(\"%d\\n\", n);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read line\");\n buf.trim().parse::().unwrap()\n };\n for _ in 0..t {\n let stuff = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read\");\n let nums: Vec = buf\n .trim()\n .split_ascii_whitespace()\n .map(|c| c.parse().unwrap())\n .collect();\n (nums[0], nums[1], nums[2])\n };\n let best_place = max(1, min(stuff.0, stuff.1 + stuff.2 - stuff.0 + 1));\n let worst_place = min(stuff.0, stuff.1 + stuff.2 - 1);\n println!(\"{} {}\", best_place, worst_place);\n }\n}", "difficulty": "medium"} {"problem_id": "0819", "problem_description": "You are given an array $$$a_1, a_2, \\dots, a_n$$$, which is sorted in non-descending order. You decided to perform the following steps to create array $$$b_1, b_2, \\dots, b_n$$$: Create an array $$$d$$$ consisting of $$$n$$$ arbitrary non-negative integers. Set $$$b_i = a_i + d_i$$$ for each $$$b_i$$$. Sort the array $$$b$$$ in non-descending order. You are given the resulting array $$$b$$$. For each index $$$i$$$, calculate what is the minimum and maximum possible value of $$$d_i$$$ you can choose in order to get the given array $$$b$$$.Note that the minimum (maximum) $$$d_i$$$-s are independent of each other, i. e. they can be obtained from different possible arrays $$$d$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n if (n == 1) {\n long long int n1;\n long long int n2;\n scanf(\"%lld %lld\", &n1, &n2);\n printf(\"%lld\\n%lld\\n\", n2 - n1, n2 - n1);\n continue;\n }\n long long int a[n];\n long long int b[n];\n long long int min[n];\n long long int max[n];\n long long int val = 0;\n long long int ind = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &b[i]);\n }\n for (int i = 0, k = 0; i < n;) {\n if (b[k] >= a[i]) {\n min[i] = b[k] - a[i];\n i++;\n } else {\n k++;\n }\n }\n val = b[n - 1];\n for (int i = n - 1, k = n - 2; i > 0, k >= 0; i--, k--) {\n max[i] = val - a[i];\n if (b[k] < a[i]) {\n val = b[i - 1];\n }\n }\n max[0] = val - a[0];\n for (int i = 0; i < n; i++) {\n printf(\"%lld \", min[i]);\n }\n printf(\"\\n\");\n for (int i = 0; i < n; i++) {\n printf(\"%lld \", max[i]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n for _tests in 0..t {\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 A: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n let B: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let mut ind_vec = vec![0; n];\n let mut ans1 = vec![0; n];\n let mut ans2 = vec![0; n];\n\n let mut ind = 0;\n\n for i in 0..n {\n while B[ind] < A[i] {\n ind += 1;\n }\n ans1[i] = B[ind] - A[i];\n ind_vec[i] = ind;\n }\n\n let mut maxind = n - 1;\n\n for i in 0..n {\n let ix = n - 1 - i;\n\n ans2[ix] = B[maxind] - A[ix];\n\n if ind_vec[ix] == ix && ix >= 1 {\n maxind = ix - 1;\n }\n }\n\n println!(\n \"{}\",\n ans1.iter()\n .map(std::string::ToString::to_string)\n .collect::>()\n .join(\" \")\n );\n\n println!(\n \"{}\",\n ans2.iter()\n .map(std::string::ToString::to_string)\n .collect::>()\n .join(\" \")\n );\n }\n}", "difficulty": "easy"} {"problem_id": "0820", "problem_description": "You have a card deck of $$$n$$$ cards, numbered from top to bottom, i. e. the top card has index $$$1$$$ and bottom card — index $$$n$$$. Each card has its color: the $$$i$$$-th card has color $$$a_i$$$.You should process $$$q$$$ queries. The $$$j$$$-th query is described by integer $$$t_j$$$. For each query you should: find the highest card in the deck with color $$$t_j$$$, i. e. the card with minimum index; print the position of the card you found; take the card and place it on top of the deck.", "c_code": "int solution() {\n int n;\n int q;\n scanf(\"%d %d\", &n, &q);\n int sol[q];\n int arrn[n];\n int arrq[q];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arrn[i]);\n }\n for (int i = 0; i < q; i++) {\n scanf(\"%d\", &arrq[i]);\n }\n for (int i = 0; i < q; i++) {\n int f = arrq[i];\n for (int j = 0; j < n; j++) {\n if (arrn[j] == f) {\n sol[i] = j + 1;\n for (int k = 0; k < j; k++) {\n int temp = arrn[j];\n arrn[j] = arrn[k];\n arrn[k] = temp;\n }\n break;\n }\n }\n }\n for (int i = 0; i < q; i++) {\n printf(\"%d \", sol[i]);\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let n = words[0];\n let _q = words[1];\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 t: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut deck = [0; 51];\n for idx in 0..n {\n if deck[a[idx as usize] as usize] == 0 {\n deck[a[idx as usize] as usize] = idx + 1;\n }\n }\n\n for color in t {\n print!(\"{} \", deck[color as usize]);\n for idx in 1..51 {\n if deck[idx as usize] < deck[color as usize] {\n deck[idx as usize] += 1;\n }\n }\n deck[color as usize] = 1;\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "0821", "problem_description": "The sequence of $$$m$$$ integers is called the permutation if it contains all integers from $$$1$$$ to $$$m$$$ exactly once. The number $$$m$$$ is called the length of the permutation.Dreamoon has two permutations $$$p_1$$$ and $$$p_2$$$ of non-zero lengths $$$l_1$$$ and $$$l_2$$$.Now Dreamoon concatenates these two permutations into another sequence $$$a$$$ of length $$$l_1 + l_2$$$. First $$$l_1$$$ elements of $$$a$$$ is the permutation $$$p_1$$$ and next $$$l_2$$$ elements of $$$a$$$ is the permutation $$$p_2$$$. You are given the sequence $$$a$$$, and you need to find two permutations $$$p_1$$$ and $$$p_2$$$. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)", "c_code": "int solution() {\n long long int t;\n scanf(\"%lld\", &t);\n\n do {\n long long int n;\n scanf(\"%lld\", &n);\n long long int a[n];\n long long int i;\n long long int st = 0;\n long long int freq2[n];\n for (int i = 0; i < n; ++i) {\n freq2[i] = 0;\n }\n for (i = 0; i < n; ++i) {\n scanf(\"%lld\", &a[i]);\n st += a[i];\n freq2[a[i] - 1] += 1;\n }\n long long int sp = 0;\n long long int alfa = 0;\n long long int beta = 0;\n long long int r = 0;\n long long int uno[n];\n long long int dos[n];\n long long int freq[n];\n\n for (i = 0; i < n; ++i) {\n freq[i] = 0;\n }\n\n for (i = 0; i < n; ++i) {\n\n freq[a[i] - 1] += 1;\n freq2[a[i] - 1] -= 1;\n\n alfa = (i + 1) * (i + 2) / 2;\n beta = (n - i - 1) * (n - i) / 2;\n sp += a[i];\n if (sp == alfa && st - sp == beta) {\n long long int cen = 0;\n for (long long int j = 0; j < n; ++j) {\n if (freq[j] > 1 || freq2[j] > 1) {\n cen = 1;\n }\n }\n\n if (cen == 0) {\n uno[r] = i + 1;\n dos[r] = n - (i + 1);\n ++r;\n }\n }\n }\n\n printf(\"%lld\\n\", r);\n for (i = 0; i < r; ++i) {\n printf(\"%lld %lld\\n\", uno[i], dos[i]);\n }\n\n --t;\n } while (t != 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 n: 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 .map(|x: usize| x - 1)\n .collect();\n let mut p: usize = 0;\n let mut bs: Vec = vec![false; n];\n let mut ixs: Vec = Vec::new();\n for i in 0..n {\n bs[xs[i]] = true;\n while p < n && bs[p] {\n p += 1;\n }\n if i + 1 == p {\n ixs.push(p);\n }\n }\n let mut p: usize = 0;\n let mut bs: Vec = vec![false; n];\n let mut jxs: Vec = Vec::new();\n for i in (0..n).rev() {\n bs[xs[i]] = true;\n while p < n && bs[p] {\n p += 1;\n }\n if n - i == p {\n jxs.push(p);\n }\n }\n jxs.reverse();\n let mut i: usize = 0;\n let mut j: usize = 0;\n let mut ans: Vec<(usize, usize)> = Vec::new();\n while i < ixs.len() && j < jxs.len() {\n if ixs[i] + jxs[j] == n {\n ans.push((ixs[i], jxs[j]));\n i += 1;\n j += 1;\n } else if ixs[i] + jxs[j] < n {\n i += 1;\n } else {\n j += 1;\n }\n }\n println!(\"{}\", ans.len());\n for (i, j) in ans {\n println!(\"{} {}\", i, j);\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0822", "problem_description": "Alice guesses the strings that Bob made for her.At first, Bob came up with the secret string $$$a$$$ consisting of lowercase English letters. The string $$$a$$$ has a length of $$$2$$$ or more characters. Then, from string $$$a$$$ he builds a new string $$$b$$$ and offers Alice the string $$$b$$$ so that she can guess the string $$$a$$$.Bob builds $$$b$$$ from $$$a$$$ as follows: he writes all the substrings of length $$$2$$$ of the string $$$a$$$ in the order from left to right, and then joins them in the same order into the string $$$b$$$.For example, if Bob came up with the string $$$a$$$=\"abac\", then all the substrings of length $$$2$$$ of the string $$$a$$$ are: \"ab\", \"ba\", \"ac\". Therefore, the string $$$b$$$=\"abbaac\".You are given the string $$$b$$$. Help Alice to guess the string $$$a$$$ that Bob came up with. It is guaranteed that $$$b$$$ was built according to the algorithm given above. It can be proved that the answer to the problem is unique.", "c_code": "int solution() {\n int t;\n scanf(\"%i\", &t);\n getchar();\n while (t--) {\n char str[101];\n scanf(\"%s\", str);\n int len = strlen(str);\n for (int i = 0; i < len; i++) {\n if (i == 0 || i % 2 == 1) {\n printf(\"%c\", str[i]);\n }\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n\n let mut iterator = stdin.lock().lines();\n let mut line: String;\n let mut original: Vec = Vec::new();\n\n line = iterator.next().unwrap().unwrap();\n let t: i32 = line.parse().unwrap();\n\n for _ in 0..t {\n original.clear();\n line = iterator.next().unwrap().unwrap();\n for (i, c) in line.chars().enumerate() {\n if i <= 1 || i % 2 == 1 {\n original.push(c.to_string());\n }\n }\n println!(\"{}\", original.join(\"\"));\n }\n}", "difficulty": "easy"} {"problem_id": "0823", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi has two positive integers A and B.

\n

It is known that A plus B equals N.\nFind the minimum possible value of \"the sum of the digits of A\" plus \"the sum of the digits of B\" (in base 10).

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ N ≤ 10^5
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible value of \"the sum of the digits of A\" plus \"the sum of the digits of B\".

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

15\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

When A=2 and B=13, the sums of their digits are 2 and 4, which minimizes the value in question.

\n
\n
\n
\n
\n
\n

Sample Input 2

100000\n
\n
\n
\n
\n
\n

Sample Output 2

10\n
\n
\n
", "c_code": "int solution(void) {\n char n[100000];\n scanf(\"%s\", n);\n\n int ans = 0;\n for (int i = 0; n[i] != '\\0'; i++) {\n ans += (int)n[i] - 48;\n }\n if (ans == 1) {\n ans = 10;\n }\n printf(\"%d\\n\", ans);\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\n let mut m = n;\n let mut ans = 0;\n while m > 0 {\n ans += m % 10;\n m /= 10;\n }\n\n let mut f = false;\n for i in 1..6 {\n if n == 10usize.pow(i) {\n f = true;\n }\n }\n\n if f {\n println!(\"10\");\n } else {\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0824", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

\n

We have a tree with N vertices numbered 1 to N.\nThe i-th edge in this tree connects Vertex a_i and Vertex b_i.
\nConsider painting each of these edges white or black. There are 2^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following M restrictions?

\n
    \n
  • The i-th (1 \\leq i \\leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black.
  • \n
\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 2 \\leq N \\leq 50
  • \n
  • 1 \\leq a_i,b_i \\leq N
  • \n
  • The graph given in input is a tree.
  • \n
  • 1 \\leq M \\leq \\min(20,\\frac{N(N-1)}{2})
  • \n
  • 1 \\leq u_i < v_i \\leq N
  • \n
  • If i \\not= j, either u_i \\not=u_j or v_i\\not=v_j
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\na_1 b_1\n:\na_{N-1} b_{N-1}\nM\nu_1 v_1\n:\nu_M v_M\n
\n
\n
\n
\n
\n

Output

\n

Print the number of ways to paint the edges that satisfy all of the M conditions.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 2\n2 3\n1\n1 3\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The tree in this input is shown below:\n
\n
\n\"Figure\"\n
\n
\nAll of the M restrictions will be satisfied if Edge 1 and 2 are respectively painted (white, black), (black, white), or (black, black), so the answer is 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n1 2\n1\n1 2\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

The tree in this input is shown below:\n
\n
\n\"Figure\"\n
\n
\nAll of the M restrictions will be satisfied only if Edge 1 is painted black, so the answer is 1.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n1 2\n3 2\n3 4\n5 3\n3\n1 3\n2 4\n2 5\n
\n
\n
\n
\n
\n

Sample Output 3

9\n
\n

The tree in this input is shown below:\n
\n
\n\"Figure\"\n
\n

\n
\n
\n
\n
\n
\n

Sample Input 4

8\n1 2\n2 3\n4 3\n2 5\n6 3\n6 7\n8 6\n5\n2 7\n3 5\n1 6\n2 8\n7 8\n
\n
\n
\n
\n
\n

Sample Output 4

62\n
\n

The tree in this input is shown below:\n
\n
\n\"Figure\"\n
\n

\n
\n
", "c_code": "int solution() {\n int i;\n int u;\n int w;\n int N;\n int M;\n int s[21];\n int t[21];\n int adj[51][51] = {};\n scanf(\"%d\", &N);\n for (i = 1; i < N; i++) {\n scanf(\"%d %d\", &u, &w);\n adj[u][w] = i;\n adj[w][u] = i;\n }\n scanf(\"%d\", &M);\n for (i = 0; i < M; i++) {\n scanf(\"%d %d\", &(s[i]), &(t[i]));\n }\n\n int prev[51];\n int q[51];\n int head;\n int tail;\n long long bit[51] = {1};\n long long P[21] = {};\n for (i = 1; i <= 50; i++) {\n bit[i] = bit[i - 1] << 1;\n }\n for (i = 0; i < M; i++) {\n for (u = 1; u <= N; u++) {\n prev[u] = -1;\n }\n q[0] = s[i];\n prev[s[i]] = s[i];\n for (head = 0, tail = 1; head < tail; head++) {\n u = q[head];\n for (w = 1; w <= N; w++) {\n if (adj[u][w] == 0) {\n continue;\n }\n if (w == t[i]) {\n break;\n }\n if (prev[w] == -1) {\n prev[w] = u;\n q[tail++] = w;\n }\n }\n if (w <= N) {\n break;\n }\n }\n for (; w != u; w = u, u = prev[u]) {\n P[i] |= bit[adj[u][w]];\n }\n }\n\n int j;\n int sum[2];\n long long n;\n long long ans = (long long)1 << N - 1;\n long long white;\n for (n = 1; n < bit[M]; n++) {\n white = 0;\n for (i = 0; i < M; i++) {\n if ((n | bit[i]) == n) {\n white |= P[i];\n }\n }\n for (j = 1, sum[0] = 0; j < N; j++) {\n sum[0] += white / bit[j] % 2;\n }\n for (i = 0, sum[1] = 0; i < M; i++) {\n sum[1] += n / bit[i] % 2;\n }\n if (sum[1] % 2 == 1) {\n ans -= bit[N - 1 - sum[0]];\n } else {\n ans += bit[N - 1 - sum[0]];\n }\n }\n\n printf(\"%lld\\n\", ans);\n fflush(stdout);\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 (a, b): (Vec, Vec) = {\n let (mut a, mut b) = (vec![], vec![]);\n for _ in 0..(N - 1) {\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 }\n (a, b)\n };\n let M: 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 (u, v): (Vec, Vec) = {\n let (mut u, mut v) = (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 u.push(iter.next().unwrap().parse().unwrap());\n v.push(iter.next().unwrap().parse().unwrap());\n }\n (u, v)\n };\n\n let G = {\n let mut G = vec![std::collections::HashMap::new(); N + 1];\n for i in 0..(N - 1) {\n G[a[i]].insert(b[i], i);\n G[b[i]].insert(a[i], i);\n }\n G\n };\n\n let mut dp = vec![std::collections::HashSet::new(); M];\n for i in 0..M {\n let mut prev = vec![(0, 0); N + 1];\n let mut q = std::collections::VecDeque::new();\n q.push_back(u[i]);\n while let Some(j) = q.pop_front() {\n for (&x, &e) in &G[j] {\n if prev[x].0 == 0 {\n prev[x] = (j, e);\n q.push_back(x);\n }\n }\n }\n let mut j = v[i];\n while j != u[i] {\n dp[i].insert(prev[j].1);\n j = prev[j].0;\n }\n }\n\n let mut res = 0i64;\n for i in 1..(1 << M) {\n let mut x = 0;\n let mut used = vec![false; N + 1];\n let mut s = 0;\n for j in 0..M {\n if (i >> j) & 1 == 1 {\n x += 1;\n for &y in &dp[j] {\n if !used[y] {\n s += 1;\n }\n used[y] = true;\n }\n }\n }\n let d = 1i64 << (N - 1 - s);\n\n if x % 2 == 1 {\n res += d;\n } else {\n res -= d;\n }\n }\n let ans = (1i64 << (N - 1)) - res;\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0825", "problem_description": "Given three distinct integers $$$a$$$, $$$b$$$, and $$$c$$$, find the medium number between all of them.The medium number is the number that is neither the minimum nor the maximum of the given three numbers. For example, the median of $$$5,2,6$$$ is $$$5$$$, since the minimum is $$$2$$$ and the maximum is $$$6$$$.", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int c = 0;\n int m = 0;\n scanf(\"%d\", &m);\n for (int i = 0; i < m; i++) {\n scanf(\"%d%d%d\", &a, &b, &c);\n if ((a < b && b < c) || (c < b && b < a)) {\n printf(\"%d\\n\", b);\n }\n if ((a < c && c < b) || (b < c && c < a)) {\n printf(\"%d\\n\", c);\n }\n if ((b < a && a < c) || (c < a && a < b)) {\n printf(\"%d\\n\", a);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"\");\n let t: i32 = line.trim().parse().expect(\"\");\n for _ in 0..t {\n let reader = io::stdin();\n let mut numbers: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|s| s.parse().unwrap())\n .collect();\n numbers.sort();\n println!(\"{:?}\", numbers[1]);\n }\n}", "difficulty": "hard"} {"problem_id": "0826", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int a[10010];\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int place = -1;\n\n for (int i = 1; i <= n; i++) {\n if (a[i] % 2 == 0) {\n place = i;\n break;\n }\n }\n if (place != -1) {\n printf(\"1\\n%d\\n\", place);\n } else if (place == -1 && n == 1) {\n printf(\"-1\\n\");\n } else {\n printf(\"2\\n1 2\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut quantity = String::new();\n stdin().read_line(&mut quantity).unwrap();\n let quantity = quantity.trim().parse::().unwrap();\n for _i in 0..quantity {\n let mut arr = String::new();\n stdin().read_line(&mut arr).unwrap();\n arr = String::new();\n stdin().read_line(&mut arr).unwrap();\n let arr: Vec<_> = arr\n .trim()\n .split(' ')\n .map(|i| i.parse::().unwrap())\n .collect();\n if arr[0] & 1 == 0 {\n println!(\"1\\n1\");\n continue;\n }\n if arr.len() != 1 {\n if arr[1] & 1 == 0 {\n println!(\"1\\n2\")\n } else {\n println!(\"2\\n1 2\")\n }\n } else {\n println!(\"-1\")\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0827", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have a string S of length N consisting of R, G, and B.

\n

Find the number of triples (i,~j,~k)~(1 \\leq i < j < k \\leq N) that satisfy both of the following conditions:

\n
    \n
  • S_i \\neq S_j, S_i \\neq S_k, and S_j \\neq S_k.
  • \n
  • j - i \\neq k - j.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 4000
  • \n
  • S is a string of length N consisting of R, G, and B.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the number of triplets in question.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\nRRGB\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

\n
\n
\n
\n
\n
\n

Sample Input 2

39\nRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB\n
\n
\n
\n
\n
\n

Sample Output 2

1800\n
\n
\n
", "c_code": "int solution() {\n int n;\n char s[4000];\n long long int sum = 0;\n scanf(\"%d\", &n);\n scanf(\"%s\", s);\n long long int rnum = 0;\n long long int gnum = 0;\n long long int bnum = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == 'R') {\n rnum++;\n continue;\n }\n if (s[i] == 'G') {\n gnum++;\n continue;\n }\n bnum++;\n continue;\n }\n sum = rnum * gnum * bnum;\n for (int i = 1; i < n - 1; i++) {\n for (int j = 1; i - j >= 0 && i + j < n; j++) {\n if (s[i] != s[i - j] && s[i] != s[i + j] && s[i - j] != s[i + j]) {\n sum--;\n }\n }\n }\n printf(\"%lld\", sum);\n return 0;\n}", "rust_code": "fn solution() {\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 s: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end()\n .chars()\n .map(|c| {\n if c == 'R' {\n 0\n } else if c == 'G' {\n 1\n } else {\n 2\n }\n })\n .collect()\n };\n\n let mut count = vec![vec![0; 3]; n + 1];\n for i in (0..n).rev() {\n for j in 0..3 {\n count[i][j] += count[i + 1][j];\n }\n count[i][s[i]] += 1;\n }\n\n let mut ans = 0usize;\n for i in 0..(n - 2) {\n for j in (i + 1)..(n - 1) {\n if s[i] == s[j] {\n continue;\n }\n ans += count[j + 1][3 - s[i] - s[j]];\n if j - i + j < n && s[j - i + j] == 3 - s[i] - s[j] {\n ans -= 1;\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0828", "problem_description": "Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself. To make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning \"(\" into \")\" or vice versa) isn't allowed. We remind that bracket sequence $$$s$$$ is called correct if: $$$s$$$ is empty; $$$s$$$ is equal to \"($$$t$$$)\", where $$$t$$$ is correct bracket sequence; $$$s$$$ is equal to $$$t_1 t_2$$$, i.e. concatenation of $$$t_1$$$ and $$$t_2$$$, where $$$t_1$$$ and $$$t_2$$$ are correct bracket sequences. For example, \"(()())\", \"()\" are correct, while \")(\" and \"())\" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct.", "c_code": "int solution() {\n int n;\n int bra = 0;\n int count = 0;\n int check = 0;\n scanf(\"%d\", &n);\n char a[n];\n\n for (int i = 0; i < n; i++) {\n scanf(\" %c\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n if (a[i] == '(') {\n count++;\n } else if (a[i] == ')') {\n count--;\n }\n if (count < 0) {\n check = 1;\n }\n if (count < -1) {\n bra = 1;\n }\n }\n if (count == 0 && check == 0) {\n printf(\"Yes\");\n } else if (count == 0 && check == 1) {\n if (bra == 0) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n } else {\n printf(\"No\");\n }\n}", "rust_code": "fn solution() {\n let mut _buffer = String::new();\n io::stdin()\n .read_line(&mut _buffer)\n .expect(\"failed to read input\");\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let vec = buffer\n .trim()\n .chars()\n .map(|c| if c == '(' { 1 } else { -1 })\n .collect::>();\n\n let mut acc: i32 = 0;\n let mut changed = false;\n for i in vec {\n acc += i as i32;\n if acc == -1 {\n if changed {\n break;\n }\n changed = true;\n acc = 0;\n }\n }\n if changed {\n acc -= 1;\n }\n println!(\"{}\", if acc == 0 { \"Yes\" } else { \"No\" });\n}", "difficulty": "easy"} {"problem_id": "0829", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

We have a string X, which has an even number of characters. Half the characters are S, and the other half are T.

\n

Takahashi, who hates the string ST, will perform the following operation 10^{10000} times:

\n
    \n
  • Among the occurrences of ST in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.
  • \n
\n

Find the eventual length of X.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≦ |X| ≦ 200,000
  • \n
  • The length of X is even.
  • \n
  • Half the characters in X are S, and the other half are T.
  • \n
\n
\n
\n
\n
\n

Partial Scores

    \n
  • In test cases worth 200 points, |X| ≦ 200.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

Print the eventual length of X.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

TSTTSS\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

In the 1-st operation, the 2-nd and 3-rd characters of TSTTSS are removed.\nX becomes TTSS, and since it does not contain ST anymore, nothing is done in the remaining 10^{10000}-1 operations.\nThus, the answer is 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

SSTTST\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

X will eventually become an empty string: SSTTSTSTSTST ⇒ ``.

\n
\n
\n
\n
\n
\n

Sample Input 3

TSSTTTSS\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n

X will become: TSSTTTSSTSTTSSTTSS.

\n
\n
", "c_code": "int solution() {\n int s_cnt = 0;\n int ans = 0;\n while (1) {\n char c = getchar();\n if (c == 'S') {\n s_cnt++;\n ans++;\n } else if (c == 'T' && s_cnt > 0) {\n s_cnt--;\n ans--;\n } else if (c == 'T') {\n ans++;\n } else {\n break;\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let v: Vec = buf.trim().chars().collect();\n\n let mut cnt = 0;\n let mut del = 0;\n for &x in v.iter() {\n if x == 'S' {\n cnt += 1;\n } else if cnt > 0 {\n cnt -= 1;\n del += 2;\n }\n }\n println!(\"{}\", v.len() - del);\n}", "difficulty": "easy"} {"problem_id": "0830", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

We have a 2 \\times N grid. We will denote the square at the i-th row and j-th column (1 \\leq i \\leq 2, 1 \\leq j \\leq N) as (i, j).

\n

You are initially in the top-left square, (1, 1).\nYou will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

\n

The square (i, j) contains A_{i, j} candies.\nYou will collect all the candies you visit during the travel.\nThe top-left and bottom-right squares also contain candies, and you will also collect them.

\n

At most how many candies can you collect when you choose the best way to travel?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq A_{i, j} \\leq 100 (1 \\leq i \\leq 2, 1 \\leq j \\leq N)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_{1, 1} A_{1, 2} ... A_{1, N}\nA_{2, 1} A_{2, 2} ... A_{2, N}\n
\n
\n
\n
\n
\n

Output

Print the maximum number of candies that can be collected.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n3 2 2 4 1\n1 2 2 2 1\n
\n
\n
\n
\n
\n

Sample Output 1

14\n
\n

The number of collected candies will be maximized when you:

\n
    \n
  • move right three times, then move down once, then move right once.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4\n1 1 1 1\n1 1 1 1\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n

You will always collect the same number of candies, regardless of how you travel.

\n
\n
\n
\n
\n
\n

Sample Input 3

7\n3 3 4 5 4 5 3\n5 3 4 4 2 3 2\n
\n
\n
\n
\n
\n

Sample Output 3

29\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1\n2\n3\n
\n
\n
\n
\n
\n

Sample Output 4

5\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int count = 0;\n int max = 0;\n scanf(\"%d\", &N);\n int A[N][2];\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &A[i][0]);\n }\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &A[i][1]);\n }\n for (int i = 0; i < N; i++) {\n count = 0;\n for (int k = 0; k <= i; k++) {\n count += A[k][0];\n }\n for (int j = i; j < N; j++) {\n count += A[j][1];\n }\n if (max < count) {\n max = count;\n }\n }\n\n printf(\"%d\\n\", max);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let squares: Vec> = vec![\n (0..n)\n .map(|_| iter.next().unwrap().parse().unwrap())\n .collect(),\n (0..n)\n .map(|_| iter.next().unwrap().parse().unwrap())\n .collect(),\n ];\n\n let mut answers = Vec::new();\n for i in 0..n {\n answers\n .push(squares[0][0..(i + 1)].iter().sum::() + squares[1][i..].iter().sum::());\n }\n\n println!(\"{}\", answers.iter().max().unwrap());\n}", "difficulty": "medium"} {"problem_id": "0831", "problem_description": "Your favorite shop sells $$$n$$$ Kinder Surprise chocolate eggs. You know that exactly $$$s$$$ stickers and exactly $$$t$$$ toys are placed in $$$n$$$ eggs in total.Each Kinder Surprise can be one of three types: it can contain a single sticker and no toy; it can contain a single toy and no sticker; it can contain both a single sticker and a single toy. But you don't know which type a particular Kinder Surprise has. All eggs look identical and indistinguishable from each other.What is the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you'll obtain at least one sticker and at least one toy?Note that you do not open the eggs in the purchasing process, that is, you just buy some number of eggs. It's guaranteed that the answer always exists.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n long long int n;\n long long int s;\n long long int t;\n scanf(\"%lld %lld %lld\", &n, &s, &t);\n if (n == s && n == t) {\n printf(\"1\\n\");\n }\n\n else {\n if (s >= t) {\n printf(\"%lld\\n\", n - t + 1);\n } else {\n printf(\"%lld\\n\", n - s + 1);\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut line = String::new();\n let stdin = io::stdin();\n\n stdin.lock().read_line(&mut line)?;\n\n let trimmed_line = line.trim();\n let number_of_queries: u8 = trimmed_line.parse().unwrap();\n\n let queries: Vec<_> = stdin\n .lock()\n .lines()\n .filter_map(Result::ok)\n .take(number_of_queries as usize)\n .collect();\n\n for query in queries {\n let query: Vec<_> = query.split(' ').collect();\n\n let n: u64 = query[0].parse().unwrap();\n let s: u64 = query[1].parse().unwrap();\n let t: u64 = query[2].parse().unwrap();\n\n let b = cmp::max(s, t) - (n - cmp::min(s, t));\n println!(\"{}\", cmp::max(s - b, t - b) + 1);\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0832", "problem_description": "Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.The pizza is a circle of radius r and center at the origin. Pizza consists of the main part — circle of radius r - d with center at the origin, and crust around the main part of the width d. Pieces of sausage are also circles. The radius of the i -th piece of the sausage is ri, and the center is given as a pair (xi, yi).Gleb asks you to help determine the number of pieces of sausage caught on the crust. A piece of sausage got on the crust, if it completely lies on the crust.", "c_code": "int solution() {\n int r;\n int d;\n scanf(\"%d%d\", &r, &d);\n int n;\n scanf(\"%d\", &n);\n int a = 0;\n int p[n][3];\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < 3; ++j) {\n scanf(\"%d\", &p[i][j]);\n }\n }\n for (int k = 0; k < n; k++) {\n float dis = sqrt((p[k][0] * p[k][0]) + (p[k][1] * p[k][1]));\n if ((r - d <= dis + p[k][2]) && (dis + p[k][2] <= r) &&\n (r - d <= dis - p[k][2]) && (dis - p[k][2] <= r)) {\n a++;\n }\n }\n printf(\"%d\", a);\n}", "rust_code": "fn solution() {\n let (r, d) = {\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).unwrap();\n let mut it = input.split_whitespace().map(|k| k.parse::().unwrap());\n (it.next().unwrap(), it.next().unwrap())\n };\n\n let n = {\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).unwrap();\n input.trim().parse::().unwrap()\n };\n\n let mut pp = Vec::with_capacity(n);\n\n for _ in 0..n {\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).unwrap();\n let mut it = input.split_whitespace().map(|k| k.parse::().unwrap());\n pp.push((it.next().unwrap(), it.next().unwrap(), it.next().unwrap()));\n }\n\n let ans = pp\n .into_iter()\n .filter(|&(x, y, ri)| {\n let di = (x.powi(2) + y.powi(2)).sqrt();\n di - ri >= r - d && di + ri <= r\n })\n .count();\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0833", "problem_description": "Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.", "c_code": "int solution() {\n int n;\n char values[200005];\n\n scanf(\"%d\", &n);\n scanf(\"%s\", values);\n int one = 0;\n int zero = 0;\n for (int i = 0; i < n; i++) {\n if (values[i] == '0') {\n zero++;\n } else {\n one++;\n }\n }\n if (one < zero) {\n printf(\"%d\\n\", n - (2 * one));\n } else {\n printf(\"%d\\n\", n - (2 * zero));\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut text = String::new();\n io::stdin().read_to_string(&mut text).unwrap();\n let mut iter = text.split_whitespace();\n let _n = iter.next().unwrap();\n let s = iter.next().unwrap();\n let mut count = 0i32;\n for c in s.chars() {\n if c == '1' {\n count += 1;\n } else {\n count -= 1;\n }\n }\n println!(\"{}\", count.abs());\n}", "difficulty": "hard"} {"problem_id": "0834", "problem_description": "A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimum unfortunate sum?", "c_code": "int solution() {\n int n;\n int i = 0;\n int x = -1000;\n scanf(\"%d\", &n);\n for (i = 0; i < n && x != 1; i++) {\n scanf(\"%d\", &x);\n }\n if (x == 1) {\n printf(\"-1\");\n } else {\n printf(\"1\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let inputstatus = 1;\n\n let mut buf = String::new();\n let filename = \"inputrust.txt\";\n\n if inputstatus == 0 {\n let mut f = File::open(filename).expect(\"file not found\");\n f.read_to_string(&mut buf)\n .expect(\"something went wrong reading the file\");\n } else {\n std::io::stdin().read_to_string(&mut buf).unwrap();\n }\n\n let mut iter = buf.split_whitespace();\n let _n: usize = iter.next().unwrap().parse().unwrap();\n let mut num = [100000; 100000];\n for (i, v) in iter.enumerate() {\n num[i] = v.parse().unwrap();\n }\n num.sort();\n\n let ans = match num[0] {\n 1 => -1,\n _ => 1,\n };\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0835", "problem_description": "After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \\ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \\ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \\ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.", "c_code": "int solution() {\n long long int n;\n scanf(\"%lld\", &n);\n\n long long int arr[n + 1];\n long long int brr[n + 1];\n long long int ais[n + 1];\n long long int bis[n + 1];\n long long int dist[n + 1];\n for (long long int i = 1; i <= n; i++) {\n scanf(\"%lld\", &arr[i]);\n ais[arr[i]] = i;\n }\n\n for (long long int i = 1; i <= n; i++) {\n scanf(\"%lld\", &brr[i]);\n bis[brr[i]] = i;\n }\n\n for (int i = 0; i <= n; i++) {\n dist[i] = 0;\n }\n\n for (int i = 1; i <= n; i++) {\n int x = ais[i];\n int y = bis[i];\n if (x <= y) {\n dist[y - x]++;\n } else {\n dist[n - x + y]++;\n }\n }\n\n int mx = -1;\n for (int i = 0; i < n; i++) {\n if (dist[i] > mx) {\n mx = dist[i];\n }\n }\n\n printf(\"%d\\n\", mx);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: 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 mut ys: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let mut ixs: Vec<(usize, usize)> = xs.iter().cloned().enumerate().collect();\n ixs.sort_by(|a, b| a.1.cmp(&b.1));\n for i in 0..ys.len() {\n ys[i] = ixs[ys[i] - 1].0;\n }\n let mut counts: Vec = vec![0; n];\n for i in 0..ys.len() {\n let j = (ys[i] + n - i) % n;\n counts[j] += 1;\n }\n let ans = counts.iter().max().unwrap();\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "0836", "problem_description": "Lee is going to fashionably decorate his house for a party, using some regular convex polygons...Lee thinks a regular $$$n$$$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $$$OX$$$-axis and at least one of its edges is parallel to the $$$OY$$$-axis at the same time.Recall that a regular $$$n$$$-sided polygon is a convex polygon with $$$n$$$ vertices such that all the edges and angles are equal.Now he is shopping: the market has $$$t$$$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.", "c_code": "int solution() {\n int n;\n scanf(\"%d\\n\", &n);\n int p[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\\n\", &p[i]);\n if (((p[i]) % 4) == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).expect(\"\");\n let t: u32 = t.trim().parse().expect(\"\");\n for _ in 0..t {\n let mut n = String::new();\n io::stdin().read_line(&mut n).expect(\"\");\n let n: u64 = n.trim().parse().expect(\"\");\n if n.is_multiple_of(4) {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0837", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given a 4-character string S consisting of uppercase English letters.\nDetermine if S consists of exactly two kinds of characters which both appear twice in S.

\n
\n
\n
\n
\n

Constraints

    \n
  • The length of S is 4.
  • \n
  • S consists of uppercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

ASSA\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

S consists of A and S which both appear twice in S.

\n
\n
\n
\n
\n
\n

Sample Input 2

STOP\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

FFEE\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 4

FREE\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
", "c_code": "int solution(void) {\n char S[5];\n scanf(\"%s\", S);\n char a = S[0];\n char b = S[1];\n char c = S[2];\n char d = S[3];\n bool ok = false;\n if (a == b && c == d && a != c) {\n ok = true;\n }\n if (a == c && b == d && a != d) {\n ok = true;\n }\n if (a == d && b == c && a != b) {\n ok = true;\n }\n if (ok) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n s = s.trim_end().to_owned();\n let mut s_vec: Vec = s.chars().collect();\n s_vec.sort();\n if s_vec[0] == s_vec[1] && s_vec[1] != s_vec[2] && s_vec[2] == s_vec[3] {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "0838", "problem_description": "You have a list of numbers from $$$1$$$ to $$$n$$$ written from left to right on the blackboard.You perform an algorithm consisting of several steps (steps are $$$1$$$-indexed). On the $$$i$$$-th step you wipe the $$$i$$$-th number (considering only remaining numbers). You wipe the whole number (not one digit). When there are less than $$$i$$$ numbers remaining, you stop your algorithm. Now you wonder: what is the value of the $$$x$$$-th remaining number after the algorithm is stopped?", "c_code": "int solution() {\n\n int T;\n scanf(\"%d\", &T);\n int x[T];\n int n;\n\n for (int i = 0; i < T; ++i) {\n scanf(\"%d %d\", &n, &x[i]);\n }\n\n for (int i = 0; i < T; ++i) {\n printf(\"%d\\n\", 2 * x[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let t: u16 = buffer.trim().parse().expect(\"invalid input\");\n\n for _ in 0..t {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let l: u64 = input.split_whitespace().last().unwrap().parse().unwrap();\n\n println!(\"{}\", 2 * l);\n }\n}", "difficulty": "medium"} {"problem_id": "0839", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

AtCoDeer has three cards, one red, one green and one blue.
\nAn integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.
\nWe will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.
\nIs this integer a multiple of 4?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ r, g, b ≤ 9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
r g b\n
\n
\n
\n
\n
\n

Output

If the three-digit integer is a multiple of 4, print YES (case-sensitive); otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3 2\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

432 is a multiple of 4, and thus YES should be printed.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 3 4\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n

234 is not a multiple of 4, and thus NO should be printed.

\n
\n
", "c_code": "int solution() {\n\n int r = 0;\n int g = 0;\n int b = 0;\n\n int number = 0;\n\n scanf(\"%d %d %d\", &r, &g, &b);\n\n number = 100 * r + 10 * g + b;\n\n if (number % 4 == 0) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n let mut input = String::new();\n let _ = io::stdin().read_line(&mut input);\n let inputs = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n let num = inputs[1] * 10 + inputs[2];\n if num % 4 == 0 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "easy"} {"problem_id": "0840", "problem_description": "

Finding Missing Cards

\n\n

\n Taro is going to play a card game. However, now he has only n cards, even though there should be 52 cards (he has no Jokers).\n

\n\n

\nThe 52 cards include 13 ranks of each of the four suits: spade, heart, club and diamond.\n

\n\n\n

Input

\n

\n In the first line, the number of cards n (n ≤ 52) is given.\n

\n\n

\n In the following n lines, data of the n cards are given. Each card is given by a pair of a character and an integer which represent its suit and rank respectively. A suit is represented by 'S', 'H', 'C' and 'D' for spades, hearts, clubs and diamonds respectively. A rank is represented by an integer from 1 to 13.\n

\n\n

Output

\n\n

\n Print the missing cards. The same as the input format, each card should be printed with a character and an integer separated by a space character in a line.\n Arrange the missing cards in the following priorities:\n

\n\n
    \n
  • Print cards of spades, hearts, clubs and diamonds in this order.
  • \n
  • If the suits are equal, print cards with lower ranks first.
  • \n
\n\n

Sample Input

\n\n
\n47\nS 10\nS 11\nS 12\nS 13\nH 1\nH 2\nS 6\nS 7\nS 8\nS 9\nH 6\nH 8\nH 9\nH 10\nH 11\nH 4\nH 5\nS 2\nS 3\nS 4\nS 5\nH 12\nH 13\nC 1\nC 2\nD 1\nD 2\nD 3\nD 4\nD 5\nD 6\nD 7\nC 3\nC 4\nC 5\nC 6\nC 7\nC 8\nC 9\nC 10\nC 11\nC 13\nD 9\nD 10\nD 11\nD 12\nD 13\n
\n\n\n\n

Sample Output

\n\n
\nS 1\nH 3\nH 7\nC 12\nD 8\n
\n\n

Note

\n\n\n
\n\n
      解説      
\n
\t\n
", "c_code": "int solution(void) {\n\n int repeatNumber = 0;\n scanf(\"%d\", &repeatNumber);\n int spade[13] = {0};\n int heart[13] = {0};\n int chulab[13] = {0};\n int dia[13] = {0};\n char judge1 = 'A';\n int judge2 = 0;\n\n for (int i = 0; i < repeatNumber; i++) {\n scanf(\" %c %d\", &judge1, &judge2);\n\n if (judge1 == 'S') {\n spade[judge2 - 1]++;\n } else if (judge1 == 'H') {\n heart[judge2 - 1]++;\n } else if (judge1 == 'C') {\n chulab[judge2 - 1]++;\n } else if (judge1 == 'D') {\n dia[judge2 - 1]++;\n }\n }\n\n for (int i = 0; i < 13; i++) {\n if (spade[i] != 1) {\n printf(\"S %d\\n\", i + 1);\n }\n }\n\n for (int i = 0; i < 13; i++) {\n if (heart[i] != 1) {\n printf(\"H %d\\n\", i + 1);\n }\n }\n\n for (int i = 0; i < 13; i++) {\n if (chulab[i] != 1) {\n printf(\"C %d\\n\", i + 1);\n }\n }\n\n for (int i = 0; i < 13; i++) {\n\n if (dia[i] != 1) {\n printf(\"D %d\\n\", i + 1);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut buf = String::new();\n\n let design = ['S', 'H', 'C', 'D'];\n let mut trump = HashMap::new();\n\n for d in &design {\n trump.insert(d, [false; 13]);\n }\n\n let _ = stdin.read_line(&mut buf);\n let n = i64::from_str(buf.trim()).unwrap();\n\n for _ in 0..n {\n buf.clear();\n let _ = stdin.read_line(&mut buf);\n let vec: Vec<&str> = buf.split_whitespace().collect();\n let (d, r) = (\n vec[0].chars().next().unwrap(),\n usize::from_str(vec[1]).unwrap(),\n );\n\n trump.get_mut(&d).unwrap()[r - 1] = true;\n }\n\n for d in &design {\n for (i, r) in trump[d].iter().enumerate() {\n if !r {\n println!(\"{} {}\", d, i + 1);\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0841", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

\n

City i is established in year Y_i and belongs to Prefecture P_i.

\n

You can assume that there are no multiple cities that are established in the same year.

\n

It is decided to allocate a 12-digit ID number to each city.

\n

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.

\n

Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.

\n

Find the ID numbers for all the cities.

\n

Note that there can be a prefecture with no cities.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq M \\leq 10^5
  • \n
  • 1 \\leq P_i \\leq N
  • \n
  • 1 \\leq Y_i \\leq 10^9
  • \n
  • Y_i are all different.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nP_1 Y_1\n:\nP_M Y_M\n
\n
\n
\n
\n
\n

Output

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3\n1 32\n2 63\n1 12\n
\n
\n
\n
\n
\n

Sample Output 1

000001000002\n000002000001\n000001000001\n
\n
    \n
  • As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.
  • \n
  • As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.
  • \n
  • As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

2 3\n2 55\n2 77\n2 99\n
\n
\n
\n
\n
\n

Sample Output 2

000002000001\n000002000002\n000002000003\n
\n
\n
", "c_code": "int solution() {\n long long N;\n long long M;\n long long i;\n scanf(\"%lld%lld\", &N, &M);\n long long P[M];\n long long Y[M];\n long long C[M];\n long long A[M];\n long long count[N + 1];\n for (i = 0; i < M; i++) {\n scanf(\"%lld%lld\", &P[i], &Y[i]);\n C[i] = i;\n }\n for (i = 0; i <= N; i++) {\n count[i] = 0;\n }\n\n long long h = M;\n long long swapped = 0;\n while (h > 1 || swapped == 1) {\n if (h > 1) {\n h = h * 10 / 13;\n }\n swapped = 0;\n for (i = 0; i < M - h; i++) {\n if (Y[i] > Y[i + h]) {\n long long tmp = Y[i];\n Y[i] = Y[i + h];\n Y[i + h] = tmp;\n tmp = P[i];\n P[i] = P[i + h];\n P[i + h] = tmp;\n tmp = C[i];\n C[i] = C[i + h];\n C[i + h] = tmp;\n swapped = 1;\n }\n }\n }\n\n for (i = 0; i < M; i++) {\n count[P[i]]++;\n A[C[i]] = P[i] * 1000000 + count[P[i]];\n }\n for (i = 0; i < M; i++) {\n if (A[i]) {\n printf(\"%012lld\\n\", A[i]);\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).ok();\n let mut it = buf.split_whitespace();\n let n = it.next().unwrap().parse::().unwrap();\n let m = it.next().unwrap().parse::().unwrap();\n let mut c = vec![Vec::new(); n];\n for i in 0..m {\n let p = it.next().unwrap().parse::().unwrap() - 1;\n let y = it.next().unwrap().parse::().unwrap();\n c[p].push((y, i));\n }\n let mut id = vec![(0, 0); m];\n for i in 0..n {\n c[i].sort();\n for j in 0..c[i].len() {\n id[c[i][j].1] = (i + 1, j + 1);\n }\n }\n for &p in &id {\n println!(\"{:06}{:06}\", p.0, p.1);\n }\n}", "difficulty": "medium"} {"problem_id": "0842", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

You are given a string S consisting of 0 and 1.\nFind the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times.

\n
    \n
  • Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\\geq K must be satisfied). For each integer i such that l\\leq i\\leq r, do the following: if S_i is 0, replace it with 1; if S_i is 1, replace it with 0.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1\\leq |S|\\leq 10^5
  • \n
  • S_i(1\\leq i\\leq N) is either 0 or 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

010\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

We can turn all the characters of S into 0 by the following operations:

\n
    \n
  • Perform the operation on the segment S[1,3] with length 3. S is now 101.
  • \n
  • Perform the operation on the segment S[1,2] with length 2. S is now 011.
  • \n
  • Perform the operation on the segment S[2,3] with length 2. S is now 000.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

100000000\n
\n
\n
\n
\n
\n

Sample Output 2

8\n
\n
\n
\n
\n
\n
\n

Sample Input 3

00001111\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n
\n
", "c_code": "int solution() {\n char S[100000];\n scanf(\"%s\", S);\n\n int length = strlen(S);\n int K = strlen(S);\n\n for (int i = 0; i < length - 1; i++) {\n if (S[i + 1] != S[i]) {\n if (2 * (i + 1) < length && length - (i + 1) < K) {\n K = length - (i + 1);\n } else if (i + 1 < K) {\n K = i + 1;\n }\n }\n }\n printf(\"%d\", K);\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut lock = stdin.lock();\n let mut str1 = String::new();\n lock.read_line(&mut str1).unwrap();\n let vec: Vec<_> = str1.split_whitespace().next().unwrap().bytes().collect();\n let mut sanjo = vec.len();\n for (i, j) in vec.iter().enumerate() {\n if i == 0 {\n continue;\n }\n if *j != vec[i - 1] {\n if i < vec.len() - i && vec.len() - i < sanjo {\n sanjo = vec.len() - i;\n } else if i < sanjo {\n sanjo = i;\n }\n }\n }\n println!(\"{}\", sanjo);\n}", "difficulty": "hard"} {"problem_id": "0843", "problem_description": "\n

Score : 800 points

\n
\n
\n

Problem Statement

There are N towns in Snuke Kingdom, conveniently numbered 1 through N.\nTown 1 is the capital.

\n

Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place.\nThe destination of the Teleporter of town i is town a_i (1≤a_i≤N).\nIt is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.

\n

King Snuke loves the integer K.\nThe selfish king wants to change the destination of the Teleporters so that the following holds:

\n
    \n
  • Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.
  • \n
\n

Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≤N≤10^5
  • \n
  • 1≤a_i≤N
  • \n
  • One can get to the capital from any town by using the Teleporters some number of times.
  • \n
  • 1≤K≤10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N K\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1\n2 3 1\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Change the destinations of the Teleporters to a = (1,1,1).

\n
\n
\n
\n
\n
\n

Sample Input 2

4 2\n1 1 2 2\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

There is no need to change the destinations of the Teleporters, since the king's desire is already satisfied.

\n
\n
\n
\n
\n
\n

Sample Input 3

8 2\n4 1 2 3 1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n

For example, change the destinations of the Teleporters to a = (1,1,2,1,1,2,2,4).

\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int K;\n int a[100001];\n scanf(\"%d %d\", &N, &K);\n for (i = 1; i <= N; i++) {\n scanf(\"%d\", &(a[i]));\n }\n\n int ans = 0;\n if (a[1] != 1) {\n a[1] = 1;\n ans++;\n }\n\n list *adj[100001];\n list e[100001];\n for (i = 2; i <= N; i++) {\n e[i].v = i;\n e[i].next = adj[a[i]];\n adj[a[i]] = &(e[i]);\n }\n\n int height[100001] = {};\n int q[100001];\n int head;\n int tail;\n list *p;\n q[0] = 1;\n for (head = 0, tail = 1; head < tail; head++) {\n for (p = adj[q[head]]; p != NULL; p = p->next) {\n q[tail++] = p->v;\n }\n }\n for (head--; head > 0; head--) {\n i = q[head];\n if (height[i] == K - 1 && a[i] != 1) {\n height[i] = -1;\n a[i] = 1;\n ans++;\n }\n if (height[a[i]] < height[i] + 1) {\n height[a[i]] = height[i] + 1;\n }\n }\n printf(\"%d\\n\", ans);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input!(n: usize, k: usize, a: [usize1; n]);\n let mut ans = 0;\n let mut a = a;\n if a[0] != 0 {\n ans += 1;\n a[0] = 0;\n }\n let mut tree = vec![vec![]; n];\n for child in 0..n {\n let parent = a[child];\n tree[parent].push(child);\n }\n\n let mut dist_from_leaf = vec![0; n];\n let mut finished_count = vec![0; n];\n let mut q = VecDeque::new();\n for i in 0..n {\n if tree[i].is_empty() {\n q.push_back(i);\n }\n }\n\n while let Some(v) = q.pop_front() {\n let parent = a[v];\n finished_count[parent] += 1;\n if dist_from_leaf[v] == k - 1 {\n if parent != 0 {\n ans += 1;\n }\n } else {\n dist_from_leaf[parent] = cmp::max(dist_from_leaf[parent], dist_from_leaf[v] + 1);\n };\n\n if finished_count[parent] == tree[parent].len() {\n q.push_back(parent);\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0844", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Three poles stand evenly spaced along a line. Their heights are a, b and c meters, from left to right.\nWe will call the arrangement of the poles beautiful if the tops of the poles lie on the same line, that is, b-a = c-b.

\n

Determine whether the arrangement of the poles is beautiful.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq a,b,c \\leq 100
  • \n
  • a, b and c are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b c\n
\n
\n
\n
\n
\n

Output

Print YES if the arrangement of the poles is beautiful; print NO otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 4 6\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

Since 4-2 = 6-4, this arrangement of poles is beautiful.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 5 6\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n

Since 5-2 \\neq 6-5, this arrangement of poles is not beautiful.

\n
\n
\n
\n
\n
\n

Sample Input 3

3 2 1\n
\n
\n
\n
\n
\n

Sample Output 3

YES\n
\n

Since 1-2 = 2-3, this arrangement of poles is beautiful.

\n
\n
", "c_code": "int solution() {\n int a[10];\n scanf(\"%d%d%d\", &a[0], &a[1], &a[2]);\n if (a[1] - a[0] == a[2] - a[1]) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut s = String::new();\n stdin.read_line(&mut s).ok();\n s.pop();\n let mut abc: Vec = Vec::new();\n let it = s.split_whitespace();\n for arg in it {\n abc.push(i32::from_str(arg).expect(\"e\"));\n }\n let a = abc[0];\n let b = abc[1];\n let c = abc[2];\n if b - a == c - b {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "0845", "problem_description": "You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consists of two phases. During the first phase you make your move and during the second phase Slavik makes his move.There are $$$n$$$ doors, the $$$i$$$-th door initially has durability equal to $$$a_i$$$.During your move you can try to break one of the doors. If you choose door $$$i$$$ and its current durability is $$$b_i$$$ then you reduce its durability to $$$max(0, b_i - x)$$$ (the value $$$x$$$ is given).During Slavik's move he tries to repair one of the doors. If he chooses door $$$i$$$ and its current durability is $$$b_i$$$ then he increases its durability to $$$b_i + y$$$ (the value $$$y$$$ is given). Slavik cannot repair doors with current durability equal to $$$0$$$.The game lasts $$$10^{100}$$$ turns. If some player cannot make his move then he has to skip it.Your goal is to maximize the number of doors with durability equal to $$$0$$$ at the end of the game. You can assume that Slavik wants to minimize the number of such doors. What is the number of such doors in the end if you both play optimally?", "c_code": "int solution() {\n int n;\n int x;\n int y;\n scanf(\"%d%d%d\", &n, &x, &y);\n int d[n];\n int good = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &d[i]);\n if (d[i] <= x) {\n good++;\n }\n }\n if (x > y) {\n printf(\"%d\", n);\n } else {\n printf(\"%d\", (good + 1) / 2);\n }\n}", "rust_code": "fn solution() {\n let (n, a, b): (usize, u32, u32) = {\n let mut read_buf = String::new();\n io::stdin().read_line(&mut read_buf).unwrap();\n let mut read_buf = read_buf.split_whitespace();\n (\n read_buf.next().unwrap().parse().unwrap(),\n read_buf.next().unwrap().parse().unwrap(),\n read_buf.next().unwrap().parse().unwrap(),\n )\n };\n let cnt = {\n let mut res = 0;\n let mut read_buf = String::new();\n io::stdin().read_line(&mut read_buf).unwrap();\n for i in read_buf.split_whitespace() {\n let i: u32 = i.parse().unwrap();\n if i <= a {\n res += 1;\n }\n }\n res\n };\n\n if a > b {\n println!(\"{}\", n);\n } else {\n println!(\"{}\", (cnt + 1) / 2);\n }\n}", "difficulty": "medium"} {"problem_id": "0846", "problem_description": "

Toggling Cases


\n\n

\n Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string.\n

\n\n\n

Input

\n\n

\n A string is given in a line.\n

\n\n

Output

\n\n

\n Print the converted string in a line. Note that you do not need to convert any characters other than alphabetical letters.\n

\n\n

Constraints

\n\n
    \n
  • The length of the input string < 1200
  • \n
\n\n

Sample Input

\n\n
\nfAIR, LATER, OCCASIONALLY CLOUDY.\n
\n\n

Sample Output

\n\n
\nFair, later, occasionally cloudy.\n
", "c_code": "int solution(void) {\n char s[1201];\n int i = 0;\n\n scanf(\"%[^\\n]\", s);\n\n while (s[i] != '\\0' && s[i] != '\\0') {\n if (s[i] > 96 && s[i] < 123) {\n s[i] = s[i] - 32;\n } else if (s[i] > 64 && s[i] < 91) {\n s[i] = s[i] + 32;\n }\n i++;\n }\n puts(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 for c in s.trim().chars() {\n let n = c as u8;\n print!(\n \"{}\",\n if (65..=90).contains(&n) {\n (n + 32) as char\n } else if (97..=122).contains(&n) {\n (n - 32) as char\n } else {\n c\n }\n );\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "0847", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

\n

You are given an integer N. Find the N-th Lucas number.

\n

Here, the i-th Lucas number L_i is defined as follows:

\n
    \n
  • L_0=2
  • \n
  • L_1=1
  • \n
  • L_i=L_{i-1}+L_{i-2} (i≥2)
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤86
  • \n
  • It is guaranteed that the answer is less than 10^{18}.
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the N-th Lucas number.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n
\n
\n
\n
\n
\n

Sample Output 1

11\n
\n
    \n
  • L_0=2
  • \n
  • L_1=1
  • \n
  • L_2=L_0+L_1=3
  • \n
  • L_3=L_1+L_2=4
  • \n
  • L_4=L_2+L_3=7
  • \n
  • L_5=L_3+L_4=11
  • \n
\n

Thus, the 5-th Lucas number is 11.

\n
\n
\n
\n
\n
\n

Sample Input 2

86\n
\n
\n
\n
\n
\n

Sample Output 2

939587134549734843\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n unsigned long long a = -1;\n unsigned long long b = 2;\n scanf(\"%d\", &n);\n while (n-- > 0) {\n b = a + b;\n a = b - a;\n }\n printf(\"%llu\\n\", b);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let n: usize = s.trim().parse().ok().unwrap();\n let mut v: Vec = Vec::new();\n\n v.push(2);\n v.push(1);\n for i in 2..(n + 1) {\n let x = v[i - 1] + v[i - 2];\n v.push(x);\n }\n println!(\"{}\", v[n]);\n}", "difficulty": "medium"} {"problem_id": "0848", "problem_description": "You are given an integer $$$n$$$. You have to change the minimum number of digits in it in such a way that the resulting number does not have any leading zeroes and is divisible by $$$7$$$.If there are multiple ways to do it, print any of them. If the given number is already divisible by $$$7$$$, leave it unchanged.", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &n);\n if (n % 7 == 0) {\n printf(\"%d\\n\", n);\n } else {\n for (int j = 0; j <= 9; j++) {\n n /= 10;\n n = n * 10 + j;\n if (n % 7 == 0) {\n printf(\"%d\\n\", n);\n break;\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let handle = io::stdin();\n let _ = handle.read_line(&mut buf);\n let ntc: usize = buf.trim().parse().unwrap();\n for _ in 1..=ntc {\n buf.clear();\n let _ = handle.read_line(&mut buf);\n let num: usize = buf.trim().parse().unwrap();\n let m10 = num % 10;\n let m7 = num % 7;\n if m10 >= m7 {\n println!(\"{}\", num - m7)\n } else {\n println!(\"{}\", num + 7 - m7);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0849", "problem_description": "Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky.When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest.Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123.What maximum number of tickets could Vasya get after that?", "c_code": "int solution() {\n int n;\n int a;\n static int cnt[3];\n\n scanf(\"%d\", &n);\n while (n-- > 0) {\n scanf(\"%d\", &a);\n cnt[a % 3]++;\n }\n printf(\"%d\\n\", (cnt[0] / 2) + (cnt[1] < cnt[2] ? cnt[1] : cnt[2]));\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf)?;\n buf.clear();\n io::stdin().read_line(&mut buf)?;\n let a: Vec = buf\n .split_ascii_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut rem = [0; 3];\n for i in a {\n rem[i % 3] += 1;\n }\n\n let ans = rem[0] / 2 + cmp::min(rem[1], rem[2]);\n println!(\"{}\", ans);\n\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "0850", "problem_description": "The elections in which three candidates participated have recently ended. The first candidate received $$$a$$$ votes, the second one received $$$b$$$ votes, the third one received $$$c$$$ votes. For each candidate, solve the following problem: how many votes should be added to this candidate so that he wins the election (i.e. the number of votes for this candidate was strictly greater than the number of votes for any other candidate)?Please note that for each candidate it is necessary to solve this problem independently, i.e. the added votes for any candidate do not affect the calculations when getting the answer for the other two candidates.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long a[3];\n long long max = 0;\n long long c = 0;\n for (int i = 0; i < 3; i++) {\n scanf(\"%lld\", &a[i]);\n if (max < a[i]) {\n max = a[i];\n }\n }\n for (int i = 0; i < 3; i++) {\n if (max == a[i]) {\n c += 1;\n }\n }\n for (int i = 0; i < 3; i++) {\n if (max == 0 && a[i] == 0) {\n printf(\"1 \");\n } else if (a[i] < max) {\n printf(\"%lld \", (max - a[i]) + 1);\n } else if (a[i] == max && c == 1) {\n printf(\"%lld \", max - a[i]);\n } else if (a[i] == max && c > 1) {\n printf(\"%lld \", max - a[i] + 1);\n }\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(1) {\n let v = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>();\n let (m, c) = v[1..].iter().fold((v[0], 0), |(m, c), &x| {\n if x > m {\n (x, 0)\n } else if x == m {\n (m, 1)\n } else {\n (m, c)\n }\n });\n for &x in v.iter() {\n write!(o, \"{} \", if x == m { c } else { m - x + 1 }).ok();\n }\n writeln!(o).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "0851", "problem_description": "Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.Katya had no problems with completing this task. Will you do the same?", "c_code": "int solution() {\n long long n = 0;\n scanf(\"%lli\", &n);\n\n long long a = -1;\n long long b = -1;\n\n if (n > 2) {\n if (n % 2 == 0) {\n long long m = n / 2;\n a = m * m - 1;\n b = m * m + 1;\n } else {\n long long k = (n - 1) / 2;\n long long m = 1 + k;\n b = 2 * k * m;\n a = m * m + k * k;\n }\n }\n\n if (a != -1) {\n printf(\"%lli %lli\\n\", a, b);\n } else {\n printf(\"-1\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut s = String::new();\n let _lines = stdin.lock().read_line(&mut s).unwrap();\n let a = s.trim().parse::().unwrap();\n if a <= 2 {\n println!(\"-1\");\n return;\n }\n if (a & 0x01u64) != 0 {\n println!(\"{} {}\", ((a * a) - 1) / 2, (a * a).div_ceil(2));\n } else {\n println!(\"{} {}\", ((a / 2) * (a / 2)) - 1, ((a / 2) * (a / 2)) + 1);\n }\n}", "difficulty": "easy"} {"problem_id": "0852", "problem_description": "A sequence $$$a = [a_1, a_2, \\ldots, a_l]$$$ of length $$$l$$$ has an ascent if there exists a pair of indices $$$(i, j)$$$ such that $$$1 \\le i < j \\le l$$$ and $$$a_i < a_j$$$. For example, the sequence $$$[0, 2, 0, 2, 0]$$$ has an ascent because of the pair $$$(1, 4)$$$, but the sequence $$$[4, 3, 3, 3, 1]$$$ doesn't have an ascent.Let's call a concatenation of sequences $$$p$$$ and $$$q$$$ the sequence that is obtained by writing down sequences $$$p$$$ and $$$q$$$ one right after another without changing the order. For example, the concatenation of the $$$[0, 2, 0, 2, 0]$$$ and $$$[4, 3, 3, 3, 1]$$$ is the sequence $$$[0, 2, 0, 2, 0, 4, 3, 3, 3, 1]$$$. The concatenation of sequences $$$p$$$ and $$$q$$$ is denoted as $$$p+q$$$.Gyeonggeun thinks that sequences with ascents bring luck. Therefore, he wants to make many such sequences for the new year. Gyeonggeun has $$$n$$$ sequences $$$s_1, s_2, \\ldots, s_n$$$ which may have different lengths. Gyeonggeun will consider all $$$n^2$$$ pairs of sequences $$$s_x$$$ and $$$s_y$$$ ($$$1 \\le x, y \\le n$$$), and will check if its concatenation $$$s_x + s_y$$$ has an ascent. Note that he may select the same sequence twice, and the order of selection matters.Please count the number of pairs ($$$x, y$$$) of sequences $$$s_1, s_2, \\ldots, s_n$$$ whose concatenation $$$s_x + s_y$$$ contains an ascent.", "c_code": "int solution() {\n long long int n;\n scanf(\"%lld\", &n);\n long long int l;\n long long int sl[100005];\n sl[0] = 0;\n long long int i;\n long long int j;\n long long int s[100005];\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &l);\n for (j = 0; j < l; j++) {\n scanf(\"%lld\", &s[sl[i] + j]);\n }\n sl[i + 1] = sl[i] + l;\n }\n long long int min[100005];\n long long int max[100005];\n long long int t[100005];\n for (i = 0; i < n; i++) {\n min[i] = 10000007;\n max[i] = -1;\n t[i] = 0;\n for (j = sl[i]; j < sl[i + 1] - 1; j++) {\n if (s[j] < min[i]) {\n min[i] = s[j];\n }\n if (max[i] < s[j]) {\n max[i] = s[j];\n }\n if (s[j] < s[j + 1]) {\n t[i] = 1;\n }\n }\n if (s[sl[i + 1] - 1] < min[i]) {\n min[i] = s[sl[i + 1] - 1];\n }\n if (max[i] < s[sl[i + 1] - 1]) {\n max[i] = s[sl[i + 1] - 1];\n }\n }\n long long int c = 0;\n for (i = 0; i < n; i++) {\n if (t[i] > 0) {\n c++;\n }\n }\n long long int a[1000006];\n for (i = 0; i < 1000006; i++) {\n a[i] = 0;\n }\n for (i = 0; i < n; i++) {\n if (t[i] == 0) {\n a[max[i]]++;\n }\n }\n for (i = 1000004; i >= 0; i--) {\n a[i] += a[i + 1];\n }\n long long int ans = 0;\n for (i = 0; i < n; i++) {\n if (t[i] > 0) {\n ans += n;\n } else {\n ans += c;\n ans += a[min[i] + 1];\n }\n }\n printf(\"%lld\\n\", ans);\n return 0;\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\n .split_whitespace()\n .map(|x| x.parse::().expect(\"convert to number\"))\n .collect::>();\n\n let n = arr[0];\n\n let mut ix = 1;\n\n let mut s = vec![];\n for _i in 0..n {\n let m = arr[ix];\n ix += 1;\n s.push(arr[ix..ix + m as usize].to_vec());\n ix += m as usize;\n }\n\n let mut info = vec![];\n for arr in s.iter() {\n let _ma = *arr.iter().max().unwrap();\n let _mi = *arr.iter().min().unwrap();\n\n let is_non_increasing = arr[..].windows(2).all(|w| w[0] >= w[1]);\n if is_non_increasing {\n info.push((arr[0], *arr.iter().rev().next().unwrap()));\n }\n }\n\n let mut ans = n * n;\n\n info.sort();\n\n let mut idx = 0;\n for i in info.iter() {\n let last = i.1;\n\n let mut l = 0;\n let mut r = info.len();\n while l < r {\n let m = (l + r) / 2;\n\n let v = info[m].0;\n if v > last {\n r = m;\n } else {\n l = m + 1;\n }\n }\n\n ans -= l as i64;\n idx += 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0853", "problem_description": "Vasya has recently got a job as a cashier at a local store. His day at work is $$$L$$$ minutes long. Vasya has already memorized $$$n$$$ regular customers, the $$$i$$$-th of which comes after $$$t_{i}$$$ minutes after the beginning of the day, and his service consumes $$$l_{i}$$$ minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for $$$a$$$ minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day?", "c_code": "int solution() {\n int n;\n int L;\n int a;\n scanf(\"%d %d %d\", &n, &L, &a);\n int t[n];\n int l[n];\n for (int i = 0; i < n; ++i) {\n scanf(\"%d %d\", &t[i], &l[i]);\n }\n\n int time_between = 0;\n int sessions = 0;\n for (int i = 0; i < n; ++i) {\n if (i == 0) {\n time_between = t[i] - 0;\n } else {\n time_between = t[i] - (t[i - 1] + l[i - 1]);\n }\n sessions += time_between / a;\n }\n if (n > 0) {\n time_between = L - (t[n - 1] + l[n - 1]);\n } else {\n time_between = L;\n }\n sessions += time_between / a;\n printf(\"%d\\n\", sessions);\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] as usize;\n let l = arr[1];\n let a = arr[2];\n let mut prev = 0;\n let mut ans = 0;\n for i in 0..n {\n let ti = arr[3 + i * 2];\n let li = arr[4 + i * 2];\n let d = ti - prev;\n ans += d / a;\n prev = ti + li;\n }\n ans += (l - prev) / a;\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0854", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, and an array $$$b$$$, with length $$$n$$$. Initially $$$b_i=0$$$ for each $$$1 \\leq i \\leq n$$$.In one move you can choose an integer $$$i$$$ ($$$1 \\leq i \\leq n$$$), and add $$$a_i$$$ to $$$b_i$$$ or subtract $$$a_i$$$ from $$$b_i$$$. What is the minimum number of moves needed to make $$$b$$$ increasing (that is, every element is strictly greater than every element before it)?", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n long long int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", a + i);\n }\n long long int zeropos;\n long long int minmov = LLONG_MAX;\n for (zeropos = 0; zeropos < n; zeropos++) {\n long long int movcount = 0;\n long long b[n];\n b[zeropos] = 0;\n for (int i = zeropos + 1; i < n; i++) {\n long long int mintimes = 1 + (b[i - 1] / a[i]);\n b[i] = a[i] * (mintimes);\n movcount += mintimes;\n }\n for (int i = zeropos - 1; i >= 0; i--) {\n long long int mintimes = 1 + ((-b[i + 1]) / a[i]);\n b[i] = (-a[i]) * (mintimes);\n movcount += mintimes;\n }\n if (movcount < minmov) {\n minmov = movcount;\n }\n }\n printf(\"%lld\\n\", minmov);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let arr: Vec = input\n .trim()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut ans = -1;\n for i in 0..n {\n let mut tmp = 0;\n\n let mut lst = 0i64;\n for j in (0..i).rev() {\n tmp += lst / arr[j] + 1;\n lst = (lst / arr[j] + 1) * arr[j];\n }\n lst = 0;\n for j in i + 1..n {\n tmp += lst / arr[j] + 1;\n lst = (lst / arr[j] + 1) * arr[j];\n }\n\n if tmp < ans || ans == -1 {\n ans = tmp;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0855", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

You are given an integer N.\nBuild an undirected graph with N vertices with indices 1 to N that satisfies the following two conditions:

\n
    \n
  • The graph is simple and connected.
  • \n
  • There exists an integer S such that, for every vertex, the sum of the indices of the vertices adjacent to that vertex is S.
  • \n
\n

It can be proved that at least one such graph exists under the constraints of this problem.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 3 \\leq N \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

In the first line, print the number of edges, M, in the graph you made. In the i-th of the following M lines, print two integers a_i and b_i, representing the endpoints of the i-th edge.

\n

The output will be judged correct if the graph satisfies the conditions.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n1 3\n2 3\n
\n
    \n
  • For every vertex, the sum of the indices of the vertices adjacent to that vertex is 3.
  • \n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n if (n & 1) {\n printf(\"%d\\n\", (n - 1) * (n - 1) / 2);\n for (int i = 2; i <= n / 2; ++i) {\n for (int j = 1; j < i; ++j) {\n printf(\"%d %d\\n\", j, i);\n printf(\"%d %d\\n\", n - j, i);\n printf(\"%d %d\\n\", j, n - i);\n printf(\"%d %d\\n\", n - j, n - i);\n }\n }\n for (int i = 1; i < n; ++i) {\n printf(\"%d %d\\n\", i, n);\n }\n } else {\n printf(\"%d\\n\", n * (n - 2) / 2);\n for (int i = 2; i <= n / 2; ++i) {\n for (int j = 1; j < i; ++j) {\n printf(\"%d %d\\n\", j, i);\n printf(\"%d %d\\n\", n - j + 1, i);\n printf(\"%d %d\\n\", j, n - i + 1);\n printf(\"%d %d\\n\", n - j + 1, n - i + 1);\n }\n }\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 n = it.next().unwrap().parse::().unwrap();\n let ng = if n % 2 == 0 { n + 1 } else { n };\n let mut res = Vec::new();\n for i in 1..n {\n for j in i + 1..n + 1 {\n if i + j == ng {\n continue;\n }\n res.push((i, j));\n }\n }\n println!(\"{}\", res.len());\n for &p in &res {\n println!(\"{} {}\", p.0, p.1);\n }\n}", "difficulty": "medium"} {"problem_id": "0856", "problem_description": "Let's call a positive integer composite if it has at least one divisor other than $$$1$$$ and itself. For example: the following numbers are composite: $$$1024$$$, $$$4$$$, $$$6$$$, $$$9$$$; the following numbers are not composite: $$$13$$$, $$$1$$$, $$$2$$$, $$$3$$$, $$$37$$$. You are given a positive integer $$$n$$$. Find two composite integers $$$a,b$$$ such that $$$a-b=n$$$.It can be proven that solution always exists.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n if (n % 2 == 0) {\n printf(\"%d %d\", n + 4, 4);\n } else {\n printf(\"%d %d\", n + 9, 9);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::from(\"\");\n io::stdin().read_line(&mut s).expect(\"\");\n\n let n: i32 = s.trim().parse().expect(\"\");\n\n println!(\"{} {}\", 9 * n, 8 * n);\n}", "difficulty": "medium"} {"problem_id": "0857", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given an integer N that has exactly four digits in base ten.\nHow many times does 2 occur in the base-ten representation of N?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1000 \\leq N \\leq 9999
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1222\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

2 occurs three times in 1222. By the way, this contest is held on December 22 (JST).

\n
\n
\n
\n
\n
\n

Sample Input 2

3456\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

9592\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n int ans = 0;\n scanf(\"%d\", &n);\n for (int i = 1; i <= 4; i++) {\n if (n % 10 == 2) {\n ans += 1;\n }\n n /= 10;\n }\n printf(\"%d\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let handle = std::io::stdin();\n handle.read_line(&mut buf).unwrap();\n let mut count = 0;\n for c in buf.chars() {\n if c == '2' {\n count += 1;\n }\n }\n println!(\"{}\", count);\n}", "difficulty": "medium"} {"problem_id": "0858", "problem_description": "Cycle sort", "c_code": "void solution(int *arr, int n) {\n int writes = 0;\n\n for (int cycle_start = 0; cycle_start <= n - 2; cycle_start++) {\n int item = arr[cycle_start];\n int pos = cycle_start;\n\n for (int i = cycle_start + 1; i < n; i++) {\n if (arr[i] < item) {\n pos++;\n }\n }\n\n if (pos == cycle_start) {\n continue;\n }\n\n while (item == arr[pos]) {\n pos += 1;\n }\n\n if (pos != cycle_start) {\n int temp;\n\n temp = item;\n item = arr[pos];\n arr[pos] = temp;\n\n writes++;\n }\n\n while (pos != cycle_start) {\n pos = cycle_start;\n\n for (int i = cycle_start + 1; i < n; i++) {\n if (arr[i] < item) {\n pos += 1;\n }\n }\n\n while (item == arr[pos]) {\n pos += 1;\n }\n\n if (item != arr[pos]) {\n int temp;\n\n temp = item;\n item = arr[pos];\n arr[pos] = temp;\n\n writes++;\n }\n }\n }\n}\n", "rust_code": "fn solution(arr: &mut [i32]) {\n for cycle_start in 0..arr.len() {\n let mut item = arr[cycle_start];\n let mut pos = cycle_start;\n for i in arr.iter().skip(cycle_start + 1) {\n if *i < item {\n pos += 1;\n }\n }\n if pos == cycle_start {\n continue;\n }\n while item == arr[pos] {\n pos += 1;\n }\n std::mem::swap(&mut arr[pos], &mut item);\n while pos != cycle_start {\n pos = cycle_start;\n for i in arr.iter().skip(cycle_start + 1) {\n if *i < item {\n pos += 1;\n }\n }\n while item == arr[pos] {\n pos += 1;\n }\n std::mem::swap(&mut arr[pos], &mut item);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0859", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N gems. The value of the i-th gem is V_i.

\n

You will choose some of these gems, possibly all or none, and get them.

\n

However, you need to pay a cost of C_i to get the i-th gem.

\n

Let X be the sum of the values of the gems obtained, and Y be the sum of the costs paid.

\n

Find the maximum possible value of X-Y.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 20
  • \n
  • 1 \\leq C_i, V_i \\leq 50
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nV_1 V_2 ... V_N\nC_1 C_2 ... C_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible value of X-Y.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n10 2 5\n6 3 4\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

If we choose the first and third gems, X = 10 + 5 = 15 and Y = 6 + 4 = 10.\nWe have X-Y = 5 here, which is the maximum possible value.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n13 21 6 19\n11 30 6 15\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1\n1\n50\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n int total = 0;\n int t = 0;\n int i = 0;\n int N = 0;\n int V[50];\n int C[50];\n\n for (i = 0; i < 50; i++) {\n V[i] = 0;\n C[i] = 0;\n }\n\n scanf(\"%d\", &N);\n\n for (i = 0; i < N; i++) {\n scanf(\"%d\", &V[i]);\n }\n\n for (i = 0; i < N; i++) {\n scanf(\"%d\", &C[i]);\n }\n\n for (i = 0; i < N; i++) {\n t = V[i] - C[i];\n if (t > 0) {\n total += t;\n }\n }\n printf(\"%d\\n\", total);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let n: usize = buf.trim().parse().unwrap();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let vs = buf\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let cs = buf\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let mut ans: i32 = 0;\n for i in 0..n {\n let vc = vs[i] - cs[i];\n if vc > 0 {\n ans += vc;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0860", "problem_description": "You are playing another computer game, and now you have to slay $$$n$$$ monsters. These monsters are standing in a circle, numbered clockwise from $$$1$$$ to $$$n$$$. Initially, the $$$i$$$-th monster has $$$a_i$$$ health.You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $$$1$$$ (deals $$$1$$$ damage to it). Furthermore, when the health of some monster $$$i$$$ becomes $$$0$$$ or less than $$$0$$$, it dies and explodes, dealing $$$b_i$$$ damage to the next monster (monster $$$i + 1$$$, if $$$i < n$$$, or monster $$$1$$$, if $$$i = n$$$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.You have to calculate the minimum number of bullets you have to fire to kill all $$$n$$$ monsters in the circle.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n long long int res[t];\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n long long int health[n];\n long long int damage[n];\n long long int bullets = 0;\n for (int j = 0; j < n; j++) {\n scanf(\"%lld %lld\", &health[j], &damage[j]);\n }\n\n for (int j = 1; j < n; j++) {\n if (damage[j - 1] < health[j]) {\n bullets += health[j] - damage[j - 1];\n health[j] = damage[j - 1];\n }\n }\n if (damage[n - 1] < health[0]) {\n bullets += health[0] - damage[n - 1];\n health[0] = damage[n - 1];\n }\n\n long long int small = health[0];\n for (int j = 1; j < n; j++) {\n if (health[j] < small) {\n small = health[j];\n }\n }\n bullets += small;\n\n res[i] = bullets;\n }\n\n for (int i = 0; i < t; i++) {\n printf(\"%lld\\n\", res[i]);\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 let mut s = String::new();\n for _ in 0..t {\n let n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let mut ls: Vec = vec![0; n];\n let mut bs: Vec = vec![0; n];\n let mut cs: Vec = vec![0; n];\n let mut total: u64 = 0;\n for i in 0..n {\n let xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n ls[i] = xs[0];\n bs[i] = xs[1];\n if i > 0 {\n cs[i] = ls[i].saturating_sub(bs[i - 1]);\n total += cs[i];\n }\n }\n cs[0] = ls[0].saturating_sub(bs[n - 1]);\n total += cs[0];\n let mut ans: u64 = u64::max_value();\n for i in 0..n {\n ans = min(ans, total - cs[i] + ls[i]);\n }\n s.push_str(format!(\"{}\\n\", ans).as_str());\n }\n print!(\"{}\", s);\n}", "difficulty": "hard"} {"problem_id": "0861", "problem_description": "You are given a grid, consisting of $$$2$$$ rows and $$$n$$$ columns. Each cell of this grid should be colored either black or white.Two cells are considered neighbours if they have a common border and share the same color. Two cells $$$A$$$ and $$$B$$$ belong to the same component if they are neighbours, or if there is a neighbour of $$$A$$$ that belongs to the same component with $$$B$$$.Let's call some bicoloring beautiful if it has exactly $$$k$$$ components.Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $$$998244353$$$.", "c_code": "int solution(int argc, char **argv) {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n\n int64_t **same = (int64_t **)calloc(n, sizeof(int64_t *));\n int64_t **opposite = (int64_t **)calloc(n, sizeof(int64_t *));\n\n for (int i = 0; i < n; i++) {\n same[i] = (int64_t *)calloc(k, sizeof(int64_t));\n opposite[i] = (int64_t *)calloc(k, sizeof(int64_t));\n }\n\n same[0][0] = 2;\n\n opposite[0][0] = 0;\n opposite[0][1] = 2;\n\n for (int i = 1; i < n; i++) {\n\n for (int j = 0; j < k; j++) {\n same[i][j] += same[i - 1][j] + 2 * opposite[i - 1][j];\n opposite[i][j] += opposite[i - 1][j];\n }\n\n for (int j = 1; j < k; j++) {\n same[i][j] += same[i - 1][j - 1];\n opposite[i][j] += 2 * same[i - 1][j - 1];\n }\n\n for (int j = 2; j < k; j++) {\n opposite[i][j] += opposite[i - 1][j - 2];\n }\n\n for (int j = 0; j < k; j++) {\n same[i][j] = same[i][j] % 998244353;\n opposite[i][j] = opposite[i][j] % 998244353;\n }\n }\n int total = (same[n - 1][k - 1] + opposite[n - 1][k - 1]) % 998244353;\n printf(\"%d\\n\", total);\n return (EXIT_SUCCESS);\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 let k = arr[1].parse::().unwrap();\n\n let mut dp: [[[u32; 4]; 2001]; 2] = [[[0; 4]; 2001]; 2];\n dp[1][1][3] = 1;\n dp[1][1][0] = 1;\n dp[1][2][1] = 1;\n dp[1][2][2] = 1;\n let mo: u32 = 998244353;\n\n for i in 2..=n {\n let x: usize = (i % 2) as usize;\n let y: usize = ((i + 1) % 2) as usize;\n for j in 1..=k {\n dp[x][j as usize][0] = 0;\n dp[x][j as usize][1] = 0;\n dp[x][j as usize][2] = 0;\n dp[x][j as usize][3] = 0;\n }\n for j in 1..=k {\n dp[x][j as usize][3] = (dp[x][j as usize][3]\n + dp[y][j as usize][2]\n + dp[y][j as usize][1]\n + dp[y][j as usize][3])\n % mo;\n if j >= 1 {\n dp[x][j as usize][3] = (dp[x][j as usize][3] + dp[y][(j - 1) as usize][0]) % mo;\n }\n dp[x][j as usize][0] = (dp[x][j as usize][0]\n + dp[y][j as usize][2]\n + dp[y][j as usize][1]\n + dp[y][j as usize][0])\n % mo;\n if j >= 1 {\n dp[x][j as usize][0] = (dp[x][j as usize][0] + dp[y][(j - 1) as usize][3]) % mo;\n }\n dp[x][j as usize][2] = (dp[x][j as usize][2] + dp[y][j as usize][2]) % mo;\n if j >= 2 {\n dp[x][j as usize][2] = (dp[x][j as usize][2] + dp[y][(j - 2) as usize][1]) % mo;\n }\n if j >= 1 {\n dp[x][j as usize][2] = (dp[x][j as usize][2]\n + dp[y][(j - 1) as usize][0]\n + dp[y][(j - 1) as usize][3])\n % mo;\n }\n dp[x][j as usize][1] = (dp[x][j as usize][1] + dp[y][j as usize][1]) % mo;\n if j >= 2 {\n dp[x][j as usize][1] = (dp[x][j as usize][1] + dp[y][(j - 2) as usize][2]) % mo;\n }\n if j >= 1 {\n dp[x][j as usize][1] = (dp[x][j as usize][1]\n + dp[y][(j - 1) as usize][0]\n + dp[y][(j - 1) as usize][3])\n % mo;\n }\n }\n }\n let mut ans: u32 = 0;\n for i in 0..4 {\n let x = (n % 2) as usize;\n ans = (ans + dp[x][k as usize][i]) % mo;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0862", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are given integers N and M.

\n

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • N \\leq M \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\n
\n
\n
\n
\n
\n

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 14\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 123\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

10000\n
\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n if (n == 1) {\n printf(\"%d\", m);\n return 0;\n }\n for (int i = m / 2; i; i--) {\n if (!(m % i) && m / i >= n) {\n printf(\"%d\", i);\n return 0;\n }\n }\n return 0;\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 n: usize = iter.next().unwrap().parse().unwrap();\n let m: usize = iter.next().unwrap().parse().unwrap();\n\n let mut ans = 1;\n let mut gcd = 1;\n\n while gcd * gcd <= m {\n if m.is_multiple_of(gcd) {\n if n <= m / gcd {\n ans = std::cmp::max(gcd, ans);\n }\n let gcd2 = m / gcd;\n if n <= m / gcd2 {\n ans = std::cmp::max(gcd2, ans);\n }\n }\n gcd += 1;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0863", "problem_description": "

Capitalize

\n\n

\nWrite a program which replace all the lower-case letters of a given text with the corresponding captital letters. \n

\n\n

Input

\n\n

\nA text including lower-case letters, periods, and space is given in a line. The number of characters in the text is less than or equal to 200.\n

\n\n

Output

\n\n

\nPrint the converted text.\n

\n\n

Sample Input

\n\n
\nthis is a pen.\n
\n\n

Output for the Sample Input

\n\n
\nTHIS IS A PEN.\n
", "c_code": "int solution() {\n char str[10001];\n int i = 0;\n fgets(str, 10001, stdin);\n while (*(str + i) != '\\0') {\n if (*(str + i) >= 'a' && *(str + i) <= 'z') {\n *(str + i) += -'a' + 'A';\n }\n i++;\n }\n *(str + i - 1) = '\\0';\n puts(str);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n let chars: Vec = buf\n .trim()\n .chars()\n .map(|c| c.to_uppercase().next().unwrap())\n .collect();\n println!(\"{}\", chars.iter().collect::());\n}", "difficulty": "hard"} {"problem_id": "0864", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Decades have passed since the beginning of AtCoder Beginner Contest.

\n

The contests are labeled as ABC001, ABC002, ... from the first round, but after the 999-th round ABC999, a problem occurred: how the future rounds should be labeled?

\n

In the end, the labels for the rounds from the 1000-th to the 1998-th are decided: ABD001, ABD002, ..., ABD999.

\n

You are given an integer N between 1 and 1998 (inclusive). Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 1998
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

999\n
\n
\n
\n
\n
\n

Sample Output 1

ABC\n
\n

The 999-th round of AtCoder Beginner Contest is labeled as ABC999.

\n
\n
\n
\n
\n
\n

Sample Input 2

1000\n
\n
\n
\n
\n
\n

Sample Output 2

ABD\n
\n

The 1000-th round of AtCoder Beginner Contest is labeled as ABD001.

\n
\n
\n
\n
\n
\n

Sample Input 3

1481\n
\n
\n
\n
\n
\n

Sample Output 3

ABD\n
\n

The 1481-th round of AtCoder Beginner Contest is labeled as ABD482.

\n
\n
", "c_code": "int solution() {\n int N = 0;\n\n scanf(\"%d\", &N);\n printf((N < 1000) ? \"ABC\" : \"ABD\");\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n io::stdin().read_line(&mut n).expect(\"Failed to read line\");\n let n: u32 = n.trim().parse().expect(\"Failed to parse\");\n if n >= 1000 {\n println!(\"ABD\");\n } else {\n println!(\"ABC\");\n }\n}", "difficulty": "easy"} {"problem_id": "0865", "problem_description": "Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.", "c_code": "int solution() {\n long long num;\n long long i = 0;\n scanf(\"%lld\", &num);\n long long arr[num];\n long long max = 0;\n long long min = 1000000000;\n for (i = 0; i < num; ++i) {\n scanf(\"%lld\", &arr[i]);\n if (arr[i] > max) {\n max = arr[i];\n }\n if (arr[i] < min) {\n min = arr[i];\n }\n }\n long long countmax = 0;\n long long countmin = 0;\n for (i = 0; i < num; ++i) {\n if (arr[i] == max) {\n countmax += 1;\n }\n if (arr[i] == min) {\n countmin += 1;\n }\n }\n long long diff = max - min;\n long long ways = countmax * countmin;\n if (max == min) {\n ways = (countmax * (countmax - 1)) / 2;\n }\n printf(\"%lld %lld\", diff, ways);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let n: u32 = buf.trim().parse().unwrap();\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut s = buf.split_whitespace();\n let mut b = Vec::new();\n for _ in 0..n {\n let t: u32 = s.next().unwrap().trim().parse().unwrap();\n b.push(t);\n }\n let max = *b.iter().max().unwrap();\n let min = *b.iter().min().unwrap();\n if max != min {\n let cnt1 = b.iter().filter(|x| **x == min).count() as u64;\n let cnt2 = b.iter().filter(|x| **x == max).count() as u64;\n println!(\"{} {}\", max - min, cnt1 * cnt2);\n } else {\n let cnt = b.iter().filter(|x| **x == min).count() as u64;\n println!(\"0 {}\", cnt * (cnt - 1) / 2);\n }\n}", "difficulty": "medium"} {"problem_id": "0866", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given two integers K and S.
\nThree variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.
\nHow many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≤K≤2500
  • \n
  • 0≤S≤3K
  • \n
  • K and S are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
K S\n
\n
\n
\n
\n
\n

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

There are six triples of X, Y and Z that satisfy the condition:

\n
    \n
  • X = 0, Y = 0, Z = 2
  • \n
  • X = 0, Y = 2, Z = 0
  • \n
  • X = 2, Y = 0, Z = 0
  • \n
  • X = 0, Y = 1, Z = 1
  • \n
  • X = 1, Y = 0, Z = 1
  • \n
  • X = 1, Y = 1, Z = 0
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5 15\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.

\n
\n
", "c_code": "int solution() {\n int *k = malloc(sizeof(int));\n int *s = malloc(sizeof(int));\n int resp = 0;\n scanf(\"%d%d\", k, s);\n for (int x = 0; x <= *k; ++x) {\n for (int y = 0; y <= *k; ++y) {\n int z = (*s) - x - y;\n if (*k >= z && z >= 0) {\n ++resp;\n }\n }\n }\n printf(\"%d\\n\", resp);\n free(k);\n free(s);\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n let _ = std::io::stdin().read_line(&mut input).unwrap();\n let input = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let k = input[0] + 1;\n let s = input[1];\n let mut count = 0;\n\n for x in 0..k {\n for y in 0..k {\n let t = x + y;\n if t > s {\n break;\n }\n if (s - t) < k {\n count += 1;\n }\n }\n }\n\n println!(\"{}\", count);\n}", "difficulty": "easy"} {"problem_id": "0867", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

A robot is put at the origin in a two-dimensional plane.\nInitially, the robot is facing in the positive x-axis direction.

\n

This robot will be given an instruction sequence s.\ns consists of the following two kinds of letters, and will be executed in order from front to back.

\n
    \n
  • F : Move in the current direction by distance 1.
  • \n
  • T : Turn 90 degrees, either clockwise or counterclockwise.
  • \n
\n

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.\nDetermine whether this objective is achievable.

\n
\n
\n
\n
\n

Constraints

    \n
  • s consists of F and T.
  • \n
  • 1 \\leq |s| \\leq 8 000
  • \n
  • x and y are integers.
  • \n
  • |x|, |y| \\leq |s|
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s\nx y\n
\n
\n
\n
\n
\n

Output

If the objective is achievable, print Yes; if it is not, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

FTFFTFFF\n4 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

\n
\n
\n
\n
\n
\n

Sample Input 2

FTFFTFFF\n-2 -2\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

\n
\n
\n
\n
\n
\n

Sample Input 3

FF\n1 0\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
\n
\n
\n
\n

Sample Input 4

TF\n1 0\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
\n
\n
\n
\n

Sample Input 5

FFTTFF\n0 0\n
\n
\n
\n
\n
\n

Sample Output 5

Yes\n
\n

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

\n
\n
\n
\n
\n
\n

Sample Input 6

TTTT\n1 0\n
\n
\n
\n
\n
\n

Sample Output 6

No\n
\n
\n
", "c_code": "int solution() {\n char s[8003];\n scanf(\"%s\", s);\n int x;\n int y;\n scanf(\"%d %d\", &x, &y);\n int i;\n int j;\n int n = strlen(s);\n int v[2][2][20000];\n for (i = 0; i < 20000; i++) {\n for (j = 0; j < 2; j++) {\n v[0][j][i] = 0;\n }\n }\n v[0][0][10000] = v[1][0][10000] = 1;\n int k = 0;\n j = 0;\n int a;\n int b = 0;\n s[n] = 'T';\n for (i = 0; i <= n; i++) {\n if (s[i] == 'F') {\n j++;\n } else {\n for (a = 0; a < 20000; a++) {\n v[k][1][a] = 0;\n }\n for (a = 0; a < 20000; a++) {\n if (v[k][0][a] > 0) {\n if (b > 0) {\n v[k][1][a - j] = 1;\n }\n v[k][1][a + j] = 1;\n }\n }\n j = 0;\n for (a = 0; a < 20000; a++) {\n v[k][0][a] = v[k][1][a];\n }\n if (k == 0) {\n k = 1;\n } else {\n k = 0;\n }\n b = 1;\n }\n }\n if (v[0][0][x + 10000] > 0 && v[1][0][y + 10000] > 0) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let op = s\n .next()\n .unwrap()\n .split(\"T\")\n .map(|a| a.len())\n .collect::>();\n let x: i32 = s.next().unwrap().parse().unwrap();\n let y: i32 = s.next().unwrap().parse().unwrap();\n let mut xs = std::collections::BTreeSet::new();\n let mut ys = std::collections::BTreeSet::new();\n xs.insert(op[0] as i32);\n ys.insert(0);\n if op.len() > 1 {\n for (i, f) in op[1..].iter().enumerate() {\n let f = *f as i32;\n if f == 0 {\n continue;\n }\n if i & 1 == 0 {\n ys = ys\n .into_iter()\n .flat_map(|a| vec![a + f, a - f])\n .collect::>();\n } else {\n xs = xs\n .into_iter()\n .flat_map(|a| vec![a + f, a - f])\n .collect::>();\n }\n }\n }\n if xs.contains(&x) && ys.contains(&y) {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0868", "problem_description": "Madoka decided to participate in an underground sports programming competition. And there was exactly one task in it:A square table of size $$$n \\times n$$$, where $$$n$$$ is a multiple of $$$k$$$, is called good if only the characters '.' and 'X' are written in it, as well as in any subtable of size $$$1 \\times k$$$ or $$$k \\times 1$$$, there is at least one character 'X'. In other words, among any $$$k$$$ consecutive vertical or horizontal cells, there must be at least one containing the character 'X'.Output any good table that has the minimum possible number of characters 'X', and also the symbol 'X' is written in the cell $$$(r, c)$$$. Rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, columns are numbered from $$$1$$$ to $$$n$$$ from left to right.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n\n int n[t];\n int k[t];\n int r[t];\n int c[t];\n\n int i = 0;\n for (; i < t; i++) {\n scanf(\"%d\", &n[i]);\n scanf(\"%d\", &k[i]);\n scanf(\"%d\", &r[i]);\n scanf(\"%d\", &c[i]);\n }\n\n for (i = 0; i < t; i++) {\n r[i]--;\n c[i]--;\n int row = 0;\n int col = 0;\n\n for (int row = 0; row < n[i]; row++) {\n for (int col = 0; col < n[i]; col++) {\n if ((row - r[i] - (col - c[i])) % k[i] == 0) {\n printf(\"X\");\n } else {\n printf(\".\");\n }\n }\n printf(\"\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let std_in = stdin();\n let mut input = Scanner::new(std_in.lock());\n\n let std_out = stdout();\n let mut output = BufWriter::new(std_out.lock());\n\n let t: usize = input.token();\n for _ in 0..t {\n let (n, k, r, c): (usize, usize, usize, usize) =\n (input.token(), input.token(), input.token(), input.token());\n let (r, c) = (r - 1, c - 1);\n\n for i in 0..n {\n for j in 0..n {\n write!(\n output,\n \"{}\",\n if (i + j) % k == (r + c) % k { 'X' } else { '.' }\n )\n .unwrap();\n }\n writeln!(output).unwrap();\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0869", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given a positive integer N.\nFind the minimum positive integer divisible by both 2 and N.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the minimum positive integer divisible by both 2 and N.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

6 is divisible by both 2 and 3.\nAlso, there is no positive integer less than 6 that is divisible by both 2 and 3.\nThus, the answer is 6.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\n
\n
\n
\n
\n
\n

Sample Output 2

10\n
\n
\n
\n
\n
\n
\n

Sample Input 3

999999999\n
\n
\n
\n
\n
\n

Sample Output 3

1999999998\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n scanf(\"%d\", &N);\n if (N % 2 == 0) {\n printf(\"%d\\n\", N);\n return 0;\n }\n printf(\"%d\\n\", 2 * N);\n return 0;\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut ln = String::new();\n io::stdin().read_line(&mut ln).ok();\n let n: i32 = ln.trim().parse().unwrap();\n\n println!(\"{}\", if n % 2 == 0 { n } else { n * 2 });\n}", "difficulty": "easy"} {"problem_id": "0870", "problem_description": "You like the card board game \"Set\". Each card contains $$$k$$$ features, each of which is equal to a value from the set $$$\\{0, 1, 2\\}$$$. The deck contains all possible variants of cards, that is, there are $$$3^k$$$ different cards in total.A feature for three cards is called good if it is the same for these cards or pairwise distinct. Three cards are called a set if all $$$k$$$ features are good for them.For example, the cards $$$(0, 0, 0)$$$, $$$(0, 2, 1)$$$, and $$$(0, 1, 2)$$$ form a set, but the cards $$$(0, 2, 2)$$$, $$$(2, 1, 2)$$$, and $$$(1, 2, 0)$$$ do not, as, for example, the last feature is not good.A group of five cards is called a meta-set, if there is strictly more than one set among them. How many meta-sets there are among given $$$n$$$ distinct cards?", "c_code": "int solution() {\n int x;\n int y;\n int z;\n int w;\n int i;\n int j;\n int k;\n int a;\n int b;\n int n;\n int m;\n int t;\n int arr[1000][20];\n long long c;\n long long cnt[1000];\n long long d;\n long long nn[1000];\n\n do {\n scanf(\"%d %d\", &n, &k);\n memset(cnt, 0, sizeof(cnt));\n\n for (x = 0; x < n; x++) {\n d = 0;\n for (y = 0; y < k; y++) {\n scanf(\"%d\", &arr[x][y]);\n d = 3 * d + arr[x][y];\n }\n\n nn[x] = d;\n }\n\n for (x = 0; x < n; x++) {\n for (y = x + 1; y < n; y++) {\n d = 0;\n for (z = 0; z < k; z++) {\n if (arr[x][z] == arr[y][z]) {\n d = 3 * d + arr[x][z];\n } else {\n for (w = 0; w < 3; w++) {\n if (arr[x][z] != w && arr[y][z] != w) {\n d = 3 * d + w;\n }\n }\n }\n }\n\n for (z = 0; z < n; z++) {\n if (nn[z] == d) {\n cnt[z]++;\n }\n }\n }\n }\n\n for (x = 0; x < n; x++) {\n c += cnt[x] * (cnt[x] - 1) / 2;\n }\n\n printf(\"%lld\\n\", c);\n\n } while (0);\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n k: usize,\n c: [[i8; k]; n],\n }\n let mut map = BTreeMap::new();\n for i in 0..n {\n map.insert(&c[i], 0i64);\n }\n for i in 0..n {\n for j in 0..i {\n let mut good = vec![0; k];\n for k in 0..k {\n good[k] = if c[i][k] == c[j][k] {\n c[i][k]\n } else {\n 3 - c[i][k] - c[j][k]\n };\n }\n if let Some(count) = map.get_mut(&good) {\n *count += 1;\n }\n }\n }\n let mut ans = 0;\n for count in map.into_values() {\n ans += count * (count - 1) / 2;\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0871", "problem_description": "You are given an array of positive integers $$$a_1, a_2, \\ldots, a_n$$$.Make the product of all the numbers in the array (that is, $$$a_1 \\cdot a_2 \\cdot \\ldots \\cdot a_n$$$) divisible by $$$2^n$$$.You can perform the following operation as many times as you like: select an arbitrary index $$$i$$$ ($$$1 \\leq i \\leq n$$$) and replace the value $$$a_i$$$ with $$$a_i=a_i \\cdot i$$$. You cannot apply the operation repeatedly to a single index. In other words, all selected values of $$$i$$$ must be different.Find the smallest number of operations you need to perform to make the product of all the elements in the array divisible by $$$2^n$$$. Note that such a set of operations does not always exist.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n;\n int max = 0;\n int a;\n int ava;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a);\n\n while (a != 0) {\n if (a % 2 == 0) {\n max++;\n a /= 2;\n }\n\n else {\n break;\n }\n }\n }\n\n if (max >= n) {\n printf(\"0\\n\");\n continue;\n }\n\n a = 2, ava = max;\n\n while (1) {\n if (n / a == 0) {\n break;\n }\n\n max += n / a;\n a *= 2;\n }\n\n if (max < n) {\n printf(\"-1\\n\");\n continue;\n }\n\n int l = 0;\n int ans = 0;\n int count = 1;\n a = n / 2;\n\n while (a != 0) {\n l++;\n a /= 2;\n }\n\n a = 1;\n\n for (int i = 0; i < l; i++) {\n a *= 2;\n }\n\n while (1) {\n while (count--) {\n ava += l;\n ans++;\n\n if (ava >= n) {\n goto end;\n }\n }\n\n l -= 1;\n a /= 2;\n count = n / a - ans;\n }\n\n end:\n printf(\"%d\\n\", ans);\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 = line_iter.next().unwrap().unwrap().parse::().unwrap();\n let nums_str = line_iter.next().unwrap().unwrap();\n let nums_iter = nums_str\n .split(' ')\n .map(|num_str| num_str.parse::().unwrap());\n\n let mut init_pows_2: usize = 0;\n for num in nums_iter {\n let mut quotient = num;\n while quotient % 2 == 0 {\n init_pows_2 += 1;\n quotient /= 2;\n }\n }\n\n if init_pows_2 >= n {\n println!(\"0\");\n continue;\n }\n\n let mut rem_pows_2: usize = n - init_pows_2;\n let mut ops_required = 0;\n\n let mut counts_by_pow: Vec = Vec::with_capacity(40);\n\n let mut quotient = n;\n while quotient > 0 {\n counts_by_pow.push(quotient);\n quotient /= 2;\n }\n\n let mut already_used_pows = 0;\n let pow_count = counts_by_pow.len() - 1;\n for pow_of_2 in (1..=pow_count).rev() {\n if rem_pows_2 <= 0 {\n break;\n }\n\n let count_with_pow_of_2 = counts_by_pow[pow_of_2] - already_used_pows;\n already_used_pows += count_with_pow_of_2;\n for _ in 0..count_with_pow_of_2 {\n ops_required += 1;\n if rem_pows_2 <= pow_of_2 {\n rem_pows_2 = 0;\n break;\n } else {\n rem_pows_2 -= pow_of_2;\n }\n }\n }\n\n if rem_pows_2 > 0 {\n println!(\"-1\");\n } else {\n println!(\"{}\", ops_required);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0872", "problem_description": "You are given the array $$$a$$$ consisting of $$$n$$$ positive (greater than zero) integers.In one move, you can choose two indices $$$i$$$ and $$$j$$$ ($$$i \\ne j$$$) such that the absolute difference between $$$a_i$$$ and $$$a_j$$$ is no more than one ($$$|a_i - a_j| \\le 1$$$) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int min = 100;\n int max = 0;\n int ct[101];\n memset(ct, 0, 101 * sizeof(int));\n for (int i = 0; i < n; ++i) {\n int aa;\n scanf(\"%d\", &aa);\n a[i] = aa;\n ct[aa] = 1;\n if (min > aa) {\n min = aa;\n }\n if (max < aa) {\n max = aa;\n }\n }\n int flag = 1;\n for (int i = min + 1; i < max; ++i) {\n if (ct[i] == 0) {\n printf(\"NO\\n\");\n flag = 0;\n break;\n }\n }\n if (flag) {\n printf(\"YES\\n\");\n }\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 'outer: 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 a: 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 a.sort();\n\n for i in 0..(n - 1) {\n if a[i + 1] - a[i] > 1 {\n println!(\"NO\");\n continue 'outer;\n }\n }\n println!(\"YES\");\n }\n}", "difficulty": "medium"} {"problem_id": "0873", "problem_description": "For an array $$$[b_1, b_2, \\ldots, b_m]$$$ define its number of inversions as the number of pairs $$$(i, j)$$$ of integers such that $$$1 \\le i < j \\le m$$$ and $$$b_i>b_j$$$. Let's call array $$$b$$$ odd if its number of inversions is odd. For example, array $$$[4, 2, 7]$$$ is odd, as its number of inversions is $$$1$$$, while array $$$[2, 1, 4, 3]$$$ isn't, as its number of inversions is $$$2$$$.You are given a permutation $$$[p_1, p_2, \\ldots, p_n]$$$ of integers from $$$1$$$ to $$$n$$$ (each of them appears exactly once in the permutation). You want to split it into several consecutive subarrays (maybe just one), so that the number of the odd subarrays among them is as large as possible. What largest number of these subarrays may be odd?", "c_code": "int solution(void) {\n int testcase = 0;\n scanf(\"%i\", &testcase);\n for (int i = 0; i < testcase; i++) {\n int n = 0;\n scanf(\"%i\", &n);\n int p[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%i\", &p[i]);\n }\n\n int count = 0;\n int i = 0;\n while (i < n) {\n if (p[i] > p[i + 1]) {\n count++;\n i++;\n }\n i++;\n }\n\n printf(\"%i\\n\", count);\n }\n}", "rust_code": "fn solution() {\n let mut inputt = String::new();\n\n io::stdin().read_line(&mut inputt).expect(\"input faild\");\n\n let t: i32 = inputt.trim().parse().expect(\"not a nubmer\");\n\n for _ in 0..t as usize {\n let mut _inputn = String::new();\n\n io::stdin().read_line(&mut _inputn).expect(\"input faild\");\n\n let n: i32 = _inputn.trim().parse().expect(\"not a number\");\n\n let mut _inputa = String::new();\n\n io::stdin().read_line(&mut _inputa).expect(\"input faild\");\n\n let a: Vec = _inputa\n .split_whitespace()\n .map(|x| x.parse().expect(\"not a number \"))\n .collect();\n\n let mut cnt = a[0];\n let mut ans = 0;\n\n for _i in 0..a.len() {\n cnt = cmp::max(a[_i], cnt);\n\n if a[_i] < cnt {\n ans += 1;\n if _i + 1 != n as usize {\n cnt = a[_i + 1];\n }\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0874", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Snuke is buying a bicycle.\nThe bicycle of his choice does not come with a bell, so he has to buy one separately.

\n

He has very high awareness of safety, and decides to buy two bells, one for each hand.

\n

The store sells three kinds of bells for the price of a, b and c yen (the currency of Japan), respectively.\nFind the minimum total price of two different bells.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq a,b,c \\leq 10000
  • \n
  • a, b and c are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b c\n
\n
\n
\n
\n
\n

Output

Print the minimum total price of two different bells.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

700 600 780\n
\n
\n
\n
\n
\n

Sample Output 1

1300\n
\n
    \n
  • Buying a 700-yen bell and a 600-yen bell costs 1300 yen.
  • \n
  • Buying a 700-yen bell and a 780-yen bell costs 1480 yen.
  • \n
  • Buying a 600-yen bell and a 780-yen bell costs 1380 yen.
  • \n
\n

The minimum among these is 1300 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

10000 10000 10000\n
\n
\n
\n
\n
\n

Sample Output 2

20000\n
\n

Buying any two bells costs 20000 yen.

\n
\n
", "c_code": "int solution() {\n int price[3];\n scanf(\"%d %d %d\", &price[0], &price[1], &price[2]);\n if (price[0] >= price[1] && price[0] >= price[2]) {\n printf(\"%d\\n\", price[1] + price[2]);\n } else if (price[1] >= price[0] && price[1] >= price[2]) {\n printf(\"%d\\n\", price[0] + price[2]);\n } else {\n printf(\"%d\\n\", price[0] + price[1]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).ok();\n let v: Vec = line\n .split_whitespace()\n .map(|n| n.parse().unwrap())\n .collect();\n let a = v[0];\n let b = v[1];\n let c = v[2];\n println!(\"{}\", cmp::min(cmp::min(a + b, b + c), a + c));\n}", "difficulty": "medium"} {"problem_id": "0875", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Compute A \\times B, truncate its fractional part, and print the result as an integer.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq A \\leq 10^{15}
  • \n
  • 0 \\leq B < 10
  • \n
  • A is an integer.
  • \n
  • B is a number with two digits after the decimal point.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the answer as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

198 1.10\n
\n
\n
\n
\n
\n

Sample Output 1

217\n
\n

We have 198 \\times 1.10 = 217.8. After truncating the fractional part, we have the answer: 217.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 0.01\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1000000000000000 9.99\n
\n
\n
\n
\n
\n

Sample Output 3

9990000000000000\n
\n
\n
", "c_code": "int solution(void) {\n\n long double A = 0.0;\n long double B = 0.0;\n scanf(\"%Lf %Lf\", &A, &B);\n B = A * B;\n B = floorl(B);\n printf(\"%0.0Lf\\n\", B);\n\n return 0;\n}", "rust_code": "fn solution() {\n let (A, B) = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let mut splitted = s.split_whitespace();\n (\n splitted.next().unwrap().to_string(),\n splitted.next().unwrap().to_string(),\n )\n };\n\n let mut aup = 0;\n let mut amake = \"\".to_string();\n for c in A.chars() {\n if aup > 0 {\n aup *= 10;\n }\n if c == '.' {\n aup = 1;\n continue;\n }\n amake.push(c);\n }\n if aup == 0 {\n aup = 1;\n }\n\n let mut bup = 0;\n let mut bmake = \"\".to_string();\n for c in B.chars() {\n if bup > 0 {\n bup *= 10;\n }\n if c == '.' {\n bup = 1;\n continue;\n }\n bmake.push(c);\n }\n if bup == 0 {\n bup = 1;\n }\n\n println!(\n \"{}\",\n amake.parse::().unwrap() * bmake.parse::().unwrap() / (aup * bup)\n );\n}", "difficulty": "hard"} {"problem_id": "0876", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Nagase is a top student in high school. One day, she's analyzing some properties of special sets of positive integers.

\n

She thinks that a set S = \\{a_{1}, a_{2}, ..., a_{N}\\} of distinct positive integers is called special if for all 1 \\leq i \\leq N, the gcd (greatest common divisor) of a_{i} and the sum of the remaining elements of S is not 1.

\n

Nagase wants to find a special set of size N. However, this task is too easy, so she decided to ramp up the difficulty. Nagase challenges you to find a special set of size N such that the gcd of all elements are 1 and the elements of the set does not exceed 30000.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 20000
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Output N space-separated integers, denoting the elements of the set S. S must satisfy the following conditions :

\n
    \n
  • The elements must be distinct positive integers not exceeding 30000.
  • \n
  • The gcd of all elements of S is 1, i.e. there does not exist an integer d > 1 that divides all elements of S.
  • \n
  • S is a special set.
  • \n
\n

If there are multiple solutions, you may output any of them. The elements of S may be printed in any order. It is guaranteed that at least one solution exist under the given contraints.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

2 5 63\n
\n

\\{2, 5, 63\\} is special because gcd(2, 5 + 63) = 2, gcd(5, 2 + 63) = 5, gcd(63, 2 + 5) = 7. Also, gcd(2, 5, 63) = 1. Thus, this set satisfies all the criteria.

\n

Note that \\{2, 4, 6\\} is not a valid solution because gcd(2, 4, 6) = 2 > 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n
\n
\n
\n
\n
\n

Sample Output 2

2 5 20 63\n
\n

\\{2, 5, 20, 63\\} is special because gcd(2, 5 + 20 + 63) = 2, gcd(5, 2 + 20 + 63) = 5, gcd(20, 2 + 5 + 63) = 10, gcd(63, 2 + 5 + 20) = 9. Also, gcd(2, 5, 20, 63) = 1. Thus, this set satisfies all the criteria.

\n
\n
", "c_code": "int solution(void) {\n int N;\n scanf(\"%d\", &N);\n if (N == 3) {\n printf(\"2 5 63\");\n return 0;\n }\n if (N == 4) {\n printf(\"2 5 20 63\");\n return 0;\n }\n int po = N % 8;\n if (po == 1) {\n printf(\"30000 \");\n }\n if (po == 2) {\n printf(\"30000 29994 \");\n }\n if (po == 3) {\n printf(\"30000 29998 29996 \");\n }\n if (po == 4) {\n printf(\"30000 29998 29996 29994 \");\n }\n if (po == 5) {\n printf(\"30000 29991 29997 29998 29996 \");\n }\n if (po == 6) {\n printf(\"30000 29994 29997 29991 29998 29996 \");\n }\n if (po == 7) {\n printf(\"29990 29992 29996 29998 29991 29997 30000 \");\n }\n N -= po;\n for (int i = 1;; i++) {\n if (i % 2 == 0 || i % 3 == 0) {\n printf(\"%d \", i);\n N--;\n }\n if (N <= 0) {\n break;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let N: i64 = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n let m = std::cmp::min(5000, 2 * ((N - 2) / 2));\n let res = if N == 3 {\n vec![2, 3, 25]\n } else {\n (0..m)\n .map(|i| 6 * i + 3)\n .chain((1..(N - m + 1)).map(|i| {\n if m + i == N && i % 3 == 1 {\n 2 * (i + 2)\n } else {\n 2 * i\n }\n }))\n .collect::>()\n };\n let ans = res\n .iter()\n .map(|&x| x.to_string())\n .collect::>()\n .join(\" \");\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0877", "problem_description": "Consider a conveyor belt represented using a grid consisting of $$$n$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row from the top and the $$$j$$$-th column from the left is labelled $$$(i,j)$$$. Every cell, except $$$(n,m)$$$, has a direction R (Right) or D (Down) assigned to it. If the cell $$$(i,j)$$$ is assigned direction R, any luggage kept on that will move to the cell $$$(i,j+1)$$$. Similarly, if the cell $$$(i,j)$$$ is assigned direction D, any luggage kept on that will move to the cell $$$(i+1,j)$$$. If at any moment, the luggage moves out of the grid, it is considered to be lost. There is a counter at the cell $$$(n,m)$$$ from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell $$$(i,j)$$$, any luggage placed in this cell should eventually end up in the cell $$$(n,m)$$$. This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.", "c_code": "int solution() {\n int n = 0;\n int i = 0;\n scanf(\"%d\", &n);\n for (; i < n; i++) {\n\n int n = 0;\n int m = 0;\n scanf(\"%d %d\", &n, &m);\n int j = 0;\n int k = 0;\n int resp = 0;\n for (; j < n; j++) {\n for (k = 0; k < m; k++) {\n char a = 0;\n scanf(\" %c\", &a);\n\n if (a == 'D' && j == n - 1) {\n resp++;\n }\n if (a == 'R' && k == m - 1) {\n resp++;\n }\n }\n }\n printf(\"%d\\n\", resp);\n }\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, m): (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 mut a = Vec::with_capacity(n);\n for _ in 0..n {\n let s: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().chars().collect()\n };\n a.push(s);\n }\n\n let mut ans = 0;\n for i in 0..(n - 1) {\n if a[i][m - 1] == 'R' {\n ans += 1;\n }\n }\n for i in 0..(m - 1) {\n if a[n - 1][i] == 'D' {\n ans += 1;\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0878", "problem_description": "Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $$$1$$$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $$$3$$$ steps, and the second contains $$$4$$$ steps, she will pronounce the numbers $$$1, 2, 3, 1, 2, 3, 4$$$.You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.", "c_code": "int solution() {\n int c = 1;\n int n = 0;\n int i = 0;\n scanf(\"%d\\n\", &n);\n int a[n];\n a[0] = 0;\n for (i = 1; i <= n; i++) {\n scanf(\"%d \", &a[i]);\n if (a[i] <= a[i - 1]) {\n\n c++;\n }\n }\n\n printf(\"%d\\n\", c);\n for (i = 1; i <= n; i++) {\n scanf(\"%d \", &a[i]);\n if (a[i] <= a[i - 1]) {\n\n printf(\"%d \", a[i - 1]);\n }\n if (i == n) {\n break;\n }\n }\n printf(\" %d\", a[i]);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut in_lines = stdin.lock().lines();\n let _l: u32 = in_lines.next().unwrap().unwrap().parse().unwrap();\n let elems: Vec = in_lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|s| s.parse::().unwrap())\n .fold(Vec::new(), |mut acc, el| {\n if el == 1 {\n acc.push(1);\n } else {\n let last = acc.last_mut().unwrap();\n *last += 1;\n }\n acc\n });\n println!(\"{}\", elems.len());\n for i in elems {\n print!(\"{} \", i);\n }\n}", "difficulty": "hard"} {"problem_id": "0879", "problem_description": "While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.Given two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one of them and not a subsequence of the other.A subsequence of some string is a sequence of characters that appears in the same order in the string, The appearances don't have to be consecutive, for example, strings \"ac\", \"bc\", \"abc\" and \"a\" are subsequences of string \"abc\" while strings \"abbc\" and \"acb\" are not. The empty string is a subsequence of any string. Any string is a subsequence of itself.", "c_code": "int solution() {\n const unsigned int n = 1 + 1e5;\n char Str1[n];\n char Str2[n];\n scanf(\"%s%s\", Str1, Str2);\n unsigned int i = 0;\n unsigned int s1 = 0;\n unsigned int s2 = 0;\n while (Str1[s1] != '\\0') {\n s1++;\n }\n while (Str2[s2] != '\\0') {\n s2++;\n }\n while (Str1[i] == Str2[i] && Str1[i] != '\\0' && Str2[i] != '\\0') {\n i++;\n }\n if (Str1[i] == '\\0' && Str2[i] == '\\0') {\n printf(\"-1\");\n } else {\n printf(\"%u\", s1 > s2 ? s1 : s2);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n let mut str1 = String::new();\n let mut str2 = String::new();\n io::stdin().read_line(&mut str1).unwrap();\n io::stdin().read_line(&mut str2).unwrap();\n let bytes1 = str1.trim().as_bytes();\n let bytes2 = str2.trim().as_bytes();\n if bytes1 == bytes2 {\n println!(\"-1\");\n } else {\n println!(\"{}\", std::cmp::max(bytes1.len(), bytes2.len()));\n }\n}", "difficulty": "medium"} {"problem_id": "0880", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Given is a string S of length N.

\n

Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.

\n

More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \\leq l_1, l_2 \\leq N - len + 1 ) that satisfy the following:

\n
    \n
  • \n

    l_1 + len \\leq l_2

    \n
  • \n
  • \n

    S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)

    \n
  • \n
\n

If there is no such integer len, print 0.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 5 \\times 10^3
  • \n
  • |S| = N
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\nababa\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.\nNote that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \\leq l_2.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\nxy\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

No non-empty string satisfies the conditions.

\n
\n
\n
\n
\n
\n

Sample Input 3

13\nstrangeorange\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution() {\n int n;\n int ans = 0;\n scanf(\"%d\\n\", &n);\n char s[5005];\n int a[n][n];\n scanf(\"%s\", s);\n for (int i = 0; i < n; i++) {\n a[i][i] = n - i;\n }\n for (int i = 0; i < n; i++) {\n int j = i + 1;\n int k = 0;\n while (j < n) {\n while (j + k < n && s[i + k] == s[j + k]) {\n k++;\n }\n a[i][j] = k;\n if (k == 0) {\n j++;\n continue;\n }\n int l = 1;\n while (j + l < n && l + a[i][l + i] < k) {\n a[i][j + l] = a[i][i + l];\n l++;\n }\n j += l;\n k -= l;\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n if (a[i][j] > j - i) {\n a[i][j] = j - i;\n }\n ans = (ans < a[i][j]) ? a[i][j] : ans;\n }\n }\n printf(\"%d\\n\", ans);\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 lines_vec = lines_str.lines().collect::>();\n let n: usize = lines_vec[0].trim().parse().unwrap();\n let s_vec = lines_vec[1].trim().chars().collect::>();\n\n let mut max_len = 0;\n let mut len: Vec> = Vec::new();\n for i in 0..n {\n len.push(vec![0; i]);\n for j in 0..i {\n if s_vec[i] != s_vec[j] {\n continue;\n }\n if j == 0 {\n len[i][j] = 1;\n if max_len < len[i][j] {\n max_len = len[i][j];\n }\n } else {\n len[i][j] = len[i - 1][j - 1] + 1;\n if len[i][j] > i - j {\n len[i][j] = i - j;\n }\n if max_len < len[i][j] {\n max_len = len[i][j];\n }\n }\n }\n }\n\n println!(\"{}\", max_len);\n}", "difficulty": "medium"} {"problem_id": "0881", "problem_description": "Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way...The string $$$s$$$ he found is a binary string of length $$$n$$$ (i. e. string consists only of 0-s and 1-s).In one move he can choose two consecutive characters $$$s_i$$$ and $$$s_{i+1}$$$, and if $$$s_i$$$ is 1 and $$$s_{i + 1}$$$ is 0, he can erase exactly one of them (he can choose which one to erase but he can't erase both characters simultaneously). The string shrinks after erasing.Lee can make an arbitrary number of moves (possibly zero) and he'd like to make the string $$$s$$$ as clean as possible. He thinks for two different strings $$$x$$$ and $$$y$$$, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner.Now you should answer $$$t$$$ test cases: for the $$$i$$$-th test case, print the cleanest possible string that Lee can get by doing some number of moves.Small reminder: if we have two strings $$$x$$$ and $$$y$$$ of the same length then $$$x$$$ is lexicographically smaller than $$$y$$$ if there is a position $$$i$$$ such that $$$x_1 = y_1$$$, $$$x_2 = y_2$$$,..., $$$x_{i - 1} = y_{i - 1}$$$ and $$$x_i < y_i$$$.", "c_code": "int solution() {\n int N;\n scanf(\"%d\", &N);\n char *out[N];\n for (int k = 0; k < N; k++) {\n int n;\n scanf(\"%d\", &n);\n out[k] = (char *)malloc((n + 1) * sizeof(char));\n scanf(\"%s\", out[k]);\n int i;\n for (i = 0; i < n - 1 && out[k][i] <= out[k][i + 1]; i++) {\n ;\n }\n if (i != n - 1) {\n i = 0;\n int zeroCount = 1;\n int oneCount = 0;\n for (int j = 0; j < n && out[k][j] == '0'; j++) {\n zeroCount++;\n }\n for (int j = 0; n - 1 - j >= 0 && out[k][n - 1 - j] == '1'; j++) {\n oneCount++;\n }\n\n for (int j = 0; j < zeroCount; j++) {\n out[k][i++] = '0';\n }\n for (int j = 0; j < oneCount; j++) {\n out[k][i++] = '1';\n }\n out[k][i] = '\\0';\n }\n }\n for (int k = 0; k < N; k++) {\n printf(\"%s\\n\", out[k]);\n free(out[k]);\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 cs: Vec = lines.next().unwrap().unwrap().chars().collect();\n let mut state: u8 = 0;\n let mut rear_zeros: usize = 0;\n let mut rear_ones: usize = 0;\n for i in 0..n {\n let k = n - i - 1;\n if cs[k] == '0' {\n rear_zeros += 1;\n if state == 0 {\n state = 1;\n }\n } else {\n if state != 0 {\n break;\n }\n rear_ones += 1;\n }\n }\n state = 0;\n let mut front_zeros: usize = 0;\n let mut front_ones: usize = 0;\n for i in 0..(n - rear_zeros - rear_ones) {\n if cs[i] == '0' {\n front_zeros += 1;\n if state == 0 {\n state = 1;\n }\n } else if state != 0 {\n break;\n } else {\n front_ones += 1;\n }\n }\n let mut ans: Vec = Vec::new();\n if front_ones == 0 {\n for _ in 0..front_zeros {\n ans.push(0_u8 + b'0');\n }\n }\n if front_ones != 0 || front_zeros != 0 {\n if rear_zeros != 0 {\n ans.push(0_u8 + b'0');\n }\n } else {\n for _ in 0..rear_zeros {\n ans.push(0_u8 + b'0');\n }\n }\n for _ in 0..rear_ones {\n ans.push(1_u8 + b'0');\n }\n println!(\"{}\", String::from_utf8(ans).unwrap());\n }\n}", "difficulty": "medium"} {"problem_id": "0882", "problem_description": "It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array $$$a$$$. Several hours after starting her journey home Nastya remembered about the present. To entertain herself she decided to check, are there four different indices $$$x, y, z, w$$$ such that $$$a_x + a_y = a_z + a_w$$$.Her train has already arrived the destination, but she still hasn't found the answer. Can you help her unravel the mystery?", "c_code": "int solution() {\n static int i;\n static int j;\n static int number[5000100];\n static int sum;\n static int n;\n static int store[5000100] = {0};\n static int pos1[5000100];\n static int pos2[5000100];\n static int flag = 0;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &number[i]);\n }\n for (i = 0; i < n; i++) {\n for (j = i + 1; j < n; j++) {\n sum = number[i] + number[j];\n store[sum]++;\n if (store[sum] > 1) {\n if (i != pos1[sum] && i != pos2[sum] && j != pos1[sum] &&\n j != pos2[sum]) {\n printf(\"YES\\n\");\n printf(\"%d %d %d %d\\n\", i + 1, j + 1, pos1[sum] + 1, pos2[sum] + 1);\n return 0;\n }\n }\n\n pos1[sum] = i, pos2[sum] = j;\n }\n }\n if (flag == 0) {\n printf(\"NO\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut qntnumb = String::new();\n io::stdin().read_line(&mut qntnumb).unwrap();\n let _qntnumb: u32 = qntnumb.trim().parse().unwrap();\n\n let mut valoresdosnumeros = String::new();\n io::stdin().read_line(&mut valoresdosnumeros).unwrap();\n\n let vectornumb = valoresdosnumeros\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let mut map: HashMap = HashMap::new();\n\n let _dimxy = 0;\n for x in 0..vectornumb.len() {\n for y in 0..x {\n let dimxy = vectornumb[x] + vectornumb[y];\n if let std::collections::hash_map::Entry::Vacant(e) = map.entry(dimxy) {\n e.insert([x, y]);\n } else {\n let pair = map.get(&dimxy).unwrap();\n if pair[0] != x && pair[0] != y && pair[1] != x && pair[1] != y {\n println!(\"YES\\n{} {} {} {}\", x + 1, y + 1, 1 + pair[0], 1 + pair[1]);\n std::process::exit(0);\n }\n }\n }\n }\n\n println!(\"NO\");\n}", "difficulty": "medium"} {"problem_id": "0883", "problem_description": "Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.There are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index $$$k$$$ such that Mr. Black can exit the house after opening the first $$$k$$$ doors.We have to note that Mr. Black opened each door at most once, and in the end all doors became open.", "c_code": "int solution() {\n int size = 0;\n scanf(\"%d\", &size);\n int l_door = 0;\n int r_door = 0;\n int arr[size];\n for (int i = 0; i < size; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] == 0) {\n l_door++;\n } else {\n r_door++;\n }\n }\n for (int i = 0; i < size; i++) {\n if (arr[i] == 0) {\n l_door--;\n } else {\n r_door--;\n }\n if (l_door == 0 || r_door == 0) {\n printf(\"%d\\n\", i + 1);\n break;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"input: read line failed\");\n let amount = input\n .trim()\n .parse::()\n .expect(\"amount: parse to usize failed\");\n input.clear();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"input: read line failed\");\n let mut vec = input\n .split_whitespace()\n .map(|item| item.parse::().expect(\"item: parse to usize failed\"))\n .collect::>();\n vec.reverse();\n\n let mut zero_index = usize::default();\n let mut one_index = usize::default();\n\n for index in 0..amount {\n if vec[index] == 0 {\n zero_index = index;\n break;\n }\n }\n\n for index in 0..amount {\n if vec[index] == 1 {\n one_index = index;\n break;\n }\n }\n\n println!(\n \"{}\",\n amount\n - if zero_index > one_index {\n zero_index\n } else {\n one_index\n }\n );\n}", "difficulty": "hard"} {"problem_id": "0884", "problem_description": "You are walking through a parkway near your house. The parkway has $$$n+1$$$ benches in a row numbered from $$$1$$$ to $$$n+1$$$ from left to right. The distance between the bench $$$i$$$ and $$$i+1$$$ is $$$a_i$$$ meters.Initially, you have $$$m$$$ units of energy. To walk $$$1$$$ meter of distance, you spend $$$1$$$ unit of your energy. You can't walk if you have no energy. Also, you can restore your energy by sitting on benches (and this is the only way to restore the energy). When you are sitting, you can restore any integer amount of energy you want (if you sit longer, you restore more energy). Note that the amount of your energy can exceed $$$m$$$.Your task is to find the minimum amount of energy you have to restore (by sitting on benches) to reach the bench $$$n+1$$$ from the bench $$$1$$$ (and end your walk).You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n int n;\n int m;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int c = 0;\n scanf(\"%d %d\", &n, &m);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n c = c + a[i];\n }\n\n int e = m - c;\n if (e < 0) {\n printf(\"%d\\n\", -1 * e);\n } else {\n if (e >= 0) {\n printf(\"%d\\n\", 0);\n }\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n\n io::stdin().read_line(&mut t).unwrap();\n let t = t.trim().parse::().unwrap();\n\n for _i in 0..t {\n let mut line = String::new();\n let mut benches = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n io::stdin().read_line(&mut benches).unwrap();\n\n let m = line.split(' ').collect::>()[1]\n .trim()\n .parse::()\n .unwrap();\n let benches = benches.split(' ');\n\n let mut sum = 0;\n for bench in benches {\n sum += bench.trim().parse::().unwrap();\n }\n\n println!(\"{}\", std::cmp::max(0, sum - m));\n }\n}", "difficulty": "hard"} {"problem_id": "0885", "problem_description": "There are $$$n$$$ candies in a row, they are numbered from left to right from $$$1$$$ to $$$n$$$. The size of the $$$i$$$-th candy is $$$a_i$$$.Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).Alice makes the first move. During the first move, she will eat $$$1$$$ candy (its size is $$$a_1$$$). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.For example, if $$$n=11$$$ and $$$a=[3,1,4,1,5,9,2,6,5,3,5]$$$, then: move 1: Alice eats one candy of size $$$3$$$ and the sequence of candies becomes $$$[1,4,1,5,9,2,6,5,3,5]$$$. move 2: Alice ate $$$3$$$ on the previous move, which means Bob must eat $$$4$$$ or more. Bob eats one candy of size $$$5$$$ and the sequence of candies becomes $$$[1,4,1,5,9,2,6,5,3]$$$. move 3: Bob ate $$$5$$$ on the previous move, which means Alice must eat $$$6$$$ or more. Alice eats three candies with the total size of $$$1+4+1=6$$$ and the sequence of candies becomes $$$[5,9,2,6,5,3]$$$. move 4: Alice ate $$$6$$$ on the previous move, which means Bob must eat $$$7$$$ or more. Bob eats two candies with the total size of $$$3+5=8$$$ and the sequence of candies becomes $$$[5,9,2,6]$$$. move 5: Bob ate $$$8$$$ on the previous move, which means Alice must eat $$$9$$$ or more. Alice eats two candies with the total size of $$$5+9=14$$$ and the sequence of candies becomes $$$[2,6]$$$. move 6 (the last): Alice ate $$$14$$$ on the previous move, which means Bob must eat $$$15$$$ or more. It is impossible, so Bob eats the two remaining candies and the game ends. Print the number of moves in the game and two numbers: $$$a$$$ — the total size of all sweets eaten by Alice during the game; $$$b$$$ — the total size of all sweets eaten by Bob during the game.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int a[1000] = {0};\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int l = 0;\n int r = n - 1;\n int suml = 0;\n int sumr = 0;\n int cnt = 0;\n int ansl = 0;\n int ansr = 0;\n while (l <= r) {\n if (cnt % 2 == 0) {\n int nsuml = 0;\n while (l <= r && nsuml <= sumr) {\n nsuml += a[l++];\n }\n ansl += nsuml;\n suml = nsuml;\n } else {\n int nsumr = 0;\n while (l <= r && nsumr <= suml) {\n nsumr += a[r--];\n }\n ansr += nsumr;\n sumr = nsumr;\n }\n ++cnt;\n }\n printf(\"%d %d %d\\n\", cnt, ansl, ansr);\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input)?;\n let n = input.trim().parse().unwrap();\n for _ in 0..n {\n input.clear();\n io::stdin().read_line(&mut input)?;\n\n input.clear();\n io::stdin().read_line(&mut input)?;\n let mut candies: Vec = input\n .trim()\n .split(\" \")\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let (mut a, mut b, mut count, mut prev) = (0, 0, 0, 0);\n let mut current = true;\n while !candies.is_empty() {\n count += 1;\n let mut now = 0;\n if current {\n while (now <= prev) && !candies.is_empty() {\n now += candies[0];\n candies.remove(0);\n }\n a += now;\n prev = now;\n } else {\n while (now <= prev) && !candies.is_empty() {\n now += candies.pop().unwrap();\n }\n b += now;\n prev = now;\n }\n\n current = !current;\n }\n\n println!(\"{} {} {}\", count, a, b);\n }\n\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "0886", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Our world is one-dimensional, and ruled by two empires called Empire A and Empire B.

\n

The capital of Empire A is located at coordinate X, and that of Empire B is located at coordinate Y.

\n

One day, Empire A becomes inclined to put the cities at coordinates x_1, x_2, ..., x_N under its control, and Empire B becomes inclined to put the cities at coordinates y_1, y_2, ..., y_M under its control.

\n

If there exists an integer Z that satisfies all of the following three conditions, they will come to an agreement, but otherwise war will break out.

\n
    \n
  • X < Z \\leq Y
  • \n
  • x_1, x_2, ..., x_N < Z
  • \n
  • y_1, y_2, ..., y_M \\geq Z
  • \n
\n

Determine if war will break out.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N, M \\leq 100
  • \n
  • -100 \\leq X < Y \\leq 100
  • \n
  • -100 \\leq x_i, y_i \\leq 100
  • \n
  • x_1, x_2, ..., x_N \\neq X
  • \n
  • x_i are all different.
  • \n
  • y_1, y_2, ..., y_M \\neq Y
  • \n
  • y_i are all different.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M X Y\nx_1 x_2 ... x_N\ny_1 y_2 ... y_M\n
\n
\n
\n
\n
\n

Output

If war will break out, print War; otherwise, print No War.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2 10 20\n8 15 13\n16 22\n
\n
\n
\n
\n
\n

Sample Output 1

No War\n
\n

The choice Z = 16 satisfies all of the three conditions as follows, thus they will come to an agreement.

\n
    \n
  • X = 10 < 16 \\leq 20 = Y
  • \n
  • 8, 15, 13 < 16
  • \n
  • 16, 22 \\geq 16
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4 2 -48 -1\n-20 -35 -91 -23\n-22 66\n
\n
\n
\n
\n
\n

Sample Output 2

War\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5 3 6 8\n-10 3 1 5 -100\n100 6 14\n
\n
\n
\n
\n
\n

Sample Output 3

War\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int M = 0;\n int X = 0;\n int Y = 0;\n scanf(\"%d\", &N);\n scanf(\"%d\", &M);\n scanf(\"%d\", &X);\n scanf(\"%d\", &Y);\n int x[100];\n int y[100];\n int i = 0;\n for (i = 0; i < N; ++i) {\n scanf(\"%d\", &x[i]);\n }\n for (i = 0; i < M; ++i) {\n scanf(\"%d\", &y[i]);\n }\n\n int x_max = X;\n int y_min = Y;\n for (i = 0; i < N; ++i) {\n if (x_max < x[i]) {\n x_max = x[i];\n }\n }\n for (i = 0; i < M; ++i) {\n if (y_min > y[i]) {\n y_min = y[i];\n }\n }\n if (x_max < y_min) {\n printf(\"No War\");\n } else {\n printf(\"War\");\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 v: Vec> = Vec::new();\n\n for _ in 0..3 {\n let mut s = String::new();\n let _ = reader.read_line(&mut s);\n let tmp = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n v.push(tmp);\n }\n\n let mut ans = false;\n\n let x_max = v[1].iter().max().unwrap();\n let y_min = v[2].iter().min().unwrap();\n\n let x = v[0][2];\n let y = v[0][3];\n\n for z in x_max + 1..y_min + 1 {\n if x < z && z <= y {\n ans = true;\n break;\n }\n }\n\n println!(\"{}\", if ans { \"No War\" } else { \"War\" });\n}", "difficulty": "easy"} {"problem_id": "0887", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Alice and Bob are playing One Card Poker.
\nOne Card Poker is a two-player game using playing cards.

\n

Each card in this game shows an integer between 1 and 13, inclusive.
\nThe strength of a card is determined by the number written on it, as follows:

\n

Weak 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < 1 Strong

\n

One Card Poker is played as follows:

\n
    \n
  1. Each player picks one card from the deck. The chosen card becomes the player's hand.
  2. \n
  3. The players reveal their hands to each other. The player with the stronger card wins the game.
    \nIf their cards are equally strong, the game is drawn.
  4. \n
\n

You are watching Alice and Bob playing the game, and can see their hands.
\nThe number written on Alice's card is A, and the number written on Bob's card is B.
\nWrite a program to determine the outcome of the game.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦A≦13
  • \n
  • 1≦B≦13
  • \n
  • A and B are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print Alice if Alice will win. Print Bob if Bob will win. Print Draw if the game will be drawn.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8 6\n
\n
\n
\n
\n
\n

Sample Output 1

Alice\n
\n

8 is written on Alice's card, and 6 is written on Bob's card.\nAlice has the stronger card, and thus the output should be Alice.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 1\n
\n
\n
\n
\n
\n

Sample Output 2

Draw\n
\n

Since their cards have the same number, the game will be drawn.

\n
\n
\n
\n
\n
\n

Sample Input 3

13 1\n
\n
\n
\n
\n
\n

Sample Output 3

Bob\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n if (a == 1) {\n a = 14;\n }\n if (b == 1) {\n b = 14;\n }\n if (a < b) {\n printf(\"Bob\");\n } else if (a > b) {\n printf(\"Alice\");\n } else {\n printf(\"Draw\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let v: Vec = s\n .split_whitespace()\n .map(|e| e.parse().ok().unwrap())\n .collect();\n let a = v[0];\n let b = v[1];\n if a == b {\n println!(\"Draw\");\n } else if a == 1 {\n println!(\"Alice\");\n } else if b == 1 {\n println!(\"Bob\");\n } else if a > b {\n println!(\"Alice\");\n } else {\n println!(\"Bob\");\n }\n}", "difficulty": "easy"} {"problem_id": "0888", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N squares arranged in a row from left to right.

\n

The height of the i-th square from the left is H_i.

\n

You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.

\n

Find the maximum number of times you can move.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq H_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nH_1 H_2 ... H_N\n
\n
\n
\n
\n
\n

Output

Print the maximum number of times you can move.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n10 4 8 7 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

By landing on the third square from the left, you can move to the right twice.

\n
\n
\n
\n
\n
\n

Sample Input 2

7\n4 4 5 6 6 5 5\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

By landing on the fourth square from the left, you can move to the right three times.

\n
\n
\n
\n
\n
\n

Sample Input 3

4\n1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n int n;\n\n scanf(\"%d\", &n);\n\n int H[n];\n int count = 0;\n int max = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &H[i]);\n\n if (i != 0 && H[i - 1] >= H[i]) {\n count++;\n } else {\n if (max < count) {\n max = count;\n }\n count = 0;\n }\n }\n if (max < count) {\n max = count;\n }\n printf(\"%d\", max);\n}", "rust_code": "fn solution() {\n let mut buft = String::new();\n io::stdin().read_line(&mut buft).unwrap();\n let n: usize = buft.trim().parse().unwrap();\n buft.clear();\n io::stdin().read_line(&mut buft).unwrap();\n let hs: Vec = buft\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n let mut i: usize = 1;\n let mut ans: usize = 0;\n while i < n {\n let mut j = i;\n while hs[j] <= hs[j - 1] {\n j += 1;\n if j >= n {\n break;\n }\n }\n ans = max(ans, j - i);\n i = j + 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0889", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Given are a positive integer N and a string S of length N consisting of lowercase English letters.

\n

Determine whether the string is a concatenation of two copies of some string.\nThat is, determine whether there is a string T such that S = T + T.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • S consists of lowercase English letters.
  • \n
  • |S| = N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

If S is a concatenation of two copies of some string, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\nabcabc\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Let T = abc, and S = T + T.

\n
\n
\n
\n
\n
\n

Sample Input 2

6\nabcadc\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1\nz\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution() {\n char S[101] = {0};\n int N = 0;\n scanf(\"%d\", &N);\n scanf(\"%s\", S);\n if (N % 2 != 0) {\n printf(\"No\\n\");\n } else {\n int ret = 0;\n ret = strncmp(&S[0], &S[(N / 2)], N / 2);\n if (ret != 0) {\n printf(\"No\\n\");\n } else {\n printf(\"Yes\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut vec = Vec::new();\n for line in stdin.lock().lines() {\n vec.push(line.unwrap())\n }\n\n let n: usize = vec[0].parse().unwrap();\n let s = &vec[1];\n\n if !n.is_multiple_of(2) {\n println!(\"No\");\n return;\n }\n\n let m = n / 2;\n\n let former = &s[..m];\n let latter = &s[m..];\n\n if former == latter {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "0890", "problem_description": "

Circle

\n\n

\nWrite a program which calculates the area and circumference of a circle for given radius r.\n

\n\n

Input

\n\n

\nA real number r is given.\n

\n\n

Output

\n\n

\nPrint the area and circumference of the circle in a line. Put a single space between them. The output should not contain an absolute error greater than 10-5.\n

\n\n

Constraints

\n\n
    \n
  • 0 < r < 10000
  • \n
\n\n

Sample Input 1

\n
\n2\n
\n

Sample Output 1

\n
\n12.566371 12.566371\n
\n\n

Sample Input 2

\n
\n3\n
\n

Sample Output 2

\n
\n28.274334 18.849556\n
", "c_code": "int solution(void) {\n double r = 0;\n\n scanf(\"%lf\", &r);\n\n printf(\"%f %f\\n\", 3.141592653589 * r * r, 2 * r * 3.141592653589);\n\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n let r: f64 = vec[0].parse().unwrap_or(0.0);\n println!(\n \"{} {}\",\n r * r * std::f64::consts::PI,\n r * 2_f64 * std::f64::consts::PI\n );\n}", "difficulty": "medium"} {"problem_id": "0891", "problem_description": "Recently Vova found $$$n$$$ candy wrappers. He remembers that he bought $$$x$$$ candies during the first day, $$$2x$$$ candies during the second day, $$$4x$$$ candies during the third day, $$$\\dots$$$, $$$2^{k-1} x$$$ candies during the $$$k$$$-th day. But there is an issue: Vova remembers neither $$$x$$$ nor $$$k$$$ but he is sure that $$$x$$$ and $$$k$$$ are positive integers and $$$k > 1$$$.Vova will be satisfied if you tell him any positive integer $$$x$$$ so there is an integer $$$k>1$$$ that $$$x + 2x + 4x + \\dots + 2^{k-1} x = n$$$. It is guaranteed that at least one solution exists. Note that $$$k > 1$$$.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 scanf(\"%d\", &n);\n int s = 1;\n int p = 1;\n while (n > 0) {\n p = p * 2;\n s = s + p;\n\n if (n % s == 0) {\n printf(\"%d\\n\", n / s);\n\n break;\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = BufReader::new(io::stdin());\n let mut stdout = BufWriter::new(io::stdout());\n\n let mut buffer = String::new();\n stdin.read_line(&mut buffer).unwrap();\n let t = buffer.trim().parse().unwrap();\n for _ in 0..t {\n let mut buffer = String::new();\n stdin.read_line(&mut buffer).unwrap();\n let n: u32 = buffer.trim().parse().unwrap();\n\n let mut base: u32 = 4;\n while !n.is_multiple_of(base - 1) {\n base *= 2;\n }\n writeln!(stdout, \"{}\", n / (base - 1)).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0892", "problem_description": "For the given integer $$$n$$$ ($$$n > 2$$$) let's write down all the strings of length $$$n$$$ which contain $$$n-2$$$ letters 'a' and two letters 'b' in lexicographical (alphabetical) order.Recall that the string $$$s$$$ of length $$$n$$$ is lexicographically less than string $$$t$$$ of length $$$n$$$, if there exists such $$$i$$$ ($$$1 \\le i \\le n$$$), that $$$s_i < t_i$$$, and for any $$$j$$$ ($$$1 \\le j < i$$$) $$$s_j = t_j$$$. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.For example, if $$$n=5$$$ the strings are (the order does matter): aaabb aabab aabba abaab ababa abbaa baaab baaba babaa bbaaa It is easy to show that such a list of strings will contain exactly $$$\\frac{n \\cdot (n-1)}{2}$$$ strings.You are given $$$n$$$ ($$$n > 2$$$) and $$$k$$$ ($$$1 \\le k \\le \\frac{n \\cdot (n-1)}{2}$$$). Print the $$$k$$$-th string from the list.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 1; i <= t; i++) {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n long long int j = 1;\n while (k > (j * (j + 1)) / 2) {\n j++;\n }\n long long int c = k - ((j * (j - 1)) / 2);\n if ((n - 1 - j) > 0) {\n for (int k = 1; k <= n - 1 - j; k++) {\n printf(\"a\");\n }\n }\n printf(\"b\");\n for (int m = 1; m <= j - c; m++) {\n printf(\"a\");\n }\n printf(\"b\");\n for (int p = 1; p <= c - 1; p++) {\n printf(\"a\");\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lock = stdin.lock();\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n let t = line.trim().parse().unwrap();\n\n for _ in 0..t {\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n let mut line = line.split_whitespace();\n let n: u32 = line.next().unwrap().parse().unwrap();\n let k: u32 = line.next().unwrap().parse().unwrap();\n\n let x = ((1. + (1. + 8. * k as f32).sqrt()) / 2.).ceil() as u32;\n let mut res = String::new();\n\n let mut i = n;\n while i > x {\n res += \"a\";\n i -= 1;\n }\n\n res += \"b\";\n i -= 1;\n\n while i > k - (x - 1) * (x - 2) / 2 {\n res += \"a\";\n i -= 1;\n }\n\n res += \"b\";\n while i > 1 {\n res += \"a\";\n i -= 1;\n }\n\n println!(\"{}\", res);\n }\n}", "difficulty": "easy"} {"problem_id": "0893", "problem_description": "Polycarp had an array $$$a$$$ of $$$3$$$ positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array $$$b$$$ of $$$7$$$ integers.For example, if $$$a = \\{1, 4, 3\\}$$$, then Polycarp wrote out $$$1$$$, $$$4$$$, $$$3$$$, $$$1 + 4 = 5$$$, $$$1 + 3 = 4$$$, $$$4 + 3 = 7$$$, $$$1 + 4 + 3 = 8$$$. After sorting, he got an array $$$b = \\{1, 3, 4, 4, 5, 7, 8\\}.$$$Unfortunately, Polycarp lost the array $$$a$$$. He only has the array $$$b$$$ left. Help him to restore the array $$$a$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int a[7];\n scanf(\"%d%d%d%d%d%d%d\", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6],\n &a[7]);\n printf(\"%d %d %d\\n\", a[0], a[1], a[6] - a[1] - a[0]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t: u16 = {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n buffer.trim().parse::().unwrap()\n };\n\n for _ in 0..t {\n let a: Vec = {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect()\n };\n\n let result: (u32, u32, u32) = (a[0], a[1], a[6] - a[0] - a[1]);\n\n println!(\"{} {} {}\", result.0, result.1, result.2);\n }\n}", "difficulty": "easy"} {"problem_id": "0894", "problem_description": "Counting sort", "c_code": "int solution() {\n int i, n, l = 0;\n\n printf(\"Enter size of array = \");\n scanf(\"%d\", &n);\n\n int *a = (int *)malloc(n * sizeof(int));\n printf(\"Enter %d elements in array :\\n\", n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] > l)\n l = a[i];\n }\n\n int *b = (int *)malloc((l + 1) * sizeof(int));\n memset(b, 0, (l + 1) * sizeof(b[0]));\n\n for (i = 0; i < n; i++)\n b[a[i]]++;\n\n for (i = 0; i < (l + 1); i++) {\n if (b[i] > 0) {\n while (b[i] != 0) {\n printf(\"%d \", i);\n b[i]--;\n }\n }\n }\n\n free(a);\n free(b);\n return 0;\n}\n", "rust_code": "fn solution(arr: &mut [T], maxval: usize)\nwhere\n T: Into + From + AddAssign + Copy,\n{\n let mut occurrences: Vec = vec![0; maxval + 1];\n\n for &value in arr.iter() {\n occurrences[value.into() as usize] += 1;\n }\n\n let mut index = 0;\n let mut current = T::from(0);\n\n for &count in occurrences.iter() {\n for _ in 0..count {\n arr[index] = current;\n index += 1;\n }\n\n current += T::from(1);\n }\n}\n", "difficulty": "hard"} {"problem_id": "0895", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

\n

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

\n
    \n
  • Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.
  • \n
  • Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.
  • \n
  • Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)
  • \n
\n

At least how much MP is needed to achieve the objective?

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 8
  • \n
  • 1 \\leq C < B < A \\leq 1000
  • \n
  • 1 \\leq l_i \\leq 1000
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B C\nl_1\nl_2\n:\nl_N\n
\n
\n
\n
\n
\n

Output

Print the minimum amount of MP needed to achieve the objective.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 100 90 80\n98\n40\n30\n21\n80\n
\n
\n
\n
\n
\n

Sample Output 1

23\n
\n

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

\n
    \n
  1. Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)
  2. \n
  3. Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)
  4. \n
  5. Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)
  6. \n
  7. Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)
  8. \n
\n
\n
\n
\n
\n
\n

Sample Input 2

8 100 90 80\n100\n100\n90\n90\n90\n80\n80\n80\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

\n
\n
\n
\n
\n
\n

Sample Input 3

8 1000 800 100\n300\n333\n400\n444\n500\n555\n600\n666\n
\n
\n
\n
\n
\n

Sample Output 3

243\n
\n
\n
", "c_code": "int solution() {\n int n;\n int i;\n int j;\n int a;\n int b;\n int c;\n int min = 10000;\n int max = 1;\n scanf(\"%d%d%d%d\", &n, &a, &b, &c);\n int l[n + 1];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &l[i]);\n max *= 4;\n }\n for (i = 1; i < max; i++) {\n int anum = 0;\n int bnum = 0;\n int cnum = 0;\n int k = i;\n int mp = 0;\n for (j = 0; j < n; j++) {\n if (k % 4 == 1) {\n if (anum != 0) {\n mp += 10;\n }\n anum += l[j];\n } else if (k % 4 == 2) {\n if (bnum != 0) {\n mp += 10;\n }\n bnum += l[j];\n } else if (k % 4 == 3) {\n if (cnum != 0) {\n mp += 10;\n }\n cnum += l[j];\n }\n k /= 4;\n }\n if (anum != 0 && bnum != 0 && cnum != 0) {\n mp += abs(anum - a) + abs(bnum - b) + abs(cnum - c);\n if (mp < min) {\n min = mp;\n }\n }\n }\n printf(\"%d\\n\", min);\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 let n: usize = iter.next().unwrap().parse().unwrap();\n let a: i32 = iter.next().unwrap().parse().unwrap();\n let b: i32 = iter.next().unwrap().parse().unwrap();\n let c: i32 = iter.next().unwrap().parse().unwrap();\n let l: Vec = (0..n)\n .map(|_| iter.next().unwrap().parse().unwrap())\n .collect();\n\n let x = 4u32.pow(n as u32);\n let mut ans = std::i32::MAX;\n\n for bit in 0..x {\n let mut sumA = 0i32;\n let mut sumB = 0i32;\n let mut sumC = 0i32;\n let mut cost = 0i32;\n let mut t = bit;\n let mut cursor = 0;\n while t > 0 {\n let m = t % 4;\n if m == 1 {\n if sumA > 0 {\n cost += 10;\n }\n sumA += l[cursor];\n } else if m == 2 {\n if sumB > 0 {\n cost += 10;\n }\n sumB += l[cursor];\n } else if m == 3 {\n if sumC > 0 {\n cost += 10;\n }\n sumC += l[cursor];\n }\n t /= 4;\n cursor += 1;\n }\n if sumA == 0 || sumB == 0 || sumC == 0 {\n continue;\n }\n ans = std::cmp::min(\n ans,\n cost + (a - sumA).abs() + (b - sumB).abs() + (c - sumC).abs(),\n );\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0896", "problem_description": "Alice and Bob are playing a game. They have an array of positive integers $$$a$$$ of size $$$n$$$.Before starting the game, Alice chooses an integer $$$k \\ge 0$$$. The game lasts for $$$k$$$ stages, the stages are numbered from $$$1$$$ to $$$k$$$. During the $$$i$$$-th stage, Alice must remove an element from the array that is less than or equal to $$$k - i + 1$$$. After that, if the array is not empty, Bob must add $$$k - i + 1$$$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $$$k$$$-th stage ends and Alice hasn't lost yet, she wins.Your task is to determine the maximum value of $$$k$$$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible.", "c_code": "int solution() {\n int a[101];\n int b;\n int t;\n int n;\n scanf(\"%d\", &t);\n while (t--) {\n memset(a, 0, sizeof(a));\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &b);\n a[b]++;\n }\n int ans = 0;\n for (int k = 1; k <= n; k++) {\n int now = 0;\n int pd = 0;\n for (int i = 1; i <= k; i++) {\n now += a[i];\n if (now < k + i - 1) {\n pd = 1;\n break;\n }\n }\n if (pd == 0) {\n ans = k;\n }\n }\n printf(\"%d\\n\", ans);\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let t = buffer.trim().parse::().unwrap();\n for _ in 0..t {\n buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let n = buffer.trim().parse::().unwrap();\n\n buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let mut a: Vec = buffer\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect();\n\n a.sort();\n\n let mut max_k = 0;\n for k in (0..n.div_ceil(2) + 1).rev() {\n let mut k_ok: bool = true;\n for i in 0..k {\n if a[2 * (k - 1) - i] > k - i {\n k_ok = false;\n break;\n }\n }\n if k_ok {\n max_k = k;\n break;\n }\n }\n println!(\"{}\", max_k);\n }\n}", "difficulty": "hard"} {"problem_id": "0897", "problem_description": "Pak Chanek has a prime number$$$^\\dagger$$$ $$$n$$$. Find a prime number $$$m$$$ such that $$$n + m$$$ is not prime.$$$^\\dagger$$$ A prime number is a number with exactly $$$2$$$ factors. The first few prime numbers are $$$2,3,5,7,11,13,\\ldots$$$. In particular, $$$1$$$ is not a prime number.", "c_code": "int solution() {\n int n = 0;\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d\", &n);\n printf(\"%d\\n\", n);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let t = s.trim().parse::().unwrap();\n let mut writer = io::BufWriter::new(io::stdout());\n\n for _ in 0..t {\n s.clear();\n io::stdin().read_line(&mut s).unwrap();\n let n = s.trim().parse::().unwrap();\n let mut m = 7;\n if n != 2 {\n m = 3;\n }\n writeln!(writer, \"{}\", m).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0898", "problem_description": "

Sorting Three Numbers

\n\n

\n Write a program which reads three integers, and prints them in ascending order.\n

\n\n\n

Input

\n\n

\n Three integers separated by a single space are given in a line.\n

\n\n

Output

\n\n

\n Print the given integers in ascending order in a line. Put a single space between two integers.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ the three integers ≤ 10000
  • \n
\n\n

Sample Input 1

\n
\n3 8 1\n
\n

Sample Output 1

\n
\n1 3 8\n
", "c_code": "int solution() {\n\n int a = 0;\n int b = 0;\n int c = 0;\n\n scanf(\"%d %d %d\", &a, &b, &c);\n\n if (a <= b && b <= c) {\n printf(\"%d %d %d\\n\", a, b, c);\n } else if (a <= c && c <= b) {\n printf(\"%d %d %d\\n\", a, c, b);\n } else if (b <= c && c <= a) {\n printf(\"%d %d %d\\n\", b, c, a);\n } else if (b <= a && a <= c) {\n printf(\"%d %d %d\\n\", b, a, c);\n } else if (c <= a && a <= b) {\n printf(\"%d %d %d\\n\", c, a, b);\n } else if (c <= b && b <= a) {\n printf(\"%d %d %d\\n\", c, b, a);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n let mut line = String::new();\n\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n\n let a: u32 = vec[0].parse().unwrap_or(0);\n let b: u32 = vec[1].parse().unwrap_or(0);\n let c: u32 = vec[2].parse().unwrap_or(0);\n\n let mut v = [a, b, c];\n v.sort();\n\n println!(\"{} {} {}\", v[0], v[1], v[2]);\n}", "difficulty": "medium"} {"problem_id": "0899", "problem_description": "Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by n hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night.In case of any danger the watchman could make a fire on the hill. One watchman could see the signal of another watchman, if on the circle arc connecting the two hills there was no hill higher than any of the two. As for any two hills there are two different circle arcs connecting them, the signal was seen if the above mentioned condition was satisfied on at least one of the arcs. For example, for any two neighbouring watchmen it is true that the signal of one will be seen by the other.An important characteristics of this watch system was the amount of pairs of watchmen able to see each other's signals. You are to find this amount by the given heights of the hills.", "c_code": "int solution() {\n long num;\n long max = 0;\n long pos = 0;\n long i;\n long j;\n unsigned long long count;\n long l[1000000];\n long r[1000000];\n long arr[1000000];\n long h[1000000];\n long c[1000000];\n scanf(\"%ld\", &num);\n for (i = 0; i < num; ++i) {\n c[i] = 0;\n scanf(\"%ld\", &h[i]);\n if (h[i] > max) {\n max = h[i];\n pos = i;\n }\n }\n for (i = pos; i < num; ++i) {\n arr[count++] = h[i];\n }\n for (i = 0; i < pos; ++i) {\n arr[count++] = h[i];\n }\n\n arr[count] = max;\n\n for (i = num - 1; i >= 0; --i) {\n r[i] = i + 1;\n while (r[i] < num && arr[i] > arr[r[i]]) {\n r[i] = r[r[i]];\n }\n if (r[i] < num && arr[i] == arr[r[i]]) {\n c[i] = c[r[i]] + 1;\n r[i] = r[r[i]];\n }\n }\n\n for (i = 1; i <= num; ++i) {\n l[i] = i - 1;\n while (l[i] > 0 && arr[i] > arr[l[i]]) {\n l[i] = l[l[i]];\n }\n if (arr[i] > 0 && arr[i] == arr[l[i]]) {\n l[i] = l[l[i]];\n }\n }\n count = 0;\n\n for (i = 1; i < num; ++i) {\n if (r[i] == num && l[i] == 0) {\n count += c[i] + 1;\n } else {\n count += c[i] + 2;\n }\n }\n\n printf(\"%lld\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n let mut input = input.split_whitespace();\n\n\n let n = read!(usize);\n let mut hs = read!([u64; n]);\n\n let max = *hs.iter().max().unwrap();\n let i = hs.iter().position(|&h| h == max).unwrap();\n let xs = hs\n .iter()\n .skip(i)\n .chain(hs.iter().take(i))\n .copied()\n .collect::>();\n let mut vs = vec![0; n];\n let mut ans = 0;\n let mut i = 0;\n for &h in &xs {\n let mut v = 1;\n while i > 0 && h >= hs[i - 1] {\n i -= 1;\n ans += vs[i];\n if h == hs[i] {\n v += vs[i];\n }\n }\n if i > 0 {\n ans += 1;\n }\n vs[i] = v;\n hs[i] = h;\n i += 1;\n }\n ans += vs\n .iter()\n .take(i)\n .skip(if vs[0] == 1 { 2 } else { 1 })\n .sum::();\n\n println!(\"{:?}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0900", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. You want to distribute these $$$n$$$ integers into two groups $$$s_1$$$ and $$$s_2$$$ (groups can be empty) so that the following conditions are satisfied: For each $$$i$$$ $$$(1 \\leq i \\leq n)$$$, $$$a_i$$$ goes into exactly one group. The value $$$|sum(s_1)| - |sum(s_2)|$$$ is the maximum possible among all such ways to distribute the integers.Here $$$sum(s_1)$$$ denotes the sum of the numbers in the group $$$s_1$$$, and $$$sum(s_2)$$$ denotes the sum of the numbers in the group $$$s_2$$$.Determine the maximum possible value of $$$|sum(s_1)| - |sum(s_2)|$$$.", "c_code": "int solution() {\n int case_num = 0;\n scanf(\"%d\", &case_num);\n for (int i = 0; i < case_num; ++i) {\n int arr_len = 0;\n scanf(\"%d\", &arr_len);\n long long int arr[100000] = {0};\n long long int sum = 0;\n for (int j = 0; j < arr_len; ++j) {\n scanf(\"%lld\", &arr[j]);\n sum += arr[j];\n }\n sum >= 0 ? printf(\"%lld\\n\", sum) : printf(\"%lld\\n\", -sum);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(2)\n .step_by(2)\n .flatten()\n .for_each(|s| {\n let ans = s\n .split_ascii_whitespace()\n .flat_map(str::parse::)\n .sum::()\n .abs();\n writeln!(buf, \"{ans}\").ok();\n });\n}", "difficulty": "hard"} {"problem_id": "0901", "problem_description": "Polycarp has guessed three positive integers $$$a$$$, $$$b$$$ and $$$c$$$. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: $$$a+b$$$, $$$a+c$$$, $$$b+c$$$ and $$$a+b+c$$$.You have to guess three numbers $$$a$$$, $$$b$$$ and $$$c$$$ using given numbers. Print three guessed integers in any order.Pay attention that some given numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (it is also possible that $$$a=b=c$$$).", "c_code": "int solution() {\n double b[4];\n double s = 0;\n scanf(\"%lf %lf %lf %lf\", &b[0], &b[1], &b[2], &b[3]);\n\n for (int i = 0; i < 4; i++) {\n s += b[i];\n }\n\n for (int i = 0; i < 4; i++) {\n if ((s / 3 - b[i]) == 0) {\n continue;\n }\n printf(\"%d \", (int)((s / 3) - b[i]));\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n std::io::stdin().read_line(&mut buffer).expect(\"error!\");\n let mut nums = Vec::new();\n let mut max = 0;\n for item in buffer.split_whitespace() {\n let num: i32 = item.parse().expect(\"invalid number\");\n nums.push(num);\n max = std::cmp::max(num, max);\n }\n let mut out = String::new();\n for item in nums.into_iter() {\n if item != max {\n out.push_str(&format!(\"{} \", max - item));\n }\n }\n println!(\"{}\", out.trim());\n}", "difficulty": "hard"} {"problem_id": "0902", "problem_description": "

Bubble Sort

\n\n

\nWrite a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:\n

\n\n
\nBubbleSort(A)\n1 for i = 0 to A.length-1\n2     for j = A.length-1 downto i+1\n3         if A[j] < A[j-1]\n4             swap A[j] and A[j-1]\n
\n

\nNote that, indices for array elements are based on 0-origin.\n

\n\n

\nYour program should also print the number of swap operations defined in line 4 of the pseudocode.\n

\n\n

Input

\n\n

\nThe first line of the input includes an integer N, the number of elements in the sequence.\n

\n

\nIn the second line, N elements of the sequence are given separated by spaces characters.\n

\n\n

Output

\n\n

\nThe output consists of 2 lines. \n

\n\n

\nIn the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.\n

\n\n

\nIn the second line, please print the number of swap operations.\n

\n\n

Constraints

\n\n

\n1 ≤ N ≤ 100\n

\n\n

Sample Input 1

\n
\n5\n5 3 2 4 1\n
\n

Sample Output 1

\n
\n1 2 3 4 5\n8\n
\n\n\n
\n\n

Sample Input 2

\n
\n6\n5 2 4 6 1 3\n
\n

Sample Output 2

\n
\n1 2 3 4 5 6\n9\n
", "c_code": "int solution() {\n int n;\n int count = 0;\n scanf(\"%d\\n\", &n);\n\n int nums[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &nums[i]);\n }\n\n int flag = 1;\n while (flag) {\n flag = 0;\n for (int i = n - 1; i > -1; i--) {\n if (nums[i] < nums[i - 1]) {\n count++;\n int tmp = nums[i - 1];\n nums[i - 1] = nums[i];\n nums[i] = tmp;\n flag = 1;\n }\n }\n }\n\n for (int i = 0; i < n - 1; i++) {\n printf(\"%d \", nums[i]);\n }\n printf(\"%d\\n\", nums[n - 1]);\n printf(\"%d\\n\", count);\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).expect(\"\");\n input = String::new();\n std::io::stdin().read_line(&mut input).expect(\"\");\n let mut v: Vec = input\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let mut temp: u8;\n let mut step: u16 = 0;\n let mut sorted: bool;\n let mut first: bool = true;\n loop {\n sorted = true;\n for i in 0..v.len() - 1 {\n if v[i] > v[i + 1] {\n sorted = false;\n temp = v[i + 1];\n v[i + 1] = v[i];\n v[i] = temp;\n step += 1;\n }\n }\n if sorted {\n break;\n }\n }\n println!(\n \"{}\",\n v.iter().fold(String::new(), |a, &b| a.to_string()\n + if !first {\n \" \"\n } else {\n first = false;\n \"\"\n }\n + &b.to_string())\n );\n println!(\"{}\", step);\n}", "difficulty": "hard"} {"problem_id": "0903", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B.\nWe are interested in the sum of those N integers. How many different possible sums there are?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N,A,B ≤ 10^9
  • \n
  • A and B are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B\n
\n
\n
\n
\n
\n

Output

Print the number of the different possible sums.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 4 6\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 4 3\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 7 10\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1 3 3\n
\n
\n
\n
\n
\n

Sample Output 4

1\n
\n
\n
", "c_code": "int solution() {\n long n;\n long a;\n long b;\n scanf(\"%ld %ld %ld\", &n, &a, &b);\n long ans = 0;\n if (n <= 2 || a > b) {\n if ((n == 1 && a == b) || (n == 2 && a <= b)) {\n ans = 1;\n }\n printf(\"%ld\\n\", ans);\n return 0;\n }\n\n ans = (b - a) * (n - 2) + 1;\n\n printf(\"%ld\\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: i64 = itr.next().unwrap().parse().unwrap();\n let a: i64 = itr.next().unwrap().parse().unwrap();\n let b: i64 = itr.next().unwrap().parse().unwrap();\n\n if a > b || (n == 1 && a != b) {\n println!(\"0\")\n } else if n == 1 {\n println!(\"1\")\n } else {\n let mx = a + (n - 1) * b;\n let mn = a * (n - 1) + b;\n println!(\"{}\", mx - mn + 1);\n }\n}", "difficulty": "medium"} {"problem_id": "0904", "problem_description": "You are given a set of $$$n$$$ segments on the axis $$$Ox$$$, each segment has integer endpoints between $$$1$$$ and $$$m$$$ inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \\le l_i \\le r_i \\le m$$$) — coordinates of the left and of the right endpoints. Consider all integer points between $$$1$$$ and $$$m$$$ inclusive. Your task is to print all such points that don't belong to any segment. The point $$$x$$$ belongs to the segment $$$[l; r]$$$ if and only if $$$l \\le x \\le r$$$.", "c_code": "int solution() {\n int n = 0;\n int m = 0;\n int l[100];\n int r[100];\n int temp[100];\n int i = 0;\n int j = 0;\n int c = 1;\n int count = 0;\n scanf(\"%d %d\", &n, &m);\n for (i = 0; i < n; i++) {\n scanf(\"%d %d\", &l[i], &r[i]);\n }\n for (i = 1; i <= m; i++) {\n c = 1;\n for (j = 0; j < n; j++) {\n if (i <= r[j] && i >= l[j]) {\n c = 0;\n break;\n }\n }\n if (c == 1) {\n temp[count] = i;\n count++;\n }\n }\n\n printf(\"%d\\n\", count);\n if (count != 0) {\n for (i = 0; i < count; i++) {\n printf(\"%d \", temp[i]);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s);\n let mut si = s.split_whitespace();\n let n: u32 = si.next().unwrap().parse().unwrap();\n let m: u32 = si.next().unwrap().parse().unwrap();\n drop(si);\n let mut dots = BTreeSet::new();\n for i in 1..=m {\n dots.insert(i);\n }\n for _ in 0..n {\n let mut s = String::new();\n io::stdin().read_line(&mut s);\n let mut si = s.split_whitespace();\n let l: u32 = si.next().unwrap().parse().unwrap();\n let r: u32 = si.next().unwrap().parse().unwrap();\n for i in l..=r {\n dots.remove(&i);\n }\n }\n println!(\"{}\", dots.len());\n for i in dots {\n print!(\"{} \", i);\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "0905", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

\n
\n
\n
\n
\n

Constraints

    \n
  • |S|=3
  • \n
  • S consists of a, b and c.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

bac\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Swapping the first and second characters in bac results in abc.

\n
\n
\n
\n
\n
\n

Sample Input 2

bab\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

abc\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 4

aaa\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
", "c_code": "int solution() {\n char s[3];\n scanf(\"%s\", s);\n int a = 0;\n if (s[0] == 'a' && s[1] == 'b' && s[2] == 'c') {\n a = 1;\n }\n if (s[0] == 'a' && s[1] == 'c' && s[2] == 'b') {\n a = 1;\n }\n if (s[0] == 'b' && s[1] == 'a' && s[2] == 'c') {\n a = 1;\n }\n if (s[0] == 'b' && s[1] == 'c' && s[2] == 'a') {\n a = 1;\n }\n if (s[0] == 'c' && s[1] == 'a' && s[2] == 'b') {\n a = 1;\n }\n if (s[0] == 'c' && s[1] == 'b' && s[2] == 'a') {\n a = 1;\n }\n if (a) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut s = String::new();\n stdin.read_line(&mut s).ok();\n s.pop();\n if s == \"abc\" || s == \"acb\" || s == \"bac\" || s == \"bca\" || s == \"cab\" || s == \"cba\" {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "0906", "problem_description": "In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum.For example, for n = 4 the sum is equal to  - 1 - 2 + 3 - 4 =  - 4, because 1, 2 and 4 are 20, 21 and 22 respectively.Calculate the answer for t values of n.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n long long numbers[n];\n long long sum[n];\n for (int i = 0; i < n; ++i) {\n scanf(\"%lld\", &numbers[i]);\n sum[i] = (numbers[i] * (numbers[i] + 1)) / 2;\n }\n for (int i = 0; i < n; ++i) {\n for (long long j = 1; j <= numbers[i]; j *= 2) {\n sum[i] = sum[i] - 2 * j;\n }\n printf(\"%lld\\n\", sum[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut temp_str = String::new();\n let mut sum: i64;\n let mut two_mult;\n\n io::stdin().read_line(&mut temp_str).expect(\"\");\n\n let req_count: usize = temp_str.trim().parse().expect(\"\");\n\n let mut numbers = vec![0; req_count];\n\n for i in 0..req_count {\n let mut temp = String::new();\n\n io::stdin().read_line(&mut temp).expect(\"\");\n\n numbers[i] = temp.trim().parse().unwrap();\n\n sum = (1 + numbers[i] as i64) * numbers[i] as i64 / 2;\n\n sum -= 2;\n\n two_mult = 2;\n\n while two_mult <= numbers[i] {\n sum -= 2 * two_mult as i64;\n\n two_mult *= 2;\n }\n\n println!(\"{}\", &mut sum);\n }\n}", "difficulty": "medium"} {"problem_id": "0907", "problem_description": "A binary string is a string that consists of characters $$$0$$$ and $$$1$$$.Let $$$\\operatorname{MEX}$$$ of a binary string be the smallest digit among $$$0$$$, $$$1$$$, or $$$2$$$ that does not occur in the string. For example, $$$\\operatorname{MEX}$$$ of $$$001011$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ occur in the string at least once, $$$\\operatorname{MEX}$$$ of $$$1111$$$ is $$$0$$$, because $$$0$$$ and $$$2$$$ do not occur in the string and $$$0 < 2$$$.A binary string $$$s$$$ is given. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring — the whole string.A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.What is the minimal sum of $$$\\operatorname{MEX}$$$ of all substrings pieces can be?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char s[100001];\n scanf(\"%s\", s);\n int oc = 0;\n int zc = 0;\n int p = 0;\n int f = 0;\n for (int i = 0; s[i] != '\\0'; i++) {\n if (s[i] == '0') {\n zc++;\n } else {\n oc++;\n }\n p++;\n }\n int c1 = zc;\n int c2 = 0;\n for (int i = 0; s[i] != '\\0'; i++) {\n if (c1 > 0 && c2 > 0 && s[i] == '1') {\n f = 1;\n break;\n }\n if (s[i] == '0') {\n c1--;\n c2++;\n }\n }\n if (oc == p) {\n printf(\"0\\n\");\n } else if (zc == p || f == 0) {\n printf(\"1\\n\");\n } else {\n printf(\"2\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut string = String::new();\n stdin.read_line(&mut string).unwrap();\n for _ in 0..string.trim().parse::().unwrap() {\n string.clear();\n stdin.read_line(&mut string).unwrap();\n let last = string.rfind(\"0\");\n let first = string.find(\"0\");\n println!(\"{}\", {\n if last.is_none() {\n 0\n } else {\n if last == first {\n 1\n } else {\n if string\n .drain(first.unwrap()..last.unwrap())\n .any(|f| f == '1')\n {\n 2\n } else {\n 1\n }\n }\n }\n })\n }\n}", "difficulty": "medium"} {"problem_id": "0908", "problem_description": "It is the hard version of the problem. The only difference is that in this version $$$3 \\le k \\le n$$$.You are given a positive integer $$$n$$$. Find $$$k$$$ positive integers $$$a_1, a_2, \\ldots, a_k$$$, such that: $$$a_1 + a_2 + \\ldots + a_k = n$$$ $$$LCM(a_1, a_2, \\ldots, a_k) \\le \\frac{n}{2}$$$ Here $$$LCM$$$ is the least common multiple of numbers $$$a_1, a_2, \\ldots, a_k$$$.We can show that for given constraints the answer always exists.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n while (k > 3) {\n printf(\"1 \");\n n--;\n k--;\n }\n if (n % 2) {\n printf(\"%d %d %d \", 1, n / 2, n / 2);\n } else if (n % 4) {\n printf(\"%d %d %d \", 2, (n - 2) / 2, (n - 2) / 2);\n } else {\n printf(\"%d %d %d \", n / 4, n / 4, n / 2);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let (n, k) = scan!(usize, usize);\n for _ in 0..k - 3 {\n print!(\"{} \", 1);\n }\n let n = n - (k - 3);\n match n % 4 {\n 0 => {\n let m = n / 4;\n print!(\"{} {} {}\", 2 * m, m, m);\n }\n\n 1 => {\n let m = n / 4;\n print!(\"{} {} {}\", 2 * m, 2 * m, 1);\n }\n\n 2 => {\n let m = n / 4;\n print!(\"{} {} {}\", 2 * m, 2 * m, 2);\n }\n\n 3 => {\n let m = n / 4;\n print!(\"{} {} {}\", 2 * m + 1, 2 * m + 1, 1);\n }\n\n _ => {\n panic!(\"wtf!\");\n }\n }\n print!(\"\\n\");\n }\n}", "difficulty": "easy"} {"problem_id": "0909", "problem_description": "You are given an integer $$$n$$$. In one move, you can either multiply $$$n$$$ by two or divide $$$n$$$ by $$$6$$$ (if it is divisible by $$$6$$$ without the remainder).Your task is to find the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ or determine if it's impossible to do that.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long int n;\n scanf(\"%lld\", &n);\n long long int c3 = 0;\n long long int c2 = 0;\n long long int f = 0;\n if (n == 1) {\n printf(\"0\\n\");\n continue;\n }\n while (1) {\n if (n % 3 == 0) {\n c3++;\n n /= 3;\n }\n if (n % 2 == 0) {\n c2++;\n n /= 2;\n }\n if ((n % 2 != 0 && n % 3 != 0) && (n != 1)) {\n f = 1;\n break;\n }\n if (n == 1) {\n break;\n }\n }\n if (f == 1) {\n printf(\"-1\\n\");\n } else {\n if (c3 < c2) {\n printf(\"-1\\n\");\n } else {\n printf(\"%lld\\n\", c2 + ((c3 - c2) * 2));\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n\n for t in stdin.lock().lines().skip(1) {\n let mut n = t.unwrap().parse::().unwrap();\n let mut s = 0;\n\n while n % 6 == 0 {\n n /= 6;\n s += 1;\n }\n\n while n % 3 == 0 {\n n /= 3;\n s += 2;\n }\n\n println!(\"{}\", if n == 1 { s } else { -1 });\n }\n}", "difficulty": "easy"} {"problem_id": "0910", "problem_description": "

ロシアの旗 (Russian Flag)

\n\n\n

問題

\n

\nK 理事長はロシアで開催される IOI 2016 に合わせて旗を作ることにした.K 理事長はまず倉庫から古い旗を取り出してきた.この旗は N 行 M 列のマス目に分けられていて,それぞれのマスには白・青・赤のいずれかの色が塗られている.\n

\n\n

\nK 理事長はこの旗のいくつかのマスを塗り替えてロシアの旗にしようとしている.ただし,この問題でいうロシアの旗とは以下のようなものである.\n

\n\n
    \n
  • 上から何行か (1 行以上) のマスが全て白で塗られている.
  • \n
  • 続く何行か (1 行以上) のマスが全て青で塗られている.
  • \n
  • それ以外の行 (1 行以上) のマスが全て赤で塗られている.
  • \n
\n\n

\nK 理事長が古い旗をロシアの旗にするために塗り替える必要のあるマスの個数の最小値を求めよ.\n

\n\n

入力

\n

\n入力は 1 + N 行からなる.\n

\n\n

\n1 行目には,2 つの整数 N, M (3 ≦ N ≦ 50, 3 ≦ M ≦ 50) が空白を区切りとして書かれている.これは,旗が N 行 M 列のマス目に区切られていることを表す.\n

\n\n

\n続く N 行にはそれぞれ M 文字からなる文字列が書かれており,古い旗のマス目に塗られている色の情報を表す.N 行のうちの i 行目の j 文字目 (1 ≦ i ≦ N, 1 ≦ j ≦ M) は,古い旗のマス目の i 行目 j 列目のマスの色を表す 'W', 'B', 'R' のいずれかの文字である.\n'W' は白,'B' は青,'R' は赤を表す.\n

\n\n

出力

\n

\nK 理事長が古い旗をロシアの旗にするために塗り替える必要のあるマスの個数の最小値を 1 行で出力せよ.\n

\n\n

入出力例

\n\n\n

入力例 1

\n\n
\n4 5\nWRWRW\nBWRWB\nWRWRW\nRWBWR\n
\n\n

出力例 1

\n\n
\n11\n
\n\n\n

入力例 2

\n\n
\n6 14\nWWWWWWWWWWWWWW\nWBBBWWRRWWBBBW\nWWBWWRRRRWWBWW\nBWBWWRRRRWWBWW\nWBBWWWRRWWBBBW\nWWWWWWWWWWWWWW\n
\n\n\n\n

出力例 2

\n\n
\n44\n
\n\n

\n入出力例 1 において,古い旗には下図のように色が塗られている.\n

\n\n

\n\"fig01\"\n

\n\n

\n下図において,'X' の書かれた 11 個のマスを塗り替える.\n

\n\n

\n\"fig02\"\n

\n\n

\nこれにより下図のようなロシアの旗にすることができる.\n

\n\n

\n\"fig03\"\n

\n\n

\n11 個未満のマスを塗り替えることではロシアの旗にすることはできないため,11 を出力する.\n

\n\n\n

\n入出力例 2 においては,古い旗には下図のように色が塗られている.\n

\n\n

\n\"fig04\"\n

\n\n\n", "c_code": "int solution() {\n\n int N;\n int M;\n int S[50][3] = {0};\n int i;\n int j;\n char H[51][51];\n\n scanf(\"%d%d\", &N, &M);\n for (i = 0; i < N; i++) {\n scanf(\"%s\", H[i]);\n }\n\n for (i = 0; i < N; i++) {\n for (j = 0; j < M; j++) {\n if (H[i][j] != 'W' && i < N - 2) {\n S[i][0]++;\n }\n if (H[i][j] != 'B' && i > 0 && i < N - 1) {\n S[i][1]++;\n }\n if (H[i][j] != 'R' && i > 1) {\n S[i][2]++;\n }\n }\n if (i < N - 1) {\n S[i + 1][0] = S[i][0];\n S[i + 1][1] = S[i][1];\n S[i + 1][2] = S[i][2];\n }\n }\n\n int min = 2500;\n for (i = 0; i < N - 2; i++) {\n for (j = i + 1; j < N - 1; j++) {\n if (min > S[i][0] + S[j][1] - S[i][1] + S[N - 1][2] - S[j][2]) {\n min = S[i][0] + S[j][1] - S[i][1] + S[N - 1][2] - S[j][2];\n }\n }\n }\n\n printf(\"%d\\n\", min);\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 (n, m) = {\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 cost = lines\n .next()\n .unwrap()\n .bytes()\n .filter(|&ch| ch != b'W')\n .count();\n\n let mut wr = Vec::with_capacity(n - 2);\n wr.extend(lines.by_ref().take(n - 2).map(|l| {\n l.bytes().fold((0, 0), |(w, r), ch| match ch {\n b'W' => (w + 1, r),\n b'R' => (w, r + 1),\n _ => (w, r),\n })\n }));\n\n let mut min = usize::max_value();\n\n for i in 0..(n - 2) {\n let w: usize = wr[..i].iter().map(|&(w, _)| m - w).sum();\n\n for j in (i + 1)..(n - 1) {\n let b: usize = wr[i..j].iter().map(|&(w, r)| w + r).sum();\n let r: usize = wr[j..].iter().map(|&(_, r)| m - r).sum();\n\n let cost = w + b + r;\n if min > cost {\n min = cost;\n }\n }\n }\n cost += min;\n\n cost += lines\n .next()\n .unwrap()\n .bytes()\n .filter(|&ch| ch != b'R')\n .count();\n\n println!(\"{}\", cost);\n}", "difficulty": "medium"} {"problem_id": "0911", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo.
\nAs the name of the contest is AtCoder Beginner Contest 100, Ringo would be happy if he is given a positive integer that can be divided by 100 exactly D times.

\n

Find the N-th smallest integer that would make Ringo happy.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • D is 0, 1 or 2.
  • \n
  • N is an integer between 1 and 100 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
D N\n
\n
\n
\n
\n
\n

Output

\n

Print the N-th smallest integer that can be divided by 100 exactly D times.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

0 5\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

The integers that can be divided by 100 exactly 0 times (that is, not divisible by 100) are as follows: 1, 2, 3, 4, 5, 6, 7, ...
\nThus, the 5-th smallest integer that would make Ringo happy is 5.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 11\n
\n
\n
\n
\n
\n

Sample Output 2

1100\n
\n

The integers that can be divided by 100 exactly once are as follows: 100, 200, 300, 400, 500, 600, 700, 800, 900, 1 \\ 000, 1 \\ 100, ...
\nThus, the integer we are seeking is 1 \\ 100.

\n
\n
\n
\n
\n
\n

Sample Input 3

2 85\n
\n
\n
\n
\n
\n

Sample Output 3

850000\n
\n

The integers that can be divided by 100 exactly twice are as follows: 10 \\ 000, 20 \\ 000, 30 \\ 000, ...
\nThus, the integer we are seeking is 850 \\ 000.

\n
\n
", "c_code": "int solution() {\n int D = 0;\n int N = 0;\n\n scanf(\"%d %d\", &D, &N);\n\n switch (D) {\n case 0:\n if (N < 100) {\n printf(\"%d\\n\", 1 * N);\n } else {\n printf(\"%d\\n\", 1 * (N + 1));\n }\n break;\n case 1:\n if (N < 100) {\n printf(\"%d\\n\", 100 * N);\n } else {\n printf(\"%d\\n\", 100 * (N + 1));\n }\n break;\n case 2:\n if (N < 100) {\n printf(\"%d\\n\", 10000 * N);\n } else {\n printf(\"%d\\n\", 10000 * (N + 1));\n }\n break;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).ok();\n let mut it = input.split_whitespace();\n let a: u32 = it.next().unwrap().parse().unwrap();\n let b: u32 = it.next().unwrap().parse().unwrap();\n let base: u32 = 100;\n println!(\n \"{}\",\n if b < 100 {\n base.pow(a) * b\n } else {\n base.pow(a) * 101\n }\n );\n}", "difficulty": "hard"} {"problem_id": "0912", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Given is a number sequence A of length N.

\n

Find the number of integers i \\left(1 \\leq i \\leq N\\right) with the following property:

\n
    \n
  • For every integer j \\left(1 \\leq j \\leq N\\right) such that i \\neq j , A_j does not divide A_i.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq A_i \\leq 10^6
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 \\cdots A_N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n24 11 8 3 16\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The integers with the property are 2, 3, and 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n5 5 5 5\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Note that there can be multiple equal numbers.

\n
\n
\n
\n
\n
\n

Sample Input 3

10\n33 18 45 28 8 19 89 86 2 4\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n scanf(\"%ld\", &n);\n long size = 1000000;\n long list[size + 1];\n for (long i = 0; i <= size; i++) {\n list[i] = 0;\n }\n long a;\n for (long i = 0; i < n; i++) {\n scanf(\"%ld\", &a);\n list[a]++;\n }\n long count = 0;\n for (long i = 0; i <= size; i++) {\n if (list[i] != 0) {\n for (long j = 2; i * j <= size; j++) {\n list[i * j] = 0;\n }\n if (list[i] == 1) {\n count++;\n }\n }\n }\n printf(\"%ld\\n\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let n = buf.trim().parse::().unwrap();\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n let mut xs: Vec<_> = buf\n .split_ascii_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n assert!(xs.len() == n);\n xs.sort_by(|a, b| a.partial_cmp(b).unwrap());\n\n let mut dp = vec![true; *xs.last().unwrap() + 1];\n\n let mut i = 0;\n while i < xs.len() {\n if dp[xs[i]] {\n let x = xs[i];\n if i < xs.len() - 1 && x == xs[i + 1] {\n dp[x] = false;\n }\n let mut xx = x + x;\n while xx < dp.len() {\n dp[xx] = false;\n xx += x;\n }\n }\n i += 1;\n }\n\n println!(\n \"{}\",\n xs.iter().filter(|x| dp[**x]).collect::>().len()\n );\n}", "difficulty": "hard"} {"problem_id": "0913", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

We have a sequence of N integers: A_1, A_2, \\cdots, A_N.

\n

You can perform the following operation between 0 and K times (inclusive):

\n
    \n
  • Choose two integers i and j such that i \\neq j, each between 1 and N (inclusive). Add 1 to A_i and -1 to A_j, possibly producing a negative element.
  • \n
\n

Compute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that y = xz.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 500
  • \n
  • 1 \\leq A_i \\leq 10^6
  • \n
  • 0 \\leq K \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nA_1 A_2 \\cdots A_{N-1} A_{N}\n
\n
\n
\n
\n
\n

Output

Print the maximum possible positive integer that divides every element of A after the operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3\n8 20\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

7 will divide every element of A if, for example, we perform the following operation:

\n
    \n
  • Choose i = 2, j = 1. A becomes (7, 21).
  • \n
\n

We cannot reach the situation where 8 or greater integer divides every element of A.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 10\n3 5\n
\n
\n
\n
\n
\n

Sample Output 2

8\n
\n

Consider performing the following five operations:

\n
    \n
  • Choose i = 2, j = 1. A becomes (2, 6).
  • \n
  • Choose i = 2, j = 1. A becomes (1, 7).
  • \n
  • Choose i = 2, j = 1. A becomes (0, 8).
  • \n
  • Choose i = 2, j = 1. A becomes (-1, 9).
  • \n
  • Choose i = 1, j = 2. A becomes (0, 8).
  • \n
\n

Then, 0 = 8 \\times 0 and 8 = 8 \\times 1, so 8 divides every element of A. We cannot reach the situation where 9 or greater integer divides every element of A.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 5\n10 1 2 22\n
\n
\n
\n
\n
\n

Sample Output 3

7\n
\n
\n
\n
\n
\n
\n

Sample Input 4

8 7\n1 7 5 6 8 2 6 5\n
\n
\n
\n
\n
\n

Sample Output 4

5\n
\n
\n
", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n int i;\n int a[502];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int sum = 0;\n for (i = 0; i < n; i++) {\n sum += a[i];\n }\n int x;\n int j;\n int l;\n int count;\n int b[502];\n int p;\n int q;\n for (i = 1; i <= sqrt(sum); i++) {\n if (sum % i == 0) {\n x = sum / i;\n for (j = 0; j < n; j++) {\n b[j] = a[j] % x;\n }\n for (j = 0; j < n - 1; j++) {\n if (b[j] > b[j + 1]) {\n b[j] ^= b[j + 1];\n b[j + 1] ^= b[j];\n b[j] ^= b[j + 1];\n if (j > 0) {\n j -= 2;\n }\n }\n }\n count = 0;\n l = n - 1;\n for (j = 0; j < n; j++) {\n if (b[j] % x != 0) {\n count += b[j];\n b[j] %= x;\n while (b[j] > 0) {\n if (b[l] + b[j] < x) {\n b[l] += b[j];\n b[j] = 0;\n } else {\n b[j] -= x - b[l];\n b[l] = x;\n l--;\n }\n }\n }\n }\n if (count <= k) {\n printf(\"%d\\n\", x);\n return 0;\n }\n }\n }\n for (i = (int)sqrt(sum); i > 0; i--) {\n if (sum % i == 0) {\n x = i;\n for (j = 0; j < n; j++) {\n b[j] = a[j] % x;\n }\n for (j = 0; j < n - 1; j++) {\n if (b[j] > b[j + 1]) {\n b[j] ^= b[j + 1];\n b[j + 1] ^= b[j];\n b[j] ^= b[j + 1];\n if (j > 0) {\n j -= 2;\n }\n }\n }\n count = 0;\n l = n - 1;\n for (j = 0; j < n; j++) {\n if (b[j] % x != 0) {\n count += b[j];\n b[j] %= x;\n while (b[j] > 0) {\n if (b[l] + b[j] < x) {\n b[l] += b[j];\n b[j] = 0;\n } else {\n b[j] -= x - b[l];\n b[l] = x;\n l--;\n }\n }\n }\n }\n if (count <= k) {\n printf(\"%d\\n\", x);\n return 0;\n }\n }\n }\n}", "rust_code": "fn solution() {\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n input! {\n n: usize,\n k: i64,\n a: [i64; n],\n }\n let s: i64 = a.iter().sum();\n let mut div = vec![];\n let mut i = 1;\n while i * i <= s {\n if s % i == 0 {\n div.push(i);\n div.push(s / i);\n }\n i += 1;\n }\n div.sort();\n use std::collections::VecDeque;\n for &d in div.iter().rev() {\n let mut v = vec![];\n for &x in &a {\n v.push(x % d);\n }\n v.sort();\n let mut q: VecDeque<_> = v.into_iter().collect();\n let mut c = 0;\n let mut mv = 0i64;\n while !q.is_empty() {\n if c > 0 {\n let x = q.pop_back().unwrap();\n c += x - d;\n mv += d - x;\n } else {\n let x = q.pop_front().unwrap();\n c += x;\n mv += x;\n }\n }\n if mv / 2 <= k {\n puts!(\"{}\\n\", d);\n return;\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0914", "problem_description": "This week Arkady wanted to cook some pancakes (to follow ancient traditions) and make a problem about that. But then he remembered that one can't make a problem about stacking pancakes without working at a specific IT company, so he decided to bake the Napoleon cake instead.To bake a Napoleon cake, one has to bake $$$n$$$ dry layers first, and then put them on each other in one stack, adding some cream. Arkady started with an empty plate, and performed the following steps $$$n$$$ times: place a new cake layer on the top of the stack; after the $$$i$$$-th layer is placed, pour $$$a_i$$$ units of cream on top of the stack. When $$$x$$$ units of cream are poured on the top of the stack, top $$$x$$$ layers of the cake get drenched in the cream. If there are less than $$$x$$$ layers, all layers get drenched and the rest of the cream is wasted. If $$$x = 0$$$, no layer gets drenched. The picture represents the first test case of the example. Help Arkady determine which layers of the cake eventually get drenched when the process is over, and which don't.", "c_code": "int solution() {\n long long int t;\n scanf(\"%lld\", &t);\n for (long long int i = 0; i < t; i++) {\n long long int n;\n\n scanf(\"%lld\", &n);\n long long int a[n];\n for (long long int j = 0; j < n; j++) {\n scanf(\"%lld\", &a[j]);\n }\n long long int temp = 0;\n long long int f = 0;\n long long int ii = n - 1;\n\n while (ii >= 0) {\n if (a[ii] == 0 && f == 0) {\n ii--;\n continue;\n }\n if (a[ii] != 0 && temp < a[ii]) {\n temp = a[ii];\n f = 1;\n }\n if (f == 1 && temp > 0) {\n a[ii] = 1;\n temp--;\n }\n if (temp == 0) {\n f = 0;\n }\n ii--;\n }\n for (long long int q = 0; q < n; q++) {\n printf(\"%lld \", a[q]);\n }\n\n printf(\"\\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\n let t = it.next().unwrap();\n\n for _ in 0..t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\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 mut stack = Vec::new();\n\n for (i, a) in it.enumerate().filter(|&(_, a)| a > 0) {\n let e = i + 1;\n let mut s = e.saturating_sub(a);\n\n loop {\n match stack.pop() {\n None => {\n stack.push((s, e));\n break;\n }\n Some((s0, e0)) => {\n if s <= e0 {\n s = std::cmp::min(s0, s);\n } else {\n stack.push((s0, e0));\n stack.push((s, e));\n break;\n }\n }\n }\n }\n }\n\n let mut i = 0;\n\n for (s, e) in stack {\n for _ in i..s {\n write!(&mut output, \"0 \").unwrap();\n }\n for _ in s..e {\n write!(&mut output, \"1 \").unwrap();\n }\n i = e;\n }\n\n for _ in i..n {\n write!(&mut output, \"0 \").unwrap();\n }\n\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0915", "problem_description": "Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes. Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d\", &n);\n scanf(\"%d\", &m);\n\n int count[1001];\n memset(count, 0, sizeof(count));\n\n int a;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a);\n count[a]++;\n }\n\n int ans = 0;\n int i = 1000;\n while (i > 0 && m > 0) {\n if (count[i] == 0) {\n i--;\n continue;\n }\n\n m -= i;\n count[i]--;\n ans++;\n if (count[i] == 0) {\n i--;\n }\n }\n printf(\"%d\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut usb_num = String::new();\n io::stdin()\n .read_line(&mut usb_num)\n .expect(\"Cannot Read Input\");\n let usb_num: i32 = usb_num.trim().parse().expect(\"WHY!\");\n\n let mut size_file = String::new();\n io::stdin().read_line(&mut size_file).expect(\"WHY!\");\n let mut size_file: i32 = size_file.trim().parse().expect(\"WHY!\");\n\n let mut usb_mem: Vec = Vec::new();\n let mut c = 0;\n let mut min = 0;\n\n while c < usb_num {\n let mut temp = String::new();\n io::stdin().read_line(&mut temp).expect(\"WHY!\");\n let temp: i32 = temp.trim().parse().expect(\"WHY!\");\n usb_mem.push(temp);\n c += 1;\n }\n usb_mem.sort_by(|a, b| b.cmp(a));\n for i in &usb_mem {\n size_file -= i;\n\n if size_file > 0 {\n min += 1\n } else {\n min += 1;\n break;\n }\n }\n println!(\"{}\", min);\n}", "difficulty": "hard"} {"problem_id": "0916", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.\nThe sequence A needs to satisfy the conditions below:

\n
    \n
  • A consists of integers between X and Y (inclusive).
  • \n
  • For each 1\\leq i \\leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.
  • \n
\n

Find the maximum possible length of the sequence.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq X \\leq Y \\leq 10^{18}
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X Y\n
\n
\n
\n
\n
\n

Output

Print the maximum possible length of the sequence.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 20\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The sequence 3,6,18 satisfies the conditions.

\n
\n
\n
\n
\n
\n

Sample Input 2

25 100\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

314159265 358979323846264338\n
\n
\n
\n
\n
\n

Sample Output 3

31\n
\n
\n
", "c_code": "int solution() {\n long long int X;\n long long int Y;\n long long int count = 1;\n scanf(\"%lld %lld\", &X, &Y);\n while (X * 2 <= Y) {\n count++;\n X *= 2;\n }\n printf(\"%lld\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n\n let mut n: Vec = Vec::new();\n\n for x in vec {\n n.push(x.parse().unwrap());\n }\n\n let mut cnt: i32 = 0;\n\n let mut tmp: i64 = n[0];\n while tmp <= n[1] {\n cnt += 1;\n tmp *= 2;\n }\n\n println!(\"{}\", cnt);\n}", "difficulty": "medium"} {"problem_id": "0917", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Given are strings s and t of length N each, both consisting of lowercase English letters.

\n

Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • |S| = |T| = N
  • \n
  • S and T are strings consisting of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS T\n
\n
\n
\n
\n
\n

Output

Print the string formed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\nip cc\n
\n
\n
\n
\n
\n

Sample Output 1

icpc\n
\n
\n
\n
\n
\n
\n

Sample Input 2

8\nhmhmnknk uuuuuuuu\n
\n
\n
\n
\n
\n

Sample Output 2

humuhumunukunuku\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5\naaaaa aaaaa\n
\n
\n
\n
\n
\n

Sample Output 3

aaaaaaaaaa\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n int count1 = 0;\n int count2 = 0;\n char str1[110];\n char str2[110];\n\n scanf(\"%d%s%s\", &n, str1, str2);\n\n for (int i = 0; i < n * 2; i++) {\n if (i % 2 == 0) {\n printf(\"%c\", str1[count1++]);\n } else {\n printf(\"%c\", str2[count2++]);\n }\n }\n\n printf(\"\\n\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let mut iter = buf.split_whitespace();\n let s: Vec = iter.next().unwrap().chars().collect();\n let t: Vec = iter.next().unwrap().chars().collect();\n for i in 0..s.len() {\n print!(\"{}{}\", s[i], t[i]);\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "0918", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Takahashi is standing on a multiplication table with infinitely many rows and columns.

\n

The square (i,j) contains the integer i \\times j. Initially, Takahashi is standing at (1,1).

\n

In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).

\n

Given an integer N, find the minimum number of moves needed to reach a square that contains N.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^{12}
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of moves needed to reach a square that contains the integer N.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

(2,5) can be reached in five moves. We cannot reach a square that contains 10 in less than five moves.

\n
\n
\n
\n
\n
\n

Sample Input 2

50\n
\n
\n
\n
\n
\n

Sample Output 2

13\n
\n

(5, 10) can be reached in 13 moves.

\n
\n
\n
\n
\n
\n

Sample Input 3

10000000019\n
\n
\n
\n
\n
\n

Sample Output 3

10000000018\n
\n

Both input and output may be enormous.

\n
\n
", "c_code": "int solution(void) {\n long n = 0;\n long count = 0;\n long min = 0;\n scanf(\"%ld\", &n);\n for (int i = 1; i <= sqrt(n); i++) {\n if (n % i == 0) {\n count = (i - 1) + (n / i - 1);\n if (min > count || min == 0) {\n min = count;\n }\n }\n }\n printf(\"%ld\", min);\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 let line: Vec = buf.split_whitespace().map(|s| s.parse().unwrap()).collect();\n let N = line[0];\n let mut x = 1;\n let mut y = N;\n let mut i = 1;\n loop {\n i += 1;\n if i * i > N {\n break;\n }\n if N % i != 0 {\n continue;\n }\n x = i;\n y = N / i;\n }\n println!(\"{}\", x + y - 2)\n}", "difficulty": "medium"} {"problem_id": "0919", "problem_description": "

Reservation System

\n\n

\nThe supercomputer system L in the PCK Research Institute performs a variety of calculations upon request from external institutes, companies, universities and other entities. To use the L system, you have to reserve operation time by specifying the start and end time. No two reservation periods are allowed to overlap each other.\n

\n\n

\n Write a program to report if a new reservation overlaps with any of the existing reservations. Note that the coincidence of start and end times is not considered to constitute an overlap. All the temporal data is given as the elapsed time from the moment at which the L system starts operation.\n

\n\n

Input

\n\n

\n The input is given in the following format.\n

\n\n
\na b\nN\ns_1 f_1\ns_2 f_2\n:\ns_N f_N\n
\n\n

\nThe first line provides new reservation information, i.e., the start time a and end time b (0 ≤ a < b ≤ 1000) in integers. The second line specifies the number of existing reservations N (0 ≤ N ≤ 100). Subsequent N lines provide temporal information for the i-th reservation: start time s_i and end time f_i (0 ≤ s_i < f_i ≤ 1000) in integers. No two existing reservations overlap.\n

\n\n

Output

\n\n

\n Output \"1\" if the new reservation temporally overlaps with any of the existing ones, or \"0\" otherwise.\n

\n\n

Sample Input 1

\n\n
\n5 7\n3\n1 4\n4 5\n7 10\n
\n\n

Sample Output 1

\n
\n0\n
\n\n

Sample Input 2

\n
\n3 7\n3\n7 10\n1 4\n4 5\n
\n\n

Sample Output 2

\n
\n1\n
", "c_code": "int solution() {\n int a;\n int b;\n int s[1001];\n int f[1001];\n int n;\n int i;\n int k = 0;\n scanf(\"%d%d%d\", &a, &b, &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d%d\", &s[i], &f[i]);\n }\n for (i = 0; i < n; i++) {\n if (s[i] < a && a < f[i]) {\n k = 1;\n }\n if (s[i] < b && b < f[i]) {\n k = 1;\n }\n if (s[i] == a || f[i] == b) {\n k = 1;\n }\n if (a < s[i] && s[i] < b) {\n k = 1;\n }\n if (a < f[i] && f[i] < b) {\n k = 1;\n }\n }\n printf(\"%d\\n\", k);\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 let b: usize = iter.next().unwrap().parse().unwrap();\n let n: usize = iter.next().unwrap().parse().unwrap();\n\n for _ in 0..n {\n let aa: usize = iter.next().unwrap().parse().unwrap();\n let bb: usize = iter.next().unwrap().parse().unwrap();\n if !(a >= bb || b <= aa) {\n println!(\"{}\", 1);\n return;\n }\n }\n\n println!(\"{}\", 0);\n}", "difficulty": "medium"} {"problem_id": "0920", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

In the year 2168, AtCoder Inc., which is much larger than now, is starting a limited express train service called AtCoder Express.

\n

In the plan developed by the president Takahashi, the trains will run as follows:

\n
    \n
  • A train will run for (t_1 + t_2 + t_3 + ... + t_N) seconds.
  • \n
  • In the first t_1 seconds, a train must run at a speed of at most v_1 m/s (meters per second). Similarly, in the subsequent t_2 seconds, a train must run at a speed of at most v_2 m/s, and so on.
  • \n
\n

According to the specifications of the trains, the acceleration of a train must be always within ±1m/s^2. Additionally, a train must stop at the beginning and the end of the run.

\n

Find the maximum possible distance that a train can cover in the run.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq t_i \\leq 200
  • \n
  • 1 \\leq v_i \\leq 100
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nt_1 t_2 t_3t_N\nv_1 v_2 v_3v_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible that a train can cover in the run.
\nOutput is considered correct if its absolute difference from the judge's output is at most 10^{-3}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1\n100\n30\n
\n
\n
\n
\n
\n

Sample Output 1

2100.000000000000000\n
\n

\"

\n

The maximum distance is achieved when a train runs as follows:

\n
    \n
  • In the first 30 seconds, it accelerates at a rate of 1m/s^2, covering 450 meters.
  • \n
  • In the subsequent 40 seconds, it maintains the velocity of 30m/s, covering 1200 meters.
  • \n
  • In the last 30 seconds, it decelerates at the acceleration of -1m/s^2, covering 450 meters.
  • \n
\n

The total distance covered is 450 + 1200 + 450 = 2100 meters.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n60 50\n34 38\n
\n
\n
\n
\n
\n

Sample Output 2

2632.000000000000000\n
\n

\"

\n

The maximum distance is achieved when a train runs as follows:

\n
    \n
  • In the first 34 seconds, it accelerates at a rate of 1m/s^2, covering 578 meters.
  • \n
  • In the subsequent 26 seconds, it maintains the velocity of 34m/s, covering 884 meters.
  • \n
  • In the subsequent 4 seconds, it accelerates at a rate of 1m/s^2, covering 144 meters.
  • \n
  • In the subsequent 8 seconds, it maintains the velocity of 38m/s, covering 304 meters.
  • \n
  • In the last 38 seconds, it decelerates at the acceleration of -1m/s^2, covering 722 meters.
  • \n
\n

The total distance covered is 578 + 884 + 144 + 304 + 722 = 2632 meters.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n12 14 2\n6 2 7\n
\n
\n
\n
\n
\n

Sample Output 3

76.000000000000000\n
\n

\"

\n

The maximum distance is achieved when a train runs as follows:

\n
    \n
  • In the first 6 seconds, it accelerates at a rate of 1m/s^2, covering 18 meters.
  • \n
  • In the subsequent 2 seconds, it maintains the velocity of 6m/s, covering 12 meters.
  • \n
  • In the subsequent 4 seconds, it decelerates at the acceleration of -1m/s^2, covering 16 meters.
  • \n
  • In the subsequent 14 seconds, it maintains the velocity of 2m/s, covering 28 meters.
  • \n
  • In the last 2 seconds, it decelerates at the acceleration of -1m/s^2, covering 2 meters.
  • \n
\n

The total distance covered is 18 + 12 + 16 + 28 + 2 = 76 meters.

\n
\n
\n
\n
\n
\n

Sample Input 4

1\n9\n10\n
\n
\n
\n
\n
\n

Sample Output 4

20.250000000000000000\n
\n

\"

\n

The maximum distance is achieved when a train runs as follows:

\n
    \n
  • In the first 4.5 seconds, it accelerates at a rate of 1m/s^2, covering 10.125 meters.
  • \n
  • In the last 4.5 seconds, it decelerates at the acceleration of -1m/s^2, covering 10.125 meters.
  • \n
\n

The total distance covered is 10.125 + 10.125 = 20.25 meters.

\n
\n
\n
\n
\n
\n

Sample Input 5

10\n64 55 27 35 76 119 7 18 49 100\n29 19 31 39 27 48 41 87 55 70\n
\n
\n
\n
\n
\n

Sample Output 5

20291.000000000000\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n scanf(\"%d\", &N);\n\n int t[N];\n int v[N];\n int endtime = 0;\n int time = 0;\n\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &t[i]);\n }\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &v[i]);\n }\n for (int i = 0; i < N; i++) {\n endtime += t[i];\n }\n\n double speed[2 * endtime];\n double ans = 0;\n\n for (int i = 0; i < 2 * endtime; i++) {\n speed[i] = 60000;\n }\n for (double i = 0; i < endtime; i += 0.5) {\n double sample[N + 2];\n sample[N] = i;\n sample[N + 1] = endtime - i;\n time = 0;\n for (int j = 0; j < N; j++) {\n if (i < time) {\n sample[j] = v[j] + (time - i);\n } else if (time + t[j] < i) {\n sample[j] = v[j] + (i - (time + t[j]));\n } else {\n sample[j] = v[j];\n }\n time += t[j];\n }\n for (int j = 0; j < N + 2; j++) {\n if (speed[(int)(i * 2)] > sample[j]) {\n speed[(int)(i * 2)] = sample[j];\n }\n }\n }\n\n for (int i = 0; i < 2 * endtime; i++) {\n ans += 0.25 * (speed[i] + speed[i + 1]);\n }\n\n printf(\"%lf\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n t: [usize; n],\n v: [f64; n]\n }\n let mut ans = 0.0;\n let mut vt = 0.0;\n let mut tac = vec![0; n + 1];\n for i in 0..n {\n tac[i + 1] = tac[i] + t[i];\n }\n for i in 0..2 * t.iter().cloned().sum::() {\n let mut a = 1.0;\n for j in 0..n {\n if tac[j] * 2 > i {\n if v[j] + (tac[j] * 2 - i) as f64 / 2.0 - 0.25 < vt {\n a = -1.0;\n break;\n }\n } else if tac[j] * 2 <= i && i < tac[j + 1] * 2 && v[j] < vt + 0.5 {\n a = 0.0;\n }\n }\n if (tac[n] * 2 - i) as f64 / 2.0 - 0.25 < vt {\n a = -1.0;\n }\n ans += (2.0 * vt + 0.5 * a) / 4.0;\n vt += 0.5 * a;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0921", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given is a positive integer L.\nFind the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ L ≤ 1000
  • \n
  • L is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
L\n
\n
\n
\n
\n
\n

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.\nYour output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

1.000000000000\n
\n

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

\n

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

\n
\n
\n
\n
\n
\n

Sample Input 2

999\n
\n
\n
\n
\n
\n

Sample Output 2

36926037.000000000000\n
\n
\n
", "c_code": "int solution(void) {\n double l = 0;\n scanf(\"%lf\", &l);\n printf(\"%.6lf\", l * l * l / 27.0);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let l = s.trim().parse::().unwrap();\n let n = l / 3.;\n\n println!(\"{}\", n * n * n);\n}", "difficulty": "medium"} {"problem_id": "0922", "problem_description": "There are $$$n$$$ blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed $$$3 \\cdot n$$$. If it is impossible to find such a sequence of operations, you need to report it.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n char arr[n + 1];\n scanf(\"%s\", arr);\n int black = 0;\n int white = 0;\n for (int i = 0; i < n; ++i) {\n if (arr[i] == 'B') {\n black++;\n } else {\n white++;\n }\n }\n int flagB = 0;\n int flagW = 0;\n if (black % 2 == 0) {\n flagB = 1;\n }\n if (white % 2 == 0) {\n flagW = 1;\n }\n if (flagB == 0 && flagW == 0) {\n printf(\"-1\");\n return 0;\n }\n int index[n];\n int count = 0;\n int j = 0;\n if (flagW == 1) {\n for (int i = 0; i < n; i++) {\n if (arr[i] == 'W') {\n count++;\n arr[i] = 'B';\n (arr[i + 1] == 'W') ? (arr[i + 1] = 'B') : (arr[i + 1] = 'W');\n index[j++] = i + 1;\n }\n }\n }\n if (flagW == 0 && flagB == 1) {\n for (int i = 0; i < n; i++) {\n if (arr[i] == 'B') {\n count++;\n arr[i] = 'W';\n (arr[i + 1] == 'W') ? (arr[i + 1] = 'B') : (arr[i + 1] = 'W');\n index[j++] = i + 1;\n }\n }\n }\n printf(\"%d\\n\", count);\n for (int i = 0; i < j; i++) {\n printf(\"%d \", index[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let _N: i32 = str.trim().parse().unwrap();\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n str.trim();\n let mut w_s: i32 = 0;\n let mut b_s: i32 = 0;\n let mut del_char = 'x';\n for ch in str.chars() {\n if ch == 'W' {\n w_s += 1;\n }\n if ch == 'B' {\n b_s += 1;\n }\n }\n if w_s % 2 == 0 {\n del_char = 'W';\n }\n if b_s % 2 == 0 {\n del_char = 'B';\n }\n if del_char == 'x' {\n println!(\"-1\");\n } else {\n let mut trig: bool = false;\n let mut fin_str = String::new();\n let mut num = 0;\n let mut ind = 0;\n for ch in str.chars() {\n ind += 1;\n trig ^= ch == del_char;\n if trig {\n num += 1;\n fin_str += &format!(\"{} \", ind);\n }\n }\n println!(\"{}\", num);\n println!(\"{}\", fin_str);\n }\n}", "difficulty": "medium"} {"problem_id": "0923", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.

\n

When counting pencils in Japanese, the counter word \"本\" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of \"本\" in the phrase \"N 本\" for a positive integer N not exceeding 999 is as follows:

\n
    \n
  • hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;
  • \n
  • pon when the digit in the one's place of N is 0, 1, 6 or 8;
  • \n
  • bon when the digit in the one's place of N is 3.
  • \n
\n

Given N, print the pronunciation of \"本\" in the phrase \"N 本\".

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • N is a positive integer not exceeding 999.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

\n

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

16\n
\n
\n
\n
\n
\n

Sample Output 1

pon\n
\n

The digit in the one's place of 16 is 6, so the \"本\" in \"16 本\" is pronounced pon.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n
\n
\n
\n
\n
\n

Sample Output 2

hon\n
\n
\n
\n
\n
\n
\n

Sample Input 3

183\n
\n
\n
\n
\n
\n

Sample Output 3

bon\n
\n
\n
", "c_code": "int solution() {\n\n int num = 0;\n\n scanf(\"%d\", &num);\n\n switch (num % 10) {\n case 3:\n printf(\"bon\\n\");\n break;\n case 0:\n case 1:\n case 6:\n case 8:\n printf(\"pon\\n\");\n break;\n default:\n printf(\"hon\\n\");\n break;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let n: i64 = s.trim().parse::().unwrap() % 10;\n\n println!(\n \"{}\",\n if n == 3 {\n \"bon\"\n } else if [2, 4, 5, 7, 9].contains(&n) {\n \"hon\"\n } else {\n \"pon\"\n }\n );\n}", "difficulty": "easy"} {"problem_id": "0924", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We will say that two integer sequences of length N, x_1, x_2, ..., x_N and y_1, y_2, ..., y_N, are similar when |x_i - y_i| \\leq 1 holds for all i (1 \\leq i \\leq N).

\n

In particular, any integer sequence is similar to itself.

\n

You are given an integer N and an integer sequence of length N, A_1, A_2, ..., A_N.

\n

How many integer sequences b_1, b_2, ..., b_N are there such that b_1, b_2, ..., b_N is similar to A and the product of all elements, b_1 b_2 ... b_N, is even?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10
  • \n
  • 1 \\leq A_i \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the number of integer sequences that satisfy the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

There are seven integer sequences that satisfy the condition:

\n
    \n
  • 1, 2
  • \n
  • 1, 4
  • \n
  • 2, 2
  • \n
  • 2, 3
  • \n
  • 2, 4
  • \n
  • 3, 2
  • \n
  • 3, 4
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3\n3 3 3\n
\n
\n
\n
\n
\n

Sample Output 2

26\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1\n100\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
\n
\n
\n
\n

Sample Input 4

10\n90 52 56 71 44 8 13 30 57 84\n
\n
\n
\n
\n
\n

Sample Output 4

58921\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n long long int min = 1;\n long long int plu = 1;\n for (int p = 0; p < n; p++) {\n scanf(\"%d\", &a[p]);\n if (a[p] % 2 == 0) {\n min *= 2;\n }\n plu *= 3;\n }\n long long int ans = plu - min;\n printf(\"%lld\", ans);\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 a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let mut c = 1;\n let mut b = 1;\n for i in 0..n {\n b *= 3;\n if a[i].is_multiple_of(2) {\n c *= 2;\n }\n }\n println!(\"{}\", b - c);\n}", "difficulty": "medium"} {"problem_id": "0925", "problem_description": "A class of students wrote a multiple-choice test.There are $$$n$$$ students in the class. The test had $$$m$$$ questions, each of them had $$$5$$$ possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question $$$i$$$ worth $$$a_i$$$ points. Incorrect answers are graded with zero points.The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.", "c_code": "int solution() {\n int n = 1;\n int m = 1;\n int i = 0;\n int j = 0;\n int k = 0;\n int p = 0;\n int q = 0;\n char st[1000] = {0};\n scanf(\"%d%d\", &n, &m);\n\n int b[m][5][1];\n int c[m];\n int d[5] = {0};\n int sum = 0;\n int val = 0;\n\n for (j = 0; j < m; j++) {\n b[j][0][0] = 0;\n b[j][1][0] = 0;\n b[j][2][0] = 0;\n b[j][3][0] = 0;\n b[j][4][0] = 0;\n }\n\n for (i = 0; i < n; i++) {\n scanf(\"%s\", st);\n for (j = 0; j < m; j++) {\n if (st[j] == 'A') {\n b[j][0][0]++;\n } else if (st[j] == 'B') {\n b[j][1][0]++;\n } else if (st[j] == 'C') {\n b[j][2][0]++;\n } else if (st[j] == 'D') {\n b[j][3][0]++;\n } else if (st[j] == 'E') {\n b[j][4][0]++;\n }\n }\n for (j = 0; j < m; j++) {\n d[0] = b[j][0][0];\n d[1] = b[j][1][0];\n d[2] = b[j][2][0];\n d[3] = b[j][3][0];\n d[4] = b[j][4][0];\n\n for (p = 0; p < 4; p++) {\n for (q = 0; q < 4; q++) {\n if (d[q] < d[q + 1]) {\n val = d[q];\n d[q] = d[q + 1];\n d[q + 1] = val;\n }\n }\n }\n c[j] = d[0];\n }\n }\n for (i = 0; i < m; i++) {\n scanf(\"%d\", &val);\n sum += val * c[i];\n }\n printf(\"%d\", sum);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let stdin_lock = stdin.lock();\n let mut student_count = 0;\n let mut question_count = 0;\n let mut answers = Vec::>::new();\n let mut points = Vec::::new();\n for (line_count, line) in stdin_lock.lines().enumerate() {\n let line = line.unwrap();\n if line_count == 0 {\n let mut splitted = line.split_whitespace();\n student_count = usize::from_str(splitted.next().unwrap()).unwrap();\n question_count = usize::from_str(splitted.next().unwrap()).unwrap();\n } else if line_count > 0 && line_count <= student_count {\n answers.push(line.as_bytes().to_vec());\n } else {\n for s in line.split_whitespace() {\n points.push(u32::from_str(s).unwrap());\n }\n break;\n }\n }\n\n let mut out = 0;\n\n for i in 0..question_count {\n let mut v = [0u32; 5];\n for a in answers.iter() {\n match a[i] {\n b'A' => v[0] += 1,\n b'B' => v[1] += 1,\n b'C' => v[2] += 1,\n b'D' => v[3] += 1,\n b'E' => v[4] += 1,\n _ => {\n unreachable!()\n }\n }\n }\n v.sort();\n out += v[4] * points[i];\n }\n println!(\"{}\", out);\n}", "difficulty": "medium"} {"problem_id": "0926", "problem_description": "Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one person — that person gets 3 points, if a word was written by two people — each of the two gets 1 point, if a word was written by all — nobody gets any points. In the end, how many points does each player have?", "c_code": "int solution() {\n int numSets = 0;\n scanf(\"%d\", &numSets);\n\n#define MAX_WORDS 1000\n\n int first[MAX_WORDS];\n int second[MAX_WORDS];\n int third[MAX_WORDS];\n\n#define AMOUNT_OF_WORDS_OF_LENGTH_3 17576\n\n int hystogram[AMOUNT_OF_WORDS_OF_LENGTH_3];\n\n for (int currentSet = 0; currentSet < numSets; ++currentSet) {\n int numWords = 0;\n scanf(\"%d\", &numWords);\n\n for (int i = 0; i < AMOUNT_OF_WORDS_OF_LENGTH_3; ++i) {\n hystogram[i] = 0;\n }\n\n for (int i = 0; i < numWords; ++i) {\n char symbols[4];\n int word = 0;\n scanf(\" %s\", symbols);\n word = (symbols[0] - 'a');\n word = 26 * word + (symbols[1] - 'a');\n word = 26 * word + (symbols[2] - 'a');\n ++hystogram[word];\n first[i] = word;\n }\n\n for (int i = 0; i < numWords; ++i) {\n char symbols[4];\n int word = 0;\n scanf(\" %s\", symbols);\n word = (symbols[0] - 'a');\n word = 26 * word + (symbols[1] - 'a');\n word = 26 * word + (symbols[2] - 'a');\n ++hystogram[word];\n second[i] = word;\n }\n\n for (int i = 0; i < numWords; ++i) {\n char symbols[4];\n int word = 0;\n scanf(\" %s\", symbols);\n word = (symbols[0] - 'a');\n word = 26 * word + (symbols[1] - 'a');\n word = 26 * word + (symbols[2] - 'a');\n ++hystogram[word];\n third[i] = word;\n }\n\n int firstScore = 0;\n for (int i = 0; i < numWords; ++i) {\n int histElem = hystogram[first[i]];\n switch (histElem) {\n case 1: {\n firstScore += 3;\n break;\n }\n case 2: {\n firstScore += 1;\n break;\n }\n }\n }\n\n int secondScore = 0;\n for (int i = 0; i < numWords; ++i) {\n int histElem = hystogram[second[i]];\n switch (histElem) {\n case 1: {\n secondScore += 3;\n break;\n }\n case 2: {\n secondScore += 1;\n break;\n }\n }\n }\n\n int thirdScore = 0;\n for (int i = 0; i < numWords; ++i) {\n int histElem = hystogram[third[i]];\n switch (histElem) {\n case 1: {\n thirdScore += 3;\n break;\n }\n case 2: {\n thirdScore += 1;\n break;\n }\n }\n }\n\n printf(\"%d %d %d\\n\", firstScore, secondScore, thirdScore);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n for _i in 0..t {\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 s1: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.trim().to_string())\n .collect()\n };\n let s2: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.trim().to_string())\n .collect()\n };\n let s3: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.trim().to_string())\n .collect()\n };\n let mut counter = HashMap::new();\n for i in 0..n {\n *counter.entry(&s1[i]).or_insert(0) += 1;\n *counter.entry(&s2[i]).or_insert(0) += 1;\n *counter.entry(&s3[i]).or_insert(0) += 1;\n }\n let mut p1 = 0;\n let mut p2 = 0;\n let mut p3 = 0;\n for s in &s1 {\n let cnt = counter[s];\n if cnt == 1 {\n p1 += 3;\n } else if cnt == 2 {\n p1 += 1;\n }\n }\n for s in &s2 {\n let cnt = counter[s];\n if cnt == 1 {\n p2 += 3;\n } else if cnt == 2 {\n p2 += 1;\n }\n }\n for s in &s3 {\n let cnt = counter[s];\n if cnt == 1 {\n p3 += 3;\n } else if cnt == 2 {\n p3 += 1;\n }\n }\n println!(\"{} {} {}\", p1, p2, p3);\n }\n}", "difficulty": "hard"} {"problem_id": "0927", "problem_description": "\n

Score : 1000 points

\n
\n
\n

Problem Statement

\n

There are N balls and N+1 holes in a line. Balls are numbered 1 through N from left to right. Holes are numbered 1 through N+1 from left to right. The i-th ball is located between the i-th hole and (i+1)-th hole. We denote the distance between neighboring items (one ball and one hole) from left to right as d_i (1 \\leq i \\leq 2 \\times N). You are given two parameters d_1 and x. d_i - d_{i-1} is equal to x for all i (2 \\leq i \\leq 2 \\times N).

\n

We want to push all N balls into holes one by one. When a ball rolls over a hole, the ball will drop into the hole if there is no ball in the hole yet. Otherwise, the ball will pass this hole and continue to roll. (In any scenario considered in this problem, balls will never collide.)

\n

In each step, we will choose one of the remaining balls uniformly at random and then choose a direction (either left or right) uniformly at random and push the ball in this direction. Please calculate the expected total distance rolled by all balls during this process.

\n

For example, when N = 3, d_1 = 1, and x = 1, the following is one possible scenario:

\n
\n\"c9264131788434ac062635a675a785e3.jpg\"\n
\n
    \n
  • first step: push the ball numbered 2 to its left, it will drop into the hole numbered 2. The distance rolled is 3.
  • \n
  • second step: push the ball numbered 1 to its right, it will pass the hole numbered 2 and drop into the hole numbered 3. The distance rolled is 9.
  • \n
  • third step: push the ball numbered 3 to its right, it will drop into the hole numbered 4. The distance rolled is 6.
  • \n
\n

So the total distance in this scenario is 18.

\n

Note that in all scenarios every ball will drop into some hole and there will be a hole containing no ball in the end.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 200,000
  • \n
  • 1 \\leq d_1 \\leq 100
  • \n
  • 0 \\leq x \\leq 100
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N d_1 x\n
\n
\n
\n
\n
\n

Output

Print a floating number denoting the answer. The relative or absolute error of your answer should not be higher than 10^{-9}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 3 3\n
\n
\n
\n
\n
\n

Sample Output 1

4.500000000000000\n
\n

The distance between the only ball and the left hole is 3 and distance between the only ball and the right hole is 6. There are only two scenarios (i.e. push left and push right). The only ball will roll 3 and 6 unit distance, respectively. So the answer for this example is (3+6)/2 = 4.5.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 1 0\n
\n
\n
\n
\n
\n

Sample Output 2

2.500000000000000\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1000 100 100\n
\n
\n
\n
\n
\n

Sample Output 3

649620280.957660079002380\n
\n
\n
", "c_code": "int solution() {\n int N;\n int i;\n double ans = 0;\n scanf(\"%d\", &N);\n double *d1 = (double *)malloc(sizeof(double) * (N + 1));\n double *x = (double *)malloc(sizeof(double) * (N + 1));\n scanf(\"%lf%lf\", &d1[N], &x[N]);\n for (i = N; i > 1; i--) {\n d1[i - 1] = (2 * (i + 1) * d1[i] + 5 * x[i]) / (2 * i);\n x[i - 1] = ((i + 2) * x[i]) / i;\n }\n for (i = 1; i <= N; i++) {\n ans += d1[i] + ((2 * i - 1) * x[i]) / 2;\n }\n printf(\"%.12lf\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n mut d: f64,\n mut x: f64\n }\n let mut ans = 0.0;\n for i in 0..n {\n ans += d + ((n - i) as f64 - 0.5) * x;\n d = (1.0 + 1.0 / (n - i) as f64) * d + 5.0 * x / 2.0 / (n - i) as f64;\n x *= 1.0 + 2.0 / (n - i) as f64;\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "0928", "problem_description": "Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell $$$(0, 0)$$$ on an infinite grid.You also have the sequence of instructions of this robot. It is written as the string $$$s$$$ consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell $$$(x, y)$$$ right now, he can move to one of the adjacent cells (depending on the current instruction). If the current instruction is 'L', then the robot can move to the left to $$$(x - 1, y)$$$; if the current instruction is 'R', then the robot can move to the right to $$$(x + 1, y)$$$; if the current instruction is 'U', then the robot can move to the top to $$$(x, y + 1)$$$; if the current instruction is 'D', then the robot can move to the bottom to $$$(x, y - 1)$$$. You've noticed the warning on the last page of the manual: if the robot visits some cell (except $$$(0, 0)$$$) twice then it breaks.So the sequence of instructions is valid if the robot starts in the cell $$$(0, 0)$$$, performs the given instructions, visits no cell other than $$$(0, 0)$$$ two or more times and ends the path in the cell $$$(0, 0)$$$. Also cell $$$(0, 0)$$$ should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: \"UD\", \"RL\", \"UUURULLDDDDLDDRRUU\", and the following are considered invalid: \"U\" (the endpoint is not $$$(0, 0)$$$) and \"UUDD\" (the cell $$$(0, 1)$$$ is visited twice).The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move. Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).You have to answer $$$q$$$ independent test cases.", "c_code": "int solution() {\n int t;\n\n scanf(\"%d\", &t);\n\n while (t--) {\n\n char s[100001];\n\n int U = 0;\n int D = 0;\n int L = 0;\n int R = 0;\n scanf(\"%s\", s);\n\n for (int i = 0; s[i] != '\\0'; ++i) {\n if (s[i] == 'U') {\n U++;\n } else if (s[i] == 'D') {\n D++;\n } else if (s[i] == 'L') {\n L++;\n } else if (s[i] == 'R') {\n R++;\n }\n }\n\n if ((!U || !D) && (!L || !R)) {\n printf(\"0\\n\");\n }\n\n else if (((!U || !D) && (L && R)) || ((U && D) && (!L || !R))) {\n printf(\"2\\n\");\n if (L && R) {\n printf(\"LR\\n\");\n } else {\n printf(\"UD\\n\");\n }\n } else {\n U = U < D ? U : D;\n D = U;\n L = L < R ? L : R;\n R = L;\n\n printf(\"%d\\n\", (2 * U) + (2 * L));\n\n while (U || L || R || D) {\n\n if (U) {\n printf(\"U\");\n U--;\n } else if (L) {\n printf(\"L\");\n L--;\n } else if (D) {\n printf(\"D\");\n D--;\n } else if (R) {\n printf(\"R\");\n R--;\n }\n }\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _b1 = stdin().read_line(&mut str).unwrap();\n let N: i32 = str.trim().parse().unwrap();\n for _test in 0..N {\n let mut str = String::new();\n let _b1 = stdin().read_line(&mut str).unwrap();\n let mut lr = [0, 0];\n let mut ud = [0, 0];\n for letter in str.chars() {\n match letter {\n 'L' => lr[0] += 1,\n 'R' => lr[1] += 1,\n 'U' => ud[0] += 1,\n 'D' => ud[1] += 1,\n _ => break,\n }\n }\n let m: i64 = *lr.iter().min().unwrap();\n let M: i64 = *ud.iter().min().unwrap();\n if M + m == 0 {\n println!(\"0\");\n } else if m == 0 {\n println!(\"2\");\n println!(\"UD\");\n } else if M == 0 {\n println!(\"2\");\n println!(\"LR\");\n } else {\n println!(\"{}\", 2 * (M + m));\n let mut answ = String::new();\n for _i in 0..M {\n answ = answ + \"U\";\n }\n for _i in 0..m {\n answ = answ + \"L\";\n }\n for _i in 0..M {\n answ = answ + \"D\";\n }\n for _i in 0..m {\n answ = answ + \"R\";\n }\n\n println!(\"{}\", answ);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0929", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are given an integer N. Among the divisors of N! (= 1 \\times 2 \\times ... \\times N), how many Shichi-Go numbers (literally \"Seven-Five numbers\") are there?

\n

Here, a Shichi-Go number is a positive integer that has exactly 75 divisors.

\n
\n
\n
\n
\n

Note

When a positive integer A divides a positive integer B, A is said to a divisor of B.\nFor example, 6 has four divisors: 1, 2, 3 and 6.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the number of the Shichi-Go numbers that are divisors of N!.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

9\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n

There are no Shichi-Go numbers among the divisors of 9! = 1 \\times 2 \\times ... \\times 9 = 362880.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

There is one Shichi-Go number among the divisors of 10! = 3628800: 32400.

\n
\n
\n
\n
\n
\n

Sample Input 3

100\n
\n
\n
\n
\n
\n

Sample Output 3

543\n
\n
\n
", "c_code": "int solution() {\n int collect[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};\n int nums[15] = {0};\n int n;\n\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n int temp = i;\n for (int j = 0; j < 15; j++) {\n if (collect[j] > temp) {\n break;\n }\n while (temp % collect[j] == 0) {\n temp /= collect[j];\n nums[j]++;\n }\n }\n }\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int e = 0;\n for (int i = 0; i < 15; i++) {\n if (nums[i] >= 4) {\n a++;\n }\n if (nums[i] >= 2) {\n b++;\n }\n if (nums[i] >= 14) {\n c++;\n }\n if (nums[i] >= 24) {\n d++;\n }\n if (nums[i] >= 74) {\n e++;\n }\n }\n if (a < 2 || b < 3) {\n printf(\"0\\n\");\n } else {\n printf(\"%d\\n\",\n (a * (a - 1) / 2 * (b - 2)) + (c * (a - 1)) + (d * (b - 1)) + e);\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\n let mut div = vec![0; 101];\n for mut i in 2..n + 1 {\n let mut j = 2;\n while j * j <= i {\n while i % j == 0 {\n i /= j;\n div[j] += 1;\n }\n j += 1;\n }\n if i != 1 {\n div[i] += 1;\n }\n }\n\n let mut c3 = 0i32;\n let mut c5 = 0i32;\n let mut c15 = 0i32;\n let mut c25 = 0i32;\n let mut c75 = 0i32;\n\n for i in 2..101 {\n div[i] += 1;\n if div[i] >= 3 {\n c3 += 1;\n }\n if div[i] >= 5 {\n c5 += 1;\n }\n if div[i] >= 15 {\n c15 += 1;\n }\n if div[i] >= 25 {\n c25 += 1;\n }\n if div[i] >= 75 {\n c75 += 1;\n }\n }\n let mut ans = 0;\n ans += c5 * (c5 - 1) / 2 * (c3 - 2);\n ans += c15 * (c5 - 1);\n ans += c25 * (c3 - 1);\n ans += c75;\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0930", "problem_description": "People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \\cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \\le i \\le n, 1 \\le j \\le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \\ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \\le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \\cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement.", "c_code": "int solution() {\n long int t;\n long int n;\n long int k;\n scanf(\"%ld\", &t);\n\n for (long int i = 0; i < t; i++) {\n scanf(\"%ld%ld\", &n, &k);\n if (k == 1) {\n printf(\"YES\\n\");\n for (long int j = 0; j < n; j++) {\n printf(\"%ld\\n\", j + 1);\n }\n continue;\n }\n\n if (n % 2 == 1) {\n printf(\"NO\\n\");\n continue;\n }\n\n printf(\"YES\\n\");\n for (long int j = 0; j < n / 2; j++) {\n for (long int l = 0; l < k; l++) {\n printf(\"%ld \", (j * (2 * k)) + (2 * l) + 1);\n }\n printf(\"\\n\");\n for (long int l = 0; l < k; l++) {\n printf(\"%ld \", (j * (2 * k)) + (2 * l) + 2);\n }\n printf(\"\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n stdin.read_line(&mut line).unwrap();\n let t: usize = line.trim().parse().unwrap();\n\n let stdout = io::stdout();\n let handle = stdout.lock();\n let mut buffer = BufWriter::with_capacity(65536, handle);\n for _t in 0..t {\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let mut n_k = line.split_ascii_whitespace();\n let shelf_count: u32 = n_k.next().unwrap().parse().unwrap();\n let shelf_length: u32 = n_k.next().unwrap().parse().unwrap();\n\n if shelf_length == 1 || shelf_count & 1 == 0 {\n writeln!(&mut buffer, \"YES\").unwrap();\n\n let mut write_shelf = |start: &mut u32| {\n for _i in 0..shelf_length {\n write!(&mut buffer, \"{} \", start).unwrap();\n *start += 2;\n }\n writeln!(&mut buffer).unwrap();\n };\n\n let mut last_even: u32 = 2;\n let mut last_odd: u32 = 1;\n for _i in 0..(shelf_count >> 1) {\n write_shelf(&mut last_odd);\n write_shelf(&mut last_even);\n }\n\n if shelf_count & 1 != 0 {\n write_shelf(&mut last_odd)\n }\n } else {\n writeln!(&mut buffer, \"NO\").unwrap();\n }\n }\n buffer.flush().unwrap();\n}", "difficulty": "medium"} {"problem_id": "0931", "problem_description": "When Serezha was three years old, he was given a set of cards with letters for his birthday. They were arranged into words in the way which formed the boy's mother favorite number in binary notation. Serezha started playing with them immediately and shuffled them because he wasn't yet able to read. His father decided to rearrange them. Help him restore the original number, on condition that it was the maximum possible one.", "c_code": "int solution() {\n int countz = 0;\n int countn = 0;\n int n = 0;\n\n scanf(\"%d\", &n);\n\n char mystring[n];\n\n scanf(\"%s\", mystring);\n\n for (int i = 0; mystring[i] != '\\0'; ++i) {\n if (mystring[i] == 'z') {\n ++countz;\n }\n }\n\n for (int i = 0; mystring[i] != '\\0'; ++i) {\n if (mystring[i] == 'n') {\n ++countn;\n }\n }\n\n for (int i = 0; i < countn; i++) {\n printf(\"1 \");\n }\n for (int i = 0; i < countz; i++) {\n printf(\"0 \");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut no = String::new();\n std::io::stdin().read_line(&mut no).expect(\"Error input\");\n\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).expect(\"Error input\");\n\n let len = s.len();\n s.retain(|c| c != 'z');\n let no_zeros = len - s.len();\n\n let remaining_count = s.len() - (no_zeros * 3);\n let no_ones = remaining_count / 3;\n\n let mut answer = std::iter::repeat_n(\"1\", no_ones).collect::>();\n answer.append(&mut std::iter::repeat_n(\"0\", no_zeros).collect::>());\n\n print!(\"{}\", answer.join(\" \"));\n}", "difficulty": "hard"} {"problem_id": "0932", "problem_description": "A string $$$s$$$ of length $$$n$$$ ($$$1 \\le n \\le 26$$$) is called alphabetical if it can be obtained using the following algorithm: first, write an empty string to $$$s$$$ (i.e. perform the assignment $$$s$$$ := \"\"); then perform the next step $$$n$$$ times; at the $$$i$$$-th step take $$$i$$$-th lowercase letter of the Latin alphabet and write it either to the left of the string $$$s$$$ or to the right of the string $$$s$$$ (i.e. perform the assignment $$$s$$$ := $$$c+s$$$ or $$$s$$$ := $$$s+c$$$, where $$$c$$$ is the $$$i$$$-th letter of the Latin alphabet). In other words, iterate over the $$$n$$$ first letters of the Latin alphabet starting from 'a' and etc. Each time we prepend a letter to the left of the string $$$s$$$ or append a letter to the right of the string $$$s$$$. Strings that can be obtained in that way are alphabetical.For example, the following strings are alphabetical: \"a\", \"ba\", \"ab\", \"bac\" and \"ihfcbadeg\". The following strings are not alphabetical: \"z\", \"aa\", \"ca\", \"acb\", \"xyz\" and \"ddcba\".From the given string, determine if it is alphabetical.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n\n for (int i = 0; i < t; i++) {\n char str[27] = {'\\0'};\n scanf(\"%s\", str);\n\n int len = strlen(str);\n\n int cnt[27] = {0};\n\n int j = 0;\n while (str[j] != '\\0') {\n cnt[str[j] - 'a']++;\n j++;\n }\n bool alpha = true;\n int c = 0;\n for (int k = 0; k < 26 && k < j; k++) {\n if (cnt[k] != 1) {\n alpha = false;\n }\n\n c += cnt[k];\n }\n\n if (c > j) {\n alpha = false;\n }\n\n if (alpha == false) {\n printf(\"NO\\n\");\n continue;\n }\n\n j = 1;\n while (str[j] != '\\0' && str[j - 1] > str[j]) {\n j++;\n }\n\n while (str[j] != '\\0' && str[j - 1] < str[j]) {\n j++;\n }\n\n if (j != len) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\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\n let t = it.next().unwrap();\n\n for _case_index in 1..=t {\n let s = lines.next().unwrap();\n let a = s.as_bytes();\n\n let ans = match a.iter().cloned().position(|x| x == b'a') {\n Some(i) => {\n let mut l = i;\n let mut r = i + 1;\n\n let mut x = true;\n\n for c in (b'b'..=b'z').take(a.len() - 1) {\n match a.iter().cloned().position(|b| b == c) {\n Some(i) => {\n if i + 1 == l {\n l -= 1;\n } else if i == r {\n r += 1;\n } else {\n x = false;\n break;\n }\n }\n None => {\n x = false;\n break;\n }\n }\n }\n\n x\n }\n None => false,\n };\n\n let ans = if ans { \"Yes\" } else { \"No\" };\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "0933", "problem_description": "

Coin Changing Problem

\n
\n\n

\nFind the minimum number of coins to make change for n cents using coins of denominations d1, d2,.., dm. The coins can be used any number of times.\n

\n\n

Input

\n\n
\nn m\nd1 d2 ... dm\n
\n\n

\nTwo integers n and m are given in the first line. The available denominations are given in the second line.\n

\n\n

Output

\n\n

\nPrint the minimum number of coins in a line.\n

\n\n

Constraints

\n\n
    \n
  • \n1 ≤ n ≤ 50000\n
  • \n
  • \n1 ≤ m ≤ 20\n
  • \n
  • \n1 ≤ denomination ≤ 10000\n
  • \n
  • \nThe denominations are all different and contain 1.\n
  • \n
\n\n

Sample Input 1

\n
\n55 4\n1 5 10 50\n
\n

Sample Output 1

\n
\n2\n
\n\n
\n\n

Sample Input 2

\n
\n15 6\n1 2 7 8 12 50\n
\n

Sample Output 2

\n
\n2\n
\n\n
\n\n

Sample Input 3

\n
\n65 6\n1 2 7 8 12 50\n
\n

Sample Output 3

\n
\n3\n
", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int c[20];\n int dp[50001];\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &c[i]);\n }\n dp[0] = 0;\n dp[1] = 1;\n for (int k = 2; k <= n; k++) {\n int min = 999999;\n\n for (int l = 0; l < m; l++) {\n if (k - c[l] >= 0) {\n if (min > dp[k - c[l]]) {\n min = dp[k - c[l]];\n }\n }\n }\n dp[k] = min + 1;\n }\n printf(\"%d\\n\", dp[n]);\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut buf = String::new();\n let _ = stdin.read_to_string(&mut buf);\n let mut iter = buf.split_whitespace();\n\n let n: usize = iter.next().unwrap().parse().unwrap();\n let _m: usize = iter.next().unwrap().parse().unwrap();\n let c: Vec = iter.map(|s| s.parse().unwrap()).collect();\n\n let mut dp = vec![std::usize::MAX; n + 1];\n dp[0] = 0;\n\n while dp[n] == std::usize::MAX {\n for i in 0..n {\n for c_i in c.iter() {\n if i + c_i <= n && dp[i] < std::usize::MAX {\n dp[i + c_i] = min(dp[i + c_i], dp[i] + 1);\n }\n }\n }\n }\n\n println!(\"{}\", dp[n]);\n}", "difficulty": "medium"} {"problem_id": "0934", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Takahashi has a water bottle with the shape of a rectangular prism whose base is a square of side a~\\mathrm{cm} and whose height is b~\\mathrm{cm}. (The thickness of the bottle can be ignored.)

\n

We will pour x~\\mathrm{cm}^3 of water into the bottle, and gradually tilt the bottle around one of the sides of the base.

\n

When will the water be spilled? More formally, find the maximum angle in which we can tilt the bottle without spilling any water.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq a \\leq 100
  • \n
  • 1 \\leq b \\leq 100
  • \n
  • 1 \\leq x \\leq a^2b
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b x\n
\n
\n
\n
\n
\n

Output

Print the maximum angle in which we can tilt the bottle without spilling any water, in degrees.\nYour output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

45.0000000000\n
\n

This bottle has a cubic shape, and it is half-full. The water gets spilled when we tilt the bottle more than 45 degrees.

\n
\n
\n
\n
\n
\n

Sample Input 2

12 21 10\n
\n
\n
\n
\n
\n

Sample Output 2

89.7834636934\n
\n

This bottle is almost empty. When the water gets spilled, the bottle is nearly horizontal.

\n
\n
\n
\n
\n
\n

Sample Input 3

3 1 8\n
\n
\n
\n
\n
\n

Sample Output 3

4.2363947991\n
\n

This bottle is almost full. When the water gets spilled, the bottle is still nearly vertical.

\n
\n
", "c_code": "int solution(void) {\n\n double a;\n double b;\n double x;\n double ans = 0;\n double pai = 3.141592653589793238462643383279;\n\n scanf(\"%lf %lf %lf\", &a, &b, &x);\n\n if (2 * x > a * a * b) {\n\n ans = (180 / pai) * atan((2 / (a * a * a)) * (a * a * b - x));\n\n } else if (2 * x == a * a * b) {\n\n ans = 90 - (180 / pai) * atan(a / b);\n\n } else {\n\n ans = 90 - (180 / pai) * atan(2 * x / (a * b * b));\n }\n\n printf(\"%.16f\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).ok();\n\n let strs: Vec<&str> = line.split_whitespace().collect();\n let a: f64 = strs[0].parse().unwrap();\n let b: f64 = strs[1].parse().unwrap();\n let x: f64 = strs[2].parse().unwrap();\n\n let s = a * a * b;\n if x > s / 2.0 {\n let y = 2.0 * (s - x) / (a * a);\n println!(\"{:.7}\", 90.0 - (a / y).atan() / f64::consts::PI * 180.0);\n } else {\n let y = 2.0 * x / (a * b);\n println!(\"{:.7}\", 90.0 - (y / b).atan() / f64::consts::PI * 180.0);\n }\n}", "difficulty": "easy"} {"problem_id": "0935", "problem_description": "Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for $$$n$$$ last days: $$$a_1, a_2, \\dots, a_n$$$, where $$$a_i$$$ is the price of berPhone on the day $$$i$$$.Polycarp considers the price on the day $$$i$$$ to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if $$$n=6$$$ and $$$a=[3, 9, 4, 6, 7, 5]$$$, then the number of days with a bad price is $$$3$$$ — these are days $$$2$$$ ($$$a_2=9$$$), $$$4$$$ ($$$a_4=6$$$) and $$$5$$$ ($$$a_5=7$$$).Print the number of days with a bad price.You have to answer $$$t$$$ independent data sets.", "c_code": "int solution() {\n int t;\n scanf(\"%d\\n\", &t);\n while (t) {\n int n;\n scanf(\"%d\\n\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int c = 0;\n int min = a[n - 1];\n for (int i = n - 2; i >= 0; i--) {\n if (a[i] > min) {\n c++;\n } else {\n min = a[i];\n }\n }\n\n printf(\"%d\\n\", c);\n t--;\n }\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 for _ in 0..t {\n let mut _buffer = String::new();\n io::stdin()\n .read_line(&mut _buffer)\n .expect(\"failed to read input\");\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let a = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let (bad, _) = a.iter().rev().fold((0, a[a.len() - 1]), |(acc, min), &x| {\n if x <= min {\n (acc, x)\n } else {\n (acc + 1, min)\n }\n });\n println!(\"{}\", bad);\n }\n}", "difficulty": "medium"} {"problem_id": "0936", "problem_description": "Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.", "c_code": "int solution(void) {\n\n long long int cases;\n long long int hasila;\n long long int hasilb;\n long long int a;\n long long int b;\n long long int c;\n\n scanf(\"%lld\", &cases);\n\n long long int hasilakhir[cases];\n\n for (int x = 0; x < cases; x++) {\n scanf(\"%lld %lld %lld %lld\", &hasila, &a, &b, &c);\n\n if (hasila % a == 0 || hasila % b == 0 || hasila % c == 0) {\n hasilakhir[x] = 0;\n } else {\n hasilakhir[x] = a - (hasila - 1) % a - 1;\n hasilb = b - (hasila - 1) % b - 1;\n\n if (hasilb < hasilakhir[x]) {\n hasilakhir[x] = hasilb;\n }\n\n hasilb = c - (hasila - 1) % c - 1;\n\n if (hasilb < hasilakhir[x]) {\n hasilakhir[x] = hasilb;\n }\n }\n }\n for (int x = 0; x < cases; x++) {\n printf(\"%lld\\n\", hasilakhir[x]);\n }\n}", "rust_code": "fn solution() -> std::io::Result<()> {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf)?;\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let p = scan!(u128);\n let mut ans = std::u128::MAX;\n\n for _ in 0..3 {\n let s = scan!(u128);\n let ub = if p % s == 0 { p / s } else { p / s + 1 };\n ans = ans.min((ub * s) - p);\n }\n print!(\"{}\", ans);\n print!(\"\\n\");\n }\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "0937", "problem_description": "You are given $$$q$$$ queries in the following form:Given three integers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$, find minimum positive integer $$$x_i$$$ such that it is divisible by $$$d_i$$$ and it does not belong to the segment $$$[l_i, r_i]$$$.Can you answer all the queries?Recall that a number $$$x$$$ belongs to segment $$$[l, r]$$$ if $$$l \\le x \\le r$$$.", "c_code": "int solution() {\n int n;\n int i;\n int x;\n scanf(\"%d\", &n);\n int l[n];\n int r[n];\n int d[n];\n for (i = 0; i < n; i++) {\n scanf(\"%d%d%d\", &l[i], &r[i], &d[i]);\n if (l[i] - d[i] > 0) {\n printf(\"%d\\n\", d[i] * 1);\n } else {\n x = r[i] / d[i];\n printf(\"%d\\n\", (x + 1) * d[i]);\n }\n }\n}", "rust_code": "fn solution() {\n let n: usize = {\n let mut read_buf = String::new();\n io::stdin().read_line(&mut read_buf).unwrap();\n read_buf.trim().parse().unwrap()\n };\n for _ in 0..n {\n let (l, r, d): (usize, usize, usize) = {\n let mut read_buf = String::new();\n io::stdin().read_line(&mut read_buf).unwrap();\n let mut read_buf = read_buf.split_whitespace().map(|s| s.parse().unwrap());\n (\n read_buf.next().unwrap(),\n read_buf.next().unwrap(),\n read_buf.next().unwrap(),\n )\n };\n if d < l || d > r {\n println!(\"{}\", d);\n } else {\n println!(\"{}\", (r / d + 1) * d);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0938", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We have two integers: A and B.

\n

Print the largest number among A + B, A - B, and A \\times B.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • -100 \\leq A,\\ B \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the largest number among A + B, A - B, and A \\times B.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

-13 3\n
\n
\n
\n
\n
\n

Sample Output 1

-10\n
\n

The largest number among A + B = -10, A - B = -16, and A \\times B = -39 is -10.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 -33\n
\n
\n
\n
\n
\n

Sample Output 2

34\n
\n

The largest number among A + B = -32, A - B = 34, and A \\times B = -33 is 34.

\n
\n
\n
\n
\n
\n

Sample Input 3

13 3\n
\n
\n
\n
\n
\n

Sample Output 3

39\n
\n

The largest number among A + B = 16, A - B = 10, and A \\times B = 39 is 39.

\n
\n
", "c_code": "int solution() {\n int A = 0;\n int B = 0;\n int pl = 0;\n int mn = 0;\n int mu = 0;\n int kai = 0;\n scanf(\"%d %d\", &A, &B);\n pl = A + B;\n mn = A - B;\n mu = A * B;\n kai = pl;\n if (kai < mn) {\n kai = mn;\n }\n if (kai < mu) {\n kai = mu;\n }\n printf(\"%d\\n\", kai);\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 a: i32 = itr.next().unwrap().parse().unwrap();\n let b: i32 = itr.next().unwrap().parse().unwrap();\n println!(\"{}\", max(a + b, max(a - b, a * b)))\n}", "difficulty": "medium"} {"problem_id": "0939", "problem_description": "Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.Monocarp has $$$n$$$ problems that none of his students have seen yet. The $$$i$$$-th problem has a topic $$$a_i$$$ (an integer from $$$1$$$ to $$$n$$$) and a difficulty $$$b_i$$$ (an integer from $$$1$$$ to $$$n$$$). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time.Monocarp decided to select exactly $$$3$$$ problems from $$$n$$$ problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both): the topics of all three selected problems are different; the difficulties of all three selected problems are different. Your task is to determine the number of ways to select three problems for the problemset.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int a[200005];\n int b[200005];\n int diff[200005];\n int topic[200005];\n int i = 0;\n memset(diff, 0, (n + 1) * sizeof *diff),\n memset(topic, 0, (n + 1) * sizeof *topic);\n for (i = 1; i <= n; i++) {\n scanf(\"%d %d\", &a[i], &b[i]);\n topic[a[i]]++;\n diff[b[i]]++;\n }\n long long int sum = 0;\n sum = (long long)n * (n - 1) * (n - 2) / 6;\n for (i = 1; i <= n; i++) {\n sum -= (long long)(topic[a[i]] - 1) * (diff[b[i]] - 1);\n }\n printf(\"%lld\\n\", sum);\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 n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let mut a: Vec = vec![0; n];\n let mut b: Vec = vec![0; n];\n let mut p: Vec<(usize, usize)> = vec![];\n for _ in 0..n {\n let x: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let i = x[0] - 1;\n let j = x[1] - 1;\n a[i] += 1;\n b[j] += 1;\n p.push((i, j));\n }\n let mut sub = 0;\n for (x, y) in p {\n sub += (a[x] - 1) * (b[y] - 1);\n }\n let ans = n as u64 * (n as u64 - 1) * (n as u64 - 2) / 6 - sub;\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0940", "problem_description": "Bachgold problem is very easy to formulate. Given a positive integer n represent it as a sum of maximum possible number of prime numbers. One can prove that such representation exists for any integer greater than 1.Recall that integer k is called prime if it is greater than 1 and has exactly two positive integer divisors — 1 and k.", "c_code": "int solution(void) {\n int n = 0;\n scanf(\"%d\", &n);\n printf(\"%d\\n\", (n / 2));\n for (int i = 0; i < (n / 2) - 1; i++) {\n printf(\"2\\t\");\n }\n if (n - 2 * (n / 2) == 1) {\n printf(\"3\");\n }\n if (n == 2 * (n / 2)) {\n printf(\"2\");\n }\n}", "rust_code": "fn solution() {\n let mut val = String::new();\n io::stdin()\n .read_line(&mut val)\n .expect(\"Не удалось прочитать строку\");\n\n let mut val: u32 = val.trim().parse().expect(\"Пожалуйста, введите число!\");\n\n if val.is_multiple_of(2) {\n println!(\"{}\", val / 2);\n for _ in 0..val / 2 {\n print!(\"2 \");\n }\n } else {\n val -= 3;\n println!(\"{}\", val / 2 + 1);\n for _ in 0..val / 2 {\n print!(\"2 \");\n }\n print!(\"3\");\n }\n}", "difficulty": "medium"} {"problem_id": "0941", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.
\nIn order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.
\nHe does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.
\nThe pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.
\nThe package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).
\nDolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.
\nFind the minimum amount of money required to generate the substance C.
\nIf it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦N≦40
  • \n
  • 1≦a_i,b_i≦10
  • \n
  • 1≦c_i≦100
  • \n
  • 1≦M_a,M_b≦10
  • \n
  • gcd(M_a,M_b)=1
  • \n
  • a_i, b_i, c_i, M_a and M_b are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N M_a M_b  \na_1 b_1 c_1  \na_2 b_2 c_2\n:  \na_N b_N c_N  \n
\n
\n
\n
\n
\n

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1 1\n1 2 1\n2 1 2\n3 3 10\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.
\nIn this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.
\nThe total price of these packages is 3 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 1 10\n10 10 10\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.

\n
\n
", "c_code": "int solution(void) {\n\n int n;\n int ma;\n int mb;\n scanf(\"%d %d %d\", &n, &ma, &mb);\n int a[n];\n int b[n];\n int c[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d %d %d\", &a[i], &b[i], &c[i]);\n }\n int dp[n][401][401];\n for (int i = 0; i <= 400; i++) {\n for (int j = 0; j <= 400; j++) {\n dp[0][i][j] = -1;\n }\n }\n dp[0][0][0] = 0;\n dp[0][a[0]][b[0]] = c[0];\n int sum_a;\n int sum_b;\n int cost;\n int min = -1;\n for (int chem = 1; chem < n; chem++) {\n for (int i = 0; i <= 400; i++) {\n for (int j = 0; j <= 400; j++) {\n dp[chem][i][j] = dp[chem - 1][i][j];\n }\n }\n for (int i = 0; i <= 400; i++) {\n for (int j = 0; j <= 400; j++) {\n if (dp[chem - 1][i][j] == -1) {\n continue;\n }\n sum_a = i + a[chem];\n sum_b = j + b[chem];\n cost = dp[chem - 1][i][j] + c[chem];\n if (dp[chem][sum_a][sum_b] == -1 || dp[chem][sum_a][sum_b] > cost) {\n dp[chem][sum_a][sum_b] = cost;\n }\n }\n }\n }\n for (int i = 1; i <= 400; i++) {\n for (int j = 1; j <= 400; j++) {\n if (i * mb == j * ma && dp[n - 1][i][j] != -1) {\n if (min == -1 || dp[n - 1][i][j] < min) {\n min = dp[n - 1][i][j];\n }\n }\n }\n }\n printf(\"%d\\n\", min);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n ma: usize,\n mb: usize,\n v: [(usize, usize, usize); n]\n }\n let max_c = 100000;\n let nab = n * 10 + 1;\n let mut dp = vec![vec![vec![max_c; nab]; nab]; n + 1];\n dp[0][0][0] = 0;\n for i in 0..n {\n dp[i + 1] = dp[i].clone();\n for j in v[i].0..nab {\n for k in v[i].1..nab {\n dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j - v[i].0][k - v[i].1] + v[i].2);\n }\n }\n }\n let mut ans = max_c;\n let mut a = ma;\n let mut b = mb;\n while a < nab && b < nab {\n ans = min(ans, dp[n][a][b]);\n a += ma;\n b += mb;\n }\n if ans == max_c {\n println!(\"-1\");\n } else {\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "0942", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

Takahashi will do a tap dance. The dance is described by a string S where each character is L, R, U, or D. These characters indicate the positions on which Takahashi should step. He will follow these instructions one by one in order, starting with the first character.

\n

S is said to be easily playable if and only if it satisfies both of the following conditions:

\n
    \n
  • Every character in an odd position (1-st, 3-rd, 5-th, \\ldots) is R, U, or D.
  • \n
  • Every character in an even position (2-nd, 4-th, 6-th, \\ldots) is L, U, or D.
  • \n
\n

Your task is to print Yes if S is easily playable, and No otherwise.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • S is a string of length between 1 and 100 (inclusive).
  • \n
  • Each character of S is L, R, U, or D.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

\n

Print Yes if S is easily playable, and No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

RUDLUDR\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Every character in an odd position (1-st, 3-rd, 5-th, 7-th) is R, U, or D.

\n

Every character in an even position (2-nd, 4-th, 6-th) is L, U, or D.

\n

Thus, S is easily playable.

\n
\n
\n
\n
\n
\n

Sample Input 2

DULL\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

The 3-rd character is not R, U, nor D, so S is not easily playable.

\n
\n
\n
\n
\n
\n

Sample Input 3

UUUUUUUUUUUUUUU\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 4

ULURU\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
\n
\n
\n
\n

Sample Input 5

RDULULDURURLRDULRLR\n
\n
\n
\n
\n
\n

Sample Output 5

Yes\n
\n
\n
", "c_code": "int solution() {\n char s[100];\n scanf(\"%s\", s);\n for (int i = 0; i < 100; i++) {\n if (s[i] == '\\0') {\n break;\n }\n if (i % 2 == 0) {\n if (s[i] != 'R' && s[i] != 'U' && s[i] != 'D') {\n printf(\"No\\n\");\n return 0;\n }\n } else {\n if (s[i] != 'L' && s[i] != 'U' && s[i] != 'D') {\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 buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n if buf\n .chars()\n .enumerate()\n .all(|(i, c)| i % 2 == 1 && c != 'R' || i % 2 == 0 && c != 'L')\n {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n };\n}", "difficulty": "easy"} {"problem_id": "0943", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides.

\n

Print the answer in degrees, but do not print units.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print an integer representing the sum of the interior angles of a regular polygon with N sides.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

180\n
\n

The sum of the interior angles of a regular triangle is 180 degrees.

\n
\n
\n
\n
\n
\n

Sample Input 2

100\n
\n
\n
\n
\n
\n

Sample Output 2

17640\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int ans = 0;\n scanf(\"%d\", &N);\n ans = 180 + (N - 3) * 180;\n printf(\"%d\\n\", ans);\n return 0;\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 println!(\"{}\", (n - 2) * 180);\n}", "difficulty": "easy"} {"problem_id": "0944", "problem_description": "You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings \"AB\" and \"BA\" (the substrings can go in any order).", "c_code": "int solution() {\n char s[100001];\n int i1[100001];\n int ic1 = 0;\n int i2[100001];\n int ic2 = 0;\n scanf(\"%s\", s);\n for (int i = 0; s[i + 1] != '\\0'; i++) {\n if (s[i] == 'A' && s[i + 1] == 'B') {\n i1[ic1++] = i;\n } else if (s[i] == 'B' && s[i + 1] == 'A') {\n i2[ic2++] = i;\n }\n }\n for (int i = 0; i < ic1; i++) {\n for (int j = 0; j < ic2; j++) {\n if (i1[i] - i2[j] != -1 && i2[j] - i1[i] != -1) {\n printf(\"YES\");\n return 0;\n }\n }\n }\n\n printf(\"NO\");\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n\n io::stdin().read_line(&mut s).unwrap();\n\n let s = s.trim();\n\n let s: Vec = s.chars().collect();\n\n let mut fin: usize = s.len();\n let mut found = false;\n\n for i in 0..s.len() - 1_usize {\n if s[i] == 'A' && s[i + 1] == 'B' {\n fin = i + 2;\n break;\n }\n }\n\n for i in fin..s.len() - 1_usize {\n if s[i] == 'B' && s[i + 1] == 'A' {\n found = true;\n }\n }\n\n fin = s.len();\n\n for i in 0..s.len() - 1_usize {\n if s[i] == 'B' && s[i + 1] == 'A' {\n fin = i + 2;\n break;\n }\n }\n\n for i in fin..s.len() - 1_usize {\n if s[i] == 'A' && s[i + 1] == 'B' {\n found = true;\n }\n }\n\n if found {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "easy"} {"problem_id": "0945", "problem_description": "NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself.He managed to calculate the number of outer circles $$$n$$$ and the radius of the inner circle $$$r$$$. NN thinks that, using this information, you can exactly determine the radius of the outer circles $$$R$$$ so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture.", "c_code": "int solution() {\n float n;\n float r;\n scanf(\"%f %f\", &n, &r);\n\n float val = 360 / (2 * n);\n\n float theta = (3.14159265 / 180) * val;\n float ans = (r * sinf(theta)) / (1 - sinf(theta));\n printf(\"%f\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).expect(\"Input error\");\n let input_segment: Vec = input\n .split_whitespace()\n .map(|x| x.parse().expect(\"Not an integer\"))\n .collect();\n let a: f64 = input_segment[0];\n let b: f64 = input_segment[1];\n let angle: f64 = (a - 2.0) / (2.0 * a) * 180.0;\n let outer_radius: f64 = angle.to_radians().cos() / (1.0 - angle.to_radians().cos());\n println!(\"{:.7}\", outer_radius * b);\n}", "difficulty": "medium"} {"problem_id": "0946", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given three words s_1, s_2 and s_3, each composed of lowercase English letters, with spaces in between.\nPrint the acronym formed from the uppercased initial letters of the words.

\n
\n
\n
\n
\n

Constraints

    \n
  • s_1, s_2 and s_3 are composed of lowercase English letters.
  • \n
  • 1 ≤ |s_i| ≤ 10 (1≤i≤3)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s_1 s_2 s_3\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

atcoder beginner contest\n
\n
\n
\n
\n
\n

Sample Output 1

ABC\n
\n

The initial letters of atcoder, beginner and contest are a, b and c. Uppercase and concatenate them to obtain ABC.

\n
\n
\n
\n
\n
\n

Sample Input 2

resident register number\n
\n
\n
\n
\n
\n

Sample Output 2

RRN\n
\n
\n
\n
\n
\n
\n

Sample Input 3

k nearest neighbor\n
\n
\n
\n
\n
\n

Sample Output 3

KNN\n
\n
\n
\n
\n
\n
\n

Sample Input 4

async layered coding\n
\n
\n
\n
\n
\n

Sample Output 4

ALC\n
\n
\n
", "c_code": "int solution(void) {\n char s[35];\n char ans[4];\n fgets(s, 35, stdin);\n ans[0] = s[0];\n ans[3] = '\\0';\n int c = 0;\n int f = 0;\n while (f != 2) {\n if (s[c] == ' ' && f == 1) {\n ans[2] = s[c + 1];\n f++;\n }\n if (s[c] == ' ' && f == 0) {\n ans[1] = s[c + 1];\n f++;\n }\n c++;\n }\n for (int i = 0; i < 3; i++) {\n ans[i] = toupper(ans[i]);\n }\n printf(\"%s\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let iter = buf.split_whitespace();\n let mut v: Vec> = Vec::new();\n for i in iter {\n v.push(i.chars().collect::>());\n }\n let s1 = v[0].clone();\n let s1c = s1[0].to_uppercase().collect::>();\n let s2 = v[1].clone();\n let s2c = s2[0].to_uppercase().collect::>();\n let s3 = v[2].clone();\n let s3c = s3[0].to_uppercase().collect::>();\n let s = format!(\"{}{}{}\", s1c[0], s2c[0], s3c[0]);\n println!(\"{}\", s);\n}", "difficulty": "medium"} {"problem_id": "0947", "problem_description": "Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi. Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.", "c_code": "int solution() {\n int i;\n int j;\n int n;\n int m;\n int *q;\n int a;\n int b;\n int c;\n int zero;\n int sum;\n struct worker {\n int *c, cnt;\n } *w;\n\n scanf(\"%d\", &n);\n q = malloc(sizeof(*q) * n);\n w = malloc(sizeof(*w) * n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &q[i]);\n }\n scanf(\"%d\", &m);\n for (i = 0; i < n; i++) {\n w[i].c = malloc(sizeof(*w[i].c) * m);\n w[i].cnt = 0;\n }\n for (i = 0; i < m; i++) {\n scanf(\"%d%d%d\", &a, &b, &c);\n w[b - 1].c[w[b - 1].cnt++] = c;\n }\n\n zero = sum = 0;\n for (i = 0; i < n; i++) {\n if (w[i].cnt == 0) {\n if (zero) {\n printf(\"-1\\n\");\n return 0;\n }\n zero = 1;\n }\n }\n for (i = 0; i < n; i++) {\n int min = 1000001;\n\n if (w[i].cnt != 0) {\n for (j = 0; j < w[i].cnt; j++) {\n if (min > w[i].c[j]) {\n min = w[i].c[j];\n }\n }\n sum += min;\n }\n }\n printf(\"%d\\n\", sum);\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let stdin = io::stdin();\n let mut input = stdin.lock();\n let mut buffer = String::new();\n input.read_line(&mut buffer)?;\n let n: usize = buffer.trim().parse().unwrap();\n\n buffer.clear();\n input.read_line(&mut buffer)?;\n let mut max_qual: i32 = -1;\n let mut max_qual_count: u32 = 0;\n\n for str_qual in buffer.split_ascii_whitespace() {\n let qual: i32 = str_qual.parse().unwrap();\n if qual > max_qual {\n max_qual = qual;\n max_qual_count = 1;\n } else if qual == max_qual {\n max_qual_count += 1;\n }\n }\n if max_qual_count >= 2 {\n println!(\"-1\");\n return Ok(());\n }\n buffer.clear();\n input.read_line(&mut buffer)?;\n let m: usize = buffer.trim().parse().unwrap();\n let mut cheapest_boss: Vec> = vec![None; n];\n for edge_line in input.lines().take(m) {\n let edge_spec: Vec = edge_line?\n .split_ascii_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let _a = edge_spec[0] - 1;\n let b = (edge_spec[1] - 1) as usize;\n let c = edge_spec[2];\n let curr_cheapest = cheapest_boss[b];\n if c < curr_cheapest.unwrap_or(c + 1) {\n cheapest_boss[b] = Some(c);\n }\n }\n let mut total: u64 = 0;\n let mut bossless: u32 = 0;\n for cheapest in cheapest_boss.iter() {\n match cheapest {\n None => {\n bossless += 1;\n }\n Some(cheapest) => {\n total += *cheapest as u64;\n }\n }\n }\n if bossless <= 1 {\n println!(\"{}\", total);\n } else {\n println!(\"-1\");\n }\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "0948", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There is an apple tree that bears apples of N colors. The N colors of these apples are numbered 1 to N, and there are a_i apples of Color i.

\n

You and Lunlun the dachshund alternately perform the following operation (starting from you):

\n
    \n
  • Choose one or more apples from the tree and eat them. Here, the apples chosen at the same time must all have different colors.
  • \n
\n

The one who eats the last apple from the tree will be declared winner. If both you and Lunlun play optimally, which will win?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq a_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1\na_2\n:\na_N\n
\n
\n
\n
\n
\n

Output

If you will win, print first; if Lunlun will win, print second.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n1\n2\n
\n
\n
\n
\n
\n

Sample Output 1

first\n
\n

Let Color 1 be red, and Color 2 be blue. In this case, the tree bears one red apple and two blue apples.

\n

You should eat the red apple in your first turn. Lunlun is then forced to eat one of the blue apples, and you can win by eating the other in your next turn.

\n

Note that you are also allowed to eat two apples in your first turn, one red and one blue (not a winning move, though).

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n100000\n30000\n20000\n
\n
\n
\n
\n
\n

Sample Output 2

second\n
\n
\n
", "c_code": "int solution() {\n int n;\n int check = 0;\n int s[100040];\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &s[i]);\n if (s[i] % 2 == 1) {\n check++;\n }\n }\n if (check >= 1) {\n printf(\"first\");\n } else {\n printf(\"second\");\n }\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n a: [usize; n],\n }\n if a.iter().any(|n| n % 2 != 0) {\n println!(\"first\");\n } else {\n println!(\"second\");\n }\n}", "difficulty": "easy"} {"problem_id": "0949", "problem_description": "A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasya and decided to teach him a lesson. He gave the lazy student a seemingly easy task: You are given an idle body in space and the forces that affect it. The body can be considered as a material point with coordinates (0; 0; 0). Vasya had only to answer whether it is in equilibrium. \"Piece of cake\" — thought Vasya, we need only to check if the sum of all vectors is equal to 0. So, Vasya began to solve the problem. But later it turned out that there can be lots and lots of these forces, and Vasya can not cope without your help. Help him. Write a program that determines whether a body is idle or is moving by the given vectors of forces.", "c_code": "int solution() {\n int A[500][500] = {0};\n int sum = 0;\n\n int n = 0;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 3; j++) {\n scanf(\"%d\", &A[i][j]);\n }\n }\n\n for (int j = 0; j < 3; j++) {\n sum = 0;\n for (int i = 0; i < n; i++) {\n sum = sum + A[i][j];\n }\n\n if (sum != 0) {\n printf(\"NO\");\n return 0;\n }\n }\n\n printf(\"YES\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut result: Vec = vec![0, 0, 0];\n std::io::stdin().lock().lines().skip(1).for_each(|l| {\n let v: Vec = l\n .expect(\"stdin not work\")\n .trim()\n .split(' ')\n .map(|s| s.parse::().expect(\"not number\"))\n .collect();\n result[0] += v[0];\n result[1] += v[1];\n result[2] += v[2];\n });\n println!(\n \"{}\",\n if result[0] == 0 && result[1] == 0 && result[2] == 0 {\n \"YES\"\n } else {\n \"NO\"\n }\n );\n}", "difficulty": "medium"} {"problem_id": "0950", "problem_description": "You are given an array $$$a$$$ of $$$2n$$$ distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its $$$2$$$ neighbours.More formally, find an array $$$b$$$, such that: $$$b$$$ is a permutation of $$$a$$$.For every $$$i$$$ from $$$1$$$ to $$$2n$$$, $$$b_i \\neq \\frac{b_{i-1}+b_{i+1}}{2}$$$, where $$$b_0 = b_{2n}$$$ and $$$b_{2n+1} = b_1$$$. It can be proved that under the constraints of this problem, such array $$$b$$$ always exists.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int m;\n scanf(\"%d\", &m);\n int arr[(2 * m)];\n for (int i = 0; i < (2 * m); i++) {\n scanf(\"%d\", &arr[i]);\n }\n for (int i = 0; i < (2 * m) - 1; i++) {\n for (int j = 0; j < (2 * m) - i - 1; j++) {\n if (arr[j] > arr[j + 1]) {\n int temp = arr[j];\n arr[j] = arr[j + 1];\n arr[j + 1] = temp;\n }\n }\n }\n int brr[(2 * m)];\n int k = 0;\n for (int i = 0; i < m; i++) {\n brr[k++] = arr[i];\n brr[k++] = arr[(2 * m) - i - 1];\n }\n for (int i = 0; i < (2 * m); i++) {\n printf(\"%d \", brr[i]);\n }\n printf(\"\\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 t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let n = lines.next().unwrap().parse::().unwrap();\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let mut a = it.collect::>();\n a.sort();\n\n for i in 0..n {\n write!(&mut output, \"{} {} \", a[i], a[2 * n - 1 - i]).unwrap();\n }\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0951", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given an integer N.

\n

Find a triple of positive integers h, n and w such that 4/N = 1/h + 1/n + 1/w.

\n

If there are multiple solutions, any of them will be accepted.

\n
\n
\n
\n
\n

Constraints

    \n
  • It is guaranteed that, for the given integer N, there exists a solution such that h,n,w \\leq 3500.
  • \n
\n
\n
\n
\n
\n
\n
\n

Inputs

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Outputs

Print a triple of positive integers h, n and w that satisfies the condition, in the following format:

\n
h n w\n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n
\n
\n
\n
\n
\n

Sample Output 1

1 2 2\n
\n

4/2 = 1/1 + 1/2 + 1/2.

\n
\n
\n
\n
\n
\n

Sample Input 2

3485\n
\n
\n
\n
\n
\n

Sample Output 2

872 1012974 1539173474040\n
\n

It is allowed to use an integer exceeding 3500 in a solution.

\n
\n
\n
\n
\n
\n

Sample Input 3

4664\n
\n
\n
\n
\n
\n

Sample Output 3

3498 3498 3498\n
\n
\n
", "c_code": "int solution() {\n long long n;\n scanf(\"%lld\", &n);\n if (n % 2 == 0) {\n printf(\"%lld %lld %lld\", n, n, n / 2);\n return 0;\n }\n for (long long i = 1; i <= 3500; i++) {\n for (long long j = 1; j <= 3500; j++) {\n if ((4 * i * j - n * i - n * j) > 0 &&\n n * i * j % (4 * i * j - n * i - n * j) == 0 &&\n n * i * j / (4 * i * j - n * i - n * j) > 0) {\n printf(\"%lld %lld %lld\", i, j, n * i * j / (4 * i * j - n * i - n * j));\n return 0;\n }\n }\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 let N: i64 = buf.trim().parse().unwrap();\n for a in 1..3500 {\n for b in a..3500 {\n let div = 4 * a * b - N * b - N * a;\n if div <= 0 {\n continue;\n }\n\n let m = (N * a * b) % (4 * a * b - N * b - N * a);\n let c = (N * a * b) / (4 * a * b - N * b - N * a);\n if m > 0 || c <= 0 {\n continue;\n }\n\n println!(\"{} {} {}\", a, b, c);\n return;\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0952", "problem_description": "It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.Serval will go to the bus station at time $$$t$$$, and there are $$$n$$$ bus routes which stop at this station. For the $$$i$$$-th bus route, the first bus arrives at time $$$s_i$$$ minutes, and each bus of this route comes $$$d_i$$$ minutes later than the previous one.As Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them.", "c_code": "int solution() {\n int n;\n int t;\n scanf(\"%d\", &n);\n scanf(\"%d\", &t);\n int s[n + 2];\n int d[n + 2];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &s[i]);\n scanf(\"%d\", &d[i]);\n }\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n while (s[i] < t) {\n s[i] = s[i] + d[i];\n }\n }\n\n for (int i = 0; i < n; i++) {\n if (s[ans] > s[i]) {\n ans = i;\n }\n }\n printf(\"%d\\n\", ans + 1);\n return 0;\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\n let n = get!();\n let t = get!();\n let mut xs = vec![];\n for i in 0..n {\n let mut s = get!();\n let d = get!();\n if s < t {\n let e = t - s;\n s += ((e + d - 1) / d) * d;\n }\n xs.push((s, i + 1));\n }\n xs.sort();\n println!(\"{}\", xs[0].1);\n}", "difficulty": "medium"} {"problem_id": "0953", "problem_description": "You are given an integer $$$n$$$.Let's define $$$s(n)$$$ as the string \"BAN\" concatenated $$$n$$$ times. For example, $$$s(1)$$$ = \"BAN\", $$$s(3)$$$ = \"BANBANBAN\". Note that the length of the string $$$s(n)$$$ is equal to $$$3n$$$.Consider $$$s(n)$$$. You can perform the following operation on $$$s(n)$$$ any number of times (possibly zero): Select any two distinct indices $$$i$$$ and $$$j$$$ $$$(1 \\leq i, j \\leq 3n, i \\ne j)$$$. Then, swap $$$s(n)_i$$$ and $$$s(n)_j$$$. You want the string \"BAN\" to not appear in $$$s(n)$$$ as a subsequence. What's the smallest number of operations you have to do to achieve this? Also, find one such shortest sequence of operations.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters.", "c_code": "int solution() {\n int t;\n int n;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d\", &n);\n if (n % 2 == 0) {\n printf(\"%d\\n\", n / 2);\n for (int i = 0; i < (n / 2); i++) {\n printf(\"%d %d \", 2 + (i * 3), (n * 3) - (i * 3));\n }\n printf(\"\\n\");\n } else {\n printf(\"%d\\n\", (n / 2) + 1);\n if (n != 1) {\n for (int i = 0; i < ((n - 1) / 2); i++) {\n printf(\"%d %d \", 2 + (i * 3), (n * 3) - (i * 3));\n }\n printf(\"%d %d\\n\", (n / 2 * 3) + 2, (n / 2 + 1) * 3);\n } else {\n printf(\"1 2\\n\");\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(1)\n .flatten()\n .flat_map(|s| s.parse::())\n .for_each(|n| {\n let m = n.wrapping_add(1).wrapping_shr(1);\n writeln!(buf, \"{m}\").ok();\n let mut i = 1isize;\n let mut j = n.wrapping_mul(3);\n while i < j {\n writeln!(buf, \"{i} {j}\").ok();\n j = j.wrapping_sub(3);\n i = i.wrapping_add(3);\n }\n });\n}", "difficulty": "hard"} {"problem_id": "0954", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

\n

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

\n
    \n
  • Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.
  • \n
\n

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

\n

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1 \\leq N \\leq 2000
  • \n
  • 1 \\leq Z, W, a_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N Z W\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the score.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 100 100\n10 1000 100\n
\n
\n
\n
\n
\n

Sample Output 1

900\n
\n

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 100 1000\n10 100 100\n
\n
\n
\n
\n
\n

Sample Output 2

900\n
\n

If X draws all the cards first, the score will be |1000 - 100| = 900.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 1 1\n1 1 1 1 1\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1 1 1\n1000000000\n
\n
\n
\n
\n
\n

Sample Output 4

999999999\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n long z;\n long w;\n scanf(\"%ld %ld %ld\", &n, &z, &w);\n long a[n];\n for (long i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n }\n long score = labs(a[n - 1] - w);\n if (n != 1) {\n if (labs(a[n - 1] - a[n - 2]) > score) {\n score = labs(a[n - 1] - a[n - 2]);\n }\n }\n printf(\"%ld\\n\", score);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let s: Vec<&str> = buf.split_whitespace().collect();\n let n: usize = s[0].parse().unwrap();\n let w: i32 = s[2].parse().unwrap();\n\n let mut buf2 = String::new();\n io::stdin().read_line(&mut buf2).unwrap();\n let s2: Vec<&str> = buf2.split_whitespace().collect();\n let mut last1: i32 = 0;\n if n > 1 {\n last1 = s2[n - 2].parse().unwrap();\n }\n let last0: i32 = s2[n - 1].parse().unwrap();\n let choice0 = (last0 - w).abs();\n let mut choice1 = 0;\n if n > 1 {\n choice1 = (last1 - last0).abs();\n }\n println!(\"{}\", cmp::max(choice0, choice1));\n}", "difficulty": "hard"} {"problem_id": "0955", "problem_description": "You have three piles of candies: red, green and blue candies: the first pile contains only red candies and there are $$$r$$$ candies in it, the second pile contains only green candies and there are $$$g$$$ candies in it, the third pile contains only blue candies and there are $$$b$$$ candies in it. Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can't eat two candies of the same color in a day.Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.", "c_code": "int solution() {\n unsigned long r;\n unsigned long g;\n unsigned long b;\n short t;\n scanf(\"%hd\", &t);\n unsigned long *max1;\n unsigned long *max2;\n unsigned long *max3;\n while (t--) {\n scanf(\"%lu %lu %lu\", &r, &g, &b);\n if (r >= g && g >= b) {\n max1 = &r;\n max2 = &g;\n max3 = &b;\n } else if (r >= b && b >= g) {\n max1 = &r;\n max2 = &b;\n max3 = &g;\n } else if (g >= r && r >= b) {\n max1 = &g;\n max2 = &r;\n max3 = &b;\n } else if (g >= b && b >= r) {\n max1 = &g;\n max2 = &b;\n max3 = &r;\n } else if (b >= r && r >= g) {\n max1 = &b;\n max2 = &r;\n max3 = &g;\n } else {\n max1 = &b;\n max2 = &g;\n max3 = &r;\n }\n if (*max1 >= (*max2 + *max3)) {\n printf(\"%lu\\n\", *max2 + *max3);\n } else {\n printf(\"%lu\\n\", (*max1 + *max2 + *max3) / 2);\n }\n }\n return 0;\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\n let t = get!();\n\n for _ in 0..t {\n let r = get!();\n let g = get!();\n let b = get!();\n let mut vs = [r, g, b];\n vs.sort();\n let max = vs[2];\n let sum = vs[0..2].iter().sum();\n let min = vs[0];\n if max >= sum {\n println!(\"{}\", sum);\n } else {\n let diff = sum - max;\n let p = std::cmp::min(min * 2, diff);\n let ans = max + p / 2;\n println!(\"{}\", ans);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "0956", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given two integers A and B as the input. Output the value of A + B.

\n

However, if A + B is 10 or greater, output error instead.

\n
\n
\n
\n
\n

Constraints

    \n
  • A and B are integers.
  • \n
  • 1 ≤ A, B ≤ 9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

If A + B is 10 or greater, print the string error (case-sensitive); otherwise, print the value of A + B.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 3\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n
\n
\n
\n
\n
\n

Sample Input 2

6 4\n
\n
\n
\n
\n
\n

Sample Output 2

error\n
\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n scanf(\"%d%d\", &a, &b);\n if (a + b >= 10) {\n printf(\"error\\n\");\n } else {\n printf(\"%d\\n\", a + b);\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| i64::from_str(n).unwrap());\n let (a, b) = (it.next().unwrap(), it.next().unwrap());\n\n if a + b >= 10 {\n println!(\"error\");\n return;\n }\n println!(\"{}\", a + b);\n}", "difficulty": "easy"} {"problem_id": "0957", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have held a popularity poll for N items on sale. Item i received A_i votes.

\n

From these N items, we will select M as popular items. However, we cannot select an item with less than \\dfrac{1}{4M} of the total number of votes.

\n

If M popular items can be selected, print Yes; otherwise, print No.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq M \\leq N \\leq 100
  • \n
  • 1 \\leq A_i \\leq 1000
  • \n
  • A_i are distinct.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_1 ... A_N\n
\n
\n
\n
\n
\n

Output

If M popular items can be selected, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 1\n5 4 2 1\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

There were 12 votes in total. The most popular item received 5 votes, and we can select it.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 2\n380 19 1\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

There were 400 votes in total. The second and third most popular items received less than \\dfrac{1}{4\\times 2} of the total number of votes, so we cannot select them. Thus, we cannot select two popular items.

\n
\n
\n
\n
\n
\n

Sample Input 3

12 3\n4 56 78 901 2 345 67 890 123 45 6 789\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution() {\n int lineup[100];\n int M = 0;\n int N = 0;\n int i = 0;\n int j = 0;\n int k = 0;\n int add = 0;\n\n scanf(\"%d %d\", &N, &M);\n for (i = 0; i < N; i++) {\n scanf(\"%d\", &lineup[i]);\n add += lineup[i];\n }\n for (j = 0; j < N; j++) {\n if (lineup[j] * 4 * M >= add) {\n k++;\n }\n }\n if (k >= M) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let (_n, m) = {\n let mut sp = s.split_whitespace().map(|x| x.parse::().unwrap());\n (sp.next().unwrap(), sp.next().unwrap())\n };\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let a: Vec<_> = s\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let sum: usize = a.iter().sum();\n let line = (sum - 1) / (4 * m) + 1;\n let ok = a.iter().filter(|&&x| x >= line).count();\n\n if ok >= m {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "0958", "problem_description": "

\nReverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free.\n

\n\n

\nWrite a program which reads an expression in the Reverse Polish notation and prints the computational result.\n

\n\n

\nAn expression in the Reverse Polish notation is calculated using a stack. To evaluate the expression, the program should read symbols in order. If the symbol is an operand, the corresponding value should be pushed into the stack. On the other hand, if the symbols is an operator, the program should pop two elements from the stack, perform the corresponding operations, then push the result in to the stack. The program should repeat this operations.\n

\n\n\n

Input

\n\n

\nAn expression is given in a line. Two consequtive symbols (operand or operator) are separated by a space character.\n

\n\n

\nYou can assume that +, - and * are given as the operator and an operand is a positive integer less than 106\n

\n\n

Output

\n\n

\nPrint the computational result in a line.\n

\n\n

Constraints

\n\n

\n2 ≤ the number of operands in the expression ≤ 100
\n1 ≤ the number of operators in the expression ≤ 99
\n-1 × 109 ≤ values in the stack ≤ 109
\n

\n\n

Sample Input 1

\n
\n1 2 +\n
\n

Sample Output 1

\n
\n3\n
\n\n

Sample Input 2

\n
\n1 2 + 3 4 - *\n
\n

Sample Output 2

\n
\n-3\n
\n\n\n\n

Notes

\n\nTemplate in C", "c_code": "int solution(void) {\n int_fast32_t stack[100];\n int_fast8_t index = 0;\n char element[8];\n\n scanf(\"%\" SCNdFAST32, &stack[index]);\n ++index;\n scanf(\"%\" SCNdFAST32, &stack[index]);\n ++index;\n\n while (EOF != scanf(\" %s\", element)) {\n if ('+' == element[0]) {\n --index;\n stack[index - 1] += stack[index];\n } else if ('-' == element[0]) {\n --index;\n stack[index - 1] -= stack[index];\n } else if ('*' == element[0]) {\n --index;\n stack[index - 1] *= stack[index];\n } else {\n sscanf(element, \"%\" SCNdFAST32, &stack[index]);\n ++index;\n }\n }\n\n printf(\"%\" PRIdFAST32 \"\\n\", stack[0]);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut buf = String::new();\n stdin.read_line(&mut buf).ok();\n let buf = buf.trim();\n let chars: Vec<&str> = buf.split(' ').collect();\n let mut arr: Vec = Vec::new();\n for i in chars {\n match i {\n \"+\" | \"-\" | \"*\" => {\n let right = arr.pop().unwrap();\n let left = arr.pop().unwrap();\n let r = match i {\n \"+\" => left + right,\n \"-\" => left - right,\n \"*\" => left * right,\n _ => panic!(),\n };\n arr.push(r);\n }\n _ => arr.push(i.parse::().unwrap()),\n }\n }\n println!(\"{}\", arr[0]);\n}", "difficulty": "easy"} {"problem_id": "0959", "problem_description": "You are given an array $$$a_1, a_2, \\dots, a_n$$$. You can perform operations on the array. In each operation you can choose an integer $$$i$$$ ($$$1 \\le i < n$$$), and swap elements $$$a_i$$$ and $$$a_{i+1}$$$ of the array, if $$$a_i + a_{i+1}$$$ is odd.Determine whether it can be sorted in non-decreasing order using this operation any number of times.", "c_code": "int solution() {\n\n int t = 0;\n int n = 0;\n int flag1 = 0;\n int flag2 = 0;\n int j = 0;\n int k = 0;\n long long int arr[100000];\n long long int arr1[100000];\n long long int arr2[100000];\n scanf(\"%d\", &t);\n\n while (t--) {\n\n flag1 = 0;\n flag2 = 0;\n k = 0;\n j = 0;\n\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n\n scanf(\"%lld\", &arr[i]);\n\n if (arr[i] % 2 == 0) {\n\n arr1[j++] = arr[i];\n } else {\n\n arr2[k++] = arr[i];\n }\n }\n\n for (int i = 0; i < j - 1; i++) {\n\n if (arr1[i] > arr1[i + 1]) {\n\n flag1 = 1;\n break;\n }\n }\n\n for (int i = 0; i < k - 1; i++) {\n\n if (arr2[i] > arr2[i + 1]) {\n\n flag2 = 1;\n break;\n }\n }\n\n if (flag1 == 0 && flag2 == 0) {\n\n printf(\"YES\\n\");\n } else {\n\n printf(\"NO\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let k = std::io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n 'jester: for _ in 0..k {\n let _l = std::io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n let mut m = String::new();\n std::io::stdin().read_line(&mut m).unwrap();\n let m = m\n .trim()\n .split(\" \")\n .flat_map(str::parse::)\n .collect::>();\n let mut odds = 0;\n let mut evens = 0;\n 'clown: for els in m.iter().enumerate() {\n if els.1 % 2 == 0 {\n if *els.1 >= evens {\n evens = *els.1;\n } else {\n println!(\"No\");\n continue 'jester;\n }\n }\n if els.1 % 2 != 0 {\n if *els.1 >= odds {\n odds = *els.1\n } else {\n println!(\"No\");\n continue 'jester;\n }\n }\n }\n println!(\"Yes\");\n }\n}", "difficulty": "hard"} {"problem_id": "0960", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

A country decides to build a palace.

\n

In this country, the average temperature of a point at an elevation of x meters is T-x \\times 0.006 degrees Celsius.

\n

There are N places proposed for the place. The elevation of Place i is H_i meters.

\n

Among them, Princess Joisino orders you to select the place whose average temperature is the closest to A degrees Celsius, and build the palace there.

\n

Print the index of the place where the palace should be built.

\n

It is guaranteed that the solution is unique.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 1000
  • \n
  • 0 \\leq T \\leq 50
  • \n
  • -60 \\leq A \\leq T
  • \n
  • 0 \\leq H_i \\leq 10^5
  • \n
  • All values in input are integers.
  • \n
  • The solution is unique.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nT A\nH_1 H_2 ... H_N\n
\n
\n
\n
\n
\n

Output

Print the index of the place where the palace should be built.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n12 5\n1000 2000\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n
    \n
  • The average temperature of Place 1 is 12-1000 \\times 0.006=6 degrees Celsius.
  • \n
  • The average temperature of Place 2 is 12-2000 \\times 0.006=0 degrees Celsius.
  • \n
\n

Thus, the palace should be built at Place 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n21 -11\n81234 94124 52141\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int best_n = 0;\n int T = 0;\n int A = 0;\n int height_i[1001] = {0};\n int temp_average = 0;\n int temp_diff_from_a = 0;\n int new_temp_diff_from_a = 0;\n scanf(\"%d\", &n);\n scanf(\"%d %d\", &T, &A);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &height_i[i]);\n }\n\n for (int i = 0; i < n; i++) {\n temp_average = 1000 * T - height_i[i] * 6;\n new_temp_diff_from_a = abs((1000 * A) - temp_average);\n\n if (i == 0 || new_temp_diff_from_a < temp_diff_from_a) {\n temp_diff_from_a = new_temp_diff_from_a;\n best_n = i;\n }\n }\n printf(\"%d\", best_n + 1);\n return 0;\n}", "rust_code": "fn solution() {\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 ta = String::new();\n stdin().read_line(&mut ta).unwrap();\n let ta: Vec = ta.split_whitespace().flat_map(str::parse).collect();\n let (t, a) = (ta[0] * 1000, ta[1] * 1000);\n\n let mut harr = String::new();\n stdin().read_line(&mut harr).unwrap();\n let harr: Vec<_> = harr\n .split_whitespace()\n .flat_map(str::parse::)\n .map(|h| {\n let t = t - a - h * 6;\n if t >= 0 {\n t\n } else {\n -t\n }\n })\n .collect();\n let mut result = None;\n for (i, &h) in harr.iter().enumerate() {\n if result.is_none() {\n result = Some((i, h));\n } else if let Some((_, oh)) = result\n && oh > h {\n result = Some((i, h));\n }\n }\n if let Some((i, _)) = result {\n println!(\"{}\", i + 1);\n }\n}", "difficulty": "medium"} {"problem_id": "0961", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given a three-digit positive integer N.
\nDetermine whether N is a palindromic number.
\nHere, a palindromic number is an integer that reads the same backward as forward in decimal notation.

\n
\n
\n
\n
\n

Constraints

    \n
  • 100≤N≤999
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If N is a palindromic number, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

575\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

N=575 is also 575 when read backward, so it is a palindromic number. You should print Yes.

\n
\n
\n
\n
\n
\n

Sample Input 2

123\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

N=123 becomes 321 when read backward, so it is not a palindromic number. You should print No.

\n
\n
\n
\n
\n
\n

Sample Input 3

812\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n\n scanf(\"%d\", &n);\n\n if (100 <= n && n <= 999) {\n int up = 0;\n int dn = 0;\n up = n / 100;\n dn = n % 10;\n if (up == dn) {\n printf(\"Yes\");\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 let s: Vec = itr.next().unwrap().chars().collect();\n if s[0] == s[2] {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "hard"} {"problem_id": "0962", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).\nPrint the string S after lowercasing the K-th character in it.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 50
  • \n
  • 1 ≤ K ≤ N
  • \n
  • S is a string of length N consisting of A, B and C.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nS\n
\n
\n
\n
\n
\n

Output

Print the string S after lowercasing the K-th character in it.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1\nABC\n
\n
\n
\n
\n
\n

Sample Output 1

aBC\n
\n
\n
\n
\n
\n
\n

Sample Input 2

4 3\nCABA\n
\n
\n
\n
\n
\n

Sample Output 2

CAbA\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int K = 0;\n scanf(\"%d\", &N);\n scanf(\"%d\", &K);\n\n char line[N];\n\n for (int i = 0; i <= N; i++) {\n scanf(\"%c\", &line[i]);\n }\n\n line[K] = line[K] + 32;\n\n for (int i = 0; i <= N; i++) {\n printf(\"%c\", line[i]);\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut nk = String::new();\n let mut s = String::new();\n\n stdin().read_line(&mut nk).unwrap();\n stdin().read_line(&mut s).unwrap();\n\n let k: usize = nk.split_whitespace().collect::>()[1]\n .parse::()\n .unwrap();\n\n let mut s = s.trim().chars().collect::>();\n s[k - 1] = (s[k - 1] as u8 + 32) as char;\n\n println!(\"{}\", s.into_iter().collect::());\n}", "difficulty": "medium"} {"problem_id": "0963", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally.

\n

Takahashi is standing at Vertex u, and Aoki is standing at Vertex v.

\n

Now, they will play a game of tag as follows:

\n
    \n
  • \n

    1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex.

    \n
  • \n
  • \n

    2. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex.

    \n
  • \n
  • \n

    3. Go back to step 1.

    \n
  • \n
\n

Takahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible.

\n

Find the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy.

\n

It can be proved that the game is bound to end.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq u,v \\leq N
  • \n
  • u \\neq v
  • \n
  • 1 \\leq A_i,B_i \\leq N
  • \n
  • The given graph is a tree.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N u v\nA_1 B_1\n:\nA_{N-1} B_{N-1}\n
\n
\n
\n
\n
\n

Output

Print the number of moves Aoki will perform before the end of the game.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 4 1\n1 2\n2 3\n3 4\n3 5\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

If both players play optimally, the game will progress as follows:

\n
    \n
  • Takahashi moves to Vertex 3.
  • \n
  • Aoki moves to Vertex 2.
  • \n
  • Takahashi moves to Vertex 5.
  • \n
  • Aoki moves to Vertex 3.
  • \n
  • Takahashi moves to Vertex 3.
  • \n
\n

Here, Aoki performs two moves.

\n

Note that, in each move, it is prohibited to stay at the current vertex.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 4 5\n1 2\n1 3\n1 4\n1 5\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2 1 2\n1 2\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
\n
\n
\n
\n

Sample Input 4

9 6 1\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n7 8\n8 9\n
\n
\n
\n
\n
\n

Sample Output 4

5\n
\n
\n
", "c_code": "int solution() {\n int i;\n int u;\n int w;\n int N;\n int T;\n int A;\n scanf(\"%d %d %d\", &N, &T, &A);\n list **adj = (list **)malloc(sizeof(list *) * (N + 1));\n list *d = (list *)malloc(sizeof(list) * (N - 1) * 2);\n for (i = 1; i <= N; i++) {\n adj[i] = NULL;\n }\n for (i = 0; i < N - 1; i++) {\n scanf(\"%d %d\", &u, &w);\n d[i * 2].v = w;\n d[(i * 2) + 1].v = u;\n d[i * 2].next = adj[u];\n d[(i * 2) + 1].next = adj[w];\n adj[u] = &(d[i * 2]);\n adj[w] = &(d[(i * 2) + 1]);\n }\n\n int dist[2][100001] = {};\n int q[100001];\n int head;\n int tail;\n list *p;\n dist[0][A] = 1;\n q[0] = A;\n for (head = 0, tail = 1; head < tail; head++) {\n u = q[head];\n for (p = adj[u]; p != NULL; p = p->next) {\n w = p->v;\n if (dist[0][w] == 0) {\n dist[0][w] = dist[0][u] + 1;\n q[tail++] = w;\n }\n }\n }\n dist[1][T] = 1;\n q[0] = T;\n for (head = 0, tail = 1; head < tail; head++) {\n u = q[head];\n for (p = adj[u]; p != NULL; p = p->next) {\n w = p->v;\n if (dist[1][w] == 0 && dist[1][u] + 1 < dist[0][w]) {\n dist[1][w] = dist[1][u] + 1;\n q[tail++] = w;\n }\n }\n }\n\n int max = 0;\n for (i = 1; i <= N; i++) {\n if (dist[1][i] > 0 && dist[0][i] - 2 > max) {\n max = dist[0][i] - 2;\n }\n }\n printf(\"%d\\n\", max);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n u: usize,\n v: usize,\n ab: [(usize, usize); n - 1]\n }\n let mut adjacency = vec![vec![]; n + 1];\n for (ai, bi) in ab {\n adjacency[ai].push(bi);\n adjacency[bi].push(ai);\n }\n\n let mut height = vec![0; n + 1];\n let mut parent = vec![0; n + 1];\n let mut queue = VecDeque::new();\n let mut visited = HashSet::new();\n queue.push_back(v);\n while !queue.is_empty() {\n let w = queue.pop_front().unwrap();\n visited.insert(w);\n for &x in adjacency[w].iter() {\n if !visited.contains(&x) {\n height[x] = height[w] + 1;\n parent[x] = w;\n queue.push_back(x);\n }\n }\n }\n\n let h = (height[u] - 1) / 2;\n let mut w = u;\n for _ in 0..h {\n w = parent[w];\n }\n\n let mut max_node = u;\n let mut queue = VecDeque::new();\n let mut visited = HashSet::new();\n queue.push_back(w);\n while !queue.is_empty() {\n let x = queue.pop_front().unwrap();\n visited.insert(x);\n for &y in adjacency[x].iter() {\n if !visited.contains(&y) && height[y] > height[w] {\n let _d = height[y] - height[w];\n if height[y] > height[max_node] {\n max_node = y;\n }\n queue.push_back(y);\n }\n }\n }\n\n println!(\"{}\", height[max_node] - 1);\n}", "difficulty": "medium"} {"problem_id": "0964", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You have decided to write a book introducing good restaurants.\nThere are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.\nNo two restaurants have the same score.

\n

You want to introduce the restaurants in the following order:

\n
    \n
  • The restaurants are arranged in lexicographical order of the names of their cities.
  • \n
  • If there are multiple restaurants in the same city, they are arranged in descending order of score.
  • \n
\n

Print the identification numbers of the restaurants in the order they are introduced in the book.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 100
  • \n
  • S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
  • \n
  • 0 ≤ P_i ≤ 100
  • \n
  • P_i is an integer.
  • \n
  • P_i ≠ P_j (1 ≤ i < j ≤ N)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS_1 P_1\n:\nS_N P_N\n
\n
\n
\n
\n
\n

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\nkhabarovsk 20\nmoscow 10\nkazan 50\nkazan 35\nmoscow 60\nkhabarovsk 40\n
\n
\n
\n
\n
\n

Sample Output 1

3\n4\n6\n1\n5\n2\n
\n

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\nyakutsk 10\nyakutsk 20\nyakutsk 30\nyakutsk 40\nyakutsk 50\nyakutsk 60\nyakutsk 70\nyakutsk 80\nyakutsk 90\nyakutsk 100\n
\n
\n
\n
\n
\n

Sample Output 2

10\n9\n8\n7\n6\n5\n4\n3\n2\n1\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int P[1000];\n char S[1000][100];\n int answer[1000] = {0};\n int count = 0;\n scanf(\"%d\", &N);\n for (int i = 0; i < N; i++) {\n scanf(\"%s %d\", S[i], &P[i]);\n }\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if (strcmp(S[i], S[j]) > 0) {\n count++;\n } else if (strcmp(S[i], S[j]) == 0) {\n if (P[i] < P[j]) {\n count++;\n }\n }\n }\n answer[count] = i;\n count = 0;\n }\n for (int i = 0; i < N; i++) {\n printf(\"%d\\n\", answer[i] + 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n\n let n = {\n let mut input = String::new();\n stdin.read_line(&mut input).unwrap();\n let mut input = input.split_whitespace();\n input.next().unwrap().parse::().unwrap()\n };\n\n let mut vec = {\n let mut vec: Vec<(String, i32, usize)> = Vec::new();\n for i in 0..n {\n let mut input = String::new();\n stdin.read_line(&mut input).unwrap();\n let mut input = input.split_whitespace();\n\n vec.push((\n input.next().unwrap().to_string(),\n input.next().unwrap().parse::().unwrap(),\n i + 1,\n ));\n }\n vec\n };\n\n vec.sort_by(|a, b| {\n if a.0 == b.0 {\n b.1.cmp(&a.1)\n } else {\n a.0.cmp(&b.0)\n }\n });\n\n for x in vec.iter() {\n println!(\"{}\", x.2);\n }\n}", "difficulty": "hard"} {"problem_id": "0965", "problem_description": "You are given two strings of equal length $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.For example, if $$$s$$$ is \"acbc\" you can get the following strings in one operation: \"aabc\" (if you perform $$$s_2 = s_1$$$); \"ccbc\" (if you perform $$$s_1 = s_2$$$); \"accc\" (if you perform $$$s_3 = s_2$$$ or $$$s_3 = s_4$$$); \"abbc\" (if you perform $$$s_2 = s_3$$$); \"acbb\" (if you perform $$$s_4 = s_3$$$); Note that you can also apply this operation to the string $$$t$$$.Please determine whether it is possible to transform $$$s$$$ into $$$t$$$, applying the operation above any number of times.Note that you have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int m;\n scanf(\"%d\", &m);\n while (m--) {\n char s[101];\n char t[101];\n scanf(\"%s %s\", s, t);\n int n = strlen(s);\n int flag = 0;\n int cnts[26] = {0};\n int cntf[26] = {0};\n for (int i = 0; i < n; i++) {\n cnts[s[i] - 'a']++;\n cntf[t[i] - 'a']++;\n }\n for (int i = 0; i < 26; i++) {\n if (cnts[i] != 0 && cntf[i] != 0) {\n printf(\"YES\\n\");\n flag = 1;\n break;\n }\n }\n if (!flag) {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let q: u8 = buffer.trim().parse().expect(\"invalid input\");\n\n for _ in 0..q {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let s = input.trim();\n\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();\n\n let mut s_chars = [false; 26];\n for c in s.chars() {\n s_chars[c as usize - 'a' as usize] = true;\n }\n\n let mut found = false;\n for c in t.chars() {\n if s_chars[c as usize - 'a' as usize] {\n found = true;\n break;\n }\n }\n\n println!(\"{}\", if found { \"YES\" } else { \"NO\" });\n }\n}", "difficulty": "easy"} {"problem_id": "0966", "problem_description": "

Official House

\n\n

\nYou manage 4 buildings, each of which has 3 floors, each of which consists of 10 rooms. Write a program which reads a sequence of tenant/leaver notices, and reports the number of tenants for each room.\n

\n\n

\n For each notice, you are given four integers b, f, r and v which represent that v persons entered to room r of fth floor at building b. If v is negative, it means that −v persons left.\n

\n\n

\nAssume that initially no person lives in the building. \n

\n\n\n

Input

\n

\n In the first line, the number of notices n is given. In the following n lines, a set of four integers b, f, r and v which represents ith notice is given in a line. \n

\n\n\n

Output

\n\n

\nFor each building, print the information of 1st, 2nd and 3rd floor in this order. For each floor information, print the number of tenants of 1st, 2nd, .. and 10th room in this order. Print a single space character before the number of tenants. Print \"####################\" (20 '#') between buildings.\n

\n\n

Constraints

\n\n
    \n
  • No incorrect building, floor and room numbers are given.
  • \n
  • 0 ≤ the number of tenants during the management ≤ 9
  • \n
\n\n

Sample Input

\n\n
\n3\n1 1 3 8\n3 2 2 7\n4 3 8 1\n
\n\n\n\n

Sample Output

\n\n
\n 0 0 8 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0\n####################\n 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0\n####################\n 0 0 0 0 0 0 0 0 0 0\n 0 7 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0\n####################\n 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 1 0 0\n
", "c_code": "int solution() {\n int n = 0;\n int i = 0;\n int j = 0;\n int k = 0;\n int l = 0;\n int b = 0;\n int f = 0;\n int r = 0;\n int v = 0;\n int a[4][3][10] = {0};\n\n scanf(\"%d\", &n);\n\n for (i = 0; i < n; i++) {\n scanf(\"%d %d %d %d\", &b, &f, &r, &v);\n a[b - 1][f - 1][r - 1] += v;\n }\n\n for (j = 0; j < 4; j++) {\n for (k = 0; k < 3; k++) {\n for (l = 0; l < 10; l++) {\n printf(\" %d\", a[j][k][l]);\n }\n printf(\"\\n\");\n }\n\n if (j != 3) {\n printf(\"####################\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).ok();\n let n = line.trim().parse::().unwrap();\n\n let mut notices = Vec::new();\n for _ in 0..n {\n let mut line = String::new();\n io::stdin().read_line(&mut line).ok();\n let mut iter = line.split_whitespace();\n let b = iter.next().unwrap().parse::().unwrap();\n let f = iter.next().unwrap().parse::().unwrap();\n let r = iter.next().unwrap().parse::().unwrap();\n let v = iter.next().unwrap().parse::().unwrap();\n notices.push((b, f, r, v));\n }\n\n let n_buildings = 4;\n let n_floors = n_buildings * 3;\n let n_rooms = n_floors * 10;\n let mut rooms = vec![0; n_rooms];\n for notice in notices {\n let (b, f, r, v) = notice;\n rooms[(b - 1) * 30 + (f - 1) * 10 + (r - 1)] += v;\n }\n\n for b in 0..4 {\n if b != 0 {\n println!(\"####################\");\n }\n for f in 0..3 {\n for r in 0..10 {\n print!(\" {}\", rooms[b * 30 + f * 10 + r]);\n }\n println!();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0967", "problem_description": "\n\n\n

Integral

\n\n

\nWrite a program which computes the area of a shape represented by the following three lines:
\n
\n$y = x^2$
\n$y = 0$
\n$x = 600$
\n
\n\n

\n\n

\nIt is clear that the area is $72000000$, if you use an integral you learn in high school. On the other hand, we can obtain an approximative area of the shape by adding up areas of many rectangles in the shape as shown in the following figure:\n

\n\n

\n$f(x) = x^2$
\n
\n
\n\n\n

\nThe approximative area $s$ where the width of the rectangles is $d$ is:
\n
\narea of rectangle where its width is $d$ and height is $f(d)$ $+$
\narea of rectangle where its width is $d$ and height is $f(2d)$ $+$
\narea of rectangle where its width is $d$ and height is $f(3d)$ $+$
\n...
\narea of rectangle where its width is $d$ and height is $f(600 - d)$
\n

\n\n

\nThe more we decrease $d$, the higer-precision value which is close to $72000000$ we could obtain. Your program should read the integer $d$ which is a divisor of $600$, and print the area $s$.\n

\n\n

Input

\n\n

\nThe input consists of several datasets. Each dataset consists of an integer $d$ in a line. The number of datasets is less than or equal to 20.\n

\n\n

Output

\n\n

\nFor each dataset, print the area $s$ in a line.\n

\n\n

Sample Input

\n\n
\n20\n10\n
\n\n

Output for the Sample Input

\n\n
\n68440000\n70210000\n
", "c_code": "int solution(void) {\n int d = 0;\n int i = 0;\n int f = 0;\n int j = 1;\n\n while (scanf(\"%d\", &d) != EOF) {\n for (i = 0; i < 600; i += d) {\n f = f + (((600 - (j * d)) * (600 - (j * d))) * d);\n j++;\n }\n printf(\"%d\\n\", f);\n f = 0;\n j = 1;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let input = BufReader::new(stdin());\n for line in input.lines() {\n let d = line.unwrap().parse::().unwrap();\n let mut s = 0;\n let mut x = d;\n while x < 600 {\n s += x * x * d;\n x += d;\n }\n println!(\"{}\", s);\n }\n}", "difficulty": "easy"} {"problem_id": "0968", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.\nThe root is Vertex 1, and the i-th edge (1 \\leq i \\leq N - 1) connects Vertex a_i and b_i.

\n

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

\n

Now, the following Q operations will be performed:

\n
    \n
  • Operation j (1 \\leq j \\leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.
  • \n
\n

Find the value of the counter on each vertex after all operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq Q \\leq 2 \\times 10^5
  • \n
  • 1 \\leq a_i < b_i \\leq N
  • \n
  • 1 \\leq p_j \\leq N
  • \n
  • 1 \\leq x_j \\leq 10^4
  • \n
  • The given graph is a tree.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N Q\na_1 b_1\n:\na_{N-1} b_{N-1}\np_1 x_1\n:\np_Q x_Q\n
\n
\n
\n
\n
\n

Output

Print the values of the counters on Vertex 1, 2, \\ldots, N after all operations, in this order, with spaces in between.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3\n1 2\n2 3\n2 4\n2 10\n1 100\n3 1\n
\n
\n
\n
\n
\n

Sample Output 1

100 110 111 110\n
\n

The tree in this input is as follows:

\n

\"Figure\"

\n

Each operation changes the values of the counters on the vertices as follows:

\n
    \n
  • Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.
  • \n
  • Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.
  • \n
  • Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

6 2\n1 2\n1 3\n2 4\n3 6\n2 5\n1 10\n1 10\n
\n
\n
\n
\n
\n

Sample Output 2

20 20 20 20 20 20\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int q;\n int i;\n scanf(\"%d%d\", &n, &q);\n Node *node;\n node = (Node *)calloc(n + 1, sizeof(Node));\n int a;\n int b;\n for (i = 1; i < n; i++) {\n scanf(\"%d%d\", &a, &b);\n (node[b].parent) = &node[a];\n }\n int p;\n int x;\n for (i = 0; i < q; i++) {\n scanf(\"%d%d\", &p, &x);\n (node[p].cnt) += x;\n }\n\n for (i = 1; i <= n; i++) {\n if (i != 1) {\n node[i].cnt += (node[i].parent)->cnt;\n }\n printf(\"%lld \", node[i].cnt);\n }\n printf(\"\\n\");\n free(node);\n return 0;\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 n = it.next().unwrap().parse::().unwrap();\n let q = it.next().unwrap().parse::().unwrap();\n let mut g = vec![Vec::new(); n];\n for _ in 0..n - 1 {\n let a = it.next().unwrap().parse::().unwrap() - 1;\n let b = it.next().unwrap().parse::().unwrap() - 1;\n g[a].push(b);\n g[b].push(a);\n }\n let mut res = vec![0; n];\n for _ in 0..q {\n let p = it.next().unwrap().parse::().unwrap() - 1;\n let x = it.next().unwrap().parse::().unwrap();\n res[p] += x;\n }\n let mut qu = VecDeque::new();\n qu.push_back(0);\n let mut visit = vec![0; n];\n visit[0] = 1;\n while !qu.is_empty() {\n let p = qu.pop_front().unwrap();\n for &t in &g[p] {\n if visit[t] == 1 {\n continue;\n }\n visit[t] = 1;\n res[t] += res[p];\n qu.push_back(t);\n }\n }\n print!(\"{}\", res[0]);\n for i in 1..n {\n print!(\" {}\", res[i]);\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "0969", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

We have a 3 \\times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.
\nAccording to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.
\nDetermine if he is correct.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • c_{i, j} \\ (1 \\leq i \\leq 3, 1 \\leq j \\leq 3) is an integer between 0 and 100 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
c_{1,1} c_{1,2} c_{1,3}\nc_{2,1} c_{2,2} c_{2,3}\nc_{3,1} c_{3,2} c_{3,3}\n
\n
\n
\n
\n
\n

Output

\n

If Takahashi's statement is correct, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 0 1\n2 1 2\n1 0 1\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2 2\n2 1 2\n2 2 2\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

Takahashi is incorrect in this case.

\n
\n
\n
\n
\n
\n

Sample Input 3

0 8 8\n0 8 8\n0 8 8\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1 8 6\n2 9 7\n0 7 7\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
", "c_code": "int solution() {\n int C[10] = {0};\n for (int i = 1; i <= 9; i++) {\n scanf(\"%d\", &C[i]);\n }\n if ((C[1] - C[4] == C[2] - C[5] && C[1] - C[4] == C[3] - C[6]) &&\n (C[4] - C[7] == C[5] - C[8] && C[4] - C[7] == C[6] - C[9]) &&\n (C[1] - C[7] == C[2] - C[8] && C[1] - C[7] == C[3] - C[9]) &&\n (C[1] - C[2] == C[4] - C[5] && C[1] - C[2] == C[7] - C[8]) &&\n (C[1] - C[3] == C[4] - C[6] && C[1] - C[3] == C[7] - C[9]) &&\n (C[2] - C[3] == C[5] - C[6] && C[2] - C[3] == C[8] - C[9])) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\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 let c: Vec = (0..9)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n for a1 in 1..4 {\n let b1 = c[0] - a1;\n let b2 = c[1] - a1;\n let b3 = c[2] - a1;\n let a2 = c[3] - b1;\n let a3 = c[6] - b1;\n if c[4] == a2 + b2 && c[5] == a2 + b3 && c[7] == a3 + b2 && c[8] == a3 + b3 {\n println!(\"Yes\");\n return;\n }\n }\n println!(\"No\");\n}", "difficulty": "easy"} {"problem_id": "0970", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Snuke has a favorite restaurant.

\n

The price of any meal served at the restaurant is 800 yen (the currency of Japan), and each time a customer orders 15 meals, the restaurant pays 200 yen back to the customer.

\n

So far, Snuke has ordered N meals at the restaurant.\nLet the amount of money Snuke has paid to the restaurant be x yen, and let the amount of money the restaurant has paid back to Snuke be y yen.\nFind x-y.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

20\n
\n
\n
\n
\n
\n

Sample Output 1

15800\n
\n

So far, Snuke has paid 16000 yen, and the restaurant has paid back 200 yen. Thus, the answer is 15800.

\n
\n
\n
\n
\n
\n

Sample Input 2

60\n
\n
\n
\n
\n
\n

Sample Output 2

47200\n
\n

Snuke has paid 48000 yen for 60 meals, and the restaurant has paid back 800 yen.

\n
\n
", "c_code": "int solution() {\n int *var = malloc(sizeof(int));\n scanf(\"%d\", var);\n printf(\"%d\\n\", (*var * 800) - (200 * (*var / 15)));\n free(var);\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n\n let mut input = String::new();\n let _ = io::stdin().read_line(&mut input);\n let num = input.trim().parse::().unwrap();\n\n println!(\"{}\", num * 800 - (num / 15) * 200);\n}", "difficulty": "easy"} {"problem_id": "0971", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

An X-layered kagami mochi (X ≥ 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.

\n

Lunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 100
  • \n
  • 1 ≤ d_i ≤ 100
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nd_1\n:\nd_N\n
\n
\n
\n
\n
\n

Output

Print the maximum number of layers in a kagami mochi that can be made.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n10\n8\n8\n6\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

If we stack the mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, we have a 3-layered kagami mochi, which is the maximum number of layers.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n15\n15\n15\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

When all the mochi have the same diameter, we can only have a 1-layered kagami mochi.

\n
\n
\n
\n
\n
\n

Sample Input 3

7\n50\n30\n50\n100\n50\n80\n30\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n
\n
", "c_code": "int solution() {\n int num = 0;\n int num_mochies = 0;\n\n int num_steps = 1;\n\n scanf(\"%d\\n\", &num_mochies);\n\n int mochies[num_mochies];\n\n for (int i = 0; i < num_mochies; i++) {\n scanf(\"%d\", &mochies[i]);\n }\n\n for (int j = 0; j < num_mochies; j++) {\n for (int i = 0; i < num_mochies - (j + 1); i++) {\n if (mochies[i] < mochies[i + 1]) {\n num = mochies[i];\n mochies[i] = mochies[i + 1];\n mochies[i + 1] = num;\n }\n }\n }\n\n for (int k = 0; k < num_mochies - 1; k++) {\n if (mochies[k] > mochies[k + 1]) {\n num_steps++;\n }\n }\n\n printf(\"%d\\n\", num_steps);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf: String = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let n: i32 = buf.trim().parse::().unwrap();\n let mut mochi: Vec = Vec::new();\n for _ in 0..n {\n let mut buf: String = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let m: i32 = buf.trim().parse::().unwrap();\n mochi.push(m);\n }\n mochi.sort();\n mochi.dedup();\n println!(\"{}\", mochi.len());\n}", "difficulty": "hard"} {"problem_id": "0972", "problem_description": "

Simultaneous Equation

\n\n

\nWrite a program which solve a simultaneous equation:
\n
\n ax + by = c
\n dx + ey = f
\n
\n\nThe program should print x and y for given a, b, c, d, e and f (-1,000 ≤ a, b, c, d, e, f ≤ 1,000). You can suppose that given equation has a unique solution.\n

\n\n\n

Input

\n\n

\nThe input consists of several data sets, 1 line for each data set. In a data set, there will be a, b, c, d, e, f separated by a single space. The input terminates with EOF.\n

\n\n

Output

\n\n

\nFor each data set, print x and y separated by a single space. Print the solution to three places of decimals. Round off the solution to three decimal places.\n

\n\n

Sample Input 1

\n
\n1 2 3 4 5 6\n2 -1 -2 -1 -1 -5\n
\n\n

Output for the Sample Input 1

\n
\n-1.000 2.000\n1.000 4.000\n
\n\n

Sample Input 2

\n\n
\n2 -1 -3 1 -1 -3\n2 -1 -3 -9 9 27\n
\n\n

Output for the Sample Input 2

\n\n
\n0.000 3.000\n0.000 3.000\n
", "c_code": "int solution(void) {\n float a = 0;\n float b = 0;\n float c = 0;\n float d = 0;\n float e = 0;\n float f = 0;\n while (scanf(\"%f %f %f %f %f %f\", &a, &b, &c, &d, &e, &f) != EOF) {\n float x = 0;\n float y = 0;\n y = (c * d - a * f) / (b * d - a * e);\n x = c / a - (b / a) * y;\n x = roundf(1000 * x) / 1000;\n y = roundf(1000 * y) / 1000;\n printf(\"%.3f %.3f\\n\", x, y);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let input = BufReader::new(stdin());\n for line in input.lines() {\n let v: Vec = line\n .unwrap()\n .split_whitespace()\n .filter_map(|x| x.parse::().ok())\n .collect();\n let x = (v[2] * v[4] - v[1] * v[5]) / (v[0] * v[4] - v[1] * v[3]);\n let y = (v[2] * v[3] - v[0] * v[5]) / (v[1] * v[3] - v[0] * v[4]);\n println!(\"{:.3} {:.3}\", x, y);\n }\n}", "difficulty": "medium"} {"problem_id": "0973", "problem_description": "After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the queue wants either to take several ice cream packs for himself and his friends or to give several ice cream packs to Kay and Gerda (carriers that bring ice cream have to stand in the same queue).If a carrier with d ice cream packs comes to the house, then Kay and Gerda take all his packs. If a child who wants to take d ice cream packs comes to the house, then Kay and Gerda will give him d packs if they have enough ice cream, otherwise the child will get no ice cream at all and will leave in distress.Kay wants to find the amount of ice cream they will have after all people will leave from the queue, and Gerda wants to find the number of distressed kids.", "c_code": "int solution() {\n int n = 0;\n int i = 0;\n int s = 1;\n int distress = 0;\n long long iceCream = 0;\n long long in = 0;\n char sig;\n scanf(\"%d %lld\\n\", &n, &iceCream);\n for (i = 0; i < n; i++) {\n scanf(\"%c %lld\\n\", &sig, &in);\n if (sig == '-') {\n s = -1;\n } else {\n s = 1;\n }\n if (iceCream + (in * s) < 0) {\n distress++;\n } else {\n iceCream += (in * s);\n }\n }\n printf(\"%lld %d\\n\", iceCream, distress);\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let n;\n let mut x;\n {\n let mut ns = buf.split(\" \").map(|str| str.trim().parse::().unwrap());\n n = ns.next().unwrap();\n x = ns.next().unwrap();\n }\n let mut distressed = 0;\n for _ in 0..n {\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n let mut b = buf.split(\" \");\n let sign = b.next().unwrap().trim();\n let m = b.next().unwrap().trim().parse::().unwrap();\n if sign == \"+\" {\n x += m;\n } else if sign == \"-\" {\n if x >= m {\n x -= m;\n } else {\n distressed += 1;\n }\n }\n }\n println!(\"{} {}\", x, distressed);\n}", "difficulty": "hard"} {"problem_id": "0974", "problem_description": "You have a sequence $$$a$$$ with $$$n$$$ elements $$$1, 2, 3, \\dots, k - 1, k, k - 1, k - 2, \\dots, k - (n - k)$$$ ($$$k \\le n < 2k$$$).Let's call as inversion in $$$a$$$ a pair of indices $$$i < j$$$ such that $$$a[i] > a[j]$$$.Suppose, you have some permutation $$$p$$$ of size $$$k$$$ and you build a sequence $$$b$$$ of size $$$n$$$ in the following manner: $$$b[i] = p[a[i]]$$$.Your goal is to find such permutation $$$p$$$ that the total number of inversions in $$$b$$$ doesn't exceed the total number of inversions in $$$a$$$, and $$$b$$$ is lexicographically maximum.Small reminder: the sequence of $$$k$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$k$$$ exactly once.Another small reminder: a sequence $$$s$$$ is lexicographically smaller than another sequence $$$t$$$, if either $$$s$$$ is a prefix of $$$t$$$, or for the first $$$i$$$ such that $$$s_i \\ne t_i$$$, $$$s_i < t_i$$$ holds (in the first position that these sequences are different, $$$s$$$ has smaller number than $$$t$$$).", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n int i;\n int a[n];\n int b[k];\n for (i = 0; i < n; i++) {\n if (i < k) {\n a[i] = i + 1;\n } else {\n a[i] = a[i - 1] - 1;\n }\n }\n int j = 0;\n for (i = 0; i < k; i++) {\n if (i < 2 * k - n - 1) {\n b[i] = a[i];\n } else {\n b[i] = a[k - 1 - j];\n j = j + 1;\n }\n }\n\n for (i = 0; i < k; i++) {\n printf(\"%d \", b[i]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n\n let mut lines = input.lines();\n\n lines.next();\n\n for line in lines {\n let mut it = line.split_whitespace().map(|x| x.parse::().unwrap());\n let n = it.next().unwrap();\n let k = it.next().unwrap();\n\n let mut p: Vec<_> = (1..=k).collect();\n\n p[2 * k - n - 1..].reverse();\n\n for x in p {\n print!(\"{} \", x);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "0975", "problem_description": "You are both a shop keeper and a shop assistant at a small nearby shop. You have $$$n$$$ goods, the $$$i$$$-th good costs $$$a_i$$$ coins.You got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all $$$n$$$ goods you have.However, you don't want to lose any money so you want to choose the price in such a way that the sum of new prices is not less than the sum of the initial prices. It means that if you sell all $$$n$$$ goods for the new price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.On the other hand, you don't want to lose customers because of big prices so among all prices you can choose you need to choose the minimum one.So you need to find the minimum possible equal price of all $$$n$$$ goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 1; i <= t; i++) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int sum = 0;\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n sum += a[j];\n }\n int c = sum / n;\n if ((c * n) < sum) {\n c++;\n }\n printf(\"%d\\n\", c);\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 tests: isize = buf.trim().parse().unwrap();\n for _ in 0..tests {\n buf.clear();\n stdin.read_line(&mut buf).unwrap();\n let n: i64 = buf.trim().parse().unwrap();\n buf.clear();\n stdin.read_line(&mut buf).unwrap();\n let total = buf\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .sum::();\n println!(\"{}\", (total + n - 1) / n);\n }\n}", "difficulty": "medium"} {"problem_id": "0976", "problem_description": "Ujan decided to make a new wooden roof for the house. He has $$$n$$$ rectangular planks numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th plank has size $$$a_i \\times 1$$$ (that is, the width is $$$1$$$ and the height is $$$a_i$$$).Now, Ujan wants to make a square roof. He will first choose some of the planks and place them side by side in some order. Then he will glue together all of these planks by their vertical sides. Finally, he will cut out a square from the resulting shape in such a way that the sides of the square are horizontal and vertical.For example, if Ujan had planks with lengths $$$4$$$, $$$3$$$, $$$1$$$, $$$4$$$ and $$$5$$$, he could choose planks with lengths $$$4$$$, $$$3$$$ and $$$5$$$. Then he can cut out a $$$3 \\times 3$$$ square, which is the maximum possible. Note that this is not the only way he can obtain a $$$3 \\times 3$$$ square. What is the maximum side length of the square Ujan can get?", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\\n\", &n);\n\n int a[n];\n int m[n][1000];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\\n\", &a[i]);\n\n for (int j = 0; j < a[i]; j++) {\n scanf(\"%d \", &m[i][j]);\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < a[i]; j++) {\n for (int k = j + 1; k < a[i]; k++) {\n if (m[i][j] < m[i][k]) {\n int l = m[i][j];\n m[i][j] = m[i][k];\n m[i][k] = l;\n }\n }\n }\n for (int j = 0; j < a[i]; j++) {\n int temp = 0;\n for (int k = 0; k < a[i]; k++) {\n if ((a[i] - j) <= m[i][k]) {\n temp++;\n }\n }\n if (temp >= (a[i] - j)) {\n printf(\"%d\\n\", a[i] - j);\n break;\n }\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n let mut pieces: Vec = Vec::new();\n\n stdin().read_line(&mut input);\n\n let test_count = input.trim().parse::().unwrap();\n input.clear();\n\n for _test in 0..test_count {\n pieces.clear();\n stdin().read_line(&mut input);\n let _piece_count = input.trim().parse::().unwrap();\n input.clear();\n stdin().read_line(&mut input);\n for piece in input.split(\" \") {\n pieces.push(piece.trim().parse::().unwrap());\n }\n input.clear();\n\n pieces.sort_by(|l, r| Ord::cmp(&r, &l));\n\n let mut saved_value = 1;\n\n for (i, &piece) in pieces.iter().enumerate() {\n if piece as usize >= (i + 1) {\n saved_value = i + 1;\n } else {\n break;\n }\n }\n\n println!(\"{}\", saved_value);\n }\n}", "difficulty": "hard"} {"problem_id": "0977", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

In some other world, today is December D-th.

\n

Write a program that prints Christmas if D = 25, Christmas Eve if D = 24, Christmas Eve Eve if D = 23 and Christmas Eve Eve Eve if D = 22.

\n
\n
\n
\n
\n

Constraints

    \n
  • 22 \\leq D \\leq 25
  • \n
  • D is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
D\n
\n
\n
\n
\n
\n

Output

Print the specified string (case-sensitive).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

25\n
\n
\n
\n
\n
\n

Sample Output 1

Christmas\n
\n
\n
\n
\n
\n
\n

Sample Input 2

22\n
\n
\n
\n
\n
\n

Sample Output 2

Christmas Eve Eve Eve\n
\n

Be sure to print spaces between the words.

\n
\n
", "c_code": "int solution() {\n int D = 0;\n scanf(\"%d\", &D);\n printf(\"Christmas \");\n while (D != 25) {\n printf(\"Eve \");\n D++;\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut day = String::new();\n io::stdin()\n .read_line(&mut day)\n .expect(\"Failed to read line\");\n let num = (*day).trim().parse::().unwrap();\n match num {\n 25 => println!(\"Christmas\"),\n 24 => println!(\"Christmas Eve\"),\n 23 => println!(\"Christmas Eve Eve\"),\n 22 => println!(\"Christmas Eve Eve Eve\"),\n _ => println!(\"Invalid Input\"),\n }\n}", "difficulty": "easy"} {"problem_id": "0978", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Your friend gave you a dequeue D as a birthday present.

\n

D is a horizontal cylinder that contains a row of N jewels.

\n

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

\n

In the beginning, you have no jewel in your hands.

\n

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

\n
    \n
  • \n

    Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

    \n
  • \n
  • \n

    Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

    \n
  • \n
  • \n

    Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

    \n
  • \n
  • \n

    Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

    \n
  • \n
\n

Find the maximum possible sum of the values of jewels in your hands after the operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 50
  • \n
  • 1 \\leq K \\leq 100
  • \n
  • -10^7 \\leq V_i \\leq 10^7
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nV_1 V_2 ... V_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 4\n-10 8 2 1 2 6\n
\n
\n
\n
\n
\n

Sample Output 1

14\n
\n

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

\n
    \n
  • Do operation A. You take out the jewel of value -10 from the left end of D.
  • \n
  • Do operation B. You take out the jewel of value 6 from the right end of D.
  • \n
  • Do operation A. You take out the jewel of value 8 from the left end of D.
  • \n
  • Do operation D. You insert the jewel of value -10 to the right end of D.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

6 4\n-6 -100 50 -2 -5 -3\n
\n
\n
\n
\n
\n

Sample Output 2

44\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6 3\n-6 -100 50 -2 -5 -3\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

It is optimal to do no operation.

\n
\n
", "c_code": "int solution() {\n int K;\n int N;\n int V[51];\n int maxim;\n int sum1;\n int sum2;\n int ind;\n int list[51][51];\n int index[50];\n int index2[50] = {0};\n scanf(\"%d %d\", &N, &K);\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &V[i]);\n }\n sum1 = 0;\n for (int i = 0; i <= N; i++) {\n sum2 = 0;\n for (int j = 0; j <= N - i; j++) {\n list[i][j] = sum1 + sum2;\n if (j != N) {\n sum2 += V[N - j - 1];\n }\n }\n sum1 += V[i];\n }\n for (int i = 0; i < N; i++) {\n maxim = 10000001;\n ind = -1;\n for (int j = 0; j < N; j++) {\n if (index2[j] == 0 && maxim > V[j]) {\n ind = j;\n maxim = V[j];\n }\n }\n index[i] = ind;\n index2[ind] = 1;\n }\n maxim = 0;\n for (int i = 0; i <= N; i++) {\n for (int j = 0; j <= N - i; j++) {\n sum1 = 0;\n sum2 = list[i][j];\n for (int k = 0; k < N; k++) {\n if (sum1 >= K - i - j) {\n break;\n }\n if ((index[k] < i || index[k] >= N - j) && V[index[k]] < 0) {\n sum2 -= V[index[k]];\n sum1++;\n }\n }\n if (maxim < sum2 && i + j <= K) {\n maxim = sum2;\n }\n }\n }\n printf(\"%d\\n\", maxim);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n k: usize,\n d: [i64; n],\n }\n let n_act = min(n, k);\n let mut ans = 0;\n for a in 0..n_act + 1 {\n for b in 0..n_act + 1 - a {\n let rm = k - a - b;\n let mut acc_vec: Vec = d\n .iter()\n .take(a)\n .chain(d.iter().skip(n - b))\n .cloned()\n .collect();\n acc_vec.sort();\n let acc = acc_vec\n .iter()\n .enumerate()\n .filter(|&(i, x)| rm <= i || (rm > i && *x > 0))\n .map(|(_, x)| *x)\n .sum();\n ans = max(ans, acc);\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "0979", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You will be given a string S of length 3 representing the weather forecast for three days in the past.

\n

The i-th character (1 \\leq i \\leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

\n

You will also be given a string T of length 3 representing the actual weather on those three days.

\n

The i-th character (1 \\leq i \\leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

\n

Print the number of days for which the forecast was correct.

\n
\n
\n
\n
\n

Constraints

    \n
  • S and T are strings of length 3 each.
  • \n
  • S and T consist of S, C, and R.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nT\n
\n
\n
\n
\n
\n

Output

Print the number of days for which the forecast was correct.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

CSS\nCSR\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n
    \n
  • For the first day, it was forecast to be cloudy, and it was indeed cloudy.
  • \n
  • For the second day, it was forecast to be sunny, and it was indeed sunny.
  • \n
  • For the third day, it was forecast to be sunny, but it was rainy.
  • \n
\n

Thus, the forecast was correct for two days in this case.

\n
\n
\n
\n
\n
\n

Sample Input 2

SSR\nSSR\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

RRR\nSSS\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n char s[3];\n char t[3];\n scanf(\"%c%c%c\\n%c%c%c\", &s[0], &s[1], &s[2], &t[0], &t[1], &t[2]);\n int ans = 0;\n for (int i = 0; i < 3; i++) {\n if (s[i] == t[i]) {\n ans++;\n }\n }\n printf(\"%d\", ans);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let s = s.trim();\n\n let mut t = String::new();\n stdin().read_line(&mut t).unwrap();\n let t = t.trim();\n\n println!(\n \"{}\",\n s.chars().zip(t.chars()).filter(|&(a, b)| a == b).count()\n );\n}", "difficulty": "medium"} {"problem_id": "0980", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

In a public bath, there is a shower which emits water for T seconds when the switch is pushed.

\n

If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for T seconds.\nNote that it does not mean that the shower emits water for T additional seconds.

\n

N people will push the switch while passing by the shower.\nThe i-th person will push the switch t_i seconds after the first person pushes it.

\n

How long will the shower emit water in total?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 200,000
  • \n
  • 1 ≤ T ≤ 10^9
  • \n
  • 0 = t_1 < t_2 < t_3 < , ..., < t_{N-1} < t_N ≤ 10^9
  • \n
  • T and each t_i are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N T\nt_1 t_2 ... t_N\n
\n
\n
\n
\n
\n

Output

Assume that the shower will emit water for a total of X seconds. Print X.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 4\n0 3\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 4\n0 5\n
\n
\n
\n
\n
\n

Sample Output 2

8\n
\n

One second after the shower stops emission of water triggered by the first person, the switch is pushed again.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 1000000000\n0 1000 1000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

2000000000\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1 1\n0\n
\n
\n
\n
\n
\n

Sample Output 4

1\n
\n
\n
\n
\n
\n
\n

Sample Input 5

9 10\n0 3 5 7 100 110 200 300 311\n
\n
\n
\n
\n
\n

Sample Output 5

67\n
\n
\n
", "c_code": "int solution() {\n int n;\n int t;\n scanf(\"%d%d\", &n, &t);\n int a[n];\n scanf(\"%d\", &a[0]);\n long long ans = 0;\n for (int i = 1; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] - a[i - 1] >= t) {\n ans += t;\n } else {\n ans += (a[i] - a[i - 1]);\n }\n }\n ans += t;\n printf(\"%lld\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let s = s.trim();\n\n let nt: Vec<&str> = s.split(\" \").collect();\n let t: u32 = nt[1].parse().unwrap();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let s = s.trim();\n\n let vec: Vec = s.split(\" \").map(|n| n.parse().unwrap()).collect();\n let mut result = 0;\n for n in 0..vec.len() {\n if n != 0 {\n let d = vec[n] - vec[n - 1];\n result += if d < t { d } else { t }\n } else {\n result += t;\n }\n }\n println!(\"{}\", result);\n}", "difficulty": "medium"} {"problem_id": "0981", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

A maze is composed of a grid of H \\times W squares - H vertical, W horizontal.

\n

The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..

\n

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

\n
    \n
  • Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.
  • \n
  • Move B: Use magic to warp himself to a road square in the 5\\times 5 area centered at the square he is currently in.
  • \n
\n

In either case, he cannot go out of the maze.

\n

At least how many times does he need to use the magic to reach (D_h, D_w)?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H,W \\leq 10^3
  • \n
  • 1 \\leq C_h,D_h \\leq H
  • \n
  • 1 \\leq C_w,D_w \\leq W
  • \n
  • S_{ij} is # or ..
  • \n
  • S_{C_h C_w} and S_{D_h D_w} are ..
  • \n
  • (C_h,C_w) \\neq (D_h,D_w)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\nC_h C_w\nD_h D_w\nS_{11}\\ldots S_{1W}\n\\vdots\nS_{H1}\\ldots S_{HW}\n
\n
\n
\n
\n
\n

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 4\n1 1\n4 4\n..#.\n..#.\n.#..\n.#..\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

\n

Note that he cannot walk diagonally.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 4\n1 4\n4 1\n.##.\n####\n####\n.##.\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

He cannot move from there.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 4\n2 2\n3 3\n....\n....\n....\n....\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

No use of magic is needed.

\n
\n
\n
\n
\n
\n

Sample Input 4

4 5\n1 2\n2 5\n#.###\n####.\n#..##\n#..##\n
\n
\n
\n
\n
\n

Sample Output 4

2\n
\n
\n
", "c_code": "int solution() {\n int i;\n int H;\n int W;\n int C[2];\n int D[2];\n char S[1010][1010] = {};\n scanf(\"%d %d\", &H, &W);\n scanf(\"%d %d\", &(C[0]), &(C[1]));\n scanf(\"%d %d\", &(D[0]), &(D[1]));\n for (i = 5; i < H + 5; i++) {\n scanf(\"%s\", &(S[i][5]));\n }\n\n short q[5000001][2];\n int j;\n int k;\n int l;\n int dist[1010][1010];\n int head;\n int tail;\n for (i = 5; i < H + 5; i++) {\n for (j = 5; j < W + 5; j++) {\n dist[i][j] = 1000000;\n }\n }\n dist[C[0] + 4][C[1] + 4] = 0;\n q[2500000][0] = C[0] + 4;\n q[2500000][1] = C[1] + 4;\n for (head = 2500000, tail = 2500001; head < tail; head++) {\n i = q[head][0];\n j = q[head][1];\n\n if (S[i - 1][j] == '.' && dist[i - 1][j] > dist[i][j]) {\n dist[i - 1][j] = dist[i][j];\n q[head][0] = i - 1;\n q[head--][1] = j;\n }\n if (S[i + 1][j] == '.' && dist[i + 1][j] > dist[i][j]) {\n dist[i + 1][j] = dist[i][j];\n q[head][0] = i + 1;\n q[head--][1] = j;\n }\n if (S[i][j - 1] == '.' && dist[i][j - 1] > dist[i][j]) {\n dist[i][j - 1] = dist[i][j];\n q[head][0] = i;\n q[head--][1] = j - 1;\n }\n if (S[i][j + 1] == '.' && dist[i][j + 1] > dist[i][j]) {\n dist[i][j + 1] = dist[i][j];\n q[head][0] = i;\n q[head--][1] = j + 1;\n }\n\n for (k = i - 2; k <= i + 2; k++) {\n for (l = j - 2; l <= j + 2; l++) {\n if (S[k][l] == '.' && dist[k][l] > dist[i][j] + 1) {\n dist[k][l] = dist[i][j] + 1;\n q[tail][0] = k;\n q[tail++][1] = l;\n }\n }\n }\n }\n\n if (dist[D[0] + 4][D[1] + 4] < 1000000) {\n printf(\"%d\\n\", dist[D[0] + 4][D[1] + 4]);\n } else {\n printf(\"-1\\n\");\n }\n fflush(stdout);\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 split = buf.split_whitespace();\n let (h, w): (usize, usize) = (\n split.next().unwrap().parse().unwrap(),\n split.next().unwrap().parse().unwrap(),\n );\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n let mut split = buf.split_whitespace();\n let (ch, cw): (usize, usize) = (\n split.next().unwrap().parse().unwrap(),\n split.next().unwrap().parse().unwrap(),\n );\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n let mut split = buf.split_whitespace();\n let (dh, dw): (usize, usize) = (\n split.next().unwrap().parse().unwrap(),\n split.next().unwrap().parse().unwrap(),\n );\n let s: Vec<_> = (0..h)\n .map(|_| {\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().as_bytes().to_owned()\n })\n .collect();\n let mut queue = VecDeque::new();\n let mut next_queue = VecDeque::new();\n let mut seen = HashSet::new();\n queue.push_back((ch - 1, cw - 1));\n let mut cost = 0i64;\n 'outer: loop {\n while let Some((x, y)) = queue.pop_front() {\n if (x, y) == (dh - 1, dw - 1) {\n break 'outer;\n }\n if s[x][y] == b'.' && seen.insert((x, y)) {\n if x != 0 {\n queue.push_back((x - 1, y));\n }\n if y != 0 {\n queue.push_back((x, y - 1));\n }\n if x != h - 1 {\n queue.push_back((x + 1, y));\n }\n if y != w - 1 {\n queue.push_back((x, y + 1));\n }\n for x in x.saturating_sub(2)..cmp::min(h, x + 3) {\n for y in y.saturating_sub(2)..cmp::min(w, y + 3) {\n next_queue.push_back((x, y));\n }\n }\n }\n }\n cost += 1;\n if next_queue.is_empty() {\n cost = -1;\n break;\n }\n mem::swap(&mut queue, &mut next_queue);\n }\n println!(\"{}\", cost);\n}", "difficulty": "medium"} {"problem_id": "0982", "problem_description": "Theofanis has a riddle for you and if you manage to solve it, he will give you a Cypriot snack halloumi for free (Cypriot cheese).You are given an integer $$$n$$$. You need to find two integers $$$l$$$ and $$$r$$$ such that $$$-10^{18} \\le l < r \\le 10^{18}$$$ and $$$l + (l + 1) + \\ldots + (r - 1) + r = n$$$.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n long long int n = 0;\n scanf(\"%lld\", &n);\n printf(\"%lld %lld\\n\", (1 - n), (n));\n }\n}", "rust_code": "fn solution() {\n let mut tt = String::new();\n io::stdin().read_line(&mut tt).unwrap();\n let tt: i32 = tt.trim().parse().unwrap();\n for _ in 0..tt {\n let mut n = String::new();\n io::stdin().read_line(&mut n).unwrap();\n let n: i64 = n.trim().parse().unwrap();\n if n > 0 {\n println!(\"{} {}\", -n + 1, n);\n } else if n < 0 {\n println!(\"{} {}\", n, -n - 1);\n } else {\n println!(\"-1 1\");\n }\n }\n}", "difficulty": "hard"} {"problem_id": "0983", "problem_description": "Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is $$$n$$$. There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains $$$2$$$ apartments, every other floor contains $$$x$$$ apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers $$$1$$$ and $$$2$$$, apartments on the second floor have numbers from $$$3$$$ to $$$(x + 2)$$$, apartments on the third floor have numbers from $$$(x + 3)$$$ to $$$(2 \\cdot x + 2)$$$, and so on.Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least $$$n$$$ apartments.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int testCases = 0;\n scanf(\"%d\", &testCases);\n while (testCases) {\n --testCases;\n int n;\n int x = 0;\n scanf(\"%d %d\", &n, &x);\n\n if (n >= 1 && n <= 1000 && x >= 1 && x <= 1000) {\n int floorNum = 0;\n if (n <= 2) {\n floorNum = 1;\n } else if ((n - 2) % x == 0) {\n floorNum = ((n - 2) / x) + 1;\n } else {\n floorNum = ((n - 2) / x) + 1 + 1;\n }\n printf(\"%d\\n\", floorNum);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let t: i64 = line.trim().parse().unwrap();\n\n for _ in 0..t {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let n = words[0];\n let x = words[1];\n\n if n <= 2 {\n println!(\"{}\", 1);\n } else {\n println!(\"{}\", ((n - 2 + x - 1) / x) + 1);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0984", "problem_description": "For a given array $$$a$$$ consisting of $$$n$$$ integers and a given integer $$$m$$$ find if it is possible to reorder elements of the array $$$a$$$ in such a way that $$$\\sum_{i=1}^{n}{\\sum_{j=i}^{n}{\\frac{a_j}{j}}}$$$ equals $$$m$$$? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, $$$\\frac{5}{2}=2.5$$$.", "c_code": "int solution(void) {\n int testcases = 0;\n\n scanf(\"%d\", &testcases);\n\n int n = 0;\n int m = 0;\n int i = 0;\n int arr[120];\n int sum = 0;\n int k = 0;\n\n for (; testcases > 0; testcases--) {\n\n scanf(\"%d %d \", &n, &m);\n\n sum = 0;\n\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n sum = sum + arr[i];\n }\n\n if (sum == m) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\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 for _i in 0..t {\n let mut inp = String::new();\n stdin().read_line(&mut inp).unwrap();\n let m: u32 = inp.split_whitespace().nth(1).unwrap().parse().unwrap();\n\n inp = String::new();\n stdin().read_line(&mut inp).unwrap();\n let mut sum: u32 = 0;\n inp.split_whitespace()\n .for_each(|x| sum += x.parse::().unwrap());\n if sum == m {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0985", "problem_description": "Innokentiy decides to change the password in the social net \"Contact!\", but he is too lazy to invent a new password by himself. That is why he needs your help. Innokentiy decides that new password should satisfy the following conditions: the length of the password must be equal to n, the password should consist only of lowercase Latin letters, the number of distinct symbols in the password must be equal to k, any two consecutive symbols in the password must be distinct. Your task is to help Innokentiy and to invent a new password which will satisfy all given conditions.", "c_code": "int solution() {\n char alphabet[26];\n int a = 97;\n for (size_t i = 0; i < 26; i++, a++) {\n alphabet[i] = a;\n }\n size_t n = 0;\n size_t k = 0;\n scanf(\"%lu%lu\", &n, &k);\n\n char *password = (char *)calloc(n + 1, sizeof(char));\n size_t q = 0;\n for (size_t i = 0; i < n; i++, q++) {\n if (q >= k) {\n q = 0;\n }\n password[i] = alphabet[q];\n }\n password[n] = '\\0';\n fputs(password, stdout);\n free(password);\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut ns = buf\n .split_whitespace()\n .map(|str| str.trim().parse::().unwrap());\n let n = ns.next().unwrap();\n let k = ns.next().unwrap();\n let chars: Vec = (0..k).map(|b| (b as u8 + b'a').into()).collect();\n let chars = chars.into_iter().cycle();\n let pw: String = chars.take(n as usize).collect();\n println!(\"{}\", pw);\n}", "difficulty": "medium"} {"problem_id": "0986", "problem_description": "At first, there was a legend related to the name of the problem, but now it's just a formal statement.You are given $$$n$$$ points $$$a_1, a_2, \\dots, a_n$$$ on the $$$OX$$$ axis. Now you are asked to find such an integer point $$$x$$$ on $$$OX$$$ axis that $$$f_k(x)$$$ is minimal possible.The function $$$f_k(x)$$$ can be described in the following way: form a list of distances $$$d_1, d_2, \\dots, d_n$$$ where $$$d_i = |a_i - x|$$$ (distance between $$$a_i$$$ and $$$x$$$); sort list $$$d$$$ in non-descending order; take $$$d_{k + 1}$$$ as a result. If there are multiple optimal answers you can print any of them.", "c_code": "int solution(void) {\n long int t;\n scanf(\" %ld\", &t);\n for (long int i = 0; i < t; i++) {\n long int n;\n long int k;\n scanf(\" %ld %ld\", &n, &k);\n long long int a[n + 1];\n for (int j = 1; j <= n; j++) {\n scanf(\" %lld\", &a[j]);\n }\n if (k == 0) {\n printf(\"%lld\\n\", a[1]);\n continue;\n }\n long long int diff = a[n] - a[1];\n long int index = 1;\n for (int j = 1; j <= n - k; j++) {\n if (a[j + k] - a[j] <= diff) {\n diff = a[j + k] - a[j];\n index = j;\n }\n }\n\n long long int ave = 0;\n ave = (a[index + k] + a[index]) / 2;\n printf(\"%lld\\n\", ave);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let t: i64 = line.trim().parse::().expect(\"error\");\n for _ in 0..t {\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let n_k: Vec = line\n .split_whitespace()\n .map(|x| x.parse().expect(\"\"))\n .collect();\n let k = n_k[1];\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let a: Vec = line\n .split_whitespace()\n .map(|x| x.parse().expect(\"\"))\n .collect();\n let mut min_val = 0x3f_3f_3f_3f_3f_3f_3f_3fi64;\n let mut x = a[0];\n for i in 0..cmp::max(a.len() - k, 0) {\n if a[i + k] - a[i] < min_val {\n min_val = a[i + k] - a[i];\n x = a[i] + (a[i + k] - a[i]) / 2;\n }\n }\n println!(\"{:?}\", x);\n }\n}", "difficulty": "easy"} {"problem_id": "0987", "problem_description": "After a long day, Alice and Bob decided to play a little game. The game board consists of $$$n$$$ cells in a straight line, numbered from $$$1$$$ to $$$n$$$, where each cell contains a number $$$a_i$$$ between $$$1$$$ and $$$n$$$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $$$i$$$ to cell $$$j$$$ only if the following two conditions are satisfied: the number in the new cell $$$j$$$ must be strictly larger than the number in the old cell $$$i$$$ (i.e. $$$a_j > a_i$$$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $$$|i-j|\\bmod a_i = 0$$$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int b[n + 1];\n int c[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b[a[i]] = i;\n c[i] = 0;\n }\n for (int i = n; i > 0; i--) {\n\n int t = b[i];\n int flag = 0;\n for (int j = (t + i); j < n;) {\n if (a[j] > i && c[j] == 1) {\n flag = 1;\n break;\n }\n j = j + i;\n }\n if (flag == 1) {\n c[b[i]] = -1;\n continue;\n }\n for (int j = (t - i); j >= 0;) {\n if (a[j] > i && c[j] == 1) {\n flag = 1;\n break;\n }\n j = j - i;\n }\n if (flag == 1) {\n c[b[i]] = -1;\n } else {\n c[b[i]] = 1;\n }\n }\n for (int i = 0; i < n; i++) {\n\n if (c[i] == 1) {\n printf(\"B\");\n } else {\n printf(\"A\");\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 handle.read_to_string(&mut buffer).unwrap();\n\n let mut arr: Vec<_> = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n let n = arr[0];\n let v: Vec<_> = arr.drain(1..).collect();\n let mut n_to_index = vec![0; n + 1];\n for (idx, &v) in v.iter().enumerate() {\n n_to_index[v] = idx;\n }\n\n let mut wl = vec![0; n + 1];\n\n for i in (1..=n).rev() {\n let idx_cur = n_to_index[i];\n\n let offset = idx_cur % i;\n\n let mut j = offset;\n while j < n {\n if j != idx_cur {\n let num = v[j];\n if i < num && wl[num] == 0 {\n wl[i] = 1;\n }\n }\n j += i;\n }\n }\n\n for i in v.iter() {\n if wl[*i] == 1 {\n print!(\"A\");\n } else {\n print!(\"B\");\n }\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "0988", "problem_description": "IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. \"It would look much better when I'll swap some of them!\" — thought the girl — \"but how to do it?\". After a while, she got an idea. IA will look at all prefixes with lengths from $$$1$$$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.", "c_code": "int solution(int argc, char const *argv[]) {\n char str[1001];\n scanf(\"%s\", str);\n int i = 0;\n int length;\n int flag = 0;\n while (str[i] != '\\0') {\n i++;\n }\n length = i;\n int arr[length];\n for (int i = 0; i < length; ++i) {\n arr[i] = 0;\n }\n i = 0;\n while (str[i] != '\\0') {\n if (str[i] == 'a' && flag == 0) {\n flag = 1;\n if (i > 0 && str[i - 1] == 'b') {\n arr[i - 1] = 1;\n }\n i++;\n continue;\n }\n if (str[i] == 'b' && flag == 1) {\n arr[i - 1] = 1;\n flag = 0;\n i++;\n continue;\n }\n i++;\n }\n if (str[i - 1] == 'a') {\n arr[i - 1] = 1;\n }\n printf(\"\\n\");\n for (int i = 0; i < length; ++i) {\n printf(\"%d \", arr[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let sin = io::stdin();\n let mut l = sin.lock().lines().map(|s| s.unwrap().trim().to_string());\n let s = l.next().unwrap();\n\n let mut ans = Vec::::new();\n\n let mut ends: char = 'b';\n for c in s.chars().rev() {\n if c != ends {\n ans.push(1);\n ends = match ends {\n 'a' => 'b',\n 'b' => 'a',\n _ => panic!(\"impossibru\"),\n };\n } else {\n ans.push(0)\n }\n }\n\n println!(\n \"{}\",\n ans.iter()\n .rev()\n .fold(\"\".to_string(), |acc, i| { acc + \" \" + &i.to_string() })\n )\n}", "difficulty": "hard"} {"problem_id": "0989", "problem_description": "When you play the game of thrones, you win, or you die. There is no middle ground.Cersei Lannister, A Game of Thrones by George R. R. MartinThere are $$$n$$$ nobles, numbered from $$$1$$$ to $$$n$$$. Noble $$$i$$$ has a power of $$$i$$$. There are also $$$m$$$ \"friendships\". A friendship between nobles $$$a$$$ and $$$b$$$ is always mutual.A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles $$$u$$$ and $$$v$$$. Remove a friendship between nobles $$$u$$$ and $$$v$$$. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles.Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive!", "c_code": "int solution() {\n int n;\n int m;\n int u;\n int v;\n int q;\n int f[200200] = {0};\n int count = 0;\n scanf(\"%d%d\", &n, &m);\n for (int i = 0; i < m; i++) {\n scanf(\"%d%d\", &u, &v);\n if (u > v) {\n f[v]++;\n if (f[v] == 1) {\n count++;\n }\n } else if (u < v) {\n f[u]++;\n if (f[u] == 1) {\n count++;\n }\n }\n }\n scanf(\"%d\", &q);\n int x;\n for (int i = 0; i < q; i++) {\n scanf(\"%d\", &x);\n if (x == 1) {\n scanf(\"%d%d\", &u, &v);\n if (u > v) {\n f[v]++;\n if (f[v] == 1) {\n count++;\n }\n } else if (u < v) {\n f[u]++;\n if (f[u] == 1) {\n count++;\n }\n }\n } else if (x == 2) {\n scanf(\"%d%d\", &u, &v);\n if (u > v) {\n f[v]--;\n if (f[v] == 0) {\n count--;\n }\n } else if (u < v) {\n f[u]--;\n if (f[u] == 0) {\n count--;\n }\n }\n } else {\n printf(\"%d\\n\", n - count);\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\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 m = it.next().unwrap();\n\n let mut k = n;\n let mut g = vec![0; n];\n\n for _ in 0..m {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let u = it.next().unwrap() - 1;\n let v = it.next().unwrap() - 1;\n\n let (u, _v) = if u < v { (u, v) } else { (v, u) };\n\n if g[u] == 0 {\n k -= 1;\n }\n g[u] += 1;\n }\n\n let q: usize = lines.next().unwrap().parse().unwrap();\n\n for _ in 0..q {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace();\n match it.next().unwrap() {\n \"1\" => {\n let mut it = it.map(|s| s.parse::().unwrap());\n\n let u = it.next().unwrap() - 1;\n let v = it.next().unwrap() - 1;\n\n let (u, _v) = if u < v { (u, v) } else { (v, u) };\n\n if g[u] == 0 {\n k -= 1;\n }\n g[u] += 1;\n }\n \"2\" => {\n let mut it = it.map(|s| s.parse::().unwrap());\n\n let u = it.next().unwrap() - 1;\n let v = it.next().unwrap() - 1;\n\n let (u, _v) = if u < v { (u, v) } else { (v, u) };\n\n g[u] -= 1;\n if g[u] == 0 {\n k += 1;\n }\n }\n _ => {\n writeln!(&mut output, \"{}\", k).unwrap();\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0990", "problem_description": "

Insertion Sort

\n\n

\nWrite a program of the Insertion Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:\n

\n\n
\nfor i = 1 to A.length-1\n    key = A[i]\n    /* insert A[i] into the sorted sequence A[0,...,j-1] */\n    j = i - 1\n    while j >= 0 and A[j] > key\n        A[j+1] = A[j]\n        j--\n    A[j+1] = key\n
\n\n

\nNote that, indices for array elements are based on 0-origin.\n

\n\n

\nTo illustrate the algorithms, your program should trace intermediate result for each step.\n

\n\n

Input

\n\n

\nThe first line of the input includes an integer N, the number of elements in the sequence.\n

\n

\nIn the second line, N elements of the sequence are given separated by a single space.\n

\n\n

Output

\n\n

\nThe output consists of N lines. Please output the intermediate sequence in a line for each step. Elements of the sequence should be separated by single space.\n

\n\n

Constraints

\n\n

\n1 ≤ N ≤ 100\n

\n\n

Sample Input 1

\n
\n6\n5 2 4 6 1 3\n
\n

Sample Output 1

\n
\n5 2 4 6 1 3\n2 5 4 6 1 3\n2 4 5 6 1 3\n2 4 5 6 1 3\n1 2 4 5 6 3\n1 2 3 4 5 6\n
\n\n

Sample Input 2

\n
\n3\n1 2 3\n
\n

Sample Output 2

\n
\n1 2 3\n1 2 3\n1 2 3\n
\n\n\n\n\n

Hint

\n\nTemplate in C", "c_code": "int solution() {\n int N = 0;\n int k = 0;\n int i = 0;\n int v = 0;\n int j = 0;\n int A[1000];\n int l = 0;\n scanf(\"%d\", &N);\n if (0 <= N && N <= 100) {\n for (k = 0; k < N; k++) {\n scanf(\"%d\", &A[k]);\n printf(\"%d\", A[k]);\n if (k < N - 1) {\n printf(\" \");\n }\n }\n printf(\"\\n\");\n for (i = 1; i <= N - 1; i++) {\n v = A[i];\n j = i - 1;\n while (j >= 0 && A[j] > v) {\n A[j + 1] = A[j];\n j--;\n }\n A[j + 1] = v;\n for (l = 0; l < N; l++) {\n printf(\"%d\", A[l]);\n if (l < N - 1) {\n printf(\" \");\n }\n }\n printf(\"\\n\");\n }\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).ok();\n let n: usize = buf.trim().parse().unwrap_or(0);\n\n let mut buf = String::new();\n stdin.read_line(&mut buf).ok();\n let vec: Vec<&str> = buf.split_whitespace().collect();\n let mut a: Vec = vec.iter().map(|&x| x.parse().unwrap_or(0)).collect();\n\n for i in 1..n {\n for (k, e) in a.iter().enumerate() {\n print!(\"{}\", e);\n if k != n - 1 {\n print!(\" \");\n }\n }\n println!();\n let key = a[i];\n let mut j = i - 1;\n while a[j] > key {\n a[j + 1] = a[j];\n if j == 0 {\n break;\n } else {\n j -= 1;\n }\n }\n a[j + 1] = key;\n if a[0] > a[1] {\n a.swap(1, 0);\n }\n }\n for (k, e) in a.iter().enumerate() {\n print!(\"{}\", e);\n if k != n - 1 {\n print!(\" \");\n }\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "0991", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

\n

There are N camels standing in a circle.\nSnuke will put one of his hats on each of these camels.

\n

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

\n
    \n
  • The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.
  • \n
\n
\nWhat is XOR?\n\nThe bitwise XOR x_1 \\oplus x_2 \\oplus \\ldots \\oplus x_n of n non-negative integers x_1, x_2, \\ldots, x_n is defined as follows:\n\n- When x_1 \\oplus x_2 \\oplus \\ldots \\oplus x_n is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if the number of integers among x_1, x_2, \\ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.\n\nFor example, 3 \\oplus 5 = 6.\n\n
\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 3 \\leq N \\leq 10^{5}
  • \n
  • 0 \\leq a_i \\leq 10^{9}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 \\ldots a_{N}\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n
    \n
  • If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4\n1 2 4 8\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
    \n
  • There is no such way to distribute the hats; the answer is No.
  • \n
\n
\n
", "c_code": "int solution() {\n\n int N;\n scanf(\"%d\", &N);\n\n int A[N];\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &A[i]);\n }\n\n int judge = A[0];\n\n for (int i = 1; i < N; i++) {\n judge = judge ^ A[i];\n }\n\n if (judge == 0) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n v: [usize; n]\n }\n let mut map = HashMap::new();\n for &a in &v {\n *map.entry(a).or_insert(0) += 1;\n }\n let l = map.len();\n if l == 1 {\n if map.get(&0).is_some() {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n return;\n }\n if n % 3 != 0 {\n println!(\"No\");\n return;\n }\n let m = n / 3;\n if l > 3 {\n println!(\"No\");\n } else if l == 3 {\n for &x in map.values() {\n if x != m {\n println!(\"No\");\n return;\n }\n }\n let ks: Vec = map.keys().copied().collect();\n if ks[0] ^ ks[1] == ks[2] {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n } else if l == 2 {\n if let Some(&c) = map.get(&0)\n && c == m {\n println!(\"Yes\");\n return;\n }\n println!(\"No\");\n }\n}", "difficulty": "hard"} {"problem_id": "0992", "problem_description": "You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \\dots, p_m$$$, where $$$1 \\le p_i < n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \\le a_2 \\le \\dots \\le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n int m = 0;\n int k = 0;\n int x;\n int y;\n int var = 0;\n\n scanf(\"%d\", &t);\n\n for (int i = 0; i < t; i++) {\n scanf(\"%d %d\", &n, &m);\n int array[n];\n int pos[m];\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &array[j]);\n }\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &pos[j]);\n }\n\n for (int j = 0; j < n; j++) {\n for (int r = 0; r < m; r++) {\n k = pos[r];\n x = array[k - 1];\n y = array[k];\n if (x > y) {\n array[k - 1] = y;\n array[k] = x;\n }\n }\n }\n\n for (int j = 0; j < n; j++) {\n if (array[j] > array[j + 1]) {\n var = 1;\n }\n }\n\n if (var == 0) {\n printf(\"YES\\n\");\n\n } else {\n printf(\"NO\\n\");\n }\n\n var = 0;\n }\n\n return 0;\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\n .split_whitespace()\n .map(|x| x.parse::().expect(\"convert to number\"))\n .collect::>();\n\n let mut it = arr.iter();\n let t = *it.next().unwrap();\n\n for _i in 0..t {\n let n = *it.next().unwrap();\n let m = *it.next().unwrap();\n let mut a = (0..n).map(|_| *it.next().unwrap()).collect::>();\n let p = (0..m)\n .map(|_| *it.next().unwrap() - 1)\n .collect::>();\n\n let mut possible = true;\n 'outer: for i in 1..n {\n let mut j = i;\n 'inner: loop {\n if j == 0 {\n break 'inner;\n }\n if a[j] >= a[j - 1] {\n break 'inner;\n }\n if p.get(&(j - 1)).is_none() {\n possible = false;\n break 'outer;\n }\n a.swap(j - 1, j);\n j -= 1;\n }\n }\n println!(\"{}\", if possible { \"YES\" } else { \"NO\" });\n }\n}", "difficulty": "medium"} {"problem_id": "0993", "problem_description": "Sho has an array $$$a$$$ consisting of $$$n$$$ integers. An operation consists of choosing two distinct indices $$$i$$$ and $$$j$$$ and removing $$$a_i$$$ and $$$a_j$$$ from the array.For example, for the array $$$[2, 3, 4, 2, 5]$$$, Sho can choose to remove indices $$$1$$$ and $$$3$$$. After this operation, the array becomes $$$[3, 2, 5]$$$. Note that after any operation, the length of the array is reduced by two.After he made some operations, Sho has an array that has only distinct elements. In addition, he made operations such that the resulting array is the longest possible. More formally, the array after Sho has made his operations respects these criteria: No pairs such that ($$$i < j$$$) and $$$a_i = a_j$$$ exist. The length of $$$a$$$ is maximized. Output the length of the final array.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t > 0) {\n int n;\n t--;\n scanf(\"%d\", &n);\n int a[n];\n int b[10001];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < 10001; i++) {\n b[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n b[a[i]] = 1;\n }\n int x = 0;\n for (int i = 0; i < 10001; i++) {\n x += b[i];\n }\n if ((n - x) % 2 != 0) {\n if (x > 0) {\n x = x - 1;\n }\n }\n printf(\"%d\\n\", x);\n }\n}", "rust_code": "fn solution() {\n let mut inputt = String::new();\n io::stdin().read_line(&mut inputt).expect(\"input faild\");\n let t: i32 = inputt.trim().parse().expect(\"not a number\");\n\n for _s in 0..t {\n let mut inputn = String::new();\n io::stdin().read_line(&mut inputn).expect(\"input faild\");\n let n: i32 = inputn.trim().parse().expect(\"not a number\");\n let mut inputa = String::new();\n io::stdin().read_line(&mut inputa).expect(\"input faild\");\n let mut a: Vec = inputa\n .split_whitespace()\n .map(|x| x.parse().expect(\"not a number\"))\n .collect();\n a.sort();\n let mut cnt = 1;\n\n for _i in 1..n as usize {\n if a[_i] > a[_i - 1] {\n cnt += 1;\n }\n }\n\n if ((n - cnt) % 2) == 0 {\n println!(\"{}\", cnt);\n } else {\n println!(\"{}\", cnt - 1);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "0994", "problem_description": "You are given an array $$$a_1, a_2, \\dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_i \\cdot a_j = i + j$$$.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n int ans = 0;\n int n;\n int a[100001] = {0};\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 1; i <= n; ++i) {\n for (int k = 1; (a[i] * k < 2 * n) && (a[i] * k <= 100000 + i); ++k) {\n int j = (a[i] * k) - i;\n if (j <= 0) {\n continue;\n }\n if ((i < j) && (a[j] == k)) {\n ++ans;\n }\n }\n }\n printf(\"%d\\n\", ans);\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\n let t = it.next().unwrap();\n\n for _ in 0..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 mut a = Vec::with_capacity(n);\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let m = 200_002;\n a.extend(it.enumerate());\n\n a.sort_by_key(|&(_, x)| x);\n\n let mut ans = 0u64;\n\n for i in 0..n {\n for j in i + 1..n {\n let (i, ai) = a[i];\n let (j, aj) = a[j];\n match ai.checked_mul(aj) {\n Some(x) if x <= m => {\n if x == i + j + 2 {\n ans += 1;\n }\n }\n _ => break,\n }\n }\n }\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "0995", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows:

\n
    \n
  • Rating 1-399 : gray
  • \n
  • Rating 400-799 : brown
  • \n
  • Rating 800-1199 : green
  • \n
  • Rating 1200-1599 : cyan
  • \n
  • Rating 1600-1999 : blue
  • \n
  • Rating 2000-2399 : yellow
  • \n
  • Rating 2400-2799 : orange
  • \n
  • Rating 2800-3199 : red
  • \n
\n

Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not.
\nCurrently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i.
\nFind the minimum and maximum possible numbers of different colors of the users.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 100
  • \n
  • 1 ≤ a_i ≤ 4800
  • \n
  • a_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n2100 2500 2700 2700\n
\n
\n
\n
\n
\n

Sample Output 1

2 2\n
\n

The user with rating 2100 is \"yellow\", and the others are \"orange\". There are two different colors.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n1100 1900 2800 3200 3200\n
\n
\n
\n
\n
\n

Sample Output 2

3 5\n
\n

The user with rating 1100 is \"green\", the user with rating 1900 is blue and the user with rating 2800 is \"red\".
\nIf the fourth user picks \"red\", and the fifth user picks \"blue\", there are three different colors. This is one possible scenario for the minimum number of colors.
\nIf the fourth user picks \"purple\", and the fifth user picks \"black\", there are five different colors. This is one possible scenario for the maximum number of colors.

\n
\n
\n
\n
\n
\n

Sample Input 3

20\n800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990\n
\n
\n
\n
\n
\n

Sample Output 3

1 1\n
\n

All the users are \"green\", and thus there is one color.

\n
\n
", "c_code": "int solution() {\n\n int N = 0;\n int RATE[110];\n for (int i = 0; i < 110; i++) {\n RATE[i] = 0;\n }\n\n int COLOR[9];\n for (int i = 0; i < 9; i++) {\n COLOR[i] = 0;\n }\n\n int MIN = 0;\n int MAX = 0;\n\n scanf(\"%d\", &N);\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &RATE[i]);\n }\n\n for (int i = 0; i < N; i++) {\n if ((RATE[i] >= 1) && (RATE[i] <= 399)) {\n COLOR[0] = 1;\n }\n\n if ((RATE[i] >= 400) && (RATE[i] <= 799)) {\n COLOR[1] = 1;\n }\n\n if ((RATE[i] >= 800) && (RATE[i] <= 1199)) {\n COLOR[2] = 1;\n }\n\n if ((RATE[i] >= 1200) && (RATE[i] <= 1599)) {\n COLOR[3] = 1;\n }\n\n if ((RATE[i] >= 1600) && (RATE[i] <= 1999)) {\n COLOR[4] = 1;\n }\n\n if ((RATE[i] >= 2000) && (RATE[i] <= 2399)) {\n COLOR[5] = 1;\n }\n\n if ((RATE[i] >= 2400) && (RATE[i] <= 2799)) {\n COLOR[6] = 1;\n }\n\n if ((RATE[i] >= 2800) && (RATE[i] <= 3199)) {\n COLOR[7] = 1;\n }\n\n if (RATE[i] >= 3200) {\n COLOR[8]++;\n }\n }\n\n for (int i = 0; i < 9; i++) {\n\n if (i <= 7) {\n MIN = MIN + COLOR[i];\n } else {\n MAX = MIN + COLOR[8];\n }\n }\n\n if (MIN == 0) {\n MIN = 1;\n }\n\n printf(\"%d %d\", MIN, MAX);\n\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 mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let a: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let mut h: HashSet = HashSet::new();\n let mut x: usize = 0;\n for i in &a {\n if *i < 3200 {\n h.insert(*i / 400);\n } else {\n x += 1;\n }\n }\n println!(\"{} {}\", cmp::max(1, h.len()), h.len() + x);\n}", "difficulty": "hard"} {"problem_id": "0996", "problem_description": "You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.You have to determine the length of the longest balanced substring of s.", "c_code": "int solution() {\n int n;\n int i;\n scanf(\"%d\", &n);\n\n char s[n];\n scanf(\"%s\", s);\n\n int min_b[(2 * n) + 1];\n int max_b[(2 * n) + 1];\n\n for (i = 0; i < (2 * n) + 1; i++) {\n min_b[i] = -1;\n max_b[i] = -1;\n }\n\n int cc = 0;\n\n for (i = 0; i < n; i++) {\n if (s[i] == '0') {\n cc++;\n } else {\n cc--;\n }\n\n if (cc != 0 && min_b[n + cc] == -1) {\n min_b[n + cc] = i;\n }\n\n max_b[n + cc] = i;\n }\n\n int ans = 0;\n\n for (i = 0; i < (2 * n) + 1; i++) {\n if ((max_b[i] - min_b[i]) > ans) {\n ans = (max_b[i] - min_b[i]);\n }\n }\n\n printf(\"%d\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n\n let mut balance_list = HashMap::new();\n balance_list.insert(0, [0, 0]);\n let line: Vec = line.trim().chars().collect();\n\n let mut balance: i32 = 0;\n\n for i in 0..line.len() {\n match line[i] {\n '1' => balance += 1,\n _ => balance -= 1,\n }\n\n if let std::collections::hash_map::Entry::Vacant(e) = balance_list.entry(balance) {\n e.insert([i + 1, i + 1]);\n } else {\n let occurance = balance_list.get_mut(&balance).unwrap();\n occurance[1] = i + 1;\n }\n }\n\n let mut best_length = 0;\n for i in (-(line.len() as i32))..((line.len() + 1) as i32) {\n if balance_list.contains_key(&i) {\n let occurance = balance_list.get_mut(&i).unwrap();\n if occurance[1] - occurance[0] > best_length {\n best_length = occurance[1] - occurance[0];\n }\n }\n }\n\n print!(\"{}\", best_length);\n}", "difficulty": "medium"} {"problem_id": "0997", "problem_description": "You are given strings $$$S$$$ and $$$T$$$, consisting of lowercase English letters. It is guaranteed that $$$T$$$ is a permutation of the string abc. Find string $$$S'$$$, the lexicographically smallest permutation of $$$S$$$ such that $$$T$$$ is not a subsequence of $$$S'$$$.String $$$a$$$ is a permutation of string $$$b$$$ if the number of occurrences of each distinct character is the same in both strings.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char S[110];\n char T[5];\n scanf(\"%s\", S);\n scanf(\"%s\", T);\n int len = strlen(S);\n int pan = 0;\n for (int i = 0; i < len; i++) {\n if (S[i] == 'a') {\n pan = 1;\n break;\n }\n }\n int l = 0;\n if (pan == 1 && T[0] == 'a' && T[1] == 'b' && T[2] == 'c') {\n int m = 97;\n int ci = 0;\n for (int j = 0; j < 26; j++) {\n for (int i = 0; i < len; i++) {\n if (S[i] == m) {\n printf(\"%c\", S[i]);\n ci++;\n }\n }\n if (m == 97 && l == 0) {\n m = 99;\n } else if (m == 99 && l == 1) {\n m = 98;\n } else if (m == 98 && l == 2) {\n m = 100;\n } else {\n m++;\n }\n l++;\n if (ci == len) {\n break;\n }\n }\n printf(\"\\n\");\n } else {\n int m = 97;\n int ci = 0;\n ;\n for (int j = 0; j < 26; j++) {\n for (int i = 0; i < len; i++) {\n if (S[i] == m) {\n printf(\"%c\", S[i]);\n ci++;\n }\n }\n m++;\n if (ci == len) {\n break;\n }\n }\n printf(\"\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n let mut input = input.split_ascii_whitespace();\n let mut output = vec![];\n let t = input.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let s = input.next().unwrap();\n let t = input.next().unwrap();\n let mut s = s.as_bytes().to_vec();\n s.sort_unstable();\n let b = s.partition_point(|&c| c < b'b');\n if t == \"abc\" && b != 0 {\n let c = s.partition_point(|&c| c < b'c');\n let c_end = s.partition_point(|&c| c <= b'c');\n let mut s_prime = vec![0; s.len()];\n s_prime[..b].copy_from_slice(&s[..b]);\n s_prime[b..b + c_end - c].copy_from_slice(&s[c..c_end]);\n s_prime[b + c_end - c..c_end].copy_from_slice(&s[b..c]);\n s_prime[c_end..].copy_from_slice(&s[c_end..]);\n output.extend_from_slice(&s_prime);\n } else {\n output.extend_from_slice(&s);\n }\n output.push(b'\\n');\n }\n stdout().write_all(&output).unwrap();\n}", "difficulty": "medium"} {"problem_id": "0998", "problem_description": "Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l2, i, r2, i).Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.The distance between periods (l1, r1) and (l2, r2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l1 ≤ i ≤ r1 and l2 ≤ j ≤ r2. In particular, when the periods intersect, the distance between them is 0.Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d\", &n);\n int a[n][2];\n int a0Max = INT_MIN;\n int a1Min = INT_MAX;\n int b0Max = INT_MIN;\n int b1Min = INT_MAX;\n for (int i = 0; i < n; i++) {\n scanf(\"%d %d\", &a[i][0], &a[i][1]);\n if (a[i][0] > a0Max) {\n a0Max = a[i][0];\n }\n if (a[i][1] < a1Min) {\n a1Min = a[i][1];\n }\n }\n scanf(\"%d\", &m);\n int b[m][2];\n for (int i = 0; i < m; i++) {\n scanf(\"%d %d\", &b[i][0], &b[i][1]);\n if (b[i][0] > b0Max) {\n b0Max = b[i][0];\n }\n if (b[i][1] < b1Min) {\n b1Min = b[i][1];\n }\n }\n int c1 = b0Max - a1Min;\n int c2 = a0Max - b1Min;\n if (c1 < 0 && c2 < 0) {\n printf(\"0\");\n } else if (c1 > c2) {\n printf(\"%d\", c1);\n } else {\n printf(\"%d\", c2);\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"Error!\");\n let x: u32 = s.trim().parse().expect(\"Error!\");\n let mut chess_arr_1: Vec = Vec::new();\n let mut chess_arr_2: Vec = Vec::new();\n for _ in 0..x {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"Error!\");\n let mut s_it = s.split_whitespace();\n chess_arr_1.push(s_it.next().expect(\"Error\").parse().expect(\"Error\"));\n chess_arr_2.push(s_it.next().expect(\"Error\").parse().expect(\"Error\"));\n }\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"Error!\");\n let x: u32 = s.trim().parse().expect(\"Error!\");\n let mut prog_arr_1: Vec = Vec::new();\n let mut prog_arr_2: Vec = Vec::new();\n for _ in 0..x {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"Error!\");\n let mut s_it = s.split_whitespace();\n prog_arr_1.push(s_it.next().expect(\"Error\").parse().expect(\"Error\"));\n prog_arr_2.push(s_it.next().expect(\"Error\").parse().expect(\"Error\"));\n }\n\n let mut x: i32 =\n prog_arr_1.iter().max().expect(\"Error\") - chess_arr_2.iter().min().expect(\"Error\");\n if x < 0 {\n x = 0;\n }\n let mut y = chess_arr_1.iter().max().expect(\"Error\") - prog_arr_2.iter().min().expect(\"Error\");\n if y < 0 {\n y = 0;\n }\n print!(\"{}\", cmp::max(x, y));\n}", "difficulty": "hard"} {"problem_id": "0999", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have a grid with H rows and W columns of squares.\nSnuke is painting these squares in colors 1, 2, ..., N.\nHere, the following conditions should be satisfied:

\n
    \n
  • For each i (1 ≤ i ≤ N), there are exactly a_i squares painted in Color i. Here, a_1 + a_2 + ... + a_N = H W.
  • \n
  • For each i (1 ≤ i ≤ N), the squares painted in Color i are 4-connected. That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i.
  • \n
\n

Find a way to paint the squares so that the conditions are satisfied.\nIt can be shown that a solution always exists.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ H, W ≤ 100
  • \n
  • 1 ≤ N ≤ H W
  • \n
  • a_i ≥ 1
  • \n
  • a_1 + a_2 + ... + a_N = H W
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\nN\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print one way to paint the squares that satisfies the conditions.\nOutput in the following format:

\n
c_{1 1} ... c_{1 W}\n:\nc_{H 1} ... c_{H W}\n
\n

Here, c_{i j} is the color of the square at the i-th row from the top and j-th column from the left.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n3\n2 1 1\n
\n
\n
\n
\n
\n

Sample Output 1

1 1\n2 3\n
\n

Below is an example of an invalid solution:

\n
1 2\n3 1\n
\n

This is because the squares painted in Color 1 are not 4-connected.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 5\n5\n1 2 3 4 5\n
\n
\n
\n
\n
\n

Sample Output 2

1 4 4 4 3\n2 5 4 5 3\n2 5 5 5 3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 1\n1\n1\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution() {\n int y = 1;\n int i = 1;\n int j = 1;\n int k = 0;\n int m;\n int n;\n int x;\n int z;\n scanf(\"%d%d\", &m, &n);\n int a[210][210] = {0};\n scanf(\"%d\", &z);\n while (z--) {\n scanf(\"%d\", &x);\n while (x--) {\n if (k < n && a[j][k + 1] == 0) {\n a[j][++k] = y;\n } else if (j < m && a[j + 1][k] == 0) {\n a[++j][k] = y;\n } else if (k > 1 && a[j][k - 1] == 0) {\n a[j][--k] = y;\n } else {\n a[--j][k] = y;\n }\n }\n y++;\n }\n j = 1;\n k = 1;\n for (i = 1; i <= m * n; i++) {\n if (i % n == 0) {\n printf(\"%d \", a[j][k]);\n j++;\n k = 1;\n printf(\"\\n\");\n } else {\n printf(\"%d \", a[j][k++]);\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).ok();\n let mut it = buf.split_whitespace();\n let h = it.next().unwrap().parse::().unwrap();\n let w = it.next().unwrap().parse::().unwrap();\n let n = it.next().unwrap().parse::().unwrap();\n let mut a = (0..n)\n .map(|_| it.next().unwrap().parse::().unwrap())\n .collect::>();\n let mut res = vec![vec![0; w]; h];\n let mut color = 0;\n for i in 0..h {\n for j in 0..w {\n if a[color] == 0 {\n color += 1\n }\n a[color] -= 1;\n res[i][if i % 2 == 0 { j } else { w - 1 - j }] = color + 1;\n }\n }\n for i in 0..h {\n print!(\"{}\", res[i][0]);\n for j in 1..w {\n print!(\" {}\", res[i][j]);\n }\n println!();\n }\n}", "difficulty": "hard"} {"problem_id": "1000", "problem_description": "We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.", "c_code": "int solution() {\n\n int i;\n char a[2000];\n char b[2000];\n scanf(\"%s\", a);\n scanf(\"%s\", b);\n if (strlen(a) != strlen(b)) {\n printf(\"No\");\n return 0;\n }\n for (i = 0; b[i] != '\\0'; i++) {\n if (b[i] == 'a' || b[i] == 'e' || b[i] == 'i' || b[i] == 'o' ||\n b[i] == 'u') {\n if (a[i] != 'a' && a[i] != 'e' && a[i] != 'i' && a[i] != 'o' &&\n a[i] != 'u') {\n printf(\"No\");\n return 0;\n }\n } else {\n if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' ||\n a[i] == 'u') {\n printf(\"No\");\n return 0;\n }\n }\n }\n printf(\"Yes\");\n return 0;\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 let t = get().as_bytes();\n\n if s.len() != t.len() {\n println!(\"No\");\n return;\n }\n\n for i in 0..s.len() {\n let vs = match s[i] {\n b'a' | b'i' | b'u' | b'e' | b'o' => true,\n _ => false,\n };\n let vt = match t[i] {\n b'a' | b'i' | b'u' | b'e' | b'o' => true,\n _ => false,\n };\n if vs != vt {\n println!(\"No\");\n return;\n }\n }\n\n println!(\"Yes\");\n}", "difficulty": "easy"} {"problem_id": "1001", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Tak performed the following action N times: rolling two dice.\nThe result of the i-th roll is D_{i,1} and D_{i,2}.

\n

Check if doublets occurred at least three times in a row.\nSpecifically, check if there exists at lease one i such that D_{i,1}=D_{i,2}, D_{i+1,1}=D_{i+1,2} and D_{i+2,1}=D_{i+2,2} hold.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 100
  • \n
  • 1\\leq D_{i,j} \\leq 6
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nD_{1,1} D_{1,2}\n\\vdots\nD_{N,1} D_{N,2}\n
\n
\n
\n
\n
\n

Output

Print Yes if doublets occurred at least three times in a row. Print No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1 2\n6 6\n4 4\n3 3\n3 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

From the second roll to the fourth roll, three doublets occurred in a row.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n1 1\n2 2\n3 4\n5 5\n6 6\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution(void) {\n\n int N = 10;\n int count = 0;\n int judge = 0;\n scanf(\"%d\", &N);\n\n int a[N];\n int b[N];\n\n for (int n = 0; n < N; n++) {\n scanf(\"%d %d\", &a[n], &b[n]);\n }\n\n for (int n = 0; n < N; n++) {\n if (a[n] == b[n]) {\n count++;\n } else {\n count = 0;\n }\n\n if (count == 3) {\n judge = 1;\n }\n }\n\n if (judge == 1) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let a: Vec<_> = (0..n)\n .map(|_| {\n let a: i32 = s.next().unwrap().parse().unwrap();\n let b: i32 = s.next().unwrap().parse().unwrap();\n a == b\n })\n .collect();\n if (0..=n - 3).any(|i| a[i] && a[i + 1] && a[i + 2]) {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "1002", "problem_description": "Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has $$$a$$$ sticks and $$$b$$$ diamonds?", "c_code": "int solution() {\n int t;\n int a;\n int b;\n int k[1000];\n scanf(\"%i\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%i %i\", &a, &b);\n if (a > 0 && b > 0 && a * 2 <= b) {\n k[i] = a;\n } else if (b * 2 <= a && a > 0 && b > 0) {\n k[i] = b;\n } else if (!(a * 2 <= b) && !(b * 2 <= a) && a > 0 && b > 0) {\n k[i] = (a + b) / 3;\n } else {\n k[i] = 0;\n }\n }\n for (int i = 0; i < t; i++) {\n printf(\"%i \\n\", k[i]);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (a, b) = if xs[0] < xs[1] {\n (xs[0], xs[1])\n } else {\n (xs[1], xs[0])\n };\n let ans = if a * 2 <= b {\n min(a, b / 2)\n } else {\n let c = b - a;\n let d = a - c;\n d / 3 * 2 + c + (if d % 3 == 2 { 1 } else { 0 })\n };\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "1003", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

\n

First, you bought one cake for A yen at a cake shop.\nThen, you bought as many donuts as possible for B yen each, at a donut shop.

\n

How much do you have left after shopping?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A, B \\leq 1 000
  • \n
  • A + B \\leq X \\leq 10 000
  • \n
  • X, A and B are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X\nA\nB\n
\n
\n
\n
\n
\n

Output

Print the amount you have left after shopping.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1234\n150\n100\n
\n
\n
\n
\n
\n

Sample Output 1

84\n
\n

You have 1234 - 150 = 1084 yen left after buying a cake.\nWith this amount, you can buy 10 donuts, after which you have 84 yen left.

\n
\n
\n
\n
\n
\n

Sample Input 2

1000\n108\n108\n
\n
\n
\n
\n
\n

Sample Output 2

28\n
\n
\n
\n
\n
\n
\n

Sample Input 3

579\n123\n456\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
\n
\n
\n
\n

Sample Input 4

7477\n549\n593\n
\n
\n
\n
\n
\n

Sample Output 4

405\n
\n
\n
", "c_code": "int solution() {\n int x;\n int a;\n int b = 0;\n scanf(\"%d %d %d\", &x, &a, &b);\n x = x - a;\n while (x >= b) {\n x = x - b;\n if (x <= 0) {\n break;\n }\n }\n printf(\"%d\", x);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf: String = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let x: i32 = buf.trim().parse::().unwrap();\n let mut buf: String = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let a: i32 = buf.trim().parse::().unwrap();\n let mut buf: String = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let b: i32 = buf.trim().parse::().unwrap();\n println!(\"{}\", (x - a) % b);\n}", "difficulty": "medium"} {"problem_id": "1004", "problem_description": "In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems.A permutation $$$p$$$ of size $$$n$$$ is given. A permutation of size $$$n$$$ is an array of size $$$n$$$ in which each integer from $$$1$$$ to $$$n$$$ occurs exactly once. For example, $$$[1, 4, 3, 2]$$$ and $$$[4, 2, 1, 3]$$$ are correct permutations while $$$[1, 2, 4]$$$ and $$$[1, 2, 2]$$$ are not.Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $$$[1, 5, 2]$$$ currently in the deque, adding an element $$$4$$$ to the beginning will produce the sequence $$$[\\color{red}{4}, 1, 5, 2]$$$, and adding same element to the end will produce $$$[1, 5, 2, \\color{red}{4}]$$$.The elements of the permutation are sequentially added to the initially empty deque, starting with $$$p_1$$$ and finishing with $$$p_n$$$. Before adding each element to the deque, you may choose whether to add it to the beginning or the end.For example, if we consider a permutation $$$p = [3, 1, 2, 4]$$$, one of the possible sequences of actions looks like this: $$$\\quad$$$ 1.add $$$3$$$ to the end of the deque:deque has a sequence $$$[\\color{red}{3}]$$$ in it;$$$\\quad$$$ 2.add $$$1$$$ to the beginning of the deque:deque has a sequence $$$[\\color{red}{1}, 3]$$$ in it;$$$\\quad$$$ 3.add $$$2$$$ to the end of the deque:deque has a sequence $$$[1, 3, \\color{red}{2}]$$$ in it;$$$\\quad$$$ 4.add $$$4$$$ to the end of the deque:deque has a sequence $$$[1, 3, 2, \\color{red}{4}]$$$ in it;Find the lexicographically smallest possible sequence of elements in the deque after the entire permutation has been processed. A sequence $$$[x_1, x_2, \\ldots, x_n]$$$ is lexicographically smaller than the sequence $$$[y_1, y_2, \\ldots, y_n]$$$ if there exists such $$$i \\leq n$$$ that $$$x_1 = y_1$$$, $$$x_2 = y_2$$$, $$$\\ldots$$$, $$$x_{i - 1} = y_{i - 1}$$$ and $$$x_i < y_i$$$. In other words, if the sequences $$$x$$$ and $$$y$$$ have some (possibly empty) matching prefix, and the next element of the sequence $$$x$$$ is strictly smaller than the corresponding element of the sequence $$$y$$$. For example, the sequence $$$[1, 3, 2, 4]$$$ is smaller than the sequence $$$[1, 3, 4, 2]$$$ because after the two matching elements $$$[1, 3]$$$ in the start the first sequence has an element $$$2$$$ which is smaller than the corresponding element $$$4$$$ in the second sequence.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int A[(2 * n) + 1];\n int i = n;\n int l = n - 1;\n int r = n + 1;\n int check = 0;\n for (int m = 0; m < n; m++) {\n int temp;\n scanf(\"%d\", &temp);\n if (check == 0) {\n A[i] = temp;\n check = 1;\n } else {\n if (temp <= A[l + 1]) {\n A[l] = temp;\n l--;\n } else {\n A[r] = temp;\n r++;\n }\n }\n }\n for (int i = l + 1; i < r; i++) {\n printf(\"%d \", A[i]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(2).step_by(2) {\n let mut v = std::collections::VecDeque::new();\n l.unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .fold((u32::MAX, 0), |(l, r), d| {\n if d < l {\n v.push_front(d);\n (d, r)\n } else {\n v.push_back(d);\n (l, d)\n }\n });\n writeln!(\n o,\n \"{}\",\n v.iter()\n .map(|d| d.to_string())\n .collect::>()\n .join(\" \")\n )\n .ok();\n }\n}", "difficulty": "hard"} {"problem_id": "1005", "problem_description": "Ivar the Boneless is a great leader. He is trying to capture Kattegat from Lagertha. The war has begun and wave after wave Ivar's warriors are falling in battle.Ivar has $$$n$$$ warriors, he places them on a straight line in front of the main gate, in a way that the $$$i$$$-th warrior stands right after $$$(i-1)$$$-th warrior. The first warrior leads the attack.Each attacker can take up to $$$a_i$$$ arrows before he falls to the ground, where $$$a_i$$$ is the $$$i$$$-th warrior's strength.Lagertha orders her warriors to shoot $$$k_i$$$ arrows during the $$$i$$$-th minute, the arrows one by one hit the first still standing warrior. After all Ivar's warriors fall and all the currently flying arrows fly by, Thor smashes his hammer and all Ivar's warriors get their previous strengths back and stand up to fight again. In other words, if all warriors die in minute $$$t$$$, they will all be standing to fight at the end of minute $$$t$$$.The battle will last for $$$q$$$ minutes, after each minute you should tell Ivar what is the number of his standing warriors.", "c_code": "int solution() {\n long long int n;\n long long int m;\n scanf(\"%lld%lld\", &n, &m);\n long long int a[n + 1];\n a[0] = 0;\n for (long long int i = 1; i <= n; i++) {\n scanf(\"%lld\", &a[i]);\n a[i] = a[i - 1] + a[i];\n }\n\n long long int lb = 0;\n long long int ub = n;\n long long int mid;\n long long int s = 0;\n for (long long int i = 0; i < m; i++) {\n long long int x;\n scanf(\"%lld\", &x);\n s = s + x;\n lb = 1;\n ub = n;\n while (lb <= ub) {\n mid = (lb + ub) / 2;\n if (a[mid] == s) {\n break;\n }\n if (a[mid] > s)\n ub = mid - 1;\n else\n lb = mid + 1;\n }\n if (s >= a[n]) {\n printf(\"%lld\\n\", n);\n s = 0;\n } else {\n if (a[mid] == s) {\n if (mid != n) {\n printf(\"%lld\\n\", n - mid);\n } else {\n printf(\"%lld\\n\", n);\n }\n } else {\n if (lb == n) {\n printf(\"1\\n\");\n } else {\n printf(\"%lld\\n\", n - lb + 1);\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n use std::io::prelude::*;\n\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n let _q: usize = it.next().unwrap().parse().unwrap();\n\n let a: Vec = it.by_ref().take(n).map(|x| x.parse().unwrap()).collect();\n let k: Vec = it.map(|x| x.parse().unwrap()).collect();\n\n let psl: Vec<_> = a\n .iter()\n .scan(0, |state, &ai| {\n *state += ai;\n Some(*state)\n })\n .collect();\n\n let mut current_warrior = 0;\n let mut current_health_points_lost = 0;\n\n let mut ans = String::new();\n\n for ki in k {\n current_health_points_lost += ki;\n let next = psl.binary_search(¤t_health_points_lost);\n current_warrior = match next {\n Ok(i) => i + 1,\n Err(i) => i,\n };\n\n if current_warrior == n {\n current_warrior = 0;\n current_health_points_lost = 0;\n }\n\n ans.push_str(&format!(\"{}\\n\", n - current_warrior));\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1006", "problem_description": "You are given a string A. Find a string B, where B is a palindrome and A is a subsequence of B.A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the order of the remaining characters. For example, \"cotst\" is a subsequence of \"contest\".A palindrome is a string that reads the same forward or backward.The length of string B should be at most 104. It is guaranteed that there always exists such string.You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 104.", "c_code": "int solution() {\n char A[1003];\n char B[2006];\n int i = 0;\n scanf(\"%s\", A);\n int len = 0;\n while (A[len] != '\\0') {\n len++;\n }\n while (A[i] != '\\0') {\n B[i] = A[len - i - 1];\n i++;\n }\n int j = 0;\n while (A[j] != '\\0') {\n B[i] = A[j];\n j++;\n i++;\n }\n B[i] = '\\0';\n printf(\"%s\\n\", B);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::prelude::*;\n std::io::stdin().read_to_string(&mut input).unwrap();\n input.trim();\n\n let output: String = input\n .trim()\n .chars()\n .chain(input.trim().chars().rev())\n .collect();\n\n println!(\"{}\", output);\n}", "difficulty": "hard"} {"problem_id": "1007", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There is a box containing N balls. The i-th ball has the integer A_i written on it.\nSnuke can perform the following operation any number of times:

\n
    \n
  • Take out two balls from the box. Then, return them to the box along with a new ball, on which the absolute difference of the integers written on the two balls is written.
  • \n
\n

Determine whether it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
  • 1 \\leq K \\leq 10^9
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

If it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written, print POSSIBLE; if it is not possible, print IMPOSSIBLE.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 7\n9 3 4\n
\n
\n
\n
\n
\n

Sample Output 1

POSSIBLE\n
\n

First, take out the two balls 9 and 4, and return them back along with a new ball, abs(9-4)=5.\nNext, take out 3 and 5, and return them back along with abs(3-5)=2.\nFinally, take out 9 and 2, and return them back along with abs(9-2)=7.\nNow we have 7 in the box, and the answer is therefore POSSIBLE.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 5\n6 9 3\n
\n
\n
\n
\n
\n

Sample Output 2

IMPOSSIBLE\n
\n

No matter what we do, it is not possible to have 5 in the box. The answer is therefore IMPOSSIBLE.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 11\n11 3 7 15\n
\n
\n
\n
\n
\n

Sample Output 3

POSSIBLE\n
\n

The box already contains 11 before we do anything. The answer is therefore POSSIBLE.

\n
\n
\n
\n
\n
\n

Sample Input 4

5 12\n10 2 8 6 4\n
\n
\n
\n
\n
\n

Sample Output 4

IMPOSSIBLE\n
\n
\n
", "c_code": "int solution() {\n int i;\n int j;\n int n;\n int k;\n int min;\n int max;\n\n scanf(\"%d %d\", &n, &k);\n int a[n];\n scanf(\"%d\", &a[0]);\n min = a[0];\n max = a[0];\n for (i = 1; i < n; i++) {\n scanf(\" %d\", &a[i]);\n if (min > a[i]) {\n min = a[i];\n }\n if (max < a[i]) {\n max = a[i];\n }\n }\n if (max < k) {\n printf(\"IMPOSSIBLE\");\n return 0;\n }\n if (min == 1) {\n printf(\"POSSIBLE\");\n return 0;\n }\n\n for (i = min; i >= 1; i--) {\n for (j = 0; j < n; j++) {\n if (a[j] % i != 0) {\n break;\n }\n }\n if (j == n) {\n if (k % i == 0) {\n printf(\"POSSIBLE\");\n return 0;\n }\n printf(\"IMPOSSIBLE\");\n return 0;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n k: i64,\n a_vec: [i64; n]\n }\n let words = (\"POSSIBLE\", \"IMPOSSIBLE\");\n let euc = |num1: i64, num2: i64| -> i64 {\n if num1 == 0 || num2 == 0 {\n return cmp::max(num1, num2);\n }\n let mut la;\n let mut mi;\n if num1 < num2 {\n la = num2;\n mi = num1;\n } else {\n la = num1;\n mi = num2;\n }\n while la % mi != 0 {\n let hold = la;\n la = mi;\n mi = hold % mi;\n }\n mi\n };\n let (euc_total, a_max) = a_vec\n .into_iter()\n .fold((0, 0), |(total, mx), i| (euc(i, total), cmp::max(mx, i)));\n\n println!(\n \"{}\",\n if k <= a_max && k % euc_total == 0 {\n words.0\n } else {\n words.1\n }\n );\n}", "difficulty": "medium"} {"problem_id": "1008", "problem_description": "Monocarp has forgotten the password to his mobile phone. The password consists of $$$4$$$ digits from $$$0$$$ to $$$9$$$ (note that it can start with the digit $$$0$$$).Monocarp remembers that his password had exactly two different digits, and each of these digits appeared exactly two times in the password. Monocarp also remembers some digits which were definitely not used in the password.You have to calculate the number of different sequences of $$$4$$$ digits that could be the password for Monocarp's mobile phone (i. e. these sequences should meet all constraints on Monocarp's password).", "c_code": "int solution() {\n\n int n = 0;\n scanf(\"%d\", &n);\n int i = 0;\n int j = 0;\n int l = 0;\n int result = 0;\n int arr[10];\n int k = 0;\n if (n < 1 || n > 200) {\n return 0;\n }\n\n for (i = 0; i < n && i < 200; i++) {\n scanf(\"%d\", &j);\n if (j < 1 || j > 8) {\n return 0;\n }\n k = 0;\n while (k < j) {\n scanf(\"%d\", &arr[k]);\n k++;\n }\n\n printf(\"%d\\n\", 3 * ((9 - j) * (9 - j + 1)));\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut tests = String::new();\n io::stdin().read_line(&mut tests).unwrap();\n let tests = tests.trim().parse().unwrap();\n\n for _ in 0..tests {\n let mut digits = String::new();\n io::stdin().read_line(&mut digits).unwrap();\n let digits: i32 = digits.trim().parse().unwrap();\n let combinations = (10 - digits) * (9 - digits) * 3;\n let mut placeholder = String::new();\n io::stdin().read_line(&mut placeholder).unwrap();\n println!(\"{combinations}\");\n }\n}", "difficulty": "medium"} {"problem_id": "1009", "problem_description": "There was an electronic store heist last night.All keyboards which were in the store yesterday were numbered in ascending order from some integer number $$$x$$$. For example, if $$$x = 4$$$ and there were $$$3$$$ keyboards in the store, then the devices had indices $$$4$$$, $$$5$$$ and $$$6$$$, and if $$$x = 10$$$ and there were $$$7$$$ of them then the keyboards had indices $$$10$$$, $$$11$$$, $$$12$$$, $$$13$$$, $$$14$$$, $$$15$$$ and $$$16$$$.After the heist, only $$$n$$$ keyboards remain, and they have indices $$$a_1, a_2, \\dots, a_n$$$. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither $$$x$$$ nor the number of keyboards in the store before the heist.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n scanf(\"%d\", &arr[0]);\n int mx = arr[0];\n int mn = arr[0];\n for (int i = 1; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] > mx) {\n mx = arr[i];\n }\n if (arr[i] < mn) {\n mn = arr[i];\n }\n }\n printf(\"%d\", mx - mn + 1 - 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\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let n = arr[0] as usize;\n\n let mut a = 2_000_000_000;\n let mut b = 0;\n for i in 0..n {\n let v = arr[1 + i];\n use std::cmp;\n a = cmp::min(a, v);\n b = cmp::max(b, v);\n }\n let dif = b - a;\n let ans = dif + 1 - n as i64;\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1010", "problem_description": "You are given a string $$$s$$$ consisting of the characters 0, 1, and ?.Let's call a string unstable if it consists of the characters 0 and 1 and any two adjacent characters are different (i. e. it has the form 010101... or 101010...).Let's call a string beautiful if it consists of the characters 0, 1, and ?, and you can replace the characters ? to 0 or 1 (for each character, the choice is independent), so that the string becomes unstable.For example, the strings 0??10, 0, and ??? are beautiful, and the strings 00 and ?1??1 are not.Calculate the number of beautiful contiguous substrings of the string $$$s$$$.", "c_code": "int solution() {\n int x;\n int y;\n int z;\n int i;\n int j;\n int k;\n int c;\n int n;\n int m;\n int t;\n long long res;\n long long a;\n long long b;\n char s[210210];\n\n scanf(\"%d\", &t);\n while (t--) {\n res = 0;\n scanf(\"%s\", s);\n n = strlen(s);\n\n i = a = 0;\n for (x = 0; x < n; x++) {\n if (s[x] == '0' + i || s[x] == '?') {\n a++;\n } else {\n res += a * (a + 1) / 2;\n a = 0;\n }\n i = 1 - i;\n }\n\n res += a * (a + 1) / 2;\n\n a = 0;\n i = 1;\n for (x = 0; x < n; x++) {\n if (s[x] == '0' + i || s[x] == '?') {\n a++;\n } else {\n res += a * (a + 1) / 2;\n a = 0;\n }\n i = 1 - i;\n }\n\n res += a * (a + 1) / 2;\n\n b = 0;\n for (x = 0; x < n; x++) {\n if (s[x] == '?') {\n b++;\n } else {\n res -= b * (b + 1) / 2;\n b = 0;\n }\n }\n\n res -= b * (b + 1) / 2;\n\n printf(\"%lld\\n\", res);\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\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 for _ in 0..t {\n let s = lines.next().unwrap();\n\n let mut x = 0;\n let mut d = 0;\n let mut e = 0;\n\n let mut ans: u64 = 0;\n\n let it = s.chars();\n\n for c in it {\n match c {\n '0' => {\n if d == 2 {\n x = e;\n }\n\n d = 2;\n e = 0;\n }\n '1' => {\n if d == 1 {\n x = e;\n }\n\n d = 1;\n e = 0;\n }\n _ => {\n if d == 2 {\n d = 1;\n } else if d == 1 {\n d = 2;\n }\n e += 1;\n }\n }\n x += 1;\n ans += x;\n }\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1011", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have N bricks arranged in a row from left to right.

\n

The i-th brick from the left (1 \\leq i \\leq N) has an integer a_i written on it.

\n

Among them, you can break at most N-1 bricks of your choice.

\n

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \\leq i \\leq K), the i-th of those brick from the left has the integer i written on it.

\n

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 200000
  • \n
  • 1 \\leq a_i \\leq N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2 1 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n2 2 2\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

\n
\n
\n
\n
\n
\n

Sample Input 3

10\n3 1 4 1 5 9 2 6 5 3\n
\n
\n
\n
\n
\n

Sample Output 3

7\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1\n1\n
\n
\n
\n
\n
\n

Sample Output 4

0\n
\n

There may be no need to break the bricks at all.

\n
\n
", "c_code": "int solution(void) {\n\n int a[200000];\n int i = 0;\n int N = 0;\n int count = 0;\n\n scanf(\"%d\", &N);\n\n while (i < N) {\n scanf(\"%d\", &a[i]);\n i++;\n }\n\n for (i = 0; i < N; i++) {\n if (a[i] == count + 1) {\n count++;\n }\n }\n\n if (count == 0) {\n printf(\"%d\", -1);\n } else {\n printf(\"%d\", N - count);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let remain = s\n .map(|a| a.parse().unwrap())\n .fold((0, 0), |r, a| if r.1 + 1 == a { (r.0 + 1, a) } else { r })\n .0;\n if remain == 0 {\n println!(\"-1\");\n } else {\n println!(\"{}\", n - remain);\n }\n}", "difficulty": "medium"} {"problem_id": "1012", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.\nHe can climb up one or two steps at a time.

\n

However, the treads of the a_1-th, a_2-th, a_3-th, \\ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

\n

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?\nFind the count modulo 1\\ 000\\ 000\\ 007.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 0 \\leq M \\leq N-1
  • \n
  • 1 \\leq a_1 < a_2 < ... < a_M \\leq N-1
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\na_1\na_2\n .\n .\n .\na_M\n
\n
\n
\n
\n
\n

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\\ 000\\ 000\\ 007.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 1\n3\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

There are four ways to climb up the stairs, as follows:

\n
    \n
  • 0 \\to 1 \\to 2 \\to 4 \\to 5 \\to 6
  • \n
  • 0 \\to 1 \\to 2 \\to 4 \\to 6
  • \n
  • 0 \\to 2 \\to 4 \\to 5 \\to 6
  • \n
  • 0 \\to 2 \\to 4 \\to 6
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

10 2\n4\n5\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

There may be no way to climb up the stairs without setting foot on the broken steps.

\n
\n
\n
\n
\n
\n

Sample Input 3

100 5\n1\n23\n45\n67\n89\n
\n
\n
\n
\n
\n

Sample Output 3

608200469\n
\n

Be sure to print the count modulo 1\\ 000\\ 000\\ 007.

\n
\n
", "c_code": "int solution() {\n int N;\n int M;\n int j = 0;\n long long int mod = 1000000007;\n scanf(\"%d%d\", &N, &M);\n int a[M];\n long long int dp[N + 1];\n for (int i = 0; i < M; i++) {\n scanf(\"%d\", &a[i]);\n }\n dp[0] = 0;\n for (int i = 1; i <= N; i++) {\n if (a[j] == i) {\n\n dp[i] = 0;\n j++;\n } else if (i == 1) {\n dp[1] = 1;\n } else if (i == 2 && dp[1] == 1) {\n dp[2] = 2 % mod;\n } else if (i == 2 && dp[1] == 0) {\n dp[2] = 1 % mod;\n } else {\n dp[i] = (dp[i - 1] + dp[i - 2]) % mod;\n }\n }\n printf(\"%lld\\n\", dp[N]);\n return 0;\n}", "rust_code": "fn solution() {\n let mut nm = String::new();\n stdin().read_line(&mut nm).unwrap();\n let nm: Vec = nm.split_whitespace().flat_map(str::parse).collect();\n let (n, m) = (nm[0], nm[1]);\n\n let danger_zone: HashSet = HashSet::from_iter((0..m).map(|_| {\n let mut a = String::new();\n stdin().read_line(&mut a).unwrap();\n a.trim().parse().unwrap()\n }));\n\n let mut cmb = vec![0; n + 1];\n cmb[0] = 1;\n for i in 0..n {\n for j in i + 1..i + 3 {\n if !danger_zone.contains(&j) && j <= n {\n cmb[j] = (cmb[j] + cmb[i]) % 1000000007;\n }\n }\n }\n println!(\"{}\", cmb[n]);\n}", "difficulty": "medium"} {"problem_id": "1013", "problem_description": "\n
\n
\n

Problem Statement

We have N pieces of ropes, numbered 1 through N. The length of piece i is a_i.

\n

At first, for each i (1≤i≤N-1), piece i and piece i+1 are tied at the ends, forming one long rope with N-1 knots. Snuke will try to untie all of the knots by performing the following operation repeatedly:

\n
    \n
  • Choose a (connected) rope with a total length of at least L, then untie one of its knots.
  • \n
\n

Is it possible to untie all of the N-1 knots by properly applying this operation? If the answer is positive, find one possible order to untie the knots.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≤N≤10^5
  • \n
  • 1≤L≤10^9
  • \n
  • 1≤a_i≤10^9
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N L\na_1 a_2 ... a_n\n
\n
\n
\n
\n
\n

Output

If it is not possible to untie all of the N-1 knots, print Impossible.

\n

If it is possible to untie all of the knots, print Possible, then print another N-1 lines that describe a possible order to untie the knots. The j-th of those N-1 lines should contain the index of the knot that is untied in the j-th operation. Here, the index of the knot connecting piece i and piece i+1 is i.

\n

If there is more than one solution, output any.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 50\n30 20 10\n
\n
\n
\n
\n
\n

Sample Output 1

Possible\n2\n1\n
\n

If the knot 1 is untied first, the knot 2 will become impossible to untie.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 21\n10 10\n
\n
\n
\n
\n
\n

Sample Output 2

Impossible\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5 50\n10 20 30 40 50\n
\n
\n
\n
\n
\n

Sample Output 3

Possible\n1\n2\n3\n4\n
\n

Another example of a possible solution is 3, 4, 1, 2.

\n
\n
", "c_code": "int solution() {\n int num;\n int length;\n int arr[100000];\n int i;\n int check = 0;\n int temp;\n\n scanf(\"%d%d\", &num, &length);\n for (i = 0; i < num; i++) {\n scanf(\"%d\", &arr[i]);\n if (i && arr[i - 1] + arr[i] >= length) {\n check = 1;\n temp = i;\n }\n }\n\n if (check) {\n printf(\"Possible\\n\");\n for (i = 1; i < temp; i++) {\n printf(\"%d\\n\", i);\n }\n for (i = num - 1; i >= temp; i--) {\n printf(\"%d\\n\", i);\n }\n } else {\n printf(\"Impossible\\n\");\n }\n\n return 0;\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 l: usize = itr.next().unwrap().parse().unwrap();\n let a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n let mut saigo = n;\n for i in 0..n - 1 {\n if a[i] + a[i + 1] >= l {\n saigo = i;\n }\n }\n if saigo == n {\n println!(\"Impossible\");\n return;\n }\n\n let mut out = Vec::new();\n writeln!(out, \"Possible\").ok();\n for i in 0..saigo {\n writeln!(out, \"{}\", i + 1).ok();\n }\n for i in (saigo..n - 1).rev() {\n writeln!(out, \"{}\", i + 1).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "easy"} {"problem_id": "1014", "problem_description": "Recently in Divanovo, a huge river locks system was built. There are now $$$n$$$ locks, the $$$i$$$-th of them has the volume of $$$v_i$$$ liters, so that it can contain any amount of water between $$$0$$$ and $$$v_i$$$ liters. Each lock has a pipe attached to it. When the pipe is open, $$$1$$$ liter of water enters the lock every second.The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $$$i$$$ to the lock $$$i + 1$$$. If the lock $$$i + 1$$$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $$$5$$$ locks with two open pipes at locks $$$1$$$ and $$$3$$$. Because locks $$$1$$$, $$$3$$$, and $$$4$$$ are already filled, effectively the water goes to locks $$$2$$$ and $$$5$$$. Note that the volume of the $$$i$$$-th lock may be greater than the volume of the $$$i + 1$$$-th lock.To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $$$q$$$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $$$j$$$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $$$t_j$$$ seconds.Please help the mayor to solve this tricky problem and answer his queries.", "c_code": "int solution() {\n int n = 0;\n long long v = 0LL;\n int q = 0;\n long long t = 0LL;\n\n int res = 0;\n\n long long max_t[200000] = {};\n long long sum = 0LL;\n\n res = scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n res = scanf(\"%lld\", &v);\n sum += v;\n max_t[i] = 1LL + (sum - 1LL) / ((long long)(i + 1));\n }\n\n for (int i = 1; i < n; i++) {\n if (max_t[i] < max_t[i - 1]) {\n max_t[i] = max_t[i - 1];\n }\n }\n\n res = scanf(\"%d\", &q);\n\n while (q > 0) {\n long long pcnt = 0LL;\n res = scanf(\"%lld\", &t);\n pcnt = 1LL + (sum - 1LL) / t;\n if (pcnt > ((long long)n) || max_t[(int)(pcnt - 1LL)] > t) {\n printf(\"-1\\n\");\n } else {\n printf(\"%lld\\n\", pcnt);\n }\n q--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin().read_to_string(&mut content);\n\n let mut lines = content.lines();\n let n: usize = lines.next().unwrap().parse().unwrap();\n let a: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse::().unwrap())\n .collect();\n let m: usize = lines.next().unwrap().parse().unwrap();\n let mut b: Vec = vec![];\n for _ in 0..m {\n let k: usize = lines.next().unwrap().parse().unwrap();\n b.push(k);\n }\n let mut sums: Vec = vec![a[0]];\n for i in 1..n {\n sums.push(sums[i - 1] + a[i]);\n }\n let mut means: Vec = vec![sums[0]];\n for i in 1..n {\n means.push(sums[i] / (i + 1) + if !sums[i].is_multiple_of(i + 1) { 1 } else { 0 });\n }\n let &min_time = means.iter().max().unwrap();\n let &total_sum = sums.last().unwrap();\n for query in b {\n if query < min_time {\n println!(\"-1\");\n } else {\n let result = total_sum / query + if total_sum % query > 0 { 1 } else { 0 };\n println!(\"{result}\");\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1015", "problem_description": "A bow adorned with nameless flowers that bears the earnest hopes of an equally nameless person.You have obtained the elegant bow known as the Windblume Ode. Inscribed in the weapon is an array of $$$n$$$ ($$$n \\ge 3$$$) positive distinct integers (i.e. different, no duplicates are allowed).Find the largest subset (i.e. having the maximum number of elements) of this array such that its sum is a composite number. A positive integer $$$x$$$ is called composite if there exists a positive integer $$$y$$$ such that $$$1 < y < x$$$ and $$$x$$$ is divisible by $$$y$$$.If there are multiple subsets with this largest size with the composite sum, you can output any of them. It can be proven that under the constraints of the problem such a non-empty subset always exists.", "c_code": "int solution() {\n int t;\n int i;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int s = 0;\n int c = 0;\n int k = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n s = s + a[i];\n }\n int d = sqrt(s) + 0.5;\n for (i = 2; i <= d; i++) {\n if (s % i == 0) {\n k = 1;\n break;\n }\n }\n if (k != 0) {\n printf(\"%d\\n\", n);\n for (i = 0; i < n; i++) {\n printf(\"%d \", i + 1);\n }\n printf(\"\\n\");\n } else {\n printf(\"%d\\n\", n - 1);\n for (i = 0; i < n; i++) {\n if (a[i] % 2 == 1 && c == 0) {\n c = 1;\n } else {\n printf(\"%d \", i + 1);\n }\n }\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut s = String::new();\n io::stdin().read_to_string(&mut s)?;\n let mut it = s.split_whitespace().map(|s| s.to_string());\n let mut out = String::new();\n let _case: usize = it.next().unwrap().parse().unwrap();\n for _ in 0.._case {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a = vec![];\n let mut tot = 0;\n for _ in 0..n {\n let x: i32 = it.next().unwrap().parse().unwrap();\n a.push(x);\n tot += x;\n }\n let mut is_prime = true;\n let mut f = 2;\n while f * f <= tot {\n if tot % f == 0 {\n is_prime = false;\n }\n f += 1;\n }\n let mut done = !is_prime;\n if done {\n out.push_str(format!(\"{}\\n\", n).as_str());\n } else {\n out.push_str(format!(\"{}\\n\", n - 1).as_str());\n }\n for i in 0..n {\n if !done && a[i] % 2 == 1 {\n done = true;\n continue;\n }\n if i + 1 != n {\n out.push_str(format!(\"{} \", i + 1).as_str());\n } else {\n out.push_str(format!(\"{}\\n\", i + 1).as_str());\n }\n }\n }\n\n print!(\"{}\", out);\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1016", "problem_description": "Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead.A snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r, c). The tail of the snake is located at (1, 1), then it's body extends to (1, m), then goes down 2 rows to (3, m), then goes left to (3, 1) and so on.Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and the snake cells should be filled with number signs ('#').Consider sample tests in order to understand the snake pattern.", "c_code": "int solution() {\n int i = 1;\n int j = 0;\n int n;\n int m;\n scanf(\"%d\", &n);\n scanf(\"%d\", &m);\n while (i <= n) {\n if (i % 4 == 0) {\n j = 0;\n while (j < m) {\n if (j == 0) {\n printf(\"#\");\n } else {\n printf(\".\");\n }\n j++;\n }\n } else if (i % 2 == 0 && i % 4 != 0) {\n j = 0;\n while (j < m) {\n if (j == m - 1) {\n printf(\"#\");\n } else {\n printf(\".\");\n }\n j++;\n }\n } else if ((i % 2 != 0)) {\n j = 0;\n while (j < m) {\n printf(\"#\");\n j++;\n }\n }\n printf(\"\\n\");\n i++;\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut n = words[0];\n let m = words[1];\n let mut direction = true;\n\n while n > 0 {\n if n % 2 == 1 {\n for _ in 0..m {\n print!(\"#\");\n }\n println!();\n } else {\n if direction {\n for _ in 0..m - 1 {\n print!(\".\");\n }\n println!(\"#\");\n direction = !direction;\n } else {\n print!(\"#\");\n for _ in 0..m - 1 {\n print!(\".\");\n }\n println!();\n direction = !direction;\n }\n }\n n -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "1017", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:

\n
    \n
  • \n

    1 yen (the currency of Japan)

    \n
  • \n
  • \n

    6 yen, 6^2(=36) yen, 6^3(=216) yen, ...

    \n
  • \n
  • \n

    9 yen, 9^2(=81) yen, 9^3(=729) yen, ...

    \n
  • \n
\n

At least how many operations are required to withdraw exactly N yen in total?

\n

It is not allowed to re-deposit the money you withdrew.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100000
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If at least x operations are required to withdraw exactly N yen in total, print x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

127\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

By withdrawing 1 yen three times, we can withdraw 3 yen in three operations.

\n
\n
\n
\n
\n
\n

Sample Input 3

44852\n
\n
\n
\n
\n
\n

Sample Output 3

16\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n scanf(\"%d\", &N);\n int six[] = {1, 6, 36, 216, 1296, 7776, 46656};\n int nine[] = {1, 9, 81, 729, 6561, 59049};\n int min = N;\n for (int s1 = 0; s1 < 6; s1++) {\n for (int s2 = 0; s2 < 6; s2++) {\n for (int s3 = 0; s3 < 6; s3++) {\n for (int s4 = 0; s4 < 6; s4++) {\n for (int s5 = 0; s5 < 6; s5++) {\n for (int s6 = 0; s6 < 3; s6++) {\n for (int n1 = 0; n1 < 9; n1++) {\n for (int n2 = 0; n2 < 9; n2++) {\n for (int n3 = 0; n3 < 9; n3++) {\n for (int n4 = 0; n4 < 9; n4++) {\n for (int n5 = 0; n5 < 2; n5++) {\n int t = (s1 * six[1]) + (s2 * six[2]) + (s3 * six[3]) +\n (s4 * six[4]) + (s5 * six[5]) + (s6 * six[6]) +\n (n1 * nine[1]) + (n2 * nine[2]) +\n (n3 * nine[3]) + (n4 * nine[4]) +\n (n5 * nine[5]);\n int t2 = N - t + s1 + s2 + s3 + s4 + s5 + s6 + n1 + n2 +\n n3 + n4 + n5;\n if (t <= N && t2 < min) {\n min = t2;\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n printf(\"%d\\n\", min);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n }\n let mut ans = n;\n for i in 0..n + 1 {\n if n < 9 * i {\n continue;\n }\n let r = n - 9 * i;\n let j = r / 6;\n let k = r % 6;\n\n let mut num = k;\n let mut t = i;\n while t > 0 {\n num += t % 9;\n t /= 9;\n }\n let mut t = j;\n while t > 0 {\n num += t % 6;\n t /= 6;\n }\n\n ans = min(ans, num);\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1018", "problem_description": "

Greatest Common Divisor

\n\n

\nWrite a program which finds the greatest common divisor of two natural numbers a and b\n

\n\n

Input

\n\n

\na and b are given in a line sparated by a single space.\n

\n\n

Output

\n\n

\nOutput the greatest common divisor of a and b.\n

\n\n\n

Constrants

\n

\n1 ≤ a, b ≤ 109\n

\n\n

Hint

\n

\nYou can use the following observation:\n

\n

\nFor integers x and y, if xy, then gcd(x, y) = gcd(y, x%y)\n

\n\n\n

Sample Input 1

\n
\n54 20\n
\n

Sample Output 1

\n
\n2\n
\n\n

Sample Input 2

\n
\n147 105\n
\n

Sample Output 2

\n
\n21\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n\n scanf(\"%d%d\", &a, &b);\n\n if (a < 1 || a > 1000000000 || b < 1 || b > 1000000000) {\n return 0;\n }\n\n while (a != b) {\n if (a > b) {\n a = a - b;\n } else {\n b = b - a;\n }\n }\n printf(\"%d\\n\", a);\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).ok();\n let vec: Vec<&str> = buf.split_whitespace().collect();\n let mut a: Vec = vec.iter().map(|&x| x.parse().unwrap_or(0)).collect();\n let mut gcd = 1;\n let mut d = 2;\n loop {\n if a[0] == 1 || a[1] == 1 {\n break;\n } else if a[0].is_multiple_of(d) && a[1].is_multiple_of(d) {\n a[0] /= d;\n a[1] /= d;\n gcd *= d;\n d = 2;\n } else if a[0] == d || a[1] == d {\n break;\n } else {\n d += 1;\n }\n }\n println!(\"{}\", gcd);\n}", "difficulty": "hard"} {"problem_id": "1019", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

A game is played on a strip consisting of N cells consecutively numbered from 1 to N.

\n

Alice has her token on cell A. Borys has his token on a different cell B.

\n

Players take turns, Alice moves first.\nThe moving player must shift his or her token from its current cell X to the neighboring cell on the left, cell X-1, or on the right, cell X+1.\nNote that it's disallowed to move the token outside the strip or to the cell with the other player's token.\nIn one turn, the token of the moving player must be shifted exactly once.

\n

The player who can't make a move loses, and the other player wins.

\n

Both players want to win. Who wins if they play optimally?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 100
  • \n
  • 1 \\leq A < B \\leq N
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B\n
\n
\n
\n
\n
\n

Output

Print Alice if Alice wins, Borys if Borys wins, and Draw if nobody wins.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

Alice\n
\n

Alice can move her token to cell 3. \nAfter that, Borys will be unable to move his token to cell 3, so he will have to move his token to cell 5. \nThen, Alice moves her token to cell 4. Borys can't make a move and loses.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 1 2\n
\n
\n
\n
\n
\n

Sample Output 2

Borys\n
\n

Alice can't make the very first move and loses.

\n
\n
\n
\n
\n
\n

Sample Input 3

58 23 42\n
\n
\n
\n
\n
\n

Sample Output 3

Borys\n
\n
\n
", "c_code": "int solution() {\n int a;\n int b;\n scanf(\"%*d %d %d\", &a, &b);\n printf(\"%s\\n\", (b - a) % 2 ? \"Borys\" : \"Alice\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let v: Vec = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();\n if (v[2] - v[1]).is_multiple_of(2) {\n println!(\"Alice\");\n } else {\n println!(\"Borys\");\n }\n}", "difficulty": "easy"} {"problem_id": "1020", "problem_description": "

クリスマスパーティー (Christmas Party)

\n\n

問題

\n\n

\nJOI 君は友達 1 から友達 N までの N 人の友達を招いてクリスマスパーティーを行った.クリスマスパーティーも盛り上がってきたところで,友達と一緒に次のようなゲームを行うことになった.\n

\n\n
    \n
  1. \n最初に JOI 君は N 人の友達の中から 1 人を選ぶ.以降はその友達をターゲットと呼ぶことにする.\n
  2. \n
  3. \nJOI 君は,ターゲットとして選んだ友達に,その人がターゲットであることをこっそり伝える.ターゲット以外の友達は,誰がターゲットかを知ることはできない.\n
  4. \n
  5. \nターゲット以外の友達はそれぞれ,ターゲットが誰かを予想して,その人の名前を紙に記入する.ターゲットは自分自身の名前を紙に記入する.\n
  6. \n
  7. \nすべての人の記入が終わった後,JOI 君はターゲットの名前を発表する.\n
  8. \n
  9. \n予想が当たった人は 1 点を得る.なお,ターゲットは自分自身の名前を紙に記入しているので,必ず 1 点を得る.予想が外れた人には得点は与えられない.\n
  10. \n
  11. \nそれに加えて,予想が外れた人の人数を X 人としたとき,ターゲットは追加で X 点を得る.\n
  12. \n
\n\n

\nJOI 君たちはこのゲームを M 回行った.それぞれの友達に対して,M 回のゲームにおける合計得点を求めよ.\n

\n\n

入力

\n\n

\n入力は 3 + M 行からなる.\n

\n\n

\n1 行目には,友達の人数 N (3 ≤ N ≤ 100) が書かれている.\n

\n\n

\n2 行目には,JOI 君たちが行ったゲームの回数 M (3 ≤ M ≤ 100) が書かれている.\n

\n\n

\n3 行目には,M 個の整数 A1, A2, ..., AM が空白を区切りとして書かれている.これは,i 回目 (1 ≦ i ≦ M) のゲームのターゲットが友達 Ai (1 ≤ Ai ≤ N) であることを表す.\n

\n\n

\n続く M 行のうちの i 行目 (1 ≦ i ≦ M) には,N 個の整数 Bi,1, Bi,2, ..., Bi,N が空白を区切りとして書かれている.これは,i 回目のゲームにおいて友達 j (1 ≤ j ≤ N) が友達 Bi,j (1 ≤ Bi,j ≤ N) の名前を紙に記入したことを表す.ターゲットは自分自身の名前を紙に記入するので,j = Ai のとき,常に Bi,j = j である.\n

\n\n

出力

\n

\nそれぞれの友達に対して,M 回のゲームにおける合計得点を出力せよ.出力は N 行からなる.j 行目 (1 ≤ j ≤ N) に友達 j の合計得点を出力せよ.\n

\n\n

入出力例

\n\n

入力例 1

\n
\n3\n4\n1 2 3 2\n1 1 2\n3 2 2\n1 1 3\n2 2 2\n
\n

出力例 1

\n
\n3\n4\n5\n
\n

入力例 2

\n
\n5\n3\n3 3 1\n2 4 3 3 3\n4 3 3 3 1\n1 3 4 1 1\n
\n\n

出力例 1

\n
\n3\n1\n6\n3\n2\n
\n\n\n

\n入出力例 1 では 3 人の友達が 4 回のゲームを行う.\n

\n\n
    \n
  • 1 回目のゲームのターゲットは友達 1 であり,友達 1 は 2 点,友達 2 は 1 点,友達 3 は 0 点を得る.
  • \n
  • 2 回目のゲームのターゲットは友達 2 であり,友達 1 は 0 点,友達 2 は 2 点,友達 3 は 1 点を得る.
  • \n
  • 3 回目のゲームのターゲットは友達 3 であり,友達 1 は 0 点,友達 2 は 0 点,友達 3 は 3 点を得る.
  • \n
  • 4 回目のゲームのターゲットは友達 2 であり,友達 1 は 1 点,友達 2 は 1 点,友達 3 は 1 点を得る.
  • \n
\n\n

\n4 回のゲーム終了後の合計得点は,友達 1 は 3 点,友達 2 は 4 点,友達 3 は 5 点である.\n

\n\n\n
\n

\n問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。\n

\n
", "c_code": "int solution() {\n int friends = 0;\n int game = 0;\n int yosou = 0;\n int i = 0;\n int j = 0;\n int k = 0;\n int target[1000] = {0};\n int point[1000] = {0};\n scanf(\"%d\", &friends);\n scanf(\"%d\", &game);\n for (i = 0; game > i; i++) {\n scanf(\"%d\", &target[i]);\n }\n for (i = 0; game > i; i++) {\n for (j = 1; friends >= j; j++) {\n scanf(\"%d\", &yosou);\n if (target[i] == yosou) {\n k = j;\n point[k] = point[k] + 1;\n } else if (target[i] != yosou) {\n k = target[i];\n point[k] = point[k] + 1;\n }\n }\n }\n for (k = 1; friends >= k; k++) {\n printf(\"%d\\n\", point[k]);\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 n = lines.next().unwrap().parse().unwrap();\n let m = lines.next().unwrap().parse().unwrap();\n\n let targets = lines\n .next()\n .unwrap()\n .split(' ')\n .map(|s| s.parse::().unwrap());\n let iter = lines\n .take(m)\n .map(|l| l.split(' ').map(|s| s.parse().unwrap()));\n\n let mut pts = vec![0; n];\n\n for (a, ints) in targets.zip(iter) {\n let mut bonus = 0;\n\n for (b, pt) in ints.zip(&mut pts) {\n if a == b {\n *pt += 1;\n } else {\n bonus += 1;\n }\n }\n pts[a - 1] += bonus;\n }\n\n let mut result = String::with_capacity(n * 2);\n for pt in pts {\n writeln!(result, \"{}\", pt);\n }\n print!(\"{}\", result);\n}", "difficulty": "hard"} {"problem_id": "1021", "problem_description": "You are given a string $$$s$$$, consisting of $$$n$$$ letters, each letter is either 'a' or 'b'. The letters in the string are numbered from $$$1$$$ to $$$n$$$.$$$s[l; r]$$$ is a continuous substring of letters from index $$$l$$$ to $$$r$$$ of the string inclusive. A string is called balanced if the number of letters 'a' in it is equal to the number of letters 'b'. For example, strings \"baba\" and \"aabbab\" are balanced and strings \"aaab\" and \"b\" are not.Find any non-empty balanced substring $$$s[l; r]$$$ of string $$$s$$$. Print its $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le n$$$). If there is no such substring, then print $$$-1$$$ $$$-1$$$.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n char s[n];\n scanf(\"%s\", s);\n int temp = 0;\n for (int i = 0; i < n - 1; i++) {\n if ((s[i] == 'a' && s[i + 1] == 'b') ||\n (s[i] == 'b' && s[i + 1] == 'a')) {\n printf(\"%d %d\", i + 1, i + 2);\n temp = 1;\n break;\n }\n }\n if (temp == 0) {\n printf(\"-1 -1\");\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut input = String::new();\n stdin.read_line(&mut input).unwrap();\n let n = input.trim().parse::().unwrap();\n for _ in 0..n {\n input.clear();\n stdin.read_line(&mut input).unwrap();\n input.clear();\n stdin.read_line(&mut input).unwrap();\n let string = input.trim().chars().collect::>();\n let len = string.len();\n let mut start = -1;\n let mut end = -1;\n for i in 0..len - 1 {\n let mut count = 0;\n for m in i..len {\n if string[m] == 'a' {\n count += 1;\n } else if string[m] == 'b' {\n count -= 1;\n }\n if count == 0 {\n end = m as i32 + 1;\n start = i as i32 + 1;\n break;\n }\n }\n if count == 0 {\n break;\n }\n }\n println!(\"{} {}\", start, end);\n }\n}", "difficulty": "medium"} {"problem_id": "1022", "problem_description": "This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.The main element of this machine are $$$n$$$ rods arranged along one straight line and numbered from $$$1$$$ to $$$n$$$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $$$1$$$ or $$$-1$$$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.More formally, the rods can be represented as an array of $$$n$$$ numbers characterizing the charge: either $$$1$$$ or $$$-1$$$. Then the condition must hold: $$$a_1 - a_2 + a_3 - a_4 + \\ldots = 0$$$, or $$$\\sum\\limits_{i=1}^n (-1)^{i-1} \\cdot a_i = 0$$$.Sparky charged all $$$n$$$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $$$q$$$ questions. In the $$$i$$$th question Sparky asks: if the machine consisted only of rods with numbers $$$l_i$$$ to $$$r_i$$$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.Help your friends and answer all of Sparky's questions!", "c_code": "int solution() {\n int t;\n int n;\n int q;\n int l;\n int r;\n int pre[300001];\n int d;\n *pre = 0;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d %d\\n\", &n, &q);\n for (int i = 0; i < n; i++) {\n d = getc(stdin);\n pre[i + 1] = pre[i] + ((d == '+') ^ (i & 1)) * 2 - 1;\n }\n while (q--) {\n scanf(\"%d %d\", &l, &r);\n d = pre[r] - pre[l - 1];\n if (d == 0) {\n puts(\"0\");\n } else if (d & 1) {\n puts(\"1\");\n } else {\n puts(\"2\");\n }\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 let t: usize = line.trim().parse().unwrap();\n line.clear();\n let mut subsums: Vec = Vec::new();\n let mut s_buf = String::new();\n let mut out_buf = String::new();\n for _ in 0..t {\n io::stdin().read_line(&mut line).unwrap();\n let mut it = line\n .split_ascii_whitespace()\n .map(|i| i.parse::().unwrap());\n let n = it.next().unwrap();\n let q = it.next().unwrap();\n line.clear();\n\n s_buf.clear();\n io::stdin().read_line(&mut s_buf).unwrap();\n let s = s_buf.as_bytes();\n\n subsums.clear();\n subsums.push(0);\n let mut sign: isize = 1;\n let mut sum: isize = 0;\n for i in 0..n {\n sum += sign * ((s[i] as isize) - 44);\n sign = -sign;\n subsums.push(sum);\n }\n out_buf.clear();\n for _ in 0..q {\n io::stdin().read_line(&mut line).unwrap();\n let mut it = line\n .split_ascii_whitespace()\n .map(|i| i.parse::().unwrap());\n let l = it.next().unwrap();\n let r = it.next().unwrap();\n line.clear();\n if subsums[r] == subsums[l - 1] {\n out_buf += \"0\\n\";\n } else if (r - l + 1) % 2 == 0 {\n out_buf += \"2\\n\";\n } else {\n out_buf += \"1\\n\";\n }\n }\n print!(\"{}\", out_buf);\n }\n}", "difficulty": "medium"} {"problem_id": "1023", "problem_description": "Selection sort", "c_code": "void solution(int *arr, int size) {\n for (int i = 0; i < size - 1; i++) {\n int min_index = i;\n\n for (int j = i + 1; j < size; j++) {\n if (arr[min_index] > arr[j]) {\n min_index = j;\n }\n }\n\n if (min_index != i) {\n int temp = arr[i];\n arr[i] = arr[min_index];\n arr[min_index] = temp;\n }\n }\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n let len = arr.len();\n for left in 0..len {\n let mut smallest = left;\n for right in (left + 1)..len {\n if arr[right] < arr[smallest] {\n smallest = right;\n }\n }\n arr.swap(smallest, left);\n }\n}\n", "difficulty": "easy"} {"problem_id": "1024", "problem_description": "\n\n\n

Watch

\n\n

\nWrite a program which reads an integer $S$ [second] and converts it to $h:m:s$ where $h$, $m$, $s$ denote hours, minutes (less than 60) and seconds (less than 60) respectively.\n

\n\n\n

Input

\n\n

\nAn integer $S$ is given in a line.\n

\n\n

Output

\n\n

\nPrint $h$, $m$ and $s$ separated by ':'. You do not need to put '0' for a value, which consists of a digit.\n

\n\n

Constraints

\n\n
    \n
  • $0 \\leq S \\leq 86400$
  • \n
\n\n

Sample Input 1

\n
\n46979\n
\n

Sample Output 1

\n
\n13:2:59\n
", "c_code": "int solution(void) {\n int S = 0;\n scanf(\"%d\", &S);\n int h = S / 3600;\n int m = (S - h * 3600) / 60;\n int s = S - (h * 60 * 60) - (m * 60);\n printf(\"%d:%d:%d\\n\", h, m, s);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let S = buf.trim().parse::().unwrap();\n println!(\"{}:{}:{}\", S / 3600, S % 3600 / 60, S % 60);\n}", "difficulty": "easy"} {"problem_id": "1025", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given three integers A, B and C.\nDetermine whether C is not less than A and not greater than B.

\n
\n
\n
\n
\n

Constraints

    \n
  • -100≤A,B,C≤100
  • \n
  • A, B and C are all integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

If the condition is satisfied, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 3 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

C=2 is not less than A=1 and not greater than B=3, and thus the output should be Yes.

\n
\n
\n
\n
\n
\n

Sample Input 2

6 5 4\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

C=4 is less than A=6, and thus the output should be No.

\n
\n
\n
\n
\n
\n

Sample Input 3

2 2 2\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\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 (c >= a && c <= b) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut b = String::new();\n let _ = std::io::stdin().read_line(&mut b);\n let c = b\n .split_whitespace()\n .map(|i| i.parse().unwrap())\n .collect::>();\n if c[0] <= c[2] && c[2] <= c[1] {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "1026", "problem_description": "The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries.", "c_code": "int solution(void) {\n int n;\n int q;\n int *a = calloc(1000002, sizeof(*a));\n int *b = calloc(1000002, sizeof(*a));\n int *temp = calloc(1000002, sizeof(*temp));\n scanf(\"%d\", &q);\n for (int j = 0; j < q; j++) {\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int k = 1;\n while (k != n + 1) {\n if (a[k]) {\n int tmp = a[k];\n int tmp2 = tmp;\n int counter = 0;\n int mas_counter = 0;\n do {\n tmp2 = a[tmp2];\n counter++;\n temp[mas_counter++] = tmp2;\n } while (tmp != tmp2);\n for (int f = 0; f < mas_counter; f++) {\n b[temp[f]] = counter;\n a[temp[f]] = 0;\n }\n }\n k++;\n }\n for (int l = 1; l <= n; l++) {\n printf(\"%d \", b[l]);\n }\n printf(\"\\n\");\n }\n free(a);\n free(b);\n free(temp);\n return 0;\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 queries: i16 = line.trim().parse().unwrap();\n line.clear();\n while queries > 0 {\n input.read_line(&mut line).unwrap();\n let total = line.trim().parse().unwrap();\n line.clear();\n input.read_line(&mut line).unwrap();\n let children = line.split_whitespace();\n let mut permutation = Vec::with_capacity(total);\n for child in children {\n permutation.push(child.parse::().unwrap() - 1);\n }\n line.clear();\n let mut loop_items = Vec::new();\n let mut books = vec![0; total];\n for book in 0..books.len() {\n if books[book] != 0 {\n continue;\n }\n let mut cur = book;\n loop {\n loop_items.push(cur);\n cur = permutation[cur];\n if cur == book {\n break;\n }\n }\n let length = loop_items.len();\n for item in 0..loop_items.len() {\n books[loop_items[item]] = length;\n }\n loop_items.clear();\n }\n loop_items.shrink_to_fit();\n for book in books {\n print!(\"{} \", book);\n }\n println!();\n queries -= 1;\n }\n}", "difficulty": "hard"} {"problem_id": "1027", "problem_description": "You're given an array $$$a$$$ of length $$$n$$$. You can perform the following operations on it: choose an index $$$i$$$ $$$(1 \\le i \\le n)$$$, an integer $$$x$$$ $$$(0 \\le x \\le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j+x$$$ for all $$$(1 \\le j \\le i)$$$, which means add $$$x$$$ to all the elements in the prefix ending at $$$i$$$. choose an index $$$i$$$ $$$(1 \\le i \\le n)$$$, an integer $$$x$$$ $$$(1 \\le x \\le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j \\% x$$$ for all $$$(1 \\le j \\le i)$$$, which means replace every element in the prefix ending at $$$i$$$ with the remainder after dividing it by $$$x$$$. Can you make the array strictly increasing in no more than $$$n+1$$$ operations?", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int ar[n + 1];\n ar[0] = 0;\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &ar[i]);\n }\n printf(\"%d\\n\", n + 1);\n printf(\"1 %d %d\\n\", n, (2 * n) + 1);\n for (int i = 1; i <= n; i++) {\n printf(\"2 %d %d\\n\", i, ar[i] + (2 * n) + 1 - i);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut l1 = String::new();\n stdin().read_line(&mut l1);\n let n: usize = l1.trim().parse().unwrap();\n\n let mut l2 = String::new();\n stdin().read_line(&mut l2);\n let l2: Vec<&str> = l2.trim().split(\" \").collect();\n let mut v: Vec = Vec::new();\n v.push(0);\n for x in l2 {\n v.push(x.parse().unwrap());\n }\n println!(\"{}\", n + 1);\n let mut add = 0;\n for i in (1..n + 1).rev() {\n let tmp = (n + i - 1) - ((v[i] + add) % n);\n add += tmp;\n println!(\"1 {} {}\", i, tmp);\n }\n println!(\"2 {} {}\", n, n);\n}", "difficulty": "medium"} {"problem_id": "1028", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads.\nThe following are known about the road network:

\n
    \n
  • People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.
  • \n
  • Different roads may have had different lengths, but all the lengths were positive integers.
  • \n
\n

Snuke the archeologist found a table with N rows and N columns, A, in the ruin of Takahashi Kingdom.\nHe thought that it represented the shortest distances between the cities along the roads in the kingdom.

\n

Determine whether there exists a road network such that for each u and v, the integer A_{u, v} at the u-th row and v-th column of A is equal to the length of the shortest path from City u to City v.\nIf such a network exist, find the shortest possible total length of the roads.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 300
  • \n
  • If i ≠ j, 1 \\leq A_{i, j} = A_{j, i} \\leq 10^9.
  • \n
  • A_{i, i} = 0
  • \n
\n
\n
\n
\n
\n
\n
\n

Inputs

Input is given from Standard Input in the following format:

\n
N\nA_{1, 1} A_{1, 2} ... A_{1, N}\nA_{2, 1} A_{2, 2} ... A_{2, N}\n...\nA_{N, 1} A_{N, 2} ... A_{N, N}\n
\n
\n
\n
\n
\n

Outputs

If there exists no network that satisfies the condition, print -1.\nIf it exists, print the shortest possible total length of the roads.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n0 1 3\n1 0 2\n3 2 0\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The network below satisfies the condition:

\n
    \n
  • City 1 and City 2 is connected by a road of length 1.
  • \n
  • City 2 and City 3 is connected by a road of length 2.
  • \n
  • City 3 and City 1 is not connected by a road.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3\n0 1 3\n1 0 1\n3 1 0\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

As there is a path of length 1 from City 1 to City 2 and City 2 to City 3, there is a path of length 2 from City 1 to City 3.\nHowever, according to the table, the shortest distance between City 1 and City 3 must be 3.

\n

Thus, we conclude that there exists no network that satisfies the condition.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n0 21 18 11 28\n21 0 13 10 26\n18 13 0 23 13\n11 10 23 0 17\n28 26 13 17 0\n
\n
\n
\n
\n
\n

Sample Output 3

82\n
\n
\n
\n
\n
\n
\n

Sample Input 4

3\n0 1000000000 1000000000\n1000000000 0 1000000000\n1000000000 1000000000 0\n
\n
\n
\n
\n
\n

Sample Output 4

3000000000\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n long long a[n][n];\n long long b[n][n];\n long long c[n][n];\n long long ans = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n scanf(\"%lld\", &a[i][j]);\n b[i][j] = a[i][j];\n c[i][j] = 1;\n }\n }\n for (int k = 0; k < n; k++) {\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (b[i][j] > b[i][k] + b[k][j]) {\n b[i][j] = b[i][k] + b[k][j];\n }\n }\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (a[i][j] != b[i][j]) {\n printf(\"-1\\n\");\n return 0;\n }\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < n; k++) {\n if (i != j && i != k && j != k) {\n if (a[i][j] == a[i][k] + a[k][j]) {\n c[i][j] = 0;\n }\n }\n }\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (c[i][j] == 1) {\n ans += a[i][j];\n }\n }\n }\n printf(\"%lld\\n\", ans / 2);\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 mut g = vec![vec![0; n]; n];\n\n for i in 0..n {\n for j in 0..n {\n g[i][j] = itr.next().unwrap().parse().unwrap();\n }\n }\n let mut ok = true;\n let mut need = vec![vec![true; n]; n];\n for k in 0..n {\n for i in 0..n {\n for j in 0..n {\n if i == j || j == k || k == i {\n continue;\n }\n if g[i][j] > g[i][k] + g[k][j] {\n ok = false;\n }\n if g[i][j] == g[i][k] + g[k][j] {\n need[i][j] = false;\n }\n }\n }\n }\n if ok {\n let mut ans = 0i64;\n for i in 0..n {\n for j in 0..i {\n ans += if need[i][j] { g[i][j] } else { 0 };\n }\n }\n println!(\"{}\", ans);\n } else {\n println!(\"-1\");\n }\n}", "difficulty": "easy"} {"problem_id": "1029", "problem_description": "Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int arrx[n];\n int arry[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d %d\", &arrx[i], &arry[i]);\n }\n int minx = 1000000000;\n int miny = 1000000000;\n int maxx = -1000000000;\n int maxy = -1000000000;\n for (int i = 0; i < n; i++) {\n if (arrx[i] > maxx) {\n maxx = arrx[i];\n }\n if (arrx[i] < minx) {\n minx = arrx[i];\n }\n if (arry[i] > maxy) {\n maxy = arry[i];\n }\n if (arry[i] < miny) {\n miny = arry[i];\n }\n }\n long long a = maxx - minx;\n long long b = maxy - miny;\n if (a > b) {\n printf(\"%lld\\n\", a * a);\n } else {\n printf(\"%lld\\n\", b * b);\n }\n}", "rust_code": "fn solution() {\n let mut input_text = String::new();\n io::stdin()\n .read_line(&mut input_text)\n .expect(\"failed to read from stdin\");\n\n let n: usize = input_text.trim().parse().unwrap();\n let mut min_x: i32 = 0x3f3f3f3f;\n let mut min_y: i32 = 0x3f3f3f3f;\n let mut max_x: i32 = -0x3f3f3f3f;\n let mut max_y: i32 = -0x3f3f3f3f;\n\n for _ in 0..n {\n input_text = String::new();\n io::stdin()\n .read_line(&mut input_text)\n .expect(\"failed to read from stdin\");\n\n let inputs: Vec = input_text\n .trim()\n .split(\" \")\n .map(|x| x.parse().expect(\"Not an integer!\"))\n .collect();\n\n let x = inputs[0];\n let y = inputs[1];\n\n min_x = std::cmp::min(min_x, x);\n min_y = std::cmp::min(min_y, y);\n max_x = std::cmp::max(max_x, x);\n max_y = std::cmp::max(max_y, y);\n }\n let d: i32 = std::cmp::max(max_x - min_x, max_y - min_y);\n println!(\"{}\", (d as i64) * (d as i64));\n}", "difficulty": "medium"} {"problem_id": "1030", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.\nIn his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

\n

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).\nNote that he cannot stay at his place.\nDetermine whether he can carry out his plan.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 N 10^5
  • \n
  • 0 x_i 10^5
  • \n
  • 0 y_i 10^5
  • \n
  • 1 t_i 10^5
  • \n
  • t_i < t_{i+1} (1 i N-1)
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nt_1 x_1 y_1\nt_2 x_2 y_2\n:\nt_N x_N y_N\n
\n
\n
\n
\n
\n

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n3 1 2\n6 1 1\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n2 100 100\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

It is impossible to be at (100,100) two seconds after being at (0,0).

\n
\n
\n
\n
\n
\n

Sample Input 3

2\n5 1 1\n100 1 1\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution(void) {\n int x = 0;\n int y = 1;\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int e = 0;\n int f = 0;\n int g = 0;\n int h = 0;\n int i = 0;\n scanf(\"%d\", &x);\n while (x >= y) {\n scanf(\"%d %d %d\", &a, &b, &c);\n g = abs(b - e);\n h = abs(c - f);\n if (a - d < g) {\n puts(\"No\");\n return 0;\n }\n i = a - d - g;\n if (i < h) {\n puts(\"No\");\n return 0;\n }\n if ((i - h) % 2) {\n puts(\"No\");\n return 0;\n } else {\n }\n\n y++;\n d = a;\n e = b;\n f = c;\n }\n puts(\"Yes\");\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: usize = s.trim().parse().unwrap();\n\n let mut v: Vec = vec![0; 3];\n for _ in 0..n {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let u: Vec = s.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n let t = u[0] - v[0];\n let d = (u[1] - v[1] + u[2] - v[2]).abs();\n if t < d || (t - d) % 2 == 1 {\n println!(\"No\");\n return;\n }\n v = u;\n }\n println!(\"Yes\");\n}", "difficulty": "easy"} {"problem_id": "1031", "problem_description": "

A: A-Z-

\n\n

問題

\n

26 マスの円環状のボードがあり、各マスには大文字のアルファベット 1 文字が、アルファベット順に時計回りに書かれています。すなわち、 'A' のマスの時計回り隣は 'B' のマスで、 'B' のマスの隣は 'C' のマスで、・・・、 'Z' のマスの時計回り隣は 'A' のマスです。

\n\n\n\n

また、ボードには 'A' のマスに駒が 1 つ置かれています。

\n\n

\nあなたは、文字列 S を受け取り、 S の先頭から 1 文字ずつ見て駒を操作します。 i 回目の操作は以下のようになります。\n

\n
    \n
  • その時点で駒のあるマスから Si 文字目のアルファベットのマスを目指して、駒を時計回りに 1 マスずつ移動させる。このとき少なくとも 1 マスは移動するとする。したがって、例えば 'A' のマスから 'A' のマスに移動する際は、ボードを 1 周しなくてはならない。
  • \n
\n\n

上記の操作の結果、駒が 'A' のマスを何回踏んだかを答えてください。なお「 'A' のマスを踏む」とは、 'Z' のマスから 'A' のマスに駒を進めることを言います。

\n\n

入力形式

\n

入力は 1 行で与えられる。

\n\n
S
\n\n

S はあなたが受け取る文字列を表す。

\n\n\n

制約

\n
    \n
  • 1 \\leq |S| \\leq 100
  • \n
  • S は英大文字のみで構成される。
  • \n
\n\n

出力形式

\n

'A' のマスを何回踏んだかを 1 行で出力せよ。

\n\n

入力例1

\n
AIZU
\n\n

出力例1

\n
2
\n\n
    \n
  • A -> A (ここで 1 回)
  • \n
  • A -> I (ここまで 1 回)
  • \n
  • I -> Z (ここまで 1 回)
  • \n
  • Z -> U (ここまで 2 回)
  • \n
\n\n

入力例2

\n
HOKKAIDO
\n\n

出力例2

\n
4
", "c_code": "int solution() {\n int i;\n int ans = 0;\n char *S = (char *)malloc(sizeof(char) * 102);\n S[0] = 'A';\n scanf(\"%s\", &S[1]);\n for (i = 1; S[i] != '\\0'; i++) {\n if (S[i] <= S[i - 1]) {\n ans++;\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n input!(S:chars);\n let S: Vec = S;\n let mut ans = 0;\n let mut now: char = 'A';\n for i in 0..S.len() {\n if S[i] <= now {\n ans += 1;\n }\n now = S[i];\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1032", "problem_description": "You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i > a_{i - 1}$$$ and $$$a_i > a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int cnt = 0;\n for (int j = 1; j < n - 1; j++) {\n if (a[j] > a[j - 1] && a[j] > a[j + 1]) {\n if (j + 2 < n && a[j + 2] > a[j]) {\n a[j + 1] = a[j + 2];\n } else {\n a[j + 1] = a[j];\n }\n cnt++;\n }\n }\n printf(\"%d\\n\", cnt);\n for (int k = 0; k < n; k++) {\n printf(\"%d \", a[k]);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input);\n let input: i32 = input.trim().parse().unwrap();\n\n for i in 0..input {\n let mut input = String::new();\n io::stdin().read_line(&mut input);\n let non: i32 = input.trim().parse().unwrap();\n\n let mut input = String::new();\n io::stdin().read_line(&mut input);\n let mut vector: Vec = input\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut p: usize = 1;\n let mut count = 0;\n loop {\n if p as i32 > non - 1 - 1 {\n break;\n }\n\n if vector[p - 1] < vector[p] && vector[p] > vector[p + 1] {\n if p as i32 <= non - 3 - 1 {\n if vector[p + 1] < vector[p + 2] && vector[p + 2] > vector[p + 3] {\n vector[p + 1] = max(vector[p], vector[p + 2]);\n count += 1;\n p += 1;\n } else {\n vector[p + 1] = vector[p];\n count += 1;\n p += 1;\n }\n } else {\n vector[p + 1] = vector[p];\n count += 1;\n p += 1;\n }\n } else {\n p += 1;\n }\n }\n\n println!(\"{}\", count);\n vector.iter().for_each(|x| print!(\"{} \", x));\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1033", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

\n

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

\n

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n

Output

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

\n
\n
\n
\n
\n
\n

Sample Input 1

3\n7 6 8\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n12 15 18\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2\n1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

1000000000\n
\n

We can replace an integer with itself.

\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n long long int a[n];\n long long int min1 = 10000000000;\n long long int min2 = 10000000000;\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n if (min2 > a[i]) {\n min2 = a[i];\n } else if (min1 > a[i]) {\n min1 = a[i];\n }\n }\n for (long long int i = min1;; i--) {\n int cnt = 0;\n for (int j = 0; j < n; j++) {\n if (a[j] % i > 0) {\n cnt++;\n }\n if (cnt > 1) {\n break;\n }\n }\n if (cnt < 2) {\n printf(\"%lld\\n\", i);\n return 0;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n a: [i64; n],\n }\n\n let f = |i| a.iter().filter(|&&a| a % i == 0).count();\n\n let mut ans = 1;\n for &a in a[0..2].iter() {\n for i in 1.. {\n if i * i > a {\n break;\n }\n if a % i == 0 {\n if f(i) >= n - 1 {\n ans = max(ans, i);\n }\n if f(a / i) >= n - 1 {\n ans = max(ans, a / i);\n }\n }\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1034", "problem_description": "You have been blessed as a child of Omkar. To express your gratitude, please solve this problem for Omkar!An array $$$a$$$ of length $$$n$$$ is called complete if all elements are positive and don't exceed $$$1000$$$, and for all indices $$$x$$$,$$$y$$$,$$$z$$$ ($$$1 \\leq x,y,z \\leq n$$$), $$$a_{x}+a_{y} \\neq a_{z}$$$ (not necessarily distinct).You are given one integer $$$n$$$. Please find any complete array of length $$$n$$$. It is guaranteed that under given constraints such array exists.", "c_code": "int solution() {\n int n = 0;\n int t;\n scanf(\"%d\", &t);\n while (t != 0) {\n if (n == 0) {\n scanf(\"%d\", &n);\n }\n if (n > 1) {\n printf(\"1 \");\n n--;\n }\n if (n == 1) {\n printf(\"1 \\n\");\n n--;\n t--;\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 for _ in 0..n {\n print!(\"1 \");\n }\n println!();\n }\n}", "difficulty": "easy"} {"problem_id": "1035", "problem_description": "\n

Score: 500 points

\n
\n
\n

Problem Statement

\n

N people are standing in a queue, numbered 1, 2, 3, ..., N from front to back. Each person wears a hat, which is red, blue, or green.

\n

The person numbered i says:

\n
    \n
  • \"In front of me, exactly A_i people are wearing hats with the same color as mine.\"
  • \n
\n

Assuming that all these statements are correct, find the number of possible combinations of colors of the N people's hats.

\n

Since the count can be enormous, compute it modulo 1000000007.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 100000
  • \n
  • 0 \\leq A_i \\leq N-1
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 A_3 ... A_N\n
\n
\n
\n
\n
\n

Output

\n

Print the number of possible combinations of colors of the N people's hats, modulo 1000000007.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n0 1 2 3 4 5\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

We have three possible combinations, as follows:

\n
    \n
  • Red, Red, Red, Red, Red, Red
  • \n
  • Blue, Blue, Blue, Blue, Blue, Blue
  • \n
  • Green, Green, Green, Green, Green, Green
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3\n0 0 0\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
\n
\n
\n
\n

Sample Input 3

54\n0 0 1 0 1 2 1 2 3 2 3 3 4 4 5 4 6 5 7 8 5 6 6 7 7 8 8 9 9 10 10 11 9 12 10 13 14 11 11 12 12 13 13 14 14 15 15 15 16 16 16 17 17 17\n
\n
\n
\n
\n
\n

Sample Output 3

115295190\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int i;\n int a[100005];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n long long int p = 1000000007;\n long long int dp[100005];\n long long int count[100005];\n for (i = 0; i < n; i++) {\n dp[i] = 0;\n }\n for (i = 0; i < n; i++) {\n count[i] = 0;\n }\n for (i = 0; i < n; i++) {\n if (a[i] == 0) {\n dp[i] = 3 - count[0];\n } else {\n dp[i] = count[a[i] - 1] - count[a[i]];\n }\n count[a[i]]++;\n }\n long long int ans = 1;\n for (i = 0; i < n; i++) {\n ans = ans * dp[i] % p;\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let n: usize = s.trim().parse().ok().unwrap();\n\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).ok();\n let a: Vec = t\n .split_whitespace()\n .map(|e| e.parse().ok().unwrap())\n .collect();\n\n let mut ans: u64 = 1;\n let mut cnt = vec![0; n + 1];\n let modulo: u64 = 1000000007;\n\n cnt[0] = 3;\n for i in 0..n {\n ans = ans * cnt[a[i]] % modulo;\n cnt[a[i] + 1] += 1;\n cnt[a[i]] -= 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1036", "problem_description": "Given an array of integer $$$a_1, a_2, \\ldots, a_n$$$. In one operation you can make $$$a_i := a_i + 1$$$ if $$$i < n$$$ and $$$a_i \\leq a_{i + 1}$$$, or $$$i = n$$$ and $$$a_i \\leq a_1$$$.You need to check whether the array $$$a_1, a_2, \\ldots, a_n$$$ can become equal to the array $$$b_1, b_2, \\ldots, b_n$$$ in some number of operations (possibly, zero). Two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$ are called equal if $$$a_i = b_i$$$ for all integers $$$i$$$ from $$$1$$$ to $$$n$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n for (int j = 0; j < t; j++) {\n scanf(\"%d\", &n);\n long long b[n];\n long long a[n];\n int flag = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &b[i]);\n if (a[i] > b[i]) {\n flag = 1;\n }\n if (i != 0) {\n if (b[i] - b[i - 1] < -1 && a[i - 1] != b[i - 1]) {\n\n flag = 1;\n }\n }\n }\n if (b[0] - b[n - 1] < -1 && a[n - 1] != b[n - 1]) {\n flag = 1;\n }\n\n if (flag == 1) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let std_in = stdin();\n let mut input = Scanner::new(std_in.lock());\n\n let std_out = stdout();\n let mut output = BufWriter::new(std_out.lock());\n\n let t: usize = input.token();\n for _ in 0..t {\n let n: usize = input.token();\n let a: Vec = (0..n).map(|_| input.token()).collect();\n let b: Vec = (0..n).map(|_| input.token()).collect();\n\n let mut result = true;\n for i in 0..n {\n let next_i = if i == n - 1 { 0 } else { i + 1 };\n if a[i] > b[i] {\n result = false;\n break;\n }\n if b[i] != a[i] && b[i] - 1 > b[next_i] {\n result = false;\n break;\n }\n }\n writeln!(output, \"{}\", if result { \"YES\" } else { \"NO\" }).unwrap();\n }\n}", "difficulty": "easy"} {"problem_id": "1037", "problem_description": "Soon after the Chunga-Changa island was discovered, it started to acquire some forms of civilization and even market economy. A new currency arose, colloquially called \"chizhik\". One has to pay in chizhiks to buy a coconut now.Sasha and Masha are about to buy some coconuts which are sold at price $$$z$$$ chizhiks per coconut. Sasha has $$$x$$$ chizhiks, Masha has $$$y$$$ chizhiks. Each girl will buy as many coconuts as she can using only her money. This way each girl will buy an integer non-negative number of coconuts.The girls discussed their plans and found that the total number of coconuts they buy can increase (or decrease) if one of them gives several chizhiks to the other girl. The chizhiks can't be split in parts, so the girls can only exchange with integer number of chizhiks.Consider the following example. Suppose Sasha has $$$5$$$ chizhiks, Masha has $$$4$$$ chizhiks, and the price for one coconut be $$$3$$$ chizhiks. If the girls don't exchange with chizhiks, they will buy $$$1 + 1 = 2$$$ coconuts. However, if, for example, Masha gives Sasha one chizhik, then Sasha will have $$$6$$$ chizhiks, Masha will have $$$3$$$ chizhiks, and the girls will buy $$$2 + 1 = 3$$$ coconuts. It is not that easy to live on the island now, so Sasha and Mash want to exchange with chizhiks in such a way that they will buy the maximum possible number of coconuts. Nobody wants to have a debt, so among all possible ways to buy the maximum possible number of coconuts find such a way that minimizes the number of chizhiks one girl gives to the other (it is not important who will be the person giving the chizhiks).", "c_code": "int solution(void) {\n long long x;\n long long y;\n long long z;\n scanf(\"%lld %lld %lld\", &x, &y, &z);\n if (z > 0) {\n long long a;\n long long b;\n long long c;\n long long d;\n long long e;\n long long f;\n long long m;\n long long mn;\n a = x % z;\n d = x / z;\n b = y % z;\n e = y / z;\n c = (x + y) % z;\n f = (x + y) / z;\n if (a > b) {\n m = z - a;\n if (m > b) {\n mn = 0;\n } else {\n mn = m;\n }\n } else {\n m = z - b;\n if (m > a) {\n mn = 0;\n } else {\n mn = m;\n }\n }\n printf(\"%lld %lld\", f, mn);\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\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let x = *arr.get(0).unwrap();\n let y = *arr.get(1).unwrap();\n let z = *arr.get(2).unwrap();\n\n let total = x + y;\n\n let max_poss = total / z;\n let a = x % z;\n let b = y % z;\n\n let min_debt = if b + a >= z { z - cmp::max(a, b) } else { 0 };\n\n println!(\"{} {}\", max_poss, min_debt);\n}", "difficulty": "medium"} {"problem_id": "1038", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

N of us are going on a trip, by train or taxi.

\n

The train will cost each of us A yen (the currency of Japan).

\n

The taxi will cost us a total of B yen.

\n

How much is our minimum total travel expense?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 20
  • \n
  • 1 \\leq A \\leq 50
  • \n
  • 1 \\leq B \\leq 50
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B\n
\n
\n
\n
\n
\n

Output

Print an integer representing the minimum total travel expense.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 2 9\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

The train will cost us 4 \\times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 2 7\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n
\n
\n
\n
\n
\n

Sample Input 3

4 2 8\n
\n
\n
\n
\n
\n

Sample Output 3

8\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int n = 0;\n\n scanf(\"%d %d %d\", &n, &a, &b);\n if (n * a >= b) {\n printf(\"%d\", b);\n } else {\n printf(\"%d\", n * a);\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n let _ = std::io::stdin().read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n let n: i32 = vec[0].parse().unwrap();\n let a: i32 = vec[1].parse().unwrap();\n let b: i32 = vec[2].parse().unwrap();\n\n println!(\"{}\", std::cmp::min(n * a, b));\n}", "difficulty": "medium"} {"problem_id": "1039", "problem_description": "A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).For a positive integer $$$n$$$, we call a permutation $$$p$$$ of length $$$n$$$ good if the following condition holds for every pair $$$i$$$ and $$$j$$$ ($$$1 \\le i \\le j \\le n$$$) — $$$(p_i \\text{ OR } p_{i+1} \\text{ OR } \\ldots \\text{ OR } p_{j-1} \\text{ OR } p_{j}) \\ge j-i+1$$$, where $$$\\text{OR}$$$ denotes the bitwise OR operation. In other words, a permutation $$$p$$$ is good if for every subarray of $$$p$$$, the $$$\\text{OR}$$$ of all elements in it is not less than the number of elements in that subarray. Given a positive integer $$$n$$$, output any good permutation of length $$$n$$$. We can show that for the given constraints such a permutation always exists.", "c_code": "int solution(void) {\n int linenum;\n\n scanf(\"%d\", &linenum);\n\n int len[linenum];\n\n for (int i = 0; i < linenum; i++) {\n scanf(\"\\n%d\", &len[i]);\n }\n\n for (int i = 0; i < linenum; i++) {\n for (int j = 0; j < len[i]; j++) {\n printf(\"%d \", j + 1);\n }\n printf(\"\\n\");\n }\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 println!(\n \"{}\",\n (1..=n).map(|x| x.to_string()).collect::>().join(\" \")\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1040", "problem_description": "Let's call a binary string $$$T$$$ of length $$$m$$$ indexed from $$$1$$$ to $$$m$$$ paranoid if we can obtain a string of length $$$1$$$ by performing the following two kinds of operations $$$m-1$$$ times in any order : Select any substring of $$$T$$$ that is equal to 01, and then replace it with 1. Select any substring of $$$T$$$ that is equal to 10, and then replace it with 0.For example, if $$$T = $$$ 001, we can select the substring $$$[T_2T_3]$$$ and perform the first operation. So we obtain $$$T = $$$ 01.You are given a binary string $$$S$$$ of length $$$n$$$ indexed from $$$1$$$ to $$$n$$$. Find the number of pairs of integers $$$(l, r)$$$ $$$1 \\le l \\le r \\le n$$$ such that $$$S[l \\ldots r]$$$ (the substring of $$$S$$$ from $$$l$$$ to $$$r$$$) is a paranoid string.", "c_code": "int solution() {\n int t;\n int n;\n char s[200001];\n\n scanf(\"%d\", &t);\n for (int i = 1; i <= t; i++) {\n scanf(\"%d\", &n);\n\n scanf(\"%s\", s);\n\n long long int count = n;\n for (int k = 1; k < n; k++) {\n if (s[k] == '1' && s[k - 1] == '0') {\n count = count + k;\n }\n if (s[k] == '0' && s[k - 1] == '1') {\n count = count + k;\n }\n }\n printf(\"%lli\\n\", count);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin().read_to_string(&mut content);\n\n let mut lines = content.lines();\n let num_tests: i32 = lines.next().unwrap().parse().unwrap();\n for _ in 0..num_tests {\n let _n: i32 = lines.next().unwrap().parse().unwrap();\n let s: Vec = lines.next().unwrap().chars().collect();\n let mut result = s.len() as i64;\n for i in 1..s.len() {\n if s[i - 1] != s[i] {\n result += i as i64;\n }\n }\n println!(\"{result}\");\n }\n}", "difficulty": "hard"} {"problem_id": "1041", "problem_description": "You have $$$r$$$ red and $$$b$$$ blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: has at least one red bean (or the number of red beans $$$r_i \\ge 1$$$); has at least one blue bean (or the number of blue beans $$$b_i \\ge 1$$$); the number of red and blue beans should differ in no more than $$$d$$$ (or $$$|r_i - b_i| \\le d$$$) Can you distribute all beans?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int r;\n int b;\n int d;\n scanf(\"%d%d%d\", &r, &b, &d);\n if ((r == b && d >= 0) || (r == 1 && b > 1 && d >= b - r) ||\n (b == 1 && r > 1 && d >= r - b)) {\n printf(\"YES\\n\");\n } else if (r > 1 && b > 1 && b > r) {\n if ((b % r == 0 && d >= b / r - 1) ||\n (b / r != 0 && d >= (b / r - 1) && d >= (b / r))) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n } else if (r > 1 && b > 1 && b < r) {\n if ((r % b == 0 && d >= r / b - 1) ||\n (r / b != 0 && d >= (r / b - 1) && d >= (r / b))) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buff = String::new();\n io::stdin().read_line(&mut buff).unwrap();\n let t = buff.trim().parse::().unwrap();\n for _ in 0..t {\n buff = String::new();\n io::stdin().read_line(&mut buff).unwrap();\n let v = buff\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n let mut r = v[0];\n let mut b = v[1];\n if b > r {\n mem::swap(&mut r, &mut b);\n }\n let mut flag = false;\n if r % b == 0 {\n let a = r / b;\n let dif = a - 1;\n if dif <= v[2] {\n flag = true;\n }\n } else {\n let a = (r / b) + 1;\n let dif = a - 1;\n if dif <= v[2] {\n flag = true;\n }\n }\n if flag {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1042", "problem_description": "You are given two integers $$$x$$$ and $$$y$$$ (it is guaranteed that $$$x > y$$$). You may choose any prime integer $$$p$$$ and subtract it any number of times from $$$x$$$. Is it possible to make $$$x$$$ equal to $$$y$$$?Recall that a prime number is a positive integer that has exactly two positive divisors: $$$1$$$ and this integer itself. The sequence of prime numbers starts with $$$2$$$, $$$3$$$, $$$5$$$, $$$7$$$, $$$11$$$.Your program should solve $$$t$$$ independent test cases.", "c_code": "int solution() {\n long long n;\n long long m;\n scanf(\"%lld\", &n);\n while (scanf(\"%lld%lld\", &n, &m) == 2) {\n printf(n - m == 1 ? \"NO\\n\" : \"YES\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let t = s.trim().parse::().unwrap();\n\n for _ in 0..t {\n s.clear();\n std::io::stdin().read_line(&mut s).unwrap();\n let vec = s\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n if vec[0] - vec[1] == 1 {\n println!(\"No\");\n } else {\n println!(\"Yes\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1043", "problem_description": "For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$\"1110011110\", the following substrings would be written: \"11\", \"11\", \"10\", \"00\", \"01\", \"11\", \"11\", \"11\", \"10\". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.", "c_code": "int solution() {\n int t;\n int i;\n int a;\n int b;\n int c;\n int j;\n scanf(\"%d\", &t);\n while (t--) {\n j = 1;\n scanf(\"%d%d%d\", &a, &b, &c);\n for (i = 0; i <= a && a != 0; i++) {\n printf(\"0\");\n }\n if (a == 0 && b != 0) {\n printf(\"0\");\n }\n for (i = 0; i < b - 1; i++) {\n if (i == b - 2 && j == 1) {\n continue;\n }\n printf(\"%d\", j);\n j = !j;\n }\n if (c == 0 && b != 0) {\n printf(\"1\");\n }\n for (i = 0; i <= c && c != 0; i++) {\n printf(\"1\");\n }\n if (b > 0 && b % 2 == 0) {\n printf(\"0\");\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input)?;\n let n = input.trim().parse().unwrap();\n for _ in 0..n {\n input.clear();\n io::stdin().read_line(&mut input)?;\n\n let numbers: Vec = input\n .trim()\n .split(\" \")\n .map(|x| x.parse().unwrap())\n .take(3)\n .collect();\n let (n0, mut n1, n2) = (numbers[0], numbers[1], numbers[2]);\n\n let mut result = String::new();\n if n1 > 0 && n1 % 2 == 0 {\n result.push('0');\n n1 -= 1;\n }\n if n2 > 0 || n1 > 0 {\n result.push('1');\n }\n for _ in 0..n2 {\n result.push('1');\n }\n for i in 0..n1 {\n if i % 2 == 0 {\n result.push('0');\n } else {\n result.push('1');\n }\n }\n if n0 > 0 && (result.is_empty() || result.ends_with('1')) {\n result.push('0');\n }\n for _ in 0..n0 {\n result.push('0');\n }\n println!(\"{}\", result);\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1044", "problem_description": "You have an $$$n \\times n$$$ chessboard and $$$k$$$ rooks. Rows of this chessboard are numbered by integers from $$$1$$$ to $$$n$$$ from top to bottom and columns of this chessboard are numbered by integers from $$$1$$$ to $$$n$$$ from left to right. The cell $$$(x, y)$$$ is the cell on the intersection of row $$$x$$$ and collumn $$$y$$$ for $$$1 \\leq x \\leq n$$$ and $$$1 \\leq y \\leq n$$$.The arrangement of rooks on this board is called good, if no rook is beaten by another rook.A rook beats all the rooks that shares the same row or collumn with it.The good arrangement of rooks on this board is called not stable, if it is possible to move one rook to the adjacent cell so arrangement becomes not good. Otherwise, the good arrangement is stable. Here, adjacent cells are the cells that share a side. Such arrangement of $$$3$$$ rooks on the $$$4 \\times 4$$$ chessboard is good, but it is not stable: the rook from $$$(1, 1)$$$ can be moved to the adjacent cell $$$(2, 1)$$$ and rooks on cells $$$(2, 1)$$$ and $$$(2, 4)$$$ will beat each other. Please, find any stable arrangement of $$$k$$$ rooks on the $$$n \\times n$$$ chessboard or report that there is no such arrangement.", "c_code": "int solution() {\n int N;\n scanf(\"%d\", &N);\n int chess[N][2];\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < 2; j++) {\n scanf(\"%d\", &chess[i][j]);\n }\n }\n\n for (int k = 0; k < N; k++) {\n int count = 0;\n if (chess[k][1] <= ceil(chess[k][0] / 2.0)) {\n\n for (int i = 0; i < chess[k][0]; i++) {\n\n for (int j = 0; j < chess[k][0]; j++) {\n\n if (i == j && i % 2 == 0 && j % 2 == 0 &&\n (count <= (chess[k][1] - 1))) {\n\n printf(\"R\");\n count++;\n\n }\n\n else {\n printf(\".\");\n }\n }\n printf(\"\\n\");\n }\n\n }\n\n else {\n printf(\"-1\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut handle = std::io::BufWriter::new(std::io::stdout());\n let mut test_no = String::new();\n std::io::stdin().read_line(&mut test_no).unwrap();\n let test_no: u8 = test_no.trim().parse().unwrap();\n for _ in 0..test_no {\n let mut string = String::new();\n std::io::stdin().read_line(&mut string).unwrap();\n let stray: Vec<&str> = string.trim().split(' ').collect();\n let array: Vec = stray.iter().map(|x| x.parse().unwrap()).collect();\n if array[1] > array[0].div_ceil(2) {\n writeln!(handle, \"-1\").expect(\"\");\n } else {\n let dotstr = \".\".repeat(array[0].into());\n let dotarr: Vec = dotstr.chars().collect();\n let mut full_dotarr = vec![dotarr; array[0].into()];\n for i in 0..array[1] {\n full_dotarr[(2 * i) as usize][(2 * i) as usize] = 'R';\n }\n for i in full_dotarr.iter() {\n writeln!(handle, \"{}\", i.iter().collect::()).ok();\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1045", "problem_description": "Bajtek, known for his unusual gifts, recently got an integer array $$$x_0, x_1, \\ldots, x_{k-1}$$$.Unfortunately, after a huge array-party with his extraordinary friends, he realized that he'd lost it. After hours spent on searching for a new toy, Bajtek found on the arrays producer's website another array $$$a$$$ of length $$$n + 1$$$. As a formal description of $$$a$$$ says, $$$a_0 = 0$$$ and for all other $$$i$$$ ($$$1 \\le i \\le n$$$) $$$a_i = x_{(i-1)\\bmod k} + a_{i-1}$$$, where $$$p \\bmod q$$$ denotes the remainder of division $$$p$$$ by $$$q$$$.For example, if the $$$x = [1, 2, 3]$$$ and $$$n = 5$$$, then: $$$a_0 = 0$$$, $$$a_1 = x_{0\\bmod 3}+a_0=x_0+0=1$$$, $$$a_2 = x_{1\\bmod 3}+a_1=x_1+1=3$$$, $$$a_3 = x_{2\\bmod 3}+a_2=x_2+3=6$$$, $$$a_4 = x_{3\\bmod 3}+a_3=x_0+6=7$$$, $$$a_5 = x_{4\\bmod 3}+a_4=x_1+7=9$$$. So, if the $$$x = [1, 2, 3]$$$ and $$$n = 5$$$, then $$$a = [0, 1, 3, 6, 7, 9]$$$.Now the boy hopes that he will be able to restore $$$x$$$ from $$$a$$$! Knowing that $$$1 \\le k \\le n$$$, help him and find all possible values of $$$k$$$ — possible lengths of the lost array.", "c_code": "int solution() {\n int n;\n int a = 0;\n int ai;\n int c = 0;\n scanf(\"%d\", &n);\n int A[n];\n int R[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &ai);\n A[i] = ai - a;\n a = ai;\n }\n\n for (int k = 1; k <= n; k++) {\n char p = 1;\n for (int i = k; i < n; i++) {\n if (A[i % k] != A[i]) {\n p = 0;\n break;\n }\n }\n if (p) {\n R[c] = k;\n c++;\n }\n }\n printf(\"%d\\n\", c);\n for (int i = 0; i < c; i++) {\n printf(\"%d \", R[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let sin = io::stdin();\n let mut f = sin.lock();\n let mut buf = String::new();\n f.read_line(&mut buf).unwrap();\n let n: usize = buf.trim().parse().unwrap();\n buf = \"\".to_string();\n f.read_line(&mut buf).unwrap();\n let mut a = Vec::::new();\n a.push(0i32);\n for p in buf.split_whitespace().map(|s| s.parse().unwrap()) {\n a.push(p);\n }\n\n let mut b = vec![0i32; n];\n for i in 0..n {\n b[i] = a[i + 1] - a[i];\n }\n let mut ans = Vec::::new();\n for k in 1..n {\n let mut z: bool = true;\n for i in k..n {\n if b[i] != b[i - k] {\n z = false;\n break;\n }\n }\n if z {\n ans.push(k);\n }\n }\n ans.push(n);\n let mut sp = String::new();\n for p in ans.iter() {\n sp = sp + &p.to_string() + \" \";\n }\n println!(\"{}\", ans.len());\n println!(\"{}\", sp)\n}", "difficulty": "medium"} {"problem_id": "1046", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

A programming competition site AtCode regularly holds programming contests.

\n

The next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200.

\n

The contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800.

\n

The contest after the ARC is called AGC, which is rated for all contestants.

\n

Takahashi's rating on AtCode is R. What is the next contest rated for him?

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 ≤ R ≤ 4208
  • \n
  • R is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
R\n
\n
\n
\n
\n
\n

Output

Print the name of the next contest rated for Takahashi (ABC, ARC or AGC).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1199\n
\n
\n
\n
\n
\n

Sample Output 1

ABC\n
\n

1199 is less than 1200, so ABC will be rated.

\n
\n
\n
\n
\n
\n

Sample Input 2

1200\n
\n
\n
\n
\n
\n

Sample Output 2

ARC\n
\n

1200 is not less than 1200 and ABC will be unrated, but it is less than 2800 and ARC will be rated.

\n
\n
\n
\n
\n
\n

Sample Input 3

4208\n
\n
\n
\n
\n
\n

Sample Output 3

AGC\n
\n
\n
", "c_code": "int solution() {\n int R = 0;\n\n scanf(\"%d\", &R);\n\n if (R < 1200) {\n printf(\"ABC\");\n } else if (1200 <= R && R < 2800) {\n printf(\"ARC\");\n } else if (2800 <= R) {\n printf(\"AGC\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut s = String::new();\n stdin.read_line(&mut s).ok();\n s.pop();\n let r = u32::from_str(&s).expect(\"e\");\n if r < 1200 {\n println!(\"ABC\");\n } else if r < 2800 {\n println!(\"ARC\");\n } else {\n println!(\"AGC\");\n }\n}", "difficulty": "easy"} {"problem_id": "1047", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Cat Snuke is learning to write characters.\nToday, he practiced writing digits 1 and 9, but he did it the other way around.

\n

You are given a three-digit integer n written by Snuke.\nPrint the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

\n
\n
\n
\n
\n

Constraints

    \n
  • 111 \\leq n \\leq 999
  • \n
  • n is an integer consisting of digits 1 and 9.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n\n
\n
\n
\n
\n
\n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

119\n
\n
\n
\n
\n
\n

Sample Output 1

991\n
\n

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

\n
\n
\n
\n
\n
\n

Sample Input 2

999\n
\n
\n
\n
\n
\n

Sample Output 2

111\n
\n
\n
", "c_code": "int solution() {\n int due = 0;\n int a = 0;\n int b = 0;\n int c = 0;\n scanf(\"%d\", &due);\n a = due / 100;\n b = (due - a * 100) / 10;\n c = (due - a * 100 - b * 10) / 1;\n\n a = 10 - a;\n b = 10 - b;\n c = 10 - c;\n\n printf(\"%d\", (a * 100) + (b * 10) + c);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let s = buf.trim();\n\n let mut v: Vec = s.chars().collect();\n\n for i in &mut v {\n if *i == '1' {\n *i = '9';\n } else if *i == '9' {\n *i = '1';\n }\n }\n\n println! {\"{}{}{}\", v[0], v[1], v[2]};\n}", "difficulty": "hard"} {"problem_id": "1048", "problem_description": "Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point $$$(x_1,y_1)$$$ to the point $$$(x_2,y_2)$$$.He has a rope, which he can use to pull the box. He can only pull the box if he stands exactly $$$1$$$ unit away from the box in the direction of one of two coordinate axes. He will pull the box to where he is standing before moving out of the way in the same direction by $$$1$$$ unit. For example, if the box is at the point $$$(1,2)$$$ and Wabbit is standing at the point $$$(2,2)$$$, he can pull the box right by $$$1$$$ unit, with the box ending up at the point $$$(2,2)$$$ and Wabbit ending at the point $$$(3,2)$$$.Also, Wabbit can move $$$1$$$ unit to the right, left, up, or down without pulling the box. In this case, it is not necessary for him to be in exactly $$$1$$$ unit away from the box. If he wants to pull the box again, he must return to a point next to the box. Also, Wabbit can't move to the point where the box is located.Wabbit can start at any point. It takes $$$1$$$ second to travel $$$1$$$ unit right, left, up, or down, regardless of whether he pulls the box while moving.Determine the minimum amount of time he needs to move the box from $$$(x_1,y_1)$$$ to $$$(x_2,y_2)$$$. Note that the point where Wabbit ends up at does not matter.", "c_code": "int solution() {\n int n;\n int a;\n int b;\n int c;\n int d;\n int sum = 0;\n scanf(\"%d\\n\", &n);\n while (n--) {\n sum = 0;\n scanf(\"%d %d %d %d\\n\", &a, &b, &c, &d);\n if (a > c) {\n sum += a - c;\n } else {\n sum += c - a;\n }\n if (b > d) {\n sum += b - d;\n } else {\n sum += d - b;\n }\n if (a != c && b != d) {\n sum += 2;\n }\n printf(\"%d\\n\", sum);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let test: i64 = str.trim().parse().unwrap();\n for _ in 0..test {\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let mut iter = str.split_whitespace();\n let x1: i64 = iter.next().unwrap().parse().unwrap();\n let y1: i64 = iter.next().unwrap().parse().unwrap();\n let x2: i64 = iter.next().unwrap().parse().unwrap();\n let y2: i64 = iter.next().unwrap().parse().unwrap();\n let mut d = (x1 - x2).abs() + (y1 - y2).abs();\n if (x1 - x2) * (y1 - y2) != 0 {\n d += 2;\n }\n println!(\"{}\", d);\n }\n}", "difficulty": "medium"} {"problem_id": "1049", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Takahashi has decided to work on K days of his choice from the N days starting with tomorrow.

\n

You are given an integer C and a string S. Takahashi will choose his workdays as follows:

\n
    \n
  • After working for a day, he will refrain from working on the subsequent C days.
  • \n
  • If the i-th character of S is x, he will not work on Day i, where Day 1 is tomorrow, Day 2 is the day after tomorrow, and so on.
  • \n
\n

Find all days on which Takahashi is bound to work.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq K \\leq N
  • \n
  • 0 \\leq C \\leq N
  • \n
  • The length of S is N.
  • \n
  • Each character of S is o or x.
  • \n
  • Takahashi can choose his workdays so that the conditions in Problem Statement are satisfied.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K C\nS\n
\n
\n
\n
\n
\n

Output

Print all days on which Takahashi is bound to work in ascending order, one per line.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

11 3 2\nooxxxoxxxoo\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

Takahashi is going to work on 3 days out of the 11 days. After working for a day, he will refrain from working on the subsequent 2 days.

\n

There are four possible choices for his workdays: Day 1,6,10, Day 1,6,11, Day 2,6,10, and Day 2,6,11.

\n

Thus, he is bound to work on Day 6.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 2 3\nooxoo\n
\n
\n
\n
\n
\n

Sample Output 2

1\n5\n
\n

There is only one possible choice for his workdays: Day 1,5.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 1 0\nooooo\n
\n
\n
\n
\n
\n

Sample Output 3

\n

There may be no days on which he is bound to work.

\n
\n
\n
\n
\n
\n

Sample Input 4

16 4 3\nooxxoxoxxxoxoxxo\n
\n
\n
\n
\n
\n

Sample Output 4

11\n16\n
\n
\n
", "c_code": "int solution() {\n int n;\n int k;\n int c;\n scanf(\"%d%d%d\", &n, &k, &c);\n char s[n + 5];\n scanf(\"%s\", s);\n int cnt[n + 1];\n int j = INT_MAX;\n cnt[n] = 0;\n for (int i = n - 1; i >= 0; i--) {\n if (s[i] == 'x' || j - i < c + 1) {\n cnt[i] = cnt[i + 1];\n } else {\n cnt[i] = cnt[i + 1] + 1;\n j = i;\n }\n }\n\n int ans[k + 1];\n ans[0] = INT_MIN;\n j = 0;\n for (int i = 0; i < n && j <= k; i++) {\n if (s[i] == 'o' && ans[j] + c < i) {\n j++;\n ans[j] = i;\n }\n }\n\n for (int i = 0; i < k; i++) {\n if (cnt[ans[i + 1]] == cnt[ans[i + 1] + 1] + 1 &&\n cnt[ans[i + 1]] == k - i) {\n printf(\"%d\\n\", ans[i + 1] + 1);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let stdout = std::io::stdout();\n let mut reader = std::io::BufReader::new(stdin.lock());\n let mut writer = std::io::BufWriter::new(stdout.lock());\n\n let (n, k, c): (usize, usize, usize) = {\n let mut buf = String::new();\n reader.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\n let s: Vec = {\n let mut buf = String::new();\n reader.read_line(&mut buf).unwrap();\n buf.trim_end().chars().collect()\n };\n\n let mut left = vec![0; k];\n let (mut li, mut lj) = (0, 0);\n while li < n && lj < k {\n if s[li] == 'o' {\n left[lj] = li;\n li += c;\n lj += 1;\n }\n li += 1;\n }\n\n let mut right = vec![0; k];\n let (mut ri, mut rj) = (0, 0);\n while ri < n && rj < k {\n if s[n - 1 - ri] == 'o' {\n right[k - 1 - rj] = n - 1 - ri;\n ri += c;\n rj += 1;\n }\n ri += 1;\n }\n\n for i in 0..k {\n if left[i] == right[i] {\n writeln!(writer, \"{}\", left[i] + 1).unwrap();\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1050", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Print the K-th element of the following sequence of length 32:

\n
1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51\n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq 32
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K\n
\n
\n
\n
\n
\n

Output

Print the K-th element.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The 6-th element is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

27\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n

The 27-th element is 5.

\n
\n
", "c_code": "int solution() {\n\n int in[] = {1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14,\n 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};\n\n int innum = 0;\n\n scanf(\"%d\", &innum);\n\n if (innum > 0 && innum <= 32) {\n printf(\"%d\\n\", in[innum - 1]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let array = [\n 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4,\n 1, 51,\n ];\n let mut input = String::new();\n\n io::stdin()\n .read_line(&mut input)\n .expect(\"Failed to read line\");\n\n let index: usize = input.trim().parse().expect(\"uooo\");\n\n println!(\"{}\", array[index - 1]);\n}", "difficulty": "medium"} {"problem_id": "1051", "problem_description": "

Red Dragonfly

\n \n

\n It’s still hot every day, but September has already come. It’s autumn according to the calendar. Looking around, I see two red dragonflies at rest on the wall in front of me. It’s autumn indeed.\n

\n\n

\n When two red dragonflies’ positional information as measured from the end of the wall is given, make a program to calculate the distance between their heads.\n

\n\n

Input

\n

\n The input is given in the following format.\n

\n
\n$x_1$ $x_2$\n
\n\n

\n The input line provides dragonflies’ head positions $x_1$ and $x_2$ ($0 \\leq x_1, x_2 \\leq 100$) as integers.\n

\n\n

Output

\n

\n Output the distance between the two red dragonflies in a line.\n

\n\n

Sample Input 1

\n
\n20 30\n
\n

Sample Output 1

\n
\n10\n
\n\n

Sample Input 2

\n\n
\n50 25\n\n
\n

Sample Output 2

\n
\n25\n
\n\n

Sample Input 3

\n
\n25 25\n
\n

Sample output 3

\n
\n0\n
", "c_code": "int solution() {\n int x1 = 0;\n int x2 = 0;\n int X = 0;\n\n scanf(\"%d %d\\n\", &x1, &x2);\n\n if (((0 <= x1) && (x1 <= 100)) && ((0 <= x2) && (x2 <= 100))) {\n\n if (x1 > x2) {\n X = x1 - x2;\n } else {\n X = x2 - x1;\n }\n printf(\"%d\\n\", X);\n }\n\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) = (iter.next().unwrap(), iter.next().unwrap());\n\n println!(\"{}\", (a - b).abs());\n}", "difficulty": "medium"} {"problem_id": "1052", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Takahashi is going to buy N items one by one.

\n

The price of the i-th item he buys is A_i yen (the currency of Japan).

\n

He has M discount tickets, and he can use any number of them when buying an item.

\n

If Y tickets are used when buying an item priced X yen, he can get the item for \\frac{X}{2^Y} (rounded down to the nearest integer) yen.

\n

What is the minimum amount of money required to buy all the items?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N, M \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum amount of money required to buy all the items.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n2 13 8\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

We can buy all the items for 9 yen, as follows:

\n
    \n
  • Buy the 1-st item for 2 yen without tickets.
  • \n
  • Buy the 2-nd item for 3 yen with 2 tickets.
  • \n
  • Buy the 3-rd item for 4 yen with 1 ticket.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4 4\n1 9 3 5\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 100000\n1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

\n
\n
\n
\n
\n
\n

Sample Input 4

10 1\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 4

9500000000\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int m;\n int a[200000] = {0};\n scanf(\"%d %d\", &n, &m);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n int ind = i;\n while (ind > 0 && a[ind] > a[(ind - 1) / 2]) {\n int temp = a[ind];\n a[ind] = a[(ind - 1) / 2];\n a[(ind - 1) / 2] = temp;\n ind = (ind - 1) / 2;\n }\n }\n\n for (int j = 0; j < m; j++) {\n a[0] = a[0] / 2;\n int ind = 0;\n while (a[ind] < a[(2 * ind) + 1] || a[ind] < a[(2 * ind) + 2]) {\n int ind2;\n if (a[(2 * ind) + 1] >= a[(2 * ind) + 2]) {\n ind2 = 2 * ind + 1;\n } else {\n ind2 = 2 * ind + 2;\n }\n int temp = a[ind];\n a[ind] = a[ind2];\n a[ind2] = temp;\n ind = ind2;\n }\n }\n\n long sum = 0;\n for (int k = 0; k < n; k++) {\n sum += a[k];\n }\n\n printf(\"%ld\", sum);\n return 0;\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 n = it.next().unwrap().parse::().unwrap();\n let m = it.next().unwrap().parse::().unwrap();\n let a = (0..n)\n .map(|_| it.next().unwrap().parse::().unwrap())\n .collect::>();\n let mut qu = BinaryHeap::from(a);\n for _i in 0..m {\n let v = qu.pop().unwrap();\n qu.push(v / 2);\n }\n println!(\"{}\", qu.iter().sum::());\n}", "difficulty": "medium"} {"problem_id": "1053", "problem_description": "Riley is a very bad boy, but at the same time, he is a yo-yo master. So, he decided to use his yo-yo skills to annoy his friend Anton.Anton's room can be represented as a grid with $$$n$$$ rows and $$$m$$$ columns. Let $$$(i, j)$$$ denote the cell in row $$$i$$$ and column $$$j$$$. Anton is currently standing at position $$$(i, j)$$$ in his room. To annoy Anton, Riley decided to throw exactly two yo-yos in cells of the room (they can be in the same cell).Because Anton doesn't like yo-yos thrown on the floor, he has to pick up both of them and return back to the initial position. The distance travelled by Anton is the shortest path that goes through the positions of both yo-yos and returns back to $$$(i, j)$$$ by travelling only to adjacent by side cells. That is, if he is in cell $$$(x, y)$$$ then he can travel to the cells $$$(x + 1, y)$$$, $$$(x - 1, y)$$$, $$$(x, y + 1)$$$ and $$$(x, y - 1)$$$ in one step (if a cell with those coordinates exists).Riley is wondering where he should throw these two yo-yos so that the distance travelled by Anton is maximized. But because he is very busy, he asked you to tell him.", "c_code": "int solution() {\n short int t;\n scanf(\"%hd\", &t);\n int arr[t][4];\n for (short int i = 0; i < t; i++) {\n for (short int j = 0; j < 4; j++) {\n scanf(\"%d\", &arr[i][j]);\n }\n }\n for (int i = 0; i < t; i++) {\n printf(\"%d %d %d %d\\n\", arr[i][0], 1, 1, arr[i][1]);\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\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 let m = it.next().unwrap();\n let _i = it.next().unwrap();\n let _j = it.next().unwrap();\n\n writeln!(&mut output, \"{} {} {} {}\", 1, 1, n, m).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1054", "problem_description": "Berland crossword is a puzzle that is solved on a square grid with $$$n$$$ rows and $$$n$$$ columns. Initially all the cells are white.To solve the puzzle one has to color some cells on the border of the grid black in such a way that: exactly $$$U$$$ cells in the top row are black; exactly $$$R$$$ cells in the rightmost column are black; exactly $$$D$$$ cells in the bottom row are black; exactly $$$L$$$ cells in the leftmost column are black. Note that you can color zero cells black and leave every cell white.Your task is to check if there exists a solution to the given puzzle.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int u;\n int r;\n int d;\n int l;\n bool f = 1;\n scanf(\"%d %d %d %d %d\", &n, &u, &r, &d, &l);\n\n while (1) {\n\n if (u == n || d == n) {\n if (r == 0 || l == 0) {\n f = 0;\n break;\n }\n }\n\n if (l == n || r == n) {\n if (u == 0 || d == 0) {\n f = 0;\n break;\n }\n }\n\n if (u == n && d == n) {\n if (r < 2 || l < 2) {\n f = 0;\n break;\n }\n }\n\n if (l == n && r == n) {\n if (u < 2 || d < 2) {\n f = 0;\n break;\n }\n }\n\n if (u == n - 1 || d == n - 1) {\n if (r == 0 && l == 0) {\n f = 0;\n break;\n }\n }\n\n if (l == n - 1 || r == n - 1) {\n if (u == 0 && d == 0) {\n f = 0;\n break;\n }\n }\n\n if (u == n - 1 && d == n - 1) {\n if (r == 0 && l == 0) {\n f = 0;\n break;\n }\n if (r == 1 && l == 0) {\n f = 0;\n break;\n }\n if (r == 0 && l == 1) {\n f = 0;\n break;\n }\n }\n\n if (l == n - 1 && r == n - 1) {\n if (u == 0 && d == 0) {\n f = 0;\n break;\n }\n if (u == 1 && d == 0) {\n f = 0;\n break;\n }\n if (u == 0 && d == 1) {\n f = 0;\n break;\n }\n }\n\n if (u == n && d == n - 1) {\n if (r < 2 && l < 2) {\n f = 0;\n break;\n }\n }\n\n if (u == n - 1 && d == n) {\n if (r < 2 && l < 2) {\n f = 0;\n break;\n }\n }\n\n if (l == n && r == n - 1) {\n if (u < 2 && d < 2) {\n f = 0;\n break;\n }\n }\n\n if (r == n && l == n - 1) {\n if (u < 2 && d < 2) {\n f = 0;\n break;\n }\n }\n break;\n }\n\n printf(f ? \"YES\\n\" : \"NO\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n let _t = it.next();\n\n 'outer: while let Some(n) = it.next() {\n let u = it.next().unwrap();\n let r = it.next().unwrap();\n let b = it.next().unwrap();\n let l = it.next().unwrap();\n\n let a = [u, r, b, l];\n\n for i in 0..(1 << 4) {\n let works = (0..4).all(|j| {\n let s = i & (1 << j);\n let e = i & (1 << ((j + 1) % 4));\n\n let up = n - if s == 0 { 1 } else { 0 } - if e == 0 { 1 } else { 0 };\n\n let low = if s != 0 { 1 } else { 0 } + if e != 0 { 1 } else { 0 };\n\n (low..=up).contains(&a[j])\n });\n\n if works {\n println!(\"YES\");\n continue 'outer;\n }\n }\n\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "1055", "problem_description": "Let's call a string adorable if its letters can be realigned in such a way that they form two consequent groups of equal symbols (note that different groups must contain different symbols). For example, ababa is adorable (you can transform it to aaabb, where the first three letters form a group of a-s and others — a group of b-s), but cccc is not since in each possible consequent partition letters in these two groups coincide.You're given a string s. Check whether it can be split into two non-empty subsequences such that the strings formed by these subsequences are adorable. Here a subsequence is an arbitrary set of indexes of the string.", "c_code": "int solution() {\n char a[100000];\n int i;\n int sum = 0;\n int k = 0;\n int m = 0;\n int b[30] = {0};\n int p = 0;\n int c[30];\n int j = 0;\n scanf(\"%s\", a);\n k = strlen(a);\n for (i = 0; i < k; i++) {\n b[a[i] - 'a']++;\n }\n for (i = 0; i <= 26; i++) {\n if (b[i] > 0) {\n p++;\n }\n }\n if (p > 4) {\n printf(\"NO\");\n }\n if (p == 1) {\n printf(\"NO\");\n }\n if (p == 2) {\n for (i = 0; i <= 26; i++) {\n if (b[i] > 0) {\n c[j++] = b[i];\n }\n }\n if ((c[0] == 1 && c[1] != 1) || (c[0] != 1 && c[1] == 1) ||\n (c[0] == 1 && c[1] == 1)) {\n printf(\"NO\");\n } else {\n printf(\"YES\");\n }\n }\n if (p == 3) {\n for (i = 0; i <= 26; i++) {\n if (b[i] > 0) {\n c[j++] = b[i];\n }\n }\n if (c[0] == 1 && c[1] == 1 && c[2] == 1) {\n printf(\"NO\");\n } else {\n printf(\"YES\");\n }\n }\n if (p == 4) {\n printf(\"YES\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::{self, prelude::*};\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut a = [0; 26];\n let mut uc = 0;\n let mut tc = 0;\n let s = input.trim();\n for i in s.bytes().map(|b| (b - b'a') as usize) {\n if a[i] == 0 {\n uc += 1;\n } else if a[i] == 1 {\n tc += 1;\n }\n a[i] += 1;\n }\n\n let ans = match uc {\n 4 => true,\n 3 if s.len() >= 4 => true,\n 2 if s.len() >= 4 && tc >= 2 => true,\n _ => false,\n };\n if ans {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "1056", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

This is an interactive task.

\n

Let N be an odd number at least 3.

\n

There are N seats arranged in a circle.\nThe seats are numbered 0 through N-1.\nFor each i (0 ≤ i ≤ N - 2), Seat i and Seat i + 1 are adjacent.\nAlso, Seat N - 1 and Seat 0 are adjacent.

\n

Each seat is either vacant, or oppupied by a man or a woman.\nHowever, no two adjacent seats are occupied by two people of the same sex.\nIt can be shown that there is at least one empty seat where N is an odd number at least 3.

\n

You are given N, but the states of the seats are not given.\nYour objective is to correctly guess the ID number of any one of the empty seats.\nTo do so, you can repeatedly send the following query:

\n
    \n
  • Choose an integer i (0 ≤ i ≤ N - 1). If Seat i is empty, the problem is solved. Otherwise, you are notified of the sex of the person in Seat i.
  • \n
\n

Guess the ID number of an empty seat by sending at most 20 queries.

\n
\n
\n
\n
\n

Constraints

    \n
  • N is an odd number.
  • \n
  • 3 ≤ N ≤ 99 999
  • \n
\n
\n
\n
\n
\n

Input and Output

First, N is given from Standard Input in the following format:

\n
N\n
\n

Then, you should send queries.\nA query should be printed to Standart Output in the following format.\nPrint a newline at the end.

\n
i\n
\n

The response to the query is given from Standard Input in the following format:

\n
s\n
\n

Here, s is Vacant, Male or Female.\nEach of these means that Seat i is empty, occupied by a man and occupied by a woman, respectively.

\n
\n
\n
\n
\n

Notice

    \n
  • Flush Standard Output each time you print something. Failure to do so may result in TLE.
  • \n
  • Immediately terminate the program when s is Vacant. Otherwise, the verdict is indeterminate.
  • \n
  • The verdict is indeterminate if more than 20 queries or ill-formatted queries are sent.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input / Output 1

\n

In this sample, N = 3, and Seat 0, 1, 2 are occupied by a man, occupied by a woman and vacant, respectively.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n
InputOutput
3
0
Male
1
Female
2
Vacant
\n
\n
\n
", "c_code": "int solution() {\n long n;\n scanf(\"%ld\", &n);\n char s[100001][7];\n printf(\"0\\n\");\n fflush(stdout);\n scanf(\"%s\", s[0]);\n if (s[0][0] == 'V') {\n return 0;\n }\n int i;\n long min = 0;\n long max = n;\n for (i = 1; i < 19; i++) {\n printf(\"%ld\\n\", (max + min) / 2);\n fflush(stdout);\n scanf(\"%s\", s[(max + min) / 2]);\n if (s[(max + min) / 2][0] == 'V') {\n break;\n }\n if (((long)((max + min) / 2) - min) % 2 == 0) {\n if (s[min][0] == s[(max + min) / 2][0]) {\n min = (max + min) / 2;\n } else {\n max = (max + min) / 2;\n }\n continue;\n }\n if (s[min][0] == s[(max + min) / 2][0]) {\n max = (max + min) / 2;\n } else {\n min = (max + min) / 2;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let n: usize = buf.trim().parse().unwrap();\n\n println!(\"0\");\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n\n let mut start = buf.trim().to_string();\n if start == \"Vacant\" {\n return;\n }\n\n let mut begin = 0;\n let mut end = n;\n for _ in 0..19 {\n let mid = (begin + end) / 2;\n println!(\"{}\", mid);\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let current = buf.trim().to_string();\n if current == \"Vacant\" {\n return;\n }\n\n if begin + 1 == mid {\n begin = mid;\n } else if (mid - begin).is_multiple_of(2) && current == start\n || (mid - begin) % 2 == 1 && current != start\n {\n begin = mid;\n start = current;\n } else {\n end = mid;\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1057", "problem_description": "Bessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play! More specifically, he wants to get from $$$(0,0)$$$ to $$$(x,0)$$$ by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its $$$n$$$ favorite numbers: $$$a_1, a_2, \\ldots, a_n$$$. What is the minimum number of hops Rabbit needs to get from $$$(0,0)$$$ to $$$(x,0)$$$? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination.Recall that the Euclidean distance between points $$$(x_i, y_i)$$$ and $$$(x_j, y_j)$$$ is $$$\\sqrt{(x_i-x_j)^2+(y_i-y_j)^2}$$$.For example, if Rabbit has favorite numbers $$$1$$$ and $$$3$$$ he could hop from $$$(0,0)$$$ to $$$(4,0)$$$ in two hops as shown below. Note that there also exists other valid ways to hop to $$$(4,0)$$$ in $$$2$$$ hops (e.g. $$$(0,0)$$$ $$$\\rightarrow$$$ $$$(2,-\\sqrt{5})$$$ $$$\\rightarrow$$$ $$$(4,0)$$$). Here is a graphic for the first example. Both hops have distance $$$3$$$, one of Rabbit's favorite numbers. In other words, each time Rabbit chooses some number $$$a_i$$$ and hops with distance equal to $$$a_i$$$ in any direction he wants. The same number can be used multiple times.", "c_code": "int solution() {\n int t;\n int n;\n int x;\n scanf(\"%d\", &t);\n while (t-- > 0) {\n scanf(\"%d%d\", &n, &x);\n int arr[n];\n int count = INT_MAX;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n for (int i = n - 1; i >= 0 && x > 0; i--) {\n int temp = x >= arr[i] ? (x + arr[i] - 1) / arr[i] : 2;\n if (count > temp) {\n count = temp;\n }\n }\n printf(\"%d\\n\", count);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read line\");\n buf.trim().parse::().unwrap()\n };\n for _ in 0..t {\n let (_, x) = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read\");\n let nums: Vec = buf\n .trim()\n .split_ascii_whitespace()\n .map(|c| c.parse().unwrap())\n .collect();\n (nums[0], nums[1])\n };\n let nums: Vec = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read\");\n buf.trim()\n .split_ascii_whitespace()\n .map(|c| c.parse().unwrap())\n .collect()\n };\n let ans;\n if nums.contains(&x) {\n ans = 1;\n } else {\n let max_ = nums.iter().max().unwrap();\n ans = max(2, (x + max_ - 1) / max_);\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1058", "problem_description": "There are $$$n$$$ boxes with different quantities of candies in each of them. The $$$i$$$-th box has $$$a_i$$$ candies inside.You also have $$$n$$$ friends that you want to give the candies to, so you decided to give each friend a box of candies. But, you don't want any friends to get upset so you decided to eat some (possibly none) candies from each box so that all boxes have the same quantity of candies in them. Note that you may eat a different number of candies from different boxes and you cannot add candies to any of the boxes.What's the minimum total number of candies you have to eat to satisfy the requirements?", "c_code": "int solution() {\n int arr[52];\n int acc[1001];\n int i = 0;\n int j = 0;\n int k = 0;\n int o = 0;\n int min = 100000000;\n int sum = 0;\n scanf(\"%d\", &j);\n for (i = 0; i < j; i++) {\n scanf(\"%d\", &k);\n for (o = 0; o < k; o++) {\n scanf(\"%d\", &arr[o]);\n if (arr[o] < min) {\n min = arr[o];\n }\n }\n for (o = 0; o < k; o++) {\n sum += arr[o] - min;\n }\n acc[i] = sum;\n sum = 0;\n for (o = 0; o < k; o++) {\n arr[o] = 0;\n }\n min = 100000000;\n }\n for (i = 0; i < j; i++) {\n printf(\"%d\\n\", acc[i]);\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: usize = input.next().unwrap();\n for _ in 0..t {\n let n = input.next().unwrap();\n let a: Vec<_> = input.by_ref().take(n).collect();\n let min = *a.iter().min().unwrap();\n let sum = a.iter().fold(0, |acc, &x| x - min + acc);\n writeln!(output, \"{sum}\").ok();\n }\n print!(\"{output}\");\n}", "difficulty": "medium"} {"problem_id": "1059", "problem_description": "\n

Score: 500 points

\n
\n
\n

Problem Statement

\n

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

\n

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

\n

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

\n
    \n
  • The absolute difference of their attendee numbers is equal to the sum of their heights.
  • \n
\n

There are \\frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

\n

P.S.: We cannot let you know the secret.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9\\ (1 \\leq i \\leq N)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 \\dots A_N\n
\n
\n
\n
\n
\n

Output

\n

Print the number of pairs satisfying the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n2 3 3 1 3 1\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n
    \n
  • A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.
  • \n
  • A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.
  • \n
  • A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.
  • \n
\n

No other pair satisfies the condition, so you should print 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

6\n5 2 4 2 8 8\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

No pair satisfies the condition, so you should print 0.

\n
\n
\n
\n
\n
\n

Sample Input 3

32\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5\n
\n
\n
\n
\n
\n

Sample Output 3

22\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n static int a[200003];\n static int conne[200003][2];\n\n for (int i = 1; i <= n; i++) {\n conne[i][0] = conne[i][1] = 0;\n }\n\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n if (i + a[i] <= n) {\n conne[i + a[i]][0]++;\n }\n if (i - a[i] > 1) {\n conne[i - a[i]][1]++;\n }\n }\n\n long long int count = 0;\n for (int i = 2; i < n; i++) {\n count = count + (long long int)(conne[i][0]) * (long long int)(conne[i][1]);\n }\n printf(\"%lld\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n let stdin = std::io::stdin();\n let _ = stdin.read_line(&mut line);\n line.clear();\n let _ = stdin.read_line(&mut line);\n let a: Vec<_> = line\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n let (mut map1, mut map2) = (HashMap::new(), HashMap::new());\n for (i, &x) in a.iter().enumerate() {\n let i = i as i64;\n map1.insert(x + i, *map1.get(&(x + i)).unwrap_or(&0) + 1);\n map2.insert(i - x, *map2.get(&(i - x)).unwrap_or(&0) + 1);\n }\n let mut cnt = 0;\n for (k, v) in map1.iter() {\n if let Some(n) = map2.get(k) {\n cnt += (*n as i64) * (*v as i64);\n }\n }\n println!(\"{}\", cnt);\n}", "difficulty": "hard"} {"problem_id": "1060", "problem_description": "You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \\leq i < j \\leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int or = a[0];\n for (int i = 1; i < n; i++) {\n or = or | a[i];\n }\n printf(\"%d\\n\", or);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input);\n let T: i32 = input.trim().parse().unwrap();\n\n for _i in 0..T {\n let mut input = String::new();\n io::stdin().read_line(&mut input);\n let n: usize = input.trim().parse().unwrap();\n\n let mut input = String::new();\n io::stdin().read_line(&mut input);\n let vector: Vec = input\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut count: i32 = 0;\n for i in 0..n {\n count |= vector[i];\n }\n println!(\"{}\", count);\n }\n}", "difficulty": "medium"} {"problem_id": "1061", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N.\nSpot i is at the point with coordinate A_i.\nIt costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

\n

You planned a trip along the axis.\nIn this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

\n

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i.\nYou will visit the remaining spots as planned in the order they are numbered.\nYou will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

\n

For each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • -5000 \\leq A_i \\leq 5000 (1 \\leq i \\leq N)
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print N lines.\nIn the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n3 5 -1\n
\n
\n
\n
\n
\n

Sample Output 1

12\n8\n10\n
\n

Spot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively.\nFor each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:

\n
    \n
  • For i = 1, the course of the trip is 0 \\rightarrow 5 \\rightarrow -1 \\rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.
  • \n
  • For i = 2, the course of the trip is 0 \\rightarrow 3 \\rightarrow -1 \\rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.
  • \n
  • For i = 3, the course of the trip is 0 \\rightarrow 3 \\rightarrow 5 \\rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5\n1 1 1 2 0\n
\n
\n
\n
\n
\n

Sample Output 2

4\n4\n4\n2\n4\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6\n-679 -2409 -3258 3095 -3291 -4462\n
\n
\n
\n
\n
\n

Sample Output 3

21630\n21630\n19932\n8924\n21630\n19288\n
\n
\n
", "c_code": "int solution() {\n int points = 0;\n int diff = 0;\n int diff2 = 0;\n int total = 0;\n int cnt = 0;\n int p[100002] = {0};\n\n scanf(\"%d\", &points);\n\n while (cnt < points) {\n scanf(\"%d\", &p[cnt + 1]);\n\n diff = p[cnt + 1] - p[cnt];\n if (diff < 0) {\n diff = diff * -1;\n }\n total = total + diff;\n\n cnt++;\n }\n\n if (p[cnt] < 0) {\n total = total - p[cnt];\n } else {\n total = total + p[cnt];\n }\n\n for (cnt = 0; cnt < points; cnt++) {\n diff = p[cnt] - p[cnt + 1];\n diff2 = p[cnt + 1] - p[cnt + 2];\n\n if (diff * diff2 > 0) {\n printf(\"%d\\n\", total);\n } else {\n if (diff > diff2) {\n diff = diff - diff2;\n } else {\n diff = diff2 - diff;\n }\n\n diff2 = p[cnt + 2] - p[cnt];\n\n if (diff2 < 0) {\n diff2 = diff2 * -1;\n }\n\n printf(\"%d\\n\", total - diff + diff2);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let n = buf.trim().parse::().unwrap();\n\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut an = vec![0];\n let iter = buf.split_whitespace();\n for it in iter {\n an.push(it.parse::().unwrap());\n }\n let mut sum = 0;\n for i in 0..n {\n sum += (an[i + 1] - an[i]).abs();\n }\n sum += an[n].abs();\n for i in 0..n - 1 {\n println!(\n \"{}\",\n sum - (an[i + 1] - an[i]).abs() - (an[i + 2] - an[i + 1]).abs()\n + (an[i + 2] - an[i]).abs()\n );\n }\n println!(\n \"{}\",\n sum - (an[n] - an[n - 1]).abs() - an[n].abs() + an[n - 1].abs()\n );\n}", "difficulty": "medium"} {"problem_id": "1062", "problem_description": "Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \\le 2$$$ and $$$a_5=1 \\le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \\le 5$$$, $$$a_3=4 \\le 5$$$ and $$$a_4=5 \\le 5$$$); the $$$6$$$-th granny cannot be called into the yard  — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies).", "c_code": "int solution() {\n long y;\n long x;\n scanf(\"%ld\", &x);\n long a[100000];\n long B[100000];\n\n for (y = 0; y < x; y++) {\n long i;\n long n;\n long k = 0;\n scanf(\"%ld\\n\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n if (a[i] > n) {\n i--;\n n--;\n }\n }\n for (i = 0; i < n; i++) {\n if (a[i] > (n - k)) {\n a[i] = 0;\n k++;\n i = -1;\n }\n }\n a[0] = n - k + 1;\n B[y] = a[0];\n }\n for (y = 0; y < x; y++) {\n printf(\"%ld\\n\", B[y]);\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 t: usize = itr.next().unwrap().parse().unwrap();\n for _ in 0..t {\n let n: usize = itr.next().unwrap().parse().unwrap();\n let mut a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n a.sort();\n let mut ans = 1;\n for i in 0..n {\n if a[i] <= 1 + i {\n ans = i + 2;\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "hard"} {"problem_id": "1063", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

Takahashi is playing with N cards.

\n

The i-th card has an integer X_i on it.

\n

Takahashi is trying to create as many pairs of cards as possible satisfying one of the following conditions:

\n
    \n
  • The integers on the two cards are the same.
  • \n
  • The sum of the integers on the two cards is a multiple of M.
  • \n
\n

Find the maximum number of pairs that can be created.

\n

Note that a card cannot be used in more than one pair.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≦N≦10^5
  • \n
  • 1≦M≦10^5
  • \n
  • 1≦X_i≦10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N M\nX_1 X_2 ... X_N\n
\n
\n
\n
\n
\n

Output

Print the maximum number of pairs that can be created.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7 5\n3 1 4 1 5 9 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

Three pairs (3,2), (1,4) and (1,9) can be created.

\n

It is possible to create pairs (3,2) and (1,1), but the number of pairs is not maximized with this.

\n
\n
\n
\n
\n
\n

Sample Input 2

15 10\n1 5 6 10 11 11 11 20 21 25 25 26 99 99 99\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int i;\n int x[100005];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &x[i]);\n }\n int a[100005];\n for (i = 0; i < 100005; i++) {\n a[i] = 0;\n }\n for (i = 0; i < n; i++) {\n a[x[i]]++;\n }\n int b[100005];\n for (i = 0; i <= m; i++) {\n b[i] = 0;\n }\n for (i = 0; i < n; i++) {\n b[x[i] % m]++;\n }\n int ans = b[0] / 2;\n b[0] = 0;\n if (m % 2 == 0) {\n ans += b[m / 2] / 2;\n b[m / 2] = 0;\n }\n for (i = 0; i < m; i++) {\n if (b[i] < b[m - i]) {\n ans += b[i];\n b[m - i] -= b[i];\n b[i] = 0;\n } else {\n ans += b[m - i];\n b[i] -= b[m - i];\n b[m - i] = 0;\n }\n }\n for (i = 0; i < 100005; i++) {\n if (a[i] > 1 && b[i % m] > 1) {\n ans++;\n a[i] -= 2;\n b[i % m] -= 2;\n i--;\n }\n }\n printf(\"%d\\n\", ans);\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 m: usize = itr.next().unwrap().parse().unwrap();\n let mut x: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n x.sort();\n\n let mut memo: Vec> = vec![Vec::new(); 100010];\n let mut ans = 0;\n for i in 0..n {\n memo[x[i] % m].push(x[i]);\n }\n for i in 0..m / 2 + 1 {\n if i == 0 || (m.is_multiple_of(2) && i == m / 2) {\n ans += memo[i].len() / 2;\n } else {\n let mut a = memo[i].clone();\n let mut b = memo[m - i].clone();\n if a.len() < b.len() {\n std::mem::swap(&mut a, &mut b);\n }\n let mut same = 0;\n let mut idx = 0;\n while idx < a.len() {\n while idx < a.len() - 1 && a[idx] == a[idx + 1] {\n idx += 2;\n same += 1;\n }\n idx += 1;\n }\n ans += b.len() + std::cmp::min((a.len() - b.len()) / 2, same);\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1064", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

The weather in Takahashi's town changes day by day, in the following cycle: Sunny, Cloudy, Rainy, Sunny, Cloudy, Rainy, ...

\n

Given is a string S representing the weather in the town today. Predict the weather tomorrow.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • S is Sunny, Cloudy, or Rainy.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

\n

Print a string representing the expected weather tomorrow, in the same format in which input is given.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

Sunny\n
\n
\n
\n
\n
\n

Sample Output 1

Cloudy\n
\n

In Takahashi's town, a sunny day is followed by a cloudy day.

\n
\n
\n
\n
\n
\n

Sample Input 2

Rainy\n
\n
\n
\n
\n
\n

Sample Output 2

Sunny\n
\n
\n
", "c_code": "int solution() {\n char s1[7] = {'S', 'u', 'n', 'n', 'y'};\n char s2[7] = {'C', 'l', 'o', 'u', 'd', 'y'};\n char s3[7] = {'R', 'a', 'i', 'n', 'y'};\n char n[7] = {0};\n\n scanf(\"%s\", n);\n\n if (*n == *s1) {\n puts(s2);\n } else if (*n == *s2) {\n puts(s3);\n } else if (*n == *s3) {\n puts(s1);\n } else {\n return 1;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut w = String::new();\n\n io::stdin().read_line(&mut w).expect(\"Failed to read line\");\n\n let w_str: &str = &w;\n\n match w_str {\n \"Sunny\\n\" => println!(\"Cloudy\"),\n \"Cloudy\\n\" => println!(\"Rainy\"),\n \"Rainy\\n\" => println!(\"Sunny\"),\n _ => println!(\"retry enter!\"),\n }\n}", "difficulty": "hard"} {"problem_id": "1065", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi is a member of a programming competition site, ButCoder.

\n

Each member of ButCoder is assigned two values: Inner Rating and Displayed Rating.

\n

The Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or more contests. Otherwise, the Displayed Rating will be their Inner Rating minus 100 \\times (10 - K) when the member has participated in K contests.

\n

Takahashi has participated in N contests, and his Displayed Rating is R. Find his Inner Rating.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 100
  • \n
  • 0 \\leq R \\leq 4111
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N R\n
\n
\n
\n
\n
\n

Output

Print his Inner Rating.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2919\n
\n
\n
\n
\n
\n

Sample Output 1

3719\n
\n

Takahashi has participated in 2 contests, which is less than 10, so his Displayed Rating is his Inner Rating minus 100 \\times (10 - 2) = 800.

\n

Thus, Takahashi's Inner Rating is 2919 + 800 = 3719.

\n
\n
\n
\n
\n
\n

Sample Input 2

22 3051\n
\n
\n
\n
\n
\n

Sample Output 2

3051\n
\n
\n
", "c_code": "int solution(void) {\n\n int N = 0;\n int R = 0;\n\n scanf(\"%d %d\", &N, &R);\n\n if (N < 10) {\n\n R = R + 100 * (10 - N);\n }\n\n printf(\"%d\\n\", R);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n\n io::stdin()\n .read_line(&mut buf)\n .expect(\"Failed to read line\");\n let vec_1: Vec<&str> = buf.split_whitespace().collect();\n let n: i32 = vec_1[0].parse().unwrap_or(0);\n let r: i32 = vec_1[1].parse().unwrap_or(0);\n let ans = if n >= 10 { r } else { r + 100 * (10 - n) };\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1066", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows:

\n
    \n
  • \n

    The first term s is given as input.

    \n
  • \n
  • \n

    Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.

    \n
  • \n
  • \n

    a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.

    \n
  • \n
\n

Find the minimum integer m that satisfies the following condition:

\n
    \n
  • There exists an integer n such that a_m = a_n (m > n).
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq s \\leq 100
  • \n
  • All values in input are integers.
  • \n
  • It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Print the minimum integer m that satisfies the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

a=\\{8,4,2,1,4,2,1,4,2,1,......\\}. As a_5=a_2, the answer is 5.

\n
\n
\n
\n
\n
\n

Sample Input 2

7\n
\n
\n
\n
\n
\n

Sample Output 2

18\n
\n

a=\\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\\}.

\n
\n
\n
\n
\n
\n

Sample Input 3

54\n
\n
\n
\n
\n
\n

Sample Output 3

114\n
\n
\n
", "c_code": "int solution() {\n int s = 0;\n scanf(\"%d\", &s);\n int count[1000000];\n for (int i = 0; i < 1000000; i++) {\n count[i] = 0;\n }\n int buf = s;\n int ans = 0;\n while (1) {\n count[buf]++;\n ans++;\n if (count[buf] > 1) {\n break;\n }\n if (buf % 2 == 0) {\n buf = buf / 2;\n } else {\n buf = 3 * buf + 1;\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\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 s: usize = iter.next().unwrap().parse().unwrap();\n\n let mut a: Vec = vec![s];\n\n let mut prev = s;\n\n if s == 1 || s == 2 || s == 4 {\n println!(\"{}\", 4);\n } else {\n loop {\n let next = if prev.is_multiple_of(2) {\n prev / 2\n } else {\n 3 * prev + 1\n };\n a.push(next);\n\n if next == 4 {\n break;\n }\n prev = next;\n }\n\n println!(\"{}\", a.len() + 3);\n }\n}", "difficulty": "medium"} {"problem_id": "1067", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.

\n

You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.

\n
\n
\n
\n
\n

Constraints

    \n
  • b is one of the letters A, C, G and T.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
b\n
\n
\n
\n
\n
\n

Output

Print the letter representing the base that bonds with the base b.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

A\n
\n
\n
\n
\n
\n

Sample Output 1

T\n
\n
\n
\n
\n
\n
\n

Sample Input 2

G\n
\n
\n
\n
\n
\n

Sample Output 2

C\n
\n
\n
", "c_code": "int solution(void) {\n char s[1];\n scanf(\"%s\", s);\n\n if (*s == 'A') {\n puts(\"T\");\n\n } else if (*s == 'T') {\n puts(\"A\");\n\n } else if (*s == 'C') {\n puts(\"G\");\n\n } else if (*s == 'G') {\n puts(\"C\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut b = String::new();\n\n std::io::stdin().read_line(&mut b).expect(\"failed\");\n\n let s = b.trim();\n match s {\n \"A\" => println!(\"T\"),\n \"T\" => println!(\"A\"),\n \"C\" => println!(\"G\"),\n \"G\" => println!(\"C\"),\n _ => println!(\"_\"),\n }\n}", "difficulty": "medium"} {"problem_id": "1068", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Some number of chocolate pieces were prepared for a training camp.\nThe camp had N participants and lasted for D days.\nThe i-th participant (1 \\leq i \\leq N) ate one chocolate piece on each of the following days in the camp: the 1-st day, the (A_i + 1)-th day, the (2A_i + 1)-th day, and so on.\nAs a result, there were X chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.

\n

Find the number of chocolate pieces prepared at the beginning of the camp.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq D \\leq 100
  • \n
  • 1 \\leq X \\leq 100
  • \n
  • 1 \\leq A_i \\leq 100 (1 \\leq i \\leq N)
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nD X\nA_1\nA_2\n:\nA_N\n
\n
\n
\n
\n
\n

Output

Find the number of chocolate pieces prepared at the beginning of the camp.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n7 1\n2\n5\n10\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

The camp has 3 participants and lasts for 7 days.\nEach participant eats chocolate pieces as follows:

\n
    \n
  • The first participant eats one chocolate piece on Day 1, 3, 5 and 7, for a total of four.
  • \n
  • The second participant eats one chocolate piece on Day 1 and 6, for a total of two.
  • \n
  • The third participant eats one chocolate piece only on Day 1, for a total of one.
  • \n
\n

Since the number of pieces remaining at the end of the camp is one, the number of pieces prepared at the beginning of the camp is 1 + 4 + 2 + 1 = 8.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n8 20\n1\n10\n
\n
\n
\n
\n
\n

Sample Output 2

29\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5\n30 44\n26\n18\n81\n18\n6\n
\n
\n
\n
\n
\n

Sample Output 3

56\n
\n
\n
", "c_code": "int solution(void) {\n\n static int n;\n static int d;\n static int x;\n scanf(\"%d%d%d\", &n, &d, &x);\n\n for (int i = 0; i < n; i++) {\n\n int a[i];\n scanf(\"%d\", &a[i]);\n\n x += ((d - 1) / a[i]) + 1;\n }\n\n printf(\"%d\\n\", x);\n\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n let l: i32 = vec[0].parse().unwrap();\n\n let scan = std::io::stdin();\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n let d: i32 = vec[0].parse().unwrap();\n let mut ans: i32 = vec[1].parse().unwrap();\n\n for _ in 0..l {\n let scan = std::io::stdin();\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n let a: i32 = vec[0].parse().unwrap();\n let mut cnt = 1;\n 'l1: loop {\n if cnt > d {\n break 'l1;\n }\n ans += 1;\n cnt += a;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1069", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

We have a tree with N vertices and N-1 edges, respectively numbered 1, 2,\\cdots, N and 1, 2, \\cdots, N-1. Edge i connects Vertex u_i and v_i.

\n

For integers L, R (1 \\leq L \\leq R \\leq N), let us define a function f(L, R) as follows:

\n
    \n
  • Let S be the set of the vertices numbered L through R. f(L, R) represents the number of connected components in the subgraph formed only from the vertex set S and the edges whose endpoints both belong to S.
  • \n
\n

Compute \\sum_{L=1}^{N} \\sum_{R=L}^{N} f(L, R).

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq u_i, v_i \\leq N
  • \n
  • The given graph is a tree.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nu_1 v_1\nu_2 v_2\n:\nu_{N-1} v_{N-1}\n
\n
\n
\n
\n
\n

Output

Print \\sum_{L=1}^{N} \\sum_{R=L}^{N} f(L, R).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 3\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

We have six possible pairs (L, R) as follows:

\n
    \n
  • For L = 1, R = 1, S = \\{1\\} and we have 1 connected component.
  • \n
  • For L = 1, R = 2, S = \\{1, 2\\} and we have 2 connected components.
  • \n
  • For L = 1, R = 3, S = \\{1, 2, 3\\} and we have 1 connected component, since S contains both endpoints of each of the edges 1, 2.
  • \n
  • For L = 2, R = 2, S = \\{2\\} and we have 1 connected component.
  • \n
  • For L = 2, R = 3, S = \\{2, 3\\} and we have 1 connected component, since S contains both endpoints of Edge 2.
  • \n
  • For L = 3, R = 3, S = \\{3\\} and we have 1 connected component.
  • \n
\n

The sum of these is 7.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n1 2\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10\n5 3\n5 7\n8 9\n1 9\n9 10\n8 4\n7 4\n6 10\n7 2\n
\n
\n
\n
\n
\n

Sample Output 3

113\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int u;\n int w;\n long long ans = 0;\n long long tmp;\n scanf(\"%d\", &N);\n for (i = 1; i <= N; i++) {\n ans += (long long)(N - i + 2) * (N - i + 1) / 2;\n }\n for (i = 1; i <= N - 1; i++) {\n scanf(\"%d %d\", &u, &w);\n if (u > w) {\n u ^= w;\n w ^= u;\n u ^= w;\n }\n ans -= (long long)u * (N - w + 1);\n }\n printf(\"%lld\\n\", ans);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\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 ans = 0;\n for i in 1..=n {\n ans += i * (n - i + 1);\n }\n\n for _ in 0..(n - 1) {\n let [u, v]: [usize; 2] = {\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 ans -= if u < v {\n u * (n - v + 1)\n } else {\n v * (n - u + 1)\n };\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1070", "problem_description": "Now it's time of Olympiads. Vanya and Egor decided to make his own team to take part in a programming Olympiad. They've been best friends ever since primary school and hopefully, that can somehow help them in teamwork.For each team Olympiad, Vanya takes his play cards with numbers. He takes only the cards containing numbers 1 and 0. The boys are very superstitious. They think that they can do well at the Olympiad if they begin with laying all the cards in a row so that: there wouldn't be a pair of any side-adjacent cards with zeroes in a row; there wouldn't be a group of three consecutive cards containing numbers one. Today Vanya brought n cards with zeroes and m cards with numbers one. The number of cards was so much that the friends do not know how to put all those cards in the described way. Help them find the required arrangement of the cards or else tell the guys that it is impossible to arrange cards in such a way.", "c_code": "int solution() {\n int x = 0;\n int n = 0;\n int m = 0;\n scanf(\"%d %d\", &n, &m);\n\n if (n > m + 1 || m > n * 2 + 2) {\n printf(\"-1\");\n return 0;\n }\n\n while (n + m) {\n if (x < 2 && m >= n) {\n printf(\"1\");\n m--;\n x++;\n } else {\n printf(\"0\");\n x = 0;\n n--;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut n = words[0];\n let mut m = words[1];\n\n let mut ans = String::new();\n\n while m > n && n > 0 && m > 0 {\n ans.push_str(\"110\");\n n -= 1;\n m -= 2;\n }\n\n if m >= n {\n while n > 0 && m > 0 {\n ans.push_str(\"10\");\n n -= 1;\n m -= 1;\n }\n } else {\n while n > 0 && m > 0 {\n ans.push_str(\"01\");\n n -= 1;\n m -= 1;\n }\n }\n\n if n > 1 || m > 2 {\n println!(\"-1\");\n return;\n }\n\n print!(\"{}\", ans);\n for _ in 0..m {\n print!(\"1\");\n }\n for _ in 0..n {\n print!(\"0\");\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "1071", "problem_description": "Given an array $$$a$$$ of length $$$n$$$, find another array, $$$b$$$, of length $$$n$$$ such that: for each $$$i$$$ $$$(1 \\le i \\le n)$$$ $$$MEX(\\{b_1$$$, $$$b_2$$$, $$$\\ldots$$$, $$$b_i\\})=a_i$$$. The $$$MEX$$$ of a set of integers is the smallest non-negative integer that doesn't belong to this set.If such array doesn't exist, determine this.", "c_code": "int solution() {\n long long int n;\n scanf(\"%lld\", &n);\n\n long long int i;\n long long int a[n];\n\n for (i = 0; i < n; ++i) {\n scanf(\"%lld\", &a[i]);\n }\n\n long long int *isin =\n (long long int *)malloc((pow(10, 6) + 1) * sizeof(long long int));\n long long int *sol = (long long int *)malloc(n * sizeof(long long int));\n\n for (i = 0; i < pow(10, 6) + 1; ++i) {\n isin[i] = 0;\n }\n for (i = 0; i < n; ++i) {\n sol[i] = -1;\n }\n\n for (i = 0; i < n; ++i) {\n isin[a[i]] = 1;\n }\n\n long long int cnt = 0;\n while (isin[cnt] != 0 && cnt <= pow(10, 6)) {\n cnt += 1;\n }\n sol[0] = cnt;\n ++cnt;\n\n for (i = 1; i < n; ++i) {\n if (a[i] != a[i - 1]) {\n sol[i] = a[i - 1];\n } else {\n if (cnt > pow(10, 6)) {\n sol[i] = cnt;\n ++cnt;\n } else {\n while (isin[cnt] != 0 && cnt <= pow(10, 6)) {\n cnt += 1;\n }\n sol[i] = cnt;\n ++cnt;\n }\n }\n }\n\n for (i = 0; i < n; ++i) {\n printf(\"%lld \", sol[i]);\n }\n printf(\"\\n\");\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n let n: 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 mut ys: Vec = vec![-1; n];\n let mut zs: Vec = Vec::new();\n for i in 1..n {\n if xs[i - 1] != xs[i] {\n ys[i] = xs[i - 1];\n zs.push(xs[i - 1]);\n }\n }\n zs.push(xs[n - 1]);\n let mut k: usize = 0;\n let mut next: i32 = 0;\n for i in 0..n {\n if ys[i] >= 0 {\n continue;\n }\n while next == zs[k] {\n next += 1;\n if k < zs.len() - 1 {\n k += 1;\n }\n }\n ys[i] = next;\n next += 1;\n }\n for y in ys {\n print!(\"{} \", y);\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "1072", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given a string S of length 2 or 3 consisting of lowercase English letters. If the length of the string is 2, print it as is; if the length is 3, print the string after reversing it.

\n
\n
\n
\n
\n

Constraints

    \n
  • The length of S is 2 or 3.
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If the length of S is 2, print S as is; if the length is 3, print S after reversing it.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

abc\n
\n
\n
\n
\n
\n

Sample Output 1

cba\n
\n

As the length of S is 3, we print it after reversing it.

\n
\n
\n
\n
\n
\n

Sample Input 2

ac\n
\n
\n
\n
\n
\n

Sample Output 2

ac\n
\n

As the length of S is 2, we print it as is.

\n
\n
", "c_code": "int solution(void) {\n char S[5];\n scanf(\"%s\", &S[0]);\n\n if (strlen(S) == 2) {\n printf(\"%c%c\", S[0], S[1]);\n return 0;\n }\n\n if (strlen(S) == 3) {\n printf(\"%c%c%c\", S[2], S[1], S[0]);\n return 0;\n }\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 if s.len() == 2 {\n print!(\"{}\", s[0] as char);\n println!(\"{}\", s[1] as char);\n } else {\n print!(\"{}\", s[2] as char);\n print!(\"{}\", s[1] as char);\n println!(\"{}\", s[0] as char);\n }\n}", "difficulty": "medium"} {"problem_id": "1073", "problem_description": "This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.", "c_code": "int solution() {\n char string[10009001];\n fgets(string, 10090001, stdin);\n int len = strlen(string);\n int stack[10000001];\n stack[0] = -1;\n int top = 0;\n int max_len = 0;\n int temp_len = 0;\n int count = 0;\n\n for (int i = 0; i < len - 1; i++) {\n if (string[i] == '(') {\n stack[++top] = i;\n } else {\n if (top > 0) {\n\n top -= 1;\n\n temp_len = i - stack[top];\n\n if (temp_len > max_len) {\n max_len = temp_len;\n count = 1;\n } else if (temp_len == max_len) {\n count += 1;\n }\n } else {\n top = 0;\n stack[top] = i;\n }\n }\n }\n if (max_len) {\n printf(\"%d %d\\n\", max_len, count);\n } else {\n printf(\"%d %d\\n\", 0, 1);\n }\n}", "rust_code": "fn solution() {\n let line = io::stdin().lock().lines().next().unwrap().unwrap();\n\n let mut cnt = 1;\n let mut max = 0;\n let mut xs: Vec = vec![-1];\n for (i, &c) in line.as_bytes().to_vec().iter().enumerate() {\n if c == b'(' {\n xs.push(i as i64);\n } else if xs.len() > 1 {\n xs.pop();\n let len = i as i64 - *xs.last().unwrap();\n if max == len {\n cnt += 1;\n } else if max < len {\n cnt = 1;\n max = len;\n }\n } else {\n xs[0] = i as i64;\n }\n }\n\n println!(\"{} {}\", max, cnt);\n}", "difficulty": "easy"} {"problem_id": "1074", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N squares aligned in a row.\nThe i-th square from the left contains an integer a_i.

\n

Initially, all the squares are white.\nSnuke will perform the following operation some number of times:

\n
    \n
  • Select K consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares are overwritten.
  • \n
\n

After Snuke finishes performing the operation, the score will be calculated as the sum of the integers contained in the black squares.\nFind the maximum possible score.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤10^5
  • \n
  • 1≤K≤N
  • \n
  • a_i is an integer.
  • \n
  • |a_i|≤10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N K\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible score.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n-10 10 -10 10 -10\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

Paint the following squares black: the second, third and fourth squares from the left.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 2\n10 -10 -10 10\n
\n
\n
\n
\n
\n

Sample Output 2

20\n
\n

One possible way to obtain the maximum score is as follows:

\n
    \n
  • Paint the following squares black: the first and second squares from the left.
  • \n
  • Paint the following squares black: the third and fourth squares from the left.
  • \n
  • Paint the following squares white: the second and third squares from the left.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 1\n-10\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
\n
\n
\n
\n

Sample Input 4

10 5\n5 -4 -5 -8 -4 7 2 -4 0 7\n
\n
\n
\n
\n
\n

Sample Output 4

17\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n int a[n];\n long long ans = 0;\n long long plus[n + 1];\n long long all = 0;\n long long sum = 0;\n long long tmp;\n plus[0] = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] > 0) {\n all += a[i];\n plus[i + 1] = plus[i] + a[i];\n } else {\n plus[i + 1] = plus[i];\n }\n if (i < k) {\n sum += a[i];\n }\n }\n for (int i = 0; i <= n - k; i++) {\n if (sum > 0) {\n tmp = all + plus[i] - plus[i + k] + sum;\n } else {\n tmp = all + plus[i] - plus[i + k];\n }\n if (ans < tmp) {\n ans = tmp;\n }\n sum = sum - a[i] + a[i + k];\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let (N, K): (usize, usize) = {\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 )\n };\n let a: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let opt = a.iter().filter(|&&x| x > 0).sum::();\n let w_sum = a[..K].iter().filter(|&&x| x < 0).sum::();\n let b_sum = a[..K].iter().filter(|&&x| x > 0).sum::();\n let (ans, _, _) = (K..N).fold(\n (std::cmp::max(opt + w_sum, opt - b_sum), w_sum, b_sum),\n |(res, w_sum, b_sum), i| {\n let w_sum_next = w_sum + std::cmp::min(0, a[i]) - std::cmp::min(0, a[i - K]);\n let b_sum_next = b_sum + std::cmp::max(0, a[i]) - std::cmp::max(0, a[i - K]);\n (\n *[res, opt + w_sum_next, opt - b_sum_next]\n .iter()\n .max()\n .unwrap(),\n w_sum_next,\n b_sum_next,\n )\n },\n );\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1075", "problem_description": "Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...$$$n$$$ friends live in a city which can be represented as a number line. The $$$i$$$-th friend lives in a house with an integer coordinate $$$x_i$$$. The $$$i$$$-th friend can come celebrate the New Year to the house with coordinate $$$x_i-1$$$, $$$x_i+1$$$ or stay at $$$x_i$$$. Each friend is allowed to move no more than once.For all friends $$$1 \\le x_i \\le n$$$ holds, however, they can come to houses with coordinates $$$0$$$ and $$$n+1$$$ (if their houses are at $$$1$$$ or $$$n$$$, respectively).For example, let the initial positions be $$$x = [1, 2, 4, 4]$$$. The final ones then can be $$$[1, 3, 3, 4]$$$, $$$[0, 2, 3, 3]$$$, $$$[2, 2, 5, 5]$$$, $$$[2, 1, 3, 5]$$$ and so on. The number of occupied houses is the number of distinct positions among the final ones.So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?", "c_code": "int solution() {\n long n;\n int i;\n scanf(\"%ld\", &n);\n int *p = (int *)malloc((n + 4) * sizeof(int));\n for (i = 0; i <= n + 1; i++) {\n p[i] = 0;\n }\n for (i = 0; i < n; i++) {\n int temp;\n scanf(\"%d\", &temp);\n p[temp]++;\n }\n long min = 0;\n long max = 0;\n for (i = 1; i <= n; i++) {\n if (p[i] >= 1) {\n min = min + 1;\n i += 2;\n }\n }\n printf(\"%ld\", min);\n\n for (i = 1; i <= n + 1; i++) {\n if (p[i] == 0) {\n continue;\n }\n if (p[i - 1] == 0) {\n p[i]--;\n max++;\n }\n if (p[i] >= 2) {\n p[i]--;\n p[i + 1]++;\n }\n if (p[i] >= 1) {\n max++;\n }\n }\n printf(\" %ld\", max);\n}", "rust_code": "fn solution() {\n let is_print = false;\n let mut input_str = String::new();\n\n let mut friend_count = 0_usize;\n match io::stdin().read_line(&mut input_str) {\n Ok(_n) => {\n if input_str.as_str() == \"\" {\n panic!(\"friend count number\");\n }\n\n friend_count = input_str.trim().parse::().unwrap();\n }\n Err(error) => panic!(\"error: {}\", error),\n }\n\n input_str = String::new();\n\n let mut localtion_vec: Vec<(i32, i32, i32)> = Vec::new();\n\n let max_vec_len = friend_count + 2;\n if is_print {\n println!(\"max count for friends {}\", max_vec_len);\n }\n let mut has_person_local: Vec = vec![0; max_vec_len];\n match io::stdin().read_line(&mut input_str) {\n Ok(_n) => {\n if input_str.as_str() == \"\" {\n panic!(\"friend \");\n }\n\n let voter_str_vec: Vec<&str> = input_str.split(' ').collect();\n let mut index = 0_usize;\n for location in voter_str_vec {\n index = location.trim().parse::().unwrap();\n has_person_local[index] += 1;\n }\n }\n Err(error) => panic!(\"error: {}\", error),\n }\n\n for index in 0..max_vec_len {\n let mut item: (i32, i32, i32) = (0, 0, 0);\n\n let mut perNum: i32 = 0;\n perNum += has_person_local[index];\n item.2 += has_person_local[index];\n\n if index > 0 {\n perNum += has_person_local[index - 1];\n }\n\n if index < (max_vec_len - 1) {\n perNum += has_person_local[index + 1];\n }\n item.0 = perNum;\n localtion_vec.push(item);\n }\n\n for index in 0..max_vec_len {\n let item = localtion_vec.get(index).unwrap();\n\n let mut valueNum = 0;\n valueNum += item.0;\n\n if index > 0 {\n let next_item = localtion_vec.get(index - 1).unwrap();\n valueNum += next_item.0;\n }\n\n if index < (max_vec_len - 1) {\n let pre_item = localtion_vec.get(index + 1).unwrap();\n valueNum += pre_item.0;\n }\n\n let item_mut = localtion_vec.get_mut(index).unwrap();\n item_mut.1 = valueNum;\n }\n\n let mut index = 0_usize;\n let mut min_house_num = 0;\n while index < max_vec_len {\n if is_print {\n println!(\"process the index {}\", index);\n }\n if has_person_local[index] == 0 {\n index += 1;\n continue;\n }\n\n let mut max_value_index = index;\n\n let mut maxPer = 0;\n let mut minValue = 1000000000;\n\n for i in 0..2 {\n max_value_index = index + i;\n\n if index + i >= max_vec_len {\n break;\n }\n\n let item = localtion_vec.get(index + i).unwrap();\n\n if item.0 > maxPer {\n max_value_index = index + i;\n maxPer = item.0;\n minValue = item.1;\n } else if item.0 == maxPer && item.1 < minValue {\n max_value_index = index + i;\n maxPer = item.0;\n minValue = item.1;\n }\n }\n\n min_house_num += 1;\n if is_print {\n println!(\"use house index {}\", max_value_index);\n }\n\n index = max_value_index + 2;\n }\n\n let mut max_house_num = 0;\n let mut min_person_local = has_person_local.clone();\n if is_print {\n println!(\"before min vec {:?}\", has_person_local);\n }\n\n for min_index in 0..max_vec_len {\n if min_person_local[min_index] == 0 {\n if min_index < (max_vec_len - 1) && min_person_local[min_index + 1] > 0 {\n min_person_local[min_index + 1] -= 1;\n min_person_local[min_index] += 1;\n max_house_num += 1;\n continue;\n }\n } else if min_person_local[min_index] > 1 {\n if has_person_local[min_index] > 0 {\n let mut temp_num = has_person_local[min_index] - 1;\n if has_person_local[min_index] < min_person_local[min_index] {\n temp_num = has_person_local[min_index];\n }\n\n if has_person_local[min_index] > min_person_local[min_index] {\n temp_num = min_person_local[min_index] - 1;\n }\n\n min_person_local[min_index] -= temp_num;\n min_person_local[min_index + 1] += temp_num;\n }\n\n max_house_num += 1;\n } else {\n max_house_num += 1;\n }\n }\n\n if is_print {\n println!(\"after min vec {:?}\", min_person_local);\n }\n println!(\"{} {}\", min_house_num, max_house_num);\n}", "difficulty": "hard"} {"problem_id": "1076", "problem_description": "The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi meters per second in any of the two directions along the road: south or north.You are to compute the minimum time needed to gather all the n friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n int a[n + 1];\n int b[n + 1];\n for (int i = 0; i < n; i++) {\n a[i] = 0;\n b[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &b[i]);\n }\n int north = 0;\n int south = 0;\n double time;\n if (a[0] > a[1]) {\n north = 0;\n south = 1;\n time = (double)(a[north] - a[south]) / (b[north] + b[south]);\n } else {\n north = 1;\n south = 0;\n time = (double)(a[north] - a[south]) / (b[north] + b[south]);\n }\n for (int i = 2; i < n; i++) {\n if (a[i] > a[north]) {\n if ((double)(a[i] - a[south]) / (b[i] + b[south]) > time) {\n time = (double)(a[i] - a[south]) / (b[i] + b[south]);\n if ((double)(a[i] - a[north]) / (b[north] + b[i]) > time) {\n south = north;\n north = i;\n time = (double)(a[i] - a[south]) / (b[south] + b[i]);\n continue;\n }\n north = i;\n }\n } else if (a[i] < a[south]) {\n if ((double)(a[north] - a[i]) / (b[north] + b[i]) > time) {\n time = (double)(a[north] - a[i]) / (b[north] + b[i]);\n if ((double)(a[south] - a[i]) / (b[south] + b[i]) > time) {\n north = south;\n south = i;\n time = (double)(a[north] - a[i]) / (b[north] + b[i]);\n continue;\n }\n south = i;\n }\n } else {\n if ((double)(a[i] - a[south]) / (b[i] + b[south]) > time) {\n time = (double)(a[i] - a[south]) / (b[i] + b[south]);\n if ((double)(a[north] - a[i]) / (b[north] + b[i]) > time) {\n south = i;\n time = (double)(a[north] - a[i]) / (b[north] + b[i]);\n continue;\n }\n north = i;\n } else if ((double)(a[north] - a[i]) / (b[north] + b[i]) > time) {\n south = i;\n time = (double)(a[north] - a[i]) / (b[north] + b[i]);\n }\n }\n }\n\n printf(\"%f\", time);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"\");\n let n = s.trim().parse::().unwrap();\n\n let mut input = io::stdin();\n let reader = BufReader::new(&mut input);\n\n let mut line = reader.lines().map(|a| a.expect(\"\"));\n\n let a: Vec = line\n .next()\n .unwrap()\n .trim()\n .split(' ')\n .map(|x| x.parse::().unwrap())\n .collect();\n let v: Vec = line\n .next()\n .unwrap()\n .trim()\n .split(' ')\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let (mut l, mut r) = (0_f64, 1e9_f64);\n for _ in 0..100 {\n let m = (l + r) / 2.0;\n let pl = l;\n let (mut ll, mut rr) = (-1e-233_f64, 1e233_f64);\n for i in 0..n {\n let l = a[i] - v[i] * m;\n let r = a[i] + v[i] * m;\n ll = ll.max(l);\n rr = rr.min(r);\n }\n if ll <= rr {\n r = m;\n } else {\n l = m;\n if (l - pl).abs() < 1e-6 {\n break;\n }\n }\n }\n println!(\"{:.10}\", l);\n}", "difficulty": "hard"} {"problem_id": "1077", "problem_description": "The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.", "c_code": "int solution() {\n int g[103] = {0};\n int b[103] = {0};\n int j;\n int r = 0;\n int i;\n int c;\n scanf(\"%d\", &c);\n for (i = 0; i < c; i++) {\n scanf(\"%d\", &j), b[j]++;\n }\n scanf(\"%d\", &c);\n for (i = 0; i < c; i++) {\n scanf(\"%d\", &j), g[j]++;\n }\n for (i = 1, r = 0; i < 101; i++) {\n while (b[i - 1] && g[i]) {\n r++;\n b[i - 1]--;\n g[i]--;\n }\n while (b[i] && g[i]) {\n r++;\n b[i]--;\n g[i]--;\n }\n while (b[i + 1] && g[i]) {\n r++;\n b[i + 1]--;\n g[i]--;\n }\n }\n printf(\"%d\", r);\n}", "rust_code": "fn solution() {\n let mut input = stdin();\n let _output = stdout();\n let mut buffer = [0; 4 * 2 * 100 + 100];\n let mut boys_girls = [0; 202];\n let len = input.read(&mut buffer).unwrap();\n\n let mut mi = 0;\n let mut pos = 0;\n let mut skip = true;\n let (mut n_s, mut n_e) = (0, 0);\n let mut num = 0;\n let (mut boys_len, mut girls_len) = (0, 0);\n for b in 0..len {\n match buffer[b] {\n b'\\r' => (),\n b'\\n' if skip => {\n skip = false;\n n_s = b + 1;\n n_e = n_s;\n }\n b'\\n' if !skip => {\n for i in n_s..n_e {\n num *= 10;\n num += buffer[i] - b'0';\n }\n boys_girls[mi + pos] = num;\n if boys_len == 0 {\n boys_len = pos + 1;\n } else {\n girls_len = pos + 1;\n }\n pos = 0;\n num = 0;\n skip = true;\n mi = 100;\n n_s = b + 1;\n n_e = n_s;\n }\n _ if skip => (),\n b' ' => {\n for i in n_s..n_e {\n num *= 10;\n num += buffer[i] - b'0';\n }\n boys_girls[mi + pos] = num;\n pos += 1;\n num = 0;\n n_s = b + 1;\n n_e = n_s;\n }\n _ => n_e += 1,\n }\n }\n boys_girls[..boys_len].sort();\n boys_girls[100..100 + girls_len].sort();\n\n let mut gi = 0;\n let mut bi = 0;\n let mut res = 0;\n loop {\n if gi >= girls_len || bi >= boys_len {\n break;\n }\n if boys_girls[100 + gi] <= boys_girls[bi] {\n if boys_girls[bi] - boys_girls[100 + gi] <= 1 {\n res += 1;\n bi += 1;\n }\n gi += 1;\n } else {\n if boys_girls[100 + gi] - boys_girls[bi] <= 1 {\n res += 1;\n gi += 1;\n }\n bi += 1;\n }\n }\n println!(\"{}\", res);\n}", "difficulty": "medium"} {"problem_id": "1078", "problem_description": "You are given a text consisting of n lines. Each line contains some space-separated words, consisting of lowercase English letters.We define a syllable as a string that contains exactly one vowel and any arbitrary number (possibly none) of consonants. In English alphabet following letters are considered to be vowels: 'a', 'e', 'i', 'o', 'u' and 'y'.Each word of the text that contains at least one vowel can be divided into syllables. Each character should be a part of exactly one syllable. For example, the word \"mamma\" can be divided into syllables as \"ma\" and \"mma\", \"mam\" and \"ma\", and \"mamm\" and \"a\". Words that consist of only consonants should be ignored.The verse patterns for the given text is a sequence of n integers p1, p2, ..., pn. Text matches the given verse pattern if for each i from 1 to n one can divide words of the i-th line in syllables in such a way that the total number of syllables is equal to pi.You are given the text and the verse pattern. Check, if the given text matches the given verse pattern.", "c_code": "int solution() {\n int N;\n int P[100];\n char Str[103];\n scanf(\"%d\", &N);\n for (int i = 0; i < N; ++i) {\n scanf(\"%d\", &P[i]);\n }\n scanf(\"\\n\");\n for (int i = 0; i < N; ++i) {\n fgets(Str, 102, stdin);\n int cnt = 0;\n for (int j = 0; Str[j] != '\\0'; ++j) {\n if (Str[j] == 'a' || Str[j] == 'e' || Str[j] == 'i' || Str[j] == 'o' ||\n Str[j] == 'u' || Str[j] == 'y') {\n ++cnt;\n }\n }\n if (cnt != P[i]) {\n puts(\"NO\");\n return 0;\n }\n }\n puts(\"YES\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let tokens: Vec<&str> = line.split_whitespace().collect();\n let n = tokens[0].parse().unwrap();\n\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let tokens: Vec<&str> = line.split_whitespace().collect();\n\n let mut v: Vec = vec![0; n];\n for i in 0..n {\n v[i] = tokens[i].parse().unwrap();\n }\n\n let mut yes: bool = true;\n for i in 0..n {\n let mut line = String::new();\n let mut cnt: i32 = 0;\n io::stdin().read_line(&mut line).unwrap();\n for c in line.chars() {\n if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y' {\n cnt += 1;\n }\n }\n if cnt != v[i] {\n yes = false;\n break;\n }\n }\n\n println!(\"{}\", if yes { \"YES\" } else { \"NO\" });\n}", "difficulty": "easy"} {"problem_id": "1079", "problem_description": "Mainak has an array $$$a_1, a_2, \\ldots, a_n$$$ of $$$n$$$ positive integers. He will do the following operation to this array exactly once: Pick a subsegment of this array and cyclically rotate it by any amount. Formally, he can do the following exactly once: Pick two integers $$$l$$$ and $$$r$$$, such that $$$1 \\le l \\le r \\le n$$$, and any positive integer $$$k$$$. Repeat this $$$k$$$ times: set $$$a_l=a_{l+1}, a_{l+1}=a_{l+2}, \\ldots, a_{r-1}=a_r, a_r=a_l$$$ (all changes happen at the same time). Mainak wants to maximize the value of $$$(a_n - a_1)$$$ after exactly one such operation. Determine the maximum value of $$$(a_n - a_1)$$$ that he can obtain.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int A[n];\n int ans = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", A + i);\n }\n for (int i = 0; i < n; i++) {\n int k = A[i] - A[(i + 1) % n];\n int k0 = A[i] - A[0];\n int kn = A[n - 1] - A[i];\n ans = k > ans ? k : ans;\n ans = k0 > ans ? k0 : ans;\n ans = kn > ans ? kn : ans;\n }\n\n printf(\"%d\\n\", ans);\n }\n}", "rust_code": "fn solution() {\n let mut input_line = String::new();\n io::stdin().read_line(&mut input_line).unwrap();\n let t: i32 = input_line.trim().parse().unwrap();\n for _ in 0..t {\n input_line.clear();\n io::stdin().read_line(&mut input_line).unwrap();\n input_line.clear();\n std::io::stdin().read_line(&mut input_line).unwrap();\n let a: Vec = input_line\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let (f, l, mut p, mut max_diff) = (a[0], a[a.len() - 1], a[0], 0);\n for ai in a[1..].iter() {\n max_diff = if (ai - f) > max_diff {\n ai - f\n } else {\n max_diff\n };\n max_diff = if (l - ai) > max_diff {\n l - ai\n } else {\n max_diff\n };\n max_diff = if (p - ai) > max_diff {\n p - ai\n } else {\n max_diff\n };\n p = *ai;\n }\n println!(\"{}\", max_diff);\n }\n}", "difficulty": "medium"} {"problem_id": "1080", "problem_description": "During a daily walk Alina noticed a long number written on the ground. Now Alina wants to find some positive number of same length without leading zeroes, such that the sum of these two numbers is a palindrome. Recall that a number is called a palindrome, if it reads the same right to left and left to right. For example, numbers $$$121, 66, 98989$$$ are palindromes, and $$$103, 239, 1241$$$ are not palindromes.Alina understands that a valid number always exist. Help her find one!", "c_code": "int solution() {\n int t;\n scanf(\"%d\\n\", &t);\n while (t--) {\n int n;\n scanf(\"%d\\n\", &n);\n char a[n];\n char c[n + 1];\n int b[n];\n scanf(\"%s\\n\", a);\n if (a[0] != 57) {\n for (int i = 0; i < n; i++) {\n printf(\"%d\", 57 - a[i]);\n }\n printf(\"\\n\");\n } else {\n for (int i = 0; i < n + 1; i++) {\n c[i] = 49;\n }\n for (int i = n - 1; i >= 0; i--) {\n if (c[i + 1] >= a[i]) {\n b[i] = c[i + 1] - a[i];\n } else if (c[i + 1] < a[i]) {\n c[i] -= 1;\n b[i] = 10 + c[i + 1] - a[i];\n }\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d\", b[i]);\n }\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n\n stdin().read_to_string(&mut buffer).unwrap();\n let input_params: Vec<&str> = buffer.split_whitespace().collect();\n let mut param_iter = input_params.iter();\n let t = param_iter.next().unwrap().parse::().unwrap();\n for _i in 0..t {\n let n = param_iter.next().unwrap().parse::().unwrap();\n let num = *param_iter.next().unwrap();\n let num_bytes = num.as_bytes();\n let mut result1: Vec = Vec::new();\n let mut result9: Vec = Vec::new();\n let mut carry = 1;\n for i in 1..=n {\n let mut result_digit = b'9' - num_bytes.get((n - i)).unwrap() + carry + 1;\n carry = 0;\n while result_digit >= 10 {\n result_digit -= 10;\n carry += 1;\n }\n result1.insert(0, b'0' + result_digit);\n }\n\n for byte in num_bytes {\n let result_digit = b'9' - byte;\n result9.push(b'0' + result_digit);\n }\n\n if carry > 0 {\n result1.insert(0, b'0' + carry);\n }\n\n if result1.len() == n {\n println!(\n \"{}\",\n std::str::from_utf8(&result1)\n .unwrap()\n .trim_start_matches('0')\n );\n } else {\n println!(\n \"{}\",\n std::str::from_utf8(&result9)\n .unwrap()\n .trim_start_matches('0')\n );\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1081", "problem_description": "

幹線道路 (Trunk Road)

\n\n

問題文

\n

\nJOI 市は,東西方向にまっすぐに伸びる H 本の道路と,南北方向にまっすぐに伸びる W 本の道路によって,碁盤の目の形に区分けされている.道路と道路の間隔は 1 である.JOI 市では,これら H+W 本の道路から,東西方向に 1 本,南北方向に 1 本,合計 2 本の道路を幹線道路として選ぶことになった.

\n\n

\n北から i 本目 (1≦i≦H) の道路と,西から j 本目 (1≦j≦W) の道路の交差点を,交差点 (i,j) とする.交差点 (i,j) と北から m 本目 (1≦m≦H) の道路の距離は |i-m| であり,交差点 (i,j) と西から n 本目 (1≦n≦W) の道路の距離は |j-n| である.\nまた,交差点 (i,j) の近くには A_{i,j} 人の住人が住んでいる.

\n\n

\n2 本の幹線道路を選んだときの,JOI 市の全ての住人に対する,最寄りの交差点から近い方の幹線道路への距離の総和の最小値を求めよ.

\n\n

制約

\n\n
    \n
  • 2 ≦ H ≦ 25
  • \n
  • 2 ≦ W ≦ 25
  • \n
  • 0 ≦ A_{i,j} ≦ 100 (1 ≦ i ≦ H, 1 ≦ j ≦ W)
  • \n
\n\n

入力・出力

\n\n

\n入力
\n入力は以下の形式で標準入力から与えられる.
\nH W
\nA_{1,1} A_{1,2} ... A_{1,W}
\n:
\nA_{H,1} A_{H,2} ... A_{H,W}\n

\n\n

\n出力
\nJOI 市の全ての住人に対する,最寄りの交差点から近い方の幹線道路への距離の総和の最小値を出力せよ.

\n\n\n\n\n

入出力例

\n\n\n入力例 1
\n
\n3 5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n
\n\n\n出力例 1
\n
\n8\n
\n\n

\n例えば,北から 2 本目の道路と西から 1 本目の道路を幹線道路とすればよい.

\n\n
\n\n\n入力例 2
\n
\n5 5\n1 2 3 1 5\n1 22 11 44 3\n1 33 41 53 2\n4 92 35 23 1\n4 2 6 3 5\n
\n\n\n出力例 2
\n
\n164\n
\n\n\n\n
\n

\n \"クリエイティブ・コモンズ・ライセンス\"\n
\n \n 情報オリンピック日本委員会作 『第 17 回日本情報オリンピック JOI 2017/2018 予選競技課題』\n

", "c_code": "int solution(void) {\n int H;\n int W;\n int A[101][101];\n int b;\n int c;\n int d;\n int j;\n int i;\n int t = 0;\n int sss = 0;\n scanf(\"%d %d\", &H, &W);\n for (b = 1; b <= H; b++) {\n for (c = 1; c <= W; c++) {\n scanf(\"%d\", &d);\n A[b][c] = d;\n };\n };\n for (b = 1; b <= H; b++) {\n for (c = 1; c <= W; c++) {\n for (j = 1; j <= H; j++) {\n for (i = 1; i <= W; i++) {\n if (abs(j - b) <= abs(i - c)) {\n t += A[j][i] * abs(j - b);\n } else {\n t += A[j][i] * abs(i - c);\n };\n };\n };\n if ((t <= sss) || ((b == 1) && (c == 1))) {\n sss = t;\n };\n t = 0;\n };\n };\n printf(\"%d\\n\", sss);\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut buf = String::new();\n let _ = stdin.read_to_string(&mut buf);\n let mut iter = buf.split_whitespace();\n\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.by_ref().take(w).map(|s| s.parse().unwrap()).collect())\n .collect();\n\n println!(\n \"{}\",\n (0..h)\n .map::(|ri| {\n (0..w)\n .map(|rj| {\n (0..h)\n .map::(|i| {\n let di = ri.abs_diff(i);\n (0..w)\n .map(|j| {\n let dj = rj.abs_diff(j);\n let d = min(di, dj);\n a[i][j] * d\n })\n .sum()\n })\n .sum()\n })\n .min()\n .unwrap()\n })\n .min()\n .unwrap()\n );\n}", "difficulty": "easy"} {"problem_id": "1082", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

Takahashi is solving quizzes. He has easily solved all but the last one.

\n

The last quiz has three choices: 1, 2, and 3.

\n

With his supernatural power, Takahashi has found out that the choices A and B are both wrong.

\n

Print the correct choice for this problem.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • Each of the numbers A and B is 1, 2, or 3.
  • \n
  • A and B are different.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
A\nB\n
\n
\n
\n
\n
\n

Output

\n

Print the correct choice.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

When we know 3 and 1 are both wrong, the correct choice is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n2\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
", "c_code": "int solution(void) {\n\n int A = 0;\n int B = 0;\n\n scanf(\"%d\", &A);\n scanf(\"%d\", &B);\n\n if (A != 1 && B != 1) {\n printf(\"1\\n\");\n } else if (A != 2 && B != 2) {\n printf(\"2\\n\");\n } else {\n printf(\"3\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n let _ = io::stdin().read_to_string(&mut input);\n let lines: Vec<&str> = input.split(\"\\n\").collect();\n let a = lines[0].parse::().unwrap();\n let b = lines[1].parse::().unwrap();\n\n println!(\"{}\", 6 - a - b);\n}", "difficulty": "hard"} {"problem_id": "1083", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are two buttons, one of size A and one of size B.

\n

When you press a button of size X, you get X coins and the size of that button decreases by 1.

\n

You will press a button twice. Here, you can press the same button twice, or press both buttons once.

\n

At most how many coins can you get?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 3 \\leq A, B \\leq 20
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the maximum number of coins you can get.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 4\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6 6\n
\n
\n
\n
\n
\n

Sample Output 3

12\n
\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n scanf(\"%d %d\", &a, &b);\n if (a == b) {\n printf(\"%d\\n\", a * 2);\n } else if (a > b) {\n printf(\"%d\\n\", (a * 2) - 1);\n } else {\n printf(\"%d\\n\", (b * 2) - 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut buf = String::new();\n let _ = stdin.read_line(&mut buf);\n let ns = buf\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect::>();\n let (a, b) = (ns[0], ns[1]);\n if a < b {\n println!(\"{}\", b * 2 - 1);\n } else if b < a {\n println!(\"{}\", a * 2 - 1);\n } else {\n println!(\"{}\", a * 2);\n }\n}", "difficulty": "easy"} {"problem_id": "1084", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

In order to pass the entrance examination tomorrow, Taro has to study for T more hours.

\n

Fortunately, he can leap to World B where time passes X times as fast as it does in our world (World A).

\n

While (X \\times t) hours pass in World B, t hours pass in World A.

\n

How many hours will pass in World A while Taro studies for T hours in World B?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq T \\leq 100
  • \n
  • 1 \\leq X \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
T X\n
\n
\n
\n
\n
\n

Output

Print the number of hours that will pass in World A.

\n

The output will be regarded as correct when its absolute or relative error from the judge's output is at most 10^{-3}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8 3\n
\n
\n
\n
\n
\n

Sample Output 1

2.6666666667\n
\n

While Taro studies for eight hours in World B where time passes three times as fast, 2.6666... hours will pass in World A.

\n

Note that an absolute or relative error of at most 10^{-3} is allowed.

\n
\n
\n
\n
\n
\n

Sample Input 2

99 1\n
\n
\n
\n
\n
\n

Sample Output 2

99.0000000000\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 100\n
\n
\n
\n
\n
\n

Sample Output 3

0.0100000000\n
\n
\n
", "c_code": "int solution(void) {\n float t = 0;\n float x = 0;\n scanf(\"%f %f\", &t, &x);\n printf(\"%f\\n\", t / x);\n return 0;\n}", "rust_code": "fn solution() {\n let mut tx = String::new();\n stdin().read_line(&mut tx).unwrap();\n let tx: Vec = tx.split_whitespace().flat_map(str::parse).collect();\n let t = tx[0] * 10000;\n let x = tx[1];\n println!(\"{}.{:>04}\", t / x / 10000, (t / x) % 10000);\n}", "difficulty": "medium"} {"problem_id": "1085", "problem_description": "The round carousel consists of $$$n$$$ figures of animals. Figures are numbered from $$$1$$$ to $$$n$$$ in order of the carousel moving. Thus, after the $$$n$$$-th figure the figure with the number $$$1$$$ follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the $$$i$$$-th figure equals $$$t_i$$$. The example of the carousel for $$$n=9$$$ and $$$t=[5, 5, 1, 15, 1, 5, 5, 1, 1]$$$. You want to color each figure in one of the colors. You think that it's boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly $$$k$$$ distinct colors, then the colors of figures should be denoted with integers from $$$1$$$ to $$$k$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n while (t--) {\n scanf(\"%d\", &n);\n int a[n];\n int b[n + 1];\n int edge = 0;\n int store = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n if (a[i] != a[(i + 1) % n]) {\n edge++;\n }\n if (a[i] == a[i + 1] && i != n - 1 && store == 0) {\n store = i + 1;\n }\n }\n if (edge == 0) {\n for (int i = 0; i < n; i++) {\n b[i] = 1;\n }\n b[n] = 1;\n } else {\n b[store] = 1;\n for (int i = store + 1; i < store + n; i++) {\n if (b[(i - 1) % n] == 1) {\n b[i % n] = 2;\n } else {\n b[i % n] = 1;\n }\n }\n b[n] = 2;\n if (store == 0) {\n if (b[n - 2] + b[0] == 3 && a[0] != a[n - 1] && a[n - 1] != a[n - 2]) {\n b[n - 1] = 3;\n b[n] = 3;\n }\n }\n }\n printf(\"%d\\n\", b[n]);\n for (int i = 0; i < n; i++) {\n printf(\"%d \", b[i]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lock = stdin.lock();\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n let q = line.trim().parse().unwrap();\n\n for _ in 0..q {\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n let t: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut all_same = true;\n for i in 1..t.len() {\n if t[i] != t[i - 1] {\n all_same = false;\n break;\n }\n }\n\n if all_same {\n println!(\"{}\", 1);\n\n for _ in 0..t.len() {\n print!(\"{} \", 1);\n }\n\n println!();\n continue;\n }\n\n let mut dp = Vec::with_capacity(t.len());\n dp.push((true, false));\n\n for i in 1..t.len() {\n if t[i] == t[i - 1] {\n dp.push((true, true));\n } else {\n dp.push((dp[i - 1].1, dp[i - 1].0));\n }\n }\n\n let mut sol = vec![0; t.len()];\n if dp.last().unwrap().1 {\n *sol.last_mut().unwrap() = 2;\n } else {\n *sol.last_mut().unwrap() = 1;\n }\n\n for i in (0..sol.len() - 1).rev() {\n if sol[i + 1] == 1 {\n sol[i] = if dp[i].1 { 2 } else { 1 };\n } else {\n sol[i] = if dp[i].0 { 1 } else { 2 };\n }\n }\n\n if *sol.last().unwrap() == 1 && t.last().unwrap() != t.first().unwrap() {\n *sol.last_mut().unwrap() = 3;\n }\n\n println!(\"{}\", if *sol.last().unwrap() == 3 { 3 } else { 2 });\n\n for i in sol {\n print!(\"{} \", i);\n }\n\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1086", "problem_description": "You are given an array $$$a_1, a_2, \\dots, a_n$$$, consisting of $$$n$$$ positive integers. Initially you are standing at index $$$1$$$ and have a score equal to $$$a_1$$$. You can perform two kinds of moves: move right — go from your current index $$$x$$$ to $$$x+1$$$ and add $$$a_{x+1}$$$ to your score. This move can only be performed if $$$x<n$$$. move left — go from your current index $$$x$$$ to $$$x-1$$$ and add $$$a_{x-1}$$$ to your score. This move can only be performed if $$$x>1$$$. Also, you can't perform two or more moves to the left in a row. You want to perform exactly $$$k$$$ moves. Also, there should be no more than $$$z$$$ moves to the left among them.What is the maximum score you can achieve?", "c_code": "int solution() {\n int t;\n int i;\n int n;\n int k;\n int z;\n int a[100000];\n int max;\n int ind;\n int b[100000];\n int s[100000];\n int temp;\n int p;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d%d%d\", &n, &k, &z);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n max = 0;\n s[0] = a[0];\n for (i = 1; i < n; i++) {\n if (a[i] + a[i - 1] > max) {\n max = a[i] + a[i - 1];\n b[i] = i - 1;\n } else {\n b[i] = b[i - 1];\n }\n s[i] = s[i - 1] + a[i];\n }\n max = 0;\n for (i = 1; i <= k; i++) {\n if ((k - i) / 2 <= z) {\n p = (k - i) / 2;\n } else {\n p = z;\n }\n temp = s[i] + (a[b[i]] + a[b[i] + 1]) * p;\n if ((k - i) % 2 == 1 && (k - i + 1) / 2 <= z) {\n temp += a[i - 1];\n }\n if (temp > max) {\n max = temp;\n }\n }\n printf(\"%d\\n\", max);\n for (i = 1; i < n; i++) {\n s[i] = 0;\n b[i] = 0;\n }\n }\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, k, z): (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\n let a: 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 = 0;\n for i in 0..=z {\n if k < 2 * i {\n break;\n }\n let sum = { a[0..=(k - 2 * i)].iter().sum::() };\n let max = {\n (0..=std::cmp::min(n - 2, k - 2 * i))\n .map(|j| a[j] + a[j + 1])\n .max()\n .unwrap()\n };\n ans = std::cmp::max(ans, sum + max * i);\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "1087", "problem_description": "This problem is actually a subproblem of problem G from the same contest.There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \\le a_i \\le n$$$).You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.You have to answer $$$q$$$ independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.", "c_code": "int solution() {\n long int q;\n long int n;\n long int i;\n long int j;\n long int k;\n long int l;\n long int f;\n long int c;\n scanf(\"%ld\", &q);\n for (i = 0; i < q; i++) {\n f = 0;\n c = 0;\n k = 0;\n scanf(\"%ld\", &n);\n long int ar[n];\n long int br[n];\n long int cr[n];\n for (j = 0; j < n; j++) {\n br[j] = 0;\n scanf(\"%ld\", &ar[j]);\n }\n for (j = 0; j < n; j++) {\n br[ar[j] - 1]++;\n }\n for (j = 0; j < n; j++) {\n\n f = 0;\n if (br[j] != 0) {\n while (br[j] != 0) {\n f = 0;\n for (l = 0; l < k; l++) {\n if (cr[l] == br[j]) {\n f = 1;\n break;\n }\n }\n if (f == 0) {\n break;\n }\n br[j]--;\n }\n if (br[j] != 0) {\n cr[k++] = br[j];\n c += br[j];\n }\n }\n }\n printf(\"%ld\\n\", c);\n }\n}", "rust_code": "fn solution() -> io::Result<()> {\n let stdin = io::stdin();\n let mut input = String::new();\n stdin.read_line(&mut input)?;\n let q: u32 = input.trim().parse().unwrap();\n\n for _ in 1..q + 1 {\n stdin.read_line(&mut input)?;\n\n let mut input = String::new();\n stdin.read_line(&mut input)?;\n let candies = input\n .split_whitespace()\n .map(|x| x.trim().parse::().unwrap());\n\n let mut map = HashMap::new();\n for candie in candies {\n if let std::collections::hash_map::Entry::Vacant(e) = map.entry(candie) {\n e.insert(1);\n } else {\n let x = map.get_mut(&candie).unwrap();\n *x += 1;\n }\n }\n\n let mut counts: Vec<_> = map.iter().collect();\n counts.sort_by(|x, y| y.1.cmp(x.1));\n\n let mut size = 0;\n let mut last_count = std::u32::MAX;\n\n for (_, &count) in counts {\n let i = min(last_count - 1, count);\n\n size += i;\n last_count = i;\n if last_count == 0 {\n break;\n }\n }\n\n println!(\"{:?}\", size);\n }\n\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "1088", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given a trapezoid. The lengths of its upper base, lower base, and height are a, b, and h, respectively.

\n
\n\n

An example of a trapezoid

\n
\n

Find the area of this trapezoid.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦a≦100
  • \n
  • 1≦b≦100
  • \n
  • 1≦h≦100
  • \n
  • All input values are integers.
  • \n
  • h is even.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
a\nb\nh\n
\n
\n
\n
\n
\n

Output

Print the area of the given trapezoid. It is guaranteed that the area is an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n4\n2\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

When the lengths of the upper base, lower base, and height are 3, 4, and 2, respectively, the area of the trapezoid is (3+4)×2/2 = 7.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n4\n4\n
\n
\n
\n
\n
\n

Sample Output 2

16\n
\n

In this case, a parallelogram is given, which is also a trapezoid.

\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int h = 0;\n int b = 0;\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n scanf(\"%d\", &h);\n printf(\"%d\\n\", (a + b) * h / 2);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let a: i64 = s.trim().parse().unwrap();\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let b: i64 = s.trim().parse().unwrap();\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let h: i64 = s.trim().parse().unwrap();\n\n println!(\"{}\", (a + b) * h / 2);\n}", "difficulty": "easy"} {"problem_id": "1089", "problem_description": "Alice and Bob are playing chess on a huge chessboard with dimensions $$$n \\times n$$$. Alice has a single piece left — a queen, located at $$$(a_x, a_y)$$$, while Bob has only the king standing at $$$(b_x, b_y)$$$. Alice thinks that as her queen is dominating the chessboard, victory is hers. But Bob has made a devious plan to seize the victory for himself — he needs to march his king to $$$(c_x, c_y)$$$ in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.Bob will win if he can move his king from $$$(b_x, b_y)$$$ to $$$(c_x, c_y)$$$ without ever getting in check. Remember that a king can move to any of the $$$8$$$ adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen. Find whether Bob can win or not.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%*d\");\n int ara[6];\n for (int i = 0; i < 6; i++) {\n scanf(\"%d\", &ara[i]);\n }\n if (((ara[0] - ara[2]) * (ara[0] - ara[4])) > 0) {\n n = 1;\n }\n if (n && ((ara[1] - ara[3]) * (ara[1] - ara[5])) > 0) {\n n = 1;\n } else {\n n = 0;\n }\n if (n) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\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 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 (ax, ay) = (arr[1], arr[2]);\n let (bx, by) = (arr[3], arr[4]);\n let (cx, cy) = (arr[5], arr[6]);\n let (dcx, dcy) = (cx - ax, cy - ay);\n let (dbx, dby) = (bx - ax, by - ay);\n let x_sign_same = (dcx > 0 && dbx > 0) || (dcx < 0 && dbx < 0);\n let y_sign_same = (dcy > 0 && dby > 0) || (dcy < 0 && dby < 0);\n if x_sign_same && y_sign_same {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "1090", "problem_description": "There is a grid with $$$n$$$ rows and $$$m$$$ columns, and three types of cells: An empty cell, denoted with '.'. A stone, denoted with '*'. An obstacle, denoted with the lowercase Latin letter 'o'. All stones fall down until they meet the floor (the bottom row), an obstacle, or other stone which is already immovable. (In other words, all the stones just fall down as long as they can fall.)Simulate the process. What does the resulting grid look like?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n char grid[n + 2][m + 3];\n for (int j = 0; j < n; j++) {\n char newlinechar;\n scanf(\"%1c\", &newlinechar);\n for (int k = 0; k < m; k++) {\n scanf(\"%1c\", &grid[j][k]);\n }\n }\n for (int k = 0; k < m; k++) {\n for (int j = n - 1; j >= 0; j--) {\n if (grid[j][k] == '*') {\n int height = j;\n grid[j][k] = '.';\n while (grid[height][k] != '*' && grid[height][k] != 'o' &&\n height < n) {\n height++;\n }\n grid[--height][k] = '*';\n }\n }\n }\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < m; k++) {\n printf(\"%c\", grid[j][k]);\n }\n printf(\"\\n\");\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n\n stdin().read_line(&mut line).unwrap();\n let t = line.trim().parse::().unwrap();\n\n for _tx in 0..t {\n line.clear();\n stdin().read_line(&mut line).unwrap();\n let mut nm = line.trim().split(\" \").filter(|x| !x.is_empty());\n\n let n = nm.next().unwrap().parse::().unwrap();\n let m = nm.next().unwrap().parse::().unwrap();\n\n let mut g: Vec> = Vec::new();\n\n for _nx in 0..n {\n line.clear();\n stdin().read_line(&mut line).unwrap();\n g.push(line.trim().chars().collect());\n }\n\n for j in 0..m {\n let mut b = 0;\n let mut c = 0;\n for i in 0..(n + 1) {\n if i == n || g[i][j] == 'o' {\n for i1 in b..(i - c) {\n g[i1][j] = '.';\n }\n for i1 in (i - c)..i {\n g[i1][j] = '*';\n }\n b = i + 1;\n c = 0;\n } else if g[i][j] == '*' {\n c += 1;\n }\n }\n }\n\n for r in g {\n for x in r {\n print!(\"{}\", x);\n }\n println!();\n }\n println!();\n }\n}", "difficulty": "easy"} {"problem_id": "1091", "problem_description": "Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It's hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it's like.Now let's imagine a typical morning in your family. You haven't woken up yet, and Mom is already going to work. She has been so hasty that she has nearly forgotten to leave the two of her darling children some money to buy lunches in the school cafeteria. She fished in the purse and found some number of coins, or to be exact, n coins of arbitrary values a1, a2, ..., an. But as Mom was running out of time, she didn't split the coins for you two. So she scribbled a note asking you to split the money equally.As you woke up, you found Mom's coins and read her note. \"But why split the money equally?\" — you thought. After all, your twin is sleeping and he won't know anything. So you decided to act like that: pick for yourself some subset of coins so that the sum of values of your coins is strictly larger than the sum of values of the remaining coins that your twin will have. However, you correctly thought that if you take too many coins, the twin will suspect the deception. So, you've decided to stick to the following strategy to avoid suspicions: you take the minimum number of coins, whose sum of values is strictly more than the sum of values of the remaining coins. On this basis, determine what minimum number of coins you need to take to divide them in the described manner.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n if (n <= 1) {\n printf(\"%d\", n);\n return 0;\n }\n int a[n];\n int sum = 0;\n int mymoney = 0;\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &a[i]);\n sum += a[i];\n }\n for (int c = 0; c < n - 1; c++) {\n for (int d = 0; d < n - c - 1; d++) {\n if (a[d] < a[d + 1]) {\n int swap = a[d];\n a[d] = a[d + 1];\n a[d + 1] = swap;\n }\n }\n }\n for (int i = 0; i < n; ++i) {\n if (mymoney <= sum / 2) {\n mymoney += a[i];\n } else {\n printf(\"%d\", i);\n return 0;\n }\n }\n printf(\"%d\", n);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input).expect(\"Input error\");\n\n input.clear();\n io::stdin().read_line(&mut input).expect(\"Input error\");\n\n let mut coins: Vec = input\n .trim()\n .split_ascii_whitespace()\n .map(|coin| coin.parse::().expect(\"Parsing error\"))\n .collect();\n\n coins.sort();\n coins.reverse();\n\n let mut total: i32 = coins.iter().sum();\n let mut sum = 0;\n let mut count = 0;\n\n for i in coins {\n sum += i;\n total -= i;\n count += 1;\n if sum > total {\n break;\n }\n }\n\n println!(\"{}\", count);\n}", "difficulty": "medium"} {"problem_id": "1092", "problem_description": "— Hey folks, how do you like this problem?— That'll do it. BThero is a powerful magician. He has got $$$n$$$ piles of candies, the $$$i$$$-th pile initially contains $$$a_i$$$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $$$(i, j)$$$ such that $$$1 \\le i, j \\le n$$$ and $$$i \\ne j$$$. All candies from pile $$$i$$$ are copied into pile $$$j$$$. Formally, the operation $$$a_j := a_j + a_i$$$ is performed. BThero can cast this spell any number of times he wants to — but unfortunately, if some pile contains strictly more than $$$k$$$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n int knum;\n scanf(\"%d\", &n);\n scanf(\"%d\", &knum);\n int arr[n];\n int pos = 0;\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &arr[j]);\n }\n int min = arr[0];\n for (int k = 1; k < n; k++) {\n if (min > arr[k]) {\n min = arr[k];\n pos = k;\n }\n }\n int sum = 0;\n for (int l = 0; l < n; l++) {\n if (pos != l && knum > arr[l]) {\n sum += (knum - arr[l]) / min;\n }\n }\n printf(\"%d\\n\", sum);\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n\n let t = input.trim().parse::().unwrap();\n\n for _i in 0..t {\n let mut n_k = String::new();\n io::stdin().read_line(&mut n_k).unwrap();\n\n let v = n_k.trim().split(\" \").collect::>();\n let _n = v[0].parse::().unwrap();\n let k = v[1].parse::().unwrap();\n\n let mut vals = String::new();\n io::stdin().read_line(&mut vals).unwrap();\n\n let mut vals = vals\n .trim()\n .split(\" \")\n .map(|e| e.parse::().unwrap())\n .collect::>();\n\n vals.sort();\n\n let mut m = 0;\n for (i1, v1) in vals.iter().enumerate().rev() {\n let mut b = 0;\n for (i2, v2) in vals.iter().enumerate() {\n if i1 == i2 {\n break;\n }\n\n let num = (k - *v1) / *v2;\n\n if num > b {\n b = num;\n }\n }\n\n m += b;\n }\n\n println!(\"{}\", m);\n }\n}", "difficulty": "medium"} {"problem_id": "1093", "problem_description": "You are given $$$n$$$ chips on a number line. The $$$i$$$-th chip is placed at the integer coordinate $$$x_i$$$. Some chips can have equal coordinates.You can perform each of the two following types of moves any (possibly, zero) number of times on any chip: Move the chip $$$i$$$ by $$$2$$$ to the left or $$$2$$$ to the right for free (i.e. replace the current coordinate $$$x_i$$$ with $$$x_i - 2$$$ or with $$$x_i + 2$$$); move the chip $$$i$$$ by $$$1$$$ to the left or $$$1$$$ to the right and pay one coin for this move (i.e. replace the current coordinate $$$x_i$$$ with $$$x_i - 1$$$ or with $$$x_i + 1$$$). Note that it's allowed to move chips to any integer coordinate, including negative and zero.Your task is to find the minimum total number of coins required to move all $$$n$$$ chips to the same coordinate (i.e. all $$$x_i$$$ should be equal after some sequence of moves).", "c_code": "int solution() {\n int n;\n int x[110];\n int sol = 0;\n int sol2 = 0;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &x[i]);\n if (x[i] % 2 == 0 && x[i] > 2) {\n x[i] = x[i] - x[i] + 2;\n }\n if (x[i] % 2 == 1 && x[i] > 1) {\n x[i] = x[i] - x[i] + 1;\n }\n }\n for (int i = 0; i < n; i++) {\n if (x[i] == 2) {\n sol++;\n }\n if (x[i] == 1) {\n sol2++;\n }\n }\n printf(\"%d\", sol > sol2 ? sol2 : sol);\n return 0;\n}", "rust_code": "fn solution() {\n let mut _buffer = String::new();\n io::stdin()\n .read_line(&mut _buffer)\n .expect(\"failed to read input\");\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let a = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n let even = a\n .iter()\n .fold(0, |acc, x| if x % 2 == 0 { acc + 1 } else { acc });\n let odd = a.len() - even;\n\n println!(\"{}\", even.min(odd));\n}", "difficulty": "medium"} {"problem_id": "1094", "problem_description": "You have an array $$$a_1, a_2, \\dots, a_n$$$. All $$$a_i$$$ are positive integers.In one step you can choose three distinct indices $$$i$$$, $$$j$$$, and $$$k$$$ ($$$i \\neq j$$$; $$$i \\neq k$$$; $$$j \\neq k$$$) and assign the sum of $$$a_j$$$ and $$$a_k$$$ to $$$a_i$$$, i. e. make $$$a_i = a_j + a_k$$$.Can you make all $$$a_i$$$ lower or equal to $$$d$$$ using the operation above any number of times (possibly, zero)?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int arr[101];\n while (t--) {\n int n;\n int d;\n scanf(\"%d%d\", &n, &d);\n int check = 1;\n int min = 101;\n int temp = -1;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] > d) {\n check = 0;\n }\n if (arr[i] < min) {\n min = arr[i];\n temp = i;\n }\n }\n if (check == 1) {\n printf(\"Yes\\n\");\n } else {\n check = 0;\n for (int i = 0; i < n; i++) {\n if (arr[i] + min <= d && i != temp) {\n check = 1;\n }\n }\n printf(\"%s\\n\", check ? \"Yes\" : \"No\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n\n let mut lines = input.lines();\n\n lines.next();\n\n while let Some(line) = lines.next() {\n let d: u8 = line.split_whitespace().nth(1).unwrap().parse().unwrap();\n let line = lines.next().unwrap();\n\n let mut v: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n v.sort();\n\n let ans = v.last().cloned().unwrap() <= d || v[0..2].iter().cloned().sum::() <= d;\n\n println!(\"{}\", if ans { \"YES\" } else { \"NO\" });\n }\n}", "difficulty": "medium"} {"problem_id": "1095", "problem_description": "Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size $$$n \\times n$$$ is called prime if the following three conditions are held simultaneously: all numbers on the square are non-negative integers not exceeding $$$10^5$$$; there are no prime numbers in the square; sums of integers in each row and each column are prime numbers. Sasha has an integer $$$n$$$. He asks you to find any prime square of size $$$n \\times n$$$. Sasha is absolutely sure such squares exist, so just help him!", "c_code": "int solution() {\n int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,\n 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};\n\n int T;\n scanf(\"%d\", &T);\n\n for (int C = 0; C < T; C++) {\n int n;\n scanf(\"%d\", &n);\n int p;\n for (int i = 0; i < 26; i++) {\n if (n < primes[i]) {\n p = primes[i - 1];\n break;\n }\n }\n int x = n - p;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if ((i <= j && j < i + x) || (n <= i + x && j < (i + x) % n)) {\n printf(\"0 \");\n } else {\n printf(\"1 \");\n }\n }\n printf(\"\\n\");\n }\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 for _i in 0..t {\n let mut n = String::new();\n stdin().read_line(&mut n).unwrap();\n let n: u32 = n.trim().parse().unwrap();\n for height in 0..n {\n for width in 0..n {\n if !n.is_multiple_of(2)\n && ((height == n / 2 && width == 0) || (width == n / 2 && height == 0))\n {\n print!(\"1 \");\n } else if height == width || height == (n - width - 1) {\n print!(\"1 \");\n } else {\n print!(\"0 \");\n }\n }\n println!();\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1096", "problem_description": "

Exhaustive Search

\n\n\n

\nWrite a program which reads a sequence A of n elements and an integer M, and outputs \"yes\" if you can make M by adding elements in A, otherwise \"no\". You can use an element only once.\n

\n

\nYou are given the sequence A and q questions where each question contains Mi.\n

\n\n

Input

\n\n

\nIn the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers (Mi) are given.\n

\n\n\n

Output

\n\n

\nFor each question Mi, print yes or no.\n

\n\n

Constraints

\n\n
    \n
  • n ≤ 20
  • \n
  • q ≤ 200
  • \n
  • 1 ≤ elements in A ≤ 2000
  • \n
  • 1 ≤ Mi ≤ 2000
  • \n
\n\n\n

Sample Input 1

\n
\n5\n1 5 7 10 21\n8\n2 4 17 8 22 21 100 35\n
\n\n

Sample Output 1

\n
\nno\nno\nyes\nyes\nyes\nyes\nno\nno\n
\n\n

Notes

\n\n

\nYou can solve this problem by a Burte Force approach. Suppose solve(p, t) is a function which checkes whether you can make t by selecting elements after p-th element (inclusive). Then you can recursively call the following functions:\n

\n\n

\nsolve(0, M)
\nsolve(1, M-{sum created from elements before 1st element})
\nsolve(2, M-{sum created from elements before 2nd element})
\n...\n

\n\n

\nThe recursive function has two choices: you selected p-th element and not. So, you can check solve(p+1, t-A[p]) and solve(p+1, t) in solve(p, t) to check the all combinations.\n

\n\n

\nFor example, the following figure shows that 8 can be made by A[0] + A[2].\n

\n\n
\n\n
\n\n", "c_code": "int solution() {\n int n;\n int i;\n int j;\n int k;\n int A[100];\n int B[3000];\n\n memset(B, 0, sizeof(B));\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", A + i);\n }\n\n B[0] = 1;\n for (i = 0; i < n; i++) {\n for (j = 2000; j >= 0; j--) {\n if (B[j] && j + A[i] <= 2000) {\n B[j + A[i]] = 1;\n }\n }\n }\n\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &k);\n if (B[k]) {\n puts(\"yes\");\n } else {\n puts(\"no\");\n }\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 let mut buf_it = buf.split_whitespace();\n\n let N = buf_it.next().unwrap().parse::().unwrap();\n let A = (0..N)\n .map(|_| buf_it.next().unwrap().parse::().unwrap())\n .collect::>();\n\n let Q = buf_it.next().unwrap().parse::().unwrap();\n let M = (0..Q)\n .map(|_| buf_it.next().unwrap().parse::().unwrap())\n .collect::>();\n\n let mut ans = HashSet::new();\n ans.insert(0);\n for &a in &A {\n let tmp = ans.iter().map(|&b| a + b).collect::>();\n ans = ans.union(&tmp).copied().collect();\n }\n\n for m in &M {\n println!(\"{}\", if ans.contains(m) { \"yes\" } else { \"no\" });\n }\n}", "difficulty": "hard"} {"problem_id": "1097", "problem_description": "You are given an array $$$a$$$ of length $$$n$$$, which initially is a permutation of numbers from $$$1$$$ to $$$n$$$. In one operation, you can choose an index $$$i$$$ ($$$1 \\leq i < n$$$) such that $$$a_i < a_{i + 1}$$$, and remove either $$$a_i$$$ or $$$a_{i + 1}$$$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $$$[1, 3, 2]$$$, you can choose $$$i = 1$$$ (since $$$a_1 = 1 < a_2 = 3$$$), then either remove $$$a_1$$$ which gives the new array $$$[3, 2]$$$, or remove $$$a_2$$$ which gives the new array $$$[1, 2]$$$.Is it possible to make the length of this array equal to $$$1$$$ with these operations?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int ns[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &ns[i]);\n }\n if (ns[0] <= ns[n - 1]) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let mut ms: Vec = vec![0; n];\n ms[n - 1] = xs[n - 1];\n for i in (0..(n - 1)).rev() {\n ms[i] = max(xs[i], ms[i + 1]);\n }\n let mut x = xs[0];\n let mut ok = true;\n for i in 1..n {\n if xs[i - 1] >= xs[i] && x >= ms[i] {\n ok = false;\n x = xs[i];\n }\n }\n println!(\"{}\", if ok { \"YES\" } else { \"NO\" });\n }\n}", "difficulty": "easy"} {"problem_id": "1098", "problem_description": "You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1.Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0.You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x.It is guaranteed that you have to color each vertex in a color different from 0.You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory).", "c_code": "int solution() {\n\n int n;\n scanf(\"%d\", &n);\n\n int p[n];\n p[0] = 1;\n int i;\n for (i = 1; i < n; i++) {\n scanf(\"%d\", &p[i]);\n }\n int c[n];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &c[i]);\n }\n\n int steps = n;\n\n for (i = 1; i < n; i++) {\n if (c[p[i] - 1] == c[i]) {\n steps--;\n }\n }\n\n printf(\"%d\\n\", steps);\n\n return 0;\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\n let n = get!(usize);\n let mut tree = vec![vec![]; n + 1];\n for i in 2..n + 1 {\n let p = get!(usize);\n tree[p].push(i);\n }\n let mut colors = vec![0; n + 1];\n for i in 1..n + 1 {\n let c = get!();\n colors[i] = c;\n }\n let mut tmp = vec![1];\n let mut current = vec![0; n + 1];\n let mut ans = 0;\n while !tmp.is_empty() {\n let x = tmp.pop().unwrap();\n let cl = colors[x];\n if current[x] != cl {\n ans += 1;\n current[x] = cl;\n }\n for &v in &tree[x] {\n current[v] = cl;\n tmp.push(v);\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1099", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i.

\n

Snuke will perform the following operation zero or more times:

\n
    \n
  • Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.
  • \n
\n

Find the number of possible final sequences of colors of the stones, modulo 10^9+7.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2\\times 10^5
  • \n
  • 1 \\leq C_i \\leq 2\\times 10^5(1\\leq i\\leq N)
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nC_1\n:\nC_N\n
\n
\n
\n
\n
\n

Output

Print the number of possible final sequences of colors of the stones, modulo 10^9+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1\n2\n1\n2\n2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

We can make three sequences of colors of stones, as follows:

\n
    \n
  • (1,2,1,2,2), by doing nothing.
  • \n
  • (1,1,1,2,2), by choosing the first and third stones to perform the operation.
  • \n
  • (1,2,2,2,2), by choosing the second and fourth stones to perform the operation.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

6\n4\n2\n5\n4\n2\n4\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
\n
\n
\n
\n
\n

Sample Input 3

7\n1\n3\n1\n2\n3\n3\n2\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int c[n + 1];\n int i;\n for (i = 1; i <= n; i++) {\n scanf(\"%d\", &c[i]);\n }\n int c_idx[200001];\n for (i = 1; i <= 200000; i++) {\n c_idx[i] = -1;\n }\n long long int ans[n + 1];\n ans[1] = 1;\n c_idx[c[1]] = 1;\n for (i = 2; i <= n; i++) {\n if (c_idx[c[i]] == i - 1 || c_idx[c[i]] == -1) {\n ans[i] = ans[i - 1];\n } else {\n ans[i] = (ans[i - 1] + ans[c_idx[c[i]]]) % 1000000007;\n }\n c_idx[c[i]] = i;\n }\n printf(\"%lld\\n\", ans[n]);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n v: [usize; n]\n }\n let d = 1000000007;\n let mut map: BTreeMap> = BTreeMap::new();\n for i in 0..n {\n map.entry(v[i]).or_default().push(i);\n }\n let css: Vec> = map.values().cloned().collect();\n let mut line: Vec = (0..n).collect();\n for cs in css {\n for i in 1..cs.len() {\n line[cs[i]] = cs[i - 1];\n }\n }\n let mut dp: Vec = vec![0; n];\n dp[0] = 1;\n for i in 1..n {\n dp[i] = dp[i - 1];\n if line[i] < i - 1 {\n dp[i] = (dp[i] + dp[line[i]]) % d;\n }\n }\n println!(\"{}\", dp[n - 1]);\n}", "difficulty": "medium"} {"problem_id": "1100", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

The development of algae in a pond is as follows.

\n

Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000, the following formula holds:

\n
    \n
  • x_{i+1} = rx_i - D
  • \n
\n

You are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ r ≤ 5
  • \n
  • 1 ≤ D ≤ 100
  • \n
  • D < x_{2000} ≤ 200
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
r D x_{2000}\n
\n
\n
\n
\n
\n

Output

Print 10 lines. The i-th line (1 ≤ i ≤ 10) should contain x_{2000+i} as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 10 20\n
\n
\n
\n
\n
\n

Sample Output 1

30\n50\n90\n170\n330\n650\n1290\n2570\n5130\n10250\n
\n

For example, x_{2001} = rx_{2000} - D = 2 \\times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \\times 30 - 10 = 50.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 40 60\n
\n
\n
\n
\n
\n

Sample Output 2

200\n760\n3000\n11960\n47800\n191160\n764600\n3058360\n12233400\n48933560\n
\n
\n
", "c_code": "int solution(void) {\n\n int r = 0;\n int d = 0;\n int x = 0;\n\n scanf(\"%d %d %d\", &r, &d, &x);\n\n for (int i = 1; i <= 10; i++) {\n x = r * x - d;\n printf(\"%d\\n\", x);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut ab = String::new();\n stdin().read_line(&mut ab).unwrap();\n let ab: Vec = ab.split_whitespace().flat_map(str::parse).collect();\n let (r, d, x) = (ab[0], ab[1], ab[2]);\n\n let mut v = x;\n for _ in 0..10 {\n v = r * v - d;\n println!(\"{}\", v);\n }\n}", "difficulty": "easy"} {"problem_id": "1101", "problem_description": "You work in the quality control department of technical support for a large company. Your job is to make sure all client issues have been resolved.Today you need to check a copy of a dialog between a client and a technical support manager. According to the rules of work, each message of the client must be followed by one or several messages, which are the answer of a support manager. However, sometimes clients ask questions so quickly that some of the manager's answers to old questions appear after the client has asked some new questions.Due to the privacy policy, the full text of messages is not available to you, only the order of messages is visible, as well as the type of each message: a customer question or a response from the technical support manager. It is guaranteed that the dialog begins with the question of the client.You have to determine, if this dialog may correspond to the rules of work described above, or the rules are certainly breached.", "c_code": "int solution() {\n int testcases;\n scanf(\"%d\", &testcases);\n\n int number_of_messages[testcases];\n\n int result[testcases];\n\n for (int i = 0; i < testcases; i++) {\n scanf(\"%d\", &number_of_messages[i]);\n\n char array[number_of_messages[i] + 1];\n\n scanf(\"%s\", array);\n\n result[i] = 0;\n\n for (int j = 0; j < number_of_messages[i]; j++) {\n if (*(array + j) == 'Q') {\n result[i]++;\n } else if (*(array + j) == 'A' && result[i] > 0) {\n result[i]--;\n }\n }\n }\n\n for (int i = 0; i < testcases; i++) {\n if (result[i] == 0) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut t = String::new();\n io::stdin().read_line(&mut t)?;\n let t = t.trim().parse::().unwrap();\n\n for _ in 0..t {\n let mut n = String::new();\n io::stdin().read_line(&mut n)?;\n let mut str = String::new();\n io::stdin().read_line(&mut str)?;\n\n let mut vec = Vec::new();\n\n for symb in str.chars() {\n if symb == 'Q' {\n vec.push(1);\n } else if symb == 'A' {\n vec.pop();\n }\n }\n\n if !vec.is_empty() {\n println!(\"No\");\n } else {\n println!(\"Yes\");\n }\n }\n\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "1102", "problem_description": "Sasha is taking part in a programming competition. In one of the problems she should check if some rooted trees are isomorphic or not. She has never seen this problem before, but, being an experienced participant, she guessed that she should match trees to some sequences and then compare these sequences instead of trees. Sasha wants to match each tree with a sequence a0, a1, ..., ah, where h is the height of the tree, and ai equals to the number of vertices that are at distance of i edges from root. Unfortunately, this time Sasha's intuition was wrong, and there could be several trees matching the same sequence. To show it, you need to write a program that, given the sequence ai, builds two non-isomorphic rooted trees that match that sequence, or determines that there is only one such tree.Two rooted trees are isomorphic, if you can reenumerate the vertices of the first one in such a way, that the index of the root becomes equal the index of the root of the second tree, and these two trees become equal.The height of a rooted tree is the maximum number of edges on a path from the root to any other vertex.", "c_code": "int solution() {\n int h;\n int A[100005];\n int i;\n int j;\n int flag = 0;\n scanf(\"%d\", &h);\n for (i = 0; i <= h; i++) {\n scanf(\"%d\", &A[i]);\n }\n for (i = 0; i < h; i++) {\n if (A[i] > 1 && A[i + 1] > 1) {\n flag = 1;\n }\n }\n if (flag == 0) {\n printf(\"perfect\\n\");\n } else {\n printf(\"ambiguous\\n\");\n int prlst = 0;\n int nwlst = 0;\n for (i = 0; i <= h; i++) {\n for (j = 0; j < A[i]; j++) {\n printf(\"%d \", prlst);\n nwlst++;\n }\n prlst = nwlst;\n }\n prlst = 0;\n nwlst = 0;\n flag = 0;\n printf(\"\\n\");\n for (i = 0; i <= h; i++) {\n if (flag == 1) {\n flag = 2;\n for (j = 0; j < A[i]; j++) {\n if (j % 2) {\n printf(\"%d \", prlst);\n } else {\n printf(\"%d \", prlst - 1);\n }\n nwlst++;\n }\n prlst = nwlst;\n } else {\n if (A[i] > 1 && A[i + 1] > 1 && flag == 0) {\n flag = 1;\n }\n for (j = 0; j < A[i]; j++) {\n printf(\"%d \", prlst);\n nwlst++;\n }\n prlst = nwlst;\n }\n }\n }\n return 0;\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\n let h = get!(usize);\n let mut tree = vec![0; h + 1];\n let mut n = 0;\n let mut flag = true;\n for i in 0..h + 1 {\n tree[i] = get!(usize);\n if i > 0 && tree[i - 1] > 1 && tree[i] > 1 {\n flag = false;\n }\n n += tree[i];\n }\n let tree = tree;\n let n = n;\n if flag {\n println!(\"perfect\");\n return;\n }\n let mut tmp = vec![0; n + 1];\n let mut x = 0;\n let mut p = 0;\n for i in 0..h + 1 {\n for i in 1..tree[i] + 1 {\n tmp[x + i] = x;\n }\n x += tree[i];\n if p == 0 && i < h && tree[i] > 1 && tree[i + 1] > 1 {\n p = x;\n }\n }\n let p = p;\n println!(\"ambiguous\");\n for i in 1..n + 1 {\n if i > 1 {\n print!(\" \");\n }\n print!(\"{}\", tmp[i]);\n }\n println!();\n tmp[p + 1] -= 1;\n for i in 1..n + 1 {\n if i > 1 {\n print!(\" \");\n }\n print!(\"{}\", tmp[i]);\n }\n println!();\n}", "difficulty": "easy"} {"problem_id": "1103", "problem_description": "Suppose you have two points $$$p = (x_p, y_p)$$$ and $$$q = (x_q, y_q)$$$. Let's denote the Manhattan distance between them as $$$d(p, q) = |x_p - x_q| + |y_p - y_q|$$$.Let's say that three points $$$p$$$, $$$q$$$, $$$r$$$ form a bad triple if $$$d(p, r) = d(p, q) + d(q, r)$$$.Let's say that an array $$$b_1, b_2, \\dots, b_m$$$ is good if it is impossible to choose three distinct indices $$$i$$$, $$$j$$$, $$$k$$$ such that the points $$$(b_i, i)$$$, $$$(b_j, j)$$$ and $$$(b_k, k)$$$ form a bad triple.You are given an array $$$a_1, a_2, \\dots, a_n$$$. Calculate the number of good subarrays of $$$a$$$. A subarray of the array $$$a$$$ is the array $$$a_l, a_{l + 1}, \\dots, a_r$$$ for some $$$1 \\le l \\le r \\le n$$$.Note that, according to the definition, subarrays of length $$$1$$$ and $$$2$$$ are good.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int result = 0;\n int tempp = 0;\n int tempn = 0;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n result += n + (n - 1);\n if (n > 2) {\n for (int i = 2; i < n; i++) {\n if (arr[i] >= arr[i - 1] && arr[i - 1] >= arr[i - 2]) {\n continue;\n }\n if (arr[i] <= arr[i - 1] && arr[i - 1] <= arr[i - 2]) {\n continue;\n }\n result++;\n }\n }\n if (n > 3) {\n for (int i = 0; i < (n - 3); i++) {\n if ((arr[i + 1] - arr[i] >= 0 && arr[i + 2] - arr[i + 1] >= 0) ||\n (arr[i + 1] - arr[i] <= 0 && arr[i + 2] - arr[i + 1] <= 0)) {\n continue;\n }\n if ((arr[i + 1] - arr[i] >= 0 && arr[i + 3] - arr[i + 1] >= 0) ||\n (arr[i + 1] - arr[i] <= 0 && arr[i + 3] - arr[i + 1] <= 0)) {\n continue;\n }\n if ((arr[i + 2] - arr[i] >= 0 && arr[i + 3] - arr[i + 2] >= 0) ||\n (arr[i + 2] - arr[i] <= 0 && arr[i + 3] - arr[i + 2] <= 0)) {\n continue;\n }\n if ((arr[i + 2] - arr[i + 1] >= 0 && arr[i + 3] - arr[i + 2] >= 0) ||\n (arr[i + 2] - arr[i + 1] <= 0 && arr[i + 3] - arr[i + 2] <= 0)) {\n continue;\n }\n result++;\n }\n }\n printf(\"%d\\n\", result);\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\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 '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 s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let mut a = Vec::with_capacity(n);\n a.extend(it);\n\n let mut ans = 0;\n\n ans += n as u64;\n ans += n as u64 - 1;\n\n for l in 3..5 {\n 'outer: for w in a.windows(l) {\n for i in 0..w.len() {\n for j in i + 1..w.len() {\n for k in j + 1..w.len() {\n if w[i] <= w[j] && w[j] <= w[k] || w[i] >= w[j] && w[j] >= w[k] {\n continue 'outer;\n }\n }\n }\n }\n\n ans += 1;\n }\n }\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1104", "problem_description": "Every December, VK traditionally holds an event for its employees named \"Secret Santa\". Here's how it happens.$$$n$$$ employees numbered from $$$1$$$ to $$$n$$$ take part in the event. Each employee $$$i$$$ is assigned a different employee $$$b_i$$$, to which employee $$$i$$$ has to make a new year gift. Each employee is assigned to exactly one other employee, and nobody is assigned to themselves (but two employees may be assigned to each other). Formally, all $$$b_i$$$ must be distinct integers between $$$1$$$ and $$$n$$$, and for any $$$i$$$, $$$b_i \\ne i$$$ must hold.The assignment is usually generated randomly. This year, as an experiment, all event participants have been asked who they wish to make a gift to. Each employee $$$i$$$ has said that they wish to make a gift to employee $$$a_i$$$.Find a valid assignment $$$b$$$ that maximizes the number of fulfilled wishes of the employees.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n const int mx = 2e5 + 5;\n int T;\n int n;\n int m;\n int p[mx];\n int ans[mx];\n int tong[mx];\n int q[mx];\n scanf(\"%d\", &n);\n m = 0;\n for (int i = 1; i <= n; i++) {\n tong[i] = ans[i] = 0;\n }\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &p[i]);\n if (!tong[p[i]]) {\n tong[p[i]] = i;\n ans[i] = p[i];\n } else {\n q[++m] = i;\n }\n }\n int k = 1;\n for (int i = 1; i <= n; i++) {\n if (!tong[i]) {\n ans[q[k++]] = i;\n }\n }\n for (int i = 1; i <= n; i++) {\n if (ans[i] == i) {\n int a = ans[i];\n int b = ans[tong[p[i]]];\n a = b + a;\n b = a - b;\n a = a - b;\n ans[i] = a;\n ans[tong[p[i]]] = b;\n }\n }\n printf(\"%d\\n\", n - m);\n for (int i = 1; i <= n; i++) {\n printf(\"%d \", ans[i]);\n }\n printf(\"\\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\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 _ in 0..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 mut a = Vec::with_capacity(n);\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n a.extend(it.map(|x| x - 1));\n\n let mut z = vec![Vec::new(); n];\n\n for (i, x) in a.into_iter().enumerate() {\n z[x].push(i);\n }\n\n let mut e = vec![None; n];\n let mut ans = 0u64;\n\n for (i, l) in z.iter().enumerate() {\n if let Some(x) = l.iter().min_by_key(|&&x| z[x].len()).cloned() {\n e[x] = Some(i);\n ans += 1;\n }\n }\n\n let mut i = 0;\n\n let mut s = Vec::new();\n let mut d = Vec::new();\n let mut b = Vec::new();\n\n while i < n && e[i].is_none() && z[i].is_empty() {\n i += 1;\n }\n\n for i in 0..n {\n match (e[i].is_some(), !z[i].is_empty()) {\n (false, false) => b.push(i),\n (false, true) => s.push(i),\n (true, false) => d.push(i),\n (true, true) => (),\n }\n }\n\n let s = s.into_iter().chain(b.iter().cloned());\n let d = d.into_iter().chain(b.iter().cloned()).cycle().skip(1);\n\n for (s, d) in s.zip(d) {\n e[s] = Some(d);\n }\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n for x in e {\n let x = x.unwrap() + 1;\n write!(&mut output, \"{} \", x).unwrap();\n }\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1105", "problem_description": "Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n\n for (int i = 0; i < T; i++) {\n int x;\n scanf(\"%d\", &x);\n\n if (x < 10) {\n printf(\"%d\\n\", x);\n } else if (x >= 10 && x < 100) {\n printf(\"%d\\n\", (9 + (x / 11)));\n } else if (x >= 100 && x < 1000) {\n printf(\"%d\\n\", (18 + (x / 111)));\n } else if (x >= 1000 && x < 10000) {\n printf(\"%d\\n\", (27 + (x / 1111)));\n } else if (x >= 10000 && x < 100000) {\n printf(\"%d\\n\", (36 + (x / 11111)));\n } else if (x >= 100000 && x < 1000000) {\n printf(\"%d\\n\", (45 + (x / 111111)));\n } else if (x >= 1000000 && x < 10000000) {\n printf(\"%d\\n\", (54 + (x / 1111111)));\n } else if (x >= 10000000 && x < 100000000) {\n printf(\"%d\\n\", (63 + (x / 11111111)));\n } else if (x >= 100000000 && x < 1000000000) {\n printf(\"%d\\n\", (72 + (x / 111111111)));\n } else {\n printf(\"81\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let reader = &mut io::BufReader::new(io::stdin());\n let writer = &mut io::BufWriter::new(io::stdout());\n let mut buf = String::new();\n\n reader.read_line(&mut buf).unwrap();\n let t = buf.trim_end().parse::().unwrap();\n\n for _i in 0..t {\n buf.clear();\n reader.read_line(&mut buf).unwrap();\n let n = buf.trim_end();\n let n_num = n.parse::().unwrap();\n let n_len = n.len() as i32;\n let first_digit = (n.as_bytes()[0] - 48u8) as i32;\n\n let mut ret = (n_len - 1) * 9 + first_digit - 1;\n if first_digit\n .to_string()\n .repeat(n_len as usize)\n .parse::()\n .unwrap()\n <= n_num\n {\n ret += 1;\n }\n\n writeln!(writer, \"{}\", ret).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1106", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan).\nIn how many ways can we select some of these coins so that they are X yen in total?

\n

Coins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that coin are different.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq A, B, C \\leq 50
  • \n
  • A + B + C \\geq 1
  • \n
  • 50 \\leq X \\leq 20 000
  • \n
  • A, B and C are integers.
  • \n
  • X is a multiple of 50.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A\nB\nC\nX\n
\n
\n
\n
\n
\n

Output

Print the number of ways to select coins.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n2\n2\n100\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

There are two ways to satisfy the condition:

\n
    \n
  • Select zero 500-yen coins, one 100-yen coin and zero 50-yen coins.
  • \n
  • Select zero 500-yen coins, zero 100-yen coins and two 50-yen coins.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5\n1\n0\n150\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Note that the total must be exactly X yen.

\n
\n
\n
\n
\n
\n

Sample Input 3

30\n40\n50\n6000\n
\n
\n
\n
\n
\n

Sample Output 3

213\n
\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int c = 0;\n int x = 0;\n int count = 0;\n scanf(\"%d %d %d %d\", &a, &b, &c, &x);\n\n for (int i = 0; i <= a; i++) {\n for (int j = 0; j <= b; j++) {\n for (int k = 0; k <= c; k++) {\n if (x == 500 * i + 100 * j + 50 * k) {\n count++;\n }\n }\n }\n }\n\n printf(\"%d\\n\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut nums: Vec = Vec::with_capacity(4);\n for _ in 0..4 {\n let mut buf: String = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n nums.push(buf.trim().parse::().unwrap());\n }\n let x = nums[3];\n let mut a = min(x / 500, nums[0]);\n let mut cnt = 0;\n while a >= 0 {\n let mut b = min((x - a * 500) / 100, nums[1]);\n while b >= 0 {\n let c = (x - a * 500 - b * 100) / 50;\n if c > nums[2] {\n break;\n }\n cnt += 1;\n b -= 1;\n }\n a -= 1;\n }\n println!(\"{}\", cnt);\n}", "difficulty": "medium"} {"problem_id": "1107", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.

\n

These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.

\n

We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \\leq i,j,x,y \\leq N:

\n
    \n
  • If (i+j) \\% 3=(x+y) \\% 3, the color of (i,j) and the color of (x,y) are the same.
  • \n
  • If (i+j) \\% 3 \\neq (x+y) \\% 3, the color of (i,j) and the color of (x,y) are different.
  • \n
\n

Here, X \\% Y represents X modulo Y.

\n

We will repaint zero or more squares so that the grid will be a good grid.

\n

For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.

\n

Find the minimum possible sum of the wrongness of all the squares.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 500
  • \n
  • 3 \\leq C \\leq 30
  • \n
  • 1 \\leq D_{i,j} \\leq 1000 (i \\neq j),D_{i,j}=0 (i=j)
  • \n
  • 1 \\leq c_{i,j} \\leq C
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N C\nD_{1,1} ... D_{1,C}\n:\nD_{C,1} ... D_{C,C}\nc_{1,1} ... c_{1,N}\n:\nc_{N,1} ... c_{N,N}\n
\n
\n
\n
\n
\n

Output

If the minimum possible sum of the wrongness of all the squares is x, print x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3\n0 1 1\n1 0 1\n1 4 0\n1 2\n3 3\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n
    \n
  • Repaint (1,1) to Color 2. The wrongness of (1,1) becomes D_{1,2}=1.
  • \n
  • Repaint (1,2) to Color 3. The wrongness of (1,2) becomes D_{2,3}=1.
  • \n
  • Repaint (2,2) to Color 1. The wrongness of (2,2) becomes D_{3,1}=1.
  • \n
\n

In this case, the sum of the wrongness of all the squares is 3.

\n

Note that D_{i,j} \\neq D_{j,i} is possible.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 3\n0 12 71\n81 0 53\n14 92 0\n1 1 2 1\n2 1 1 2\n2 2 1 3\n1 1 2 2\n
\n
\n
\n
\n
\n

Sample Output 2

428\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int c;\n scanf(\"%d %d\", &n, &c);\n\n int d[c + 1][c + 1];\n for (int i = 1; i <= c; i++) {\n for (int j = 1; j <= c; j++) {\n scanf(\"%d\", &d[i][j]);\n }\n }\n\n int k[n + 1][n + 1];\n int waru1[31] = {0};\n int waru2[31] = {0};\n int waru0[31] = {0};\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= n; j++) {\n scanf(\"%d\", &k[i][j]);\n if ((i + j) % 3 == 1) {\n waru1[k[i][j]]++;\n } else if ((i + j) % 3 == 2) {\n waru2[k[i][j]]++;\n } else {\n waru0[k[i][j]]++;\n }\n }\n }\n\n int min = 1000000000;\n for (int i = 1; i <= c; i++) {\n for (int j = 1; j <= c; j++) {\n for (int x = 1; x <= c; x++) {\n if (i != j && j != x && x != i) {\n int tas = 0;\n for (int y = 1; y <= c; y++) {\n tas += waru1[y] * d[y][i];\n tas += waru2[y] * d[y][j];\n tas += waru0[y] * d[y][x];\n }\n\n if (min > tas) {\n min = tas;\n }\n }\n }\n }\n }\n\n printf(\"%d\\n\", min);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N: usize,\n C: usize,\n D: [[usize; C]; C],\n c: [[usize1; N]; N],\n }\n\n let mut counts = vec![vec![0; 3]; C];\n for x in 0..N {\n for y in 0..N {\n counts[c[x][y]][(x + y + 2) % 3] += 1;\n }\n }\n\n let mut min = std::usize::MAX;\n for c0 in 0..C {\n for c1 in 0..C {\n if c1 == c0 {\n continue;\n }\n for c2 in 0..C {\n if c2 == c1 || c2 == c0 {\n continue;\n }\n let mut sum = 0;\n let cc = [c0, c1, c2];\n for ind in 0..3 {\n for c3 in 0..C {\n sum += D[c3][cc[ind]] * counts[c3][ind];\n }\n }\n min = cmp::min(min, sum);\n }\n }\n }\n\n println!(\"{}\", min);\n}", "difficulty": "easy"} {"problem_id": "1108", "problem_description": "Ezzat has an array of $$$n$$$ integers (maybe negative). He wants to split it into two non-empty subsequences $$$a$$$ and $$$b$$$, such that every element from the array belongs to exactly one subsequence, and the value of $$$f(a) + f(b)$$$ is the maximum possible value, where $$$f(x)$$$ is the average of the subsequence $$$x$$$. A sequence $$$x$$$ is a subsequence of a sequence $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) elements.The average of a subsequence is the sum of the numbers of this subsequence divided by the size of the subsequence.For example, the average of $$$[1,5,6]$$$ is $$$(1+5+6)/3 = 12/3 = 4$$$, so $$$f([1,5,6]) = 4$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int max = -1000000001;\n long long int sum = 0;\n double avg = 0.0;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] > max) {\n max = arr[i];\n }\n sum += arr[i];\n }\n sum = sum - max;\n avg = (sum * 1.0) / (n - 1) + (max * 1.0);\n printf(\"%0.9f\\n\", avg);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n std::io::stdin().read_line(&mut input).unwrap();\n\n let test_count: usize = input.trim().parse().unwrap();\n\n for _test in 0..test_count {\n input.clear();\n std::io::stdin().read_line(&mut input).unwrap();\n\n input.clear();\n std::io::stdin().read_line(&mut input).unwrap();\n\n let mut max: f64 = f64::NEG_INFINITY;\n let mut sum: f64 = 0.0;\n let mut count: usize = 0;\n input\n .split_ascii_whitespace()\n .map(str::parse)\n .map(Result::unwrap)\n .for_each(|x| {\n if x > max {\n max = x\n }\n sum += x;\n count += 1;\n });\n let average = (sum - max) / (count - 1) as f64;\n println!(\"{}\", average + max);\n }\n}", "difficulty": "medium"} {"problem_id": "1109", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

We have two distinct integers A and B.

\n

Print the integer K such that |A - K| = |B - K|.

\n

If such an integer does not exist, print IMPOSSIBLE instead.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 0 \\leq A,\\ B \\leq 10^9
  • \n
  • A and B are distinct.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the integer K satisfying the condition.

\n

If such an integer does not exist, print IMPOSSIBLE instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 16\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

|2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition.

\n
\n
\n
\n
\n
\n

Sample Input 2

0 3\n
\n
\n
\n
\n
\n

Sample Output 2

IMPOSSIBLE\n
\n

No integer satisfies the condition.

\n
\n
\n
\n
\n
\n

Sample Input 3

998244353 99824435\n
\n
\n
\n
\n
\n

Sample Output 3

549034394\n
\n
\n
", "c_code": "int solution(void) {\n long A = 0;\n long B = 0;\n long sa = 0;\n scanf(\"%ld %ld\", &A, &B);\n sa = A + B;\n if (sa < 0) {\n sa *= -1;\n }\n if (sa % 2 != 0) {\n printf(\"IMPOSSIBLE\\n\");\n return 0;\n }\n printf(\"%ld\\n\", sa / 2);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut ab = String::new();\n stdin().read_line(&mut ab).unwrap();\n let mut ab: Vec = ab.split_whitespace().flat_map(str::parse).collect();\n ab.sort();\n let (a, b) = (ab[0], ab[1]);\n\n if (b - a) % 2 == 0 {\n println!(\"{}\", a + (b - a) / 2);\n } else {\n println!(\"IMPOSSIBLE\");\n }\n}", "difficulty": "easy"} {"problem_id": "1110", "problem_description": "

Maximum Sum Sequence

\n\n

\nGiven a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence.\n

\n\n

Input

\n

\nThe input consists of multiple datasets. Each data set consists of:\n\n

\nn\na1\na2\n.\n.\nan\n
\n\n

\nYou can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000.\n

\n\n

\nThe input end with a line consisting of a single 0.\n

\n\n

Output

\n\n

\nFor each dataset, print the maximum sum in a line.\n

\n\n

Sample Input

\n
\n7\n-5\n-1\n6\n4\n9\n-6\n-7\n13\n1\n2\n3\n2\n-2\n-1\n1\n2\n3\n2\n1\n-2\n1\n3\n1000\n-200\n201\n0\n
\n\n

Output for the Sample Input

\n\n
\n19\n14\n1001\n
", "c_code": "int solution(void) {\n\n int max = -25000000;\n int n = 0;\n int sum = 0;\n int i = 0;\n int j = 0;\n int k = 0;\n\n while (1) {\n scanf(\"%d\", &n);\n int a[n];\n if (n == 0) {\n break;\n }\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n for (i = 0; i < n; i++) {\n for (j = i; j < n; j++) {\n sum = sum + a[j];\n if (max < sum) {\n max = sum;\n }\n }\n sum = 0;\n }\n printf(\"%d\\n\", max);\n max = -25000000;\n }\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut row = String::new();\n io::stdin().read_line(&mut row).ok();\n let row: usize = row.trim().parse().unwrap();\n if row == 0 {\n break;\n }\n\n let mut v: Vec = Vec::new();\n for _ in 0..row {\n let mut input = String::new();\n io::stdin().read_line(&mut input).ok();\n let input: isize = input.trim().parse().unwrap();\n v.push(input);\n }\n\n let mut sum = 0;\n let mut min = 0;\n let mut max = -99999999;\n for z in 0..row {\n sum += v[z];\n max = cmp::max(max, sum - min);\n min = cmp::min(min, sum);\n }\n\n println!(\"{}\", max);\n }\n}", "difficulty": "hard"} {"problem_id": "1111", "problem_description": "You are given an array $$$a[0 \\ldots n-1]$$$ of length $$$n$$$ which consists of non-negative integers. Note that array indices start from zero.An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all $$$i$$$ ($$$0 \\le i \\le n - 1$$$) the equality $$$i \\bmod 2 = a[i] \\bmod 2$$$ holds, where $$$x \\bmod 2$$$ is the remainder of dividing $$$x$$$ by 2.For example, the arrays [$$$0, 5, 2, 1$$$] and [$$$0, 17, 0, 3$$$] are good, and the array [$$$2, 4, 6, 7$$$] is bad, because for $$$i=1$$$, the parities of $$$i$$$ and $$$a[i]$$$ are different: $$$i \\bmod 2 = 1 \\bmod 2 = 1$$$, but $$$a[i] \\bmod 2 = 4 \\bmod 2 = 0$$$.In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).Find the minimum number of moves in which you can make the array $$$a$$$ good, or say that this is not possible.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n\n int odd = 0;\n int eve = 0;\n for (int i = 0; i < n; i++) {\n if (i % 2 != arr[i] % 2) {\n if (arr[i] % 2 == 0) {\n eve++;\n } else {\n odd++;\n }\n }\n }\n int sub = 0;\n if (odd == eve) {\n if (odd > 0 && eve > 0) {\n sub = odd;\n }\n } else {\n sub = -1;\n }\n printf(\"%d\\n\", sub);\n }\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 a: 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 count = [0, 0];\n for i in 0..n {\n if i % 2 != a[i] % 2 {\n count[i % 2] += 1;\n }\n }\n\n if count[0] == count[1] {\n println!(\"{}\", count[0]);\n } else {\n println!(\"-1\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1112", "problem_description": "

ソーシャルゲーム (Social Game)

\n\n\n\n

問題文

\n\n

\nJOI 君は明日から新たにソーシャルゲームを始めることにした.\n

\n\n

\nこのソーシャルゲームでは,1 日につき 1 回までログインすることができ,ログインするたびに A 枚のコインが得られる.\n

\n\n

\nまた,月曜日から日曜日まで 7 日連続でログインすると,そのたびに,追加で B 枚のコインが得られる.\n

\n\n

\nこれ以外にコインがもらえることはない.\n

\n\n

\n明日は月曜日である.JOI 君が少なくとも C 枚のコインを得るためにログインしなければならない回数の最小値を求めよ.\n

\n\n\n

制約

\n\n
    \n
  • 1 ≦ A ≦ 1000
  • \n
  • 0 ≦ B ≦ 1000
  • \n
  • 1 ≦ C ≦ 1000000 (= 10^6)
  • \n
\n\n

入力・出力

\n\n

\n入力
\n入力は以下の形式で標準入力から与えられる.
\nA B C\n

\n\n

\n出力
\nJOI 君が少なくとも C 枚のコインを得るためにログインしなければならない回数の最小値を出力せよ.\n

\n\n\n\n

入出力例

\n\n

入力例 1

\n
\n3 0 10\n
\n\n

出力例 1

\n
\n4\n
\n\n
    \n
  • 1 回のログインあたり 3 枚のコインが得られ,10 枚のコインを集めたい.
  • \n
  • JOI 君は,月曜日から連続 4 日間ログインすることで 12 枚のコインが得られる.
  • \n
  • 3 回以下のログインで 10 枚以上のコインを得ることはできないので,JOI 君がログインしなければならない回数の最小値は 4 である.従って,4 を出力する.
  • \n
\n

\n\n

入力例 2

\n
\n1 2 10\n
\n\n

出力例 2

\n
\n8\n
\n\n
    \n
  • 1 回のログインあたり 1 枚のコインが得られる.それとは別に,1 週間連続でログインすることで 2 枚のコインが得られる.10 枚のコインを集めたい.
  • \n
  • 月曜日から日曜日まで連続でログインすると,日々のコイン 7 枚に加えて,2 枚のコインが得られるため,合計 9 枚のコインが得られる.従って,更にもう 1 回ログインすることにより,10 枚のコインが得られる.
  • \n
  • 7 回以下のログインで 10 枚以上のコインを得ることはできないので,JOI 君がログインしなければならない回数の最小値は 8 である.従って,8 を出力する.
  • \n
\n\n
\n", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int c = 0;\n int sum = 0;\n int i = 0;\n\n scanf(\"%d %d %d\", &a, &b, &c);\n\n while (sum < c) {\n i++;\n sum += a;\n if ((i % 7) == 0) {\n sum += b;\n }\n }\n\n printf(\"%d\\n\", i);\n\n return 0;\n}", "rust_code": "fn solution() {\n let nums: Vec = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s.trim_end()\n .to_owned()\n .split(' ')\n .map(|s| s.parse::().unwrap())\n .collect()\n };\n let check1: i32 = nums[2] % (7 * nums[0] + nums[1]);\n let check2: i32 = check1 / nums[0];\n let mut ans: i32 = 7 * (nums[2] / (7 * nums[0] + nums[1]));\n if check1 != 0 {\n if check2 >= 7 {\n ans += 7;\n } else {\n if check1 % nums[0] == 0 {\n ans += check2;\n } else {\n ans = ans + check2 + 1;\n }\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1113", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given a string S.

\n

Takahashi can insert the character A at any position in this string any number of times.

\n

Can he change S into AKIHABARA?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 50
  • \n
  • S consists of uppercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If it is possible to change S into AKIHABARA, print YES; otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

KIHBR\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

Insert one A at each of the four positions: the beginning, immediately after H, immediately after B and the end.

\n
\n
\n
\n
\n
\n

Sample Input 2

AKIBAHARA\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n

The correct spell is AKIHABARA.

\n
\n
\n
\n
\n
\n

Sample Input 3

AAKIAHBAARA\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
", "c_code": "int solution() {\n char s[51];\n scanf(\"%s\", s);\n int l = strlen(s);\n if (l >= 10) {\n printf(\"NO\");\n return 0;\n }\n if (l == 9 && strcmp(s, \"AKIHABARA\") == 0) {\n printf(\"YES\");\n return 0;\n }\n if (l == 8 && (strcmp(s, \"AKIHABAR\") == 0 || strcmp(s, \"AKIHABRA\") == 0 ||\n strcmp(s, \"AKIHBARA\") == 0 || strcmp(s, \"KIHABARA\") == 0)) {\n printf(\"YES\");\n return 0;\n }\n if (l == 7 && (strcmp(s, \"AKIHABR\") == 0 || strcmp(s, \"AKIHBAR\") == 0 ||\n strcmp(s, \"KIHABAR\") == 0 || strcmp(s, \"AKIHBRA\") == 0 ||\n strcmp(s, \"KIHABRA\") == 0 || strcmp(s, \"KIHBARA\") == 0)) {\n printf(\"YES\");\n return 0;\n }\n if (l == 6 && (strcmp(s, \"KIHBRA\") == 0 || strcmp(s, \"KIHBAR\") == 0 ||\n strcmp(s, \"KIHABR\") == 0 || strcmp(s, \"AKIHBR\") == 0)) {\n printf(\"YES\");\n return 0;\n }\n if (l == 5 && strcmp(s, \"KIHBR\") == 0) {\n printf(\"YES\");\n return 0;\n }\n printf(\"NO\");\n\n return 0;\n}", "rust_code": "fn solution() {\n input!(s: String);\n let vs = vec![\n \"AKIHABARA\",\n \"KIHABARA\",\n \"AKIHBARA\",\n \"KIHBARA\",\n \"AKIHABRA\",\n \"KIHABRA\",\n \"AKIHBRA\",\n \"KIHBRA\",\n \"AKIHABAR\",\n \"KIHABAR\",\n \"AKIHBAR\",\n \"KIHBAR\",\n \"AKIHABR\",\n \"KIHABR\",\n \"AKIHBR\",\n \"KIHBR\",\n ];\n\n let s: &str = &s;\n let ok = vs.contains(&s);\n\n println!(\"{}\", if ok { \"YES\" } else { \"NO\" });\n}", "difficulty": "medium"} {"problem_id": "1114", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have a string S of length N consisting of uppercase English letters.

\n

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 50
  • \n
  • S consists of uppercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print number of occurrences of ABC in S as contiguous subsequences.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10\nZABCDBABCQ\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

\n
\n
\n
\n
\n
\n

Sample Input 2

19\nTHREEONEFOURONEFIVE\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

No contiguous subsequences of S are equal to ABC.

\n
\n
\n
\n
\n
\n

Sample Input 3

33\nABCCABCBABCCABACBCBBABCBCBCBCABCB\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution(void) {\n\n int N = 0;\n int count = 0;\n\n scanf(\"%d\", &N);\n\n char S[N];\n\n scanf(\"%s\", S);\n\n for (int i = 0; i < (N - 2); i++) {\n\n if (S[i] == 'A' && S[i + 1] == 'B' && S[i + 2] == 'C') {\n\n count++;\n }\n }\n printf(\"%d\\n\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let (_n, s): (i32, String) = {\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 iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().to_string(),\n )\n };\n\n println!(\"{}\", s.matches(\"ABC\").collect::>().len());\n}", "difficulty": "medium"} {"problem_id": "1115", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Kurohashi has never participated in AtCoder Beginner Contest (ABC).

\n

The next ABC to be held is ABC N (the N-th ABC ever held).\nKurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.

\n

What is the earliest ABC where Kurohashi can make his debut?

\n
\n
\n
\n
\n

Constraints

    \n
  • 100 \\leq N \\leq 999
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If the earliest ABC where Kurohashi can make his debut is ABC n, print n.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

111\n
\n
\n
\n
\n
\n

Sample Output 1

111\n
\n

The next ABC to be held is ABC 111, where Kurohashi can make his debut.

\n
\n
\n
\n
\n
\n

Sample Input 2

112\n
\n
\n
\n
\n
\n

Sample Output 2

222\n
\n

The next ABC to be held is ABC 112, which means Kurohashi can no longer participate in ABC 111.\nAmong the ABCs where Kurohashi can make his debut, the earliest one is ABC 222.

\n
\n
\n
\n
\n
\n

Sample Input 3

750\n
\n
\n
\n
\n
\n

Sample Output 3

777\n
\n
\n
", "c_code": "int solution(void) {\n\n int n = 0;\n int x[10] = {111, 222, 333, 444, 555, 666, 777, 888, 999};\n\n scanf(\"%d\", &n);\n\n if (n <= x[0]) {\n n = x[0];\n } else if (n > x[0] && n <= x[1]) {\n n = x[1];\n } else if (n > x[1] && n <= x[2]) {\n n = x[2];\n } else if (n > x[2] && n <= x[3]) {\n n = x[3];\n } else if (n > x[3] && n <= x[4]) {\n n = x[4];\n } else if (n > x[4] && n <= x[5]) {\n n = x[5];\n } else if (n > x[5] && n <= x[6]) {\n n = x[6];\n } else if (n > x[6] && n <= x[7]) {\n n = x[7];\n } else if (n > x[7] && n <= x[8]) {\n n = x[8];\n }\n\n printf(\"%d\\n\", n);\n return 0;\n}", "rust_code": "fn solution() {\n let handle = std::io::stdin();\n let mut buf = String::new();\n handle.lock().read_line(&mut buf).unwrap();\n let num: usize = usize::from_str(buf.trim()).unwrap();\n if num < 112 {\n println!(\"111\");\n } else if num < 223 {\n println!(\"222\");\n } else if num < 334 {\n println!(\"333\");\n } else if num < 445 {\n println!(\"444\");\n } else if num < 556 {\n println!(\"555\");\n } else if num < 667 {\n println!(\"666\");\n } else if num < 778 {\n println!(\"777\");\n } else if num < 889 {\n println!(\"888\");\n } else if num < 1000 {\n println!(\"999\");\n } else {\n println!(\"u r wrong\");\n }\n}", "difficulty": "medium"} {"problem_id": "1116", "problem_description": "Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n int a = 0;\n int b = 0;\n int c = 0;\n int n = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; ++i) {\n scanf(\"%d %d %d %d\", &a, &b, &c, &n);\n if (a >= b && a >= c) {\n if (n < a - b + a - c) {\n printf(\"NO\\n\");\n continue;\n }\n printf(((n - (a - b) - (a - c)) % 3 == 0) ? \"YES\\n\" : \"NO\\n\");\n } else if (b >= a && b >= c) {\n if (n < b - a + b - c) {\n printf(\"NO\\n\");\n continue;\n }\n printf(((n - (b - a) - (b - c)) % 3 == 0) ? \"YES\\n\" : \"NO\\n\");\n } else {\n if (n < c - b + c - a) {\n printf(\"NO\\n\");\n continue;\n }\n printf(((n - (c - b) - (c - a)) % 3 == 0) ? \"YES\\n\" : \"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n\n for t in stdin.lock().lines().skip(1) {\n let t: Vec = t.unwrap().split(' ').map(|x| x.parse().unwrap()).collect();\n\n if let [a, b, c, n] = t[..] {\n let m = std::cmp::max(a, std::cmp::max(b, c));\n let r = n - (m - a) - (m - b) - (m - c);\n\n if r >= 0 && r % 3 == 0 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1117", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.\nThe i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

\n

Answer the following Q queries:

\n
    \n
  • Query i (1 \\leq i \\leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A, B \\leq 10^5
  • \n
  • 1 \\leq Q \\leq 10^5
  • \n
  • 1 \\leq s_1 < s_2 < ... < s_A \\leq 10^{10}
  • \n
  • 1 \\leq t_1 < t_2 < ... < t_B \\leq 10^{10}
  • \n
  • 1 \\leq x_i \\leq 10^{10}
  • \n
  • s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B Q\ns_1\n:\ns_A\nt_1\n:\nt_B\nx_1\n:\nx_Q\n
\n
\n
\n
\n
\n

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 4\n100\n600\n400\n900\n1000\n150\n2000\n899\n799\n
\n
\n
\n
\n
\n

Sample Output 1

350\n1400\n301\n399\n
\n

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

\n
    \n
  • Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.
  • \n
  • Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.
  • \n
  • Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.
  • \n
  • Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

1 1 3\n1\n10000000000\n2\n9999999999\n5000000000\n
\n
\n
\n
\n
\n

Sample Output 2

10000000000\n10000000000\n14999999998\n
\n

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.

\n
\n
", "c_code": "int solution() {\n int i;\n int A;\n int B;\n int Q;\n long long s[100002];\n long long t[100002];\n long long x[100001];\n scanf(\"%d %d %d\", &A, &B, &Q);\n for (i = 1; i <= A; i++) {\n scanf(\"%lld\", &(s[i]));\n }\n for (i = 1; i <= B; i++) {\n scanf(\"%lld\", &(t[i]));\n }\n for (i = 1; i <= Q; i++) {\n scanf(\"%lld\", &(x[i]));\n }\n s[A + 1] = (long long)1 << 60;\n s[0] = -s[A + 1];\n t[B + 1] = s[A + 1];\n t[0] = -s[A + 1];\n\n int l[2];\n int r[2];\n int m;\n long long ans;\n for (i = 1; i <= Q; i++) {\n l[0] = 0;\n r[0] = A + 1;\n while (r[0] - l[0] > 1) {\n m = (r[0] + l[0]) / 2;\n if (s[m] < x[i]) {\n l[0] = m;\n } else {\n r[0] = m;\n }\n }\n\n l[1] = 0;\n r[1] = B + 1;\n while (r[1] - l[1] > 1) {\n m = (r[1] + l[1]) / 2;\n if (t[m] < x[i]) {\n l[1] = m;\n } else {\n r[1] = m;\n }\n }\n\n ans = x[i] - ((s[l[0]] < t[l[1]]) ? s[l[0]] : t[l[1]]);\n if (((s[r[0]] < t[r[1]]) ? t[r[1]] : s[r[0]]) - x[i] < ans) {\n ans = ((s[r[0]] < t[r[1]]) ? t[r[1]] : s[r[0]]) - x[i];\n }\n if (s[r[0]] - t[l[1]] + s[r[0]] - x[i] < ans) {\n ans = s[r[0]] - t[l[1]] + s[r[0]] - x[i];\n }\n if (s[r[0]] - t[l[1]] + x[i] - t[l[1]] < ans) {\n ans = s[r[0]] - t[l[1]] + x[i] - t[l[1]];\n }\n if (t[r[1]] - s[l[0]] + t[r[1]] - x[i] < ans) {\n ans = t[r[1]] - s[l[0]] + t[r[1]] - x[i];\n }\n if (t[r[1]] - s[l[0]] + x[i] - s[l[0]] < ans) {\n ans = t[r[1]] - s[l[0]] + x[i] - s[l[0]];\n }\n printf(\"%lld\\n\", ans);\n }\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input!(\n a: usize,\n b: usize,\n q: usize,\n s: [i64; a],\n t: [i64; b],\n query: [i64; q],\n );\n\n let shrine: Vec = vec![std::i64::MIN]\n .into_iter()\n .chain(s)\n .chain(vec![std::i64::MAX])\n .collect();\n\n let temple: Vec = vec![std::i64::MIN]\n .into_iter()\n .chain(t)\n .chain(vec![std::i64::MAX])\n .collect();\n\n for qi in query.iter() {\n let sh = shrine.binary_search(qi);\n let tm = temple.binary_search(qi);\n\n let mut scand = vec![];\n let mut tcand = vec![];\n\n match sh {\n Ok(x) => {\n scand.push(shrine[x]);\n }\n Err(x) => {\n if x - 1 != 0 {\n scand.push(shrine[x - 1]);\n }\n if x != a + 1 {\n scand.push(shrine[x]);\n }\n }\n }\n\n match tm {\n Ok(x) => {\n tcand.push(temple[x]);\n }\n Err(x) => {\n if x - 1 != 0 {\n tcand.push(temple[x - 1]);\n }\n if x != b + 1 {\n tcand.push(temple[x]);\n }\n }\n }\n\n let mut ans: i64 = std::i64::MAX;\n for si in scand.iter() {\n for ti in tcand.iter() {\n if si < qi && ti < qi {\n ans = cmp::min(ans, qi - cmp::min(si, ti));\n } else if si >= qi && ti < qi {\n ans = cmp::min(ans, si - ti + cmp::min(si - qi, qi - ti));\n } else if si < qi && ti >= qi {\n ans = cmp::min(ans, ti - si + cmp::min(ti - qi, qi - si));\n } else {\n ans = cmp::min(ans, cmp::max(si, ti) - qi);\n }\n }\n }\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1118", "problem_description": "Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.", "c_code": "int solution()\n\n{\n int n;\n int i;\n int j;\n int sum = 0;\n int c = 0;\n int min;\n int a = 0;\n int k;\n\n scanf(\"%d\", &n);\n\n int x[n];\n\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &x[i]);\n }\n\n for (j = 0; j < n; j++) {\n\n min = 1000000001;\n\n for (i = 0; i < n; i++) {\n\n if (x[i] >= sum && x[i] - sum < min) {\n min = x[i] - sum;\n k = i;\n a = 1;\n if (!min) {\n break;\n }\n }\n }\n\n if (!a) {\n break;\n }\n c++;\n sum += x[k];\n x[k] = 0;\n a = 0;\n }\n\n printf(\"%d\", c);\n}", "rust_code": "fn solution() {\n io::stdin().read_line(&mut String::new()).unwrap();\n\n let mut line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n\n let mut words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n words.sort();\n\n let mut suma = 0;\n let mut answer = 0;\n for i in words {\n if suma <= i {\n suma += i;\n answer += 1;\n }\n }\n\n println!(\"{}\", answer);\n}", "difficulty": "hard"} {"problem_id": "1119", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given integers A and B, each between 1 and 3 (inclusive).

\n

Determine if there is an integer C between 1 and 3 (inclusive) such that A \\times B \\times C is an odd number.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A, B \\leq 3
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

If there is an integer C between 1 and 3 that satisfies the condition, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Let C = 3. Then, A \\times B \\times C = 3 \\times 1 \\times 3 = 9, which is an odd number.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 2\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2 2\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n scanf(\"%d %d\", &a, &b);\n if ((a * b % 2) == 0) {\n printf(\"No\");\n } else {\n printf(\"Yes\");\n }\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n let mut reader = BufReader::new(reader.lock());\n\n let mut s = String::new();\n let _ = reader.read_line(&mut s);\n let v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n if v[0] % 2 == 1 && v[1] % 2 == 1 {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "1120", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}.
\nSnuke, an employee, would like to play with this sequence.

\n

Specifically, he would like to repeat the following operation as many times as possible:

\n
For every i satisfying 1 \\leq i \\leq N, perform one of the following: \"divide a_i by 2\" and \"multiply a_i by 3\".  \nHere, choosing \"multiply a_i by 3\" for every i is not allowed, and the value of a_i after the operation must be an integer.\n
\n

At most how many operations can be performed?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • N is an integer between 1 and 10 \\ 000 (inclusive).
  • \n
  • a_i is an integer between 1 and 1 \\ 000 \\ 000 \\ 000 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 a_3 ... a_N\n
\n
\n
\n
\n
\n

Output

\n

Print the maximum number of operations that Snuke can perform.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n5 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The sequence is initially {5, 2, 4}. Three operations can be performed as follows:

\n
    \n
  • First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}.
  • \n
  • Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}.
  • \n
  • Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 3}.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4\n631 577 243 199\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

No operation can be performed since all the elements are odd. Thus, the answer is 0.

\n
\n
\n
\n
\n
\n

Sample Input 3

10\n2184 2126 1721 1800 1024 2528 3360 1945 1280 1776\n
\n
\n
\n
\n
\n

Sample Output 3

39\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\\n\", &n);\n int a[n];\n int i = 0;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n i = 0;\n int num = 0;\n while (i < n) {\n if (a[i] % 2 == 0) {\n a[i] = a[i] / 2;\n num++;\n } else {\n i++;\n }\n }\n printf(\"%d\\n\", num);\n return 0;\n}", "rust_code": "fn solution() {\n let mut count_line = String::new();\n std::io::stdin().read_line(&mut count_line).ok();\n let mut values_line = String::new();\n std::io::stdin().read_line(&mut values_line).ok();\n let it = values_line.split_whitespace();\n let mut dividable_count = 0;\n\n for i in it {\n let mut u: u64 = i.parse().unwrap();\n let mut current_divide = 0;\n while u.is_multiple_of(2) {\n u /= 2;\n current_divide += 1;\n }\n dividable_count += current_divide;\n }\n println!(\"{}\", dividable_count);\n}", "difficulty": "hard"} {"problem_id": "1121", "problem_description": "A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \\times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \\le i \\le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \\le j \\le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\\max(R)-\\min(R))^2 + (\\max(C)-\\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\\max(X)$$$ as the maximum value in $$$X$$$ and $$$\\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.", "c_code": "int solution() {\n int N;\n scanf(\"%d\", &N);\n int in[N][2];\n int out[N];\n for (int i = 0; i < N; i++) {\n scanf(\"%d %d\", &in[i][0], &in[i][1]);\n if (in[i][1] % in[i][0] == 0) {\n out[i] = 0;\n } else {\n out[i] = 2;\n }\n }\n for (int i = 0; i < N; i++) {\n printf(\"%d\\n\", out[i], in[i][0], in[i][1]);\n int a[in[i][0]][in[i][0]];\n for (int j = 0; j < in[i][0]; j++) {\n for (int k = 0; k < in[i][0] - j; k++) {\n if (in[i][1] > 0) {\n a[k][j + k] = 1;\n in[i][1]--;\n } else {\n a[k][j + k] = 0;\n }\n }\n for (int k = 0; k < j; k++) {\n if (in[i][1] > 0) {\n a[in[i][0] - j + k][k] = 1;\n in[i][1]--;\n } else {\n a[in[i][0] - j + k][k] = 0;\n }\n }\n }\n for (int j = 0; j < in[i][0]; j++) {\n for (int k = 0; k < in[i][0]; k++) {\n printf(\"%d\", a[j][k]);\n }\n printf(\"\\n\");\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, k) = (xs[0], xs[1]);\n println!(\"{}\", if k % n == 0 { 0 } else { 2 });\n let mut m: Vec> = vec![vec![false; n]; n];\n for i in 0..k {\n let j = i % n;\n let l = (i + (i / n)) % n;\n m[j][l] = true;\n }\n for i in 0..n {\n let s: Vec = m[i].iter().map(|&b| if b { b'1' } else { b'0' }).collect();\n println!(\"{}\", String::from_utf8(s).unwrap());\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1122", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1\\leq A\\leq B\\leq 10^{18}
  • \n
  • 1\\leq C,D\\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C D\n
\n
\n
\n
\n
\n

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 9 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

5 and 7 satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 40 6 8\n
\n
\n
\n
\n
\n

Sample Output 2

23\n
\n
\n
\n
\n
\n
\n

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582\n
\n
\n
\n
\n
\n

Sample Output 3

532105071133627368\n
\n
\n
", "c_code": "int solution(void) {\n long long A = 0;\n long long B = 0;\n long long C = 0;\n long long D = 0;\n\n long long bC = 0;\n long long bD = 0;\n long long bCD = 0;\n long long aC = 0;\n long long aD = 0;\n long long aCD = 0;\n long long sumB = 0;\n long long sumA = 0;\n long long max = 0;\n long long min = 0;\n long long amari = 0;\n long long lcm = 0;\n scanf(\"%lld %lld %lld %lld\", &A, &B, &C, &D);\n bC = B / C;\n bD = B / D;\n aC = (A - 1) / C;\n aD = (A - 1) / D;\n if (C < D) {\n max = D;\n min = C;\n } else {\n max = C;\n min = D;\n }\n while (1) {\n amari = max % min;\n if (amari == 0) {\n break;\n }\n max = min;\n min = amari;\n }\n\n lcm = C * D / min;\n\n bCD = B / (lcm);\n aCD = (A - 1) / (lcm);\n\n sumB = B - bC - bD + bCD;\n sumA = (A - 1) - aC - aD + aCD;\n\n printf(\"%lld\\n\", sumB - sumA);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n a:u64,\n b:u64,\n c:u64,\n d:u64\n }\n\n let mut temp: u64;\n\n let mut m = d;\n let mut n = c;\n\n while m % n != 0 {\n temp = n;\n n = m % n;\n m = temp;\n }\n\n let min_multi = d * c / n;\n\n let diff_c = b / c - (a - 1) / c;\n let diff_d = b / d - (a - 1) / d;\n let diff_mulit = b / min_multi - (a - 1) / min_multi;\n\n let muti_num = diff_c + diff_d - diff_mulit;\n\n let ans = b - a + 1 - muti_num;\n println!(\"{}\", ans)\n}", "difficulty": "medium"} {"problem_id": "1123", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.

\n

Given an integer N, determine whether it is a Harshad number.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1?N?10^8
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print Yes if N is a Harshad number; print No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

12\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

f(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number.

\n
\n
\n
\n
\n
\n

Sample Input 2

57\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

f(57)=5+7=12. Since 57 is not divisible by 12, 12 is not a Harshad number.

\n
\n
\n
\n
\n
\n

Sample Input 3

148\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n int x = 0;\n int ten = 1;\n for (int i = 0; i <= 8; i++) {\n x += n / ten % 10;\n ten *= 10;\n }\n if (n % x == 0) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut stdinlock = stdin.lock();\n let mut str1 = String::new();\n stdinlock.read_line(&mut str1).unwrap();\n let mut it1 = str1.split_whitespace().map(|x| u32::from_str(x).unwrap());\n let n = it1.next().unwrap();\n let mut na = n;\n let mut j = 0;\n for _ in 0..9 {\n j += na % 10u32;\n na /= 10;\n }\n if n % j == 0 {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "1124", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given two positive integers a and b.\nLet x be the average of a and b.\nPrint x rounded up to the nearest integer.

\n
\n
\n
\n
\n

Constraints

    \n
  • a and b are integers.
  • \n
  • 1 \\leq a, b \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b\n
\n
\n
\n
\n
\n

Output

Print x rounded up to the nearest integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 4\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n

The average of 7 and 4 is 5.5, and it will be rounded up to the nearest integer, 6.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 5\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int answer = 0;\n scanf(\"%d %d\", &a, &b);\n answer = (a + b) / 2;\n if ((a + b) % 2 != 0) {\n answer++;\n }\n printf(\"%d\\n\", answer);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let input: Vec = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();\n println!(\"{}\", (input[0] + input[1] + 1) / 2)\n}", "difficulty": "medium"} {"problem_id": "1125", "problem_description": "Boboniu likes playing chess with his employees. As we know, no employee can beat the boss in the chess game, so Boboniu has never lost in any round.You are a new applicant for his company. Boboniu will test you with the following chess question:Consider a $$$n\\times m$$$ grid (rows are numbered from $$$1$$$ to $$$n$$$, and columns are numbered from $$$1$$$ to $$$m$$$). You have a chess piece, and it stands at some cell $$$(S_x,S_y)$$$ which is not on the border (i.e. $$$2 \\le S_x \\le n-1$$$ and $$$2 \\le S_y \\le m-1$$$).From the cell $$$(x,y)$$$, you can move your chess piece to $$$(x,y')$$$ ($$$1\\le y'\\le m, y' \\neq y$$$) or $$$(x',y)$$$ ($$$1\\le x'\\le n, x'\\neq x$$$). In other words, the chess piece moves as a rook. From the cell, you can move to any cell on the same row or column.Your goal is to visit each cell exactly once. Can you find a solution?Note that cells on the path between two adjacent cells in your route are not counted as visited, and it is not required to return to the starting point.", "c_code": "int solution() {\n int x;\n int y;\n int n;\n int m;\n scanf(\"%d%d%d%d\", &n, &m, &x, &y);\n printf(\"%d %d\\n\", x, y);\n printf(\"%d %d\\n\", 1, y);\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m && i % 2; j++) {\n if ((i == x && j == y) || (i == 1 && j == y)) {\n continue;\n }\n printf(\"%d %d\\n\", i, j);\n }\n for (int j = m; j >= 1 && !(i % 2); j--) {\n if (i == x && j == y) {\n continue;\n }\n printf(\"%d %d\\n\", i, j);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let stdout = std::io::stdout();\n let mut reader = std::io::BufReader::new(stdin.lock());\n let mut writer = std::io::BufWriter::new(stdout.lock());\n\n let (n, m, mut sx, mut sy): (usize, usize, usize, usize) = {\n let mut buf = String::new();\n reader.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 iter.next().unwrap().parse().unwrap(),\n )\n };\n\n sx -= 1;\n sy -= 1;\n for i in 0..n {\n for j in 0..m {\n writeln!(writer, \"{} {}\", (sx + i) % n + 1, (sy + j) % m + 1,).unwrap();\n }\n sy = (sy + m - 1) % m;\n }\n}", "difficulty": "hard"} {"problem_id": "1126", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.\nHere, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.\nBelow is an example of a checked pattern of side 3:

\n
\n\"cba927b2484fad94fb5ff7473e9aadef.png\"\n
\n

AtCoDeer has N desires.\nThe i-th desire is represented by x_i, y_i and c_i.\nIf c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.\nAt most how many desires can he satisfy at the same time?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 N 10^5
  • \n
  • 1 K 1000
  • \n
  • 0 x_i 10^9
  • \n
  • 0 y_i 10^9
  • \n
  • If i j, then (x_i,y_i) (x_j,y_j).
  • \n
  • c_i is B or W.
  • \n
  • N, K, x_i and y_i are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nx_1 y_1 c_1\nx_2 y_2 c_2\n:\nx_N y_N c_N\n
\n
\n
\n
\n
\n

Output

Print the maximum number of desires that can be satisfied at the same time.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3\n0 1 W\n1 2 W\n5 3 B\n5 4 B\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

He can satisfy all his desires by painting as shown in the example above.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 1000\n0 0 B\n0 1 W\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6 2\n1 2 B\n2 1 W\n2 2 B\n1 0 B\n0 6 W\n4 5 W\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n
\n
", "c_code": "int solution(void) {\n\n int n;\n int m;\n int x;\n int y;\n char c;\n static int map[2010][2010];\n static int ruiseki[2010][2010];\n static int ruisekitemp[2010][2010];\n\n int i;\n int j;\n int k;\n int l;\n int flag = 0;\n int ans = 0;\n int count = 0;\n int sum = 0;\n int temp;\n int temp1;\n int temp2;\n\n int len;\n\n scanf(\"%d %d\", &n, &m);\n\n for (i = 0; i <= 2000; i++) {\n for (j = 0; j <= 2000; j++) {\n map[i][j] = 0;\n }\n }\n\n for (i = 0; i < n; i++) {\n scanf(\"%d %d %c\", &x, &y, &c);\n if (c == 'W') {\n x = x + m;\n }\n x = x % (2 * m);\n y = y % (2 * m);\n map[x][y] = map[x][y] + 1;\n }\n\n for (i = 0; i < 2 * m; i++) {\n for (j = 0; j < 2 * m; j++) {\n if (j == 0) {\n ruisekitemp[i][0] = map[i][0];\n } else {\n ruisekitemp[i][j] = ruisekitemp[i][j - 1] + map[i][j];\n }\n }\n }\n\n for (j = 0; j < 2 * m; j++) {\n for (i = 0; i < 2 * m; i++) {\n if (i == 0) {\n ruiseki[0][j] = ruisekitemp[0][j];\n } else {\n ruiseki[i][j] = ruiseki[i - 1][j] + ruisekitemp[i][j];\n }\n }\n }\n\n for (i = 0; i < m; i++) {\n for (j = 0; j < m; j++) {\n temp = 0;\n temp = temp + ruiseki[i + m][j + m] - ruiseki[i + m][j] -\n ruiseki[i][j + m] + ruiseki[i][j];\n temp = temp + ruiseki[i][j];\n temp = temp + ruiseki[(2 * m) - 1][(2 * m) - 1] -\n ruiseki[i + m][(2 * m) - 1] - ruiseki[(2 * m) - 1][j + m] +\n ruiseki[i + m][j + m];\n temp = temp + ruiseki[(2 * m) - 1][j] - ruiseki[i + m][j];\n temp = temp + ruiseki[i][(2 * m) - 1] - ruiseki[i][j + m];\n if (temp < n - temp) {\n temp = n - temp;\n }\n if (temp > ans) {\n ans = temp;\n }\n }\n }\n\n printf(\"%d\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n k: usize,\n query: [(usize, usize, chars); n],\n }\n let m = k * 2;\n let mut field = vec![vec![0i64; k * 3]; k * 2];\n for &(x, y, ref c) in &query {\n let y = match *c.first().unwrap() {\n 'B' => y,\n _ => y + k,\n };\n let (x, y) = if (x % m) < k {\n (x % m, y % m)\n } else {\n ((x + k) % m, (y + k) % m)\n };\n field[y][x] += 1;\n field[(y + k) % m][x + k] += 1;\n field[y][x + m] += 1;\n }\n for i in 1..2 * k {\n for j in 0..3 * k {\n field[i][j] += field[i - 1][j];\n }\n }\n for i in 0..2 * k {\n for j in 1..3 * k {\n field[i][j] += field[i][j - 1];\n }\n }\n let mut ans = 0;\n for i in 0..k {\n for j in 0..2 * k {\n ans = cmp::max(\n ans,\n field[i + k][j + k] - field[i][j + k] - field[i + k][j] + field[i][j],\n );\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1127", "problem_description": "You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \\le k \\le n$$$ that $$$k \\bmod x = y$$$, where $$$\\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.", "c_code": "int solution() {\n int x;\n int y;\n int n;\n int N = 0;\n\n scanf(\"%d\", &N);\n\n while (N > 0) {\n\n scanf(\"%d\", &x);\n scanf(\"%d\", &y);\n scanf(\"%d\", &n);\n\n if (x == 0) {\n printf(\"%s\\n\", \"x is zero, no answer\");\n return -1;\n }\n\n int k = (n / x) * x;\n\n if (k + y <= n) {\n\n printf(\"%d\\n\", k + y);\n } else {\n\n printf(\"%d\\n\", k - x + y);\n }\n\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: u16 = 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 module: i32 = iter.next().unwrap().parse().unwrap();\n let least: i32 = iter.next().unwrap().parse().unwrap();\n let max: i32 = iter.next().unwrap().parse().unwrap();\n let (mut whole, rest) = (max / module, max % module);\n if rest < least {\n whole -= 1;\n }\n println!(\"{}\", module * whole + least);\n count -= 1;\n }\n}", "difficulty": "hard"} {"problem_id": "1128", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. Initially all elements of $$$a$$$ are either $$$0$$$ or $$$1$$$. You need to process $$$q$$$ queries of two kinds: 1 x : Assign to $$$a_x$$$ the value $$$1 - a_x$$$. 2 k : Print the $$$k$$$-th largest value of the array. As a reminder, $$$k$$$-th largest value of the array $$$b$$$ is defined as following: Sort the array in the non-increasing order, return $$$k$$$-th element from it. For example, the second largest element in array $$$[0, 1, 0, 1]$$$ is $$$1$$$, as after sorting in non-increasing order it becomes $$$[1, 1, 0, 0]$$$, and the second element in this array is equal to $$$1$$$.", "c_code": "int solution(void) {\n int n = 0;\n int q = 0;\n\n scanf(\"%d %d\", &n, &q);\n int a[n];\n int count[2] = {0};\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n count[a[i]]++;\n }\n int t = 0;\n int k = 0;\n int l = 0;\n for (int m = 0; m < q; m++) {\n scanf(\"%d %d\", &t, &k);\n if (t == 1) {\n a[k - 1] = 1 - a[k - 1];\n ++count[a[k - 1]];\n --count[1 - a[k - 1]];\n }\n if (t == 2) {\n if (count[1] >= k) {\n printf(\"1\\n\");\n } else {\n printf(\"0\\n\");\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\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 q = it.next().unwrap();\n\n let mut a = Vec::with_capacity(n);\n let mut nones = 0;\n a.extend(\n lines\n .next()\n .unwrap()\n .split_whitespace()\n .map(|s| s == \"1\")\n .inspect(|&a| {\n if a {\n nones += 1;\n }\n }),\n );\n\n for _ in 0..q {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace();\n\n let t = it.next().unwrap();\n match t {\n \"1\" => {\n let x: usize = it.next().unwrap().parse::().unwrap() - 1;\n nones += if a[x] { -1 } else { 1 };\n a[x] = !a[x];\n }\n _ => {\n let k: i64 = it.next().unwrap().parse().unwrap();\n let ans = if k <= nones { 1 } else { 0 };\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1129", "problem_description": "

Standard Deviation


\n\n

\n You have final scores of an examination for n students. Calculate standard deviation of the scores s1, s2 ... sn.\n

\n\n

\nThe variance α2 is defined by \n

\n\n

\nα2 = (∑ni=1(si - m)2)/n\n

\n\n

\n where m is an average of si.\n\n The standard deviation of the scores is the square root of their variance.\n

\n\n

Input

\n\n

\n The input consists of multiple datasets. Each dataset is given in the following format:\n

\n\n
\nn\ns1 s2 ... sn\n
\n\n

\n The input ends with single zero for n.\n

\n\n

Output

\n\n

\n For each dataset, print the standard deviation in a line. The output should not contain an absolute error greater than 10-4.\n

\n\n\n

Constraints

\n\n\n
    \n
  • n ≤ 1000
  • \n
  • 0 ≤ si ≤ 100
  • \n
\n\n\n

Sample Input

\n\n
\n5\n70 80 100 90 20\n3\n80 80 80\n0\n
\n\n

Sample Output

\n\n
\n27.85677655\n0.00000000\n
", "c_code": "int solution(void) {\n int i;\n double n;\n double s[1000] = {0};\n double a = 0;\n double m = 0;\n scanf(\"%lf\", &n);\n\n while (n != 0) {\n\n for (i = 0; i < n; i++) {\n scanf(\"%lf\", &s[i]);\n\n m = m + s[i];\n }\n\n m = m / n;\n\n for (i = 0; i < n; i++) {\n a = a + (s[i] - m) * (s[i] - m);\n\n s[i] = 0;\n }\n\n printf(\"%lf\\n\", sqrt(a / n));\n\n a = 0;\n m = 0;\n\n scanf(\"%lf\", &n);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut lines = stdin.lock().lines();\n\n loop {\n let n = lines.next().unwrap().unwrap().parse::().unwrap();\n\n if n == 0. {\n break;\n }\n\n let v = lines\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let average = v.iter().sum::() / n;\n let deviation = v.iter().map(|x| (x - average).powi(2)).sum::() / n;\n\n println!(\"{}\", deviation.sqrt());\n }\n}", "difficulty": "medium"} {"problem_id": "1130", "problem_description": "There are $$$n$$$ piles of sand where the $$$i$$$-th pile has $$$a_i$$$ blocks of sand. The $$$i$$$-th pile is called too tall if $$$1 < i < n$$$ and $$$a_i > a_{i-1} + a_{i+1}$$$. That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.)You are given an integer $$$k$$$. An operation consists of picking $$$k$$$ consecutive piles of sand and adding one unit of sand to them all. Formally, pick $$$1 \\leq l,r \\leq n$$$ such that $$$r-l+1=k$$$. Then for all $$$l \\leq i \\leq r$$$, update $$$a_i \\gets a_i+1$$$.What is the maximum number of piles that can simultaneously be too tall after some (possibly zero) operations?", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n int n;\n int k;\n for (int i = 0; i < t; i++) {\n scanf(\"%d%d\", &n, &k);\n int a[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n }\n if (k == 1) {\n printf(\"%d\\n\", (n - 1) / 2);\n } else {\n int c = 0;\n for (int j = 1; j < n - 1; j++) {\n if (a[j] > (a[j - 1] + a[j + 1])) {\n c++;\n }\n }\n printf(\"%d\\n\", c);\n }\n }\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin()\n .read_to_string(&mut content)\n .expect(\"Unable to read from stdin\");\n\n let mut lines = content.lines();\n let num_tests: usize = lines.next().unwrap().parse().unwrap();\n for _ in 0..num_tests {\n let arr: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse::().unwrap())\n .collect();\n let n = arr[0];\n let k = arr[1];\n let a: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse::().unwrap())\n .collect();\n if k == 1 {\n println!(\"{}\", (n - 1) / 2);\n } else {\n let mut result = 0usize;\n for i in 1..n - 1 {\n if a[i] > a[i - 1] + a[i + 1] {\n result += 1;\n }\n }\n println!(\"{result}\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1131", "problem_description": "You are given $$$4n$$$ sticks, the length of the $$$i$$$-th stick is $$$a_i$$$.You have to create $$$n$$$ rectangles, each rectangle will consist of exactly $$$4$$$ sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length.You want to all rectangles to have equal area. The area of the rectangle with sides $$$a$$$ and $$$b$$$ is $$$a \\cdot b$$$.Your task is to say if it is possible to create exactly $$$n$$$ rectangles of equal area or not.You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int i;\n int x = 0;\n int y = 0;\n int j;\n int temp;\n int area;\n scanf(\"%d\", &n);\n int a[4 * n];\n for (i = 0; i < 4 * n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (i = 0; i < 4 * n; i++) {\n for (j = 0; j < i; j++) {\n if (a[i] > a[j]) {\n temp = a[i];\n a[i] = a[j];\n a[j] = temp;\n }\n }\n }\n area = a[0] * a[(4 * n) - 1];\n for (i = 0; i < 2 * n; i += 2) {\n if (a[i] == a[i + 1] && a[(4 * n) - 1 - i] == a[(4 * n) - 2 - i] &&\n a[i] * a[(4 * n) - 1 - i] == area) {\n x++;\n } else {\n break;\n }\n }\n if (x == n) {\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 for _ in 0..t {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let n = input.trim().parse::().unwrap();\n\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let mut sticks: Vec = input\n .split_whitespace()\n .map(|n| n.parse().expect(\"invalid input\"))\n .collect();\n sticks.sort();\n\n let prod = sticks[0] * sticks[4 * n - 1];\n let mut possible = true;\n for (i, ri) in (0..(2 * n))\n .step_by(2)\n .zip(((2 * n)..(4 * n)).rev().step_by(2))\n {\n if sticks[i] != sticks[i + 1]\n || sticks[ri] != sticks[ri - 1]\n || sticks[i] * sticks[ri] != prod\n {\n possible = false;\n break;\n }\n }\n println!(\"{}\", if possible { \"YES\" } else { \"NO\" });\n }\n}", "difficulty": "easy"} {"problem_id": "1132", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are given an integer N. Find the number of strings of length N that satisfy the following conditions, modulo 10^9+7:

\n
    \n
  • The string does not contain characters other than A, C, G and T.
  • \n
  • The string does not contain AGC as a substring.
  • \n
  • The condition above cannot be violated by swapping two adjacent characters once.
  • \n
\n
\n
\n
\n
\n

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

\n

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the number of strings of length N that satisfy the following conditions, modulo 10^9+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

61\n
\n

There are 4^3 = 64 strings of length 3 that do not contain characters other than A, C, G and T. Among them, only AGC, ACG and GAC violate the condition, so the answer is 64 - 3 = 61.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n
\n
\n
\n
\n
\n

Sample Output 2

230\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100\n
\n
\n
\n
\n
\n

Sample Output 3

388130742\n
\n

Be sure to print the number of strings modulo 10^9+7.

\n
\n
", "c_code": "int solution(void) {\n\n long n;\n scanf(\"%ld\", &n);\n long ans = 0;\n if (n == 3) {\n ans = pow(4, 3) - 3;\n printf(\"%ld\\n\", ans);\n return 0;\n }\n long mod = pow(10, 9) + 7;\n long dp[n + 1][4][4][4][4];\n for (int i = 0; i < 4; i++) {\n for (int j = 0; j < 4; j++) {\n for (int k = 0; k < 4; k++) {\n for (int l = 0; l < 4; l++) {\n dp[4][i][j][k][l] = 1;\n if (i == 0 && k == 2 && l == 1) {\n dp[4][i][j][k][l] = 0;\n }\n if (i == 0 && j == 2 && l == 1) {\n dp[4][i][j][k][l] = 0;\n }\n if (i == 0 && j == 2 && k == 1) {\n dp[4][i][j][k][l] = 0;\n }\n if (j == 0 && k == 2 && l == 1) {\n dp[4][i][j][k][l] = 0;\n }\n if (i == 2 && j == 0 && k == 1) {\n dp[4][i][j][k][l] = 0;\n }\n if (j == 2 && k == 0 && l == 1) {\n dp[4][i][j][k][l] = 0;\n }\n if (i == 0 && j == 1 && k == 2) {\n dp[4][i][j][k][l] = 0;\n }\n if (j == 0 && k == 1 && l == 2) {\n dp[4][i][j][k][l] = 0;\n }\n }\n }\n }\n }\n for (int length = 5; length <= n; length++) {\n for (int i = 0; i < 4; i++) {\n for (int j = 0; j < 4; j++) {\n for (int k = 0; k < 4; k++) {\n for (int l = 0; l < 4; l++) {\n dp[length][i][j][k][l] = 0;\n if (i == 0 && k == 2 && l == 1) {\n continue;\n }\n if (i == 0 && j == 2 && l == 1) {\n continue;\n }\n if (i == 0 && j == 2 && k == 1) {\n continue;\n }\n if (j == 0 && k == 2 && l == 1) {\n continue;\n }\n if (i == 2 && j == 0 && k == 1) {\n continue;\n }\n if (j == 2 && k == 0 && l == 1) {\n continue;\n }\n if (i == 0 && j == 1 && k == 2) {\n continue;\n }\n if (j == 0 && k == 1 && l == 2) {\n continue;\n }\n for (int m = 0; m < 4; m++) {\n dp[length][i][j][k][l] += dp[length - 1][m][i][j][k];\n dp[length][i][j][k][l] %= mod;\n }\n }\n }\n }\n }\n }\n for (int i = 0; i < 4; i++) {\n for (int j = 0; j < 4; j++) {\n for (int k = 0; k < 4; k++) {\n for (int l = 0; l < 4; l++) {\n ans += dp[n][i][j][k][l];\n ans %= mod;\n }\n }\n }\n }\n printf(\"%ld\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N : usize,\n }\n let MOD = 1_000_000_007;\n\n let mut dp = vec![vec![vec![vec![0; 5]; 5]; 5]; N + 1];\n dp[0][0][0][0] = 1;\n\n for n in 0..N {\n for c0 in 0..5 {\n for c1 in 0..5 {\n for c2 in 0..5 {\n for c3 in 1..5 {\n if c0 == 1 && c1 == 2 && c3 == 3 {\n continue;\n }\n if c0 == 1 && c2 == 2 && c3 == 3 {\n continue;\n }\n if c1 == 1 && c2 == 2 && c3 == 3 {\n continue;\n }\n if c1 == 2 && c2 == 1 && c3 == 3 {\n continue;\n }\n if c1 == 1 && c2 == 3 && c3 == 2 {\n continue;\n }\n dp[n + 1][c1][c2][c3] += dp[n][c0][c1][c2];\n dp[n + 1][c1][c2][c3] %= MOD;\n }\n }\n }\n }\n }\n let mut res = 0usize;\n for i in 1..5 {\n for j in 1..5 {\n for k in 1..5 {\n res += dp[N][i][j][k];\n res %= MOD;\n }\n }\n }\n println!(\"{}\", res);\n}", "difficulty": "easy"} {"problem_id": "1133", "problem_description": "You are given two matrices $$$A$$$ and $$$B$$$. Each matrix contains exactly $$$n$$$ rows and $$$m$$$ columns. Each element of $$$A$$$ is either $$$0$$$ or $$$1$$$; each element of $$$B$$$ is initially $$$0$$$.You may perform some operations with matrix $$$B$$$. During each operation, you choose any submatrix of $$$B$$$ having size $$$2 \\times 2$$$, and replace every element in the chosen submatrix with $$$1$$$. In other words, you choose two integers $$$x$$$ and $$$y$$$ such that $$$1 \\le x < n$$$ and $$$1 \\le y < m$$$, and then set $$$B_{x, y}$$$, $$$B_{x, y + 1}$$$, $$$B_{x + 1, y}$$$ and $$$B_{x + 1, y + 1}$$$ to $$$1$$$.Your goal is to make matrix $$$B$$$ equal to matrix $$$A$$$. Two matrices $$$A$$$ and $$$B$$$ are equal if and only if every element of matrix $$$A$$$ is equal to the corresponding element of matrix $$$B$$$.Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes $$$B$$$ equal to $$$A$$$. Note that you don't have to minimize the number of operations.", "c_code": "int solution() {\n int m;\n int n;\n int free = 1;\n scanf(\"%d%d\", &n, &m);\n int a[n][m];\n int b[n][m];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &a[i][j]);\n if (a[i][j] == 1) {\n free = 0;\n }\n b[i][j] = 0;\n }\n }\n if (free) {\n printf(\"0\");\n return 0;\n }\n int k = 0;\n int x[n * m];\n int y[n * m];\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < m - 1; j++) {\n if (a[i][j] == 1 && a[i][j + 1] == 1 && a[i + 1][j] == 1 &&\n a[i + 1][j + 1] == 1) {\n b[i][j] = b[i + 1][j] = b[i][j + 1] = b[i + 1][j + 1] = 1;\n x[k] = i;\n y[k] = j;\n k++;\n }\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (a[i][j] != b[i][j]) {\n printf(\"-1\");\n return 0;\n }\n }\n printf(\"\\n\");\n }\n printf(\"%d\", k);\n for (int i = 0; i < k; i++) {\n printf(\"\\n%d %d\", x[i] + 1, y[i] + 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n\n let mut buf = String::new();\n stdin.read_line(&mut buf).unwrap();\n let mut w = buf.split_whitespace();\n let (n, m): (usize, usize) = (\n w.next().unwrap().parse().unwrap(),\n w.next().unwrap().parse().unwrap(),\n );\n let mut eta = vec![vec![false; m]; n];\n for i in 0..n {\n buf.clear();\n stdin.read_line(&mut buf).unwrap();\n let v = buf\n .split_whitespace()\n .map(|w| w.parse::().unwrap())\n .collect::>();\n for j in 0..m {\n eta[i][j] = v[j] == 1;\n }\n }\n let mut maj = vec![vec![false; m]; n];\n let mut ans = Vec::new();\n for i in 0..n - 1 {\n for j in 0..m - 1 {\n if eta[i][j] && eta[i + 1][j] && eta[i][j + 1] && eta[i + 1][j + 1] {\n maj[i][j] = true;\n maj[i][j + 1] = true;\n maj[i + 1][j] = true;\n maj[i + 1][j + 1] = true;\n ans.push((i, j));\n }\n }\n }\n for i in 0..n {\n for j in 0..m {\n if eta[i][j] && !maj[i][j] {\n println!(\"-1\");\n return;\n }\n }\n }\n println!(\"{}\", ans.len());\n for a in ans {\n println!(\"{} {}\", a.0 + 1, a.1 + 1);\n }\n}", "difficulty": "easy"} {"problem_id": "1134", "problem_description": "After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \\dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \\ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i < h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n int k = 0;\n int pit = 0;\n int th = 0;\n int h[100] = {0};\n scanf(\"%d\", &t);\n for (int te = 0; te < t; te++) {\n scanf(\"%d %d\", &n, &k);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &h[i]);\n }\n h[n] = 0;\n for (int i = 0; i < k; i++) {\n pit = 1;\n for (int j = 0; j < n; j++) {\n if (h[j] < h[j + 1]) {\n th = (j + 1);\n pit = 0;\n h[j] += 1;\n break;\n }\n }\n if (pit == 1) {\n th = -1;\n break;\n }\n }\n printf(\"%d\\n\", th);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let k = std::io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n for _ele in 0..k {\n let mut l = String::new();\n std::io::stdin().read_line(&mut l).unwrap();\n let mut l = l\n .trim()\n .split(\" \")\n .flat_map(str::parse::)\n .collect::>();\n let mut m = String::new();\n std::io::stdin().read_line(&mut m).unwrap();\n let mut m = m\n .trim()\n .split(\" \")\n .flat_map(str::parse::)\n .collect::>();\n let _o_vals: Vec = vec![];\n let swch = true;\n let mut ans_pos: Vec = vec![];\n 'rest: while swch {\n let j = m.clone();\n 'jest: for (index, its) in m.iter_mut().enumerate() {\n if index == j.len() - 1 {\n break 'rest;\n }\n if *its < j[index + 1] && l[1] > 0 {\n *its += 1;\n l[1] -= 1;\n ans_pos.push(index as i32 + 1);\n break 'jest;\n }\n }\n }\n if l[1] > 0 {\n ans_pos.push(-1)\n };\n println!(\"{:?}\", ans_pos[ans_pos.len() - 1]);\n }\n}", "difficulty": "hard"} {"problem_id": "1135", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N boxes arranged in a row from left to right. The i-th box from the left contains A_i candies.

\n

You will take out the candies from some consecutive boxes and distribute them evenly to M children.

\n

Such being the case, find the number of the pairs (l, r) that satisfy the following:

\n
    \n
  • l and r are both integers and satisfy 1 \\leq l \\leq r \\leq N.
  • \n
  • A_l + A_{l+1} + ... + A_r is a multiple of M.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 2 \\leq M \\leq 10^9
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the number of the pairs (l, r) that satisfy the conditions.

\n

Note that the number may not fit into a 32-bit integer type.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n4 1 5\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The sum A_l + A_{l+1} + ... + A_r for each pair (l, r) is as follows:

\n
    \n
  • Sum for (1, 1): 4
  • \n
  • Sum for (1, 2): 5
  • \n
  • Sum for (1, 3): 10
  • \n
  • Sum for (2, 2): 1
  • \n
  • Sum for (2, 3): 6
  • \n
  • Sum for (3, 3): 5
  • \n
\n

Among these, three are multiples of 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

13 17\n29 7 5 7 9 51 7 13 8 55 42 9 81\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 400000000\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

25\n
\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n int a[n];\n int i;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n long long int ans = 0;\n int tmp[n + 1];\n tmp[0] = 0;\n int j = 0;\n for (i = 0; i < n; i++) {\n tmp[i + 1] = (tmp[i] + a[i]) % m;\n for (j = 0; j <= i; j++) {\n if (tmp[i + 1] == tmp[j]) {\n ans++;\n }\n }\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut s = String::new();\n let mut nums: Vec = Vec::new();\n {\n stdin.read_line(&mut s).ok();\n let it = s.split_whitespace();\n for arg in it {\n nums.push(i64::from_str(arg).expect(\"e1\"));\n }\n }\n let _n = nums[0];\n let m = nums[1];\n let mut a: Vec = Vec::new();\n s = String::new();\n stdin.read_line(&mut s).ok();\n let it = s.split_whitespace();\n for arg in it {\n a.push(i64::from_str(arg).expect(\"e2\") % m);\n }\n let mut sum: Vec = Vec::new();\n sum.push(0);\n let mut s: i64 = 0;\n for i in 0..a.len() {\n s += a[i];\n s %= m;\n sum.push(s);\n }\n sum.sort();\n let mut ans: i64 = 0;\n let mut temp = sum[0];\n let mut c = 1;\n for i in 1..sum.len() {\n if temp == sum[i] {\n c += 1;\n } else {\n ans += c * (c - 1) / 2;\n c = 1;\n temp = sum[i];\n }\n }\n ans += c * (c - 1) / 2;\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1136", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N towns located in a line, conveniently numbered 1 through N. Takahashi the merchant is going on a travel from town 1 to town N, buying and selling apples.

\n

Takahashi will begin the travel at town 1, with no apple in his possession. The actions that can be performed during the travel are as follows:

\n
    \n
  • Move: When at town i (i < N), move to town i + 1.
  • \n
  • Merchandise: Buy or sell an arbitrary number of apples at the current town. Here, it is assumed that one apple can always be bought and sold for A_i yen (the currency of Japan) at town i (1 ≦ i ≦ N), where A_i are distinct integers. Also, you can assume that he has an infinite supply of money.
  • \n
\n

For some reason, there is a constraint on merchandising apple during the travel: the sum of the number of apples bought and the number of apples sold during the whole travel, must be at most T. (Note that a single apple can be counted in both.)

\n

During the travel, Takahashi will perform actions so that the profit of the travel is maximized. Here, the profit of the travel is the amount of money that is gained by selling apples, minus the amount of money that is spent on buying apples. Note that we are not interested in apples in his possession at the end of the travel.

\n

Aoki, a business rival of Takahashi, wants to trouble Takahashi by manipulating the market price of apples. Prior to the beginning of Takahashi's travel, Aoki can change A_i into another arbitrary non-negative integer A_i' for any town i, any number of times. The cost of performing this operation is |A_i - A_i'|. After performing this operation, different towns may have equal values of A_i.

\n

Aoki's objective is to decrease Takahashi's expected profit by at least 1 yen. Find the minimum total cost to achieve it. You may assume that Takahashi's expected profit is initially at least 1 yen.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ N ≦ 10^5
  • \n
  • 1 ≦ A_i ≦ 10^9 (1 ≦ i ≦ N)
  • \n
  • A_i are distinct.
  • \n
  • 2 ≦ T ≦ 10^9
  • \n
  • In the initial state, Takahashi's expected profit is at least 1 yen.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N T\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum total cost to decrease Takahashi's expected profit by at least 1 yen.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n100 50 200\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

In the initial state, Takahashi can achieve the maximum profit of 150 yen as follows:

\n
    \n
  1. Move from town 1 to town 2.
  2. \n
  3. Buy one apple for 50 yen at town 2.
  4. \n
  5. Move from town 2 to town 3.
  6. \n
  7. Sell one apple for 200 yen at town 3.
  8. \n
\n

If, for example, Aoki changes the price of an apple at town 2 from 50 yen to 51 yen, Takahashi will not be able to achieve the profit of 150 yen. The cost of performing this operation is 1, thus the answer is 1.

\n

There are other ways to decrease Takahashi's expected profit, such as changing the price of an apple at town 3 from 200 yen to 199 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 8\n50 30 40 10 20\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 100\n7 10 4 5 9 3 6 8 2 1\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int t;\n int max = 0;\n int min = INT_MAX;\n int ans = 0;\n scanf(\"%d%d\", &n, &t);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n if (min > a[i]) {\n min = a[i];\n }\n if (max < a[i] - min) {\n max = a[i] - min;\n }\n }\n min = INT_MAX;\n for (int i = 0; i < n; i++) {\n if (min > a[i]) {\n min = a[i];\n }\n if (max == a[i] - min) {\n ans++;\n }\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n io::stdin().read_line(&mut String::new()).ok();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n\n let (_, res, _) = s.trim().split(' ').rev().fold((0, 0, 0), |(v, n, x), y| {\n let z = y.parse().unwrap();\n let d = x - z;\n\n if z > x {\n (v, n, z)\n } else if v < d {\n (d, 1, x)\n } else if v == d {\n (v, n + 1, x)\n } else {\n (v, n, x)\n }\n });\n\n println!(\"{}\", res);\n}", "difficulty": "hard"} {"problem_id": "1137", "problem_description": "Michael and Joe are playing a game. The game is played on a grid with $$$n$$$ rows and $$$m$$$ columns, filled with distinct integers. We denote the square on the $$$i$$$-th ($$$1\\le i\\le n$$$) row and $$$j$$$-th ($$$1\\le j\\le m$$$) column by $$$(i, j)$$$ and the number there by $$$a_{ij}$$$.Michael starts by saying two numbers $$$h$$$ ($$$1\\le h \\le n$$$) and $$$w$$$ ($$$1\\le w \\le m$$$). Then Joe picks any $$$h\\times w$$$ subrectangle of the board (without Michael seeing).Formally, an $$$h\\times w$$$ subrectangle starts at some square $$$(a,b)$$$ where $$$1 \\le a \\le n-h+1$$$ and $$$1 \\le b \\le m-w+1$$$. It contains all squares $$$(i,j)$$$ for $$$a \\le i \\le a+h-1$$$ and $$$b \\le j \\le b+w-1$$$. Possible move by Joe if Michael says $$$3\\times 2$$$ (with maximum of $$$15$$$). Finally, Michael has to guess the maximum number in the subrectangle. He wins if he gets it right.Because Michael doesn't like big numbers, he wants the area of the chosen subrectangle (that is, $$$h \\cdot w$$$), to be as small as possible, while still ensuring that he wins, not depending on Joe's choice. Help Michael out by finding this minimum possible area. It can be shown that Michael can always choose $$$h, w$$$ for which he can ensure that he wins.", "c_code": "int solution() {\n int t = 0;\n int i = 0;\n int j = 0;\n int k = 0;\n int n = 0;\n int m = 0;\n int a[40];\n int x[40];\n int temporary = 0;\n int key = 0;\n int var1 = 0;\n int var2 = 0;\n\n scanf(\"%d\\n\", &t);\n i = t;\n\n while (i > 0) {\n scanf(\"%d %d\\n\", &n, &m);\n\n for (j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n x[j] = 0;\n for (k = 1; k < m; k++) {\n scanf(\"%d\", &temporary);\n if (temporary > a[j]) {\n a[j] = temporary;\n x[j] = k;\n }\n }\n scanf(\"\\n\");\n }\n\n key = a[0];\n var2 = 0;\n var1 = x[0];\n\n for (j = 1; j < n; j++) {\n if (a[j] > key) {\n key = a[j];\n var1 = x[j];\n var2 = j;\n }\n }\n\n var1 = var1 + 1;\n\n if ((m - var1 + 1) > (var1)) {\n var1 = (m - var1 + 1);\n }\n\n var2 = var2 + 1;\n\n if ((n - var2 + 1) > (var2)) {\n var2 = (n - var2 + 1);\n }\n\n printf(\"%d\\n\", (var1 * var2));\n i = i - 1;\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n let stdin = io::stdin();\n\n let (mut n, mut m): (i32, i32);\n let mut num: Vec;\n let (mut best, mut best_i, mut best_j): (i32, i32, i32);\n\n stdin.read_line(&mut input).expect(\"bruh\");\n let tests: i32 = input.trim().parse::().unwrap();\n\n for _ in 0..tests {\n input.clear();\n stdin.read_line(&mut input).expect(\"bruh\");\n num = input\n .trim()\n .split(' ')\n .map(|x| x.parse::().unwrap())\n .collect();\n\n n = num[0];\n m = num[1];\n best = i32::MIN;\n best_i = 1;\n best_j = 1;\n\n for i in 0..n {\n input.clear();\n stdin.read_line(&mut input).expect(\"bruh\");\n num = input\n .trim()\n .split(' ')\n .map(|x| x.parse::().unwrap())\n .collect();\n if n == 0 {\n best = num[0];\n best_i = 1;\n best_j = 1;\n }\n for j in 0..num.len() {\n if num[j] > best {\n best = num[j];\n best_i = i + 1;\n best_j = j as i32 + 1;\n }\n }\n }\n println!(\n \"{}\",\n cmp::max(best_i, n - best_i + 1) * cmp::max(best_j, m - best_j + 1)\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1138", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There are N cards placed face down in a row. On each card, an integer 1 or 2 is written.

\n

Let A_i be the integer written on the i-th card.

\n

Your objective is to guess A_1, A_2, ..., A_N correctly.

\n

You know the following facts:

\n
    \n
  • For each i = 1, 2, ..., M, the value A_{X_i} + A_{Y_i} + Z_i is an even number.
  • \n
\n

You are a magician and can use the following magic any number of times:

\n

Magic: Choose one card and know the integer A_i written on it. The cost of using this magic is 1.

\n

What is the minimum cost required to determine all of A_1, A_2, ..., A_N?

\n

It is guaranteed that there is no contradiction in given input.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq M \\leq 10^5
  • \n
  • 1 \\leq X_i < Y_i \\leq N
  • \n
  • 1 \\leq Z_i \\leq 100
  • \n
  • The pairs (X_i, Y_i) are distinct.
  • \n
  • There is no contradiction in input. (That is, there exist integers A_1, A_2, ..., A_N that satisfy the conditions.)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nX_1 Y_1 Z_1\nX_2 Y_2 Z_2\n\\vdots\nX_M Y_M Z_M\n
\n
\n
\n
\n
\n

Output

Print the minimum total cost required to determine all of A_1, A_2, ..., A_N.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1\n1 2 1\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

You can determine all of A_1, A_2, A_3 by using the magic for the first and third cards.

\n
\n
\n
\n
\n
\n

Sample Input 2

6 5\n1 2 1\n2 3 2\n1 3 3\n4 5 4\n5 6 5\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100000 1\n1 100000 100\n
\n
\n
\n
\n
\n

Sample Output 3

99999\n
\n
\n
", "c_code": "int solution() {\n int i;\n int u;\n int w;\n int z;\n int N;\n int M;\n scanf(\"%d %d\", &N, &M);\n list **adj = (list **)malloc(sizeof(list *) * (N + 1));\n list *d = (list *)malloc(sizeof(list) * M * 2);\n for (i = 1; i <= N; i++) {\n adj[i] = NULL;\n }\n for (i = 0; i < M; i++) {\n scanf(\"%d %d %d\", &u, &w, &z);\n d[i * 2].v = w;\n d[(i * 2) + 1].v = u;\n d[i * 2].next = adj[u];\n d[(i * 2) + 1].next = adj[w];\n adj[u] = &(d[i * 2]);\n adj[w] = &(d[(i * 2) + 1]);\n }\n\n int k;\n int flag[100001] = {};\n int q[100001];\n int head;\n int tail;\n list *p;\n for (i = 1, k = 0; i <= N; i++) {\n if (flag[i] != 0) {\n continue;\n }\n flag[i] = ++k;\n q[0] = i;\n for (head = 0, tail = 1; head < tail; head++) {\n for (p = adj[q[head]]; p != NULL; p = p->next) {\n if (flag[p->v] == 0) {\n flag[p->v] = k;\n q[tail++] = p->v;\n }\n }\n }\n }\n\n printf(\"%d\\n\", k);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n m: usize,\n xyz: [(usize1, usize1, usize); m]\n }\n let mut graph = vec![vec![]; n];\n for &(x, y, _) in &xyz {\n graph[x].push(y);\n graph[y].push(x);\n }\n let mut count = 0;\n let mut dist = vec![false; n];\n\n for i in 0..n {\n if dist[i] {\n continue;\n }\n dist[i] = true;\n count += 1;\n let mut queue = VecDeque::new();\n queue.push_back(i);\n while let Some(x) = queue.pop_front() {\n for &p in &graph[x] {\n if dist[p] {\n continue;\n }\n dist[p] = true;\n queue.push_back(p);\n }\n }\n }\n println!(\"{}\", count);\n}", "difficulty": "medium"} {"problem_id": "1139", "problem_description": "You're given an array of $$$n$$$ integers between $$$0$$$ and $$$n$$$ inclusive.In one operation, you can choose any element of the array and replace it by the MEX of the elements of the array (which may change after the operation).For example, if the current array is $$$[0, 2, 2, 1, 4]$$$, you can choose the second element and replace it by the MEX of the present elements  — $$$3$$$. Array will become $$$[0, 3, 2, 1, 4]$$$.You must make the array non-decreasing, using at most $$$2n$$$ operations.It can be proven that it is always possible. Please note that you do not have to minimize the number of operations. If there are many solutions, you can print any of them. –An array $$$b[1 \\ldots n]$$$ is non-decreasing if and only if $$$b_1 \\le b_2 \\le \\ldots \\le b_n$$$.The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of $$$[2, 2, 1]$$$ is $$$0$$$, because $$$0$$$ does not belong to the array. The MEX of $$$[3, 1, 0, 1]$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the array, but $$$2$$$ does not. The MEX of $$$[0, 3, 1, 2]$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the array, but $$$4$$$ does not. It's worth mentioning that the MEX of an array of length $$$n$$$ is always between $$$0$$$ and $$$n$$$ inclusive.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n int i;\n int j;\n int k;\n int a[1003];\n int ans[2003];\n int cnt[1003];\n int l;\n int r;\n for (; t > 0; t--) {\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n k = 0;\n for (i = 0; i <= n; i++) {\n cnt[i] = 0;\n }\n for (i = 0; i < n; i++) {\n cnt[a[i]]++;\n }\n l = 0;\n r = n - 1;\n while (a[l] == l) {\n l++;\n }\n while (a[r] == r + 1) {\n r--;\n }\n while (l <= r) {\n j = 0;\n while (cnt[j] > 0) {\n j++;\n }\n if (j == l) {\n ans[k] = j + 1;\n k++;\n cnt[a[j]]--;\n a[j] = j;\n cnt[j]++;\n } else {\n ans[k] = j;\n k++;\n cnt[a[j - 1]]--;\n a[j - 1] = j;\n cnt[j]++;\n }\n while (a[l] == l) {\n l++;\n }\n while (a[r] == r + 1) {\n r--;\n }\n }\n printf(\"%d\\n\", k);\n if (k > 0) {\n for (i = 0; i < k - 1; i++) {\n printf(\"%d \", ans[i]);\n }\n printf(\"%d\\n\", ans[k - 1]);\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 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: Vec = Vec::new();\n for _ in 0..(2 * n) {\n let mut bs: Vec = vec![false; n + 1];\n let mut sorted = true;\n bs[xs[0]] = true;\n for i in 1..n {\n bs[xs[i]] = true;\n sorted = sorted && xs[i - 1] <= xs[i];\n }\n let k = bs.iter().position(|b| !b).unwrap();\n if k == n {\n if sorted {\n break;\n } else {\n for i in 0..n {\n if xs[i] != i {\n xs[i] = n;\n ans.push(i + 1);\n break;\n }\n }\n }\n } else {\n for i in k..n {\n if xs[i] != i {\n xs[i] = k;\n ans.push(i + 1);\n break;\n }\n }\n }\n }\n println!(\"{}\", ans.len());\n for i in 0..ans.len() {\n print!(\"{} \", ans[i]);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1140", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Snuke has a string S consisting of three kinds of letters: a, b and c.

\n

He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 10^5
  • \n
  • S consists of a, b and c.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If the objective is achievable, print YES; if it is unachievable, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

abac\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

As it stands now, S contains a palindrome aba, but we can permute the characters to get acba, for example, that does not contain a palindrome of length 2 or more.

\n
\n
\n
\n
\n
\n

Sample Input 2

aba\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 3

babacccabab\n
\n
\n
\n
\n
\n

Sample Output 3

YES\n
\n
\n
", "c_code": "int solution() {\n int S_length = 0;\n int count_a = 0;\n int count_b = 0;\n int count_c = 0;\n char S[100001];\n scanf(\"%s\", S);\n while (S[S_length] != '\\0') {\n S_length++;\n }\n\n for (int i = 0; i < S_length; i++) {\n switch (S[i]) {\n case 'a':\n count_a++;\n break;\n case 'b':\n count_b++;\n break;\n case 'c':\n count_c++;\n break;\n }\n }\n\n if (abs(count_a - count_b) < 2 && abs(count_a - count_c) < 2 &&\n abs(count_b - count_c) < 2) {\n puts(\"YES\");\n } else {\n puts(\"NO\");\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 let s: Vec = itr.next().unwrap().chars().collect();\n let n = s.len();\n if n == 1 {\n println!(\"YES\");\n } else if n == 2 {\n if s[0] == s[1] {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n }\n } else {\n let mut na = 0;\n let mut nb = 0;\n let mut nc = 0;\n for &c in s.iter() {\n if c == 'a' {\n na += 1;\n } else if c == 'b' {\n nb += 1;\n } else {\n nc += 1;\n }\n }\n let min = min(na, min(nb, nc));\n na -= min;\n nb -= min;\n nc -= min;\n if max(na, max(nb, nc)) > 1 {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1141", "problem_description": "A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 ≤ i ≤ n) (n is the permutation size) the following equations hold ppi = i and pi ≠ i. Nickolas asks you to print any perfect permutation of size n for the given n.", "c_code": "int solution() {\n int a = 0;\n int i = 0;\n\n scanf(\"%d\", &a);\n if (a % 2) {\n printf(\"-1\");\n } else {\n for (i = 1; i < a + 1; i++) {\n if (i % 2 == 0) {\n printf(\"%d \", i - 1);\n } else {\n printf(\"%d \", i + 1);\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut guess = String::new();\n io::stdin()\n .read_line(&mut guess)\n .expect(\"Failed to read line\");\n let guess: u32 = match guess.trim().parse() {\n Ok(number) => number,\n Err(_) => 0,\n };\n if !guess.is_multiple_of(2) {\n println!(\"-1\");\n return;\n }\n let vec: Vec = (0..guess).collect();\n\n if vec.len() == 1 || vec.is_empty() {\n println!(\"-1\");\n return;\n }\n for i in 1..vec.len() + 1 {\n if i % 2 == 0 {\n println!(\"{}\", i - 1);\n } else {\n println!(\"{}\", i + 1);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1142", "problem_description": "You are given a string $$$s=s_1s_2\\dots s_n$$$ of length $$$n$$$, which only contains digits $$$1$$$, $$$2$$$, ..., $$$9$$$.A substring $$$s[l \\dots r]$$$ of $$$s$$$ is a string $$$s_l s_{l + 1} s_{l + 2} \\ldots s_r$$$. A substring $$$s[l \\dots r]$$$ of $$$s$$$ is called even if the number represented by it is even. Find the number of even substrings of $$$s$$$. Note, that even if some substrings are equal as strings, but have different $$$l$$$ and $$$r$$$, they are counted as different substrings.", "c_code": "int solution() {\n int n;\n int sum = 0;\n char a[70000];\n scanf(\"%d\", &n);\n scanf(\"%s\", a);\n for (int i = 0; i < n; i++) {\n if ((a[i]) % 2 == 0 && a[i] != 0) {\n sum += (i + 1);\n }\n }\n printf(\"%d\", sum);\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"INPUT:: read line failed\");\n let amount: usize = input.trim().parse().expect(\"amount: parse to usize failed\");\n input.clear();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"INPUT:: read line failed\");\n\n let sentence = &mut input.trim().chars();\n\n let mut outcome = usize::default();\n\n for counter in 0usize..amount {\n let letter = sentence.next().unwrap();\n\n let number = letter.to_digit(10).unwrap();\n\n if number.is_multiple_of(2) {\n outcome += counter + 1;\n }\n }\n\n println!(\"{}\", &outcome);\n}", "difficulty": "hard"} {"problem_id": "1143", "problem_description": "A positive integer $$$x$$$ is called a power of two if it can be represented as $$$x = 2^y$$$, where $$$y$$$ is a non-negative integer. So, the powers of two are $$$1, 2, 4, 8, 16, \\dots$$$.You are given two positive integers $$$n$$$ and $$$k$$$. Your task is to represent $$$n$$$ as the sum of exactly $$$k$$$ powers of two.", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n if (k > n) {\n printf(\"NO\");\n return 0;\n }\n int b[31] = {0};\n\n int j = 30;\n int even = 0;\n while (n > 0) {\n b[j] = n % 2;\n if (n % 2) {\n even++;\n }\n n /= 2;\n j--;\n }\n\n for (int z = 0; z < 31; z++) {\n if (even < k && b[z] >= 1) {\n b[z + 1] += 2;\n even++;\n b[z]--;\n }\n if (b[z] >= 1 && even < k) {\n z--;\n }\n }\n\n int lan = 1;\n if (even == k) {\n printf(\"YES\\n\");\n for (int j = 30; j >= 0; j--) {\n while (b[j]) {\n\n printf(\"%d \", lan);\n b[j]--;\n }\n lan *= 2;\n }\n } else {\n printf(\"NO\");\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_to_string(&mut buf).unwrap();\n let mut tok = buf.split_whitespace();\n let mut get = || tok.next().unwrap();\n\n\n let n = get!();\n let k = get!(usize);\n\n let mut p = n;\n let mut b = 1_i64;\n let mut bits = vec![];\n\n while p > 0 {\n if p & 1 != 0 {\n bits.push(b);\n }\n p >>= 1;\n b <<= 1;\n }\n\n bits.sort();\n\n let mut r = k;\n let mut ans = vec![];\n while r > 0 && !bits.is_empty() {\n if bits.len() == r {\n let e = bits.pop().unwrap();\n ans.push(e);\n r -= 1;\n } else {\n let e = bits.pop().unwrap();\n if e == 1 {\n ans.push(1);\n r -= 1;\n } else {\n bits.push(e / 2);\n bits.push(e / 2);\n }\n }\n }\n if r > 0 || !bits.is_empty() {\n println!(\"NO\");\n return;\n }\n println!(\"YES\");\n for i in 0..ans.len() {\n if i == 0 {\n print!(\"{}\", ans[i]);\n } else {\n print!(\" {}\", ans[i]);\n }\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "1144", "problem_description": "The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n char *array = (char *)malloc(2 * sizeof(char));\n scanf(\"%s\", array);\n printf(\"%d\\n\", ((array[0] - 97) * 25) +\n (array[0] > array[1] ? array[1] - 96 : array[1] - 97));\n free(array);\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();\n let t = input.next().unwrap().parse::().unwrap();\n for _ in 0..t {\n let s = input.next().unwrap().as_bytes();\n let a = s[0] - b'a';\n let b = s[1] - b'a';\n let mut index = a as u32 * 25 + b as u32;\n if b < a {\n index += 1;\n }\n writeln!(output, \"{}\", index).ok();\n }\n print!(\"{output}\");\n}", "difficulty": "medium"} {"problem_id": "1145", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

This contest is CODEFESTIVAL, which can be shortened to the string CF by deleting some characters.

\n

Mr. Takahashi, full of curiosity, wondered if he could obtain CF from other strings in the same way.

\n

You are given a string s consisting of uppercase English letters.\nDetermine whether the string CF can be obtained from the string s by deleting some characters.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ |s| ≤ 100
  • \n
  • All characters in s are uppercase English letters (A-Z).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Print Yes if the string CF can be obtained from the string s by deleting some characters.\nOtherwise print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

CODEFESTIVAL\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

CF is obtained by deleting characters other than the first character C and the fifth character F.

\n
\n
\n
\n
\n
\n

Sample Input 2

FESTIVALCODE\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

FC can be obtained but CF cannot be obtained because you cannot change the order of the characters.

\n
\n
\n
\n
\n
\n

Sample Input 3

CF\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n

It is also possible not to delete any characters.

\n
\n
\n
\n
\n
\n

Sample Input 4

FCF\n
\n
\n
\n
\n
\n

Sample Output 4

Yes\n
\n

CF is obtained by deleting the first character.

\n
\n
", "c_code": "int solution(void) {\n char s[100];\n int a = 0;\n int i = 0;\n\n scanf(\"%s\", s);\n\n for (i = 0; i < 100; i++) {\n if (a == 0 && s[i] == 'C') {\n a++;\n }\n if (a == 1 && s[i] == 'F') {\n a++;\n break;\n }\n }\n\n if (a == 2) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let y: Vec = st.chars().collect();\n let mut x: i32 = 0;\n for i in y {\n if x == 0 {\n if i == 'C' {\n x += 1;\n };\n } else {\n if i == 'F' {\n x += 1;\n }\n }\n }\n if x >= 2 {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "1146", "problem_description": "Binary search", "c_code": "int solution(const int *arr, int l, int r, int x) {\n if (r >= l) {\n int mid = l + ((r - l) / 2);\n\n if (arr[mid] == x) {\n return mid;\n }\n\n if (arr[mid] > x) {\n return binarysearch1(arr, l, mid - 1, x);\n }\n\n return binarysearch1(arr, mid + 1, r, x);\n }\n\n return -1;\n}\n", "rust_code": "fn solution(item: &T, arr: &[T]) -> Option {\n let is_asc = arr.len() > 1 && arr[0] < arr[arr.len() - 1];\n\n let mut left = 0;\n let mut right = arr.len();\n\n while left < right {\n let mid = left + (right - left) / 2;\n let cmp_result = item.cmp(&arr[mid]);\n\n match (is_asc, cmp_result) {\n (true, Ordering::Less) | (false, Ordering::Greater) => {\n right = mid;\n }\n (true, Ordering::Greater) | (false, Ordering::Less) => {\n left = mid + 1;\n }\n (_, Ordering::Equal) => {\n return Some(mid);\n }\n }\n }\n\n None\n}\n", "difficulty": "hard"} {"problem_id": "1147", "problem_description": "Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.Their algorithm will be tested on an array of integers, where the $$$i$$$-th integer represents the color of the $$$i$$$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $$$k$$$, and each color should belong to exactly one group.Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $$$k$$$ to the right. To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.", "c_code": "int solution() {\n int input;\n int n;\n int k;\n int p[256];\n\n scanf(\"%d%d\", &n, &k);\n for (int i = 0; i < 256; i++) {\n p[i] = -1;\n }\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &input);\n if (p[input] == -1) {\n int temp = (input - k + 1) < 0 ? 0 : (input - k + 1);\n for (int j = temp; j <= input; ++j) {\n if (p[j] == -1 || p[j] == j) {\n for (int l = j; l <= input; ++l) {\n p[l] = j;\n }\n break;\n }\n }\n }\n printf(\"%d \", p[input]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n use std::io::prelude::*;\n\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let _n: usize = it.next().unwrap().parse().unwrap();\n let k: usize = it.next().unwrap().parse().unwrap();\n\n let p: Vec = it.map(|x| x.parse().unwrap()).collect();\n\n let mut rl = vec![None; 256];\n\n let mut mil = vec![false; 256];\n\n for &p in p.iter() {\n if rl[p] == None {\n let mut f = p;\n loop {\n if mil[f] {\n f += 1;\n break;\n }\n if f == 0 {\n break;\n }\n if f + k == p + 1 {\n break;\n }\n f -= 1;\n }\n for i in f..f + k {\n if i == 256 || mil[i] {\n break;\n }\n if i < p {\n mil[i] = true;\n }\n rl[i] = Some(f);\n }\n }\n mil[p] = true;\n }\n\n let mut ans = String::new();\n for p in p {\n let s = format!(\"{} \", rl[p].unwrap());\n ans.push_str(&s);\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1148", "problem_description": "Vasya adores sport programming. He can't write programs but he loves to watch the contests' progress. Vasya even has a favorite coder and Vasya pays special attention to him.One day Vasya decided to collect the results of all contests where his favorite coder participated and track the progress of his coolness. For each contest where this coder participated, he wrote out a single non-negative number — the number of points his favorite coder earned in the contest. Vasya wrote out the points for the contest in the order, in which the contests run (naturally, no two contests ran simultaneously).Vasya considers a coder's performance in a contest amazing in two situations: he can break either his best or his worst performance record. First, it is amazing if during the contest the coder earns strictly more points that he earned on each past contest. Second, it is amazing if during the contest the coder earns strictly less points that he earned on each past contest. A coder's first contest isn't considered amazing. Now he wants to count the number of amazing performances the coder had throughout his whole history of participating in contests. But the list of earned points turned out long and Vasya can't code... That's why he asks you to help him.", "c_code": "int solution() {\n\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n scanf(\"%d\", &arr[0]);\n int max = arr[0];\n int min = arr[0];\n int jlm = 0;\n for (int i = 1; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] > max) {\n jlm++;\n max = arr[i];\n }\n if (arr[i] < min) {\n min = arr[i];\n jlm++;\n }\n }\n printf(\"%d\\n\", jlm);\n return 0;\n}", "rust_code": "fn solution() {\n {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n }\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n\n let (mut minimum, mut maximum, mut delta) = (10001, -1, -2);\n for num in buf\n .split_whitespace()\n .map(|elem| elem.parse::().unwrap())\n {\n if num < minimum {\n minimum = num;\n delta += 1;\n }\n if num > maximum {\n maximum = num;\n delta += 1;\n }\n }\n\n println!(\"{:?}\", delta);\n}", "difficulty": "medium"} {"problem_id": "1149", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N mountains ranging from east to west, and an ocean to the west.

\n

At the top of each mountain, there is an inn. You have decided to choose where to stay from these inns.

\n

The height of the i-th mountain from the west is H_i.

\n

You can certainly see the ocean from the inn at the top of the westmost mountain.

\n

For the inn at the top of the i-th mountain from the west (i = 2, 3, ..., N), you can see the ocean if and only if H_1 \\leq H_i, H_2 \\leq H_i, ..., and H_{i-1} \\leq H_i.

\n

From how many of these N inns can you see the ocean?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 20
  • \n
  • 1 \\leq H_i \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nH_1 H_2 ... H_N\n
\n
\n
\n
\n
\n

Output

Print the number of inns from which you can see the ocean.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n6 5 6 8\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

You can see the ocean from the first, third and fourth inns from the west.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n4 5 3 5 4\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5\n9 5 6 8 4\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution(void) {\n\n int N = 0;\n int Ni[20];\n int max = 0;\n int result = 0;\n scanf(\"%d\", &N);\n\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &Ni[i]);\n }\n\n for (int i = 0; i < N; i++) {\n if (Ni[i] >= max) {\n max = Ni[i];\n result++;\n }\n }\n printf(\"%d\", result);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let hs: Vec = buf.trim().split(\" \").map(|x| x.parse().unwrap()).collect();\n let mut ans = 0;\n let mut max = 0;\n for h in hs {\n if h >= max {\n ans += 1;\n }\n max = std::cmp::max(h, max);\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1150", "problem_description": "\n

Score : 800 points

\n
\n
\n

Problem Statement

This is an interactive task.

\n

Snuke has a favorite positive integer, N. You can ask him the following type of question at most 64 times: \"Is n your favorite integer?\" Identify N.

\n

Snuke is twisted, and when asked \"Is n your favorite integer?\", he answers \"Yes\" if one of the two conditions below is satisfied, and answers \"No\" otherwise:

\n
    \n
  • Both n \\leq N and str(n) \\leq str(N) hold.
  • \n
  • Both n > N and str(n) > str(N) hold.
  • \n
\n

Here, str(x) is the decimal representation of x (without leading zeros) as a string. For example, str(123) = 123 and str(2000) = 2000.\nStrings are compared lexicographically. For example, 11111 < 123 and 123456789 < 9.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^{9}
  • \n
\n
\n
\n
\n
\n

Input and Output

Write your question to Standard Output in the following format:

\n
? n\n
\n

Here, n must be an integer between 1 and 10^{18} (inclusive).

\n

Then, the response to the question shall be given from Standard Input in the following format:

\n
ans\n
\n

Here, ans is either Y or N. Y represents \"Yes\"; N represents \"No\".

\n

Finally, write your answer in the following format:

\n
! n\n
\n

Here, n=N must hold.

\n
\n
\n
\n
\n
\n
\n

Judging

    \n
  • After each output, you must flush Standard Output. Otherwise you may get TLE.
  • \n
  • After you print the answer, the program must be terminated immediately. Otherwise, the behavior of the judge is undefined.
  • \n
  • When your output is invalid or incorrect, the behavior of the judge is undefined (it does not necessarily give WA).
  • \n
\n
\n
\n
\n
\n

Sample

Below is a sample communication for the case N=123:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
InputOutput
? 1
Y
? 32
N
? 1010
N
? 999
Y
! 123
\n
    \n
  • Since 1 \\leq 123 and str(1) \\leq str(123), the first response is \"Yes\".
  • \n
  • Since 32 \\leq 123 but str(32) > str(123), the second response is \"No\".
  • \n
  • Since 1010 > 123 but str(1010) \\leq str(123), the third response is \"No\".
  • \n
  • Since 999 \\geq 123 and str(999) > str(123), the fourth response is \"Yes\".
  • \n
  • The program successfully identifies N=123 in four questions, and thus passes the case.
  • \n
\n
\n
\n
", "c_code": "int solution() {\n int k = 10;\n char res;\n const long long pow[11] = {1, 10, 100, 1000,\n 10000, 100000, 1000000, 10000000,\n 100000000, 1000000000, 10000000000};\n long long tmp = 49999999999;\n long long ans = 0;\n\n printf(\"? %lld\\n\", tmp);\n fflush(stdout);\n scanf(\" %c\", &res);\n if (res == 'Y') {\n for (tmp -= pow[k]; tmp >= pow[k]; tmp -= pow[k]) {\n printf(\"? %lld\\n\", tmp);\n fflush(stdout);\n scanf(\" %c\", &res);\n if (res == 'N') {\n break;\n }\n }\n ans = tmp / pow[k] + 1;\n tmp += pow[k];\n } else {\n for (tmp += pow[k]; tmp <= pow[k] * 9; tmp += pow[k]) {\n printf(\"? %lld\\n\", tmp);\n fflush(stdout);\n scanf(\" %c\", &res);\n if (res == 'Y') {\n break;\n }\n }\n ans = tmp / pow[k];\n }\n printf(\"? %lld\\n\", ((ans - 1) * 10) + 9);\n fflush(stdout);\n scanf(\" %c\", &res);\n\n if ((ans > pow[10 - k] && res == 'Y') || (ans == pow[10 - k] && res == 'N')) {\n for (k--; k >= 0; k--) {\n tmp -= pow[k] * 5;\n printf(\"? %lld\\n\", tmp);\n fflush(stdout);\n scanf(\" %c\", &res);\n if (res == 'Y') {\n for (tmp -= pow[k]; tmp >= ans * pow[k + 1]; tmp -= pow[k]) {\n printf(\"? %lld\\n\", tmp);\n fflush(stdout);\n scanf(\" %c\", &res);\n if (res == 'N') {\n break;\n }\n }\n ans = ans * 10 + (tmp / pow[k] + 1) % 10;\n tmp += pow[k];\n } else {\n for (tmp += pow[k]; tmp < (ans + 1) * pow[k + 1] - 1; tmp += pow[k]) {\n printf(\"? %lld\\n\", tmp);\n fflush(stdout);\n scanf(\" %c\", &res);\n if (res == 'Y') {\n break;\n }\n }\n ans = ans * 10 + tmp / pow[k] % 10;\n }\n printf(\"? %lld\\n\", ((ans - 1) * 10) + 9);\n fflush(stdout);\n scanf(\" %c\", &res);\n if ((ans > pow[10 - k] && res == 'N') ||\n (ans == pow[10 - k] && res == 'Y')) {\n break;\n }\n }\n }\n\n printf(\"! %lld\\n\", ans);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let mut res: i64 = 0;\n for i in 0..15 {\n let mut low: i64 = if res > 0 { -1 } else { 0 };\n let mut high: i64 = 9;\n while high - low > 1 {\n let mid = (low + high) / 2;\n if i < 5 {\n println!(\"? {}9999999999\", 10 * res + mid);\n } else {\n println!(\"? {}999999\", 10 * res + mid);\n }\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n if &buf[0..1] == \"Y\" {\n high = mid;\n } else {\n low = mid;\n }\n }\n res = 10 * res + high;\n println!(\"? {}\", res + 1);\n let str_cmp = res.to_string() < (res + 1).to_string();\n {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n if str_cmp == (&buf[0..1] == \"Y\") {\n println!(\"! {}\", res);\n break;\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1151", "problem_description": "Gregor is learning about RSA cryptography, and although he doesn't understand how RSA works, he is now fascinated with prime numbers and factoring them.Gregor's favorite prime number is $$$P$$$. Gregor wants to find two bases of $$$P$$$. Formally, Gregor is looking for two integers $$$a$$$ and $$$b$$$ which satisfy both of the following properties. $$$P \\bmod a = P \\bmod b$$$, where $$$x \\bmod y$$$ denotes the remainder when $$$x$$$ is divided by $$$y$$$, and $$$2 \\le a < b \\le P$$$. Help Gregor find two bases of his favorite prime number!", "c_code": "int solution() {\n\n int t = 0;\n scanf(\"%d\", &t);\n\n int prime_number = 0;\n int counter = 0;\n\n int results[2000];\n\n for (int i = 0; i < t; i++) {\n\n prime_number = 0;\n scanf(\"%d\", &prime_number);\n\n results[counter] = 2;\n counter++;\n results[counter] = (prime_number - 1);\n counter++;\n }\n\n counter = 0;\n\n for (int m = 0; m < t; m++) {\n printf(\"%d %d\\n\", results[counter], results[counter + 1]);\n counter += 2;\n }\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).expect(\"\");\n let mut t: u32 = t.trim().parse().expect(\"\");\n\n while t > 0 {\n let mut p = String::new();\n io::stdin().read_line(&mut p).expect(\"\");\n let p: u32 = p.trim().parse().expect(\"\");\n println!(\"{} {}\", 2, p - 1);\n\n t -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "1152", "problem_description": "Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters from the set in one line, separated by a comma. He also added an opening curved bracket at the beginning of the line and a closing curved bracket at the end of the line. Unfortunately, from time to time Anton would forget writing some letter and write it again. He asks you to count the total number of distinct letters in his set.", "c_code": "int solution() {\n int a[27];\n int count = 0;\n int ascii = 'a';\n char in[1002];\n for (int i = 0; i < 27; i++) {\n a[i] = 0;\n }\n\n scanf(\" %c\", &in[0]);\n int i = 1;\n int flag = 1;\n while (flag) {\n scanf(\" %c\", &in[i]);\n if (in[i] == '}') {\n break;\n }\n a[in[i] - ascii]++;\n }\n\n printf(\" %d\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let s: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.chars().collect()\n };\n\n let mut set = HashSet::new();\n for &c in s.iter() {\n if let 'a'..='z' = c {\n set.insert(c);\n }\n }\n println!(\"{}\", set.len());\n}", "difficulty": "medium"} {"problem_id": "1153", "problem_description": "\n

Score : 800 points

\n
\n
\n

Problem Statement

Takahashi and Aoki will play a game using a grid with H rows and W columns of square cells.\nThere are N obstacles on this grid; the i-th obstacle is at (X_i,Y_i).\nHere, we represent the cell at the i-th row and j-th column (1 \\leq i \\leq H, 1 \\leq j \\leq W) by (i,j).\nThere is no obstacle at (1,1), and there is a piece placed there at (1,1).

\n

Starting from Takahashi, he and Aoki alternately perform one of the following actions:

\n
    \n
  • Move the piece to an adjacent cell.\n Here, let the position of the piece be (x,y). Then Takahashi can only move the piece to (x+1,y), and Aoki can only move the piece to (x,y+1).\n If the destination cell does not exist or it is occupied by an obstacle, this action cannot be taken.
  • \n
  • Do not move the piece, and end his turn without affecting the grid.
  • \n
\n

The game ends when the piece does not move twice in a row.

\n

Takahashi would like to perform as many actions (including not moving the piece) as possible before the game ends, while Aoki would like to perform as few actions as possible before the game ends.\nHow many actions will Takahashi end up performing?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H,W \\leq 2\\times 10^5
  • \n
  • 0 \\leq N \\leq 2\\times 10^5
  • \n
  • 1 \\leq X_i \\leq H
  • \n
  • 1 \\leq Y_i \\leq W
  • \n
  • If i \\neq j, (X_i,Y_i) \\neq (X_j,Y_j)
  • \n
  • (X_i,Y_i) \\neq (1,1)
  • \n
  • X_i and Y_i are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W N\nX_1 Y_1\n:\nX_N Y_N\n
\n
\n
\n
\n
\n

Output

Print the number of actions Takahashi will end up performing.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3 1\n3 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

For example, the game proceeds as follows:

\n
    \n
  • Takahashi moves the piece to (2,1).
  • \n
  • Aoki does not move the piece.
  • \n
  • Takahashi moves the piece to (3,1).
  • \n
  • Aoki does not move the piece.
  • \n
  • Takahashi does not move the piece.
  • \n
\n

Takahashi performs three actions in this case, but if both players play optimally, Takahashi will perform only two actions before the game ends.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 10 14\n4 3\n2 2\n7 3\n9 10\n7 7\n8 1\n10 10\n5 4\n3 4\n2 8\n6 4\n4 4\n5 8\n9 2\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100000 100000 0\n
\n
\n
\n
\n
\n

Sample Output 3

100000\n
\n
\n
", "c_code": "int solution() {\n long H;\n long W;\n long N;\n long i;\n long j;\n long k;\n long a = 0;\n long *A;\n\n scanf(\"%ld %ld %ld\", &H, &W, &N);\n A = (long *)malloc((H + 1) * sizeof(long));\n for (i = 1; i <= H; i++) {\n A[i] = 0;\n }\n\n for (k = 0; k < N; k++) {\n scanf(\"%ld %ld\", &i, &j);\n if (i < j) {\n continue;\n }\n if (A[i] == 0 || A[i] > j) {\n A[i] = j;\n }\n }\n\n for (i = 1; i <= H; i++) {\n if (A[i] == 0) {\n continue;\n }\n\n if (A[i] == i - a) {\n a++;\n continue;\n }\n\n if (A[i] < i - a) {\n k = 1;\n break;\n }\n }\n\n printf(\"%ld\\n\", i - 1);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n h: usize,\n w: usize,\n n: usize,\n ps: [[usize; 2]; n]\n }\n if h == 1 {\n println!(\"1\");\n return;\n }\n let mut tab = vec![Vec::new(); w];\n let mut set = HashSet::new();\n for i in 0..n {\n if ps[i][0] < ps[i][1] {\n continue;\n }\n tab[ps[i][1] - 1].push(ps[i][0]);\n set.insert(ps[i].clone());\n }\n let mut r = vec![0; w];\n let mut pos = 0;\n for i in 0..w {\n pos += 1;\n while set.contains(&vec![pos, i + 1]) {\n pos += 1;\n }\n let mut min = h + 1;\n for j in 0..tab[i].len() {\n if tab[i][j] > pos && min > tab[i][j] {\n min = tab[i][j];\n }\n }\n r[i] = min;\n }\n println!(\"{}\", r.iter().min().unwrap() - 1);\n}", "difficulty": "hard"} {"problem_id": "1154", "problem_description": "Polycarp bought a new expensive painting and decided to show it to his $$$n$$$ friends. He hung it in his room. $$$n$$$ of his friends entered and exited there one by one. At one moment there was no more than one person in the room. In other words, the first friend entered and left first, then the second, and so on.It is known that at the beginning (before visiting friends) a picture hung in the room. At the end (after the $$$n$$$-th friend) it turned out that it disappeared. At what exact moment it disappeared — there is no information.Polycarp asked his friends one by one. He asked each one if there was a picture when he entered the room. Each friend answered one of three: no (response encoded with 0); yes (response encoded as 1); can't remember (response is encoded with ?). Everyone except the thief either doesn't remember or told the truth. The thief can say anything (any of the three options).Polycarp cannot understand who the thief is. He asks you to find out the number of those who can be considered a thief according to the answers.", "c_code": "int solution() {\n int cases;\n scanf(\"%d\", &cases);\n char string[200000];\n for (int c = 0; c < cases; c++) {\n scanf(\"%s\", &string[0]);\n if (strlen(string) == 1) {\n printf(\"1\\n\");\n continue;\n }\n int start = -1;\n int end = 200001;\n int i = 0;\n while (string[i] != 0) {\n if (string[i] == '0' && end == 200001) {\n end = i;\n }\n i++;\n }\n if (end == 200001) {\n end = i - 1;\n }\n i--;\n while (i >= 0) {\n if (string[i] == '1' && start == -1) {\n start = i;\n }\n i--;\n }\n if (start != -1) {\n start--;\n }\n if (start > end) {\n printf(\"1\\n\");\n } else {\n printf(\"%d\\n\", end - start);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let k = std::io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n for _ in 0..k {\n let mut m = String::new();\n std::io::stdin().read_line(&mut m);\n let m = m\n .trim()\n .split(\"\")\n .flat_map(str::parse::)\n .collect::>();\n let mut ones = vec![];\n let mut zeros = vec![];\n for (index, vals) in m.iter().enumerate() {\n if vals == &'0' {\n zeros.push(index)\n } else if vals == &'1' {\n ones.push(index)\n }\n }\n if ones.is_empty() && zeros.is_empty() {\n println!(\"{}\", m.len());\n } else if !ones.is_empty() && zeros.is_empty() {\n println!(\"{}\", m.len() - ones[ones.len() - 1])\n } else if ones.is_empty() && !zeros.is_empty() {\n println!(\"{}\", zeros[0] + 1)\n } else {\n println!(\"{}\", zeros[0] - ones[ones.len() - 1] + 1);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1155", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

How many multiples of d are there among the integers between L and R (inclusive)?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq L \\leq R \\leq 100
  • \n
  • 1 \\leq d \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
L R d\n
\n
\n
\n
\n
\n

Output

Print the number of multiples of d among the integers between L and R (inclusive).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 10 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n
    \n
  • Among the integers between 5 and 10, there are three multiples of 2: 6, 8, and 10.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

6 20 7\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
    \n
  • Among the integers between 6 and 20, there are two multiples of 7: 7 and 14.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 100 1\n
\n
\n
\n
\n
\n

Sample Output 3

100\n
\n
\n
", "c_code": "int solution() {\n int L;\n int R;\n int d;\n int k = 1;\n int i = 0;\n int n = 0;\n scanf(\"%d%d%d\", &L, &R, &d);\n while (1) {\n n = d * k;\n k++;\n if (n >= L && n <= R) {\n i++;\n }\n if (n > R) {\n break;\n }\n }\n printf(\"%d\\n\", i);\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 let mut it = buf.split_whitespace().map(|n| usize::from_str(n).unwrap());\n let (mut l, r, d) = (it.next().unwrap(), it.next().unwrap(), it.next().unwrap());\n l -= 1;\n println!(\"{}\", r / d - l / d);\n}", "difficulty": "medium"} {"problem_id": "1156", "problem_description": "A camera you have accidentally left in a desert has taken an interesting photo. The photo has a resolution of n pixels width, and each column of this photo is all white or all black. Thus, we can represent the photo as a sequence of n zeros and ones, where 0 means that the corresponding column is all white, and 1 means that the corresponding column is black.You think that this photo can contain a zebra. In this case the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width. For example, the photo [0, 0, 0, 1, 1, 1, 0, 0, 0] can be a photo of zebra, while the photo [0, 0, 0, 1, 1, 1, 1] can not, because the width of the black stripe is 3, while the width of the white stripe is 4. Can the given photo be a photo of zebra or not?", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n int *value = (int *)malloc(sizeof(int[n]));\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &value[i]);\n }\n int index = 1;\n for (; index < n; index++) {\n if (value[index] != value[0]) {\n break;\n }\n }\n if (n % index != 0) {\n printf(\"NO\");\n return 0;\n }\n for (int i = index; i < n; i += index) {\n if (value[i] == value[i - index]) {\n printf(\"NO\");\n return 0;\n }\n for (int j = 0; j < index; j++) {\n if (value[i + j] != value[i]) {\n printf(\"NO\");\n return 0;\n }\n }\n }\n printf(\"YES\");\n return 0;\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n let mut s = String::new();\n let _ = io::stdin().read_line(&mut s).expect(\"???\");\n let numbers: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .trim()\n .split(' ')\n .map(|s| s.parse().unwrap())\n .collect();\n let mut locked: bool = false;\n let mut seq_start = 0;\n let mut current_sequence = 0;\n let mut current = numbers[0];\n for d in numbers {\n if (d != current) & (!locked) {\n seq_start = current_sequence;\n current_sequence = 1;\n locked = true;\n current = d\n } else if (d != current) & (locked) & (current_sequence != seq_start) {\n println!(\"NO\");\n return;\n } else if d != current {\n current_sequence = 1;\n current = d\n } else if !locked {\n current_sequence += 1;\n seq_start += 1;\n } else {\n current_sequence += 1\n }\n }\n if current_sequence != seq_start {\n println!(\"NO\")\n } else {\n println!(\"YES\")\n }\n}", "difficulty": "medium"} {"problem_id": "1157", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \\ldots, p_N.

\n

Among the integers not contained in the sequence p_1, \\ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq X \\leq 100
  • \n
  • 0 \\leq N \\leq 100
  • \n
  • 1 \\leq p_i \\leq 100
  • \n
  • p_1, \\ldots, p_N are all distinct.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X N\np_1 ... p_N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 5\n4 7 10 6 5\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 5\n4 7 10 6 5\n
\n
\n
\n
\n
\n

Sample Output 2

9\n
\n

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

\n
\n
\n
\n
\n
\n

Sample Input 3

100 0\n\n
\n
\n
\n
\n
\n

Sample Output 3

100\n
\n

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.

\n
\n
", "c_code": "int solution() {\n int x = 0;\n int N = 0;\n int b = 0;\n int list[100] = {0};\n scanf(\"%d %d\", &x, &N);\n int check[100][2] = {0};\n\n if (N != 0) {\n for (int i = 0; i < N; i++) {\n scanf(\"%d \", &b);\n list[i] = b;\n }\n }\n\n for (int i = 0; i < N; i++) {\n if (x - list[i] > 0) {\n check[x - list[i]][0] = 1;\n } else if (x - list[i] < 0) {\n check[-(x - list[i])][1] = 1;\n } else {\n check[x - list[i]][0] = 1;\n check[x - list[i]][1] = 1;\n }\n }\n for (int i = 0; i <= N; i++) {\n if (check[i][0] == 0) {\n printf(\"%d\", x - i);\n break;\n }\n if (check[i][1] == 0) {\n printf(\"%d\", x + i);\n break;\n }\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let l1: Vec = buf\n .split_whitespace()\n .filter_map(|x| x.parse().ok())\n .collect();\n let x = l1[0];\n let _n = l1[1];\n\n buf.clear();\n stdin().read_line(&mut buf).unwrap();\n let l2: Vec = buf\n .split_whitespace()\n .filter_map(|x| x.parse().ok())\n .collect();\n let mut answer = -1;\n for p in 0..102 {\n if l2.contains(&p) {\n continue;\n }\n\n if (p - x).abs() < (answer - x).abs() {\n answer = p;\n }\n }\n print!(\"{}\", answer);\n}", "difficulty": "hard"} {"problem_id": "1158", "problem_description": "Alica and Bob are playing a game.Initially they have a binary string $$$s$$$ consisting of only characters 0 and 1.Alice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, Alice makes the third one, and so on. During each move, the current player must choose two different adjacent characters of string $$$s$$$ and delete them. For example, if $$$s = 1011001$$$ then the following moves are possible: delete $$$s_1$$$ and $$$s_2$$$: $$$\\textbf{10}11001 \\rightarrow 11001$$$; delete $$$s_2$$$ and $$$s_3$$$: $$$1\\textbf{01}1001 \\rightarrow 11001$$$; delete $$$s_4$$$ and $$$s_5$$$: $$$101\\textbf{10}01 \\rightarrow 10101$$$; delete $$$s_6$$$ and $$$s_7$$$: $$$10110\\textbf{01} \\rightarrow 10110$$$. If a player can't make any move, they lose. Both players play optimally. You have to determine if Alice can win.", "c_code": "int solution()\n\n{\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char s[144] = {0};\n int one = 0;\n int zero = 0;\n scanf(\"%s\", s);\n for (int i = 0; s[i] != '\\0'; i++) {\n if (s[i] == '1') {\n one++;\n } else {\n zero++;\n }\n }\n if ((one > zero && zero % 2 == 0) || (zero >= one && one % 2 == 0)) {\n printf(\"NET\\n\");\n } else {\n printf(\"DA\\n\");\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 cs: Vec = lines.next().unwrap().unwrap().chars().collect();\n let zeros = cs.iter().filter(|&&c| c == '0').count();\n let ones = cs.len() - zeros;\n let x = min(zeros, ones);\n println!(\"{}\", if x % 2 == 0 { \"NET\" } else { \"DA\" });\n }\n}", "difficulty": "easy"} {"problem_id": "1159", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\\/\\/\\/ when the following conditions are satisfied:

\n
    \n
  • For each i = 1,2,..., n-2, a_i = a_{i+2}.
  • \n
  • Exactly two different numbers appear in the sequence.
  • \n
\n

You are given a sequence v_1,v_2,...,v_n whose length is even.\nWe would like to make this sequence /\\/\\/\\/ by replacing some of its elements.\nFind the minimum number of elements that needs to be replaced.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq n \\leq 10^5
  • \n
  • n is even.
  • \n
  • 1 \\leq v_i \\leq 10^5
  • \n
  • v_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n\nv_1 v_2 ... v_n\n
\n
\n
\n
\n
\n

Output

Print the minimum number of elements that needs to be replaced.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n3 1 3 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

The sequence 3,1,3,2 is not /\\/\\/\\/, but we can make it /\\/\\/\\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

\n
\n
\n
\n
\n
\n

Sample Input 2

6\n105 119 105 119 105 119\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

The sequence 105,119,105,119,105,119 is /\\/\\/\\/.

\n
\n
\n
\n
\n
\n

Sample Input 3

4\n1 1 1 1\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n

The elements of the sequence 1,1,1,1 are all the same, so it is not /\\/\\/\\/.

\n
\n
", "c_code": "int solution(void) {\n int n;\n int v[100000];\n int v1[100000] = {0};\n int pv1 = 0;\n int pv2 = 0;\n int pv2_2 = 0;\n int pv1_2 = 0;\n int v2[100000] = {0};\n\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &v[i]);\n if (i % 2 == 0) {\n v2[v[i]]++;\n if (v2[pv2] < v2[v[i]]) {\n pv2_2 = pv2;\n pv2 = v[i];\n } else {\n if (v2[pv2_2] < v2[v[i]] && v[i] != pv2) {\n pv2_2 = v[i];\n }\n }\n } else {\n v1[v[i]]++;\n if (v1[pv1] < v1[v[i]]) {\n pv1_2 = pv1;\n pv1 = v[i];\n } else {\n if (v1[pv1_2] < v1[v[i]] && v[i] != pv1) {\n pv1_2 = v[i];\n }\n }\n }\n }\n\n if (pv1 == pv2) {\n if (v1[pv1_2] > v2[pv2_2]) {\n pv1 = pv1_2;\n } else {\n pv2 = pv2_2;\n }\n }\n\n int cnt = 0;\n for (int i = 0; i < n; i++) {\n if (i % 2 == 0) {\n if (v[i] != pv2) {\n cnt++;\n }\n } else {\n if (v[i] != pv1) {\n cnt++;\n }\n }\n }\n\n printf(\"%d\", cnt);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf: String = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let n: usize = buf.trim().parse::().unwrap();\n let mut v1: Vec = vec![0; 100001];\n let mut v2: Vec = vec![0; 100001];\n let mut buf: String = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n while let Some(i) = itr.next() {\n v1[i.parse::().unwrap()] += 1;\n if let Some(i) = itr.next() {\n v2[i.parse::().unwrap()] += 1;\n }\n }\n let (mut m11, mut m12, mut m21, mut m22, mut m31, mut m32): (\n usize,\n usize,\n usize,\n usize,\n usize,\n usize,\n ) = (0, 0, 0, 0, 0, 0);\n for x in 0..100001 {\n if v1[x] > m12 {\n if v1[x] > m11 {\n m12 = m11;\n m11 = v1[x];\n m31 = x;\n } else {\n m12 = v1[x];\n }\n }\n if v2[x] > m22 {\n if v2[x] > m21 {\n m22 = m21;\n m21 = v2[x];\n m32 = x;\n } else {\n m22 = v2[x];\n }\n }\n }\n println!(\n \"{}\",\n if m31 != m32 {\n n - m11 - m21\n } else {\n std::cmp::min(n - m11 - m22, n - m12 - m21)\n }\n );\n}", "difficulty": "medium"} {"problem_id": "1160", "problem_description": "William really likes the cellular automaton called \"Game of Life\" so he decided to make his own version. For simplicity, William decided to define his cellular automaton on an array containing $$$n$$$ cells, with each cell either being alive or dead.Evolution of the array in William's cellular automaton occurs iteratively in the following way: If the element is dead and it has exactly $$$1$$$ alive neighbor in the current state of the array, then on the next iteration it will become alive. For an element at index $$$i$$$ the neighbors would be elements with indices $$$i - 1$$$ and $$$i + 1$$$. If there is no element at that index, it is considered to be a dead neighbor. William is a humane person so all alive elements stay alive. Check the note section for examples of the evolution.You are given some initial state of all elements and you need to help William find the state of the array after $$$m$$$ iterations of evolution.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int a;\n int b;\n int dk;\n scanf(\"%d%d\", &a, &b);\n char s[a];\n int n[a];\n for (int i = 0; i < a; i++) {\n n[i] = 0;\n }\n scanf(\"%s\", s);\n while (b--) {\n dk = 0;\n for (int i = 0; i < a; i++) {\n if (s[i] == '1' && n[i] == 0) {\n if (i <= a - 2 && s[i + 1] == '0' &&\n (i + 2 == a || s[i + 2] == '0')) {\n s[i + 1] = '1';\n n[i + 1] = 1;\n dk = 1;\n }\n if (i >= 1 && s[i - 1] == '0' &&\n (i - 1 == 0 || s[i - 2] == '0' || n[i - 2] == 1)) {\n s[i - 1] = '1';\n n[i - 1] = 1;\n dk = 1;\n }\n }\n }\n if (dk == 0) {\n break;\n }\n for (int i = 0; i < a; i++) {\n n[i] = 0;\n }\n }\n puts(s);\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 t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let n = it.next().unwrap();\n let m = it.next().unwrap();\n\n let mut a = Vec::with_capacity(n);\n\n let s = lines.next().unwrap();\n a.extend(\n s.chars()\n .enumerate()\n .filter_map(|(i, c)| if c == '1' { Some(i) } else { None }),\n );\n\n if let Some(&i) = a.first() {\n let j = i.saturating_sub(m);\n\n for _ in 0..j {\n write!(&mut output, \"0\").unwrap();\n }\n for _ in j..i {\n write!(&mut output, \"1\").unwrap();\n }\n }\n\n for b in a.windows(2) {\n let i = b[0];\n let j = b[1];\n let k = j - i - 1;\n let q = k / 2;\n\n let x = std::cmp::min(m, q);\n\n write!(&mut output, \"1\").unwrap();\n\n for _ in 0..x {\n write!(&mut output, \"1\").unwrap();\n }\n for _ in 2 * x..k {\n write!(&mut output, \"0\").unwrap();\n }\n for _ in 0..x {\n write!(&mut output, \"1\").unwrap();\n }\n }\n\n if let Some(&i) = a.last() {\n let x = std::cmp::min(m + 1, n - i);\n\n for _ in 0..x {\n write!(&mut output, \"1\").unwrap();\n }\n for _ in x..n - i {\n write!(&mut output, \"0\").unwrap();\n }\n } else {\n write!(&mut output, \"{}\", s).unwrap();\n }\n\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1161", "problem_description": "

Debt Hell

\n\n

\nYour friend who lives in undisclosed country is involved in debt. He is borrowing 100,000-yen from a loan shark. The loan shark adds 5% interest of the debt and rounds it to the nearest 1,000 above week by week.\n

\n\n

\nWrite a program which computes the amount of the debt in n weeks. \n

\n\n

Input

\n\n

\nAn integer n (0 ≤ n ≤ 100) is given in a line.\n

\n\n

Output

\n\n

\nPrint the amout of the debt in a line.\n

\n\n

Sample Input

\n\n
\n5\n
\n\n

Output for the Sample Input

\n\n
\n130000\n
", "c_code": "int solution() {\n int n = 0;\n int awia = 100000;\n\n scanf(\"%d\", &n);\n while (--n >= 0) {\n awia += awia * 0.05;\n if (awia % 1000) {\n awia = (awia / 1000 * 1000) + 1000;\n }\n }\n printf(\"%d\\n\", awia);\n return 0;\n}", "rust_code": "fn solution() {\n let mut debt = 100000;\n let mut buf = String::new();\n if let Ok(_) = stdin().read_line(&mut buf) {\n let week: u32 = buf.trim().parse::().unwrap();\n for _ in 1..week + 1 {\n let interest = debt * 5 / 100;\n debt += (interest / 1000) * 1000;\n if interest % 1000 > 0 {\n debt += 1000;\n }\n }\n println!(\"{}\", debt);\n }\n}", "difficulty": "medium"} {"problem_id": "1162", "problem_description": "Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.", "c_code": "int solution() {\n int n;\n char S[100010];\n while (scanf(\"%d\", &n) != EOF) {\n int con[2] = {0};\n int count = 1;\n scanf(\"%s\", S);\n for (int i = 1; i < n; i++) {\n if (S[i - 1] == S[i]) {\n con[S[i] - '0']++;\n } else if (S[i - 1] != S[i]) {\n count++;\n }\n if (i - 3 >= 0 && S[i - 3] == S[i - 2] && S[i - 2] != S[i - 1] &&\n S[i - 1] == S[i]) {\n con[0] = 2;\n }\n }\n con[0] += con[1];\n if (con[0] > 2) {\n con[0] = 2;\n }\n printf(\"%d\\n\", count + con[0]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut iter = stdin.lock().lines();\n iter.next().unwrap().unwrap();\n let line = iter.next().unwrap().unwrap();\n let (n, v) = line.bytes().fold((0, vec![]), |(n, mut v), c| {\n v.push(c == 49);\n (n + 1, v)\n });\n println!(\n \"{}\",\n min(\n n,\n 3 + v\n .iter()\n .fold((0, None), |(r, last), c| (\n if last == Some(!c) { r + 1 } else { r },\n Some(*c)\n ))\n .0\n )\n );\n}", "difficulty": "hard"} {"problem_id": "1163", "problem_description": "The robot is located on a checkered rectangular board of size $$$n \\times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.The sequence of commands $$$s$$$ executed by the robot is given. Each command is denoted by one of the symbols 'L', 'R', 'D' or 'U', and triggers the movement to left, right, down or up, respectively.The robot can start its movement in any cell. The robot executes the commands starting from the first one, strictly in the order in which they are listed in $$$s$$$. If the robot moves beyond the edge of the board, it falls and breaks. A command that causes the robot to break is not considered successfully executed.The robot's task is to execute as many commands as possible without falling off the board. For example, on board $$$3 \\times 3$$$, if the robot starts a sequence of actions $$$s=$$$\"RRDLUU\" (\"right\", \"right\", \"down\", \"left\", \"up\", \"up\") from the central cell, the robot will perform one command, then the next command will force him to cross the edge. If the robot starts moving from the cell $$$(2, 1)$$$ (second row, first column) then all commands will be executed successfully and the robot will stop at the cell $$$(1, 2)$$$ (first row, second column). The robot starts from cell $$$(2, 1)$$$ (second row, first column). It moves right, right, down, left, up, and up. In this case it ends in the cell $$$(1, 2)$$$ (first row, second column). Determine the cell from which the robot should start its movement in order to execute as many commands as possible.", "c_code": "int solution() {\n int n;\n int m;\n int i;\n int j;\n int t;\n int up;\n int down;\n int right;\n int left;\n int row;\n int column;\n int h;\n int v;\n scanf(\"%d\", &t);\n char s[1000000 + 1];\n for (j = 0; j < t; j++) {\n up = 0;\n down = 0;\n right = 0;\n left = 0;\n column = 0;\n row = 0;\n scanf(\"%d%d\", &m, &n);\n scanf(\"%s\", s);\n i = 0;\n while (s[i] != '\\0') {\n if (s[i] == 'L') {\n row--;\n if ((-row) > left) {\n left = -row;\n if ((left + right) >= n) {\n left--;\n break;\n }\n }\n }\n if (s[i] == 'R') {\n row++;\n if (row > right) {\n right = row;\n if ((left + right) >= n) {\n right--;\n break;\n }\n }\n }\n if (s[i] == 'D') {\n column--;\n if ((-column) > down) {\n down = -column;\n if ((up + down) >= m) {\n down--;\n break;\n }\n }\n }\n if (s[i] == 'U') {\n column++;\n if ((column) > up) {\n up = column;\n if ((up + down) >= m) {\n up--;\n break;\n }\n }\n }\n i++;\n }\n\n h = 1 + left;\n v = 1 + up;\n printf(\"%d %d\\n\", v, h);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n let mut input = input.split_ascii_whitespace();\n\n let t: u64 = input.next().unwrap().parse().unwrap();\n 'main_loop: for _ in 0..t {\n let rows: i64 = input.next().unwrap().parse().unwrap();\n let cols: i64 = input.next().unwrap().parse().unwrap();\n let s = input.next().unwrap();\n let mut x = 0i64;\n let mut y = 0i64;\n let mut left = 0;\n let mut right = 0;\n let mut top = 0;\n let mut bottom = 0;\n let mut offsets = Vec::with_capacity(s.len() + 1);\n offsets.push((0, 0, 0, 0));\n for c in s.chars() {\n match c {\n 'L' => x -= 1,\n 'R' => x += 1,\n 'D' => y += 1,\n 'U' => y -= 1,\n _ => unreachable!(),\n }\n left = min(x, left);\n right = max(x, right);\n top = min(y, top);\n bottom = max(y, bottom);\n offsets.push((left, right, top, bottom))\n }\n\n let check = |curr: (i64, i64, i64, i64)| {\n -curr.0 <= cols - 1 - curr.1 && -curr.2 <= rows - 1 - curr.3\n };\n let mut l = 0;\n let mut r = s.len();\n while r - l > 1 {\n let m = l + (r - l) / 2;\n let curr = offsets[m];\n if check(curr) {\n l = m;\n } else {\n r = m;\n }\n }\n if check(offsets[r]) {\n println!(\"{} {}\", -offsets[r].2 + 1, -offsets[r].0 + 1)\n } else {\n println!(\"{} {}\", -offsets[l].2 + 1, -offsets[l].0 + 1)\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1164", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N students and M checkpoints on the xy-plane.
\nThe coordinates of the i-th student (1 \\leq i \\leq N) is (a_i,b_i), and the coordinates of the checkpoint numbered j (1 \\leq j \\leq M) is (c_j,d_j).
\nWhen the teacher gives a signal, each student has to go to the nearest checkpoint measured in Manhattan distance.
\nThe Manhattan distance between two points (x_1,y_1) and (x_2,y_2) is |x_1-x_2|+|y_1-y_2|.
\nHere, |x| denotes the absolute value of x.
\nIf there are multiple nearest checkpoints for a student, he/she will select the checkpoint with the smallest index.
\nWhich checkpoint will each student go to?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N,M \\leq 50
  • \n
  • -10^8 \\leq a_i,b_i,c_j,d_j \\leq 10^8
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N M\na_1 b_1\n:  \na_N b_N\nc_1 d_1\n:  \nc_M d_M\n
\n
\n
\n
\n
\n

Output

Print N lines.
\nThe i-th line (1 \\leq i \\leq N) should contain the index of the checkpoint for the i-th student to go.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n2 0\n0 0\n-1 0\n1 0\n
\n
\n
\n
\n
\n

Sample Output 1

2\n1\n
\n

The Manhattan distance between the first student and each checkpoint is:

\n
    \n
  • For checkpoint 1: |2-(-1)|+|0-0|=3
  • \n
  • For checkpoint 2: |2-1|+|0-0|=1
  • \n
\n

The nearest checkpoint is checkpoint 2. Thus, the first line in the output should contain 2.

\n

The Manhattan distance between the second student and each checkpoint is:

\n
    \n
  • For checkpoint 1: |0-(-1)|+|0-0|=1
  • \n
  • For checkpoint 2: |0-1|+|0-0|=1
  • \n
\n

When there are multiple nearest checkpoints, the student will go to the checkpoint with the smallest index. Thus, the second line in the output should contain 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 4\n10 10\n-10 -10\n3 3\n1 2\n2 3\n3 5\n3 5\n
\n
\n
\n
\n
\n

Sample Output 2

3\n1\n2\n
\n

There can be multiple checkpoints at the same coordinates.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 5\n-100000000 -100000000\n-100000000 100000000\n100000000 -100000000\n100000000 100000000\n0 0\n0 0\n100000000 100000000\n100000000 -100000000\n-100000000 100000000\n-100000000 -100000000\n
\n
\n
\n
\n
\n

Sample Output 3

5\n4\n3\n2\n1\n
\n
\n
", "c_code": "int solution() {\n int a;\n int b;\n scanf(\"%d %d\", &a, &b);\n int vetor[2 * a];\n int vet[2 * b];\n for (int x = 0; x < 2 * a; ++x) {\n scanf(\"%d\", &vetor[x]);\n }\n for (int z = 0; z < 2 * b; ++z) {\n scanf(\"%d\", &vet[z]);\n }\n\n for (int f = 0; f < 2 * a; f += 2) {\n int val = 0;\n int v = 1000000000;\n int pos = 0;\n for (int g = 0; g < 2 * b; g += 2) {\n val = abs(vetor[f] - vet[g]) + abs(vetor[f + 1] - vet[g + 1]);\n if (val < v) {\n v = val;\n pos = (g / 2) + 1;\n }\n }\n printf(\"%d\\n\", pos);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n let mut itr = is.split_whitespace().map(|e| e.parse::().unwrap());\n let n = itr.next().unwrap();\n let m = itr.next().unwrap();\n let mut x = vec![];\n let mut y = vec![];\n\n for _ in 0..n {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n let mut itr = is.split_whitespace().map(|e| e.parse::().unwrap());\n let a = itr.next().unwrap();\n let b = itr.next().unwrap();\n x.push((a, b));\n }\n\n for _ in 0..m {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n let mut itr = is.split_whitespace().map(|e| e.parse::().unwrap());\n let a = itr.next().unwrap();\n let b = itr.next().unwrap();\n y.push((a, b));\n }\n\n for (a, b) in x {\n let mut h = 1 << 30;\n let mut ans = 0;\n for (i, &(c, d)) in y.iter().enumerate() {\n let t = (a - c).abs() + (b - d).abs();\n if t < h {\n h = t;\n ans = i;\n }\n }\n\n println!(\"{}\", ans + 1);\n }\n}", "difficulty": "hard"} {"problem_id": "1165", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are given N items.
\nThe value of the i-th item (1 \\leq i \\leq N) is v_i.
\nYour have to select at least A and at most B of these items.
\nUnder this condition, find the maximum possible arithmetic mean of the values of selected items.
\nAdditionally, find the number of ways to select items so that the mean of the values of selected items is maximized.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 50
  • \n
  • 1 \\leq A,B \\leq N
  • \n
  • 1 \\leq v_i \\leq 10^{15}
  • \n
  • Each v_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N A B\nv_1\nv_2\n...\nv_N\n
\n
\n
\n
\n
\n

Output

Print two lines.
\nThe first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most 10^{-6}.
\nThe second line should contain the number of ways to select items so that the mean of the values of selected items is maximized.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2 2\n1 2 3 4 5\n
\n
\n
\n
\n
\n

Sample Output 1

4.500000\n1\n
\n

The mean of the values of selected items will be maximized when selecting the fourth and fifth items. Hence, the first line of the output should contain 4.5.
\nThere is no other way to select items so that the mean of the values will be 4.5, and thus the second line of the output should contain 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 2 3\n10 20 10 10\n
\n
\n
\n
\n
\n

Sample Output 2

15.000000\n3\n
\n

There can be multiple ways to select items so that the mean of the values will be maximized.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 1 5\n1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996\n
\n
\n
\n
\n
\n

Sample Output 3

1000000000000000.000000\n1\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int l;\n int r;\n scanf(\"%i %i %i\", &n, &l, &r);\n\n unsigned long long a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%llu\", &a[i]);\n }\n\n for (int i = 0; i < n; i++) {\n int min_idx = i;\n for (int j = i + 1; j < n; j++) {\n if (a[min_idx] > a[j]) {\n min_idx = j;\n }\n }\n\n unsigned long long tmp = a[i];\n a[i] = a[min_idx];\n a[min_idx] = tmp;\n }\n\n unsigned long long f[51][51];\n for (int i = 0; i <= 50; i++) {\n f[i][0] = f[i][i] = 1;\n for (int j = 1; j < i; j++) {\n f[i][j] = f[i - 1][j] + f[i - 1][j - 1];\n }\n }\n\n unsigned long long p = 0;\n unsigned long long q = 1;\n for (int j = l; j <= r; j++) {\n unsigned long long sum = 0;\n for (int i = n - j; i < n; i++) {\n sum += a[i];\n }\n if (sum * q > p * j) {\n p = sum;\n q = j;\n }\n }\n\n unsigned long long tot = 0;\n for (int j = l; j <= r; j++) {\n unsigned long long sum = 0;\n for (int i = n - j; i < n; i++) {\n sum += a[i];\n }\n\n if (sum * q == p * j) {\n int count = 0;\n for (int i = 0; i < n; i++) {\n if (a[n - j] == a[i]) {\n count++;\n }\n }\n\n int m = 1;\n for (int i = n - j + 1; i < n && a[i] == a[i - 1]; i++) {\n m++;\n }\n\n tot += f[count][m];\n }\n }\n\n printf(\"%.7f\\n%llu\\n\", (double)p / q, tot);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N: usize,\n A: usize,\n B: usize,\n V: [u64; N],\n }\n let mut V = V;\n V.sort();\n V.reverse();\n\n let mut total = 0;\n for i in 0..A {\n total += V[i];\n }\n let ans1 = (total as f64) / (A as f64);\n\n let mut cnt0 = 0;\n let mut cnt1 = 0;\n let mut cnt2 = 0;\n for i in 0..N {\n if V[i] == V[0] {\n cnt0 += 1;\n }\n if V[i] == V[A - 1] {\n cnt1 += 1;\n }\n if V[i] > V[A - 1] {\n cnt2 += 1;\n }\n }\n\n let mut ans2 = 1;\n\n if cnt0 > A {\n ans2 = 0;\n for i in A..(min(cnt0, B) + 1) {\n let mut tmp = 1;\n let c = cnt0;\n let r = i;\n for j in 0..r {\n tmp *= c - j;\n tmp /= j + 1;\n }\n ans2 += tmp;\n }\n } else {\n if cnt1 + cnt2 > A {\n let c = cnt1;\n let r = A - cnt2;\n for i in 0..r {\n ans2 *= c - i;\n ans2 /= i + 1;\n }\n }\n }\n\n println!(\"{}\", ans1);\n println!(\"{}\", ans2);\n}", "difficulty": "hard"} {"problem_id": "1166", "problem_description": "Stanley has decided to buy a new desktop PC made by the company \"Monoblock\", and to solve captcha on their website, he needs to solve the following task.The awesomeness of an array is the minimum number of blocks of consecutive identical numbers in which the array could be split. For example, the awesomeness of an array $$$[1, 1, 1]$$$ is $$$1$$$; $$$[5, 7]$$$ is $$$2$$$, as it could be split into blocks $$$[5]$$$ and $$$[7]$$$; $$$[1, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9]$$$ is 3, as it could be split into blocks $$$[1]$$$, $$$[7, 7, 7, 7, 7, 7, 7]$$$, and $$$[9, 9, 9, 9, 9, 9, 9, 9, 9]$$$. You are given an array $$$a$$$ of length $$$n$$$. There are $$$m$$$ queries of two integers $$$i$$$, $$$x$$$. A query $$$i$$$, $$$x$$$ means that from now on the $$$i$$$-th element of the array $$$a$$$ is equal to $$$x$$$.After each query print the sum of awesomeness values among all subsegments of array $$$a$$$. In other words, after each query you need to calculate $$$$$$\\sum\\limits_{l = 1}^n \\sum\\limits_{r = l}^n g(l, r),$$$$$$ where $$$g(l, r)$$$ is the awesomeness of the array $$$b = [a_l, a_{l + 1}, \\ldots, a_r]$$$.", "c_code": "int solution() {\n long long int n;\n long long int m;\n scanf(\"%lld %lld\", &n, &m);\n long long int a[n];\n long long int total = 0;\n long long int ind = 1;\n long long int b[n];\n long long int t1 = 0;\n long long int t2 = 0;\n\n for (long long int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n for (long long int i = 0; i < n - 1; i++) {\n b[i] = ind;\n\n if (a[i] != a[i + 1]) {\n ind++;\n }\n }\n b[n - 1] = ind;\n\n for (long long int i = 0; i < n; i++) {\n t2 += b[i];\n }\n for (long long int i = 0; i < n; i++) {\n total += t2 - (b[i] - 1) * (n - i) - t1;\n t1 += b[i];\n }\n while (m--) {\n if (n == 1) {\n printf(\"1\\n\");\n continue;\n }\n long long int i;\n long long int x;\n scanf(\"%lld %lld\", &i, &x);\n\n if (a[i - 1] == x) {\n goto end;\n } else if (i == 1) {\n if (a[0] == a[1]) {\n total += n - 1;\n } else if (x == a[1]) {\n total -= n - 1;\n }\n } else if (i == n) {\n if (a[n - 2] == a[n - 1]) {\n total += n - 1;\n } else if (x == a[n - 2]) {\n total -= n - 1;\n }\n } else if (a[i - 1] == a[i - 2] && a[i - 1] == a[i]) {\n total += (n - i) * (2 * i - 1) + i - 1;\n } else if (a[i - 1] == a[i - 2] && a[i - 1] != a[i] && x == a[i]) {\n total += 2 * i - n - 1;\n } else if (a[i - 1] == a[i - 2] && a[i - 1] != a[i] && x != a[i]) {\n total += (i - 1) * (n - i + 1);\n } else if (a[i - 1] != a[i - 2] && a[i - 1] == a[i] && x == a[i - 2]) {\n total += n + 1 - 2 * i;\n } else if (a[i - 1] != a[i - 2] && a[i - 1] == a[i] && x != a[i - 2]) {\n total += (i) * (n - i);\n } else if (a[i] == a[i - 2] && a[i - 1] != a[i] && x == a[i]) {\n total -= (n - i) * (2 * i - 1) + i - 1;\n } else if (a[i - 1] != a[i - 2] && a[i - 1] != a[i] && x == a[i - 2]) {\n total -= (i - 1) * (n - i + 1);\n } else if (a[i - 1] != a[i - 2] && a[i - 1] != a[i] && x == a[i]) {\n total -= (i) * (n - i);\n }\n a[i - 1] = x;\n end:\n printf(\"%lld\\n\", total);\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let stdin_ = io::stdin();\n let mut stdin = stdin_.lock();\n let stdout_ = io::stdout();\n let mut stdout = BufWriter::new(stdout_.lock());\n\n let mut buf = String::new();\n stdin.read_line(&mut buf)?;\n let toks: Vec<&str> = buf.split_whitespace().collect();\n let N: usize = toks[0].parse().unwrap();\n let M: i64 = toks[1].parse().unwrap();\n\n let mut buf = String::new();\n stdin.read_line(&mut buf)?;\n let mut A: Vec = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let mut Adiff: Vec = vec![0; N - 1];\n let mut acc = ((N * (N + 1)) / 2) as i64;\n for i in 1..N {\n Adiff[i - 1] = (A[i] != A[i - 1]) as i64;\n acc += Adiff[i - 1] * ((i * (N - i)) as i64);\n }\n eprintln!(\"starting acc: {acc}\");\n for _ in 0..M {\n let mut buf = String::new();\n stdin.read_line(&mut buf)?;\n let toks: Vec<&str> = buf.split_whitespace().collect();\n let ix: usize = toks[0].parse().unwrap();\n let val: i64 = toks[1].parse().unwrap();\n\n A[ix - 1] = val;\n\n if ix < N {\n let prev = Adiff[ix - 1];\n Adiff[ix - 1] = (A[ix - 1] != A[ix]) as i64;\n acc += (Adiff[ix - 1] - prev) * ((ix * (N - ix)) as i64);\n }\n\n if ix > 1 {\n let prev = Adiff[ix - 1 - 1];\n Adiff[ix - 1 - 1] = (A[ix - 1] != A[ix - 2]) as i64;\n acc += (Adiff[ix - 1 - 1] - prev) * (((ix - 1) * (N - (ix - 1))) as i64);\n }\n\n stdout.write_fmt(format_args!(\"{acc}\\n\"))?;\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1167", "problem_description": "You are given an array consisting of $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$. Initially $$$a_x = 1$$$, all other elements are equal to $$$0$$$.You have to perform $$$m$$$ operations. During the $$$i$$$-th operation, you choose two indices $$$c$$$ and $$$d$$$ such that $$$l_i \\le c, d \\le r_i$$$, and swap $$$a_c$$$ and $$$a_d$$$.Calculate the number of indices $$$k$$$ such that it is possible to choose the operations so that $$$a_k = 1$$$ in the end.", "c_code": "int solution() {\n int t;\n int n;\n int x;\n int m;\n int i;\n int l;\n int r;\n int l_v = -1;\n int r_v = -1;\n int flag = 0;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d %d %d\", &n, &x, &m);\n flag = 0;\n l_v = -1;\n r_v = -1;\n for (i = 1; i <= m; i++) {\n scanf(\"%d %d\", &l, &r);\n if (x >= l && x <= r && flag == 0) {\n l_v = l;\n r_v = r;\n flag = 1;\n }\n if (l < l_v && r >= l_v && flag) {\n l_v = l;\n }\n if (r > r_v && l <= r_v && flag) {\n r_v = r;\n }\n }\n printf(\"%d\\n\", r_v - l_v + 1);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut buffer = String::with_capacity(32);\n\n stdin.read_line(&mut buffer).unwrap();\n let t: i32 = buffer.trim_end().parse().unwrap();\n\n for _ in 0..t {\n buffer.clear();\n stdin.read_line(&mut buffer).unwrap();\n let data = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let x = data[1];\n let m = data[2];\n\n let mut l = x;\n let mut r = x;\n\n for _ in 0..m {\n buffer.clear();\n stdin.read_line(&mut buffer).unwrap();\n let data = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let l_i = data[0];\n let r_i = data[1];\n\n if i32::abs(l + r - l_i - r_i) <= r_i - l_i + r - l {\n l = i32::min(l_i, l);\n r = i32::max(r_i, r);\n }\n }\n\n println!(\"{}\", r - l + 1);\n }\n}", "difficulty": "medium"} {"problem_id": "1168", "problem_description": "Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacher's Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.The tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point $$$(0, 0)$$$. While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to $$$OX$$$ or $$$OY$$$ axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.Alexey wants to make a polyline in such a way that its end is as far as possible from $$$(0, 0)$$$. Please help him to grow the tree this way.Note that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches.", "c_code": "int solution() {\n long long n;\n long long a[10000] = {0};\n long long b;\n long long sum = 0;\n long long d = 0;\n long long e = 0;\n scanf(\"%lld\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &b);\n a[b]++;\n sum += b;\n }\n for (int i = 0;; i++) {\n if (a[i] == n) {\n d = n / 2 * i;\n } else {\n if (e + a[i] <= n / 2) {\n e += a[i];\n if (a[i] > 0) {\n d += i * a[i];\n }\n } else {\n d = d + (n / 2 - e) * i;\n\n break;\n }\n }\n }\n printf(\"%lld\\n\", (d * d) + ((sum - d) * (sum - d)));\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\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n\n let mut sticks: Vec = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n sticks.sort();\n\n let sum1: u64 = sticks[0..(sticks.len() / 2)].iter().sum();\n let sum2: u64 = sticks[(sticks.len() / 2)..].iter().sum();\n\n println!(\"{}\", sum1 * sum1 + sum2 * sum2);\n}", "difficulty": "hard"} {"problem_id": "1169", "problem_description": "Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.", "c_code": "int solution() {\n int n = 0;\n\n scanf(\"%i\", &n);\n int l = 0;\n int cartas[n];\n for (l = 0; l < n; l++) {\n scanf(\"%i\", &cartas[l]);\n }\n\n int sd[2] = {0};\n int inicio = 0;\n int fim = n - 1;\n for (l = 0; l < n; l++) {\n if (cartas[inicio] > cartas[fim]) {\n sd[l % 2] += cartas[inicio];\n inicio++;\n\n } else {\n sd[l % 2] += cartas[fim];\n fim--;\n }\n }\n\n printf(\"%i %i\\n\", sd[0], sd[1]);\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let n = buf.trim().parse::().unwrap();\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n let ns: Vec = buf\n .split(\" \")\n .map(|str| str.trim().parse::().unwrap())\n .collect();\n let (mut i, mut j) = (0, ns.len() - 1);\n let (mut s, mut d) = (0, 0);\n for k in 0..n {\n let picked;\n if ns[i] > ns[j] {\n picked = ns[i];\n i += 1;\n } else {\n picked = ns[j];\n j = j.saturating_sub(1)\n }\n if k % 2 == 0 {\n s += picked;\n } else {\n d += picked;\n }\n }\n println!(\"{} {}\", s, d);\n}", "difficulty": "medium"} {"problem_id": "1170", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have a grid with N rows and M columns of squares. Initially, all the squares are white.

\n

There is a button attached to each row and each column.\nWhen a button attached to a row is pressed, the colors of all the squares in that row are inverted; that is, white squares become black and vice versa.\nWhen a button attached to a column is pressed, the colors of all the squares in that column are inverted.

\n

Takahashi can freely press the buttons any number of times. Determine whether he can have exactly K black squares in the grid.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N,M \\leq 1000
  • \n
  • 0 \\leq K \\leq NM
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M K\n
\n
\n
\n
\n
\n

Output

If Takahashi can have exactly K black squares in the grid, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Press the buttons in the order of the first row, the first column.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2 1\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3 5 8\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n

Press the buttons in the order of the first column, third column, second row, fifth column.

\n
\n
\n
\n
\n
\n

Sample Input 4

7 9 20\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
", "c_code": "int solution(void) {\n\n int n = 0;\n int m = 0;\n int k = 0;\n\n int ans = 0;\n\n scanf(\"%d %d %d\", &n, &m, &k);\n\n for (int a = 0; a <= n && ans == 0; a++) {\n for (int b = 0; b <= m && ans == 0; b++) {\n if ((a * m - 2 * a * b + b * n) == k) {\n ans = 1;\n break;\n }\n }\n }\n if (ans) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut string = String::new();\n io::stdin().read_line(&mut string).expect(\"read error\");\n let int_vec: Vec = string\n .trim()\n .split(\" \")\n .map(|x| x.parse::().expect(\"Not an integer!\"))\n .collect();\n\n let m = min(int_vec[0], int_vec[1]);\n let n = max(int_vec[0], int_vec[1]);\n let k = int_vec[2];\n\n for i in 0..(m + 1) {\n for j in 0..(n + 1) {\n if m * n - (m - i) * (n - j) - i * j == k {\n println!(\"Yes\");\n return;\n }\n }\n }\n println!(\"No\");\n}", "difficulty": "medium"} {"problem_id": "1171", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a string S of length N.\nAmong its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

\n

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100000
  • \n
  • S consists of lowercase English letters.
  • \n
  • |S|=N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\nabcd\n
\n
\n
\n
\n
\n

Sample Output 1

15\n
\n

Since all characters in S itself are different, all its subsequences satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\nbaa\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\nabcab\n
\n
\n
\n
\n
\n

Sample Output 3

17\n
\n
\n
", "c_code": "int solution() {\n int n;\n long mod = 1000000007;\n scanf(\"%d\\n\", &n);\n char a[n];\n int b[26];\n for (int i = 0; i < 26; ++i) {\n b[i] = 0;\n }\n for (int i = 0; i < n; ++i) {\n scanf(\"%c\", &a[i]);\n }\n for (int i = 0; i < n; ++i) {\n b[a[i] - 'a']++;\n }\n long answer = 1;\n for (int i = 0; i < 26; ++i) {\n answer *= (1 + (long)b[i]);\n answer = answer % mod;\n }\n answer--;\n answer = answer % mod;\n printf(\"%ld\\n\", answer);\n return 0;\n}", "rust_code": "fn solution() {\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let _n = st.trim().parse::().unwrap();\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let s: Vec = st.trim().chars().collect();\n let mut map: HashMap<&char, i64> = HashMap::new();\n for i in &s {\n let count = map.entry(i).or_insert(1);\n *count += 1;\n }\n let mo = 1000000007;\n let mut result: i64 = 1;\n for (_i, j) in map.iter() {\n result = (result * j) % mo;\n }\n println!(\"{}\", result - 1);\n}", "difficulty": "medium"} {"problem_id": "1172", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have an integer sequence A, whose length is N.

\n

Find the number of the non-empty contiguous subsequences of A whose sums are 0.\nNote that we are counting the ways to take out subsequences.\nThat is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • -10^9 \\leq A_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Find the number of the non-empty contiguous subsequences of A whose sum is 0.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n1 3 -4 2 2 -2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).

\n
\n
\n
\n
\n
\n

Sample Input 2

7\n1 -1 1 -1 1 -1 1\n
\n
\n
\n
\n
\n

Sample Output 2

12\n
\n

In this case, some subsequences that have the same contents but are taken from different positions are counted individually.\nFor example, three occurrences of (1, -1) are counted.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n1 -2 3 -4 5\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

There are no contiguous subsequences whose sums are 0.

\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n long long A[200001] = {};\n scanf(\"%d\", &N);\n for (i = 1; i <= N; i++) {\n scanf(\"%lld\", &(A[i]));\n }\n\n long long h;\n list *p;\n list data[200001] = {};\n list *hash[100003] = {&(data[0])};\n for (i = 1; i <= N; i++) {\n data[i].value = data[i - 1].value + A[i];\n for (h = llabs(data[i].value) % 100003, p = hash[h]; p != NULL;\n p = p->next) {\n if (data[i].value == p->value) {\n break;\n }\n }\n if (p != NULL) {\n (p->num)++;\n } else {\n data[i].next = hash[h];\n data[i].num = 0;\n hash[h] = &(data[i]);\n }\n }\n\n long long ans = 0;\n for (i = 0; i < 100003; i++) {\n for (p = hash[i]; p != NULL; p = p->next) {\n ans += p->num * (p->num + 1) / 2;\n }\n }\n\n printf(\"%lld\\n\", ans);\n fflush(stdout);\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 let n: usize = iter.next().unwrap().parse().unwrap();\n let mut ary: Vec = vec![0; n];\n for i in 0..n {\n ary[i] = iter.next().unwrap().parse().unwrap();\n }\n let mut csum: Vec = vec![0; n + 1];\n for i in 0..n {\n csum[i + 1] = csum[i] + ary[i];\n }\n\n let mut ret: i64 = 0;\n let mut hmap: HashMap = HashMap::new();\n for c in csum {\n let v = { hmap.get(&c).unwrap_or(&0) + 1 };\n hmap.insert(c, v);\n }\n for (_, v) in hmap {\n if v >= 2 {\n ret += v * (v - 1) / 2;\n }\n }\n println!(\"{}\", ret);\n}", "difficulty": "medium"} {"problem_id": "1173", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are given a tree with N vertices.
\nHere, a tree is a kind of graph, and more specifically, a connected undirected graph with N-1 edges, where N is the number of its vertices.
\nThe i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of c_i.

\n

You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):

\n
    \n
  • find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 3≤N≤10^5
  • \n
  • 1≤a_i,b_i≤N (1≤i≤N-1)
  • \n
  • 1≤c_i≤10^9 (1≤i≤N-1)
  • \n
  • The given graph is a tree.
  • \n
  • 1≤Q≤10^5
  • \n
  • 1≤K≤N
  • \n
  • 1≤x_j,y_j≤N (1≤j≤Q)
  • \n
  • x_j≠y_j (1≤j≤Q)
  • \n
  • x_j≠K,y_j≠K (1≤j≤Q)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N  \na_1 b_1 c_1  \n:  \na_{N-1} b_{N-1} c_{N-1}\nQ K\nx_1 y_1\n:  \nx_{Q} y_{Q}\n
\n
\n
\n
\n
\n

Output

Print the responses to the queries in Q lines.
\nIn the j-th line j(1≤j≤Q), print the response to the j-th query.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1 2 1\n1 3 1\n2 4 1\n3 5 1\n3 1\n2 4\n2 3\n4 5\n
\n
\n
\n
\n
\n

Sample Output 1

3\n2\n4\n
\n

The shortest paths for the three queries are as follows:

\n
    \n
  • Query 1: Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3
  • \n
  • Query 2: Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2
  • \n
  • Query 3: Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

7\n1 2 1\n1 3 3\n1 4 5\n1 5 7\n1 6 9\n1 7 11\n3 2\n1 3\n4 5\n6 7\n
\n
\n
\n
\n
\n

Sample Output 2

5\n14\n22\n
\n

The path for each query must pass Vertex K=2.

\n
\n
\n
\n
\n
\n

Sample Input 3

10\n1 2 1000000000\n2 3 1000000000\n3 4 1000000000\n4 5 1000000000\n5 6 1000000000\n6 7 1000000000\n7 8 1000000000\n8 9 1000000000\n9 10 1000000000\n1 1\n9 10\n
\n
\n
\n
\n
\n

Sample Output 3

17000000000\n
\n
\n
", "c_code": "int solution() {\n long long n;\n long long i;\n long long a;\n long long b;\n long long c;\n long long d[100010];\n long long f[100010] = {};\n int ta[100010];\n int co[200010];\n int nt[200010];\n int to[200010];\n int t;\n int r;\n int m;\n int q[100010];\n scanf(\"%lld\", &n);\n for (i = 0; i < n; i++) {\n ta[i + 1] = -1;\n }\n for (i = 0; i < n - 1; i++) {\n scanf(\"%lld %lld %lld\", &a, &b, &c);\n to[i] = b;\n co[i] = c;\n nt[i] = ta[a];\n ta[a] = i;\n to[i + n - 1] = a;\n co[i + n - 1] = c;\n nt[i + n - 1] = ta[b];\n ta[b] = i + n - 1;\n }\n scanf(\"%lld %d\", &n, &m);\n f[q[0] = m] = 1;\n d[m] = 0;\n for (r = 1; r - t; t++) {\n for (i = ta[q[t]]; i + 1; i = nt[i]) {\n if (f[to[i]]) {\n continue;\n }\n d[to[i]] = d[q[t]] + co[i];\n f[q[r++] = to[i]] = 1;\n }\n }\n for (i = 0; i < n; i++) {\n scanf(\"%lld %lld\", &a, &b);\n printf(\"%lld\\n\", d[a] + d[b]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n es: [(usize1, usize1, usize); n - 1],\n q: usize,\n k: usize1,\n qs: [(usize1, usize1); q]\n }\n let mut t = vec![vec![]; n];\n for &(a, b, c) in &es {\n t[a].push((b, c));\n t[b].push((a, c));\n }\n let mut d = vec![1 << 60; n];\n d[k] = 0;\n let mut q = VecDeque::new();\n q.push_back((k, k));\n while let Some((u, p)) = q.pop_front() {\n for &(v, c) in &t[u] {\n if v == p {\n continue;\n }\n d[v] = d[u] + c;\n q.push_back((v, u));\n }\n }\n for &(x, y) in &qs {\n println!(\"{}\", d[x] + d[y]);\n }\n}", "difficulty": "hard"} {"problem_id": "1174", "problem_description": "There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.", "c_code": "int solution() {\n int k = 0;\n int n = 0;\n scanf(\"%d\", &k);\n for (int i = 1; i <= k; i++) {\n scanf(\"%d\", &n);\n if (n % 2 == 0) {\n for (int j = 2; j <= n; j += 2) {\n printf(\"%d \", j);\n printf(\"%d \", j - 1);\n }\n } else {\n printf(\"3 1 2 \");\n for (int j = 5; j <= n; j += 2) {\n printf(\"%d \", j);\n printf(\"%d \", j - 1);\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 for l in i.lock().lines().skip(1) {\n let n: u32 = l.unwrap().parse().unwrap();\n let mut x = 1;\n let mut r = String::new();\n if !n.is_multiple_of(2) {\n write!(r, \"3 1 2 \").ok();\n x = 4;\n }\n for y in (x..n).step_by(2) {\n write!(r, \"{} {} \", y + 1, y).ok();\n }\n writeln!(o, \"{}\", r.trim()).ok();\n }\n}", "difficulty": "medium"} {"problem_id": "1175", "problem_description": "You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: \"You have to write names in this notebook during $$$n$$$ consecutive days. During the $$$i$$$-th day you have to write exactly $$$a_i$$$ names.\". You got scared (of course you got scared, who wouldn't get scared if he just receive a notebook which is named Death Note with a some strange rule written in it?).Of course, you decided to follow this rule. When you calmed down, you came up with a strategy how you will write names in the notebook. You have calculated that each page of the notebook can contain exactly $$$m$$$ names. You will start writing names from the first page. You will write names on the current page as long as the limit on the number of names on this page is not exceeded. When the current page is over, you turn the page. Note that you always turn the page when it ends, it doesn't matter if it is the last day or not. If after some day the current page still can hold at least one name, during the next day you will continue writing the names from the current page.Now you are interested in the following question: how many times will you turn the page during each day? You are interested in the number of pages you will turn each day from $$$1$$$ to $$$n$$$.", "c_code": "int solution() {\n int n;\n int m;\n int s = 0;\n scanf(\"%d %d\", &n, &m);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n s += a[i];\n printf(\"%d \", s / m);\n s = s % m;\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n\n let stdin = io::stdin();\n\n let mut handle = stdin.lock();\n\n handle.read_to_string(&mut buffer).unwrap();\n\n let s = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n let mut it = s.iter();\n let n = *it.next().unwrap();\n let m = *it.next().unwrap();\n let mut pos = 0;\n for _ in 0..n {\n let num = *it.next().unwrap();\n pos += num;\n let num_pages = pos / m;\n pos %= m;\n println!(\"{}\", num_pages);\n }\n}", "difficulty": "medium"} {"problem_id": "1176", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi loves takoyaki - a ball-shaped snack.

\n

With a takoyaki machine, he can make at most X pieces of takoyaki at a time, taking T minutes regardless of the number of pieces to make.

\n

How long does it take to make N takoyaki?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N,X,T \\leq 1000
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N X T\n
\n
\n
\n
\n
\n

Output

Print an integer representing the minimum number of minutes needed to make N pieces of takoyaki.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

20 12 6\n
\n
\n
\n
\n
\n

Sample Output 1

12\n
\n

He can make 12 pieces of takoyaki in the first 6 minutes and 8 more in the next 6 minutes, so he can make 20 in a total of 12 minutes.

\n

Note that being able to make 12 in 6 minutes does not mean he can make 2 in 1 minute.

\n
\n
\n
\n
\n
\n

Sample Input 2

1000 1 1000\n
\n
\n
\n
\n
\n

Sample Output 2

1000000\n
\n

It seems to take a long time to make this kind of takoyaki.

\n
\n
", "c_code": "int solution(void) {\n int i = 0;\n int a[100] = {0};\n int kara = 0;\n int k = 0;\n int tya = 0;\n for (i = 0; i < 3; i++) {\n scanf(\"%d\", &a[i]);\n }\n if (a[1] >= a[0]) {\n printf(\"%d\", a[2]);\n } else if (a[1] < a[0]) {\n if (a[1] > 1) {\n i = 0;\n while (a[0] >= a[1]) {\n a[0] = a[0] - a[1];\n i++;\n }\n if (a[0] > 0) {\n k = i + 1;\n tya = k * a[2];\n printf(\"%d\", tya);\n } else if (a[0] == 0) {\n tya = i * a[2];\n printf(\"%d\", tya);\n }\n } else if (a[1] == 1) {\n tya = a[0] * a[2];\n printf(\"%d\", tya);\n }\n }\n return 0;\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\n let n: usize = iter.next().unwrap().parse().unwrap();\n let x: usize = iter.next().unwrap().parse().unwrap();\n let t: usize = iter.next().unwrap().parse().unwrap();\n\n println!(\"{}\", n.div_ceil(x) * t);\n}", "difficulty": "hard"} {"problem_id": "1177", "problem_description": "Mihai has an $$$8 \\times 8$$$ chessboard whose rows are numbered from $$$1$$$ to $$$8$$$ from top to bottom and whose columns are numbered from $$$1$$$ to $$$8$$$ from left to right.Mihai has placed exactly one bishop on the chessboard. The bishop is not placed on the edges of the board. (In other words, the row and column of the bishop are between $$$2$$$ and $$$7$$$, inclusive.)The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack. Note that the cell on which the bishop is placed is also considered attacked. An example of a bishop on a chessboard. The squares it attacks are marked in red. Mihai has marked all squares the bishop attacks, but forgot where the bishop was! Help Mihai find the position of the bishop.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n char s[8][8];\n while (t--) {\n scanf(\"\\n\\n\");\n for (int i = 0; i < 8; i++) {\n for (int j = 0; j < 8; j++) {\n scanf(\"%c\", &s[i][j]);\n }\n scanf(\"\\n\");\n }\n int ans = 0;\n for (int i = 1; i < 7; i++) {\n for (int j = 1; j < 7; j++) {\n if (s[i][j] == '#' && s[i - 1][j - 1] == '#' &&\n s[i + 1][j - 1] == '#' && s[i - 1][j + 1] == '#' &&\n s[i + 1][j + 1] == '#') {\n printf(\"%d %d\\n\", i + 1, j + 1);\n ans = 1;\n break;\n }\n }\n if (ans) {\n break;\n }\n }\n scanf(\"\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n\n let mut buffer = String::new();\n\n stdin.read_line(&mut buffer).unwrap();\n let count: u32 = buffer.trim().parse().unwrap();\n\n for _ in 0..count {\n stdin.read_line(&mut buffer).unwrap();\n let mut candidate_x = 0;\n let mut candidate_y = 0;\n\n let mut has_had_2s = false;\n let mut has_had_1 = false;\n\n for y in 1..=8 {\n buffer.clear();\n stdin.read_line(&mut buffer).unwrap();\n\n let mut count_acc = 0;\n let mut acc_pos = 0;\n\n for (x, value) in buffer.trim().bytes().enumerate() {\n let x = x + 1;\n\n if value == b'#' {\n count_acc += 1;\n acc_pos = x;\n }\n }\n if count_acc == 2 {\n has_had_2s = true;\n }\n\n let x = acc_pos;\n if count_acc == 1 && has_had_2s && !has_had_1 {\n candidate_x = x;\n candidate_y = y;\n has_had_1 = true;\n }\n }\n println!(\"{} {}\", candidate_y, candidate_x);\n }\n}", "difficulty": "medium"} {"problem_id": "1178", "problem_description": "Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.", "c_code": "int solution() {\n int n;\n int arr[5001];\n int arr2[5001];\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d %d\", &arr[i], &arr2[i]);\n }\n for (int i = 1; i <= n; i++) {\n for (int j = i + 1; j <= n; j++) {\n if (arr[j] < arr[i]) {\n int x = arr[i];\n arr[i] = arr[j];\n arr[j] = x;\n\n x = arr2[i];\n arr2[i] = arr2[j];\n arr2[j] = x;\n }\n\n if (arr[j] == arr[i]) {\n if (arr2[j] < arr2[i]) {\n int y = arr2[i];\n arr2[i] = arr2[j];\n arr2[j] = y;\n }\n }\n }\n }\n\n int d = arr2[1];\n for (int i = 2; i <= n; i++) {\n if (arr2[i] >= d) {\n d = arr2[i];\n } else {\n d = arr[i];\n }\n }\n printf(\"%d\", d);\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: i16 = n.trim().parse().unwrap();\n\n let mut input = Vec::>::new();\n\n for _ in 0..n {\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n\n let words: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n input.push(words)\n }\n\n input.sort();\n\n let mut c: i32 = 0;\n\n for i in input {\n if i[1] < c {\n c = i[0];\n } else {\n c = i[1];\n }\n }\n\n println!(\"{}\", c)\n}", "difficulty": "hard"} {"problem_id": "1179", "problem_description": "There is a string $$$s$$$ of length $$$3$$$, consisting of uppercase and lowercase English letters. Check if it is equal to \"YES\" (without quotes), where each letter can be in any case. For example, \"yES\", \"Yes\", \"yes\" are all allowable.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char s[4];\n scanf(\"%s\", s);\n if ((s[0] == 'y' || s[0] == 'Y') && (s[1] == 'e' || s[1] == 'E') &&\n (s[2] == 's' || s[2] == 'S')) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n std::io::stdin().lines().skip(1).flatten().for_each(|s| {\n if let &[a, b, c] = s.as_bytes() {\n if [a | b' ', b | b' ', c | b' '] == [b'y', b'e', b's'] {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n };\n });\n}", "difficulty": "easy"} {"problem_id": "1180", "problem_description": "Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.", "c_code": "int solution() {\n long long int a;\n long long int b;\n long long int k;\n long long int r;\n long long int ra;\n long long int rb;\n long long int temp;\n scanf(\"%lli\", &k);\n scanf(\"%lli\", &a);\n scanf(\"%lli\", &b);\n if (k != 0) {\n if (b > 0) {\n if (a == b) {\n if (k > llabs(b)) {\n r = 0;\n } else if ((b + (b % k)) > b) {\n r = 0;\n } else {\n r = 1;\n }\n } else {\n\n if (k <= b) {\n if (b % k != 0) {\n b = b - (b % k);\n rb = (b / k) + 1;\n } else {\n rb = (b / k) + 1;\n }\n if (a > 0)\n\n {\n if (a % k != 0) {\n a = a - (a % k) + k;\n ra = (a / k);\n } else {\n ra = (a / k);\n }\n r = rb - llabs(ra);\n } else {\n if (a % k != 0) {\n a = a - (a % k);\n ra = (a / k);\n } else {\n ra = (a / k);\n }\n r = rb + llabs(ra);\n }\n\n } else {\n if (a <= 0) {\n r = 1;\n } else {\n r = 0;\n }\n }\n }\n } else {\n if (b == 0) {\n if (a == 0) {\n r = 1;\n } else {\n\n temp = llabs(a);\n a = llabs(b);\n b = temp;\n if (b % k != 0) {\n b = b - (b % k);\n rb = (b / k) + 1;\n } else {\n rb = (b / k) + 1;\n }\n r = rb;\n }\n } else {\n if (a == b) {\n if (k > llabs(b)) {\n r = 0;\n } else {\n r = 1;\n }\n } else {\n\n temp = llabs(a);\n a = llabs(b);\n b = temp;\n if (b % k != 0) {\n b = b - (b % k);\n rb = (b / k) + 1;\n } else {\n rb = (b / k) + 1;\n }\n if (a % k != 0) {\n a = a - (a % k) + k;\n ra = (a / k);\n } else {\n ra = (a / k);\n }\n r = llabs(rb) - llabs(ra);\n }\n }\n }\n } else {\n r = 0;\n }\n printf(\"%lli\", r);\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let mut line = line.split_whitespace();\n\n let k: u64 = line.next().unwrap().parse().unwrap();\n let a: i64 = line.next().unwrap().parse().unwrap();\n let b: i64 = line.next().unwrap().parse().unwrap();\n\n let first = a + (k as i64 - a % k as i64) % k as i64;\n let last = b - (k as i64 + b % k as i64) % k as i64;\n\n println!(\"{}\", (last - first) / k as i64 + 1);\n}", "difficulty": "easy"} {"problem_id": "1181", "problem_description": "Luca has a cypher made up of a sequence of $$$n$$$ wheels, each with a digit $$$a_i$$$ written on it. On the $$$i$$$-th wheel, he made $$$b_i$$$ moves. Each move is one of two types: up move (denoted by $$$\\texttt{U}$$$): it increases the $$$i$$$-th digit by $$$1$$$. After applying the up move on $$$9$$$, it becomes $$$0$$$. down move (denoted by $$$\\texttt{D}$$$): it decreases the $$$i$$$-th digit by $$$1$$$. After applying the down move on $$$0$$$, it becomes $$$9$$$. Example for $$$n=4$$$. The current sequence is 0 0 0 0. Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[100];\n int b[100][100];\n b[0][3] = 1;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n\n for (int j = 0; j < a[i]; j++) {\n scanf(\"%d\", &b[i][j]);\n }\n for (int j = 0; j < a[i]; j++) {\n int c[100][100];\n scanf(\"%d\", &c[i][j]);\n for (int k = 0; k < c[i][j]; k++) {\n char d[10];\n scanf(\" %c\", &d[k]);\n if (d[k] == 'U') {\n b[i][j]--;\n if (b[i][j] < 0) {\n b[i][j] += 10;\n }\n } else {\n b[i][j]++;\n if (b[i][j] >= 10) {\n b[i][j] -= 10;\n }\n }\n }\n }\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < a[i]; j++) {\n printf(\"%d \", b[i][j]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let t: i32 = input.trim().parse().unwrap();\n for _ in 0..t {\n input.clear();\n io::stdin().read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n\n input.clear();\n io::stdin().read_line(&mut input).unwrap();\n let mut final_state: Vec = input\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n for i in 0..n {\n input.clear();\n io::stdin().read_line(&mut input).unwrap();\n let input: Vec<&str> = input.split_whitespace().collect();\n let input = String::from(input[1]);\n for j in input.chars() {\n if j == 'D' {\n final_state[i] += 1;\n if final_state[i] == 10 {\n final_state[i] = 0;\n }\n } else {\n final_state[i] -= 1;\n if final_state[i] == -1 {\n final_state[i] = 9\n }\n }\n }\n }\n for i in final_state {\n print!(\"{} \", i);\n }\n println!();\n }\n}", "difficulty": "hard"} {"problem_id": "1182", "problem_description": "Some number of people (this number is even) have stood in a circle. The people stand in the circle evenly. They are numbered clockwise starting from a person with the number $$$1$$$. Each person is looking through the circle's center at the opposite person. A sample of a circle of $$$6$$$ persons. The orange arrows indicate who is looking at whom. You don't know the exact number of people standing in the circle (but this number is even, no doubt). It is known that the person with the number $$$a$$$ is looking at the person with the number $$$b$$$ (and vice versa, of course). What is the number associated with a person being looked at by the person with the number $$$c$$$? If, for the specified $$$a$$$, $$$b$$$, and $$$c$$$, no such circle exists, output -1.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n int a[t];\n int b[t];\n int c[t];\n\n for (int i = 0; i < t; i++) {\n scanf(\"%d%d%d\", &a[i], &b[i], &c[i]);\n }\n\n int ans[t];\n\n for (int i = 0; i < t; i++) {\n int ab = abs(a[i] - b[i]);\n if (2 * ab < c[i] || 2 * ab < a[i] || 2 * ab < b[i]) {\n ans[i] = -1;\n continue;\n }\n\n ans[i] = c[i] > ab ? c[i] - ab : c[i] + ab;\n }\n\n for (int i = 0; i < t; i++) {\n printf(\"%d\\n\", ans[i]);\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(1) {\n if let [a, b, c] = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>()[..]\n {\n let m = (a - b).abs();\n let p = if m < c { c - m } else { m + c };\n writeln!(\n o,\n \"{}\",\n if c > 2 * m || p > 2 * m || a.max(b) > 2 * m {\n -1\n } else {\n p\n }\n )\n .ok();\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1183", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a string S consisting of lowercase English letters. Determine whether all the characters in S are different.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ |S| ≤ 26, where |S| denotes the length of S.
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If all the characters in S are different, print yes (case-sensitive); otherwise, print no.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

uncopyrightable\n
\n
\n
\n
\n
\n

Sample Output 1

yes\n
\n
\n
\n
\n
\n
\n

Sample Input 2

different\n
\n
\n
\n
\n
\n

Sample Output 2

no\n
\n
\n
\n
\n
\n
\n

Sample Input 3

no\n
\n
\n
\n
\n
\n

Sample Output 3

yes\n
\n
\n
", "c_code": "int solution(void) {\n char S[100];\n scanf(\"%s\\n\", S);\n int tbl[26];\n memset(tbl, 0, sizeof(tbl));\n char *p = S;\n while (*p) {\n int index = (*p) - 'a';\n if (tbl[index] > 0) {\n printf(\"no\\n\");\n return 0;\n }\n tbl[index]++;\n p++;\n }\n printf(\"yes\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let s: Vec = buf.trim().chars().collect();\n let mut set = HashSet::new();\n let mut flag = true;\n for i in s {\n if !set.insert(i) {\n flag = false;\n println!(\"no\");\n break;\n }\n }\n if flag {\n println!(\"yes\");\n }\n}", "difficulty": "medium"} {"problem_id": "1184", "problem_description": "A sequence of $$$n$$$ numbers is called permutation if it contains all numbers from $$$1$$$ to $$$n$$$ exactly once. For example, the sequences $$$[3, 1, 4, 2]$$$, [$$$1$$$] and $$$[2,1]$$$ are permutations, but $$$[1,2,1]$$$, $$$[0,1]$$$ and $$$[1,3,4]$$$ are not.For a given number $$$n$$$ you need to make a permutation $$$p$$$ such that two requirements are satisfied at the same time: For each element $$$p_i$$$, at least one of its neighbors has a value that differs from the value of $$$p_i$$$ by one. That is, for each element $$$p_i$$$ ($$$1 \\le i \\le n$$$), at least one of its neighboring elements (standing to the left or right of $$$p_i$$$) must be $$$p_i + 1$$$, or $$$p_i - 1$$$. the permutation must have no fixed points. That is, for every $$$i$$$ ($$$1 \\le i \\le n$$$), $$$p_i \\neq i$$$ must be satisfied. Let's call the permutation that satisfies these requirements funny.For example, let $$$n = 4$$$. Then [$$$4, 3, 1, 2$$$] is a funny permutation, since: to the right of $$$p_1=4$$$ is $$$p_2=p_1-1=4-1=3$$$; to the left of $$$p_2=3$$$ is $$$p_1=p_2+1=3+1=4$$$; to the right of $$$p_3=1$$$ is $$$p_4=p_3+1=1+1=2$$$; to the left of $$$p_4=2$$$ is $$$p_3=p_4-1=2-1=1$$$. for all $$$i$$$ is $$$p_i \\ne i$$$. For a given positive integer $$$n$$$, output any funny permutation of length $$$n$$$, or output -1 if funny permutation of length $$$n$$$ does not exist.", "c_code": "int solution(void) {\n int n = 0;\n int number = 0;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &number);\n if (number == 3) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d %d\", number, number - 1);\n for (int j = 1; j <= number - 2; j++) {\n printf(\" %d\", j);\n }\n printf(\"\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut tests = String::new();\n io::stdin().read_line(&mut tests).unwrap();\n let tests = tests.trim().parse().unwrap();\n\n for _ in 0..tests {\n let mut testline = String::new();\n io::stdin().read_line(&mut testline).unwrap();\n let length = testline.trim().parse().unwrap();\n match length {\n 3 => println!(\"-1\"),\n _ => {\n for i in 3..=length {\n print!(\"{i} \")\n }\n println!(\"2 1\")\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1185", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

The word internationalization is sometimes abbreviated to i18n.\nThis comes from the fact that there are 18 letters between the first i and the last n.

\n

You are given a string s of length at least 3 consisting of lowercase English letters.\nAbbreviate s in the same way.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 ≤ |s| ≤ 100 (|s| denotes the length of s.)
  • \n
  • s consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Print the abbreviation of s.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

internationalization\n
\n
\n
\n
\n
\n

Sample Output 1

i18n\n
\n
\n
\n
\n
\n
\n

Sample Input 2

smiles\n
\n
\n
\n
\n
\n

Sample Output 2

s4s\n
\n
\n
\n
\n
\n
\n

Sample Input 3

xyz\n
\n
\n
\n
\n
\n

Sample Output 3

x1z\n
\n
\n
", "c_code": "int solution(void) {\n char s[101] = {0};\n char n[256] = {0};\n char r[256] = {0};\n int len = 0;\n int sum = 0;\n\n scanf(\"%s\", s);\n\n len = strlen(s);\n\n if (3 <= len && len <= 100) {\n int len_tmp = 0;\n sum = len - 2;\n sprintf(n, \"%d\", sum);\n r[0] = s[0];\n strncpy(&r[1], &n[0], strlen(n));\n len_tmp = strlen(r);\n r[len_tmp] = s[len - 1];\n }\n\n printf(\"%s\\n\", r);\n\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 len = s.trim().len();\n let ss = s.chars().collect::>();\n println!(\"{}{}{}\", ss[0], len - 2, ss[len - 1]);\n}", "difficulty": "hard"} {"problem_id": "1186", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

You are given sequences A and B consisting of non-negative integers.\nThe lengths of both A and B are N, and the sums of the elements in A and B are equal.\nThe i-th element in A is A_i, and the i-th element in B is B_i.

\n

Tozan and Gezan repeats the following sequence of operations:

\n
    \n
  • If A and B are equal sequences, terminate the process.
  • \n
  • Otherwise, first Tozan chooses a positive element in A and decrease it by 1.
  • \n
  • Then, Gezan chooses a positive element in B and decrease it by 1.
  • \n
  • Then, give one candy to Takahashi, their pet.
  • \n
\n

Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible.\nFind the number of candies given to Takahashi when both of them perform the operations optimally.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 × 10^5
  • \n
  • 0 \\leq A_i,B_i \\leq 10^9(1\\leq i\\leq N)
  • \n
  • The sums of the elements in A and B are equal.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 B_1\n:\nA_N B_N\n
\n
\n
\n
\n
\n

Output

Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n1 2\n3 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

When both Tozan and Gezan perform the operations optimally, the process will proceed as follows:

\n
    \n
  • Tozan decreases A_1 by 1.
  • \n
  • Gezan decreases B_1 by 1.
  • \n
  • One candy is given to Takahashi.
  • \n
  • Tozan decreases A_2 by 1.
  • \n
  • Gezan decreases B_1 by 1.
  • \n
  • One candy is given to Takahashi.
  • \n
  • As A and B are equal, the process is terminated.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3\n8 3\n0 1\n4 8\n
\n
\n
\n
\n
\n

Sample Output 2

9\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1\n1 1\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n scanf(\"%d\", &N);\n long long int a[N];\n long long int b[N];\n long long int ans = 0;\n long long int min = 1000000007;\n int zero = 1;\n for (int i = 0; i < N; i++) {\n scanf(\"%lld %lld\", &a[i], &b[i]);\n if (a[i] != b[i]) {\n zero = 0;\n }\n ans += b[i];\n if (a[i] > b[i] && b[i] < min) {\n min = b[i];\n }\n }\n if (zero == 1) {\n printf(\"0\");\n } else {\n printf(\"%lld\", ans - min);\n }\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 (A, B): (Vec, Vec) = {\n let (mut A, mut B) = (vec![], vec![]);\n for _ in 0..N {\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 }\n (A, B)\n };\n\n let s = B.iter().sum::();\n let m = (0..N)\n .filter(|&i| A[i] > B[i])\n .min_by_key(|&i| B[i])\n .map_or(s, |i| B[i]);\n let ans = s - m;\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1187", "problem_description": "There is a grid with $$$n$$$ rows and $$$m$$$ columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers $$$r$$$ and $$$c$$$. Find the minimum number of operations required to make the cell in row $$$r$$$ and column $$$c$$$ black, or determine that it is impossible.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n for (int x = 0; x < t; x++) {\n int n;\n int m;\n int a;\n int b;\n scanf(\"%d\", &n);\n scanf(\"%d\", &m);\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n char arr[n][m];\n\n int z = 0;\n int y = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n scanf(\" %c\", &arr[i][j]);\n if (arr[i][j] == 'B') {\n z = 1;\n }\n if ((a - 1 < n && b - 1 < m) && (i == a - 1 || j == b - 1) &&\n (arr[i][j] == 'B')) {\n y = 1;\n }\n }\n }\n\n if (arr[a - 1][b - 1] == 'B') {\n printf(\"0\\n\");\n continue;\n }\n if (y == 1) {\n printf(\"1\\n\");\n continue;\n }\n if (z == 1) {\n printf(\"2\\n\");\n } else {\n printf(\"-1\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut handle = io::BufWriter::new(io::stdout());\n let mut test_no = String::new();\n io::stdin().read_line(&mut test_no).unwrap();\n let test_no = test_no.trim().parse().unwrap();\n let mut loop_count = 0;\n loop {\n loop_count += 1;\n let mut numberstr = String::new();\n io::stdin().read_line(&mut numberstr).unwrap();\n let numbers = numberstr\n .trim()\n .split(' ')\n .collect::>()\n .iter()\n .map(|x| x.parse().unwrap())\n .collect::>();\n let mut grid: Vec> = Vec::new();\n let mut loop_countt = 0;\n loop {\n loop_countt += 1;\n let mut string = String::new();\n io::stdin().read_line(&mut string).unwrap();\n let array: Vec = string\n .trim()\n .split(' ')\n .collect::()\n .chars()\n .collect::>();\n grid.push(array);\n if loop_countt == numbers[0] {\n break;\n }\n }\n if grid[numbers[2] as usize - 1][numbers[3] as usize - 1] == 'B' {\n writeln!(handle, \"0\").expect(\"\");\n } else if grid[numbers[2] as usize - 1].contains(&'B') {\n writeln!(handle, \"1\").expect(\"\");\n } else {\n let mut num: i8 = 3;\n for i in &grid {\n if i[numbers[3] as usize - 1] == 'B' {\n num = 1;\n break;\n }\n }\n if num != 1 {\n for i in &grid {\n if i.contains(&'B') {\n num = 2;\n break;\n } else {\n num = -1;\n }\n }\n }\n writeln!(handle, \"{}\", num).expect(\"\");\n }\n if loop_count == test_no {\n break;\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1188", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

An integer N is a multiple of 9 if and only if the sum of the digits in the decimal representation of N is a multiple of 9.

\n

Determine whether N is a multiple of 9.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq N < 10^{200000}
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If N is a multiple of 9, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

123456789\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The sum of these digits is 1+2+3+4+5+6+7+8+9=45, which is a multiple of 9, so 123456789 is a multiple of 9.

\n
\n
\n
\n
\n
\n

Sample Input 2

0\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 3

31415926535897932384626433832795028841971693993751058209749445923078164062862089986280\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution() {\n char x[1];\n long long count = 0;\n scanf(\"%c\", &x[0]);\n while (x[0] != 10 && x[0] != ' ') {\n count += x[0] - 48;\n scanf(\"%c\", &x[0]);\n }\n if (count % 9 == 0) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin()\n .read_line(&mut buf)\n .expect(\"failed to read\");\n\n let r: u32 = buf\n .trim()\n .chars()\n .fold(0, |r, c| (c.to_digit(10).unwrap() + r) % 9);\n println!(\"{}\", if r == 0 { \"Yes\" } else { \"No\" })\n}", "difficulty": "easy"} {"problem_id": "1189", "problem_description": "You are given an integer $$$k$$$ and a string $$$s$$$ that consists only of characters 'a' (a lowercase Latin letter) and '*' (an asterisk).Each asterisk should be replaced with several (from $$$0$$$ to $$$k$$$ inclusive) lowercase Latin letters 'b'. Different asterisk can be replaced with different counts of letter 'b'.The result of the replacement is called a BA-string.Two strings $$$a$$$ and $$$b$$$ are different if they either have different lengths or there exists such a position $$$i$$$ that $$$a_i \\neq b_i$$$.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Now consider all different BA-strings and find the $$$x$$$-th lexicographically smallest of them.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int *a = malloc(10000);\n int *b = malloc(10000);\n char *c = malloc(5000);\n char *d = malloc(5000000);\n for (int n1 = 0; n1 < n; n1++) {\n int l;\n long long k;\n long long x;\n scanf(\"%d %lld %lld\", &l, &x, &k);\n k--;\n scanf(\"%s\", c);\n int b0 = 0;\n int b1 = 0;\n for (int l1 = 0; l1 < l; l1++) {\n if (c[l1] == 'a') {\n if (b0 != 0) {\n b[b1] = b0;\n b0 = 0;\n b1++;\n }\n } else {\n b0++;\n }\n }\n if (b0 != 0) {\n b[b1] = b0;\n b0 = 0;\n b1++;\n }\n for (int l1 = 0; l1 < b1; l1++) {\n a[l1] = 0;\n }\n b0 = b1 - 1;\n while (k != 0) {\n a[b0] = k % (x * b[b0] + 1);\n k = k / (x * b[b0] + 1);\n b0--;\n }\n b0 = 0;\n int d1 = 0;\n for (int l1 = 0; l1 < l; l1++) {\n if (c[l1] == 'a') {\n d[d1] = 'a';\n d1++;\n } else {\n for (int l2 = 0; l2 < a[b0]; l2++) {\n d[d1] = 'b';\n d1++;\n }\n b0++;\n int l2;\n for (l2 = l1 + 1; l2 < l; l2++) {\n if (c[l2] == 'a') {\n break;\n }\n }\n l1 = l2 - 1;\n }\n }\n d[d1] = '\\0';\n printf(\"%s\\n\", d);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let k = xs[1];\n let mut x = xs[2] - 1;\n let cs: Vec = lines.next().unwrap().unwrap().chars().collect();\n let mut is_ast = true;\n let mut xs: Vec = vec![];\n let mut ys: Vec = vec![];\n let mut v: usize = 0;\n for c in cs {\n if c == '*' {\n if is_ast {\n v += 1;\n } else {\n xs.push(v);\n is_ast = true;\n v = 1;\n }\n } else {\n if is_ast {\n ys.push(v);\n is_ast = false;\n v = 1;\n } else {\n v += 1;\n }\n }\n }\n if is_ast {\n ys.push(v);\n } else {\n xs.push(v);\n ys.push(0);\n }\n let mut zs: Vec = vec![0; ys.len()];\n for i in (0..ys.len()).rev() {\n if ys[i] == 0 {\n zs[i] = 0;\n } else {\n zs[i] = x % (ys[i] * k + 1);\n x /= ys[i] * k + 1;\n }\n }\n for i in 0..zs.len() {\n if i > 0 {\n print!(\n \"{}\",\n std::iter::repeat_n(\"a\", xs[i - 1]).collect::()\n );\n }\n if zs[i] != 0 {\n print!(\"{}\", std::iter::repeat_n(\"b\", zs[i]).collect::());\n }\n }\n println!();\n }\n}", "difficulty": "hard"} {"problem_id": "1190", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Snuke has N strings. The i-th string is s_i.

\n

Let us concatenate these strings into one string after arranging them in some order.\nFind the maximum possible number of occurrences of AB in the resulting string.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^{4}
  • \n
  • 2 \\leq |s_i| \\leq 10
  • \n
  • s_i consists of uppercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\ns_1\n\\vdots\ns_N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\nABCA\nXBAZ\nBAD\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

\n
\n
\n
\n
\n
\n

Sample Input 2

9\nBEWPVCRWH\nZZNQYIJX\nBAVREA\nPA\nHJMYITEOX\nBCJHMRMNK\nBP\nQVFABZ\nPRGKSPUNA\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n
\n
\n
\n
\n
\n

Sample Input 3

7\nRABYBBE\nJOZ\nBMHQUVA\nBPA\nISU\nMCMABAOBHZ\nSZMEHMA\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n
\n
", "c_code": "int solution() {\n int numOfStrings;\n scanf(\"%d\", &numOfStrings);\n\n int numBxA = 0;\n int numBeginB = 0;\n int numEndA = 0;\n int numAB = 0;\n char str[11] = {0};\n for (int i = 0; i < numOfStrings; i++) {\n scanf(\"%s\", str);\n int len = strlen(str);\n\n if (str[0] == 'B' && str[len - 1] == 'A') {\n numBxA++;\n } else if (str[0] == 'B') {\n numBeginB++;\n } else if (str[len - 1] == 'A') {\n numEndA++;\n }\n\n for (int j = 0; j < len - 1; j++) {\n if (str[j] == 'A' && str[j + 1] == 'B') {\n numAB++;\n }\n }\n }\n if (numBeginB + numEndA != 0) {\n numAB += numBxA + ((numBeginB < numEndA) ? numBeginB : numEndA);\n } else if (numBxA != 0) {\n numAB += numBxA - 1;\n }\n\n printf(\"%d\\n\", numAB);\n return 0;\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 n = it.next().unwrap().parse::().unwrap();\n let s = (0..n).map(|_| it.next().unwrap()).collect::>();\n let mut res = 0;\n let mut cnt_a = 0;\n let mut cnt_b = 0;\n let mut check_ab = false;\n for &c in &s {\n res += c.matches(\"AB\").count();\n let start_b = c.starts_with(\"B\");\n let end_a = c.ends_with(\"A\");\n if start_b && end_a {\n if check_ab {\n res += 1;\n } else {\n check_ab = true;\n }\n } else if start_b {\n cnt_b += 1;\n } else if end_a {\n cnt_a += 1;\n }\n }\n if check_ab {\n if cnt_a > 0 {\n res += 1;\n cnt_a -= 1;\n }\n if cnt_b > 0 {\n res += 1;\n cnt_b -= 1;\n }\n }\n res += std::cmp::min(cnt_a, cnt_b);\n println!(\"{}\", res);\n}", "difficulty": "medium"} {"problem_id": "1191", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a string S. Each character of S is uppercase or lowercase English letter.\nDetermine if S satisfies all of the following conditions:

\n
    \n
  • The initial character of S is an uppercase A.
  • \n
  • There is exactly one occurrence of C between the third character from the beginning and the second to last character (inclusive).
  • \n
  • All letters except the A and C mentioned above are lowercase.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 4 ≤ |S| ≤ 10 (|S| is the length of the string S.)
  • \n
  • Each character of S is uppercase or lowercase English letter.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If S satisfies all of the conditions in the problem statement, print AC; otherwise, print WA.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

AtCoder\n
\n
\n
\n
\n
\n

Sample Output 1

AC\n
\n

The first letter is A, the third letter is C and the remaining letters are all lowercase, so all the conditions are satisfied.

\n
\n
\n
\n
\n
\n

Sample Input 2

ACoder\n
\n
\n
\n
\n
\n

Sample Output 2

WA\n
\n

The second letter should not be C.

\n
\n
\n
\n
\n
\n

Sample Input 3

AcycliC\n
\n
\n
\n
\n
\n

Sample Output 3

WA\n
\n

The last letter should not be C, either.

\n
\n
\n
\n
\n
\n

Sample Input 4

AtCoCo\n
\n
\n
\n
\n
\n

Sample Output 4

WA\n
\n

There should not be two or more occurrences of C.

\n
\n
\n
\n
\n
\n

Sample Input 5

Atcoder\n
\n
\n
\n
\n
\n

Sample Output 5

WA\n
\n

The number of C should not be zero, either.

\n
\n
", "c_code": "int solution() {\n char s[11];\n scanf(\"%s\", s);\n if (s[0] != 'A') {\n printf(\"WA\");\n return 0;\n }\n int len = strlen(s);\n\n int count = 0;\n int i = 1;\n while (s[i]) {\n if (i >= 2 && i <= len - 2) {\n if (s[i] == 'C') {\n count++;\n }\n if (s[i] != 'C' && s[i] >= 'A' && s[i] <= 'Z') {\n printf(\"WA\");\n return 0;\n }\n } else if (s[i] >= 'A' && s[i] <= 'Z') {\n printf(\"WA\");\n return 0;\n }\n i++;\n }\n if (count == 1) {\n printf(\"AC\");\n } else {\n printf(\"WA\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).ok();\n let input = input.trim();\n let len = input.len();\n\n let mut input = input.chars();\n\n let mut i = 0;\n let mut countC = 0;\n let mut countU = 0;\n let mut ans = true;\n\n loop {\n i += 1;\n let buf = input.next();\n if buf.is_none() {\n break;\n }\n let buf = buf.unwrap();\n\n if i == 1 && buf != 'A' {\n ans = false;\n break;\n }\n if i >= 3 && i < len && buf == 'C' {\n countC += 1;\n }\n if buf.is_uppercase() {\n countU += 1;\n }\n }\n if ans && countC == 1 && countU == 2 {\n println!(\"AC\");\n } else {\n println!(\"WA\");\n }\n}", "difficulty": "easy"} {"problem_id": "1192", "problem_description": "Rot13", "c_code": "void solution(char *s) {\n for (int i = 0; s[i]; i++) {\n if (s[i] >= 'A' && s[i] <= 'Z') {\n s[i] = 'A' + ((s[i] - 'A' + 13) % 26);\n } else if (s[i] >= 'a' && s[i] <= 'z') {\n s[i] = 'a' + ((s[i] - 'a' + 13) % 26);\n }\n }\n}\n", "rust_code": "fn solution(text: &str) -> String {\n let to_enc = text.to_uppercase();\n to_enc\n .chars()\n .map(|c| match c {\n 'A'..='M' => ((c as u8) + 13) as char,\n 'N'..='Z' => ((c as u8) - 13) as char,\n _ => c,\n })\n .collect()\n}", "difficulty": "medium"} {"problem_id": "1193", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Given is a positive integer N.

\n

We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.

\n
    \n
  • Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K.
  • \n
\n

In how many choices of K will N become 1 in the end?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^{12}
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the number of choices of K in which N becomes 1 in the end.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

There are three choices of K in which N becomes 1 in the end: 2, 5, and 6.

\n

In each of these choices, N will change as follows:

\n
    \n
  • When K=2: 6 \\to 3 \\to 1
  • \n
  • When K=5: 6 \\to 1
  • \n
  • When K=6: 6 \\to 1
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3141\n
\n
\n
\n
\n
\n

Sample Output 2

13\n
\n
\n
\n
\n
\n
\n

Sample Input 3

314159265358\n
\n
\n
\n
\n
\n

Sample Output 3

9\n
\n
\n
", "c_code": "int solution() {\n long n;\n scanf(\"%ld\", &n);\n if (n == 2) {\n printf(\"1\\n\");\n return 0;\n }\n long ans = 2;\n for (long i = 2; i * i <= n; i++) {\n if (i * i == n - 1) {\n ans++;\n } else if ((n - 1) % i == 0) {\n ans += 2;\n }\n if (n % i != 0) {\n continue;\n }\n long p = n / i;\n long m = n;\n while (m % i == 0) {\n m /= i;\n }\n if (m % i == 1) {\n ans++;\n }\n m = n;\n while (m % p == 0) {\n m /= p;\n }\n if (m % p == 1 && i != p) {\n ans++;\n }\n }\n printf(\"%ld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\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 ans = 0;\n\n {\n let mut k = 1;\n while k * k < n {\n if (n - 1).is_multiple_of(k) {\n ans += if k == (n - 1) / k { 1 } else { 2 };\n }\n k += 1;\n }\n ans -= 1;\n }\n\n {\n let mut k = 2;\n while k * k <= n {\n if n.is_multiple_of(k) {\n let mut x = n;\n while x.is_multiple_of(k) {\n x /= k;\n }\n if x % k == 1 {\n ans += 1;\n }\n let l = n / k;\n if k != l {\n let mut y = n;\n while y.is_multiple_of(l) {\n y /= l;\n }\n if y % l == 1 {\n ans += 1;\n }\n }\n }\n k += 1;\n }\n ans += 1;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1194", "problem_description": "

QQ

\n\n\n

\nWrite a program which prints multiplication tables in the following format:\n

\n\n
\n1x1=1\n1x2=2\n.\n.\n9x8=72\n9x9=81\n
\n\n

Input

\n\n

\nNo input.\n

\n\n

Output

\n\n
\n1x1=1\n1x2=2\n.\n.\n9x8=72\n9x9=81\n
\n\n\n

Template for C

\n\n
\n#include<stdio.h>\n\nint main(){\n\n    return 0;\n}\n
\n\n

Template for C++

\n\n
\n#include<iostream>\nusing namespace std;\n\nint main(){\n\n    return 0;\n}\n
\n\n\n

Template for Java

\n\n
\nclass Main{\n    public static void main(String[] a){\n\n    }\n}\n
", "c_code": "int solution(void) {\n\n int i = 0;\n int j = 0;\n for (i = 1; i < 10; i++) {\n for (j = 1; j < 10; j++) {\n if (i == 9 && j == 10) {\n break;\n }\n printf(\"%dx%d=%d\\n\", i, j, i * j);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut i = 1;\n let mut j = 1;\n let _done = false;\n\n while i != 10 {\n while j != 10 {\n println!(\"{}x{}={}\", i, j, i * j);\n j += 1;\n }\n j = 1;\n i += 1;\n }\n}", "difficulty": "easy"} {"problem_id": "1195", "problem_description": "Odd even sort", "c_code": "void solution(int *arr, int size) {\n bool isSorted = false;\n\n while (!isSorted) {\n isSorted = true;\n\n for (int i = 0; i <= size - 2; i += 2) {\n if (arr[i] > arr[i + 1]) {\n int temp = arr[i];\n arr[i] = arr[i + 1];\n arr[i + 1] = temp;\n\n isSorted = false;\n }\n }\n\n for (int i = 1; i <= size - 2; i += 2) {\n if (arr[i] > arr[i + 1]) {\n int temp = arr[i];\n arr[i] = arr[i + 1];\n arr[i + 1] = temp;\n\n isSorted = false;\n }\n }\n }\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n let len = arr.len();\n if len == 0 {\n return;\n }\n\n let mut sorted = false;\n while !sorted {\n sorted = true;\n\n for i in (1..len - 1).step_by(2) {\n if arr[i] > arr[i + 1] {\n arr.swap(i, i + 1);\n sorted = false;\n }\n }\n\n for i in (0..len - 1).step_by(2) {\n if arr[i] > arr[i + 1] {\n arr.swap(i, i + 1);\n sorted = false;\n }\n }\n }\n}\n", "difficulty": "easy"} {"problem_id": "1196", "problem_description": "In Aramic language words can only represent objects.Words in Aramic have special properties: A word is a root if it does not contain the same letter more than once. A root and all its permutations represent the same object. The root $$$x$$$ of a word $$$y$$$ is the word that contains all letters that appear in $$$y$$$ in a way that each letter appears once. For example, the root of \"aaaa\", \"aa\", \"aaa\" is \"a\", the root of \"aabb\", \"bab\", \"baabb\", \"ab\" is \"ab\". Any word in Aramic represents the same object as its root. You have an ancient script in Aramic. What is the number of different objects mentioned in the script?", "c_code": "int solution() {\n int n;\n int j = 0;\n int g = 0;\n int u = 0;\n char a;\n char x[30];\n char z[1000][30];\n scanf(\"%d\\n\", &n);\n for (int i = 0; i < n; i++) {\n a = getchar(), memset(x, 0, 26);\n while (a != ' ' && a != '\\n') {\n x[a - 97] = 1, a = getchar();\n }\n for (j = 0; j < u; j++) {\n for (g = 0; g < 26; g++) {\n if (x[g] != z[j][g]) {\n break;\n }\n }\n if (g == 26) {\n break;\n }\n }\n if (j == u) {\n memcpy(z[u], x, sizeof(x)), u++;\n }\n }\n printf(\"%d\", u);\n}", "rust_code": "fn solution() {\n use std::io;\n use std::io::prelude::*;\n\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let _n: usize = it.next().unwrap().parse().unwrap();\n\n let f = |s: &str| -> usize {\n let bl = s.as_bytes();\n let mut retval = 0;\n for &b in bl {\n let shift = b - b'a';\n let bit = 1 << shift;\n retval |= bit;\n }\n retval\n };\n\n use std::collections::HashSet;\n let mut mask = HashSet::new();\n let mut ans = 0;\n for s in it {\n let idx = f(s);\n if mask.insert(idx) {\n ans += 1;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1197", "problem_description": "There are $$$n$$$ people participating in some contest, they start participating in $$$x$$$ minutes intervals. That means the first participant starts at time $$$0$$$, the second participant starts at time $$$x$$$, the third — at time $$$2 \\cdot x$$$, and so on.Duration of contest is $$$t$$$ minutes for each participant, so the first participant finishes the contest at time $$$t$$$, the second — at time $$$t + x$$$, and so on. When a participant finishes the contest, their dissatisfaction equals to the number of participants that started the contest (or starting it now), but haven't yet finished it.Determine the sum of dissatisfaction of all participants.", "c_code": "int solution() {\n int setsData;\n scanf(\"%d\", &setsData);\n\n for (int i = 1; i <= setsData; i++) {\n long long numberOfParticipants;\n long long intervalBetweenStudents;\n long long timeOfOlympiad;\n scanf(\"%lld %lld %lld\", &numberOfParticipants, &intervalBetweenStudents,\n &timeOfOlympiad);\n\n long long averageParticipants =\n 1 + (timeOfOlympiad / intervalBetweenStudents);\n if (averageParticipants > numberOfParticipants) {\n averageParticipants = numberOfParticipants;\n }\n long long durationOfOlympiad =\n timeOfOlympiad + (intervalBetweenStudents * (numberOfParticipants - 1));\n long long averageDiscontent =\n (1 + (durationOfOlympiad - timeOfOlympiad -\n (averageParticipants - 1) * intervalBetweenStudents) /\n intervalBetweenStudents) *\n (averageParticipants - 1);\n long long additionalDiscontent =\n (1 + (averageParticipants - 2)) * (averageParticipants - 2) / 2;\n long long fullDiscontent = averageDiscontent + additionalDiscontent;\n\n printf(\"%lld \\n\", fullDiscontent);\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\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let k = it.next().unwrap();\n\n for _ in 0..k {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let n = it.next().unwrap();\n let x = it.next().unwrap();\n let t = it.next().unwrap();\n\n let q = t / x;\n\n let full = if q < n { (n - q) * q } else { 0 };\n\n let m = std::cmp::min(q, n);\n let part = (m * (m - 1)) / 2;\n\n let ans = full + part;\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1198", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Akaki, a patissier, can make N kinds of doughnut using only a certain powder called \"Okashi no Moto\" (literally \"material of pastry\", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.

\n

Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:

\n
    \n
  • For each of the N kinds of doughnuts, make at least one doughnut of that kind.
  • \n
\n

At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ N ≤ 100
  • \n
  • 1 ≤ m_i ≤ 1000
  • \n
  • m_1 + m_2 + ... + m_N ≤ X ≤ 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N X\nm_1\nm_2\n:\nm_N\n
\n
\n
\n
\n
\n

Output

Print the maximum number of doughnuts that can be made under the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1000\n120\n100\n140\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

She has 1000 grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes 120 + 100 + 140 = 360 grams of Moto. From the 640 grams of Moto that remains here, she can make additional six Doughnuts 2. This is how she can made a total of nine doughnuts, which is the maximum.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 360\n90\n90\n90\n90\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n

Making one doughnut for each of the four kinds consumes all of her Moto.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 3000\n150\n130\n150\n130\n110\n
\n
\n
\n
\n
\n

Sample Output 3

26\n
\n
\n
", "c_code": "int solution(void) {\n\n static int n;\n static int x;\n static int min = 100000;\n scanf(\"%d%d\", &n, &x);\n\n for (int i = 0; i < n; i++) {\n\n int m[i];\n scanf(\"%d\", &m[i]);\n\n x -= m[i];\n\n if (min > m[i]) {\n min = m[i];\n }\n }\n\n printf(\"%d\\n\", n + (x / min));\n\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 = iter.next().unwrap().parse::().unwrap();\n let x = iter.next().unwrap().parse::().unwrap();\n let mut t = 0;\n let mut num = 0;\n let mut min = 1001;\n for _ in 0..n {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let a = buf.trim().parse::().unwrap();\n t += a;\n num += 1;\n if min > a {\n min = a;\n }\n }\n let x = x - t;\n println!(\"{}\", x / min + num);\n}", "difficulty": "easy"} {"problem_id": "1199", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Snuke has N sticks.\nThe length of the i-th stick is l_i.

\n

Snuke is making a snake toy by joining K of the sticks together.

\n

The length of the toy is represented by the sum of the individual sticks that compose it.\nFind the maximum possible length of the toy.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq N \\leq 50
  • \n
  • 1 \\leq l_i \\leq 50
  • \n
  • l_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nl_1 l_2 l_3 ... l_{N}\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n1 2 3 4 5\n
\n
\n
\n
\n
\n

Sample Output 1

12\n
\n

You can make a toy of length 12 by joining the sticks of lengths 3, 4 and 5, which is the maximum possible length.

\n
\n
\n
\n
\n
\n

Sample Input 2

15 14\n50 26 27 21 41 7 42 35 7 5 5 36 39 1 45\n
\n
\n
\n
\n
\n

Sample Output 2

386\n
\n
\n
", "c_code": "int solution() {\n int a;\n int b;\n int j = 0;\n int ans = 0;\n scanf(\"%d %d\", &a, &b);\n int l[a];\n for (int i = 0; i < a; i++) {\n scanf(\"%d\", &l[i]);\n }\n for (int i = 0; i < a - 1; j++) {\n if (l[j] < l[j + 1]) {\n int t = l[j + 1];\n l[j + 1] = l[j];\n l[j] = t;\n }\n if (j + 3 > a) {\n j = -1;\n i++;\n }\n }\n for (int i = 0; i < b; i++) {\n ans += l[i];\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut vec: Vec = Vec::new();\n\n let stdin = io::stdin();\n let mut buf = String::new();\n stdin.read_line(&mut buf).ok();\n let mut it = buf.split_whitespace().map(|n| usize::from_str(n).unwrap());\n let (n, k) = (it.next().unwrap(), it.next().unwrap());\n\n let mut buf = String::new();\n stdin.read_line(&mut buf).ok();\n let mut it = buf.split_whitespace().map(|n| usize::from_str(n).unwrap());\n\n for _ in 0..n {\n let n: usize = it.next().unwrap();\n vec.push(n);\n }\n\n let mut sum: usize = 0;\n\n for _ in 0..k {\n let mut m = 0;\n for i in 0..n {\n m = if vec[m] < vec[i] { i } else { m }\n }\n sum += vec[m];\n vec[m] = 0\n }\n println!(\"{}\", sum);\n}", "difficulty": "hard"} {"problem_id": "1200", "problem_description": "Bead sort", "c_code": "void solution(int *a, size_t len) {\n int i, j, max, sum;\n unsigned char *beads;\n\n for (i = 1, max = a[0]; i < len; i++)\n if (a[i] > max)\n max = a[i];\n\n beads = calloc(1, max * len);\n\n for (i = 0; i < len; i++)\n for (j = 0; j < a[i]; j++)\n beads[i * max + j] = 1;\n\n for (j = 0; j < max; j++) {\n\n for (sum = i = 0; i < len; i++) {\n sum += beads[i * max + j];\n beads[i * max + j] = 0;\n }\n\n for (i = len - sum; i < len; i++)\n beads[i * max + j] = 1;\n }\n\n for (i = 0; i < len; i++) {\n for (j = 0; j < max && beads[i * max + j]; j++)\n ;\n a[i] = j;\n }\n free(beads);\n}\n", "rust_code": "fn solution(a: &mut [usize]) {\n let mut max = a[0];\n (1..a.len()).for_each(|i| {\n if a[i] > max {\n max = a[i];\n }\n });\n\n let mut beads = vec![vec![0; max]; a.len()];\n\n for i in 0..a.len() {\n for j in (0..a[i]).rev() {\n beads[i][j] = 1;\n }\n }\n\n for j in 0..max {\n let mut sum = 0;\n (0..a.len()).for_each(|i| {\n sum += beads[i][j];\n beads[i][j] = 0;\n });\n\n for k in ((a.len() - sum)..a.len()).rev() {\n a[k] = j + 1;\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1201", "problem_description": "Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \\dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \\le i \\le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\\dots$$$ $$$+ a_n \\ne 0$$$ and $$$a_1 \\cdot a_2 \\cdot$$$ $$$\\dots$$$ $$$\\cdot a_n \\ne 0$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int t = 0;\n int n = 0;\n int c = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n t = t + a[j];\n if (a[j] == 0) {\n c++;\n }\n }\n int s = c;\n if (c + t == 0) {\n s++;\n printf(\"%d\\n\", s);\n } else {\n printf(\"%d\\n\", s);\n }\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let t: u32 = line.trim().parse().unwrap();\n for _ in 0..t {\n line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let _n: u32 = line.trim().parse().unwrap();\n let mut a = Vec::new();\n line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let pieces = line.split_whitespace();\n for v_str in pieces {\n let v: i32 = v_str.trim().parse().unwrap();\n a.push(v);\n }\n let mut count = a.iter().filter(|&&x| x == 0).count();\n a = a.iter().map(|&x| if x == 0 { 1 } else { x }).collect();\n let sum: i32 = a.iter().sum();\n if sum == 0 {\n a[0] += 1;\n count += 1;\n }\n\n println!(\"{}\", count);\n }\n}", "difficulty": "medium"} {"problem_id": "1202", "problem_description": "$$$n$$$ students are taking an exam. The highest possible score at this exam is $$$m$$$. Let $$$a_{i}$$$ be the score of the $$$i$$$-th student. You have access to the school database which stores the results of all students.You can change each student's score as long as the following conditions are satisfied: All scores are integers $$$0 \\leq a_{i} \\leq m$$$ The average score of the class doesn't change. You are student $$$1$$$ and you would like to maximize your own score.Find the highest possible score you can assign to yourself such that all conditions are satisfied.", "c_code": "int solution() {\n int n;\n scanf(\"%d\\n\", &n);\n for (int i = 1; i <= n; i++) {\n int x1;\n int x2;\n scanf(\"%d %d\\n\", &x1, &x2);\n int arr[x1];\n for (int j = 0; j < x1; j++) {\n if (j != (x1 - 1)) {\n scanf(\"%d \", &arr[j]);\n } else {\n scanf(\"%d\\n\", &arr[j]);\n }\n }\n int sum = 0;\n for (int z = 0; z < x1; z++) {\n sum = sum + arr[z];\n }\n if (x1 == 1) {\n printf(\"%d\\n\", arr[0]);\n } else {\n if (sum >= x2) {\n printf(\"%d\\n\", x2);\n } else {\n printf(\"%d\\n\", sum);\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t: String = String::new();\n std::io::stdin().read_line(&mut t).expect(\"input\");\n let counter: Vec = t\n .split_whitespace()\n .map(|x| x.parse::().expect(\"Not an Integer!\"))\n .collect();\n\n let mut test_case = counter[0];\n while test_case > 0 {\n let mut n_m = String::new();\n std::io::stdin().read_line(&mut n_m).expect(\"input\");\n let nums: Vec = n_m\n .trim()\n .split(' ')\n .flat_map(str::parse::)\n .collect::>();\n\n let mut list_value = String::new();\n std::io::stdin().read_line(&mut list_value).expect(\"input\");\n let mut nums1: Vec = list_value\n .trim()\n .split(' ')\n .flat_map(str::parse::)\n .collect::>();\n nums1.sort();\n\n let mut max: i32 = 0;\n\n if nums1[0] != nums[1] {\n max = nums1[0];\n nums1.remove(0);\n let mut total: i32 = 0;\n for num in nums1 {\n total += num;\n }\n while max != nums[1] && total != 0 {\n max += 1;\n total -= 1;\n }\n println!(\"{}\", max);\n } else {\n println!(\"{}\", nums1[0]);\n }\n\n test_case -= 1;\n }\n}", "difficulty": "hard"} {"problem_id": "1203", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

\n

We will collect all of these balls, by choosing two integers p and q such that p \\neq 0 or q \\neq 0 and then repeating the following operation:

\n
    \n
  • Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.
  • \n
\n

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 50
  • \n
  • |x_i|, |y_i| \\leq 10^9
  • \n
  • If i \\neq j, x_i \\neq x_j or y_i \\neq y_j.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nx_1 y_1\n:\nx_N y_N\n
\n
\n
\n
\n
\n

Output

Print the minimum total cost required to collect all the balls.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n1 1\n2 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 4\n4 6\n7 8\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

\n
\n
\n
\n
\n
\n

Sample Input 3

4\n1 1\n1 2\n2 1\n2 2\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int x[n];\n int y[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d %d\", &x[i], &y[i]);\n }\n int max = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (i != j) {\n int p = x[j] - x[i];\n int q = y[j] - y[i];\n int cnt = 0;\n for (int s = 0; s < n; s++) {\n for (int t = 0; t < n; t++) {\n if ((x[s] + p == x[t]) && (y[s] + q == y[t])) {\n cnt++;\n }\n }\n }\n if (cnt > max) {\n max = cnt;\n }\n }\n }\n }\n printf(\"%d\\n\", n - max);\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 a: Vec<(i64, i64)> = (0..n)\n .map(|_| {\n (\n itr.next().unwrap().parse().unwrap(),\n itr.next().unwrap().parse().unwrap(),\n )\n })\n .collect();\n\n let mut ans = n;\n for i in 0..n {\n for j in 0..n {\n if i == j {\n continue;\n }\n let p = a[j].0 - a[i].0;\n let q = a[j].1 - a[i].1;\n\n let mut cnt = 0;\n for k in 0..n {\n for l in 0..n {\n if k == l {\n continue;\n }\n let r = a[l].0 - a[k].0;\n let s = a[l].1 - a[k].1;\n if p == r && q == s {\n cnt += 1;\n }\n }\n }\n ans = min(ans, n - cnt);\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1204", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called \"it\" tries to catch the other child.) The child who is \"it\" is now at coordinate A, and he can travel the distance of V per second.\nThe other child is now at coordinate B, and she can travel the distance of W per second.

\n

He can catch her when his coordinate is the same as hers.\nDetermine whether he can catch her within T seconds (including exactly T seconds later).\nWe assume that both children move optimally.

\n
\n
\n
\n
\n

Constraints

    \n
  • -10^9 \\leq A,B \\leq 10^9
  • \n
  • 1 \\leq V,W \\leq 10^9
  • \n
  • 1 \\leq T \\leq 10^9
  • \n
  • A \\neq B
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A V\nB W\nT\n
\n
\n
\n
\n
\n

Output

If \"it\" can catch the other child, print YES; otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 2\n3 1\n3\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n
\n
\n
\n
\n
\n

Sample Input 2

1 2\n3 2\n3\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 2\n3 3\n3\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
", "c_code": "int solution(void) {\n long long int a;\n long long int b;\n long long int v;\n long long int w;\n long long int t;\n\n scanf(\"%lld %lld %lld %lld %lld\", &a, &v, &b, &w, &t);\n\n if (a < b) {\n if (a + v * t >= b + w * t) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n } else {\n if (a + v * t * -1 <= b + w * t * -1 && b < a) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n let mut itr = is.split_whitespace().map(|e| e.parse::().unwrap());\n let a = itr.next().unwrap();\n let v = itr.next().unwrap();\n is.clear();\n stdin().read_line(&mut is).ok();\n let mut itr = is.split_whitespace().map(|e| e.parse::().unwrap());\n let b = itr.next().unwrap();\n let w = itr.next().unwrap();\n is.clear();\n stdin().read_line(&mut is).ok();\n let t: i64 = is.trim().parse().unwrap();\n let d = (a - b).abs();\n\n println!(\"{}\", if v * t >= w * t + d { \"YES\" } else { \"NO\" });\n}", "difficulty": "easy"} {"problem_id": "1205", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Snuke has two boards, each divided into a grid with N rows and N columns.\nFor both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j).

\n

There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is S_{i,j}. On the second board, nothing is written yet.

\n

Snuke will write letters on the second board, as follows:

\n
    \n
  • First, choose two integers A and B ( 0 \\leq A, B < N ).
  • \n
  • Write one letter in each square on the second board.\nSpecifically, write the letter written in Square ( i+A, j+B ) on the first board into Square (i,j) on the second board.\nHere, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.
  • \n
\n

After this operation, the second board is called a good board when, for every i and j ( 1 \\leq i, j \\leq N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.

\n

Find the number of the ways to choose integers A and B ( 0 \\leq A, B < N ) such that the second board is a good board.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 300
  • \n
  • S_{i,j} ( 1 \\leq i, j \\leq N ) is a lowercase English letter.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS_{1,1}S_{1,2}..S_{1,N}\nS_{2,1}S_{2,2}..S_{2,N}\n:\nS_{N,1}S_{N,2}..S_{N,N}\n
\n
\n
\n
\n
\n

Output

Print the number of the ways to choose integers A and B ( 0 \\leq A, B < N ) such that the second board is a good board.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\nab\nca\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

For each pair of A and B, the second board will look as shown below:

\n

\"\"

\n

The second board is a good board when (A,B) = (0,1) or (A,B) = (1,0), thus the answer is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\naaaa\naaaa\naaaa\naaaa\n
\n
\n
\n
\n
\n

Sample Output 2

16\n
\n

Every possible choice of A and B makes the second board good.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\nabcde\nfghij\nklmno\npqrst\nuvwxy\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

No possible choice of A and B makes the second board good.

\n
\n
", "c_code": "int solution() {\n int N;\n scanf(\"%d\", &N);\n char S[N][N];\n for (int i = 0; i < N; i++) {\n scanf(\" %s \", S[i]);\n }\n\n int num = 0;\n for (int k = 0; k < N; k++) {\n int symmetry = 1;\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n if (S[(i + k) % N][j] != S[(j + k) % N][i]) {\n symmetry = 0;\n }\n }\n }\n\n if (symmetry) {\n num += N;\n }\n }\n\n printf(\"%d\\n\", num);\n\n return 0;\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 g: Vec> = (0..n)\n .map(|_| itr.next().unwrap().chars().collect())\n .collect();\n\n let mut ans = 0;\n for i in 0..n {\n let mut ok = true;\n for j in 0..n {\n for k in 0..n {\n if g[j][(i + k) % n] != g[k][(i + j) % n] {\n ok = false;\n break;\n }\n }\n if !ok {\n break;\n }\n }\n if ok {\n ans += n;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1206", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are playing the following game with Joisino.

\n
    \n
  • Initially, you have a blank sheet of paper.
  • \n
  • Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.
  • \n
  • Then, you are asked a question: How many numbers are written on the sheet now?
  • \n
\n

The numbers announced by Joisino are given as A_1, ... ,A_N in the order she announces them. How many numbers will be written on the sheet at the end of the game?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤100000
  • \n
  • 1≤A_i≤1000000000(=10^9)
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1\n:\nA_N\n
\n
\n
\n
\n
\n

Output

Print how many numbers will be written on the sheet at the end of the game.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n6\n2\n6\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

The game proceeds as follows:

\n
    \n
  • \n

    6 is not written on the sheet, so write 6.

    \n
  • \n
  • \n

    2 is not written on the sheet, so write 2.

    \n
  • \n
  • \n

    6 is written on the sheet, so erase 6.

    \n
  • \n
\n

Thus, the sheet contains only 2 in the end. The answer is 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n2\n5\n5\n2\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

It is possible that no number is written on the sheet in the end.

\n
\n
\n
\n
\n
\n

Sample Input 3

6\n12\n22\n16\n22\n18\n12\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n
\n
", "c_code": "int solution() {\n int N;\n scanf(\"%d\", &N);\n int ans = N;\n int A[N];\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &A[i]);\n int j = i - 1;\n while (A[i] != A[j] && j >= 0) {\n j--;\n }\n if (j < 0) {\n continue;\n }\n A[j] = 0;\n A[i] = 0;\n ans -= 2;\n }\n\n printf(\"%d\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n for _ in stdin\n .by_ref()\n .bytes()\n .take_while(|r| r.as_ref().unwrap() != &b'\\n')\n {}\n\n let mut set = HashSet::with_capacity(100_000);\n for a in stdin.lines() {\n let a: u32 = a.unwrap().parse().unwrap();\n if !set.remove(&a) {\n set.insert(a);\n }\n }\n\n println!(\"{}\", set.len());\n}", "difficulty": "hard"} {"problem_id": "1207", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers.In one move, you can choose two indices $$$1 \\le i, j \\le n$$$ such that $$$i \\ne j$$$ and set $$$a_i := a_j$$$. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose $$$i$$$ and $$$j$$$ and replace $$$a_i$$$ with $$$a_j$$$).Your task is to say if it is possible to obtain an array with an odd (not divisible by $$$2$$$) sum of elements.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 count = 0;\n int temp = 0;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n count += arr[i];\n if (arr[i] % 2 == 1) {\n temp++;\n }\n }\n if (temp != 0) {\n if (n % 2 == 0 && (arr[0] * n == count || temp == n)) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n\n else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let stdin = io::stdin();\n let mut stdin = stdin.lock().lines();\n let _ = stdin.next();\n for line in stdin\n .map(|e| e.unwrap())\n .collect::>()\n .chunks(2)\n .map(|a| {\n let (cnt, (odd_sum, odd_numbers)) = (\n a[0].parse::().unwrap(),\n a[1].split(' ').map(|e| e.parse::().unwrap()).fold(\n (false, 0),\n |(odd_sum, odd_numbers), elem| {\n (\n odd_sum ^ (elem.rem_euclid(2) == 1),\n odd_numbers + elem.rem_euclid(2),\n )\n },\n ),\n );\n odd_sum || (cnt > odd_numbers && odd_numbers > 0)\n })\n .map(|res| match res {\n true => \"YES\",\n false => \"NO\",\n })\n {\n println!(\"{}\", line);\n }\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1208", "problem_description": "

Sum of 4 Integers

\n\n

\nWrite a program which reads an integer n and identifies the number of combinations of a, b, c and d (0 ≤ a, b, c, d ≤ 9) which meet the following equality:
\n
\na + b + c + d = n
\n
\n\nFor example, for n = 35, we have 4 different combinations of (a, b, c, d): (8, 9, 9, 9), (9, 8, 9, 9), (9, 9, 8, 9), and (9, 9, 9, 8).\n

\n\n

Input

\n\n

\nThe input consists of several datasets. Each dataset consists of n (1 ≤ n ≤ 50) in a line. The number of datasets is less than or equal to 50.\n

\n\n

Output

\n\n

\nPrint the number of combination in a line.\n

\n\n

Sample Input

\n\n
\n35\n1\n
\n\n

Output for the Sample Input

\n\n
\n4\n4\n
", "c_code": "int solution() {\n int No1 = 0;\n int No2 = 0;\n int No3 = 0;\n int No4 = 0;\n int Solns = 0;\n int Sum = 0;\n\n while (scanf(\"%d\", &Sum) != EOF) {\n Solns = 0;\n for (No1 = 0; No1 < 10; No1++) {\n for (No2 = 0; No2 < 10; No2++) {\n for (No3 = 0; No3 < 10; No3++) {\n for (No4 = 0; No4 < 10; No4++) {\n if (No1 + No2 + No3 + No4 == Sum) {\n Solns++;\n }\n }\n }\n }\n }\n printf(\"%d\\n\", Solns);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input;\n loop {\n input = String::new();\n match std::io::stdin().read_line(&mut input) {\n Ok(0) => break,\n Err(_) => break,\n Ok(_) => {}\n };\n let n = input.trim().parse::().expect(\"Error parse\");\n let mut count = 0;\n for i in 0..10 {\n for j in 0..10 {\n for k in 0..10 {\n for l in 0..10 {\n if i + j + k + l == n {\n count += 1;\n }\n }\n }\n }\n }\n println!(\"{}\", count);\n }\n}", "difficulty": "medium"} {"problem_id": "1209", "problem_description": "You have decided to watch the best moments of some movie. There are two buttons on your player: Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie. Skip exactly x minutes of the movie (x is some fixed positive integer). If the player is now at the t-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (t + x). Initially the movie is turned on in the player on the first minute, and you want to watch exactly n best moments of the movie, the i-th best moment starts at the li-th minute and ends at the ri-th minute (more formally, the i-th best moment consists of minutes: li, li + 1, ..., ri). Determine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments?", "c_code": "int solution() {\n int n = 0;\n int x = 0;\n int i = 0;\n int current = 1;\n int min = 0;\n scanf(\"%d %d\", &n, &x);\n int moments[n][2];\n for (i = 0; i < n; i++) {\n scanf(\"%d %d\", &moments[n][0], &moments[n][1]);\n min += moments[n][1] - (((moments[n][0] - current) / x) * x + current) + 1;\n current = moments[n][1] + 1;\n }\n printf(\"%d\", min);\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 = v[0];\n let x = v[1];\n let mut v = vec![];\n for _i in 0..n {\n let tmp: 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 v.push(tmp)\n }\n\n let (_, total) = v.iter().fold((1, 0), |(prev, total), item| {\n let remain = (item[0] - prev) % x;\n (item[1] + 1, item[1] - item[0] + 1 + remain + total)\n });\n println!(\"{:?}\", total);\n}", "difficulty": "hard"} {"problem_id": "1210", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given is an integer x that is greater than or equal to 0, and less than or equal to 1.\nOutput 1 if x is equal to 0, or 0 if x is equal to 1.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq x \\leq 1
  • \n
  • x is an integer
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
x\n
\n
\n
\n
\n
\n

Output

Print 1 if x is equal to 0, or 0 if x is equal to 1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n
\n
\n
\n
\n
\n

Sample Input 2

0\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
", "c_code": "int solution() {\n int x = 0;\n scanf(\"%d\", &x);\n if (x == 1) {\n printf(\"0\");\n } else {\n printf(\"1\");\n }\n}", "rust_code": "fn solution() {\n let mut no = String::new();\n\n io::stdin()\n .read_line(&mut no)\n .expect(\"failed to read line \");\n\n let no: i32 = no.trim().parse().expect(\"error!\");\n\n if no == 0 {\n println!(\"1\");\n } else if no == 1 {\n println!(\"0\");\n } else {\n println!(\"...\");\n }\n}", "difficulty": "easy"} {"problem_id": "1211", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given strings s and t, consisting of lowercase English letters.\nYou will create a string s' by freely rearranging the characters in s.\nYou will also create a string t' by freely rearranging the characters in t.\nDetermine whether it is possible to satisfy s' < t' for the lexicographic order.

\n
\n
\n
\n
\n

Notes

For a string a = a_1 a_2 ... a_N of length N and a string b = b_1 b_2 ... b_M of length M, we say a < b for the lexicographic order if either one of the following two conditions holds true:

\n
    \n
  • N < M and a_1 = b_1, a_2 = b_2, ..., a_N = b_N.
  • \n
  • There exists i (1 \\leq i \\leq N, M) such that a_1 = b_1, a_2 = b_2, ..., a_{i - 1} = b_{i - 1} and a_i < b_i. Here, letters are compared using alphabetical order.
  • \n
\n

For example, xy < xya and atcoder < atlas.

\n
\n
\n
\n
\n

Constraints

    \n
  • The lengths of s and t are between 1 and 100 (inclusive).
  • \n
  • s and t consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s\nt\n
\n
\n
\n
\n
\n

Output

If it is possible to satisfy s' < t', print Yes; if it is not, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

yx\naxy\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

We can, for example, rearrange yx into xy and axy into yxa. Then, xy < yxa.

\n
\n
\n
\n
\n
\n

Sample Input 2

ratcode\natlas\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n

We can, for example, rearrange ratcode into acdeort and atlas into tslaa. Then, acdeort < tslaa.

\n
\n
\n
\n
\n
\n

Sample Input 3

cd\nabc\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n

No matter how we rearrange cd and abc, we cannot achieve our objective.

\n
\n
\n
\n
\n
\n

Sample Input 4

w\nww\n
\n
\n
\n
\n
\n

Sample Output 4

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 5

zzz\nzzz\n
\n
\n
\n
\n
\n

Sample Output 5

No\n
\n
\n
", "c_code": "int solution() {\n int i = 0;\n int j = 0;\n int s_max = 0;\n int t_max = 0;\n int s_baketu[26] = {0};\n int t_baketu[26] = {0};\n char s[200] = {0};\n char t[200] = {0};\n\n scanf(\"%s\", s);\n scanf(\"%s\", t);\n\n for (i = 0; i < 200; i++) {\n if (s[i] != 0) {\n s_baketu[s[i] - 97]++;\n }\n }\n for (i = 0; i < 26; i++) {\n while (s_baketu[i] > 0) {\n s[s_max] = i;\n s_baketu[i]--;\n s_max++;\n }\n }\n\n for (i = 0; i < 200; i++) {\n if (t[i] != 0) {\n t_baketu[t[i] - 97]++;\n }\n }\n for (i = 25; i >= 0; i--) {\n while (t_baketu[i] > 0) {\n t[t_max] = i;\n t_baketu[i]--;\n t_max++;\n }\n }\n\n for (i = 0; i < 200 && j == 0; i++) {\n if (s[i] < t[i]) {\n j = 1;\n }\n if (s[i] > t[i] && j == 0) {\n printf(\"No\");\n return 0;\n }\n }\n if (j == 1) {\n printf(\"Yes\");\n } else if (j == 0) {\n printf(\"No\");\n }\n return 0;\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 let t = get().as_bytes();\n\n let mut ss = vec![0; 256];\n let mut tt = vec![0; 256];\n\n for &c in s.iter() {\n ss[c as usize] += 1;\n }\n for &c in t.iter() {\n tt[c as usize] += 1;\n }\n\n let mut sx = vec![];\n let mut tx = vec![];\n\n for i in 0..256 {\n for _ in 0..ss[i] {\n sx.push(i);\n }\n for _ in 0..tt[255 - i] {\n tx.push(255 - i);\n }\n }\n\n for i in 0..std::cmp::min(sx.len(), tx.len()) {\n if sx[i] < tx[i] {\n println!(\"Yes\");\n return;\n } else if sx[i] > tx[i] {\n println!(\"No\");\n return;\n }\n }\n if sx.len() < tx.len() {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "1212", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given three integers, A, B and C.
\nAmong them, two are the same, but the remaining one is different from the rest.
\nFor example, when A=5,B=7,C=5, A and C are the same, but B is different.
\nFind the one that is different from the rest among the given three integers.

\n
\n
\n
\n
\n

Constraints

    \n
  • -100 \\leq A,B,C \\leq 100
  • \n
  • A, B and C are integers.
  • \n
  • The input satisfies the condition in the statement.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Among A, B and C, print the integer that is different from the rest.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 7 5\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

This is the same case as the one in the statement.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 1 7\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n

In this case, C is the one we seek.

\n
\n
\n
\n
\n
\n

Sample Input 3

-100 100 100\n
\n
\n
\n
\n
\n

Sample Output 3

-100\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) {\n printf(\"%d\", C);\n } else if (B == C) {\n printf(\"%d\", A);\n } else if (A == C) {\n printf(\"%d\", B);\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let vec: Vec<&str> = s.trim().split(' ').collect();\n if vec[0] == vec[1] {\n println!(\"{}\", vec[2]);\n } else if vec[1] == vec[2] {\n println!(\"{}\", vec[0]);\n } else if vec[0] == vec[2] {\n println!(\"{}\", vec[1]);\n }\n}", "difficulty": "easy"} {"problem_id": "1213", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

We have a two-dimensional grid with H \\times W squares. There are M targets to destroy in this grid - the position of the i-th target is \\left(h_i, w_i \\right).

\n

Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.

\n

Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq H, W \\leq 3 \\times 10^5
  • \n
  • 1 \\leq M \\leq \\min\\left(H\\times W, 3 \\times 10^5\\right)
  • \n
  • 1 \\leq h_i \\leq H
  • \n
  • 1 \\leq w_i \\leq W
  • \n
  • \\left(h_i, w_i\\right) \\neq \\left(h_j, w_j\\right) \\left(i \\neq j\\right)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W M\nh_1 w_1\n\\vdots\nh_M w_M\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 3\n2 2\n1 1\n1 3\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

We can destroy all the targets by placing the bomb at \\left(1, 2\\right).

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3 4\n3 3\n3 1\n1 1\n1 2\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5 5 10\n2 5\n4 3\n2 3\n5 5\n2 2\n5 4\n5 3\n5 1\n3 5\n1 4\n
\n
\n
\n
\n
\n

Sample Output 3

6\n
\n
\n
", "c_code": "int solution() {\n int H;\n int W;\n int m;\n int hmax = 0;\n int wmax = 0;\n\n scanf(\"%d %d %d\", &H, &W, &m);\n\n int hbomb[H + 1];\n int h[m + 1];\n int wbomb[W + 1];\n int w[m + 1];\n\n for (int i = 1; i <= H; i++) {\n hbomb[i] = 0;\n }\n\n for (int i = 1; i <= W; i++) {\n wbomb[i] = 0;\n }\n\n for (int i = 1; i <= m; i++) {\n scanf(\"%d %d\", &h[i], &w[i]);\n hbomb[h[i]] += 1;\n wbomb[w[i]] += 1;\n }\n\n for (int i = 1; i <= H; i++) {\n if (hmax < hbomb[i]) {\n hmax = hbomb[i];\n }\n }\n\n for (int i = 1; i <= W; i++) {\n if (wmax < wbomb[i]) {\n wmax = wbomb[i];\n }\n }\n\n int hbox[H + 1];\n int wbox[W + 1];\n int hcount = 0;\n int wcount = 0;\n int count = 0;\n\n for (int i = 1; i <= H; i++) {\n if (hbomb[i] == hmax) {\n hbox[i] = 1;\n hcount += 1;\n } else {\n hbox[i] = 0;\n }\n }\n\n for (int i = 1; i <= W; i++) {\n if (wbomb[i] == wmax) {\n wbox[i] = 1;\n wcount += 1;\n } else {\n wbox[i] = 0;\n }\n }\n\n for (int i = 1; i <= m; i++) {\n if (hbox[h[i]] == 1 && wbox[w[i]] == 1) {\n count += 1;\n }\n }\n\n if (count == hcount * wcount) {\n printf(\"%d\", hmax + wmax - 1);\n return 0;\n }\n printf(\"%d\", hmax + wmax);\n return 0;\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 h = it.next().unwrap().parse::().unwrap();\n let w = it.next().unwrap().parse::().unwrap();\n let m = it.next().unwrap().parse::().unwrap();\n let b = (0..m)\n .map(|_| {\n (\n it.next().unwrap().parse::().unwrap(),\n it.next().unwrap().parse::().unwrap(),\n )\n })\n .collect::>();\n let mut cnt_h = vec![0; h + 1];\n let mut cnt_w = vec![0; w + 1];\n for &p in &b {\n cnt_h[p.0] += 1;\n cnt_w[p.1] += 1;\n }\n let max_h = *cnt_h.iter().max().unwrap();\n let max_w = *cnt_w.iter().max().unwrap();\n let overlap = cnt_h\n .iter()\n .fold(0, |a, &x| a + if x == max_h { 1 } else { 0 })\n * cnt_w\n .iter()\n .fold(0, |a, &x| a + if x == max_w { 1 } else { 0 });\n let cover = b.iter().fold(0, |a, &(x, y)| {\n a + if cnt_h[x] == max_h && cnt_w[y] == max_w {\n 1\n } else {\n 0\n }\n });\n println!(\"{}\", max_h + max_w - if cover == overlap { 1 } else { 0 });\n}", "difficulty": "easy"} {"problem_id": "1214", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Rng is going to a festival.

\n

The name of the festival is given to you as a string S, which ends with FESTIVAL, from input. Answer the question: \"Rng is going to a festival of what?\" Output the answer.

\n

Here, assume that the name of \"a festival of s\" is a string obtained by appending FESTIVAL to the end of s.\nFor example, CODEFESTIVAL is a festival of CODE.

\n
\n
\n
\n
\n

Constraints

    \n
  • 9 \\leq |S| \\leq 50
  • \n
  • S consists of uppercase English letters.
  • \n
  • S ends with FESTIVAL.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the answer to the question: \"Rng is going to a festival of what?\"

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

CODEFESTIVAL\n
\n
\n
\n
\n
\n

Sample Output 1

CODE\n
\n

This is the same as the example in the statement.

\n
\n
\n
\n
\n
\n

Sample Input 2

CODEFESTIVALFESTIVAL\n
\n
\n
\n
\n
\n

Sample Output 2

CODEFESTIVAL\n
\n

This string is obtained by appending FESTIVAL to the end of CODEFESTIVAL, so it is a festival of CODEFESTIVAL.

\n
\n
\n
\n
\n
\n

Sample Input 3

YAKINIKUFESTIVAL\n
\n
\n
\n
\n
\n

Sample Output 3

YAKINIKU\n
\n
\n
", "c_code": "int solution() {\n char s[99];\n scanf(\"%s\", s);\n int n = strlen(s);\n s[n - 8] = 0;\n puts(s);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n println!(\"{}\", s.chars().take(s.len() - 9).collect::());\n}", "difficulty": "easy"} {"problem_id": "1215", "problem_description": "\n
\n
\n

Problem Statement

You are given two integers a and b (a≤b). Determine if the product of the integers a, a+1, , b is positive, negative or zero.

\n
\n
\n
\n
\n

Constraints

    \n
  • a and b are integers.
  • \n
  • -10^9≤a≤b≤10^9
  • \n
\n
\n
\n
\n
\n

Partial Score

    \n
  • In test cases worth 100 points, -10≤a≤b≤10.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
a b\n
\n
\n
\n
\n
\n

Output

If the product is positive, print Positive. If it is negative, print Negative. If it is zero, print Zero.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 3\n
\n
\n
\n
\n
\n

Sample Output 1

Positive\n
\n

1×2×3=6 is positive.

\n
\n
\n
\n
\n
\n

Sample Input 2

-3 -1\n
\n
\n
\n
\n
\n

Sample Output 2

Negative\n
\n

(-3)×(-2)×(-1)=-6 is negative.

\n
\n
\n
\n
\n
\n

Sample Input 3

-1 1\n
\n
\n
\n
\n
\n

Sample Output 3

Zero\n
\n

(-1)×0×1=0.

\n
\n
", "c_code": "int solution(void) {\n\n long long int a;\n long long int b;\n\n scanf(\"%lld\", &a);\n scanf(\"%lld\", &b);\n\n if (a <= 0 && b >= 0) {\n\n printf(\"Zero\\n\");\n return 0;\n }\n if (a < 0 && b < 0) {\n\n if ((b - a) % 2 == 0) {\n printf(\"Negative\\n\");\n return 0;\n }\n printf(\"Positive\\n\");\n return 0;\n\n } else if (a > 0 && b > 0) {\n\n printf(\"Positive\\n\");\n return 0;\n }\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 vec2 = vec[0].split(' ').collect::>();\n let a: i64 = vec2[0].trim().parse().unwrap();\n let b: i64 = vec2[1].trim().parse().unwrap();\n\n if a * b <= 0 {\n println!(\"Zero\");\n } else if a > 0 {\n println!(\"Positive\");\n } else {\n if (b - a + 1) % 2 == 0 {\n println!(\"Positive\");\n } else {\n println!(\"Negative\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1216", "problem_description": "You are given $$$n$$$ numbers $$$a_1, a_2, \\dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \\cdot a_2$$$ $$$\\dots$$$ $$$\\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\\cdot (-1) \\cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?", "c_code": "int solution() {\n int n = 1;\n scanf(\"%d\", &n);\n\n int positive = 0;\n int negative = 0;\n int zero = 0;\n\n long long int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &arr[i]);\n long long int number = arr[i];\n\n if (number > 0) {\n positive++;\n } else if (number < 0) {\n negative++;\n } else {\n zero++;\n }\n }\n\n unsigned long long int answer = 0;\n\n for (int i = 0; i < n; i++) {\n long long int number = arr[i];\n\n if (number > 0) {\n answer += number - 1;\n } else if (number < 0) {\n answer += -number - 1;\n } else {\n answer += 1;\n }\n }\n\n if (negative % 2 == 0 || zero > 0) {\n ;\n } else {\n (answer += 2);\n }\n\n printf(\"%lld\\n\", answer);\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 n = input.trim().parse::().unwrap();\n let mut neg = 0;\n let mut zeros = 0;\n let mut sum = 0;\n\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let mut input_iter = input.split_whitespace();\n for _ in 0..n {\n let num = input_iter.next().unwrap().parse::().unwrap();\n if num <= -1 {\n sum += -1 - num;\n neg += 1;\n } else if num == 0 {\n zeros += 1;\n } else {\n sum += num - 1;\n }\n }\n sum += zeros;\n if neg % 2 == 1 && zeros == 0 {\n sum += 2;\n }\n println!(\"{}\", sum);\n}", "difficulty": "easy"} {"problem_id": "1217", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

\n
\n
\n
\n
\n

Constraints

    \n
  • S and T are strings consisting of lowercase English letters.
  • \n
  • The lengths of S and T are between 1 and 100 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S T\n
\n
\n
\n
\n
\n

Output

Print the resulting string.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

oder atc\n
\n
\n
\n
\n
\n

Sample Output 1

atcoder\n
\n

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

\n
\n
\n
\n
\n
\n

Sample Input 2

humu humu\n
\n
\n
\n
\n
\n

Sample Output 2

humuhumu\n
\n
\n
", "c_code": "int solution(void) {\n char s[101];\n char t[101];\n char buf[203];\n\n fgets(buf, sizeof(buf), stdin);\n sscanf(buf, \"%s %s\", s, t);\n printf(\"%s%s\\n\", t, s);\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 let mut iter = buf.split_whitespace();\n let s = iter.next().unwrap().to_owned();\n let t = iter.next().unwrap().to_owned();\n println!(\"{}\", t + &s);\n}", "difficulty": "medium"} {"problem_id": "1218", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There are N towns on a line running east-west.\nThe towns are numbered 1 through N, in order from west to east.\nEach point on the line has a one-dimensional coordinate, and a point that is farther east has a greater coordinate value.\nThe coordinate of town i is X_i.

\n

You are now at town 1, and you want to visit all the other towns.\nYou have two ways to travel:

\n
    \n
  • \n

    Walk on the line.\nYour fatigue level increases by A each time you travel a distance of 1, regardless of direction.

    \n
  • \n
  • \n

    Teleport to any location of your choice.\nYour fatigue level increases by B, regardless of the distance covered.

    \n
  • \n
\n

Find the minimum possible total increase of your fatigue level when you visit all the towns in these two ways.

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 2≤N≤10^5
  • \n
  • 1≤X_i≤10^9
  • \n
  • For all i(1≤i≤N-1), X_i<X_{i+1}.
  • \n
  • 1≤A≤10^9
  • \n
  • 1≤B≤10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N A B\nX_1 X_2 ... X_N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible total increase of your fatigue level when you visit all the towns.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 2 5\n1 2 5 7\n
\n
\n
\n
\n
\n

Sample Output 1

11\n
\n

From town 1, walk a distance of 1 to town 2, then teleport to town 3, then walk a distance of 2 to town 4.\nThe total increase of your fatigue level in this case is 2×1+5+2×2=11, which is the minimum possible value.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 1 100\n40 43 45 105 108 115 124\n
\n
\n
\n
\n
\n

Sample Output 2

84\n
\n

From town 1, walk all the way to town 7.\nThe total increase of your fatigue level in this case is 84, which is the minimum possible value.

\n
\n
\n
\n
\n
\n

Sample Input 3

7 1 2\n24 35 40 68 72 99 103\n
\n
\n
\n
\n
\n

Sample Output 3

12\n
\n

Visit all the towns in any order by teleporting six times.\nThe total increase of your fatigue level in this case is 12, which is the minimum possible value.

\n
\n
", "c_code": "int solution(void) {\n\n long n;\n long a;\n long b;\n scanf(\"%ld %ld %ld\", &n, &a, &b);\n long x[n];\n for (long i = 0; i < n; i++) {\n scanf(\"%ld\", &x[i]);\n }\n long min = 0;\n for (long i = 1; i < n; i++) {\n if ((x[i] - x[i - 1]) * a <= b) {\n min += (x[i] - x[i - 1]) * a;\n } else {\n min += b;\n }\n }\n printf(\"%ld\\n\", min);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize, a: usize, b: usize,\n x: [usize; n],\n }\n let mut tired = 0;\n for i in 1..n {\n if (x[i] - x[i - 1]) * a <= b {\n tired += (x[i] - x[i - 1]) * a\n } else {\n tired += b;\n }\n }\n println!(\"{}\", tired);\n}", "difficulty": "easy"} {"problem_id": "1219", "problem_description": "Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length $$$k$$$ have the same sum. A subarray of an array is any sequence of consecutive elements.Phoenix currently has an array $$$a$$$ of length $$$n$$$. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between $$$1$$$ and $$$n$$$ inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n int A[n];\n int uniq[100] = {0};\n int len = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &A[i]);\n if (uniq[A[i] - 1] == 0) {\n len++;\n uniq[A[i] - 1] = 1;\n }\n }\n if (k < len) {\n printf(\"-1\\n\");\n continue;\n }\n for (int i = 0; len < k; i++) {\n if (uniq[i] == 0) {\n uniq[i] = 1;\n len++;\n }\n }\n printf(\"%d\\n\", len * n);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 100; j++) {\n if (uniq[j]) {\n printf(\"%d \", j + 1);\n }\n }\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, k) = (xs[0], xs[1]);\n let ys: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let mut s: HashSet = ys.iter().cloned().collect();\n if s.len() > k {\n println!(\"-1\");\n continue;\n }\n let mut zs: Vec = Vec::new();\n let mut i: usize = 0;\n while i < n {\n if zs.len() < k {\n if k - zs.len() <= s.len() && !s.contains(&ys[i]) {\n let z = s.iter().cloned().next().unwrap();\n zs.push(z);\n s.remove(&z);\n } else {\n zs.push(ys[i]);\n s.remove(&ys[i]);\n i += 1;\n }\n } else {\n if zs[zs.len() - k] == ys[i] {\n zs.push(ys[i]);\n i += 1;\n } else {\n zs.push(zs[zs.len() - k]);\n }\n }\n }\n println!(\"{}\", zs.len());\n for z in zs {\n print!(\"{} \", z);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1220", "problem_description": "Let $$$s$$$ be a string of lowercase Latin letters. Its price is the sum of the indices of letters (an integer between 1 and 26) that are included in it. For example, the price of the string abca is $$$1+2+3+1=7$$$.The string $$$w$$$ and the integer $$$p$$$ are given. Remove the minimal number of letters from $$$w$$$ so that its price becomes less than or equal to $$$p$$$ and print the resulting string. Note that the resulting string may be empty. You can delete arbitrary letters, they do not have to go in a row. If the price of a given string $$$w$$$ is less than or equal to $$$p$$$, then nothing needs to be deleted and $$$w$$$ must be output.Note that when you delete a letter from $$$w$$$, the order of the remaining letters is preserved. For example, if you delete the letter e from the string test, you get tst.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int q = 0; q < t; q++) {\n int v[27] = {0};\n char w[200001];\n char *c = w;\n scanf(\"%s\", w);\n while (*c) {\n v[*c - '`']++;\n v[0] = v[0] + *c - '`';\n c++;\n }\n int p;\n scanf(\"%d\", &p);\n p++;\n char i = 26;\n int num;\n while (v[0] >= p) {\n if (v[0] - i * v[i] >= p) {\n v[0] = v[0] - i * v[i];\n i--;\n } else {\n num = (v[0] - p) / i + 1;\n v[0] = 0;\n }\n }\n c = w;\n i += '`';\n while (*c) {\n if (*c == i) {\n if (!num) {\n printf(\"%c\", *c);\n } else {\n num--;\n }\n } else {\n if (*c < i) {\n printf(\"%c\", *c);\n }\n }\n c++;\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (stdin, stdout) = (io::stdin(), io::stdout());\n let mut scan = Scanner::new(stdin.lock());\n let mut out = io::BufWriter::new(stdout.lock());\n let t: usize = scan.next();\n for _ in 0..t {\n let s: String = scan.next();\n let v: Vec = s.chars().collect();\n let p: i32 = scan.next();\n let mut vv = Vec::new();\n let n = v.len();\n let mut w = 0;\n for i in 0..n {\n vv.push((v[i], i));\n w += v[i] as i32 - 'a' as i32 + 1;\n }\n vv.sort();\n vv.reverse();\n let mut used = Vec::new();\n used.resize(n, false);\n if w > p {\n for i in 0..n {\n w -= vv[i].0 as i32 - 'a' as i32 + 1;\n used[vv[i].1] = true;\n if w <= p {\n break;\n }\n }\n }\n for i in 0..n {\n if !used[i] {\n write!(out, \"{}\", v[i]);\n }\n }\n writeln!(out);\n }\n}", "difficulty": "hard"} {"problem_id": "1221", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Three people, A, B and C, are trying to communicate using transceivers.\nThey are standing along a number line, and the coordinates of A, B and C are a, b and c (in meters), respectively.\nTwo people can directly communicate when the distance between them is at most d meters.\nDetermine if A and C can communicate, either directly or indirectly.\nHere, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 a,b,c 100
  • \n
  • 1 d 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b c d\n
\n
\n
\n
\n
\n

Output

If A and C can communicate, print Yes; if they cannot, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 7 9 3\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

A and B can directly communicate, and also B and C can directly communicate, so we should print Yes.

\n
\n
\n
\n
\n
\n

Sample Input 2

100 10 1 2\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

They cannot communicate in this case.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 10 10 1\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n

There can be multiple people at the same position.

\n
\n
\n
\n
\n
\n

Sample Input 4

1 100 2 10\n
\n
\n
\n
\n
\n

Sample Output 4

Yes\n
\n
\n
", "c_code": "int solution(void) {\n\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int dca = 0;\n int dba = 0;\n int dcb = 0;\n\n scanf(\"%d %d %d %d\", &a, &b, &c, &d);\n\n if (c - a >= 0) {\n dca = c - a;\n } else {\n dca = a - c;\n }\n if (b - a >= 0) {\n dba = b - a;\n } else {\n dba = a - b;\n }\n if (c - b >= 0) {\n dcb = c - b;\n } else {\n dcb = b - c;\n }\n\n if (dca <= d) {\n printf(\"Yes\\n\");\n return 0;\n }\n if (dba <= d && dcb <= d) {\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::new();\n io::stdin().read_line(&mut s).ok();\n let s = s.split_whitespace();\n let mut i: Vec = Vec::new();\n for z in s {\n i.push(z.parse().unwrap());\n }\n\n let a = i[0];\n let b = i[1];\n let c = i[2];\n let d = i[3];\n\n let mut ans = false;\n if (c - a).abs() <= d {\n ans = true;\n }\n if (a - b).abs() <= d && (b - c).abs() <= d {\n ans = true;\n }\n\n if ans {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "1222", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

AtCoder Inc. holds a contest every Saturday.

\n

There are two types of contests called ABC and ARC, and just one of them is held at a time.

\n

The company holds these two types of contests alternately: an ARC follows an ABC and vice versa.

\n

Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.

\n
\n
\n
\n
\n

Constraints

    \n
  • S is ABC or ARC.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the string representing the type of the contest held this week.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

ABC\n
\n
\n
\n
\n
\n

Sample Output 1

ARC\n
\n

They held an ABC last week, so they will hold an ARC this week.

\n
\n
", "c_code": "int solution() {\n char a[3];\n scanf(\"%s\", a);\n if (a[0] == 'A' && a[1] == 'B' && a[2] == 'C') {\n printf(\"ARC\");\n } else if (a[0] == 'A' && a[1] == 'R' && a[2] == 'C') {\n printf(\"ABC\");\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 if s.trim() == \"ABC\" {\n println!(\"ARC\");\n } else {\n println!(\"ABC\")\n }\n}", "difficulty": "medium"} {"problem_id": "1223", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi likes the sound when he buys a drink from a vending machine.

\n

That sound can be heard by spending A yen (the currency of Japan) each time.

\n

Takahashi has B yen. He will hear the sound as many times as he can with that money, but at most C times, as he would be satisfied at that time.

\n

How many times will he hear the sound?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A, B, C \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print the number of times Takahashi will hear his favorite sound.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 11 4\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

Since he has not less than 8 yen, he will hear the sound four times and be satisfied.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 9 5\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

He may not be able to be satisfied.

\n
\n
\n
\n
\n
\n

Sample Input 3

100 1 10\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n\n int a = 0;\n int b = 0;\n int c = 0;\n int son = 0;\n\n scanf(\"%d %d %d\", &a, &b, &c);\n\n while (b >= a) {\n b = b - a;\n son++;\n if (son == c) {\n break;\n }\n }\n printf(\"%d\\n\", son);\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 let a: usize = iter.next().unwrap().parse().unwrap();\n let b: usize = iter.next().unwrap().parse().unwrap();\n let c: usize = iter.next().unwrap().parse().unwrap();\n\n println!(\"{}\", min(b / a, c));\n}", "difficulty": "hard"} {"problem_id": "1224", "problem_description": "Vasya claims that he had a paper square. He cut it into two rectangular parts using one vertical or horizontal cut. Then Vasya informed you the dimensions of these two rectangular parts. You need to check whether Vasya originally had a square. In other words, check if it is possible to make a square using two given rectangles.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[10000][4];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 4; j++) {\n scanf(\"%d\", &a[i][j]);\n }\n if ((a[i][0] == a[i][2] && a[i][1] + a[i][3] == a[i][0]) ||\n (a[i][0] == a[i][3] && a[i][1] + a[i][2] == a[i][0]) ||\n (a[i][1] == a[i][2] && a[i][0] + a[i][3] == a[i][1]) ||\n (a[i][1] == a[i][3] && a[i][0] + a[i][2] == a[i][1])) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input)?;\n\n let n = input.trim().parse().unwrap();\n for _ in 0..n {\n input.clear();\n io::stdin().read_line(&mut input)?;\n let a: Vec = input\n .trim()\n .split(\" \")\n .map(|x| x.parse().unwrap())\n .collect();\n\n input.clear();\n io::stdin().read_line(&mut input)?;\n let b: Vec = input\n .trim()\n .split(\" \")\n .map(|x| x.parse().unwrap())\n .collect();\n\n if (a[0] == b[0]) && (a[0] == a[1] + b[1])\n || (a[0] == b[1]) && (a[0] == a[1] + b[0])\n || (a[1] == b[0]) && (a[1] == a[0] + b[1])\n || (a[1] == b[1]) && (a[1] == a[0] + b[0])\n {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1225", "problem_description": "When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the i-th book.Valera decided to choose an arbitrary book with number i and read the books one by one, starting from this book. In other words, he will first read book number i, then book number i + 1, then book number i + 2 and so on. He continues the process until he either runs out of the free time or finishes reading the n-th book. Valera reads each book up to the end, that is, he doesn't start reading the book if he doesn't have enough free time to finish reading it. Print the maximum number of books Valera can read.", "c_code": "int solution() {\n long n;\n long t;\n scanf(\"%ld %ld\", &n, &t);\n long a[n];\n long i = 0;\n long j = 0;\n long books = 0;\n long mints = 0;\n for (i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n mints += a[i];\n if (mints <= t) {\n books++;\n } else if (mints > t) {\n mints -= a[j];\n j++;\n }\n }\n printf(\"%ld\", books);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut iterator = stdin.lock().lines();\n\n let line: Vec = iterator\n .next()\n .unwrap()\n .unwrap()\n .split(\" \")\n .map(|x| x.parse().expect(\"Not an integer!\"))\n .collect();\n\n let books: Vec = iterator\n .next()\n .unwrap()\n .unwrap()\n .split(\" \")\n .map(|x| x.parse().expect(\"Not an integer!\"))\n .collect();\n\n let mut s = 0;\n let mut x = 0;\n let mut ans = 0;\n for i in 0..line[0] {\n s += books[i];\n while s > line[1] {\n s -= books[x];\n x += 1;\n }\n if i - x + 1 > ans {\n ans = i - x + 1;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1226", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

E869120 found a chest which is likely to contain treasure.
\nHowever, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.
\nHe also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

\n

One more thing he found is a sheet of paper with the following facts written on it:

\n
    \n
  • Condition 1: The string S contains a string T as a contiguous substring.
  • \n
  • Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.
  • \n
\n

Print the string S.
\nIf such a string does not exist, print UNRESTORABLE.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S'|, |T| \\leq 50
  • \n
  • S' consists of lowercase English letters and ?.
  • \n
  • T consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nT'\n
\n
\n
\n
\n
\n

Output

Print the string S.
\nIf such a string does not exist, print UNRESTORABLE instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

?tc????\ncoder\n
\n
\n
\n
\n
\n

Sample Output 1

atcoder\n
\n

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.\nAmong them, the lexicographically smallest is atcoder, so we can say S = atcoder.

\n
\n
\n
\n
\n
\n

Sample Input 2

??p??d??\nabc\n
\n
\n
\n
\n
\n

Sample Output 2

UNRESTORABLE\n
\n

There is no string that satisfies Condition 1, so the string S does not exist.

\n
\n
", "c_code": "int solution(void) {\n\n char s[51];\n char t[51];\n scanf(\"%s %s\", s, t);\n int s_length = strlen(s);\n int t_length = strlen(t);\n int flag;\n for (int i = s_length - 1; i >= t_length - 1; i--) {\n flag = 1;\n for (int j = 0; j < t_length; j++) {\n if (s[i - j] != t[t_length - 1 - j] && s[i - j] != '?') {\n flag = 0;\n break;\n }\n }\n if (flag == 1) {\n for (int j = 0; j < s_length; j++) {\n if (j >= i - t_length + 1 && j <= i) {\n s[j] = t[j - (i - t_length + 1)];\n } else {\n if (s[j] == '?') {\n s[j] = 'a';\n }\n }\n }\n printf(\"%s\\n\", s);\n return 0;\n }\n }\n printf(\"UNRESTORABLE\\n\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n let mut t = String::new();\n std::io::stdin().read_line(&mut s).ok();\n std::io::stdin().read_line(&mut t).ok();\n let a = s.trim_end().as_bytes();\n let b = t.trim_end().as_bytes();\n for i in (0..a.len()).rev() {\n if i + b.len() > a.len() {\n continue;\n }\n let mut valid = true;\n for j in 0..b.len() {\n if a[i + j] != b'?' && a[i + j] != b[j] {\n valid = false;\n break;\n }\n }\n if valid {\n for j in 0..i {\n print!(\"{}\", if a[j] == b'?' { \"a\" } else { &s[j..j + 1] })\n }\n print!(\"{}\", t.trim_end());\n for j in i + b.len()..a.len() {\n print!(\"{}\", if a[j] == b'?' { \"a\" } else { &s[j..j + 1] })\n }\n println!();\n return;\n }\n }\n println!(\"UNRESTORABLE\");\n}", "difficulty": "easy"} {"problem_id": "1227", "problem_description": "Let's denote a $$$k$$$-step ladder as the following structure: exactly $$$k + 2$$$ wooden planks, of which two planks of length at least $$$k+1$$$ — the base of the ladder; $$$k$$$ planks of length at least $$$1$$$ — the steps of the ladder; Note that neither the base planks, nor the steps planks are required to be equal.For example, ladders $$$1$$$ and $$$3$$$ are correct $$$2$$$-step ladders and ladder $$$2$$$ is a correct $$$1$$$-step ladder. On the first picture the lengths of planks are $$$[3, 3]$$$ for the base and $$$[1]$$$ for the step. On the second picture lengths are $$$[3, 3]$$$ for the base and $$$[2]$$$ for the step. On the third picture lengths are $$$[3, 4]$$$ for the base and $$$[2, 3]$$$ for the steps. You have $$$n$$$ planks. The length of the $$$i$$$-th planks is $$$a_i$$$. You don't have a saw, so you can't cut the planks you have. Though you have a hammer and nails, so you can assemble the improvised \"ladder\" from the planks.The question is: what is the maximum number $$$k$$$ such that you can choose some subset of the given planks and assemble a $$$k$$$-step ladder using them?", "c_code": "int solution(void) {\n int T = 0;\n scanf(\"%d\", &T);\n\n while (T-- > 0) {\n long first = 0;\n long second = 0;\n int n = 0;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n long a = 0;\n scanf(\"%ld\", &a);\n if (a > first) {\n second = first;\n first = a;\n } else if (a > second) {\n second = a;\n }\n }\n\n long k = second - 1 < n - 2 ? second - 1 : n - 2;\n\n printf(\"%ld\\n\", k);\n }\n}", "rust_code": "fn solution() {\n let mut s: String = String::new();\n let t: i32 = {\n io::stdin().read_line(&mut s).ok();\n s.trim().parse().unwrap()\n };\n s.clear();\n for _t in 0..t {\n let n: i32 = {\n io::stdin().read_line(&mut s).ok();\n s.trim().parse().unwrap()\n };\n s.clear();\n let a: Vec = {\n io::stdin().read_line(&mut s).ok();\n s.split_whitespace().map(|s| s.parse().unwrap()).collect()\n };\n s.clear();\n let (mut max1, mut max2) = (cmp::max(a[0], a[1]), cmp::min(a[0], a[1]));\n for &el in &a[2..] {\n if el > max1 {\n max2 = max1;\n max1 = el;\n } else if el > max2 {\n max2 = el;\n }\n }\n\n println!(\"{}\", cmp::min(max2 - 1, n - 2));\n }\n}", "difficulty": "medium"} {"problem_id": "1228", "problem_description": "There are $$$n$$$ piles of stones, where the $$$i$$$-th pile has $$$a_i$$$ stones. Two people play a game, where they take alternating turns removing stones.In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player who cannot make a move (because all piles are empty) loses the game. If both players play optimally, determine the winner of the game.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int k = 0; k < t; ++k) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int check1 = 0;\n int not1 = 0;\n int flag = 0;\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &a[i]);\n if (a[i] == 1) {\n ++check1;\n if (flag == 0) {\n ++not1;\n }\n } else {\n flag = 1;\n }\n }\n if (check1 == n && n % 2 == 0) {\n printf(\"Second\\n\");\n } else if (check1 == n && n % 2 == 1) {\n printf(\"First\\n\");\n } else {\n if (not1 % 2 == 0) {\n printf(\"First\\n\");\n } else {\n printf(\"Second\\n\");\n }\n }\n }\n return 0;\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 input.read_line(&mut line).unwrap();\n line.clear();\n let mut first_wins = false;\n input.read_line(&mut line).unwrap();\n for heap in line.split_whitespace() {\n first_wins = !first_wins;\n if heap != \"1\" {\n break;\n }\n }\n if first_wins {\n println!(\"First\");\n } else {\n println!(\"Second\");\n }\n count -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "1229", "problem_description": "There is a famous olympiad, which has more than a hundred participants. The Olympiad consists of two stages: the elimination stage, and the final stage. At least a hundred participants will advance to the final stage. The elimination stage in turn consists of two contests.A result of the elimination stage is the total score in two contests, but, unfortunately, the jury lost the final standings and has only standings for the first and for the second contest separately. In each contest, the participants are ranked by their point score in non-increasing order. When two participants have a tie (earned the same score), they are ranked by their passport number (in accordance with local regulations, all passport numbers are distinct). In the first contest, the participant on the 100-th place scored $$$a$$$ points. Also, the jury checked all participants from the 1-st to the 100-th place (inclusive) in the first contest and found out that all of them have at least $$$b$$$ points in the second contest.Similarly, for the second contest, the participant on the 100-th place has $$$c$$$ points. And the jury checked that all the participants from the 1-st to the 100-th place (inclusive) have at least $$$d$$$ points in the first contest.After two contests, all participants are ranked by their total score in two contests in non-increasing order. When participants have the same total score, tie-breaking with passport numbers is used. The cutoff score to qualify to the final stage is the total score of the participant on the 100-th place.Given integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$, please help the jury determine the smallest possible value of the cutoff score.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n\n while (t--) {\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n scanf(\"%d %d %d %d\", &a, &b, &c, &d);\n\n printf(\"%d\\n\", (a + b) > (c + d) ? (a + b) : (c + d));\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut reader = BufReader::new(stdin());\n let mut get_line = || {\n let mut buf = String::new();\n reader.read_line(&mut buf).unwrap();\n buf\n };\n let mut writer = BufWriter::new(stdout());\n\n let t: usize = get_line().trim().parse().unwrap();\n for _ in 0..t {\n let a: Vec = get_line()\n .split_whitespace()\n .map(|a| a.parse().unwrap())\n .collect();\n writeln!(writer, \"{}\", std::cmp::max(a[0] + a[1], a[2] + a[3])).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1230", "problem_description": "Bash wants to become a Pokemon master one day. Although he liked a lot of Pokemon, he has always been fascinated by Bulbasaur the most. Soon, things started getting serious and his fascination turned into an obsession. Since he is too young to go out and catch Bulbasaur, he came up with his own way of catching a Bulbasaur.Each day, he takes the front page of the newspaper. He cuts out the letters one at a time, from anywhere on the front page of the newspaper to form the word \"Bulbasaur\" (without quotes) and sticks it on his wall. Bash is very particular about case — the first letter of \"Bulbasaur\" must be upper case and the rest must be lower case. By doing this he thinks he has caught one Bulbasaur. He then repeats this step on the left over part of the newspaper. He keeps doing this until it is not possible to form the word \"Bulbasaur\" from the newspaper.Given the text on the front page of the newspaper, can you tell how many Bulbasaurs he will catch today?Note: uppercase and lowercase letters are considered different.", "c_code": "int solution() {\n char str[1000001];\n scanf(\"%s\", str);\n int i;\n int len = strlen(str);\n int count = 0;\n int B = 0;\n int u = 0;\n int b = 0;\n int a = 0;\n int s = 0;\n int r = 0;\n int l = 0;\n\n for (i = 0; i < len; i++) {\n if (str[i] == 'B') {\n B++;\n } else if (str[i] == 'b') {\n b++;\n } else if (str[i] == 'u') {\n u++;\n } else if (str[i] == 'l') {\n l++;\n } else if (str[i] == 'a') {\n a++;\n } else if (str[i] == 's') {\n s++;\n } else if (str[i] == 'r') {\n r++;\n }\n }\n u /= 2;\n a /= 2;\n while (B > 0 && b > 0 && u > 0 && l > 0 && a > 0 && s > 0 && r > 0) {\n B--;\n b--;\n u--;\n l--;\n a--;\n s--;\n r--;\n count++;\n }\n printf(\"%d\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).expect(\"wrong format\");\n\n let mut counter: [i32; 256] = [0; 256];\n\n for c in buffer.as_bytes() {\n let i: usize = (*c).into();\n counter[i] += 1;\n }\n\n let mut max = 1000000;\n for c in \"Blbsr\".as_bytes() {\n let i: usize = (*c).into();\n max = cmp::min(max, counter[i]);\n }\n\n for c in \"au\".as_bytes() {\n let i: usize = (*c).into();\n max = cmp::min(max, counter[i] / 2);\n }\n println!(\"{}\", max);\n}", "difficulty": "hard"} {"problem_id": "1231", "problem_description": "Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 ≤ i ≤ n) he makes ai money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.Help Kefa cope with this task!", "c_code": "int solution() {\n long long n;\n scanf(\"%lli\", &n);\n long long x[n];\n long long z = 1;\n long long temp = 1;\n for (int i = 0; i < n; i++) {\n scanf(\"%lli\", &x[i]);\n if (i == 0) {\n continue;\n }\n if (i > 0 && x[i] >= x[i - 1] && i != n - 1) {\n z++;\n } else if (i > 0 && x[i] < x[i - 1]) {\n if (z > temp) {\n temp = z;\n z = 1;\n } else {\n z = 1;\n }\n } else if (i > 0 && x[i] >= x[i - 1] && i == n - 1) {\n z++;\n if (z > temp) {\n temp = z;\n z = 1;\n } else {\n z = 1;\n }\n }\n }\n\n printf(\"%lli\", temp);\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input).expect(\"read_line error\");\n\n input.clear();\n io::stdin().read_line(&mut input).expect(\"read_line error\");\n\n let n: Vec = input\n .trim()\n .split_ascii_whitespace()\n .map(|num| num.parse::().expect(\"Parsing error\"))\n .collect();\n\n let mut best = 0;\n let mut ant = 0;\n let mut count = 0;\n\n for i in n {\n if i >= ant {\n count += 1;\n if count > best {\n best = count;\n }\n } else {\n count = 1;\n if count > best {\n best = count;\n }\n }\n ant = i;\n }\n\n println!(\"{}\", best);\n}", "difficulty": "medium"} {"problem_id": "1232", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

Takahashi the Jumbo will practice golf.

\n

His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive).

\n

If he can achieve the objective, print OK; if he cannot, print NG.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A \\leq B \\leq 1000
  • \n
  • 1 \\leq K \\leq 1000
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
K\nA B\n
\n
\n
\n
\n
\n

Output

\n

If he can achieve the objective, print OK; if he cannot, print NG.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7\n500 600\n
\n
\n
\n
\n
\n

Sample Output 1

OK\n
\n

Among the multiples of 7, for example, 567 lies between 500 and 600.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n5 7\n
\n
\n
\n
\n
\n

Sample Output 2

NG\n
\n

No multiple of 4 lies between 5 and 7.

\n
\n
\n
\n
\n
\n

Sample Input 3

1\n11 11\n
\n
\n
\n
\n
\n

Sample Output 3

OK\n
\n
\n
", "c_code": "int solution() {\n int k = 0;\n int a = 0;\n int b = 0;\n int i = 0;\n\n scanf(\"%d\", &k);\n scanf(\"%d %d\", &a, &b);\n\n i = 1;\n while (k <= a) {\n if (a <= k * i) {\n break;\n }\n i++;\n }\n\n if (k * i <= b) {\n printf(\"OK\\n\");\n } else {\n printf(\"NG\\n\");\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 buf.pop();\n let K = i64::from_str(&buf).unwrap();\n buf.clear();\n stdin.read_line(&mut buf).ok();\n let mut it = buf.split_whitespace().map(|n| i64::from_str(n).unwrap());\n let (A, B) = (it.next().unwrap(), it.next().unwrap());\n for i in A..(B + 1) {\n if i % K == 0 {\n println!(\"OK\");\n return;\n }\n }\n println!(\"NG\");\n}", "difficulty": "easy"} {"problem_id": "1233", "problem_description": "Ehab loves number theory, but for some reason he hates the number $$$x$$$. Given an array $$$a$$$, find the length of its longest subarray such that the sum of its elements isn't divisible by $$$x$$$, or determine that such subarray doesn't exist.An array $$$a$$$ is a subarray of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.", "c_code": "int solution() {\n int t;\n int size;\n int x;\n scanf(\"%d\", &t);\n\n for (int i = 0; i < t; i++) {\n\n scanf(\"%d %d\", &size, &x);\n int arr[size];\n int flag = 0;\n int sum = 0;\n for (int j = 0; j < size; j++) {\n scanf(\"%d\", &arr[j]);\n if ((arr[j] % x) != 0) {\n flag = 1;\n }\n sum = sum + arr[j];\n }\n\n if (flag == 0) {\n printf(\"-1\\n\");\n } else if (flag == 1 && (sum % x) != 0) {\n printf(\"%d\\n\", size);\n } else {\n for (int k = 0; k < size; k++) {\n if (((arr[k] % x) != 0) || ((arr[size - k - 1] % x) != 0)) {\n printf(\"%d\\n\", size - k - 1);\n break;\n }\n }\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, x) = (xs[0], xs[1] as u64);\n let ys: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let ok = ys.iter().any(|y| y % x != 0);\n if !ok {\n println!(\"-1\");\n continue;\n }\n let sum: u64 = ys.iter().sum();\n if !sum.is_multiple_of(x) {\n println!(\"{}\", n);\n continue;\n }\n let mut m: u64 = sum % x;\n let mut left: usize = 0;\n while m == 0 && left < n {\n m = (m + (x - ys[left] % x)) % x;\n left += 1;\n }\n m = sum % x;\n let mut right: usize = 0;\n while m == 0 && right < n {\n m = (m + (x - ys[n - right - 1] % x)) % x;\n right += 1;\n }\n println!(\"{}\", n - min(left, right));\n }\n}", "difficulty": "medium"} {"problem_id": "1234", "problem_description": "During the quarantine, Sicromoft has more free time to create the new functions in \"Celex-2021\". The developers made a new function GAZ-GIZ, which infinitely fills an infinite table to the right and down from the upper left corner as follows: The cell with coordinates $$$(x, y)$$$ is at the intersection of $$$x$$$-th row and $$$y$$$-th column. Upper left cell $$$(1,1)$$$ contains an integer $$$1$$$.The developers of the SUM function don't sleep either. Because of the boredom, they teamed up with the developers of the RAND function, so they added the ability to calculate the sum on an arbitrary path from one cell to another, moving down or right. Formally, from the cell $$$(x,y)$$$ in one step you can move to the cell $$$(x+1, y)$$$ or $$$(x, y+1)$$$. After another Dinwows update, Levian started to study \"Celex-2021\" (because he wants to be an accountant!). After filling in the table with the GAZ-GIZ function, he asked you to calculate the quantity of possible different amounts on the path from a given cell $$$(x_1, y_1)$$$ to another given cell $$$(x_2, y_2$$$), if you can only move one cell down or right.Formally, consider all the paths from the cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$ such that each next cell in the path is located either to the down or to the right of the previous one. Calculate the number of different sums of elements for all such paths.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n int x1;\n int y1;\n int x2;\n int y2;\n scanf(\"%d %d %d %d\", &x1, &y1, &x2, &y2);\n int x = x2 - x1;\n long long y = y2 - y1;\n long long ans = x * y;\n\n printf(\"%lld \\n\", ++ans);\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 a: u64 = itr.next().unwrap().parse().unwrap();\n let b: u64 = itr.next().unwrap().parse().unwrap();\n let c: u64 = itr.next().unwrap().parse().unwrap();\n let d: u64 = itr.next().unwrap().parse().unwrap();\n writeln!(out, \"{}\", (c - a) * (d - b) + 1).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "medium"} {"problem_id": "1235", "problem_description": "Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a \"+=\" operation that adds the right-hand side value to the left-hand side variable. For example, performing \"a += b\" when a = $$$2$$$, b = $$$3$$$ changes the value of a to $$$5$$$ (the value of b does not change).In a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations \"a += b\" or \"b += a\". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value $$$n$$$. What is the smallest number of operations he has to perform?", "c_code": "int solution() {\n int t;\n\n scanf(\"%d\", &t);\n\n int vet[t][3];\n int count[t];\n\n for (int i = 0; i < t; ++i) {\n scanf(\"%d %d %d\", &vet[i][0], &vet[i][1], &vet[i][2]);\n count[i] = 0;\n }\n for (int i = 0; i < t; ++i) {\n while (vet[i][0] <= vet[i][2] && vet[i][1] <= vet[i][2]) {\n if (vet[i][0] <= vet[i][1]) {\n vet[i][0] += vet[i][1];\n ++count[i];\n }\n if (vet[i][0] > vet[i][2]) {\n break;\n }\n if (vet[i][0] >= vet[i][1]) {\n vet[i][1] += vet[i][0];\n ++count[i];\n }\n if (vet[i][1] > vet[i][2]) {\n break;\n }\n }\n }\n for (int i = 0; i < t; ++i) {\n printf(\"%d\\n\", count[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"\");\n let s: u32 = s.trim().parse().expect(\"\");\n for _ in 0..s {\n let mut st = String::new();\n io::stdin().read_line(&mut st).expect(\"\");\n let st: Vec<&str> = st.split(\" \").collect();\n let mut a: u32 = st[0].trim().parse().expect(\"\");\n let mut b: u32 = st[1].trim().parse().expect(\"\");\n let n: u32 = st[2].trim().parse().expect(\"\");\n let mut cnt = 0;\n while (a <= n) && (b <= n) {\n if a > b {\n b += a;\n } else {\n a += b;\n }\n cnt += 1;\n }\n println!(\"{}\", cnt);\n }\n}", "difficulty": "hard"} {"problem_id": "1236", "problem_description": "After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.", "c_code": "int solution() {\n int a;\n int b;\n double c;\n double d;\n double e;\n double f;\n double g;\n double h;\n scanf(\"%d%d%lf%lf%lf%lf\", &a, &b, &c, &d, &e, &f);\n if (a < 20) {\n h = (20 - a) * 60 + (0 - b);\n } else {\n h = 0;\n }\n h = c + h * d;\n h = h / f;\n if (h != (int)h) {\n h = ((int)h + 1);\n }\n h = e * h;\n h = h - h * 20 / 100;\n g = c;\n g = g / f;\n if (g != (int)g) {\n g = (int)g + 1;\n }\n g = e * g;\n if (g <= h) {\n printf(\"%.4lf\", g);\n } else {\n printf(\"%.4f\", h);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::{self, prelude::*};\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let hh: f64 = it.next().unwrap().parse().unwrap();\n let mm: f64 = it.next().unwrap().parse().unwrap();\n let h: f64 = it.next().unwrap().parse().unwrap();\n let d: f64 = it.next().unwrap().parse().unwrap();\n let c: f64 = it.next().unwrap().parse().unwrap();\n let n: f64 = it.next().unwrap().parse().unwrap();\n\n if hh >= 20.0 {\n let c = c * 0.8;\n let cnt = (h / n).ceil();\n let ans = cnt * c;\n println!(\"{}\", ans);\n return;\n }\n\n let a1 = {\n let cnt = (h / n).ceil();\n cnt * c\n };\n let a2 = {\n let tio = 60.0 * (20.0 - hh) - mm;\n let c = 0.8 * c;\n let cnt = ((h + tio * d) / n).ceil();\n c * cnt\n };\n let ans = if a1 < a2 { a1 } else { a2 };\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1237", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given an integer sequence of length n, a_1, ..., a_n.\nLet us consider performing the following n operations on an empty sequence b.

\n

The i-th operation is as follows:

\n
    \n
  1. Append a_i to the end of b.
  2. \n
  3. Reverse the order of the elements in b.
  4. \n
\n

Find the sequence b obtained after these n operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq n \\leq 2\\times 10^5
  • \n
  • 0 \\leq a_i \\leq 10^9
  • \n
  • n and a_i are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n\na_1 a_2 ... a_n\n
\n
\n
\n
\n
\n

Output

Print n integers in a line with spaces in between.\nThe i-th integer should be b_i.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 1

4 2 1 3\n
\n
    \n
  • After step 1 of the first operation, b becomes: 1.
  • \n
  • After step 2 of the first operation, b becomes: 1.
  • \n
  • After step 1 of the second operation, b becomes: 1, 2.
  • \n
  • After step 2 of the second operation, b becomes: 2, 1.
  • \n
  • After step 1 of the third operation, b becomes: 2, 1, 3.
  • \n
  • After step 2 of the third operation, b becomes: 3, 1, 2.
  • \n
  • After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.
  • \n
  • After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.
  • \n
\n

Thus, the answer is 4 2 1 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 2

3 1 2\n
\n

As shown above in Sample Output 1, b becomes 3, 1, 2 after step 2 of the third operation. Thus, the answer is 3 1 2.

\n
\n
\n
\n
\n
\n

Sample Input 3

1\n1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

1000000000\n
\n
\n
\n
\n
\n
\n

Sample Input 4

6\n0 6 7 6 7 0\n
\n
\n
\n
\n
\n

Sample Output 4

0 6 6 0 7 7\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int a[200001];\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n if (n & 1) {\n printf(\"%d\", a[n]);\n for (int i = n - 2; i >= 1; i -= 2) {\n printf(\" %d\", a[i]);\n }\n for (int i = 2; i <= n - 1; i += 2) {\n printf(\" %d\", a[i]);\n }\n } else {\n printf(\"%d\", a[n]);\n for (int i = n - 2; i >= 2; i -= 2) {\n printf(\" %d\", a[i]);\n }\n for (int i = 1; i <= n - 1; i += 2) {\n printf(\" %d\", a[i]);\n }\n }\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 a: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let ans = (0..n)\n .map(|i| {\n if i < n.div_ceil(2) {\n a[n - 2 * i - 1]\n } else {\n a[2 * (i - n.div_ceil(2)) + n % 2]\n }\n })\n .map(|x| x.to_string())\n .collect::>()\n .join(\" \");\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1238", "problem_description": "Vlad, like everyone else, loves to sleep very much.Every day Vlad has to do $$$n$$$ things, each at a certain time. For each of these things, he has an alarm clock set, the $$$i$$$-th of them is triggered on $$$h_i$$$ hours $$$m_i$$$ minutes every day ($$$0 \\le h_i < 24, 0 \\le m_i < 60$$$). Vlad uses the $$$24$$$-hour time format, so after $$$h=12, m=59$$$ comes $$$h=13, m=0$$$ and after $$$h=23, m=59$$$ comes $$$h=0, m=0$$$.This time Vlad went to bed at $$$H$$$ hours $$$M$$$ minutes ($$$0 \\le H < 24, 0 \\le M < 60$$$) and asks you to answer: how much he will be able to sleep until the next alarm clock.If any alarm clock rings at the time when he went to bed, then he will sleep for a period of time of length $$$0$$$.", "c_code": "int solution() {\n int test;\n scanf(\"%d\", &test);\n while (test--) {\n int n;\n int t = 0;\n int x = 24 * 60;\n int y = x;\n scanf(\"%d\", &n);\n int H[n + 1];\n int M[n + 1];\n int T[n + 1];\n scanf(\"%d %d\", &H[0], &M[0]);\n T[0] = H[0] * 60 + M[0];\n for (int i = 1; i < n + 1; i++) {\n scanf(\"%d %d\", &H[i], &M[i]);\n T[i] = H[i] * 60 + M[i];\n if ((H[i] == H[0]) && (M[i] == M[0])) {\n t = 1;\n }\n if ((T[i] - T[0]) < 0) {\n x = T[i] - T[0] + 24 * 60;\n } else {\n x = T[i] - T[0];\n }\n if (x < y) {\n y = x;\n }\n }\n if (t == 1) {\n printf(\"0 0\\n\");\n } else {\n printf(\"%d %d\\n\", y / 60, y % 60);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut tln = String::new();\n std::io::stdin().read_line(&mut tln).unwrap();\n let tcn = tln.trim_end().parse::().unwrap();\n\n for _k in 0..tcn {\n let mut sln = String::new();\n\n std::io::stdin().read_line(&mut sln).unwrap();\n let sln = sln\n .split_ascii_whitespace()\n .map(|x| x.trim_end().parse::().unwrap())\n .collect::>();\n let (n, t_hour, t_minute) = (sln[0], sln[1], sln[2]);\n\n let mut alarm_vec = Vec::new();\n\n for _i in 0..n {\n let mut cln = String::new();\n std::io::stdin().read_line(&mut cln).unwrap();\n\n let ca = cln\n .split_ascii_whitespace()\n .map(|x| x.trim_end().parse::().unwrap())\n .collect::>();\n\n alarm_vec.push(ca[0] * 60 + ca[1]);\n }\n\n alarm_vec.sort_unstable();\n if !alarm_vec.is_empty() {\n alarm_vec.push(alarm_vec[0] + 1440);\n }\n match alarm_vec.binary_search(&(t_hour * 60 + t_minute)) {\n Ok(_idx) => {\n println!(\"0 0\");\n }\n Err(idx) => {\n let intv = alarm_vec[idx] - (t_hour * 60 + t_minute);\n println!(\"{} {}\", intv / 60, intv % 60);\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1239", "problem_description": "

電子レンジ (Microwave)

\n

問題

\n\n

\nJOI 君は食事の準備のため,A ℃の肉を電子レンジで B ℃まで温めようとしている.\n肉は温度が 0 ℃未満のとき凍っている.\nまた,温度が 0 ℃より高いとき凍っていない.\n温度がちょうど 0 ℃のときの肉の状態は,凍っている場合と,凍っていない場合の両方があり得る.\n

\n\n

\nJOI 君は,肉の加熱にかかる時間は以下のようになると仮定して,肉を温めるのにかかる時間を見積もることにした.\n

\n\n
    \n
  • 肉が凍っていて,その温度が 0 ℃より小さいとき: C 秒で 1 ℃温まる.\n
  • 肉が凍っていて,その温度がちょうど 0 ℃のとき: D 秒で肉が解凍され,凍っていない状態になる.\n
  • 肉が凍っていないとき: E 秒で 1 ℃温まる.\n
\n\n

\nこの見積もりにおいて,肉を B ℃にするのに何秒かかるかを求めよ.\n

\n\n

入力

\n

\n入力は 5 行からなり,1 行に 1 個ずつ整数が書かれている.\n

\n\n

\n1 行目には,もともとの肉の温度 A が書かれている.
\n2 行目には,目的の温度 B が書かれている.
\n3 行目には,凍った肉を 1 ℃温めるのにかかる時間 C が書かれている.
\n4 行目には,凍った肉を解凍するのにかかる時間 D が書かれている.
\n5 行目には,凍っていない肉を 1 ℃温めるのにかかる時間 E が書かれている.\n

\n\n

\nもともとの温度 A は -100 以上 100 以下,目的の温度 B は 1 以上 100 以下であり,A ≠ 0 および A < B を満たす.\n

\n\n

\n温めるのにかかる時間 C, D, E はすべて 1 以上 100 以下である.\n

\n\n

出力

\n

\n肉を B ℃にするのにかかる秒数を 1 行で出力せよ.\n

\n\n

入出力例

\n

入力例 1

\n
\n-10\n20\n5\n10\n3\n
\n

出力例 1

\n\n
\n120\n
\n\n
\n\n

入力例 2

\n
\n35\n92\n31\n50\n11\n
\n\n

出力例 2

\n\n
\n627\n
\n
\n\n

\n入出力例 1 では,もともとの肉は -10 ℃で凍っている.かかる時間は以下のようになる.\n

\n\n
    \n
  • -10 ℃から 0 ℃まで温めるのに 5 × 10 = 50 秒.\n
  • 0 ℃の肉を解凍するのに 10 秒.\n
  • 0 ℃から 20 ℃まで温めるのに 3 × 20 = 60 秒.\n
\n\n

\nしたがって,かかる時間の合計は 120 秒である.\n

\n\n

\n入出力例 2 では,もともとの肉は凍っていない.したがって,肉を 35 ℃から 92 ℃まで温めるのにかかる時間は 627 秒である.\n

\n\n
\n\n", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int e = 0;\n int t = 0;\n scanf(\"%d%d%d%d%d\", &a, &b, &c, &d, &e);\n if (a < 0) {\n t = -a * c + d + e * b;\n } else {\n t = (b - a) * e;\n }\n printf(\"%d\\n\", t);\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').map(|s| s.parse::().unwrap());\n\n let a = lines.next().unwrap();\n let b = lines.next().unwrap();\n let c = lines.next().unwrap();\n let d = lines.next().unwrap();\n let e = lines.next().unwrap();\n\n let sec = if a <= 0 {\n -a * c + d + b * e\n } else {\n (b - a) * e\n };\n\n println!(\"{}\", sec);\n}", "difficulty": "easy"} {"problem_id": "1240", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the \"year-month-day\" order, or the \"month-day\" order without the year.
\nFor example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year.

\n

In this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.
\nHow many days from 2018-1-1 through 2018-a-b are Takahashi?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • a is an integer between 1 and 12 (inclusive).
  • \n
  • b is an integer between 1 and 31 (inclusive).
  • \n
  • 2018-a-b is a valid date in Gregorian calendar.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
a b\n
\n
\n
\n
\n
\n

Output

\n

Print the number of days from 2018-1-1 through 2018-a-b that are Takahashi.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 5\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

There are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

There is only one day that is Takahashi: 1-1.

\n
\n
\n
\n
\n
\n

Sample Input 3

11 30\n
\n
\n
\n
\n
\n

Sample Output 3

11\n
\n

There are eleven days that are Takahashi: 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 9-9, 10-10 and 11-11.

\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n scanf(\"%d %d\", &a, &b);\n if (a <= b) {\n printf(\"%d\", a);\n } else {\n printf(\"%d\", a - 1);\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let v: Vec = s.trim().split(\" \").map(|n| n.parse().unwrap()).collect();\n if v[0] > v[1] {\n println!(\"{}\", v[0] - 1);\n } else {\n println!(\"{}\", v[0]);\n }\n}", "difficulty": "medium"} {"problem_id": "1241", "problem_description": "You are given two arrays $$$a$$$ and $$$b$$$ of positive integers, with length $$$n$$$ and $$$m$$$ respectively. Let $$$c$$$ be an $$$n \\times m$$$ matrix, where $$$c_{i,j} = a_i \\cdot b_j$$$. You need to find a subrectangle of the matrix $$$c$$$ such that the sum of its elements is at most $$$x$$$, and its area (the total number of elements) is the largest possible.Formally, you need to find the largest number $$$s$$$ such that it is possible to choose integers $$$x_1, x_2, y_1, y_2$$$ subject to $$$1 \\leq x_1 \\leq x_2 \\leq n$$$, $$$1 \\leq y_1 \\leq y_2 \\leq m$$$, $$$(x_2 - x_1 + 1) \\times (y_2 - y_1 + 1) = s$$$, and $$$$$$\\sum_{i=x_1}^{x_2}{\\sum_{j=y_1}^{y_2}{c_{i,j}}} \\leq x.$$$$$$", "c_code": "int solution() {\n int n;\n int m;\n int a[2010];\n int b[2010];\n int x;\n\n scanf(\"%d %d\", &n, &m);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 1; i <= m; i++) {\n scanf(\"%d\", &b[i]);\n }\n scanf(\"%d\", &x);\n\n a[0] = b[0] = 0;\n for (int i = 1; i <= n; i++) {\n a[i] = a[i - 1] + a[i];\n }\n for (int i = 1; i <= m; i++) {\n b[i] = b[i - 1] + b[i];\n }\n\n long long sa[2005];\n long long sb[2005];\n memset(sa, 0x7f, sizeof(sa));\n memset(sb, 0x7f, sizeof(sb));\n\n for (int i = 1; i <= n; i++) {\n for (int j = i; j <= n; j++) {\n int sum = a[j] - a[i - 1];\n int len = j - i + 1;\n if (sa[len] > sum) {\n sa[len] = sum;\n }\n }\n }\n\n for (int i = 1; i <= m; i++) {\n for (int j = i; j <= m; j++) {\n int sum = b[j] - b[i - 1];\n int len = j - i + 1;\n if (sb[len] > sum) {\n sb[len] = sum;\n }\n }\n }\n\n int answer = 0;\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (sa[i] * sb[j] <= (long long)(x) && answer < i * j) {\n answer = i * j;\n }\n }\n }\n\n printf(\"%d\\n\", answer);\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 m = arr[1];\n\n let a = arr.iter().skip(2).take(n as usize).collect::>();\n let b = arr\n .iter()\n .skip(2 + n as usize)\n .take(m as usize)\n .collect::>();\n let x = *arr.get(2 + n as usize + m as usize).unwrap();\n\n use std::cmp;\n\n use std::collections::HashMap;\n let mut ma = HashMap::new();\n let mut mb = HashMap::new();\n\n for i in 0..a.len() {\n let mut accm = 0;\n for j in i..a.len() {\n accm += a[j];\n if accm <= x {\n let num_squares = j - i + 1;\n if let std::collections::hash_map::Entry::Vacant(e) = ma.entry(num_squares) {\n e.insert(accm);\n } else {\n let v = *ma.get_mut(&num_squares).unwrap();\n *(ma.get_mut(&num_squares).unwrap()) = cmp::min(v, accm);\n }\n }\n }\n }\n for i in 0..b.len() {\n let mut accm = 0;\n for j in i..b.len() {\n accm += b[j];\n if accm <= x {\n let num_squares = j - i + 1;\n if let std::collections::hash_map::Entry::Vacant(e) = mb.entry(num_squares) {\n e.insert(accm);\n } else {\n let v = *mb.get_mut(&num_squares).unwrap();\n *(mb.get_mut(&num_squares).unwrap()) = cmp::min(v, accm);\n }\n }\n }\n }\n\n let va: Vec<_> = ma.drain().collect();\n let vb: Vec<_> = mb.drain().collect();\n let mut max_area = 0;\n for (k1, v1) in va.iter() {\n for (k2, v2) in vb.iter() {\n if *v1 * v2 <= x {\n max_area = cmp::max(max_area, k1 * k2);\n }\n }\n }\n\n println!(\"{}\", max_area);\n}", "difficulty": "medium"} {"problem_id": "1242", "problem_description": "Today we will be playing a red and white colouring game (no, this is not the Russian Civil War; these are just the colours of the Canadian flag).You are given an $$$n \\times m$$$ grid of \"R\", \"W\", and \".\" characters. \"R\" is red, \"W\" is white and \".\" is blank. The neighbours of a cell are those that share an edge with it (those that only share a corner do not count).Your job is to colour the blank cells red or white so that every red cell only has white neighbours (and no red ones) and every white cell only has red neighbours (and no white ones). You are not allowed to recolour already coloured cells.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int m;\n int n;\n scanf(\"%d%d\", &m, &n);\n char st[m][n];\n char ch;\n scanf(\"%c\", &ch);\n for (int i = 0; i < m; i++) {\n scanf(\"%s\", st[i]);\n scanf(\"%c\", &ch);\n }\n char mt1[m][n];\n char mt2[m][n];\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if ((i + j) % 2 == 0) {\n mt1[i][j] = 'W';\n } else {\n mt1[i][j] = 'R';\n }\n }\n }\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if ((i + j) % 2 == 0) {\n mt2[i][j] = 'R';\n } else {\n mt2[i][j] = 'W';\n }\n }\n }\n\n int ct = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (st[i][j] == 'R' && mt1[i][j] == 'W') {\n ct = 1;\n break;\n }\n if (st[i][j] == 'W' && mt1[i][j] == 'R') {\n ct = 1;\n break;\n }\n }\n if (ct == 1) {\n break;\n }\n }\n if (ct == 0) {\n printf(\"YES\\n\");\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n printf(\"%c\", mt1[i][j]);\n }\n printf(\"\\n\");\n }\n } else {\n ct = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (st[i][j] == 'R' && mt2[i][j] == 'W') {\n ct = 1;\n break;\n }\n if (st[i][j] == 'W' && mt2[i][j] == 'R') {\n ct = 1;\n break;\n }\n }\n if (ct == 1) {\n break;\n }\n }\n if (ct == 0) {\n printf(\"YES\\n\");\n for (int i = 0; i < m; i++) {\n\n for (int j = 0; j < n; j++) {\n printf(\"%c\", mt2[i][j]);\n }\n printf(\"\\n\");\n }\n\n } else {\n printf(\"NO\\n\");\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 m = it.next().unwrap();\n\n let mut grid = Vec::with_capacity(n * m);\n\n for _ in 0..n {\n for c in lines.next().unwrap().chars() {\n grid.push(match c {\n 'R' => 0,\n 'W' => 1,\n _ => 2,\n })\n }\n }\n\n let mut r_even = None;\n\n for i in 0..n {\n for j in 0..m {\n let even = (i + j) % 2 == 0;\n let c = grid[i * m + j];\n if c == 2 {\n continue;\n }\n\n if let Some(r) = r_even {\n let o = if r && even || !r && !even { 0 } else { 1 };\n\n if c != o {\n writeln!(&mut output, \"NO\").unwrap();\n continue 'cases;\n }\n } else {\n r_even = Some(c == 0 && even || c == 1 && !even);\n }\n }\n }\n\n writeln!(&mut output, \"YES\").unwrap();\n\n let r_even = r_even.unwrap_or(true);\n\n for i in 0..n {\n for j in 0..m {\n let even = (i + j) % 2 == 0;\n let c = match (even, r_even) {\n (true, true) | (false, false) => 'R',\n _ => 'W',\n };\n write!(&mut output, \"{}\", c).unwrap();\n }\n writeln!(&mut output).unwrap();\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1243", "problem_description": "You are given a string $$$a$$$, consisting of $$$n$$$ characters, $$$n$$$ is even. For each $$$i$$$ from $$$1$$$ to $$$n$$$ $$$a_i$$$ is one of 'A', 'B' or 'C'.A bracket sequence is a string containing only characters \"(\" and \")\". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters \"1\" and \"+\" between the original characters of the sequence. For example, bracket sequences \"()()\" and \"(())\" are regular (the resulting expressions are: \"(1)+(1)\" and \"((1+1)+1)\"), and \")(\", \"(\" and \")\" are not.You want to find a string $$$b$$$ that consists of $$$n$$$ characters such that: $$$b$$$ is a regular bracket sequence; if for some $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$) $$$a_i=a_j$$$, then $$$b_i=b_j$$$. In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.Your task is to determine if such a string $$$b$$$ exists.", "c_code": "int solution() {\n int i;\n int t;\n scanf(\"%d\", &t);\n char a[51];\n for (i = 0; i < t; i++) {\n int s = 0;\n int b = 0;\n int c = 0;\n int m = 50;\n\n scanf(\"%s\", a);\n int j = 0;\n int d = 1;\n\n while (a[j] != '\\0' && j < 50) {\n if (a[j] == a[0]) {\n s++;\n b++;\n c++;\n }\n if (a[j] != a[0] && d == 1) {\n m = j;\n\n d = 0;\n }\n if (a[j] == a[m]) {\n s--;\n b++;\n c--;\n }\n if (a[j] != a[0] && a[j] != a[m] && a[j] != '\\0') {\n s++;\n b--;\n c--;\n }\n if (s < 0) {\n s = -50;\n }\n if (b < 0) {\n b = -50;\n }\n if (c < 0) {\n c = -50;\n }\n j++;\n }\n if (b == 0 || c == 0 || s == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let _t = it.next();\n\n for a in it {\n let a = a.as_bytes();\n let s = a.first().unwrap();\n let e = a.last().unwrap();\n\n let ans = if e == s {\n false\n } else {\n let mut ans = false;\n\n for &opening in &[true, false] {\n let mut d = 0;\n for c in a {\n if c == s || (c != e && opening) {\n d += 1;\n } else {\n if d == 0 {\n d = 1;\n break;\n }\n d -= 1;\n }\n }\n\n if d == 0 {\n ans = true;\n break;\n }\n }\n ans\n };\n\n println!(\"{}\", if ans { \"YES\" } else { \"NO \" });\n }\n}", "difficulty": "medium"} {"problem_id": "1244", "problem_description": "You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning.You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language.You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes.", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n char l1[m][15];\n char l2[m][15];\n for (int i = 0; i < m; i++) {\n scanf(\"%s %s\", l1[i], l2[i]);\n }\n char s[15];\n for (int i = 1; i <= n; i++) {\n scanf(\"%s\", s);\n for (int j = 0; j < m; j++) {\n if (strcmp(s, l1[j]) == 0) {\n if (strlen(s) <= strlen(l2[j])) {\n printf(\"%s \", s);\n } else {\n printf(\"%s \", l2[j]);\n }\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut fl = String::new();\n io::stdin().read_line(&mut fl).expect(\"error reading line\");\n let fl: Vec<&str> = fl.as_str().split(' ').collect();\n let n: i32 = fl[0].trim().parse().expect(\"en\");\n let m: i32 = fl[1].trim().parse().expect(\"en\");\n let mut v1 = Vec::new();\n let mut v2 = Vec::new();\n let mut sl = String::new();\n for _i in 0..m {\n sl = \"\".to_string();\n io::stdin().read_line(&mut sl).expect(\"error reading line\");\n let sl: Vec = sl.as_str().split(' ').map(|s| s.to_string()).collect();\n v1.push(sl[0].clone());\n v2.push(sl[1].clone());\n }\n let mut fl = String::new();\n io::stdin().read_line(&mut fl).expect(\"error reading line\");\n let fl: Vec<&str> = fl.as_str().split(' ').collect();\n for i in 0..n {\n let i = i as usize;\n for j in 0..m {\n let j = j as usize;\n if fl[i].trim() == v1[j].trim() || fl[i].trim() == v2[j].trim() {\n if v1[j].trim().len() <= v2[j].trim().len() {\n print!(\"{} \", v1[j].trim());\n } else {\n print!(\"{} \", v2[j].trim());\n }\n break;\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1245", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N people living on a number line.

\n

The i-th person lives at coordinate X_i.

\n

You are going to hold a meeting that all N people have to attend.

\n

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

\n

Find the minimum total points of stamina the N people have to spend.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq X_i \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nX_1 X_2 ... X_N\n
\n
\n
\n
\n
\n

Output

Print the minimum total stamina the N people have to spend.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n1 4\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

\n

Note that you can hold the meeting only at an integer coordinate.

\n
\n
\n
\n
\n
\n

Sample Input 2

7\n14 14 2 13 56 2 37\n
\n
\n
\n
\n
\n

Sample Output 2

2354\n
\n
\n
", "c_code": "int solution() {\n int N;\n int sum = 0;\n int sum_sq = 0;\n int p = 0;\n int ans_a = 0;\n int ans_b = 0;\n scanf(\"%d\", &N);\n int x[N];\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &x[i]);\n sum += x[i];\n sum_sq += x[i] * x[i];\n }\n p = sum / N;\n ans_a = N * p * p - 2 * sum * p + sum_sq;\n p++;\n ans_b = N * p * p - 2 * sum * p + sum_sq;\n printf(\"%d\\n\", (ans_a > ans_b ? ans_b : ans_a));\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin()\n .read_line(&mut buf)\n .expect(\"Failed to read line\");\n let vec_1: Vec<&str> = buf.split_whitespace().collect();\n let n: i32 = vec_1[0].parse().unwrap_or(0);\n\n let mut buf = String::new();\n io::stdin()\n .read_line(&mut buf)\n .expect(\"Failed to read line\");\n let vec_x: Vec<&str> = buf.split_whitespace().collect();\n\n let mut sum_x: i32 = 0;\n for i in &vec_x {\n let t: i32 = i.parse().unwrap();\n sum_x += t;\n }\n let mut p: i32 = sum_x / n;\n\n let mut ans_1: i32 = 0;\n for i in &vec_x {\n let t: i32 = i.parse().unwrap();\n ans_1 += (t - p) * (t - p);\n }\n\n p += 1;\n let mut ans_2: i32 = 0;\n for i in &vec_x {\n let t: i32 = i.parse().unwrap();\n ans_2 += (t - p) * (t - p);\n }\n\n println!(\"{}\", min(ans_1, ans_2));\n}", "difficulty": "hard"} {"problem_id": "1246", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There is a string s of length 3 or greater.\nNo two neighboring characters in s are equal.

\n

Takahashi and Aoki will play a game against each other.\nThe two players alternately performs the following operation, Takahashi going first:

\n
    \n
  • Remove one of the characters in s, excluding both ends. However, a character cannot be removed if removal of the character would result in two neighboring equal characters in s.
  • \n
\n

The player who becomes unable to perform the operation, loses the game. Determine which player will win when the two play optimally.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 ≤ |s| ≤ 10^5
  • \n
  • s consists of lowercase English letters.
  • \n
  • No two neighboring characters in s are equal.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

If Takahashi will win, print First. If Aoki will win, print Second.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

aba\n
\n
\n
\n
\n
\n

Sample Output 1

Second\n
\n

Takahashi, who goes first, cannot perform the operation, since removal of the b, which is the only character not at either ends of s, would result in s becoming aa, with two as neighboring.

\n
\n
\n
\n
\n
\n

Sample Input 2

abc\n
\n
\n
\n
\n
\n

Sample Output 2

First\n
\n

When Takahashi removes b from s, it becomes ac.\nThen, Aoki cannot perform the operation, since there is no character in s, excluding both ends.

\n
\n
\n
\n
\n
\n

Sample Input 3

abcab\n
\n
\n
\n
\n
\n

Sample Output 3

First\n
\n
\n
", "c_code": "int solution() {\n char s[100002];\n scanf(\"%s\", s);\n int len = strlen(s);\n if ((s[0] == s[len - 1] || len % 2 != 0) &&\n (s[0] != s[len - 1] || len % 2 == 0)) {\n printf(\"First\\n\");\n } else {\n printf(\"Second\\n\");\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut buf = String::new();\n\n stdin.read_line(&mut buf).unwrap();\n let s_vec: Vec = buf.trim().chars().collect();\n\n let parity = s_vec.len() + if s_vec.first() == s_vec.last() { 1 } else { 0 };\n\n match parity % 2 {\n 1 => println!(\"First\"),\n _ => println!(\"Second\"),\n }\n}", "difficulty": "easy"} {"problem_id": "1247", "problem_description": "Tired of boring office work, Denis decided to open a fast food restaurant.On the first day he made $$$a$$$ portions of dumplings, $$$b$$$ portions of cranberry juice and $$$c$$$ pancakes with condensed milk.The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules: every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes); each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk; all visitors should receive different sets of dishes. What is the maximum number of visitors Denis can feed?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int s[3];\n scanf(\"%d %d %d\", &s[0], &s[1], &s[2]);\n int ans = 0;\n if (s[0] > 0) {\n ans++;\n s[0]--;\n }\n if (s[1] > 0) {\n ans++;\n s[1]--;\n }\n if (s[2] > 0) {\n ans++;\n s[2]--;\n }\n for (int i = 0; i < 2; i++) {\n for (int j = 0; j < 2 - i; j++) {\n if (s[j] > s[j + 1]) {\n int swap = s[j];\n s[j] = s[j + 1];\n s[j + 1] = swap;\n }\n }\n }\n if (s[2] > 0 && s[1] > 0) {\n ans++;\n s[2]--;\n s[1]--;\n }\n if (s[2] > 0 && s[0] > 0) {\n ans++;\n s[2]--;\n s[0]--;\n }\n if (s[1] > 0 && s[0] > 0) {\n ans++;\n s[1]--;\n s[0]--;\n }\n if (s[0] > 0 && s[1] > 0 && s[2] > 0) {\n ans++;\n s[0]--;\n s[1]--;\n s[2]--;\n }\n printf(\"%d\\n\", ans);\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines().map(|l| l.unwrap());\n let t: u32 = lines.next().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let line = lines.next().unwrap();\n let mut parts = line.split_whitespace();\n let a: u32 = parts.next().unwrap().parse().unwrap();\n let b: u32 = parts.next().unwrap().parse().unwrap();\n let c: u32 = parts.next().unwrap().parse().unwrap();\n let mut x = [a, b, c];\n x.sort();\n match x[0] {\n 0 => {\n if x[2] == 0 {\n println!(\"0\");\n } else if x[1] == 0 {\n println!(\"1\");\n } else if x[1] == 1 {\n println!(\"2\");\n } else {\n println!(\"3\");\n }\n }\n 1 => {\n if x[1] == 1 {\n println!(\"3\");\n } else {\n println!(\"4\");\n }\n }\n 2 => {\n if x[2] == 2 {\n println!(\"4\");\n } else {\n println!(\"5\");\n }\n }\n 3 => {\n println!(\"6\");\n }\n _ => {\n println!(\"7\");\n }\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1248", "problem_description": "

C: 祈祷 (Pray)

\n\n

とある双子は、コンテスト前に祈祷をすることで有名である。

\n

4 つの整数 $H, W, X, Y$ があって、$H \\times W$ と $x + y$ が両方とも奇数だと縁起が悪いらしい。

\n\n

入力

\n

4 つの整数 $H, W, X, Y$ が空白区切りで与えられる。

\n\n

出力

\n

縁起が悪いなら \"No\"、そうでないなら \"Yes\" を出力しなさい。ただし、最後の改行を忘れないこと。

\n\n

制約

\n
    \n
  • $H, W, X, Y$ は $1$ 以上 $100$ 以下の整数
  • \n
\n\n

入力例1

\n
\n3 5 1 4\n
\n\n

出力例1

\n
\nNo\n
\n

$3 \\times 5 = 15$, $1 + 4 = 5$ で、両方とも奇数なので、縁起が悪いです。

\n\n

入力例2

\n
\n3 5 2 4\n
\n\n

出力例2

\n
\nYes\n
\n

$3 \\times 5 = 15$ で奇数ですが、$2 + 4 = 6$ で偶数なので、縁起は悪くないです。

", "c_code": "int solution() {\n int h;\n int w;\n int x;\n int y;\n\n scanf(\"%d%d%d%d\", &h, &w, &x, &y);\n if ((h * w & 1) && ((x + y) & 1)) {\n puts(\"No\");\n } else {\n puts(\"Yes\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n H:i32,\n W:i32,\n X:i32,\n Y:i32,\n }\n if (((H * W) % 2) == 1) & (((X + Y) % 2) == 1) {\n println!(\"No\")\n } else {\n println!(\"Yes\")\n }\n}", "difficulty": "easy"} {"problem_id": "1249", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

Given are positive integers N and K.

\n

Determine if the 3N integers K, K+1, ..., K+3N-1 can be partitioned into N triples (a_1,b_1,c_1), ..., (a_N,b_N,c_N) so that the condition below is satisfied. Any of the integers K, K+1, ..., K+3N-1 must appear in exactly one of those triples.

\n
    \n
  • For every integer i from 1 to N, a_i + b_i \\leq c_i holds.
  • \n
\n

If the answer is yes, construct one such partition.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq K \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

If it is impossible to partition the integers satisfying the condition, print -1. If it is possible, print N triples in the following format:

\n
a_1 b_1 c_1\n:\na_N b_N c_N\n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 1\n
\n
\n
\n
\n
\n

Sample Output 1

1 2 3\n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 3\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n
\n
", "c_code": "int solution() {\n int N;\n int K;\n scanf(\"%d %d\", &N, &K);\n if (K * 2 + N * 2 - 1 >= K + (N * 5 + 1) / 2) {\n printf(\"-1\\n\");\n fflush(stdout);\n return 0;\n }\n\n int i;\n for (i = 0; i < (N + 1) / 2; i++) {\n printf(\"%d %d %d\\n\", K + (i * 2), K + (N * 2) - 1 - i, K + (N * 5 / 2) + i);\n }\n for (i = 0; i < N / 2; i++) {\n printf(\"%d %d %d\\n\", K + (i * 2) + 1, K + (N * 2) - ((N + 1) / 2) - 1 - i,\n K + (N * 2) + i);\n }\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let (N, K): (usize, usize) = {\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 )\n };\n\n let mut a = vec![0; N];\n let mut b = vec![0; N];\n let c = (0..N).map(|i| K + 2 * N + i).collect::>();\n let m = (N - 1) / 2;\n let mut j = m;\n for x in (K..(K + N)).rev() {\n a[j] = x;\n j = (j + 1) % N;\n }\n for i in 0..m {\n b[i] = K + N + 2 * i + 1;\n }\n for i in m..N {\n b[i] = std::cmp::min(K + N + 2 * (i - m), K + 2 * N - 1);\n }\n let ans = if (0..N).all(|i| a[i] + b[i] <= c[i]) {\n (0..N)\n .map(|i| format!(\"{} {} {}\", a[i], b[i], c[i]))\n .collect::>()\n .join(\"\\n\")\n } else {\n \"-1\".to_string()\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1250", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

\n

Initially, the health of Monster i is A_i.

\n

Below, a monster with at least 1 health is called alive.

\n

Until there is only one alive monster, the following is repeated:

\n
    \n
  • A random alive monster attacks another random alive monster.
  • \n
  • As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.
  • \n
\n

Find the minimum possible final health of the last monster alive.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible final health of the last monster alive.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n2 10 8 40\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n5 13 8 1000000000\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3\n1000000000 1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

1000000000\n
\n
\n
", "c_code": "int solution(void) {\n\n int N;\n scanf(\"%d\", &N);\n int A[N];\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &A[i]);\n }\n while (1) {\n int min_iter = 0;\n while (A[min_iter] == 0) {\n min_iter++;\n }\n for (int i = 0; i < N; i++) {\n if (A[i] < A[min_iter] && A[i] != 0) {\n min_iter = i;\n }\n }\n for (int i = 0; i < N; i++) {\n if (i != min_iter) {\n A[i] = A[i] % A[min_iter];\n }\n }\n int count = 0;\n for (int i = 0; i < N; i++) {\n if (A[i] != 0) {\n count++;\n }\n }\n if (count == 1) {\n break;\n }\n }\n for (int i = 0; i < N; i++) {\n if (A[i] != 0) {\n printf(\"%d\\n\", A[i]);\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 let _n: usize = iter.next().unwrap().parse().unwrap();\n let ns: Vec = iter.map(|n| n.parse::().unwrap()).collect();\n\n let mut min: usize = *ns.iter().min().unwrap();\n\n loop {\n let pre_min = min;\n for n in ns.iter() {\n let sub = n % min;\n if sub != 0 && sub < min {\n min = sub;\n }\n }\n if min == pre_min {\n break;\n }\n }\n\n println!(\"{}\", min);\n}", "difficulty": "easy"} {"problem_id": "1251", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

When you asked some guy in your class his name, he called himself S, where S is a string of length between 3 and 20 (inclusive) consisting of lowercase English letters.\nYou have decided to choose some three consecutive characters from S and make it his nickname. Print a string that is a valid nickname for him.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq |S| \\leq 20
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print your answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

takahashi\n
\n
\n
\n
\n
\n

Sample Output 1

tak\n
\n
\n
\n
\n
\n
\n

Sample Input 2

naohiro\n
\n
\n
\n
\n
\n

Sample Output 2

nao\n
\n
\n
", "c_code": "int solution(void) {\n char a[3];\n scanf(\"%c%c%c\", &a[0], &a[1], &a[2]);\n printf(\"%c%c%c\", a[0], a[1], a[2]);\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n\n stdin().read_line(&mut is).ok();\n\n println!(\"{}\", &is[..3]);\n}", "difficulty": "hard"} {"problem_id": "1252", "problem_description": "We call two numbers $$$x$$$ and $$$y$$$ similar if they have the same parity (the same remainder when divided by $$$2$$$), or if $$$|x-y|=1$$$. For example, in each of the pairs $$$(2, 6)$$$, $$$(4, 3)$$$, $$$(11, 7)$$$, the numbers are similar to each other, and in the pairs $$$(1, 4)$$$, $$$(3, 12)$$$, they are not.You are given an array $$$a$$$ of $$$n$$$ ($$$n$$$ is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other.For example, for the array $$$a = [11, 14, 16, 12]$$$, there is a partition into pairs $$$(11, 12)$$$ and $$$(14, 16)$$$. The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n int a[n];\n int nc = 0;\n int nl = n - 1;\n int v = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &v);\n if (v % 2 == 0) {\n a[nc] = v;\n nc++;\n } else {\n a[nl] = v;\n nl--;\n }\n }\n\n if (nc % 2 == 0 && (n - nc) % 2 == 0) {\n printf(\"YES\\n\");\n } else {\n bool k = false;\n while (nc--) {\n for (int i = n - 1; i > nl; i--) {\n if (a[nc] - a[i] == 1 || a[i] - a[nc] == 1) {\n k = true;\n break;\n }\n }\n\n if (k) {\n break;\n }\n }\n\n if (k) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\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 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 mut a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n let odd = a.iter().filter(|&&x| x % 2 == 0).count();\n let even = n - odd;\n if odd % 2 == 0 && even.is_multiple_of(2) {\n writeln!(out, \"YES\").ok();\n } else {\n a.sort();\n let mut ok = false;\n for i in 0..n - 1 {\n if a[i + 1] - a[i] == 1 {\n writeln!(out, \"YES\").ok();\n ok = true;\n break;\n }\n }\n if !ok {\n writeln!(out, \"NO\").ok();\n }\n }\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "easy"} {"problem_id": "1253", "problem_description": "Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated?There are $$$n$$$ students living in a building, and for each of them the favorite drink $$$a_i$$$ is known. So you know $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$, where $$$a_i$$$ ($$$1 \\le a_i \\le k$$$) is the type of the favorite drink of the $$$i$$$-th student. The drink types are numbered from $$$1$$$ to $$$k$$$.There are infinite number of drink sets. Each set consists of exactly two portions of the same drink. In other words, there are $$$k$$$ types of drink sets, the $$$j$$$-th type contains two portions of the drink $$$j$$$. The available number of sets of each of the $$$k$$$ types is infinite.You know that students will receive the minimum possible number of sets to give all students exactly one drink. Obviously, the number of sets will be exactly $$$\\lceil \\frac{n}{2} \\rceil$$$, where $$$\\lceil x \\rceil$$$ is $$$x$$$ rounded up.After students receive the sets, they will distribute their portions by their choice: each student will get exactly one portion. Note, that if $$$n$$$ is odd then one portion will remain unused and the students' teacher will drink it.What is the maximum number of students that can get their favorite drink if $$$\\lceil \\frac{n}{2} \\rceil$$$ sets will be chosen optimally and students will distribute portions between themselves optimally?", "c_code": "int solution() {\n\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n int a[k];\n int por = (n / 2) + (n % 2);\n for (int i = 0; i < k; i++) {\n a[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n int tmp;\n scanf(\"%d\", &tmp);\n a[tmp - 1]++;\n }\n for (int i = 0; i < k; i++) {\n while (a[i] > 1) {\n por--;\n a[i] -= 2;\n }\n }\n if (por > 0) {\n for (int i = 0; i < k; i++) {\n if (a[i] > 0 && por > 0) {\n a[i]--;\n por--;\n }\n }\n }\n int res = 0;\n for (int i = 0; i < k; i++) {\n res += a[i];\n }\n printf(\"%d\", n - res);\n return 0;\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\n let n = get!();\n let k = get!(usize);\n let mut ds = vec![0; k];\n for _ in 0..n {\n let d = get!(usize) - 1;\n ds[d] += 1;\n }\n ds.retain(|&d| d > 0);\n ds.sort();\n let mut o = (n + 1) / 2;\n let mut ans = 0;\n let mut rem = 0;\n while let Some(d) = ds.pop() {\n rem += d % 2;\n let d = d - d % 2;\n if o >= d / 2 {\n ans += d;\n o -= d / 2;\n }\n }\n ans += std::cmp::min(o, rem);\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1254", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are four towns, numbered 1,2,3 and 4.\nAlso, there are three roads. The i-th road connects different towns a_i and b_i bidirectionally.\nNo two roads connect the same pair of towns. Other than these roads, there is no way to travel between these towns, but any town can be reached from any other town using these roads.

\n

Determine if we can visit all the towns by traversing each of the roads exactly once.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq a_i,b_i \\leq 4(1\\leq i\\leq 3)
  • \n
  • a_i and b_i are different. (1\\leq i\\leq 3)
  • \n
  • No two roads connect the same pair of towns.
  • \n
  • Any town can be reached from any other town using the roads.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a_1 b_1\na_2 b_2\na_3 b_3\n
\n
\n
\n
\n
\n

Output

If we can visit all the towns by traversing each of the roads exactly once, print YES; otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 2\n1 3\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

We can visit all the towns in the order 1,3,2,4.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 2\n2 4\n1 2\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2 1\n3 2\n4 3\n
\n
\n
\n
\n
\n

Sample Output 3

YES\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int e = 0;\n int f = 0;\n int g = 0;\n scanf(\"%d\", &a);\n scanf(\"%d\", &b);\n scanf(\"%d\", &c);\n scanf(\"%d\", &d);\n scanf(\"%d\", &e);\n scanf(\"%d\", &f);\n g = a + b + c + d + e + f;\n if (g == 15) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\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 iter = buf.split_whitespace().map(|c| c.parse::().unwrap());\n let mut counts = HashMap::new();\n\n for c in iter {\n if let Some(x) = counts.get_mut(&c) {\n *x += 1;\n continue;\n }\n counts.insert(c, 1);\n }\n\n if counts.values().all(|&c| (1..=2).contains(&c)) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "hard"} {"problem_id": "1255", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.

\n

You are given N numbers X_1, X_2, ..., X_N, where N is an even number.\nFor each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i, that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.

\n

Find B_i for each i = 1, 2, ..., N.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 200000
  • \n
  • N is even.
  • \n
  • 1 \\leq X_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nX_1 X_2 ... X_N\n
\n
\n
\n
\n
\n

Output

Print N lines.\nThe i-th line should contain B_i.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n2 4 4 3\n
\n
\n
\n
\n
\n

Sample Output 1

4\n3\n3\n4\n
\n
    \n
  • Since the median of X_2, X_3, X_4 is 4, B_1 = 4.
  • \n
  • Since the median of X_1, X_3, X_4 is 3, B_2 = 3.
  • \n
  • Since the median of X_1, X_2, X_4 is 3, B_3 = 3.
  • \n
  • Since the median of X_1, X_2, X_3 is 4, B_4 = 4.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

2\n1 2\n
\n
\n
\n
\n
\n

Sample Output 2

2\n1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6\n5 5 4 4 3 3\n
\n
\n
\n
\n
\n

Sample Output 3

4\n4\n4\n4\n4\n4\n
\n
\n
", "c_code": "int solution(void) {\n int i;\n int j;\n int l;\n int r;\n int t = 2;\n int n;\n int p;\n int tmp;\n int s[200000];\n int a[200000];\n int b[200000];\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b[i] = a[i];\n }\n s[0] = 0;\n s[1] = n - 1;\n while (t > 0) {\n t -= 2;\n i = l = s[t];\n j = r = s[t + 1];\n p = a[i];\n while (i <= j) {\n while (a[i] < p && i < r) {\n i++;\n }\n while (a[j] > p && j > l) {\n j--;\n }\n if (i <= j) {\n tmp = a[i];\n a[i] = a[j];\n a[j] = tmp;\n i++;\n j--;\n }\n }\n if (j > l) {\n s[t] = l;\n s[t + 1] = j;\n t += 2;\n }\n if (r > i) {\n s[t] = i;\n s[t + 1] = r;\n t += 2;\n }\n }\n for (i = 0; i < n; i++) {\n printf(\"%d\\n\", b[i] <= a[(n / 2) - 1] ? a[n / 2] : a[(n / 2) - 1]);\n }\n\n return 0;\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 mut x = Vec::new();\n for i in 0..n {\n let a: usize = itr.next().unwrap().parse().unwrap();\n x.push((a, i));\n }\n x.sort();\n\n let mut ans = vec![0; n];\n let mut out = Vec::new();\n for i in 0..n {\n if i < n / 2 {\n ans[x[i].1] = x[n / 2].0;\n } else {\n ans[x[i].1] = x[n / 2 - 1].0;\n }\n }\n for i in 0..n {\n writeln!(out, \"{}\", ans[i]).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "hard"} {"problem_id": "1256", "problem_description": "$$$2^k$$$ teams participate in a playoff tournament. The tournament consists of $$$2^k - 1$$$ games. They are held as follows: first of all, the teams are split into pairs: team $$$1$$$ plays against team $$$2$$$, team $$$3$$$ plays against team $$$4$$$ (exactly in this order), and so on (so, $$$2^{k-1}$$$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $$$2^{k-1}$$$ teams remain. If only one team remains, it is declared the champion; otherwise, $$$2^{k-2}$$$ games are played: in the first one of them, the winner of the game \"$$$1$$$ vs $$$2$$$\" plays against the winner of the game \"$$$3$$$ vs $$$4$$$\", then the winner of the game \"$$$5$$$ vs $$$6$$$\" plays against the winner of the game \"$$$7$$$ vs $$$8$$$\", and so on. This process repeats until only one team remains.For example, this picture describes the chronological order of games with $$$k = 3$$$: Let the string $$$s$$$ consisting of $$$2^k - 1$$$ characters describe the results of the games in chronological order as follows: if $$$s_i$$$ is 0, then the team with lower index wins the $$$i$$$-th game; if $$$s_i$$$ is 1, then the team with greater index wins the $$$i$$$-th game; if $$$s_i$$$ is ?, then the result of the $$$i$$$-th game is unknown (any team could win this game). Let $$$f(s)$$$ be the number of possible winners of the tournament described by the string $$$s$$$. A team $$$i$$$ is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team $$$i$$$ is the champion.You are given the initial state of the string $$$s$$$. You have to process $$$q$$$ queries of the following form: $$$p$$$ $$$c$$$ — replace $$$s_p$$$ with character $$$c$$$, and print $$$f(s)$$$ as the result of the query.", "c_code": "int solution(void) {\n int k;\n scanf(\"%d\\n\", &k);\n int n = (1 << k) - 1;\n unsigned *a = malloc(sizeof(unsigned) * n);\n char *r = malloc(n);\n for (int i = 0; i < n; ++i) {\n r[n - i - 1] = getchar();\n }\n for (int i = n - 1; i >= 0; --i) {\n if (i * 2 + 1 >= n) {\n a[i] = r[i] == '?' ? 2 : 1;\n } else {\n a[i] = r[i] == '0' ? a[(i * 2) + 2]\n : r[i] == '1' ? a[(i * 2) + 1]\n : a[(i * 2) + 2] + a[(i * 2) + 1];\n }\n }\n int q;\n scanf(\"%d\", &q);\n for (int qq = 0; qq < q; ++qq) {\n int p;\n char c;\n scanf(\"%d %c\", &p, &c);\n int i = n - p;\n r[i] = c;\n for (;;) {\n c = r[i];\n int prev = a[i];\n if (i * 2 + 1 >= n) {\n a[i] = r[i] == '?' ? 2 : 1;\n } else {\n a[i] = r[i] == '0' ? a[(i * 2) + 2]\n : r[i] == '1' ? a[(i * 2) + 1]\n : a[(i * 2) + 2] + a[(i * 2) + 1];\n }\n if (i == 0 || a[i] == prev) {\n break;\n }\n i = (i - 1) / 2;\n }\n printf(\"%d\\n\", a[0]);\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 k = it.next().unwrap();\n\n let s = lines.next().unwrap();\n\n let n = 1 << k;\n let mut t = vec![(0, 0); n];\n\n for (i, c) in s.chars().enumerate() {\n let i = n - 1 - i;\n match c {\n '0' => {\n t[i].1 = 0;\n t[i].0 = t.get(2 * i + 1).map(|&(x, _)| x).unwrap_or(1);\n }\n '1' => {\n t[i].1 = 1;\n t[i].0 = t.get(2 * i).map(|&(x, _)| x).unwrap_or(1);\n }\n _ => {\n t[i].1 = 2;\n t[i].0 = t.get(2 * i).map(|&(x, _)| x).unwrap_or(1)\n + t.get(2 * i + 1).map(|&(x, _)| x).unwrap_or(1);\n }\n }\n }\n\n let q = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..q {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace();\n\n let p = it.next().unwrap().parse::().unwrap();\n let c = it.next().unwrap();\n\n let mut i = n - p;\n let d = match c {\n \"0\" => 0,\n \"1\" => 1,\n _ => 2,\n };\n\n let a = match d {\n 0 => t.get(2 * i + 1).map(|&(x, _)| x).unwrap_or(1),\n 1 => t.get(2 * i).map(|&(x, _)| x).unwrap_or(1),\n _ => {\n t.get(2 * i).map(|&(x, _)| x).unwrap_or(1)\n + t.get(2 * i + 1).map(|&(x, _)| x).unwrap_or(1)\n }\n };\n\n t[i].0 += a;\n\n let s = match t[i].1 {\n 0 => t.get(2 * i + 1).map(|&(x, _)| x).unwrap_or(1),\n 1 => t.get(2 * i).map(|&(x, _)| x).unwrap_or(1),\n _ => {\n t.get(2 * i).map(|&(x, _)| x).unwrap_or(1)\n + t.get(2 * i + 1).map(|&(x, _)| x).unwrap_or(1)\n }\n };\n\n t[i].0 -= s;\n\n t[i].1 = d;\n\n i /= 2;\n\n while i > 0 {\n let s = match t[i].1 {\n 0 => t.get(2 * i + 1).map(|&(x, _)| x).unwrap_or(1),\n 1 => t.get(2 * i).map(|&(x, _)| x).unwrap_or(1),\n _ => {\n t.get(2 * i).map(|&(x, _)| x).unwrap_or(1)\n + t.get(2 * i + 1).map(|&(x, _)| x).unwrap_or(1)\n }\n };\n\n t[i].0 = s;\n\n i /= 2;\n }\n\n let ans: u64 = t[1].0;\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1257", "problem_description": "Patience sort", "c_code": "void solution(int *array, int length) {\n int i, j;\n\n int **piles = (int **)malloc(sizeof(int *) * length);\n\n for (i = 0; i < length; ++i) {\n piles[i] = (int *)malloc(sizeof(int) * length);\n }\n\n int *pileSizes = (int *)calloc(length, sizeof(int));\n\n piles[0][0] = array[0];\n int pileCount = 1;\n\n for (i = 1; i < length; ++i) {\n int placed = 0;\n\n for (j = 0; j < pileCount; ++j) {\n if (piles[j][pileSizes[j]] > array[i]) {\n piles[j][pileSizes[j] + 1] = array[i];\n pileSizes[j]++;\n placed = 1;\n break;\n }\n }\n\n if (!placed) {\n piles[pileCount][0] = array[i];\n pileCount++;\n }\n }\n\n for (i = 0; i < length; ++i) {\n int min, minLocation = -1;\n\n for (j = 0; j < pileCount; ++j) {\n if (pileSizes[j] >= 0) {\n min = piles[j][pileSizes[j]];\n minLocation = j;\n break;\n }\n }\n\n for (j = 0; j < pileCount; ++j) {\n if (pileSizes[j] < 0)\n continue;\n\n if (piles[j][pileSizes[j]] < min) {\n min = piles[j][pileSizes[j]];\n minLocation = j;\n }\n }\n\n array[i] = min;\n pileSizes[minLocation]--;\n }\n\n free(pileSizes);\n\n for (i = 0; i < length; ++i) {\n free(piles[i]);\n }\n\n free(piles);\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n if arr.is_empty() {\n return;\n }\n\n let mut piles: Vec> = Vec::new();\n for &card in arr.iter() {\n let mut left = 0usize;\n let mut right = piles.len();\n\n while left < right {\n let mid = left + (right - left) / 2;\n if piles[mid][piles[mid].len() - 1] >= card {\n right = mid;\n } else {\n left = mid + 1;\n }\n }\n\n if left == piles.len() {\n piles.push(vec![card]);\n } else {\n piles[left].push(card);\n }\n }\n\n let mut idx = 0usize;\n while let Some((min_id, pile)) = piles\n .iter()\n .enumerate()\n .min_by_key(|(_, pile)| *pile.last().unwrap())\n {\n arr[idx] = *pile.last().unwrap();\n idx += 1;\n piles[min_id].pop();\n\n if piles[min_id].is_empty() {\n _ = piles.remove(min_id);\n }\n }\n}\n", "difficulty": "easy"} {"problem_id": "1258", "problem_description": "\"Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come.\" — The Night's Watch oath.With that begins the watch of Jon Snow. He is assigned the task to support the stewards.This time he has n stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him.Can you find how many stewards will Jon support?", "c_code": "int solution(void) {\n int n = 0;\n int i = 0;\n int min = -1;\n int max = -1;\n int cnt = 0;\n int arr[100000] = {\n 0,\n };\n\n scanf(\"%d\", &n);\n\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (min == -1 || min > arr[i]) {\n min = arr[i];\n }\n\n if (max < arr[i]) {\n max = arr[i];\n }\n }\n\n for (i = 0; i < n; i++) {\n if (arr[i] > min && arr[i] < max) {\n cnt++;\n }\n }\n\n printf(\"%d\\n\", cnt);\n\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\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 let mut ns: Vec = buf\n .split_whitespace()\n .map(|str| str.trim().parse().unwrap())\n .collect();\n ns.sort();\n let min = ns[0];\n let max = ns.last().copied().unwrap();\n if min == max {\n println!(\"0\");\n } else {\n let num_min = ns.iter().take_while(|&&x| x == min).count();\n let num_max = ns.iter().rev().take_while(|&&x| x == max).count();\n println!(\"{}\", (ns.len() - num_min) - num_max);\n }\n}", "difficulty": "medium"} {"problem_id": "1259", "problem_description": "

Deque

\n \n

\n For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:\n

\n\n
    \n
  • push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
  • \n
  • randomAccess($p$): Print element $a_p$.
  • \n
  • pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
  • \n
\n\n

\n $A$ is a 0-origin array and it is empty in the initial state.\n

\n\n\n

Input

\n\n

\n The input is given in the following format.\n

\n\n
\n$q$\n$query_1$\n$query_2$\n:\n$query_q$\n
\n\n

\nEach query $query_i$ is given by\n

\n\n
\n0 $d$ $x$\n
\n\n

or

\n\n
\n1 $p$\n
\n\n

or

\n\n
\n2 $d$\n
\n\n

\n where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.\n

\n\n

\n randomAccess and pop operations will not be given for an empty array. \n

\n\n

Output

\n

\n For each randomAccess, print $a_p$ in a line.\n

\n\n

Constraints

\n
    \n
  • $1 \\leq q \\leq 400,000$
  • \n
  • $0 \\leq p < $ the size of $A$
  • \n
  • $-1,000,000,000 \\leq x \\leq 1,000,000,000$
  • \n
\n\n

Sample Input 1

\n\n
\n11\n0 0 1\n0 0 2\n0 1 3\n1 0\n1 1\n1 2\n2 0\n2 1\n0 0 4\n1 0\n1 1\n
\n\n

Sample Output 1

\n\n
\n2\n1\n3\n4\n1\n
", "c_code": "int solution() {\n int q;\n scanf(\"%d\", &q);\n int A[q * 2];\n int head = q;\n int end = q;\n int order;\n int hob;\n int position;\n int data;\n\n while (q--) {\n scanf(\"%d\", &order);\n switch (order) {\n case 0:\n scanf(\"%d%d\", &hob, &data);\n if (hob == 0) {\n head--;\n A[head] = data;\n\n } else {\n A[end] = data;\n end++;\n }\n\n break;\n\n case 1:\n scanf(\"%d\", &position);\n printf(\"%d\\n\", A[head + position]);\n break;\n\n case 2:\n scanf(\"%d\", &hob);\n if (hob == 0) {\n head++;\n } else {\n end--;\n }\n\n break;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n let row: usize = s.trim().parse().unwrap_or(0);\n s.clear();\n let mut array = VecDeque::with_capacity(row);\n\n for _ in 0..row {\n io::stdin().read_line(&mut s).ok();\n let v: Vec = s\n .split_whitespace()\n .map(|e| e.parse().unwrap_or(0))\n .collect();\n\n match v[0] {\n 0 => {\n if v[1] == 0 {\n array.push_front(v[2]);\n } else {\n array.push_back(v[2]);\n }\n }\n 1 => {\n println!(\"{}\", array[v[1] as usize]);\n }\n 2 => {\n if v[1] == 0 {\n array.pop_front();\n } else {\n array.pop_back();\n }\n }\n _ => println!(\"error 0 or 1 or 2\"),\n }\n s.clear();\n }\n}", "difficulty": "hard"} {"problem_id": "1260", "problem_description": "We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x < y$$$. If you sort the array in increasing order (such that $$$a_1 < a_2 < \\ldots < a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \\ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\\max(a_1, a_2, \\dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int x;\n int y;\n scanf(\"%d%d%d\", &n, &x, &y);\n int jiange = 1;\n int zhongjiangeshu = y - x - 1;\n\n while (1) {\n if ((zhongjiangeshu + 1) % jiange == 0) {\n if (zhongjiangeshu / jiange + 2 <= n) {\n break;\n }\n }\n jiange++;\n }\n int tempy = y;\n while (1) {\n printf(\"%d \", tempy);\n tempy -= jiange;\n n--;\n if (tempy <= 0 || n == 0) {\n break;\n }\n }\n tempy = y + jiange;\n while (n--) {\n printf(\"%d \", tempy);\n tempy += jiange;\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let t: i32 = s.trim().parse().unwrap();\n for _i in 0..t {\n s.clear();\n io::stdin().read_line(&mut s).unwrap();\n let arr: Vec = s.split_whitespace().map(|s| s.parse().unwrap()).collect();\n let (n, x, y) = (arr[0], arr[1], arr[2]);\n let mut d = 0;\n loop {\n d += 1;\n if (y - x) % d == 0 && (y - x) / d < n {\n break;\n }\n }\n let d = d;\n let st = {\n let mut x = y % d;\n if x == 0 {\n x = d;\n }\n x\n };\n let st = cmp::max(st, y - (n - 1) * d);\n for i in 0..n {\n if i > 0 {\n print!(\" \");\n }\n print!(\"{}\", st + i * d);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1261", "problem_description": "You are given an array $$$a$$$ with $$$n$$$ integers. You can perform the following operation at most $$$k$$$ times: Choose two indices $$$i$$$ and $$$j$$$, in which $$$i \\,\\bmod\\, k = j \\,\\bmod\\, k$$$ ($$$1 \\le i < j \\le n$$$). Swap $$$a_i$$$ and $$$a_j$$$. After performing all operations, you have to select $$$k$$$ consecutive elements, and the sum of the $$$k$$$ elements becomes your score. Find the maximum score you can get.Here $$$x \\bmod y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$.", "c_code": "int solution() {\n long x[1000] = {0};\n int t = 0;\n scanf(\"%d\", &t);\n int len = 0;\n int k = 0;\n for (int T = 0; T < t; T++) {\n scanf(\"%d %d\", &len, &k);\n for (int i = 0; i < len; i++) {\n scanf(\"%ld\", &x[i]);\n }\n long long sum = 0;\n for (int i = 0; i < k; i++) {\n int mid = 0;\n for (int j = i; j < len; j += k) {\n mid = x[j] > mid ? x[j] : mid;\n }\n sum += mid;\n }\n printf(\"%lld\\n\", sum);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let k = std::io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n 'joker: for _ in 0..k {\n let mut l = String::new();\n std::io::stdin().read_line(&mut l).unwrap();\n let l = l\n .trim()\n .split(\" \")\n .flat_map(str::parse::)\n .collect::>();\n let mut m = String::new();\n std::io::stdin().read_line(&mut m).unwrap();\n let mut m = m\n .trim()\n .split(\" \")\n .flat_map(str::parse::)\n .collect::>();\n let mut ans = 0i64;\n let _flag = true;\n let mut cl_f = l[0];\n 'jester: while cl_f != 0 {\n 'clown: for ids in 0..l[0] {\n if ids == m.len() as i64 - l[1] {\n break 'clown;\n }\n if m[ids as usize] < m[ids as usize + l[1] as usize] {\n m.swap(ids as usize, ids as usize + l[1] as usize);\n }\n }\n cl_f -= 1;\n }\n for vls in 0..l[1] {\n ans += m[vls as usize]\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "hard"} {"problem_id": "1262", "problem_description": "Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling.Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them.Pinkie Pie can eat the patty-cakes in any order. She is impatient about eating all the patty-cakes up so she asks you to help her to count the greatest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating!Pinkie Pie is going to buy more bags of patty-cakes so she asks you to solve this problem for several bags!", "c_code": "int solution() {\n long long int t;\n scanf(\"%lld\", &t);\n\n while (t != 0) {\n long long int n;\n scanf(\"%lld\", &n);\n\n long long int a[n];\n long long int b[n];\n long long int i;\n for (i = 0; i < n; ++i) {\n b[i] = 0;\n }\n for (i = 0; i < n; ++i) {\n scanf(\"%lld\", &a[i]);\n b[a[i] - 1] += 1;\n }\n\n long long int *c = (long long int *)malloc(100000 * sizeof(long long int));\n\n for (i = 0; i < 100000; ++i) {\n c[i] = 0;\n }\n\n for (i = 0; i < n; ++i) {\n c[b[i] - 1] += 1;\n }\n long long int select;\n for (i = n - 1; i >= 0; --i) {\n if (c[i] != 0) {\n select = i;\n break;\n }\n }\n\n long long int sol = c[select];\n long long int aux = n - ((select + 1) * c[select]);\n sol = sol + aux / select - 1;\n printf(\"%lld\\n\", sol);\n\n --t;\n }\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 a: 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 map = HashMap::new();\n for i in 0..n {\n *map.entry(a[i]).or_insert(0) += 1;\n }\n\n let (mut left, mut right) = (0, n);\n 'outer: while right - left > 1 {\n let mid = (left + right) / 2;\n\n let mut heap = BinaryHeap::new();\n for &value in map.values() {\n heap.push(value);\n }\n\n let mut memo = vec![0; n];\n for i in 0..n {\n if let Some(value) = heap.pop() {\n memo[i] = value - 1;\n } else {\n right = mid;\n continue 'outer;\n }\n if i >= mid && memo[i - mid] > 0 {\n heap.push(memo[i - mid]);\n }\n }\n left = mid;\n }\n println!(\"{}\", left);\n }\n}", "difficulty": "hard"} {"problem_id": "1263", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N islands lining up from west to east, connected by N-1 bridges.

\n

The i-th bridge connects the i-th island from the west and the (i+1)-th island from the west.

\n

One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:

\n

Request i: A dispute took place between the a_i-th island from the west and the b_i-th island from the west. Please make traveling between these islands with bridges impossible.

\n

You decided to remove some bridges to meet all these M requests.

\n

Find the minimum number of bridges that must be removed.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq M \\leq 10^5
  • \n
  • 1 \\leq a_i < b_i \\leq N
  • \n
  • All pairs (a_i, b_i) are distinct.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\na_1 b_1\na_2 b_2\n:\na_M b_M\n
\n
\n
\n
\n
\n

Output

Print the minimum number of bridges that must be removed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2\n1 4\n2 5\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

The requests can be met by removing the bridge connecting the second and third islands from the west.

\n
\n
\n
\n
\n
\n

Sample Input 2

9 5\n1 8\n2 7\n3 5\n4 6\n7 9\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int m;\n int near = 0;\n int ans = 0;\n scanf(\"%d%d\", &n, &m);\n int a[m];\n int b[m];\n int c[n];\n for (int i = 0; i < n; i++) {\n c[i] = 1e9;\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%d%d\", &a[i], &b[i]);\n a[i]--;\n b[i]--;\n if (c[a[i]] > b[i]) {\n c[a[i]] = b[i];\n }\n }\n for (int i = 0; i < n; i++) {\n if (c[i] != 1e9) {\n if (i >= near) {\n ans++;\n near = c[i];\n } else if (c[i] < near) {\n near = c[i];\n }\n }\n }\n printf(\"%d\\n\", ans);\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.trim().split(\" \");\n let _n: usize = iter.next().unwrap().parse().unwrap();\n let m: usize = iter.next().unwrap().parse().unwrap();\n let mut v: Vec<(u32, u32)> = Vec::with_capacity(m);\n for _ in 0..m {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.trim().split(\" \");\n let a: u32 = iter.next().unwrap().parse().unwrap();\n let b: u32 = iter.next().unwrap().parse().unwrap();\n v.push((a, b));\n }\n v.sort_by_key(|&x| x.1);\n let mut s: BTreeSet = BTreeSet::new();\n for (a, b) in v {\n if s.iter().any(|&x| a <= x && x < b) {\n continue;\n }\n s.insert(b - 1);\n }\n println!(\"{}\", s.len());\n}", "difficulty": "medium"} {"problem_id": "1264", "problem_description": "You are given a binary string $$$s$$$ (recall that a string is binary if each character is either $$$0$$$ or $$$1$$$).Let $$$f(t)$$$ be the decimal representation of integer $$$t$$$ written in binary form (possibly with leading zeroes). For example $$$f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$$$ and $$$f(000100) = 4$$$.The substring $$$s_{l}, s_{l+1}, \\dots , s_{r}$$$ is good if $$$r - l + 1 = f(s_l \\dots s_r)$$$.For example string $$$s = 1011$$$ has $$$5$$$ good substrings: $$$s_1 \\dots s_1 = 1$$$, $$$s_3 \\dots s_3 = 1$$$, $$$s_4 \\dots s_4 = 1$$$, $$$s_1 \\dots s_2 = 10$$$ and $$$s_2 \\dots s_4 = 011$$$. Your task is to calculate the number of good substrings of string $$$s$$$.You have to answer $$$t$$$ independent queries.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n char *s = malloc(sizeof(char) * 200001);\n int str_len;\n int good_substrings = 0;\n int zeros = 0;\n\n scanf(\"%s\", s);\n str_len = strlen(s);\n\n for (int i = 0; i < str_len; i++) {\n if (s[i] == '0') {\n zeros++;\n } else {\n long long val = 0;\n int substr_len = zeros;\n for (int j = i; j < str_len && val <= substr_len; j++) {\n substr_len++;\n val <<= 1;\n val += s[j] - 48;\n if (val <= substr_len) {\n good_substrings++;\n }\n }\n zeros = 0;\n }\n }\n\n free(s);\n printf(\"%d\\n\", good_substrings);\n }\n\n return 0;\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\n let t = get!();\n for _ in 0..t {\n let s = get().as_bytes();\n let mut z = 0;\n let mut ans = 0;\n for i in 0..s.len() {\n let x = (s[i] - b'0') as u32;\n if x == 0 {\n z += 1;\n continue;\n }\n let mut e = x;\n ans += 1;\n for j in i + 1..s.len() {\n let c = (j - i + 1) + z;\n let y = (s[j] - b'0') as u32;\n e = (e << 1) | y;\n if e > c as u32 {\n break;\n }\n ans += 1;\n }\n z = 0;\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1265", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.

\n
    \n
  • The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)
  • \n
  • The s_i-th digit from the left is c_i. \\left(i = 1, 2, \\cdots, M\\right)
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 3
  • \n
  • 0 \\leq M \\leq 5
  • \n
  • 1 \\leq s_i \\leq N
  • \n
  • 0 \\leq c_i \\leq 9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\ns_1 c_1\n\\vdots\ns_M c_M\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n1 7\n3 2\n1 7\n
\n
\n
\n
\n
\n

Sample Output 1

702\n
\n

702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 2\n2 1\n2 3\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3 1\n1 0\n
\n
\n
\n
\n
\n

Sample Output 3

-1\n
\n
\n
", "c_code": "int solution() {\n long n;\n long m;\n int flag = 0;\n scanf(\"%ld %ld\", &n, &m);\n\n long s[m];\n long c[m];\n for (int i = 0; i < m; i++) {\n scanf(\"%ld %ld\", &s[i], &c[i]);\n }\n\n long ans[3];\n for (int i = 0; i < 3; i++) {\n ans[i] = -1;\n }\n\n for (int i = 0; i < m; i++) {\n if (ans[s[i] - 1] == -1) {\n ans[s[i] - 1] = c[i];\n } else if (ans[s[i] - 1] != c[i]) {\n ans[s[i] - 1] = -2;\n }\n }\n\n if (ans[0] == 0 && n != 1) {\n flag = 1;\n }\n for (int i = 0; i < 3; i++) {\n if (ans[i] == -2) {\n flag = 1;\n } else if (i == 0 && ans[i] == -1 && n != 1) {\n ans[i] = 1;\n } else if (ans[i] == -1) {\n ans[i] = 0;\n }\n }\n\n if (flag == 0 && n == 3) {\n printf(\"%ld\", (ans[0] * 100) + (ans[1] * 10) + ans[2]);\n } else if (flag == 0 && n == 2) {\n printf(\"%ld\", (ans[0] * 10) + ans[1]);\n } else if (flag == 0 && n == 1) {\n printf(\"%ld\", ans[0]);\n } else {\n printf(\"-1\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let m: usize = s.next().unwrap().parse().unwrap();\n let a: Vec<_> = (0..m)\n .map(|_| {\n (\n s.next().unwrap().parse::().unwrap(),\n s.next().unwrap().parse::().unwrap(),\n )\n })\n .collect();\n let range = match n {\n 1 => 0..10,\n 2 => 10..100,\n 3 => 100..1000,\n _ => panic!(),\n };\n 'a: for i in range {\n let mut j = i;\n let mut d = vec![];\n for _ in 0..n {\n d.push(j % 10);\n j /= 10;\n }\n for &(s, c) in a.iter() {\n if d[n - s] != c {\n continue 'a;\n }\n }\n println!(\"{}\", i);\n return;\n }\n println!(\"-1\");\n}", "difficulty": "medium"} {"problem_id": "1266", "problem_description": "In order to write a string, Atilla needs to first learn all letters that are contained in the string.Atilla needs to write a message which can be represented as a string $$$s$$$. He asks you what is the minimum alphabet size required so that one can write this message.The alphabet of size $$$x$$$ ($$$1 \\leq x \\leq 26$$$) contains only the first $$$x$$$ Latin letters. For example an alphabet of size $$$4$$$ contains only the characters $$$\\texttt{a}$$$, $$$\\texttt{b}$$$, $$$\\texttt{c}$$$ and $$$\\texttt{d}$$$.", "c_code": "int solution() {\n int m = 0;\n int n = 0;\n int max = 0;\n char a[102];\n scanf(\"%d\", &m);\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &n);\n scanf(\"%s\", a);\n max = a[0];\n for (int j = 0; j < n; j++) {\n if (max < a[j]) {\n max = a[j];\n }\n }\n max = max - 'a' + 1;\n printf(\"%d\\n\", max);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(2)\n .step_by(2)\n .flatten()\n .for_each(|s| {\n if let Some(m) = s.as_bytes().iter().max() {\n let ans = m.wrapping_sub(b'`');\n writeln!(buf, \"{ans}\").ok();\n };\n });\n}", "difficulty": "hard"} {"problem_id": "1267", "problem_description": "Vasya has his favourite number $$$n$$$. He wants to split it to some non-zero digits. It means, that he wants to choose some digits $$$d_1, d_2, \\ldots, d_k$$$, such that $$$1 \\leq d_i \\leq 9$$$ for all $$$i$$$ and $$$d_1 + d_2 + \\ldots + d_k = n$$$.Vasya likes beauty in everything, so he wants to find any solution with the minimal possible number of different digits among $$$d_1, d_2, \\ldots, d_k$$$. Help him!", "c_code": "int solution() {\n int x;\n scanf(\"%d\", &x);\n printf(\"%d\\n\", x);\n while (x--) {\n printf(\"1 \");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines();\n\n let number: u16 = lines.next().unwrap().unwrap().parse::().unwrap();\n\n let mut output = String::with_capacity(2000);\n write!(output, \"{}\\n1\", number).unwrap();\n for _ in 1..number {\n write!(output, \" 1\").unwrap();\n }\n\n println!(\"{}\", output);\n}", "difficulty": "hard"} {"problem_id": "1268", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

In programming, hexadecimal notation is often used.

\n

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

\n

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

\n

When X and Y are seen as hexadecimal numbers, which is larger?

\n
\n
\n
\n
\n

Constraints

    \n
  • Each X and Y is A, B, C, D, E or F.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X Y\n
\n
\n
\n
\n
\n

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

A B\n
\n
\n
\n
\n
\n

Sample Output 1

<\n
\n

10 < 11.

\n
\n
\n
\n
\n
\n

Sample Input 2

E C\n
\n
\n
\n
\n
\n

Sample Output 2

>\n
\n

14 > 12.

\n
\n
\n
\n
\n
\n

Sample Input 3

F F\n
\n
\n
\n
\n
\n

Sample Output 3

=\n
\n

15 = 15.

\n
\n
", "c_code": "int solution(void) {\n char c[3];\n scanf(\"%c %c\", &c[0], &c[1]);\n if (c[0] > c[1]) {\n printf(\">\");\n } else if (c[0] == c[1]) {\n printf(\"=\");\n } else {\n printf(\"<\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let s: Vec<&str> = buf.split_whitespace().collect();\n\n if s[0] > s[1] {\n println!(\">\");\n } else if s[0] == s[1] {\n println!(\"=\");\n } else {\n println!(\"<\");\n }\n}", "difficulty": "easy"} {"problem_id": "1269", "problem_description": "You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int i = 0;\n int t = 0;\n int m = 0;\n int num[10000 + 1] = {0};\n scanf(\"%d\", &t);\n for (; i < t; ++i) {\n scanf(\"%d\", &num[i]);\n }\n for (i = 0; i < t; ++i) {\n if (num[i] % 3 == 0) {\n m = num[i] / 3;\n } else if (num[i] % 3 == 2) {\n m = num[i] / 3 + 1;\n } else {\n m = (num[i] - 3) / 3 + 2;\n }\n printf(\"%d\\n\", m);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut testcases = String::new();\n io::stdin().read_line(&mut testcases).expect(\"Error\");\n\n let t: usize = testcases.trim().parse().expect(\"errr\");\n testcases.clear();\n\n for _ in 0..t {\n let mut n = String::new();\n io::stdin().read_line(&mut n).expect(\"Error\");\n let n: usize = n.trim().parse().expect(\"errr\");\n let tme: usize;\n let m = n % 3;\n let n3: usize = n / 3;\n if n == 1 {\n tme = 2;\n } else if m == 0 {\n tme = n3;\n } else {\n tme = n3 + 1;\n };\n println!(\"{}\", tme);\n }\n}", "difficulty": "easy"} {"problem_id": "1270", "problem_description": "Let's call a number a binary decimal if it's a positive integer and all digits in its decimal notation are either $$$0$$$ or $$$1$$$. For example, $$$1\\,010\\,111$$$ is a binary decimal, while $$$10\\,201$$$ and $$$787\\,788$$$ are not.Given a number $$$n$$$, you are asked to represent $$$n$$$ as a sum of some (not necessarily distinct) binary decimals. Compute the smallest number of binary decimals required for that.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n long int n = 0;\n int temp = 0;\n int count = 0;\n for (int i = 0; i < t; i++) {\n scanf(\"%ld\", &n);\n count = 0;\n while (n != 0) {\n temp = n % 10;\n if (temp > count) {\n count = temp;\n }\n n = n / 10;\n }\n printf(\"%d\\n\", count);\n }\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let mut input = String::new();\n io::stdin().read_line(&mut input)?;\n let cases = input.trim().parse::()?;\n for _ in 0..cases {\n input.clear();\n io::stdin().read_line(&mut input)?;\n let mut n = input.trim().parse::()?;\n let mut max_digit = 0;\n while n != 0 {\n let d = n % 10;\n if d > max_digit {\n max_digit = d;\n }\n n /= 10;\n }\n println!(\"{}\", max_digit)\n }\n\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "1271", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N bags of biscuits. The i-th bag contains A_i biscuits.

\n

Takaki will select some of these bags and eat all of the biscuits inside.\nHere, it is also possible to select all or none of the bags.

\n

He would like to select bags so that the total number of biscuits inside is congruent to P modulo 2.\nHow many such ways to select bags there are?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 50
  • \n
  • P = 0 or 1
  • \n
  • 1 \\leq A_i \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N P\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the number of ways to select bags so that the total number of biscuits inside is congruent to P modulo 2.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 0\n1 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

There are two ways to select bags so that the total number of biscuits inside is congruent to 0 modulo 2:

\n
    \n
  • Select neither bag. The total number of biscuits is 0.
  • \n
  • Select both bags. The total number of biscuits is 4.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

1 1\n50\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3 0\n1 1 1\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n

Two bags are distinguished even if they contain the same number of biscuits.

\n
\n
\n
\n
\n
\n

Sample Input 4

45 1\n17 55 85 55 74 20 90 67 40 70 39 89 91 50 16 24 14 43 24 66 25 9 89 71 41 16 53 13 61 15 85 72 62 67 42 26 36 66 4 87 59 91 4 25 26\n
\n
\n
\n
\n
\n

Sample Output 4

17592186044416\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int p;\n int even = 0;\n int odd = 0;\n long long ans = 1;\n long long tmp1 = 1;\n long long tmp2 = 0;\n scanf(\"%d%d\", &n, &p);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] % 2 == 0) {\n even++;\n } else {\n odd++;\n }\n }\n if (p == 0) {\n for (int i = 0; i < even; i++) {\n ans *= 2;\n }\n for (int i = 0; i <= odd / 2; i++) {\n for (int j = 0; j < i * 2; j++) {\n tmp1 = tmp1 * (odd - j) / (j + 1);\n }\n tmp2 += tmp1;\n tmp1 = 1;\n }\n ans = ans * tmp2;\n } else {\n for (int i = 0; i < even; i++) {\n ans *= 2;\n }\n for (int i = 0; i < odd / 2; i++) {\n for (int j = 0; j < i * 2 + 1; j++) {\n tmp1 = tmp1 * (odd - j) / (j + 1);\n }\n tmp2 += tmp1;\n tmp1 = 1;\n }\n ans = ans * tmp2;\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let (N, P): (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 )\n };\n let A: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let ans = if A.iter().all(|&a| a % 2 == 0) {\n (1 - P) * 2i64.pow(N as u32)\n } else {\n 2i64.pow((N - 1) as u32)\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1272", "problem_description": "A palindrome is a string that reads the same backward as forward. For example, the strings \"z\", \"aaa\", \"aba\", and \"abccba\" are palindromes, but \"codeforces\" and \"ab\" are not. You hate palindromes because they give you déjà vu.There is a string $$$s$$$. You must insert exactly one character 'a' somewhere in $$$s$$$. If it is possible to create a string that is not a palindrome, you should find one example. Otherwise, you should report that it is impossible.For example, suppose $$$s=$$$ \"cbabc\". By inserting an 'a', you can create \"acbabc\", \"cababc\", \"cbaabc\", \"cbabac\", or \"cbabca\". However \"cbaabc\" is a palindrome, so you must output one of the other options.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int l;\n int i;\n int k;\n char c[300009];\n char p;\n char *x;\n scanf(\"%s\", c);\n l = strlen(c);\n p = c[0];\n for (i = 0; i < ((l / 2) + (l % 2)); i++) {\n if (c[i] != c[l - 1 - i]) {\n break;\n }\n if (p != c[i]) {\n p = '&';\n }\n }\n if (i != ((l / 2) + (l % 2))) {\n printf(\"YES\\n\");\n for (k = 0; k <= i; k++) {\n printf(\"%c\", c[k]);\n }\n x = &c[i + 1];\n printf(\"a\");\n printf(\"%s\\n\", x);\n } else if (i == ((l / 2) + (l % 2)) && p == 'a') {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n printf(\"%sa\\n\", c);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let _t = it.next().unwrap();\n\n for s in it {\n match s.chars().position(|c| c != 'a') {\n Some(i) => {\n let j = s.len() - i;\n let (s0, s1) = s.split_at(j);\n println!(\"YES\\n{}a{}\", s0, s1);\n }\n None => println!(\"NO\"),\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1273", "problem_description": "You are given a bracket sequence $$$s$$$ of length $$$n$$$, where $$$n$$$ is even (divisible by two). The string $$$s$$$ consists of $$$\\frac{n}{2}$$$ opening brackets '(' and $$$\\frac{n}{2}$$$ closing brackets ')'.In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index $$$i$$$, remove the $$$i$$$-th character of $$$s$$$ and insert it before or after all remaining characters of $$$s$$$).Your task is to find the minimum number of moves required to obtain regular bracket sequence from $$$s$$$. It can be proved that the answer always exists under the given constraints.Recall what the regular bracket sequence is: \"()\" is regular bracket sequence; if $$$s$$$ is regular bracket sequence then \"(\" + $$$s$$$ + \")\" is regular bracket sequence; if $$$s$$$ and $$$t$$$ are regular bracket sequences then $$$s$$$ + $$$t$$$ is regular bracket sequence. For example, \"()()\", \"(())()\", \"(())\" and \"()\" are regular bracket sequences, but \")(\", \"()(\" and \")))\" are not.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n int br = 0;\n int sum = 0;\n int moves = 0;\n char c = '0';\n scanf(\"%d\", &br);\n while (br >= 0) {\n br--;\n scanf(\"%c\", &c);\n if (c == '(') {\n sum++;\n } else if (c == ')') {\n sum--;\n }\n if (sum < 0) {\n moves++;\n sum++;\n }\n }\n printf(\"%d\\n\", moves);\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 _ = lines.next().unwrap().unwrap();\n let cs: Vec = lines.next().unwrap().unwrap().chars().collect();\n let mut x: i32 = 0;\n let mut m: i32 = 0;\n for c in cs {\n x += if c == ')' { -1 } else { 1 };\n m = min(m, x);\n }\n println!(\"{}\", -m);\n }\n}", "difficulty": "medium"} {"problem_id": "1274", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

E869120's and square1001's 16-th birthday is coming soon.
\nTakahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.

\n

E869120 and square1001 were just about to eat A and B of those pieces, respectively,
\nwhen they found a note attached to the cake saying that \"the same person should not take two adjacent pieces of cake\".

\n

Can both of them obey the instruction in the note and take desired numbers of pieces of cake?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • A and B are integers between 1 and 16 (inclusive).
  • \n
  • A+B is at most 16.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

\n

If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 4\n
\n
\n
\n
\n
\n

Sample Output 1

Yay!\n
\n

Both of them can take desired number of pieces as follows:\n\"

\n
\n
\n
\n
\n
\n

Sample Input 2

8 8\n
\n
\n
\n
\n
\n

Sample Output 2

Yay!\n
\n

Both of them can take desired number of pieces as follows:\n\"

\n
\n
\n
\n
\n
\n

Sample Input 3

11 4\n
\n
\n
\n
\n
\n

Sample Output 3

:(\n
\n

In this case, there is no way for them to take desired number of pieces, unfortunately.

\n
\n
", "c_code": "int solution() {\n int A = 0;\n int B = 0;\n\n scanf(\"%d %d\", &A, &B);\n\n if (A <= 8 && B <= 8) {\n printf(\"Yay!\\n\");\n } else {\n printf(\":(\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut args = String::new();\n stdin().read_line(&mut args).expect(\"Failed to read line\");\n let args: Vec = args\n .split_whitespace()\n .filter_map(|it| it.parse().ok())\n .collect();\n let a = args[0];\n let b = args[1];\n println!(\"{}\", if a > 8 || b > 8 { \":(\" } else { \"Yay!\" });\n}", "difficulty": "easy"} {"problem_id": "1275", "problem_description": "

Sorting Five Numbers

\n\n

\nWrite a program which reads five numbers and sorts them in descending order.\n

\n\n

Input

\n

\nInput consists of five numbers a, b, c, d and e (-100000 ≤ a, b, c, d,e ≤ 100000). The five numbers are separeted by a space.\n

\n\n

Output

\n\n

\nPrint the ordered numbers in a line. Adjacent numbers should be separated by a space.\n

\n\n

Sample Input

\n\n
\n3 6 9 7 5\n
\n\n

Output for the Sample Input

\n\n
\n9 7 6 5 3\n
", "c_code": "int solution(void) {\n int number[5];\n int i;\n int j;\n scanf(\"%d %d %d %d %d\", &number[0], &number[1], &number[2], &number[3],\n &number[4]);\n\n for (i = 0; i < 5; i++) {\n for (j = 4; j > i; j--) {\n if (number[j] > number[j - 1]) {\n int temp = number[j];\n number[j] = number[j - 1];\n number[j - 1] = temp;\n }\n }\n }\n for (i = 0; i < 4; i++) {\n printf(\"%d \", number[i]);\n }\n printf(\"%d\\n\", number[4]);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n let mut values: Vec = buf\n .split_whitespace()\n .filter_map(|x| x.parse::().ok())\n .collect();\n values.sort();\n let values: Vec = values.iter().rev().map(|x| x.to_string()).collect();\n println!(\"{}\", values.join(\" \"));\n}", "difficulty": "hard"} {"problem_id": "1276", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

\n

More specifically, in one move, he can go from coordinate x to x + D or x - D.

\n

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

\n

Find the minimum possible absolute value of the coordinate of the destination.

\n
\n
\n
\n
\n

Constraints

    \n
  • -10^{15} \\leq X \\leq 10^{15}
  • \n
  • 1 \\leq K \\leq 10^{15}
  • \n
  • 1 \\leq D \\leq 10^{15}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X K D\n
\n
\n
\n
\n
\n

Output

Print the minimum possible absolute value of the coordinate of the destination.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Takahashi is now at coordinate 6. It is optimal to make the following moves:

\n
    \n
  • Move from coordinate 6 to (6 - 4 =) 2.
  • \n
  • Move from coordinate 2 to (2 - 4 =) -2.
  • \n
\n

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 4 3\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

\n
    \n
  • Move from coordinate 7 to 4.
  • \n
  • Move from coordinate 4 to 7.
  • \n
  • Move from coordinate 7 to 4.
  • \n
  • Move from coordinate 4 to 1.
  • \n
\n

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 1 2\n
\n
\n
\n
\n
\n

Sample Output 3

8\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1000000000000000 1000000000000000 1000000000000000\n
\n
\n
\n
\n
\n

Sample Output 4

1000000000000000\n
\n

The answer can be enormous.

\n
\n
", "c_code": "int solution(void) {\n long long int x;\n long long int k;\n long long int d;\n long long int q;\n long long int r;\n scanf(\"%lld%lld%lld\", &x, &k, &d);\n x = llabs(x);\n d = llabs(d);\n q = x / d;\n r = x % d;\n if (k <= q) {\n printf(\"%lld\", x - (d * k));\n return 0;\n }\n if ((k - q) % 2 == 0) {\n printf(\"%lld\", r);\n return 0;\n }\n if ((k - q) % 2 != 0) {\n printf(\"%lld\", d - r);\n return 0;\n }\n}", "rust_code": "fn solution() {\n let (x, k, d): (i64, i64, i64) = {\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\n let x = x.abs();\n let c = x / d;\n if c <= k {\n if (k - c) % 2 == 0 {\n println!(\"{}\", x - c * d);\n } else {\n println!(\"{}\", (c + 1) * d - x);\n }\n } else {\n println!(\"{}\", x - k * d);\n }\n}", "difficulty": "easy"} {"problem_id": "1277", "problem_description": "Suppose you are performing the following algorithm. There is an array $$$v_1, v_2, \\dots, v_n$$$ filled with zeroes at start. The following operation is applied to the array several times — at $$$i$$$-th step ($$$0$$$-indexed) you can: either choose position $$$pos$$$ ($$$1 \\le pos \\le n$$$) and increase $$$v_{pos}$$$ by $$$k^i$$$; or not choose any position and skip this step. You can choose how the algorithm would behave on each step and when to stop it. The question is: can you make array $$$v$$$ equal to the given array $$$a$$$ ($$$v_j = a_j$$$ for each $$$j$$$) after some step?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n int i;\n int j;\n long long int k;\n long long int a[33];\n int b[102];\n for (; t > 0; t--) {\n scanf(\"%d %lld\", &n, &k);\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n for (i = 0; i < 102; i++) {\n b[i] = 0;\n }\n for (i = 0; i < n; i++) {\n j = 0;\n while (a[i] > 0) {\n b[j] += a[i] % k;\n a[i] /= k;\n j++;\n }\n }\n j = 0;\n for (i = 0; i < 102; i++) {\n if (b[i] > 1) {\n j++;\n }\n }\n if (j > 0) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\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 = t.trim().parse().unwrap();\n\n for _ in 0..t {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let _n = words[0];\n let k = words[1];\n\n line = String::new();\n stdin().read_line(&mut line).unwrap();\n\n let mut a: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut ans = \"YES\";\n while a.iter().any(|&x| x != 0) {\n let b = a\n .iter()\n .map(|x| x % k)\n .filter(|&x| x != 0)\n .collect::>();\n if b > vec![1] {\n ans = \"NO\";\n break;\n }\n a = a.iter().map(|x| x / k).collect();\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1278", "problem_description": "Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters, or print that it is impossible.String s consists only of lowercase Latin letters, and it is allowed to change characters only to lowercase Latin letters too.", "c_code": "int solution() {\n char str[1001];\n scanf(\"%s\", str);\n int i = 0;\n int ans = 0;\n int k;\n int cnt[26];\n scanf(\"%d\", &k);\n while (i < 26) {\n cnt[i++] = 0;\n }\n i = 0;\n while (str[i] != '\\0') {\n if (cnt[str[i] - 'a'] == 0) {\n ans++;\n cnt[str[i] - 'a'] = 1;\n }\n i++;\n }\n if (i < k) {\n printf(\"impossible\\n\");\n } else if (ans > k) {\n printf(\"0\\n\");\n } else {\n printf(\"%d\\n\", k - ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"Something went wrong\");\n let s: &str = s.trim();\n let mut k = String::new();\n io::stdin().read_line(&mut k).expect(\"Something went wrong\");\n let k: i32 = k.trim().parse().expect(\"Something went wrong\");\n if k > s.len() as i32 {\n println!(\"impossible\")\n } else {\n let s1: Vec = \"abcdefghijklmnopqrstuvwxyz\".chars().collect();\n let mut a: [i32; 26] = [0; 26];\n for c in s.chars() {\n let mut d: usize = 25;\n let mut b: usize = 0;\n let mut m: usize = (b + d) / 2;\n while b < d - 1 {\n m = (b + d) / 2;\n if c > s1[m] {\n b = m\n } else {\n d = m\n }\n }\n if d == 1 && c == 'a' {\n a[0] = 1\n } else if a[d] == 0 {\n a[d] = 1;\n }\n }\n let mut sum: i32 = 0;\n for f in 0..26 {\n sum += a[f];\n }\n if k - sum > 0 {\n println!(\"{}\", k - sum)\n } else {\n println!(\"0\")\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1279", "problem_description": "The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters \"+\", \"-\", \"X\". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).", "c_code": "int solution() {\n static int i;\n static int n;\n static int x;\n char str[4];\n\n scanf(\"%d\", &n);\n if (n >= 1 && n <= 150) {\n for (i = 0; i < n; i++) {\n scanf(\"%s\", str);\n if (str[0] == '+' && str[2] == 'X') {\n ++x;\n }\n if (str[0] == 'X' && str[1] == '+') {\n x++;\n }\n if (str[0] == '-' && str[2] == 'X') {\n --x;\n }\n if (str[0] == 'X' && str[1] == '-') {\n x--;\n }\n }\n printf(\"%d\", x);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let num = buf.trim().parse::().unwrap();\n let mut result = 0;\n buf.clear();\n for _ in 0..num {\n io::stdin().read_line(&mut buf).unwrap();\n }\n\n for c in buf.chars() {\n if c == '+' {\n result += 1;\n }\n if c == '-' {\n result -= 1;\n }\n }\n println!(\"{}\", result / 2);\n}", "difficulty": "medium"} {"problem_id": "1280", "problem_description": "Polycarp has an array $$$a$$$ consisting of $$$n$$$ integers.He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains $$$n-1$$$ elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.Formally: If it is the first move, he chooses any element and deletes it; If it is the second or any next move: if the last deleted element was odd, Polycarp chooses any even element and deletes it; if the last deleted element was even, Polycarp chooses any odd element and deletes it. If after some move Polycarp cannot make a move, the game ends. Polycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.Help Polycarp find this value.", "c_code": "int solution() {\n int n;\n int even = 0;\n scanf(\"%d\", &n);\n int ara[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &ara[i]);\n if (ara[i] % 2 == 0) {\n even++;\n }\n }\n int odd = n - even;\n if (odd == even || odd + 1 == even || even + 1 == odd) {\n printf(\"0\\n\");\n } else if (odd > even) {\n int len = n - (2 * even) - 1;\n for (int i = 0; i < n - 1; i++) {\n for (int j = i + 1; j < n; j++) {\n if (ara[i] > ara[j]) {\n int tmp = ara[i];\n ara[i] = ara[j];\n ara[j] = tmp;\n }\n }\n }\n int sum = 0;\n for (int i = 0; i < len; i++) {\n if (ara[i] % 2 == 1) {\n sum += ara[i];\n } else {\n len++;\n }\n }\n printf(\"%d\\n\", sum);\n } else {\n int len = n - (2 * odd) - 1;\n for (int i = 0; i < n - 1; i++) {\n for (int j = i + 1; j < n; j++) {\n if (ara[i] > ara[j]) {\n int tmp = ara[i];\n ara[i] = ara[j];\n ara[j] = tmp;\n }\n }\n }\n int sum = 0;\n for (int i = 0; i < len; i++) {\n if (ara[i] % 2 == 0) {\n sum += ara[i];\n } else {\n len++;\n }\n }\n printf(\"%d\\n\", sum);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let _n: usize = input.trim().parse().unwrap();\n input.clear();\n\n io::stdin().read_line(&mut input).unwrap();\n let a: Vec = input\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let (mut even, mut odd): (Vec, Vec) = a.into_iter().partition(|&x| x % 2 == 0);\n\n even.sort();\n even.reverse();\n odd.sort();\n odd.reverse();\n\n let diff = (odd.len() as isize - even.len() as isize).abs();\n\n let mut diff_vec = vec![];\n if diff > 1 {\n for _ in 0..diff as usize {\n if even.len() > odd.len() {\n diff_vec.push(even.pop().unwrap());\n } else if odd.len() > even.len() {\n diff_vec.push(odd.pop().unwrap());\n }\n }\n }\n diff_vec.pop();\n\n let sum = diff_vec.iter().sum::();\n println!(\"{}\", sum);\n}", "difficulty": "medium"} {"problem_id": "1281", "problem_description": "The only difference between easy and hard versions are constraints on $$$n$$$ and $$$k$$$.You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $$$k$$$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $$$0$$$).Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.You (suddenly!) have the ability to see the future. You know that during the day you will receive $$$n$$$ messages, the $$$i$$$-th message will be received from the friend with ID $$$id_i$$$ ($$$1 \\le id_i \\le 10^9$$$).If you receive a message from $$$id_i$$$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.Otherwise (i.e. if there is no conversation with $$$id_i$$$ on the screen): Firstly, if the number of conversations displayed on the screen is $$$k$$$, the last conversation (which has the position $$$k$$$) is removed from the screen. Now the number of conversations on the screen is guaranteed to be less than $$$k$$$ and the conversation with the friend $$$id_i$$$ is not displayed on the screen. The conversation with the friend $$$id_i$$$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all $$$n$$$ messages.", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\" %d\", &a[i]);\n }\n int b[k];\n int idx = 1;\n for (int i = 0; i < k; i++) {\n b[i] = 0;\n }\n b[0] = a[0];\n for (int i = 1; i < n; i++) {\n int flag = 0;\n for (int j = 0; j < k; j++) {\n if (a[i] == b[j]) {\n flag = 1;\n break;\n }\n }\n if (flag == 0) {\n for (int j = k - 1; j >= 1; j--) {\n b[j] = b[j - 1];\n }\n b[0] = a[i];\n }\n }\n int c = 0;\n for (int i = 0; i < k; i++) {\n if (b[i] != 0) {\n c++;\n }\n }\n printf(\"%d\\n\", c);\n for (int i = 0; i < k; i++) {\n if (b[i] != 0) {\n printf(\"%d \", b[i]);\n }\n }\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n\n let mut result = Vec::new();\n\n let parts: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .filter(|s| !s.is_empty())\n .map(|s| s.parse::().unwrap())\n .collect();\n\n let screen_size = parts[1];\n\n let message_conv_ids: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split_whitespace()\n .filter(|s| !s.is_empty())\n .map(|s| s.parse::().unwrap())\n .collect();\n\n for mc_id in message_conv_ids {\n if !result.is_empty() && result[0] == mc_id {\n continue;\n }\n\n if result.contains(&mc_id) {\n continue;\n }\n result.insert(0, mc_id);\n\n if result.len() > screen_size as usize {\n result.pop();\n }\n }\n\n println!(\"{}\", result.len());\n for val in result {\n print!(\"{} \", val);\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1282", "problem_description": "A binary string is a string consisting only of the characters 0 and 1. You are given a binary string $$$s_1 s_2 \\ldots s_n$$$. It is necessary to make this string non-decreasing in the least number of operations. In other words, each character should be not less than the previous. In one operation, you can do the following: Select an arbitrary index $$$1 \\leq i \\leq n$$$ in the string; For all $$$j \\geq i$$$, change the value in the $$$j$$$-th position to the opposite, that is, if $$$s_j = 1$$$, then make $$$s_j = 0$$$, and vice versa.What is the minimum number of operations needed to make the string non-decreasing?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n int i;\n while (t--) {\n scanf(\"%d\\n\", &n);\n char s[n];\n int ops = 0;\n scanf(\"%c\", &s[0]);\n for (i = 1; i < n; i++) {\n scanf(\"%c\", &s[i]);\n if (s[i] != s[i - 1]) {\n ops++;\n }\n }\n if ((s[0] == '0') && (ops > 0)) {\n ops--;\n }\n printf(\"%d\\n\", ops);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n for _ in 0..t {\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\n let s: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n };\n\n let mut ans = 0;\n\n if s[0] == '1' {\n ans += 1;\n }\n\n for i in 1..n {\n if s[i] != s[i - 1] {\n ans += 1;\n }\n }\n\n ans -= 1;\n\n if ans < 0 {\n ans = 0;\n }\n\n println!(\"{ans:?}\");\n }\n}", "difficulty": "medium"} {"problem_id": "1283", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

\n

The door of Snuke's laboratory is locked with a security code.

\n

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

\n

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • S is a 4-character string consisting of digits.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

\n

If S is hard to enter, print Bad; otherwise, print Good.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3776\n
\n
\n
\n
\n
\n

Sample Output 1

Bad\n
\n

The second and third digits are the same, so 3776 is hard to enter.

\n
\n
\n
\n
\n
\n

Sample Input 2

8080\n
\n
\n
\n
\n
\n

Sample Output 2

Good\n
\n

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

\n
\n
\n
\n
\n
\n

Sample Input 3

1333\n
\n
\n
\n
\n
\n

Sample Output 3

Bad\n
\n
\n
\n
\n
\n
\n

Sample Input 4

0024\n
\n
\n
\n
\n
\n

Sample Output 4

Bad\n
\n
\n
", "c_code": "int solution() {\n char str[5];\n scanf(\"%s\", str);\n if (str[0] != str[1] && str[1] != str[2] && str[2] != str[3]) {\n printf(\"Good\\n\");\n } else {\n printf(\"Bad\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n\n std::io::stdin().read_line(&mut s).unwrap();\n\n let vecs: Vec = s.chars().collect();\n\n let mut isNotInputtable: bool = false;\n\n for i in 0..(vecs.len() - 1) {\n if vecs[i] == vecs[i + 1] {\n isNotInputtable = true;\n }\n }\n\n if isNotInputtable {\n println!(\"Bad\");\n } else {\n println!(\"Good\");\n }\n}", "difficulty": "medium"} {"problem_id": "1284", "problem_description": "ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen.For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.", "c_code": "int solution()\n\n{\n int i = 0;\n int count = 0;\n int n = 0;\n int c = 0;\n int t[111111];\n scanf(\"%d %d\", &n, &c);\n\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &t[i]);\n if (i >= 1) {\n if (t[i] - t[i - 1] <= c) {\n count++;\n } else {\n count = 1;\n }\n }\n\n if (i == 0) {\n count++;\n }\n }\n\n printf(\"%d\", count);\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 mut ints = s.split_whitespace().map(|x| x.parse::().unwrap());\n let n = ints.next().unwrap();\n let t = ints.next().unwrap();\n let mut r = String::new();\n std::io::stdin().read_line(&mut r).unwrap();\n let ints: Vec = r\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n let mut count = 1;\n for i in (1..n).rev() {\n if ints[i] - ints[i - 1] > t {\n break;\n }\n count += 1;\n }\n println!(\"{}\", count);\n}", "difficulty": "hard"} {"problem_id": "1285", "problem_description": "The King of Berland Polycarp LXXXIV has $$$n$$$ daughters. To establish his power to the neighbouring kingdoms he wants to marry his daughters to the princes of these kingdoms. As a lucky coincidence there are $$$n$$$ other kingdoms as well.So Polycarp LXXXIV has enumerated his daughters from $$$1$$$ to $$$n$$$ and the kingdoms from $$$1$$$ to $$$n$$$. For each daughter he has compiled a list of kingdoms princes of which she wanted to marry.Polycarp LXXXIV is very busy, so he finds a couple for his daughters greedily one after another.For the first daughter he takes the kingdom with the lowest number from her list and marries the daughter to their prince. For the second daughter he takes the kingdom with the lowest number from her list, prince of which hasn't been taken already. If there are no free princes in the list then the daughter marries nobody and Polycarp LXXXIV proceeds to the next daughter. The process ends after the $$$n$$$-th daughter.For example, let there be $$$4$$$ daughters and kingdoms, the lists daughters have are $$$[2, 3]$$$, $$$[1, 2]$$$, $$$[3, 4]$$$, $$$[3]$$$, respectively. In that case daughter $$$1$$$ marries the prince of kingdom $$$2$$$, daughter $$$2$$$ marries the prince of kingdom $$$1$$$, daughter $$$3$$$ marries the prince of kingdom $$$3$$$, leaving daughter $$$4$$$ nobody to marry to.Actually, before starting the marriage process Polycarp LXXXIV has the time to convince one of his daughters that some prince is also worth marrying to. Effectively, that means that he can add exactly one kingdom to exactly one of his daughter's list. Note that this kingdom should not be present in the daughter's list.Polycarp LXXXIV wants to increase the number of married couples.Unfortunately, what he doesn't have the time for is determining what entry to add. If there is no way to increase the total number of married couples then output that the marriages are already optimal. Otherwise, find such an entry that the total number of married couples increases if Polycarp LXXXIV adds it.If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.For your and our convenience you are asked to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n long long int t;\n scanf(\"%lld\", &t);\n\n do {\n long long int n;\n scanf(\"%lld\", &n);\n\n long long int pos[n];\n long long int k[n];\n for (long long int i = 0; i < n; ++i) {\n pos[i] = -1;\n k[i] = -1;\n }\n long long int pr[n + 1];\n long long int alfa = -5;\n long long int beta = -5;\n for (long long int i = 0; i < n; ++i) {\n long long int r = 0;\n do {\n scanf(\"%lld\", &pr[r]);\n ++r;\n } while (getchar() != '\\n');\n\n for (long long int j = 1; j <= pr[0]; ++j) {\n if (pos[pr[j] - 1] == -1) {\n k[i] = pr[j];\n pos[pr[j] - 1] = 1;\n break;\n }\n }\n }\n for (long long int j = 0; j < n; ++j) {\n if (pos[j] == -1) {\n alfa = j + 1;\n break;\n }\n }\n\n for (long long int j = 0; j < n; ++j) {\n if (k[j] == -1) {\n beta = j + 1;\n break;\n }\n }\n\n if (alfa != -5 && beta != -5) {\n printf(\"IMPROVE\\n\");\n printf(\"%lld %lld\\n\", beta, alfa);\n } else {\n printf(\"OPTIMAL\\n\");\n }\n\n --t;\n } while (t != 0);\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lock = stdin.lock();\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n let t = line.trim().parse().unwrap();\n\n for _ in 0..t {\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n let n = line.trim().parse().unwrap();\n\n let mut choices: Vec> = Vec::with_capacity(n);\n\n for _ in 0..n {\n let mut line = String::new();\n lock.read_line(&mut line).unwrap();\n\n choices.push(\n line.split_whitespace()\n .skip(1)\n .map(|x| x.parse::().unwrap() - 1)\n .collect(),\n );\n }\n\n let mut princesses = vec![false; n];\n let mut kingdoms = vec![false; n];\n\n for (i, p) in choices.iter().enumerate() {\n for &k in p {\n if !kingdoms[k] {\n princesses[i] = true;\n kingdoms[k] = true;\n\n break;\n }\n }\n }\n\n let p = princesses.into_iter().position(|x| !x);\n let k = kingdoms.into_iter().position(|x| !x);\n\n if let Some(p) = p {\n if let Some(k) = k {\n println!(\"IMPROVE\");\n println!(\"{} {}\", p + 1, k + 1);\n } else {\n println!(\"OPTIMAL\");\n }\n } else {\n println!(\"OPTIMAL\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1286", "problem_description": "We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n for (; t > 0; t--) {\n int n;\n long long sum = 0;\n int x = 0;\n long long y = 0;\n int flag2 = 0;\n scanf(\"%d\", &n);\n long long a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n sum = sum + a[i];\n }\n if (sum == 0) {\n for (int i = n - 1; i > -1; i--) {\n if (a[i] != 0) {\n x = i;\n break;\n }\n x = i;\n }\n if (x == 0) {\n printf(\"YES\\n\");\n } else if (x == 1 && a[0] > 0) {\n printf(\"YES\");\n } else if (a[x] >= 0) {\n printf(\"NO\\n\");\n } else if (a[0] > 0 && a[x] < 0) {\n y = a[x];\n x--;\n for (int i = x; i > 0; i--) {\n y = y + a[i];\n if (y >= 0) {\n flag2 = 1;\n }\n }\n if (flag2 == 1) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n } else {\n printf(\"NO\\n\");\n }\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin().read_to_string(&mut content);\n\n let mut lines = content.lines();\n let num_tests: i32 = lines.next().unwrap().parse().unwrap();\n 'cycle: for _ in 0..num_tests {\n let _n: i32 = lines.next().unwrap().parse().unwrap();\n let a: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse().unwrap())\n .collect();\n if a.iter().sum::() != 0 {\n println!(\"No\");\n continue;\n }\n let mut prefix_sum = 0i64;\n let mut observed_zero = false;\n for x in a {\n prefix_sum += x;\n if prefix_sum < 0 {\n println!(\"No\");\n continue 'cycle;\n } else if prefix_sum == 0 {\n observed_zero = true;\n } else if observed_zero {\n println!(\"No\");\n continue 'cycle;\n }\n }\n println!(\"Yes\");\n }\n}", "difficulty": "medium"} {"problem_id": "1287", "problem_description": "Acacius is studying strings theory. Today he came with the following problem.You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string \"abacaba\" occurs as a substring in a resulting string exactly once?Each question mark should be replaced with exactly one lowercase English letter. For example, string \"a?b?c\" can be transformed into strings \"aabbc\" and \"azbzc\", but can't be transformed into strings \"aabc\", \"a?bbc\" and \"babbc\".Occurrence of a string $$$t$$$ of length $$$m$$$ in the string $$$s$$$ of length $$$n$$$ as a substring is a index $$$i$$$ ($$$1 \\leq i \\leq n - m + 1$$$) such that string $$$s[i..i+m-1]$$$ consisting of $$$m$$$ consecutive symbols of $$$s$$$ starting from $$$i$$$-th equals to string $$$t$$$. For example string \"ababa\" has two occurrences of a string \"aba\" as a substring with $$$i = 1$$$ and $$$i = 3$$$, but there are no occurrences of a string \"aba\" in the string \"acba\" as a substring.Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string \"abacaba\" occurs as a substring in a resulting string exactly once.", "c_code": "int solution() {\n int n;\n int t1;\n int i;\n int j;\n int k;\n int count;\n int flag;\n int curr_idx;\n char a[65];\n char c[65];\n char t[8];\n scanf(\"%d\", &t1);\n while (t1--) {\n for (i = 0; i < 64; i++) {\n a[i] = 'o';\n c[i] = 'o';\n }\n scanf(\"%d\", &n);\n scanf(\"%c\", &c[55]);\n scanf(\"%s\", a);\n count = 0;\n for (i = 0; i <= n - 7; i++) {\n if (a[i] == 'a' && a[i + 1] == 'b' && a[i + 2] == 'a' &&\n a[i + 3] == 'c' && a[i + 4] == 'a' && a[i + 5] == 'b' &&\n a[i + 6] == 'a') {\n count++;\n }\n }\n t[0] = 'a', t[1] = 'b', t[2] = 'a', t[3] = 'c', t[4] = 'a', t[5] = 'b',\n t[6] = 'a';\n for (i = 0; i < n; i++) {\n c[i] = a[i];\n }\n if (count == 1) {\n printf(\"YES\\n\");\n for (i = 0; i < n; i++) {\n if (a[i] == '?') {\n printf(\"d\");\n } else {\n printf(\"%c\", a[i]);\n }\n }\n printf(\"\\n\");\n } else if (count >= 2) {\n printf(\"NO\\n\");\n } else {\n for (i = 0; i <= n - 7; i++) {\n curr_idx = 0;\n if (count == 1 && flag == 0) {\n break;\n }\n for (j = i; j <= i + 6; j++) {\n if (a[j] == t[curr_idx]) {\n curr_idx++;\n } else if (a[j] == '?') {\n c[j] = t[curr_idx];\n curr_idx++;\n }\n }\n count = 0, flag = 0;\n j = i;\n if (c[j] == 'a' && c[j + 1] == 'b' && c[j + 2] == 'a' &&\n c[j + 3] == 'c' && c[j + 4] == 'a' && c[j + 5] == 'b' &&\n c[j + 6] == 'a') {\n count++;\n }\n\n j = i + 6;\n if (c[j] == 'a' && c[j + 1] == 'b' && c[j + 2] == 'a' &&\n c[j + 3] == 'c' && c[j + 4] == 'a' && c[j + 5] == 'b' &&\n c[j + 6] == 'a' && count == 1) {\n flag = 1;\n }\n j = i + 4;\n if (c[j] == 'a' && c[j + 1] == 'b' && c[j + 2] == 'a' &&\n c[j + 3] == 'c' && c[j + 4] == 'a' && c[j + 5] == 'b' &&\n c[j + 6] == 'a' && count == 1) {\n flag = 1;\n }\n if (i - 6 >= 0) {\n j = i - 6;\n }\n\n if (c[j] == 'a' && c[j + 1] == 'b' && c[j + 2] == 'a' &&\n c[j + 3] == 'c' && c[j + 4] == 'a' && c[j + 5] == 'b' &&\n c[j + 6] == 'a' && count == 1) {\n\n flag = 1;\n }\n if (i - 4 >= 0) {\n j = i - 4;\n }\n if (c[j] == 'a' && c[j + 1] == 'b' && c[j + 2] == 'a' &&\n c[j + 3] == 'c' && c[j + 4] == 'a' && c[j + 5] == 'b' &&\n c[j + 6] == 'a' && count == 1) {\n flag = 1;\n }\n if (!(count == 1 && flag == 0)) {\n for (j = i; j <= i + 6; j++) {\n c[j] = a[j];\n }\n }\n }\n if (!(count == 1 && flag == 0)) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n for (i = 0; i < n; i++) {\n if (c[i] == '?') {\n printf(\"d\");\n } else {\n printf(\"%c\", c[i]);\n }\n }\n printf(\"\\n\");\n }\n }\n }\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 let mut s: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().chars().collect()\n };\n\n let abacaba = ['a', 'b', 'a', 'c', 'a', 'b', 'a'];\n let mut count = 0;\n for i in 0..(n - 6) {\n let mut ok = true;\n for j in 0..7 {\n if s[i + j] != abacaba[j] {\n ok = false;\n break;\n }\n }\n if ok {\n count += 1;\n }\n }\n if count == 0 {\n let mut ok = false;\n 'outer: for i in 0..(n - 6) {\n for j in 0..7 {\n if s[i + j] != '?' && s[i + j] != abacaba[j] {\n continue 'outer;\n }\n }\n if i >= 6 {\n let mut ng = true;\n for j in 0..6 {\n if s[i + j - 6] != abacaba[j] {\n ng = false;\n }\n }\n if ng {\n continue 'outer;\n }\n }\n if i >= 4 {\n let mut ng = true;\n for j in 0..4 {\n if s[i + j - 4] != abacaba[j] {\n ng = false;\n }\n }\n if ng {\n continue 'outer;\n }\n }\n if i + 10 < n {\n let mut ng = true;\n for j in 0..4 {\n if s[i + j + 7] != abacaba[j + 3] {\n ng = false;\n }\n }\n if ng {\n continue 'outer;\n }\n }\n if i + 12 < n {\n let mut ng = true;\n for j in 0..6 {\n if s[i + j + 7] != abacaba[j + 1] {\n ng = false;\n }\n }\n if ng {\n continue 'outer;\n }\n }\n for j in 0..7 {\n s[i + j] = abacaba[j];\n }\n ok = true;\n break;\n }\n if ok {\n let ans: String = { s.iter().map(|&c| if c == '?' { 'd' } else { c }).collect() };\n println!(\"Yes\\n{}\", ans);\n } else {\n println!(\"No\");\n }\n } else if count == 1 {\n let ans: String = { s.iter().map(|&c| if c == '?' { 'd' } else { c }).collect() };\n println!(\"Yes\\n{}\", ans);\n } else {\n println!(\"No\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1288", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Let N be a positive odd number.

\n

There are N coins, numbered 1, 2, \\ldots, N.\nFor each i (1 \\leq i \\leq N), when Coin i is tossed, it comes up heads with probability p_i and tails with probability 1 - p_i.

\n

Taro has tossed all the N coins.\nFind the probability of having more heads than tails.

\n
\n
\n
\n
\n

Constraints

    \n
  • N is an odd number.
  • \n
  • 1 \\leq N \\leq 2999
  • \n
  • p_i is a real number and has two decimal places.
  • \n
  • 0 < p_i < 1
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\np_1 p_2 \\ldots p_N\n
\n
\n
\n
\n
\n

Output

Print the probability of having more heads than tails.\nThe output is considered correct when the absolute error is not greater than 10^{-9}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n0.30 0.60 0.80\n
\n
\n
\n
\n
\n

Sample Output 1

0.612\n
\n

The probability of each case where we have more heads than tails is as follows:

\n
    \n
  • The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Head) is 0.3 × 0.6 × 0.8 = 0.144;
  • \n
  • The probability of having (Coin 1, Coin 2, Coin 3) = (Tail, Head, Head) is 0.7 × 0.6 × 0.8 = 0.336;
  • \n
  • The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Tail, Head) is 0.3 × 0.4 × 0.8 = 0.096;
  • \n
  • The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Tail) is 0.3 × 0.6 × 0.2 = 0.036.
  • \n
\n

Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\n0.50\n
\n
\n
\n
\n
\n

Sample Output 2

0.5\n
\n

Outputs such as 0.500, 0.500000001 and 0.499999999 are also considered correct.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n0.42 0.01 0.42 0.99 0.42\n
\n
\n
\n
\n
\n

Sample Output 3

0.3821815872\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n scanf(\"%d\", &N);\n double a[N + 1];\n for (int i = 0; i < N; i++) {\n scanf(\"%lf\", &a[i + 1]);\n }\n double dp[N + 1][N + 1];\n\n for (int i = 0; i <= N; i++) {\n for (int j = 0; j <= N; j++) {\n dp[i][j] = 0;\n }\n }\n dp[0][0] = 1;\n for (int i = 1; i <= N; i++) {\n dp[i][0] = dp[i - 1][0] * (1 - a[i]);\n }\n\n for (int i = 1; i <= N; i++) {\n for (int j = 1; j <= i; j++) {\n dp[i][j] = dp[i - 1][j - 1] * a[i] + dp[i - 1][j] * (1 - a[i]);\n }\n }\n\n double sum = 0;\n for (int i = (N / 2) + 1; i <= N; i++) {\n sum += dp[N][i];\n }\n\n printf(\"%.10f\", sum);\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 a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let mut dp: Vec> = vec![vec![0.0; n + 1]; n + 1];\n dp[0][0] = 1.0;\n for i in 0..n {\n for num in 0..i + 1 {\n dp[i + 1][num + 1] += dp[i][num] * a[i];\n dp[i + 1][num] += dp[i][num] * (1.0 - a[i]);\n }\n }\n\n let mut ans: f64 = 0.0;\n for omote in 0..n + 1 {\n if omote > (n - omote) {\n ans += dp[n][omote];\n }\n }\n println!(\"{:.10}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1289", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi is meeting up with Aoki.

\n

They have planned to meet at a place that is D meters away from Takahashi's house in T minutes from now.

\n

Takahashi will leave his house now and go straight to the place at a speed of S meters per minute.

\n

Will he arrive in time?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq D \\leq 10000
  • \n
  • 1 \\leq T \\leq 10000
  • \n
  • 1 \\leq S \\leq 10000
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
D T S\n
\n
\n
\n
\n
\n

Output

If Takahashi will reach the place in time, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1000 15 80\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

It takes 12.5 minutes to go 1000 meters to the place at a speed of 80 meters per minute. They have planned to meet in 15 minutes so he will arrive in time.

\n
\n
\n
\n
\n
\n

Sample Input 2

2000 20 100\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n

It takes 20 minutes to go 2000 meters to the place at a speed of 100 meters per minute. They have planned to meet in 20 minutes so he will arrive just on time.

\n
\n
\n
\n
\n
\n

Sample Input 3

10000 1 1\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n

He will be late.

\n
\n
", "c_code": "int solution(void) {\n int D = 0;\n int T = 0;\n int S = 0;\n scanf(\"%d %d %d\", &D, &T, &S);\n if (S * T < D) {\n printf(\"No\");\n } else if (S * T >= D) {\n printf(\"Yes\");\n }\n}", "rust_code": "fn solution() -> Result<()> {\n let mut s = String::new();\n let _ls = io::stdin().read_line(&mut s)? - 1;\n let input: Vec = s\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n if input[1] * input[2] >= input[0] {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1290", "problem_description": "\n

Score : 900 points

\n
\n
\n

Problem Statement

Takahashi has decided to give a string to his mother.

\n

The value of a string T is the length of the longest common subsequence of T and T', where T' is the string obtained by reversing T.\nThat is, the value is the longest length of the following two strings that are equal: a subsequence of T (possibly non-contiguous), and a subsequence of T' (possibly non-contiguous).

\n

Takahashi has a string S. He wants to give her mother a string of the highest possible value, so he would like to change at most K characters in S to any other characters in order to obtain a string of the highest possible value.\nFind the highest possible value achievable.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 300
  • \n
  • 0 \\leq K \\leq |S|
  • \n
  • S consists of lowercase English letters.
  • \n
  • K is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nK\n
\n
\n
\n
\n
\n

Output

Print the highest possible value achievable.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

abcabcabc\n1\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

Changing the first character to c results in cbcabcabc.\nLet this tring be T, then one longest common subsequence of T and T' is cbabcbc, whose length is 7.

\n
\n
\n
\n
\n
\n

Sample Input 2

atcodergrandcontest\n3\n
\n
\n
\n
\n
\n

Sample Output 2

15\n
\n
\n
", "c_code": "int solution() {\n int K;\n char S[301];\n scanf(\"%s\", S);\n scanf(\"%d\", &K);\n\n int i;\n int l;\n char T[301];\n for (l = 0; S[l] != 0; l++) {\n ;\n }\n for (i = 0, T[l] = 0; i < l; i++) {\n T[i] = S[l - i - 1];\n }\n if (K >= l / 2) {\n printf(\"%d\\n\", l);\n fflush(stdout);\n return 0;\n }\n\n int j;\n int k;\n int dp[151][301][301] = {};\n for (i = 1; i <= l; i++) {\n for (j = 1; j <= l; j++) {\n for (k = 0; k <= K; k++) {\n if (dp[k][i - 1][j] > dp[k][i][j]) {\n dp[k][i][j] = dp[k][i - 1][j];\n }\n if (dp[k][i][j - 1] > dp[k][i][j]) {\n dp[k][i][j] = dp[k][i][j - 1];\n }\n if (S[i - 1] == T[j - 1] && dp[k][i - 1][j - 1] >= dp[k][i][j]) {\n dp[k][i][j] = dp[k][i - 1][j - 1] + 1;\n }\n if (dp[k][i - 1][j - 1] >= dp[k + 1][i][j]) {\n dp[k + 1][i][j] = dp[k][i - 1][j - 1] + 1;\n }\n }\n }\n }\n\n int ans = 0;\n for (i = 0; i <= l - 1; i++) {\n for (k = 0; k <= K; k++) {\n if (dp[k][i][l - i] * 2 > ans) {\n ans = dp[k][i][l - i] * 2;\n }\n if (dp[k][i][l - i - 1] * 2 + 1 > ans) {\n ans = dp[k][i][l - i - 1] * 2 + 1;\n }\n }\n }\n printf(\"%d\\n\", ans);\n fflush(stdout);\n return 0;\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 s = it.next().unwrap().as_bytes();\n let n = s.len();\n let k = it.next().unwrap().parse::().unwrap();\n let mut dp = vec![vec![vec![0; n]; n]; k + 1];\n for d in (1..n).rev() {\n for i in 0..k + 1 {\n for j in 0..n - d {\n dp[i][j + 1][j + d] = std::cmp::max(dp[i][j + 1][j + d], dp[i][j][j + d]);\n dp[i][j][j + d - 1] = std::cmp::max(dp[i][j][j + d - 1], dp[i][j][j + d]);\n let next_i = i + if s[j] == s[j + d] { 0 } else { 1 };\n if next_i > k {\n continue;\n }\n dp[next_i][j + 1][j + d - 1] =\n std::cmp::max(dp[next_i][j + 1][j + d - 1], dp[i][j][j + d] + 2);\n }\n }\n }\n let mut res = 0;\n for i in 0..k + 1 {\n for j in 0..n {\n for l in 0..n {\n res = std::cmp::max(res, dp[i][j][l] + if j == l { 1 } else { 0 });\n }\n }\n }\n println!(\"{}\", res);\n}", "difficulty": "medium"} {"problem_id": "1291", "problem_description": "William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands.A valid nested list is any list which can be created from a list with one item \"1\" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $$$a_1 \\,.\\, a_2 \\,.\\, a_3 \\,.\\, \\,\\cdots\\, \\,.\\,a_k$$$ and can be one of two types: Add an item $$$a_1 \\,.\\, a_2 \\,.\\, a_3 \\,.\\, \\cdots \\,.\\, a_k \\,.\\, 1$$$ (starting a list of a deeper level), or Add an item $$$a_1 \\,.\\, a_2 \\,.\\, a_3 \\,.\\, \\cdots \\,.\\, (a_k + 1)$$$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the \"Notes\" section.When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the \"Ctrl-S\" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number.William wants you to help him restore a fitting original nested list.", "c_code": "int solution(int argc, const char *argv[]) {\n int tests;\n scanf(\"%d\", &tests);\n while (tests--) {\n int n;\n static int a[1005];\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d\", a + i);\n }\n static int stk[1005];\n int top = 0;\n for (int i = 1; i <= n; ++i) {\n if (a[i] == 1) {\n stk[++top] = 1;\n } else {\n for (; top != 0 && stk[top] != a[i] - 1; --top) {\n ;\n }\n stk[top += !top] = a[i];\n }\n for (int j = 1; j <= top; ++j) {\n printf(\"%d%c\", stk[j], \".\\n\"[j == top]);\n }\n }\n }\n exit(EXIT_SUCCESS);\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 t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let n = it.next().unwrap();\n\n let _it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let mut stack = Vec::new();\n\n for _ in 0..n {\n let x: usize = lines.next().unwrap().parse().unwrap();\n\n if x == 1 {\n stack.push(1);\n } else {\n while *stack.last().unwrap() != x - 1 {\n stack.pop();\n }\n *stack.last_mut().unwrap() += 1;\n }\n\n let a = *stack.first().unwrap();\n write!(&mut output, \"{}\", a).unwrap();\n for &a in &stack[1..] {\n write!(&mut output, \".{}\", a).unwrap();\n }\n writeln!(&mut output).unwrap();\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1292", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.

\n

Shift each character of S by N in alphabetical order (see below), and print the resulting string.

\n

We assume that A follows Z. For example, shifting A by 2 results in C (A \\to B \\to C), and shifting Y by 3 results in B (Y \\to Z \\to A \\to B).

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq N \\leq 26
  • \n
  • 1 \\leq |S| \\leq 10^4
  • \n
  • S consists of uppercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the string resulting from shifting each character of S by N in alphabetical order.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\nABCXYZ\n
\n
\n
\n
\n
\n

Sample Output 1

CDEZAB\n
\n

Note that A follows Z.

\n
\n
\n
\n
\n
\n

Sample Input 2

0\nABCXYZ\n
\n
\n
\n
\n
\n

Sample Output 2

ABCXYZ\n
\n
\n
\n
\n
\n
\n

Sample Input 3

13\nABCDEFGHIJKLMNOPQRSTUVWXYZ\n
\n
\n
\n
\n
\n

Sample Output 3

NOPQRSTUVWXYZABCDEFGHIJKLM\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n char s[10240];\n int i = 0;\n scanf(\"%d\", &n);\n scanf(\"%s\", s);\n\n for (i = 0; i < 10240; i++) {\n if (s[i] == '\\0') {\n break;\n }\n s[i] = (s[i] - 'A' + n) % 26 + 'A';\n }\n\n printf(\"%s\\n\", s);\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 let n: u8 = iter.next().unwrap().parse().unwrap();\n let s = iter.next().unwrap();\n for c in s.bytes() {\n print!(\"{}\", (((c - b'A') + n) % 26 + b'A') as char)\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "1293", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Snuke built an online judge to hold a programming contest.

\n

When a program is submitted to the judge, the judge returns a verdict, which is a two-character string that appears in the string S as a contiguous substring.\n(The judge can return any two-character substring of S.)

\n

Determine whether the judge can return the string AC as the verdict to a program.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq |S| \\leq 5
  • \n
  • S consists of uppercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If the judge can return the string AC as a verdict to a program, print Yes; if it cannot, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

BACD\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The string AC appears in BACD as a contiguous substring (the second and third characters).

\n
\n
\n
\n
\n
\n

Sample Input 2

ABCD\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

Although the string ABCD contains both A and C (the first and third characters), the string AC does not appear in ABCD as a contiguous substring.

\n
\n
\n
\n
\n
\n

Sample Input 3

CABD\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
\n
\n
\n
\n

Sample Input 4

ACACA\n
\n
\n
\n
\n
\n

Sample Output 4

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 5

XX\n
\n
\n
\n
\n
\n

Sample Output 5

No\n
\n
\n
", "c_code": "int solution(void) {\n char s[6];\n scanf(\"%s\", s);\n int ans = 0;\n int p = 0;\n while (ans == 0 && p < 6) {\n if (s[p] == 0x41 && s[p + 1] == 0x43) {\n printf(\"Yes\");\n ans = 1;\n }\n p++;\n }\n if (ans == 0) {\n printf(\"No\");\n }\n}", "rust_code": "fn solution() {\n let S: String = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n };\n\n let ans = if S.find(\"AC\").is_some() { \"Yes\" } else { \"No\" };\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1294", "problem_description": "The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,\\ldots,a_n$$$.Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$|x-y|\\le 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$.Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes.", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n long long k = 0LL;\n long long a[200000] = {};\n\n int res = 0;\n\n long long sum[200001] = {};\n\n res = scanf(\"%d\", &t);\n while (t > 0) {\n long long ans = 0LL;\n res = scanf(\"%d\", &n);\n res = scanf(\"%lld\", &k);\n for (int i = 0; i < n; i++) {\n res = scanf(\"%lld\", a + i);\n sum[i + 1] = sum[i] + a[i];\n }\n if (k < ((long long)n)) {\n for (int i = (int)k; i <= n; i++) {\n long long tmp = sum[i] - sum[i - ((int)k)];\n if (tmp > ans) {\n ans = tmp;\n }\n }\n ans += (k * (k - 1LL)) / 2LL;\n } else {\n ans = sum[n] + k * ((long long)n);\n ans -= (((long long)n) * ((long long)(n + 1))) / 2LL;\n }\n printf(\"%lld\\n\", ans);\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut content = String::new();\n io::stdin().read_to_string(&mut content);\n\n let mut lines = content.lines();\n let num_tests: i32 = lines.next().unwrap().parse().unwrap();\n for _ in 0..num_tests {\n let arr: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse().unwrap())\n .collect();\n let n = arr[0];\n let k = arr[1];\n let a: Vec = lines\n .next()\n .unwrap()\n .split(\" \")\n .map(|token| token.parse().unwrap())\n .collect();\n\n if k < n {\n let mut prefix: Vec = vec![0; a.len() + 1];\n prefix[0] = 0;\n for i in 1..=a.len() {\n prefix[i] = prefix[i - 1] + a[i - 1];\n }\n let mut max_sum = 0;\n let mut max_idx = 0;\n for i in 0..=a.len() - k as usize {\n if prefix[i + k as usize] - prefix[i] > max_sum {\n max_sum = prefix[i + k as usize] - prefix[i];\n max_idx = i;\n }\n }\n let mut result = k * (k - 1) / 2;\n for &x in &a[max_idx..max_idx + k as usize] {\n result += x;\n }\n println!(\"{result}\");\n } else {\n let mut result = k * (k - 1) / 2 - (k - n) * (k - n - 1) / 2;\n for &x in &a {\n result += x;\n }\n println!(\"{result}\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1295", "problem_description": "In the snake exhibition, there are $$$n$$$ rooms (numbered $$$0$$$ to $$$n - 1$$$) arranged in a circle, with a snake in each room. The rooms are connected by $$$n$$$ conveyor belts, and the $$$i$$$-th conveyor belt connects the rooms $$$i$$$ and $$$(i+1) \\bmod n$$$. In the other words, rooms $$$0$$$ and $$$1$$$, $$$1$$$ and $$$2$$$, $$$\\ldots$$$, $$$n-2$$$ and $$$n-1$$$, $$$n-1$$$ and $$$0$$$ are connected with conveyor belts.The $$$i$$$-th conveyor belt is in one of three states: If it is clockwise, snakes can only go from room $$$i$$$ to $$$(i+1) \\bmod n$$$. If it is anticlockwise, snakes can only go from room $$$(i+1) \\bmod n$$$ to $$$i$$$. If it is off, snakes can travel in either direction. Above is an example with $$$4$$$ rooms, where belts $$$0$$$ and $$$3$$$ are off, $$$1$$$ is clockwise, and $$$2$$$ is anticlockwise.Each snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there?", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n\n for (int C = 0; C < T; C++) {\n int n;\n scanf(\"%d\", &n);\n char s[300005];\n scanf(\"%s\", s);\n\n int ans = 0;\n int clkf = 0;\n int aclkf = 0;\n\n for (int i = 0; i < n; i++) {\n if (s[i] == '>') {\n clkf = 1;\n }\n if (s[i] == '<') {\n aclkf = 1;\n }\n }\n\n if (s[n - 1] == '-' || s[0] == '-' ||\n (s[n - 1] == '>' && s[0] == '>' && aclkf == 0) ||\n (s[n - 1] == '<' && s[0] == '<' && clkf == 0)) {\n ans++;\n }\n\n for (int i = 1; i < n; i++) {\n if (s[i - 1] == '-' || s[i] == '-' ||\n (s[i - 1] == '>' && s[i] == '>' && aclkf == 0) ||\n (s[i - 1] == '<' && s[i] == '<' && clkf == 0)) {\n ans++;\n }\n }\n\n printf(\"%d\\n\", ans);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let test: i64 = str.trim().parse().unwrap();\n for _ in 0..test {\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let n: usize = str.trim().len();\n let mut l = true;\n let mut r = true;\n let mut acc = 0;\n let s: Vec = str.trim().chars().collect();\n for i in 0..n {\n if s[i] == '-' || s[(i + 1) % n] == '-' {\n acc += 1;\n }\n if s[i] == '>' {\n l = false;\n }\n if s[i] == '<' {\n r = false;\n }\n }\n if l || r {\n println!(\"{}\", n);\n } else {\n println!(\"{}\", acc);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1296", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Fennec is fighting with N monsters.

\n

The health of the i-th monster is H_i.

\n

Fennec can do the following two actions:

\n
    \n
  • Attack: Fennec chooses one monster. That monster's health will decrease by 1.
  • \n
  • Special Move: Fennec chooses one monster. That monster's health will become 0.
  • \n
\n

There is no way other than Attack and Special Move to decrease the monsters' health.

\n

Fennec wins when all the monsters' healths become 0 or below.

\n

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • 0 \\leq K \\leq 2 \\times 10^5
  • \n
  • 1 \\leq H_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nH_1 ... H_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1\n4 1 5\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

\n
\n
\n
\n
\n
\n

Sample Input 2

8 9\n7 9 3 2 3 8 4 6\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

She can use Special Move on all the monsters.

\n
\n
\n
\n
\n
\n

Sample Input 3

3 0\n1000000000 1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

3000000000\n
\n

Watch out for overflow.

\n
\n
", "c_code": "int solution() {\n int n;\n int j;\n int temp;\n int prev;\n int k;\n int data[200005];\n int order[200005][2];\n int i;\n int f = 1;\n int b;\n int start = 0;\n int s;\n int a;\n long m = 0;\n scanf(\"%d\", &n);\n scanf(\"%d\", &s);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &data[i]);\n order[i][0] = -1;\n order[i][1] = i + 1;\n }\n order[n - 1][1] = -1;\n while (order[start][1] != -1) {\n i = start;\n prev = -1;\n while (i != -1) {\n j = i;\n k = order[j][1];\n if (k == -1) {\n break;\n }\n i = order[k][1];\n if (data[j] < data[k]) {\n if (prev == -1) {\n start = k;\n } else {\n order[prev][1] = k;\n }\n temp = j;\n j = k;\n k = temp;\n }\n order[j][1] = i;\n prev = j;\n while (k != -1) {\n if ((j == -1) | (data[j] < data[k])) {\n temp = order[k][0];\n order[k][0] = order[a][0];\n order[a][0] = k;\n a = k;\n k = temp;\n } else {\n a = j;\n j = order[a][0];\n }\n }\n }\n }\n for (i = start, j = 0; i != -1; i = order[i][0], j++) {\n if (j >= s) {\n m += data[i];\n }\n }\n printf(\"%ld\", m);\n return 0;\n}", "rust_code": "fn solution() {\n let (n, k): (usize, usize) = {\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().parse().unwrap(),\n )\n };\n let mut h = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n input\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>()\n };\n h.sort_by(|a, b| b.cmp(a));\n if n <= k {\n println!(\"0\");\n } else {\n println!(\"{}\", h.iter().skip(k).sum::());\n }\n}", "difficulty": "hard"} {"problem_id": "1297", "problem_description": "Pak Chanek has $$$n$$$ two-dimensional slices of cheese. The $$$i$$$-th slice of cheese can be represented as a rectangle of dimensions $$$a_i \\times b_i$$$. We want to arrange them on the two-dimensional plane such that: Each edge of each cheese is parallel to either the x-axis or the y-axis. The bottom edge of each cheese is a segment of the x-axis. No two slices of cheese overlap, but their sides can touch. They form one connected shape. Note that we can arrange them in any order (the leftmost slice of cheese is not necessarily the first slice of cheese). Also note that we can rotate each slice of cheese in any way as long as all conditions still hold.Find the minimum possible perimeter of the constructed shape.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long int small = 0;\n long long int big = 0;\n\n long long int n;\n scanf(\"%lld\", &n);\n\n long long int a;\n long long int b;\n\n while (n--) {\n scanf(\"%lld %lld\", &a, &b);\n small += a > b ? b : a;\n if (a > big) {\n big = a;\n }\n if (b > big) {\n big = b;\n }\n }\n printf(\"%lld\\n\", ((small * 2) + (big * 2)));\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n\n let mut reader = Scanner::new(stdin.lock());\n let mut writer = io::BufWriter::new(stdout.lock());\n\n let tests_number = reader.token::();\n for _ in 0..tests_number {\n let slices = reader.token::();\n let mut l = 0;\n let mut h = 0;\n for _ in 0..slices {\n let n = reader.token::();\n let m = reader.token::();\n let a_i = n.max(m);\n let b_i = n.min(m);\n\n l += b_i;\n h = h.max(a_i);\n }\n let perimeter = 2 * l + 2 * h;\n writeln!(writer, \"{}\", perimeter).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1298", "problem_description": "Ternary search", "c_code": "int solution(int l, int r, int key, int ar[]) {\n if (r >= l) {\n\n int mid1 = l + ((r - l) / 3);\n int mid2 = r - ((r - l) / 3);\n\n if (ar[mid1] == key) {\n return mid1;\n }\n if (ar[mid2] == key) {\n return mid2;\n }\n\n if (key < ar[mid1]) {\n\n return ternarySearch(l, mid1 - 1, key, ar);\n }\n if (key > ar[mid2]) {\n\n return ternarySearch(mid2 + 1, r, key, ar);\n } else {\n\n return ternarySearch(mid1 + 1, mid2 - 1, key, ar);\n }\n }\n\n return -1;\n}\n", "rust_code": "fn solution(item: &T, arr: &[T]) -> Option {\n if arr.is_empty() {\n return None;\n }\n\n let is_asc = arr.len() > 1 && arr[0] < arr[arr.len() - 1];\n\n let mut left = 0;\n let mut right = arr.len() - 1;\n\n while left <= right {\n let first_mid = left + (right - left) / 3;\n let second_mid = right - (right - left) / 3;\n\n if first_mid == second_mid && first_mid == left {\n if &arr[left] == item {\n return Some(left);\n } else {\n left += 1;\n continue;\n }\n }\n\n let cmp_first_mid = item.cmp(&arr[first_mid]);\n let cmp_second_mid = item.cmp(&arr[second_mid]);\n\n match (is_asc, cmp_first_mid, cmp_second_mid) {\n (_, Ordering::Equal, _) => return Some(first_mid),\n (_, _, Ordering::Equal) => return Some(second_mid),\n\n (true, Ordering::Less, _) | (false, Ordering::Greater, _) => {\n if first_mid == 0 {\n break;\n }\n right = first_mid - 1;\n }\n\n (true, _, Ordering::Greater) | (false, _, Ordering::Less) => {\n left = second_mid + 1;\n }\n\n (_, _, _) => {\n left = first_mid + 1;\n if second_mid == 0 {\n break;\n }\n right = second_mid - 1;\n }\n }\n }\n\n None\n}\n", "difficulty": "medium"} {"problem_id": "1299", "problem_description": "You have a board represented as a grid with $$$2 \\times n$$$ cells.The first $$$k_1$$$ cells on the first row and first $$$k_2$$$ cells on the second row are colored in white. All other cells are colored in black.You have $$$w$$$ white dominoes ($$$2 \\times 1$$$ tiles, both cells are colored in white) and $$$b$$$ black dominoes ($$$2 \\times 1$$$ tiles, both cells are colored in black).You can place a white domino on the board if both board's cells are white and not occupied by any other domino. In the same way, you can place a black domino if both cells are black and not occupied by any other domino.Can you place all $$$w + b$$$ dominoes on the board if you can place dominoes both horizontally and vertically?", "c_code": "int solution() {\n int T = 0;\n scanf(\"%d\", &T);\n for (int i = T; i--;) {\n int n;\n int k1;\n int k2;\n scanf(\"%d %d %d\", &n, &k1, &k2);\n int w;\n int b;\n scanf(\"%d %d\", &w, &b);\n\n if (w == 0 && b == 0) {\n printf(\"YES\\n\");\n continue;\n }\n\n if (w * 2 <= (k1 + k2) && b * 2 <= 2 * n - (k1 + k2)) {\n printf(\"YES\\n\");\n continue;\n }\n printf(\"NO\\n\");\n continue;\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let (n, k1, k2) = scan!(usize, usize, usize);\n let (w, b) = scan!(usize, usize);\n\n let solve = |k1: usize, k2, x: usize| -> bool {\n let (k1, k2) = (k1.min(k2), k1.max(k2));\n x <= k1 + (k2 - k1) / 2\n };\n\n if solve(k1, k2, w) && solve(n - k1, n - k2, b) {\n print!(\"YES\");\n } else {\n print!(\"NO\");\n }\n print!(\"\\n\");\n }\n}", "difficulty": "easy"} {"problem_id": "1300", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N candles placed on a number line.\nThe i-th candle from the left is placed on coordinate x_i.\nHere, x_1 < x_2 < ... < x_N holds.

\n

Initially, no candles are burning.\nSnuke decides to light K of the N candles.

\n

Now, he is at coordinate 0.\nHe can move left and right along the line with speed 1.\nHe can also light a candle when he is at the same position as the candle, in negligible time.

\n

Find the minimum time required to light K candles.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq K \\leq N
  • \n
  • x_i is an integer.
  • \n
  • |x_i| \\leq 10^8
  • \n
  • x_1 < x_2 < ... < x_N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nx_1 x_2 ... x_N\n
\n
\n
\n
\n
\n

Output

Print the minimum time required to light K candles.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n-30 -10 10 20 50\n
\n
\n
\n
\n
\n

Sample Output 1

40\n
\n

He should move and light candles as follows:

\n
    \n
  • Move from coordinate 0 to -10.
  • \n
  • Light the second candle from the left.
  • \n
  • Move from coordinate -10 to 10.
  • \n
  • Light the third candle from the left.
  • \n
  • Move from coordinate 10 to 20.
  • \n
  • Light the fourth candle from the left.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 2\n10 20 30\n
\n
\n
\n
\n
\n

Sample Output 2

20\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 1\n0\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
    \n
  • There may be a candle placed at coordinate 0.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 4

8 5\n-9 -7 -4 -3 1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 4

10\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n long long x[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &x[i]);\n }\n\n long long tmp;\n long long ans = LLONG_MAX;\n for (int i = 0; i + k - 1 < n; i++) {\n\n if (x[i] * x[i + k - 1] >= 0) {\n tmp = fmax(llabs(x[i]), llabs(x[i + k - 1]));\n\n } else if (x[i] * x[i + k - 1] < 0) {\n if (x[i] * -2 + x[i + k - 1] > x[i] * -1 + x[i + k - 1] * 2) {\n tmp = x[i] * -1 + x[i + k - 1] * 2;\n } else {\n tmp = x[i] * -2 + x[i + k - 1];\n }\n }\n\n if (ans > tmp) {\n ans = tmp;\n }\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut nk = String::new();\n std::io::stdin().read_line(&mut nk).unwrap();\n let nk: Vec = nk.split_whitespace().flat_map(str::parse).collect();\n let n = nk[0];\n let k = nk[1];\n\n let mut x = String::new();\n std::io::stdin().read_line(&mut x).unwrap();\n let x: Vec = x.split_whitespace().flat_map(str::parse).collect();\n\n println!(\n \"{}\",\n (0..(n - k + 1))\n .map(|s| {\n let start = x[s];\n let end = x[s + k - 1];\n if start >= 0 {\n end\n } else if end > 0 {\n let a = -start * 2 + end;\n let b = -start + end * 2;\n if a < b {\n a\n } else {\n b\n }\n } else {\n -start\n }\n })\n .min()\n .unwrap()\n );\n}", "difficulty": "medium"} {"problem_id": "1301", "problem_description": "The only difference between the two versions is that this version asks the maximal possible answer.Homer likes arrays a lot. Today he is painting an array $$$a_1, a_2, \\dots, a_n$$$ with two kinds of colors, white and black. A painting assignment for $$$a_1, a_2, \\dots, a_n$$$ is described by an array $$$b_1, b_2, \\dots, b_n$$$ that $$$b_i$$$ indicates the color of $$$a_i$$$ ($$$0$$$ for white and $$$1$$$ for black).According to a painting assignment $$$b_1, b_2, \\dots, b_n$$$, the array $$$a$$$ is split into two new arrays $$$a^{(0)}$$$ and $$$a^{(1)}$$$, where $$$a^{(0)}$$$ is the sub-sequence of all white elements in $$$a$$$ and $$$a^{(1)}$$$ is the sub-sequence of all black elements in $$$a$$$. For example, if $$$a = [1,2,3,4,5,6]$$$ and $$$b = [0,1,0,1,0,0]$$$, then $$$a^{(0)} = [1,3,5,6]$$$ and $$$a^{(1)} = [2,4]$$$.The number of segments in an array $$$c_1, c_2, \\dots, c_k$$$, denoted $$$\\mathit{seg}(c)$$$, is the number of elements if we merge all adjacent elements with the same value in $$$c$$$. For example, the number of segments in $$$[1,1,2,2,3,3,3,2]$$$ is $$$4$$$, because the array will become $$$[1,2,3,2]$$$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $$$0$$$.Homer wants to find a painting assignment $$$b$$$, according to which the number of segments in both $$$a^{(0)}$$$ and $$$a^{(1)}$$$, i.e. $$$\\mathit{seg}(a^{(0)})+\\mathit{seg}(a^{(1)})$$$, is as large as possible. Find this number.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int last1 = -1;\n int last2 = -1;\n int ans = n;\n for (int i = 0; i < n - 1; i++) {\n if (a[i] != last1 && a[i] != last2) {\n if (a[i + 1] == last1) {\n last1 = a[i];\n } else {\n last2 = a[i];\n }\n } else if (a[i] != last1 && a[i] == last2) {\n last1 = a[i];\n } else if (a[i] == last1 && a[i] != last2) {\n last2 = a[i];\n } else {\n last1 = a[i];\n ans -= 1;\n }\n }\n if (last1 == a[n - 1] && last2 == a[n - 1]) {\n ans -= 1;\n }\n printf(\"%d\", ans);\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 n: usize = sc.next();\n let mut array: Vec = vec![0; n + 1];\n for i in 1..=n {\n array[i] = sc.next();\n }\n let mut next: Vec = vec![n + 1; n + 1];\n let mut dp: Vec = vec![n + 1; n + 1];\n for i in (1..=n).rev() {\n next[i] = dp[array[i]];\n dp[array[i]] = i;\n }\n let (mut l, mut r, mut ans) = (0, 0, 0);\n for i in 1..=n {\n if array[i] == array[l] {\n ans += (array[i] != array[r]) as i32;\n r = i;\n } else if array[i] == array[r] {\n ans += (array[i] != array[l]) as i32;\n l = i;\n } else if next[l] < next[r] {\n ans += (array[l] != array[i]) as i32;\n l = i;\n } else {\n ans += (array[r] != array[i]) as i32;\n r = i;\n }\n }\n writeln!(out, \"{}\", ans).unwrap();\n}", "difficulty": "hard"} {"problem_id": "1302", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

\n

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

\n

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1\\leq A,B,C,D \\leq 10
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C D\n
\n
\n
\n
\n
\n

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 8 7 1\n
\n
\n
\n
\n
\n

Sample Output 1

Left\n
\n

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 4 5 2\n
\n
\n
\n
\n
\n

Sample Output 2

Balanced\n
\n

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 7 6 4\n
\n
\n
\n
\n
\n

Sample Output 3

Right\n
\n

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.

\n
\n
", "c_code": "int solution() {\n\n int A = 0;\n int B = 0;\n int C = 0;\n int D = 0;\n scanf(\"%d %d %d %d\\n\", &A, &B, &C, &D);\n\n if (A + B > C + D) {\n printf(\"Left\");\n } else if (A + B == C + D) {\n printf(\"Balanced\");\n } else {\n printf(\"Right\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n\n let q: Vec = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n match (q[0] + q[1]).cmp(&(q[2] + q[3])) {\n Ordering::Greater => println!(\"Left\"),\n Ordering::Equal => println!(\"Balanced\"),\n Ordering::Less => println!(\"Right\"),\n }\n}", "difficulty": "easy"} {"problem_id": "1303", "problem_description": "Two T-shirt sizes are given: $$$a$$$ and $$$b$$$. The T-shirt size is either a string M or a string consisting of several (possibly zero) characters X and one of the characters S or L.For example, strings M, XXL, S, XXXXXXXS could be the size of some T-shirts. And the strings XM, LL, SX are not sizes.The letter M stands for medium, S for small, L for large. The letter X refers to the degree of size (from eXtra). For example, XXL is extra-extra-large (bigger than XL, and smaller than XXXL).You need to compare two given sizes of T-shirts $$$a$$$ and $$$b$$$.The T-shirts are compared as follows: any small size (no matter how many letters X) is smaller than the medium size and any large size; any large size (regardless of the number of letters X) is larger than the medium size and any small size; the more letters X before S, the smaller the size; the more letters X in front of L, the larger the size. For example: XXXS < XS XXXL > XL XL > M XXL = XXL XXXXXS < M XL > XXXS", "c_code": "int solution(void) {\n\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n char a[51];\n char b[51];\n\n scanf(\"%s %s\", a, b);\n\n if (((a[0] == 'L' || a[strlen(a) - 1] == 'L') && b[0] == 'M') ||\n (a[0] == 'M' && (b[0] == 'S' || b[strlen(b) - 1] == 'S')) ||\n (a[strlen(a) - 1] == 'L' && b[strlen(b) - 1] == 'S') ||\n (a[strlen(a) - 1] == 'L' && b[strlen(b) - 1] == 'L' &&\n strcmp(a, b) > 0) ||\n (a[strlen(a) - 1] == 'S' && b[strlen(b) - 1] == 'S' &&\n strcmp(a, b) < 0)) {\n printf(\">\\n\");\n\n } else if (!(strcmp(a, b))) {\n printf(\"=\\n\");\n\n } else {\n printf(\"<\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n for _ in 0..t {\n let (a, b): (Vec, Vec) = {\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().chars().collect(),\n iter.next().unwrap().chars().collect(),\n )\n };\n\n if a == b {\n println!(\"=\");\n continue;\n }\n\n if a[a.len() - 1] == 'S' {\n if b[b.len() - 1] == 'S' {\n if a.len() > b.len() {\n println!(\"<\");\n } else {\n println!(\">\");\n }\n } else {\n println!(\"<\")\n }\n } else if a[a.len() - 1] == 'L' {\n if b[b.len() - 1] == 'L' {\n if a.len() > b.len() {\n println!(\">\");\n } else {\n println!(\"<\");\n }\n } else {\n println!(\">\")\n }\n } else {\n if b[b.len() - 1] == 'L' {\n println!(\"<\");\n } else {\n println!(\">\")\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1304", "problem_description": "\n

Score: 800 points

\n
\n
\n

Problem Statement

\n

This is an interactive task.

\n

We have 2N balls arranged in a row, numbered 1, 2, 3, ..., 2N from left to right, where N is an odd number. Among them, there are N red balls and N blue balls.

\n

While blindfolded, you are challenged to guess the color of every ball correctly, by asking at most 210 questions of the following form:

\n
    \n
  • You choose any N of the 2N balls and ask whether there are more red balls than blue balls or not among those N balls.
  • \n
\n

Now, let us begin.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 99
  • \n
  • N is an odd number.
  • \n
\n
\n
\n
\n
\n
\n

Input and Output

\n

First, receive the number of balls of each color, N, from Standard Input:

\n
N\n
\n

Then, ask questions until you find out the color of every ball.\nA question should be printed to Standard Output in the following format:

\n
? A_1 A_2 A_3 ... A_N\n
\n

This means that you are asking about the N balls A_1, A_2, A_3, ..., A_N, where 1 \\leq A_i \\leq 2N and A_i \\neq A_j (i \\neq j) must hold.

\n

The response T to this question will be given from Standard Input in the following format:

\n
T\n
\n

Here T is one of the following strings:

\n
    \n
  • Red: Among the N balls chosen, there are more red balls than blue balls.
  • \n
  • Blue: Among the N balls chosen, there are more blue balls than red balls.
  • \n
  • -1: You printed an invalid question (including the case you asked more than 210 questions), or something else that was invalid.
  • \n
\n

If the judge returns -1, your submission is already judged as incorrect. The program should immediately quit in this case.

\n

When you find out the color of every ball, print your guess to Standard Output in the following format:

\n
! c_1c_2c_3...c_{2N}\n
\n

Here c_i should be the character representing the color of Ball i; use R for red and B for blue.

\n
\n
\n
\n
\n

Notice

\n
    \n
  • Flush Standard Output each time you print something. Failure to do so may result in TLE.
  • \n
  • Immediately terminate your program after printing your guess (or receiving the -1 response). Otherwise, the verdict will be indeterminate.
  • \n
  • If your program prints something invalid, the verdict will be indeterminate.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input and Output

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
InputOutput
3
? 1 2 3
Red
? 2 4 6
Blue
! RRBBRB
\n

In this sample, N = 3, and the colors of Ball 1, 2, 3, 4, 5, 6 are red, red, blue, blue, red, blue, respectively.

\n
    \n
  • In the first question, there are two red balls and one blue ball among the balls 1, 2, 3, so the judge returns Red.
  • \n
  • In the second question, there are one red ball and two blue balls among the balls 2, 4, 6, so the judge returns Blue.
  • \n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n char ans[102];\n int i;\n int j;\n for (i = 0; i < 2 * n; i++) {\n ans[i] = '?';\n }\n int r;\n int b;\n int c;\n int min;\n int mid;\n int max;\n char minc;\n char res[16];\n printf(\"?\");\n for (i = 0; i < n; i++) {\n printf(\" %d\", i + 1);\n }\n printf(\"\\n\");\n fflush(stdout);\n scanf(\"%s\", res);\n minc = res[0];\n min = 0;\n max = n;\n while (max - min > 1) {\n mid = (max + min) / 2;\n printf(\"?\");\n for (i = 0; i < n; i++) {\n printf(\" %d\", i + mid + 1);\n }\n printf(\"\\n\");\n fflush(stdout);\n scanf(\"%s\", res);\n if (res[0] == minc) {\n min = mid;\n } else {\n max = mid;\n }\n }\n ans[min] = minc;\n if (minc == 'R') {\n ans[max + n - 1] = 'B';\n } else {\n ans[max + n - 1] = 'R';\n }\n for (j = max; j < min + n; j++) {\n printf(\"?\");\n for (i = min; i < max + n; i++) {\n if (i != j) {\n printf(\" %d\", i + 1);\n }\n }\n printf(\"\\n\");\n fflush(stdout);\n scanf(\"%s\", res);\n if (res[0] == 'B') {\n ans[j] = 'R';\n } else {\n ans[j] = 'B';\n }\n }\n for (;;) {\n c = -1;\n for (i = 0; i < 2 * n; i++) {\n if (ans[i] == '?') {\n c = i;\n }\n }\n if (c < 0) {\n break;\n }\n printf(\"?\");\n for (i = max; i < min + n; i++) {\n printf(\" %d\", i + 1);\n }\n printf(\" %d\\n\", c + 1);\n fflush(stdout);\n scanf(\"%s\", res);\n if (res[0] == 'B') {\n ans[c] = 'B';\n } else {\n ans[c] = 'R';\n }\n }\n printf(\"! \");\n for (i = 0; i < 2 * n; i++) {\n printf(\"%c\", ans[i]);\n }\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\n println!(\n \"? {}\",\n (1..(N + 1))\n .map(|i| i.to_string())\n .collect::>()\n .join(\" \")\n );\n let s: String = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().to_string()\n };\n\n let mut ok = N + 1;\n let mut ng = 1;\n\n while ok - ng > 1 {\n let mid = (ok + ng) / 2;\n let query = (mid..(mid + N))\n .map(|i| i.to_string())\n .collect::>()\n .join(\" \");\n println!(\"? {}\", query);\n let T: String = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().to_string()\n };\n if T == s {\n ng = mid;\n } else {\n ok = mid;\n }\n }\n let mut q = vec![0; N];\n for i in 0..(N - 1) {\n q[i] = ok + i;\n }\n\n let mut res = vec!['?'; 2 * N];\n for i in 1..(2 * N + 1) {\n if !q.contains(&i) {\n q[N - 1] = i;\n let query = q\n .iter()\n .map(|&x| x.to_string())\n .collect::>()\n .join(\" \");\n println!(\"? {}\", query);\n let T: String = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().to_string()\n };\n res[i - 1] = if T.as_str() == \"Red\" { 'R' } else { 'B' };\n }\n }\n let mut r = 0;\n let mut b = 0;\n for i in 0..(2 * N) {\n if res[i] == 'R' && r < N / 2 {\n q[r + b] = i + 1;\n r += 1;\n } else if res[i] == 'B' && b < N / 2 {\n q[r + b] = i + 1;\n b += 1;\n }\n }\n for i in ok..(ok + N - 1) {\n q[N - 1] = i;\n let query = q\n .iter()\n .map(|&x| x.to_string())\n .collect::>()\n .join(\" \");\n println!(\"? {}\", query);\n let T: String = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().to_string()\n };\n res[i - 1] = if T.as_str() == \"Red\" { 'R' } else { 'B' };\n }\n\n let ans = res\n .iter()\n .map(|&c| c.to_string())\n .collect::>()\n .join(\"\");\n println!(\"! {}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1305", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N points in a D-dimensional space.

\n

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

\n

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \\sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

\n

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10
  • \n
  • 1 \\leq D \\leq 10
  • \n
  • -20 \\leq X_{ij} \\leq 20
  • \n
  • No two given points have the same coordinates. That is, if i \\neq j, there exists k such that X_{ik} \\neq X_{jk}.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N D\nX_{11} X_{12} ... X_{1D}\nX_{21} X_{22} ... X_{2D}\n\\vdots\nX_{N1} X_{N2} ... X_{ND}\n
\n
\n
\n
\n
\n

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n1 2\n5 5\n-2 8\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

The number of pairs with an integer distance is one, as follows:

\n
    \n
  • The distance between the first point and the second point is \\sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.
  • \n
  • The distance between the second point and the third point is \\sqrt{|5-(-2)|^2 + |5-8|^2} = \\sqrt{58}, which is not an integer.
  • \n
  • The distance between the third point and the first point is \\sqrt{|-2-1|^2+|8-2|^2} = 3\\sqrt{5}, which is not an integer.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 4\n-3 7 8 2\n-12 1 10 2\n-2 8 9 3\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5 1\n1\n2\n3\n4\n5\n
\n
\n
\n
\n
\n

Sample Output 3

10\n
\n
\n
", "c_code": "int solution() {\n int N = 0;\n int D = 0;\n int i = 0;\n int j = 0;\n int ans = 0;\n int count = 0;\n double ansd = 0.0;\n int X[11][10] = {0};\n scanf(\"%d %d\", &N, &D);\n for (i = 0; i < N; i++) {\n for (j = 0; j < D; j++) {\n scanf(\"%d\", &X[i][j]);\n }\n }\n\n for (j = 0; j < D; j++) {\n X[N][j] = X[0][j];\n }\n\n int k = 0;\n for (k = 0; k < N; k++) {\n for (i = k + 1; i < N; i++) {\n ans = 0;\n for (j = 0; j < D; j++) {\n ans += abs(X[k][j] - X[i][j]) * abs(X[k][j] - X[i][j]);\n }\n\n ansd = sqrt((double)ans);\n if (ceil(ansd) != floor(ansd)) {\n\n } else {\n count++;\n }\n }\n }\n printf(\"%d\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let (N, D): (usize, usize) = {\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 )\n };\n let X: Vec> = {\n let mut X = vec![];\n for _ in 0..N {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n X.push(\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect(),\n );\n }\n X\n };\n\n let sq = (0..1000)\n .map(|i| i * i)\n .collect::>();\n let ans = (0..(N - 1))\n .map(|i| {\n ((i + 1)..N)\n .map(|j| (0..D).map(|k| (X[i][k] - X[j][k]).pow(2)).sum::())\n .filter(|&d| sq.contains(&d))\n .count()\n })\n .sum::();\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1306", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We will buy a product for N yen (the currency of Japan) at a shop.

\n

If we use only 1000-yen bills to pay the price, how much change will we receive?

\n

Assume we use the minimum number of bills required.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10000
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the amount of change as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1900\n
\n
\n
\n
\n
\n

Sample Output 1

100\n
\n

We will use two 1000-yen bills to pay the price and receive 100 yen in change.

\n
\n
\n
\n
\n
\n

Sample Input 2

3000\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

We can pay the exact price.

\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n scanf(\"%d\", &N);\n for (int i = 0; i < 10; i++) {\n if (((1000 * i) < N) && (N <= (1000 * (i + 1)))) {\n printf(\"%d\", (1000 * (i + 1)) - N);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n stdin().read_line(&mut line).ok();\n let n: i32 = line.trim().parse().ok().unwrap();\n println!(\n \"{}\",\n if n % 1000 == 0 {\n 0\n } else {\n (n / 1000 + 1) * 1000 - n\n }\n );\n}", "difficulty": "medium"} {"problem_id": "1307", "problem_description": "Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals s.If Kirito starts duelling with the i-th (1 ≤ i ≤ n) dragon and Kirito's strength is not greater than the dragon's strength xi, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by yi.Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.", "c_code": "int solution() {\n int s;\n int n;\n int flag = 0;\n scanf(\"%d%d\", &s, &n);\n int a[n][2];\n for (int i = 0; i < n; i++) {\n scanf(\"%d%d\", &a[i][0], &a[i][1]);\n }\n for (int i = 0; i < n; i++) {\n for (int k = 0; k < n; k++) {\n if (s > a[k][0] && a[k][0] != 0) {\n s += a[k][1];\n a[k][0] = 0;\n break;\n }\n }\n }\n for (int j = 0; j < n; j++) {\n if (a[j][0] != 0) {\n flag = 1;\n }\n }\n if (flag == 0) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut s = words[0];\n let n = words[1];\n\n let mut dragons = Vec::>::new();\n\n for _ in 0..n {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut dragon = Vec::new();\n dragon.push(words[0]);\n dragon.push(words[1]);\n dragons.push(dragon);\n }\n\n dragons.sort_by(|a, b| {\n if a.first().unwrap() > b.first().unwrap() {\n Ordering::Greater\n } else if a.first().unwrap() < b.first().unwrap() {\n Ordering::Less\n } else {\n Ordering::Equal\n }\n });\n\n let mut answer = \"YES\";\n\n for i in dragons {\n if s - i.first().unwrap() > 0 {\n s += i.get(1).unwrap();\n } else {\n answer = \"NO\";\n }\n }\n\n println!(\"{}\", answer);\n}", "difficulty": "hard"} {"problem_id": "1308", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

In K-city, there are n streets running east-west, and m streets running north-south. Each street running east-west and each street running north-south cross each other. We will call the smallest area that is surrounded by four streets a block. How many blocks there are in K-city?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ n, m ≤ 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n m\n
\n
\n
\n
\n
\n

Output

Print the number of blocks in K-city.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

There are six blocks, as shown below:

\n
\n\"9179be829dc9810539213537d4c7398c.png\"\n
\n
\n
\n
\n
\n
\n

Sample Input 2

2 2\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

There are one block, as shown below:

\n
\n\"997bfafa99be630b54d037225a5c68ea.png\"\n
\n
\n
", "c_code": "int solution() {\n int m = 0;\n int n = 0;\n if (scanf(\"%d %d\", &n, &m) && n >= 2 && n <= 100 && m >= 2 && m <= 100) {\n printf(\"%d\", (n - 1) * (m - 1));\n } else {\n return 0;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).ok();\n let v: Vec = line\n .split_whitespace()\n .map(|n| n.parse().unwrap())\n .collect();\n let n = v[0];\n let m = v[1];\n println!(\"{}\", (n - 1) * (m - 1));\n}", "difficulty": "medium"} {"problem_id": "1309", "problem_description": "In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if there are two Bobs?).A group of soldiers is effective if and only if their names are different. For example, a group (John, Bob, Limak) would be effective, while groups (Gary, Bob, Gary) and (Alice, Alice) wouldn't.You are a spy in the enemy's camp. You noticed n soldiers standing in a row, numbered 1 through n. The general wants to choose a group of k consecutive soldiers. For every k consecutive soldiers, the general wrote down whether they would be an effective group or not.You managed to steal the general's notes, with n - k + 1 strings s1, s2, ..., sn - k + 1, each either \"YES\" or \"NO\". The string s1 describes a group of soldiers 1 through k (\"YES\" if the group is effective, and \"NO\" otherwise). The string s2 describes a group of soldiers 2 through k + 1. And so on, till the string sn - k + 1 that describes a group of soldiers n - k + 1 through n. Your task is to find possible names of n soldiers. Names should match the stolen notes. Each name should be a string that consists of between 1 and 10 English letters, inclusive. The first letter should be uppercase, and all other letters should be lowercase. Names don't have to be existing names — it's allowed to print \"Xyzzzdj\" or \"T\" for example.Find and print any solution. It can be proved that there always exists at least one solution.", "c_code": "int solution() {\n int n;\n int k;\n unsigned char str[4];\n unsigned char ans[51];\n scanf(\"%d %d\", &n, &k);\n for (int i = 0; i < n; ++i) {\n ans[i] = i + 1;\n }\n for (int i = 0; i < n - k + 1; ++i) {\n scanf(\"%s\", str);\n if (str[0] == 'N') {\n ans[i + k - 1] = ans[i];\n }\n }\n for (int i = 0; i < n; ++i) {\n if (ans[i] < 27) {\n printf(\"%c \", 'A' + ans[i] - 1);\n } else {\n printf(\"A%c \", 'a' + ans[i] - 27);\n }\n }\n puts(\"\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut soldiers = vec![];\n\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).unwrap();\n let mut input = input.split_whitespace();\n\n let n = input.next().unwrap().parse::().unwrap();\n let k = input.next().unwrap().parse::().unwrap();\n\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).unwrap();\n let mut effective = input.split_whitespace().map(|s| s == \"YES\");\n\n for i in 1..k {\n soldiers.push(i);\n }\n\n for i in k..n + 1 {\n if effective.next().unwrap() {\n soldiers.push(i);\n } else {\n let id = soldiers[i - k];\n soldiers.push(id);\n }\n }\n\n let mut emit_space = false;\n for mut id in soldiers {\n if emit_space {\n print!(\" \");\n }\n let mut first = true;\n while id > 0 {\n let base = if first { b'A' } else { b'a' };\n print!(\"{}\", ((id % 26) as u8 + base) as char);\n id /= 26;\n first = false;\n }\n emit_space = true;\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "1310", "problem_description": "Monocarp is playing a computer game. In this game, his character fights different monsters.A fight between a character and a monster goes as follows. Suppose the character initially has health $$$h_C$$$ and attack $$$d_C$$$; the monster initially has health $$$h_M$$$ and attack $$$d_M$$$. The fight consists of several steps: the character attacks the monster, decreasing the monster's health by $$$d_C$$$; the monster attacks the character, decreasing the character's health by $$$d_M$$$; the character attacks the monster, decreasing the monster's health by $$$d_C$$$; the monster attacks the character, decreasing the character's health by $$$d_M$$$; and so on, until the end of the fight. The fight ends when someone's health becomes non-positive (i. e. $$$0$$$ or less). If the monster's health becomes non-positive, the character wins, otherwise the monster wins.Monocarp's character currently has health equal to $$$h_C$$$ and attack equal to $$$d_C$$$. He wants to slay a monster with health equal to $$$h_M$$$ and attack equal to $$$d_M$$$. Before the fight, Monocarp can spend up to $$$k$$$ coins to upgrade his character's weapon and/or armor; each upgrade costs exactly one coin, each weapon upgrade increases the character's attack by $$$w$$$, and each armor upgrade increases the character's health by $$$a$$$.Can Monocarp's character slay the monster if Monocarp spends coins on upgrades optimally?", "c_code": "int solution() {\n int t;\n long long int d;\n long long int e;\n scanf(\"%d\", &t);\n long long int hc[t];\n long long int dc[t];\n long long int hm[t];\n long long int dm[t];\n long long int k[t];\n long long int w[t];\n long long int a[t];\n\n int i;\n int j;\n int flag[t];\n for (i = 0; i < t; i++) {\n flag[i] = 0;\n }\n int m = 0;\n while (m < t) {\n scanf(\"%lld %lld %lld %lld %lld %lld %lld\", &hc[m], &dc[m], &hm[m], &dm[m],\n &k[m], &w[m], &a[m]);\n\n for (i = 0; i <= k[m]; i++) {\n d = dc[m] + w[m] * i;\n e = hc[m] + a[m] * (k[m] - i);\n if (((hm[m] + d - 1) / d) <= ((e + dm[m] - 1) / dm[m])) {\n flag[m] = 1;\n }\n }\n\n m++;\n }\n for (i = 0; i < t; i++) {\n if (flag[i] == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n stdin.read_line(&mut line).unwrap();\n let t: usize = line.trim().parse().unwrap();\n\n let stdout = io::stdout();\n let handle = stdout.lock();\n let mut buffer = BufWriter::with_capacity(65536, handle);\n 'test_case: for _t in 0..t {\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let mut character = line.split_ascii_whitespace();\n let character_health: u64 = character.next().unwrap().parse().unwrap();\n let character_damage: u64 = character.next().unwrap().parse().unwrap();\n\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let mut monster = line.split_ascii_whitespace();\n let monster_health: u64 = monster.next().unwrap().parse().unwrap();\n let monster_damage: u64 = monster.next().unwrap().parse().unwrap();\n\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let mut upgrades = line.split_ascii_whitespace();\n let coins: u64 = upgrades.next().unwrap().parse().unwrap();\n let damage_upgrade: u64 = upgrades.next().unwrap().parse().unwrap();\n let health_upgrade: u64 = upgrades.next().unwrap().parse().unwrap();\n\n for i in 0..=coins {\n let new_health = character_health + i * health_upgrade;\n let new_damage = character_damage + (coins - i) * damage_upgrade;\n let turns_to_win_monster =\n new_health / monster_damage + (!new_health.is_multiple_of(monster_damage) as u64);\n let turns_to_win_character =\n monster_health / new_damage + (!monster_health.is_multiple_of(new_damage) as u64);\n if turns_to_win_character <= turns_to_win_monster {\n writeln!(&mut buffer, \"YES\").unwrap();\n continue 'test_case;\n }\n }\n writeln!(&mut buffer, \"NO\").unwrap();\n }\n buffer.flush().unwrap();\n}", "difficulty": "medium"} {"problem_id": "1311", "problem_description": "You are given a positive integer $$$x$$$. Check whether the number $$$x$$$ is representable as the sum of the cubes of two positive integers.Formally, you need to check if there are two integers $$$a$$$ and $$$b$$$ ($$$1 \\le a, b$$$) such that $$$a^3+b^3=x$$$.For example, if $$$x = 35$$$, then the numbers $$$a=2$$$ and $$$b=3$$$ are suitable ($$$2^3+3^3=8+27=35$$$). If $$$x=4$$$, then no pair of numbers $$$a$$$ and $$$b$$$ is suitable.", "c_code": "int solution() {\n int t = 0;\n int yes = 0;\n long long int k = 0;\n long long int j = 0;\n long long int s = 0;\n long long int n = 0;\n long long int i = 0;\n long long int last = 0;\n scanf(\"%d\", &t);\n\n while (t-- > 0) {\n i = 1;\n yes = 0;\n scanf(\"%lld\", &n);\n while ((i * i * i) < n) {\n i++;\n }\n last = i - 1;\n for (j = 1; j <= last; j++) {\n for (k = last; k >= j; k--) {\n s = (j * j * j) + (k * k * k);\n if (s == n) {\n printf(\"YES\\n\");\n yes = 1;\n break;\n }\n if (s < n) {\n last = k;\n break;\n }\n }\n if (yes == 1) {\n break;\n }\n }\n if (yes == 0) {\n printf(\"NO\\n\");\n }\n }\n return 0;\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 cubes: BTreeSet = (1..10001_i64).map(|i| i * i * i).collect();\n let tc: usize = sc.next();\n for _ in 0..tc {\n let x: i64 = sc.next();\n let mut contains = false;\n for a in cubes.clone() {\n if cubes.contains(&(x - a)) {\n contains = true;\n break;\n }\n }\n writeln!(out, \"{}\", if contains { \"YES\" } else { \"NO\" }).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1312", "problem_description": "Today, Marin is at a cosplay exhibition and is preparing for a group photoshoot!For the group picture, the cosplayers form a horizontal line. A group picture is considered beautiful if for every contiguous segment of at least $$$2$$$ cosplayers, the number of males does not exceed the number of females (obviously).Currently, the line has $$$n$$$ cosplayers which can be described by a binary string $$$s$$$. The $$$i$$$-th cosplayer is male if $$$s_i = 0$$$ and female if $$$s_i = 1$$$. To ensure that the line is beautiful, you can invite some additional cosplayers (possibly zero) to join the line at any position. You can't remove any cosplayer from the line.Marin wants to know the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. She can't do this on her own, so she's asking you for help. Can you help her?", "c_code": "int solution() {\n int t;\n int l = 0;\n int ans = 0;\n scanf(\"%d\", &t);\n int arr[t];\n char *p[t];\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &arr[i]);\n p[i] = (char *)calloc(arr[i] + 1, sizeof(char));\n scanf(\"%s\", p[i]);\n }\n for (int i = 0; i < t; i++) {\n l = strlen(p[i]);\n ans = 0;\n for (int j = 0; j < l - 1; j++) {\n if (*(p[i] + j) == '0' && *(p[i] + j + 1) == '0') {\n ans += 2;\n } else if (*(p[i] + j) == '1' && *(p[i] + j + 1) == '0') {\n if (!(*(p[i] + j - 1) == '1' || j == 0)) {\n ans++;\n }\n }\n }\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut qntcasos = String::new();\n\n io::stdin().read_line(&mut qntcasos).unwrap();\n let qntcasos: u32 = qntcasos.trim().parse().unwrap();\n\n for _i in 0..qntcasos {\n let mut casoatual = String::new();\n let mut casoatualnumero = String::new();\n io::stdin().read_line(&mut casoatualnumero).unwrap();\n let _casoatualnumero: u32 = casoatualnumero.trim().parse().unwrap();\n\n io::stdin().read_line(&mut casoatual).unwrap();\n\n let mut qnt = 0;\n\n for x in 1..casoatual.len() {\n if &casoatual[x - 1..x + 1] == \"00\" {\n qnt += 2;\n }\n\n if x != casoatual.len() - 1 && &casoatual[x - 1..x + 2] == \"010\" {\n qnt += 1;\n }\n }\n\n println!(\"{}\", qnt);\n }\n}", "difficulty": "medium"} {"problem_id": "1313", "problem_description": "

問題 1: 平均点 (Average Score)\n

\n
\n\n

問題

\n\n

\nJOI 高校の授業には,太郎君,次郎君,三郎君,四郎君,花子さんの 5 人の生徒が参加した.\n

\n\n

\nこの授業では,期末試験を実施した.期末試験は,5 人全員が受験した.期末試験の点数が 40 点以上の生徒は,期末試験の点数がそのまま成績になった.期末試験の点数が 40 点未満の生徒は全員,補習を受け,成績が 40 点になった.\n

\n\n

\n5 人の生徒の期末試験の点数が与えられたとき,5 人の成績の平均点を計算するプログラムを作成せよ.\n

\n\n

入力

\n

\n入力は 5 行からなり,1 行に 1 つずつ整数が書かれている.
\n1 行目には 太郎君の期末試験の点数が書かれている.
\n2 行目には 次郎君の期末試験の点数が書かれている.
\n3 行目には 三郎君の期末試験の点数が書かれている.
\n4 行目には 四郎君の期末試験の点数が書かれている.
\n5 行目には 花子さんの期末試験の点数が書かれている.
\n

\n\n

\n生徒の期末試験の点数はすべて 0 点以上 100 点以下の整数である.
\n生徒の期末試験の点数はすべて 5 の倍数である.
\n5 人の生徒の成績の平均点は必ず整数になる.\n

\n\n

出力

\n

\n5 人の生徒の成績の平均点をあらわす整数を 1 行で出力せよ.\n

\n\n

入出力例

\n\n

入力例 1

\n\n
\n10\n65\n100\n30\n95\n
\n\n

出力例 1

\n
\n68\n
\n\n

\n入出力例 1 では,太郎君と四郎君の期末試験の点数は 40 点未満なので,太郎君と四郎君の成績は 40 点になる.次郎君と三郎君と花子さんの期末試験の点数は 40 点以上なので,次郎君の成績は 65 点,三郎君の成績は 100 点,花子さんの成績は 95 点となる.5 人の成績の合計は 340 なので,平均点は 68 点となる.\n

\n\n

入力例 2

\n\n
\n40\n95\n0\n95\n50\n
\n\n\n

出力例 2

\n
\n64\n
\n\n\n\n
\n

\n問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。\n

\n
", "c_code": "int solution() {\n int a = 0;\n int ans = 0;\n int i = 0;\n for (i = 0; i < 5; i++) {\n\n scanf(\"%d\", &a);\n if (a <= 40) {\n a = 40;\n }\n ans += a;\n }\n printf(\"%d\\n\", ans / 5);\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 lines = input.split('\\n');\n\n let avg = lines\n .take(5)\n .map(|l| {\n let score = l.parse().unwrap();\n if score < 40 {\n 40\n } else {\n score\n }\n })\n .sum::()\n / 5;\n\n println!(\"{}\", avg);\n}", "difficulty": "easy"} {"problem_id": "1314", "problem_description": "Mishka wants to buy some food in the nearby shop. Initially, he has $$$s$$$ burles on his card. Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number $$$1 \\le x \\le s$$$, buy food that costs exactly $$$x$$$ burles and obtain $$$\\lfloor\\frac{x}{10}\\rfloor$$$ burles as a cashback (in other words, Mishka spends $$$x$$$ burles and obtains $$$\\lfloor\\frac{x}{10}\\rfloor$$$ back). The operation $$$\\lfloor\\frac{a}{b}\\rfloor$$$ means $$$a$$$ divided by $$$b$$$ rounded down.It is guaranteed that you can always buy some food that costs $$$x$$$ for any possible value of $$$x$$$.Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.For example, if Mishka has $$$s=19$$$ burles then the maximum number of burles he can spend is $$$21$$$. Firstly, he can spend $$$x=10$$$ burles, obtain $$$1$$$ burle as a cashback. Now he has $$$s=10$$$ burles, so can spend $$$x=10$$$ burles, obtain $$$1$$$ burle as a cashback and spend it too.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n\n int snum;\n\n int s[10000];\n scanf(\"%d\", &snum);\n for (int i = 0; i < snum; i++) {\n scanf(\"%d\", &s[i]);\n }\n for (int i = 0; i < snum; i++) {\n int x = s[i];\n\n do {\n x = x + s[i] / 10;\n s[i] = s[i] - s[i] / 10 * 10 + s[i] / 10;\n } while (s[i] / 10 > 0);\n\n printf(\"%d\\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 let t: u32 = line.trim().parse().unwrap();\n for _ in 0..t {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let mut coins: u32 = line.trim().parse().unwrap();\n let mut spent = 0;\n while coins >= 10 {\n let to_spend = coins - (coins % 10);\n spent += to_spend;\n coins = coins - to_spend + (to_spend / 10);\n }\n spent += coins;\n\n println!(\"{}\", spent);\n }\n}", "difficulty": "medium"} {"problem_id": "1315", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq a \\leq 9
  • \n
  • 1 \\leq b \\leq 9
  • \n
  • a and b are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a b\n
\n
\n
\n
\n
\n

Output

Print the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.)

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 3\n
\n
\n
\n
\n
\n

Sample Output 1

3333\n
\n

We have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 7\n
\n
\n
\n
\n
\n

Sample Output 2

7777777\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n scanf(\"%d\", &a);\n\n int b = 0;\n scanf(\"%d\", &b);\n\n int ans = 0;\n\n if (a <= b) {\n for (int i = 0; i < b; i++) {\n ans = ans + a * pow(10, i);\n }\n printf(\"%d\", ans);\n } else {\n for (int i = 0; i < a; i++) {\n ans = ans + b * pow(10, i);\n }\n printf(\"%d\", ans);\n }\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 ab: Vec = s.split_whitespace().map(|w| w.parse().unwrap()).collect();\n if ab[0] < ab[1] {\n for _ in 0..ab[1] {\n print!(\"{}\", ab[0]);\n }\n } else {\n for _ in 0..ab[0] {\n print!(\"{}\", ab[1]);\n }\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1316", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There is an integer sequence A of length N whose values are unknown.

\n

Given is an integer sequence B of length N-1 which is known to satisfy the following:

\n

B_i \\geq \\max(A_i, A_{i+1})

\n

Find the maximum possible sum of the elements of A.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 100
  • \n
  • 0 \\leq B_i \\leq 10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nB_1 B_2 ... B_{N-1}\n
\n
\n
\n
\n
\n

Output

Print the maximum possible sum of the elements of A.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2 5\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n3\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6\n0 153 10 10 23\n
\n
\n
\n
\n
\n

Sample Output 3

53\n
\n
\n
", "c_code": "int solution() {\n int N;\n int ans = 0;\n scanf(\"%d\", &N);\n int B[N - 1];\n for (int i = 0; i < N - 1; i++) {\n scanf(\"%d\", &B[i]);\n }\n if (N > 2) {\n for (int i = 0; i < N - 1; i++) {\n if ((B[i] > B[i + 1]) && (i < N - 2)) {\n ans += B[i + 1];\n }\n if ((B[i] <= B[i + 1]) && (i < N - 2)) {\n ans += B[i];\n }\n }\n }\n ans += B[0];\n ans += B[N - 2];\n printf(\"%d\", ans);\n return 0;\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 n: usize = iter.next().unwrap().parse().unwrap();\n let mut b: Vec = vec![];\n for _ in 0..(n - 1) {\n b.push(iter.next().unwrap().parse().unwrap());\n }\n\n let mut a: Vec = vec![];\n\n for i in 0..n {\n if i == 0 {\n a.push(b[0]);\n continue;\n } else if i == n - 1 {\n a.push(b[i - 1]);\n break;\n }\n a.push(std::cmp::min(b[i], b[i - 1]));\n }\n\n let mut ans = 0;\n for a in a.into_iter() {\n ans += a;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1317", "problem_description": "There are quite a lot of ways to have fun with inflatable balloons. For example, you can fill them with water and see what happens.Grigory and Andrew have the same opinion. So, once upon a time, they went to the shop and bought $$$n$$$ packets with inflatable balloons, where $$$i$$$-th of them has exactly $$$a_i$$$ balloons inside.They want to divide the balloons among themselves. In addition, there are several conditions to hold: Do not rip the packets (both Grigory and Andrew should get unbroken packets); Distribute all packets (every packet should be given to someone); Give both Grigory and Andrew at least one packet; To provide more fun, the total number of balloons in Grigory's packets should not be equal to the total number of balloons in Andrew's packets. Help them to divide the balloons or determine that it's impossible under these conditions.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n + 1];\n int sum = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n sum += a[i];\n }\n if (n == 1 || (n == 2 && a[0] == a[1])) {\n printf(\"-1\\n\");\n return 0;\n }\n if (sum - a[0] == a[0]) {\n printf(\"2\\n1 2\\n\");\n } else {\n printf(\"1\\n1\\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).unwrap();\n\n let input_strs = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let _n = input_strs.first().unwrap();\n\n let _possible = true;\n\n if input_strs.iter().skip(1).count() == 1 {\n println!(\"-1\");\n return;\n }\n\n let total = input_strs.iter().skip(1).sum::();\n\n let mut found_min = 2000;\n let found_idx = input_strs\n .iter()\n .skip(1)\n .enumerate()\n .fold(0, |acc, (e, v)| {\n if *v < found_min {\n found_min = *v;\n e\n } else {\n acc\n }\n });\n\n if found_min == total - found_min {\n println!(\"-1\");\n return;\n }\n\n println!(\"1\");\n println!(\"{}\", found_idx + 1);\n}", "difficulty": "medium"} {"problem_id": "1318", "problem_description": "Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains $$$26$$$ buttons (one for each letter of the Latin alphabet). Each button is either working fine or malfunctioning. To check which buttons need replacement, Polycarp pressed some buttons in sequence, and a string $$$s$$$ appeared on the screen. When Polycarp presses a button with character $$$c$$$, one of the following events happened: if the button was working correctly, a character $$$c$$$ appeared at the end of the string Polycarp was typing; if the button was malfunctioning, two characters $$$c$$$ appeared at the end of the string. For example, suppose the buttons corresponding to characters a and c are working correctly, and the button corresponding to b is malfunctioning. If Polycarp presses the buttons in the order a, b, a, c, a, b, a, then the string he is typing changes as follows: a $$$\\rightarrow$$$ abb $$$\\rightarrow$$$ abba $$$\\rightarrow$$$ abbac $$$\\rightarrow$$$ abbaca $$$\\rightarrow$$$ abbacabb $$$\\rightarrow$$$ abbacabba.You are given a string $$$s$$$ which appeared on the screen after Polycarp pressed some buttons. Help Polycarp to determine which buttons are working correctly for sure (that is, this string could not appear on the screen if any of these buttons was malfunctioning).You may assume that the buttons don't start malfunctioning when Polycarp types the string: each button either works correctly throughout the whole process, or malfunctions throughout the whole process.", "c_code": "int solution(void) {\n unsigned short test_cases;\n scanf(\"%hu\\n\", &test_cases);\n const unsigned short SIZE = 26;\n bool button_states[SIZE];\n\n for (unsigned short i = 0; i < test_cases; ++i) {\n memset(button_states, false, sizeof(button_states));\n\n const unsigned short MAX_LENGTH = 500;\n char str[MAX_LENGTH];\n scanf(\"%s\", str);\n\n char *c = str;\n do {\n if (*c != *(c + 1)) {\n button_states[*c - 'a'] = true;\n ++c;\n } else {\n c += 2;\n }\n } while (*c != '\\0');\n\n for (unsigned short j = 0; j < SIZE; ++j) {\n if (button_states[j]) {\n putchar('a' + j);\n }\n }\n\n putchar('\\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 for _ in 0..t {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n\n let letters = input.trim();\n let mut working = [false; 26];\n\n let mut letters_iter = letters.chars();\n let mut current = letters_iter.next().unwrap();\n let mut counter = 1;\n for c in letters_iter {\n if c == current {\n counter += 1;\n } else {\n if counter % 2 != 0 {\n working[current as usize - 'a' as usize] = true;\n }\n\n counter = 1;\n current = c;\n }\n }\n if counter % 2 != 0 {\n working[current as usize - 'a' as usize] = true;\n }\n\n for i in 0..26 {\n if working[i] {\n print!(\"{}\", (i as u8 + b'a') as char);\n }\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1319", "problem_description": "\n

Score : 1000 points

\n
\n
\n

Problem Statement

There are N balls in a row.\nInitially, the i-th ball from the left has the integer A_i written on it.

\n

When Snuke cast a spell, the following happens:

\n
    \n
  • Let the current number of balls be k. All the balls with k written on them disappear at the same time.
  • \n
\n

Snuke's objective is to vanish all the balls by casting the spell some number of times.\nThis may not be possible as it is. If that is the case, he would like to modify the integers on the minimum number of balls to make his objective achievable.

\n

By the way, the integers on these balls sometimes change by themselves.\nThere will be M such changes. In the j-th change, the integer on the X_j-th ball from the left will change into Y_j.

\n

After each change, find the minimum number of modifications of integers on the balls Snuke needs to make if he wishes to achieve his objective before the next change occurs. We will assume that he is quick enough in modifying integers. Here, note that he does not actually perform those necessary modifications and leaves them as they are.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 200000
  • \n
  • 1 \\leq M \\leq 200000
  • \n
  • 1 \\leq A_i \\leq N
  • \n
  • 1 \\leq X_j \\leq N
  • \n
  • 1 \\leq Y_j \\leq N
  • \n
\n
\n
\n
\n
\n

Subscore

    \n
  • In the test set worth 500 points, N \\leq 200 and M \\leq 200.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_1 A_2 ... A_N\nX_1 Y_1\nX_2 Y_2\n:\nX_M Y_M\n
\n
\n
\n
\n
\n

Output

Print M lines.\nThe j-th line should contain the minimum necessary number of modifications of integers on the balls to make Snuke's objective achievable.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n1 1 3 4 5\n1 2\n2 5\n5 4\n
\n
\n
\n
\n
\n

Sample Output 1

0\n1\n1\n
\n
    \n
  • After the first change, the integers on the balls become 2, 1, 3, 4, 5, from left to right. Here, all the balls can be vanished by casting the spell five times. Thus, no modification is necessary.
  • \n
  • After the second change, the integers on the balls become 2, 5, 3, 4, 5, from left to right. In this case, at least one modification must be made. One optimal solution is to modify the integer on the fifth ball from the left to 2, and cast the spell four times.
  • \n
  • After the third change, the integers on the balls become 2, 5, 3, 4, 4, from left to right. Also in this case, at least one modification must be made. One optimal solution is to modify the integer on the third ball from the left to 2, and cast the spell three times.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4 4\n4 4 4 4\n4 1\n3 1\n1 1\n2 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n1\n2\n3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 10\n8 7 2 9 10 6 6 5 5 4\n8 1\n6 3\n6 2\n7 10\n9 7\n9 9\n2 4\n8 1\n1 8\n7 7\n
\n
\n
\n
\n
\n

Sample Output 3

1\n0\n1\n2\n2\n3\n3\n3\n3\n2\n
\n
\n
", "c_code": "int solution() {\n int N;\n int M;\n int i;\n int j;\n int ans = 0;\n int old;\n int new;\n scanf(\"%d%d\", &N, &M);\n int *A = (int *)malloc(sizeof(int) * N);\n int *Anum = (int *)malloc(sizeof(int) * N);\n int *B = (int *)malloc(sizeof(int) * N * 2);\n int *X = (int *)malloc(sizeof(int) * M);\n int *Y = (int *)malloc(sizeof(int) * M);\n for (i = 0; i < N; i++) {\n scanf(\"%d\", &A[i]);\n A[i]--;\n Anum[i] = 0;\n B[i] = 0;\n B[N + i] = 0;\n }\n for (j = 0; j < M; j++) {\n scanf(\"%d%d\", &X[j], &Y[j]);\n X[j]--;\n Y[j]--;\n }\n for (i = 0; i < N; i++) {\n B[N + A[i] - Anum[A[i]]]++;\n Anum[A[i]]++;\n }\n for (i = 0; i < N; i++) {\n if (B[N + i] == 0) {\n ans++;\n }\n }\n for (j = 0; j < M; j++) {\n\n old = A[X[j]];\n new = Y[j];\n A[X[j]] = Y[j];\n Anum[old]--;\n B[N + old - Anum[old]]--;\n if (B[N + old - Anum[old]] == 0 && old - Anum[old] >= 0) {\n ans++;\n }\n if (B[N + new - Anum[new]] == 0 && new - Anum[new] >= 0) {\n ans--;\n }\n B[N + new - Anum[new]]++;\n Anum[new]++;\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n m: usize,\n mut v: [usize; n],\n xy: [(usize1, usize); m]\n }\n let mut cnt = vec![0; n + 1];\n let mut ds = vec![0; n];\n for &a in &v {\n if cnt[a] < a {\n ds[a - cnt[a] - 1] += 1;\n }\n cnt[a] += 1;\n }\n let mut ans = ds.iter().filter(|&&x| x == 0).count();\n for &(x, y) in &xy {\n cnt[v[x]] -= 1;\n if cnt[v[x]] < v[x] {\n ds[v[x] - cnt[v[x]] - 1] -= 1;\n if ds[v[x] - cnt[v[x]] - 1] == 0 {\n ans += 1;\n }\n }\n if cnt[y] < y {\n if ds[y - cnt[y] - 1] == 0 {\n ans -= 1;\n }\n ds[y - cnt[y] - 1] += 1;\n }\n cnt[y] += 1;\n v[x] = y;\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1320", "problem_description": "Berland State University has received a new update for the operating system. Initially it is installed only on the $$$1$$$-st computer.Update files should be copied to all $$$n$$$ computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them using a patch cable (a cable connecting two computers directly). Only one patch cable can be connected to a computer at a time. Thus, from any computer where the update files are installed, they can be copied to some other computer in exactly one hour.Your task is to find the minimum number of hours required to copy the update files to all $$$n$$$ computers if there are only $$$k$$$ patch cables in Berland State University.", "c_code": "int solution() {\n int t;\n long long int n;\n long long int k;\n long long int done;\n long long int path;\n long long int h;\n scanf(\"%d%*c\", &t);\n while (t > 0) {\n scanf(\"%lld %lld%*c\", &n, &k);\n done = 1;\n n -= 1;\n h = 0;\n path = 1;\n while (n > 0) {\n if (2 * path <= k && 2 * path <= done) {\n path = 2 * path;\n n -= path;\n done += path;\n h += 1;\n } else if (path < k && k <= done) {\n path = k;\n n -= path;\n done += path;\n h += 1;\n } else {\n if (done == 1) {\n n -= path;\n done += path;\n h += 1;\n } else {\n h += n / path;\n if (n % path != 0) {\n h += 1;\n }\n n = 0;\n }\n }\n }\n printf(\"%lld\\n\", h);\n t -= 1;\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut s = String::new();\n io::stdin().read_to_string(&mut s)?;\n let mut it = s.split_whitespace().map(|s| s.to_string());\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut out = String::new();\n for _case in 1..=t {\n let n: u64 = it.next().unwrap().parse().unwrap();\n let k: u64 = it.next().unwrap().parse().unwrap();\n let mut hour = 0;\n let mut install = 1;\n while install < n {\n let new_install = k.min(install);\n if new_install == k {\n let to_install = n - install;\n let rem_hours = to_install / k;\n hour += rem_hours;\n if rem_hours * k < to_install {\n hour += 1;\n }\n break;\n } else {\n hour += 1;\n install += new_install;\n }\n }\n let ans = format!(\"{}\\n\", hour);\n out.push_str(ans.as_str());\n }\n print!(\"{}\", out);\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1321", "problem_description": "Given a permutation $$$p$$$ of length $$$n$$$, find its subsequence $$$s_1$$$, $$$s_2$$$, $$$\\ldots$$$, $$$s_k$$$ of length at least $$$2$$$ such that: $$$|s_1-s_2|+|s_2-s_3|+\\ldots+|s_{k-1}-s_k|$$$ is as big as possible over all subsequences of $$$p$$$ with length at least $$$2$$$. Among all such subsequences, choose the one whose length, $$$k$$$, is as small as possible. If multiple subsequences satisfy these conditions, you are allowed to find any of them.A sequence $$$a$$$ is a subsequence of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deleting some (possibly, zero or all) elements.A permutation of length $$$n$$$ is an array of length $$$n$$$ in which every element from $$$1$$$ to $$$n$$$ occurs exactly once.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n\n int arr[n];\n int res[n];\n int tmp = 0;\n int r = 0;\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", arr + i);\n }\n\n res[r++] = arr[0];\n\n for (int i = 1; i < (n - 1); i++) {\n if ((arr[i - 1] < arr[i]) != (arr[i] < arr[i + 1])) {\n res[r++] = arr[i];\n }\n }\n\n res[r++] = arr[n - 1];\n\n printf(\"%d\\n\", r);\n for (int i = 0; i < r; i++) {\n printf(\"%d \", res[i]);\n }\n printf(\"\\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 n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let ss: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let mut ts: Vec = vec![ss[0]];\n for i in 1..(n - 1) {\n if ss[i - 1] < ss[i] && ss[i] > ss[i + 1] {\n ts.push(ss[i]);\n } else if ss[i - 1] == ss[i] && ss[i] != ss[i + 1] {\n ts.push(ss[i]);\n } else if ss[i - 1] > ss[i] && ss[i] < ss[i + 1] {\n ts.push(ss[i]);\n }\n }\n ts.push(ss[n - 1]);\n println!(\"{}\", ts.len());\n for t in ts {\n print!(\"{} \", t);\n }\n println!();\n }\n}", "difficulty": "easy"} {"problem_id": "1322", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are N slimes lining up in a row.\nInitially, the i-th slime from the left has a size of a_i.

\n

Taro is trying to combine all the slimes into a larger slime.\nHe will perform the following operation repeatedly until there is only one slime:

\n
    \n
  • Choose two adjacent slimes, and combine them into a new slime. The new slime has a size of x + y, where x and y are the sizes of the slimes before combining them. Here, a cost of x + y is incurred. The positional relationship of the slimes does not change while combining slimes.
  • \n
\n

Find the minimum possible total cost incurred.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 400
  • \n
  • 1 \\leq a_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 \\ldots a_N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible total cost incurred.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n10 20 30 40\n
\n
\n
\n
\n
\n

Sample Output 1

190\n
\n

Taro should do as follows (slimes being combined are shown in bold):

\n
    \n
  • (10, 20, 30, 40) → (30, 30, 40)
  • \n
  • (30, 30, 40) → (60, 40)
  • \n
  • (60, 40) → (100)
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5\n10 10 10 10 10\n
\n
\n
\n
\n
\n

Sample Output 2

120\n
\n

Taro should do, for example, as follows:

\n
    \n
  • (10, 10, 10, 10, 10) → (20, 10, 10, 10)
  • \n
  • (20, 10, 10, 10) → (20, 20, 10)
  • \n
  • (20, 20, 10) → (20, 30)
  • \n
  • (20, 30) → (50)
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

3\n1000000000 1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

5000000000\n
\n

The answer may not fit into a 32-bit integer type.

\n
\n
\n
\n
\n
\n

Sample Input 4

6\n7 6 8 6 1 1\n
\n
\n
\n
\n
\n

Sample Output 4

68\n
\n

Taro should do, for example, as follows:

\n
    \n
  • (7, 6, 8, 6, 1, 1) → (7, 6, 8, 6, 2)
  • \n
  • (7, 6, 8, 6, 2) → (7, 6, 8, 8)
  • \n
  • (7, 6, 8, 8) → (13, 8, 8)
  • \n
  • (13, 8, 8) → (13, 16)
  • \n
  • (13, 16) → (29)
  • \n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n long long a[n];\n long long dp[n][n];\n for (int i = 0; i < n; ++i) {\n scanf(\"%lld\", a + i), dp[i][i] = 0;\n }\n for (int d = 1; d < n; ++d) {\n for (int l = 0; l <= n - 1 - d; ++l) {\n dp[l][l + d] = 1000000000000000000;\n for (int m = l; m < l + d; ++m) {\n if (dp[l][m] + dp[m + 1][l + d] < dp[l][l + d]) {\n dp[l][l + d] = dp[l][m] + dp[m + 1][l + d];\n }\n }\n for (int m = l; m <= l + d; ++m) {\n dp[l][l + d] += a[m];\n }\n }\n }\n printf(\"%lld\\n\", dp[0][n - 1]);\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n v: [usize; n]\n }\n let mut tab: Vec> = vec![vec![usize::max_value(); n]; n];\n let mut ps: Vec = v\n .iter()\n .clone()\n .scan(0, |ac, x| {\n *ac += *x;\n Some(*ac)\n })\n .collect();\n ps.insert(0, 0);\n for i in 0..n {\n tab[i][i] = 0;\n }\n for d in 1..n {\n for i in 0..n - d {\n tab[i][i + d] = ps[i + d + 1] - ps[i]\n + (i..i + d)\n .map(|x| tab[i][x] + tab[x + 1][i + d])\n .min()\n .unwrap();\n }\n }\n println!(\"{}\", tab[0][n - 1]);\n}", "difficulty": "medium"} {"problem_id": "1323", "problem_description": "Dima's got a staircase that consists of n stairs. The first stair is at height a1, the second one is at a2, the last one is at an (1 ≤ a1 ≤ a2 ≤ ... ≤ an). Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The i-th box has width wi and height hi. Dima throws each box vertically down on the first wi stairs of the staircase, that is, the box covers stairs with numbers 1, 2, ..., wi. Each thrown box flies vertically down until at least one of the two following events happen: the bottom of the box touches the top of a stair; the bottom of the box touches the top of a box, thrown earlier. We only consider touching of the horizontal sides of stairs and boxes, at that touching with the corners isn't taken into consideration. Specifically, that implies that a box with width wi cannot touch the stair number wi + 1.You are given the description of the staircase and the sequence in which Dima threw the boxes at it. For each box, determine how high the bottom of the box after landing will be. Consider a box to fall after the previous one lands.", "c_code": "int solution() {\n\n int n;\n int m;\n int i;\n int w;\n int h;\n\n double max = 0;\n\n scanf(\"%d\", &n);\n\n int v[n];\n\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &v[i]);\n }\n\n scanf(\"%d\", &m);\n\n for (i = 0; i < m; i++) {\n scanf(\"%d %d\", &w, &h);\n if (max > v[w - 1]) {\n max = max;\n } else {\n max = v[w - 1];\n }\n printf(\"%.f\\n\", max);\n max = max + h;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer);\n buffer = String::new();\n io::stdin().read_line(&mut buffer);\n\n let a: Vec = buffer\n .split_whitespace()\n .filter_map(|str| str.parse::().ok())\n .collect();\n buffer = String::new();\n io::stdin().read_line(&mut buffer);\n let m = buffer\n .split_whitespace()\n .filter_map(|str| str.parse::().ok())\n .next()\n .unwrap();\n let mut last: i64 = -1;\n for _i in 0..m {\n buffer = String::new();\n io::stdin().read_line(&mut buffer);\n let o: Vec = buffer\n .split_whitespace()\n .filter_map(|str| str.parse::().ok())\n .collect();\n let w = o[0];\n let h = o[1];\n let answer = std::cmp::max(last, a[w as usize - 1]);\n println!(\"{}\", answer);\n last = answer + h;\n }\n}", "difficulty": "medium"} {"problem_id": "1324", "problem_description": "One day, Vogons wanted to build a new hyperspace highway through a distant system with $$$n$$$ planets. The $$$i$$$-th planet is on the orbit $$$a_i$$$, there could be multiple planets on the same orbit. It's a pity that all the planets are on the way and need to be destructed.Vogons have two machines to do that. The first machine in one operation can destroy any planet at cost of $$$1$$$ Triganic Pu. The second machine in one operation can destroy all planets on a single orbit in this system at the cost of $$$c$$$ Triganic Pus. Vogons can use each machine as many times as they want.Vogons are very greedy, so they want to destroy all planets with minimum amount of money spent. Can you help them to know the minimum cost of this project?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int c;\n int count = 0;\n scanf(\"%d %d\", &n, &c);\n\n int b[101];\n for (int i = 0; i < 101; i++) {\n b[i] = 0;\n }\n\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b[a[i]]++;\n }\n\n for (int i = 0; i < 101; i++) {\n if (b[i] > c) {\n count = count + c;\n } else {\n count = count + b[i];\n }\n }\n\n printf(\"%d\\n\", count);\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 line1 = line_iter.next().unwrap().unwrap();\n let (n_str, c_str) = line1.split_once(' ').unwrap();\n let _n = n_str.parse::().unwrap();\n let c = c_str.parse::().unwrap();\n\n let mut orbit_counts = [0_usize; 101];\n\n let line2 = line_iter.next().unwrap().unwrap();\n let orbit_iter = line2\n .split(' ')\n .map(|orbit_str| orbit_str.parse::().unwrap());\n for orbit in orbit_iter {\n orbit_counts[orbit] += 1;\n }\n\n let mut min_cost: usize = 0;\n for orbit_count in orbit_counts {\n if orbit_count > 0 {\n min_cost += orbit_count.min(c);\n }\n }\n\n println!(\"{}\", min_cost);\n }\n}", "difficulty": "medium"} {"problem_id": "1325", "problem_description": "Sayaka Saeki is a member of the student council, which has $$$n$$$ other members (excluding Sayaka). The $$$i$$$-th member has a height of $$$a_i$$$ millimeters.It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.A pair of two consecutive members $$$u$$$ and $$$v$$$ on a line is considered photogenic if their average height is an integer, i.e. $$$\\frac{a_u + a_v}{2}$$$ is an integer.Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int odd = 0;\n int even = 0;\n int o[n];\n int e[n];\n for (int i = 0; i < n; i++) {\n o[i] = 0;\n e[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n if (a[i] % 2 == 1) {\n o[odd] = a[i];\n odd++;\n }\n if (a[i] % 2 == 0) {\n e[even] = a[i];\n even++;\n }\n }\n for (int i = 0; i < odd; i++) {\n printf(\"%d \", o[i]);\n }\n for (int i = 0; i < even; i++) {\n printf(\"%d \", e[i]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let t: i64 = line.trim().parse().unwrap();\n\n for _ in 0..t {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let _n: i64 = line.trim().parse().unwrap();\n\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let mut a: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n a.sort_by_key(|x| x % 2);\n\n for item in a {\n print!(\"{} \", item);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1326", "problem_description": "You are given four positive integers $$$n$$$, $$$m$$$, $$$a$$$, $$$b$$$ ($$$1 \\le b \\le n \\le 50$$$; $$$1 \\le a \\le m \\le 50$$$). Find any such rectangular matrix of size $$$n \\times m$$$ that satisfies all of the following conditions: each row of the matrix contains exactly $$$a$$$ ones; each column of the matrix contains exactly $$$b$$$ ones; all other elements are zeros. If the desired matrix does not exist, indicate this.For example, for $$$n=3$$$, $$$m=6$$$, $$$a=2$$$, $$$b=1$$$, there exists a matrix satisfying the conditions above:$$$$$$ \\begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\\ 1 & 0 & 0 & 1 & 0 & 0 \\\\ 0 & 0 & 1 & 0 & 1 & 0 \\end{vmatrix} $$$$$$", "c_code": "int solution() {\n int t;\n int m;\n int n;\n int i;\n int b;\n int a;\n int j;\n int count;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d%d%d%d\", &n, &m, &a, &b);\n if (n * a != m * b) {\n printf(\"NO\\n\");\n continue;\n }\n printf(\"YES\\n\");\n int aa[51][51];\n i = -1;\n while (++i < n) {\n j = -1;\n while (++j < m) {\n aa[i][j] = 0;\n }\n }\n i = 0;\n j = 0;\n count = 0;\n while (count < n * a) {\n if (aa[i % n][j % m] == 0) {\n aa[i % n][j % m] = 1;\n count++;\n i++;\n j++;\n } else {\n i++;\n }\n }\n i = -1;\n while (++i < n) {\n j = -1;\n while (++j < m) {\n putchar(aa[i][j] + '0');\n }\n putchar('\\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 m: usize = itr.next().unwrap().parse().unwrap();\n let a: usize = itr.next().unwrap().parse().unwrap();\n let b: usize = itr.next().unwrap().parse().unwrap();\n if n * a != m * b {\n writeln!(out, \"NO\").ok();\n } else {\n writeln!(out, \"YES\").ok();\n let mut ans: Vec> = vec![vec![0; m]; n];\n\n let mut x = 0;\n for i in 0..n {\n for _ in 0..a {\n ans[i][x] = 1;\n x = (x + 1) % m;\n }\n }\n\n for i in 0..n {\n for j in 0..m {\n write!(out, \"{}\", ans[i][j]).ok();\n }\n writeln!(out).ok();\n }\n }\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "easy"} {"problem_id": "1327", "problem_description": "Ania has a large integer $$$S$$$. Its decimal representation has length $$$n$$$ and doesn't contain any leading zeroes. Ania is allowed to change at most $$$k$$$ digits of $$$S$$$. She wants to do it in such a way that $$$S$$$ still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n char num[n];\n scanf(\"%s\", num);\n if (n == 1 && k >= 1) {\n printf(\"%c\\n\", '0');\n return 0;\n } else {\n if (num[0] != '1' && k > 0) {\n num[0] = '1';\n k--;\n }\n int y = 1;\n\n while (k > 0 && y <= n - 1) {\n if (num[y] != '0') {\n num[y] = '0';\n k--;\n }\n y++;\n }\n\n printf(\"%s\", num);\n printf(\"\\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 mut input_iter = input.split_whitespace();\n let _n = input_iter.next();\n let mut k = input_iter.next().unwrap().parse::().unwrap();\n\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let mut s = input.trim().bytes().map(|x| x - b'0').collect::>();\n\n if k != 0 {\n let mut start = 0;\n if s.len() > 1 {\n start = 1;\n if s[0] != 1 {\n s[0] = 1;\n k -= 1;\n }\n }\n\n if k != 0 {\n for d in start..s.len() {\n if s[d] != 0 {\n s[d] = 0;\n k -= 1;\n if k == 0 {\n break;\n }\n }\n }\n }\n }\n\n let out = std::io::stdout();\n let mut buf = std::io::BufWriter::new(out.lock());\n\n for d in s.iter() {\n write!(buf, \"{}\", d);\n }\n writeln!(buf);\n}", "difficulty": "medium"} {"problem_id": "1328", "problem_description": "Recently, Norge found a string $$$s = s_1 s_2 \\ldots s_n$$$ consisting of $$$n$$$ lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string $$$s$$$. Yes, all $$$\\frac{n (n + 1)}{2}$$$ of them!A substring of $$$s$$$ is a non-empty string $$$x = s[a \\ldots b] = s_{a} s_{a + 1} \\ldots s_{b}$$$ ($$$1 \\leq a \\leq b \\leq n$$$). For example, \"auto\" and \"ton\" are substrings of \"automaton\".Shortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only $$$k$$$ Latin letters $$$c_1, c_2, \\ldots, c_k$$$ out of $$$26$$$.After that, Norge became interested in how many substrings of the string $$$s$$$ he could still type using his broken keyboard. Help him to find this number.", "c_code": "int solution() {\n\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n char c;\n scanf(\"%c\", &c);\n char input[n];\n int chars[26];\n for (int i = 0; i < 26; i++) {\n chars[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%c\", &c);\n input[i] = c;\n }\n scanf(\"%c\", &c);\n for (int i = 0; i < k; i++) {\n scanf(\"%c\", &c);\n int tmp = c - 'a';\n chars[tmp] = 1;\n scanf(\"%c\", &c);\n }\n long long count = 0;\n long long res = 0;\n for (int i = 0; i < n; i++) {\n if (chars[input[i] - 'a'] == 1) {\n count++;\n } else {\n res += (count * (count + 1)) / 2;\n count = 0;\n }\n }\n res += (count * (count + 1)) / 2;\n\n printf(\"%lld\", res);\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _b1 = stdin().read_line(&mut str).unwrap();\n let mut iter = str.split_whitespace();\n let m: i64 = iter.next().unwrap().parse().unwrap();\n let _n: i64 = iter.next().unwrap().parse().unwrap();\n\n let mut test_string = String::new();\n let _b1 = stdin().read_line(&mut test_string).unwrap();\n\n str.clear();\n let _b1 = stdin().read_line(&mut str).unwrap();\n let mut iter = str.split_whitespace();\n let mut work: Vec = Vec::new();\n loop {\n match iter.next() {\n Some(c) => work.push(c.chars().next().unwrap()),\n None => break,\n }\n }\n let mut last: i64 = -1;\n let mut total: i64 = 0;\n test_string = test_string.trim().to_string();\n test_string.push('1');\n let mut char_iter = test_string.chars();\n for i in 0..(m + 1) {\n let c = char_iter.next().unwrap();\n if work.contains(&c) {\n } else {\n let x = i - last - 1;\n total += x * (x + 1);\n last = i;\n }\n }\n\n println!(\"{}\", total / 2);\n}", "difficulty": "hard"} {"problem_id": "1329", "problem_description": "Polycarp remembered the $$$2020$$$-th year, and he is happy with the arrival of the new $$$2021$$$-th year. To remember such a wonderful moment, Polycarp wants to represent the number $$$n$$$ as the sum of a certain number of $$$2020$$$ and a certain number of $$$2021$$$.For example, if: $$$n=4041$$$, then the number $$$n$$$ can be represented as the sum $$$2020 + 2021$$$; $$$n=4042$$$, then the number $$$n$$$ can be represented as the sum $$$2021 + 2021$$$; $$$n=8081$$$, then the number $$$n$$$ can be represented as the sum $$$2020 + 2020 + 2020 + 2021$$$; $$$n=8079$$$, then the number $$$n$$$ cannot be represented as the sum of the numbers $$$2020$$$ and $$$2021$$$. Help Polycarp to find out whether the number $$$n$$$ can be represented as the sum of a certain number of numbers $$$2020$$$ and a certain number of numbers $$$2021$$$.", "c_code": "int solution() {\n int n = 0;\n int num = 0;\n scanf(\"%d\", &n);\n while (n--) {\n scanf(\"%d\", &num);\n int a = num / 2020;\n int b = num % 2020;\n if (a >= b) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n\n let mut hash = HashSet::new();\n hash.insert(0u64);\n\n let mut producables = HashSet::new();\n let mut a = 0;\n while a <= 1_000_000 {\n let mut b = 0;\n while a + b <= 1_000_000 {\n producables.insert(a + b);\n b += 2021;\n }\n a += 2020\n }\n let mut t = String::new();\n let _ = stdin.read_line(&mut t);\n let t: u64 = t.trim().parse().unwrap();\n for _ in 0..t {\n let mut n = String::new();\n let _ = stdin.read_line(&mut n);\n let n: u64 = n.trim().parse().unwrap();\n match producables.contains(&n) {\n true => println!(\"yes\"),\n false => println!(\"no\"),\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1330", "problem_description": "There is a bookshelf which can fit $$$n$$$ books. The $$$i$$$-th position of bookshelf is $$$a_i = 1$$$ if there is a book on this position and $$$a_i = 0$$$ otherwise. It is guaranteed that there is at least one book on the bookshelf.In one move, you can choose some contiguous segment $$$[l; r]$$$ consisting of books (i.e. for each $$$i$$$ from $$$l$$$ to $$$r$$$ the condition $$$a_i = 1$$$ holds) and: Shift it to the right by $$$1$$$: move the book at index $$$i$$$ to $$$i + 1$$$ for all $$$l \\le i \\le r$$$. This move can be done only if $$$r+1 \\le n$$$ and there is no book at the position $$$r+1$$$. Shift it to the left by $$$1$$$: move the book at index $$$i$$$ to $$$i-1$$$ for all $$$l \\le i \\le r$$$. This move can be done only if $$$l-1 \\ge 1$$$ and there is no book at the position $$$l-1$$$. Your task is to find the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without any gaps).For example, for $$$a = [0, 0, 1, 0, 1]$$$ there is a gap between books ($$$a_4 = 0$$$ when $$$a_3 = 1$$$ and $$$a_5 = 1$$$), for $$$a = [1, 1, 0]$$$ there are no gaps between books and for $$$a = [0, 0,0]$$$ there are also no gaps between books.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d%*c\", &t);\n for (int i = 0; i < t; i++) {\n int n = 0;\n scanf(\"%d%*c\", &n);\n int startCounting = 0;\n int tempSum = 0;\n int res = 0;\n for (int j = 0; j < n; j++) {\n int temp = 0;\n scanf(\"%d%*c\", &temp);\n if (temp == 0) {\n tempSum++;\n }\n if (temp == 1 && startCounting == 0) {\n tempSum = 0;\n startCounting = 1;\n }\n if (temp == 1 && startCounting == 1) {\n res += tempSum;\n tempSum = 0;\n }\n }\n printf(\"%d\\n\", res);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = \"\".split_ascii_whitespace();\n let mut read = || loop {\n if let Some(word) = input.next() {\n break word;\n }\n input = {\n let mut input = \"\".to_owned();\n io::stdin().read_line(&mut input).unwrap();\n if input.is_empty() {\n panic!(\"reached EOF\");\n }\n Box::leak(input.into_boxed_str()).split_ascii_whitespace()\n };\n };\n\n\n let t = read!(usize);\n for _ in 0..t {\n let n = read!(usize);\n let mut a: Vec = vec![0; 0];\n for _ in 0..n {\n a.push(read!(i64));\n }\n\n let mut r = 0;\n let mut f = false;\n let mut c = 0;\n for i in 0..n {\n if a[i] == 1 {\n if f {\n r += c;\n }\n c = 0;\n f = true;\n } else {\n c += 1;\n }\n }\n println!(\"{}\", r);\n }\n}", "difficulty": "medium"} {"problem_id": "1331", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have N weights indexed 1 to N. The \bmass of the weight indexed i is W_i.

\n

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \\leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.

\n

Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 100
  • \n
  • 1 \\leq W_i \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nW_1 W_2 ... W_{N-1} W_N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible absolute difference of S_1 and S_2.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n1 3 1 1\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.

\n
\n
\n
\n
\n
\n

Sample Input 3

8\n27 23 76 2 3 5 62 52\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int total = 0;\n int s1 = 0;\n int s2 = 0;\n int s3 = 0;\n int w[100];\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &w[i]);\n total = w[i] + total;\n }\n int ans = total;\n for (int i = 0; i < n; i++) {\n s1 = s1 + w[i];\n s2 = total - s1;\n s3 = abs(s2 - s1);\n if (s3 < ans) {\n ans = s3;\n\n } else {\n }\n }\n\n printf(\"%d\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let n: usize = {\n let mut n = String::new();\n io::stdin().read_line(&mut n).unwrap();\n n.trim_end().to_owned().trim().parse().unwrap()\n };\n\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let w: Vec = buf.split_whitespace().map(|s| s.parse().unwrap()).collect();\n let _sum: i32 = w.iter().sum();\n let mut bb: Vec = vec![];\n for i in 0..n {\n let (a, b) = w.split_at(i);\n let asum: i32 = a.iter().sum();\n let bsum: i32 = b.iter().sum();\n bb.push(i32::abs(asum - bsum));\n }\n bb.sort();\n println!(\"{}\", bb[0]);\n}", "difficulty": "medium"} {"problem_id": "1332", "problem_description": "You are given two strings $$$s$$$ and $$$t$$$. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by $$$1$$$. You can't choose a string if it is empty.For example: by applying a move to the string \"where\", the result is the string \"here\", by applying a move to the string \"a\", the result is an empty string \"\". You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.Write a program that finds the minimum number of moves to make two given strings $$$s$$$ and $$$t$$$ equal.", "c_code": "int solution() {\n char *A = malloc(sizeof(char) * 200005);\n char *B = malloc(sizeof(char) * 200005);\n scanf(\"%s %s\", A, B);\n int length_A = strlen(A);\n int length_B = strlen(B);\n int totalamt = length_A + length_B;\n for (int index_A = length_A - 1, index_B = length_B - 1;\n index_A >= 0 && index_B >= 0;) {\n if (A[index_A] != B[index_B]) {\n break;\n }\n index_A--;\n index_B--;\n totalamt -= 2;\n }\n printf(\"%i\", totalamt);\n free(A);\n free(B);\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 let t = get().as_bytes();\n let mut i = s.len() - 1;\n let mut j = t.len() - 1;\n\n use std::cmp::min;\n\n for _ in 0..min(s.len(), t.len()) {\n if s[i] != t[j] {\n println!(\"{}\", i + j + 2);\n break;\n }\n if i == 0 || j == 0 {\n println!(\"{}\", s.len() + t.len() - min(s.len(), t.len()) * 2);\n break;\n }\n i -= 1;\n j -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "1333", "problem_description": "You are given a digital clock with $$$n$$$ digits. Each digit shows an integer from $$$0$$$ to $$$9$$$, so the whole clock shows an integer from $$$0$$$ to $$$10^n-1$$$. The clock will show leading zeroes if the number is smaller than $$$10^{n-1}$$$.You want the clock to show $$$0$$$ with as few operations as possible. In an operation, you can do one of the following: decrease the number on the clock by $$$1$$$, or swap two digits (you can choose which digits to swap, and they don't have to be adjacent). Your task is to determine the minimum number of operations needed to make the clock show $$$0$$$.", "c_code": "int solution() {\n int tc;\n scanf(\"%d\", &tc);\n\n for (int i = 0; i < tc; i++) {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%1d\", &arr[j]);\n }\n int sum = 0;\n for (int j = 0; j < n; j++) {\n if (j == n - 1 && arr[j] > 0) {\n sum += arr[j];\n } else if (j != n - 1 && arr[j] > 0) {\n sum += (arr[j] + 1);\n }\n }\n printf(\"%d\\n\", sum);\n }\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut string = String::new();\n stdin.read_line(&mut string).unwrap();\n for _ in 0..string.trim().parse::().unwrap() {\n string.clear();\n stdin.read_line(&mut string).unwrap();\n string.clear();\n stdin.read_line(&mut string).unwrap();\n let vec = string\n .trim()\n .chars()\n .map(|f| f.to_digit(10).unwrap())\n .collect::>();\n let mut re = 0;\n for i in 0..vec.len() {\n if vec[i] != 0 {\n re += vec[i];\n if i != vec.len() - 1 {\n re += 1\n }\n }\n }\n println!(\"{}\", re);\n }\n}", "difficulty": "hard"} {"problem_id": "1334", "problem_description": "

The Balance of the World

\n\n\n\n
\n\n

\nThe world should be finely balanced. Positive vs. negative,\nlight vs. shadow, and left vs. right brackets.\nYour mission is to write a program that judges whether a string is balanced\nwith respect to brackets so that we can observe the balance of the\nworld.\n

\n\n\n\n

\nA string that will be given to the program may have two kinds of brackets,\nround (“( )”) and square (“[ ]”).\nA string is balanced if and only if the following conditions hold.\n

    \n
  • For every left round bracket (“(”), there is a corresponding\n right round bracket (“)”) in the following part of the string.
  • \n
  • For every left square bracket (“[”), there is a corresponding\n right square bracket (“]”) in the following part of the string.
  • \n
  • For every right bracket, there is a left bracket corresponding to it.
  • \n
  • Correspondences of brackets have to be one to one, that is, a\n single bracket never corresponds to two or more brackets.
  • \n
  • For every pair of corresponding left and right brackets,\n the substring between them is balanced.
  • \n
\n

\n\n
\n\n

Input

\n\n
\n\n

\nThe input consists of one or more lines, each of which being a dataset.\nA dataset is a string that consists of English alphabets,\nspace characters, and two kinds of brackets, round (“( )”) and square (“[ ]”),\nterminated by a period. You can assume that every line has 100\ncharacters or less.\nThe line formed by a single period indicates the end of the input,\nwhich is not a dataset.\n

\n\n
\n\n

Output

\n
\n\n

\nFor each dataset, output “yes” if the\nstring is balanced, or “no” otherwise, in a line.\nThere may not be any extra characters in the output.\n

\n\n
\n\n

Sample Input

\n
\n
\nSo when I die (the [first] I will see in (heaven) is a score list).\n[ first in ] ( first out ).\nHalf Moon tonight (At least it is better than no Moon at all].\nA rope may form )( a trail in a maze.\nHelp( I[m being held prisoner in a fortune cookie factory)].\n([ (([( [ ] ) ( ) (( ))] )) ]).\n .\n.\n
\n
\n\n

Output for the Sample Input

\n
\n
\nyes\nyes\nno\nno\nno\nyes\nyes\n
\n
", "c_code": "int solution(void) {\n char s[128];\n char stack[128];\n while (fgets(s, 128, stdin)) {\n int n = strlen(s) - 1;\n char *p = strchr(s, '\\n');\n int i;\n int ok = 1;\n int sp = 0;\n if (p) {\n *p = 0;\n }\n if (*s == '.' && n == 1) {\n break;\n }\n\n for (i = 0; i < n; ++i) {\n switch (s[i]) {\n case '(':\n stack[sp++] = s[i];\n break;\n case ')':\n if (!sp || stack[--sp] != '(') {\n ok = 0;\n }\n break;\n case '[':\n stack[sp++] = s[i];\n break;\n case ']':\n if (!sp || stack[--sp] != '[') {\n ok = 0;\n }\n break;\n }\n }\n ok = ok && sp == 0;\n puts(ok ? \"yes\" : \"no\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n for line in stdin.lock().lines() {\n let chars: Vec = line.unwrap().chars().collect();\n\n if chars[0] == '.' && chars.len() <= 1 {\n return;\n }\n\n let mut stack: Vec = Vec::new();\n\n for char in chars {\n if char == '(' || char == '[' {\n stack.push(char);\n } else if char == ')' {\n if stack.is_empty() || stack.last().unwrap() != &'(' {\n stack.push(char);\n break;\n }\n stack.pop();\n } else if char == ']' {\n if stack.is_empty() || stack.last().unwrap() != &'[' {\n stack.push(char);\n break;\n }\n stack.pop();\n }\n }\n\n if !stack.is_empty() {\n println!(\"no\");\n } else {\n println!(\"yes\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1335", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Caracal is fighting with a monster.

\n

The health of the monster is H.

\n

Caracal can attack by choosing one monster. When a monster is attacked, depending on that monster's health, the following happens:

\n
    \n
  • If the monster's health is 1, it drops to 0.
  • \n
  • If the monster's health, X, is greater than 1, that monster disappears. Then, two new monsters appear, each with the health of \\lfloor X/2 \\rfloor.
  • \n
\n

(\\lfloor r \\rfloor denotes the greatest integer not exceeding r.)

\n

Caracal wins when the healths of all existing monsters become 0 or below.

\n

Find the minimum number of attacks Caracal needs to make before winning.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H \\leq 10^{12}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H\n
\n
\n
\n
\n
\n

Output

Find the minimum number of attacks Caracal needs to make before winning.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

When Caracal attacks the initial monster, it disappears, and two monsters appear, each with the health of 1.

\n

Then, Caracal can attack each of these new monsters once and win with a total of three attacks.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1000000000000\n
\n
\n
\n
\n
\n

Sample Output 3

1099511627775\n
\n
\n
", "c_code": "int solution() {\n unsigned long n = 0;\n unsigned long r = 1;\n scanf(\"%lu\", &n);\n while (n != 0) {\n n >>= 1;\n r <<= 1;\n }\n\n printf(\"%lu\", r - 1);\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let stdin = std::io::stdin();\n stdin.lock().read_to_string(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n let h = iter.next().unwrap().parse::().unwrap();\n\n let mut ans = 0_usize;\n for i in (0..41).rev() {\n if (h & 1 << i) != 0_usize {\n for j in 0..i + 1 {\n ans += 1 << j;\n }\n break;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1336", "problem_description": "You are given two arrays of integers $$$a_1,\\ldots,a_n$$$ and $$$b_1,\\ldots,b_m$$$.Your task is to find a non-empty array $$$c_1,\\ldots,c_k$$$ that is a subsequence of $$$a_1,\\ldots,a_n$$$, and also a subsequence of $$$b_1,\\ldots,b_m$$$. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it.A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) elements. For example, $$$[3,1]$$$ is a subsequence of $$$[3,2,1]$$$ and $$$[4,3,1]$$$, but not a subsequence of $$$[1,3,3,7]$$$ and $$$[3,10,4]$$$.", "c_code": "int solution() {\n int tc;\n scanf(\"%d\", &tc);\n while (tc--) {\n int a;\n int b;\n scanf(\"%d %d\", &a, &b);\n int input1[a];\n int input2[b];\n int ctr1[1005] = {0};\n int ctr2[1005] = {0};\n for (int i = 0; i < a; i++) {\n scanf(\"%d\", &input1[i]);\n ctr1[input1[i]]++;\n }\n for (int i = 0; i < b; i++) {\n scanf(\"%d\", &input2[i]);\n ctr2[input2[i]]++;\n }\n int flag = 1;\n for (int i = 0; i < 1005; i++) {\n if (ctr1[i] > 0 && ctr2[i] > 0) {\n printf(\"YES\\n\");\n printf(\"1 %d\\n\", i);\n flag = 0;\n break;\n }\n }\n if (flag) {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut line: String = String::new();\n stdin.read_line(&mut line).unwrap();\n let ntc: i32 = line.trim().parse().unwrap();\n\n for _ in 0..ntc {\n stdin.read_line(&mut line).unwrap();\n\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let a: Vec = line\n .split_whitespace()\n .map(|s| s.parse().expect(\"parse error\"))\n .collect();\n\n line.clear();\n stdin.read_line(&mut line).unwrap();\n let mut b: Vec = line\n .split_whitespace()\n .map(|s| s.parse().expect(\"parse error\"))\n .collect();\n b.sort();\n\n let mut ans: i32 = 0;\n for x in a {\n let mut fr = 0;\n let mut to = b.len() - 1;\n while fr < to {\n let mid = (fr + to) / 2;\n if x > b[mid] {\n fr = mid + 1;\n } else {\n to = mid;\n }\n }\n if b[fr] == x {\n ans = x;\n break;\n }\n }\n\n if ans == 0 {\n println!(\"NO\")\n } else {\n println!(\"YES\\n1 {}\", ans);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1337", "problem_description": "You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k > 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.", "c_code": "int solution() {\n int N;\n scanf(\"%d\", &N);\n while (N--) {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int a[n][m];\n int goodness = 1;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &a[i][j]);\n if (a[i][j] > 4 ||\n (a[i][j] == 4 && (i == 0 || i == n - 1 || j == 0 || j == m - 1)) ||\n (a[i][j] == 3 && ((i == 0 && (j == 0 || j == m - 1)) ||\n (i == n - 1 && (j == 0 || j == m - 1))))) {\n goodness = 0;\n }\n }\n }\n if (goodness == 0) {\n printf(\"NO\\n\");\n continue;\n }\n printf(\"YES\\n\");\n for (int i = 1; i < n - 1; i++) {\n for (int j = 1; j < m - 1; j++) {\n a[i][j] = 4;\n }\n }\n for (int i = 1; i < n - 1; i++) {\n a[i][0] = (a[i][m - 1] = 3);\n }\n for (int i = 1; i < m - 1; i++) {\n a[0][i] = (a[n - 1][i] = 3);\n }\n a[0][0] = a[0][m - 1] = a[n - 1][0] = a[n - 1][m - 1] = 2;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n printf(\"%d \", a[i][j]);\n }\n printf(\"\\n\");\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, m) = (xs[0], xs[1]);\n let mut ok = true;\n for i in 0..n {\n let xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n if i == 0 || i == n - 1 {\n for j in 0..m {\n if j == 0 || j == m - 1 {\n if xs[j] > 2 {\n ok = false;\n }\n } else {\n if xs[j] > 3 {\n ok = false;\n }\n }\n }\n } else {\n for j in 0..m {\n if j == 0 || j == m - 1 {\n if xs[j] > 3 {\n ok = false;\n }\n } else {\n if xs[j] > 4 {\n ok = false;\n }\n }\n }\n }\n }\n println!(\"{}\", if ok { \"YES\" } else { \"NO\" });\n if ok {\n for i in 0..n {\n print!(\"{}\", if i == 0 || i == n - 1 { 2 } else { 3 });\n for _ in 1..(m - 1) {\n print!(\" {}\", if i == 0 || i == n - 1 { 3 } else { 4 });\n }\n println!(\" {}\", if i == 0 || i == n - 1 { 2 } else { 3 });\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1338", "problem_description": "Shohag has an integer sequence $$$a_1, a_2, \\ldots, a_n$$$. He can perform the following operation any number of times (possibly, zero): Select any positive integer $$$k$$$ (it can be different in different operations). Choose any position in the sequence (possibly the beginning or end of the sequence, or in between any two elements) and insert $$$k$$$ into the sequence at this position. This way, the sequence $$$a$$$ changes, and the next operation is performed on this changed sequence. For example, if $$$a=[3,3,4]$$$ and he selects $$$k = 2$$$, then after the operation he can obtain one of the sequences $$$[\\underline{2},3,3,4]$$$, $$$[3,\\underline{2},3,4]$$$, $$$[3,3,\\underline{2},4]$$$, or $$$[3,3,4,\\underline{2}]$$$.Shohag wants this sequence to satisfy the following condition: for each $$$1 \\le i \\le |a|$$$, $$$a_i \\le i$$$. Here, $$$|a|$$$ denotes the size of $$$a$$$.Help him to find the minimum number of operations that he has to perform to achieve this goal. We can show that under the constraints of the problem it's always possible to achieve this goal in a finite number of operations.", "c_code": "int solution() {\n int c = 0;\n int d = 0;\n int n;\n int x = 0;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &x);\n int a[x + 1];\n for (int j = 1; j < x + 1; j++) {\n scanf(\"%d\", &a[j]);\n if (a[j] > j && (j + c < a[j])) {\n d = a[j] - (j + c);\n c = c + d;\n }\n }\n printf(\"%d\\n\", c);\n c = 0;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut nn = String::new();\n stdin().read_line(&mut nn).unwrap();\n let mut n: u32 = nn.trim().parse().unwrap();\n while n > 0 {\n let mut _numberofe = String::new();\n stdin().read_line(&mut _numberofe).unwrap();\n let _numberofe: usize = _numberofe.trim().parse().unwrap();\n n -= 1;\n let mut numbers = String::new();\n stdin().read_line(&mut numbers).unwrap();\n let numbers: Vec = numbers\n .split_whitespace()\n .map(|s| s.trim().parse().unwrap())\n .collect();\n let mut ans: i64 = 0;\n for i in 0.._numberofe {\n ans = Ord::max(ans, numbers[i] - (i + 1) as i64);\n }\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1339", "problem_description": "In an ICPC contest, balloons are distributed as follows: Whenever a team solves a problem, that team gets a balloon. The first team to solve a problem gets an additional balloon. A contest has 26 problems, labelled $$$\\textsf{A}$$$, $$$\\textsf{B}$$$, $$$\\textsf{C}$$$, ..., $$$\\textsf{Z}$$$. You are given the order of solved problems in the contest, denoted as a string $$$s$$$, where the $$$i$$$-th character indicates that the problem $$$s_i$$$ has been solved by some team. No team will solve the same problem twice.Determine the total number of balloons that the teams received. Note that some problems may be solved by none of the teams.", "c_code": "int solution(void) {\n int test_cases = 0;\n int problems = 0;\n int index = 0;\n int counting = 0;\n int letter[26] = {0};\n scanf(\"%d\", &test_cases);\n for (int i = 0; i < test_cases; i++) {\n scanf(\"%d\", &problems);\n char ara[problems + 1];\n scanf(\"%s\", ara);\n for (int j = 0; j < problems; j++) {\n index = ara[j] - 'A';\n if (letter[index] == 0) {\n counting = counting + 2;\n letter[index] = 1;\n } else {\n counting++;\n }\n }\n printf(\"%d\\n\", counting);\n counting = 0;\n for (int k = 0; k < 26; k++) {\n letter[k] = 0;\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).expect(\"input err\");\n let t: i32 = input.trim().parse().expect(\"input is not i32\");\n for _ in 0..t {\n input.clear();\n io::stdin().read_line(&mut input).expect(\"input err\");\n let n: usize = input.trim().parse().expect(\"input is not usize\");\n\n input.clear();\n io::stdin().read_line(&mut input).expect(\"input err\");\n let problem_set: std::collections::HashSet = input.trim().chars().collect();\n println!(\"{}\", problem_set.len() + n);\n }\n}", "difficulty": "medium"} {"problem_id": "1340", "problem_description": "You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\\lfloor \\frac{n + 1}{2} \\rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\\ldots,a_r$$$ for some $$$1 \\leq l \\leq r \\leq n$$$, its length is $$$r - l + 1$$$.", "c_code": "int solution() {\n int n;\n int k;\n int l;\n int r;\n int median;\n int min;\n int max;\n int *a;\n int *b;\n\n scanf(\"%d%d\", &n, &k);\n a = malloc(n * sizeof(int));\n b = malloc(n * sizeof(int));\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n l = 1;\n r = n + 1;\n while (l + 1 < r) {\n median = (l + r) >> 1;\n b[0] = (a[0] < median) ? -1 : 1;\n for (int i = 1; i < n; i++) {\n b[i] = b[i - 1] + ((a[i] < median) ? -1 : 1);\n }\n\n min = 0;\n max = b[k - 1];\n for (int i = k; i < n; i++) {\n min = (min < b[i - k]) ? min : b[i - k];\n max = (max > b[i] - min) ? max : b[i] - min;\n }\n\n if (max > 0) {\n l = median;\n } else {\n r = median;\n }\n }\n\n printf(\"%d\", l);\n}", "rust_code": "fn solution() -> std::io::Result<()> {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf)?;\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let (n, k) = scan!(usize, usize);\n let a = scan!([usize; n]);\n let ok = |x: usize| -> bool {\n let mut b = a\n .iter()\n .map(|&y| if y >= x { 1 } else { -1 })\n .collect::>();\n for i in 1..n {\n b[i] += b[i - 1];\n }\n let mut prfmin = 0;\n let mut flag = false;\n for i in k - 1..n {\n if b[i] - prfmin > 0 {\n flag = true;\n\n break;\n }\n prfmin = prfmin.min(b[i - (k - 1)]);\n }\n flag\n };\n let (mut l, mut r) = (1, n);\n while l < r {\n let m = (l + r).div_ceil(2);\n\n if ok(m) {\n l = m;\n } else {\n r = m - 1;\n }\n }\n print!(\"{}\", l);\n print!(\"\\n\");\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1341", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Given is a string S. Each character in S is either a digit (0, ..., 9) or ?.

\n

Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0.

\n

Since the answer can be enormous, print the count modulo 10^9+7.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • S is a string consisting of digits (0, ..., 9) and ?.
  • \n
  • 1 \\leq |S| \\leq 10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

\n

Print the number of integers satisfying the condition, modulo 10^9+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

??2??5\n
\n
\n
\n
\n
\n

Sample Output 1

768\n
\n

For example, 482305, 002865, and 972665 satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 2

?44\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

Only 044 satisfies the condition.

\n
\n
\n
\n
\n
\n

Sample Input 3

7?4\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

We may not be able to produce an integer satisfying the condition.

\n
\n
\n
\n
\n
\n

Sample Input 4

?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???\n
\n
\n
\n
\n
\n

Sample Output 4

153716888\n
\n
\n
", "c_code": "int solution(void) {\n char s[100001];\n scanf(\"%s\", s);\n\n int s_len = strlen(s);\n\n long reminder_divided_by_13[13] = {0};\n\n reminder_divided_by_13[0] = 1;\n\n const int mod = 1000000007;\n\n long digit = 1;\n\n for (int i = s_len - 1; i >= 0; i--) {\n long next_reminder[13] = {0};\n\n if (s[i] == '?') {\n for (int j = 0; j < 10; j++) {\n for (int k = 0; k < 13; k++) {\n next_reminder[(j * digit + k) % 13] += reminder_divided_by_13[k];\n next_reminder[(j * digit + k) % 13] %= mod;\n }\n }\n } else {\n int j = s[i] - '0';\n for (int k = 0; k < 13; k++) {\n next_reminder[(j * digit + k) % 13] += reminder_divided_by_13[k];\n next_reminder[(j * digit + k) % 13] %= mod;\n }\n }\n digit *= 10;\n digit %= 13;\n\n for (int j = 0; j < 13; j++) {\n reminder_divided_by_13[j] = next_reminder[j];\n }\n }\n\n printf(\"%ld\\n\", reminder_divided_by_13[5]);\n return 0;\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 let mut dp = vec![0; 13];\n let n = s.len();\n if s[n - 1] == b'?' {\n for i in 0..10 {\n dp[i] = 1;\n }\n } else {\n let x = (s[n - 1] - b'0') as usize;\n dp[x] = 1;\n }\n let mut p = 10;\n let md = 1_000_000_007;\n for i in 1..s.len() {\n let i = (s.len() - 1) - i;\n let mut tmp = vec![0; 13];\n if s[i] == b'?' {\n for c in 0..10 {\n let x = ((c * p) % 13) as usize;\n for j in 0..13 {\n tmp[(j + x) % 13] += dp[j];\n tmp[(j + x) % 13] %= md;\n }\n }\n } else {\n let c = (s[i] - b'0') as i64;\n let x = ((c * p) % 13) as usize;\n for j in 0..13 {\n tmp[(j + x) % 13] = dp[j];\n }\n }\n p = (p * 10) % 13;\n dp = tmp;\n }\n let ans = dp[5];\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1342", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N rectangular plate materials made of special metal called AtCoder Alloy.\nThe dimensions of the i-th material are A_i \\times B_i (A_i vertically and B_i horizontally).

\n

Takahashi wants a rectangular plate made of AtCoder Alloy whose dimensions are exactly H \\times W.\nHe is trying to obtain such a plate by choosing one of the N materials and cutting it if necessary.\nWhen cutting a material, the cuts must be parallel to one of the sides of the material.\nAlso, the materials have fixed directions and cannot be rotated.\nFor example, a 5 \\times 3 material cannot be used as a 3 \\times 5 plate.

\n

Out of the N materials, how many can produce an H \\times W plate if properly cut?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 1000
  • \n
  • 1 \\leq H \\leq 10^9
  • \n
  • 1 \\leq W \\leq 10^9
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
  • 1 \\leq B_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N H W\nA_1 B_1\nA_2 B_2\n:\nA_N B_N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 5 2\n10 3\n5 2\n2 5\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Takahashi wants a 5 \\times 2 plate.

\n
    \n
  • The dimensions of the first material are 10 \\times 3. We can obtain a 5 \\times 2 plate by properly cutting it.
  • \n
  • The dimensions of the second material are 5 \\times 2. We can obtain a 5 \\times 2 plate without cutting it.
  • \n
  • The dimensions of the third material are 2 \\times 5. We cannot obtain a 5 \\times 2 plate, whatever cuts are made. Note that the material cannot be rotated and used as a 5 \\times 2 plate.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

10 587586158 185430194\n894597290 708587790\n680395892 306946994\n590262034 785368612\n922328576 106880540\n847058850 326169610\n936315062 193149191\n702035777 223363392\n11672949 146832978\n779291680 334178158\n615808191 701464268\n
\n
\n
\n
\n
\n

Sample Output 2

8\n
\n
\n
", "c_code": "int solution(void) {\n int i = 0;\n int n = 0;\n int h = 0;\n int w = 0;\n\n int a = 0;\n int b = 0;\n\n int c = 0;\n\n scanf(\"%d %d %d\", &n, &h, &w);\n\n for (i = 0; i < n; i++) {\n scanf(\"%d %d\", &a, &b);\n if (a >= h && b >= w) {\n c++;\n }\n }\n\n printf(\"%d\\n\", c);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let handle = std::io::stdin();\n handle.read_line(&mut buf).unwrap();\n let info: Vec = buf.split_whitespace().map(|a| a.parse().unwrap()).collect();\n let (n, h, w) = (info[0], info[1], info[2]);\n buf.clear();\n let mut count = 0;\n\n for _i in 0..n {\n handle.read_line(&mut buf).unwrap();\n let board: Vec = buf.split_whitespace().map(|a| a.parse().unwrap()).collect();\n if board[0] >= h && board[1] >= w {\n count += 1;\n }\n buf.clear();\n }\n println!(\"{}\", count);\n}", "difficulty": "easy"} {"problem_id": "1343", "problem_description": "You have an image file of size $$$2 \\times 2$$$, consisting of $$$4$$$ pixels. Each pixel can have one of $$$26$$$ different colors, denoted by lowercase Latin letters.You want to recolor some of the pixels of the image so that all $$$4$$$ pixels have the same color. In one move, you can choose no more than two pixels of the same color and paint them into some other color (if you choose two pixels, both should be painted into the same color).What is the minimum number of moves you have to make in order to fulfill your goal?", "c_code": "int solution() {\n int numSets = 0;\n scanf(\"%d\", &numSets);\n\n char image[4];\n\n for (int currentSet = 0; currentSet < numSets; ++currentSet) {\n getchar();\n\n image[0] = getchar();\n int numDifferentColors = 1;\n\n image[1] = getchar();\n getchar();\n if (image[1] != image[0]) {\n ++numDifferentColors;\n }\n\n image[2] = getchar();\n if ((image[2] != image[1]) && (image[2] != image[0])) {\n ++numDifferentColors;\n }\n\n image[3] = getchar();\n if ((image[3] != image[2]) && (image[3] != image[1]) &&\n (image[3] != image[0])) {\n ++numDifferentColors;\n }\n\n printf(\"%d\\n\", numDifferentColors - 1);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut s = String::new();\n stdin.read_line(&mut s).unwrap();\n let i: u32 = s.trim().parse().unwrap();\n s.clear();\n for _ in 0..i {\n let mut h = HashSet::new();\n for _ in 0..2 {\n stdin.read_line(&mut s).unwrap();\n }\n for c in s.chars() {\n if !c.is_ascii_lowercase() {\n continue;\n }\n h.insert(c);\n }\n println!(\"{}\", h.len() - 1);\n s.clear();\n }\n}", "difficulty": "hard"} {"problem_id": "1344", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Joisino is working as a receptionist at a theater.

\n

The theater has 100000 seats, numbered from 1 to 100000.

\n

According to her memo, N groups of audiences have come so far, and the i-th group occupies the consecutive seats from Seat l_i to Seat r_i (inclusive).

\n

How many people are sitting at the theater now?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤1000
  • \n
  • 1≤l_i≤r_i≤100000
  • \n
  • No seat is occupied by more than one person.
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nl_1 r_1\n:\nl_N r_N\n
\n
\n
\n
\n
\n

Output

Print the number of people sitting at the theater.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1\n24 30\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

There are 7 people, sitting at Seat 24,25,26,27,28,29 and 30.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n6 8\n3 3\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n
\n
", "c_code": "int solution() {\n int x = 0;\n int i = 0;\n int total = 0;\n\n scanf(\"%d\", &x);\n\n for (i = 0; i < x; i++) {\n int start = 0;\n int end = 0;\n scanf(\"%d %d\", &start, &end);\n total = total + (end - start + 1);\n }\n\n printf(\"%d\\n\", total);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let n: i32 = s.trim().parse().ok().unwrap();\n let mut ans: i32 = 0;\n for _i in 0..n {\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).ok();\n let v: Vec = t\n .split_whitespace()\n .map(|e| e.parse().ok().unwrap())\n .collect();\n ans += v[1] - v[0] + 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1345", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers.You can remove at most one element from this array. Thus, the final length of the array is $$$n-1$$$ or $$$n$$$.Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.Recall that the contiguous subarray $$$a$$$ with indices from $$$l$$$ to $$$r$$$ is $$$a[l \\dots r] = a_l, a_{l + 1}, \\dots, a_r$$$. The subarray $$$a[l \\dots r]$$$ is called strictly increasing if $$$a_l < a_{l+1} < \\dots < a_r$$$.", "c_code": "int solution() {\n int n;\n int flg = 0;\n int max = 0;\n scanf(\"%d\", &n);\n int arr[3][n];\n if (n >= 1) {\n scanf(\"%d\", &arr[0][0]);\n arr[1][0] = arr[2][0] = 1;\n max = 1;\n }\n if (n >= 2) {\n scanf(\"%d\", &arr[0][1]);\n if (arr[0][1] > arr[0][0]) {\n arr[1][1] = arr[2][1] = 2;\n max = 2;\n } else {\n arr[1][1] = arr[2][1] = 1;\n }\n }\n for (int i = 2; i < n; i++) {\n scanf(\"%d\", &arr[0][i]);\n if (arr[0][i] > arr[0][i - 1]) {\n arr[1][i] = arr[1][i - 1] + 1;\n if (flg == 1) {\n if (arr[0][i] > arr[0][i - 2]) {\n arr[2][i] = arr[2][i - 1] + 1;\n } else {\n arr[2][i] = arr[1][i];\n }\n flg = 0;\n } else {\n arr[2][i] = arr[2][i - 1] + 1;\n }\n } else {\n arr[1][i] = 1;\n if (arr[0][i] > arr[0][i - 2]) {\n arr[2][i] = arr[1][i - 2] + 1;\n } else {\n arr[2][i] = arr[1][i - 1];\n flg = 1;\n }\n }\n if (arr[1][i] > max) {\n max = arr[1][i];\n }\n if (arr[2][i] > max) {\n max = arr[2][i];\n }\n }\n\n printf(\"%d\", max);\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _b1 = stdin().read_line(&mut str).unwrap();\n let n: usize = str.trim().parse().unwrap();\n str.clear();\n let _b1 = stdin().read_line(&mut str).unwrap();\n let mut iter = str.split_whitespace();\n let mut arr: Vec = Vec::new();\n loop {\n match iter.next() {\n Some(x) => arr.push(x.parse::().unwrap()),\n None => break,\n }\n }\n let mut top_vec: Vec = vec![1; n];\n let mut low_vec: Vec = vec![1; n];\n for i in 1..n {\n if arr[i] > arr[i - 1] {\n low_vec[i] = low_vec[i - 1] + 1;\n }\n }\n for i in (0..n - 1).rev() {\n if arr[i + 1] > arr[i] {\n top_vec[i] = top_vec[i + 1] + 1;\n }\n }\n let mut answ: i64 = *top_vec.iter().max().unwrap();\n for i in 1..n - 1 {\n if arr[i - 1] < arr[i + 1] {\n answ = max(answ, low_vec[i - 1] + top_vec[i + 1]);\n }\n }\n println!(\"{}\", answ);\n}", "difficulty": "medium"} {"problem_id": "1346", "problem_description": "You are given two integers $$$n$$$ and $$$k$$$. Your task is to find if $$$n$$$ can be represented as a sum of $$$k$$$ distinct positive odd (not divisible by $$$2$$$) integers or not.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n\n int tc;\n scanf(\"%d\", &tc);\n while (tc--) {\n long long int n;\n long long int k;\n scanf(\"%lld %lld\", &n, &k);\n if (k * k <= n && n % 2 == 0) {\n if (k % 2 == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n } else if (k * k <= n && n % 2 != 0) {\n if (k % 2 != 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n } else {\n printf(\"NO\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let t = {\n stdin().read_line(&mut buf).unwrap();\n buf.trim().parse::().unwrap()\n };\n for _ in 0..t {\n buf.clear();\n stdin().read_line(&mut buf).unwrap();\n let line: Vec = buf\n .split_whitespace()\n .map(str::parse)\n .map(Result::unwrap)\n .collect();\n let (n, k): (i64, i64) = (line[0], line[1]);\n let mut possible = true;\n if n % 2 != k % 2 {\n possible = false;\n } else {\n let sum_of_odd_nums = (k - 1).pow(2);\n let last_odd = n - sum_of_odd_nums;\n if last_odd <= 2 * k - 3 {\n possible = false;\n }\n }\n if possible {\n println!(\"YES\")\n } else {\n println!(\"NO\")\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1347", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N rabbits, numbered 1 through N.

\n

The i-th (1≤i≤N) rabbit likes rabbit a_i.\nNote that no rabbit can like itself, that is, a_i≠i.

\n

For a pair of rabbits i and j (i<j), we call the pair (i,j) a friendly pair if the following condition is met.

\n
    \n
  • Rabbit i likes rabbit j and rabbit j likes rabbit i.
  • \n
\n

Calculate the number of the friendly pairs.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≤N≤10^5
  • \n
  • 1≤a_i≤N
  • \n
  • a_i≠i
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the number of the friendly pairs.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n2 1 4 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

There are two friendly pairs: (1,2) and (3,4).

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n2 3 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

There are no friendly pairs.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n5 5 5 5 1\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n

There is one friendly pair: (1,5).

\n
\n
", "c_code": "int solution(void) {\n int N;\n scanf(\"%d\", &N);\n int a[100000];\n\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n int pairs = 0;\n for (int i = 0; i < N; i++) {\n if (a[a[i] - 1] == i + 1) {\n pairs++;\n }\n }\n pairs = pairs / 2;\n printf(\"%d\", pairs);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n\n let xs: Vec = s.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n\n let mut res = 0;\n for (i, &x) in xs.iter().enumerate() {\n if xs[x - 1] == i + 1 {\n res += 1;\n }\n }\n\n println!(\"{}\", res / 2);\n}", "difficulty": "medium"} {"problem_id": "1348", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

In this problem, we use the 24-hour clock.

\n

Takahashi gets up exactly at the time H_1 : M_1 and goes to bed exactly at the time H_2 : M_2. (See Sample Inputs below for clarity.)\nHe has decided to study for K consecutive minutes while he is up.\nWhat is the length of the period in which he can start studying?

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\le H_1, H_2 \\le 23
  • \n
  • 0 \\le M_1, M_2 \\le 59
  • \n
  • The time H_1 : M_1 comes before the time H_2 : M_2.
  • \n
  • K \\ge 1
  • \n
  • Takahashi is up for at least K minutes.
  • \n
  • All values in input are integers (without leading zeros).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H_1 M_1 H_2 M_2 K\n
\n
\n
\n
\n
\n

Output

Print the length of the period in which he can start studying, as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10 0 15 0 30\n
\n
\n
\n
\n
\n

Sample Output 1

270\n
\n

Takahashi gets up at exactly ten in the morning and goes to bed at exactly three in the afternoon.\nIt takes 30 minutes to do the study, so he can start it in the period between ten o'clock and half-past two. The length of this period is 270 minutes, so we should print 270.

\n
\n
\n
\n
\n
\n

Sample Input 2

10 0 12 0 120\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Takahashi gets up at exactly ten in the morning and goes to bed at exactly noon. It takes 120 minutes to do the study, so he has to start it at exactly ten o'clock. Thus, we should print 0.

\n
\n
", "c_code": "int solution() {\n int H1 = 0;\n int M1 = 0;\n int H2 = 0;\n int M2 = 0;\n int K = 0;\n\n int T = 0;\n\n scanf(\"%d %d %d %d %d\", &H1, &M1, &H2, &M2, &K);\n\n T = (60 * H2 + M2 - K) - (60 * H1 + M1);\n\n printf(\"%d\", T);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n let mut itr = is.split_whitespace().map(|e| e.parse().unwrap());\n let h1: usize = itr.next().unwrap();\n let m1: usize = itr.next().unwrap();\n let h2: usize = itr.next().unwrap();\n let m2: usize = itr.next().unwrap();\n let k: usize = itr.next().unwrap();\n let t = (h2 - h1) * 60 + m2 - m1;\n\n println!(\"{}\", t - k);\n}", "difficulty": "easy"} {"problem_id": "1349", "problem_description": "Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a1 or a2, a3 or a4, ..., a2n - 1 or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v.Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4)  →  (1 or 2 = 3, 3 or 4 = 7)  →  (3 xor 7 = 4). The result is v = 4.You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional m queries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new value v for the new sequence a.", "c_code": "int solution() {\n double n;\n int m;\n int i;\n int j;\n int f = 0;\n int x;\n int p;\n int b;\n scanf(\"%lf %d\", &n, &m);\n x = (int)pow(2, n);\n int tree[2 * x];\n for (i = x; i < 2 * x; i++) {\n scanf(\"%d\", &tree[i]);\n }\n for (i = x, f = 0; i > 1; i = i / 2) {\n for (j = i; j < 2 * i; j = j + 2) {\n if (f == 0) {\n tree[j / 2] = tree[j] | tree[j + 1];\n } else {\n tree[j / 2] = tree[j] ^ tree[j + 1];\n }\n }\n if (f == 0) {\n f = 1;\n } else {\n f = 0;\n }\n }\n for (i = 0; i < m; i++) {\n scanf(\"%d %d\", &p, &b);\n tree[x + p - 1] = b;\n for (j = x + p - 1, f = 0; j > 1; j = j / 2) {\n if (j % 2 == 0) {\n if (f == 0) {\n tree[j / 2] = tree[j] | tree[j + 1];\n f = 1;\n } else {\n tree[j / 2] = tree[j] ^ tree[j + 1];\n f = 0;\n }\n } else {\n if (f == 0) {\n tree[j / 2] = tree[j] | tree[j - 1];\n f = 1;\n } else {\n tree[j / 2] = tree[j] ^ tree[j - 1];\n f = 0;\n }\n }\n }\n printf(\"%d\\n\", tree[1]);\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n let cin = stdin();\n cin.read_line(&mut s).unwrap();\n let p = s\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let m = p[1];\n\n s.clear();\n cin.read_line(&mut s).unwrap();\n let a = s\n .split_whitespace()\n .map(|x| x.parse::())\n .collect::, _>>()\n .unwrap();\n\n let mut tree = Vec::new();\n tree.push(a);\n for k in 0.. {\n let mut b = Vec::new();\n for i in 0..tree[k].len() / 2 {\n if k % 2 == 0 {\n b.push(tree[k][2 * i] | tree[k][2 * i + 1]);\n } else {\n b.push(tree[k][2 * i] ^ tree[k][2 * i + 1]);\n }\n }\n tree.push(b);\n if tree[k + 1].len() == 1 {\n break;\n }\n }\n\n for _ in 0..m {\n s.clear();\n cin.read_line(&mut s).unwrap();\n let mut t = s.split_whitespace();\n let mut p = t.next().unwrap().parse::().unwrap();\n p -= 1;\n let b = t.next().unwrap().parse::().unwrap();\n\n tree[0][p] = b;\n\n for k in 1..tree.len() {\n if (k - 1) % 2 == 0 {\n tree[k][p / 2] = tree[k - 1][p] | tree[k - 1][p ^ 1];\n } else {\n tree[k][p / 2] = tree[k - 1][p] ^ tree[k - 1][p ^ 1];\n }\n p /= 2;\n }\n\n println!(\"{}\", tree[tree.len() - 1][0]);\n }\n}", "difficulty": "medium"} {"problem_id": "1350", "problem_description": "Mishka is a little polar bear. As known, little bears loves spending their free time playing dice for chocolates. Once in a wonderful sunny morning, walking around blocks of ice, Mishka met her friend Chris, and they started playing the game.Rules of the game are very simple: at first number of rounds n is defined. In every round each of the players throws a cubical dice with distinct numbers from 1 to 6 written on its faces. Player, whose value after throwing the dice is greater, wins the round. In case if player dice values are equal, no one of them is a winner.In average, player, who won most of the rounds, is the winner of the game. In case if two players won the same number of rounds, the result of the game is draw.Mishka is still very little and can't count wins and losses, so she asked you to watch their game and determine its result. Please help her!", "c_code": "int solution()\n\n{\n int n = 0;\n scanf(\"%d\", &n);\n int m[100];\n int c[100];\n int a = 0;\n int b = 0;\n int i = 1;\n for (i = 1; i <= n; i++) {\n scanf(\"%d %d\", &m[i], &c[i]);\n\n if (m[i] > c[i] && m[i] != c[i]) {\n a++;\n }\n\n if (m[i] < c[i] && m[i] != c[i]) {\n b++;\n }\n }\n if (a > b) {\n printf(\"Mishka\");\n }\n\n if (a < b) {\n printf(\"Chris\");\n }\n\n if (a == b) {\n printf(\"Friendship is magic!^^\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n\n io::stdin().read_line(&mut n).expect(\"read error\");\n\n let n: usize = n.trim().parse().expect(\"parse error\");\n\n let mut score: isize = 0;\n\n for _i in 0..n {\n let mut t = String::new();\n\n io::stdin().read_line(&mut t).expect(\"read error\");\n\n let t = t\n .split_whitespace()\n .map(|x| x.parse::().expect(\"parse error\"))\n .collect::>();\n\n let m = t[0];\n\n let c = t[1];\n\n if m > c {\n score += 1;\n } else if m < c {\n score -= 1;\n }\n }\n\n if score > 0 {\n println!(\"Mishka\");\n } else if score < 0 {\n println!(\"Chris\");\n } else {\n println!(\"Friendship is magic!^^\");\n }\n}", "difficulty": "easy"} {"problem_id": "1351", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

The postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9.

\n

You are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤A,B≤5
  • \n
  • |S|=A+B+1
  • \n
  • S consists of - and digits from 0 through 9.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\nS\n
\n
\n
\n
\n
\n

Output

Print Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4\n269-6650\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 1\n---\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

S contains unnecessary -s other than the (A+1)-th character, so it does not follow the format.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 2\n7444\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution() {\n\n int A;\n int B;\n int ans = 1;\n scanf(\"%d%d\", &A, &B);\n char S[A + B + 2];\n\n for (int i = 0; i < A + B + 2; i++) {\n scanf(\"%c\", &S[i]);\n }\n\n for (int i = 1; i < A + B + 2; i++) {\n if ((i < A) && !(S[i] >= 48 && S[i] <= 57)) {\n ans = 0;\n break;\n }\n if (i == A + 1 && !(S[A + 1] == '-')) {\n ans = 0;\n break;\n }\n if (A + 1 < i && !(S[i] >= 48 && S[i] <= 57)) {\n ans = 0;\n break;\n }\n }\n ans > 0 ? puts(\"Yes\") : puts(\"No\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut ab = String::new();\n io::stdin().read_line(&mut ab).expect(\"error\");\n let vec: Vec<_> = ab.split_whitespace().collect();\n let a: usize = vec[0].parse().unwrap();\n let b: usize = vec[1].parse().unwrap();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"error\");\n let s = s.trim();\n\n let s_len: usize = s.chars().count();\n if a + b + 1 != s_len || s.chars().nth(a).unwrap() != '-' {\n println!(\"No\");\n } else {\n let s_a: String = s.chars().take(a).collect();\n let s_b: String = s.chars().skip(a + 1).collect();\n\n let a_is_number = s_a.parse::().is_ok();\n let b_is_number = s_b.parse::().is_ok();\n\n if a_is_number && b_is_number {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1352", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Snuke has come to a store that sells boxes containing balls. The store sells the following three kinds of boxes:

\n
    \n
  • Red boxes, each containing R red balls
  • \n
  • Green boxes, each containing G green balls
  • \n
  • Blue boxes, each containing B blue balls
  • \n
\n

Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes.\nHow many triples of non-negative integers (r,g,b) achieve this?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq R,G,B,N \\leq 3000
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
R G B N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

Four triples achieve the objective, as follows:

\n
    \n
  • (4,0,0)
  • \n
  • (2,1,0)
  • \n
  • (1,0,1)
  • \n
  • (0,2,0)
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

13 1 4 3000\n
\n
\n
\n
\n
\n

Sample Output 2

87058\n
\n
\n
", "c_code": "int solution(void) {\n\n int r[3];\n int n;\n\n scanf(\"%d%d%d%d\", &r[0], &r[1], &r[2], &n);\n int i;\n int dp[n + 1];\n for (i = 0; i < n + 1; i++) {\n dp[i] = 0;\n }\n dp[0] = 1;\n int j;\n for (i = 0; i < 3; i++) {\n for (j = r[i]; j <= n; j++) {\n dp[j] = dp[j] + dp[j - r[i]];\n }\n }\n printf(\"%d\\n\", dp[n]);\n return 0;\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\n let r: i64 = iter.next().unwrap().parse().unwrap();\n\n let g: i64 = iter.next().unwrap().parse().unwrap();\n\n let b: i64 = iter.next().unwrap().parse().unwrap();\n\n let n: i64 = iter.next().unwrap().parse().unwrap();\n\n let mut ans: i64 = 0;\n\n let weight_min: i64 = min(r, min(g, b));\n let loop_min: i64 = (n + 1) / weight_min + 1;\n\n let weight_max: i64 = max(r, min(g, b));\n let _loop_max: i64 = (n + 1) / weight_max + 1;\n\n let weight_mid: i64 = r + g + b - weight_min - weight_max;\n let loop_mid: i64 = (n + 1) / weight_mid + 1;\n\n for i_min in 0..loop_min {\n for i_mid in 0..loop_mid {\n let i_max: i64 = (n - weight_min * i_min - weight_mid * i_mid) / weight_max;\n if i_max < 0 {\n continue;\n }\n let num_ball: i64 = i_min * weight_min + i_mid * weight_mid + i_max * weight_max;\n if num_ball == n {\n ans += 1;\n }\n }\n }\n\n let out = stdout();\n let mut out = BufWriter::new(out.lock());\n writeln!(out, \"{}\", ans).unwrap();\n}", "difficulty": "hard"} {"problem_id": "1353", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given an integer sequence of length N, a_1,a_2,...,a_N.

\n

For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or do nothing.

\n

After these operations, you select an integer X and count the number of i such that a_i=X.

\n

Maximize this count by making optimal choices.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤10^5
  • \n
  • 0≤a_i<10^5 (1≤i≤N)
  • \n
  • a_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\na_1 a_2 .. a_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number of i such that a_i=X.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7\n3 1 4 1 5 9 2\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\n0 1 2 3 4 5 6 7 8 9\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1\n99999\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\\n\", &n);\n int *a = (int *)calloc(100001, sizeof(int));\n int buf = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &buf);\n if (buf > 0) {\n a[buf - 1]++;\n a[buf]++;\n a[buf + 1]++;\n } else {\n a[buf]++;\n a[buf + 1]++;\n }\n }\n int max = 0;\n for (int i = 0; i < 100001; i++) {\n if (a[i] > max) {\n max = a[i];\n }\n }\n printf(\"%d\\n\", max);\n free(a);\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 mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let a: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n let mut h: HashMap = HashMap::new();\n let mut p = std::usize::MAX;\n let mut q = 0;\n for i in &a {\n let x = match h.get(i) {\n Some(x) => *x,\n None => 0,\n };\n h.insert(*i, x + 1);\n if *i < p {\n p = *i;\n }\n if *i > q {\n q = *i;\n }\n }\n\n let mut x = 0;\n if p < std::usize::MAX {\n p += 1;\n }\n q = q.saturating_sub(1);\n for i in std::cmp::min(p, q)..std::cmp::max(p, q) + 1 {\n let mut y = 0;\n y += match h.get(&i) {\n Some(x) => *x,\n None => 0,\n };\n y += match h.get(&(i - 1)) {\n Some(x) => *x,\n None => 0,\n };\n y += match h.get(&(i + 1)) {\n Some(x) => *x,\n None => 0,\n };\n if y > x {\n x = y;\n }\n }\n\n println!(\"{}\", x);\n}", "difficulty": "medium"} {"problem_id": "1354", "problem_description": "A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ of length $$$n$$$ containing each number exactly once. For example, $$$[1]$$$, $$$[4, 3, 5, 1, 2]$$$, $$$[3, 2, 1]$$$ are permutations, and $$$[1, 1]$$$, $$$[0, 1]$$$, $$$[2, 2, 1, 4]$$$ are not.There was a permutation $$$p[1 \\dots n]$$$. It was merged with itself. In other words, let's take two instances of $$$p$$$ and insert elements of the second $$$p$$$ into the first maintaining relative order of elements. The result is a sequence of the length $$$2n$$$.For example, if $$$p=[3, 1, 2]$$$ some possible results are: $$$[3, 1, 2, 3, 1, 2]$$$, $$$[3, 3, 1, 1, 2, 2]$$$, $$$[3, 1, 3, 1, 2, 2]$$$. The following sequences are not possible results of a merging: $$$[1, 3, 2, 1, 2, 3$$$], [$$$3, 1, 2, 3, 2, 1]$$$, $$$[3, 3, 1, 2, 2, 1]$$$.For example, if $$$p=[2, 1]$$$ the possible results are: $$$[2, 2, 1, 1]$$$, $$$[2, 1, 2, 1]$$$. The following sequences are not possible results of a merging: $$$[1, 1, 2, 2$$$], [$$$2, 1, 1, 2]$$$, $$$[1, 2, 2, 1]$$$.Your task is to restore the permutation $$$p$$$ by the given resulting sequence $$$a$$$. It is guaranteed that the answer exists and is unique.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution(void) {\n int t = 0;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n = 0;\n int x = 0;\n\n scanf(\"%d\", &n);\n\n int arr[51] = {0};\n\n for (int i = 0; i < n * 2; i++) {\n scanf(\"%d\", &x);\n\n if (arr[x] == 0) {\n arr[x] += 1;\n printf(\"%d \", x);\n }\n }\n\n printf(\"\\n\");\n }\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 let a: 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 b = vec![false; n + 1];\n let mut ans = Vec::with_capacity(n);\n for i in 0..(n * 2) {\n if !b[a[i]] {\n ans.push(a[i].to_string());\n b[a[i]] = true;\n }\n }\n println!(\"{}\", ans.join(\" \"));\n }\n}", "difficulty": "hard"} {"problem_id": "1355", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Let {\\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.\nFrom n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\\rm comb}(a_i,a_j) is maximized.\nIf there are multiple pairs that maximize the value, any of them is accepted.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq n \\leq 10^5
  • \n
  • 0 \\leq a_i \\leq 10^9
  • \n
  • a_1,a_2,...,a_n are pairwise distinct.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n\na_1 a_2 ... a_n\n
\n
\n
\n
\n
\n

Output

Print a_i and a_j that you selected, with a space in between.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n6 9 4 2 11\n
\n
\n
\n
\n
\n

Sample Output 1

11 6\n
\n

\\rm{comb}(a_i,a_j) for each possible selection is as follows:

\n
    \n
  • \\rm{comb}(4,2)=6
  • \n
  • \\rm{comb}(6,2)=15
  • \n
  • \\rm{comb}(6,4)=15
  • \n
  • \\rm{comb}(9,2)=36
  • \n
  • \\rm{comb}(9,4)=126
  • \n
  • \\rm{comb}(9,6)=84
  • \n
  • \\rm{comb}(11,2)=55
  • \n
  • \\rm{comb}(11,4)=330
  • \n
  • \\rm{comb}(11,6)=462
  • \n
  • \\rm{comb}(11,9)=55
  • \n
\n

Thus, we should print 11 and 6.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n100 0\n
\n
\n
\n
\n
\n

Sample Output 2

100 0\n
\n
\n
", "c_code": "int solution(void) {\n int i = 0;\n int j = 0;\n int n;\n scanf(\"%d\", &n);\n int t[100000];\n int max = 0;\n int scn = 0;\n for (; i < n; i++) {\n scanf(\"%d\", &t[i]);\n if (t[i] > max) {\n max = t[i];\n }\n }\n for (; j < n; j++) {\n if (abs(t[j] - (max / 2)) < abs(scn - (max / 2))) {\n scn = t[j];\n }\n }\n printf(\"%d %d\\n\", max, scn);\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..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n a.sort();\n a.reverse();\n\n let mut ans = a[1];\n let mut val = 1i64 << 60;\n for i in 1..n {\n let tmp = (a[0] / 2 - a[i]).abs();\n if val > tmp {\n val = tmp;\n ans = a[i];\n }\n }\n println!(\"{} {}\", a[0], ans);\n}", "difficulty": "medium"} {"problem_id": "1356", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

For an integer n not less than 0, let us define f(n) as follows:

\n
    \n
  • f(n) = 1 (if n < 2)
  • \n
  • f(n) = n f(n-2) (if n \\geq 2)
  • \n
\n

Given is an integer N. Find the number of trailing zeros in the decimal notation of f(N).

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq N \\leq 10^{18}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the number of trailing zeros in the decimal notation of f(N).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

12\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

f(12) = 12 × 10 × 8 × 6 × 4 × 2 = 46080, which has one trailing zero.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

f(5) = 5 × 3 × 1 = 15, which has no trailing zeros.

\n
\n
\n
\n
\n
\n

Sample Input 3

1000000000000000000\n
\n
\n
\n
\n
\n

Sample Output 3

124999999999999995\n
\n
\n
", "c_code": "int solution(void) {\n long long int n = 0;\n long long int ans = 0;\n long long int count = 1;\n scanf(\"%lld\", &n);\n\n if (n % 2 != 0) {\n printf(\"0\\n\");\n } else {\n int under = 10;\n while (1) {\n if (under * count > n) {\n break;\n }\n ans += n / (under * count);\n count *= 5;\n }\n printf(\"%lld\\n\", ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let n: u64 = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s.trim_end().parse().unwrap()\n };\n println!(\"{}\", {\n let mut ans: u64 = 0;\n let mut t: u64 = 10;\n while n >= t {\n ans += n / t;\n t *= 5;\n }\n if n.is_multiple_of(2) {\n ans\n } else {\n 0\n }\n });\n}", "difficulty": "easy"} {"problem_id": "1357", "problem_description": "Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one).Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell i - 1, i - 2 or i - k). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number i < k. The player who can't make a move loses the game.Who wins if both participants play optimally?Alice and Bob would like to play several games, so you should determine the winner in each game.", "c_code": "int solution() {\n int t;\n int n;\n int k;\n scanf(\"%d\", &t);\n while (t > 0) {\n t--;\n scanf(\"%d %d\", &n, &k);\n if (n < k) {\n if (n % 3) {\n printf(\"Alice\\n\");\n } else {\n printf(\"Bob\\n\");\n }\n } else {\n if (k % 3 == 0) {\n n %= k + 1;\n if (n == k || n % 3) {\n printf(\"Alice\\n\");\n } else {\n printf(\"Bob\\n\");\n }\n } else {\n if (n % 3) {\n printf(\"Alice\\n\");\n } else {\n printf(\"Bob\\n\");\n }\n }\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin()\n .read_line(&mut s)\n .expect(\"Did not enter a correct string\");\n if let Some('\\n') = s.chars().next_back() {\n s.pop();\n }\n if let Some('\\r') = s.chars().next_back() {\n s.pop();\n }\n let mut i = s.parse::().unwrap();\n while i > 0 {\n i -= 1;\n s.clear();\n stdin()\n .read_line(&mut s)\n .expect(\"Did not enter a correct string\");\n if let Some('\\n') = s.chars().next_back() {\n s.pop();\n }\n if let Some('\\r') = s.chars().next_back() {\n s.pop();\n }\n let mut s = s.split(\" \");\n let n = s.next().unwrap().parse::().unwrap();\n\n let k = s.next().unwrap().parse::().unwrap();\n\n if k % 3 != 0 {\n let c = if n % 3 == 0 { \"Bob\" } else { \"Alice\" };\n println!(\"{}\", c);\n } else {\n let c = if n % (k + 1) % 3 == 0 && (n + 1) % (k + 1) != 0 {\n \"Bob\"\n } else {\n \"Alice\"\n };\n println!(\"{}\", c);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1358", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

The window of Takahashi's room has a width of A. There are two curtains hung over the window, each of which has a horizontal length of B. (Vertically, the curtains are long enough to cover the whole window.)

\n

We will close the window so as to minimize the total horizontal length of the uncovered part of the window.\nFind the total horizontal length of the uncovered parts of the window then.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A \\leq 100
  • \n
  • 1 \\leq B \\leq 100
  • \n
  • A and B are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the total horizontal length of the uncovered parts of the window.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

12 4\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

We have a window with a horizontal length of 12, and two curtains, each of length 4, that cover both ends of the window, for example. The uncovered part has a horizontal length of 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

20 15\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

If the window is completely covered, print 0.

\n
\n
\n
\n
\n
\n

Sample Input 3

20 30\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

Each curtain may be longer than the window.

\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n scanf(\"%d %d\", &a, &b);\n if (a - 2 * b > 0) {\n printf(\"%d\", a - (2 * b));\n } else {\n printf(\"0\");\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).ok();\n let mut nums = buf.split_whitespace().map(|n| i32::from_str(n).unwrap());\n let (A, B) = (nums.next().unwrap(), nums.next().unwrap());\n\n let ans = A - 2 * B;\n println!(\"{}\", i32::max(0, ans));\n}", "difficulty": "medium"} {"problem_id": "1359", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^9
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the largest square number not exceeding N.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

10 is not square, but 9 = 3 × 3 is. Thus, we print 9.

\n
\n
\n
\n
\n
\n

Sample Input 2

81\n
\n
\n
\n
\n
\n

Sample Output 2

81\n
\n
\n
\n
\n
\n
\n

Sample Input 3

271828182\n
\n
\n
\n
\n
\n

Sample Output 3

271821169\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n int a = 0;\n scanf(\"%d\", &n);\n a = sqrt(n);\n n = a * a;\n printf(\"%d\\n\", n);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let n: i32 = s.trim().parse().ok().unwrap();\n\n let mut k = 0;\n let mut ans = 1;\n while k * k <= n {\n ans = k * k;\n k += 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1360", "problem_description": "Polycarp is sad — New Year is coming in few days but there is still no snow in his city. To bring himself New Year mood, he decided to decorate his house with some garlands.The local store introduced a new service this year, called \"Build your own garland\". So you can buy some red, green and blue lamps, provide them and the store workers will solder a single garland of them. The resulting garland will have all the lamps you provided put in a line. Moreover, no pair of lamps of the same color will be adjacent to each other in this garland!For example, if you provide $$$3$$$ red, $$$3$$$ green and $$$3$$$ blue lamps, the resulting garland can look like this: \"RGBRBGBGR\" (\"RGB\" being the red, green and blue color, respectively). Note that it's ok to have lamps of the same color on the ends of the garland.However, if you provide, say, $$$1$$$ red, $$$10$$$ green and $$$2$$$ blue lamps then the store workers won't be able to build any garland of them. Any garland consisting of these lamps will have at least one pair of lamps of the same color adjacent to each other. Note that the store workers should use all the lamps you provided.So Polycarp has bought some sets of lamps and now he wants to know if the store workers can build a garland from each of them.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int a[3];\n for (int i = 0; i <= 2; i++) {\n scanf(\"%d\", &a[i]);\n }\n if ((a[0] + a[1] + 1) >= a[2] && (a[1] + a[2] + 1) >= a[0] &&\n (a[0] + a[2] + 1) >= a[1]) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n outset! { buf }\n input! {\n t: usize,\n rgb: [(usize, usize, usize); t],\n }\n for x in rgb.iter() {\n let mut v = [x.0, x.1, x.2];\n v.sort();\n if v[2] - 1 <= v[0] + v[1] {\n output!(buf, \"Yes\\n\");\n } else {\n output!(buf, \"No\\n\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1361", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N squares arranged in a row from left to right. The height of the i-th square from the left is H_i.

\n

For each square, you will perform either of the following operations once:

\n
    \n
  • Decrease the height of the square by 1.
  • \n
  • Do nothing.
  • \n
\n

Determine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq H_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nH_1 H_2 ... H_N\n
\n
\n
\n
\n
\n

Output

If it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1 2 1 1 3\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

You can achieve the objective by decreasing the height of only the second square from the left by 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n1 3 2 1\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5\n1 2 3 4 5\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1\n1000000000\n
\n
\n
\n
\n
\n

Sample Output 4

Yes\n
\n
\n
", "c_code": "int solution() {\n\n int n;\n scanf(\"%d\", &n);\n long long h[n + 1];\n h[0] = 0;\n for (int i = 1; i <= n; i++) {\n scanf(\"%lld\", &h[i]);\n }\n\n int flg = 0;\n for (int i = 1; i < n; i++) {\n if (h[i - 1] <= h[i] && h[i] <= h[i + 1]) {\n if (h[i - 1] < h[i] && h[i] == h[i + 1]) {\n h[i]--;\n }\n } else {\n if (h[i - 1] < h[i] && (h[i] - h[i + 1]) == 1) {\n h[i]--;\n } else {\n flg = 1;\n i = n;\n }\n }\n }\n\n if (flg == 0) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n\n let mut buf = String::new();\n stdin.read_line(&mut buf).ok();\n let _n: i32 = buf.trim().parse().unwrap();\n\n buf.clear();\n stdin.read_line(&mut buf).ok();\n let hs: Vec = buf.split_whitespace().map(|w| w.parse().unwrap()).collect();\n\n let mut last = std::i32::MAX;\n for &h in hs.iter().rev() {\n if last >= h {\n last = h;\n } else if last == h - 1 {\n last = h - 1;\n } else {\n println!(\"No\");\n std::process::exit(0);\n }\n }\n println!(\"Yes\");\n}", "difficulty": "medium"} {"problem_id": "1362", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

We have N locked treasure boxes, numbered 1 to N.

\n

A shop sells M keys. The i-th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1}, c_{i2}, ..., c_{i{b_i}}. Each key purchased can be used any number of times.

\n

Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 12
  • \n
  • 1 \\leq M \\leq 10^3
  • \n
  • 1 \\leq a_i \\leq 10^5
  • \n
  • 1 \\leq b_i \\leq N
  • \n
  • 1 \\leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \\leq N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\na_1 b_1\nc_{11} c_{12} ... c_{1{b_1}}\n:\na_M b_M\nc_{M1} c_{M2} ... c_{M{b_M}}\n
\n
\n
\n
\n
\n

Output

Print the minimum cost required to unlock all the treasure boxes.\nIf it is impossible to unlock all of them, print -1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3\n10 1\n1\n15 1\n2\n30 2\n1 2\n
\n
\n
\n
\n
\n

Sample Output 1

25\n
\n

We can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required.

\n
\n
\n
\n
\n
\n

Sample Input 2

12 1\n100000 1\n2\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

We cannot unlock all the boxes.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 6\n67786 3\n1 3 4\n3497 1\n2\n44908 3\n2 3 4\n2156 3\n2 3 4\n26230 1\n2\n86918 1\n3\n
\n
\n
\n
\n
\n

Sample Output 3

69942\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int m;\n int i;\n int j;\n int tmp;\n int big = 1000000;\n scanf(\"%d %d\", &n, &m);\n int com = pow(2, n);\n int a[m];\n int b[m];\n int c[m][12];\n int dp[com];\n for (i = 0; i < com; i++) {\n dp[i] = big;\n }\n\n for (i = 0; i < m; i++) {\n scanf(\"%d %d\", &a[i], &b[i]);\n for (j = 0; j < b[i]; j++) {\n scanf(\"%d\", &c[i][j]);\n }\n tmp = 0;\n for (j = 0; j < b[i]; j++) {\n tmp += pow(2, c[i][j] - 1);\n }\n if (dp[tmp] > a[i]) {\n dp[tmp] = a[i];\n }\n for (j = 0; j < com; j++) {\n if (dp[j] != big && dp[j | tmp] > dp[j] + a[i]) {\n dp[j | tmp] = dp[j] + a[i];\n }\n }\n }\n if (dp[com - 1] == big) {\n printf(\"-1\");\n } else {\n printf(\"%d\", dp[com - 1]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (n, m) = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let mut it = buf.split_whitespace();\n let n: u32 = it.next().unwrap().parse().unwrap();\n let m: usize = it.next().unwrap().parse().unwrap();\n (n, m)\n };\n let mut a: Vec = Vec::new();\n let mut b: Vec = Vec::new();\n let mut c: Vec> = Vec::new();\n for _ in 0..m {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let mut it = buf.split_whitespace();\n let _a: i32 = it.next().unwrap().parse().unwrap();\n let _b: i32 = it.next().unwrap().parse().unwrap();\n a.push(_a);\n b.push(_b);\n\n let mut buf2 = String::new();\n std::io::stdin().read_line(&mut buf2).unwrap();\n let mut it2 = buf2.split_whitespace();\n let _c = (0.._b)\n .map(|_| it2.next().unwrap().parse().unwrap())\n .collect::>();\n c.push(_c);\n }\n\n let mx = 10i32.pow(9);\n let mut dp = vec![vec![mx; 1 << n]; m + 1];\n dp[0][0] = 0;\n\n for i in 1..m + 1 {\n let key = c.get(i - 1).unwrap();\n let cost = a.get(i - 1).unwrap();\n let mut k = 0;\n for j in key {\n k |= 1 << (j - 1)\n }\n for j in 0..(1 << n) {\n dp[i][j | k] = min(dp[i][j | k], dp[i - 1][j] + cost);\n dp[i][j] = min(dp[i][j], dp[i - 1][j]);\n }\n }\n if dp[m][(1 << n) - 1] == mx {\n println!(\"{}\", -1);\n } else {\n println!(\"{}\", dp[m][(1 << n) - 1]);\n }\n}", "difficulty": "medium"} {"problem_id": "1363", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integer numbers.You have to color this array in $$$k$$$ colors in such a way that: Each element of the array should be colored in some color; For each $$$i$$$ from $$$1$$$ to $$$k$$$ there should be at least one element colored in the $$$i$$$-th color in the array; For each $$$i$$$ from $$$1$$$ to $$$k$$$ all elements colored in the $$$i$$$-th color should be distinct. Obviously, such coloring might be impossible. In this case, print \"NO\". Otherwise print \"YES\" and any coloring (i.e. numbers $$$c_1, c_2, \\dots c_n$$$, where $$$1 \\le c_i \\le k$$$ and $$$c_i$$$ is the color of the $$$i$$$-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any.", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n int a[n];\n int cnt = 0;\n int hash[10000] = {0};\n int max = 0;\n int color[10000] = {0};\n\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &a[i]);\n hash[a[i]]++;\n if (hash[a[i]] > max) {\n max = hash[a[i]];\n }\n }\n int q = 1;\n int temp = 1;\n\n for (int i = 0; i < 10000; ++i) {\n if (hash[i] != 0) {\n color[i] += temp;\n temp += hash[i];\n }\n\n q++;\n }\n if (max > k || temp < k) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n for (int i = 0; i < n; ++i) {\n if (color[a[i]] % k != 0) {\n printf(\"%d \", color[a[i]] % k);\n } else {\n printf(\"%d \", k);\n }\n color[a[i]]++;\n }\n }\n}", "rust_code": "fn solution() {\n let (_n, k): (usize, usize) = {\n let mut read_buf = String::new();\n io::stdin().read_line(&mut read_buf).unwrap();\n let mut read_buf = read_buf.split_whitespace();\n (\n read_buf.next().unwrap().parse().unwrap(),\n read_buf.next().unwrap().parse().unwrap(),\n )\n };\n let a: Vec = {\n let mut read_buf = String::new();\n io::stdin().read_line(&mut read_buf).unwrap();\n read_buf\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect()\n };\n\n let mut ans_flag = true;\n let mut cnt: Vec = Vec::new();\n let mut max_cnt = 0;\n cnt.resize(5000 + 1, 0);\n for x in &a {\n cnt[*x] += 1;\n if cnt[*x] > max_cnt {\n max_cnt = cnt[*x];\n }\n if max_cnt > k {\n ans_flag = false;\n break;\n }\n }\n\n if ans_flag {\n println!(\"YES\");\n\n let mut last_colors = k;\n let mut changed: Vec = Vec::new();\n changed.resize(5000 + 1, false);\n for x in &a {\n if !changed[*x] && cnt[*x] <= last_colors {\n let c = cnt[*x];\n cnt[*x] = last_colors;\n last_colors -= c;\n changed[*x] = true;\n }\n print!(\"{} \", cnt[*x]);\n cnt[*x] -= 1;\n }\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "1364", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N people standing on the x-axis.\nLet the coordinate of Person i be x_i.\nFor every i, x_i is an integer between 0 and 10^9 (inclusive).\nIt is possible that more than one person is standing at the same coordinate.

\n

You will given M pieces of information regarding the positions of these people.\nThe i-th piece of information has the form (L_i, R_i, D_i).\nThis means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.

\n

It turns out that some of these M pieces of information may be incorrect.\nDetermine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100 000
  • \n
  • 0 \\leq M \\leq 200 000
  • \n
  • 1 \\leq L_i, R_i \\leq N (1 \\leq i \\leq M)
  • \n
  • 0 \\leq D_i \\leq 10 000 (1 \\leq i \\leq M)
  • \n
  • L_i \\neq R_i (1 \\leq i \\leq M)
  • \n
  • If i \\neq j, then (L_i, R_i) \\neq (L_j, R_j) and (L_i, R_i) \\neq (R_j, L_j).
  • \n
  • D_i are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nL_1 R_1 D_1\nL_2 R_2 D_2\n:\nL_M R_M D_M\n
\n
\n
\n
\n
\n

Output

If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n1 2 1\n2 3 1\n1 3 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103).

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3\n1 2 1\n2 3 1\n1 3 5\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 3\n2 1 1\n2 3 5\n3 4 2\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 4

10 3\n8 7 100\n7 9 100\n9 8 100\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
\n
\n
\n
\n

Sample Input 5

100 0\n
\n
\n
\n
\n
\n

Sample Output 5

Yes\n
\n
\n
", "c_code": "int solution() {\n int i;\n int u;\n int w;\n int D;\n int N;\n int M;\n scanf(\"%d %d\", &N, &M);\n list **adj = (list **)malloc(sizeof(list *) * (N + 1));\n list *d = (list *)malloc(sizeof(list) * M * 2);\n for (i = 0; i < M; i++) {\n scanf(\"%d %d %d\", &u, &w, &D);\n d[i * 2].v = w;\n d[(i * 2) + 1].v = u;\n d[i * 2].len = D;\n d[(i * 2) + 1].len = -D;\n d[i * 2].next = adj[u];\n d[(i * 2) + 1].next = adj[w];\n adj[u] = &(d[i * 2]);\n adj[w] = &(d[(i * 2) + 1]);\n }\n\n int flag[100001] = {};\n int q[100001];\n int head;\n int tail;\n long long dist[100001];\n list *p;\n for (i = 1; i <= N; i++) {\n if (flag[i] == 1) {\n continue;\n }\n flag[i] = 1;\n dist[i] = 0;\n q[0] = i;\n for (head = 0, tail = 1; head < tail; head++) {\n u = q[head];\n for (p = adj[u]; p != NULL; p = p->next) {\n w = p->v;\n if (flag[w] == 0) {\n flag[w] = 1;\n dist[w] = dist[u] + p->len;\n q[tail++] = w;\n } else if (dist[w] != dist[u] + p->len) {\n break;\n }\n }\n if (p != NULL) {\n break;\n }\n }\n if (head < tail) {\n break;\n }\n }\n if (i > N) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n fflush(stdout);\n return 0;\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 m: usize = itr.next().unwrap().parse().unwrap();\n\n let mut g = vec![Vec::new(); n];\n for _ in 0..m {\n let l = itr.next().unwrap().parse::().unwrap() - 1;\n let r = itr.next().unwrap().parse::().unwrap() - 1;\n let c: i64 = itr.next().unwrap().parse().unwrap();\n\n g[l].push((r, c));\n g[r].push((l, -c));\n }\n\n const INF: i64 = 1 << 30;\n let mut d = vec![INF; n];\n let mut q = std::collections::VecDeque::new();\n\n for i in 0..n {\n if d[i] == INF {\n d[i] = 100000;\n }\n q.clear();\n q.push_back(i);\n while let Some(v) = q.pop_front() {\n for &(nv, c) in g[v].iter() {\n if d[nv] == INF {\n d[nv] = d[v] + c;\n q.push_back(nv);\n } else {\n if d[nv] != d[v] + c {\n println!(\"No\");\n return;\n }\n }\n }\n }\n }\n\n println!(\"Yes\");\n}", "difficulty": "hard"} {"problem_id": "1365", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There is an image with a height of H pixels and a width of W pixels. Each of the pixels is represented by either . or *. The character representing the pixel at the i-th row from the top and the j-th column from the left, is denoted by C_{i,j}.

\n

Extend this image vertically so that its height is doubled. That is, print a image with a height of 2H pixels and a width of W pixels where the pixel at the i-th row and j-th column is equal to C_{(i+1)/2,j} (the result of division is rounded down).

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦H, W≦100
  • \n
  • C_{i,j} is either . or *.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
H W\nC_{1,1}...C_{1,W}\n:\nC_{H,1}...C_{H,W}\n
\n
\n
\n
\n
\n

Output

Print the extended image.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n*.\n.*\n
\n
\n
\n
\n
\n

Sample Output 1

*.\n*.\n.*\n.*\n
\n
\n
\n
\n
\n
\n

Sample Input 2

1 4\n***.\n
\n
\n
\n
\n
\n

Sample Output 2

***.\n***.\n
\n
\n
\n
\n
\n
\n

Sample Input 3

9 20\n.....***....***.....\n....*...*..*...*....\n...*.....**.....*...\n...*.....*......*...\n....*.....*....*....\n.....**..*...**.....\n.......*..*.*.......\n........**.*........\n.........**.........\n
\n
\n
\n
\n
\n

Sample Output 3

.....***....***.....\n.....***....***.....\n....*...*..*...*....\n....*...*..*...*....\n...*.....**.....*...\n...*.....**.....*...\n...*.....*......*...\n...*.....*......*...\n....*.....*....*....\n....*.....*....*....\n.....**..*...**.....\n.....**..*...**.....\n.......*..*.*.......\n.......*..*.*.......\n........**.*........\n........**.*........\n.........**.........\n.........**.........\n
\n
\n
", "c_code": "int solution() {\n\n int H;\n int W = 0;\n scanf(\"%d %d\", &H, &W);\n\n char C[2 * H][W];\n\n for (int i = 0; i < H; i++) {\n scanf(\"%s\", C[i]);\n printf(\"%s\\n\", C[i]);\n printf(\"%s\\n\", C[i]);\n }\n\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 v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n for _ in 0..v[0] {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n println!(\"{}\\n{}\", s.trim(), s.trim());\n }\n}", "difficulty": "hard"} {"problem_id": "1366", "problem_description": "\n\n\n

Breadth First Search

\n\n

\n Write a program which reads an directed graph $G = (V, E)$, and finds the shortest distance from vertex $1$ to each vertex (the number of edges in the shortest path). Vertices are identified by IDs $1, 2, ... n$. \n

\n\n

Input

\n\n

\n In the first line, an integer $n$ denoting the number of vertices, is given. In the next $n$ lines, adjacent lists of vertex $u$ are given in the following format:\n

\n\n

\n$u$ $k$ $v_1$ $v_2$ ... $v_k$\n

\n\n

\n$u$ is ID of the vertex and $k$ denotes its degree.$v_i$ are IDs of vertices adjacent to $u$.\n

\n\n\n

Constraints

\n\n
    \n
  • $1 \\leq n \\leq 100$
  • \n
\n\n

Output

\n\n

\n For each vertex $u$, print $id$ and $d$ in a line. $id$ is ID of vertex $u$ and $d$ is the distance from vertex $1$ to vertex $u$. If there are no path from vertex $1$ to vertex $u$, print -1 as the shortest distance. Print in order of IDs.\n

\n\n

Sample Input 1

\n
\n4\n1 2 2 4\n2 1 4\n3 0\n4 1 3\n
\n\n

Sample Output 1

\n
\n1 0\n2 1\n3 2\n4 1\n
\n\n
\n\n\n

Reference

\n\n

\nIntroduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.\n

", "c_code": "int solution() {\n\n int n;\n\n scanf(\"%d\", &n);\n\n int g[102][102];\n int d[102];\n\n int i;\n int j;\n for (i = 1; i <= n; i++) {\n for (j = 1; j <= n; j++) {\n g[i][j] = 0;\n }\n d[i] = -1;\n }\n\n int u;\n int k;\n for (i = 0; i < n; i++) {\n scanf(\"%d %d\", &u, &k);\n for (j = 0; j < k; j++) {\n int v;\n scanf(\"%d\", &v);\n g[u][v] = 1;\n }\n }\n\n int Q[102];\n int head = 0;\n int tail;\n tail = head + 1;\n\n Q[head] = 1;\n\n d[Q[head]] = 0;\n while (head != tail) {\n u = Q[head];\n head++;\n for (i = 1; i <= n; i++) {\n if (g[u][i] == 1 && d[i] == -1) {\n g[u][i] = 0;\n d[i] = d[u] + 1;\n Q[tail++] = i;\n }\n }\n }\n\n for (i = 1; i <= n; i++) {\n printf(\"%d %d\\n\", i, d[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let _ = std::io::stdin().read_line(&mut buf).ok();\n let n: usize = buf.trim().parse().unwrap();\n let mut g = Vec::new();\n for _ in 0..n {\n let mut buf = String::new();\n let _ = std::io::stdin().read_line(&mut buf).ok();\n let mut buf = buf.split_whitespace();\n let id: usize = buf.next().unwrap().parse().unwrap();\n let k: usize = buf.next().unwrap().parse().unwrap();\n let mut e = Vec::new();\n for _ in 0..k {\n let p: usize = buf.next().unwrap().parse().unwrap();\n e.push(p);\n }\n g.push((id, e));\n }\n let mut dis: Vec = vec![-1; n];\n dis[0] = 0;\n for distance in 0..n {\n for i in 0..n {\n if dis[i] == distance as i64 {\n for j in &g[i].1 {\n if dis[*j - 1] == -1 {\n dis[*j - 1] = distance as i64 + 1;\n }\n }\n }\n }\n }\n\n for (i, d) in dis.iter().enumerate() {\n println!(\"{} {}\", i + 1, d);\n }\n}", "difficulty": "medium"} {"problem_id": "1367", "problem_description": "A flower shop has got n bouquets, and the i-th bouquet consists of ai flowers. Vasya, the manager of the shop, decided to make large bouquets from these bouquets. Vasya thinks that a bouquet is large if it is made of two or more initial bouquets, and there is a constraint: the total number of flowers in a large bouquet should be odd. Each of the initial bouquets can be a part of at most one large bouquet. If an initial bouquet becomes a part of a large bouquet, all its flowers are included in the large bouquet.Determine the maximum possible number of large bouquets Vasya can make.", "c_code": "int solution() {\n int count = 0;\n scanf(\"%d\", &count);\n int inp = 0;\n int first = 0;\n int second = 0;\n for (int i = 0; i < count; i++) {\n scanf(\"%d\", &inp);\n if (inp % 2) {\n first++;\n } else {\n second++;\n }\n }\n int answer = 0;\n if (first > second) {\n answer = second + (first - second) / 3;\n } else {\n answer = first;\n }\n printf(\"%d\", answer);\n return 0;\n}", "rust_code": "fn solution() {\n let mut n = String::new();\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 arr = String::new();\n\n std::io::stdin()\n .read_line(&mut arr)\n .expect(\"Reading arr error!\");\n let tmp = arr.split(\" \");\n let vec = tmp.collect::>();\n let mut chet = 0;\n let mut nechet = 0;\n for x in vec {\n let y: u32 = x.trim().parse().expect(\"Error while parsing!\");\n if y.is_multiple_of(2) {\n chet += 1;\n } else {\n nechet += 1;\n }\n }\n if nechet * 2 <= n {\n println!(\"{}\", nechet);\n } else {\n println!(\"{}\", chet + ((nechet - chet) / 3));\n }\n}", "difficulty": "medium"} {"problem_id": "1368", "problem_description": "Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \\times y$$$ fits into some wallet $$$h \\times w$$$ if either $$$x \\le h$$$ and $$$y \\le w$$$ or $$$y \\le h$$$ and $$$x \\le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ — Polycarp earns a bill of size $$$x \\times y$$$; $$$?~h~w$$$ — Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \\times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print \"YES\" if all the bills he has earned to this moment fit into a wallet of given size. Print \"NO\" otherwise.", "c_code": "int solution() {\n\n int n;\n scanf(\"%d\", &n);\n int max_x = -1;\n int max_y = -1;\n while (n--) {\n char c = getchar();\n int a;\n int b;\n\n scanf(\"%c %d %d\", &c, &a, &b);\n int temp = a < b ? a : b;\n b = a > b ? a : b;\n a = temp;\n if (c == '+') {\n if (a > max_x) {\n max_x = a;\n }\n if (b > max_y) {\n max_y = b;\n }\n } else if (c == '?') {\n\n if (a >= max_x && b >= max_y) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input)?;\n\n let mut output = String::with_capacity(input.len());\n\n let mut tokens = input.split_whitespace();\n\n let _ = tokens.next().unwrap().parse::().unwrap();\n\n let mut max_min = 0;\n let mut max_max = 0;\n\n use std::cmp::{max, min};\n\n while let Some(kind) = tokens.next() {\n let a = tokens.next().unwrap().parse::().unwrap();\n let b = tokens.next().unwrap().parse::().unwrap();\n\n match kind {\n \"+\" => {\n max_min = max(max_min, min(a, b));\n max_max = max(max_max, max(a, b));\n }\n \"?\" => {\n let ans = max_min <= min(a, b) && max_max <= max(a, b);\n output.push_str(if ans { \"YES\\n\" } else { \"NO\\n\" });\n }\n _ => (),\n }\n }\n\n write!(io::stdout(), \"{}\", output)\n}", "difficulty": "easy"} {"problem_id": "1369", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Two students of AtCoder Kindergarten are fighting over candy packs.

\n

There are three candy packs, each of which contains a, b, and c candies, respectively.

\n

Teacher Evi is trying to distribute the packs between the two students so that each student gets the same number of candies. Determine whether it is possible.

\n

Note that Evi cannot take candies out of the packs, and the whole contents of each pack must be given to one of the students.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ a, b, c ≦ 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
a b c\n
\n
\n
\n
\n
\n

Output

If it is possible to distribute the packs so that each student gets the same number of candies, print Yes. Otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10 30 20\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Give the pack with 30 candies to one student, and give the two packs with 10 and 20 candies to the other. Then, each gets 30 candies.

\n
\n
\n
\n
\n
\n

Sample Input 2

30 30 100\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

In this case, the student who gets the pack with 100 candies always has more candies than the other.

\n

Note that every pack must be given to one of them.

\n
\n
\n
\n
\n
\n

Sample Input 3

56 25 31\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\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\n if (a + b == c || a + c == b || b + c == a) {\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::new();\n io::stdin().read_line(&mut s).ok();\n\n let mut xs: Vec = s.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n xs.sort();\n\n println!(\"{}\", if xs[0] + xs[1] == xs[2] { \"Yes\" } else { \"No\" });\n}", "difficulty": "medium"} {"problem_id": "1370", "problem_description": "Comb sort", "c_code": "void solution(int *numbers, int size) {\n const SHRINK = 1.3 int gap = size;\n while (gap > 1) {\n gap = gap / SHRINK;\n int i = 0;\n while ((i + gap) < size) {\n if (numbers[i] > numbers[i + gap]) {\n int tmp = numbers[i];\n numbers[i] = numbers[i + gap];\n numbers[i + gap] = tmp;\n }\n i++;\n }\n }\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n let mut gap = arr.len();\n let shrink = 1.3;\n let mut sorted = false;\n\n while !sorted {\n gap = (gap as f32 / shrink).floor() as usize;\n if gap <= 1 {\n gap = 1;\n sorted = true;\n }\n for i in 0..arr.len() - gap {\n let j = i + gap;\n if arr[i] > arr[j] {\n arr.swap(i, j);\n sorted = false;\n }\n }\n }\n}\n", "difficulty": "medium"} {"problem_id": "1371", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Given is a sequence of N integers A_1, \\ldots, A_N.

\n

Find the (multiplicative) inverse of the sum of the inverses of these numbers, \\frac{1}{\\frac{1}{A_1} + \\ldots + \\frac{1}{A_N}}.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq A_i \\leq 1000
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 \\ldots A_N\n
\n
\n
\n
\n
\n

Output

Print a decimal number (or an integer) representing the value of \\frac{1}{\\frac{1}{A_1} + \\ldots + \\frac{1}{A_N}}.

\n

Your output will be judged correct when its absolute or relative error from the judge's output is at most 10^{-5}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n10 30\n
\n
\n
\n
\n
\n

Sample Output 1

7.5\n
\n

\\frac{1}{\\frac{1}{10} + \\frac{1}{30}} = \\frac{1}{\\frac{4}{30}} = \\frac{30}{4} = 7.5.

\n

Printing 7.50001, 7.49999, and so on will also be accepted.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n200 200 200\n
\n
\n
\n
\n
\n

Sample Output 2

66.66666666666667\n
\n

\\frac{1}{\\frac{1}{200} + \\frac{1}{200} + \\frac{1}{200}} = \\frac{1}{\\frac{3}{200}} = \\frac{200}{3} = 66.6666....

\n

Printing 6.66666e+1 and so on will also be accepted.

\n
\n
\n
\n
\n
\n

Sample Input 3

1\n1000\n
\n
\n
\n
\n
\n

Sample Output 3

1000\n
\n

\\frac{1}{\\frac{1}{1000}} = 1000.

\n

Printing +1000.0 and so on will also be accepted.

\n
\n
", "c_code": "int solution() {\n int num = 0;\n int i = 0;\n double sum = 0.0;\n double ans = 0.0;\n scanf(\"%d\", &num);\n int yoshio[num];\n for (i = 0; i < num; i++) {\n scanf(\"%d\", &yoshio[i]);\n sum += 1 / (double)yoshio[i];\n }\n ans = 1 / sum;\n printf(\"%lf\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let vec_str: Vec = s.split_whitespace().map(|x| x.to_string()).collect();\n let _N = vec_str[0].parse::().unwrap();\n\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let vec_str: Vec = s.split_whitespace().map(|x| x.to_string()).collect();\n let a_vec: Vec = vec_str.iter().map(|x| x.parse::().unwrap()).collect();\n\n println!(\"{:?}\", 1.0 / a_vec.iter().fold(0.0, |sum, x| sum + 1.0 / x));\n}", "difficulty": "hard"} {"problem_id": "1372", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You've come to your favorite store Infinitesco to buy some ice tea.

\n

The store sells ice tea in bottles of different volumes at different costs.\nSpecifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen.\nThe store has an infinite supply of bottles of each type.

\n

You want to buy exactly N liters of ice tea. How many yen do you have to spend?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq Q, H, S, D \\leq 10^8
  • \n
  • 1 \\leq N \\leq 10^9
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
Q H S D\nN\n
\n
\n
\n
\n
\n

Output

Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

20 30 70 90\n3\n
\n
\n
\n
\n
\n

Sample Output 1

150\n
\n

Buy one 2-liter bottle and two 0.5-liter bottles. You'll get 3 liters for 90 + 30 + 30 = 150 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

10000 1000 100 10\n1\n
\n
\n
\n
\n
\n

Sample Output 2

100\n
\n

Even though a 2-liter bottle costs just 10 yen, you need only 1 liter.\nThus, you have to buy a 1-liter bottle for 100 yen.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 100 1000 10000\n1\n
\n
\n
\n
\n
\n

Sample Output 3

40\n
\n

Now it's better to buy four 0.25-liter bottles for 10 + 10 + 10 + 10 = 40 yen.

\n
\n
\n
\n
\n
\n

Sample Input 4

12345678 87654321 12345678 87654321\n123456789\n
\n
\n
\n
\n
\n

Sample Output 4

1524157763907942\n
\n
\n
", "c_code": "int solution(void) {\n long long int q;\n long long int h;\n long long int s;\n long long int d;\n long long int n;\n scanf(\"%lld %lld %lld %lld\", &q, &h, &s, &d);\n scanf(\"%lld\", &n);\n long long int cost = 0;\n long long int count = 0;\n if (q * 8 < h * 4 && q * 8 < s * 2 && q * 8 < d) {\n count = n / 0.25;\n n -= 0.25 * count;\n cost += q * count;\n printf(\"%lld\", cost);\n return 0;\n }\n if (h * 4 < q * 8 && h * 4 < s * 2 && h * 4 < d) {\n count = n / 0.5;\n n -= 0.5 * count;\n cost += h * count;\n if (n == 0) {\n printf(\"%lld\", cost);\n return 0;\n }\n count = n / 0.25;\n n -= 0.25 * count;\n cost += q * count;\n printf(\"%lld\", cost);\n return 0;\n }\n if (s * 2 < h * 4 && s * 2 < q * 8 && s * 2 < d) {\n count = n / 1;\n n -= 1 * count;\n cost += s * count;\n if (n == 0) {\n printf(\"%lld\", cost);\n return 0;\n }\n if (q * 8 < h * 4 && q * 8 < d) {\n count = n / 0.25;\n n -= 0.25 * count;\n cost += q * count;\n printf(\"%lld\", cost);\n return 0;\n } else if (h * 4 < q * 8 && h * 4 < d) {\n count = n / 0.5;\n n -= 0.5 * count;\n cost += h * count;\n if (n == 0) {\n printf(\"%lld\", cost);\n return 0;\n }\n count = n / 0.25;\n n -= 0.25 * count;\n cost += q * count;\n printf(\"%lld\", cost);\n return 0;\n }\n } else if (d < h * 4 && d < s * 2 && d < q * 8) {\n count = n / 2;\n n -= 2 * count;\n cost += d * count;\n if (n == 0) {\n printf(\"%lld\", cost);\n return 0;\n }\n if (q * 8 < h * 4 && q * 8 < s * 2) {\n count = n / 0.25;\n n -= 0.25 * count;\n cost += q * count;\n printf(\"%lld\", cost);\n return 0;\n } else if (h * 4 < q * 8 && h * 4 < s * 2) {\n count = n / 0.5;\n n -= 0.5 * count;\n cost += h * count;\n if (n == 0) {\n printf(\"%lld\", cost);\n return 0;\n }\n count = n / 0.25;\n n -= 0.25 * count;\n cost += q * count;\n printf(\"%lld\", cost);\n return 0;\n } else if (s * 2 < h * 4 && s * 2 < q * 8) {\n count = n / 1;\n n -= 1 * count;\n cost += s * count;\n if (n == 0) {\n printf(\"%lld\", cost);\n return 0;\n }\n if (q * 8 < h * 4) {\n count = n / 0.25;\n n -= 0.25 * count;\n cost += q * count;\n printf(\"%lld\", cost);\n return 0;\n } else if (h * 4 < q * 8) {\n count = n / 0.5;\n n -= 0.5 * count;\n cost += h * count;\n if (n == 0) {\n printf(\"%lld\", cost);\n return 0;\n }\n count = n / 0.25;\n n -= 0.25 * count;\n cost += q * count;\n printf(\"%lld\", cost);\n return 0;\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (Q, H, S, D): (i64, i64, i64, 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 iter.next().unwrap().parse().unwrap(),\n )\n };\n let N: i64 = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n let C = {\n let mut C = vec![(8 * Q, 0), (4 * H, 1), (2 * S, 2), (D, 3)];\n C.sort();\n C\n };\n let (c, i) = C[0];\n let ans = if i == 3 {\n (N / 2) * c + (N % 2) * (C[1].0 / 2)\n } else {\n N * (c / 2)\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1373", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

In this problem, we only consider strings consisting of lowercase English letters.

\n

Strings s and t are said to be isomorphic when the following conditions are satisfied:

\n
    \n
  • |s| = |t| holds.
  • \n
  • For every pair i, j, one of the following holds:
      \n
    • s_i = s_j and t_i = t_j.
    • \n
    • s_i \\neq s_j and t_i \\neq t_j.
    • \n
    \n
  • \n
\n

For example, abcac and zyxzx are isomorphic, while abcac and ppppp are not.

\n

A string s is said to be in normal form when the following condition is satisfied:

\n
    \n
  • For every string t that is isomorphic to s, s \\leq t holds. Here \\leq denotes lexicographic comparison.
  • \n
\n

For example, abcac is in normal form, but zyxzx is not since it is isomorphic to abcac, which is lexicographically smaller than zyxzx.

\n

You are given an integer N.\nPrint all strings of length N that are in normal form, in lexicographically ascending order.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Assume that there are K strings of length N that are in normal form: w_1, \\ldots, w_K in lexicographical order.\nOutput should be in the following format:

\n
w_1\n:\nw_K\n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1\n
\n
\n
\n
\n
\n

Sample Output 1

a\n
\n
\n
\n
\n
\n
\n

Sample Input 2

2\n
\n
\n
\n
\n
\n

Sample Output 2

aa\nab\n
\n
\n
", "c_code": "int solution(void) {\n\n int n;\n scanf(\"%d\", &n);\n int generate[n];\n for (int i = 0; i < n; i++) {\n generate[i] = 0;\n }\n int max[n];\n while (1) {\n for (int i = 0; i < n; i++) {\n max[0] = 0;\n if (i != 0 && generate[i] > max[i - 1]) {\n max[i] = generate[i];\n } else {\n max[i] = max[i - 1];\n }\n printf(\"%c\", 'a' + generate[i]);\n }\n printf(\"\\n\");\n if (generate[n - 1] == n - 1) {\n break;\n }\n for (int i = n - 1; i >= 1; i--) {\n if (generate[i] > max[i - 1]) {\n generate[i] = 0;\n } else {\n generate[i]++;\n break;\n }\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n };\n let mut s = vec![0; n];\n loop {\n for c in s.iter() {\n print!(\"{}\", (*c + b'a') as char);\n }\n println!();\n let mut change = false;\n for i in (1..n).rev() {\n if s[i] == s[..i].iter().max().unwrap() + 1 {\n s[i] = 0\n } else {\n s[i] += 1;\n change = true;\n break;\n }\n }\n if !change {\n break;\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1374", "problem_description": "Pigeonhole sort", "c_code": "void solution(int arr[], int size) {\n int i, j, min = arr[0], max = arr[0], range;\n\n for (i = 1; i < size; i++) {\n if (arr[i] < min)\n min = arr[i];\n if (arr[i] > max)\n max = arr[i];\n }\n range = max - min + 1;\n\n int *holes = (int *)malloc(sizeof(int) * range);\n for (i = 0; i < range; i++) {\n holes[i] = 0;\n }\n for (i = 0; i < size; i++) {\n holes[arr[i] - min]++;\n }\n\n j = 0;\n for (i = 0; i < range; i++) {\n while (holes[i] > 0) {\n arr[j] = i + min;\n holes[i]--;\n j++;\n }\n }\n\n free(holes);\n}\n", "rust_code": "fn solution(array: &mut [i32]) {\n if let (Some(min), Some(max)) = (array.iter().min(), array.iter().max()) {\n let holes_range: usize = (max - min + 1) as usize;\n let mut holes = vec![0; holes_range];\n let mut holes_repeat = vec![0; holes_range];\n for i in array.iter() {\n let index = *i - min;\n holes[index as usize] = *i;\n holes_repeat[index as usize] += 1;\n }\n let mut index = 0;\n for i in 0..holes_range {\n while holes_repeat[i] > 0 {\n array[index] = holes[i];\n index += 1;\n holes_repeat[i] -= 1;\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1375", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N people. The name of the i-th person is S_i.

\n

We would like to choose three people so that the following conditions are met:

\n
    \n
  • The name of every chosen person begins with M, A, R, C or H.
  • \n
  • There are no multiple people whose names begin with the same letter.
  • \n
\n

How many such ways are there to choose three people, disregarding order?

\n

Note that the answer may not fit into a 32-bit integer type.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • S_i consists of uppercase English letters.
  • \n
  • 1 \\leq |S_i| \\leq 10
  • \n
  • S_i \\neq S_j (i \\neq j)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS_1\n:\nS_N\n
\n
\n
\n
\n
\n

Output

If there are x ways to choose three people so that the given conditions are met, print x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\nMASHIKE\nRUMOI\nOBIRA\nHABORO\nHOROKANAI\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

We can choose three people with the following names:

\n
    \n
  • \n

    MASHIKE, RUMOI, HABORO

    \n
  • \n
  • \n

    MASHIKE, RUMOI, HOROKANAI

    \n
  • \n
\n

Thus, we have two ways.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\nZZ\nZZZ\nZ\nZZZZZZZZZZ\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Note that there may be no ways to choose three people so that the given conditions are met.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\nCHOKUDAI\nRNG\nMAKOTO\nAOKI\nRINGO\n
\n
\n
\n
\n
\n

Sample Output 3

7\n
\n
\n
", "c_code": "int solution(void) {\n int num;\n long long int m = 0;\n long long int a = 0;\n long long int r = 0;\n long long int c = 0;\n long long int h = 0;\n\n long long int ans = 0;\n long long int mix[10];\n char name[100000][11];\n scanf(\"%d\", &num);\n\n for (int i = 0; i < num; i++) {\n scanf(\"%s\", name[i]);\n switch (name[i][0]) {\n case 'M':\n m++;\n break;\n case 'A':\n a++;\n break;\n case 'R':\n r++;\n break;\n case 'C':\n c++;\n break;\n case 'H':\n h++;\n break;\n default:\n break;\n }\n }\n\n mix[0] = m * a * r;\n mix[1] = m * a * c;\n mix[2] = m * a * h;\n mix[3] = m * r * c;\n mix[4] = m * r * h;\n mix[5] = m * c * h;\n mix[6] = a * r * c;\n mix[7] = a * r * h;\n mix[8] = a * c * h;\n mix[9] = r * c * h;\n\n for (int i = 0; i < 10; i++) {\n ans += mix[i];\n }\n printf(\"%lld\\n\", ans);\n}", "rust_code": "fn solution() {\n let mut buf: String = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let n = buf.trim().parse::().unwrap();\n let mut map: HashMap = HashMap::new();\n for _ in 0..n {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let c: char = buf.chars().next().unwrap();\n if c != 'M' && c != 'A' && c != 'R' && c != 'C' && c != 'H' {\n continue;\n }\n let i: i64 = match map.get(&c) {\n Some(i) => *i,\n None => 0,\n };\n map.insert(c, i + 1);\n }\n let ans: i64 = match map.len() {\n 3 => map.values().copied().product(),\n 4 => {\n let vs = map.values().copied().collect::>();\n vs[0] * vs[1] * vs[2]\n + vs[1] * vs[2] * vs[3]\n + vs[2] * vs[3] * vs[0]\n + vs[3] * vs[0] * vs[1]\n }\n 5 => {\n let vs = map.values().copied().collect::>();\n vs[0] * vs[1] * vs[2]\n + vs[1] * vs[2] * vs[3]\n + vs[2] * vs[3] * vs[4]\n + vs[3] * vs[4] * vs[0]\n + vs[4] * vs[0] * vs[1]\n + vs[0] * vs[1] * vs[3]\n + vs[1] * vs[2] * vs[4]\n + vs[2] * vs[3] * vs[0]\n + vs[3] * vs[4] * vs[1]\n + vs[4] * vs[0] * vs[2]\n }\n _ => 0,\n };\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1376", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

\n

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

\n

When traveling between these houses, you can only go around the pond.

\n

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq K \\leq 10^6
  • \n
  • 2 \\leq N \\leq 2 \\times 10^5
  • \n
  • 0 \\leq A_1 < ... < A_N < K
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

20 3\n5 10 15\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

\n
\n
\n
\n
\n
\n

Sample Input 2

20 3\n0 5 15\n
\n
\n
\n
\n
\n

Sample Output 2

10\n
\n

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.

\n
\n
", "c_code": "int solution(void) {\n\n int K = 0;\n int N = 0;\n int max = 0;\n\n scanf(\"%d %d\", &K, &N);\n\n int A[N];\n int distance[N];\n\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &A[i]);\n\n if (i > 0) {\n distance[i] = A[i] - A[i - 1];\n }\n if (distance[i] > max) {\n max = distance[i];\n }\n }\n distance[0] = (K - A[N - 1]) + A[0];\n\n if (distance[0] > max) {\n max = distance[0];\n }\n max = K - max;\n\n printf(\"%d\\n\", max);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n let mut s2 = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let s_spl: Vec<&str> = s.trim().split(' ').collect();\n let k: i32 = s_spl[0].to_string().parse().unwrap();\n let mut max = -1;\n let mut a_first = -1;\n let mut a_old = -1;\n io::stdin().read_line(&mut s2).unwrap();\n for s_loop in s2.trim().split(' ') {\n let a: i32 = s_loop.parse().unwrap();\n if a_old == -1 {\n a_first = a;\n } else {\n max = cmp::max(max, a - a_old);\n }\n a_old = a;\n }\n max = cmp::max(max, k + a_first - a_old);\n println!(\"{}\", k - max);\n}", "difficulty": "medium"} {"problem_id": "1377", "problem_description": "One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2·m from left to right, then the j-th flat of the i-th floor has windows 2·j - 1 and 2·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping.", "c_code": "int solution(void) {\n\n int n = 0;\n int m = 0;\n int count = 0;\n\n int floors[100][200] = {{0}};\n\n scanf(\"%d %d\", &n, &m);\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m * 2; j++) {\n scanf(\"%d\", floors[i] + j);\n if (j % 2 && floors[i][j - 1] + floors[i][j]) {\n count++;\n }\n }\n }\n\n printf(\"%d\\n\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin()\n .read_to_string(&mut input)\n .expect(\"input: read to string failed\");\n\n let vec = input\n .lines()\n .map(|line| line.split_whitespace())\n .map(|item| {\n item.map(|item| item.parse::().unwrap())\n .collect::>()\n })\n .collect::>>();\n\n let floors = vec[0][0];\n let flats_per_floor = vec[0][1];\n let windows = flats_per_floor * 2;\n\n let mut lighter = i32::default();\n\n for floor in 1usize..floors as usize + 1usize {\n for window in (0usize..windows as usize).step_by(2) {\n if vec[floor][window] != 0 || vec[floor][window + 1usize] != 0 {\n lighter += 1;\n }\n }\n }\n\n println!(\"{}\", &lighter);\n}", "difficulty": "medium"} {"problem_id": "1378", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given a string S consisting of lowercase English letters.\nAnother string T is initially empty.\nDetermine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

\n
    \n
  • Append one of the following at the end of T: dream, dreamer, erase and eraser.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦|S|≦10^5
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

erasedream\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

Append erase and dream at the end of T in this order, to obtain S = T.

\n
\n
\n
\n
\n
\n

Sample Input 2

dreameraser\n
\n
\n
\n
\n
\n

Sample Output 2

YES\n
\n

Append dream and eraser at the end of T in this order, to obtain S = T.

\n
\n
\n
\n
\n
\n

Sample Input 3

dreamerer\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
", "c_code": "int solution() {\n char s[100050];\n char temp[10];\n scanf(\"%s\", s);\n int i = 0;\n int end = strlen(s);\n int isPosible = 1;\n\n while (isPosible) {\n\n if (i == end) {\n break;\n }\n strncpy(temp, s + i, 5);\n i += 5;\n if (strcmp(temp, \"dream\") != 0 && strcmp(temp, \"erase\") != 0) {\n isPosible = 0;\n\n } else if (strcmp(temp, \"erase\") == 0) {\n\n if (s[i] == 'r') {\n\n i++;\n }\n } else {\n\n if (s[i] == 'e' && s[i + 1] == 'r') {\n\n strncpy(temp, s + i, 5);\n if (strcmp(temp, \"erase\") != 0) {\n i += 2;\n }\n }\n }\n }\n\n if (isPosible) {\n puts(\"YES\");\n } else {\n puts(\"NO\");\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).ok();\n let s = buf.trim();\n let n = s.len();\n let mut g = vec![Vec::new(); n];\n let mut reach = vec![0; n + 1];\n reach[0] = 1;\n for &t in &[\"dream\", \"dreamer\", \"erase\", \"eraser\"] {\n for p in s.match_indices(t) {\n g[p.0].push(p.0 + t.len());\n }\n }\n for i in 0..n {\n if reach[i] == 0 {\n continue;\n }\n for &t in &g[i] {\n reach[t] = 1;\n }\n }\n println!(\"{}\", if reach[n] == 1 { \"YES\" } else { \"NO\" });\n}", "difficulty": "medium"} {"problem_id": "1379", "problem_description": "Vova is again playing some computer game, now an RPG. In the game Vova's character received a quest: to slay the fearsome monster called Modcrab.After two hours of playing the game Vova has tracked the monster and analyzed its tactics. The Modcrab has h2 health points and an attack power of a2. Knowing that, Vova has decided to buy a lot of strong healing potions and to prepare for battle.Vova's character has h1 health points and an attack power of a1. Also he has a large supply of healing potions, each of which increases his current amount of health points by c1 when Vova drinks a potion. All potions are identical to each other. It is guaranteed that c1 > a2.The battle consists of multiple phases. In the beginning of each phase, Vova can either attack the monster (thus reducing its health by a1) or drink a healing potion (it increases Vova's health by c1; Vova's health can exceed h1). Then, if the battle is not over yet, the Modcrab attacks Vova, reducing his health by a2. The battle ends when Vova's (or Modcrab's) health drops to 0 or lower. It is possible that the battle ends in a middle of a phase after Vova's attack.Of course, Vova wants to win the fight. But also he wants to do it as fast as possible. So he wants to make up a strategy that will allow him to win the fight after the minimum possible number of phases.Help Vova to make up a strategy! You may assume that Vova never runs out of healing potions, and that he can always win.", "c_code": "int solution() {\n int map[10005];\n int sum = 0;\n int h1;\n int a1;\n int c1;\n int h2;\n int a2;\n scanf(\"%d %d %d %d %d\", &h1, &a1, &c1, &h2, &a2);\n for (int i = 0;; i++) {\n if (h1 > a2 && h2 > a1) {\n map[sum] = 1;\n h1 = h1 - a2;\n h2 = h2 - a1;\n\n sum++;\n continue;\n }\n if (h1 <= a2 && h2 > a1) {\n map[sum] = 2;\n h1 = h1 + c1;\n h1 = h1 - a2;\n\n sum++;\n continue;\n }\n if (h1 <= a2 && h2 <= a1) {\n map[sum] = 1;\n\n sum++;\n break;\n }\n if (h1 > a2 && h2 <= a1) {\n map[sum] = 1;\n\n sum++;\n break;\n }\n }\n printf(\"%d\\n\", sum);\n for (int i = 0; i < sum; i++) {\n if (map[i] == 1) {\n printf(\"STRIKE\\n\");\n } else if (map[i] == 2) {\n printf(\"HEAL\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::prelude::*;\n std::io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n let (mut h1, a1, c1, mut h2, a2) = (\n it.next().unwrap(),\n it.next().unwrap(),\n it.next().unwrap(),\n it.next().unwrap(),\n it.next().unwrap(),\n );\n\n let mut ans_str = String::new();\n\n let mut ans_n = 1;\n\n while h2 > a1 {\n ans_n += 1;\n if h1 > a2 {\n ans_str.push_str(\"STRIKE\\n\");\n h2 -= a1;\n } else {\n ans_str.push_str(\"HEAL\\n\");\n h1 += c1;\n }\n h1 -= a2;\n }\n\n ans_str.push_str(\"STRIKE\\n\");\n\n print!(\"{}\\n{}\", ans_n, ans_str);\n}", "difficulty": "easy"} {"problem_id": "1380", "problem_description": "You talked to Polycarp and asked him a question. You know that when he wants to answer \"yes\", he repeats Yes many times in a row.Because of the noise, you only heard part of the answer — some substring of it. That is, if he answered YesYes, then you could hear esY, YesYes, sYes, e, but you couldn't Yess, YES or se.Determine if it is true that the given string $$$s$$$ is a substring of YesYesYes... (Yes repeated many times in a row).", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n int i = 0;\n char s1[53] = \"YesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesY\";\n char s2[1000][51] = {0};\n char s3[1000][4] = {0};\n for (i = 0; i < t; i++) {\n\n scanf(\"%s\", s2[i]);\n if (strstr(s1, s2[i]) != NULL) {\n strcpy(s3[i], \"YES\");\n } else {\n strcpy(s3[i], \"NO\");\n }\n }\n for (i = 0; i < t; i++) {\n puts(s3[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let srch = \"YesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYesYes\";\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin().lines().skip(1).flatten().for_each(|s| {\n let ans = if srch.contains(s.as_str()) {\n \"YES\"\n } else {\n \"NO\"\n };\n writeln!(buf, \"{ans}\").ok();\n });\n}", "difficulty": "medium"} {"problem_id": "1381", "problem_description": "Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $$$a$$$ and $$$b$$$ ($$$a < b$$$). After that, the student can apply any of the following operations any number of times: $$$a := a + 1$$$ (increase $$$a$$$ by $$$1$$$), $$$b := b + 1$$$ (increase $$$b$$$ by $$$1$$$), $$$a := a \\ | \\ b$$$ (replace $$$a$$$ with the bitwise OR of $$$a$$$ and $$$b$$$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $$$a$$$ and $$$b$$$ equal.Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $$$a$$$ equal to $$$b$$$.", "c_code": "int solution() {\n int t;\n int a;\n int b;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d %d\", &a, &b);\n int a1 = 0;\n int b1 = 1;\n int a0 = a;\n int b0 = b;\n while ((a0 | b) != b) {\n a1++;\n a0++;\n }\n if (a0 != b) {\n a1++;\n }\n while ((a | b0) != b0) {\n b1++;\n b0++;\n }\n printf(\"%d\\n\", a1 < b1 ? a1 : b1);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let mut a = xs[0];\n let b = xs[1];\n let mut ans1 = 0;\n loop {\n if a == b {\n break;\n }\n if a | b == b {\n ans1 += 1;\n break;\n }\n a += 1;\n ans1 += 1;\n }\n let a = xs[0];\n let mut b = xs[1];\n let mut ans2 = 0;\n for _ in 0..ans1 {\n if a == b {\n break;\n }\n if a | b == b {\n ans2 += 1;\n break;\n }\n b += 1;\n ans2 += 1;\n }\n println!(\"{}\", min(ans1, ans2));\n }\n}", "difficulty": "easy"} {"problem_id": "1382", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Print all the integers that satisfies the following in ascending order:

\n
    \n
  • Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A \\leq B \\leq 10^9
  • \n
  • 1 \\leq K \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B K\n
\n
\n
\n
\n
\n

Output

Print all the integers that satisfies the condition above in ascending order.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 8 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n4\n7\n8\n
\n
    \n
  • 3 is the first smallest integer among the integers between 3 and 8.
  • \n
  • 4 is the second smallest integer among the integers between 3 and 8.
  • \n
  • 7 is the second largest integer among the integers between 3 and 8.
  • \n
  • 8 is the first largest integer among the integers between 3 and 8.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4 8 3\n
\n
\n
\n
\n
\n

Sample Output 2

4\n5\n6\n7\n8\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2 9 100\n
\n
\n
\n
\n
\n

Sample Output 3

2\n3\n4\n5\n6\n7\n8\n9\n
\n
\n
", "c_code": "int solution(void) {\n\n static int a;\n static int b;\n static int k;\n scanf(\"%d%d%d\", &a, &b, &k);\n\n if (a + k - 1 < b - k + 1) {\n\n for (int i = 0; i < k; i++) {\n\n printf(\"%d\\n\", a + i);\n }\n\n for (int i = 0; i < k; i++) {\n\n printf(\"%d\\n\", b - k + i + 1);\n }\n\n } else {\n\n for (int i = a; i <= b; i++) {\n\n printf(\"%d\\n\", i);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buff = String::new();\n std::io::stdin().read_to_string(&mut buff).unwrap();\n let mut iter = buff.split_whitespace();\n\n let a: i32 = iter.next().unwrap().parse().unwrap();\n let b: i32 = iter.next().unwrap().parse().unwrap();\n let k: i32 = iter.next().unwrap().parse().unwrap();\n\n let mut v: Vec = Vec::new();\n\n for i in a..(a + k) {\n if i <= b - k || i <= b {\n v.push(i)\n }\n }\n for i in (b - k + 1)..(b + 1) {\n if v.last().unwrap() < &i {\n v.push(i)\n }\n }\n\n for i in v {\n println!(\"{}\", i)\n }\n}", "difficulty": "easy"} {"problem_id": "1383", "problem_description": "

椅子の総数

\n\n

\n選手のみなさん、パソコン甲子園にようこそ。パソコン甲子園の本選は会津大学で行われ、会場内では\n1つの机に1チームが割り当てられます。パソコン甲子園は1チーム2人なので、チーム数×2脚の椅子が必要です。大学では、他にも様々なイベントの会場設営で机と椅子を準備する機会がありますが、必要な机と椅子の数も様々です。そこで、あるイベントに対して準備する机の数と、机1つあたりに必要な椅子の数が与えられたとき、必要な椅子の総数を計算するプログラムを作成してください。\n

\n\n

入力

\n

\n入力は以下の形式で与えられる。\n

\n
\nd c\n
\n

\n入力は1行であり、必要な机の数 d (1 ≤ d ≤ 100) と机1つあたりに必要な椅子の数 c (1 ≤ c ≤ 10) を表す整数が与えられる。\n

\n\n

出力

\n

\nイベントにおいて必要な椅子の総数を1行に出力する。\n

\n\n

入出力例

\n
\n\n

入力例1

\n
\n20 2 \n
\n\n

出力例1

\n
\n40\n
\n\n

入力例2

\n
\n1 1 \n
\n\n

出力例2

\n
\n1\n
", "c_code": "int solution() {\n int d = 0;\n int c = 0;\n scanf(\"%d%d\", &d, &c);\n printf(\"%d\\n\", d * c);\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\n let (d, c) = {\n let mut iter = input.split_whitespace().map(|s| s.parse::().unwrap());\n\n (iter.next().unwrap(), iter.next().unwrap())\n };\n\n println!(\"{}\", d * c);\n}", "difficulty": "easy"} {"problem_id": "1384", "problem_description": "You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once?", "c_code": "int solution() {\n int x;\n char str[200007];\n int n[11];\n\n scanf(\"%d\", &x);\n\n scanf(\"%s\", str);\n\n for (int i = 0; i < 9; i++) {\n scanf(\"%d\", &n[i + 1]);\n }\n int flag = 0;\n for (int i = 0; i < x; i++) {\n if ((flag && str[i] - '0' <= n[str[i] - '0']) ||\n (!flag && str[i] - '0' < n[str[i] - '0'])) {\n flag = 1;\n }\n if (flag && (str[i] - '0') > n[str[i] - '0']) {\n break;\n }\n if (flag) {\n str[i] = n[str[i] - '0'] + '0';\n }\n }\n printf(\"%s\", str);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut _buffer = String::new();\n io::stdin()\n .read_line(&mut _buffer)\n .expect(\"failed to read input\");\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let mut a = buffer\n .trim()\n .chars()\n .map(|c| c.to_digit(10).unwrap() as u8)\n .collect::>();\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let f = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let mut started = false;\n for i in &mut a {\n if f[*i as usize - 1] > *i {\n if !started {\n started = true;\n }\n *i = f[*i as usize - 1];\n } else if f[*i as usize - 1] < *i && started {\n break;\n }\n }\n for i in &a {\n print!(\"{}\", *i);\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1385", "problem_description": "There are three sticks with integer lengths $$$l_1, l_2$$$ and $$$l_3$$$.You are asked to break exactly one of them into two pieces in such a way that: both pieces have positive (strictly greater than $$$0$$$) integer length; the total length of the pieces is equal to the original length of the stick; it's possible to construct a rectangle from the resulting four sticks such that each stick is used as exactly one of its sides. A square is also considered a rectangle.Determine if it's possible to do that.", "c_code": "int solution()\n\n{\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int aa[3];\n for (int j = 0; j < 3; j++) {\n scanf(\"%d\", &aa[j]);\n }\n\n if ((aa[0] != aa[1]) && (aa[0] != aa[2]) && (aa[1] != aa[2])) {\n int max = -22;\n for (int j = 0; j < 3; j++) {\n if (aa[j] >= max) {\n max = aa[j];\n }\n }\n int sum = 0;\n for (int j = 0; j < 3; j++) {\n sum = sum + aa[j];\n }\n if (sum - 2 * max == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n } else if ((aa[0] == aa[1]) && (aa[0] == aa[2]) && (aa[1] == aa[2])) {\n if (aa[0] % 2 == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n } else {\n if (aa[0] == aa[1]) {\n if (aa[2] % 2 == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n } else if (aa[0] == aa[2]) {\n if (aa[1] % 2 == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n } else if (aa[1] == aa[2]) {\n if (aa[0] % 2 == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).expect(\"read t\");\n let t: i32 = t.trim().parse().expect(\"parse t\");\n\n for _ in 0..t {\n let mut l = String::new();\n io::stdin().read_line(&mut l).expect(\"read l\");\n let l: Result, _> = l.trim().split(' ').map(str::parse).collect();\n let mut l = l.expect(\"parse l\");\n l.sort();\n if (l[0] + l[1] == l[2])\n || (l[0] % 2 == 0 && l[1] == l[2])\n || (l[0] == l[1] && l[2] % 2 == 0)\n {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1386", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

\n

In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short.
\nThe name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string.

\n

Given a string S of length N and an integer k (k \\geq 3),\nhe defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions:

\n
    \n
  • 0 \\leq a < b < c \\leq N - 1
  • \n
  • S[a] = D
  • \n
  • S[b] = M
  • \n
  • S[c] = C
  • \n
  • c-a < k
  • \n
\n

Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \\leq a \\leq N - 1 holds.

\n

For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \\leq i \\leq Q-1).

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 3 \\leq N \\leq 10^6
  • \n
  • S consists of uppercase English letters
  • \n
  • 1 \\leq Q \\leq 75
  • \n
  • 3 \\leq k_i \\leq N
  • \n
  • All numbers given in input are integers
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nS\nQ\nk_{0} k_{1} ... k_{Q-1}\n
\n
\n
\n
\n
\n

Output

\n

Print Q lines.\nThe i-th line should contain the k_i-DMC number of the string S.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

\n
18\nDWANGOMEDIACLUSTER\n1\n18\n
\n
\n
\n
\n
\n

Sample Output 1

\n
1\n
\n

(a,b,c) = (0, 6, 11) satisfies the conditions.
\nStrangely, Dwango Media Cluster does not have so much DMC-ness by his definition.

\n
\n
\n
\n
\n
\n

Sample Input 2

\n
18\nDDDDDDMMMMMCCCCCCC\n1\n18\n
\n
\n
\n
\n
\n

Sample Output 2

\n
210\n
\n

The number of triples can be calculated as 6\\times 5\\times 7.

\n
\n
\n
\n
\n
\n

Sample Input 3

\n
54\nDIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED\n3\n20 30 40\n
\n
\n
\n
\n
\n

Sample Output 3

\n
0\n1\n2\n
\n

(a, b, c) = (0, 23, 36), (8, 23, 36) satisfy the conditions except the last one, namely, c-a < k_i.
\nBy the way, DWANGO is an acronym for \"Dial-up Wide Area Network Gaming Operation\".

\n
\n
\n
\n
\n
\n

Sample Output 4

\n
30\nDMCDMCDMCDMCDMCDMCDMCDMCDMCDMC\n4\n5 10 15 20\n
\n
\n
\n
\n
\n

Sample Output 4

\n
10\n52\n110\n140\n
\n
\n
", "c_code": "int solution() {\n int l = 1000010;\n int n;\n int q;\n int d[l];\n long long m[l];\n long long c[l];\n long long x[l];\n char s[l];\n for (int i = 0; i < l; i++) {\n d[i] = -1;\n }\n scanf(\"%d%s%d\", &n, s, &q);\n int j = 0;\n for (int i = 1; i <= n; i++) {\n m[i] = m[i - 1];\n c[i] = c[i - 1];\n x[i] = x[i - 1];\n if (s[i - 1] == 'D') {\n d[j++] = i - 1;\n } else if (s[i - 1] == 'M') {\n m[i]++;\n } else if (s[i - 1] == 'C') {\n c[i]++;\n x[i] += m[i];\n }\n }\n while (q--) {\n long long a = 0;\n int k;\n scanf(\"%d\", &k);\n for (int i = 0; d[i] > -1; i++) {\n int j = d[i];\n int l = j + k;\n if (l > n) {\n l = n;\n }\n a += x[l] - x[j] - (c[l] - c[j]) * m[j];\n }\n printf(\"%lld\\n\", a);\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::{Read, Write};\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut it = s.split_whitespace();\n let _n: usize = it.next().unwrap().parse().unwrap();\n let s: Vec<_> = it.next().unwrap().chars().collect();\n let _q: usize = it.next().unwrap().parse().unwrap();\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n for k in it.map(|a| a.parse::().unwrap()) {\n let mut ans = 0u64;\n let mut cnt = (0, 0, 0);\n for i in 0..s.len() {\n match s[i] {\n 'D' => cnt.0 += 1,\n 'M' => {\n cnt.1 += 1;\n cnt.2 += cnt.0;\n }\n 'C' => {\n ans += cnt.2;\n }\n _ => (),\n }\n if i < k - 1 {\n continue;\n }\n match s[i - (k - 1)] {\n 'D' => {\n cnt.0 -= 1;\n cnt.2 -= cnt.1;\n }\n 'M' => {\n cnt.1 -= 1;\n }\n _ => (),\n }\n }\n writeln!(out, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1387", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

\n

Given is a string S. Replace every character in S with x and print the result.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • S is a string consisting of lowercase English letters.
  • \n
  • The length of S is between 1 and 100 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

\n

Replace every character in S with x and print the result.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

sardine\n
\n
\n
\n
\n
\n

Sample Output 1

xxxxxxx\n
\n

Replacing every character in S with x results in xxxxxxx.

\n
\n
\n
\n
\n
\n

Sample Input 2

xxxx\n
\n
\n
\n
\n
\n

Sample Output 2

xxxx\n
\n
\n
\n
\n
\n
\n

Sample Input 3

gone\n
\n
\n
\n
\n
\n

Sample Output 3

xxxx\n
\n
\n
", "c_code": "int solution() {\n char S[200];\n int i = 0;\n\n scanf(\"%s\", S);\n\n while (S[i] != '\\0') {\n if (S[i] >= 'a' && S[i] <= 'z') {\n S[i] = 'x';\n }\n i++;\n }\n\n printf(\"%s\\n\", S);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n buf = buf.trim().to_string();\n for _n in 0..buf.len() {\n print!(\"x\");\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1388", "problem_description": "In order to celebrate Twice's 5th anniversary, Tzuyu and Sana decided to play a game.Tzuyu gave Sana two integers $$$a$$$ and $$$b$$$ and a really important quest.In order to complete the quest, Sana has to output the smallest possible value of ($$$a \\oplus x$$$) + ($$$b \\oplus x$$$) for any given $$$x$$$, where $$$\\oplus$$$ denotes the bitwise XOR operation.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int a = 0;\n int b = 0;\n int x = 0;\n scanf(\"%d %d\", &a, &b);\n if (a == b) {\n printf(\"0\\n\");\n } else {\n x = a ^ b;\n printf(\"%d\\n\", x);\n }\n }\n}", "rust_code": "fn solution() {\n let mut input = \"\".split_ascii_whitespace();\n let mut read = || loop {\n if let Some(word) = input.next() {\n break word;\n }\n input = {\n let mut input = \"\".to_owned();\n io::stdin().read_line(&mut input).unwrap();\n if input.is_empty() {\n panic!(\"reached EOF\");\n }\n Box::leak(input.into_boxed_str()).split_ascii_whitespace()\n };\n };\n\n\n let t = read!(usize);\n for _ in 0..t {\n let a = read!(i64);\n let b = read!(i64);\n println!(\"{}\", a ^ b);\n }\n}", "difficulty": "medium"} {"problem_id": "1389", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Snuke lives on an infinite two-dimensional plane. He is going on an N-day trip.\nAt the beginning of Day 1, he is at home. His plan is described in a string S of length N.\nOn Day i(1 ≦ i ≦ N), he will travel a positive distance in the following direction:

\n
    \n
  • North if the i-th letter of S is N
  • \n
  • West if the i-th letter of S is W
  • \n
  • South if the i-th letter of S is S
  • \n
  • East if the i-th letter of S is E
  • \n
\n

He has not decided each day's travel distance. Determine whether it is possible to set each day's travel distance so that he will be back at home at the end of Day N.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ | S | ≦ 1000
  • \n
  • S consists of the letters N, W, S, E.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print Yes if it is possible to set each day's travel distance so that he will be back at home at the end of Day N. Otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

SENW\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

If Snuke travels a distance of 1 on each day, he will be back at home at the end of day 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

NSNNSNSN\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 3

NNEW\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
\n
\n
\n
\n

Sample Input 4

W\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
", "c_code": "int solution() {\n char s[1111];\n scanf(\"%s\", s);\n char *N = strchr(s, 'N');\n char *S = strchr(s, 'S');\n char *E = strchr(s, 'E');\n char *W = strchr(s, 'W');\n int ans = 0;\n if ((N != 0 && S != 0) || (N == 0 && S == 0)) {\n if ((E != 0 && W != 0) || (E == 0 && W == 0)) {\n ans = 1;\n }\n }\n puts(ans ? \"Yes\" : \"No\");\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 s = vec[0];\n\n let mut ncnt = 0;\n let mut scnt = 0;\n let mut ecnt = 0;\n let mut wcnt = 0;\n for i in s.chars() {\n match i {\n 'N' => {\n ncnt += 1;\n }\n 'S' => {\n scnt += 1;\n }\n 'E' => {\n ecnt += 1;\n }\n 'W' => {\n wcnt += 1;\n }\n _ => {}\n }\n }\n if (ncnt == 0 && scnt != 0)\n || (scnt == 0 && ncnt != 0)\n || (ecnt == 0 && wcnt != 0)\n || (wcnt == 0 && ecnt != 0)\n {\n println!(\"No\");\n } else {\n println!(\"Yes\");\n }\n}", "difficulty": "easy"} {"problem_id": "1390", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are N stones, numbered 1, 2, \\ldots, N.\nFor each i (1 \\leq i \\leq N), the height of Stone i is h_i.

\n

There is a frog who is initially on Stone 1.\nHe will repeat the following action some number of times to reach Stone N:

\n
    \n
  • If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.
  • \n
\n

Find the minimum possible total cost incurred before the frog reaches Stone N.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq h_i \\leq 10^4
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nh_1 h_2 \\ldots h_N\n
\n
\n
\n
\n
\n

Output

Print the minimum possible total cost incurred.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n10 30 40 20\n
\n
\n
\n
\n
\n

Sample Output 1

30\n
\n

If we follow the path 124, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n10 10\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

If we follow the path 12, the total cost incurred would be |10 - 10| = 0.

\n
\n
\n
\n
\n
\n

Sample Input 3

6\n30 10 60 10 60 50\n
\n
\n
\n
\n
\n

Sample Output 3

40\n
\n

If we follow the path 1356, the total cost incurred would be |30 - 60| + |60 - 60| + |60 - 50| = 40.

\n
\n
", "c_code": "int solution() {\n int N;\n scanf(\"%d\", &N);\n int h[N];\n int dp[N];\n for (int i = 1; i <= N; i++) {\n scanf(\"%d\", &h[i]);\n }\n\n dp[0] = 0;\n for (int i = 1; i <= N - 1; i++) {\n dp[i] = dp[i - 1] + abs(h[i] - h[i + 1]);\n if (i > 1) {\n if (dp[i] > (dp[i - 2] + abs(h[i - 1] - h[i + 1]))) {\n dp[i] = dp[i - 2] + abs(h[i - 1] - h[i + 1]);\n }\n }\n }\n printf(\"%d\", dp[N - 1]);\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 a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let mut dp: Vec = vec![1 << 30; n + 2];\n dp[0] = 0;\n for i in 0..n - 1 {\n dp[i + 1] = std::cmp::min(dp[i + 1], dp[i] + (a[i + 1] - a[i]).abs());\n\n if i + 2 < n {\n dp[i + 2] = std::cmp::min(dp[i + 2], dp[i] + (a[i + 2] - a[i]).abs());\n }\n }\n println!(\"{}\", dp[n - 1]);\n}", "difficulty": "medium"} {"problem_id": "1391", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Given is a string S consisting of L and R.

\n

Let N be the length of S. There are N squares arranged from left to right, and the i-th character of S from the left is written on the i-th square from the left.

\n

The character written on the leftmost square is always R, and the character written on the rightmost square is always L.

\n

Initially, one child is standing on each square.

\n

Each child will perform the move below 10^{100} times:

\n
    \n
  • Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right.
  • \n
\n

Find the number of children standing on each square after the children performed the moves.

\n
\n
\n
\n
\n

Constraints

    \n
  • S is a string of length between 2 and 10^5 (inclusive).
  • \n
  • Each character of S is L or R.
  • \n
  • The first and last characters of S are R and L, respectively.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the number of children standing on each square after the children performed the moves, in order from left to right.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

RRLRL\n
\n
\n
\n
\n
\n

Sample Output 1

0 1 2 1 1\n
\n
    \n
  • After each child performed one move, the number of children standing on each square is 0, 2, 1, 1, 1 from left to right.
  • \n
  • After each child performed two moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.
  • \n
  • After each child performed 10^{100} moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

RRLLLLRLRRLL\n
\n
\n
\n
\n
\n

Sample Output 2

0 3 3 0 0 0 1 1 0 2 2 0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

RRRLLRLLRRRLLLLL\n
\n
\n
\n
\n
\n

Sample Output 3

0 0 3 2 0 2 1 0 0 0 4 4 0 0 0 0\n
\n
\n
", "c_code": "int solution() {\n char a[100000];\n scanf(\"%s\", a);\n int cnt = 0;\n while (a[cnt] != '\\0') {\n cnt++;\n }\n int b[100000] = {0};\n for (int i = 0; i < cnt; i++) {\n if (a[i] == 'R' && a[i + 1] == 'L') {\n b[i]++;\n b[i + 1]++;\n int temp1 = 1;\n int temp2 = 2;\n while (a[i - temp1] == 'R' && (i - temp1) >= 0) {\n b[i + (temp1 % 2)]++;\n temp1++;\n }\n while (a[i + temp2] == 'L' && (i + temp2) < cnt) {\n b[i + (temp2 % 2)]++;\n temp2++;\n }\n }\n }\n for (int i = 0; i < cnt; i++) {\n printf(\"%d \", b[i]);\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n\n let s = {\n let mut input = String::new();\n stdin.read_line(&mut input).unwrap();\n input.trim().to_string()\n };\n\n let vec = {\n let mut vec = Vec::with_capacity(100_000);\n\n let mut state = 'R';\n let mut counter: i32 = 0;\n\n for c in s.chars() {\n if c == state {\n counter += 1;\n } else {\n vec.push(counter);\n counter = 1;\n state = if state == 'R' { 'L' } else { 'R' };\n }\n }\n vec.push(counter);\n\n vec\n };\n\n for i in 0..vec.len() / 2 {\n let i = 2 * i;\n let x = (vec[i] + 1) / 2 + vec[i + 1] / 2;\n let y = vec[i] / 2 + (vec[i + 1] + 1) / 2;\n\n let xp: Vec = (0..2 * vec[i] - 2)\n .map(|i| if i % 2 == 0 { 48 } else { 32 })\n .collect();\n let yp: Vec = (0..2 * vec[i + 1] - 2)\n .map(|i| if i % 2 == 0 { 48 } else { 32 })\n .collect();\n\n print!(\n \"{}{} {} {}\",\n String::from_utf8(xp).unwrap(),\n x,\n y,\n String::from_utf8(yp).unwrap(),\n );\n }\n\n println!();\n}", "difficulty": "easy"} {"problem_id": "1392", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Does \\sqrt{a} + \\sqrt{b} < \\sqrt{c} hold?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq a, b, c \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a \\ b \\ c\n
\n
\n
\n
\n
\n

Output

If \\sqrt{a} + \\sqrt{b} < \\sqrt{c}, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 9\n
\n
\n
\n
\n
\n

Sample Output 1

No\n
\n

\\sqrt{2} + \\sqrt{3} < \\sqrt{9} does not hold.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 3 10\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n

\\sqrt{2} + \\sqrt{3} < \\sqrt{10} holds.

\n
\n
", "c_code": "int solution() {\n long a;\n long b;\n long c;\n scanf(\"%ld %ld %ld\", &a, &b, &c);\n if (!((a >= 1 && a <= 1000000000) && (b >= 1 && b <= 1000000000) &&\n (c >= 1 && c <= 1000000000))) {\n printf(\"No\");\n return (0);\n }\n if (c - a - b < 0) {\n if (4 * (a * b) < (c - a - b) * (c - a - b) * (-1)) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n } else {\n if (4 * (a * b) < (c - a - b) * (c - a - b)) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\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 a: i64 = itr.next().unwrap().parse().unwrap();\n let b: i64 = itr.next().unwrap().parse().unwrap();\n let c: i64 = itr.next().unwrap().parse().unwrap();\n let x = 4 * a * b;\n let y = c - a - b;\n if y > 0 && x < y * y {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "easy"} {"problem_id": "1393", "problem_description": "Recently a new building with a new layout was constructed in Monocarp's hometown. According to this new layout, the building consists of three types of apartments: three-room, five-room, and seven-room apartments. It's also known that each room of each apartment has exactly one window. In other words, a three-room apartment has three windows, a five-room — five windows, and a seven-room — seven windows.Monocarp went around the building and counted $$$n$$$ windows. Now he is wondering, how many apartments of each type the building may have.Unfortunately, Monocarp only recently has learned to count, so he is asking you to help him to calculate the possible quantities of three-room, five-room, and seven-room apartments in the building that has $$$n$$$ windows. If there are multiple answers, you can print any of them.Here are some examples: if Monocarp has counted $$$30$$$ windows, there could have been $$$2$$$ three-room apartments, $$$2$$$ five-room apartments and $$$2$$$ seven-room apartments, since $$$2 \\cdot 3 + 2 \\cdot 5 + 2 \\cdot 7 = 30$$$; if Monocarp has counted $$$67$$$ windows, there could have been $$$7$$$ three-room apartments, $$$5$$$ five-room apartments and $$$3$$$ seven-room apartments, since $$$7 \\cdot 3 + 5 \\cdot 5 + 3 \\cdot 7 = 67$$$; if Monocarp has counted $$$4$$$ windows, he should have mistaken since no building with the aforementioned layout can have $$$4$$$ windows.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int m = 0; m < t; m++) {\n int n;\n int flag = 1;\n scanf(\"%d\", &n);\n for (int i = 0; 3 * i <= n && flag; i++) {\n for (int j = 0; 3 * i + 5 * j <= n && flag; j++) {\n for (int k = 0; 3 * i + 5 * j + 7 * k <= n && flag; k++) {\n if (3 * i + 5 * j + 7 * k == n) {\n printf(\"%d %d %d\\n\", i, j, k);\n flag = 0;\n }\n }\n }\n }\n if (flag == 1) {\n printf(\"-1\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let test: i64 = str.trim().parse().unwrap();\n for _ in 0..test {\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let n: i64 = str.trim().parse().unwrap();\n if n == 3 {\n println!(\"1 0 0\");\n } else if n < 5 {\n println!(\"-1\");\n } else {\n let mut t: i64 = 0;\n let mut f: i64 = (n / 5) - 1;\n let mut s: i64 = 0;\n match n % 5 {\n 0 => f += 1,\n 1 => t += 2,\n 2 => s += 1,\n 3 => {\n f += 1;\n t += 1\n }\n 4 => t += 3,\n _ => t += 0,\n }\n println!(\"{} {} {}\", t, f, s);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1394", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands.\nFor convenience, we will call them Island 1, Island 2, ..., Island N.

\n

There are M kinds of regular boat services between these islands.\nEach service connects two islands. The i-th service connects Island a_i and Island b_i.

\n

Cat Snuke is on Island 1 now, and wants to go to Island N.\nHowever, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.

\n

Help him.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 ≤ N ≤ 200 000
  • \n
  • 1 ≤ M ≤ 200 000
  • \n
  • 1 ≤ a_i < b_i ≤ N
  • \n
  • (a_i, b_i) \\neq (1, N)
  • \n
  • If i \\neq j, (a_i, b_i) \\neq (a_j, b_j).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\na_1 b_1\na_2 b_2\n:\na_M b_M\n
\n
\n
\n
\n
\n

Output

If it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n1 2\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

POSSIBLE\n
\n
\n
\n
\n
\n
\n

Sample Input 2

4 3\n1 2\n2 3\n3 4\n
\n
\n
\n
\n
\n

Sample Output 2

IMPOSSIBLE\n
\n

You have to use three boat services to get to Island 4.

\n
\n
\n
\n
\n
\n

Sample Input 3

100000 1\n1 99999\n
\n
\n
\n
\n
\n

Sample Output 3

IMPOSSIBLE\n
\n
\n
\n
\n
\n
\n

Sample Input 4

5 5\n1 3\n4 5\n2 3\n2 4\n1 4\n
\n
\n
\n
\n
\n

Sample Output 4

POSSIBLE\n
\n

You can get to Island 5 by using two boat services: Island 1 -> Island 4 -> Island 5.

\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n int a[m];\n int b[m];\n int f[200001] = {0};\n int s[200001] = {0};\n for (int i = 0; i < m; i++) {\n scanf(\"%d%d\", &a[i], &b[i]);\n if (a[i] == 1) {\n f[b[i]] = 1;\n }\n if (b[i] == n) {\n s[a[i]] = 1;\n }\n }\n for (int i = 1; i <= n; i++) {\n if (f[i] == 1 && s[i] == 1) {\n printf(\"POSSIBLE\");\n return 0;\n }\n }\n printf(\"IMPOSSIBLE\");\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 v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let mut x: Vec = Vec::new();\n let mut y: HashSet = HashSet::new();\n for _ in 0..v[1] {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let u: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n if u[0] == 1 {\n x.push(u[1]);\n }\n if u[1] == v[0] {\n y.insert(u[0]);\n }\n }\n for i in &x {\n if y.contains(i) {\n println!(\"POSSIBLE\");\n return;\n }\n }\n println!(\"IMPOSSIBLE\");\n}", "difficulty": "medium"} {"problem_id": "1395", "problem_description": "You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads! Initially Zmei Gorynich has $$$x$$$ heads. You can deal $$$n$$$ types of blows. If you deal a blow of the $$$i$$$-th type, you decrease the number of Gorynich's heads by $$$min(d_i, curX)$$$, there $$$curX$$$ is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows $$$h_i$$$ new heads. If $$$curX = 0$$$ then Gorynich is defeated. You can deal each blow any number of times, in any order.For example, if $$$curX = 10$$$, $$$d = 7$$$, $$$h = 10$$$ then the number of heads changes to $$$13$$$ (you cut $$$7$$$ heads off, but then Zmei grows $$$10$$$ new ones), but if $$$curX = 10$$$, $$$d = 11$$$, $$$h = 100$$$ then number of heads changes to $$$0$$$ and Zmei Gorynich is considered defeated.Calculate the minimum number of blows to defeat Zmei Gorynich!You have to answer $$$t$$$ independent queries.", "c_code": "int solution() {\n int t;\n int n;\n int x;\n int d;\n int h;\n scanf(\"%d\", &t);\n while (t--) {\n int D = 0;\n int max = 0;\n scanf(\"%d%d\", &n, &x);\n for (int i = 0; i < n; i++) {\n scanf(\"%d%d\", &d, &h);\n if (d > max) {\n max = d;\n }\n if (d - h > D) {\n D = d - h;\n }\n }\n if (max >= x) {\n printf(\"1\\n\");\n } else if (max < x && D <= 0) {\n printf(\"-1\\n\");\n } else {\n int ans;\n ans = (x + D - max - 1) / D + 1;\n printf(\"%d\\n\", ans);\n }\n }\n return 0;\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\n let t = get!();\n for _ in 0..t {\n let n = get!();\n let x = get!();\n let mut bs = vec![];\n let mut max_d = 0;\n for _ in 0..n {\n let d = get!();\n let h = get!();\n if d > h {\n bs.push((d, h));\n }\n max_d = std::cmp::max(max_d, d);\n }\n if max_d >= x {\n println!(\"1\");\n continue;\n }\n if bs.is_empty() {\n println!(\"-1\");\n continue;\n }\n let mut ans = std::i64::MAX;\n for &(d, h) in bs.iter() {\n let mut c = (max_d - x) / (h - d);\n while x - d * c + h * c > max_d {\n c += 1;\n }\n ans = std::cmp::min(ans, c + 1);\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "1396", "problem_description": "Igor the analyst has adopted n little bunnies. As we all know, bunnies love carrots. Thus, Igor has bought a carrot to be shared between his bunnies. Igor wants to treat all the bunnies equally, and thus he wants to cut the carrot into n pieces of equal area. Formally, the carrot can be viewed as an isosceles triangle with base length equal to 1 and height equal to h. Igor wants to make n - 1 cuts parallel to the base to cut the carrot into n pieces. He wants to make sure that all n pieces have the same area. Can you help Igor determine where to cut the carrot so that each piece have equal area? Illustration to the first example.", "c_code": "int solution() {\n double n;\n double h;\n scanf(\"%lf%lf\", &n, &h);\n for (int i = 1; i < n; i++) {\n printf(\"%.12lf\\n\", sqrt(i / n) * h);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (n, h): (f64, f64) = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let mut it = input.split_whitespace().map(|k| k.parse().unwrap());\n (it.next().unwrap(), it.next().unwrap())\n };\n\n for k in 1..n as usize {\n let ans = h * (k as f64 / n).sqrt();\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "1397", "problem_description": "The string $$$t_1t_2 \\dots t_k$$$ is good if each letter of this string belongs to at least one palindrome of length greater than 1.A palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not.Here are some examples of good strings: $$$t$$$ = AABBB (letters $$$t_1$$$, $$$t_2$$$ belong to palindrome $$$t_1 \\dots t_2$$$ and letters $$$t_3$$$, $$$t_4$$$, $$$t_5$$$ belong to palindrome $$$t_3 \\dots t_5$$$); $$$t$$$ = ABAA (letters $$$t_1$$$, $$$t_2$$$, $$$t_3$$$ belong to palindrome $$$t_1 \\dots t_3$$$ and letter $$$t_4$$$ belongs to palindrome $$$t_3 \\dots t_4$$$); $$$t$$$ = AAAAA (all letters belong to palindrome $$$t_1 \\dots t_5$$$); You are given a string $$$s$$$ of length $$$n$$$, consisting of only letters A and B.You have to calculate the number of good substrings of string $$$s$$$.", "c_code": "int solution() {\n int n = 0;\n int i = 0;\n long long int ans = 0;\n char current = 0;\n char s[524228];\n char stroka_answer[32];\n int m[524228];\n int M = 0;\n scanf(\"%d\", &n);\n scanf(\"%s\", s);\n for (i = 0; i < n; i++) {\n if (s[i] != current) {\n current = s[i];\n M++;\n m[M - 1] = 1;\n } else {\n m[M - 1]++;\n }\n }\n if (M == 1) {\n ans = ((long long int)n) * ((long long int)(n - 1)) / 2;\n } else {\n ans = m[0] + m[M - 1] - M + 1;\n for (i = 1; i < M - 1; i++) {\n ans += 2 * m[i];\n }\n ans = ((long long int)n) * ((long long int)(n - 1)) / 2 - ans;\n }\n stroka_answer[31] = 0;\n i = 30;\n while (ans != 0) {\n stroka_answer[i] = (ans % 10) + '0';\n ans /= 10;\n i--;\n }\n printf(\"%s\\n\", stroka_answer + i + 1);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin\n .lock()\n .lines()\n .map(|l| l.expect(\"stdin line exhausted\"));\n\n let n: usize = lines.next().and_then(|s| s.parse().ok()).expect(\"read n\");\n let mut s: Vec<_> = lines.next().expect(\"read s\").bytes().collect();\n assert_eq!(s.len(), n);\n\n let mut res = {\n let t = n as i64;\n t * (t - 1) / 2\n };\n for x in 0..2 {\n let mut cur = 1;\n for i in 1..n {\n if s[i] == s[i - 1] {\n cur += 1;\n } else {\n res -= cur - x;\n cur = 1;\n }\n }\n s.reverse();\n }\n\n println!(\"{}\", res);\n}", "difficulty": "hard"} {"problem_id": "1398", "problem_description": "\n

Score: 400 points

\n
\n
\n

Problem Statement

\n

Takahashi became a pastry chef and opened a shop La Confiserie d'ABC to celebrate AtCoder Beginner Contest 100.

\n

The shop sells N kinds of cakes.
\nEach kind of cake has three parameters \"beauty\", \"tastiness\" and \"popularity\". The i-th kind of cake has the beauty of x_i, the tastiness of y_i and the popularity of z_i.
\nThese values may be zero or negative.

\n

Ringo has decided to have M pieces of cakes here. He will choose the set of cakes as follows:

\n
    \n
  • Do not have two or more pieces of the same kind of cake.
  • \n
  • Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity).
  • \n
\n

Find the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • N is an integer between 1 and 1 \\ 000 (inclusive).
  • \n
  • M is an integer between 0 and N (inclusive).
  • \n
  • x_i, y_i, z_i \\ (1 \\leq i \\leq N) are integers between -10 \\ 000 \\ 000 \\ 000 and 10 \\ 000 \\ 000 \\ 000 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N M\nx_1 y_1 z_1\nx_2 y_2 z_2\n :  :\nx_N y_N z_N\n
\n
\n
\n
\n
\n

Output

\n

Print the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3\n3 1 4\n1 5 9\n2 6 5\n3 5 8\n9 7 9\n
\n
\n
\n
\n
\n

Sample Output 1

56\n
\n

Consider having the 2-nd, 4-th and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

\n
    \n
  • Beauty: 1 + 3 + 9 = 13
  • \n
  • Tastiness: 5 + 5 + 7 = 17
  • \n
  • Popularity: 9 + 8 + 9 = 26
  • \n
\n

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 13 + 17 + 26 = 56. This is the maximum value.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 3\n1 -2 3\n-4 5 -6\n7 -8 -9\n-10 11 -12\n13 -14 15\n
\n
\n
\n
\n
\n

Sample Output 2

54\n
\n

Consider having the 1-st, 3-rd and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

\n
    \n
  • Beauty: 1 + 7 + 13 = 21
  • \n
  • Tastiness: (-2) + (-8) + (-14) = -24
  • \n
  • Popularity: 3 + (-9) + 15 = 9
  • \n
\n

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 21 + 24 + 9 = 54. This is the maximum value.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 5\n10 -80 21\n23 8 38\n-94 28 11\n-26 -2 18\n-69 72 79\n-26 -86 -54\n-72 -50 59\n21 65 -32\n40 -94 87\n-62 18 82\n
\n
\n
\n
\n
\n

Sample Output 3

638\n
\n

If we have the 3-rd, 4-th, 5-th, 7-th and 10-th kinds of cakes, the total beauty, tastiness and popularity will be -323, 66 and 249, respectively.
\nThe value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 323 + 66 + 249 = 638. This is the maximum value.

\n
\n
\n
\n
\n
\n

Sample Input 4

3 2\n2000000000 -9000000000 4000000000\n7000000000 -5000000000 3000000000\n6000000000 -1000000000 8000000000\n
\n
\n
\n
\n
\n

Sample Output 4

30000000000\n
\n

The values of the beauty, tastiness and popularity of the cakes and the value to be printed may not fit into 32-bit integers.

\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n long x;\n long y;\n long z;\n long a[8][n];\n long sum[8];\n long ans = 0;\n for (int i = 0; i < 8; i++) {\n sum[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%ld%ld%ld\", &x, &y, &z);\n a[0][i] = x + y + z;\n a[1][i] = x + y - z;\n a[2][i] = x - y + z;\n a[3][i] = x - y - z;\n a[4][i] = -x + y + z;\n a[5][i] = -x + y - z;\n a[6][i] = -x - y + z;\n a[7][i] = -x - y - z;\n }\n for (int i = 0; i < 8; i++) {\n for (int j = 0; j < n - 1; j++) {\n for (int k = j + 1; k < n; k++) {\n if (a[i][j] < a[i][k]) {\n long tmp = a[i][j];\n a[i][j] = a[i][k];\n a[i][k] = tmp;\n }\n }\n }\n }\n for (int i = 0; i < 8; i++) {\n for (int j = 0; j < m; j++) {\n sum[i] += a[i][j];\n }\n }\n for (int i = 0; i < 8; i++) {\n ans = (ans < sum[i]) ? sum[i] : ans;\n }\n printf(\"%ld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut count_line = String::new();\n std::io::stdin().read_line(&mut count_line).ok();\n let mut count_it = count_line.split_whitespace();\n let line_count: u32 = count_it.next().unwrap().parse().unwrap();\n let amount: u32 = count_it.next().unwrap().parse().unwrap();\n let values: Vec<(i64, i64, i64)> = (0..line_count)\n .map(|_| {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let mut it = s.split_whitespace();\n (\n it.next().unwrap().parse().unwrap(),\n it.next().unwrap().parse().unwrap(),\n it.next().unwrap().parse().unwrap(),\n )\n })\n .collect();\n let mut ppp: BinaryHeap = values.iter().map(|v| v.0 + v.1 + v.2).collect();\n let mut ppp_val = 0;\n for _ in 0..amount {\n ppp_val += ppp.pop().unwrap();\n }\n let mut ppm: BinaryHeap = values.iter().map(|v| v.0 + v.1 - v.2).collect();\n let mut ppm_val = 0;\n for _ in 0..amount {\n ppm_val += ppm.pop().unwrap();\n }\n let mut pmp: BinaryHeap = values.iter().map(|v| v.0 - v.1 + v.2).collect();\n let mut pmp_val = 0;\n for _ in 0..amount {\n pmp_val += pmp.pop().unwrap();\n }\n let mut pmm: BinaryHeap = values.iter().map(|v| v.0 - v.1 - v.2).collect();\n let mut pmm_val = 0;\n for _ in 0..amount {\n pmm_val += pmm.pop().unwrap();\n }\n let mut mpp: BinaryHeap = values.iter().map(|v| -v.0 + v.1 + v.2).collect();\n let mut mpp_val = 0;\n for _ in 0..amount {\n mpp_val += mpp.pop().unwrap();\n }\n let mut mpm: BinaryHeap = values.iter().map(|v| -v.0 + v.1 - v.2).collect();\n let mut mpm_val = 0;\n for _ in 0..amount {\n mpm_val += mpm.pop().unwrap();\n }\n let mut mmp: BinaryHeap = values.iter().map(|v| -v.0 - v.1 + v.2).collect();\n let mut mmp_val = 0;\n for _ in 0..amount {\n mmp_val += mmp.pop().unwrap();\n }\n let mut mmm: BinaryHeap = values.iter().map(|v| -v.0 - v.1 - v.2).collect();\n let mut mmm_val = 0;\n for _ in 0..amount {\n mmm_val += mmm.pop().unwrap();\n }\n println!(\n \"{}\",\n [ppp_val, ppm_val, pmp_val, pmm_val, mpp_val, mpm_val, mmp_val, mmm_val]\n .iter()\n .fold(0, |acc, x| if acc >= *x { acc } else { *x })\n );\n}", "difficulty": "hard"} {"problem_id": "1399", "problem_description": "You are given three integers $$$a \\le b \\le c$$$.In one move, you can add $$$+1$$$ or $$$-1$$$ to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this operation several times with one number. Note that you cannot make non-positive numbers using such operations.You have to perform the minimum number of such operations in order to obtain three integers $$$A \\le B \\le C$$$ such that $$$B$$$ is divisible by $$$A$$$ and $$$C$$$ is divisible by $$$B$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n while (n--) {\n int res = 90009;\n int a;\n int b;\n int c;\n int aa;\n int bb;\n int cc;\n scanf(\"%d %d %d\", &a, &b, &c);\n for (int i = 1; i <= 10100; i++) {\n for (int j = 1; i * j <= 10100; j++) {\n for (int k = 1; i * j * k <= 10100; k++) {\n int d = abs(a - i) + abs(b - (i * j)) + abs(c - (i * j * k));\n if (d < res) {\n res = d;\n aa = i;\n bb = j * i;\n cc = i * j * k;\n }\n }\n }\n }\n printf(\"%d\\n%d %d %d\\n\", res, aa, bb, cc);\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut lines = stdin.lock().lines().map(|l| l.unwrap());\n let t: u64 = lines.next().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let line = lines.next().unwrap();\n let mut parts = line.split_whitespace();\n let a: i64 = parts.next().unwrap().parse().unwrap();\n let b: i64 = parts.next().unwrap().parse().unwrap();\n let c: i64 = parts.next().unwrap().parse().unwrap();\n\n let mut besta = -1;\n let mut bestb = -1;\n let mut bestc = -1;\n let mut bestcost = 10000000;\n\n let limit = 40000;\n for ac in 1..limit {\n let mut bcs = Vec::new();\n let mut bc = ac;\n while bc <= limit {\n bcs.push(bc);\n bc += ac;\n }\n\n for bc in bcs {\n let mut cc1 = c - (c % bc);\n let cc2 = cc1 + bc;\n\n if cc1 == 0 {\n cc1 = cc2;\n }\n\n let cost1 = (ac - a).abs() + (bc - b).abs() + (cc1 - c).abs();\n let cost2 = (ac - a).abs() + (bc - b).abs() + (cc2 - c).abs();\n\n if cost1 < bestcost {\n besta = ac;\n bestb = bc;\n bestc = cc1;\n bestcost = cost1;\n }\n\n if cost2 < bestcost {\n besta = ac;\n bestb = bc;\n bestc = cc2;\n bestcost = cost2;\n }\n }\n }\n\n println!(\"{}\", bestcost);\n println!(\"{} {} {}\", besta, bestb, bestc);\n }\n}", "difficulty": "medium"} {"problem_id": "1400", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions:

\n
    \n
  • 1 \\leq x,y,z
  • \n
  • x^2 + y^2 + z^2 + xy + yz + zx = n
  • \n
\n

Given an integer N, find each of f(1),f(2),f(3),\\ldots,f(N).

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^4
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print N lines. The i-th line should contain the value f(i).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

20\n
\n
\n
\n
\n
\n

Sample Output 1

0\n0\n0\n0\n0\n1\n0\n0\n0\n0\n3\n0\n0\n0\n0\n0\n3\n3\n0\n0\n
\n
    \n
  • For n=6, only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1.
  • \n
  • For n=11, three triples, (1,1,2), (1,2,1), and (2,1,1), satisfy both of the conditions. Thus, f(6) = 3.
  • \n
  • For n=17, three triples, (1,2,2), (2,1,2), and (2,2,1), satisfy both of the conditions. Thus, f(17) = 3.
  • \n
  • For n=18, three triples, (1,1,3), (1,3,1), and (3,1,1), satisfy both of the conditions. Thus, f(18) = 3.
  • \n
\n
\n
", "c_code": "int solution(void) {\n int datanum = 0;\n int x = 0;\n int y = 0;\n int z = 0;\n int n = 0;\n int i = 0;\n int count = 0;\n\n scanf(\"%d\", &datanum);\n\n for (i = 1; i <= datanum; i++) {\n count = 0;\n for (x = 1; x < 99; x++) {\n for (y = 1; y < 99; y++) {\n for (z = 1; z < 99; z++) {\n n = (x * x) + (y * y) + (z * z) + (x * y) + (y * z) + (z * x);\n if (n > i) {\n break;\n }\n if (n == i) {\n count++;\n }\n }\n }\n }\n printf(\"%d\\n\", count);\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 let n: usize = itr.next().unwrap().parse().unwrap();\n\n let mut memo = vec![0; 10010];\n let mut x = 1;\n while x * x <= n {\n let mut y = 1;\n while y * y <= n {\n let mut z = 1;\n while z * z <= n {\n let val = x * x + y * y + z * z + x * y + y * z + z * x;\n if val <= n {\n memo[val] += 1;\n }\n z += 1;\n }\n y += 1;\n }\n x += 1;\n }\n let mut out = Vec::new();\n\n for i in 1..=n {\n writeln!(out, \"{}\", memo[i]).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "easy"} {"problem_id": "1401", "problem_description": "Denis was very sad after Nastya rejected him. So he decided to walk through the gateways to have some fun. And luck smiled at him! When he entered the first courtyard, he met a strange man who was selling something. Denis bought a mysterious item and it was... Random permutation generator! Denis could not believed his luck.When he arrived home, he began to study how his generator works and learned the algorithm. The process of generating a permutation consists of $$$n$$$ steps. At the $$$i$$$-th step, a place is chosen for the number $$$i$$$ $$$(1 \\leq i \\leq n)$$$. The position for the number $$$i$$$ is defined as follows: For all $$$j$$$ from $$$1$$$ to $$$n$$$, we calculate $$$r_j$$$  — the minimum index such that $$$j \\leq r_j \\leq n$$$, and the position $$$r_j$$$ is not yet occupied in the permutation. If there are no such positions, then we assume that the value of $$$r_j$$$ is not defined. For all $$$t$$$ from $$$1$$$ to $$$n$$$, we calculate $$$count_t$$$  — the number of positions $$$1 \\leq j \\leq n$$$ such that $$$r_j$$$ is defined and $$$r_j = t$$$. Consider the positions that are still not occupied by permutation and among those we consider the positions for which the value in the $$$count$$$ array is maximum. The generator selects one of these positions for the number $$$i$$$. The generator can choose any position. Let's have a look at the operation of the algorithm in the following example: Let $$$n = 5$$$ and the algorithm has already arranged the numbers $$$1, 2, 3$$$ in the permutation. Consider how the generator will choose a position for the number $$$4$$$: The values of $$$r$$$ will be $$$r = [3, 3, 3, 4, \\times]$$$, where $$$\\times$$$ means an indefinite value. Then the $$$count$$$ values will be $$$count = [0, 0, 3, 1, 0]$$$. There are only two unoccupied positions in the permutation: $$$3$$$ and $$$4$$$. The value in the $$$count$$$ array for position $$$3$$$ is $$$3$$$, for position $$$4$$$ it is $$$1$$$. The maximum value is reached only for position $$$3$$$, so the algorithm will uniquely select this position for number $$$4$$$. Satisfied with his purchase, Denis went home. For several days without a break, he generated permutations. He believes that he can come up with random permutations no worse than a generator. After that, he wrote out the first permutation that came to mind $$$p_1, p_2, \\ldots, p_n$$$ and decided to find out if it could be obtained as a result of the generator.Unfortunately, this task was too difficult for him, and he asked you for help. It is necessary to define whether the written permutation could be obtained using the described algorithm if the generator always selects the position Denis needs.", "c_code": "int solution() {\n long long int t;\n scanf(\"%lld\", &t);\n\n do {\n long long int n;\n scanf(\"%lld\", &n);\n\n long long int a[n];\n long long int i;\n for (i = 0; i < n; ++i) {\n scanf(\"%lld\", &a[i]);\n }\n\n long long int sol = 0;\n for (i = 0; i < n - 1; ++i) {\n if (a[i] + 1 < a[i + 1]) {\n sol = 1;\n }\n }\n\n if (sol == 0) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n\n --t;\n } while (t != 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 n: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let mut xs: Vec<(usize, usize)> = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .enumerate()\n .collect();\n xs.sort_by(|a, b| a.1.cmp(&b.1));\n let mut lo = xs[0].0;\n let mut hi = n;\n let mut beg = 0;\n let mut ok = true;\n for i in 0..n {\n if i >= beg + hi - lo {\n hi = lo;\n lo = xs[i].0;\n beg = i;\n }\n if xs[i].0 != lo + i - beg {\n ok = false;\n break;\n }\n }\n println!(\"{}\", if ok { \"YES\" } else { \"NO\" });\n }\n}", "difficulty": "medium"} {"problem_id": "1402", "problem_description": "You have a sequence $$$a_1, a_2, \\ldots, a_n$$$ of length $$$n$$$, consisting of integers between $$$1$$$ and $$$m$$$. You also have a string $$$s$$$, consisting of $$$m$$$ characters B.You are going to perform the following $$$n$$$ operations. At the $$$i$$$-th ($$$1 \\le i \\le n$$$) operation, you replace either the $$$a_i$$$-th or the $$$(m + 1 - a_i)$$$-th character of $$$s$$$ with A. You can replace the character at any position multiple times through the operations. Find the lexicographically smallest string you can get after these operations.A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ of the same length if and only if in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n\n while (--t >= 0) {\n int n = 0;\n int m = 0;\n scanf(\"%d\", &n);\n scanf(\"%d\", &m);\n\n char *carr = (char *)malloc(sizeof(char) * (m + 1));\n\n for (int i = 0; i < m; ++i) {\n carr[i] = 'B';\n }\n carr[m] = '\\0';\n\n for (int i = 0; i < n; ++i) {\n int pos = 1;\n scanf(\"%d\", &pos);\n\n int rpos = 0;\n int lpos = 0;\n\n if (m + 1 < pos + pos) {\n lpos = m - pos;\n rpos = pos - 1;\n } else {\n lpos = pos - 1;\n rpos = m - pos;\n }\n\n if (carr[lpos] == 'A') {\n carr[rpos] = 'A';\n } else {\n carr[lpos] = 'A';\n }\n }\n\n printf(\"%s\\n\", carr);\n\n free(carr);\n }\n}", "rust_code": "fn solution() {\n let n = std::io::stdin()\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n for _ in 0..n {\n let mut k = String::new();\n let mut l = String::new();\n std::io::stdin().read_line(&mut k);\n std::io::stdin().read_line(&mut l);\n let k = k\n .trim()\n .split(\" \")\n .flat_map(str::parse::)\n .collect::>();\n let l = l\n .trim()\n .split(\" \")\n .flat_map(str::parse::)\n .collect::>();\n let mut poss = Vec::new();\n for jm in l.iter() {\n if *jm > k[1] + 1 - jm {\n poss.push(k[1] + 1 - jm);\n poss.push(*jm);\n continue;\n }\n poss.push(*jm);\n poss.push(k[1] + 1 - jm);\n }\n\n let _cnt = 0;\n let mut cnp = Vec::new();\n for (p1, p2) in poss.iter().enumerate() {\n if p1 % 2 != 0 {\n continue;\n }\n if cnp.contains(p2) && cnp.contains(&poss[p1 + 1]) {\n continue;\n }\n if cnp.contains(p2) {\n cnp.push(poss[p1 + 1]);\n continue;\n } else {\n cnp.push(*p2);\n continue;\n }\n }\n cnp.sort();\n for jk in 0..k[1] {\n if cnp.contains(&(jk + 1)) {\n print!(\"A\");\n } else {\n print!(\"B\")\n }\n }\n println!();\n }\n}", "difficulty": "hard"} {"problem_id": "1403", "problem_description": "Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited n his friends there.If there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from 1 to n. Petya remembered that a friend number i gave a gift to a friend number pi. He also remembered that each of his friends received exactly one gift.Now Petya wants to know for each friend i the number of a friend who has given him a gift.", "c_code": "int solution() {\n int n = 0;\n int i = 0;\n int ara1[101] = {0};\n int ara2[101] = {0};\n\n scanf(\"%d\", &n);\n\n for (i = 1; i <= n; i++) {\n scanf(\"%d\", &ara1[i]);\n ara2[ara1[i]] = i;\n }\n\n for (i = 1; i <= n; i++) {\n printf(\"%d \", ara2[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\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 let friends: Vec = buf\n .split_whitespace()\n .map(|str| str.trim().parse().unwrap())\n .collect();\n let mut gave = vec![0; friends.len()];\n for (i, gave_to) in friends.into_iter().enumerate() {\n let gave_to = gave_to as usize;\n gave[gave_to - 1] = i;\n }\n for (i, from) in gave.into_iter().enumerate() {\n if i != 0 {\n print!(\" \");\n }\n print!(\"{}\", from + 1);\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1404", "problem_description": "You are given two integer arrays $$$a$$$ and $$$b$$$ of length $$$n$$$.You can reverse at most one subarray (continuous subsegment) of the array $$$a$$$. Your task is to reverse such a subarray that the sum $$$\\sum\\limits_{i=1}^n a_i \\cdot b_i$$$ is maximized.", "c_code": "int solution() {\n long long int n;\n long long int i;\n long long int a[5003];\n long long int b[5003];\n long long int d;\n long long int j;\n long long int mult1[5005];\n long long int mult2[5005];\n long long int sum1;\n long long int sum2;\n long long int sum;\n long long int x;\n long long int y;\n scanf(\"%lld\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &b[i]);\n }\n sum1 = 0;\n sum2 = 0;\n for (j = 0; j < n; j++) {\n sum1 = sum1 + a[j] * b[j];\n mult1[j] = sum1;\n sum2 = sum2 + a[n - 1 - j] * b[n - 1 - j];\n mult2[j] = sum2;\n }\n d = 0;\n for (i = 0; i < n; i++) {\n x = i;\n y = i;\n sum = 0;\n while (x >= 0 && y < n) {\n if (x == y) {\n sum = sum + a[x] * b[x];\n } else {\n sum = sum + a[x] * b[y] + a[y] * b[x];\n }\n x--;\n y++;\n sum1 = 0;\n if (x >= 0) {\n sum1 = sum1 + mult1[x];\n }\n if (y < n) {\n sum1 = sum1 + mult2[n - y - 1];\n }\n if (d < (sum + sum1)) {\n d = sum + sum1;\n }\n }\n }\n for (i = 0; i < n - 1; i++) {\n x = i;\n y = i + 1;\n sum = 0;\n while (x >= 0 && y < n) {\n sum = sum + a[x] * b[y] + a[y] * b[x];\n x--;\n y++;\n sum1 = 0;\n if (x >= 0) {\n sum1 = sum1 + mult1[x];\n }\n if (y < n) {\n sum1 = sum1 + mult2[n - y - 1];\n }\n if (d < (sum + sum1)) {\n d = sum + sum1;\n }\n }\n }\n printf(\"%lld\\n\", d);\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let n: usize = str.trim().parse().unwrap();\n let mut A: Vec = vec![0; n + 1];\n let mut B: Vec = vec![0; n + 1];\n let mut A_dot_B: Vec> = vec![Vec::new(); n];\n let mut A_not_B: Vec = vec![0; n];\n let mut a_str = String::new();\n let mut b_str = String::new();\n let _ = stdin().read_line(&mut a_str).unwrap();\n let _ = stdin().read_line(&mut b_str).unwrap();\n let mut a_iter = a_str.split_whitespace();\n let mut b_iter = b_str.split_whitespace();\n for i in 0..n {\n let a: i64 = a_iter.next().unwrap().parse().unwrap();\n let b: i64 = b_iter.next().unwrap().parse().unwrap();\n A[i] = a;\n B[i] = b;\n A_not_B[i] = a * b;\n }\n for i in 2..n + 1 {\n A_not_B[n - i] += A_not_B[n - i + 1];\n }\n\n A_dot_B[0].push(A[0] * B[0]);\n let mut max_sum = 0;\n for i in 1..n {\n for j in 0..i + 1 {\n let new_dot = if i == j {\n A_dot_B[i - 1][i - 1] + B[i] * A[i]\n } else if j + 1 < i {\n A_dot_B[i - 1][j + 1] - B[j] * A[j] + B[j] * A[i] + B[i] * A[j]\n } else {\n A_dot_B[i - 1][j] - B[j] * A[j] + B[j] * A[i] + B[i] * A[j]\n };\n if i < n - 1 && max_sum < new_dot + A_not_B[i + 1] {\n max_sum = new_dot + A_not_B[i + 1];\n }\n A_dot_B[i].push(new_dot);\n }\n }\n for i in 0..n {\n if A_dot_B[n - 1][i] > max_sum {\n max_sum = A_dot_B[n - 1][i];\n }\n }\n println!(\"{}\", max_sum);\n}", "difficulty": "medium"} {"problem_id": "1405", "problem_description": "You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\\operatorname{AB}(s) = \\operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \\le i \\le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \\dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char str[100];\n scanf(\"%s\", str);\n int i = 0;\n while (str[i] != '\\0') {\n i++;\n }\n if (str[0] == 'a' && str[i - 1] == 'b') {\n str[i - 1] = 'a';\n } else if (str[0] == 'b' && str[i - 1] == 'a') {\n str[i - 1] = 'b';\n }\n printf(\"%s\\n\", str);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut n = String::new();\n\n stdin.read_line(&mut n).unwrap();\n let n = n.trim().parse::().unwrap();\n\n for _ in 0..n {\n let mut s = String::new();\n stdin.read_line(&mut s).unwrap();\n let mut s = s.trim().to_string();\n\n let ab = s.matches(\"ab\").count();\n let ba = s.matches(\"ba\").count();\n\n if ab > ba {\n let pos = s.rfind(\"b\").unwrap();\n s.replace_range(pos..pos + 1, \"a\");\n } else if ab < ba {\n let pos = s.rfind(\"a\").unwrap();\n s.replace_range(pos..pos + 1, \"b\");\n }\n\n println!(\"{s}\");\n }\n}", "difficulty": "medium"} {"problem_id": "1406", "problem_description": "You are given two integers $$$n$$$ and $$$m$$$. Calculate the number of pairs of arrays $$$(a, b)$$$ such that: the length of both arrays is equal to $$$m$$$; each element of each array is an integer between $$$1$$$ and $$$n$$$ (inclusive); $$$a_i \\le b_i$$$ for any index $$$i$$$ from $$$1$$$ to $$$m$$$; array $$$a$$$ is sorted in non-descending order; array $$$b$$$ is sorted in non-ascending order. As the result can be very large, you should print it modulo $$$10^9+7$$$.", "c_code": "int solution() {\n long long int n;\n long long int m;\n scanf(\"%lld%lld\", &n, &m);\n m = 2 * m;\n long long int a[21][1001] = {0};\n for (int i = 1; i <= n; i++) {\n a[1][i] = i;\n }\n for (int i = 1; i <= m; i++) {\n a[i][1] = 1;\n }\n for (long long int i = 2; i <= n; i++) {\n for (long long int o = 2; o <= m; o++) {\n a[o][i] = (a[o - 1][i] % (1000000007) + a[o][i - 1] % (1000000007)) %\n (1000000007);\n }\n }\n printf(\"%lld\\n\", a[m][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\n .split_whitespace()\n .map(|x| x.parse::().expect(\"convert to number\"))\n .collect::>();\n\n let n = arr[0];\n let m = arr[1];\n\n const M: u64 = 1_000_000_000 + 7;\n\n let mut a = vec![1u64; n];\n let mut b = vec![1u64; n];\n\n for _ in 1..m {\n for j in 0..n {\n a[j] = (a[j] + if j > 0 { a[j - 1] } else { 0 }) % M;\n }\n }\n\n for _ in 1..m {\n for j in (0..n).rev() {\n b[j] = (b[j] + if j < n - 1 { b[j + 1] } else { 0 }) % M;\n }\n }\n\n let mut ans = 0;\n let mut accum = 0;\n for i in (0..n).rev() {\n accum = (accum + b[i]) % M;\n ans = (((a[i] * accum) % M) + ans) % M;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1407", "problem_description": "Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: $$$f(0) = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) \\oplus f(n-2)$$$ when $$$n > 1$$$, where $$$\\oplus$$$ denotes the bitwise XOR operation. You are given three integers $$$a$$$, $$$b$$$, and $$$n$$$, calculate $$$f(n)$$$.You have to answer for $$$T$$$ independent test cases.", "c_code": "int solution() {\n int qtos;\n int a;\n int b;\n int n;\n scanf(\"%d\", &qtos);\n if (qtos >= 1 && qtos <= 1000) {\n for (int i = 0; i < qtos; i++) {\n scanf(\"%d %d %d\", &a, &b, &n);\n if (a >= 0 && b >= 0 && n >= 0) {\n if (n % 3 == 0) {\n printf(\"%d\\n\", a);\n } else if (n % 3 == 1) {\n printf(\"%d\\n\", b);\n } else {\n printf(\"%d\\n\", a ^ b);\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin()\n .read_line(&mut t)\n .expect(\"failed to read from stdin\");\n let t: u32 = t.trim().parse().expect(\"expect a number\");\n for _ in 0..t {\n let mut line = String::new();\n io::stdin()\n .read_line(&mut line)\n .expect(\"failed to read from stdin\");\n let mut iter = line.split_whitespace();\n let a: u32 = iter\n .next()\n .expect(\"expect a number\")\n .trim()\n .parse()\n .unwrap();\n let b: u32 = iter\n .next()\n .expect(\"expect a number\")\n .trim()\n .parse()\n .unwrap();\n let n: u32 = iter\n .next()\n .expect(\"expect a number\")\n .trim()\n .parse()\n .unwrap();\n let c: u32 = a ^ b;\n let res = match n % 3 {\n 0 => a,\n 1 => b,\n 2 => c,\n _ => unreachable!(),\n };\n println!(\"{}\", res);\n }\n}", "difficulty": "medium"} {"problem_id": "1408", "problem_description": "You took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems: the Power Gem of purple color, the Time Gem of green color, the Space Gem of blue color, the Soul Gem of orange color, the Reality Gem of red color, the Mind Gem of yellow color. Using colors of Gems you saw in the Gauntlet determine the names of absent Gems.", "c_code": "int solution() {\n char save[6][20] = {\"Power\", \"Time\", \"Space\", \"Soul\", \"Reality\", \"Mind\"};\n char s[20];\n int n;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", s);\n if (*s == 'p') {\n memset(save[0], 0, sizeof(save[0]));\n }\n if (*s == 'g') {\n memset(save[1], 0, sizeof(save[0]));\n }\n if (*s == 'b') {\n memset(save[2], 0, sizeof(save[0]));\n }\n if (*s == 'o') {\n memset(save[3], 0, sizeof(save[0]));\n }\n if (*s == 'r') {\n memset(save[4], 0, sizeof(save[0]));\n }\n if (*s == 'y') {\n memset(save[5], 0, sizeof(save[0]));\n }\n }\n printf(\"%d\\n\", 6 - n);\n for (int i = 0; i < 6; i++) {\n if (*save[i] == 0) {\n continue;\n }\n printf(\"%s\\n\", save[i]);\n }\n}", "rust_code": "fn solution() {\n let mut visited = HashMap::new();\n visited.insert(\"purple\", (\"Power\", false));\n visited.insert(\"green\", (\"Time\", false));\n visited.insert(\"blue\", (\"Space\", false));\n visited.insert(\"orange\", (\"Soul\", false));\n visited.insert(\"red\", (\"Reality\", false));\n visited.insert(\"yellow\", (\"Mind\", false));\n\n let mut mut_line = String::new();\n io::stdin().read_line(&mut mut_line).unwrap();\n let n: u8 = mut_line.trim().parse().unwrap();\n\n println!(\"{}\", 6 - n);\n for _ in 0..n {\n mut_line = String::new();\n io::stdin().read_line(&mut mut_line).unwrap();\n if let Some(tuple) = visited.get_mut(mut_line.trim()) {\n tuple.1 = true;\n }\n }\n\n for (_color, (name, v)) in visited {\n if !v {\n println!(\"{}\", name);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1409", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We have A apples and P pieces of apple.

\n

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

\n

Find the maximum number of apple pies we can make with what we have now.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 0 \\leq A, P \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A P\n
\n
\n
\n
\n
\n

Output

Print the maximum number of apple pies we can make with what we have.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 3\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

\n
\n
\n
\n
\n
\n

Sample Input 2

0 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

We cannot make an apple pie in this case, unfortunately.

\n
\n
\n
\n
\n
\n

Sample Input 3

32 21\n
\n
\n
\n
\n
\n

Sample Output 3

58\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int p = 0;\n int ans = 0;\n\n scanf(\"%d%d\", &a, &p);\n\n ans = (a * 3 + p) / 2;\n\n printf(\"%d\\n\", ans);\n}", "rust_code": "fn solution() {\n let mut ap = String::new();\n stdin().read_line(&mut ap).unwrap();\n let a: usize = ap.split_whitespace().collect::>()[0]\n .parse()\n .unwrap();\n let p: usize = ap.split_whitespace().collect::>()[1]\n .parse()\n .unwrap();\n let k = a * 3 + p;\n println!(\"{}\", k / 2);\n}", "difficulty": "easy"} {"problem_id": "1410", "problem_description": "Let's say string $$$s$$$ has period $$$k$$$ if $$$s_i = s_{i + k}$$$ for all $$$i$$$ from $$$1$$$ to $$$|s| - k$$$ ($$$|s|$$$ means length of string $$$s$$$) and $$$k$$$ is the minimum positive integer with this property.Some examples of a period: for $$$s$$$=\"0101\" the period is $$$k=2$$$, for $$$s$$$=\"0000\" the period is $$$k=1$$$, for $$$s$$$=\"010\" the period is $$$k=2$$$, for $$$s$$$=\"0011\" the period is $$$k=4$$$.You are given string $$$t$$$ consisting only of 0's and 1's and you need to find such string $$$s$$$ that: String $$$s$$$ consists only of 0's and 1's; The length of $$$s$$$ doesn't exceed $$$2 \\cdot |t|$$$; String $$$t$$$ is a subsequence of string $$$s$$$; String $$$s$$$ has smallest possible period among all strings that meet conditions 1—3. Let us recall that $$$t$$$ is a subsequence of $$$s$$$ if $$$t$$$ can be derived from $$$s$$$ by deleting zero or more elements (any) without changing the order of the remaining elements. For example, $$$t$$$=\"011\" is a subsequence of $$$s$$$=\"10101\".", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char ch[100];\n scanf(\"%s\", ch);\n int l = strlen(ch);\n int cnt1 = 0;\n int cnt0 = 0;\n int k = l;\n while (l) {\n l--;\n\n if (ch[l] == '1') {\n cnt1++;\n } else {\n cnt0++;\n }\n }\n\n if (cnt1 == 0 || cnt0 == 0) {\n printf(\"%s\", ch);\n } else {\n for (int i = 0; i < k; i++) {\n printf(\"10\");\n }\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let test: i64 = str.trim().parse().unwrap();\n for _ in 0..test {\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let ones = str\n .trim()\n .chars()\n .fold(0, |x, c| if c == '1' { x + 1 } else { x });\n if ones == str.trim().len() || ones == 0 {\n println!(\"{}\", str);\n } else {\n println!(\"{}\", \"01\".repeat(str.trim().len()));\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1411", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K.

\n

She is shopping, and now paying at the cashier.\nHer total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change).

\n

However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.

\n

Find the amount of money that she will hand to the cashier.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ N < 10000
  • \n
  • 1 ≦ K < 10
  • \n
  • 0 ≦ D_1 < D_2 < … < D_K≦9
  • \n
  • \\{D_1,D_2,...,D_K\\} ≠ \\{1,2,3,4,5,6,7,8,9\\}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N K\nD_1 D_2D_K\n
\n
\n
\n
\n
\n

Output

Print the amount of money that Iroha will hand to the cashier.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1000 8\n1 3 4 5 6 7 8 9\n
\n
\n
\n
\n
\n

Sample Output 1

2000\n
\n

She dislikes all digits except 0 and 2.

\n

The smallest integer equal to or greater than N=1000 whose decimal notation contains only 0 and 2, is 2000.

\n
\n
\n
\n
\n
\n

Sample Input 2

9999 1\n0\n
\n
\n
\n
\n
\n

Sample Output 2

9999\n
\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n int i;\n int a;\n int b[20] = {0};\n scanf(\"%d %d\", &n, &m);\n for (i = 0; i < m; i++) {\n scanf(\"%d\", &a);\n b[a] = 1;\n }\n while (1) {\n for (i = n++; i && b[i % 10] == 0; i /= 10) {\n ;\n }\n if (i == 0) {\n break;\n }\n }\n printf(\"%d\\n\", n - 1);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n k: usize,\n v: [usize; k]\n }\n let mut l = [false; 10];\n for i in 0..k {\n l[v[i]] = true;\n }\n 'outer: for i in n..n * 10 {\n let mut m = i;\n while m > 0 {\n if l[m % 10] {\n continue 'outer;\n }\n m /= 10;\n }\n println!(\"{}\", i);\n return;\n }\n}", "difficulty": "medium"} {"problem_id": "1412", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have a permutation p = {p_1,\\ p_2,\\ ...,\\ p_n} of {1,\\ 2,\\ ...,\\ n}.

\n

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

\n
    \n
  • p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 3 \\leq n \\leq 20
  • \n
  • p is a permutation of {1,\\ 2,\\ ...,\\ n}.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n\np_1 p_2 ... p_n\n
\n
\n
\n
\n
\n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1 3 5 4 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 2

9\n9 6 3 2 5 8 7 4 1\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
\n
", "c_code": "int solution() {\n\n int n;\n scanf(\"%d\", &n);\n\n int p[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &p[i]);\n }\n\n int cnt = 0;\n\n for (int i = 1; i < n - 1; i++) {\n\n if ((p[i - 1] <= p[i] && p[i] < p[i + 1]) ||\n (p[i + 1] <= p[i] && p[i] < p[i - 1]) ||\n (p[i] < p[i - 1] && p[i - 1] == p[i + 1])) {\n cnt += 1;\n }\n }\n\n printf(\"%d\", cnt);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n\n let n: usize = iter.next().unwrap().parse().unwrap();\n\n let items: Vec = (0..n)\n .map(|_| iter.next().unwrap().parse().unwrap())\n .collect();\n\n let zero = 0;\n let ordinality: Vec = (2..n)\n .map(|i| (items[i - 2] - items[i - 1]) * (items[i] - items[i - 1]))\n .filter(|v| v.lt(&zero))\n .collect();\n\n println!(\"{}\", ordinality.len());\n}", "difficulty": "hard"} {"problem_id": "1413", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

In the State of Takahashi in AtCoderian Federation, there are N cities, numbered 1, 2, ..., N.\nM bidirectional roads connect these cities.\nThe i-th road connects City A_i and City B_i.\nEvery road connects two distinct cities.\nAlso, for any two cities, there is at most one road that directly connects them.

\n

One day, it was decided that the State of Takahashi would be divided into two states, Taka and Hashi.\nAfter the division, each city in Takahashi would belong to either Taka or Hashi.\nIt is acceptable for all the cities to belong Taka, or for all the cities to belong Hashi.\nHere, the following condition should be satisfied:

\n
    \n
  • Any two cities in the same state, Taka or Hashi, are directly connected by a road.
  • \n
\n

Find the minimum possible number of roads whose endpoint cities belong to the same state.\nIf it is impossible to divide the cities into Taka and Hashi so that the condition is satisfied, print -1.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 700
  • \n
  • 0 \\leq M \\leq N(N-1)/2
  • \n
  • 1 \\leq A_i \\leq N
  • \n
  • 1 \\leq B_i \\leq N
  • \n
  • A_i \\neq B_i
  • \n
  • If i \\neq j, at least one of the following holds: A_i \\neq A_j and B_i \\neq B_j.
  • \n
  • If i \\neq j, at least one of the following holds: A_i \\neq B_j and B_i \\neq A_j.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_1 B_1\nA_2 B_2\n:\nA_M B_M\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 5\n1 2\n1 3\n3 4\n3 5\n4 5\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

For example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong to Hashi, the condition is satisfied.\nHere, the number of roads whose endpoint cities belong to the same state, is 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 1\n1 2\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

In this sample, the condition cannot be satisfied regardless of which cities belong to each state.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 3\n1 2\n1 3\n2 3\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
\n
\n
\n
\n

Sample Input 4

10 39\n7 2\n7 1\n5 6\n5 8\n9 10\n2 8\n8 7\n3 10\n10 1\n8 10\n2 3\n7 4\n3 9\n4 10\n3 4\n6 1\n6 7\n9 5\n9 7\n6 9\n9 4\n4 6\n7 5\n8 3\n2 5\n9 2\n10 7\n8 6\n8 9\n7 3\n5 3\n4 5\n6 3\n2 10\n5 10\n4 2\n6 2\n8 4\n10 6\n
\n
\n
\n
\n
\n

Sample Output 4

21\n
\n
\n
", "c_code": "int solution() {\n int i;\n int u;\n int w;\n int N;\n int M;\n int adj[701][701] = {};\n scanf(\"%d %d\", &N, &M);\n for (i = 1; i <= M; i++) {\n scanf(\"%d %d\", &u, &w);\n adj[u][w] = 1;\n adj[w][u] = 1;\n }\n\n int k;\n int flag[701] = {};\n int size[701];\n int q[701];\n int head;\n int tail;\n for (i = 1, k = 0; i <= N; i++) {\n if (flag[i] != 0) {\n continue;\n }\n size[++k] = 1;\n flag[i] = 1;\n q[0] = i;\n for (head = 0, tail = 1; head < tail; head++) {\n for (u = q[head], w = 1; w <= N; w++) {\n if (w != u && adj[u][w] == 0) {\n if (flag[w] == 0) {\n flag[w] = -flag[u];\n size[k] += flag[w];\n q[tail++] = w;\n } else if (flag[w] == flag[u]) {\n break;\n }\n }\n }\n if (w <= N) {\n break;\n }\n }\n if (head < tail) {\n break;\n }\n size[k] = abs(size[k]);\n }\n if (i <= N) {\n printf(\"-1\\n\");\n fflush(stdout);\n return 0;\n }\n\n int j;\n int dp[701][1401] = {};\n for (i = 1, dp[0][N] = 1; i <= k; i++) {\n for (j = size[i]; j <= N * 2 - size[i]; j++) {\n dp[i][j + size[i]] |= dp[i - 1][j];\n dp[i][j - size[i]] |= dp[i - 1][j];\n }\n }\n\n for (i = N; i >= 0; i--) {\n if (dp[k][i] == 1) {\n break;\n }\n }\n for (j = N; j <= N * 2; j++) {\n if (dp[k][j] == 1) {\n break;\n }\n }\n j = (N - i > j - N) ? j - N : N - i;\n printf(\"%d\\n\", ((N + j) * (N + j - 2) + (N - j) * (N - j - 2)) / 8);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n m: usize,\n es: [(usize1, usize1); m]\n }\n let mut g = vec![(0..n).collect::>(); n];\n for i in 0..n {\n g[i].remove(&i);\n }\n for &(a, b) in &es {\n g[a].remove(&b);\n g[b].remove(&a);\n }\n let g = g\n .iter()\n .map(|s| s.iter().cloned().collect::>())\n .collect::>();\n let mut done = vec![None; n];\n let mut ds = vec![];\n for i in 0..n {\n if done[i].is_some() {\n continue;\n }\n let mut ct: i64 = 1;\n let mut cf: i64 = 0;\n done[i] = Some(true);\n let mut st = vec![i];\n while let Some(v) = st.pop() {\n let f = !done[v].unwrap();\n for &c in &g[v] {\n if let Some(fc) = done[c] {\n if fc ^ f {\n println!(\"-1\");\n return;\n }\n continue;\n }\n if f {\n ct += 1;\n } else {\n cf += 1;\n }\n done[c] = Some(f);\n st.push(c);\n }\n }\n ds.push((ct - cf).unsigned_abs() as usize);\n }\n let mut dp = vec![false; n + 1];\n dp[ds[0]] = true;\n for i in 1..ds.len() {\n let mut next = vec![false; n + 1];\n for j in 0..n + 1 - ds[i] {\n next[j + ds[i]] |= dp[j];\n }\n for j in 0..n + 1 {\n next[(j as i64 - ds[i] as i64).unsigned_abs() as usize] |= dp[j];\n }\n dp = next;\n }\n for i in 0..n + 1 {\n if dp[i] {\n println!(\n \"{}\",\n ((n + i) / 2) * ((n + i) / 2 - 1) / 2 + ((n - i) / 2) * ((n - i) / 2 - 1) / 2\n );\n return;\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1414", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have N integers. The i-th integer is A_i.

\n

Find \\sum_{i=1}^{N-1}\\sum_{j=i+1}^{N} (A_i \\mbox{ XOR } A_j), modulo (10^9+7).

\n
\nWhat is \\mbox{ XOR }?\n

\nThe XOR of integers A and B, A \\mbox{ XOR } B, is defined as follows:\n

    \n
  • When A \\mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.
  • \n
\nFor example, 3 \\mbox{ XOR } 5 = 6. (In base two: 011 \\mbox{ XOR } 101 = 110.)\n

\n
\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 3 \\times 10^5
  • \n
  • 0 \\leq A_i < 2^{60}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the value \\sum_{i=1}^{N-1}\\sum_{j=i+1}^{N} (A_i \\mbox{ XOR } A_j), modulo (10^9+7).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

We have (1\\mbox{ XOR } 2)+(1\\mbox{ XOR } 3)+(2\\mbox{ XOR } 3)=3+2+1=6.

\n
\n
\n
\n
\n
\n

Sample Input 2

10\n3 1 4 1 5 9 2 6 5 3\n
\n
\n
\n
\n
\n

Sample Output 2

237\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10\n3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820\n
\n
\n
\n
\n
\n

Sample Output 3

103715602\n
\n

Print the sum modulo (10^9+7).

\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int i;\n int j;\n long long int a[300005];\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n int b[300005][62];\n for (i = 0; i < n; i++) {\n for (j = 0; j < 62; j++) {\n b[i][j] = (int)(a[i] % 2);\n a[i] /= 2;\n }\n }\n long long int ans = 0;\n long long int c0;\n long long int c1;\n long long int q = 1;\n long long int p = 1000000007;\n for (j = 0; j < 62; j++) {\n c0 = c1 = 0;\n for (i = 0; i < n; i++) {\n if (b[i][j] == 0) {\n c0++;\n } else {\n c1++;\n }\n }\n ans = (ans + q * c0 % p * c1 % p) % p;\n q = q * 2 % p;\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n std::io::stdin().read_to_string(&mut input_str).unwrap();\n\n let input_parts = input_str.split_whitespace().collect::>();\n let mut input_parts_it = input_parts.iter().cloned();\n let mut next = || input_parts_it.next().unwrap();\n\n let n: usize = next().parse().unwrap();\n let a: Vec = (0..n).map(|_| next().parse().unwrap()).collect();\n\n let mut a_sum = vec![0; a.len() + 1];\n\n let sum = (0..60u32)\n .map(|bit| {\n for i in 1..a.len() + 1 {\n a_sum[i] = a_sum[i - 1] + ((a[i - 1] >> bit) & 1);\n }\n\n let sum: u64 = (0..a.len() - 1)\n .map(|i| {\n let j = i + 1..a.len();\n let set_count = a_sum[j.end] - a_sum[j.start];\n if (a[i] >> bit) & 1 != 0 {\n (j.end - j.start) as u64 - set_count\n } else {\n set_count\n }\n })\n .sum();\n\n (0..bit).fold(sum, |x, _| (x * 2) % 1000000007)\n })\n .fold(0, |x, y| (x + y) % 1000000007);\n\n println!(\"{}\", sum);\n}", "difficulty": "hard"} {"problem_id": "1415", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

\n

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq r \\leq 100
  • \n
  • r is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
r\n
\n
\n
\n
\n
\n

Output

Print an integer representing the area of the regular dodecagon.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n
\n
\n
\n
\n
\n

Sample Output 1

48\n
\n

The area of the regular dodecagon is 3 \\times 4^2 = 48.

\n
\n
\n
\n
\n
\n

Sample Input 2

15\n
\n
\n
\n
\n
\n

Sample Output 2

675\n
\n
\n
\n
\n
\n
\n

Sample Input 3

80\n
\n
\n
\n
\n
\n

Sample Output 3

19200\n
\n
\n
", "c_code": "int solution(void) {\n\n int r = 0;\n scanf(\"%d\", &r);\n\n printf(\"%d\", 3 * r * r);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let r = buf.trim().parse::().ok().unwrap();\n println!(\"{}\", 3 * r * r);\n}", "difficulty": "easy"} {"problem_id": "1416", "problem_description": "Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire. Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.", "c_code": "int solution(void) {\n int n = 0;\n int m = 0;\n int x = 0;\n int y = 0;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n scanf(\"%d\", &m);\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &x);\n scanf(\"%d\", &y);\n if (x - 1 != 0) {\n arr[x - 2] = y - 1 + arr[x - 2];\n }\n if (x != n) {\n arr[x] = arr[x - 1] - y + arr[x];\n }\n arr[x - 1] = 0;\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d\\n\", arr[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\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 let mut wires: Vec = buf\n .split(\" \")\n .map(|str| str.trim().parse().unwrap())\n .collect();\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n let m = buf.trim().parse::().unwrap();\n for _ in 0..m {\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n let mut ns = buf.split(\" \").map(|str| str.trim().parse::().unwrap());\n let x = ns.next().unwrap();\n let y = ns.next().unwrap();\n if x != 1 {\n wires[x as usize - 2] += y - 1;\n }\n if x != wires.len() as u32 {\n wires[x as usize] += wires[x as usize - 1] - y;\n }\n wires[x as usize - 1] = 0;\n }\n for wire in wires {\n println!(\"{}\", wire);\n }\n}", "difficulty": "medium"} {"problem_id": "1417", "problem_description": "Vasya owns a cornfield which can be defined with two integers $$$n$$$ and $$$d$$$. The cornfield can be represented as rectangle with vertices having Cartesian coordinates $$$(0, d), (d, 0), (n, n - d)$$$ and $$$(n - d, n)$$$. An example of a cornfield with $$$n = 7$$$ and $$$d = 2$$$. Vasya also knows that there are $$$m$$$ grasshoppers near the field (maybe even inside it). The $$$i$$$-th grasshopper is at the point $$$(x_i, y_i)$$$. Vasya does not like when grasshoppers eat his corn, so for each grasshopper he wants to know whether its position is inside the cornfield (including the border) or outside.Help Vasya! For each grasshopper determine if it is inside the field (including the border).", "c_code": "int solution(void) {\n\n int n = 0;\n int d = 0;\n int c = 0;\n double dlA = 0.0;\n double dlB = 0.0;\n double S = 0.0;\n double S1 = 0.0;\n double S2 = 0.0;\n double S3 = 0.0;\n double S4 = 0.0;\n scanf(\"%d %d\", &n, &d);\n scanf(\"%d\", &c);\n\n int x[c];\n int y[c];\n int yG[4];\n int xG[4];\n for (int j = 0; j < c; ++j) {\n scanf(\"%d %d\", &x[j], &y[j]);\n }\n\n xG[0] = 0;\n yG[0] = d;\n xG[1] = d;\n yG[1] = 0;\n xG[2] = n;\n yG[2] = n - d;\n xG[3] = n - d;\n yG[3] = n;\n\n dlA = sqrt((pow(n - d, 2) + pow(n - d, 2)));\n\n dlB = sqrt(pow(-d, 2) + pow(d, 2));\n\n S = dlA * dlB;\n\n for (int j = 0; j < c; j++) {\n\n S1 = fabs(0.5 * ((xG[0] - x[j]) * (yG[1] - y[j]) -\n (xG[1] - x[j]) * (yG[0] - y[j])));\n S2 = fabs(0.5 * ((xG[1] - x[j]) * (yG[2] - y[j]) -\n (xG[2] - x[j]) * (yG[1] - y[j])));\n S3 = fabs(0.5 * ((xG[2] - x[j]) * (yG[3] - y[j]) -\n (xG[3] - x[j]) * (yG[2] - y[j])));\n S4 = fabs(0.5 * ((xG[3] - x[j]) * (yG[0] - y[j]) -\n (xG[0] - x[j]) * (yG[3] - y[j])));\n\n if (S1 + S2 + S3 + S4 - 0.00001 < S) {\n\n printf(\"Yes\\n\");\n\n } else {\n\n printf(\"No\\n\");\n }\n }\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).unwrap();\n\n let arr = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n let n = arr[0];\n let d = arr[1];\n let m = arr[2] as usize;\n for i in 0..m {\n let x = arr[3 + i * 2];\n let y = arr[3 + i * 2 + 1];\n\n let y1 = x + d;\n let y2 = x - d;\n let y3 = -x + d;\n let y4 = -x + 2 * n - d;\n\n if y <= y1 && y >= y2 && y >= y3 && y <= y4 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1418", "problem_description": "Baby Ehab is known for his love for a certain operation. He has an array $$$a$$$ of length $$$n$$$, and he decided to keep doing the following operation on it: he picks $$$2$$$ adjacent elements; he then removes them and places a single integer in their place: their bitwise XOR. Note that the length of the array decreases by one. Now he asks you if he can make all elements of the array equal. Since babies like to make your life harder, he requires that you leave at least $$$2$$$ elements remaining.", "c_code": "int solution() {\n int a[2010];\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n\n int temp = 0;\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n temp ^= a[i];\n }\n if (temp == 0) {\n printf(\"YES\\n\");\n continue;\n }\n int cnt = 0;\n int tt = 0;\n for (int i = 1; i <= n; i++) {\n tt ^= a[i];\n if (tt == temp) {\n cnt++;\n tt = 0;\n }\n }\n if (cnt != 1 && cnt % 2 == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let t: usize = scan!(usize);\n for _ in 1..=t {\n let n = scan!(usize);\n let a = scan!([u32; n]);\n\n let tot_xor = a.iter().fold(0, |x, a| x ^ *a);\n let mut cur = 0;\n let mut flag = tot_xor == 0;\n 'done: for i in 0..n {\n cur ^= a[i];\n\n let mut lxor = 0;\n for j in i + 1..n {\n lxor ^= a[j];\n if cur == lxor ^ tot_xor ^ cur && cur == lxor {\n flag = true;\n break 'done;\n }\n }\n }\n if flag {\n print!(\"YES\");\n } else {\n print!(\"NO\");\n }\n print!(\"\\n\");\n }\n}", "difficulty": "easy"} {"problem_id": "1419", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have N points in a two-dimensional plane.
\nThe coordinates of the i-th point (1 \\leq i \\leq N) are (x_i,y_i).
\nLet us consider a rectangle whose sides are parallel to the coordinate axes that contains K or more of the N points in its interior.
\nHere, points on the sides of the rectangle are considered to be in the interior.
\nFind the minimum possible area of such a rectangle.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq K \\leq N \\leq 50
  • \n
  • -10^9 \\leq x_i,y_i \\leq 10^9 (1 \\leq i \\leq N)
  • \n
  • x_i≠x_j (1 \\leq i<j \\leq N)
  • \n
  • y_i≠y_j (1 \\leq i<j \\leq N)
  • \n
  • All input values are integers. (Added at 21:50 JST)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K  \nx_1 y_1\n:  \nx_{N} y_{N}\n
\n
\n
\n
\n
\n

Output

Print the minimum possible area of a rectangle that satisfies the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 4\n1 4\n3 3\n6 2\n8 1\n
\n
\n
\n
\n
\n

Sample Output 1

21\n
\n

One rectangle that satisfies the condition with the minimum possible area has the following vertices: (1,1), (8,1), (1,4) and (8,4).
\nIts area is (8-1) × (4-1) = 21.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 2\n0 0\n1 1\n2 2\n3 3\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

4 3\n-1000000000 -1000000000\n1000000000 1000000000\n-999999999 999999999\n999999999 -999999999\n
\n
\n
\n
\n
\n

Sample Output 3

3999999996000000001\n
\n

Watch out for integer overflows.

\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int K;\n long long x[51];\n long long y[51];\n scanf(\"%d %d\", &N, &K);\n for (i = 1; i <= N; i++) {\n scanf(\"%lld %lld\", &(x[i]), &(y[i]));\n }\n\n int j;\n int k;\n int l;\n int m;\n int count;\n long long X[2];\n long long Y[2];\n long long min = (long long)1 << 62;\n for (i = 1; i < N; i++) {\n for (j = i + 1; j <= N; j++) {\n X[0] = (x[i] < x[j]) ? x[i] : x[j];\n X[1] = x[i] + x[j] - X[0];\n for (k = 1; k < N; k++) {\n for (l = k + 1; l <= N; l++) {\n Y[0] = (y[k] < y[l]) ? y[k] : y[l];\n Y[1] = y[k] + y[l] - Y[0];\n for (m = 1, count = 0; m <= N; m++) {\n if (x[m] >= X[0] && x[m] <= X[1] && y[m] >= Y[0] && y[m] <= Y[1]) {\n count++;\n }\n }\n if (count >= K && (X[1] - X[0]) * (Y[1] - Y[0]) < min) {\n min = (X[1] - X[0]) * (Y[1] - Y[0]);\n }\n }\n }\n }\n }\n\n printf(\"%lld\\n\", min);\n fflush(stdout);\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 K: usize = itr.next().unwrap().parse().unwrap();\n let xy: Vec<(i64, i64)> = (0..n)\n .map(|_| {\n (\n itr.next().unwrap().parse().unwrap(),\n itr.next().unwrap().parse().unwrap(),\n )\n })\n .collect();\n\n let mut ans = 1i64 << 62;\n for i in 0..n {\n for j in 0..n {\n for k in 0..n {\n for l in 0..n {\n let R = max(xy[i].0, max(xy[j].0, max(xy[k].0, xy[l].0)));\n let L = min(xy[i].0, min(xy[j].0, min(xy[k].0, xy[l].0)));\n let U = max(xy[i].1, max(xy[j].1, max(xy[k].1, xy[l].1)));\n let D = min(xy[i].1, min(xy[j].1, min(xy[k].1, xy[l].1)));\n let mut cnt = 0;\n\n for a in 0..n {\n if L <= xy[a].0 && xy[a].0 <= R && D <= xy[a].1 && xy[a].1 <= U {\n cnt += 1;\n }\n }\n if cnt >= K {\n ans = min(ans, (R - L) * (U - D));\n }\n }\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1420", "problem_description": "Little Vasya loves orange juice very much. That's why any food and drink in his kitchen necessarily contains orange juice. There are n drinks in his fridge, the volume fraction of orange juice in the i-th drink equals pi percent.One day Vasya decided to make himself an orange cocktail. He took equal proportions of each of the n drinks and mixed them. Then he wondered, how much orange juice the cocktail has.Find the volume fraction of orange juice in the final drink.", "c_code": "int solution() {\n float n;\n scanf(\"%f\", &n);\n float a[100];\n float sum = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%f\", &a[i]);\n sum = sum + a[i];\n }\n float orange = (sum / n) * 1.0;\n printf(\"%f\", orange);\n return 0;\n}", "rust_code": "fn solution() {\n let n: f32 = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim().parse().unwrap()\n };\n\n let mut input = String::new();\n std::io::stdin().read_line(&mut input).unwrap();\n println!(\n \"{}\",\n input\n .split_whitespace()\n .map(|elem| elem.parse::().unwrap())\n .sum::()\n / n\n )\n}", "difficulty": "medium"} {"problem_id": "1421", "problem_description": "ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has n rows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?", "c_code": "int solution() {\n\n int n;\n int flat = 0;\n scanf(\"%d\", &n);\n char a[100000];\n for (int i = 0; i < n * 6; i++) {\n scanf(\"%c\", &a[i]);\n }\n for (int i = 1; i < n * 6; i++) {\n if (a[i] == 'O' && a[i - 1] == 'O') {\n flat = 1;\n a[i] = '+';\n a[i - 1] = '+';\n break;\n }\n }\n if (flat == 1) {\n printf(\"YES%s\", a);\n } else {\n printf(\"NO\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let n: usize = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim().parse().unwrap()\n };\n let mut bus = Vec::with_capacity(n);\n let mut found = false;\n\n for _ in 0..n {\n let mut row = String::new();\n std::io::stdin().read_line(&mut row).unwrap();\n if !found {\n let seats: Vec<&str> = row.trim().split('|').collect();\n if seats[0] == \"OO\" {\n bus.push(format!(\"++|{}\", seats[1]));\n found = true;\n } else if seats[1] == \"OO\" {\n bus.push(format!(\"{}|++\", seats[0]));\n found = true;\n } else {\n bus.push(row);\n }\n } else {\n bus.push(row);\n }\n }\n\n if found {\n println!(\"YES\");\n for row in bus {\n println!(\"{}\", row);\n }\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "1422", "problem_description": "You are given $$$n$$$ points with integer coordinates on a coordinate axis $$$OX$$$. The coordinate of the $$$i$$$-th point is $$$x_i$$$. All points' coordinates are distinct and given in strictly increasing order.For each point $$$i$$$, you can do the following operation no more than once: take this point and move it by $$$1$$$ to the left or to the right (i..e., you can change its coordinate $$$x_i$$$ to $$$x_i - 1$$$ or to $$$x_i + 1$$$). In other words, for each point, you choose (separately) its new coordinate. For the $$$i$$$-th point, it can be either $$$x_i - 1$$$, $$$x_i$$$ or $$$x_i + 1$$$.Your task is to determine if you can move some points as described above in such a way that the new set of points forms a consecutive segment of integers, i. e. for some integer $$$l$$$ the coordinates of points should be equal to $$$l, l + 1, \\ldots, l + n - 1$$$.Note that the resulting points should have distinct coordinates.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n;\n int pass = 0;\n int f = 1;\n scanf(\"%d\", &n);\n\n int v[n];\n\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", (v + i));\n }\n\n for (int i = 1; i < n; ++i) {\n if (v[i] - 1 == v[i - 1]) {\n continue;\n }\n if (v[i] - 3 == v[i - 1] && !pass) {\n v[i]--;\n pass = 1;\n }\n\n else if (v[i] - 2 == v[i - 1] && !pass)\n pass = 1;\n\n else if (v[i] - 2 == v[i - 1] && pass)\n v[i]--;\n\n else {\n f = 0;\n break;\n }\n }\n\n printf(\"%s\\n\", (f ? \"YES\" : \"NO\"));\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut buf = String::new();\n stdin.read_line(&mut buf).unwrap();\n let t: usize = buf.trim().parse().unwrap();\n\n for _ in 0..t {\n buf.clear();\n stdin.read_line(&mut buf).unwrap();\n buf.clear();\n stdin.read_line(&mut buf).unwrap();\n let a: Vec = buf.trim().split(\" \").map(|x| x.parse().unwrap()).collect();\n\n let n = a.len();\n let x: i32 = a[n - 1] - a[0] - n as i32;\n\n if x <= 1 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1423", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are S sheep and W wolves.

\n

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

\n

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq S \\leq 100
  • \n
  • 1 \\leq W \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S W\n
\n
\n
\n
\n
\n

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 5\n
\n
\n
\n
\n
\n

Sample Output 1

unsafe\n
\n

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

\n
\n
\n
\n
\n
\n

Sample Input 2

100 2\n
\n
\n
\n
\n
\n

Sample Output 2

safe\n
\n

Many a sheep drive away two wolves.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 10\n
\n
\n
\n
\n
\n

Sample Output 3

unsafe\n
\n
\n
", "c_code": "int solution() {\n int s = 0;\n int w = 0;\n scanf(\"%d %d\", &s, &w);\n\n if (s > w) {\n printf(\"safe\\n\");\n } else {\n printf(\"unsafe\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let s: Vec = s\n .trim()\n .split(' ')\n .map(|x| x.parse::().unwrap())\n .collect();\n\n if s[0] > s[1] {\n println!(\"safe\");\n } else {\n println!(\"unsafe\");\n }\n}", "difficulty": "easy"} {"problem_id": "1424", "problem_description": "Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between $$$n$$$ boxes, arranged in a row. The $$$i$$$-th box contains $$$a_i$$$ magazines. Some of the boxes are covered with lids, others are not. Suddenly it started to rain, and now Monocarp has to save as many magazines from the rain as possible. To do this, he can move the lids between boxes as follows: if the $$$i$$$-th box was covered with a lid initially, he can either move the lid from the $$$i$$$-th box to the box $$$(i-1)$$$ (if it exists), or keep the lid on the $$$i$$$-th box. You may assume that Monocarp can move the lids instantly at the same moment, and no lid can be moved more than once. If a box will be covered with a lid after Monocarp moves the lids, the magazines in it will be safe from the rain; otherwise they will soak.You have to calculate the maximum number of magazines Monocarp can save from the rain.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int sum = 0;\n scanf(\"%d\", &n);\n char s[n + 1];\n scanf(\"%s\", s);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%i\", &arr[i]);\n if (s[i] == '1') {\n sum += arr[i];\n }\n }\n int var = -1;\n int small = -1;\n for (int i = 0; i < n; i++) {\n if (s[i] == '0' && s[i + 1] == '1') {\n var = arr[i];\n small = arr[i + 1];\n }\n if (s[i] == '1' && small != -1) {\n if (arr[i] < small) {\n small = arr[i];\n }\n }\n if (i == n - 1 && s[i] == '1') {\n if (var > small && var != -1 && small != -1) {\n sum -= small;\n sum += var;\n }\n }\n if (s[i] == '1' && s[i + 1] == '0' && var != -1) {\n if (var > small && var != -1 && small != -1) {\n sum -= small;\n sum += var;\n }\n }\n }\n\n printf(\"%i\\n\", sum);\n }\n}", "rust_code": "fn solution() {\n use std::io::*;\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n for _ in 0i32..(s.trim().parse().unwrap()) {\n stdin().read_line(&mut \"\".to_owned()).ok();\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let lids: Vec = s.trim().chars().map(|x| x == '1').collect();\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let books: Vec = s\n .split_ascii_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let ans = lids\n .into_iter()\n .zip(books.into_iter())\n .rev()\n .collect::>();\n println!(\n \"{}\",\n ans.split_inclusive(|(i, _)| !*i)\n .map(|v| {\n if v.iter().all(|(i, _)| *i) {\n v.iter().map(|(_, x)| *x).sum::()\n } else {\n v.iter().map(|(_, x)| *x).sum::()\n - v.iter().map(|(_, x)| *x).min().unwrap_or(0)\n }\n })\n .sum::()\n )\n }\n}", "difficulty": "hard"} {"problem_id": "1425", "problem_description": "New Year is coming! Vasya has prepared a New Year's verse and wants to recite it in front of Santa Claus.Vasya's verse contains $$$n$$$ parts. It takes $$$a_i$$$ seconds to recite the $$$i$$$-th part. Vasya can't change the order of parts in the verse: firstly he recites the part which takes $$$a_1$$$ seconds, secondly — the part which takes $$$a_2$$$ seconds, and so on. After reciting the verse, Vasya will get the number of presents equal to the number of parts he fully recited.Vasya can skip at most one part of the verse while reciting it (if he skips more than one part, then Santa will definitely notice it).Santa will listen to Vasya's verse for no more than $$$s$$$ seconds. For example, if $$$s = 10$$$, $$$a = [100, 9, 1, 1]$$$, and Vasya skips the first part of verse, then he gets two presents.Note that it is possible to recite the whole verse (if there is enough time). Determine which part Vasya needs to skip to obtain the maximum possible number of gifts. If Vasya shouldn't skip anything, print 0. If there are multiple answers, print any of them.You have to process $$$t$$$ test cases.", "c_code": "int solution() {\n int32_t t;\n scanf(\"%d\", &t);\n while (t--) {\n int32_t ch = 0;\n int32_t n;\n int32_t s;\n int32_t max = 0;\n int32_t sum = 0;\n int32_t ans = 0;\n scanf(\"%d%d\", &n, &s);\n int32_t num[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &num[i]);\n }\n for (int i = 0; i < n; i++) {\n if (!ch && num[i] > max) {\n max = num[i];\n ans = i + 1;\n }\n if (sum + num[i] <= s) {\n sum += num[i];\n } else {\n if (!ch) {\n ch = 1;\n sum -= max;\n } else {\n break;\n }\n }\n }\n if (ch) {\n printf(\"%d\\n\", ans);\n } else {\n printf(\"0\\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 queries: i8 = line.trim().parse().unwrap();\n while queries > 0 {\n line.clear();\n input.read_line(&mut line).unwrap();\n let mut numbers = line.split_whitespace();\n let blocks: i32 = numbers.next().unwrap().parse().unwrap();\n let max_time: i32 = numbers.next().unwrap().parse().unwrap();\n line.clear();\n input.read_line(&mut line).unwrap();\n let mut numbers = line.split_whitespace();\n let mut max_block: i32 = 0;\n let mut max_index: i32 = 0;\n let mut time: i32 = 0;\n let mut index: i32 = 1;\n loop {\n let block: i32 = numbers.next().unwrap().parse().unwrap();\n if block > max_block {\n max_block = block;\n max_index = index;\n }\n time += block;\n if time > max_time {\n break;\n }\n if index == blocks {\n max_index = 0;\n break;\n }\n index += 1;\n }\n println!(\"{}\", max_index);\n queries -= 1;\n }\n}", "difficulty": "hard"} {"problem_id": "1426", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Snuke stands on a number line. He has L ears, and he will walk along the line continuously under the following conditions:

\n
    \n
  • He never visits a point with coordinate less than 0, or a point with coordinate greater than L.
  • \n
  • He starts walking at a point with integer coordinate, and also finishes walking at a point with integer coordinate.
  • \n
  • He only changes direction at a point with integer coordinate.
  • \n
\n

Each time when Snuke passes a point with coordinate i-0.5, where i is an integer, he put a stone in his i-th ear.

\n

After Snuke finishes walking, Ringo will repeat the following operations in some order so that, for each i, Snuke's i-th ear contains A_i stones:

\n
    \n
  • Put a stone in one of Snuke's ears.
  • \n
  • Remove a stone from one of Snuke's ears.
  • \n
\n

Find the minimum number of operations required when Ringo can freely decide how Snuke walks.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq L \\leq 2\\times 10^5
  • \n
  • 0 \\leq A_i \\leq 10^9(1\\leq i\\leq L)
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
L\nA_1\n:\nA_L\n
\n
\n
\n
\n
\n

Output

Print the minimum number of operations required when Ringo can freely decide how Snuke walks.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n1\n0\n2\n3\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Assume that Snuke walks as follows:

\n
    \n
  • He starts walking at coordinate 3 and finishes walking at coordinate 4, visiting coordinates 3,4,3,2,3,4 in this order.
  • \n
\n

Then, Snuke's four ears will contain 0,0,2,3 stones, respectively.\nRingo can satisfy the requirement by putting one stone in the first ear.

\n
\n
\n
\n
\n
\n

Sample Input 2

8\n2\n0\n0\n2\n1\n3\n4\n1\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

7\n314159265\n358979323\n846264338\n327950288\n419716939\n937510582\n0\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution() {\n int i;\n int L;\n int A[200001];\n scanf(\"%d\", &L);\n for (i = 1; i <= L; i++) {\n scanf(\"%d\", &(A[i]));\n }\n\n int j;\n const long long sup = (long long)1 << 60;\n long long dp[2][5];\n long long tmp[5];\n for (j = 0; j < 5; j++) {\n dp[0][j] = 0;\n }\n for (i = 1; i <= L; i++) {\n for (j = 1; j < 5; j++) {\n if (dp[1 - (i % 2)][j - 1] < dp[1 - (i % 2)][j]) {\n dp[1 - (i % 2)][j] = dp[1 - (i % 2)][j - 1];\n }\n }\n dp[i % 2][0] = dp[1 - (i % 2)][0] + A[i];\n dp[i % 2][1] = dp[1 - (i % 2)][1] + ((A[i] == 0) ? 2 : A[i] % 2);\n dp[i % 2][2] = dp[1 - (i % 2)][2] + 1 - A[i] % 2;\n dp[i % 2][3] = dp[1 - (i % 2)][3] + ((A[i] == 0) ? 2 : A[i] % 2);\n dp[i % 2][4] = dp[1 - (i % 2)][4] + A[i];\n }\n\n long long min = sup;\n for (j = 0; j < 5; j++) {\n if (dp[1 - (i % 2)][j] < min) {\n min = dp[1 - (i % 2)][j];\n }\n }\n printf(\"%lld\\n\", min);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n v: [usize; n]\n }\n let mut dp: Vec<[usize; 4]> = vec![[0, 0, 0, 0]; n + 1];\n let v_o: Vec = v.iter().clone().map(|x| (x + 1) % 2).collect();\n let v_e: Vec = v\n .iter()\n .clone()\n .map(|x| if *x == 0 { 2 } else { x % 2 })\n .collect();\n for i in 0..n {\n dp[i + 1][0] = dp[i][0] + v[i];\n dp[i + 1][1] = min(dp[i][0], dp[i][1]) + v_e[i];\n dp[i + 1][2] = min(min(dp[i][0], dp[i][1]), dp[i][2]) + v_o[i];\n dp[i + 1][3] = min(min(dp[i][0], dp[i][1]), min(dp[i][2], dp[i][3])) + v_e[i];\n }\n let total = dp[n][0];\n println!(\n \"{}\",\n dp.iter()\n .map(|x| min(min(x[0], x[1]), min(x[2], x[3])) + total - x[0])\n .min()\n .unwrap()\n );\n}", "difficulty": "hard"} {"problem_id": "1427", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Snuke is interested in strings that satisfy the following conditions:

\n
    \n
  • The length of the string is at least N.
  • \n
  • The first N characters equal to the string s.
  • \n
  • The last N characters equal to the string t.
  • \n
\n

Find the length of the shortest string that satisfies the conditions.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤100
  • \n
  • The lengths of s and t are both N.
  • \n
  • s and t consist of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\ns\nt\n
\n
\n
\n
\n
\n

Output

Print the length of the shortest string that satisfies the conditions.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\nabc\ncde\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

The shortest string is abcde.

\n
\n
\n
\n
\n
\n

Sample Input 2

1\na\nz\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

The shortest string is az.

\n
\n
\n
\n
\n
\n

Sample Input 3

4\nexpr\nexpr\n
\n
\n
\n
\n
\n

Sample Output 3

4\n
\n

The shortest string is expr.

\n
\n
", "c_code": "int solution() {\n int n;\n char s[101];\n char t[101];\n scanf(\"%d%s%s\", &n, s, t);\n for (int i = 0; i <= n; i++) {\n bool isOK = true;\n for (int j = 0; j < n; j++) {\n if (i + j < n && s[i + j] != t[j]) {\n isOK = false;\n break;\n }\n }\n if (isOK) {\n printf(\"%d\\n\", n + i);\n break;\n }\n }\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 s: String = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().to_string()\n };\n let t: String = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().to_string()\n };\n\n let A = s.len();\n let B = t.len();\n let ans = A + B\n - (1..(std::cmp::min(s.len(), t.len()) + 1))\n .rev()\n .find(|&k| s[A - k..] == t[..k])\n .unwrap_or(0);\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1428", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

There is an infinitely large pond, which we consider as a number line.\nIn this pond, there are N lotuses floating at coordinates 0, 1, 2, ..., N-2 and N-1.\nOn the lotus at coordinate i, an integer s_i is written.

\n

You are standing on the lotus at coordinate 0. You will play a game that proceeds as follows:

\n
    \n
  • 1. Choose positive integers A and B. Your score is initially 0.
  • \n
  • 2. Let x be your current coordinate, and y = x+A. The lotus at coordinate x disappears, and you move to coordinate y.
      \n
    • If y = N-1, the game ends.
    • \n
    • If y \\neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
    • \n
    • If y \\neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
    • \n
    \n
  • \n
  • 3. Let x be your current coordinate, and y = x-B. The lotus at coordinate x disappears, and you move to coordinate y.
      \n
    • If y = N-1, the game ends.
    • \n
    • If y \\neq N-1 and there is a lotus floating at coordinate y, your score increases by s_y.
    • \n
    • If y \\neq N-1 and there is no lotus floating at coordinate y, you drown. Your score decreases by 10^{100} points, and the game ends.
    • \n
    \n
  • \n
  • 4. Go back to step 2.
  • \n
\n

You want to end the game with as high a score as possible.\nWhat is the score obtained by the optimal choice of A and B?

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N \\leq 10^5
  • \n
  • -10^9 \\leq s_i \\leq 10^9
  • \n
  • s_0=s_{N-1}=0
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\ns_0 s_1 ...... s_{N-1}\n
\n
\n
\n
\n
\n

Output

Print the score obtained by the optimal choice of A and B.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n0 2 5 1 0\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

If you choose A = 3 and B = 2, the game proceeds as follows:

\n
    \n
  • Move to coordinate 0 + 3 = 3. Your score increases by s_3 = 1.
  • \n
  • Move to coordinate 3 - 2 = 1. Your score increases by s_1 = 2.
  • \n
  • Move to coordinate 1 + 3 = 4. The game ends with a score of 3.
  • \n
\n

There is no way to end the game with a score of 4 or higher, so the answer is 3. Note that you cannot land the lotus at coordinate 2 without drowning later.

\n
\n
\n
\n
\n
\n

Sample Input 2

6\n0 10 -7 -4 -13 0\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

The optimal strategy here is to land the final lotus immediately by choosing A = 5 (the value of B does not matter).

\n
\n
\n
\n
\n
\n

Sample Input 3

11\n0 -4 0 -99 31 14 -15 -39 43 18 0\n
\n
\n
\n
\n
\n

Sample Output 3

59\n
\n
\n
", "c_code": "int solution() {\n int n;\n int i;\n int j;\n long long d[100010];\n long long s;\n long long m = 0;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &d[i]);\n }\n for (i = 1; i < n; i++) {\n for (j = s = 0; i * j < n; j++) {\n if ((n - 1) % i == 0 && i * j >= n - 1 - i * j) {\n break;\n }\n if (n - i * j - 1 < i) {\n break;\n }\n s += d[i * j] + d[n - (i * j) - 1];\n if (m < s) {\n m = s;\n }\n }\n }\n printf(\"%lld\\n\", m);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n v: [isize; n]\n }\n let mut ans: isize = 0;\n for c in 1..n - 1 {\n let mut s: isize = 0;\n for i in 1..(n - 1) / c {\n s += v[i * c] + v[n - 1 - i * c];\n if (n - 1) % c == 0 && (n - 1) / c <= 2 * i {\n continue;\n }\n ans = max(ans, s);\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1429", "problem_description": "Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.There is a set $$$S$$$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $$$k$$$ and replace each element $$$s$$$ of the set $$$S$$$ with $$$s \\oplus k$$$ ($$$\\oplus$$$ denotes the exclusive or operation). Help him choose such $$$k$$$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $$$\\{1, 2, 3\\}$$$ equals to set $$$\\{2, 1, 3\\}$$$.Formally, find the smallest positive integer $$$k$$$ such that $$$\\{s \\oplus k | s \\in S\\} = S$$$ or report that there is no such number.For example, if $$$S = \\{1, 3, 4\\}$$$ and $$$k = 2$$$, new set will be equal to $$$\\{3, 1, 6\\}$$$. If $$$S = \\{0, 1, 2, 3\\}$$$ and $$$k = 1$$$, after playing set will stay the same.", "c_code": "int solution() {\n long long int t;\n scanf(\"%lld\", &t);\n while (t--) {\n long long int n;\n long long int a[1024];\n scanf(\"%lld\", &n);\n for (long int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n long int x = 1;\n long int y;\n long int q = 1;\n while (x < 1025) {\n long int count = 0;\n for (long int i = 0; i < n; i++) {\n y = a[i] ^ x;\n for (long long int j = 0; j < n; j++) {\n if (y == a[j]) {\n count++;\n j = n;\n }\n }\n }\n if (count == n) {\n q = 1;\n break;\n }\n x++;\n }\n if (x == 1025) {\n printf(\"-1\\n\");\n } else if (q == 1) {\n printf(\"%ld\\n\", x);\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 mut a: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n a.sort();\n let mut good = false;\n for bit in 1..1 << 10 {\n let mut cmp = vec![0; n];\n for i in 0..n {\n cmp[i] = a[i] ^ bit;\n }\n cmp.sort();\n let mut ok = true;\n for i in 0..n {\n if a[i] != cmp[i] {\n ok = false;\n }\n }\n if ok {\n writeln!(out, \"{}\", bit).ok();\n good = true;\n break;\n }\n }\n if !good {\n writeln!(out, \"-1\").ok();\n }\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "medium"} {"problem_id": "1430", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.
\nTwo antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.
\nDetermine if there exists a pair of antennas that cannot communicate directly.
\nHere, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • a, b, c, d, e and k are integers between 0 and 123 (inclusive).
  • \n
  • a < b < c < d < e
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
a\nb\nc\nd\ne\nk\n
\n
\n
\n
\n
\n

Output

\n

Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1\n2\n4\n8\n9\n15\n
\n
\n
\n
\n
\n

Sample Output 1

Yay!\n
\n

In this case, there is no pair of antennas that cannot communicate directly, because:

\n
    \n
  • the distance between A and B is 2 - 1 = 1
  • \n
  • the distance between A and C is 4 - 1 = 3
  • \n
  • the distance between A and D is 8 - 1 = 7
  • \n
  • the distance between A and E is 9 - 1 = 8
  • \n
  • the distance between B and C is 4 - 2 = 2
  • \n
  • the distance between B and D is 8 - 2 = 6
  • \n
  • the distance between B and E is 9 - 2 = 7
  • \n
  • the distance between C and D is 8 - 4 = 4
  • \n
  • the distance between C and E is 9 - 4 = 5
  • \n
  • the distance between D and E is 9 - 8 = 1
  • \n
\n

and none of them is greater than 15. Thus, the correct output is Yay!.

\n
\n
\n
\n
\n
\n

Sample Input 2

15\n18\n26\n35\n36\n18\n
\n
\n
\n
\n
\n

Sample Output 2

:(\n
\n

In this case, the distance between antennas A and D is 35 - 15 = 20 and exceeds 18, so they cannot communicate directly.\nThus, the correct output is :(.

\n
\n
", "c_code": "int solution(void) {\n int a[256];\n scanf(\"%d %d %d %d %d %d\", &a[1], &a[2], &a[3], &a[4], &a[5], &a[0]);\n if (a[0] >= a[5] - a[1]) {\n printf(\"Yay!\");\n } else {\n printf(\":(\");\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let a: usize = buf.trim().parse().unwrap();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let _b: usize = buf.trim().parse().unwrap();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let _c: usize = buf.trim().parse().unwrap();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let _d: usize = buf.trim().parse().unwrap();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let e: usize = buf.trim().parse().unwrap();\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let k: usize = buf.trim().parse().unwrap();\n\n if e - a > k {\n println!(\":(\");\n } else {\n println!(\"Yay!\");\n }\n}", "difficulty": "medium"} {"problem_id": "1431", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

\n

You are given a 4-digit integer N. Answer the question: Is N good?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1000 ≤ N ≤ 9999
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If N is good, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1118\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

N is good, since it contains three consecutive 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

7777\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n

An integer is also good when all the digits are the same.

\n
\n
\n
\n
\n
\n

Sample Input 3

1234\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
", "c_code": "int solution(void) {\n char n[5];\n\n scanf(\"%s\", n);\n\n if ((n[0] == n[1] && n[1] == n[2]) || (n[1] == n[2] && n[2] == n[3])) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let d: Vec = buf.trim().chars().collect();\n if d[0] == d[1] && d[1] == d[2] || d[1] == d[2] && d[2] == d[3] {\n println!(\"Yes\");\n } else {\n println! {\"No\"};\n }\n}", "difficulty": "easy"} {"problem_id": "1432", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ nonnegative integers. Let's define the prefix OR array $$$b$$$ as the array $$$b_i = a_1~\\mathsf{OR}~a_2~\\mathsf{OR}~\\dots~\\mathsf{OR}~a_i$$$, where $$$\\mathsf{OR}$$$ represents the bitwise OR operation. In other words, the array $$$b$$$ is formed by computing the $$$\\mathsf{OR}$$$ of every prefix of $$$a$$$.You are asked to rearrange the elements of the array $$$a$$$ in such a way that its prefix OR array is lexicographically maximum.An array $$$x$$$ is lexicographically greater than an array $$$y$$$ if in the first position where $$$x$$$ and $$$y$$$ differ, $$$x_i > y_i$$$.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n unsigned long long data[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%llu\", &data[j]);\n }\n int settled = 0;\n int alive = n;\n unsigned long long now = 0;\n while (settled < alive) {\n unsigned long long max = now;\n int maxi;\n for (int j = settled; j < alive; j++) {\n unsigned long long result = now | data[j];\n if (result > max) {\n max = result;\n maxi = j;\n } else if (result == now) {\n unsigned long long tmp = data[--alive];\n data[alive] = data[j];\n data[j] = tmp;\n\n j--;\n }\n }\n if (max != now) {\n unsigned long long tmp = data[maxi];\n data[maxi] = data[settled];\n data[settled++] = tmp;\n now = max;\n }\n }\n for (int j = 0; j < n; j++) {\n printf(\"%lld \", data[j]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n for _ in 0..t {\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 a: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let mut b = a.clone();\n let mut used = vec![0; n];\n let mut ans = vec![0; 0];\n\n loop {\n let mut maxvalue = 0;\n let mut maxid = 0;\n\n for i in 0..n {\n if maxvalue < b[i] {\n maxvalue = b[i];\n maxid = i;\n }\n }\n\n if maxvalue == 0 {\n break;\n }\n\n ans.push(a[maxid]);\n used[maxid] = 1;\n let xor = ((1 << 32) - 1) ^ b[maxid];\n\n for i in 0..n {\n b[i] &= xor;\n }\n }\n\n for i in 0..n {\n if used[i] == 0 {\n ans.push(a[i]);\n }\n }\n\n println!(\n \"{}\",\n ans.iter()\n .map(std::string::ToString::to_string)\n .collect::>()\n .join(\" \")\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1433", "problem_description": "Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons: Tetrahedron. Tetrahedron has 4 triangular faces. Cube. Cube has 6 square faces. Octahedron. Octahedron has 8 triangular faces. Dodecahedron. Dodecahedron has 12 pentagonal faces. Icosahedron. Icosahedron has 20 triangular faces. All five kinds of polyhedrons are shown on the picture below: Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!", "c_code": "int solution() {\n char poligono[12];\n int n = 0;\n int i = 0;\n int caras = 0;\n\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%s\", poligono);\n if (poligono[0] == 'T') {\n caras = caras + 4;\n } else if (poligono[0] == 'C') {\n caras = caras + 6;\n } else if (poligono[0] == 'O') {\n caras = caras + 8;\n } else if (poligono[0] == 'D') {\n caras = caras + 12;\n } else if (poligono[0] == 'I') {\n caras = caras + 20;\n }\n }\n printf(\"%d\", caras);\n return 0;\n}", "rust_code": "fn solution() {\n let input = stdin();\n let handle = input.lock();\n let mut lines = handle.lines();\n\n let count: i32 = lines\n .next()\n .expect(\"ERROR: next count\")\n .expect(\"Unwrap ended\")\n .parse()\n .expect(\"Error: parse round\");\n\n let mut stack: Vec = Vec::new();\n\n for _ in 0..count {\n let line = &lines.next().unwrap().unwrap()[..];\n let face = match line {\n \"Tetrahedron\" => 4,\n \"Cube\" => 6,\n \"Octahedron\" => 8,\n \"Dodecahedron\" => 12,\n \"Icosahedron\" => 20,\n _ => 0,\n };\n stack.push(face);\n }\n println!(\"{}\", stack.iter().sum::());\n}", "difficulty": "hard"} {"problem_id": "1434", "problem_description": "\n\n

Problem A: Clock

\n

Problem

\n\n

\nがっちょ君はお気に入りの時計を持っている。ある日時計の長針が取れてしまい、どこかへなくしてしまった。しかし、がっちょ君はその時計を使い続けたいため短針だけで時刻を読み取りたいと考えている。\n

\n\n

\n短針の情報θに対する時刻(時h、分m)を出力せよ。時計はいわゆるアナログ時計で、1から12の数が等間隔に時計回りに昇順に並んでいるものである。
\n

\n\n\n

Input

\n

入力は以下の形式で与えられる。

\n
\nθ\n
\n\n

\n1行に短針の角度 θを表す整数が度数法(degree)で与えられる。\n短針が指す方向は、12時を指す方向を0度としたとき、時計回りにθ度回転した方向である。\n

\n\n

Constraints

\n\n
    \n
  • 0 ≤ θ ≤ 359
  • \n
\n\n\n\n

Output

\n

\n 短針が与えられた角度をとる時の時刻を
\n h m
\n の形で1行に出力する。
\n
\n 時刻に午前午後の区別はないため0 ≤ h ≤ 11とする。
\n

\n\n\n

Sample Input 1

\n
\n0\n
\n\n

Sample Output 1

\n
\n0 0\n
\n\n

Sample Input 2

\n
\n45\n
\n\n

Sample Output 2

\n
\n1 30\n
\n\n

Sample Input 3

\n
\n100\n
\n\n

Sample Output 3

\n
\n3 20\n
", "c_code": "int solution() {\n int a;\n scanf(\"%d\", &a);\n printf(\"%d %d\\n\", a / 30, a % 30 * 2);\n return 0;\n}", "rust_code": "fn solution() {\n input!(theta:i32);\n let S = 12 * 3600 / 360 * theta;\n let h = S / 3600;\n let m = S % 3600 / 60;\n println!(\"{} {}\", h, m);\n}", "difficulty": "hard"} {"problem_id": "1435", "problem_description": "

English Sentence

\n\n

\nYour task is to write a program which reads a text and prints two words. The first one is the word which is arise most frequently in the text. The second one is the word which has the maximum number of letters.\n

\n\n

\nThe text includes only alphabetical characters and spaces. A word is a sequence of letters which is separated by the spaces. \n

\n\n

Input

\n\n

\nA text is given in a line. You can assume the following conditions:\n

\n\n
    \n
  • The number of letters in the text is less than or equal to 1000.
  • \n
  • The number of letters in a word is less than or equal to 32.
  • \n
  • There is only one word which is arise most frequently in given text.
  • \n
  • There is only one word which has the maximum number of letters in given text.
  • \n
\n\n

Output

\n\n

\nThe two words separated by a space.\n

\n\n

Sample Input

\n\n
\nThank you for your mail and your lectures\n
\n\n

Output for the Sample Input

\n\n
\nyour lectures\n
", "c_code": "int solution() {\n int n = 0;\n int i;\n int cnt[100] = {0};\n int j = 0;\n int max = 0;\n int longer = 0;\n int cou[100] = {0};\n char str;\n char save[100][100];\n while (1) {\n scanf(\"%c\", &str);\n if (str == '\\n') {\n break;\n }\n if (str != ' ') {\n if (isupper(str)) {\n str = tolower(str);\n }\n save[j][n] = str;\n n++;\n cou[j]++;\n } else {\n for (i = 0; i < j; i++) {\n if (!strcmp(save[i], save[j])) {\n cnt[i]++;\n }\n }\n j++;\n n = 0;\n }\n }\n for (i = 0; i <= j; i++) {\n if (cnt[max] <= cnt[i]) {\n max = i;\n }\n }\n for (i = 1; i <= j + 1; i++) {\n if (cou[longer] <= cou[i]) {\n longer = i;\n }\n }\n printf(\"%s %s\\n\", save[max], save[longer]);\n\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 ss = s.split_whitespace().collect::>();\n\n let mut map_count = HashMap::with_capacity(ss.len());\n for s in ss {\n *map_count.entry(s).or_insert(0) += 1;\n }\n\n let max_count = {\n let mut ret = String::new();\n let max_value = map_count.values().max().unwrap();\n for (key, value) in &map_count {\n if value != max_value {\n continue;\n }\n ret = key.to_string();\n break;\n }\n ret\n };\n\n let max_len = map_count\n .keys()\n .max_by(|l, r| l.len().cmp(&r.len()))\n .unwrap();\n println!(\"{} {}\", max_count, max_len);\n}", "difficulty": "medium"} {"problem_id": "1436", "problem_description": "You are given a set of $$$n$$$ ($$$n$$$ is always a power of $$$2$$$) elements containing all integers $$$0, 1, 2, \\ldots, n-1$$$ exactly once.Find $$$\\frac{n}{2}$$$ pairs of elements such that: Each element in the set is in exactly one pair. The sum over all pairs of the bitwise AND of its elements must be exactly equal to $$$k$$$. Formally, if $$$a_i$$$ and $$$b_i$$$ are the elements of the $$$i$$$-th pair, then the following must hold: $$$$$$\\sum_{i=1}^{n/2}{a_i \\& b_i} = k,$$$$$$ where $$$\\&$$$ denotes the bitwise AND operation. If there are many solutions, print any of them, if there is no solution, print $$$-1$$$ instead.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n getchar();\n\n for (int qwq = 0; qwq < t; qwq++) {\n long long n;\n long long k;\n scanf(\"%lld %lld\", &n, &k);\n getchar();\n if (k == 3 && n == 4) {\n printf(\"-1\\n\");\n continue;\n }\n if (k == n - 1) {\n printf(\"0 2\\n1 %lld\\n\", n - 3);\n for (long long i = 3; i < n / 2; i++) {\n printf(\"%lld %lld\\n\", i, n - 1 - i);\n }\n printf(\"%lld %lld\\n\", k - 1, k);\n continue;\n }\n for (long long i = 0; i < n / 2; i++) {\n if (i != 0 && i != k && i != n - k - 1) {\n printf(\"%lld %lld\\n\", i, n - 1 - i);\n } else if (i == 0) {\n printf(\"%lld %lld\\n\", i, n - k - 1);\n } else if (i == k || i == n - k - 1) {\n printf(\"%lld %lld\\n\", k, n - 1);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let n = xs[0];\n let k = xs[1];\n let mask = n - 1;\n if k == mask {\n if n == 4 {\n println!(\"-1\");\n } else {\n println!(\"0 {}\", mask - 1);\n for i in 1..(n / 2 - 1) {\n let mut m = 1;\n let mut b = true;\n while m <= i {\n if i == m {\n println!(\"{} {}\", i, n - 2 * i - 1);\n b = false;\n }\n m *= 2;\n }\n if b {\n println!(\"{} {}\", i, n - i - 1);\n }\n }\n println!(\"{} {}\", n - 1, n / 2);\n }\n } else {\n println!(\"{} {}\", k, mask);\n for i in 1..(n / 2) {\n if i == k {\n println!(\"0 {}\", n - k - 1);\n } else if i == n - k - 1 {\n println!(\"{} 0\", n - k - 1);\n } else {\n println!(\"{} {}\", i, n - i - 1);\n }\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1437", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Takahashi, Nakahashi and Hikuhashi have integers A, B and C, respectively.\nAfter repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will get:

\n
    \n
  • Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.
  • \n
\n

However, if the absolute value of the answer exceeds 10^{18}, print Unfair instead.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A,B,C \\leq 10^9
  • \n
  • 0 \\leq K \\leq 10^{18}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C K\n
\n
\n
\n
\n
\n

Output

Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation K times.\nIf the absolute value of the answer exceeds 10^{18}, print Unfair instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 2 3 1\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

After one operation, Takahashi, Nakahashi and Hikuhashi have 5, 4 and 3, respectively. We should print 5-4=1.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 3 2 0\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1000000000 1000000000 1000000000 1000000000000000000\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n int a;\n int b;\n int k;\n scanf(\"%d%d%*d%d\", &a, &b, &k);\n if (k & 1) {\n printf(\"%d\", b - a);\n } else {\n printf(\"%d\", a - b);\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let (a, b, _c, k) = {\n let v: Vec = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();\n (v[0], v[1], v[2], v[3])\n };\n println!(\"{}\", if k % 2 == 0 { a - b } else { b - a });\n}", "difficulty": "medium"} {"problem_id": "1438", "problem_description": "

Bit Operation II

\n\n\n

\n Given two non-negative decimal integers $a$ and $b$, calculate their AND (logical conjunction), OR (logical disjunction) and XOR (exclusive disjunction) and print them in binary representation of 32 bits.\n

\n\n\n

Input

\n\n

\n The input is given in the following format.\n

\n\n
\n$a \\; b$\n
\n\n

Output

\n

\n Print results of AND, OR and XOR in a line respectively.\n

\n\n

Constraints

\n
    \n
  • $0 \\leq a, b \\leq 2^{32} - 1$
  • \n
\n\n

Sample Input 1

\n\n
\n8 10\n
\n\n

Sample Output 1

\n\n
\n00000000000000000000000000001000\n00000000000000000000000000001010\n00000000000000000000000000000010\n
", "c_code": "int solution() {\n unsigned int a;\n unsigned int b;\n unsigned int n;\n unsigned int m;\n unsigned int o;\n unsigned int x;\n fscanf(stdin, \"%u %u\", &n, &m);\n char buf[100];\n buf[32] = buf[65] = buf[98] = '\\n';\n buf[99] = '\\0';\n a = n & m;\n o = n | m;\n x = n ^ m;\n int i = 0;\n for (b = 2147483648U; b > 0; b >>= 1) {\n buf[i] = '0' + a / b;\n buf[i + 33] = '0' + o / b;\n buf[i + 66] = '0' + x / b;\n a %= b;\n o %= b;\n x %= b;\n i++;\n }\n fputs(buf, stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut buf = String::new();\n let _ = stdin.read_to_string(&mut buf);\n let mut iter = buf.split_whitespace();\n let a: u32 = iter.next().unwrap().parse().unwrap();\n let b: u32 = iter.next().unwrap().parse().unwrap();\n println!(\"{:032b}\", a & b);\n println!(\"{:032b}\", a | b);\n println!(\"{:032b}\", a ^ b);\n}", "difficulty": "hard"} {"problem_id": "1439", "problem_description": "Ivan wants to play a game with you. He picked some string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from $$$1$$$ to $$$n-1$$$), but he didn't tell you which strings are prefixes and which are suffixes.Ivan wants you to guess which of the given $$$2n-2$$$ strings are prefixes of the given string and which are suffixes. It may be impossible to guess the string Ivan picked (since multiple strings may give the same set of suffixes and prefixes), but Ivan will accept your answer if there is at least one string that is consistent with it. Let the game begin!", "c_code": "int solution() {\n int a;\n int b = 0;\n int n;\n int i;\n int j;\n int k;\n int l = 0;\n int m = 0;\n int flag = 0;\n int len[200];\n int len2[105] = {0};\n int len3[105] = {0};\n char str[200][105];\n char str2[105];\n char str3[105] = \" \";\n char str4[200];\n char str5[200];\n scanf(\"%d\", &n);\n a = 2 * n - 2;\n for (i = 0; i < a; i++) {\n scanf(\" %s\", str[i]);\n len[i] = strlen(str[i]);\n len2[len[i]] = 1;\n len3[len[i]] = 1;\n if (len[i] == b) {\n str2[n - 1] = str[i][n - 2];\n str3[0] = str[i][0];\n }\n if (len[i] == (n - 1) && b == 0) {\n strcpy(str2, str[i]);\n strcat(str3, str[i]);\n b = (n - 1);\n }\n }\n\n for (i = 0; i < a; i++) {\n j = 0;\n k = 0;\n while (str[i][j] == str2[j]) {\n j++;\n }\n\n if (str[i][j] == '\\0' && len2[len[i]] == 1) {\n str4[l] = 'P';\n len2[len[i]] = 0;\n l++;\n } else {\n str4[l] = 'S';\n l++;\n }\n while (str[i][k] == str3[k]) {\n k++;\n }\n if (str[i][k] == '\\0' && len3[len[i]] == 1) {\n str5[m] = 'P';\n len3[len[i]] = 0;\n m++;\n } else {\n str5[m] = 'S';\n m++;\n }\n }\n str4[l] = '\\0';\n str5[m] = '\\0';\n\n l = 0;\n m = 0;\n for (i = 0; i < a; i++) {\n if (str4[i] == 'P') {\n l++;\n }\n if (str5[i] == 'P') {\n m++;\n }\n }\n if (l == a / 2 && m != a / 2) {\n printf(\"%s\", str4);\n } else if (m == a / 2 && l != a / 2) {\n printf(\"%s\", str5);\n } else {\n for (i = 0; i < a; i++) {\n if (len[i] == 1) {\n if (str[i][0] == str3[0] || str[i][0] == str3[n - 1]) {\n for (j = i + 1; j < a; j++) {\n if (len[j] == 1) {\n if (str[j][0] == str3[0] || str[j][0] == str3[n - 1]) {\n printf(\"%s\", str5);\n flag = 1;\n }\n }\n }\n }\n break;\n }\n }\n if (flag != 1) {\n printf(\"%s\", str4);\n }\n }\n return 0;\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\n let n = get!(usize);\n let mut ss = vec![];\n for i in 0..2 * n - 2 {\n let s = get().as_bytes();\n ss.push((s, i, 0));\n }\n ss.sort();\n let mut i = 0;\n let mut guess = vec![0_u8; n];\n while i < ss.len() - 1 {\n if ss[i].0 == ss[i + 1].0 {\n ss[i].2 = 1;\n ss[i + 1].2 = 2;\n for j in 0..ss[i].0.len() {\n guess[j] = ss[i].0[j];\n let k = ss[i].0.len() - 1 - j;\n let j = guess.len() - 1 - j;\n guess[j] = ss[i].0[k];\n }\n i += 2;\n } else {\n i += 1;\n }\n }\n\n let mut foo = vec![];\n let mut bar = vec![];\n foo.push(guess);\n bar.push(vec![]);\n\n for i in 0..ss.len() {\n if ss[i].2 != 0 {\n for j in 0..bar.len() {\n bar[j].push(ss[i].2);\n }\n continue;\n }\n let mut tmp1 = vec![];\n let mut tmp2 = vec![];\n for j in 0..foo.len() {\n let f = &foo[j];\n let b = &bar[j];\n let s = &ss[i].0;\n let mut ok = true;\n for k in 0..s.len() {\n if f[k] != 0 && s[k] != f[k] {\n ok = false;\n break;\n }\n }\n if ok {\n let mut fc = f.clone();\n let mut bc = b.clone();\n bc.push(1);\n for k in 0..s.len() {\n fc[k] = s[k];\n }\n tmp1.push(fc);\n tmp2.push(bc);\n }\n let mut ok = true;\n for k in 0..s.len() {\n let r = f.len() - 1 - k;\n let k = s.len() - 1 - k;\n if f[r] != 0 && s[k] != f[r] {\n ok = false;\n break;\n }\n }\n if ok {\n let mut fc = f.clone();\n let mut bc = b.clone();\n bc.push(2);\n for k in 0..s.len() {\n let r = f.len() - 1 - k;\n let k = s.len() - 1 - k;\n fc[r] = s[k];\n }\n tmp1.push(fc);\n tmp2.push(bc);\n }\n }\n foo = tmp1;\n bar = tmp2;\n }\n\n for b in bar.iter() {\n let mut c1 = 0;\n let mut c2 = 0;\n for e in b.iter() {\n if *e == 1 {\n c1 += 1;\n } else {\n c2 += 1;\n }\n }\n if c1 != c2 {\n continue;\n }\n let mut ans = vec![0; ss.len()];\n for i in 0..ss.len() {\n ans[ss[i].1] = b[i];\n }\n for a in ans.iter() {\n if *a == 1 {\n print!(\"P\");\n } else {\n print!(\"S\");\n }\n }\n println!();\n break;\n }\n}", "difficulty": "medium"} {"problem_id": "1440", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

There is a train going from Station A to Station B that costs X yen (the currency of Japan).

\n

Also, there is a bus going from Station B to Station C that costs Y yen.

\n

Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.

\n

How much does it cost to travel from Station A to Station C if she uses this ticket?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq X,Y \\leq 100
  • \n
  • Y is an even number.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X Y\n
\n
\n
\n
\n
\n

Output

If it costs x yen to travel from Station A to Station C, print x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

81 58\n
\n
\n
\n
\n
\n

Sample Output 1

110\n
\n
    \n
  • The train fare is 81 yen.
  • \n
  • The train fare is 582=29 yen with the 50% discount.
  • \n
\n

Thus, it costs 110 yen to travel from Station A to Station C.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 54\n
\n
\n
\n
\n
\n

Sample Output 2

31\n
\n
\n
", "c_code": "int solution() {\n int x = 0;\n int y = 0;\n\n scanf(\"%d %d\", &x, &y);\n\n printf(\"%d\", x + (y / 2));\n\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n\n let mut line = String::new();\n\n let _ = scan.read_line(&mut line);\n\n let vec: Vec<&str> = line.split_whitespace().collect();\n\n let n: u32 = vec[0].parse().unwrap_or(0);\n let m: u32 = vec[1].parse().unwrap_or(0);\n println!(\"{}\", n + m / 2);\n}", "difficulty": "medium"} {"problem_id": "1441", "problem_description": "Petya has a rectangular Board of size $$$n \\times m$$$. Initially, $$$k$$$ chips are placed on the board, $$$i$$$-th chip is located in the cell at the intersection of $$$sx_i$$$-th row and $$$sy_i$$$-th column.In one action, Petya can move all the chips to the left, right, down or up by $$$1$$$ cell.If the chip was in the $$$(x, y)$$$ cell, then after the operation: left, its coordinates will be $$$(x, y - 1)$$$; right, its coordinates will be $$$(x, y + 1)$$$; down, its coordinates will be $$$(x + 1, y)$$$; up, its coordinates will be $$$(x - 1, y)$$$. If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.Note that several chips can be located in the same cell.For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.Since Petya does not have a lot of free time, he is ready to do no more than $$$2nm$$$ actions.You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in $$$2nm$$$ actions.", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n printf(\"%d\\n\", n + m - 3 + (n * m));\n for (int i = 0; i < m - 1; i++) {\n printf(\"L\");\n }\n for (int i = 0; i < n - 1; i++) {\n printf(\"D\");\n }\n for (int i = 0; i < n; i++) {\n if (i != 0) {\n printf(\"U\");\n }\n for (int j = 0; j < m - 1; j++) {\n if (i % 2) {\n printf(\"L\");\n } else {\n printf(\"R\");\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut stdin = BufReader::new(io::stdin());\n let mut stdout = BufWriter::new(io::stdout());\n\n let mut buffer = String::new();\n stdin.read_line(&mut buffer).unwrap();\n let buffer: Vec = buffer\n .trim()\n .split_ascii_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, m, k) = (buffer[0], buffer[1], buffer[2]);\n for _ in 0..k {\n let mut buffer = String::new();\n stdin.read_line(&mut buffer).unwrap();\n }\n for _ in 0..k {\n let mut buffer = String::new();\n stdin.read_line(&mut buffer).unwrap();\n }\n\n let mut rls: Vec = Vec::new();\n\n for _ in 1..n {\n rls.push(\"D\".to_string());\n }\n for _ in 1..m {\n rls.push(\"L\".to_string());\n }\n\n for i in 0..n {\n if i % 2 == 0 {\n for _ in 1..m {\n rls.push(\"R\".to_string());\n }\n } else {\n for _ in 1..m {\n rls.push(\"L\".to_string());\n }\n }\n rls.push(\"U\".to_string());\n }\n rls.pop();\n\n writeln!(stdout, \"{}\", rls.len()).unwrap();\n writeln!(stdout, \"{}\", rls.join(\"\")).unwrap();\n}", "difficulty": "medium"} {"problem_id": "1442", "problem_description": "There are n incoming messages for Vasya. The i-th message is going to be received after ti minutes. Each message has a cost, which equals to A initially. After being received, the cost of a message decreases by B each minute (it can become negative). Vasya can read any message after receiving it at any moment of time. After reading the message, Vasya's bank account receives the current cost of this message. Initially, Vasya's bank account is at 0.Also, each minute Vasya's bank account receives C·k, where k is the amount of received but unread messages.Vasya's messages are very important to him, and because of that he wants to have all messages read after T minutes.Determine the maximum amount of money Vasya's bank account can hold after T minutes.", "c_code": "int solution() {\n int n;\n int a;\n int b;\n int c;\n int t;\n scanf(\"%d%d%d%d%d\", &n, &a, &b, &c, &t);\n\n int time[n];\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &time[i]);\n }\n\n int ans = 0;\n\n if (b >= c) {\n printf(\"%d\", n * a);\n }\n\n else {\n ans = n * a;\n int x = 0;\n for (int i = 0; i < n; ++i) {\n x = x + t - time[i];\n }\n\n ans = ans + (c - b) * x;\n\n printf(\"%d\", ans);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io;\n use std::io::prelude::*;\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n let n: i64 = it.next().unwrap().parse().unwrap();\n let a: i64 = it.next().unwrap().parse().unwrap();\n let b: i64 = it.next().unwrap().parse().unwrap();\n let c: i64 = it.next().unwrap().parse().unwrap();\n let t: i64 = it.next().unwrap().parse().unwrap();\n\n let tl: Vec = it.map(|x| x.parse().unwrap()).collect();\n\n let ans = if c < b {\n n * a\n } else {\n tl.into_iter().map(|ti| a + (t - ti) * (c - b)).sum()\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1443", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

Snuke is buying a lamp.\nThe light of the lamp can be adjusted to m levels of brightness, represented by integers from 1 through m, by the two buttons on the remote control.

\n

The first button is a \"forward\" button. When this button is pressed, the brightness level is increased by 1, except when the brightness level is m, in which case the brightness level becomes 1.

\n

The second button is a \"favorite\" button. When this button is pressed, the brightness level becomes the favorite brightness level x, which is set when the lamp is purchased.

\n

Snuke is thinking of setting the favorite brightness level x so that he can efficiently adjust the brightness.\nHe is planning to change the brightness n-1 times. In the i-th change, the brightness level is changed from a_i to a_{i+1}. The initial brightness level is a_1.\nFind the number of times Snuke needs to press the buttons when x is set to minimize this number.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq n,m \\leq 10^5
  • \n
  • 1 \\leq a_i\\leq m
  • \n
  • a_i \\neq a_{i+1}
  • \n
  • n, m and a_i are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
n m\na_1 a_2a_n\n
\n
\n
\n
\n
\n

Output

Print the minimum number of times Snuke needs to press the buttons.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 6\n1 5 1 4\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

When the favorite brightness level is set to 1, 2, 3, 4, 5 and 6, Snuke needs to press the buttons 8, 9, 7, 5, 6 and 9 times, respectively.\nThus, Snuke should set the favorite brightness level to 4.\nIn this case, the brightness is adjusted as follows:

\n
    \n
  • In the first change, press the favorite button once, then press the forward button once.
  • \n
  • In the second change, press the forward button twice.
  • \n
  • In the third change, press the favorite button once.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

10 10\n10 9 8 7 6 5 4 3 2 1\n
\n
\n
\n
\n
\n

Sample Output 2

45\n
\n
\n
", "c_code": "int solution() {\n int i;\n int n;\n int m;\n int a;\n int b;\n int diff;\n long long sum = 0;\n long long add[100001][2] = {};\n scanf(\"%d %d\", &n, &m);\n scanf(\"%d\", &a);\n a--;\n for (i = 2; i <= n; i++) {\n scanf(\"%d\", &b);\n b--;\n diff = (b - a + m) % m;\n sum += diff;\n if (diff >= 2) {\n a = (a + 2) % m;\n b = (b + 1) % m;\n add[a][0]++;\n add[a][1]++;\n add[b][0]--;\n add[b][1] -= diff;\n if (b < a) {\n add[0][0]++;\n add[0][1] += m - a + 1;\n }\n a = (b + m - 1) % m;\n } else {\n a = b;\n }\n }\n\n long long tmp[2] = {};\n long long max = 0;\n for (i = 0; i < m; i++) {\n tmp[0] += add[i][0];\n tmp[1] += add[i][1];\n if (tmp[1] > max) {\n max = tmp[1];\n }\n tmp[1] += tmp[0];\n }\n printf(\"%lld\\n\", sum - max);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input!(n: usize, m: usize, a: [usize1; n]);\n let mut start = vec![vec![]; m];\n let mut end = vec![vec![]; m];\n for i in 0..(n - 1) {\n let from = a[i];\n let to = a[i + 1];\n start[from].push(to);\n end[to].push(from);\n }\n\n let mut total = 0;\n let mut count = 0;\n for from in 0..m {\n for &to in start[from].iter() {\n if from > to {\n count += 1;\n total += to + 1;\n } else {\n total += to - from;\n }\n }\n }\n let mut ans = total;\n for cur in 1..m {\n for &from in end[cur - 1].iter() {\n count -= 1;\n let to = cur - 1;\n total += (to + m - from) % m;\n total -= 1;\n }\n total -= count;\n\n ans = cmp::min(ans, total);\n count += start[cur - 1].len();\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1444", "problem_description": "Alice and Bob are playing a game. They start with a positive integer $$$n$$$ and take alternating turns doing operations on it. Each turn a player can subtract from $$$n$$$ one of its divisors that isn't $$$1$$$ or $$$n$$$. The player who cannot make a move on his/her turn loses. Alice always moves first.Note that they subtract a divisor of the current number in each turn.You are asked to find out who will win the game if both players play optimally.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int flag = 0;\n scanf(\"%d\", &n);\n if (n % 2 == 1) {\n printf(\"Bob\\n\");\n continue;\n }\n while (n > 1) {\n if (n % 2 == 1) {\n flag = -2;\n break;\n }\n n = n / 2;\n flag++;\n }\n if (flag == -2) {\n printf(\"Alice\\n\");\n continue;\n }\n if (flag % 2 == 0) {\n printf(\"Alice\\n\");\n continue;\n }\n if (flag % 2 == 1) {\n printf(\"Bob\\n\");\n continue;\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\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 alice_wins = if n % 2 == 0 {\n if n.count_ones() > 1 {\n true\n } else {\n n.trailing_zeros() % 2 == 0\n }\n } else {\n false\n };\n\n let ans = if alice_wins { \"Alice\" } else { \"Bob\" };\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "easy"} {"problem_id": "1445", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 1; i <= t; i++) {\n int size = 0;\n scanf(\"%d\", &size);\n int arr[size];\n for (int j = 0; j < size; j++) {\n scanf(\"%d\", &arr[j]);\n }\n int flag = 0;\n for (int j = 0; j <= size - 3; j++) {\n for (int k = j + 2; k < size; k++) {\n if (arr[k] == arr[j]) {\n flag = 1;\n break;\n }\n }\n if (flag == 1) {\n break;\n }\n }\n if (flag == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n stdin().read_line(&mut t).unwrap();\n for _i in 0..t.trim().parse::().unwrap() {\n let mut n = String::new();\n stdin().read_line(&mut n).unwrap();\n n = String::new();\n stdin().read_line(&mut n).unwrap();\n let n: Vec<_> = n\n .trim()\n .split(' ')\n .map(|i| i.parse::().unwrap())\n .collect();\n if n.len() < 3 {\n println!(\"NO\");\n continue;\n }\n let mut b = false;\n for (j, iter) in n[..n.len() - 2].iter().enumerate() {\n if n[j + 2..].contains(iter) {\n println!(\"YES\");\n b = true;\n break;\n }\n }\n if !b {\n println!(\"NO\")\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1446", "problem_description": "Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below. So, for example, to show the digit 3 on the display, 5 sections must be highlighted; and for the digit 6, 6 sections must be highlighted. The battery of the newest device allows to highlight at most n sections on the display. Stepan wants to know the maximum possible integer number which can be shown on the display of his newest device. Your task is to determine this number. Note that this number must not contain leading zeros. Assume that the size of the display is enough to show any integer.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n if (n % 2 == 1 && n > 2) {\n printf(\"7\");\n n -= 3;\n }\n while (n > 1) {\n printf(\"1\");\n n -= 2;\n }\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 mut n: u32 = n.trim().parse().expect(\"Error while parsing!\");\n\n while n > 0 {\n if n % 2 == 1 {\n print!(\"7\");\n n -= 3;\n } else {\n print!(\"1\");\n n -= 2;\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1447", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

It is September 9 in Japan now.

\n

You are given a two-digit integer N. Answer the question: Is 9 contained in the decimal notation of N?

\n
\n
\n
\n
\n

Constraints

    \n
  • 10≤N≤99
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If 9 is contained in the decimal notation of N, print Yes; if not, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

29\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

The one's digit of 29 is 9.

\n
\n
\n
\n
\n
\n

Sample Input 2

72\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

72 does not contain 9.

\n
\n
\n
\n
\n
\n

Sample Input 3

91\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution(void) {\n char no[100];\n\n scanf(\"%s\", no);\n\n if (no[0] != '9' && no[1] != '9') {\n printf(\"No\\n\");\n } else {\n printf(\"Yes\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n let _ = io::stdin().read_to_string(&mut buffer);\n print!(\n \"{}\",\n if buffer.chars().any(|c| c == '9') {\n \"Yes\"\n } else {\n \"No\"\n }\n );\n}", "difficulty": "easy"} {"problem_id": "1448", "problem_description": "To improve the boomerang throwing skills of the animals, Zookeeper has set up an $$$n \\times n$$$ grid with some targets, where each row and each column has at most $$$2$$$ targets each. The rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns are numbered from $$$1$$$ to $$$n$$$ from left to right. For each column, Zookeeper will throw a boomerang from the bottom of the column (below the grid) upwards. When the boomerang hits any target, it will bounce off, make a $$$90$$$ degree turn to the right and fly off in a straight line in its new direction. The boomerang can hit multiple targets and does not stop until it leaves the grid. In the above example, $$$n=6$$$ and the black crosses are the targets. The boomerang in column $$$1$$$ (blue arrows) bounces $$$2$$$ times while the boomerang in column $$$3$$$ (red arrows) bounces $$$3$$$ times. The boomerang in column $$$i$$$ hits exactly $$$a_i$$$ targets before flying out of the grid. It is known that $$$a_i \\leq 3$$$.However, Zookeeper has lost the original positions of the targets. Thus, he asks you to construct a valid configuration of targets that matches the number of hits for each column, or tell him that no such configuration exists. If multiple valid configurations exist, you may print any of them.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int i;\n int a[100005];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int y[100005];\n int ans[200005][2];\n int cnt = 0;\n int s[100005];\n int ss = 0;\n int t[100005];\n int tt = 0;\n for (i = n - 1; i >= 0; i--) {\n if (a[i] == 0) {\n continue;\n }\n if (a[i] == 1) {\n ans[cnt][0] = i + 1;\n ans[cnt][1] = i + 1;\n cnt++;\n s[ss] = i;\n ss++;\n y[i] = i + 1;\n } else if (a[i] == 2) {\n if (ss == 0) {\n printf(\"-1\\n\");\n return 0;\n }\n ss--;\n ans[cnt][0] = y[s[ss]];\n ans[cnt][1] = i + 1;\n cnt++;\n t[tt] = i;\n tt++;\n } else {\n if (tt > 0) {\n tt--;\n ans[cnt][0] = i + 1;\n ans[cnt][1] = i + 1;\n cnt++;\n ans[cnt][0] = i + 1;\n ans[cnt][1] = t[tt] + 1;\n cnt++;\n t[tt] = i;\n tt++;\n y[i] = i + 1;\n } else {\n if (ss == 0) {\n printf(\"-1\\n\");\n return 0;\n }\n ss--;\n ans[cnt][0] = i + 1;\n ans[cnt][1] = i + 1;\n cnt++;\n ans[cnt][0] = i + 1;\n ans[cnt][1] = s[ss] + 1;\n cnt++;\n t[tt] = i;\n tt++;\n y[i] = i + 1;\n }\n }\n }\n printf(\"%d\\n\", cnt);\n for (i = 0; i < cnt; i++) {\n printf(\"%d %d\\n\", ans[i][0], ans[i][1]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let n: usize = str.trim().parse().unwrap();\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let throws: Vec = str\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .rev()\n .collect();\n let mut heights = vec![0; n];\n let mut one: usize = 0;\n let mut line: usize = 0;\n let mut two: usize = 0;\n let mut flag = true;\n let mut xy: Vec<(usize, usize)> = Vec::new();\n for i in 0..n {\n match throws[i] {\n 1 => {\n heights[i] = line;\n line += 1;\n xy.push((i, heights[i]));\n }\n 2 => {\n while one <= i {\n if throws[one] == 1 {\n break;\n }\n one += 1;\n }\n if one > i {\n flag = false;\n break;\n } else {\n heights[i] = heights[one];\n xy.push((i, heights[i]));\n one += 1;\n }\n }\n 3 => {\n while two < i {\n if throws[two] > 1 {\n break;\n }\n two += 1;\n }\n let mut bounce = two;\n if two >= i {\n while one <= i {\n if throws[one] == 1 {\n break;\n }\n one += 1;\n }\n if one > i {\n flag = false;\n break;\n }\n bounce = one;\n one += 1;\n } else {\n two += 1;\n }\n heights[i] = line;\n line += 1;\n xy.push((i, heights[i]));\n xy.push((bounce, heights[i]));\n }\n _ => {\n continue;\n }\n }\n }\n if flag {\n println!(\"{}\", xy.len());\n for (a, b) in xy {\n println!(\"{} {}\", n - b, n - a);\n }\n } else {\n println!(\"-1\");\n }\n}", "difficulty": "hard"} {"problem_id": "1449", "problem_description": "A binary matrix is called good if every even length square sub-matrix has an odd number of ones. Given a binary matrix $$$a$$$ consisting of $$$n$$$ rows and $$$m$$$ columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all. All the terms above have their usual meanings — refer to the Notes section for their formal definitions.", "c_code": "int solution() {\n int j;\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n char a[n][m];\n for (j = 0; j < n; j++) {\n scanf(\"%s\", &(a[j][0]));\n }\n if (n > 3) {\n printf(\"-1\\n\");\n return (0);\n }\n if (n == 1) {\n printf(\"0\\n\");\n return (0);\n }\n if (n == 2) {\n int min, count = 0;\n for (j = 0; j < m; j++) {\n int f = 0;\n if (a[0][j] == '1')\n f++;\n if (a[1][j] == '1')\n f++;\n if (j % 2 == 0) {\n if ((f % 2) == 1)\n count++;\n } else {\n if (f % 2 == 0)\n count++;\n }\n }\n min = count;\n count = 0;\n for (j = 0; j < m; j++) {\n int f = 0;\n if (a[0][j] == '1')\n f++;\n if (a[1][j] == '1')\n f++;\n if (j % 2 == 0) {\n if ((f % 2) == 0)\n count++;\n } else {\n if (f % 2 == 1)\n count++;\n }\n }\n if (min > count)\n min = count;\n printf(\"%d\\n\", min);\n } else if (n == 3) {\n int min, count = 0;\n for (j = 0; j < m; j++) {\n int f1 = 0, f2 = 0;\n if (a[0][j] == '1')\n f1++;\n if (a[1][j] == '1') {\n f2++;\n f1++;\n }\n if (a[2][j] == '1')\n f2++;\n if (j % 2 == 0) {\n if (((f1 % 2) == 1) && ((f2 % 2) == 1)) {\n count++;\n continue;\n }\n if ((f1 % 2) == 1)\n count++;\n if (f2 % 2 == 1)\n count++;\n } else {\n if (((f1 % 2) == 0) && ((f2 % 2) == 0)) {\n count++;\n continue;\n }\n if (f1 % 2 == 0)\n count++;\n if (f2 % 2 == 0)\n count++;\n }\n }\n min = count;\n count = 0;\n for (j = 0; j < m; j++) {\n int f1 = 0, f2 = 0;\n if (a[0][j] == '1')\n f1++;\n if (a[1][j] == '1') {\n f2++;\n f1++;\n };\n if (a[2][j] == '1')\n f2++;\n if (j % 2 == 0) {\n if (((f1 % 2) == 1) && ((f2 % 2) == 0)) {\n count++;\n continue;\n }\n if ((f1 % 2) == 1)\n count++;\n if (f2 % 2 == 0)\n count++;\n } else {\n if (((f1 % 2) == 0) && ((f2 % 2) == 1)) {\n count++;\n continue;\n }\n if (f1 % 2 == 0)\n count++;\n if (f2 % 2 == 1)\n count++;\n }\n }\n if (min > count)\n min = count;\n count = 0;\n for (j = 0; j < m; j++) {\n int f1 = 0, f2 = 0;\n if (a[0][j] == '1')\n f1++;\n if (a[1][j] == '1') {\n f2++;\n f1++;\n };\n if (a[2][j] == '1')\n f2++;\n if (j % 2 == 0) {\n if (((f1 % 2) == 0) && ((f2 % 2) == 1)) {\n count++;\n continue;\n }\n if ((f1 % 2) == 0)\n count++;\n if (f2 % 2 == 1)\n count++;\n } else {\n if (((f1 % 2) == 1) && ((f2 % 2) == 0)) {\n count++;\n continue;\n }\n if (f1 % 2 == 1)\n count++;\n if (f2 % 2 == 0)\n count++;\n }\n }\n if (min > count)\n min = count;\n count = 0;\n for (j = 0; j < m; j++) {\n int f1 = 0, f2 = 0;\n if (a[0][j] == '1')\n f1++;\n if (a[1][j] == '1') {\n f2++;\n f1++;\n };\n if (a[2][j] == '1')\n f2++;\n if (j % 2 == 0) {\n if (((f1 % 2) == 0) && ((f2 % 2) == 0)) {\n count++;\n continue;\n }\n if ((f1 % 2) == 0)\n count++;\n if (f2 % 2 == 0)\n count++;\n } else {\n if (((f1 % 2) == 1) && ((f2 % 2) == 1)) {\n count++;\n continue;\n }\n if (f1 % 2 == 1)\n count++;\n if (f2 % 2 == 1)\n count++;\n }\n }\n if (min > count)\n min = count;\n printf(\"%d\\n\", min);\n }\n return (0);\n}", "rust_code": "fn solution() {\n let stdout = io::stdout();\n let mut output = BufWriter::new(stdout.lock());\n let mut stdin = io::stdin();\n let mut input_string = String::new();\n stdin.read_to_string(&mut input_string).unwrap();\n let mut input_iter = input_string.split_whitespace();\n\n\n \n\n let n: usize = read!();\n let m: usize = read!();\n let mut matrix = Vec::with_capacity(n);\n\n for _ in 0..n {\n let row_string: String = read!();\n let row = row_string.chars().collect::>();\n matrix.push(row);\n }\n\n if n == 1 || m == 1 {\n println!(\"0\");\n return;\n }\n\n if n > 3 && m > 3 {\n println!(\"-1\");\n return;\n }\n\n if m > n {\n let matrix_clone = matrix;\n matrix = vec![vec!['_'; n]; m];\n for i in 0..n {\n for j in 0..m {\n matrix[j][i] = matrix_clone[i][j];\n }\n }\n }\n\n let decimal_vector: Vec = matrix\n .iter()\n .map(|row| row.iter().collect::())\n .map(|row_str| u32::from_str_radix(&row_str, 2).unwrap())\n .collect();\n\n let mut possible_adjacent_values = vec![vec![(0, 0); 8]; 4];\n\n possible_adjacent_values[2][0b_00] = (0b_01, 0b_10);\n possible_adjacent_values[2][0b_01] = (0b_00, 0b_11);\n possible_adjacent_values[2][0b_10] = (0b_00, 0b_11);\n possible_adjacent_values[2][0b_11] = (0b_01, 0b_10);\n\n possible_adjacent_values[3][0b_000] = (0b_010, 0b_101);\n possible_adjacent_values[3][0b_001] = (0b_011, 0b_100);\n possible_adjacent_values[3][0b_010] = (0b_000, 0b_111);\n possible_adjacent_values[3][0b_011] = (0b_001, 0b_110);\n possible_adjacent_values[3][0b_100] = (0b_001, 0b_110);\n possible_adjacent_values[3][0b_101] = (0b_000, 0b_111);\n possible_adjacent_values[3][0b_110] = (0b_011, 0b_100);\n possible_adjacent_values[3][0b_111] = (0b_010, 0b_101);\n\n let width = min(n, m);\n let mut mask_upperbound = 8;\n if width == 2 {\n mask_upperbound = 4;\n }\n\n let mut min_cost = vec![vec![0; mask_upperbound]; decimal_vector.len()];\n\n for mask in 0..mask_upperbound {\n min_cost[0][mask] = ((mask as u32) ^ decimal_vector[0]).count_ones();\n }\n\n for i in 1..decimal_vector.len() {\n for mask in 0..mask_upperbound {\n let change_to_mask_cost = ((mask as u32) ^ decimal_vector[i]).count_ones();\n let (a, b) = possible_adjacent_values[width][mask];\n min_cost[i][mask] = change_to_mask_cost + min(min_cost[i - 1][a], min_cost[i - 1][b]);\n }\n }\n\n println!(\n \"{}\",\n min_cost[decimal_vector.len() - 1].iter().min().unwrap()\n );\n}", "difficulty": "easy"} {"problem_id": "1450", "problem_description": "You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps.", "c_code": "int solution() {\n\n int t = 0;\n int n = 0;\n int x = 0;\n int a = 0;\n int b = 0;\n\n int i = 0;\n int j = 0;\n\n int nminusbig = 0;\n int smallminusone = 0;\n int modified = 0;\n int finaldistance = 0;\n int nsdistance = 0;\n\n scanf(\"%d\", &t);\n\n for (i = 0; i < t; i++) {\n scanf(\"%d %d %d %d\", &n, &x, &a, &b);\n\n if (a < b) {\n\n nminusbig = n - b;\n smallminusone = a - 1;\n\n if (nminusbig == 0) {\n\n if (smallminusone == 0) {\n finaldistance = b - a;\n } else if (smallminusone >= x) {\n modified = a - x;\n\n finaldistance = (b - modified);\n } else if (smallminusone < x) {\n nsdistance = nminusbig + smallminusone;\n if (nsdistance >= x) {\n finaldistance = (b - a + x);\n } else if (nsdistance < x) {\n finaldistance = (n - 1);\n }\n }\n } else if (nminusbig >= x) {\n modified = b + x;\n\n finaldistance = modified - a;\n } else if (nminusbig < x) {\n\n if (smallminusone >= x) {\n modified = a - x;\n\n finaldistance = (b - modified);\n } else if (smallminusone < x) {\n nsdistance = nminusbig + smallminusone;\n if (nsdistance >= x) {\n finaldistance = (b - a + x);\n } else if (nsdistance < x) {\n finaldistance = (n - 1);\n }\n }\n }\n } else {\n\n nminusbig = n - a;\n smallminusone = b - 1;\n\n if (nminusbig == 0) {\n\n if (smallminusone == 0) {\n finaldistance = a - b;\n } else if (smallminusone >= x) {\n modified = b - x;\n\n finaldistance = (a - modified);\n } else if (smallminusone < x) {\n nsdistance = nminusbig + smallminusone;\n if (nsdistance >= x) {\n finaldistance = (a - b + x);\n } else {\n finaldistance = (n - 1);\n }\n }\n } else if (nminusbig >= x) {\n modified = a + x;\n\n finaldistance = modified - b;\n } else if (nminusbig < x) {\n\n if (smallminusone >= x) {\n modified = b - x;\n\n finaldistance = (a - modified);\n } else if (smallminusone < x) {\n nsdistance = nminusbig + smallminusone;\n if (nsdistance >= x) {\n finaldistance = (a - b + x);\n } else {\n finaldistance = (n - 1);\n }\n }\n }\n }\n\n printf(\"%d\\n\", finaldistance);\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"Failed to read line\");\n let num_queries = buffer.trim().parse::().expect(\"parse error\");\n\n for _i in 0..num_queries {\n buffer.clear();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"Failed to read line\");\n let values = buffer\n .split_whitespace()\n .map(|x| x.parse::())\n .collect::, _>>()\n .unwrap();\n\n assert!(values.len() == 4);\n let n = values[0];\n let x = values[1];\n let a = values[2];\n let b = values[3];\n\n let max_dist = cmp::min(a, b) - 1 + n - cmp::max(a, b);\n let acc_dist = (a - b).abs();\n let acc_max_sep = acc_dist + cmp::min(max_dist, x);\n\n println!(\"{}\", acc_max_sep);\n }\n}", "difficulty": "medium"} {"problem_id": "1451", "problem_description": "There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet.You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order. You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout.Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters.", "c_code": "int solution() {\n char s[26];\n char s1[26];\n char w[10001];\n char t[26];\n scanf(\"%s\", s);\n scanf(\"%s\", s1);\n scanf(\"%s\", w);\n for (int i = 0; i < 26; i++) {\n t[s[i] - 'a'] = s1[i];\n }\n int i = 0;\n while (w[i] != '\\0') {\n if (w[i] - '0' >= 0 && w[i] - '0' <= 9) {\n printf(\"%c\", w[i]);\n } else if (w[i] - 'a' >= 0 && w[i] - 'a' <= 25) {\n printf(\"%c\", t[w[i] - 'a']);\n } else if (w[i] - 'A' >= 0 && w[i] - 'A' <= 25) {\n printf(\"%c\", t[w[i] - 'A'] - 32);\n }\n i++;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut fl = String::new();\n\n stdin().read_line(&mut fl).unwrap();\n\n let fl = fl.trim();\n\n let mut sl = String::new();\n\n stdin().read_line(&mut sl).unwrap();\n\n let sl = sl.trim();\n\n let mut txt = String::new();\n\n stdin().read_line(&mut txt).unwrap();\n\n let txt = txt.trim();\n\n let hm: HashMap<_, _> = fl.chars().zip(sl.chars()).collect();\n\n let txt: String = txt\n .chars()\n .map(|c| {\n if let Some(&d) = hm.get(&c) {\n d\n } else if let Some(&d) = hm.get(&c.to_lowercase().next().unwrap()) {\n d.to_uppercase().next().unwrap()\n } else {\n c\n }\n })\n .collect();\n\n println!(\"{}\", txt);\n}", "difficulty": "hard"} {"problem_id": "1452", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

\n

We have three boxes A, B, and C, each of which contains an integer.
\nCurrently, the boxes A, B, and C contain the integers X, Y, and Z, respectively.
\nWe will now do the operations below in order. Find the content of each box afterward.

\n
    \n
  • Swap the contents of the boxes A and B
  • \n
  • Swap the contents of the boxes A and C
  • \n
\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq X,Y,Z \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
X Y Z\n
\n
\n
\n
\n
\n

Output

\n

Print the integers contained in the boxes A, B, and C, in this order, with space in between.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

3 1 2\n
\n

After the contents of the boxes A and B are swapped, A, B, and C contain 2, 1, and 3, respectively.
\nThen, after the contents of A and C are swapped, A, B, and C contain 3, 1, and 2, respectively.

\n
\n
\n
\n
\n
\n

Sample Input 2

100 100 100\n
\n
\n
\n
\n
\n

Sample Output 2

100 100 100\n
\n
\n
\n
\n
\n
\n

Sample Input 3

41 59 31\n
\n
\n
\n
\n
\n

Sample Output 3

31 41 59\n
\n
\n
", "c_code": "int solution(void) {\n int X = 0;\n int Y = 0;\n int Z = 0;\n\n scanf(\"%d %d %d\", &X, &Y, &Z);\n\n printf(\"%d %d %d\", Z, X, Y);\n\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 mut v: Vec<_> = s\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n v.swap(0, 1);\n v.swap(0, 2);\n\n println!(\"{} {} {}\", v[0], v[1], v[2]);\n}", "difficulty": "medium"} {"problem_id": "1453", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Snuke signed up for a new website which holds programming competitions.\nHe worried that he might forget his password, and he took notes of it.\nSince directly recording his password would cause him trouble if stolen,\nhe took two notes: one contains the characters at the odd-numbered positions, and the other contains the characters at the even-numbered positions.

\n

You are given two strings O and E. O contains the characters at the odd-numbered positions retaining their relative order, and E contains the characters at the even-numbered positions retaining their relative order.\nRestore the original password.

\n
\n
\n
\n
\n

Constraints

    \n
  • O and E consists of lowercase English letters (a - z).
  • \n
  • 1 \\leq |O|,|E| \\leq 50
  • \n
  • |O| - |E| is either 0 or 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
O\nE\n
\n
\n
\n
\n
\n

Output

Print the original password.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

xyz\nabc\n
\n
\n
\n
\n
\n

Sample Output 1

xaybzc\n
\n

The original password is xaybzc. Extracting the characters at the odd-numbered positions results in xyz, and extracting the characters at the even-numbered positions results in abc.

\n
\n
\n
\n
\n
\n

Sample Input 2

atcoderbeginnercontest\natcoderregularcontest\n
\n
\n
\n
\n
\n

Sample Output 2

aattccooddeerrbreeggiunlnaerrccoonntteesstt\n
\n
\n
", "c_code": "int solution() {\n char o[51];\n char e[51];\n scanf(\"%s%s\", o, e);\n\n int eoo = 0;\n int eoe = 0;\n for (int i = 0; i < 51; i++) {\n if (o[i] != '\\0' && eoo == 0) {\n printf(\"%c\", o[i]);\n } else {\n eoo = 1;\n }\n\n if (e[i] != '\\0' && eoe == 0) {\n printf(\"%c\", e[i]);\n } else {\n eoe = 1;\n }\n\n if (eoo == 1 && eoe == 1) {\n break;\n }\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut a = String::new();\n let mut b = String::new();\n let mut s = String::new();\n\n std::io::stdin().read_line(&mut a).ok();\n std::io::stdin().read_line(&mut b).ok();\n\n let a = a.chars().collect::>();\n let b = b.chars().collect::>();\n\n for i in 0..a.len() + b.len() {\n if i % 2 == 0 {\n s.push(a[i / 2]);\n } else {\n s.push(b[i / 2]);\n }\n }\n println!(\"{}\", &s);\n}", "difficulty": "hard"} {"problem_id": "1454", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Snuke has N integers: 1,2,\\ldots,N.\nHe will choose K of them and give those to Takahashi.

\n

How many ways are there to choose K consecutive integers?

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq K \\leq N \\leq 50
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

There are two ways to choose two consecutive integers: (1,2) and (2,3).

\n
\n
\n
\n
\n
\n

Sample Input 2

13 3\n
\n
\n
\n
\n
\n

Sample Output 2

11\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int k = 0;\n scanf(\"%d %d\", &n, &k);\n printf(\"%d\\n\", n - k + 1);\n return 0;\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\n let n: usize = iter.next().unwrap().parse().unwrap();\n\n let k: usize = iter.next().unwrap().parse().unwrap();\n\n let ans: usize = n - k + 1;\n\n let out = stdout();\n let mut out = BufWriter::new(out.lock());\n writeln!(out, \"{}\", ans).unwrap();\n}", "difficulty": "easy"} {"problem_id": "1455", "problem_description": "There are $$$n$$$ logs, the $$$i$$$-th log has a length of $$$a_i$$$ meters. Since chopping logs is tiring work, errorgorn and maomao90 have decided to play a game.errorgorn and maomao90 will take turns chopping the logs with errorgorn chopping first. On his turn, the player will pick a log and chop it into $$$2$$$ pieces. If the length of the chosen log is $$$x$$$, and the lengths of the resulting pieces are $$$y$$$ and $$$z$$$, then $$$y$$$ and $$$z$$$ have to be positive integers, and $$$x=y+z$$$ must hold. For example, you can chop a log of length $$$3$$$ into logs of lengths $$$2$$$ and $$$1$$$, but not into logs of lengths $$$3$$$ and $$$0$$$, $$$2$$$ and $$$2$$$, or $$$1.5$$$ and $$$1.5$$$.The player who is unable to make a chop will be the loser. Assuming that both errorgorn and maomao90 play optimally, who will be the winner?", "c_code": "int solution() {\n int n = 0;\n int m = 0;\n int x[100];\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &m);\n for (int f = 0; f < m; f++) {\n scanf(\"%d\", &x[f]);\n }\n\n int count = 0;\n for (int k = 0; k < m; k++) {\n if (x[k] != 1) {\n count += x[k] - 1;\n }\n }\n\n if (count % 2 == 0) {\n printf(\"maomao90\\n\");\n } else {\n printf(\"errorgorn\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut str: String = String::new();\n io::stdin()\n .read_line(&mut str)\n .expect(\"failed to read input\");\n let t: usize = str.trim().parse::().expect(\"could not parse T\");\n let mut arr: [u32; 105] = [0; 105];\n for _t in 0..t {\n str.clear();\n io::stdin().read_line(&mut str).expect(\"failed to read n\");\n let n: usize = str.trim().parse::().expect(\"could not parse n\");\n str.clear();\n io::stdin().read_line(&mut str).expect(\"\");\n let ok = str.split(' ');\n let mut idx: usize = 0;\n for val in ok {\n arr[idx] = val.trim().parse::().expect(\"could not parse value\");\n idx += 1;\n }\n\n let mut res: u32 = 0;\n for i in 0..n {\n res ^= arr[i] - 1;\n }\n if res.is_multiple_of(2) {\n println!(\"maomao90\");\n } else {\n println!(\"errorgorn\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1456", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

In Japan, people make offerings called hina arare, colorful crackers, on March 3.

\n

We have a bag that contains N hina arare. (From here, we call them arare.)

\n

It is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.

\n

We have taken out the arare in the bag one by one, and the color of the i-th arare was S_i, where colors are represented as follows - pink: P, white: W, green: G, yellow: Y.

\n

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • S_i is P, W, G or Y.
  • \n
  • There always exist i, j and k such that S_i=P, S_j=W and S_k=G.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS_1 S_2 ... S_N\n
\n
\n
\n
\n
\n

Output

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\nG W Y P Y W\n
\n
\n
\n
\n
\n

Sample Output 1

Four\n
\n

The bag contained arare in four colors, so you should print Four.

\n
\n
\n
\n
\n
\n

Sample Input 2

9\nG W W G P W P G G\n
\n
\n
\n
\n
\n

Sample Output 2

Three\n
\n

The bag contained arare in three colors, so you should print Three.

\n
\n
\n
\n
\n
\n

Sample Input 3

8\nP Y W G Y W Y Y\n
\n
\n
\n
\n
\n

Sample Output 3

Four\n
\n
\n
", "c_code": "int solution(void) {\n\n int n = 0;\n char hina[101] = {0};\n int i = 0;\n int cnt = 0;\n\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%c\", hina + i);\n }\n i = 0;\n\n while (hina[i] != '\\0') {\n if (hina[i] == 'Y') {\n cnt++;\n }\n i++;\n }\n\n if (cnt > 0) {\n printf(\"Four\\n\");\n } else {\n printf(\"Three\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n\n let mut hn = [false; 4];\n for s in buf.trim().split(' ') {\n match s {\n \"P\" => hn[0] = true,\n \"W\" => hn[1] = true,\n \"G\" => hn[2] = true,\n \"Y\" => hn[3] = true,\n _ => (),\n }\n }\n\n if hn.iter().all(|&x| x) {\n println!(\"Four\");\n return;\n }\n println!(\"Three\");\n}", "difficulty": "medium"} {"problem_id": "1457", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given an integer N, find the base -2 representation of N.

\n

Here, S is the base -2 representation of N when the following are all satisfied:

\n
    \n
  • S is a string consisting of 0 and 1.
  • \n
  • Unless S = 0, the initial character of S is 1.
  • \n
  • Let S = S_k S_{k-1} ... S_0, then S_0 \\times (-2)^0 + S_1 \\times (-2)^1 + ... + S_k \\times (-2)^k = N.
  • \n
\n

It can be proved that, for any integer M, the base -2 representation of M is uniquely determined.

\n
\n
\n
\n
\n

Constraints

    \n
  • Every value in input is integer.
  • \n
  • -10^9 \\leq N \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the base -2 representation of N.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

-9\n
\n
\n
\n
\n
\n

Sample Output 1

1011\n
\n

As (-2)^0 + (-2)^1 + (-2)^3 = 1 + (-2) + (-8) = -9, 1011 is the base -2 representation of -9.

\n
\n
\n
\n
\n
\n

Sample Input 2

123456789\n
\n
\n
\n
\n
\n

Sample Output 2

11000101011001101110100010101\n
\n
\n
\n
\n
\n
\n

Sample Input 3

0\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n long long a[20];\n long long b[20];\n a[0] = 1;\n b[0] = 1;\n for (int i = 1; i < 20; i++) {\n a[i] = a[i - 1] * 4;\n }\n for (int i = 1; i < 20; i++) {\n b[i] = b[i - 1] + a[i];\n }\n int x = 0;\n for (int i = 17; i > 0; i--) {\n if (n > b[i - 1] && b[i] >= n) {\n printf(\"1\");\n x = 1;\n n -= a[i];\n } else if (x) {\n printf(\"0\");\n }\n if (n >= 0 - (b[i] - 1) / 2 && 0 - (b[i - 1] + 1) / 2 >= n) {\n printf(\"1\");\n x = 1;\n n += a[i] / 2;\n } else if (x) {\n printf(\"0\");\n }\n }\n printf(\"%d\\n\", n);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let mut n: i32 = s.trim().parse().unwrap();\n if n == 0 {\n println!(\"0\");\n } else {\n let mut ans = vec![];\n while n != 0 {\n ans.push(if n & 1 != 0 { '1' } else { '0' });\n n = (n & !1) / -2;\n }\n println!(\"{}\", ans.into_iter().rev().collect::());\n }\n}", "difficulty": "hard"} {"problem_id": "1458", "problem_description": "Petya has got an interesting flower. Petya is a busy person, so he sometimes forgets to water it. You are given $$$n$$$ days from Petya's live and you have to determine what happened with his flower in the end.The flower grows as follows: If the flower isn't watered for two days in a row, it dies. If the flower is watered in the $$$i$$$-th day, it grows by $$$1$$$ centimeter. If the flower is watered in the $$$i$$$-th and in the $$$(i-1)$$$-th day ($$$i > 1$$$), then it grows by $$$5$$$ centimeters instead of $$$1$$$. If the flower is not watered in the $$$i$$$-th day, it does not grow. At the beginning of the $$$1$$$-st day the flower is $$$1$$$ centimeter tall. What is its height after $$$n$$$ days?", "c_code": "int solution() {\n int t;\n int n;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &n);\n int a[n];\n int height = 1;\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n if (j > 0 && a[j] == 0 && a[j - 1] == 0) {\n height = -1;\n while (++j < n) {\n scanf(\"%d\", &a[j]);\n }\n break;\n }\n if (j > 0 && a[j] == 1 && a[j - 1] == 1) {\n height += 5;\n } else if (a[j] == 1) {\n height += 1;\n }\n }\n printf(\"%d\\n\", height);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut handle = std::io::BufWriter::new(std::io::stdout());\n let mut test_no = String::new();\n std::io::stdin().read_line(&mut test_no).unwrap();\n let test_no: u8 = test_no.trim().parse().unwrap();\n for _ in 0..test_no {\n let mut array_size = String::new();\n std::io::stdin().read_line(&mut array_size).unwrap();\n let array_size: u8 = array_size.trim().parse().unwrap();\n let mut array = String::new();\n std::io::stdin().read_line(&mut array).unwrap();\n let array: Vec<&str> = array.trim().split(' ').collect();\n let array: Vec = array.iter().map(|x| x.parse::().unwrap()).collect();\n let mut height: i16 = 1;\n if array[0] == 1 {\n height += 1\n }\n for i in 1..array_size {\n if array[i as usize] == 0 && array[(i - 1) as usize] == 0 {\n height = -1;\n break;\n } else if array[i as usize] == 1 {\n if array[(i - 1) as usize] == 1 {\n height += 5;\n } else {\n height += 1;\n }\n }\n }\n writeln!(handle, \"{}\", height).ok();\n }\n}", "difficulty": "medium"} {"problem_id": "1459", "problem_description": "Bob is a competitive programmer. He wants to become red, and for that he needs a strict training regime. He went to the annual meeting of grandmasters and asked $$$n$$$ of them how much effort they needed to reach red.\"Oh, I just spent $$$x_i$$$ hours solving problems\", said the $$$i$$$-th of them. Bob wants to train his math skills, so for each answer he wrote down the number of minutes ($$$60 \\cdot x_i$$$), thanked the grandmasters and went home. Bob could write numbers with leading zeroes — for example, if some grandmaster answered that he had spent $$$2$$$ hours, Bob could write $$$000120$$$ instead of $$$120$$$.Alice wanted to tease Bob and so she took the numbers Bob wrote down, and for each of them she did one of the following independently: rearranged its digits, or wrote a random number. This way, Alice generated $$$n$$$ numbers, denoted $$$y_1$$$, ..., $$$y_n$$$.For each of the numbers, help Bob determine whether $$$y_i$$$ can be a permutation of a number divisible by $$$60$$$ (possibly with leading zeroes).", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n while (n--) {\n char ISLA[150];\n scanf(\"%s\", ISLA);\n char E = 0;\n char Z = 0;\n int LL = 0;\n for (int i = 0; ISLA[i]; i++) {\n LL += ISLA[i] - '0';\n if (!((ISLA[i] - '0') & 1)) {\n if (ISLA[i] == '0') {\n if (Z == 0) {\n Z = 1;\n } else {\n E = 1;\n }\n } else {\n E = 1;\n }\n }\n }\n if (LL % 3 == 0 && E == 1 && Z == 1) {\n printf(\"red\\n\");\n } else {\n printf(\"cyan\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut result = Vec::new();\n\n let reader = io::stdin();\n let grandmaster_count = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n\n for _i in 0..grandmaster_count {\n let str_number = reader.lock().lines().next().unwrap().unwrap();\n\n let digits: Vec = str_number\n .chars()\n .map(|s| s.to_string().parse::().unwrap())\n .collect();\n\n let iterator = digits.iter();\n let is_divisible_by_3 = iterator.clone().map(|x| x % 3).sum::() % 3 == 0;\n\n let is_divisible_by_10 = !iterator\n .clone()\n .filter(|s| **s == 0)\n .collect::>()\n .is_empty();\n\n let amount_of_even = iterator\n .clone()\n .filter(|s| **s % 2 == 0)\n .collect::>()\n .len();\n\n if is_divisible_by_10 && is_divisible_by_3 && amount_of_even > 1 {\n result.push(\"red\");\n } else {\n result.push(\"cyan\");\n }\n }\n\n for val in result {\n println!(\"{}\", val);\n }\n}", "difficulty": "easy"} {"problem_id": "1460", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Given are a sequence of N positive integers A_1, A_2, \\ldots, A_N and another positive integer S.
\nFor a non-empty subset T of the set \\{1, 2, \\ldots , N \\}, let us define f(T) as follows:

\n
    \n
  • f(T) is the number of different non-empty subsets \\{x_1, x_2, \\ldots , x_k \\} of T such that A_{x_1}+A_{x_2}+\\cdots +A_{x_k} = S.
  • \n
\n

Find the sum of f(T) over all 2^N-1 subsets T of \\{1, 2, \\ldots , N \\}. Since the sum can be enormous, print it modulo 998244353.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 3000
  • \n
  • 1 \\leq S \\leq 3000
  • \n
  • 1 \\leq A_i \\leq 3000
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N S\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the sum of f(T) modulo 998244353.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4\n2 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

For each T, the value of f(T) is shown below. The sum of these values is 6.

\n
    \n
  • f(\\{1\\}) = 0
  • \n
  • f(\\{2\\}) = 0
  • \n
  • f(\\{3\\}) = 1 (One subset \\{3\\} satisfies the condition.)
  • \n
  • f(\\{1, 2\\}) = 1 (\\{1, 2\\})
  • \n
  • f(\\{2, 3\\}) = 1 (\\{3\\})
  • \n
  • f(\\{1, 3\\}) = 1 (\\{3\\})
  • \n
  • f(\\{1, 2, 3\\}) = 2 (\\{1, 2\\}, \\{3\\})
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5 8\n9 9 9 9 9\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 10\n3 1 4 1 5 9 2 6 5 3\n
\n
\n
\n
\n
\n

Sample Output 3

3296\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n long s;\n scanf(\"%ld %ld\", &n, &s);\n long a[n];\n for (long i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n }\n long mod = 998244353;\n long value;\n long dp[n][s + 1];\n for (long i = 0; i < n; i++) {\n for (long j = 0; j <= s; j++) {\n dp[i][j] = 0;\n }\n }\n dp[0][0] = 2;\n if (a[0] <= s) {\n dp[0][a[0]] = 1;\n }\n for (long i = 1; i < n; i++) {\n for (long j = 0; j <= s; j++) {\n dp[i][j] += dp[i - 1][j] * 2;\n dp[i][j] %= mod;\n value = j + a[i];\n if (value <= s) {\n dp[i][value] += dp[i - 1][j];\n dp[i][value] %= mod;\n }\n }\n }\n printf(\"%ld\\n\", dp[n - 1][s]);\n\n return 0;\n}", "rust_code": "fn solution() {\n const MOD: u64 = 998244353;\n\n let (n, s): (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 let n: usize = iter.next().unwrap().parse().unwrap();\n let s: usize = iter.next().unwrap().parse().unwrap();\n (n, s)\n };\n\n let a: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.split_whitespace().map(|x| x.parse().unwrap()).collect()\n };\n\n let mut dp: Vec> = vec![vec![0; s + 1]; n + 1];\n dp[0][0] = 1;\n\n for i in 0..n {\n for j in 0..(s + 1) {\n dp[i + 1][j] += dp[i][j] * 2;\n dp[i + 1][j] %= MOD;\n\n if j + a[i] <= s {\n dp[i + 1][j + a[i]] += dp[i][j];\n dp[i + 1][j + a[i]] %= MOD;\n }\n }\n }\n\n println!(\"{}\", dp[n][s]);\n}", "difficulty": "easy"} {"problem_id": "1461", "problem_description": "A class of students got bored wearing the same pair of shoes every day, so they decided to shuffle their shoes among themselves. In this problem, a pair of shoes is inseparable and is considered as a single object.There are $$$n$$$ students in the class, and you are given an array $$$s$$$ in non-decreasing order, where $$$s_i$$$ is the shoe size of the $$$i$$$-th student. A shuffling of shoes is valid only if no student gets their own shoes and if every student gets shoes of size greater than or equal to their size. You have to output a permutation $$$p$$$ of $$$\\{1,2,\\ldots,n\\}$$$ denoting a valid shuffling of shoes, where the $$$i$$$-th student gets the shoes of the $$$p_i$$$-th student ($$$p_i \\ne i$$$). And output $$$-1$$$ if a valid shuffling does not exist.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).", "c_code": "int solution() {\n int tc;\n scanf(\"%d\", &tc);\n for (int i = 0; i < tc; i++) {\n long long int n;\n scanf(\"%lli\\n\", &n);\n long long int arr[n + 1];\n for (long long int l = 0; l < n; l++) {\n scanf(\"%lli \", &arr[l]);\n }\n arr[n + 1] = -1;\n long long int count = 1;\n long long int flag = 0;\n long long int j = 0;\n long long int op[n];\n for (long long int l = 0; l < n; l++) {\n op[l] = 0;\n }\n while (j < n && flag == 0) {\n if (arr[j + 1] != arr[j] && count == 1) {\n flag = 1;\n } else if (arr[j + 1] == arr[j] && count >= 1) {\n count++;\n op[j] = j + 2;\n } else if (arr[j + 1] != arr[j] && count > 1) {\n op[j] = j - count + 2;\n count = 1;\n }\n j++;\n }\n if (flag == 0) {\n for (long long int k = 0; k < n; k++) {\n printf(\"%lli \", op[k]);\n }\n } else {\n printf(\"-1\");\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n\n let mut buffer = String::new();\n\n stdin.read_line(&mut buffer).unwrap();\n let count: u32 = buffer.trim().parse().unwrap();\n\n 'a: for _ in 0..count {\n stdin.read_line(&mut buffer).unwrap();\n\n buffer.clear();\n\n stdin.read_line(&mut buffer).unwrap();\n\n let data: Vec = buffer\n .trim()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut record = HashMap::new();\n\n for (index, value) in data.iter().enumerate() {\n let edit = if let Some(x) = record.get_mut(&value) {\n x\n } else {\n record.insert(value, (0, vec![]));\n record.get_mut(&value).unwrap()\n };\n edit.1.push(index);\n }\n\n let mut output = Vec::with_capacity(data.len());\n\n for value in data.iter() {\n let data = record.get_mut(&value).unwrap();\n\n if data.1.len() == 1 {\n println!(\"-1\");\n continue 'a;\n }\n\n data.0 += 1;\n output.push(data.1[data.0 % data.1.len()])\n }\n let mut output = output.into_iter();\n\n print!(\"{}\", 1 + output.next().unwrap());\n\n for i in output {\n print!(\" {}\", 1 + i);\n }\n println!()\n }\n}", "difficulty": "medium"} {"problem_id": "1462", "problem_description": "

Print Test Cases

\n\n

\nIn the online judge system, a judge file may include multiple datasets to check whether the submitted program outputs a correct answer for each test case. This task is to practice solving a problem with multiple datasets.\n

\n\n

\nWrite a program which reads an integer x and print it as is. Note that multiple datasets are given for this problem.\n

\n\n\n

Input

\n\n

\nThe input consists of multiple datasets. Each dataset consists of an integer x in a line.\n

\n\n

\nThe input ends with an integer 0. You program should not process (print) for this terminal symbol.\n

\n\n\n

Output

\n\n

\nFor each dataset, print x in the following format:\n

\n\n
\nCase i: x\n
\n\n

\nwhere i is the case number which starts with 1. Put a single space between \"Case\" and i. Also, put a single space between ':' and x.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ x ≤ 10000
  • \n
  • The number of datasets ≤ 10000
  • \n
\n\n\n

Sample Input

\n
\n3\n5\n11\n7\n8\n19\n0\n
\n

Sample Output

\n
\nCase 1: 3\nCase 2: 5\nCase 3: 11\nCase 4: 7\nCase 5: 8\nCase 6: 19\n
", "c_code": "int solution() {\n int n = 0;\n int i = 1;\n while (scanf(\"%d\", &n) && n != 0) {\n printf(\"Case %d: %d\\n\", i++, n);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let out = &mut BufWriter::new(stdout());\n\n for i in 0..10000 {\n let mut input = String::new();\n stdin().read_line(&mut input).ok();\n match input.trim().parse::() {\n Ok(x) => {\n writeln!(out, \"Case {}: {}\", i + 1, x).ok();\n }\n _ => break,\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1463", "problem_description": "

Shuffle


\n\n

\n Your task is to shuffle a deck of n cards, each of which is marked by a alphabetical letter.\n

\n\n

\n A single shuffle action takes out h cards from the bottom of the deck and moves them to the top of the deck.\n

\n\n

\n The deck of cards is represented by a string as follows.\n

\n\n
\nabcdeefab\n
\n\n

\n The first character and the last character correspond to the card located at the bottom of the deck and the card on the top of the deck respectively.\n

\n\n

\n For example, a shuffle with h = 4 to the above deck, moves the first 4 characters \"abcd\" to the end of the remaining characters \"eefab\", and generates the following deck:\n

\n\n
\neefababcd\n
\n\n

\n You can repeat such shuffle operations.\n

\n\n

\n Write a program which reads a deck (a string) and a sequence of h, and prints the final state (a string). \n

\n\n\n

Input

\n\n

\n The input consists of multiple datasets. Each dataset is given in the following format:\n

\n\n
\nA string which represents a deck\nThe number of shuffle m\nh1\nh2\n  .\n  .\nhm\n
\n\n

\n The input ends with a single character '-' for the string.\n

\n\n

Constraints

\n\n
    \n
  • The length of the string ≤ 200
  • \n
  • 1 ≤ m ≤ 100
  • \n
  • 1 ≤ hi < The length of the string
  • \n
  • The number of datasets ≤ 10
  • \n
\n\n\n\n

Output

\n\n

\n For each dataset, print a string which represents the final state in a line.\n

\n\n

Sample Input

\n\n
\naabc\n3\n1\n2\n1\nvwxyz\n2\n3\n4\n-\n
\n\n

Sample Output

\n\n
\naabc\nxyzvw\n
", "c_code": "int solution() {\n char buf[2][201];\n char *p1 = buf[0];\n char *p2 = buf[1];\n\n while (scanf(\"%s\\n\", p1) && strcmp(p1, \"-\") != 0) {\n int m;\n scanf(\"%d\\n\", &m);\n\n while (m--) {\n int h;\n scanf(\"%d\\n\", &h);\n\n int rest = strlen(p1) - h;\n memcpy(p2, p1 + h, rest);\n memcpy(p2 + rest, p1, h);\n *(p2 + h + rest) = '\\0';\n\n char *t = p1;\n p1 = p2;\n p2 = t;\n }\n puts(p1);\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut lines = stdin.lock().lines();\n\n loop {\n let word = lines.by_ref().next().unwrap().unwrap();\n let word = word.trim();\n\n if word == \"-\" {\n break;\n }\n\n let m = lines\n .by_ref()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n let h = lines\n .by_ref()\n .take(m)\n .map(|x| x.unwrap().parse::().unwrap())\n .sum::()\n % word.len();\n\n println!(\"{}{}\", &word[h..], &word[..h]);\n }\n}", "difficulty": "medium"} {"problem_id": "1464", "problem_description": "Hilbert's Hotel is a very unusual hotel since the number of rooms is infinite! In fact, there is exactly one room for every integer, including zero and negative integers. Even stranger, the hotel is currently at full capacity, meaning there is exactly one guest in every room. The hotel's manager, David Hilbert himself, decides he wants to shuffle the guests around because he thinks this will create a vacancy (a room without a guest).For any integer $$$k$$$ and positive integer $$$n$$$, let $$$k\\bmod n$$$ denote the remainder when $$$k$$$ is divided by $$$n$$$. More formally, $$$r=k\\bmod n$$$ is the smallest non-negative integer such that $$$k-r$$$ is divisible by $$$n$$$. It always holds that $$$0\\le k\\bmod n\\le n-1$$$. For example, $$$100\\bmod 12=4$$$ and $$$(-1337)\\bmod 3=1$$$.Then the shuffling works as follows. There is an array of $$$n$$$ integers $$$a_0,a_1,\\ldots,a_{n-1}$$$. Then for each integer $$$k$$$, the guest in room $$$k$$$ is moved to room number $$$k+a_{k\\bmod n}$$$.After this shuffling process, determine if there is still exactly one guest assigned to each room. That is, there are no vacancies or rooms with multiple guests.", "c_code": "int solution() {\n int t;\n int n;\n scanf(\"%d\", &t);\n while (t--) {\n int a[200001];\n int b[200001];\n int k = 1;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b[i] = ((i + a[i]) % n + n) % n;\n }\n for (int i = 0; i < n - 1 && k != 0; i++) {\n for (int j = i + 1; j < n && k != 0; j++) {\n\n if (b[i] == b[j]) {\n k = 0;\n }\n }\n }\n if (k == 0) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"Failed to read line\");\n let t: i32 = buffer.trim().parse().unwrap();\n for _ in 0..t {\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"Failed to read line\");\n let n: i32 = buffer.trim().parse().unwrap();\n let mut vec = vec![0; n as usize];\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"Failed to read line\");\n let a: Vec = buffer\n .trim()\n .split(\" \")\n .map(|s| s.parse().unwrap())\n .collect();\n let mut flag = false;\n for i in 0..a.len() {\n let x = (((i as i32 + a[i]) % n) + n) % n;\n if vec[x as usize] != 0 {\n flag = true;\n break;\n } else {\n vec[x as usize] = 1;\n }\n }\n if flag {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1465", "problem_description": "A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily $$$7$$$ days!In detail, she can choose any integer $$$k$$$ which satisfies $$$1 \\leq k \\leq r$$$, and set $$$k$$$ days as the number of days in a week.Alice is going to paint some $$$n$$$ consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.She wants to make all of the painted cells to be connected by side. It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side.Alice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar's sides.For example, in the picture, a week has $$$4$$$ days and Alice paints $$$5$$$ consecutive days. [1] and [2] are different shapes, but [1] and [3] are equal shapes. Alice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive $$$n$$$ days and paints them in calendar started in one of the days of the week. As was said before, she considers only shapes, there all cells are connected by side.", "c_code": "int solution() {\n int t;\n int i;\n long long A[1005];\n long long B[1005];\n scanf(\"%d\\n\", &t);\n for (i = 0; i < t; i++) {\n scanf(\"%lld %lld\", &A[i], &B[i]);\n\n if (A[i] > B[i]) {\n A[i] = B[i] * (B[i] + 1) / 2;\n } else {\n A[i] = A[i] * (A[i] - 1) / 2 + 1;\n }\n }\n for (i = 0; i < t; i++) {\n printf(\"%lld\\n\", A[i]);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, r) = (xs[0], xs[1]);\n let ans = if n > r {\n (1 + r) * r / 2\n } else {\n 1 + (n - 1) * n / 2\n };\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1466", "problem_description": "Mocha is a young girl from high school. She has learned so much interesting knowledge from her teachers, especially her math teacher. Recently, Mocha is learning about binary system and very interested in bitwise operation.This day, Mocha got a sequence $$$a$$$ of length $$$n$$$. In each operation, she can select an arbitrary interval $$$[l, r]$$$ and for all values $$$i$$$ ($$$0\\leq i \\leq r-l$$$), replace $$$a_{l+i}$$$ with $$$a_{l+i} \\,\\&\\, a_{r-i}$$$ at the same time, where $$$\\&$$$ denotes the bitwise AND operation. This operation can be performed any number of times.For example, if $$$n=5$$$, the array is $$$[a_1,a_2,a_3,a_4,a_5]$$$, and Mocha selects the interval $$$[2,5]$$$, then the new array is $$$[a_1,a_2\\,\\&\\, a_5, a_3\\,\\&\\, a_4, a_4\\,\\&\\, a_3, a_5\\,\\&\\, a_2]$$$.Now Mocha wants to minimize the maximum value in the sequence. As her best friend, can you help her to get the answer?", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n int x = arr[0];\n for (int i = 1; i < n; i++) {\n x &= arr[i];\n }\n printf(\"%d\\n\", x);\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 for _ in 0..t {\n let _n = lines.next().unwrap().parse::().unwrap();\n\n let s = lines.next().unwrap();\n let a = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let ans = a.fold(!0, |a, x| a & x);\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1467", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\\times N square. We denote with 1, 2,\\dots, N the viewers in the first row (from left to right); with N+1, \\dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\\dots, N^2.

\n

At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat.\nTo exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column).\nWhile leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever.

\n

Compute the number of pairs of viewers (x, y) such that y will hate x forever.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\le N \\le 500
  • \n
  • The sequence P_1, P_2, \\dots, P_{N^2} is a permutation of \\{1, 2, \\dots, N^2\\}.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the format

\n
N\nP_1 P_2 \\cdots P_{N^2}\n
\n
\n
\n
\n
\n

Output

If ans is the number of pairs of viewers described in the statement, you should print on Standard Output

\n
ans\n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 3 7 9 5 4 8 6 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Before the end of the movie, the viewers are arranged in the cinema as follows:

\n
1 2 3\n4 5 6\n7 8 9\n
\n

The first four viewers leaving the cinema (1, 3, 7, 9) can leave the cinema without going through any seat, so they will not be hated by anybody.

\n

Then, viewer 5 must go through one of the seats where viewers 2, 4, 6, 8 are currently seated while leaving the cinema; hence he will be hated by at least one of those viewers.

\n

Finally the remaining viewers can leave the cinema (in the order 4, 8, 6, 2) without going through any occupied seat (actually, they can leave the cinema without going through any seat at all).

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6\n11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30\n
\n
\n
\n
\n
\n

Sample Output 3

11\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int P[250001];\n scanf(\"%d\", &N);\n for (i = 1; i <= N * N; i++) {\n scanf(\"%d\", &(P[i]));\n }\n\n int j;\n int min[502][502] = {};\n int tmp;\n for (i = 1; i <= (N + 1) / 2; i++) {\n for (j = 1; j <= (N + 1) / 2; j++) {\n tmp = ((i < j) ? i : j) - 1;\n min[i][j] = tmp;\n min[N - i + 1][j] = tmp;\n min[i][N - j + 1] = tmp;\n min[N - i + 1][N - j + 1] = tmp;\n }\n }\n\n int k;\n int l;\n int n;\n int ans = 0;\n int flag[502][502] = {};\n int q[250001][2];\n int head;\n int tail;\n for (n = 1; n <= N * N; n++) {\n k = (P[n] + N - 1) / N;\n l = (P[n] - 1) % N + 1;\n flag[k][l] = 1;\n ans += min[k][l]--;\n\n q[0][0] = k;\n q[0][1] = l;\n for (head = 0, tail = 1; head < tail; head++) {\n i = q[head][0];\n j = q[head][1];\n if (i > 1 && min[i - 1][j] > min[i][j] + (flag[i - 1][j] ^ 1)) {\n min[i - 1][j] = min[i][j] + (flag[i - 1][j] ^ 1);\n q[tail][0] = i - 1;\n q[tail++][1] = j;\n }\n if (i < N && min[i + 1][j] > min[i][j] + (flag[i + 1][j] ^ 1)) {\n min[i + 1][j] = min[i][j] + (flag[i + 1][j] ^ 1);\n q[tail][0] = i + 1;\n q[tail++][1] = j;\n }\n if (j > 1 && min[i][j - 1] > min[i][j] + (flag[i][j - 1] ^ 1)) {\n min[i][j - 1] = min[i][j] + (flag[i][j - 1] ^ 1);\n q[tail][0] = i;\n q[tail++][1] = j - 1;\n }\n if (i < N && min[i][j + 1] > min[i][j] + (flag[i][j + 1] ^ 1)) {\n min[i][j + 1] = min[i][j] + (flag[i][j + 1] ^ 1);\n q[tail][0] = i;\n q[tail++][1] = j + 1;\n }\n }\n }\n\n printf(\"%d\\n\", ans);\n fflush(stdout);\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 buf_it = buf.split_whitespace();\n\n let N = buf_it.next().unwrap().parse::().unwrap();\n let P = (0..N * N)\n .map(|_| buf_it.next().unwrap().parse::().unwrap() - 1)\n .collect::>();\n\n let mut ans = 0;\n let mut cost = (0..N)\n .map(|_| (0..N).map(|_| 0).collect::>())\n .collect::>();\n let mut seated = (0..N)\n .map(|_| (0..N).map(|_| 1).collect::>())\n .collect::>();\n for i in 0..N {\n for j in 0..N {\n let mut tmp = min(i, N - 1 - i);\n tmp = min(tmp, j);\n tmp = min(tmp, N - 1 - j);\n cost[i][j] = tmp;\n }\n }\n\n for p in P {\n let i = p % N;\n let j = p / N;\n ans += cost[i][j];\n seated[i][j] = 0;\n let mut q = vec![(i, j)];\n while !q.is_empty() {\n let mut qq = Vec::new();\n while let Some((i, j)) = q.pop() {\n let c = cost[i][j] + seated[i][j];\n let i = i as isize;\n let j = j as isize;\n for (di, dj) in [(1, 0), (-1, 0), (0, 1), (0, -1)] {\n let i = i + di;\n let j = j + dj;\n if i < 0 || i >= (N as isize) || j < 0 || j >= (N as isize) {\n continue;\n }\n let i = i as usize;\n let j = j as usize;\n if cost[i][j] <= c {\n continue;\n }\n cost[i][j] = c;\n qq.push((i, j));\n }\n }\n q = qq;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1468", "problem_description": "

Reverse Sequence

\n\n

\nWrite a program which reverses a given string str.\n

\n\n

Input

\n\n

\nstr (the size of str ≤ 20) is given in a line.\n

\n\n

Output

\n\n

\nPrint the reversed str in a line.\n

\n\n

Sample Input

\n
\nw32nimda\n
\n\n

Output for the Sample Input

\n\n
\nadmin23w\n
", "c_code": "int solution(void) {\n char word[21];\n scanf(\"%s\", word);\n for (int i = 20; i >= 0; i--) {\n if ((word[i] >= 48 && word[i] <= 57) || (word[i] >= 65 && word[i] <= 90) ||\n (word[i] >= 97 && word[i] <= 122)) {\n printf(\"%c\", word[i]);\n }\n }\n printf(\"\\n\");\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n if let Ok(_) = stdin().read_line(&mut buf) {\n println!(\"{}\", buf.trim().chars().rev().collect::());\n }\n}", "difficulty": "hard"} {"problem_id": "1469", "problem_description": "You are given four distinct integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$. Timur and three other people are running a marathon. The value $$$a$$$ is the distance that Timur has run and $$$b$$$, $$$c$$$, $$$d$$$ correspond to the distances the other three participants ran. Output the number of participants in front of Timur.", "c_code": "int solution() {\n int tc = 0;\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int ahead = 0;\n scanf(\"%d\", &tc);\n while (tc--) {\n scanf(\"%d %d %d %d\", &a, &b, &c, &d);\n if (b > a) {\n ahead++;\n }\n if (c > a) {\n ahead++;\n }\n if (d > a) {\n ahead++;\n }\n printf(\"%d\\n\", ahead);\n ahead = 0;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).expect(\"E\");\n for _ in 0..(input.trim().to_string().parse::().unwrap()) {\n input = String::new();\n io::stdin().read_line(&mut input).expect(\"E\");\n let mut data: Vec = vec![];\n for i in input.trim().to_string().split(\" \") {\n data.push(i.parse::().unwrap());\n }\n let tim: u16 = data[0];\n data.sort();\n println!(\"{}\", 3 - data.iter().position(|&x| x == tim).unwrap());\n }\n}", "difficulty": "hard"} {"problem_id": "1470", "problem_description": "Welcome to Rockport City!It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet $$$a$$$ dollars and Ronnie has bet $$$b$$$ dollars. But the fans seem to be disappointed. The excitement of the fans is given by $$$gcd(a,b)$$$, where $$$gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$. To make the race more exciting, you can perform two types of operations: Increase both $$$a$$$ and $$$b$$$ by $$$1$$$. Decrease both $$$a$$$ and $$$b$$$ by $$$1$$$. This operation can only be performed if both $$$a$$$ and $$$b$$$ are greater than $$$0$$$. In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.Note that $$$gcd(x,0)=x$$$ for any $$$x \\ge 0$$$.", "c_code": "int solution(void) {\n int tc;\n scanf(\"%d\", &tc);\n while (tc-- > 0) {\n long long a;\n long long b;\n scanf(\"%lld %lld\", &a, &b);\n if (a == b) {\n puts(\"0 0\");\n continue;\n }\n if (a > b) {\n long long t = a;\n a = b;\n b = t;\n }\n long long d = b - a;\n long long lo = a / d * d;\n long long hi = lo + d;\n long long moves = hi - a < a - lo ? hi - a : a - lo;\n printf(\"%lld %lld\\n\", d, moves);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n\n let t: i64 = line.trim().parse().unwrap();\n\n for _ in 0..t {\n line.clear();\n stdin().read_line(&mut line).unwrap();\n\n let words: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let a = words[0];\n let b = words[1];\n\n let m = min(a, b);\n let d = i64::abs(a - b);\n\n if d == 0 {\n println!(\"0 0\");\n continue;\n }\n\n println!(\"{} {}\", d, min(m % d, d - m % d));\n }\n}", "difficulty": "medium"} {"problem_id": "1471", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Snuke is giving cookies to his three goats.

\n

He has two cookie tins. One contains A cookies, and the other contains B cookies. He can thus give A cookies, B cookies or A+B cookies to his goats (he cannot open the tins).

\n

Your task is to determine whether Snuke can give cookies to his three goats so that each of them can have the same number of cookies.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq A,B \\leq 100
  • \n
  • Both A and B are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

If it is possible to give cookies so that each of the three goats can have the same number of cookies, print Possible; otherwise, print Impossible.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 5\n
\n
\n
\n
\n
\n

Sample Output 1

Possible\n
\n

If Snuke gives nine cookies, each of the three goats can have three cookies.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 1\n
\n
\n
\n
\n
\n

Sample Output 2

Impossible\n
\n

Since there are only two cookies, the three goats cannot have the same number of cookies no matter what Snuke gives to them.

\n
\n
", "c_code": "int solution() {\n int a[4];\n scanf(\"%d%d\", &a[0], &a[1]);\n if ((a[0] + a[1]) % 3 != 0 && a[0] % 3 != 0 && a[1] % 3 != 0) {\n printf(\"Impossible\");\n } else {\n printf(\"Possible\");\n }\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| i64::from_str(n).unwrap());\n let (a, b) = (it.next().unwrap(), it.next().unwrap());\n\n if a % 3 == 0 {\n println!(\"Possible\");\n return;\n }\n if b % 3 == 0 {\n println!(\"Possible\");\n return;\n }\n if (a + b) % 3 == 0 {\n println!(\"Possible\");\n return;\n }\n println!(\"Impossible\");\n}", "difficulty": "easy"} {"problem_id": "1472", "problem_description": "You have a string $$$s_1 s_2 \\ldots s_n$$$ and you stand on the left of the string looking right. You want to choose an index $$$k$$$ ($$$1 \\le k \\le n$$$) and place a mirror after the $$$k$$$-th letter, so that what you see is $$$s_1 s_2 \\ldots s_k s_k s_{k - 1} \\ldots s_1$$$. What is the lexicographically smallest string you can see?A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n char s[n];\n scanf(\"%s\", s);\n int j = 1;\n while ((s[j] < s[j - 1] && j < n) || (s[j] == s[j - 1] && j > 1)) {\n j++;\n }\n for (int i = 0; i < j; i++) {\n printf(\"%c\", s[i]);\n }\n for (int i = j - 1; i >= 0; i--) {\n printf(\"%c\", s[i]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let stdin = io::stdin();\n let r = stdin.read_line(&mut buf);\n match r {\n Ok(_) => {}\n _ => panic!(\"could not read from stdin\"),\n }\n\n let n: u32 = buf.trim().parse().unwrap();\n\n 'testcases: for _i in 0..n {\n let mut buf = String::new();\n let r = stdin.read_line(&mut buf);\n match r {\n Ok(_) => {}\n _ => panic!(\"could not read from stdin\"),\n }\n let mut buf = String::new();\n let r = stdin.read_line(&mut buf);\n match r {\n Ok(_) => {}\n _ => panic!(\"could not read from stdin\"),\n }\n\n let rb = buf.trim().as_bytes();\n let mut best = String::new();\n\n for i in 0..rb.len() - 1 {\n if rb[i] < rb[i + 1] {\n let new = format!(\n \"{}{}\",\n &buf[0..i + 1],\n buf[0..i + 1].chars().rev().collect::()\n );\n best = if best.is_empty() {\n new\n } else {\n std::cmp::min(best, new)\n };\n\n println!(\"{}\", best);\n continue 'testcases;\n }\n if rb[i] == rb[i + 1] && i == 0 {\n let new = format!(\n \"{}{}\",\n &buf[0..i + 1],\n buf[0..i + 1].chars().rev().collect::()\n );\n\n best = if best.is_empty() {\n new\n } else {\n std::cmp::min(best, new)\n };\n break;\n }\n }\n let new = format!(\n \"{}{}\",\n buf.trim(),\n buf.trim().chars().rev().collect::()\n );\n best = if best.is_empty() {\n new\n } else {\n std::cmp::min(best, new)\n };\n\n println!(\"{}\", best);\n }\n}", "difficulty": "hard"} {"problem_id": "1473", "problem_description": "Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any $$$i$$$ ($$$1 \\le i \\le n-1$$$), this condition must be satisfied: $$$$$$\\frac{\\max(a[i], a[i+1])}{\\min(a[i], a[i+1])} \\le 2$$$$$$For example, the arrays $$$[1, 2, 3, 4, 3]$$$, $$$[1, 1, 1]$$$ and $$$[5, 10]$$$ are dense. And the arrays $$$[5, 11]$$$, $$$[1, 4, 2]$$$, $$$[6, 6, 1]$$$ are not dense.You are given an array $$$a$$$ of $$$n$$$ integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added.For example, if $$$a=[4,2,10,1]$$$, then the answer is $$$5$$$, and the array itself after inserting elements into it may look like this: $$$a=[4,2,\\underline{\\textbf{3}},\\underline{\\textbf{5}},10,\\underline{\\textbf{6}},\\underline{\\textbf{4}},\\underline{\\textbf{2}},1]$$$ (there are other ways to build such $$$a$$$).", "c_code": "int solution() {\n int t;\n int n;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int c = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n }\n for (int j = 0; j < n - 1; j++) {\n if (a[j] > a[j + 1] * 2) {\n int g = a[j + 1];\n while (a[j] > a[j + 1] * 2) {\n a[j + 1] = 2 * a[j + 1];\n c++;\n }\n a[j + 1] = g;\n continue;\n }\n if (a[j + 1] > a[j] * 2) {\n int f = a[j];\n while (a[j + 1] > a[j] * 2) {\n a[j] = 2 * a[j];\n c++;\n }\n a[j] = f;\n continue;\n }\n }\n printf(\"%d\\n\", c);\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 arr: Vec = (0..n).map(|_| sc.next()).collect();\n let mut ans: i32 = 0;\n for i in 1..n {\n let mut a = min(arr[i - 1], arr[i]);\n let b = max(arr[i - 1], arr[i]);\n while 2 * a < b {\n a *= 2;\n ans += 1;\n }\n }\n writeln!(out, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1474", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given a string S consisting of digits between 1 and 9, inclusive.\nYou can insert the letter + into some of the positions (possibly none) between two letters in this string.\nHere, + must not occur consecutively after insertion.

\n

All strings that can be obtained in this way can be evaluated as formulas.

\n

Evaluate all possible formulas, and print the sum of the results.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 10
  • \n
  • All letters in S are digits between 1 and 9, inclusive.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the sum of the evaluated value over all possible formulas.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

125\n
\n
\n
\n
\n
\n

Sample Output 1

176\n
\n

There are 4 formulas that can be obtained: 125, 1+25, 12+5 and 1+2+5. When each formula is evaluated,

\n
    \n
  • 125
  • \n
  • 1+25=26
  • \n
  • 12+5=17
  • \n
  • 1+2+5=8
  • \n
\n

Thus, the sum is 125+26+17+8=176.

\n
\n
\n
\n
\n
\n

Sample Input 2

9999999999\n
\n
\n
\n
\n
\n

Sample Output 2

12656242944\n
\n
\n
", "c_code": "int solution() {\n long S[12];\n int fig = 0;\n long ans = 0;\n int x;\n\n S[0] = getc(stdin) - '0';\n ans += S[0];\n\n for (fig = 1; fig < 10; fig++) {\n S[fig] = getchar() - '0';\n if (S[fig] > 9 || S[fig] < 1) {\n break;\n }\n ans *= 10;\n for (x = 1; x <= fig; x++) {\n ans += S[x - 1] * pow(2, fig - 1);\n }\n ans += S[fig] * pow(2, fig);\n }\n\n printf(\"%ld\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n S: chars,\n }\n let mut ans: u64 = 0;\n\n for bit in 0..1 << (S.len() - 1) {\n let mut acc_str = String::new();\n let mut acc = 0_u64;\n acc_str.push(S[0]);\n for i in 0..S.len() - 1 {\n if bit & 1 << i != 0 {\n acc += acc_str.parse::().unwrap();\n acc_str = String::new();\n }\n acc_str.push(S[i + 1]);\n }\n acc += acc_str.parse::().unwrap();\n ans += acc;\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1475", "problem_description": "Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bi passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty.Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.", "c_code": "int solution() {\n\n int n = 0;\n scanf(\"%d\", &n);\n int pass = 0;\n int capacity = 0;\n\n for (int i = 0; i < n; i++) {\n int exitt = 0;\n int enter = 0;\n scanf(\"%d %d\\n\", &exitt, &enter);\n pass = pass + enter - exitt;\n if (pass > capacity) {\n capacity = pass;\n }\n }\n printf(\"%d\", capacity);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut text = String::new();\n std::io::stdin()\n .read_line(&mut text)\n .expect(\"cannot read line number\");\n let n = text.trim().parse::().expect(\"failed to parse\");\n let mut total = 0;\n let mut max = 0;\n for _ in 0..n {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).expect(\"line\");\n let mut i = line.split_whitespace();\n let a = i.next().unwrap().parse::().expect(\"not a number\");\n let b = i.next().unwrap().parse::().expect(\"not a number\");\n total -= a;\n total += b;\n if total > max {\n max = total;\n }\n }\n println!(\"{}\", max);\n}", "difficulty": "hard"} {"problem_id": "1476", "problem_description": "A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: \"fced\", \"xyz\", \"r\" and \"dabcef\". The following string are not diverse: \"az\", \"aa\", \"bad\" and \"babc\". Note that the letters 'a' and 'z' are not adjacent.Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).You are given a sequence of strings. For each string, if it is diverse, print \"Yes\". Otherwise, print \"No\".", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int letter;\n int sig;\n int occurences[40];\n int correct = 1;\n getc(stdin);\n for (int i = 0; i < n; i++) {\n correct = 1;\n for (int j = 0; j < 40; j++) {\n occurences[j] = 0;\n }\n while ((letter = getc(stdin)) != '\\n' && letter != EOF) {\n sig = letter - 'a';\n\n if (occurences[sig] != 0) {\n correct = 0;\n } else {\n occurences[sig] = 1;\n }\n }\n int beg = 0;\n while (beg < 39 && occurences[beg] == 0) {\n\n beg++;\n }\n while (beg < 39 && occurences[beg] == 1) {\n\n beg++;\n }\n while (beg < 39 && occurences[beg] == 0) {\n\n beg++;\n }\n if (beg != 39) {\n correct = 0;\n }\n if (correct == 1) {\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().read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n input.clear();\n\n for _ in 0..n {\n io::stdin().read_line(&mut input).unwrap();\n\n let mut s: Vec = input.trim().chars().collect();\n\n s.sort();\n\n let mut iter = s.iter();\n\n let mut cur = iter.next().unwrap();\n\n let mut yes = true;\n\n for i in iter {\n if *i as u8 == *cur as u8 + 1 {\n cur = i\n } else {\n yes = false;\n }\n }\n\n if yes {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n\n input.clear();\n }\n}", "difficulty": "medium"} {"problem_id": "1477", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

An adult game master and N children are playing a game on an ice rink.\nThe game consists of K rounds.\nIn the i-th round, the game master announces:

\n
    \n
  • Form groups consisting of A_i children each!
  • \n
\n

Then the children who are still in the game form as many groups of A_i children as possible.\nOne child may belong to at most one group.\nThose who are left without a group leave the game. The others proceed to the next round.\nNote that it's possible that nobody leaves the game in some round.

\n

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

\n

You have heard the values of A_1, A_2, ..., A_K. You don't know N, but you want to estimate it.

\n

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq 10^5
  • \n
  • 2 \\leq A_i \\leq 10^9
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K\nA_1 A_2 ... A_K\n
\n
\n
\n
\n
\n

Output

Print two integers representing the smallest and the largest possible value of N, respectively,\nor a single integer -1 if the described situation is impossible.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n3 4 3 2\n
\n
\n
\n
\n
\n

Sample Output 1

6 8\n
\n

For example, if the game starts with 6 children, then it proceeds as follows:

\n
    \n
  • In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.
  • \n
  • In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.
  • \n
  • In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.
  • \n
  • In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.
  • \n
\n

The last 2 children are declared the winners.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n3 4 100 3 2\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

This situation is impossible.\nIn particular, if the game starts with less than 100 children, everyone leaves after the third round.

\n
\n
\n
\n
\n
\n

Sample Input 3

10\n2 2 2 2 2 2 2 2 2 2\n
\n
\n
\n
\n
\n

Sample Output 3

2 3\n
\n
\n
", "c_code": "int solution(void) {\n long long k;\n long long max = 2;\n long long min = 2;\n scanf(\"%lld\", &k);\n long long a[k];\n for (int i = 0; i < k; i++) {\n scanf(\"%lld\", &a[i]);\n }\n for (int i = k - 1; i >= 0; i--) {\n min = ((min + a[i] - 1) / a[i]) * a[i];\n if (min > (max / a[i]) * a[i]) {\n printf(\"-1\\n\");\n return 0;\n }\n max = ((max / a[i]) * a[i]) + a[i] - 1;\n }\n printf(\"%lld %lld\\n\", min, max);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf: String = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let iter = buf.split_whitespace();\n let mut nums: Vec = Vec::new();\n for i in iter {\n nums.push(i.parse::().unwrap());\n }\n nums.reverse();\n let mut n_max = 2;\n let mut n_min = 2;\n for n in nums {\n if n_max % n != 0 {\n n_max -= n_max % n;\n }\n if n_min % n != 0 {\n n_min += n - n_min % n;\n }\n if n_max < n_min {\n println!(\"-1\");\n return;\n }\n n_max += n - 1;\n }\n println!(\"{} {}\", n_min, n_max);\n}", "difficulty": "medium"} {"problem_id": "1478", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There is a grid with H horizontal rows and W vertical columns.\nLet (i, j) denote the square at the i-th row from the top and the j-th column from the left.

\n

For each i and j (1 \\leq i \\leq H, 1 \\leq j \\leq W), Square (i, j) is described by a character a_{i, j}.\nIf a_{i, j} is ., Square (i, j) is an empty square; if a_{i, j} is #, Square (i, j) is a wall square.\nIt is guaranteed that Squares (1, 1) and (H, W) are empty squares.

\n

Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.

\n

Find the number of Taro's paths from Square (1, 1) to (H, W).\nAs the answer can be extremely large, find the count modulo 10^9 + 7.

\n
\n
\n
\n
\n

Constraints

    \n
  • H and W are integers.
  • \n
  • 2 \\leq H, W \\leq 1000
  • \n
  • a_{i, j} is . or #.
  • \n
  • Squares (1, 1) and (H, W) are empty squares.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\na_{1, 1}\\ldotsa_{1, W}\n:\na_{H, 1}\\ldotsa_{H, W}\n
\n
\n
\n
\n
\n

Output

Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4\n...#\n.#..\n....\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

There are three paths as follows:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 2

5 2\n..\n#.\n..\n.#\n..\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

There may be no paths.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 5\n..#..\n.....\n#...#\n.....\n..#..\n
\n
\n
\n
\n
\n

Sample Output 3

24\n
\n
\n
\n
\n
\n
\n

Sample Input 4

20 20\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n....................\n
\n
\n
\n
\n
\n

Sample Output 4

345263555\n
\n

Be sure to print the count modulo 10^9 + 7.

\n
\n
", "c_code": "int solution() {\n int h;\n int w;\n scanf(\"%d %d\", &h, &w);\n int i;\n int j;\n char a[1003][1003];\n for (i = 0; i < h; i++) {\n scanf(\"%s\", a[i]);\n }\n long long int p = 1e9 + 7;\n long long int dp[1003][1003];\n for (i = 0; i <= h; i++) {\n for (j = 0; j <= w; j++) {\n dp[i][j] = 0;\n }\n }\n dp[1][1] = 1;\n for (i = 0; i < h; i++) {\n for (j = 0; j < w; j++) {\n if (a[i][j] == '.') {\n dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i + 1][j] + dp[i][j + 1]) % p;\n }\n }\n }\n printf(\"%lld\\n\", dp[h][w]);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n h: usize,\n w: usize,\n boards: [chars; h],\n }\n\n let d = 1000000000 + 7;\n let mut dp = vec![vec![0; w + 1]; h + 1];\n\n let mut left;\n let mut top;\n\n for i in 0..h {\n for j in 0..w {\n if j > 0 {\n left = if boards[i][j - 1] == '.' {\n dp[i][j - 1]\n } else {\n 0\n };\n } else {\n left = 0;\n }\n if i > 0 {\n top = if boards[i - 1][j] == '.' {\n dp[i - 1][j]\n } else {\n 0\n };\n } else {\n top = 0;\n }\n if i == 0 && j == 0 {\n dp[i][j] = 1;\n } else {\n dp[i][j] = (top + left) % d;\n }\n }\n }\n\n println!(\"{}\", dp[h - 1][w - 1]);\n}", "difficulty": "easy"} {"problem_id": "1479", "problem_description": "Chouti and his classmates are going to the university soon. To say goodbye to each other, the class has planned a big farewell party in which classmates, teachers and parents sang and danced.Chouti remembered that $$$n$$$ persons took part in that party. To make the party funnier, each person wore one hat among $$$n$$$ kinds of weird hats numbered $$$1, 2, \\ldots n$$$. It is possible that several persons wore hats of the same kind. Some kinds of hats can remain unclaimed by anyone.After the party, the $$$i$$$-th person said that there were $$$a_i$$$ persons wearing a hat differing from his own.It has been some days, so Chouti forgot all about others' hats, but he is curious about that. Let $$$b_i$$$ be the number of hat type the $$$i$$$-th person was wearing, Chouti wants you to find any possible $$$b_1, b_2, \\ldots, b_n$$$ that doesn't contradict with any person's statement. Because some persons might have a poor memory, there could be no solution at all.", "c_code": "int solution() {\n\n int n;\n scanf(\"%d\", &n);\n int i;\n int en[n];\n int con = 1;\n int fre[n + 1];\n int va[n + 1];\n memset(fre, 0, sizeof fre);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &en[i]);\n en[i] = n - en[i];\n fre[en[i]]++;\n }\n for (i = 0; i < n; i++) {\n if (fre[en[i]] % en[i]) {\n break;\n }\n }\n if (i < n) {\n printf(\"Impossible\\n\");\n } else {\n printf(\"Possible\\n\");\n for (i = 0; i < n; i++) {\n if (fre[en[i]] % en[i] == 0) {\n va[en[i]] = con++;\n }\n printf(\"%d\", va[en[i]]);\n fre[en[i]]--;\n if (i < n - 1) {\n printf(\" \");\n } else {\n printf(\"\\n\");\n }\n }\n }\n\n return 0;\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 total: i32 = line.trim().parse().unwrap();\n line.clear();\n let mut recalls: BTreeMap = BTreeMap::new();\n let mut hats = Vec::with_capacity(total as usize);\n let mut hat_type = 0;\n input.read_line(&mut line).unwrap();\n for recall in line.split_whitespace() {\n let recall: i32 = recall.parse().unwrap();\n if let std::collections::btree_map::Entry::Vacant(e) = recalls.entry(recall) {\n hat_type += 1;\n hats.push(hat_type);\n let left_hats = total - recall - 1;\n if left_hats != 0 {\n e.insert((hat_type, left_hats));\n }\n } else {\n let same_hats = recalls.get_mut(&recall).unwrap();\n hats.push(same_hats.0);\n if same_hats.1 == 1 {\n recalls.remove(&recall);\n } else {\n same_hats.1 -= 1;\n }\n }\n }\n if recalls.is_empty() {\n println!(\"Possible\");\n for hat in hats {\n print!(\"{} \", hat);\n }\n } else {\n println!(\"Impossible\");\n }\n}", "difficulty": "hard"} {"problem_id": "1480", "problem_description": "Let $$$\\mathsf{AND}$$$ denote the bitwise AND operation, and $$$\\mathsf{OR}$$$ denote the bitwise OR operation.You are given an array $$$a$$$ of length $$$n$$$ and a non-negative integer $$$k$$$. You can perform at most $$$k$$$ operations on the array of the following type: Select an index $$$i$$$ ($$$1 \\leq i \\leq n$$$) and replace $$$a_i$$$ with $$$a_i$$$ $$$\\mathsf{OR}$$$ $$$2^j$$$ where $$$j$$$ is any integer between $$$0$$$ and $$$30$$$ inclusive. In other words, in an operation you can choose an index $$$i$$$ ($$$1 \\leq i \\leq n$$$) and set the $$$j$$$-th bit of $$$a_i$$$ to $$$1$$$ ($$$0 \\leq j \\leq 30$$$). Output the maximum possible value of $$$a_1$$$ $$$\\mathsf{AND}$$$ $$$a_2$$$ $$$\\mathsf{AND}$$$ $$$\\dots$$$ $$$\\mathsf{AND}$$$ $$$a_n$$$ after performing at most $$$k$$$ operations.", "c_code": "int solution() {\n int test_cases;\n scanf(\"%d\", &test_cases);\n\n int ans[test_cases];\n\n for (int t = 0; t < test_cases; t++) {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n int remaining = k;\n for (int pos = 30; pos >= 0; pos--) {\n\n int guess = n;\n for (int i = 0; i < n; i++) {\n guess -= (a[i] >> pos) & 1;\n }\n\n if (remaining >= guess) {\n remaining -= guess;\n for (int i = 0; i < n; i++) {\n a[i] |= 1 << pos;\n }\n }\n }\n\n unsigned int and = (1 << 31);\n and--;\n for (int i = 0; i < n; i++) {\n and &= a[i];\n }\n ans[t] = and;\n }\n\n for (int i = 0; i < test_cases; i++) {\n printf(\"%d\\n\", ans[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let t = read!(u64);\n for _i in 0..t {\n let (_n, k) = read_pair!(usize);\n let a: Vec = read_vec!(u32);\n\n let mut bits: [u32; 31] = [0; 31];\n for num in a {\n for j in 0..31 {\n let jth_bit = (num >> j) & 1;\n\n if jth_bit == 0 {\n bits[j] += 1\n }\n }\n }\n\n let mut budget = k as u32;\n for j in (0..31).rev() {\n if bits[j] <= budget {\n budget -= bits[j];\n bits[j] = 0;\n }\n }\n\n let mut ans: u32 = 0;\n for j in 0..31 {\n if bits[j] == 0 {\n ans |= 1 << j;\n }\n }\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1481", "problem_description": "\n

Score: 600 points

\n
\n
\n

Problem Statement

\n

Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east.

\n

They start simultaneously at the same point and moves as follows towards the east:

\n
    \n
  • Takahashi runs A_1 meters per minute for the first T_1 minutes, then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.
  • \n
  • Aoki runs B_1 meters per minute for the first T_1 minutes, then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.
  • \n
\n

How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq T_i \\leq 100000
  • \n
  • 1 \\leq A_i \\leq 10^{10}
  • \n
  • 1 \\leq B_i \\leq 10^{10}
  • \n
  • A_1 \\neq B_1
  • \n
  • A_2 \\neq B_2
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
T_1 T_2\nA_1 A_2\nB_1 B_2\n
\n
\n
\n
\n
\n

Output

\n

Print the number of times Takahashi and Aoki will meet each other.
\nIf they meet infinitely many times, print infinity instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 2\n10 10\n12 4\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

They will meet just once, \\frac{4}{3} minutes after they start, at \\frac{40}{3} meters from where they start.

\n
\n
\n
\n
\n
\n

Sample Input 2

100 1\n101 101\n102 1\n
\n
\n
\n
\n
\n

Sample Output 2

infinity\n
\n

They will meet 101, 202, 303, 404, 505, 606, ... minutes after they start, that is, they will meet infinitely many times.

\n
\n
\n
\n
\n
\n

Sample Input 3

12000 15700\n3390000000 3810000000\n5550000000 2130000000\n
\n
\n
\n
\n
\n

Sample Output 3

113\n
\n

The values in input may not fit into a 32-bit integer type.

\n
\n
", "c_code": "int solution() {\n long long int T1;\n long long int T2;\n long long int A1;\n long long int A2;\n long long int B1;\n long long int B2;\n scanf(\"%lld %lld\", &T1, &T2);\n scanf(\"%lld %lld\", &A1, &A2);\n scanf(\"%lld %lld\", &B1, &B2);\n long long int diff = (T1 * (A1 - B1)) + (T2 * (A2 - B2));\n if (diff == 0) {\n printf(\"infinity\\n\");\n } else {\n long long diff2 = T1 * (A1 - B1);\n if ((diff > 0 && diff2 > 0) || (diff < 0 && diff2 < 0)) {\n printf(\"0\\n\");\n } else {\n long long int ct = -diff2 / diff;\n ct *= 2;\n ct++;\n if (diff2 % diff == 0) {\n ct--;\n }\n printf(\"%lld\\n\", ct);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let t = (\n s.next().unwrap().parse::().unwrap(),\n s.next().unwrap().parse::().unwrap(),\n );\n let a = (\n s.next().unwrap().parse::().unwrap(),\n s.next().unwrap().parse::().unwrap(),\n );\n let b = (\n s.next().unwrap().parse::().unwrap(),\n s.next().unwrap().parse::().unwrap(),\n );\n let alen = a.0 * t.0 + a.1 * t.1;\n let blen = b.0 * t.0 + b.1 * t.1;\n if alen == blen {\n println!(\"infinity\");\n } else {\n let (a, b) = if alen < blen { (b, a) } else { (a, b) };\n let ans = if a.0 > b.0 {\n 0\n } else {\n let dif = (alen - blen).abs();\n let r = b.0 - a.0;\n 2 * ((t.0 * r + dif - 1) / dif) + if t.0 * r % dif == 0 { 1 } else { 0 } - 1\n };\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "1482", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Two deer, AtCoDeer and TopCoDeer, are playing a game called Honest or Dishonest.\nIn this game, an honest player always tells the truth, and an dishonest player always tell lies.\nYou are given two characters a and b as the input. Each of them is either H or D, and carries the following information:

\n

If a=H, AtCoDeer is honest; if a=D, AtCoDeer is dishonest.\nIf b=H, AtCoDeer is saying that TopCoDeer is honest; if b=D, AtCoDeer is saying that TopCoDeer is dishonest.

\n

Given this information, determine whether TopCoDeer is honest.

\n
\n
\n
\n
\n

Constraints

    \n
  • a=H or a=D.
  • \n
  • b=H or b=D.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
a b\n
\n
\n
\n
\n
\n

Output

If TopCoDeer is honest, print H. If he is dishonest, print D.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

H H\n
\n
\n
\n
\n
\n

Sample Output 1

H\n
\n

In this input, AtCoDeer is honest. Hence, as he says, TopCoDeer is honest.

\n
\n
\n
\n
\n
\n

Sample Input 2

D H\n
\n
\n
\n
\n
\n

Sample Output 2

D\n
\n

In this input, AtCoDeer is dishonest. Hence, contrary to what he says, TopCoDeer is dishonest.

\n
\n
\n
\n
\n
\n

Sample Input 3

D D\n
\n
\n
\n
\n
\n

Sample Output 3

H\n
\n
\n
", "c_code": "int solution() {\n\n char A = ' ';\n char B = ' ';\n\n scanf(\"%c %c\", &A, &B);\n\n if (A == B) {\n printf(\"H\");\n } else {\n printf(\"D\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let iter = buf.split_whitespace();\n let mut v: Vec = Vec::new();\n for i in iter {\n v.push(i.chars().collect::>()[0]);\n }\n if v[0] == 'H' {\n println!(\"{}\", v[1]);\n } else {\n if v[1] == 'H' {\n println!(\"D\");\n } else {\n println!(\"H\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1483", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

\n

You are given three integers A, B and C.

\n

Determine if there exists an equilateral triangle whose sides have lengths A, B and C.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A,B,C \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

If there exists an equilateral triangle whose sides have lengths A, B and C, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n
    \n
  • There exists an equilateral triangle whose sides have lengths 2, 2 and 2.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 4 5\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
    \n
  • There is no equilateral triangle whose sides have lengths 3, 4 and 5.
  • \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 && b == c) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n\n let mut three_iter = buf.split_whitespace().map(|s| s.parse::().unwrap());\n let first = three_iter.next().unwrap();\n match three_iter.all(|elem| elem == first) {\n true => println!(\"Yes\"),\n false => println!(\"No\"),\n }\n}", "difficulty": "medium"} {"problem_id": "1484", "problem_description": "Polycarp wrote on a whiteboard an array $$$p$$$ of length $$$n$$$, which is a permutation of numbers from $$$1$$$ to $$$n$$$. In other words, in $$$p$$$ each number from $$$1$$$ to $$$n$$$ occurs exactly once.He also prepared a resulting array $$$a$$$, which is initially empty (that is, it has a length of $$$0$$$).After that, he did exactly $$$n$$$ steps. Each step looked like this: Look at the leftmost and rightmost elements of $$$p$$$, and pick the smaller of the two. If you picked the leftmost element of $$$p$$$, append it to the left of $$$a$$$; otherwise, if you picked the rightmost element of $$$p$$$, append it to the right of $$$a$$$. The picked element is erased from $$$p$$$. Note that on the last step, $$$p$$$ has a length of $$$1$$$ and its minimum element is both leftmost and rightmost. In this case, Polycarp can choose what role the minimum element plays. In other words, this element can be added to $$$a$$$ both on the left and on the right (at the discretion of Polycarp).Let's look at an example. Let $$$n=4$$$, $$$p=[3, 1, 4, 2]$$$. Initially $$$a=[]$$$. Then: During the first step, the minimum is on the right (with a value of $$$2$$$), so after this step, $$$p=[3,1,4]$$$ and $$$a=[2]$$$ (he added the value $$$2$$$ to the right). During the second step, the minimum is on the left (with a value of $$$3$$$), so after this step, $$$p=[1,4]$$$ and $$$a=[3,2]$$$ (he added the value $$$3$$$ to the left). During the third step, the minimum is on the left (with a value of $$$1$$$), so after this step, $$$p=[4]$$$ and $$$a=[1,3,2]$$$ (he added the value $$$1$$$ to the left). During the fourth step, the minimum is both left and right (this value is $$$4$$$). Let's say Polycarp chose the right option. After this step, $$$p=[]$$$ and $$$a=[1,3,2,4]$$$ (he added the value $$$4$$$ to the right).Thus, a possible value of $$$a$$$ after $$$n$$$ steps could be $$$a=[1,3,2,4]$$$.You are given the final value of the resulting array $$$a$$$. Find any possible initial value for $$$p$$$ that can result the given $$$a$$$, or determine that there is no solution.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int arr[n + 1];\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n if (arr[1] != n && arr[n] != n) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d \", n);\n for (int i = n; i >= 1; i--) {\n if (arr[i] != n) {\n printf(\"%d \", arr[i]);\n }\n }\n printf(\"\\n\");\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 for l in i.lock().lines().skip(2).step_by(2) {\n let v = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>();\n let x = *v.iter().max().unwrap();\n if v[0] == x || v[v.len() - 1] == x {\n for n in v.iter().rev() {\n write!(o, \"{} \", n).ok();\n }\n writeln!(o).ok();\n } else {\n writeln!(o, \"-1\").ok();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1485", "problem_description": "This is the easy version of the problem. The only difference is the constraints on $$$n$$$ and $$$k$$$. You can make hacks only if all versions of the problem are solved.You have a string $$$s$$$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $$$s:=s+s$$$, where $$$+$$$ denotes concatenation. You can use each operation any number of times (possibly none).Your task is to find the lexicographically smallest string of length exactly $$$k$$$ that can be obtained by doing these operations on string $$$s$$$.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a\\ne b$$$; In the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.", "c_code": "int solution() {\n int n;\n int k;\n int a = 1;\n scanf(\"%d %d\", &n, &k);\n char s[n + 1];\n scanf(\"%s\", s);\n for (int i = 0; i < n; i++) {\n if (s[i] > s[i % a]) {\n break;\n }\n if (s[i] < s[i % a]) {\n a = i + 1;\n }\n }\n for (int i = 0; i < k; i++) {\n putchar(s[i % a]);\n }\n printf(\"\\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\n let n = it.next().unwrap();\n let k = it.next().unwrap();\n\n let s = lines.next().unwrap();\n let a = s.as_bytes();\n\n let i = (1..=n)\n .min_by(|&i, &j| {\n a[..i]\n .iter()\n .cycle()\n .take(k)\n .cmp(a[..j].iter().cycle().take(k))\n })\n .unwrap();\n\n let mut k = k;\n while k > 0 {\n let x = std::cmp::min(i, k);\n output.write_all(&a[..x]).unwrap();\n k -= x;\n }\n\n writeln!(&mut output).unwrap();\n}", "difficulty": "hard"} {"problem_id": "1486", "problem_description": "You had $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$ arranged in a circle. For each pair of neighboring numbers ($$$a_1$$$ and $$$a_2$$$, $$$a_2$$$ and $$$a_3$$$, ..., $$$a_{n - 1}$$$ and $$$a_n$$$, and $$$a_n$$$ and $$$a_1$$$), you wrote down: are the numbers in the pair equal or not.Unfortunately, you've lost a piece of paper with the array $$$a$$$. Moreover, you are afraid that even information about equality of neighboring elements may be inconsistent. So, you are wondering: is there any array $$$a$$$ which is consistent with information you have about equality or non-equality of corresponding pairs?", "c_code": "int solution() {\n int n_case = 0;\n scanf(\"%d\", &n_case);\n char s[52];\n int n_sum = 0;\n for (int ii = 0; ii < n_case; ++ii) {\n scanf(\"%s\", s);\n n_sum = 0;\n for (int i = 0; s[i] != '\\0'; ++i) {\n if (s[i] == 'N') {\n ++n_sum;\n if (n_sum > 1) {\n printf(\"YES\\n\");\n break;\n }\n }\n }\n if (n_sum == 0) {\n printf(\"YES\\n\");\n }\n if (n_sum == 1) {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).expect(\"input error\");\n let mut t: i32 = t.trim().parse().expect(\"input error\");\n while t > 0 {\n let mut input = String::new();\n io::stdin().read_line(&mut input).expect(\"input error\");\n if input.matches('N').count() == 1 {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n }\n t -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "1487", "problem_description": "Berland annual chess tournament is coming!Organizers have gathered 2·n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.Thus, organizers should divide all 2·n players into two teams with n people each in such a way that the first team always wins.Every chess player has its rating ri. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.After teams assignment there will come a drawing to form n pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.Is it possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing?", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int arr[2 * n];\n int i = 0;\n int j = 0;\n for (i = 0; i < 2 * n; i++) {\n scanf(\"%d \", &arr[i]);\n }\n for (i = 0; i < 2 * n; i++) {\n for (j = i + 1; j < 2 * n; j++) {\n if (arr[i] > arr[j]) {\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n }\n }\n }\n for (i = 0; i < n; i++) {\n for (j = n; j < 2 * n; j++) {\n if (arr[i] == arr[j]) {\n printf(\"NO\");\n return 0;\n }\n }\n }\n printf(\"YES\");\n}", "rust_code": "fn solution() {\n let mut reader = BufReader::new(io::stdin());\n let mut res = String::new();\n reader\n .read_to_string(&mut res)\n .expect(\"Could not read stdin\");\n let values: Vec<&str> = res.split_whitespace().collect();\n\n let n: usize = values[0].parse().unwrap();\n let mut players: Vec = values[1..].iter().map(|s| s.parse().unwrap()).collect();\n\n players.sort();\n if players[n - 1] == players[n] {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n }\n}", "difficulty": "medium"} {"problem_id": "1488", "problem_description": "Bucket sort", "c_code": "void solution(int arr[]) {\n const int NARRAY = 8;\n const int NBUCKET = 5;\n const int INTERVAL = 10;\n\n int i, j;\n struct Node **buckets;\n\n buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET);\n\n for (i = 0; i < NBUCKET; ++i) {\n buckets[i] = NULL;\n }\n\n for (i = 0; i < NARRAY; ++i) {\n struct Node *current;\n int pos = arr[i] / INTERVAL;\n\n current = (struct Node *)malloc(sizeof(struct Node));\n current->data = arr[i];\n\n current->next = buckets[pos];\n buckets[pos] = current;\n }\n\n for (i = 0; i < NBUCKET; i++) {\n struct Node *p;\n\n printf(\"Bucket[\\\"%d\\\"] : \", i);\n\n p = buckets[i];\n while (p) {\n printf(\"%d \", p->data);\n p = p->next;\n }\n\n printf(\"\\n\");\n }\n\n for (i = 0; i < NBUCKET; ++i) {\n struct Node *list;\n struct Node *k;\n\n list = buckets[i];\n\n if (list == NULL || list->next == NULL) {\n buckets[i] = list;\n continue;\n }\n\n k = list->next;\n list->next = NULL;\n\n while (k != NULL) {\n struct Node *ptr;\n\n if (list->data > k->data) {\n struct Node *tmp;\n\n tmp = k;\n k = k->next;\n\n tmp->next = list;\n list = tmp;\n\n continue;\n }\n\n for (ptr = list; ptr->next != NULL; ptr = ptr->next) {\n if (ptr->next->data > k->data)\n break;\n }\n\n if (ptr->next != NULL) {\n struct Node *tmp;\n\n tmp = k;\n k = k->next;\n\n tmp->next = ptr->next;\n ptr->next = tmp;\n\n continue;\n } else {\n\n ptr->next = k;\n k = k->next;\n\n ptr->next->next = NULL;\n\n continue;\n }\n }\n\n buckets[i] = list;\n }\n\n printf(\"--------------\\n\");\n printf(\"Buckets after sorted\\n\");\n\n for (i = 0; i < NBUCKET; i++) {\n struct Node *p;\n\n printf(\"Bucket[\\\"%d\\\"] : \", i);\n\n p = buckets[i];\n while (p) {\n printf(\"%d \", p->data);\n p = p->next;\n }\n\n printf(\"\\n\");\n }\n\n for (j = 0, i = 0; i < NBUCKET; ++i) {\n struct Node *node;\n\n node = buckets[i];\n\n while (node) {\n assert(j < NARRAY);\n\n arr[j++] = node->data;\n node = node->next;\n }\n }\n\n for (i = 0; i < NBUCKET; ++i) {\n struct Node *node;\n\n node = buckets[i];\n\n while (node) {\n struct Node *tmp;\n\n tmp = node;\n node = node->next;\n\n free(tmp);\n }\n }\n\n free(buckets);\n}\n", "rust_code": "fn solution(arr: &[usize]) -> Vec {\n if arr.is_empty() {\n return vec![];\n }\n\n let max = *arr.iter().max().unwrap();\n let len = arr.len();\n let mut buckets = vec![vec![]; len + 1];\n\n for x in arr {\n buckets[len * *x / max].push(*x);\n }\n\n for bucket in buckets.iter_mut() {\n super::insertion_sort(bucket);\n }\n\n let mut result = vec![];\n for bucket in buckets {\n for x in bucket {\n result.push(x);\n }\n }\n\n result\n}", "difficulty": "medium"} {"problem_id": "1489", "problem_description": "\n\n\n

Transformation


\n\n

\n Write a program which performs a sequence of commands to a given string $str$. The command is one of:\n

\n\n
    \n
  • print a b: print from the a-th character to the b-th character of $str$
  • \n
  • reverse a b: reverse from the a-th character to the b-th character of $str$
  • \n
  • replace a b p: replace from the a-th character to the b-th character of $str$ with p
  • \n
\n\n

\n Note that the indices of $str$ start with 0.\n

\n\n

Input

\n

\n In the first line, a string $str$ is given. $str$ consists of lowercase letters. In the second line, the number of commands q is given. In the next q lines, each command is given in the above mentioned format.\n

\n\n

Output

\n

\nFor each print command, print a string in a line.\n

\n\n

Constraints

\n\n
    \n
  • $1 \\leq $ length of $str \\leq 1000$
  • \n
  • $1 \\leq q \\leq 100$
  • \n
  • $0 \\leq a \\leq b < $ length of $str$
  • \n
  • for replace command, $b - a + 1 = $ length of $p$
  • \n
\n\n

Sample Input 1

\n
\nabcde\n3\nreplace 1 3 xyz\nreverse 0 2\nprint 1 4\n
\n\n

Sample Output 1

\n
\nxaze\n
\n\n\n

Sample Input 2

\n
\nxyz\n3\nprint 0 2\nreplace 0 2 abc\nprint 0 2\n
\n\n

Sample Output 2

\n
\nxyz\nabc\n
", "c_code": "int solution() {\n char str[1000];\n char operation[10];\n int operation_number = 0;\n int a = 0;\n int b = 0;\n int i = 0;\n char p[1000];\n char tmp;\n scanf(\"%s\", str);\n scanf(\"%d\", &operation_number);\n\n while (operation_number > 0) {\n scanf(\"%s %d %d\", operation, &a, &b);\n if (strcmp(operation, \"replace\") == 0) {\n scanf(\"%s\", p);\n }\n if (strcmp(operation, \"print\") == 0) {\n while (a <= b) {\n printf(\"%c\", str[a]);\n if (a == b) {\n printf(\"\\n\");\n }\n a++;\n }\n } else if (strcmp(operation, \"reverse\") == 0) {\n while (a <= b) {\n tmp = str[b];\n str[b] = str[a];\n str[a] = tmp;\n b--;\n a++;\n }\n } else if (strcmp(operation, \"replace\") == 0) {\n i = 0;\n while (a <= b) {\n str[a] = p[i];\n a++;\n i++;\n }\n }\n operation_number--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n s = s.trim().to_string();\n\n let mut n = String::new();\n io::stdin().read_line(&mut n).unwrap();\n let n: i32 = n.trim().parse().unwrap();\n\n for _ in 0..n {\n let mut t = String::new();\n io::stdin().read_line(&mut t).unwrap();\n let mut iter = t.split_whitespace();\n\n let command = iter.next().unwrap().trim();\n let a: usize = iter.next().unwrap().parse().unwrap();\n let b: usize = iter.next().unwrap().parse().unwrap();\n\n match command {\n \"replace\" => {\n let p = iter.next().unwrap();\n\n let f = s[0..a].to_string();\n let t = s[b + 1..].to_string();\n\n s = f + p;\n s = s + &t;\n }\n \"reverse\" => {\n let f = s[0..a].to_string();\n let m = s[a..b + 1].to_string().chars().rev().collect::();\n let t = s[b + 1..].to_string();\n\n s = f + &m;\n s = s + &t;\n }\n _ => {\n println!(\"{}\", &s[a..b + 1]);\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1490", "problem_description": "You have a string $$$s$$$ consisting of digits from $$$0$$$ to $$$9$$$ inclusive. You can perform the following operation any (possibly zero) number of times: You can choose a position $$$i$$$ and delete a digit $$$d$$$ on the $$$i$$$-th position. Then insert the digit $$$\\min{(d + 1, 9)}$$$ on any position (at the beginning, at the end or in between any two adjacent digits). What is the lexicographically smallest string you can get by performing these operations?A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ of the same length if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a smaller digit than the corresponding digit in $$$b$$$.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n\n char str[200001];\n for (int i = 0; i < t; i++) {\n\n scanf(\"%s\", str);\n int arr[10];\n int ind[10];\n\n for (int j = 0; j < 10; j++) {\n arr[j] = 0;\n ind[j] = -1;\n }\n for (int j = 0; str[j] != '\\0'; j++) {\n arr[str[j] - 48]++;\n ind[str[j] - 48] = j;\n }\n\n int min = -1;\n\n if (ind[0] != -1) {\n min = ind[0];\n int k = ind[0];\n\n while (k >= 0) {\n if (str[k] != '0' && str[k] != '9') {\n arr[str[k] - 48]--;\n arr[str[k] - 47]++;\n }\n k--;\n }\n }\n\n for (int j = 1; j < 9; j++) {\n if (ind[j] != -1) {\n\n int k = ind[j];\n while (k >= 0 && k > min) {\n if (str[k] > (48 + j) && str[k] != '9') {\n\n arr[str[k] - 48]--;\n arr[str[k] - 47]++;\n }\n k--;\n }\n if (ind[j] > min) {\n min = ind[j];\n }\n }\n }\n\n for (int j = 0; j < 10; j++) {\n while (arr[j] != 0) {\n printf(\"%d\", j);\n arr[j]--;\n }\n }\n\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut tests_str = String::new();\n std::io::stdin().read_line(&mut tests_str).unwrap();\n\n for _ in 0..tests_str.trim().parse::().unwrap() {\n let mut s_str = String::new();\n std::io::stdin().read_line(&mut s_str).unwrap();\n\n let mut new_chars = Vec::new();\n let mut min_seen = b'9';\n\n for c in s_str.trim().bytes().rev() {\n if c <= min_seen {\n min_seen = c;\n new_chars.push(c);\n } else {\n new_chars.push((c + 1).min(b'9'));\n }\n }\n\n new_chars.sort_unstable();\n println!(\"{}\", String::from_utf8_lossy(&new_chars));\n }\n}", "difficulty": "medium"} {"problem_id": "1491", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N empty boxes arranged in a row from left to right.\nThe integer i is written on the i-th box from the left (1 \\leq i \\leq N).

\n

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

\n

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

\n
    \n
  • For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.
  • \n
\n

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • a_i is 0 or 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

If a good set of choices does not exist, print -1.

\n

If a good set of choices exists, print one such set of choices in the following format:

\n
M\nb_1 b_2 ... b_M\n
\n

where M denotes the number of boxes that will contain a ball, and b_1,\\ b_2,\\ ...,\\ b_M are the integers written on these boxes, in any order.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 0 0\n
\n
\n
\n
\n
\n

Sample Output 1

1\n1\n
\n

Consider putting a ball only in the box with 1 written on it.

\n
    \n
  • There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.
  • \n
  • There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.
  • \n
  • There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.
  • \n
\n

Thus, the condition is satisfied, so this set of choices is good.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n0 0 0 0 0\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Putting nothing in the boxes can be a good set of choices.

\n
\n
", "c_code": "int solution(void) {\n int N;\n scanf(\"%d\", &N);\n\n int a[N];\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n int ans[N];\n for (int i = 0; i < N; i++) {\n ans[i] = 0;\n }\n\n int ball = 0;\n for (int i = N; i >= 1; i--) {\n if (i == 1) {\n if (ball % 2 != a[i - 1]) {\n ans[i - 1] = 1;\n ball++;\n }\n break;\n }\n int count = 0;\n for (int j = 1;; j++) {\n if (i * j - 1 >= N) {\n break;\n }\n count += ans[(i * j) - 1];\n }\n if (count % 2 != a[i - 1]) {\n ans[i - 1] = 1;\n ball++;\n }\n }\n\n printf(\"%d\\n\", ball);\n for (int i = 0; i < N; i++) {\n if (ans[i] == 1) {\n ball--;\n printf(\"%d\", i + 1);\n if (ball == 0) {\n printf(\"\\n\");\n } else {\n printf(\" \");\n }\n }\n }\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: usize = n.trim().parse().unwrap();\n\n let mut array = String::new();\n stdin().read_line(&mut array).unwrap();\n let array: Vec = array.split_whitespace().flat_map(str::parse).collect();\n let mut boxes = [0; 200002];\n\n let mut result = vec![];\n for (i, &a) in array.iter().enumerate().rev() {\n let mut total = 0;\n let mut j = i + 1;\n while j <= n {\n total += boxes[j];\n j += i + 1;\n }\n if total % 2 != a {\n boxes[i + 1] += 1;\n result.push(format!(\"{}\", i + 1));\n }\n }\n\n println!(\"{}\", result.len());\n println!(\"{}\", result.join(\" \"));\n}", "difficulty": "medium"} {"problem_id": "1492", "problem_description": "You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. Polycarp wants to remove exactly $$$k$$$ characters ($$$k \\le n$$$) from the string $$$s$$$. Polycarp uses the following algorithm $$$k$$$ times: if there is at least one letter 'a', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; if there is at least one letter 'b', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; ... remove the leftmost occurrence of the letter 'z' and stop the algorithm. This algorithm removes a single letter from the string. Polycarp performs this algorithm exactly $$$k$$$ times, thus removing exactly $$$k$$$ characters.Help Polycarp find the resulting string.", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d %d\\n\", &n, &k);\n char str[n + 1];\n scanf(\"%s\", str);\n int count = 0;\n for (int i = 0; i < 26 && count < k; i++) {\n for (int j = 0; j < n; j++) {\n if (str[j] == i + 97 && count < k) {\n str[j] = '.';\n count++;\n } else if (count == k) {\n break;\n }\n }\n }\n for (int i = 0; i < n; i++) {\n if (str[i] != '.') {\n printf(\"%c\", str[i]);\n }\n }\n}", "rust_code": "fn solution() {\n let stdin: io::Stdin = io::stdin();\n let mut lines: io::Lines = stdin.lock().lines();\n\n let next_line = lines.next().unwrap().unwrap();\n\n let flv: Vec<_> = next_line\n .trim()\n .split(' ')\n .map(|n| n.parse::().unwrap())\n .collect();\n\n let k = flv[1];\n\n let mut s: String = lines.next().unwrap().unwrap().trim().to_string();\n\n let abc = \"abcdefghijklmnopqrstuvwxyz\";\n let mut rc = 0;\n let mut v: Vec = vec![];\n\n 'outer: for c in abc.chars() {\n for (i, cc) in s.chars().enumerate() {\n if rc == k {\n break 'outer;\n }\n if c == cc {\n v.push(i);\n rc += 1;\n }\n }\n }\n\n v.sort();\n for i in v.iter().rev() {\n s.remove(*i);\n }\n\n println!(\"{}\", s);\n}", "difficulty": "hard"} {"problem_id": "1493", "problem_description": "Timur has a stairway with $$$n$$$ steps. The $$$i$$$-th step is $$$a_i$$$ meters higher than its predecessor. The first step is $$$a_1$$$ meters higher than the ground, and the ground starts at $$$0$$$ meters. The stairs for the first test case. Timur has $$$q$$$ questions, each denoted by an integer $$$k_1, \\dots, k_q$$$. For each question $$$k_i$$$, you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length $$$k_i$$$. Timur can only climb the $$$j$$$-th step if his legs are of length at least $$$a_j$$$. In other words, $$$k_i \\geq a_j$$$ for each step $$$j$$$ climbed.Note that you should answer each question independently.", "c_code": "int solution() {\n int zushu;\n scanf(\"%d\", &zushu);\n for (int zu = 1; zu <= zushu; zu++) {\n int n;\n int q;\n scanf(\"%d%d\", &n, &q);\n\n if (zu != 1) {\n printf(\"\\n\");\n }\n long long int step[n + 1][2];\n long long int temp;\n step[0][0] = 0;\n step[0][1] = 0;\n for (int i = 1; i <= n; i++) {\n scanf(\"%lld\", &temp);\n step[i][0] = (temp > step[i - 1][0]) ? temp : step[i - 1][0];\n step[i][1] = step[i - 1][1] + temp;\n }\n for (int i = 1; i <= q; i++) {\n if (i != 1) {\n printf(\" \");\n }\n long long int h;\n long long int left = 1;\n long long int right = n;\n scanf(\"%lld\", &h);\n if (step[n][0] <= h) {\n printf(\"%lld\", step[n][1]);\n right = 0;\n } else if (step[1][0] > h) {\n printf(\"0\");\n right = 0;\n }\n while (left < right) {\n long long int mid = (left + right) / 2;\n\n if (step[mid][0] == h) {\n left = mid;\n break;\n }\n if (step[mid][0] < h)\n left = mid + 1;\n else\n right = mid;\n }\n if (right != 0) {\n while (step[left][0] == h) {\n left++;\n }\n printf(\"%lld\", step[left - 1][1]);\n }\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n\n for _ in 0..t {\n let (n, q): (usize, usize) = {\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 )\n };\n\n let a: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let b: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect()\n };\n\n let mut max_height = vec![0; n + 1];\n let mut sum_height = vec![0; n + 1];\n\n for i in 0..n {\n let h = a[i];\n\n max_height[i + 1] = std::cmp::max(max_height[i], h);\n sum_height[i + 1] = sum_height[i] + h;\n }\n\n let mut ans = vec![0; q];\n\n for i in 0..q {\n let qu = b[i];\n\n let mut OK = 0;\n let mut NG = n + 1;\n\n while NG > OK + 1 {\n let mid = (OK + NG) / 2;\n\n if max_height[mid] <= qu {\n OK = mid;\n } else {\n NG = mid;\n }\n }\n\n ans[i] = sum_height[OK];\n }\n\n println!(\n \"{}\",\n ans.iter()\n .map(std::string::ToString::to_string)\n .collect::>()\n .join(\" \")\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1494", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board.

\n

The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns.

\n

How many ways are there to choose where to put the notice so that it completely covers exactly HW squares?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H, W \\leq N \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nH\nW\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n2\n3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

There are two ways to put the notice, as follows:

\n
###   ...\n###   ###\n...   ###\n
\n

Here, # represents a square covered by the notice, and . represents a square not covered.

\n
\n
\n
\n
\n
\n

Sample Input 2

100\n1\n1\n
\n
\n
\n
\n
\n

Sample Output 2

10000\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5\n4\n2\n
\n
\n
\n
\n
\n

Sample Output 3

8\n
\n
\n
", "c_code": "int solution() {\n int w = 0;\n int h = 0;\n int n = 0;\n scanf(\"%d\", &n);\n scanf(\"%d\", &h);\n scanf(\"%d\", &w);\n\n if (h == n && w == n) {\n printf(\"%d\", 1);\n } else if ((n - w) == 0 || (n - h) == 0) {\n printf(\"%d\", ((n - h) + 1) * ((n - w) + 1));\n } else {\n printf(\"%d\", ((n - w) + 1) * ((n - h) + 1));\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let handle = std::io::stdin();\n\n let n: isize = {\n handle.read_line(&mut buf).unwrap();\n let tmp = buf.trim().parse().unwrap();\n buf.clear();\n tmp\n };\n let h: isize = {\n handle.read_line(&mut buf).unwrap();\n let tmp = buf.trim().parse().unwrap();\n buf.clear();\n tmp\n };\n let w: isize = {\n handle.read_line(&mut buf).unwrap();\n let tmp = buf.trim().parse().unwrap();\n buf.clear();\n tmp\n };\n println!(\"{}\", (n - h + 1) * (n - w + 1));\n}", "difficulty": "easy"} {"problem_id": "1495", "problem_description": "\"We've tried solitary confinement, waterboarding and listening to Just In Beaver, to no avail. We need something extreme.\"\"Little Alena got an array as a birthday present...\"The array b of length n is obtained from the array a of length n and two integers l and r (l ≤ r) using the following procedure:b1 = b2 = b3 = b4 = 0.For all 5 ≤ i ≤ n: bi = 0 if ai, ai - 1, ai - 2, ai - 3, ai - 4 > r and bi - 1 = bi - 2 = bi - 3 = bi - 4 = 1 bi = 1 if ai, ai - 1, ai - 2, ai - 3, ai - 4 < l and bi - 1 = bi - 2 = bi - 3 = bi - 4 = 0 bi = bi - 1 otherwise You are given arrays a and b' of the same length. Find two integers l and r (l ≤ r), such that applying the algorithm described above will yield an array b equal to b'.It's guaranteed that the answer exists.", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n char b[n + 1];\n scanf(\"%s\", b);\n\n int lmin = -1000000000;\n int rmax = 1000000000;\n for (int i = 4; i < n; i++) {\n bool same = true;\n for (int j = 1; j < 4; j++) {\n if (b[i - j] != b[i - j - 1]) {\n same = false;\n break;\n }\n }\n\n if (!same || b[i] == b[i - 1]) {\n continue;\n }\n\n if (b[i] == '0') {\n int min = a[i];\n for (int j = 1; j < 5; j++) {\n if (a[i - j] < min) {\n min = a[i - j];\n }\n }\n\n if (rmax > min) {\n rmax = min - 1;\n }\n } else {\n int max = a[i];\n for (int j = 1; j < 5; j++) {\n if (a[i - j] > max) {\n max = a[i - j];\n }\n }\n\n if (lmin < max) {\n lmin = max + 1;\n }\n }\n }\n\n printf(\"%d %d\\n\", lmin, rmax);\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io;\n use std::io::prelude::*;\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n\n let mut a = Vec::with_capacity(n);\n for _ in 0..n {\n let x: i32 = it.next().unwrap().parse().unwrap();\n a.push(x);\n }\n\n let mut bp = Vec::with_capacity(n);\n for b in it.next().unwrap().bytes() {\n let x = match b {\n b'0' => false,\n b'1' => true,\n _ => unreachable!(),\n };\n bp.push(x);\n }\n\n let mut cl = Vec::new();\n\n let mut it = bp.iter();\n let mut cur = *it.next().unwrap();\n let mut cnt = 1;\n\n for (i, &b) in it.enumerate() {\n if cnt >= 4 {\n cl.push(i + 1);\n }\n\n if cur != b {\n cur = b;\n cnt = 0;\n } else {\n cnt += 1;\n }\n }\n\n let mut l = -1_000_000_000;\n let mut r = 1_000_000_000;\n\n for i in cl {\n let x = bp[i];\n if x {\n if bp[i - 1] != x {\n let y = *a[i - 4..i + 1].iter().max().unwrap() + 1;\n if l < y {\n l = y;\n }\n } else {\n let y = *a[i - 4..i + 1].iter().min().unwrap();\n if r < y {\n r = y;\n }\n }\n } else {\n if bp[i - 1] != x {\n let y = *a[i - 4..i + 1].iter().min().unwrap() - 1;\n if y < r {\n r = y;\n }\n } else {\n let y = *a[i - 4..i + 1].iter().max().unwrap();\n if y < l {\n l = y;\n }\n }\n }\n }\n\n println!(\"{} {}\", l, r);\n}", "difficulty": "medium"} {"problem_id": "1496", "problem_description": "

A + B Problem

\n\n\n\n\n

\nCompute A + B. \n

\n\n

Input

\n\n

\nThe input will consist of a series of pairs of integers A and B separated by a space, one pair of integers per line. The input will be terminated by EOF.\n

\n\n

Output

\n\n

\nFor each pair of input integers A and B, you must output the sum of A and B in one line.\n

\n\n

Constraints

\n
    \n
  • -1000 ≤ A, B ≤ 1000
  • \n
\n\n

Sample Input

\n
\n1 2\n10 5\n100 20\n
\n\n

Output for the Sample Input

\n
\n3\n15\n120\n
\n\n\n

Sample Program

\n\n\n\n\n\n\n\n
\n
\n#include<stdio.h>\n\nint main(){\n    int a, b;\n    while( scanf(\"%d %d\", &a, &b) != EOF ){\n        printf(\"%d\\n\", a + b);\n    }\n    return 0;\n}\n
\n
\n\n
", "c_code": "int solution() {\n\n int a = 0;\n int b = 0;\n\n while (scanf(\"%d %d\", &a, &b) != EOF) {\n printf(\"%d\\n\", a + b);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input: String;\n loop {\n input = String::new();\n match std::io::stdin().read_line(&mut input) {\n Ok(0) => {\n break;\n }\n Ok(_) => {\n println!(\n \"{}\",\n input\n .split_whitespace()\n .fold(0, |x, y| x + y.parse::().unwrap())\n );\n }\n Err(_) => {}\n };\n }\n}", "difficulty": "medium"} {"problem_id": "1497", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

\n

N friends of Takahashi has come to a theme park.

\n

To ride the most popular roller coaster in the park, you must be at least K centimeters tall.

\n

The i-th friend is h_i centimeters tall.

\n

How many of the Takahashi's friends can ride the roller coaster?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\le N \\le 10^5
  • \n
  • 1 \\le K \\le 500
  • \n
  • 1 \\le h_i \\le 500
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N K\nh_1 h_2 \\ldots h_N\n
\n
\n
\n
\n
\n

Output

\n

Print the number of people among the Takahashi's friends who can ride the roller coaster.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 150\n150 140 100 200\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Two of them can ride the roller coaster: the first and fourth friends.

\n
\n
\n
\n
\n
\n

Sample Input 2

1 500\n499\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5 1\n100 200 300 400 500\n
\n
\n
\n
\n
\n

Sample Output 3

5\n
\n
\n
", "c_code": "int solution() {\n int i = 0;\n int n = 0;\n int k = 0;\n int h[10000000];\n int result = 0;\n\n scanf(\"%d %d\", &n, &k);\n\n while (i < n) {\n\n if (i < n - 1) {\n scanf(\"%d \", &h[i]);\n } else {\n scanf(\"%d\", &h[i]);\n }\n if (h[i] >= 1 && h[i] <= 500) {\n if (h[i] >= k) {\n result++;\n } else {\n }\n }\n i++;\n }\n\n printf(\"%d\\n\", result);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let mut iter = buf.split_whitespace();\n let n = match iter.next().unwrap().parse::() {\n Ok(m) => m,\n _ => panic!(\"\"),\n };\n let k = match iter.next().unwrap().parse::() {\n Ok(m) => m,\n _ => panic!(\"\"),\n };\n\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let mut iter = buf.split_whitespace();\n\n let mut cnt = 0;\n for _a in 0..n {\n let hi = match iter.next().unwrap().parse::() {\n Ok(m) => m,\n _ => panic!(\"\"),\n };\n if hi >= k {\n cnt += 1;\n }\n }\n println!(\"{}\", cnt);\n}", "difficulty": "medium"} {"problem_id": "1498", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

\n

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

\n
    \n
  • Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.
  • \n
\n

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • -10^4 \\leq A_i \\leq 10^4
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

\n

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

\n

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

\n
M\nx_1 y_1\n:\nx_{N-1} y_{N-1}\n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 -1 2\n
\n
\n
\n
\n
\n

Sample Output 1

4\n-1 1\n2 -2\n
\n

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

\n

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

\n

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 1 1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n1 1\n1 0\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n scanf(\"%ld\", &n);\n long a[n];\n for (long i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n }\n long plus = 0;\n long minus = 0;\n long sum = 0;\n long abs;\n long min = -1;\n long min_i;\n for (long i = 0; i < n; i++) {\n if (a[i] >= 0) {\n plus++;\n abs = a[i];\n } else {\n minus++;\n abs = -a[i];\n }\n sum += abs;\n if (min == -1 || abs < min) {\n min = abs;\n min_i = i;\n }\n }\n if (plus == 0 || minus == 0) {\n sum -= min * 2;\n }\n printf(\"%ld\\n\", sum);\n if (plus == 0) {\n for (long i = 0; i < n; i++) {\n if (i == min_i) {\n continue;\n }\n printf(\"%ld %ld\\n\", a[min_i], a[i]);\n a[min_i] -= a[i];\n }\n } else if (minus == 0) {\n long other;\n for (long i = 0; i < n; i++) {\n if (i != min_i) {\n other = i;\n break;\n }\n }\n for (long i = 0; i < n; i++) {\n if (i != min_i && i != other) {\n printf(\"%ld %ld\\n\", a[min_i], a[i]);\n a[min_i] -= a[i];\n }\n }\n printf(\"%ld %ld\\n\", a[other], a[min_i]);\n } else {\n long p_1;\n long m_1;\n for (long i = 0; i < n; i++) {\n if (a[i] >= 0) {\n p_1 = i;\n break;\n }\n }\n for (long i = 0; i < n; i++) {\n if (a[i] < 0) {\n m_1 = i;\n break;\n }\n }\n for (long i = 0; i < n; i++) {\n if (i != p_1 && i != m_1) {\n if (a[i] >= 0) {\n printf(\"%ld %ld\\n\", a[m_1], a[i]);\n a[m_1] -= a[i];\n } else {\n printf(\"%ld %ld\\n\", a[p_1], a[i]);\n a[p_1] -= a[i];\n }\n }\n }\n printf(\"%ld %ld\\n\", a[p_1], a[m_1]);\n }\n\n return 0;\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\n let n = get!(usize);\n let mut vs = vec![];\n for _ in 0..n {\n let v = get!();\n vs.push(v);\n }\n let mut op = vec![];\n vs.sort();\n\n for i in 1..n - 1 {\n if vs[i] < 0 {\n continue;\n }\n op.push((vs[0], vs[i]));\n vs[0] -= vs[i];\n }\n for i in 1..n - 1 {\n if vs[i] >= 0 {\n break;\n }\n op.push((vs[n - 1], vs[i]));\n vs[n - 1] -= vs[i];\n }\n op.push((vs[n - 1], vs[0]));\n vs[n - 1] -= vs[0];\n\n println!(\"{}\", vs[n - 1]);\n for &(u, v) in op.iter() {\n println!(\"{} {}\", u, v);\n }\n}", "difficulty": "hard"} {"problem_id": "1499", "problem_description": "Nastia has received an array of $$$n$$$ positive integers as a gift.She calls such an array $$$a$$$ good that for all $$$i$$$ ($$$2 \\le i \\le n$$$) takes place $$$gcd(a_{i - 1}, a_{i}) = 1$$$, where $$$gcd(u, v)$$$ denotes the greatest common divisor (GCD) of integers $$$u$$$ and $$$v$$$.You can perform the operation: select two different indices $$$i, j$$$ ($$$1 \\le i, j \\le n$$$, $$$i \\neq j$$$) and two integers $$$x, y$$$ ($$$1 \\le x, y \\le 2 \\cdot 10^9$$$) so that $$$\\min{(a_i, a_j)} = \\min{(x, y)}$$$. Then change $$$a_i$$$ to $$$x$$$ and $$$a_j$$$ to $$$y$$$.The girl asks you to make the array good using at most $$$n$$$ operations.It can be proven that this is always possible.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d \", &a[i]);\n }\n printf(\"%d\\n\", n / 2);\n for (int i = 0; i < n - 1; i += 2) {\n printf(\"%d %d %d %d\\n\", i + 1, i + 2, a[i] < a[i + 1] ? a[i] : a[i + 1],\n 1000000007);\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(|x| x.unwrap());\n\n let t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let n = lines.next().unwrap().parse::().unwrap();\n\n let s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let (i, x) = it.enumerate().min_by_key(|(_, x)| *x).unwrap();\n\n let k = if i > 0 { n } else { n - 1 };\n writeln!(&mut output, \"{}\", k).unwrap();\n if i > 0 {\n writeln!(&mut output, \"{} {} {} {}\", 1, i + 1, x, x).unwrap();\n }\n\n for i in 1..n {\n let y = if i % 2 == 0 { x } else { x + 1 };\n writeln!(&mut output, \"{} {} {} {}\", 1, i + 1, x, y).unwrap();\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1500", "problem_description": "

BMI

\n\n

\n肥満は多くの成人病の原因として挙げられています。過去においては、一部の例外を除けば、高校生には無縁なものでした。しかし、過度の受験勉強等のために運動不足となり、あるいはストレスによる過食症となることが、非現実的なこととはいえなくなっています。高校生にとっても十分関心を持たねばならない問題になるかもしれません。\n

\n\n

\nそこで、あなたは、保健室の先生の助手となって、生徒のデータから肥満の疑いのある生徒を探し出すプログラムを作成することになりました。\n

\n\n

\n方法は BMI (Body Mass Index) という数値を算出する方法です。BMIは次の式で与えられます。\n

\n\n
\n\n
\n
\n\n

\nBMI = 22 が標準的で、25 以上だと肥満の疑いがあります。\n

\n\n

\n各々の生徒の体重、身長の情報から BMI を算出し、25 以上の生徒の学籍番号を出力するプログラムを作成してください。\n

\n\n\n

入力

\n\n

\n入力は以下の形式で与えられます。\n

\n\n
\ns1,w1,h1\ns2,w2,h2\n...\n...\n
\n\n

\nsi (1 ≤ si ≤ 2,000)、wi (1 ≤ wi ≤ 200)、hi (1.0 ≤ hi ≤ 2.0) はそれぞれ、i 番目の生徒の学籍番号(整数)、体重(実数)、身長(実数)を表します。\n

\n\n

\n生徒の数は 50 を超えません。\n

\n\n\n

出力

\n\n

\nBMI が 25 以上の生徒の学籍番号を、入力された順番にそれぞれ1行に出力します。\n

\n\n

Sample Input

\n\n
\n1001,50.0,1.60 \n1002,60.0,1.70 \n1003,70.0,1.80 \n1004,80.0,1.70 \n1005,90.0,1.60 \n
\n\n\n\n

Output for the Sample Input

\n\n
\n1004\n1005 \n
", "c_code": "int solution(void) {\n int num[50];\n int c = 0;\n double h[50];\n double w[50];\n\n while (scanf(\"%d,%lf,%lf\", &num[c], &w[c], &h[c]) != EOF) {\n if ((w[c] / (h[c] * h[c])) >= 25) {\n printf(\"%d\\n\", num[c]);\n }\n c++;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut input = String::new();\n match std::io::stdin().read_line(&mut input).unwrap() {\n 0 => break,\n _ => {\n let v: Vec<&str> = input.trim().split(',').collect();\n let (w, h): (f64, f64) = (v[1].parse().unwrap(), v[2].parse().unwrap());\n if w / (h * h) >= 25.0 {\n println!(\"{}\", v[0])\n }\n }\n };\n }\n}", "difficulty": "easy"} {"problem_id": "1501", "problem_description": "You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?", "c_code": "int solution() {\n int cases;\n scanf(\"%d\", &cases);\n\n while (cases--) {\n int arrLen;\n scanf(\"%d\", &arrLen);\n\n int array[arrLen];\n for (int index = 0; index < arrLen; index++) {\n scanf(\"%d \", array + index);\n }\n\n if (arrLen % 2 == 0) {\n for (int index = 0; index < arrLen; index += 2) {\n if (array[index] > array[index + 1]) {\n int backup = array[index];\n array[index] = array[index + 1];\n array[index + 1] = backup;\n }\n }\n } else {\n for (int index = 1; index < arrLen; index += 2) {\n if (array[index] > array[index + 1]) {\n int backup = array[index];\n array[index] = array[index + 1];\n array[index + 1] = backup;\n }\n }\n }\n\n bool result = true;\n for (int index = 0; index < arrLen - 1; index++) {\n if (array[index] > array[index + 1]) {\n result = false;\n break;\n }\n }\n if (result) {\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 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 n = input.next().unwrap();\n let a: Vec<_> = input.by_ref().take(n).map(|x| x as u32).collect();\n let mut is_sorted = true;\n let mut max = 1_000_000;\n for chunk in a.rchunks(2) {\n match *chunk {\n [a] => {\n if a > max {\n is_sorted = false;\n break;\n } else {\n max = a;\n }\n }\n [a, b] => {\n if a > b {\n if a > max {\n is_sorted = false;\n break;\n }\n max = b;\n } else {\n if b > max {\n is_sorted = false;\n break;\n }\n max = a;\n }\n }\n _ => {}\n }\n }\n if is_sorted {\n output.push_str(\"YES\\n\");\n } else {\n output.push_str(\"NO\\n\");\n }\n }\n print!(\"{output}\");\n}", "difficulty": "medium"} {"problem_id": "1502", "problem_description": "

Factorial

\n\n

\nWrite a program which reads an integer n and prints the factorial of n. You can assume that n ≤ 20.\n

\n\n

Input

\n\n

\nAn integer n (1 ≤ n ≤ 20) in a line.\n

\n\n

Output

\n\n

\nPrint the factorial of n in a line.\n

\n\n

Sample Input

\n\n
\n5\n
\n\n

Output for the Sample Input

\n\n
\n120\n
", "c_code": "int solution(void) {\n long long int f = 1;\n int a = 0;\n scanf(\"%d\", &a);\n while (a) {\n f *= a;\n a--;\n }\n printf(\"%lld\\n\", f);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n let n = buf.trim().parse::().unwrap();\n let mut ans = 1;\n for i in 2..n + 1 {\n ans *= i;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1503", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

\n

There are N stations on the railroad, numbered 1 through N from west to east.

\n

Tomorrow, the opening ceremony of the railroad will take place.

\n

On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.

\n

The first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.

\n

Here, it is guaranteed that F_i divides S_i.

\n

That is, for each Time t satisfying S_i≤t and t%F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A%B denotes A modulo B, and there will be no other trains.

\n

For each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤500
  • \n
  • 1≤C_i≤100
  • \n
  • 1≤S_i≤10^5
  • \n
  • 1≤F_i≤10
  • \n
  • S_i%F_i=0
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nC_1 S_1 F_1\n:\nC_{N-1} S_{N-1} F_{N-1}\n
\n
\n
\n
\n
\n

Output

Print N lines. Assuming that we are at Station i (1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n6 5 1\n1 10 1\n
\n
\n
\n
\n
\n

Sample Output 1

12\n11\n0\n
\n

We will travel from Station 1 as follows:

\n
    \n
  • 5 seconds after the beginning: take the train to Station 2.
  • \n
  • 11 seconds: arrive at Station 2.
  • \n
  • 11 seconds: take the train to Station 3.
  • \n
  • 12 seconds: arrive at Station 3.
  • \n
\n

We will travel from Station 2 as follows:

\n
    \n
  • 10 seconds: take the train to Station 3.
  • \n
  • 11 seconds: arrive at Station 3.
  • \n
\n

Note that we should print 0 for Station 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n12 24 6\n52 16 4\n99 2 2\n
\n
\n
\n
\n
\n

Sample Output 2

187\n167\n101\n0\n
\n
\n
\n
\n
\n
\n

Sample Input 3

4\n12 13 1\n44 17 17\n66 4096 64\n
\n
\n
\n
\n
\n

Sample Output 3

4162\n4162\n4162\n0\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n\n int c[n - 1];\n int s[n - 1];\n int f[n - 1];\n\n for (int i = 0; i < n - 1; i++) {\n scanf(\"%d %d %d\", &c[i], &s[i], &f[i]);\n }\n\n for (int i = 0; i < n - 1; i++) {\n int t = 0;\n for (int j = i; j < n - 1; j++) {\n t = (((t - 1) / f[j] + 1) * f[j] < s[j]) ? s[j]\n : ((t - 1) / f[j] + 1) * f[j];\n t += c[j];\n }\n\n printf(\"%d\\n\", t);\n }\n\n printf(\"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).ok();\n let mut it = buf.split_whitespace();\n let n = it.next().unwrap().parse::().unwrap() - 1;\n let mut t = Vec::new();\n for _ in 0..n {\n let c = it.next().unwrap().parse::().unwrap();\n let s = it.next().unwrap().parse::().unwrap();\n let f = it.next().unwrap().parse::().unwrap();\n t.push((c, s, f));\n }\n for i in 0..n {\n let mut cur = 0;\n for j in i..n {\n cur = std::cmp::max(t[j].1, cur + t[j].2 - 1) / t[j].2 * t[j].2 + t[j].0;\n }\n println!(\"{}\", cur);\n }\n println!(\"0\");\n}", "difficulty": "medium"} {"problem_id": "1504", "problem_description": "

Sum of Numbers


\n\n

\n Write a program which reads an integer and prints sum of its digits.\n

\n\n\n

Input

\n\n

\n The input consists of multiple datasets. For each dataset, an integer x is given in a line. The number of digits in x does not exceed 1000.\n

\n\n

\nThe input ends with a line including single zero. Your program should not process for this terminal symbol.\n

\n\n

Output

\n\n

\n For each dataset, print the sum of digits in x.\n

\n\n

Sample Input

\n\n
\n123\n55\n1000\n0\n
\n\n

Sample Output

\n\n
\n6\n10\n1\n
", "c_code": "int solution(void) {\n char str[1001] = {0};\n int i = 0;\n int sum = 0;\n\n while (1) {\n scanf(\"%c\", &str[i]);\n\n if (str[i] == '\\n') {\n if (i == 1 && str[0] == '0') {\n break;\n }\n printf(\"%d\\n\", sum);\n sum = 0;\n i = 0;\n\n } else {\n sum += str[i] - 0x30;\n i++;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let mut b = String::new();\n let s = std::io::stdin();\n s.read_line(&mut b).unwrap();\n if b.trim() == \"0\" {\n break;\n }\n println!(\n \"{}\",\n b.trim()\n .chars()\n .map(|c| c.to_digit(10).unwrap())\n .sum::()\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1505", "problem_description": "You are given two integers $$$n$$$ and $$$k$$$.You should create an array of $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$ such that the sum $$$(a_1 + a_2 + \\dots + a_n)$$$ is divisible by $$$k$$$ and maximum element in $$$a$$$ is minimum possible.What is the minimum possible maximum element in $$$a$$$?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n long long output[t];\n for (int i = 0; i < t; i++) {\n long long n;\n long long k;\n scanf(\"%lld %lld\", &n, &k);\n long long mult = k;\n while (mult / n == 0) {\n if (n % k) {\n mult = (n / k) * k + k;\n } else {\n mult = (n / k) * k;\n }\n }\n if (mult % n == 0) {\n output[i] = mult / n;\n } else {\n output[i] = mult / n + 1;\n }\n }\n for (int k = 0; k < t; k++) {\n printf(\"%lld\\n\", output[k]);\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut input = stdin.lock();\n let mut buf = String::new();\n\n input.read_line(&mut buf).unwrap();\n let t = buf.trim().parse().unwrap();\n\n for _ in 0..t {\n buf.clear();\n input.read_line(&mut buf).unwrap();\n let mut words = buf.trim().split_ascii_whitespace();\n let n: usize = words.next().unwrap().parse().unwrap();\n let k: usize = words.next().unwrap().parse().unwrap();\n\n let mut target = (n / k) * k;\n if target < n {\n target += k;\n }\n\n let ans = if target.is_multiple_of(n) {\n target / n\n } else {\n target / n + 1\n };\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1506", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).\nEach a_i is a positive integer.

\n

Snuke's objective is to permute the element in a so that the following condition is satisfied:

\n
    \n
  • For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.
  • \n
\n

Determine whether Snuke can achieve his objective.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ N ≤ 10^5
  • \n
  • a_i is an integer.
  • \n
  • 1 ≤ a_i ≤ 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 10 100\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

One solution is (1, 100, 10).

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

It is impossible to permute a so that the condition is satisfied.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n1 4 1\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n

The condition is already satisfied initially.

\n
\n
\n
\n
\n
\n

Sample Input 4

2\n1 1\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
\n
\n
\n
\n

Sample Input 5

6\n2 7 1 8 2 8\n
\n
\n
\n
\n
\n

Sample Output 5

Yes\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n long long s[150000] = {0};\n int s4 = 0;\n int s2 = 0;\n int i = 0;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &s[i]);\n }\n for (i = 0; i < n; i++) {\n if (s[i] % 4 == 0) {\n s4++;\n } else if (s[i] % 2 == 0) {\n s2++;\n }\n }\n if (s4 * 2 + s2 == n || s4 >= n / 2) {\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::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let a: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n let mut x: isize = 0;\n let mut y: isize = 0;\n let mut z: isize = 0;\n\n for i in &a {\n if *i % 4 == 0 {\n x += 1;\n } else if *i % 2 == 0 {\n y += 1;\n } else {\n z += 1;\n }\n }\n\n if y == 0 {\n z -= 1;\n }\n println!(\"{}\", if z <= x { \"Yes\" } else { \"No\" });\n}", "difficulty": "medium"} {"problem_id": "1507", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Snuke has decided to play with a six-sided die. Each of its six sides shows an integer 1 through 6, and two numbers on opposite sides always add up to 7.

\n

Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:

\n
    \n
  • Operation: Rotate the die 90° toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain y points where y is the number written in the side facing upward.
  • \n
\n

For example, let us consider the situation where the side showing 1 faces upward, the near side shows 5 and the right side shows 4, as illustrated in the figure.\nIf the die is rotated toward the right as shown in the figure, the side showing 3 will face upward.\nBesides, the side showing 4 will face upward if the die is rotated toward the left, the side showing 2 will face upward if the die is rotated toward the front, and the side showing 5 will face upward if the die is rotated toward the back.

\n
\n\"864abc2e4a08c26015ffd007a30aab03.png\"\n
\n

Find the minimum number of operation Snuke needs to perform in order to score at least x points in total.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ x ≦ 10^{15}
  • \n
  • x is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
x\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n
\n
\n
\n
\n
\n

Sample Input 2

149696127901\n
\n
\n
\n
\n
\n

Sample Output 2

27217477801\n
\n
\n
", "c_code": "int solution(void) {\n long long int x;\n scanf(\"%lld\", &x);\n if (1 <= x && x <= 6) {\n printf(\"1\");\n } else if (x <= 10) {\n printf(\"2\");\n } else if (x % 11 == 0) {\n printf(\"%lld\", 2 * x / 11);\n } else if (1 <= x % 11 && x % 11 <= 6) {\n printf(\"%lld\", (2 * (x / 11)) + 1);\n } else if (7 <= x % 11 && x % 11 <= 10) {\n printf(\"%lld\", (2 * (x / 11)) + 2);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n\n let x: u64 = s.trim().parse().unwrap();\n\n let r = x / 11;\n let m = x % 11;\n\n println!(\n \"{}\",\n if m == 0 {\n r * 2\n } else if m > 6 {\n r * 2 + 2\n } else {\n r * 2 + 1\n }\n );\n}", "difficulty": "easy"} {"problem_id": "1508", "problem_description": "A rectangle with its opposite corners in $$$(0, 0)$$$ and $$$(w, h)$$$ and sides parallel to the axes is drawn on a plane.You are given a list of lattice points such that each point lies on a side of a rectangle but not in its corner. Also, there are at least two points on every side of a rectangle.Your task is to choose three points in such a way that: exactly two of them belong to the same side of a rectangle; the area of a triangle formed by them is maximum possible. Print the doubled area of this triangle. It can be shown that the doubled area of any triangle formed by lattice points is always an integer.", "c_code": "int solution() {\n int n_case = 0;\n scanf(\"%d\", &n_case);\n long long w = 0;\n long long h = 0;\n long long w_up[2 * 100000];\n long long k = 0;\n long long min = 0;\n long long max = 0;\n long long a[4];\n long long final = 0;\n for (int ii = 0; ii < n_case; ++ii) {\n scanf(\"%lld\", &w);\n scanf(\"%lld\", &h);\n scanf(\"%lld\", &k);\n int i = 0;\n for (i = 0; i < k; i++) {\n scanf(\"%lld\", &w_up[i]);\n }\n\n min = w_up[0];\n max = w_up[k - 1];\n a[0] = (max - min) * h;\n\n for (i = 0; i < k; i++) {\n w_up[i] = 0;\n }\n\n scanf(\"%lld\", &k);\n for (i = 0; i < k; i++) {\n scanf(\"%lld\", &w_up[i]);\n }\n\n min = w_up[0];\n max = w_up[k - 1];\n a[1] = (max - min) * h;\n\n for (i = 0; i < k; i++) {\n w_up[i] = 0;\n }\n\n scanf(\"%lld\", &k);\n for (i = 0; i < k; i++) {\n scanf(\"%lld\", &w_up[i]);\n }\n\n min = w_up[0];\n max = w_up[k - 1];\n a[2] = (max - min) * w;\n\n for (i = 0; i < k; i++) {\n w_up[i] = 0;\n }\n\n scanf(\"%lld\", &k);\n for (i = 0; i < k; i++) {\n scanf(\"%lld\", &w_up[i]);\n }\n\n min = w_up[0];\n max = w_up[k - 1];\n a[3] = (max - min) * w;\n\n final = a[0];\n for (i = 1; i < 4; ++i) {\n if (a[i] > final) {\n final = a[i];\n }\n }\n printf(\"%lld\\n\", final);\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let stdin = io::stdin();\n let r = stdin.read_line(&mut buf);\n match r {\n Ok(_) => {}\n _ => panic!(\"could not read from stdin\"),\n }\n\n let n: u128 = buf.trim().parse().unwrap();\n\n for _i in 0..n {\n let mut buf = String::new();\n let r = stdin.read_line(&mut buf);\n match r {\n Ok(_) => {}\n _ => panic!(\"could not read from stdin\"),\n }\n let p: Vec<&str> = buf.trim().split(\" \").collect();\n\n let w: u128 = p[0].parse().unwrap();\n let h: u128 = p[1].parse().unwrap();\n\n let mut buf = String::new();\n let r = stdin.read_line(&mut buf);\n match r {\n Ok(_) => {}\n _ => panic!(\"could not read from stdin\"),\n }\n let line1: Vec = buf\n .trim()\n .split(\" \")\n .skip(1)\n .map(|s| s.parse::().unwrap())\n .collect();\n\n let mut buf = String::new();\n let r = stdin.read_line(&mut buf);\n match r {\n Ok(_) => {}\n _ => panic!(\"could not read from stdin\"),\n }\n let line2: Vec = buf\n .trim()\n .split(\" \")\n .skip(1)\n .map(|s| s.parse::().unwrap())\n .collect();\n\n let mut buf = String::new();\n let r = stdin.read_line(&mut buf);\n match r {\n Ok(_) => {}\n _ => panic!(\"could not read from stdin\"),\n }\n let col1: Vec = buf\n .trim()\n .split(\" \")\n .skip(1)\n .map(|s| s.parse::().unwrap())\n .collect();\n\n let mut buf = String::new();\n let r = stdin.read_line(&mut buf);\n match r {\n Ok(_) => {}\n _ => panic!(\"could not read from stdin\"),\n }\n let col2: Vec = buf\n .trim()\n .split(\" \")\n .skip(1)\n .map(|s| s.parse::().unwrap())\n .collect();\n\n let a1 = std::cmp::max(\n line1[line1.len() - 1] - line1[0],\n line2[line2.len() - 1] - line2[0],\n ) * h;\n let a2 = std::cmp::max(\n col1[col1.len() - 1] - col1[0],\n col2[col2.len() - 1] - col2[0],\n ) * w;\n\n println!(\"{}\", std::cmp::max(a1, a2));\n }\n}", "difficulty": "medium"} {"problem_id": "1509", "problem_description": "David was given a red checkered rectangle of size $$$n \\times m$$$. But he doesn't like it. So David cuts the original or any other rectangle piece obtained during the cutting into two new pieces along the grid lines. He can do this operation as many times as he wants.As a result, he will get a set of rectangles. Rectangles $$$1 \\times 1$$$ are forbidden.David also knows how to paint the cells blue. He wants each rectangle from the resulting set of pieces to be colored such that any pair of adjacent cells by side (from the same piece) have different colors.What is the minimum number of cells David will have to paint?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n\n if (n == 2 && m == 2) {\n printf(\"2\\n\");\n continue;\n }\n if (n % 3 == 0 || m % 3 == 0) {\n printf(\"%d\\n\", (m * n) / 3);\n continue;\n } else if (m % 3 == 1) {\n printf(\"%d\\n\", n * (m - 1) / 3 + n / 3 + 1);\n continue;\n } else if (n % 3 == 1) {\n printf(\"%d\\n\", (n - 1) * m / 3 + m / 3 + 1);\n continue;\n } else if (n > 2 && n % 3 == 2 && m % 3 == 2) {\n printf(\"%d\\n\", (n - 2) * m / 3 + 2 * (m / 3 + 1));\n continue;\n } else if (m > 2 && n % 3 == 2 && m % 3 == 2) {\n printf(\"%d\\n\", (m - 2) * n / 3 + 2 * (n / 3 + 1));\n continue;\n }\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(1) {\n if let [n, m] = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .collect::>()[..]\n {\n writeln!(o, \"{}\", (n * m - 1) / 3 + 1).ok();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1510", "problem_description": "Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path.Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.", "c_code": "int solution() {\n int n;\n int a[100000];\n int b[100000];\n int s[100000];\n int k;\n int root;\n int n_r;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n s[i] = 0;\n }\n for (int i = 0; i < n - 1; i++) {\n scanf(\"%d%d\", &a[i], &b[i]);\n s[a[i] - 1] += 1;\n s[b[i] - 1] += 1;\n }\n k = 0;\n\n for (int i = 0; i < n; i++) {\n\n if (s[i] > 2) {\n root = i;\n k += 1;\n }\n }\n\n if (k > 1) {\n printf(\"No\\n\");\n } else if (k == 0) {\n printf(\"Yes\\n1\\n\");\n for (int i = 0; i < n; i++) {\n if (s[i] == 1) {\n printf(\"%d \", i + 1);\n }\n }\n printf(\"\\n\");\n } else {\n n_r = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == 1) {\n n_r += 1;\n }\n }\n printf(\"Yes\\n%d\\n\", n_r);\n for (int i = 0; i < n; i++) {\n if (s[i] == 1) {\n printf(\"%d %d\\n\", i + 1, root + 1);\n }\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n\n let mut buf = String::new();\n stdin.read_line(&mut buf);\n\n let n: i32 = buf.trim().parse::().unwrap();\n\n let mut edges: Vec> = Vec::new();\n for _idx in 0..n {\n let mut buf = String::new();\n stdin.read_line(&mut buf);\n\n let edge: Vec = buf\n .split_whitespace()\n .map(|x| x.trim().parse::().unwrap())\n .collect();\n\n edges.push(edge);\n }\n\n let mut nodeToEdgeIndices: HashMap> = HashMap::new();\n\n for edgeIdx in 0..edges.len() {\n for node in edges.get(edgeIdx).unwrap() {\n nodeToEdgeIndices\n .entry(*node)\n .or_default()\n .push(edgeIdx as i32);\n }\n }\n\n let mut centerNode = -1i32;\n for (node, edgeIndices) in nodeToEdgeIndices.iter() {\n if edgeIndices.len() > 2 {\n if centerNode >= 0 {\n println!(\"No\");\n return;\n }\n centerNode = *node;\n }\n }\n if centerNode < 0 {\n centerNode = *nodeToEdgeIndices.keys().next().unwrap();\n }\n\n println!(\"Yes\");\n\n let centerEdges: Vec = nodeToEdgeIndices.get(¢erNode).unwrap().to_vec();\n println!(\"{}\", centerEdges.len());\n for edgeIdx in centerEdges {\n let mut endNode = centerNode;\n let mut endEdgeIdx = edgeIdx;\n loop {\n let edge = edges.get(endEdgeIdx as usize).unwrap();\n if endNode == edge[0] {\n endNode = edge[1];\n } else {\n endNode = edge[0];\n }\n\n let edgeIndices = nodeToEdgeIndices.get(&endNode).unwrap();\n if edgeIndices.len() < 2 {\n break;\n }\n if endEdgeIdx == edgeIndices[0] {\n endEdgeIdx = edgeIndices[1];\n } else {\n endEdgeIdx = edgeIndices[0];\n }\n }\n println!(\"{} {}\", centerNode, endNode);\n }\n}", "difficulty": "medium"} {"problem_id": "1511", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from $$$a$$$ to make it a good array. Recall that the prefix of the array $$$a=[a_1, a_2, \\dots, a_n]$$$ is a subarray consisting several first elements: the prefix of the array $$$a$$$ of length $$$k$$$ is the array $$$[a_1, a_2, \\dots, a_k]$$$ ($$$0 \\le k \\le n$$$).The array $$$b$$$ of length $$$m$$$ is called good, if you can obtain a non-decreasing array $$$c$$$ ($$$c_1 \\le c_2 \\le \\dots \\le c_{m}$$$) from it, repeating the following operation $$$m$$$ times (initially, $$$c$$$ is empty): select either the first or the last element of $$$b$$$, remove it from $$$b$$$, and append it to the end of the array $$$c$$$. For example, if we do $$$4$$$ operations: take $$$b_1$$$, then $$$b_{m}$$$, then $$$b_{m-1}$$$ and at last $$$b_2$$$, then $$$b$$$ becomes $$$[b_3, b_4, \\dots, b_{m-3}]$$$ and $$$c =[b_1, b_{m}, b_{m-1}, b_2]$$$.Consider the following example: $$$b = [1, 2, 3, 4, 4, 2, 1]$$$. This array is good because we can obtain non-decreasing array $$$c$$$ from it by the following sequence of operations: take the first element of $$$b$$$, so $$$b = [2, 3, 4, 4, 2, 1]$$$, $$$c = [1]$$$; take the last element of $$$b$$$, so $$$b = [2, 3, 4, 4, 2]$$$, $$$c = [1, 1]$$$; take the last element of $$$b$$$, so $$$b = [2, 3, 4, 4]$$$, $$$c = [1, 1, 2]$$$; take the first element of $$$b$$$, so $$$b = [3, 4, 4]$$$, $$$c = [1, 1, 2, 2]$$$; take the first element of $$$b$$$, so $$$b = [4, 4]$$$, $$$c = [1, 1, 2, 2, 3]$$$; take the last element of $$$b$$$, so $$$b = [4]$$$, $$$c = [1, 1, 2, 2, 3, 4]$$$; take the only element of $$$b$$$, so $$$b = []$$$, $$$c = [1, 1, 2, 2, 3, 4, 4]$$$ — $$$c$$$ is non-decreasing. Note that the array consisting of one element is good.Print the length of the shortest prefix of $$$a$$$ to delete (erase), to make $$$a$$$ to be a good array. Note that the required length can be $$$0$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n int i = 0;\n for (; i < t; i++) {\n int n = 0;\n scanf(\"%d\", &n);\n int v[200000] = {0};\n int j = 0;\n for (; j < n; j++) {\n scanf(\"%d\", &v[j]);\n }\n int suf = 0;\n int aux = 0;\n for (j = 0; j < n; j++) {\n if (aux == 0 && v[n - j - 1] > v[n - j - 2]) {\n\n aux = 1;\n suf++;\n } else if (aux == 1 && v[n - j - 1] < v[n - j - 2]) {\n suf++;\n break;\n } else {\n suf++;\n }\n }\n printf(\"%d\\n\", n - suf);\n }\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 'outer: 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 let a: 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 x = *a.last().unwrap();\n let mut b = true;\n for i in (0..n).rev() {\n if b && a[i] >= x {\n x = a[i];\n } else if a[i] <= x {\n x = a[i];\n b = false;\n } else {\n println!(\"{}\", i + 1);\n continue 'outer;\n }\n }\n println!(\"0\");\n }\n}", "difficulty": "hard"} {"problem_id": "1512", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Given are integers A, B, and N.

\n

Find the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N.

\n

Here floor(t) denotes the greatest integer not greater than the real number t.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ A ≤ 10^{6}
  • \n
  • 1 ≤ B ≤ 10^{12}
  • \n
  • 1 ≤ N ≤ 10^{12}
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N, as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 7 4\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

When x=3, floor(Ax/B)-A×floor(x/B) = floor(15/7) - 5×floor(3/7) = 2. This is the maximum value possible.

\n
\n
\n
\n
\n
\n

Sample Input 2

11 10 9\n
\n
\n
\n
\n
\n

Sample Output 2

9\n
\n
\n
", "c_code": "int solution() {\n long int x = 0;\n double A;\n double B;\n double N;\n scanf(\"%lf %lf %lf\", &A, &B, &N);\n if (N >= B - 1) {\n x = A * ((B - 1) / B);\n printf(\"%ld\", x);\n } else {\n x = A * (N / B);\n printf(\"%ld\", x);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n std::io::stdin().read_to_string(&mut line);\n let mut iter = line.split_whitespace();\n let a: i64 = iter.next().unwrap().parse().unwrap();\n let b: i64 = iter.next().unwrap().parse().unwrap();\n let n: i64 = iter.next().unwrap().parse().unwrap();\n if b <= n {\n let x = b - 1;\n println!(\"{}\", a * x / b - a * (x / b))\n } else {\n let x = n;\n println!(\"{}\", a * x / b - a * (x / b))\n }\n}", "difficulty": "easy"} {"problem_id": "1513", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

\n

You are given Q queries.

\n

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤Q≤10^5
  • \n
  • 1≤l_i≤r_i≤10^5
  • \n
  • l_i and r_i are odd.
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
Q\nl_1 r_1\n:\nl_Q r_Q\n
\n
\n
\n
\n
\n

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1\n3 7\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n
    \n
  • 3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.
  • \n
  • 5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.
  • \n
  • 7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.
  • \n
\n

Thus, the response to the first query should be 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n13 13\n7 11\n7 11\n2017 2017\n
\n
\n
\n
\n
\n

Sample Output 2

1\n0\n0\n1\n
\n

Note that 2017 is also similar to 2017.

\n
\n
\n
\n
\n
\n

Sample Input 3

6\n1 53\n13 91\n37 55\n19 51\n73 91\n13 49\n
\n
\n
\n
\n
\n

Sample Output 3

4\n4\n1\n1\n1\n2\n
\n
\n
", "c_code": "int solution(void) {\n\n long q;\n scanf(\"%ld\", &q);\n long l[q];\n long r[q];\n for (long i = 0; i < q; i++) {\n scanf(\"%ld %ld\", &l[i], &r[i]);\n }\n int prime[200001];\n prime[0] = 0;\n prime[1] = 0;\n for (long i = 2; i < 200001; i++) {\n prime[i] = 1;\n }\n for (long i = 2; i < 200001; i++) {\n if (prime[i] == 1) {\n for (long j = 2; i * j < 200001; j++) {\n prime[i * j] = 0;\n }\n }\n }\n long sum[200001];\n sum[0] = 0;\n for (long i = 1; i < 200001; i++) {\n sum[i] = sum[i - 1];\n if (i % 2 == 1 && prime[i] == 1 && prime[(i + 1) / 2] == 1) {\n sum[i]++;\n }\n }\n for (long i = 0; i < q; i++) {\n printf(\"%ld\\n\", sum[r[i]] - sum[l[i] - 1]);\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 let q: usize = itr.next().unwrap().parse().unwrap();\n let mut out = Vec::new();\n\n let mut is_prime: Vec = vec![true; 100010];\n let mut cumsum: Vec = vec![0; 100010];\n is_prime[0] = false;\n is_prime[1] = false;\n for i in 2..100005 {\n if is_prime[i] {\n let mut j = i * 2;\n while j < 100005 {\n is_prime[j] = false;\n j += i;\n }\n }\n }\n for i in 1..100005 {\n if is_prime[i] && is_prime[(i + 1) >> 1] {\n cumsum[i] += 1;\n }\n }\n for i in 0..100005 {\n cumsum[i + 1] += cumsum[i];\n }\n\n for _ in 0..q {\n let l: usize = itr.next().unwrap().parse().unwrap();\n let r: usize = itr.next().unwrap().parse().unwrap();\n writeln!(out, \"{}\", cumsum[r] - cumsum[l - 1]).unwrap();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "easy"} {"problem_id": "1514", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

\n

At least how many sheets of paper does he need?

\n
\n
\n
\n
\n

Constraints

    \n
  • N is an integer.
  • \n
  • 1 \\leq N \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100\n
\n
\n
\n
\n
\n

Sample Output 3

50\n
\n
\n
", "c_code": "int solution() {\n int N = 0;\n scanf(\"%d\", &N);\n if (N % 2 == 0 && (N >= 1 && N <= 100)) {\n printf(\"%d\", N / 2);\n } else if (N % 2 == 1 && (N >= 1 && N <= 100)) {\n printf(\"%d\", (N / 2) + 1);\n }\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"error\");\n let n: i32 = match buf.trim().parse() {\n Ok(num) => num,\n Err(_) => panic!(\"error\"),\n };\n println!(\"{}\", n / 2 + n % 2);\n}", "difficulty": "easy"} {"problem_id": "1515", "problem_description": "Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!).Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it \"Mishka's Adjacent Replacements Algorithm\". This algorithm can be represented as a sequence of steps: Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$; Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$; Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$; Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$; Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$; Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$; $$$\\dots$$$ Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$; Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \\in\\{1, 2, \\ldots, 5 \\cdot 10^8\\}$$$ as described above.For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\\rightarrow$$$ $$$\\dots$$$ $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array.Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.", "c_code": "int solution() {\n int n;\n int a[1005] = {0};\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n - 1; i++) {\n if (a[i] & 1) {\n printf(\"%d \", a[i]);\n } else {\n printf(\"%d \", a[i] - 1);\n }\n }\n if (a[n - 1] & 1) {\n printf(\"%d\\n\", a[n - 1]);\n } else {\n printf(\"%d\\n\", a[n - 1] - 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n }\n\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let nums: Vec = buf\n .split_whitespace()\n .map(|elem| elem.parse().unwrap())\n .collect();\n\n for num in nums {\n if num % 2 == 0 {\n print!(\"{} \", num - 1);\n } else {\n print!(\"{} \", num);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1516", "problem_description": "Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly $$$1$$$ problem, during the second day — exactly $$$2$$$ problems, during the third day — exactly $$$3$$$ problems, and so on. During the $$$k$$$-th day he should solve $$$k$$$ problems.Polycarp has a list of $$$n$$$ contests, the $$$i$$$-th contest consists of $$$a_i$$$ problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly $$$k$$$ problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least $$$k$$$ problems that Polycarp didn't solve yet during the $$$k$$$-th day, then Polycarp stops his training.How many days Polycarp can train if he chooses the contests optimally?", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int i;\n int b[200001] = {0};\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b[a[i]]++;\n }\n int ans = 1;\n for (i = 1; i < 200001; i++) {\n if (i == ans && b[i] > 0) {\n ans++;\n } else if (i > ans && b[i] > 0) {\n if (ans + b[i] > i) {\n ans = i + 1;\n } else {\n ans += b[i];\n }\n }\n }\n printf(\"%d\\n\", ans - 1);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"INPUT::read line failed\");\n\n let contests = input\n .trim()\n .parse::()\n .expect(\"CONTESTS::parse to i32 failed\");\n input.clear();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"INPUT::read line failed\");\n\n let mut vec = vec![];\n let input = input.trim().split(' ');\n\n for i in input {\n vec.push(i.parse::().unwrap());\n }\n\n let mut max_days = 1;\n vec.sort();\n for i in 0..contests {\n if vec[i as usize] >= max_days {\n max_days += 1;\n }\n }\n\n println!(\"{}\", max_days - 1);\n}", "difficulty": "hard"} {"problem_id": "1517", "problem_description": "Madoka finally found the administrator password for her computer. Her father is a well-known popularizer of mathematics, so the password is the answer to the following problem.Find the maximum decimal number without zeroes and with no equal digits in a row, such that the sum of its digits is $$$n$$$.Madoka is too tired of math to solve it herself, so help her to solve this problem!", "c_code": "int solution() {\n char result[1000] = {0};\n int t = 0;\n int n = 0;\n int bd = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n memset(result, 0, 1000);\n bd = 0;\n scanf(\"%d\", &n);\n if (n == 1) {\n printf(\"1\");\n printf(\"\\n\");\n } else if (n == 2) {\n printf(\"2\");\n printf(\"\\n\");\n } else {\n if (n % 3 == 0) {\n for (int i2 = 0; i2 < (n / 3); i2++) {\n\n result[bd] = '2';\n bd++;\n result[bd] = '1';\n bd++;\n }\n printf(\"%s\", result);\n printf(\"\\n\");\n\n } else {\n if (n % 3 == 2) {\n\n result[bd] = '2';\n bd++;\n for (int i3 = 0; i3 < (n / 3); i3++) {\n\n result[bd] = '1';\n bd++;\n result[bd] = '2';\n bd++;\n }\n printf(\"%s\", result);\n printf(\"\\n\");\n } else {\n\n result[bd] = '1';\n bd++;\n for (int i4 = 0; i4 < (n / 3); i4++) {\n\n result[bd] = '2';\n bd++;\n result[bd] = '1';\n bd++;\n }\n printf(\"%s\", result);\n printf(\"\\n\");\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut events = String::new();\n io::stdin()\n .read_line(&mut events)\n .expect(\"Failed to read input\");\n let events: i64 = events.trim().parse().expect(\"Invalid input\");\n\n let mut count = 0;\n while count < events {\n let mut number = String::new();\n io::stdin()\n .read_line(&mut number)\n .expect(\"Failed to read input\");\n let number: i64 = number.trim().parse().expect(\"Invald input\");\n\n let mut maxdecimal = if number % 3 == 1 { vec![1] } else { vec![2] };\n\n while maxdecimal.iter().clone().sum::() < number {\n if maxdecimal.last().unwrap() == &1 {\n maxdecimal.push(2)\n } else {\n maxdecimal.push(1)\n }\n }\n\n println!(\n \"{}\",\n maxdecimal.iter().map(|i| i.to_string()).collect::()\n );\n count += 1\n }\n}", "difficulty": "medium"} {"problem_id": "1518", "problem_description": "Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.", "c_code": "int solution() {\n long int n;\n scanf(\"%ld\", &n);\n long int arrI[n];\n long int arrM[300000] = {0};\n int i;\n int s;\n int m;\n int b;\n for (i = 0; i < n; i++) {\n arrI[i] = 0;\n }\n int j;\n int k;\n s = 0;\n m = 0;\n b = 0;\n long long int sumS = 0;\n long long int sumB = 0;\n long long int x;\n for (i = 0; i < n; i++) {\n scanf(\"%lld\", &x);\n if (x < 1) {\n sumS += x;\n s++;\n } else if (x > n) {\n sumB += x;\n b++;\n } else {\n if (arrI[x - 1] == 0) {\n arrI[x - 1]++;\n } else {\n arrM[x - 1]++;\n m++;\n }\n }\n }\n\n long long int moves = 0;\n long long int sumTemp = 0;\n i = 0;\n int c = 0;\n while (c < s) {\n if (arrI[i] == 0) {\n c++;\n sumTemp += i + 1;\n arrI[i] = 1;\n }\n i++;\n }\n c = 0;\n i = 0;\n moves += sumTemp - sumS;\n\n long long int arrN[m][2];\n int y = 0;\n for (i = 0; y < m && i < 300000; i++) {\n if (arrM[i] != 0) {\n arrN[y][0] = i;\n arrN[y][1] = arrM[i];\n y++;\n }\n }\n y = 0;\n for (i = 0; y < m && i < n; i++) {\n if (arrI[i] == 0) {\n if (arrN[y][1] > 0) {\n arrI[i] = 1;\n moves += abs(arrN[y][0] - i);\n arrN[y][1]--;\n if (arrN[y][1] == 0) {\n y++;\n }\n }\n }\n }\n\n sumS = 0;\n for (i = 0; i < n; i++) {\n if (arrI[i] == 0) {\n sumS += i + 1;\n }\n }\n moves += sumB - sumS;\n printf(\"%lld\", moves);\n return 0;\n}", "rust_code": "fn solution() {\n let mut v: Vec = BufReader::new(io::stdin())\n .lines()\n .nth(1)\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect();\n v.sort();\n let mut res = 0u64;\n for (i, itm) in v.iter().enumerate() {\n res += (i as i32 - itm + 1).unsigned_abs() as u64;\n }\n println!(\"{}\", res);\n}", "difficulty": "hard"} {"problem_id": "1519", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There are N people standing in a queue from west to east.

\n

Given is a string S of length N representing the directions of the people.\nThe i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

\n

A person is happy if the person in front of him/her is facing the same direction.\nIf no person is standing in front of a person, however, he/she is not happy.

\n

You can perform the following operation any number of times between 0 and K (inclusive):

\n

Operation: Choose integers l and r such that 1 \\leq l \\leq r \\leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

\n

What is the maximum possible number of happy people you can have?

\n
\n
\n
\n
\n

Constraints

    \n
  • N is an integer satisfying 1 \\leq N \\leq 10^5.
  • \n
  • K is an integer satisfying 1 \\leq K \\leq 10^5.
  • \n
  • |S| = N
  • \n
  • Each character of S is L or R.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nS\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number of happy people after at most K operations.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6 1\nLRLRRL\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

\n
\n
\n
\n
\n
\n

Sample Input 2

13 3\nLRRLRLRRLRLLR\n
\n
\n
\n
\n
\n

Sample Output 2

9\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10 1\nLLLLLRRRRR\n
\n
\n
\n
\n
\n

Sample Output 3

9\n
\n
\n
\n
\n
\n
\n

Sample Input 4

9 2\nRRRLRLRLL\n
\n
\n
\n
\n
\n

Sample Output 4

7\n
\n
\n
", "c_code": "int solution(void) {\n\n int n;\n int k;\n char s[199999];\n int ans = 0;\n int tmp = 0;\n\n scanf(\"%d %d\", &n, &k);\n scanf(\"%s\", s);\n\n for (int i = 0; i < n; i++) {\n\n if (s[i] == 'L') {\n\n if (i >= 1) {\n\n if (s[i - 1] == 'L') {\n\n tmp += 1;\n }\n }\n\n } else {\n\n if (i < n) {\n\n if (s[i + 1] == 'R') {\n\n tmp += 1;\n }\n }\n }\n }\n\n tmp += k * 2;\n\n if (tmp > n - 1) {\n\n ans = n - 1;\n\n } else {\n\n ans = tmp;\n }\n\n printf(\"%d\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let k: usize = s.next().unwrap().parse().unwrap();\n let mut s: Vec<_> = s.next().unwrap().chars().collect();\n s.dedup();\n println!(\"{}\", n - std::cmp::max(1, s.len().saturating_sub(2 * k)));\n}", "difficulty": "hard"} {"problem_id": "1520", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N < 10^{100}
  • \n
  • 1 \\leq K \\leq 3
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nK\n
\n
\n
\n
\n
\n

Output

Print the count.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

100\n1\n
\n
\n
\n
\n
\n

Sample Output 1

19\n
\n

The following 19 integers satisfy the condition:

\n
    \n
  • 1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

25\n2\n
\n
\n
\n
\n
\n

Sample Output 2

14\n
\n

The following 14 integers satisfy the condition:

\n
    \n
  • 11,12,13,14,15,16,17,18,19,21,22,23,24,25
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

314159\n2\n
\n
\n
\n
\n
\n

Sample Output 3

937\n
\n
\n
\n
\n
\n
\n

Sample Input 4

9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999\n3\n
\n
\n
\n
\n
\n

Sample Output 4

117879300\n
\n
\n
", "c_code": "int solution(void) {\n\n char n[200];\n scanf(\"%s\", n);\n int length = strlen(n);\n int k;\n scanf(\"%d\", &k);\n long dp0[length][4];\n long dp1[length][4];\n dp0[0][0] = 1;\n dp0[0][1] = n[0] - '0' - 1;\n dp0[0][2] = 0;\n dp0[0][3] = 0;\n dp1[0][0] = 0;\n dp1[0][1] = 1;\n dp1[0][2] = 0;\n dp1[0][3] = 0;\n for (int i = 1; i < length; i++) {\n dp0[i][0] = 1;\n dp0[i][1] = dp0[i - 1][0] * 9 + dp0[i - 1][1];\n dp0[i][2] = dp0[i - 1][1] * 9 + dp0[i - 1][2];\n dp0[i][3] = dp0[i - 1][2] * 9 + dp0[i - 1][3];\n dp1[i][0] = 0;\n dp1[i][1] = 0;\n dp1[i][2] = 0;\n dp1[i][3] = 0;\n if (n[i] - '0' > 1) {\n dp0[i][1] += dp1[i - 1][0] * (n[i] - '0' - 1);\n dp0[i][2] += dp1[i - 1][1] * (n[i] - '0' - 1);\n dp0[i][3] += dp1[i - 1][2] * (n[i] - '0' - 1);\n }\n if (n[i] - '0' != 0) {\n dp0[i][1] += dp1[i - 1][1];\n dp0[i][2] += dp1[i - 1][2];\n dp0[i][3] += dp1[i - 1][3];\n dp1[i][1] += dp1[i - 1][0];\n dp1[i][2] += dp1[i - 1][1];\n dp1[i][3] += dp1[i - 1][2];\n } else {\n dp1[i][1] += dp1[i - 1][1];\n dp1[i][2] += dp1[i - 1][2];\n dp1[i][3] += dp1[i - 1][3];\n }\n }\n printf(\"%ld\\n\", dp0[length - 1][k] + dp1[length - 1][k]);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N: chars,\n K: usize,\n }\n let mut dp = [[[0; 2]; 4]; 101];\n dp[0][0][0] = 1;\n for (i, &c) in N.iter().enumerate() {\n let d = c as i32 - '0' as i32;\n for j in 0..(K + 1) {\n for k in 0..2 {\n for di in 0..10 {\n let (mut ni, mut nj, mut nk) = (i, j, k);\n ni += 1;\n if di != 0 {\n nj += 1;\n }\n if nj > K {\n continue;\n }\n if k == 0 {\n if di > d {\n continue;\n }\n if di < d {\n nk = 1;\n }\n }\n dp[ni][nj][nk] += dp[i][j][k];\n }\n }\n }\n }\n println!(\"{}\", dp[N.len()][K][1] + dp[N.len()][K][0]);\n}", "difficulty": "medium"} {"problem_id": "1521", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

\n

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

\n

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

\n

Determine if there is a pair of stations that will be connected by a bus service.

\n
\n
\n
\n
\n

Constraints

    \n
  • Each character of S is A or B.
  • \n
  • |S| = 3
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

ABA\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

Company A operates Station 1 and 3, while Company B operates Station 2.

\n

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

\n
\n
\n
\n
\n
\n

Sample Input 2

BBA\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n
\n

Company B operates Station 1 and 2, while Company A operates Station 3.

\n

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

\n
\n
\n
\n
\n
\n

Sample Input 3

BBB\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n

Company B operates all the stations. Thus, there will be no bus service, so print No.

\n
\n
", "c_code": "int solution(void) {\n char s[3];\n scanf(\"%c %c %c\", &s[0], &s[1], &s[2]);\n\n if ((s[0] == 'A' && s[1] == 'A' && s[2] == 'A') ||\n (s[0] == 'B' && s[1] == 'B' && s[2] == 'B')) {\n printf(\"No\\n\");\n } else {\n printf(\"Yes\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n if io::stdin().read_line(&mut input).is_ok() {\n let s = input.trim();\n println!(\n \"{}\",\n if s == \"AAA\" || s == \"BBB\" {\n \"No\"\n } else {\n \"Yes\"\n }\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1522", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
\nInitially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..
\nHowever, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.
\nDetermine if square1001 can achieve his objective.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • H is an integer between 1 and 50 (inclusive).
  • \n
  • W is an integer between 1 and 50 (inclusive).
  • \n
  • For every (i, j) (1 \\leq i \\leq H, 1 \\leq j \\leq W), s_{i, j} is # or ..
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
H W\ns_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}\ns_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}\n  :   :\ns_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}\n
\n
\n
\n
\n
\n

Output

\n

If square1001 can achieve his objective, print Yes; if he cannot, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n.#.\n###\n.#.\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

\n

\"

\n
\n
\n
\n
\n
\n

Sample Input 2

5 5\n#.#.#\n.#.#.\n#.#.#\n.#.#.\n#.#.#\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

square1001 cannot achieve his objective here.

\n
\n
\n
\n
\n
\n

Sample Input 3

11 11\n...#####...\n.##.....##.\n#..##.##..#\n#..##.##..#\n#.........#\n#...###...#\n.#########.\n.#.#.#.#.#.\n##.#.#.#.##\n..##.#.##..\n.##..#..##.\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution() {\n\n char canvas[52][52];\n int h = 0;\n int w = 0;\n int loop_h = 0;\n int loop_w = 0;\n int flag = 0;\n char strin[52];\n\n memset(canvas, 0x00, sizeof(canvas));\n\n scanf(\"%d%d\", &h, &w);\n while ('\\n' != getchar()) {\n ;\n }\n for (loop_h = 0; loop_h < h; loop_h++) {\n memset(strin, 0x00, sizeof(strin));\n fgets(strin, 52, stdin);\n\n for (loop_w = 0; loop_w < w; loop_w++) {\n canvas[loop_h + 1][loop_w + 1] = strin[loop_w];\n }\n }\n\n for (loop_h = 1; loop_h < h + 1; loop_h++) {\n for (loop_w = 1; loop_w < w + 1; loop_w++) {\n if ('#' == canvas[loop_h][loop_w]) {\n\n if (('#' != canvas[loop_h - 1][loop_w]) &&\n ('#' != canvas[loop_h + 1][loop_w]) &&\n ('#' != canvas[loop_h][loop_w - 1]) &&\n ('#' != canvas[loop_h][loop_w + 1])) {\n flag = 1;\n break;\n }\n }\n }\n }\n\n if (0 == flag) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n\n return 0;\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 y: usize = iter.next().unwrap().parse().unwrap();\n let x: usize = iter.next().unwrap().parse().unwrap();\n let squares: Vec> = (0..y)\n .map(|_| {\n let str: String = iter.next().unwrap().parse().unwrap();\n str.chars().collect::>()\n })\n .collect();\n\n let mut can_paint = true;\n 'outer: for (i, line) in squares.iter().enumerate() {\n for (j, &square) in line.iter().enumerate() {\n if square == '#' {\n if i != 0 && squares[i - 1][j] == '#' {\n continue;\n }\n\n if j != 0 && squares[i][j - 1] == '#' {\n continue;\n }\n if j < x - 1 && squares[i][j + 1] == '#' {\n continue;\n }\n\n if i < y - 1 && squares[i + 1][j] == '#' {\n continue;\n }\n\n can_paint = false;\n break 'outer;\n }\n }\n }\n println!(\"{}\", if can_paint { \"Yes\" } else { \"No\" });\n}", "difficulty": "medium"} {"problem_id": "1523", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given an undirected unweighted graph with N vertices and M edges that contains neither self-loops nor double edges.
\nHere, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).
\nHow many different paths start from vertex 1 and visit all the vertices exactly once?
\nHere, the endpoints of a path are considered visited.

\n

For example, let us assume that the following undirected graph shown in Figure 1 is given.

\n
\n\n

Figure 1: an example of an undirected graph

\n
\n

The following path shown in Figure 2 satisfies the condition.

\n
\n\n

Figure 2: an example of a path that satisfies the condition

\n
\n

However, the following path shown in Figure 3 does not satisfy the condition, because it does not visit all the vertices.

\n
\n\n

Figure 3: an example of a path that does not satisfy the condition

\n
\n

Neither the following path shown in Figure 4, because it does not start from vertex 1.

\n
\n\n

Figure 4: another example of a path that does not satisfy the condition

\n
\n
\n
\n
\n
\n

Constraints

    \n
  • 2≦N≦8
  • \n
  • 0≦M≦N(N-1)/2
  • \n
  • 1≦a_i<b_i≦N
  • \n
  • The given graph contains neither self-loops nor double edges.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N M  \na_1 b_1  \na_2 b_2\n:  \na_M b_M  \n
\n
\n
\n
\n
\n

Output

Print the number of the different paths that start from vertex 1 and visit all the vertices exactly once.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n1 2\n1 3\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The given graph is shown in the following figure:

\n
\n\"43c0ac53de20d989d100bf60b3cd05fa.png\"\n
\n

The following two paths satisfy the condition:

\n
\n\"c4a27b591d364fa479314e3261b85071.png\"\n
\n
\n
\n
\n
\n
\n

Sample Input 2

7 7\n1 3\n2 7\n3 4\n4 5\n4 6\n5 6\n6 7\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

This test case is the same as the one described in the problem statement.

\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n int i;\n int ab[30][2];\n int ans = 0;\n int ttyk[10][10] = {0};\n int j;\n scanf(\"%d%d\", &n, &m);\n for (i = 0; i < m; i++) {\n scanf(\"%d%d\", &ab[i][0], &ab[i][1]);\n ttyk[ab[i][0]][ab[i][1]] = 1;\n ttyk[ab[i][1]][ab[i][0]] = 1;\n }\n int dfs[100000][10] = {0};\n int st = 0;\n int fn = 1;\n dfs[0][0] = 1;\n dfs[0][1] = 1;\n while (fn > st) {\n int f = 0;\n for (j = 1; j <= n; j++) {\n if (ttyk[dfs[st][0]][j] == 1 && dfs[st][j] != 1) {\n dfs[fn][0] = j;\n for (i = 1; i <= n; i++) {\n dfs[fn][i] = dfs[st][i];\n }\n dfs[fn][j] = 1;\n fn++;\n f = 1;\n }\n }\n if (f == 0) {\n for (j = 1; j <= n; j++) {\n if (dfs[st][j] == 0) {\n break;\n }\n }\n if (j == n + 1) {\n ans++;\n }\n }\n st++;\n }\n printf(\"%d\\n\", ans);\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 mut hens: Vec<(usize, usize)> = Vec::new();\n for i in 0..m {\n let thisline = vec[i + 1].split(' ').collect::>();\n let a: usize = thisline[0].trim().parse().unwrap();\n let b: usize = thisline[1].trim().parse().unwrap();\n hens.push((a - 1, b - 1));\n }\n let hens = hens;\n let mut dp: Vec = vec![0; n * (1 << n)];\n dp[n] = 1;\n for i in 2..(1 << n) {\n for j in 0..n {\n if (1 << j) & i == 0 {\n continue;\n }\n for &(a, b) in &hens {\n let tail = if a == j {\n b\n } else if b == j {\n a\n } else {\n n\n };\n if tail == n {\n continue;\n }\n dp[i * n + j] += dp[(i ^ (1 << j)) * n + tail];\n }\n }\n }\n let mut fin = 0;\n for i in 0..n {\n fin += dp[((1 << n) - 1) * n + i];\n }\n println!(\"{}\", fin);\n}", "difficulty": "hard"} {"problem_id": "1524", "problem_description": "There are $$$n$$$ workers and $$$m$$$ tasks. The workers are numbered from $$$1$$$ to $$$n$$$. Each task $$$i$$$ has a value $$$a_i$$$ — the index of worker who is proficient in this task.Every task should have a worker assigned to it. If a worker is proficient in the task, they complete it in $$$1$$$ hour. Otherwise, it takes them $$$2$$$ hours.The workers work in parallel, independently of each other. Each worker can only work on one task at once.Assign the workers to all tasks in such a way that the tasks are completed as early as possible. The work starts at time $$$0$$$. What's the minimum time all tasks can be completed by?", "c_code": "int solution() {\n int testcases;\n int n;\n int m;\n long long int extra;\n long long int need;\n int low;\n int high;\n int mid;\n scanf(\"%d\", &testcases);\n int a[200500];\n int temp;\n while (testcases--) {\n scanf(\"%d%d\", &n, &m);\n for (int i = 0; i < n; i++) {\n a[i] = 0;\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &temp);\n ++a[temp - 1];\n }\n low = 0;\n high = m + 1;\n while (low < high) {\n mid = (low + high) / 2;\n need = 0;\n extra = 0;\n for (int i = 0; i < n; i++) {\n if (a[i] <= mid) {\n extra += (mid - a[i]) / 2;\n } else {\n need += a[i] - mid;\n }\n }\n if (need <= extra) {\n high = mid;\n }\n\n else {\n low = mid + 1;\n }\n }\n printf(\"%d\\n\", low);\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n name = reader,\n tests: usize\n }\n for _ in 0..tests {\n input! {\n use reader,\n n: usize,\n m: usize,\n a: [usize1; m]\n }\n let mut cnt = vec![0i64; n];\n for a in a {\n cnt[a] += 1;\n }\n let mut ok = *cnt.iter().max().unwrap();\n let mut ng = 0;\n while ng + 1 < ok {\n let mid = (ng + ok) / 2;\n let mut over = 0;\n let mut under = 0;\n for &c in cnt.iter() {\n if c >= mid {\n over += c - mid;\n } else {\n under += (mid - c) / 2;\n }\n }\n if over <= under {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n println!(\"{}\", ok);\n }\n}", "difficulty": "medium"} {"problem_id": "1525", "problem_description": "A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).Let $$$p$$$ be any permutation of length $$$n$$$. We define the fingerprint $$$F(p)$$$ of $$$p$$$ as the sorted array of sums of adjacent elements in $$$p$$$. More formally,$$$$$$F(p)=\\mathrm{sort}([p_1+p_2,p_2+p_3,\\ldots,p_{n-1}+p_n]).$$$$$$For example, if $$$n=4$$$ and $$$p=[1,4,2,3],$$$ then the fingerprint is given by $$$F(p)=\\mathrm{sort}([1+4,4+2,2+3])=\\mathrm{sort}([5,6,5])=[5,5,6]$$$.You are given a permutation $$$p$$$ of length $$$n$$$. Your task is to find a different permutation $$$p'$$$ with the same fingerprint. Two permutations $$$p$$$ and $$$p'$$$ are considered different if there is some index $$$i$$$ such that $$$p_i \\ne p'_i$$$.", "c_code": "int solution() {\n int T = 1;\n scanf(\"%d\", &T);\n while (T--) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = n - 1; i >= 0; i--) {\n printf(\"%d \", a[i]);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let t: u32 = input.trim().parse().unwrap();\n input.clear();\n let _ans: &mut Vec;\n let mut q: Vec;\n\n for _i in 0..t {\n io::stdin().read_line(&mut input).unwrap();\n input.clear();\n io::stdin().read_line(&mut input).unwrap();\n q = input\n .split_whitespace()\n .rev()\n .map(|num| num.parse().unwrap())\n .collect();\n println!(\"{}\", q.join(\" \"));\n input.clear();\n }\n}", "difficulty": "medium"} {"problem_id": "1526", "problem_description": "There are $$$n$$$ boxers, the weight of the $$$i$$$-th boxer is $$$a_i$$$. Each of them can change the weight by no more than $$$1$$$ before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique).Write a program that for given current values ​$$$a_i$$$ will find the maximum possible number of boxers in a team.It is possible that after some change the weight of some boxer is $$$150001$$$ (but no more).", "c_code": "int solution() {\n int n;\n int m;\n int i;\n int j;\n int k;\n int arr[200009] = {};\n int brr[150009] = {};\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &m);\n arr[m]++;\n }\n k = 0;\n for (i = 1; i <= 150001; i++) {\n if (arr[i] >= 1 && i > 1 && brr[i - 1] == 0) {\n k++;\n brr[i - 1] = 1;\n arr[i]--;\n }\n if (arr[i] >= 1 && brr[i] == 0) {\n k++;\n brr[i] = 1;\n arr[i]--;\n }\n if (arr[i] >= 1 && brr[i + 1] == 0) {\n k++;\n brr[i + 1] = 1;\n arr[i]--;\n }\n }\n printf(\"%d\", k);\n}", "rust_code": "fn solution() {\n let mut v: Vec = BufReader::new(io::stdin())\n .lines()\n .nth(1)\n .unwrap()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect();\n v.sort();\n let mut max = 0u32;\n let mut cnt = 0u32;\n for mut w in v {\n let diff = w - max as i32;\n match diff {\n 0 => w += 1,\n 1 => (),\n v if v > 1 => w -= 1,\n _ => continue,\n }\n max = w as u32;\n cnt += 1;\n }\n println!(\"{}\", cnt)\n}", "difficulty": "hard"} {"problem_id": "1527", "problem_description": "The Squareland national forest is divided into equal $$$1 \\times 1$$$ square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates $$$(x, y)$$$ of its south-west corner.Three friends, Alice, Bob, and Charlie are going to buy three distinct plots of land $$$A, B, C$$$ in the forest. Initially, all plots in the forest (including the plots $$$A, B, C$$$) are covered by trees. The friends want to visit each other, so they want to clean some of the plots from trees. After cleaning, one should be able to reach any of the plots $$$A, B, C$$$ from any other one of those by moving through adjacent cleared plots. Two plots are adjacent if they share a side. For example, $$$A=(0,0)$$$, $$$B=(1,1)$$$, $$$C=(2,2)$$$. The minimal number of plots to be cleared is $$$5$$$. One of the ways to do it is shown with the gray color. Of course, the friends don't want to strain too much. Help them find out the smallest number of plots they need to clean from trees.", "c_code": "int solution(int argc, char const *argv[]) {\n int x1;\n int x2;\n int x3;\n int y1;\n int y2;\n int y3;\n scanf(\"%d%d\", &x1, &y1);\n scanf(\"%d%d\", &x2, &y2);\n scanf(\"%d%d\", &x3, &y3);\n int minx = 2000;\n int miny = 2000;\n int maxx = -1;\n int maxy = -1;\n if (x1 < minx) {\n minx = x1;\n }\n if (x2 < minx) {\n minx = x2;\n }\n if (x3 < minx) {\n minx = x3;\n }\n if (x1 > maxx) {\n maxx = x1;\n }\n if (x2 > maxx) {\n maxx = x2;\n }\n if (x3 > maxx) {\n maxx = x3;\n }\n\n if (y1 < miny) {\n miny = y1;\n }\n if (y2 < miny) {\n miny = y2;\n }\n if (y3 < miny) {\n miny = y3;\n }\n if (y1 > maxy) {\n maxy = y1;\n }\n if (y2 > maxy) {\n maxy = y2;\n }\n if (y3 > maxy) {\n maxy = y3;\n }\n int ans = maxy + maxx - miny - minx + 1;\n printf(\"%d\", ans);\n\n int midx;\n int midy;\n if ((x1 <= x2 && x1 >= x3) || (x1 >= x2 && x1 <= x3)) {\n midx = x1;\n }\n if ((x2 <= x1 && x2 >= x3) || (x2 >= x1 && x2 <= x3)) {\n midx = x2;\n }\n if ((x3 <= x2 && x3 >= x1) || (x3 >= x2 && x3 <= x1)) {\n midx = x3;\n }\n if ((y1 <= y2 && y1 >= y3) || (y1 >= y2 && y1 <= y3)) {\n midy = y1;\n }\n if ((y2 <= y1 && y2 >= y3) || (y2 >= y1 && y2 <= y3)) {\n midy = y2;\n }\n if ((y3 <= y2 && y3 >= y1) || (y3 >= y2 && y3 <= y1)) {\n midy = y3;\n }\n\n for (int i = miny; i <= maxy; ++i) {\n printf(\"\\n%d %d\", midx, i);\n }\n int y;\n if (minx == x1) {\n y = y1;\n }\n if (minx == x2) {\n y = y2;\n }\n if (minx == x3) {\n y = y3;\n }\n\n for (int i = minx; i < midx; ++i) {\n printf(\"\\n%d %d\", i, y);\n }\n if (maxx == x1) {\n y = y1;\n }\n if (maxx == x2) {\n y = y2;\n }\n if (maxx == x3) {\n y = y3;\n }\n\n for (int i = maxx; i > midx; --i) {\n printf(\"\\n%d %d\", i, y);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut visited = vec![vec![0; 1005]; 1005];\n let mut v = Vec::new();\n for _ in 0..3 {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let mut iter = s.split_whitespace();\n let t: (usize, usize) = (\n iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().parse().unwrap(),\n );\n v.push(t);\n }\n v.sort();\n let mut x = v[0].0;\n let mut y = v[0].1;\n while x < v[1].0 {\n visited[x][y] = 1;\n x += 1;\n }\n let mut dy = if v[1].1 > y { 1 } else { -1i32 };\n while y != v[1].1 {\n visited[x][y] = 1;\n y = (y as i32 + dy) as usize;\n }\n dy = if v[2].1 > y { 1 } else { -1 };\n while y != v[2].1 {\n visited[x][y] = 1;\n y = (y as i32 + dy) as usize;\n }\n while x < v[2].0 {\n visited[x][y] = 1;\n x += 1;\n }\n visited[x][y] = 1;\n let mut tot = 0;\n for i in 0..visited.len() {\n for j in 0..visited.len() {\n if visited[i][j] == 1 {\n tot += 1;\n }\n }\n }\n println!(\"{}\", tot);\n for i in 0..visited.len() {\n for j in 0..visited.len() {\n if visited[i][j] == 1 {\n println!(\"{} {}\", i, j);\n }\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1528", "problem_description": "Madoka is a very strange girl, and therefore she suddenly wondered how many pairs of integers $$$(a, b)$$$ exist, where $$$1 \\leq a, b \\leq n$$$, for which $$$\\frac{\\operatorname{lcm}(a, b)}{\\operatorname{gcd}(a, b)} \\leq 3$$$.In this problem, $$$\\operatorname{gcd}(a, b)$$$ denotes the greatest common divisor of the numbers $$$a$$$ and $$$b$$$, and $$$\\operatorname{lcm}(a, b)$$$ denotes the smallest common multiple of the numbers $$$a$$$ and $$$b$$$.", "c_code": "int solution() {\n\n int licz;\n scanf(\"%d\", &licz);\n\n int wczyt[licz];\n\n int i = 0;\n for (i = 0; i < licz; i++) {\n scanf(\"%d\", &wczyt[i]);\n }\n\n for (i = 0; i < licz; i++) {\n printf(\"%d\\n\", wczyt[i] + ((wczyt[i] / 2) * 2) + ((wczyt[i] / 3) * 2));\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let std_in = stdin();\n let mut input = Scanner::new(std_in.lock());\n\n let std_out = stdout();\n let mut output = BufWriter::new(std_out.lock());\n\n let t: usize = input.token();\n for _ in 0..t {\n let n: usize = input.token();\n let result = n + 2 * (n / 2 + n / 3);\n writeln!(output, \"{}\", result).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1529", "problem_description": "Arkady is playing Battleship. The rules of this game aren't really important.There is a field of $$$n \\times n$$$ cells. There should be exactly one $$$k$$$-decker on the field, i. e. a ship that is $$$k$$$ cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship.Consider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship.", "c_code": "int solution() {\n int n;\n int k;\n int i;\n int j;\n int bi = 0;\n int bj = 0;\n int bv = 0;\n char map[101][101];\n scanf(\"%d%d\", &n, &k);\n for (i = 0; i < n; i++) {\n scanf(\"%s\", map[i]);\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < n; j++) {\n if (map[i][j] == '#') {\n continue;\n }\n int i1 = i;\n int i2 = i;\n while (i1 >= 0 && i - i1 < k && map[i1][j] == '.') {\n i1--;\n }\n while (i2 < n && i2 - i < k && map[i2][j] == '.') {\n i2++;\n }\n int d1 = i2 - i1 - k;\n if (d1 < 0) {\n d1 = 0;\n }\n int j1 = j;\n int j2 = j;\n while (j1 >= 0 && j - j1 < k && map[i][j1] == '.') {\n j1--;\n }\n while (j2 < n && j2 - j < k && map[i][j2] == '.') {\n j2++;\n }\n int d2 = j2 - j1 - k;\n if (d2 < 0) {\n d2 = 0;\n }\n if (d1 + d2 > bv) {\n bv = d1 + d2;\n bi = i;\n bj = j;\n }\n }\n }\n printf(\"%d %d\\n\", bi + 1, bj + 1);\n}", "rust_code": "fn solution() {\n use std::io;\n use std::io::prelude::*;\n\n let mut input = String::new();\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n let n: usize = it.next().unwrap().parse().unwrap();\n let k: usize = it.next().unwrap().parse().unwrap();\n\n let mut field = Vec::with_capacity(n * n);\n\n for _ in 0..n {\n for c in it.next().unwrap().chars() {\n match c {\n '#' => field.push(Cell::Empty),\n '.' => field.push(Cell::Unknown),\n _ => panic!(\"Invalid input\"),\n }\n }\n }\n\n let check_h = |p| {\n (p..).take(k).all(|p| match field[p] {\n Cell::Empty => false,\n Cell::Unknown => true,\n })\n };\n\n let check_v = |mut p| {\n for _ in 0..k {\n match field[p] {\n Cell::Empty => return false,\n Cell::Unknown => (),\n }\n p += n;\n }\n true\n };\n\n let _ans = 0;\n\n let mut pl = vec![0; n * n];\n\n for y in 0..n {\n for x in 0..n {\n let p = y * n + x;\n if x + k <= n && check_h(p) {\n for i in 0..k {\n pl[p + i] += 1;\n }\n }\n if y + k <= n && check_v(p) {\n for j in 0..k {\n pl[p + n * j] += 1;\n }\n }\n }\n }\n\n let max_val = *pl.iter().max().unwrap();\n for y in 0..n {\n for x in 0..n {\n if pl[y * n + x] == max_val {\n println!(\"{} {}\", y + 1, x + 1);\n return;\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1530", "problem_description": "Vasya has a grid with $$$2$$$ rows and $$$n$$$ columns. He colours each cell red, green, or blue.Vasya is colourblind and can't distinguish green from blue. Determine if Vasya will consider the two rows of the grid to be coloured the same.", "c_code": "int solution() {\n\n int n = 0;\n int m = 0;\n scanf(\"%d\", &m);\n getchar();\n for (int v = 0; v < m; v++) {\n scanf(\"%d\", &n);\n\n getchar();\n char s1[9999] = \"0\";\n char s2[9999] = \"0\";\n\n for (int i = 0; i < n; i++) {\n scanf(\"%c\", &s1[i]);\n\n if (s1[i] == 'B') {\n s1[i] = 'G';\n }\n }\n getchar();\n for (int i = 0; i < n; i++) {\n scanf(\"%c\", &s2[i]);\n if (s2[i] == 'B') {\n s2[i] = 'G';\n }\n }\n getchar();\n if (strcmp(s1, s2) == 0) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut inp = String::new();\n\n std::io::stdin().read_line(&mut inp).unwrap();\n let t: i32 = inp.trim().parse().unwrap();\n\n for _ in 0..t {\n inp = String::new();\n std::io::stdin().read_line(&mut inp).unwrap();\n\n let mut l1 = String::new();\n std::io::stdin().read_line(&mut l1).unwrap();\n\n let mut l2 = String::new();\n std::io::stdin().read_line(&mut l2).unwrap();\n\n l1 = l1.replace(\"G\", \"B\");\n l2 = l2.replace(\"G\", \"B\");\n\n if l1 == l2 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1531", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are parking at a parking lot. You can choose from the following two fee plans:

\n
    \n
  • Plan 1: The fee will be A×T yen (the currency of Japan) when you park for T hours.
  • \n
  • Plan 2: The fee will be B yen, regardless of the duration.
  • \n
\n

Find the minimum fee when you park for N hours.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≤N≤20
  • \n
  • 1≤A≤100
  • \n
  • 1≤B≤2000
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N A B\n
\n
\n
\n
\n
\n

Output

When the minimum fee is x yen, print the value of x.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7 17 120\n
\n
\n
\n
\n
\n

Sample Output 1

119\n
\n
    \n
  • If you choose Plan 1, the fee will be 7×17=119 yen.
  • \n
  • If you choose Plan 2, the fee will be 120 yen.
  • \n
\n

Thus, the minimum fee is 119 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 20 100\n
\n
\n
\n
\n
\n

Sample Output 2

100\n
\n

The fee might be the same in the two plans.

\n
\n
\n
\n
\n
\n

Sample Input 3

6 18 100\n
\n
\n
\n
\n
\n

Sample Output 3

100\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int n = 0;\n scanf(\"%d %d %d\", &n, &a, &b);\n if (a * n <= b) {\n printf(\"%d\\n\", a * n);\n } else {\n printf(\"%d\\n\", b);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let input: Vec = buf.split_whitespace().map(|c| c.parse().unwrap()).collect();\n let n = input[0];\n let a = input[1];\n let b = input[2];\n\n println!(\"{}\", std::cmp::min(n * a, b));\n}", "difficulty": "medium"} {"problem_id": "1532", "problem_description": "You are given a following process. There is a platform with $$$n$$$ columns. $$$1 \\times 1$$$ squares are appearing one after another in some columns on this platform. If there are no squares in the column, a square will occupy the bottom row. Otherwise a square will appear at the top of the highest square of this column. When all of the $$$n$$$ columns have at least one square in them, the bottom row is being removed. You will receive $$$1$$$ point for this, and all the squares left will fall down one row. You task is to calculate the amount of points you will receive.", "c_code": "int solution() {\n int len = 0;\n int num = 0;\n scanf(\"%d %d\", &len, &num);\n int arr[1000] = {0};\n int count = 0;\n int stop = 0;\n for (int i = 0; i < num; i++) {\n scanf(\"%d\", &arr[i]);\n }\n int platform[1000] = {0};\n for (int i = 0; i < num; i++) {\n for (int j = 1; j < len + 1; j++) {\n if (arr[i] == j) {\n platform[j - 1]++;\n break;\n }\n }\n }\n for (int i = 0; i < num; i++) {\n for (int j = 0; j < len; j++) {\n platform[j]--;\n }\n for (int k = 0; k < len; k++) {\n if (platform[k] < 0) {\n stop = 1;\n break;\n }\n }\n if (stop == 1) {\n break;\n }\n count++;\n }\n printf(\"%d\\n\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input);\n let i = input.clone();\n let mut iter1 = i.split_whitespace().map(|x| x.parse::().unwrap());\n let squares = iter1.next().unwrap();\n input = String::new();\n\n io::stdin().read_line(&mut input);\n let iter = input.split_whitespace().map(|x| x.parse::().unwrap());\n let mut counter = vec![0; squares];\n for number in iter {\n counter[number as usize - 1] += 1;\n }\n\n let mut min = 100000;\n for &number in counter.iter() {\n if number < min {\n min = number;\n }\n }\n\n println!(\"{}\", min);\n}", "difficulty": "hard"} {"problem_id": "1533", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There is always an integer in Takahashi's mind.

\n

Initially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is + or -. When he eats +, the integer in his mind increases by 1; when he eats -, the integer in his mind decreases by 1.

\n

The symbols Takahashi is going to eat are given to you as a string S. The i-th character in S is the i-th symbol for him to eat.

\n

Find the integer in Takahashi's mind after he eats all the symbols.

\n
\n
\n
\n
\n

Constraints

    \n
  • The length of S is 4.
  • \n
  • Each character in S is + or -.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the integer in Takahashi's mind after he eats all the symbols.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

+-++\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n
    \n
  • Initially, the integer in Takahashi's mind is 0.
  • \n
  • The first integer for him to eat is +. After eating it, the integer in his mind becomes 1.
  • \n
  • The second integer to eat is -. After eating it, the integer in his mind becomes 0.
  • \n
  • The third integer to eat is +. After eating it, the integer in his mind becomes 1.
  • \n
  • The fourth integer to eat is +. After eating it, the integer in his mind becomes 2.
  • \n
\n

Thus, the integer in Takahashi's mind after he eats all the symbols is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

-+--\n
\n
\n
\n
\n
\n

Sample Output 2

-2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

----\n
\n
\n
\n
\n
\n

Sample Output 3

-4\n
\n
\n
", "c_code": "int solution(void) {\n char S[3];\n scanf(\"%c%c%c%c\", &S[0], &S[1], &S[2], &S[3]);\n int x = 0;\n for (int i = 0; i < 4; i++) {\n if (S[i] == '+') {\n x++;\n } else if (S[i] == '-') {\n x--;\n }\n }\n printf(\"%d\\n \", x);\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 cnt: isize = 0;\n for c in buf.chars() {\n if c == '+' {\n cnt += 1;\n } else if c == '-' {\n cnt -= 1;\n }\n }\n\n println!(\"{}\", cnt);\n}", "difficulty": "easy"} {"problem_id": "1534", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi's house has only one socket.

\n

Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.

\n

One power strip with A sockets can extend one empty socket into A empty sockets.

\n

Find the minimum number of power strips required.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq A \\leq 20
  • \n
  • 1 \\leq B \\leq 20
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B\n
\n
\n
\n
\n
\n

Output

Print the minimum number of power strips required.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 10\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

3 power strips, each with 4 sockets, extend the socket into 10 empty sockets.

\n
\n
\n
\n
\n
\n

Sample Input 2

8 9\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

2 power strips, each with 8 sockets, extend the socket into 15 empty sockets.

\n
\n
\n
\n
\n
\n

Sample Input 3

8 8\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution(void) {\n int mouth = 0;\n int want = 0;\n int ans = 0;\n int ima = 1;\n int flag = 0;\n\n scanf(\"%d\", &mouth);\n scanf(\"%d\", &want);\n\n while (flag == 0) {\n if (ima < want) {\n ima = ima - 1;\n ima = ima + mouth;\n ans++;\n } else {\n flag = 1;\n }\n }\n\n printf(\"%d\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n let v: Vec = s\n .split_whitespace()\n .map(|n| n.parse().ok().unwrap())\n .collect();\n let a = v[0];\n let b = v[1];\n let _s = a - 1;\n let _t = b - 1;\n let mut r: i32 = 0;\n let mut o: i32 = 1;\n while o < b {\n o += a - 1;\n r += 1;\n }\n println!(\"{}\", r);\n}", "difficulty": "medium"} {"problem_id": "1535", "problem_description": "\n
\n\t\t\t\tMax Score: 350 Points
\n
\n

Problem Statement

\n\t\t\t\t\tThere are N buildings along the line. The i-th building from the left is colored in color i, and its height is currently a_i meters.
\n\t\t\t\t\tChokudai is a mayor of the city, and he loves colorful thigs. And now he wants to see at least K buildings from the left.
\n
\n\t\t\t\t\tYou can increase height of buildings, but it costs 1 yens to increase 1 meters. It means you cannot make building that height is not integer.
\n\t\t\t\t\tYou cannot decrease height of buildings.
\n\t\t\t\t\tCalculate the minimum cost of satisfying Chokudai's objective.
\n\t\t\t\t\tNote: \"Building i can see from the left\" means there are no j exists that (height of building j) ≥ (height of building i) and j < i.
\n
\n
\n
\n
\n
\n

Input Format

\n
\nN K\na_1 a_2 a_3 ... a_N\n
\n
\n
\n
\n
\n

Output Format

\n\t\t\t\t\t\tPrint the minimum cost in one line. In the end put a line break.
\n
\n
\n

Constraints

\n
    \n
  • 1 ≤ K ≤ N ≤ 15
  • \n
  • 1 ≤ a_i ≤ 10^9
  • \n
\n
\n
\n

Scoring

\n\t\t\t\t\t\tSubtask 1 [120 points]
\n
    \n
  • N = K
  • \n
\n\t\t\t\t\t\tSubtask 2 [90 points]
\n
    \n
  • N ≤ 5
  • \n
  • a_i ≤ 7
  • \n
\n\t\t\t\t\t\tSubtask 3 [140 points]
\n
    \n
  • There are no additional constraints.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 1

\n
\n5 5\n3949 3774 3598 3469 3424\n
\n
\n
\n

Sample Output 1

\n
\n1541\n
\n\t\t\t\t\tThe optimal solution is (height of buildings from the left) = [3949, 3950, 3951, 3952, 3953].
\n
\n
\n
\n
\n

Sample Input 2

\n
\n5 3\n7 4 2 6 4\n
\n
\n
\n

Sample Output 2

\n
\n7\n
\n\t\t\t\t\tThe optimal solution is (height of buildings from the left) = [7, 8, 2, 9, 4].
\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n int a[15];\n int i;\n int j;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n long long int ans = 999999999999999;\n int bit;\n int a_temp[15];\n int can_see = 0;\n int maxh = 0;\n long long int cost;\n for (bit = 0; bit < (1 << n); ++bit) {\n cost = 0;\n can_see = 0;\n for (i = 0; i < n; i++) {\n a_temp[i] = a[i];\n }\n for (i = 0; i < n; i++) {\n if (bit & (1 << i)) {\n can_see++;\n\n maxh = 0;\n for (j = 0; j <= i - 1; j++) {\n maxh = maxh > a_temp[j] ? maxh : a_temp[j];\n }\n\n if (a_temp[i] < maxh + 1) {\n cost += maxh + 1 - a_temp[i];\n a_temp[i] = maxh + 1;\n }\n }\n }\n if (can_see >= k) {\n ans = ans < cost ? ans : cost;\n }\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n k: usize,\n a: [usize; n]\n }\n let mut ans = usize::max_value();\n for i in 0..1 << n {\n let i: usize = i;\n let q = i.count_ones() as usize;\n if q < k {\n continue;\n }\n let mut c = 0;\n let mut h = a[0];\n for j in 1..n {\n if 1 << j & i > 0 && h >= a[j] {\n c += h - a[j] + 1;\n h += 1;\n } else if h < a[j] {\n h = a[j];\n }\n }\n ans = min(c, ans);\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1536", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

\n

We have a grid of H rows and W columns. Initially, there is a stone in the top left cell. Shik is trying to move the stone to the bottom right cell. In each step, he can move the stone one cell to its left, up, right, or down (if such cell exists). It is possible that the stone visits a cell multiple times (including the bottom right and the top left cell).

\n

You are given a matrix of characters a_{ij} (1 \\leq i \\leq H, 1 \\leq j \\leq W). After Shik completes all moving actions, a_{ij} is # if the stone had ever located at the i-th row and the j-th column during the process of moving. Otherwise, a_{ij} is .. Please determine whether it is possible that Shik only uses right and down moves in all steps.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq H, W \\leq 8
  • \n
  • a_{i,j} is either # or ..
  • \n
  • There exists a valid sequence of moves for Shik to generate the map a.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
H W\na_{11}a_{12}...a_{1W}\n:\na_{H1}a_{H2}...a_{HW}\n
\n
\n
\n
\n
\n

Output

If it is possible that Shik only uses right and down moves, print Possible. Otherwise, print Impossible.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 5\n##...\n.##..\n..##.\n...##\n
\n
\n
\n
\n
\n

Sample Output 1

Possible\n
\n

The matrix can be generated by a 7-move sequence: right, down, right, down, right, down, and right.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 3\n###\n..#\n###\n#..\n###\n
\n
\n
\n
\n
\n

Sample Output 2

Impossible\n
\n
\n
\n
\n
\n
\n

Sample Input 3

4 5\n##...\n.###.\n.###.\n...##\n
\n
\n
\n
\n
\n

Sample Output 3

Impossible\n
\n
\n
", "c_code": "int solution(void) {\n int H;\n int W;\n scanf(\"%d%d\", &H, &W);\n\n char a[H][W + 1];\n for (int i = 0; i < H; i++) {\n scanf(\"%s\", a[i]);\n }\n\n int s[H][W];\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n if (a[i][j] == '#') {\n s[i][j] = 1;\n } else {\n s[i][j] = 0;\n }\n }\n }\n s[0][0] = 0;\n\n int i = 0;\n int j = 0;\n while (1) {\n if (j + 1 < W && s[i][j + 1] == 1) {\n s[i][j + 1] = 0;\n j++;\n continue;\n }\n if (i + 1 < H && s[i + 1][j] == 1) {\n s[i + 1][j] = 0;\n i++;\n continue;\n }\n break;\n }\n\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n if (s[i][j] == 1) {\n printf(\"Impossible\\n\");\n return 0;\n }\n }\n }\n\n printf(\"Possible\\n\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n\n let hw: Vec<_> = s.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n\n let mut xs = vec![vec![false; hw[1]]; hw[0]];\n\n let mut res = true;\n\n 'outer: for i in 0..hw[0] {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n if !res {\n continue;\n }\n\n let cs: Vec<_> = s.trim().chars().collect();\n\n for j in 0..hw[1] {\n if cs[j] == '.' {\n continue;\n }\n\n if i == j && i == 0 {\n xs[i][j] = true;\n continue;\n }\n\n let f1 = if i == 0 { false } else { xs[i - 1][j] };\n\n let f2 = if j == 0 { false } else { xs[i][j - 1] };\n\n res = f1 != f2;\n xs[i][j] = res;\n\n if !res {\n break 'outer;\n }\n }\n }\n\n if res {\n 'outer2: for i in 0..hw[0] {\n for j in 0..hw[1] {\n if i == hw[0] - 1 && j == hw[1] - 1 {\n break 'outer2;\n }\n\n if !xs[i][j] {\n continue;\n }\n\n let f1 = if i == hw[0] - 1 { false } else { xs[i + 1][j] };\n\n let f2 = if j == hw[1] - 1 { false } else { xs[i][j + 1] };\n\n res = f1 != f2;\n if !res {\n break 'outer2;\n }\n }\n }\n }\n\n println!(\"{}\", if res { \"Possible\" } else { \"Impossible\" });\n}", "difficulty": "medium"} {"problem_id": "1537", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Find \\displaystyle{\\sum_{a=1}^{K}\\sum_{b=1}^{K}\\sum_{c=1}^{K} \\gcd(a,b,c)}.

\n

Here \\gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq 200
  • \n
  • K is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K\n
\n
\n
\n
\n
\n

Output

Print the value of \\displaystyle{\\sum_{a=1}^{K}\\sum_{b=1}^{K}\\sum_{c=1}^{K} \\gcd(a,b,c)}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n
\n
\n
\n
\n
\n

Sample Output 1

9\n
\n

\\gcd(1,1,1)+\\gcd(1,1,2)+\\gcd(1,2,1)+\\gcd(1,2,2)\n+\\gcd(2,1,1)+\\gcd(2,1,2)+\\gcd(2,2,1)+\\gcd(2,2,2)\n=1+1+1+1+1+1+1+2=9

\n

Thus, the answer is 9.

\n
\n
\n
\n
\n
\n

Sample Input 2

200\n
\n
\n
\n
\n
\n

Sample Output 2

10813692\n
\n
\n
", "c_code": "int solution(void) {\n int k;\n long long int ans = 0;\n int min = 200;\n scanf(\"%d\", &k);\n\n for (int i = 1; i <= k; i++) {\n for (int j = 1; j <= k; j++) {\n for (int l = 1; l <= k; l++) {\n min = fmin(i, j);\n min = fmin(min, l);\n for (int m = min; m > 0; m--) {\n if (i % m == 0 && j % m == 0 && l % m == 0) {\n ans += m;\n break;\n }\n }\n }\n }\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let result: Vec = vec![\n 1, 9, 30, 76, 141, 267, 400, 624, 885, 1249, 1590, 2208, 2689, 3411, 4248, 5248, 6081,\n 7485, 8530, 10248, 11889, 13687, 15228, 17988, 20053, 22569, 25242, 28588, 31053, 35463,\n 38284, 42540, 46581, 50893, 55362, 61824, 65857, 71247, 76884, 84388, 89349, 97881, 103342,\n 111528, 120141, 128047, 134580, 146316, 154177, 164817, 174438, 185836, 194157, 207927,\n 218812, 233268, 245277, 257857, 268182, 288216, 299257, 313635, 330204, 347836, 362973,\n 383709, 397042, 416448, 434025, 456967, 471948, 499740, 515581, 536073, 559758, 583960,\n 604833, 633651, 652216, 683712, 709065, 734233, 754734, 793188, 818917, 846603, 874512,\n 909496, 933081, 977145, 1006126, 1041504, 1073385, 1106467, 1138536, 1187112, 1215145,\n 1255101, 1295142, 1342852, 1373253, 1422195, 1453816, 1502376, 1553361, 1595437, 1629570,\n 1691292, 1726717, 1782111, 1827492, 1887772, 1925853, 1986837, 2033674, 2089776, 2145333,\n 2197483, 2246640, 2332104, 2379085, 2434833, 2490534, 2554600, 2609625, 2693919, 2742052,\n 2813988, 2875245, 2952085, 3003306, 3096024, 3157249, 3224511, 3306240, 3388576, 3444609,\n 3533637, 3591322, 3693924, 3767085, 3842623, 3912324, 4027884, 4102093, 4181949, 4270422,\n 4361548, 4427853, 4548003, 4616104, 4718640, 4812789, 4918561, 5003286, 5131848, 5205481,\n 5299011, 5392008, 5521384, 5610705, 5739009, 5818390, 5930196, 6052893, 6156139, 6239472,\n 6402720, 6493681, 6623853, 6741078, 6864016, 6953457, 7094451, 7215016, 7359936, 7475145,\n 7593865, 7689630, 7886244, 7984165, 8130747, 8253888, 8403448, 8523897, 8684853, 8802826,\n 8949612, 9105537, 9267595, 9376656, 9574704, 9686065, 9827097, 9997134, 10174780, 10290813,\n 10493367, 10611772, 10813692,\n ];\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let k = st.trim().parse::().unwrap();\n println!(\"{:?}\", result[k - 1]);\n}", "difficulty": "hard"} {"problem_id": "1538", "problem_description": "Polycarp got an array of integers $$$a[1 \\dots n]$$$ as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become $$$a_1=a_2=\\dots=a_n$$$). In one operation, he can take some indices in the array and increase the elements of the array at those indices by $$$1$$$.For example, let $$$a=[4,2,1,6,2]$$$. He can perform the following operation: select indices 1, 2, and 4 and increase elements of the array in those indices by $$$1$$$. As a result, in one operation, he can get a new state of the array $$$a=[5,3,1,7,2]$$$.What is the minimum number of operations it can take so that all elements of the array become equal to each other (that is, to become $$$a_1=a_2=\\dots=a_n$$$)?", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n int a = 0;\n scanf(\"%d\", &a);\n int max = 0;\n int min = 1000000000;\n for (int j = 0; j < a; j++) {\n int temp = 0;\n scanf(\"%d\", &temp);\n if (temp > max) {\n max = temp;\n }\n if (temp < min) {\n min = temp;\n }\n }\n printf(\"%d\\n\", max - min);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(2)\n .step_by(2)\n .flatten()\n .for_each(|s| {\n let (max, min) = s\n .split_ascii_whitespace()\n .flat_map(str::parse::)\n .fold((0usize, usize::MAX), |(max, min), c| {\n (max.max(c), min.min(c))\n });\n let ans = max.wrapping_sub(min);\n writeln!(buf, \"{ans}\").ok();\n });\n}", "difficulty": "hard"} {"problem_id": "1539", "problem_description": "You are given a permutation $$$a_1, a_2, \\ldots, a_n$$$ of size $$$n$$$, where each integer from $$$1$$$ to $$$n$$$ appears exactly once.You can do the following operation any number of times (possibly, zero): Choose any three indices $$$i, j, k$$$ ($$$1 \\le i < j < k \\le n$$$). If $$$a_i > a_k$$$, replace $$$a_i$$$ with $$$a_i + a_j$$$. Otherwise, swap $$$a_j$$$ and $$$a_k$$$. Determine whether you can make the array $$$a$$$ sorted in non-descending order.", "c_code": "int solution() {\n\n int t = 0;\n int a = 0;\n int b[100] = {0};\n\n scanf(\"%d\", &t);\n\n while (t != 0) {\n\n scanf(\"%d\", &a);\n\n for (int i = 0; i < a; i++) {\n scanf(\"%d \", &b[i]);\n }\n\n if (b[0] == 1) {\n printf(\"\\nYes\\n\");\n } else {\n printf(\"\\nNo\\n\");\n }\n\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(2)\n .step_by(2)\n .flatten()\n .for_each(|s| {\n writeln!(buf, \"{}\", if s.starts_with(\"1 \") { \"Yes\" } else { \"No\" }).unwrap_or_default();\n });\n}", "difficulty": "medium"} {"problem_id": "1540", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There are K items placed on a grid of squares with R rows and C columns. Let (i, j) denote the square at the i-th row (1 \\leq i \\leq R) and the j-th column (1 \\leq j \\leq C). The i-th item is at (r_i, c_i) and has the value v_i.

\n

Takahashi will begin at (1, 1), the start, and get to (R, C), the goal. When he is at (i, j), he can move to (i + 1, j) or (i, j + 1) (but cannot move to a non-existent square).

\n

He can pick up items on the squares he visits, including the start and the goal, but at most three for each row. It is allowed to ignore the item on a square he visits.

\n

Find the maximum possible sum of the values of items he picks up.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq R, C \\leq 3000
  • \n
  • 1 \\leq K \\leq \\min(2 \\times 10^5, R \\times C)
  • \n
  • 1 \\leq r_i \\leq R
  • \n
  • 1 \\leq c_i \\leq C
  • \n
  • (r_i, c_i) \\neq (r_j, c_j) (i \\neq j)
  • \n
  • 1 \\leq v_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
R C K\nr_1 c_1 v_1\nr_2 c_2 v_2\n:\nr_K c_K v_K\n
\n
\n
\n
\n
\n

Output

Print the maximum possible sum of the values of items Takahashi picks up.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2 3\n1 1 3\n2 1 4\n1 2 5\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

He has two ways to get to the goal:

\n
    \n
  • Visit (1, 1), (1, 2), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 5 = 8.
  • \n
  • Visit (1, 1), (2, 1), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 4 = 7.
  • \n
\n

Thus, the maximum possible sum of the values of items he picks up is 8.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 5 5\n1 1 3\n2 4 20\n1 2 1\n1 3 4\n1 4 2\n
\n
\n
\n
\n
\n

Sample Output 2

29\n
\n

We have four items in the 1-st row. The optimal choices are as follows:

\n
    \n
  • Visit (1, 1) (1, 2), (1, 3), (1, 4), (2, 4), and (2, 5), in this order, and pick up all items except the one on (1, 2). Then, the total value of the items he picks up will be 3 + 4 + 2 + 20 = 29.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

4 5 10\n2 5 12\n1 5 12\n2 3 15\n1 2 20\n1 1 28\n2 4 26\n3 2 27\n4 5 21\n3 5 10\n1 3 10\n
\n
\n
\n
\n
\n

Sample Output 3

142\n
\n
\n
", "c_code": "int solution() {\n int R;\n int C;\n int K;\n scanf(\"%d %d %d\", &R, &C, &K);\n int i;\n int j;\n int r[200005];\n int c[200005];\n long long int v[200005];\n for (i = 0; i < K; i++) {\n scanf(\"%d %d %lld\", &r[i], &c[i], &v[i]);\n }\n int ind[3003][3003];\n for (i = 0; i < R; i++) {\n for (j = 0; j < C; j++) {\n ind[i][j] = -1;\n }\n }\n for (i = 0; i < K; i++) {\n ind[r[i] - 1][c[i] - 1] = i;\n }\n long long int dp[2][4][3003];\n for (i = 0; i < 4; i++) {\n for (j = 0; j < C; j++) {\n dp[0][i][j] = dp[1][i][j] = 0;\n }\n }\n for (i = 0; i < R; i++) {\n for (j = 0; j < C; j++) {\n dp[1][0][j] = dp[0][0][j];\n if (dp[1][0][j] < dp[0][1][j]) {\n dp[1][0][j] = dp[0][1][j];\n }\n if (dp[1][0][j] < dp[0][2][j]) {\n dp[1][0][j] = dp[0][2][j];\n }\n if (dp[1][0][j] < dp[0][3][j]) {\n dp[1][0][j] = dp[0][3][j];\n }\n dp[1][1][j] = 0;\n dp[1][2][j] = 0;\n if (j > 0) {\n if (dp[1][0][j] < dp[1][0][j - 1]) {\n dp[1][0][j] = dp[1][0][j - 1];\n }\n if (dp[1][1][j] < dp[1][1][j - 1]) {\n dp[1][1][j] = dp[1][1][j - 1];\n }\n if (dp[1][2][j] < dp[1][2][j - 1]) {\n dp[1][2][j] = dp[1][2][j - 1];\n }\n if (dp[1][3][j] < dp[1][3][j - 1]) {\n dp[1][3][j] = dp[1][3][j - 1];\n }\n }\n if (ind[i][j] >= 0) {\n if (dp[1][3][j] < dp[1][2][j] + v[ind[i][j]]) {\n dp[1][3][j] = dp[1][2][j] + v[ind[i][j]];\n }\n if (dp[1][2][j] < dp[1][1][j] + v[ind[i][j]]) {\n dp[1][2][j] = dp[1][1][j] + v[ind[i][j]];\n }\n if (dp[1][1][j] < dp[1][0][j] + v[ind[i][j]]) {\n dp[1][1][j] = dp[1][0][j] + v[ind[i][j]];\n }\n }\n }\n for (j = 0; j < C; j++) {\n dp[0][0][j] = dp[1][0][j];\n dp[0][1][j] = dp[1][1][j];\n dp[0][2][j] = dp[1][2][j];\n dp[0][3][j] = dp[1][3][j];\n }\n }\n long long int ans = dp[0][0][C - 1];\n if (ans < dp[0][1][C - 1]) {\n ans = dp[0][1][C - 1];\n }\n if (ans < dp[0][2][C - 1]) {\n ans = dp[0][2][C - 1];\n }\n if (ans < dp[0][3][C - 1]) {\n ans = dp[0][3][C - 1];\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin()\n .read_to_string(&mut buf)\n .expect(\"fail to read\");\n let mut iter = buf.split_whitespace();\n\n let h: usize = iter.next().unwrap().parse().unwrap();\n let w: usize = iter.next().unwrap().parse().unwrap();\n let k: usize = iter.next().unwrap().parse().unwrap();\n\n let mut dp = vec![vec![vec![0_i64; 4]; w + 1]; h + 1];\n let mut map = vec![vec![None; w + 1]; h + 1];\n\n for _i in 0..k {\n let r: usize = iter.next().unwrap().parse().unwrap();\n let c: usize = iter.next().unwrap().parse().unwrap();\n let v: i64 = iter.next().unwrap().parse().unwrap();\n map[r][c] = Some(v);\n }\n let map = map;\n\n for y in 1..=h {\n for x in 1..=w {\n if let Some(v) = map[y][x] {\n dp[y][x][1] = max(\n dp[y][x][1],\n max(*dp[y - 1][x].iter().max().unwrap() + v, dp[y][x - 1][0] + v),\n );\n dp[y][x][2] = max(dp[y][x][2], dp[y][x - 1][1] + v);\n dp[y][x][3] = max(dp[y][x][3], dp[y][x - 1][2] + v);\n }\n\n dp[y][x][0] = max(*dp[y - 1][x].iter().max().unwrap(), dp[y][x - 1][0]);\n dp[y][x][1] = max(dp[y][x][1], dp[y][x - 1][1]);\n dp[y][x][2] = max(dp[y][x][2], dp[y][x - 1][2]);\n dp[y][x][3] = max(dp[y][x][3], dp[y][x - 1][3]);\n }\n }\n println!(\"{}\", *dp[h][w].iter().max().unwrap());\n}", "difficulty": "medium"} {"problem_id": "1541", "problem_description": "Ashish has a string $$$s$$$ of length $$$n$$$ containing only characters 'a', 'b' and 'c'.He wants to find the length of the smallest substring, which satisfies the following conditions: Length of the substring is at least $$$2$$$ 'a' occurs strictly more times in this substring than 'b' 'a' occurs strictly more times in this substring than 'c' Ashish is busy planning his next Codeforces round. Help him solve the problem.A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n for (int m = 0; m < t; m++) {\n scanf(\"%d\", &n);\n char str[n];\n scanf(\"%s\", str);\n\n int b[n];\n int c[n];\n\n int store[n];\n int k = 0;\n\n int cb = 0;\n int ca = 0;\n int cc = 0;\n\n for (int i = 0; str[i] != '\\0'; i++) {\n if (str[i] == 'b') {\n cb++;\n }\n if (str[i] == 'c') {\n\n cc++;\n }\n if (str[i] == 'a') {\n store[k] = i;\n b[k] = cb;\n c[k] = cc;\n k++;\n cb = 0;\n cc = 0;\n }\n }\n int final = -1;\n for (int i = 0; i < n - 1; i++) {\n if (str[i] == 'a' && str[i + 1] == 'a') {\n final = 2;\n break;\n }\n }\n\n if (final == -1) {\n\n for (int i = 0; i < n - 2; i++) {\n if (str[i] == 'a' && str[i + 2] == 'a') {\n final = 3;\n break;\n }\n }\n }\n\n if (final == -1) {\n for (int i = 0; i < n - 3; i++) {\n if (str[i] == 'a' && str[i + 3] == 'a') {\n int t1 = 0;\n int t2 = 0;\n int t3 = 0;\n for (int w = i + 1; w < i + 3; w++) {\n if (str[w] == 'a') {\n t1++;\n }\n if (str[w] == 'b') {\n t2++;\n }\n if (str[w] == 'c') {\n t3++;\n }\n }\n if (t2 < t1 + 2 && t3 < t1 + 2) {\n final = 4;\n break;\n }\n }\n }\n }\n if (final == -1) {\n for (int i = 0; i < n - 6; i++) {\n if (str[i] == 'a' && str[i + 6] == 'a') {\n int t1 = 0;\n int t2 = 0;\n int t3 = 0;\n for (int w = i + 1; w < i + 6; w++) {\n if (str[w] == 'a') {\n t1++;\n }\n if (str[w] == 'b') {\n t2++;\n }\n if (str[w] == 'c') {\n t3++;\n }\n }\n if (t2 < t1 + 2 && t3 < t1 + 2) {\n final = 7;\n break;\n }\n }\n }\n }\n\n printf(\"%d\\n\", final);\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 let s = lines.next().unwrap();\n let t = s.parse::().unwrap();\n\n 'cases: for _ in 0..t {\n let _n = lines.next().unwrap().parse::().unwrap();\n let s = lines.next().unwrap();\n let s = s.as_bytes();\n\n for k in 2..=7 {\n for w in s.windows(k) {\n let mut na = 0;\n let mut nb = 0;\n let mut nc = 0;\n for &c in w {\n match c {\n b'a' => na += 1,\n b'b' => nb += 1,\n _ => nc += 1,\n }\n }\n if na > nb && na > nc {\n writeln!(&mut output, \"{}\", k).unwrap();\n continue 'cases;\n }\n }\n }\n\n writeln!(&mut output, \"-1\").unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1542", "problem_description": "Phoenix has collected $$$n$$$ pieces of gold, and he wants to weigh them together so he can feel rich. The $$$i$$$-th piece of gold has weight $$$w_i$$$. All weights are distinct. He will put his $$$n$$$ pieces of gold on a weight scale, one piece at a time. The scale has an unusual defect: if the total weight on it is exactly $$$x$$$, it will explode. Can he put all $$$n$$$ gold pieces onto the scale in some order, without the scale exploding during the process? If so, help him find some possible order. Formally, rearrange the array $$$w$$$ so that for each $$$i$$$ $$$(1 \\le i \\le n)$$$, $$$\\sum\\limits_{j = 1}^{i}w_j \\ne x$$$.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n int x = 0;\n scanf(\"%d\", &n);\n scanf(\"%d\", &x);\n int w[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &w[i]);\n }\n int sum = 0;\n int flag = 0;\n for (int i = 0; i < n; i++) {\n sum += w[i];\n if (sum > x) {\n flag = 1;\n break;\n }\n if (sum == x) {\n for (int j = i + 1; j < n; j++) {\n if (w[i] != w[j]) {\n sum -= w[i];\n int temp = 0;\n temp = w[i];\n w[i] = w[j];\n w[j] = temp;\n sum += w[j];\n\n if (sum > x) {\n flag = 1;\n }\n break;\n }\n }\n }\n if (i == n - 1 && sum < x) {\n flag = 1;\n }\n }\n if (flag == 0) {\n printf(\"No\\n\");\n }\n if (flag == 1) {\n printf(\"Yes\\n\");\n }\n if (flag == 1) {\n for (int i = 0; i < n; i++) {\n printf(\"%d \", w[i]);\n }\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n\n std::io::stdin().read_line(&mut buf).expect(\"Failed read\");\n let t: i32 = buf.trim().parse().expect(\"Failed parse\");\n buf = String::new();\n\n for _ in 0..t {\n std::io::stdin().read_line(&mut buf).expect(\"Failed read\");\n let x: i32 = buf.split_whitespace().last().unwrap().parse().unwrap();\n buf = String::new();\n\n std::io::stdin().read_line(&mut buf).expect(\"Failed read\");\n let mut w: Vec = buf\n .split_whitespace()\n .map(|elem| elem.parse().expect(\"Failed parse\"))\n .collect();\n buf = String::new();\n\n if w.iter().sum::() == x {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n let mut cur_sum = 0;\n for i in 0..w.len() {\n if cur_sum + w[i] == x {\n w.swap(i, i + 1);\n }\n print!(\"{} \", w[i]);\n cur_sum += w[i];\n }\n println!();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1543", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

The number 105 is quite special - it is odd but still it has eight divisors.\nNow, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • N is an integer between 1 and 200 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

\n

Print the count.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

105\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.

\n
\n
\n
\n
\n
\n

Sample Input 2

7\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

1 has one divisor. 3, 5 and 7 are all prime and have two divisors. Thus, there is no number that satisfies the condition.

\n
\n
", "c_code": "int solution() {\n int n = 0;\n int qntdiv = 0;\n int divi = 0;\n\n scanf(\"%d\", &n);\n\n for (int i = 1; i <= n; ++i) {\n if (i & 1) {\n for (int j = 1; j <= i; ++j) {\n if (i % j == 0) {\n ++qntdiv;\n if (qntdiv == 8) {\n ++divi;\n }\n }\n }\n qntdiv = 0;\n }\n }\n printf(\"%d\\n\", divi);\n return 0;\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n let mut reader = BufReader::new(reader.lock());\n\n let mut s = String::new();\n\n let _ = reader.read_line(&mut s);\n let n: u32 = s.trim().parse().unwrap();\n let mut result = 0;\n\n for i in 1..n + 1 {\n if i % 2 == 0 {\n continue;\n }\n let mut count = 0;\n for j in 1..i + 1 {\n if i % j == 0 {\n count += 1;\n }\n }\n if count == 8 {\n result += 1;\n }\n }\n println!(\"{}\", result);\n}", "difficulty": "easy"} {"problem_id": "1544", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N people, conveniently numbered 1 through N.\nThey were standing in a row yesterday, but now they are unsure of the order in which they were standing.\nHowever, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person.\nAccording to their reports, the difference above for person i is A_i.

\n

Based on these reports, find the number of the possible orders in which they were standing.\nSince it can be extremely large, print the answer modulo 10^9+7.\nNote that the reports may be incorrect and thus there may be no consistent order.\nIn such a case, print 0.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦N≦10^5
  • \n
  • 0≦A_i≦N-1
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the number of the possible orders in which they were standing, modulo 10^9+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n2 4 4 0 2\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

There are four possible orders, as follows:

\n
    \n
  • 2,1,4,5,3
  • \n
  • 2,5,4,1,3
  • \n
  • 3,1,4,5,2
  • \n
  • 3,5,4,1,2
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

7\n6 4 0 2 4 0 2\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Any order would be inconsistent with the reports, thus the answer is 0.

\n
\n
\n
\n
\n
\n

Sample Input 3

8\n7 5 1 1 7 3 5 3\n
\n
\n
\n
\n
\n

Sample Output 3

16\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int fil[500001] = {0};\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n fil[a[i]]++;\n if (fil[a[i]] > 2) {\n printf(\"0\");\n return 0;\n }\n }\n if (n % 2 == 1 && fil[0] > 1) {\n printf(\"0\");\n return 0;\n }\n long long ans = 1;\n for (int i = 0; i < n / 2; i++) {\n ans *= 2;\n ans %= 1000000007;\n }\n printf(\"%lld\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n N: usize,\n A: [usize; N],\n }\n let mut ans = 1;\n let modulo = 1_000_000_007;\n let mut A = A;\n A.sort();\n if N % 2 == 0 {\n for i in 0..N / 2 {\n if A[i * 2] == i * 2 + 1 && A[i * 2 + 1] == i * 2 + 1 {\n ans *= 2;\n ans %= modulo;\n } else {\n ans = 0;\n break;\n }\n }\n } else {\n for i in 0..(N - 1) / 2 {\n if A[i * 2 + 1] == (i + 1) * 2 && A[i * 2 + 2] == (i + 1) * 2 {\n ans *= 2;\n ans %= modulo;\n } else {\n ans = 0;\n break;\n }\n }\n if A[0] != 0 {\n ans = 0;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1545", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.

\n

First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.

\n

Then, Aoki gave him M queries. The i-th of them is as follows:

\n
    \n
  • Increment the number written at each edge along the path connecting vertices a_i and b_i, by one.
  • \n
\n

After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number.\nHowever, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.

\n

Determine whether there exists a tree that has the property mentioned by Takahashi.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ N ≤ 10^5
  • \n
  • 1 ≤ M ≤ 10^5
  • \n
  • 1 ≤ a_i,b_i ≤ N
  • \n
  • a_i ≠ b_i
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\na_1 b_1\n:\na_M b_M\n
\n
\n
\n
\n
\n

Output

Print YES if there exists a tree that has the property mentioned by Takahashi; print NO otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 4\n1 2\n2 4\n1 3\n3 4\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

For example, Takahashi's graph has the property mentioned by him if it has the following edges: 1-2, 1-3 and 1-4.\nIn this case, the number written at every edge will become 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 5\n1 2\n3 5\n5 1\n3 4\n2 3\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n int M;\n scanf(\"%d %d\", &N, &M);\n int a[M];\n int b[M];\n for (int i = 0; i < M; i++) {\n scanf(\"%d %d\", &a[i], &b[i]);\n }\n\n int C[N + 1];\n for (int i = 1; i <= N; i++) {\n C[i] = 0;\n }\n for (int i = 0; i < M; i++) {\n C[a[i]]++;\n C[b[i]]++;\n }\n for (int i = 1; i <= N; i++) {\n if (C[i] % 2 == 1) {\n printf(\"NO\\n\");\n return 0;\n }\n }\n printf(\"YES\\n\");\n return 0;\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 n = it.next().unwrap().parse::().unwrap();\n let m = it.next().unwrap().parse::().unwrap();\n let mut cnt = vec![0; n];\n for _ in 0..2 * m {\n cnt[it.next().unwrap().parse::().unwrap() - 1] += 1;\n }\n println!(\n \"{}\",\n if cnt.iter().all(|x| x % 2 == 0) {\n \"YES\"\n } else {\n \"NO\"\n }\n );\n}", "difficulty": "easy"} {"problem_id": "1546", "problem_description": "Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up $$$q$$$ questions about this song. Each question is about a subsegment of the song starting from the $$$l$$$-th letter to the $$$r$$$-th letter. Vasya considers a substring made up from characters on this segment and repeats each letter in the subsegment $$$k$$$ times, where $$$k$$$ is the index of the corresponding letter in the alphabet. For example, if the question is about the substring \"abbcb\", then Vasya repeats letter 'a' once, each of the letters 'b' twice, letter 'c\" three times, so that the resulting string is \"abbbbcccbb\", its length is $$$10$$$. Vasya is interested about the length of the resulting string.Help Petya find the length of each string obtained by Vasya.", "c_code": "int solution() {\n int n;\n int q;\n scanf(\"%d %d\", &n, &q);\n char *song = malloc(sizeof(char) * (n + 1));\n scanf(\"%s\", song);\n int count[n + 2];\n char alphabet[27] = \"abcdefghijklmnopqrstuvwxyz\";\n count[0] = 0;\n for (int i = 0; i < n; i++) {\n const char *find = strchr(alphabet, song[i]);\n count[i + 1] = count[i] + (find - alphabet) + 1;\n }\n\n for (int i = 0; i < q; i++) {\n int l;\n int r;\n scanf(\"%d %d\", &l, &r);\n printf(\"%d\\n\", count[r] - count[l - 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 n = it.next().unwrap();\n let q = it.next().unwrap();\n\n let s = lines.next().unwrap();\n let a = s.as_bytes();\n\n let mut t = vec![0; n + 1];\n\n for i in 1..=n {\n let x = (a[i - 1] - (b'a' - 1)) as u64;\n t[i] = t[i - 1] + x;\n }\n\n for _ in 0..q {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let l = it.next().unwrap();\n let r = it.next().unwrap();\n\n let ans = t[r] - t[l - 1];\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1547", "problem_description": "One day Nikita found the string containing letters \"a\" and \"b\" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters \"a\" and the 2-nd contains only letters \"b\".Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?", "c_code": "int solution() {\n char s1[5001];\n scanf(\"%s\", s1);\n int ca[5001];\n int cb[5001];\n memset(ca, 0, sizeof(ca));\n memset(cb, 0, sizeof(cb));\n int i;\n for (i = 0; s1[i]; i++) {\n ca[i + 1] = ca[i];\n cb[i + 1] = cb[i];\n if (s1[i] == 'a') {\n ca[i + 1]++;\n } else {\n cb[i + 1]++;\n }\n }\n int len = i;\n int alla = ca[len];\n int allb = cb[len];\n int max = 0;\n int j;\n for (i = 1; i <= len; i++) {\n for (j = i; j <= len; j++) {\n int nowans = len - (cb[i - 1] + allb - cb[j] + ca[j] - ca[i - 1]);\n if (nowans > max) {\n max = nowans;\n }\n }\n }\n if (alla > max) {\n max = alla;\n }\n if (allb > max) {\n max = allb;\n }\n printf(\"%d\\n\", max);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_line(&mut buf);\n let bs: Vec = once(0)\n .chain(buf.chars().scan(0, |acc, c| match c {\n 'a' => Some(*acc),\n 'b' => {\n *acc += 1;\n Some(*acc)\n }\n _ => None,\n }))\n .collect();\n let n = bs.len() - 1;\n let na = n as i32 - bs[n];\n let _ac = |i: usize| i as i32 - bs[i];\n let res: i32 = (0..n + 1)\n .map(|i| {\n (i..n + 1)\n .map(|j| na - (j - i) as i32 + 2 * (bs[j] - bs[i]))\n .max()\n .unwrap()\n })\n .max()\n .unwrap();\n println!(\"{:?}\", res);\n}", "difficulty": "medium"} {"problem_id": "1548", "problem_description": "\n

Score: 400 points

\n
\n
\n

Problem Statement

\n

There is a cave.

\n

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

\n

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

\n

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

\n
    \n
  • If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.
  • \n
\n

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq M \\leq 2 \\times 10^5
  • \n
  • 1 \\leq A_i, B_i \\leq N\\ (1 \\leq i \\leq M)
  • \n
  • A_i \\neq B_i\\ (1 \\leq i \\leq M)
  • \n
  • One can travel between any two rooms by traversing passages.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N M\nA_1 B_1\n:\nA_M B_M\n
\n
\n
\n
\n
\n

Output

\n

If there is no way to place signposts satisfying the objective, print No.

\n

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \\leq i \\leq N) should contain the integer representing the room indicated by the signpost in Room i.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 4\n1 2\n2 3\n3 4\n4 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n1\n2\n2\n
\n

If we place the signposts as described in the sample output, the following happens:

\n
    \n
  • Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \\to 1. This is the minimum number of passages possible.
  • \n
  • Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \\to 2 \\to 1. This is the minimum number of passages possible.
  • \n
  • Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \\to 2 \\to 1. This is the minimum number of passages possible.
  • \n
\n

Thus, the objective is satisfied.

\n
\n
\n
\n
\n
\n

Sample Input 2

6 9\n3 4\n6 1\n2 4\n5 3\n4 6\n1 5\n6 2\n4 5\n5 6\n
\n
\n
\n
\n
\n

Sample Output 2

Yes\n6\n5\n5\n1\n1\n
\n

If there are multiple solutions, any of them will be accepted.

\n
\n
", "c_code": "int solution() {\n int N;\n int M;\n scanf(\"%d %d\", &N, &M);\n int *node[N + 1];\n int n_edge[N + 1];\n for (int i = 0; i <= N; i++) {\n node[i] = NULL;\n n_edge[i] = 0;\n }\n for (int m = 1; m <= M; m++) {\n int i;\n int j;\n scanf(\"%d %d\", &i, &j);\n n_edge[i]++;\n n_edge[j]++;\n node[i] = (int *)realloc(node[i], sizeof(int) * n_edge[i]);\n node[i][n_edge[i] - 1] = j;\n node[j] = (int *)realloc(node[j], sizeof(int) * n_edge[j]);\n node[j][n_edge[j] - 1] = i;\n }\n\n int start = 1;\n int end = start;\n int vacant = 2;\n int queue[N + 1];\n int state[N + 1];\n for (int i = 1; i <= N; i++) {\n state[i] = -1;\n }\n queue[1] = 1;\n state[1] = 1;\n while (start <= end) {\n for (int current = start; current <= end; current++) {\n int base = queue[current];\n for (int i = 0; i < n_edge[base]; i++) {\n int neighbor = node[base][i];\n if (state[neighbor] < 0) {\n queue[vacant] = neighbor;\n state[neighbor] = base;\n vacant++;\n }\n }\n }\n start = end + 1;\n end = vacant - 1;\n }\n printf(\"Yes\\n\");\n for (int n = 2; n <= N; n++) {\n printf(\"%d\\n\", state[n]);\n }\n for (int i = 0; i <= N; i++) {\n free(node[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n m: usize,\n v: [(usize1, usize1); m]\n }\n let mut g = vec![vec![]; n];\n for &(a, b) in &v {\n g[a].push(b);\n g[b].push(a);\n }\n let mut q = VecDeque::new();\n let mut ans = vec![n; n];\n ans[0] = 0;\n q.push_back(0);\n while let Some(i) = q.pop_front() {\n for &j in &g[i] {\n if ans[j] == n {\n ans[j] = i;\n q.push_back(j);\n }\n }\n }\n println!(\"Yes\");\n for &a in ans.iter().skip(1) {\n println!(\"{}\", a + 1);\n }\n}", "difficulty": "medium"} {"problem_id": "1549", "problem_description": "In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of $$$n$$$ students doing yet another trick. Let's assume that all these students are numbered from $$$1$$$ to $$$n$$$. The teacher came to student $$$a$$$ and put a hole in his badge. The student, however, claimed that the main culprit is some other student $$$p_a$$$.After that, the teacher came to student $$$p_a$$$ and made a hole in his badge as well. The student in reply said that the main culprit was student $$$p_{p_a}$$$.This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.You don't know the first student who was caught by the teacher. However, you know all the numbers $$$p_i$$$. Your task is to find out for every student $$$a$$$, who would be the student with two holes in the badge if the first caught student was $$$a$$$.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int A[n];\n int vis[n];\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &A[i]);\n vis[i] = 0;\n }\n for (int i = 1; i <= n; i++) {\n int j = i;\n while (1) {\n if (vis[j] == 0) {\n vis[j] = 1;\n j = A[j];\n } else {\n printf(\"%d\\t\", j);\n break;\n }\n }\n for (int k = 1; k <= n; k++) {\n vis[k] = 0;\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 handle.read_to_string(&mut buffer).unwrap();\n let arr: Vec = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n let n = *arr.get(0).unwrap();\n for i in 0..n as usize {\n let mut hs = HashSet::new();\n let mut a = i as i32 + 1;\n let mut pa = arr[i + 1];\n hs.insert(a);\n while !hs.contains(&pa) {\n a = pa;\n pa = arr[a as usize];\n hs.insert(a);\n }\n println!(\"{}\", pa);\n }\n}", "difficulty": "medium"} {"problem_id": "1550", "problem_description": "You are given two sequences $$$a_1, a_2, \\dots, a_n$$$ and $$$b_1, b_2, \\dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \\begin{cases} a_i b_i & \\mbox{if }a_i > b_i \\\\ 0 & \\mbox{if }a_i = b_i \\\\ -a_i b_i & \\mbox{if }a_i < b_i \\end{cases}$$$You'd like to make $$$\\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?", "c_code": "int solution() {\n int t;\n int a[3];\n int b[3];\n scanf(\"%d\", &t);\n while (t--) {\n int sum = 0;\n scanf(\"%d%d%d\", &a[0], &a[1], &a[2]);\n scanf(\"%d%d%d\", &b[0], &b[1], &b[2]);\n if (a[2] >= b[1]) {\n sum += b[1] * 2;\n a[2] -= b[1];\n b[1] = 0;\n if (b[2] >= a[0]) {\n b[2] -= a[0];\n a[0] = 0;\n if (b[2] >= a[2]) {\n b[2] -= a[2];\n a[2] = 0;\n sum -= 2 * b[2];\n }\n }\n } else {\n sum += a[2] * 2;\n a[2] = 0;\n b[1] -= a[2];\n if (b[2] >= a[0]) {\n b[2] -= a[0];\n a[0] = 0;\n sum -= b[2] * 2;\n }\n }\n printf(\"%d\\n\", sum);\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 (_x1, y1, z1): (i64, i64, i64) = {\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\n let (x2, y2, _z2): (i64, i64, i64) = {\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\n let ans = min(z1, y2) - max(0, y1 - (x2 + max(0, y2 - z1)));\n println!(\"{}\", ans * 2);\n }\n}", "difficulty": "medium"} {"problem_id": "1551", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Mountaineers Mr. Takahashi and Mr. Aoki recently trekked across a certain famous mountain range.\nThe mountain range consists of N mountains, extending from west to east in a straight line as Mt. 1, Mt. 2, ..., Mt. N.\nMr. Takahashi traversed the range from the west and Mr. Aoki from the east.

\n

The height of Mt. i is h_i, but they have forgotten the value of each h_i.\nInstead, for each i (1 ≤ i ≤ N), they recorded the maximum height of the mountains climbed up to the time they reached the peak of Mt. i (including Mt. i).\nMr. Takahashi's record is T_i and Mr. Aoki's record is A_i.

\n

We know that the height of each mountain h_i is a positive integer.\nCompute the number of the possible sequences of the mountains' heights, modulo 10^9 + 7.

\n

Note that the records may be incorrect and thus there may be no possible sequence of the mountains' heights.\nIn such a case, output 0.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 10^5
  • \n
  • 1 ≤ T_i ≤ 10^9
  • \n
  • 1 ≤ A_i ≤ 10^9
  • \n
  • T_i ≤ T_{i+1} (1 ≤ i ≤ N - 1)
  • \n
  • A_i ≥ A_{i+1} (1 ≤ i ≤ N - 1)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nT_1 T_2 ... T_N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the number of possible sequences of the mountains' heights, modulo 10^9 + 7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1 3 3 3 3\n3 3 2 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

The possible sequences of the mountains' heights are:

\n
    \n
  • 1, 3, 2, 2, 2
  • \n
  • 1, 3, 2, 1, 2
  • \n
  • 1, 3, 1, 2, 2
  • \n
  • 1, 3, 1, 1, 2
  • \n
\n

for a total of four sequences.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n1 1 1 2 2\n3 2 1 1 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

The records are contradictory, since Mr. Takahashi recorded 2 as the highest peak after climbing all the mountains but Mr. Aoki recorded 3.

\n
\n
\n
\n
\n
\n

Sample Input 3

10\n1 3776 3776 8848 8848 8848 8848 8848 8848 8848\n8848 8848 8848 8848 8848 8848 8848 8848 3776 5\n
\n
\n
\n
\n
\n

Sample Output 3

884111967\n
\n

Don't forget to compute the number modulo 10^9 + 7.

\n
\n
\n
\n
\n
\n

Sample Input 4

1\n17\n17\n
\n
\n
\n
\n
\n

Sample Output 4

1\n
\n

Some mountain ranges consist of only one mountain.

\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int T[100001];\n int A[100002];\n scanf(\"%d\", &N);\n for (i = 1; i <= N; i++) {\n scanf(\"%d\", &(T[i]));\n }\n for (i = 1; i <= N; i++) {\n scanf(\"%d\", &(A[i]));\n }\n\n long long ans = 1;\n for (i = 1, T[0] = 0, A[N + 1] = 0; i <= N; i++) {\n if (T[i] < T[i - 1] || A[i] < A[i + 1]) {\n ans = 0;\n break;\n }\n if (T[i] > T[i - 1] || A[i] > A[i + 1]) {\n if ((T[i] > T[i - 1] && A[i] < T[i]) ||\n (A[i] > A[i + 1] && T[i] < A[i])) {\n ans = 0;\n break;\n }\n } else if (T[i] < A[i]) {\n ans = ans * T[i] % 1000000007;\n } else {\n ans = ans * A[i] % 1000000007;\n }\n }\n\n printf(\"%lld\\n\", ans);\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n let n: usize = s.trim().parse().unwrap();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n let xs: Vec = s.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n let ys: Vec = s.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n\n let mut tbs = vec![false; n];\n\n let mut p = 0;\n for i in 0..n {\n if p < xs[i] {\n tbs[i] = true;\n p = xs[i];\n }\n }\n\n let mut abs = vec![false; n];\n\n p = 0;\n for j in 1..(n + 1) {\n let i = n - j;\n\n if p < ys[i] {\n abs[i] = true;\n p = ys[i];\n }\n }\n\n let m = 1000000007;\n\n let mut r = 1;\n for i in 0..n {\n if tbs[i] && abs[i] && xs[i] != ys[i] {\n r = 0;\n break;\n } else if !tbs[i] && !abs[i] {\n r *= cmp::min(xs[i], ys[i]);\n r %= m;\n } else if tbs[i] != abs[i] && ((tbs[i] && xs[i] > ys[i]) || (abs[i] && ys[i] > xs[i])) {\n r = 0;\n break;\n }\n }\n\n println!(\"{}\", r);\n}", "difficulty": "hard"} {"problem_id": "1552", "problem_description": "

路線バスの時刻表

\n\n

\nバスマニアの健次郎君は、A市内のバスをよく利用しています。ある日ふと、健次郎君の家の前のバス停から出発するすべてのバスを写真に収めることを思い立ちました。このバス停には飯盛山行きと鶴ケ城行きの2つのバス路線が通ります。各路線の時刻表は手に入れましたが、1つの時刻表としてまとめた方がバス停で写真が撮りやすくなります。\n

\n\n

\n健次郎君を助けるために、2つの路線の時刻表を、0時0分を基準として出発時刻が早い順に1つの時刻表としてまとめるプログラムを作成してください。\n

\n\n

入力

\n

\n入力は以下の形式で与えられる。\n

\n
\nN h1 m1 h2 m2 ... hN mN\nM k1 g1 k2 g2 ... kM gM\n
\n\n

\n1行目に、飯盛山行きのバスの数を表す整数 N (1 ≤ N ≤ 100) と、それに続いて飯盛山行きのバス出発時刻が早い順に与えられる。出発時刻は整数 hi (0 ≤ hi ≤ 23) と mi (0 ≤ mi ≤ 59) からなり、himi 分に i 番目のバスが出発することを表す。2行目に、鶴ケ城行きのバスの数を表す整数 M (1 ≤ M ≤ 100) と、それに続いて鶴ケ城行きのバス出発時刻が早い順に与えられる。出発時刻は整数 ki (0 ≤ ki ≤ 23) と gi (0 ≤ gi ≤ 59) からなり、kigi 分に i 番目のバスが出発することを表す。同じ路線には、同じ時刻に出発するバスはないものとする。\n

\n\n

出力

\n

\n2つの路線の時刻表を、0時0分を基準として出発時刻が早い順に1つにまとめた時刻表を1行に出力する。ただし、同じ時刻に複数のバスが出発するときは1つだけを出力する。出発時刻の時と分を : で区切る。分が1桁のときは左に0をひとつ加えて2桁で出力する。時刻と時刻の間は空白1 つで区切る。\n

\n\n

入出力例

\n
\n\n

入力例1

\n
\n2 9 8 15 59\n1 8 27\n
\n\n

出力例1

\n
\n8:27 9:08 15:59\n
\n\n

入力例2

\n
\n2 0 0 21 0\n3 7 30 21 0 23 7\n
\n\n

出力例2

\n
\n0:00 7:30 21:00 23:07\n
", "c_code": "int solution(void) {\n int bus[24][60] = {0};\n int j;\n int i;\n int n;\n int m;\n int l;\n int f = 0;\n scanf(\"%d\", &n);\n for (j = 1; j <= n; j++) {\n scanf(\"%d %d\", &m, &l);\n bus[m][l] = 1;\n }\n scanf(\"%d\", &n);\n for (j = 1; j <= n; j++) {\n scanf(\"%d %d\", &m, &l);\n bus[m][l] = 1;\n }\n for (j = 0; j < 24; j++) {\n for (i = 0; i < 60; i++) {\n if (bus[j][i] == 1) {\n if (f == 1) {\n printf(\" %d:%02d\", j, i);\n } else {\n printf(\"%d:%02d\", j, i);\n f = 1;\n }\n }\n }\n }\n puts(\"\");\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 lines = input.split('\\n');\n\n let mut table = vec![];\n\n for line in lines.take(2) {\n let mut iter = line.split(' ').map(|s| s.parse::().unwrap());\n let n = iter.next().unwrap() as usize;\n\n table.reserve(n);\n\n for _ in 0..n {\n let (h, m) = (iter.next().unwrap(), iter.next().unwrap());\n table.push(h * 60 + m);\n }\n }\n\n table.sort();\n table.dedup();\n\n let mut res = String::with_capacity(5 * table.len());\n for v in table {\n write!(res, \"{}:{:02} \", v / 60, v % 60);\n }\n println!(\"{}\", res.trim_end());\n}", "difficulty": "hard"} {"problem_id": "1553", "problem_description": "The flag of Berland is such rectangular field n × m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n × m, consisting of characters 'R', 'G' and 'B'. Output \"YES\" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print \"NO\" (without quotes).", "c_code": "int solution() {\n int n;\n int m;\n int i;\n int j;\n int flag;\n scanf(\"%d%d\", &n, &m);\n char arr[n][m];\n int c[3] = {0};\n getchar();\n int hor = 1;\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n arr[i][j] = getchar();\n if (arr[i][j] == 'R') {\n c[0]++;\n } else if (arr[i][j] == 'G') {\n c[1]++;\n } else {\n c[2]++;\n }\n if (i == 0 && j > 0 && arr[i][j] != arr[0][0]) {\n hor = 0;\n }\n }\n getchar();\n }\n if (c[0] != c[1] || c[0] != c[2] || c[1] != c[2]) {\n printf(\"NO\\n\");\n } else {\n if (!hor) {\n if (m % 3) {\n printf(\"NO\\n\");\n } else {\n flag = 0;\n for (j = 0; j < m && !flag; j++) {\n for (i = 0; i < n; i++) {\n if (arr[i][j] != arr[0][(j * 3 / m) * m / 3]) {\n flag = 1;\n break;\n }\n }\n }\n if (flag) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n } else {\n if (n % 3) {\n printf(\"NO\\n\");\n } else {\n flag = 0;\n for (i = 0; i < n; i++) {\n if (flag) {\n break;\n }\n for (j = 0; j < m; j++) {\n if (arr[i][j] != arr[(i * 3 / n) * n / 3][0]) {\n flag = 1;\n break;\n }\n }\n }\n if (flag) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n }\n }\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 nums: Vec = buf\n .split_whitespace()\n .map(|c| c.parse::().unwrap())\n .collect();\n let width = nums.pop().unwrap();\n let height = nums.pop().unwrap();\n\n let mut buf = String::new();\n let mut flag: Vec> = Vec::new();\n for _ in 0..height {\n io::stdin().read_line(&mut buf).unwrap();\n flag.push(buf.trim().chars().collect::>());\n buf = String::new();\n }\n\n let mut colors: HashSet = ['R', 'G', 'B'].iter().cloned().collect();\n let mut sizes: HashMap = [('R', 0), ('G', 0), ('B', 0)].iter().cloned().collect();\n let mut line_is_same = true;\n let mut color_is_onece = true;\n let mut curr_color = '_';\n for i in 0..height {\n let i = i as usize;\n let color = flag[i][0];\n if let Some(c) = sizes.get_mut(&color) {\n *c += 1;\n };\n\n for j in 1..width {\n let j = j as usize;\n if color != flag[i][j] {\n line_is_same = false;\n break;\n }\n }\n\n if curr_color != color {\n if !colors.remove(&color) {\n color_is_onece = false;\n break;\n }\n curr_color = color;\n }\n\n if !line_is_same || !color_is_onece {\n break;\n }\n }\n\n let sizes: HashSet = sizes.values().cloned().collect();\n\n if line_is_same && color_is_onece && sizes.len() == 1 {\n println!(\"YES\");\n return;\n }\n\n let mut colors: HashSet = ['R', 'G', 'B'].iter().cloned().collect();\n let mut sizes: HashMap = [('R', 0), ('G', 0), ('B', 0)].iter().cloned().collect();\n let mut line_is_same = true;\n let mut color_is_onece = true;\n let mut curr_color = '_';\n for i in 0..width {\n let i = i as usize;\n let color = flag[0][i];\n if let Some(c) = sizes.get_mut(&color) {\n *c += 1;\n };\n\n for j in 1..height {\n let j = j as usize;\n if color != flag[j][i] {\n line_is_same = false;\n break;\n }\n }\n\n if curr_color != color {\n if !colors.remove(&color) {\n color_is_onece = false;\n break;\n }\n curr_color = color;\n }\n\n if !line_is_same || !color_is_onece {\n break;\n }\n }\n\n let sizes: HashSet = sizes.values().cloned().collect();\n\n if line_is_same && color_is_onece && sizes.len() == 1 {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "1554", "problem_description": "Alice has a birthday today, so she invited home her best friend Bob. Now Bob needs to find a way to commute to the Alice's home.In the city in which Alice and Bob live, the first metro line is being built. This metro line contains $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$. Bob lives near the station with number $$$1$$$, while Alice lives near the station with number $$$s$$$. The metro line has two tracks. Trains on the first track go from the station $$$1$$$ to the station $$$n$$$ and trains on the second track go in reverse direction. Just after the train arrives to the end of its track, it goes to the depot immediately, so it is impossible to travel on it after that.Some stations are not yet open at all and some are only partially open — for each station and for each track it is known whether the station is closed for that track or not. If a station is closed for some track, all trains going in this track's direction pass the station without stopping on it.When the Bob got the information on opened and closed stations, he found that traveling by metro may be unexpectedly complicated. Help Bob determine whether he can travel to the Alice's home by metro or he should search for some other transport.", "c_code": "int solution() {\n int n;\n int s;\n int a[1001];\n int b[1001];\n scanf(\"%d %d\", &n, &s);\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d\", &b[i]);\n }\n if (a[1] && a[s]) {\n puts(\"YES\");\n } else {\n for (int i = 1; i <= n; ++i) {\n if (a[1] && a[i] && b[i] && b[s] && i >= s) {\n puts(\"YES\");\n return 0;\n }\n }\n puts(\"NO\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n\n let n_s_array: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|s| s.trim())\n .filter(|s| !s.is_empty())\n .map(|s| s.parse().unwrap())\n .collect();\n\n let a_array: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|s| s.trim())\n .filter(|s| !s.is_empty())\n .map(|s| s.parse().unwrap())\n .collect();\n\n let b_array: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|s| s.trim())\n .filter(|s| !s.is_empty())\n .map(|s| s.parse().unwrap())\n .collect();\n\n let n = (n_s_array[0] as usize) - 1;\n let s = (n_s_array[1] as usize) - 1;\n\n if a_array[0] == 0 {\n println!(\"NO\");\n return;\n }\n\n if s == n {\n if a_array[n] == 0 {\n println!(\"NO\");\n return;\n } else {\n println!(\"YES\");\n return;\n }\n }\n\n if a_array[s] == 0 && b_array[s] == 0 {\n println!(\"NO\");\n return;\n }\n\n if a_array[s] != 0 && b_array[s] != 0 {\n println!(\"YES\");\n return;\n }\n\n if a_array[s] == 0 {\n for i in s..(n + 1) {\n if a_array[i] == 1 && b_array[i] == 1 {\n println!(\"YES\");\n return;\n }\n }\n\n println!(\"NO\");\n return;\n }\n\n println!(\"YES\");\n}", "difficulty": "medium"} {"problem_id": "1555", "problem_description": "\n
\n
\n

Problem Statement

We have N boxes, numbered 1 through N. At first, box 1 contains one red ball, and each of the other boxes contains one white ball.

\n

Snuke will perform the following M operations, one by one. In the i-th operation, he randomly picks one ball from box x_i, then he puts it into box y_i.

\n

Find the number of boxes that may contain the red ball after all operations are performed.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≤N≤10^5
  • \n
  • 1≤M≤10^5
  • \n
  • 1≤x_i,y_i≤N
  • \n
  • x_i≠y_i
  • \n
  • Just before the i-th operation is performed, box x_i contains at least 1 ball.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N M\nx_1 y_1\n:\nx_M y_M\n
\n
\n
\n
\n
\n

Output

Print the number of boxes that may contain the red ball after all operations are performed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n1 2\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Just after the first operation, box 1 is empty, box 2 contains one red ball and one white ball, and box 3 contains one white ball.

\n

Now, consider the second operation. If Snuke picks the red ball from box 2, the red ball will go into box 3. If he picks the white ball instead, the red ball will stay in box 2.\nThus, the number of boxes that may contain the red ball after all operations, is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3\n1 2\n2 3\n2 3\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

All balls will go into box 3.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 4\n1 2\n2 3\n4 1\n3 4\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution(void) {\n\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int x[m];\n int y[m];\n for (int i = 0; i < m; i++) {\n scanf(\"%d %d\", &x[i], &y[i]);\n }\n int box[n];\n for (int i = 0; i < n; i++) {\n box[i] = 1;\n }\n int red[n];\n red[0] = 1;\n for (int i = 1; i < n; i++) {\n red[i] = 0;\n }\n for (int i = 0; i < m; i++) {\n if (red[x[i] - 1] == 1) {\n if (box[x[i] - 1] == 1) {\n red[x[i] - 1] = 0;\n }\n red[y[i] - 1] = 1;\n }\n box[x[i] - 1]--;\n box[y[i] - 1]++;\n }\n int answer = 0;\n for (int i = 0; i < n; i++) {\n if (red[i] == 1) {\n answer++;\n }\n }\n printf(\"%d\\n\", answer);\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 vec2 = vec[0].split(' ').collect::>();\n let n: usize = vec2[0].trim().parse().unwrap();\n let m: usize = vec2[1].trim().parse().unwrap();\n let mut xy: Vec<(usize, usize)> = Vec::new();\n for i in 0..m {\n let vec3 = vec[i + 1].split(' ').collect::>();\n xy.push((\n vec3[0].trim().parse::().unwrap() - 1,\n vec3[1].trim().parse::().unwrap() - 1,\n ));\n }\n let xy = xy;\n\n let mut is_red = vec![false; n];\n is_red[0] = true;\n let mut ball_num = vec![1; n];\n for (x, y) in xy {\n if is_red[x] {\n is_red[y] = true;\n }\n ball_num[x] -= 1;\n ball_num[y] += 1;\n if ball_num[x] == 0 {\n is_red[x] = false;\n }\n }\n let mut red_ball_num = 0;\n for i in 0..n {\n if is_red[i] {\n red_ball_num += 1;\n }\n }\n println!(\"{}\", red_ball_num);\n}", "difficulty": "medium"} {"problem_id": "1556", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the currency of Japan) has started a business in programming education.
\nOne day, there was an exam where a one-year-old child must write a program that prints Hello World, and a two-year-old child must write a program that receives integers A, B and prints A+B.
\nTakahashi, who is taking this exam, suddenly forgets his age.
\nHe decides to write a program that first receives his age N (1 or 2) as input, then prints Hello World if N=1, and additionally receives integers A, B and prints A+B if N=2.
\nWrite this program for him.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • N is 1 or 2.
  • \n
  • A is an integer between 1 and 9 (inclusive).
  • \n
  • B is an integer between 1 and 9 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in one of the following formats:

\n
1\n
\n
2\nA\nB\n
\n
\n
\n
\n
\n

Output

\n

If N=1, print Hello World; if N=2, print A+B.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1\n
\n
\n
\n
\n
\n

Sample Output 1

Hello World\n
\n

As N=1, Takahashi is one year old. Thus, we should print Hello World.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n3\n5\n
\n
\n
\n
\n
\n

Sample Output 2

8\n
\n

As N=2, Takahashi is two years old. Thus, we should print A+B, which is 8 since A=3 and B=5.

\n
\n
", "c_code": "int solution() {\n int number = 0;\n int A = 0;\n int B = 0;\n scanf(\"%d\", &number);\n\n if (number == 1) {\n printf(\"Hello World\");\n } else {\n scanf(\"%d%d\", &A, &B);\n printf(\"%d\", A + B);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n line = line.trim_end().to_owned();\n\n if line == \"1\" {\n println!(\"Hello World\");\n } else if line == \"2\" {\n let mut a_s = String::new();\n let _ = scan.read_line(&mut a_s);\n let mut b_s = String::new();\n let _ = scan.read_line(&mut b_s);\n let a = &a_s.trim_end().to_owned().parse::().unwrap();\n let b = &b_s.trim_end().to_owned().parse::().unwrap();\n println!(\"{}\", a + b);\n }\n}", "difficulty": "easy"} {"problem_id": "1557", "problem_description": "Everyone knows that agents in Valorant decide, who will play as attackers, and who will play as defenders. To do that Raze and Breach decided to play $$$t$$$ matches of a digit game...In each of $$$t$$$ matches of the digit game, a positive integer is generated. It consists of $$$n$$$ digits. The digits of this integer are numerated from $$$1$$$ to $$$n$$$ from the highest-order digit to the lowest-order digit. After this integer is announced, the match starts.Agents play in turns. Raze starts. In one turn an agent can choose any unmarked digit and mark it. Raze can choose digits on odd positions, but can not choose digits on even positions. Breach can choose digits on even positions, but can not choose digits on odd positions. The match ends, when there is only one unmarked digit left. If the single last digit is odd, then Raze wins, else Breach wins.It can be proved, that before the end of the match (for every initial integer with $$$n$$$ digits) each agent has an ability to make a turn, i.e. there is at least one unmarked digit, that stands on a position of required parity.For each of $$$t$$$ matches find out, which agent wins, if both of them want to win and play optimally.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n char s[n];\n scanf(\"%s\", s);\n if (n & 1) {\n int ans = 0;\n for (int i = 0; i < n; i += 2) {\n int x = s[i] - '0';\n if (x & 1) {\n ans = 1;\n break;\n }\n }\n ans ? printf(\"1\\n\") : printf(\"2\\n\");\n } else {\n int ans = 0;\n for (int i = 1; i < n; i += 2) {\n int x = s[i] - '0';\n if (!(x & 1)) {\n ans = 1;\n break;\n }\n }\n ans ? printf(\"2\\n\") : printf(\"1\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut lines = { stdin.lock().lines().map(|l| l.unwrap()) };\n\n let t: usize = lines.next().unwrap().parse().unwrap();\n let mut ds: Vec = Vec::new();\n let mut ns: Vec> = Vec::new();\n for i in 0..t {\n ds.push(lines.next().unwrap().parse().unwrap());\n\n let n = lines.next().unwrap();\n ns.push(Vec::new());\n for c in n.chars() {\n ns[i].push(c.to_digit(10).unwrap() as u8);\n }\n }\n\n for i in 0..t {\n let mut b_wins;\n if ds[i].is_multiple_of(2) {\n b_wins = false;\n for digit in ns[i].iter().skip(1).step_by(2) {\n if digit % 2 == 0 {\n b_wins = true;\n break;\n }\n }\n } else {\n b_wins = true;\n for digit in ns[i].iter().step_by(2) {\n if digit % 2 == 1 {\n b_wins = false;\n break;\n }\n }\n }\n if b_wins {\n println!(\"2\");\n } else {\n println!(\"1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1558", "problem_description": "There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can't use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team.", "c_code": "int solution() {\n int re[3];\n int N = 0;\n re[1] = re[2] = 0;\n scanf(\"%d\", &N);\n while (N--) {\n int tmp = 0;\n scanf(\"%d\", &tmp);\n ++re[tmp];\n }\n int result = re[1];\n\n result = result > re[2] ? re[2] : result;\n\n result = result + (re[1] - result) / 3;\n printf(\"%d\\n\", result);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io::prelude::*;\n std::io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n let n = it.next().unwrap();\n\n let mut d = [0; 2];\n\n for i in it.take(n) {\n match i {\n 1 => d[0] += 1,\n 2 => d[1] += 1,\n _ => panic!(\"Fail.\"),\n }\n }\n\n let ans = if d[0] >= d[1] {\n d[1] + (d[0] - d[1]) / 3\n } else {\n d[0]\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1559", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

AtCoder Mart sells 1000000 of each of the six items below:

\n
    \n
  • Riceballs, priced at 100 yen (the currency of Japan) each
  • \n
  • Sandwiches, priced at 101 yen each
  • \n
  • Cookies, priced at 102 yen each
  • \n
  • Cakes, priced at 103 yen each
  • \n
  • Candies, priced at 104 yen each
  • \n
  • Computers, priced at 105 yen each
  • \n
\n

Takahashi wants to buy some of them that cost exactly X yen in total.\nDetermine whether this is possible.
\n(Ignore consumption tax.)

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq X \\leq 100000
  • \n
  • X is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

\n

If it is possible to buy some set of items that cost exactly X yen in total, print 1; otherwise, print 0.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

615\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

For example, we can buy one of each kind of item, which will cost 100+101+102+103+104+105=615 yen in total.

\n
\n
\n
\n
\n
\n

Sample Input 2

217\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

No set of items costs 217 yen in total.

\n
\n
", "c_code": "int solution() {\n int X = 0;\n int i = 0;\n int j = 0;\n int ans = 0;\n ;\n scanf(\"%d\", &X);\n i = X / 100;\n j = X % 100;\n\n if (i * 5 >= j) {\n ans = 1;\n } else {\n ans = 0;\n }\n printf(\"%d\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let x: u32 = s.trim().parse().ok().unwrap();\n\n let y: u32 = x / 100;\n let z: u32 = (x % 100).div_ceil(5);\n println!(\"{}\", if y >= z { 1 } else { 0 });\n}", "difficulty": "easy"} {"problem_id": "1560", "problem_description": "Polycarp is playing a new computer game. This game has $$$n$$$ stones in a row. The stone on the position $$$i$$$ has integer power $$$a_i$$$. The powers of all stones are distinct.Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more.Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal.For example, if $$$n = 5$$$ and $$$a = [1, 5, 4, 3, 2]$$$, then Polycarp could make the following moves: Destroy the leftmost stone. After this move $$$a = [5, 4, 3, 2]$$$; Destroy the rightmost stone. After this move $$$a = [5, 4, 3]$$$; Destroy the leftmost stone. After this move $$$a = [4, 3]$$$. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: Destroy the leftmost stone. After this move $$$a = [5, 4, 3, 2]$$$; Destroy the leftmost stone. After this move $$$a = [4, 3, 2]$$$. Polycarp destroyed the stones with the greatest and least power, so he can end the game.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int power[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &power[i]);\n }\n\n int min = power[0];\n int max = power[0];\n int m_l = 0;\n int M_l = 0;\n\n for (int i = 1; i < n; i++) {\n if (power[i] < min) {\n min = power[i];\n m_l = i;\n }\n if (power[i] > max) {\n max = power[i];\n M_l = i;\n }\n }\n\n int steps = 0;\n if ((m_l + 1 < (n - M_l)) || (M_l + 1 < (n - m_l))) {\n if ((m_l + 1 < (n - M_l)) && (m_l + 1 < M_l + 1)) {\n steps += m_l + 1;\n\n {\n if (n - M_l > M_l - m_l) {\n steps += M_l - m_l;\n }\n if (n - M_l <= M_l - m_l) {\n steps += n - M_l;\n }\n }\n }\n\n if (M_l + 1 < (n - m_l) && m_l + 1 > M_l + 1) {\n steps += M_l + 1;\n {\n if (n - m_l > m_l - M_l) {\n steps += m_l - M_l;\n }\n if (n - m_l <= m_l - M_l) {\n steps += n - m_l;\n }\n }\n }\n }\n if (m_l + 1 > (n - M_l) || (M_l + 1 > (n - m_l))) {\n if (m_l + 1 > (n - M_l) && n - M_l < n - m_l) {\n steps += n - M_l;\n {\n if (m_l + 1 < M_l - m_l) {\n steps += m_l + 1;\n }\n if (m_l + 1 >= M_l - m_l) {\n steps += M_l - m_l;\n }\n }\n }\n\n if (M_l + 1 > (n - m_l) && n - M_l > n - m_l) {\n steps += n - m_l;\n {\n if (M_l + 1 < m_l - M_l) {\n steps += M_l + 1;\n }\n if (M_l + 1 >= m_l - M_l) {\n steps += m_l - M_l;\n }\n }\n }\n }\n\n if (m_l + 1 == (n - M_l)) {\n if (m_l + 1 < M_l + 1) {\n steps = m_l + 1;\n {\n if (n - M_l > M_l - m_l) {\n steps += M_l - m_l;\n }\n if (n - M_l <= M_l - m_l) {\n steps += n - M_l;\n }\n }\n }\n if (m_l + 1 > M_l + 1) {\n steps = M_l + 1;\n {\n if (n - m_l > m_l - M_l) {\n steps += m_l - M_l;\n }\n if (n - m_l <= m_l - M_l) {\n steps += n - m_l;\n }\n }\n }\n }\n printf(\"%d\\n\", steps);\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 let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let x = it.next().unwrap();\n let mut il = 0;\n let mut xl = x;\n let mut ih = 0;\n let mut xh = x;\n\n for (i, x) in it.enumerate() {\n let i = i + 1;\n if x < xl {\n xl = x;\n il = i;\n }\n if x > xh {\n xh = x;\n ih = i;\n }\n }\n\n let l = std::cmp::min(il, ih);\n let r = std::cmp::max(il, ih);\n\n let ans = [r + 1, n - 1 - l + 1, l + n - 1 - r + 2]\n .iter()\n .min()\n .cloned()\n .unwrap();\n\n writeln!(&mut output, \"{}\", ans).unwrap()\n }\n}", "difficulty": "hard"} {"problem_id": "1561", "problem_description": "\n

Score: 300 points

\n
\n
\n

Problem Statement

\n

In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.
\nThe pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0).

\n

Aoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information:

\n
    \n
  • C_X, C_Y was integers between 0 and 100 (inclusive), and H was an integer not less than 1.
  • \n
  • Additionally, he obtained N pieces of information. The i-th of them is: \"the altitude of point (x_i, y_i) is h_i.\"
  • \n
\n

This was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • N is an integer between 1 and 100 (inclusive).
  • \n
  • x_i and y_i are integers between 0 and 100 (inclusive).
  • \n
  • h_i is an integer between 0 and 10^9 (inclusive).
  • \n
  • The N coordinates (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_N, y_N) are all different.
  • \n
  • The center coordinates and the height of the pyramid can be uniquely identified.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nx_1 y_1 h_1\nx_2 y_2 h_2\nx_3 y_3 h_3\n:\nx_N y_N h_N\n
\n
\n
\n
\n
\n

Output

\n

Print values C_X, C_Y and H representing the center coordinates and the height of the pyramid in one line, with spaces in between.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n2 3 5\n2 1 5\n1 2 5\n3 2 5\n
\n
\n
\n
\n
\n

Sample Output 1

2 2 6\n
\n

In this case, the center coordinates and the height can be identified as (2, 2) and 6.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n0 0 100\n1 1 98\n
\n
\n
\n
\n
\n

Sample Output 2

0 0 100\n
\n

In this case, the center coordinates and the height can be identified as (0, 0) and 100.
\nNote that C_X and C_Y are known to be integers between 0 and 100.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n99 1 191\n100 1 192\n99 0 192\n
\n
\n
\n
\n
\n

Sample Output 3

100 0 193\n
\n

In this case, the center coordinates and the height can be identified as (100, 0) and 193.

\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n\n int p[n][3];\n for (int i = 0; i < n; i++) {\n int x;\n int y;\n int h;\n scanf(\"%d %d %d\", &x, &y, &h);\n p[i][0] = x;\n p[i][1] = y;\n p[i][2] = h;\n }\n\n for (int i = 0; i <= 100; i++) {\n for (int j = 0; j <= 100; j++) {\n int ans_h = -1;\n int cnt = 0;\n int ok = 1;\n int q[n][3];\n for (int l = 0; l < n; l++) {\n int x = p[l][0];\n int y = p[l][1];\n int h = p[l][2];\n if (h == 0) {\n q[cnt][0] = x;\n q[cnt][1] = y;\n q[cnt][2] = h;\n cnt++;\n continue;\n }\n\n int tmp = abs(x - i) + abs(y - j) + h;\n\n if (tmp < 1) {\n ok = 0;\n break;\n }\n\n if (ans_h == -1) {\n ans_h = tmp;\n continue;\n }\n\n if (ans_h != tmp) {\n ok = 0;\n break;\n }\n }\n\n for (int k = 0; k < cnt; k++) {\n int x = q[k][0];\n int y = q[k][1];\n if ((ans_h - abs(x - i) - abs(y - j)) > 0) {\n ok = 0;\n break;\n }\n }\n if (ok) {\n printf(\"%d %d %d\\n\", i, j, ans_h);\n return 0;\n }\n }\n }\n\n return 0;\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 n: usize = iter.next().unwrap().parse().unwrap();\n let mut x = vec![0; n];\n let mut y = vec![0; n];\n let mut h = vec![0; n];\n for i in 0..n {\n x[i] = iter.next().unwrap().parse().unwrap();\n y[i] = iter.next().unwrap().parse().unwrap();\n h[i] = iter.next().unwrap().parse().unwrap();\n }\n\n let mut t = 0;\n for i in 0..n {\n if h[i] > 0 {\n t = i;\n break;\n }\n }\n\n for cx in 0..101 {\n for cy in 0..101 {\n let ch = h[t] + i32::abs(cx - x[t]) + i32::abs(cy - y[t]);\n let mut ok = true;\n for i in 0..n {\n let h2 = std::cmp::max(0, ch - (i32::abs(cx - x[i]) + i32::abs(cy - y[i])));\n if h2 != h[i] {\n ok = false;\n break;\n }\n }\n if ok {\n println!(\"{} {} {}\", cx, cy, ch);\n return;\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1562", "problem_description": "Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?", "c_code": "int solution(void) {\n int n = 0;\n int p = 0;\n int d = 0;\n int o = 0;\n\n int counter1 = 0;\n\n int sum = 0;\n scanf(\"%d\", &n);\n scanf(\"%d\", &p);\n scanf(\"%d\", &d);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &o);\n\n if (o <= p) {\n sum += o;\n }\n if ((sum > d)) {\n counter1++;\n sum = 0;\n }\n }\n\n printf(\"%d\", counter1);\n\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let b;\n let d;\n {\n let mut ns = buf.split(\" \").map(|str| str.trim().parse::().unwrap());\n ns.next();\n b = ns.next().unwrap();\n d = ns.next().unwrap();\n }\n buf.clear();\n io::stdin().read_line(&mut buf).unwrap();\n let oranges = buf.split(\" \").map(|str| str.trim().parse::().unwrap());\n let mut limit = d;\n let mut overflows = 0;\n for orange in oranges {\n if orange > b {\n continue;\n }\n let (n, overflowed) = limit.overflowing_sub(orange);\n if overflowed {\n limit = d;\n overflows += 1;\n } else {\n limit = n;\n }\n }\n println!(\"{}\", overflows);\n}", "difficulty": "hard"} {"problem_id": "1563", "problem_description": "And where the are the phone numbers?You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t.It's guaranteed that the answer exists.Note that the set of letters is a set, not a multiset. For example, the set of letters of abadaba is {a, b, d}.String p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that pi < qi and for all j < i it is satisfied that pj = qj. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abec, afa is not lexicographically smaller than ab and a is not lexicographically smaller than a.", "c_code": "int solution() {\n int a;\n int b;\n scanf(\"%d %d\\n\", &a, &b);\n\n char c[a];\n char v[27];\n char min = 'z';\n char max = 'a';\n\n for (int i = 0; i < 27; i++) {\n v[i] = 0;\n }\n\n for (int i = 0; i < a; i++) {\n scanf(\"%c\", &c[i]);\n if (c[i] < min) {\n min = c[i];\n }\n if (c[i] > max) {\n max = c[i];\n }\n v[(int)c[i] - 'a'] = 1;\n }\n\n if (b > a) {\n for (int i = 0; i < a; i++) {\n printf(\"%c\", c[i]);\n }\n for (int i = 0; i < (b - a); i++) {\n printf(\"%c\", min);\n }\n } else {\n for (int i = b - 1; i >= 0; i--) {\n if (c[i] != max) {\n int j = 1;\n while (!v[((int)c[i] - 'a' + j)]) {\n j++;\n }\n c[i] = (char)((int)c[i] + j);\n break;\n }\n c[i] = min;\n }\n for (int i = 0; i < b; i++) {\n printf(\"%c\", c[i]);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io;\n use std::io::prelude::*;\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n let k: usize = it.next().unwrap().parse().unwrap();\n\n let s = it.next().unwrap().as_bytes();\n\n let mut set = [false; 26];\n\n for &b in s.iter() {\n set[(b - b'a') as usize] = true;\n }\n\n let first = set.iter().position(|&x| x).unwrap() as u8;\n let last = set.iter().rposition(|&x| x).unwrap() as u8;\n\n use std::cmp::Ordering;\n\n match n.cmp(&k) {\n Ordering::Less => {\n let mut ans = s.to_vec();\n for _ in n..k {\n ans.push(first + b'a');\n }\n let ans = std::str::from_utf8(&ans).unwrap();\n\n println!(\"{}\", ans);\n }\n Ordering::Equal | Ordering::Greater => {\n let pos = s[..k].iter().rposition(|&b| b != last + b'a').unwrap();\n let mut ans = s[..k].to_vec();\n let b = ans[pos] - b'a';\n let next = set[b as usize..].iter().skip(1).position(|&x| x).unwrap() as u8 + b + 1;\n ans[pos] = next + b'a';\n for b in ans[pos..].iter_mut().skip(1) {\n *b = first + b'a';\n }\n let ans = std::str::from_utf8(&ans).unwrap();\n println!(\"{}\", ans);\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1564", "problem_description": "A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from $$$1$$$ to $$$9$$$ (inclusive) are round.For example, the following numbers are round: $$$4000$$$, $$$1$$$, $$$9$$$, $$$800$$$, $$$90$$$. The following numbers are not round: $$$110$$$, $$$707$$$, $$$222$$$, $$$1001$$$.You are given a positive integer $$$n$$$ ($$$1 \\le n \\le 10^4$$$). Represent the number $$$n$$$ as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number $$$n$$$ as a sum of the least number of terms, each of which is a round number.", "c_code": "int solution() {\n int n = 0;\n\n scanf(\"%d\", &n);\n for (int c = 0; c < n; c++) {\n int a = 0;\n scanf(\"%d\", &a);\n int re[100] = {0};\n int sum = 0;\n int i = 0;\n for (i = 0; a > 10; i++, a /= 10) {\n int tmp = 1;\n for (int j = 0; j < i; j++) {\n tmp *= 10;\n }\n if (a % 10 != 0) {\n re[sum] = tmp * (a % 10);\n sum++;\n }\n }\n for (int j = 0; j < i; j++) {\n a *= 10;\n }\n re[sum++] = a;\n printf(\"%d\\n\", sum);\n for (int i = 0; i < sum; i++) {\n printf(i == sum - 1 ? \"%d\\n\" : \"%d \", re[i]);\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let lockin = stdin.lock();\n let mut r = BufReader::new(lockin);\n\n let stdout = stdout();\n let lockout = stdout.lock();\n let mut w = BufWriter::new(lockout);\n\n let mut i = String::new();\n r.read_line(&mut i).expect(\"\");\n\n for _ in 0..i.trim().parse::().unwrap() {\n let mut x = String::new();\n r.read_line(&mut x).expect(\"\");\n x = x.trim().to_string();\n\n let mut n: Vec = Vec::with_capacity(x.len());\n\n for (ii, xi) in x.chars().rev().enumerate() {\n if xi != '0' {\n n.push(format!(\"{}{}\", xi, &\"00000\"[0..ii]));\n }\n }\n write!(w, \"{}\\n{}\\n\", n.len(), n.join(\" \")).expect(\"\");\n }\n}", "difficulty": "hard"} {"problem_id": "1565", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Takahashi made N problems for competitive programming.\nThe problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

\n

He is dividing the problems into two categories by choosing an integer K, as follows:

\n
    \n
  • A problem with difficulty K or higher will be for ARCs.
  • \n
  • A problem with difficulty lower than K will be for ABCs.
  • \n
\n

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

\n
\n
\n
\n
\n

Problem Statement

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • N is an even number.
  • \n
  • 1 \\leq d_i \\leq 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nd_1 d_2 ... d_N\n
\n
\n
\n
\n
\n

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\n9 1 4 4 6 7\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.\nThus, the answer is 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

8\n9 1 14 5 5 4 4 14\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

\n
\n
\n
\n
\n
\n

Sample Input 3

14\n99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1\n
\n
\n
\n
\n
\n

Sample Output 3

42685\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int d[100001];\n int a[100001] = {0};\n int x = 0;\n int i;\n int j;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &d[i]);\n a[d[i]]++;\n }\n for (i = 1; i < 100001; i++) {\n if (a[i] > 0) {\n x += a[i];\n }\n if (x >= n / 2) {\n if (x > n / 2) {\n printf(\"0\\n\");\n } else {\n x = 0;\n for (j = i + 1; j < 100001; j++) {\n x++;\n if (a[j] > 0) {\n break;\n }\n }\n printf(\"%d\\n\", x);\n }\n break;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).ok();\n let n = input.trim().parse().unwrap();\n\n let mut input = String::new();\n io::stdin().read_line(&mut input).ok();\n\n let mut v: Vec = Vec::new();\n for z in input.split_whitespace() {\n v.push(z.parse().unwrap());\n }\n\n let mut v2: Vec = Vec::new();\n v.sort();\n\n for z in 0..n {\n let buf;\n if z == 0 {\n buf = v[z];\n } else {\n buf = v[z] - v[z - 1];\n }\n\n v2.push(buf);\n }\n\n println!(\"{}\", v2[n / 2]);\n}", "difficulty": "hard"} {"problem_id": "1566", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given a lowercase English letter c, determine whether it is a vowel. Here, there are five vowels in the English alphabet: a, e, i, o and u.

\n
\n
\n
\n
\n

Constraints

    \n
  • c is a lowercase English letter.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
c\n
\n
\n
\n
\n
\n

Output

If c is a vowel, print vowel. Otherwise, print consonant.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

a\n
\n
\n
\n
\n
\n

Sample Output 1

vowel\n
\n

Since a is a vowel, print vowel.

\n
\n
\n
\n
\n
\n

Sample Input 2

z\n
\n
\n
\n
\n
\n

Sample Output 2

consonant\n
\n
\n
\n
\n
\n
\n

Sample Input 3

s\n
\n
\n
\n
\n
\n

Sample Output 3

consonant\n
\n
\n
", "c_code": "int solution(void) {\n char str = 0;\n scanf(\"%c\", &str);\n if (str == 'a' || str == 'i' || str == 'u' || str == 'e' || str == 'o') {\n printf(\"vowel\");\n } else {\n printf(\"consonant\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let s = s.trim();\n let set: HashSet<&str> = [\"a\", \"e\", \"i\", \"o\", \"u\"].iter().cloned().collect();\n\n println!(\n \"{}\",\n if set.contains(s) {\n \"vowel\"\n } else {\n \"consonant\"\n }\n );\n}", "difficulty": "easy"} {"problem_id": "1567", "problem_description": "

B: 双子 (Twins)

\n\n

とある双子は、自分たちのどちらが兄でどちらが弟かがあまり知られていないことに腹を立てた。

\n

\"ani\" と入力されたら \"square1001\"、\"otouto\" と入力されたら \"e869120\" と出力するプログラムを作りなさい。

\n\n

入力

\n

入力として \"ani\" または \"otouto\" という文字列のどちらかが与えられます。

\n\n

出力

\n

\"e869120\" または \"square1001\" を、問題文の通りに出力してください。最後の改行を忘れないようにしましょう。

\n\n

入力例1

\n
\nani\n
\n\n

出力例1

\n
\nsquare1001\n
\n\n

入力例2

\n
\notouto\n
\n\n

出力例2

\n
\ne869120\n
", "c_code": "int solution(void) {\n char nyuuryoku[128];\n if (scanf(\"%127s\", nyuuryoku) != 1) {\n return 1;\n }\n if (strcmp(nyuuryoku, \"ani\") == 0) {\n puts(\"square1001\");\n } else if (strcmp(nyuuryoku, \"otouto\") == 0) {\n puts(\"e869120\");\n } else {\n puts(\"YOU ARE AN IDIOT!!!!!\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n s : String,\n }\n if s == \"ani\" {\n println!(\"square1001\")\n } else {\n println!(\"e869120\")\n }\n}", "difficulty": "medium"} {"problem_id": "1568", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There is a string S consisting of digits 1, 2, ..., 9.\nLunlun, the Dachshund, will take out three consecutive digits from S, treat them as a single integer X and bring it to her master. (She cannot rearrange the digits.)

\n

The master's favorite number is 753. The closer to this number, the better.\nWhat is the minimum possible (absolute) difference between X and 753?

\n
\n
\n
\n
\n

Constraints

    \n
  • S is a string of length between 4 and 10 (inclusive).
  • \n
  • Each character in S is 1, 2, ..., or 9.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the minimum possible difference between X and 753.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1234567876\n
\n
\n
\n
\n
\n

Sample Output 1

34\n
\n

Taking out the seventh to ninth characters results in X = 787, and the difference between this and 753 is 787 - 753 = 34. The difference cannot be made smaller, no matter where X is taken from.

\n

Note that the digits cannot be rearranged. For example, taking out 567 and rearranging it to 765 is not allowed.

\n

We cannot take out three digits that are not consecutive from S, either. For example, taking out the seventh digit 7, the ninth digit 7 and the tenth digit 6 to obtain 776 is not allowed.

\n
\n
\n
\n
\n
\n

Sample Input 2

35753\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

If 753 itself can be taken out, the answer is 0.

\n
\n
\n
\n
\n
\n

Sample Input 3

1111111111\n
\n
\n
\n
\n
\n

Sample Output 3

642\n
\n

No matter where X is taken from, X = 111, with the difference 753 - 111 = 642.

\n
\n
", "c_code": "int solution(void) {\n\n char k[20];\n\n int dif = 1000;\n\n scanf(\"%s\", k);\n\n for (int i = 0; k[i + 2] != '\\0'; i++) {\n\n if (dif >\n abs((100 * (k[i] - '0') + 10 * (k[i + 1] - '0') + (k[i + 2] - '0')) -\n 753)) {\n dif =\n abs((100 * (k[i] - '0') + 10 * (k[i + 1] - '0') + (k[i + 2] - '0')) -\n 753);\n }\n }\n\n printf(\"%d\", dif);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n let t = 753;\n stdin().read_line(&mut s).unwrap();\n let s: Vec<_> = s.trim().chars().flat_map(|v| v.to_digit(10)).collect();\n let mut result = 753;\n for ((&a, &b), &c) in s.iter().zip(s.iter().skip(1)).zip(s.iter().skip(2)) {\n let mut v = a * 100 + b * 10 + c;\n if v > t {\n v -= t;\n } else {\n v = t - v;\n }\n if v < result {\n result = v;\n }\n }\n println!(\"{}\", result);\n}", "difficulty": "medium"} {"problem_id": "1569", "problem_description": "Phoenix has $$$n$$$ coins with weights $$$2^1, 2^2, \\dots, 2^n$$$. He knows that $$$n$$$ is even.He wants to split the coins into two piles such that each pile has exactly $$$\\frac{n}{2}$$$ coins and the difference of weights between the two piles is minimized. Formally, let $$$a$$$ denote the sum of weights in the first pile, and $$$b$$$ denote the sum of weights in the second pile. Help Phoenix minimize $$$|a-b|$$$, the absolute value of $$$a-b$$$.", "c_code": "int solution() {\n int t;\n int n;\n int p = 0;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d\", &n);\n p = pow(2, (n / 2) + 1) - 2;\n printf(\"%d\\n\", p);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n io::stdin().read_line(&mut input_str).expect(\"err\");\n let t = input_str.trim().parse::().unwrap();\n for _ in 0..t {\n input_str.clear();\n io::stdin().read_line(&mut input_str).expect(\"err\");\n let n = input_str.trim().parse::().unwrap();\n let mut s1: i64 = 0;\n let mut s2: i64 = 0;\n for i in 0..n / 2 - 1 {\n s1 += 2 << i;\n }\n for i in n / 2 - 1..n - 1 {\n s2 += 2 << i;\n }\n s1 += 2 << (n - 1);\n println!(\"{}\", (s2 - s1).abs())\n }\n}", "difficulty": "medium"} {"problem_id": "1570", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given a image with a height of H pixels and a width of W pixels.\nEach pixel is represented by a lowercase English letter.\nThe pixel at the i-th row from the top and j-th column from the left is a_{ij}.

\n

Put a box around this image and output the result. The box should consist of # and have a thickness of 1.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ H, W ≤ 100
  • \n
  • a_{ij} is a lowercase English letter.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\na_{11} ... a_{1W}\n:\na_{H1} ... a_{HW}\n
\n
\n
\n
\n
\n

Output

Print the image surrounded by a box that consists of # and has a thickness of 1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3\nabc\narc\n
\n
\n
\n
\n
\n

Sample Output 1

#####\n#abc#\n#arc#\n#####\n
\n
\n
\n
\n
\n
\n

Sample Input 2

1 1\nz\n
\n
\n
\n
\n
\n

Sample Output 2

###\n#z#\n###\n
\n
\n
", "c_code": "int solution() {\n char a[100][100];\n int n = 0;\n int m = 0;\n int i = 0;\n scanf(\"%d%d\", &n, &m);\n\n for (int j = 0; j < n; j++) {\n scanf(\"%s\", a[j]);\n }\n for (i = 0; i < m + 2; i++) {\n printf(\"#\");\n }\n printf(\"\\n\");\n for (i = 0; i < n; i++) {\n printf(\"#\");\n for (int j = 0; j < m; j++) {\n printf(\"%c\", a[i][j]);\n }\n printf(\"#\\n\");\n }\n for (i = 0; i < m + 2; i++) {\n printf(\"#\");\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let split: Vec<&str> = input.as_str().split_whitespace().collect();\n\n let h: i64 = split[0].parse().unwrap();\n let w: i64 = split[1].parse().unwrap();\n\n for _n in 0..w + 2 {\n print!(\"#\");\n }\n\n println!();\n\n for _n in 0..h {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let pict = input.as_str();\n println!(\"#{}#\", pict.replace(\"\\n\", \"\"));\n }\n\n for _n in 0..w + 2 {\n print!(\"#\");\n }\n}", "difficulty": "medium"} {"problem_id": "1571", "problem_description": "

問題 2:  投票 (Vote)\n

\n
\n\n

問題

\n\n

\n20XX年に東京で世界的なスポーツ大会が開かれることになった.プログラミングコンテストはスポーツとして世界で楽しまれており,競技として採用される可能性がある.採用される競技を決める審査委員会について調べたところ,次のようなことが分かった.\n

\n
    \n
  • \n審査委員会のために,候補となる N 個の競技を面白い方から順番に並べたリストが作成された.リストの上から i 番目には i 番目に面白い競技が書かれている.それを競技 i とする.さらに競技 i の開催に必要な費用 Ai が書かれている.\n
  • \n
  • また,審査委員会は委員 1 から委員 M までの M 人の委員で構成されている.委員 j は自分の審査基準 Bj をもっており,開催に必要な費用が Bj 以下の競技のうち最も面白いものに 1 票を投票した.\n
  • \n
  • \nどの委員の審査基準に対しても,少なくとも 1 つの競技は開催に必要な費用が審査基準以下であった.したがって,委員は全員 1 票を投票した.\n
  • \n
  • \n最も多く票を獲得した競技は 1 つだけであった.\n
  • \n
\n\n

\n競技のリストと委員の情報が与えられたとき,最も多く票を獲得した競技の番号を求めるプログラムを作成せよ.\n

\n\n\n

入力

\n

\n入力は 1 + N + M 行からなる.\n

\n\n

\n1 行目には整数 N, M (1 ≤ N ≤ 1000,1 ≤ M ≤ 1000) が書かれており,それぞれ競技の数,委員の数を表す.\n

\n\n

\n続く N 行のうちの i 行目 (1 ≤ i ≤ N) には整数 Ai (1 ≤ Ai ≤ 1000) が書かれており, 競技 i の開催に必要な費用 Ai を表す.\n

\n\n

\n続く M 行のうちの j 行目 (1 ≤ j ≤ M) には整数 Bj (1 ≤ Bj ≤ 1000) が書かれており,委員 j の審査基準 Bj を表す.\n

\n\n

\n与えられる入力データにおいては,どの委員も必ず 1 票を投票し,最も多く票を獲得した競技は 1 つであることが保証されている.\n

\n\n\n

出力

\n

\n最も多く票を獲得した競技の番号を 1 行で出力せよ.\n

\n\n

入出力例

\n\n

入力例 1

\n\n
\n4 3\n5\n3\n1\n4\n4\n3\n2\n
\n\n

出力例 1

\n
\n2\n
\n\n

\n入出力例 1 では,競技は 4 つあり,委員は 3 人いる.リストの 4 つの競技にかかる費用はそれぞれ 5, 3, 1, 4 である.\n

\n
    \n
  • 委員 1 の審査基準は 4 である.費用が 4 以下の競技のうち最も面白いものは競技 2 である.
  • \n
  • 委員 2 の審査基準は 3 である.費用が 3 以下の競技のうち最も面白いものは競技 2 である.
  • \n
  • 委員 3 の審査基準は 2 である.費用が 2 以下の競技のうち最も面白いものは競技 3 である.
  • \n
\n\n

\nよって,競技 2 が 2 票,競技 3 が 1 票を獲得する.最も多く票を獲得した競技は競技 2 であるので,2 を出力する.\n

\n\n\n

入力例 2

\n\n
\n6 6\n3\n1\n4\n1\n5\n9\n2\n6\n5\n3\n5\n9\n
\n\n

出力例 2

\n\n
\n1\n
\n\n

\n入出力例 2 では,競技 1 が 5 票,競技 2 が 1 票を獲得する.最も多く票を獲得した競技は競技 1 なので,1 を出力する.\n

\n\n
\n

\n問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。\n

\n
", "c_code": "int solution(void) {\n int n = 0;\n int m = 0;\n int i = 0;\n int j = 0;\n int a[1001] = {0};\n int b[1001] = {0};\n int hyou[1001] = {0};\n int sum = 0;\n int k = 0;\n scanf(\"%d%d\", &n, &m);\n for (i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (j = 1; j <= m; j++) {\n scanf(\"%d\", &b[j]);\n }\n for (j = 1; j <= m; j++) {\n for (i = 1; i <= n; i++) {\n if (b[j] >= a[i]) {\n hyou[i]++;\n break;\n }\n }\n }\n for (i = 0; i < n; i++) {\n if (sum < hyou[i]) {\n sum = hyou[i];\n k = i;\n }\n }\n printf(\"%d\\n\", k);\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, m) = {\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 list: Vec<(u32, u32)> = lines\n .by_ref()\n .take(n)\n .map(|l| (l.parse().unwrap(), 0))\n .collect();\n\n for line in lines.take(m) {\n let b = line.parse().unwrap();\n let &mut (_, ref mut n) = list.iter_mut().find(|&&mut (i, _)| i <= b).unwrap();\n *n += 1;\n }\n\n let (i, _) = list\n .into_iter()\n .enumerate()\n .max_by_key(|&(_, (_, n))| n)\n .unwrap();\n\n println!(\"{}\", i + 1);\n}", "difficulty": "medium"} {"problem_id": "1572", "problem_description": "

Distance


\n\n

\n Write a program which calculates the distance between two points P1(x1, y1) and P2(x2, y2).\n

\n\n\n

Input

\n\n

\n Four real numbers x1, y1, x2 and y2 are given in a line.\n

\n\n\n

Output

\n\n

\n Print the distance in real number. The output should not contain an absolute error greater than 10-4.\n

\n\n

Sample Input

\n\n
\n0 0 1 1\n
\n\n

Sample Output

\n\n
\n1.41421356\n
", "c_code": "int solution() {\n double x1 = 0;\n double x2 = 0;\n double y1 = 0;\n double y2 = 0;\n double x = 0;\n double y = 0;\n\n scanf(\"%lf %lf %lf %lf\", &x1, &y1, &x2, &y2);\n x = (x2 - x1) * (x2 - x1);\n y = (y2 - y1) * (y2 - y1);\n\n printf(\"%lf\\n\", sqrt(x + y));\n\n return (0);\n}", "rust_code": "fn solution() {\n let handle = std::io::stdin();\n let mut s = String::new();\n handle.lock().read_line(&mut s).unwrap();\n let points: Vec = s.split_whitespace().map(|a| a.parse().unwrap()).collect();\n let (ax, ay, bx, by) = (points[0], points[1], points[2], points[3]);\n println!(\"{}\", ((ax - bx).powi(2) + (ay - by).powi(2)).powf(0.5));\n}", "difficulty": "medium"} {"problem_id": "1573", "problem_description": "Little Dormi received a histogram with $$$n$$$ bars of height $$$a_1, a_2, \\ldots, a_n$$$ for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times: Select an index $$$i$$$ ($$$1 \\le i \\le n$$$) where $$$a_i>0$$$, and assign $$$a_i := a_i-1$$$.Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.Consider the following example where the histogram has $$$4$$$ columns of heights $$$4,8,9,6$$$: The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is $$$4+4+1+3+6 = 18$$$, so if Little Dormi does not modify the histogram at all, the ugliness would be $$$18$$$.However, Little Dormi can apply the operation once on column $$$2$$$ and twice on column $$$3$$$, resulting in a histogram with heights $$$4,7,7,6$$$: Now, as the total vertical length of the outline (red lines) is $$$4+3+1+6=14$$$, the ugliness is $$$14+3=17$$$ dollars. It can be proven that this is optimal.", "c_code": "int solution() {\n long long int testcases;\n scanf(\"%lld\", &testcases);\n while (testcases--) {\n long long int n;\n scanf(\"%lld\", &n);\n long long int arrrr[n + 2];\n arrrr[0] = 0;\n arrrr[n + 1] = 0;\n for (int i = 1; i <= n; i++) {\n scanf(\"%lld\", &arrrr[i]);\n }\n long long int count = 0;\n for (long long int i = 1; i <= n; i++) {\n if (arrrr[i] > arrrr[i - 1] && arrrr[i] > arrrr[i + 1]) {\n if (arrrr[i - 1] > arrrr[i + 1]) {\n count = count + arrrr[i] - arrrr[i - 1];\n arrrr[i] = arrrr[i - 1];\n } else {\n count = count + arrrr[i] - arrrr[i + 1];\n arrrr[i] = arrrr[i + 1];\n }\n }\n }\n for (long long int i = 1; i <= n + 1; i++) {\n if (arrrr[i] - arrrr[i - 1] >= 0) {\n count += (arrrr[i] - arrrr[i - 1]);\n } else {\n count += (arrrr[i - 1] - arrrr[i]);\n }\n }\n printf(\"%lld\\n\", count);\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 s = lines.next().unwrap();\n let it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let mut ans = 0u64;\n let mut x = 0;\n let mut y = 0;\n\n for z in it.chain(std::iter::once(0)) {\n let o = z.abs_diff(y);\n\n ans += o;\n if x < y && z < y {\n let m = std::cmp::max(x, z);\n ans -= y - m;\n }\n x = y;\n y = z;\n }\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1574", "problem_description": "Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink \"Beecola\", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of \"Beecola\".", "c_code": "int solution(void) {\n int n;\n int b[1000001] = {0};\n int tmp;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &tmp), b[tmp]++;\n }\n for (int i = 1; i < 100001; i++) {\n b[i] += b[i - 1];\n }\n scanf(\"%d\", &n);\n while (n--) {\n scanf(\"%d\", &tmp), printf(\"%d\\n\", b[tmp <= 100000 ? tmp : 100000]);\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: i32 = n.trim().parse().unwrap();\n\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n\n let mut x: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n x.sort();\n\n let mut q = String::new();\n stdin().read_line(&mut q).unwrap();\n let q: i32 = q.trim().parse().unwrap();\n\n for _ in 0..q {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let line: i32 = line.trim().parse().unwrap();\n\n let mut min: i32 = -1;\n let mut max = n;\n\n while max - min > 1 {\n let current = (min + max) / 2;\n if x[current as usize] > line {\n max = current;\n } else {\n min = current;\n }\n }\n\n println!(\"{}\", max);\n }\n}", "difficulty": "hard"} {"problem_id": "1575", "problem_description": "Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's \"Black Square\", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.", "c_code": "int solution() {\n char a[101][101];\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int minx = m;\n int miny = n;\n int maxx = 0;\n int maxy = 0;\n int count = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", a[i]);\n }\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (a[i][j] == 'B') {\n\n if (i >= maxy) {\n maxy = i;\n }\n if (j >= maxx) {\n maxx = j;\n }\n if (i <= miny) {\n miny = i;\n }\n if (j <= minx) {\n minx = j;\n }\n count++;\n }\n }\n }\n if (count == 0) {\n printf(\"1\\n\");\n return 0;\n }\n\n int length = maxx - minx + 1;\n int breadth = maxy - miny + 1;\n int side = (length > breadth) ? length : breadth;\n if (side > n || side > m) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d\\n\", (side * side) - count);\n }\n}", "rust_code": "fn solution() {\n let (n, m) = {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let mut it = input\n .split_whitespace()\n .map(|k| k.parse::().unwrap());\n (it.next().unwrap(), it.next().unwrap())\n };\n\n let br = BufReader::new(stdin());\n\n let arr = br\n .lines()\n .map(|l| {\n l.unwrap()\n .chars()\n .map(|c| match c {\n 'W' => false,\n 'B' => true,\n _ => panic!(\"HEEEEELP!!!\"),\n })\n .collect::>()\n })\n .collect::>();\n\n let cnt: usize = arr.iter().map(|v| v.iter().filter(|&b| *b).count()).sum();\n\n if cnt == 0 {\n println!(\"{}\", 1);\n } else {\n let max_x = arr\n .iter()\n .filter_map(|row| {\n row.iter()\n .enumerate()\n .filter_map(|(i, &b)| if b { Some(i) } else { None })\n .max()\n })\n .max()\n .unwrap();\n let min_x = arr\n .iter()\n .filter_map(|row| {\n row.iter()\n .enumerate()\n .filter_map(|(i, &b)| if b { Some(i) } else { None })\n .min()\n })\n .min()\n .unwrap();\n let max_y = arr\n .iter()\n .enumerate()\n .filter_map(|(i, row)| {\n row.iter()\n .filter_map(|&b| if b { Some(i) } else { None })\n .max()\n })\n .max()\n .unwrap();\n let min_y = arr\n .iter()\n .enumerate()\n .filter_map(|(i, row)| {\n row.iter()\n .filter_map(|&b| if b { Some(i) } else { None })\n .min()\n })\n .min()\n .unwrap();\n\n let max = max(max_x - min_x, max_y - min_y) + 1;\n\n if max > min(n, m) {\n println!(\"-1\");\n } else {\n println!(\"{}\", max.pow(2) - cnt);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1576", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

\n

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

\n
    \n
  • a < b + c
  • \n
  • b < c + a
  • \n
  • c < a + b
  • \n
\n

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 3 \\leq N \\leq 2 \\times 10^3
  • \n
  • 1 \\leq L_i \\leq 10^3
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nL_1 L_2 ... L_N\n
\n
\n
\n
\n
\n

Constraints

Print the number of different triangles that can be formed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n3 4 2 1\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 1000 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

No triangles can be formed.

\n
\n
\n
\n
\n
\n

Sample Input 3

7\n218 786 704 233 645 728 389\n
\n
\n
\n
\n
\n

Sample Output 3

23\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n int L[2000];\n scanf(\"%d\", &N);\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &L[i]);\n }\n int count = 0;\n for (int i = 0; i < N; i++) {\n for (int j = 0; j < i; j++) {\n for (int k = 0; k < j; k++) {\n if (L[i] < L[j] + L[k] && L[j] < L[k] + L[i] && L[k] < L[i] + L[j]) {\n ++count;\n }\n }\n }\n }\n printf(\"%d\\n\", count);\n return 0;\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 mut l_s = buf\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n l_s.sort();\n\n let mut ans = 0usize;\n\n for a in 0..l_s.len() {\n for b in (a + 1)..l_s.len() {\n let c_over = l_s[a] + l_s[b];\n\n let mut l = b;\n let mut r = l_s.len();\n let mut mid = (l + r) / 2;\n\n while r - l > 1 {\n if l_s[mid] < c_over {\n l = mid;\n } else {\n r = mid;\n }\n mid = (l + r) / 2;\n }\n\n ans += mid - b;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1577", "problem_description": "Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negative integer x.You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is maximum possible. If there are multiple such numbers find the smallest of them.", "c_code": "int solution(void) {\n int n = 0;\n long long l = 0;\n long long r = 0;\n\n scanf(\"%d\", &n);\n\n while (n--) {\n scanf(\"%lli %lli\", &l, &r);\n\n while ((l | (l + 1)) <= r) {\n l = l | (l + 1);\n }\n printf(\"%lli\\n\", l);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let n: u16 = buffer.trim().parse().expect(\"invalid input\");\n\n for _ in 0..n {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let mut input = input\n .split_whitespace()\n .map(|n| n.parse::().expect(\"invalid input\"));\n let l = input.next().unwrap();\n let r = input.next().unwrap();\n\n let mut inc = 1;\n let mut result = l;\n while result | inc <= r {\n result |= inc;\n inc <<= 1;\n }\n println!(\"{}\", result);\n }\n}", "difficulty": "medium"} {"problem_id": "1578", "problem_description": "PizzaForces is Petya's favorite pizzeria. PizzaForces makes and sells pizzas of three sizes: small pizzas consist of $$$6$$$ slices, medium ones consist of $$$8$$$ slices, and large pizzas consist of $$$10$$$ slices each. Baking them takes $$$15$$$, $$$20$$$ and $$$25$$$ minutes, respectively.Petya's birthday is today, and $$$n$$$ of his friends will come, so he decided to make an order from his favorite pizzeria. Petya wants to order so much pizza that each of his friends gets at least one slice of pizza. The cooking time of the order is the total baking time of all the pizzas in the order.Your task is to determine the minimum number of minutes that is needed to make pizzas containing at least $$$n$$$ slices in total. For example: if $$$12$$$ friends come to Petya's birthday, he has to order pizzas containing at least $$$12$$$ slices in total. He can order two small pizzas, containing exactly $$$12$$$ slices, and the time to bake them is $$$30$$$ minutes; if $$$15$$$ friends come to Petya's birthday, he has to order pizzas containing at least $$$15$$$ slices in total. He can order a small pizza and a large pizza, containing $$$16$$$ slices, and the time to bake them is $$$40$$$ minutes; if $$$300$$$ friends come to Petya's birthday, he has to order pizzas containing at least $$$300$$$ slices in total. He can order $$$15$$$ small pizzas, $$$10$$$ medium pizzas and $$$13$$$ large pizzas, in total they contain $$$15 \\cdot 6 + 10 \\cdot 8 + 13 \\cdot 10 = 300$$$ slices, and the total time to bake them is $$$15 \\cdot 15 + 10 \\cdot 20 + 13 \\cdot 25 = 750$$$ minutes; if only one friend comes to Petya's birthday, he can order a small pizza, and the time to bake it is $$$15$$$ minutes.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n long long int n = 0;\n scanf(\"%lld\", &n);\n if (n <= 6) {\n printf(\"15\\n\");\n continue;\n }\n if (n % 2 == 0) {\n printf(\"%lld\\n\", (n / 2) * 5);\n } else {\n printf(\"%lld\\n\", ((n / 2) + 1) * 5);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut inPut = String::new();\n\n io::stdin()\n .read_line(&mut inPut)\n .expect(\"Failed to read line\");\n let mut inPutInt: i32 = inPut.trim().parse().unwrap();\n inPut.clear();\n let mut out: i128 = 0;\n while inPutInt != 0 {\n io::stdin()\n .read_line(&mut inPut)\n .expect(\"Failed to read line\");\n\n let mut frens: i128 = inPut.trim().parse().unwrap();\n inPut.clear();\n inPutInt -= 1;\n\n if frens < 6 {\n out = 15\n } else {\n frens = (frens + frens % 2) / 2;\n out = frens * 5;\n }\n println!(\"{}\", out);\n }\n}", "difficulty": "easy"} {"problem_id": "1579", "problem_description": "Bubble sort", "c_code": "void solution(int *arr, int size) {\n int i, j;\n\n for (i = 0; i < size - 1; i++) {\n bool swapped = false;\n\n for (j = 0; j < size - 1 - i; j++) {\n\n if (arr[j] > arr[j + 1]) {\n int temp;\n\n temp = arr[j];\n arr[j] = arr[j + 1];\n arr[j + 1] = temp;\n\n swapped = true;\n }\n }\n\n if (!swapped) {\n break;\n }\n }\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n if arr.is_empty() {\n return;\n }\n let mut sorted = false;\n let mut n = arr.len();\n while !sorted {\n sorted = true;\n for i in 0..n - 1 {\n if arr[i] > arr[i + 1] {\n arr.swap(i, i + 1);\n sorted = false;\n }\n }\n n -= 1;\n }\n}\n", "difficulty": "easy"} {"problem_id": "1580", "problem_description": "Boyer moore search", "c_code": "void solution(char *str, char *pattern) {\n int n = strlen(str);\n int m = strlen(pattern);\n\n if (m == 0 || n == 0 || m > n)\n return;\n\n int shift = 0;\n\n int bad_char[256];\n\n for (int i = 0; i < 256; i++)\n bad_char[i] = -1;\n\n for (int i = 0; i < m; i++)\n bad_char[(unsigned char)pattern[i]] = i;\n\n while (shift <= (n - m)) {\n int j = m - 1;\n\n while (j >= 0 && pattern[j] == str[shift + j])\n j--;\n\n if (j < 0) {\n printf(\"--Pattern is found at: %d\\n\", shift);\n\n if (shift + m < n)\n shift += m - bad_char[(unsigned char)str[shift + m]];\n else\n shift += 1;\n } else {\n int bad_shift = j - bad_char[(unsigned char)str[shift + j]];\n\n shift += (bad_shift > 1) ? bad_shift : 1;\n }\n }\n}\n", "rust_code": "fn solution(text: &str, pat: &str) -> Vec {\n let mut positions = Vec::new();\n\n let text_len = text.len() as isize;\n let pat_len = pat.len() as isize;\n\n if text_len == 0 || pat_len == 0 || pat_len > text_len {\n return positions;\n }\n\n let text: Vec = text.chars().collect();\n let pat: Vec = pat.chars().collect();\n\n let mut bad_char_table = HashMap::new();\n for (i, &ch) in pat.iter().enumerate() {\n bad_char_table.insert(ch, i as isize);\n }\n\n let mut shift = 0;\n\n while shift <= text_len - pat_len {\n let mut j = pat_len - 1;\n\n while j >= 0 && pat[j as usize] == text[(shift + j) as usize] {\n j -= 1;\n }\n\n if j < 0 {\n positions.push(shift as usize);\n\n if shift + pat_len >= text_len {\n shift += 1;\n } else {\n let next_ch = text[(shift + pat_len) as usize];\n shift += pat_len - bad_char_table.get(&next_ch).unwrap_or(&-1);\n }\n } else {\n let mis_ch = text[(shift + j) as usize];\n let bad_char_shift = bad_char_table.get(&mis_ch).unwrap_or(&-1);\n\n shift += std::cmp::max(1, j - bad_char_shift);\n }\n }\n\n positions\n}", "difficulty": "easy"} {"problem_id": "1581", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

A string is called a KEYENCE string when it can be changed to keyence by removing its contiguous substring (possibly empty) only once.

\n

Given a string S consisting of lowercase English letters, determine if S is a KEYENCE string.

\n
\n
\n
\n
\n

Constraints

    \n
  • The length of S is between 7 and 100 (inclusive).
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

If S is a KEYENCE string, print YES; otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

keyofscience\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

keyence is an abbreviation of key of science.

\n
\n
\n
\n
\n
\n

Sample Input 2

mpyszsbznf\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 3

ashlfyha\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 4

keyence\n
\n
\n
\n
\n
\n

Sample Output 4

YES\n
\n
\n
", "c_code": "int solution(void) {\n char s[100];\n scanf(\"%s\", s);\n int l = strlen(s);\n\n if ((s[l - 7] == 'k' && s[l - 6] == 'e' && s[l - 5] == 'y' &&\n s[l - 4] == 'e' && s[l - 3] == 'n' && s[l - 2] == 'c' &&\n s[l - 1] == 'e') ||\n (s[0] == 'k' && s[l - 6] == 'e' && s[l - 5] == 'y' && s[l - 4] == 'e' &&\n s[l - 3] == 'n' && s[l - 2] == 'c' && s[l - 1] == 'e') ||\n (s[0] == 'k' && s[1] == 'e' && s[l - 5] == 'y' && s[l - 4] == 'e' &&\n s[l - 3] == 'n' && s[l - 2] == 'c' && s[l - 1] == 'e') ||\n (s[0] == 'k' && s[1] == 'e' && s[2] == 'y' && s[l - 4] == 'e' &&\n s[l - 3] == 'n' && s[l - 2] == 'c' && s[l - 1] == 'e') ||\n (s[0] == 'k' && s[1] == 'e' && s[2] == 'y' && s[3] == 'e' &&\n s[l - 3] == 'n' && s[l - 2] == 'c' && s[l - 1] == 'e') ||\n (s[0] == 'k' && s[1] == 'e' && s[2] == 'y' && s[3] == 'e' &&\n s[4] == 'n' && s[l - 2] == 'c' && s[l - 1] == 'e') ||\n (s[0] == 'k' && s[1] == 'e' && s[2] == 'y' && s[3] == 'e' &&\n s[4] == 'n' && s[5] == 'c' && s[l - 1] == 'e') ||\n (s[0] == 'k' && s[1] == 'e' && s[2] == 'y' && s[3] == 'e' &&\n s[4] == 'n' && s[5] == 'c' && s[6] == 'e'))\n\n {\n printf(\"YES\");\n\n } else {\n printf(\"NO\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let handle = std::io::stdin();\n handle.read_line(&mut buf).unwrap();\n let s: String = buf.trim().to_string();\n let k: String = \"keyence\".to_string();\n let _k_index: Vec = vec![];\n\n let mut left = 0;\n let mut right = s.len() - k.len();\n let mut find = false;\n loop {\n let mut target = s[0..left].to_string();\n target.push_str(&s[right..]);\n if target == k {\n find = true;\n break;\n }\n if right == s.len() {\n break;\n }\n left += 1;\n right += 1;\n }\n if find {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "hard"} {"problem_id": "1582", "problem_description": "You are given a permutation $$$p_1, p_2, \\dots, p_n$$$. Recall that sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.Find three indices $$$i$$$, $$$j$$$ and $$$k$$$ such that: $$$1 \\le i < j < k \\le n$$$; $$$p_i < p_j$$$ and $$$p_j > p_k$$$. Or say that there are no such indices.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n; i++) {\n int flag = 0;\n int size = 0;\n scanf(\"%d\", &size);\n int ind[size];\n for (int j = 0; j < size; j++) {\n scanf(\"%d\", &ind[j]);\n }\n for (int j = 1; j < size - 1; j++) {\n if (ind[j] > ind[j + 1] && ind[j] > ind[j - 1]) {\n printf(\"Yes\\n%d %d %d\\n\", j, j + 1, j + 2);\n flag = 1;\n break;\n }\n }\n if (flag == 0) {\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 input.read_line(&mut line).unwrap();\n line.clear();\n input.read_line(&mut line).unwrap();\n let mut value = i16::MAX;\n let mut first: i16 = 0;\n let mut index: i16 = 0;\n for number in line.split_whitespace() {\n index += 1;\n let number: i16 = number.parse().unwrap();\n if first == 0 {\n if number > value {\n first = index - 1;\n }\n value = number;\n } else {\n if number < value {\n println!(\"YES\\n{} {} {}\", first, index - 1, index);\n first = -1;\n break;\n }\n value = number;\n }\n }\n if first != -1 {\n println!(\"NO\");\n }\n count -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "1583", "problem_description": "You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n\n char str[101] = {0};\n int res = 0;\n int flag = 0;\n scanf(\"%s\", str);\n\n for (int i = 0; str[i] != '\\0'; i++) {\n\n if (str[i] == '1' && str[i + 1] == '0') {\n flag = 1;\n while (str[++i] == '0') {\n\n res++;\n }\n i--;\n }\n }\n for (int j = strlen(str) - 1; flag == 1 && j >= 0; j--) {\n if (str[j] == '1') {\n break;\n }\n res--;\n }\n\n printf(\"%d\\n\", res);\n }\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut input = String::new();\n io::stdin().read_to_string(&mut input)?;\n let input = input.trim();\n\n for line in input.lines().skip(1) {\n let mut erased_0 = 0;\n let mut flag = false;\n let mut prev_digit = '0';\n let line = line.trim_matches('0');\n for digit in line.chars() {\n if flag {\n if digit == '0' {\n erased_0 += 1;\n } else {\n flag = false;\n }\n } else if prev_digit == '1' && digit == '0' {\n erased_0 += 1;\n flag = true;\n }\n prev_digit = digit;\n }\n println!(\"{}\", erased_0);\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1584", "problem_description": "A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).Killjoy's account is already infected and has a rating equal to $$$x$$$. Its rating is constant. There are $$$n$$$ accounts except hers, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th account's initial rating is $$$a_i$$$. Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed.Contests are regularly held on Codeforces. In each contest, any of these $$$n$$$ accounts (including infected ones) can participate. Killjoy can't participate. After each contest ratings are changed this way: each participant's rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.It can be proven that all accounts can be infected in some finite number of contests.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int x;\n scanf(\"%d\", &n);\n scanf(\"%d\", &x);\n int a[n];\n int count = 0;\n int sum = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] == x) {\n count++;\n }\n }\n for (int i = 0; i < n; i++) {\n sum += a[i];\n }\n if (count == n) {\n printf(\"0\\n\");\n } else if (count >= 1 || sum == n * x) {\n printf(\"1\\n\");\n } else {\n printf(\"2\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let mut handle = stdin.lock();\n let mut buf = String::new();\n\n handle.read_line(&mut buf).unwrap();\n let tests: i64 = buf.trim().parse().unwrap();\n\n for _test in 0..tests {\n buf.clear();\n handle.read_line(&mut buf).unwrap();\n let mut input = buf.split_whitespace();\n input.next();\n let joy: i64 = input.next().unwrap().parse().unwrap();\n\n buf.clear();\n handle.read_line(&mut buf).unwrap();\n let maj: Vec = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n let mut An = 0;\n let mut Bn = 0;\n let mut Btotal = 0i64;\n for one in maj {\n if one == joy {\n An += 1;\n } else {\n Bn += 1;\n Btotal += one;\n }\n }\n\n println!(\n \"{}\",\n if Bn == 0 {\n 0\n } else if An != 0 || Btotal % Bn == 0 && Btotal / Bn == joy {\n 1\n } else {\n 2\n }\n );\n }\n}", "difficulty": "hard"} {"problem_id": "1585", "problem_description": "

通学経路

\n\n

問題

\n\n

\n太郎君の住んでいるJOI市は,南北方向にまっすぐに伸びる a 本の道路と,東西方向にまっすぐに伸びる b 本の道路により,碁盤の目の形に区分けされている.\n

\n

\n南北方向の a 本の道路には,西から順に 1, 2, ... , a の番号が付けられている.また,東西方向の b 本の道路には,南から順に 1, 2, ... , b の番号が付けられている.西から i 番目の南北方向の道路と,南から j 番目の東西方向の道路が交わる交差点を (i, j) で表す.\n

\n

\n太郎君は,交差点 (1, 1) の近くに住んでおり,交差点 (a, b) の近くのJOI高校に自転車で通っている.自転車は道路に沿ってのみ移動することができる.太郎君は,通学時間を短くするため,東または北にのみ向かって移動して通学している.\n

\n

\n現在, JOI市では, n 個の交差点 (x1, y1), (x2, y2), ... , (xn, yn) で工事を行っている.太郎君は工事中の交差点を通ることができない.\n

\n

\n太郎君が交差点 (1, 1) から交差点 (a, b) まで,工事中の交差点を避けながら,東または北にのみ向かって移動して通学する方法は何通りあるだろうか.太郎君の通学経路の個数 m を求めるプログラムを作成せよ.\n

\n\n\n

入力

\n\n

\n入力は複数のデータセットからなる.各データセットは以下の形式で与えられる.入力はゼロを2つ含む行で終了する.\n

\n\n

\n1行目には,空白を区切りとして2個の整数 a, b が書かれている.これは,南北方向の道路の本数と,東西方向の道路の本数を表す. a, b は 1 ≤ a, b ≤ 16 をみたす.\n

\n

\n2行目には, 工事中の交差点の個数を表す整数 n が書かれている. n は 1 ≤ n ≤ 40 をみたす.\n

\n

\n続く n 行 (3行目から n+2 行目) には,工事中の交差点の位置が書かれている. i+2 行目には空白で区切られた整数 xi, yi が書かれており,交差点 (xi, yi) が工事中であることを表す. xi, yi は 1 ≤ xi, yi ≤ 16 をみたす. \n

\n\n

\nデータセットの数は 5 を超えない.\n

\n\n

出力

\n

\n\nデータセットごとに, 太郎君の通学経路の個数 m を1行に出力する.\n

\n\n

入出力例

\n\n\n

入力例

\n\n
\n5 4\n3\n2 2\n2 3\n4 2\n5 4\n3\n2 2\n2 3\n4 2\n0 0\n
\n\n

出力例

\n\n
\n5\n5\n
\n\n

下図は a=5, b=4, n=3 で,工事中の交差点が (2,2), (2,3), (4,2) の場合を表している.

\n\n
\n\n
\n\n

\nこの場合,通学経路は m=5 通りある. 5通りの通学経路を全て図示すると,以下の通り.\n

\n\n
\n\n
\n\n\n
\n

\n上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。\n

\n
", "c_code": "int solution() {\n int w;\n int h;\n int i;\n int j;\n int n;\n while (scanf(\"%d %d\", &w, &h), w || h) {\n int f[20][20];\n int d[20][20] = {0};\n for (i = 0; i < 400; i++) {\n f[i / 20][i % 20] = 1;\n }\n scanf(\"%d\", &n);\n while (n--) {\n scanf(\"%d %d\", &i, &j);\n f[j][i] = 0;\n }\n d[1][0] = 1;\n for (i = 1; i <= h; i++) {\n for (j = 1; j <= w; j++) {\n d[i][j] = d[i - 1][j] * f[i - 1][j] + d[i][j - 1] * f[i][j - 1];\n }\n }\n printf(\"%d\\n\", d[h][w]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut buf = String::new();\n stdin.read_to_string(&mut buf);\n let mut iter = buf.split_whitespace();\n\n loop {\n let a: usize = iter.next().unwrap().parse().unwrap();\n let b: usize = iter.next().unwrap().parse().unwrap();\n if (a, b) == (0, 0) {\n break;\n }\n let n: usize = iter.next().unwrap().parse().unwrap();\n let c: Vec<(usize, usize)> = (0..n)\n .map(|_| {\n (\n iter.next().unwrap().parse().unwrap(),\n iter.next().unwrap().parse().unwrap(),\n )\n })\n .collect();\n\n let mut path: Vec> =\n std::iter::repeat_n(std::iter::repeat_n(0, b + 1).collect(), a + 1).collect();\n path[1][1] = 1;\n\n for d in 3..(a + b + 1) {\n for x in 1..min(d, a + 1) {\n let y = d - x;\n if y > b || c.contains(&(x, y)) {\n continue;\n }\n path[x][y] = path[x - 1][y] + path[x][y - 1];\n }\n }\n\n println!(\"{}\", path[a][b]);\n }\n}", "difficulty": "hard"} {"problem_id": "1586", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are taking a computer-based examination. The examination consists of N questions, and the score allocated to the i-th question is s_i. Your answer to each question will be judged as either \"correct\" or \"incorrect\", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.

\n

However, the examination system is actually flawed, and if your grade is a multiple of 10, the system displays 0 as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1 ≤ N ≤ 100
  • \n
  • 1 ≤ s_i ≤ 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\ns_1\ns_2\n:\ns_N\n
\n
\n
\n
\n
\n

Output

Print the maximum value that can be displayed as your grade.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n5\n10\n15\n
\n
\n
\n
\n
\n

Sample Output 1

25\n
\n

Your grade will be 25 if the 10-point and 15-point questions are answered correctly and the 5-point question is not, and this grade will be displayed correctly. Your grade will become 30 if the 5-point question is also answered correctly, but this grade will be incorrectly displayed as 0.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n10\n10\n15\n
\n
\n
\n
\n
\n

Sample Output 2

35\n
\n

Your grade will be 35 if all the questions are answered correctly, and this grade will be displayed correctly.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n10\n20\n30\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

Regardless of whether each question is answered correctly or not, your grade will be a multiple of 10 and displayed as 0.

\n
\n
", "c_code": "int solution() {\n int a;\n int b = 0;\n int c = 100000;\n scanf(\"%d\", &a);\n int d[a];\n for (int i = 0; i < a; i++) {\n scanf(\"%d\", &d[i]);\n b += d[i];\n if (d[i] % 10 != 0 && d[i] < c) {\n c = d[i];\n }\n }\n if (b % 10 != 0) {\n printf(\"%d\", b);\n } else {\n b -= c;\n if (b < 0) {\n b = 0;\n }\n printf(\"%d\", b);\n }\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 s: 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 const M: usize = 10000;\n let mut dp = vec![false; M + 1];\n dp[0] = true;\n for i in 0..N {\n for j in (s[i]..(M + 1)).rev() {\n if dp[j - s[i]] {\n dp[j] = true;\n }\n }\n }\n let ans = (0..(M + 1))\n .rev()\n .find(|&i| dp[i] && i % 10 != 0)\n .unwrap_or(0);\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1587", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi lives in another world. There are slimes (creatures) of 10000 colors in this world. Let us call these colors Color 1, 2, ..., 10000.

\n

Takahashi has N slimes, and they are standing in a row from left to right. The color of the i-th slime from the left is a_i.\nIf two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.

\n

Takahashi can change the color of one slime to any of the 10000 colors by one spell.\nHow many spells are required so that no slimes will start to combine themselves?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 100
  • \n
  • 1 \\leq a_i \\leq N
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of spells required.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1 1 2 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

For example, if we change the color of the second slime from the left to 4, and the color of the fourth slime to 5, the colors of the slimes will be 1, 4, 2, 5, 2, which satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 2 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Although the colors of the first and third slimes are the same, they are not adjacent, so no spell is required.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n1 1 1 1 1\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n

For example, if we change the colors of the second and fourth slimes from the left to 2, the colors of the slimes will be 1, 2, 1, 2, 1, which satisfy the condition.

\n
\n
\n
\n
\n
\n

Sample Input 4

14\n1 2 2 3 3 3 4 4 4 4 1 2 3 4\n
\n
\n
\n
\n
\n

Sample Output 4

4\n
\n
\n
", "c_code": "int solution() {\n\n int n = 0;\n scanf(\"%d\\n\", &n);\n int a[n];\n int i = 0;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int buf = 0;\n int count = 0;\n int ans = 0;\n for (i = 0; i < n; i++) {\n if (buf == a[i]) {\n count++;\n } else {\n buf = a[i];\n ans = ans + count / 2;\n count = 1;\n }\n }\n ans = ans + count / 2;\n printf(\"%d\", ans);\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 buf_it = buf.split_whitespace();\n let N = buf_it.next().unwrap().parse::().unwrap();\n let mut A = (0..N)\n .map(|_| buf_it.next().unwrap().parse::().unwrap())\n .collect::>();\n let mut ans = 0;\n for i in 1..N {\n if A[i - 1] == A[i] {\n ans += 1;\n A[i] = 0;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1588", "problem_description": "Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int flag = 0;\n for (int i = 0; i < n; i++) {\n int temp = sqrt(a[i]);\n if (a[i] != temp * temp) {\n printf(\"Yes\\n\");\n flag = 1;\n break;\n }\n }\n if (flag == 0) {\n printf(\"NO\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input\n .split_whitespace()\n .map(|s| s.parse::().unwrap());\n\n let t = it.next().unwrap();\n\n let mut output = String::new();\n\n for _ in 0..t {\n let n = it.next().unwrap();\n let mut ans = false;\n for ai in (&mut it).take(n) {\n if (0..=101).all(|i| i * i != ai) {\n ans = true;\n }\n }\n\n output.push_str(if ans { \"YES\\n\" } else { \"NO\\n\" });\n }\n\n print!(\"{}\", output);\n}", "difficulty": "medium"} {"problem_id": "1589", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi is a user of a site that hosts programming contests.
\nWhen a user competes in a contest, the rating of the user (not necessarily an integer) changes according to the performance of the user, as follows:

\n
    \n
  • Let the current rating of the user be a.
  • \n
  • Suppose that the performance of the user in the contest is b.
  • \n
  • Then, the new rating of the user will be the avarage of a and b.
  • \n
\n

For example, if a user with rating 1 competes in a contest and gives performance 1000, his/her new rating will be 500.5, the average of 1 and 1000.

\n

Takahashi's current rating is R, and he wants his rating to be exactly G after the next contest.
\nFind the performance required to achieve it.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq R, G \\leq 4500
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
R\nG\n
\n
\n
\n
\n
\n

Output

Print the performance required to achieve the objective.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2002\n2017\n
\n
\n
\n
\n
\n

Sample Output 1

2032\n
\n

Takahashi's current rating is 2002.
\nIf his performance in the contest is 2032, his rating will be the average of 2002 and 2032, which is equal to the desired rating, 2017.

\n
\n
\n
\n
\n
\n

Sample Input 2

4500\n0\n
\n
\n
\n
\n
\n

Sample Output 2

-4500\n
\n

Although the current and desired ratings are between 0 and 4500, the performance of a user can be below 0.

\n
\n
", "c_code": "int solution() {\n int R = 0;\n int G = 0;\n int ans = 0;\n scanf(\"%d\\n%d\", &R, &G);\n ans = (2 * G) - R;\n printf(\"%d\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let a: i32 = s.trim().parse().ok().unwrap();\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).ok();\n let b: i32 = t.trim().parse().ok().unwrap();\n println!(\"{}\", (2 * b - a));\n}", "difficulty": "medium"} {"problem_id": "1590", "problem_description": "You are given an array of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$You can apply the following operation an arbitrary number of times: select an index $$$i$$$ ($$$1 \\le i \\le n$$$) and replace the value of the element $$$a_i$$$ with the value $$$a_i + (a_i \\bmod 10)$$$, where $$$a_i \\bmod 10$$$ is the remainder of the integer dividing $$$a_i$$$ by $$$10$$$. For a single index (value $$$i$$$), this operation can be applied multiple times. If the operation is applied repeatedly to the same index, then the current value of $$$a_i$$$ is taken into account each time. For example, if $$$a_i=47$$$ then after the first operation we get $$$a_i=47+7=54$$$, and after the second operation we get $$$a_i=54+4=58$$$.Check if it is possible to make all array elements equal by applying multiple (possibly zero) operations.For example, you have an array $$$[6, 11]$$$. Let's apply this operation to the first element of the array. Let's replace $$$a_1 = 6$$$ with $$$a_1 + (a_1 \\bmod 10) = 6 + (6 \\bmod 10) = 6 + 6 = 12$$$. We get the array $$$[12, 11]$$$. Then apply this operation to the second element of the array. Let's replace $$$a_2 = 11$$$ with $$$a_2 + (a_2 \\bmod 10) = 11 + (11 \\bmod 10) = 11 + 1 = 12$$$. We get the array $$$[12, 12]$$$. Thus, by applying $$$2$$$ operations, you can make all elements of an array equal.", "c_code": "int solution() {\n int t;\n scanf(\"%d\\n\", &t);\n while (t--) {\n int n;\n scanf(\"%d\\n\", &n);\n int a[n];\n int max = -1;\n int m = -1;\n int count = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\\n\", &a[i]);\n if (a[i] % 2 != 0) {\n a[i] += a[i] % 10;\n }\n if (max < a[i]) {\n max = a[i];\n }\n if (a[i] % 10 == 0 && a[i] != m) {\n count++;\n m = a[i];\n }\n }\n if (count > 1) {\n printf(\"NO\\n\");\n } else {\n count = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 3; j++) {\n if (a[i] % 10 != max % 10) {\n a[i] += a[i] % 10;\n } else {\n break;\n }\n }\n a[i] = max - a[i];\n }\n for (int i = 0; i < n; i++) {\n if (a[i] % 20 == 0) {\n count++;\n }\n }\n if (count == n) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut tln = String::new();\n std::io::stdin().read_line(&mut tln).unwrap();\n let tcn = tln.trim_end().parse::().unwrap();\n\n for _k in 0..tcn {\n let mut sln = String::new();\n std::io::stdin().read_line(&mut sln).unwrap();\n\n let _n = sln.trim_end().parse::().unwrap();\n\n let mut thln = String::new();\n std::io::stdin().read_line(&mut thln).unwrap();\n\n let mut groups = vec![0, 0, 0];\n let mut zero_idx = -1;\n let mut unique_zero = true;\n thln.trim_end()\n .split_ascii_whitespace()\n .map(|s| {\n let cur = s.parse::().unwrap();\n (cur / 10, cur % 10)\n })\n .for_each(|i| match i {\n (idx, 0 | 5) => {\n groups[0] = 1;\n let z_i = idx + if i.1 == 0 { 0 } else { 1 };\n if zero_idx != -1 && zero_idx != z_i {\n unique_zero = false;\n }\n\n zero_idx = z_i;\n }\n (idx, rem) => {\n let idx =\n (idx + (match rem {\n 3 | 6 | 7 | 9 => 1,\n _ => 0,\n })) % 2;\n if idx == 0 {\n groups[1] = 1;\n } else {\n groups[2] = 1;\n }\n }\n });\n\n println!(\n \"{}\",\n if groups.into_iter().sum::() == 1 && unique_zero {\n \"Yes\"\n } else {\n \"No\"\n }\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1591", "problem_description": "Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length $$$n$$$ consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements. Vasya doesn't want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is $$$0$$$, and removing $$$i$$$-th character increases the ambiguity by $$$a_i$$$ (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still $$$4$$$ even though you delete it from the string had).Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.", "c_code": "int solution() {\n int n;\n int i;\n scanf(\"%d%*c\", &n);\n long long int minimum_values[n][4];\n int values[n];\n char str[n];\n for (i = 0; i < n; i++) {\n scanf(\"%c\", &str[i]);\n minimum_values[i][0] = minimum_values[i][1] = minimum_values[i][2] =\n minimum_values[i][3] = 0;\n }\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &values[i]);\n }\n if (str[n - 1] == 'd') {\n minimum_values[n - 1][3] = values[n - 1];\n }\n for (i = n - 2; i >= 0; i--) {\n if (str[i] == 'd') {\n minimum_values[i][3] = minimum_values[i + 1][3] + values[i];\n } else {\n minimum_values[i][3] = minimum_values[i + 1][3];\n }\n if (str[i] == 'r') {\n if (minimum_values[i + 1][2] + values[i] < minimum_values[i][3]) {\n minimum_values[i][2] = minimum_values[i + 1][2] + values[i];\n } else {\n minimum_values[i][2] = minimum_values[i][3];\n }\n } else {\n minimum_values[i][2] = minimum_values[i + 1][2];\n }\n if (str[i] == 'a') {\n if (minimum_values[i + 1][1] + values[i] < minimum_values[i][2]) {\n minimum_values[i][1] = minimum_values[i + 1][1] + values[i];\n } else {\n minimum_values[i][1] = minimum_values[i][2];\n }\n } else {\n minimum_values[i][1] = minimum_values[i + 1][1];\n }\n if (str[i] == 'h') {\n if (minimum_values[i + 1][0] + values[i] < minimum_values[i][1]) {\n minimum_values[i][0] = minimum_values[i + 1][0] + values[i];\n } else {\n minimum_values[i][0] = minimum_values[i][1];\n }\n } else {\n minimum_values[i][0] = minimum_values[i + 1][0];\n }\n }\n printf(\"%d\", minimum_values[0][0]);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let n: usize = s.trim().parse().unwrap();\n\n s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let chars: Vec = s.trim().chars().collect();\n\n s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let a: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n #[derive(Clone)]\n struct Prefix {\n empty: u64,\n h: u64,\n ha: u64,\n har: u64,\n }\n\n let mut mem: Vec = vec![\n Prefix {\n empty: 0,\n h: 0,\n ha: 0,\n har: 0\n };\n n + 1\n ];\n\n for i in 0..n {\n mem[i + 1] = match chars[i] {\n 'h' => Prefix {\n empty: mem[i].empty + a[i],\n h: min(mem[i].empty, mem[i].h),\n ha: mem[i].ha,\n har: mem[i].har,\n },\n\n 'a' => Prefix {\n empty: mem[i].empty,\n h: mem[i].h + a[i],\n ha: min(mem[i].ha, mem[i].h),\n har: mem[i].har,\n },\n\n 'r' => Prefix {\n empty: mem[i].empty,\n h: mem[i].h,\n ha: mem[i].ha + a[i],\n har: min(mem[i].har, mem[i].ha),\n },\n\n 'd' => Prefix {\n empty: mem[i].empty,\n h: mem[i].h,\n ha: mem[i].ha,\n har: mem[i].har + a[i],\n },\n\n _ => mem[i].clone(),\n }\n }\n\n let p = &mem[n];\n println!(\"{}\", min(p.empty, min(p.h, min(p.ha, p.har))));\n}", "difficulty": "medium"} {"problem_id": "1592", "problem_description": "Nauuo is a girl who loves playing cards.One day she was playing cards but found that the cards were mixed with some empty ones.There are $$$n$$$ cards numbered from $$$1$$$ to $$$n$$$, and they were mixed with another $$$n$$$ empty cards. She piled up the $$$2n$$$ cards and drew $$$n$$$ of them. The $$$n$$$ cards in Nauuo's hands are given. The remaining $$$n$$$ cards in the pile are also given in the order from top to bottom.In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.Nauuo wants to make the $$$n$$$ numbered cards piled up in increasing order (the $$$i$$$-th card in the pile from top to bottom is the card $$$i$$$) as quickly as possible. Can you tell her the minimum number of operations?", "c_code": "int solution() {\n int i;\n int j;\n int k = 0;\n int n;\n int x = 0;\n int y;\n int z = 0;\n int d = 1;\n int e = 0;\n scanf(\"%d\", &n);\n int a[n + 1];\n int b[n + 1];\n for (i = 1; i <= n; i++) {\n scanf(\"%d\", &b[i]);\n if (b[i] == 1) {\n z = 1;\n }\n }\n for (i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] == 1) {\n y = i;\n }\n }\n if (z == 1) {\n for (i = 1; i <= n; i++) {\n if ((a[i] <= i) && (a[i] != 0)) {\n x = i - a[i] + 1;\n }\n if (x > k) {\n k = x;\n }\n }\n printf(\"%d\", n + k);\n } else {\n for (i = y + 1; i <= n; i++) {\n d++;\n if (a[i] != d) {\n e = 1;\n break;\n }\n }\n if (e == 0) {\n for (i = 1; i < y; i++) {\n d++;\n if ((a[i] <= d) && (a[i] != 0)) {\n e = 1;\n break;\n }\n }\n }\n if (e == 0) {\n printf(\"%d\", y - 1);\n } else {\n int c[n - y + 1];\n for (i = y + 1; i <= n; i++) {\n c[i - y] = a[i];\n }\n for (i = 1; i <= n - y; i++) {\n if ((c[i] <= i) && (c[i] != 0)) {\n x = i - c[i] + 1;\n }\n if (x > k) {\n k = x;\n }\n }\n printf(\"%d\", k + y + n);\n }\n }\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\n let n = get!(usize);\n let mut zeros = 0;\n for _ in 0..n {\n let h = get!();\n if h == 0 {\n zeros += 1;\n }\n }\n if zeros == 0 {\n println!(\"{}\", n);\n return;\n }\n let mut vs = vec![0; n + 1];\n let mut ps = vec![0; n + 1];\n for i in 0..n {\n let p = get!(usize);\n ps[i + 1] = p;\n vs[p] = i as i64 + 2;\n }\n if n == 1 {\n if ps[1] == 1 {\n println!(\"0\");\n } else {\n println!(\"1\");\n }\n return;\n }\n let mut x = 1;\n for i in 0..n {\n let i = n - i;\n if ps[i] == 1 {\n x = i;\n break;\n } else if ps[i] == 0 {\n x = 0;\n break;\n } else if i < n && ps[i] + 1 != ps[i + 1] {\n x = 0;\n break;\n }\n }\n if zeros == n && x == 1 {\n println!(\"0\");\n return;\n }\n if zeros < n && x > 0 {\n let mut c = 1;\n for i in ps[n] + 1..n {\n if vs[i] > c {\n x = 0;\n break;\n }\n c += 1;\n }\n }\n let mut min = 0;\n if zeros < n && x > 0 {\n min = -((n - x + 1) as i64);\n for i in x..n + 1 {\n vs[ps[i]] = -((n - i + 1) as i64);\n }\n }\n let mut ans = vs[n];\n for i in 1..n + 1 {\n let mut v = vs[i];\n if v == 0 {\n v = min;\n }\n ans = std::cmp::max(ans, min + i as i64);\n ans = std::cmp::max(ans, v + (n - i) as i64);\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1593", "problem_description": "Polycarp has $$$26$$$ tasks. Each task is designated by a capital letter of the Latin alphabet.The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp cannot return to the previous task.Polycarp can only solve one task during the day. Every day he wrote down what task he solved. Now the teacher wants to know if Polycarp followed his advice.For example, if Polycarp solved tasks in the following order: \"DDBBCCCBBEZ\", then the teacher will see that on the third day Polycarp began to solve the task 'B', then on the fifth day he got distracted and began to solve the task 'C', on the eighth day Polycarp returned to the task 'B'. Other examples of when the teacher is suspicious: \"BAB\", \"AABBCCDDEEBZZ\" and \"AAAAZAAAAA\".If Polycarp solved the tasks as follows: \"FFGZZZY\", then the teacher cannot have any suspicions. Please note that Polycarp is not obligated to solve all tasks. Other examples of when the teacher doesn't have any suspicious: \"BA\", \"AFFFCC\" and \"YYYYY\".Help Polycarp find out if his teacher might be suspicious.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n char string[n + 1];\n scanf(\"%s\", string);\n int flag1 = 0;\n {\n for (char c = 'A'; c <= 'Z' && flag1 == 0; c++) {\n int count = 0;\n for (int i = 0; i < n; i++) {\n if (string[i] == c) {\n count++;\n }\n }\n\n int l[count];\n int j = 0;\n if (count > 1) {\n for (int i = 0; i < n; i++) {\n if (string[i] == c) {\n l[j] = i;\n j++;\n }\n }\n\n {\n for (int i = 0; i < count - 1 && flag1 == 0; i++) {\n if (l[i + 1] - l[i] > 1 && l[i + 1] - l[i] != 1) {\n printf(\"No\\n\");\n flag1 = 1;\n }\n }\n }\n }\n }\n }\n if (flag1 == 0) {\n printf(\"Yes\\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(|x| x.unwrap());\n\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 mut a = [false; 26];\n let mut it = s.bytes();\n let mut x = it.next().unwrap();\n loop {\n let next = it.next();\n match next {\n Some(y) if y == x => {}\n y => {\n let i = (x - b'A') as usize;\n if a[i] {\n writeln!(&mut output, \"NO\").unwrap();\n continue 'cases;\n } else {\n a[i] = true;\n }\n if let Some(y) = y {\n x = y;\n } else {\n break;\n }\n }\n }\n }\n writeln!(&mut output, \"YES\").unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1594", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

\n

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \\leq i \\leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \\leq i \\leq M).

\n

Consider the following action:

\n
    \n
  • Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.
  • \n
\n

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N, M \\leq 200000
  • \n
  • 1 \\leq K \\leq 10^9
  • \n
  • 1 \\leq A_i, B_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M K\nA_1 A_2 \\ldots A_N\nB_1 B_2 \\ldots B_M\n
\n
\n
\n
\n
\n

Output

Print an integer representing the maximum number of books that can be read.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4 240\n60 90 120\n80 150 80 150\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

\n

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

\n
    \n
  • Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.
  • \n
  • Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.
  • \n
  • Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 4 730\n60 90 120\n80 150 80 150\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5 4 1\n1000000000 1000000000 1000000000 1000000000 1000000000\n1000000000 1000000000 1000000000 1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

Watch out for integer overflows.

\n
\n
", "c_code": "int solution(void) {\n\n long int desk1;\n long int desk2;\n long int limit;\n scanf(\"%ld%ld%ld\", &desk1, &desk2, &limit);\n\n long int now1 = 0;\n long int now2 = desk2;\n long int sum1 = 0;\n long int sum2 = 0;\n long int max = 0;\n long int read1[desk1 + 1];\n long int read2[desk2 + 1];\n\n for (long int i = 0; i < desk1; i++) {\n scanf(\"%ld\", &read1[i]);\n }\n\n for (long int i = 0; i < desk2; i++) {\n scanf(\"%ld\", &read2[i]);\n sum2 += read2[i];\n }\n\n for (long int i = 0; i <= desk1; i++) {\n if (sum1 > limit) {\n break;\n }\n for (int j = 0; j < desk2; j++) {\n\n if (sum1 + sum2 > limit && now2 > 0) {\n now2--;\n\n sum2 -= read2[now2];\n } else {\n if (max < now1 + now2) {\n max = now1 + now2;\n }\n break;\n }\n }\n\n sum1 += read1[now1];\n now1++;\n }\n\n printf(\"%ld\\n\", max);\n\n return 0;\n}", "rust_code": "fn solution() {\n let (n, m, k): (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 a: 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 let b: 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 t = 0;\n let mut i = 0;\n while i < n && t + a[i] <= k {\n t += a[i];\n i += 1;\n }\n let mut ans = i;\n let mut j = 0;\n while j < m {\n while j < m && t + b[j] <= k {\n t += b[j];\n j += 1;\n }\n ans = std::cmp::max(ans, i + j);\n if i == 0 {\n break;\n }\n i -= 1;\n t -= a[i];\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1595", "problem_description": "Alice is playing with some stones.Now there are three numbered heaps of stones. The first of them contains $$$a$$$ stones, the second of them contains $$$b$$$ stones and the third of them contains $$$c$$$ stones.Each time she can do one of two operations: take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones); take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones). She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has $$$0$$$ stones. Can you help her?", "c_code": "int solution(void) {\n int times;\n int a[3];\n int t = 0;\n scanf(\"%d\", ×);\n while (times--) {\n scanf(\"%d %d %d\", &a[0], &a[1], &a[2]);\n while (a[2] >= 2 && a[1] > 0) {\n t++;\n a[2] -= 2;\n a[1] -= 1;\n }\n while (a[1] > 1 && a[0] > 0) {\n t++;\n a[0]--;\n a[1] -= 2;\n }\n printf(\"%d\\n\", t * 3);\n t = 0;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let t: u16 = buffer.trim().parse().expect(\"invalid input\");\n\n for _ in 0..t {\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_iter = input.split_whitespace();\n let a: u8 = input_iter.next().unwrap().parse().unwrap();\n let b: u8 = input_iter.next().unwrap().parse().unwrap();\n let c: u8 = input_iter.next().unwrap().parse().unwrap();\n\n let op2 = b.min(c / 2);\n let op1 = a.min((b - op2) / 2);\n println!(\"{}\", 3 * (op1 + op2));\n }\n}", "difficulty": "hard"} {"problem_id": "1596", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Snuke, who loves animals, built a zoo.

\n

There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle.\nThe animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1.

\n

There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.

\n

Snuke cannot tell the difference between these two species, and asked each animal the following question: \"Are your neighbors of the same species?\" The animal numbered i answered s_i. Here, if s_i is o, the animal said that the two neighboring animals are of the same species, and if s_i is x, the animal said that the two neighboring animals are of different species.

\n

More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.\nSimilarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.

\n

Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 ≤ N ≤ 10^{5}
  • \n
  • s is a string of length N consisting of o and x.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\ns\n
\n
\n
\n
\n
\n

Output

If there does not exist an valid assignment that is consistent with s, print -1.\nOtherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.

\n
    \n
  • t is a string of length N consisting of S and W.
  • \n
  • If t_i is S, it indicates that the animal numbered i is a sheep. If t_i is W, it indicates that the animal numbered i is a wolf.
  • \n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

6\nooxoox\n
\n
\n
\n
\n
\n

Sample Output 1

SSSWWS\n
\n

For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.

\n

Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x. If the neiboring animals are of different species, a sheep answers x and a wolf answers o.

\n
\n\"b34c052fc21c42d2def9b98d6dccd05c.png\"\n
\n
\n
\n
\n
\n
\n

Sample Input 2

3\noox\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

Print -1 if there is no valid assignment of species.

\n
\n
\n
\n
\n
\n

Sample Input 3

10\noxooxoxoox\n
\n
\n
\n
\n
\n

Sample Output 3

SSWWSSSWWS\n
\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n char s[100005];\n scanf(\"%s\", s);\n int i;\n int j;\n char ans[4][100005];\n ans[0][0] = ans[1][0] = 'S';\n ans[2][0] = ans[3][0] = 'W';\n ans[0][1] = ans[2][1] = 'S';\n ans[1][1] = ans[3][1] = 'W';\n for (i = 1; i < n - 1; i++) {\n for (j = 0; j < 4; j++) {\n if (ans[j][i - 1] == 'S' && ans[j][i] == 'S' && s[i] == 'o') {\n ans[j][i + 1] = 'S';\n }\n if (ans[j][i - 1] == 'S' && ans[j][i] == 'S' && s[i] == 'x') {\n ans[j][i + 1] = 'W';\n }\n if (ans[j][i - 1] == 'S' && ans[j][i] == 'W' && s[i] == 'o') {\n ans[j][i + 1] = 'W';\n }\n if (ans[j][i - 1] == 'S' && ans[j][i] == 'W' && s[i] == 'x') {\n ans[j][i + 1] = 'S';\n }\n if (ans[j][i - 1] == 'W' && ans[j][i] == 'S' && s[i] == 'o') {\n ans[j][i + 1] = 'W';\n }\n if (ans[j][i - 1] == 'W' && ans[j][i] == 'S' && s[i] == 'x') {\n ans[j][i + 1] = 'S';\n }\n if (ans[j][i - 1] == 'W' && ans[j][i] == 'W' && s[i] == 'o') {\n ans[j][i + 1] = 'S';\n }\n if (ans[j][i - 1] == 'W' && ans[j][i] == 'W' && s[i] == 'x') {\n ans[j][i + 1] = 'W';\n }\n }\n }\n for (i = 0; i < 4; i++) {\n if (ans[i][0] == 'S' && ans[i][1] == 'S' && ans[i][n - 1] == 'S' &&\n s[0] == 'x') {\n continue;\n }\n if (ans[i][0] == 'S' && ans[i][1] == 'S' && ans[i][n - 1] == 'W' &&\n s[0] == 'o') {\n continue;\n }\n if (ans[i][0] == 'S' && ans[i][1] == 'W' && ans[i][n - 1] == 'S' &&\n s[0] == 'o') {\n continue;\n }\n if (ans[i][0] == 'S' && ans[i][1] == 'W' && ans[i][n - 1] == 'W' &&\n s[0] == 'x') {\n continue;\n }\n if (ans[i][0] == 'W' && ans[i][1] == 'S' && ans[i][n - 1] == 'S' &&\n s[0] == 'o') {\n continue;\n }\n if (ans[i][0] == 'W' && ans[i][1] == 'S' && ans[i][n - 1] == 'W' &&\n s[0] == 'x') {\n continue;\n }\n if (ans[i][0] == 'W' && ans[i][1] == 'W' && ans[i][n - 1] == 'S' &&\n s[0] == 'x') {\n continue;\n }\n if (ans[i][0] == 'W' && ans[i][1] == 'W' && ans[i][n - 1] == 'W' &&\n s[0] == 'o') {\n continue;\n }\n if (ans[i][n - 1] == 'S' && ans[i][n - 2] == 'S' && ans[i][0] == 'S' &&\n s[n - 1] == 'x') {\n continue;\n }\n if (ans[i][n - 1] == 'S' && ans[i][n - 2] == 'S' && ans[i][0] == 'W' &&\n s[n - 1] == 'o') {\n continue;\n }\n if (ans[i][n - 1] == 'S' && ans[i][n - 2] == 'W' && ans[i][0] == 'S' &&\n s[n - 1] == 'o') {\n continue;\n }\n if (ans[i][n - 1] == 'S' && ans[i][n - 2] == 'W' && ans[i][0] == 'W' &&\n s[n - 1] == 'x') {\n continue;\n }\n if (ans[i][n - 1] == 'W' && ans[i][n - 2] == 'S' && ans[i][0] == 'S' &&\n s[n - 1] == 'o') {\n continue;\n }\n if (ans[i][n - 1] == 'W' && ans[i][n - 2] == 'S' && ans[i][0] == 'W' &&\n s[n - 1] == 'x') {\n continue;\n }\n if (ans[i][n - 1] == 'W' && ans[i][n - 2] == 'W' && ans[i][0] == 'S' &&\n s[n - 1] == 'x') {\n continue;\n }\n if (ans[i][n - 1] == 'W' && ans[i][n - 2] == 'W' && ans[i][0] == 'W' &&\n s[n - 1] == 'o') {\n continue;\n }\n ans[i][n] = '\\0';\n printf(\"%s\\n\", ans[i]);\n return 0;\n }\n printf(\"-1\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut line1 = String::new();\n let mut line2 = String::new();\n let stdin = std::io::stdin();\n stdin.read_line(&mut line1).expect(\"Could not read line\");\n stdin.read_line(&mut line2).expect(\"Could not read line\");\n let p: Vec<_> = line1.trim().split(' ').collect();\n let n: usize = p[0].parse().unwrap();\n let s = line2.trim().as_bytes();\n for c1 in ['S', 'W'] {\n for c2 in ['S', 'W'] {\n let mut ret = Vec::new();\n ret.push(c1);\n ret.push(c2);\n for i in 2..n {\n if ret[i - 1] == 'S' {\n if s[i - 1] == b'o' {\n let x = ret[i - 2];\n ret.push(x);\n } else {\n let x = if ret[i - 2] == 'S' { 'W' } else { 'S' };\n ret.push(x);\n }\n } else {\n if s[i - 1] == b'o' {\n let x = if ret[i - 2] == 'S' { 'W' } else { 'S' };\n ret.push(x);\n } else {\n let x = ret[i - 2];\n ret.push(x);\n }\n }\n }\n if ((ret[1] == ret[n - 1]) == ((ret[0] == 'S') ^ (s[0] != b'o')))\n && ((ret[0] == ret[n - 2]) == ((ret[n - 1] == 'S') ^ (s[n - 1] != b'o')))\n {\n for i in 0..n {\n print!(\"{}\", ret[i]);\n }\n println!();\n return;\n }\n }\n }\n println!(\"-1\");\n}", "difficulty": "medium"} {"problem_id": "1597", "problem_description": "One night, Mark realized that there is an essay due tomorrow. He hasn't written anything yet, so Mark decided to randomly copy-paste substrings from the prompt to make the essay.More formally, the prompt is a string $$$s$$$ of initial length $$$n$$$. Mark will perform the copy-pasting operation $$$c$$$ times. Each operation is described by two integers $$$l$$$ and $$$r$$$, which means that Mark will append letters $$$s_l s_{l+1} \\ldots s_r$$$ to the end of string $$$s$$$. Note that the length of $$$s$$$ increases after this operation.Of course, Mark needs to be able to see what has been written. After copying, Mark will ask $$$q$$$ queries: given an integer $$$k$$$, determine the $$$k$$$-th letter of the final string $$$s$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n int c;\n int q;\n for (int m = 0; m < t; m++) {\n scanf(\"%d %d %d\", &n, &c, &q);\n\n char str[n];\n scanf(\"%s\", str);\n long long a = strlen(str);\n long long arr1[c];\n long long arr2[c];\n long long total[c];\n for (int i = 0; i < c; i++) {\n scanf(\"%lld %lld\", &arr1[i], &arr2[i]);\n\n if (i == 0) {\n total[i] = a + arr2[i] - arr1[i] + 1;\n } else {\n total[i] = total[i - 1] + arr2[i] - arr1[i] + 1;\n }\n }\n long long temp;\n\n for (int i = 0; i < q; i++) {\n scanf(\"%lld\", &temp);\n int k = 0;\n while (temp > total[k]) {\n k++;\n }\n\n if (temp <= a) {\n printf(\"%c\\n\", str[temp - 1]);\n }\n\n else {\n int flag = 0;\n long long w = total[k] - temp;\n w = arr2[k] - w;\n k--;\n while (flag == 0) {\n if (w <= a) {\n printf(\"%c\\n\", str[w - 1]);\n flag = 1;\n } else {\n long long a1 = total[k] - w;\n if (arr2[k] - a1 >= arr1[k]) {\n w = arr2[k] - a1;\n }\n }\n k--;\n }\n }\n }\n }\n}", "rust_code": "fn solution() {\n input! {\n name = reader,\n tests: usize\n }\n for _ in 0..tests {\n input! {\n use reader,\n n: usize,\n c: usize,\n q: usize,\n s: bytes,\n cs: [(usize1, usize); c],\n qs: [usize1; q]\n }\n let mut shifts = vec![];\n let mut len = n;\n for (l, r) in cs {\n shifts.push((len, len - l));\n len += r - l;\n }\n for qi in qs {\n let mut pos = qi;\n for &(a, b) in shifts.iter().rev() {\n if pos >= a {\n pos -= b;\n }\n }\n println!(\"{}\", s[pos] as char)\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1598", "problem_description": "You are given an array $$$a_1, a_2, \\dots a_n$$$. Count the number of pairs of indices $$$1 \\leq i, j \\leq n$$$ such that $$$a_i < i < a_j < j$$$.", "c_code": "int solution() {\n int x;\n int y;\n int z;\n int i;\n int j;\n int k;\n int a;\n int b;\n int c;\n int n;\n int m;\n int t;\n int arr[200001];\n int aa[200001];\n long long ans;\n long long d;\n\n scanf(\"%d\", &t);\n while (t--) {\n ans = 0;\n scanf(\"%d\", &n);\n aa[0] = 0;\n for (x = 1; x <= n; x++) {\n scanf(\"%d\", &arr[x]), aa[x] = aa[x - 1] + (arr[x] < x);\n }\n\n for (x = n; x > 0; x--) {\n if (arr[x] && (arr[x] < x)) {\n ans += aa[arr[x] - 1];\n }\n }\n\n printf(\"%lld\\n\", ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut inp_t = String::new();\n io::stdin()\n .read_line(&mut inp_t)\n .expect(\"Failed to read line\");\n\n let t: i32 = inp_t.trim().parse().expect(\"T is not an integer\");\n\n for _ in 0..t {\n let mut inp_n = String::new();\n io::stdin()\n .read_line(&mut inp_n)\n .expect(\"Failed to read line\");\n\n let n: usize = inp_n.trim().parse().expect(\"N is not an integer\");\n\n let mut v: Vec = Vec::new();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).expect(\"Failed to read line\");\n\n v = s.trim().split(\" \").map(|x| x.parse().unwrap()).collect();\n\n let mut ans: i64 = 0;\n\n let mut all: Vec<(i32, i32)> = Vec::new();\n\n for i in 0..n {\n if v[i] < (i + 1) as i32 {\n all.push((v[i], (i + 1) as i32));\n }\n }\n\n all.sort();\n\n for elem in &all {\n let temp = all.binary_search(&(elem.1 + 1, -1_i32));\n\n match temp {\n Err(pl) => ans += all.len() as i64 - pl as i64,\n _ => continue,\n };\n }\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "hard"} {"problem_id": "1599", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Takahashi wants to be a member of some web service.

\n

He tried to register himself with the ID S, which turned out to be already used by another user.

\n

Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.

\n

He is now trying to register with the ID T. Determine whether this string satisfies the property above.

\n
\n
\n
\n
\n

Constraints

    \n
  • S and T are strings consisting of lowercase English letters.
  • \n
  • 1 \\leq |S| \\leq 10
  • \n
  • |T| = |S| + 1
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nT\n
\n
\n
\n
\n
\n

Output

If T satisfies the property in Problem Statement, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

chokudai\nchokudaiz\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

chokudaiz can be obtained by appending z at the end of chokudai.

\n
\n
\n
\n
\n
\n

Sample Input 2

snuke\nsnekee\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

snekee cannot be obtained by appending one character at the end of snuke.

\n
\n
\n
\n
\n
\n

Sample Input 3

a\naa\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution(void) {\n char *S = (char *)malloc(128);\n char *T = (char *)malloc(128);\n scanf(\"%s\", S);\n scanf(\"%s\", T);\n *(T + (strlen(S))) = '\\0';\n if (strcmp(S, T) != 0) {\n puts(\"No\");\n } else {\n puts(\"Yes\");\n }\n free(S);\n free(T);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n s.pop();\n let mut t = String::new();\n stdin().read_line(&mut t).ok();\n t.pop();\n t.pop();\n\n println!(\"{}\", if s == t { \"Yes\" } else { \"No\" });\n}", "difficulty": "medium"} {"problem_id": "1600", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There are N boxes arranged in a circle. The i-th box contains A_i stones.

\n

Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:

\n
    \n
  • Select one box. Let the box be the i-th box. Then, for each j from 1 through N, remove exactly j stones from the (i+j)-th box. Here, the (N+k)-th box is identified with the k-th box.
  • \n
\n

Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ N ≦ 10^5
  • \n
  • 1 ≦ A_i ≦ 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nA_1 A_2A_N\n
\n
\n
\n
\n
\n

Output

If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n4 5 1 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

All the stones can be removed in one operation by selecting the second box.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n6 9 12 10 8\n
\n
\n
\n
\n
\n

Sample Output 2

YES\n
\n
\n
\n
\n
\n
\n

Sample Input 3

4\n1 2 3 1\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
", "c_code": "int solution() {\n long long i;\n long long N;\n long long A[100001];\n scanf(\"%lld\", &N);\n for (i = 0; i < N; i++) {\n scanf(\"%lld\", &(A[i]));\n }\n A[N] = A[0];\n\n long long sum = 0;\n long long diff[100001];\n for (i = 1; i <= N; i++) {\n sum += A[i];\n diff[i] = A[i] - A[i - 1];\n }\n if (sum % (N * (N + 1) / 2) != 0) {\n printf(\"NO\\n\");\n } else {\n sum /= N * (N + 1) / 2;\n for (i = 1, diff[0] = 0; i <= N; i++) {\n diff[i] = (sum - diff[i]) / N;\n if (diff[i] < 0 || diff[i] > sum) {\n break;\n }\n diff[0] += diff[i];\n }\n if (i <= N || diff[0] != sum) {\n printf(\"NO\\n\");\n } else {\n for (i = 1; i <= N; i++) {\n A[N] -= diff[i] * (N - i + 1);\n }\n if (A[N] != 0) {\n printf(\"No\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n }\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_line = String::new();\n io::stdin().read_line(&mut input_line).unwrap();\n let N = input_line.trim().parse::().unwrap();\n let Ni = N as isize;\n\n let mut input_line = String::new();\n io::stdin().read_line(&mut input_line).unwrap();\n let data = input_line\n .trim()\n .split(\" \")\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let step = (N * (N + 1) / 2) as isize;\n let total = data.iter().sum::();\n if total % step != 0 {\n println!(\"NO\");\n } else {\n let K = total / step;\n let mut delta: Vec = Vec::with_capacity(N);\n for i in 0..N {\n delta.push(data[i] - data[(N + i - 1) % N]);\n }\n let invalid = delta\n .iter()\n .map(|x| (K - x < 0) || ((K - x) % Ni > 0))\n .fold(false, |a, b| a || b);\n if invalid {\n println!(\"NO\");\n } else {\n let sum = delta.iter().map(|x| (K - x) / Ni).sum::();\n if sum == K {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1601", "problem_description": "Stanley defines the beauty of an array $$$a$$$ of length $$$n$$$, which contains non-negative integers, as follows: $$$$$$\\sum\\limits_{i = 1}^{n} \\left \\lfloor \\frac{a_{i}}{k} \\right \\rfloor,$$$$$$ which means that we divide each element by $$$k$$$, round it down, and sum up the resulting values.Stanley told Sam the integer $$$k$$$ and asked him to find an array $$$a$$$ of $$$n$$$ non-negative integers, such that the beauty is equal to $$$b$$$ and the sum of elements is equal to $$$s$$$. Help Sam — find any of the arrays satisfying the conditions above.", "c_code": "int solution() {\n int t;\n int i;\n int j;\n long long n[1005];\n long long k[1005];\n long long b[1005];\n long long s[1005];\n long long left;\n scanf(\"%d\", &t);\n for (i = 1; i <= t; i++) {\n scanf(\"%lld%lld%lld%lld\", &n[i], &k[i], &b[i], &s[i]);\n }\n for (i = 1; i <= t; i++) {\n if (s[i] >= (k[i] * b[i]) &&\n s[i] - (k[i] - 1) * (n[i] - 1) < k[i] * (b[i] + 1)) {\n if (s[i] < k[i] * (b[i] + 1)) {\n printf(\"%lld \", s[i]);\n for (j = 1; j <= n[i] - 1; j++) {\n printf(\"0 \");\n }\n } else {\n printf(\"%lld \", (k[i] * (b[i] + 1)) - 1);\n left = s[i] - k[i] * (b[i] + 1) + 1;\n for (j = 1; j <= n[i] - 1; j++) {\n if (left >= k[i]) {\n printf(\"%lld \", k[i] - 1);\n left -= (k[i] - 1);\n } else {\n printf(\"%lld \", left);\n break;\n }\n }\n for (j += 1; j <= n[i] - 1; j++) {\n printf(\"0 \");\n }\n }\n printf(\"\\n\");\n } else {\n printf(\"-1\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n let t: u32 = buf.trim().parse().unwrap();\n buf.clear();\n\n let mut out = String::new();\n for _ in 0..t {\n stdin().read_line(&mut buf).unwrap();\n let inp: Vec<&str> = buf.split_whitespace().collect();\n\n let n: usize = inp[0].parse().unwrap();\n let k: usize = inp[1].parse().unwrap();\n let b: usize = inp[2].parse().unwrap();\n let s: usize = inp[3].parse().unwrap();\n\n drop(inp);\n buf.clear();\n\n let first = k * b;\n\n if (s / k) * k < first {\n println!(\"-1\");\n continue;\n }\n\n let mut rem = s - first;\n let cap = n * (k - 1);\n if cap + first < s {\n println!(\"-1\");\n continue;\n }\n\n let first = first + rem.min(k - 1);\n out = first.to_string();\n rem -= rem.min(k - 1);\n\n for _ in 0..n - 1 {\n let t = rem.min(k - 1);\n out.push_str(&format!(\" {}\", t));\n rem -= t;\n }\n println!(\"{}\", out);\n out.clear();\n }\n}", "difficulty": "easy"} {"problem_id": "1602", "problem_description": "Given the integer $$$n$$$ — the number of available blocks. You must use all blocks to build a pedestal. The pedestal consists of $$$3$$$ platforms for $$$2$$$-nd, $$$1$$$-st and $$$3$$$-rd places respectively. The platform for the $$$1$$$-st place must be strictly higher than for the $$$2$$$-nd place, and the platform for the $$$2$$$-nd place must be strictly higher than for the $$$3$$$-rd place. Also, the height of each platform must be greater than zero (that is, each platform must contain at least one block). Example pedestal of $$$n=11$$$ blocks: second place height equals $$$4$$$ blocks, first place height equals $$$5$$$ blocks, third place height equals $$$2$$$ blocks.Among all possible pedestals of $$$n$$$ blocks, deduce one such that the platform height for the $$$1$$$-st place minimum as possible. If there are several of them, output any of them.", "c_code": "int solution(void) {\n int testcase = 0;\n scanf(\"%i\", &testcase);\n for (int i = 0; i < testcase; i++) {\n int n = 0;\n scanf(\"%i\", &n);\n\n int h1 = 0;\n int h2 = 0;\n int h3 = 0;\n\n if (n % 3 == 0) {\n h3 = (n / 3) - 1;\n } else {\n h3 = (n / 3) - 2;\n }\n\n if ((n % 3 == 2) || h3 == 0) {\n h3++;\n }\n n = n - h3;\n if (n % 2 == 0) {\n h1 = (n / 2) + 1;\n h2 = (n / 2) - 1;\n } else {\n h2 = (n - 1) / 2;\n h1 = h2 + 1;\n }\n\n printf(\"%i %i %i\\n\", h2, h1, h3);\n }\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(1)\n .flatten()\n .flat_map(|s| s.parse::())\n .for_each(|n| {\n let a = n.wrapping_add(1).wrapping_div(3);\n let b = n.wrapping_add(5).wrapping_div(3);\n let c = n.wrapping_div(3).wrapping_sub(1);\n writeln!(buf, \"{a} {b} {c}\").ok();\n });\n}", "difficulty": "hard"} {"problem_id": "1603", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are K pieces of cakes.\nMr. Takahashi would like to eat one cake per day, taking K days to eat them all.

\n

There are T types of cake, and the number of the cakes of type i (1 ≤ i ≤ T) is a_i.

\n

Eating the same type of cake two days in a row would be no fun,\nso Mr. Takahashi would like to decide the order for eating cakes that minimizes the number of days on which he has to eat the same type of cake as the day before.

\n

Compute the minimum number of days on which the same type of cake as the previous day will be eaten.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ K ≤ 10000
  • \n
  • 1 ≤ T ≤ 100
  • \n
  • 1 ≤ a_i ≤ 100
  • \n
  • a_1 + a_2 + ... + a_T = K
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
K T\na_1 a_2 ... a_T\n
\n
\n
\n
\n
\n

Output

Print the minimum number of days on which the same type of cake as the previous day will be eaten.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7 3\n3 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n

For example, if Mr. Takahashi eats cakes in the order of 2, 1, 2, 3, 1, 3, 1, he can avoid eating the same type of cake as the previous day.

\n
\n
\n
\n
\n
\n

Sample Input 2

6 3\n1 4 1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

There are 6 cakes.\nFor example, if Mr. Takahashi eats cakes in the order of 2, 3, 2, 2, 1, 2, he has to eat the same type of cake (i.e., type 2) as the previous day only on the fourth day.\nSince this is the minimum number, the answer is 1.

\n
\n
\n
\n
\n
\n

Sample Input 3

100 1\n100\n
\n
\n
\n
\n
\n

Sample Output 3

99\n
\n

Since Mr. Takahashi has only one type of cake, he has no choice but to eat the same type of cake as the previous day from the second day and after.

\n
\n
", "c_code": "int solution(void) {\n\n int k;\n int t;\n scanf(\"%d %d\", &k, &t);\n int a[t];\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &a[i]);\n }\n int max = 0;\n for (int i = 0; i < t; i++) {\n if (a[i] > max) {\n max = a[i];\n }\n }\n int answer = max - 1 - (k - max);\n if (answer < 0) {\n answer = 0;\n }\n printf(\"%d\\n\", answer);\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 _k: usize = itr.next().unwrap().parse().unwrap();\n let t: usize = itr.next().unwrap().parse().unwrap();\n let mut a: Vec = (0..t)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n a.sort();\n a.reverse();\n\n let s = a.iter().sum::() - a[0];\n println!(\"{}\", a[0].saturating_sub(s + 1));\n}", "difficulty": "medium"} {"problem_id": "1604", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

\n
    \n
  • Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \\leq A_i \\leq 9).
  • \n
  • The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 10^4
  • \n
  • 1 \\leq M \\leq 9
  • \n
  • 1 \\leq A_i \\leq 9
  • \n
  • A_i are all different.
  • \n
  • There exists an integer that can be formed by exactly N matchsticks under the conditions.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_1 A_2 ... A_M\n
\n
\n
\n
\n
\n

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

20 4\n3 7 8 4\n
\n
\n
\n
\n
\n

Sample Output 1

777773\n
\n

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

\n
\n
\n
\n
\n
\n

Sample Input 2

101 9\n9 8 7 6 5 4 3 2 1\n
\n
\n
\n
\n
\n

Sample Output 2

71111111111111111111111111111111111111111111111111\n
\n

The output may not fit into a 64-bit integer type.

\n
\n
\n
\n
\n
\n

Sample Input 3

15 3\n5 4 6\n
\n
\n
\n
\n
\n

Sample Output 3

654\n
\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int i;\n int j;\n int k;\n int a[10];\n for (i = 0; i < m; i++) {\n scanf(\"%d\", &a[i]);\n }\n int c[10] = {1000000009, 2, 5, 5, 4, 5, 6, 3, 7, 6};\n int dp[10004][10];\n for (i = 0; i < 10004; i++) {\n for (j = 0; j < 10; j++) {\n dp[i][j] = 0;\n }\n }\n for (i = 0; i <= n; i++) {\n dp[i][0] = -1;\n }\n dp[0][0] = 0;\n int v[10];\n int p;\n for (i = 1; i <= n; i++) {\n for (j = 0; j < m; j++) {\n if (i >= c[a[j]]) {\n if (i == 10) {\n i = 10;\n }\n if (dp[i - c[a[j]]][0] >= 0 && dp[i][0] < dp[i - c[a[j]]][0] + 1) {\n for (k = 0; k < 10; k++) {\n dp[i][k] = dp[i - c[a[j]]][k];\n }\n dp[i][0]++;\n dp[i][a[j]]++;\n }\n if (dp[i][0] == dp[i - c[a[j]]][0] + 1 && dp[i - c[a[j]]][0] >= 0) {\n for (k = 0; k < 10; k++) {\n v[k] = dp[i - c[a[j]]][k];\n }\n v[0]++;\n v[a[j]]++;\n p = -1;\n for (k = 9; k > 0; k--) {\n if (dp[i][k] > v[k]) {\n break;\n }\n if (dp[i][k] < v[k]) {\n p = 1;\n break;\n }\n }\n if (p > 0) {\n for (k = 0; k < 10; k++) {\n dp[i][k] = v[k];\n }\n }\n }\n }\n }\n }\n for (i = 9; i > 0; i--) {\n for (j = 0; j < dp[n][i]; j++) {\n printf(\"%d\", i);\n }\n }\n printf(\"\\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 m: usize = itr.next().unwrap().parse().unwrap();\n let a: Vec = (0..m)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n\n let mut dp = vec![\"unko\".to_string(); n + 9];\n dp[0] = \"\".to_string();\n let maxs = |a: String, b: String| -> String {\n if a == \"unko\" || a.len() < b.len() {\n b\n } else if a.len() > b.len() {\n a\n } else {\n max(a, b)\n }\n };\n let b = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6].to_vec();\n for i in 0..n {\n for &j in a.iter() {\n if dp[i] == \"unko\" {\n continue;\n }\n dp[i + b[j]] = maxs(dp[i + b[j]].clone(), dp[i].clone() + &j.to_string());\n }\n }\n println!(\"{}\", dp[n]);\n}", "difficulty": "hard"} {"problem_id": "1605", "problem_description": "

Is it a Right Triangle?

\n\n

\nWrite a program which judges wheather given length of three side form a right triangle. Print \"YES\" if the given sides (integers) form a right triangle, \"NO\" if not so.\n

\n\n

Input

\n\n

\nInput consists of several data sets. In the first line, the number of data set, N is given. Then, N lines follow, each line corresponds to a data set. A data set consists of three integers separated by a single space.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ length of the side ≤ 1,000
  • \n
  • N ≤ 1,000
  • \n
\n\n\n

Output

\n\n

\nFor each data set, print \"YES\" or \"NO\".\n

\n\n

Sample Input

\n
\n3\n4 3 5\n4 3 6\n8 8 8\n
\n\n

Output for the Sample Input

\n\n
\nYES\nNO\nNO\n
", "c_code": "int solution(void) {\n int kosu = 0;\n int a = 0;\n int b = 0;\n int c = 0;\n\n scanf(\"%d\", &kosu);\n\n for (; kosu > 0; --kosu) {\n scanf(\"%d%d%d\", &a, &b, &c);\n if (a * a == b * b + c * c || b * b == a * a + c * c ||\n c * c == a * a + b * b) {\n puts(\"YES\");\n } else {\n puts(\"NO\");\n }\n a = 0;\n b = 0;\n c = 0;\n }\n\n return (0);\n}", "rust_code": "fn solution() {\n let input = BufReader::new(stdin());\n for line in input.lines() {\n let mut values: Vec = line\n .unwrap()\n .split_whitespace()\n .filter_map(|x| x.parse::().ok())\n .collect();\n if values.len() == 1 {\n continue;\n }\n values.sort();\n let values: Vec = values.into_iter().map(|x| x.pow(2)).collect();\n if values[2] == (values[0] + values[1]) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1606", "problem_description": "\n

Score: 500 points

\n
\n
\n

Problem Statement

\n

N programmers are going to participate in the preliminary stage of DDCC 20XX. Due to the size of the venue, however, at most 9 contestants can participate in the finals.

\n

The preliminary stage consists of several rounds, which will take place as follows:

\n
    \n
  • All the N contestants will participate in the first round.
  • \n
  • When X contestants participate in some round, the number of contestants advancing to the next round will be decided as follows:
      \n
    • The organizer will choose two consecutive digits in the decimal notation of X, and replace them with the sum of these digits. The number resulted will be the number of contestants advancing to the next round.
      \nFor example, when X = 2378, the number of contestants advancing to the next round will be 578 (if 2 and 3 are chosen), 2108 (if 3 and 7 are chosen), or 2315 (if 7 and 8 are chosen).
      \nWhen X = 100, the number of contestants advancing to the next round will be 10, no matter which two digits are chosen.
    • \n
    \n
  • \n
  • The preliminary stage ends when 9 or fewer contestants remain.
  • \n
\n

Ringo, the chief organizer, wants to hold as many rounds as possible.\nFind the maximum possible number of rounds in the preliminary stage.

\n

Since the number of contestants, N, can be enormous, it is given to you as two integer sequences d_1, \\ldots, d_M and c_1, \\ldots, c_M, which means the following: the decimal notation of N consists of c_1 + c_2 + \\ldots + c_M digits, whose first c_1 digits are all d_1, the following c_2 digits are all d_2, \\ldots, and the last c_M digits are all d_M.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq M \\leq 200000
  • \n
  • 0 \\leq d_i \\leq 9
  • \n
  • d_1 \\neq 0
  • \n
  • d_i \\neq d_{i+1}
  • \n
  • c_i \\geq 1
  • \n
  • 2 \\leq c_1 + \\ldots + c_M \\leq 10^{15}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
M\nd_1 c_1\nd_2 c_2\n:\nd_M c_M\n
\n
\n
\n
\n
\n

Output

\n

Print the maximum possible number of rounds in the preliminary stage.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n2 2\n9 1\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

In this case, N = 229 contestants will participate in the first round. One possible progression of the preliminary stage is as follows:

\n
    \n
  • 229 contestants participate in Round 1, 49 contestants participate in Round 2, 13 contestants participate in Round 3, and 4 contestants advance to the finals.
  • \n
\n

Here, three rounds take place in the preliminary stage, which is the maximum possible number.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1 1\n0 8\n7 1\n
\n
\n
\n
\n
\n

Sample Output 2

9\n
\n

In this case, 1000000007 will participate in the first round.

\n
\n
", "c_code": "int solution() {\n int m;\n scanf(\"%d\", &m);\n int i;\n long long int d[200005];\n long long int c[200005];\n for (i = 0; i < m; i++) {\n scanf(\"%lld %lld\", &d[i], &c[i]);\n }\n long long int ans = 0;\n for (i = 0; i < m; i++) {\n ans += c[i];\n }\n ans--;\n long long int v = 0;\n for (i = 0; i < m; i++) {\n v += d[i] * c[i];\n }\n ans += v / 10;\n while (v > 9) {\n v = v / 10 + v % 10;\n ans += v / 10;\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let M: 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 (d, c): (Vec, Vec) = {\n let (mut d, mut c) = (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 d.push(iter.next().unwrap().parse().unwrap());\n c.push(iter.next().unwrap().parse().unwrap());\n }\n (d, c)\n };\n\n let s = (0..M).map(|i| d[i] * c[i]).sum::();\n let y = if s <= 9 { 0 } else { 1 + (s - 10) / 9 };\n let ans = c.iter().sum::() - 1 + y;\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1607", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have a board with a 2 \\times N grid.\nSnuke covered the board with N dominoes without overlaps.\nHere, a domino can cover a 1 \\times 2 or 2 \\times 1 square.

\n

Then, Snuke decided to paint these dominoes using three colors: red, cyan and green.\nTwo dominoes that are adjacent by side should be painted by different colors.\nHere, it is not always necessary to use all three colors.

\n

Find the number of such ways to paint the dominoes, modulo 1000000007.

\n

The arrangement of the dominoes is given to you as two strings S_1 and S_2 in the following manner:

\n
    \n
  • Each domino is represented by a different English letter (lowercase or uppercase).
  • \n
  • The j-th character in S_i represents the domino that occupies the square at the i-th row from the top and j-th column from the left.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 52
  • \n
  • |S_1| = |S_2| = N
  • \n
  • S_1 and S_2 consist of lowercase and uppercase English letters.
  • \n
  • S_1 and S_2 represent a valid arrangement of dominoes.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS_1\nS_2\n
\n
\n
\n
\n
\n

Output

Print the number of such ways to paint the dominoes, modulo 1000000007.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\naab\nccb\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n

There are six ways as shown below:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 2

1\nZ\nZ\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

Note that it is not always necessary to use all the colors.

\n
\n
\n
\n
\n
\n

Sample Input 3

52\nRvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn\nRLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn\n
\n
\n
\n
\n
\n

Sample Output 3

958681902\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n char s1[n];\n char s2[n];\n scanf(\"%s\", s1);\n scanf(\"%s\", s2);\n\n long long exp = 1;\n int leftis2 = 0;\n int leftis1 = 0;\n for (int i = 0; i < n; i++) {\n if (s1[i] == s2[i]) {\n if (leftis1 == 1) {\n exp *= 2;\n } else if (leftis2 == 1) {\n exp *= 1;\n } else {\n exp *= 3;\n }\n leftis1 = 1;\n leftis2 = 0;\n } else {\n if (leftis1 == 1) {\n exp *= 2;\n } else if (leftis2 == 1) {\n exp *= 3;\n } else {\n exp *= 6;\n }\n leftis1 = 0;\n leftis2 = 1;\n i++;\n }\n if (exp > 1000000008) {\n exp = exp % 1000000007;\n }\n }\n printf(\"%lld\", exp);\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 s: Vec = itr.next().unwrap().chars().collect();\n let t: Vec = itr.next().unwrap().chars().collect();\n\n let mut op = Vec::new();\n const MOD: u64 = 1_000_000_007;\n\n let mut i = 0;\n while i < n {\n if s[i] != t[i] {\n op.push(0);\n i += 1;\n } else {\n op.push(1);\n }\n i += 1;\n }\n\n let mut ans = 0u64;\n if op[0] == 0 {\n ans += 6;\n } else {\n ans += 3;\n }\n for i in 1..op.len() {\n if op[i - 1] == 0 {\n if op[i] == 0 {\n ans = ans * 3 % MOD;\n }\n } else {\n ans = ans * 2 % MOD;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1608", "problem_description": "You are given two binary strings $$$x$$$ and $$$y$$$, which are binary representations of some two integers (let's denote these integers as $$$f(x)$$$ and $$$f(y)$$$). You can choose any integer $$$k \\ge 0$$$, calculate the expression $$$s_k = f(x) + f(y) \\cdot 2^k$$$ and write the binary representation of $$$s_k$$$ in reverse order (let's denote it as $$$rev_k$$$). For example, let $$$x = 1010$$$ and $$$y = 11$$$; you've chosen $$$k = 1$$$ and, since $$$2^1 = 10_2$$$, so $$$s_k = 1010_2 + 11_2 \\cdot 10_2 = 10000_2$$$ and $$$rev_k = 00001$$$.For given $$$x$$$ and $$$y$$$, you need to choose such $$$k$$$ that $$$rev_k$$$ is lexicographically minimal (read notes if you don't know what does \"lexicographically\" means).It's guaranteed that, with given constraints, $$$k$$$ exists and is finite.", "c_code": "int solution() {\n int T;\n char strX[100001];\n char strY[100001];\n scanf(\"%d\", &T);\n while (T--) {\n scanf(\"%s %s\", strX, strY);\n int lenX = strlen(strX);\n int lenY = strlen(strY);\n int idxY = -1;\n for (int i = lenY - 1; i >= 0; --i) {\n if (strY[i] == '1') {\n idxY = i;\n break;\n }\n }\n if (idxY == -1) {\n puts(\"0\");\n } else {\n char find = 0;\n int ans = 0;\n for (int i = lenX - 1 - (lenY - 1 - idxY); i >= 0; --i) {\n if (strX[i] == '1') {\n find = 1;\n break;\n }\n ++ans;\n }\n printf(\"%d\\n\", find ? ans : 0);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n let mut stdin = stdin.lock();\n let t: usize = {\n let mut string = String::new();\n stdin.read_line(&mut string);\n string\n .split_ascii_whitespace()\n .next()\n .unwrap()\n .parse()\n .unwrap()\n };\n\n for _ in 0..t {\n let mut x = String::new();\n let mut y = String::new();\n stdin.read_line(&mut x);\n stdin.read_line(&mut y);\n let x = x.bytes().collect::>();\n let y = y.bytes().collect::>();\n\n let yf = y\n .iter()\n .rev()\n .enumerate()\n .find(|&it| *it.1 == b'1')\n .unwrap()\n .0;\n let xf = x\n .iter()\n .rev()\n .enumerate()\n .skip(yf)\n .find(|&it| *it.1 == b'1')\n .unwrap()\n .0;\n\n println!(\"{}\", xf - yf);\n }\n}", "difficulty": "medium"} {"problem_id": "1609", "problem_description": "Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with $$$n$$$ integers written on it.Polycarp wrote the numbers from the disk into the $$$a$$$ array. It turned out that the drive works according to the following algorithm: the drive takes one positive number $$$x$$$ as input and puts a pointer to the first element of the $$$a$$$ array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the $$$a$$$ array, the last element is again followed by the first one; as soon as the sum is at least $$$x$$$, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you $$$m$$$ questions. To answer the $$$i$$$-th of them, you need to find how many seconds the drive will work if you give it $$$x_i$$$ as input. Please note that in some cases the drive can work infinitely.For example, if $$$n=3, m=3$$$, $$$a=[1, -3, 4]$$$ and $$$x=[1, 5, 2]$$$, then the answers to the questions are as follows: the answer to the first query is $$$0$$$ because the drive initially points to the first item and the initial sum is $$$1$$$. the answer to the second query is $$$6$$$, the drive will spin the disk completely twice and the amount becomes $$$1+(-3)+4+1+(-3)+4+1=5$$$. the answer to the third query is $$$2$$$, the amount is $$$1+(-3)+4=2$$$.", "c_code": "int solution() {\n int t;\n int n;\n int m;\n static long long a[200000] = {[0 ... 199999] = 0};\n static long long ascA[200000] = {[0 ... 199999] = 0};\n static long long x[200000] = {[0 ... 199999] = 0};\n int ans;\n int rm;\n int upbound;\n long long step;\n long long lcnt;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%d %d\", &n, &m);\n lcnt = 0;\n scanf(\"%d\", &a[0]);\n ascA[lcnt++] = 0;\n for (int j = 1; j < n; j++) {\n scanf(\"%d\", &a[j]);\n a[j] += a[j - 1];\n if (a[j] > a[ascA[lcnt - 1]]) {\n ascA[lcnt++] = j;\n }\n }\n upbound = (int)lcnt;\n lcnt = a[n - 1];\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &ans);\n step = 0;\n int up = upbound;\n int low = -1;\n int mid = (up + low) / 2;\n if (lcnt > 0) {\n if (a[ascA[up - 1]] < ans) {\n rm = ans - a[ascA[up - 1]];\n step = rm / lcnt + !!(rm % lcnt);\n ans -= lcnt * step;\n step *= n;\n }\n } else {\n if (a[ascA[up - 1]] < ans) {\n step = -1;\n }\n }\n if (step != -1) {\n while (up - low > 1) {\n if (a[ascA[mid]] >= ans) {\n up = mid;\n } else {\n low = mid;\n }\n mid = (up + low) / 2;\n }\n step += ascA[up];\n }\n x[j] = step;\n }\n for (int j = 0; j < m; j++) {\n printf(\"%d \", x[j]);\n }\n printf(\"\\n\");\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, q): (usize, usize) = (sc.next(), sc.next());\n let arr: Vec = (0..n).map(|_| sc.next()).collect();\n\n let mut pref_sum: Vec = arr.clone();\n (1..n).for_each(|i| pref_sum[i] += pref_sum[i - 1]);\n\n let mut max_arr: Vec = pref_sum.clone();\n (1..n).for_each(|i| max_arr[i] = max(max_arr[i], max_arr[i - 1]));\n\n let mx: i64 = *pref_sum.iter().max().unwrap();\n let sum: i64 = pref_sum[n - 1];\n\n for _ in 0..q {\n let mut x: i64 = sc.next();\n let mut ans: i64 = 0;\n if mx < x {\n if sum <= 0 {\n write!(out, \"-1 \").unwrap();\n continue;\n }\n let times = (x - mx + sum - 1) / sum;\n ans += times * n as i64;\n x -= times * sum;\n }\n let (mut start, mut end, mut idx): (i32, i32, i32) = (0, n as i32 - 1, 0);\n while start <= end {\n let mid: i32 = (start + end) >> 1;\n if max_arr[mid as usize] >= x {\n idx = mid;\n end = mid - 1;\n } else {\n start = mid + 1;\n }\n }\n ans += idx as i64;\n write!(out, \"{} \", ans).unwrap();\n }\n writeln!(out).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1610", "problem_description": "

How Many Divisors?

\n\n

\n Write a program which reads three integers a, b and c, and prints the number of divisors of c between a and b.\n

\n\n\n

Input

\n\n

\n Three integers a, b and c are given in a line separated by a single space.\n

\n\n

Output

\n\n

\nPrint the number of divisors in a line.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ a, b, c ≤ 10000
  • \n
  • ab
  • \n
\n\n

Sample Input 1

\n
\n5 14 80\n
\n

Sample Output 1

\n
\n3\n
", "c_code": "int solution(void) {\n\n int a = 0;\n scanf(\"%d\", &a);\n int b = 0;\n scanf(\"%d\", &b);\n int c = 0;\n scanf(\"%d\", &c);\n\n int counter = 0;\n\n for (int i = 1; i <= c; i++) {\n\n if (c % i == 0) {\n\n if (((c / i) >= a) && ((c / i) <= b)) {\n counter++;\n }\n }\n }\n\n printf(\"%d\\n\", counter);\n\n return 0;\n}", "rust_code": "fn solution() {\n let 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 println!(\n \"{}\",\n (v[0]..v[1] + 1).filter(|i| v[2].is_multiple_of(*i)).count()\n );\n}", "difficulty": "hard"} {"problem_id": "1611", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

10^{10^{10}} participants, including Takahashi, competed in two programming contests.\nIn each contest, all participants had distinct ranks from first through 10^{10^{10}}-th.

\n

The score of a participant is the product of his/her ranks in the two contests.

\n

Process the following Q queries:

\n
    \n
  • In the i-th query, you are given two positive integers A_i and B_i. Assuming that Takahashi was ranked A_i-th in the first contest and B_i-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq Q \\leq 100
  • \n
  • 1\\leq A_i,B_i\\leq 10^9(1\\leq i\\leq Q)
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
Q\nA_1 B_1\n:\nA_Q B_Q\n
\n
\n
\n
\n
\n

Output

For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8\n1 4\n10 5\n3 3\n4 11\n8 9\n22 40\n8 36\n314159265 358979323\n
\n
\n
\n
\n
\n

Sample Output 1

1\n12\n4\n11\n14\n57\n31\n671644785\n
\n

Let us denote a participant who was ranked x-th in the first contest and y-th in the second contest as (x,y).

\n

In the first query, (2,1) is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print 1.

\n
\n
", "c_code": "int solution() {\n int i;\n int Q;\n long long A;\n long long B;\n long long C;\n long long ans;\n scanf(\"%d\", &Q);\n for (i = 1; i <= Q; i++) {\n scanf(\"%lld %lld\", &A, &B);\n C = (long long)sqrt(A * B);\n ans = (C - 1) * 2;\n if (C * (C + 1) < A * B) {\n ans++;\n } else if (C * C == A * B) {\n ans--;\n }\n if (A != B) {\n printf(\"%lld\\n\", ans);\n } else {\n printf(\"%lld\\n\", A + B - 2);\n }\n }\n\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n q: usize,\n v: [(usize, usize); q]\n }\n for &(a, b) in &v {\n let x = min(a, b);\n let y = max(a, b);\n let mut l = 0;\n let mut r = y;\n while l + 1 < r {\n let m = (l + r) / 2;\n if m * m < x * y {\n l = m;\n } else {\n r = m;\n }\n }\n if x == y || x + 1 == y {\n println!(\"{}\", 2 * x - 2);\n } else if l * (l + 1) >= x * y {\n println!(\"{}\", 2 * l - 2);\n } else {\n println!(\"{}\", 2 * l - 1);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1612", "problem_description": "The winner of the card game popular in Berland \"Berlogging\" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line \"name score\", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points.", "c_code": "int solution() {\n int n;\n int i;\n int t[1005] = {0};\n int g[1005];\n int yer = 0;\n int kontrol;\n int k;\n int j;\n int max;\n int sonuc;\n int puan[1005] = {0};\n char c[1005][35] = {0};\n char s[1005][35];\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n kontrol = 1;\n scanf(\"%s %d\", s[i], &g[i]);\n for (j = 0; j < yer; j++) {\n if (!strcmp(c[j], s[i])) {\n t[j] += g[i];\n kontrol = 0;\n }\n }\n if (kontrol) {\n for (k = 0; k < 35; k++) {\n c[yer][k] = s[i][k];\n }\n t[yer] += g[i];\n yer++;\n }\n }\n\n max = t[0];\n for (i = 1; i < yer; i++) {\n if (t[i] > max) {\n max = t[i];\n }\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < yer; j++) {\n if (!strcmp(c[j], s[i])) {\n puan[j] += g[i];\n if (puan[j] >= max && t[j] == max) {\n printf(\"%s\", c[j]);\n return 0;\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let stdin = std::io::stdin();\n let stdin = stdin.lock();\n let mut lines = stdin.lines();\n\n let n = lines.next().unwrap()?.parse::().unwrap();\n let mut a = Vec::<(String, i32)>::new();\n let mut scores = HashMap::::new();\n for _ in 0..n {\n let line = lines.next().unwrap().unwrap();\n let mut line = line.split_whitespace();\n let (name, score) = (line.next().unwrap(), line.next().unwrap());\n let score = score.parse::()?;\n a.push((name.to_string(), score));\n *scores.entry(name.to_string()).or_default() += score;\n }\n let mx = *scores.values().max().unwrap();\n let cnt = scores.values().filter(|s| **s == mx).count();\n let mut ans = String::new();\n\n if cnt == 1 {\n ans = scores\n .iter()\n .find(|(_, v)| **v == mx)\n .unwrap()\n .0\n .to_string();\n } else {\n let mut scores2 = HashMap::::new();\n for (name, score) in a {\n *scores2.entry(name.clone()).or_default() += score;\n if scores2[&name] >= mx && scores[&name] == mx {\n ans = name;\n break;\n }\n }\n }\n\n println!(\"{}\", ans);\n\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "1613", "problem_description": "Recall that the sequence $$$b$$$ is a a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$a=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is $$$k$$$ then your task is to find the maximum sum of elements of some alternating subsequence of length $$$k$$$.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int size = 0;\n long long int sum = 0;\n scanf(\"%d\", &size);\n long long int arr[size];\n for (int i = 1; i <= size; i++) {\n scanf(\"%lld\", &arr[i]);\n }\n\n arr[0] = -arr[1];\n\n int j = 0;\n int k = 1;\n while (k <= size) {\n if (arr[k] * arr[j] < 0) {\n if (k <= size - 1) {\n int p = k + 1;\n while (arr[p] * arr[k] > 0 && p <= size) {\n if (arr[p] >= arr[k]) {\n k = p;\n }\n p++;\n }\n }\n\n sum = sum + arr[k];\n j = k;\n }\n k++;\n }\n printf(\"%lld\\n\", sum);\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\n let mut out = Vec::new();\n for _ in 0..t {\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 if a.iter().all(|&x| x > 0) || a.iter().all(|&x| x < 0) {\n writeln!(out, \"{}\", a.iter().max().unwrap()).ok();\n } else {\n let mut i = 0;\n let mut arr = Vec::new();\n let mut max = -(1 << 60);\n while i < n {\n max = std::cmp::max(max, a[i]);\n if i + 1 < n && a[i] * a[i + 1] < 0 {\n arr.push(max);\n max = -(1 << 60);\n }\n i += 1;\n }\n arr.push(max);\n writeln!(out, \"{}\", arr.iter().sum::()).ok();\n }\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "medium"} {"problem_id": "1614", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.

\n

The color of the i-th square from the left is black if the i-th character of S is B, and white if that character is W.

\n

You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.

\n

Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.

\n

Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.

\n

Two ways to make the squares white are considered different if and only if there exists i (1 \\leq i \\leq N) such that the pair of the squares chosen in the i-th operation is different.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • |S| = 2N
  • \n
  • Each character of S is B or W.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\nBWWB\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

There are four ways to make all the squares white, as follows:

\n
    \n
  • Choose Squares 1, 3 in the first operation, and choose Squares 2, 4 in the second operation.
  • \n
  • Choose Squares 2, 4 in the first operation, and choose Squares 1, 3 in the second operation.
  • \n
  • Choose Squares 1, 4 in the first operation, and choose Squares 2, 3 in the second operation.
  • \n
  • Choose Squares 2, 3 in the first operation, and choose Squares 1, 4 in the second operation.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4\nBWBBWWWB\n
\n
\n
\n
\n
\n

Sample Output 2

288\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5\nWWWWWWWWWW\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n long long int n;\n scanf(\"%lld\\n\", &n);\n char s[200005];\n scanf(\"%s\", s);\n long long int p = 1e9 + 7;\n long long int i;\n long long int count = 0;\n long long int ans = 1;\n for (i = 1; i <= n; i++) {\n ans = ans * i % p;\n }\n for (i = 0; i < 2 * n; i++) {\n if (s[i] == 'B') {\n if (count % 2 == 0) {\n count++;\n } else {\n ans = ans * count % p;\n count--;\n }\n } else {\n if (count % 2 == 1) {\n count++;\n } else {\n ans = ans * count % p;\n count--;\n }\n }\n }\n if (count != 0) {\n ans = 0;\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let N: i64 = {\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: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n };\n\n let MOD = 1_000_000_007;\n\n let ans = {\n let (x, y) = S.iter().fold(\n ((1..(N + 1)).fold(1, |f, i| (f * i) % MOD), 0),\n |(res, k), &c| {\n if c == 'B' && k % 2 == 0 || c == 'W' && k % 2 == 1 {\n (res, k + 1)\n } else {\n ((res * k) % MOD, k - 1)\n }\n },\n );\n if y == 0 {\n x\n } else {\n 0\n }\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1615", "problem_description": "You are given an array $$$a$$$ of $$$n$$$ integers.You want to make all elements of $$$a$$$ equal to zero by doing the following operation exactly three times: Select a segment, for each number in this segment we can add a multiple of $$$len$$$ to it, where $$$len$$$ is the length of this segment (added integers can be different). It can be proven that it is always possible to make all elements of $$$a$$$ equal to zero.", "c_code": "int solution() {\n long long int n;\n scanf(\"%lld\", &n);\n int arr[n];\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n }\n if (n == 1) {\n printf(\"%d %d\\n%d\\n\", 1, 1, -arr[0]);\n printf(\"1 1\\n0\\n1 1\\n0\");\n return 0;\n }\n printf(\"%d %lld\\n\", 1, n - 1);\n for (int i = 0; i < n - 1; i++) {\n printf(\"%lld \", (n - 1) * arr[i]);\n }\n printf(\"\\n%d %lld\\n\", 1, n);\n for (int i = 0; i < n; i++) {\n printf(\"%lld \", -n * arr[i]);\n }\n printf(\"\\n%lld %lld\\n\", n, n);\n printf(\"%lld\", (arr[n - 1] * (n - 1)));\n return 0;\n}", "rust_code": "fn solution() {\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 a: 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 = Vec::with_capacity(6);\n if n == 1 {\n ans.push(\"1 1\\n0\".to_string());\n ans.push(\"1 1\\n0\".to_string());\n ans.push(format!(\"1 1\\n{}\", -a[0]));\n } else {\n ans.push(format!(\"1 1\\n{}\", -a[0]));\n let tmp: Vec<_> = { (1..n).map(|i| (-a[i] * n as i64).to_string()).collect() };\n ans.push(format!(\"1 {}\\n0 {}\", n, tmp.join(\" \")));\n let tmp: Vec<_> = {\n (1..n)\n .map(|i| (a[i] * (n - 1) as i64).to_string())\n .collect()\n };\n ans.push(format!(\"2 {}\\n{}\", n, tmp.join(\" \")));\n }\n println!(\"{}\", ans.join(\"\\n\"));\n}", "difficulty": "medium"} {"problem_id": "1616", "problem_description": "Alice and Bob are playing a game on an array $$$a$$$ of $$$n$$$ positive integers. Alice and Bob make alternating moves with Alice going first.In his/her turn, the player makes the following move: If $$$a_1 = 0$$$, the player loses the game, otherwise: Player chooses some $$$i$$$ with $$$2\\le i \\le n$$$. Then player decreases the value of $$$a_1$$$ by $$$1$$$ and swaps $$$a_1$$$ with $$$a_i$$$. Determine the winner of the game if both players play optimally.", "c_code": "int solution() {\n int t;\n int n;\n char c[10];\n scanf(\"%d\", &t);\n while (t > 0) {\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 1; i < n; ++i) {\n if (a[0] <= a[i]) {\n strcpy(c, \"Bob\");\n } else {\n strcpy(c, \"Alice\");\n break;\n }\n }\n printf(\"%s\\n\", c);\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin().lines().flatten().for_each(|s| {\n let mut i = s.split_ascii_whitespace().flat_map(str::parse::);\n if let Some(a) = i.next()\n && let Some(m) = i.min() {\n writeln!(buf, \"{}\", if a > m { \"Alice\" } else { \"Bob\" }).ok();\n }\n });\n}", "difficulty": "easy"} {"problem_id": "1617", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

\n

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.\nThe lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.\nIn each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

\n

Snuke wants to maximize the number of squares lighted by the lamp.

\n

You are given H strings S_i (1 \\leq i \\leq H), each of length W. If the j-th character (1 \\leq j \\leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

\n

Find the maximum possible number of squares lighted by the lamp.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H \\leq 2,000
  • \n
  • 1 \\leq W \\leq 2,000
  • \n
  • S_i is a string of length W consisting of # and ..
  • \n
  • . occurs at least once in one of the strings S_i (1 \\leq i \\leq H).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\nS_1\n:\nS_H\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number of squares lighted by the lamp.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 6\n#..#..\n.....#\n....#.\n#.#...\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

\n
\n
\n
\n
\n
\n

Sample Input 2

8 8\n..#...#.\n....#...\n##......\n..###..#\n...#..#.\n##....#.\n#...#...\n###.#..#\n
\n
\n
\n
\n
\n

Sample Output 2

13\n
\n
\n
", "c_code": "int solution() {\n int h;\n int w;\n scanf(\"%d %d\", &h, &w);\n int field[2005][2005] = {0};\n for (int i = 0; i < h; i++) {\n char s[2005];\n scanf(\"%s\", s);\n for (int j = 0; j < w; j++) {\n if (s[j] == '#') {\n field[i + 1][j + 1] = 1;\n }\n }\n }\n\n int tate[2020][2020] = {0};\n for (int i = 1; i <= h; i++) {\n for (int j = 1; j <= w; j++) {\n if (tate[i][j] || field[i][j]) {\n continue;\n }\n int stack[2020] = {0};\n int now = i;\n while (!field[now][j] && now <= h) {\n stack[++stack[0]] = now++;\n }\n int max = stack[0];\n while (stack[0]) {\n tate[stack[stack[0]--]][j] = max;\n }\n }\n }\n\n int yoko[2020][2020] = {0};\n for (int i = 1; i <= h; i++) {\n for (int j = 1; j <= w; j++) {\n if (yoko[i][j] || field[i][j]) {\n continue;\n }\n int stack[2020] = {0};\n int now = j;\n while (!field[i][now] && now <= w) {\n stack[++stack[0]] = now++;\n }\n int max = stack[0];\n while (stack[0]) {\n yoko[i][stack[stack[0]--]] = max;\n }\n }\n }\n\n int ans = 0;\n for (int i = 1; i <= h; i++) {\n for (int j = 1; j <= w; j++) {\n if (field[i][j]) {\n continue;\n }\n if (tate[i][j] + yoko[i][j] > ans) {\n ans = tate[i][j] + yoko[i][j];\n }\n }\n }\n\n printf(\"%d\\n\", ans - 1);\n return 0;\n}", "rust_code": "fn solution() {\n let (h, w) = {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).ok();\n let mut integers = line.split_whitespace().map(|e| e.parse::().unwrap());\n (integers.next().unwrap(), integers.next().unwrap())\n };\n\n let mut s: Vec> = Vec::new();\n for _ in 0..h {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).ok();\n s.push(line.chars().collect());\n }\n\n let mut l: Vec> = Vec::new();\n let mut r: Vec> = Vec::new();\n let mut u: Vec> = Vec::new();\n let mut d: Vec> = Vec::new();\n for x in 0..h {\n l.push(Vec::new());\n r.push(Vec::new());\n u.push(Vec::new());\n d.push(Vec::new());\n l[x].resize(w, 0);\n r[x].resize(w, 0);\n u[x].resize(w, 0);\n d[x].resize(w, 0);\n }\n\n for x in 0..h {\n for y in (0..w).rev() {\n r[x][y] = if s[x][y] == '#' {\n 0\n } else {\n if y == w - 1 {\n 1\n } else {\n r[x][y + 1] + 1\n }\n }\n }\n }\n\n for x in 0..h {\n for y in 0..w {\n l[x][y] = if s[x][y] == '#' {\n 0\n } else {\n if y == 0 {\n 1\n } else {\n l[x][y - 1] + 1\n }\n }\n }\n }\n\n for y in 0..w {\n for x in (0..h).rev() {\n u[x][y] = if s[x][y] == '#' {\n 0\n } else {\n if x == h - 1 {\n 1\n } else {\n u[x + 1][y] + 1\n }\n }\n }\n }\n\n for y in 0..w {\n for x in 0..h {\n d[x][y] = if s[x][y] == '#' {\n 0\n } else {\n if x == 0 {\n 1\n } else {\n d[x - 1][y] + 1\n }\n }\n }\n }\n\n let mut answer = 0;\n for x in 0..h {\n for y in 0..w {\n if s[x][y] == '.' {\n let t = l[x][y] + r[x][y] + u[x][y] + d[x][y] - 3;\n answer = std::cmp::max(answer, t);\n }\n }\n }\n\n println!(\"{}\", answer);\n}", "difficulty": "medium"} {"problem_id": "1618", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.

\n

According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:

\n
    \n
  • All even numbers written on the document are divisible by 3 or 5.
  • \n
\n

If the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.

\n
\n
\n
\n
\n

Notes

\n
    \n
  • The condition in the statement can be rephrased as \"If x is an even number written on the document, x is divisible by 3 or 5\".\nHere \"if\" and \"or\" are logical terms.
  • \n
\n
\n
\n
\n
\n

Constraints

\n
    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq A_i \\leq 1000
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 \\dots A_N\n
\n
\n
\n
\n
\n

Output

\n

If the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n6 7 9 10 31\n
\n
\n
\n
\n
\n

Sample Output 1

APPROVED\n
\n

The even numbers written on the document are 6 and 10.

\n

All of them are divisible by 3 or 5, so the immigrant should be allowed entry.

\n
\n
\n
\n
\n
\n

Sample Input 2

3\n28 27 24\n
\n
\n
\n
\n
\n

Sample Output 2

DENIED\n
\n

28 violates the condition, so the immigrant should not be allowed entry.

\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n int a = 0;\n int flag = 1;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a);\n if (a % 2 == 0) {\n if (a % 3 != 0 && a % 5 != 0) {\n flag = 0;\n }\n }\n }\n\n if (flag == 1) {\n printf(\"APPROVED\");\n } else {\n printf(\"DENIED\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut number_count = String::new();\n io::stdin().read_line(&mut number_count).unwrap();\n number_count.pop();\n let mut numbers = String::new();\n io::stdin().read_line(&mut numbers).unwrap();\n numbers.pop();\n let numbers: Vec = numbers\n .split(' ')\n .map(|s| u32::from_str(s).unwrap())\n .collect();\n for number in numbers {\n if number % 2 == 0 && !(number % 3 == 0 || number % 5 == 0) {\n println!(\"DENIED\");\n return;\n }\n }\n println!(\"APPROVED\");\n}", "difficulty": "easy"} {"problem_id": "1619", "problem_description": "

改元

\n\n

平成31年4月30日をもって現行の元号である平成が終了し,その翌日より新しい元号が始まることになった.平成最後の日の翌日は新元号元年5月1日になる.

\n

ACM-ICPC OB/OGの会 (Japanese Alumni Group; JAG) が開発するシステムでは,日付が和暦(元号とそれに続く年数によって年を表現する日本の暦)を用いて \"平成 ymd 日\" という形式でデータベースに保存されている.この保存形式は変更することができないため,JAGは元号が変更されないと仮定して和暦で表した日付をデータベースに保存し,出力の際に日付を正しい元号を用いた形式に変換することにした.

\n

あなたの仕事はJAGのデータベースに保存されている日付を,平成または新元号を用いた日付に変換するプログラムを書くことである.新元号はまだ発表されていないため,\"?\" を用いて表すことにする.

\n\n\n

Input

\n\n\n\n

入力は複数のデータセットからなる.各データセットは次の形式で表される.

\n
g y m d
\n

g は元号を表す文字列であり, g=HEISEI が成り立つ.y, m, d は整数であり,それぞれ年,月,日を表す.1 ≤ y ≤ 100, 1 ≤ m ≤ 12, 1 ≤ d ≤ 31 が成り立つ.

\n

2月30日などの和暦に存在しない日付はデータセットとして与えられない.和暦として正しく変換したときに,平成よりも前の元号を用いる必要がある日付もデータセットとして与えられない.

\n

入力の終わりは '#' 一つのみからなる行であらわされる.データセットの個数は 100 個を超えない.

\n\n\n

Output

\n\n\n\n

各データセットについて,変換後の元号,年,月,日を空白で区切って 1 行に出力せよ.変換後の元号が \"平成\" である場合は \"HEISEI\" を元号として用い,新元号であれば \"?\" を用いよ.

\n

通常,元号の第 1 年目は元年と表記するが,本問題の出力ではこの規則を無視し,1 を年として出力せよ.

\n\n\n\n

Sample Input

HEISEI 1 1 8\nHEISEI 31 4 30\nHEISEI 31 5 1\nHEISEI 99 12 31\nHEISEI 38 8 30\nHEISEI 98 2 22\nHEISEI 2 3 26\nHEISEI 28 4 23\n#\n

Output for the Sample Input

HEISEI 1 1 8\nHEISEI 31 4 30\n? 1 5 1\n? 69 12 31\n? 8 8 30\n? 68 2 22\nHEISEI 2 3 26\nHEISEI 28 4 23\n
", "c_code": "int solution() {\n int y;\n int m;\n int d;\n char g[10];\n\n while (1) {\n scanf(\"%s%d%d%d\", g, &y, &m, &d);\n if (strcmp(g, \"#\") == 0) {\n break;\n }\n if ((y == 31 && m > 4) || y > 31) {\n printf(\"? %d %d %d\\n\", y - 30, m, d);\n } else {\n printf(\"%s %d %d %d\\n\", g, y, m, d);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n loop {\n let reader = std::io::stdin();\n let mut line = String::new();\n let _r = reader.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n let mut v = vec[0];\n if v == \"#\" {\n break;\n } else {\n let mut a: i64 = vec[1].parse().unwrap();\n let b: i64 = vec[2].parse().unwrap();\n let c: i64 = vec[3].parse().unwrap();\n if 31 < a {\n v = \"?\";\n a -= 30;\n } else if 31 == a && 5 <= b {\n v = \"?\";\n a -= 30;\n }\n println!(\"{} {} {} {}\", v, a, b, c);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1620", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi.

\n

Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours.\nThe time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours.\nHere, we are using 24-hour notation.\nThat is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example.

\n

Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours.

\n

Find the maximum possible value of s.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 50
  • \n
  • 0 \\leq D_i \\leq 12
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nD_1 D_2 ... D_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible value of s.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n7 12 8\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

For example, consider the situation where it is 7, 12 and 16 o'clock in each person's city at the moment when it is 0 o'clock in Takahashi's city. In this case, the time gap between the second and third persons' cities is 4 hours.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n11 11\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1\n0\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

Note that Takahashi himself is also a participant.

\n
\n
", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int i;\n int d[55];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &d[i]);\n }\n d[n] = 0;\n for (i = 0; i < n; i++) {\n if (d[i] > d[i + 1]) {\n d[i] ^= d[i + 1];\n d[i + 1] ^= d[i];\n d[i] ^= d[i + 1];\n if (i > 0) {\n i -= 2;\n }\n }\n }\n for (i = 0; i <= n; i += 2) {\n d[i] *= -1;\n }\n for (i = 0; i < n; i++) {\n if (d[i] > d[i + 1]) {\n d[i] ^= d[i + 1];\n d[i + 1] ^= d[i];\n d[i] ^= d[i + 1];\n if (i > 0) {\n i -= 2;\n }\n }\n }\n int s = 50;\n for (i = 0; i < n; i++) {\n if (s > d[i + 1] - d[i]) {\n s = d[i + 1] - d[i];\n }\n }\n if (s > d[0] + 24 - d[n]) {\n s = d[0] + 24 - d[n];\n }\n printf(\"%d\\n\", s);\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 d: Vec = (0..n)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n d.push(0);\n d.sort();\n\n let mut unko = Vec::new();\n for i in 0..n + 1 {\n if i % 2 == 0 {\n unko.push(d[i]);\n } else {\n unko.push((24 - d[i]) % 24);\n }\n }\n unko.sort();\n unko.push(24);\n\n let mut ans = 1 << 30;\n for i in 0..n + 1 {\n ans = min(ans, unko[i + 1] - unko[i]);\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1621", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given is an integer r.

\n

How many times is the area of a circle of radius r larger than the area of a circle of radius 1?

\n

It can be proved that the answer is always an integer under the constraints given.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq r \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
r\n
\n
\n
\n
\n
\n

Output

Print the area of a circle of radius r, divided by the area of a circle of radius 1, as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

The area of a circle of radius 2 is 4 times larger than the area of a circle of radius 1.

\n

Note that output must be an integer - for example, 4.0 will not be accepted.

\n
\n
\n
\n
\n
\n

Sample Input 2

100\n
\n
\n
\n
\n
\n

Sample Output 2

10000\n
\n
\n
", "c_code": "int solution() {\n int r = 0;\n scanf(\"%d\", &r);\n printf(\"%d\\n\", r * r);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).ok();\n let r = buf.trim().parse::().unwrap();\n\n println!(\"{}\", r * r);\n}", "difficulty": "easy"} {"problem_id": "1622", "problem_description": "

休憩スペース (Refreshment Area)

\n\n

問題

\n\n

\n世界的なプログラミングコンテストが日本で開催されることになり,現在会場の設営が行われている.この会場は南北方向に N マス,東西方向に M マスのマス目状に区切られており,いくつかのマスには競技用の機材が置かれている.\n

\n\n

\nこのコンテストでは,選手が競技中に休憩するために,軽食や飲み物などを置いた休憩スペースを 1 箇所会場内に設けることになった.休憩スペースは南北方向または東西方向に連続した D マスでなければならない.ただし,機材の置かれたマス目に休憩スペースを設けることはできない.\n

\n\n

\n会場内に休憩スペースを設ける方法は何通りあるかを求めるプログラムを作成せよ.\n

\n\n

入力

\n\n

\n入力は 1 + N 行からなる.\n

\n\n

\n1 行目には 3 個の整数 N, M, D (1 ≦ N ≦ 100, 1 ≦ M ≦ 100, 2 ≦ D ≦ 100) が空白を区切りとして書かれており,会場が南北方向に N マス,東西方向に M マスのマス目状に区切られており,休憩スペースが南北方向または東西方向に連続した D マスからなることを表す.\n

\n\n

\n続く N 行にはそれぞれ M 文字からなる文字列が書かれており,会場の情報を表す.\nN 行のうちの i 行目の j 文字目 (1 ≦ i ≦ N, 1 ≦ j ≦ M) は,会場のマス目の北から i 行目,西から j 列目のマスの状態を表す '#' または '.' のいずれかの文字である.'#' はそのマスに機材が置かれていることを,'.' はそのマスに機材が置かれていないことを表す.\n

\n\n

出力

\n\n

\n会場内に休憩スペースを設ける方法は何通りあるかを 1 行で出力せよ.\n

\n\n

入出力例

\n

入力例 1

\n\n
\n3 5 2\n...#.\n#...#\n....#\n
\n\n

出力例 1

\n\n
\n12\n
\n
\n\n

入力例 2

\n\n
\n4 7 5\n.#.....\n.....##\n.......\n#......\n
\n\n

出力例 2

\n\n
\n7\n
\n\n

\n入出力例 1 では,下の図に示すように,休憩スペースを設ける方法は全部で 12 通りある.\n

\n\n
\n\"picture\"\n
\n
\n\n
\n", "c_code": "int solution(void) {\n int n;\n int m;\n int d;\n int i;\n int j;\n int l;\n int g = 0;\n char a[300][300];\n scanf(\"%d %d %d\", &n, &m, &d);\n for (i = 0; i < n; i++) {\n scanf(\"%s\", a[i]);\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n l = 0;\n while (l <= d - 1) {\n if (a[i + l][j] == '.' && l != d - 1) {\n l++;\n } else if (a[i + l][j] == '.' && l == d - 1) {\n g += 1;\n l++;\n } else {\n l = 100;\n }\n }\n l = 0;\n while (l <= d - 1) {\n if (a[i][j + l] == '.' && l != d - 1) {\n l++;\n } else if (a[i][j + l] == '.' && l == d - 1) {\n g += 1;\n l++;\n } else {\n l = 100;\n }\n }\n }\n }\n printf(\"%d\\n\", g);\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.splitn(2, '\\n');\n\n let (n, m, d) = {\n let line = lines.next().unwrap();\n let mut iter = line.split(' ').map(|s| s.parse().unwrap());\n\n (\n iter.next().unwrap(),\n iter.next().unwrap(),\n iter.next().unwrap(),\n )\n };\n\n if n < d && m < d {\n println!(\"0\");\n return;\n }\n\n let mut chars = lines.next().unwrap().bytes();\n\n let mut table = vec![0; m];\n let mut cnt = 0;\n\n for _ in 0..n {\n let mut x = 0;\n\n for (ch, y) in chars.by_ref().zip(table.iter_mut()) {\n if ch == b'.' {\n x += 1;\n *y += 1;\n } else {\n if x >= d {\n cnt += x - d + 1;\n }\n if *y >= d {\n cnt += *y - d + 1;\n }\n x = 0;\n *y = 0;\n }\n }\n\n if x >= d {\n cnt += x - d + 1;\n }\n }\n\n for y in table {\n if y >= d {\n cnt += y - d + 1;\n }\n }\n\n println!(\"{}\", cnt);\n}", "difficulty": "easy"} {"problem_id": "1623", "problem_description": "You have a large electronic screen which can display up to $$$998244353$$$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $$$7$$$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $$$10$$$ decimal digits:As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $$$1$$$, you have to turn on $$$2$$$ segments of the screen, and if you want to display $$$8$$$, all $$$7$$$ segments of some place to display a digit should be turned on.You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $$$n$$$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $$$n$$$ segments.Your program should be able to process $$$t$$$ different test cases.", "c_code": "int solution() {\n int t;\n int n[100];\n\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &n[i]);\n }\n\n for (int i = 0; i < t; i++) {\n if (n[i] % 2 == 0) {\n for (int j = 0; j < n[i] / 2; j++) {\n printf(\"1\");\n }\n } else {\n printf(\"7\");\n for (int j = 0; j < (n[i] / 2) - 1; j++) {\n printf(\"1\");\n }\n }\n printf(\"\\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 n = lines.next().unwrap().unwrap().parse::().unwrap();\n let mut inputs = Vec::new();\n for _ in 0..n {\n let tmp = lines.next().unwrap().unwrap().parse::().unwrap();\n inputs.push(tmp);\n }\n for mut input in inputs.into_iter() {\n match input % 2 == 1 {\n true => print!(\"{}\", 7),\n false => print!(\"{}\", 1),\n }\n input -= 2 + input % 2;\n while input > 0 {\n input -= 2;\n print!(\"{}\", 1);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1624", "problem_description": "You are given a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters.A substring of string $$$s$$$ is a continuous segment of letters from $$$s$$$. For example, \"defor\" is a substring of \"codeforces\" and \"fors\" is not. The length of the substring is the number of letters in it.Let's call some string of length $$$n$$$ diverse if and only if there is no letter to appear strictly more than $$$\\frac n 2$$$ times. For example, strings \"abc\" and \"iltlml\" are diverse and strings \"aab\" and \"zz\" are not.Your task is to find any diverse substring of string $$$s$$$ or report that there is none. Note that it is not required to maximize or minimize the length of the resulting substring.", "c_code": "int solution(void) {\n int ch = 0;\n while (islower(ch) == 0) {\n ch = getchar();\n }\n int prev = ch;\n int next = ch;\n while (prev == next && ch != '\\n') {\n prev = next;\n next = ch;\n ch = getchar();\n }\n if (prev != next) {\n puts(\"YES\");\n putchar(prev);\n putchar(next);\n } else {\n puts(\"NO\");\n }\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n std::io::stdin().read_line(&mut n).expect(\"\");\n let _n: i32 = n.trim().parse().expect(\"\");\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).expect(\"\");\n let s = String::from(s.trim());\n let s: Vec<_> = s.chars().collect();\n\n let mut l = 0;\n let mut r = 0;\n for i in 0..s.len() {\n let mut cnt = [0; 26];\n for j in i..s.len() {\n cnt[s[j] as usize - 97] += 1;\n let mut ok = true;\n for k in 0..cnt.len() {\n if cnt[k] > (j - i).div_ceil(2) {\n ok = false;\n continue;\n }\n }\n if ok {\n l = i;\n r = j;\n }\n }\n }\n if r == 0 {\n println!(\"NO\");\n } else {\n println!(\"YES\");\n for k in l..r + 1 {\n print!(\"{}\", s[k]);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1625", "problem_description": "Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.", "c_code": "int solution() {\n char StrA[11];\n char StrB[11];\n char StrC[11];\n char StrD[11];\n int n;\n scanf(\"%s %s\", StrA, StrB);\n scanf(\"%d\", &n);\n printf(\"%s %s\\n\", StrA, StrB);\n while (n--) {\n scanf(\"%s %s\", StrC, StrD);\n if (strcmp(StrC, StrA) == 0) {\n memcpy(StrA, StrD, sizeof(StrA));\n } else if (strcmp(StrC, StrB) == 0) {\n memcpy(StrB, StrD, sizeof(StrB));\n }\n printf(\"%s %s\\n\", StrA, StrB);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut names = String::new();\n let mut n = String::new();\n io::stdin().read_line(&mut names).unwrap();\n let mut iter = names.split_whitespace();\n let mut a = iter.next().unwrap().to_string();\n let mut b = iter.next().unwrap().to_string();\n io::stdin().read_line(&mut n).unwrap();\n let n: i32 = n.trim().parse().unwrap();\n println!(\"{} {}\", a, b);\n for _ in 0..n {\n let mut next = String::new();\n io::stdin().read_line(&mut next).unwrap();\n let mut iter = next.split_whitespace();\n let c = iter.next().unwrap();\n let d = iter.next().unwrap().to_string();\n print!(\"{} \", d);\n if c == a {\n println!(\"{}\", b);\n a = d;\n } else {\n println!(\"{}\", a);\n b = d;\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1626", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

\n

Initially, you are in Square X.\nYou can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.\nHowever, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.\nIt is guaranteed that there is no toll gate in Square 0, Square X and Square N.

\n

Find the minimum cost incurred before reaching the goal.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 1 \\leq M \\leq 100
  • \n
  • 1 \\leq X \\leq N - 1
  • \n
  • 1 \\leq A_1 < A_2 < ... < A_M \\leq N
  • \n
  • A_i \\neq X
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M X\nA_1 A_2 ... A_M\n
\n
\n
\n
\n
\n

Output

Print the minimum cost incurred before reaching the goal.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 3 3\n1 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

The optimal solution is as follows:

\n
    \n
  • First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.
  • \n
  • Then, travel from Square 4 to Square 5. This time, no cost is incurred.
  • \n
  • Now, we are in Square 5 and we have reached the goal.
  • \n
\n

In this case, the total cost incurred is 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

7 3 2\n4 5 6\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

We may be able to reach the goal at no cost.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 7 5\n1 2 3 4 6 8 9\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution(void) {\n\n static int n;\n static int m;\n static int x;\n static int left;\n static int right;\n scanf(\"%d%d%d\", &n, &m, &x);\n\n for (int i = 0; i < m; i++) {\n\n int a[i];\n scanf(\"%d\", &a[i]);\n\n if (a[i] < x) {\n\n left++;\n\n } else {\n\n right++;\n }\n }\n\n if (left < right) {\n\n printf(\"%d\\n\", left);\n\n } else {\n\n printf(\"%d\\n\", right);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n _n: usize,\n m: usize,\n x: u32,\n a: [u32;m]\n }\n let mut l = 0;\n let mut r = 0;\n for i in a {\n if i < x {\n l += 1;\n }\n if i > x {\n r += 1;\n }\n }\n println!(\"{}\", std::cmp::min(l, r));\n}", "difficulty": "medium"} {"problem_id": "1627", "problem_description": "A big football championship will occur soon! $$$n$$$ teams will compete in it, and each pair of teams will play exactly one game against each other.There are two possible outcomes of a game: the game may result in a tie, then both teams get $$$1$$$ point; one team might win in a game, then the winning team gets $$$3$$$ points and the losing team gets $$$0$$$ points. The score of a team is the number of points it gained during all games that it played.You are interested in a hypothetical situation when all teams get the same score at the end of the championship. A simple example of that situation is when all games result in ties, but you want to minimize the number of ties as well.Your task is to describe a situation (choose the result of each game) so that all teams get the same score, and the number of ties is the minimum possible.", "c_code": "int solution() {\n int x;\n int y;\n int z;\n int i;\n int j;\n int k;\n int a;\n int b;\n int c;\n int n;\n int m;\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d\", &n);\n i = 1;\n j = 2;\n for (x = 1; x <= n; x++) {\n for (y = x + 1; y <= n; y++) {\n if (!(n & 1) && x == i && j == y) {\n putchar('0');\n putchar(' ');\n i += 2;\n j += 2;\n } else {\n if (!((x + y) & 1)) {\n putchar('-');\n }\n putchar('1');\n putchar(' ');\n }\n }\n }\n\n puts(\"\");\n }\n return 0;\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: i32 = sc.next();\n let mut tot = n * (n - 1) / 2;\n let mut result: HashMap<(i32, i32), i32> = HashMap::new();\n let mut diff = 1;\n while tot >= n {\n for i in 0..n {\n result.insert((i, (i + diff) % n), i);\n }\n tot -= n;\n diff += 1;\n }\n for i in 0..n {\n for j in i + 1..n {\n let winner = if result.contains_key(&(i, j)) {\n result.get(&(i, j))\n } else {\n result.get(&(j, i))\n };\n if let Some(&winner) = winner {\n write!(out, \"{} \", if winner == i { 1 } else { -1 }).unwrap();\n } else {\n write!(out, \"0 \").unwrap();\n }\n }\n }\n writeln!(out).unwrap();\n }\n}", "difficulty": "hard"} {"problem_id": "1628", "problem_description": "You are given an array of integers $$$a$$$ of length $$$n$$$. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by $$$1$$$; or you can select any red element and increase its value by $$$1$$$. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make $$$0$$$ or more steps such that the resulting array is a permutation of numbers from $$$1$$$ to $$$n$$$?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array $$$a$$$ contains in some order all numbers from $$$1$$$ to $$$n$$$ (inclusive), each exactly once.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &n);\n long long arr[n];\n char str[n];\n int inc[n + 1];\n int dec[n + 1];\n for (int j = 0; j < n; j++) {\n scanf(\"%lld\", &arr[j]);\n inc[j] = 0;\n dec[j] = 0;\n }\n dec[n] = 0;\n inc[n] = 0;\n scanf(\"%s\", str);\n int flag = 0;\n for (int j = 0; j < n; j++) {\n if (arr[j] <= 0 && str[j] == 'B') {\n flag = 1;\n break;\n }\n if (arr[j] <= 0 && str[j] == 'R') {\n inc[1]++;\n } else if (arr[j] > n && str[j] == 'R') {\n flag = 1;\n break;\n } else if (arr[j] > n && str[j] == 'B') {\n\n dec[n]++;\n\n } else {\n if (str[j] == 'R') {\n inc[arr[j]]++;\n } else {\n dec[arr[j]]++;\n }\n }\n }\n\n if (flag == 1) {\n printf(\"NO\\n\");\n } else {\n int p = n;\n long long suminc = 0;\n long long sumdec = 0;\n int q = 1;\n for (int j = n; j > 0; j--) {\n suminc += inc[j];\n if (inc[j] != 0 && suminc > n - j + 1) {\n flag = 1;\n\n break;\n }\n if (inc[j] != 0) {\n p = p - inc[j];\n }\n }\n\n for (int j = 1; j <= n; j++) {\n sumdec += dec[j];\n if (dec[j] != 0 && sumdec > j) {\n flag = 1;\n break;\n }\n\n if (dec[j] != 0) {\n q = q + dec[j];\n }\n }\n\n if (flag == 1) {\n printf(\"NO\\n\");\n\n } else {\n if (p == q - 1 || p == 0 || q == n + 1) {\n printf(\"YES\\n\");\n } else {\n\n printf(\"NO\\n\");\n }\n }\n }\n }\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut s = String::new();\n io::stdin().read_to_string(&mut s)?;\n let mut it = s.split_whitespace().map(|s| s.to_string());\n let t: usize = it.next().unwrap().parse().unwrap();\n let mut out = String::new();\n for _case in 1..=t {\n let n: usize = it.next().unwrap().parse().unwrap();\n let mut a = Vec::with_capacity(n);\n for _ in 0..n {\n let x: i32 = it.next().unwrap().parse().unwrap();\n a.push(x);\n }\n let colors: String = it.next().unwrap().parse().unwrap();\n let mut blue = vec![];\n let mut red = vec![];\n for (c, x) in colors.bytes().zip(a.into_iter()) {\n if c == b'R' {\n red.push(x);\n } else {\n blue.push(x);\n }\n }\n red.sort();\n blue.sort();\n let _perm = vec![0; n];\n let mut ans = \"YES\";\n let mut right = n as i32;\n for &v in red.iter().rev() {\n if v <= right {\n right -= 1;\n } else {\n ans = \"NO\";\n }\n }\n let mut left = 1;\n for &v in blue.iter() {\n if v >= left {\n left += 1;\n } else {\n ans = \"NO\";\n }\n }\n let ans = format!(\"{}\\n\", ans);\n out.push_str(ans.as_str());\n }\n print!(\"{}\", out);\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1629", "problem_description": "Given a sequence $$$a_1, a_2, \\ldots, a_n$$$, find the minimum number of elements to remove from the sequence such that after the removal, the sum of every $$$2$$$ consecutive elements is even.", "c_code": "int solution() {\n int num_case = 0;\n scanf(\"%d\", &num_case);\n\n int nums = 0;\n int number = 0;\n int odd_cnt = 0;\n for (int i = 0; i < num_case; i++) {\n scanf(\"%d\", &nums);\n for (int j = 0; j < nums; j++) {\n scanf(\"%d\", &number);\n if (number % 2) {\n odd_cnt++;\n }\n }\n if (odd_cnt < nums - odd_cnt) {\n printf(\"%d\\n\", odd_cnt);\n } else {\n printf(\"%d\\n\", nums - odd_cnt);\n }\n odd_cnt = 0;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = stdin();\n\n let mut count = String::new();\n\n stdin.read_line(&mut count).unwrap();\n let count: u32 = count.trim().parse().unwrap();\n\n let mut data_read = String::new();\n for _ in 0..count {\n stdin.read_line(&mut data_read).unwrap();\n\n data_read.clear();\n\n stdin.read_line(&mut data_read).unwrap();\n\n let mut counts = [0, 0];\n\n for i in data_read.trim().split(' ') {\n let value: usize = i.parse().unwrap();\n counts[value % 2] += 1;\n }\n println!(\n \"{}\",\n if counts[0] > counts[1] {\n counts[1]\n } else {\n counts[0]\n }\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1630", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We have a 3×3 square grid, where each square contains a lowercase English letters.\nThe letter in the square at the i-th row from the top and j-th column from the left is c_{ij}.

\n

Print the string of length 3 that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

\n
\n
\n
\n
\n

Constraints

    \n
  • Input consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
c_{11}c_{12}c_{13}\nc_{21}c_{22}c_{23}\nc_{31}c_{32}c_{33}\n
\n
\n
\n
\n
\n

Output

Print the string of length 3 that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

ant\nobe\nrec\n
\n
\n
\n
\n
\n

Sample Output 1

abc\n
\n

The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are a, b and c from top-right to bottom-left. Concatenate these letters and print abc.

\n
\n
\n
\n
\n
\n

Sample Input 2

edu\ncat\nion\n
\n
\n
\n
\n
\n

Sample Output 2

ean\n
\n
\n
", "c_code": "int solution(void) {\n char str[9];\n scanf(\"%c%c%c\\n\", &str[0], &str[1], &str[2]);\n scanf(\"%c%c%c\\n\", &str[3], &str[4], &str[5]);\n scanf(\"%c%c%c\", &str[6], &str[7], &str[8]);\n printf(\"%c%c%c\", str[0], str[4], str[8]);\n return 0;\n}", "rust_code": "fn solution() {\n for i in 0..3 {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let v: Vec = s.trim().chars().collect();\n print!(\"{}\", v[i]);\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1631", "problem_description": "A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of $$$n$$$ rows and $$$m$$$ columns. The rows of the floor are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and columns of the floor are numbered from $$$1$$$ to $$$m$$$ from left to right. The cell on the intersection of the $$$r$$$-th row and the $$$c$$$-th column is denoted as $$$(r,c)$$$. The initial position of the robot is $$$(r_b, c_b)$$$.In one second, the robot moves by $$$dr$$$ rows and $$$dc$$$ columns, that is, after one second, the robot moves from the cell $$$(r, c)$$$ to $$$(r + dr, c + dc)$$$. Initially $$$dr = 1$$$, $$$dc = 1$$$. If there is a vertical wall (the left or the right walls) in the movement direction, $$$dc$$$ is reflected before the movement, so the new value of $$$dc$$$ is $$$-dc$$$. And if there is a horizontal wall (the upper or lower walls), $$$dr$$$ is reflected before the movement, so the new value of $$$dr$$$ is $$$-dr$$$.Each second (including the moment before the robot starts moving), the robot cleans every cell lying in the same row or the same column as its position. There is only one dirty cell at $$$(r_d, c_d)$$$. The job of the robot is to clean that dirty cell. Illustration for the first example. The blue arc is the robot. The red star is the target dirty cell. Each second the robot cleans a row and a column, denoted by yellow stripes. Given the floor size $$$n$$$ and $$$m$$$, the robot's initial position $$$(r_b, c_b)$$$ and the dirty cell's position $$$(r_d, c_d)$$$, find the time for the robot to do its job.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n int a[6];\n int dc = 1;\n int dr = 1;\n int s = 0;\n for (int j = 0; j < 6; j++) {\n scanf(\"%d\", &a[j]);\n }\n if (a[2] == a[4] || a[3] == a[5]) {\n printf(\"%d\\n\", s);\n } else {\n for (int z = 0;; z++) {\n if (a[2] == a[0]) {\n dc = dc * (-1);\n }\n if (a[3] == a[1]) {\n dr = dr * (-1);\n }\n a[2] = a[2] + dc;\n a[3] = a[3] + dr;\n s++;\n if (a[2] == a[4] || a[3] == a[5]) {\n printf(\"%d\\n\", s);\n break;\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n let mut input = input.split_ascii_whitespace().flat_map(str::parse);\n let t = input.next().unwrap();\n let mut output = String::new();\n for _ in 0..t {\n let n = input.next().unwrap();\n let m = input.next().unwrap();\n let mut rb = input.next().unwrap();\n let mut cb = input.next().unwrap();\n let rd = input.next().unwrap();\n let cd = input.next().unwrap();\n let mut r_defl = false;\n let mut c_defl = false;\n let mut t = 0;\n while rb != rd && cb != cd {\n if rb == 1 {\n r_defl = false;\n } else if rb == n {\n r_defl = true;\n }\n if cb == 1 {\n c_defl = false;\n } else if cb == m {\n c_defl = true;\n }\n if r_defl {\n rb -= 1;\n } else {\n rb += 1;\n }\n if c_defl {\n cb -= 1;\n } else {\n cb += 1;\n }\n t += 1;\n }\n writeln!(output, \"{}\", t).unwrap();\n }\n print!(\"{}\", output);\n}", "difficulty": "hard"} {"problem_id": "1632", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Let us consider the following operations on a string consisting of A and B:

\n
    \n
  1. Select a character in a string. If it is A, replace it with BB. If it is B, replace with AA.
  2. \n
  3. Select a substring that is equal to either AAA or BBB, and delete it from the string.
  4. \n
\n

For example, if the first operation is performed on ABA and the first character is selected, the string becomes BBBA.\nIf the second operation is performed on BBBAAAA and the fourth through sixth characters are selected, the string becomes BBBA.

\n

These operations can be performed any number of times, in any order.

\n

You are given two string S and T, and q queries a_i, b_i, c_i, d_i.\nFor each query, determine whether S_{a_i} S_{{a_i}+1} ... S_{b_i}, a substring of S, can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i}, a substring of T.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S|, |T| \\leq 10^5
  • \n
  • S and T consist of letters A and B.
  • \n
  • 1 \\leq q \\leq 10^5
  • \n
  • 1 \\leq a_i \\leq b_i \\leq |S|
  • \n
  • 1 \\leq c_i \\leq d_i \\leq |T|
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nT\nq\na_1 b_1 c_1 d_1\n...\na_q b_q c_q d_q\n
\n
\n
\n
\n
\n

Output

Print q lines. The i-th line should contain the response to the i-th query. If S_{a_i} S_{{a_i}+1} ... S_{b_i} can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i}, print YES. Otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

BBBAAAABA\nBBBBA\n4\n7 9 2 5\n7 9 1 4\n1 7 2 5\n1 7 2 4\n
\n
\n
\n
\n
\n

Sample Output 1

YES\nNO\nYES\nNO\n
\n

The first query asks whether the string ABA can be made into BBBA.\nAs explained in the problem statement, it can be done by the first operation.

\n

The second query asks whether ABA can be made into BBBB, and the fourth query asks whether BBBAAAA can be made into BBB.\nNeither is possible.

\n

The third query asks whether the string BBBAAAA can be made into BBBA.\nAs explained in the problem statement, it can be done by the second operation.

\n
\n
\n
\n
\n
\n

Sample Input 2

AAAAABBBBAAABBBBAAAA\nBBBBAAABBBBBBAAAAABB\n10\n2 15 2 13\n2 13 6 16\n1 13 2 20\n4 20 3 20\n1 18 9 19\n2 14 1 11\n3 20 3 15\n6 16 1 17\n4 18 8 20\n7 20 3 14\n
\n
\n
\n
\n
\n

Sample Output 2

YES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\n
\n
\n
", "c_code": "int solution() {\n char s[100005];\n char t[100005];\n scanf(\"%s%s\", s, t);\n int q;\n scanf(\"%d\", &q);\n int a[100005];\n int b[100005];\n int c[100005];\n int d[100005];\n int i;\n for (i = 0; i < q; i++) {\n scanf(\"%d %d %d %d\", &a[i], &b[i], &c[i], &d[i]);\n }\n int vs[100005];\n int vt[100005];\n vs[0] = 0;\n for (i = 0; s[i] != '\\0'; i++) {\n if (s[i] == 'A') {\n vs[i + 1] = vs[i] + 1;\n } else {\n vs[i + 1] = vs[i] + 2;\n }\n }\n vt[0] = 0;\n for (i = 0; t[i] != '\\0'; i++) {\n if (t[i] == 'A') {\n vt[i + 1] = vt[i] + 1;\n } else {\n vt[i + 1] = vt[i] + 2;\n }\n }\n for (i = 0; i < q; i++) {\n if ((vs[b[i]] - vs[a[i] - 1]) % 3 != (vt[d[i]] - vt[c[i] - 1]) % 3) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let S: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n };\n let T: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\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 (a, b, c, d): (Vec, Vec, Vec, Vec) = {\n let (mut a, mut b, mut c, mut d) = (vec![], vec![], 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 a.push(iter.next().unwrap().parse().unwrap());\n b.push(iter.next().unwrap().parse().unwrap());\n c.push(iter.next().unwrap().parse().unwrap());\n d.push(iter.next().unwrap().parse().unwrap());\n }\n (a, b, c, d)\n };\n\n let mut acc_S = vec![0; S.len() + 1];\n let mut acc_T = vec![0; T.len() + 1];\n for i in 0..(S.len()) {\n acc_S[i + 1] = if S[i] == 'A' { acc_S[i] + 1 } else { acc_S[i] };\n }\n for i in 0..(T.len()) {\n acc_T[i + 1] += if T[i] == 'A' { acc_T[i] + 1 } else { acc_T[i] };\n }\n let ans = (0..q)\n .map(|i| {\n let a_s = (acc_S[b[i]] - acc_S[a[i] - 1]) as i64;\n let b_s = ((b[i] - a[i] + 1) as i64) - a_s;\n let a_t = (acc_T[d[i]] - acc_T[c[i] - 1]) as i64;\n let b_t = ((d[i] - c[i] + 1) as i64) - a_t;\n if b_t % 3 == ((b_s + (a_t - a_s)) % 3 + 3) % 3 {\n \"YES\"\n } else {\n \"NO\"\n }\n })\n .collect::>()\n .join(\"\\n\");\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1633", "problem_description": "Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters \"1\" (the table is occupied) and \"0\" (the table is empty).Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$.For example, if $$$n=8$$$ and $$$k=2$$$, then: strings \"10010001\", \"10000010\", \"00000000\", \"00100000\" satisfy the rules of the restaurant; strings \"10100100\", \"10011001\", \"11111111\" do not satisfy to the rules of the restaurant, since each of them has a pair of \"1\" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without \"1\" or a string with one \"1\", then the requirement of the restaurant is satisfied.You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of \"0\" that can be replaced by \"1\" such that the requirement will still be satisfied?For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$ \"100010\", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t) {\n int n;\n int k;\n int count = 0;\n int zeros = 0;\n scanf(\"%d\", &n);\n scanf(\"%d\", &k);\n char a[n];\n scanf(\"%s\", a);\n for (int i = 0; i < n; i++) {\n if (a[i] == '0') {\n zeros++;\n }\n if (zeros > k || i == 0) {\n count++;\n zeros = 0;\n }\n if (a[i] == '1') {\n if (zeros < k && count != 0) {\n count--;\n }\n zeros = 0;\n }\n }\n printf(\"%d\\n\", count);\n t--;\n }\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, 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 s: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().chars().collect()\n };\n\n let mut ans = 0;\n let mut count = 0;\n let mut i = 0;\n while i < n {\n if s[i] == '1' {\n count = k;\n i += 1;\n continue;\n } else if count > 0 {\n count -= 1;\n i += 1;\n continue;\n }\n\n let mut j = 1;\n while j <= k && i + j < n {\n if s[i + j] == '1' {\n break;\n }\n j += 1;\n }\n if j > k || i + j == n {\n ans += 1;\n }\n\n i += j;\n }\n\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "1634", "problem_description": "Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with $$$n$$$ elements. The $$$i$$$-th element is $$$a_i$$$ ($$$i$$$ = $$$1, 2, \\ldots, n$$$). He gradually takes the first two leftmost elements from the deque (let's call them $$$A$$$ and $$$B$$$, respectively), and then does the following: if $$$A > B$$$, he writes $$$A$$$ to the beginning and writes $$$B$$$ to the end of the deque, otherwise, he writes to the beginning $$$B$$$, and $$$A$$$ writes to the end of the deque. We call this sequence of actions an operation.For example, if deque was $$$[2, 3, 4, 5, 1]$$$, on the operation he will write $$$B=3$$$ to the beginning and $$$A=2$$$ to the end, so he will get $$$[3, 4, 5, 1, 2]$$$.The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him $$$q$$$ queries. Each query consists of the singular number $$$m_j$$$ $$$(j = 1, 2, \\ldots, q)$$$. It is required for each query to answer which two elements he will pull out on the $$$m_j$$$-th operation.Note that the queries are independent and for each query the numbers $$$A$$$ and $$$B$$$ should be printed in the order in which they will be pulled out of the deque.Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides.", "c_code": "int solution() {\n int n;\n int q;\n scanf(\"%d %d\", &n, &q);\n int a[n];\n int b[n];\n int c[n];\n\n int A[n];\n int B[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d \", &a[i]);\n }\n for (int i = 1; i < n; i++) {\n c[i] = a[0];\n if (a[0] > a[i]) {\n b[i] = a[i];\n } else {\n b[i] = a[0];\n a[0] = a[i];\n }\n }\n b[0] = a[0];\n\n while (q--) {\n long long int m;\n scanf(\"%lld\", &m);\n if (m < n) {\n printf(\"%d %d\\n\", c[m], a[m]);\n } else {\n int t = (m - n) % (n - 1);\n printf(\"%d %d\\n\", b[0], b[t + 1]);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let stdout = io::stdout();\n let mut out = io::BufWriter::new(stdout.lock());\n let line = stdin.lock().lines().next().unwrap().unwrap();\n let nq: Vec = line.split(\" \").map(|i| i.parse().unwrap()).collect();\n let n = nq[0];\n let q = nq[1];\n\n let line = stdin.lock().lines().next().unwrap().unwrap();\n let mut a: Vec = line.split(\" \").map(|i| i.parse().unwrap()).collect();\n\n assert_eq!(n, a.len() as i32);\n\n let mut ans: Vec<(i32, i32)> = vec![];\n for i in 0..2 * n as usize {\n let mut A = a[i];\n let mut B = a[i + 1];\n ans.push((A, B));\n if A < B {\n mem::swap(&mut A, &mut B);\n }\n a[i + 1] = A;\n a.push(B);\n }\n\n for _ in 0..q as usize {\n let line = stdin.lock().lines().next().unwrap().unwrap();\n let mj: i64 = line.parse().unwrap();\n let mj = mj - 1;\n if mj < (n - 1) as i64 {\n writeln!(out, \"{} {}\", ans[mj as usize].0, ans[mj as usize].1).ok();\n } else {\n let m = mj % (n - 1) as i64 + (n - 1) as i64;\n writeln!(out, \"{} {}\", ans[m as usize].0, ans[m as usize].1).ok();\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1635", "problem_description": "We just discovered a new data structure in our research group: a suffix three!It's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in.It's super simple, 100% accurate, and doesn't involve advanced machine learning algorithms.Let us tell you how it works. If a sentence ends with \"po\" the language is Filipino. If a sentence ends with \"desu\" or \"masu\" the language is Japanese. If a sentence ends with \"mnida\" the language is Korean. Given this, we need you to implement a suffix three that can differentiate Filipino, Japanese, and Korean.Oh, did I say three suffixes? I meant four.", "c_code": "int solution() {\n int t;\n scanf(\"%d\\n\", &t);\n while (t--) {\n char str[1001];\n scanf(\"%[^\\n]%*c\\n\", str);\n int d = strlen(str);\n if (str[d - 1] == 'o' && str[d - 2] == 'p') {\n printf(\"FILIPINO\\n\");\n }\n if (str[d - 1] == 'u' && str[d - 2] == 's' && str[d - 3] == 'e' &&\n str[d - 4] == 'd') {\n printf(\"JAPANESE\\n\");\n }\n if (str[d - 1] == 'u' && str[d - 2] == 's' && str[d - 3] == 'a' &&\n str[d - 4] == 'm') {\n printf(\"JAPANESE\\n\");\n }\n if (str[d - 1] == 'a' && str[d - 2] == 'd' && str[d - 3] == 'i' &&\n str[d - 4] == 'n' && str[d - 5] == 'm') {\n printf(\"KOREAN\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut buffer = String::new();\n let input = stdin();\n input.read_line(&mut buffer)?;\n let x: u32 = buffer.trim().parse().expect(\"Not int\");\n for _ in 0..x {\n buffer = String::new();\n input.read_line(&mut buffer)?;\n if buffer.trim().ends_with(\"po\") {\n println!(\"FILIPINO\");\n } else if buffer.trim().ends_with(\"mnida\") {\n println!(\"KOREAN\");\n } else {\n println!(\"JAPANESE\");\n }\n }\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "1636", "problem_description": "A number $$$a_2$$$ is said to be the arithmetic mean of two numbers $$$a_1$$$ and $$$a_3$$$, if the following condition holds: $$$a_1 + a_3 = 2\\cdot a_2$$$. We define an arithmetic mean deviation of three numbers $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$ as follows: $$$d(a_1, a_2, a_3) = |a_1 + a_3 - 2 \\cdot a_2|$$$.Arithmetic means a lot to Jeevan. He has three numbers $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$ and he wants to minimize the arithmetic mean deviation $$$d(a_1, a_2, a_3)$$$. To do so, he can perform the following operation any number of times (possibly zero): Choose $$$i, j$$$ from $$$\\{1, 2, 3\\}$$$ such that $$$i \\ne j$$$ and increment $$$a_i$$$ by $$$1$$$ and decrement $$$a_j$$$ by $$$1$$$ Help Jeevan find out the minimum value of $$$d(a_1, a_2, a_3)$$$ that can be obtained after applying the operation any number of times.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int a[3];\n scanf(\"%d %d %d\", &a[0], &a[1], &a[2]);\n\n int s = a[0] + a[1] + a[2];\n if (s % 3 == 0) {\n printf(\"0\\n\");\n } else {\n printf(\"1\\n\");\n }\n }\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 t = u64::from_str(s.trim_end()).unwrap();\n for _ in 0..t {\n s.clear();\n std::io::stdin().read_line(&mut s).unwrap();\n println!(\n \"{}\",\n if s.split(' ')\n .map(|it| i64::from_str(it.trim()).unwrap() % 3)\n .sum::()\n % 3\n == 0\n {\n 0\n } else {\n 1\n }\n );\n }\n}", "difficulty": "easy"} {"problem_id": "1637", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

There are N piles of stones. The i-th pile has A_i stones.

\n

Aoki and Takahashi are about to use them to play the following game:

\n
    \n
  • Starting with Aoki, the two players alternately do the following operation:
      \n
    • Operation: Choose one pile of stones, and remove one or more stones from it.
    • \n
    \n
  • \n
  • When a player is unable to do the operation, he loses, and the other player wins.
  • \n
\n

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

\n

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.

\n

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 300
  • \n
  • 1 \\leq A_i \\leq 10^{12}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 \\ldots A_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n5 3\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

\n

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n3 5\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

It is not allowed to move stones from the 2-nd pile to the 1-st.

\n
\n
\n
\n
\n
\n

Sample Input 3

3\n1 1 2\n
\n
\n
\n
\n
\n

Sample Output 3

-1\n
\n

It is not allowed to move all stones from the 1-st pile.

\n
\n
\n
\n
\n
\n

Sample Input 4

8\n10 9 8 7 6 5 4 3\n
\n
\n
\n
\n
\n

Sample Output 4

3\n
\n
\n
\n
\n
\n
\n

Sample Input 5

3\n4294967297 8589934593 12884901890\n
\n
\n
\n
\n
\n

Sample Output 5

1\n
\n

Watch out for overflows.

\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n long long A[301];\n long long xor = 0;\n scanf(\"%d\", &N);\n for (i = 1; i <= N; i++) {\n scanf(\"%lld\", &(A[i]));\n xor ^= A[i];\n }\n if (xor== 0) {\n printf(\"0\\n\");\n fflush(stdout);\n return 0;\n }\n xor ^= A[1] ^ A[2];\n\n long long bit[41];\n long long x = A[1] + A[2];\n long long y = x - xor;\n for (i = 1, bit[0] = 1; i <= 40; i++) {\n bit[i] = bit[i - 1] << 1;\n }\n if (y < 0 || x < y || y % 2 == 1 || y / 2 > A[1]) {\n printf(\"-1\\n\");\n fflush(stdout);\n return 0;\n }\n y /= 2;\n\n int flag[41];\n long long a = y;\n long long b = y;\n long long c = y << 1;\n for (i = 0; i <= 40; i++) {\n flag[i] = ((y & bit[i]) > 0) ? 1 : 0;\n }\n for (i = 0; i <= 40; i++) {\n if ((x & bit[i]) == (c & bit[i])) {\n continue;\n }\n if (flag[i] == 1) {\n break;\n }\n flag[i] = -1;\n c += bit[i];\n }\n if (c != x) {\n printf(\"-1\\n\");\n fflush(stdout);\n return 0;\n }\n\n for (i = 40; i >= 0; i--) {\n if (flag[i] == -1 && a + bit[i] <= A[1]) {\n a += bit[i];\n }\n }\n if (a > 0) {\n printf(\"%lld\\n\", A[1] - a);\n } else {\n printf(\"-1\\n\");\n }\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\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 let a: 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 p = a[0] + a[1];\n let q = (2..n).fold(0, |sum, i| sum ^ a[i]);\n if p < q || (p - q) % 2 == 1 {\n println!(\"-1\");\n return;\n }\n let mut p = (p - q) >> 1;\n if (p & q) > 0 || p > a[0] {\n println!(\"-1\");\n return;\n }\n\n for i in (0..40).rev() {\n let b = 1 << i;\n if q & b > 0 && p + b <= a[0] {\n p += b;\n }\n }\n\n let ans = a[0] - p;\n if ans == a[0] {\n println!(\"-1\");\n } else {\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1638", "problem_description": "Fibonacci search", "c_code": "int solution(const int arr[], int x, int n) {\n\n int fibMMm2 = 0;\n int fibMMm1 = 1;\n int fibM = fibMMm2 + fibMMm1;\n\n while (fibM < n) {\n fibMMm2 = fibMMm1;\n fibMMm1 = fibM;\n fibM = fibMMm2 + fibMMm1;\n }\n\n int offset = -1;\n\n while (fibM > 1) {\n\n int i = ((offset + fibMMm2) < (n - 1)) ? (offset + fibMMm2) : (n - 1);\n\n if (arr[i] < x) {\n fibM = fibMMm1;\n fibMMm1 = fibMMm2;\n fibMMm2 = fibM - fibMMm1;\n offset = i;\n }\n\n else if (arr[i] > x) {\n fibM = fibMMm2;\n fibMMm1 = fibMMm1 - fibMMm2;\n fibMMm2 = fibM - fibMMm1;\n }\n\n else {\n return i;\n }\n }\n\n if (fibMMm1 && arr[offset + 1] == x) {\n return offset + 1;\n }\n\n return -1;\n}\n", "rust_code": "fn solution(item: &T, arr: &[T]) -> Option {\n let len = arr.len();\n if len == 0 {\n return None;\n }\n let mut start = -1;\n\n let mut f0 = 0;\n let mut f1 = 1;\n let mut f2 = 1;\n while f2 < len {\n f0 = f1;\n f1 = f2;\n f2 = f0 + f1;\n }\n while f2 > 1 {\n let index = min((f0 as isize + start) as usize, len - 1);\n match item.cmp(&arr[index]) {\n Ordering::Less => {\n f2 = f0;\n f1 -= f0;\n f0 = f2 - f1;\n }\n Ordering::Equal => return Some(index),\n Ordering::Greater => {\n f2 = f1;\n f1 = f0;\n f0 = f2 - f1;\n start = index as isize;\n }\n }\n }\n if (f1 != 0) && (&arr[len - 1] == item) {\n return Some(len - 1);\n }\n None\n}\n", "difficulty": "hard"} {"problem_id": "1639", "problem_description": "You are given two strings $$$s$$$ and $$$t$$$. The string $$$s$$$ consists of lowercase Latin letters and at most one wildcard character '*', the string $$$t$$$ consists only of lowercase Latin letters. The length of the string $$$s$$$ equals $$$n$$$, the length of the string $$$t$$$ equals $$$m$$$.The wildcard character '*' in the string $$$s$$$ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $$$s$$$ can be replaced with anything. If it is possible to replace a wildcard character '*' in $$$s$$$ to obtain a string $$$t$$$, then the string $$$t$$$ matches the pattern $$$s$$$.For example, if $$$s=$$$\"aba*aba\" then the following strings match it \"abaaba\", \"abacaba\" and \"abazzzaba\", but the following strings do not match: \"ababa\", \"abcaaba\", \"codeforces\", \"aba1aba\", \"aba?aba\".If the given string $$$t$$$ matches the given string $$$s$$$, print \"YES\", otherwise print \"NO\".", "c_code": "int solution() {\n int n;\n int m;\n int i = 0;\n int f = 0;\n int b = 0;\n scanf(\"%d\", &n);\n scanf(\"%d\", &m);\n int temp = n;\n char s[200001];\n char t[200001];\n scanf(\"%s\", s);\n scanf(\"%s\", t);\n while (s[i] != '\\0') {\n if (s[i] == '*') {\n temp = n - 1;\n }\n i++;\n }\n i = 0;\n if (temp <= m) {\n while (s[i] != '*' && t[i] != '\\0') {\n if (s[i] != t[i]) {\n f = 1;\n break;\n }\n i++;\n }\n i = 0;\n while (s[n - 1 - i] != '*' && m - 1 - i != -1) {\n if (s[n - 1 - i] != t[m - 1 - i]) {\n b = 1;\n break;\n }\n i++;\n }\n if (f == 1 || b == 1) {\n printf(\"NO\");\n } else {\n printf(\"YES\");\n }\n } else {\n printf(\"NO\");\n }\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\n let n = get!(usize);\n let m = get!(usize);\n let s = get().as_bytes();\n let t = get().as_bytes();\n\n let mut ok = true;\n\n if s.contains(&b'*') && n <= m + 1 {\n for i in 0..n {\n if s[i] == b'*' {\n break;\n }\n if s[i] != t[i] {\n ok = false;\n break;\n }\n }\n for i in 0..n {\n let j = (m - 1) - i;\n let i = (n - 1) - i;\n if s[i] == b'*' {\n break;\n }\n if s[i] != t[j] {\n ok = false;\n break;\n }\n }\n } else {\n ok = s == t;\n }\n\n if ok {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "easy"} {"problem_id": "1640", "problem_description": "You're given an array $$$b$$$ of length $$$n$$$. Let's define another array $$$a$$$, also of length $$$n$$$, for which $$$a_i = 2^{b_i}$$$ ($$$1 \\leq i \\leq n$$$). Valerii says that every two non-intersecting subarrays of $$$a$$$ have different sums of elements. You want to determine if he is wrong. More formally, you need to determine if there exist four integers $$$l_1,r_1,l_2,r_2$$$ that satisfy the following conditions: $$$1 \\leq l_1 \\leq r_1 \\lt l_2 \\leq r_2 \\leq n$$$; $$$a_{l_1}+a_{l_1+1}+\\ldots+a_{r_1-1}+a_{r_1} = a_{l_2}+a_{l_2+1}+\\ldots+a_{r_2-1}+a_{r_2}$$$. If such four integers exist, you will prove Valerii wrong. Do they exist?An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int arr[n];\n _Bool ok = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n for (int j = 0; j < i; j++) {\n if (arr[j] == arr[i] && ok == 0) {\n printf(\"YES\\n\");\n ok = 1;\n }\n }\n }\n if (ok == 0) {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let t: i32 = s.trim().parse().unwrap();\n for _ in 0..t {\n std::io::stdin().read_line(&mut s).unwrap();\n s.clear();\n let mut map = std::collections::HashSet::new();\n std::io::stdin().read_line(&mut s).unwrap();\n\n let mut result = || {\n for i in s.split_whitespace() {\n if map.contains(i) {\n return \"YES\";\n } else {\n map.insert(i);\n }\n }\n \"NO\"\n };\n println!(\"{}\", result());\n }\n}", "difficulty": "medium"} {"problem_id": "1641", "problem_description": "A bracket sequence is a string containing only characters \"(\" and \")\". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters \"1\" and \"+\" between the original characters of the sequence. For example, bracket sequences \"()()\" and \"(())\" are regular (the resulting expressions are: \"(1)+(1)\" and \"((1+1)+1)\"), and \")(\", \"(\" and \")\" are not.You are given an integer $$$n$$$. Your goal is to construct and print exactly $$$n$$$ different regular bracket sequences of length $$$2n$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n\n for (int i = 0; i < n * 2; i += 2) {\n for (int k = 0; k < i / 2; k++) {\n printf(\"()\");\n }\n\n for (int j = i; j < n * 2; j++) {\n if (j < (i + ((n * 2) - i) / 2)) {\n printf(\"(\");\n } else {\n printf(\")\");\n }\n }\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n let stdin = io::stdin();\n stdin.read_line(&mut input).unwrap();\n let mut t: i32 = input.trim().parse().unwrap();\n while t > 0 {\n let mut input = String::new();\n stdin.read_line(&mut input).unwrap();\n let n: usize = input.trim().parse().unwrap();\n for i in 1..n + 1 {\n print!(\"{}\", std::iter::repeat_n(\"(\", i).collect::());\n print!(\"{}\", std::iter::repeat_n(\")\", i).collect::());\n print!(\"{}\", std::iter::repeat_n(\"(\", n - i).collect::());\n print!(\"{}\", std::iter::repeat_n(\")\", n - i).collect::());\n println!();\n }\n t -= 1;\n }\n}", "difficulty": "easy"} {"problem_id": "1642", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given is a string S of length N-1.\nEach character in S is < or >.

\n

A sequence of N non-negative integers, a_1,a_2,\\cdots,a_N, is said to be good when the following condition is satisfied for all i (1 \\leq i \\leq N-1):

\n
    \n
  • If S_i= <: a_i<a_{i+1}
  • \n
  • If S_i= >: a_i>a_{i+1}
  • \n
\n

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 5 \\times 10^5
  • \n
  • S is a string of length N-1 consisting of < and >.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Find the minimum possible sum of the elements of a good sequence of N non-negative integers.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

<>>\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

a=(0,2,1,0) is a good sequence whose sum is 3.\nThere is no good sequence whose sum is less than 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

<>>><<><<<<<>>><\n
\n
\n
\n
\n
\n

Sample Output 2

28\n
\n
\n
", "c_code": "int solution(void) {\n char S[(5 * 100000) + 3];\n long long cnt_p = 0;\n long long cnt_m = 0;\n long long ans = 0;\n\n scanf(\"%s\", S);\n\n int i = 0;\n for (i = 0; S[i] != '\\0'; i++) {\n if (S[i] == '>') {\n cnt_m++;\n } else {\n cnt_p++;\n }\n if ((S[i] == '>' && S[i + 1] == '<') || S[i + 1] == '\\0') {\n if (cnt_p >= cnt_m) {\n ans += cnt_p * (cnt_p + 1) / 2 + cnt_m * (cnt_m - 1) / 2;\n } else {\n ans += cnt_p * (cnt_p - 1) / 2 + cnt_m * (cnt_m + 1) / 2;\n }\n cnt_p = 0;\n cnt_m = 0;\n }\n }\n\n printf(\"%lld\", ans);\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 s = buf.chars().collect::>();\n let n = buf.len() + 1;\n let mut vs: Vec = vec![0; n];\n\n for i in 0..n - 1 {\n if s[i] == '<' {\n vs[i + 1] = vs[i] + 1;\n }\n }\n\n for i in 0..n - 1 {\n let j = n - 2 - i;\n if s[j] == '>' {\n vs[j] = max(vs[j + 1] + 1, vs[j]);\n }\n }\n println!(\"{}\", vs.iter().fold(0, |sum, elem| sum + *elem));\n}", "difficulty": "medium"} {"problem_id": "1643", "problem_description": "Makes solves problems on Decoforces and lots of other different online judges. Each problem is denoted by its difficulty — a positive integer number. Difficulties are measured the same across all the judges (the problem with difficulty d on Decoforces is as hard as the problem with difficulty d on any other judge). Makes has chosen n problems to solve on Decoforces with difficulties a1, a2, ..., an. He can solve these problems in arbitrary order. Though he can solve problem i with difficulty ai only if he had already solved some problem with difficulty (no matter on what online judge was it).Before starting this chosen list of problems, Makes has already solved problems with maximum difficulty k.With given conditions it's easy to see that Makes sometimes can't solve all the chosen problems, no matter what order he chooses. So he wants to solve some problems on other judges to finish solving problems from his list. For every positive integer y there exist some problem with difficulty y on at least one judge besides Decoforces.Makes can solve problems on any judge at any time, it isn't necessary to do problems from the chosen list one right after another.Makes doesn't have too much free time, so he asked you to calculate the minimum number of problems he should solve on other judges in order to solve all the chosen problems from Decoforces.", "c_code": "int solution(int argc, char const *argv[]) {\n int n;\n int k;\n int i;\n int j;\n int ar[1001];\n int max = 0;\n int c = 0;\n int t;\n scanf(\"%d %d\", &n, &k);\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &ar[i]);\n }\n c = 1;\n while (c != 0) {\n c = 0;\n for (i = 1; i < n; i++) {\n if (ar[i - 1] > ar[i]) {\n t = ar[i];\n ar[i] = ar[i - 1];\n ar[i - 1] = t;\n c++;\n }\n }\n }\n c = 0;\n for (int i = 0; i < n; ++i) {\n if (ar[i] <= k * 2) {\n\n if (ar[i] > k) {\n k = ar[i];\n }\n continue;\n }\n while (k * 2 < ar[i]) {\n k = k * 2;\n\n c++;\n }\n if (ar[i] > k) {\n k = ar[i];\n }\n }\n printf(\"%d\\n\", c);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n std::io::stdin().read_line(&mut input).unwrap();\n\n let mut it = input.split_whitespace().map(|s| s.parse::().unwrap());\n\n let (_n, mut k) = (it.next().unwrap(), it.next().unwrap());\n\n let mut input = String::new();\n\n std::io::stdin().read_line(&mut input).unwrap();\n\n let a: Vec = input\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n\n let max = a.iter().max().unwrap();\n let mut ans = 0;\n\n while k * 2 < *max {\n let o_max = a.iter().filter(|&p| *p <= k * 2).max();\n match o_max {\n Some(p) if *p > k => k = *p,\n _ => {\n ans += 1;\n k *= 2;\n }\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1644", "problem_description": "You are given an integer $$$x$$$. Can you make $$$x$$$ by summing up some number of $$$11, 111, 1111, 11111, \\ldots$$$? (You can use any number among them any number of times).For instance, $$$33=11+11+11$$$ $$$144=111+11+11+11$$$", "c_code": "int solution() {\n int numInputs = 0;\n scanf(\"%d\", &numInputs);\n for (int i = 0; i < numInputs; i++) {\n int num = 0;\n scanf(\"%d\", &num);\n int r = num % 11;\n if (r * 111 <= num) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\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\n let t = lines.next().unwrap().parse::().unwrap();\n\n for _ in 0..t {\n let x = lines.next().unwrap().parse::().unwrap();\n\n let a = x / 111;\n let b = x % 11;\n\n let ans = if b <= a { \"YES\" } else { \"NO\" };\n\n writeln!(&mut output, \"{}\", ans).unwrap();\n }\n}", "difficulty": "easy"} {"problem_id": "1645", "problem_description": "Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game: First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal. Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off. Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!", "c_code": "int solution() {\n int clocks;\n scanf(\"%d\", &clocks);\n int A[clocks][2];\n int *Hashx = (int *)calloc(sizeof(int), 101);\n int *Hashy = (int *)calloc(sizeof(int), 101);\n for (int i = 0; i < clocks; i++) {\n for (int j = 0; j < 2; j++) {\n scanf(\"%d\", &A[i][j]);\n if (j == 0) {\n Hashx[A[i][j]]++;\n } else {\n Hashy[A[i][j]]++;\n }\n }\n }\n int countx = 0;\n int county = 0;\n for (int i = 0; i < 101; i++) {\n if (Hashx[i] != 0) {\n countx++;\n }\n }\n for (int i = 0; i < 101; i++) {\n if (Hashy[i] != 0) {\n county++;\n }\n }\n if (countx < county) {\n printf(\"%d\", countx);\n } else {\n printf(\"%d\", county);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let n = {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read line 1\");\n buf.trim().parse::().unwrap()\n };\n let mut rows: HashSet = HashSet::new();\n let mut cols: HashSet = HashSet::new();\n for _ in 0..n {\n let mut buf = String::new();\n stdin().read_line(&mut buf).expect(\"Failed to read line 1\");\n let point: Vec = buf\n .trim()\n .split_ascii_whitespace()\n .map(|c| c.parse().unwrap())\n .collect();\n rows.insert(point[0]);\n cols.insert(point[1]);\n }\n println!(\"{}\", min(rows.len(), cols.len()));\n}", "difficulty": "hard"} {"problem_id": "1646", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

We have N switches with \"on\" and \"off\" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

\n

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are \"on\" among these switches is congruent to p_i modulo 2.

\n

How many combinations of \"on\" and \"off\" states of the switches light all the bulbs?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N, M \\leq 10
  • \n
  • 1 \\leq k_i \\leq N
  • \n
  • 1 \\leq s_{ij} \\leq N
  • \n
  • s_{ia} \\neq s_{ib} (a \\neq b)
  • \n
  • p_i is 0 or 1.
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nk_1 s_{11} s_{12} ... s_{1k_1}\n:\nk_M s_{M1} s_{M2} ... s_{Mk_M}\np_1 p_2 ... p_M\n
\n
\n
\n
\n
\n

Output

Print the number of combinations of \"on\" and \"off\" states of the switches that light all the bulbs.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n2 1 2\n1 2\n0 1\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n
    \n
  • Bulb 1 is lighted when there is an even number of switches that are \"on\" among the following: Switch 1 and 2.
  • \n
  • Bulb 2 is lighted when there is an odd number of switches that are \"on\" among the following: Switch 2.
  • \n
\n

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 3\n2 1 2\n1 1\n1 2\n0 0 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
    \n
  • Bulb 1 is lighted when there is an even number of switches that are \"on\" among the following: Switch 1 and 2.
  • \n
  • Bulb 2 is lighted when there is an even number of switches that are \"on\" among the following: Switch 1.
  • \n
  • Bulb 3 is lighted when there is an odd number of switches that are \"on\" among the following: Switch 2.
  • \n
\n

Switch 1 has to be \"off\" to light Bulb 2 and Switch 2 has to be \"on\" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 2\n3 1 2 5\n2 2 3\n1 0\n
\n
\n
\n
\n
\n

Sample Output 3

8\n
\n
\n
", "c_code": "int solution() {\n\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n\n int k[m];\n int s[m][n];\n\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &k[i]);\n for (int j = 0; j < k[i]; j++) {\n scanf(\"%d\", &s[i][j]);\n }\n }\n\n int p[m];\n for (int i = 0; i < m; i++) {\n scanf(\"%d\", &p[i]);\n }\n\n int ret = 0;\n for (int bit = 0; bit < (1 << n); bit++) {\n int sum = 0;\n for (int i = 0; i < m; i++) {\n int temp = 0;\n for (int j = 0; j < k[i]; j++) {\n if ((bit >> (s[i][j] - 1)) & 1) {\n temp++;\n }\n }\n\n if (temp % 2 == p[i]) {\n sum++;\n }\n }\n if (sum == m) {\n ret++;\n }\n }\n\n printf(\"%d\", ret);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let v: Vec<&str> = s.split_whitespace().collect();\n let (n, m) = (\n v[0].parse::().unwrap(),\n v[1].parse::().unwrap(),\n );\n\n let mut lamps = Vec::new();\n for _i in 0..m {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let s = buf\n .split_whitespace()\n .skip(1)\n .map(|i| i.parse::().unwrap() - 1)\n .collect::>();\n lamps.push(s);\n }\n\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).unwrap();\n let p = buf\n .split_whitespace()\n .map(|i| i.parse::().unwrap())\n .collect::>();\n\n let mut ans = 0;\n 'a: for bit in 0..(1 << n) {\n let mut swc = vec![false; n];\n for sft in 0..n {\n if (bit >> sft) & 1 == 1 {\n swc[sft] = true;\n }\n }\n\n for (i, ls) in lamps.iter().enumerate() {\n if !ls.iter().filter(|&l| swc[*l]).count() % 2 == p[i] {\n continue 'a;\n }\n }\n ans += 1;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1647", "problem_description": "\n\n\n

Longest Common Subsequence

\n\n

\nFor given two sequences $X$ and $Y$, a sequence $Z$ is a common subsequence of $X$ and $Y$ if $Z$ is a subsequence of both $X$ and $Y$. For example, if $X = \\{a,b,c,b,d,a,b\\}$ and $Y = \\{b,d,c,a,b,a\\}$, the sequence $\\{b,c,a\\}$ is a common subsequence of both $X$ and $Y$. On the other hand, the sequence $\\{b,c,a\\}$ is not a longest common subsequence (LCS) of $X$ and $Y$, since it has length 3 and the sequence $\\{b,c,b,a\\}$, which is also common to both $X$ and $Y$, has length 4. The sequence $\\{b,c,b,a\\}$ is an LCS of $X$ and $Y$, since there is no common subsequence of length 5 or greater.\n

\n\n

\n Write a program which finds the length of LCS of given two sequences $X$ and $Y$. The sequence consists of alphabetical characters.\n

\n\n

Input

\n\n

\n The input consists of multiple datasets. In the first line, an integer $q$ which is the number of datasets is given. In the following $2 \\times q$ lines, each dataset which consists of the two sequences $X$ and $Y$ are given.\n

\n\n

Output

\n\n

\n For each dataset, print the length of LCS of $X$ and $Y$ in a line.\n

\n\n

Constraints

\n\n
    \n
  • $1 \\leq q \\leq 150$
  • \n
  • $1 \\leq$ length of $X$ and $Y$ $\\leq 1,000$
  • \n
  • $q \\leq 20$ if the dataset includes a sequence whose length is more than 100
  • \n
\n\n

Sample Input 1

\n
\n3\nabcbdab\nbdcaba\nabc\nabc\nabc\nbc\n
\n\n

Sample Output 1

\n
\n4\n3\n2\n
\n\n

Reference

\n\n

\nIntroduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.\n

", "c_code": "int solution() {\n int q;\n scanf(\"%d\", &q);\n for (int n = 0; n < q; n++) {\n char x[1010];\n char y[1010];\n int lenx;\n int leny;\n scanf(\"%s%n\", x, &lenx);\n scanf(\"%s%n\", y, &leny);\n lenx--;\n leny--;\n int dp[lenx + 1][leny + 1];\n\n for (int i = 0; i < lenx + 1; i++) {\n dp[i][0] = 0;\n }\n for (int j = 0; j < leny + 1; j++) {\n dp[0][j] = 0;\n }\n\n for (int i = 0; i < lenx; i++) {\n for (int j = 0; j < leny; j++) {\n if (x[i] == y[j]) {\n dp[i + 1][j + 1] = dp[i][j] + 1;\n } else {\n dp[i + 1][j + 1] =\n (dp[i + 1][j] > dp[i][j + 1]) ? dp[i + 1][j] : dp[i][j + 1];\n }\n }\n }\n printf(\"%d\\n\", dp[lenx][leny]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n q: usize,\n str: [(chars, chars); q]\n }\n\n for (l, r) in str {\n let llen = l.len();\n let rlen = r.len();\n\n let mut dp = vec![vec![0; llen + 1]; rlen + 1];\n\n for i in 0..rlen {\n for j in 0..llen {\n if r[i] == l[j] {\n dp[i + 1][j + 1] = dp[i][j] + 1;\n } else {\n dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);\n }\n }\n }\n println!(\"{}\", dp[rlen][llen]);\n }\n}", "difficulty": "medium"} {"problem_id": "1648", "problem_description": "Anton likes to play chess, and so does his friend Danik.Once they have played n games in a row. For each game it's known who was the winner — Anton or Danik. None of the games ended with a tie.Now Anton wonders, who won more games, he or Danik? Help him determine this.", "c_code": "int solution() {\n long n = 0;\n\n int a = 0;\n int d = 0;\n\n scanf(\"%ld\\n\", &n);\n\n if ((n >= 1) && (n <= 100000)) {\n\n char s[n];\n int i = 0;\n for (i = 0; i < n; i++) {\n scanf(\"%c\", s + i);\n if (s[i] == 'A') {\n a++;\n } else if (s[i] == 'D') {\n d++;\n }\n }\n s[i] = '\\0';\n\n if (a > d) {\n printf(\"Anton\");\n } else if (d > a) {\n printf(\"Danik\");\n } else {\n printf(\"Friendship\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut round = String::new();\n let mut winners = String::new();\n\n let _ = io::stdin().read_line(&mut round);\n let _ = io::stdin().read_line(&mut winners);\n\n let mut anton = 0;\n let mut danik = 0;\n for winner in winners.trim().chars() {\n if winner == 'A' {\n anton += 1;\n } else {\n danik += 1;\n }\n }\n\n if anton > danik {\n println!(\"Anton\");\n } else if anton < danik {\n println!(\"Danik\");\n } else {\n println!(\"Friendship\");\n }\n}", "difficulty": "easy"} {"problem_id": "1649", "problem_description": "You find yourself on an unusual crossroad with a weird traffic light. That traffic light has three possible colors: red (r), yellow (y), green (g). It is known that the traffic light repeats its colors every $$$n$$$ seconds and at the $$$i$$$-th second the color $$$s_i$$$ is on.That way, the order of the colors is described by a string. For example, if $$$s=$$$\"rggry\", then the traffic light works as the following: red-green-green-red-yellow-red-green-green-red-yellow- ... and so on.More formally, you are given a string $$$s_1, s_2, \\ldots, s_n$$$ of length $$$n$$$. At the first second the color $$$s_1$$$ is on, at the second — $$$s_2$$$, ..., at the $$$n$$$-th second the color $$$s_n$$$ is on, at the $$$n + 1$$$-st second the color $$$s_1$$$ is on and so on.You need to cross the road and that can only be done when the green color is on. You know which color is on the traffic light at the moment, but you don't know the current moment of time. You need to find the minimum amount of time in which you are guaranteed to cross the road.You can assume that you cross the road immediately. For example, with $$$s=$$$\"rggry\" and the current color r there are two options: either the green color will be on after $$$1$$$ second, or after $$$3$$$. That way, the answer is equal to $$$3$$$ — that is the number of seconds that we are guaranteed to cross the road, if the current color is r.", "c_code": "int solution() {\n int zushu;\n scanf(\"%d\", &zushu);\n for (int zu = 1; zu <= zushu; zu++) {\n int n;\n char c;\n scanf(\"%d %c\", &n, &c);\n\n char s[2 * n];\n scanf(\"%s\", s);\n for (int i = 0; i < n; i++) {\n s[i + n] = s[i];\n }\n\n int max = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == c) {\n for (int j = 0; j < n; j++) {\n if (s[i + j] == 'g') {\n i = i + j;\n if (j > max) {\n max = j;\n }\n break;\n }\n }\n }\n }\n printf(\"%d\", max);\n if (zu != zushu) {\n printf(\"\\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_c_str = line_iter.next().unwrap().unwrap();\n let (n_str, c_str) = n_c_str.split_once(' ').unwrap();\n let (_n, c) = (\n n_str.parse::().unwrap(),\n c_str.chars().next().unwrap(),\n );\n let s = line_iter.next().unwrap().unwrap();\n\n if c == 'g' {\n println!(\"0\");\n continue;\n }\n\n let mut first_g_found = false;\n let mut dist_to_first_g: usize = 0;\n let mut dist_to_green: usize = 0;\n let mut searching_for_green = false;\n let mut max_dist_to_green: usize = 0;\n\n for symbol in s.chars() {\n if searching_for_green {\n dist_to_green += 1;\n }\n if !first_g_found {\n dist_to_first_g += 1;\n }\n if symbol == 'g' {\n first_g_found = true;\n if searching_for_green {\n if dist_to_green > max_dist_to_green {\n max_dist_to_green = dist_to_green;\n }\n searching_for_green = false;\n }\n } else {\n if !searching_for_green && symbol == c {\n searching_for_green = true;\n dist_to_green = 0;\n }\n }\n }\n\n if searching_for_green {\n dist_to_green += dist_to_first_g;\n if dist_to_green > max_dist_to_green {\n max_dist_to_green = dist_to_green;\n }\n }\n println!(\"{0}\", max_dist_to_green);\n }\n}", "difficulty": "medium"} {"problem_id": "1650", "problem_description": "New Year is coming and you are excited to know how many minutes remain before the New Year. You know that currently the clock shows $$$h$$$ hours and $$$m$$$ minutes, where $$$0 \\le hh < 24$$$ and $$$0 \\le mm < 60$$$. We use 24-hour time format!Your task is to find the number of minutes before the New Year. You know that New Year comes when the clock shows $$$0$$$ hours and $$$0$$$ minutes.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n\n int n = 0;\n int i = 0;\n int c[n];\n int a = 0;\n int b = 0;\n\n scanf(\"%d\", &n);\n\n for (i = 0; i < n; i++) {\n scanf(\"%d %d\", &a, &b);\n printf(\"%d\\n\", ((23 - a) * 60) + 60 - b);\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 queries: i16 = line.trim().parse().unwrap();\n while queries > 0 {\n line.clear();\n input.read_line(&mut line).unwrap();\n let mut numbers = line.split_whitespace();\n let hours: i16 = numbers.next().unwrap().parse().unwrap();\n let minutes: i16 = numbers.next().unwrap().parse().unwrap();\n println!(\"{}\", 1440 - hours * 60 - minutes);\n queries -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "1651", "problem_description": "

Bit Flag

\n\n\n

\n A state with $n$ flags of ON or OFF can be represented by a sequence of bits where $0, 1, ..., n-1$ -th flag corresponds to 1 (ON) or 0 (OFF).\n The state can be managed by the corresponding decimal integer, because the sequence of bits is a binary representation where each bit is 0 or 1.\n

\n\n

\n Given a sequence of bits with 64 flags which represent a state, perform the following operations. Note that each flag of the bits is initialized by OFF.\n

\n\n
    \n
  • test(i): \t\tPrint 1 if $i$-th flag is ON, otherwise 0
  • \t\n
  • set(i): \t\tSet $i$-th flag to ON
  • \n
  • clear(i):\t\tSet $i$-th flag to OFF
  • \n
  • flip(i): \t\tInverse $i$-th flag
  • \n
  • all:\t\tPrint 1 if all flags are ON, otherwise 0
  • \n
  • any:\t\tPrint 1 if at least one flag is ON, otherwise 0
  • \n
  • none:\t\tPrint 1 if all flags are OFF, otherwise 0
  • \n
  • count:\t\tPrint the number of ON flags
  • \n
  • val:\t\tPrint the decimal value of the state
  • \t\n
\n\n\n

Input

\n\n

\n The input is given in the following format.\n

\n\n
\n$q$\n$query_1$\n$query_2$\n:\n$query_q$\n
\n\n

\nEach query $query_i$ is given in the following format:\n

\n\n
\n0 $i$\n
\n\n

or

\n\n
\n1 $i$\n
\n\n

or

\n\n
\n2 $i$\n
\n\n

or

\n\n
\n3 $i$\n
\n\n

or

\n\n
\n4\n
\n\n

or

\n\n
\n5\n
\n\n

or

\n\n
\n6\n
\n\n

or

\n\n
\n7\n
\n\n

or

\n\n
\n8\n
\n\n

\n The first digit 0, 1,...,8 represents the operation test(i), set(i), clear(i), flip(i), all, any, none, count or val respectively.\n

\n\n

Output

\n

\n Print the result in a line for each test, all, any, none, count and val operation.\n

\n\n

Constraints

\n
    \n
  • $1 \\leq q \\leq 200,000$
  • \n
  • $0 \\leq i < 64$
  • \n
\n\n

Sample Input 1

\n\n
\n14\n1 0\n1 1\n1 2\n2 1\n0 0\n0 1\n0 2\n0 3\n3 3\n4\n5\n6\n7\n8\n
\n\n

Sample Output 1

\n\n
\n1\n0\n1\n0\n0\n1\n0\n3\n13\n
", "c_code": "int solution(void) {\n unsigned long long int bit = 0;\n int q;\n scanf(\"%d\", &q);\n for (int i = 0; i < q; i++) {\n int command;\n scanf(\"%d\", &command);\n if (0 <= command && command <= 3) {\n int t;\n scanf(\"%d\", &t);\n if (command == 0) {\n if (bit >> t & 1UL) {\n printf(\"1\\n\");\n } else {\n printf(\"0\\n\");\n }\n } else if (command == 1) {\n bit |= 1UL << t;\n } else if (command == 2) {\n bit &= ~(1UL << t);\n } else {\n bit ^= (1UL << t);\n }\n } else {\n if (command == 4) {\n printf(\"%d\\n\", !(bit + 1));\n } else if (command == 5) {\n if (bit == 0) {\n printf(\"0\\n\");\n } else {\n printf(\"1\\n\");\n }\n } else if (command == 6) {\n printf(\"%d\\n\", bit == 0);\n } else if (command == 7) {\n printf(\"%d\\n\", __builtin_popcountll(bit));\n } else {\n printf(\"%llu\\n\", bit);\n }\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\n let mut state = 0u64;\n\n let q = lines.next().unwrap().unwrap().parse().unwrap();\n\n for (_, l) in (0..q).zip(lines) {\n let l = l.unwrap();\n let mut sw = l.split_whitespace();\n let com = sw.next().unwrap().parse().unwrap();\n let i = sw.next();\n match com {\n 0 => {\n let i: u8 = i.unwrap().parse().unwrap();\n println!(\"{}\", if state & (1 << i) != 0 { 1 } else { 0 });\n }\n 1 => {\n let i: u8 = i.unwrap().parse().unwrap();\n state |= 1 << i;\n }\n 2 => {\n let i: u8 = i.unwrap().parse().unwrap();\n state &= !(1 << i);\n }\n 3 => {\n let i: u8 = i.unwrap().parse().unwrap();\n state ^= 1 << i;\n }\n 4 => {\n println!(\"{}\", if state == 0xffffffffffffffff { 1 } else { 0 });\n }\n 5 => {\n println!(\"{}\", if state != 0 { 1 } else { 0 });\n }\n 6 => {\n println!(\"{}\", if state == 0 { 1 } else { 0 });\n }\n 7 => {\n println!(\"{}\", state.count_ones());\n }\n _ => {\n println!(\"{}\", state);\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1652", "problem_description": "Fillomino is a classic logic puzzle. (You do not need to know Fillomino in order to solve this problem.) In one classroom in Yunqi town, some volunteers are playing a board game variant of it:Consider an $$$n$$$ by $$$n$$$ chessboard. Its rows are numbered from $$$1$$$ to $$$n$$$ from the top to the bottom. Its columns are numbered from $$$1$$$ to $$$n$$$ from the left to the right. A cell on an intersection of $$$x$$$-th row and $$$y$$$-th column is denoted $$$(x, y)$$$. The main diagonal of the chessboard is cells $$$(x, x)$$$ for all $$$1 \\le x \\le n$$$.A permutation of $$$\\{1, 2, 3, \\dots, n\\}$$$ is written on the main diagonal of the chessboard. There is exactly one number written on each of the cells. The problem is to partition the cells under and on the main diagonal (there are exactly $$$1+2+ \\ldots +n$$$ such cells) into $$$n$$$ connected regions satisfying the following constraints: Every region should be connected. That means that we can move from any cell of a region to any other cell of the same region visiting only cells of the same region and moving from a cell to an adjacent cell. The $$$x$$$-th region should contain cell on the main diagonal with number $$$x$$$ for all $$$1\\le x\\le n$$$. The number of cells that belong to the $$$x$$$-th region should be equal to $$$x$$$ for all $$$1\\le x\\le n$$$. Each cell under and on the main diagonal should belong to exactly one region.", "c_code": "int solution() {\n unsigned n;\n unsigned a[500];\n unsigned b[500][500];\n scanf(\"%u\", &n);\n\n for (unsigned i = 0; i < n; i++) {\n scanf(\"%u\", &a[i]);\n }\n\n for (unsigned i = 0; i < n; i++) {\n unsigned j = 0;\n for (unsigned k = 0; k < n; k++) {\n if (a[k] >= i + 1) {\n b[i + j][j] = a[k];\n j++;\n }\n }\n }\n\n for (unsigned i = 0; i < n; i++) {\n for (unsigned j = 0; j <= i; j++) {\n printf(\"%u \", b[i][j]);\n }\n putchar('\\n');\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::Read;\n let mut buf = String::new();\n std::io::stdin().read_to_string(&mut buf).unwrap();\n let mut itr = buf.split_whitespace();\n\n use std::io::Write;\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n\n\n\n\n let n = scan!(usize);\n let mut g = vec![vec![0; n + 1]; n + 1];\n for i in 1..=n {\n g[i][i] = scan!(usize);\n\n let mut dfs = Vec::<(usize, usize, usize)>::new();\n dfs.push((i, i, g[i][i]));\n while let Some((x, y, c)) = dfs.pop() {\n if c == 0 {\n continue;\n }\n\n if y > 1 && g[x][y - 1] == 0 {\n g[x][y] = g[i][i];\n dfs.push((x, y - 1, c - 1));\n continue;\n } else {\n assert!(x <= n);\n g[x][y] = g[i][i];\n dfs.push((x + 1, y, c - 1));\n continue;\n }\n }\n }\n\n for i in 1..=n {\n for j in 1..=i {\n print!(\"{} \", g[i][j]);\n }\n print!(\"\\n\");\n }\n}", "difficulty": "hard"} {"problem_id": "1653", "problem_description": "Are you going to Scarborough Fair?Parsley, sage, rosemary and thyme.Remember me to one who lives there.He once was the true love of mine.Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.Willem asks his friend, Grick for directions, Grick helped them, and gave them a task.Although the girl wants to help, Willem insists on doing it by himself.Grick gave Willem a string of length n.Willem needs to do m operations, each operation has four parameters l, r, c1, c2, which means that all symbols c1 in range [l, r] (from l-th to r-th, including l and r) are changed into c2. String is 1-indexed.Grick wants to know the final string after all the m operations.", "c_code": "int solution() {\n short len;\n short n;\n scanf(\"%hi %hi\", &len, &n);\n\n short start[n];\n short end[n];\n char from[n];\n char to[n];\n char str[len];\n\n scanf(\"%s\", str);\n\n for (short i = 0; i < n; ++i) {\n scanf(\"%hi %hi %c %c\", &start[i], &end[i], &from[i], &to[i]);\n }\n\n for (short i = 0; i < n; ++i) {\n for (short j = start[i]; j <= end[i]; ++j) {\n if (str[j - 1] == from[i]) {\n str[j - 1] = to[i];\n }\n }\n }\n\n for (short i = 0; i < len; ++i) {\n printf(\"%c\", str[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"input: read line failed\");\n\n let vec = input\n .split_whitespace()\n .map(|item| item.parse::().unwrap())\n .collect::>();\n\n let (_length, amount) = (vec[0], vec[1]);\n\n input.clear();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"input: read line failed\");\n let mut str = input.clone().into_bytes();\n for _i in 0..amount {\n input.clear();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"input: read line failed\");\n let mut input_splite = input.split_whitespace();\n let (start, end, source, destination) = (\n input_splite.next().unwrap().parse::().unwrap(),\n input_splite.next().unwrap().parse::().unwrap(),\n input_splite.next().unwrap().parse::().unwrap(),\n input_splite.next().unwrap().parse::().unwrap(),\n );\n\n (0..str.len()).for_each(|i| {\n if i < end && i >= start - 1 && str[i] == source.as_bytes()[0] {\n str[i] = destination.as_bytes()[0];\n }\n })\n }\n\n println!(\"{}\", String::from_utf8(str).unwrap());\n}", "difficulty": "medium"} {"problem_id": "1654", "problem_description": "

Problem B: 友だちの誘い方

\n\n

\n明日から、待ちに待った夏休みが始まります。なのでわたしは、友だちを誘って、海に遊びに行くこ\nとに決めました。\n

\n\n

\nけれども、わたしの友だちには、恥ずかしがりやさんが多いです。その人たちは、あまり多くの人が\nいっしょに来ると知ったら、きっと嫌がるでしょう。\n

\n\n

\nほかにも、わたしの友だちには、目立ちたがりやさんも多いです。その人たちは、いっしょに来る人\nがあまり多くないと知れば、きっと嫌がるでしょう。\n

\n\n

\nそれと、わたしの友だちには、いつもは目立ちたがりやなのに実は恥ずかしがりやさんな人もいま\nす。その人たちは、いっしょに来る人が多すぎても少なすぎても、きっと嫌がるでしょう。\n

\n\n

\nこういうのは、大勢で行った方が楽しいはずです。だからわたしは、できるだけたくさんの友だちを\n誘いたいと思っています。けれども、嫌がる友だちを無理やり連れていくのはよくありません。\n

\n

\nいったい、わたしは最大で何人の友だちを誘うことができるのでしょうか。\n

\n\n

\nわたしは、こういう頭を使いそうな問題が大の苦手です。なので、あなたにお願いがあります。もし\nよろしければ、わたしの代わりにこの問題を解いていただけないでしょうか。いえ、決して無理にと\nは言いません。けれど、もし解いていただるのでしたら、わたしはとても嬉しいです。\n

\n\n

Input

\n\n

\nN
\na1 b1
\na2 b2
\n.
\n.
\n.
\naN bN
\n

\n\n

\n入力の1行目には、整数N(1 ≤ N ≤ 100,000)が書かれている。これは、友だちの数をあらわす。\n

\n\n

\n続くN行には、整数 ai と整数 bi(2 ≤ aibi ≤ 100,001)が、空白区切りで書かれている。1+ i 行目に書かれた整数 aibi は、i 番目の友だちは海に行く人数が ai 人以上 bi 人以下でないと嫌がることをあらわす。海に行く人数には「わたし」も含まれることに注意せよ。\n

\n\n

Output

\n\n

\n嫌がる友だちが出ないように、海に誘うことのできる友だちの最大人数を出力せよ。\n

\n\n

Sample Input 1

\n
\n4\n2 5\n4 7\n2 4\n3 6\n
\n

Sample output 1

\n
\n3\n
\n\n

Sample Input 2

\n
\n5\n8 100001\n7 100001\n12 100001\n8 100001\n3 100001\n
\n

Sample output 2

\n
\n0\n
\n\n

Sample Input 3

\n
\n6\n2 9\n4 8\n6 7\n6 6\n5 7\n2 100001\n
\n

Sample output 3

\n
\n5\n
", "c_code": "int solution() {\n\n int a[100002] = {0};\n int b[100002] = {0};\n int N;\n int i;\n int k;\n int answer = 0;\n int p;\n\n scanf(\"%d\", &N);\n for (i = 1; i <= N; i++) {\n scanf(\"%d %d\", &a[i], &b[i]);\n }\n\n for (k = N + 1; k >= 1; k--) {\n p = 0;\n for (i = 1; i <= N; i++) {\n if (a[i] <= k && k <= b[i]) {\n p++;\n }\n\n if (p == k) {\n answer = p - 1;\n break;\n }\n if (p == k - 1) {\n answer = p;\n break;\n }\n }\n if (answer > 0) {\n break;\n }\n }\n\n printf(\"%d\\n\", answer);\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 mut v: Vec = (0..100010).map(|_| 0).collect();\n let n: usize = iter.next().unwrap().parse().unwrap();\n for _i in 0..n {\n let a: usize = iter.next().unwrap().parse().unwrap();\n let b: usize = iter.next().unwrap().parse().unwrap();\n v[a] += 1;\n v[b + 1] -= 1;\n }\n let mut result = 0;\n let mut member = 1;\n for i in 2..n + 2 {\n member += v[i];\n let i = i as i32;\n if i <= member {\n result = i;\n }\n }\n if result > 0 {\n result -= 1;\n }\n println!(\"{}\", result);\n}", "difficulty": "medium"} {"problem_id": "1655", "problem_description": "You are given a binary string $$$s$$$ consisting of $$$n$$$ zeros and ones.Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like \"010101 ...\" or \"101010 ...\" (i.e. the subsequence should not contain two adjacent zeros or ones).Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of \"1011101\" are \"0\", \"1\", \"11111\", \"0111\", \"101\", \"1001\", but not \"000\", \"101010\" and \"11100\".You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int tc;\n scanf(\"%d\", &tc);\n while (tc--) {\n int n;\n int i;\n int z = -1;\n int o = -1;\n int s = 0;\n int max = -1;\n scanf(\"%d\", &n);\n char ss[n + 5];\n char at[2];\n\n scanf(\"%s\", ss);\n int a[n];\n int b[n];\n int zero[n];\n int one[n];\n for (i = 0; i < n; i++) {\n at[0] = ss[i];\n at[1] = '\\0';\n a[i] = atoi(at);\n }\n for (i = 0; i < n; i++) {\n if (!a[i]) {\n if (o >= 0) {\n zero[++z] = one[o--];\n b[i] = zero[z];\n } else {\n zero[++z] = ++s;\n b[i] = zero[z];\n }\n }\n if (a[i]) {\n if (z >= 0) {\n one[++o] = zero[z--];\n b[i] = one[o];\n } else {\n one[++o] = ++s;\n b[i] = one[o];\n }\n }\n if (max < b[i]) {\n max = b[i];\n }\n }\n printf(\"%d\\n\", max);\n for (i = 0; i < n; i++) {\n printf(\"%d \", b[i]);\n }\n printf(\"\\n\");\n }\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 s: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().chars().collect()\n };\n\n let mut a = Vec::with_capacity(n);\n let mut zeros: VecDeque = VecDeque::new();\n let mut ones: VecDeque = VecDeque::new();\n for i in 0..n {\n if s[i] == '0' {\n if let Some(x) = ones.pop_front() {\n a.push(x.to_string());\n zeros.push_back(x);\n } else {\n let x = zeros.len() + 1;\n a.push(x.to_string());\n zeros.push_back(x);\n }\n } else {\n if let Some(x) = zeros.pop_front() {\n a.push(x.to_string());\n ones.push_back(x);\n } else {\n let x = ones.len() + 1;\n a.push(x.to_string());\n ones.push_back(x);\n }\n }\n }\n\n println!(\"{}\", zeros.len() + ones.len());\n println!(\"{}\", a.join(\" \"));\n }\n}", "difficulty": "hard"} {"problem_id": "1656", "problem_description": "A pair of positive integers $$$(a,b)$$$ is called special if $$$\\lfloor \\frac{a}{b} \\rfloor = a \\bmod b$$$. Here, $$$\\lfloor \\frac{a}{b} \\rfloor$$$ is the result of the integer division between $$$a$$$ and $$$b$$$, while $$$a \\bmod b$$$ is its remainder.You are given two integers $$$x$$$ and $$$y$$$. Find the number of special pairs $$$(a,b)$$$ such that $$$1\\leq a \\leq x$$$ and $$$1 \\leq b \\leq y$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int x;\n int y;\n long long int Answer = 0;\n scanf(\"%d%d\", &x, &y);\n for (int k = 1; k < pow(x, 0.5); k++) {\n int temp = (x / k) - 1;\n int min1;\n int max1;\n if (y < temp) {\n min1 = y;\n } else {\n min1 = temp;\n }\n\n min1 = min1 - k;\n if (min1 > 0) {\n Answer += min1;\n }\n }\n\n printf(\"%lld\\n\", Answer);\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 (x, y): (i64, i64) = (sc.next(), sc.next());\n let mut i = 1;\n let mut ans = 0;\n while i * i <= x && i < y {\n ans += max(min((x - i) / i, y) - i, 0);\n i += 1;\n }\n writeln!(out, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1657", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.

\n

The square (i, j) has two numbers A_{ij} and B_{ij} written on it.

\n

First, for each square, Takahashi paints one of the written numbers red and the other blue.

\n

Then, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.

\n

Let the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).

\n

Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.

\n

Find the minimum unbalancedness possible.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq H \\leq 80
  • \n
  • 2 \\leq W \\leq 80
  • \n
  • 0 \\leq A_{ij} \\leq 80
  • \n
  • 0 \\leq B_{ij} \\leq 80
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\nA_{11} A_{12} \\ldots A_{1W}\n:\nA_{H1} A_{H2} \\ldots A_{HW}\nB_{11} B_{12} \\ldots B_{1W}\n:\nB_{H1} B_{H2} \\ldots B_{HW}\n
\n
\n
\n
\n
\n

Output

Print the minimum unbalancedness possible.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n1 2\n3 4\n3 4\n2 1\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n

By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0.

\n

\"Figure\"

\n
\n
\n
\n
\n
\n

Sample Input 2

2 3\n1 10 80\n80 10 1\n1 2 3\n4 5 6\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
", "c_code": "int solution(void) {\n\n int h;\n int w;\n scanf(\"%d %d\", &h, &w);\n long a[h][w];\n long b[h][w];\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n scanf(\"%ld\", &a[i][j]);\n }\n }\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n scanf(\"%ld\", &b[i][j]);\n }\n }\n long ab[h][w];\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n ab[i][j] = labs(a[i][j] - b[i][j]);\n }\n }\n long limit = (h + w) * 80;\n long ***dp;\n dp = (long ***)malloc(sizeof(long **) * 80);\n for (long i = 0; i < 80; i++) {\n dp[i] = (long **)malloc(sizeof(long *) * 80);\n for (long j = 0; j < 80; j++) {\n dp[i][j] = (long *)malloc(sizeof(long) * (limit + 1));\n for (long k = 0; k <= limit; k++) {\n dp[i][j][k] = 0;\n }\n }\n }\n long value = ab[0][0];\n dp[0][0][value] = 1;\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n if (i != 0) {\n for (long k = 0; k <= limit; k++) {\n if (dp[i - 1][j][k] == 1) {\n value = labs(k + ab[i][j]);\n dp[i][j][value] = 1;\n value = labs(k - ab[i][j]);\n dp[i][j][value] = 1;\n }\n }\n }\n if (j != 0) {\n for (long k = 0; k <= limit; k++) {\n if (dp[i][j - 1][k] == 1) {\n value = labs(k + ab[i][j]);\n dp[i][j][value] = 1;\n value = labs(k - ab[i][j]);\n dp[i][j][value] = 1;\n }\n }\n }\n }\n }\n for (long i = 0; i <= limit; i++) {\n if (dp[h - 1][w - 1][i] == 1) {\n printf(\"%ld\\n\", i);\n break;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n std::io::stdin().read_to_string(&mut input_str).unwrap();\n\n let input_parts = input_str.split_whitespace().collect::>();\n let mut input_parts_it = input_parts.iter().cloned();\n let mut next = || input_parts_it.next().unwrap();\n\n let h: usize = next().parse().unwrap();\n let w: usize = next().parse().unwrap();\n let a_map: Vec = (0..(w * h)).map(|_| next().parse().unwrap()).collect();\n let b_map: Vec = (0..(w * h)).map(|_| next().parse().unwrap()).collect();\n\n let idx = |x: usize, y: usize| x + y * w;\n\n let mut costs: Vec> = vec![Vec::new(); a_map.len()];\n\n let mut buf = Vec::new();\n\n for y in 0..h {\n for x in 0..w {\n buf.clear();\n\n if x == 0 && y == 0 {\n buf.push(0);\n }\n if x > 0 {\n buf.extend(costs[idx(x - 1, y)].iter().cloned());\n }\n if y > 0 {\n buf.extend(costs[idx(x, y - 1)].iter().cloned());\n }\n\n let (a, b) = (a_map[idx(x, y)], b_map[idx(x, y)]);\n\n let out_cost = &mut costs[idx(x, y)];\n out_cost.reserve(buf.len() * 2);\n out_cost.extend(buf.iter().map(|&x| x + (a - b)));\n out_cost.extend(buf.iter().map(|&x| x + (b - a)));\n out_cost.sort();\n out_cost.dedup();\n }\n }\n\n let goal = costs.last().unwrap();\n\n let best = goal.iter().map(|&cost| cost.abs()).min().unwrap();\n\n println!(\"{}\", best);\n}", "difficulty": "medium"} {"problem_id": "1658", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

You are given N non-negative integers A_1, A_2, ..., A_N and another non-negative integer K.

\n

For a integer X between 0 and K (inclusive), let f(X) = (X XOR A_1) + (X XOR A_2) + ... + (X XOR A_N).

\n

Here, for non-negative integers a and b, a XOR b denotes the bitwise exclusive OR of a and b.

\n

Find the maximum value of f.

\n

\nWhat is XOR?

\n

The bitwise exclusive OR of a and b, X, is defined as follows:

\n
    \n
  • When X is written in base two, the digit in the 2^k's place (k \\geq 0) is 1 if, when written in base two, exactly one of A and B has 1 in the 2^k's place, and 0 otherwise.
  • \n
\n

For example, 3 XOR 5 = 6. (When written in base two: 011 XOR 101 = 110.)

\n

", "c_code": "int solution(void) {\n int N = 0;\n long K = 0;\n int bin[50] = {0};\n long long sum = 0;\n long X = 0;\n\n scanf(\"%d %ld\", &N, &K);\n\n long A[N];\n\n for (int i = 0; i < N; i++) {\n A[i] = 0;\n scanf(\"%ld\", &A[i]);\n sum += A[i];\n for (int j = 0; j < 50; j++) {\n if (A[i] % 2) {\n bin[j]++;\n }\n A[i] /= (long)2;\n }\n }\n\n for (int i = 49; i >= 0; i--) {\n if (bin[i] < (N + 1) / 2) {\n if (X + (long)(pow(2, i)) <= K) {\n X += (long)pow(2, i);\n sum += (long long)pow(2, i) * (long long)(N - (bin[i] * 2));\n }\n }\n }\n\n printf(\"%lld\", sum);\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 let mut iter = buf.split_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let k: u64 = iter.next().unwrap().parse().unwrap();\n let a: Vec = (0..n)\n .map(|_| iter.next().unwrap().parse().unwrap())\n .collect();\n let mut ans: u64 = 0;\n let mut x: u64 = 0;\n\n for i in (0..61).rev() {\n let bit = 1u64 << i;\n let mut bit0 = 0;\n let mut bit1 = 0;\n for y in a.iter() {\n if (y & bit) != 0 {\n bit1 += 1;\n } else {\n bit0 += 1;\n }\n }\n if x + bit <= k && bit1 < bit0 {\n x += bit;\n ans += bit * bit0;\n } else {\n ans += bit * bit1;\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1659", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There is a directed graph with N vertices numbered 1 to N and M edges.\nThe i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins \bplaced along that edge.\nAdditionally, there is a button on Vertex N.

\n

We will play a game on this graph.\nYou start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins.\nIt takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it.\nAs usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.

\n

When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.)\nHowever, when you end the game, you will be asked to pay T \\times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \\times P coins, you will have to pay all of your coins instead.

\n

Your score will be the number of coins you have after this payment.\nDetermine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 2500
  • \n
  • 1 \\leq M \\leq 5000
  • \n
  • 1 \\leq A_i, B_i \\leq N
  • \n
  • 1 \\leq C_i \\leq 10^5
  • \n
  • 0 \\leq P \\leq 10^5
  • \n
  • All values in input are integers.
  • \n
  • Vertex N can be reached from Vertex 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M P\nA_1 B_1 C_1\n:\nA_M B_M C_M\n
\n
\n
\n
\n
\n

Output

If there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print -1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3 10\n1 2 20\n2 3 30\n1 3 45\n
\n
\n
\n
\n
\n

Sample Output 1

35\n
\n

\"Figure

\n

There are two ways to travel from Vertex 1 to Vertex 3:

\n
    \n
  • Vertex 1 \\rightarrow 2 \\rightarrow 3: You collect 20 + 30 = 50 coins on the way. After two minutes from the start of the game, you press the button, pay 2 \\times 10 = 20 coins, and you have 50 - 20 = 30 coins left.
  • \n
  • Vertex 1 \\rightarrow 2: You collect 45 coins on the way. After one minute from the start of the game, you press the button, pay 1 \\times 10 = 10 coins, and you have 45 - 10 = 35 coins left.
  • \n
\n

Thus, the maximum score that can be obtained is 35.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2 10\n1 2 100\n2 2 100\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

\"Figure

\n

The edge extending from Vertex 1 takes you to Vertex 2. If you then traverse the edge extending from Vertex 2 to itself t times and press the button, your score will be 90 + 90t. Thus, you can infinitely increase your score, which means there is no maximum value of the score that can be obtained.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 5 10\n1 2 1\n1 4 1\n3 4 1\n2 2 100\n3 3 100\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

\"Figure

\n

There is no way to travel from Vertex 1 to Vertex 4 other than traversing the edge leading from Vertex 1 to Vertex 4 directly. You will pick up one coin along this edge, but after being asked to paying 10 coins, your score will be 0.

\n

Note that you can collect an infinite number of coins if you traverse the edge leading from Vertex 1 to Vertex 2, but this is pointless since you can no longer reach Vertex 4 and end the game.

\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n long long int p;\n scanf(\"%d %d %lld\", &n, &m, &p);\n int a[5003];\n int b[5003];\n long long int c[5003];\n int i;\n int j;\n for (i = 0; i < m; i++) {\n scanf(\"%d %d %lld\", &a[i], &b[i], &c[i]);\n }\n for (i = 0; i < m; i++) {\n c[i] -= p;\n }\n long long int d[3003];\n for (i = 0; i < n; i++) {\n d[i] = -10000000000;\n }\n d[0] = 0;\n int u[3003];\n for (i = 0; i < n; i++) {\n u[i] = 0;\n }\n u[0] = 1;\n for (i = 0; i < m; i++) {\n a[i]--;\n b[i]--;\n }\n for (j = 0; j < n; j++) {\n for (i = 0; i < m; i++) {\n if (u[a[i]] > 0) {\n if (d[b[i]] < d[a[i]] + c[i]) {\n d[b[i]] = d[a[i]] + c[i];\n }\n u[b[i]] = 1;\n }\n }\n }\n int e[3003];\n for (i = 0; i < n; i++) {\n e[i] = 0;\n }\n for (j = 0; j < n; j++) {\n for (i = 0; i < m; i++) {\n if (u[a[i]] > 0) {\n if (e[a[i]] > 0) {\n e[b[i]] = 1;\n }\n if (d[b[i]] < d[a[i]] + c[i]) {\n e[b[i]] = 1;\n }\n }\n }\n }\n if (e[n - 1] > 0) {\n printf(\"-1\\n\");\n return 0;\n }\n if (d[n - 1] < 0) {\n d[n - 1] = 0;\n }\n printf(\"%lld\\n\", d[n - 1]);\n return 0;\n}", "rust_code": "fn solution() {\n let out = std::io::stdout();\n let mut out = std::io::BufWriter::new(out.lock());\n\n input! {\n n: usize,\n m: usize,\n p: i64,\n es: [(usize1, usize1, i64); m],\n }\n let es: Vec<_> = es.into_iter().map(|(a, b, c)| (a, b, c - p)).collect();\n let mut score = vec![-(1i64 << 60); n];\n score[0] = 0;\n let mut reach_start = vec![false; n];\n reach_start[0] = true;\n let mut reach_end = vec![false; n];\n reach_end[n - 1] = true;\n for _ in 0..n - 1 {\n for &(a, b, c) in &es {\n score[b] = cmp::max(score[b], score[a] + c);\n reach_start[b] = reach_start[b] || reach_start[a];\n reach_end[a] = reach_end[a] || reach_end[b];\n }\n }\n let mut update = false;\n for &(a, b, c) in &es {\n if reach_start[b] && reach_end[b] && score[a] + c > score[b] {\n update = true;\n break;\n }\n }\n if update {\n puts!(\"-1\\n\");\n } else {\n puts!(\"{}\\n\", cmp::max(score[n - 1], 0));\n }\n}", "difficulty": "easy"} {"problem_id": "1660", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You will be given an integer a and a string s consisting of lowercase English letters as input.

\n

Write a program that prints s if a is not less than 3200 and prints red if a is less than 3200.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2800 \\leq a < 5000
  • \n
  • s is a string of length between 1 and 10 (inclusive).
  • \n
  • Each character of s is a lowercase English letter.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
a\ns\n
\n
\n
\n
\n
\n

Output

If a is not less than 3200, print s; if a is less than 3200, print red.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3200\npink\n
\n
\n
\n
\n
\n

Sample Output 1

pink\n
\n

a = 3200 is not less than 3200, so we print s = pink.

\n
\n
\n
\n
\n
\n

Sample Input 2

3199\npink\n
\n
\n
\n
\n
\n

Sample Output 2

red\n
\n

a = 3199 is less than 3200, so we print red.

\n
\n
\n
\n
\n
\n

Sample Input 3

4049\nred\n
\n
\n
\n
\n
\n

Sample Output 3

red\n
\n

a = 4049 is not less than 3200, so we print s = red.

\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n char s[20];\n char *ss = \"red\";\n scanf(\"%d\", &a);\n scanf(\"%s\", s);\n if (a >= 3200) {\n printf(\"%s\\n\", s);\n } else {\n printf(\"%s\\n\", ss);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut a = String::new();\n let mut s = String::new();\n std::io::stdin().read_line(&mut a).unwrap();\n std::io::stdin().read_line(&mut s).unwrap();\n let a: i32 = a.trim().parse().unwrap();\n println!(\"{}\", if a >= 3200 { s.trim() } else { \"red\" });\n}", "difficulty": "easy"} {"problem_id": "1661", "problem_description": "You are given a rooted tree consisting of $$$n$$$ vertices. Vertices are numbered from $$$1$$$ to $$$n$$$. Any vertex can be the root of a tree.A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root.The tree is specified by an array of ancestors $$$b$$$ containing $$$n$$$ numbers: $$$b_i$$$ is an ancestor of the vertex with the number $$$i$$$. The ancestor of a vertex $$$u$$$ is a vertex that is the next vertex on a simple path from $$$u$$$ to the root. For example, on the simple path from $$$5$$$ to $$$3$$$ (the root), the next vertex would be $$$1$$$, so the ancestor of $$$5$$$ is $$$1$$$.The root has no ancestor, so for it, the value of $$$b_i$$$ is $$$i$$$ (the root is the only vertex for which $$$b_i=i$$$).For example, if $$$n=5$$$ and $$$b=[3, 1, 3, 3, 1]$$$, then the tree looks like this. An example of a rooted tree for $$$n=5$$$, the root of the tree is a vertex number $$$3$$$. You are given an array $$$p$$$ — a permutation of the vertices of the tree. If it is possible, assign any positive integer weights on the edges, so that the vertices sorted by distance from the root would form the given permutation $$$p$$$.In other words, for a given permutation of vertices $$$p$$$, it is necessary to choose such edge weights so that the condition $$$dist[p_i]<dist[p_{i+1}]$$$ is true for each $$$i$$$ from $$$1$$$ to $$$n-1$$$. $$$dist[u]$$$ is a sum of the weights of the edges on the path from the root to $$$u$$$. In particular, $$$dist[u]=0$$$ if the vertex $$$u$$$ is the root of the tree.For example, assume that $$$p=[3, 1, 2, 5, 4]$$$. In this case, the following edge weights satisfy this permutation: the edge ($$$3, 4$$$) has a weight of $$$102$$$; the edge ($$$3, 1$$$) has weight of $$$1$$$; the edge ($$$1, 2$$$) has a weight of $$$10$$$; the edge ($$$1, 5$$$) has a weight of $$$100$$$. The array of distances from the root looks like: $$$dist=[1,11,0,102,101]$$$. The vertices sorted by increasing the distance from the root form the given permutation $$$p$$$.Print the required edge weights or determine that there is no suitable way to assign weights. If there are several solutions, then print any of them.", "c_code": "int solution() {\n int t;\n int i;\n scanf(\"%d\", &t);\n for (i = 0; i < t; i++) {\n int n;\n int j;\n scanf(\"%d\", &n);\n\n int parent[n + 1];\n int order[n + 1];\n int weight[n + 1];\n int tot_weight[n + 1];\n order[0] = -100;\n parent[0] = -100;\n weight[0] = -100;\n for (j = 1; j <= n; j++) {\n scanf(\"%d\", &parent[j]);\n }\n for (j = 1; j <= n; j++) {\n scanf(\"%d\", &order[j]);\n }\n for (j = 1; j <= n; j++) {\n weight[j] = -1;\n tot_weight[j] = -1;\n }\n int root = order[1];\n\n weight[root] = 0;\n tot_weight[root] = 0;\n int curr_weight = 0;\n int prev_weight = 0;\n int k;\n int ans = 0;\n for (j = 2; j <= n; j++) {\n prev_weight = curr_weight;\n curr_weight = 0;\n k = parent[order[j]];\n\n if (weight[k] == -1) {\n ans = -1;\n break;\n }\n curr_weight = tot_weight[k];\n\n if (curr_weight >= prev_weight) {\n weight[order[j]] = 1;\n curr_weight++;\n tot_weight[order[j]] = curr_weight;\n } else {\n weight[order[j]] = prev_weight - curr_weight + 1;\n curr_weight = prev_weight + 1;\n tot_weight[order[j]] = curr_weight;\n }\n }\n if (ans == 0) {\n for (j = 1; j <= n; j++) {\n printf(\"%d \", weight[j]);\n }\n } else {\n printf(\"-1\");\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 b: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse::().unwrap() - 1)\n .collect();\n let p: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse::().unwrap() - 1)\n .collect();\n let root = b.iter().enumerate().find(|&(i, &x)| i == x).unwrap().0;\n let mut d: Vec> = vec![None; n];\n if root != p[0] {\n println!(\"-1\");\n continue;\n }\n d[root] = Some(0);\n let mut is_valid = true;\n for i in 1..n {\n let u = p[i];\n let par = b[u];\n if d[par].is_some() {\n d[u] = Some(i);\n } else {\n is_valid = false;\n break;\n }\n }\n if is_valid {\n for i in 0..n {\n let w = d[i].unwrap() - d[b[i]].unwrap();\n print!(\"{} \", w);\n }\n println!();\n } else {\n println!(\"-1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1662", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

\n

Niwango created a playlist of N songs.\nThe title and the duration of the i-th song are s_i and t_i seconds, respectively.\nIt is guaranteed that s_1,\\ldots,s_N are all distinct.

\n

Niwango was doing some work while playing this playlist. (That is, all the songs were played once, in the order they appear in the playlist, without any pause in between.)\nHowever, he fell asleep during his work, and he woke up after all the songs were played.\nAccording to his record, it turned out that he fell asleep at the very end of the song titled X.

\n

Find the duration of time when some song was played while Niwango was asleep.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 50
  • \n
  • s_i and X are strings of length between 1 and 100 (inclusive) consisting of lowercase English letters.
  • \n
  • s_1,\\ldots,s_N are distinct.
  • \n
  • There exists an integer i such that s_i = X.
  • \n
  • 1 \\leq t_i \\leq 1000
  • \n
  • t_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\ns_1 t_1\n\\vdots\ns_{N} t_N\nX\n
\n
\n
\n
\n
\n

Output

\n

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\ndwango 2\nsixth 5\nprelims 25\ndwango\n
\n
\n
\n
\n
\n

Sample Output 1

30\n
\n
    \n
  • While Niwango was asleep, two songs were played: sixth and prelims.
  • \n
  • The answer is the total duration of these songs, 30.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

1\nabcde 1000\nabcde\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
    \n
  • No songs were played while Niwango was asleep.
  • \n
  • In such a case, the total duration of songs is 0.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 3

15\nypnxn 279\nkgjgwx 464\nqquhuwq 327\nrxing 549\npmuduhznoaqu 832\ndagktgdarveusju 595\nwunfagppcoi 200\ndhavrncwfw 720\njpcmigg 658\nwrczqxycivdqn 639\nmcmkkbnjfeod 992\nhtqvkgkbhtytsz 130\ntwflegsjz 467\ndswxxrxuzzfhkp 989\nszfwtzfpnscgue 958\npmuduhznoaqu\n
\n
\n
\n
\n
\n

Sample Output 3

6348\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\\n\", &n);\n\n char s[n][100];\n int t[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%s %d\\n\", s[i], &t[i]);\n }\n\n char x[100];\n scanf(\"%s\\n\", x);\n\n int flag = 0;\n int sum = 0;\n for (int i = 0; i < n; i++) {\n if (flag == 1) {\n sum += t[i];\n }\n if (strcmp(x, s[i]) == 0) {\n flag = 1;\n }\n }\n\n printf(\"%d\\n\", sum);\n\n return (0);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let st: Vec<_> = (0..n)\n .map(|_| (s.next().unwrap(), s.next().unwrap().parse::().unwrap()))\n .collect();\n let x = s.next().unwrap();\n let ans = st\n .iter()\n .fold((0, false), |r, &(s, t)| {\n if r.1 {\n (r.0 + t, true)\n } else {\n (r.0, s == x)\n }\n })\n .0;\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1663", "problem_description": "\n\n\n\n\n

A: 丸付け

\n\n

問題

\n

\nAORイカちゃんはテストに合格するため勉強しています。\n

\n

\nAORイカちゃんは、 $N$ 問、問題を解きました。\nその後、解いた問題の丸付けを以下の手順で行います。\n

\n\n
    \n
  1. 解答の正誤を確認する。
  2. \n
  3. 解答が正しい場合はマル印、誤っていた場合はバツ印を解答用紙に書き込む。
  4. \n
\n\n

\n解答が $2$ 問連続で誤りであるとわかった瞬間、テストに不合格になってしまう恐怖から、AORイカちゃんは失神してしまいます。そして、それ以降丸付けを行うことはできません。\n

\n

\n失神は手順 $1$ と $2$ の間で起こります。\n

\n\n

\nAORイカちゃんが解いた問題の数を表す整数 $N$ と、解答の正誤を表した長さ $N$ の文字列 $S$ が与えられます。\n文字列は 'o' と 'x' からなり、 'o' は解答が正しく、 'x' は解答が誤りであることを表しています。\n $i$ 文字目が $i$ 問目の正誤を表しており、AORイカちゃんは $1$ 問目から順番に丸付けを行います。\n

\n\n

\nAORイカちゃんが正誤を書き込めた問題数を出力してください。\n

\n\n

制約

\n
    \n
  • $1 \\leq N \\leq 10^5$
  • \n
\n\n

入力形式

\n

\n入力は以下の形式で与えられる。\n

\n

\n$N$
\n$S$
\n

\n\n

出力

\n

\nAORイカちゃんが正誤を書き込めた問題数を $1$ 行で出力せよ。また、末尾に改行も出力せよ。\n

\n\n

サンプル

\n\n

サンプル入力 1

\n
\n3\noxx\n
\n

サンプル出力 1

\n
\n2\n
\n\n

\n $3$ 問目の手順 $1$ を行うと失神するため、手順 $2$ は行えません。\n

\n\n

サンプル入力 2

\n
\n8\noxoxoxox\n
\n

サンプル出力 2

\n
\n8\n
\n\n

サンプル入力 3

\n
\n4\nxxxx\n
\n

サンプル出力 3

\n
\n1\n
\n\n\n", "c_code": "int solution() {\n char mozi[100000];\n int i;\n int n;\n int count = 0;\n\n scanf(\"%d\", &n);\n scanf(\"%s\", mozi);\n\n for (i = 0; i < n; i++) {\n count++;\n if (mozi[i] == 'x' && mozi[i + 1] == 'x') {\n printf(\"%d\\n\", count);\n return 0;\n }\n }\n printf(\"%d\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n input!(N:usize,S:chars);\n let mut S: Vec = S;\n S.push('x');\n S.push('x');\n let mut ans = 0;\n let mut flag = false;\n for i in 0..N {\n if S[i] == 'x' {\n if flag {\n break;\n }\n flag = true;\n } else {\n flag = false;\n }\n ans += 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1664", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

\n

Starting with this string, he will produce a new one in the procedure given as follows.

\n

The procedure consists of Q operations. In Operation i (1 \\leq i \\leq Q), an integer T_i is provided, which means the following:

\n
    \n
  • \n

    If T_i = 1: reverse the string S.

    \n
  • \n
  • \n

    If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

    \n
      \n
    • If F_i = 1 : Add C_i to the beginning of the string S.
    • \n
    • If F_i = 2 : Add C_i to the end of the string S.
    • \n
    \n
  • \n
\n

Help Takahashi by finding the final string that results from the procedure.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |S| \\leq 10^5
  • \n
  • S consists of lowercase English letters.
  • \n
  • 1 \\leq Q \\leq 2 \\times 10^5
  • \n
  • T_i = 1 or 2.
  • \n
  • F_i = 1 or 2, if provided.
  • \n
  • C_i is a lowercase English letter, if provided.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\nQ\nQuery_1\n:\nQuery_Q\n
\n

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

\n
1\n
\n

which means T_i = 1, and:

\n
2 F_i C_i\n
\n

which means T_i = 2.

\n
\n
\n
\n
\n

Output

Print the resulting string.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

a\n4\n2 1 p\n1\n2 2 c\n1\n
\n
\n
\n
\n
\n

Sample Output 1

cpa\n
\n

There will be Q = 4 operations. Initially, S is a.

\n
    \n
  • \n

    Operation 1: Add p at the beginning of S. S becomes pa.

    \n
  • \n
  • \n

    Operation 2: Reverse S. S becomes ap.

    \n
  • \n
  • \n

    Operation 3: Add c at the end of S. S becomes apc.

    \n
  • \n
  • \n

    Operation 4: Reverse S. S becomes cpa.

    \n
  • \n
\n

Thus, the resulting string is cpa.

\n
\n
\n
\n
\n
\n

Sample Input 2

a\n6\n2 2 a\n2 1 b\n1\n2 2 c\n1\n1\n
\n
\n
\n
\n
\n

Sample Output 2

aabc\n
\n

There will be Q = 6 operations. Initially, S is a.

\n
    \n
  • \n

    Operation 1: S becomes aa.

    \n
  • \n
  • \n

    Operation 2: S becomes baa.

    \n
  • \n
  • \n

    Operation 3: S becomes aab.

    \n
  • \n
  • \n

    Operation 4: S becomes aabc.

    \n
  • \n
  • \n

    Operation 5: S becomes cbaa.

    \n
  • \n
  • \n

    Operation 6: S becomes aabc.

    \n
  • \n
\n

Thus, the resulting string is aabc.

\n
\n
\n
\n
\n
\n

Sample Input 3

y\n1\n2 1 x\n
\n
\n
\n
\n
\n

Sample Output 3

xy\n
\n
\n
", "c_code": "int solution(void) {\n char s[500001];\n char *p1 = &s[200000];\n scanf(\"%s\", p1);\n char *p2 = p1 + strlen(p1);\n int q;\n scanf(\"%d\", &q);\n int flag = 1;\n for (int i = 0; i < q; i++) {\n int n;\n int f;\n char c;\n scanf(\"%d\", &n);\n if (n == 1) {\n flag = 1 - flag;\n continue;\n }\n scanf(\"%d %c\", &f, &c);\n if (flag ^ --f) {\n *--p1 = c;\n } else {\n *p2++ = c;\n }\n }\n *p2 = '\\0';\n if (flag) {\n puts(p1);\n } else {\n while (--p2 >= p1) {\n putchar(*p2);\n }\n putchar('\\n');\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let mut s: VecDeque = s.trim_end().chars().collect();\n\n let mut q = String::new();\n std::io::stdin().read_line(&mut q).unwrap();\n let q: u64 = q.trim_end().parse().unwrap();\n\n let mut reversed = false;\n\n for _ in 0..q {\n let mut query = String::new();\n std::io::stdin().read_line(&mut query).unwrap();\n let query: Vec<&str> = query.split_whitespace().collect();\n\n if query[0] == \"1\" {\n reversed = !reversed;\n }\n if query[0] == \"2\" {\n let push_front = query[1] == \"1\";\n if reversed ^ push_front {\n s.push_front(query[2].chars().next().unwrap());\n } else {\n s.push_back(query[2].chars().next().unwrap());\n }\n }\n }\n\n let mut res = String::new();\n if reversed {\n res.extend(s.iter().rev());\n } else {\n res.extend(s.iter());\n };\n\n println!(\"{}\", res);\n}", "difficulty": "hard"} {"problem_id": "1665", "problem_description": "Suppose there is a $$$h \\times w$$$ grid consisting of empty or full cells. Let's make some definitions: $$$r_{i}$$$ is the number of consecutive full cells connected to the left side in the $$$i$$$-th row ($$$1 \\le i \\le h$$$). In particular, $$$r_i=0$$$ if the leftmost cell of the $$$i$$$-th row is empty. $$$c_{j}$$$ is the number of consecutive full cells connected to the top end in the $$$j$$$-th column ($$$1 \\le j \\le w$$$). In particular, $$$c_j=0$$$ if the topmost cell of the $$$j$$$-th column is empty. In other words, the $$$i$$$-th row starts exactly with $$$r_i$$$ full cells. Similarly, the $$$j$$$-th column starts exactly with $$$c_j$$$ full cells. These are the $$$r$$$ and $$$c$$$ values of some $$$3 \\times 4$$$ grid. Black cells are full and white cells are empty. You have values of $$$r$$$ and $$$c$$$. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of $$$r$$$ and $$$c$$$. Since the answer can be very large, find the answer modulo $$$1000000007\\,(10^{9} + 7)$$$. In other words, find the remainder after division of the answer by $$$1000000007\\,(10^{9} + 7)$$$.", "c_code": "int solution() {\n int h;\n int w;\n scanf(\"%d %d\", &h, &w);\n int matrix[h][w];\n\n memset(matrix, 0, sizeof(matrix));\n\n for (int i = 0; i < h; ++i) {\n int r = 0;\n scanf(\"%d\", &r);\n\n for (int j = 0; j < r; ++j) {\n matrix[i][j] += 1;\n }\n if (r < w) {\n matrix[i][r] += 4;\n }\n }\n\n for (int i = 0; i < w; ++i) {\n int c = 0;\n scanf(\"%d\", &c);\n\n for (int j = 0; j < c; ++j) {\n matrix[j][i] += 1;\n }\n if (c < h) {\n matrix[c][i] += 4;\n }\n }\n int n = 0;\n int ans = 1;\n int l = 1000000007;\n int flag = 0;\n\n for (int k = 0; k < h; ++k) {\n for (int i = 0; i < w; ++i) {\n if (!matrix[k][i]) {\n n++;\n ans = ans * 2 % l;\n }\n if (matrix[k][i] == 5) {\n\n flag = -1;\n break;\n }\n }\n }\n\n if (flag != -1) {\n printf(\"%d\", ans);\n } else {\n printf(\"%d\", 0);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_to_string(&mut buf).unwrap();\n let mut tok = buf.split_whitespace();\n let mut get = || tok.next().unwrap();\n\n\n let h = get!(usize);\n let w = get!(usize);\n let mut grid = vec![vec![0; w]; h];\n for r in 0..h {\n let c = get!(usize);\n for i in 0..c {\n grid[r][i] |= 1;\n }\n if c < w {\n grid[r][c] |= 2;\n }\n }\n for c in 0..w {\n let r = get!(usize);\n for i in 0..r {\n grid[i][c] |= 1;\n }\n if r < h {\n grid[r][c] |= 2;\n }\n }\n let mut ans = 1_i64;\n for r in 0..h {\n for c in 0..w {\n match grid[r][c] {\n 0 => ans = ans * 2 % 1_000_000_007,\n 3 => ans = 0,\n _ => {}\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1666", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given is a three-digit integer N. Does N contain the digit 7?

\n

If so, print Yes; otherwise, print No.

\n
\n
\n
\n
\n

Constraints

    \n
  • 100 \\leq N \\leq 999
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If N contains the digit 7, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

117\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

117 contains 7 as its last digit.

\n
\n
\n
\n
\n
\n

Sample Input 2

123\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

123 does not contain the digit 7.

\n
\n
\n
\n
\n
\n

Sample Input 3

777\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution() {\n char s[3];\n scanf(\"%s\", s);\n if (s[0] != '7' && s[1] != '7' && s[2] != '7') {\n printf(\"No\");\n } else {\n printf(\"Yes\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let mut itr = s.trim().chars();\n println!(\"{}\", if itr.any(|e| e == '7') { \"Yes\" } else { \"No\" });\n}", "difficulty": "easy"} {"problem_id": "1667", "problem_description": "You are given two positive integers $$$n$$$ and $$$k$$$. Print the $$$k$$$-th positive integer that is not divisible by $$$n$$$.For example, if $$$n=3$$$, and $$$k=7$$$, then all numbers that are not divisible by $$$3$$$ are: $$$1, 2, 4, 5, 7, 8, 10, 11, 13 \\dots$$$. The $$$7$$$-th number among them is $$$10$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\\n\", &t);\n long long int n[t];\n long long int k[t];\n for (int i = 0; i < t; i++) {\n scanf(\"%lld %lld\\n\", &n[i], &k[i]);\n }\n for (int i = 0; i < t; i++) {\n long long int q;\n long long int r;\n q = k[i] / (n[i] - 1);\n r = k[i] % (n[i] - 1);\n if (r == 0) {\n printf(\"%lld\\n\", (q * n[i]) - 1);\n } else {\n printf(\"%lld\\n\", (q * n[i]) + r);\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, k) = (xs[0], xs[1] - 1);\n let q = k / (n - 1);\n let m = k % (n - 1);\n println!(\"{}\", q * n + m + 1);\n }\n}", "difficulty": "medium"} {"problem_id": "1668", "problem_description": "\n

Score : 900 points

\n
\n
\n

Problem Statement

You are given an undirected graph consisting of N vertices and M edges.\nThe vertices are numbered 1 to N, and the edges are numbered 1 to M.\nIn addition, each vertex has a label, A or B. The label of Vertex i is s_i.\nEdge i bidirectionally connects vertex a_i and b_i.

\n

The phantom thief Nusook likes to choose some vertex as the startpoint and traverse an edge zero or more times.\nToday, he will make a string after traveling as above, by placing the labels of the visited vertices in the order visited, beginning from the startpoint.

\n

For example, in a graph where Vertex 1 has the label A and Vertex 2 has the label B, if Nusook travels along the path 1 \\rightarrow 2 \\rightarrow 1 \\rightarrow 2 \\rightarrow 2, the resulting string is ABABB.

\n

Determine if Nusook can make all strings consisting of A and B.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 2 \\times 10^{5}
  • \n
  • 1 \\leq M \\leq 2 \\times 10^{5}
  • \n
  • |s| = N
  • \n
  • s_i is A or B.
  • \n
  • 1 \\leq a_i, b_i \\leq N
  • \n
  • The given graph may NOT be simple or connected.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\ns\na_1 b_1\n:\na_{M} b_{M}\n
\n
\n
\n
\n
\n

Output

If Nusook can make all strings consisting of A and B, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3\nAB\n1 1\n1 2\n2 2\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n
    \n
  • Since Nusook can visit Vertex 1 and Vertex 2 in any way he likes, he can make all strings consisting of A and B.
  • \n
\n
\n\"77e96cf8e213d606ddd8f3c3f8315d32.png\"\n
\n
\n
\n
\n
\n
\n

Sample Input 2

4 3\nABAB\n1 2\n2 3\n3 1\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
    \n
  • For example, Nusook cannot make BB.
  • \n
  • The given graph may not be connected.
  • \n
\n
\n\"1ab1411cb9d6ee023d14ca4e77c4b584.png\"\n
\n
\n
\n
\n
\n
\n

Sample Input 3

13 23\nABAAAABBBBAAB\n7 1\n10 6\n1 11\n2 10\n2 8\n2 11\n11 12\n8 3\n7 12\n11 2\n13 13\n11 9\n4 1\n9 7\n9 6\n8 13\n8 6\n4 10\n8 7\n4 3\n2 1\n8 12\n6 9\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
\n
\n
\n
\n

Sample Input 4

13 17\nBBABBBAABABBA\n7 1\n7 9\n11 12\n3 9\n11 9\n2 1\n11 5\n12 11\n10 8\n1 11\n1 8\n7 7\n9 10\n8 8\n8 12\n6 2\n13 11\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int M;\n int u;\n int w;\n int deg[2][200001] = {};\n char s[200002];\n list *adj[200001] = {};\n list e[400001];\n list *p;\n scanf(\"%d %d\", &N, &M);\n scanf(\"%s\", &(s[1]));\n for (i = 0; i < M; i++) {\n scanf(\"%d %d\", &u, &w);\n if (u == w) {\n deg[s[u] - 'A'][u]++;\n continue;\n }\n e[i * 2].v = w;\n e[(i * 2) + 1].v = u;\n e[i * 2].next = adj[u];\n e[(i * 2) + 1].next = adj[w];\n adj[u] = &(e[i * 2]);\n adj[w] = &(e[(i * 2) + 1]);\n deg[s[w] - 'A'][u]++;\n deg[s[u] - 'A'][w]++;\n }\n\n int flag[200001] = {};\n int q[200001];\n int head;\n int tail;\n for (i = 1; i <= N; i++) {\n if (flag[i] == 1 || (deg[0][i] != 0 && deg[1][i] != 0)) {\n continue;\n }\n flag[i] = 1;\n q[0] = i;\n for (head = 0, tail = 1; head < tail; head++) {\n u = q[head];\n for (p = adj[u]; p != NULL; p = p->next) {\n if (flag[p->v] == 1) {\n continue;\n }\n w = p->v;\n deg[s[u] - 'A'][w]--;\n if (deg[s[u] - 'A'][w] == 0) {\n flag[w] = 1;\n q[tail++] = w;\n }\n }\n }\n }\n for (i = 1; i <= N; i++) {\n if (flag[i] == 0) {\n break;\n }\n }\n if (i <= N) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n fflush(stdout);\n return 0;\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 n = it.next().unwrap().parse::().unwrap();\n let m = it.next().unwrap().parse::().unwrap();\n let s = it.next().unwrap().as_bytes();\n let mut to = vec![vec![0; n]; 2];\n let mut g = vec![Vec::new(); n];\n for _ in 0..m {\n let a = it.next().unwrap().parse::().unwrap() - 1;\n let b = it.next().unwrap().parse::().unwrap() - 1;\n to[(s[b] - b'A') as usize][a] += 1;\n to[(s[a] - b'A') as usize][b] += 1;\n g[a].push(b);\n g[b].push(a);\n }\n let mut visit = vec![0; n];\n let mut qu = VecDeque::new();\n for i in 0..n {\n if to[0][i] == 0 || to[1][i] == 0 {\n qu.push_back(i);\n visit[i] = 1;\n }\n }\n while !qu.is_empty() {\n let p = qu.pop_front().unwrap();\n let idx = (s[p] - b'A') as usize;\n for &t in &g[p] {\n to[idx][t] -= 1;\n if to[idx][t] == 0 && visit[t] == 0 {\n qu.push_back(t);\n visit[t] = 1;\n }\n }\n }\n println!(\"{}\", if visit.contains(&0) { \"Yes\" } else { \"No\" });\n}", "difficulty": "medium"} {"problem_id": "1669", "problem_description": "It has finally been decided to build a roof over the football field in School 179. Its construction will require placing $$$n$$$ consecutive vertical pillars. Furthermore, the headmaster wants the heights of all the pillars to form a permutation $$$p$$$ of integers from $$$0$$$ to $$$n - 1$$$, where $$$p_i$$$ is the height of the $$$i$$$-th pillar from the left $$$(1 \\le i \\le n)$$$.As the chief, you know that the cost of construction of consecutive pillars is equal to the maximum value of the bitwise XOR of heights of all pairs of adjacent pillars. In other words, the cost of construction is equal to $$$\\max\\limits_{1 \\le i \\le n - 1}{p_i \\oplus p_{i + 1}}$$$, where $$$\\oplus$$$ denotes the bitwise XOR operation.Find any sequence of pillar heights $$$p$$$ of length $$$n$$$ with the smallest construction cost.In this problem, a permutation is an array consisting of $$$n$$$ distinct integers from $$$0$$$ to $$$n - 1$$$ in arbitrary order. For example, $$$[2,3,1,0,4]$$$ is a permutation, but $$$[1,0,1]$$$ is not a permutation ($$$1$$$ appears twice in the array) and $$$[1,0,3]$$$ is also not a permutation ($$$n=3$$$, but $$$3$$$ is in the array).", "c_code": "int solution() {\n int tt;\n scanf(\"%d\", &tt);\n while (tt--) {\n int n;\n scanf(\"%d\", &n);\n int k = 1;\n while (k < n) {\n k *= 2;\n }\n k /= 2;\n for (int i = 1; i < n; i++) {\n if (i == k) {\n printf(\"0 %d \", k);\n } else {\n printf(\"%d \", i);\n }\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 m = 1 << 18;\n while (n - 1) & m == 0 {\n m >>= 1;\n }\n let mut ans = vec![];\n for i in 0..m {\n ans.push(m - i - 1);\n }\n for i in m..n {\n ans.push(i);\n }\n print!(\"{}\", ans[0]);\n for i in 1..n {\n print!(\" {}\", ans[i]);\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1670", "problem_description": "A sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.Given two integers $$$n$$$ and $$$k$$$, construct a permutation $$$a$$$ of numbers from $$$1$$$ to $$$n$$$ which has exactly $$$k$$$ peaks. An index $$$i$$$ of an array $$$a$$$ of size $$$n$$$ is said to be a peak if $$$1 < i < n$$$ and $$$a_i \\gt a_{i-1}$$$ and $$$a_i \\gt a_{i+1}$$$. If such permutation is not possible, then print $$$-1$$$.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n int k = 0;\n scanf(\"%d\", &n);\n scanf(\"%d\", &k);\n int a[n];\n for (int i = 0; i < n; i++) {\n a[i] = i + 1;\n }\n\n if (k > (n - 1) / 2) {\n printf(\"-1\\n\");\n } else {\n for (int i = 1; i <= k; i++) {\n\n int temp = 0;\n temp = a[n - 1];\n for (int j = n - 1; j >= 2 * i; j--) {\n a[j] = a[j - 1];\n }\n\n a[(2 * i) - 1] = temp;\n }\n\n for (int i = 0; i < n; i++) {\n printf(\"%d \", a[i]);\n }\n printf(\"\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let t: i64 = s.trim().parse().unwrap();\n for _i in 0..t {\n s.clear();\n stdin().read_line(&mut s).unwrap();\n let mut iter = s.split_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let k: usize = iter.next().unwrap().parse().unwrap();\n\n if n <= 2 * k || (n < 3 && k != 0) {\n println!(\"-1\");\n continue;\n }\n let mut l = 1;\n let mut r = n;\n let mut ans = vec![0; n];\n for i in 0..k {\n ans[2 * i] = l;\n ans[2 * i + 1] = r;\n\n l += 1;\n r -= 1;\n }\n for i in 2 * k..n {\n ans[i] = l;\n l += 1;\n }\n for i in 0..ans.len() {\n if i != 0 {\n print!(\" {}\", ans[i]);\n } else {\n print!(\"{}\", ans[i]);\n }\n }\n println!();\n }\n}", "difficulty": "easy"} {"problem_id": "1671", "problem_description": "Alice gave Bob two integers $$$a$$$ and $$$b$$$ ($$$a > 0$$$ and $$$b \\ge 0$$$). Being a curious boy, Bob wrote down an array of non-negative integers with $$$\\operatorname{MEX}$$$ value of all elements equal to $$$a$$$ and $$$\\operatorname{XOR}$$$ value of all elements equal to $$$b$$$.What is the shortest possible length of the array Bob wrote?Recall that the $$$\\operatorname{MEX}$$$ (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the $$$\\operatorname{XOR}$$$ of an array is the bitwise XOR of all the elements of the array.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int a;\n int b;\n scanf(\"%d%d\", &a, &b);\n int temp = 0;\n if ((a - 1) % 4 == 0) {\n temp = a - 1;\n } else if ((a - 1) % 4 == 1) {\n temp = 1;\n } else if ((a - 1) % 4 == 2) {\n temp = a;\n } else {\n temp = 0;\n }\n if (temp == b) {\n printf(\"%d\\n\", a);\n } else if (temp != b && (temp ^ b) != a) {\n printf(\"%d\\n\", a + 1);\n } else {\n printf(\"%d\\n\", a + 2);\n }\n }\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n const MAX_VALUE: usize = 3 * 100000;\n let mut dp: [usize; MAX_VALUE] = [0; MAX_VALUE];\n\n for i in 1..MAX_VALUE {\n dp[i] = dp[i - 1] ^ i;\n }\n\n let solve = |a, b| -> usize {\n let prev = a - 1;\n let xor_up_to = dp[prev];\n let remain = xor_up_to ^ b;\n if remain == 0 {\n a\n } else if remain == a {\n a + 2\n } else {\n a + 1\n }\n };\n\n let stdin = io::stdin();\n stdin.read_line(&mut s).expect(\"string\");\n let t: i32 = s.trim().parse().expect(\"int\");\n s.clear();\n\n for _ in 0..t {\n stdin.read_line(&mut s).expect(\"string\");\n let v: Vec = s\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n s.clear();\n println!(\"{}\", solve(v[0], v[1]));\n }\n}", "difficulty": "hard"} {"problem_id": "1672", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

\n
    \n
  • Query i (1 \\leq i \\leq Q): You will be given integers l_i and r_i (1 \\leq l_i < r_i \\leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?
  • \n
\n
\n
\n
\n
\n

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

\n

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 10^5
  • \n
  • 1 \\leq Q \\leq 10^5
  • \n
  • S is a string of length N.
  • \n
  • Each character in S is A, C, G or T.
  • \n
  • 1 \\leq l_i < r_i \\leq N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N Q\nS\nl_1 r_1\n:\nl_Q r_Q\n
\n
\n
\n
\n
\n

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8 3\nACACTACG\n3 7\n2 3\n1 8\n
\n
\n
\n
\n
\n

Sample Output 1

2\n0\n3\n
\n
    \n
  • Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.
  • \n
  • Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.
  • \n
  • Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.
  • \n
\n
\n
", "c_code": "int solution(void) {\n int n;\n int q;\n scanf(\"%d %d\\n\", &n, &q);\n char s[n];\n int count[n];\n int l[q];\n int r[q];\n count[0] = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%c\\n\", &s[i]);\n }\n for (int j = 0; j < n; j++) {\n if (s[j] == 'A' && s[j + 1] == 'C') {\n count[j + 1] = count[j] + 1;\n } else {\n count[j + 1] = count[j];\n }\n }\n for (int i = 0; i < q; i++) {\n scanf(\"%d %d\\n\", &l[i], &r[i]);\n printf(\"%d\\n\", count[r[i] - 1] - count[l[i] - 1]);\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 let mut iter = buf.split_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let q: usize = iter.next().unwrap().parse().unwrap();\n let s: Vec = iter.next().unwrap().chars().collect();\n let mut a = vec![0; n + 1];\n for i in 0..n - 1 {\n if s[i] == 'A' && s[i + 1] == 'C' {\n a[i + 1] = 1;\n }\n }\n for i in 1..n + 1 {\n a[i] += a[i - 1];\n }\n for _ in 0..q {\n let l: usize = iter.next().unwrap().parse().unwrap();\n let r: usize = iter.next().unwrap().parse().unwrap();\n println!(\"{}\", a[r - 1] - a[l - 1]);\n }\n}", "difficulty": "easy"} {"problem_id": "1673", "problem_description": "There's a chessboard of size $$$n \\times n$$$. $$$m$$$ rooks are placed on it in such a way that: no two rooks occupy the same cell; no two rooks attack each other. A rook attacks all cells that are in its row or column.Is it possible to move exactly one rook (you can choose which one to move) into a different cell so that no two rooks still attack each other? A rook can move into any cell in its row or column if no other rook stands on its path.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int m;\n int n;\n scanf(\"%d%d\", &m, &n);\n int arr[2 * n];\n for (int i = 0; i < 2 * n; i++) {\n scanf(\"%d\", &arr[n]);\n }\n if (m == n) {\n printf(\"No\\n\");\n } else {\n printf(\"Yes\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let t = buffer.trim().parse::().unwrap();\n for _ in 0..t {\n buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let mut parts = buffer.split_whitespace().map(|s| s.parse::().unwrap());\n let n = parts.next().unwrap();\n let m = parts.next().unwrap();\n for _ in 0..m {\n io::stdin().read_line(&mut buffer).unwrap();\n }\n if m < n {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1674", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

\n

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.\nThese buttons are numbered 1 through N.\nWhen Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.\nWhen Button i is not lighten up, nothing will happen by pressing it.

\n

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

\n

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ N ≤ 10^5
  • \n
  • 1 ≤ a_i ≤ N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1\na_2\n:\na_N\n
\n
\n
\n
\n
\n

Output

Print -1 if it is impossible to lighten up Button 2.\nOtherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n3\n1\n2\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Press Button 1, then Button 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n3\n4\n1\n2\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

\n
\n
\n
\n
\n
\n

Sample Input 3

5\n3\n3\n4\n2\n4\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution() {\n int N = 0;\n scanf(\"%d\", &N);\n int a[N - 1];\n for (int n = 0; n < N; n++) {\n scanf(\"%d\", &a[n]);\n }\n int check = 1;\n int num = 0;\n int count = 0;\n while (check) {\n num = a[num];\n count++;\n if (num == 2) {\n check = 0;\n }\n num--;\n if (count > N) {\n break;\n }\n }\n\n if (check) {\n printf(\"-1\\n\");\n } else {\n printf(\"%d\\n\", count);\n }\n\n return 0;\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 n: usize = iter.next().unwrap().parse().unwrap();\n let buttons: Vec = (0..n)\n .map(|_| iter.next().unwrap().parse().unwrap())\n .collect();\n\n let mut cnt = 0;\n let mut target_button_index = 1;\n while target_button_index != 2 && cnt < 100000 {\n target_button_index = buttons[target_button_index - 1];\n cnt += 1;\n }\n if cnt == 100000 {\n println!(\"-1\");\n } else {\n println!(\"{}\", cnt);\n }\n}", "difficulty": "medium"} {"problem_id": "1675", "problem_description": "An array is beautiful if both of the following two conditions meet: there are at least $$$l_1$$$ and at most $$$r_1$$$ elements in the array equal to its minimum; there are at least $$$l_2$$$ and at most $$$r_2$$$ elements in the array equal to its maximum. For example, the array $$$[2, 3, 2, 4, 4, 3, 2]$$$ has $$$3$$$ elements equal to its minimum ($$$1$$$-st, $$$3$$$-rd and $$$7$$$-th) and $$$2$$$ elements equal to its maximum ($$$4$$$-th and $$$5$$$-th).Another example: the array $$$[42, 42, 42]$$$ has $$$3$$$ elements equal to its minimum and $$$3$$$ elements equal to its maximum.Your task is to calculate the minimum possible number of elements in a beautiful array.", "c_code": "int solution() {\n int test_cases = 0;\n scanf(\"%d\", &test_cases);\n\n while (test_cases--) {\n int l1 = 0;\n int r1 = 0;\n int l2 = 0;\n int r2 = 0;\n\n scanf(\"%d\", &l1);\n scanf(\"%d\", &r1);\n scanf(\"%d\", &l2);\n scanf(\"%d\", &r2);\n\n int min = 100000;\n if (r2 < l1 || r1 < l2) {\n printf(\"%d\\n\", l1 + l2);\n } else {\n if (l2 > l1) {\n printf(\"%d\\n\", l2);\n } else {\n printf(\"%d\\n\", l1);\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut str_t = String::new();\n io::stdin().read_line(&mut str_t);\n let mut total_case = 0;\n match str::parse::(str_t.trim()) {\n Ok(val) => total_case = val,\n Err(_) => print!(\"error\"),\n };\n\n for _i in 0..total_case {\n let mut str_l = String::new();\n io::stdin().read_line(&mut str_l);\n let numbers = str_l\n .trim()\n .split(' ')\n .flat_map(str::parse::)\n .collect::>();\n let (l1, r1, l2, r2) = (numbers[0], numbers[1], numbers[2], numbers[3]);\n\n if r1 < l2 || r2 < l1 {\n println!(\"{}\", l1 + l2);\n } else if l1 <= l2 && l2 <= r1 {\n println!(\"{}\", l2);\n } else {\n println!(\"{}\", l1);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1676", "problem_description": "Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.", "c_code": "int solution() {\n int numSets = 0;\n scanf(\"%d\", &numSets);\n\n for (int currentSet = 0; currentSet < numSets; ++currentSet) {\n int numElements = 0;\n scanf(\"%d\", &numElements);\n\n if (numElements == 3) {\n printf(\"2 1 3\\n\");\n continue;\n }\n\n if ((numElements % 2) == 1) {\n printf(\"0 \");\n numElements -= 1;\n }\n if ((numElements % 4) == 2) {\n printf(\"4 1 2 12 3 8\");\n numElements -= 6;\n if (numElements > 0) {\n printf(\" \");\n } else {\n printf(\"\\n\");\n continue;\n }\n }\n int currentPrint = 14;\n for (int i = 1; i < numElements; ++i) {\n printf(\"%d \", currentPrint++);\n }\n printf(\"%d\\n\", currentPrint);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut lines = io::stdin().lines();\n lines.next();\n for line in lines {\n let n = line.unwrap().parse::().unwrap();\n let v = (0..(n - 3)).collect::>();\n let xor = v.iter().fold(0, |a, b| a ^ b);\n let x1 = 1 << 29;\n let x2 = 1 << 30;\n let x3 = xor ^ x1 ^ x2;\n let res = v\n .iter()\n .map(|x| x.to_string())\n .collect::>()\n .join(\" \");\n\n println!(\"{res} {x1} {x2} {x3}\");\n }\n}", "difficulty": "hard"} {"problem_id": "1677", "problem_description": "An array $$$a_1, a_2, \\ldots, a_n$$$ is good if and only if for every subsegment $$$1 \\leq l \\leq r \\leq n$$$, the following holds: $$$a_l + a_{l + 1} + \\ldots + a_r = \\frac{1}{2}(a_l + a_r) \\cdot (r - l + 1)$$$. You are given an array of integers $$$a_1, a_2, \\ldots, a_n$$$. In one operation, you can replace any one element of this array with any real number. Find the minimum number of operations you need to make this array good.", "c_code": "int solution() {\n int t;\n int i;\n scanf(\"%d\", &t);\n for (i = 0; i < t; i++) {\n int n;\n int j;\n int k;\n int l;\n scanf(\"%d\", &n);\n int arr[n];\n int max = 1;\n for (j = 0; j < n; j++) {\n scanf(\"%d\", &arr[j]);\n }\n for (j = 0; j < n - 1; j++) {\n for (k = j + 1; k < n; k++) {\n int min = 2;\n for (l = k + 1; l < n; l++) {\n if ((arr[k] - arr[j]) * (l - k) == (arr[l] - arr[k]) * (k - j)) {\n min++;\n }\n }\n if (min > max) {\n max = min;\n }\n }\n }\n printf(\"%d\\n\", n - max);\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 x: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n if n == 1 {\n println!(\"0\");\n continue;\n }\n let mut ans: usize = n;\n for i in 0..(n - 1) {\n for j in (i + 1)..n {\n let nu: i64 = x[j] - x[i];\n let d: i64 = (j - i) as i64;\n let b: i64 = x[i] * d - (nu * i as i64);\n let cnt = (0..n).filter(|&i| x[i] * d != nu * i as i64 + b).count();\n ans = min(ans, cnt);\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1678", "problem_description": "Polycarp must pay exactly $$$n$$$ burles at the checkout. He has coins of two nominal values: $$$1$$$ burle and $$$2$$$ burles. Polycarp likes both kinds of coins equally. So he doesn't want to pay with more coins of one type than with the other.Thus, Polycarp wants to minimize the difference between the count of coins of $$$1$$$ burle and $$$2$$$ burles being used. Help him by determining two non-negative integer values $$$c_1$$$ and $$$c_2$$$ which are the number of coins of $$$1$$$ burle and $$$2$$$ burles, respectively, so that the total value of that number of coins is exactly $$$n$$$ (i. e. $$$c_1 + 2 \\cdot c_2 = n$$$), and the absolute value of the difference between $$$c_1$$$ and $$$c_2$$$ is as little as possible (i. e. you must minimize $$$|c_1-c_2|$$$).", "c_code": "int solution() {\n int n = 0;\n int c1 = 0;\n int c2 = 0;\n int t = 0;\n int c = 0;\n int y = 0;\n scanf(\"%d\\n\", &t);\n for (int i = 0; i < t; i++) {\n scanf(\"%d\", &n);\n c = n / 3;\n y = n % 3;\n if (1 <= y && y <= 2) {\n if (y == 1) {\n c1 = c + 1;\n c2 = c;\n } else {\n c1 = c;\n c2 = c + 1;\n }\n } else {\n c1 = c;\n c2 = c;\n ;\n }\n printf(\"%d %d\\n\", c1, c2);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n\n io::stdin().read_line(&mut t).unwrap();\n\n for _ in 0..t.trim().parse::().unwrap() {\n let mut n = String::new();\n io::stdin().read_line(&mut n).unwrap();\n\n let sum = n.trim().parse::().unwrap();\n\n if sum % 3 == 0 {\n println!(\"{} {}\", sum / 3, sum / 3)\n } else {\n if sum % 3 == 1 {\n println!(\"{} {}\", sum / 3 + 1, sum / 3)\n } else {\n println!(\"{} {}\", sum / 3, sum / 3 + 1);\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1679", "problem_description": "There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n while (t--) {\n long long int n;\n long long int val = 0;\n long long int k = 0;\n scanf(\"%lld\\n\", &n);\n char c[n];\n long long int a[n];\n\n for (long long int i = 0; i < n; i++) {\n scanf(\"%c\", &c[i]);\n\n if (c[i] == 'L') {\n val += i;\n } else {\n val += n - 1 - i;\n }\n }\n for (long long int i = 0; i < n / 2; i++) {\n if (c[i] == 'L') {\n printf(\"%lld \", val += n - (2 * i + 1));\n k++;\n }\n if (c[n - i - 1] == 'R') {\n printf(\"%lld \", val += n - (2 * i + 1));\n k++;\n }\n }\n for (int i = k; i < n; i++) {\n printf(\"%lld \", val);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let t: usize = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().parse().unwrap()\n };\n for _i in 0..t {\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 s: Vec = {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n };\n let mut ans = Vec::new();\n let mut sum = 0;\n\n for i in 0..n {\n let ss = s[i];\n if ss == 'L' {\n ans.push((n - i - 1 - i) as i64);\n sum += i as i64;\n } else {\n ans.push((i - (n - i - 1)) as i64);\n sum += n as i64 - i as i64 - 1;\n }\n }\n ans.sort_by_key(|&x| Reverse(x));\n\n let mut ans2 = Vec::new();\n for d in ans {\n if d > 0 {\n sum += d;\n }\n ans2.push(sum);\n }\n println!(\n \"{}\",\n ans2.iter()\n .map(std::string::ToString::to_string)\n .collect::>()\n .join(\" \")\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1680", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

In \"Takahashi-ya\", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions).

\n

A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen.

\n

Write a program that, when S is given, prints the price of the corresponding bowl of ramen.

\n
\n
\n
\n
\n

Constraints

    \n
  • S is a string of length 3.
  • \n
  • Each character in S is o or x.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

Print the price of the bowl of ramen corresponding to S.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

oxo\n
\n
\n
\n
\n
\n

Sample Output 1

900\n
\n

The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \\times 2 = 900 yen.

\n
\n
\n
\n
\n
\n

Sample Input 2

ooo\n
\n
\n
\n
\n
\n

Sample Output 2

1000\n
\n

The price of a ramen topped with all three kinds of toppings is 700 + 100 \\times 3 = 1000 yen.

\n
\n
\n
\n
\n
\n

Sample Input 3

xxx\n
\n
\n
\n
\n
\n

Sample Output 3

700\n
\n

The price of a ramen without any toppings is 700 yen.

\n
\n
", "c_code": "int solution() {\n char top = 0;\n char in[4];\n scanf(\"%s\", in);\n\n for (int i = 0; i < 4; i++) {\n if (*(in + (i * sizeof(char))) == 'o') {\n top++;\n }\n }\n printf(\"%d\\n\", 700 + (top * 100));\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n let cnt = buf.trim().chars().filter(|&x| x == 'o').count();\n println!(\"{}\", 700 + cnt * 100);\n}", "difficulty": "easy"} {"problem_id": "1681", "problem_description": "Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx ≤ T ≤ rx.Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d\", &n);\n long int a[n];\n int i;\n long int sum = 0;\n long int max = 100001;\n for (i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n sum += a[i];\n }\n\n int flag = 0;\n\n scanf(\"%d\", &m);\n long int l[m];\n long int r[m];\n for (i = 0; i < m; i++) {\n scanf(\"%ld %ld\", &l[i], &r[i]);\n }\n for (i = 0; i < m; i++) {\n\n if (l[i] <= sum && (r[i] >= sum)) {\n printf(\"%ld\", sum);\n flag = 1;\n break;\n }\n if (r[i] < max && (r[i] > sum)) {\n max = r[i];\n }\n if (l[i] < max && (l[i] > sum)) {\n max = l[i];\n }\n }\n if (!flag) {\n if (max == 100001) {\n printf(\"-1\");\n } else {\n printf(\"%ld\", max);\n }\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let n = buffer.trim().parse::().unwrap();\n\n buffer.clear();\n io::stdin().read_line(&mut buffer).unwrap();\n let mut v = buffer\n .split_whitespace()\n .map(|x| x.trim().parse::().unwrap())\n .collect::>();\n v.sort();\n\n buffer.clear();\n io::stdin().read_line(&mut buffer).unwrap();\n let m = buffer.trim().parse::().unwrap();\n let mut i = vec![-1];\n let mut e = vec![-1];\n\n for _ in 0..m {\n buffer.clear();\n io::stdin().read_line(&mut buffer).unwrap();\n let aux = buffer\n .split_whitespace()\n .map(|x| x.trim().parse::().unwrap())\n .collect::>();\n i.push(aux[0]);\n e.push(aux[1]);\n }\n\n let mut last = 0;\n let mut ok = m > 0;\n if ok {\n for j in 0..n {\n if last + v[j] > e[e.len() - 1] {\n ok = false;\n break;\n }\n last += v[j];\n }\n }\n if ok {\n let index = match i.binary_search(&last) {\n Ok(x) => x,\n Err(x) => x,\n };\n if e[index - 1] >= last {\n println!(\"{}\", last);\n } else {\n println!(\"{}\", i[index]);\n }\n } else {\n println!(\"-1\");\n }\n}", "difficulty": "medium"} {"problem_id": "1682", "problem_description": "

Vector

\n \n

\n For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:\n

\n\n
    \n
  • pushBack($x$): add element $x$ at the end of $A$
  • \n
  • randomAccess($p$):print element $a_p$
  • \n
  • popBack(): delete the last element of $A$
  • \n
\n\n

\n \n$A$ is a 0-origin array and it is empty in the initial state.\n

\n\n\n

Input

\n\n

\n The input is given in the following format.\n

\n\n
\n$q$\n$query_1$\n$query_2$\n:\n$query_q$\n
\n\n

\n Each query $query_i$ is given by\n

\n\n
\n0 $x$\n
\n\n

or

\n\n
\n1 $p$\n
\n\n

or

\n\n
\n2\n
\n\n

\n where the first digits 0, 1 and 2 represent pushBack, randomAccess and popBack operations respectively.\n

\n\n

\n randomAccess and popBack operations will not be given for an empty array.\n

\n\n

Output

\n

\n For each randomAccess, print $a_p$ in a line.\n

\n\n

Constraints

\n
    \n
  • $1 \\leq q \\leq 200,000$
  • \n
  • $0 \\leq p < $ the size of $A$
  • \n
  • $-1,000,000,000 \\leq x \\leq 1,000,000,000$
  • \n
\n\n

Sample Input 1

\n\n
\n8\n0 1\n0 2\n0 3\n2\n0 4\n1 0\n1 1\n1 2\n
\n\n

Sample Output 1

\n\n
\n1\n2\n4\n
", "c_code": "int solution() {\n int q;\n scanf(\"%d\\n\", &q);\n\n int a[q];\n int i = 0;\n\n int com;\n int x;\n while (q--) {\n scanf(\"%d \\n\", &com);\n switch (com) {\n case 0:\n scanf(\"%d\\n\", &x);\n a[i++] = x;\n break;\n case 1:\n scanf(\"%d\\n\", &x);\n printf(\"%d\\n\", a[x]);\n break;\n case 2:\n a[i--] = -1;\n break;\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n let row: usize = s.trim().parse().unwrap_or(0);\n s.clear();\n let mut array = Vec::new();\n\n for _ in 0..row {\n io::stdin().read_line(&mut s).ok();\n let v: Vec = s\n .split_whitespace()\n .map(|e| e.parse().unwrap_or(0))\n .collect();\n\n match v[0] {\n 0 => {\n array.push(v[1]);\n }\n 1 => {\n println!(\"{}\", array[v[1] as usize]);\n }\n 2 => {\n array.pop();\n }\n _ => println!(\"error 0 or 1 or 2\"),\n }\n s.clear();\n }\n}", "difficulty": "hard"} {"problem_id": "1683", "problem_description": "You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.", "c_code": "int solution(void) {\n int Q;\n char s[51];\n char t[51];\n\n scanf(\"%d\", &Q);\n while (Q--) {\n scanf(\"%s %s\", s, t);\n int a = 0;\n int b = 0;\n for (int i = 0; t[i]; ++i) {\n if (t[i] == 'a') {\n ++a;\n } else {\n b = 1;\n }\n }\n if (a && b) {\n printf(\"-1\\n\");\n } else if (b) {\n int len = strlen(s);\n printf(\"%lld\\n\", (1LL << len));\n } else if (a) {\n if (a == 1) {\n printf(\"1\\n\");\n } else {\n printf(\"-1\\n\");\n }\n }\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();\n let q: usize = input.next().unwrap().parse().unwrap();\n for _ in 0..q {\n let s = input.next().unwrap();\n let t = input.next().unwrap();\n if t.contains('a') {\n if t.len() > 1 {\n output.push_str(\"-1\\n\");\n } else {\n output.push_str(\"1\\n\");\n }\n } else {\n let a_count = s.bytes().filter(|&c| c == b'a').count();\n writeln!(output, \"{}\", 1u64 << a_count).ok();\n }\n }\n print!(\"{output}\");\n}", "difficulty": "easy"} {"problem_id": "1684", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given an integer N.
\nFor two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B.
\nFor example, F(3,11) = 2 since 3 has one digit and 11 has two digits.
\nFind the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \\times B.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^{10}
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \\times B.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10000\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

F(A,B) has a minimum value of 3 at (A,B)=(100,100).

\n
\n
\n
\n
\n
\n

Sample Input 2

1000003\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n

There are two pairs (A,B) that satisfy the condition: (1,1000003) and (1000003,1). For these pairs, F(1,1000003)=F(1000003,1)=7.

\n
\n
\n
\n
\n
\n

Sample Input 3

9876543210\n
\n
\n
\n
\n
\n

Sample Output 3

6\n
\n
\n
", "c_code": "int solution(void) {\n int64_t N;\n scanf(\"%ld\", &N);\n\n int MIN = 10;\n for (int64_t i = 1; i * i <= N; i++) {\n if (N % i == 0) {\n int64_t B = N / i;\n int M = 0;\n while (B > 0) {\n B /= 10;\n M++;\n }\n MIN = M;\n }\n }\n printf(\"%d\\n\", MIN);\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\n let keta = |x: usize| {\n let mut y = x;\n let mut res = 0;\n while y > 0 {\n res += 1;\n y /= 10;\n }\n res\n };\n\n let mut ans = 1 << 30;\n let mut i = 1;\n while i * i <= n {\n if n.is_multiple_of(i) {\n let a = i;\n let b = n / i;\n ans = min(ans, max(keta(a), keta(b)));\n }\n i += 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1685", "problem_description": "There is a chessboard of size $$$n$$$ by $$$n$$$. The square in the $$$i$$$-th row from top and $$$j$$$-th column from the left is labelled $$$(i,j)$$$.Currently, Gregor has some pawns in the $$$n$$$-th row. There are also enemy pawns in the $$$1$$$-st row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (from $$$(i,j)$$$ to $$$(i-1,j)$$$) if there is no pawn in the destination square. Additionally, a pawn can move one square diagonally up (from $$$(i,j)$$$ to either $$$(i-1,j-1)$$$ or $$$(i-1,j+1)$$$) if and only if there is an enemy pawn in that square. The enemy pawn is also removed.Gregor wants to know what is the maximum number of his pawns that can reach row $$$1$$$?Note that only Gregor takes turns in this game, and the enemy pawns never move. Also, when Gregor's pawn reaches row $$$1$$$, it is stuck and cannot make any further moves.", "c_code": "int solution() {\n int tc;\n scanf(\"%d\", &tc);\n while (tc--) {\n int n;\n scanf(\"%d\", &n);\n char self[n + 1];\n char enemy[n + 1];\n scanf(\"%s\", enemy);\n scanf(\"%s\", self);\n\n int ans = 0;\n for (int i = 0; i < n; i++) {\n if (self[i] == '1' && enemy[i] == '0') {\n self[i] = '0';\n ans++;\n }\n }\n if (self[0] == '1' && enemy[1] == '1') {\n enemy[1] = '0';\n ans++;\n }\n for (int i = 1; i < n - 1; i++) {\n if (self[i] == '1' && enemy[i - 1] == '1') {\n ans++;\n } else if (self[i] == '1' && enemy[i + 1] == '1') {\n enemy[i + 1] = '0';\n ans++;\n }\n }\n if (self[n - 1] == '1' && enemy[n - 2] == '1') {\n ans++;\n }\n printf(\"%d\\n\", ans);\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 for _ 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 black = String::new();\n stdin().read_line(&mut black).unwrap();\n let mut black: Vec = black.trim().chars().collect();\n\n let mut white = String::new();\n stdin().read_line(&mut white).unwrap();\n let white: Vec = white.trim().chars().collect();\n\n let mut c: u32 = 0;\n for p in 0..n {\n if white[p] == '0' {\n continue;\n }\n\n if p > 0 && black[p - 1] == '1' {\n black[p - 1] = '0';\n c += 1;\n } else if black[p] == '0' {\n black[p] = '0';\n c += 1;\n } else if p + 1 < n && black[p + 1] == '1' {\n black[p + 1] = '0';\n c += 1;\n }\n }\n println!(\"{}\", c);\n }\n}", "difficulty": "medium"} {"problem_id": "1686", "problem_description": "While sailing on a boat, Inessa noticed a beautiful water lily flower above the lake's surface. She came closer and it turned out that the lily was exactly $$$H$$$ centimeters above the water surface. Inessa grabbed the flower and sailed the distance of $$$L$$$ centimeters. Exactly at this point the flower touched the water surface. Suppose that the lily grows at some point $$$A$$$ on the lake bottom, and its stem is always a straight segment with one endpoint at point $$$A$$$. Also suppose that initially the flower was exactly above the point $$$A$$$, i.e. its stem was vertical. Can you determine the depth of the lake at point $$$A$$$?", "c_code": "int solution() {\n float L;\n float H;\n scanf(\"%f\", &H);\n scanf(\"%f\", &L);\n printf(\"%f\", (L * L - H * H) / (2 * H));\n\n return 0;\n}", "rust_code": "fn solution() {\n let (h, l) = {\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 )\n };\n\n let answ = (l * l - h * h) / (2.0 * h);\n println!(\"{:.10}\", answ);\n}", "difficulty": "easy"} {"problem_id": "1687", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

\n
\n

\n\t\t Kyoto University Programming Contest is a programming contest voluntarily held by some Kyoto University students.\n\t\t This contest is abbreviated as Kyoto University Programming Contest and called KUPC.\n

\n

\n\t\t source: Kyoto University Programming Contest Information\n

\n
\n

\n\t\t The problem-preparing committee met to hold this year's KUPC and N problems were proposed there.\n\t\t The problems are numbered from 1 to N and the name of i-th problem is P_i.\n\t\t However, since they proposed too many problems, they decided to divide them into some sets for several contests.\n

\n

\n\t\t They decided to divide this year's KUPC into several KUPCs by dividing problems under the following conditions.\n

    \n
  • One KUPC provides K problems.
  • \n
  • Each problem appears at most once among all the KUPCs.
  • \n
  • All the first letters of the problem names in one KUPC must be different.
  • \n
\n

\n

\n\t\t You, one of the committee members, want to hold as many KUPCs as possible.\n\t\t Write a program to find the maximum number of KUPCs that can be held this year.\n

\n

Constraints

\n
    \n
  • 1 \\leq N \\leq 10^4
  • \n
  • 1 \\leq K \\leq 26
  • \n
  • 1 \\leq |P_i| \\leq 10
  • \n
  • All characters in P_i are capital letters.
  • \n
\n

\n\t\t Note that, for each i and j (1 \\leq i < j \\leq N),\n P_i \\neq P_j are not necessarily satisfied.\n

\n
\n
\n
\n
\n
\n
\n

Input

\n

The input is given from Standard Input in the following format:

\n
\nN K\nP_1\n:\nP_N\n
\n
\n
\n
\n
\n

Output

\n

\n\t\t Print the maximum number of KUPCs that can be held on one line.\n

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

\n
\n9 3\nAPPLE\nANT\nATCODER\nBLOCK\nBULL\nBOSS\nCAT\nDOG\nEGG\n
\n

For example, three KUPCs can be held by dividing the problems as follows.

\n
    \n
  • First: APPLE, BLOCK, CAT
  • \n
  • Second: ANT, BULL, DOG
  • \n
  • Third: ATCODER, BOSS, EGG
  • \n
\n
\n
\n
\n
\n

Sample Output 1

\n
\n3\n
\n
\n
\n
\n
\n

Sample Input 2

\n
\n3 2\nKU\nKYOUDAI\nKYOTOUNIV\n
\n
\n
\n
\n
\n

Sample Output 2

\n
\n0\n
\n

No KUPC can be held.

\n
\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n int a;\n int b[30] = {0};\n int c;\n int i;\n int j;\n char s[100010];\n scanf(\"%d %d\", &n, &m);\n for (i = 0; i < n; i++) {\n scanf(\"%s\", s);\n b[s[0] - 'A']++;\n }\n for (c = 0; 1; c++) {\n for (i = 0; i < 26; i++) {\n a = b[i];\n for (j = i; j && b[j - 1] < a; j--) {\n b[j] = b[j - 1];\n }\n b[j] = a;\n }\n\n for (i = 0; b[i]; i++) {\n ;\n }\n if (i < m) {\n break;\n }\n for (i = 0; i < m; i++) {\n b[i]--;\n }\n }\n printf(\"%d\\n\", c);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let n: usize = s.next().unwrap().parse().unwrap();\n let k: usize = s.next().unwrap().parse().unwrap();\n let mut mp = std::collections::BTreeMap::new();\n for _ in 0..n {\n let p = s.next().unwrap().chars().next().unwrap();\n *mp.entry(p).or_insert(0) += 1;\n }\n let mut q = mp\n .values()\n .cloned()\n .collect::>();\n let mut ans = 0;\n while q.len() >= k {\n let mut r = (0..k)\n .map(|_| q.pop().unwrap() - 1)\n .filter(|&a| a != 0)\n .collect::>();\n q.append(&mut r);\n ans += 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1688", "problem_description": "To celebrate your birthday you have prepared a festive table! Now you want to seat as many guests as possible.The table can be represented as a rectangle with height $$$h$$$ and width $$$w$$$, divided into $$$h \\times w$$$ cells. Let $$$(i, j)$$$ denote the cell in the $$$i$$$-th row and the $$$j$$$-th column of the rectangle ($$$1 \\le i \\le h$$$; $$$1 \\le j \\le w$$$).Into each cell of the table you can either put a plate or keep it empty.As each guest has to be seated next to their plate, you can only put plates on the edge of the table — into the first or the last row of the rectangle, or into the first or the last column. Formally, for each cell $$$(i, j)$$$ you put a plate into, at least one of the following conditions must be satisfied: $$$i = 1$$$, $$$i = h$$$, $$$j = 1$$$, $$$j = w$$$.To make the guests comfortable, no two plates must be put into cells that have a common side or corner. In other words, if cell $$$(i, j)$$$ contains a plate, you can't put plates into cells $$$(i - 1, j)$$$, $$$(i, j - 1)$$$, $$$(i + 1, j)$$$, $$$(i, j + 1)$$$, $$$(i - 1, j - 1)$$$, $$$(i - 1, j + 1)$$$, $$$(i + 1, j - 1)$$$, $$$(i + 1, j + 1)$$$.Put as many plates on the table as possible without violating the rules above.", "c_code": "int solution(void) {\n int T;\n int h;\n int w;\n scanf(\"%d\", &T);\n while (T--) {\n scanf(\"%d%d\", &h, &w);\n int a[h + 2][w + 2];\n memset(a, 0, sizeof(a));\n for (int i = 1; i <= h; ++i) {\n for (int j = 1; j <= w; ++j) {\n if (i == 1 || i == h || j == 1 || j == w) {\n if (!a[i - 1][j - 1] && !a[i][j - 1] && !a[i][j + 1] &&\n !a[i + 1][j] && !a[i - 1][j] && !a[i - 1][j + 1] &&\n !a[i][j + 1] && !a[i + 1][j + 1]) {\n a[i][j] = 1;\n }\n }\n if (i >= 1 && i <= h && j >= 1 && j <= w) {\n printf(\"%d\", a[i][j]);\n }\n }\n printf(\"\\n\");\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\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 _ in 0..t {\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let h = it.next().unwrap();\n let w = it.next().unwrap();\n\n for i in 0..h {\n for j in 0..w {\n let s = if i % 2 == 0 && (j == 0 || j + 1 == w)\n || j > 1 && j + 2 < w && j % 2 == 0 && (i == 0 || i + 1 == h)\n {\n \"1\"\n } else {\n \"0\"\n };\n output.write_all(s.as_bytes()).unwrap();\n }\n writeln!(&mut output).unwrap();\n }\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1689", "problem_description": "An array $$$b$$$ of length $$$k$$$ is called good if its arithmetic mean is equal to $$$1$$$. More formally, if $$$$$$\\frac{b_1 + \\cdots + b_k}{k}=1.$$$$$$Note that the value $$$\\frac{b_1+\\cdots+b_k}{k}$$$ is not rounded up or down. For example, the array $$$[1,1,1,2]$$$ has an arithmetic mean of $$$1.25$$$, which is not equal to $$$1$$$.You are given an integer array $$$a$$$ of length $$$n$$$. In an operation, you can append a non-negative integer to the end of the array. What's the minimum number of operations required to make the array good?We have a proof that it is always possible with finitely many operations.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n int i = 0;\n for (i = 0; i < t; i++) {\n int n = 0;\n scanf(\"%d\", &n);\n int a[n];\n int j = 0;\n int sum = 0;\n for (j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n sum += a[j];\n }\n if (sum == n) {\n printf(\"0\\n\");\n } else if (sum > n) {\n printf(\"%d\\n\", sum - n);\n } else if (sum < n) {\n printf(\"1\\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 for l in i.lock().lines().skip(2).step_by(2) {\n let (c, s) = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .fold((0, 0), |(c, s), x| (c + 1, s + x));\n writeln!(o, \"{}\", if s < c { 1 } else { s - c }).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "1690", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

You are given a string s of length n.\nDoes a tree with n vertices that satisfies the following conditions exist?

\n
    \n
  • The vertices are numbered 1,2,..., n.
  • \n
  • The edges are numbered 1,2,..., n-1, and Edge i connects Vertex u_i and v_i.
  • \n
  • If the i-th character in s is 1, we can have a connected component of size i by removing one edge from the tree.
  • \n
  • If the i-th character in s is 0, we cannot have a connected component of size i by removing any one edge from the tree.
  • \n
\n

If such a tree exists, construct one such tree.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq n \\leq 10^5
  • \n
  • s is a string of length n consisting of 0 and 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

If a tree with n vertices that satisfies the conditions does not exist, print -1.

\n

If a tree with n vertices that satisfies the conditions exist, print n-1 lines.\nThe i-th line should contain u_i and v_i with a space in between.\nIf there are multiple trees that satisfy the conditions, any such tree will be accepted.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1111\n
\n
\n
\n
\n
\n

Sample Output 1

-1\n
\n

It is impossible to have a connected component of size n after removing one edge from a tree with n vertices.

\n
\n
\n
\n
\n
\n

Sample Input 2

1110\n
\n
\n
\n
\n
\n

Sample Output 2

1 2\n2 3\n3 4\n
\n

If Edge 1 or Edge 3 is removed, we will have a connected component of size 1 and another of size 3. If Edge 2 is removed, we will have two connected components, each of size 2.

\n
\n
\n
\n
\n
\n

Sample Input 3

1010\n
\n
\n
\n
\n
\n

Sample Output 3

1 2\n1 3\n1 4\n
\n

Removing any edge will result in a connected component of size 1 and another of size 3.

\n
\n
", "c_code": "int solution() {\n char s[100005];\n scanf(\"%s\", s);\n int n = 0;\n while (s[n] != '\\0') {\n n++;\n }\n int i;\n int f = 0;\n if (s[0] == '0' || s[n - 1] == '1') {\n f++;\n }\n for (i = 0; i < n - 1; i++) {\n if (s[i] != s[n - i - 2]) {\n f++;\n }\n }\n if (f > 0) {\n printf(\"-1\\n\");\n return 0;\n }\n int v = 2;\n printf(\"1 2\\n\");\n for (i = 2; i < n; i++) {\n printf(\"%d %d\\n\", v, i + 1);\n if (s[i - 1] == '1') {\n v = i + 1;\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 let mut buf_it = buf.split_whitespace();\n\n let S = buf_it.next().unwrap().chars().collect::>();\n let N = S.len();\n\n if S[0] == '0' || S[N - 1] == '1' {\n println!(\"-1\");\n return;\n }\n for i in 0..N - 2 {\n let j = N - 2 - i;\n if S[i] != S[j] {\n println!(\"-1\");\n return;\n }\n }\n let mut j = 1;\n for i in 0..N - 1 {\n if S[i] == '1' {\n println!(\"{} {}\", j, i + 2);\n j = i + 2;\n } else {\n println!(\"{} {}\", j, i + 2);\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1691", "problem_description": "$$$m$$$ chairs are arranged in a circle sequentially. The chairs are numbered from $$$0$$$ to $$$m-1$$$. $$$n$$$ people want to sit in these chairs. The $$$i$$$-th of them wants at least $$$a[i]$$$ empty chairs both on his right and left side. More formally, if the $$$i$$$-th person sits in the $$$j$$$-th chair, then no one else should sit in the following chairs: $$$(j-a[i]) \\bmod m$$$, $$$(j-a[i]+1) \\bmod m$$$, ... $$$(j+a[i]-1) \\bmod m$$$, $$$(j+a[i]) \\bmod m$$$.Decide if it is possible to sit down for all of them, under the given limitations.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long n;\n long long m;\n long long sum = 0;\n scanf(\"%lld %lld\", &n, &m);\n\n long long a[n];\n long long min = 10000000000;\n long long max = 0;\n\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n sum += a[i];\n\n if (a[i] > max) {\n max = a[i];\n }\n\n if (a[i] < min) {\n min = a[i];\n }\n }\n\n sum = sum + max - min + n;\n\n if (n > m || sum > m) {\n printf(\"NO\\n\");\n\n }\n\n else {\n printf(\"YES\\n\");\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 x: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let _n = x[0];\n let m = x[1];\n let a: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let hi = a.iter().max().unwrap();\n let lo = a.iter().min().unwrap();\n let total = a.len() as u64 + a.iter().sum::() + hi - lo;\n println!(\"{}\", if total <= m { \"YES\" } else { \"NO\" });\n }\n}", "difficulty": "easy"} {"problem_id": "1692", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.

\n

Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.

\n

Ultimately, how many slimes will be there?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • |S| = N
  • \n
  • S consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nS\n
\n
\n
\n
\n
\n

Output

Print the final number of slimes.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10\naabbbbaaca\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

Ultimately, these slimes will fuse into abaca.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\naaaaa\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

All the slimes will fuse into one.

\n
\n
\n
\n
\n
\n

Sample Input 3

20\nxxzaffeeeeddfkkkkllq\n
\n
\n
\n
\n
\n

Sample Output 3

10\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n scanf(\"%d\", &N);\n\n char s[100001];\n scanf(\"%s\", s);\n\n int ans = 1;\n for (int i = 1; i < N; i++) {\n if (s[i] != s[i - 1]) {\n ans++;\n }\n }\n printf(\"%d\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut first_line = String::new();\n std::io::stdin().read_line(&mut first_line).unwrap();\n let _n: usize = first_line.trim().parse().unwrap();\n let mut second_line = String::new();\n std::io::stdin().read_line(&mut second_line).unwrap();\n let s = second_line.trim();\n\n let mut queue = VecDeque::::new();\n for slime in s.chars() {\n match queue.pop_back() {\n None => queue.push_back(slime),\n Some(back) => {\n queue.push_back(back);\n if slime != back {\n queue.push_back(slime);\n }\n }\n }\n }\n print!(\"{}\", queue.len())\n}", "difficulty": "medium"} {"problem_id": "1693", "problem_description": "Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers $$$n$$$ pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy.The pieces of sushi are of two types: either with tuna or with eel. Let's denote the type of the $$$i$$$-th from the left sushi as $$$t_i$$$, where $$$t_i = 1$$$ means it is with tuna, and $$$t_i = 2$$$ means it is with eel.Arkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment $$$[2, 2, 2, 1, 1, 1]$$$ is valid, but subsegment $$$[1, 2, 1, 2, 1, 2]$$$ is not, because both halves contain both types of sushi.Find the length of the longest continuous subsegment of sushi Arkady can buy.", "c_code": "int solution() {\n\n int num_sushis = 0;\n int sushi[100000];\n int num_ones = 0;\n int num_twos = 0;\n int current;\n int max = 0;\n scanf(\"%d\", &num_sushis);\n for (int i = 0; i < num_sushis; i++) {\n scanf(\"%d\", &sushi[i]);\n }\n current = sushi[0];\n for (int i = 0; i < num_sushis; i++) {\n if (sushi[i] == 1) {\n if (current == 2) {\n num_ones = 0;\n }\n num_ones++;\n }\n if (sushi[i] == 2) {\n if (current == 1) {\n num_twos = 0;\n }\n num_twos++;\n }\n\n if (num_ones >= num_twos && num_twos != 0) {\n if (num_twos * 2 > max) {\n max = num_twos * 2;\n }\n }\n if (num_twos >= num_ones && num_ones != 0) {\n if (num_ones * 2 > max) {\n max = num_ones * 2;\n }\n }\n current = sushi[i];\n }\n printf(\"%d\", max);\n}", "rust_code": "fn solution() {\n let stream = io::stdin();\n let line = stream\n .lock()\n .lines()\n .nth(1)\n .expect(\"Iterator is inf\")\n .expect(\"Blah\");\n\n let result = line.trim().split(\" \").filter_map(|x| x.parse::().ok());\n let mut cnts = [0u64; 2];\n let mut prev = 0u8;\n let mut answer = 0u64;\n\n for x in result {\n let counter = x as usize - 1;\n if x == prev {\n cnts[counter] += 1;\n } else {\n answer = answer.max(*cnts.iter().min().expect(\"ads\"));\n prev = x;\n cnts[counter] = 1;\n }\n }\n answer = answer.max(*cnts.iter().min().expect(\"ads\"));\n println!(\"{}\", 2 * answer);\n}", "difficulty": "hard"} {"problem_id": "1694", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.

\n

You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • A, B, and C are all integers between 1 and 9 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

\n

If the given triple is poor, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 7 5\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

A and C are equal, but B is different from those two numbers, so this triple is poor.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 4 4\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

A, B, and C are all equal, so this triple is not poor.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 9 6\n
\n
\n
\n
\n
\n

Sample Output 3

No\n
\n
\n
\n
\n
\n
\n

Sample Input 4

3 3 4\n
\n
\n
\n
\n
\n

Sample Output 4

Yes\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int c = 0;\n\n scanf(\"%d %d %d\", &a, &b, &c);\n\n if (a == b && a != c) {\n printf(\"Yes\");\n }\n if (b == c && a != c) {\n printf(\"Yes\");\n }\n if (a == c && a != b) {\n printf(\"Yes\");\n }\n if (a == c && a == b) {\n printf(\"No\");\n }\n if (a != c && a != b && b != c) {\n printf(\"No\");\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 line.\");\n let x: Vec<&str> = input.split_whitespace().collect();\n if x.len() == 3 {\n let mut count = 0;\n if x[0] == x[1] {\n count += 1;\n }\n if x[0] == x[2] {\n count += 1;\n }\n if x[2] == x[1] {\n count += 1;\n }\n if count == 1 {\n println!(\"Yes\");\n } else {\n println!(\"No\")\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1695", "problem_description": "You got a box with a combination lock. The lock has a display showing n digits. There are two buttons on the box, each button changes digits on the display. You have quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the display one position to the right (the last digit becomes the first one). For example, if the display is currently showing number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068.You know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number.", "c_code": "int solution() {\n int n;\n int i;\n int j;\n int arr[2000];\n\n scanf(\"%d\", &n);\n char s[1003];\n scanf(\"%s\", s);\n for (i = 0; i < n; i++) {\n arr[i] = s[i] - '0';\n }\n int start = 0;\n int min[1003];\n for (i = 0; i < n; i++) {\n min[i] = 10;\n }\n int curr[1003];\n for (i = 0; i < n; i++) {\n start = i;\n int jump = 10 - arr[i];\n for (j = 0; j < n; j++) {\n curr[j] = (arr[(j + start) % n] + jump) % 10;\n }\n j = 0;\n while (j < n && (min[j] == curr[j])) {\n j++;\n }\n if (j < n && min[j] > curr[j]) {\n int k;\n for (k = 0; k < n; k++) {\n min[k] = curr[k];\n }\n }\n }\n for (i = 0; i < n; i++) {\n printf(\"%d\", min[i]);\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\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 line = String::new();\n stdin().read_line(&mut line).unwrap();\n let line: Vec = line.trim().as_bytes().iter().map(|&it| it - b'0').collect();\n\n let mut answ = line.to_owned();\n for i in 0..10 {\n for shift in 0..n {\n let variant: Vec = line\n .iter()\n .skip(shift)\n .chain(line.iter().take(shift))\n .map(|&it| (it + i) % 10)\n .collect();\n\n for i in 0..n {\n if variant[i] == answ[i] {\n continue;\n } else if variant[i] < answ[i] {\n answ = variant;\n break;\n } else {\n break;\n }\n }\n }\n }\n answ.iter().for_each(|it| print!(\"{}\", it));\n println!()\n}", "difficulty": "medium"} {"problem_id": "1696", "problem_description": "You are given a matrix with $$$n$$$ rows (numbered from $$$1$$$ to $$$n$$$) and $$$m$$$ columns (numbered from $$$1$$$ to $$$m$$$). A number $$$a_{i, j}$$$ is written in the cell belonging to the $$$i$$$-th row and the $$$j$$$-th column, each number is either $$$0$$$ or $$$1$$$.A chip is initially in the cell $$$(1, 1)$$$, and it will be moved to the cell $$$(n, m)$$$. During each move, it either moves to the next cell in the current row, or in the current column (if the current cell is $$$(x, y)$$$, then after the move it can be either $$$(x + 1, y)$$$ or $$$(x, y + 1)$$$). The chip cannot leave the matrix.Consider each path of the chip from $$$(1, 1)$$$ to $$$(n, m)$$$. A path is called palindromic if the number in the first cell is equal to the number in the last cell, the number in the second cell is equal to the number in the second-to-last cell, and so on.Your goal is to change the values in the minimum number of cells so that every path is palindromic.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n while (n--) {\n int m;\n int n;\n scanf(\"%d %d\", &m, &n);\n int matrix[m + 1][n + 1];\n for (int l = 1; l <= m; l++) {\n for (int r = 1; r <= n; r++) {\n scanf(\"%d\", &matrix[l][r]);\n }\n }\n int min = 0;\n if (matrix[1][1] != matrix[m][n]) {\n min++;\n }\n for (int k = 1; k <= (m + n - 2) / 2; k++) {\n if ((m + n - 2) % 2 == 0 && k == (m + n - 2) / 2) {\n break;\n }\n int n11 = 0;\n int n10 = 0;\n int n21 = 0;\n int n20 = 0;\n for (int i = 1, j = k + 1; i <= m && j > 0; i++, j = k - i + 2) {\n if (j > n) {\n continue;\n }\n if (matrix[i][j] == 1) {\n n11++;\n } else {\n n10++;\n }\n }\n for (int i = 1, j = n + m - 2 - k + 1; i <= m && j > 0;\n i++, j = n + m - 2 - k - i + 2) {\n if (j > n) {\n continue;\n }\n if (matrix[i][j] == 1) {\n n21++;\n } else {\n n20++;\n }\n }\n if (n10 + n20 <= n11 + n21) {\n min += n10 + n20;\n } else {\n min += n11 + n21;\n }\n }\n printf(\"%d\\n\", min);\n }\n return 0;\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 t: usize = itr.next().unwrap().parse().unwrap();\n let mut out = Vec::new();\n for _ in 0..t {\n let h: usize = itr.next().unwrap().parse().unwrap();\n let w: usize = itr.next().unwrap().parse().unwrap();\n let g: Vec> = (0..h)\n .map(|_| {\n (0..w)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect()\n })\n .collect();\n let mut sum = vec![(0, 0); h + w];\n for i in 0..h {\n for j in 0..w {\n if g[i][j] == 0 {\n sum[i + j].0 += 1;\n } else {\n sum[i + j].1 += 1;\n }\n }\n }\n let last = h + w - 2;\n let mut ans = 0;\n\n for i in 0..last / 2 {\n let zero = sum[i].0 + sum[last - i].0;\n let one = sum[i].1 + sum[last - i].1;\n ans += min(zero, one);\n }\n if (h + w) % 2 == 1 {\n let i = last / 2;\n let j = last / 2 + 1;\n let zero = sum[i].0 + sum[j].0;\n let one = sum[i].1 + sum[j].1;\n ans += min(zero, one);\n }\n writeln!(out, \"{}\", ans).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "hard"} {"problem_id": "1697", "problem_description": "Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti. As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.It's time for Susie to go to bed, help her find such string p or state that it is impossible.", "c_code": "int solution() {\n char S[100001];\n char T[100001];\n int i;\n int N = 0;\n scanf(\"%s %s\", S, T);\n for (i = 0; S[i] != '\\0'; ++i) {\n if (S[i] != T[i]) {\n ++N;\n }\n }\n if (N & 1) {\n puts(\"impossible\");\n return 0;\n }\n N /= 2;\n for (i = 0; S[i] != '\\0'; ++i) {\n if (S[i] != T[i]) {\n putchar(N ? S[i] : T[i]);\n if (N) {\n --N;\n }\n } else {\n putchar(S[i]);\n }\n }\n putchar('\\n');\n return 0;\n}", "rust_code": "fn solution() {\n let inputstatus = 1;\n\n let mut buf = String::new();\n let filename = \"inputrust.txt\";\n\n if inputstatus == 0 {\n let mut f = File::open(filename).expect(\"file not found\");\n f.read_to_string(&mut buf)\n .expect(\"something went wrong reading the file\");\n } else {\n std::io::stdin().read_to_string(&mut buf).unwrap();\n }\n\n let mut iter = buf.split_whitespace();\n let s: Vec = iter.next().unwrap().chars().collect();\n let t: Vec = iter.next().unwrap().chars().collect();\n let mut ans = String::new();\n let n: usize = t.len();\n let mut a = 0;\n\n for i in 0..n {\n if s[i] == t[i] {\n ans.push(s[i]);\n } else {\n if a == 0 {\n ans.push(s[i]);\n } else {\n ans.push(t[i]);\n }\n a ^= 1;\n }\n }\n if a == 0 {\n println!(\"{}\", ans);\n } else {\n println!(\"impossible\");\n }\n}", "difficulty": "easy"} {"problem_id": "1698", "problem_description": "Given an array $$$a$$$ of length $$$n$$$, you can do at most $$$k$$$ operations of the following type on it: choose $$$2$$$ different elements in the array, add $$$1$$$ to the first, and subtract $$$1$$$ from the second. However, all the elements of $$$a$$$ have to remain non-negative after this operation. What is lexicographically the smallest array you can obtain?An array $$$x$$$ is lexicographically smaller than an array $$$y$$$ if there exists an index $$$i$$$ such that $$$x_i<y_i$$$, and $$$x_j=y_j$$$ for all $$$1 \\le j < i$$$. Less formally, at the first index $$$i$$$ in which they differ, $$$x_i<y_i$$$.", "c_code": "int solution() {\n\n int t = 0;\n scanf(\"%d\", &t);\n\n int n = 0;\n int k = 0;\n int index = 0;\n int a[101] = {0};\n\n while (t-- > 0) {\n index = 0;\n\n scanf(\"%d %d\", &n, &k);\n\n for (int i = 0; i < n; i++) {\n\n scanf(\"%d\", &a[i]);\n }\n\n for (int i = 0; i < k; i++) {\n\n while (a[index] <= 0) {\n index++;\n if (index == (n - 1)) {\n break;\n }\n }\n if (index == (n - 1)) {\n break;\n }\n\n a[index]--;\n\n a[n - 1]++;\n }\n\n for (int i = 0; i < n; i++) {\n printf(\"%d \", a[i]);\n }\n\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n io::stdin().read_line(&mut input).unwrap();\n\n let tt: u32 = input.trim().parse().unwrap();\n\n for _ in 0..tt {\n input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let v: Vec<&str> = input.trim().split(\" \").collect();\n let n: usize = v[0].parse().unwrap();\n let k: usize = v[1].parse().unwrap();\n\n input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let v: Vec<&str> = input.trim().split(\" \").collect();\n let mut a = Vec::::with_capacity(n);\n for i in 0..n {\n a.push(v[i].parse::().unwrap());\n }\n let mut f: usize = 0;\n for _ in 0..k {\n while f < n - 1 && a[f] == 0 {\n f += 1;\n }\n if f == n - 1 {\n break;\n }\n a[f] -= 1;\n a[n - 1] += 1\n }\n for i in 0..n {\n if i == 0 {\n print!(\"{}\", a[i]);\n } else {\n print!(\" {}\", a[i]);\n }\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1699", "problem_description": "Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on.During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.It is known that the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off.", "c_code": "int solution() {\n long long int k;\n long long int d;\n long long int t;\n scanf(\"%lld %lld %lld\", &k, &d, &t);\n if (k % d == 0 || t <= k) {\n printf(\"%lf\", (double)t);\n } else {\n long long int x;\n if (d >= k) {\n x = d;\n } else {\n long long int c = k / d;\n x = (c + 1) * d;\n }\n\n t *= 2;\n\n long long int p = (2 * k) + (x - k);\n\n long long int a = t / p;\n\n long long int b = t - (a * p);\n\n double T = (double)a * x;\n\n if (b <= 2 * k) {\n T = T + (double)b / 2;\n } else {\n T = T + k;\n T = T + ((double)b - 2 * k);\n }\n printf(\"%lf\", T);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n use std::io;\n use std::io::prelude::*;\n io::stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input.split_whitespace();\n\n let k: u64 = it.next().unwrap().parse().unwrap();\n let d: u64 = it.next().unwrap().parse().unwrap();\n let t: u64 = it.next().unwrap().parse().unwrap();\n\n let c = (k - 1) / d + 1;\n let cl = d * c;\n\n let pc = cl + k;\n\n let cc = (2 * t) / pc;\n\n let rh = (2 * t) % pc;\n\n let rt = if rh < 2 * k {\n rh as f64 / 2.0\n } else {\n (rh - k) as f64\n };\n\n let ans = (cl * cc) as f64 + rt;\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1700", "problem_description": "In the pet store on sale there are: $$$a$$$ packs of dog food; $$$b$$$ packs of cat food; $$$c$$$ packs of universal food (such food is suitable for both dogs and cats). Polycarp has $$$x$$$ dogs and $$$y$$$ cats. Is it possible that he will be able to buy food for all his animals in the store? Each of his dogs and each of his cats should receive one pack of suitable food for it.", "c_code": "int solution(int argc, char const *argv[]) {\n int t = 0;\n scanf(\"%d\", &t);\n\n unsigned long a = 0;\n unsigned long b = 0;\n unsigned long c = 0;\n unsigned long x = 0;\n unsigned long y = 0;\n\n unsigned long common_pets = 0;\n\n unsigned long results[10000];\n\n for (int i = 0; i < t; i++) {\n a = b = c = x = y = 0;\n common_pets = 0;\n\n scanf(\"%lu %lu %lu %lu %lu\", &a, &b, &c, &x, &y);\n\n if (x > 0 && a > 0) {\n if (x > a) {\n x = x - a;\n } else {\n x = 0;\n }\n }\n\n if (y > 0 && b > 0) {\n if (y > b) {\n y = y - b;\n } else {\n y = 0;\n }\n }\n\n if (x > 0 || y > 0) {\n common_pets = x + y;\n\n if (common_pets > 0 && c > 0) {\n if (c >= common_pets) {\n common_pets = 0;\n } else {\n common_pets += 1;\n }\n }\n }\n\n if (common_pets == 0) {\n\n results[i] = 1;\n } else {\n results[i] = 0;\n }\n }\n\n for (int m = 0; m < t; m++) {\n if (results[m] == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n stdin().read_line(&mut t);\n let t = t.trim().parse::().unwrap();\n\n for _ in 0..t {\n let mut buf = String::new();\n stdin().read_line(&mut buf);\n\n let mut v = Vec::with_capacity(5);\n for i in buf.split_whitespace() {\n v.push(i.parse::().unwrap())\n }\n\n v[3] -= v[0];\n if v[3] < 0 {\n v[3] = 0\n }\n v[4] -= v[1];\n if v[4] < 0 {\n v[4] = 0\n }\n v[2] -= v[3] + v[4];\n\n if v[2] < 0 {\n println!(\"no\");\n } else {\n println!(\"yes\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1701", "problem_description": "The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game.", "c_code": "int solution() {\n int t;\n\n scanf(\"%d\", &t);\n\n int n[t];\n int m[t];\n int k[t];\n int i;\n\n for (i = 0; i < t; i++) {\n scanf(\"%d %d %d\", &n[i], &m[i], &k[i]);\n }\n for (i = 0; i < t; i++) {\n int j;\n int d;\n int l;\n\n d = n[i] / k[i];\n l = (m[i] - d) % k[i];\n\n if (m[i] == 0) {\n j = 0;\n } else if (m[i] < d) {\n j = m[i];\n } else if (m[i] == d) {\n j = m[i];\n } else if (m[i] > d) {\n if ((m[i] - d) <= (k[i] - 1)) {\n j = d - 1;\n } else {\n if ((m[i] - d) % (k[i] - 1) == 0) {\n j = d - (m[i] - d) / (k[i] - 1);\n } else {\n j = d - (m[i] - d) / (k[i] - 1) - 1;\n }\n }\n }\n printf(\"%d\\n\", j);\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 m: usize = itr.next().unwrap().parse().unwrap();\n let k: usize = itr.next().unwrap().parse().unwrap();\n\n let one = n / k;\n let max_joker = std::cmp::min(one, m);\n let rem_joker = (m - max_joker + k - 2) / (k - 1);\n writeln!(out, \"{}\", max_joker - rem_joker).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "easy"} {"problem_id": "1702", "problem_description": "YouKn0wWho has an integer sequence $$$a_1, a_2, \\ldots, a_n$$$. He will perform the following operation until the sequence becomes empty: select an index $$$i$$$ such that $$$1 \\le i \\le |a|$$$ and $$$a_i$$$ is not divisible by $$$(i + 1)$$$, and erase this element from the sequence. Here $$$|a|$$$ is the length of sequence $$$a$$$ at the moment of operation. Note that the sequence $$$a$$$ changes and the next operation is performed on this changed sequence.For example, if $$$a=[3,5,4,5]$$$, then he can select $$$i = 2$$$, because $$$a_2 = 5$$$ is not divisible by $$$i+1 = 3$$$. After this operation the sequence is $$$[3,4,5]$$$.Help YouKn0wWho determine if it is possible to erase the whole sequence using the aforementioned operation.", "c_code": "int solution() {\n int testcase;\n scanf(\"%d\", &testcase);\n\n while (testcase--) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n int ok = 1;\n for (int i = 0; i < n; i++) {\n int flag = 0;\n for (int j = 2; j <= i + 2; j++) {\n if (a[i] % j != 0) {\n flag = 1;\n break;\n }\n }\n ok &= flag;\n }\n\n if (ok) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() -> std::io::Result<()> {\n let mut input = String::new();\n stdin().read_to_string(&mut input)?;\n\n let mut input_iter = input.split_whitespace();\n let mut next_int = || input_iter.next().unwrap().parse::().unwrap();\n\n 'task: for _ in 0..next_int() {\n let n = next_int() as usize;\n\n let mut arr = vec![];\n arr.resize_with(n, &mut next_int);\n\n 'outer: for i in 0..n {\n for j in (0..=i).rev() {\n if arr[i] % (j + 2) as i64 != 0 {\n continue 'outer;\n }\n }\n\n println!(\"NO\");\n continue 'task;\n }\n\n println!(\"YES\");\n }\n\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "1703", "problem_description": "

Doubly Linked List

\n\n

\nYour task is to implement a double linked list.\n

\n\n

\nWrite a program which performs the following operations:\n

\n
    \n
  • insert x: insert an element with key x into the front of the list.
  • \n
  • delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.
  • \n
  • deleteFirst: delete the first element from the list.
  • \n
  • deleteLast: delete the last element from the list.
  • \n
\n\n\n

Input

\n\n

\nThe input is given in the following format:\n

\n\n

\nn
\ncommand1
\ncommand2
\n...
\ncommandn
\n

\n\n\n

\nIn the first line, the number of operations n is given. In the following n lines, the above mentioned operations are given in the following format:\n

\n\n
    \n
  • insert x
  • \n
  • delete x
  • \n
  • deleteFirst
  • \n
  • deleteLast
  • \n
\n\n\n

Output

\n\n

\nPrint all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.\n

\n\n

Constraints

\n\n
    \n
  • The number of operations ≤ 2,000,000
  • \n
  • The number of delete operations ≤ 20
  • \n
  • 0 ≤ value of a key ≤ 109
  • \n
  • The number of elements in the list does not exceed 106
  • \n
  • For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.
  • \n
\n\n

Sample Input 1

\n
\n7\ninsert 5\ninsert 2\ninsert 3\ninsert 1\ndelete 3\ninsert 6\ndelete 5\n
\n

Sample Output 1

\n
\n6 1 2\n
\n\n

Sample Input 2

\n
\n9\ninsert 5\ninsert 2\ninsert 3\ninsert 1\ndelete 3\ninsert 6\ndelete 5\ndeleteFirst\ndeleteLast\n
\n

Sample Output 2

\n
\n1\n
\n\n\n", "c_code": "int solution() {\n int i;\n int j;\n int a;\n int num;\n int y = 0;\n int l;\n int h;\n int key;\n\n scanf(\"%d\", &a);\n\n int list[a];\n char input[12];\n\n for (i = 0; i < a; i++) {\n scanf(\"%s %d\", input, &num);\n\n if (input[0] == 'i') {\n list[i] = num;\n }\n\n else if (input[6] == 'F') {\n i -= 2;\n a -= 2;\n }\n\n else if (input[6] == 'L') {\n i--;\n a--;\n y++;\n }\n\n else {\n i--;\n a--;\n l = 0;\n for (j = i; j >= 0; j--) {\n if (l > 0) {\n break;\n }\n if (num == list[j]) {\n key = 0;\n h = i - j;\n l++;\n\n while (h != key++) {\n list[j + key - 1] = list[j + 1 + key - 1];\n }\n i--;\n a--;\n }\n }\n }\n }\n\n for (i = 0; i < a - y; i++) {\n if (i == a - y - 1) {\n printf(\"%d\\n\", list[a - 1 - i]);\n } else {\n printf(\"%d \", list[a - 1 - i]);\n }\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).ok();\n let n: usize = buf.trim().parse().unwrap_or(0);\n\n let mut dll = Vec::new();\n\n let mut head = 0;\n\n for _ in 0..n {\n let mut buf = String::new();\n stdin.read_line(&mut buf).ok();\n let vec: Vec<&str> = buf.split_whitespace().collect();\n\n if vec[0] == \"insert\" {\n dll.push(vec[1].parse().unwrap_or(0));\n } else if vec[0] == \"delete\" {\n for j in (head..dll.len()).rev() {\n if vec[1].parse().unwrap_or(0) == dll[j] {\n dll.remove(j);\n break;\n }\n }\n } else if vec[0] == \"deleteFirst\" {\n dll.pop();\n } else if vec[0] == \"deleteLast\" {\n head += 1;\n }\n }\n for i in (head..dll.len()).rev() {\n print!(\"{}\", dll[i]);\n if i == head {\n break;\n }\n print!(\" \");\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1704", "problem_description": "Andre has very specific tastes. Recently he started falling in love with arrays.Andre calls an nonempty array $$$b$$$ good, if sum of its elements is divisible by the length of this array. For example, array $$$[2, 3, 1]$$$ is good, as sum of its elements — $$$6$$$ — is divisible by $$$3$$$, but array $$$[1, 1, 2, 3]$$$ isn't good, as $$$7$$$ isn't divisible by $$$4$$$. Andre calls an array $$$a$$$ of length $$$n$$$ perfect if the following conditions hold: Every nonempty subarray of this array is good. For every $$$i$$$ ($$$1 \\le i \\le n$$$), $$$1 \\leq a_i \\leq 100$$$. Given a positive integer $$$n$$$, output any perfect array of length $$$n$$$. We can show that for the given constraints such an array always exists.An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.", "c_code": "int solution() {\n int n = 0;\n\n scanf(\"%d\", &n);\n\n int test[100];\n\n int temp = 0;\n\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &temp);\n test[i] = temp;\n }\n\n int arr[200];\n\n for (int i = 0; i < 200; i++) {\n arr[i] = 1;\n }\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < test[i]; j++) {\n printf(\"%d \", arr[i]);\n }\n }\n printf(\"\\n\");\n}", "rust_code": "fn solution() {\n let mut inp = String::new();\n stdin().read_line(&mut inp).unwrap();\n let t: u16 = inp.trim().parse().unwrap();\n inp.clear();\n for _ in 0..t {\n stdin().read_line(&mut inp).unwrap();\n let n: u16 = inp.trim().parse().unwrap();\n for _ in 1..n {\n print!(\"3 \");\n }\n println!(\"3\");\n inp.clear();\n }\n}", "difficulty": "hard"} {"problem_id": "1705", "problem_description": "Alice and Bob received $$$n$$$ candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice's candies is equal to the total weight of Bob's candies.Check if they can do that.Note that candies are not allowed to be cut in half.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n while (T--) {\n int arr[100];\n int n;\n scanf(\"%d\", &n);\n int num = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] == 2) {\n num++;\n }\n }\n if ((n % 2 && num % 2 && n != num) || (n % 2 == 0 && num % 2 == 0)) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n\n io::stdin().read_line(&mut t).unwrap();\n\n for _ in 0..t.trim().parse::().unwrap() {\n let mut n = String::new();\n\n io::stdin().read_line(&mut n).unwrap();\n\n let mut candies = String::new();\n\n io::stdin().read_line(&mut candies).unwrap();\n\n candies = candies.trim().to_string();\n\n let ones = candies.split(\" \").filter(|&x| x == \"1\").count();\n let twos = candies.split(\" \").collect::>().len() - ones;\n\n if ones % 2 == 1 || (ones == 0 && twos % 2 == 1) {\n println!(\"NO\");\n continue;\n }\n\n println!(\"YES\");\n }\n}", "difficulty": "medium"} {"problem_id": "1706", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

There is a sequence X of length N, where every element is initially 0. Let X_i denote the i-th element of X.

\n

You are given a sequence A of length N. The i-th element of A is A_i. Determine if we can make X equal to A by repeating the operation below. If we can, find the minimum number of operations required.

\n
    \n
  • Choose an integer i such that 1\\leq i\\leq N-1. Replace the value of X_{i+1} with the value of X_i plus 1.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • 0 \\leq A_i \\leq 10^9(1\\leq i\\leq N)
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1\n:\nA_N\n
\n
\n
\n
\n
\n

Output

If we can make X equal to A by repeating the operation, print the minimum number of operations required. If we cannot, print -1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n0\n1\n1\n2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

We can make X equal to A as follows:

\n
    \n
  • Choose i=2. X becomes (0,0,1,0).
  • \n
  • Choose i=1. X becomes (0,1,1,0).
  • \n
  • Choose i=3. X becomes (0,1,1,2).
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3\n1\n2\n1\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n
\n
\n
\n
\n
\n

Sample Input 3

9\n0\n1\n1\n0\n1\n2\n2\n1\n2\n
\n
\n
\n
\n
\n

Sample Output 3

8\n
\n
\n
", "c_code": "int solution(void) {\n long long n;\n long long ans = 0;\n scanf(\"%lld\", &n);\n long long a[n + 1];\n a[n] = 0;\n for (long long i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n if (n == 1) {\n if (a[0] == 0) {\n printf(\"0\\n\");\n } else {\n printf(\"-1\\n\");\n }\n return 0;\n }\n for (long long i = 1; i < n; i++) {\n if (a[i] > a[i - 1] + 1 || a[i] > i || a[0] != 0) {\n printf(\"-1\\n\");\n return 0;\n }\n if (a[i] >= a[i + 1]) {\n ans += a[i];\n }\n }\n printf(\"%lld\\n\", ans);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n let n = buf.trim().parse::().unwrap();\n let mut a: Vec = BufReader::new(stdin())\n .lines()\n .map(|l| l.unwrap().trim().parse::().unwrap())\n .collect();\n for (i, &n) in a.iter().enumerate() {\n if n > i {\n println!(\"-1\");\n return;\n }\n }\n a.reverse();\n let mut tmp = n + 1;\n let mut total = 0;\n for i in a {\n if tmp < n + 1 && i + 1 < tmp {\n println!(\"-1\");\n return;\n }\n if i + 1 != tmp {\n total += i;\n }\n tmp = i;\n }\n println!(\"{}\", total);\n}", "difficulty": "easy"} {"problem_id": "1707", "problem_description": "This is an interactive problem.We hid from you a permutation $$$p$$$ of length $$$n$$$, consisting of the elements from $$$1$$$ to $$$n$$$. You want to guess it. To do that, you can give us 2 different indices $$$i$$$ and $$$j$$$, and we will reply with $$$p_{i} \\bmod p_{j}$$$ (remainder of division $$$p_{i}$$$ by $$$p_{j}$$$).We have enough patience to answer at most $$$2 \\cdot n$$$ queries, so you should fit in this constraint. Can you do it?As a reminder, a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int p[10004];\n int x;\n int y;\n int res[2];\n int i;\n int j;\n for (i = 0; i < n; i++) {\n p[i] = -1;\n }\n i = 0;\n for (int a = 0; a < n - 1; a++) {\n j = 0;\n while (p[j] > 0 || j == i) {\n j++;\n }\n printf(\"? %d %d\\n\", i + 1, j + 1);\n fflush(stdout);\n scanf(\"%d\", &res[0]);\n printf(\"? %d %d\\n\", j + 1, i + 1);\n fflush(stdout);\n scanf(\"%d\", &res[1]);\n if (res[0] < res[1]) {\n p[j] = res[1];\n } else {\n p[i] = res[0];\n while (p[i] > 0) {\n i++;\n }\n }\n }\n for (i = 0; i < n; i++) {\n if (p[i] < 0) {\n p[i] = n;\n }\n }\n printf(\"!\");\n for (i = 0; i < n; i++) {\n printf(\" %d\", p[i]);\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\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 ans = vec![0; n + 1];\n let mut unk_idx = 1;\n for i in 2..=n {\n println!(\"? {} {}\", unk_idx, i);\n let k1: usize = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().parse().unwrap()\n };\n println!(\"? {} {}\", i, unk_idx);\n let k2: 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 if k1 < k2 {\n ans[i] = k2;\n } else {\n ans[unk_idx] = k1;\n unk_idx = i;\n }\n }\n ans[unk_idx] = n;\n println!(\n \"! {}\",\n ans.iter()\n .skip(1)\n .map(|val| val.to_string())\n .collect::>()\n .join(\" \")\n );\n}", "difficulty": "medium"} {"problem_id": "1708", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given positive integers X and Y.\nIf there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.\nIf it does not exist, print -1.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ X,Y ≤ 10^9
  • \n
  • X and Y are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
X Y\n
\n
\n
\n
\n
\n

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8 6\n
\n
\n
\n
\n
\n

Sample Output 1

16\n
\n

For example, 16 is a multiple of 8 but not a multiple of 6.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

A multiple of 3 is a multiple of 3.

\n
\n
", "c_code": "int solution() {\n int a;\n int b;\n int x;\n scanf(\"%d %d\", &a, &b);\n x = -1;\n if (b != 0 && a % b != 0) {\n x = a;\n }\n printf(\"%d\\n\", x);\n return 0;\n}", "rust_code": "fn solution() {\n let base: u64 = 10;\n let scan = stdin();\n let mut line = String::new();\n\n let _ = scan.read_line(&mut line);\n\n let mut iter = line.split_whitespace();\n let x: u64 = iter\n .next()\n .expect(\"Iteration Error?\")\n .trim()\n .parse()\n .expect(\"Number pls...\");\n let y: u64 = iter\n .next()\n .expect(\"Iteration Error?\")\n .trim()\n .parse()\n .expect(\"Number pls...\");\n\n if x == y {\n println!(\"{}\", -1);\n return;\n }\n\n let mut i: u64 = 1;\n loop {\n let mul: u64 = x * i;\n i += 1;\n\n if mul >= base.pow(18) {\n println!(\"{}\", -1);\n return;\n }\n\n if i > 3 {\n println!(\"{}\", -1);\n return;\n }\n\n if !mul.is_multiple_of(y) {\n println!(\"{}\", mul);\n return;\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1709", "problem_description": "Little Vasya has received a young builder’s kit. The kit consists of several wooden bars, the lengths of all of them are known. The bars can be put one on the top of the other if their lengths are the same.Vasya wants to construct the minimal number of towers from the bars. Help Vasya to use the bars in the best way possible.", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n int nr = n;\n int size = 1;\n int tow[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &tow[i]);\n }\n\n int dub[1001] = {0};\n for (int i = 0; i < n; i++) {\n if (dub[i] == 1) {\n continue;\n }\n int sz = 1;\n for (int j = i + 1; j < n; j++) {\n\n if (tow[i] == tow[j]) {\n sz++;\n nr--;\n dub[j] = 1;\n }\n }\n if (sz > size) {\n size = sz;\n }\n }\n printf(\"%d %d\", size, nr);\n}", "rust_code": "fn solution() {\n let mut s0 = String::new();\n std::io::stdin().read_line(&mut s0).unwrap();\n let n: usize = s0.trim_end().parse().unwrap();\n\n let mut s1 = String::new();\n std::io::stdin().read_line(&mut s1).unwrap();\n let mut v: Vec = s1\n .trim_end()\n .to_string()\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n v.sort();\n\n let mut max_len: usize = 0;\n let mut h = 1;\n\n let mut p: usize = 0;\n while p < n {\n let mut q: usize = p + 1;\n while q < n {\n if v[p] != v[q] {\n h += 1;\n break;\n }\n\n q += 1;\n }\n\n if max_len < q - p {\n max_len = q - p;\n }\n\n p = q;\n }\n\n println!(\"{} {}\", max_len, h);\n}", "difficulty": "hard"} {"problem_id": "1710", "problem_description": "As their story unravels, a timeless tale is told once again...Shirahime, a friend of Mocha's, is keen on playing the music game Arcaea and sharing Mocha interesting puzzles to solve. This day, Shirahime comes up with a new simple puzzle and wants Mocha to solve them. However, these puzzles are too easy for Mocha to solve, so she wants you to solve them and tell her the answers. The puzzles are described as follow.There are $$$n$$$ squares arranged in a row, and each of them can be painted either red or blue.Among these squares, some of them have been painted already, and the others are blank. You can decide which color to paint on each blank square.Some pairs of adjacent squares may have the same color, which is imperfect. We define the imperfectness as the number of pairs of adjacent squares that share the same color.For example, the imperfectness of \"BRRRBBR\" is $$$3$$$, with \"BB\" occurred once and \"RR\" occurred twice.Your goal is to minimize the imperfectness and print out the colors of the squares after painting.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 1; i <= t; i++) {\n int n;\n scanf(\"%d\", &n);\n char str[100];\n scanf(\"%s\", str);\n for (int i = 0; i < 100; i++) {\n if (str[i] == '?' && str[i - 1] == 'R') {\n int k = i % 2;\n while (str[i] == '?') {\n if (i % 2 == k) {\n str[i] = 'B';\n } else {\n str[i] = 'R';\n }\n i++;\n }\n } else if (str[i] == '?' && str[i - 1] == 'B') {\n int k = i % 2;\n while (str[i] == '?') {\n if (i % 2 == k) {\n str[i] = 'R';\n } else {\n str[i] = 'B';\n }\n i++;\n }\n }\n }\n for (int i = 99; i >= 0; i--) {\n if (str[i] == '?' && str[i + 1] == 'B') {\n int k = i % 2;\n while (str[i] == '?' && i >= 0) {\n if (i % 2 == k) {\n str[i] = 'R';\n } else {\n str[i] = 'B';\n }\n i--;\n }\n } else if (str[i] == '?' && str[i + 1] == 'R') {\n int k = i % 2;\n while (str[i] == '?' && i >= 0) {\n if (i % 2 == k) {\n str[i] = 'B';\n } else {\n str[i] = 'R';\n }\n i--;\n }\n } else {\n int k = i % 2;\n while (str[i] == '?' && i >= 0) {\n if (i % 2 == k) {\n str[i] = 'B';\n } else {\n str[i] = 'R';\n }\n i--;\n }\n }\n }\n printf(\"%s\\n\", str);\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\n let mut a = lines.next().unwrap().into_bytes();\n\n let (first_span, s) = match a.iter().position(|&b| b != b'?') {\n Some(i) => a.split_at_mut(i),\n None => {\n let mut i = 0;\n while i < n {\n output.write(b\"B\").unwrap();\n i += 1;\n if i == n {\n break;\n }\n output.write(b\"R\").unwrap();\n i += 1;\n }\n writeln!(&mut output).unwrap();\n continue 'cases;\n }\n };\n\n let j = s\n .iter()\n .enumerate()\n .rev()\n .find_map(|(j, &b)| if b != b'?' { Some(j) } else { None })\n .unwrap();\n let (s, last_span) = s.split_at_mut(j + 1);\n\n let mut x = *s.first().unwrap() == b'B';\n for b in first_span.iter_mut().rev() {\n x = !x;\n *b = if x { b'B' } else { b'R' }\n }\n\n let mut x = *s.last().unwrap() == b'B';\n for b in last_span.iter_mut() {\n x = !x;\n *b = if x { b'B' } else { b'R' }\n }\n\n let mut x = *s.first().unwrap() == b'B';\n for b in s.iter_mut().skip(1) {\n match *b {\n b'R' => x = false,\n b'B' => x = true,\n _ => {\n x = !x;\n *b = if x { b'B' } else { b'R' }\n }\n }\n }\n\n output.write_all(a.as_slice()).unwrap();\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "easy"} {"problem_id": "1711", "problem_description": "Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka's behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told — ai during the i-th minute. Otherwise he writes nothing.You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that and will write down all the theorems lecturer tells.You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n int matrix[2][n];\n int beiyong[n];\n memset(matrix, 0, sizeof(int) * 2 * n);\n memset(beiyong, 0, sizeof(int) * n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &matrix[0][i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &matrix[1][i]);\n }\n\n int score = 0;\n for (int j = 0; j < n; j++) {\n if (matrix[1][j] == 1) {\n score += matrix[0][j];\n } else {\n beiyong[j] = matrix[0][j];\n }\n }\n int maxScore = 0;\n int cont = 0;\n for (int l = 0; l < k; l++) {\n cont += beiyong[l];\n }\n maxScore = maxScore > cont ? maxScore : cont;\n for (int j = 0; j < n - k; j++) {\n cont = cont - beiyong[j];\n cont += beiyong[j + k];\n maxScore = maxScore > cont ? maxScore : cont;\n }\n printf(\"%d\", maxScore + score);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input1 = String::new();\n io::stdin().read_line(&mut input1);\n let mut iter1 = input1\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n let n = iter1.next().unwrap();\n let k = iter1.next().unwrap();\n\n let mut input2 = String::new();\n io::stdin().read_line(&mut input2);\n let mut theorem_arr = input2\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n let mut input3 = String::new();\n io::stdin().read_line(&mut input3);\n let mut awake_arr = input3\n .split_whitespace()\n .map(|x| x.parse::().unwrap() == 1);\n\n let _y = n - k + 1;\n\n let mut highest_sum = 0;\n let mut highest_bonus = 0;\n let mut bonus = 0;\n let mut previous_sleep = vec![0; k];\n for i in 0..n {\n let t = theorem_arr.next().unwrap();\n let a = awake_arr.next().unwrap();\n if a {\n highest_sum += t;\n bonus -= previous_sleep[i];\n previous_sleep.push(0);\n } else {\n bonus += t;\n bonus -= previous_sleep[i];\n highest_bonus = max(highest_bonus, bonus);\n previous_sleep.push(t);\n }\n }\n\n println!(\"{}\", highest_sum + highest_bonus);\n}", "difficulty": "hard"} {"problem_id": "1712", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have N points in the two-dimensional plane. The coordinates of the i-th point are (X_i,Y_i).

\n

Among them, we are looking for the points such that the distance from the origin is at most D. How many such points are there?

\n

We remind you that the distance between the origin and the point (p, q) can be represented as \\sqrt{p^2+q^2}.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2\\times 10^5
  • \n
  • 0 \\leq D \\leq 2\\times 10^5
  • \n
  • |X_i|,|Y_i| \\leq 2\\times 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N D\nX_1 Y_1\n\\vdots\nX_N Y_N\n
\n
\n
\n
\n
\n

Output

Print an integer representing the number of points such that the distance from the origin is at most D.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4 5\n0 5\n-2 4\n3 4\n4 -4\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

The distance between the origin and each of the given points is as follows:

\n
    \n
  • \\sqrt{0^2+5^2}=5
  • \n
  • \\sqrt{(-2)^2+4^2}=4.472\\ldots
  • \n
  • \\sqrt{3^2+4^2}=5
  • \n
  • \\sqrt{4^2+(-4)^2}=5.656\\ldots
  • \n
\n

Thus, we have three points such that the distance from the origin is at most 5.

\n
\n
\n
\n
\n
\n

Sample Input 2

12 3\n1 1\n1 1\n1 1\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n

Multiple points may exist at the same coordinates.

\n
\n
\n
\n
\n
\n

Sample Input 3

20 100000\n14309 -32939\n-56855 100340\n151364 25430\n103789 -113141\n147404 -136977\n-37006 -30929\n188810 -49557\n13419 70401\n-88280 165170\n-196399 137941\n-176527 -61904\n46659 115261\n-153551 114185\n98784 -6820\n94111 -86268\n-30401 61477\n-55056 7872\n5901 -163796\n138819 -185986\n-69848 -96669\n
\n
\n
\n
\n
\n

Sample Output 3

6\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n long int d = 0;\n long int a = 0;\n long int b = 0;\n int count = 0;\n\n scanf(\"%d %ld\", &n, &d);\n d *= d;\n\n while (n--) {\n scanf(\"%ld %ld\", &a, &b);\n if (d >= (a * a + b * b)) {\n count++;\n }\n }\n\n printf(\"%d\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let (n, d): (usize, i64) = {\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 mut ans = 0;\n for _ in 0..n {\n let (x, y): (i64, i64) = {\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 if x * x + y * y <= d * d {\n ans += 1;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1713", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are 2000001 stones placed on a number line. The coordinates of these stones are -1000000, -999999, -999998, \\ldots, 999999, 1000000.

\n

Among them, some K consecutive stones are painted black, and the others are painted white.

\n

Additionally, we know that the stone at coordinate X is painted black.

\n

Print all coordinates that potentially contain a stone painted black, in ascending order.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq 100
  • \n
  • 0 \\leq X \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K X\n
\n
\n
\n
\n
\n

Output

Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 7\n
\n
\n
\n
\n
\n

Sample Output 1

5 6 7 8 9\n
\n

We know that there are three stones painted black, and the stone at coordinate 7 is painted black. There are three possible cases:

\n
    \n
  • The three stones painted black are placed at coordinates 5, 6, and 7.
  • \n
  • The three stones painted black are placed at coordinates 6, 7, and 8.
  • \n
  • The three stones painted black are placed at coordinates 7, 8, and 9.
  • \n
\n

Thus, five coordinates potentially contain a stone painted black: 5, 6, 7, 8, and 9.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 0\n
\n
\n
\n
\n
\n

Sample Output 2

-3 -2 -1 0 1 2 3\n
\n

Negative coordinates can also contain a stone painted black.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 100\n
\n
\n
\n
\n
\n

Sample Output 3

100\n
\n
\n
", "c_code": "int solution(void) {\n int K = 0;\n int X = 0;\n int min = 0;\n int max = 0;\n scanf(\"%d %d\", &K, &X);\n min = X - K + 1;\n max = X + K - 1;\n for (int i = min; i <= max; i++) {\n printf(\"%d \", i);\n }\n printf(\"\\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 let mut iter = buf.split_whitespace();\n let k: i32 = iter.next().unwrap().parse().unwrap();\n let x: i32 = iter.next().unwrap().parse().unwrap();\n\n let from = x - k + 1;\n let to = x + k - 1;\n println!(\n \"{}\",\n (from..to + 1)\n .map(|x| x.to_string())\n .collect::>()\n .join(\" \")\n );\n}", "difficulty": "medium"} {"problem_id": "1714", "problem_description": "\n

Balls and Boxes 8

\n\n\n \n \n \n \n \n
BallsBoxesAny wayAt most one ballAt least one ball
DistinguishableDistinguishable123
IndistinguishableDistinguishable456
DistinguishableIndistinguishable789
IndistinguishableIndistinguishable101112
\n\n

Problem

\n\n

You have $n$ balls and $k$ boxes. You want to put these balls into the boxes.

\n

Find the number of ways to put the balls under the following conditions:

\n\n
    \n
  • Each ball is distinguished from the other.
  • \n
  • Each box is not distinguished from the other.
  • \n
  • Each ball can go into only one box and no one remains outside of the boxes.
  • \n
  • Each box can contain at most one ball.
  • \n
\n\n

Note that you must print this count modulo $10^9+7$.

\n\n

Input

\n\n
\n$n$ $k$\n
\n\n

The first line will contain two integers $n$ and $k$.

\n\n

Output

\n\n

Print the number of ways modulo $10^9+7$ in a line.

\n\n

Constraints

\n\n
    \n
  • $1 \\le n \\le 1000$
  • \n
  • $1 \\le k \\le 1000$
  • \n
\n\n

Sample Input 1

\n
\n5 10\n
\n\n

Sample Output 1

\n
\n1\n
\n\n

Sample Input 2

\n\n
\n200 100\n
\n

Sample Output 2

\n
\n0\n
", "c_code": "int solution() {\n int n;\n int k;\n\n scanf(\"%d%d\", &n, &k);\n puts(n <= k ? \"1\" : \"0\");\n return 0;\n}", "rust_code": "fn solution() {\n input!(n:i64,k:i64);\n if n > k {\n println!(\"0\");\n } else {\n println!(\"1\");\n }\n}", "difficulty": "easy"} {"problem_id": "1715", "problem_description": "You are given an array $$$a_1, a_2, \\ldots, a_n$$$ consisting of $$$n$$$ positive integers and a positive integer $$$m$$$.You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want.Let's call an array $$$m$$$-divisible if for each two adjacent numbers in the array (two numbers on the positions $$$i$$$ and $$$i+1$$$ are called adjacent for each $$$i$$$) their sum is divisible by $$$m$$$. An array of one element is $$$m$$$-divisible.Find the smallest number of $$$m$$$-divisible arrays that $$$a_1, a_2, \\ldots, a_n$$$ is possible to divide into.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n int m;\n for (int i = 0; i < t; i++) {\n int count = 0;\n\n scanf(\"%d %d\", &n, &m);\n int arr[n];\n int store[m];\n for (int j = 0; j < m; j++) {\n store[j] = 0;\n }\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &arr[j]);\n store[arr[j] % m]++;\n }\n\n if (store[0] != 0) {\n count = count + 1;\n }\n for (int j = 1; j <= m / 2; j++) {\n\n if (store[j] > store[m - j]) {\n count++;\n store[j] = store[j] - store[m - j] - 1;\n count = count + store[j];\n\n } else if (store[j] < store[m - j]) {\n count++;\n store[m - j] = store[m - j] - store[j] - 1;\n count = count + store[m - j];\n } else if (store[j] == store[m - j]) {\n if (store[j] != 0) {\n count++;\n }\n }\n }\n\n printf(\"%d\\n\", count);\n }\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 v = s\n .split_whitespace()\n .map(|x| x.parse::())\n .collect::, _>>()\n .unwrap();\n let mut _n = v[0];\n let m = v[1];\n let mut g = String::new();\n io::stdin().read_line(&mut g).unwrap();\n let a = g\n .split_whitespace()\n .map(|x| x.parse::())\n .collect::, _>>()\n .unwrap();\n let mut v = vec![0; m as usize];\n for el in a {\n v[(el % m) as usize] += 1;\n }\n let mut ans = 0;\n for i in 0..m {\n let j = (m - i) % m;\n if i > j {\n continue;\n }\n let mut mx = cmp::max(v[i as usize], v[j as usize]);\n let mn = cmp::min(v[i as usize], v[j as usize]);\n if mx == 0 {\n continue;\n }\n mx -= mn + 1;\n ans += 1;\n if mx > 0 {\n ans += mx;\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "hard"} {"problem_id": "1716", "problem_description": "There are $$$n$$$ friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.For each friend the value $$$f_i$$$ is known: it is either $$$f_i = 0$$$ if the $$$i$$$-th friend doesn't know whom he wants to give the gift to or $$$1 \\le f_i \\le n$$$ if the $$$i$$$-th friend wants to give the gift to the friend $$$f_i$$$.You want to fill in the unknown values ($$$f_i = 0$$$) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.If there are several answers, you can print any.", "c_code": "int solution() {\n int i;\n int n;\n scanf(\"%d\", &n);\n int giftGiving[n];\n int giftGetting[n];\n int giftNot[n];\n int top = 0;\n int bottom = 0;\n for (i = 0; i < n; i++) {\n giftGetting[i] = 0;\n }\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &giftGiving[i]);\n giftGetting[giftGiving[i] - 1] = 1;\n }\n for (i = 0; i < n; i++) {\n if (giftGetting[i] != 1) {\n giftNot[bottom++] = i + 1;\n }\n }\n for (i = 0; i < n; i++) {\n if (giftGiving[i] == 0 && giftGetting[i] == 0) {\n if (giftNot[top] == i + 1) {\n giftGiving[i] = giftNot[--bottom];\n } else {\n giftGiving[i] = giftNot[top++];\n }\n }\n }\n for (i = 0; i < n; i++) {\n if (giftGiving[i] == 0) {\n giftGiving[i] = giftNot[top++];\n }\n }\n for (i = 0; i < n; i++) {\n printf(\"%d \", giftGiving[i]);\n }\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 l2 = String::new();\n std::io::stdin().read_line(&mut l2).unwrap();\n let mut f = Vec::with_capacity(n + 1);\n f.push(0usize);\n for it in l2.split_whitespace() {\n f.push(it.parse().unwrap());\n }\n let mut finv = vec![0; n + 1];\n for u in 1..n + 1 {\n if f[u] != 0 {\n finv[f[u]] = u;\n }\n }\n let mut path = vec![];\n let mut vis = vec![false; n + 1];\n for u in 1..n + 1 {\n if vis[u] {\n continue;\n }\n let mut v = u;\n while f[v] != 0 && f[v] != u {\n v = f[v];\n vis[v] = true;\n }\n if f[v] == u {\n continue;\n }\n let mut w = u;\n while finv[w] != 0 {\n w = finv[w];\n vis[w] = true;\n }\n path.push((w, v));\n }\n for i in 0..path.len() - 1 {\n f[path[i].1] = path[i + 1].0;\n }\n f[path[path.len() - 1].1] = path[0].0;\n for i in 1..n + 1 {\n print!(\"{}{}\", f[i], if i == n { '\\n' } else { ' ' });\n }\n}", "difficulty": "medium"} {"problem_id": "1717", "problem_description": "You are given an array $$$a_1, a_2, \\dots, a_n$$$ where all $$$a_i$$$ are integers and greater than $$$0$$$. In one operation, you can choose two different indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$). If $$$gcd(a_i, a_j)$$$ is equal to the minimum element of the whole array $$$a$$$, you can swap $$$a_i$$$ and $$$a_j$$$. $$$gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$. Now you'd like to make $$$a$$$ non-decreasing using the operation any number of times (possibly zero). Determine if you can do this. An array $$$a$$$ is non-decreasing if and only if $$$a_1 \\le a_2 \\le \\ldots \\le a_n$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int tt = 0; tt < t; tt++) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int b[n];\n int min = 1000000000;\n int c = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b[i] = a[i];\n if (a[i] < min) {\n min = a[i];\n }\n }\n for (int i = 1; i < n; i++) {\n int j = i - 1;\n int temp = a[i];\n while (j >= 0 && a[j] > temp) {\n a[j + 1] = a[j];\n j--;\n }\n a[j + 1] = temp;\n }\n int i;\n for (i = 0; i < n; i++) {\n if (b[i] % min != 0) {\n if (b[i] != a[i]) {\n break;\n }\n }\n }\n if (i == n) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\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 a: 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 m = *a.iter().min().unwrap();\n let mut indexes = Vec::new();\n let mut values = Vec::new();\n\n for i in 0..n {\n if a[i] % m == 0 {\n indexes.push(i);\n values.push(a[i]);\n }\n }\n values.sort();\n\n for (&i, &val) in indexes.iter().zip(values.iter()) {\n a[i] = val;\n }\n\n if (0..(n - 1)).all(|i| a[i] <= a[i + 1]) {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1718", "problem_description": "You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray strictly greater than previous.", "c_code": "int solution() {\n int count = 0;\n scanf(\"%d\\n\", &count);\n int last = 0;\n int i = 0;\n int ninccount = 0;\n int maxinccount = 0;\n for (; i < count; i++) {\n int now = 0;\n scanf(\"%d\", &now);\n if (last < now) {\n ninccount++;\n } else {\n if (maxinccount < ninccount) {\n maxinccount = ninccount;\n }\n ninccount = 1;\n }\n last = now;\n }\n if (maxinccount < ninccount) {\n maxinccount = ninccount;\n }\n printf(\"%d\\n\", maxinccount);\n}", "rust_code": "fn solution() {\n let handle = io::stdin();\n let line = &mut String::new();\n handle.read_line(line).unwrap();\n let n = line.trim().parse::().unwrap();\n line.clear();\n handle.read_line(line).unwrap();\n let a: &mut Vec = &mut line\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let mut lm = 0;\n let mut rm = 0;\n let mut l = 0;\n for i in 1..n {\n if a[i] > a[i - 1] {\n if i - l > rm - lm {\n lm = l;\n rm = i;\n }\n } else {\n l = i;\n }\n }\n\n println!(\"{}\", rm - lm + 1);\n}", "difficulty": "hard"} {"problem_id": "1719", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

The problem set at CODE FESTIVAL 20XX Finals consists of N problems.

\n

The score allocated to the i-th (1≦i≦N) problem is i points.

\n

Takahashi, a contestant, is trying to score exactly N points. For that, he is deciding which problems to solve.

\n

As problems with higher scores are harder, he wants to minimize the highest score of a problem among the ones solved by him.

\n

Determine the set of problems that should be solved.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦N≦10^7
  • \n
\n
\n
\n
\n
\n

Partial Score

    \n
  • 200 points will be awarded for passing the test set satisfying 1≦N≦1000.
  • \n
  • Additional 100 points will be awarded for passing the test set without additional constraints.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Among the sets of problems with the total score of N, find a set in which the highest score of a problem is minimum, then print the indices of the problems in the set in any order, one per line.

\n

If there exists more than one such set, any of them will be accepted.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n
\n
\n
\n
\n
\n

Sample Output 1

1\n3\n
\n

Solving only the 4-th problem will also result in the total score of 4 points, but solving the 1-st and 3-rd problems will lower the highest score of a solved problem.

\n
\n
\n
\n
\n
\n

Sample Input 2

7\n
\n
\n
\n
\n
\n

Sample Output 2

1\n2\n4\n
\n

The set \\{3,4\\} will also be accepted.

\n
\n
\n
\n
\n
\n

Sample Input 3

1\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n
\n
", "c_code": "int solution() {\n long n = 0;\n long maxi = 0;\n long i = 0;\n long remove = 0;\n scanf(\"%ld\", &n);\n for (i = 1; i < n; i++) {\n if (((i + 1) * i) / 2 >= n) {\n break;\n }\n }\n maxi = i;\n\n remove = (maxi * (maxi + 1)) / 2 - n;\n\n for (i = 1; i <= maxi; i++) {\n if (i != remove) {\n printf(\"%ld\\n\", i);\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 let n: usize = itr.next().unwrap().parse().unwrap();\n let mut out = Vec::new();\n\n let mut ng = 0;\n let mut ok = n;\n while ok - ng > 1 {\n let mid = (ok + ng) >> 1;\n if (1..mid + 1).sum::() >= n {\n ok = mid;\n } else {\n ng = mid;\n }\n }\n let sum = (1..ok + 1).sum::();\n\n for i in 1..ok + 1 {\n if sum - n == i {\n continue;\n }\n writeln!(out, \"{}\", i).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "hard"} {"problem_id": "1720", "problem_description": "You are given a number $$$k$$$ and a string $$$s$$$ of length $$$n$$$, consisting of the characters '.' and '*'. You want to replace some of the '*' characters with 'x' characters so that the following conditions are met: The first character '*' in the original string should be replaced with 'x'; The last character '*' in the original string should be replaced with 'x'; The distance between two neighboring replaced characters 'x' must not exceed $$$k$$$ (more formally, if you replaced characters at positions $$$i$$$ and $$$j$$$ ($$$i < j$$$) and at positions $$$[i+1, j-1]$$$ there is no \"x\" symbol, then $$$j-i$$$ must be no more than $$$k$$$). For example, if $$$n=7$$$, $$$s=$$$.**.*** and $$$k=3$$$, then the following strings will satisfy the conditions above: .xx.*xx; .x*.x*x; .xx.xxx. But, for example, the following strings will not meet the conditions: .**.*xx (the first character '*' should be replaced with 'x'); .x*.xx* (the last character '*' should be replaced with 'x'); .x*.*xx (the distance between characters at positions $$$2$$$ and $$$6$$$ is greater than $$$k=3$$$). Given $$$n$$$, $$$k$$$, and $$$s$$$, find the minimum number of '*' characters that must be replaced with 'x' in order to meet the above conditions.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n char str[n + 1];\n scanf(\"%s\", str);\n int dis = -1;\n int preindex = 0;\n int bufferdex = 0;\n int tot = 0;\n int nextdex = 0;\n int counttot = 0;\n for (int i = 0; i < n; i++) {\n if (str[i] == '*') {\n counttot++;\n }\n }\n if (counttot == 1) {\n printf(\"1\\n\");\n } else {\n for (int i = 0; i < n; i++) {\n if (dis == -1) {\n if (str[i] == '.') {\n continue;\n }\n if (str[i] == '*') {\n dis = 0;\n preindex = i;\n }\n }\n if (dis != -1) {\n if (str[i] == '.' && i - preindex <= k) {\n dis++;\n } else if (str[i] == '.' && i - preindex > k) {\n if (str[preindex] != 'x') {\n tot++;\n str[preindex] = 'x';\n if (nextdex != 0) {\n preindex = nextdex;\n nextdex = 0;\n } else {\n preindex = preindex;\n }\n }\n } else if (str[i] == '*' && i - preindex <= k) {\n nextdex = i;\n } else if (str[i] == '*' && i - preindex > k) {\n if (i - preindex == k + 1) {\n tot++;\n str[preindex] = 'x';\n preindex = nextdex;\n nextdex = i;\n }\n }\n }\n }\n if (nextdex != 0) {\n str[nextdex] = 'x';\n str[preindex] = 'x';\n tot++;\n tot++;\n } else if (nextdex == 0) {\n if (str[preindex] != 'x') {\n str[preindex] = 'x';\n tot++;\n }\n }\n printf(\"%d\\n\", tot, str);\n }\n }\n return 0;\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, k): (usize, usize) = (sc.next(), sc.next());\n let s: Vec = sc.next::().chars().collect();\n let mut idx: Vec = vec![];\n for i in 0..n {\n if s[i] == '*' {\n idx.push(i);\n }\n }\n let mut ans: Vec = vec![0; n];\n let mut last = 0;\n for i in 1..idx.len() {\n if idx[i] - idx[last] > k {\n last = i - 1;\n ans[idx[last]] = 1;\n }\n }\n ans[idx[0]] = 1;\n ans[idx[idx.len() - 1]] = 1;\n writeln!(out, \"{}\", ans.iter().sum::()).unwrap();\n }\n}", "difficulty": "easy"} {"problem_id": "1721", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are an integer sequence A_1,...,A_N consisting of N terms, and N buttons.\nWhen the i-th (1 ≦ i ≦ N) button is pressed, the values of the i terms from the first through the i-th are all incremented by 1.

\n

There is also another integer sequence B_1,...,B_N. Takahashi will push the buttons some number of times so that for every i, A_i will be a multiple of B_i.

\n

Find the minimum number of times Takahashi will press the buttons.

\n
\n
\n
\n
\n

Constraints

    \n
  • All input values are integers.
  • \n
  • 1 ≦ N ≦ 10^5
  • \n
  • 0 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)
  • \n
  • 1 ≦ B_i ≦ 10^9(1 ≦ i ≦ N)
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nA_1 B_1\n:\nA_N B_N\n
\n
\n
\n
\n
\n

Output

Print an integer representing the minimum number of times Takahashi will press the buttons.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n3 5\n2 7\n9 4\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

Press the first button twice, the second button twice and the third button three times.

\n
\n
\n
\n
\n
\n

Sample Input 2

7\n3 1\n4 1\n5 9\n2 6\n5 3\n5 8\n9 7\n
\n
\n
\n
\n
\n

Sample Output 2

22\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n long long ans = 0;\n scanf(\"%d\", &n);\n long long a[n];\n long long b[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld%lld\", &a[i], &b[i]);\n }\n for (int i = n - 1; i >= 0; i--) {\n a[i] += ans;\n if (a[i] % b[i] != 0) {\n ans += b[i] - (a[i] % b[i]);\n }\n }\n printf(\"%lld\\n\", ans);\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 (A, B): (Vec, Vec) = {\n let (mut A, mut B) = (vec![], vec![]);\n for _ in 0..N {\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 }\n (A, B)\n };\n\n let ans = (0..N)\n .rev()\n .fold(0, |s, i| s + ((B[i] - (A[i] + s)) % B[i] + B[i]) % B[i]);\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1722", "problem_description": "\n\n\n

Matrix-chain Multiplication

\n\n

\n The goal of the matrix-chain multiplication problem is to find the most efficient way to multiply given $n$ matrices $M_1, M_2, M_3,...,M_n$.\n

\n\n

\n Write a program which reads dimensions of $M_i$, and finds the minimum number of scalar multiplications to compute the maxrix-chain multiplication $M_1M_2...M_n$.\n

\n\n

Input

\n\n

\n In the first line, an integer $n$ is given. In the following $n$ lines, the dimension of matrix $M_i$ ($i = 1...n$) is given by two integers $r$ and $c$ which respectively represents the number of rows and columns of $M_i$.\n

\n\n

Output

\n\n

\n Print the minimum number of scalar multiplication in a line.\n

\n\n

Constraints

\n\n
    \n
  • $1 \\leq n \\leq 100$
  • \n
  • $1 \\leq r, c \\leq 100$
  • \n
\n\n\n

Sample Input 1

\n
\n6\n30 35\n35 15\n15 5\n5 10\n10 20\n20 25\n
\n\n

Sample Output 1

\n
\n15125\n
", "c_code": "int solution(void) {\n int i;\n int j;\n int k;\n int n;\n scanf(\"%d\", &n);\n\n int M[n][2];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &M[i][0]);\n scanf(\"%d\", &M[i][1]);\n }\n\n int x[n][n];\n\n for (i = 1; i < n; i++) {\n for (j = 0; j < n; j++) {\n x[i][j] = 999999;\n }\n }\n\n for (j = 0; j < n; j++) {\n x[0][j] = 0;\n }\n for (j = 0; j < n - 1; j++) {\n x[1][j] = M[j][0] * M[j][1] * M[j + 1][1];\n }\n\n for (i = 2; i < n; i++) {\n for (j = 0; j < n - i; j++) {\n for (k = 0; k < i; k++) {\n if (x[i][j] > x[k][j] + x[i - k - 1][j + k + 1] +\n M[j][0] * M[i + j][1] * M[j + k][1]) {\n x[i][j] = x[k][j] + x[i - k - 1][j + k + 1] +\n M[j][0] * M[i + j][1] * M[j + k][1];\n }\n }\n }\n }\n\n printf(\"%d\\n\", x[n - 1][0]);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let _ = std::io::stdin().read_line(&mut buf).ok();\n let n: usize = buf.trim().parse().unwrap();\n let mut mats = vec![vec![(0, 0, 0); n]; n];\n for i in 0..n {\n let mut buf = String::new();\n let _ = std::io::stdin().read_line(&mut buf).ok();\n let mut buf = buf.split_whitespace();\n mats[i][i].1 = buf.next().unwrap().parse().unwrap();\n mats[i][i].2 = buf.next().unwrap().parse().unwrap();\n }\n for j in 1..n {\n for i in 0..n - j {\n let mut res = Vec::new();\n for k in 0..j {\n res.push(\n mats[i][i + k].0\n + mats[i + k + 1][i + j].0\n + mats[i][i + k].1 * mats[i][i + k].2 * mats[i + k + 1][i + j].2,\n );\n }\n mats[i][i + j].0 = *res.iter().min().unwrap();\n mats[i][i + j].1 = mats[i][i].1;\n mats[i][i + j].2 = mats[i + j][i + j].2;\n }\n }\n println!(\"{}\", mats[0][n - 1].0);\n}", "difficulty": "medium"} {"problem_id": "1723", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.\nEach square is painted black or white.

\n

The grid is said to be good if and only if the following condition is satisfied:

\n
    \n
  • From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.
  • \n
\n

Note that (1, 1) and (H, W) must be white if the grid is good.

\n

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

\n
    \n
  • Choose four integers r_0, c_0, r_1, c_1(1 \\leq r_0 \\leq r_1 \\leq H, 1 \\leq c_0 \\leq c_1 \\leq W). For each pair r, c (r_0 \\leq r \\leq r_1, c_0 \\leq c \\leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq H, W \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\ns_{11} s_{12} \\cdots s_{1W}\ns_{21} s_{22} \\cdots s_{2W}\n \\vdots\ns_{H1} s_{H2} \\cdots s_{HW}\n
\n

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

\n
\n
\n
\n
\n

Output

Print the minimum number of operations needed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n.##\n.#.\n##.\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2\n#.\n.#\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

4 4\n..##\n#...\n###.\n###.\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

No operation may be needed.

\n
\n
\n
\n
\n
\n

Sample Input 4

5 5\n.#.#.\n#.#.#\n.#.#.\n#.#.#\n.#.#.\n
\n
\n
\n
\n
\n

Sample Output 4

4\n
\n
\n
", "c_code": "int solution(void) {\n\n int h;\n int w;\n scanf(\"%d %d\", &h, &w);\n char s[h][w + 1];\n for (int i = 0; i < h; i++) {\n scanf(\"%s\", s[i]);\n }\n int dp[h][w];\n dp[0][0] = 0;\n if (s[0][0] == '#') {\n dp[0][0] = 1;\n }\n for (int i = 1; i < w; i++) {\n dp[0][i] = dp[0][i - 1];\n if (s[0][i - 1] == '.' && s[0][i] == '#') {\n dp[0][i]++;\n }\n }\n int case1;\n int case2;\n for (int i = 1; i < h; i++) {\n dp[i][0] = dp[i - 1][0];\n if (s[i - 1][0] == '.' && s[i][0] == '#') {\n dp[i][0]++;\n }\n for (int j = 1; j < w; j++) {\n case1 = dp[i - 1][j];\n if (s[i - 1][j] == '.' && s[i][j] == '#') {\n case1++;\n }\n case2 = dp[i][j - 1];\n if (s[i][j - 1] == '.' && s[i][j] == '#') {\n case2++;\n }\n dp[i][j] = case1;\n if (case2 < case1) {\n dp[i][j] = case2;\n }\n }\n }\n printf(\"%d\\n\", dp[h - 1][w - 1]);\n\n return 0;\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 h = it.next().unwrap().parse::().unwrap();\n let w = it.next().unwrap().parse::().unwrap();\n let s = (0..h)\n .map(|_| it.next().unwrap().as_bytes())\n .collect::>();\n let mut dp = vec![vec!(100000; w); h];\n dp[0][0] = if s[0][0] == b'#' { 1 } else { 0 };\n for i in 0..h {\n for j in 0..w {\n if i + 1 < h {\n dp[i + 1][j] = cmp::min(\n dp[i + 1][j],\n dp[i][j]\n + if s[i][j] == b'.' && s[i + 1][j] == b'#' {\n 1\n } else {\n 0\n },\n );\n }\n if j + 1 < w {\n dp[i][j + 1] = cmp::min(\n dp[i][j + 1],\n dp[i][j]\n + if s[i][j] == b'.' && s[i][j + 1] == b'#' {\n 1\n } else {\n 0\n },\n );\n }\n }\n }\n println!(\"{}\", dp[h - 1][w - 1]);\n}", "difficulty": "easy"} {"problem_id": "1724", "problem_description": "The string $$$s$$$ is given, the string length is odd number. The string consists of lowercase letters of the Latin alphabet.As long as the string length is greater than $$$1$$$, the following operation can be performed on it: select any two adjacent letters in the string $$$s$$$ and delete them from the string. For example, from the string \"lemma\" in one operation, you can get any of the four strings: \"mma\", \"lma\", \"lea\" or \"lem\" In particular, in one operation, the length of the string reduces by $$$2$$$.Formally, let the string $$$s$$$ have the form $$$s=s_1s_2 \\dots s_n$$$ ($$$n>1$$$). During one operation, you choose an arbitrary index $$$i$$$ ($$$1 \\le i < n$$$) and replace $$$s=s_1s_2 \\dots s_{i-1}s_{i+2} \\dots s_n$$$.For the given string $$$s$$$ and the letter $$$c$$$, determine whether it is possible to make such a sequence of operations that in the end the equality $$$s=c$$$ will be true? In other words, is there such a sequence of operations that the process will end with a string of length $$$1$$$, which consists of the letter $$$c$$$?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t-- > 0) {\n char s[49];\n char c;\n scanf(\"%s %c\", s, &c);\n int count = 0;\n int i = 0;\n while (s[i] != '\\0') {\n if (s[i] == c && i % 2 == 0) {\n count = 1;\n break;\n }\n i++;\n }\n if (count == 1) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let out = &mut BufWriter::new(stdout());\n\n let mut input = String::new();\n let _firstline = stdin().read_line(&mut input).ok();\n\n let t = input.trim_end().parse::().expect(\"Failed parsed\");\n\n for _ in 0..t {\n let mut src = String::new();\n let mut target = String::new();\n stdin().read_line(&mut src).ok();\n stdin().read_line(&mut target).ok();\n src = String::from(src.trim_end());\n target = String::from(target.trim_end());\n\n let mut is_ok = false;\n for s_index in 0..src.len() {\n if s_index % 2 == 0 && src.get(s_index..=s_index).unwrap() == target {\n is_ok = true;\n\n break;\n }\n }\n\n if is_ok {\n writeln!(out, \"YES\").ok();\n } else {\n writeln!(out, \"NO\").ok();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1725", "problem_description": "

水道料金 (Water Rate)

\n\n

問題

\n

\nJOI 君が住んでいる地域には水道会社が X 社と Y 社の 2 つある.2 つの会社の 1 ヶ月の水道料金は,1 ヶ月の水道の使用量に応じて次のように決まる.\n

\n\n
    \n
  • X 社: 1 リットル あたり A 円かかる.
  • \n
  • Y 社: 基本料金は B 円である.使用量が C リットル以下ならば,料金は基本料金 B 円のみがかかる.使用量が C リットルを超えると基本料金 B 円に加えて追加料金がかかる.追加料金は使用量が C リットルを 1 リットル超えるごとに D 円である.
  • \n
\n\n\n

\nJOI 君の家では 1 ヶ月の水道の使用量が P リットルである.\n

\n\n

\n水道料金ができるだけ安くなるように水道会社を選ぶとき,JOI 君の家の 1 ヶ月の水道料金を求めよ.\n

\n\n

入力

\n

\n入力は 5 行からなり,1 行に 1 つずつ整数が書かれている.
\n\n1 行目には X 社の 1 リットルあたりの料金 A が書かれている.
\n2 行目には Y 社の基本料金 B が書かれている.
\n3 行目には Y 社の料金が基本料金のみになる使用量の上限 C が書かれている.
\n4 行目には Y 社の 1 リットルあたりの追加料金 D が書かれている.
\n5 行目には JOI 君の家の 1 ヶ月の水道の使用量 P が書かれている.\n

\n\n

\n書かれている整数 A, B, C, D, P はすべて 1 以上 10000 以下である.\n

\n\n

出力

\n

\nJOI 君の家の 1 ヶ月の水道料金を表す整数を 1 行で出力せよ.\n

\n\n

入出力例

\n\n

入力例 1

\n\n
\n9\n100\n20\n3\n10\n
\n

出力例 1

\n
\n90\n
\n\n

入力例 2

\n
\n8\n300\n100\n10\n250\n
\n\n

出力例 2

\n
\n1800\n
\n\n

\n入出力例 1 では,JOI 君の家の 1 ヶ月の水道の使用量は 10 リットルである.\n

\n\n
    \n
  • X 社の水道料金は 9 × 10 = 90 円である.
  • \n
  • JOI 君の家の 1 ヶ月の水道の使用量は 20 リットル以下なので,Y 社の水道料金は基本料金の 100 円である.
  • \n
\n\n

\nJOI 君の家は水道料金がより安い X 社を選ぶ.そのときの JOI 君の家の 1 ヶ月の水道料金は 90 円である.\n

\n\n

\n入出力例 2 では,JOI 君の家の 1 ヶ月の水道の使用量は 250 リットルである.\n

\n\n
    \n
  • X 社の水道料金は 8 × 250 = 2000 円である.
  • \n
  • JOI 君の家の 1 ヶ月の水道の使用量は 100 リットル以上で,超過量は 250 - 100 = 150 リットルである.よって,Y 社の水道料金は基本料金の 300 円に加えて 10 × 150 = 1500 円の追加料金がかかり,合計の水道料金は 300 + 1500 = 1800 円である.
  • \n
\n\n

\nJOI 君の家は水道料金がより安い Y 社を選ぶ.そのときの JOI 君の家の 1 ヶ月の水道料金は 1800 円である.\n

\n\n\n\n
\n

\n問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。\n

\n
", "c_code": "int solution(void) {\n\n int P = 0;\n int A = 0;\n int B = 0;\n int C = 0;\n int D = 0;\n scanf(\"%d %d %d %d %d\", &A, &B, &C, &D, &P);\n\n int X = A * P;\n int Y = B;\n int y = ((P - C) * D) + B;\n if (P <= C) {\n if (X >= Y) {\n printf(\"%d\\n\", Y);\n } else if (X < Y) {\n printf(\"%d\\n\", X);\n }\n }\n\n if (P > C) {\n if (X >= y) {\n printf(\"%d\\n\", y);\n } else if (X < y) {\n printf(\"%d\\n\", X);\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').map(|s| s.parse::().unwrap());\n\n let a = lines.next().unwrap();\n let b = lines.next().unwrap();\n let c = lines.next().unwrap();\n let d = lines.next().unwrap();\n let p = lines.next().unwrap();\n\n let x = p * a;\n let y = b + p.saturating_sub(c) * d;\n\n println!(\"{}\", if x < y { x } else { y });\n}", "difficulty": "easy"} {"problem_id": "1726", "problem_description": "Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options: on this day the gym is closed and the contest is not carried out; on this day the gym is closed and the contest is carried out; on this day the gym is open and the contest is not carried out; on this day the gym is open and the contest is carried out. On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.", "c_code": "int solution() {\n int n = 0;\n int x = 0;\n int y = 0;\n int count = 0;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n if (a[i] == 0) {\n count++;\n x = 0;\n y = 0;\n } else if (a[i] == 1) {\n if (y == 1) {\n count++;\n x = 0;\n y = 0;\n } else {\n y = 1;\n x = 0;\n }\n } else if (a[i] == 2) {\n if (x == 1) {\n count++;\n x = 0;\n y = 0;\n } else {\n y = 0;\n x = 1;\n }\n } else if (a[i] == 3) {\n if (y == 1) {\n x = 1;\n y = 0;\n } else if (x == 1) {\n y = 1;\n x = 0;\n }\n }\n }\n printf(\"%d\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let n = {\n let mut buf = String::new();\n stdin.read_line(&mut buf).unwrap();\n buf.trim().parse::().unwrap()\n };\n\n let mut a = vec![0; n];\n for (i, cur) in stdin.lock().split(b' ').enumerate() {\n let str = String::from_utf8(cur.unwrap()).unwrap();\n a[i] = str.trim().parse::().unwrap();\n }\n\n let mut dp1 = vec![0; n + 1];\n let mut dp2 = vec![0; n + 1];\n\n for i in 1..n + 1 {\n {\n let do_nothing = min(dp1[i - 1], dp2[i - 1]) + 1;\n dp1[i] = do_nothing;\n dp2[i] = do_nothing;\n }\n\n let cur = a[i - 1];\n\n if cur & 1 != 0 {\n dp1[i] = min(dp1[i], dp2[i - 1]);\n }\n\n if cur & 2 != 0 {\n dp2[i] = min(dp2[i], dp1[i - 1]);\n }\n }\n\n println!(\"{}\", min(dp2[n], dp1[n]));\n}", "difficulty": "medium"} {"problem_id": "1727", "problem_description": "Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The $$$i$$$-th page contains some mystery that will be explained on page $$$a_i$$$ ($$$a_i \\ge i$$$).Ivan wants to read the whole book. Each day, he reads the first page he didn't read earlier, and continues to read the following pages one by one, until all the mysteries he read about are explained and clear to him (Ivan stops if there does not exist any page $$$i$$$ such that Ivan already has read it, but hasn't read page $$$a_i$$$). After that, he closes the book and continues to read it on the following day from the next page.How many days will it take to read the whole book?", "c_code": "int solution(void) {\n int n = 0;\n scanf(\"%d\", &n);\n int max = 0;\n int days = 0;\n\n for (int i = 1; i <= n; i++) {\n int a = 0;\n scanf(\"%d\", &a);\n\n if (a > max) {\n max = a;\n }\n\n if (a == max && a == i) {\n days++;\n }\n }\n\n printf(\"%d\\n\", days);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let _n = input.trim().parse::().unwrap();\n input.clear();\n io::stdin().read_line(&mut input).unwrap();\n let v: Vec = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let mut day = 0;\n let mut idx = 0;\n let mut max = 0;\n loop {\n if v[idx] >= max {\n max = v[idx];\n }\n if max <= idx + 1 {\n day += 1;\n }\n idx += 1;\n if idx == v.len() {\n break;\n }\n }\n println!(\"{}\", day);\n}", "difficulty": "easy"} {"problem_id": "1728", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N cities on a number line. The i-th city is located at coordinate x_i.

\n

Your objective is to visit all these cities at least once.

\n

In order to do so, you will first set a positive integer D.

\n

Then, you will depart from coordinate X and perform Move 1 and Move 2 below, as many times as you like:

\n
    \n
  • Move 1: travel from coordinate y to coordinate y + D.
  • \n
  • Move 2: travel from coordinate y to coordinate y - D.
  • \n
\n

Find the maximum value of D that enables you to visit all the cities.

\n

Here, to visit a city is to travel to the coordinate where that city is located.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq X \\leq 10^9
  • \n
  • 1 \\leq x_i \\leq 10^9
  • \n
  • x_i are all different.
  • \n
  • x_1, x_2, ..., x_N \\neq X
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N X\nx_1 x_2 ... x_N\n
\n
\n
\n
\n
\n

Output

Print the maximum value of D that enables you to visit all the cities.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n1 7 11\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Setting D = 2 enables you to visit all the cities as follows, and this is the maximum value of such D.

\n
    \n
  • Perform Move 2 to travel to coordinate 1.
  • \n
  • Perform Move 1 to travel to coordinate 3.
  • \n
  • Perform Move 1 to travel to coordinate 5.
  • \n
  • Perform Move 1 to travel to coordinate 7.
  • \n
  • Perform Move 1 to travel to coordinate 9.
  • \n
  • Perform Move 1 to travel to coordinate 11.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 81\n33 105 57\n
\n
\n
\n
\n
\n

Sample Output 2

24\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 1\n1000000000\n
\n
\n
\n
\n
\n

Sample Output 3

999999999\n
\n
\n
", "c_code": "int solution() {\n int n;\n int x;\n int y;\n int d;\n int i;\n int j;\n int k;\n int t;\n\n scanf(\"%d %d\", &n, &x);\n for (d = i = 0; i < n; i++) {\n scanf(\"%d\", &y);\n if (d == 0) {\n d = abs(x - y);\n } else {\n j = d;\n k = abs(x - y);\n if (k > j) {\n t = j;\n j = k;\n k = t;\n }\n while (k > 0) {\n t = j % k;\n j = k;\n k = t;\n }\n d = j;\n x = y;\n }\n }\n printf(\"%d\\n\", d);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n init: i32,\n xs: [i32; n],\n }\n let xs = {\n let mut xs: Vec<_> = xs.iter().map(|x| x - init).collect();\n xs.push(0);\n xs.sort();\n xs\n };\n let min_d = xs.windows(2).map(|w| w[1] - w[0]).min().unwrap();\n for d in (1..min_d + 1).rev() {\n if xs.iter().all(|x| x % d == 0) {\n println!(\"{}\", d);\n return;\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1729", "problem_description": "Ichihime is the current priestess of the Mahjong Soul Temple. She claims to be human, despite her cat ears.These days the temple is holding a math contest. Usually, Ichihime lacks interest in these things, but this time the prize for the winner is her favorite — cookies. Ichihime decides to attend the contest. Now she is solving the following problem. You are given four positive integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$, such that $$$a \\leq b \\leq c \\leq d$$$. Your task is to find three integers $$$x$$$, $$$y$$$, $$$z$$$, satisfying the following conditions: $$$a \\leq x \\leq b$$$. $$$b \\leq y \\leq c$$$. $$$c \\leq z \\leq d$$$. There exists a triangle with a positive non-zero area and the lengths of its three sides are $$$x$$$, $$$y$$$, and $$$z$$$.Ichihime desires to get the cookie, but the problem seems too hard for her. Can you help her?", "c_code": "int solution() {\n int a[1000][4];\n int b[1000][3];\n int listNum;\n scanf(\"%d\", &listNum);\n for (int i = 0; i < listNum; i++) {\n scanf(\"%d %d %d %d\", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);\n b[i][0] = a[i][0];\n b[i][1] = a[i][2];\n b[i][2] = a[i][2];\n }\n\n for (int i = 0; i < listNum; i++) {\n printf(\"%d \", b[i][0]);\n printf(\"%d \", b[i][1]);\n if (i == listNum - 1) {\n printf(\"%d\", b[i][2]);\n } else {\n printf(\"%d\\n\", b[i][2]);\n }\n }\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let test: i64 = str.trim().parse().unwrap();\n for _ in 0..test {\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let mut iter = str.split_whitespace();\n let _: i64 = iter.next().unwrap().parse().unwrap();\n let b: i64 = iter.next().unwrap().parse().unwrap();\n let c: i64 = iter.next().unwrap().parse().unwrap();\n println!(\"{} {} {}\", b, c, c);\n }\n}", "difficulty": "hard"} {"problem_id": "1730", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.

\n

To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:

\n
    \n
  • The 0 key: a letter 0 will be inserted to the right of the string.
  • \n
  • The 1 key: a letter 1 will be inserted to the right of the string.
  • \n
  • The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.
  • \n
\n

Sig has launched the editor, and pressed these keys several times. You are given a string s, which is a record of his keystrokes in order. In this string, the letter 0 stands for the 0 key, the letter 1 stands for the 1 key and the letter B stands for the backspace key. What string is displayed in the editor now?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≦ |s| ≦ 10 (|s| denotes the length of s)
  • \n
  • s consists of the letters 0, 1 and B.
  • \n
  • The correct answer is not an empty string.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

Print the string displayed in the editor in the end.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

01B0\n
\n
\n
\n
\n
\n

Sample Output 1

00\n
\n

Each time the key is pressed, the string in the editor will change as follows: 0, 01, 0, 00.

\n
\n
\n
\n
\n
\n

Sample Input 2

0BB1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

Each time the key is pressed, the string in the editor will change as follows: 0, (empty), (empty), 1.

\n
\n
", "c_code": "int solution(void) {\n char str[15];\n int i = 0;\n while ((str[i] = getchar()) != '\\n') {\n if (str[i] == 'B' && i != 0) {\n i--;\n str[i] = '\\0';\n continue;\n }\n if (str[i] == 'B') {\n continue;\n }\n i++;\n }\n str[i] = '\\0';\n printf(\"%s\\n\", str);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n\n let s = buf.trim();\n let mut stack = Vec::with_capacity(s.len());\n\n for c in s.chars() {\n match c {\n 'B' => {\n stack.pop();\n }\n _ => {\n stack.push(c);\n }\n }\n }\n\n println!(\"{}\", stack.iter().copied().collect::());\n}", "difficulty": "medium"} {"problem_id": "1731", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Let w be a string consisting of lowercase letters.\nWe will call w beautiful if the following condition is satisfied:

\n
    \n
  • Each lowercase letter of the English alphabet occurs even number of times in w.
  • \n
\n

You are given the string w. Determine if w is beautiful.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |w| \\leq 100
  • \n
  • w consists of lowercase letters (a-z).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
w\n
\n
\n
\n
\n
\n

Output

Print Yes if w is beautiful. Print No otherwise.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

abaccaba\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

a occurs four times, b occurs twice, c occurs twice and the other letters occur zero times.

\n
\n
\n
\n
\n
\n

Sample Input 2

hthth\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n
\n
", "c_code": "int solution() {\n char w[101];\n int keys[26] = {0};\n int flag = 0;\n scanf(\"%s\", w);\n\n for (int i = 0; w[i] != '\\0'; i++) {\n keys[w[i] - 'a']++;\n }\n\n for (int i = 0; i < 26; i++) {\n flag = (flag | keys[i]);\n }\n\n printf((flag & 1) == 0 ? \"Yes\\n\" : \"No\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let w = s.trim();\n let mut d = HashMap::new();\n\n for e in w.chars() {\n *d.entry(e).or_insert(0) += 1;\n }\n\n println!(\n \"{}\",\n if d.iter().all(|e| *e.1 % 2 == 0) {\n \"Yes\"\n } else {\n \"No\"\n }\n );\n}", "difficulty": "medium"} {"problem_id": "1732", "problem_description": "Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.", "c_code": "int solution() {\n long long int n;\n long long int m;\n scanf(\"%lli %lli\", &n, &m);\n int i;\n int rows[n + 1];\n int columns[n + 1];\n int rws = 0;\n int cols = 0;\n long long int frees[m];\n long long int freeCounter = 0;\n for (i = 1; i <= n; i++) {\n rows[i] = columns[i] = 0;\n }\n long long int free = n * n;\n for (i = 0; i < m; i++) {\n int x;\n int y;\n scanf(\"%d %d\", &x, &y);\n if (rows[x] + columns[y] == 2) {\n frees[freeCounter] = free;\n freeCounter++;\n continue;\n }\n if (rows[x] + columns[y] == 0) {\n free -= 2 * n - 1 - (rws + cols);\n rws++;\n cols++;\n rows[x] = 1;\n columns[y] = 1;\n } else if (columns[y] == 1) {\n free -= n - cols;\n rows[x] = 1;\n rws++;\n } else if (rows[x] == 1) {\n free -= n - rws;\n columns[y] = 1;\n cols++;\n }\n frees[freeCounter] = free;\n freeCounter++;\n }\n for (i = 0; i < freeCounter; i++) {\n printf(\"%lli \", frees[i]);\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n std::io::stdin().read_to_string(&mut buffer).unwrap();\n let mut cin = buffer\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n let n = cin.next().unwrap();\n cin.next().unwrap();\n\n let mut cntx = n as u64;\n let mut cnty = n as u64;\n let mut usedx = vec![false; n];\n let mut usedy = vec![false; n];\n\n while let Some(x) = cin.next() {\n let x = x - 1;\n let y = cin.next().unwrap() - 1;\n if !usedx[x] {\n usedx[x] = true;\n cntx -= 1;\n }\n if !usedy[y] {\n usedy[y] = true;\n cnty -= 1;\n }\n\n print!(\"{} \", cntx * cnty);\n }\n\n println!();\n}", "difficulty": "hard"} {"problem_id": "1733", "problem_description": "As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. The school has n servers. Each server has a name and an ip (names aren't necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we'll assume that an nginx command is of form \"command ip;\" where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers. Each ip is of form \"a.b.c.d\" where a, b, c and d are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has m commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is \"command ip;\" Dustin has to replace it with \"command ip; #name\" where name is the name of the server with ip equal to ip.Dustin doesn't know anything about nginx, so he panicked again and his friends asked you to do his task for him.", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n char name[1000][15];\n int ip[1000][15];\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", &name[i][0]);\n scanf(\"%d.%d.%d.%d\", &ip[i][0], &ip[i][1], &ip[i][2], &ip[i][3]);\n }\n char name2[15];\n int ip2[4];\n for (int i = 0; i < m; i++) {\n scanf(\"%s\", name2);\n scanf(\"%d.%d.%d.%d;\", &ip2[0], &ip2[1], &ip2[2], &ip2[3]);\n for (int j = 0; j < n; j++) {\n if (ip[j][0] == ip2[0] && ip[j][1] == ip2[1] && ip[j][2] == ip2[2] &&\n ip[j][3] == ip2[3]) {\n printf(\"%s %d.%d.%d.%d; #%s\\n\", name2, ip2[0], ip2[1], ip2[2], ip2[3],\n &name[j][0]);\n }\n }\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_to_string(&mut buffer).unwrap();\n\n let mut lines = buffer.lines();\n\n let nm: Vec = lines\n .next()\n .unwrap()\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect();\n let (n, m) = (nm[0], nm[1]);\n let mut ip_to_name = HashMap::new();\n\n for _ in 0..n {\n let l = lines.next().unwrap();\n let index = l.find(' ').unwrap();\n let name = l[..index].to_string();\n let ip = l[(index + 1)..l.len()].to_string();\n\n ip_to_name.insert(ip, name);\n }\n for _ in 0..m {\n let l = lines.next().unwrap();\n let index = l.find(' ').unwrap();\n let index_end = l.find(';').unwrap();\n\n let ip = l[(index + 1)..index_end].to_string();\n\n let name = ip_to_name.get(&ip).unwrap();\n println!(\"{} #{}\", l.trim(), name);\n }\n}", "difficulty": "medium"} {"problem_id": "1734", "problem_description": "You are given a square grid with $$$n$$$ rows and $$$n$$$ columns. Each cell contains either $$$0$$$ or $$$1$$$. In an operation, you can select a cell of the grid and flip it (from $$$0 \\to 1$$$ or $$$1 \\to 0$$$). Find the minimum number of operations you need to obtain a square that remains the same when rotated $$$0^{\\circ}$$$, $$$90^{\\circ}$$$, $$$180^{\\circ}$$$ and $$$270^{\\circ}$$$.The picture below shows an example of all rotations of a grid.", "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][n];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n scanf(\" %c\", &a[i][j]);\n }\n }\n\n int x = 0;\n int c = 0;\n int y = 0;\n int z = n - 1;\n int w = n - 1;\n while (n > 0) {\n int k = n;\n while (--k) {\n int l = 0;\n if (a[x][y] == '0') {\n l++;\n }\n if (a[y][z] == '0') {\n l++;\n }\n if (a[z][w] == '0') {\n l++;\n }\n if (a[w][x] == '0') {\n l++;\n }\n\n if (l == 1 || l == 3) {\n c = c + 1;\n } else if (l == 2) {\n c = c + 2;\n }\n\n y++;\n w--;\n }\n x++;\n y = x;\n z--;\n w = z;\n n = n - 2;\n }\n\n printf(\"%d\\n\", c);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let (stdin, stdout) = (io::stdin(), io::stdout());\n let mut scan = Scanner::new(stdin.lock());\n let mut out = io::BufWriter::new(stdout.lock());\n let t = scan.next();\n for _ in 0..t {\n let n: usize = scan.next();\n let mut v: Vec> = Vec::new();\n for _i in 0..n {\n let s: String = scan.next();\n v.push(s.chars().collect());\n }\n let mut ans = 0;\n for i in 0..n {\n for j in 0..n {\n if n - i - 1 == i && n - j - 1 == j {\n continue;\n }\n let mut cnt0 = 0;\n let mut cnt1 = 0;\n let mut ii = i;\n let mut jj = j;\n for _ in 0..4 {\n let nx = jj;\n let ny = n - ii - 1;\n if v[ii][jj] == '1' {\n cnt1 += 1;\n } else {\n cnt0 += 1;\n }\n ii = nx;\n jj = ny;\n }\n assert_eq!(cnt0 + cnt1, 4);\n ans += min(cnt0, cnt1);\n }\n }\n writeln!(out, \"{}\", ans / 4);\n }\n}", "difficulty": "medium"} {"problem_id": "1735", "problem_description": "Given an array $$$a$$$ of $$$n$$$ elements, print any value that appears at least three times or print -1 if there is no such value.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int number = 0;\n int count = 0;\n int frq[200001] = {0};\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &number);\n for (int f = 0; f < 200001; f++) {\n frq[f] = 0;\n }\n for (int i = 0; i < number; i++) {\n int arr[i];\n scanf(\"%d\", &arr[i]);\n frq[arr[i]]++;\n }\n\n for (int k = 1; k <= number; k++) {\n if (frq[k] > 2) {\n printf(\"%d\\n\", k);\n count++;\n break;\n }\n }\n if (count == 0) {\n printf(\"-1\\n\");\n }\n count = 0;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"couldn't read from input\");\n\n let num_test_cases: isize = input.trim().parse().expect(\"couldn't parse number\");\n for _ in 0..num_test_cases {\n let mut input = String::new();\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"couldn't read from input\");\n\n let mut tmp: HashMap = HashMap::new();\n let mut input = String::new();\n\n std::io::stdin()\n .read_line(&mut input)\n .expect(\"couldn't read from input\");\n\n let str: String = input.trim().parse().expect(\"couldn't parse numbers\");\n for c in str.split(' ') {\n let c: u32 = c.parse().expect(\"couldn't parse char\");\n let counter = tmp.entry(c).or_insert(0);\n *counter += 1;\n }\n\n let mut result = false;\n\n for (n, o) in tmp.drain() {\n if o >= 3 {\n println!(\"{n}\");\n result = true;\n break;\n }\n }\n\n if !result {\n println!(\"-1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1736", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You have decided to give an allowance to your child depending on the outcome of the game that he will play now.

\n

The game is played as follows:

\n
    \n
  • There are three \"integer panels\", each with a digit between 1 and 9 (inclusive) printed on it, and one \"operator panel\" with a + printed on it.
  • \n
  • The player should construct a formula of the form X + Y, by arranging the four panels from left to right. (The operator panel should not be placed at either end of the formula.)
  • \n
  • Then, the amount of the allowance will be equal to the resulting value of the formula.
  • \n
\n

Given the values A, B and C printed on the integer panels used in the game, find the maximum possible amount of the allowance.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A, B, C \\leq 9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C\n
\n
\n
\n
\n
\n

Output

Print the maximum possible amount of the allowance.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 5 2\n
\n
\n
\n
\n
\n

Sample Output 1

53\n
\n

The amount of the allowance will be 53 when the panels are arranged as 52+1, and this is the maximum possible amount.

\n
\n
\n
\n
\n
\n

Sample Input 2

9 9 9\n
\n
\n
\n
\n
\n

Sample Output 2

108\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6 6 7\n
\n
\n
\n
\n
\n

Sample Output 3

82\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 && a >= c) {\n printf(\"%d\\n\", (a * 10) + b + c);\n } else if (b >= a && b >= c) {\n printf(\"%d\\n\", (b * 10) + a + c);\n } else if (c >= a && c >= b) {\n printf(\"%d\\n\", (c * 10) + a + b);\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 let _ = reader.read_line(&mut s);\n\n let mut v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n v.sort();\n\n println!(\"{}\", v[2] * 10 + v[1] + v[0]);\n}", "difficulty": "medium"} {"problem_id": "1737", "problem_description": "Vlad likes to eat in cafes very much. During his life, he has visited cafes n times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research.First of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes he visited in a row, in order of visiting them. Now, Vlad wants to find such a cafe that his last visit to that cafe was before his last visits to every other cafe. In other words, he wants to find such a cafe that he hasn't been there for as long as possible. Help Vlad to find that cafe.", "c_code": "int solution() {\n int n;\n int a[(2 * 100000) + 1] = {0};\n int b[(2 * 100000) + 1] = {0};\n int min = INT_MAX;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b[a[i] - 1] = i;\n }\n int count = 0;\n for (int i = 0; i < n; i++) {\n if (min > b[a[i] - 1]) {\n min = b[a[i] - 1];\n count = a[i];\n }\n }\n printf(\"%d\", count);\n}", "rust_code": "fn solution() {\n let mut text = String::new();\n stdin().read_line(&mut text).unwrap();\n text.clear();\n stdin().read_line(&mut text).unwrap();\n let a: Vec = text\n .split_whitespace()\n .map(|e| e.parse().unwrap())\n .collect();\n let mut hash: HashMap = HashMap::new();\n for (i, val) in a.iter().enumerate() {\n hash.insert(*val, i);\n }\n let a: Vec<(&usize, &usize)> = hash.iter().collect();\n let mut v = vec![];\n for i in &a {\n v.push(*i.1)\n }\n v.sort();\n\n for i in a {\n if *i.1 == v[0] {\n println!(\"{:?}\", i.0);\n break;\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1738", "problem_description": "You are given a system of pipes. It consists of two rows, each row consists of $$$n$$$ pipes. The top left pipe has the coordinates $$$(1, 1)$$$ and the bottom right — $$$(2, n)$$$.There are six types of pipes: two types of straight pipes and four types of curved pipes. Here are the examples of all six types: Types of pipes You can turn each of the given pipes $$$90$$$ degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types $$$1$$$ and $$$2$$$ can become each other and types $$$3, 4, 5, 6$$$ can become each other).You want to turn some pipes in a way that the water flow can start at $$$(1, 0)$$$ (to the left of the top left pipe), move to the pipe at $$$(1, 1)$$$, flow somehow by connected pipes to the pipe at $$$(2, n)$$$ and flow right to $$$(2, n + 1)$$$.Pipes are connected if they are adjacent in the system and their ends are connected. Here are examples of connected pipes: Examples of connected pipes Let's describe the problem using some example: The first example input And its solution is below: The first example answer As you can see, the water flow is the poorly drawn blue line. To obtain the answer, we need to turn the pipe at $$$(1, 2)$$$ $$$90$$$ degrees clockwise, the pipe at $$$(2, 3)$$$ $$$90$$$ degrees, the pipe at $$$(1, 6)$$$ $$$90$$$ degrees, the pipe at $$$(1, 7)$$$ $$$180$$$ degrees and the pipe at $$$(2, 7)$$$ $$$180$$$ degrees. Then the flow of water can reach $$$(2, n + 1)$$$ from $$$(1, 0)$$$.You have to answer $$$q$$$ independent queries.", "c_code": "int solution(void) {\n int n;\n int q;\n static int a[2][200000];\n static int b[100000];\n scanf(\"%d\", &q);\n for (int i = 0; i < q; i++) {\n scanf(\"%d\", &n);\n for (int j = 0; j < 2; j++) {\n for (int k = 0; k < n; k++) {\n scanf(\"%1d\", &a[j][k]);\n }\n }\n int g = 1;\n int l = 0;\n int r = 0;\n while (l < n && r < 2 && r > -1) {\n switch (g) {\n case 1:\n switch (a[r][l]) {\n case 1:\n case 2:\n l++;\n break;\n default:\n switch (r) {\n case 0:\n r++;\n g = 2;\n break;\n case 1:\n r--;\n g = 2;\n break;\n }\n break;\n }\n break;\n case 2:\n switch (a[r][l]) {\n case 1:\n case 2:\n r = 3;\n break;\n default:\n l++;\n g = 1;\n break;\n }\n break;\n }\n }\n if (r == 3 || r == 0) {\n b[i] = 0;\n } else {\n if (g == 1) {\n b[i] = 1;\n } else {\n b[i] = 0;\n }\n }\n }\n for (int i = 0; i < q; i++) {\n switch (b[i]) {\n case 1:\n printf(\"YES\\n\");\n break;\n case 0:\n printf(\"NO\\n\");\n break;\n }\n }\n}", "rust_code": "fn solution() {\n let reader = io::stdin();\n\n let mut result = Vec::new();\n\n let queries_count = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n\n for _i in 0..queries_count {\n let n = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .parse::()\n .unwrap();\n\n let row1: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .chars()\n .map(|s| s.to_string().parse::().unwrap())\n .collect();\n\n let row2: Vec = reader\n .lock()\n .lines()\n .next()\n .unwrap()\n .unwrap()\n .chars()\n .map(|s| s.to_string().parse::().unwrap())\n .collect();\n\n let mut cur_row = 1;\n let mut flag = true;\n for col in 0..(n as usize) {\n match cur_row {\n 1 => match row1[col] {\n 3..=6 => {\n match row2[col] {\n 1 | 2 => {\n flag = false;\n break;\n }\n _ => {}\n }\n\n cur_row = 2;\n }\n _ => {}\n },\n 2 => match row2[col] {\n 3..=6 => {\n match row1[col] {\n 1 | 2 => {\n flag = false;\n break;\n }\n _ => {}\n }\n\n cur_row = 1;\n }\n _ => {}\n },\n _ => {}\n }\n }\n\n if flag && cur_row == 2 {\n result.push(\"YES\")\n } else {\n result.push(\"NO\")\n }\n }\n\n for val in result {\n println!(\"{} \", val);\n }\n}", "difficulty": "medium"} {"problem_id": "1739", "problem_description": "You are given a string $$$s$$$, consisting of lowercase Latin letters. Every letter appears in it no more than twice.Your task is to rearrange the letters in the string in such a way that for each pair of letters that appear exactly twice, the distance between the letters in the pair is the same. You are not allowed to add or remove letters.It can be shown that the answer always exists. If there are multiple answers, print any of them.", "c_code": "int solution() {\n unsigned int tests;\n scanf(\"%u\\n\", &tests);\n\n for (unsigned int l = 0; l != tests; ++l) {\n char s[64];\n fgets(s, sizeof(s), stdin);\n\n char frec['z' + 1] = {0};\n for (const char *i = s; *i && *i != '\\n'; ++i) {\n ++frec[*i];\n }\n\n for (char i = 'a'; i <= 'z'; ++i) {\n while (frec[i]--) {\n putchar(i);\n }\n }\n putchar('\\n');\n }\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\n let t: i32 = input.trim().parse().expect(\"invalid input\");\n\n let mut i = 0;\n while i < t {\n let mut s = String::new();\n io::stdin()\n .read_line(&mut s)\n .expect(\"failed to read input.\");\n\n i += 1;\n\n let mut l: Vec = s.chars().collect();\n l.sort();\n let a: String = l.into_iter().collect();\n\n println!(\"{}\", a.trim());\n }\n}", "difficulty": "hard"} {"problem_id": "1740", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

An uppercase or lowercase English letter \\alpha will be given as input.\nIf \\alpha is uppercase, print A; if it is lowercase, print a.

\n
\n
\n
\n
\n

Constraints

    \n
  • \\alpha is an uppercase (A - Z) or lowercase (a - z) English letter.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
α\n
\n
\n
\n
\n
\n

Output

If \\alpha is uppercase, print A; if it is lowercase, print a.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

B\n
\n
\n
\n
\n
\n

Sample Output 1

A\n
\n

B is uppercase, so we should print A.

\n
\n
\n
\n
\n
\n

Sample Input 2

a\n
\n
\n
\n
\n
\n

Sample Output 2

a\n
\n

a is lowercase, so we should print a.

\n
\n
", "c_code": "int solution() {\n\n char str[2];\n char *pt = &str[0];\n\n scanf(\"%s\", str);\n\n if ('A' <= *pt && *pt <= 'Z') {\n printf(\"A\\n\");\n } else if ('a' <= *pt && *pt <= 'z') {\n printf(\"a\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n is.pop();\n println!(\n \"{}\",\n if is.chars().all(|c| c.is_ascii_uppercase()) {\n 'A'\n } else {\n 'a'\n }\n );\n}", "difficulty": "medium"} {"problem_id": "1741", "problem_description": "You are given an array $$$a_1, a_2, \\ldots, a_n$$$.In one operation you can choose two elements $$$a_i$$$ and $$$a_j$$$ ($$$i \\ne j$$$) and decrease each of them by one.You need to check whether it is possible to make all the elements equal to zero or not.", "c_code": "int solution() {\n long long int Arraylength = 0;\n long long int i = 0;\n long long int num = 0;\n long long int sum = 0;\n long long int ara[100001] = {0};\n scanf(\"%lld\", &Arraylength);\n for (i = 0; i < Arraylength; i++) {\n scanf(\"%lld\", &num);\n ara[i] = num;\n sum += num;\n }\n for (i = 0; i < Arraylength; i++) {\n if (ara[i] > (sum / 2)) {\n printf(\"NO\");\n return 0;\n }\n }\n if (sum % 2 == 0) {\n printf(\"YES\");\n } else {\n printf(\"NO\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.clear();\n std::io::stdin().read_line(&mut buf).unwrap();\n\n let mut sum: u64 = 0;\n let mut max: u64 = 0;\n let numbers = buf.split_whitespace().map(|x| x.parse::().unwrap());\n for x in numbers {\n max = std::cmp::max(x, max);\n sum += x;\n }\n let is_zeroable = sum.is_multiple_of(2) && max * 2 <= sum;\n\n println!(\"{}\", if is_zeroable { \"YES\" } else { \"NO\" });\n}", "difficulty": "medium"} {"problem_id": "1742", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Given is an integer N.

\n

Takahashi chooses an integer a from the positive integers not greater than N with equal probability.

\n

Find the probability that a is odd.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the probability that a is odd.\nYour output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-6}.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n
\n
\n
\n
\n
\n

Sample Output 1

0.5000000000\n
\n

There are four positive integers not greater than 4: 1, 2, 3, and 4. Among them, we have two odd numbers: 1 and 3. Thus, the answer is \\frac{2}{4} = 0.5.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n
\n
\n
\n
\n
\n

Sample Output 2

0.6000000000\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1\n
\n
\n
\n
\n
\n

Sample Output 3

1.0000000000\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n scanf(\"%d\", &N);\n if (N % 2 == 0) {\n printf(\"%f\\n\", 0.5);\n } else {\n double a = N;\n printf(\"%f\\n\", (a + 1) / (2 * a));\n }\n return 0;\n}", "rust_code": "fn solution() {\n let _stdin: String = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s.trim_end().to_owned()\n };\n let n: f32 = _stdin.parse().unwrap();\n if n % 2. == 0.0 {\n println!(\"{}\", 1. - (n / 2. / n));\n } else {\n println!(\"{}\", 1. - ((n - 1.) / 2. / n));\n }\n}", "difficulty": "medium"} {"problem_id": "1743", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

\n

As a Japanese saying goes, these dogs and monkeys are on bad terms. (\"ken'en no naka\", literally \"the relationship of dogs and monkeys\", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

\n

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).\nHere, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N,M ≤ 10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\n
\n
\n
\n
\n
\n

Output

Print the number of possible arrangements, modulo 10^9+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 2\n
\n
\n
\n
\n
\n

Sample Output 2

12\n
\n
\n
\n
\n
\n
\n

Sample Input 3

1 8\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
\n
\n
\n
\n

Sample Input 4

100000 100000\n
\n
\n
\n
\n
\n

Sample Output 4

530123477\n
\n
\n
", "c_code": "int solution(void) {\n int d;\n int m;\n unsigned long long int p = 1;\n scanf(\"%d%d\", &d, &m);\n switch (d - m) {\n case 0:\n for (int i = 1; i <= d; i++) {\n p = p * i;\n p = p % 1000000007;\n }\n p = p * p;\n p = p % 1000000007;\n p = p * 2;\n break;\n case -1:\n for (int i = 1; i <= d; i++) {\n p = p * i;\n p = p % 1000000007;\n }\n p = p * p;\n p = p % 1000000007;\n p = p * m;\n break;\n case 1:\n for (int i = 1; i <= m; i++) {\n p = p * i;\n p = p % 1000000007;\n }\n p = p * p;\n p = p % 1000000007;\n p = p * d;\n break;\n default:\n p = 0;\n break;\n }\n p = p % 1000000007;\n printf(\"%llu\", p);\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 v: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let d: usize = std::cmp::max(v[0], v[1]) - std::cmp::min(v[0], v[1]);\n if d > 1 {\n println!(\"0\");\n return;\n }\n const Q: usize = 1000000007;\n let mut x = 1;\n let mut y = 1;\n for i in 1..v[0] + 1 {\n x = (x * i) % Q;\n }\n for i in 1..v[1] + 1 {\n y = (y * i) % Q;\n }\n println!(\n \"{}\",\n if d == 1 {\n (x * y) % Q\n } else {\n (2 * ((x * y) % Q)) % Q\n }\n );\n}", "difficulty": "medium"} {"problem_id": "1744", "problem_description": "Three friends are going to meet each other. Initially, the first friend stays at the position $$$x = a$$$, the second friend stays at the position $$$x = b$$$ and the third friend stays at the position $$$x = c$$$ on the coordinate axis $$$Ox$$$.In one minute each friend independently from other friends can change the position $$$x$$$ by $$$1$$$ to the left or by $$$1$$$ to the right (i.e. set $$$x := x - 1$$$ or $$$x := x + 1$$$) or even don't change it.Let's introduce the total pairwise distance — the sum of distances between each pair of friends. Let $$$a'$$$, $$$b'$$$ and $$$c'$$$ be the final positions of the first, the second and the third friend, correspondingly. Then the total pairwise distance is $$$|a' - b'| + |a' - c'| + |b' - c'|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.Friends are interested in the minimum total pairwise distance they can reach if they will move optimally. Each friend will move no more than once. So, more formally, they want to know the minimum total pairwise distance they can reach after one minute.You have to answer $$$q$$$ independent test cases.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n for (int k = 0; k < n; k++) {\n int a[3];\n int x;\n int s = 0;\n scanf(\"%d%d%d\", &a[0], &a[1], &a[2]);\n for (int i = 0; i < 3 - 1; i++) {\n for (int j = 0; j < 3 - i - 1; j++) {\n if (a[j] > a[j + 1]) {\n x = a[j];\n a[j] = a[j + 1];\n a[j + 1] = x;\n }\n }\n }\n if (a[0] < a[1] && a[1] < a[2]) {\n a[0] += 1;\n a[2] -= 1;\n } else if (a[0] == a[1] && a[1] < a[2]) {\n a[0] += 1;\n a[1] += 1;\n if (a[1] < a[2]) {\n a[2] -= 1;\n }\n } else if (a[0] < a[1] && a[1] == a[2]) {\n a[1] -= 1;\n a[2] -= 1;\n if (a[0] < a[1]) {\n a[0] += 1;\n }\n }\n s = a[1] - a[0] + a[2] - a[1] + a[2] - a[0];\n printf(\"%d\\n\", s);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _b1 = stdin().read_line(&mut str).unwrap();\n let N: i32 = str.trim().parse().unwrap();\n for _test in 0..N {\n let mut str = String::new();\n let _b1 = stdin().read_line(&mut str).unwrap();\n let mut iter = str.split_whitespace();\n let a: i64 = iter.next().unwrap().parse().unwrap();\n let b: i64 = iter.next().unwrap().parse().unwrap();\n let c: i64 = iter.next().unwrap().parse().unwrap();\n let m = min(min(a, b), c);\n let M = max(max(a, b), c);\n println!(\"{}\", max(2 * (M - m - 2), 0));\n }\n}", "difficulty": "hard"} {"problem_id": "1745", "problem_description": "A and B are preparing themselves for programming contests.B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake.However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared — the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change.Can you help B find out exactly what two errors he corrected?", "c_code": "int solution() {\n int n = 0;\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int i = 0;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &d), a += d;\n }\n for (i = 0; i < n - 1; i++) {\n scanf(\"%d\", &d), b += d;\n }\n for (i = 0; i < n - 2; i++) {\n scanf(\"%d\", &d), c += d;\n }\n printf(\"%d\\n%d\", a - b, b - c);\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n\n io::stdin().read_line(&mut n).unwrap();\n\n let _n: i64 = n.trim().parse().unwrap();\n\n let mut line = String::new();\n\n io::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\n io::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 line = String::new();\n\n io::stdin().read_line(&mut line).unwrap();\n\n let c: Vec = line\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect();\n\n let mut errors_first = HashMap::new();\n\n for item in &a {\n let count = errors_first.entry(item).or_insert(0);\n *count += 1;\n }\n\n let mut errors_second = HashMap::new();\n for item in &b {\n let count = errors_second.entry(item).or_insert(0);\n *count += 1;\n }\n\n for (key, value) in &errors_first {\n match errors_second.get(key) {\n Some(item) => {\n if item != value {\n println!(\"{}\", key);\n }\n }\n None => println!(\"{}\", key),\n }\n }\n\n let mut errors_third = HashMap::new();\n for item in &c {\n let count = errors_third.entry(item).or_insert(0);\n *count += 1;\n }\n\n for (key, value) in &errors_second {\n match errors_third.get(key) {\n Some(item) => {\n if item != value {\n println!(\"{}\", key);\n }\n }\n None => println!(\"{}\", key),\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1746", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

\n

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

\n

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

\n

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

\n

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

\n

We can prove that this value is uniquely determined.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 200
  • \n
  • -100 \\leq L \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N L\n
\n
\n
\n
\n
\n

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2\n
\n
\n
\n
\n
\n

Sample Output 1

18\n
\n

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 -1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

\n
\n
\n
\n
\n
\n

Sample Input 3

30 -50\n
\n
\n
\n
\n
\n

Sample Output 3

-1044\n
\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n int L = 0;\n int sweet = 0;\n int pie = 0;\n int i = 1;\n int j = 1;\n\n scanf(\"%d %d\", &N, &L);\n\n sweet = L * L;\n\n for (i = 2; i <= N; i++) {\n if (sweet > (L + i - 1) * (L + i - 1)) {\n sweet = (L + i - 1) * (L + i - 1);\n j = i;\n }\n }\n\n for (i = 1; i <= N; i++) {\n pie += L + i - 1;\n }\n\n pie -= L + j - 1;\n\n printf(\"%d\", pie);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut x = String::new();\n stdin().read_line(&mut x).unwrap();\n let x: Vec = x.split_whitespace().flat_map(str::parse).collect();\n let (n, l) = (x[0], x[1]);\n\n let mut tastes: Vec<(isize, isize)> = (1..n + 1)\n .map(|i| l + i - 1)\n .map(|i| (if i < 0 { -i } else { i }, i))\n .collect();\n tastes.sort();\n println!(\n \"{}\",\n tastes.into_iter().skip(1).map(|(_, i)| i).sum::()\n );\n}", "difficulty": "hard"} {"problem_id": "1747", "problem_description": "Vasya lives in a strange world. The year has n months and the i-th month has ai days. Vasya got a New Year present — the clock that shows not only the time, but also the date.The clock's face can display any number from 1 to d. It is guaranteed that ai ≤ d for all i from 1 to n. The clock does not keep information about the current month, so when a new day comes, it simply increases the current day number by one. The clock cannot display number d + 1, so after day number d it shows day 1 (the current day counter resets). The mechanism of the clock allows you to increase the day number by one manually. When you execute this operation, day d is also followed by day 1.Vasya begins each day checking the day number on the clock. If the day number on the clock does not match the actual day number in the current month, then Vasya manually increases it by one. Vasya is persistent and repeats this operation until the day number on the clock matches the actual number of the current day in the current month.A year passed and Vasya wonders how many times he manually increased the day number by one, from the first day of the first month to the last day of the n-th month inclusive, considering that on the first day of the first month the clock display showed day 1.", "c_code": "int solution() {\n int d;\n scanf(\"%d\", &d);\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int i = 0;\n for (; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int ans = 0;\n for (i = 0; i < n - 1; i++) {\n ans += d - a[i];\n }\n printf(\"%d\", ans);\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n\n let mut s = String::new();\n stdin.read_line(&mut s).unwrap();\n let d = s.trim().parse::().unwrap();\n\n s = String::new();\n stdin.read_line(&mut s).unwrap();\n let _n = s.trim().parse::().unwrap();\n\n s = String::new();\n stdin.read_line(&mut s).unwrap();\n let a: Vec = s.trim().split(\" \").map(|x| x.parse().unwrap()).collect();\n\n let mut sum = 0;\n for i in 0..a.len() - 1 {\n sum += d - a[i];\n }\n\n println!(\"{}\", sum);\n}", "difficulty": "easy"} {"problem_id": "1748", "problem_description": "Monocarp and Polycarp are learning new programming techniques. Now they decided to try pair programming.It's known that they have worked together on the same file for $$$n + m$$$ minutes. Every minute exactly one of them made one change to the file. Before they started, there were already $$$k$$$ lines written in the file.Every minute exactly one of them does one of two actions: adds a new line to the end of the file or changes one of its lines.Monocarp worked in total for $$$n$$$ minutes and performed the sequence of actions $$$[a_1, a_2, \\dots, a_n]$$$. If $$$a_i = 0$$$, then he adds a new line to the end of the file. If $$$a_i > 0$$$, then he changes the line with the number $$$a_i$$$. Monocarp performed actions strictly in this order: $$$a_1$$$, then $$$a_2$$$, ..., $$$a_n$$$.Polycarp worked in total for $$$m$$$ minutes and performed the sequence of actions $$$[b_1, b_2, \\dots, b_m]$$$. If $$$b_j = 0$$$, then he adds a new line to the end of the file. If $$$b_j > 0$$$, then he changes the line with the number $$$b_j$$$. Polycarp performed actions strictly in this order: $$$b_1$$$, then $$$b_2$$$, ..., $$$b_m$$$.Restore their common sequence of actions of length $$$n + m$$$ such that all actions would be correct — there should be no changes to lines that do not yet exist. Keep in mind that in the common sequence Monocarp's actions should form the subsequence $$$[a_1, a_2, \\dots, a_n]$$$ and Polycarp's — subsequence $$$[b_1, b_2, \\dots, b_m]$$$. They can replace each other at the computer any number of times.Let's look at an example. Suppose $$$k = 3$$$. Monocarp first changed the line with the number $$$2$$$ and then added a new line (thus, $$$n = 2, \\: a = [2, 0]$$$). Polycarp first added a new line and then changed the line with the number $$$5$$$ (thus, $$$m = 2, \\: b = [0, 5]$$$).Since the initial length of the file was $$$3$$$, in order for Polycarp to change line number $$$5$$$ two new lines must be added beforehand. Examples of correct sequences of changes, in this case, would be $$$[0, 2, 0, 5]$$$ and $$$[2, 0, 0, 5]$$$. Changes $$$[0, 0, 5, 2]$$$ (wrong order of actions) and $$$[0, 5, 2, 0]$$$ (line $$$5$$$ cannot be edited yet) are not correct.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int k;\n int n;\n int m;\n scanf(\"%d %d %d\\n\", &k, &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 x = 0;\n int y = 0;\n int steps[n + m];\n int limexc = 0;\n while (!(x == n && y == m)) {\n if (x < n && a[x] == 0) {\n steps[x + y] = 0;\n x++;\n k++;\n } else if (y < m && b[y] == 0) {\n steps[x + y] = 0;\n y++;\n k++;\n } else if (x < n && a[x] <= k) {\n steps[x + y] = a[x];\n x++;\n } else if (y < m && b[y] <= k) {\n steps[x + y] = b[y];\n y++;\n } else {\n limexc = 1;\n break;\n }\n }\n if (!limexc) {\n for (int i = 0; i < n + m; i++) {\n printf(\"%d \", steps[i]);\n }\n } else {\n printf(\"-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\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 'cases: for _case_index in 1..=t {\n lines.next();\n\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let mut k = it.next().unwrap();\n let n = it.next().unwrap();\n let m = it.next().unwrap();\n\n let s = lines.next().unwrap();\n let mut it1 = s\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .peekable();\n\n let s = lines.next().unwrap();\n let mut it2 = s.split_whitespace().map(|s| s.parse::().unwrap());\n\n let mut ans = Vec::with_capacity(n + m);\n\n while ans.len() < n + m {\n let a1 = it1.peek().cloned();\n match a1 {\n Some(a1) if a1 <= k => {\n if a1 == 0 {\n k += 1;\n }\n it1.next();\n ans.push(a1);\n }\n _ => match it2.next() {\n Some(a2) if a2 <= k => {\n if a2 == 0 {\n k += 1;\n }\n ans.push(a2);\n }\n _ => {\n writeln!(&mut output, \"-1\").unwrap();\n continue 'cases;\n }\n },\n }\n }\n\n for x in ans {\n write!(&mut output, \"{} \", x).unwrap();\n }\n\n writeln!(&mut output).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1749", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Takahashi is playing a board game called Sugoroku.

\n

On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at Square 0, and he has to stop exactly at Square N to win the game.

\n

The game uses a roulette with the M numbers from 1 to M. In each turn, Takahashi spins the roulette. If the number x comes up when he is at Square s, he moves to Square s+x. If this makes him go beyond Square N, he loses the game.

\n

Additionally, some of the squares are Game Over Squares. He also loses the game if he stops at one of those squares. You are given a string S of length N + 1, representing which squares are Game Over Squares. For each i (0 \\leq i \\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.

\n

Find the sequence of numbers coming up in the roulette in which Takahashi can win the game in the fewest number of turns possible. If there are multiple such sequences, find the lexicographically smallest such sequence. If Takahashi cannot win the game, print -1.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq M \\leq 10^5
  • \n
  • |S| = N + 1
  • \n
  • S consists of 0 and 1.
  • \n
  • S[0] = 0
  • \n
  • S[N] = 0
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nS\n
\n
\n
\n
\n
\n

Output

If Takahashi can win the game, print the lexicographically smallest sequence among the shortest sequences of numbers coming up in the roulette in which Takahashi can win the game, with spaces in between.

\n

If Takahashi cannot win the game, print -1.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

9 3\n0001000100\n
\n
\n
\n
\n
\n

Sample Output 1

1 3 2 3\n
\n

If the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9 via Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and this is the lexicographically smallest sequence in which he reaches Square 9 in four turns.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 4\n011110\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

Takahashi cannot reach Square 5.

\n
\n
\n
\n
\n
\n

Sample Input 3

6 6\n0101010\n
\n
\n
\n
\n
\n

Sample Output 3

6\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int M;\n char S[100002];\n scanf(\"%d %d\", &N, &M);\n scanf(\"%s\", S);\n\n int j;\n int k;\n int dice[100001];\n for (i = N, k = 0; i > 0;) {\n for (j = (i < M) ? i : M; j >= 1; j--) {\n if (S[i - j] == '0') {\n break;\n }\n }\n if (j == 0) {\n break;\n }\n dice[k++] = j;\n i -= j;\n }\n\n if (i > 0) {\n printf(\"-1\\n\");\n } else {\n for (i = k - 1; i >= 1; i--) {\n printf(\"%d \", dice[i]);\n }\n printf(\"%d\\n\", dice[0]);\n }\n fflush(stdout);\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 m: usize = itr.next().unwrap().parse().unwrap();\n let mut s: Vec = itr.next().unwrap().chars().collect();\n s.reverse();\n\n let mut used = vec![false; n + 1];\n let mut q = std::collections::VecDeque::new();\n let mut ans = Vec::new();\n q.push_back(0);\n while let Some(v) = q.pop_front() {\n for nv in (1..m + 1).rev() {\n if v + nv <= n && s[v + nv] != '1' && !used[v + nv] {\n used[nv + v] = true;\n ans.push(nv);\n q.push_back(nv + v);\n break;\n }\n }\n }\n if used[n] {\n ans.reverse();\n let mut out = Vec::new();\n for i in 0..ans.len() {\n write!(out, \"{}\", ans[i]).ok();\n if i == ans.len() - 1 {\n writeln!(out).ok();\n } else {\n write!(out, \" \").ok();\n }\n }\n stdout().write_all(&out).unwrap();\n } else {\n println!(\"-1\");\n }\n}", "difficulty": "medium"} {"problem_id": "1750", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Determine if we can choose K different integers between 1 and N (inclusive) so that no two of them differ by 1.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1\\leq N,K\\leq 100
  • \n
  • N and K are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

If we can choose K integers as above, print YES; otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

We can choose 1 and 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 5\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 3

31 10\n
\n
\n
\n
\n
\n

Sample Output 3

YES\n
\n
\n
\n
\n
\n
\n

Sample Input 4

10 90\n
\n
\n
\n
\n
\n

Sample Output 4

NO\n
\n
\n
", "c_code": "int solution() {\n int n;\n int N = 0;\n int K = 0;\n scanf(\"%d%d\", &N, &K);\n if (N % 2 != 0) {\n n = (N + 1) / 2;\n } else {\n n = N / 2;\n }\n if (n >= K) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let vs: Vec = s\n .split_whitespace()\n .map(|token| token.parse().ok().unwrap())\n .collect();\n let n = vs[0];\n let k = vs[1];\n if n.div_ceil(2) >= k {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n}", "difficulty": "medium"} {"problem_id": "1751", "problem_description": "Given $$$n$$$, find any array $$$a_1, a_2, \\ldots, a_n$$$ of integers such that all of the following conditions hold: $$$1 \\le a_i \\le 10^9$$$ for every $$$i$$$ from $$$1$$$ to $$$n$$$.$$$a_1 < a_2 < \\ldots <a_n$$$For every $$$i$$$ from $$$2$$$ to $$$n$$$, $$$a_i$$$ isn't divisible by $$$a_{i-1}$$$It can be shown that such an array always exists under the constraints of the problem.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n = 0;\n scanf(\"%d\", &n);\n for (int j = 0; j < n; j++) {\n printf(\"%d \", j + 2);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut handle = std::io::BufWriter::new(std::io::stdout());\n let mut test_no = String::new();\n std::io::stdin().read_line(&mut test_no).unwrap();\n let test_no: u8 = test_no.trim().parse::().unwrap();\n for _ in 0..test_no {\n let mut case = String::new();\n std::io::stdin().read_line(&mut case).unwrap();\n let case: u16 = case.trim().parse::().unwrap();\n for i in 0..case - 1 {\n write!(handle, \"{} \", i + 2).ok();\n }\n writeln!(handle, \"{}\", case + 1).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "1752", "problem_description": "Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!Given the string $$$s$$$ representing Jeff's product name and the string $$$c$$$ representing his competitor's product name, find a way to swap at most one pair of characters in $$$s$$$ (that is, find two distinct indices $$$i$$$ and $$$j$$$ and swap $$$s_i$$$ and $$$s_j$$$) such that the resulting new name becomes strictly lexicographically smaller than $$$c$$$, or determine that it is impossible.Note: String $$$a$$$ is strictly lexicographically smaller than string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a proper prefix of $$$b$$$, that is, $$$a$$$ is a prefix of $$$b$$$ such that $$$a \\neq b$$$; There exists an integer $$$1 \\le i \\le \\min{(|a|, |b|)}$$$ such that $$$a_i < b_i$$$ and $$$a_j = b_j$$$ for $$$1 \\le j < i$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n char s[5001];\n scanf(\"%s\", s);\n char c[5001];\n scanf(\"%s\", c);\n int n = strlen(s);\n int m = strlen(c);\n int flag = 1;\n for (int i = 0; i < (n < m ? n : m) && flag; i++) {\n int j;\n if (s[i] < c[i]) {\n flag = 0;\n } else if (s[i] == c[i]) {\n for (j = i + 1; j < n; j++) {\n if (s[j] < s[i]) {\n break;\n }\n }\n if (j < n && s[j] < s[i]) {\n char temp;\n temp = s[j];\n s[j] = s[i];\n s[i] = temp;\n flag = 0;\n }\n } else {\n for (j = n - 1; j > i; j--) {\n if (s[j] < c[i]) {\n break;\n }\n }\n\n if (j > i && s[j] < c[i]) {\n char temp;\n temp = s[j];\n s[j] = s[i];\n s[i] = temp;\n flag = 0;\n } else {\n for (j = n - 1; j > i; j--) {\n if (s[j] == c[i]) {\n break;\n }\n }\n if (j > i && s[j] == c[i]) {\n char temp;\n temp = s[j];\n s[j] = s[i];\n s[i] = temp;\n flag = 0;\n } else {\n flag = 0;\n }\n }\n }\n }\n flag = 1;\n for (int i = 0; i < (n < m ? n : m) && flag; i++) {\n if (s[i] < c[i]) {\n printf(\"%s\\n\", s);\n flag = 0;\n } else if (s[i] > c[i]) {\n puts(\"---\");\n flag = 0;\n }\n }\n if (flag == 1) {\n if (strlen(s) < strlen(c)) {\n printf(\"%s\\n\", s);\n } else {\n puts(\"---\");\n }\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut scan = Scanner::new(stdin.lock());\n let tt: usize = scan.token();\n for _ in 0..tt {\n let mut s: String = scan.token();\n let c: String = scan.token();\n let mut ss = s.clone().char_indices().collect::>();\n ss.sort_by(|a, b| {\n if a.1 < b.1 || (a.1 == b.1 && a.0 < b.0) {\n std::cmp::Ordering::Less\n } else if a.1 == b.1 && a.0 == b.0 {\n std::cmp::Ordering::Equal\n } else {\n std::cmp::Ordering::Greater\n }\n });\n for i in 0..s.len() {\n if s.chars().nth(i).unwrap() > ss[i].1 {\n let mut ans = i;\n let mut j = i + 1;\n while j < ss.len() && ss[j].1 == ss[i].1 {\n ans = j;\n j += 1;\n }\n let mut sv = s.chars().collect::>();\n sv.swap(i, ss[ans].0);\n let ms = sv.iter().collect::();\n s = ms;\n break;\n }\n }\n if s < c {\n println!(\"{}\", s);\n } else {\n println!(\"---\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1753", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

We have a rectangular grid of squares with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.\nOn this grid, there is a piece, which is initially placed at square (s_r,s_c).

\n

Takahashi and Aoki will play a game, where each player has a string of length N.\nTakahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: L, R, U and D.

\n

The game consists of N steps. The i-th step proceeds as follows:

\n
    \n
  • First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.
  • \n
  • Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.
  • \n
\n

Here, to move the piece in the direction of L, R, U and D, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.

\n

Takahashi wants to remove the piece from the grid in one of the N steps.\nAoki, on the other hand, wants to finish the N steps with the piece remaining on the grid.\nDetermine if the piece will remain on the grid at the end of the game when both players play optimally.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq H,W \\leq 2 \\times 10^5
  • \n
  • 2 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq s_r \\leq H
  • \n
  • 1 \\leq s_c \\leq W
  • \n
  • |S|=|T|=N
  • \n
  • S and T consists of the four kinds of letters L, R, U and D.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W N\ns_r s_c\nS\nT\n
\n
\n
\n
\n
\n

Output

If the piece will remain on the grid at the end of the game, print YES; otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 3\n2 2\nRRL\nLUD\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

Here is one possible progress of the game:

\n
    \n
  • Takahashi moves the piece right. The piece is now at (2,3).
  • \n
  • Aoki moves the piece left. The piece is now at (2,2).
  • \n
  • Takahashi does not move the piece. The piece remains at (2,2).
  • \n
  • Aoki moves the piece up. The piece is now at (1,2).
  • \n
  • Takahashi moves the piece left. The piece is now at (1,1).
  • \n
  • Aoki does not move the piece. The piece remains at (1,1).
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

4 3 5\n2 2\nUDRRR\nLLDUD\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n
\n
\n
\n
\n
\n

Sample Input 3

5 6 11\n2 1\nRLDRRUDDLRL\nURRDRLLDLRD\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
", "c_code": "int solution() {\n int h;\n int w;\n int n;\n int sr;\n int sc;\n scanf(\"%d%d%d%d%d\", &h, &w, &n, &sr, &sc);\n char s[n + 1];\n char t[n + 1];\n scanf(\"%s%s\", s, t);\n int i;\n int c_l = sc;\n int c_r = sc;\n int r_u = sr;\n int r_d = sr;\n for (i = 0; i < n; i++) {\n if (s[i] == 'L') {\n c_l--;\n if (c_l < 1) {\n puts(\"NO\");\n return 0;\n }\n } else if (s[i] == 'R') {\n c_r++;\n if (c_r > w) {\n puts(\"NO\");\n return 0;\n }\n } else if (s[i] == 'U') {\n r_u--;\n if (r_u < 1) {\n puts(\"NO\");\n return 0;\n }\n } else if (s[i] == 'D') {\n r_d++;\n if (r_d > h) {\n puts(\"NO\");\n return 0;\n }\n }\n if (t[i] == 'L' && c_r > 1) {\n c_r--;\n } else if (t[i] == 'R' && c_l < w) {\n c_l++;\n } else if (t[i] == 'U' && r_d > 1) {\n r_d--;\n } else if (t[i] == 'D' && r_u < h) {\n r_u++;\n }\n }\n puts(\"YES\");\n return 0;\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 h: usize = itr.next().unwrap().parse().unwrap();\n let w: usize = itr.next().unwrap().parse().unwrap();\n let n: usize = itr.next().unwrap().parse().unwrap();\n let sr: usize = itr.next().unwrap().parse().unwrap();\n let sc: usize = itr.next().unwrap().parse().unwrap();\n let s: Vec = itr.next().unwrap().chars().collect();\n let t: Vec = itr.next().unwrap().chars().collect();\n\n let mut l = sc;\n let mut r = sc;\n for i in 0..n {\n if s[i] == 'L' {\n l -= 1;\n }\n if s[i] == 'R' {\n r += 1;\n }\n if l == 0 || r == w + 1 {\n println!(\"NO\");\n return;\n }\n if t[i] == 'R' && l < w {\n l += 1;\n }\n if t[i] == 'L' && r > 1 {\n r -= 1;\n }\n }\n let mut u = sr;\n let mut d = sr;\n\n for i in 0..n {\n if s[i] == 'U' {\n u -= 1;\n }\n if s[i] == 'D' {\n d += 1;\n }\n if u == 0 || d == h + 1 {\n println!(\"NO\");\n return;\n }\n if t[i] == 'D' && u < h {\n u += 1;\n }\n if t[i] == 'U' && d > 1 {\n d -= 1;\n }\n }\n println!(\"YES\");\n}", "difficulty": "easy"} {"problem_id": "1754", "problem_description": "\n\n\n

Connected Components

\n\n

\n Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network.\n

\n\n

Input

\n\n

\n In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$.\n

\n\n

\n In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other).\n

\n\n

\n In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character.\n

\n\n

Output

\n\n

\n For each query, print \"yes\" if $t$ is reachable from $s$ through the social network, \"no\" otherwise.\n

\n\n

Constraints

\n\n
    \n
  • $2 \\leq n \\leq 100,000$
  • \n
  • $0 \\leq m \\leq 100,000$
  • \n
  • $1 \\leq q \\leq 10,000$
  • \n
\n\n

Sample Input

\n
\n10 9\n0 1\n0 2\n3 4\n5 7\n5 6\n6 7\n6 8\n7 8\n8 9\n3\n0 1\n5 9\n1 3\n
\n\n

Sample Output

\n
\nyes\nyes\nno\n
", "c_code": "int solution() {\n int i;\n int j;\n int n;\n int m;\n int s;\n int t;\n int t1;\n int t2;\n fscanf(stdin, \"%d %d\", &n, &m);\n int *gg[n];\n int gid[m];\n\n for (i = 0; i < n; i++) {\n gg[i] = NULL;\n }\n for (i = 0; i < m; i++) {\n gid[i] = i;\n }\n\n int maxgrp = 0;\n\n for (i = 0; i < m; i++) {\n fscanf(stdin, \"%d %d\", &s, &t);\n if (gg[s] == NULL && gg[t] == NULL) {\n gg[s] = gid + maxgrp;\n gg[t] = gid + maxgrp;\n maxgrp++;\n continue;\n }\n\n if (gg[s] == NULL) {\n gg[s] = gg[t];\n } else if (gg[t] == NULL) {\n gg[t] = gg[s];\n } else {\n if (*gg[s] < *gg[t]) {\n t1 = *gg[s];\n t2 = *gg[t];\n } else {\n t1 = *gg[t];\n t2 = *gg[s];\n }\n for (j = 0; j < maxgrp; j++) {\n if (gid[j] == t2) {\n gid[j] = t1;\n }\n }\n }\n }\n\n fscanf(stdin, \"%d\", &m);\n for (i = 0; i < m; i++) {\n fscanf(stdin, \"%d %d\", &s, &t);\n\n if (gg[s] == NULL || gg[t] == NULL || *gg[s] != *gg[t]) {\n puts(\"no\");\n } else {\n puts(\"yes\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let _ = std::io::stdin().read_line(&mut buf).ok();\n let mut buf = buf.split_whitespace();\n let n: usize = buf.next().unwrap().parse().unwrap();\n let m: usize = buf.next().unwrap().parse().unwrap();\n let mut g = vec![vec![]; n];\n for _ in 0..m {\n let mut buf = String::new();\n let _ = std::io::stdin().read_line(&mut buf).ok();\n let mut buf = buf.split_whitespace();\n let j: usize = buf.next().unwrap().parse().unwrap();\n let k: usize = buf.next().unwrap().parse().unwrap();\n g[k].push(j);\n g[j].push(k);\n }\n let mut colors = vec![-1; n];\n let mut color = 0;\n for i in 0..n {\n let mut s = Vec::new();\n if colors[i] == -1 {\n s.push(i);\n while let Some(p) = s.pop() {\n colors[p] = color;\n for j in &g[p] {\n if colors[*j] == -1 {\n s.push(*j);\n }\n }\n }\n color += 1;\n }\n }\n\n let mut buf = String::new();\n let _ = std::io::stdin().read_line(&mut buf).ok();\n let n: usize = buf.trim().parse().unwrap();\n for _ in 0..n {\n let mut buf = String::new();\n let _ = std::io::stdin().read_line(&mut buf).ok();\n let mut buf = buf.split_whitespace();\n let j: usize = buf.next().unwrap().parse().unwrap();\n let k: usize = buf.next().unwrap().parse().unwrap();\n if colors[j] == colors[k] {\n println!(\"yes\");\n } else {\n println!(\"no\");\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1755", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.

\n

Integers M_1, D_1, M_2, and D_2 will be given as input.
\nIt is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
\nDetermine whether the date 2019-M_1-D_1 is the last day of a month.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
  • \n
  • The date 2019-M_2-D_2 follows 2019-M_1-D_1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
M_1 D_1\nM_2 D_2\n
\n
\n
\n
\n
\n

Output

\n

If the date 2019-M_1-D_1 is the last day of a month, print 1; otherwise, print 0.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

11 16\n11 17\n
\n
\n
\n
\n
\n

Sample Output 1

0\n
\n

November 16 is not the last day of a month.

\n
\n
\n
\n
\n
\n

Sample Input 2

11 30\n12 1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

November 30 is the last day of November.

\n
\n
", "c_code": "int solution(void) {\n\n int M1 = 0;\n int M2 = 0;\n int D1 = 0;\n int D2 = 0;\n\n scanf(\"%d %d\", &M1, &D1);\n scanf(\"%d %d\", &M2, &D2);\n\n if (M1 != M2 && D2 == 1) {\n printf(\"1\\n\");\n } else {\n printf(\"0\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let a: Vec = s\n .split_whitespace()\n .map(|e| e.parse().ok().unwrap())\n .collect();\n\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let b: Vec = s\n .split_whitespace()\n .map(|e| e.parse().ok().unwrap())\n .collect();\n\n println!(\"{}\", if a[1] > b[1] { 1 } else { 0 });\n}", "difficulty": "hard"} {"problem_id": "1756", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

A group of people played a game. All players had distinct scores, which are positive integers.

\n

Takahashi knows N facts on the players' scores. The i-th fact is as follows: the A_i-th highest score among the players is B_i.

\n

Find the maximum possible number of players in the game.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq A_i \\leq 10^9(1\\leq i\\leq N)
  • \n
  • 0 \\leq B_i \\leq 10^9(1\\leq i\\leq N)
  • \n
  • If i ≠ j, A_i ≠ A_j.
  • \n
  • There exists a possible outcome of the game that are consistent with the facts.
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Inputs

Input is given from Standard Input in the following format:

\n
N\nA_1 B_1\n:\nA_N B_N\n
\n
\n
\n
\n
\n

Outputs

Print the maximum possible number of players in the game.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n4 7\n2 9\n6 2\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

The maximum possible number of players is achieved when, for example, the players have the following scores: 12,9,8,7,5,2,1,0.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n1 10\n3 6\n5 2\n4 4\n2 8\n
\n
\n
\n
\n
\n

Sample Output 2

7\n
\n
\n
\n
\n
\n
\n

Sample Input 3

2\n1 1000000000\n1000000000 1\n
\n
\n
\n
\n
\n

Sample Output 3

1000000001\n
\n
\n
", "c_code": "int solution(void) {\n\n long n;\n scanf(\"%ld\", &n);\n long a[n];\n long b[n];\n for (long i = 0; i < n; i++) {\n scanf(\"%ld %ld\", &a[i], &b[i]);\n }\n long max = 0;\n long n_th;\n for (long i = 0; i < n; i++) {\n if (a[i] > max) {\n max = a[i];\n n_th = i;\n }\n }\n printf(\"%ld\\n\", max + b[n_th]);\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let (a, b): (u32, u32) = stdin\n .lock()\n .lines()\n .skip(1)\n .map(|r| {\n r.as_ref()\n .map(|l| {\n let mut s = l.split_whitespace().map(|s| s.parse().unwrap());\n (s.next().unwrap(), s.next().unwrap())\n })\n .unwrap()\n })\n .max_by_key(|&(a, _)| a)\n .unwrap();\n println!(\"{}\", a + b);\n}", "difficulty": "medium"} {"problem_id": "1757", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

We have an N \\times N square grid.

\n

We will paint each square in the grid either black or white.

\n

If we paint exactly A squares white, how many squares will be painted black?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • 0 \\leq A \\leq N^2
  • \n
\n
\n
\n
\n
\n
\n
\n

Inputs

Input is given from Standard Input in the following format:

\n
N\nA\n
\n
\n
\n
\n
\n

Outputs

Print the number of squares that will be painted black.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n4\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

There are nine squares in a 3 \\times 3 square grid.\nFour of them will be painted white, so the remaining five squares will be painted black.

\n
\n
\n
\n
\n
\n

Sample Input 2

19\n100\n
\n
\n
\n
\n
\n

Sample Output 2

261\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10\n0\n
\n
\n
\n
\n
\n

Sample Output 3

100\n
\n

As zero squares will be painted white, all the squares will be painted black.

\n
\n
", "c_code": "int solution() {\n\n int N = 0;\n int A = 0;\n\n scanf(\"%d %d\", &N, &A);\n\n printf(\"%d\", (N * N) - A);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).ok();\n let vec: Vec<&str> = line.split_whitespace().collect();\n let n: i32 = vec[0].parse().unwrap_or(0);\n\n let mut l = String::new();\n io::stdin().read_line(&mut l).ok();\n let v: Vec<&str> = l.split_whitespace().collect();\n let m: i32 = v[0].parse().unwrap_or(0);\n\n println!(\"{}\", n * n - m);\n}", "difficulty": "hard"} {"problem_id": "1758", "problem_description": "You are given $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$. Find the maximum value of $$$max(a_l, a_{l + 1}, \\ldots, a_r) \\cdot min(a_l, a_{l + 1}, \\ldots, a_r)$$$ over all pairs $$$(l, r)$$$ of integers for which $$$1 \\le l < r \\le n$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n long long int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &a[i]);\n }\n long long int ans = 0;\n ans = a[0] * a[1];\n if (n > 2) {\n for (int i = 1; i < n - 1; i++) {\n if ((a[i] * a[i + 1]) > ans) {\n ans = a[i] * a[i + 1];\n }\n }\n }\n printf(\"%lld\\n\", ans);\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(2).step_by(2) {\n let m = l\n .unwrap()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .fold((1, 1), |(p, m), n| (n, m.max(p * n)))\n .1;\n writeln!(o, \"{}\", m).ok();\n }\n}", "difficulty": "hard"} {"problem_id": "1759", "problem_description": "A permutation is a sequence of $$$n$$$ integers from $$$1$$$ to $$$n$$$, in which all numbers occur exactly once. For example, $$$[1]$$$, $$$[3, 5, 2, 1, 4]$$$, $$$[1, 3, 2]$$$ are permutations, and $$$[2, 3, 2]$$$, $$$[4, 3, 1]$$$, $$$[0]$$$ are not.Polycarp was presented with a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$. However, when Polycarp came home, he noticed that in his pocket, the permutation $$$p$$$ had turned into an array $$$q$$$ according to the following rule: $$$q_i = \\max(p_1, p_2, \\ldots, p_i)$$$. Now Polycarp wondered what lexicographically minimal and lexicographically maximal permutations could be presented to him.An array $$$a$$$ of length $$$n$$$ is lexicographically smaller than an array $$$b$$$ of length $$$n$$$ if there is an index $$$i$$$ ($$$1 \\le i \\le n$$$) such that the first $$$i-1$$$ elements of arrays $$$a$$$ and $$$b$$$ are the same, and the $$$i$$$-th element of the array $$$a$$$ is less than the $$$i$$$-th element of the array $$$b$$$. For example, the array $$$a=[1, 3, 2, 3]$$$ is lexicographically smaller than the array $$$b=[1, 3, 4, 2]$$$.For example, if $$$n=7$$$ and $$$p=[3, 2, 4, 1, 7, 5, 6]$$$, then $$$q=[3, 3, 4, 4, 7, 7, 7]$$$ and the following permutations could have been as $$$p$$$ initially: $$$[3, 1, 4, 2, 7, 5, 6]$$$ (lexicographically minimal permutation); $$$[3, 1, 4, 2, 7, 6, 5]$$$; $$$[3, 2, 4, 1, 7, 5, 6]$$$; $$$[3, 2, 4, 1, 7, 6, 5]$$$ (lexicographically maximum permutation). For a given array $$$q$$$, find the lexicographically minimal and lexicographically maximal permutations that could have been originally presented to Polycarp.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n int n;\n for (int m = 0; m < t; m++) {\n scanf(\"%d\", &n);\n int arr[n];\n int max = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &arr[i]);\n if (arr[i] > max) {\n max = arr[i];\n }\n }\n\n int s1[max];\n int s2[max];\n int a[max];\n int p = 1;\n int c = 0;\n int q = c;\n s1[0] = arr[0];\n for (int i = 1; i < arr[0]; i++) {\n a[c] = i;\n c++;\n }\n\n for (int i = 1; i < n; i++) {\n if (arr[i] == arr[i - 1]) {\n s1[p] = a[c - 1];\n p++;\n c--;\n } else {\n s1[p] = arr[i];\n p++;\n for (int k = arr[i - 1] + 1; k < arr[i]; k++) {\n a[c] = k;\n c++;\n }\n }\n }\n\n s2[0] = arr[0];\n int b[max];\n c = 0;\n p = 1;\n q = 0;\n for (int i = 1; i < arr[0]; i++) {\n b[c] = i;\n c++;\n }\n for (int i = 1; i < n; i++) {\n if (arr[i] == arr[i - 1]) {\n s2[p] = b[q];\n q++;\n p++;\n\n } else {\n s2[p] = arr[i];\n p++;\n for (int k = arr[i - 1] + 1; k < arr[i]; k++) {\n b[c] = k;\n c++;\n }\n }\n }\n for (int i = 0; i < p; i++) {\n printf(\"%d \", s2[i]);\n }\n printf(\"\\n\");\n for (int i = 0; i < p; i++) {\n printf(\"%d \", s1[i]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let sin = std::io::stdin();\n let mut s = String::new();\n\n sin.read_line(&mut s).unwrap();\n let mut t = s.trim().parse::().unwrap();\n while t > 0 {\n t -= 1;\n let mut s = String::new();\n sin.read_line(&mut s).unwrap();\n let n = s.trim().parse::().unwrap();\n\n let mut minset = BTreeSet::new();\n\n for i in 1..n {\n minset.insert(i);\n }\n\n let mut maxset = minset.clone();\n\n let mut s = String::new();\n sin.read_line(&mut s).unwrap();\n let v: Vec = s\n .trim()\n .split(\" \")\n .map(|ss| ss.parse::().unwrap())\n .collect();\n\n let mut cur = 0;\n\n let mut mmin = Vec::new();\n let mut mmax = Vec::new();\n for i in v {\n if i != cur {\n mmin.push(i);\n mmax.push(i);\n minset.remove(&i);\n maxset.remove(&i);\n cur = i;\n } else {\n let mn = *minset.range(..cur).next().unwrap();\n mmin.push(mn);\n minset.remove(&mn);\n let mx = *maxset.range(..cur).next_back().unwrap();\n mmax.push(mx);\n maxset.remove(&mx);\n }\n }\n println!(\n \"{}\",\n mmin.iter()\n .map(|i| format!(\"{}\", i))\n .collect::>()\n .join(\" \")\n );\n println!(\n \"{}\",\n mmax.iter()\n .map(|i| format!(\"{}\", i))\n .collect::>()\n .join(\" \")\n );\n }\n}", "difficulty": "medium"} {"problem_id": "1760", "problem_description": "Let's define the value of the permutation $$$p$$$ of $$$n$$$ integers $$$1$$$, $$$2$$$, ..., $$$n$$$ (a permutation is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once) as follows: initially, an integer variable $$$x$$$ is equal to $$$0$$$; if $$$x < p_1$$$, then add $$$p_1$$$ to $$$x$$$ (set $$$x = x + p_1$$$), otherwise assign $$$0$$$ to $$$x$$$; if $$$x < p_2$$$, then add $$$p_2$$$ to $$$x$$$ (set $$$x = x + p_2$$$), otherwise assign $$$0$$$ to $$$x$$$; ... if $$$x < p_n$$$, then add $$$p_n$$$ to $$$x$$$ (set $$$x = x + p_n$$$), otherwise assign $$$0$$$ to $$$x$$$; the value of the permutation is $$$x$$$ at the end of this process. For example, for $$$p = [4, 5, 1, 2, 3, 6]$$$, the value of $$$x$$$ changes as follows: $$$0, 4, 9, 0, 2, 5, 11$$$, so the value of the permutation is $$$11$$$.You are given an integer $$$n$$$. Find a permutation $$$p$$$ of size $$$n$$$ with the maximum possible value among all permutations of size $$$n$$$. If there are several such permutations, you can print any of them.", "c_code": "int solution() {\n int a;\n int n;\n scanf(\"%d\", &n);\n while (n--) {\n scanf(\"%d\", &a);\n int b[a];\n for (int i = 0; i < a; i++) {\n if (i < (a - 5) && i >= 0 && a > 4) {\n printf(\"%d \", i + 4);\n }\n }\n if (a > 4) {\n printf(\"2 3 1 %d %d\\n\", a - 1, a);\n } else {\n printf(\"2 1 3 4\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut lines = io::stdin().lines();\n\n let t: i32 = lines.next().unwrap().unwrap().trim().parse().unwrap();\n for _ in 0..t {\n let n: i32 = lines.next().unwrap().unwrap().trim().parse().unwrap();\n if n % 2 == 0 {\n for i in (3..n - 1).rev() {\n print!(\"{i} \")\n }\n print!(\"{} {} {} {}\", 2, 1, n - 1, n);\n } else {\n for i in (4..n - 1).rev() {\n print!(\"{i} \")\n }\n print!(\"{} {} {} {} {}\", 1, 2, 3, n - 1, n);\n }\n println!()\n }\n}", "difficulty": "medium"} {"problem_id": "1761", "problem_description": "\n

Score: 500 points

\n
\n
\n

Problem Statement

\n

In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \\dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)

\n

To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.

\n

What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?

\n

Assume that you have sufficient numbers of banknotes, and so does the clerk.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • N is an integer between 1 and 10^{1,000,000} (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

\n

Print the minimum possible number of total banknotes used by you and the clerk.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

36\n
\n
\n
\n
\n
\n

Sample Output 1

8\n
\n

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.

\n

The payment cannot be made with less than eight banknotes in total, so the answer is 8.

\n
\n
\n
\n
\n
\n

Sample Input 2

91\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n

If you give two banknotes of value 100, 1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.

\n
\n
\n
\n
\n
\n

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170\n
\n
\n
\n
\n
\n

Sample Output 3

243\n
\n
\n
", "c_code": "int solution(void) {\n\n char s[1000000 + 10];\n scanf(\"%s\", s);\n int length = strlen(s);\n int num;\n long bill_a = 0;\n long bill_b = 1;\n long case1;\n long case2;\n long case3;\n long case4;\n for (int i = 0; i < length; i++) {\n num = s[i] - '0';\n case1 = bill_a + num;\n case2 = bill_b + 10 - num;\n case3 = bill_a + (num + 1);\n case4 = bill_b + 10 - (num + 1);\n bill_a = case1;\n if (case2 < bill_a) {\n bill_a = case2;\n }\n bill_b = case3;\n if (case4 < bill_b) {\n bill_b = case4;\n }\n }\n printf(\"%ld\\n\", bill_a);\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 s: Vec = s\n .trim()\n .chars()\n .map(|c| c.to_digit(10).unwrap() as i64)\n .collect();\n let mut dp: Vec = [0, 1].to_vec();\n for &c in s.iter() {\n let tmp1 = std::cmp::min(dp[0] + c, dp[1] + 10 - c);\n let tmp2 = std::cmp::min(dp[0] + c + 1, dp[1] + 10 - (c + 1));\n dp[0] = tmp1;\n dp[1] = tmp2;\n }\n println!(\"{}\", dp[0]);\n}", "difficulty": "medium"} {"problem_id": "1762", "problem_description": "\n

Score: 400 points

\n
\n
\n

Problem Statement

Amidakuji is a traditional method of lottery in Japan.

\n

To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical line.

\n

A valid amidakuji is an amidakuji that satisfies the following conditions:

\n
    \n
  • No two horizontal lines share an endpoint.
  • \n
  • The two endpoints of each horizontal lines must be at the same height.
  • \n
  • A horizontal line must connect adjacent vertical lines.
  • \n
\n

\"\"

\n

Find the number of the valid amidakuji that satisfy the following condition, modulo 1\\ 000\\ 000\\ 007: if we trace the path from the top of the leftmost vertical line to the bottom, always following horizontal lines when we encounter them, we reach the bottom of the K-th vertical line from the left.

\n

For example, in the following amidakuji, we will reach the bottom of the fourth vertical line from the left.

\n

\"\"

\n
\n
\n
\n
\n

Constraints

    \n
  • H is an integer between 1 and 100 (inclusive).
  • \n
  • W is an integer between 1 and 8 (inclusive).
  • \n
  • K is an integer between 1 and W (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W K\n
\n
\n
\n
\n
\n

Output

Print the number of the amidakuji that satisfy the condition, modulo 1\\ 000\\ 000\\ 007.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 3 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Only the following one amidakuji satisfies the condition:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 2

1 3 1\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

Only the following two amidakuji satisfy the condition:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 3

2 3 3\n
\n
\n
\n
\n
\n

Sample Output 3

1\n
\n

Only the following one amidakuji satisfies the condition:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 4

2 3 1\n
\n
\n
\n
\n
\n

Sample Output 4

5\n
\n

Only the following five amidakuji satisfy the condition:

\n

\"\"

\n
\n
\n
\n
\n
\n

Sample Input 5

7 1 1\n
\n
\n
\n
\n
\n

Sample Output 5

1\n
\n

As there is only one vertical line, we cannot draw any horizontal lines. Thus, there is only one amidakuji that satisfies the condition: the amidakuji with no horizontal lines.

\n
\n
\n
\n
\n
\n

Sample Input 6

15 8 5\n
\n
\n
\n
\n
\n

Sample Output 6

437760187\n
\n

Be sure to print the answer modulo 1\\ 000\\ 000\\ 007.

\n
\n
", "c_code": "int solution() {\n long long int h;\n long long int w;\n long long int k;\n long long int memo;\n long long int result;\n long long int two[10] = {1, 2, 3, 5, 8, 13, 21, 34, 55, 256, 512};\n scanf(\"%lld%lld%lld\", &h, &w, &k);\n long long int s[105][10] = {};\n s[h + 1][k] = 1;\n for (int i = h; i >= 1; i--) {\n for (int j = 1; j <= w; j++) {\n memo = 0;\n if (j == 1 || j == w) {\n memo += two[w - 2] * s[i + 1][j];\n } else {\n memo += (two[j - 2] * two[w - j - 1]) * s[i + 1][j];\n }\n\n if (j == 1 || j == w - 1) {\n memo += two[w - 3] * s[i + 1][j + 1];\n } else if (j == w) {\n\n } else {\n memo += (two[j - 2] * two[w - j - 2]) * s[i + 1][j + 1];\n }\n\n if (j == 1) {\n\n } else if (j == w || j == 2) {\n memo += two[w - 3] * s[i + 1][j - 1];\n } else {\n memo += (two[j - 3] * two[w - j - 1]) * s[i + 1][j - 1];\n }\n memo %= 1000000007;\n s[i][j] = memo;\n }\n }\n printf(\"%lld\", s[1][1]);\n}", "rust_code": "fn solution() {\n input! {\n h: usize,\n w: usize,\n k: usize\n }\n let d = 1000000007;\n let fib = vec![0, 1, 1, 2, 3, 5, 8, 13, 21, 34];\n let mut v: Vec> = vec![vec![0; w + 2]; h + 1];\n v[0][1] = 1;\n for i in 0..h {\n for j in 1..w + 1 {\n v[i + 1][j] = (v[i][j] * fib[j] % d * fib[w - j + 1] % d\n + v[i][j - 1] * fib[j - 1] % d * fib[w - j + 1] % d\n + v[i][j + 1] * fib[j] % d * fib[w - j] % d)\n % d;\n }\n }\n println!(\"{}\", v[h][k]);\n}", "difficulty": "easy"} {"problem_id": "1763", "problem_description": "You are given two integers $$$n$$$ and $$$k$$$. You are asked to choose maximum number of distinct integers from $$$1$$$ to $$$n$$$ so that there is no subset of chosen numbers with sum equal to $$$k$$$.A subset of a set is a set that can be obtained from initial one by removing some (possibly all or none) elements of it.", "c_code": "int solution() {\n int n;\n int k;\n int t;\n int count = 0;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d%d\", &n, &k);\n int a[1200] = {0};\n count = n - 1;\n a[k] = 1;\n for (int i = 1; i < k; i++) {\n if (a[k - i] == 0 && k - i != i) {\n a[i] = 1;\n count--;\n }\n }\n printf(\"%d\\n\", count);\n for (int i = 1; i <= n; i++) {\n if (a[i] == 0) {\n printf(\"%d \", i);\n }\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n\n let mut it = input\n .split_whitespace()\n .map(|x| x.parse::().unwrap());\n\n let _t = it.next().unwrap();\n\n while let Some(n) = it.next() {\n let k = it.next().unwrap();\n\n let p = k.div_ceil(2);\n println!(\"{}\", n - p);\n for x in p..=n {\n if x != k {\n print!(\"{} \", x);\n }\n }\n println!();\n }\n}", "difficulty": "medium"} {"problem_id": "1764", "problem_description": "Let's denote correct match equation (we will denote it as CME) an equation $$$a + b = c$$$ there all integers $$$a$$$, $$$b$$$ and $$$c$$$ are greater than zero.For example, equations $$$2 + 2 = 4$$$ (||+||=||||) and $$$1 + 2 = 3$$$ (|+||=|||) are CME but equations $$$1 + 2 = 4$$$ (|+||=||||), $$$2 + 2 = 3$$$ (||+||=|||), and $$$0 + 1 = 1$$$ (+|=|) are not.Now, you have $$$n$$$ matches. You want to assemble a CME using all your matches. Unfortunately, it is possible that you can't assemble the CME using all matches. But you can buy some extra matches and then assemble CME!For example, if $$$n = 2$$$, you can buy two matches and assemble |+|=||, and if $$$n = 5$$$ you can buy one match and assemble ||+|=|||. Calculate the minimum number of matches which you have to buy for assembling CME.Note, that you have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int q;\n scanf(\"%d\", &q);\n int n[q];\n for (int i = 0; i < q; i++) {\n scanf(\"%d\", &n[i]);\n }\n for (int i = 0; i < q; i++) {\n if (n[i] == 2) {\n printf(\"2\\n\");\n }\n if (n[i] % 2 == 0 && n[i] != 2) {\n printf(\"0\\n\");\n }\n if (n[i] % 2 == 1) {\n printf(\"1\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let q: u8 = buffer.trim().parse().expect(\"invalid input\");\n\n for _ in 0..q {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let n: u32 = input.split_whitespace().next().unwrap().parse().unwrap();\n println!(\"{}\", if n < 4 { 4 - n } else { n % 2 });\n }\n}", "difficulty": "medium"} {"problem_id": "1765", "problem_description": "Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n char s[100];\n scanf(\"%d%d %s\", &n, &k, s);\n int equal = 1;\n for (int i = 0; i < n; i++) {\n if (s[i] != s[n - 1 - i]) {\n equal = 0;\n }\n }\n if (k == 0) {\n printf(\"1\\n\");\n continue;\n }\n if (equal) {\n printf(\"1\\n\");\n } else {\n printf(\"2\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut n_cases = String::new();\n io::stdin()\n .read_line(&mut n_cases)\n .expect(\"Failed to read input\");\n let mut n_cases: i8 = n_cases.trim().parse().expect(\"Invalid input\");\n\n loop {\n if n_cases == 0 {\n break;\n } else {\n n_cases -= 1\n };\n\n let mut case_input = String::new();\n io::stdin()\n .read_line(&mut case_input)\n .expect(\"Failed to read input\");\n let case_input: Vec = case_input\n .split_whitespace()\n .map(|x| x.parse().expect(\"Invalid input\"))\n .collect();\n\n let mut input_str = String::new();\n io::stdin()\n .read_line(&mut input_str)\n .expect(\"Failed to read input\");\n let input_str = input_str.trim();\n\n match case_input.last().unwrap() == &0\n || input_str == input_str.chars().rev().collect::()\n {\n true => println!(\"{}\", 1),\n false => println!(\"{}\", 2),\n };\n }\n}", "difficulty": "hard"} {"problem_id": "1766", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

A 3×3 grid with a integer written in each square, is called a magic square if and only if the integers in each row, the integers in each column, and the integers in each diagonal (from the top left corner to the bottom right corner, and from the top right corner to the bottom left corner), all add up to the same sum.

\n

You are given the integers written in the following three squares in a magic square:

\n
    \n
  • The integer A at the upper row and left column
  • \n
  • The integer B at the upper row and middle column
  • \n
  • The integer C at the middle row and middle column
  • \n
\n

Determine the integers written in the remaining squares in the magic square.

\n

It can be shown that there exists a unique magic square consistent with the given information.

\n
\n
\n
\n
\n

Constraints

    \n
  • 0 \\leq A, B, C \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
A\nB\nC\n
\n
\n
\n
\n
\n

Output

Output the integers written in the magic square, in the following format:

\n
X_{1,1} X_{1,2} X_{1,3}\nX_{2,1} X_{2,2} X_{2,3}\nX_{3,1} X_{3,2} X_{3,3}\n
\n

where X_{i,j} is the integer written in the square at the i-th row and j-th column.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8\n3\n5\n
\n
\n
\n
\n
\n

Sample Output 1

8 3 4\n1 5 9\n6 7 2\n
\n
\n
\n
\n
\n
\n

Sample Input 2

1\n1\n1\n
\n
\n
\n
\n
\n

Sample Output 2

1 1 1\n1 1 1\n1 1 1\n
\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n int b = 0;\n int c = 0;\n int d = 0;\n int e = 0;\n int f = 0;\n int g = 0;\n int h = 0;\n int M = 0;\n\n scanf(\"%d %d %d\", &a, &b, &M);\n\n g = 2 * M - b;\n h = 2 * M - a;\n f = 3 * M - g - h;\n c = 2 * M - f;\n e = 3 * M - c - h;\n d = 2 * M - e;\n\n printf(\"%d %d %d\\n\", a, b, c);\n printf(\"%d %d %d\\n\", d, M, e);\n printf(\"%d %d %d\\n\", f, g, h);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let a: i32 = s.next().unwrap().parse().unwrap();\n let b: i32 = s.next().unwrap().parse().unwrap();\n let c: i32 = s.next().unwrap().parse().unwrap();\n let s = 3 * c;\n println!(\"{} {} {}\", a, b, -a - b + s);\n println!(\"{} {} {}\", 2 * s - 2 * a - b - 2 * c, c, 2 * a + b + c - s);\n println!(\"{} {} {}\", a + b + 2 * c - s, s - b - c, s - a - c);\n}", "difficulty": "easy"} {"problem_id": "1767", "problem_description": "You have a statistic of price changes for one product represented as an array of $$$n$$$ positive integers $$$p_0, p_1, \\dots, p_{n - 1}$$$, where $$$p_0$$$ is the initial price of the product and $$$p_i$$$ is how the price was increased during the $$$i$$$-th month.Using these price changes you are asked to calculate the inflation coefficients for each month as the ratio of current price increase $$$p_i$$$ to the price at the start of this month $$$(p_0 + p_1 + \\dots + p_{i - 1})$$$.Your boss said you clearly that the inflation coefficients must not exceed $$$k$$$ %, so you decided to increase some values $$$p_i$$$ in such a way, that all $$$p_i$$$ remain integers and the inflation coefficients for each month don't exceed $$$k$$$ %.You know, that the bigger changes — the more obvious cheating. That's why you need to minimize the total sum of changes.What's the minimum total sum of changes you need to make all inflation coefficients not more than $$$k$$$ %?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n scanf(\"%d %d\", &n, &k);\n long long int p[n + 1];\n long long int arr[n + 1];\n long long int max = 0;\n arr[0] = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%lli\", &p[i]);\n if (i == 0) {\n arr[i] = p[i];\n } else {\n arr[i] = arr[i - 1] + p[i];\n }\n }\n\n for (int i = 1; i < n; i++) {\n if ((p[i] * 100) <= arr[i - 1] * k) {\n continue;\n }\n if (((p[i] * 100) - arr[i - 1] * k) > max)\n max = (p[i] * 100) - arr[i - 1] * k;\n }\n long long int ans = ceil((double)max / k);\n printf(\"%lli\\n\", ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::{self, Write};\n\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, k): (usize, i64) = (sc.next(), sc.next());\n let p: Vec = (0..n).map(|_| sc.next()).collect();\n let mut sum: i64 = p[0];\n let mut ans: i64 = 0;\n for i in 1..n {\n let tmp = max(0, (p[i] * 100 - k * sum + k - 1) / k);\n ans += tmp;\n sum += p[i] + tmp;\n }\n writeln!(out, \"{}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1768", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of Mysterious Light.

\n

Three mirrors of length N are set so that they form an equilateral triangle.\nLet the vertices of the triangle be a, b and c.

\n

Inside the triangle, the rifle is placed at the point p on segment ab such that ap = X.\n(The size of the rifle is negligible.)\nNow, the rifle is about to fire a ray of Mysterious Light in the direction of bc.

\n

The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as \"ordinary\" light.\nThere is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror!\nWhen the ray comes back to the rifle, the ray will be absorbed.

\n

The following image shows the ray's trajectory where N = 5 and X = 2.

\n
\n\"btriangle.png\"\n
\n

It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of N and X.\nFind the total length of the ray's trajectory.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2≦N≦10^{12}
  • \n
  • 1≦X≦N-1
  • \n
  • N and X are integers.
  • \n
\n
\n
\n
\n
\n

Partial Points

    \n
  • 300 points will be awarded for passing the test set satisfying N≦1000.
  • \n
  • Another 200 points will be awarded for passing the test set without additional constraints.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N X\n
\n
\n
\n
\n
\n

Output

Print the total length of the ray's trajectory.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 2\n
\n
\n
\n
\n
\n

Sample Output 1

12\n
\n

Refer to the image in the Problem Statement section.\nThe total length of the trajectory is 2+3+2+2+1+1+1 = 12.

\n
\n
", "c_code": "int solution(void) {\n long long N;\n long long X;\n scanf(\"%lld%lld\", &N, &X);\n if (X > N / 2) {\n X = N - X;\n }\n long long sum = 0;\n long long k;\n sum += X + N - X;\n N = N - X;\n while (X != 0) {\n sum += 2 * X * (N / X);\n k = N % X;\n N = X;\n X = k;\n }\n\n sum -= N;\n\n printf(\"%lld\", sum);\n}", "rust_code": "fn solution() {\n let stdin = std::io::stdin();\n let line = stdin.lock().lines().next().unwrap().unwrap();\n let mut split = line.split(' ').map(|x| x.parse::().unwrap());\n let (n, mut x) = (split.next().unwrap(), split.next().unwrap());\n\n if x * 2 == n {\n println!(\"{}\", x * 3);\n std::process::exit(0);\n }\n\n if x > n / 2 {\n x = n - x;\n }\n let mut dist = n;\n\n let (mut a, mut b) = (n - x, x);\n loop {\n let rem = a % b;\n if rem == 0 {\n dist += (a - rem) * 2 - b;\n break;\n }\n dist += (a - rem) * 2;\n a = b;\n b = rem;\n }\n println!(\"{}\", dist);\n}", "difficulty": "easy"} {"problem_id": "1769", "problem_description": "The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c).Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 ≤ c, and 0, otherwise.You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right — (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.A star lies in a rectangle if it lies on its border or lies strictly inside it.", "c_code": "int solution() {\n int i;\n int j;\n int c;\n int n;\n int q;\n int x;\n int y;\n int s;\n int t;\n int x1;\n int x2;\n int y1;\n int y2;\n int ans;\n int z;\n scanf(\"%d%d%d\", &n, &q, &c);\n int arr[101][101][11] = {0};\n for (i = 0; i < n; i++) {\n scanf(\"%d%d%d\", &x, &y, &s);\n arr[x][y][s]++;\n }\n for (i = 1; i < 101; i++) {\n for (j = 1; j < 101; j++) {\n for (z = 0; z < c + 1; z++) {\n arr[i][j][z] +=\n arr[i - 1][j][z] + arr[i][j - 1][z] - arr[i - 1][j - 1][z];\n }\n }\n }\n while (q--) {\n scanf(\"%d%d%d%d%d\", &t, &x1, &y1, &x2, &y2);\n t %= (c + 1);\n ans = 0;\n for (z = 0; z + t <= c; z++) {\n ans += (arr[x2][y2][z] - arr[x1 - 1][y2][z] - arr[x2][y1 - 1][z] +\n arr[x1 - 1][y1 - 1][z]) *\n (z + t);\n }\n for (; z <= c; z++) {\n ans += (arr[x2][y2][z] - arr[x1 - 1][y2][z] - arr[x2][y1 - 1][z] +\n arr[x1 - 1][y1 - 1][z]) *\n (z + t - c - 1);\n }\n printf(\"%d\\n\", ans);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io::stdin;\n\n let (n, q, c) = {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let mut input = input\n .split_whitespace()\n .map(|k| k.parse::().unwrap());\n (\n input.next().unwrap(),\n input.next().unwrap(),\n input.next().unwrap(),\n )\n };\n\n use std::iter;\n\n let mut arr: Vec<_> = std::iter::repeat_n(\n std::iter::repeat_n(std::iter::repeat_n(0, 100).collect::>(), 100)\n .collect::>(),\n c + 1,\n )\n .collect();\n\n for _ in 0..n {\n let (x, y, s) = {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let mut input = input\n .split_whitespace()\n .map(|k| k.parse::().unwrap());\n (\n input.next().unwrap(),\n input.next().unwrap(),\n input.next().unwrap(),\n )\n };\n arr[s][x - 1][y - 1] += 1;\n }\n\n for arr in arr.iter_mut() {\n for i in 1..100 {\n arr[i][0] += arr[i - 1][0];\n }\n for j in 1..100 {\n arr[0][j] += arr[0][j - 1];\n }\n for i in 1..100 {\n for j in 1..100 {\n arr[i][j] += arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1];\n }\n }\n }\n\n for _ in 0..q {\n let (t, x1, y1, x2, y2) = {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let mut input = input\n .split_whitespace()\n .map(|k| k.parse::().unwrap());\n (\n input.next().unwrap(),\n input.next().unwrap() - 1,\n input.next().unwrap() - 1,\n input.next().unwrap() - 1,\n input.next().unwrap() - 1,\n )\n };\n\n let ans: usize = arr\n .iter()\n .enumerate()\n .map(|(i, arr)| {\n (i + t) % (c + 1)\n * (if x1 > 0 && y1 > 0 {\n arr[x2][y2] + arr[x1 - 1][y1 - 1] - arr[x1 - 1][y2] - arr[x2][y1 - 1]\n } else if x1 > 0 && y1 == 0 {\n arr[x2][y2] - arr[x1 - 1][y2]\n } else if x1 == 0 && y1 > 0 {\n arr[x2][y2] - arr[x2][y1 - 1]\n } else {\n arr[x2][y2]\n })\n })\n .sum();\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "1770", "problem_description": "Ashish has $$$n$$$ elements arranged in a line. These elements are represented by two integers $$$a_i$$$ — the value of the element and $$$b_i$$$ — the type of the element (there are only two possible types: $$$0$$$ and $$$1$$$). He wants to sort the elements in non-decreasing values of $$$a_i$$$.He can perform the following operation any number of times: Select any two elements $$$i$$$ and $$$j$$$ such that $$$b_i \\ne b_j$$$ and swap them. That is, he can only swap two elements of different types in one move. Tell him if he can sort the elements in non-decreasing values of $$$a_i$$$ after performing any number of operations.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int m;\n int oc = 0;\n int zc = 0;\n scanf(\"%d\", &m);\n int arr[m];\n int arr1[m];\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &arr[j]);\n }\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &arr1[j]);\n if (arr1[j] == 0) {\n zc++;\n } else {\n oc++;\n }\n }\n int c = 0;\n for (int j = 0; j < m - 1; j++) {\n if (arr[j] <= arr[j + 1]) {\n c++;\n }\n }\n if (c == m - 1) {\n printf(\"Yes\");\n } else {\n if (oc > 0 && zc > 0) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n }\n\n if (i < t - 1) {\n printf(\"\\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 n: 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 ys: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let zeros = ys.iter().filter(|y| **y == 0).count();\n let is_sorted = (0..(n - 1)).all(|i| xs[i] <= xs[i + 1]);\n let ok = (0 < zeros && zeros < n) || is_sorted;\n println!(\"{}\", if ok { \"Yes\" } else { \"No\" });\n }\n}", "difficulty": "medium"} {"problem_id": "1771", "problem_description": "Autocomplete is a program function that enables inputting the text (in editors, command line shells, browsers etc.) completing the text by its inputted part. Vasya is busy working on a new browser called 'BERowser'. He happens to be working on the autocomplete function in the address line at this very moment. A list consisting of n last visited by the user pages and the inputted part s are known. Your task is to complete s to make it an address of one of the pages from the list. You have to find the lexicographically smallest address having a prefix s.", "c_code": "int solution(void) {\n int n;\n char s[101];\n\n scanf(\"%s\", s);\n scanf(\"%d\", &n);\n char page[n][101];\n int ans = -1;\n int mini = 0;\n int len = strlen(s);\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", page[i]);\n\n if (strncmp(s, page[i], len) == 0 && strcmp(page[i], page[mini]) <= 0) {\n mini = i;\n ans = i;\n }\n }\n\n if (ans == -1) {\n printf(\"%s\", s);\n } else {\n printf(\"%s\", page[ans]);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut word = String::new();\n io::stdin().read_line(&mut word).unwrap();\n word = word.trim().to_string();\n\n let mut count = String::new();\n io::stdin().read_line(&mut count).unwrap();\n let count: u32 = match count.trim().parse() {\n Ok(num) => num,\n Err(_) => return,\n };\n\n let mut rows = vec![];\n\n for _ in 0..count {\n let mut input = String::new();\n io::stdin().read_line(&mut input).expect(\"err\");\n let trimmed_input = input.trim();\n\n if trimmed_input.starts_with(&word) {\n rows.push(String::from(trimmed_input));\n }\n }\n rows.sort();\n\n if !rows.is_empty() {\n print!(\"{}\\r\", rows[0]);\n } else {\n print!(\"{}\\r\", word);\n }\n}", "difficulty": "hard"} {"problem_id": "1772", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given an integer K.\nPrint the string obtained by repeating the string ACL K times and concatenating them.

\n

For example, if K = 3, print ACLACLACL.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq K \\leq 5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K\n
\n
\n
\n
\n
\n

Output

Print the string obtained by repeating the string ACL K times and concatenating them.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

ACLACLACL\n
\n
\n
", "c_code": "int solution() {\n int K = 0;\n\n scanf(\"%d\", &K);\n\n for (int i = 0; i < K * 3; i++) {\n switch (i % 3) {\n case 0:\n printf(\"A\");\n break;\n\n case 1:\n printf(\"C\");\n break;\n\n case 2:\n printf(\"L\");\n break;\n\n default:\n break;\n }\n }\n\n printf(\"\\n\");\n\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let mut buf = String::new();\n let stdin = io::stdin();\n\n stdin.read_line(&mut buf)?;\n buf.retain(|c| c != '\\n');\n\n let times: usize = buf.parse().unwrap();\n for _ in 0..times {\n print!(\"ACL\");\n }\n println!();\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "1773", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

You are given an H × W grid.
\nThe squares in the grid are described by H strings, S_1,...,S_H.
\nThe j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \\leq i \\leq H,1 \\leq j \\leq W).
\n. stands for an empty square, and # stands for a square containing a bomb.

\n

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.
\n(Below, we will simply say \"adjacent\" for this meaning. For each square, there are at most eight adjacent squares.)
\nHe decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

\n

Print the strings after the process.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H,W \\leq 50
  • \n
  • S_i is a string of length W consisting of # and ..
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\nS_1\n:\nS_H\n
\n
\n
\n
\n
\n

Output

Print the H strings after the process.
\nThe i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \\leq i \\leq H, 1 \\leq j \\leq W).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 5\n.....\n.#.#.\n.....\n
\n
\n
\n
\n
\n

Sample Output 1

11211\n1#2#1\n11211\n
\n

For example, let us observe the empty square at the first row from the top and first column from the left.
\nThere is one bomb square adjacent to this empty square: the square at the second row and second column.
\nThus, the . corresponding to this empty square is replaced with 1.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 5\n#####\n#####\n#####\n
\n
\n
\n
\n
\n

Sample Output 2

#####\n#####\n#####\n
\n

It is possible that there is no empty square.

\n
\n
\n
\n
\n
\n

Sample Input 3

6 6\n#####.\n#.#.##\n####.#\n.#..#.\n#.##..\n#.#...\n
\n
\n
\n
\n
\n

Sample Output 3

#####3\n#8#7##\n####5#\n4#65#2\n#5##21\n#4#310\n
\n
\n
", "c_code": "int solution(void) {\n int H = 0;\n int W = 0;\n char board[51][51];\n int count = 0;\n scanf(\"%d %d\", &H, &W);\n for (int i = 0; i < H; i++) {\n scanf(\"%s\", board[i]);\n }\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n if (board[i][j] == '.') {\n if (i > 0) {\n if (board[i - 1][j] == '#') {\n count++;\n }\n }\n if (i < H - 1) {\n if (board[i + 1][j] == '#') {\n count++;\n }\n }\n\n if (j > 0) {\n if (board[i][j - 1] == '#') {\n count++;\n }\n if (i > 0) {\n if (board[i - 1][j - 1] == '#') {\n count++;\n }\n }\n if (i < H - 1) {\n if (board[i + 1][j - 1] == '#') {\n count++;\n }\n }\n }\n if (j < W - 1) {\n if (board[i][j + 1] == '#') {\n count++;\n }\n if (i > 0) {\n if (board[i - 1][j + 1] == '#') {\n count++;\n }\n }\n if (i < H - 1) {\n if (board[i + 1][j + 1] == '#') {\n count++;\n }\n }\n }\n board[i][j] = '0' + count;\n count = 0;\n }\n }\n }\n for (int i = 0; i < H; i++) {\n printf(\"%s\\n\", board[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let v: Vec = s\n .split_whitespace()\n .map(|e| e.parse().ok().unwrap())\n .collect();\n let mut mat: Vec = Vec::new();\n let mut ans: Vec> = Vec::new();\n let mut ans2: Vec = Vec::new();\n let h = v[0];\n let w = v[1];\n\n mat.push(std::iter::repeat_n(\".\", w + 2).collect());\n ans.push(vec![0; w + 2]);\n for _i in 0..h {\n let mut t = String::new();\n std::io::stdin().read_line(&mut t).ok();\n let mut pstr = \".\".to_string();\n pstr.push_str(t.trim());\n pstr.push('.');\n\n mat.push(pstr);\n ans.push(vec![0; w + 2]);\n }\n mat.push(std::iter::repeat_n(\".\", w + 2).collect());\n ans.push(vec![0; w + 2]);\n\n for i in 1..(h + 1) {\n for j in 1..(w + 1) {\n if mat[i].chars().nth(j).unwrap() == '#' {\n for k in 0..3 {\n for l in 0..3 {\n ans[i - 1 + k][j - 1 + l] += 1;\n }\n }\n }\n }\n }\n for i in 1..(h + 1) {\n let mut u = String::new();\n for j in 1..(w + 1) {\n if mat[i].chars().nth(j).unwrap() == '#' {\n u.push('#');\n } else {\n u.push_str(&ans[i][j].to_string());\n }\n }\n ans2.push(u);\n }\n\n for i in 0..(h) {\n println!(\"{}\", ans2[i]);\n }\n}", "difficulty": "easy"} {"problem_id": "1774", "problem_description": "You are given two even integers $$$n$$$ and $$$m$$$. Your task is to find any binary matrix $$$a$$$ with $$$n$$$ rows and $$$m$$$ columns where every cell $$$(i,j)$$$ has exactly two neighbours with a different value than $$$a_{i,j}$$$.Two cells in the matrix are considered neighbours if and only if they share a side. More formally, the neighbours of cell $$$(x,y)$$$ are: $$$(x-1,y)$$$, $$$(x,y+1)$$$, $$$(x+1,y)$$$ and $$$(x,y-1)$$$.It can be proven that under the given constraints, an answer always exists.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int o = 0; o < t; o++) {\n int n;\n int m;\n scanf(\"%d%d\", &n, &m);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n int i1 = i / 2;\n int j1 = j / 2;\n int x = (i1 + j1) & 1;\n int y = ((i & 1) + (j & 1));\n int z = y & 1;\n int ans = x ^ z;\n printf(\"%d \", ans);\n }\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut test_cases_remaining = String::new();\n io::stdin()\n .read_line(&mut test_cases_remaining)\n .expect(\"bla\");\n let mut test_cases_remaining: u32 = test_cases_remaining.trim().parse().expect(\"bla\");\n\n loop {\n if test_cases_remaining == 0 {\n break;\n }\n\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"input\");\n let nums = line\n .trim()\n .split(' ')\n .flat_map(str::parse::)\n .collect::>();\n let (n, m): (i32, i32) = (nums[0], nums[1]);\n\n let mut _n = 0;\n loop {\n if _n == n {\n break;\n }\n let mut _m = 0;\n loop {\n if _m == m {\n println!();\n break;\n }\n print!(\"{} \", (((_n >> 1) + (_m >> 1)) & 1) ^ ((_n + _m) & 1));\n _m += 1;\n }\n _n += 1;\n }\n test_cases_remaining -= 1;\n }\n}", "difficulty": "medium"} {"problem_id": "1775", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem

Takahashi, who is a novice in competitive programming, wants to learn M algorithms.\nInitially, his understanding level of each of the M algorithms is 0.

\n

Takahashi is visiting a bookstore, where he finds N books on algorithms.\nThe i-th book (1\\leq i\\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\\leq j\\leq M).\nThere is no other way to increase the understanding levels of the algorithms.

\n

Takahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1\\leq N, M\\leq 12
  • \n
  • 1\\leq X\\leq 10^5
  • \n
  • 1\\leq C_i \\leq 10^5
  • \n
  • 0\\leq A_{i, j} \\leq 10^5
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M X\nC_1 A_{1,1} A_{1,2} \\cdots A_{1,M}\nC_2 A_{2,1} A_{2,2} \\cdots A_{2,M}\n\\vdots\nC_N A_{N,1} A_{N,2} \\cdots A_{N,M}\n
\n
\n
\n
\n
\n

Output

If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3 10\n60 2 2 4\n70 8 7 9\n50 2 3 9\n
\n
\n
\n
\n
\n

Sample Output 1

120\n
\n

Buying the second and third books makes his understanding levels of all the algorithms 10 or higher, at the minimum cost possible.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 3 10\n100 3 1 4\n100 1 5 9\n100 2 6 5\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

Buying all the books is still not enough to make his understanding levels of all the algorithms 10 or higher.

\n
\n
\n
\n
\n
\n

Sample Input 3

8 5 22\n100 3 7 5 3 1\n164 4 5 2 7 8\n334 7 2 7 2 9\n234 4 7 2 8 2\n541 5 4 3 3 6\n235 4 8 6 9 7\n394 3 6 1 6 2\n872 8 4 3 7 2\n
\n
\n
\n
\n
\n

Sample Output 3

1067\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n int M;\n int X;\n int ans = 1e9;\n scanf(\"%d %d %d\", &N, &M, &X);\n int C[N];\n int A[N][M];\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &C[i]);\n for (int j = 0; j < M; j++) {\n scanf(\"%d\", &A[i][j]);\n }\n }\n\n for (int bit = 0; bit < (1 << N); bit++) {\n int money = 0;\n int totalA[M];\n\n for (int j = 0; j < M; j++) {\n totalA[j] = 0;\n }\n for (int i = 0; i < N; i++) {\n if (bit & (1 << i)) {\n money += C[i];\n for (int j = 0; j < M; j++) {\n totalA[j] += A[i][j];\n }\n }\n }\n int judge = 1;\n for (int j = 0; j < M; j++) {\n if (totalA[j] < X) {\n judge = 0;\n break;\n }\n }\n if (judge == 1 && money < ans) {\n ans = money;\n }\n }\n if (ans == 1e9) {\n puts(\"-1\");\n } else {\n printf(\"%d\\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 m: usize = itr.next().unwrap().parse().unwrap();\n let x: usize = itr.next().unwrap().parse().unwrap();\n let mut c = vec![0; n];\n let mut a = vec![vec![0; m]; n];\n for i in 0..n {\n c[i] = itr.next().unwrap().parse().unwrap();\n a[i] = (0..m)\n .map(|_| itr.next().unwrap().parse().unwrap())\n .collect();\n }\n\n const INF: usize = 1 << 30;\n let mut ans = INF;\n for bit in 0..1 << n {\n let mut cnt = 0;\n let mut get = vec![0; m];\n let mut ok = true;\n for i in 0..n {\n if bit >> i & 1 == 1 {\n cnt += c[i];\n for j in 0..m {\n get[j] += a[i][j];\n }\n }\n }\n for j in 0..m {\n if get[j] < x {\n ok = false;\n }\n }\n if ok {\n ans = min(ans, cnt);\n }\n }\n if ans == INF {\n println!(\"-1\");\n } else {\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "1776", "problem_description": "You've got a string $$$a_1, a_2, \\dots, a_n$$$, consisting of zeros and ones.Let's call a sequence of consecutive elements $$$a_i, a_{i + 1}, \\ldots, a_j$$$ ($$$1\\leq i\\leq j\\leq n$$$) a substring of string $$$a$$$. You can apply the following operations any number of times: Choose some substring of string $$$a$$$ (for example, you can choose entire string) and reverse it, paying $$$x$$$ coins for it (for example, «0101101» $$$\\to$$$ «0111001»); Choose some substring of string $$$a$$$ (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying $$$y$$$ coins for it (for example, «0101101» $$$\\to$$$ «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.What is the minimum number of coins you need to spend to get a string consisting only of ones?", "c_code": "int solution() {\n long long int n;\n long long int x;\n long long int y;\n long long int i;\n long long int j;\n long long int k = 0;\n scanf(\"%lld%lld%lld\", &n, &x, &y);\n char a[300005];\n long long int b[300005];\n scanf(\"%s\", a);\n\n for (i = 0; i < n - 1; i++) {\n if (a[i] == a[i + 1]) {\n continue;\n }\n b[k++] = a[i] - '0';\n }\n b[k++] = a[n - 1] - '0';\n long long int count = 0;\n\n for (i = 0; i < k; i++) {\n\n if (b[i] == 0) {\n count++;\n }\n }\n if (x >= y) {\n printf(\"%lld\\n\", count * y);\n } else {\n if (count > 1) {\n printf(\"%lld\\n\", ((count - 1) * x) + y);\n } else {\n if (count == 1) {\n printf(\"%lld\\n\", y);\n } else {\n printf(\"0\\n\");\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).unwrap();\n\n let input_strs = buffer\n .split_whitespace()\n .take(3)\n .map(|x| x.parse::().unwrap())\n .collect::>();\n\n let _n = *input_strs.get(0).unwrap();\n let x = *input_strs.get(1).unwrap();\n let y = *input_strs.get(2).unwrap();\n\n let s = buffer.split_whitespace().skip(3).next().unwrap();\n\n let nums = s.chars().map(|x| if x == '0' { 0 } else { 1 });\n\n let iter_chunks = nums.scan((false, 0), |s, x| {\n if !s.0 && x == 0 {\n s.0 = true;\n s.1 += 1;\n } else if x == 1 {\n s.0 = false;\n }\n Some(s.1)\n });\n\n let num_chunks = iter_chunks.max().expect(\"num_chunks\");\n\n let cost_optimal = if num_chunks == 0 {\n 0\n } else {\n (num_chunks - 1) * std::cmp::min(x, y) + y\n };\n\n println!(\"{}\", cost_optimal);\n}", "difficulty": "medium"} {"problem_id": "1777", "problem_description": "Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!A password is an array $$$a$$$ of $$$n$$$ positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index $$$i$$$ such that $$$1 \\leq i < n$$$ and $$$a_{i} \\neq a_{i+1}$$$, delete both $$$a_i$$$ and $$$a_{i+1}$$$ from the array and put $$$a_{i}+a_{i+1}$$$ in their place. For example, for array $$$[7, 4, 3, 7]$$$ you can choose $$$i = 2$$$ and the array will become $$$[7, 4+3, 7] = [7, 7, 7]$$$. Note that in this array you can't apply this operation anymore.Notice that one operation will decrease the size of the password by $$$1$$$. What is the shortest possible length of the password after some number (possibly $$$0$$$) of operations?", "c_code": "int solution() {\n int n = 0;\n scanf(\"%d\", &n);\n int i = 0;\n for (; i < n; i++) {\n int res = 1;\n int j = 0;\n int t = 0;\n int v[200000] = {0};\n scanf(\"%d\", &t);\n for (; j < t; j++) {\n scanf(\"%d\", &v[j]);\n }\n for (j = 0; j < t; j++) {\n if (j == t - 1) {\n res = t;\n } else if (v[j] != v[j + 1]) {\n break;\n }\n }\n printf(\"%d\\n\", res);\n }\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 a: 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 if (0..(n - 1)).all(|i| a[i] == a[i + 1]) {\n println!(\"{}\", n);\n } else {\n println!(\"1\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1778", "problem_description": "Vasya has got $$$n$$$ books, numbered from $$$1$$$ to $$$n$$$, arranged in a stack. The topmost book has number $$$a_1$$$, the next one — $$$a_2$$$, and so on. The book at the bottom of the stack has number $$$a_n$$$. All numbers are distinct.Vasya wants to move all the books to his backpack in $$$n$$$ steps. During $$$i$$$-th step he wants to move the book number $$$b_i$$$ into his backpack. If the book with number $$$b_i$$$ is in the stack, he takes this book and all the books above the book $$$b_i$$$, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order $$$[1, 2, 3]$$$ (book $$$1$$$ is the topmost), and Vasya moves the books in the order $$$[2, 1, 3]$$$, then during the first step he will move two books ($$$1$$$ and $$$2$$$), during the second step he will do nothing (since book $$$1$$$ is already in the backpack), and during the third step — one book (the book number $$$3$$$). Note that $$$b_1, b_2, \\dots, b_n$$$ are distinct.Help Vasya! Tell him the number of books he will put into his backpack during each step.", "c_code": "int solution() {\n int books[200001];\n int n = 0;\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n int book = 0;\n scanf(\"%d\", &book);\n books[book] = i;\n }\n int lastInsert = 0;\n for (int j = 1; j <= n; j++) {\n int order = 0;\n scanf(\"%d\", &order);\n order = books[order];\n if ((order - lastInsert) > 0) {\n printf(\"%d \", (order - lastInsert));\n lastInsert = order;\n } else {\n printf(\"0 \");\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).expect(\"read_line error\");\n let _n = match s.split_whitespace().next() {\n Some(s) => match s.parse::() {\n Ok(n) => n,\n _ => 0,\n },\n _ => 0,\n };\n s.clear();\n std::io::stdin().read_line(&mut s).expect(\"read_line error\");\n let stack = s\n .split_whitespace()\n .map(|s| match s.parse::() {\n Ok(n) => n,\n _ => 0,\n })\n .collect::>();\n s.clear();\n std::io::stdin().read_line(&mut s).expect(\"read_line error\");\n let order = s\n .split_whitespace()\n .map(|s| match s.parse::() {\n Ok(n) => n,\n _ => 0,\n })\n .collect::>();\n\n let mut answ: Vec = Vec::new();\n let mut stack_set = HashSet::new();\n let mut seen = HashSet::new();\n let mut idx = 0;\n\n for x in &stack {\n stack_set.insert(x);\n }\n\n for x in order {\n if !stack_set.contains(&x) {\n continue;\n }\n if seen.contains(&x) || idx == stack.len() {\n answ.push(0);\n } else {\n let mut cnt = 0;\n while idx < stack.len() {\n cnt += 1;\n if stack[idx] == x {\n answ.push(cnt);\n idx += 1;\n break;\n } else {\n seen.insert(stack[idx]);\n idx += 1;\n }\n }\n }\n }\n\n for x in answ {\n print!(\"{} \", x);\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1779", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

We will play a one-player game using a number line and N pieces.

\n

First, we place each of these pieces at some integer coordinate.

\n

Here, multiple pieces can be placed at the same coordinate.

\n

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

\n

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

\n

Note that the coordinates where we initially place the pieces are already regarded as visited.

\n

Find the minimum number of moves required to achieve the objective.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 1 \\leq M \\leq 10^5
  • \n
  • -10^5 \\leq X_i \\leq 10^5
  • \n
  • X_1, X_2, ..., X_M are all different.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nX_1 X_2 ... X_M\n
\n
\n
\n
\n
\n

Output

Find the minimum number of moves required to achieve the objective.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 5\n10 12 1 2 14\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

\n
    \n
  • Initially, put the two pieces at coordinates 1 and 10.
  • \n
  • Move the piece at coordinate 1 to 2.
  • \n
  • Move the piece at coordinate 10 to 11.
  • \n
  • Move the piece at coordinate 11 to 12.
  • \n
  • Move the piece at coordinate 12 to 13.
  • \n
  • Move the piece at coordinate 13 to 14.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

3 7\n-10 -3 0 9 -100 2 17\n
\n
\n
\n
\n
\n

Sample Output 2

19\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100 1\n-100000\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution() {\n\n int N;\n int M;\n int ary[100000] = {0};\n int bucket_1[200001] = {0};\n int margin[99999] = {0};\n int bucket_2[200000] = {0};\n int i;\n int j;\n int k;\n int notpass = 0;\n\n scanf(\"%d %d\", &N, &M);\n for (i = 0; i < M; i++) {\n scanf(\"%d\", &ary[i]);\n }\n\n if (M <= N) {\n printf(\"%d\\n\", 0);\n return 0;\n }\n\n for (i = 0; i < M; i++) {\n bucket_1[ary[i] + 100000] = 1;\n }\n for (j = 0, i = 0; i < M; j++) {\n if (bucket_1[j] != 0) {\n ary[i] = j - 100000;\n i++;\n }\n }\n\n for (i = 0; i < M - 1; i++) {\n margin[i] = ary[i + 1] - ary[i];\n }\n\n for (i = 0; i < M - 1; i++) {\n bucket_2[margin[i] - 1] += 1;\n }\n for (i = 0, j = 0; i < M - 1; j++) {\n if (bucket_2[j] != 0) {\n for (k = 0; k < bucket_2[j]; k++) {\n margin[i] = j + 1;\n i++;\n }\n }\n }\n\n for (i = M - 2; M - N - 1 < i; i--) {\n notpass += margin[i];\n }\n\n printf(\"%d\\n\", ary[M - 1] - ary[0] - notpass);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = io::stdin();\n let mut buf = String::new();\n let _ = stdin.read_to_string(&mut buf);\n let mut iter = buf.split_whitespace();\n let n: usize = iter.next().unwrap().parse().unwrap();\n let m: usize = iter.next().unwrap().parse().unwrap();\n let mut x: Vec = (0..m)\n .map(|_| iter.next().unwrap().parse().unwrap())\n .collect();\n\n x.sort();\n let mut d: Vec = (0..(m - 1)).map(|i| x[i + 1] - x[i]).collect();\n d.sort();\n println!(\"{}\", d.iter().rev().skip(n - 1).sum::());\n}", "difficulty": "medium"} {"problem_id": "1780", "problem_description": "You and your friend are participating in a TV show \"Run For Your Prize\".At the start of the show n prizes are located on a straight line. i-th prize is located at position ai. Positions of all prizes are distinct. You start at position 1, your friend — at position 106 (and there is no prize in any of these two positions). You have to work as a team and collect all prizes in minimum possible time, in any order.You know that it takes exactly 1 second to move from position x to position x + 1 or x - 1, both for you and your friend. You also have trained enough to instantly pick up any prize, if its position is equal to your current position (and the same is true for your friend). Carrying prizes does not affect your speed (or your friend's speed) at all.Now you may discuss your strategy with your friend and decide who will pick up each prize. Remember that every prize must be picked up, either by you or by your friend.What is the minimum number of seconds it will take to pick up all the prizes?", "c_code": "int solution() {\n\n int n;\n scanf(\"%d\", &n);\n\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n\n int t1 = 0;\n int t2 = 0;\n\n if (a[n - 1] <= 500000) {\n printf(\"%d\", a[n - 1] - 1);\n }\n\n if (a[0] > 500000) {\n printf(\"%d\", 1000000 - a[0]);\n }\n\n else {\n for (int i = 0; i < n; i++) {\n if (a[i] > 500000 && a[i - 1] <= 500000) {\n t1 = 1000000 - a[i];\n t2 = a[i - 1] - 1;\n if (t1 > t2) {\n printf(\"%d\", t1);\n } else {\n printf(\"%d\", t2);\n }\n }\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n let _n: i64 = buf.split_whitespace().next().unwrap().parse().unwrap();\n buf.clear();\n stdin().read_line(&mut buf).unwrap();\n let a: Vec = buf\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect();\n let _quit = false;\n let _xl = 1;\n let _xr = 1000_000;\n let mut cl = 2;\n let mut cr = 999_999;\n for p in a {\n if p <= 500_000 {\n cl = p;\n } else {\n cr = p;\n break;\n }\n }\n\n println!(\"{}\", (cl - 1).max(1000_000 - cr));\n}", "difficulty": "medium"} {"problem_id": "1781", "problem_description": "You are given two positive integers $$$n$$$ ($$$1 \\le n \\le 10^9$$$) and $$$k$$$ ($$$1 \\le k \\le 100$$$). Represent the number $$$n$$$ as the sum of $$$k$$$ positive integers of the same parity (have the same remainder when divided by $$$2$$$).In other words, find $$$a_1, a_2, \\ldots, a_k$$$ such that all $$$a_i>0$$$, $$$n = a_1 + a_2 + \\ldots + a_k$$$ and either all $$$a_i$$$ are even or all $$$a_i$$$ are odd at the same time.If such a representation does not exist, then report it.", "c_code": "int solution() {\n\n int t = 0;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n = 0;\n int k = 0;\n scanf(\"%d%d\", &n, &k);\n if ((k <= n) && (n - k + 1) % 2 != 0) {\n printf(\"YES\\n\");\n for (int j = 0; j < (k - 1); j++) {\n printf(\"1 \");\n }\n printf(\"%d\\n\", n - k + 1);\n } else if ((k * 2 <= n) && (n - k * 2 + 2) % 2 == 0) {\n printf(\"YES\\n\");\n for (int j = 0; j < (k - 1); j++) {\n printf(\"2 \");\n }\n printf(\"%d\\n\", n - (k * 2) + 2);\n } else {\n printf(\"NO\\n\");\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 xs: Vec = lines\n .next()\n .unwrap()\n .unwrap()\n .split(' ')\n .map(|x| x.parse().unwrap())\n .collect();\n let (n, k) = (xs[0], xs[1]);\n if n % 2 == 0 {\n if k % 2 == 0 {\n if n >= k {\n println!(\"YES\");\n print!(\"{}\", n - k + 1);\n for _ in 1..k {\n print!(\" 1\");\n }\n println!();\n } else {\n println!(\"NO\");\n }\n } else {\n if n >= k * 2 {\n println!(\"YES\");\n print!(\"{}\", n - k * 2 + 2);\n for _ in 1..k {\n print!(\" 2\");\n }\n println!();\n } else {\n println!(\"NO\");\n }\n }\n } else {\n if k % 2 == 0 {\n println!(\"NO\");\n } else {\n if n >= k {\n println!(\"YES\");\n print!(\"{}\", n - k + 1);\n for _ in 1..k {\n print!(\" 1\");\n }\n println!();\n } else {\n println!(\"NO\")\n }\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1782", "problem_description": "

Reversing Numbers


\n\n

\n Write a program which reads a sequence and prints it in the reverse order.\n

\n\n\n

Input

\n

\nThe input is given in the following format:\n

\n\n
\nn\na1 a2 . . . an\n
\n\n

\nn is the size of the sequence and ai is the ith element of the sequence.\n

\n\n\n

Output

\n\n

\n Print the reversed sequence in a line. Print a single space character between adjacent elements (Note that your program should not put a space character after the last element).\n

\n\n

Constraints

\n
    \n
  • n ≤ 100
  • \n
  • 0 ≤ ai < 1000
  • \n
\n\n\n

Sample Input 1

\n\n
\n5\n1 2 3 4 5\n
\n\n

Sample Output 1

\n\n
\n5 4 3 2 1\n
\n\n

Sample Input 2

\n\n
\n8\n3 3 4 4 5 8 7 9\n
\n\n

Sample Output 2

\n\n
\n9 7 8 5 4 4 3 3\n
\n\n

Note

\n\n
\n\n
      解説      
\n
\t\n
", "c_code": "int solution(void) {\n\n int n = 100;\n scanf(\"%d\", &n);\n\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 1; i < n; i++) {\n printf(\"%d \", a[n - i]);\n }\n printf(\"%d\\n\", a[0]);\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut buf = String::new();\n\n let _ = stdin.read_line(&mut buf);\n buf.clear();\n\n let _ = stdin.read_line(&mut buf);\n let mut vec: Vec<&str> = buf.split_whitespace().collect();\n vec.reverse();\n\n println!(\"{}\", vec.join(\" \"));\n}", "difficulty": "hard"} {"problem_id": "1783", "problem_description": "Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.", "c_code": "int solution() {\n int count = 2;\n int max = 0;\n int n;\n int flat = 1;\n char goingUp = '1';\n scanf(\"%d\", &n);\n getchar();\n if (n == 1) {\n printf(\"%d\", 1);\n } else if (n == 2) {\n printf(\"%d\", 2);\n } else {\n int t1;\n int t2;\n scanf(\"%d\", &t1);\n scanf(\"%d\", &t2);\n if (t2 < t1) {\n goingUp = '0';\n }\n for (int i = 2; i < n; ++i) {\n t1 = t2;\n scanf(\"%d\", &t2);\n if (goingUp == '1' && t2 >= t1) {\n ++count;\n } else if (goingUp == '1' && t2 < t1) {\n ++count;\n goingUp = '0';\n } else if (goingUp == '0' && t2 < t1) {\n ++count;\n flat = 1;\n } else if (goingUp == '0' && t2 == t1) {\n ++count;\n ++flat;\n } else {\n goingUp = '1';\n if (count > max) {\n max = count;\n }\n count = flat + 1;\n flat = 1;\n }\n }\n if (count > max) {\n max = count;\n }\n printf(\"%d\", max);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf);\n buf.clear();\n io::stdin().read_line(&mut buf);\n let s: Vec = buf\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n let mut mx = 1;\n for i in 0..s.len() {\n let mut curr = 0;\n let mut mx_height = s[i];\n for j in i..s.len() {\n if s[j] <= mx_height {\n mx_height = s[j];\n curr += 1\n } else {\n break;\n }\n }\n mx_height = s[i];\n for j in (0..i).rev() {\n if s[j] <= mx_height {\n mx_height = s[j];\n curr += 1\n } else {\n break;\n }\n }\n mx = u32::max(mx, curr);\n }\n println!(\"{}\", mx);\n}", "difficulty": "hard"} {"problem_id": "1784", "problem_description": "Polycarp wants to buy exactly $$$n$$$ shovels. The shop sells packages with shovels. The store has $$$k$$$ types of packages: the package of the $$$i$$$-th type consists of exactly $$$i$$$ shovels ($$$1 \\le i \\le k$$$). The store has an infinite number of packages of each type.Polycarp wants to choose one type of packages and then buy several (one or more) packages of this type. What is the smallest number of packages Polycarp will have to buy to get exactly $$$n$$$ shovels?For example, if $$$n=8$$$ and $$$k=7$$$, then Polycarp will buy $$$2$$$ packages of $$$4$$$ shovels.Help Polycarp find the minimum number of packages that he needs to buy, given that he: will buy exactly $$$n$$$ shovels in total; the sizes of all packages he will buy are all the same and the number of shovels in each package is an integer from $$$1$$$ to $$$k$$$, inclusive.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int k;\n int m = 1;\n scanf(\"%d %d\", &n, &k);\n if (n <= k) {\n printf(\"1\\n\");\n } else {\n for (int i = 1; i <= sqrt(n); i++) {\n if (n % i == 0) {\n if ((n / i) > m && (n / i) <= k) {\n m = n / i;\n }\n if (i > m && i <= k) {\n m = i;\n }\n }\n }\n printf(\"%d\\n\", (n / m));\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: u64 = itr.next().unwrap().parse().unwrap();\n let k: u64 = itr.next().unwrap().parse().unwrap();\n let mut ans = 1u64 << 60;\n let mut i = 1;\n while i * i <= n {\n if n.is_multiple_of(i) {\n if i <= k {\n ans = std::cmp::min(ans, n / i);\n }\n if n / i <= k {\n ans = std::cmp::min(ans, i);\n }\n }\n i += 1;\n }\n writeln!(out, \"{}\", ans).ok();\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "medium"} {"problem_id": "1785", "problem_description": "We say that a positive integer $$$n$$$ is $$$k$$$-good for some positive integer $$$k$$$ if $$$n$$$ can be expressed as a sum of $$$k$$$ positive integers which give $$$k$$$ distinct remainders when divided by $$$k$$$.Given a positive integer $$$n$$$, find some $$$k \\geq 2$$$ so that $$$n$$$ is $$$k$$$-good or tell that such a $$$k$$$ does not exist.", "c_code": "int solution() {\n\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n long long n;\n int flag = 0;\n scanf(\"%lld\", &n);\n long long p = n;\n\n while (p % 2 == 0) {\n p = p / 2;\n }\n\n if (p == 1) {\n printf(\"-1\\n\");\n } else if (p <= 2e9 && (p * (p + 1)) / 2 <= n) {\n printf(\"%lld\\n\", p);\n } else {\n printf(\"%lld\\n\", 2 * (n / p));\n }\n }\n}", "rust_code": "fn solution() {\n let std_in = stdin();\n let mut input = Scanner::new(std_in.lock());\n\n let std_out = stdout();\n let mut output = BufWriter::new(std_out.lock());\n\n let tests = input.token();\n for _ in 0..tests {\n let n: i64 = input.token();\n let mut k = -1i128;\n for i in 0..62 {\n if n % (1i64 << i) != 0 {\n k = 1i128 << i;\n break;\n }\n }\n if n as i128 >= (k * (k + 1) / 2) {\n writeln!(output, \"{}\", k).unwrap();\n continue;\n }\n k = 2 * (n as i128) / k;\n if k == 1 {\n writeln!(output, \"-1\").unwrap();\n continue;\n } else {\n writeln!(output, \"{}\", k).unwrap();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1786", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You are given strings s and t.\nFind one longest string that is a subsequence of both s and t.

\n
\n
\n
\n
\n

Notes

A subsequence of a string x is the string obtained by removing zero or more characters from x and concatenating the remaining characters without changing the order.

\n
\n
\n
\n
\n

Constraints

    \n
  • s and t are strings consisting of lowercase English letters.
  • \n
  • 1 \\leq |s|, |t| \\leq 3000
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s\nt\n
\n
\n
\n
\n
\n

Output

Print one longest string that is a subsequence of both s and t.\nIf there are multiple such strings, any of them will be accepted.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

axyb\nabyxb\n
\n
\n
\n
\n
\n

Sample Output 1

axb\n
\n

The answer is axb or ayb; either will be accepted.

\n
\n
\n
\n
\n
\n

Sample Input 2

aa\nxayaz\n
\n
\n
\n
\n
\n

Sample Output 2

aa\n
\n
\n
\n
\n
\n
\n

Sample Input 3

a\nz\n
\n
\n
\n
\n
\n

Sample Output 3

\n
\n

The answer is (an empty string).

\n
\n
\n
\n
\n
\n

Sample Input 4

abracadabra\navadakedavra\n
\n
\n
\n
\n
\n

Sample Output 4

aaadara\n
\n
\n
", "c_code": "int solution() {\n char a[3001];\n char b[3001];\n scanf(\"%s\", a);\n scanf(\"%s\", b);\n int s1 = strlen(a);\n int s2 = strlen(b);\n int i;\n int j;\n int dp[s1 + 1][s2 + 1];\n int count;\n for (i = 0; i <= s1; i++) {\n for (j = 0; j <= s2; j++) {\n if (i == 0 || j == 0) {\n dp[i][j] = 0;\n } else if (a[i - 1] == b[j - 1]) {\n dp[i][j] = 1 + dp[i - 1][j - 1];\n } else {\n dp[i][j] = dp[i - 1][j] > dp[i][j - 1] ? dp[i - 1][j] : dp[i][j - 1];\n }\n }\n }\n char s[dp[s1][s2]];\n count = dp[s1][s2];\n\n while (i > 0 && j > 0) {\n if (a[i - 1] == b[j - 1]) {\n s[count] = a[i - 1];\n count--;\n i--;\n j--;\n } else if (dp[i - 1][j] > dp[i][j - 1]) {\n i--;\n } else {\n j--;\n }\n }\n printf(\"%s\", s);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut it = s.split_whitespace();\n let s: Vec<_> = it.next().unwrap().chars().collect();\n let t: Vec<_> = it.next().unwrap().chars().collect();\n let mut dp = vec![vec![0u16; t.len() + 1]; s.len() + 1];\n for i in 0..s.len() {\n for j in 0..t.len() {\n if s[i] == t[j] {\n dp[i + 1][j + 1] = dp[i][j] + 1;\n } else {\n dp[i + 1][j + 1] = std::cmp::max(dp[i][j + 1], dp[i + 1][j]);\n }\n }\n }\n let mut ans = vec![];\n let mut i = s.len();\n let mut j = t.len();\n while i > 0 && j > 0 {\n if dp[i][j] == dp[i - 1][j] {\n i -= 1;\n } else if dp[i][j] == dp[i][j - 1] {\n j -= 1;\n } else {\n ans.push(s[i - 1]);\n i -= 1;\n j -= 1;\n }\n }\n println!(\"{}\", ans.into_iter().rev().collect::());\n}", "difficulty": "medium"} {"problem_id": "1787", "problem_description": "Suppose you have an integer $$$v$$$. In one operation, you can: either set $$$v = (v + 1) \\bmod 32768$$$ or set $$$v = (2 \\cdot v) \\bmod 32768$$$. You are given $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$. What is the minimum number of operations you need to make each $$$a_i$$$ equal to $$$0$$$?", "c_code": "int solution() {\n\n int broj = 32768;\n int n;\n scanf(\"%d\", &n);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n int potezi = 30;\n for (int k = 0; k < 15; k++) {\n for (int l = 0; l < 15; l++) {\n int stepen = pow(2.0, l);\n if ((((a[i] + k) * stepen) % 32768) == 0) {\n if (potezi > k + l) {\n potezi = k + l;\n break;\n }\n }\n }\n }\n printf(\"%d \", potezi);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut dp = vec![-1; 32768];\n dp[0] = 0;\n let mut q = VecDeque::with_capacity(32768);\n q.push_back(0);\n while !q.is_empty() {\n for _ in 0..q.len() {\n let x = q.pop_front().unwrap();\n let y = (x + 32768 - 1) % 32768;\n if dp[y] == -1 {\n dp[y] = dp[x] + 1;\n q.push_back(y);\n }\n if x & 1 == 0 {\n let mut y = x / 2;\n if dp[y] == -1 {\n dp[y] = dp[x] + 1;\n q.push_back(y);\n }\n y += 32768 / 2;\n if dp[y] == -1 {\n dp[y] = dp[x] + 1;\n q.push_back(y);\n }\n }\n }\n }\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let _: usize = s.trim().parse().unwrap();\n let mut s = String::new();\n stdin().read_line(&mut s).unwrap();\n let nums = s\n .trim()\n .split_ascii_whitespace()\n .map(|x| x.parse().unwrap())\n .collect::>();\n for x in nums {\n println!(\"{}\", dp[x]);\n }\n}", "difficulty": "medium"} {"problem_id": "1788", "problem_description": "\n

Score : 600 points

\n
\n
\n

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

\n

Some of the squares have a lotus leaf on it and cannot be entered.\nThe square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

\n

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.\nThe move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

\n

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).\nIf the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H,W,K \\leq 10^6
  • \n
  • H \\times W \\leq 10^6
  • \n
  • 1 \\leq x_1,x_2 \\leq H
  • \n
  • 1 \\leq y_1,y_2 \\leq W
  • \n
  • x_1 \\neq x_2 or y_1 \\neq y_2.
  • \n
  • c_{i,j} is . or @.
  • \n
  • c_{x_1,y_1} = .
  • \n
  • c_{x_2,y_2} = .
  • \n
  • All numbers in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W K\nx_1 y_1 x_2 y_2\nc_{1,1}c_{1,2} .. c_{1,W}\nc_{2,1}c_{2,2} .. c_{2,W}\n:\nc_{H,1}c_{H,2} .. c_{H,W}\n
\n
\n
\n
\n
\n

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 5 2\n3 2 3 4\n.....\n.@..@\n..@..\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

Initially, Snuke is at the square (3,2).\nHe can reach the square (3, 4) by making five strokes as follows:

\n
    \n
  • \n

    From (3, 2), go west one square to (3, 1).

    \n
  • \n
  • \n

    From (3, 1), go north two squares to (1, 1).

    \n
  • \n
  • \n

    From (1, 1), go east two squares to (1, 3).

    \n
  • \n
  • \n

    From (1, 3), go east one square to (1, 4).

    \n
  • \n
  • \n

    From (1, 4), go south two squares to (3, 4).

    \n
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

1 6 4\n1 1 1 6\n......\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

3 3 1\n2 1 2 3\n.@.\n.@.\n.@.\n
\n
\n
\n
\n
\n

Sample Output 3

-1\n
\n
\n
", "c_code": "int solution() {\n int i;\n int j;\n int H;\n int W;\n int K;\n int x[2];\n int y[2];\n char **c;\n scanf(\"%d %d %d\", &H, &W, &K);\n scanf(\"%d %d %d %d\", &(x[0]), &(y[0]), &(x[1]), &(y[1]));\n c = (char **)malloc(sizeof(char *) * (H + 2));\n for (i = 1; i <= H; i++) {\n c[i] = (char *)malloc(sizeof(char) * (W + 2));\n scanf(\"%s\", &(c[i][1]));\n }\n c[0] = (char *)malloc(sizeof(char) * (W + 2));\n c[H + 1] = (char *)malloc(sizeof(char) * (W + 2));\n for (i = 1; i <= H; i++) {\n c[i][0] = '@';\n c[i][W + 1] = '@';\n }\n for (j = 1; j <= W; j++) {\n c[0][j] = '@';\n c[H + 1][j] = '@';\n }\n\n const int sup = 1 << 30;\n int k;\n int l;\n int q[1000001][2];\n int head;\n int tail;\n int **dist = (int **)malloc(sizeof(int *) * (H + 2));\n for (i = 1; i <= H; i++) {\n dist[i] = (int *)malloc(sizeof(int) * (W + 2));\n for (j = 1; j <= W; j++) {\n dist[i][j] = sup;\n }\n }\n dist[x[0]][y[0]] = 0;\n q[0][0] = x[0];\n q[0][1] = y[0];\n for (head = 0, tail = 1; head < tail; head++) {\n i = q[head][0];\n j = q[head][1];\n for (k = i - 1; k >= i - K && c[k][j] == '.' && dist[k][j] > dist[i][j];\n k--) {\n if (dist[k][j] == dist[i][j] + 1) {\n continue;\n }\n dist[k][j] = dist[i][j] + 1;\n q[tail][0] = k;\n q[tail++][1] = j;\n }\n for (k = i + 1; k <= i + K && c[k][j] == '.' && dist[k][j] > dist[i][j];\n k++) {\n if (dist[k][j] == dist[i][j] + 1) {\n continue;\n }\n dist[k][j] = dist[i][j] + 1;\n q[tail][0] = k;\n q[tail++][1] = j;\n }\n for (l = j - 1; l >= j - K && c[i][l] == '.' && dist[i][l] > dist[i][j];\n l--) {\n if (dist[i][l] == dist[i][j] + 1) {\n continue;\n }\n dist[i][l] = dist[i][j] + 1;\n q[tail][0] = i;\n q[tail++][1] = l;\n }\n for (l = j + 1; l <= j + K && c[i][l] == '.' && dist[i][l] > dist[i][j];\n l++) {\n if (dist[i][l] == dist[i][j] + 1) {\n continue;\n }\n dist[i][l] = dist[i][j] + 1;\n q[tail][0] = i;\n q[tail++][1] = l;\n }\n }\n if (dist[x[1]][y[1]] < sup) {\n printf(\"%d\\n\", dist[x[1]][y[1]]);\n } else {\n printf(\"-1\\n\");\n }\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n use std::io::Read;\n std::io::stdin().read_to_string(&mut s).unwrap();\n let mut s = s.split_whitespace();\n let h: usize = s.next().unwrap().parse().unwrap();\n let w: usize = s.next().unwrap().parse().unwrap();\n let k: i32 = s.next().unwrap().parse().unwrap();\n let sy = s.next().unwrap().parse::().unwrap() - 1;\n let sx = s.next().unwrap().parse::().unwrap() - 1;\n let gy = s.next().unwrap().parse::().unwrap() - 1;\n let gx = s.next().unwrap().parse::().unwrap() - 1;\n let field: Vec> = s.take(h).map(|s| s.chars().collect()).collect();\n\n let mut dist = vec![vec![[std::i32::MAX; 4]; w]; h];\n use std::cmp::Reverse;\n let mut q = std::collections::BinaryHeap::new();\n q.push((Reverse(0), sy, sx, 4));\n while let Some((Reverse(cost), y, x, d)) = q.pop() {\n if d != 4 && cost > dist[y][x][d] {\n continue;\n }\n for &(ny, nx, nd) in [\n (y, x + 1, 0),\n (y.wrapping_sub(1), x, 1),\n (y, x.wrapping_sub(1), 2),\n (y + 1, x, 3),\n ]\n .iter()\n .filter(|&&(ny, nx, _)| (y, x) != (ny, nx) && ny < h && nx < w && field[ny][nx] == '.')\n {\n if d == nd {\n if cost + 1 < dist[ny][nx][nd] {\n dist[ny][nx][d] = cost + 1;\n q.push((Reverse(cost + 1), ny, nx, d));\n }\n } else {\n let nc = 1 + (cost + k - 1) / k * k;\n if nc < dist[ny][nx][nd] {\n dist[ny][nx][nd] = nc;\n q.push((Reverse(nc), ny, nx, nd));\n }\n }\n }\n }\n let ans = dist[gy][gx]\n .iter()\n .map(|&d| {\n if d == std::i32::MAX {\n std::i32::MAX\n } else {\n (d + k - 1) / k\n }\n })\n .min()\n .unwrap();\n if ans == std::i32::MAX {\n println!(\"-1\");\n } else {\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1789", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

It's now the season of TAKOYAKI FESTIVAL!

\n

This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.

\n

As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \\times y health points.

\n

There are \\frac{N \\times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \\frac{N \\times (N - 1)}{2} values.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 2 \\leq N \\leq 50
  • \n
  • 0 \\leq d_i \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nd_1 d_2 ... d_N\n
\n
\n
\n
\n
\n

Output

Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n3 1 2\n
\n
\n
\n
\n
\n

Sample Output 1

11\n
\n

There are three possible choices:

\n
    \n
  • Eat the first and second takoyaki. You will restore 3 health points.
  • \n
  • Eat the second and third takoyaki. You will restore 2 health points.
  • \n
  • Eat the first and third takoyaki. You will restore 6 health points.
  • \n
\n

The sum of these values is 11.

\n
\n
\n
\n
\n
\n

Sample Input 2

7\n5 0 7 8 3 3 2\n
\n
\n
\n
\n
\n

Sample Output 2

312\n
\n
\n
", "c_code": "int solution(void) {\n int takoyakis = 0;\n int delicious_points_list[50];\n int sum_heal_points = 0;\n\n scanf(\"%d\", &takoyakis);\n\n for (int i = 0; i < takoyakis; i++) {\n scanf(\"%d\", &delicious_points_list[i]);\n }\n\n for (int i = 0; i < takoyakis * takoyakis; i++) {\n int base = i / takoyakis;\n int material = i % takoyakis;\n\n if (material >= base) {\n continue;\n }\n\n sum_heal_points +=\n delicious_points_list[base] * delicious_points_list[material];\n }\n\n printf(\"%d\\n\", sum_heal_points);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let iter = buf.split_whitespace();\n let buf: Vec<&str> = iter.collect();\n let mut d = Vec::new();\n for n in buf {\n d.push(match n.to_string().parse::() {\n Ok(m) => m,\n _ => panic!(\"\"),\n });\n }\n let mut ret = 0;\n for i in 0..d.len() {\n for j in (i + 1)..d.len() {\n ret += d[i] * d[j];\n }\n }\n println!(\"{}\", ret);\n}", "difficulty": "hard"} {"problem_id": "1790", "problem_description": "\n

Score: 200 points

\n
\n
\n

Problem Statement

\n

We have a string S consisting of lowercase English letters.

\n

If the length of S is at most K, print S without change.

\n

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • K is an integer between 1 and 100 (inclusive).
  • \n
  • S is a string consisting of lowercase English letters.
  • \n
  • The length of S is between 1 and 100 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
K\nS\n
\n
\n
\n
\n
\n

Output

\n

Print a string as stated in Problem Statement.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7\nnikoandsolstice\n
\n
\n
\n
\n
\n

Sample Output 1

nikoand...\n
\n

nikoandsolstice has a length of 15, which exceeds K=7.

\n

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

\n
\n
\n
\n
\n
\n

Sample Input 2

40\nferelibenterhominesidquodvoluntcredunt\n
\n
\n
\n
\n
\n

Sample Output 2

ferelibenterhominesidquodvoluntcredunt\n
\n

The famous quote from Gaius Julius Caesar.

\n
\n
", "c_code": "int solution(void) {\n int a = 0;\n scanf(\"%d\", &a);\n char s[999] = \"\";\n scanf(\"%s\", s);\n int y = strlen(s);\n\n if (y <= a) {\n printf(\"%s\", s);\n } else {\n char t[999] = \" \";\n strncpy(t, s, a);\n printf(\"%s\", t);\n printf(\"...\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let k: usize = s.trim().parse().unwrap();\n s.clear();\n stdin().read_line(&mut s).ok();\n s.pop();\n println!(\n \"{}\",\n if s.len() <= k {\n s\n } else {\n format!(\"{}...\", &s[..k])\n }\n );\n}", "difficulty": "medium"} {"problem_id": "1791", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

Given is a sequence of integers A_1, A_2, ..., A_N.\nIf its elements are pairwise distinct, print YES; otherwise, print NO.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ N ≤ 200000
  • \n
  • 1 ≤ A_i ≤ 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 ... A_N\n
\n
\n
\n
\n
\n

Output

If the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n2 6 1 4 5\n
\n
\n
\n
\n
\n

Sample Output 1

YES\n
\n

The elements are pairwise distinct.

\n
\n
\n
\n
\n
\n

Sample Input 2

6\n4 1 3 1 6 2\n
\n
\n
\n
\n
\n

Sample Output 2

NO\n
\n

The second and fourth elements are identical.

\n
\n
\n
\n
\n
\n

Sample Input 3

2\n10000000 10000000\n
\n
\n
\n
\n
\n

Sample Output 3

NO\n
\n
\n
", "c_code": "int solution(void) {\n static char A[1000000010];\n int N;\n int num;\n int ans;\n\n scanf(\"%d\", &N);\n\n for (int i = 0; i < 1000000010; i++) {\n A[i] = 0;\n }\n\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &num);\n A[num]++;\n if (A[num] > 1) {\n printf(\"NO\");\n return 0;\n }\n }\n\n printf(\"YES\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let n: usize = {\n let mut n = String::new();\n std::io::stdin().read_line(&mut n).ok();\n n.trim().parse().unwrap()\n };\n\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).ok();\n let line: HashSet<&str> = line.split_whitespace().collect();\n println!(\"{}\", if n == line.len() { \"YES\" } else { \"NO\" });\n}", "difficulty": "medium"} {"problem_id": "1792", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Snuke is having a barbeque party.

\n

At the party, he will make N servings of Skewer Meal.

\n
\n\n

Example of a serving of Skewer Meal

\n
\n

He has a stock of 2N skewers, all of which will be used in Skewer Meal. The length of the i-th skewer is L_i.\nAlso, he has an infinite supply of ingredients.

\n

To make a serving of Skewer Meal, he picks 2 skewers and threads ingredients onto those skewers.\nLet the length of the shorter skewer be x, then the serving can hold the maximum of x ingredients.

\n

What is the maximum total number of ingredients that his N servings of Skewer Meal can hold, if he uses the skewers optimally?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1≦N≦100
  • \n
  • 1≦L_i≦100
  • \n
  • For each i, L_i is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\nL_1 L_2 ... L_{2N}\n
\n
\n
\n
\n
\n

Output

Print the maximum total number of ingredients that Snuke's N servings of Skewer Meal can hold.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2\n1 3 1 2\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

If he makes a serving using the first and third skewers, and another using the second and fourth skewers, each serving will hold 1 and 2 ingredients, for the total of 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n100 1 2 3 14 15 58 58 58 29\n
\n
\n
\n
\n
\n

Sample Output 2

135\n
\n
\n
", "c_code": "int solution() {\n\n int N = 0;\n int sum = 0;\n int i = 0;\n int L[200];\n\n scanf(\"%d\", &N);\n\n for (int x = 0; x < 2 * N; x++) {\n\n scanf(\"%d\", &L[x]);\n ++i;\n }\n\n for (int y = 0; y < i + 1; y++) {\n for (int z = 0; z < i - 1; z++) {\n if (L[z] < L[z + 1]) {\n sum = L[z];\n L[z] = L[z + 1];\n L[z + 1] = sum;\n }\n }\n }\n\n sum = 0;\n\n for (int y = 0; y < i; y += 2) {\n sum += L[y + 1];\n }\n printf(\"%d\\n\", sum);\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 let mut buf_it = buf.split_whitespace();\n\n let N = buf_it.next().unwrap().parse::().unwrap();\n let mut A = (0..2 * N)\n .map(|_| buf_it.next().unwrap().parse::().unwrap())\n .collect::>();\n A.sort();\n println!(\"{}\", (0..N).map(|i| A[i * 2]).sum::());\n}", "difficulty": "hard"} {"problem_id": "1793", "problem_description": "You are given a positive integer $$$n$$$. You have to find $$$4$$$ positive integers $$$a, b, c, d$$$ such that $$$a + b + c + d = n$$$, and $$$\\gcd(a, b) = \\operatorname{lcm}(c, d)$$$.If there are several possible answers you can output any of them. It is possible to show that the answer always exists.In this problem $$$\\gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$, and $$$\\operatorname{lcm}(c, d)$$$ denotes the least common multiple of $$$c$$$ and $$$d$$$.", "c_code": "int solution() {\n int m = 0;\n scanf(\"%d\", &m);\n while (m--) {\n int n = 0;\n scanf(\"%d\", &n);\n printf(\"%d %d %d %d\\n\", 1, n - 3, 1, 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let lines = stdin.lock().lines().skip(1);\n let mut oup = io::BufWriter::new(io::stdout());\n for line in lines {\n let n = line.unwrap().parse::().unwrap();\n writeln!(oup, \"{} 1 1 1\", n - 3).unwrap();\n }\n oup.flush().ok();\n}", "difficulty": "easy"} {"problem_id": "1794", "problem_description": "President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say, the two desks (President's and each deputy's) have a common side of a positive length.The office-room plan can be viewed as a matrix with n rows and m columns. Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The «period» character («.») stands for an empty cell.", "c_code": "int solution() {\n int c;\n int r;\n int s = 0;\n char car;\n scanf(\"%i %i %c\\n\", &c, &r, &car);\n char a[c][r];\n for (int i = 0; i < c; i++) {\n for (int j = 0; j < r; j++) {\n scanf(\"%c\", &a[i][j]);\n }\n if (i != c - 1) {\n scanf(\"\\n\");\n }\n }\n for (int i = 0; i < c; i++) {\n for (int j = 0; j < r; j++) {\n if (a[i][j] == car) {\n if (i >= 0 && i < c - 1 && a[i + 1][j] != car && a[i + 1][j] != '.') {\n if (a[i + 1][j] != '.') {\n s++;\n }\n if (j < r - 1 && a[i + 1][j] == a[i + 1][j + 1] &&\n a[i][j + 1] == car) {\n s--;\n }\n }\n if (i <= c - 1 && i > 0 && a[i - 1][j] != car && a[i - 1][j] != '.') {\n if (a[i - 1][j] != '.') {\n s++;\n }\n if (j < r - 1 && a[i - 1][j] == a[i - 1][j + 1] &&\n a[i][j + 1] == car) {\n s--;\n }\n }\n if (j >= 0 && j < r - 1 && a[i][j + 1] != car && a[i][j + 1] != '.') {\n if (a[i][j + 1] != '.') {\n s++;\n }\n if (i < c - 1 && a[i][j + 1] == a[i + 1][j + 1] &&\n a[i + 1][j] == car) {\n s--;\n }\n }\n if (j <= r - 1 && j > 0 && a[i][j - 1] != car && a[i][j - 1] != '.') {\n if (a[i][j - 1] != '.') {\n s++;\n }\n if (i < c - 1 && a[i][j - 1] == a[i + 1][j - 1] &&\n a[i + 1][j] == car) {\n s--;\n }\n }\n }\n }\n }\n printf(\"%i\", s);\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 lines = buf.lines();\n let (h, w, c) = {\n let mut tokens = lines.next().unwrap().split_whitespace();\n let h: i32 = tokens.next().unwrap().parse().unwrap();\n let w: i32 = tokens.next().unwrap().parse().unwrap();\n let c: char = tokens.next().unwrap().parse().unwrap();\n (h, w, c)\n };\n\n let room: Vec> = lines.map(|l| l.chars().collect()).collect();\n\n let di: Vec = vec![0, 1, 0, -1];\n let dj: Vec = vec![1, 0, -1, 0];\n let mut set = HashSet::new();\n for i in 0..h {\n for j in 0..w {\n if room[i as usize][j as usize] == c {\n for k in 0..4 {\n let ii = i + di[k];\n let jj = j + dj[k];\n if ii >= 0 && ii < h && jj >= 0 && jj < w {\n set.insert(room[ii as usize][jj as usize]);\n }\n }\n }\n }\n }\n set.remove(&c);\n set.remove(&'.');\n println!(\"{}\", set.len());\n}", "difficulty": "medium"} {"problem_id": "1795", "problem_description": "All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi — a coordinate on the Ox axis. No two cities are located at a single point.Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in).Strange but true, the cost of sending the letter is exactly equal to the distance between the sender's city and the recipient's city.For each city calculate two values ​​mini and maxi, where mini is the minimum cost of sending a letter from the i-th city to some other city, and maxi is the the maximum cost of sending a letter from the i-th city to some other city", "c_code": "int solution() {\n int number = 0;\n int p = 0;\n scanf(\"%d\", &number);\n int location[number];\n for (int i = 0; i < number; i++) {\n scanf(\"%d\", &location[i]);\n }\n int max = location[number - 1] - location[0];\n int min = location[1] - location[0];\n printf(\"%d %d\\n\", min, max);\n int max1 = 0;\n int max2 = 0;\n int min1 = 0;\n int min2 = 0;\n for (int i = 1; i < number - 1; i++) {\n max1 = location[i] - location[0];\n max2 = location[number - 1] - location[i];\n min1 = location[i] - location[i - 1];\n min2 = location[i + 1] - location[i];\n max = max1;\n if (max1 < max2) {\n max = max2;\n }\n min = min1;\n if (min1 > min2) {\n min = min2;\n }\n printf(\"%d %d\\n\", min, max);\n }\n max = location[number - 1] - location[0];\n min = location[number - 1] - location[number - 2];\n printf(\"%d %d\\n\", min, max);\n}", "rust_code": "fn solution() {\n use std::io;\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 let cities: Vec = buf\n .split_whitespace()\n .map(|str| str.trim().parse().unwrap())\n .collect();\n for (i, &city) in cities.iter().enumerate() {\n let max = std::cmp::max(\n (cities[0] - city).abs(),\n (cities[cities.len() - 1] - city).abs(),\n );\n let mut min = i64::max_value();\n if i != 0 {\n min = std::cmp::min(min, (cities[i - 1] - city).abs());\n }\n if i != cities.len() - 1 {\n min = std::cmp::min(min, (cities[i + 1] - city).abs());\n }\n println!(\"{} {}\", min, max);\n }\n}", "difficulty": "easy"} {"problem_id": "1796", "problem_description": "A batch of $$$n$$$ goods ($$$n$$$ — an even number) is brought to the store, $$$i$$$-th of which has weight $$$a_i$$$. Before selling the goods, they must be packed into packages. After packing, the following will be done: There will be $$$\\frac{n}{2}$$$ packages, each package contains exactly two goods; The weight of the package that contains goods with indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$) is $$$a_i + a_j$$$. With this, the cost of a package of weight $$$x$$$ is always $$$\\left \\lfloor\\frac{x}{k}\\right\\rfloor$$$ burles (rounded down), where $$$k$$$ — a fixed and given value.Pack the goods to the packages so that the revenue from their sale is maximized. In other words, make such $$$\\frac{n}{2}$$$ pairs of given goods that the sum of the values $$$\\left \\lfloor\\frac{x_i}{k} \\right \\rfloor$$$, where $$$x_i$$$ is the weight of the package number $$$i$$$ ($$$1 \\le i \\le \\frac{n}{2}$$$), is maximal.For example, let $$$n = 6, k = 3$$$, weights of goods $$$a = [3, 2, 7, 1, 4, 8]$$$. Let's pack them into the following packages. In the first package we will put the third and sixth goods. Its weight will be $$$a_3 + a_6 = 7 + 8 = 15$$$. The cost of the package will be $$$\\left \\lfloor\\frac{15}{3}\\right\\rfloor = 5$$$ burles. In the second package put the first and fifth goods, the weight is $$$a_1 + a_5 = 3 + 4 = 7$$$. The cost of the package is $$$\\left \\lfloor\\frac{7}{3}\\right\\rfloor = 2$$$ burles. In the third package put the second and fourth goods, the weight is $$$a_2 + a_4 = 2 + 1 = 3$$$. The cost of the package is $$$\\left \\lfloor\\frac{3}{3}\\right\\rfloor = 1$$$ burle. With this packing, the total cost of all packs would be $$$5 + 2 + 1 = 8$$$ burles.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int T = 0; T < t; T++) {\n int n;\n int k;\n int x;\n scanf(\"%d%d\", &n, &k);\n int *r = calloc(k, sizeof(int));\n long long tot = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &x);\n tot += (x / k);\n r[x % k]++;\n }\n\n x = k >> 1;\n for (int i = 1; i < x; i++) {\n if (r[i] < r[k - i]) {\n tot += r[i];\n r[k - i - 1] = r[k - i - 1] + r[k - i] - r[i];\n } else {\n tot += r[k - i];\n }\n }\n if (k > 1) {\n if (k & 1) {\n if (r[x] >= r[x + 1]) {\n tot += r[x + 1];\n } else {\n tot = tot + r[x] + ((r[x + 1] - r[x]) >> 1);\n }\n } else {\n tot += (r[x] >> 1);\n }\n }\n printf(\"%lld\\n\", tot);\n free(r);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (stdin, stdout) = (io::stdin(), io::stdout());\n let mut scan = Scanner::new(stdin.lock());\n let mut out = io::BufWriter::new(stdout.lock());\n\n let t = scan.token::();\n\n for _ in 0..t {\n let n = scan.token::();\n let k = scan.token::();\n let mut A = vec![];\n let mut M = vec![];\n for __ in 0..n {\n let a = scan.token::();\n A.push(a);\n M.push(a % k);\n }\n\n M.sort();\n let mut Q = VecDeque::new();\n\n for i in 0..n {\n Q.push_back(M[i]);\n }\n\n let mut ans = 0;\n while !Q.is_empty() {\n let f = Q.pop_front().unwrap();\n let b = Q.pop_back().unwrap();\n if (f + b) >= k {\n ans += (f + b) / k;\n } else {\n Q.push_back(b);\n let f2 = Q.pop_front().unwrap();\n ans += (f + f2) / k;\n }\n }\n\n for i in 0..n {\n ans += (A[i] - (A[i] % k)) / k\n }\n\n writeln!(out, \"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1797", "problem_description": "Nick had received an awesome array of integers $$$a=[a_1, a_2, \\dots, a_n]$$$ as a gift for his $$$5$$$ birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product $$$a_1 \\cdot a_2 \\cdot \\dots a_n$$$ of its elements seemed to him not large enough.He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index $$$i$$$ ($$$1 \\le i \\le n$$$) and do $$$a_i := -a_i - 1$$$.For example, he can change array $$$[3, -1, -4, 1]$$$ to an array $$$[-4, -1, 3, 1]$$$ after applying this operation to elements with indices $$$i=1$$$ and $$$i=3$$$. Kolya had immediately understood that sometimes it's possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index. Help Kolya and print the array with the maximal possible product of elements $$$a_1 \\cdot a_2 \\cdot \\dots a_n$$$ which can be received using only this operation in some order.If there are multiple answers, print any of them.", "c_code": "int solution() {\n int n;\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 }\n for (int i = 0; i < n; i++) {\n if (a[i] >= 0) {\n b[i] = -1 * (a[i] + 1);\n } else {\n b[i] = a[i];\n }\n }\n if (n % 2 == 0) {\n for (int i = 0; i < n; i++) {\n printf(\"%d \", b[i]);\n }\n } else {\n int j = 0;\n int c = b[0];\n for (int i = 0; i < n; i++) {\n if (c > b[i]) {\n c = b[i];\n j = i;\n }\n }\n b[j] = -1 * b[j] - 1;\n for (int i = 0; i < n; i++) {\n printf(\"%d \", b[i]);\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut it = stdin.lock().lines();\n let _input = it.next().unwrap().unwrap();\n let input = it.next().unwrap().unwrap();\n let a: Vec = input\n .split_whitespace()\n .map(|s| s.parse().unwrap())\n .collect();\n let b: Vec = a\n .clone()\n .into_iter()\n .map(|i| if i >= 0 { -i - 1 } else { i })\n .collect();\n let len = a.len();\n\n if len.is_multiple_of(2) {\n for i in b.into_iter() {\n print!(\"{} \", if i >= 0 { -i - 1 } else { i });\n }\n } else {\n let min = b.clone().into_iter().min().unwrap();\n let mut changed = false;\n for i in b.into_iter() {\n if i == min && !changed {\n print!(\"{} \", -i - 1);\n changed = true;\n } else {\n print!(\"{} \", i);\n }\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1798", "problem_description": "

鉛筆 (Pencils)

\n\n

問題文

\n\n

\nJOI 君は鉛筆を N 本買うために近くの文房具店に行くことにした.\n

\n\n

\n文房具店では鉛筆が一定の本数ずつのセットで売られている.セット XA 本で B 円,セット YC 本で D 円である.\n

\n\n

\nJOI 君はセット X かセット Y の一方を選び,選んだセットをいくつか購入する.両方のセットを購入することはできない.N 本以上の鉛筆を得るために必要な金額の最小値を求めよ.\n

\n\n

制約

\n\n
    \n
  • 1 \\leq N \\leq 1000
  • \n
  • 1 \\leq A \\leq 1000
  • \n
  • 1 \\leq B \\leq 1000
  • \n
  • 1 \\leq C \\leq 1000
  • \n
  • 1 \\leq D \\leq 1000
  • \n
\n\n

入力・出力

\n\n

\n入力
\n入力は以下の形式で標準入力から与えられる.
\nN A B C D\n

\n\n

\n出力
\nJOI 君が N 本以上の鉛筆を手に入れるのに必要な金額の最小値を出力せよ.
\n\n

入出力例

\n\n\n入力例 1
\n
\n10 3 100 5 180\n
\n\n\n出力例 1
\n
\n360\n
\n\n

\nJOI 君は10本の鉛筆を入手したい.セット X は3本で100円,セット Y は5本で180円である.この時,セット X を選んだ場合は,セットを4つ購入する必要があり400円必要である.セット Y を選んだ場合は,セットを2つ購入する必要があり360円必要である.したがって,必要な金額の最小値は400円と360円の小さい方で360円である.\n

\n\n
\n\n\n入力例 2
\n
\n6 2 200 3 300\n
\n\n\n出力例 2
\n
\n600\n
\n\n

\nこのとき,セット X を選んだ場合もセット Y を選んだ場合も必要な金額は600円である.必要な金額の最小値は600円である.\n

\n\n\n
\n

\n \"クリエイティブ・コモンズ・ライセンス\"\n
\n \n 情報オリンピック日本委員会作 『第 17 回日本情報オリンピック JOI 2017/2018 予選競技課題』\n

", "c_code": "int solution() {\n int n[5];\n int i;\n int m = 1;\n for (i = 0; i < 5; i++) {\n scanf(\"%d\", &n[i]);\n }\n i = 1;\n while (n[0] > n[1] * m) {\n ++m;\n }\n while (n[0] > n[3] * i) {\n ++i;\n }\n n[2] *= m, n[4] *= i;\n if (n[2] > n[4]) {\n n[2] = n[4];\n }\n printf(\"%d\\n\", n[2]);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n let handle = std::io::stdin();\n handle.read_line(&mut buf).unwrap();\n let data: Vec = buf.split_whitespace().map(|a| a.parse().unwrap()).collect();\n\n let (n, a, b, c, d) = (data[0], data[1], data[2], data[3], data[4]);\n\n let mut total = 0;\n let mut a_money = 0;\n while n > total {\n total += a;\n a_money += b;\n }\n let mut total = 0;\n let mut c_money = 0;\n while n > total {\n total += c;\n c_money += d;\n }\n let ans = if a_money < c_money { a_money } else { c_money };\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1799", "problem_description": "There are two infinite sources of water: hot water of temperature $$$h$$$; cold water of temperature $$$c$$$ ($$$c < h$$$). You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water $$$\\dots$$$ and so on $$$\\dots$$$ Note that you always start with the cup of hot water.The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.You want to achieve a temperature as close as possible to $$$t$$$. So if the temperature in the barrel is $$$t_b$$$, then the absolute difference of $$$t_b$$$ and $$$t$$$ ($$$|t_b - t|$$$) should be as small as possible.How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $$$t$$$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.", "c_code": "int solution() {\n int T;\n scanf(\"%d\", &T);\n long long int h;\n long long int c;\n long long int t;\n long long int min;\n long long int mid;\n long long int max;\n for (; T > 0; T--) {\n scanf(\"%lld %lld %lld\", &h, &c, &t);\n if (h <= t) {\n printf(\"1\\n\");\n continue;\n }\n if (h + c >= 2 * t) {\n printf(\"2\\n\");\n continue;\n }\n min = 0;\n max = 1000000009;\n while (max - min > 1) {\n mid = (max + min) / 2;\n if (c * mid + h * (mid + 1) < t * (2 * mid + 1)) {\n max = mid;\n } else {\n min = mid;\n }\n }\n if ((c * min + h * (min + 1)) * (2 * max + 1) -\n t * (2 * min + 1) * (2 * max + 1) <=\n t * (2 * min + 1) * (2 * max + 1) -\n (c * max + h * (max + 1)) * (2 * min + 1)) {\n printf(\"%lld\\n\", (2 * min) + 1);\n } else {\n printf(\"%lld\\n\", (2 * max) + 1);\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 let t: usize = itr.next().unwrap().parse().unwrap();\n let mut out = Vec::new();\n for _ in 0..t {\n let h: i64 = itr.next().unwrap().parse().unwrap();\n let c: i64 = itr.next().unwrap().parse().unwrap();\n let t: i64 = itr.next().unwrap().parse().unwrap();\n if t == h {\n writeln!(out, \"1\").ok();\n } else if t <= (h + c) / 2 {\n writeln!(out, \"2\").ok();\n } else {\n let mut l = 1;\n let mut r = 1000000;\n while r - l > 1 {\n let mid = (l + r) >> 1;\n\n if (h + c) * (mid - 1) + h > t * (mid * 2 - 1) {\n l = mid;\n } else {\n r = mid;\n }\n }\n\n if 2 * t * (2 * l - 1) * (2 * l + 1)\n < ((h + c) * (l - 1) + h) * (2 * l + 1) + (l * c + (l + 1) * h) * (2 * l - 1)\n {\n writeln!(out, \"{}\", r + r - 1).ok();\n } else {\n writeln!(out, \"{}\", l + l - 1).ok();\n }\n }\n }\n stdout().write_all(&out).unwrap();\n}", "difficulty": "easy"} {"problem_id": "1800", "problem_description": "One very important person has a piece of paper in the form of a rectangle a × b.Also, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size xi × yi. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees).A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals?", "c_code": "int solution() {\n long long int n;\n long long int a;\n long long int b;\n long long int c[101][2];\n long long int i;\n long long int d[101];\n long long int e[101][2];\n long long int x = 0;\n long long int s = 0;\n long long int t;\n long long int j;\n long long int q;\n scanf(\"%lld %lld %lld\", &n, &a, &b);\n if (a < b) {\n t = a;\n a = b;\n b = t;\n }\n for (i = 1; i <= n; i++) {\n d[i] = 1;\n }\n for (i = 1; i <= n; i++) {\n scanf(\"%lld %lld\", &c[i][0], &c[i][1]);\n if (c[i][0] < c[i][1]) {\n t = c[i][0];\n c[i][0] = c[i][1];\n c[i][1] = t;\n }\n if (c[i][0] > a || c[i][1] > b) {\n d[i] = 0;\n } else {\n x++;\n e[x][0] = i;\n e[x][1] = c[i][0] * c[i][1];\n }\n }\n for (i = 1; i <= x; i++) {\n for (j = i + 1; j <= x; j++) {\n q = e[i][1] + e[j][1];\n if (q > s) {\n if ((c[e[i][0]][0] + c[e[j][0]][0] <= a) ||\n (c[e[i][0]][1] + c[e[j][0]][1] <= a && c[e[i][0]][0] <= b &&\n c[e[j][0]][0] <= b) ||\n (c[e[i][0]][1] + c[e[j][0]][1] <= b) ||\n (c[e[i][0]][0] + c[e[j][0]][0] <= b) ||\n (c[e[i][0]][0] + c[e[j][0]][1] <= a && c[e[i][0]][1] <= b &&\n c[e[j][0]][0] <= b) ||\n (c[e[i][0]][1] + c[e[j][0]][0] <= a && c[e[i][0]][0] <= b &&\n c[e[j][0]][1] <= b)) {\n s = q;\n }\n }\n }\n }\n printf(\"%lld\", s);\n return 0;\n}", "rust_code": "fn solution() {\n let (n, a, b) = {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let mut it = input\n .split_whitespace()\n .map(|k| k.parse::().unwrap());\n (it.next().unwrap(), it.next().unwrap(), it.next().unwrap())\n };\n\n let v = {\n let mut v = Vec::new();\n for _ in 0..n {\n let mut input = String::new();\n stdin().read_line(&mut input).unwrap();\n let mut it = input\n .split_whitespace()\n .map(|k| k.parse::().unwrap());\n v.push([it.next().unwrap(), it.next().unwrap()]);\n }\n v\n };\n\n let mut ans = 0;\n\n let r = [a, b];\n\n for (ind, p) in v.iter().enumerate() {\n for q in v.iter().skip(ind + 1) {\n for i in 0..2 {\n for j in 0..2 {\n for k in 0..2 {\n let i2 = (i + 1) % 2;\n let j2 = (j + 1) % 2;\n let k2 = (k + 1) % 2;\n if p[i] + q[j] <= r[k] && max(p[i2], q[j2]) <= r[k2] {\n ans = max(ans, p[0] * p[1] + q[0] * q[1]);\n }\n }\n }\n }\n }\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1801", "problem_description": "You are given a positive integer $$$n$$$, it is guaranteed that $$$n$$$ is even (i.e. divisible by $$$2$$$).You want to construct the array $$$a$$$ of length $$$n$$$ such that: The first $$$\\frac{n}{2}$$$ elements of $$$a$$$ are even (divisible by $$$2$$$); the second $$$\\frac{n}{2}$$$ elements of $$$a$$$ are odd (not divisible by $$$2$$$); all elements of $$$a$$$ are distinct and positive; the sum of the first half equals to the sum of the second half ($$$\\sum\\limits_{i=1}^{\\frac{n}{2}} a_i = \\sum\\limits_{i=\\frac{n}{2} + 1}^{n} a_i$$$). If there are multiple answers, you can print any. It is not guaranteed that the answer exists.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n int sum = 0;\n int num = 0;\n scanf(\"%d\", &n);\n if (n % 4 != 0) {\n printf(\"NO\\n\");\n } else {\n printf(\"YES\\n\");\n for (int i = 1; i < (n / 2) + 1; i++) {\n int x = 2 * i;\n sum = sum + x;\n printf(\"%d \", 2 * i);\n }\n for (int i = 1; i < (n / 2); i++) {\n int h = (2 * i) - 1;\n num = num + h;\n printf(\"%d \", ((2 * i) - 1));\n }\n printf(\"%d\", (sum - num));\n printf(\"\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut stdin = BufReader::new(io::stdin());\n let mut stdout = BufWriter::new(io::stdout());\n\n let mut buffer = String::new();\n stdin.read_line(&mut buffer).unwrap();\n let t = buffer.trim().parse().unwrap();\n for _ in 0..t {\n let mut buffer = String::new();\n stdin.read_line(&mut buffer).unwrap();\n let n: usize = buffer.trim().parse().unwrap();\n\n if !(n / 2).is_multiple_of(2) {\n writeln!(stdout, \"NO\").unwrap();\n } else {\n let mut a = vec![0; n];\n for i in (0..n / 2).step_by(2) {\n a[i] = i * 6 + 2;\n a[i + 1] = i * 6 + 4;\n a[n / 2 + i] = i * 6 + 1;\n a[n / 2 + i + 1] = i * 6 + 5;\n }\n let a: Vec = a.iter().map(|x| x.to_string()).collect();\n writeln!(stdout, \"YES\").unwrap();\n writeln!(stdout, \"{}\", a.join(\" \")).unwrap();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1802", "problem_description": "

A: テスト

\n\n

問題

\n

\n $N$ 個の席が一直線上に並んでいる教室で $M$ 人の生徒がテストを受けることになった。\n 席には、前から $1 \\dots N$ の番号が振られており、席 $1$ つにつき生徒 $1$ 人が座れる。\n

\n\n

\n いま、 各生徒は、 $A_1, \\dots, A_M$ 番の席に座っている。\n

\n\n

\n テストを始めるためには、以下の条件を満たさなければならない。\n

    \n
  • $1 \\dots M$ 番のどの席にも生徒が座っている。
  • \n
\n

\n\n

\n そこで、条件を満たすまで次の操作を繰り返すことにした。\n

    \n
  • 最も後ろに座っている生徒を移動させ、空いている席のうち最も前に座らせる。
  • \n
\n

\n\n

\n 条件を満たすために必要な操作回数を求めよ。\n

\n\n

制約

\n
    \n
  • 入力値は全て整数である。
  • \n
  • $1 \\leq N \\leq 1000$
  • \n
  • $1 \\leq M \\leq N$
  • \n
  • $1 \\leq A_i \\leq N$
  • \n
  • $1 \\leq i < j \\leq M$ ならば $A_i < A_j$
  • \n\t\t\t\t\t
\n\n

入力形式

\n

入力は以下の形式で与えられる。

\n\n

\n $N\\ M$
\n $A_1 \\dots A_M$
\n

\n\n

出力

\n

条件を満たすために必要な操作回数を出力せよ。また、末尾に改行も出力せよ。

\n\n

サンプル

\n\n

サンプル入力 1

\n
\n6 4\n1 4 5 6\n
\n

サンプル出力 1

\n
\n2\n
\n\n

サンプル入力 2

\n
\n10 3\n1 2 3\n
\n

サンプル出力 2

\n
\n0\n
", "c_code": "int solution() {\n int m;\n scanf(\"%*d%d\", &m);\n int ans = 0;\n for (int i = 0; i < m; i++) {\n int t;\n scanf(\"%d\", &t);\n if (t <= m) {\n ans++;\n }\n }\n printf(\"%d\\n\", m - ans);\n}", "rust_code": "fn solution() {\n input!(n: usize, m: usize, a: [usize1; m]);\n let mut ans = 0;\n for &a in a.iter() {\n if a >= m {\n ans += 1;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1803", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Snuke is going to open a contest named \"AtCoder s Contest\".\nHere, s is a string of length 1 or greater, where the first character is an uppercase English letter, and the second and subsequent characters are lowercase English letters.

\n

Snuke has decided to abbreviate the name of the contest as \"AxC\".\nHere, x is the uppercase English letter at the beginning of s.

\n

Given the name of the contest, print the abbreviation of the name.

\n
\n
\n
\n
\n

Constraints

    \n
  • The length of s is between 1 and 100, inclusive.
  • \n
  • The first character in s is an uppercase English letter.
  • \n
  • The second and subsequent characters in s are lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
AtCoder s Contest\n
\n
\n
\n
\n
\n

Output

Print the abbreviation of the name of the contest.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

AtCoder Beginner Contest\n
\n
\n
\n
\n
\n

Sample Output 1

ABC\n
\n

The contest in which you are participating now.

\n
\n
\n
\n
\n
\n

Sample Input 2

AtCoder Snuke Contest\n
\n
\n
\n
\n
\n

Sample Output 2

ASC\n
\n

This contest does not actually exist.

\n
\n
\n
\n
\n
\n

Sample Input 3

AtCoder X Contest\n
\n
\n
\n
\n
\n

Sample Output 3

AXC\n
\n
\n
", "c_code": "int solution(void) {\n char text0[100];\n char text[100];\n char text1[100];\n scanf(\"%s\", text0);\n scanf(\"%s\", text);\n scanf(\"%s\", text1);\n printf(\"%c\", *text0);\n printf(\"%c\", *text);\n printf(\"%c\", *text1);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n io::stdin().read_line(&mut buf).expect(\"\");\n let iter = buf.split_whitespace();\n for i in iter {\n let s: Vec = i.chars().collect::>();\n print!(\"{}\", s[0]);\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1804", "problem_description": "For the New Year, Polycarp decided to send postcards to all his $$$n$$$ friends. He wants to make postcards with his own hands. For this purpose, he has a sheet of paper of size $$$w \\times h$$$, which can be cut into pieces.Polycarp can cut any sheet of paper $$$w \\times h$$$ that he has in only two cases: If $$$w$$$ is even, then he can cut the sheet in half and get two sheets of size $$$\\frac{w}{2} \\times h$$$; If $$$h$$$ is even, then he can cut the sheet in half and get two sheets of size $$$w \\times \\frac{h}{2}$$$; If $$$w$$$ and $$$h$$$ are even at the same time, then Polycarp can cut the sheet according to any of the rules above.After cutting a sheet of paper, the total number of sheets of paper is increased by $$$1$$$.Help Polycarp to find out if he can cut his sheet of size $$$w \\times h$$$ at into $$$n$$$ or more pieces, using only the rules described above.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n\n while (t-- != 0) {\n\n int w = 0;\n int h = 0;\n int n = 0;\n int count = 1;\n\n scanf(\"%d%d%d\", &w, &h, &n);\n\n while (h % 2 == 0) {\n count *= 2;\n h = h / 2;\n }\n while (w % 2 == 0) {\n count *= 2;\n w = w / 2;\n }\n\n if (count >= n) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\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\n for _ in 0..n.trim().parse::().unwrap() {\n let mut a = String::new();\n io::stdin().read_line(&mut a).unwrap();\n\n let array = a.trim().split(\" \").collect::>();\n let mut w = array[0].parse::().unwrap();\n let mut h = array[1].parse::().unwrap();\n let n = array[2].parse::().unwrap();\n\n let mut counter = 1;\n\n while w % 2 == 0 || h % 2 == 0 {\n if w % 2 == 0 {\n w /= 2;\n counter *= 2;\n } else if h % 2 == 0 {\n h /= 2;\n counter *= 2;\n };\n }\n\n if counter >= n {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1805", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There is a rooted tree (see Notes) with N vertices numbered 1 to N.\nEach of the vertices, except the root, has a directed edge coming from its parent.\nNote that the root may not be Vertex 1.

\n

Takahashi has added M new directed edges to this graph.\nEach of these M edges, u \\rightarrow v, extends from some vertex u to its descendant v.

\n

You are given the directed graph with N vertices and N-1+M edges after Takahashi added edges.\nMore specifically, you are given N-1+M pairs of integers, (A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}), which represent that the i-th edge extends from Vertex A_i to Vertex B_i.

\n

Restore the original rooted tree.

\n
\n
\n
\n
\n

Notes

For \"tree\" and other related terms in graph theory, see the article in Wikipedia, for example.

\n
\n
\n
\n
\n

Constraints

    \n
  • 3 \\leq N
  • \n
  • 1 \\leq M
  • \n
  • N + M \\leq 10^5
  • \n
  • 1 \\leq A_i, B_i \\leq N
  • \n
  • A_i \\neq B_i
  • \n
  • If i \\neq j, (A_i, B_i) \\neq (A_j, B_j).
  • \n
  • The graph in input can be obtained by adding M edges satisfying the condition in the problem statement to a rooted tree with N vertices.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nA_1 B_1\n:\nA_{N-1+M} B_{N-1+M}\n
\n
\n
\n
\n
\n

Output

Print N lines.\nIn the i-th line, print 0 if Vertex i is the root of the original tree, and otherwise print the integer representing the parent of Vertex i in the original tree.

\n

Note that it can be shown that the original tree is uniquely determined.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 1\n1 2\n1 3\n2 3\n
\n
\n
\n
\n
\n

Sample Output 1

0\n1\n2\n
\n

The graph in this input is shown below:

\n

\"\"

\n

It can be seen that this graph is obtained by adding the edge 1 \\rightarrow 3 to the rooted tree 1 \\rightarrow 2 \\rightarrow 3.

\n
\n
\n
\n
\n
\n

Sample Input 2

6 3\n2 1\n2 3\n4 1\n4 2\n6 1\n2 6\n4 6\n6 5\n
\n
\n
\n
\n
\n

Sample Output 2

6\n4\n2\n0\n6\n2\n
\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n m = n - 1 + m;\n int **graph;\n int to[100005] = {0};\n graph = (int **)malloc(sizeof(int *) * (n + 10));\n for (int i = 0; i < n + 10; i++) {\n graph[i] = (int *)malloc(sizeof(int) * (m));\n }\n while (m--) {\n int a;\n int b;\n scanf(\"%d %d\", &a, &b);\n graph[a][++graph[a][0]] = b;\n to[b]++;\n }\n\n int queue[100005];\n int front = 0;\n int rear = 0;\n for (int i = 1; i <= n; i++) {\n if (!to[i]) {\n queue[rear++] = i;\n }\n }\n\n int topo[100005] = {0};\n while (front != rear) {\n int now = queue[front++];\n topo[++topo[0]] = now;\n for (int i = 1; i <= graph[now][0]; i++) {\n if (--to[graph[now][i]] == 0) {\n queue[rear++] = graph[now][i];\n }\n }\n }\n\n int ans[100005] = {0};\n for (int i = 1; i <= topo[0]; i++) {\n for (int j = 1; j <= graph[topo[i]][0]; j++) {\n ans[graph[topo[i]][j]] = topo[i];\n }\n }\n\n for (int i = 1; i <= n; i++) {\n printf(\"%d\\n\", ans[i]);\n }\n\n return 0;\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\n let n = get!();\n let m = get!();\n let mut tree = vec![vec![]; n];\n let mut undi = vec![vec![]; n];\n let mut cnt = vec![0_i32; n];\n let mut pair = vec![std::usize::MAX; n];\n for _ in 0..n + m - 1 {\n let a = get!() - 1;\n let b = get!() - 1;\n cnt[a] += 1;\n tree[a].push(b);\n undi[b].push(a);\n }\n\n let mut ps = vec![];\n\n for i in 0..n {\n if cnt[i] == 0 {\n ps.push(i);\n }\n }\n\n while !ps.is_empty() {\n for &p in ps.iter() {\n for &t in tree[p].iter() {\n if cnt[t] == 0 {\n cnt[t] = -1;\n pair[t] = p;\n }\n }\n }\n let mut es = vec![];\n for &p in ps.iter() {\n for &u in undi[p].iter() {\n cnt[u] -= 1;\n if cnt[u] == 0 {\n es.push(u);\n }\n }\n }\n ps = es;\n }\n for &p in pair.iter() {\n if p == std::usize::MAX {\n println!(\"0\");\n } else {\n println!(\"{}\", p + 1);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1806", "problem_description": "You are given two strings $$$s$$$ and $$$t$$$, both consisting of exactly $$$k$$$ lowercase Latin letters, $$$s$$$ is lexicographically less than $$$t$$$.Let's consider list of all strings consisting of exactly $$$k$$$ lowercase Latin letters, lexicographically not less than $$$s$$$ and not greater than $$$t$$$ (including $$$s$$$ and $$$t$$$) in lexicographical order. For example, for $$$k=2$$$, $$$s=$$$\"az\" and $$$t=$$$\"bf\" the list will be [\"az\", \"ba\", \"bb\", \"bc\", \"bd\", \"be\", \"bf\"].Your task is to print the median (the middle element) of this list. For the example above this will be \"bc\".It is guaranteed that there is an odd number of strings lexicographically not less than $$$s$$$ and not greater than $$$t$$$.", "c_code": "int solution() {\n int i;\n int j;\n int k;\n int n;\n scanf(\"%d\", &n);\n char a[n + 1];\n char b[n + 1];\n scanf(\"%s%s\", a, b);\n int c[n];\n int d[n];\n for (i = 0; i < n; i++) {\n c[i] = a[i] - 97;\n d[i] = b[i] - 97;\n c[i] = c[i] + d[i];\n }\n for (i = n - 1; i > 0; i--) {\n c[i - 1] += c[i] / 26;\n c[i] = c[i] % 26;\n }\n for (i = 0; i < n; i++) {\n k = c[i] % 2;\n c[i] = c[i] / 2;\n c[i + 1] += k * 26;\n printf(\"%c\", c[i] + 97);\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n stdin().read_line(&mut line).unwrap();\n let len = line.trim().parse::().unwrap();\n\n let mut s = String::new();\n let mut t = String::new();\n {\n let i = stdin();\n let mut lock = i.lock();\n lock.read_line(&mut s).unwrap();\n lock.read_line(&mut t).unwrap();\n }\n let s: Vec<_> = s.trim().chars().map(|c| (c as u8) - b'a').collect();\n let t: Vec<_> = t.trim().chars().map(|c| (c as u8) - b'a').collect();\n\n let mut sum: Vec<_> = (0..len + 1).map(|_| 0).collect();\n for i in (1..len + 1).map(|i| len + 1 - i) {\n sum[i] += s[i - 1] + t[i - 1];\n while sum[i] >= 26 {\n sum[i] -= 26;\n sum[i - 1] += 1;\n }\n }\n\n let mut result: Vec<_> = (0..len + 1).map(|_| 'a').collect();\n let mut carry = 0;\n for i in 0..len + 1 {\n result[i] = (b'a' + (26 * carry + sum[i]) / 2) as char;\n carry = sum[i] % 2;\n }\n\n let out: String = result.into_iter().skip(1).collect();\n println!(\"{}\", out);\n}", "difficulty": "medium"} {"problem_id": "1807", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have N+1 integers: 10^{100}, 10^{100}+1, ..., 10^{100}+N.

\n

We will choose K or more of these integers. Find the number of possible values of the sum of the chosen numbers, modulo (10^9+7).

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2\\times 10^5
  • \n
  • 1 \\leq K \\leq N+1
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\n
\n
\n
\n
\n
\n

Output

Print the number of possible values of the sum, modulo (10^9+7).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

The sum can take 10 values, as follows:

\n
    \n
  • (10^{100})+(10^{100}+1)=2\\times 10^{100}+1
  • \n
  • (10^{100})+(10^{100}+2)=2\\times 10^{100}+2
  • \n
  • (10^{100})+(10^{100}+3)=(10^{100}+1)+(10^{100}+2)=2\\times 10^{100}+3
  • \n
  • (10^{100}+1)+(10^{100}+3)=2\\times 10^{100}+4
  • \n
  • (10^{100}+2)+(10^{100}+3)=2\\times 10^{100}+5
  • \n
  • (10^{100})+(10^{100}+1)+(10^{100}+2)=3\\times 10^{100}+3
  • \n
  • (10^{100})+(10^{100}+1)+(10^{100}+3)=3\\times 10^{100}+4
  • \n
  • (10^{100})+(10^{100}+2)+(10^{100}+3)=3\\times 10^{100}+5
  • \n
  • (10^{100}+1)+(10^{100}+2)+(10^{100}+3)=3\\times 10^{100}+6
  • \n
  • (10^{100})+(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=4\\times 10^{100}+6
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

200000 200001\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

We must choose all of the integers, so the sum can take just 1 value.

\n
\n
\n
\n
\n
\n

Sample Input 3

141421 35623\n
\n
\n
\n
\n
\n

Sample Output 3

220280457\n
\n
\n
", "c_code": "int solution(void) {\n int N;\n int K;\n scanf(\"%d %d\", &N, &K);\n\n if (N + 1 == K) {\n printf(\"1\\n\");\n return 0;\n }\n if (N == K) {\n printf(\"%d\\n\", 1 + (N + 1));\n return 0;\n }\n\n long long result = 1 + (N + 1);\n long long i = 1;\n long long mod = 1000000007;\n long long n = (long long)N;\n while (N - i >= K) {\n long long max = ((i + 1 + n) * (n - i)) / 2;\n long long min = ((0 + n - i - 1) * (n - i)) / 2;\n result += (long long)((max - min) + 1);\n result = result % mod;\n i++;\n }\n printf(\"%lld\\n\", result);\n\n return 0;\n}", "rust_code": "fn solution() {\n let a: i64 = 10;\n let mo: i64 = a.pow(9) + 7;\n let mut st = String::new();\n stdin().read_line(&mut st).unwrap();\n let (n, k) = {\n let nk: Vec = st.trim().split(\" \").map(|x| x.parse().unwrap()).collect();\n (nk[0], nk[1])\n };\n let mut r: i64 = 0;\n for i in k..(n + 2) {\n let s: i64 = i * (i + 1) / 2;\n let e: i64 = i * (n + 1 + (n + 2 - i)) / 2;\n r += e - s + 1;\n r %= mo;\n }\n println!(\"{}\", r);\n}", "difficulty": "medium"} {"problem_id": "1808", "problem_description": "

\nThere are n processes in a queue. Each process has namei and timei. The round-robin scheduling handles the processes in order. A round-robin scheduler gives each process a quantum (a time slot) and interrupts the process if it is not completed by then. The process is resumed and moved to the end of the queue, then the scheduler handles the next process in the queue.\n

\n\n

\nFor example, we have the following queue with the quantum of 100ms.\n

\n\n
\nA(150) - B(80) - C(200) - D(200)\n
\n\n

\nFirst, process A is handled for 100ms, then the process is moved to the end of the queue with the remaining time (50ms).\n

\n\n
\nB(80) - C(200) - D(200) - A(50)\n
\n\n

\nNext, process B is handled for 80ms. The process is completed with the time stamp of 180ms and removed from the queue.\n

\n\n
\nC(200) - D(200) - A(50)\n
\n\n

\nYour task is to write a program which simulates the round-robin scheduling. \n\n\n

\n\n\n

Input

\n\n

\nn q
\nname1 time1
\nname2 time2
\n...
\nnamen timen
\n

\n\n

\nIn the first line the number of processes n and the quantum q are given separated by a single space.\n

\n\n

\nIn the following n lines, names and times for the n processes are given. namei and timei are separated by a single space.\n

\n\n

Output

\n\n

\nFor each process, prints its name and the time the process finished in order.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ n ≤ 100000
  • \n
  • 1 ≤ q ≤ 1000
  • \n
  • 1 ≤ timei ≤ 50000
  • \n
  • 1 ≤ length of namei ≤ 10
  • \n
  • 1 ≤ Sum of timei ≤ 1000000
  • \n
\n\n

Sample Input 1

\n
\n5 100\np1 150\np2 80\np3 200\np4 350\np5 20\n
\n

Sample Output 1

\n
\np2 180\np5 400\np1 450\np3 550\np4 800\n
\n\n\n\n

Notes

\n\nTemplate in C", "c_code": "int solution(void) {\n\n int i;\n int n;\n int q;\n\n scanf(\"%d %d\", &n, &q);\n\n pro pr[n];\n\n for (i = 0; i < n; i++) {\n scanf(\"%*c%s %d\", pr[i].na, &pr[i].ti);\n }\n\n int top = 0;\n int tail = 0;\n int tim = 0;\n int sta = 0;\n\n while (1) {\n\n if (sta == 1 && top == tail) {\n break;\n }\n\n if (pr[top].ti <= q) {\n\n tim += pr[top].ti;\n\n pr[top].ti = 0;\n\n printf(\"%s %d\\n\", pr[top].na, tim);\n\n }\n\n else {\n\n tim += q;\n\n pr[top].ti -= q;\n\n pr[tail] = pr[top];\n\n tail++;\n }\n\n top++;\n\n if (top != tail) {\n sta = 1;\n }\n\n if (top == n) {\n top = 0;\n }\n if (tail == n) {\n tail = 0;\n }\n }\n\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 a = s.split_whitespace().collect::>();\n let n = a[0].parse::().unwrap();\n let q = a[1].parse::().unwrap();\n\n let mut queue: VecDeque<(String, i64)> = VecDeque::new();\n\n for _ in 0..n {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n let a = s.split_whitespace().collect::>();\n let name = a[0].to_string().clone();\n let t = a[1].parse::().unwrap();\n queue.push_back((name, t));\n }\n\n let mut elaps = 0;\n while !queue.is_empty() {\n let mut u = queue.pop_front().unwrap();\n let a = if u.1 < q { u.1 } else { q };\n u.1 -= a;\n elaps += a;\n if u.1 > 0 {\n queue.push_back((u.0, u.1));\n } else {\n println!(\"{} {}\", u.0, elaps);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1809", "problem_description": "Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.Initially, on day $$$1$$$, there is one bacterium with mass $$$1$$$.Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass $$$m$$$ splits, it becomes two bacteria of mass $$$\\frac{m}{2}$$$ each. For example, a bacterium of mass $$$3$$$ can split into two bacteria of mass $$$1.5$$$.Also, every night, the mass of every bacteria will increase by one.Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly $$$n$$$. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int num;\n int arr[1000000];\n scanf(\"%d\", &num);\n int massbact = 1;\n int rate = 1;\n int count = 0;\n while (1) {\n if (num - massbact >= 4 * rate) {\n arr[count++] = rate;\n rate *= 2;\n massbact += rate;\n ;\n } else if (num - massbact <= 2 * rate) {\n arr[count++] = (num - massbact) - rate;\n break;\n } else {\n arr[count++] = (num - massbact) / 2 - rate;\n rate = (num - massbact) / 2;\n massbact += rate;\n }\n }\n printf(\"%d\\n\", count);\n for (int i = 0; i < count; i++) {\n printf(\"%d \", arr[i]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let mut str = String::new();\n let _ = stdin().read_line(&mut str).unwrap();\n let test: u64 = str.trim().parse().unwrap();\n for _ in 0..test {\n str.clear();\n let _ = stdin().read_line(&mut str).unwrap();\n let mut n: i64 = str.trim().parse().unwrap();\n n -= 1;\n let mut iter = 1;\n let mut count = 0;\n let mut ans = String::new();\n loop {\n if n > 6 * iter {\n ans += &format!(\"{} \", iter);\n n -= 2 * iter;\n count += 1;\n iter *= 2;\n } else {\n if n <= 2 * iter {\n println!(\"{}\", count + 1);\n println!(\"{}{}\", ans, n - iter);\n } else if n <= 4 * iter {\n println!(\"{}\", count + 2);\n println!(\"{}{} {}\", ans, (n - 2 * iter) / 2, (n - 2 * iter) % 2);\n } else {\n println!(\"{}\", count + 2);\n println!(\"{}{} {}\", ans, iter, n - 4 * iter);\n }\n break;\n }\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1810", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N boxes arranged in a row.\nInitially, the i-th box from the left contains a_i candies.

\n

Snuke can perform the following operation any number of times:

\n
    \n
  • Choose a box containing at least one candy, and eat one of the candies in the chosen box.
  • \n
\n

His objective is as follows:

\n
    \n
  • Any two neighboring boxes contain at most x candies in total.
  • \n
\n

Find the minimum number of operations required to achieve the objective.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 ≤ N ≤ 10^5
  • \n
  • 0 ≤ a_i ≤ 10^9
  • \n
  • 0 ≤ x ≤ 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N x\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of operations required to achieve the objective.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 3\n2 2 2\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

Eat one candy in the second box.\nThen, the number of candies in each box becomes (2, 1, 2).

\n
\n
\n
\n
\n
\n

Sample Input 2

6 1\n1 6 1 2 0 4\n
\n
\n
\n
\n
\n

Sample Output 2

11\n
\n

For example, eat six candies in the second box, two in the fourth box, and three in the sixth box.\nThen, the number of candies in each box becomes (1, 0, 1, 0, 0, 1).

\n
\n
\n
\n
\n
\n

Sample Input 3

5 9\n3 1 4 1 5\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

The objective is already achieved without performing operations.

\n
\n
\n
\n
\n
\n

Sample Input 4

2 0\n5 5\n
\n
\n
\n
\n
\n

Sample Output 4

10\n
\n

All the candies need to be eaten.

\n
\n
", "c_code": "int solution(void) {\n int N = 0;\n long x = 0;\n long count = 0;\n long a[100002] = {0};\n scanf(\"%d %ld\", &N, &x);\n for (int i = 0; i < N; i++) {\n scanf(\"%ld\", &a[i]);\n }\n for (int i = 0; i < N - 1; i++) {\n if (i == 0 && a[i] > x) {\n count += a[i] - x;\n a[i] = x;\n }\n if (a[i] + a[i + 1] > x) {\n count += a[i] + a[i + 1] - x;\n a[i + 1] -= a[i] + a[i + 1] - x;\n }\n }\n printf(\"%ld\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let mut itr = s.split_whitespace().map(|e| e.parse().unwrap());\n let _n: i64 = itr.next().unwrap();\n let x: i64 = itr.next().unwrap();\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let mut a: Vec = s.split_whitespace().map(|e| e.parse().unwrap()).collect();\n let mut ans = 0;\n\n for e in &mut a {\n let t = min(*e, x);\n ans += *e - t;\n *e = t;\n }\n\n for i in 1..a.len() {\n let t = max(0, a[i] + a[i - 1] - x);\n ans += t;\n a[i - 1] = min(a[i] + a[i - 1] - t, a[i - 1]);\n a[i] = max(0, a[i] - t);\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1811", "problem_description": "Polycarp wrote on the board a string $$$s$$$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input.After that, he erased some letters from the string $$$s$$$, and he rewrote the remaining letters in any order. As a result, he got some new string $$$t$$$. You have to find it with some additional information.Suppose that the string $$$t$$$ has length $$$m$$$ and the characters are numbered from left to right from $$$1$$$ to $$$m$$$. You are given a sequence of $$$m$$$ integers: $$$b_1, b_2, \\ldots, b_m$$$, where $$$b_i$$$ is the sum of the distances $$$|i-j|$$$ from the index $$$i$$$ to all such indices $$$j$$$ that $$$t_j > t_i$$$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $$$b_i$$$, Polycarp finds all such indices $$$j$$$ that the index $$$j$$$ contains a letter that is later in the alphabet than $$$t_i$$$ and sums all the values $$$|i-j|$$$.For example, if $$$t$$$ = \"abzb\", then: since $$$t_1$$$='a', all other indices contain letters which are later in the alphabet, that is: $$$b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$$$; since $$$t_2$$$='b', only the index $$$j=3$$$ contains the letter, which is later in the alphabet, that is: $$$b_2=|2-3|=1$$$; since $$$t_3$$$='z', then there are no indexes $$$j$$$ such that $$$t_j>t_i$$$, thus $$$b_3=0$$$; since $$$t_4$$$='b', only the index $$$j=3$$$ contains the letter, which is later in the alphabet, that is: $$$b_4=|4-3|=1$$$. Thus, if $$$t$$$ = \"abzb\", then $$$b=[6,1,0,1]$$$.Given the string $$$s$$$ and the array $$$b$$$, find any possible string $$$t$$$ for which the following two requirements are fulfilled simultaneously: $$$t$$$ is obtained from $$$s$$$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $$$t$$$ according to the rules above, equals to the array $$$b$$$ specified in the input data.", "c_code": "int solution() {\n int q;\n scanf(\"%d\", &q);\n char s[55];\n int n;\n int i;\n int j;\n int m;\n int b[55];\n int cnt[30];\n char t[55];\n int v[55];\n int c;\n int cc;\n for (; q > 0; q--) {\n scanf(\"%s\", s);\n scanf(\"%d\", &m);\n for (i = 0; i < m; i++) {\n scanf(\"%d\", &b[i]);\n }\n n = 0;\n while (s[n] != '\\0') {\n n++;\n }\n for (i = 0; i < 30; i++) {\n cnt[i] = 0;\n }\n for (i = 0; i < n; i++) {\n cnt[s[i] - 'a']++;\n }\n for (i = 0; i < m; i++) {\n t[i] = 'a' - 1;\n }\n for (j = 29;;) {\n c = 0;\n for (i = 0; i < m; i++) {\n if (t[i] < 'a') {\n c++;\n }\n }\n if (c == 0) {\n break;\n }\n for (i = 0; i < m; i++) {\n v[i] = 0;\n }\n c = cc = 0;\n for (i = 0; i < m; i++) {\n v[i] += cc;\n if (t[i] >= 'a') {\n c++;\n }\n cc += c;\n }\n c = cc = 0;\n for (i = m - 1; i >= 0; i--) {\n v[i] += cc;\n if (t[i] >= 'a') {\n c++;\n }\n cc += c;\n }\n c = 0;\n for (i = 0; i < m; i++) {\n if (b[i] == v[i] && t[i] < 'a') {\n c++;\n }\n }\n while (cnt[j] < c) {\n j--;\n }\n for (i = 0; i < m; i++) {\n if (v[i] == b[i] && t[i] < 'a') {\n t[i] = 'a' + j;\n }\n }\n j--;\n }\n t[m] = '\\0';\n printf(\"%s\\n\", t);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let q: 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..q {\n let s: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().chars().collect()\n };\n let m: usize = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n buf.trim_end().parse().unwrap()\n };\n let mut b: 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 counts = std::collections::BTreeMap::new();\n for i in 0..s.len() {\n *counts.entry(s[i]).or_insert(0) += 1;\n }\n let mut iter = counts.iter().rev();\n\n let mut t = vec!['?'; m];\n loop {\n let mut pos = Vec::new();\n for i in 0..m {\n if b[i] == 0 {\n pos.push(i);\n }\n }\n if pos.is_empty() {\n break;\n }\n for i in 0..m {\n if b[i] == 0 {\n b[i] = std::usize::MAX;\n } else {\n for &j in &pos {\n b[i] -= j.abs_diff(i);\n }\n }\n }\n for (&c, &count) in iter.by_ref() {\n if count >= pos.len() {\n for &j in &pos {\n t[j] = c;\n }\n break;\n }\n }\n }\n\n println!(\"{}\", t.iter().collect::());\n }\n}", "difficulty": "hard"} {"problem_id": "1812", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

There are N positive integers written on a blackboard: A_1, ..., A_N.

\n

Snuke can perform the following operation when all integers on the blackboard are even:

\n
    \n
  • Replace each integer X on the blackboard by X divided by 2.
  • \n
\n

Find the maximum possible number of operations that Snuke can perform.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 200
  • \n
  • 1 \\leq A_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 ... A_N\n
\n
\n
\n
\n
\n

Output

Print the maximum possible number of operations that Snuke can perform.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n8 12 40\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

Initially, [8, 12, 40] are written on the blackboard.\nSince all those integers are even, Snuke can perform the operation.

\n

After the operation is performed once, [4, 6, 20] are written on the blackboard.\nSince all those integers are again even, he can perform the operation.

\n

After the operation is performed twice, [2, 3, 10] are written on the blackboard.\nNow, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

\n

Thus, Snuke can perform the operation at most twice.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n5 6 8 10\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

\n
\n
\n
\n
\n
\n

Sample Input 3

6\n382253568 723152896 37802240 379425024 404894720 471526144\n
\n
\n
\n
\n
\n

Sample Output 3

8\n
\n
\n
", "c_code": "int solution(void) {\n int data[200];\n int max = 0;\n int result = 0;\n int i = 0;\n int j = 0;\n scanf(\"%d\", &max);\n for (i = 0; i < max; i++) {\n scanf(\"%d\", &data[i]);\n }\n i = 0;\n for (; i == 0;) {\n for (j = 0; j < max; j++) {\n if (data[j] % 2 != 0) {\n i = 1;\n break;\n }\n data[j] /= 2;\n }\n result++;\n }\n printf(\"%d\", result - 1);\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).unwrap();\n let ns = buffer.trim().split(' ').collect::>();\n let xs = ns.into_iter().map(|n| {\n let mut counter = 0;\n let mut x = n.parse::().unwrap();\n while x % 2 == 0 {\n x /= 2;\n counter += 1;\n }\n counter\n });\n println!(\"{}\", xs.min().unwrap());\n}", "difficulty": "medium"} {"problem_id": "1813", "problem_description": "You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.", "c_code": "int solution() {\n int a = 0;\n int d = 0;\n int e = 0;\n scanf(\"%d\", &a);\n int b[a];\n int c[a];\n for (int i = 0; i < a; i++) {\n scanf(\"%d %d\", &b[i], &c[i]);\n }\n for (int i = 0; i < a; i++) {\n if (b[i] > 0) {\n d++;\n }\n if (b[i] < 0) {\n e++;\n }\n }\n if (d < 2 || e < 2) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n\n use std::io::prelude::*;\n std::io::stdin().read_to_string(&mut input).unwrap();\n let mut it = input.split_whitespace();\n\n let n: usize = it.next().unwrap().parse().unwrap();\n\n let mut cnt = (0, 0);\n\n for _ in 0..n {\n let (x, _y): (i32, i32) = (\n it.next().unwrap().parse().unwrap(),\n it.next().unwrap().parse().unwrap(),\n );\n if x > 0 {\n cnt.1 += 1;\n } else {\n cnt.0 += 1;\n }\n }\n\n let ans = cnt.0 <= 1 || cnt.1 <= 1;\n if ans {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n}", "difficulty": "medium"} {"problem_id": "1814", "problem_description": "The only difference between easy and hard versions is the maximum value of $$$n$$$.You are given a positive integer number $$$n$$$. You really love good numbers so you want to find the smallest good number greater than or equal to $$$n$$$.The positive integer is called good if it can be represented as a sum of distinct powers of $$$3$$$ (i.e. no duplicates of powers of $$$3$$$ are allowed).For example: $$$30$$$ is a good number: $$$30 = 3^3 + 3^1$$$, $$$1$$$ is a good number: $$$1 = 3^0$$$, $$$12$$$ is a good number: $$$12 = 3^2 + 3^1$$$, but $$$2$$$ is not a good number: you can't represent it as a sum of distinct powers of $$$3$$$ ($$$2 = 3^0 + 3^0$$$), $$$19$$$ is not a good number: you can't represent it as a sum of distinct powers of $$$3$$$ (for example, the representations $$$19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0$$$ are invalid), $$$20$$$ is also not a good number: you can't represent it as a sum of distinct powers of $$$3$$$ (for example, the representation $$$20 = 3^2 + 3^2 + 3^0 + 3^0$$$ is invalid). Note, that there exist other representations of $$$19$$$ and $$$20$$$ as sums of powers of $$$3$$$ but none of them consists of distinct powers of $$$3$$$.For the given positive integer $$$n$$$ find such smallest $$$m$$$ ($$$n \\le m$$$) that $$$m$$$ is a good number.You have to answer $$$q$$$ independent queries.", "c_code": "int solution() {\n int q;\n scanf(\"%d\", &q);\n while (q--) {\n int n;\n scanf(\"%d\", &n);\n\n int i = n;\n while (1) {\n n = i;\n int res = 0;\n while (n > 0) {\n if (n % 3 == 0) {\n n = n / 3;\n } else {\n n--;\n if (n % 3 == 0) {\n n = n / 3;\n } else {\n res = 1;\n break;\n }\n }\n }\n if (res == 0) {\n break;\n }\n i++;\n }\n printf(\"%d\\n\", i);\n }\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let q: u16 = buffer.trim().parse().expect(\"invalid input\");\n\n for _ in 0..q {\n let mut input = String::new();\n io::stdin()\n .read_line(&mut input)\n .expect(\"failed to read input\");\n let a: u64 = input.trim().parse().unwrap();\n let mut p3: Vec = Vec::with_capacity(39);\n let mut a_mut = a;\n while a_mut != 0 {\n p3.push((a_mut % 3) as u8);\n a_mut /= 3;\n }\n p3.push(0);\n\n match p3.iter().rposition(|&x| x == 2) {\n Some(x) => {\n for i in 0..=x {\n p3[i] = 0;\n }\n\n let mut pos = x + 1;\n while p3[pos] != 0 {\n p3[pos] = 0;\n pos += 1;\n }\n p3[pos] = 1;\n let mut total: u64 = 0;\n for i in p3[pos..].iter().rev() {\n total = 3 * total + *i as u64;\n }\n total *= 3u64.pow(pos as u32);\n\n println!(\"{}\", total);\n }\n None => {\n println!(\"{}\", a);\n }\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1815", "problem_description": "Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.", "c_code": "int solution() {\n int n;\n int m;\n int p[100002];\n int l[100002];\n int r[100002];\n int x[100002];\n scanf(\"%d%d\", &n, &m);\n int i;\n int j;\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &p[i]);\n }\n for (i = 0; i < m; i++) {\n scanf(\"%d%d%d\", &l[i], &r[i], &x[i]);\n int count = 0;\n for (j = l[i] - 1; j < r[i]; j++) {\n if (p[j] < p[x[i] - 1]) {\n count++;\n }\n }\n if (l[i] + count == x[i]) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let (_n, m) = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let mut it = input\n .split_whitespace()\n .map(|k| k.parse::().unwrap());\n (it.next().unwrap(), it.next().unwrap())\n };\n\n let p: Vec = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n input\n .split_whitespace()\n .map(|k| k.parse().unwrap())\n .collect()\n };\n\n for _ in 0..m {\n let (l, r, x) = {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let mut it = input\n .split_whitespace()\n .map(|k| k.parse::().unwrap());\n (\n it.next().unwrap() - 1,\n it.next().unwrap(),\n it.next().unwrap() - 1,\n )\n };\n\n let pos = l + p[l..r].iter().filter(|&&k| k < p[x]).count();\n if x == pos {\n println!(\"Yes\");\n } else {\n println!(\"No\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1816", "problem_description": "You are given a sequence $$$a=[a_1,a_2,\\dots,a_n]$$$ consisting of $$$n$$$ positive integers.Let's call a group of consecutive elements a segment. Each segment is characterized by two indices: the index of its left end and the index of its right end. Denote by $$$a[l,r]$$$ a segment of the sequence $$$a$$$ with the left end in $$$l$$$ and the right end in $$$r$$$, i.e. $$$a[l,r]=[a_l, a_{l+1}, \\dots, a_r]$$$.For example, if $$$a=[31,4,15,92,6,5]$$$, then $$$a[2,5]=[4,15,92,6]$$$, $$$a[5,5]=[6]$$$, $$$a[1,6]=[31,4,15,92,6,5]$$$ are segments.We split the given sequence $$$a$$$ into segments so that: each element is in exactly one segment; the sums of elements for all segments are equal. For example, if $$$a$$$ = [$$$55,45,30,30,40,100$$$], then such a sequence can be split into three segments: $$$a[1,2]=[55,45]$$$, $$$a[3,5]=[30, 30, 40]$$$, $$$a[6,6]=[100]$$$. Each element belongs to exactly segment, the sum of the elements of each segment is $$$100$$$.Let's define thickness of split as the length of the longest segment. For example, the thickness of the split from the example above is $$$3$$$.Find the minimum thickness among all possible splits of the given sequence of $$$a$$$ into segments in the required way.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n scanf(\"%d\", &n);\n int seq[n];\n int seq_sum[n];\n scanf(\"%d\", &seq[0]);\n seq_sum[0] = seq[0];\n for (int j = 1; j < n; j++) {\n scanf(\"%d\", &seq[j]);\n seq_sum[j] = seq_sum[j - 1] + seq[j];\n }\n int min_len = INT32_MAX;\n for (int first_end = 0; first_end < n; first_end++) {\n int each_sum = seq_sum[first_end];\n int max_len = first_end + 1;\n if (seq_sum[n - 1] % each_sum != 0) {\n continue;\n }\n int ptr = first_end;\n int ptr_prev = ptr;\n for (int j = 2; j < seq_sum[n - 1] / each_sum; j++) {\n while (ptr < n && seq_sum[ptr] % (each_sum * j)) {\n ptr++;\n }\n int distance = ptr - ptr_prev;\n max_len = max_len > distance ? max_len : distance;\n ptr_prev = ptr;\n }\n if (ptr >= n) {\n continue;\n }\n int distance = n - 1 - ptr;\n max_len = max_len > distance ? max_len : distance;\n min_len = max_len > min_len ? min_len : max_len;\n }\n printf(\"%d\\n\", min_len);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut tests = String::new();\n io::stdin().read_line(&mut tests).unwrap();\n let tests = tests.trim().parse().unwrap();\n\n for _ in 0..tests {\n let mut testline = String::new();\n io::stdin().read_line(&mut testline).unwrap();\n testline = String::new();\n io::stdin().read_line(&mut testline).unwrap();\n let numbers: Vec = testline\n .split_whitespace()\n .map(|s| s.trim().parse().unwrap())\n .collect();\n let mut minimal_thickness = numbers.len();\n let mut first_thickness = 0;\n let mut part_sum = 0;\n while part_sum + numbers[first_thickness] < *numbers.iter().max().unwrap() {\n part_sum += numbers[first_thickness];\n first_thickness += 1;\n }\n\n while first_thickness < numbers.len() && minimal_thickness > first_thickness {\n part_sum += numbers[first_thickness];\n first_thickness += 1;\n let mut idx = first_thickness;\n let mut thickness = first_thickness;\n while idx < numbers.len() {\n let mut part = 0;\n let mut part_length = 0;\n while part < part_sum && idx < numbers.len() {\n part += numbers[idx];\n idx += 1;\n part_length += 1;\n }\n if part != part_sum {\n thickness = numbers.len();\n break;\n } else {\n thickness = cmp::max(thickness, part_length);\n }\n }\n if idx == numbers.len() {\n minimal_thickness = cmp::min(thickness, minimal_thickness);\n }\n }\n println!(\"{minimal_thickness}\")\n }\n}", "difficulty": "hard"} {"problem_id": "1817", "problem_description": "A new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — $$$n$$$ and $$$k$$$, where $$$n$$$ is even. Next, he takes all the integers from $$$1$$$ to $$$n$$$, and splits them all into pairs $$$(a, b)$$$ (each integer must be in exactly one pair) so that for each pair the integer $$$(a + k) \\cdot b$$$ is divisible by $$$4$$$ (note that the order of the numbers in the pair matters), or reports that, unfortunately for viewers, such a split is impossible.Burenka really likes such performances, so she asked her friend Tonya to be a magician, and also gave him the numbers $$$n$$$ and $$$k$$$.Tonya is a wolf, and as you know, wolves do not perform in the circus, even in a mathematical one. Therefore, he asks you to help him. Let him know if a suitable splitting into pairs is possible, and if possible, then tell it.", "c_code": "int solution() {\n int numSets = 0;\n\n scanf(\"%d\", &numSets);\n\n for (int current_set = 0; current_set < numSets; ++current_set) {\n\n int n = 0;\n int k = 0;\n scanf(\"%d %d\", &n, &k);\n if (n % 2 == 1) {\n printf(\"NO\\n\");\n }\n int k_remnant = k % 4;\n switch (k_remnant) {\n case 0: {\n printf(\"NO\\n\");\n break;\n }\n case 2: {\n printf(\"YES\\n\");\n\n int main_cycle_elements = n & (~3);\n for (int i = 0; i < main_cycle_elements; i += 4) {\n printf(\"%d %d\\n\", i + 2, i + 1);\n printf(\"%d %d\\n\", i + 3, i + 4);\n }\n if (n > main_cycle_elements) {\n printf(\"%d %d\\n\", n, n - 1);\n }\n break;\n }\n default: {\n printf(\"YES\\n\");\n\n for (int i = 0; i < n; i += 2) {\n printf(\"%d %d\\n\", i + 1, i + 2);\n }\n break;\n }\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 n = strings.first().unwrap().parse::().unwrap();\n let k = strings.get(1).unwrap().parse::().unwrap();\n if k % 4 == 0 {\n println!(\"NO\");\n } else if k % 2 == 1 {\n println!(\"YES\");\n let mut x = 1;\n while x < n {\n println!(\"{} {}\", x, x + 1);\n x += 2;\n }\n } else {\n println!(\"YES\");\n let mut x = 1;\n while x < n {\n if (x + k) * (x + 1) % 4 == 0 {\n println!(\"{} {}\", x, x + 1);\n } else {\n println!(\"{} {}\", x + 1, x);\n }\n x += 2;\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1818", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

Snuke prepared 6 problems for a upcoming programming contest.\nFor each of those problems, Rng judged whether it can be used in the contest or not.

\n

You are given a string S of length 6.\nIf the i-th character of s is 1, it means that the i-th problem prepared by Snuke is accepted to be used; 0 means that the problem is not accepted.

\n

How many problems prepared by Snuke are accepted to be used in the contest?

\n
\n
\n
\n
\n

Constraints

    \n
  • The length of S is 6.
  • \n
  • S consists of 0 and 1.
  • \n
\n
\n
\n
\n
\n
\n
\n

Inputs

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Outputs

Print the number of problems prepared by Snuke that are accepted to be used in the contest.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

111100\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

The first, second, third and fourth problems are accepted, for a total of four.

\n
\n
\n
\n
\n
\n

Sample Input 2

001001\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n
\n
\n
\n
\n
\n

Sample Input 3

000000\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n char s[7];\n scanf(\"%s\", s);\n printf(\"%d\", s[0] + s[1] + s[2] + s[3] + s[4] + s[5] - (48 * 6));\n return 0;\n}", "rust_code": "fn solution() {\n println!(\n \"{}\",\n io::stdin()\n .bytes()\n .take(6)\n .filter(|r| r.as_ref().map(|b| b'1' == *b).unwrap_or(false))\n .count()\n );\n}", "difficulty": "hard"} {"problem_id": "1819", "problem_description": "Mikhail walks on a Cartesian plane. He starts at the point $$$(0, 0)$$$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $$$(0, 0)$$$, he can go to any of the following points in one move: $$$(1, 0)$$$; $$$(1, 1)$$$; $$$(0, 1)$$$; $$$(-1, 1)$$$; $$$(-1, 0)$$$; $$$(-1, -1)$$$; $$$(0, -1)$$$; $$$(1, -1)$$$. If Mikhail goes from the point $$$(x1, y1)$$$ to the point $$$(x2, y2)$$$ in one move, and $$$x1 \\ne x2$$$ and $$$y1 \\ne y2$$$, then such a move is called a diagonal move.Mikhail has $$$q$$$ queries. For the $$$i$$$-th query Mikhail's target is to go to the point $$$(n_i, m_i)$$$ from the point $$$(0, 0)$$$ in exactly $$$k_i$$$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $$$(0, 0)$$$ to the point $$$(n_i, m_i)$$$ in $$$k_i$$$ moves.Note that Mikhail can visit any point any number of times (even the destination point!).", "c_code": "int solution() {\n int q;\n scanf(\"%d\", &q);\n while (q--) {\n long long int a[3];\n int i;\n for (i = 0; i < 3; i++) {\n scanf(\"%lld\", &a[i]);\n }\n long long int bigger;\n long long int smaller;\n long long int ans = 0;\n if (a[0] > a[1]) {\n bigger = a[0];\n smaller = a[1];\n } else {\n bigger = a[1];\n smaller = a[0];\n }\n\n if (bigger > a[2]) {\n printf(\"-1\\n\");\n } else {\n long long int rem = a[2];\n if ((bigger % 2 == 0 && smaller % 2 == 0) ||\n (bigger % 2 == 1 && smaller % 2 == 1)) {\n ans = ans + bigger - 1;\n rem = rem - (bigger - 1);\n if ((rem) % 2 == 1) {\n ans += rem;\n } else {\n ans += rem - 2;\n }\n } else {\n ans = ans + rem - 1;\n }\n printf(\"%lld\\n\", ans);\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\n .split_whitespace()\n .map(|x| x.parse::().unwrap())\n .collect();\n\n let mut ind = 0;\n let q = arr[ind];\n ind += 1;\n\n for _i in 0..q {\n let x2 = arr[ind];\n ind += 1;\n let y2 = arr[ind];\n ind += 1;\n let k = arr[ind];\n ind += 1;\n\n let _ans = 0;\n\n let dx = x2.abs();\n let dy = y2.abs();\n use std::cmp;\n let mut num_diagonals = cmp::min(dx, dy);\n let mut num_straight = dx - num_diagonals + dy - num_diagonals;\n\n num_diagonals += (num_straight / 2) * 2;\n num_straight %= 2;\n\n let moves = num_diagonals + num_straight;\n\n if moves > k {\n println!(\"-1\");\n } else {\n let mut moves_left = k - moves;\n if moves_left == 0 {\n println!(\"{}\", num_diagonals);\n } else {\n if num_straight > 0 && moves_left >= 3 {\n let rep_diag = moves_left / 3;\n num_diagonals += rep_diag * 3;\n moves_left %= 3;\n }\n\n if moves_left >= 2 {\n let rep_diag = moves_left / 2;\n num_diagonals += rep_diag * 2;\n moves_left %= 2;\n }\n\n if moves_left > 0 {\n if num_straight > 0 {\n num_diagonals += 1;\n } else {\n num_diagonals -= 1;\n }\n }\n println!(\"{}\", num_diagonals);\n }\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1820", "problem_description": "

Structured Programming

\n\n

\nIn programming languages like C/C++, a goto statement provides an unconditional jump from the \"goto\" to a labeled statement. For example, a statement \"goto CHECK_NUM;\" is executed, control of the program jumps to CHECK_NUM. Using these constructs, you can implement, for example, loops.\n

\n\n

\nNote that use of goto statement is highly discouraged, because it is difficult to trace the control flow of a program which includes goto.\n

\n\n

\nWrite a program which does precisely the same thing as the following program (this example is wrtten in C++). Let's try to write the program without goto statements.\n

\n\n
\nvoid call(int n){\n  int i = 1;\n CHECK_NUM:\n  int x = i;\n  if ( x % 3 == 0 ){\n    cout << \" \" << i;\n    goto END_CHECK_NUM;\n  }\n INCLUDE3:\n  if ( x % 10 == 3 ){\n    cout << \" \" << i;\n    goto END_CHECK_NUM;\n  }\n  x /= 10;\n  if ( x ) goto INCLUDE3;\n END_CHECK_NUM:\n  if ( ++i <= n ) goto CHECK_NUM;\n\n  cout << endl;\n}\n
\n\n\n

Input

\n\n

\nAn integer n is given in a line.\n

\n\n

Output

\n\n

\nPrint the output result of the above program for given integer n.\n

\n\n

Constraints

\n
    \n
  • 3 ≤ n ≤ 10000
  • \n
\n\n\n

Sample Input

\n\n
\n30\n
\n\n

Sample Output

\n\n
\n 3 6 9 12 13 15 18 21 23 24 27 30\n
\n\n

\n Put a single space character before each element.\n

", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n for (int i = 3; i <= n; i++) {\n if (i % 3 == 0) {\n printf(\" %d\", i);\n continue;\n }\n for (int j = 1; j <= n * 10; j *= 10) {\n if ((i / j) % 10 == 3) {\n printf(\" %d\", i);\n break;\n }\n }\n }\n printf(\"\\n\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).ok();\n let n = s.trim().parse::().ok().unwrap();\n\n for i in 1..n + 1 {\n if i % 3 == 0 || i.to_string().contains(\"3\") {\n print!(\" {}\", i);\n }\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "1821", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

\n

Takahashi loves palindromes. Non-palindromic strings are unacceptable to him. Each time he hugs a string, he can change one of its characters to any character of his choice.

\n

Given is a string S. Find the minimum number of hugs needed to make S palindromic.

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • S is a string consisting of lowercase English letters.
  • \n
  • The length of S is between 1 and 100 (inclusive).
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
S\n
\n
\n
\n
\n
\n

Output

\n

Print the minimum number of hugs needed to make S palindromic.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

redcoder\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

For example, we can change the fourth character to o and get a palindrome redooder.

\n
\n
\n
\n
\n
\n

Sample Input 2

vvvvvv\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

We might need no hugs at all.

\n
\n
\n
\n
\n
\n

Sample Input 3

abcdabc\n
\n
\n
\n
\n
\n

Sample Output 3

2\n
\n
\n
", "c_code": "int solution() {\n char a[100];\n int i = 0;\n int count = 0;\n int change = 0;\n scanf(\"%s\", &a[0]);\n for (count = 0; a[count] != '\\0'; count++) {\n ;\n }\n for (i = 0; i < count / 2; i++) {\n if (a[i] != a[count - i - 1]) {\n change++;\n }\n }\n printf(\"%d\\n\", change);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n\n io::stdin().read_line(&mut s).unwrap();\n\n let s = s.trim();\n\n let n = s.len();\n let s: Vec = s.chars().collect();\n\n let mut answer = 0;\n\n for i in 0..n {\n if s[i] != s[n - i - 1] {\n answer += 1;\n }\n }\n\n println!(\"{}\", answer / 2);\n}", "difficulty": "easy"} {"problem_id": "1822", "problem_description": "

階乗 II

\n\n\n

\nn! = n × (n − 1) × (n − 2) × ... × 3 × 2 × 1
\n
\nを n の階乗といいます。例えば、12 の階乗は
\n
\n12! = 12 × 11 × 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 = 479001600
\n
\nとなり、末尾に 0 が 2 つ連続して並んでいます。\n

\n\n

\n整数 n を入力して、n! の末尾に連続して並んでいる 0 の数を出力するプログラムを作成してください。ただし、n は 20000 以下の正の整数とします。\n

\n\n

Input

\n\n

\n複数のデータが与えられます。各データに n (n ≤ 20000) が1行に与えられます。n が 0 の時入力の最後とします。\n

\n\n

\nデータの数は 20 を超えません。\n

\n\n

Output

\n\n

\n各データに対して n! の末尾に連続して並んでいる 0 の数を1行に出力して下さい。\n

\n\n

Sample Input

\n\n
\n2\n12\n10000\n0\n
\n\n

Output for the Sample Input

\n\n
\n0\n2\n2499\n
", "c_code": "int solution() {\n while (1) {\n int n;\n int c = 0;\n int k = 5;\n scanf(\"%d\", &n);\n if (n == 0) {\n break;\n }\n while (k < 20000) {\n c += n / (k);\n k *= 5;\n }\n\n printf(\"%d\\n\", c);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut input;\n loop {\n input = String::new();\n std::io::stdin().read_line(&mut input).expect(\"Input error\");\n let mut n = input.trim().parse::().expect(\"Error parse\");\n if n == 0 {\n break;\n }\n let mut zero = 0;\n while n >= 5 {\n zero += n / 5;\n n /= 5;\n }\n println!(\"{}\", zero);\n }\n}", "difficulty": "easy"} {"problem_id": "1823", "problem_description": "A chess tournament will be held soon, where $$$n$$$ chess players will take part. Every participant will play one game against every other participant. Each game ends in either a win for one player and a loss for another player, or a draw for both players.Each of the players has their own expectations about the tournament, they can be one of two types: a player wants not to lose any game (i. e. finish the tournament with zero losses); a player wants to win at least one game. You have to determine if there exists an outcome for all the matches such that all the players meet their expectations. If there are several possible outcomes, print any of them. If there are none, report that it's impossible.", "c_code": "int solution(void) {\n int t;\n scanf(\"%d\", &t);\n for (int k = 0; k < t; k++) {\n int n;\n scanf(\"%d\", &n);\n char s[n];\n scanf(\"%s\", s);\n char m[n][n];\n int c = 0;\n int d = 0;\n for (int j = 0; j < n; j++) {\n if (s[j] == '1') {\n c++;\n } else {\n d++;\n }\n }\n int r = 0;\n int e = 0;\n for (int y = 0; y < n; y++) {\n if (s[y] == '1') {\n r++;\n } else {\n e++;\n }\n }\n int q = (n - r) * (n - r - 1) / 2;\n if (q >= e) {\n printf(\"YES\\n\");\n for (int i = 0; i < n; i++) {\n int pl = 0;\n for (int j = 0; j < n; j++) {\n\n if (i == j) {\n m[i][j] = 'X';\n printf(\"X\");\n } else if (s[i] == '1' && s[j] == '2') {\n m[i][j] = '+';\n printf(\"+\");\n } else if (s[i] == '1' && s[j] == '1') {\n m[i][j] = '=';\n printf(\"=\");\n } else if (s[i] == '2' && s[j] == '1') {\n m[i][j] = '-';\n printf(\"-\");\n } else if (s[i] == '2' && s[j] == '2' && pl == 0) {\n if (j < i && m[j][i] == '+') {\n printf(\"-\");\n m[i][j] = '-';\n } else {\n m[i][j] = '+';\n printf(\"+\");\n pl = 1;\n }\n\n } else if (s[i] == '2' && s[j] == '2' && pl == 1) {\n if (j < i && m[j][i] == '-') {\n printf(\"+\");\n m[i][j] = '+';\n } else {\n printf(\"-\");\n m[i][j] = '-';\n }\n }\n }\n printf(\"\\n\");\n }\n } else {\n printf(\"NO\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(2).step_by(2) {\n let v = l\n .unwrap()\n .chars()\n .map(|c| c.to_digit(10).unwrap())\n .collect::>();\n let t = v.iter().filter(|&c| *c == 2).count();\n if matches!(t, 1 | 2) {\n writeln!(o, \"NO\").ok();\n continue;\n }\n let mut r = vec![vec!['X'; v.len()]; v.len()];\n let s = ['+', '-'];\n for j in 0..v.len() {\n let mut w = 0;\n for k in j..v.len() {\n if j == k {\n continue;\n }\n let (a, b) = match (v[j], v[k]) {\n (1, _) | (_, 1) => ('=', '='),\n _ => {\n w ^= 1;\n (s[w], s[w ^ 1])\n }\n };\n r[j][k] = a;\n r[k][j] = b;\n }\n }\n let s: String = r\n .iter()\n .map(|v| v.iter().collect::() + \"\\n\")\n .collect();\n writeln!(o, \"YES\\n{}\", s.trim()).ok();\n }\n}", "difficulty": "medium"} {"problem_id": "1824", "problem_description": "You are given an array $$$a$$$ of length $$$n$$$.Let's define the eversion operation. Let $$$x = a_n$$$. Then array $$$a$$$ is partitioned into two parts: left and right. The left part contains the elements of $$$a$$$ that are not greater than $$$x$$$ ($$$\\le x$$$). The right part contains the elements of $$$a$$$ that are strictly greater than $$$x$$$ ($$$> x$$$). The order of elements in each part is kept the same as before the operation, i. e. the partition is stable. Then the array is replaced with the concatenation of the left and the right parts.For example, if the array $$$a$$$ is $$$[2, 4, 1, 5, 3]$$$, the eversion goes like this: $$$[2, 4, 1, 5, 3] \\to [2, 1, 3], [4, 5] \\to [2, 1, 3, 4, 5]$$$.We start with the array $$$a$$$ and perform eversions on this array. We can prove that after several eversions the array $$$a$$$ stops changing. Output the minimum number $$$k$$$ such that the array stops changing after $$$k$$$ eversions.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n int max = -1;\n int end = 0;\n int count = 0;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] >= max) {\n max = a[i];\n end = i;\n }\n }\n int prev = -1;\n for (int i = n - 1; i > end; i--) {\n if (a[i] > prev) {\n count++;\n prev = a[i];\n }\n }\n printf(\"%d\\n\", count);\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).unwrap();\n\n for _ in 0..t.trim().parse::().unwrap() {\n let mut n = String::new();\n io::stdin().read_line(&mut n).unwrap();\n\n let mut a = String::new();\n io::stdin().read_line(&mut a).unwrap();\n let mut array = a\n .trim()\n .split(\" \")\n .map(|x| x.parse::().unwrap())\n .collect::>();\n array.reverse();\n\n let max = *array.iter().max().unwrap();\n let mut counter = 0;\n let mut element = array[0];\n\n for value in array {\n if value > element {\n element = value;\n counter += 1;\n }\n\n if value == max {\n break;\n }\n }\n\n println!(\"{}\", counter);\n }\n}", "difficulty": "easy"} {"problem_id": "1825", "problem_description": "Polycarp found a rectangular table consisting of $$$n$$$ rows and $$$m$$$ columns. He noticed that each cell of the table has its number, obtained by the following algorithm \"by columns\": cells are numbered starting from one; cells are numbered from left to right by columns, and inside each column from top to bottom; number of each cell is an integer one greater than in the previous cell. For example, if $$$n = 3$$$ and $$$m = 5$$$, the table will be numbered as follows:$$$$$$ \\begin{matrix} 1 & 4 & 7 & 10 & 13 \\\\ 2 & 5 & 8 & 11 & 14 \\\\ 3 & 6 & 9 & 12 & 15 \\\\ \\end{matrix} $$$$$$However, Polycarp considers such numbering inconvenient. He likes the numbering \"by rows\": cells are numbered starting from one; cells are numbered from top to bottom by rows, and inside each row from left to right; number of each cell is an integer one greater than the number of the previous cell. For example, if $$$n = 3$$$ and $$$m = 5$$$, then Polycarp likes the following table numbering: $$$$$$ \\begin{matrix} 1 & 2 & 3 & 4 & 5 \\\\ 6 & 7 & 8 & 9 & 10 \\\\ 11 & 12 & 13 & 14 & 15 \\\\ \\end{matrix} $$$$$$Polycarp doesn't have much time, so he asks you to find out what would be the cell number in the numbering \"by rows\", if in the numbering \"by columns\" the cell has the number $$$x$$$?", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n long long n[t];\n long long m[t];\n long long x[t];\n long long c;\n long long r;\n for (int i = 0; i < t; ++i) {\n scanf(\"%lld %lld %lld\", &n[i], &m[i], &x[i]);\n c = x[i] / n[i];\n r = x[i] % n[i];\n if (r == 0) {\n r = n[i];\n } else {\n ++c;\n }\n x[i] = (r - 1) * m[i] + c;\n }\n for (int i = 0; i < t; ++i) {\n printf(\"%lld\\n\", x[i]);\n }\n\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let mut input = String::new();\n let stdin = io::stdin();\n let mut stdin = stdin.lock();\n stdin.read_line(&mut input)?;\n let stdout = io::stdout();\n let mut stdout = BufWriter::new(stdout.lock());\n\n let case_count: usize = input.trim_end().parse()?;\n input.clear();\n for _ in 0..case_count {\n stdin.read_line(&mut input)?;\n let mut vals = input.trim_end().split_ascii_whitespace();\n\n let n: i64 = vals.next().unwrap().parse()?;\n let m: i64 = vals.next().unwrap().parse()?;\n let x: i64 = vals.next().unwrap().parse()?;\n input.clear();\n\n let col = (x - 1) / n;\n let row = (x - 1) % n;\n\n writeln!(&mut stdout, \"{}\", (row) * m + (col + 1))?;\n }\n\n Ok(())\n}", "difficulty": "medium"} {"problem_id": "1826", "problem_description": "\n

Score: 400 points

\n
\n
\n

Problem Statement

\n

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

\n

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

\n
    \n
  • A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.
  • \n
\n

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

\n
    \n
  • Buy stock: Pay A_i yen and receive one stock.
  • \n
  • Sell stock: Sell one stock for A_i yen.
  • \n
\n

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 2 \\leq N \\leq 80
  • \n
  • 100 \\leq A_i \\leq 200
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
N\nA_1 A_2 \\cdots A_N\n
\n
\n
\n
\n
\n

Output

\n

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

7\n100 130 130 130 115 115 150\n
\n
\n
\n
\n
\n

Sample Output 1

1685\n
\n

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

\n
    \n
  • Initially, he has 1000 yen and no stocks.
  • \n
  • Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.
  • \n
  • Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.
  • \n
  • Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.
  • \n
  • Day 4: Do nothing.
  • \n
  • Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.
  • \n
  • Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.
  • \n
  • Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.
  • \n
\n

There is no way to have 1686 yen or more in the end, so the answer is 1685.

\n
\n
\n
\n
\n
\n

Sample Input 2

6\n200 180 160 140 120 100\n
\n
\n
\n
\n
\n

Sample Output 2

1000\n
\n

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

\n
\n
\n
\n
\n
\n

Sample Input 3

2\n157 193\n
\n
\n
\n
\n
\n

Sample Output 3

1216\n
\n

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.

\n
\n
", "c_code": "int solution(void) {\n\n long n;\n scanf(\"%ld\", &n);\n long a[n];\n for (long i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n }\n long stock = 0;\n long money = 1000;\n int flag = 0;\n for (long i = 0; i < n; i++) {\n if (flag == 0) {\n if (i != n - 1 && a[i] < a[i + 1]) {\n stock += money / a[i];\n money %= a[i];\n flag = 1;\n }\n } else {\n if (i == n - 1 || (i != n - 1 && a[i] > a[i + 1])) {\n money += stock * a[i];\n stock = 0;\n flag = 0;\n }\n }\n }\n printf(\"%ld\\n\", money);\n\n return 0;\n}", "rust_code": "fn solution() {\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 let a: 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 money = 1000;\n let mut stock = 0;\n let mut s = 'n';\n for i in 1..n {\n if a[i - 1] < a[i] {\n if s != 'u' {\n stock += money / a[i - 1];\n money %= a[i - 1];\n }\n s = 'u';\n } else if a[i - 1] > a[i] {\n if s != 'd' {\n money += stock * a[i - 1];\n stock = 0;\n }\n s = 'd';\n }\n }\n money += stock * a[n - 1];\n println!(\"{}\", money);\n}", "difficulty": "hard"} {"problem_id": "1827", "problem_description": "Polycarp has a poor memory. Each day he can remember no more than $$$3$$$ of different letters. Polycarp wants to write a non-empty string of $$$s$$$ consisting of lowercase Latin letters, taking minimum number of days. In how many days will he be able to do it?Polycarp initially has an empty string and can only add characters to the end of that string.For example, if Polycarp wants to write the string lollipops, he will do it in $$$2$$$ days: on the first day Polycarp will memorize the letters l, o, i and write lolli; On the second day Polycarp will remember the letters p, o, s, add pops to the resulting line and get the line lollipops. If Polycarp wants to write the string stringology, he will do it in $$$4$$$ days: in the first day will be written part str; on day two will be written part ing; on the third day, part of olog will be written; on the fourth day, part of y will be written. For a given string $$$s$$$, print the minimum number of days it will take Polycarp to write it.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int d = 0;\n int l;\n char arr[2000001];\n char a = '1';\n char b = '1';\n char c = '1';\n scanf(\"%s\", arr);\n l = strlen(arr);\n for (int i = 0; i < l; i++) {\n if (a == '1' && b == '1' && c == '1') {\n a = arr[i];\n } else if (a != '1' && b == '1' && c == '1' && arr[i] != a) {\n b = arr[i];\n } else if (a != '1' && b != '1' && c == '1' && arr[i] != a &&\n arr[i] != b) {\n c = arr[i];\n }\n if (a != '1' && b != '1' && c != '1' && arr[i] != a && arr[i] != b &&\n arr[i] != c) {\n d++;\n a = '1';\n b = '1';\n c = '1';\n i--;\n }\n }\n if (a != '1' || b != '1' || c != '1') {\n d++;\n }\n printf(\"%d\\n\", d);\n }\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).expect(\"Error!\");\n let x: i32 = line.trim().parse().expect(\"Error!\");\n\n for _ in 0..x {\n let mut stroke = String::new();\n io::stdin().read_line(&mut stroke).expect(\"Error!\");\n\n let mut res = 1;\n let mut daily_let: Vec = Vec::new();\n for ch in stroke.chars() {\n if ch == '\\n' || ch == '\\r' {\n break;\n }\n if daily_let.len() >= 3 && !daily_let.contains(&ch) {\n daily_let.clear();\n daily_let.push(ch);\n res += 1;\n }\n if !daily_let.contains(&ch) {\n daily_let.push(ch);\n }\n }\n println!(\"{}\", res);\n }\n}", "difficulty": "hard"} {"problem_id": "1828", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

ButCoder Inc. runs a programming competition site called ButCoder. In this site, a user is given an integer value called rating that represents his/her skill, which changes each time he/she participates in a contest. The initial value of a new user's rating is 0, and a user whose rating reaches K or higher is called Kaiden (\"total transmission\"). Note that a user's rating may become negative.

\n

Hikuhashi is a new user in ButCoder. It is estimated that, his rating increases by A in each of his odd-numbered contests (first, third, fifth, ...), and decreases by B in each of his even-numbered contests (second, fourth, sixth, ...).

\n

According to this estimate, after how many contests will he become Kaiden for the first time, or will he never become Kaiden?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ K, A, B ≤ 10^{18}
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
K A B\n
\n
\n
\n
\n
\n

Output

If it is estimated that Hikuhashi will never become Kaiden, print -1. Otherwise, print the estimated number of contests before he become Kaiden for the first time.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4000 2000 500\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

Each time Hikuhashi participates in a contest, his rating is estimated to change as 020001500350030005000 After his fifth contest, his rating will reach 4000 or higher for the first time.

\n
\n
\n
\n
\n
\n

Sample Input 2

4000 500 2000\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

Each time Hikuhashi participates in a contest, his rating is estimated to change as 0500-1500-1000-3000-2500 He will never become Kaiden.

\n
\n
\n
\n
\n
\n

Sample Input 3

1000000000000000000 2 1\n
\n
\n
\n
\n
\n

Sample Output 3

1999999999999999997\n
\n

The input and output values may not fit into 32-bit integers.

\n
\n
", "c_code": "int solution() {\n long long a;\n long long b;\n long long c;\n scanf(\"%lld %lld %lld\", &a, &b, &c);\n if (a <= b) {\n printf(\"1\\n\");\n } else if (b <= c) {\n printf(\"-1\\n\");\n } else {\n printf(\"%lld\\n\", ((a - b - 1) / (b - c) * 2) + 3);\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 k: usize = itr.next().unwrap().parse().unwrap();\n let a: usize = itr.next().unwrap().parse().unwrap();\n let b: usize = itr.next().unwrap().parse().unwrap();\n if k <= a {\n println!(\"1\");\n } else if a <= b {\n println!(\"-1\");\n } else {\n println!(\"{}\", (k - a).div_ceil(a - b) * 2 + 1);\n }\n}", "difficulty": "medium"} {"problem_id": "1829", "problem_description": "There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.", "c_code": "int solution() {\n int beauty[1005];\n int i = 0;\n for (; i < 1002; i++) {\n beauty[i] = 0;\n }\n int n;\n int a;\n scanf(\"%d\", &n);\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &a);\n beauty[a]++;\n }\n int max = 0;\n for (i = 0; i < 1002; i++) {\n if (beauty[i] > max) {\n max = beauty[i];\n }\n }\n printf(\"%d\", n - max);\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n\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 let mut ns: Vec = buf\n .split(\" \")\n .map(|str| str.trim().parse().unwrap())\n .collect();\n\n {\n let (i, _) = ns.iter().enumerate().min_by_key(|&(_, n)| n).unwrap();\n ns.swap(0, i);\n }\n\n let mut increases = 0;\n for i in 1..ns.len() {\n let mut k = usize::max_value();\n let mut min = usize::max_value();\n for j in i..ns.len() {\n if ns[j] > ns[i - 1] && ns[j] < min {\n min = ns[j];\n k = j;\n }\n }\n\n if min == usize::max_value() {\n let (k, _) = ns\n .iter()\n .enumerate()\n .skip(i)\n .min_by_key(|&(_, n)| n)\n .unwrap();\n ns.swap(i, k);\n } else {\n increases += 1;\n ns.swap(i, k);\n }\n }\n\n println!(\"{}\", increases);\n}", "difficulty": "hard"} {"problem_id": "1830", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Snuke loves working out. He is now exercising N times.

\n

Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.

\n

Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ N ≤ 10^{5}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

The input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the answer modulo 10^{9}+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n
\n
\n
\n
\n
\n

Sample Output 1

6\n
\n
    \n
  • After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.
  • \n
  • After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.
  • \n
  • After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

10\n
\n
\n
\n
\n
\n

Sample Output 2

3628800\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100000\n
\n
\n
\n
\n
\n

Sample Output 3

457992974\n
\n

Print the answer modulo 10^{9}+7.

\n
\n
", "c_code": "int solution() {\n long long int N = 0;\n long long int power = 1;\n scanf(\"%lld\", &N);\n for (long long int i = 1; i <= N; i++) {\n power = power * i % 1000000007;\n }\n\n printf(\"%lld\", power);\n return 0;\n}", "rust_code": "fn solution() {\n let n: u64 = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n buf.trim().parse().unwrap()\n };\n println!(\n \"{}\",\n (1..(n + 1)).fold(1, |p, k| (p * k) % (10u64.pow(9) + 7))\n );\n}", "difficulty": "medium"} {"problem_id": "1831", "problem_description": "It's a walking tour day in SIS.Winter, so $$$t$$$ groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.Initially, some students are angry. Let's describe a group of students by a string of capital letters \"A\" and \"P\": \"A\" corresponds to an angry student \"P\" corresponds to a patient student Such string describes the row from the last to the first student.Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index $$$i$$$ in the string describing a group then they will throw a snowball at the student that corresponds to the character with index $$$i+1$$$ (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them.Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry.Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group.", "c_code": "int solution() {\n\n int totalGroups;\n\n scanf(\"%d\", &totalGroups);\n\n int studentsPerGroup[totalGroups];\n char *groups[totalGroups];\n\n int *angryStudents[totalGroups];\n int totalAngryStudents[totalGroups];\n\n for (int i = 0; i < totalGroups; i++) {\n scanf(\"%d\", &studentsPerGroup[i]);\n groups[i] = malloc(studentsPerGroup[i] * sizeof(int));\n angryStudents[i] = malloc(studentsPerGroup[i] * sizeof(int));\n\n totalAngryStudents[i] = 0;\n\n for (int j = 0; j < studentsPerGroup[i]; j++) {\n scanf(\" %c\", (groups[i] + j));\n if (*(groups[i] + j) == 'A') {\n *(angryStudents[i] + totalAngryStudents[i]) = j;\n totalAngryStudents[i]++;\n }\n }\n }\n\n for (int i = 0; i < totalGroups; i++) {\n int highestInterval = 0;\n for (int j = 0; j < totalAngryStudents[i]; j++) {\n if (j == totalAngryStudents[i] - 1) {\n if (studentsPerGroup[i] - *(angryStudents[i] + j) - 1 >\n *(angryStudents[i] + highestInterval + 1) -\n *(angryStudents[i] + highestInterval) - 1) {\n highestInterval = j;\n }\n } else {\n if (*(angryStudents[i] + j + 1) - *(angryStudents[i] + j) - 1 >\n *(angryStudents[i] + highestInterval + 1) -\n *(angryStudents[i] + highestInterval) - 1) {\n highestInterval = j;\n }\n }\n }\n\n if (totalAngryStudents[i] == 0) {\n printf(\"0\\n\");\n } else if (highestInterval == totalAngryStudents[i] - 1) {\n printf(\"%d\\n\",\n studentsPerGroup[i] - *(angryStudents[i] + highestInterval) - 1);\n } else {\n printf(\"%d\\n\", *(angryStudents[i] + highestInterval + 1) -\n *(angryStudents[i] + highestInterval) - 1);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buffer = String::new();\n io::stdin().read_line(&mut buffer).expect(\"err\");\n let n: i32 = buffer.trim().parse().unwrap();\n for _ in 0..n {\n io::stdin().read_line(&mut buffer).expect(\"err\");\n buffer.clear();\n io::stdin().read_line(&mut buffer).expect(\"err\");\n\n buffer = String::from(buffer.trim());\n buffer = String::from(buffer.trim_start_matches(\"P\"));\n\n let subs: Vec<&str> = buffer.split(\"A\").collect();\n let mut max_v = 0;\n for v in subs {\n if v.len() > max_v {\n max_v = v.len();\n }\n }\n println!(\"{}\", max_v);\n }\n}", "difficulty": "hard"} {"problem_id": "1832", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Alice and Bob are controlling a robot. They each have one switch that controls the robot.
\nAlice started holding down her button A second after the start-up of the robot, and released her button B second after the start-up.
\nBob started holding down his button C second after the start-up, and released his button D second after the start-up.
\nFor how many seconds both Alice and Bob were holding down their buttons?

\n
\n
\n
\n
\n

Constraints

    \n
  • 0≤A<B≤100
  • \n
  • 0≤C<D≤100
  • \n
  • All input values are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A B C D\n
\n
\n
\n
\n
\n

Output

Print the length of the duration (in seconds) in which both Alice and Bob were holding down their buttons.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

0 75 25 100\n
\n
\n
\n
\n
\n

Sample Output 1

50\n
\n

Alice started holding down her button 0 second after the start-up of the robot, and released her button 75 second after the start-up.
\nBob started holding down his button 25 second after the start-up, and released his button 100 second after the start-up.
\nTherefore, the time when both of them were holding down their buttons, is the 50 seconds from 25 seconds after the start-up to 75 seconds after the start-up.

\n
\n
\n
\n
\n
\n

Sample Input 2

0 33 66 99\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

Alice and Bob were not holding their buttons at the same time, so the answer is zero seconds.

\n
\n
\n
\n
\n
\n

Sample Input 3

10 90 20 80\n
\n
\n
\n
\n
\n

Sample Output 3

60\n
\n
\n
", "c_code": "int solution() {\n int as = 0;\n int ae = 0;\n int bs = 0;\n int be = 0;\n\n scanf(\"%d %d %d %d\", &as, &ae, &bs, &be);\n if (as - bs >= 0) {\n if (be - as <= 0) {\n printf(\"0\\n\");\n } else if (be - as > 0 && ae - be > 0) {\n printf(\"%d\\n\", be - as);\n } else if (be - as > 0 && ae - be <= 0) {\n printf(\"%d\\n\", ae - as);\n }\n } else {\n if (ae - bs <= 0) {\n printf(\"0\\n\");\n } else if (ae - bs > 0 && be - ae > 0) {\n printf(\"%d\\n\", ae - bs);\n } else if (ae - bs > 0 && be - ae <= 0) {\n printf(\"%d\\n\", be - bs);\n }\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut times: Vec = {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).ok();\n buf.split_whitespace()\n .map(|e| e.trim().parse().ok().unwrap())\n .collect()\n };\n\n if times[0] >= times[3] || times[1] <= times[2] {\n println!(\"{}\", 0);\n return;\n }\n\n times.sort();\n println!(\"{}\", times[2] - times[1]);\n}", "difficulty": "medium"} {"problem_id": "1833", "problem_description": "\n\n\n

Distance II


\n\n

\n Your task is to calculate the distance between two $n$ dimensional vectors $x = \\{x_1, x_2, ..., x_n\\}$ and $y = \\{y_1, y_2, ..., y_n\\}$.\n

\n\n

\n The Minkowski's distance defined below is a metric which is a generalization of both the Manhattan distance and the Euclidean distance.\n
\n\\[\nD_{xy} = (\\sum_{i=1}^n |x_i - y_i|^p)^{\\frac{1}{p}}\n\\]\n
\n\nIt can be the Manhattan distance \n
\n\n\\[\nD_{xy} = |x_1 - y_1| + |x_2 - y_2| + ... + |x_n - y_n|\n\\]\n\n
\nwhere $p = 1 $.\n\n
\n
\nIt can be the Euclidean distance\n
\n\\[\nD_{xy} = \\sqrt{(|x_1 - y_1|)^{2} + (|x_2 - y_2|)^{2} + ... + (|x_n - y_n|)^{2}}\n\\]\n
\nwhere $p = 2 $.\n\n
\n
\nAlso, it can be the Chebyshev distance\n
\n
\n\\[\nD_{xy} = max_{i=1}^n (|x_i - y_i|)\n\\]\n
\n
\nwhere $p = \\infty$\n

\n\n

\nWrite a program which reads two $n$ dimensional vectors $x$ and $y$, and calculates Minkowski's distance where $p = 1, 2, 3, \\infty$ respectively.\n

\n\n

Input

\n

\n In the first line, an integer $n$ is given. In the second and third line, $x = \\{x_1, x_2, ... x_n\\}$ and $y = \\{y_1, y_2, ... y_n\\}$ are given respectively. The elements in $x$ and $y$ are given in integers.\n

\n\n

Output

\n

\n Print the distance where $p = 1, 2, 3$ and $\\infty$ in a line respectively.\nThe output should not contain an absolute error greater than 10-5.\n

\n\n

Constraints

\n
    \n
  • $1 \\leq n \\leq 100$
  • \n
  • $0 \\leq x_i, y_i \\leq 1000$
  • \n
\n\n

Sample Input

\n
\n3\n1 2 3\n2 0 4\n
\n\n\n

Sample Output

\n\n
\n4.000000\n2.449490\n2.154435\n2.000000\n
", "c_code": "int solution() {\n int n;\n int x[100];\n int y[100];\n double ans[3] = {0.0};\n int ans_max = -1;\n\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &x[i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &y[i]);\n }\n\n for (int i = 0; i < n; i++) {\n double temp = (double)abs(x[i] - y[i]);\n ans[0] += temp;\n ans[1] += temp * temp;\n ans[2] += temp * temp * temp;\n if (ans_max < (int)temp) {\n ans_max = (int)temp;\n }\n }\n ans[1] = sqrt(ans[1]);\n ans[2] = cbrt(ans[2]);\n printf(\"%f\\n%f\\n%f\\n%d\\n\", ans[0], ans[1], ans[2], ans_max);\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let n = s.trim().parse().unwrap();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let x: Vec = s\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect::<_>();\n\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let y: Vec = s\n .split_whitespace()\n .map(|x| x.parse().unwrap())\n .collect::<_>();\n\n let mut a: f64 = 0.;\n for i in 0..n {\n a += (x[i] - y[i]).abs();\n }\n\n let mut b: f64 = 0.;\n for i in 0..n {\n b += (x[i] - y[i]).powi(2);\n }\n\n let mut c: f64 = 0.;\n for i in 0..n {\n c += (x[i] - y[i]).abs().powi(3);\n }\n\n let mut d: f64 = 0.;\n for i in 0..n {\n if d < (x[i] - y[i]).abs() {\n d = (x[i] - y[i]).abs();\n }\n }\n\n println!(\"{}\\n{}\\n{}\\n{}\", a, b.sqrt(), c.powf(1_f64 / 3_f64), d);\n}", "difficulty": "medium"} {"problem_id": "1834", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

We have N squares assigned the numbers 1,2,3,\\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i.

\n

How many squares i satisfy both of the following conditions?

\n
    \n
  • The assigned number, i, is odd.
  • \n
  • The written integer is odd.
  • \n
\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N, a_i \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 \\cdots a_N\n
\n
\n
\n
\n
\n

Output

Print the number of squares that satisfy both of the conditions.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n1 3 4 5 7\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n
    \n
  • Two squares, Square 1 and 5, satisfy both of the conditions.
  • \n
  • For Square 2 and 4, the assigned numbers are not odd.
  • \n
  • For Square 3, the written integer is not odd.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

15\n13 76 46 15 50 98 93 77 31 43 84 90 6 24 14\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
", "c_code": "int solution() {\n int n = 0;\n int a = 0;\n int array[101];\n\n scanf(\"%d\", &n);\n\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &array[i]);\n if (((array[i] % 2) == 1) && ((i % 2) == 1)) {\n a++;\n }\n }\n\n printf(\"%d\", a);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = String::new();\n std::io::stdin()\n .read_line(&mut buf)\n .expect(\"failed to read\");\n buf.clear();\n std::io::stdin()\n .read_line(&mut buf)\n .expect(\"failed to read\");\n let va: Vec = buf.split_whitespace().map(|e| e.parse().unwrap()).collect();\n\n let ct: usize = va\n .iter()\n .enumerate()\n .filter(|&(i, &x)| i % 2 == 0 && x % 2 == 1)\n .count();\n\n println!(\"{}\", ct);\n}", "difficulty": "medium"} {"problem_id": "1835", "problem_description": "

Counting Sort

\n\n

\nCounting sort can be used for sorting elements in an array which each of the n input elements is an integer in the range 0 to k. The idea of counting sort is to determine, for each input element x, the number of elements less than x as C[x]. This information can be used to place element x directly into its position in the output array B. This scheme must be modified to handle the situation in which several elements have the same value. Please see the following pseudocode for the detail:\n

\n\n
\nCounting-Sort(A, B, k)\n1    for i = 0 to k\n2        do C[i] = 0\n3    for j = 1 to length[A]\n4        do C[A[j]] = C[A[j]]+1\n5    /* C[i] now contains the number of elements equal to i */\n6    for i = 1 to k\n7    do C[i] = C[i] + C[i-1]\n8    /* C[i] now contains the number of elements less than or equal to i */\n9    for j = length[A] downto 1\n10       do B[C[A[j]]] = A[j]\n11          C[A[j]] = C[A[j]]-1\n
\n\n

\nWrite a program which sorts elements of given array ascending order based on the counting sort.\n

\n\n

Input

\n\n

\nThe first line of the input includes an integer n, the number of elements in the sequence.\n

\n

\nIn the second line, n elements of the sequence are given separated by spaces characters.\n

\n\n

Output

\n\n

\nPrint the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.\n

\n\n\n

Constraints

\n\n\n
    \n
  • 1 ≤ n ≤ 2,000,000
  • \n
  • 0 ≤ A[i] ≤ 10,000
  • \n
\n\n

Sample Input 1

\n
\n7\n2 5 1 3 2 3 0\n
\n

Sample Output 1

\n
\n0 1 2 2 3 3 5\n
", "c_code": "int solution() {\n int i = 0;\n int j;\n int k = 0;\n int n;\n scanf(\"%d\", &n);\n int A[n];\n int B[n];\n int C[2000001];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &A[i]);\n }\n for (i = 0; i < 2000001; i++) {\n C[i] = 0;\n }\n\n for (i = 0; i < n; i++) {\n C[A[i]]++;\n }\n i = 0;\n k = 0;\n while (i < n) {\n for (j = 0; j < C[k]; j++) {\n B[i] = k;\n i++;\n }\n k++;\n }\n\n for (i = 0; i < n - 1; i++) {\n printf(\"%d \", B[i]);\n }\n printf(\"%d\\n\", B[n - 1]);\n return 0;\n}", "rust_code": "fn solution() {\n input!(n: usize, vs: [usize; n]);\n\n let nax = vs.iter().max().unwrap();\n let nax = *nax;\n let mut cs = vec![0usize; nax + 1];\n\n for i in 0..n {\n cs[vs[i]] += 1;\n }\n\n for i in 1..nax + 1 {\n cs[i] += cs[i - 1];\n }\n\n let mut bs = vec![0usize; n];\n for i in (0..n).rev() {\n bs[cs[vs[i]] - 1] = vs[i];\n\n cs[vs[i]] -= 1;\n }\n\n for i in 0..n {\n if i != n - 1 {\n print!(\"{} \", bs[i])\n } else {\n println!(\"{}\", bs[i]);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1836", "problem_description": "Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn. Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.", "c_code": "int solution() {\n\n int n = 0;\n int l = 0;\n int r = 0;\n\n scanf(\"%d %d %d\", &n, &l, &r);\n int a[n + 1];\n int b[n + 1];\n\n int dp1[100001];\n int dp2[100001];\n int i;\n for (int i = 0; i <= 100000; i++) {\n dp1[i] = 0;\n dp2[i] = 0;\n }\n for (i = 1; i <= n; i++) {\n\n scanf(\"%d\", &a[i]);\n if (i >= l && i <= r) {\n dp1[a[i]]++;\n }\n }\n\n for (i = 1; i <= n; i++) {\n\n scanf(\"%d\", &b[i]);\n if (i >= l && i <= r) {\n dp2[b[i]]++;\n } else {\n if (a[i] != b[i]) {\n printf(\"LIE\");\n return 0;\n }\n }\n }\n for (i = 1; i <= 100000; i++) {\n\n if (dp1[i] != dp2[i]) {\n printf(\"LIE\");\n return 0;\n }\n }\n printf(\"TRUTH\");\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let mut numbers = line.split_whitespace();\n\n let n: usize = numbers.next().unwrap().parse().unwrap();\n let l: usize = numbers.next().unwrap().parse().unwrap();\n let r: usize = numbers.next().unwrap().parse().unwrap();\n\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let a1: Vec = line\n .split_whitespace()\n .map(|i| i.parse().unwrap())\n .collect();\n\n let mut line = String::new();\n io::stdin().read_line(&mut line).unwrap();\n let a2: Vec = line\n .split_whitespace()\n .map(|i| i.parse().unwrap())\n .collect();\n\n for i in 0..n {\n if a1[i] != a2[i] && (i + 1 < l || i + 1 > r) {\n return println!(\"LIE\");\n }\n }\n println!(\"TRUTH\");\n}", "difficulty": "easy"} {"problem_id": "1837", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

\n
    \n
  • If both 3 and 5 divides i, a_i=\\mbox{FizzBuzz}.
  • \n
  • If the above does not hold but 3 divides i, a_i=\\mbox{Fizz}.
  • \n
  • If none of the above holds but 5 divides i, a_i=\\mbox{Buzz}.
  • \n
  • If none of the above holds, a_i=i.
  • \n
\n

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^6
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

15\n
\n
\n
\n
\n
\n

Sample Output 1

60\n
\n

The first 15 terms of the FizzBuzz sequence are:

\n

1,2,\\mbox{Fizz},4,\\mbox{Buzz},\\mbox{Fizz},7,8,\\mbox{Fizz},\\mbox{Buzz},11,\\mbox{Fizz},13,14,\\mbox{FizzBuzz}

\n

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

\n
\n
\n
\n
\n
\n

Sample Input 2

1000000\n
\n
\n
\n
\n
\n

Sample Output 2

266666333332\n
\n

Watch out for overflow.

\n
\n
", "c_code": "int solution() {\n int n = 0;\n unsigned long long total = 0;\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n if (i % 3 != 0 && i % 5 != 0) {\n total += i;\n }\n }\n printf(\"%llu\", total);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n stdin().read_line(&mut s).ok();\n let n: i64 = s.trim().parse().unwrap();\n let itr = (1..=n).filter(|&e| !(e % 5 == 0 || e % 3 == 0));\n\n println!(\"{}\", itr.sum::());\n}", "difficulty": "easy"} {"problem_id": "1838", "problem_description": "For his birthday, Kevin received the set of pairwise distinct numbers $$$1, 2, 3, \\ldots, n$$$ as a gift.He is going to arrange these numbers in a way such that the minimum absolute difference between two consecutive numbers be maximum possible. More formally, if he arranges numbers in order $$$p_1, p_2, \\ldots, p_n$$$, he wants to maximize the value $$$$$$\\min \\limits_{i=1}^{n - 1} \\lvert p_{i + 1} - p_i \\rvert,$$$$$$ where $$$|x|$$$ denotes the absolute value of $$$x$$$.Help Kevin to do that.", "c_code": "int solution() {\n int n;\n\n scanf(\"%d\", &n);\n int A[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &A[i]);\n }\n for (int k = 0; k < n; k++) {\n int z = A[k];\n int a = z / 2;\n int b = 2 * a;\n for (int q = 1; q <= z / 2; q++) {\n\n printf(\"%d %d \", a, b);\n a--;\n b--;\n }\n\n if (z % 2 == 1) {\n printf(\"%d\", z);\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut buf = BufWriter::new(io::stdout().lock());\n io::stdin()\n .lines()\n .skip(1)\n .flatten()\n .flat_map(|s| s.parse::())\n .for_each(|n| {\n for i in 0..(n.wrapping_div(2)) {\n write!(\n buf,\n \"{} {} \",\n n.wrapping_add(1).wrapping_div(2).wrapping_sub(i),\n n.wrapping_sub(i)\n )\n .ok();\n }\n if (n & 1) == 1 {\n writeln!(buf, \"1\",).ok();\n } else {\n writeln!(buf).ok();\n }\n });\n}", "difficulty": "hard"} {"problem_id": "1839", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

There are N points on the 2D plane, i-th of which is located on (x_i, y_i).\nThere can be multiple points that share the same coordinate.\nWhat is the maximum possible Manhattan distance between two distinct points?

\n

Here, the Manhattan distance between two points (x_i, y_i) and (x_j, y_j) is defined by |x_i-x_j| + |y_i-y_j|.

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq x_i,y_i \\leq 10^9
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nx_1 y_1\nx_2 y_2\n:\nx_N y_N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3\n1 1\n2 4\n3 2\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n

The Manhattan distance between the first point and the second point is |1-2|+|1-4|=4, which is maximum possible.

\n
\n
\n
\n
\n
\n

Sample Input 2

2\n1 1\n1 1\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n long int x;\n long int y;\n long int minz = 2000000000;\n long int maxz = 2;\n long int minw = 1000000000;\n long int maxw = -1000000000;\n for (int i = 0; i < n; i++) {\n scanf(\"%ld %ld\", &x, &y);\n if (x + y < minz) {\n minz = x + y;\n }\n if (x + y > maxz) {\n maxz = x + y;\n }\n if (x - y < minw) {\n minw = x - y;\n }\n if (x - y > maxw) {\n maxw = x - y;\n }\n }\n\n if (maxz - minz > maxw - minw) {\n printf(\"%ld\\n\", maxz - minz);\n } else {\n printf(\"%ld\\n\", maxw - minw);\n }\n return 0;\n}", "rust_code": "fn solution() {\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 x, mut y) = (vec![0i64; n], vec![0i64; n]);\n for i in 0..n {\n let mut buf = String::new();\n std::io::stdin().read_line(&mut buf).unwrap();\n let mut iter = buf.split_whitespace();\n x[i] = iter.next().unwrap().parse().unwrap();\n y[i] = iter.next().unwrap().parse().unwrap();\n }\n\n let ans = {\n *[\n (0..n).map(|i| x[i] + y[i]).max().unwrap() - (0..n).map(|i| x[i] + y[i]).min().unwrap(),\n (0..n).map(|i| x[i] - y[i]).max().unwrap() - (0..n).map(|i| x[i] - y[i]).min().unwrap(),\n (0..n).map(|i| -x[i] + y[i]).max().unwrap()\n - (0..n).map(|i| -x[i] + y[i]).min().unwrap(),\n (0..n).map(|i| -x[i] - y[i]).max().unwrap()\n - (0..n).map(|i| -x[i] - y[i]).min().unwrap(),\n ]\n .iter()\n .max()\n .unwrap()\n };\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1840", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \\leq i \\leq H, 1 \\leq j \\leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #.

\n

Consider doing the following operation:

\n
    \n
  • Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
  • \n
\n

You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq H, W \\leq 6
  • \n
  • 1 \\leq K \\leq HW
  • \n
  • c_{i,j} is . or #.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W K\nc_{1,1}c_{1,2}...c_{1,W}\nc_{2,1}c_{2,2}...c_{2,W}\n:\nc_{H,1}c_{H,2}...c_{H,W}\n
\n
\n
\n
\n
\n

Output

Print an integer representing the number of choices of rows and columns satisfying the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 3 2\n..#\n###\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

Five choices below satisfy the condition.

\n
    \n
  • The 1-st row and 1-st column
  • \n
  • The 1-st row and 2-nd column
  • \n
  • The 1-st row and 3-rd column
  • \n
  • The 1-st and 2-nd column
  • \n
  • The 3-rd column
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

2 3 4\n..#\n###\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

One choice, which is choosing nothing, satisfies the condition.

\n
\n
\n
\n
\n
\n

Sample Input 3

2 2 3\n##\n##\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
\n
\n
\n
\n

Sample Input 4

6 6 8\n..##..\n.#..#.\n#....#\n######\n#....#\n#....#\n
\n
\n
\n
\n
\n

Sample Output 4

208\n
\n
\n
", "c_code": "int solution() {\n int H;\n int W;\n int K;\n scanf(\"%d %d %d\", &H, &W, &K);\n char m[H * W];\n for (int i = 0; i < H * W; i++) {\n scanf(\" %c\", &m[i]);\n }\n int count = 0;\n for (int h = 0; h < 1 << H; h++) {\n for (int w = 0; w < 1 << W; w++) {\n int black = 0;\n for (int i = 0; i < H; i++) {\n for (int j = 0; j < W; j++) {\n if (h >> i & 1 && w >> j & 1 && m[(i * W) + j] == '#') {\n black++;\n }\n }\n }\n if (black == K) {\n count++;\n }\n }\n }\n printf(\"%d\\n\", count);\n return 0;\n}", "rust_code": "fn solution() {\n let mut x = String::new();\n io::stdin().read_line(&mut x).unwrap();\n let hwk: Vec = x\n .trim_end()\n .split(' ')\n .map(|s| s.parse().unwrap())\n .collect();\n let h = hwk[0];\n let w = hwk[1];\n let k = hwk[2];\n let mut b: [[bool; 6]; 6] = Default::default();\n let lines = BufReader::new(io::stdin()).split(b'\\n');\n for (row, line) in b.iter_mut().zip(lines) {\n for (cell, x) in row.iter_mut().zip(line.unwrap()) {\n if x == b'#' {\n *cell = true;\n }\n }\n }\n let mut ans = 0;\n for i in 0..1 << h {\n for j in 0..1 << w {\n let mut cnt = 0;\n for r in 0..h {\n if (i >> r) & 1 == 0 {\n for c in 0..w {\n if (j >> c) & 1 == 0 && b[r][c] {\n cnt += 1;\n }\n }\n }\n }\n if cnt == k {\n ans += 1;\n }\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1841", "problem_description": "Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings ak points to the player. Alex is a perfectionist, so he decided to get as many points as possible. Help him.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n int a[n];\n long long int b[100002];\n long long int dp[100002];\n for (int i = 0; i <= 100001; i++) {\n b[i] = 0;\n dp[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n b[a[i]]++;\n }\n\n dp[1] = b[1] * 1;\n dp[2] = b[2] * 2;\n\n long long int max = 0;\n\n for (int i = 3; i < 100001; i++) {\n if (dp[i - 2] > dp[i - 3]) {\n dp[i] = dp[i - 2] + b[i] * i;\n } else {\n dp[i] = dp[i - 3] + b[i] * i;\n }\n if (max < dp[i]) {\n max = dp[i];\n }\n }\n\n printf(\"%lld\", max);\n}", "rust_code": "fn solution() {\n let _ = {\n let mut line = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().to_string()\n };\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 mut count = HashMap::new();\n v.iter().for_each(|i| *count.entry(i).or_insert(0) += 1);\n\n let n = v.iter().max().unwrap();\n let mut dp = HashMap::new();\n for i in 1..n + 1 {\n *dp.entry(i).or_insert(0) = std::cmp::max(\n (count.get(&i).unwrap_or(&0)) * i + dp.get(&(i - 2)).cloned().unwrap_or(0),\n dp.get(&(i - 1)).cloned().unwrap_or(0),\n )\n }\n println!(\"{:?}\", dp.values().max().unwrap());\n}", "difficulty": "medium"} {"problem_id": "1842", "problem_description": "\n

Score : 200 points

\n
\n
\n

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together.

\n

Given an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print Yes; if it cannot, print No.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 100
  • \n
  • N is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

If N can be represented as the product of two integers between 1 and 9 (inclusive), print Yes; if it cannot, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

10\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

10 can be represented as, for example, 2 \\times 5.

\n
\n
\n
\n
\n
\n

Sample Input 2

50\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

50 cannot be represented as the product of two integers between 1 and 9.

\n
\n
\n
\n
\n
\n

Sample Input 3

81\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n
\n
", "c_code": "int solution(void) {\n int n = 0;\n int flag = 0;\n scanf(\"%d\", &n);\n for (int i = 1; i <= 9; i++) {\n if (n == (n / i) * i && (n / i) <= 9) {\n flag = 1;\n break;\n }\n }\n if (flag == 1) {\n printf(\"Yes\");\n } else {\n printf(\"No\");\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 let line: Vec = buf.split_whitespace().map(|s| s.parse().unwrap()).collect();\n let N = line[0];\n for i in 1..10 {\n for j in 1..10 {\n if i * j == N {\n println!(\"Yes\");\n return;\n }\n }\n }\n println!(\"No\")\n}", "difficulty": "medium"} {"problem_id": "1843", "problem_description": "Exponential search", "c_code": "int64_t solution(const int64_t *arr, const uint16_t length,\n const int64_t n) {\n if (length == 0) {\n return -1;\n }\n\n uint32_t upper_bound = 1;\n while (upper_bound < length && arr[upper_bound] < n) {\n upper_bound = upper_bound * 2;\n }\n\n uint16_t lower_bound = upper_bound / 2;\n if (upper_bound >= length) {\n upper_bound = length - 1;\n }\n\n while (lower_bound <= upper_bound) {\n uint16_t middle_index = lower_bound + (upper_bound - lower_bound) / 2;\n\n if (arr[middle_index] == n) {\n return middle_index;\n }\n\n if (arr[middle_index] > n) {\n\n if (middle_index == 0) {\n break;\n }\n upper_bound = middle_index - 1;\n } else {\n\n lower_bound = middle_index + 1;\n }\n }\n\n return -1;\n}\n", "rust_code": "fn solution(item: &T, arr: &[T]) -> Option {\n let len = arr.len();\n if len == 0 {\n return None;\n }\n let mut upper = 1;\n while (upper < len) && (&arr[upper] <= item) {\n upper *= 2;\n }\n if upper > len {\n upper = len\n }\n\n let mut lower = upper / 2;\n while lower < upper {\n let mid = lower + (upper - lower) / 2;\n\n match item.cmp(&arr[mid]) {\n Ordering::Less => upper = mid,\n Ordering::Equal => return Some(mid),\n Ordering::Greater => lower = mid + 1,\n }\n }\n None\n}\n", "difficulty": "medium"} {"problem_id": "1844", "problem_description": "A new e-mail service \"Berlandesk\" is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that's why they ask you to help. You're suggested to implement the prototype of site registration system. The system should work on the following principle. Each time a new user wants to register, he sends to the system a request with his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, ...), among these numbers the least i is found so that namei does not yet exist in the database.", "c_code": "int solution() {\n int i;\n int n;\n int j;\n int c = 0;\n scanf(\"%d\", &n);\n int k[n];\n char s[n][100];\n for (i = 0; i < n; i++) {\n k[i] = 0;\n scanf(\"%s\", s[i]);\n for (j = i - 1; j >= 0; j--) {\n for (c = 0; s[i][c] == s[j][c]; c++) {\n if (s[i][c] == 0) {\n break;\n }\n }\n if (s[i][c] == 0 && s[j][c] == 0) {\n k[i] = k[j] + 1;\n break;\n }\n }\n k[i] ? printf(\"%s%d\\n\", s[i], k[i]) : printf(\"OK\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n let n: i32 = s.trim().parse().unwrap();\n let mut map = HashMap::new();\n for _i in 0..n {\n let mut s = String::new();\n io::stdin().read_line(&mut s).unwrap();\n s = s.trim().to_string();\n let s2 = String::from(&s);\n let count = map.entry(s2).or_insert(0);\n if *count == 0 {\n println!(\"OK\");\n } else {\n println!(\"{}{}\", s, *count);\n }\n *count += 1;\n }\n}", "difficulty": "medium"} {"problem_id": "1845", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

There are N integers a_1, a_2, ..., a_N not less than 1.\nThe values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \\times a_2 \\times ... \\times a_N = P.

\n

Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^{12}
  • \n
  • 1 \\leq P \\leq 10^{12}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N P\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 24\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

The greatest common divisor would be 2 when, for example, a_1=2, a_2=6 and a_3=2.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 1\n
\n
\n
\n
\n
\n

Sample Output 2

1\n
\n

As a_i are positive integers, the only possible case is a_1 = a_2 = a_3 = a_4 = a_5 = 1.

\n
\n
\n
\n
\n
\n

Sample Input 3

1 111\n
\n
\n
\n
\n
\n

Sample Output 3

111\n
\n
\n
\n
\n
\n
\n

Sample Input 4

4 972439611840\n
\n
\n
\n
\n
\n

Sample Output 4

206\n
\n
\n
", "c_code": "int solution(void) {\n long N;\n long P;\n long ans = 1;\n scanf(\"%ld %ld\", &N, &P);\n\n if (N == 1) {\n printf(\"%ld\", P);\n return 0;\n }\n\n int div = 0;\n\n for (int i = 2; P > 1 && i <= sqrt(P); i++) {\n div = 0;\n while (P % i == 0) {\n P /= i;\n div++;\n if (div >= N) {\n ans *= i;\n div -= N;\n }\n }\n }\n\n printf(\"%ld\", ans);\n\n return 0;\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\n let n = get!();\n let mut p = get!();\n if n == 1 {\n println!(\"{}\", p);\n return;\n }\n\n let mut ans = 1_i64;\n let mut i = 2;\n while i * i <= p {\n if p % i == 0 {\n let mut c = 0;\n while p % i == 0 {\n p /= i;\n c += 1;\n }\n let x = c / n;\n for _ in 0..x {\n ans *= i;\n }\n }\n i += 1;\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1846", "problem_description": "Consider the array $$$a$$$ composed of all the integers in the range $$$[l, r]$$$. For example, if $$$l = 3$$$ and $$$r = 7$$$, then $$$a = [3, 4, 5, 6, 7]$$$.Given $$$l$$$, $$$r$$$, and $$$k$$$, is it possible for $$$\\gcd(a)$$$ to be greater than $$$1$$$ after doing the following operation at most $$$k$$$ times? Choose $$$2$$$ numbers from $$$a$$$. Permanently remove one occurrence of each of them from the array. Insert their product back into $$$a$$$. $$$\\gcd(b)$$$ denotes the greatest common divisor (GCD) of the integers in $$$b$$$.", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n while (n--) {\n int l;\n int r;\n int k;\n scanf(\"%d %d %d\", &l, &r, &k);\n int j = 0;\n if (l == r && l > 1) {\n j = 1;\n } else {\n int number = r - l + 1;\n int js = number / 2;\n if (l % 2 && r % 2) {\n js++;\n }\n if (js < number && js <= k) {\n j = 1;\n }\n }\n if (j == 1) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut input = String::new();\n stdin().read_to_string(&mut input).unwrap();\n let mut input = input.split_ascii_whitespace().flat_map(str::parse);\n let t = input.next().unwrap();\n let mut output = String::new();\n for _ in 0..t {\n let l = input.next().unwrap();\n let r = input.next().unwrap();\n let k = input.next().unwrap();\n let len = r - l;\n let can = if len == 0 {\n l != 1\n } else if len % 2 == 0 {\n k >= len / 2 + l % 2\n } else {\n k > len / 2\n };\n if can {\n writeln!(output, \"YES\").unwrap();\n } else {\n writeln!(output, \"NO\").unwrap();\n }\n }\n print!(\"{}\", output);\n}", "difficulty": "easy"} {"problem_id": "1847", "problem_description": "You are given three integers $$$a$$$, $$$b$$$, and $$$c$$$. Determine if one of them is the sum of the other two.", "c_code": "int solution() {\n int a = 0;\n int b[4];\n scanf(\"%d\", &a);\n for (int j = 0; j < a; j++) {\n for (int i = 0; i < 3; i++) {\n scanf(\"%d\", &b[i]);\n }\n if ((b[0] == b[1] + b[2]) || (b[1] == b[2] + b[0]) ||\n (b[2] == b[0] + b[1])) {\n printf(\"YES\\n\");\n } else {\n printf(\"NO\\n\");\n }\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut line = String::new();\n\n std::io::stdin().read_line(&mut line).unwrap();\n\n let line_len = line.trim().parse::().unwrap();\n\n for _ in 0..line_len {\n line = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n\n let num_strs: Vec<&str> = line.trim().split(\" \").collect();\n\n let a = num_strs.first().unwrap().parse::().unwrap();\n let b = num_strs.get(1).unwrap().parse::().unwrap();\n let c = num_strs.get(2).unwrap().parse::().unwrap();\n\n if a == b + c || b == a + c || c == a + b {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1848", "problem_description": "\n

Score : 700 points

\n
\n
\n

Problem Statement

We have a sequence of N \\times K integers: X=(X_0,X_1,\\cdots,X_{N \\times K-1}).\nIts elements are represented by another sequence of N integers: A=(A_0,A_1,\\cdots,A_{N-1}). For each pair i, j (0 \\leq i \\leq K-1,\\ 0 \\leq j \\leq N-1), X_{i \\times N + j}=A_j holds.

\n

Snuke has an integer sequence s, which is initially empty.\nFor each i=0,1,2,\\cdots,N \\times K-1, in this order, he will perform the following operation:

\n
    \n
  • If s does not contain X_i: add X_i to the end of s.
  • \n
  • If s does contain X_i: repeatedly delete the element at the end of s until s no longer contains X_i. Note that, in this case, we do not add X_i to the end of s.
  • \n
\n

Find the elements of s after Snuke finished the operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq K \\leq 10^{12}
  • \n
  • 1 \\leq A_i \\leq 2 \\times 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nA_0 A_1 \\cdots A_{N-1}\n
\n
\n
\n
\n
\n

Output

Print the elements of s after Snuke finished the operations, in order from beginning to end, with spaces in between.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 2\n1 2 3\n
\n
\n
\n
\n
\n

Sample Output 1

2 3\n
\n

In this case, X=(1,2,3,1,2,3).\nWe will perform the operations as follows:

\n
    \n
  • i=0: s does not contain 1, so we add 1 to the end of s, resulting in s=(1).
  • \n
  • i=1: s does not contain 2, so we add 2 to the end of s, resulting in s=(1,2).
  • \n
  • i=2: s does not contain 3, so we add 3 to the end of s, resulting in s=(1,2,3).
  • \n
  • i=3: s does contain 1, so we repeatedly delete the element at the end of s as long as s contains 1, which causes the following changes to s: (1,2,3)→(1,2)→(1)→().
  • \n
  • i=4: s does not contain 2, so we add 2 to the end of s, resulting in s=(2).
  • \n
  • i=5: s does not contain 3, so we add 3 to the end of s, resulting in s=(2,3).
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

5 10\n1 2 3 2 3\n
\n
\n
\n
\n
\n

Sample Output 2

3\n
\n
\n
\n
\n
\n
\n

Sample Input 3

6 1000000000000\n1 1 2 2 3 3\n
\n
\n
\n
\n
\n

Sample Output 3

\n

s may be empty in the end.

\n
\n
\n
\n
\n
\n

Sample Input 4

11 97\n3 1 4 1 5 9 2 6 5 3 5\n
\n
\n
\n
\n
\n

Sample Output 4

9 2 6\n
\n
\n
", "c_code": "int solution() {\n int i;\n int N;\n int A[200001];\n long long K;\n scanf(\"%d %lld\", &N, &K);\n for (i = 1; i <= N; i++) {\n scanf(\"%d\", &(A[i]));\n }\n A[0] = 0;\n\n list *L[200001] = {};\n list d[200002];\n list *p;\n d[0].v = 0;\n d[0].next = L[0];\n L[0] = &(d[0]);\n d[N + 1].v = 0;\n d[N + 1].next = NULL;\n for (i = N; i >= 1; i--) {\n d[i].v = i;\n d[i].next = L[A[i]];\n L[A[i]] = &(d[i]);\n }\n\n int j;\n int k;\n int l;\n int move[200001];\n for (i = 0; i <= 200000; i++) {\n move[i] = -1;\n }\n for (i = 0, k = 0; move[i] == -1; i = move[i], k++) {\n for (p = &(d[L[i]->v + 1]); p->next != NULL; p = &(d[p->next->v + 1])) {\n ;\n }\n move[i] = A[p->v];\n }\n for (j = 0, l = 0; j != i; j = move[j], l++, k--) {\n ;\n }\n\n int s[200001];\n if (K >= l) {\n K = (int)(((K - l) % k) + l);\n }\n for (i = 0, j = 1; j < K; i = move[i], j++) {\n ;\n }\n j = L[i]->v + 1;\n k = 0;\n while (j <= N) {\n if (d[j].next == NULL) {\n s[k++] = A[j++];\n } else {\n j = d[j].next->v + 1;\n }\n }\n for (i = 0; i < k - 1; i++) {\n printf(\"%d \", s[i]);\n }\n if (k > 0) {\n printf(\"%d\\n\", s[k - 1]);\n }\n fflush(stdout);\n return 0;\n}", "rust_code": "fn solution() {\n let mut input_str = String::new();\n std::io::stdin().read_to_string(&mut input_str).unwrap();\n\n let input_parts = input_str.split_whitespace().collect::>();\n let mut input_parts_it = input_parts.iter().cloned();\n\n let n: usize = input_parts_it.next().unwrap().parse().unwrap();\n let k: u64 = input_parts_it.next().unwrap().parse().unwrap();\n\n let a: Vec = (0..n)\n .map(|_| input_parts_it.next().unwrap().parse().unwrap())\n .collect();\n\n const NONE: usize = 0xffffffff;\n let mut group_end: Vec = vec![NONE; a.len()];\n\n let mut next_a_same_value_for_a: Vec = vec![NONE; a.len()];\n let mut last_a_for_aval: Vec = vec![NONE; 200001];\n\n for (i, &aval) in a.iter().enumerate().rev() {\n next_a_same_value_for_a[i] = last_a_for_aval[aval as usize];\n last_a_for_aval[aval as usize] = i;\n }\n\n for (i, &aval) in a.iter().enumerate() {\n let mut k = next_a_same_value_for_a[i];\n if k == NONE {\n k = last_a_for_aval[aval as usize];\n debug_assert!(k != NONE);\n k += a.len();\n }\n k += 1;\n\n group_end[i] = k;\n }\n\n let mut cycle_len = 0;\n {\n let mut i = 0;\n loop {\n let hop = group_end[i] - i;\n cycle_len += hop as u64;\n i = group_end[i];\n while i >= a.len() {\n i -= a.len();\n }\n if i == 0 {\n break;\n }\n }\n }\n\n let mut step = n as u64 * k;\n step %= cycle_len;\n\n let mut state: Vec = Vec::with_capacity(a.len());\n let mut index: Vec = vec![NONE; 200001];\n\n {\n let mut i = 0;\n let mut remaining_steps = step;\n\n while remaining_steps >= (group_end[i] - i) as u64 {\n remaining_steps -= (group_end[i] - i) as u64;\n i = group_end[i];\n while i >= a.len() {\n i -= a.len();\n }\n }\n\n while remaining_steps > 0 {\n let aval = a[i];\n\n if index[aval as usize] == NONE {\n index[aval as usize] = state.len();\n state.push(aval);\n } else {\n while state.len() > index[aval as usize] {\n let k = state.pop().unwrap();\n index[k as usize] = NONE;\n }\n }\n\n i += 1;\n while i >= a.len() {\n i -= a.len();\n }\n remaining_steps -= 1;\n }\n }\n\n if state.is_empty() {\n println!();\n } else {\n print!(\"{}\", state[0]);\n for &x in state[1..].iter() {\n print!(\" {}\", x);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1849", "problem_description": "Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number.Help Igor find the maximum number he can write on the fence.", "c_code": "int solution() {\n int n;\n int i = 0;\n int l = 0;\n int min = 1000000;\n int a[9];\n scanf(\"%d\", &n);\n for (i = 0; i < 9; i++) {\n scanf(\"%d\", &a[i]);\n if (min >= a[i]) {\n min = a[i];\n }\n }\n if (n < min) {\n printf(\"-1\");\n }\n l = n / min;\n while (l--) {\n for (i = 8; i >= 0; i--) {\n if ((n - a[i]) / min == l && n - a[i] >= 0) {\n printf(\"%d\", i + 1);\n break;\n }\n }\n n = n - a[i];\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut s = String::new();\n let mut t = String::new();\n\n stdin.read_line(&mut s).unwrap();\n stdin.read_line(&mut t).unwrap();\n\n let n: i32 = s.trim().parse().unwrap();\n let v: Vec = t.trim().split(' ').map(|x| x.parse().unwrap()).collect();\n\n let mut cheapest_idx = 8;\n let mut cost = 100_001;\n for i in (0..v.len()).rev() {\n if v[i] < cost {\n cost = v[i];\n cheapest_idx = i;\n }\n }\n\n let mut len = n / cost;\n\n if len == 0 {\n println!(\"-1\");\n return;\n }\n\n let mut spare = n % cost;\n let mut i = v.len() - 1;\n let mut s = String::new();\n\n while spare > 0 && i > cheapest_idx {\n if cost + spare >= v[i] {\n s = format!(\"{}{}\", s, i + 1);\n spare -= v[i] - cost;\n len -= 1;\n } else {\n i -= 1;\n }\n }\n\n if len > 0 {\n let tail: String = vec![(cheapest_idx + 1).to_string(); len as usize]\n .into_iter()\n .collect();\n s = format!(\"{}{}\", s, tail);\n }\n\n println!(\"{}\", s);\n}", "difficulty": "medium"} {"problem_id": "1850", "problem_description": "One day Prof. Slim decided to leave the kingdom of the GUC to join the kingdom of the GIU. He was given an easy online assessment to solve before joining the GIU. Citizens of the GUC were happy sad to see the prof leaving, so they decided to hack into the system and change the online assessment into a harder one so that he stays at the GUC. After a long argument, they decided to change it into the following problem.Given an array of $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$, where $$$a_{i} \\neq 0$$$, check if you can make this array sorted by using the following operation any number of times (possibly zero). An array is sorted if its elements are arranged in a non-decreasing order. select two indices $$$i$$$ and $$$j$$$ ($$$1 \\le i,j \\le n$$$) such that $$$a_i$$$ and $$$a_j$$$ have different signs. In other words, one must be positive and one must be negative. swap the signs of $$$a_{i}$$$ and $$$a_{j}$$$. For example if you select $$$a_i=3$$$ and $$$a_j=-2$$$, then they will change to $$$a_i=-3$$$ and $$$a_j=2$$$. Prof. Slim saw that the problem is still too easy and isn't worth his time, so he decided to give it to you to solve.", "c_code": "int solution() {\n unsigned short t;\n scanf(\"%hu\", &t);\n while (t--) {\n unsigned short n;\n scanf(\"%hu\", &n);\n int arr[n];\n unsigned short neg_count = 0;\n for (unsigned register short i = 0; i < n; i++) {\n scanf(\"%i\", &arr[i]);\n neg_count += arr[i] < 0;\n }\n for (unsigned register short i = 0; i < n; i++) {\n arr[i] = abs(arr[i]);\n }\n bool neg_order = true, pos_order = true;\n for (unsigned register short i = 0; i < n - 1; i++) {\n if (i < neg_count - 1 && neg_count > 0) {\n if (arr[i] < arr[i + 1]) {\n neg_order = 0;\n break;\n }\n }\n if (i > neg_count - 1) {\n if (arr[i] > arr[i + 1]) {\n pos_order = 0;\n break;\n }\n }\n }\n (neg_order && pos_order) ? printf(\"YES\\n\") : printf(\"NO\\n\");\n }\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let lines = stdin\n .lock()\n .lines()\n .collect::>>();\n let mut lines = lines.iter();\n lines.next();\n lines.next();\n\n for line in lines.step_by(2) {\n let line = line.as_ref().unwrap();\n let numbers = line\n .split_whitespace()\n .map(|s| s.parse::().unwrap())\n .collect::>();\n let negative_count = numbers.iter().filter(|x| x < &&0).count();\n let numbers = numbers.iter().map(|x| x.abs()).collect::>();\n let (negatives, positives) = (\n numbers.iter().take(negative_count),\n numbers.iter().skip(negative_count),\n );\n if negatives\n .fold(Some(i32::MAX), |x, y| {\n if let Some(x) = x {\n if x >= *y {\n Some(*y)\n } else {\n None\n }\n } else {\n None\n }\n })\n .is_some()\n && positives\n .fold(Some(-1), |x, y| {\n if let Some(x) = x {\n if x <= *y {\n Some(*y)\n } else {\n None\n }\n } else {\n None\n }\n })\n .is_some()\n {\n println!(\"YES\");\n } else {\n println!(\"NO\");\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1851", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

You have three tasks, all of which need to be completed.

\n

First, you can complete any one task at cost 0.

\n

Then, just after completing the i-th task, you can complete the j-th task at cost |A_j - A_i|.

\n

Here, |x| denotes the absolute value of x.

\n

Find the minimum total cost required to complete all the task.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq A_1, A_2, A_3 \\leq 100
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
A_1 A_2 A_3\n
\n
\n
\n
\n
\n

Output

Print the minimum total cost required to complete all the task.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 6 3\n
\n
\n
\n
\n
\n

Sample Output 1

5\n
\n

When the tasks are completed in the following order, the total cost will be 5, which is the minimum:

\n
    \n
  • Complete the first task at cost 0.
  • \n
  • Complete the third task at cost 2.
  • \n
  • Complete the second task at cost 3.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

11 5 5\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n
\n
\n
\n
\n
\n

Sample Input 3

100 100 100\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
", "c_code": "int solution(void) {\n int A1 = 0;\n int A2 = 0;\n int A3 = 0;\n int tmp = 0;\n\n do {\n scanf(\"%d %d %d\", &A1, &A2, &A3);\n } while (A1 < 1 && A1 > 100 && A2 < 1 && A2 > 100 && A3 < 1 && A3 > 100);\n\n if (A1 >= A2 && A1 >= A3) {\n tmp = A1;\n } else if (A2 >= A1 && A2 >= A3) {\n tmp = A2;\n } else if (A3 >= A1 && A3 >= A2) {\n tmp = A3;\n }\n\n if (A1 <= A2 && A1 <= A3) {\n tmp -= A1;\n } else if (A2 <= A1 && A2 <= A3) {\n tmp -= A2;\n } else if (A3 <= A1 && A3 <= A2) {\n tmp -= A3;\n }\n\n printf(\"%d\\n\", tmp);\n}", "rust_code": "fn solution() {\n let mut n = String::new();\n std::io::stdin().read_line(&mut n).unwrap();\n let mut task: Vec = n.split_whitespace().map(|x| x.parse().unwrap()).collect();\n\n task.sort();\n\n let mut ans = 0;\n for i in 1..task.len() {\n ans += task[i] - task[i - 1];\n }\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1852", "problem_description": "Cowboy Vlad has a birthday today! There are $$$n$$$ children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standing next to each other, and it will be difficult for them to hold hands. Therefore, children want to stand in a circle so that the maximum difference between the growth of two neighboring children would be minimal possible.Formally, let's number children from $$$1$$$ to $$$n$$$ in a circle order, that is, for every $$$i$$$ child with number $$$i$$$ will stand next to the child with number $$$i+1$$$, also the child with number $$$1$$$ stands next to the child with number $$$n$$$. Then we will call the discomfort of the circle the maximum absolute difference of heights of the children, who stand next to each other.Please help children to find out how they should reorder themselves, so that the resulting discomfort is smallest possible.", "c_code": "int solution() {\n int n;\n int a[100];\n scanf(\"%d\", &n);\n for (int i = 0; i < n; ++i) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; ++i) {\n int k = i;\n for (int j = i + 1; j < n; ++j) {\n if (a[j] > a[k]) {\n k = j;\n }\n }\n if (k != i) {\n int temp = a[k];\n a[k] = a[i];\n a[i] = temp;\n }\n }\n for (int i = n - (n & 1); i >= 0; i -= 2) {\n if (i <= n - 1) {\n printf(\"%d \", a[i]);\n }\n }\n for (int i = 1; i < n; i += 2) {\n printf(\"%d \", a[i]);\n }\n puts(\"\");\n return 0;\n}", "rust_code": "fn solution() {\n let mut sin = String::new();\n std::io::stdin().read_line(&mut sin).expect(\"read error\");\n sin.clear();\n\n std::io::stdin().read_line(&mut sin).expect(\"read error\");\n let mut vi32: Vec = sin.split_whitespace().map(|c| c.parse().unwrap()).collect();\n vi32.sort_unstable();\n\n let mut i: i32 = 0;\n let mut rev = 0;\n loop {\n print!(\"{} \", vi32[i as usize]);\n if rev == 0 {\n i += 2;\n if i as usize >= vi32.len() {\n rev = 1;\n if vi32.len().is_multiple_of(2) {\n i = vi32.len() as i32 - 1;\n } else {\n i = vi32.len() as i32 - 2;\n }\n }\n } else {\n i -= 2;\n if i == -1 {\n break;\n }\n }\n }\n println!();\n}", "difficulty": "hard"} {"problem_id": "1853", "problem_description": "Masha has $$$n$$$ types of tiles of size $$$2 \\times 2$$$. Each cell of the tile contains one integer. Masha has an infinite number of tiles of each type.Masha decides to construct the square of size $$$m \\times m$$$ consisting of the given tiles. This square also has to be a symmetric with respect to the main diagonal matrix, and each cell of this square has to be covered with exactly one tile cell, and also sides of tiles should be parallel to the sides of the square. All placed tiles cannot intersect with each other. Also, each tile should lie inside the square. See the picture in Notes section for better understanding.Symmetric with respect to the main diagonal matrix is such a square $$$s$$$ that for each pair $$$(i, j)$$$ the condition $$$s[i][j] = s[j][i]$$$ holds. I.e. it is true that the element written in the $$$i$$$-row and $$$j$$$-th column equals to the element written in the $$$j$$$-th row and $$$i$$$-th column.Your task is to determine if Masha can construct a square of size $$$m \\times m$$$ which is a symmetric matrix and consists of tiles she has. Masha can use any number of tiles of each type she has to construct the square. Note that she can not rotate tiles, she can only place them in the orientation they have in the input.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n\n int k;\n int j = 0;\n int yes = 0;\n scanf(\"%d\", &k);\n int n[k];\n int m[k];\n int matrix[k][100][2][2];\n while (j < k) {\n int i = 0;\n scanf(\"%d %d\", &n[j], &m[j]);\n while (i < n[j]) {\n scanf(\"%d %d\", &matrix[j][i][0][0], &matrix[j][i][0][1]);\n scanf(\"%d %d\", &matrix[j][i][1][0], &matrix[j][i][1][1]);\n i++;\n }\n j++;\n }\n j = 0;\n\n while (j < k) {\n yes = 0;\n if (m[j] % 2 == 0) {\n while (n[j]--) {\n if (matrix[j][n[j]][0][1] == matrix[j][n[j]][1][0]) {\n printf(\"YES\\n\");\n yes = 1;\n break;\n }\n }\n }\n if (yes == 0) {\n printf(\"NO\\n\");\n }\n j++;\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).unwrap();\n let t = t.trim().parse::().unwrap();\n\n for _i in 0..t {\n let mut input = String::new();\n io::stdin().read_line(&mut input).unwrap();\n let mut input = input.split_whitespace();\n\n let n = input.next().unwrap().parse::().unwrap();\n let m = input.next().unwrap().parse::().unwrap();\n\n if m % 2 != 0 {\n println!(\"NO\");\n\n for _q in 0..n {\n let mut up = String::new();\n io::stdin().read_line(&mut up).unwrap();\n let _up = up.split_whitespace();\n\n let mut down = String::new();\n io::stdin().read_line(&mut down).unwrap();\n let _down = down.split_whitespace();\n }\n } else {\n let mut correct = false;\n\n for _q in 0..n {\n let mut up = String::new();\n io::stdin().read_line(&mut up).unwrap();\n let mut up = up.split_whitespace();\n\n let _x1 = up.next().unwrap().parse::().unwrap();\n let x2 = up.next().unwrap().parse::().unwrap();\n\n let mut down = String::new();\n io::stdin().read_line(&mut down).unwrap();\n let mut down = down.split_whitespace();\n\n let x3 = down.next().unwrap().parse::().unwrap();\n let _x4 = down.next().unwrap().parse::().unwrap();\n\n if !correct && x2 == x3 {\n println!(\"YES\");\n correct = true;\n }\n }\n\n if !correct {\n println!(\"NO\");\n }\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1854", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

You are given a sequence with N integers: A = \\{ A_1, A_2, \\cdots, A_N \\}.\nFor each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

\n
    \n
  • If A_i and A_j (i < j) are painted with the same color, A_i < A_j.
  • \n
\n

Find the minimum number of colors required to satisfy the condition.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • 0 \\leq A_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nA_1\n:\nA_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of colors required to satisfy the condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5\n2\n1\n4\n5\n3\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

\n
\n
\n
\n
\n
\n

Sample Input 2

4\n0\n0\n0\n0\n
\n
\n
\n
\n
\n

Sample Output 2

4\n
\n

We have to paint all the integers with distinct colors.

\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n int ary[n];\n\n int bry[n];\n int length = 0;\n for (int i = 0; i < n; i++) {\n bry[i] = -1;\n scanf(\"%d\", &ary[i]);\n int k = 0;\n while (bry[k] >= ary[i]) {\n k++;\n }\n if (bry[k] == -1) {\n length++;\n }\n bry[k] = ary[i];\n }\n printf(\"%d\\n\", length);\n\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n v: [usize; n]\n }\n let mut c = vec![v[0]];\n for &a in &v[1..] {\n if a > c[0] {\n c[0] = a;\n continue;\n }\n let mut l = 0;\n let mut r = c.len();\n while l + 1 < r {\n let m = (l + r) / 2;\n if c[m] >= a {\n l = m;\n } else {\n r = m;\n }\n }\n if r == c.len() {\n c.push(a);\n } else {\n c[r] = a;\n }\n }\n println!(\"{}\", c.len());\n}", "difficulty": "medium"} {"problem_id": "1855", "problem_description": "Stooge sort", "c_code": "void solution(int arr[], int i, int j) {\n int temp;\n int k;\n if (arr[i] > arr[j]) {\n temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n }\n if ((i + 1) >= j) {\n return;\n }\n k = ((j - i + 1) / 3);\n stoogesort(arr, i, j - k);\n stoogesort(arr, i + k, j);\n stoogesort(arr, i, j - k);\n}\n", "rust_code": "fn solution(arr: &mut [T]) {\n if arr.is_empty() {\n return;\n }\n\n fn sort(arr: &mut [T], start: usize, end: usize) {\n if arr[start] > arr[end] {\n arr.swap(start, end);\n }\n\n if start + 1 >= end {\n return;\n }\n\n let k = (end - start + 1) / 3;\n\n sort(arr, start, end - k);\n sort(arr, start + k, end);\n sort(arr, start, end - k);\n }\n\n sort(arr, 0, arr.len() - 1);\n}\n", "difficulty": "easy"} {"problem_id": "1856", "problem_description": "Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm: Root the tree at node 1. Count the number of nodes at an even depth. Let it be evenCnt. Count the number of nodes at an odd depth. Let it be oddCnt. The answer is the minimum between evenCnt and oddCnt. The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.", "c_code": "int solution() {\n int n;\n scanf(\"%d\", &n);\n if (2 <= n && n <= 5) {\n puts(\"-1\");\n } else {\n for (int i = 2; i <= 4; i++) {\n printf(\"%d 1\\n\", i);\n }\n for (int i = 5; i <= n; i++) {\n printf(\"%d 2\\n\", i);\n }\n }\n for (int i = 2; i <= n; i++) {\n printf(\"%d %d\\n\", i, 1);\n }\n return 0;\n}", "rust_code": "fn solution() {\n use std::io;\n use std::io::prelude::*;\n\n let mut input = String::new();\n\n io::stdin().read_to_string(&mut input).unwrap();\n\n let n: usize = input.trim().parse().unwrap();\n\n let _g = \"1 2\n1 3\n2 4\n2 5\n3 6\n4 7\n4 8\\n\";\n\n let g = \"1 2\n1 3\n1 4\n2 5\n2 6\\n\";\n\n let mut ans = String::new();\n\n if n < 6 {\n ans.push_str(\"-1\\n\");\n } else {\n ans.push_str(g);\n for i in 6 + 1..n + 1 {\n ans.push_str(&format!(\"2 {}\\n\", i));\n }\n }\n for i in 2..n + 1 {\n ans.push_str(&format!(\"1 {}\\n\", i));\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1857", "problem_description": "Lee tried so hard to make a good div.2 D problem to balance his recent contest, but it still doesn't feel good at all. Lee invented it so tediously slow that he managed to develop a phobia about div.2 D problem setting instead. And now he is hiding behind the bushes...Let's define a Rooted Dead Bush (RDB) of level $$$n$$$ as a rooted tree constructed as described below.A rooted dead bush of level $$$1$$$ is a single vertex. To construct an RDB of level $$$i$$$ we, at first, construct an RDB of level $$$i-1$$$, then for each vertex $$$u$$$: if $$$u$$$ has no children then we will add a single child to it; if $$$u$$$ has one child then we will add two children to it; if $$$u$$$ has more than one child, then we will skip it. Rooted Dead Bushes of level $$$1$$$, $$$2$$$ and $$$3$$$. Let's define a claw as a rooted tree with four vertices: one root vertex (called also as center) with three children. It looks like a claw: The center of the claw is the vertex with label $$$1$$$. Lee has a Rooted Dead Bush of level $$$n$$$. Initially, all vertices of his RDB are green.In one move, he can choose a claw in his RDB, if all vertices in the claw are green and all vertices of the claw are children of its center, then he colors the claw's vertices in yellow.He'd like to know the maximum number of yellow vertices he can achieve. Since the answer might be very large, print it modulo $$$10^9+7$$$.", "c_code": "int solution(int argc, char const *argv[]) {\n const long long int N = 2e6;\n const long long int T = 1e4 + 1;\n const long long int mod = 1e9 + 7;\n\n long long int a[N];\n\n long long int n;\n long long int max = 0;\n a[0] = 0;\n a[1] = 0;\n a[2] = 0;\n a[3] = 4;\n if (scanf(\"%lld\", &n) != EOF) {\n };\n long long int q[T];\n long long int i;\n for (i = 0; i < n; i++) {\n if (scanf(\"%lld\", &q[i]) != EOF) {\n }\n if (q[i] > max) {\n max = q[i];\n }\n }\n for (i = 4; i <= max; i++) {\n a[i] = 2 * a[i - 2] + a[i - 1];\n a[i] = a[i] % mod;\n if (i % 3 == 0) {\n a[i] = (a[i] + 4) % mod;\n }\n }\n for (i = 0; i < n; i++) {\n printf(\"%lld\\n\", a[q[i]]);\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 let mut xs: Vec = Vec::new();\n for _ in 0..t {\n let x: i64 = lines.next().unwrap().unwrap().parse().unwrap();\n xs.push(x);\n }\n let m = xs.iter().max().unwrap();\n let mut memo: Vec = vec![0; *m as usize + 1];\n memo[3] = 4;\n memo[4] = 4;\n memo[5] = 12;\n for i in 6..(*m as usize + 1) {\n if i % 3 == 0 {\n memo[i] = (memo[i - 2] + 4 * memo[i - 3] + 4 * memo[i - 4] + 4) % 1000000007;\n } else {\n memo[i] = (memo[i - 1] + 2 * memo[i - 2]) % 1000000007;\n }\n }\n for i in 0..t {\n println!(\"{}\", memo[xs[i] as usize]);\n }\n}", "difficulty": "easy"} {"problem_id": "1858", "problem_description": "There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).", "c_code": "int solution(void) {\n int n = 0;\n int k = 0;\n\n scanf(\"%d %d\", &n, &k);\n\n int arr[n];\n\n for (int i = 0, a = 0, b = 0; i < n; i++) {\n scanf(\"%d\", &a);\n arr[i] = a + b;\n b = arr[i];\n }\n\n int m = arr[k - 1];\n int r = 0;\n\n for (int i = 1, j = k; j < n; i++, j++) {\n int tmp = arr[j] - arr[i - 1];\n if (m > tmp) {\n r = i + 1;\n m = tmp;\n }\n }\n\n printf(\"%d\\n\", r > 0 ? r : 1);\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let input = &mut String::new();\n\n input.clear();\n stdin.read_line(input);\n\n let A: Vec = input\n .trim()\n .split(' ')\n .map(|s| s.parse().unwrap())\n .collect();\n let n = A[0];\n let k = A[1];\n\n input.clear();\n stdin.read_line(input);\n\n let h: Vec = input\n .trim()\n .split(' ')\n .map(|s| s.parse().unwrap())\n .collect();\n\n let mut C = vec![0u32; n - k + 1];\n\n C[0] = h.iter().take(k).sum();\n\n for i in 1..n - k + 1 {\n C[i] = C[i - 1] - h[i - 1] + h[i + k - 1];\n }\n\n println!(\n \"{}\",\n C.iter()\n .enumerate()\n .min_by_key(|&(_, item)| item)\n .unwrap()\n .0\n + 1\n );\n}", "difficulty": "medium"} {"problem_id": "1859", "problem_description": "You are given a multiset (i. e. a set that can contain multiple equal integers) containing $$$2n$$$ integers. Determine if you can split it into exactly $$$n$$$ pairs (i. e. each element should be in exactly one pair) so that the sum of the two elements in each pair is odd (i. e. when divided by $$$2$$$, the remainder is $$$1$$$).", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n scanf(\"%d\", &n);\n int A[2 * n];\n int sum = 0;\n int even = 0;\n int odd = 0;\n for (int i = 0; i < 2 * n; i++) {\n scanf(\"%d\", &A[i]);\n\n if (A[i] % 2 == 0) {\n even++;\n }\n if (A[i] % 2 == 1) {\n odd++;\n }\n }\n if (odd == even) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(2).step_by(2) {\n let (l, e) = l\n .unwrap()\n .trim()\n .split(' ')\n .map(|w| w.parse::().unwrap())\n .fold((0, 0), |(c, e), n| {\n if n % 2 == 0 {\n (c + 1, e + 1)\n } else {\n (c + 1, e)\n }\n });\n writeln!(o, \"{}\", if l / 2 == e { \"Yes\" } else { \"No\" }).ok();\n }\n}", "difficulty": "medium"} {"problem_id": "1860", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given two non-negative integers L and R.\nWe will choose two integers i and j such that L \\leq i < j \\leq R.\nFind the minimum possible value of (i \\times j) \\mbox{ mod } 2019.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 0 \\leq L < R \\leq 2 \\times 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
L R\n
\n
\n
\n
\n
\n

Output

Print the minimum possible value of (i \\times j) \\mbox{ mod } 2019 when i and j are chosen under the given condition.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2020 2040\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

When (i, j) = (2020, 2021), (i \\times j) \\mbox{ mod } 2019 = 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

4 5\n
\n
\n
\n
\n
\n

Sample Output 2

20\n
\n

We have only one choice: (i, j) = (4, 5).

\n
\n
", "c_code": "int solution() {\n long long L = 0;\n long long R = 0;\n long long i = 0;\n long long j = 0;\n long long ans = 0;\n long long min = -1;\n scanf(\"%lld %lld\", &L, &R);\n if (R - L >= 2019) {\n min = 0;\n } else {\n for (i = 0; L + i < R; i++) {\n for (j = i + 1; L + j <= R; j++) {\n ans = (((L + i) % 2019) * ((L + j) % 2019)) % 2019;\n\n if (ans < min || min == -1) {\n min = ans;\n }\n }\n }\n }\n printf(\"%lld\", min);\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 l: u64 = itr.next().unwrap().parse().unwrap();\n let r: u64 = itr.next().unwrap().parse().unwrap();\n\n if r - l >= 2019 {\n println!(\"0\");\n } else {\n let mut ans = 1u64 << 60;\n for i in l..r + 1 {\n for j in i + 1..r + 1 {\n ans = std::cmp::min(ans, i % 2019 * j % 2019);\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "easy"} {"problem_id": "1861", "problem_description": "This is an interactive problem.This is an easy version of the problem. The difference from the hard version is that in the easy version $$$t=1$$$ and the number of queries is limited to $$$20$$$.Polycarp is playing a computer game. In this game, an array consisting of zeros and ones is hidden. Polycarp wins if he guesses the position of the $$$k$$$-th zero from the left $$$t$$$ times.Polycarp can make no more than $$$20$$$ requests of the following type: ? $$$l$$$ $$$r$$$ — find out the sum of all elements in positions from $$$l$$$ to $$$r$$$ ($$$1 \\le l \\le r \\le n$$$) inclusive. In this (easy version) of the problem, this paragraph doesn't really make sense since $$$t=1$$$ always. To make the game more interesting, each guessed zero turns into one and the game continues on the changed array. More formally, if the position of the $$$k$$$-th zero was $$$x$$$, then after Polycarp guesses this position, the $$$x$$$-th element of the array will be replaced from $$$0$$$ to $$$1$$$. Of course, this feature affects something only for $$$t>1$$$.Help Polycarp win the game.", "c_code": "int solution() {\n int n;\n int t;\n int k;\n int l = 1;\n int r;\n int mid;\n int cnt;\n scanf(\"%d%d%d\", &n, &t, &k);\n r = n;\n while (l <= r) {\n mid = (l + r) / 2;\n printf(\"? 1 %d\\n\", mid);\n fflush(stdout);\n scanf(\"%d\", &cnt);\n if (mid - cnt >= k) {\n r = mid - 1;\n } else {\n l = mid + 1;\n }\n }\n printf(\"! %d\", l);\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(|x| x.unwrap());\n\n let s = lines.next().unwrap();\n let mut it = s.split_whitespace().map(|s| s.parse::().unwrap());\n let n = it.next().unwrap();\n let t = it.next().unwrap();\n\n 'cases: for _ in 0..t {\n let k = lines.next().unwrap().parse::().unwrap();\n\n let mut low = 0;\n let mut up = n;\n\n while low + 1 < up {\n let i = (low + up) / 2;\n\n writeln!(&mut output, \"? {} {}\", 1, i).unwrap();\n output.flush().unwrap();\n let resp = lines.next().unwrap().parse::().unwrap();\n\n let x = i - resp;\n\n if x >= k {\n up = i;\n } else {\n low = i;\n }\n\n dbg!(low, up);\n }\n\n let ans = up;\n\n writeln!(&mut output, \"! {}\", ans).unwrap();\n }\n}", "difficulty": "medium"} {"problem_id": "1862", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have an H-by-W matrix.\nLet a_{ij} be the element at the i-th row from the top and j-th column from the left.\nIn this matrix, each a_{ij} is a lowercase English letter.

\n

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A.\nHere, he wants to satisfy the following condition:

\n
    \n
  • Every row and column in A' can be read as a palindrome.
  • \n
\n

Determine whether he can create a matrix satisfying the condition.

\n
\n
\n
\n
\n

Note

A palindrome is a string that reads the same forward and backward.\nFor example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 ≤ H, W ≤ 100
  • \n
  • a_{ij} is a lowercase English letter.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
H W\na_{11}a_{12}...a_{1W}\n:\na_{H1}a_{H2}...a_{HW}\n
\n
\n
\n
\n
\n

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

3 4\naabb\naabb\naacc\n
\n
\n
\n
\n
\n

Sample Output 1

Yes\n
\n

For example, the following matrix satisfies the condition.

\n
abba\nacca\nabba\n
\n
\n
\n
\n
\n
\n

Sample Input 2

2 2\naa\nbb\n
\n
\n
\n
\n
\n

Sample Output 2

No\n
\n

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.

\n
\n
\n
\n
\n
\n

Sample Input 3

5 1\nt\nw\ne\ne\nt\n
\n
\n
\n
\n
\n

Sample Output 3

Yes\n
\n

For example, the following matrix satisfies the condition.

\n
t\ne\nw\ne\nt\n
\n
\n
\n
\n
\n
\n

Sample Input 4

2 5\nabxba\nabyba\n
\n
\n
\n
\n
\n

Sample Output 4

No\n
\n
\n
\n
\n
\n
\n

Sample Input 5

1 1\nz\n
\n
\n
\n
\n
\n

Sample Output 5

Yes\n
\n
\n
", "c_code": "int solution(void) {\n\n int h;\n int w;\n scanf(\"%d %d\", &h, &w);\n char a[h][w + 1];\n for (int i = 0; i < h; i++) {\n scanf(\"%s\", a[i]);\n }\n int alp[26];\n for (int i = 0; i < 26; i++) {\n alp[i] = 0;\n }\n for (int i = 0; i < h; i++) {\n for (int j = 0; j < w; j++) {\n alp[a[i][j] - 'a']++;\n }\n }\n int need_4 = (h / 2) * (w / 2);\n int need_2 = 0;\n if (h % 2 == 1) {\n need_2 += w / 2;\n }\n if (w % 2 == 1) {\n need_2 += h / 2;\n }\n int stock_4 = 0;\n int stock_2 = 0;\n for (int i = 0; i < 26; i++) {\n stock_4 += alp[i] / 4;\n if (alp[i] % 4 == 2) {\n stock_2++;\n }\n }\n if (stock_4 >= need_4) {\n stock_2 += (stock_4 - need_4) * 2;\n if (stock_2 >= need_2) {\n printf(\"Yes\\n\");\n } else {\n printf(\"No\\n\");\n }\n } else {\n printf(\"No\\n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let (H, W): (usize, usize) = {\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 )\n };\n let S: Vec> = (0..H)\n .map(|_| {\n let mut line: String = String::new();\n std::io::stdin().read_line(&mut line).unwrap();\n line.trim().chars().collect()\n })\n .collect();\n\n let mut c = std::collections::HashMap::new();\n for i in 0..H {\n for j in 0..W {\n *(c.entry(S[i][j]).or_insert(0)) += 1;\n }\n }\n let mut dp = [0, 0, 0, 0];\n for &v in c.values() {\n dp[v % 4] += 1;\n }\n\n let ans = if (H % 2 == 0 && W % 2 == 0 && dp[1] == 0 && dp[2] == 0 && dp[3] == 0)\n || (H % 2 == 0 && W % 2 == 1 && dp[1] == 0 && dp[3] == 0 && dp[2] <= H / 2)\n || (H % 2 == 1 && W % 2 == 0 && dp[1] == 0 && dp[3] == 0 && dp[2] <= W / 2)\n || (H % 2 == 1 && W % 2 == 1 && dp[1] + dp[3] <= 1 && dp[2] + dp[3] <= H / 2 + W / 2)\n {\n \"Yes\"\n } else {\n \"No\"\n };\n\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1863", "problem_description": "Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.Omkar currently has $$$n$$$ supports arranged in a line, the $$$i$$$-th of which has height $$$a_i$$$. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In $$$1$$$ operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add $$$1$$$ to each of their heights. Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!An array $$$b$$$ is a subsegment of an array $$$c$$$ if $$$b$$$ can be obtained from $$$c$$$ by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.An array $$$b_1, b_2, \\dots, b_n$$$ is called nondecreasing if $$$b_i\\le b_{i+1}$$$ for every $$$i$$$ from $$$1$$$ to $$$n-1$$$.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t) {\n int n;\n long long d = 0;\n long long sum = 0;\n scanf(\"%d\", &n);\n long long a[n];\n for (long long i = 0; i < n; i++) {\n scanf(\"%lld \", &a[i]);\n }\n\n for (int i = n - 1; i > 0; i--) {\n d = a[i - 1] - a[i];\n if (d > 0) {\n sum = sum + d;\n }\n }\n printf(\"%lld\\n\", sum);\n t--;\n }\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 a: 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 = 0;\n for i in 0..(n - 1) {\n if a[i] > a[i + 1] {\n ans += a[i] - a[i + 1];\n }\n }\n println!(\"{}\", ans);\n }\n}", "difficulty": "medium"} {"problem_id": "1864", "problem_description": "Ashish has a binary string $$$s$$$ of length $$$n$$$ that he wants to sort in non-decreasing order.He can perform the following operation: Choose a subsequence of any length such that its elements are in non-increasing order. Formally, choose any $$$k$$$ such that $$$1 \\leq k \\leq n$$$ and any sequence of $$$k$$$ indices $$$1 \\le i_1 \\lt i_2 \\lt \\ldots \\lt i_k \\le n$$$ such that $$$s_{i_1} \\ge s_{i_2} \\ge \\ldots \\ge s_{i_k}$$$. Reverse this subsequence in-place. Formally, swap $$$s_{i_1}$$$ with $$$s_{i_k}$$$, swap $$$s_{i_2}$$$ with $$$s_{i_{k-1}}$$$, $$$\\ldots$$$ and swap $$$s_{i_{\\lfloor k/2 \\rfloor}}$$$ with $$$s_{i_{\\lceil k/2 \\rceil + 1}}$$$ (Here $$$\\lfloor x \\rfloor$$$ denotes the largest integer not exceeding $$$x$$$, and $$$\\lceil x \\rceil$$$ denotes the smallest integer not less than $$$x$$$) Find the minimum number of operations required to sort the string in non-decreasing order. It can be proven that it is always possible to sort the given binary string in at most $$$n$$$ operations.", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n char string[n + 1];\n scanf(\"%s\", string);\n int ans[n + 1];\n\n int one = 0;\n int zero = 0;\n for (int i = 0; i < n; i++) {\n if (string[i] == '1') {\n one++;\n } else {\n zero++;\n }\n }\n int wrong = 0;\n for (int i = 0; i < zero; i++) {\n if (string[i] == '1') {\n wrong++;\n ans[wrong] = i + 1;\n }\n }\n for (int i = zero; i < n; i++) {\n if (string[i] == '0') {\n wrong++;\n ans[wrong] = i + 1;\n }\n }\n\n ans[0] = wrong;\n if (wrong == 0) {\n printf(\"0\\n\");\n continue;\n }\n printf(\"1\\n\");\n for (int i = 0; i <= wrong; i++) {\n printf(\"%d \", ans[i]);\n }\n printf(\"\\n\");\n }\n}", "rust_code": "fn solution() {\n let (i, o) = (io::stdin(), io::stdout());\n let mut o = bw::new(o.lock());\n for l in i.lock().lines().skip(2).step_by(2) {\n let (mut x, mut y) = (vec![], vec![]);\n for (j, c) in l.unwrap().chars().enumerate() {\n if c == '0' {\n x.push(j)\n } else {\n y.push(j)\n }\n }\n x.reverse();\n\n let m = x.iter().zip(y.iter()).take_while(|(x, y)| x > y).count();\n\n if m == 0 {\n writeln!(o, \"0\").ok();\n } else {\n write!(o, \"1\\n{} \", m * 2).ok();\n for n in y[..m].iter().chain(x[..m].iter().rev()) {\n write!(o, \"{} \", n + 1).ok();\n }\n writeln!(o).ok();\n }\n }\n}", "difficulty": "hard"} {"problem_id": "1865", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

We have N bulbs arranged on a number line, numbered 1 to N from left to right.\nBulb i is at coordinate i.

\n

Each bulb has a non-negative integer parameter called intensity.\nWhen there is a bulb of intensity d at coordinate x, the bulb illuminates the segment from coordinate x-d-0.5 to x+d+0.5.\nInitially, the intensity of Bulb i is A_i. We will now do the following operation K times in a row:

\n
    \n
  • For each integer i between 1 and N (inclusive), let B_i be the number of bulbs illuminating coordinate i. Then, change the intensity of each bulb i to B_i.
  • \n
\n

Find the intensity of each bulb after the K operations.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 2 \\times 10^5
  • \n
  • 1 \\leq K \\leq 2 \\times 10^5
  • \n
  • 0 \\leq A_i \\leq N
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N K\nA_1 A_2 \\ldots A_N\n
\n
\n
\n
\n
\n

Output

Print the intensity A{'}_i of each bulb i after the K operations to Standard Output in the following format:

\n
A{'}_1 A{'}_2 \\ldots A{'}_N\n
\n
\n
\n
\n
\n
\n
\n

Sample Input 1

5 1\n1 0 0 1 0\n
\n
\n
\n
\n
\n

Sample Output 1

1 2 2 1 2 \n
\n

Initially, only Bulb 1 illuminates coordinate 1, so the intensity of Bulb 1 becomes 1 after the operation.\nSimilarly, the bulbs initially illuminating coordinate 2 are Bulb 1 and 2, so the intensity of Bulb 2 becomes 2.

\n
\n
\n
\n
\n
\n

Sample Input 2

5 2\n1 0 0 1 0\n
\n
\n
\n
\n
\n

Sample Output 2

3 3 4 4 3 \n
\n
\n
", "c_code": "int solution() {\n int n;\n int k;\n scanf(\"%d%d\", &n, &k);\n int a[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int b[n];\n for (; k > 0; k--) {\n int j = 0;\n for (int i = 0; i < n; i++) {\n if (a[i] < n) {\n j++;\n }\n }\n if (j == 0) {\n break;\n }\n for (int i = 0; i < n; i++) {\n b[i] = 0;\n }\n for (int i = 0; i < n; i++) {\n if (i - a[i] >= 0) {\n b[i - a[i]]++;\n } else {\n b[0]++;\n }\n if (a[i] + i + 1 < n) {\n b[a[i] + i + 1]--;\n }\n }\n a[0] = b[0];\n for (int i = 0; i < n - 1; i++) {\n a[i + 1] = b[i + 1] + a[i];\n }\n }\n for (int i = 0; i < n; i++) {\n printf(\"%d \", a[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut is = String::new();\n stdin().read_line(&mut is).ok();\n let mut itr = is.split_whitespace().map(|e| e.parse::().unwrap());\n let n: usize = itr.next().unwrap();\n let k: usize = itr.next().unwrap();\n is.clear();\n stdin().read_line(&mut is).ok();\n let mut a = is\n .split_whitespace()\n .map(|e| e.parse::().unwrap())\n .collect::>();\n\n for _ in 0..k {\n let mut b = vec![0usize; n + 1];\n for i in 0..n {\n let l = i.saturating_sub(a[i]);\n let r = min(i + a[i] + 1, n);\n b[l] = b[l].wrapping_add(1);\n b[r] = b[r].wrapping_add(!0);\n }\n a = vec![0; n];\n for i in 0..n {\n a[i] = a[if i > 0 { i - 1 } else { 0 }].wrapping_add(b[i]);\n }\n\n if a.iter().all(|e| e == &n) {\n break;\n }\n }\n\n let mut flg = false;\n for e in a {\n if flg {\n print!(\" \");\n }\n print!(\"{}\", e);\n flg = true;\n }\n println!();\n}", "difficulty": "medium"} {"problem_id": "1866", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

Snuke received a positive integer N from Takahashi.\nA positive integer m is called a favorite number when the following condition is satisfied:

\n
    \n
  • The quotient and remainder of N divided by m are equal, that is, \\lfloor \\frac{N}{m} \\rfloor = N \\bmod m holds.
  • \n
\n

Find all favorite numbers and print the sum of those.

\n
\n
\n
\n
\n

Constraints

    \n
  • All values in input are integers.
  • \n
  • 1 \\leq N \\leq 10^{12}
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the answer.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

8\n
\n
\n
\n
\n
\n

Sample Output 1

10\n
\n

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

\n
\n
\n
\n
\n
\n

Sample Input 2

1000000000000\n
\n
\n
\n
\n
\n

Sample Output 2

2499686339916\n
\n

Watch out for overflow.

\n
\n
", "c_code": "int solution() {\n long long int n = 0;\n long long int sum = 0;\n scanf(\"%lld\\n\", &n);\n for (long long int i = 1; i * (i + 1) + 1 <= n; i++) {\n if ((n - i) % i == 0) {\n sum += (n - i) / i;\n }\n }\n printf(\"%lld\\n\", sum);\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\n let mut ans = 0;\n let mut i = 1;\n while i * i <= n {\n if n.is_multiple_of(i) {\n let d = n / i;\n if i > 1 && d == n % (i - 1) {\n ans += i - 1;\n }\n\n if n / i != i {\n let d = i;\n if n / i > 1 && d == n % (n / i - 1) {\n ans += n / i - 1;\n }\n }\n }\n i += 1;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "medium"} {"problem_id": "1867", "problem_description": "

Prime Numbers

\n\n

\nA 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\n

\nWrite a program which reads a list of N integers and prints the number of prime numbers in the list.\n

\n\n

Input

\n\n

\nThe first line contains an integer N, the number of elements in the list.\n

\n\n

\nN numbers are given in the following lines.\n

\n\n

Output

\n\n

\nPrint the number of prime numbers in the given list.\n

\n\n

Constraints

\n\n

\n1 ≤ N ≤ 10000\n

\n\n

\n2 ≤ an element of the list ≤ 108\n

\n\n

Sample Input 1

\n
\n5\n2\n3\n4\n5\n6\n
\n

Sample Output 1

\n
\n3\n
\n\n

Sample Input 2

\n
\n11\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n
\n

Sample Output 2

\n
\n4\n
", "c_code": "int solution() {\n int N = 0;\n int number = 0;\n int i = 0;\n int divide = 0;\n int count = 0;\n int flag = 0;\n\n scanf(\"%d\", &N);\n for (i = 0; i < N; i++) {\n scanf(\"%d\", &number);\n divide = 2;\n flag = 0;\n while (divide * divide <= number) {\n if ((number % divide) == 0) {\n flag = 1;\n break;\n }\n divide++;\n }\n\n if (flag != 1) {\n count++;\n }\n }\n\n printf(\"%d\\n\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n let stdin = io::stdin();\n\n let _ = stdin.read_line(&mut s);\n\n let n: usize = s.trim().parse().unwrap();\n\n s.clear();\n\n let nums = stdin\n .lock()\n .lines()\n .take(n)\n .map(|s| s.ok().unwrap().trim().parse().unwrap_or(0));\n\n let answer = nums\n .filter(|&x| {\n let m = f64::sqrt(x as f64).ceil() as u64;\n\n x == 2 || (2..(m + 1)).all(|y| x % y != 0)\n })\n .count();\n\n println!(\"{}\", answer);\n}", "difficulty": "medium"} {"problem_id": "1868", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

We have a string s consisting of lowercase English letters.\nSnuke can perform the following operation repeatedly:

\n
    \n
  • Insert a letter x to any position in s of his choice, including the beginning and end of s.
  • \n
\n

Snuke's objective is to turn s into a palindrome.\nDetermine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

\n
\n
\n
\n
\n

Notes

A palindrome is a string that reads the same forward and backward.\nFor example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq |s| \\leq 10^5
  • \n
  • s consists of lowercase English letters.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
s\n
\n
\n
\n
\n
\n

Output

If the objective is achievable, print the number of operations required.\nIf it is not, print -1 instead.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

xabxa\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

One solution is as follows (newly inserted x are shown in bold):

\n

xabxa → xaxbxa → xaxbxax

\n
\n
\n
\n
\n
\n

Sample Input 2

ab\n
\n
\n
\n
\n
\n

Sample Output 2

-1\n
\n

No sequence of operations can turn s into a palindrome.

\n
\n
\n
\n
\n
\n

Sample Input 3

a\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n

s is a palindrome already at the beginning.

\n
\n
\n
\n
\n
\n

Sample Input 4

oxxx\n
\n
\n
\n
\n
\n

Sample Output 4

3\n
\n

One solution is as follows:

\n

oxxx → xoxxx → xxoxxx → xxxoxxx

\n
\n
", "c_code": "int solution() {\n int ans = 0;\n int flag = 0;\n int first = 0;\n int last = 99999;\n char s[100001];\n scanf(\"%s\", s);\n\n last = strlen(s) - 1;\n while (flag == 0) {\n if (s[first] != s[last] && s[first] != 'x' && s[last] != 'x') {\n flag++;\n } else if (s[first] == s[last]) {\n first++;\n last--;\n } else if (s[first] == 'x' && s[last] != 'x') {\n first++;\n ans++;\n } else if (s[first] != 'x' && s[last] == 'x') {\n last--;\n ans++;\n }\n\n if (first >= last) {\n printf(\"%d\", ans);\n return 0;\n }\n }\n printf(\"-1\");\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 s: Vec = itr.next().unwrap().chars().collect();\n\n let mut l = 0;\n let mut r = s.len() - 1;\n let mut ans = 0;\n while r > l {\n if s[l] == s[r] {\n l += 1;\n r -= 1;\n } else if s[l] == 'x' {\n l += 1;\n ans += 1;\n } else if s[r] == 'x' {\n r -= 1;\n ans += 1;\n } else {\n println!(\"-1\");\n return;\n }\n }\n println!(\"{}\", ans);\n}", "difficulty": "easy"} {"problem_id": "1869", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \\leq i \\leq N) is given to you as a character c_i; R stands for red and W stands for white.

\n

You can do the following two kinds of operations any number of times in any order:

\n
    \n
  • Choose two stones (not necessarily adjacent) and swap them.
  • \n
  • Choose one stone and change its color (from red to white and vice versa).
  • \n
\n

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

\n
\n
\n
\n
\n

Constraints

    \n
  • 2 \\leq N \\leq 200000
  • \n
  • c_i is R or W.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\nc_{1}c_{2}...c_{N}\n
\n
\n
\n
\n
\n

Output

Print an integer representing the minimum number of operations needed.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\nWWRR\n
\n
\n
\n
\n
\n

Sample Output 1

2\n
\n

For example, the two operations below will achieve the objective.

\n
    \n
  • Swap the 1-st and 3-rd stones from the left, resulting in RWWR.
  • \n
  • Change the color of the 4-th stone from the left, resulting in RWWW.
  • \n
\n
\n
\n
\n
\n
\n

Sample Input 2

2\nRR\n
\n
\n
\n
\n
\n

Sample Output 2

0\n
\n

It can be the case that no operation is needed.

\n
\n
\n
\n
\n
\n

Sample Input 3

8\nWRWWRWRR\n
\n
\n
\n
\n
\n

Sample Output 3

3\n
\n
\n
", "c_code": "int solution(void) {\n int n;\n scanf(\"%d\", &n);\n char s[n + 1];\n scanf(\"%s\", s);\n int r = 0;\n int w = 0;\n int ans[n];\n for (int i = 0; i < n; i++) {\n ans[i] = w;\n (*(s[i] == 'R' ? &r : &w))++;\n }\n printf(\"%d\\n\", r < n ? ans[r] : 0);\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n let n: usize = s.trim().parse().ok().unwrap();\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n\n let (mut r, mut w) = (VecDeque::new(), VecDeque::new());\n r.push_back(0);\n for (i, c) in s.trim().chars().enumerate() {\n match c {\n 'R' => r.push_back(i),\n 'W' => w.push_front(i),\n _ => exit(1),\n };\n }\n w.push_front(n);\n let c = r\n .iter()\n .rev()\n .zip(w.iter().rev())\n .filter(|(x, y)| x > y)\n .count();\n println!(\"{}\", c);\n}", "difficulty": "easy"} {"problem_id": "1870", "problem_description": "Jump search", "c_code": "int solution(const int *arr, int x, size_t n) {\n if (n == 0)\n return -1;\n\n size_t step = (size_t)floor(sqrt((double)n));\n size_t prev = 0;\n size_t current_limit;\n\n while (1) {\n current_limit = (step < n) ? step : n;\n\n if (arr[current_limit - 1] >= x) {\n break;\n }\n\n prev = step;\n step += (size_t)floor(sqrt((double)n));\n\n if (prev >= n) {\n return -1;\n }\n }\n\n current_limit = (step < n) ? step : n;\n while (arr[prev] < x) {\n prev++;\n if (prev == current_limit) {\n return -1;\n }\n }\n\n if (arr[prev] == x) {\n return (int)prev;\n }\n\n return -1;\n}\n", "rust_code": "fn solution(item: &T, arr: &[T]) -> Option {\n let len = arr.len();\n if len == 0 {\n return None;\n }\n let mut step = (len as f64).sqrt() as usize;\n let mut prev = 0;\n\n while &arr[min(len, step) - 1] < item {\n prev = step;\n step += (len as f64).sqrt() as usize;\n if prev >= len {\n return None;\n }\n }\n while &arr[prev] < item {\n prev += 1;\n }\n if &arr[prev] == item {\n return Some(prev);\n }\n None\n}\n", "difficulty": "medium"} {"problem_id": "1871", "problem_description": "Suppose you are living with two cats: A and B. There are $$$n$$$ napping spots where both cats usually sleep.Your cats like to sleep and also like all these spots, so they change napping spot each hour cyclically: Cat A changes its napping place in order: $$$n, n - 1, n - 2, \\dots, 3, 2, 1, n, n - 1, \\dots$$$ In other words, at the first hour it's on the spot $$$n$$$ and then goes in decreasing order cyclically; Cat B changes its napping place in order: $$$1, 2, 3, \\dots, n - 1, n, 1, 2, \\dots$$$ In other words, at the first hour it's on the spot $$$1$$$ and then goes in increasing order cyclically. The cat B is much younger, so they have a strict hierarchy: A and B don't lie together. In other words, if both cats'd like to go in spot $$$x$$$ then the A takes this place and B moves to the next place in its order (if $$$x < n$$$ then to $$$x + 1$$$, but if $$$x = n$$$ then to $$$1$$$). Cat B follows his order, so it won't return to the skipped spot $$$x$$$ after A frees it, but will move to the spot $$$x + 2$$$ and so on.Calculate, where cat B will be at hour $$$k$$$?", "c_code": "int solution() {\n int j = 0;\n int n;\n int k;\n int t;\n scanf(\"%d\", &t);\n int ans[t];\n while (j++ < t) {\n scanf(\"%d %d\", &n, &k);\n if (n % 2 == 0) {\n ans[j] = (k - 1) % n + 1;\n } else {\n ans[j] = ((k - 1) % n + (k - 1) / (n / 2)) % n + 1;\n }\n }\n j = 0;\n while (j++ < t) {\n printf(\"%d\\n\", ans[j]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).unwrap();\n let t: usize = t.trim().parse().unwrap();\n\n let mut n_k = String::new();\n for _t in 0..t {\n n_k.clear();\n io::stdin().read_line(&mut n_k).unwrap();\n let mut n_k = n_k.split_whitespace();\n let n: u64 = n_k.next().unwrap().parse().unwrap();\n let k: u64 = n_k.next().unwrap().parse().unwrap();\n let mut b_position: u64;\n if n.is_multiple_of(2) {\n b_position = k % n;\n } else {\n let times_crossed = (k - 1) / (n / 2);\n b_position = (k + times_crossed) % n;\n }\n\n if b_position == 0 {\n b_position = n;\n }\n\n println!(\"{}\", b_position);\n }\n}", "difficulty": "medium"} {"problem_id": "1872", "problem_description": "

Drawing Lots

\n\n

\nLet's play Amidakuji. \n

\n\n

\nIn the following example, there are five vertical lines and four horizontal lines. The horizontal lines can intersect (jump across) the vertical lines.\n

\n\n
\n\n
\n
\n\n

\nIn the starting points (top of the figure), numbers are assigned to vertical lines in ascending order from left to right. At the first step, 2 and 4 are swaped by the first horizontal line which connects second and fourth vertical lines (we call this operation (2, 4)). Likewise, we perform (3, 5), (1, 2) and (3, 4), then obtain \"4 1 2 5 3\" in the bottom.\n

\n\n

\nYour task is to write a program which reads the number of vertical lines w and configurations of horizontal lines and prints the final state of the Amidakuji. In the starting pints, numbers 1, 2, 3, ..., w are assigne to the vertical lines from left to right.\n

\n\n

Input

\n\n
\nw\nn\na1,b1\na2,b2\n.\n.\nan,bn\n
\n\n

\nw (w ≤ 30) is the number of vertical lines. n (n ≤ 30) is the number of horizontal lines. A pair of two integers ai and bi delimited by a comma represents the i-th horizontal line.\n

\n\n

Output

\n\n

\nThe number which should be under the 1st (leftmost) vertical line
\nThe number which should be under the 2nd vertical line
\n:
\nThe number which should be under the w-th vertical line
\n

\n\n

Sample Input

\n\n
\n5\n4\n2,4\n3,5\n1,2\n3,4\n
\n\n

Output for the Sample Input

\n\n
\n4\n1\n2\n5\n3\n
\n\n", "c_code": "int solution() {\n int i = 0;\n int count = 0;\n int tate = 0;\n int yoko = 0;\n int change = 0;\n int in_out_box[30 + 1];\n int num1 = 0;\n int num1_index = 0;\n int num2 = 0;\n int num2_index = 0;\n char str[10];\n\n scanf(\"%d\", &tate);\n\n fgets(str, (sizeof(str)), stdin);\n for (i = 0; i < tate; i++) {\n in_out_box[i] = i + 1;\n }\n\n scanf(\"%d\", &yoko);\n\n fgets(str, (sizeof(str)), stdin);\n\n for (count = 0; count < yoko; count++) {\n scanf(\"%d,%d\", &num1, &num2);\n for (i = 0; i < tate; i++) {\n if (i + 1 == num1) {\n change = in_out_box[i];\n num1_index = i;\n }\n if (i + 1 == num2) {\n in_out_box[num1_index] = in_out_box[i];\n in_out_box[i] = change;\n }\n }\n }\n\n for (i = 0; i < tate; i++) {\n printf(\"%d\\n\", in_out_box[i]);\n }\n return 0;\n}", "rust_code": "fn solution() {\n let mut w: u32 = 0;\n for i in 0..2 {\n let mut buf = String::new();\n stdin().read_line(&mut buf).unwrap();\n match i {\n 0 => w = buf.trim().parse().unwrap(),\n _ => break,\n }\n }\n let reader = BufReader::new(stdin());\n let mut hlines = vec![];\n for line in reader.lines() {\n let l: Vec = line\n .unwrap()\n .trim()\n .split(',')\n .filter_map(|x| x.parse::().ok())\n .collect();\n hlines.push((l[0], l[1]));\n }\n\n let mut answer = Vec::with_capacity((w + 1) as usize);\n for i in 1..w + 2 {\n answer.push(i);\n }\n for i in 1..w + 1 {\n let mut current = i;\n for hline in &hlines {\n if hline.0 == current {\n current = hline.1;\n } else if hline.1 == current {\n current = hline.0;\n }\n }\n answer[(current - 1) as usize] = i;\n }\n for i in 0..w {\n println!(\"{}\", answer[i as usize]);\n }\n}", "difficulty": "hard"} {"problem_id": "1873", "problem_description": "You are given a board of size $$$2 \\times n$$$ ($$$2$$$ rows, $$$n$$$ columns). Some cells of the board contain chips. The chip is represented as '*', and an empty space is represented as '.'. It is guaranteed that there is at least one chip on the board.In one move, you can choose any chip and move it to any adjacent (by side) cell of the board (if this cell is inside the board). It means that if the chip is in the first row, you can move it left, right or down (but it shouldn't leave the board). Same, if the chip is in the second row, you can move it left, right or up.If the chip moves to the cell with another chip, the chip in the destination cell disappears (i. e. our chip captures it).Your task is to calculate the minimum number of moves required to leave exactly one chip on the board.You have to answer $$$t$$$ independent test cases.", "c_code": "int solution() {\n int t = 0;\n int n = 0;\n char s[2][200001] = {};\n\n int res = 0;\n\n int visited[2][200000] = {};\n int rchip[2][200000] = {};\n\n res = scanf(\"%d\", &t);\n\n while (t > 0) {\n int cnt[2] = {};\n int min = 0;\n int ans = 0;\n res = scanf(\"%d\", &n);\n res = scanf(\"%s\", s[0]);\n res = scanf(\"%s\", s[1]);\n min = n;\n for (int i = 0; i < 2; i++) {\n int tmp_min = n;\n for (int j = n - 1; j >= 0; j--) {\n rchip[i][j] = tmp_min;\n if (s[i][j] == '*') {\n cnt[i]++;\n if (j < tmp_min) {\n tmp_min = j;\n }\n }\n visited[i][j] = 0;\n }\n if (tmp_min < min) {\n min = tmp_min;\n }\n }\n ans = 2 * n;\n for (int i = 0; i < 2; i++) {\n if (s[i][min] == '*') {\n int ci = i;\n int cj = min;\n int tmp_cnt = 1;\n int tmp_ans = 0;\n while (tmp_cnt < cnt[0] + cnt[1]) {\n visited[ci][cj] = i + 1;\n if (visited[1 - ci][cj] < i + 1 && s[1 - ci][cj] == '*') {\n if (rchip[1 - ci][cj] < rchip[ci][cj]) {\n ci = 1 - ci;\n } else {\n visited[1 - ci][cj] = i + 1;\n if (s[ci][cj] != '*') {\n tmp_cnt++;\n }\n }\n } else {\n cj++;\n }\n tmp_ans++;\n if (s[ci][cj] == '*') {\n tmp_cnt++;\n }\n }\n if (tmp_ans < ans) {\n ans = tmp_ans;\n }\n }\n }\n printf(\"%d\\n\", ans);\n t--;\n }\n\n return 0;\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let stdin = stdin.lock();\n let mut lines = stdin.lines();\n lines.next();\n while let (Some(_), Some(Ok(first_line)), Some(Ok(second_line))) =\n (lines.next(), lines.next(), lines.next())\n {\n let (mut a, mut b) = (false, false);\n let mut count = 0;\n let mut prospective_count = 0;\n let iterator = first_line.chars().zip(second_line.chars());\n iterator\n .skip_while(|&(c1, c2)| c1 == '.' && c2 == '.')\n .for_each(|(c1, c2)| match (c1, c2) {\n ('.', '.') => {\n prospective_count += 1;\n }\n ('.', '*') => {\n if b {\n count += 1;\n\n a = false;\n b = true;\n } else if a {\n count += 2;\n\n a = true;\n b = true;\n } else {\n a = false;\n b = true;\n }\n count += prospective_count;\n prospective_count = 0;\n }\n ('*', '.') => {\n if a {\n count += 1;\n\n a = true;\n b = false;\n } else if b {\n count += 2;\n\n a = true;\n b = true;\n } else {\n a = true;\n b = false;\n }\n count += prospective_count;\n prospective_count = 0;\n }\n ('*', '*') => {\n if a || b {\n count += 2;\n\n a = true;\n b = true;\n } else {\n count += 1;\n\n a = true;\n b = true;\n }\n count += prospective_count;\n prospective_count = 0;\n }\n _ => panic!(\"invalid input\"),\n });\n println!(\"{}\", count);\n }\n}", "difficulty": "medium"} {"problem_id": "1874", "problem_description": "Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer $$$n$$$, he encrypts it in the following way: he picks three integers $$$a$$$, $$$b$$$ and $$$c$$$ such that $$$l \\leq a,b,c \\leq r$$$, and then he computes the encrypted value $$$m = n \\cdot a + b - c$$$.Unfortunately, an adversary intercepted the values $$$l$$$, $$$r$$$ and $$$m$$$. Is it possible to recover the original values of $$$a$$$, $$$b$$$ and $$$c$$$ from this information? More formally, you are asked to find any values of $$$a$$$, $$$b$$$ and $$$c$$$ such that $$$a$$$, $$$b$$$ and $$$c$$$ are integers, $$$l \\leq a, b, c \\leq r$$$, there exists a strictly positive integer $$$n$$$, such that $$$n \\cdot a + b - c = m$$$.", "c_code": "int solution() {\n\n int t;\n long long l;\n long long r;\n long long m;\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%lld %lld %lld\", &l, &r, &m);\n long long min = m + l - r;\n long long max = m - l + r;\n\n for (long long a = l; a <= r; a++) {\n if ((max / a) * a >= min && (max / a > 0)) {\n\n long long tmp = m - ((max / a) * a);\n long long b;\n long long c;\n if (tmp > 0) {\n b = r;\n c = b - tmp;\n } else {\n c = r;\n b = tmp + c;\n }\n printf(\"%lld %lld %lld\\n\", a, b, c);\n break;\n }\n }\n }\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 (l, r, m): (u64, u64, u64) = {\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\n for a in l..=r {\n let p = m % a;\n let q = a - p;\n\n if m >= a && p <= r - l {\n let b = r;\n let c = r - p;\n println!(\"{} {} {}\", a, b, c);\n break;\n } else if q <= r - l {\n let b = l;\n let c = l + q;\n println!(\"{} {} {}\", a, b, c);\n break;\n }\n }\n }\n}", "difficulty": "easy"} {"problem_id": "1875", "problem_description": "Kristina has two arrays $$$a$$$ and $$$b$$$, each containing $$$n$$$ non-negative integers. She can perform the following operation on array $$$a$$$ any number of times: apply a decrement to each non-zero element of the array, that is, replace the value of each element $$$a_i$$$ such that $$$a_i > 0$$$ with the value $$$a_i - 1$$$ ($$$1 \\le i \\le n$$$). If $$$a_i$$$ was $$$0$$$, its value does not change. Determine whether Kristina can get an array $$$b$$$ from an array $$$a$$$ in some number of operations (probably zero). In other words, can she make $$$a_i = b_i$$$ after some number of operations for each $$$1 \\le i \\le n$$$?For example, let $$$n = 4$$$, $$$a = [3, 5, 4, 1]$$$ and $$$b = [1, 3, 2, 0]$$$. In this case, she can apply the operation twice: after the first application of the operation she gets $$$a = [2, 4, 3, 0]$$$; after the second use of the operation she gets $$$a = [1, 3, 2, 0]$$$. Thus, in two operations, she can get an array $$$b$$$ from an array $$$a$$$.", "c_code": "int solution() {\n int a[50000];\n int b[50000];\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &a[i]);\n }\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &b[i]);\n }\n int max = 0;\n for (int i = 0; i < n; i++) {\n int c = a[i] - b[i];\n max = max > c ? max : c;\n }\n int flag = 0;\n for (int i = 0; i < n; i++) {\n if (!flag && a[i] - b[i] != max && b[i] != 0) {\n printf(\"NO\\n\");\n flag = 1;\n }\n }\n if (!flag) {\n printf(\"YES\\n\");\n }\n }\n}", "rust_code": "fn solution() {\n let mut num = String::new();\n io::stdin().read_line(&mut num).unwrap();\n let num = num.trim().parse::().unwrap();\n 'main: for _ in 0..num {\n let mut _ln: String = String::new();\n io::stdin().read_line(&mut _ln).unwrap();\n let ln: usize = _ln.trim().parse::().unwrap() as usize;\n let mut ai: String = String::new();\n io::stdin().read_line(&mut ai).unwrap();\n let mut bi: String = String::new();\n io::stdin().read_line(&mut bi).unwrap();\n let a: Vec = ai\n .split_whitespace()\n .map(|num| num.parse::().unwrap())\n .collect();\n let b: Vec = bi\n .split_whitespace()\n .map(|num| num.parse::().unwrap())\n .collect();\n let mut delta: u128 = 0;\n let mut flag: bool = false;\n for i in 0..ln {\n if a[i] < b[i] {\n println!(\"NO\");\n continue 'main;\n }\n let local_delta: u128 = a[i] - b[i];\n if b[i] == 0 {\n if flag {\n if local_delta > delta {\n println!(\"NO\");\n continue 'main;\n }\n } else {\n delta = max(delta, local_delta);\n }\n } else {\n if !flag {\n flag = true;\n if local_delta < delta {\n println!(\"NO\");\n continue 'main;\n }\n delta = local_delta;\n } else {\n if local_delta != delta {\n println!(\"NO\");\n continue 'main;\n }\n }\n }\n }\n println!(\"YES\");\n }\n}", "difficulty": "medium"} {"problem_id": "1876", "problem_description": "You have an axis-aligned rectangle room with width $$$W$$$ and height $$$H$$$, so the lower left corner is in point $$$(0, 0)$$$ and the upper right corner is in $$$(W, H)$$$.There is a rectangular table standing in this room. The sides of the table are parallel to the walls, the lower left corner is in $$$(x_1, y_1)$$$, and the upper right corner in $$$(x_2, y_2)$$$.You want to place another rectangular table in this room with width $$$w$$$ and height $$$h$$$ with the width of the table parallel to the width of the room.The problem is that sometimes there is not enough space to place the second table without intersecting with the first one (there are no problems with tables touching, though).You can't rotate any of the tables, but you can move the first table inside the room. Example of how you may move the first table. What is the minimum distance you should move the first table to free enough space for the second one?", "c_code": "int solution() {\n int test;\n scanf(\"%d\", &test);\n float ans[test];\n for (int i = 0; i < test; i++) {\n int W;\n int H;\n int x1;\n int y1;\n int x2;\n int y2;\n int w;\n int h;\n scanf(\"%d%d%d%d%d%d%d%d\", &W, &H, &x1, &y1, &x2, &y2, &w, &h);\n ans[i] = 100000000;\n if (W >= w + x2 - x1) {\n if (w - x1 <= 0 || x2 - W + w <= 0) {\n ans[i] = 0;\n continue;\n }\n if (ans[i] > w - x1) {\n ans[i] = w - x1;\n }\n if (ans[i] > x2 - W + w) {\n ans[i] = x2 - W + w;\n }\n }\n if (H >= h + y2 - y1) {\n if (h - y1 <= 0 || y2 - H + h <= 0) {\n ans[i] = 0;\n continue;\n }\n if (ans[i] > h - y1) {\n ans[i] = h - y1;\n }\n if (ans[i] > y2 - H + h) {\n ans[i] = y2 - H + h;\n }\n }\n if (W < w + x2 - x1 && H < h + y2 - y1) {\n ans[i] = -1;\n }\n }\n for (int i = 0; i < test; i++) {\n if (ans[i] == -1) {\n printf(\"%.0f\\n\", ans[i]);\n continue;\n }\n printf(\"%.9f\\n\", ans[i]);\n }\n}", "rust_code": "fn solution() {\n let mut t = String::new();\n io::stdin().read_line(&mut t).unwrap();\n let t: usize = t.trim().parse().unwrap();\n\n let mut line = String::new();\n for _t in 0..t {\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let mut rw_rh = line.split_whitespace();\n let room_width: i64 = rw_rh.next().unwrap().parse().unwrap();\n let room_height: i64 = rw_rh.next().unwrap().parse().unwrap();\n\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let mut ft = line.split_whitespace();\n let left_x: i64 = ft.next().unwrap().parse().unwrap();\n let bottom_y: i64 = ft.next().unwrap().parse().unwrap();\n let right_x: i64 = ft.next().unwrap().parse().unwrap();\n let top_y: i64 = ft.next().unwrap().parse().unwrap();\n let first_table_height = top_y - bottom_y;\n let first_table_width = right_x - left_x;\n\n line.clear();\n io::stdin().read_line(&mut line).unwrap();\n let mut st = line.split_whitespace();\n let table_width: i64 = st.next().unwrap().parse().unwrap();\n let table_height: i64 = st.next().unwrap().parse().unwrap();\n\n let mut min_distance: i64 = i64::MAX;\n let mut calc_min_distance = |dist| {\n if dist <= 0 {\n min_distance = 0;\n } else {\n min_distance = std::cmp::min(min_distance, dist);\n }\n };\n\n if table_height + first_table_height <= room_height {\n {\n let max_y = room_height - table_height;\n let dist = top_y - max_y;\n calc_min_distance(dist);\n }\n\n {\n let min_y = table_height;\n let dist = min_y - bottom_y;\n calc_min_distance(dist);\n }\n }\n\n if table_width + first_table_width <= room_width {\n {\n let max_x = room_width - table_width;\n let dist = right_x - max_x;\n calc_min_distance(dist);\n }\n\n {\n let min_x = table_width;\n let dist = min_x - left_x;\n calc_min_distance(dist);\n }\n }\n\n if min_distance == i64::MAX {\n println!(\"-1\");\n } else {\n println!(\"{}\", min_distance);\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1877", "problem_description": "

A/B Problem

\n\n

\nWrite a program which reads two integers a and b, and calculates the following values:\n

\n\n
    \n
  • a ÷ b: d (in integer)
  • \n
  • remainder of a ÷ b: r (in integer)
  • \n
  • a ÷ b: f (in real number)
  • \n\n
\n\n\n

Input

\n\n

\nTwo integers a and b are given in a line.\n

\n\n

Output

\n\n

\nPrint d, r and f separated by a space in a line. For f, the output should not contain an absolute error greater than 10-5.\n

\n\n

Constraints

\n\n
    \n
  • 1 ≤ a, b ≤ 109
  • \n
\n\n

Sample Input 1

\n
\n3 2\n
\n

Sample Output 1

\n
\n1 1 1.50000\n
", "c_code": "int solution() {\n double x;\n double y;\n scanf(\"%lf %lf\", &x, &y);\n printf(\"%.0lf %.0lf %.5lf\\n\", floor(x / y), x - (y * (int)(x / y)), x / y);\n return 0;\n}", "rust_code": "fn solution() {\n let scan = std::io::stdin();\n let mut line = String::new();\n let _ = scan.read_line(&mut line);\n let vec: Vec<&str> = line.split_whitespace().collect();\n let a: u32 = vec[0].parse().unwrap_or(0);\n let b: u32 = vec[1].parse().unwrap_or(0);\n println!(\"{} {} {:.5}\", &(a / b), &(a % b), &(a as f64 / b as f64));\n}", "difficulty": "medium"} {"problem_id": "1878", "problem_description": "You are given an array $$$a$$$ consisting of $$$n$$$ ($$$n \\ge 3$$$) positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array $$$[4, 11, 4, 4]$$$ all numbers except one are equal to $$$4$$$).Print the index of the element that does not equal others. The numbers in the array are numbered from one.", "c_code": "int solution() {\n int t = 0;\n scanf(\"%d\", &t);\n while (t--) {\n int n = 0;\n int k = 0;\n scanf(\"%d\", &n);\n int a[n];\n scanf(\"%d\", &a[0]);\n for (int i = 1; i < n; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] != a[i - 1]) {\n k = i;\n }\n }\n if (a[n - 1] != a[n - 2] && a[n - 2] == a[n - 3]) {\n k = k + 1;\n }\n printf(\"%d\\n\", k);\n }\n return 0;\n}", "rust_code": "fn solution() -> Result<(), Box> {\n let stdin = std::io::stdin();\n let mut input = stdin.lock();\n\n let stdout = std::io::stdout();\n let mut output = stdout.lock();\n\n let mut buffer = String::with_capacity(32);\n\n input.read_line(&mut buffer)?;\n let tests = buffer.trim().parse::()?;\n\n for _ in 0..tests {\n buffer.clear();\n input.read_line(&mut buffer)?;\n\n buffer.clear();\n input.read_line(&mut buffer)?;\n\n let (mut c1, mut i1, mut v1) = (0_i32, 0, \"\");\n let (mut c2, mut i2, mut v2) = (0_i32, 0, \"\");\n\n for (idx, n) in buffer.split_ascii_whitespace().enumerate() {\n if n == v1 {\n c1 += 1;\n } else if n == v2 {\n c2 += 1;\n } else if c1 == 0 {\n c1 = 1;\n i1 = idx;\n v1 = n;\n } else if c2 == 0 {\n c2 = 1;\n i2 = idx;\n v2 = n;\n } else {\n c1 -= 1;\n c2 -= 1;\n }\n\n if c1 != 0 && c2 != 0 && c1.sub(c2).abs() >= 1 {\n let to_print = if c1 > c2 { i2 } else { i1 } + 1;\n writeln!(output, \"{}\", to_print)?;\n break;\n }\n }\n }\n\n Ok(())\n}", "difficulty": "hard"} {"problem_id": "1879", "problem_description": "\n

Score : 300 points

\n
\n
\n

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).\nYour objective is to remove some of the elements in a so that a will be a good sequence.

\n

Here, an sequence b is a good sequence when the following condition holds true:

\n
    \n
  • For each element x in b, the value x occurs exactly x times in b.
  • \n
\n

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

\n

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^5
  • \n
  • a_i is an integer.
  • \n
  • 1 \\leq a_i \\leq 10^9
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\na_1 a_2 ... a_N\n
\n
\n
\n
\n
\n

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n3 3 3 3\n
\n
\n
\n
\n
\n

Sample Output 1

1\n
\n

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

\n
\n
\n
\n
\n
\n

Sample Input 2

5\n2 4 1 4 2\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

\n
\n
\n
\n
\n
\n

Sample Input 3

6\n1 2 2 3 3 3\n
\n
\n
\n
\n
\n

Sample Output 3

0\n
\n
\n
\n
\n
\n
\n

Sample Input 4

1\n1000000000\n
\n
\n
\n
\n
\n

Sample Output 4

1\n
\n

Remove one occurrence of 10^9. Then, () is a good sequence.

\n
\n
\n
\n
\n
\n

Sample Input 5

8\n2 7 1 8 2 8 1 8\n
\n
\n
\n
\n
\n

Sample Output 5

5\n
\n
\n
", "c_code": "int solution() {\n int N;\n int p = 0;\n scanf(\"%d\", &N);\n int a[N];\n int b[N];\n for (int i = 0; i <= N; i++) {\n b[i] = 0;\n }\n for (int i = 0; i < N; i++) {\n scanf(\"%d\", &a[i]);\n if (a[i] > N) {\n p++;\n } else {\n b[a[i]]++;\n }\n }\n for (int i = 1; i <= N; i++) {\n if (b[i] != i && b[i] != 0) {\n if (b[i] < i) {\n p += b[i];\n }\n if (b[i] > i) {\n p += b[i] - i;\n }\n }\n }\n printf(\"%d\\n\", p);\n return 0;\n}", "rust_code": "fn solution() {\n let si = io::stdin();\n let mut si = si.lock();\n si.read_until(b'\\n', &mut Vec::new()).unwrap();\n println!(\n \"{}\",\n si.split(b' ')\n .map(|r| String::from_utf8(r.unwrap())\n .unwrap()\n .trim_end()\n .parse::()\n .unwrap())\n .fold(HashMap::new(), |mut map, a| {\n *map.entry(a).or_insert(0) += 1;\n map\n })\n .iter()\n .fold(0, |acc, (&a, &n)| acc + if n < a { n } else { n - a })\n );\n}", "difficulty": "medium"} {"problem_id": "1880", "problem_description": "\n

Score : 500 points

\n
\n
\n

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

\n

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

\n

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

\n

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

\n

Since the answer can be tremendous, print the number modulo 10^9+7.

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N, M \\leq 2 \\times 10^3
  • \n
  • The length of S is N.
  • \n
  • The length of T is M.
  • \n
  • 1 \\leq S_i, T_i \\leq 10^5
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N M\nS_1 S_2 ... S_{N-1} S_{N}\nT_1 T_2 ... T_{M-1} T_{M}\n
\n
\n
\n
\n
\n

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

2 2\n1 3\n3 1\n
\n
\n
\n
\n
\n

Sample Output 1

3\n
\n

S has four subsequences: (), (1), (3), (1, 3).

\n

T has four subsequences: (), (3), (1), (3, 1).

\n

There are 1 \\times 1 pair of subsequences in which the subsequences are both (), 1 \\times 1 pair of subsequences in which the subsequences are both (1), and 1 \\times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

\n
\n
\n
\n
\n
\n

Sample Input 2

2 2\n1 1\n1 1\n
\n
\n
\n
\n
\n

Sample Output 2

6\n
\n

S has four subsequences: (), (1), (1), (1, 1).

\n

T has four subsequences: (), (1), (1), (1, 1).

\n

There are 1 \\times 1 pair of subsequences in which the subsequences are both (), 2 \\times 2 pairs of subsequences in which the subsequences are both (1), and 1 \\times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.\nNote again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

\n
\n
\n
\n
\n
\n

Sample Input 3

4 4\n3 4 5 6\n3 4 5 6\n
\n
\n
\n
\n
\n

Sample Output 3

16\n
\n
\n
\n
\n
\n
\n

Sample Input 4

10 9\n9 6 5 7 5 9 8 5 6 7\n8 6 8 5 5 7 9 9 7\n
\n
\n
\n
\n
\n

Sample Output 4

191\n
\n
\n
\n
\n
\n
\n

Sample Input 5

20 20\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n
\n
\n
\n
\n
\n

Sample Output 5

846527861\n
\n

Be sure to print the number modulo 10^9+7.

\n
\n
", "c_code": "int solution() {\n int n;\n int m;\n scanf(\"%d %d\", &n, &m);\n int i;\n int j;\n int s[2003];\n int t[2003];\n for (i = 0; i < n; i++) {\n scanf(\"%d\", &s[i]);\n }\n for (i = 0; i < m; i++) {\n scanf(\"%d\", &t[i]);\n }\n long long int p = 1000000007;\n long long int dp[2003][2003];\n for (i = 0; i < 2003; i++) {\n for (j = 0; j < 2003; j++) {\n dp[i][j] = 0;\n }\n }\n for (i = 0; i < 2003; i++) {\n dp[i][0] = dp[0][i] = 1;\n }\n for (i = 0; i < n; i++) {\n for (j = 0; j < m; j++) {\n if (s[i] == t[j]) {\n dp[i + 1][j + 1] += dp[i][j];\n dp[i + 1][j + 1] %= p;\n }\n dp[i + 1][j + 1] += dp[i][j + 1] + dp[i + 1][j] - dp[i][j];\n dp[i + 1][j + 1] %= p;\n if (dp[i + 1][j + 1] < 0) {\n dp[i + 1][j + 1] += p;\n }\n }\n }\n printf(\"%lld\\n\", dp[n][m]);\n return 0;\n}", "rust_code": "fn solution() {\n input! {\n n: usize,\n m: usize,\n s: [usize; n],\n t: [usize; m]\n }\n let d = 1000000007;\n let mut tab: Vec> = vec![vec![0; m + 1]; n + 1];\n for i in 0..n + 1 {\n tab[i][0] = 1;\n }\n for j in 0..m + 1 {\n tab[0][j] = 1;\n }\n for i in 0..n {\n for j in 0..m {\n if s[i] == t[j] {\n tab[i + 1][j + 1] = (tab[i][j + 1] + tab[i + 1][j]) % d;\n } else {\n tab[i + 1][j + 1] = (tab[i][j + 1] + tab[i + 1][j] + d - tab[i][j]) % d;\n }\n }\n }\n println!(\"{}\", tab[n][m]);\n}", "difficulty": "medium"} {"problem_id": "1881", "problem_description": "\n

Score: 100 points

\n
\n
\n

Problem Statement

\n

M-kun is a competitor in AtCoder, whose highest rating is X.
\nIn this site, a competitor is given a kyu (class) according to his/her highest rating. For ratings from 400 through 1999, the following kyus are given:

\n
    \n
  • From 400 through 599: 8-kyu
  • \n
  • From 600 through 799: 7-kyu
  • \n
  • From 800 through 999: 6-kyu
  • \n
  • From 1000 through 1199: 5-kyu
  • \n
  • From 1200 through 1399: 4-kyu
  • \n
  • From 1400 through 1599: 3-kyu
  • \n
  • From 1600 through 1799: 2-kyu
  • \n
  • From 1800 through 1999: 1-kyu
  • \n
\n

What kyu does M-kun have?

\n
\n
\n
\n
\n

Constraints

\n
    \n
  • 400 \\leq X \\leq 1999
  • \n
  • X is an integer.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

\n

Input is given from Standard Input in the following format:

\n
X\n
\n
\n
\n
\n
\n

Output

\n

Print the kyu M-kun has, as an integer.\nFor example, if he has 8-kyu, print 8.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

725\n
\n
\n
\n
\n
\n

Sample Output 1

7\n
\n

M-kun's highest rating is 725, which corresponds to 7-kyu.
\nThus, 7 is the correct output.

\n
\n
\n
\n
\n
\n

Sample Input 2

1600\n
\n
\n
\n
\n
\n

Sample Output 2

2\n
\n

M-kun's highest rating is 1600, which corresponds to 2-kyu.
\nThus, 2 is the correct output.

\n
\n
", "c_code": "int solution(void) {\n int X = 0;\n\n scanf(\"%d\", &X);\n\n if (X >= 400 && X <= 599) {\n printf(\"8\\n\");\n } else if (X >= 600 && X <= 799) {\n printf(\"7\\n\");\n } else if (X >= 800 && X <= 999) {\n printf(\"6\\n\");\n } else if (X >= 1000 && X <= 1199) {\n printf(\"5\\n\");\n } else if (X >= 1200 && X <= 1399) {\n printf(\"4\\n\");\n } else if (X >= 1400 && X <= 1599) {\n printf(\"3\\n\");\n } else if (X >= 1600 && X <= 1799) {\n printf(\"2\\n\");\n } else if (X >= 1800 && X <= 1999) {\n printf(\"1\\n\");\n } else {\n printf(\"err¥n\");\n }\n\n return 0;\n}", "rust_code": "fn solution() -> io::Result<()> {\n let s = {\n let mut s = String::new();\n std::io::stdin().read_line(&mut s).unwrap();\n s.trim_end().to_owned()\n };\n\n let num: i32 = s.parse().unwrap();\n\n match num {\n 400..=599 => println!(\"8\"),\n 600..=799 => println!(\"7\"),\n 800..=999 => println!(\"6\"),\n 1000..=1199 => println!(\"5\"),\n 1200..=1399 => println!(\"4\"),\n 1400..=1599 => println!(\"3\"),\n 1600..=1799 => println!(\"2\"),\n 1800..=1999 => println!(\"1\"),\n _ => println!(\"something else\"),\n }\n\n Ok(())\n}", "difficulty": "easy"} {"problem_id": "1882", "problem_description": "\n

Score : 100 points

\n
\n
\n

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

\n

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

\n

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

\n

What is the minimum possible sum of the flight times?

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq P,Q,R \\leq 100
  • \n
  • All values in input are integers.
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
P Q R\n
\n
\n
\n
\n
\n

Output

Print the minimum possible sum of the flight times.

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

1 3 4\n
\n
\n
\n
\n
\n

Sample Output 1

4\n
\n
    \n
  • The sum of the flight times in the route A \\rightarrow B \\rightarrow C: 1 + 3 = 4 hours
  • \n
  • The sum of the flight times in the route A \\rightarrow C \\rightarrow C: 4 + 3 = 7 hours
  • \n
  • The sum of the flight times in the route B \\rightarrow A \\rightarrow C: 1 + 4 = 5 hours
  • \n
  • The sum of the flight times in the route B \\rightarrow C \\rightarrow A: 3 + 4 = 7 hours
  • \n
  • The sum of the flight times in the route C \\rightarrow A \\rightarrow B: 4 + 1 = 5 hours
  • \n
  • The sum of the flight times in the route C \\rightarrow B \\rightarrow A: 3 + 1 = 4 hours
  • \n
\n

The minimum of these is 4 hours.

\n
\n
\n
\n
\n
\n

Sample Input 2

3 2 3\n
\n
\n
\n
\n
\n

Sample Output 2

5\n
\n
\n
", "c_code": "int solution() {\n int a = 0;\n int b = 0;\n int c = 0;\n int menor = 0;\n scanf(\"%d%d%d\", &a, &b, &c);\n if (c >= a && c >= b) {\n menor = a + b;\n } else if (a >= c && a >= b) {\n menor = c + b;\n } else if (b >= a && b >= c) {\n menor = a + c;\n }\n\n printf(\"%d\\n\", menor);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut pqr = String::new();\n stdin().read_line(&mut pqr).unwrap();\n let mut pqr: Vec = pqr.split_whitespace().flat_map(str::parse).collect();\n pqr.sort();\n println!(\"{}\", pqr[0] + pqr[1]);\n}", "difficulty": "hard"} {"problem_id": "1883", "problem_description": "Being stuck at home, Ray became extremely bored. To pass time, he asks Lord Omkar to use his time bending power: Infinity Clock! However, Lord Omkar will only listen to mortals who can solve the following problem:You are given an array $$$a$$$ of $$$n$$$ integers. You are also given an integer $$$k$$$. Lord Omkar wants you to do $$$k$$$ operations with this array.Define one operation as the following: Set $$$d$$$ to be the maximum value of your array. For every $$$i$$$ from $$$1$$$ to $$$n$$$, replace $$$a_{i}$$$ with $$$d-a_{i}$$$. The goal is to predict the contents in the array after $$$k$$$ operations. Please help Ray determine what the final sequence will look like!", "c_code": "int solution() {\n int t;\n scanf(\"%d\", &t);\n for (int i = 0; i < t; i++) {\n int n;\n long long k;\n scanf(\"%d %lld\", &n, &k);\n int a[n];\n for (int j = 0; j < n; j++) {\n scanf(\"%d\", &a[j]);\n }\n\n int max = a[0];\n int min = a[0];\n for (int j = 1; j < n; j++) {\n if (max < a[j]) {\n max = a[j];\n }\n if (min > a[j]) {\n min = a[j];\n }\n }\n\n if (k & 1) {\n for (int j = 0; j < n; j++) {\n printf(\"%d \", max - a[j]);\n }\n } else {\n for (int j = 0; j < n; j++) {\n printf(\"%d \", a[j] - min);\n }\n }\n printf(\"\\n\");\n }\n return 0;\n}", "rust_code": "fn solution() {\n let reader = BufReader::new(stdin());\n let mut lines = reader.lines();\n let cases: usize = lines.next().unwrap().unwrap().parse().unwrap();\n let mut lines_take = lines.take(cases << 1);\n for _ in 0..cases {\n let info = lines_take.next().unwrap().unwrap();\n let mut info_sp = info.split_ascii_whitespace();\n let _n: usize = info_sp.next().unwrap().parse().unwrap();\n let mut k: u64 = info_sp.next().unwrap().parse().unwrap();\n let line = lines_take.next().unwrap().unwrap();\n let line_sp = line.split_ascii_whitespace();\n let mut arr: Vec = line_sp.map(|x| x.parse::().unwrap()).collect();\n let mut new_arr = arr.clone();\n arr.sort();\n let mut min = *arr.first().unwrap();\n if k % 2 == 1 {\n let max = *new_arr.iter().max().unwrap();\n for x in new_arr.iter_mut() {\n *x = max - *x;\n }\n k -= 1;\n min = 0;\n }\n\n if k == 0 {\n let mut iter = new_arr.iter();\n print!(\"{}\", iter.next().unwrap());\n for x in iter {\n print!(\" {}\", x);\n }\n println!();\n } else {\n let mut iter = new_arr.iter();\n print!(\"{}\", iter.next().unwrap() - min);\n for x in iter {\n print!(\" {}\", x - min);\n }\n println!();\n }\n }\n}", "difficulty": "medium"} {"problem_id": "1884", "problem_description": "\n

Score : 400 points

\n
\n
\n

Problem Statement

For a positive integer X, let f(X) be the number of positive divisors of X.

\n

Given a positive integer N, find \\sum_{K=1}^N K\\times f(K).

\n
\n
\n
\n
\n

Constraints

    \n
  • 1 \\leq N \\leq 10^7
  • \n
\n
\n
\n
\n
\n
\n
\n

Input

Input is given from Standard Input in the following format:

\n
N\n
\n
\n
\n
\n
\n

Output

Print the value \\sum_{K=1}^N K\\times f(K).

\n
\n
\n
\n
\n
\n
\n

Sample Input 1

4\n
\n
\n
\n
\n
\n

Sample Output 1

23\n
\n

We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\\times 1 + 2\\times 2 + 3\\times 2 + 4\\times 3 =23.

\n
\n
\n
\n
\n
\n

Sample Input 2

100\n
\n
\n
\n
\n
\n

Sample Output 2

26879\n
\n
\n
\n
\n
\n
\n

Sample Input 3

10000000\n
\n
\n
\n
\n
\n

Sample Output 3

838627288460105\n
\n

Watch out for overflows.

\n
\n
", "c_code": "int solution(void) {\n long int n;\n scanf(\"%ld\", &n);\n long int ans = 0;\n for (int i = 1; i <= n; i++) {\n long int now = n / i;\n ans += (now + 1) * now / 2 * i;\n }\n printf(\"%ld\\n\", ans);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut s = String::new();\n io::stdin().read_line(&mut s).ok();\n let n: u64 = s.trim().parse().unwrap();\n\n let mut v: Vec<(u64, u64)> = (1..(n + 1))\n .map(|e| (e, n / e))\n .filter(|(e, _)| e * e <= n)\n .collect();\n let x = v[v.len() - 1];\n let mut ans = 0;\n if x.0 == x.1 {\n ans += x.0 * x.0 * (x.0 + 1) / 2;\n } else {\n v.push((x.1, x.0));\n }\n\n for i in 0..(v.len() - 1) {\n ans += v[i].0 * v[i].1 * (v[i].1 + 1) / 2;\n\n ans += v[i].0 * (v[i].0 + 1) * (v[i].1 - v[i + 1].1) * (v[i].1 + v[i + 1].1 + 1) / 4;\n }\n\n println!(\"{}\", ans);\n}", "difficulty": "hard"} {"problem_id": "1885", "problem_description": "\n\n\n

Min, Max and Sum

\n\n

\nWrite a program which reads a sequence of $n$ integers $a_i (i = 1, 2, ... n)$, and prints the minimum value, maximum value and sum of the sequence.\n

\n\n

Input

\n

\n In the first line, an integer $n$ is given. In the next line, $n$ integers $a_i$ are given in a line.\n

\n\n

Output

\n

\n Print the minimum value, maximum value and sum in a line. Put a single space between the values.\n

\n\n

Constraints

\n\n
    \n
  • $0 < n \\leq 10000$
  • \n
  • $-1000000 \\leq a_i \\leq 1000000$
  • \n
\n\n

Sample Input

\n
\n5\n10 1 5 4 17\n
\n\n

Sample Output

\n\n
\n1 17 37\n
", "c_code": "int solution(void) {\n int n = 0;\n int a = 0;\n int maximum = -1000000;\n int minimum = 1000000;\n long sum = 0;\n\n scanf(\"%d\", &n);\n\n while (scanf(\"%d\", &a) != EOF) {\n sum += a;\n if (a >= maximum) {\n maximum = a;\n }\n if (a <= minimum) {\n minimum = a;\n }\n }\n\n printf(\"%d %d %ld\\n\", minimum, maximum, sum);\n}", "rust_code": "fn solution() {\n let stdin = io::stdin();\n let mut buf = String::new();\n\n let _ = stdin.read_line(&mut buf);\n buf.clear();\n\n let _ = stdin.read_line(&mut buf);\n let vec: Vec = buf\n .split_whitespace()\n .map(|x| i64::from_str(x).unwrap())\n .collect();\n\n println!(\n \"{} {} {}\",\n vec.iter().min().unwrap(),\n vec.iter().max().unwrap(),\n vec.iter().sum::()\n );\n}", "difficulty": "medium"} {"problem_id": "1886", "problem_description": "After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 ≤ si ≤ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)?", "c_code": "int solution() {\n\n long long int n;\n long long int count = 0;\n long long int a = 0;\n long long int b = 0;\n long long int c = 0;\n scanf(\"%lld\", &n);\n long long int arr[n];\n for (int i = 0; i < n; i++) {\n scanf(\"%lld\", &arr[i]);\n }\n for (int i = 0; i < n; i++) {\n if (arr[i] == 4) {\n count++;\n }\n if (arr[i] == 3) {\n a++;\n }\n if (arr[i] == 1) {\n b++;\n }\n if (arr[i] == 2) {\n c++;\n }\n }\n count = count + a;\n if (b > a) {\n b = b - a;\n if (c % 2 == 0 && b % 4 == 0) {\n count = count + c / 2 + b / 4;\n } else if (c % 2 == 0 && b % 4 != 0) {\n count = count + c / 2 + b / 4 + 1;\n } else if (b <= 2 && c % 2 != 0) {\n count = count + c / 2 + c % 2;\n } else if ((b - 2) % 4 == 0 && b > 2 && c % 2 != 0) {\n count = count + c / 2 + c % 2 + (b - 2) / 4;\n } else {\n count = count + c / 2 + c % 2 + (b - 2) / 4 + 1;\n }\n } else {\n count = count + c / 2 + c % 2;\n }\n\n printf(\"%lld\", count);\n\n return 0;\n}", "rust_code": "fn solution() {\n let mut _buffer = String::new();\n io::stdin()\n .read_line(&mut _buffer)\n .expect(\"failed to read input\");\n let mut buffer = String::new();\n io::stdin()\n .read_line(&mut buffer)\n .expect(\"failed to read input\");\n let nums = buffer\n .split_whitespace()\n .map(|n| n.parse::().unwrap())\n .collect::>();\n let mut num_groups = [0; 4];\n for i in nums {\n num_groups[i - 1] += 1;\n }\n let min13 = if num_groups[0] < num_groups[2] {\n num_groups[0]\n } else {\n num_groups[2]\n };\n let mut count = num_groups[3] + num_groups[1] / 2 + min13;\n num_groups[0] -= min13;\n num_groups[1] %= 2;\n num_groups[2] -= min13;\n if num_groups[0] != 0 {\n count += num_groups[0] / 4;\n num_groups[0] %= 4;\n\n if num_groups[1] != 0 {\n count += 1;\n num_groups[0] -= if num_groups[0] >= 2 { 2 } else { num_groups[0] };\n }\n\n if num_groups[0] != 0 {\n count += 1;\n }\n } else {\n if num_groups[1] != 0 {\n count += 1;\n }\n count += num_groups[2];\n }\n println!(\"{}\", count);\n}", "difficulty": "easy"}