Upload folder using huggingface_hub (part 11)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- cf/cf-1848-B.jsonlAdded+4
- cf/cf-1848-C.jsonlAdded+4
- cf/cf-1848-D.jsonlAdded+4
- cf/cf-1848-E.jsonlAdded+4
- cf/cf-1848-F.jsonlAdded+4
- cf/cf-1849-A.jsonlAdded+4
- cf/cf-1849-B.jsonlAdded+4
- cf/cf-1849-C.jsonlAdded+4
- cf/cf-1849-D.jsonlAdded+4
- cf/cf-1849-E.jsonlAdded+4
- cf/cf-1849-F.jsonlAdded+4
- cf/cf-1850-A.jsonlAdded+4
- cf/cf-1850-B.jsonlAdded+4
- cf/cf-1850-C.jsonlAdded+4
- cf/cf-1850-D.jsonlAdded+4
- cf/cf-1850-E.jsonlAdded+4
- cf/cf-1850-F.jsonlAdded+4
- cf/cf-1850-G.jsonlAdded+4
- cf/cf-1850-H.jsonlAdded+4
- cf/cf-1851-A.jsonlAdded+4
- cf/cf-1851-B.jsonlAdded+4
- cf/cf-1851-C.jsonlAdded+4
- cf/cf-1851-D.jsonlAdded+4
- cf/cf-1851-E.jsonlAdded+4
- cf/cf-1851-F.jsonlAdded+4
- cf/cf-1851-G.jsonlAdded+4
- cf/cf-1854-E.jsonlAdded+4
- cf/cf-1854-F.jsonlAdded+4
- cf/cf-1856-A.jsonlAdded+4
- cf/cf-1856-B.jsonlAdded+4
- cf/cf-1856-C.jsonlAdded+4
- cf/cf-1856-D.jsonlAdded+4
- cf/cf-1856-E1.jsonlAdded+4
- cf/cf-1856-E2.jsonlAdded+4
- cf/cf-1857-A.jsonlAdded+4
- cf/cf-1857-B.jsonlAdded+4
- cf/cf-1857-C.jsonlAdded+4
- cf/cf-1857-D.jsonlAdded+4
- cf/cf-1857-E.jsonlAdded+4
- cf/cf-1857-F.jsonlAdded+4
- cf/cf-1857-G.jsonlAdded+4
- cf/cf-1858-A.jsonlAdded+4
- cf/cf-1858-B.jsonlAdded+4
- cf/cf-1858-C.jsonlAdded+4
- cf/cf-1858-D.jsonlAdded+4
- cf/cf-1858-E2.jsonlAdded+4
- cf/cf-1859-A.jsonlAdded+4
- cf/cf-1859-B.jsonlAdded+4
- cf/cf-1859-D.jsonlAdded+4
- cf/cf-1860-A.jsonlAdded+4
1234{"type": "session", "harness": "opencode", "id": "cf-1848-B", "name": "Codeforces: Vika and the Bridge"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Vika and the Bridge (Rating: 1200)\nTags: binary search, data structures, greedy, implementation, math, sortings\n\nIn the summer, Vika likes to visit her country house. There is everything for relaxation: comfortable swings, bicycles, and a river.\n\nThere is a wooden bridge over the river, consisting of $n$ planks. It is quite old and unattractive, so Vika decided to paint it. And in the shed, they just found cans of paint of $k$ colors.\n\nAfter painting each plank in one of $k$ colors, Vika was about to go swinging to take a break from work. However, she realized that the house was on the other side of the river, and the paint had not yet completely dried, so she could not walk on the bridge yet.\n\nIn order not to spoil the appearance of the bridge, Vika decided that she would still walk on it, but only stepping on planks of the same color. Otherwise, a small layer of paint on her sole will spoil the plank of another color. Vika also has a little paint left, but it will only be enough to repaint one plank of the bridge.\n\nNow Vika is standing on the ground in front of the first plank. To walk across the bridge, she will choose some planks of the same color (after repainting), which have numbers $1 \\le i_1 < i_2 < \\ldots < i_m \\le n$ (planks are numbered from $1$ from left to right). Then Vika will have to cross $i_1 - 1, i_2 - i_1 - 1, i_3 - i_2 - 1, \\ldots, i_m - i_{m-1} - 1, n - i_m$ planks as a result of each of $m + 1$ steps.\n\nSince Vika is afraid of falling, she does not want to take too long steps. Help her and tell her the minimum possible maximum number of planks she will have to cross in one step, if she can repaint one (or zero) plank a different color while crossing the bridge.\n\nInput: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$) — the number of planks in the bridge and the number of different colors of paint.\n\nThe second line of each test case contains $n$ integers $c_1, c_2, c_3, \\dots, c_n$ ($1 \\le c_i \\le k$) — the colors in which Vika painted the planks of the bridge.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput: For each test case, output a single integer — the minimum possible maximum number of planks that Vika will have to step over in one step.\n\nExamples:\nInput: 5\n5 2\n1 1 2 1 1\n7 3\n1 2 3 3 3 2 1\n6 6\n1 2 3 4 5 6\n8 4\n1 2 3 4 2 3 1 4\n3 1\n1 1 1\nOutput: 0\n1\n2\n2\n0\n"}}
{"type": "message", "message": {"role": "assistant", "content": "In a single linear pass through the array, let's calculate, for each color, the lengths of the two maximum steps between planks of that color. To do this, we will maintain when we last encountered that color.\nNow we need to consider that we can repaint one of the planks. Let's say we repaint a plank in color $$$c$$$. It is easy to notice that we should repaint the plank in the middle of the longest step between planks of color $$$c$$$. After all, if we don't repaint such a plank, we will still have to make that longest step.\nTherefore, the answer for a fixed color will be the maximum of two values: half the length of the longest step between planks of that color, and the length of the second largest step between planks of that color.\nKnowing the answer for each individual color, we can determine the answer to the problem. To do this, we just need to take the minimum of the answers for all colors."}}1234{"type": "session", "harness": "opencode", "id": "cf-1848-C", "name": "Codeforces: Vika and Price Tags"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Vika and Price Tags (Rating: 1800)\nTags: math, number theory\n\nVika came to her favorite cosmetics store \"Golden Pear\". She noticed that the prices of $n$ items have changed since her last visit.\n\nShe decided to analyze how much the prices have changed and calculated the difference between the old and new prices for each of the $n$ items.\n\nVika enjoyed calculating the price differences and decided to continue this process.\n\nLet the old prices be represented as an array of non-negative integers $a$, and the new prices as an array of non-negative integers $b$. Both arrays have the same length $n$.\n\nIn one operation, Vika constructs a new array $c$ according to the following principle: $c_i = |a_i - b_i|$. Then, array $c$ renamed into array $b$, and array $b$ renamed into array $a$ at the same time, after which Vika repeats the operation with them.\n\nFor example, if $a = [1, 2, 3, 4, 5, 6, 7]$; $b = [7, 6, 5, 4, 3, 2, 1]$, then $c = [6, 4, 2, 0, 2, 4, 6]$. Then, $a = [7, 6, 5, 4, 3, 2, 1]$; $b = [6, 4, 2, 0, 2, 4, 6]$.\n\nVika decided to call a pair of arrays $a$, $b$ dull if after some number of such operations all elements of array $a$ become zeros.\n\nOutput \"YES\" if the original pair of arrays is dull, and \"NO\" otherwise.\n\nInput: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 10^5$) — the number of items whose prices have changed.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i \\le 10^9$) — the old prices of the items.\n\nThe third line contains $n$ integers $b_1, b_2, \\ldots, b_n$ ($0 \\le b_i \\le 10^9$) — the new prices of the items.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\nOutput: For each test case, output \"YES\" if the pair of price arrays is dull, and \"NO\" otherwise.\n\nYou can output each letter in any case (lowercase or uppercase). For example, the strings \"yEs\", \"yes\", \"Yes\", and \"YES\" will be accepted as a positive answer.\n\nExamples:\nInput: 9\n4\n0 0 0 0\n1 2 3 4\n3\n1 2 3\n1 2 3\n2\n1 2\n2 1\n6\n100 23 53 11 56 32\n1245 31 12 6 6 6\n7\n1 2 3 4 5 6 7\n7 6 5 4 3 2 1\n3\n4 0 2\n4 0 2\n3\n2 5 2\n1 3 4\n2\n6 1\n4 2\n2\n0 0\n0 3\nOutput: YES\nYES\nNO\nNO\nYES\nYES\nNO\nYES\nYES\n"}}
{"type": "message", "message": {"role": "assistant", "content": "First of all, if $$$a_i$$$ and $$$b_i$$$ are both zero, then all numbers in the sequence will be zero.\nOtherwise, if one of the numbers $$$a_i, b_i$$$ is not zero, then if $$$a_i \\ge b_i$$$, after one operation, the sum $$$a_i - b_i + b_i = a_i$$$ will decrease relative to the original value, or if $$$b_i > a_i$$$, after two operations, the sum $$$b_i - a_i + a_i = b_i$$$ will also decrease relative to the original value. Since the sum of non-negative integers cannot decrease infinitely, eventually one of the numbers $$$a_i, b_i$$$ will become zero. Let the first such moment occur after $$$cnt_i$$$ operations. Then, notice that now zeros will alternate with a period of $$$3$$$. Therefore, in the problem, we need to check that all $$$cnt_i$$$ have the same remainder when divided by $$$3$$$. Thus, the problem reduces to finding $$$cnt_i$$$ for each pair of non-zero $$$a_i, b_i$$$ modulo $$$3$$$.\nSolution 1: Without loss of generality, assume $$$a_i \\ge b_i$$$, otherwise apply one operation. Then, the sequence of numbers will have the form: $$$(a_i, b_i, a_i - b_i, a_i - 2 \\cdot b_i, b_i, a_i - 3 \\cdot b_i, a_i - 4 \\cdot b_i, b_i, \\ldots)$$$. Let $$$a_i = k \\cdot b_i + r$$$. Then, using simple formulas, we can find the first moment when the neighboring pair of numbers becomes $$$r$$$ and $$$b_i$$$ in some order, and then simply find the answer for them. Thus, the problem can be solved using the generalized Euclidean algorithm.\nSolution 2: We will build the sequence from the end. Let's find the first moment when we obtain $$$0$$$. Before this zero, there is some number $$$d$$$. It can be easily proven that $$$d$$$ is exactly equal to $$$gcd(a_i, b_i)$$$. Now, let's divide each number in the sequence by $$$d$$$, and obtain a new sequence of numbers, where the last number is zero and the penultimate number is $$$1$$$. Then, let's denote even numbers as $$$0$$$ and odd numbers as $$$1$$$. In this way, the sequence can be uniquely reconstructed from the end: $$$(0, 1, 1, 0, 1, 1, 0, 1, 1, \\ldots)$$$. Thus, we can determine the remainder $$$cnt_i$$$ modulo $$$3$$$ by looking at the pair $$$(a_i / d, b_i / d)$$$ modulo $$$2$$$.\nThe complexity of both solutions will be $$$O(n \\cdot \\log{a_i})$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1848-D", "name": "Codeforces: Vika and Bonuses"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Vika and Bonuses (Rating: 2200)\nTags: binary search, brute force, math, ternary search\n\nA new bonus system has been introduced at Vika's favorite cosmetics store, \"Golden Pear\"!\n\nThe system works as follows: suppose a customer has $b$ bonuses. Before paying for the purchase, the customer can choose one of two options:\n\n- Get a discount equal to the current number of bonuses, while the bonuses are not deducted.\n- Accumulate an additional $x$ bonuses, where $x$ is the last digit of the number $b$. As a result, the customer's account will have $b+x$ bonuses.\n\nFor example, if a customer had $24$ bonuses, he can either get a discount of $24$ or accumulate an additional $4$ bonuses, after which his account will have $28$ bonuses.\n\nAt the moment, Vika has already accumulated $s$ bonuses.\n\nThe girl knows that during the remaining time of the bonus system, she will make $k$ more purchases at the \"Golden Pear\" store network.\n\nAfter familiarizing herself with the rules of the bonus system, Vika became interested in the maximum total discount she can get.\n\nHelp the girl answer this question.\n\nInput: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 10^5$) — the number of test cases. The description of the test cases follows.\n\nThe test case consists of a single line containing two integers $s$ and $k$ ($0 \\le s \\le 10^9$, $1 \\le k \\le 10^9$) — the current number of bonuses in Vika's account and how many more purchases the girl will make.\n\nOutput: For each test case, output a single integer — the maximum total discount that can be obtained through the bonus system.\n\nExamples:\nInput: 6\n1 3\n11 3\n0 179\n5 1000000000\n723252212 856168102\n728598293 145725253\nOutput: 4\n33\n0\n9999999990\n1252047198518668448\n106175170582793129\n"}}
{"type": "message", "message": {"role": "assistant", "content": "First, let's note that the optimal strategy for Vika will be to accumulate bonuses first and then only get the discount. This simple observation is proved by greedy considerations.\nNext, let's note that the last digit of the bonus number will either become zero after a few actions (in which case it makes no sense for Vika to accumulate more bonuses), or it will cycle through the digits $$$2$$$, $$$4$$$, $$$8$$$, $$$6$$$.\nTo begin with, let's consider two options: whether Vika will accumulate bonuses at all or always choose the option to get the discount. In the second case, the answer is trivially calculated, but in the first case, we can use the above statement.\nNow let's consider four options for the last digit. With a fixed last digit, we can simulate the first actions until we reach that last digit. Then we need to determine how many times we will \"scroll\" through the digits $$$2$$$, $$$4$$$, $$$8$$$, $$$6$$$ for the best result. Let's say that at the moment of obtaining the desired last digit, Vika has accumulated $$$b$$$ bonuses and she can perform $$$a$$$ more actions. Then, if the cycle is \"scroll\" $$$x$$$ times, the discount will be $$$(b + 20 \\cdot x) \\cdot (a - x)$$$. This is a parabola equation. Its maximum can be found using the formula for the vertex of a parabola or by ternary search."}}1234{"type": "session", "harness": "opencode", "id": "cf-1848-E", "name": "Codeforces: Vika and Stone Skipping"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Vika and Stone Skipping (Rating: 2600)\nTags: brute force, implementation, math, number theory\n\nIn Vika's hometown, Vladivostok, there is a beautiful sea.\n\nOften you can see kids skimming stones. This is the process of throwing a stone into the sea at a small angle, causing it to fly far and bounce several times off the water surface.\n\nVika has skimmed stones many times and knows that if you throw a stone from the shore perpendicular to the coastline with a force of $f$, it will first touch the water at a distance of $f$ from the shore, then bounce off and touch the water again at a distance of $f - 1$ from the previous point of contact. The stone will continue to fly in a straight line, reducing the distances between the points where it touches the water, until it falls into the sea.\n\nFormally, the points at which the stone touches the water surface will have the following coordinates: $f$, $f + (f - 1)$, $f + (f - 1) + (f - 2)$, ... , $f + (f - 1) + (f - 2) + \\ldots + 1$ (assuming that $0$ is the coordinate of the shoreline).\n\nOnce, while walking along the embankment of Vladivostok in the evening, Vika saw a group of guys skipping stones across the sea, launching them from the same point with different forces.\n\nShe became interested in what is the maximum number of guys who can launch a stone with their force $f_i$, so that all $f_i$ are different positive integers, and all $n$ stones touched the water at the point with the coordinate $x$ (assuming that $0$ is the coordinate of the shoreline).\n\nAfter thinking a little, Vika answered her question. After that, she began to analyze how the answer to her question would change if she multiplied the coordinate $x$ by some positive integers $x_1$, $x_2$, ... , $x_q$, which she picked for analysis.\n\nVika finds it difficult to cope with such analysis on her own, so she turned to you for help.\n\nFormally, Vika is interested in the answer to her question for the coordinates $X_1 = x \\cdot x_1$, $X_2 = X_1 \\cdot x_2$, ... , $X_q = X_{q-1} \\cdot x_q$. Since the answer for such coordinates can be quite large, find it modulo $M$. It is guaranteed that $M$ is prime.\n\nInput: The first line of the input contains three integers $x$ ($1 \\le x \\le 10^9$), $q$ ($1 \\le q \\le 10^5$) and $M$ ($100 \\le M \\le 2 \\cdot 10^9$) — the initial coordinate for which Vika answered the question on her own, the number of integers $x_i$ by which Vika will multiply the initial coordinate and prime module $M$.\n\nThe second line of the input contains $q$ integers $x_1, x_2, x_3, \\ldots, x_q$ ($1 \\le x_i \\le 10^6$) — the integers described in the statement.\n\nOutput: Output $q$ integers, where the $i$-th number corresponds to the answer to Vika's question for the coordinate $X_i$. Output all the answers modulo $M$.\n\nExamples:\nInput: 1 2 179\n2 3\nOutput: 1\n2\n\nInput: 7 5 998244353\n2 13 1 44 179\nOutput: 2\n4\n4\n8\n16\n\nInput: 1000000000 10 179\n58989 49494 8799 9794 97414 141241 552545 145555 548959 774175\nOutput: 120\n4\n16\n64\n111\n43\n150\n85\n161\n95\n"}}
{"type": "message", "message": {"role": "assistant", "content": "The key observation is that the answer for coordinate $$$x$$$ is the number of odd divisors of $$$x$$$. Let's prove this.\nLet's see how far a pebble will fly with force $$$f$$$, which touches the water $$$cnt$$$ times: $$$f + (f - 1) + \\ldots + (f - cnt + 1) = x$$$. If $$$cnt$$$ is even, then $$$x = (cnt / 2) \\cdot (2 \\cdot f - cnt + 1) = (p) \\cdot (2 \\cdot k + 1)$$$, where $$$p = cnt / 2, k = f - p$$$. In this case, the condition $$$f - cnt + 1 > 0$$$ is necessary, which is equivalent to $$$p \\le k$$$.\nIf $$$cnt$$$ is odd, then $$$x = cnt \\cdot (f - (cnt - 1) / 2) = (2 \\cdot k + 1) \\cdot (p)$$$, where $$$p = (f - (cnt - 1) / 2), k = (cnt - 1) / 2$$$. Thus, the necessary condition $$$f - cnt + 1 > 0$$$ is equivalent to $$$p > k$$$.\nTherefore, for each odd divisor $$$2 \\cdot k + 1$$$ of the number $$$x$$$, we can uniquely associate one of the decomposition options, and hence the number of possible answers is exactly equal to the number of different odd divisors in the factorization of the number $$$x$$$.\nUsing this observation, it is easy to obtain the answer. We will maintain the power of each prime number in the current coordinate. The answer is the product of (powers $$$+ 1$$$). In order to quickly understand how these quantities change, we will pre-calculate the factorization of all numbers from $$$1$$$ to $$$10^6$$$. Then the query can be processed by quickly recalculating the powers using the pre-calculated factorizations."}}1234{"type": "session", "harness": "opencode", "id": "cf-1848-F", "name": "Codeforces: Vika and Wiki"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Vika and Wiki (Rating: 2400)\nTags: binary search, bitmasks, combinatorics, divide and conquer, dp, math\n\nRecently, Vika was studying her favorite internet resource - Wikipedia.\n\nOn the expanses of Wikipedia, she read about an interesting mathematical operation bitwise XOR, denoted by $\\oplus$.\n\nVika began to study the properties of this mysterious operation. To do this, she took an array $a$ consisting of $n$ non-negative integers and applied the following operation to all its elements at the same time: $a_i = a_i \\oplus a_{(i+1) \\bmod n}$. Here $x \\bmod y$ denotes the remainder of dividing $x$ by $y$. The elements of the array are numbered starting from $0$.\n\nSince it is not enough to perform the above actions once for a complete study, Vika repeats them until the array $a$ becomes all zeros.\n\nDetermine how many of the above actions it will take to make all elements of the array $a$ zero. If this moment never comes, output $-1$.\n\nInput: The first line contains a single integer $n$ ($1 \\le n \\le 2^{20}$) - the length of the array $a$.\n\nIt is guaranteed that $n$ can be represented as $2^k$ for some integer $k$ ($0 \\le k \\le 20$).\n\nThe second line contains $n$ integers $a_0, a_1, a_2, \\dots, a_{n-1}$ ($0 \\le a_i \\le 10^9$) - the elements of the array $a$.\n\nOutput: Output a single number - the minimum number of actions required to make all elements of the array $a$ zero, or $-1$ if the array $a$ will never become zero.\n\nExamples:\nInput: 4\n1 2 1 2\nOutput: 2\n\nInput: 2\n0 0\nOutput: 0\n\nInput: 1\n14\nOutput: 1\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's denote $$$next(i, delta) = (i + delta \\bmod n) + 1$$$, and $$$a[cnt][i]$$$ represents the value of $$$a_i$$$ after $$$cnt$$$ operations.\nFirst, let's observe that if the array becomes all zeros, it will always remain all zeros. Therefore, we need to find the first moment in time when the array becomes all zeros.\nFurthermore, let's see how the array looks like after $$$2^t$$$ operations. We will prove by induction that $$$a[2^t][i] = a[0][i] \\oplus a[0][next(i, 2^t)]$$$ for each $$$i$$$. Indeed, for $$$t = 0$$$, this is true by definition, and for $$$t \\ge 1$$$:\n$$$a[2^t][i] = a[2^{t-1}][i] \\oplus a[2^{t-1}][next(i, 2^{t-1})] = a[0][i] \\oplus a[0][next(i, 2^{t-1})] \\oplus a[0][next(i, 2^{t-1})] \\oplus a[0][next(i, 2^t)] = a[0][i] \\oplus a[0][next(i, 2^t)]$$$.\nThus, $$$a[n][i] = a[2^k][i] = a[0][i] \\oplus a[0][next(i, n)] = a[0][i] \\oplus a[0][i] = 0$$$, which means that after $$$n$$$ steps, the array becomes all zeros.\nNow, we want to find the minimum number of operations $$$c$$$ after which the array $$$a$$$ becomes all zeros, knowing that $$$c \\in [0, n]$$$. Let's check that $$$c \\le n / 2$$$, by explicitly constructing the array $$$a[n/2][i] = a[0][i] \\oplus a[0][next(i, n / 2)]$$$. The condition $$$c \\le n / 2$$$ is equivalent to all elements of the array $$$a[n/2][i]$$$ being zeros.\nThe algorithm can be summarized as follows:\nCheck if $$$a_i = a_{next(i, n/2)}$$$ for each $$$i$$$. If this is true, find the answer for the prefix of the array $$$a$$$ of length $$$n/2$$$. Otherwise, perform $$$n/2$$$ operations on the array $$$a$$$, specifically $$$a_i = a_i \\oplus a_{next(i, n / 2)}$$$. Now, the array satisfies the condition $$$a_i = a_{next(i, n/2)}$$$ for each $$$i$$$, so we only need to find the answer for its prefix of size $$$n/2$$$ and add $$$n/2$$$ to it.\nThe overall complexity of the solution is $$$O(n)$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1849-A", "name": "Codeforces: Morning Sandwich"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Morning Sandwich (Rating: 800)\nTags: implementation, math\n\nMonocarp always starts his morning with a good ol' sandwich. Sandwiches Monocarp makes always consist of bread, cheese and/or ham.\n\nA sandwich always follows the formula:\n\n- a piece of bread\n- a slice of cheese or ham\n- a piece of bread\n- $\\dots$\n- a slice of cheese or ham\n- a piece of bread\n\nSo it always has bread on top and at the bottom, and it alternates between bread and filling, where filling is a slice of either cheese or ham. Each piece of bread and each slice of cheese or ham is called a layer.\n\nToday Monocarp woke up and discovered that he has $b$ pieces of bread, $c$ slices of cheese and $h$ slices of ham. What is the maximum number of layers his morning sandwich can have?\n\nInput: The first line contains a single integer $t$ ($1 \\le t \\le 1000$) — the number of testcases.\n\nEach testcase consists of three integers $b, c$ and $h$ ($2 \\le b \\le 100$; $1 \\le c, h \\le 100$) — the number of pieces of bread, slices of cheese and slices of ham, respectively.\n\nOutput: For each testcase, print a single integer — the maximum number of layers Monocarp's morning sandwich can have.\n\nExamples:\nInput: 3\n2 1 1\n10 1 2\n3 7 8\nOutput: 3\n7\n5\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Notice that the type of filling doesn't matter. We can treat both cheese and ham together as one filling, with quantity $$$c + h$$$.\nThen let's start building a sandwich layer by layer. Put a piece of bread. Then put a layer of filling and a piece of bread. Then another layer of filling and a piece of bread. Observe that you can add one layer of filling and one piece of bread until one of them runs out. After you've done that $$$k$$$ times, you placed $$$k + 1$$$ layers of bread and $$$k$$$ layers of filling.\nThus, there are two general cases. If bread runs out first, then $$$k = b - 1$$$. Otherwise, $$$k = c + h$$$. The one that runs out first is the smaller of these two values. So the answer is $$$min(b - 1, c + h)$$$.\nOverall complexity: $$$O(1)$$$ per testcase."}}1234{"type": "session", "harness": "opencode", "id": "cf-1849-B", "name": "Codeforces: Monsters"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Monsters (Rating: 1000)\nTags: greedy, math, sortings\n\nMonocarp is playing yet another computer game. And yet again, his character is killing some monsters. There are $n$ monsters, numbered from $1$ to $n$, and the $i$-th of them has $a_i$ health points initially.\n\nMonocarp's character has an ability that deals $k$ damage to the monster with the highest current health. If there are several of them, the one with the smaller index is chosen. If a monster's health becomes less than or equal to $0$ after Monocarp uses his ability, then it dies.\n\nMonocarp uses his ability until all monsters die. Your task is to determine the order in which monsters will die.\n\nInput: The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases.\n\nThe first line of each test case contains two integers $n$ and $k$ ($1 \\le n \\le 3 \\cdot 10^5$; $1 \\le k \\le 10^9$) — the number of monsters and the damage which Monocarp's ability deals.\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) — the initial health points of monsters.\n\nThe sum of $n$ over all test cases doesn't exceed $3 \\cdot 10^5$.\n\nOutput: For each test case, print $n$ integers — the indices of monsters in the order they die.\n\nExamples:\nInput: 3\n3 2\n1 2 3\n2 3\n1 1\n4 3\n2 8 3 5\nOutput: 2 1 3 \n1 2 \n3 1 2 4\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's simulate the game process until the number of health points of each monster becomes $$$k$$$ or less. Then we can consider that the $$$i$$$-th monster has $$$a_i \\bmod k$$$ health instead of $$$a_i$$$ (except for the case when $$$a_i$$$ is divisible by $$$k$$$, then the remaining health is $$$k$$$, not $$$0$$$).\nNow, the health points of all monsters are from $$$1$$$ to $$$k$$$, so each time we damage a monster, we kill it. Therefore, monsters with $$$k$$$ health points will die first, then the ones with $$$k-1$$$ health points, and so on. So, let's sort the monsters by their remaining health points in descending order (don't forget that, if two monsters have the same health, then they should be compared by index). And the order you get after sorting is the answer to the problem."}}1234{"type": "session", "harness": "opencode", "id": "cf-1849-C", "name": "Codeforces: Binary String Copying"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Binary String Copying (Rating: 1600)\nTags: binary search, brute force, data structures, hashing, strings\n\nYou are given a string $s$ consisting of $n$ characters 0 and/or 1.\n\nYou make $m$ copies of this string, let the $i$-th copy be the string $t_i$. Then you perform exactly one operation on each of the copies: for the $i$-th copy, you sort its substring $[l_i; r_i]$ (the substring from the $l_i$-th character to the $r_i$-th character, both endpoints inclusive). Note that each operation affects only one copy, and each copy is affected by only one operation.\n\nYour task is to calculate the number of different strings among $t_1, t_2, \\ldots, t_m$. Note that the initial string $s$ should be counted only if at least one of the copies stays the same after the operation.\n\nInput: The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases.\n\nThe first line of each test case contains two integers $n$ and $m$ ($1 \\le n, m \\le 2 \\cdot 10^5$) — the length of $s$ and the number of copies, respectively.\n\nThe second line contains $n$ characters 0 and/or 1 — the string $s$.\n\nThen $m$ lines follow. The $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \\le l_i \\le r_i \\le n$) — the description of the operation applied to the $i$-th copy.\n\nThe sum of $n$ over all test cases doesn't exceed $2 \\cdot 10^5$. The sum of $m$ over all test cases doesn't exceed $2 \\cdot 10^5$.\n\nOutput: Print one integer — the number of different strings among $t_1, t_2, \\ldots, t_m$.\n\nExamples:\nInput: 3\n6 5\n101100\n1 2\n1 3\n2 4\n5 5\n1 6\n6 4\n100111\n2 2\n1 4\n1 3\n1 2\n1 1\n0\n1 1\nOutput: 3\n3\n1\n"}}
{"type": "message", "message": {"role": "assistant", "content": "We can see that each modified copy is determined by only two integers $$$lb$$$ and $$$rb$$$ — the first position at which the character has changed and the last such position. If we can find such numbers for each of the copies, the number of different pairs will be the answer to the problem.\nLet $$$lf_i$$$ be the position of the nearest character 0 at the position $$$i$$$ or to the left of it, and $$$rg_i$$$ be the position of the nearest character 1 at the position $$$i$$$ or to the right of it. If the first character of the string $$$s$$$ is 1 then $$$lf_0 = -1$$$, otherwise $$$lf_0 = 0$$$. And if the last character of the string $$$s$$$ is 0, then $$$rg_{n - 1} = n$$$, otherwise $$$rg_{n - 1} = n - 1$$$. The values $$$lf$$$ and $$$rg$$$ can be calculated using simple dynamic programming, $$$lf$$$ is calculated from left to right, and $$$rg$$$ — from right to left. Then the numbers $$$lb$$$ and $$$rb$$$ we need are equal to $$$rg_l$$$ and $$$lf_r$$$, respectively. If $$$rg_l > lf_r$$$, then the changed segment is degenerate (and this means that the string does not change at all). We can define some special segment for this type of strings, for example, $$$(-1, -1)$$$. Otherwise, the segment $$$(rg_l, lf_r)$$$ of the string will change.\nTime complexity: $$$O(n \\log{n})$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1849-D", "name": "Codeforces: Array Painting"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Array Painting (Rating: 1700)\nTags: constructive algorithms, greedy, two pointers\n\nYou are given an array of $n$ integers, where each integer is either $0$, $1$, or $2$. Initially, each element of the array is blue.\n\nYour goal is to paint each element of the array red. In order to do so, you can perform operations of two types:\n\n- pay one coin to choose a blue element and paint it red;\n- choose a red element which is not equal to $0$ and a blue element adjacent to it, decrease the chosen red element by $1$, and paint the chosen blue element red.\n\nWhat is the minimum number of coins you have to spend to achieve your goal?\n\nInput: The first line contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$).\n\nThe second line contains $n$ integers $a_1, a_2, \\dots, a_n$ ($0 \\le a_i \\le 2$).\n\nOutput: Print one integer — the minimum number of coins you have to spend in order to paint all elements red.\n\nExamples:\nInput: 3\n0 2 0\nOutput: 1\n\nInput: 4\n0 0 1 1\nOutput: 2\n\nInput: 7\n0 1 0 0 1 0 2\nOutput: 4\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Suppose we used a second operation as follows: we decreased a red element $$$x$$$, and painted another element $$$y$$$ red. Let's then say that $$$x$$$ is the parent of $$$y$$$.\nFurthermore, let's say that the element $$$x$$$ controls the element $$$y$$$ if one of the two conditions applies:\n• $$$x$$$ is the parent of $$$y$$$;\n• $$$x$$$ controls the parent of $$$y$$$.\nNow, suppose we used coins to paint some elements red. For each of those elements, there exists a segment of red elements which it controls. So, the problem can actually be reformulated as follows: we want to split the given array into the minimum number of segments so that each segment can be painted using one coin. Let's call a segment of the array good if it can be painted using only one coin.\nTo continue with our solution, we need the following property: if a segment is good, then its every subsegment is also good. This is kinda intuitive, but if you are interested in a formal proof, you can read the following paragraph.\nFormal proof: there are two main cases we need to consider: either the element that controls the main segment belongs to the subsegment we analyze, or it does not. In the first case, the proof is simple: we can paint all elements of the subsegment red using just one coin in the same way as we painted the whole segment, by starting from the element that controls it. In the second case, it is a bit more difficult, but instead of the element controlling the main segment, we can either start from the leftmost element of the subsegment, or from the rightmost element of the subsegment — depending on whether this subsegment is to the right or to the left of the element controlling the whole segment. This starting element will be painted red by spending a coin, and every other element of the subsegment can be painted red in the same way we painted the whole segment.\nSince if a segment is good, its every subsegment is also good, we can use the following greedy approach to solve the problem: start with the segment which contains only the first element of the array, and expand it to the right until it becomes bad. When the segment is no longer good, we need to start a new segment, which we will again expand to the right until it becomes bad, and so on. Then each element of the array will be considered only once. If we design a way to determine if the segment is still good when we add a new element to it in $$$O(1)$$$, our solution will work in $$$O(n)$$$.\nAll that's left is to analyze how to check if the segment is good. There are multiple ways to do this. The way used in the model solution employs the following ideas:\n• there cannot be any zeroes in the middle of the segment, since if we start painting red from the left of that zero, we cannot reach the elements to the right of that zero, and vice versa;\n• if there are no zeroes in the middle, and at least one of the endpoints is not zero, the segment is good because we can just start from that endpoint and expand to the other endpoint;\n• if there are no zeroes in the middle, and there is at least one element equal to $$$2$$$, we can start by painting that element red and expand to the left and to the right of it until we arrive at the borders of the segment;\n• and if there are zeroes at both endpoints, but all other elements are $$$1$$$'s, it's easy to see that paining the whole segment using only one coin is impossible: the sum of elements on the segment is $$$k-2$$$, where $$$k$$$ is the length of the segment, but we need to paint at least $$$k-1$$$ elements red without spending coins.\nAll of these ideas allow us to verify that the segment is good using just a couple if-statements.\nSolution complexity: $$$O(n)$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1849-E", "name": "Codeforces: Max to the Right of Min"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Max to the Right of Min (Rating: 2300)\nTags: binary search, data structures, divide and conquer, dp, dsu, two pointers\n\nYou are given a permutation $p$ of length $n$ — an array, consisting of integers from $1$ to $n$, all distinct.\n\nLet $p_{l,r}$ denote a subarray — an array formed by writing down elements from index $l$ to index $r$, inclusive.\n\nLet $\\mathit{maxpos}_{l,r}$ denote the index of the maximum element on $p_{l,r}$. Similarly, let $\\mathit{minpos}_{l,r}$ denote the index of the minimum element on it.\n\nCalculate the number of subarrays $p_{l,r}$ such that $\\mathit{maxpos}_{l,r} > \\mathit{minpos}_{l,r}$.\n\nInput: The first line contains a single integer $n$ ($1 \\le n \\le 10^6$) — the number of elements in the permutation.\n\nThe second line contains $n$ integers $p_1, p_2, \\dots, p_n$ ($1 \\le p_i \\le n$). All $p_i$ are distinct.\n\nOutput: Print a single integer — the number of subarrays $p_{l,r}$ such that $\\mathit{maxpos}_{l,r} > \\mathit{minpos}_{l,r}$.\n\nExamples:\nInput: 3\n1 2 3\nOutput: 3\n\nInput: 6\n5 3 6 1 4 2\nOutput: 4\n\nInput: 10\n5 1 6 2 8 3 4 10 9 7\nOutput: 38\n"}}
{"type": "message", "message": {"role": "assistant", "content": "The problem was originally prepared as part of the lecture on a monotonic stack. Thus, I will omit its explanation.\nFirst, recall a common technique of counting all segments satisfying some property. You can count the segments that have the same right border at the same time.\nConsider all segments $$$[l, r]$$$ with a fixed $$$r$$$. How do the minimums and the maximums on them change from each other? If you look at the segments in the order of decreasing $$$l$$$, we can write down the sequence of the following events: the minimum becomes smaller or the maximum becomes larger. In fact, we can maintain both of these types with two monotonic stacks.\nNow, which $$$l$$$ correspond to good segments with regard to the events? Consider two adjacent events $$$(i_1, t_1)$$$ and $$$(i_2, t_2)$$$, where $$$i_j$$$ is the index of the event and $$$t_j$$$ is the type ($$$0$$$ for min, $$$1$$$ for max). It's easy to see that if $$$t_2 = 0$$$, then all segments that have $$$l$$$ from $$$i_1 + 1$$$ to $$$i_2$$$ are good. It means that, while going from right to left, the last event we encountered was the minimum getting smaller. Thus, the index of the minimum becomes to the left from the index of the maximum.\nAs for the implementation, we will maintain these events in a set of pairs: (index of the event, type of the event). This way, it's not that hard to maintain the sum of distances from each event of type $$$0$$$ to the left to the previous event.\nWhen you erase an event, only a few distances can be affected: the distance from the next one to the current one, from the current one to the previous one and the newly created distance from the next one to the previous one. Just check for the types. When you add an event, you only add it to the very end of the set, so it's trivial to recalculate.\nThat will be $$$O(n \\log n)$$$ just from the set, the monotonic stacks by themselves are linear.\nYou can optimize this solution to $$$O(n)$$$ by using a doubly-linked list, but it really was not necessary for the problem.\nThere's also a different solution for which you can maintain the intervals of good values $$$l$$$ explicitly. First, compress them in such a way that the segments don't touch at borders. Now, you can notice that by going from $$$r$$$ to $$$r+1$$$ we can only affect the rightmost ones of them: possibly remove some, then change the last one and add a new one. So we can actually simulate this behavior with another stack. The details are left as an exercise to a reader. With the correct implementation, this solution will be $$$O(n)$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1849-F", "name": "Codeforces: XOR Partition"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: XOR Partition (Rating: 2700)\nTags: binary search, bitmasks, data structures, divide and conquer, greedy, trees\n\nFor a set of integers $S$, let's define its cost as the minimum value of $x \\oplus y$ among all pairs of different integers from the set (here, $\\oplus$ denotes bitwise XOR). If there are less than two elements in the set, its cost is equal to $2^{30}$.\n\nYou are given a set of integers $\\{a_1, a_2, \\dots, a_n\\}$. You have to partition it into two sets $S_1$ and $S_2$ in such a way that every element of the given set belongs to exactly one of these two sets. The value of the partition is the minimum among the costs of $S_1$ and $S_2$.\n\nFind the partition with the maximum possible value.\n\nInput: The first line contains $n$ ($2 \\le n \\le 200000$) — the number of elements in the set.\n\nThe second line contains $n$ distinct integers $a_1$, $a_2$, ..., $a_n$ ($0 \\le a_i < 2^{30}$) — the elements of the set.\n\nOutput: Print a string $n$ characters 0 and/or 1 describing the partition as follows: the $i$-th character should be 0 if the element $a_i$ belongs to $S_1$, otherwise, that character should be 1.\n\nIf there are multiple optimal answers, print any of them.\n\nExamples:\nInput: 5\n42 13 1337 37 152\nOutput: 10001\n\nInput: 4\n1 2 3 4\nOutput: 1100\n\nInput: 2\n1 2\nOutput: 10\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Disclaimer: the model solution to this problem is a bit more complicated than most of the solutions written by participants, but I will still use it for the editorial so that I can explain some of the classical techniques appearing in it.\nSuppose we have built a graph on $$$n$$$ vertices, where each pair of vertices is connected by an edge, and the weight of the edge connecting $$$i$$$ with $$$j$$$ is $$$a_i \\oplus a_j$$$. If we treat the partition of the set as a coloring of this graph, then the cost of the partition is equal to the minimum weight of an edge connecting two vertices of the same color. So, suppose we want to check that the answer is at least $$$x$$$. It means that every edge with weight less than $$$x$$$ should connect two vertices of different colors; so, the graph where we erase all edges with weight greater than or equal to $$$x$$$ should be bipartite. This allows us to write a binary search solution if we somehow understand how to construct the graph and check that it is bipartite implicitly, but this is not the way the model solution goes.\nInstead, suppose we do the following greedy. Initially, let the graph be empty. Then, we add all edges with weight $$$1$$$, and check if it is bipartite. Then, we add all edges with weight $$$2$$$, and check if it is bipartite. And so on, until the graph is no longer bipartite.\nOf course, there are $$$O(n^2)$$$ edges, so this is too slow. But in fact, we don't need all the edges for this graph. Some of the edges we added didn't really affect the coloring of the graph: if, in this process, we add an edge which connects two vertices from the same component, one of the following two things happens:\n• if it connects two vertices of the same color, then the graph is no longer bipartite;\n• otherwise, this edge actually does not change anything in the coloring.\nNow, suppose we stop before making the graph non-bipartite. It means if an edge we added was connecting two vertices from the same component, that edge was actually redundant.\nLet's take a look again at what we're actually doing. Initially, the graph is empty. Then, we consider all possible edges in ascending order of their weights; if an edge connects two vertices in different components, it should be added to the graph (and it affects the coloring), otherwise, it does not matter (unless it makes us stop the algorithm, since the graph is no longer bipartite). Doesn't it sound familiar? Yup, it is almost like Kruskal's algorithm.\nAnd in fact, the problem can be solved as follows: build any MST of this graph, and use it to form the two-coloring (paint the vertices in such a way that every edge from MST connects two vertices of different colors). The paragraph below contains the formal proof that this coloring is optimal; feel free to skip it if you're not interested in the strict proof.\nFormal proof: suppose that, if we color the vertices using the MST, we get an answer equal to $$$x$$$. This means that there are two vertices $$$i$$$ and $$$j$$$ such that their colors are the same, and $$$a_i \\oplus a_j = x$$$. Let us consider the cycle formed by the edge from $$$i$$$ to $$$j$$$ and the path from $$$j$$$ to $$$i$$$ in the MST. Since these two vertices have the same colors, this cycle contains an odd number of edges — so, at least one edge on this cycle will connect vertices of the same color no matter how we paint the graph. And there are no edges on this cycle with weight greater than $$$x$$$ — otherwise, that edge would be replaced by the edge $$$(i,j)$$$ in the MST. So, at least one edge with weight $$$\\le x$$$ will be connecting two vertices of the same color, and thus the answer cannot be greater than $$$x$$$.\nOkay, now we have to actually build the MST in this graph. XOR-MST is a fairly classical problem (it was even used in one of the previous Educational Rounds several years ago). I know two different solutions to it: the one based on Boruvka's algorithm and the D&C + trie merging method. I will describe the former one.\nThe classical Boruvka's algorithm is one of the algorithms of finding a minimum spanning tree. It begins with a graph containing no edges, and performs several iterations until it becomes connected. On each iteration, the algorithm considers each component of the graph and adds the minimum edge connecting any vertex from this component with any vertex outside this component (these edges are added at the same time for all components of the graph, so be careful about creating a cycle or adding the same edge twice; different methods can be used to prevent this — in the model solution, I memorize the edges I want to add on each iteration, then use a DSU to actually check which ones should be added without causing a cycle to appear).\nDuring each iteration of the algorithm, the number of components decreases to at least half of the original number, so the algorithm works in $$$O(I \\log n)$$$, where $$$I$$$ is the complexity of one iteration. Usually, $$$I$$$ is $$$O(m)$$$ since we need to consider each edge of the graph twice; but in this problem, we can perform one iteration of Boruvka's algorithm much faster, in $$$O(n \\log A)$$$, where $$$A$$$ is the constraint on the numbers in the input.\nIt can be done as follows: maintain a trie data structure which stores all values of $$$a_i$$$ and allows processing a query \"given an integer $$$x$$$, find the value $$$y$$$ in the data structure such that $$$x \\oplus y$$$ is the minimum possible\" (this can be done with a trie descent). When we consider a component and try to find the minimum edge leading outside of it, we first delete all vertices belonging to this component from the data structure; then, for each vertex from the component, find the best vertex in the data structure; and then, insert all vertices back. That way, a component of size $$$V$$$ will be processed in $$$O(V \\log A)$$$, and the total time for one iteration is $$$O(n \\log A)$$$.\nAll of this results in a solution with complexity $$$O(n \\log n \\log A)$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1850-A", "name": "Codeforces: To My Critics"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: To My Critics (Rating: 800)\nTags: implementation, sortings\n\nSuneet has three digits $a$, $b$, and $c$.\n\nSince math isn't his strongest point, he asks you to determine if you can choose any two digits to make a sum greater or equal to $10$.\n\nOutput \"YES\" if there is such a pair, and \"NO\" otherwise.\n\nInput: The first line contains a single integer $t$ ($1 \\leq t \\leq 1000$) — the number of test cases.\n\nThe only line of each test case contains three digits $a$, $b$, $c$ ($0 \\leq a, b, c \\leq 9$).\n\nOutput: For each test case, output \"YES\" if such a pair exists, and \"NO\" otherwise.\n\nYou can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).\n\nExamples:\nInput: 5\n8 1 2\n4 4 5\n9 9 9\n0 0 0\n8 5 3\nOutput: YES\nNO\nYES\nNO\nYES\n"}}
{"type": "message", "message": {"role": "assistant", "content": "One way to solve the problem is to check if $$$a + b + c - min(a, b, c) \\geq 10$$$ using an if statement."}}1234{"type": "session", "harness": "opencode", "id": "cf-1850-B", "name": "Codeforces: Ten Words of Wisdom"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Ten Words of Wisdom (Rating: 800)\nTags: implementation, sortings\n\nIn the game show \"Ten Words of Wisdom\", there are $n$ participants numbered from $1$ to $n$, each of whom submits one response. The $i$-th response is $a_i$ words long and has quality $b_i$. No two responses have the same quality, and at least one response has length at most $10$.\n\nThe winner of the show is the response which has the highest quality out of all responses that are not longer than $10$ words. Which response is the winner?\n\nInput: The first line contains a single integer $t$ ($1 \\leq t \\leq 100$) — the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($1 \\leq n \\leq 50$) — the number of responses.\n\nThen $n$ lines follow, the $i$-th of which contains two integers $a_i$ and $b_i$ ($1 \\leq a_i, b_i \\leq 50$) — the number of words and the quality of the $i$-th response, respectively.\n\nAdditional constraints on the input: in each test case, at least one value of $i$ satisfies $a_i \\leq 10$, and all values of $b_i$ are distinct.\n\nOutput: For each test case, output a single line containing one integer $x$ ($1 \\leq x \\leq n$) — the winner of the show, according to the rules given in the statement.\n\nIt can be shown that, according to the constraints in the statement, exactly one winner exists for each test case.\n\nExamples:\nInput: 3\n5\n7 2\n12 5\n9 3\n9 4\n10 1\n3\n1 2\n3 4\n5 6\n1\n1 43\nOutput: 4\n3\n1\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's iterate through all responses: if it has $$$> 10$$$ words, ignore it. Otherwise, keep track of the maximum quality and its index, and update it as we go along. Then output the index with maximum quality. The time complexity is $$$\\mathcal{O}(n)$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1850-C", "name": "Codeforces: Word on the Paper"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Word on the Paper (Rating: 800)\nTags: implementation, strings\n\nOn an $8 \\times 8$ grid of dots, a word consisting of lowercase Latin letters is written vertically in one column, from top to bottom. What is it?\n\nInput: The input consists of multiple test cases. The first line of the input contains a single integer $t$ ($1 \\leq t \\leq 1000$) — the number of test cases.\n\nEach test case consists of $8$ lines, each containing $8$ characters. Each character in the grid is either $\\texttt{.}$ (representing a dot) or a lowercase Latin letter ($\\texttt{a}$–$\\texttt{z}$).\n\nThe word lies entirely in a single column and is continuous from the beginning to the ending (without gaps). See the sample input for better understanding.\n\nOutput: For each test case, output a single line containing the word made up of lowercase Latin letters ($\\texttt{a}$–$\\texttt{z}$) that is written vertically in one column from top to bottom.\n\nExamples:\nInput: 5\n........\n........\n........\n........\n...i....\n........\n........\n........\n........\n.l......\n.o......\n.s......\n.t......\n........\n........\n........\n........\n........\n........\n........\n......t.\n......h.\n......e.\n........\n........\n........\n........\n........\n.......g\n.......a\n.......m\n.......e\na.......\na.......\na.......\na.......\na.......\na.......\na.......\na.......\nOutput: i\nlost\nthe\ngame\naaaaaaaa\n"}}
{"type": "message", "message": {"role": "assistant", "content": "You can iterate through the grid and then once you find a letter, iterate downwards until you get the whole word.\nHowever there is an approach that is even faster to code: just input each character, and output it if it is not a dot. This works because we will input the characters in the same order from top to bottom. The complexity is $$$\\mathcal{O}(1)$$$ regardless."}}1234{"type": "session", "harness": "opencode", "id": "cf-1850-D", "name": "Codeforces: Balanced Round"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Balanced Round (Rating: 900)\nTags: brute force, greedy, implementation, sortings\n\nYou are the author of a Codeforces round and have prepared $n$ problems you are going to set, problem $i$ having difficulty $a_i$. You will do the following process:\n\n- remove some (possibly zero) problems from the list;\n- rearrange the remaining problems in any order you wish.\n\nA round is considered balanced if and only if the absolute difference between the difficulty of any two consecutive problems is at most $k$ (less or equal than $k$).\n\nWhat is the minimum number of problems you have to remove so that an arrangement of problems is balanced?\n\nInput: The first line contains a single integer $t$ ($1 \\leq t \\leq 1000$) — the number of test cases.\n\nThe first line of each test case contains two positive integers $n$ ($1 \\leq n \\leq 2 \\cdot 10^5$) and $k$ ($1 \\leq k \\leq 10^9$) — the number of problems, and the maximum allowed absolute difference between consecutive problems.\n\nThe second line of each test case contains $n$ space-separated integers $a_i$ ($1 \\leq a_i \\leq 10^9$) — the difficulty of each problem.\n\nNote that the sum of $n$ over all test cases doesn't exceed $2 \\cdot 10^5$.\n\nOutput: For each test case, output a single integer — the minimum number of problems you have to remove so that an arrangement of problems is balanced.\n\nExamples:\nInput: 7\n5 1\n1 2 4 5 6\n1 2\n10\n8 3\n17 3 1 20 12 5 17 12\n4 2\n2 4 6 8\n5 3\n2 3 19 10 8\n3 4\n1 10 5\n8 1\n8 3 1 4 5 10 7 3\nOutput: 2\n0\n5\n0\n3\n1\n4\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's calculate the maximum number of problems we can take, and the answer will be $$$n$$$ subtracted by that count.\nAn arrangement that always minimizes the absolute difference between adjacent pairs is the array in sorted order. What we notice, is that if the array is sorted, we will always take a subarray (all taken elements will be consecutive).\nSo, the problem converts to finding the largest subarray for which $$$a_i - a_{i-1} \\leq k$$$. It's easy to see that all the subarrays are totally different (don't share any intersection of elements), thus, we can maintain a count variable of the current number of elements in the current subarray, and iterate through array elements from left to right. If we currently are at $$$i$$$ and $$$a_i - a_{i-1} > k$$$ then we just set the count to $$$1$$$ since we know a new subarray starts, otherwise, we just increase our count by $$$1$$$. The answer will be $$$n$$$ subtracted by the largest value that our count has achieved."}}1234{"type": "session", "harness": "opencode", "id": "cf-1850-E", "name": "Codeforces: Cardboard for Pictures"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Cardboard for Pictures (Rating: 1100)\nTags: binary search, geometry, implementation, math\n\nMircea has $n$ pictures. The $i$-th picture is a square with a side length of $s_i$ centimeters.\n\nHe mounted each picture on a square piece of cardboard so that each picture has a border of $w$ centimeters of cardboard on all sides. In total, he used $c$ square centimeters of cardboard. Given the picture sizes and the value $c$, can you find the value of $w$?\n\nA picture of the first test case. Here $c = 50 = 5^2 + 4^2 + 3^2$, so $w=1$ is the answer.\n\nPlease note that the piece of cardboard goes behind each picture, not just the border.\n\nInput: The first line contains a single integer $t$ ($1 \\leq t \\leq 1000$) — the number of test cases.\n\nThe first line of each test case contains two positive integers $n$ ($1 \\leq n \\leq 2 \\cdot 10^5$) and $c$ ($1 \\leq c \\leq 10^{18}$) — the number of paintings, and the amount of used square centimeters of cardboard.\n\nThe second line of each test case contains $n$ space-separated integers $s_i$ ($1 \\leq s_i \\leq 10^4$) — the sizes of the paintings.\n\nThe sum of $n$ over all test cases doesn't exceed $2 \\cdot 10^5$.\n\nAdditional constraint on the input: Such an integer $w$ exists for each test case.\n\nPlease note, that some of the input 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++).\n\nOutput: For each test case, output a single integer — the value of $w$ ($w \\geq 1$) which was used to use exactly $c$ squared centimeters of cardboard.\n\nExamples:\nInput: 10\n3 50\n3 2 1\n1 100\n6\n5 500\n2 2 2 2 2\n2 365\n3 4\n2 469077255466389\n10000 2023\n10 635472106413848880\n9181 4243 7777 1859 2017 4397 14 9390 2245 7225\n7 176345687772781240\n9202 9407 9229 6257 7743 5738 7966\n14 865563946464579627\n3654 5483 1657 7571 1639 9815 122 9468 3079 2666 5498 4540 7861 5384\n19 977162053008871403\n9169 9520 9209 9013 9300 9843 9933 9454 9960 9167 9964 9701 9251 9404 9462 9277 9661 9164 9161\n18 886531871815571953\n2609 10 5098 9591 949 8485 6385 4586 1064 5412 6564 8460 2245 6552 5089 8353 3803 3764\nOutput: 1\n2\n4\n5\n7654321\n126040443\n79356352\n124321725\n113385729\n110961227\n"}}
{"type": "message", "message": {"role": "assistant", "content": "The key idea is to binary search on the answer. If you don't know what that is, you should read this Codeforces EDU article. Let's make a function $$$f(x)$$$, which tells us the total area of cardboard if we use a width of $$$x$$$. Then you can see that we can calculate $$$f(x)$$$ in $$$\\mathcal{O}(n)$$$ time as $$$(a_1 + 2x)^2 + (a_2 + 2x)^2 + \\dots + (a_n + 2x)^2$$$, because the side length of the $$$i$$$-th cardboard is $$$a_i + 2x$$$.\nSo this means that now we can binary search on the answer: let's find the largest $$$w$$$ so that $$$f(w) \\leq c$$$. The maximum theoretical value of $$$w$$$ can be seen not to exceed $$$10^9$$$, since $$$c$$$ is not more than $$$10^{18}$$$ (you can set an even lower bound).\nA quick note about implementation: the value of $$$f(x)$$$ can exceed 64-bit numbers, so you need to exit the function as soon as you get a value greater than $$$c$$$, or else you risk overflow.\nSo the time complexity is $$$\\mathcal{O}(n \\log(10^9))$$$ per test case, which is equal to $$$\\mathcal{O}(n)$$$ with some constant factor. It's not that big to make it fail.\nYou can also use the quadratic formula, but be careful about implementation of square root and precision issues."}}1234{"type": "session", "harness": "opencode", "id": "cf-1850-F", "name": "Codeforces: We Were Both Children"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: We Were Both Children (Rating: 1300)\nTags: brute force, implementation, math, number theory\n\nMihai and Slavic were looking at a group of $n$ frogs, numbered from $1$ to $n$, all initially located at point $0$. Frog $i$ has a hop length of $a_i$.\n\nEach second, frog $i$ hops $a_i$ units forward. Before any frogs start hopping, Slavic and Mihai can place exactly one trap in a coordinate in order to catch all frogs that will ever pass through the corresponding coordinate.\n\nHowever, the children can't go far away from their home so they can only place a trap in the first $n$ points (that is, in a point with a coordinate between $1$ and $n$) and the children can't place a trap in point $0$ since they are scared of frogs.\n\nCan you help Slavic and Mihai find out what is the maximum number of frogs they can catch using a trap?\n\nInput: The first line of the input contains a single integer $t$ ($1 \\le t \\le 100$) — the number of test cases. The description of test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\leq n \\leq 2 \\cdot 10^5$) — the number of frogs, which equals the distance Slavic and Mihai can travel to place a trap.\n\nThe second line of each test case contains $n$ integers $a_1, \\ldots, a_n$ ($1 \\leq a_i \\leq 10^9$) — the lengths of the hops of the corresponding frogs.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput: For each test case output a single integer — the maximum number of frogs Slavic and Mihai can catch using a trap.\n\nExamples:\nInput: 7\n5\n1 2 3 4 5\n3\n2 2 2\n6\n3 1 3 4 9 10\n9\n1 3 2 4 2 3 7 8 5\n1\n10\n8\n7 11 6 8 12 4 4 8\n10\n9 11 9 12 1 7 2 5 8 10\nOutput: 3\n3\n3\n5\n0\n4\n4\n"}}
{"type": "message", "message": {"role": "assistant", "content": "We disregard any $$$a_i$$$ larger than $$$n$$$ since we can't catch them anyway. We keep in $$$cnt_i$$$ how many frogs we have for each hop distance. We go through each $$$i$$$ from $$$1$$$ to $$$n$$$ and add $$$cnt_i$$$ to every multiple of $$$i$$$ smaller or equal to $$$n$$$. This action is a harmonic series and takes $$$O(nlogn)$$$ time. We go through all from $$$1$$$ to $$$n$$$ and take the maximum."}}1234{"type": "session", "harness": "opencode", "id": "cf-1850-G", "name": "Codeforces: The Morning Star"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: The Morning Star (Rating: 1500)\nTags: combinatorics, data structures, geometry, implementation, math, sortings\n\nA compass points directly toward the morning star. It can only point in one of eight directions: the four cardinal directions (N, S, E, W) or some combination (NW, NE, SW, SE). Otherwise, it will break.\n\nThe directions the compass can point.\n\nThere are $n$ distinct points with integer coordinates on a plane. How many ways can you put a compass at one point and the morning star at another so that the compass does not break?\n\nInput: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \\le t \\le 10^4$). The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2 \\leq n \\leq 2 \\cdot 10^5$) — the number of points.\n\nThen $n$ lines follow, each line containing two integers $x_i$, $y_i$ ($-10^9 \\leq x_i, y_i \\leq 10^9$) — the coordinates of each point, all points have distinct coordinates.\n\nIt is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \\cdot 10^5$.\n\nOutput: For each test case, output a single integer — the number of pairs of points that don't break the compass.\n\nExamples:\nInput: 5\n3\n0 0\n-1 -1\n1 1\n4\n4 5\n5 7\n6 9\n10 13\n3\n-1000000000 1000000000\n0 0\n1000000000 -1000000000\n5\n0 0\n2 2\n-1 5\n-1 10\n2 11\n3\n0 0\n-1 2\n1 -2\nOutput: 6\n2\n6\n8\n0\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's look at four directions of the line connecting the compass and morning star: vertical, horizontal, with slope $$$1$$$ (looks like /), and with slope $$$-1$$$ (looks like \\).\n• vertical: the two points need to have the same $$$y$$$-coordinate. If there are $$$k$$$ points with the same $$$y$$$-coordinate, then how many pairs are possible for the morning star and compass? Well, there are $$$k$$$ possibilities for the compass, and $$$k-1$$$ for the morning star, so there are a total of $$$k(k-1)$$$ valid pairs. In this case, we can use a data structure like a C++ map to count the number of points at each $$$y$$$-coordinate, and add $$$k(k-1)$$$ to the total for each $$$k$$$ in the map.\n• horizontal: the two points need to have the same $$$x$$$-coordinate. Similarly, we count pairs with the same $$$x$$$-coordinate using a map.\n• slope $$$1$$$: note that all lines of this form can be written as $$$x-y=c$$$ for a constant $$$c$$$. (Draw some examples out for $$$c=-1, 0, 1$$$.) So we can use a map to count values of $$$x-y$$$, and add to the total.\n• slope $$$-1$$$: similarly, all such lines can be written as $$$x+y=c$$$ for a constant $$$c$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1850-H", "name": "Codeforces: The Third Letter"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: The Third Letter (Rating: 1700)\nTags: dfs and similar, dsu, graphs, greedy, implementation\n\nIn order to win his toughest battle, Mircea came up with a great strategy for his army. He has $n$ soldiers and decided to arrange them in a certain way in camps. Each soldier has to belong to exactly one camp, and there is one camp at each integer point on the $x$-axis (at points $\\cdots, -2, -1, 0, 1, 2, \\cdots$).\n\nThe strategy consists of $m$ conditions. Condition $i$ tells that soldier $a_i$ should belong to a camp that is situated $d_i$ meters in front of the camp that person $b_i$ belongs to. (If $d_i < 0$, then $a_i$'s camp should be $-d_i$ meters behind $b_i$'s camp.)\n\nNow, Mircea wonders if there exists a partition of soldiers that respects the condition and he asks for your help! Answer \"YES\" if there is a partition of the $n$ soldiers that satisfies all of the $m$ conditions and \"NO\" otherwise.\n\nNote that two different soldiers may be placed in the same camp.\n\nInput: The first line contains a single integer $t$ ($1 \\leq t \\leq 100$) — the number of test cases.\n\nThe first line of each test case contains two positive integers $n$ and $m$ ($2 \\leq n \\leq 2 \\cdot 10^5$; $1 \\leq m \\leq n$) — the number of soldiers, and the number of conditions respectively.\n\nThen $m$ lines follow, each of them containing $3$ integers: $a_i$, $b_i$, $d_i$ ($a_i \\neq b_i$; $1 \\leq a_i, b_i \\leq n$; $-10^9 \\leq d_i \\leq 10^9$) — denoting the conditions explained in the statement. Note that if $d_i$ is positive, $a_i$ should be $d_i$ meters in front of $b_i$ and if it is negative, $a_i$ should be $-d_i$ meters behind $b_i$.\n\nNote that the sum of $n$ over all test cases doesn't exceed $2 \\cdot 10^5$.\n\nOutput: For each test case, output \"YES\" if there is an arrangement of the $n$ soldiers that satisfies all of the $m$ conditions and \"NO\" otherwise.\n\nExamples:\nInput: 4\n5 3\n1 2 2\n2 3 4\n4 2 -6\n6 5\n1 2 2\n2 3 4\n4 2 -6\n5 4 4\n3 5 100\n2 2\n1 2 5\n1 2 4\n4 1\n1 2 3\nOutput: YES\nNO\nNO\nYES\n"}}
{"type": "message", "message": {"role": "assistant", "content": "We can view the conditions and soldiers as a directed graph. The soldiers represent nodes and the conditions represent directed edges. Saying $$$a_i$$$ should be $$$d_i$$$ meters in front of $$$b_i$$$ is equivalent to adding two weighted directed edges:\n• An edge from $$$a_i$$$ to $$$b_i$$$ with weight $$$d_i$$$.\n• An edge from $$$b_i$$$ to $$$a_i$$$ with weight $$$-d_i$$$.\nNow, we iterate over all $$$n$$$ soldiers, and do a standard dfs whenever we encounter an unvisited soldier, assigning coordinates respecting the weights of the edges. That is, for the first soldier in the set we can just set his coordinate to $$$0$$$, then for every neighbor we visit we set it's coordinate to the coordinate of the current node added by the weight of the edge between it and its neighbor.\nFinally, we check at the end if all of the $$$m$$$ conditions are satisfied."}}1234{"type": "session", "harness": "opencode", "id": "cf-1851-A", "name": "Codeforces: Escalator Conversations"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Escalator Conversations (Rating: 800)\nTags: brute force, constructive algorithms, math\n\nOne day, Vlad became curious about who he can have a conversation with on the escalator in the subway. There are a total of $n$ passengers. The escalator has a total of $m$ steps, all steps indexed from $1$ to $m$ and $i$-th step has height $i \\cdot k$.\n\nVlad's height is $H$ centimeters. Two people with heights $a$ and $b$ can have a conversation on the escalator if they are standing on different steps and the height difference between them is equal to the height difference between the steps.\n\nFor example, if two people have heights $170$ and $180$ centimeters, and $m = 10, k = 5$, then they can stand on steps numbered $7$ and $5$, where the height difference between the steps is equal to the height difference between the two people: $k \\cdot 2 = 5 \\cdot 2 = 10 = 180 - 170$. There are other possible ways.\n\nGiven an array $h$ of size $n$, where $h_i$ represents the height of the $i$-th person. Vlad is interested in how many people he can have a conversation with on the escalator individually.\n\nFor example, if $n = 5, m = 3, k = 3, H = 11$, and $h = [5, 4, 14, 18, 2]$, Vlad can have a conversation with the person with height $5$ (Vlad will stand on step $1$, and the other person will stand on step $3$) and with the person with height $14$ (for example, Vlad can stand on step $3$, and the other person will stand on step $2$). Vlad cannot have a conversation with the person with height $2$ because even if they stand on the extreme steps of the escalator, the height difference between them will be $6$, while their height difference is $9$. Vlad cannot have a conversation with the rest of the people on the escalator, so the answer for this example is $2$.\n\nInput: The first line contains a single integer $t$ ($1 \\le t \\le 1000$) — the number of test cases.\n\nThen the descriptions of the test cases follow.\n\nThe first line of each test case contains integers: $n, m, k, H$ ($1 \\le n,m \\le 50$, $1 \\le k,H \\le 10^6$). Here, $n$ is the number of people, $m$ is the number of steps, $k$ is the height difference between neighboring steps, and $H$ is Vlad's height.\n\nThe second line contains $n$ integers: $h_1, h_2, \\ldots, h_n$ ($1 \\le h_i \\le 10^6$). Here, $h_i$ represents the height of the $i$-th person.\n\nOutput: For each test case, output a single integer — the number of people Vlad can have a conversation with on the escalator individually.\n\nExamples:\nInput: 7\n5 3 3 11\n5 4 14 18 2\n2 9 5 6\n11 9\n10 50 3 11\n43 44 74 98 62 60 99 4 11 73\n4 8 8 49\n68 58 82 73\n7 1 4 66\n18 66 39 83 48 99 79\n9 1 1 13\n26 23 84 6 60 87 40 41 25\n6 13 3 28\n30 70 85 13 1 55\nOutput: 2\n1\n4\n1\n0\n0\n3\n"}}
{"type": "message", "message": {"role": "assistant", "content": "For each person in the array $$$h$$$, we will check the conditions. First, the height should not be the same as Vlad's height, then their difference should be divisible by $$$k$$$, and finally, the difference between the extreme steps should not exceed the difference in height. If these requirements are met, there will be steps where Vlad can talk to a person with that height."}}1234{"type": "session", "harness": "opencode", "id": "cf-1851-B", "name": "Codeforces: Parity Sort"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Parity Sort (Rating: 800)\nTags: greedy, sortings, two pointers\n\nYou have an array of integers $a$ of length $n$. You can apply the following operation to the given array:\n\n- Swap two elements $a_i$ and $a_j$ such that $i \\neq j$, $a_i$ and $a_j$ are either both even or both odd.\n\nDetermine whether it is possible to sort the array in non-decreasing order by performing the operation any number of times (possibly zero).\n\nFor example, let $a$ = [$7, 10, 1, 3, 2$]. Then we can perform $3$ operations to sort the array:\n\n1. Swap $a_3 = 1$ and $a_1 = 7$, since $1$ and $7$ are odd. We get $a$ = [$1, 10, 7, 3, 2$];\n2. Swap $a_2 = 10$ and $a_5 = 2$, since $10$ and $2$ are even. We get $a$ = [$1, 2, 7, 3, 10$];\n3. Swap $a_4 = 3$ and $a_3 = 7$, since $3$ and $7$ are odd. We get $a$ = [$1, 2, 3, 7, 10$].\n\nInput: The first line of input data contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases.\n\nThe description of the test cases follows.\n\nThe first line of each test case contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) — the length of array $a$.\n\nThe second line of each test case contains exactly $n$ positive integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10^9$) — the elements of array $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput: For each test case, output on a separate line:\n\n- YES if the array can be sorted by applying the operation to it some number of times;\n- NO otherwise.\n\nYou can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).\n\nExamples:\nInput: 6\n5\n7 10 1 3 2\n4\n11 9 3 5\n5\n11 3 15 3 2\n6\n10 7 8 1 2 3\n1\n10\n5\n6 6 4 1 6\nOutput: YES\nYES\nNO\nNO\nYES\nNO\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's copy array $$$a$$$ to array $$$b$$$. Then sort array $$$b$$$. Let's check that for each $$$1 \\le i \\le n$$$ it is satisfied that $$$(a_i \\bmod 2) = (b_i \\bmod 2)$$$, where $$$\\bmod$$$ is the operation of taking the remainder from division. In other words, we need to check that in the sorted array $$$b$$$, the element at the $$$i$$$-th position has the same parity as the element at the $$$i$$$-th position in the unsorted array $$$a$$$.\nThis is true because any array can be sorted using at most $$$n^2$$$ operations, in which any two elements are swapped. Consequently, if the parity of the elements in the sorted array is preserved, then the even and odd subsequences of the elements can be sorted separately, and the answer is YES. If the parity of the elements is not preserved after sorting, the answer is NO."}}1234{"type": "session", "harness": "opencode", "id": "cf-1851-C", "name": "Codeforces: Tiles Comeback"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Tiles Comeback (Rating: 1000)\nTags: greedy\n\nVlad remembered that he had a series of $n$ tiles and a number $k$. The tiles were numbered from left to right, and the $i$-th tile had colour $c_i$.\n\nIf you stand on the first tile and start jumping any number of tiles right, you can get a path of length $p$. The length of the path is the number of tiles you stood on.\n\nVlad wants to see if it is possible to get a path of length $p$ such that:\n\n- it ends at tile with index $n$;\n- $p$ is divisible by $k$\n- the path is divided into blocks of length exactly $k$ each;\n- tiles in each block have the same colour, the colors in adjacent blocks are not necessarily different.\n\nFor example, let $n = 14$, $k = 3$.\n\nThe colours of the tiles are contained in the array $c$ = [$\\color{red}{1}, \\color{violet}{2}, \\color{red}{1}, \\color{red}{1}, \\color{gray}{7}, \\color{orange}{5}, \\color{green}{3}, \\color{green}{3}, \\color{red}{1}, \\color{green}{3}, \\color{blue}{4}, \\color{blue}{4}, \\color{violet}{2}, \\color{blue}{4}$]. Then we can construct a path of length $6$ consisting of $2$ blocks:\n\n$\\color{red}{c_1} \\rightarrow \\color{red}{c_3} \\rightarrow \\color{red}{c_4} \\rightarrow \\color{blue}{c_{11}} \\rightarrow \\color{blue}{c_{12}} \\rightarrow \\color{blue}{c_{14}}$\n\nAll tiles from the $1$-st block will have colour $\\color{red}{\\textbf{1}}$, from the $2$-nd block will have colour $\\color{blue}{\\textbf{4}}$.\n\nIt is also possible to construct a path of length $9$ in this example, in which all tiles from the $1$-st block will have colour $\\color{red}{\\textbf{1}}$, from the $2$-nd block will have colour $\\color{green}{\\textbf{3}}$, and from the $3$-rd block will have colour $\\color{blue}{\\textbf{4}}$.\n\nInput: The first line of input data contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases.\n\nThe description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $k$ ($1 \\le k \\le n \\le 2 \\cdot 10^5$)—the number of tiles in the series and the length of the block.\n\nThe second line of each test case contains $n$ integers $c_1, c_2, c_3, \\dots, c_n$ ($1 \\le c_i \\le n$) — the colours of the tiles.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput: For each test case, output on a separate line:\n\n- YES if you can get a path that satisfies these conditions;\n- NO otherwise.\n\nYou can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).\n\nExamples:\nInput: 10\n4 2\n1 1 1 1\n14 3\n1 2 1 1 7 5 3 3 1 3 4 4 2 4\n3 3\n3 1 3\n10 4\n1 2 1 2 1 2 1 2 1 2\n6 2\n1 3 4 1 6 6\n2 2\n1 1\n4 2\n2 1 1 1\n2 1\n1 2\n3 2\n2 2 2\n4 1\n1 1 2 2\nOutput: YES\nYES\nNO\nNO\nYES\nYES\nNO\nYES\nYES\nYES\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Since the path must start in the first tile and end in the last tile, it is enough to construct a path consisting of $$$1$$$ or $$$2$$$ blocks of length $$$k$$$ to solve the problem.\nIf $$$c_1 = c_n$$$, then we need to check that there are $$$k-2$$$ tiles of colour $$$c_0$$$ between the first and the last tile. If this condition is satisfied, then the tiles that are found together with the first and the last tile form the path $$$p$$$, and the answer is — YES. Otherwise — the answer is NO.\nIf $$$c_1 \\neq c_n$$$ we can solve the problem by the method of two pointers: let's move from the two ends of the array $$$c$$$ to the middle, counting the number of tiles of colour $$$c_1$$$ on the left and tiles of colour $$$c_n$$$ on the right. If the pointers meet no later than $$$k$$$ tiles of the desired colours are found on both sides, the answer is — YES, otherwise — NO."}}1234{"type": "session", "harness": "opencode", "id": "cf-1851-D", "name": "Codeforces: Prefix Permutation Sums"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Prefix Permutation Sums (Rating: 1300)\nTags: implementation, math\n\nYour friends have an array of $n$ elements, calculated its array of prefix sums and passed it to you, accidentally losing one element during the transfer. Your task is to find out if the given array can matches permutation.\n\nA permutation of $n$ elements is an array of $n$ numbers from $1$ to $n$ such that each number occurs exactly one times in it.\n\nThe array of prefix sums of the array $a$ — is such an array $b$ that $b_i = \\sum_{j=1}^i a_j, 1 \\le i \\le n$.\n\nFor example, the original permutation was $[1, 5, 2, 4, 3]$. Its array of prefix sums — $[1, 6, 8, 12, 15]$. Having lost one element, you can get, for example, arrays $[6, 8, 12, 15]$ or $[1, 6, 8, 15]$.\n\nIt can also be shown that the array $[1, 2, 100]$ does not correspond to any permutation.\n\nInput: The first line contains a positive number $t$ ($1 \\le t \\le 10^4$) — the number of test cases. The description of the test cases follows.\n\nThe first line of the description of each test case contains a positive number $n$ ($2 \\le n \\le 2 \\cdot 10^5$) — the size of the initial array.\n\nThe second line of the description of each test case contains $n - 1$ positive number $a_i$ ($1 \\le a_i \\le 10^{18}$), $a_{i-1} < a_i$ — elements of the array of prefix sums.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput: For each test case, output \"YES\" if such a permutation exists, and \"NO\" otherwise.\n\nYou can output \"YES\" and \"NO\" in any case (for example, the strings \"yEs\", \"yes\" and \"Yes\" will be recognized as a positive response).\n\nExamples:\nInput: 12\n5\n6 8 12 15\n5\n1 6 8 15\n4\n1 2 100\n4\n1 3 6\n2\n2\n3\n1 2\n4\n3 7 10\n5\n5 44 46 50\n4\n1 9 10\n5\n13 21 36 42\n5\n1 2 3 1000000000000000000\n9\n9 11 12 20 25 28 30 33\nOutput: YES\nYES\nNO\nYES\nYES\nNO\nYES\nNO\nNO\nNO\nNO\nNO\n"}}
{"type": "message", "message": {"role": "assistant", "content": "To begin with, let's learn how to reconstruct an array from its prefix sum array. This can be done by calculating the differences between adjacent elements.\nIf the element $$$\\frac{n * (n + 1)}{2}$$$ is missing from the array, we will add it and check if the array corresponds to some permutation.\nOtherwise, there is a missing element in the middle or at the beginning of the array.\nLet's count the occurrences of each difference between adjacent elements. Obviously, we should have one extra number and $$$2$$$ missing numbers.\nIf the count of differences occurring at least $$$2$$$ times is at least $$$2$$$, the answer is $$$NO$$$.\nThe answer is also $$$NO$$$ if any difference occurs at least $$$3$$$ times.\nOtherwise, we check that exactly $$$2$$$ distinct numbers are missing, and their sum is equal to the only duplicate."}}1234{"type": "session", "harness": "opencode", "id": "cf-1851-E", "name": "Codeforces: Nastya and Potions"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Nastya and Potions (Rating: 1500)\nTags: dfs and similar, dp, graphs, sortings\n\nAlchemist Nastya loves mixing potions. There are a total of $n$ types of potions, and one potion of type $i$ can be bought for $c_i$ coins.\n\nAny kind of potions can be obtained in no more than one way, by mixing from several others. The potions used in the mixing process will be consumed. Moreover, no potion can be obtained from itself through one or more mixing processes.\n\nAs an experienced alchemist, Nastya has an unlimited supply of $k$ types of potions $p_1, p_2, \\dots, p_k$, but she doesn't know which one she wants to obtain next. To decide, she asks you to find, for each $1 \\le i \\le n$, the minimum number of coins she needs to spend to obtain a potion of type $i$ next.\n\nInput: The first line of each test contains an integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases.\n\nEach test case is described as follows:\n\nThe first line contains two integers $n$ and $k$ ($1 \\le k < n \\le 2 \\cdot 10^5$) — the total number of potion types and the number of potion types Nastya already has.\n\nThe second line contains $n$ integers $c_1, c_2, \\dots, c_n$ ($1 \\le c_i \\le 10^9$) — the costs of buying the potions.\n\nThe third line contains $k$ distinct integers $p_1, p_2, \\dots, p_k$ ($1 \\le p_i \\le n$) — the indices of potions Nastya already has an unlimited supply of.\n\nThis is followed by $n$ lines describing ways to obtain potions by mixing.\n\nEach line starts with the integer $m_i$ ($0 \\le m_i < n$) — the number of potions required to mix a potion of the type $i$ ($1 \\le i \\le n$).\n\nThen line contains $m_i$ distinct integers $e_1, e_2, \\dots, e_{m_i}$ ($1 \\le e_j \\le n$, $e_j \\ne i$) — the indices of potions needed to mix a potion of the type $i$. If this list is empty, then a potion of the type $i$ can only be bought.\n\nIt is guaranteed that no potion can be obtained from itself through one or more mixing processes.\n\nIt is guaranteed that the sum of all $n$ values across all test cases does not exceed $2 \\cdot 10^5$. Similarly, it is guaranteed that the sum of all $m_i$ values across all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput: For each test case, output $n$ integers — the minimum number of coins Nastya needs to spend to obtain a potion of each type.\n\nExamples:\nInput: 4\n5 1\n30 8 3 5 10\n3\n3 2 4 5\n0\n0\n2 3 5\n0\n3 2\n5 143 3\n1 3\n1 2\n0\n2 1 2\n5 1\n5 4 1 3 4\n2\n2 4 5\n3 3 5 4\n2 1 4\n1 5\n0\n4 2\n1 1 5 4\n2 4\n3 2 4 3\n0\n2 2 4\n1 2\nOutput: 23 8 0 5 10 \n0 143 0 \n5 0 1 3 4 \n0 0 0 0\n\nInput: 3\n6 3\n5 5 4 5 2 2\n3 4 5\n2 2 5\n1 5\n3 4 1 6\n4 2 6 1 5\n0\n0\n6 2\n1 4 4 1 5 2\n3 6\n4 6 3 4 5\n4 6 5 3 4\n0\n1 5\n1 6\n0\n2 1\n4 3\n1\n0\n1 1\nOutput: 0 0 0 0 0 2 \n0 0 0 0 0 0 \n0 0\n"}}
{"type": "message", "message": {"role": "assistant", "content": "To begin with, let's note that potions of types $$$p1, p2, \\dots, p_k$$$ are essentially free, so we can replace their costs with $$$0$$$.\nLet $$$ans[i]$$$ be the answer for the $$$i$$$-th potion. Each potion can be obtained in one of two ways: by buying it or by mixing it from other potions. For mixing, we obtain all the required potions at the minimum cost. That is, if there is a way to mix a potion of type $$$i$$$, then the answer for it is either $$$c_i$$$ or the sum of answers for all $$$e_i$$$. Since the graph in the problem does not have cycles, this can be done by a simple depth-first search."}}1234{"type": "session", "harness": "opencode", "id": "cf-1851-F", "name": "Codeforces: Lisa and the Martians"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Lisa and the Martians (Rating: 1800)\nTags: bitmasks, greedy, math, strings, trees\n\nLisa was kidnapped by martians! It okay, because she has watched a lot of TV shows about aliens, so she knows what awaits her. Let's call integer martian if it is a non-negative integer and strictly less than $2^k$, for example, when $k = 12$, the numbers $51$, $1960$, $0$ are martian, and the numbers $\\pi$, $-1$, $\\frac{21}{8}$, $4096$ are not.\n\nThe aliens will give Lisa $n$ martian numbers $a_1, a_2, \\ldots, a_n$. Then they will ask her to name any martian number $x$. After that, Lisa will select a pair of numbers $a_i, a_j$ ($i \\neq j$) in the given sequence and count $(a_i \\oplus x) \\& (a_j \\oplus x)$. The operation $\\oplus$ means Bitwise exclusive OR, the operation $\\&$ means Bitwise And. For example, $(5 \\oplus 17) \\& (23 \\oplus 17) = (00101_2 \\oplus 10001_2) \\& (10111_2 \\oplus 10001_2) = 10100_2 \\& 00110_2 = 00100_2 = 4$.\n\nLisa is sure that the higher the calculated value, the higher her chances of returning home. Help the girl choose such $i, j, x$ that maximize the calculated value.\n\nInput: The first line contains an integer $t$ ($1 \\le t \\le 10^4$) — number of testcases.\n\nEach testcase is described by two lines.\n\nThe first line contains integers $n, k$ ($2 \\le n \\le 2 \\cdot 10^5$, $1 \\le k \\le 30$) — the length of the sequence of martian numbers and the value of $k$.\n\nThe second line contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($0 \\le a_i < 2^k$) — a sequence of martian numbers.\n\nIt is guaranteed that the sum of $n$ over all testcases does not exceed $2 \\cdot 10^5$.\n\nOutput: For each testcase, print three integers $i, j, x$ ($1 \\le i, j \\le n$, $i \\neq j$, $0 \\le x < 2^k$). The value of $(a_i \\oplus x) \\& (a_j \\oplus x)$ should be the maximum possible.\n\nIf there are several solutions, you can print any one.\n\nExamples:\nInput: 10\n5 4\n3 9 1 4 13\n3 1\n1 0 1\n6 12\n144 1580 1024 100 9 13\n4 3\n7 3 0 4\n3 2\n0 0 1\n2 4\n12 2\n9 4\n6 14 9 4 4 4 5 10 2\n2 1\n1 0\n2 4\n11 4\n9 4\n2 11 10 1 6 9 11 0 5\nOutput: 1 3 14\n1 3 0\n5 6 4082\n2 3 7\n1 2 3\n1 2 15\n4 5 11\n1 2 0\n1 2 0\n2 7 4\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Solution 1. Let's use the data structure called a bitwise trie. Fix some $$$a_i$$$, where all $$$a_j$$$ for $$$j < i$$$ have already been added to the trie. We will iterate over the bits in $$$a_i$$$ from the $$$(k - 1)$$$-th bit to the $$$0$$$-th bit. Since $$$2^t > 2^{t - 1} + 2^{t - 2} + \\ldots + 2 + 1$$$, if there exists $$$a_j$$$ with the same bit at the corresponding position in $$$a_i$$$, we will go into that branch of the trie and append $$$1 - b$$$ to the corresponding bit $$$x$$$. Otherwise, our path is uniquely determined. When we reach a leaf, the bits on the path will correspond to the optimal number $$$a_j$$$ for $$$a_i$$$. The complexity of this solution is $$$O(n k)$$$.\nSolution 2. Sort $$$a_1, a_2, \\ldots, a_n$$$ in non-decreasing order. We will prove that the answer is some pair of adjacent numbers. Let the answer be numbers $$$a_i, a_j$$$ ($$$j - i > 1$$$). If $$$a_i = a_j$$$, then $$$a_i = a_{i + 1}$$$. Otherwise, they have a common prefix of bits, after which there is a differing bit. That is, at some position $$$t$$$, $$$a_i$$$ has a $$$0$$$ and $$$a_j$$$ has a $$$1$$$. Since $$$j - i > 1$$$, $$$a_{i + 1}$$$ can have either $$$0$$$ or $$$1$$$ at this position, but in the first case it is more advantageous to choose $$$a_i, a_{i + 1}$$$ as the answer, and in the second case it is more advantageous to choose $$$a_{i + 1}, a_j$$$ as the answer. The complexity of this solution is $$$O(n \\log n)$$$.\nSolution 3 (secret). The problem can be easily reduced to finding a pair of numbers with the minimum $$$\\oplus$$$. If you don't know about the bitwise trie and the sorting trick, such a problem can be solved using AVX instructions. The complexity of this solution is $$$O(\\frac{n^2}{2 \\cdot 8})$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1851-G", "name": "Codeforces: Vlad and the Mountains"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Vlad and the Mountains (Rating: 2000)\nTags: binary search, data structures, dsu, graphs, implementation, sortings, trees, two pointers\n\nVlad decided to go on a trip to the mountains. He plans to move between $n$ mountains, some of which are connected by roads. The $i$-th mountain has a height of $h_i$.\n\nIf there is a road between mountains $i$ and $j$, Vlad can move from mountain $i$ to mountain $j$ by spending $h_j - h_i$ units of energy. If his energy drops below zero during the transition, he will not be able to move from mountain $i$ to mountain $j$. Note that $h_j - h_i$ can be negative and then the energy will be restored.\n\nVlad wants to consider different route options, so he asks you to answer the following queries: is it possible to construct some route starting at mountain $a$ and ending at mountain $b$, given that he initially has $e$ units of energy?\n\nInput: The first line of the input contains an integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases.\n\nThe descriptions of the test cases follow.\n\nThe first line of each test case contains two numbers $n$ and $m$ ($2 \\le n \\le 2 \\cdot 10^5$, $1 \\le m \\le \\min(\\frac{n\\cdot(n - 1)}{2}, 2 \\cdot 10^5)$) — the number of mountains and the number of roads between them, respectively.\n\nThe second line contains $n$ integers $h_1, h_2, h_3, \\dots, h_n$ ($1 \\le h_i \\le 10^9$) — the heights of the mountains.\n\nThe next $m$ lines contain two integers $u$ and $v$ ($1 \\le u, v \\le n$, $u \\ne v$) — the numbers of the mountains connected by a road. It is guaranteed that no road appears twice.\n\nThe next line contains an integer $q$ ($1 \\le q \\le 2 \\cdot 10^5$) — the number of queries.\n\nThe following $q$ lines contain three numbers $a$, $b$, and $e$ ($1 \\le a, b \\le n$, $0 \\le e \\le 10^9$) — the initial and final mountains of the route, and the amount of energy, respectively.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2 \\cdot 10^5$. The same guarantee applies to $m$ and $q$.\n\nOutput: For each query, output \"YES\" if Vlad can construct a route from mountain $a$ to mountain $b$, and \"NO\" otherwise.\n\nYou can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\", and \"YES\" will be recognized as a positive answer).\n\nIn the examples below, the answers for different test cases are separated by an empty line, which you do not need to output.\n\nExamples:\nInput: 2\n7 7\n1 5 3 4 2 4 1\n1 4\n4 3\n3 6\n3 2\n2 5\n5 6\n5 7\n5\n1 1 3\n6 2 0\n4 7 0\n1 7 4\n1 7 2\n6 5\n4 7 6 2 5 1\n1 3\n5 3\n1 5\n2 4\n6 2\n5\n1 5 1\n1 3 1\n1 2 1000\n6 2 6\n6 2 5\nOutput: YES\nNO\nYES\nYES\nNO\n\nYES\nNO\nNO\nYES\nNO\n\nInput: 2\n3 2\n1 3 9\n1 2\n2 3\n5\n1 1 1\n3 2 2\n1 1 2\n3 3 0\n1 2 1\n3 3\n1 4 1\n1 2\n2 3\n1 3\n5\n3 3 9\n1 3 6\n1 1 2\n3 3 6\n3 3 4\nOutput: YES\nYES\nYES\nYES\nNO\n\nYES\nYES\nYES\nYES\nYES\n\nInput: 1\n6 10\n7 9 2 10 8 6\n4 2\n6 1\n4 5\n3 5\n6 4\n1 3\n2 6\n6 5\n1 2\n3 6\n5\n4 4 8\n3 3 1\n5 5 9\n2 1 7\n6 6 10\nOutput: YES\nYES\nYES\nYES\nYES\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's consider the change in energy when traveling from $$$i \\rightarrow j \\rightarrow k$$$, $$$h_i - h_j + h_j - h_k = h_i - h_k$$$, it can be seen that this is the difference between the heights of the first and last mountains on the path. In other words, from vertex $$$a$$$, it is possible to reach any vertex for which there is a path that does not pass through vertices with a height greater than $$$h_a+e$$$.\nTherefore, for each query, it is necessary to construct a component from vertex $$$a$$$, in which all vertices with a height not greater than $$$h_a + e$$$ are included, and check if vertex $$$b$$$ lies within it. To do this efficiently, let's sort the queries by $$$h_a + e$$$, and the edges of the graph by $$$\\max(h_u, h_v)$$$, and maintain a disjoint set data structure (DSU).\nBefore each query, add all edges that have not been added yet and their $$$\\max(h_u, h_v)$$$ is not greater than $$$h_a+e$$$ for that specific query. After this, it remains to only check if vertices $$$a$$$ and $$$b$$$ belong to the same connected component."}}1234{"type": "session", "harness": "opencode", "id": "cf-1854-E", "name": "Codeforces: Game Bundles"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Game Bundles (Rating: 3000)\nTags: brute force, constructive algorithms, dp, greedy, math\n\nRishi is developing games in the 2D metaverse and wants to offer game bundles to his customers. Each game has an associated enjoyment value. A game bundle consists of a subset of games whose total enjoyment value adds up to $60$.\n\nYour task is to choose $k$ games, where $1 \\leq k \\leq 60$, along with their respective enjoyment values $a_1, a_2, \\dots, a_k$, in such a way that exactly $m$ distinct game bundles can be formed.\n\nInput: The input is a single integer $m$ ($1 \\le m \\le 10^{10}$) — the desired number of game bundles.\n\nOutput: The first line should contain an integer $k$ ($1 \\le k \\le 60$) — the number of games.\n\nThe second line should contain $k$ integers, $a_1, a_2, \\dots, a_k$ ($1 \\le a_1, a_2, \\dots, a_k \\le 60$) — the enjoyment values of the $k$ games.\n\nExamples:\nInput: 4\nOutput: 4\n20 20 20 20\n\nInput: 722\nOutput: 15\n15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\n\nInput: 1\nOutput: 1\n60\n"}}
{"type": "message", "message": {"role": "assistant", "content": "We describe a randomized solution that solves the problem for $$$m$$$ up to $$$10^{11}$$$ (and, with some additional care, may be able to solve also $$$m$$$ up to $$$10^{12}$$$). We decided to give the problem with the smaller constraint $$$m\\le 10^{10}$$$ to make the problem more accessible and because there may be some rare cases below $$$10^{11}$$$ for which our solution is too slow (even though we could not find any). We don't know any provably correct solution, if you have one we would be curious to see it.\nMain idea: Choose suitably the values $$$a_1, a_2, \\dots, a_h$$$ that belong to $$$[1,29]$$$ and then find $$$a_{h+1}, a_{h+2},\\dots,a_k$$$ in $$$[31,60]$$$ by solving a backpack-like problem.\nLet us describe more precisely the main idea. Assume that $$$a_1, a_2, \\dots, a_h\\le 30$$$ are fixed and they satisfy $$$a_1+a_2+\\cdots+a_h<60$$$. For any $$$s=0,1,2,\\dots,29$$$, let $$$f(s)$$$ be the number of subsets $$$I\\subseteq\\{1,2,\\dots,h\\}$$$ so that $$$\\sum_{i\\in I}a_i=s$$$. If we can find some values $$$0\\le s_1,s_2,\\dots,s_{k-h}\\le 29$$$ so that $$$f(s_1)+f(s_2)+\\cdots+f(s_{k-h})=s$$$, then by setting $$$a_{h+i} = 60-s_i$$$ for $$$i=1,2,\\dots, k-h$$$ we have found a valid solution to the problem.\nThere are two main difficulties:\n• How can we find $$$s_1, s_2,\\dots, s_{k-h}$$$?\n• How should we choose $$$a_1, a_2,\\dots, a_h$$$?\nSince it is important to get a good intuitive understanding of the computational complexity of the algorithm, let us say now that we will choose $$$h\\le 44$$$ and (accordingly) $$$k-h=16$$$. These values are flexible (the solution would still work with $$$h\\le 45$$$ and $$$k-h=45$$$ for example). We will say something more about the choice of these values when we will describe how $$$a_1,a_2,\\dots, a_h$$$ shall be chosen.\nThe backpack problem to find $$$s_1, s_2,\\dots, s_{k-h}$$$.\nThe naive way to find $$$s_1,\\dots, s_{k-h}$$$ would be to try all of them. There are $$$\\binom{k-h + 29}{29}$$$ possible ways (up to order, which does not matter). Since $$$k-h=16$$$ this number is $$$\\approx 2\\cdot 10^{11}$$$ which is too much to fit in the time limit.\nTo speed up the process, we will do as follows. Partition randomly $$$A\\cup B=\\{0,1,\\dots, 29\\}$$$ into two sets of size $$$15$$$. We iterate over all possible $$$s_1, s_2, \\dots, s_{(k-h)/2}\\in A$$$ and over all possible $$$s_{(k-h)/2+1},\\dots, s_{k-h}\\in B$$$ and check whether the sum of one choice from the first group and one choice from the second group yields the result. This is a standard optimization for the subset sum problem. What is its complexity? It can be implemented in linear time in the size of the two groups we have to iterate over, which have size $$$\\binom{(k-h)/2+15}{15}\\approx 5\\cdot 10^5$$$. Notice that in this faster way we will not visit all the $$$\\binom{k-h+29}{29}$$$ possible choices $$$s_1,s_2,\\dots, s_{k-h}$$$ because we are assuming that exactly half of them belong to $$$A$$$ and exactly half of them belong to $$$B$$$. This is not a big deal because with sufficiently high probability we will find a solution in any case.\nThe choice of $$$a_1, a_2,\\dots, a_{h}$$$.\nIt remains to decide how we should select $$$a_1, a_2, \\dots, a_{h}$$$. The following choice works:\n• Approximately the first $$$\\log_2(m)$$$ values are set equal to $$$1$$$.\n• Five additional values are chosen randomly from $$$[1, 6]$$$ so that the total sum stays below $$$60$$$.\nSome intuition on the construction. The choice of $$$a_1, \\dots, a_{h}$$$ may seem arbitrary; let us try to justify it. The goal is to generate a set of values $$$f(0), f(1),\\dots, f(29)$$$ that are simultaneously \"random enough\" and with size smaller but comparable to $$$m$$$. These two conditions are necessary to expect that the backpacking problem finds a solution with high enough probability.\nIf $$$a_1=a_2=\\cdots=a_{h}=1$$$, then $$$f(s) = \\binom{k-h}{s}$$$ and these numbers have size comparable to $$$m$$$ if $$$2^{h}$$$ is comparable to $$$m$$$. This observation explains why we start with approximately $$$\\log_2(m)$$$ ones. The issue is that we need some flexibility in the process as we may need to repeat it many times, this flexibility is provided by the addition of some additional random elements which don't change the magnitude of the values $$$f(0), f(1), \\dots, f(29)$$$ but that modify them as much as possible (if we added a large number it would not affect many $$$f(s)$$$ and thus it would not be very useful)."}}1234{"type": "session", "harness": "opencode", "id": "cf-1854-F", "name": "Codeforces: Mark and Spaceship"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Mark and Spaceship (Rating: 3500)\nTags: brute force, dp\n\nMark loves to move fast. So he made a spaceship that works in $4$-dimensional space.\n\nHe wants to use the spaceship to complete missions as fast as possible. In each mission, the spaceship starts at $(0, 0, 0, 0)$ and needs to end up at $(a, b, c, d)$. To do this, he instructs the spaceship's computer to execute a series of moves, where each move is a unit step in one of the eight cardinal directions: $(\\pm 1, 0, 0, 0)$, $(0, \\pm 1, 0, 0)$, $(0, 0, \\pm 1, 0)$, $(0, 0, 0, \\pm 1)$.\n\nUnfortunately, he also moved fast when building the spaceship, so there is a bug in the spaceship's code. The first move will be executed once, the second move will be executed twice, the third move will be executed thrice, and so on. In general, the $i$-th move will be executed $i$ times.\n\nFor any four integers $a, b, c, d$, let $f(a, b, c, d)$ be the minimum number of moves of a mission that ends up at $(a, b, c, d)$. Compute the sum of $f(a, b, c, d)$ over all points (with integer coordinates) such that $-A\\le a\\le A$, $-B\\le b\\le B$, $-C\\le c\\le C$, $-D\\le d\\le D$.\n\nInput: The only line of the input contains the four integers $A, B, C, D$ ($0\\le A,B,C,D\\le 1000$).\n\nOutput: Print the sum of $f(a, b, c, d)$ over the set of points described in the statement.\n\nExamples:\nInput: 1 0 0 0\nOutput: 2\n\nInput: 1 1 0 1\nOutput: 82\n\nInput: 3 2 4 1\nOutput: 4616\n"}}
{"type": "message", "message": {"role": "assistant", "content": "The $$$2$$$-dimensional case.\nLet us begin by cpnsidering the $$$2$$$-dimensional version of the problem. The solution to this simpler version provides the idea of the approach for the $$$4$$$-dimensional version.\nWe want to reach $$$(a, b)$$$. Can we do it with exactly $$$k$$$ moves? Two simple necessary conditions are:\n• $$$|a|+|b|\\le 1 + 2 + \\cdots + k$$$,\n• $$$a+b$$$ and $$$1 + 2 + \\cdots + k$$$ shall have the same parity.\nWithout loss of generality we may assume $$$0\\le a\\le b$$$. If $$$|a|+|b-k| \\le 1 + 2 + \\cdots + k-1$$$, then the statement follows by inductive hypothesis. Assume by contradiction that such inequality is false. If $$$b\\ge k$$$ then we have a contradiction because $$$|a|+|b-k| = |a|+|b|-k \\le (1 + 2 + \\cdots + k) - k$$$. Otherwise $$$b < k$$$ and the contradiction is $$$|a|+|b-k| = a + k-b \\le k \\le 1 + 2 + \\cdots + k-1$$$.\nHence, we have shown:\nLemma 1: The point $$$(a, b)$$$ is reachable with exactly $$$k$$$ moves if and only if $$$|a|+|b| \\le 1 + 2 + \\cdots + k$$$ and $$$a+b$$$ has the same parity of $$$1+2+\\cdots + k$$$.\nThe $$$4$$$-dimensional case.\nOne may expect statement analogous to the one of Lemma 1 to hold also when there are $$$4$$$ coordinates. It does not, but it almost does and this is the crucial idea of the solution. More precisely, the number of counter examples to such statement is rather small and we can find all of them. This is the intuition behind the following definition.\nDefinition: For $$$k\\ge 0$$$, let $$$A_k$$$ be the set of points $$$(a, b, c, d)$$$ such that $$$|a|+|b|+|c|+|d|\\le 1 + 2 + \\cdots + k$$$ and $$$a+b+c+d$$$ has the same parity of $$$1 + 2 + \\cdots + k$$$ but $$$(a, b, c, d)$$$ is not reachable with exactly $$$k$$$ moves.\nAs an immediate consequence of the definition, we have\nObservation: The point $$$(a, b, c, d)$$$ is reachable with exactly $$$k$$$ moves if and only if $$$|a|+|b|+|c|+|d| \\le 1 + 2 + \\dots + k$$$ and $$$a+b+c+d$$$ has the same parity of $$$1+2+\\cdots + k$$$ and $$$(a, b, c, d)\\not\\in A_k$$$.\nThanks to this observation, if one is able to efficiently find $$$A_k$$$ for all interesting values of $$$k$$$, then solving the problem is (comparatively) easy. The following lemma is our main tool for this purpose.\nLemma 2: Assume that $$$(a, b, c, d) \\in A_k$$$ with $$$0\\le a\\le b\\le c\\le d$$$. Then, either $$$k\\le 6$$$ or $$$(a, b, c, d - k) \\in A_{k-1}$$$.\nProof: The strategy is the same adopted to show Lemma 1. In some sense, we are saying that the inductive step works also in dimension $$$4$$$, but the base cases don't.\nIf $$$|a|+|b|+|c|+|d-k|\\le 1 + 2 + \\cdots + k-1$$$, then it must be $$$(a, b, c, d-k)\\in A_{k-1}$$$ because if $$$(a, b, c, d-k$$$ were reachable with $$$k-1$$$ moves then $$$(a, b, c, d)$$$ were reachable with $$$k$$$ and we know that this is not true.\nAssume by contradiction that $$$|a|+|b|+|c|+|d-k|> 1 + 2 + \\cdots + k-1$$$. If $$$d\\ge k$$$ then we reach the contradiction $$$|a|+|b|+|c|+|d-k| = a+b+c+d-k \\le (1 + 2 + \\dots + k) - k$$$. Otherwise, $$$d < k$$$ and thus we reach the contradiction $$$|a|+|b|+|c|+|d-k| = a+b+c+k-d\\le a+b+k\\le 3k-2\\le 1 + 2 + \\dots + k-1$$$ (for $$$k\\ge 7$$$).\nWe can now describe the solution. Assume that we know $$$A_{k-1}$$$. First of all, notice that it is then possible to determine in $$$O(1)$$$ whether a point belongs to $$$A_k$$$ or not. To generate a list of candidate elements for $$$A_k$$$ we proceed as follows:\n• If $$$k\\le 6$$$, we simply iterate over all points with $$$|a|+|b|+|c|+|d|\\le 1 + 2 + \\cdots + k$$$.\n• Otherwise, we iterate over the points in $$$A_{k-1}$$$ and we consider as candidate elements for $$$A_k$$$ the points that can be obtained by changing the value of one coordinate by $$$k$$$.\nOne can verify that to handle correctly all points with coordinates up to $$$1000$$$ it is necessary to compute $$$A_k$$$ for $$$0\\le k \\le 62$$$.\nOne additional cheap trick is required to make $$$A_k$$$ sufficiently small and get a sufficiently fast solution. Given $$$(a, b, c, d)$$$, the instance of the problem is equivalent if we change the signs of the coordinates or we change the order of the coordinates. Hence we shall always \"normalize\" the point so that $$$0\\le a \\le b\\le c\\le d$$$. If we do this consistently everywhere in the process, the solution becomes an order of magnitude faster. In particular, this trick guarantees $$$|A_k|\\le 5000$$$ for all $$$0\\le k\\le 62$$$.\nBonus question: Find an explicit closed form for the elements in $$$A_k$$$ for any $$$k$$$. (in this way one can solve the problem also with larger constraints on $$$A, B, C, D$$$; but it is tedious)"}}1234{"type": "session", "harness": "opencode", "id": "cf-1856-A", "name": "Codeforces: Tales of a Sort"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Tales of a Sort (Rating: 800)\nTags: implementation\n\nAlphen has an array of positive integers $a$ of length $n$.\n\nAlphen can perform the following operation:\n\n- For all $i$ from $1$ to $n$, replace $a_i$ with $\\max(0, a_i - 1)$.\n\nAlphen will perform the above operation until $a$ is sorted, that is $a$ satisfies $a_1 \\leq a_2 \\leq \\ldots \\leq a_n$. How many operations will Alphen perform? Under the constraints of the problem, it can be proven that Alphen will perform a finite number of operations.\n\nInput: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \\le t \\le 500$) — the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2 \\le n \\le 50$) — the length of the array $a$.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10 ^ 9$) — the elements of the array $a$.\n\nOutput: For each test case, output a single integer — the number of operations that Alphen will perform.\n\nExamples:\nInput: 7\n3\n1 2 3\n5\n2 1 2 1 2\n4\n3 1 5 4\n2\n7 7\n5\n4 1 3 2 5\n5\n2 3 1 4 5\n3\n1000000000 1 2\nOutput: 0\n2\n5\n0\n4\n3\n1000000000\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Suppose we have performed $$$k$$$ operations and have gotten the array $$$b$$$. Then $$$b_i := \\max(0, a_i - k)$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$.\nIf $$$b$$$ is sorted, then $$$b_i \\le b_{i + 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n - 1$$$. In other words, $$$\\max(0, a_i - k) \\le \\max(0, a_{i + 1} - k)$$$. Let's find for which $$$k$$$ this inequality holds.\nSuppose $$$a_i \\le a_{i + 1}$$$. Then $$$\\max(0, a_i - k) \\le \\max(0, a_{i + 1} - k)$$$ is true for any $$$k \\ge 0$$$.\nNow suppose $$$a_i > a_{i + 1}$$$. Then $$$\\max(0, a_i - k) \\le \\max(0, a_{i + 1} - k)$$$ is true only if $$$k \\ge a_i$$$.\nFor the array to be sorted after $$$k$$$ operations, $$$\\max(0, a_i - k) \\le \\max(0, a_{i + 1} - k)$$$ must hold for all $$$1 \\le i < n$$$. From the statements above, we can see that the smallest $$$k$$$ for which this is true is equal to the maximum value of $$$a_{i}$$$ over all $$$1 \\le i < n$$$ such that $$$a_{i} > a_{i + 1}$$$. If no such $$$i$$$ exists, the answer is $$$0$$$, since the array is already sorted.\nComplexity: $$$\\mathcal{O}(n)$$$\nNote: there are at least two other solutions:\n• The solution above is also equivalent to finding the maximum value of $$$a_{i}$$$ over all $$$1 \\le i < j \\le n$$$ such that $$$a_i > a_j$$$, which leads to a $$$\\mathcal{O}(n^2)$$$ solution.\n• Alternatively, you can do binary search on the answer to get a $$$\\mathcal{O}(n \\log A)$$$ solution, where $$$A$$$ is the maximum possible value of $$$a_i$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1856-B", "name": "Codeforces: Good Arrays"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Good Arrays (Rating: 900)\nTags: implementation, math\n\nYou are given an array of positive integers $a$ of length $n$.\n\nLet's call an array of positive integers $b$ of length $n$ good if:\n\n1. $a_i \\neq b_i$ for all $i$ from $1$ to $n$,\n2. $a_1 + a_2 +\\ldots + a_n = b_1 + b_2 + \\ldots + b_n$.\n\nDoes a good array exist?\n\nInput: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 10^5$) — the length of the array $a$.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 10 ^ 9$) — the elements of the array $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\nOutput: For each test case, output \"YES\" (without quotes) if there exists a good array, and \"NO\" (without quotes) otherwise.\n\nYou can output the answer in any case (upper or lower). For example, the strings \"yEs\",\"yes\", \"Yes\", and \"YES\" will be recognized as positive responses.\n\nExamples:\nInput: 6\n3\n6 1 2\n2\n1 1\n4\n3 1 2 4\n1\n17\n5\n1 2 1 1 1\n3\n618343152 819343431 1000000000\nOutput: YES\nNO\nYES\nNO\nNO\nYES\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Suppose $$$b$$$ didn't have to consist of only positive integers. Then, one simple strategy would be to to decrease each of $$$a_2, a_3, \\ldots a_n$$$ by $$$1$$$ and increase $$$a_1$$$ by $$$n - 1$$$. Except for $$$n = 1$$$, when it is impossible to get a good array.\nWhen $$$b$$$ has to consist of only positive integers, we can't decrease elements that are equal to $$$1$$$. But to make $$$b_i \\neq a_i$$$, we also have to increase them by at least $$$1$$$.\nLet $$$cnt_1$$$ be the number of elements in $$$a$$$ that are equal to $$$1$$$. To not change the sum of the array, we have to decrease the other $$$n - cnt_1$$$ elements by at least $$$cnt_1$$$. For this to be possible, their sum must be equal to at least $$$(n - cnt_1) + cnt_1 = n$$$, since each of the $$$n - cnt_1$$$ elements must remain positive. So, $$$a_1 + a_2 + \\ldots + a_n$$$ has to be equal to at least $$$cnt_1 + (n - cnt_1) + cnt_1 = cnt_1 + n$$$.\nSo, if $$$a_1 + \\ldots + a_n < cnt_1 + n$$$, a good array doesn't exist. If $$$n = 1$$$, a good array also doesn't exist.\nNow suppose $$$a_1 + \\ldots + a_n \\ge cnt_1 + n$$$ and $$$n \\neq 1$$$. We will prove that if this is the case, a good array must exist.\nIf $$$cnt_1 \\le \\frac{n}{2}$$$, we increase all $$$a_i = 1$$$ by $$$1$$$ and decrease $$$cnt_1$$$ elements that are $$$\\ge 2$$$ by $$$1$$$ and apply the simple strategy described above for the other $$$n - 2 \\cdot cnt_1$$$ elements $$$\\ge 2$$$, so in this case, a good array exists.\nIf $$$cnt_1 > \\frac{n}{2}$$$, we increase all $$$a_i = 1$$$ by $$$1$$$ and decrease each element $$$\\ge 2$$$ by at least $$$1$$$, so $$$b_i \\neq a_i$$$ holds for all $$$1 \\le i \\le n$$$, so in this case, a good array also exists.\nComplexity: $$$\\mathcal{O}(n)$$$"}}1234{"type": "session", "harness": "opencode", "id": "cf-1856-C", "name": "Codeforces: To Become Max"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: To Become Max (Rating: 1600)\nTags: binary search, brute force, data structures, dp\n\nYou are given an array of integers $a$ of length $n$.\n\nIn one operation you:\n\n- Choose an index $i$ such that $1 \\le i \\le n - 1$ and $a_i \\le a_{i + 1}$.\n- Increase $a_i$ by $1$.\n\nFind the maximum possible value of $\\max(a_1, a_2, \\ldots a_n)$ that you can get after performing this operation at most $k$ times.\n\nInput: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \\le t \\le 100$) — the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains two integers $n$ and $k$ ($2 \\le n \\le 1000$, $1 \\le k \\le 10^{8}$) — the length of the array $a$ and the maximum number of operations that can be performed.\n\nThe second line of each test case contains $n$ integers $a_1,a_2,\\ldots,a_n$ ($1 \\le a_i \\le 10^{8}$) — the elements of the array $a$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.\n\nOutput: For each test case output a single integer — the maximum possible maximum of the array after performing at most $k$ operations.\n\nExamples:\nInput: 6\n3 4\n1 3 3\n5 6\n1 3 4 5 1\n4 13\n1 1 3 179\n5 3\n4 3 2 2 2\n5 6\n6 5 4 1 5\n2 17\n3 5\nOutput: 4\n7\n179\n5\n7\n6\n"}}
{"type": "message", "message": {"role": "assistant", "content": "We will do binary search on the answer. The lower bound can be set to $$$0$$$, while $$$\\max(a_1, \\ldots a_n) + k$$$ is clearly enough for the upper bound.\nLet $$$b$$$ be some resulting array after performing at most $$$k$$$ operations.\nSuppose for some $$$x$$$ we want to check if we can get $$$\\max(b_1, \\ldots b_n) \\ge x$$$ in at most $$$k$$$ operations. That is, there must exist some index $$$i$$$ such that $$$b_i \\ge x$$$. So, let's iterate $$$i$$$ from $$$1$$$ to $$$n$$$ and check if it possible to have $$$b_i \\ge x$$$ in at most $$$k$$$ operations.\nLet $$$f(i, y)$$$ be the minimum number of operations needed to make $$$b_i \\ge y$$$. Then:\n• $$$f(i, y) = 0$$$ for all $$$y \\le a_i$$$,\n• $$$f(i, y) = y - a_i + f(i + 1, y - 1)$$$ for all $$$1 \\le i < n$$$ and $$$y > a_i$$$,\n• $$$f(i, y) = +\\infty$$$ for $$$i = n$$$ and all $$$y > a_i$$$.\nIt is easy to see that calculating $$$f(i, x)$$$ takes $$$\\mathcal{O}(n)$$$ time for one call in the worst case.\nThus, our check consists of comparing $$$f(i, x)$$$ and $$$k$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. If at least one of the values is $$$\\le k$$$, it is possible to have some $$$b_i \\ge x$$$ in at most $$$k$$$ operations and we increase the lower bound in the binary search after updating the current answer. Otherwise, it is impossible and we decrease the upper bound.\nComplexity: $$$\\mathcal{O}(n^2 \\cdot \\log A)$$$, where A is the maximum possible value of $$$a_i$$$ and $$$k$$$.\nNotes:\n• You can get a $$$\\mathcal{O}(n^2 \\cdot \\log n)$$$ solution by setting the lower bound in the binary search to $$$\\max(a_1, \\ldots, a_n)$$$ and the upper bound to $$$\\max(a_1, \\ldots, a_n) + n$$$.\n• There exists a $$$\\mathcal{O}(n^2)$$$ dp solution that relies on the fact that the answer lies in the range $$$[ \\max(a_1, \\ldots, a_n); \\max(a_1, \\ldots, a_n) + n ]$$$"}}1234{"type": "session", "harness": "opencode", "id": "cf-1856-D", "name": "Codeforces: More Wrong"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: More Wrong (Rating: 2100)\nTags: divide and conquer, interactive\n\nThis is an interactive problem.\n\nThe jury has hidden a permutation$^\\dagger$ $p$ of length $n$.\n\nIn one query, you can pick two integers $l$ and $r$ ($1 \\le l < r \\le n$) by paying $(r - l)^2$ coins. In return, you will be given the number of inversions$^\\ddagger$ in the subarray $[p_l, p_{l + 1}, \\ldots p_r]$.\n\nFind the index of the maximum element in $p$ by spending at most $5 \\cdot n^2$ coins.\n\nNote: the grader is not adaptive: the permutation is fixed before any queries are made.\n\n$^\\dagger$ 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).\n\n$^\\ddagger$ The number of inversions in an array is the number of pairs of indices $(i,j)$ such that $i < j$ and $a_i > a_j$. For example, the array $[10,2,6,3]$ contains $4$ inversions. The inversions are $(1,2),(1,3),(1,4)$, and $(3,4)$.\n\nInput: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \\le t \\le 100$) — the number of test cases.\n\nThe only line of each test case contains a single integer $n$ ($2 \\le n \\le 2000$) — the length of the hidden permutation $p$.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.\n\nExamples:\nInput: 2\n4\n\n1\n\n0\n\n2\n\n1\nOutput: ? 1 3\n\n? 3 4\n\n! 4\n\n? 1 2\n\n! 1\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let $$$q(l, r)$$$ be be the number of inversions in the subarray $$$[p_l, p_{l + 1}, \\ldots p_r]$$$. If $$$l = r$$$, we have $$$q(l, r) = 0$$$, otherwise, $$$q(l, r) = $$$ is equal to the result of the query \"? l r\".\nLet $$$f(l, r)$$$ calculate the index of the maximum value in $$$p_l, p_{l+1}, \\ldots, p_r$$$. If $$$l = r$$$, we have $$$f(l, r) = l$$$.\nSuppose $$$l < r$$$. Let $$$i := f(l, m)$$$ and $$$j := f(m + 1, r)$$$, where $$$m := \\lfloor \\frac{l + r}{2} \\rfloor$$$. Let's compare $$$q(l, j - 1)$$$ and $$$q(l, j)$$$.\nIf they are equal, $$$p_j$$$ is greater than all the elements in the subarray $$$[p_l, p_{l + 1}, \\ldots, p_m]$$$, so $$$f(l, r) = j$$$.\nIf $$$q(l, j - 1) < q(l, j)$$$, $$$p_j$$$ is not greater that all the elements in the subarray $$$[p_l, p_{l + 1}, \\ldots, p_m]$$$, and thus, the maximum on the whole subarray is $$$p_i$$$, so $$$f(l, r) = i$$$.\nNote that the case $$$q(l, j - 1) > q(l, j)$$$ is impossible, since all inversions in the subarray $$$[p_l, p_{l + 1}, \\ldots, p_{j - 1}]$$$ remain as inversions for the subarray $$$[p_l, p_{l + 1}, \\ldots, p_j]$$$.\nTo find the values of $$$q(l, j - 1)$$$ and $$$q(l, j)$$$, we will use $$$(j - 1 - l)^2 + (j - l)^2 \\le (r - l)^2 + (r - l)^2 = 2 \\cdot (r - l)^2$$$ coins.\nLet $$$g_n$$$ be the number of coins needed to find the maximum on a subarray of length $$$n$$$. We will prove by induction that $$$g_n \\le 4 \\cdot n^2$$$ for all natural $$$n$$$.\nBase case: $$$n = 1$$$, $$$g_1 := 0 \\le 4 \\cdot n^2 = 4$$$.\nInduction step: let $$$m := \\lceil \\frac{n}{2} \\rceil$$$. From the statements above, we have: $$$$$$g_n \\le 2 \\cdot (n - 1)^2 + g_{m} + g_{n - m} \\le$$$$$$ $$$$$$2 \\cdot (n - 1)^2 + 4 \\cdot (m^2 + (n - m)^2) =$$$$$$ $$$$$$6n^2 + 8m^2 + 2 - 8nm - 4n =$$$$$$ $$$$$$4n^2 + 2 \\cdot (n^2 - 4nm + 4m^2) + 2 - 4n =$$$$$$ $$$$$$4n^2 + 2 \\cdot (n - 2m)^2 + 2 - 4n \\le$$$$$$ $$$$$$4n^2 + 2 \\cdot 1 + 2 - 4n =$$$$$$ $$$$$$4n^2 + 4 - 4n \\le 4n^2$$$$$$\nThus, to calculate $$$f(1, n)$$$, the answer to our problem, we will use $$$g_n \\le 4 \\cdot n^2$$$ coins, which comfortably fits into the problem's $$$5 \\cdot n^2$$$ coin limit.\nComplexity: $$$\\mathcal{O}(n)$$$"}}1234{"type": "session", "harness": "opencode", "id": "cf-1856-E1", "name": "Codeforces: PermuTree (easy version)"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: PermuTree (easy version) (Rating: 1800)\nTags: dfs and similar, dp, trees\n\nThis is the easy version of the problem. The differences between the two versions are the constraint on $n$ and the time limit. You can make hacks only if both versions of the problem are solved.\n\nYou are given a tree with $n$ vertices rooted at vertex $1$.\n\nFor some permutation$^\\dagger$ $a$ of length $n$, let $f(a)$ be the number of pairs of vertices $(u, v)$ such that $a_u < a_{\\operatorname{lca}(u, v)} < a_v$. Here, $\\operatorname{lca}(u,v)$ denotes the lowest common ancestor of vertices $u$ and $v$.\n\nFind the maximum possible value of $f(a)$ over all permutations $a$ of length $n$.\n\n$^\\dagger$ 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).\n\nInput: The first line contains a single integer $n$ ($2 \\le n \\le 5000$).\n\nThe second line contains $n - 1$ integers $p_2,p_3,\\ldots,p_n$ ($1 \\le p_i < i$) indicating that there is an edge between vertices $i$ and $p_i$.\n\nOutput: Output the maximum value of $f(a)$.\n\nExamples:\nInput: 5\n1 1 3 3\nOutput: 4\n\nInput: 2\n1\nOutput: 0\n\nInput: 6\n1 2 2 1 5\nOutput: 7\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's consider the subproblem of maximizing the number of suitable pairs for some fixed $$$\\operatorname{lca}(u, v) = x$$$. Then, we want to maximize the number of pairs $$$(u, v)$$$ such that $$$a_u < a_x < a_v$$$ and $$$u$$$ and $$$v$$$ are in different subtrees of $$$x$$$. So for each subtree of $$$x$$$, we only care about the number of vertices with $$$a_v > a_{x}$$$ and about the number of vertices with $$$a_v < a_x$$$.\nSuppose vertex $$$x$$$ has $$$m$$$ subtrees and the $$$i$$$-th of them has $$$s_i$$$ vertices in it and $$$b_i$$$ vertices less with $$$a_v < a_x$$$. We will prove later that a suitable permutation $$$a$$$ exists for all possible correct values of $$$b_i$$$ (that is $$$0 \\le b_i \\le s_i$$$).\nThen, the total number of suitable pairs (and the value we are trying to maximize) is equal to: $$$(s_1 - b_1) \\cdot(0 + b_2 + \\ldots + b_m) + (s_2 - b_2) \\cdot(b_1 + 0 + \\ldots + b_m) + \\ldots + (s_m - b_m) \\cdot(b_1 + b_2 + \\ldots + 0)$$$.\nLet $$$dp[i][B]$$$ be the maximum possible number of suitable pairs such that $$$u$$$ and $$$v$$$ lie in the first $$$i$$$ subtrees and $$$b_1 + b_2 + \\cdots + b_i = B$$$ ($$$0 \\le b_j \\le s_j$$$ for all $$$1 \\le j \\le i$$$). Also define $$$S_i := s_1 + s_2 + \\ldots + s_i$$$. Then:\n• $$$dp[1][B] := 0$$$ for $$$0 \\le B \\le s_1$$$.\n• $$$dp[i][B] := \\max\\limits_{\\max(0, B - S_{i-1}) \\le b_i \\le \\min(s_i, B)}{(dp[i - 1][B - b_i] + b_i \\cdot (S_{i-1} - (B - b_i)) + (s_i - b_i) \\cdot (B - b_i))}$$$ for $$$2 \\le i \\le m$$$ and $$$0 \\le B \\le S_{i}$$$.\nIn the statement above:\n• The limits for $$$b_i$$$ are $$$\\max(0, B - S_{i-1}) \\le b_i \\le \\min(s_i, B)$$$ because: $$$0 \\le b_i \\le s_i$$$ must hold, $$$0 \\le B - b_i \\le S_{i - 1}$$$ must hold.\n• $$$0 \\le b_i \\le s_i$$$ must hold,\n• $$$0 \\le B - b_i \\le S_{i - 1}$$$ must hold.\n• $$$dp[i - 1][B - b_i]$$$ is the number of suitable pairs $$$(u, v)$$$ in the first $$$i - 1$$$ subtrees.\n• $$$b_i \\cdot (S_{i-1} - (B - b_i))$$$ is the number of suitable pairs $$$(u, v)$$$ where $$$u$$$ lies in the $$$i$$$-th subtree and $$$v$$$ lies in the first $$$i - 1$$$ subtrees.\n• $$$(s_i - b_i) \\cdot (B - b_i)$$$ is the number of suitable pairs $$$(u, v)$$$ where $$$v$$$ lies in the $$$i$$$-th subtree and $$$u$$$ lies in the first $$$i - 1$$$ subtrees.\nThe maximum possible number of suitable pairs is equal to $$$\\max\\limits_{0 \\le B \\le S_m}{dp[m][B]}$$$.\nLet's calculate the complexity of this dynamic programming subproblem. To do that, it's easier to consider iterating over $$$b_i$$$ and then iterating over all suitable values of $$$B$$$. Then, from the condition $$$0 \\le b_i \\le s_i$$$ we get $$$s_i + 1$$$ values of $$$b_i$$$, and from the condition $$$0 \\le B - b_i \\le S_{i - 1} \\implies b_i \\le B \\le b_i + S_{i - 1}$$$ we get $$$S_{i - 1} + 1$$$ suitable values of $$$B$$$ for each $$$b_i$$$.\nTaking into account that the $$$dp[i][B]$$$ array has $$$m \\cdot (S_m + 1)$$$ values, adding everything up, the number of \"operations\" we get is equal to:\n$$$$$$m \\cdot (S_m + 1) + (s_2 + 1) \\cdot (S_1 + 1) + (s_3 + 1) \\cdot (S_2 + 1) + \\ldots + (s_m + 1) \\cdot (S_{m - 1} + 1) = $$$$$$\n$$$$$$m \\cdot (S_m + 1) + (s_2 \\cdot S_1 + s_3 \\cdot S_2 + \\ldots + s_m \\cdot S_{m-1}) + (s2 + \\ldots + s_m) + (S_1 + \\ldots + S_{m-1}) + (m - 1) \\le $$$$$$\n$$$$$$m \\cdot S_m + m + (s_2 \\cdot S_1 + s_3 \\cdot S_2 + \\ldots + s_m \\cdot S_{m-1}) + S_m + (m - 1) \\cdot S_m + m = $$$$$$\n$$$$$$2m \\cdot (S_m + 1) + (s_2 \\cdot S_1 + s_3 \\cdot S_2 + \\ldots + s_m \\cdot S_{m-1}) \\le $$$$$$\n$$$$$$2mn + (s_2 \\cdot S_1 + s_3 \\cdot S_2 + \\ldots + s_m \\cdot S_{m-1})$$$$$$\nReturning to the original problem, let's solve the subproblem for each $$$x$$$ from $$$1$$$ to $$$n$$$ and add up the results. This is the upper bound on the answer, and we will prove a bit later that a suitable permutation $$$a$$$ exists.\nLet's change our notation a bit:\n• $$$m_x$$$ is the number of subtrees of vertex $$$x$$$.\n• $$$s_{x,i}$$$ is the size of the $$$i$$$-th subtree of vertex $$$x$$$.\n• $$$S_{x,i}$$$ is equal to $$$s_{x,1} + s_{x,2} + \\ldots + s_{x,i}$$$.\n• $$$b_{x,i}$$$ is the number of vertices in the $$$i$$$-th subtree for which $$$a_v < a_x$$$.\nAdding up the number of \"operations\" for all subproblems, we get:\n$$$$$$\\displaystyle\\sum_{x=1} ^{n} (2m_xn + s_{x,2} \\cdot S_{x,1} + s_{x,3} \\cdot S_{x,2} + \\ldots + s_{x,m_x} \\cdot S_{x,m_x-1}) = $$$$$$ $$$$$$2n \\cdot (n - 1) + \\displaystyle\\sum_{x=1} ^{n} (s_{x,2} \\cdot S_{x,1} + s_{x,3} \\cdot S_{x,2} + \\ldots + s_{x,m_x} \\cdot S_{x,m_x-1})$$$$$$\nConsider an undirected graph with $$$n$$$ vertices where there are initially no edges. You can think of the value of $$$s_{x,j} \\cdot S_{x,j-1}$$$ as the number of edges added between each vertex of the first $$$j-1$$$ subtrees of $$$x$$$ and the $$$j$$$-th subtree of $$$x$$$. Since each pair of $$$(x, j)$$$ accounts for a unique set of edges of size $$$s_{x,j} \\cdot S_{x,j-1}$$$, the sum $$$\\sum_{x=1} ^{n} (\\ldots)$$$ can be bounded above by $$$\\frac{n \\cdot (n - 1)}{2}$$$, the maximum number of unique edges in a graph with $$$n$$$ vertices:\n$$$$$$2n \\cdot (n - 1) + \\displaystyle\\sum_{x=1} ^{n} (s_{x,2} \\cdot S_{x,1} + s_{x,3} \\cdot S_{x,2} + \\ldots + s_{x,m_x} \\cdot S_{x,m_x-1}) \\le$$$$$$\n$$$$$$2n \\cdot (n - 1) + \\frac{n \\cdot (n - 1)}{2}$$$$$$\nSo we get a complexity of $$$\\mathcal{O}(n^2)$$$.\nYou can read more about this complexity analysis in section 7 of this Codeforces blog.\n$$$ $$$\nWe solve independently for each value of $$$\\operatorname{lca}(u,v)$$$ and add up the answers to get an upper bound for $$$f(a)$$$. We will prove by constructing $$$a$$$ that this bound is achievable.\nLet $$$g(x, L)$$$ be a function that takes a vertex $$$x$$$ and a set of integers $$$L$$$, assigns some value from $$$L$$$ to $$$a_x$$$, and then calls $$$g(y, L_y)$$$ for all children of $$$x$$$ (direct descendants), where the sets $$$L_y$$$ do not intersect and their union is equal to $$$L \\setminus \\{ a_x \\}$$$.\nHow it works:\n1. Let $$$y_{1}, \\ldots y_{m_x}$$$ be the children of vertex $$$x$$$ and $$$L_{y_i}$$$ initially be empty sets. Suppose that for vertex $$$x$$$ it is optimal to have the values of $$$b_{x,1}, b_{x,2}, \\ldots, b_{x,m_x}$$$ be equal to $$$c_1, c_2, \\ldots, c_{m_x}$$$ respectively.\n2. For each $$$i$$$ from $$$1$$$ to $$$m_x$$$, take the $$$c_i$$$ smallest values from $$$L$$$, remove them from $$$L$$$, and add them to the set $$$L_{y_i}$$$.\n3. Set $$$a_x$$$ to the smallest value in $$$L$$$ and remove it from the set.\n4. For each $$$i$$$ from $$$1$$$ to $$$m_x$$$, take the $$$s_i - c_i$$$ smallest values from $$$L$$$, remove them from $$$L$$$, and add them to the set $$$L_{y_i}$$$.\n5. For each $$$i$$$ from $$$1$$$ to $$$m_x$$$, call $$$g(y_i, L_{y_i})$$$.\nCalling $$$g(x, \\{1, 2, \\ldots n \\})$$$ constructs an optimal permutation $$$a$$$ for the given tree.\nComplexity: $$$\\mathcal{O}(n^2)$$$\nNote: you can change $$$dp[i][B]$$$ to $$$dp[B]$$$ and iterate $$$B$$$ from $$$S_i$$$ to $$$0$$$. This improves the memory usage, but does not change the time complexity."}}1234{"type": "session", "harness": "opencode", "id": "cf-1856-E2", "name": "Codeforces: PermuTree (hard version)"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: PermuTree (hard version) (Rating: 2700)\nTags: bitmasks, dfs and similar, dp, fft, greedy, implementation, math, trees\n\nThis is the hard version of the problem. The differences between the two versions are the constraint on $n$ and the time limit. You can make hacks only if both versions of the problem are solved.\n\nYou are given a tree with $n$ vertices rooted at vertex $1$.\n\nFor some permutation$^\\dagger$ $a$ of length $n$, let $f(a)$ be the number of pairs of vertices $(u, v)$ such that $a_u < a_{\\operatorname{lca}(u, v)} < a_v$. Here, $\\operatorname{lca}(u,v)$ denotes the lowest common ancestor of vertices $u$ and $v$.\n\nFind the maximum possible value of $f(a)$ over all permutations $a$ of length $n$.\n\n$^\\dagger$ 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).\n\nInput: The first line contains a single integer $n$ ($2 \\le n \\le 10^6$).\n\nThe second line contains $n - 1$ integers $p_2,p_3,\\ldots,p_n$ ($1 \\le p_i < i$) indicating that there is an edge between vertices $i$ and $p_i$.\n\nOutput: Output the maximum value of $f(a)$.\n\nExamples:\nInput: 5\n1 1 3 3\nOutput: 4\n\nInput: 2\n1\nOutput: 0\n\nInput: 6\n1 2 2 1 5\nOutput: 7\n"}}
{"type": "message", "message": {"role": "assistant", "content": "First of all, the most important takeaways from the editorial for the easy version:\n• For each $$$x$$$ from $$$1$$$ to $$$n$$$, you can solve $$$n$$$ independent subproblems of maximizing the number of suitable pairs for a fixed value of $$$\\operatorname{lca}(u, v) = x$$$, and then add up the results to get the final answer.\n• For each subproblem, the number of suitable pairs of vertices can be calculate based on the sizes of all the child subtrees $$$s_{x,i}$$$ and the number of vertices with $$$a_v < a_x$$$ in each of them, which we will call $$$b_{x,i}$$$.\nConsider the subproblem for some $$$x$$$. Let $$$m_x$$$ be the number of child subtrees of vertex $$$x$$$. Suppose some optimal values of $$$b_{x,1}, b_{x,2}, \\ldots b_{x,m_x}$$$ that maximize the number of suitable pairs are $$$c_1, c_2, \\ldots c_{m_x}$$$ respectively.\n$$$ $$$\nClaim: there exists an optimal solution where $$$c_i = 0$$$ or $$$c_i = s_{x,i}$$$ for all $$$i$$$ from $$$1$$$ to $$$m_x$$$.\nProof: let's consider some optimal solution $$$c_1, c_2, \\ldots c_{m_x}$$$ where $$$0 < c_i < s_{x,i}$$$ for some $$$i$$$. Suppose $$$D$$$ is the number of suitable pairs of vertices such that both $$$u$$$ and $$$v$$$ don't lie in the $$$i$$$-th child's subtree. Then, the total number of suitable pairs is equal to: $$$$$$D + c_i \\cdot \\sum_{j \\neq i}{(s_{x, j} - c_j)} + (s_{x, i} - c_i) \\cdot \\sum_{j \\neq i} c_j$$$$$$ If we define $$$C_1 := \\sum_{j \\neq i}{(s_{x, j} - c_j)}$$$ and $$$C_2 := \\sum_{j \\neq i} c_j$$$, we get an even simpler formula: $$$$$$D + c_i \\cdot C_1 + (s_{x, i} - c_i) \\cdot C_2 =$$$$$$ $$$$$$(D + C_2 \\cdot s_{x,i}) + c_i \\cdot (C_1 - C_2)$$$$$$\nThe values of $$$(D + C_2 \\cdot s_{x,i})$$$, $$$C_1$$$, and $$$C_2$$$ don't depend on the value of $$$c_i$$$, so we can freely change it to anything from $$$[0; s_{x, i}]$$$ to maximize the value of the formula. So:\n• If $$$C_1 \\ge C_2$$$, the value of the formula will not decrease if we set $$$c_i$$$ to $$$s_{x,i}$$$.\n• If $$$C_1 \\le C_2$$$, the value of the formula will not decrease if we set $$$c_i$$$ to $$$0$$$.\nSince at least one of this conditions has to be true, we can always take some optimal solution with $$$k$$$ indices such that $$$0 < c_i < s_{x, i}$$$ and get an optimal solution with $$$k - 1$$$ such indices. We repeat this process until $$$k$$$ becomes $$$0$$$, which means that $$$c_i = 0$$$ or $$$c_i = s_{x, i}$$$ for all $$$i$$$ from $$$1$$$ to $$$m_x$$$, as was stated in the claim.\n$$$ $$$\nConsider an optimal solution $$$c_1, c_2, \\ldots c_{m_x}$$$ where $$$c_i$$$ is equal to either $$$0$$$ or $$$s_{i, x}$$$. Let $$$S_1$$$ be the sum of $$$s_{i, x}$$$ over all $$$i$$$ such that $$$c_i = 0$$$ and $$$S_2$$$ be the sum of $$$s_{i, x}$$$ over all $$$i$$$ such that $$$c_i = s_{x, i}$$$. Then, the number of suitable pairs is equal to: $$$$$$S_1 \\cdot S_2$$$$$$\nIf we define $$$S$$$ as $$$s_{x, 1} + s_{x, 2} + \\ldots s_{x,m_x}$$$, we can see that $$$S_2$$$ is equal to $$$S - S_1$$$, so we are actually maximizing the value of: $$$$$$S_1 \\cdot (S - S_1)$$$$$$\nTo do this, we can iterate $$$S_1$$$ from $$$0$$$ to $$$S$$$, find if it is possible to choose some subset of indices $$$I \\subseteq \\{1, 2, \\ldots, m_x \\}$$$ such that $$$\\displaystyle\\sum_{i \\in I}{s_{x,i}} = S_1$$$, and update the current best answer if such a set $$$I$$$ exists.\nDoing this efficiently is known as the subset sum problem (or some variation of it). One of the simplest ways to solve it in this particular case with a $$$\\mathcal{O}(m_x \\cdot S)$$$ dynamic programming solution: $$$dp[j][S_1]$$$ is equal to $$$1$$$ if there exists a set of indices $$$I \\subseteq \\{ 1, 2, \\ldots, j \\}$$$ such that $$$\\displaystyle\\sum_{i \\in I}{s_{x,i}} = S_1$$$ and is equal to $$$0$$$ otherwise. We will proceed to optimize it.\nThe first standard optimization will bring the complexity down to $$$\\mathcal{O}(S \\sqrt S)$$$, you can read about it in Section 3 of this Codeforces blog or in the \"Subset Sum Speedup 1\" section of this Codeforces blog.\n$$$ $$$\nSince we are trying to maximize the value of $$$S_1 \\cdot (S - S_1)$$$, the optimal choice will also minimize the value of $$$| (S - S_1) - S_1 | = | S - 2S_1 |$$$.\nClaim: denote $$$\\max(s_{x,1}, \\ldots, s_{x,m_x})$$$ as $$$M_x$$$. If $$$M_x \\ge \\frac{S}{2}$$$, the smallest possible value of $$$| S - 2S_1 |$$$ is achieved with $$$S_1 = M_x$$$ or $$$S_1 = S - M_x$$$.\nProof: it is clearly possible to get $$$S_1 = M_x$$$. Suppose it is possible to get some value of $$$S_1$$$ in the segment $$$[S - M_x + 1; M_x - 1]$$$. Then, $$$M_x$$$ did not contribute to the sum $$$S_1$$$, and so $$$S_1 + M_x$$$ can't exceed $$$S$$$. But $$$S_1 + M_x \\ge S - M_x + 1 + M_x = S + 1$$$, so we get a contradiction, hence it is not possible to get some $$$S_1 \\in [S - M_x + 1; M_x - 1]$$$.\nSo if we have $$$M_x \\ge \\frac{S}{2}$$$, we actually don't have to run the dynamic programming part. We will now analyze the complexity of the solution with this optimization.\n$$$ $$$\nLet's now define $$$S_x$$$ as $$$s_{x, 1} + \\ldots + s_{x, m_x}$$$. Let's also call a vertex $$$x$$$ heavy if $$$M_x \\ge \\frac{S_x}{2}$$$ and light otherwise.\nSuppose that vertex $$$x$$$ has $$$k$$$ light ancestors on it's path to the root:\n• Every subtree of a light vertex $$$v$$$ has at most $$$\\frac{S_v}{2}$$$ vertices in it.\n• Every subtree of a heavy vertex $$$v$$$ has at most $$$S_v - 1$$$ vertices in it.\nFor each $$$k$$$ from $$$0$$$ to $$$n$$$, let $$$L_k$$$ be the set of light vertices with exactly $$$k$$$ light ancestors (a vertex doesn't count as it's own ancestor):\n• Note that if two light vertices $$$u$$$ and $$$v$$$ are in the same set $$$L_k$$$, one can't be the ancestor of the other. So, the sum $$$\\sum_{x \\in L_{k}} S_x$$$ does not exceed $$$n$$$.\n• $$$S_x \\le \\frac{n}{2^k}$$$ for all $$$x \\in L_{k}$$$.\nFor some $$$k$$$, let's calculate the total number of \"operations\" taken in the dynamic programming part for all $$$x \\in L_k$$$. To do that, we:\n• Iterate over the number of vertices in the set $$$L_{k}$$$, let's call it $$$p$$$, from $$$0$$$ to $$$n$$$.\n• Let $$$d_1, d_2, \\ldots, d_p$$$ be the sizes of the subtrees of vertices in $$$L_{k}$$$. Then we need to maximize the value of: $$$$$$d_1 \\sqrt d_1 + d_2 \\sqrt d_2 + \\ldots + d_p \\sqrt d_p$$$$$$ over all $$$d_1, d_2, \\ldots, d_p$$$ such that $$$1 \\le d_n \\le \\frac{n}{2^k}$$$ and $$$d_1 + \\ldots + d_n \\le n$$$.\nYou might have noticed that the above process can be simplified by allowing $$$d_i$$$ to be equal to $$$0$$$. In that case, the number of vertices $$$p$$$ is equal to the number of positive integers in $$$d_1, d_2, \\ldots, d_n$$$, and we need to maximize:\n$$$$$$d_1 \\sqrt d_1 + d_2 \\sqrt d_2 + \\ldots + d_n \\sqrt d_n$$$$$$\nover all integer $$$d_1, d_2, \\ldots , d_n$$$ such that $$$0 \\le d_n \\le \\frac{n}{2^k}$$$ and $$$d_1 + \\ldots + d_n \\le n$$$.\nLet $$$t := \\left\\lfloor \\frac{n}{2^k} \\right\\rfloor$$$. We'll assume $$$t \\ge 1$$$, otherwise, $$$d_i$$$ is equal to $$$0$$$ for all $$$1 \\le i \\le n$$$ and the sum is always equal to $$$0$$$.\nLemma 1: it is optimal to have $$$d_1 + d_2 + \\ldots + d_n = n$$$.\nProof: if we can increase some $$$d_i$$$, it is clearly optimal to do so.\nLemma 2: suppose the maximum is achieved on the set of values $$$e_1, e_2, \\ldots, e_n$$$. Then there does not exist a pair of indices $$$i$$$ and $$$j$$$ such that $$$0 < e_i \\le e_j < t$$$.\nProof: define $$$f(x) := \\left( x + 1 \\right) \\sqrt {x + 1} - x \\sqrt x$$$. Note that $$$f(x)$$$ is a monotonically increasing function for all $$$x \\ge 0$$$. You can prove it by, for example, analyzing it's derivative: $$$\\frac{3}{2} (\\sqrt {x + 1} - \\sqrt x)$$$.\nIf we decrease $$$e_i$$$ by $$$1$$$ and increase $$$e_j$$$ by $$$1$$$, the sum changes by: $$$$$$(e_j + 1) \\sqrt {e_j + 1} + (e_i - 1) \\sqrt {e_i - 1} - e_j \\sqrt e_j - e_i \\sqrt e_i =$$$$$$ $$$$$$ f(e_j) - f(e_i - 1) > 0$$$$$$\nBy doing this, we haven't broken any constraints on the values of $$$e_i$$$, but have increased the total sum, so the previous values of $$$e$$$ couldn't have been optimal.\nThis lemma gives us the nice property that the optimal values of $$$e_1, e_2, \\ldots, e_n$$$ look something like $$$t, \\ldots, t, y, 0, \\ldots, 0$$$; where $$$0 < y < t$$$.\nWith this we can conclude that the total number of \"operations\" performed for all vertices in $$$L_k$$$ is at most: $$$$$$\\left\\lceil \\frac{n}{t} \\right\\rceil \\cdot t \\sqrt t \\le \\left( \\frac{n}{t} + 1 \\right) \\cdot t \\sqrt t \\le (n + t) \\sqrt t \\le 2n \\sqrt t$$$$$$\nAdding this up over all $$$k$$$ with $$$t \\ge 1$$$, we get: $$$$$$2n \\cdot \\left(\\sqrt{\\left\\lfloor \\frac{n}{1} \\right\\rfloor} + \\sqrt{\\left\\lfloor \\frac{n}{2} \\right\\rfloor} + \\sqrt{\\left\\lfloor \\frac{n}{4} \\right\\rfloor} + \\sqrt{\\left\\lfloor \\frac{n}{8} \\right\\rfloor} + \\ldots + 1 \\right) \\le$$$$$$ $$$$$$2n \\cdot \\left( \\sqrt{\\frac{n}{1}} + \\sqrt{\\frac{n}{2}} + \\sqrt{\\frac{n}{4}} + \\sqrt{\\frac{n}{8}} + \\ldots \\right) =$$$$$$ $$$$$$2n \\sqrt n \\cdot \\left(1 + \\frac{1}{\\sqrt{2}} + \\frac{1}{\\sqrt{4}} + \\frac{1}{\\sqrt{8}} + \\ldots \\right) $$$$$$\nWe can notice that the sum in the brackets in bounded by the sum of an infinite geometric progression with a sum of $$$\\frac{\\sqrt{2}}{\\sqrt{2} - 1} \\le 4$$$, so the total sum is: $$$$$$\\le 2n \\sqrt n \\cdot 4 = 8n \\sqrt n = \\mathcal{O}(n \\sqrt n)$$$$$$\nFinally, we will also use bitsets to speed up our dynamic programming part and bring the complexity down to $$$\\mathcal{O}(\\frac{n \\sqrt n}{w})$$$, where $$$w$$$ is the word size, usually $$$32$$$ or $$$64$$$.\nYou can learn about bitsets in this Codeforces blog. To get a simple variable length bitset in C++, refer to this comment.\nComplexity: $$$\\mathcal{O}(\\frac{n \\sqrt n}{w})$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1857-A", "name": "Codeforces: Array Coloring"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Array Coloring (Rating: 800)\nTags: greedy, math\n\nYou are given an array consisting of $n$ integers. Your task is to determine whether it is possible to color all its elements in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored.\n\nFor example, if the array is [$1,2,4,3,2,3,5,4$], we can color it as follows: [$\\color{blue}{1},\\color{blue}{2},\\color{red}{4},\\color{blue}{3},\\color{red}{2},\\color{red}{3},\\color{red}{5},\\color{red}{4}$], where the sum of the blue elements is $6$ and the sum of the red elements is $18$.\n\nInput: The first line contains an integer $t$ ($1 \\le t \\le 1000$) — the number of test cases.\n\nEach test case begins with a line containing an integer $n$ ($2 \\le n \\le 50$) — the length of the array $a$.\n\nThe next line contains $n$ integers $a_1,a_2, \\dots, a_n$ ($1 \\le a_i \\le 50$) — the elements of the array $a$.\n\nOutput: For each test case, output \"YES\" (without quotes) if it is possible to color the array in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored, and \"NO\" otherwise.\n\nYou can output \"Yes\" and \"No\" in any case (for example, the strings \"yES\", \"yes\", and \"Yes\" will be recognized as correct answers).\n\nExamples:\nInput: 7\n8\n1 2 4 3 2 3 5 4\n2\n4 7\n3\n3 9 8\n2\n1 7\n5\n5 4 3 2 1\n4\n4 3 4 5\n2\n50 48\nOutput: YES\nNO\nYES\nYES\nNO\nYES\nYES\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's analyze the impact of adding odd or even numbers to a set with sum $$$S$$$:\n1. If you add an even element to the set, the parity of $$$S$$$ remains unchanged.\n2. If you add an odd element to the set, the parity of $$$S$$$ changes.\nBased on this observation, let's focus on the coloring of odd elements in the array. The number of odd elements colored in blue and the number of odd elements colored in red must have the same parity for a valid coloring. This is because we need to divide the odd elements into two parts (blue and red).\nTo achieve the same parity for both blue and red odd elements, the total number of odd elements in the array must be even. If it is not even, we won't be able to create two sets with the same parity.\nHence, the answer is \"YES\" if the number of odd elements in the array is even, and \"NO\" otherwise."}}1234{"type": "session", "harness": "opencode", "id": "cf-1857-B", "name": "Codeforces: Maximum Rounding"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Maximum Rounding (Rating: 1100)\nTags: greedy, implementation, math\n\nGiven a natural number $x$. You can perform the following operation:\n\n- choose a positive integer $k$ and round $x$ to the $k$-th digit\n\nNote that the positions are numbered from right to left, starting from zero. If the number has $k$ digits, it is considered that the digit at the $k$-th position is equal to $0$.\n\nThe rounding is done as follows:\n\n- if the digit at the $(k-1)$-th position is greater than or equal to $5$, then the digit at the $k$-th position is increased by $1$, otherwise the digit at the $k$-th position remains unchanged (mathematical rounding is used).\n- if before the operations the digit at the $k$-th position was $9$, and it should be increased by $1$, then we search for the least position $k'$ ($k'>k$), where the digit at the $k'$-th position is less than $9$ and add $1$ to the digit at the $k'$-th position. Then we assign $k=k'$.\n- after that, all digits which positions are less than $k$ are replaced with zeros.\n\nYour task is to make $x$ as large as possible, if you can perform the operation as many times as you want.\n\nFor example, if $x$ is equal to $3451$, then if you choose consecutively:\n\n- $k=1$, then after the operation $x$ will become $3450$\n- $k=2$, then after the operation $x$ will become $3500$\n- $k=3$, then after the operation $x$ will become $4000$\n- $k=4$, then after the operation $x$ will become $0$\n\nInput: The first line contains a single integer $t$ ($1\\le t\\le 10^4$) — the number of test cases.\n\nEach test case consists of positive integer $x$ with a length of up to $2 \\cdot 10^5$. It is guaranteed that there are no leading zeros in the integer.\n\nIt is guaranteed that the sum of the lengths of all integers $x$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput: For each set of input data, output the maximum possible value of $x$ after the operations. The number should not have leading zeros in its representation.\n\nExamples:\nInput: 10\n1\n5\n99\n913\n1980\n20444\n20445\n60947\n419860\n40862016542130810467\nOutput: 1\n10\n100\n1000\n2000\n20444\n21000\n100000\n420000\n41000000000000000000\n"}}
{"type": "message", "message": {"role": "assistant", "content": "First, sorry for the unclear statement. We have rewritten it for several times and have chosen the best one.\nLet's define $$$n$$$ as the length of the $$$x$$$.\nNotice, that after applying the rounding to $$$k$$$, all the digits to the right of $$$k$$$ become $$$0$$$. If the $$$k$$$-th digit is less than $$$5$$$, after the rounding it'll only worsen the answer.\nOn the other side, if the $$$k$$$-th digit is not less than $$$5$$$, than rounding to the $$$k$$$-th digit always leads to better answer, because after the operation $$$x$$$ will increase.\nFrom these observations, we can come up with the following greedy algorithm: look through all $$$k$$$ from $$$1$$$ to $$$n$$$, and if the $$$k$$$-th digit is not less than $$$5$$$, we use the rounding operation adding one to the ($$$k+1$$$)-th digit. And don't worry if that digit which must be increased by one is $$$9$$$, because in such case we always use the rounding operation on the next step."}}1234{"type": "session", "harness": "opencode", "id": "cf-1857-C", "name": "Codeforces: Assembly via Minimums"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Assembly via Minimums (Rating: 1200)\nTags: greedy, sortings\n\nSasha has an array $a$ of $n$ integers. He got bored and for all $i$, $j$ ($i < j$), he wrote down the minimum value of $a_i$ and $a_j$. He obtained a new array $b$ of size $\\frac{n\\cdot (n-1)}{2}$.\n\nFor example, if $a=$ [$2,3,5,1$], he would write [$\\min(2, 3), \\min(2, 5), \\min(2, 1), \\min(3, 5), \\min(3, 1), min(5, 1)$] $=$ [$2, 2, 1, 3, 1, 1$].\n\nThen, he randomly shuffled all the elements of the array $b$.\n\nUnfortunately, he forgot the array $a$, and your task is to restore any possible array $a$ from which the array $b$ could have been obtained.\n\nThe elements of array $a$ should be in the range $[-10^9,10^9]$.\n\nInput: The first line contains a single integer $t$ ($1\\le t\\le 200$) — the number of test cases.\n\nThe first line of each test case contains a single integer $n$ ($2\\le n\\le 10^3$) — the length of array $a$.\n\nThe second line of each test case contains $\\frac{n\\cdot (n-1)}{2}$ integers $b_1,b_2,\\dots,b_{\\frac{n\\cdot (n-1)}{2}}$ ($−10^9\\le b_i\\le 10^9$) — the elements of array $b$.\n\nIt is guaranteed that the sum of $n$ over all tests does not exceed $10^3$ and for each array $b$ in the test, there exists an original array.\n\nOutput: For each test case, output any possible array $a$ of length $n$.\n\nExamples:\nInput: 5\n3\n1 3 1\n2\n10\n4\n7 5 3 5 3 3\n5\n2 2 2 2 2 2 2 2 2 2\n5\n3 0 0 -2 0 -2 0 0 -2 -2\nOutput: 1 3 3\n10 10\n7 5 3 12\n2 2 2 2 2\n0 -2 0 3 5\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Suppose we have an array $$$a$$$ that we want to construct, with elements $$$a_1, a_2, \\dots, a_n$$$. To simplify the process, let's assume that the elements of $$$a$$$ are sorted in non-decreasing order, meaning $$$a_1 \\le a_2 \\le \\dots \\le a_n$$$.\nLet's start with $$$a_1$$$. Since the elements of $$$a$$$ are sorted, the pairs $$$(a_1, a_2), (a_1, a_3), \\dots, (a_1, a_n)$$$ will have $$$a_1$$$ as the smallest element in each pair. Therefore, the number of occurrences of $$$a_1$$$ in array $$$b$$$ will be $$$n-1$$$.\nMoving on to $$$a_2$$$, we already know that $$$a_1$$$ appears $$$n-1$$$ times in $$$b$$$. Since the elements of $$$a$$$ are sorted, all pairs involving $$$a_2$$$ will have $$$a_2$$$ as the second smallest element. This means $$$a_2$$$ will appear $$$n-2$$$ times in array $$$b$$$.\nWe continue this process for each element $$$a_i$$$ in $$$a$$$. The number of occurrences of $$$a_i$$$ in array $$$b$$$ will be $$$n-i$$$.\nWe can't determine the exact value of $$$a_n$$$ , because it won't be written to array b. Therefore, for $$$a_n$$$ we can choose any number in the range $$$[a_{n-1};10^9]$$$.\nIn case there are multiple elements $$$b_i$$$ in array $$$b$$$ that satisfy the condition for a particular $$$a_i$$$, we choose the smallest $$$b_i$$$. This greedy approach works, because we are constructing $$$a$$$ in non-decreasing order.\nThe complexity is $$$O(n^2 \\log n)$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1857-D", "name": "Codeforces: Strong Vertices"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Strong Vertices (Rating: 1300)\nTags: math, sortings, trees\n\nGiven two arrays $a$ and $b$, both of length $n$. Elements of both arrays indexed from $1$ to $n$. You are constructing a directed graph, where edge from $u$ to $v$ ($u\\neq v$) exists if $a_u-a_v \\ge b_u-b_v$.\n\nA vertex $V$ is called strong if there exists a path from $V$ to all other vertices.\n\nA path in a directed graph is a chain of several vertices, connected by edges, such that moving from the vertex $u$, along the directions of the edges, the vertex $v$ can be reached.\n\nYour task is to find all strong vertices.\n\nFor example, if $a=[3,1,2,4]$ and $b=[4,3,2,1]$, the graph will look like this:\n\nThe graph has only one strong vertex with number $4$\n\nInput: The first line contains an integer $t$ ($1\\le t\\le 10^4$) — the number of test cases.\n\nThe first line of each test case contains an integer $n$ ($2 \\le n \\le 2\\cdot 10^5$) — the length of $a$ and $b$.\n\nThe second line of each test case contains $n$ integers $a_1,a_2 \\dots a_n$ ($-10^9 \\le a_i \\le 10^9$) — the array $a$.\n\nThe third line of each test case contains $n$ integers $b_1,b_2 \\dots b_n$ ($-10^9 \\le b_i \\le 10^9$) — the array $b$.\n\nIt is guaranteed that the sum of $n$ for all test cases does not exceed $2\\cdot 10^5$.\n\nOutput: For each test case, output two lines: in the first line, output the number of strong vertices, and in the second line, output all strong vertices in ascending order.\n\nExamples:\nInput: 5\n4\n3 1 2 4\n4 3 2 1\n5\n1 2 4 1 2\n5 2 3 3 1\n2\n1 2\n2 1\n3\n0 2 1\n1 3 2\n3\n5 7 4\n-2 -3 -6\nOutput: 1\n4 \n2\n3 5 \n1\n2 \n3\n1 2 3 \n2\n2 3\n"}}
{"type": "message", "message": {"role": "assistant", "content": "The first step is to modify the inequality.\n$$$a_u-a_v \\geq b_u-b_v \\Leftrightarrow a_u-b_u \\geq a_v-b_v$$$.\nWe can create a new array $$$c$$$, where $$$c_i=a_i-b_i$$$ and our inequality is transformed to $$$c_u\\geq c_v$$$.\nSuppose the set $$$p_1,\\dots p_m$$$ is the set of such vertices $$$v$$$ that $$$c_v$$$ is maximum possible. From each $$$p_i$$$ there will be a path to all other vertices, because $$$c_{p_i}$$$ is not less than any other $$$c_u$$$, so the set $$$p$$$ is guaranteed will be in our answer.\nNow the question is whether there are other vertices in our answer?\nLet's prove that from any such vertex $$$v$$$, that is not maximum, there is no path to any vertex from $$$p$$$. The first observation, that there is no edge between $$$v$$$ and any $$$p_i$$$. So the path must go through other vertices. But even if there exist a path to another vertex $$$u$$$, $$$c_u$$$ will be still less that $$$c_{p_i}$$$, so it is impossible to get any $$$p_i$$$.\nIn such way we proved that the answer will always be the set of such $$$v$$$, that $$$c_v$$$ is maximized.\nThe complexity is $$$O(n)$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1857-E", "name": "Codeforces: Power of Points"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Power of Points (Rating: 1500)\nTags: math, sortings\n\nYou are given $n$ points with integer coordinates $x_1,\\dots x_n$, which lie on a number line.\n\nFor some integer $s$, we construct segments [$s,x_1$], [$s,x_2$], $\\dots$, [$s,x_n$]. Note that if $x_i<s$, then the segment will look like [$x_i,s$]. The segment [$a, b$] covers all integer points $a, a+1, a+2, \\dots, b$.\n\nWe define the power of a point $p$ as the number of segments that intersect the point with coordinate $p$, denoted as $f_p$.\n\nYour task is to compute $\\sum\\limits_{p=1}^{10^9}f_p$ for each $s \\in \\{x_1,\\dots,x_n\\}$, i.e., the sum of $f_p$ for all integer points from $1$ to $10^9$.\n\nFor example, if the initial coordinates are $[1,2,5,7,1]$ and we choose $s=5$, then the segments will be: $[1,5]$,$[2,5]$,$[5,5]$,$[5,7]$,$[1,5]$. And the powers of the points will be: $f_1=2, f_2=3, f_3=3, f_4=3, f_5=5, f_6=1, f_7=1, f_8=0, \\dots, f_{10^9}=0$. Their sum is $2+3+3+3+5+1+1=18$.\n\nInput: The first line contains an integer $t$ ($1\\le t\\le 10^4$) — the number of test cases.\n\nThe first line of each test case contains an integer $n$ ($1 \\le n \\le 2\\cdot 10^5$) — the number of points.\n\nThe second line contains $n$ integers $x_1,x_2 \\dots x_n$ ($1 \\le x_i \\le 10^9$) — the coordinates of the points.\n\nIt is guaranteed that the sum of the values of $n$ over all test cases does not exceed $2\\cdot 10^5$.\n\nOutput: For each test case, output $n$ integers, where the $i$-th integer is equal to the sum of the powers of all points for $s=x_i$.\n\nExamples:\nInput: 3\n3\n1 4 3\n5\n1 2 5 7 1\n4\n1 10 100 1000\nOutput: 8 7 6\n16 15 18 24 16\n1111 1093 1093 2893\n"}}
{"type": "message", "message": {"role": "assistant", "content": "If we have the segments $$$[l_1,r_1],\\dots,[l_n,r_n]$$$, the sum of $$$f_p$$$ is the sum of the segments' lengths. That's because a segment $$$[a,b]$$$ intersect exactly $$$b-a+1$$$ points. Now we can find the answer for fixed $$$s$$$ in $$$O(N)$$$.\nHow to do it more efficiently?\nLet's sort the given points so that $$$x_1\\le x_2\\le \\dots \\le x_n$$$.\nProcessing $$$s=x_i$$$ we get that for all $$$j\\le i$$$ the segments are $$$[x_j,s]$$$ and for all $$$j>i$$$ the segments are $$$[s,x_j]$$$. We need to summarize their lengths. Formally we need to calculate\n$$$$$$ \\sum\\limits_{j=1}^{i}(s-x_j+1)+\\sum\\limits_{j=i+1}^{n}(x_j-s+1)= $$$$$$\n$$$$$$ n+s \\cdot i-\\sum\\limits_{j=1}^{i}x_j+\\sum\\limits_{j=i+1}^{n}x_j -s(n-i)= $$$$$$\n$$$$$$ n+s(2\\cdot i-n)-\\sum\\limits_{j=1}^{i}x_j+\\sum\\limits_{j=i+1}^{n}x_j $$$$$$\nWe can maintain the sum of coordinates on the prefix and suffix, and calculate this formula for fixed $$$s$$$ in $$$O(1)$$$.\nThe total complexity is $$$O(n\\log n)$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1857-F", "name": "Codeforces: Sum and Product"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Sum and Product (Rating: 1600)\nTags: binary search, data structures, math\n\nYou have an array $a$ of length $n$.\n\nYour task is to answer $q$ queries: given $x,y$, find the number of pairs $i$ and $j$ ($1 \\le i < j \\le n$) that both $a_i + a_j = x$ and $a_i \\cdot a_j = y$.\n\nThat is, for the array $[1,3,2]$ and asking for $x=3,y=2$ the answer is $1$:\n\n- $i=1$ and $j=2$ fail because $1 + 3 = 4$ and not $3,$ also $1 \\cdot 3=3$ and not $2$;\n- $i=1$ and $j=3$ satisfies both conditions;\n- $i=2$ and $j=3$ fail because $3 + 2 = 5$ and not $3,$ also $3 \\cdot 2=6$ and not $2$;\n\nInput: The first line contains one integer $t$ ($1\\le t\\le 10^4$) — the number of test cases.\n\nThe second line of each test case contains one integer $n$ ($1 \\le n \\le 2\\cdot 10^5$) — the length of the array $a$.\n\nThe third line of each test case contains $n$ integers $a_1,a_2,\\dots,a_n$ ($1 \\le |a_i| \\le 10^9$) — array $a$.\n\nThe fourth line of each test case contains the integer $q$ ($1 \\le q \\le 2\\cdot 10^5$) — the number of requests.\n\nThe next $q$ lines contain two numbers each $x$ and $y$ ($1 \\le |x|\\le 2\\cdot 10^9,1\\le |y|\\le 10^{18}$) — request.\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $2\\cdot 10^5$. This is also guaranteed for the sum of $q$ values.\n\nOutput: For each test case print a line with $q$ numbers — the answers to the queries.\n\nExamples:\nInput: 3\n3\n1 3 2\n4\n3 2\n5 6\n3 1\n5 5\n4\n1 1 1 1\n1\n2 1\n6\n1 4 -2 3 3 3\n3\n2 -8\n-1 -2\n7 12\nOutput: 1 1 0 0 \n6 \n1 1 3\n"}}
{"type": "message", "message": {"role": "assistant", "content": "The system of equations in the statement resembles Vieta's formula for quadratic equations. If we have $$$$$$ \\begin{cases} a_i + a_j = b \\\\ a_i \\cdot a_j = c \\end{cases} $$$$$$ the quadratic equation will be $$$x^2 - bx + c = 0$$$, here $$$a_i$$$ and $$$a_j$$$ will be its roots.\nTo find the roots of the quadratic equation, we can use the discriminant formula, $$$D = b^2 - 4ac$$$. The roots will then be $$$x_1 = \\frac{b - \\sqrt{D}}{2}$$$ and $$$x_2 = \\frac{b + \\sqrt{D}}{2}$$$.\nOnce we have the potential integer values for $$$a_i$$$ and $$$a_j$$$, we can calculate the number of pairs by multiplying the number of occurrences, respectively.\nHowever, remember to consider some special cases:\n1. If $$$D < 0$$$, the equation won't have real roots.\n2. If $$$D = 0$$$, then $$$x_1 = x_2$$$, the formula for counting pairs in this case is different.\nThe complexity of this solution is $$$O((n + q) \\log n)$$$, using maps to store integer occurrences."}}1234{"type": "session", "harness": "opencode", "id": "cf-1857-G", "name": "Codeforces: Counting Graphs"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Counting Graphs (Rating: 2000)\nTags: combinatorics, divide and conquer, dsu, graphs, greedy, sortings, trees\n\nGiven a tree consisting of $n$ vertices. A tree is a connected undirected graph without cycles. Each edge of the tree has its weight, $w_i$.\n\nYour task is to count the number of different graphs that satisfy all four conditions:\n\n1. The graph does not have self-loops and multiple edges.\n2. The weights on the edges of the graph are integers and do not exceed $S$.\n3. The graph has exactly one minimum spanning tree.\n4. The minimum spanning tree of the graph is the given tree.\n\nTwo graphs are considered different if their sets of edges are different, taking into account the weights of the edges.\n\nThe answer can be large, output it modulo $998244353$.\n\nInput: The first line contains an integer $t$ ($1\\le t\\le 10^4$) — the number of test cases.\n\nThe first line of each test case contains two integers $n$ and $S$ ($2 \\le n \\le 2 \\cdot 10^5, 1\\le S\\le 10^9$) — the number of vertices and the upper bound of the weights.\n\nThen follow $n-1$ lines describing the tree, the $i$-th line contains three integers $u_i$, $v_i$, and $w_i$ ($1\\le u_i,v_i\\le n, u_i \\ne v_i, 1\\le w_i\\le S$) — an edge in the tree with weight $w_i$.\n\nIt is guaranteed that the sum of $n$ for all tests does not exceed $2\\cdot 10^5$.\n\nOutput: For each test, output the number of different graphs that satisfy the conditions, modulo $998244353$.\n\nExamples:\nInput: 4\n2 5\n1 2 4\n4 5\n1 2 2\n2 3 4\n3 4 3\n5 6\n1 2 3\n1 3 2\n3 4 6\n3 5 1\n10 200\n1 2 3\n2 3 33\n3 4 200\n1 5 132\n5 6 1\n5 7 29\n7 8 187\n7 9 20\n7 10 4\nOutput: 1\n8\n80\n650867886\n"}}
{"type": "message", "message": {"role": "assistant", "content": "The first observation is that the graphs will consist of $$$n$$$ vertices because the MST is fixed. Hence, the graphs will look like the given tree with some new vertices connected.\nThe next step is to determine the possible weights of a new edge between vertices $$$u$$$ and $$$v$$$. Let $$$P(u,v)$$$ be the maximum weight on the simple path from $$$u$$$ to $$$v$$$. I assume that we can add a new edge between vertices $$$u$$$ and $$$v$$$ with any weight in the range $$$[P(u,v)+1,S]$$$. This becomes clear when we consider some examples. If the edge has a weight less or equal than $$$P(u,v)$$$, the MST will change by taking the new edge instead of the edge with the maximal weight on the path.\nNotice that if we add a new edge, and it doesn't affect the MST, we can add one more edge independently from the previous.\nSo now the task is to calculate $$$\\prod\\limits_{1\\le u < v \\le n}{} (S-P(u,v)+1)$$$, because for each pair of vertices, we can assign a new weight from the range, which gives us $$$S-P(u,v)$$$ cases, or we can choose not to add any edge, which is one more case.\nNow, let's discuss how to calculate the formula efficiently:\n1. Sort the given edges in ascending order according to their weights: $$$w_1\\le w_2\\le \\dots \\le w_{n-1}$$$.\n2. We'll begin from the graph without edges, and add new ones step by step. Suppose, we already added all the edges up to $$$u_i,v_i,w_i$$$. Now, we want to add the $$$i$$$-th one. Notice that $$$w_i$$$ is greater than any of the weights before, and $$$u_i$$$ and $$$v_i$$$ are from different components.\n3. After adding the edge, we need to calculate the number of paths that go through this edge. If we know the sizes of the components containing $$$u_i$$$ and $$$v_i$$$, denoted as $$$s_u$$$ and $$$s_v$$$ respectively, then there exist $$$s_u\\cdot s_v-1$$$ paths through edge $$$u_i,v_i$$$ without including the path formed by these two vertices.\n4. We know the number of paths, and on each of these paths, we can determine the weight we can put on the edge. Thus, we need to multiply answer by $$$(S-w_i+1)^{s_u\\cdot s_v-1}$$$, using binary exponentiation.\nTo add edges and find the sizes of the components efficiently, you can use DSU (Disjoint Set Union).\nThe complexity of this approach is $$$O(n\\log n)$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1858-A", "name": "Codeforces: Buttons"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Buttons (Rating: 800)\nTags: games, greedy, math\n\nAnna and Katie ended up in a secret laboratory.\n\nThere are $a+b+c$ buttons in the laboratory. It turned out that $a$ buttons can only be pressed by Anna, $b$ buttons can only be pressed by Katie, and $c$ buttons can be pressed by either of them. Anna and Katie decided to play a game, taking turns pressing these buttons. Anna makes the first turn. Each button can be pressed at most once, so at some point, one of the girls will not be able to make her turn.\n\nThe girl who cannot press a button loses. Determine who will win if both girls play optimally.\n\nInput: The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases.\n\nEach test case consists of three integers $a$, $b$, and $c$ ($1 \\le a, b, c \\le 10^9$) — the number of buttons that can only be pressed by Anna, the number of buttons that can only be pressed by Katie, and the number of buttons that can be pressed by either of them, respectively.\n\nOutput: For each test case, output First if Anna wins, or Second if Katie wins.\n\nExamples:\nInput: 5\n1 1 1\n9 3 3\n1 2 3\n6 6 9\n2 2 8\nOutput: First\nFirst\nSecond\nFirst\nSecond\n"}}
{"type": "message", "message": {"role": "assistant", "content": "On each turn, the current player gets rid of one of the buttons available to them. At the same time, if you press the \"common\" button, the enemy will not be able to press it as well. Since each player wants to leave their opponent without buttons to press before they run out of those themselves, they will click on the \"common\" buttons as long as there is at least one available.\nThe number of the player who clicks on their (not the general) button first depends on $$$c$$$. It can be noticed that this player will win if and only if they have strictly more buttons than their opponent."}}1234{"type": "session", "harness": "opencode", "id": "cf-1858-B", "name": "Codeforces: The Walkway"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: The Walkway (Rating: 1500)\nTags: brute force, dp, greedy, math, number theory\n\nThere are $n$ benches near the Main Walkway in Summer Infomatics School. These benches are numbered by integers from $1$ to $n$ in order they follow. Also there are $m$ cookie sellers near the Walkway. The $i$-th ($1 \\le i \\le m$) cookie sellers is located near the $s_i$-th bench.\n\nPetya is standing in the beginning of the Walkway. He will pass near all benches starting from the $1$-st bench and ending with the $n$-th bench. Petya passes the distance between two consecutive benches in $1$ minute. He has a knapsack with an infinite amount of cookies. Petya is going to eat cookies from his knapsack and buy them from cookie sellers during the walk.\n\nPetya eats cookies only near the benches according to the following rule: he will eat the cookie near the $i$-th ($1 \\le i \\le n$) bench if and only if at least one of the following conditions holds:\n\n- There is a cookie seller near the $i$-th bench. Then Petya will buy a cookie from cookie seller and eat it immediately.\n- Petya has not yet eaten a cookie. Then Petya will take a cookie from his knapsack and eat it immediately.\n- At least $d$ minutes passed since Petya ate the previous cookie. In other words, Petya has not eaten a cookie near the benches $i-1, i-2, \\ldots, \\max(i-d+1, 1)$. Then Petya will take a cookie from his knapsack and eat it immediately.\n\nYou may assume that Petya eats cookies instantly. Petya will not eat two or more cookies near the same bench.\n\nYou want to minimize the number of cookies Petya will eat during his walk. In order to do this, you will ask the administration of the Summer Informatics School to remove exactly one cookie seller from the Walkway before Petya starts his walk.\n\nPlease determine the minimum possible number of cookies Petya can eat after removing exactly one cookie seller. Also determine the number of cookie sellers, such that if you remove one of them, Petya will eat the minimum possible number of cookies.\n\nInput: The first line contains a single integer $t$ ($1 \\le t \\le 10^3$) — the number of test cases.\n\nThe first line of each test case contains three integers $n$, $m$ and $d$ ($2 \\le d \\le n \\le 10^9$, $2 \\le m \\le \\min(10^{5}, n)$) — the number of benches, the number of cookie sellers and the value of parameter $d$ from the statement, respectively.\n\nThe second line of each test case contains $m$ integers $s_1, s_2, \\ldots, s_m$ ($1 \\le s_i \\le n$) — the locations of the cookie sellers. It is guaranteed that $s_{i} < s_{i+1}$ for all $1 \\leq i \\leq m - 1$.\n\nIt is guaranteed that the sum of $m$ over all test cases does not exceed $10^5$.\n\nOutput: For each test case print two integers — the minimum number of cookies that Petya can eat if exactly one cookie seller is removed, and the number of cookie sellers such that if one of them is removed, Petya will eat the minimum possible number of cookies.\n\nExamples:\nInput: 8\n6 2 2\n2 5\n8 3 2\n3 5 8\n10 4 9\n2 8 9 10\n30 5 8\n6 8 15 24 29\n30 5 8\n6 8 12 20 27\n8 8 3\n1 2 3 4 5 6 7 8\n2 2 2\n1 2\n1000000000 3 20000000\n57008429 66778899 837653445\nOutput: 3 1\n4 1\n4 4\n6 4\n5 2\n7 7\n1 1\n51 1\n"}}
{"type": "message", "message": {"role": "assistant", "content": "First, let's calculate how many cookies Petya will eat if we don't remove the cookie sellers at all (we will later refer to this value as $$$res$$$). Note that since the cookie sellers reset the time elapsed since the last eaten cookie, the number of cookies eaten on all segments between adjacent cookie sellers are counted independently. Therefore, we can easily calculate $$$res$$$: let's iterate through the cookie sellers from $$$1$$$ to $$$m-1$$$. For each of them, we should add the number $$$\\left( \\left\\lfloor\\frac{s_{i+1}-s_{i}-1}{d}\\right\\rfloor + 1 \\right)$$$ to $$$res$$$. We should also carefully take into account the cookies that Petya will eat on the segments $$$[1;s_{1}-1]$$$ and $$$[s_{m}, n]$$$ (it might help to assume that there are two more cookie sellers at positions $$$1-d$$$ and $$$n+1$$$).\nIn order to find the minimum possible number of cookies eaten by Petya, we will fix the cookie seller that we remove. Let it be the cookie seller $$$i$$$. Then the number of cookies eaten by Petya will be $$$$$$res - \\left\\lfloor\\frac{s_{i}-s_{i-1}-1}{d}\\right\\rfloor - \\left\\lfloor\\frac{s_{i+1}-s_{i}-1}{d}\\right\\rfloor + \\left\\lfloor\\frac{s_{i+1}-s_{i-1}-1}{d}\\right\\rfloor - 1$$$$$$ (for the first and last cookie sellers, the formulas may differ slightly). Thus, we can find the number of cookies that Petya will eat if a certain cookie seller is removed in $$$O(1)$$$. After going through all $$$m$$$ options, we will be able to find the answer to the problem.\nThe final complexity of the solution is $$$\\mathcal{O}(m)$$$ (because array $$$s$$$ is sorted in the input)."}}1234{"type": "session", "harness": "opencode", "id": "cf-1858-C", "name": "Codeforces: Yet Another Permutation Problem"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Yet Another Permutation Problem (Rating: 1000)\nTags: constructive algorithms, greedy, math, number theory\n\nAlex got a new game called \"GCD permutations\" as a birthday present. Each round of this game proceeds as follows:\n\n- First, Alex chooses a permutation$^{\\dagger}$ $a_1, a_2, \\ldots, a_n$ of integers from $1$ to $n$.\n- Then, for each $i$ from $1$ to $n$, an integer $d_i = \\gcd(a_i, a_{(i \\bmod n) + 1})$ is calculated.\n- The score of the round is the number of distinct numbers among $d_1, d_2, \\ldots, d_n$.\n\nAlex has already played several rounds so he decided to find a permutation $a_1, a_2, \\ldots, a_n$ such that its score is as large as possible.\n\nRecall that $\\gcd(x, y)$ denotes the greatest common divisor (GCD) of numbers $x$ and $y$, and $x \\bmod y$ denotes the remainder of dividing $x$ by $y$.\n\n$^{\\dagger}$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).\n\nInput: The first line of the input contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of test cases.\n\nEach test case consists of one line containing a single integer $n$ ($2 \\le n \\le 10^5$).\n\nIt is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.\n\nOutput: For each test case print $n$ distinct integers $a_{1},a_{2},\\ldots,a_{n}$ ($1 \\le a_i \\le n$) — the permutation with the largest possible score.\n\nIf there are several permutations with the maximum possible score, you can print any one of them.\n\nExamples:\nInput: 4\n5\n2\n7\n10\nOutput: 1 2 4 3 5 \n1 2 \n1 2 3 6 4 5 7 \n1 2 3 4 8 5 10 6 9 7\n"}}
{"type": "message", "message": {"role": "assistant", "content": "It is impossible to get $$$d_i = \\gcd(a_i, a_{(i\\mod n) + 1}) > \\left\\lfloor \\frac{n}{2}\\right\\rfloor$$$: otherwise, at least one of the numbers in $$$a$$$ would be divisible by $$$d_i$$$ and would be greater than $$$d_i$$$ at the same time, so it would be at least $$$2 \\cdot d_i$$$, which is greater than $$$n$$$. Therefore, the maximum possible score is no more than $$$\\left\\lfloor\\frac{n}{2}\\right\\rfloor$$$. Actually, we can always get a score equal to $$$\\left\\lfloor\\frac{n}{2}\\right\\rfloor$$$.\nHow do we get such score? Let's set $$$a_1= 1$$$. After that, we put the powers of $$$2$$$ less or equal $$$n$$$ sequentially. Then we put $$$3$$$ and powers of $$$2$$$ multiplied by $$$3$$$, then $$$5$$$ and so on (for example, for $$$n=12$$$, we will get an array $$$a=[1,2,4,8,3,6,12,5,10,7,9,11]$$$). Then, for each number $$$a_i = x \\leq \\lfloor \\frac{n}{2} \\rfloor$$$, the next number will be $$$a_{(i\\bmod n) + 1} = x \\cdot 2 \\leq n$$$. Their $$$\\gcd$$$ will be exactly $$$x$$$, so there will be a pair of adjacent elements of $$$a$$$ with greatest common divisor equal to $$$x$$$ for all $$$1 \\leq x \\leq \\lfloor\\frac{n}{2}\\rfloor$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1858-D", "name": "Codeforces: Trees and Segments"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Trees and Segments (Rating: 2200)\nTags: brute force, data structures, dp, greedy, two pointers\n\nThe teachers of the Summer Informatics School decided to plant $n$ trees in a row, and it was decided to plant only oaks and firs. To do this, they made a plan, which can be represented as a binary string $s$ of length $n$. If $s_i = 0$, then the $i$-th tree in the row should be an oak, and if $s_i = 1$, then the $i$-th tree in the row should be a fir.\n\nThe day of tree planting is tomorrow, and the day after tomorrow an inspector will come to the School. The inspector loves nature very much, and he will evaluate the beauty of the row as follows:\n\n- First, he will calculate $l_0$ as the maximum number of consecutive oaks in the row (the maximum substring consisting of zeros in the plan $s$). If there are no oaks in the row, then $l_0 = 0$.\n- Then, he will calculate $l_1$ as the maximum number of consecutive firs in the row (the maximum substring consisting of ones in the plan $s$). If there are no firs in the row, then $l_1 = 0$.\n- Finally, he will calculate the beauty of the row as $a \\cdot l_0 + l_1$ for some $a$ — the inspector's favourite number.\n\nThe teachers know the value of the parameter $a$, but for security reasons they cannot tell it to you. They only told you that $a$ is an integer from $1$ to $n$.\n\nSince the trees have not yet been planted, the teachers decided to change the type of no more than $k$ trees to the opposite (i.e., change $s_i$ from $0$ to $1$ or from $1$ to $0$ in the plan) in order to maximize the beauty of the row of trees according to the inspector.\n\nFor each integer $j$ from $1$ to $n$ answer the following question independently:\n\n- What is the maximum beauty of the row of trees that the teachers can achieve by changing the type of no more than $k$ trees if the inspector's favourite number $a$ is equal to $j$?\n\nInput: The first line contains a single integer $t$ ($1 \\le t \\le 1000$) — the number of test cases.\n\nThe first line of each test case contains two integers $n$ and $k$ ($1 \\le n \\le 3000$, $0 \\le k \\le n$) — the number of trees in the row and the maximum number of changes.\n\nThe second line contains a string $s$ of length $n$, consisting of zeros and ones — the plan description.\n\nIt is guaranteed that the sum of all $n$ values for all test cases does not exceed $3000$.\n\nOutput: For each test case, print $n$ integers, the $j$-th ($1 \\le j \\le n$) of which is the maximum beauty of the row of trees after no more than $k$ changes if $a = j$ is used to calculate the beauty.\n\nExamples:\nInput: 5\n3 0\n111\n4 1\n0110\n5 0\n10000\n6 2\n101101\n7 1\n0001101\nOutput: 3 3 3 \n4 5 7 9 \n5 9 13 17 21 \n6 9 13 17 21 25 \n7 10 13 17 21 25 29\n"}}
{"type": "message", "message": {"role": "assistant", "content": "There are many various dynamic programming solutions of this problem. We will describe one of them. Let's calculate the dynamics $$$pref_{i, \\ j}$$$ = the length of the longest subsegment of zeros that can be obtained on the prefix up to $$$i$$$, which ends at index $$$i$$$ and costs exactly $$$j$$$ operations. Similarly, $$$suf_{i, \\ j}$$$ is the length of the longest subsegment of zeros on the suffix starting at $$$i$$$, which starts at index $$$i$$$ and costs exactly $$$j$$$ operations. Such dynamics can be easily computed: $$$$$$ pref_{i, \\ j} = \\left\\{ \\begin{array} \\\\ pref_{i - 1, \\ j} + 1 & \\text{if} & s_i = 0 \\\\ pref_{i - 1, \\ j - 1} + 1 & \\text{if} & s_i = 1 & \\text{and} & j > 0 \\\\ 0 & \\text{otherwise} \\end{array}\\right. $$$$$$ In the first case, we simply prolong the existing subsegment of zeros, in the second case, we change the current $$$1$$$ to $$$0$$$, spending one operation on it (so if have $$$0$$$ operations left ($$$j = 0$$$), we cannot do anything and the value of the dynamics is $$$0$$$, meaning the segment has ended). $$$suf_{i,j}$$$ can be calculated similarly. Let's update both dynamics in such a way that $$$pref_{i,j}$$$ will mean the maximum length of a subsegment of zeros that ends no later than $$$i$$$ and costs no more than $$$j$$$ operations. This can be easily done by updating $$$pref_{i,j}$$$ with the value of $$$pref_{i - 1,j}$$$, and then with $$$pref_{i,j - 1}$$$. Similarly, we update the second dynamics.\nNow let's consider a subsegment [$$$l, \\ r]$$$ that we want to convert into a segment of ones. We can easily calculate the number of operations $$$x$$$ that we will need (we'll just need to calculate the number of zeros in such a segment). Now, calculate the new dynamics $$$dp_{len}$$$ for the length $$$len = r - l + 1$$$ of the segment of ones, which equals the maximum length of a subsegment of zeros that we can obtain. Update this value with $$$\\max(pref_{l - 1, k - x}, suf_{r + 1, k - x})$$$.\nThen, to answer the question for a fixed number $$$a$$$, we can iterate over the length $$$len$$$ of the segment of ones that will be in our answer and update the answer with the value $$$a \\cdot dp_{len} + len$$$, if there exists a value for $$$len$$$ in the dynamics $$$dp$$$.\nThe complexity is $$$O(nk + n^2)$$$. Solutions with complexity $$$O(nk \\log n)$$$ and $$$O(nk)$$$ using various optimizations of the dynamics (convex hull trick, D&C) also exist."}}1234{"type": "session", "harness": "opencode", "id": "cf-1858-E2", "name": "Codeforces: Rollbacks (Hard Version)"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Rollbacks (Hard Version) (Rating: 2600)\nTags: data structures, interactive, trees\n\nThis is a hard version of this problem. The only difference between the versions is that you have to solve the hard version in online mode. You can make hacks only if both versions of the problem are solved.\n\nYou have an array $a$, which is initially empty. You need to process queries of the following types:\n\n- + $x$ — add the integer $x$ to the end of the array $a$.\n- - $k$ — remove the last $k$ numbers from the array $a$.\n- ! — roll back the last active change (i.e., make the array $a$ the way it was before the change). In this problem, only queries of the first two types (+ and -) are considered as changes.\n- ? — find the number of distinct numbers in the array $a$.\n\nInput: The first line contains an integer $q$ ($1 \\leq q \\leq 10^6$) — the number of queries.\n\nThe next $q$ lines contain the queries as described above.\n\nIt is guaranteed that\n\n- in the queries of the first type, $1 \\le x \\le 10^6$;\n- in the queries of the second type, $k \\ge 1$ and $k$ does not exceed the current length of the array $a$;\n- at the moment of the queries of the third type, there is at least one query of the first or of the second type that can be rolled back.\n\nIt is also guaranteed that the number of queries of the fourth type (?) does not exceed $10^5$.\n\nNote that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program.\n\nOutput: For each query of the fourth type output one integer — the number of distinct elements in array $a$ at the moment of query.\n\nExamples:\nInput: 10\n+ 1\n+ 2\n+ 2\n?\n!\n+ 3\n- 2\n?\n+ 1\n?\nOutput: 2\n1\n1\n\nInput: 6\n+ 1\n+ 1000000\n?\n!\n!\n?\nOutput: 2\n0\n"}}
{"type": "message", "message": {"role": "assistant", "content": "First, let's learn how to solve the problem without rollbacks. Let $$$b$$$ be an array of the same length as $$$a$$$, where $$$b_i=1$$$ if $$$i$$$ is the minimum position at which the number $$$a_i$$$ is in the array $$$a$$$, and $$$b_i=0$$$ otherwise. Then the number of different numbers in the array $$$a$$$ is equal to the sum of all the elements of the array $$$b$$$.\nThe $$$b$$$ array can be maintained using a Fenwick tree, a segment tree, or any other data structure that supports point updates and range sum queries. The author's solution uses the Fenwick tree.\nLet's use the method that is often used when implementing a stack or a deque: we create a large array $$$A$$$ and at each moment we store the index of the last \"existing\" element $$$r$$$ (this is just the size of the array $$$a$$$ at this moment). The array $$$a$$$ itself will be a prefix of $$$A$$$, that is, $$$a_{i}=A_{i}$$$ for $$$i\\leq len(a)$$$. Also, for each value $$$val$$$ we maintain std::set of indexes on which the $$$val$$$ value is located in the $$$A$$$ array (in $$$A$$$, not in $$$a$$$).\nThen for the operation of removing $$$k$$$ elements from the end of the array $$$a$$$, it is enough to reduce the value of the index $$$r$$$ by $$$k$$$. This operation works in $$$O(1)$$$.\nWhen adding one element $$$x$$$ to the end of the array, we need to check if it has been encountered before, and change one element in the Fenwick tree accordingly. This can be done in $$$O(\\log{q})$$$ using std::set for the corresponding value. You also need to increase $$$r$$$ by 1, assign $$$A_{r} =x$$$ after that, and update the corresponding std::set. This operation works for $$$O(\\log{q})$$$.\nIn order to find the number of different numbers in $$$a$$$, we need to find the sum in the Fenwick tree on the prefix of length $$$r$$$ in the array $$$b$$$. This operation works in $$$O(\\log{q})$$$.\nNow, we need to learn how to roll back operations. Note that we perform the deletion operation in $$$O(1)$$$, and the addition operation in $$$O(\\log{q})$$$, so we can roll back these operations with the same asymptotics. We can just store a stack of all changes, and remember everything that we changed during the operations.\nThe final asymptotics is $$$O(q\\log{q})$$$."}}1234{"type": "session", "harness": "opencode", "id": "cf-1859-A", "name": "Codeforces: United We Stand"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: United We Stand (Rating: 800)\nTags: constructive algorithms, math, number theory\n\nGiven an array $a$ of length $n$, containing integers. And there are two initially empty arrays $b$ and $c$. You need to add each element of array $a$ to exactly one of the arrays $b$ or $c$, in order to satisfy the following conditions:\n\n- Both arrays $b$ and $c$ are non-empty. More formally, let $l_b$ be the length of array $b$, and $l_c$ be the length of array $c$. Then $l_b, l_c \\ge 1$.\n- For any two indices $i$ and $j$ ($1 \\le i \\le l_b, 1 \\le j \\le l_c$), $c_j$ is not a divisor of $b_i$.\n\nOutput the arrays $b$ and $c$ that can be obtained, or output $-1$ if they do not exist.\n\nInput: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 500$) — the number of test cases. The description of the test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($2 \\le n \\le 100$) — the length of array $a$.\n\nThe second line of each test case contains $n$ integers $a_1, a_2, \\ldots, a_n$ ($1 \\le a_i \\le 10^9$) — the elements of array $a$.\n\nOutput: For each test case, output a single integer $-1$ if a solution does not exist.\n\nOtherwise, in the first line, output two integers $l_b$ and $l_c$ — the lengths of arrays $b$ and $c$ respectively.\n\nIn the second line, output $l_b$ integers $b_1, b_2, \\ldots, b_{l_b}$ — the elements of array $b$.\n\nIn the third line, output $l_c$ integers $c_1, c_2, \\ldots, c_{l_c}$ — the elements of array $c$.\n\nIf there are multiple solutions, output any of them. You can output the elements of the arrays in any order.\n\nExamples:\nInput: 5\n3\n2 2 2\n5\n1 2 3 4 5\n3\n1 3 5\n7\n1 7 7 2 9 1 4\n5\n4 8 12 12 4\nOutput: -1\n3 2\n1 3 5 \n2 4 \n1 2\n1 \n3 5 \n2 5\n1 1 \n2 4 7 7 9 \n3 2\n4 8 4 \n12 12\n"}}
{"type": "message", "message": {"role": "assistant", "content": "First, if all numbers are equal, then there is no answer (since $$$a$$$ is divisible by $$$a$$$, if both arrays are non-empty, then $$$c_1$$$ is a divisor of $$$b_1$$$).\nSecond, if $$$a$$$ is divisible by $$$b$$$ and they are both natural numbers, then the following equality holds: $$$b \\le a$$$ (by definition, if $$$a$$$ is divisible by $$$b$$$, then $$$a$$$ can be represented as $$$k \\dot b$$$, where $$$k$$$ is a natural number).\nNow we can place all instances of the smallest number into $$$b$$$, and all other numbers into $$$c$$$. It can be seen that such a construction always gives a valid answer."}}1234{"type": "session", "harness": "opencode", "id": "cf-1859-B", "name": "Codeforces: Olya and Game with Arrays"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Olya and Game with Arrays (Rating: 1000)\nTags: constructive algorithms, greedy, math, sortings\n\nArtem suggested a game to the girl Olya. There is a list of $n$ arrays, where the $i$-th array contains $m_i \\ge 2$ positive integers $a_{i,1}, a_{i,2}, \\ldots, a_{i,m_i}$.\n\nOlya can move at most one (possibly $0$) integer from each array to another array. Note that integers can be moved from one array only once, but integers can be added to one array multiple times, and all the movements are done at the same time.\n\nThe beauty of the list of arrays is defined as the sum $\\sum_{i=1}^n \\min_{j=1}^{m_i} a_{i,j}$. In other words, for each array, we find the minimum value in it and then sum up these values.\n\nThe goal of the game is to maximize the beauty of the list of arrays. Help Olya win this challenging game!\n\nInput: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 25000$) — the number of test cases. The description of test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 25000$) — the number of arrays in the list.\n\nThis is followed by descriptions of the arrays. Each array description consists of two lines.\n\nThe first line contains a single integer $m_i$ ($2 \\le m_i \\le 50000$) — the number of elements in the $i$-th array.\n\nThe next line contains $m_i$ integers $a_{i, 1}, a_{i, 2}, \\ldots, a_{i, m_i}$ ($1 \\le a_{i,j} \\le 10^9$) — the elements of the $i$-th array.\n\nIt is guaranteed that the sum of $m_i$ over all test cases does not exceed $50000$.\n\nOutput: For each test case, output a single line containing a single integer — the maximum beauty of the list of arrays that Olya can achieve.\n\nExamples:\nInput: 3\n2\n2\n1 2\n2\n4 3\n1\n3\n100 1 6\n3\n4\n1001 7 1007 5\n3\n8 11 6\n2\n2 9\nOutput: 5\n1\n19\n"}}
{"type": "message", "message": {"role": "assistant", "content": "To increase the answer for each array separately, it is necessary to move the minimum to another array. Then, notice that it is optimal to move all the minimums to one array. Let's figure out which array. After moving the minimum from an array, the second minimum in the original array becomes the new minimum. Then, it is easy to notice that it is optimal to move all the minimums to the array with the smallest second minimum. After all the movements, we will have one array where the minimum element is the smallest number among all the arrays, and $$$n-1$$$ arrays where the minimum element is the second minimum in the original array.\nTherefore, the answer to the problem will be $$$M + K - S$$$, where $$$M$$$ is the minimum element among all the arrays, $$$K$$$ is the sum of all the second minimums, and $$$S$$$ is the smallest second minimum."}}1234{"type": "session", "harness": "opencode", "id": "cf-1859-D", "name": "Codeforces: Andrey and Escape from Capygrad"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Andrey and Escape from Capygrad (Rating: 1800)\nTags: binary search, data structures, dp, dsu, greedy, sortings\n\nAn incident occurred in Capygrad, the capital of Tyagoland, where all the capybaras in the city went crazy and started throwing mandarins. Andrey was forced to escape from the city as far as possible, using portals.\n\nTyagoland is represented by a number line, and the city of Capygrad is located at point $0$. There are $n$ portals all over Tyagoland, each of which is characterised by four integers $l_i$, $r_i$, $a_i$ and $b_i$ ($1 \\le l_i \\le a_i \\le b_i \\le r_i \\le 10^9$). Note that the segment $[a_i, b_i]$ is contained in the segment $[l_i, r_i]$.\n\nIf Andrey is on the segment $[l_i, r_i]$, then the portal can teleport him to any point on the segment $[a_i, b_i]$. Andrey has a pass that allows him to use the portals an unlimited number of times.\n\nAndrey thinks that the point $x$ is on the segment $[l, r]$ if the inequality $l \\le x \\le r$ is satisfied.\n\nAndrey has $q$ options for where to start his escape, each option is characterized by a single integer $x_i$ — the starting position of the escape. He wants to escape from Capygrad as far as possible (to the point with the maximum possible coordinate). Help Andrey determine how far he could escape from Capygrad, starting at each of the $q$ positions.\n\nInput: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \\le t \\le 10^4$) — the number of sets of test cases. The description of test cases follows.\n\nThe first line of each test case contains a single integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) — the number of portals.\n\nEach of the next $n$ lines contains four integers $l_i$, $r_i$, $a_i$, and $b_i$ $(1 \\le l_i \\le a_i \\le b_i \\le r_i \\le 10^9)$ — the characteristics of the portals.\n\nThe next line contains a single integer $q$ ($1 \\le q \\le 2 \\cdot 10^5$) — the number of positions.\n\nThe following line contains $q$ integers $x_1, x_2, \\ldots, x_q$ ($1 \\le x_i \\le 10^9$) — the position from which Andrey will start his escape in the $i$-th options.\n\nIt is guaranteed that the sum of $n$ and the sum of $q$ over all test cases does not exceed $2 \\cdot 10^5$.\n\nOutput: For each test case, output a single line of $q$ integers, containing the answers to Andrey's questions.\n\nExamples:\nInput: 5\n3\n6 17 7 14\n1 12 3 8\n16 24 20 22\n6\n10 2 23 15 28 18\n3\n3 14 7 10\n16 24 20 22\n1 16 3 14\n9\n2 4 6 8 18 23 11 13 15\n2\n1 4 2 3\n3 9 6 7\n3\n4 8 1\n5\n18 24 18 24\n1 8 2 4\n11 16 14 14\n26 32 28 30\n5 10 6 8\n9\n15 14 13 27 22 17 31 1 7\n6\n9 22 14 20\n11 26 13 24\n21 33 22 23\n21 33 25 32\n1 6 3 4\n18 29 20 21\n8\n11 23 16 5 8 33 2 21\nOutput: 14 14 23 15 28 22 \n14 14 14 14 22 23 14 14 15 \n7 8 7 \n15 14 14 30 24 17 31 4 8 \n32 32 32 5 8 33 4 32\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Statement: It is always beneficial to teleport to point $$$b_i$$$.\nProof: Let's assume that we were able to teleport from point $$$X$$$ to the right of $$$b_i$$$, but not from $$$b_i$$$. Then we used some segment $$$A$$$ that covers point $$$X$$$, but does not cover point $$$b$$$, and ends to the right of $$$b$$$. This is a contradiction.\nLet $$$ans_i$$$ be the maximum coordinate we can reach while being on segment $$$i$$$, and let $$$p_j$$$ be the answer to the $$$j$$$-th query. Then we notice that the answer to query $$$p_j = \\max(x_j, \\max_{i = 1}^n \\{ans_i \\vert l_i \\le x_j \\le r_i\\})$$$.\nWe will use the scanline method from the end. Events: $$$l_i$$$, $$$b_i$$$, $$$r_i$$$, $$$x_j$$$.\nWe notice that events of type $$$r_i$$$ are not important to us when scanning from the end (according to statement number 1). It is important for us that we process events of type $$$b_i$$$ first, then events of type $$$x_j$$$, and then events of type $$$l_i$$$ (closing the segment in the scanline).\nWe will go through the events of the scanline, processing them in the order of $$$b_i$$$, then events of type $$$x_j$$$, and then events of type $$$l_i$$$.\nWe assume that there is a data structure that allows us to add elements, remove elements, and quickly output the maximum.\n• For each event of type $$$b_i$$$, update the value of $$$ans_i$$$ - take the maximum value of $$$ans$$$ of all open segments from the structure.\n• For each event of type $$$x_j$$$, update the value of $$$p_j$$$ - take the maximum value of $$$ans$$$ of all open segments from the structure, as well as $$$x_j$$$.\n• For each event of type $$$l_i$$$, remove $$$ans_i$$$ from the structure.\nWe notice that to solve this problem, we can use the $$$\\bf{std::multiset}$$$ container, which automatically sorts elements in ascending order. We can store in $$$multiset$$$ $$$ans_i$$$ of all open segments. And then, when processing events, extract the maximum from $$$multiset$$$, all operations are performed in $$$O(\\log n)$$$. This allows us to solve the problem in $$$O((n + q) \\log n)$$$ time and $$$O(n)$$$ memory."}}1234{"type": "session", "harness": "opencode", "id": "cf-1860-A", "name": "Codeforces: Not a Substring"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Not a Substring (Rating: 900)\nTags: constructive algorithms, strings\n\nA bracket sequence is a string consisting of characters '(' and/or ')'. 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:\n\n- bracket sequences \"()()\" and \"(())\" are regular (they can be transformed into \"(1)+(1)\" and \"((1+1)+1)\", respectively);\n- bracket sequences \")(\", \"(\" and \")\" are not regular.\n\nYou are given a bracket sequence $s$; let's define its length as $n$. Your task is to find a regular bracket sequence $t$ of length $2n$ such that $s$ does not occur in $t$ as a contiguous substring, or report that there is no such sequence.\n\nInput: The first line contains a single integer $t$ ($1 \\le t \\le 1000$) — the number of test cases.\n\nThe only line of each test case contains a string $s$ ($2 \\le |s| \\le 50$), consisting of characters \"(\" and/or \")\".\n\nOutput: For each test case, print the answer to it. If there is no required regular bracket sequence, print NO in a separate line. Otherwise, print YES in the first line, and the required regular bracket sequence $t$ itself in the second line. If there are multiple answers — you may print any of them.\n\nExamples:\nInput: 4\n)(\n(()\n()\n))()\nOutput: YES\n(())\nYES\n()()()\nNO\nYES\n()(()())\n"}}
{"type": "message", "message": {"role": "assistant", "content": "Let's consider the following two cases:\n• the string $$$s$$$ contains two consecutive equal characters, for example, \")(((\" or \"())\". In this case, we can choose a string $$$t$$$ of the form \"()()()\", since it does not contain a substring of two equal characters, therefore $$$s$$$ is not a substring of $$$t$$$;\n• in the string $$$s$$$, all adjacent characters are different, i.e., it is alternating. In this case, we can choose a string $$$t$$$ of the form \"((()))\", since here the length of the longest alternating substring is $$$2$$$. The only alternating substring it contains is \"()\", which is a part of every regular bracket sequence.\nThus, it is sufficient to consider the answer among only two candidates — \"()()()\" and \"((()))\" of length $$$2n$$$."}}cf/cf-1848-B.jsonlAdded+4
cf/cf-1848-C.jsonlAdded+4
cf/cf-1848-D.jsonlAdded+4
cf/cf-1848-E.jsonlAdded+4
cf/cf-1848-F.jsonlAdded+4
cf/cf-1849-A.jsonlAdded+4
cf/cf-1849-B.jsonlAdded+4
cf/cf-1849-C.jsonlAdded+4
cf/cf-1849-D.jsonlAdded+4
cf/cf-1849-E.jsonlAdded+4
cf/cf-1849-F.jsonlAdded+4
cf/cf-1850-A.jsonlAdded+4
cf/cf-1850-B.jsonlAdded+4
cf/cf-1850-C.jsonlAdded+4
cf/cf-1850-D.jsonlAdded+4
cf/cf-1850-E.jsonlAdded+4
cf/cf-1850-F.jsonlAdded+4
cf/cf-1850-G.jsonlAdded+4
cf/cf-1850-H.jsonlAdded+4
cf/cf-1851-A.jsonlAdded+4
cf/cf-1851-B.jsonlAdded+4
cf/cf-1851-C.jsonlAdded+4
cf/cf-1851-D.jsonlAdded+4
cf/cf-1851-E.jsonlAdded+4
cf/cf-1851-F.jsonlAdded+4
cf/cf-1851-G.jsonlAdded+4
cf/cf-1854-E.jsonlAdded+4
cf/cf-1854-F.jsonlAdded+4
cf/cf-1856-A.jsonlAdded+4
cf/cf-1856-B.jsonlAdded+4
cf/cf-1856-C.jsonlAdded+4
cf/cf-1856-D.jsonlAdded+4
cf/cf-1856-E1.jsonlAdded+4
cf/cf-1856-E2.jsonlAdded+4
cf/cf-1857-A.jsonlAdded+4
cf/cf-1857-B.jsonlAdded+4
cf/cf-1857-C.jsonlAdded+4
cf/cf-1857-D.jsonlAdded+4
cf/cf-1857-E.jsonlAdded+4
cf/cf-1857-F.jsonlAdded+4
cf/cf-1857-G.jsonlAdded+4
cf/cf-1858-A.jsonlAdded+4
cf/cf-1858-B.jsonlAdded+4
cf/cf-1858-C.jsonlAdded+4
cf/cf-1858-D.jsonlAdded+4
cf/cf-1858-E2.jsonlAdded+4
cf/cf-1859-A.jsonlAdded+4
cf/cf-1859-B.jsonlAdded+4
cf/cf-1859-D.jsonlAdded+4
cf/cf-1860-A.jsonlAdded+4