Problem ID
stringlengths
2
6
Problem Description
stringlengths
0
7.52k
Rating
float64
800
3.5k
math
bool
2 classes
greedy
bool
2 classes
implementation
bool
2 classes
dp
bool
2 classes
data structures
bool
2 classes
constructive algorithms
bool
2 classes
brute force
bool
2 classes
binary search
bool
2 classes
sortings
bool
2 classes
graphs
bool
2 classes
__index_level_0__
int64
3
9.98k
6C
Problem - 6C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy two pointers *1200 No tag edit access → Contest materials ") . When the player consumes a chocolate bar, he immediately starts with another. It is not allowed to eat two chocolate bars at the same time, to leave the bar unfinished and to make pauses. If both players start to eat the same bar simultaneously, Bob leaves it to Alice as a true gentleman. How many bars each of the players will consume? Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the amount of bars on the table. The second line contains a sequence _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t__i_u2009≤u20091000), where _t__i_ is the time (in seconds) needed to consume the _i_-th bar (in the order from left to right). Output Print two numbers _a_ and _b_, where _a_ is the amount of bars consumed by Alice, and _b_ is the amount of bars consumed by Bob. Examples Input 5 2 9 8 2 7 Output 2 3
1,200
false
true
false
false
false
false
false
false
false
false
9,963
1398G
A running competition is going to be held soon. The stadium where the competition will be held can be represented by several segments on the coordinate plane: two horizontal segments: one connecting the points $$$(0, 0)$$$ and $$$(x, 0)$$$, the other connecting the points $$$(0, y)$$$ and $$$(x, y)$$$; $$$n + 1$$$ vertical segments, numbered from $$$0$$$ to $$$n$$$. The $$$i$$$-th segment connects the points $$$(a_i, 0)$$$ and $$$(a_i, y)$$$; $$$0 = a_0 < a_1 < a_2 < dots < a_{n - 1} < a_n = x$$$. For example, here is a picture of the stadium with $$$x = 10$$$, $$$y = 5$$$, $$$n = 3$$$ and $$$a = [0, 3, 5, 10]$$$: A lap is a route that goes along the segments, starts and finishes at the same point, and never intersects itself (the only two points of a lap that coincide are its starting point and ending point). The length of a lap is a total distance travelled around it. For example, the red route in the picture representing the stadium is a lap of length $$$24$$$. The competition will be held in $$$q$$$ stages. The $$$i$$$-th stage has length $$$l_i$$$, and the organizers want to choose a lap for each stage such that the length of the lap is a divisor of $$$l_i$$$. The organizers don't want to choose short laps for the stages, so for each stage, they want to find the maximum possible length of a suitable lap. Help the organizers to calculate the maximum possible lengths of the laps for the stages! In other words, for every $$$l_i$$$, find the maximum possible integer $$$L$$$ such that $$$l_i bmod L = 0$$$, and there exists a lap of length exactly $$$L$$$. If it is impossible to choose such a lap then print $$$-1$$$. Input The first line contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$1 le n, x, y le 2 cdot 10^5$$$, $$$n le x$$$). The second line contains $$$n + 1$$$ integers $$$a_0$$$, $$$a_1$$$, ..., $$$a_n$$$ ($$$0 = a_0 < a_1 < a_2 < dots < a_{n - 1} < a_n = x$$$). The third line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0— the number of stages. The fourth line contains $$$q$$$ even integers $$$l_1$$$, $$$l_2$$$, ..., $$$l_q$$$ ($$$4 le l_i le 10^6$$$)xa0— the lengths of the stages.
2,600
true
false
false
false
false
false
false
false
false
false
3,680
1887C
Given an array $$$a$$$ of length $$$n$$$ consisting of integers. Then the following operation is sequentially applied to it $$$q$$$ times: Choose indices $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$) and an integer $$$x$$$; Add $$$x$$$ to all elements of the array $$$a$$$ in the segment $$$[l, r]$$$. More formally, assign $$$a_i := a_i + x$$$ for all $$$l le i le r$$$. Let $$$b_j$$$ be the array $$$a$$$ obtained after applying the first $$$j$$$ operations ($$$0 le j le q$$$). Note that $$$b_0$$$ is the array $$$a$$$ before applying any operations. You need to find the lexicographically minimum$$$^{dagger}$$$ array among all arrays $$$b_j$$$. $$$^{dagger}$$$An array $$$x$$$ is lexicographically smaller than array $$$y$$$ if there is an index $$$i$$$ such that $$$x_i < y_i$$$, and $$$x_j = y_j$$$ for all $$$j < i$$$. In other words, for the first index $$$i$$$ where the arrays differ, $$$x_i < y_i$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 5 cdot 10^5$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$) — the elements of array $$$a$$$. The third line of each test case contains a single integer $$$q$$$ ($$$0 le q le 5 cdot 10^5$$$) — the number of operations on the array. In each of the next $$$q$$$ lines, there are three integers $$$l_j$$$, $$$r_j$$$, and $$$x_j$$$ $$$(1 le l_j le r_j le n, -10^9 le x_j le 10^9)$$$ — the description of each operation. The operations are given in the order they are applied. It is guaranteed that the sum of $$$n$$$ over all test cases and the sum of $$$q$$$ over all test cases do not exceed $$$5 cdot 10^5$$$. Output For each test case, output the lexicographically minimum array among all arrays $$$b_j$$$. Example Input 2 4 1 2 3 4 2 1 4 0 1 3 -100 5 2 1 2 5 4 3 2 4 3 2 5 -2 1 3 1 Output -99 -98 -97 4 2 1 2 5 4 Note In the first test case: $$$b_0 = [1,2,3,4]$$$; $$$b_1 = [1,2,3,4]$$$; $$$b_2 = [-99,-98,-97,4]$$$. Thus, the lexicographically minimum array is $$$b_2$$$. In the second test case, the lexicographically minimum array is $$$b_0$$$.
2,400
false
true
false
false
true
true
true
true
false
false
970
2005C
Narek is too lazy to create the third problem of this contest. His friend Artur suggests that he should use ChatGPT. ChatGPT creates $$$n$$$ problems, each consisting of $$$m$$$ letters, so Narek has $$$n$$$ strings. To make the problem harder, he combines the problems by selecting some of the $$$n$$$ strings possibly none and concatenating them without altering their order. His chance of solving the problem is defined as $$$score_n - score_c$$$, where $$$score_n$$$ is Narek's score and $$$score_c$$$ is ChatGPT's score. Narek calculates $$$score_n$$$ by examining the selected string (he moves from left to right). He initially searches for the letter $$$ exttt{"n"}$$$, followed by $$$ exttt{"a"}$$$, $$$ exttt{"r"}$$$, $$$ exttt{"e"}$$$, and $$$ exttt{"k"}$$$. Upon finding all occurrences of these letters, he increments $$$score_n$$$ by $$$5$$$ and resumes searching for $$$ exttt{"n"}$$$ again (he doesn't go back, and he just continues from where he left off). After Narek finishes, ChatGPT scans through the array and increments $$$score_c$$$ by $$$1$$$ for each letter $$$ exttt{"n"}$$$, $$$ exttt{"a"}$$$, $$$ exttt{"r"}$$$, $$$ exttt{"e"}$$$, or $$$ exttt{"k"}$$$ that Narek fails to utilize (note that if Narek fails to complete the last occurrence by finding all of the $$$5$$$ letters, then all of the letters he used are counted in ChatGPT's score $$$score_c$$$, and Narek doesn't get any points if he doesn't finish finding all the 5 letters). Narek aims to maximize the value of $$$score_n - score_c$$$ by selecting the most optimal subset of the initial strings. Input In the first line of the input, you're given a single integer $$$t$$$ ($$$1 le t le 10^5$$$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you're given two integers $$$n, m$$$ ($$$1 le n, m le 10^3$$$), the number of strings and the length of each string. In the next $$$n$$$ lines, you're given $$$n$$$ strings, each having a length of $$$m$$$. The strings only contain lowercase letters of the English alphabet. The sum of values of $$$n cdot m$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output a single integer: the maximal possible value of $$$score_n - score_c$$$. Example Input 4 5 2 nn aa rr ee kk 1 5 narek 1 4 nare 5 7 nrrarek nrnekan uuuuuuu ppppppp nkarekz Note In the first test case, one of the optimal answers is when Narek doesn't choose any of the strings, so the answer is $$$0$$$. He can alternatively choose all the strings. In this case, the full string becomes "nnaarreekk". Narek can choose the first appearances of all letters and add $$$5$$$ to the score. His opponent will add $$$1$$$ for all second appearances, which will be $$$5$$$ in total. So the answer will be $$$5 - 5 = 0$$$. In the third test case, the only optimal answer is when Narek doesn't choose the string. Note that if he were to choose the string, he wouldn't be able to find the last letter "k", so his score would stay at $$$0$$$ instead of becoming $$$5$$$. Then ChatGPT would add $$$4$$$ for all of the $$$4$$$ letters, and the answer would become $$$0 - 4 = -4$$$. In the last test case, Narek needs to choose the first and the last strings. After putting these two next to each other, he gets "$$${color{red}{n}}rr{color{red}{a}}{color{red}{r}}{color{red}{e}}{color{red}{k}}{color{red}{n}}k{color{red}{a}}{color{red}{r}}{color{red}{e}}{color{red}{k}}{color{blue}{z}}$$$". Narek can choose the letters marked with red and add $$$10$$$ to his score. Since the black colored letters Narek left behind are eligible for the opponent to claim (they are used in the word "narek"), the opponent adds all other letters to the score and gets a score of $$$3$$$. Therefore, the answer is $$$10 - 3 = 7$$$.
1,800
false
false
true
true
false
false
false
false
false
false
221
242D
Valera has _n_ counters numbered from 1 to _n_. Some of them are connected by wires, and each of the counters has a special button. Initially, all the counters contain number 0. When you press a button on a certain counter, the value it has increases by one. Also, the values recorded in all the counters, directly connected to it by a wire, increase by one. Valera and Ignat started having a dispute, the dispute is as follows. Ignat thought of a sequence of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Valera should choose some set of distinct counters and press buttons on each of them exactly once (on other counters the buttons won't be pressed). If after that there is a counter with the number _i_, which has value _a__i_, then Valera loses the dispute, otherwise he wins the dispute. Help Valera to determine on which counters he needs to press a button to win the dispute. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105), that denote the number of counters Valera has and the number of pairs of counters connected by wires. Each of the following _m_ lines contains two space-separated integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_,u2009_u__i_u2009≠u2009_v__i_), that mean that counters with numbers _u__i_ and _v__i_ are connected by a wire. It is guaranteed that each pair of connected counters occurs exactly once in the input. The last line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009105), where _a__i_ is the value that Ignat choose for the _i_-th counter. Output If Valera can't win the dispute print in the first line -1. Otherwise, print in the first line integer _k_ (0u2009≤u2009_k_u2009≤u2009_n_). In the second line print _k_ distinct space-separated integers — the numbers of the counters, where Valera should push buttons to win the dispute, in arbitrary order. If there exists multiple answers, you are allowed to print any of them. Examples Input 5 5 2 3 4 1 1 5 5 3 2 1 1 1 2 0 2
2,100
false
true
false
false
false
false
false
false
false
true
8,865
1970D1
Professor Vector is preparing to teach her Arithmancy class. She needs to prepare $$$n$$$ distinct magic words for the class. Each magic word is a string consisting of characters X and O. A spell is a string created by concatenating two magic words together. The power of a spell is equal to the number of its different non-empty substrings. For example, the power of the spell XOXO is equal to 7, because it has 7 different substrings: X, O, XO, OX, XOX, OXO and XOXO. Each student will create their own spell by concatenating two magic words. Since the students are not very good at magic yet, they will choose each of the two words independently and uniformly at random from the $$$n$$$ words provided by Professor Vector. It is therefore also possible that the two words a student chooses are the same. Each student will then compute the power of their spell, and tell it to Professor Vector. In order to check their work, and of course to impress the students, Professor Vector needs to find out which two magic words and in which order were concatenated by each student. Your program needs to perform the role of Professor Vector: first, create $$$n$$$ distinct magic words, and then handle multiple requests where it is given the spell power and needs to determine the indices of the two magic words, in the correct order, that were used to create the corresponding spell. Interaction This is an interactive problem. First, your program should read a single integer $$$n$$$ ($$$1 le n le 3$$$), the number of magic words to prepare. Then, it should print $$$n$$$ magic words it has created, one per line. The magic words must be distinct, each magic word must have at least 1 and at most $$$30cdot n$$$ characters, and each character must be either X or O. We will denote the $$$i$$$-th magic word you printed as $$$w_i$$$ ($$$1 le i le n$$$). Then, your program should read a single integer $$$q$$$ ($$$1 le q le 1000$$$), the number of students in the class. Then, it should repeat the following process $$$q$$$ times, one per student. For the $$$j$$$-th student, it should first read a single integer $$$p_j$$$, the power of their spell. It is guaranteed that this number is computed by choosing two indices $$$u_j$$$ and $$$v_j$$$ independently and uniformly at random between 1 and $$$n$$$ inclusive, concatenating $$$w_{u_j}$$$ and $$$w_{v_j}$$$, and finding the number of different non-empty substrings of the resulting string. Then, your program must print the numbers $$$u_j$$$ and $$$v_j$$$, in this order ($$$1 le u_j, v_j le n$$$). Note that it is not enough to find any two magic words that concatenate into a spell with the given power. You must find the exact words used by the student in the exact order. Remember to flush the output stream after printing all magic words and after printing $$$u_j$$$ and $$$v_j$$$ for each student.
2,100
false
false
false
false
false
true
true
false
false
false
477
1685D2
This is a hard version of the problem. The difference between the easy and hard versions is that in this version, you have to output the lexicographically smallest permutation with the smallest weight. You are given a permutation $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$. Let's define the weight of the permutation $$$q_1, q_2, ldots, q_n$$$ of integers from $$$1$$$ to $$$n$$$ as $$$$$$q_1 - p_{q_{2}} + q_2 - p_{q_{3}} + ldots + q_{n-1} - p_{q_{n}} + q_n - p_{q_{1}}$$$$$$ You want your permutation to be as lightweight as possible. Among the permutations $$$q$$$ with the smallest possible weight, find the lexicographically smallest. Permutation $$$a_1, a_2, ldots, a_n$$$ is lexicographically smaller than permutation $$$b_1, b_2, ldots, b_n$$$, if there exists some $$$1 le i le n$$$ such that $$$a_j = b_j$$$ for all $$$1 le j < i$$$ and $$$a_i<b_i$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 200$$$) xa0— the size of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ are distinct) xa0— the elements of the permutation. The sum of $$$n$$$ over all test cases doesn't exceed $$$400$$$. Output For each test case, output $$$n$$$ integers $$$q_1, q_2, ldots, q_n$$$ ($$$1 le q_i le n$$$, all $$$q_i$$$ are distinct) xa0— the lexicographically smallest permutation with the smallest weight. Example Input 3 2 2 1 4 2 3 1 4 5 5 4 3 2 1 Output 1 2 1 3 4 2 1 3 4 2 5 Note In the first test case, there are two permutations of length $$$2$$$: $$$(1, 2)$$$ and $$$(2, 1)$$$. Permutation $$$(1, 2)$$$ has weight $$$1 - p_2 + 2 - p_1 = 0$$$, and the permutation $$$(2, 1)$$$ has the same weight: $$$2 - p_1 + 1 - p_2 = 0$$$. In this version, you have to output the lexicographically smaller of them xa0— $$$(1, 2)$$$. In the second test case, the weight of the permutation $$$(1, 3, 4, 2)$$$ is $$$1 - p_3 + 3 - p_4 + 4 - p_2 + 2 - p_1 = 1 - 1 + 3 - 4 + 4 - 3 + 2 - 2 = 2$$$. There are no permutations with smaller weights. In the third test case, the weight of the permutation $$$(1, 3, 4, 2, 5)$$$ is $$$1 - p_3 + 3 - p_4 + 4 - p_2 + 2 - p_5 + 5 - p_1 = 1 - 3 + 3 - 2 + 4 - 4 + 2 - 1 + 5 - 5 = 4$$$. There are no permutations with smaller weights.
3,500
false
true
false
false
false
true
false
false
false
false
2,161
1612E
Monocarp is a tutor of a group of $$$n$$$ students. He communicates with them using a conference in a popular messenger. Today was a busy day for Monocarp — he was asked to forward a lot of posts and announcements to his group, that's why he had to write a very large number of messages in the conference. Monocarp knows the students in the group he is tutoring quite well, so he understands which message should each student read: Monocarp wants the student $$$i$$$ to read the message $$$m_i$$$. Of course, no one's going to read all the messages in the conference. That's why Monocarp decided to pin some of them. Monocarp can pin any number of messages, and if he wants anyone to read some message, he should pin it — otherwise it will definitely be skipped by everyone. Unfortunately, even if a message is pinned, some students may skip it anyway. For each student $$$i$$$, Monocarp knows that they will read at most $$$k_i$$$ messages. Suppose Monocarp pins $$$t$$$ messages; if $$$t le k_i$$$, then the $$$i$$$-th student will read all the pinned messages; but if $$$t > k_i$$$, the $$$i$$$-th student will choose exactly $$$k_i$$$ random pinned messages (all possible subsets of pinned messages of size $$$k_i$$$ are equiprobable) and read only the chosen messages. Monocarp wants to maximize the expected number of students that read their respective messages (i.e. the number of such indices $$$i$$$ that student $$$i$$$ reads the message $$$m_i$$$). Help him to choose how many (and which) messages should he pin! Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of students in the conference. Then $$$n$$$ lines follow. The $$$i$$$-th line contains two integers $$$m_i$$$ and $$$k_i$$$ ($$$1 le m_i le 2 cdot 10^5$$$; $$$1 le k_i le 20$$$) — the index of the message which Monocarp wants the $$$i$$$-th student to read and the maximum number of messages the $$$i$$$-th student will read, respectively. Output In the first line, print one integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$) — the number of messages Monocarp should pin. In the second line, print $$$t$$$ distinct integers $$$c_1$$$, $$$c_2$$$, ..., $$$c_t$$$ ($$$1 le c_i le 2 cdot 10^5$$$) — the indices of the messages Monocarp should pin. The messages can be listed in any order. If there are multiple answers, print any of them.
2,000
false
true
false
true
false
false
true
false
true
false
2,579
1999G1
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 964 (Div. 4) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search interactive *1500 No tag edit access → Contest materials Announcement (en) Discussion stream (en) Video Tutorial (en) Tutorial #2 (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST G1. Ruler (easy version) time limit per test1 second memory limit per test256 megabytes This is the easy version of the problem. The only difference between the two versions is that in this version, you can make at most $$$mathbf{10}$$$ queries. This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read the guide for participants. We have a secret ruler that is missing one number $$$x$$$ ($$$2 leq x leq 999$$$). When you measure an object of length $$$y$$$, the ruler reports the following values: If $$$y < x$$$, the ruler (correctly) measures the object as having length $$$y$$$. If $$$y geq x$$$, the ruler incorrectly measures the object as having length $$$y+1$$$. The ruler above is missing the number $$$4$$$, so it correctly measures the first segment as length $$$3$$$ but incorrectly measures the second segment as length $$$6$$$ even though it is actually $$$5$$$. You need to find the value of $$$x$$$. To do that, you can make queries of the following form: $$$ exttt{?}~a~b$$$xa0— in response, we will measure the side lengths of an $$$a imes b$$$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $$$x=4$$$ and you query a $$$3 imes 5$$$ rectangle, we will measure its side lengths as $$$3 imes 6$$$ and report $$$18$$$ back to you. Find the value of $$$x$$$. You can ask at most $$$mathbf{10}$$$ queries. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. Interaction There is no initial input for each test case. You should begin the interaction by asking a query. To make a query, output a single line of the form $$$ exttt{?}~a~b$$$ ($$$1 leq a, b leq 1000$$$). In response, you will be told the measured area of the rectangle, according to our secret ruler. When you are ready to print the answer, output a single line of the form $$$ exttt{!}~x$$$ ($$$2 leq x leq 999$$$). After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query. The interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn't depend on the queries asked by the participant. If your program makes more than $$$10$$$ queries for one set of input data, makes an invalid query, or prints the wrong value of $$$x$$$, then the response to the query will be $$$-1$$$. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get Idleness limit exceeded verdict. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hacks To make a hack, use the following format. The first line should contain a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The only line of each test case should contain a single integer $$$x$$$ ($$$2 leq x leq 999$$$)xa0— the missing number on the ruler. Example input 2 18 25 9999 output ? 3 5 ? 4 4 ! 4 ? 99 100 ! 100 Note In the first test, the interaction proceeds as follows. Solution Jury Explanation $$$ exttt{2}$$$ There are 2 test cases. $$$ exttt{? 3 5}$$$ $$$ exttt{18}$$$ Secretly, the jury picked $$$x=4$$$. The solution requests the $$$3 imes 5$$$ rectangle, and the jury responds with $$$3 imes 6 = 18$$$, as described in the statement. $$$ exttt{? 4 4}$$$ $$$ exttt{25}$$$ The solution requests the $$$4 imes 4$$$ rectangle, which the jury measures as $$$5 imes 5$$$ and responds with $$$25$$$. $$$ exttt{! 4}$$$ The solution has somehow determined that $$$x=4$$$, and outputs it. Since the output is correct, the jury continues to the next test case. $$$ exttt{? 99 100}$$$ $$$ exttt{1}$$$ Secretly, the jury picked $$$x=100$$$. The solution requests the $$$99 imes 100$$$ rectangle, which the jury measures as $$$99 imes 101$$$ and responds with $$$9999$$$. $$$ exttt{! 100}$$$ The solution has somehow determined that $$$x=100$$$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction. Codeforces (c)
1,500
false
false
false
false
false
false
false
true
false
false
265
177E1
The Smart Beaver from ABBYY plans a space travel on an ultramodern spaceship. During the voyage he plans to visit _n_ planets. For planet _i_ _a__i_ is the maximum number of suitcases that an alien tourist is allowed to bring to the planet, and _b__i_ is the number of citizens on the planet. The Smart Beaver is going to bring some presents from ABBYY to the planets he will be visiting. The presents are packed in suitcases, _x_ presents in each. The Beaver will take to the ship exactly _a_1u2009+u2009...u2009+u2009_a__n_ suitcases. As the Beaver lands on the _i_-th planet, he takes _a__i_ suitcases and goes out. On the first day on the planet the Beaver takes a walk and gets to know the citizens. On the second and all subsequent days the Beaver gives presents to the citizens — each of the _b__i_ citizens gets one present per day. The Beaver leaves the planet in the evening of the day when the number of presents left is strictly less than the number of citizens (i.e. as soon as he won't be able to give away the proper number of presents the next day). He leaves the remaining presents at the hotel. The Beaver is going to spend exactly _c_ days traveling. The time spent on flights between the planets is considered to be zero. In how many ways can one choose the positive integer _x_ so that the planned voyage will take exactly _c_ days? Input The first input line contains space-separated integers _n_ and _c_ — the number of planets that the Beaver is going to visit and the number of days he is going to spend traveling, correspondingly. The next _n_ lines contain pairs of space-separated integers _a__i_,u2009_b__i_ (1u2009≤u2009_i_u2009≤u2009_n_) — the number of suitcases he can bring to the _i_-th planet and the number of citizens of the _i_-th planet, correspondingly. The input limitations for getting 30 points are: 1u2009≤u2009_n_u2009≤u2009100 1u2009≤u2009_a__i_u2009≤u2009100 1u2009≤u2009_b__i_u2009≤u2009100 1u2009≤u2009_c_u2009≤u2009100 The input limitations for getting 100 points are: 1u2009≤u2009_n_u2009≤u2009104 0u2009≤u2009_a__i_u2009≤u2009109 1u2009≤u2009_b__i_u2009≤u2009109 1u2009≤u2009_c_u2009≤u2009109 Due to possible overflow, it is recommended to use the 64-bit arithmetic. In some solutions even the 64-bit arithmetic can overflow. So be careful in calculations! Output Print a single number _k_ — the number of ways to choose _x_ so as to travel for exactly _c_ days. If there are infinitely many possible values of _x_, print -1. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. Note In the first example there is only one suitable value _x_u2009=u20095. Then the Beaver takes 1 suitcase with 5 presents to the first planet. Here he spends 2 days: he hangs around on the first day, and he gives away five presents on the second day. He takes 2 suitcases with 10 presents to the second planet. Here he spends 3 days — he gives away 4 presents on the second and the third days and leaves the remaining 2 presents at the hotel. In total, the Beaver spends 5 days traveling. For _x_u2009=u20094 or less the Beaver won't have enough presents for the second day on the first planet, so the voyage will end too soon. For _x_u2009=u20096 and more the Beaver will spend at least one more day on the second planet, and the voyage will take too long.
1,700
false
false
false
false
false
false
false
true
false
false
9,146
952C
Everybody knows of . Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number _n_ (1u2009≤u2009_n_u2009≤u200910) — the size of the array. The second line of input contains _n_ space-separated integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2,u20092,u20092}.
1,600
false
false
true
false
false
false
false
false
false
false
5,914
1849F
Problem - 1849F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search bitmasks data structures divide and conquer greedy trees *2700 No tag edit access → Contest materials . If there are less than two elements in the set, its cost is equal to $$$2^{30}$$$. You 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$$$. Find the partition with the maximum possible value. Input The first line contains $$$n$$$ ($$$2 le n le 200000$$$) — the number of elements in the set. The second line contains $$$n$$$ distinct integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 le a_i < 2^{30}$$$) — the elements of the set. Output 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. If there are multiple optimal answers, print any of them. Examples Input 5 42 13 1337 37 152 Output 10001 Input 4 1 2 3 4 Output 1100 Input 2 1 2 Output 10 Input 8 7 6 5 4 3 2 1 0 Output 10010110
2,700
false
true
false
false
true
false
false
true
false
false
1,183
248A
One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house. Karlsson's gaze immediately fell on _n_ wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find. And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open. Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds _t_, in which he is able to bring all the cupboard doors in the required position. Your task is to write a program that will determine the required number of seconds _t_. Input The first input line contains a single integer _n_ — the number of cupboards in the kitchen (2u2009≤u2009_n_u2009≤u2009104). Then follow _n_ lines, each containing two integers _l__i_ and _r__i_ (0u2009≤u2009_l__i_,u2009_r__i_u2009≤u20091). Number _l__i_ equals one, if the left door of the _i_-th cupboard is opened, otherwise number _l__i_ equals zero. Similarly, number _r__i_ equals one, if the right door of the _i_-th cupboard is opened, otherwise number _r__i_ equals zero. The numbers in the lines are separated by single spaces.
800
false
false
true
false
false
false
false
false
false
false
8,843
814E
Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be faced with if someone gets lost. There are _n_ towns in the region, numbered from 1 to _n_. The town numbered 1 is called the capital. The traffic network is formed by bidirectional roads connecting pairs of towns. No two roads connect the same pair of towns, and no road connects a town with itself. The time needed to travel through each of the roads is the same. Lost travelers will not be able to find out how the towns are connected, but the residents can help them by providing the following facts: Starting from each town other than the capital, the shortest path (i.e. the path passing through the minimum number of roads) to the capital exists, and is unique; Let _l__i_ be the number of roads on the shortest path from town _i_ to the capital, then _l__i_u2009≥u2009_l__i_u2009-u20091 holds for all 2u2009≤u2009_i_u2009≤u2009_n_; For town _i_, the number of roads connected to it is denoted by _d__i_, which equals either 2 or 3. You are to count the number of different ways in which the towns are connected, and give the answer modulo 109u2009+u20097. Two ways of connecting towns are considered different if a pair (_u_,u2009_v_) (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_) exists such there is a road between towns _u_ and _v_ in one of them but not in the other. Input The first line of input contains a positive integer _n_ (3u2009≤u2009_n_u2009≤u200950) — the number of towns. The second line contains _n_ space-separated integers _d_1,u2009_d_2,u2009...,u2009_d__n_ (2u2009≤u2009_d__i_u2009≤u20093) — the number of roads connected to towns 1,u20092,u2009...,u2009_n_, respectively. It is guaranteed that the sum of _d__i_ over all _i_ is even. Output Output one integer — the total number of different possible ways in which the towns are connected, modulo 109u2009+u20097. Examples Input 20 2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2 Note In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2,u20093,u20094 to the capital are all 1. In the second example, the following two structures satisfy the constraints.
2,600
false
false
false
true
false
false
false
false
false
true
6,501
1744B
You are given $$$n$$$ of integers $$$a_1, a_2, ldots, a_n$$$. Process $$$q$$$ queries of two types: query of the form "0 $$$x_j$$$": add the value $$$x_j$$$ to all even elements of the array $$$a$$$, query of the form "1 $$$x_j$$$": add the value $$$x_j$$$ to all odd elements of the array $$$a$$$. Note that when processing the query, we look specifically at the odd/even value of $$$a_i$$$, not its index. After processing each query, print the sum of the elements of the array $$$a$$$. Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). Input The first line of the input contains an integer $$$t$$$ $$$(1 leq t leq 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 leq n$$$, $$$q leq 10^5$$$) — the length of array $$$a$$$ and the number of queries. The second line of each test case contains exactly $$$n$$$ integers: $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) — elements of the array $$$a$$$. The following $$$q$$$ lines contain queries as two integers $$$type_j$$$ and $$$x_j$$$ $$$(0 leq type_j leq 1$$$, $$$1 leq x_j leq 10^4$$$). It is guaranteed that the sum of values $$$n$$$ over all test cases in a test does not exceed $$$10^5$$$. Similarly, the sum of values $$$q$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print $$$q$$$ numbers: the sum of the elements of the array $$$a$$$ after processing a query. Example Input 4 1 1 1 1 1 3 3 1 2 4 0 2 1 3 0 5 6 7 1 3 2 4 10 48 1 6 0 5 0 4 0 5 1 3 0 12 0 1 6 7 1000000000 1000000000 1000000000 11 15 17 0 17 1 10000 1 51 0 92 0 53 1 16 0 1 Output 2 11 14 29 80 100 100 100 118 190 196 3000000094 3000060094 3000060400 3000060952 3000061270 3000061366 3000061366 Note In the first test case, the array $$$a = [2]$$$ after the first query. In the third test case, the array $$$a$$$ is modified as follows: $$$[1, 3, 2, 4, 10, 48]$$$ $$$ ightarrow$$$ $$$[7, 9, 2, 4, 10, 48]$$$ $$$ ightarrow$$$ $$$[7, 9, 7, 9, 15, 53]$$$ $$$ ightarrow$$$ $$$[7, 9, 7, 9, 15, 53]$$$ $$$ ightarrow$$$ $$$[10, 12, 10, 12, 18, 56]$$$ $$$ ightarrow$$$ $$$[22, 24, 22, 24, 30, 68]$$$ $$$ ightarrow$$$ $$$[23, 25, 23, 25, 31, 69]$$$.
800
true
false
true
false
false
false
false
false
false
false
1,816
1303D
You have a bag of size $$$n$$$. Also you have $$$m$$$ boxes. The size of $$$i$$$-th box is $$$a_i$$$, where each $$$a_i$$$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $$$n = 10$$$ and $$$a = [1, 1, 32]$$$ then you have to divide the box of size $$$32$$$ into two parts of size $$$16$$$, and then divide the box of size $$$16$$$. So you can fill the bag with boxes of size $$$1$$$, $$$1$$$ and $$$8$$$. Calculate the minimum number of divisions required to fill the bag of size $$$n$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^{18}, 1 le m le 10^5$$$) — the size of bag and the number of boxes, respectively. The second line of each test case contains $$$m$$$ integers $$$a_1, a_2, dots , a_m$$$ ($$$1 le a_i le 10^9$$$) — the sizes of boxes. It is guaranteed that each $$$a_i$$$ is a power of two. It is also guaranteed that sum of all $$$m$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print one integer — the minimum number of divisions required to fill the bag of size $$$n$$$ (or $$$-1$$$, if it is impossible). Example Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8
1,900
false
true
false
false
false
false
false
false
false
false
4,175
264C
There are _n_ balls. They are arranged in a row. Each ball has a color (for convenience an integer) and an integer value. The color of the _i_-th ball is _c__i_ and the value of the _i_-th ball is _v__i_. Squirrel Liss chooses some balls and makes a new sequence without changing the relative order of the balls. She wants to maximize the value of this sequence. The value of the sequence is defined as the sum of following values for each ball (where _a_ and _b_ are given constants): If the ball is not in the beginning of the sequence and the color of the ball is same as previous ball's color, add (the value of the ball) u2009×u2009 _a_. Otherwise, add (the value of the ball) u2009×u2009 _b_. You are given _q_ queries. Each query contains two integers _a__i_ and _b__i_. For each query find the maximal value of the sequence she can make when _a_u2009=u2009_a__i_ and _b_u2009=u2009_b__i_. Note that the new sequence can be empty, and the value of an empty sequence is defined as zero. Input The first line contains two integers _n_ and _q_ (1u2009≤u2009_n_u2009≤u2009105;xa01u2009≤u2009_q_u2009≤u2009500). The second line contains _n_ integers: _v_1,u2009_v_2,u2009...,u2009_v__n_ (_v__i_u2009≤u2009105). The third line contains _n_ integers: _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c__i_u2009≤u2009_n_). The following _q_ lines contain the values of the constants _a_ and _b_ for queries. The _i_-th of these lines contains two integers _a__i_ and _b__i_ (_a__i_,u2009_b__i_u2009≤u2009105). In each line integers are separated by single spaces. Output For each query, output a line containing an integer — the answer to the query. The _i_-th line contains the answer to the _i_-th query in the input order. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 6 3 1 -2 3 4 0 -1 1 2 1 2 1 1 5 1 -2 1 1 0 Input 4 1 -3 6 -1 2 1 2 3 1 1 -1 Note In the first example, to achieve the maximal value: In the first query, you should select 1st, 3rd, and 4th ball. In the second query, you should select 3rd, 4th, 5th and 6th ball. In the third query, you should select 2nd and 4th ball. Note that there may be other ways to achieve the maximal value.
2,000
false
false
false
true
false
false
false
false
false
false
8,776
1919E
There is a hidden array $$$a$$$ of size $$$n$$$ consisting of only $$$1$$$ and $$$-1$$$. Let $$$p$$$ be the prefix sums of array $$$a$$$. More formally, $$$p$$$ is an array of length $$$n$$$ defined as $$$p_i = a_1 + a_2 + ldots + a_i$$$. Afterwards, array $$$p$$$ is sorted in non-decreasing order. For example, if $$$a = [1, -1, -1, 1, 1]$$$, then $$$p = [1, 0, -1, 0, 1]$$$ before sorting and $$$p = [-1, 0, 0, 1, 1]$$$ after sorting. You are given the prefix sum array $$$p$$$ after sorting, but you do not know what array $$$a$$$ is. Your task is to count the number of initial arrays $$$a$$$ such that the above process results in the given sorted prefix sum array $$$p$$$. As this number can be large, you are only required to find it modulo $$$998,244,353$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5000$$$)xa0— the size of the hidden array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$p_i le n$$$)xa0— the $$$n$$$ prefix sums of $$$a$$$ sorted in non-decreasing order. It is guaranteed that $$$p_1 le p_2 le ldots le p_n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output For each test case, output the answer modulo $$$998,244,353$$$. Example Input 5 1 0 1 1 3 -1 1 2 5 -1 0 0 1 1 5 -4 -3 -3 -2 -1 Note In the first two test cases, the only possible arrays $$$a$$$ for $$$n = 1$$$ are $$$a = [1]$$$ and $$$a = [-1]$$$. Their respective sorted prefix sum arrays $$$p$$$ are $$$p = [1]$$$ and $$$p = [-1]$$$. Hence, there is no array $$$a$$$ that can result in the sorted prefix sum array $$$p = [0]$$$ and there is exactly $$$1$$$ array $$$a$$$ that can result in the sorted prefix sum array $$$p = [1]$$$. In the third test case, it can be proven that there is no array $$$a$$$ that could result in the sorted prefix sum array $$$p = [-1, 1, 2]$$$. In the fourth test case, the $$$3$$$ possible arrays $$$a$$$ that could result in the sorted prefix sum array $$$p = [-1, 0, 0, 1, 1]$$$ are: $$$a = [1, -1, 1, -1, -1]$$$. The prefix sum array before sorting is $$$p = [1, 0, 1, 0, -1]$$$, which after sorting gives $$$p = [-1, 0, 0, 1, 1]$$$. $$$a = [1, -1, -1, 1, 1]$$$. The prefix sum array before sorting is $$$p = [1, 0, -1, 0, 1]$$$, which after sorting gives $$$p = [-1, 0, 0, 1, 1]$$$. $$$a = [-1, 1, 1, -1, 1]$$$. The prefix sum array before sorting is $$$p = [-1, 0, 1, 0, 1]$$$, which after sorting gives $$$p = [-1, 0, 0, 1, 1]$$$. For the fifth test case, the only possible array $$$a$$$ that could result in the sorted prefix sum array $$$p = [-4, -3, -3, -2, -1]$$$ is $$$a = [-1, -1, -1, -1, 1]$$$.
2,600
true
false
true
true
false
true
false
false
false
false
777
1949E
You just created a new character in your favourite role-playing game and now have to decide how to skill him. The two skill attributes to be chosen are: damage per hit and hits per second. Damage per hit is the amount of damage you deal with a single hit, while hits per second is the number of hits you can make in one second. Initially, both skill attributes are set at $$$0$$$. You have $$$k$$$ skill points to distribute as you want; in other words, you can choose the values of the two skills so that they are positive integers with sum at most $$$k$$$. The tutorial of the game (the boring part you want to finish as soon as possible) consists of $$$n$$$ monsters to be killed one after the other. The $$$i$$$-th monster has $$$h_i$$$ health points, i.e., it dies after you have inflicted at least $$$h_i$$$ damage. How can you assign the two skill attributes to minimize the time necessary to kill all the $$$n$$$ monsters? Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1leq nleq200,000$$$, $$$2leq kleq200,000$$$) — the number of enemies and the number of skill points. The second line contains $$$n$$$ integers $$$h_i$$$ ($$$1leq h_ileq10^{13}$$$) — the health of the $$$i$$$th enemy. Output Print two positive integers $$$x$$$ and $$$y$$$ ($$$1le x, y$$$ and $$$x+yle k$$$)xa0— the number of skill points you want to invest in damage per hit and hits per second. If there are multiple optimal solutions, print any of them. Note In the first sample, there is only one monster and you have $$$7$$$ skill points to distribute. If you make $$$3$$$ damage per hit, you will need $$$5$$$ hits to kill it. If you do $$$4$$$ hits per second, you will need $$$1.25$$$ seconds to beat the monster. There is no way to beat the monster faster than this. In the second sample, you will need one hit for each monster and a total time of $$$0.8$$$ seconds if you distribute $$$4$$$ skill points on damage per hit and the remaining $$$5$$$ points on hits per second.
2,900
true
false
false
false
false
false
true
false
false
false
588
1791E
Problem - 1791E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp greedy sortings *1100 No tag edit access → Contest materials ") xa0— the number of test cases. The descriptions of the test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$2 leq n leq 2cdot10^5$$$)xa0— the length of the array. The following line contains $$$n$$$ space-separated integers $$$a_1,a_2,dots,a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, output the maximum possible sum the array can have after performing the described operation any number of times. Example Input 5 3 -1 -1 -1 5 1 5 -5 0 2 3 1 2 3 6 -1 10 9 8 7 6 2 -1 -1 Output 1 13 6 39 2 Note For the first test case, by performing the operation on the first two elements, we can change the array from $$$
1,100
false
true
false
true
false
false
false
false
true
false
1,531
1641C
In the work of a doctor, it is important to maintain the anonymity of clients and the results of tests. The test results are sent to everyone personally by email, but people are very impatient and they want to know the results right away. That's why in the testing lab "De-vitro" doctors came up with an experimental way to report the results. Let's assume that $$$n$$$ people took the tests in the order of the queue. Then the chief doctor Sam can make several statements, in each telling if there is a sick person among the people in the queue from $$$l$$$-th to $$$r$$$-th (inclusive), for some values $$$l$$$ and $$$r$$$. During the process, Sam will check how well this scheme works and will be interested in whether it is possible to find out the test result of $$$i$$$-th person from the information he announced. And if it can be done, then is that patient sick or not. Help Sam to test his scheme. Input The first line contains two integers $$$n$$$, $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$) — the number of people and the number of queries. In each of the next $$$q$$$ lines, the description of the query is given. The first number in the line is $$$t$$$ ($$$t = 0$$$ or $$$t = 1$$$) — the type of the query. If $$$t = 0$$$, the line contains three more integers $$$l, r, x$$$ ($$$1 le l le r le n$$$, $$$x = 0$$$ or $$$x = 1$$$). This query means that Sam tells that among the people in the queue from $$$l$$$-th to $$$r$$$-th (inclusive): there was at least one sick person, if $$$x=1$$$, there is no sick people, if $$$x=0$$$. If $$$t = 1$$$, the line contains one more integer $$$j$$$ ($$$1 le j le n$$$) — the position of the patient in the queue, for which Sam wants to know the status. All queries are correct, that means that there always exists an example of the queue of length $$$n$$$ for which all reported results (statements from queries with $$$t = 0$$$) are true. Output After each Sam question (query with $$$t = 1$$$) print: "NO", if the patient is definitely not sick, "YES", if the patient is definitely sick. "N/A", if it is impossible to definitely identify the status of patient having the given information. Example Input 6 9 0 4 5 0 1 5 1 6 0 4 6 1 1 6 0 2 5 1 0 2 2 0 1 3 1 2 Note In the first test for the five first queries: 1. Initially Sam tells that people $$$4$$$, $$$5$$$ are not sick. 2. In the next query Sam asks the status of the patient $$$5$$$. From the previous query, we know that the patient is definitely not sick. 3. In the next query Sam asks the status of the patient $$$6$$$. We don't know any information about that patient now. 4. After that Sam tells that there exists a sick patient among $$$4$$$, $$$5$$$, $$$6$$$. 5. In the next query Sam asks the status of the patient $$$6$$$. Now we can tell that this patient is definitely sick.
2,200
false
true
false
false
true
false
true
true
true
false
2,407
1468G
The hobbits Frodo and Sam are carrying the One Ring to Mordor. In order not to be spotted by orcs, they decided to go through the mountains. The mountain relief can be represented as a polyline with $$$n$$$ points $$$(x_i, y_i)$$$, numbered from $$$1$$$ to $$$n$$$ ($$$x_i < x_{i + 1}$$$ for $$$1 le i le n - 1$$$). Hobbits start their journey at the point $$$(x_1, y_1)$$$ and should reach the point $$$(x_n, y_n)$$$ to complete their mission. The problem is that there is a tower with the Eye of Sauron, which watches them. The tower is located at the point $$$(x_n, y_n)$$$ and has the height $$$H$$$, so the Eye is located at the point $$$(x_n, y_n + H)$$$. In order to complete the mission successfully, the hobbits have to wear cloaks all the time when the Sauron Eye can see them, i.u2009e. when there is a direct line from the Eye to the hobbits which is not intersected by the relief. The hobbits are low, so their height can be considered negligibly small, but still positive, so when a direct line from the Sauron Eye to the hobbits only touches the relief, the Eye can see them. The Sauron Eye can't see hobbits when they are in the left position, but can see them when they are in the right position. The hobbits do not like to wear cloaks, so they wear them only when they can be spotted by the Eye. Your task is to calculate the total distance the hobbits have to walk while wearing cloaks. Input The first line of the input contains two integers $$$n$$$ and $$$H$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le H le 10^4$$$)xa0— the number of vertices in polyline and the tower height. The next $$$n$$$ lines contain two integers $$$x_i, y_i$$$ each ($$$0 le x_i le 4 cdot 10^5$$$; $$$0 le y_i le 10^4$$$)xa0— the coordinates of the polyline vertices. It is guaranteed that $$$x_i < x_{i + 1}$$$ for $$$1 le i le n - 1$$$. Output Print one real number — the total distance the hobbits have to walk while wearing cloaks. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$ — formally, if your answer is $$$a$$$, and the jury's answer is $$$b$$$, your answer will be accepted if $$$dfrac{a - b}{max(1, b)} le 10^{-6}$$$.
2,500
false
false
false
false
false
false
false
true
false
false
3,346
578A
Problem - 578A - Codeforces =============== xa0 ]( "Codeforces Round #320 u2009–u2009(_x_,u2009_x_)u2009–u2009(2_x_,u20090)u2009–u2009(3_x_,u2009_x_)u2009–u2009(4_x_,u20090)u2009–u2009...u2009-u2009(2_kx_,u20090)u2009–u2009(2_kx_u2009+u2009_x_,u2009_x_)u2009–u2009.... We know that the polyline passes through the point (_a_,u2009_b_). Find minimum positive value _x_ such that it is true or determine that there is no such _x_. Input Only one line containing two positive integers _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009109). Output Output the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10u2009-u20099. If there is no such _x_ then output u2009-u20091 as the answer. Examples Input 3 1 Output 1.000000000000 Input 1 3 Output -1 Input 4 1 Output 1.250000000000 Note You can see following graphs for sample 1 and sample 3.
1,700
true
false
false
false
false
false
false
false
false
false
7,536
1685C
Alina has a bracket sequence $$$s$$$ of length $$$2n$$$, consisting of $$$n$$$ opening brackets '(' and $$$n$$$ closing brackets ')'. As she likes balance, she wants to turn this bracket sequence into a balanced bracket sequence. In one operation, she can reverse any substring of $$$s$$$. What's the smallest number of operations that she needs to turn $$$s$$$ into a balanced bracket sequence? It can be shown that it's always possible in at most $$$n$$$ operations. As a reminder, a sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters + and 1. For example, sequences (())(), (), and (()(())) are balanced, while )(, ((), and (()))( are not. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line of each test case contains a string $$$s$$$ of length $$$2n$$$, consisting of $$$n$$$ opening and $$$n$$$ closing brackets. The sum of $$$n$$$ over all test cases doesn't exceed $$$2cdot 10^5$$$. Output For each test case, in the first line output a single integer $$$k$$$ $$$(0 le k le n)$$$ xa0— the smallest number of operations required. The $$$i$$$-th of the next $$$k$$$ lines should contain two integers $$$l_i, r_i$$$ ($$$1 le l_i le r_i le 2n$$$), indicating that in the $$$i$$$-th operation, Alina will reverse the substring $$$s_ls_{l+1} ldots s_{r-1}s_r$$$. Here the numeration starts from $$$1$$$. If there are multiple sequences of operations with the smallest length which transform the sequence into a balanced one, you can output any of them. Example Input 3 2 (()) 5 ())((()))( 6 ())((()))(() Output 0 2 3 4 9 10 1 2 11 Note In the first test case, the string is already balanced. In the second test case, the string will be transformed as follows: ())((()))( $$$ o$$$ ()()(()))( $$$ o$$$ ()()(())(), where the last string is balanced. In the third test case, the string will be transformed to ((()))((())), which is balanced.
2,600
false
true
false
false
false
true
true
false
false
false
2,163
1707E
You are given an integer array $$$a_1,ldots, a_n$$$, where $$$1le a_i le n$$$ for all $$$i$$$. There's a "replace" function $$$f$$$ which takes a pair of integers $$$(l, r)$$$, where $$$l le r$$$, as input and outputs the pair $$$$$$fbig( (l, r) big)=left(min{a_l,a_{l+1},ldots,a_r},, max{a_l,a_{l+1},ldots,a_r} ight).$$$$$$ Consider repeated calls of this function. That is, from a starting pair $$$(l, r)$$$ we get $$$fbig((l, r)big)$$$, then $$$fbig(fbig((l, r)big)big)$$$, then $$$fbig(fbig(fbig((l, r)big)big)big)$$$, and so on. Now you need to answer $$$q$$$ queries. For the $$$i$$$-th query you have two integers $$$l_i$$$ and $$$r_i$$$ ($$$1le l_ile r_ile n$$$). You must answer the minimum number of times you must apply the "replace" function to the pair $$$(l_i,r_i)$$$ to get $$$(1, n)$$$, or report that it is impossible. Input The first line contains two positive integers $$$n$$$, $$$q$$$ ($$$1le n,qle 10^5$$$)xa0— the length of the sequence $$$a$$$ and the number of the queries. The second line contains $$$n$$$ positive integers $$$a_1,a_2,ldots,a_n$$$ ($$$1le a_ile n$$$)xa0— the sequence $$$a$$$. Each line of the following $$$q$$$ lines contains two integers $$$l_i$$$, $$$r_i$$$ ($$$1le l_ile r_ile n$$$)xa0— the queries. Output For each query, output the required number of times, or $$$-1$$$ if it is impossible. Examples Input 5 6 2 5 4 1 3 4 4 1 5 1 4 3 5 4 5 2 3 Input 6 3 2 3 4 6 1 2 5 6 2 5 2 3 Input 5 3 3 2 2 4 1 2 5 1 3 1 5 Note In the first example, $$$n=5$$$ and $$$a=[2,5,4,1,3]$$$. For the first query: $$$(4,4) o(1,1) o(2,2) o(5,5) o(3,3) o(4,4) oldots$$$, so it's impossible to get $$$(1,5)$$$. For the second query, you already have $$$(1,5)$$$. For the third query: $$$(1,4) o(1,5)$$$. For the fourth query: $$$(3,5) o(1,4) o(1,5)$$$. For the fifth query: $$$(4,5) o(1,3) o(2,5) o(1,5)$$$. For the sixth query: $$$(2,3) o(4,5) o(1,3) o(2,5) o(1,5)$$$.
3,500
false
false
false
false
true
false
false
true
false
false
2,030
1932C
You are given an array $$$a$$$ of length $$$n$$$, a positive integer $$$m$$$, and a string of commands of length $$$n$$$. Each command is either the character 'L' or the character 'R'. Process all $$$n$$$ commands in the order they are written in the string $$$s$$$. Processing a command is done as follows: First, output the remainder of the product of all elements of the array $$$a$$$ when divided by $$$m$$$. Then, if the command is 'L', remove the leftmost element from the array $$$a$$$, if the command is 'R', remove the rightmost element from the array $$$a$$$. Note that after each move, the length of the array $$$a$$$ decreases by $$$1$$$, and after processing all commands, it will be empty. Write a program that will process all commands in the order they are written in the string $$$s$$$ (from left to right). Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. Then descriptions of $$$t$$$ test cases follow. Each test case of the input is given by three lines. The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2cdot10^5, 1 le m le 10^4$$$) — the initial length of the array $$$a$$$ and the value to take the remainder by. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^4$$$) — the elements of the array $$$a$$$. The third line contains a string $$$s$$$ consisting of $$$n$$$ characters 'L' and 'R'. It is guaranteed that the sum of the values of $$$n$$$ for all test cases in a test does not exceed $$$2cdot10^5$$$. Output For each test case, output $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$, where $$$b_i$$$ is the remainder when dividing the product of all elements of the current state of the array $$$a$$$ by $$$m$$$ at the beginning of the execution of the $$$i$$$-th command. Example Input 4 4 6 3 1 4 2 LRRL 5 1 1 1 1 1 1 LLLLL 6 8 1 2 3 4 5 6 RLLLRR 1 10000 10000 R Output 0 2 4 1 0 0 0 0 0 0 0 0 4 4 4 0 Note In the first test case of the example: $$$3 cdot 1 cdot 4 cdot 2 bmod 6 = 24 bmod 6 = 0$$$; $$$s_1 = ext{L}$$$, so we remove the first element and get the array $$$[1, 4, 2]$$$; $$$1 cdot 4 cdot 2 bmod 6 = 8 bmod 6 = 2$$$; $$$s_2 = ext{R}$$$, so we remove the last element and get the array $$$[1, 4]$$$; $$$1 cdot 4 bmod 6 = 4 bmod 6 = 4$$$; $$$s_3 = ext{R}$$$, so we remove the last element and get the array $$$[1]$$$; $$$1 bmod 6 = 1$$$; $$$s_4 = ext{L}$$$, so we remove the first element and get an empty array.
1,400
true
false
true
false
true
false
true
false
false
false
692
792F
Vova plays a computer game known as Mages and Monsters. Vova's character is a mage. Though as he has just started, his character knows no spells. Vova's character can learn new spells during the game. Every spell is characterized by two values _x__i_ and _y__i_ — damage per second and mana cost per second, respectively. Vova doesn't have to use a spell for an integer amount of seconds. More formally, if he uses a spell with damage _x_ and mana cost _y_ for _z_ seconds, then he will deal _x_·_z_ damage and spend _y_·_z_ mana (no rounding). If there is no mana left (mana amount is set in the start of the game and it remains the same at the beginning of every fight), then character won't be able to use any spells. It is prohibited to use multiple spells simultaneously. Also Vova can fight monsters. Every monster is characterized by two values _t__j_ and _h__j_ — monster kills Vova's character in _t__j_ seconds and has _h__j_ health points. Mana refills after every fight (or Vova's character revives with full mana reserve), so previous fights have no influence on further ones. Vova's character kills a monster, if he deals _h__j_ damage to it in no more than _t__j_ seconds using his spells (it is allowed to use more than one spell in a fight) and spending no more mana than he had at the beginning of the fight. If monster's health becomes zero exactly in _t__j_ seconds (it means that the monster and Vova's character kill each other at the same time), then Vova wins the fight. You have to write a program which can answer two types of queries: 1 _x_ _y_ — Vova's character learns new spell which deals _x_ damage per second and costs _y_ mana per second. 2 _t_ _h_ — Vova fights the monster which kills his character in _t_ seconds and has _h_ health points. Note that queries are given in a different form. Also remember that Vova's character knows no spells at the beginning of the game. For every query of second type you have to determine if Vova is able to win the fight with corresponding monster. Input The first line contains two integer numbers _q_ and _m_ (2u2009≤u2009_q_u2009≤u2009105,u20091u2009≤u2009_m_u2009≤u20091012) — the number of queries and the amount of mana at the beginning of every fight. _i_-th of each next _q_ lines contains three numbers _k__i_, _a__i_ and _b__i_ (1u2009≤u2009_k__i_u2009≤u20092,u20091u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009106). Using them you can restore queries this way: let _j_ be the index of the last query of second type with positive answer (_j_u2009=u20090 if there were none of these). If _k__i_u2009=u20091, then character learns spell with _x_u2009=u2009(_a__i_u2009+u2009_j_) _mod_ 106u2009+u20091, _y_u2009=u2009(_b__i_u2009+u2009_j_) _mod_ 106u2009+u20091. If _k__i_u2009=u20092, then you have to determine if Vova is able to win the fight against monster with _t_u2009=u2009(_a__i_u2009+u2009_j_) _mod_ 106u2009+u20091, _h_u2009=u2009(_b__i_u2009+u2009_j_) _mod_ 106u2009+u20091. Note In first example Vova's character at first learns the spell with 5 damage and 10 mana cost per second. Next query is a fight with monster which can kill character in 20 seconds and has 50 health points. Vova kills it in 10 seconds (spending 100 mana). Next monster has 52 health, so Vova can't deal that much damage with only 100 mana.
3,100
false
false
false
false
true
false
false
false
false
false
6,608
1990E1
This is the easy version of the problem. The only difference is the limit on the number of queries. This is an interactive problem. You are given a tree of $$$n$$$ nodes with node $$$1$$$ as its root node. There is a hidden mole in one of the nodes. To find its position, you can pick an integer $$$x$$$ ($$$1 le x le n$$$) to make an inquiry to the jury. Next, the jury will return $$$1$$$ when the mole is in subtree $$$x$$$. Otherwise, the judge will return $$$0$$$. If the judge returns $$$0$$$ and the mole is not in root node $$$1$$$, the mole will move to the parent node of the node it is currently on. Use at most $$$300$$$ operations to find the current node where the mole is located. Interaction The first line of each test case contains one integer $$$n$$$ ($$$2 le n le 5000$$$). The following $$$n-1$$$ lines describe the edges of the tree. Each line contains two space-separated integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$), indicating an edge between nodes $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the input data represents a tree. The interactor in this task is not adaptive. In other words, the node where the mole is located at first is fixed in every test case and does not change during the interaction. To ask a query, you need to pick a vertex $$$x$$$ ($$$1 le x le n$$$) and print the line of the following form: "? x" After that, you receive: $$$0$$$ if the mole is not in subtree $$$x$$$; $$$1$$$ if the mole is in subtree $$$x$$$. You can make at most $$$300$$$ queries of this form for each test case. Next, if your program has found the current node where the mole is located, print the line of the following form: "! x" Note that this line is not considered a query and is not taken into account when counting the number of queries asked. After this, proceed to the next test case. If you make more than $$$300$$$ queries during an interaction, your program must terminate immediately, and you will receive the Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query or the answer for a test case, do not forget to output the end of line and flush the output. Otherwise, you will get the verdict Idleness Limit Exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hacks To hack, follow the test format below. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$2 le n le 5000$$$, $$$1 le x le n$$$)xa0— the size of the tree and the initial position of the mole. The following $$$n-1$$$ lines describe the edges of the tree. Each line contains two space-separated integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$), indicating an edge between nodes $$$u_i$$$ and $$$v_i$$$. The input data must represent a tree. Note In the first test case, the mole is in node $$$2$$$ initially. For the query "? 2", the jury returns $$$1$$$ because the mole is in subtree $$$2$$$. After this query, the mole does not move. The answer $$$2$$$ is the current node where the mole is located, so the answer is considered correct. In the second test case, the mole is in node $$$6$$$ initially. For the query "? 2", the jury returns $$$0$$$ because the mole is not in subtree $$$2$$$. After this query, the mole moves from node $$$6$$$ to node $$$5$$$. For the query "? 6", the jury returns $$$0$$$ because the mole is not in subtree $$$6$$$. After this query, the mole moves from node $$$5$$$ to node $$$4$$$. For the query "? 4", the jury returns $$$1$$$ because the mole is in subtree $$$4$$$. After this query, the mole does not move. The answer $$$4$$$ is the current node where the mole is located, so the answer is considered correct. Please note that the example is only for understanding the statement, and the queries in the example do not guarantee to determine the unique position of the mole.
2,500
false
false
false
false
true
false
false
true
false
false
331
159D
Problem - 159D - Codeforces =============== xa0 such that 1u2009≤u2009_a_u2009≤u2009_b_u2009<u2009_x_u2009≤u2009_y_u2009≤u2009_s_ and substrings _s_
1,500
false
false
false
true
false
false
true
false
false
false
9,237
63C
The "Bulls and Cows" game needs two people to play. The thinker thinks of a number and the guesser tries to guess it. The thinker thinks of a four-digit number in the decimal system. All the digits in the number are different and the number may have a leading zero. It can't have more than one leading zero, because all it's digits should be different. The guesser tries to guess the number. He makes a series of guesses, trying experimental numbers and receives answers from the first person in the format "_x_ bulls _y_ cows". _x_ represents the number of digits in the experimental number that occupy the same positions as in the sought number. _y_ represents the number of digits of the experimental number that present in the sought number, but occupy different positions. Naturally, the experimental numbers, as well as the sought number, are represented by four-digit numbers where all digits are different and a leading zero can be present. For example, let's suppose that the thinker thought of the number 0123. Then the guessers' experimental number 1263 will receive a reply "1 bull 2 cows" (3 occupies the same positions in both numbers and 1 and 2 are present in both numbers but they occupy different positions). Also, the answer to number 8103 will be "2 bulls 1 cow" (analogically, 1 and 3 occupy the same positions and 0 occupies a different one). When the guesser is answered "4 bulls 0 cows", the game is over. Now the guesser has already made several guesses and wants to know whether his next guess can possibly be the last one. Input The first input line contains an integer _n_ (1u2009≤u2009_n_u2009≤u200910) which represents the number of already made guesses. Then follow _n_ lines in the form of "_a__i_ _b__i_ _c__i_", where _a__i_ is the _i_-th experimental number, _b__i_ is the number of bulls, _c__i_ is the number of cows (1u2009≤u2009_i_u2009≤u2009_n_, 0u2009≤u2009_b__i_,u2009_c__i_,u2009_b__i_u2009+u2009_c__i_u2009≤u20094). The experimental numbers are correct, i.e., each of them contains exactly four digits, in each of them all the four digits are different, and there can be a leading zero. All the experimental numbers are different. As the guesser hasn't guessed the number yet, the answer "4 bulls 0 cows" is not present. Output If the input data is enough to determine the sought number, print the number with four digits on a single line. If it has less than four digits, add leading zero. If the data is not enough, print "Need more data" without the quotes. If the thinker happens to have made a mistake in his replies, print "Incorrect data" without the quotes.
1,700
false
false
true
false
false
false
true
false
false
false
9,658
955C
Problem - 955C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search math number theory *2100 No tag edit access → Contest materials ") . For each query you have to find the number of such _x_ that _L_u2009≤u2009_x_u2009≤u2009_R_ and there exist integer numbers _a_u2009>u20090, _p_u2009>u20091 such that _x_u2009=u2009_a__p_. Input The first line contains the number of queries _Q_ (1u2009≤u2009_Q_u2009≤u2009105). The next _Q_ lines contains two integers _L_, _R_ each (1u2009≤u2009_L_u2009≤u2009_R_u2009≤u20091018). Output Output _Q_ lines — the answers to the queries. Example Input 6 1 4 9 9 5 7 12 29 137 591 1 1000000 Output 2 1 0 3 17 1111 Note In query one the suitable numbers are 1 and 4.
2,100
true
false
false
false
false
false
false
true
false
false
5,898
1974G
You can never buy enough happiness, so here we go again! In this version, you can only buy $$$h_i = 1$$$ unit of happiness each month, but the number of months is hugely increased. We are in the realm of quantum happiness and time dilation. Being a physicist, Charlie likes to plan his life in simple and precise terms. For the next $$$m$$$ months, starting with no money, Charlie will work hard and earn $$$x$$$ pounds per month. For the $$$i$$$-th month $$$(1 le i le m)$$$, there'll be a single opportunity of paying cost $$$c_i$$$ pounds to obtain one unit of happiness. You cannot buy more than one unit each month. Borrowing is not allowed. Money earned in the $$$i$$$-th month can only be spent in a later $$$j$$$-th month ($$$j>i$$$). Since physicists don't code, help Charlie find the maximum reachable units of happiness. Input The first line of the input contains $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers, $$$m$$$ and $$$x$$$ ($$$1 le m le 2 cdot 10^5$$$, $$$1 le x le 10^3$$$)xa0— the total number of months and the monthly salary. The second line of each test case contains $$$m$$$ integers $$$c_1, c_2, dots, c_m$$$ ($$$1 leq c_i leq 10^3$$$)xa0— the cost of one unit of happiness for each month. It is guaranteed that sum of $$$m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output one integerxa0— the maximal amount of happiness Charlie can get. Example Input 6 3 3 2 2 2 6 5 2 2 8 2 6 8 6 4 4 10 3 8 6 10 2 1 1 1 4 1 4 1 3 1 4 2 1 3 4 3
2,000
false
true
false
false
true
false
false
false
true
false
443
442D
When Adam gets a rooted tree (connected non-directed graph without cycles), he immediately starts coloring it. More formally, he assigns a color to each edge of the tree so that it meets the following two conditions: There is no vertex that has more than two incident edges painted the same color. For any two vertexes that have incident edges painted the same color (say, _c_), the path between them consists of the edges of the color _c_. Not all tree paintings are equally good for Adam. Let's consider the path from some vertex to the root. Let's call the number of distinct colors on this path the cost of the vertex. The cost of the tree's coloring will be the maximum cost among all the vertexes. Help Adam determine the minimum possible cost of painting the tree. Initially, Adam's tree consists of a single vertex that has number one and is the root. In one move Adam adds a new vertex to the already existing one, the new vertex gets the number equal to the minimum positive available integer. After each operation you need to calculate the minimum cost of coloring the resulting tree. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009106) — the number of times a new vertex is added. The second line contains _n_ numbers _p__i_ (1u2009≤u2009_p__i_u2009≤u2009_i_) — the numbers of the vertexes to which we add another vertex. Output Print _n_ integers — the minimum costs of the tree painting after each addition. Examples Input 11 1 1 1 3 4 4 7 3 7 6 6 Output 1 1 1 1 1 2 2 2 2 2 3 Note The figure below shows one of the possible variants to paint a tree from the sample at the last moment. The cost of the vertexes with numbers 11 and 12 equals 3.
2,600
false
false
false
false
true
false
false
false
false
false
8,067
2027B
Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an $$$mathcal{O}(n)$$$ time complexity. It goes as follows: starting from the second element in the array, if it is strictly smaller than the previous element (ignoring those which have already been deleted), then delete it. Continue iterating through the array until it is sorted in non-decreasing order. For example, the array $$$[1, 4, 2, 3, 6, 5, 5, 7, 7]$$$ becomes $$$[1, 4, 6, 7, 7]$$$ after a Stalin Sort. We define an array as vulnerable if you can sort it in non-increasing order by repeatedly applying a Stalin Sort to any of its subarrays$$$^{ ext{∗}}$$$, as many times as is needed. Given an array $$$a$$$ of $$$n$$$ integers, determine the minimum number of integers which must be removed from the array to make it vulnerable. $$$^{ ext{∗}}$$$An array $$$a$$$ is a subarray of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of test cases. This is followed by descriptions of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2000$$$)xa0— the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, output a single integerxa0— the minimum number of integers which must be removed from the array to make it vulnerable. Example Input 6 7 3 6 4 9 2 5 2 5 5 4 4 2 2 8 2 2 4 4 6 6 10 10 1 1000 9 6 8 9 10 12 9 7 5 4 7 300000000 600000000 400000000 900000000 200000000 400000000 200000000 Note In the first test case, the optimal answer is to remove the numbers $$$3$$$ and $$$9$$$. Then we are left with $$$a = [6, 4, 2, 5, 2]$$$. To show this array is vulnerable, we can first apply a Stalin Sort on the subarray $$$[4, 2, 5]$$$ to get $$$a = [6, 4, 5, 2]$$$ and then apply a Stalin Sort on the subarray $$$[6, 4, 5]$$$ to get $$$a = [6, 2]$$$, which is non-increasing. In the second test case, the array is already non-increasing, so we don't have to remove any integers.
1,100
false
true
false
false
false
false
true
false
false
false
109
1487E
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert. There are $$$n_1$$$ different types of first courses Ivan can buy (the $$$i$$$-th of them costs $$$a_i$$$ coins), $$$n_2$$$ different types of second courses (the $$$i$$$-th of them costs $$$b_i$$$ coins), $$$n_3$$$ different types of drinks (the $$$i$$$-th of them costs $$$c_i$$$ coins) and $$$n_4$$$ different types of desserts (the $$$i$$$-th of them costs $$$d_i$$$ coins). Some dishes don't go well with each other. There are $$$m_1$$$ pairs of first courses and second courses that don't go well with each other, $$$m_2$$$ pairs of second courses and drinks, and $$$m_3$$$ pairs of drinks and desserts that don't go well with each other. Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option! Input The first line contains four integers $$$n_1$$$, $$$n_2$$$, $$$n_3$$$ and $$$n_4$$$ ($$$1 le n_i le 150000$$$) — the number of types of first courses, second courses, drinks and desserts, respectively. Then four lines follow. The first line contains $$$n_1$$$ integers $$$a_1, a_2, dots, a_{n_1}$$$ ($$$1 le a_i le 10^8$$$), where $$$a_i$$$ is the cost of the $$$i$$$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($$$1 le b_i, c_i, d_i le 10^8$$$). The next line contains one integer $$$m_1$$$ ($$$0 le m_1 le 200000$$$)xa0— the number of pairs of first and second courses that don't go well with each other. Each of the next $$$m_1$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i le n_1$$$; $$$1 le y_i le n_2$$$) denoting that the first course number $$$x_i$$$ doesn't go well with the second course number $$$y_i$$$. All these pairs are different. The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($$$0 le m_2, m_3 le 200000$$$). Output If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $$$-1$$$. Otherwise, print one integerxa0— the minimum total cost of the dinner. Examples Input 4 3 2 1 1 2 3 4 5 6 7 8 9 10 2 1 2 1 1 2 3 1 3 2 1 1 1 Input 1 1 1 1 1 1 1 1 1 1 1 0 0 Note The best option in the first example is to take the first course $$$2$$$, the second course $$$1$$$, the drink $$$2$$$ and the dessert $$$1$$$. In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
2,000
false
true
true
false
true
false
true
false
true
true
3,242
1221F
Petya recently found a game "Choose a Square". In this game, there are $$$n$$$ points numbered from $$$1$$$ to $$$n$$$ on an infinite field. The $$$i$$$-th point has coordinates $$$(x_i, y_i)$$$ and cost $$$c_i$$$. You have to choose a square such that its sides are parallel to coordinate axes, the lower left and upper right corners belong to the line $$$y = x$$$, and all corners have integer coordinates. The score you get is the sum of costs of the points covered by the selected square minus the length of the side of the square. Note that the length of the side can be zero. Petya asks you to calculate the maximum possible score in the game that can be achieved by placing exactly one square. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$) — the number of points on the field. Each of the following $$$n$$$ lines contains three integers $$$x_i, y_i, c_i$$$ ($$$0 le x_i, y_i le 10^9, -10^6 le c_i le 10^6$$$) — coordinates of the $$$i$$$-th point and its cost, respectively. Output In the first line print the maximum score Petya can achieve. In the second line print four integers $$$x_1, y_1, x_2, y_2$$$ ($$$0 le x_1, y_1, x_2, y_2 le 2 cdot 10^9, x_1 = y_1, x_2 = y_2, x_1 le x_2$$$) separated by spaces — the coordinates of the lower left and upper right corners of the square which Petya has to select in order to achieve the maximum score. Examples Input 6 0 0 2 1 0 -5 1 1 3 2 3 4 1 4 -4 3 1 -1 Input 5 3 3 0 3 3 -3 0 2 -1 3 1 3 0 0 -2 Note The field corresponding to the first example:
2,400
false
false
false
false
true
false
false
true
true
false
4,561
1666I
This is an interactive problem. There is a grid of $$$n imes m$$$ cells. Two treasure chests are buried in two different cells of the grid. Your task is to find both of them. You can make two types of operations: DIG $$$r$$$ $$$c$$$: try to find the treasure in the cell $$$(r, c)$$$. The interactor will tell you if you found the treasure or not. SCAN $$$r$$$ $$$c$$$: scan from the cell $$$(r, c)$$$. The result of this operation is the sum of Manhattan distances from the cell $$$(r, c)$$$ to the cells where the treasures are hidden. Manhattan distance from a cell $$$(r_1, c_1)$$$ to a cell $$$(r_2, c_2)$$$ is calculated as $$$r_1 - r_2 + c_1 - c_2$$$. You need to find the treasures in at most 7 operations. This includes both DIG and SCAN operations in total. To solve the test you need to call DIG operation at least once in both of the cells where the treasures are hidden. Interaction Your program has to process multiple test cases in a single run. First, the testing system writes $$$t$$$xa0— the number of test cases ($$$1le t le 100$$$). Then, $$$t$$$ test cases should be processed one by one. In each test case, your program should start by reading the integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 16$$$). Then, your program can make queries of two types: DIG $$$r$$$ $$$c$$$ ($$$1le rle n$$$; $$$1le cle m$$$). The interactor responds with integer $$$1$$$, if you found the treasure, and $$$0$$$ if not. If you try to find the treasure in the same cell multiple times, the result will be $$$0$$$ since the treasure is already found. SCAN $$$r$$$ $$$c$$$ ($$$1le rle n$$$; $$$1le cle m$$$). The interactor responds with an integer that is the sum of Manhattan distances from the cell $$$(r, c)$$$ to the cells where the treasures were hidden. The sum is calculated for both cells with treasures, even if you already found one of them. After you found both treasures, i.xa0e. you received $$$1$$$ for two DIG operations, your program should continue to the next test case or exit if that test case was the last one. Example Output SCAN 1 2 DIG 1 2 SCAN 2 2 DIG 1 1 DIG 1 3
2,200
true
false
false
false
false
true
true
false
false
false
2,277
1744E2
This is an hard version of the problem. The only difference between an easy and a hard version is the constraints on $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$. You are given $$$4$$$ positive integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ with $$$a < c$$$ and $$$b < d$$$. Find any pair of numbers $$$x$$$ and $$$y$$$ that satisfies the following conditions: $$$a < x leq c$$$, $$$b < y leq d$$$, $$$x cdot y$$$ is divisible by $$$a cdot b$$$. Note that required $$$x$$$ and $$$y$$$ may not exist. Input The first line of the input contains a single integer $$$t$$$ $$$(1 leq t leq 10$$$), the number of test cases. The descriptions of the test cases follow. The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$1 leq a < c leq 10^9$$$, $$$1 leq b < d leq 10^9$$$). Output For each test case print a pair of numbers $$$a < x leq c$$$ and $$$b < y leq d$$$ such that $$$x cdot y$$$ is divisible by $$$a cdot b$$$. If there are multiple answers, print any of them. If there is no such pair of numbers, then print -1 -1. Example Input 10 1 1 2 2 3 4 5 7 8 9 15 18 12 21 14 24 36 60 48 66 1024 729 373248 730 1024 729 373247 730 5040 40320 40319 1000000000 999999999 999999999 1000000000 1000000000 268435456 268435456 1000000000 1000000000 Output 2 2 4 6 12 12 -1 -1 -1 -1 373248 730 -1 -1 15120 53760 -1 -1 536870912 536870912
1,900
true
false
false
false
false
false
true
false
false
false
1,812
2029E
For two integers $$$x$$$ and $$$y$$$ ($$$x,yge 2$$$), we will say that $$$x$$$ is a generator of $$$y$$$ if and only if $$$x$$$ can be transformed to $$$y$$$ by performing the following operation some number of times (possibly zero): Choose a divisor $$$d$$$ ($$$dge 2$$$) of $$$x$$$, then increase $$$x$$$ by $$$d$$$. For example, $$$3$$$ is a generator of $$$8$$$ since we can perform the following operations: $$$3 xrightarrow{d = 3} 6 xrightarrow{d = 2} 8$$$; $$$4$$$ is a generator of $$$10$$$ since we can perform the following operations: $$$4 xrightarrow{d = 4} 8 xrightarrow{d = 2} 10$$$; $$$5$$$ is not a generator of $$$6$$$ since we cannot transform $$$5$$$ into $$$6$$$ with the operation above. Now, Kevin gives you an array $$$a$$$ consisting of $$$n$$$ pairwise distinct integers ($$$a_ige 2$$$). You have to find an integer $$$xge 2$$$ such that for each $$$1le ile n$$$, $$$x$$$ is a generator of $$$a_i$$$, or determine that such an integer does not exist. Input Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 10^5$$$)xa0— the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$2le a_ile 4cdot 10^5$$$)xa0— the elements in the array $$$a$$$. It is guaranteed that the elements are pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integer $$$x$$$xa0— the integer you found. Print $$$-1$$$ if there does not exist a valid $$$x$$$. If there are multiple answers, you may output any of them. Example Input 4 3 8 9 10 4 2 3 4 5 2 147 154 5 3 6 8 25 100000 Note In the first test case, for $$$x=2$$$: $$$2$$$ is a generator of $$$8$$$, since we can perform the following operations: $$$2 xrightarrow{d = 2} 4 xrightarrow{d = 4} 8$$$; $$$2$$$ is a generator of $$$9$$$, since we can perform the following operations: $$$2 xrightarrow{d = 2} 4 xrightarrow{d = 2} 6 xrightarrow{d = 3} 9$$$. $$$2$$$ is a generator of $$$10$$$, since we can perform the following operations: $$$2 xrightarrow{d = 2} 4 xrightarrow{d = 2} 6 xrightarrow{d = 2} 8 xrightarrow{d = 2} 10$$$. In the second test case, it can be proven that it is impossible to find a common generator of the four integers.
2,100
true
false
false
false
false
true
true
false
false
false
93
549G
Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of _n_ Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person _a_ stands just behind person _b_, then person _a_ can pay person _b_ 1 dollar, then _a_ and _b_ get swapped. Of course, if person _a_ has zero dollars, he can not swap places with person _b_. Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them. Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000) — the number of residents who stand in the line. The second line contains _n_ space-separated integers _a__i_ (0u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ is the number of Berland dollars of a man standing on the _i_-th position in the line. The positions are numbered starting from the end of the line. Output If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line _n_ space-separated integers, the _i_-th of them must be equal to the number of money of the person on position _i_ in the new line. If there are multiple answers, print any of them. Note In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line. In the second sample it is impossible to achieve the desired result. In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
2,200
false
true
false
false
false
true
false
false
true
false
7,639
1649B
Daniel is watching a football team playing a game during their training session. They want to improve their passing skills during that session. The game involves $$$n$$$ players, making multiple passes towards each other. Unfortunately, since the balls were moving too fast, after the session Daniel is unable to know how many balls were involved during the game. The only thing he knows is the number of passes delivered by each player during all the session. Find the minimum possible amount of balls that were involved in the game. Input There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5 cdot 10^4$$$)xa0— the number of test cases. This is followed by the test cases description. The first line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 10^5$$$)xa0— the number of players. The second line of the test case contains a sequence of integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$), where $$$a_i$$$ is the number of passes delivered by the $$$i$$$-th player. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case print a single integerxa0— the answer to the problem. Example Input 4 4 2 3 3 2 3 1 5 2 2 0 0 4 1000000000 1000000000 1000000000 1000000000 Note In the first test case, with the only ball, the game can go like this: $$$2 ightarrow 1 ightarrow 3 ightarrow 4 ightarrow 1 ightarrow 2 ightarrow 3 ightarrow 4 ightarrow 2 ightarrow 3 ightarrow 2$$$. In the second test case, there is no possible way to play the game with only one ball. One possible way to play with two balls: $$$2 ightarrow 1 ightarrow 2 ightarrow 3 ightarrow 2 ightarrow 1$$$. $$$2 ightarrow 3 ightarrow 2 ightarrow 1$$$ In the third example, there were no passes, so $$$0$$$ balls are possible.
1,300
false
true
true
false
false
false
false
false
false
false
2,376
1462D
Polycarp was given an array of $$$a[1 dots n]$$$ of $$$n$$$ integers. He can perform the following operation with the array $$$a$$$ no more than $$$n$$$ times: Polycarp selects the index $$$i$$$ and adds the value $$$a_i$$$ to one of his choice of its neighbors. More formally, Polycarp adds the value of $$$a_i$$$ to $$$a_{i-1}$$$ or to $$$a_{i+1}$$$ (if such a neighbor does not exist, then it is impossible to add to it). After adding it, Polycarp removes the $$$i$$$-th element from the $$$a$$$ array. During this step the length of $$$a$$$ is decreased by $$$1$$$. The two items above together denote one single operation. For example, if Polycarp has an array $$$a = [3, 1, 6, 6, 2]$$$, then it can perform the following sequence of operations with it: Polycarp selects $$$i = 2$$$ and adds the value $$$a_i$$$ to $$$(i-1)$$$-th element: $$$a = [4, 6, 6, 2]$$$. Polycarp selects $$$i = 1$$$ and adds the value $$$a_i$$$ to $$$(i+1)$$$-th element: $$$a = [10, 6, 2]$$$. Polycarp selects $$$i = 3$$$ and adds the value $$$a_i$$$ to $$$(i-1)$$$-th element: $$$a = [10, 8]$$$. Polycarp selects $$$i = 2$$$ and adds the value $$$a_i$$$ to $$$(i-1)$$$-th element: $$$a = [18]$$$. Note that Polycarp could stop performing operations at any time. Polycarp wondered how many minimum operations he would need to perform to make all the elements of $$$a$$$ equal (i.e., he wants all $$$a_i$$$ are equal to each other). Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 3000$$$)xa0— the number of test cases in the test. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 3000$$$)xa0— the length of the array. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^5$$$)xa0— array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. Note In the first test case of the example, the answer can be constructed like this (just one way among many other ways): $$$[3, 1, 6, 6, 2]$$$ $$$xrightarrow[]{i=4,~add~to~left}$$$ $$$[3, 1, 12, 2]$$$ $$$xrightarrow[]{i=2,~add~to~right}$$$ $$$[3, 13, 2]$$$ $$$xrightarrow[]{i=1,~add~to~right}$$$ $$$[16, 2]$$$ $$$xrightarrow[]{i=2,~add~to~left}$$$ $$$[18]$$$. All elements of the array $$$[18]$$$ are the same. In the second test case of the example, the answer can be constructed like this (just one way among other ways): $$$[1, 2, 2, 1]$$$ $$$xrightarrow[]{i=1,~add~to~right}$$$ $$$[3, 2, 1]$$$ $$$xrightarrow[]{i=3,~add~to~left}$$$ $$$[3, 3]$$$. All elements of the array $$$[3, 3]$$$ are the same. In the third test case of the example, Polycarp doesn't need to perform any operations since $$$[2, 2, 2]$$$ contains equal (same) elements only. In the fourth test case of the example, the answer can be constructed like this (just one way among other ways): $$$[6, 3, 2, 1]$$$ $$$xrightarrow[]{i=3,~add~to~right}$$$ $$$[6, 3, 3]$$$ $$$xrightarrow[]{i=3,~add~to~left}$$$ $$$[6, 6]$$$. All elements of the array $$$[6, 6]$$$ are the same.
1,400
true
true
false
false
false
false
false
false
false
false
3,377
432D
Problem - 432D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp string suffix structures strings two pointers *2000 No tag edit access → Contest materials ") Editorial ") of string _s_ is string _s__i__s__i_u2009+u20091..._s__j_. The prefix of string _s_ of length _l_ (1u2009≤u2009_l_u2009≤u2009_s_) is string _s_
2,000
false
false
false
true
false
false
false
false
false
false
8,111
749E
You are given a permutation of integers from 1 to _n_. Exactly once you apply the following operation to this permutation: pick a random segment and shuffle its elements. Formally: 1. Pick a random segment (continuous subsequence) from _l_ to _r_. All segments are equiprobable. 2. Let _k_u2009=u2009_r_u2009-u2009_l_u2009+u20091, i.e. the length of the chosen segment. Pick a random permutation of integers from 1 to _k_, _p_1,u2009_p_2,u2009...,u2009_p__k_. All _k_! permutation are equiprobable. 3. This permutation is applied to elements of the chosen segment, i.e. permutation _a_1,u2009_a_2,u2009...,u2009_a__l_u2009-u20091,u2009_a__l_,u2009_a__l_u2009+u20091,u2009...,u2009_a__r_u2009-u20091,u2009_a__r_,u2009_a__r_u2009+u20091,u2009...,u2009_a__n_ is transformed to _a_1,u2009_a_2,u2009...,u2009_a__l_u2009-u20091,u2009_a__l_u2009-u20091u2009+u2009_p_1,u2009_a__l_u2009-u20091u2009+u2009_p_2,u2009...,u2009_a__l_u2009-u20091u2009+u2009_p__k_u2009-u20091,u2009_a__l_u2009-u20091u2009+u2009_p__k_,u2009_a__r_u2009+u20091,u2009...,u2009_a__n_. Inversion if a pair of elements (not necessary neighbouring) with the wrong relative order. In other words, the number of inversion is equal to the number of pairs (_i_,u2009_j_) such that _i_u2009<u2009_j_ and _a__i_u2009>u2009_a__j_. Find the expected number of inversions after we apply exactly one operation mentioned above. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the length of the permutation. The second line contains _n_ distinct integers from 1 to _n_xa0— elements of the permutation. Output Print one real valuexa0— the expected number of inversions. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20099. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Example Output 1.916666666666666666666666666667
2,400
false
false
false
false
true
false
false
false
false
false
6,794
625B
A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000. This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol's artificial intelligence. Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol's director decided to replace some characters in AI name with "#". As this operation is pretty expensive, you should find the minimum number of characters to replace with "#", such that the name of AI doesn't contain the name of the phone as a substring. Substring is a continuous subsequence of a string. Input The first line of the input contains the name of AI designed by Gogol, its length doesn't exceed 100u2009000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn't exceed 30. Both string are non-empty and consist of only small English letters. Output Print the minimum number of characters that must be replaced with "#" in order to obtain that the name of the phone doesn't occur in the name of AI as a substring. Note In the first sample AI's name may be replaced with "int#llect". In the second sample Gogol can just keep things as they are. In the third sample one of the new possible names of AI may be "s#ris#ri".
1,200
false
true
false
false
false
true
false
false
false
false
7,332
1704B
Luke likes to eat. There are $$$n$$$ piles of food aligned in a straight line in front of him. The $$$i$$$-th pile contains $$$a_i$$$ units of food. Luke will walk from the $$$1$$$-st pile towards the $$$n$$$-th pile, and he wants to eat every pile of food without walking back. When Luke reaches the $$$i$$$-th pile, he can eat that pile if and only if $$$v - a_i leq x$$$, where $$$x$$$ is a fixed integer, and $$$v$$$ is Luke's food affinity. Before Luke starts to walk, he can set $$$v$$$ to any integer. Also, for each $$$i$$$ ($$$1 leq i leq n$$$), Luke can change his food affinity to any integer before he eats the $$$i$$$-th pile. Find the minimum number of changes needed to eat every pile of food. Note that the initial choice for $$$v$$$ is not considered as a change. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The description of test cases follows. For each test case, the first line contains two integers, $$$n, x$$$ ($$$1 leq n leq 2 cdot 10^5$$$, $$$1 leq x leq 10^9$$$) — the number of piles, and the maximum difference between the size of a pile and Luke's food affinity, such that Luke can eat the pile. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots , a_n$$$ ($$$1 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output an integer on a separate line, which is the minimum number of changes needed. Example Input 7 5 3 3 8 5 6 7 5 3 3 10 9 8 7 12 8 25 3 3 17 8 6 1 16 15 25 17 23 10 2 1 2 3 4 5 6 7 8 9 10 8 2 2 4 6 8 6 4 12 14 8 2 2 7 8 9 6 13 21 28 15 5 11 4 13 23 7 10 5 21 20 11 17 5 29 16 11 Note In the first test case, Luke can set $$$v$$$ to $$$5$$$ before he starts to walk. And he can walk straight to eat every piles of food without changing $$$v$$$. In the second test case, Luke can set $$$v$$$ to $$$3$$$ before he starts to walk. And he could change $$$v$$$ to $$$10$$$ before he eats the second pile. After that, he can walk straight to eat remaining food without changing $$$v$$$. In the fourth test case, Luke can set $$$v$$$ to $$$3$$$ before he starts to walk. And he could change $$$v$$$ to $$$8$$$ before he eats the sixth pile. After that, he can walk straight to eat remaining food without changing $$$v$$$. In the fifth test case, Luke can set $$$v$$$ to $$$4$$$ before he starts to walk. And he could change $$$v$$$ to $$$6$$$ before he eats the fourth pile. Then he could change $$$v$$$ to $$$12$$$ before he eats the seventh pile. After that, he can walk straight to eat remaining food without changing $$$v$$$.
1,000
false
true
true
false
false
false
true
false
false
false
2,054
1270H
Suppose that we have an array of $$$n$$$ distinct numbers $$$a_1, a_2, dots, a_n$$$. Let's build a graph on $$$n$$$ vertices as follows: for every pair of vertices $$$i < j$$$ let's connect $$$i$$$ and $$$j$$$ with an edge, if $$$a_i < a_j$$$. Let's define weight of the array to be the number of connected components in this graph. For example, weight of array $$$[1, 4, 2]$$$ is $$$1$$$, weight of array $$$[5, 4, 3]$$$ is $$$3$$$. You have to perform $$$q$$$ queries of the following formxa0— change the value at some position of the array. After each operation, output the weight of the array. Updates are not independent (the change stays for the future). Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 5 cdot 10^5$$$)xa0— the size of the array and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0— the initial array. Each of the next $$$q$$$ lines contains two integers $$$pos$$$ and $$$x$$$ ($$$1 le pos le n$$$, $$$1 le x le 10^6, x e a_{pos}$$$). It means that you have to make $$$a_{pos}=x$$$. It's guaranteed that at every moment of time, all elements of the array are different. Output After each query, output the weight of the array. Example Input 5 3 50 40 30 20 10 1 25 3 45 1 48 Note After the first query array looks like $$$[25, 40, 30, 20, 10]$$$, the weight is equal to $$$3$$$. After the second query array looks like $$$[25, 40, 45, 20, 10]$$$, the weight is still equal to $$$3$$$. After the third query array looks like $$$[48, 40, 45, 20, 10]$$$, the weight is equal to $$$4$$$.
3,300
false
false
false
false
true
false
false
false
false
false
4,337
33A
In one little known, but very beautiful country called Waterland, lives a lovely shark Valerie. Like all the sharks, she has several rows of teeth, and feeds on crucians. One of Valerie's distinguishing features is that while eating one crucian she uses only one row of her teeth, the rest of the teeth are "relaxing". For a long time our heroine had been searching the sea for crucians, but a great misfortune happened. Her teeth started to ache, and she had to see the local dentist, lobster Ashot. As a professional, Ashot quickly relieved Valerie from her toothache. Moreover, he managed to determine the cause of Valerie's developing caries (for what he was later nicknamed Cap). It turned that Valerie eats too many crucians. To help Valerie avoid further reoccurrence of toothache, Ashot found for each Valerie's tooth its residual viability. Residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth. Every time Valerie eats a crucian, viability of all the teeth used for it will decrease by one. When the viability of at least one tooth becomes negative, the shark will have to see the dentist again. Unhappy, Valerie came back home, where a portion of crucians was waiting for her. For sure, the shark couldn't say no to her favourite meal, but she had no desire to go back to the dentist. That's why she decided to eat the maximum amount of crucians from the portion but so that the viability of no tooth becomes negative. As Valerie is not good at mathematics, she asked you to help her to find out the total amount of crucians that she can consume for dinner. We should remind you that while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one. Input The first line contains three integers _n_, _m_, _k_ (1u2009≤u2009_m_u2009≤u2009_n_u2009≤u20091000,u20090u2009≤u2009_k_u2009≤u2009106) — total amount of Valerie's teeth, amount of tooth rows and amount of crucians in Valerie's portion for dinner. Then follow _n_ lines, each containing two integers: _r_ (1u2009≤u2009_r_u2009≤u2009_m_) — index of the row, where belongs the corresponding tooth, and _c_ (0u2009≤u2009_c_u2009≤u2009106) — its residual viability. It's guaranteed that each tooth row has positive amount of teeth.
1,200
false
true
true
false
false
false
false
false
false
false
9,833
1525C
There are $$$n$$$ robots driving along an OX axis. There are also two walls: one is at coordinate $$$0$$$ and one is at coordinate $$$m$$$. The $$$i$$$-th robot starts at an integer coordinate $$$x_i~(0 < x_i < m)$$$ and moves either left (towards the $$$0$$$) or right with the speed of $$$1$$$ unit per second. No two robots start at the same coordinate. Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed. Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens. For each robot find out if it ever explodes and print the time of explosion if it happens and $$$-1$$$ otherwise. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 3 cdot 10^5$$$; $$$2 le m le 10^8$$$)xa0— the number of robots and the coordinate of the right wall. The second line of each testcase contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$0 < x_i < m$$$)xa0— the starting coordinates of the robots. The third line of each testcase contains $$$n$$$ space-separated characters 'L' or 'R'xa0— the starting directions of the robots ('L' stands for left and 'R' stands for right). All coordinates $$$x_i$$$ in the testcase are distinct. The sum of $$$n$$$ over all testcases doesn't exceed $$$3 cdot 10^5$$$. Output For each testcase print $$$n$$$ integersxa0— for the $$$i$$$-th robot output the time it explodes at if it does and $$$-1$$$ otherwise. Example Input 5 7 12 1 2 3 4 9 10 11 R R L L R R R 2 10 1 6 R R 2 10 1 3 L L 1 10 5 R 7 8 6 1 7 2 3 5 4 R L R L L L L Output 1 1 1 1 2 -1 2 -1 -1 2 2 -1 -1 2 7 3 2 7 3 Note Here is the picture for the seconds $$$0, 1, 2$$$ and $$$3$$$ of the first testcase: Notice that robots $$$2$$$ and $$$3$$$ don't collide because they meet at the same point $$$2.5$$$, which is not integer. After second $$$3$$$ robot $$$6$$$ just drive infinitely because there's no robot to collide with.
2,000
false
true
true
false
true
false
false
false
true
false
3,040
724F
Problem - 724F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp trees *2700 No tag edit access → Contest materials + Photos") is present in the first tree if and only if the edge (_p__u_,u2009_p__v_) is present in the second tree. Vertex of the tree is called internal if its degree is greater than or equal to two. Count the number of different non-isomorphic trees, consisting of _n_ vertices, such that the degree of each internal vertex is exactly _d_. Print the answer over the given prime modulo _mod_. Input The single line of the input contains three integers _n_, _d_ andxa0_mod_ (1u2009≤u2009_n_u2009≤u20091000, 2u2009≤u2009_d_u2009≤u200910, 108u2009≤u2009_mod_u2009≤u2009109) xa0— the number of vertices in the tree, the degree of internal vertices and the prime modulo. Output Print the number of trees over the modulo _mod_. Examples Input 5 2 433416647 Output 1 Input 10 3 409693891 Output 2 Input 65 4 177545087 Output 910726
2,700
false
false
false
true
false
false
false
false
false
false
6,909
2041K
Image generated by ChatGPT 4o. In an interdisciplinary collaboration, an ecosystem scientist and a computer scientist join forces to analyze the structure of a complex ecosystem using computational methods. The ecosystem scientist models the ecosystem as a directed graph $$$D = (V, A)$$$, where each species is represented by a node $$$v in V$$$, and each feeding relationship is represented as a directed edge $$$(x, y) in A$$$ from prey $$$x$$$ to predator $$$y$$$. This graph structure allows them to simulate the flow of energy throughout the ecosystem from one species to another. Two essential features of the ecosystem are defined: Independent Trophic Group: A set $$$S$$$ of animal species is classified as an independent trophic group if no species $$$x in S$$$ can reach another species $$$y in S$$$ (for some $$$y e x$$$) through a series of directed feeding relationships, meaning there is no directed path in $$$D$$$ from $$$x$$$ to $$$y$$$. Trophic Balance Species: A species is termed a trophic balance species if it has a nearly equal number of species that affect it as directly or indirectly predators (species it can reach via a directed path in $$$D$$$, excluding itself) and species that affect it as directly or indirectly prey (species that can reach it via a directed path in $$$D$$$, excluding itself). Specifically, trophic balance species are those for which the absolute difference between the above two numbers is minimum among all species in the ecosystem. Consider an ecosystem with $$$n = 4$$$ species and $$$m = 3$$$ feeding relationships: Species 1: Grass (Node 1) Species 2: Rabbits (Node 2) Species 3: Foxes (Node 3) Species 4: Hawks (Node 4) The directed edges representing the feeding relationships are as follows: $$$(1, 2)$$$: Grass is eaten by Rabbits. $$$(2, 3)$$$: Rabbits are eaten by Foxes. $$$(2, 4)$$$: Rabbits are also eaten by Hawks. Now, consider the set $$$S={3,4}$$$ (Foxes and Hawks). There are no directed paths between Foxes (Node 3) and Hawks (Node 4); Foxes cannot reach Hawks, and Hawks cannot reach Foxes through any directed paths. Therefore, this set qualifies as an independent trophic group. Examination of Species Species 1 (Grass): Can reach: 3 (Rabbits, Foxes, and Hawks) Can be reached by: 0 (None) Absolute difference: $$$3 - 0 = 3$$$ Species 2 (Rabbits): Can reach: 2 (Foxes and Hawks) Can be reached by: 1 (Grass) Absolute difference: $$$2 - 1 = 1$$$ Species 3 (Foxes): Can reach: 0 (None) Can be reached by: 2 (Grass and Rabbits) Absolute difference: $$$0-2 = 2$$$ Species 4 (Hawks): Can reach: 0 (None) Can be reached by: 2 (Grass and Rabbits) Absolute difference: $$$0-2 = 2$$$ Among these species, Rabbits have the smallest absolute difference of $$$1$$$, indicating that they are a trophic balance species within the ecosystem. It is known that any independent trophic group in the ecosystem has a size of at most $$$k$$$. The task is to find the set of all trophic balance species in the ecosystem. Input The first line contains exactly two integers $$$n$$$ and $$$m$$$, where $$$n$$$ (resp. $$$m$$$) denotes the number of nodes (resp. edges) in the directed graph $$$D$$$ induced by the investigated ecosystem. The nodes are numbered as $$$1, 2, ldots, n$$$. Then, $$$m$$$ lines follow. The $$$i$$$-th line contains two integers $$$x_i$$$ and $$$y_i$$$ indicating a directed edge from node $$$x_i$$$ to node $$$y_i$$$. $$$1 le n le 2 imes 10^5$$$ $$$0 le m le min{ n(n-1), 4 imes 10^5}$$$ $$$k$$$ is not an input value, and it is guaranteed that $$$1 le k le 16$$$ for each investigated ecosystem. For all $$$i$$$ ($$$1le ile m$$$), $$$1le x_i, y_ile n$$$ and $$$x_i eq y_i$$$. Each ordered pair $$$(x_i, y_i)$$$ appears at most once in the input.
3,100
false
false
false
false
false
false
true
true
false
true
3
774L
Polycarp's workday lasts exactly $$$n$$$ minutes. He loves chocolate bars and can eat one bar in one minute. Today Polycarp has $$$k$$$ bars at the beginning of the workday. In some minutes of the workday Polycarp has important things to do and in such minutes he is not able to eat a chocolate bar. In other minutes he can either eat or not eat one chocolate bar. It is guaranteed, that in the first and in the last minutes of the workday Polycarp has no important things to do and he will always eat bars in this minutes to gladden himself at the begining and at the end of the workday. Also it is guaranteed, that $$$k$$$ is strictly greater than $$$1$$$. Your task is to determine such an order of eating chocolate bars that the maximum break time between eating bars is as minimum as possible. Consider that Polycarp eats a bar in the minute $$$x$$$ and the next bar in the minute $$$y$$$ ($$$x < y$$$). Then the break time is equal to $$$y - x - 1$$$ minutes. It is not necessary for Polycarp to eat all bars he has. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 200,000$$$, $$$2 le k le n$$$) — the length of the workday in minutes and the number of chocolate bars, which Polycarp has in the beginning of the workday. The second line contains the string with length $$$n$$$ consisting of zeros and ones. If the $$$i$$$-th symbol in the string equals to zero, Polycarp has no important things to do in the minute $$$i$$$ and he can eat a chocolate bar. In the other case, Polycarp is busy in the minute $$$i$$$ and can not eat a chocolate bar. It is guaranteed, that the first and the last characters of the string are equal to zero, and Polycarp always eats chocolate bars in these minutes. Output Print the minimum possible break in minutes between eating chocolate bars. Note In the first example Polycarp can not eat the chocolate bar in the second minute, so the time of the break equals to one minute. In the second example Polycarp will eat bars in the minutes $$$1$$$ and $$$8$$$ anyway, also he needs to eat the chocolate bar in the minute $$$5$$$, so that the time of the maximum break will be equal to $$$3$$$ minutes.
2,000
false
false
false
false
false
false
false
true
false
false
6,670
1205B
Problem - 1205B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks brute force graphs shortest paths *1900 No tag edit access → Contest materials are connected if and only if, $$$a_i$$$ AND $$$a_j eq 0$$$, where AND denotes the $$$xa0— number of numbers. The second line contains $$$n$$$ integer numbers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^{18}$$$). Output If the graph doesn't have any cycles, output $$$-1$$$. Else output the length of the shortest cycle. Examples Input 4 3 6 28 9 Output 4 Input 5 5 12 9 16 48 Output 3 Input 4 1 2 4 8 Output -1 Note In the first example, the shortest cycle is $$$(9, 3, 6, 28)$$$. In the second example, the shortest cycle is $$$(5, 12, 9)$$$. The graph has no cycles in the third example.
1,900
false
false
false
false
false
false
true
false
false
true
4,668
799D
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are _n_ extensions, the _i_-th of them multiplies the width or the length (by Arkady's choice) by _a__i_. Each extension can't be used more than once, the extensions can be used in any order. Now Arkady's field has size _h_u2009×u2009_w_. He wants to enlarge it so that it is possible to place a rectangle of size _a_u2009×u2009_b_ on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal. Input The first line contains five integers _a_, _b_, _h_, _w_ and _n_ (1u2009≤u2009_a_,u2009_b_,u2009_h_,u2009_w_,u2009_n_u2009≤u2009100u2009000)xa0— the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (2u2009≤u2009_a__i_u2009≤u2009100u2009000), where _a__i_ equals the integer a side multiplies by when the _i_-th extension is applied. Output Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0. Note In the first example it is enough to use any of the extensions available. For example, we can enlarge _h_ in 5 times using the second extension. Then _h_ becomes equal 10 and it is now possible to place the rectangle on the field.
2,100
false
false
false
true
false
false
true
false
false
false
6,573
1879C
You are given a binary string $$$s$$$. A binary string is a string consisting of characters 0 and/or 1. You can perform the following operation on $$$s$$$ any number of times (even zero): choose an integer $$$i$$$ such that $$$1 le i le s$$$, then erase the character $$$s_i$$$. You have to make $$$s$$$ alternating, i.u2009e. after you perform the operations, every two adjacent characters in $$$s$$$ should be different. Your goal is to calculate two values: the minimum number of operations required to make $$$s$$$ alternating; the number of different shortest sequences of operations that make $$$s$$$ alternating. Two sequences of operations are different if in at least one operation, the chosen integer $$$i$$$ is different in these two sequences. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 le s le 2 cdot 10^5$$$). The string $$$s$$$ consists of characters 0 and/or 1 only. Additional constraint on the input: the total length of strings $$$s$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print two integers: the minimum number of operations you have to perform, and the number of different shortest sequences of operations. Since the second number might be large, print its remainder modulo $$$998244353$$$. Note In the first test case of the example, the shortest sequences of operations are: $$$[2]$$$ (delete the $$$2$$$-nd character); $$$[3]$$$ (delete the $$$3$$$-rd character). In the second test case of the example, the shortest sequences of operations are: $$$[2, 1]$$$ (delete the $$$2$$$-nd character, then delete the $$$1$$$-st character); $$$[2, 2]$$$; $$$[1, 1]$$$; $$$[1, 2]$$$; $$$[3, 1]$$$; $$$[3, 2]$$$. In the third test case of the example, the only shortest sequence of operations is $$$[]$$$ (empty sequence).
1,300
false
true
false
true
false
false
false
false
false
false
1,008
1919F2
This is the hard version of the problem. The only difference between the two versions is the constraint on $$$c_i$$$ and $$$z$$$. You can make hacks only if both versions of the problem are solved. There are three arrays $$$a$$$, $$$b$$$ and $$$c$$$. $$$a$$$ and $$$b$$$ have length $$$n$$$ and $$$c$$$ has length $$$n-1$$$. Let $$$W(a,b,c)$$$ denote the liters of wine created from the following process. Create $$$n$$$ water towers. The $$$i$$$-th water tower initially has $$$a_i$$$ liters of water and has a wizard with power $$$b_i$$$ in front of it. Furthermore, for each $$$1 le i le n - 1$$$, there is a valve connecting water tower $$$i$$$ to $$$i + 1$$$ with capacity $$$c_i$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$ in this order, the following happens: 1. The wizard in front of water tower $$$i$$$ removes at most $$$b_i$$$ liters of water from the tower and turns the removed water into wine. 2. If $$$i eq n$$$, at most $$$c_i$$$ liters of the remaining water left in water tower $$$i$$$ flows through the valve into water tower $$$i + 1$$$. There are $$$q$$$ updates. In each update, you will be given integers $$$p$$$, $$$x$$$, $$$y$$$ and $$$z$$$ and you will update $$$a_p := x$$$, $$$b_p := y$$$ and $$$c_p := z$$$. After each update, find the value of $$$W(a,b,c)$$$. Note that previous updates to arrays $$$a$$$, $$$b$$$ and $$$c$$$ persist throughout future updates. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 5cdot 10^5$$$, $$$1 le q le 5cdot 10^5$$$)xa0— the number of water towers and the number of updates. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— the number of liters of water in water tower $$$i$$$. The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$0 le b_i le 10^9$$$)xa0— the power of the wizard in front of water tower $$$i$$$. The fourth line contains $$$n - 1$$$ integers $$$c_1, c_2, ldots, c_{n - 1}$$$ ($$$0 le c_i color{red}{le} 10^{18}$$$)xa0— the capacity of the pipe connecting water tower $$$i$$$ to $$$i + 1$$$. Each of the next $$$q$$$ lines contains four integers $$$p$$$, $$$x$$$, $$$y$$$ and $$$z$$$ ($$$1 le p le n$$$, $$$0 le x, y le 10^9$$$, $$$0 le z color{red}{le} 10^{18}$$$)xa0— the updates done to arrays $$$a$$$, $$$b$$$ and $$$c$$$. Note that $$$c_n$$$ does not exist, so the value of $$$z$$$ does not matter when $$$p = n$$$. Output Print $$$q$$$ lines, each line containing a single integer representing $$$W(a, b, c)$$$ after each update. Examples Input 4 3 3 3 3 3 1 4 2 8 5 2 1 4 3 8 1000000000 2 5 1 1 3 0 0 0 Input 5 5 10 3 8 9 2 3 4 10 8 1 6 5 9 2 5 4 9 1 1 1 1 1 2 7 4 8 4 1 1 1 1 8 3 3 Note The first update does not make any modifications to the arrays. When $$$i = 1$$$, there are $$$3$$$ liters of water in tower 1 and $$$1$$$ liter of water is turned into wine. The remaining $$$2$$$ liters of water flow into tower 2. When $$$i = 2$$$, there are $$$5$$$ liters of water in tower 2 and $$$4$$$ liters of water is turned into wine. The remaining $$$1$$$ liter of water flows into tower 3. When $$$i = 3$$$, there are $$$4$$$ liters of water in tower 3 and $$$2$$$ liters of water is turned into wine. Even though there are $$$2$$$ liters of water remaining, only $$$1$$$ liter of water can flow into tower 4. When $$$i = 4$$$, there are $$$4$$$ liters of water in tower 4. All $$$4$$$ liters of water are turned into wine. Hence, $$$W(a,b,c)=1 + 4 + 2 + 4 = 11$$$ after the first update. The second update modifies the arrays to $$$a = [3, 5, 3, 3]$$$, $$$b = [1, 1, 2, 8]$$$, and $$$c = [5, 1, 1]$$$. When $$$i = 1$$$, there are $$$3$$$ liters of water in tower 1 and $$$1$$$ liter of water is turned into wine. The remaining $$$2$$$ liters of water flow into tower 2. When $$$i = 2$$$, there are $$$7$$$ liters of water in tower 2 and $$$1$$$ liter of water is turned into wine. Even though there are $$$6$$$ liters of water remaining, only $$$1$$$ liter of water can flow to tower 3. When $$$i = 3$$$, there are $$$4$$$ liters of water in tower 3 and $$$2$$$ liters of water is turned into wine. Even though there are $$$2$$$ liters of water remaining, only $$$1$$$ liter of water can flow into tower 4. When $$$i = 4$$$, there are $$$4$$$ liters of water in tower 4. All $$$4$$$ liters of water are turned into wine. Hence, $$$W(a,b,c)=1 + 1 + 2 + 4 = 8$$$ after the second update. The third update modifies the arrays to $$$a = [3, 5, 0, 3]$$$, $$$b = [1, 1, 0, 8]$$$, and $$$c = [5, 1, 0]$$$. When $$$i = 1$$$, there are $$$3$$$ liters of water in tower 1 and $$$1$$$ liter of water is turned into wine. The remaining $$$2$$$ liters of water flow into tower 2. When $$$i = 2$$$, there are $$$7$$$ liters of water in tower 2 and $$$1$$$ liter of water is turned into wine. Even though there are $$$6$$$ liters of water remaining, only $$$1$$$ liter of water can flow to tower 3. When $$$i = 3$$$, there is $$$1$$$ liter of water in tower 3 and $$$0$$$ liters of water is turned into wine. Even though there is $$$1$$$ liter of water remaining, no water can flow to tower 4. When $$$i = 4$$$, there are $$$3$$$ liters of water in tower 4. All $$$3$$$ liters of water are turned into wine. Hence, $$$W(a,b,c)=1 + 1 + 0 + 3 = 5$$$ after the third update.
2,800
false
true
false
true
true
false
false
false
false
false
775
1551D2
The only difference between this problem and D1 is that you don't have to provide the way to construct the answer in D1, but you have to do it in this problem. There's a table of $$$n imes m$$$ cells ($$$n$$$ rows and $$$m$$$ columns). The value of $$$n cdot m$$$ is even. A domino is a figure that consists of two cells having a common side. It may be horizontal (one of the cells is to the right of the other) or vertical (one of the cells is above the other). You need to place $$$frac{nm}{2}$$$ dominoes on the table so that exactly $$$k$$$ of them are horizontal and all the other dominoes are vertical. The dominoes cannot overlap and must fill the whole table. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of a single line. The line contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 le n,m le 100$$$, $$$0 le k le frac{nm}{2}$$$, $$$n cdot m$$$ is even) — the count of rows, columns and horizontal dominoes, respectively. Output For each test case: print "NO" if it's not possible to place the dominoes on the table in the described way; otherwise, print "YES" on a separate line, then print $$$n$$$ lines so that each of them contains $$$m$$$ lowercase letters of the Latin alphabet — the layout of the dominoes on the table. Each cell of the table must be marked by the letter so that for every two cells having a common side, they are marked by the same letters if and only if they are occupied by the same domino. I.e. both cells of the same domino must be marked with the same letter, but two dominoes that share a side must be marked with different letters. If there are multiple solutions, print any of them. Example Input 8 4 4 2 2 3 0 3 2 3 1 2 0 2 4 2 5 2 2 2 17 16 2 1 1 Output YES accx aegx bega bdda YES aha aha YES zz aa zz NO YES aaza bbza NO YES bbaabbaabbaabbaay ddccddccddccddccy NO
2,100
true
false
true
false
false
true
false
false
false
false
2,887
11E
Jack has become a soldier now. Unfortunately, he has trouble with the drill. Instead of marching beginning with the left foot and then changing legs with each step, as ordered, he keeps repeating a sequence of steps, in which he sometimes makes the wrong steps or — horror of horrors! — stops for a while. For example, if Jack uses the sequence 'right, left, break', when the sergeant yells: 'Left! Right! Left! Right! Left! Right!', Jack first makes a step with the right foot, then one with the left foot, then he is confused and stops for a moment, then again - this time according to the order - starts with the right foot, then uses the left foot, then - to the sergeant's irritation - he stops to catch his breath, to incorrectly start with the right foot again... Marching this way, Jack will make the step that he is supposed to in the given moment in only one third of cases. When the officers convinced him he should do something about it, Jack decided to modify the basic sequence of steps that he repeats. However, in order not to get too tired, he has decided that the only thing he'll do is adding any number of breaks in any positions of the original sequence (a break corresponds to stopping for the duration of one step). Of course, Jack can't make a step on the same foot twice in a row, if there is no pause between these steps. It is, however, not impossible that the sequence of steps he used so far is incorrect (it would explain a lot, actually). Help Private Jack! Given the sequence of steps he keeps repeating, calculate the maximal percentage of time that he can spend marching correctly after adding some breaks to his scheme. Input The first line of input contains a sequence consisting only of characters 'L', 'R' and 'X', where 'L' corresponds to a step with the left foot, 'R' — with the right foot, and 'X' — to a break. The length of the sequence will not exceed 106. Note In the second example, if we add two breaks to receive LXXRXR, Jack will march: LXXRXRLXXRXRL... instead of LRLRLRLRLRLRL... and will make the correct step in half the cases. If we didn't add any breaks, the sequence would be incorrect — Jack can't step on his right foot twice in a row.
2,800
false
true
false
true
false
false
false
true
false
false
9,936
1009E
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car. The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is $$$n$$$ km. Let's say that Moscow is situated at the point with coordinate $$$0$$$ km, and Saratov — at coordinate $$$n$$$ km. Driving for a long time may be really difficult. Formally, if Leha has already covered $$$i$$$ kilometers since he stopped to have a rest, he considers the difficulty of covering $$$(i + 1)$$$-th kilometer as $$$a_{i + 1}$$$. It is guaranteed that for every $$$i in [1, n - 1]$$$ $$$a_i le a_{i + 1}$$$. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey. Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from $$$1$$$ to $$$n - 1$$$ may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty $$$a_1$$$, the kilometer after it — difficulty $$$a_2$$$, and so on. For example, if $$$n = 5$$$ and there is a rest site in coordinate $$$2$$$, the difficulty of journey will be $$$2a_1 + 2a_2 + a_3$$$: the first kilometer will have difficulty $$$a_1$$$, the second one — $$$a_2$$$, then Leha will have a rest, and the third kilometer will have difficulty $$$a_1$$$, the fourth — $$$a_2$$$, and the last one — $$$a_3$$$. Another example: if $$$n = 7$$$ and there are rest sites in coordinates $$$1$$$ and $$$5$$$, the difficulty of Leha's journey is $$$3a_1 + 2a_2 + a_3 + a_4$$$. Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are $$$2^{n - 1}$$$ different distributions of rest sites (two distributions are different if there exists some point $$$x$$$ such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate $$$p$$$ — the expected value of difficulty of his journey. Obviously, $$$p cdot 2^{n - 1}$$$ is an integer number. You have to calculate it modulo $$$998244353$$$. Input The first line contains one number $$$n$$$ ($$$1 le n le 10^6$$$) — the distance from Moscow to Saratov. The second line contains $$$n$$$ integer numbers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_1 le a_2 le dots le a_n le 10^6$$$), where $$$a_i$$$ is the difficulty of $$$i$$$-th kilometer after Leha has rested.
2,000
true
false
false
false
false
false
false
false
false
false
5,638
369A
Valera is a lazy student. He has _m_ clean bowls and _k_ clean plates. Valera has made an eating plan for the next _n_ days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl. We know that Valera can cook only two types of dishes. He can eat dishes of the first type from bowls and dishes of the second type from either bowls or plates. When Valera finishes eating, he leaves a dirty plate/bowl behind. His life philosophy doesn't let him eat from dirty kitchenware. So sometimes he needs to wash his plate/bowl before eating. Find the minimum number of times Valera will need to wash a plate/bowl, if he acts optimally. Input The first line of the input contains three integers _n_, _m_, _k_ (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u20091000)xa0— the number of the planned days, the number of clean bowls and the number of clean plates. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20092). If _a__i_ equals one, then on day _i_ Valera will eat a first type dish. If _a__i_ equals two, then on day _i_ Valera will eat a second type dish. Output Print a single integer — the minimum number of times Valera will need to wash a plate/bowl. Examples Input 8 2 2 1 2 1 2 1 2 1 2 Note In the first sample Valera will wash a bowl only on the third day, so the answer is one. In the second sample, Valera will have the first type of the dish during all four days, and since there are only three bowls, he will wash a bowl exactly once. In the third sample, Valera will have the second type of dish for all three days, and as they can be eaten from either a plate or a bowl, he will never need to wash a plate/bowl.
900
false
true
true
false
false
false
false
false
false
false
8,358
1942D
Elsie is learning how to paint. She has a canvas of $$$n$$$ cells numbered from $$$1$$$ to $$$n$$$ and can paint any (potentially empty) subset of cells. Elsie has a 2D array $$$a$$$ which she will use to evaluate paintings. Let the maximal contiguous intervals of painted cells in a painting be $$$[l_1,r_1],[l_2,r_2],ldots,[l_x,r_x]$$$. The beauty of the painting is the sum of $$$a_{l_i,r_i}$$$ over all $$$1 le i le x$$$. In the image above, the maximal contiguous intervals of painted cells are $$$[2,4],[6,6],[8,9]$$$ and the beauty of the painting is $$$a_{2,4}+a_{6,6}+a_{8,9}$$$. There are $$$2^n$$$ ways to paint the strip. Help Elsie find the $$$k$$$ largest possible values of the beauty of a painting she can obtain, among all these ways. Note that these $$$k$$$ values do not necessarily have to be distinct. It is guaranteed that there are at least $$$k$$$ different ways to paint the canvas. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of test cases. The first line of each test case contains $$$2$$$ integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 10^3$$$, $$$1 leq k leq min(2^n, 5 cdot 10^3)$$$)xa0— the number of cells and the number of largest values of the beauty of a painting you must find. The next $$$n$$$ lines of each test case describe $$$a$$$ where the $$$i$$$-th of which contains $$$n-i+1$$$ integers $$$a_{i,i},a_{i,i+1},ldots,a_{i,n}$$$ ($$$-10^6 leq a_{i,j} leq 10^6$$$). It is guaranteed the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$ and the sum of $$$k$$$ over all test cases does not exceed $$$5 cdot 10^3$$$. Output For each test case, output $$$k$$$ integers in one line: the $$$i$$$-th of them must represent the $$$i$$$-th largest value of the beauty of a painting Elsie can obtain. Example Input 4 1 2 -5 2 4 2 -3 -1 3 8 2 4 3 1 3 5 6 20 0 -6 -3 0 -6 -2 -7 -5 -2 -3 -4 7 0 -9 -4 2 -1 1 1 -2 -6 Output 0 -5 2 0 -1 -3 7 5 4 3 3 2 1 0 8 8 7 7 5 5 2 2 1 1 1 1 1 1 0 0 0 0 0 -1 Note In the first test case, Elsie can either paint the only cell or not paint it. If she paints the only cell, the beauty of the painting is $$$-5$$$. If she chooses not to paint it, the beauty of the painting is $$$0$$$. Thus, the largest beauty she can obtain is $$$0$$$ and the second largest beauty she can obtain is $$$-5$$$. Below is an illustration of the third test case.
2,100
false
true
true
true
true
false
false
true
true
false
628
39D
Problem - 39D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *1100 No tag edit access → Contest materials ") : Tutorial") : Problem K Tutorial") and (1,u20091,u20091). Two flies live on the planet. At the moment they are sitting on two different vertices of the cubical planet. Your task is to determine whether they see each other or not. The flies see each other when the vertices they occupy lie on the same face of the cube. Input The first line contains three space-separated integers (0 or 1) — the coordinates of the first fly, the second line analogously contains the coordinates of the second fly. Output Output "YES" (without quotes) if the flies see each other. Otherwise, output "NO". Examples Input 0 0 0 0 1 0 Output YES Input 1 1 0 0 1 0 Output YES Input 0 0 0 1 1 1 Output NO
1,100
true
false
false
false
false
false
false
false
false
false
9,797
418D
The Finals of the "Russian Code Cup" 2214 will be held in _n_ hotels. Two hotels (let's assume that they are the main hotels), will host all sorts of events, and the remaining hotels will accommodate the participants. The hotels are connected by _n_u2009-u20091 roads, you can get from any hotel to any other one. The organizers wonder what is the minimum time all the participants need to get to the main hotels, if each participant goes to the main hotel that is nearest to him and moving between two hotels connected by a road takes one unit of time. The hosts consider various options for the location of the main hotels. For each option help the organizers to find minimal time. Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009100000)xa0— the number of hotels. The next _n_u2009-u20091 lines contain two integers each xa0— the numbers of the hotels that have a road between them. Consider hotels are numbered from 1 to _n_. The next line contains an integer _m_ (1u2009≤u2009_m_u2009≤u2009100000)xa0— the number of queries. The following _m_ lines contains two distinct integers eachxa0— the numbers of the hotels we assume to be the main. Output For each request of the organizers print a single integerxa0— the time that all participants need to reach the main hotels. Examples Input 4 1 4 1 2 2 3 3 1 4 1 3 2 3
2,800
false
false
false
false
true
false
false
false
false
true
8,152
1508E
Yuu Koito and Touko Nanami are newlyweds! On the wedding day, Yuu gifted Touko a directed tree with $$$n$$$ nodes and rooted at $$$1$$$, and a labeling $$$a$$$ which is some DFS order of the tree. Every edge in this tree is directed away from the root. After calling dfs(1) the following algorithm returns $$$a$$$ as a DFS order of a tree rooted at $$$1$$$ : order := 0 a := array of length n function dfs(u): order := order + 1 a[u] := order for all v such that there is a directed edge (u -> v): dfs(v) Note that there may be different DFS orders for a given tree. Touko likes the present so much she decided to play with it! On each day following the wedding day, Touko performs this procedure once: Among all directed edges $$$u ightarrow v$$$ such that $$$a_u < a_v$$$, select the edge $$$u' ightarrow v'$$$ with the lexicographically smallest pair $$$(a_{u'}, a_{v'})$$$. Swap $$$a_{u'}$$$ and $$$a_{v'}$$$. Days have passed since their wedding, and Touko has somehow forgotten which date the wedding was and what was the original labeling $$$a$$$! Fearing that Yuu might get angry, Touko decided to ask you to derive these two pieces of information using the current labeling. Being her good friend, you need to find the number of days that have passed since the wedding, and the original labeling of the tree. However, there is a chance that Touko might have messed up her procedures, which result in the current labeling being impossible to obtain from some original labeling; in that case, please inform Touko as well. Input The first line of the input contains an integer $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$) — the number of nodes on the tree. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le n$$$, all $$$a_i$$$ are distinct) — the current labeling of the tree. Each of the next $$$n - 1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$), describing an directed edge from $$$u_i$$$ to $$$v_i$$$. The edges form a directed tree rooted at $$$1$$$. Output If the current labeling is impossible to arrive at from any DFS order, print NO. Else, on the first line, print YES. On the second line, print a single integer denoting the number of days since the wedding. On the third line, print $$$n$$$ numbers space-separated denoting the original labeling of the tree. If there are multiple correct outputs, print any. This means: you are allowed to output any pair (DFS order, number of days), such that we get the current configuration from the DFS order you provided in exactly the number of days you provided. Examples Input 7 4 5 2 1 7 6 3 1 5 7 6 1 2 2 7 3 4 1 3 Output YES 5 1 4 2 3 7 6 5 Input 7 7 6 5 3 1 4 2 4 3 2 5 3 7 1 4 7 2 2 6 Note The following animation showcases the first sample test case. The white label inside the node represents the index of the node $$$i$$$, while the boxed orange label represents the value $$$a_i$$$.
3,100
false
false
false
false
true
true
true
false
true
false
3,132
110B
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya recently learned to determine whether a string of lowercase Latin letters is lucky. For each individual letter all its positions in the string are written out in the increasing order. This results in 26 lists of numbers; some of them can be empty. A string is considered lucky if and only if in each list the absolute difference of any two adjacent numbers is a lucky number. For example, let's consider string "zbcdzefdzc". The lists of positions of equal letters are: b: 2 c: 3,u200910 d: 4,u20098 e: 6 f: 7 z: 1,u20095,u20099 Lists of positions of letters a, g, h, ..., y are empty. This string is lucky as all differences are lucky numbers. For letters z: 5u2009-u20091u2009=u20094, 9u2009-u20095u2009=u20094, for letters c: 10u2009-u20093u2009=u20097, for letters d: 8u2009-u20094u2009=u20094. Note that if some letter occurs only once in a string, it doesn't influence the string's luckiness after building the lists of positions of equal letters. The string where all the letters are distinct is considered lucky. Find the lexicographically minimal lucky string whose length equals _n_. Note The lexical comparison of strings is performed by the < operator in modern programming languages. String _a_ is lexicographically less than string _b_ if exists such _i_ (1u2009≤u2009_i_u2009≤u2009_n_), that _a__i_u2009<u2009_b__i_, and for any _j_ (1u2009≤u2009_j_u2009<u2009_i_) _a__j_u2009=u2009_b__j_.
1,100
false
false
false
false
false
true
false
false
false
false
9,443
1610C
Keshi is throwing a party and he wants everybody in the party to be happy. He has $$$n$$$ friends. His $$$i$$$-th friend has $$$i$$$ dollars. If you invite the $$$i$$$-th friend to the party, he will be happy only if at most $$$a_i$$$ people in the party are strictly richer than him and at most $$$b_i$$$ people are strictly poorer than him. Keshi wants to invite as many people as possible. Find the maximum number of people he can invite to the party so that every invited person would be happy. Input The first line contains a single integer $$$t$$$ $$$(1le tle 10^4)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1le nle 2 cdot 10^5)$$$ — the number of Keshi's friends. The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ $$$(0 le a_i, b_i < n)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case print the maximum number of people Keshi can invite. Example Input 3 3 1 2 2 1 1 1 2 0 0 0 1 2 1 0 0 1 Note In the first test case, he invites the first and the second person. If he invites all of them, the third person won't be happy because there will be more than $$$1$$$ person poorer than him.
1,600
false
true
false
false
false
false
false
true
false
false
2,598
1770C
Joi has an array $$$a$$$ of $$$n$$$ positive integers. Koxia wants you to determine whether there exists a positive integer $$$x > 0$$$ such that $$$gcd(a_i+x,a_j+x)=1$$$ for all $$$1 leq i < j leq n$$$. Here $$$gcd(y, z)$$$ denotes the — the number of test cases. The description of test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 leq n leq 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq {10}^{18}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. Output For each test case, output "YES" (without quotes) if there exists a positive integer $$$x$$$ such that $$$gcd(a_i+x,a_j+x)=1$$$ for all $$$1 leq i < j leq n$$$, and "NO" (without quotes) otherwise. You 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. Note In the first test case, we can set $$$x = 4$$$. This is valid because: When $$$i=1$$$ and $$$j=2$$$, $$$gcd(a_i+x,a_j+x)=gcd(5+4,7+4)=gcd(9,11)=1$$$. When $$$i=1$$$ and $$$j=3$$$, $$$gcd(a_i+x,a_j+x)=gcd(5+4,10+4)=gcd(9,14)=1$$$. When $$$i=2$$$ and $$$j=3$$$, $$$gcd(a_i+x,a_j+x)=gcd(7+4,10+4)=gcd(11,14)=1$$$. In the second test case, any choice of $$$x$$$ makes $$$gcd(a_1 + x, a_2 + x) = gcd(3+x,3+x)=3+x$$$. Therefore, no such $$$x$$$ exists.
1,700
true
false
false
false
false
false
true
false
false
false
1,675
265B
Problem - 265B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation *1000 No tag edit access → Contest materials time limit per test 2 seconds memory limit per test 256 megabytes input stdin output stdout Squirrel Liss loves nuts. There are _n_ trees (numbered 1 to _n_ from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree _i_ is _h__i_. Liss wants to eat all nuts. Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions: Walk up or down one unit on a tree. Eat a nut on the top of the current tree. Jump to the next tree. In this action the height of Liss doesn't change. More formally, when Liss is at height _h_ of the tree _i_ (1u2009≤u2009_i_u2009≤u2009_n_u2009-u20091), she jumps to height _h_ of the tree _i_u2009+u20091. This action can't be performed if _h_u2009>u2009_h__i_u2009+u20091. Compute the minimal time (in seconds) required to eat all nuts. Input The first line contains an integer _n_ (1u2009u2009≤u2009u2009_n_u2009≤u2009105) — the number of trees. Next _n_ lines contains the height of trees: _i_-th line contains an integer _h__i_ (1u2009≤u2009_h__i_u2009≤u2009104) — the height of the tree with the number _i_. Output Print a single integer — the minimal time required to eat all nuts in seconds. Examples Input 2 1 2 Output 5 Input 5 2 1 2 1 1 Output 14
1,000
false
true
true
false
false
false
false
false
false
false
8,772
1337B
Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. xa0The dragon has a hit point of $$$x$$$ initially. When its hit point goes to $$$0$$$ or under $$$0$$$, it will be defeated. In order to defeat the dragon, Kana can cast the two following types of spells. Void AbsorptionAssume that the dragon's current hit point is $$$h$$$, after casting this spell its hit point will become $$$leftlfloor frac{h}{2} ight floor + 10$$$. Here $$$leftlfloor frac{h}{2} ight floor$$$ denotes $$$h$$$ divided by two, rounded down. Lightning StrikeThis spell will decrease the dragon's hit point by $$$10$$$. Assume that the dragon's current hit point is $$$h$$$, after casting this spell its hit point will be lowered to $$$h-10$$$. Due to some reasons Kana can only cast no more than $$$n$$$ Void Absorptions and $$$m$$$ Lightning Strikes. She can cast the spells in any order and doesn't have to cast all the spells. Kana isn't good at math, so you are going to help her to find out whether it is possible to defeat the dragon. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) xa0— the number of test cases. The next $$$t$$$ lines describe test cases. For each test case the only line contains three integers $$$x$$$, $$$n$$$, $$$m$$$ ($$$1le x le 10^5$$$, $$$0le n,mle30$$$) xa0— the dragon's intitial hit point, the maximum number of Void Absorptions and Lightning Strikes Kana can cast respectively. Output If it is possible to defeat the dragon, print "YES" (without quotes). Otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower). Example Input 7 100 3 4 189 3 4 64 2 3 63 2 3 30 27 7 10 9 1 69117 21 2 Output YES NO NO YES YES YES YES Note One possible casting sequence of the first test case is shown below: Void Absorption $$$leftlfloor frac{100}{2} ight floor + 10=60$$$. Lightning Strike $$$60-10=50$$$. Void Absorption $$$leftlfloor frac{50}{2} ight floor + 10=35$$$. Void Absorption $$$leftlfloor frac{35}{2} ight floor + 10=27$$$. Lightning Strike $$$27-10=17$$$. Lightning Strike $$$17-10=7$$$. Lightning Strike $$$7-10=-3$$$.
900
true
true
true
false
false
false
false
false
false
false
4,015
2004E
Problem - 2004E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force games math number theory *2100 No tag edit access → Contest materials — Video Tutorial") ") . Alice goes first. Determine who will win. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of two lines: the first line contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$); the second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^7$$$). Additional constraint on the input: the sum of $$$n$$$ across all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output Alice if Alice wins, or Bob if Bob wins. Example Input 3 3 3 2 9 4 3 3 6 1 5 1 2 3 4 5 Output Bob Alice Bob
2,100
true
false
false
false
false
false
true
false
false
false
227
1910C
The first location in the brand new critically acclaimed fantasy action RPG "Ancient Staff" is a poisonous swamp. The swamp has some lily pads growing in it. It can be represented as a $$$2 imes n$$$ grid ($$$2$$$ rows and $$$n$$$ columns), where each cell is either empty or has a lily pad in it. There are exactly $$$n$$$ lily pads in a swampxa0— one in each column. A frog is sitting on top of every lily pad. In one move, every frog must jump to an adjacent by a side cell. After the move, no frog can be outside the grid and no two frogs can share the same lily pad. Two frogs from adjacent cells can't jump towards each other (i.e. swap cells). If a frog jumps to a lily pad, it survives. Otherwise, it falls into a poisonous swamp and gets devoured by an eldritch creature living on its bottom. You can choose the direction each frogs jumps at. Determine the maximum number of frogs that can survive after one move. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of the testcase contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of columns in a swamp grid. Each of the following two lines contains a description of a row of a swampxa0— a string, consisting of exactly $$$n$$$ characters. Each character is either a dot ('.'), denoting a swamp cell, or an asterisk ('*'), denoting a lily pad with a frog. In each column, there is exactly one dot and exactly one asterisk. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the maximum number of frogs that can survive after one move, if you choose the direction each frogs jumps at. Example Input 3 5 *..** .**.. 1 * . 3 ... *** Note The $$$i$$$-th frog is the frog on the lily pad in the $$$i$$$-th column. In the first testcase: the first frog can't survive because there's no adjacent lily pad; the second and the third frogs can jump right and up, respectively,xa0— there's no way to save them both at the same time; the fourth and the fifth frogs can jump left and left, respectively,xa0— there's no way to save them both at the same time; note, however, that the third and the fourth frogs will end up in the same cell in the swamp, which is not prohibited.
1,600
false
false
true
false
false
false
false
false
false
false
845
928B
There are times you recall a good old friend and everything you've come through together. Luckily there are social networksxa0— they store all your message history making it easy to know what you argued over 10 years ago. More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to _n_ where _n_ is the total number of messages in the chat. Each message might contain a link to an earlier message which it is a reply to. When opening a message _x_ or getting a link to it, the dialogue is shown in such a way that _k_ previous messages, message _x_ and _k_ next messages are visible (with respect to message _x_). In case there are less than _k_ messages somewhere, they are yet all shown. Digging deep into your message history, you always read all visible messages and then go by the link in the current message _x_ (if there is one) and continue reading in the same manner. Determine the number of messages you'll read if your start from message number _t_ for all _t_ from 1 to _n_. Calculate these numbers independently. If you start with message _x_, the initial configuration is _x_ itself, _k_ previous and _k_ next messages. Messages read multiple times are considered as one. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105, 0u2009≤u2009_k_u2009≤u2009_n_) — the total amount of messages and the number of previous and next messages visible. The second line features a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009<u2009_i_), where _a__i_ denotes the _i_-th message link destination or zero, if there's no link from _i_. All messages are listed in chronological order. It's guaranteed that the link from message _x_ goes to message with number strictly less than _x_. Output Print _n_ integers with _i_-th denoting the number of distinct messages you can read starting from message _i_ and traversing the links while possible. Examples Input 10 1 0 1 0 3 4 5 2 3 7 0 Output 2 3 3 4 5 6 6 6 8 2 Note Consider _i_u2009=u20096 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go. In the second sample case _i_u2009=u20096 gives you messages 5,u20096,u20097 since _k_u2009=u20091, then 4,u20095,u20096, then 2,u20093,u20094 and then the link sequence breaks. The number of distinct messages here is equal to 6.
1,400
false
false
false
true
false
false
false
false
false
false
5,993
1392A
Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret! A password is an array $$$a$$$ of $$$n$$$ positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index $$$i$$$ such that $$$1 leq i < n$$$ and $$$a_{i} eq a_{i+1}$$$, delete both $$$a_i$$$ and $$$a_{i+1}$$$ from the array and put $$$a_{i}+a_{i+1}$$$ in their place. For example, for array $$$[7, 4, 3, 7]$$$ you can choose $$$i = 2$$$ and the array will become $$$[7, 4+3, 7] = [7, 7, 7]$$$. Note that in this array you can't apply this operation anymore. Notice that one operation will decrease the size of the password by $$$1$$$. What is the shortest possible length of the password after some number (possibly $$$0$$$) of operations? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the length of the password. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},dots,a_{n}$$$ ($$$1 leq a_{i} leq 10^9$$$)xa0— the initial contents of your password. The sum of $$$n$$$ over all test cases will not exceed $$$2 cdot 10^5$$$. Output For each password, print one integer: the shortest possible length of the password after some number of operations. Example Input 2 4 2 1 3 1 2 420 420 Note In the first test case, you can do the following to achieve a length of $$$1$$$: Pick $$$i=2$$$ to get $$$[2, 4, 1]$$$ Pick $$$i=1$$$ to get $$$[6, 1]$$$ Pick $$$i=1$$$ to get $$$[7]$$$ In the second test case, you can't perform any operations because there is no valid $$$i$$$ that satisfies the requirements mentioned above.
800
true
true
false
false
false
false
false
false
false
false
3,716
64H
Problem - 64H - Codeforces =============== xa0 and by the name lexicographically in the case of a tie. Places are numerated from 1. If more than one participant has some score, all of them share the places and you should output something like "12-14 john". Please, look into the samples for clarification. Input The first line contains _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of rows in the table. Following _n_ lines contain the given table. Each line has the form "name score", where "name" is a sequence of lowercase Latin letters, and "score" — is an integer number between 0 and 1000, inclusive. All the names are distinct. The length of each name is between 1 and 10 characters, inclusive. There is single space between the name and the score in each line. Output Print the required table. Look at the sample outputs for clarifications. Examples Input 5 vasya 10 ted 11 petya 10 katya 33 mike 44 Output 1 mike 2 katya 3 ted 4-5 petya 4-5 vasya Input 3 a 1 b 13 c 1 Output 1 b 2-3 a 2-3 c
2,300
false
false
false
false
false
false
false
false
true
false
9,648
1688B
She is skilled in all kinds of magics, and is keen on inventing new one. —Perfect Memento in Strict Sense Patchouli is making a magical talisman. She initially has $$$n$$$ magical tokens. Their magical power can be represented with positive integers $$$a_1, a_2, ldots, a_n$$$. Patchouli may perform the following two operations on the tokens. Fusion: Patchouli chooses two tokens, removes them, and creates a new token with magical power equal to the sum of the two chosen tokens. Reduction: Patchouli chooses a token with an even value of magical power $$$x$$$, removes it and creates a new token with magical power equal to $$$frac{x}{2}$$$. Tokens are more effective when their magical powers are odd values. Please help Patchouli to find the minimum number of operations she needs to make magical powers of all tokens odd values. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$) — the number of test cases. The description of the test cases follows. For each test case, the first line contains one integer $$$n$$$ ($$$1 leq nleq 2cdot 10^5$$$) — the initial number of tokens. The second line contains $$$n$$$ intergers $$$a_1,a_2,ldots,a_n$$$ ($$$1 leq a_i leq 10^9$$$) — the initial magical power of the $$$n$$$ tokens. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integer — the minimum number of operations Patchouli needs to make all tokens have an odd value of magical power. It can be shown that under such restrictions the required sequence of operations exists. Example Input 4 2 1 9 3 1 1 2 3 2 4 8 3 1049600 33792 1280 Note Test case 1: $$$a$$$ consists solely of odd numbers initially. Test case 2: Choose the tokens with magical power of $$$1$$$ and $$$2$$$ and perform Fusion. Now $$$a=[1,3]$$$, both are odd numbers. Test case 3: Choose the tokens with magical power of $$$2$$$ and $$$8$$$ and perform Fusion. Now $$$a=[4,10]$$$. Choose the token with magical power of $$$10$$$ and perform Reduction. Now $$$a=[4,5]$$$. Choose the tokens with magical power of $$$4$$$ and $$$5$$$ and perform Fusion. Now $$$a=[9]$$$, and $$$9$$$ is an odd number. It can be shown that you can not make all the magical powers odd numbers in less than $$$3$$$ moves, so the answer is $$$3$$$.
800
false
true
false
false
false
true
false
false
true
false
2,150
51C
The New Vasjuki village is stretched along the motorway and that's why every house on it is characterized by its shift relative to some fixed point — the _x__i_ coordinate. The village consists of _n_ houses, the _i_-th house is located in the point with coordinates of _x__i_. TELE3, a cellular communication provider planned to locate three base stations so as to provide every house in the village with cellular communication. The base station having power _d_ located in the point _t_ provides with communication all the houses on the segment [_t_u2009-u2009_d_,u2009_t_u2009+u2009_d_] (including boundaries). To simplify the integration (and simply not to mix anything up) all the three stations are planned to possess the equal power of _d_. Which minimal value of _d_ is enough to provide all the houses in the village with cellular communication. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) which represents the number of houses in the village. The second line contains the coordinates of houses — the sequence _x_1,u2009_x_2,u2009...,u2009_x__n_ of integer numbers (1u2009≤u2009_x__i_u2009≤u2009109). It is possible that two or more houses are located on one point. The coordinates are given in a arbitrary order. Output Print the required minimal power _d_. In the second line print three numbers — the possible coordinates of the base stations' location. Print the coordinates with 6 digits after the decimal point. The positions of the stations can be any from 0 to 2·109 inclusively. It is accepted for the base stations to have matching coordinates. If there are many solutions, print any of them. Examples Output 0.500000 1.500000 2.500000 3.500000 Output 0 10.000000 20.000000 30.000000 Input 5 10003 10004 10001 10002 1 Output 0.500000 1.000000 10001.500000 10003.500000
1,800
false
true
false
false
false
false
false
true
false
false
9,717
100G
The famous singer, Aryo, is going to publish a new album of his great work! Unfortunately these days, there are many albums, Aryo wants to choose a new name for his album, a name that has not been used or at least has not been used recently. He has a list of all used album names together with the year the albums were published. He also has a list of suitable names for his album. If he finds a suitable name which has not been used before, he'll use it. Otherwise he will use the name which was used as long ago as possible. If two such names are found (that haven't been used or were used at the same year), he uses the name that is alphabetically latest. Help him name his album. Input The first line contains a single integer _n_ (0u2009≤u2009_n_u2009≤u2009105), the number of used names. The following _n_ lines each contain a string (the album name) and an integer (the year album was published). Album names are made of lowercase Latin letters and contain at most 14 letters. The year is in range [1900,u20092011]. The following line contains a single integer _m_ (1u2009≤u2009_m_u2009≤u2009104), the number of suitable album names. The following _m_ lines each contain a string — a suitable name. It contains at most 14 lowercase Latin letters. All album names and suitable names are non-empty. Output Write a single string. The name of the new album. Examples Input 3 eyesonme 2008 anewdayhascome 2002 oneheart 2003 2 oneheart bienbien Input 2 nasimevasl 2003 basetareha 2006 2 nasimevasl basetareha
1,800
false
false
true
false
true
false
false
false
false
false
9,483
958A1
The stardate is 1977 and the science and art of detecting Death Stars is in its infancy. Princess Heidi has received information about the stars in the nearby solar system from the Rebel spies and now, to help her identify the exact location of the Death Star, she needs to know whether this information is correct. Two rebel spies have provided her with the maps of the solar system. Each map is an _N_u2009×u2009_N_ grid, where each cell is either occupied by a star or empty. To see whether the information is correct, Heidi needs to know whether the two maps are of the same solar system, or if possibly one of the spies is actually an Empire double agent, feeding her false information. Unfortunately, spies may have accidentally rotated a map by 90, 180, or 270 degrees, or flipped it along the vertical or the horizontal axis, before delivering it to Heidi. If Heidi can rotate or flip the maps so that two of them become identical, then those maps are of the same solar system. Otherwise, there are traitors in the Rebel ranks! Help Heidi find out. Input The first line of the input contains one number _N_ (1u2009≤u2009_N_u2009≤u200910) – the dimension of each map. Next _N_ lines each contain _N_ characters, depicting the first map: 'X' indicates a star, while 'O' indicates an empty quadrant of space. Next _N_ lines each contain _N_ characters, depicting the second map in the same format. Output The only line of output should contain the word Yes if the maps are identical, or No if it is impossible to match them by performing rotations and translations. Examples Input 4 XOOO XXOO OOOO XXXX XOOO XOOO XOXO XOXX Note In the first test, you can match the first map to the second map by first flipping the first map along the vertical axis, and then by rotating it 90 degrees clockwise.
1,400
false
false
true
false
false
false
false
false
false
false
5,893
773A
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made _y_ submissions, out of which _x_ have been successful. Thus, your current success rate on Codeforces is equal to _x_u2009/u2009_y_. Your favorite rational number in the [0;1] range is _p_u2009/u2009_q_. Now you wonder: what is the smallest number of submissions you have to make if you want your success rate to be _p_u2009/u2009_q_? Input The first line contains a single integer _t_ (1u2009≤u2009_t_u2009≤u20091000)xa0— the number of test cases. Each of the next _t_ lines contains four integers _x_, _y_, _p_ and _q_ (0u2009≤u2009_x_u2009≤u2009_y_u2009≤u2009109; 0u2009≤u2009_p_u2009≤u2009_q_u2009≤u2009109; _y_u2009>u20090; _q_u2009>u20090). It is guaranteed that _p_u2009/u2009_q_ is an irreducible fraction. Hacks. For hacks, an additional constraint of _t_u2009≤u20095 must be met. Output For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve. Example Input 4 3 10 1 2 7 14 3 8 20 70 2 7 5 6 1 1 Note In the first example, you have to make 4 successful submissions. Your success rate will be equal to 7u2009/u200914, or 1u2009/u20092. In the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9u2009/u200924, or 3u2009/u20098. In the third example, there is no need to make any new submissions. Your success rate is already equal to 20u2009/u200970, or 2u2009/u20097. In the fourth example, the only unsuccessful submission breaks your hopes of having the success rate equal to 1.
1,700
true
false
false
false
false
false
false
true
false
false
6,687
1771D
Hossam has an unweighted tree $$$G$$$ with letters in vertices. Hossam defines $$$s(v, , u)$$$ as a string that is obtained by writing down all the letters on the unique simple path from the vertex $$$v$$$ to the vertex $$$u$$$ in the tree $$$G$$$. A string $$$a$$$ is a subsequence of a string $$$s$$$ if $$$a$$$ can be obtained from $$$s$$$ by deletion of several (possibly, zero) letters. For example, "dores", "cf", and "for" are subsequences of "codeforces", while "decor" and "fork" are not. A palindrome is a string that reads the same from left to right and from right to left. For example, "abacaba" is a palindrome, but "abac" is not. Hossam defines a sub-palindrome of a string $$$s$$$ as a subsequence of $$$s$$$, that is a palindrome. For example, "k", "abba" and "abhba" are sub-palindromes of the string "abhbka", but "abka" and "cat" are not. Hossam defines a maximal sub-palindrome of a string $$$s$$$ as a sub-palindrome of $$$s$$$, which has the maximal length among all sub-palindromes of $$$s$$$. For example, "abhbka" has only one maximal sub-palindrome — "abhba". But it may also be that the string has several maximum sub-palindromes: the string "abcd" has $$$4$$$ maximum sub-palindromes. Help Hossam find the length of the longest maximal sub-palindrome among all $$$s(v, , u)$$$ in the tree $$$G$$$. Note that the sub-palindrome is a subsequence, not a substring. Input The first line contains one integer $$$t$$$ ($$$1 le t le 200$$$) — the number of test cases. The first line of each test case has one integer number $$$n$$$ ($$$1 le n le 2 cdot 10^3$$$) — the number of vertices in the graph. The second line contains a string $$$s$$$ of length $$$n$$$, the $$$i$$$-th symbol of which denotes the letter on the vertex $$$i$$$. It is guaranteed that all characters in this string are lowercase English letters. The next $$$n - 1$$$ lines describe the edges of the tree. Each edge is given by two integers $$$v$$$ and $$$u$$$ ($$$1 le v, , u le n$$$, $$$v eq u$$$). These two numbers mean that there is an edge $$$(v, , u)$$$ in the tree. It is guaranteed that the given edges form a tree. It is guaranteed that sum of all $$$n$$$ doesn't exceed $$$2 cdot 10^3$$$. Output For each test case output one integer — the length of the longest maximal sub-palindrome among all $$$s(v, , u)$$$. Example Input 2 5 abaca 1 2 1 3 3 4 4 5 9 caabadedb 1 2 2 3 2 4 1 5 5 6 5 7 5 8 8 9 Note In the first example the maximal subpalindromes are "aaa" with letters in vertices $$$1, , 3, , 5$$$, or "aca" with letters in vertices $$$1, , 4, , 5$$$. The tree from the first example. In the second example there is only one maximal palindrome "bacab" with letters in vertices $$$4, , 2, , 1, , 5, , 9$$$. The tree from the second example.
2,100
false
false
false
true
true
false
true
false
false
false
1,666
2014C
In Sherwood, we judge a man not by his wealth, but by his merit. Look around, the rich are getting richer, and the poor are getting poorer. We need to take from the rich and give to the poor. We need Robin Hood! There are $$$n$$$ people living in the town. Just now, the wealth of the $$$i$$$-th person was $$$a_i$$$ gold. But guess what? The richest person has found an extra pot of gold! More formally, find an $$$a_j=max(a_1, a_2, dots, a_n)$$$, change $$$a_j$$$ to $$$a_j+x$$$, where $$$x$$$ is a non-negative integer number of gold found in the pot. If there are multiple maxima, it can be any one of them. A person is unhappy if their wealth is strictly less than half of the average wealth$$$^{ ext{∗}}$$$. If strictly more than half of the total population $$$n$$$ are unhappy, Robin Hood will appear by popular demand. Determine the minimum value of $$$x$$$ for Robin Hood to appear, or output $$$-1$$$ if it is impossible. $$$^{ ext{∗}}$$$The average wealth is defined as the total wealth divided by the total population $$$n$$$, that is, $$$frac{sum a_i}{n}$$$, the result is a real number. Input The first line of input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 2cdot10^5$$$)xa0— the total population. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0— the wealth of each person. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output one integerxa0— the minimum number of gold that the richest person must find for Robin Hood to appear. If it is impossible, output $$$-1$$$ instead. Example Input 6 1 2 2 2 19 3 1 3 20 4 1 2 3 4 5 1 2 3 4 5 6 1 2 1 1 1 25 Note In the first test case, it is impossible for a single person to be unhappy. In the second test case, there is always $$$1$$$ happy person (the richest). In the third test case, no additional gold are required, so the answer is $$$0$$$. In the fourth test case, after adding $$$15$$$ gold, the average wealth becomes $$$frac{25}{4}$$$, and half of this average is $$$frac{25}{8}$$$, resulting in $$$3$$$ people being unhappy. In the fifth test case, after adding $$$16$$$ gold, the average wealth becomes $$$frac{31}{5}$$$, resulting in $$$3$$$ people being unhappy.
1,100
true
true
false
false
false
false
false
true
false
false
169
1130B
Sasha and Dima want to buy two $$$n$$$-tier cakes. Each cake should consist of $$$n$$$ different tiers: from the size of $$$1$$$ to the size of $$$n$$$. Tiers should go in order from the smallest to the biggest (from top to bottom). They live on the same street, there are $$$2 cdot n$$$ houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the $$$i$$$-th house you can buy a tier of the size $$$a_i$$$ ($$$1 le a_i le n$$$). Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: $$$1$$$, then $$$2$$$, then $$$3$$$ and so on up to $$$n$$$. Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly $$$1$$$. Input The first line of the input contains an integer number $$$n$$$ — the number of tiers in each cake ($$$1 le n le 10^5$$$). The second line contains $$$2 cdot n$$$ integers $$$a_1, a_2, dots, a_{2n}$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ is equal to the size of the tier, which can be bought in the $$$i$$$-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from $$$1$$$ to $$$n$$$ occurs in $$$a$$$ exactly two times. Output Print one number xa0— the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy $$$n$$$ tiers in ascending order of their sizes. Note In the first example, the possible optimal sequence of actions is: Sasha buys a tier of size $$$1$$$ near the $$$1$$$-st house ($$$a_1=1$$$); Dima goes to the house $$$2$$$; Dima buys a tier of size $$$1$$$ near the $$$2$$$-nd house ($$$a_2=1$$$); Sasha goes to the house $$$4$$$; Sasha buys a tier of size $$$2$$$ near the $$$4$$$-th house ($$$a_4=2$$$); Sasha goes to the house $$$5$$$; Sasha buys a tier of size $$$3$$$ near the $$$5$$$-th house ($$$a_5=3$$$); Dima goes to the house $$$3$$$; Dima buys a tier of size $$$2$$$ near the $$$3$$$-rd house ($$$a_3=2$$$); Dima goes to the house $$$6$$$; Dima buys a tier of size $$$3$$$ near the $$$6$$$-th house ($$$a_6=3$$$). So, Sasha goes the distance $$$3+1=4$$$, and Dima goes the distance $$$1+1+3=5$$$. In total, they cover a distance of $$$4+5=9$$$. You can make sure that with any other sequence of actions they will walk no less distance.
1,200
false
true
false
false
false
false
false
false
false
false
5,070
1375A
# Sign Flipping Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes You are given n integers a1, a 2, . . . , a n, where n is odd . You are allowed to flip the sign of some (possibly all or none) of them. You wish to perform these flips in such a way that the following conditions hold: 1. At least n−12 of the adjacent differences ai+1 − ai for i = 1 , 2, . . . , n − 1 are greater than or equal to 0.2. At least n−12 of the adjacent differences ai+1 − ai for i = 1 , 2, . . . , n − 1 are less than or equal to 0.Find any valid way to flip the signs. It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them. # Input The input consists of multiple test cases. The first line contains an integer t (1 ≤ t ≤ 500 ) x16 the number of test cases. The description of the test cases follows. The first line of each test case contains an integer n (3 ≤ n ≤ 99 , n is odd) x16 the number of integers given to you. The second line of each test case contains n integers a1, a 2, . . . , a n (−10 9 ≤ ai ≤ 10 9) x16 the numbers themselves. It is guaranteed that the sum of n over all test cases does not exceed 10000 . # Output For each test case, print n integers b1, b 2, . . . , b n, corresponding to the integers after flipping signs. bi has to be equal to either ai or −ai, and of the adjacent differences bi+1 − bi for i = 1 , . . . , n − 1, at least n−12 should be non-negative and at least n−12 should be non-positive. It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them. # Example standard input standard output 53-2 4 3 51 1 1 1 1 5-2 4 7 -6 4 99 7 -4 -2 1 -3 9 -4 -5 9-4 1 9 4 8 9 5 1 -9 -2 -4 3 1 1 1 1 1 -2 -4 7 -6 4 -9 -7 -4 2 1 -3 -9 -4 -5 4 -1 -9 -4 -8 -9 -5 -1 9 Page 1 of 2 Note In the first test case, the difference (−4) − (−2) = −2 is non-positive, while the difference 3 − (−4) = 7 is non-negative. In the second test case, we don’t have to flip any signs. All 4 differences are equal to 0, which is both non-positive and non-negative. In the third test case, 7 − (−4) and 4 − (−6) are non-negative, while (−4) − (−2) and (−6) − 7 are non-positive. Page 2 of 2
1,100
true
false
false
false
false
true
false
false
false
false
3,787
369E
Valera loves segments. He has recently come up with one interesting problem. The _Ox_ axis of coordinates has _n_ segments, the _i_-th segment starts in position _l__i_ and ends in position _r__i_ (we will mark it as [_l__i_,u2009_r__i_]). Your task is to process _m_ queries, each consists of number _cnt__i_ and a set of _cnt__i_ coordinates of points located on the _Ox_ axis. The answer to the query is the number of segments, such that each of them contains at least one point from the query. Segment [_l_,u2009_r_] contains point _q_, if _l_u2009≤u2009_q_u2009≤u2009_r_. Valera found the solution of this problem too difficult. So he asked you to help him. Help Valera. Input The first line contains two integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20093·105) — the number of segments on the axis of coordinates and the number of queries. Next _n_ lines contain the descriptions of the segments. The _i_-th line contains two positive integers _l__i_, _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009106) — the borders of the _i_-th segment. Next _m_ lines contain the description of the queries, one per line. Each line starts from integer _cnt__i_ (1u2009≤u2009_cnt__i_u2009≤u20093·105) — the number of points in the _i_-th query. Then the line contains _cnt__i_ distinct positive integers _p_1,u2009_p_2,u2009...,u2009_p__cnt__i_ (1u2009≤u2009_p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__cnt__i_u2009≤u2009106) — the coordinates of points in the _i_-th query. It is guaranteed that the total number of points in all queries doesn't exceed 3·105. Output Print _m_ non-negative integers, where the _i_-th number is the response to the _i_-th query. Examples Input 3 3 1 3 4 5 6 7 3 1 4 7 2 4 5 1 8
2,200
false
false
false
false
true
false
false
true
false
false
8,354
10B
All cinema halls in Berland are rectangles with _K_ rows of _K_ seats each, and _K_ is an odd number. Rows and seats are numbered from 1 to _K_. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of _M_ people, who come to watch a movie, want necessarily to occupy _M_ successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for _M_ seats comes, the program should determine the row number _x_ and the segment [_y__l_,u2009_y__r_] of the seats numbers in this row, where _y__r_u2009-u2009_y__l_u2009+u20091u2009=u2009_M_. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, — the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is . If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number _x_ is lower). If the variants are still multiple, it should choose the one with the minimum _y__l_. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers _N_ and _K_ (1u2009≤u2009_N_u2009≤u20091000,u20091u2009≤u2009_K_u2009≤u200999) — the amount of requests and the hall size respectively. The second line contains _N_ space-separated integers _M__i_ from the range [1,u2009_K_] — requests to the program. Output Output _N_ lines. In the _i_-th line output «-1» (without quotes), if it is impossible to find _M__i_ successive seats in one row, otherwise output three numbers _x_,u2009_y__l_,u2009_y__r_. Separate the numbers with a space. Examples Output 2 2 2 1 1 2 3 1 3 2 1 1
1,500
false
false
true
true
false
false
false
false
false
false
9,944
952A
800
true
false
false
false
false
false
false
false
false
false
5,916
1693C
AmShZ has traveled to Italy from Iran for the Thom Yorke concert. There are $$$n$$$ cities in Italy indexed from $$$1$$$ to $$$n$$$ and $$$m$$$ directed roads indexed from $$$1$$$ to $$$m$$$. Initially, Keshi is located in the city $$$1$$$ and wants to go to AmShZ's house in the city $$$n$$$. Since Keshi doesn't know the map of Italy, AmShZ helps him to see each other as soon as possible. In the beginning of each day, AmShZ can send one of the following two messages to Keshi: AmShZ sends the index of one road to Keshi as a blocked road. Then Keshi will understand that he should never use that road and he will remain in his current city for the day. AmShZ tells Keshi to move. Then, Keshi will randomly choose one of the cities reachable from his current city and move there. (city $$$B$$$ is reachable from city $$$A$$$ if there's an out-going road from city $$$A$$$ to city $$$B$$$ which hasn't become blocked yet). If there are no such cities, Keshi will remain in his current city.Note that AmShZ always knows Keshi's current location. AmShZ and Keshi want to find the smallest possible integer $$$d$$$ for which they can make sure that they will see each other after at most $$$d$$$ days. Help them find $$$d$$$. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ $$$(2 le n le 2 cdot 10^5, 1 le m le 2 cdot 10^5)$$$ xa0— the number of cities and roads correspondingly. The $$$i$$$-th line of the following $$$m$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ $$$(1 le v_i , u_i le n,v_i eq u_i)$$$, denoting a directed road going from city $$$v_i$$$ to city $$$u_i$$$. It is guaranteed that there is at least one route from city $$$1$$$ to city $$$n$$$. Note that there may be more than one road between a pair of cities in each direction. Output Output the smallest possible integer $$$d$$$ to make sure that AmShZ and Keshi will see each other after at most $$$d$$$ days. Examples Input 5 7 1 2 2 3 3 5 1 4 4 3 4 5 3 1 Note In the first sample, it's enough for AmShZ to send the second type of message. In the second sample, on the first day, AmShZ blocks the first road. So the only reachable city from city $$$1$$$ will be city $$$4$$$. Hence on the second day, AmShZ can tell Keshi to move and Keshi will arrive at AmShZ's house. It's also possible for AmShZ to tell Keshi to move for two days.
2,300
false
true
false
false
false
false
false
false
false
true
2,120
1996C
You are given two strings $$$a$$$ and $$$b$$$ of length $$$n$$$. Then, you are (forced against your will) to answer $$$q$$$ queries. For each query, you are given a range bounded by $$$l$$$ and $$$r$$$. In one operation, you can choose an integer $$$i$$$ ($$$l leq i leq r$$$) and set $$$a_i = x$$$ where $$$x$$$ is any character you desire. Output the minimum number of operations you must perform such that $$$ exttt{sorted(a[l..r])} = exttt{sorted(b[l..r])}$$$. The operations you perform on one query does not affect other queries. For an arbitrary string $$$c$$$, $$$ exttt{sorted(c[l..r])}$$$ denotes the substring consisting of characters $$$c_l, c_{l+1}, ... , c_r$$$ sorted in lexicographical order. Input The first line contains $$$t$$$ ($$$1 leq t leq 1000$$$) – the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 leq n, q leq 2 cdot 10^5$$$) – the length of both strings and the number of queries. The following line contains $$$a$$$ of length $$$n$$$. It is guaranteed $$$a$$$ only contains lowercase latin letters. The following line contains $$$b$$$ of length $$$n$$$. It is guaranteed $$$b$$$ only contains lowercase latin letters. The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq n$$$) – the range of the query. It is guaranteed the sum of $$$n$$$ and $$$q$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each query, output an integer, the minimum number of operations you need to perform in a new line. Example Input 3 5 3 abcde edcba 1 5 1 4 3 3 4 2 zzde azbe 1 3 1 4 6 3 uwuwuw wuwuwu 2 4 1 3 1 6 Note For the first query, $$$ exttt{sorted(a[1..5])} =$$$ abcde and $$$ exttt{sorted(b[1..5])} =$$$ abcde, so no operations are necessary. For the second query, you need to set $$$a_1 = $$$ e. Then, $$$ exttt{sorted(a[1..4])} = exttt{sorted(b[1..4])} = $$$ bcde.
1,200
false
true
false
true
false
false
false
false
true
false
288
1315C
Problem - 1315C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy *1200 No tag edit access → Contest materials ") and Codeforces Round #623 ") $$$, or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The first line of each test case consists of one integer $$$n$$$xa0— the number of elements in the sequence $$$b$$$ ($$$1 le n le 100$$$). The second line of each test case consists of $$$n$$$ different integers $$$b_1, ldots, b_n$$$xa0— elements of the sequence $$$b$$$ ($$$1 le b_i le 2n$$$). It is guaranteed that the sum of $$$n$$$ by all test cases doesn't exceed $$$100$$$. Output For each test case, if there is no appropriate permutation, print one number $$$-1$$$. Otherwise, print $$$2n$$$ integers $$$a_1, ldots, a_{2n}$$$xa0— required lexicographically minimal permutation of numbers from $$$1$$$ to $$$2n$$$. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
1,200
false
true
false
false
false
false
false
false
false
false
4,122
1242B
Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook with a description of a graph. It is an undirected weighted graph on $$$n$$$ vertices. It is a complete graph: each pair of vertices is connected by an edge. The weight of each edge is either $$$0$$$ or $$$1$$$; exactly $$$m$$$ edges have weight $$$1$$$, and all others have weight $$$0$$$. Since Ujan doesn't really want to organize his notes, he decided to find the weight of the minimum spanning tree of the graph. (The weight of a spanning tree is the sum of all its edges.) Can you find the answer for Ujan so he stops procrastinating? Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 10^5$$$, $$$0 leq m leq min(frac{n(n-1)}{2},10^5)$$$), the number of vertices and the number of edges of weight $$$1$$$ in the graph. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq n$$$, $$$a_i eq b_i$$$), the endpoints of the $$$i$$$-th edge of weight $$$1$$$. It is guaranteed that no edge appears twice in the input. Output Output a single integer, the weight of the minimum spanning tree of the graph. Examples Input 6 11 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 Note The graph from the first sample is shown below. Dashed edges have weight $$$0$$$, other edges have weight $$$1$$$. One of the minimum spanning trees is highlighted in orange and has total weight $$$2$$$. In the second sample, all edges have weight $$$0$$$ so any spanning tree has total weight $$$0$$$.
1,900
false
false
false
false
false
false
false
false
true
true
4,488
283C
In the Isle of Guernsey there are _n_ different types of coins. For each _i_ (1u2009≤u2009_i_u2009≤u2009_n_), coin of type _i_ is worth _a__i_ cents. It is possible that _a__i_u2009=u2009_a__j_ for some _i_ and _j_ (_i_u2009≠u2009_j_). Bessie has some set of these coins totaling _t_ cents. She tells Jessie _q_ pairs of integers. For each _i_ (1u2009≤u2009_i_u2009≤u2009_q_), the pair _b__i_,u2009_c__i_ tells Jessie that Bessie has a strictly greater number of coins of type _b__i_ than coins of type _c__i_. It is known that all _b__i_ are distinct and all _c__i_ are distinct. Help Jessie find the number of possible combinations of coins Bessie could have. Two combinations are considered different if there is some _i_ (1u2009≤u2009_i_u2009≤u2009_n_), such that the number of coins Bessie has of type _i_ is different in the two combinations. Since the answer can be very large, output it modulo 1000000007 (109u2009+u20097). If there are no possible combinations of coins totaling _t_ cents that satisfy Bessie's conditions, output 0. Input The first line contains three space-separated integers, _n_,u2009_q_ and _t_ (1u2009≤u2009_n_u2009≤u2009300;xa00u2009≤u2009_q_u2009≤u2009_n_;xa01u2009≤u2009_t_u2009≤u2009105). The second line contains _n_ space separated integers, _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105). The next _q_ lines each contain two distinct space-separated integers, _b__i_ and _c__i_ (1u2009≤u2009_b__i_,u2009_c__i_u2009≤u2009_n_;xa0_b__i_u2009≠u2009_c__i_). It's guaranteed that all _b__i_ are distinct and all _c__i_ are distinct. Output A single integer, the number of valid coin combinations that Bessie could have, modulo 1000000007 (109u2009+u20097). Note For the first sample, the following 3 combinations give a total of 17 cents and satisfy the given conditions: {0xa0_of_xa0_type_xa01,u20091xa0_of_xa0_type_xa02,u20093xa0_of_xa0_type_xa03,u20092xa0_of_xa0_type_xa04},u2009{0,u20090,u20096,u20091},u2009{2,u20090,u20093,u20091}. No other combinations exist. Note that even though 4 occurs in both _b__i_ and _c__i_,u2009 the problem conditions are still satisfied because all _b__i_ are distinct and all _c__i_ are distinct.
2,100
false
false
false
true
false
false
false
false
false
false
8,701
1333C
Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array $$$c$$$ is a subarray of an array $$$b$$$ if $$$c$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array $$$[-1, 2, -3]$$$ is good, as all arrays $$$[-1]$$$, $$$[-1, 2]$$$, $$$[-1, 2, -3]$$$, $$$[2]$$$, $$$[2, -3]$$$, $$$[-3]$$$ have nonzero sums of elements. However, array $$$[-1, 2, -1, -3]$$$ isn't good, as his subarray $$$[-1, 2, -1]$$$ has sum of elements equal to $$$0$$$. Help Eugene to calculate the number of nonempty good subarrays of a given array $$$a$$$. Input The first line of the input contains a single integer $$$n$$$ ($$$1 le n le 2 imes 10^5$$$) xa0— the length of array $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$) xa0— the elements of $$$a$$$. Output Output a single integer xa0— the number of good subarrays of $$$a$$$. Note In the first sample, the following subarrays are good: $$$[1]$$$, $$$[1, 2]$$$, $$$[2]$$$, $$$[2, -3]$$$, $$$[-3]$$$. However, the subarray $$$[1, 2, -3]$$$ isn't good, as its subarray $$$[1, 2, -3]$$$ has sum of elements equal to $$$0$$$. In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray $$$[41, -41, 41]$$$ isn't good, as its subarray $$$[41, -41]$$$ has sum of elements equal to $$$0$$$.
1,700
false
false
true
false
true
false
false
true
false
false
4,041
1015E1
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $$$0$$$ are not allowed). Let's consider empty cells are denoted by '.', then the following figures are stars: The leftmost figure is a star of size $$$1$$$, the middle figure is a star of size $$$2$$$ and the rightmost figure is a star of size $$$3$$$. You are given a rectangular grid of size $$$n imes m$$$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $$$1$$$ to $$$n$$$, columns are numbered from $$$1$$$ to $$$m$$$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $$$n cdot m$$$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes. In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $$$n cdot m$$$ stars. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$3 le n, m le 100$$$) — the sizes of the given grid. The next $$$n$$$ lines contains $$$m$$$ characters each, the $$$i$$$-th line describes the $$$i$$$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only. Output If it is impossible to draw the given grid using stars only, print "-1". Otherwise in the first line print one integer $$$k$$$ ($$$0 le k le n cdot m$$$) — the number of stars needed to draw the given grid. The next $$$k$$$ lines should contain three integers each — $$$x_j$$$, $$$y_j$$$ and $$$s_j$$$, where $$$x_j$$$ is the row index of the central star character, $$$y_j$$$ is the column index of the central star character and $$$s_j$$$ is the size of the star. Each star should be completely inside the grid. Examples Input 6 8 ....*... ...**... ..*****. ...**... ....*... ........ Input 5 5 .*... ****. .*** ..**. ..... Input 5 5 .*... ***.. .*... .*... ..... Note In the first example the output 2 3 4 1 3 5 2 is also correct.
1,700
false
true
false
true
false
false
true
false
false
false
5,615
1980A
Vlad is planning to hold $$$m$$$ rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'. Vlad already has a bank of $$$n$$$ problems, where the $$$i$$$-th problem has a difficulty level of $$$a_i$$$. There may not be enough of these problems, so he may have to come up with a few more problems. Vlad wants to come up with as few problems as possible, so he asks you to find the minimum number of problems he needs to come up with in order to hold $$$m$$$ rounds. For example, if $$$m=1$$$, $$$n = 10$$$, $$$a=$$$ 'BGECDCBDED', then he needs to come up with two problems: one of difficulty level 'A' and one of difficulty level 'F'. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 50$$$, $$$1 le m le 5$$$)xa0— the number of problems in the bank and the number of upcoming rounds, respectively. The second line of each test case contains a string $$$a$$$ of $$$n$$$ characters from 'A' to 'G'xa0— the difficulties of the problems in the bank. Output For each test case, output a single integerxa0— the minimum number of problems that need to come up with to hold $$$m$$$ rounds. Example Input 3 10 1 BGECDCBDED 10 2 BGECDCBDED 9 1 BBCDEFFGG
800
true
false
false
false
false
false
false
false
false
false
410
1943D2
This is the hard version of the problem. The only difference between the two versions is the constraint on $$$n$$$. You can make hacks only if both versions of the problem are solved. An array $$$b$$$ of $$$m$$$ non-negative integers is said to be good if all the elements of $$$b$$$ can be made equal to $$$0$$$ using the following operation some (possibly, zero) times: Select two distinct indices $$$l$$$ and $$$r$$$ ($$$1 leq l color{red}{<} r leq m$$$) and subtract $$$1$$$ from all $$$b_i$$$ such that $$$l leq i leq r$$$. You are given two positive integers $$$n$$$, $$$k$$$ and a prime number $$$p$$$. Over all $$$(k+1)^n$$$ arrays of length $$$n$$$ such that $$$0 leq a_i leq k$$$ for all $$$1 leq i leq n$$$, count the number of good arrays. Since the number might be too large, you are only required to find it modulo $$$p$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains three positive integers $$$n$$$, $$$k$$$ and $$$p$$$ ($$$3 leq n leq 3000$$$, $$$1 leq k leq n$$$, $$$10^8 < p < 10^9$$$)xa0— the length of the array $$$a$$$, the upper bound on the elements of $$$a$$$ and modulus $$$p$$$. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^7$$$, and $$$p$$$ is prime. Output For each test case, on a new line, output the number of good arrays modulo $$$p$$$. Example Input 4 3 1 998244853 4 1 998244353 3 2 998244353 343 343 998244353 Note In the first test case, the $$$4$$$ good arrays $$$a$$$ are: $$$[0,0,0]$$$; $$$[0,1,1]$$$; $$$[1,1,0]$$$; $$$[1,1,1]$$$.
2,800
false
false
false
true
false
false
false
false
false
false
619
20B
Problem - 20B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *2000 No tag edit access . Any coefficient may be equal to 0. Output In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. Examples Input 1 -5 6 Output 2 2.0000000000 3.0000000000
2,000
true
false
false
false
false
false
false
false
false
false
9,894
5B
Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product. You are to implement the alignment in the shortest possible time. Good luck! Input The input file consists of one or more lines, each of the lines contains Latin letters, digits and/or spaces. The lines cannot start or end with a space. It is guaranteed that at least one of the lines has positive length. The length of each line and the total amount of the lines do not exceed 1000. Output Format the given text, aligning it center. Frame the whole text with characters «*» of the minimum size. If a line cannot be aligned perfectly (for example, the line has even length, while the width of the block is uneven), you should place such lines rounding down the distance to the left or to the right edge and bringing them closer left or right alternatively (you should start with bringing left). Study the sample tests carefully to understand the output format better. Examples Input This isCodeforces Beta Round 5 Output *********** This is *Codeforces Beta Round 5 *********** Input welcome to the Codeforces Beta Round 5and good luck Output *************** *welcome to the Codeforces Beta Round 5 and good luck ****************
1,200
false
false
true
false
false
false
false
false
false
false
9,969
2025D
Imagine a game where you play as a character that has two attributes: "Strength" and "Intelligence", that are at zero level initially. During the game, you'll acquire $$$m$$$ attribute points that allow you to increase your attribute levelsxa0— one point will increase one of the attributes by one level. But sometimes, you'll encounter a so-called "Attribute Checks": if your corresponding attribute is high enough, you'll pass it; otherwise, you'll fail it. Spending some time, you finally prepared a list which contains records of all points you got and all checks you've met. And now you're wondering: what is the maximum number of attribute checks you can pass in a single run if you'd spend points wisely? Note that you can't change the order of records. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le m le 5000$$$; $$$m < n le 2 cdot 10^6$$$)xa0— the number of records in the list and the total number of points you'll get during the game. The second line contains $$$n$$$ integers $$$r_1, r_2, dots, r_n$$$ ($$$-m le r_i le m$$$), where $$$r_i$$$ encodes the $$$i$$$-th record: If $$$r_i = 0$$$, then the $$$i$$$-th record is an acquiring one attribute point. You can spend to level up either Strength or Intelligence; If $$$r_i > 0$$$, then it's an Intelligence check: if your Intelligence level is greater than or equal to $$$r_i$$$, you pass. If $$$r_i < 0$$$, then it's a Strength check: if your Strength level is greater than or equal to $$$r_i$$$, you pass. Additional constraint on the input: the sequence $$$r_1, r_2, dots, r_n$$$ contains exactly $$$m$$$ elements equal to $$$0$$$. Output Print one integer — the maximum number of checks you can pass. Examples Input 10 5 0 1 0 2 0 -3 0 -4 0 -5 Input 9 3 0 0 1 0 2 -3 -2 -2 1 Note In the first test, it's optimal to spend each point in Strength, so you'll fail $$$2$$$ Intelligence checks but pass $$$3$$$ Strength checks. In the second test, you'll fail both checks, since the first point you get comes after the checks. In the third test, one of the optimal strategies is: 1. spend the first point on Intelligence; 2. spend the second point on Strength; 3. spend the third point on Strength; As a result, you'll pass $$$2$$$ Intelligence checks $$$r_3$$$ and $$$r_9$$$ and $$$2$$$ Strength checks $$$r_7$$$ and $$$r_8$$$.
1,800
true
false
true
true
true
false
true
false
false
false
120
644C
There are some websites that are accessible through several different addresses. For example, for a long time Codeforces was accessible with two hostnames codeforces.com and codeforces.ru. You are given a list of page addresses being queried. For simplicity we consider all addresses to have the form where: <hostname>xa0— server name (consists of words and maybe some dots separating them), /<path>xa0— optional part, where <path> consists of words separated by slashes. We consider two <hostname> to correspond to one website if for each query to the first <hostname> there will be exactly the same query to the second one and vice versaxa0— for each query to the second <hostname> there will be the same query to the first one. Take a look at the samples for further clarifications. Your goal is to determine the groups of server names that correspond to one website. Ignore groups consisting of the only server name. Please note, that according to the above definition queries and are different. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of page queries. Then follow _n_ lines each containing exactly one address. Each address is of the form where: <hostname> consists of lowercase English letters and dots, there are no two consecutive dots, <hostname> doesn't start or finish with a dot. The length of <hostname> is positive and doesn't exceed 20. <path> consists of lowercase English letters, dots and slashes. There are no two consecutive slashes, <path> doesn't start with a slash and its length doesn't exceed 20. Addresses are not guaranteed to be distinct. Output First print _k_xa0— the number of groups of server names that correspond to one website. You should count only groups of size greater than one. Next _k_ lines should contain the description of groups, one group per line. For each group print all server names separated by a single space. You are allowed to print both groups and names inside any group in arbitrary order. Examples Input 10 Output 1 Input 14 Output 2
2,100
false
false
true
false
true
false
false
true
true
false
7,235
223B
Problem - 223B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures dp strings *1900 No tag edit access → Contest materials is a string _x_u2009=u2009_s__k_1_s__k_2... _s__k__x_ (1u2009≤u2009_k_1u2009<u2009_k_2u2009<u2009...u2009<u2009_k__x_u2009≤u2009_s_). You've got two stringsxa0— _s_ and _t_. Let's consider all subsequences of string _s_, coinciding with string _t_. Is it true that each character of string _s_ occurs in at least one of these subsequences? In other words, is it true that for all _i_ (1u2009≤u2009_i_u2009≤u2009_s_), there is such subsequence _x_u2009=u2009_s__k_1_s__k_2... _s__k__x_ of string _s_, that _x_u2009=u2009_t_ and for some _j_ (1u2009≤u2009_j_u2009≤u2009_x_) _k__j_u2009=u2009_i_. Input The first line contains string _s_, the second line contains string _t_. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2·105. Output Print "Yes" (without the quotes), if each character of the string _s_ occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise. Examples Input abab ab Output Yes Input abacaba aba Output No Input abc ba Output No Note In the first sample string _t_ can occur in the string _s_ as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string _s_ occurs at least once. In the second sample the 4-th character of the string _s_ doesn't occur in any occurrence of string _t_. In the third sample there is no occurrence of string _t_ in string _s_.
1,900
false
false
false
true
true
false
false
false
false
false
8,946