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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1154C | Polycarp has a cat and his cat is a real gourmet! Dependent on a day of the week he eats certain type of food: on Mondays, Thursdays and Sundays he eats fish food; on Tuesdays and Saturdays he eats rabbit stew; on other days of week he eats chicken stake. Polycarp plans to go on a trip and already packed his backpack. His backpack contains: $$$a$$$ daily rations of fish food; $$$b$$$ daily rations of rabbit stew; $$$c$$$ daily rations of chicken stakes. Polycarp has to choose such day of the week to start his trip that his cat can eat without additional food purchases as long as possible. Print the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally. Input The first line of the input contains three positive integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 le a, b, c le 7cdot10^8$$$) β the number of daily rations of fish food, rabbit stew and chicken stakes in Polycarps backpack correspondingly. Output Print the maximum number of days the cat can eat in a trip without additional food purchases, if Polycarp chooses the day of the week to start his trip optimally. Note In the first example the best day for start of the trip is Sunday. In this case, during Sunday and Monday the cat will eat fish food, during Tuesday β rabbit stew and during Wednesday β chicken stake. So, after four days of the trip all food will be eaten. In the second example Polycarp can start his trip in any day of the week. In any case there are food supplies only for one week in Polycarps backpack. In the third example Polycarp can start his trip in any day, excluding Wednesday, Saturday and Sunday. In this case, the cat will eat three different dishes in three days. Nevertheless that after three days of a trip there will be $$$99$$$ portions of rabbit stew in a backpack, can cannot eat anything in fourth day of a trip. | 1,400 | true | false | true | false | false | false | false | false | false | false | 4,941 |
1862D | Tema decided to improve his ice cream making skills. He has already learned how to make ice cream in a cone using exactly two balls. Before his ice cream obsession, Tema was interested in mathematics. Therefore, he is curious about the minimum number of balls he needs to have in order to make exactly $$$n$$$ different types of ice cream. There are plenty possible ice cream flavours: $$$1, 2, 3, dots$$$. Tema can make two-balls ice cream with any flavours (probably the same). Two ice creams are considered different if their sets of ball flavours are different. For example, $$${1, 2} = {2, 1}$$$, but $$${1, 1} eq {1, 2}$$$. For example, having the following ice cream balls: $$${1, 1, 2}$$$, Tema can make only two types of ice cream: $$${1, 1}$$$ and $$${1, 2}$$$. Note, that Tema do not need to make all the ice cream cones at the same time. This means that he making ice cream cones independently. Also in order to make a following cone $$${x, x}$$$ for some $$$x$$$, Tema needs at least $$$2$$$ balls of type $$$x$$$. Help Tema answer this question. It can be shown that answer always exist. Input Each test consists of multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then follows the description of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^{18}$$$)xa0β the number of ice cream types that Tema wants to make. Output For each test case, output a single integerxa0β the minimum number of balls Tema needs to buy. Example Input 5 1 3 6 179 1000000000000000000 Output 2 3 4 27 2648956421 Note In the first sample, it is enough to have following balls types: $$${1, 1}$$$. Note, that set $$${1}$$$ if not enough since we need at least $$$2$$$ balls of a type $$$1$$$ in order to make such cone $$${1, 1}$$$. In the second sample, it is not possible to make it with $$$2$$$ balls, but it can be done with these balls: $$${1, 2, 3}$$$. In the third sample, $$${1, 2, 3, 4}$$$ is optimal answer, so we can get following ice cream cones: $$${1, 2}$$$, $$${1, 3}$$$, $$${1, 4}$$$, $$${2, 3}$$$, $$${2, 4}$$$, $$${3, 4}$$$. | 1,300 | true | false | false | false | false | true | false | true | false | false | 1,110 |
1829G | In a carnival game, there is a huge pyramid of cans with $$$2023$$$ rows, numbered in a regular pattern as shown. If can $$$9^2$$$ is hit initially, then all cans colored red in the picture above would fall. You throw a ball at the pyramid, and it hits a single can with number $$$n^2$$$. This causes all cans that are stacked on top of this can to fall (that is, can $$$n^2$$$ falls, then the cans directly above $$$n^2$$$ fall, then the cans directly above those cans, and so on). For example, the picture above shows the cans that would fall if can $$$9^2$$$ is hit. What is the sum of the numbers on all cans that fall? Recall that $$$n^2 = n imes n$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0β the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^6$$$)xa0β it means that the can you hit has label $$$n^2$$$. Output For each test case, output a single integerxa0β the sum of the numbers on all cans that fall. 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++). For all valid inputs, the answer will always fit into 64-bit integer type. Example Input 10 9 1 2 3 4 5 6 10 1434 1000000 Output 156 1 5 10 21 39 46 146 63145186 58116199242129511 Note The first test case is pictured in the statement. The sum of the numbers that fall is $$$$$$1^2 + 2^2 + 3^2 + 5^2 + 6^2 + 9^2 = 1 + 4 + 9 + 25 + 36 + 81 = 156.$$$$$$ In the second test case, only the can labeled $$$1^2$$$ falls, so the answer is $$$1^2=1$$$. In the third test case, the cans labeled $$$1^2$$$ and $$$2^2$$$ fall, so the answer is $$$1^2+2^2=1+4=5$$$. In the fourth test case, the cans labeled $$$1^2$$$ and $$$3^2$$$ fall, so the answer is $$$1^2+3^2=1+9=10$$$. In the fifth test case, the cans labeled $$$1^2$$$, $$$2^2$$$, and $$$4^2$$$ fall, so the answer is $$$1^2+2^2+4^2=1+4+16=21$$$. | 1,600 | true | false | true | true | true | false | false | false | false | false | 1,308 |
600F | Problem - 600F - Codeforces =============== xa0 ]( "21794") , _a_ is the size of the first part, _b_ is the size of the second part, _m_ is the number of edges in the graph. Each of the next _m_ lines contains two integers _x_,u2009_y_ (1u2009β€u2009_x_u2009β€u2009_a_,u20091u2009β€u2009_y_u2009β€u2009_b_), where _x_ is the number of the vertex in the first part and _y_ is the number of the vertex in the second part. It is guaranteed that there are no multiple edges. Output In the first line print integer _c_ β the minimal number of colours. The second line should contain _m_ integers from 1 to _c_ β the colours of the edges (in the order they appear in the input). If there are several solutions, you can print any one of them. Examples Input 4 3 5 1 2 2 2 3 2 4 1 4 3 Output 3 1 2 3 1 2 | 2,800 | false | false | false | false | false | false | false | false | false | true | 7,441 |
1866F | After the success of the basketball teams formed and trained by Pak Chanek last year (Basketball Together), Pak Chanek wants to measure the performance of each player that is considered as a superstar. There are $$$N$$$ superstar players that have been trained by Pak Chanek. At the end of the season, some calculations will be made on the performance of the $$$N$$$ players using an international method. Each player has two values $$$A_i$$$ and $$$B_i$$$ where each represents the offensive and defensive value of that player. Define $$$ ext{RankA}(i)$$$ as the offensive ranking of the $$$i$$$-th player, whose value is $$$c+1$$$ with $$$c$$$ here representing the number of $$$j$$$ ($$$1 leq j leq N$$$) such that $$$A_j>A_i$$$. Define $$$ ext{RankB}(i)$$$ as the defensive ranking of the $$$i$$$-th player, whose value is $$$c+1$$$ with $$$c$$$ here representing the number of $$$j$$$ ($$$1 leq j leq N$$$) such that $$$B_j>B_i$$$. Define $$$ ext{RankOverall}(i)$$$ as the overall ranking of the $$$i$$$-th player, whose value is $$$c+1$$$ with $$$c$$$ here representing the number of $$$j$$$ ($$$1 leq j leq N$$$) such that $$$ ext{RankA}(j)+ ext{RankB}(j)< ext{RankA}(i)+ ext{RankB}(i)$$$. During the next $$$Q$$$ days, exactly one event will happen on each day. Each event is one of the three following possibilities: 1 k c β If $$$c$$$ is +, then $$$A_k$$$ increases by $$$1$$$. If $$$c$$$ is -, then $$$A_k$$$ decreases by $$$1$$$. ($$$1leq kleq N$$$; $$$c$$$ is + or -) 2 k c β If $$$c$$$ is +, then $$$B_k$$$ increases by $$$1$$$. If $$$c$$$ is -, then $$$B_k$$$ decreases by $$$1$$$. ($$$1leq kleq N$$$; $$$c$$$ is + or -) 3 k β Pak Chanek wants to know the value of $$$ ext{RankOverall}(k)$$$ at that moment. ($$$1leq kleq N$$$) Input The first line contains a single integer $$$N$$$ ($$$1leq Nleq10^5$$$) β the number of superstar players. The second line contains $$$N$$$ integers $$$A_1, A_2, A_3 ldots, A_N$$$ ($$$1 leq A_i leq 10^5$$$) β the offensive value of each player. The third line contains $$$N$$$ integers $$$B_1, B_2, B_3 ldots, B_N$$$ ($$$1 leq B_i leq 10^5$$$) β the defensive value of each player. The fourth line contains a single integer $$$Q$$$ ($$$1leq Qleq10^5$$$) β the number of events. The $$$j$$$-th of the next $$$Q$$$ lines contains the $$$j$$$-th event as described. At any moment, each value of $$$A_i$$$ and $$$B_i$$$ is always between $$$1$$$ and $$$10^5$$$ inclusive. There is at least one event of type $$$3$$$. Output For each event of type $$$3$$$, output a line containing an integer representing the value of $$$ ext{RankOverall}(k)$$$ at that moment. Example Input 5 3 3 1 3 2 3 7 1 3 1 8 3 1 3 2 2 4 + 1 2 - 3 2 3 3 2 2 - 3 1 Note At the $$$8$$$-th event, $$$A=[3,2,1,3,2]$$$ and $$$B=[3,6,1,4,1]$$$. It can be obtained that the values of $$$ ext{RankA}$$$ and $$$ ext{RankB}$$$ for each player are as follows: $$$ ext{RankA}(1)=1$$$, $$$ ext{RankB}(1)=3$$$ $$$ ext{RankA}(2)=3$$$, $$$ ext{RankB}(2)=1$$$ $$$ ext{RankA}(3)=5$$$, $$$ ext{RankB}(3)=4$$$ $$$ ext{RankA}(4)=1$$$, $$$ ext{RankB}(4)=2$$$ $$$ ext{RankA}(5)=3$$$, $$$ ext{RankB}(5)=4$$$ So it can be obtained that $$$ ext{RankOverall}(1)=2$$$. | 3,100 | false | false | false | false | true | false | false | true | true | false | 1,083 |
239A | Problem - 239A - 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 math *1200 No tag edit access β Contest materials potatoes, and the second β _y_ (_y_u2009β₯u20091) potatoes. Valera β very scattered boy, so the first bag of potatoes (it contains _x_ potatoes) Valera lost. Valera remembers that the total amount of potatoes (_x_u2009+u2009_y_) in the two bags, firstly, was not gerater than _n_, and, secondly, was divisible by _k_. Help Valera to determine how many potatoes could be in the first bag. Print all such possible numbers in ascending order. Input The first line of input contains three integers _y_, _k_, _n_ (1u2009β€u2009_y_,u2009_k_,u2009_n_u2009β€u2009109; u2009β€u2009105). Output Print the list of whitespace-separated integers β all possible values of _x_ in ascending order. You should print each possible value of _x_ exactly once. If there are no such values of _x_ print a single integer -1. Examples Input 10 1 10 Output -1 Input 10 6 40 Output 2 8 14 20 26 | 1,200 | true | true | true | false | false | false | false | false | false | false | 8,879 |
522B | One day _n_ friends met at a party, they hadn't seen each other for a long time and so they decided to make a group photo together. Simply speaking, the process of taking photos can be described as follows. On the photo, each photographed friend occupies a rectangle of pixels: the _i_-th of them occupies the rectangle of width _w__i_ pixels and height _h__i_ pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is _W_u2009Γu2009_H_, where _W_ is the total sum of all widths and _H_ is the maximum height of all the photographed friends. As is usually the case, the friends made _n_ photos β the _j_-th (1u2009β€u2009_j_u2009β€u2009_n_) photo had everybody except for the _j_-th friend as he was the photographer. Print the minimum size of each made photo in pixels. Input The first line contains integer _n_ (2u2009β€u2009_n_u2009β€u2009200u2009000) β the number of friends. Then _n_ lines follow: the _i_-th line contains information about the _i_-th friend. The line contains a pair of integers _w__i_,u2009_h__i_ (1u2009β€u2009_w__i_u2009β€u200910,u20091u2009β€u2009_h__i_u2009β€u20091000) β the width and height in pixels of the corresponding rectangle. Output Print _n_ space-separated numbers _b_1,u2009_b_2,u2009...,u2009_b__n_, where _b__i_ β the total number of pixels on the minimum photo containing all friends expect for the _i_-th one. | 1,100 | false | false | true | true | true | false | false | false | false | false | 7,749 |
1016C | Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into _n_ consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there. Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell. Help Vasya! Calculate the maximum total weight of mushrooms he can collect. Input The first line contains the number _n_xa0(1u2009β€u2009_n_u2009β€u20093Β·105) β the length of the glade. The second line contains _n_ numbers _a_1,u2009_a_2,u2009...,u2009_a__n_xa0(1u2009β€u2009_a__i_u2009β€u2009106) β the growth rate of mushrooms in the first row of the glade. The third line contains _n_ numbers _b_1,u2009_b_2,u2009...,u2009_b__n_xa0(1u2009β€u2009_b__i_u2009β€u2009106) is the growth rate of mushrooms in the second row of the glade. Output Output one number β the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once. Examples Input 3 1 1000 10000 10 100 100000 Note In the first test case, the optimal route is as follows: Thus, the collected weight of mushrooms will be 0Β·1u2009+u20091Β·2u2009+u20092Β·3u2009+u20093Β·4u2009+u20094Β·5u2009+u20095Β·6u2009=u200970. In the second test case, the optimal route is as follows: Thus, the collected weight of mushrooms will be 0Β·1u2009+u20091Β·10u2009+u20092Β·100u2009+u20093Β·1000u2009+u20094Β·10000u2009+u20095Β·100000u2009=u2009543210. | 1,800 | false | false | true | true | false | false | false | false | false | false | 5,610 |
1325D | Problem - 1325D - 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 constructive algorithms greedy number theory *1700 No tag edit access β Contest materials $$$. Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, $$$n$$$, representing the length of the desired array. The next line should contain $$$n$$$ positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, $$$3oplus 1 = 2$$$ and $$$3 + 1 = 4$$$. There is no valid array of smaller length. Notice that in the fourth sample the array is empty. | 1,700 | false | true | false | false | false | true | false | false | false | false | 4,090 |
1592E | Bakry got bored of solving problems related to xor, so he asked you to solve this problem for him. You are given an array $$$a$$$ of $$$n$$$ integers $$$ β the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^6$$$) β elements of the array. Output Print a single integer β the length of the longest good subarray. If there are no good subarrays, print $$$0$$$. Note In the first case, the answer is $$$2$$$, as the whole array is good: $$$5 & 6 = 4 > 5 oplus 6 = 3$$$. In the third case, the answer is $$$4$$$, and one of the longest good subarrays is $$$[a_2, a_3, a_4, a_5]$$$: $$$1& 3 & 3 &1 = 1 > 1oplus 3 oplus 3oplus 1 = 0$$$. | 2,400 | true | true | false | false | false | false | false | false | false | false | 2,689 |
1211G | There are $$$n$$$ cities and $$$n-1$$$ two-way roads in Treeland. Each road connects a pair of different cities. From any city you can drive to any other, moving only along the roads. Cities are numbered from $$$1$$$ to $$$n$$$. Yes, of course, you recognized an undirected tree in this description. There is exactly one flag in each city, in the $$$i$$$-th city the flag color is $$$c_i$$$. The colors of the flags in different cities may be the same. If the King travels along the route $$$[u_1, u_2, u_3, dots, u_k]$$$, then this means that he starts in the city $$$u_1$$$, then moves to the city $$$u_2$$$ ($$$u_2$$$ is connected by road with $$$u_1$$$), then from $$$u_2$$$ to $$$u_3$$$ ($$$u_3$$$ is connected by road to $$$u_2$$$), and so on until he arrives in the city of $$$u_k$$$. It is possible that during this route the King will visit the same city more than once. In other words, the route $$$[u_1, u_2, u_3, dots, u_k]$$$ does not necessarily consist of different cities. In terms of graph theory β the King moves from $$$u_1$$$ to $$$u_k$$$ along some path $$$[u_1, u_2, u_3, dots, u_k]$$$, which is not necessarily simple (for all $$$j$$$ from $$$1$$$ to $$$k-1$$$ of the city $$$u_j$$$ and $$$u_{j+1}$$$ are connected by road). When the King moves from one city to another, city heads exchange flags as a sign of their friendship. Example of moving the King along the route $$$[1, 4, 2, 6]$$$. The color of the vertex matches the color of the flag at that vertex. For aesthetic reasons, the King wants the flag color in the city $$$i$$$ to be equal to $$$d_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. Determine whether the King can choose some route and drive along it so that for each city the flag color in it turns out to be equal to the desired color $$$d_i$$$. Note that the King can choose (and drive) exactly one route. If yes, find the shortest possible route for the King. If the initial colors of the flags already match the King's requirements (i.e. $$$c_i=d_i$$$ for all $$$i$$$), then consider that the King makes a route of length $$$k=0$$$. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^5$$$) β the number of test cases to solve. The following are the cases. Each case begins with a line containing an integer $$$n$$$ ($$$2 le n le 2cdot10^5$$$) β the number of cities in Treeland. The following is a line of $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le 10^6$$$), where $$$c_i$$$ denotes the color of the flag at the $$$i$$$-th vertex before the King's journey. The following is a line of $$$n$$$ integers $$$d_1, d_2, dots, d_n$$$ ($$$1 le d_i le 10^6$$$), where $$$d_i$$$ denotes the required flag color at the $$$i$$$-th vertex after the completion of the King's journey. Further, in the $$$n-1$$$ line, the Treeland's roads are listed. Each road is given by a line containing two integers $$$x_j, y_j$$$ ($$$1 le x_j, y_j le n$$$) β numbers of cities that are connected by the $$$j$$$ th road. It is guaranteed that from every city you can get to any other by road (in other words, the system of cities and roads forms an undirected tree). The sum of all $$$n$$$ values u200bu200bfor all cases in one test does not exceed $$$2cdot10^5$$$. Output Print the answers to all cases in the order of their appearance in the input data. Each answer must begin with a line containing "Yes" (in the case of a positive answer) or "No" (in the case that the required route does not exist). In the case of a positive answer, the following line must contain an integer $$$k$$$ β the number of cities in the shortest possible route of the King. The next line should contain the required route $$$u_1, u_2, dots, u_k$$$ ($$$1 le u_i le n$$$). You can skip the line if $$$k=0$$$. | 2,500 | true | false | false | false | false | false | false | false | false | false | 4,622 |
1858E1 | This is an easy version of this problem. The only difference between the versions is that you have to solve the hard version in online mode. You can make hacks only if both versions of the problem are solved. You have an array $$$a$$$, which is initially empty. You need to process queries of the following types: + $$$x$$$xa0β add the integer $$$x$$$ to the end of the array $$$a$$$. - $$$k$$$xa0β remove the last $$$k$$$ numbers from the array $$$a$$$. !xa0β roll back the last active change (i.e., make the array $$$a$$$ the way it was before the change). In this problem, only queries of the first two types (+ and -) are considered as changes. ?xa0β find the number of distinct numbers in the array $$$a$$$. Input The first line contains an integer $$$q$$$ ($$$1 leq q leq 10^6$$$)xa0β the number of queries. The next $$$q$$$ lines contain the queries as described above. It is guaranteed that in the queries of the first type, $$$1 le x le 10^6$$$; in the queries of the second type, $$$k ge 1$$$ and $$$k$$$ does not exceed the current length of the array $$$a$$$; at the moment of the queries of the third type, there is at least one query of the first or of the second type that can be rolled back. It is also guaranteed that the number of queries of the fourth type (?) does not exceed $$$10^5$$$. Output For each query of the fourth type output one integerxa0β the number of distinct elements in array $$$a$$$ at the moment of query. Examples Input 10 + 1 + 2 + 2 ? ! + 3 - 2 ? + 1 ? Input 6 + 1 + 1000000 ? ! ! ? Note In the first example array $$$a$$$ changes as follows: 1. After the first query, $$$a=[1]$$$. 2. After the second query, $$$a=[1,2]$$$. 3. After the third query, $$$a=[1,2,2]$$$. 4. At the moment of the fourth query, there are $$$2$$$ distinct intergers in the array $$$a$$$: $$$1$$$ and $$$2$$$. 5. After the fifth query, $$$a=[1,2]$$$ (rolled back the change + 2). 6. After the sixth query, $$$a=[1,2,3]$$$. 7. After the seventh query, $$$a=[1]$$$. 8. At the moment of the eigth query, there is only one $$$1$$$ in the array $$$a$$$. 9. After the ninth query, $$$a=[1,1]$$$. 10. At the moment of the tenth query, there are only two $$$1$$$ in the array $$$a$$$. In the second example array $$$a$$$ changes as follows: 1. After the first query, $$$a=[1]$$$. 2. After the second query, $$$a=[1,1,000,000]$$$. 3. At the moment of the third query, there are $$$2$$$ distinct intergers in the array $$$a$$$: $$$1$$$ and $$$1,000,000$$$. 4. After the fourth query, $$$a=[1]$$$ (rolled back the change + 1000000). 5. After the fifth query, $$$a=[]$$$ (rolled back the change + 1). 6. At the moment of the sixth query, there are no integers in the array $$$a$$$, so the answer to this query is $$$0$$$. | 2,500 | false | false | false | false | true | false | true | false | false | false | 1,133 |
749B | Long time ago Alex created an interesting problem about parallelogram. The input data for this problem contained four integer points on the Cartesian plane, that defined the set of vertices of some non-degenerate (positive area) parallelogram. Points not necessary were given in the order of clockwise or counterclockwise traversal. Alex had very nice test for this problem, but is somehow happened that the last line of the input was lost and now he has only three out of four points of the original parallelogram. He remembers that test was so good that he asks you to restore it given only these three points. Input The input consists of three lines, each containing a pair of integer coordinates _x__i_ and _y__i_ (u2009-u20091000u2009β€u2009_x__i_,u2009_y__i_u2009β€u20091000). It's guaranteed that these three points do not lie on the same line and no two of them coincide. Output First print integer _k_xa0β the number of ways to add one new integer point such that the obtained set defines some parallelogram of positive area. There is no requirement for the points to be arranged in any special order (like traversal), they just define the set of vertices. Then print _k_ lines, each containing a pair of integerxa0β possible coordinates of the fourth point. Note If you need clarification of what parallelogram is, please check Wikipedia page: | 1,200 | false | false | false | false | false | true | true | false | false | false | 6,797 |
443A | Problem - 443A - 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 constructive algorithms implementation *800 No tag edit access β Contest materials | 800 | false | false | true | false | false | true | false | false | false | false | 8,065 |
1995B1 | This is the easy version of the problem. The only difference is that in this version, the flowers are specified by enumeration. A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $$$n$$$ flowers in the store, each of which is characterized by the number of petals, and a flower with $$$k$$$ petals costs $$$k$$$ coins. The girl has decided that the difference in the number of petals between any two flowers she will use in her bouquet should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has $$$m$$$ coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet? Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0β the number of test cases. This is followed by descriptions of the test cases. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 le n le 2 cdot 10^5, 1 le m le 10^{18}$$$)xa0β the number of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the number of petals of the $$$i$$$-th flower in the store. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot {10}^5$$$. Output For each test case, output a single integerxa0β the maximum possible number of petals in the bouquet that the girl can assemble while meeting all the conditions listed above. Example Input 5 5 10 1 1 2 2 3 8 20 4 2 7 5 6 1 1 1 8 100000 239 30 610 122 24 40 8 2 11 13 2 4 11 1 1 2 3 5 4 3 2 8 1033 206 206 206 207 207 207 207 1000 Note In the first test case, you can assemble a bouquet with $$$(1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2)$$$. The maximum over all valid bouquets not greater than $$$10$$$ is $$$7$$$ for $$$(2, 2, 3)$$$. In the third test case, you can assemble a bouquet with only one flower of any type, so the answer is $$$610$$$. In the fourth test case, you can assemble a bouquet with $$$(4, 4, 5)$$$, which gives you $$$13$$$ petals, and it is the maximum amount of petals that the girl can buy. | 1,100 | false | true | false | false | false | false | true | true | true | false | 296 |
1861B | You are given two strings $$$a$$$ and $$$b$$$ of equal length, consisting of only characters 0 and/or 1; both strings start with character 0 and end with character 1. You can perform the following operation any number of times (possibly zero): choose one of the strings and two equal characters in it; then turn all characters between them into those characters. Formally, you choose one of these two strings (let the chosen string be $$$s$$$), then pick two integers $$$l$$$ and $$$r$$$ such that $$$1 le l < r le s$$$ and $$$s_l = s_r$$$, then replace every character $$$s_i$$$ such that $$$l < i < r$$$ with $$$s_l$$$. For example, if the chosen string is 010101, you can transform it into one of the following strings by applying one operation: 000101 if you choose $$$l = 1$$$ and $$$r = 3$$$; 000001 if you choose $$$l = 1$$$ and $$$r = 5$$$; 010001 if you choose $$$l = 3$$$ and $$$r = 5$$$; 010111 if you choose $$$l = 4$$$ and $$$r = 6$$$; 011111 if you choose $$$l = 2$$$ and $$$r = 6$$$; 011101 if you choose $$$l = 2$$$ and $$$r = 4$$$. You have to determine if it's possible to make the given strings equal by applying this operation any number of times. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2000$$$) xa0β the number of test cases. Each test case consists of two lines: the first line contains the string $$$a$$$ ($$$2 le a le 5000$$$), consisting of only characters 0 and/or 1. the second line contains the string $$$b$$$ ($$$2 le b le 5000$$$), consisting of only characters 0 and/or 1. Additional constraints on the input: in each test case, $$$a = b$$$ (the strings have equal length); in each test case, both strings start with 0 and end with 1; the total length of all strings $$$a$$$ in all test cases does not exceed $$$5000$$$. Output For each test case, print YES if it is possible to make the strings equal. Otherwise, print NO. You can print each letter in any register. Example Input 7 01010001 01110101 01001 01001 000101 010111 00001 01111 011 001 001001 011011 010001 011011 Output YES YES YES NO NO NO YES Note In the first test case, we can perform the following operations: 1. choose the string $$$a$$$, $$$l = 2$$$, $$$r = 4$$$; after this operation, $$$a$$$ is 01110001, $$$b$$$ is 01110101; 2. choose the string $$$b$$$, $$$l = 5$$$, $$$r = 7$$$; after this operation, $$$a$$$ is 01110001, $$$b$$$ is 01110001. In the second test case, the strings are already equal. In the third test case, we can perform the following operations: 1. choose the string $$$a$$$, $$$l = 4$$$, $$$r = 6$$$; after this operation, $$$a$$$ is 000111, $$$b$$$ is 010111; 2. choose the string $$$b$$$, $$$l = 1$$$, $$$r = 3$$$; after this operation, $$$a$$$ is 000111, $$$b$$$ is 000111; In the fourth and fifth test cases, it's impossible to make the given strings equal. | 1,000 | false | true | false | true | false | true | false | false | false | false | 1,118 |
1810H | You are given a multiset $$$S$$$. Initially, $$$S = {1,2,3, ldots, n}$$$. You will perform the following operation $$$n-1$$$ times. Choose the largest number $$$S_{ ext{max}}$$$ in $$$S$$$ and the smallest number $$$S_{ ext{min}}$$$ in $$$S$$$. Remove the two numbers from $$$S$$$, and add $$$S_{ ext{max}} - S_{ ext{min}}$$$ into $$$S$$$. It's easy to show that there will be exactly one number left after $$$n-1$$$ operations. Output that number. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) β the number of test cases. Their description follows. For each test case, one single line contains a single integer $$$n$$$ ($$$2 le n le 10^9$$$) β the initial size of the multiset $$$S$$$. Output For each test case, output an integer denoting the only number left after $$$n-1$$$ operations. Note We show how the multiset $$$S$$$ changes for $$$n=4$$$. Operation $$$1$$$: $$$S={1,2,3,4}$$$, remove $$$4$$$, $$$1$$$, add $$$3$$$. Operation $$$2$$$: $$$S={2,3,3}$$$, remove $$$3$$$, $$$2$$$, add $$$1$$$. Operation $$$3$$$: $$$S={1,3}$$$, remove $$$3$$$, $$$1$$$, add $$$2$$$. Final: $$$S = {2}$$$. Thus, the answer for $$$n = 4$$$ is $$$2$$$. | 2,900 | true | false | false | false | false | false | false | false | false | false | 1,406 |
641D | Little Artyom decided to study probability theory. He found a book with a lot of nice exercises and now wants you to help him with one of them. Consider two dices. When thrown each dice shows some integer from 1 to _n_ inclusive. For each dice the probability of each outcome is given (of course, their sum is 1), and different dices may have different probability distributions. We throw both dices simultaneously and then calculate values _max_(_a_,u2009_b_) and _min_(_a_,u2009_b_), where _a_ is equal to the outcome of the first dice, while _b_ is equal to the outcome of the second dice. You don't know the probability distributions for particular values on each dice, but you know the probability distributions for _max_(_a_,u2009_b_) and _min_(_a_,u2009_b_). That is, for each _x_ from 1 to _n_ you know the probability that _max_(_a_,u2009_b_) would be equal to _x_ and the probability that _min_(_a_,u2009_b_) would be equal to _x_. Find any valid probability distribution for values on the dices. It's guaranteed that the input data is consistent, that is, at least one solution exists. Input First line contains the integer _n_ (1u2009β€u2009_n_u2009β€u2009100u2009000)xa0β the number of different values for both dices. Second line contains an array consisting of _n_ real values with up to 8 digits after the decimal point xa0β probability distribution for _max_(_a_,u2009_b_), the _i_-th of these values equals to the probability that _max_(_a_,u2009_b_)u2009=u2009_i_. It's guaranteed that the sum of these values for one dice is 1. The third line contains the description of the distribution _min_(_a_,u2009_b_) in the same format. Output Output two descriptions of the probability distribution for _a_ on the first line and for _b_ on the second line. The answer will be considered correct if each value of max(_a_,u2009_b_) and min(_a_,u2009_b_) probability distribution values does not differ by more than 10u2009-u20096 from ones given in input. Also, probabilities should be non-negative and their sums should differ from 1 by no more than 10u2009-u20096. Examples Input 3 0.125 0.25 0.625 0.625 0.25 0.125 Output 0.25 0.25 0.5 0.5 0.25 0.25 | 2,400 | true | false | true | true | false | false | false | false | false | false | 7,248 |
1873C | A $$$10 imes 10$$$ target is made out of five "rings" as shown. Each ring has a different point value: the outermost ringxa0β 1 point, the next ringxa0β 2 points, ..., the center ringxa0β 5 points. Vlad fired several arrows at the target. Help him determine how many points he got. Input The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0β the number of test cases. Each test case consists of 10 lines, each containing 10 characters. Each character in the grid is either $$$ exttt{X}$$$ (representing an arrow) or $$$ exttt{.}$$$ (representing no arrow). Output For each test case, output a single integerxa0β the total number of points of the arrows. Example Input 4 X......... .......... .......X.. .....X.... ......X... .......... .........X ..X....... .......... .........X .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... ....X..... .......... .......... .......... .......... .......... XXXXXXXXXX XXXXXXXXXX XXXXXXXXXX XXXXXXXXXX XXXXXXXXXX XXXXXXXXXX XXXXXXXXXX XXXXXXXXXX XXXXXXXXXX XXXXXXXXXX Note In the first test case, there are three arrows on the outer ring worth 1 point each, two arrows on the ring worth 3 points each, and two arrows on the ring worth 4 points each. The total score is $$$3 imes 1 + 2 imes 3 + 2 imes 4 = 17$$$. In the second test case, there aren't any arrows, so the score is $$$0$$$. | 800 | true | false | true | false | false | false | false | false | false | false | 1,042 |
1619G | Polycarp is very fond of playing the game Minesweeper. Recently he found a similar game and there are such rules. There are mines on the field, for each the coordinates of its location are known ($$$x_i, y_i$$$). Each mine has a lifetime in seconds, after which it will explode. After the explosion, the mine also detonates all mines vertically and horizontally at a distance of $$$k$$$ (two perpendicular lines). As a result, we get an explosion on the field in the form of a "plus" symbol ('+'). Thus, one explosion can cause new explosions, and so on. Also, Polycarp can detonate anyone mine every second, starting from zero seconds. After that, a chain reaction of explosions also takes place. Mines explode instantly and also instantly detonate other mines according to the rules described above. Polycarp wants to set a new record and asks you to help him calculate in what minimum number of seconds all mines can be detonated. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases in the test. An empty line is written in front of each test suite. Next comes a line that contains integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le k le 10^9$$$) β the number of mines and the distance that hit by mines during the explosion, respectively. Then $$$n$$$ lines follow, the $$$i$$$-th of which describes the $$$x$$$ and $$$y$$$ coordinates of the $$$i$$$-th mine and the time until its explosion ($$$-10^9 le x, y le 10^9$$$, $$$0 le timer le 10^9$$$). It is guaranteed that all mines have different coordinates. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2 cdot 10^5$$$. Output Print $$$t$$$ lines, each of the lines must contain the answer to the corresponding set of input data xa0β the minimum number of seconds it takes to explode all the mines. Example Input 3 5 0 0 0 1 0 1 4 1 0 2 1 1 3 2 2 9 5 2 0 0 1 0 1 4 1 0 2 1 1 3 2 2 9 6 1 1 -1 3 0 -1 9 0 1 7 -1 0 1 -1 1 9 -1 -1 7 Note Picture from examples First example: $$$0$$$ second: we explode a mine at the cell $$$(2, 2)$$$, it does not detonate any other mine since $$$k=0$$$. $$$1$$$ second: we explode the mine at the cell $$$(0, 1)$$$, and the mine at the cell $$$(0, 0)$$$ explodes itself. $$$2$$$ second: we explode the mine at the cell $$$(1, 1)$$$, and the mine at the cell $$$(1, 0)$$$ explodes itself. Second example: $$$0$$$ second: we explode a mine at the cell $$$(2, 2)$$$ we get: $$$1$$$ second: the mine at coordinate $$$(0, 0)$$$ explodes and since $$$k=2$$$ the explosion detonates mines at the cells $$$(0, 1)$$$ and $$$(1, 0)$$$, and their explosions detonate the mine at the cell $$$(1, 1)$$$ and there are no mines left on the field. | 2,000 | false | true | false | false | false | false | false | true | true | false | 2,529 |
143A | Vasilisa the Wise from the Kingdom of Far Far Away got a magic box with a secret as a present from her friend Hellawisa the Wise from the Kingdom of A Little Closer. However, Vasilisa the Wise does not know what the box's secret is, since she cannot open it again. She hopes that you will help her one more time with that. The box's lock looks as follows: it contains 4 identical deepenings for gems as a 2u2009Γu20092 square, and some integer numbers are written at the lock's edge near the deepenings. The example of a lock is given on the picture below. The box is accompanied with 9 gems. Their shapes match the deepenings' shapes and each gem contains one number from 1 to 9 (each number is written on exactly one gem). The box will only open after it is decorated with gems correctly: that is, each deepening in the lock should be filled with exactly one gem. Also, the sums of numbers in the square's rows, columns and two diagonals of the square should match the numbers written at the lock's edge. For example, the above lock will open if we fill the deepenings with gems with numbers as is shown on the picture below. Now Vasilisa the Wise wants to define, given the numbers on the box's lock, which gems she should put in the deepenings to open the box. Help Vasilisa to solve this challenging task. Input The input contains numbers written on the edges of the lock of the box. The first line contains space-separated integers _r_1 and _r_2 that define the required sums of numbers in the rows of the square. The second line contains space-separated integers _c_1 and _c_2 that define the required sums of numbers in the columns of the square. The third line contains space-separated integers _d_1 and _d_2 that define the required sums of numbers on the main and on the side diagonals of the square (1u2009β€u2009_r_1,u2009_r_2,u2009_c_1,u2009_c_2,u2009_d_1,u2009_d_2u2009β€u200920). Correspondence between the above 6 variables and places where they are written is shown on the picture below. For more clarifications please look at the second sample test that demonstrates the example given in the problem statement. Output Print the scheme of decorating the box with stones: two lines containing two space-separated integers from 1 to 9. The numbers should be pairwise different. If there is no solution for the given lock, then print the single number "-1" (without the quotes). If there are several solutions, output any. Note Pay attention to the last test from the statement: it is impossible to open the box because for that Vasilisa the Wise would need 4 identical gems containing number "5". However, Vasilisa only has one gem with each number from 1 to 9. | 1,000 | true | false | false | false | false | false | true | false | false | false | 9,302 |
1705C | One night, Mark realized that there is an essay due tomorrow. He hasn't written anything yet, so Mark decided to randomly copy-paste substrings from the prompt to make the essay. More formally, the prompt is a string $$$s$$$ of initial length $$$n$$$. Mark will perform the copy-pasting operation $$$c$$$ times. Each operation is described by two integers $$$l$$$ and $$$r$$$, which means that Mark will append letters $$$s_l s_{l+1} ldots s_r$$$ to the end of string $$$s$$$. Note that the length of $$$s$$$ increases after this operation. Of course, Mark needs to be able to see what has been written. After copying, Mark will ask $$$q$$$ queries: given an integer $$$k$$$, determine the $$$k$$$-th letter of the final string $$$s$$$. Input The first line contains a single integer $$$t$$$ ($$$1leq tleq 1000$$$) β the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$c$$$, and $$$q$$$ ($$$1leq nleq 2cdot 10^5$$$, $$$1leq cleq 40$$$, and $$$1leq qleq 10^4$$$) β the length of the initial string $$$s$$$, the number of copy-pasting operations, and the number of queries, respectively. The second line of each test case contains a single string $$$s$$$ of length $$$n$$$. It is guaranteed that $$$s$$$ only contains lowercase English letters. The following $$$c$$$ lines describe the copy-pasting operation. Each line contains two integers $$$l$$$ and $$$r$$$ ($$$1leq lleq rleq 10^{18}$$$). It is also guaranteed that $$$r$$$ does not exceed the current length of $$$s$$$. The last $$$q$$$ lines of each test case describe the queries. Each line contains a single integer $$$k$$$ ($$$1leq kleq 10^{18}$$$). It is also guaranteed that $$$k$$$ does not exceed the final length of $$$s$$$. It is guaranteed that the sum of $$$n$$$ and $$$q$$$ across all test cases does not exceed $$$2cdot 10^5$$$ and $$$10^4$$$, respectively. Output For each query, print the $$$k$$$-th letter of the final string $$$s$$$. Example Input 2 4 3 3 mark 1 4 5 7 3 8 1 10 12 7 3 3 creamii 2 3 3 4 2 9 9 11 12 Note In the first test case, the copy-paste process is as follows. The first step is pasting string $$$ exttt{mark}$$$ at the end, yielding the string $$$ exttt{mark}color{red}{ exttt{mark}}$$$. The second step is pasting string $$$ exttt{mar}$$$ at the end, yielding the string $$$ exttt{markmark}color{red}{ exttt{mar}}$$$. The third step is pasting string $$$ exttt{rkmark}$$$ at the end, yielding the string $$$ exttt{markmarkmar}color{red}{ exttt{rkmark}}$$$. In the second test case, the copy-paste process is as follows. The first step is pasting string $$$ exttt{re}$$$ at the end, yielding the string $$$ exttt{creamii}color{red}{ exttt{re}}$$$. The second step is pasting string $$$ exttt{ea}$$$ at the end, yielding the string $$$ exttt{creamiire}color{red}{ exttt{ea}}$$$. The third step is pasting string $$$ exttt{reamiire}$$$ at the end, yielding the string $$$ exttt{creamiireea}color{red}{ exttt{reamiire}}$$$. | 1,400 | false | false | true | false | false | false | true | false | false | false | 2,044 |
508E | Notice that the memory limit is non-standard. Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length 2_n_. Unlike Arthur, Sasha understood the topic very badly, and broke Arthur's favorite correct bracket sequence just to spite him. All Arthur remembers about his favorite sequence is for each opening parenthesis ('(') the approximate distance to the corresponding closing one (')'). For the _i_-th opening bracket he remembers the segment [_l__i_,u2009_r__i_], containing the distance to the corresponding closing bracket. Formally speaking, for the _i_-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment [_l__i_,u2009_r__i_]. Help Arthur restore his favorite correct bracket sequence! Input The first line contains integer _n_ (1u2009β€u2009_n_u2009β€u2009600), the number of opening brackets in Arthur's favorite correct bracket sequence. Next _n_ lines contain numbers _l__i_ and _r__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009<u20092_n_), representing the segment where lies the distance from the _i_-th opening bracket and the corresponding closing one. The descriptions of the segments are given in the order in which the opening brackets occur in Arthur's favorite sequence if we list them from left to right. Output If it is possible to restore the correct bracket sequence by the given data, print any possible choice. If Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line "IMPOSSIBLE" (without the quotes). | 2,200 | false | true | false | true | false | false | false | false | false | false | 7,807 |
1821E | A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); bracket sequences ")(", "(" and ")" are not. You are given a regular bracket sequence. In one move, you can remove a pair of adjacent brackets such that the left one is an opening bracket and the right one is a closing bracket. Then concatenate the resulting parts without changing the order. The cost of this move is the number of brackets to the right of the right bracket of this pair. The cost of the regular bracket sequence is the smallest total cost of the moves required to make the sequence empty. Actually, you are not removing any brackets. Instead, you are given a regular bracket sequence and an integer $$$k$$$. You can perform the following operation at most $$$k$$$ times: extract some bracket from the sequence and insert it back at any position (between any two brackets, at the start or at the end; possibly, at the same place it was before). After all operations are performed, the bracket sequence has to be regular. What is the smallest possible cost of the resulting regular bracket sequence? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of testcases. The first line of each testcase contains a single integer $$$k$$$ ($$$0 le k le 5$$$)xa0β the maximum number of operations you can perform. The second line contains a non-empty regular bracket sequence, it consists only of characters '(' and ')'. The total length of the regular bracket sequences over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0β the smallest possible cost of the regular bracket sequence after you perform at most $$$k$$$ operations on it. Example Input 7 0 () 0 (()) 1 (()) 5 () 1 (()()(())) 2 ((())()(()())((()))) 3 ((())()(()())((()))) | 2,100 | false | true | false | true | false | false | true | false | true | false | 1,353 |
1934D1 | This is the solo version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the game version. You can solve and get points for both versions independently. You can make hacks only if both versions of the problem are solved. Given an integer variable $$$x$$$ with the initial value of $$$n$$$. A single break operation consists of the following steps: Choose a value $$$y$$$ such that $$$0 lt y lt x$$$ and $$$0 lt (x oplus y) lt x$$$. Update $$$x$$$ by either setting $$$x = y$$$ or setting $$$x = x oplus y$$$. Determine whether it is possible to transform $$$x$$$ into $$$m$$$ using a maximum of $$$63$$$ break operations. If it is, provide the sequence of operations required to achieve $$$x = m$$$. You don't need to minimize the number of operations. Here $$$oplus$$$ denotes the xa0β the number of test cases. Each test case consists of a single line containing two integers $$$n$$$ and $$$m$$$ ($$$1 leq m lt n leq 10^{18}$$$)xa0β the initial value of $$$x$$$ and the target value of $$$x$$$. Output For each test case, output your answer in the following format. If it is not possible to achieve $$$m$$$ in $$$63$$$ operations, print $$$-1$$$. Otherwise, The first line should contain $$$k$$$ ($$$1 leq k leq 63$$$)xa0β where $$$k$$$ is the number of operations required. The next line should contain $$$k+1$$$ integersxa0β the sequence where variable $$$x$$$ changes after each break operation. The $$$1$$$-st and $$$k+1$$$-th integers should be $$$n$$$ and $$$m$$$, respectively. Example Input 3 7 3 4 2 481885160128643072 45035996273704960 Output 1 7 3 -1 3 481885160128643072 337769972052787200 49539595901075456 45035996273704960 Note In the first test case $$$n = 7$$$, for the first operation $$$x = 7$$$ if we choose $$$y = 3$$$ then $$$(7 oplus 3) lt 7$$$, hence we can update $$$x$$$ with $$$3$$$ which is equal to $$$m$$$. In the second test case $$$n = 4$$$, for the first operation $$$x = 4$$$. If we choose: $$$y = 1$$$ then $$$(4 oplus 1) gt 4$$$ $$$y = 2$$$ then $$$(4 oplus 2) gt 4$$$ $$$y = 3$$$ then $$$(4 oplus 3) gt 4$$$ Hence we can't do the first operation and it is impossible to make $$$x = 2$$$. | 2,100 | false | true | false | false | false | true | false | false | false | false | 677 |
165B | One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of _n_ lines of code. Vasya is already exhausted, so he works like that: first he writes _v_ lines of code, drinks a cup of tea, then he writes as much as lines, drinks another cup of tea, then he writes lines and so on: , , , ... The expression is regarded as the integral part from dividing number _a_ by number _b_. The moment the current value equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished. Vasya is wondering, what minimum allowable value _v_ can take to let him write not less than _n_ lines of code before he falls asleep. Input The input consists of two integers _n_ and _k_, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1u2009β€u2009_n_u2009β€u2009109, 2u2009β€u2009_k_u2009β€u200910. Output Print the only integer β the minimum value of _v_ that lets Vasya write the program in one night. Note In the first sample the answer is _v_u2009=u20094. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4u2009+u20092u2009+u20091u2009=u20097 lines in a night and complete the task. In the second sample the answer is _v_u2009=u200954. Vasya writes the code in the following portions: 54, 6. The total sum is 54u2009+u20096u2009=u200960, that's even more than _n_u2009=u200959. | 1,500 | false | false | true | false | false | false | false | true | false | false | 9,204 |
1740C | There are $$$n$$$ bricks numbered from $$$1$$$ to $$$n$$$. Brick $$$i$$$ has a weight of $$$a_i$$$. Pak Chanek has $$$3$$$ bags numbered from $$$1$$$ to $$$3$$$ that are initially empty. For each brick, Pak Chanek must put it into one of the bags. After this, each bag must contain at least one brick. After Pak Chanek distributes the bricks, Bu Dengklek will take exactly one brick from each bag. Let $$$w_j$$$ be the weight of the brick Bu Dengklek takes from bag $$$j$$$. The score is calculated as $$$w_1 - w_2 + w_2 - w_3$$$, where $$$x$$$ denotes the absolute value of $$$x$$$. It is known that Bu Dengklek will take the bricks in such a way that minimises the score. What is the maximum possible final score if Pak Chanek distributes the bricks optimally? Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 2 cdot 10^4$$$) β the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$3 leq n leq 2 cdot 10^5$$$) β the number of bricks. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) β the weights of the bricks. 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 a line containing an integer representing the maximum possible final score if Pak Chanek distributes the bricks optimally. Example Input 3 5 3 1 5 2 3 4 17 8 19 45 8 265 265 265 265 265 265 265 265 Note In the first test case, one way of achieving a final score of $$$6$$$ is to do the following: Put bricks $$$1$$$, $$$4$$$, and $$$5$$$ into bag $$$1$$$. Put brick $$$3$$$ into bag $$$2$$$. Put brick $$$2$$$ into bag $$$3$$$. If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is: Take brick $$$5$$$ from bag $$$1$$$. Take brick $$$3$$$ from bag $$$2$$$. Take brick $$$2$$$ from bag $$$3$$$. The score is $$$a_5 - a_3 + a_3 - a_2 = 3 - 5 + 5 - 1 = 6$$$. It can be shown that Bu Dengklek cannot get a smaller score from this distribution. It can be shown that there is no other distribution that results in a final score bigger than $$$6$$$. | 1,400 | false | true | false | false | false | true | false | false | true | false | 1,845 |
1799D1 | This is an easy version of the problem. The constraints of $$$t$$$, $$$n$$$, $$$k$$$ are the only difference between versions. You have a device with two CPUs. You also have $$$k$$$ programs, numbered $$$1$$$ through $$$k$$$, that you can run on the CPUs. The $$$i$$$-th program ($$$1 le i le k$$$) takes $$$cold_i$$$ seconds to run on some CPU. However, if the last program we ran on this CPU was also program $$$i$$$, it only takes $$$hot_i$$$ seconds ($$$hot_i le cold_i$$$). Note that this only applies if we run program $$$i$$$ multiple times consecutively xa0β if we run program $$$i$$$, then some different program, then program $$$i$$$ again, it will take $$$cold_i$$$ seconds the second time. You are given a sequence $$$a_1, a_2, ldots, a_n$$$ of length $$$n$$$, consisting of integers from $$$1$$$ to $$$k$$$. You need to use your device to run programs $$$a_1, a_2, ldots, a_n$$$ in sequence. For all $$$2 le i le n$$$, you cannot start running program $$$a_i$$$ until program $$$a_{i - 1}$$$ has completed. Find the minimum amount of time needed to run all programs $$$a_1, a_2, ldots, a_n$$$ in sequence. Input Input consists of multiple test cases. The first line contains a single integer $$$t$$$, the number of test cases ($$$1 le t le 5000$$$). The first line of each test case contains $$$n$$$ and $$$k$$$ ($$$1 le n, k le 5000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le k$$$). The third line of each test case contains $$$k$$$ integers $$$cold_1, cold_2, ldots, cold_k$$$ ($$$1 le cold_i le 10^9$$$). The fourth line of each test case contains $$$k$$$ integers $$$hot_1, hot_2, ldots, hot_k$$$ ($$$1 le hot_i le cold_i$$$). It is guaranteed the sum of $$$n$$$ and the sum of $$$k$$$ over all test cases do not exceed $$$5000$$$. Output For each test case, print the minimum time needed to run all programs in the given order. Example Input 9 3 2 1 2 2 3 2 2 1 4 2 1 2 1 2 5 3 2 1 4 3 1 2 3 1 100 100 100 1 1 1 5 2 2 1 2 1 1 65 45 54 7 5 3 1 3 2 1 2 2 2 2 1 1 1 5 1 1 1 1 1 1 1000000000 999999999 5 6 1 6 1 4 1 3 6 4 1 4 5 1 1 1 1 4 1 1 3 3 4 5 6 1 2 3 8 3 3 3 3 1 2 3 2 1 10 10 8 10 10 5 Output 6 11 301 225 8 4999999996 11 6 63 Note In the first test case, we can do the following: Run program $$$a_1 = 1$$$ on CPU $$$1$$$. It takes $$$cold_1 = 3$$$ seconds to run. Run program $$$a_2 = 2$$$ on CPU $$$2$$$. It takes $$$cold_2 = 2$$$ seconds to run. Run program $$$a_3 = 2$$$ on CPU $$$2$$$. The last program run on this CPU was also program $$$2$$$, so it takes $$$hot_2 = 1$$$ second to run. In total, we need $$$3 + 2 + 1 = 6$$$ seconds to run them all. We can show this is optimal. In the second test case, we can use do the following: Run program $$$a_1 = 1$$$ on CPU $$$1$$$. It takes $$$cold_1 = 5$$$ seconds to run. Run program $$$a_2 = 2$$$ on CPU $$$2$$$. It takes $$$cold_2 = 3$$$ seconds to run. Run program $$$a_3 = 1$$$ on CPU $$$1$$$. The last program run on this CPU was also program $$$1$$$, so it takes $$$hot_1 = 2$$$ seconds to run. Run program $$$a_4 = 2$$$ on CPU $$$2$$$. The last program run on this CPU was also program $$$2$$$, so it takes $$$hot_2 = 1$$$ second to run. In total, we need $$$5 + 3 + 2 + 1 = 11$$$ seconds. We can show this is optimal. | 1,900 | false | false | false | true | false | false | false | false | false | false | 1,481 |
241E | LiLand is a country, consisting of _n_ cities. The cities are numbered from 1 to _n_. The country is well known because it has a very strange transportation system. There are many one-way flights that make it possible to travel between the cities, but the flights are arranged in a way that once you leave a city you will never be able to return to that city again. Previously each flight took exactly one hour, but recently Lily has become the new manager of transportation system and she wants to change the duration of some flights. Specifically, she wants to change the duration of some flights to exactly 2 hours in such a way that all trips from city 1 to city _n_ take the same time regardless of their path. Your task is to help Lily to change the duration of flights. Input First line of the input contains two integer numbers _n_ and _m_ (2u2009β€u2009_n_u2009β€u20091000;xa01u2009β€u2009_m_u2009β€u20095000) specifying the number of cities and the number of flights. Each of the next _m_ lines contains two integers _a__i_ and _b__i_ (1u2009β€u2009_a__i_u2009<u2009_b__i_u2009β€u2009_n_) specifying a one-directional flight from city _a__i_ to city _b__i_. It is guaranteed that there exists a way to travel from city number 1 to city number _n_ using the given flights. It is guaranteed that there is no sequence of flights that forms a cyclical path and no two flights are between the same pair of cities. Output If it is impossible for Lily to do her task, print "No" (without quotes) on the only line of the output. Otherwise print "Yes" (without quotes) on the first line of output, then print an integer _ans__i_ (1u2009β€u2009_ans__i_u2009β€u20092) to each of the next _m_ lines being the duration of flights in new transportation system. You should print these numbers in the order that flights are given in the input. If there are multiple solutions for the input, output any of them. Examples Input 5 6 1 2 2 3 3 5 1 4 4 5 1 3 | 2,600 | false | false | false | false | false | false | false | false | false | true | 8,871 |
822F | The second semester starts at the University of Pavlopolis. After vacation in ViΔkopolis Noora needs to return to Pavlopolis and continue her study. Sometimes (or quite often) there are teachers who do not like you. Incidentally Noora also has one such teacher. His name is Yury Dmitrievich and he teaches graph theory. Yury Dmitrievich doesn't like Noora, so he always gives the girl the most difficult tasks. So it happened this time. The teacher gives Noora a tree with _n_ vertices. Vertices are numbered with integers from 1 to _n_. The length of all the edges of this tree is 1. Noora chooses a set of simple paths that pairwise don't intersect in edges. However each vertex should belong to at least one of the selected path. For each of the selected paths, the following is done: 1. We choose exactly one edge (_u_,u2009_v_) that belongs to the path. 2. On the selected edge (_u_,u2009_v_) there is a point at some selected distance _x_ from the vertex _u_ and at distance 1u2009-u2009_x_ from vertex _v_. But the distance _x_ chosen by Noora arbitrarily, i. e. it can be different for different edges. 3. One of the vertices _u_ or _v_ is selected. The point will start moving to the selected vertex. Let us explain how the point moves by example. Suppose that the path consists of two edges (_v_1,u2009_v_2) and (_v_2,u2009_v_3), the point initially stands on the edge (_v_1,u2009_v_2) and begins its movement to the vertex _v_1. Then the point will reach _v_1, then "turn around", because the end of the path was reached, further it will move in another direction to vertex _v_2, then to vertex _v_3, then "turn around" again, then move to _v_2 and so on. The speed of the points is 1 edge per second. For example, for 0.5 second the point moves to the length of the half of an edge. A stopwatch is placed at each vertex of the tree. The time that the stopwatches indicate at start time is 0 seconds. Then at the starting moment of time, all points simultaneously start moving from the selected positions to selected directions along the selected paths, and stopwatches are simultaneously started. When one of the points reaches the vertex _v_, the stopwatch at the vertex _v_ is automatically reset, i.e. it starts counting the time from zero. Denote by _res__v_ the maximal time that the stopwatch at the vertex _v_ will show if the point movement continues infinitely. Noora is asked to select paths and points on them so that _res_1 is as minimal as possible. If there are several solutions to do this, it is necessary to minimize _res_2, then _res_3, _res_4,u2009...,u2009_res__n_. Help Noora complete the teacher's task. For the better understanding of the statement, see the explanation for the example. Input The first line contains single integer _n_ (2u2009β€u2009_n_u2009β€u2009100) β number of vertices in the given tree. Each of next _n_u2009-u20091 lines contains two integers _u_ and _v_ (1u2009β€u2009_u_,u2009_v_u2009β€u2009_n_,u2009_u_u2009β u2009_v_) β vertices connected by an edge. Guaranteed that input defines a valid tree. Note Consider an example. In starting moment of time points are located as following: The first path is highlighted in red, the second in blue, green circles represent chosen points, and brown numbers inside vertices β current time at stopwatch. Purple arrows represent direction in which points will move. In 0.(3) seconds points will be located in following way (before stopwatch reset): After stopwatch reset: In 1.0 second after the start of moving: In 1.(3) seconds after the start of moving (after stopwatch reset): Finally, in 2 seconds after the start of moving points return to their initial positions. This process will continue infinitely. | 2,500 | false | false | false | false | false | true | false | false | false | false | 6,463 |
1769D3 | Π ΡΡΠΎΠΉ Π²Π΅ΡΡΠΈΠΈ Π·Π°Π΄Π°ΡΠΈ Π½ΡΠΆΠ½ΠΎ Π½Π°ΠΉΡΠΈ $$$26$$$ ΡΠ°ΡΠΊΠ»Π°Π΄ΠΎΠ² Ρ ΡΠ°Π·Π»ΠΈΡΠ½ΡΠΌΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΡΠΌΠΈ Π²Π°ΠΆΠ½ΠΎΡΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Ρ
ΠΎΠ΄Π°. ΠΠ»ΠΈΡΠ° ΠΈ ΠΠΎΠ± ΡΠ΅ΡΠΈΠ»ΠΈ ΡΡΠ³ΡΠ°ΡΡ Π² ΠΊΠ°ΡΡΠΎΡΠ½ΡΡ ΠΈΠ³ΡΡ Β«ΠΠ΅Π²ΡΡΠΊΠ°Β». ΠΠΎΠΆΠ°Π»ΡΠΉΡΡΠ°, Π²Π½ΠΈΠΌΠ°ΡΠ΅Π»ΡΠ½ΠΎ ΠΏΡΠΎΡΠΈΡΠ°ΠΉΡΠ΅ ΡΡΠ»ΠΎΠ²ΠΈΠ΅ Π·Π°Π΄Π°ΡΠΈ, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΏΡΠ°Π²ΠΈΠ»Π° ΠΌΠΎΠ³ΡΡ ΠΎΡΠ»ΠΈΡΠ°ΡΡΡΡ ΠΎΡ ΠΈΠ·Π²Π΅ΡΡΠ½ΡΡ
Π²Π°ΠΌ. ΠΠ»Ρ ΠΈΠ³ΡΡ Π½ΡΠΆΠ½Π° ΡΡΠ°Π½Π΄Π°ΡΡΠ½Π°Ρ ΠΊΠΎΠ»ΠΎΠ΄Π° ΠΈΠ· $$$36$$$ ΠΊΠ°ΡΡxa0β ΠΏΠΎ Π΄Π΅Π²ΡΡΡ ΠΊΠ°ΡΡ (ΠΎΡ ΡΠ΅ΡΡΡΡΠΊΠΈ Π΄ΠΎ ΡΡΠ·Π°) ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ· ΡΠ΅ΡΡΡΡΡ
ΠΌΠ°ΡΡΠ΅ΠΉ (ΡΡΠ΅ΡΡ, Π±ΡΠ±Π½Ρ, ΠΏΠΈΠΊΠΈ ΠΈ ΡΠ΅ΡΠ²ΠΈ). ΠΠ°ΡΡΡ ΠΏΠΎ Π΄ΠΎΡΡΠΎΠΈΠ½ΡΡΠ²Ρ ΠΎΡ ΠΌΠ»Π°Π΄ΡΠ΅ΠΉ ΠΊ ΡΡΠ°ΡΡΠ΅ΠΉ ΠΈΠ΄ΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ: ΡΠ΅ΡΡΡΡΠΊΠ°, ΡΠ΅ΠΌΡΡΠΊΠ°, Π²ΠΎΡΡΠΌΡΡΠΊΠ°, Π΄Π΅Π²ΡΡΠΊΠ°, Π΄Π΅ΡΡΡΠΊΠ°, Π²Π°Π»Π΅Ρ, Π΄Π°ΠΌΠ°, ΠΊΠΎΡΠΎΠ»Ρ, ΡΡΠ·. ΠΠ΅ΡΠ΅Π΄ ΠΈΠ³ΡΠΎΠΉ ΠΊΠΎΠ»ΠΎΠ΄Π° ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠΈΠ²Π°Π΅ΡΡΡ, ΠΈ ΠΊΠ°ΠΆΠ΄ΠΎΠΌΡ ΠΈΠ³ΡΠΎΠΊΡ ΡΠ°Π·Π΄Π°ΡΡΡΡ ΠΏΠΎ $$$18$$$ ΠΊΠ°ΡΡ. ΠΠ°ΡΡΡ Π½ΡΠΆΠ½ΠΎ Π²ΡΠΊΠ»Π°Π΄ΡΠ²Π°ΡΡ ΠΈΠ· ΡΡΠΊΠΈ Π½Π° ΡΡΠΎΠ» ΠΏΠΎ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ½Π½ΡΠΌ ΠΏΡΠ°Π²ΠΈΠ»Π°ΠΌ. ΠΡΠΈΠ³ΡΡΠ²Π°Π΅Ρ ΠΈΠ³ΡΠΎΠΊ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΠ΅ΡΠ²ΡΠΌ Π²ΡΠ»ΠΎΠΆΠΈΡ Π²ΡΠ΅ ΠΊΠ°ΡΡΡ ΠΈΠ· ΡΠ²ΠΎΠ΅ΠΉ ΡΡΠΊΠΈ. ΠΠ³ΡΠΎΠΊΠΈ Ρ
ΠΎΠ΄ΡΡ ΠΏΠΎ ΠΎΡΠ΅ΡΠ΅Π΄ΠΈ. Π₯ΠΎΠ΄ ΠΈΠ³ΡΠΎΠΊΠ° ΠΈΠΌΠ΅Π΅Ρ ΠΎΠ΄ΠΈΠ½ ΠΈΠ· ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
Π²ΠΈΠ΄ΠΎΠ²: Π²ΡΠ»ΠΎΠΆΠΈΡΡ Π½Π° ΡΡΠΎΠ» ΠΈΠ· ΡΠ²ΠΎΠ΅ΠΉ ΡΡΠΊΠΈ Π΄Π΅Π²ΡΡΠΊΡ Π»ΡΠ±ΠΎΠΉ ΠΌΠ°ΡΡΠΈ; Π²ΡΠ»ΠΎΠΆΠΈΡΡ Π½Π° ΡΡΠΎΠ» ΡΠ΅ΡΡΡΡΠΊΡ, ΡΠ΅ΠΌΡΡΠΊΡ ΠΈΠ»ΠΈ Π²ΠΎΡΡΠΌΡΡΠΊΡ Π»ΡΠ±ΠΎΠΉ ΠΌΠ°ΡΡΠΈ, Π΅ΡΠ»ΠΈ Π½Π° ΡΡΠΎΠ»Π΅ ΡΠΆΠ΅ Π»Π΅ΠΆΠΈΡ ΠΊΠ°ΡΡΠ° ΡΠΎΠΉ ΠΆΠ΅ ΠΌΠ°ΡΡΠΈ Π΄ΠΎΡΡΠΎΠΈΠ½ΡΡΠ²ΠΎΠΌ Π½Π° Π΅Π΄ΠΈΠ½ΠΈΡΡ Π²ΡΡΠ΅; Π²ΡΠ»ΠΎΠΆΠΈΡΡ Π½Π° ΡΡΠΎΠ» Π΄Π΅ΡΡΡΠΊΡ, Π²Π°Π»Π΅ΡΠ°, Π΄Π°ΠΌΡ, ΠΊΠΎΡΠΎΠ»Ρ ΠΈΠ»ΠΈ ΡΡΠ·Π° Π»ΡΠ±ΠΎΠΉ ΠΌΠ°ΡΡΠΈ, Π΅ΡΠ»ΠΈ Π½Π° ΡΡΠΎΠ»Π΅ ΡΠΆΠ΅ Π»Π΅ΠΆΠΈΡ ΠΊΠ°ΡΡΠ° ΡΠΎΠΉ ΠΆΠ΅ ΠΌΠ°ΡΡΠΈ Π΄ΠΎΡΡΠΎΠΈΠ½ΡΡΠ²ΠΎΠΌ Π½Π° Π΅Π΄ΠΈΠ½ΠΈΡΡ Π½ΠΈΠΆΠ΅. ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, Π΄Π΅Π²ΡΡΠΊΡ ΠΏΠΈΠΊ ΠΌΠΎΠΆΠ½ΠΎ Π²ΡΠ»ΠΎΠΆΠΈΡΡ Π½Π° ΡΡΠΎΠ» Π² Π»ΡΠ±ΠΎΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ, Π΄Π»Ρ Π²ΡΠΊΠ»Π°Π΄ΡΠ²Π°Π½ΠΈΡ ΡΠ΅ΠΌΡΡΠΊΠΈ ΡΡΠ΅Ρ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°Π»ΠΈΡΠΈΠ΅ Π½Π° ΡΡΠΎΠ»Π΅ Π²ΠΎΡΡΠΌΡΡΠΊΠΈ ΡΡΠ΅Ρ, Π° Π΄Π»Ρ Π²ΡΠΊΠ»Π°Π΄ΡΠ²Π°Π½ΠΈΡ ΡΡΠ·Π° ΡΠ΅ΡΠ²Π΅ΠΉ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°Π»ΠΈΡΠΈΠ΅ Π½Π° ΡΡΠΎΠ»Π΅ ΠΊΠΎΡΠΎΠ»Ρ ΡΠ΅ΡΠ²Π΅ΠΉ. ΠΡΠ»ΠΈ ΠΈΠ³ΡΠΎΠΊ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠ»ΠΎΠΆΠΈΡΡ Π½Π° ΡΡΠΎΠ» Π½ΠΈ ΠΎΠ΄Π½Ρ ΠΊΠ°ΡΡΡ ΠΈΠ· ΡΠ²ΠΎΠ΅ΠΉ ΡΡΠΊΠΈ, ΡΠΎ Ρ
ΠΎΠ΄ ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠ΄ΠΈΡ ΠΊ ΡΠΎΠΏΠ΅ΡΠ½ΠΈΠΊΡ. ΠΠ±ΡΠ°ΡΠΈΡΠ΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅: Π½Π΅Π»ΡΠ·Ρ ΠΏΡΠΎΠΏΡΡΡΠΈΡΡ Ρ
ΠΎΠ΄ ΠΏΡΠΎΡΡΠΎ ΡΠ°ΠΊxa0β Π²ΡΠ΅Π³Π΄Π° Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ Π²ΡΠ»ΠΎΠΆΠΈΡΡ ΠΊΠ°ΡΡΡ Π½Π° ΡΡΠΎΠ» ΠΊΠΎΡΡΠ΅ΠΊΡΠ½ΡΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. ΠΠΎΠΌΠΈΠΌΠΎ ΡΠΎΠ³ΠΎ, ΡΡΠΎ ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ³ΡΠΎΠΊ ΡΡΡΠ΅ΠΌΠΈΡΡΡ ΠΈΠ·Π±Π°Π²ΠΈΡΡΡΡ ΠΎΡ ΠΊΠ°ΡΡ Π² ΡΠ²ΠΎΠ΅ΠΉ ΡΡΠΊΠ΅, ΠΠ»ΠΈΡΠ° ΠΈ ΠΠΎΠ± ΡΠ°ΠΊΠΆΠ΅ Ρ
ΠΎΡΡΡ, ΡΡΠΎΠ±Ρ Π² ΠΊΠΎΠ½ΡΠ΅ ΠΈΠ³ΡΡ Π² ΡΡΠΊΠ΅ Ρ ΠΈΡ
ΡΠΎΠΏΠ΅ΡΠ½ΠΈΠΊΠ° ΠΊΠ°ΡΡ ΠΎΡΡΠ°Π»ΠΎΡΡ ΠΊΠ°ΠΊ ΠΌΠΎΠΆΠ½ΠΎ Π±ΠΎΠ»ΡΡΠ΅, Π° Π² ΠΈΡ
ΡΡΠΊΠ΅xa0β ΠΊΠ°ΠΊ ΠΌΠΎΠΆΠ½ΠΎ ΠΌΠ΅Π½ΡΡΠ΅. ΠΠ°ΠΏΠΎΠΌΠ½ΠΈΠΌ, ΡΡΠΎ ΠΈΠ³ΡΠ° Π·Π°ΠΊΠ°Π½ΡΠΈΠ²Π°Π΅ΡΡΡ, ΠΊΠ°ΠΊ ΡΠΎΠ»ΡΠΊΠΎ ΠΎΠ΄ΠΈΠ½ ΠΈΠ· ΠΈΠ³ΡΠΎΠΊΠΎΠ² Π²ΡΠΊΠ»Π°Π΄ΡΠ²Π°Π΅Ρ Π½Π° ΡΡΠΎΠ» ΠΏΠΎΡΠ»Π΅Π΄Π½ΡΡ ΠΊΠ°ΡΡΡ ΠΈΠ· ΡΠ²ΠΎΠ΅ΠΉ ΡΡΠΊΠΈ. Π Π΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ ΠΈΠ³ΡΡ Π½Π°Π·ΠΎΠ²ΡΠΌ ΡΠΎΠ²ΠΎΠΊΡΠΏΠ½ΠΎΡΡΡ ΠΈΠ· ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠΈ ΠΎ ΡΠΎΠΌ, ΠΊΡΠΎ ΠΈΠ· Π΄Π²ΡΡ
ΠΈΠ³ΡΠΎΠΊΠΎΠ² Π²ΡΠΈΠ³ΡΠ°Π΅Ρ ΠΏΡΠΈ ΠΎΠΏΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠΉ ΠΈΠ³ΡΠ΅, Π° ΡΠ°ΠΊΠΆΠ΅ ΠΎ ΡΠΎΠΌ, ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΊΠ°ΡΡ ΠΎΡΡΠ°Π½Π΅ΡΡΡ Π² ΡΡΠΊΠ΅ Ρ ΠΏΡΠΎΠΈΠ³ΡΠ°Π²ΡΠ΅Π³ΠΎ. ΠΡΡΡΡ ΠΠ»ΠΈΡΠ° ΠΈ ΠΠΎΠ± ΡΠΆΠ΅ Π²Π·ΡΠ»ΠΈ Π² ΡΡΠΊΠΈ ΡΠ²ΠΎΠΈ $$$18$$$ ΠΊΠ°ΡΡ ΠΊΠ°ΠΆΠ΄ΡΠΉ, Π½ΠΎ Π΅ΡΡ Π½Π΅ ΡΠ΅ΡΠΈΠ»ΠΈ, ΠΊΡΠΎ ΠΈΠ· Π½ΠΈΡ
Π±ΡΠ΄Π΅Ρ Ρ
ΠΎΠ΄ΠΈΡΡ ΠΏΠ΅ΡΠ²ΡΠΌ. ΠΠ΅Π»ΠΈΡΠΈΠ½ΠΎΠΉ Π²Π°ΠΆΠ½ΠΎΡΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Ρ
ΠΎΠ΄Π° Π΄Π»Ρ Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΡΠ°ΡΠΊΠ»Π°Π΄Π° Π½Π°Π·ΠΎΠ²ΡΠΌ Π°Π±ΡΠΎΠ»ΡΡΠ½ΡΡ ΡΠ°Π·Π½ΠΎΡΡΡ ΠΌΠ΅ΠΆΠ΄Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°ΠΌΠΈ ΠΈΠ³ΡΡ Π² ΡΠ»ΡΡΠ°Π΅, Π΅ΡΠ»ΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΉ Π±ΡΠ΄Π΅Ρ Ρ
ΠΎΠ΄ΠΈΡΡ ΠΠ»ΠΈΡΠ°, ΠΈ Π² ΡΠ»ΡΡΠ°Π΅, Π΅ΡΠ»ΠΈ ΠΏΠ΅ΡΠ²ΡΠΌ Π±ΡΠ΄Π΅Ρ Ρ
ΠΎΠ΄ΠΈΡΡ ΠΠΎΠ±. ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, Π΅ΡΠ»ΠΈ Π² ΠΎΠ±ΠΎΠΈΡ
ΡΠ»ΡΡΠ°ΡΡ
Π²ΡΠΈΠ³ΡΠ°Π΅Ρ ΠΠΎΠ±, Π½ΠΎ Π² ΠΎΠ΄Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Ρ ΠΠ»ΠΈΡΡ ΠΎΡΡΠ°Π½Π΅ΡΡΡ $$$6$$$ ΠΊΠ°ΡΡ Π² ΡΡΠΊΠ΅ Π² ΠΊΠΎΠ½ΡΠ΅ ΠΈΠ³ΡΡ, Π° Π²ΠΎ Π²ΡΠΎΡΠΎΠΌxa0β Π²ΡΠ΅Π³ΠΎ $$$2$$$, ΡΠΎ Π²Π΅Π»ΠΈΡΠΈΠ½Π° Π²Π°ΠΆΠ½ΠΎΡΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Ρ
ΠΎΠ΄Π° ΡΠ°Π²Π½Π° $$$4$$$. ΠΡΠ»ΠΈ ΠΆΠ΅ Π² ΠΎΠ΄Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π²ΡΠΈΠ³ΡΠ°Π΅Ρ ΠΠ»ΠΈΡΠ° ΠΈ Ρ ΠΠΎΠ±Π° ΠΎΡΡΠ°Π½Π΅ΡΡΡ $$$5$$$ ΠΊΠ°ΡΡ Π² ΡΡΠΊΠ΅, Π° Π²ΠΎ Π²ΡΠΎΡΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π²ΡΠΈΠ³ΡΠ°Π΅Ρ ΠΠΎΠ± ΠΈ Ρ ΠΠ»ΠΈΡΡ ΠΎΡΡΠ°Π½Π΅ΡΡΡ $$$3$$$ ΠΊΠ°ΡΡΡ Π² ΡΡΠΊΠ΅, ΡΠΎ Π²Π΅Π»ΠΈΡΠΈΠ½Π° Π²Π°ΠΆΠ½ΠΎΡΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Ρ
ΠΎΠ΄Π° ΡΠ°Π²Π½Π° $$$8$$$. Π Π΅Π±ΡΡΠ° Ρ
ΠΎΡΡΡ ΡΠ·Π½Π°ΡΡ, Π½Π°ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠ°Π·Π½ΠΎΠΉ Π±ΡΠ²Π°Π΅Ρ Π²Π΅Π»ΠΈΡΠΈΠ½Π° Π²Π°ΠΆΠ½ΠΎΡΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Ρ
ΠΎΠ΄Π° Π΄Π»Ρ ΡΠ°Π·Π½ΡΡ
ΡΠ°ΡΠΊΠ»Π°Π΄ΠΎΠ². ΠΠΎ Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡ ΡΠΈΡΠ»Ρ $$$k le 26$$$ ΠΏΠΎΠΌΠΎΠ³ΠΈΡΠ΅ ΠΈΠΌ Π½Π°ΠΉΡΠΈ ΡΠ°ΠΊΠΈΠ΅ $$$k$$$ ΡΠ°ΡΠΊΠ»Π°Π΄ΠΎΠ², ΡΡΠΎ Π²Π΅Π»ΠΈΡΠΈΠ½Ρ Π²Π°ΠΆΠ½ΠΎΡΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Ρ
ΠΎΠ΄Π° Π΄Π»Ρ Π²ΡΠ΅Ρ
Π½ΠΈΡ
xa0β ΡΠ°Π·Π»ΠΈΡΠ½ΡΠ΅ ΡΠ΅Π»ΡΠ΅ ΡΠΈΡΠ»Π°. ΠΡΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ $$$k$$$ ΠΏΠ°Ρ ΡΡΡΠΎΠΊ. ΠΠ°ΠΆΠ΄Π°Ρ ΠΏΠ°ΡΠ° ΡΡΡΠΎΠΊ Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΠΎΠ²Π°ΡΡ Π½Π΅ΠΊΠΎΡΠΎΡΠΎΠΌΡ ΡΠ°ΡΠΊΠ»Π°Π΄Ρ. ΠΠ΅Π»ΠΈΡΠΈΠ½Ρ Π²Π°ΠΆΠ½ΠΎΡΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Ρ
ΠΎΠ΄Π° Π΄Π»Ρ Π²ΡΠ΅Ρ
Π²ΡΠ²Π΅Π΄Π΅Π½Π½ΡΡ
ΡΠ°ΡΠΊΠ»Π°Π΄ΠΎΠ² Π΄ΠΎΠ»ΠΆΠ½Ρ Π±ΡΡΡ ΡΠ°Π·Π»ΠΈΡΠ½ΡΠΌΠΈ ΡΠ΅Π»ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ. Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΏΠ°ΡΡ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ $$$18$$$ ΡΡΡΠΎΠΊ Π΄Π»ΠΈΠ½Ρ $$$2$$$ ΡΠ΅ΡΠ΅Π· ΠΏΡΠΎΠ±Π΅Π», ΠΎΠΏΠΈΡΡΠ²Π°ΡΡΠΈΡ
ΠΊΠ°ΡΡΡ ΠΠ»ΠΈΡΡ Π² Π»ΡΠ±ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. ΠΠ΅ΡΠ²ΡΠΉ ΡΠΈΠΌΠ²ΠΎΠ» ΡΡΡΠΎΠΊΠΈ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΎΠ±ΠΎΠ·Π½Π°ΡΠ°ΡΡ Π΄ΠΎΡΡΠΎΠΈΠ½ΡΡΠ²ΠΎ ΠΊΠ°ΡΡΡxa0β ΡΠΈΠΌΠ²ΠΎΠ» ΠΈΠ· Π½Π°Π±ΠΎΡΠ° 6, 7, 8, 9, T, J, Q, K, A, ΠΎΠ±ΠΎΠ·Π½Π°ΡΠ°ΡΡΠΈΠΉ ΡΠ΅ΡΡΡΡΠΊΡ, ΡΠ΅ΠΌΡΡΠΊΡ, Π²ΠΎΡΡΠΌΡΡΠΊΡ, Π΄Π΅Π²ΡΡΠΊΡ, Π΄Π΅ΡΡΡΠΊΡ, Π²Π°Π»Π΅ΡΠ°, Π΄Π°ΠΌΡ, ΠΊΠΎΡΠΎΠ»Ρ ΠΈ ΡΡΠ·Π° ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ. ΠΡΠΎΡΠΎΠΉ ΡΠΈΠΌΠ²ΠΎΠ» ΡΡΡΠΎΠΊΠΈ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΎΠ±ΠΎΠ·Π½Π°ΡΠ°ΡΡ ΠΌΠ°ΡΡΡ ΠΊΠ°ΡΡΡxa0β ΡΠΈΠΌΠ²ΠΎΠ» ΠΈΠ· Π½Π°Π±ΠΎΡΠ° C, D, S, H, ΠΎΠ±ΠΎΠ·Π½Π°ΡΠ°ΡΡΠΈΠΉ ΡΡΠ΅ΡΡ, Π±ΡΠ±Π½Ρ, ΠΏΠΈΠΊΠΈ ΠΈ ΡΠ΅ΡΠ²ΠΈ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ. ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ $$$18$$$ ΡΡΡΠΎΠΊ Π΄Π»ΠΈΠ½Ρ $$$2$$$ ΡΠ΅ΡΠ΅Π· ΠΏΡΠΎΠ±Π΅Π», ΠΎΠΏΠΈΡΡΠ²Π°ΡΡΠΈΡ
ΠΊΠ°ΡΡΡ ΠΠΎΠ±Π° Π² ΡΠΎΠΌ ΠΆΠ΅ ΡΠΎΡΠΌΠ°ΡΠ΅. ΠΠ°ΠΆΠ΄Π°Ρ ΠΈΠ· $$$36$$$ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΡΡ
ΠΊΠ°ΡΡ Π΄ΠΎΠ»ΠΆΠ½Π° Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡΡ Π² ΡΡΠΊΠ΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ· Π΄Π²ΡΡ
ΠΈΠ³ΡΠΎΠΊΠΎΠ² Π² Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΠΎΠΌ ΡΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡΠ΅. ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅ Π ΠΏΠ΅ΡΠ²ΠΎΠΌ Π²ΡΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ ΡΠ°ΡΠΊΠ»Π°Π΄Π΅ Π²ΡΠ΅ Π΄Π΅Π²ΡΡΠΊΠΈ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ Π² ΡΡΠΊΠ΅ Ρ ΠΠ»ΠΈΡΡ. ΠΠ°ΠΆΠ΅ Π΅ΡΠ»ΠΈ ΠΠΎΠ± Π±ΡΠ΄Π΅Ρ Ρ
ΠΎΠ΄ΠΈΡΡ ΠΏΠ΅ΡΠ²ΡΠΌ, Π΅ΠΌΡ Π²ΡΡ ΡΠ°Π²Π½ΠΎ ΠΏΡΠΈΠ΄ΡΡΡΡ ΠΏΡΠΎΠΏΡΡΡΠΈΡΡ ΠΏΠ΅ΡΠ²ΡΠΉ ΠΆΠ΅ ΡΠ²ΠΎΠΉ Ρ
ΠΎΠ΄. Π‘Π»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎ, ΠΏΠ΅ΡΠ²ΡΠΉ Ρ
ΠΎΠ΄ ΠΏΡΠΈ ΡΠ°ΠΊΠΎΠΌ ΡΠ°ΡΠΊΠ»Π°Π΄Π΅ ΠΈΠΌΠ΅Π΅Ρ Π²Π°ΠΆΠ½ΠΎΡΡΡ $$$0$$$. ΠΠΎ Π²ΡΠΎΡΠΎΠΌ Π²ΡΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ ΡΠ°ΡΠΊΠ»Π°Π΄Π΅ Π²Π½Π΅ Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡΠΈ ΠΎΡ ΡΠΎΠ³ΠΎ, ΡΡΠΈΠΌ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ²ΡΠΉ Ρ
ΠΎΠ΄, Π²ΡΠΈΠ³ΡΠ°Π΅Ρ ΠΠ»ΠΈΡΠ°. ΠΠ΄Π½Π°ΠΊΠΎ Π΅ΡΠ»ΠΈ ΠΠ»ΠΈΡΠ° Π±ΡΠ΄Π΅Ρ Ρ
ΠΎΠ΄ΠΈΡΡ ΠΏΠ΅ΡΠ²ΠΎΠΉ, ΡΠΎ Ρ ΠΠΎΠ±Π° Π² ΠΊΠΎΠ½ΡΠ΅ ΠΈΠ³ΡΡ Π² ΡΡΠΊΠ΅ ΠΎΡΡΠ°Π½Π΅ΡΡΡ ΠΎΠ΄Π½Π° ΠΊΠ°ΡΡΠ°, Π° Π΅ΡΠ»ΠΈ ΠΆΠ΅ ΠΎΠ½Π° Π±ΡΠ΄Π΅Ρ Ρ
ΠΎΠ΄ΠΈΡΡ Π²ΡΠΎΡΠΎΠΉ, ΡΠΎ Ρ ΠΠΎΠ±Π° ΠΎΡΡΠ°Π½Π΅ΡΡΡ ΠΏΡΡΡ ΠΊΠ°ΡΡ. Π‘ΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ, Π²Π΅Π»ΠΈΡΠΈΠ½Π° Π²Π°ΠΆΠ½ΠΎΡΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Ρ
ΠΎΠ΄Π° ΠΏΡΠΈ ΡΠ°ΠΊΠΎΠΌ ΡΠ°ΡΠΊΠ»Π°Π΄Π΅ ΡΠ°Π²Π½Π° $$$4$$$. | 2,300 | false | false | false | false | false | false | true | false | false | false | 1,678 |
1759F | A positive number $$$x$$$ of length $$$n$$$ in base $$$p$$$ ($$$2 le p le 10^9$$$) is written on the blackboard. The number $$$x$$$ is given as a sequence $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i < p$$$) β the digits of $$$x$$$ in order from left to right (most significant to least significant). Dmitry is very fond of all the digits of this number system, so he wants to see each of them at least once. In one operation, he can: take any number $$$x$$$ written on the board, increase it by $$$1$$$, and write the new value $$$x + 1$$$ on the board. For example, $$$p=5$$$ and $$$x=234_5$$$. Initially, the board contains the digits $$$2$$$, $$$3$$$ and $$$4$$$; Dmitry increases the number $$$234_5$$$ by $$$1$$$ and writes down the number $$$240_5$$$. On the board there are digits $$$0, 2, 3, 4$$$; Dmitry increases the number $$$240_5$$$ by $$$1$$$ and writes down the number $$$241_5$$$. Now the board contains all the digits from $$$0$$$ to $$$4$$$. Your task is to determine the minimum number of operations required to make all the digits from $$$0$$$ to $$$p-1$$$ appear on the board at least once. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^3$$$)xa0β the number of test cases. The descriptions of the input test cases follow. The first line of description of each test case contains two integers $$$n$$$ ($$$1 le n le 100$$$) and $$$p$$$ ($$$2 le p le 10^9$$$) β the length of the number and the base of the number system. The second line of the description of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i < p$$$) β digits of $$$x$$$ in number system with base $$$p$$$ It is guaranteed that the number $$$x$$$ does not contain leading zeros (that is, $$$a_1>0$$$). Output For each test case print a single integer β the minimum number of operations required for Dmitry to get all the digits on the board from $$$0$$$ to $$$p-1$$$. It can be shown that this always requires a finite number of operations. Example Input 11 2 3 1 2 4 2 1 1 1 1 6 6 1 2 3 4 5 0 5 2 1 0 1 0 1 3 10 1 2 3 5 1000 4 1 3 2 5 3 5 2 3 4 4 4 3 2 3 0 1 3 2 5 5 1 2 2 2 4 3 4 1 0 1 Output 1 1 0 0 7 995 2 1 1 1 2 | 1,800 | true | true | false | false | true | false | false | true | false | false | 1,757 |
1374E2 | , # Reading Books (hard version) Input file: standard input Output file: standard output Time limit: 3 seconds Memory limit: 256 megabytes Easy and hard versions are actually different problems, so read statements of both problems completely and carefully .Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesnβt think so. She says that they have to read exactly m books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: ti x16 the amount of time Alice and Bob need to spend to read it, ai (equals 1 if Alice likes the i-th book and 0 if not), and bi (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose exactly m books from the given n books in such a way that: β’ Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; β’ the total reading time of these m books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (itβs shared between them) and they read all books together , so the total reading time is the sum of ti over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. # Input The first line of the input contains three integers n, m and k (1 β€ k β€ m β€ n β€ 2 Β· 10 5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers ti, ai and bi (1 β€ ti β€ 10 4, 0 β€ ai, b i β€ 1), where: β’ ti x16 the amount of time required for reading the i-th book; β’ ai equals 1 if Alice likes the i-th book and 0 otherwise; β’ bi equals 1 if Bob likes the i-th book and 0 otherwise. # Output If there is no solution, print only one integer -1 .If the solution exists, print T in the first line x16 the minimum total reading time of the suitable set of books. In the second line print m distinct integers from 1 to n in any order x16 indices of books which are in the set you found. If there are several answers, print any of them. Page 1 of 2 , # Examples standard input standard output 6 3 1 6 0 0 11 1 0 9 0 1 21 1 1 10 1 0 8 0 1 24 6 5 1 6 3 2 6 0 0 11 1 0 9 0 1 21 1 1 10 1 0 8 0 1 39 4 6 5 Page 2 of 2 | 2,500 | false | true | true | false | true | false | false | false | true | false | 3,789 |
1624A | Polycarp got an array of integers $$$a[1 dots n]$$$ as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become $$$a_1=a_2=dots=a_n$$$). In one operation, he can take some indices in the array and increase the elements of the array at those indices by $$$1$$$. For example, let $$$a=[4,2,1,6,2]$$$. He can perform the following operation: select indices 1, 2, and 4 and increase elements of the array in those indices by $$$1$$$. As a result, in one operation, he can get a new state of the array $$$a=[5,3,1,7,2]$$$. What is the minimum number of operations it can take so that all elements of the array become equal to each other (that is, to become $$$a_1=a_2=dots=a_n$$$)? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) xa0β the number of test cases in the test. The following are descriptions of the input test cases. The first line of the description of each test case contains one integer $$$n$$$ ($$$1 le n le 50$$$) xa0β the array $$$a$$$. The second line of the description of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) xa0β elements of the array $$$a$$$. Output For each test case, print one integer xa0β the minimum number of operations to make all elements of the array $$$a$$$ equal. Example Input 3 6 3 4 2 4 1 2 3 1000 1002 998 2 12 11 Note First test case: $$$a=[3,4,2,4,1,2]$$$ take $$$a_3, a_5$$$ and perform an operation plus one on them, as a result we get $$$a=[3,4,3,4,2,2]$$$. $$$a=[3,4,3,4,2,2]$$$ we take $$$a_1, a_5, a_6$$$ and perform an operation on them plus one, as a result we get $$$a=[4,4,3,4,3,3]$$$. $$$a=[4,4,3,4,3,3]$$$ we take $$$a_3, a_5, a_6$$$ and perform an operation on them plus one, as a result we get $$$a=[4,4,4,4,4,4]$$$. There are other sequences of $$$3$$$ operations, after the application of which all elements become equal. Second test case: $$$a=[1000,1002,998]$$$ 2 times we take $$$a_1, a_3$$$ and perform an operation plus one on them, as a result we get $$$a=[1002,1002,1000]$$$. $$$a=[1002,1002,1000]$$$ also take $$$a_3$$$ 2 times and perform an operation plus one on it, as a result we get $$$a=[1002,1002,1002]$$$. Third test case: $$$a=[12,11]$$$ take $$$a_2$$$ and perform an operation plus one on it, as a result we get $$$a=[12,12]$$$. | 800 | true | false | false | false | false | false | false | false | false | false | 2,500 |
1656I | Given an undirected graph $$$G$$$, we say that a neighbour ordering is an ordered list of all the neighbours of a vertex for each of the vertices of $$$G$$$. Consider a given neighbour ordering of $$$G$$$ and three vertices $$$u$$$, $$$v$$$ and $$$w$$$, such that $$$v$$$ is a neighbor of $$$u$$$ and $$$w$$$. We write $$$u <_{v} w$$$ if $$$u$$$ comes after $$$w$$$ in $$$v$$$'s neighbor list. A neighbour ordering is said to be good if, for each simple cycle $$$v_1, v_2, ldots, v_c$$$ of the graph, one of the following is satisfied: $$$v_1 <_{v_2} v_3, v_2 <_{v_3} v_4, ldots, v_{c-2} <_{v_{c-1}} v_c, v_{c-1} <_{v_c} v_1, v_c <_{v_1} v_2$$$. $$$v_1 >_{v_2} v_3, v_2 >_{v_3} v_4, ldots, v_{c-2} >_{v_{c-1}} v_c, v_{c-1} >_{v_c} v_1, v_c >_{v_1} v_2$$$. Given a graph $$$G$$$, determine whether there exists a good neighbour ordering for it and construct one if it does. 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. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 3 cdot 10^5$$$, $$$1 leq m leq 3 cdot 10^5$$$), the number of vertices and the number of edges of the graph. The next $$$m$$$ lines each contain two integers $$$u, v$$$ ($$$0 leq u, v < n$$$), denoting that there is an edge connecting vertices $$$u$$$ and $$$v$$$. It is guaranteed that the graph is connected and there are no loops or multiple edges between the same vertices. The sum of $$$n$$$ and the sum of $$$m$$$ for all test cases are at most $$$3 cdot 10^5$$$. Output For each test case, output one line with YES if there is a good neighbour ordering, otherwise output one line with NO. You can print each letter in any case (upper or lower). If the answer is YES, additionally output $$$n$$$ lines describing a good neighbour ordering. In the $$$i$$$-th line, output the neighbours of vertex $$$i$$$ in order. If there are multiple good neigbour orderings, print any. Example Input 3 5 6 0 1 0 2 1 2 2 3 3 4 4 1 2 1 0 1 6 10 0 1 2 0 0 3 0 4 1 2 1 4 2 3 2 5 3 5 4 5 Output YES 1 2 4 2 0 0 1 3 2 4 3 1 YES 1 0 NO | 3,500 | false | false | false | false | false | true | false | false | false | true | 2,346 |
780C | Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them. The park consists of _n_ squares connected with (_n_u2009-u20091) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if _a_, _b_ and _c_ are distinct squares that _a_ and _b_ have a direct path between them, and _b_ and _c_ have a direct path between them, then balloon colors on these three squares are distinct. Andryusha wants to use as little different colors as possible. Help him to choose the colors! Input The first line contains single integer _n_ (3u2009β€u2009_n_u2009β€u20092Β·105)xa0β the number of squares in the park. Each of the next (_n_u2009-u20091) lines contains two integers _x_ and _y_ (1u2009β€u2009_x_,u2009_y_u2009β€u2009_n_)xa0β the indices of two squares directly connected by a path. It is guaranteed that any square is reachable from any other using the paths. Output In the first line print single integer _k_xa0β the minimum number of colors Andryusha has to use. In the second line print _n_ integers, the _i_-th of them should be equal to the balloon color on the _i_-th square. Each of these numbers should be within range from 1 to _k_. Note In the first sample the park consists of three squares: 1u2009βu20093u2009βu20092. Thus, the balloon colors have to be distinct. Illustration for the first sample. In the second example there are following triples of consequently connected squares: 1u2009βu20093u2009βu20092 1u2009βu20093u2009βu20094 1u2009βu20093u2009βu20095 2u2009βu20093u2009βu20094 2u2009βu20093u2009βu20095 4u2009βu20093u2009βu20095 We can see that each pair of squares is encountered in some triple, so all colors have to be distinct. Illustration for the second sample. In the third example there are following triples: 1u2009βu20092u2009βu20093 2u2009βu20093u2009βu20094 3u2009βu20094u2009βu20095 We can see that one or two colors is not enough, but there is an answer that uses three colors only. Illustration for the third sample. | 1,600 | false | true | false | false | false | false | false | false | false | true | 6,646 |
8C | Girl Lena likes it when everything is in order, and looks for order everywhere. Once she was getting ready for the University and noticed that the room was in a mess β all the objects from her handbag were thrown about the room. Of course, she wanted to put them back into her handbag. The problem is that the girl cannot carry more than two objects at a time, and cannot move the handbag. Also, if he has taken an object, she cannot put it anywhere except her handbag β her inherent sense of order does not let her do so. You are given the coordinates of the handbag and the coordinates of the objects in some Π‘artesian coordinate system. It is known that the girl covers the distance between any two objects in the time equal to the squared length of the segment between the points of the objects. It is also known that initially the coordinates of the girl and the handbag are the same. You are asked to find such an order of actions, that the girl can put all the objects back into her handbag in a minimum time period. Input The first line of the input file contains the handbag's coordinates _x__s_,u2009_y__s_. The second line contains number _n_ (1u2009β€u2009_n_u2009β€u200924) β the amount of objects the girl has. The following _n_ lines contain the objects' coordinates. All the coordinates do not exceed 100 in absolute value. All the given positions are different. All the numbers are integer. Output In the first line output the only number β the minimum time the girl needs to put the objects into her handbag. In the second line output the possible optimum way for Lena. Each object in the input is described by its index number (from 1 to _n_), the handbag's point is described by number 0. The path should start and end in the handbag's point. If there are several optimal paths, print any of them. | 2,000 | false | false | false | true | false | false | false | false | false | false | 9,953 |
798D | Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers _A_u2009=u2009[_a_1,u2009_a_2,u2009...,u2009_a__n_] and _B_u2009=u2009[_b_1,u2009_b_2,u2009...,u2009_b__n_] of length _n_ each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select _k_ numbers _P_u2009=u2009[_p_1,u2009_p_2,u2009...,u2009_p__k_] such that 1u2009β€u2009_p__i_u2009β€u2009_n_ for 1u2009β€u2009_i_u2009β€u2009_k_ and elements in _P_ are distinct. Sequence _P_ will represent indices of elements that you'll select from both sequences. He calls such a subset _P_ "unfair" if and only if the following conditions are satisfied: 2Β·(_a__p_1u2009+u2009...u2009+u2009_a__p__k_) is greater than the sum of all elements from sequence _A_, and 2Β·(_b__p_1u2009+u2009...u2009+u2009_b__p__k_) is greater than the sum of all elements from the sequence _B_. Also, _k_ should be smaller or equal to because it will be to easy to find sequence _P_ if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of elements in the sequences. On the second line there are _n_ space-separated integers _a_1,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009109) β elements of sequence _A_. On the third line there are also _n_ space-separated integers _b_1,u2009...,u2009_b__n_ (1u2009β€u2009_b__i_u2009β€u2009109) β elements of sequence _B_. Output On the first line output an integer _k_ which represents the size of the found subset. _k_ should be less or equal to . On the next line print _k_ integers _p_1,u2009_p_2,u2009...,u2009_p__k_ (1u2009β€u2009_p__i_u2009β€u2009_n_) β the elements of sequence _P_. You can print the numbers in any order you want. Elements in sequence _P_ should be distinct. | 2,400 | false | false | false | false | false | true | false | false | true | false | 6,578 |
1461F | Barbara was late for her math class so as a punishment the teacher made her solve the task on a sheet of paper. Barbara looked at the sheet of paper and only saw $$$n$$$ numbers $$$a_1, a_2, ldots, a_n$$$ without any mathematical symbols. The teacher explained to Barbara that she has to place the available symbols between the numbers in a way that would make the resulting expression's value as large as possible. To find out which symbols were available the teacher has given Barbara a string $$$s$$$ which contained that information. It's easy to notice that Barbara has to place $$$n - 1$$$ symbols between numbers in total. The expression must start with a number and all symbols must be allowed (i.e. included in $$$s$$$). Note that multiplication takes precedence over addition or subtraction, addition and subtraction have the same priority and performed from left to right. Help Barbara and create the required expression! Input The first line of the input contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β the amount of numbers on the paper. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains the string $$$s$$$ ($$$1 le s le 3$$$)xa0β symbols allowed in the expression. It is guaranteed that the string may only consist of symbols "-", "+" and "*". It is also guaranteed that all symbols in the string are distinct. Output Print $$$n$$$ numbers separated by $$$n - 1$$$ symbolsxa0β a mathematical expression with the greatest result. If there are multiple equally valid resultsxa0β output any one of them. Note The following answers also fit the first example: "2+2+0", "2+2-0", "2*2+0". | 2,700 | false | true | false | true | false | true | false | false | false | false | 3,381 |
1456E | Given integers $$$c_{0}, c_{1}, ldots, c_{k-1}$$$ we can define the cost of a number $$$0 le x < 2^{k}$$$ as $$$p(x) = sum_{i=0}^{k-1} left( leftlfloor frac{x}{2^{i}} ight floor bmod 2 ight) cdot c_{i}$$$. In other words, the cost of number $$$x$$$ is the sum of $$$c_{i}$$$ over the bits of $$$x$$$ which are equal to one. Let's define the cost of array $$$a$$$ of length $$$n ge 2$$$ with elements from $$$xa0β the size of an array and bit length of the numbers in question. Next $$$n$$$ lines contain the restrictions for elements of the array: the $$$i$$$-th line contains two integers $$$l_{i}$$$ and $$$r_{i}$$$ ($$$0 le l_{i} le r_{i} < 2^{k}$$$). The last line contains integers $$$c_{0}, c_{1}, ldots, c_{k-1}$$$ ($$$0 le c_{i} le 10^{12}$$$). Output Output one integerxa0β the minimal cost of an array satisfying all the restrictions. Examples Input 4 3 3 3 5 5 6 6 1 1 5 2 7 Input 3 3 2 2 3 4 4 6 1 10 100 Note In the first example there is only one array satisfying all the restrictionsxa0β $$$[3, 5, 6, 1]$$$xa0β and its cost is equal to $$$cost([3, 5, 6, 1]) = p(3 oplus 5) + p(5 oplus 6) + p(6 oplus 1) = p(6) + p(3) + p(7) = (c_{1} + c_{2}) + (c_{0} + c_{1}) + (c_{0} + c_{1} + c_{2}) = (2 + 7) + (5 + 2) + (5 + 2 + 7) = 30$$$. In the second example the only optimal array is $$$[2, 3, 6]$$$. | 3,500 | false | true | false | true | false | false | false | false | false | false | 3,395 |
1814A | Problem - 1814A - 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 implementation math *800 No tag edit access β Contest materials xa0β the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 10^{18}$$$; $$$k e 2$$$). Output For each test case, print YES if it is possible to represent $$$n$$$ burles in coins; otherwise, print NO. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). Example Input 4 5 3 6 1 7 4 8 8 Output YES YES NO YES Note In the first test case, you can take one coin with denomination $$$2$$$ and one coin with denomination $$$k = 3$$$. In the second test case, you can take three coins with denomination $$$2$$$. Alternatively, you can take six coins with denomination $$$k = 1$$$. In the third test case, there is no way to represent $$$7$$$ burles. In the fourth test case, you can take one coin with denomination $$$k = 8$$$. | 800 | true | false | true | false | false | false | false | false | false | false | 1,387 |
786C | Rick and Morty want to find MR. PBH and they can't do it alone. So they need of Mr. Meeseeks. They Have generated _n_ Mr. Meeseeks, standing in a line numbered from 1 to _n_. Each of them has his own color. _i_-th Mr. Meeseeks' color is _a__i_. Rick and Morty are gathering their army and they want to divide Mr. Meeseeks into some squads. They don't want their squads to be too colorful, so each squad should have Mr. Meeseeks of at most _k_ different colors. Also each squad should be a continuous subarray of Mr. Meeseeks in the line. Meaning that for each 1u2009β€u2009_i_u2009β€u2009_e_u2009β€u2009_j_u2009β€u2009_n_, if Mr. Meeseeks number _i_ and Mr. Meeseeks number _j_ are in the same squad then Mr. Meeseeks number _e_ should be in that same squad. Also, each squad needs its own presidio, and building a presidio needs money, so they want the total number of squads to be minimized. Rick and Morty haven't finalized the exact value of _k_, so in order to choose it, for each _k_ between 1 and _n_ (inclusive) need to know the minimum number of presidios needed. Input The first line of input contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β number of Mr. Meeseeks. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ separated by spaces (1u2009β€u2009_a__i_u2009β€u2009_n_) β colors of Mr. Meeseeks in order they standing in a line. Output In the first and only line of input print _n_ integers separated by spaces. _i_-th integer should be the minimum number of presidios needed if the value of _k_ is _i_. Note For the first sample testcase, some optimal ways of dividing army into squads for each _k_ are: 1. [1],u2009[3],u2009[4],u2009[3,u20093] 2. [1],u2009[3,u20094,u20093,u20093] 3. [1,u20093,u20094,u20093,u20093] 4. [1,u20093,u20094,u20093,u20093] 5. [1,u20093,u20094,u20093,u20093] For the second testcase, some optimal ways of dividing army into squads for each _k_ are: 1. [1],u2009[5],u2009[7],u2009[8],u2009[1],u2009[7],u2009[6],u2009[1] 2. [1,u20095],u2009[7,u20098],u2009[1,u20097],u2009[6,u20091] 3. [1,u20095,u20097],u2009[8],u2009[1,u20097,u20096,u20091] 4. [1,u20095,u20097,u20098],u2009[1,u20097,u20096,u20091] 5. [1,u20095,u20097,u20098,u20091,u20097,u20096,u20091] 6. [1,u20095,u20097,u20098,u20091,u20097,u20096,u20091] 7. [1,u20095,u20097,u20098,u20091,u20097,u20096,u20091] 8. [1,u20095,u20097,u20098,u20091,u20097,u20096,u20091] | 2,400 | false | false | false | false | true | false | false | false | false | false | 6,626 |
546C | Two bored soldiers are playing card war. Their card deck consists of exactly _n_ cards, numbered from 1 to _n_, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game. The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins. You have to calculate how many fights will happen and who will win the game, or state that game won't end. Input First line contains a single integer _n_ (2u2009β€u2009_n_u2009β€u200910), the number of cards. Second line contains integer _k_1 (1u2009β€u2009_k_1u2009β€u2009_n_u2009-u20091), the number of the first soldier's cards. Then follow _k_1 integers that are the values on the first soldier's cards, from top to bottom of his stack. Third line contains integer _k_2 (_k_1u2009+u2009_k_2u2009=u2009_n_), the number of the second soldier's cards. Then follow _k_2 integers that are the values on the second soldier's cards, from top to bottom of his stack. All card values are different. Output If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won. If the game won't end and will continue forever output u2009-u20091. Note First sample: Second sample: | 1,400 | false | false | false | false | false | false | true | false | false | false | 7,655 |
1945C | In the village of Letovo, there are $$$n$$$ houses. The villagers decided to build a big road that will divide the village into left and right sides. Each resident wants to live on either the right or the left side of the street, which is described as a sequence $$$a_1, a_2, dots, a_n$$$, where $$$a_j = 0$$$ if the resident of the $$$j$$$-th house wants to live on the left side of the street; otherwise, $$$a_j = 1$$$. The road will pass between two houses. The houses to the left of it will be declared the left-side, and the houses to the right will be declared the right-side. More formally, let the road pass between houses $$$i$$$ and $$$i+1$$$. Then the houses at positions between $$$1$$$ and $$$i$$$ will be on the left side of the street, and at positions between $$$i+1$$$ and $$$n$$$ will be on the right side. The road also may pass before the first and after the last house; in this case, the entire village is declared to be either the right or left side, respectively. To make the design fair, it was decided to lay the road so that at least half of the residents on each side of the village are satisfied with the choice. That is, among $$$x$$$ residents on one side, at least $$$lceilfrac{x}{2} ceil$$$ should want to live on that side, where $$$lceil x ceil$$$ denotes rounding up a real number $$$x$$$. To the left of the road, there will be $$$i$$$ houses, among the corresponding $$$a_j$$$ there must be at least $$$lceilfrac{i}{2} ceil$$$ zeros. To the right of the road, there will be $$$n-i$$$ houses, among the corresponding $$$a_j$$$ there must be at least $$$lceilfrac{n-i}{2} ceil$$$ ones. Determine after which house $$$i$$$ the road should be laid in order to satisfy the described condition and be as close to the middle of the village as possible. Formally, among all suitable positions $$$i$$$, minimize $$$leftfrac{n}{2} - i ight$$$. If there are multiple suitable positions $$$i$$$ with the minimum $$$leftfrac{n}{2} - i ight$$$, output the smaller one. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 2cdot 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 3cdot 10^5$$$). The next line of each test case contains a string $$$a$$$ of length $$$n$$$, consisting only of $$$0$$$ and $$$1$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3cdot 10^5$$$. Output For each test case, output a single number $$$i$$$xa0β the position of the house after which the road should be laid (if it should be laid before the first house, output $$$0$$$). We can show that the answer always exists. Note Let's consider the first example of input data. If we lay the road after the first house, there will be one house $$$a_1 = 1$$$ on the left side of the street, the resident of which would like to live on the right side of the street. Then $$$0$$$ out of $$$1$$$ residents on the even side will be satisfied with the choice, which means that the road cannot be laid after house $$$1$$$. If we lay the road after the second house, $$$1$$$ out of $$$2$$$ residents on the left side (with preferences $$$a_1 = 1$$$, $$$a_2 = 0$$$) and $$$1$$$ out of $$$1$$$ resident on the right side (with preference $$$a_3 = 1$$$) will be satisfied with the choice. More than half of the residents on each side are satisfied with the choice, which means that the road can be laid after house $$$2$$$. We can show that this is the optimal answer. | 1,200 | false | false | false | false | false | false | true | false | false | false | 611 |
1622D | You are given a binary string (i.u2009e. a string consisting of characters 0 and/or 1) $$$s$$$ of length $$$n$$$. You can perform the following operation with the string $$$s$$$ at most once: choose a substring (a contiguous subsequence) of $$$s$$$ having exactly $$$k$$$ characters 1 in it, and shuffle it (reorder the characters in the substring as you wish). Calculate the number of different strings which can be obtained from $$$s$$$ by performing this operation at most once. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 5000$$$; $$$0 le k le n$$$). The second line contains the string $$$s$$$ of length $$$n$$$, consisting of characters 0 and/or 1. Output Print one integer β the number of different strings which can be obtained from $$$s$$$ by performing the described operation at most once. Since the answer can be large, output it modulo $$$998244353$$$. Note Some strings you can obtain in the first example: to obtain 0110110, you can take the substring from the $$$1$$$-st character to the $$$4$$$-th character, which is 1100, and reorder its characters to get 0110; to obtain 1111000, you can take the substring from the $$$3$$$-rd character to the $$$7$$$-th character, which is 00110, and reorder its characters to get 11000; to obtain 1100101, you can take the substring from the $$$5$$$-th character to the $$$7$$$-th character, which is 110, and reorder its characters to get 101. In the second example, $$$k = 0$$$ so you can only choose the substrings consisting only of 0 characters. Reordering them doesn't change the string at all, so the only string you can obtain is 10010. | 2,000 | true | false | false | false | false | false | false | false | false | false | 2,508 |
641B | Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it. That element can store information about the matrix of integers size _n_u2009Γu2009_m_. There are _n_u2009+u2009_m_ inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to _n_ from top to bottom, while columns are numbered with integers from 1 to _m_ from left to right. Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of _q_ turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix. Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists. Input The first line of the input contains three integers _n_, _m_ and _q_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009100,u20091u2009β€u2009_q_u2009β€u200910u2009000)xa0β dimensions of the matrix and the number of turns in the experiment, respectively. Next _q_ lines contain turns descriptions, one per line. Each description starts with an integer _t__i_ (1u2009β€u2009_t__i_u2009β€u20093) that defines the type of the operation. For the operation of first and second type integer _r__i_ (1u2009β€u2009_r__i_u2009β€u2009_n_) or _c__i_ (1u2009β€u2009_c__i_u2009β€u2009_m_) follows, while for the operations of the third type three integers _r__i_, _c__i_ and _x__i_ (1u2009β€u2009_r__i_u2009β€u2009_n_, 1u2009β€u2009_c__i_u2009β€u2009_m_, u2009-u2009109u2009β€u2009_x__i_u2009β€u2009109) are given. Operation of the first type (_t__i_u2009=u20091) means that signal comes to the input corresponding to row _r__i_, that is it will shift cyclically. Operation of the second type (_t__i_u2009=u20092) means that column _c__i_ will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row _r__i_ and column _c__i_ stores value _x__i_. Output Print the description of any valid initial matrix as _n_ lines containing _m_ integers each. All output integers should not exceed 109 by their absolute value. If there are multiple valid solutions, output any of them. Examples Input 2 2 6 2 1 2 2 3 1 1 1 3 2 2 2 3 1 2 8 3 2 1 8 | 1,400 | false | false | true | false | false | false | false | false | false | false | 7,250 |
856D | Masha is fond of cacti. When she was a little girl, she decided to plant a tree. Now Masha wants to make a nice cactus out of her tree. Recall that tree is a connected undirected graph that has no cycles. Cactus is a connected undirected graph such that each vertex belongs to at most one cycle. Masha has some additional edges that she can add to a tree. For each edge she knows which vertices it would connect and the beauty of this edge. Masha can add some of these edges to the graph if the resulting graph is a cactus. Beauty of the resulting cactus is sum of beauties of all added edges. Help Masha find out what maximum beauty of the resulting cactus she can achieve. Input The first line of the input data contains two integers _n_ and _m_xa0β the number of vertices in a tree, and the number of additional edges available (3u2009β€u2009_n_u2009β€u20092Β·105; 0u2009β€u2009_m_u2009β€u20092Β·105). Let us describe Masha's tree. It has a root at vertex 1. The second line contains _n_u2009-u20091 integers: _p_2,u2009_p_3,u2009...,u2009_p__n_, here _p__i_xa0β is the parent of a vertex _i_xa0β the first vertex on a path from the vertex _i_ to the root of the tree (1u2009β€u2009_p__i_u2009<u2009_i_). The following _m_ lines contain three integers _u__i_, _v__i_ and _c__i_xa0β pairs of vertices to be connected by the additional edges that Masha can add to the tree and beauty of edge (1u2009β€u2009_u__i_,u2009_v__i_u2009β€u2009_n_; _u__i_u2009β u2009_v__i_; 1u2009β€u2009_c__i_u2009β€u2009104). It is guaranteed that no additional edge coincides with the edge of the tree. Output Output one integerxa0β the maximum beauty of a cactus Masha can achieve. Example Input 7 3 1 1 2 2 3 3 4 5 1 6 7 1 2 3 1 | 2,400 | false | false | false | true | false | false | false | false | false | false | 6,317 |
327C | Problem - 327C - 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 math *1700 No tag edit access β Contest materials to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros. Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109u2009+u20097). Two ways are different, if the set of deleted positions in _s_ differs. Look at the input part of the statement, _s_ is given in a special form. Input In the first line you're given a string _a_ (1u2009β€u2009_a_u2009β€u2009105), containing digits only. In the second line you're given an integer _k_ (1u2009β€u2009_k_u2009β€u2009109). The plate _s_ is formed by concatenating _k_ copies of _a_ together. That is _n_u2009=u2009_a_Β·_k_. Output Print a single integer β the required number of ways modulo 1000000007 (109u2009+u20097). Examples Input 1256 1 Output 4 Input 13990 2 Output 528 Input 555 2 Output 63 Note In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125. In the second case, remember to concatenate the copies of _a_. The actual plate is 1399013990. In the third case, except deleting all digits, any choice will do. Therefore there are 26u2009-u20091u2009=u200963 possible ways to delete digits. | 1,700 | true | false | false | false | false | false | false | false | false | false | 8,534 |
412C | Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given _n_ patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of patterns. Next _n_ lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem β the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on. | 1,200 | false | false | true | false | false | false | false | false | false | false | 8,177 |
1820A | After holding one team contest, boy Yura got very tired and wanted to change his life and move to Japan. In honor of such a change, Yura changed his name to something nice. Fascinated by this idea he already thought up a name $$$s$$$ consisting only of characters "_" and "^". But there's a problem β Yura likes smiley faces "^_^" and "^^". Therefore any character of the name must be a part of at least one such smiley. Note that only the consecutive characters of the name can be a smiley face. More formally, consider all occurrences of the strings "^_^" and "^^" in the string $$$s$$$. Then all such occurrences must cover the whole string $$$s$$$, possibly with intersections. For example, in the string "^^__^_^^__^" the characters at positions $$$3,4,9,10$$$ and $$$11$$$ are not contained inside any smileys, and the other characters at positions $$$1,2,5,6,7$$$ and $$$8$$$ are contained inside smileys. In one operation Jura can insert one of the characters "_" and "^" into his name $$$s$$$ (you can insert it at any position in the string). He asks you to tell him the minimum number of operations you need to do to make the name fit Yura's criteria. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0βthe number of test cases. The description of test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 leq s leq 100$$$), consisting of characters "_" and "^", xa0β the name to change. Output For each test case, output a single integerxa0β the minimum number of characters you need to add to the name to make it fit for Yura. If you don't need to change anything in the name, print $$$0$$$. Example Input 7 ^______^ ___^_^^^_^___^ ^_ ^ ^_^^^^^_^_^^ ___^^ _ Note In the first test case, you can get the following name by adding $$$5$$$ characters: ^_^_^_^_^_^_^ In the third test case, we can add one character "^" to the end of the name, then we get the name: ^_^ In the fourth test case, we can add one character "^" to the end of the name, then we get the name: ^^ In the fifth test case, all of the characters are already contained in smiley faces, so the answer is $$$0$$$. In the seventh test case, you can add one character "^" at the beginning of the name and one character "^" at the end of the name, then you get the name: ^_^ | 800 | false | false | true | false | false | false | false | false | false | false | 1,359 |
242E | You've got an array _a_, consisting of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_. You are allowed to perform two operations on this array: 1. Calculate the sum of current array elements on the segment [_l_,u2009_r_], that is, count value _a__l_u2009+u2009_a__l_u2009+u20091u2009+u2009...u2009+u2009_a__r_. 2. Apply the xor operation with a given number _x_ to each array element on the segment [_l_,u2009_r_], that is, execute . This operation changes exactly _r_u2009-u2009_l_u2009+u20091 array elements. Expression means applying bitwise xor operation to numbers _x_ and _y_. The given operation exists in all modern programming languages, for example in language C++ and Java it is marked as "^", in Pascal β as "xor". You've got a list of _m_ operations of the indicated type. Your task is to perform all given operations, for each sum query you should print the result you get. Input The first line contains integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the size of the array. The second line contains space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009β€u2009106) β the original array. The third line contains integer _m_ (1u2009β€u2009_m_u2009β€u20095Β·104) β the number of operations with the array. The _i_-th of the following _m_ lines first contains an integer _t__i_ (1u2009β€u2009_t__i_u2009β€u20092) β the type of the _i_-th query. If _t__i_u2009=u20091, then this is the query of the sum, if _t__i_u2009=u20092, then this is the query to change array elements. If the _i_-th operation is of type 1, then next follow two integers _l__i_,u2009_r__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_n_). If the _i_-th operation is of type 2, then next follow three integers _l__i_,u2009_r__i_,u2009_x__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_n_,u20091u2009β€u2009_x__i_u2009β€u2009106). The numbers on the lines are separated by single spaces. Output For each query of type 1 print in a single line the sum of numbers on the given segment. Print the answers to the queries in the order in which the queries go in the input. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier. Examples Input 5 4 10 3 13 7 8 1 2 4 2 1 3 3 1 2 4 1 3 3 2 2 5 5 1 1 5 2 1 2 10 1 2 3 Input 6 4 7 4 0 7 3 5 2 2 3 8 1 1 5 2 3 5 1 2 4 5 6 1 2 3 | 2,000 | false | false | false | false | true | false | false | false | false | false | 8,864 |
1070D | Enough is enough. Too many times it happened that Vasya forgot to dispose of garbage and his apartment stank afterwards. Now he wants to create a garbage disposal plan and stick to it. For each of next $$$n$$$ days Vasya knows $$$a_i$$$ β number of units of garbage he will produce on the $$$i$$$-th day. Each unit of garbage must be disposed of either on the day it was produced or on the next day. Vasya disposes of garbage by putting it inside a bag and dropping the bag into a garbage container. Each bag can contain up to $$$k$$$ units of garbage. It is allowed to compose and drop multiple bags into a garbage container in a single day. Being economical, Vasya wants to use as few bags as possible. You are to compute the minimum number of bags Vasya needs to dispose of all of his garbage for the given $$$n$$$ days. No garbage should be left after the $$$n$$$-th day. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2cdot10^5, 1 le k le 10^9$$$) β number of days to consider and bag's capacity. The second line contains $$$n$$$ space separated integers $$$a_i$$$ ($$$0 le a_i le 10^9$$$) β the number of units of garbage produced on the $$$i$$$-th day. Output Output a single integer β the minimum number of bags Vasya needs to dispose of all garbage. Each unit of garbage should be disposed on the day it was produced or on the next day. No garbage can be left after the $$$n$$$-th day. In a day it is allowed to compose and drop multiple bags. Examples Input 5 1 1000000000 1000000000 1000000000 1000000000 1000000000 | 1,300 | false | true | false | false | false | false | false | false | false | false | 5,364 |
215C | There is a board with a grid consisting of _n_ rows and _m_ columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid we will denote the cell that lies on row number _i_ and column number _j_ as (_i_,u2009_j_). A group of six numbers (_a_,u2009_b_,u2009_c_,u2009_d_,u2009_x_0,u2009_y_0), where 0u2009β€u2009_a_,u2009_b_,u2009_c_,u2009_d_, is a cross, and there is a set of cells that are assigned to it. Cell (_x_,u2009_y_) belongs to this set if at least one of two conditions are fulfilled: _x_0u2009-u2009_x_u2009β€u2009_a_ and _y_0u2009-u2009_y_u2009β€u2009_b_ _x_0u2009-u2009_x_u2009β€u2009_c_ and _y_0u2009-u2009_y_u2009β€u2009_d_ The picture shows the cross (0,u20091,u20091,u20090,u20092,u20093) on the grid 3u2009Γu20094. Your task is to find the number of different groups of six numbers, (_a_,u2009_b_,u2009_c_,u2009_d_,u2009_x_0,u2009_y_0) that determine the crosses of an area equal to _s_, which are placed entirely on the grid. The cross is placed entirely on the grid, if any of its cells is in the range of the grid (that is for each cell (_x_,u2009_y_) of the cross 1u2009β€u2009_x_u2009β€u2009_n_;xa01u2009β€u2009_y_u2009β€u2009_m_ holds). The area of the cross is the number of cells it has. Note that two crosses are considered distinct if the ordered groups of six numbers that denote them are distinct, even if these crosses coincide as sets of points. Input The input consists of a single line containing three integers _n_, _m_ and _s_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009500, 1u2009β€u2009_s_u2009β€u2009_n_Β·_m_). The integers are separated by a space. Output Print a single integer β the number of distinct groups of six integers that denote crosses with area _s_ and that are fully placed on the _n_u2009Γu2009_m_ grid. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Note In the first sample the sought groups of six numbers are: (0,u20090,u20090,u20090,u20091,u20091), (0,u20090,u20090,u20090,u20091,u20092), (0,u20090,u20090,u20090,u20092,u20091), (0,u20090,u20090,u20090,u20092,u20092). In the second sample the sought groups of six numbers are: (0,u20091,u20091,u20090,u20092,u20092), (0,u20091,u20091,u20090,u20092,u20093), (1,u20090,u20090,u20091,u20092,u20092), (1,u20090,u20090,u20091,u20092,u20093). | 2,100 | false | false | true | false | false | false | true | false | false | false | 8,979 |
837F | Problem - 837F - Codeforces =============== xa0 , where _x_ is an array of _m_ integers, which returns an array _y_ consisting of _m_u2009+u20091 integers such that _y__i_ is equal to the sum of first _i_ elements of array _x_ (0u2009β€u2009_i_u2009β€u2009_m_). You have an infinite sequence of arrays _A_0,u2009_A_1,u2009_A_2..., where _A_0 is given in the input, and for each _i_u2009β₯u20091 _A__i_u2009=u2009_p_(_A__i_u2009-u20091). Also you have a positive integer _k_. You have to find minimum possible _i_ such that _A__i_ contains a number which is larger or equal than _k_. Input The first line contains two integers _n_ and _k_ (2u2009β€u2009_n_u2009β€u2009200000, 1u2009β€u2009_k_u2009β€u20091018). _n_ is the size of array _A_0. The second line contains _n_ integers _A_00,u2009_A_01... _A_0_n_u2009-u20091 β the elements of _A_0 (0u2009β€u2009_A_0_i_u2009β€u2009109). At least two elements of _A_0 are positive. Output Print the minimum _i_ such that _A__i_ contains a number which is larger or equal than _k_. Examples Input 2 2 1 1 Output 1 Input 3 6 1 1 1 Output 2 Input 3 1 1 0 1 Output 0 | 2,400 | true | false | false | false | false | false | true | true | false | false | 6,416 |
877F | In Ann's favorite book shop are as many as _n_ books on math and economics. Books are numbered from 1 to _n_. Each of them contains non-negative number of problems. Today there is a sale: any subsegment of a segment from _l_ to _r_ can be bought at a fixed price. Ann decided that she wants to buy such non-empty subsegment that the sale operates on it and the number of math problems is greater than the number of economics problems exactly by _k_. Note that _k_ may be positive, negative or zero. Unfortunately, Ann is not sure on which segment the sale operates, but she has _q_ assumptions. For each of them she wants to know the number of options to buy a subsegment satisfying the condition (because the time she spends on choosing depends on that). Currently Ann is too busy solving other problems, she asks you for help. For each her assumption determine the number of subsegments of the given segment such that the number of math problems is greaten than the number of economics problems on that subsegment exactly by _k_. Input The first line contains two integers _n_ and _k_ (1u2009β€u2009_n_u2009β€u2009100u2009000, u2009-u2009109u2009β€u2009_k_u2009β€u2009109) β the number of books and the needed difference between the number of math problems and the number of economics problems. The second line contains _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009β€u2009_t__i_u2009β€u20092), where _t__i_ is 1 if the _i_-th book is on math or 2 if the _i_-th is on economics. The third line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009β€u2009109), where _a__i_ is the number of problems in the _i_-th book. The fourth line contains a single integer _q_ (1u2009β€u2009_q_u2009β€u2009100u2009000) β the number of assumptions. Each of the next _q_ lines contains two integers _l__i_ and _r__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_n_) describing the _i_-th Ann's assumption. Output Print _q_ lines, in the _i_-th of them print the number of subsegments for the _i_-th Ann's assumption. Examples Input 4 1 1 1 1 2 1 1 1 1 4 1 2 1 3 1 4 3 4 Input 4 0 1 2 1 2 0 0 0 0 1 1 4 Note In the first sample Ann can buy subsegments [1;1],u2009[2;2],u2009[3;3],u2009[2;4] if they fall into the sales segment, because the number of math problems is greater by 1 on them that the number of economics problems. So we should count for each assumption the number of these subsegments that are subsegments of the given segment. Segments [1;1] and [2;2] are subsegments of [1;2]. Segments [1;1],u2009[2;2] and [3;3] are subsegments of [1;3]. Segments [1;1],u2009[2;2],u2009[3;3],u2009[2;4] are subsegments of [1;4]. Segment [3;3] is subsegment of [3;4]. | 2,300 | false | false | false | false | true | false | false | false | false | false | 6,235 |
1697B | The store sells $$$n$$$ items, the price of the $$$i$$$-th item is $$$p_i$$$. The store's management is going to hold a promotion: if a customer purchases at least $$$x$$$ items, $$$y$$$ cheapest of them are free. The management has not yet decided on the exact values of $$$x$$$ and $$$y$$$. Therefore, they ask you to process $$$q$$$ queries: for the given values of $$$x$$$ and $$$y$$$, determine the maximum total value of items received for free, if a customer makes one purchase. Note that all queries are independent; they don't affect the store's stock. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$)xa0β the number of items in the store and the number of queries, respectively. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le 10^6$$$), where $$$p_i$$$xa0β the price of the $$$i$$$-th item. The following $$$q$$$ lines contain two integers $$$x_i$$$ and $$$y_i$$$ each ($$$1 le y_i le x_i le n$$$)xa0β the values of the parameters $$$x$$$ and $$$y$$$ in the $$$i$$$-th query. Output For each query, print a single integerxa0β the maximum total value of items received for free for one purchase. Example Input 5 3 5 3 1 5 2 3 2 1 1 5 3 Note In the first query, a customer can buy three items worth $$$5, 3, 5$$$, the two cheapest of them are $$$3 + 5 = 8$$$. In the second query, a customer can buy two items worth $$$5$$$ and $$$5$$$, the cheapest of them is $$$5$$$. In the third query, a customer has to buy all the items to receive the three cheapest of them for free; their total price is $$$1 + 2 + 3 = 6$$$. | 900 | false | true | false | false | false | false | false | false | true | false | 2,099 |
1566H | This is an interactive problem. You are given two integers $$$c$$$ and $$$n$$$. The jury has a randomly generated set $$$A$$$ of distinct positive integers not greater than $$$c$$$ (it is generated from all such possible sets with equal probability). The size of $$$A$$$ is equal to $$$n$$$. Your task is to guess the set $$$A$$$. In order to guess it, you can ask at most $$$lceil 0.65 cdot c ceil$$$ queries. In each query, you choose a single integer $$$1 le x le c$$$. As the answer to this query you will be given the = 1$$$ (i.e. $$$x$$$ and $$$y$$$ are coprime). If there is no such $$$y$$$ this xor sum is equal to $$$0$$$. You can ask all queries at the beginning and you will receive the answers to all your queries. After that, you won't have the possibility to ask queries. You should find any set $$$A'$$$, such that $$$A' = n$$$ and $$$A'$$$ and $$$A$$$ have the same answers for all $$$c$$$ possible queries. Input Firstly you are given two integers $$$c$$$ and $$$n$$$ ($$$100 le c le 10^6$$$, $$$0 le n le c$$$). Interaction In the first line you should print an integer $$$q$$$ $$$(0 le q le lceil 0.65 cdot c ceil)$$$xa0β the number of queries you want to ask. After that in the same line print $$$q$$$ integers $$$x_1, x_2, ldots, x_q$$$ $$$(1 le x_i le c)$$$xa0β the queries. For these queries you should read $$$q$$$ integers, $$$i$$$-th of them is the answer to the described query for $$$x = x_i$$$. After that you should print $$$n$$$ distinct integers $$$A'_1, A'_2, ldots, A'_n$$$xa0β the set $$$A'$$$ you found. If there are different sets $$$A'$$$ that have the same answers for all possible queries, print any of them. If you will ask more than $$$lceil 0.65 cdot c ceil$$$ queries or if the queries will be invalid, the interactor will terminate immediately and your program will receive verdict Wrong Answer. After printing the queries and answers do not forget to output end of line and flush the output buffer. Otherwise, you will get the Idleness limit exceeded verdict. To do flush use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; Read documentation for other languages. Hacks You cannot make hacks in this problem. Example Output 7 10 2 3 5 7 1 6 1 4 5 6 8 10 Note The sample is made only for you to understand the interaction protocol. Your solution will not be tested on the sample. In the sample $$$A = {1, 4, 5, 6, 8, 10}$$$. $$$7$$$ queries are made, $$$7 le lceil 0.65 cdot 10 ceil = 7$$$, so the query limit is not exceeded. Answers for the queries: For $$$10$$$: $$$1$$$ is the only number in the set $$$A$$$ coprime with $$$10$$$, so the answer is $$$1$$$ For $$$2$$$: $$$1_{10} oplus 5_{10} = 001_2 oplus 101_2 = 4_{10}$$$, where $$$oplus$$$ is the bitwise xor For $$$3$$$: $$$1_{10} oplus 4_{10} oplus 5_{10} oplus 8_{10} oplus 10_{10} = 0001_2 oplus 0100_2 oplus 0101_2 oplus 1000_2 oplus 1010_2 = 2_{10}$$$ For $$$5$$$: $$$1_{10} oplus 4_{10} oplus 6_{10} oplus 8_{10} = 0001_2 oplus 0100_2 oplus 0110_2 oplus 1000_2 = 11_{10}$$$ For $$$7$$$: $$$1_{10} oplus 4_{10} oplus 5_{10} oplus 6_{10} oplus 8_{10} oplus 10_{10} = 0001_2 oplus 0100_2 oplus 0101_2 oplus 0110_2 oplus 1000_2 oplus 1010_2 = 4_{10}$$$ For $$$1$$$: $$$1_{10} oplus 4_{10} oplus 5_{10} oplus 6_{10} oplus 8_{10} oplus 10_{10} = 0001_2 oplus 0100_2 oplus 0101_2 oplus 0110_2 oplus 1000_2 oplus 1010_2 = 4_{10}$$$ For $$$6$$$: $$$1_{10} oplus 5_{10} = 0001_2 oplus 0101_2 = 4_{10}$$$ | 3,200 | true | false | false | true | false | true | false | false | false | false | 2,805 |
1996G | On Penacony, The Land of the Dreams, there exists $$$n$$$ houses and $$$n$$$ roads. There exists a road between house $$$i$$$ and $$$i+1$$$ for all $$$1 leq i leq n-1$$$ and a road between house $$$n$$$ and house $$$1$$$. All roads are bidirectional. However, due to the crisis on Penacony, the overseeing family has gone into debt and may not be able to maintain all roads. There are $$$m$$$ pairs of friendships between the residents of Penacony. If the resident living in house $$$a$$$ is friends with the resident living in house $$$b$$$, there must be a path between houses $$$a$$$ and $$$b$$$ through maintained roads. What is the minimum number of roads that must be maintained? Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$) β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3 leq n leq 2 cdot 10^5, 1 leq m leq 2 cdot 10^5$$$) β the number of houses and the number of friendships. The next $$$m$$$ lines contain two integers $$$a$$$ and $$$b$$$ ($$$1 leq a < b leq n$$$) β the resident in house $$$a$$$ is friends with the resident in house $$$b$$$. It is guaranteed all ($$$a, b$$$) are distinct. It is guaranteed the sum of $$$n$$$ and $$$m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output an integer, the minimum number of roads that must be maintained. Example Input 7 8 3 1 8 2 7 4 5 13 4 1 13 2 12 3 11 4 10 10 2 2 3 3 4 10 4 3 8 5 10 2 10 4 10 4 1 1 3 5 2 3 5 1 4 5 2 2 5 1 3 Note For the first test case, the following roads must be maintained: $$$8 leftarrow ightarrow 1$$$ $$$7 leftarrow ightarrow 8$$$ $$$1 leftarrow ightarrow 2$$$ $$$4 leftarrow ightarrow 5$$$ | 2,200 | false | true | false | false | true | false | true | false | false | true | 284 |
1933A | You are given an array $$$a$$$ of $$$n$$$ integers. You must perform the following two operations on the array (the first, then the second): 1. Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged. 2. Choose at most one contiguous segment of elements and replace the signs of all elements in this segment with their opposites. Formally, you can choose a pair of indices $$$l, r$$$ such that $$$1 le l le r le n$$$ and assign $$$a_i = -a_i$$$ for all $$$l le i le r$$$ (negate elements). Note that you may choose not to select a pair of indices and leave all the signs of the elements unchanged. What is the maximum sum of the array elements after performing these two operations (the first, then the second)? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 50$$$)xa0β the number of elements in array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-100 le a_i le 100$$$)xa0β elements of the array. Note In the first test case, you can first rearrange the array to get $$$[3,-2,-3]$$$ (operation 1), then choose $$$l = 2, r = 3$$$ and get the sum $$$3 + -((-2) + (-3)) = 8$$$ (operation 2). In the second test case, you can do nothing in both operations and get the sum $$$0$$$. In the third test case, you can do nothing in both operations and get the sum $$$0 + 1 = 1$$$. In the fourth test case, you can first leave the order unchanged (operation 1), then choose $$$l = 1, r = 1$$$ and get the sum $$$-(-99) = 99$$$ (operation 2). In the fifth test case, you can first leave the order unchanged (operation 1), then choose $$$l = 2, r = 3$$$ and get the sum $$$10 + -((-2) + (-3)) + 7 = 22$$$ (operation 2). In the sixth test case, you can first leave the order unchanged (operation 1), then choose $$$l = 1, r = 5$$$ and get the sum $$$-((-1)+(-2)+(-3)+(-4)+(-5))=15$$$ (operation 2). | 800 | true | true | false | false | false | false | false | false | true | false | 687 |
25B | Problem - 25B - 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 implementation *1100 No tag edit access β Contest materials ") β amount of digits in the phone number. The second line contains _n_ digits β the phone number to divide into groups. Output Output any of divisions of the given phone number into groups of two or three digits. Separate groups by single character -. If the answer is not unique, output any. Examples Input 6 549871 Output 54-98-71 Input 7 1198733 Output 11-987-33 | 1,100 | false | false | true | false | false | false | false | false | false | false | 9,872 |
1478C | Long time ago there was a symmetric array $$$a_1,a_2,ldots,a_{2n}$$$ consisting of $$$2n$$$ distinct integers. Array $$$a_1,a_2,ldots,a_{2n}$$$ is called symmetric if for each integer $$$1 le i le 2n$$$, there exists an integer $$$1 le j le 2n$$$ such that $$$a_i = -a_j$$$. For each integer $$$1 le i le 2n$$$, Nezzar wrote down an integer $$$d_i$$$ equal to the sum of absolute differences from $$$a_i$$$ to all integers in $$$a$$$, i.xa0e. $$$d_i = sum_{j = 1}^{2n} {a_i - a_j}$$$. Now a million years has passed and Nezzar can barely remember the array $$$d$$$ and totally forget $$$a$$$. Nezzar wonders if there exists any symmetric array $$$a$$$ consisting of $$$2n$$$ distinct integers that generates the array $$$d$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line of each test case contains $$$2n$$$ integers $$$d_1, d_2, ldots, d_{2n}$$$ ($$$0 le d_i le 10^{12}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print "YES" in a single line if there exists a possible array $$$a$$$. Otherwise, print "NO". You can print letters in any case (upper or lower). Example Input 6 2 8 12 8 12 2 7 7 9 11 2 7 11 7 11 1 1 1 4 40 56 48 40 80 56 80 48 6 240 154 210 162 174 154 186 240 174 186 162 210 Output YES NO NO NO NO YES Note In the first test case, $$$a=[1,-3,-1,3]$$$ is one possible symmetric array that generates the array $$$d=[8,12,8,12]$$$. In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array $$$d$$$. | 1,700 | true | false | true | false | false | false | false | false | true | false | 3,282 |
1398B | Alice and Bob play a game. They have a binary string $$$s$$$ (a string such that each character in it is either $$$0$$$ or $$$1$$$). Alice moves first, then Bob, then Alice again, and so on. During their move, the player can choose any number (not less than one) of consecutive equal characters in $$$s$$$ and delete them. For example, if the string is $$$10110$$$, there are $$$6$$$ possible moves (deleted characters are bold): 1. $$$ extbf{1}0110 o 0110$$$; 2. $$$1 extbf{0}110 o 1110$$$; 3. $$$10 extbf{1}10 o 1010$$$; 4. $$$101 extbf{1}0 o 1010$$$; 5. $$$10 extbf{11}0 o 100$$$; 6. $$$1011 extbf{0} o 1011$$$. After the characters are removed, the characters to the left and to the right of the removed block become adjacent. I.u2009e. the following sequence of moves is valid: $$$10 extbf{11}0 o 1 extbf{00} o 1$$$. The game ends when the string becomes empty, and the score of each player is the number of $$$1$$$-characters deleted by them. Each player wants to maximize their score. Calculate the resulting score of Alice. Input The first line contains one integer $$$T$$$ ($$$1 le T le 500$$$) β the number of test cases. Each test case contains exactly one line containing a binary string $$$s$$$ ($$$1 le s le 100$$$). Output For each test case, print one integer β the resulting score of Alice (the number of $$$1$$$-characters deleted by her). Example Input 5 01111001 0000 111111 101010101 011011110111 Note Questions about the optimal strategy will be ignored. | 800 | false | true | false | false | false | false | false | false | true | false | 3,685 |
710F | You should process _m_ queries over a set _D_ of strings. Each query is one of three kinds: 1. Add a string _s_ to the set _D_. It is guaranteed that the string _s_ was not added before. 2. Delete a string _s_ from the set _D_. It is guaranteed that the string _s_ is in the set _D_. 3. For the given string _s_ find the number of occurrences of the strings from the set _D_. If some string _p_ from _D_ has several occurrences in _s_ you should count all of them. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query of the third type. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program. Input The first line contains integer _m_ (1u2009β€u2009_m_u2009β€u20093Β·105) β the number of queries. Each of the next _m_ lines contains integer _t_ (1u2009β€u2009_t_u2009β€u20093) and nonempty string _s_ β the kind of the query and the string to process. All strings consist of only lowercase English letters. The sum of lengths of all strings in the input will not exceed 3Β·105. Output For each query of the third kind print the only integer _c_ β the desired number of occurrences in the string _s_. Examples Input 5 1 abc 3 abcabc 2 abc 1 aba 3 abababc Input 10 1 abc 1 bcd 1 abcd 3 abcd 2 abcd 3 abcd 2 bcd 3 abcd 2 abc 3 abcd | 2,400 | false | false | false | false | true | false | true | false | false | false | 6,977 |
148A | Problem - 148A - 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 constructive algorithms implementation math *800 No tag edit access β Contest materials ") : editorial") . Output Output the number of damaged dragons. Examples Input 1 2 3 4 12 Output 12 Input 2 3 4 5 24 Output 17 Note In the first case every first dragon got punched with a frying pan. Some of the dragons suffered from other reasons as well, but the pan alone would be enough. In the second case dragons 1, 7, 11, 13, 17, 19 and 23 escaped unharmed. | 800 | true | false | true | false | false | true | false | false | false | false | 9,286 |
536E | Tavas lives in Tavaspolis. Tavaspolis has _n_ cities numbered from 1 to _n_ connected by _n_u2009-u20091 bidirectional roads. There exists a path between any two cities. Also each road has a length. Tavas' favorite strings are binary strings (they contain only 0 and 1). For any binary string like _s_u2009=u2009_s_1_s_2... _s__k_, _T_(_s_) is its _Goodness_. _T_(_s_) can be calculated as follows: Consider there are exactly _m_ blocks of 1s in this string (a block of 1s in _s_ is a maximal consecutive substring of _s_ that only contains 1) with lengths _x_1,u2009_x_2,u2009...,u2009_x__m_. Define where _f_ is a given sequence (if _m_u2009=u20090, then _T_(_s_)u2009=u20090). Tavas loves queries. He asks you to answer _q_ queries. In each query he gives you numbers _v_,u2009_u_,u2009_l_ and you should print following number: Consider the roads on the path from city _v_ to city _u_: _e_1,u2009_e_2,u2009...,u2009_e__x_. Build the binary string _b_ of length _x_ such that: _b__i_u2009=u20091 if and only if _l_u2009β€u2009_w_(_e__i_) where _w_(_e_) is the length of road _e_. You should print _T_(_b_) for this query. Input The first line of input contains integers _n_ and _q_ (2u2009β€u2009_n_u2009β€u2009105 and 1u2009β€u2009_q_u2009β€u2009105). The next line contains _n_u2009-u20091 space separated integers _f_1,u2009_f_2,u2009...,u2009_f__n_u2009-u20091 (_f__i_u2009β€u20091000). The next _n_u2009-u20091 lines contain the details of the roads. Each line contains integers _v_,u2009_u_ and _w_ and it means that there's a road between cities _v_ and _u_ of length _w_ (1u2009β€u2009_v_,u2009_u_u2009β€u2009_n_ and 1u2009β€u2009_w_u2009β€u2009109). The next _q_ lines contain the details of the queries. Each line contains integers _v_,u2009_u_,u2009_l_ (1u2009β€u2009_v_,u2009_u_u2009β€u2009_n_, _v_u2009β u2009_u_ and 1u2009β€u2009_l_u2009β€u2009109). | 3,100 | false | false | false | false | true | false | false | false | false | false | 7,689 |
1195C | Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $$$2 cdot n$$$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $$$n$$$ people in each row). Students are numbered from $$$1$$$ to $$$n$$$ in each row in order from left to right. Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $$$2n$$$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. Input The first line of the input contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β the number of students in each row. The second line of the input contains $$$n$$$ integers $$$h_{1, 1}, h_{1, 2}, ldots, h_{1, n}$$$ ($$$1 le h_{1, i} le 10^9$$$), where $$$h_{1, i}$$$ is the height of the $$$i$$$-th student in the first row. The third line of the input contains $$$n$$$ integers $$$h_{2, 1}, h_{2, 2}, ldots, h_{2, n}$$$ ($$$1 le h_{2, i} le 10^9$$$), where $$$h_{2, i}$$$ is the height of the $$$i$$$-th student in the second row. Output Print a single integer β the maximum possible total height of players in a team Demid can choose. Examples Input 5 9 3 5 7 3 5 8 1 4 5 Note In the first example Demid can choose the following team as follows: In the second example Demid can choose the following team as follows: | 1,400 | false | false | false | true | false | false | false | false | false | false | 4,727 |
1277D | Polycarp has $$$n$$$ different binary words. A word called binary if it contains only characters '0' and '1'. For example, these words are binary: "0001", "11", "0" and "0011100". Polycarp wants to offer his set of $$$n$$$ binary words to play a game "words". In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: "0101", "1", "10", "00", "00001". Word reversal is the operation of reversing the order of the characters. For example, the word "0111" after the reversal becomes "1110", the word "11010" after the reversal becomes "01011". Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that: the final set of $$$n$$$ words still contains different words (i.e. all words are unique); there is a way to put all words of the final set of words in the order so that the final sequence of $$$n$$$ words is consistent with the game rules. Polycarp wants to reverse minimal number of words. Please, help him. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 le n le 2cdot10^5$$$) β the number of words in the Polycarp's set. Next $$$n$$$ lines contain these words. All of $$$n$$$ words aren't empty and contains only characters '0' and '1'. The sum of word lengths doesn't exceed $$$4cdot10^6$$$. All words are different. Guaranteed, that the sum of $$$n$$$ for all test cases in the input doesn't exceed $$$2cdot10^5$$$. Also, guaranteed that the sum of word lengths for all test cases in the input doesn't exceed $$$4cdot10^6$$$. Output Print answer for all of $$$t$$$ test cases in the order they appear. If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain $$$k$$$ ($$$0 le k le n$$$) β the minimal number of words in the set which should be reversed. The second line of the output should contain $$$k$$$ distinct integers β the indexes of the words in the set which should be reversed. Words are numerated from $$$1$$$ to $$$n$$$ in the order they appear. If $$$k=0$$$ you can skip this line (or you can print an empty line). If there are many answers you can print any of them. | 1,900 | true | false | true | false | true | false | false | false | false | false | 4,307 |
53C | Problem - 53C - 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 constructive algorithms *1200 No tag edit access β Contest materials ") . All problems now.") ") which is the number of mounds. Output Print _n_ integers _p__i_ (1u2009β€u2009_p__i_u2009β€u2009_n_) which are the frog's route plan. All the _p__i_'s should be mutually different. All the _p__i_β_p__i_u2009+u20091's should be mutually different (1u2009β€u2009_i_u2009β€u2009_n_u2009-u20091). If there are several solutions, output any. Examples Input 2 Output 1 2 Input 3 Output 1 3 2 | 1,200 | false | false | false | false | false | true | false | false | false | false | 9,708 |
920D | Petya sometimes has to water his field. To water the field, Petya needs a tank with exactly _V_ ml of water. Petya has got _N_ tanks, _i_-th of them initially containing _a__i_ ml of water. The tanks are really large, any of them can contain any amount of water (no matter how large this amount is). Also Petya has got a scoop that can contain up to _K_ ml of water (initially the scoop is empty). This scoop can be used to get some water from some tank, and after that pour it all into some tank (it is impossible to get water from multiple tanks without pouring it, or leave some water in the scoop when pouring it). When Petya tries to get some water from a tank, he gets _min_(_v_,u2009_K_) water, where _v_ is the current volume of water in the tank. Is it possible to obtain a tank with exactly _V_ ml of water using these operations? If it is possible, print a sequence of operations that allows to do it. If there are multiple ways to obtain needed amount of water in some tank, print any of them. Input The first line contains 3 integers: _N_ (2u2009β€u2009_N_u2009β€u20095000), _K_ (1u2009β€u2009_K_u2009β€u20095000), and _V_ (0u2009β€u2009_V_u2009β€u2009109) β the number of tanks, the maximum volume of water the scoop can contain, and the required amount of water in some tank, respectively. The second line contains _N_ integers _a__i_ (0u2009β€u2009_a__i_u2009β€u2009105), where _a__i_ is initial volume of water in _i_-th tank. Output If it is impossible to obtain a tank with exactly _V_ ml of water, print NO. Otherwise print YES in the first line, and beginning from the second line, print the sequence of operations in the following format: Each line has to contain 3 numbers denoting a compressed operation: "_cnt_ _x_ _y_" (1u2009β€u2009_cnt_u2009β€u2009109,u20091u2009β€u2009_x_,u2009_y_u2009β€u2009_N_), where _x_ is the index of the tank where we get water, _y_ is the index of the tank where we pour water, and _cnt_ is the number of times we transfer water from tank _x_ to tank _y_. The number of these lines must not exceed _N_u2009+u20095. Examples Output YES 2 2 1 3 3 1 4 4 1 5 5 1 | 2,400 | false | true | true | true | false | false | false | false | false | false | 6,047 |
915D | You are given a . You are allowed to remove at most one edge from it. Can you make this graph . Input The first line contains two integers _n_ and _m_ (2u2009β€u2009_n_u2009β€u2009500, 1u2009β€u2009_m_u2009β€u2009_min_(_n_(_n_u2009-u20091),u2009100000)) β the number of vertices and the number of edges, respectively. Then _m_ lines follow. Each line contains two integers _u_ and _v_ denoting a directed edge going from vertex _u_ to vertex _v_ (1u2009β€u2009_u_,u2009_v_u2009β€u2009_n_, _u_u2009β u2009_v_). Each ordered pair (_u_,u2009_v_) is listed at most once (there is at most one directed edge from _u_ to _v_). Output If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO. Examples Input 5 6 1 2 2 3 3 2 3 1 2 1 4 5 Note In the first example you can remove edge , and the graph becomes acyclic. In the second example you have to remove at least two edges (for example, and ) in order to make the graph acyclic. | 2,200 | false | false | false | false | false | false | false | false | false | true | 6,072 |
1687E | Come, let's build a world where even the weak are not forgotten! βKijin Seija, Double Dealing Characters Shinmyoumaru has a mallet that can turn objects bigger or smaller. She is testing it out on a sequence $$$a$$$ and a number $$$v$$$ whose initial value is $$$1$$$. She wants to make $$$v = gcdlimits_{i e j}{a_icdot a_j}$$$ by no more than $$$10^5$$$ operations ($$$gcdlimits_{i e j}{a_icdot a_j}$$$ denotes the $$$gcd$$$ of all products of two distinct elements of the sequence $$$a$$$). In each operation, she picks a subsequence $$$b$$$ of $$$a$$$, and does one of the followings: Enlarge: $$$v = v cdot mathrm{lcm}(b)$$$ Reduce: $$$v = frac{v}{mathrm{lcm}(b)}$$$ Note that she does not need to guarantee that $$$v$$$ is an integer, that is, $$$v$$$ does not need to be a multiple of $$$mathrm{lcm}(b)$$$ when performing Reduce. Moreover, she wants to guarantee that the total length of $$$b$$$ chosen over the operations does not exceed $$$10^6$$$. Fine a possible operation sequence for her. You don't need to minimize anything. Input The first line contains a single integer $$$n$$$ ($$$2leq nleq 10^5$$$) β the size of sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,cdots,a_n$$$ ($$$1leq a_ileq 10^6$$$) β the sequence $$$a$$$. It can be shown that the answer exists. Output The first line contains a non-negative integer $$$k$$$ ($$$0leq kleq 10^5$$$) β the number of operations. The following $$$k$$$ lines contains several integers. For each line, the first two integers $$$f$$$ ($$$fin{0,1}$$$) and $$$p$$$ ($$$1le ple n$$$) stand for the option you choose ($$$0$$$ for Enlarge and $$$1$$$ for Reduce) and the length of $$$b$$$. The other $$$p$$$ integers of the line $$$i_1,i_2,ldots,i_p$$$ ($$$1le i_1<i_2<ldots<i_ple n$$$) represents the indexes of the subsequence. Formally, $$$b_j=a_{i_j}$$$. Note Test case 1: $$$gcdlimits_{i e j}{a_icdot a_j}=gcd{60,90,150}=30$$$. Perform $$$v = vcdot operatorname{lcm}{a_1,a_2,a_3}=30$$$. Test case 2: $$$gcdlimits_{i e j}{a_icdot a_j}=8$$$. Perform $$$v = vcdot operatorname{lcm}{a_4}=16$$$. Perform $$$v = frac{v}{operatorname{lcm}{a_1}}=8$$$. | 3,500 | true | true | false | false | false | true | false | false | false | false | 2,153 |
1202C | You have a string $$$s$$$ β a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' β move one cell up; 'S' β move one cell down; 'A' β move one cell left; 'D' β move one cell right. Let $$$Grid(s)$$$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $$$s$$$. For example, if $$$s = ext{DSAWWAW}$$$ then $$$Grid(s)$$$ is the $$$4 imes 3$$$ grid: 1. you can place the robot in the cell $$$(3, 2)$$$; 2. the robot performs the command 'D' and moves to $$$(3, 3)$$$; 3. the robot performs the command 'S' and moves to $$$(4, 3)$$$; 4. the robot performs the command 'A' and moves to $$$(4, 2)$$$; 5. the robot performs the command 'W' and moves to $$$(3, 2)$$$; 6. the robot performs the command 'W' and moves to $$$(2, 2)$$$; 7. the robot performs the command 'A' and moves to $$$(2, 1)$$$; 8. the robot performs the command 'W' and moves to $$$(1, 1)$$$. You have $$$4$$$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $$$s$$$ to minimize the area of $$$Grid(s)$$$. What is the minimum area of $$$Grid(s)$$$ you can achieve? Input The first line contains one integer $$$T$$$ ($$$1 le T le 1000$$$) β the number of queries. Next $$$T$$$ lines contain queries: one per line. This line contains single string $$$s$$$ ($$$1 le s le 2 cdot 10^5$$$, $$$s_i in { ext{W}, ext{A}, ext{S}, ext{D}}$$$) β the sequence of commands. It's guaranteed that the total length of $$$s$$$ over all queries doesn't exceed $$$2 cdot 10^5$$$. Output Print $$$T$$$ integers: one per query. For each query print the minimum area of $$$Grid(s)$$$ you can achieve. Note In the first query you have to get string $$$ ext{DSAWW}underline{D} ext{AW}$$$. In second and third queries you can not decrease the area of $$$Grid(s)$$$. | 2,100 | true | true | true | true | true | false | true | false | false | false | 4,687 |
254D | Rats have bred to hundreds and hundreds in the basement of the store, owned by Vasily Petrovich. Vasily Petrovich may have not noticed their presence, but they got into the habit of sneaking into the warehouse and stealing food from there. Vasily Petrovich cannot put up with it anymore, he has to destroy the rats in the basement. Since mousetraps are outdated and do not help, and rat poison can poison inattentive people as well as rats, he chose a radical way: to blow up two grenades in the basement (he does not have more). In this problem, we will present the shop basement as a rectangular table of _n_u2009Γu2009_m_ cells. Some of the cells are occupied by walls, and the rest of them are empty. Vasily has been watching the rats and he found out that at a certain time they go to sleep, and all the time they sleep in the same places. He wants to blow up a grenade when this convenient time comes. On the plan of his basement, he marked cells with sleeping rats in them. Naturally, these cells are not occupied by walls. Grenades can only blow up in a cell that is not occupied by a wall. The blast wave from a grenade distributes as follows. We assume that the grenade blast occurs at time 0. During this initial time only the cell where the grenade blew up gets 'clear'. If at time _t_ some cell is clear, then at time _t_u2009+u20091 those side-neighbouring cells which are not occupied by the walls get clear too (some of them could have been cleared before). The blast wave distributes for exactly _d_ seconds, then it dies immediately. An example of a distributing blast wave: Picture 1 shows the situation before the blast, and the following pictures show "clear" cells by time 0,1,2,3 and 4. Thus, the blast wave on the picture distributes for _d_u2009=u20094 seconds. Vasily Petrovich wonders, whether he can choose two cells to blast the grenades so as to clear all cells with sleeping rats. Write the program that finds it out. Input The first line contains three integers _n_, _m_ and _d_, separated by single spaces (4u2009β€u2009_n_,u2009_m_u2009β€u20091000,u20091u2009β€u2009_d_u2009β€u20098). Next _n_ lines contain the table that represents the basement plan. Each row of the table consists of _m_ characters. Character "X" means that the corresponding cell is occupied by the wall, character "." represents a empty cell, character "R" represents a empty cell with sleeping rats. It is guaranteed that the first and the last row, as well as the first and the last column consist of characters "X". The plan has at least two empty cells. There is at least one cell with sleeping rats. Output If it is impossible to blow up all cells with sleeping rats, print a single integer -1. Otherwise, print four space-separated integers _r_1,u2009_c_1,u2009_r_2,u2009_c_2, that mean that one grenade should go off in cell (_r_1,u2009_c_1), and the other one β in cell (_r_2,u2009_c_2). Consider the table rows numbered from top to bottom from 1 to _n_ and the table columns β from left to right from 1 to _m_. As _r_1 and _r_2 represent the row numbers, and _c_1 and _c_2 represent the column numbers in the table, they should fit the limits: 1u2009β€u2009_r_1,u2009_r_2u2009β€u2009_n_,u20091u2009β€u2009_c_1,u2009_c_2u2009β€u2009_m_. It is forbidden to blow a grenade twice in the same cell. The blast waves of the grenades can intersect. It is possible that one grenade blast destroys no rats, and the other one destroys all of them. Examples Input 4 4 1 XXXX XR.X X.RX XXXX Input 9 14 5 XXXXXXXXXXXXXX X....R...R...X X..R.........X X....RXR..R..X X..R...X.....X XR.R...X.....X X....XXR.....X X....R..R.R..X XXXXXXXXXXXXXX Input 7 7 1 XXXXXXX X.R.R.X X.....X X..X..X X..R..X X....RX XXXXXXX | 2,300 | false | false | true | false | false | false | true | false | false | true | 8,816 |
1675C | Polycarp bought a new expensive painting and decided to show it to his $$$n$$$ friends. He hung it in his room. $$$n$$$ of his friends entered and exited there one by one. At one moment there was no more than one person in the room. In other words, the first friend entered and left first, then the second, and so on. It is known that at the beginning (before visiting friends) a picture hung in the room. At the end (after the $$$n$$$-th friend) it turned out that it disappeared. At what exact moment it disappearedxa0β there is no information. Polycarp asked his friends one by one. He asked each one if there was a picture when he entered the room. Each friend answered one of three: no (response encoded with 0); yes (response encoded as 1); can't remember (response is encoded with ?). Everyone except the thief either doesn't remember or told the truth. The thief can say anything (any of the three options). Polycarp cannot understand who the thief is. He asks you to find out the number of those who can be considered a thief according to the answers. Input The first number $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases in the test. The following is a description of test cases. The first line of each test case contains one string $$$s$$$ (length does not exceed $$$2 cdot 10^5$$$)xa0β a description of the friends' answers, where $$$s_i$$$ indicates the answer of the $$$i$$$-th friend. Each character in the string is either 0 or 1 or ?. The given regularity is described in the actual situation. In particular, on the basis of answers, at least one friend can be suspected of stealing a painting. It is guaranteed that the sum of string lengths over the entire input data set does not exceed $$$2 cdot 10^5$$$. Output Output one positive (strictly more zero) numberxa0β the number of people who could steal the picture based on the data shown. Example Input 8 0 1 1110000 ????? 1?1??0?0 0?0??? ??11 ??0?? Note In the first case, the answer is $$$1$$$ since we had exactly $$$1$$$ friend. The second case is similar to the first. In the third case, the suspects are the third and fourth friends (we count from one). It can be shown that no one else could be the thief. In the fourth case, we know absolutely nothing, so we suspect everyone. | 1,100 | false | false | true | false | false | false | false | false | false | false | 2,220 |
2038I | Berland is this year's host country of the International Collegiate Polyathlon Competition! Similar to biathlon being a competition of two sports, polyathlon is a competition of many sports. This year, there are $$$m$$$ sports. Also, there are $$$n$$$ participants in the event. The sports are numbered from $$$1$$$ to $$$m$$$, and the participants are numbered from $$$1$$$ to $$$n$$$. Some participants are skilled in multiple sports. You are given a binary matrix $$$n imes m$$$ such that the $$$j$$$-th character of the $$$i$$$-th row is 1 if the $$$i$$$-th participant is skilled in the $$$j$$$-th sport, and 0, otherwise. It's also known that, for each pair of participants, there exists at least one sport such that one of them is skilled in that sport and the other one isn't. The order of sports in the competition is determined at the opening ceremony. Historically, it's done by the almighty Random Number Generator. A random number $$$x$$$ from $$$1$$$ to $$$m$$$ is rolled. The competition then starts with the sport $$$x$$$, then the sport $$$(x bmod m + 1)$$$ is played, then the sport $$$((x + 1) bmod m + 1)$$$, and so on. Each sport is played as follows. If all remaining participants (all participants which are not eliminated yet) are not skilled in that sport, everyone goes through to the next sport. Otherwise, all skilled participants go through to the next sport, and all unskilled participants are eliminated from the competition. Once there is a single participant remaining in the competition, the competition ends, and that participant is declared the winner. As an organizer of the competition, you are curious of the possible outcomes of the competition beforehand (not that you are going to rig the random roll, how could you possibly think that...). For each sport $$$x$$$, print the index of the winner if the competition starts with the sport $$$x$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 10^6$$$; $$$n le 2^m$$$; $$$nm le 2 cdot 10^6$$$)xa0β the number of participants and the number of sports, respectively. The $$$i$$$-th of the next $$$n$$$ lines contains a binary string consisting of exactly $$$m$$$ characters 0 or 1xa0β the skillset of the $$$i$$$-th participant. If the $$$j$$$-th character is 1, the $$$i$$$-th participant is skilled in the $$$j$$$-th sport. If it's 0, the $$$i$$$-th participant is not skilled in the $$$j$$$-th sport. Additional constraint on the input: for each pair of participants, there exists at least one sport such that one of them is skilled in that sport and the other one isn't. In other words, all $$$n$$$ binary strings are pairwise distinct. | 2,500 | false | false | false | false | true | false | false | true | false | false | 30 |
1689B | Monocarp is a little boy who lives in Byteland and he loves programming. Recently, he found a permutation of length $$$n$$$. He has to come up with a mystic permutation. It has to be a new permutation such that it differs from the old one in each position. More formally, if the old permutation is $$$p_1,p_2,ldots,p_n$$$ and the new one is $$$q_1,q_2,ldots,q_n$$$ it must hold that $$$$$$p_1 eq q_1, p_2 eq q_2, ldots ,p_n eq q_n.$$$$$$ Monocarp is afraid of lexicographically large permutations. Can you please help him to find the lexicographically minimal mystic permutation? Input There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1leq tleq 200$$$)xa0β the number of test cases. This is followed by the test cases description. The first line of each test case contains a positive integer $$$n$$$ ($$$1leq nleq 1000$$$)xa0β the length of the permutation. The second line of each test case contains $$$n$$$ distinct positive integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq n$$$). It's guaranteed that $$$p$$$ is a permutation, i.xa0e. $$$p_i eq p_j$$$ for all $$$i eq j$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$1000$$$ over all test cases. Note In the first test case possible permutations that are mystic are $$$[2,3,1]$$$ and $$$[3,1,2]$$$. Lexicographically smaller of the two is $$$[2,3,1]$$$. In the second test case, $$$[1,2,3,4,5]$$$ is the lexicographically minimal permutation and it is also mystic. In third test case possible mystic permutations are $$$[1,2,4,3]$$$, $$$[1,4,2,3]$$$, $$$[1,4,3,2]$$$, $$$[3,1,4,2]$$$, $$$[3,2,4,1]$$$, $$$[3,4,2,1]$$$, $$$[4,1,2,3]$$$, $$$[4,1,3,2]$$$ and $$$[4,3,2,1]$$$. The smallest one is $$$[1,2,4,3]$$$. | 900 | false | true | false | false | true | false | false | false | false | false | 2,147 |
1296E2 | This is a hard version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. You have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $$$s$$$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the length of $$$s$$$. The second line of the input contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters. Output In the first line print one integer $$$res$$$ ($$$1 le res le n$$$) β the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps. In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array $$$c$$$ of length $$$n$$$, where $$$1 le c_i le res$$$ and $$$c_i$$$ means the color of the $$$i$$$-th character. Examples Output 2 1 1 2 1 2 1 2 1 2 | 2,000 | false | false | false | true | true | false | false | false | false | false | 4,212 |
1508C | As a teacher, Riko Hakozaki often needs to help her students with problems from various subjects. Today, she is asked a programming task which goes as follows. You are given an undirected complete graph with $$$n$$$ nodes, where some edges are pre-assigned with a positive weight while the rest aren't. You need to assign all unassigned edges with non-negative weights so that in the resulting fully-assigned complete graph the $$$ with $$$1 le u < v le n$$$; such a graph has $$$frac{n(n-1)}{2}$$$ edges. She is not sure how to solve this problem, so she asks you to solve it for her. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$0 le m le min(2 cdot 10^5, frac{n(n-1)}{2} - 1)$$$) xa0β the number of nodes and the number of pre-assigned edges. The inputs are given so that there is at least one unassigned edge. The $$$i$$$-th of the following $$$m$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u e v$$$, $$$1 le w_i < 2^{30}$$$), representing the edge from $$$u_i$$$ to $$$v_i$$$ has been pre-assigned with the weight $$$w_i$$$. No edge appears in the input more than once. Output Print on one line one integer xa0β the minimum ugliness among all weight assignments with XOR sum equal to $$$0$$$. Examples Input 4 4 2 1 14 1 4 14 3 2 15 4 3 8 Input 6 6 3 6 4 2 4 1 4 5 7 3 4 10 3 5 1 5 2 15 Input 5 6 2 3 11 5 3 7 1 4 10 2 4 14 4 3 8 2 5 6 Note The following image showcases the first test case. The black weights are pre-assigned from the statement, the red weights are assigned by us, and the minimum spanning tree is denoted by the blue edges. | 2,500 | false | true | false | false | true | false | true | false | false | true | 3,134 |
1452C | You are given a string $$$s$$$, consisting of brackets of two types: '(', ')', ' (", then "()" is left. After that you can erase it whole. You could erase the whole string from the beginning but you would get one move instead of two. In the third example you can first erase the brackets on positions $$$1$$$ and $$$3$$$: "([)]". They form an RBS "()". Then "[]" is left, so you can erase it whole. In the fourth example there is no subsequence that is an RBS, so you can't perform a move at all. In the fifth example you can erase the brackets on positions $$$2$$$ and $$$4$$$: ")[(]" and get ")(" as a result. You can erase nothing from it. | 800 | false | true | false | false | false | false | false | false | false | false | 3,419 |
1076E | Vasya has a tree consisting of $$$n$$$ vertices with root in vertex $$$1$$$. At first all vertices has $$$0$$$ written on it. Let $$$d(i, j)$$$ be the distance between vertices $$$i$$$ and $$$j$$$, i.e. number of edges in the shortest path from $$$i$$$ to $$$j$$$. Also, let's denote $$$k$$$-subtree of vertex $$$x$$$ β set of vertices $$$y$$$ such that next two conditions are met: $$$x$$$ is the ancestor of $$$y$$$ (each vertex is the ancestor of itself); $$$d(x, y) le k$$$. Vasya needs you to process $$$m$$$ queries. The $$$i$$$-th query is a triple $$$v_i$$$, $$$d_i$$$ and $$$x_i$$$. For each query Vasya adds value $$$x_i$$$ to each vertex from $$$d_i$$$-subtree of $$$v_i$$$. Report to Vasya all values, written on vertices of the tree after processing all queries. Input The first line contains single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$) β number of vertices in the tree. Each of next $$$n - 1$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$) β edge between vertices $$$x$$$ and $$$y$$$. It is guarantied that given graph is a tree. Next line contains single integer $$$m$$$ ($$$1 le m le 3 cdot 10^5$$$) β number of queries. Each of next $$$m$$$ lines contains three integers $$$v_i$$$, $$$d_i$$$, $$$x_i$$$ ($$$1 le v_i le n$$$, $$$0 le d_i le 10^9$$$, $$$1 le x_i le 10^9$$$) β description of the $$$i$$$-th query. Output Print $$$n$$$ integers. The $$$i$$$-th integers is the value, written in the $$$i$$$-th vertex after processing all queries. Examples Input 5 1 2 1 3 2 4 2 5 3 1 1 1 2 0 10 4 10 100 Input 5 2 3 2 1 5 4 3 4 5 2 0 4 3 10 1 1 2 3 2 3 10 1 1 7 Note In the first exapmle initial values in vertices are $$$0, 0, 0, 0, 0$$$. After the first query values will be equal to $$$1, 1, 1, 0, 0$$$. After the second query values will be equal to $$$1, 11, 1, 0, 0$$$. After the third query values will be equal to $$$1, 11, 1, 100, 0$$$. | 1,900 | false | false | false | false | true | false | false | false | false | false | 5,340 |
1327D | You are given a colored permutation $$$p_1, p_2, dots, p_n$$$. The $$$i$$$-th element of the permutation has color $$$c_i$$$. Let's define an infinite path as infinite sequence $$$i, p[i], p[p[i]], p[p[p[i]]] dots$$$ where all elements have same color ($$$c[i] = c[p[i]] = c[p[p[i]]] = dots$$$). We can also define a multiplication of permutations $$$a$$$ and $$$b$$$ as permutation $$$c = a imes b$$$ where $$$c[i] = b[a[i]]$$$. Moreover, we can define a power $$$k$$$ of permutation $$$p$$$ as $$$p^k=underbrace{p imes p imes dots imes p}_{k ext{ times}}$$$. Find the minimum $$$k > 0$$$ such that $$$p^k$$$ has at least one infinite path (i.e. there is a position $$$i$$$ in $$$p^k$$$ such that the sequence starting from $$$i$$$ is an infinite path). It can be proved that the answer always exists. Input The first line contains single integer $$$T$$$ ($$$1 le T le 10^4$$$) β the number of test cases. Next $$$3T$$$ lines contain test cases β one per three lines. The first line contains single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the size of the permutation. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$, $$$p_i eq p_j$$$ for $$$i eq j$$$) β the permutation $$$p$$$. The third line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le n$$$) β the colors of elements of the permutation. It is guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 cdot 10^5$$$. Output Print $$$T$$$ integers β one per test case. For each test case print minimum $$$k > 0$$$ such that $$$p^k$$$ has at least one infinite path. Example Input 3 4 1 3 4 2 1 2 2 3 5 2 3 4 5 1 1 2 3 4 5 8 7 4 5 6 1 8 3 2 5 3 6 4 7 5 8 4 Note In the first test case, $$$p^1 = p = [1, 3, 4, 2]$$$ and the sequence starting from $$$1$$$: $$$1, p[1] = 1, dots$$$ is an infinite path. In the second test case, $$$p^5 = [1, 2, 3, 4, 5]$$$ and it obviously contains several infinite paths. In the third test case, $$$p^2 = [3, 6, 1, 8, 7, 2, 5, 4]$$$ and the sequence starting from $$$4$$$: $$$4, p^2[4]=8, p^2[8]=4, dots$$$ is an infinite path since $$$c_4 = c_8 = 4$$$. | 2,200 | true | false | false | false | false | false | true | false | false | true | 4,075 |
2022E1 | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 978 (Div. 2) 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 2-sat binary search combinatorics constructive algorithms dfs and similar dsu graphs *2500 No tag edit access β Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST E1. Billetes MX (Easy Version) time limit per test2 seconds memory limit per test512 megabytes This is the easy version of the problem. In this version, it is guaranteed that $$$q = 0$$$. You can make hacks only if both versions of the problem are solved. An integer grid $$$A$$$ with $$$p$$$ rows and $$$q$$$ columns is called beautiful if: All elements of the grid are integers between $$$0$$$ and $$$2^{30}-1$$$, and For any subgrid, the XOR of the values at the corners is equal to $$$0$$$. Formally, for any four integers $$$i_1$$$, $$$i_2$$$, $$$j_1$$$, $$$j_2$$$ ($$$1 le i_1 < i_2 le p$$$; $$$1 le j_1 < j_2 le q$$$), $$$A_{i_1, j_1} oplus A_{i_1, j_2} oplus A_{i_2, j_1} oplus A_{i_2, j_2} = 0$$$, where $$$oplus$$$ denotes the bitwise XOR operation. There is a partially filled integer grid $$$G$$$ with $$$n$$$ rows and $$$m$$$ columns where only $$$k$$$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful. However, Monocarp thinks that this problem is too easy. Therefore, he will perform $$$q$$$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates. For each of the $$$q + 1$$$ states of the grid, the initial state and after each of the $$$q$$$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $$$10^9+7$$$. Input The first line contains $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$k$$$ and $$$q$$$ ($$$2 le n, m le 10^5$$$; $$$0 le k le 10^5$$$; $$$q = 0$$$)xa0β the number of rows, the number of columns, the number of fixed cells, and the number of updates. The following $$$k$$$ lines contain three integers $$$r$$$, $$$c$$$ and $$$v$$$ ($$$1 le r le n, 1 le c le m$$$; $$$0 le v < 2^{30}$$$)xa0β indicating that $$$G_{r, c}$$$ is assigned the integer $$$v$$$. The following $$$q$$$ lines contain three integers $$$r$$$, $$$c$$$ and $$$v$$$ ($$$1 le r le n, 1 le c le m$$$; $$$0 le v < 2^{30}$$$)xa0β indicating that $$$G_{r, c}$$$ is assigned the integer $$$v$$$. It is guaranteed that the pairs $$$(r,c)$$$ over all assignments are distinct. It is guaranteed that the sum of $$$n$$$, $$$m$$$, $$$k$$$ and $$$q$$$ over all test cases does not exceed $$$10^5$$$ respectively. Output For each test case, output $$$q + 1$$$ lines. The $$$i$$$-th line of output should contain the answer of the $$$i$$$-th state of the grid modulo $$$10^9 + 7$$$. Example input 2 3 3 8 0 2 1 6 3 2 12 1 2 6 2 2 0 1 3 10 1 1 0 2 3 12 3 1 10 2 5 2 0 1 1 10 1 2 30 output 1 489373567 Note In the first test case of the example, we have the following grid: $$$0$$$ $$$6$$$ $$$10$$$ $$$6$$$ $$$0$$$ $$$12$$$ $$$10$$$ $$$12$$$ $$$?$$$ It can be proven that the only valid value for tile $$$(3, 3)$$$ is $$$0$$$. Codeforces (c) | 2,500 | false | false | false | false | false | true | false | true | false | true | 133 |
446A | Problem - 446A - 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 implementation two pointers *1600 No tag edit access β Contest materials ") Editorial") a subsegment of the sequence _a_. The value (_j_u2009-u2009_i_u2009+u20091) denotes the length of the subsegment. Your task is to find the longest subsegment of _a_, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing. You only need to output the length of the subsegment you find. Input The first line contains integer _n_xa0(1u2009β€u2009_n_u2009β€u2009105). The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_xa0(1u2009β€u2009_a__i_u2009β€u2009109). Output In a single line print the answer to the problem β the maximum length of the required subsegment. Examples Input 6 7 2 3 1 5 6 Output 5 Note You can choose subsegment _a_2,u2009_a_3,u2009_a_4,u2009_a_5,u2009_a_6 and change its 3rd element (that is _a_4) to 4. | 1,600 | false | false | true | true | false | false | false | false | false | false | 8,056 |
785C | Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale: "Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..." More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of _n_ grains was full. Then, every day (starting with the first day) the following happens: _m_ grains are brought to the barn. If _m_ grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account). Sparrows come and eat grain. In the _i_-th day _i_ sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing. Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day! Input The only line of the input contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u20091018)xa0β the capacity of the barn and the number of grains that are brought every day. Output Output one integerxa0β the number of the day when the barn will become empty for the first time. Days are numbered starting with one. Note In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens: At the beginning of the first day grain is brought to the barn. It's full, so nothing happens. At the end of the first day one sparrow comes and eats one grain, so 5u2009-u20091u2009=u20094 grains remain. At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it. At the end of the second day two sparrows come. 5u2009-u20092u2009=u20093 grains remain. At the beginning of the third day two grains are brought. The barn becomes full again. At the end of the third day three sparrows come and eat grain. 5u2009-u20093u2009=u20092 grains remain. At the beginning of the fourth day grain is brought again. 2u2009+u20092u2009=u20094 grains remain. At the end of the fourth day four sparrows come and eat grain. 4u2009-u20094u2009=u20090 grains remain. The barn is empty. So the answer is 4, because by the end of the fourth day the barn becomes empty. | 1,600 | true | false | false | false | false | false | false | true | false | false | 6,631 |
1638A | You are given a permutation $$$p_1, p_2, ldots, p_n$$$ of length $$$n$$$. You have to choose two integers $$$l,r$$$ ($$$1 le l le r le n$$$) and reverse the subsegment $$$[l,r]$$$ of the permutation. The permutation will become $$$p_1,p_2, dots, p_{l-1},p_r,p_{r-1}, dots, p_l,p_{r+1},p_{r+2}, dots ,p_n$$$. Find the lexicographically smallest permutation that can be obtained by performing exactly one reverse operation on the initial permutation. Note that for two distinct permutations of equal length $$$a$$$ and $$$b$$$, $$$a$$$ is lexicographically smaller than $$$b$$$ if at the first position they differ, $$$a$$$ has the smaller element. A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$)xa0β the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 500$$$)xa0β the length of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$)xa0β the elements of the permutation. Output For each test case print the lexicographically smallest permutation you can obtain. Example Input 4 1 1 3 2 1 3 4 1 4 2 3 5 1 2 3 4 5 Output 1 1 2 3 1 2 4 3 1 2 3 4 5 Note In the first test case, the permutation has length $$$1$$$, so the only possible segment is $$$[1,1]$$$. The resulting permutation is $$$[1]$$$. In the second test case, we can obtain the identity permutation by reversing the segment $$$[1,2]$$$. The resulting permutation is $$$[1,2,3]$$$. In the third test case, the best possible segment is $$$[2,3]$$$. The resulting permutation is $$$[1,2,4,3]$$$. In the fourth test case, there is no lexicographically smaller permutation, so we can leave it unchanged by choosing the segment $$$[1,1]$$$. The resulting permutation is $$$[1,2,3,4,5]$$$. | 800 | true | true | false | false | false | true | false | false | false | false | 2,426 |
1152B | Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat. Assume that we have a cat with a number $$$x$$$. A perfect longcat is a cat with a number equal $$$2^m - 1$$$ for some non-negative integer $$$m$$$. For example, the numbers $$$0$$$, $$$1$$$, $$$3$$$, $$$7$$$, $$$15$$$ and so on are suitable for the perfect longcats. In the Cat Furrier Transform, the following operations can be performed on $$$x$$$: (Operation A): you select any non-negative integer $$$n$$$ and replace $$$x$$$ with $$$x oplus (2^n - 1)$$$, with $$$oplus$$$ being a : replace $$$x$$$ with $$$x + 1$$$. The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B. Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most $$$40$$$ operations. Can you help Neko writing a transformation plan? Note that it is not required to minimize the number of operations. You just need to use no more than $$$40$$$ operations. Output The first line should contain a single integer $$$t$$$ ($$$0 le t le 40$$$)xa0β the number of operations to apply. Then for each odd-numbered operation print the corresponding number $$$n_i$$$ in it. That is, print $$$lceil frac{t}{2} ceil$$$ integers $$$n_i$$$ ($$$0 le n_i le 30$$$), denoting the replacement $$$x$$$ with $$$x oplus (2^{n_i} - 1)$$$ in the corresponding step. If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem. Note In the first test, one of the transforms might be as follows: $$$39 o 56 o 57 o 62 o 63$$$. Or more precisely: 1. Pick $$$n = 5$$$. $$$x$$$ is transformed into $$$39 oplus 31$$$, or $$$56$$$. 2. Increase $$$x$$$ by $$$1$$$, changing its value to $$$57$$$. 3. Pick $$$n = 3$$$. $$$x$$$ is transformed into $$$57 oplus 7$$$, or $$$62$$$. 4. Increase $$$x$$$ by $$$1$$$, changing its value to $$$63 = 2^6 - 1$$$. In the second and third test, the number already satisfies the goal requirement. | 1,300 | true | false | false | false | false | true | false | false | false | false | 4,955 |
2039G | Shohag has a tree with $$$n$$$ nodes. Pebae has an integer $$$m$$$. She wants to assign each node a valuexa0β an integer from $$$1$$$ to $$$m$$$. So she asks Shohag to count the number, modulo $$$998,244,353$$$, of assignments such that following conditions are satisfied: For each pair $$$1 le u lt v le n$$$, the ]( of the values of all nodes from $$$1$$$ to $$$n$$$ is $$$1$$$. But this problem is too hard for Shohag to solve. As Shohag loves Pebae, he has to solve the problem. Please save Shohag! Input The first line contains two space-separated integers $$$n$$$ and $$$m$$$ ($$$2 le n le 10^6$$$, $$$1 le m le 10^{9}$$$). Each of the next $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$) indicating there is an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree. Output Print a single integerxa0β the number of valid ways to assign each vertex a value, modulo $$$998,244,353$$$. Note In the first test case, the valid assignments are $$$[1, 1, 1, 1, 1, 1]$$$ and $$$[1, 1, 1, 1, 1, 5]$$$. In the second test case, the valid assignments are $$$[1, 1]$$$, $$$[1, 3]$$$, $$$[1, 5]$$$, $$$[3, 1]$$$, $$$[3, 5]$$$, $$$[5, 1]$$$ and $$$[5, 3]$$$. | 3,500 | true | false | false | false | false | false | false | false | false | false | 16 |
171F | Problem - 171F - Codeforces =============== xa0 β jxu edu-rqiut ydtun ev jxu ucyhf je vydt. Output ekjfkj q iydwbu dkcruh. Examples Input 1 Output 13 | 1,600 | false | false | true | false | false | false | true | false | false | false | 9,181 |
1179A | Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with $$$n$$$ elements. The $$$i$$$-th element is $$$a_i$$$ ($$$i$$$ = $$$1, 2, ldots, n$$$). He gradually takes the first two leftmost elements from the deque (let's call them $$$A$$$ and $$$B$$$, respectively), and then does the following: if $$$A > B$$$, he writes $$$A$$$ to the beginning and writes $$$B$$$ to the end of the deque, otherwise, he writes to the beginning $$$B$$$, and $$$A$$$ writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was $$$[2, 3, 4, 5, 1]$$$, on the operation he will write $$$B=3$$$ to the beginning and $$$A=2$$$ to the end, so he will get $$$[3, 4, 5, 1, 2]$$$. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him $$$q$$$ queries. Each query consists of the singular number $$$m_j$$$ $$$(j = 1, 2, ldots, q)$$$. It is required for each query to answer which two elements he will pull out on the $$$m_j$$$-th operation. Note that the queries are independent and for each query the numbers $$$A$$$ and $$$B$$$ should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 leq n leq 10^5$$$, $$$0 leq q leq 3 cdot 10^5$$$)xa0β the number of elements in the deque and the number of queries. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$, where $$$a_i$$$ $$$(0 leq a_i leq 10^9)$$$xa0β the deque element in $$$i$$$-th position. The next $$$q$$$ lines contain one number each, meaning $$$m_j$$$ ($$$1 leq m_j leq 10^{18}$$$). | 1,500 | false | false | true | false | true | false | false | false | false | false | 4,821 |
822B | Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you? Leha has two strings _s_ and _t_. The hacker wants to change the string _s_ at such way, that it can be found in _t_ as a substring. All the changes should be the following: Leha chooses one position in the string _s_ and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string _s_="ab?b" as a result, it will appear in _t_="aabrbb" as a substring. Guaranteed that the length of the string _s_ doesn't exceed the length of the string _t_. Help the hacker to replace in _s_ as few symbols as possible so that the result of the replacements can be found in _t_ as a substring. The symbol "?" should be considered equal to any other symbol. Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u2009_m_u2009β€u20091000) β the length of the string _s_ and the length of the string _t_ correspondingly. The second line contains _n_ lowercase English letters β string _s_. The third line contains _m_ lowercase English letters β string _t_. Output In the first line print single integer _k_ β the minimal number of symbols that need to be replaced. In the second line print _k_ distinct integers denoting the positions of symbols in the string _s_ which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. | 1,000 | false | false | true | false | false | false | true | false | false | false | 6,467 |
1776I | The two siblings Alberto and Beatrice have to eat a spinach pizza together. However, none of them likes spinach, so they both want to eat as little as possible. The pizza has the shape of a strictly convex polygon with $$$n$$$ vertices located at integer coordinates $$$(x_1, y_1), , (x_2, y_2), , dots, , (x_n, y_n)$$$ of the plane. The siblings have decided to eat the pizza in the following way: taking turns, starting with Alberto, each sibling chooses a vertex of the remaining part of the pizza and eats out the triangle determined by its two neighboring edges. In this way, after each of the first $$$n - 3$$$ turns the pizza will have one less vertex. The game ends after the $$$(n - 2)$$$-th turn, when all the pizza has been eaten. Assuming that Alberto and Beatrice choose the slices to eat optimally, which of the siblings manages to eat at most half of the pizza? You should identify a sibling that has a strategy to do so and help them choose the slices appropriately. Note that it is possible that both Alberto and Beatrice end up eating exactly half of the area if they choose their slices optimally. Input The first line contains a single integer $$$n$$$ ($$$4 le n le 100$$$) β the number of vertices. The next $$$n$$$ lines contain two integers $$$x_i$$$ and $$$y_i$$$ each ($$$-10^6 le x_i, y_i le 10^6$$$) β the coordinates of the $$$i$$$-th vertex of the polygon representing the initial shape of the pizza. It is guaranteed that the polygon is strictly convex and that its vertices are given in counterclockwise order. Interaction First, you should print a line containing either the string $$$ exttt{Alberto}$$$ or the string $$$ exttt{Beatrice}$$$ β the sibling that you will help to win. Then, for the next $$$n - 2$$$ turns, you will alternate with the judge in choosing a slice of the pizza and removing it, starting with you if you chose to help Alberto, or starting with the judge if you chose to help Beatrice. When it is your turn, print a single line containing an integer $$$p$$$ ($$$1 leq p leq n$$$) that has not been chosen before, indicating that you want to eat the slice determined by the vertex located at $$$(x_p, y_p)$$$. When it is the judge's turn, read an integer $$$q$$$ ($$$1 leq q leq n$$$), indicating that the other player eats the slice determined by the vertex located at $$$(x_q, y_q)$$$. It is guaranteed that $$$q$$$ has not been chosen before. If one of your interactions is malformed, the interactor terminates immediately and your program receives the verdict $$$ exttt{WRONG-ANSWER}$$$. Otherwise, you will receive $$$ exttt{CORRECT}$$$ if at the end your player has eaten at most half of the pizza, and $$$ exttt{WRONG-ANSWER}$$$ otherwise. After printing a line do not forget to end the line and flush the output. Otherwise, you will get the verdict $$$ exttt{TIMELIMIT}$$$. To flush the output, use: $$$ exttt{fflush(stdout)}$$$ in C; $$$ exttt{fflush(stdout)}$$$, $$$ exttt{cout <} exttt{< flush}$$$ or $$$ exttt{cout.flush()}$$$ in C++; $$$ exttt{System.out.flush()}$$$ in Java and Kotlin; $$$ exttt{sys.stdout.flush()}$$$ in Python. Examples Input 6 0 0 2 0 3 2 2 4 0 4 -1 2 Input 7 0 0 2 0 5 2 4 5 1 5 -1 4 -1 2 Note In the first sample, the pizza has area $$$15$$$. Alberto can eat less than half of the pizza by eating the slice around vertex $$$2$$$ (which has area $$$6.5$$$) or around vertex $$$3$$$ (which has area $$$3.5$$$). In the second sample, it can be proved that both players will eat exactly half of the pizza if they eat optimally. Therefore it is possible to choose to help either Alberto or Beatrice. In the third sample, it is possible to show that only Beatrice has a strategy to eat at most half of the pizza. The following is an example of a valid interaction (after reading the input): $$$$$$ begin{array}{ccc} hline extbf{Contestant} & extbf{Judge} & extbf{Explanation} hline exttt{Beatrice} & & ext{The contestant will help Beatrice} hline & exttt{7} & ext{Alberto eats the triangle with vertices $6$, $7$, $1$ and area $1$} hline exttt{2} & & ext{Beatrice eats the triangle with vertices $1$, $2$, $3$ and area $2$} hline & exttt{5} & ext{Alberto eats the triangle with vertices $4$, $5$, $6$ and area $1.5$} hline exttt{4} & & ext{Beatrice eats the triangle with vertices $3$, $4$, $6$ and area $8$} hline & exttt{6} & ext{Alberto eats the triangle with vertices $3$, $6$, $1$ and area $11$} hline end{array} $$$$$$ The total area eaten by Alberto is $$$13.5$$$ and the total area eaten by Beatrice is $$$10$$$, which is less than half the area of the whole pizza. The actions performed by the contestant and the judge in this example of interaction may be non-optimal. The process is illustrated below: | 2,500 | false | true | false | false | false | false | false | false | false | false | 1,620 |
1621H | Railway network of one city consists of $$$n$$$ stations connected by $$$n-1$$$ roads. These stations and roads forms a tree. Station $$$1$$$ is a city center. For each road you know the time trains spend to pass this road. You can assume that trains don't spend time on stops. Let's define $$$dist(v)$$$ as the time that trains spend to get from the station $$$v$$$ to the station $$$1$$$. This railway network is splitted into zones named by first $$$k$$$ capital latin letters. The zone of the $$$i$$$-th station is $$$z_i$$$. City center is in the zone A. For all other stations it is guaranteed that the first station on the road from this station to the city center is either in the same zone or in the zone with lexicographically smaller name. Any road is completely owned by the zone of the most distant end from the city center. Tourist will arrive at the airport soon and then he will go to the city center. Here's how the trip from station $$$v$$$ to station $$$1$$$ happends: At the moment $$$0$$$, tourist enters the train that follows directly from the station $$$v$$$ to the station $$$1$$$. The trip will last for $$$dist(v)$$$ minutes. Tourist can buy tickets for any subset of zones at any moment. Ticket for zone $$$i$$$ costs $$$pass_i$$$ euro. Every $$$T$$$ minutes since the start of the trip (that is, at the moments $$$T, 2T, ldots$$$) the control system will scan tourist. If at the moment of scan tourist is in the zone $$$i$$$ without zone $$$i$$$ ticket, he should pay $$$fine_i$$$ euro. Formally, the zone of tourist is determined in the following way: If tourist is at the station $$$1$$$, then he already at the city center so he shouldn't pay fine. If tourist is at the station $$$u eq 1$$$, then he is in the zone $$$z_u$$$. If tourist is moving from the station $$$x$$$ to the station $$$y$$$ that are directly connected by road, then he is in the zone $$$z_x$$$.Note, that tourist can pay fine multiple times in the same zone. Tourist always selects such way to buy tickets and pay fines that minimizes the total cost of trip. Let $$$f(v)$$$ be such cost for station $$$v$$$. Unfortunately, tourist doesn't know the current values of $$$pass_i$$$ and $$$fine_i$$$ for different zones and he has forgot the location of the airport. He will ask you queries of $$$3$$$ types: $$$1$$$ $$$i$$$ $$$c$$$xa0β the cost of ticket in zone $$$i$$$ has changed. Now $$$pass_i$$$ is $$$c$$$. $$$2$$$ $$$i$$$ $$$c$$$xa0β the cost of fine in zone $$$i$$$ has changed. Now $$$fine_i$$$ is $$$c$$$. $$$3$$$ $$$u$$$xa0β solve the following problem for current values of $$$pass$$$ and $$$fine$$$: You are given the station $$$u$$$. Consider all the stations $$$v$$$ that satisfy the following conditions: $$$z_v = z_u$$$ The station $$$u$$$ is on the path from the station $$$v$$$ to the station $$$1$$$. Find the value of $$$min(f(v))$$$ over all such stations $$$v$$$ with the following assumption: tourist has the ticket for the zone of station $$$z_u$$$. Input The first line contains the single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$)xa0β the number of stations. Each of the next $$$n - 1$$$ lines contains three integers $$$v_i$$$, $$$u_i$$$, $$$t_i$$$ ($$$1 leq v_i, u_i leq n, 1 leq t_i leq 10^9$$$)xa0β the ends of the $$$i$$$-th road and the time it takes a train to pass this road. It is guaranteed that this roads forms a tree. The next line contains the single integer $$$k$$$ ($$$1 leq k leq 26$$$)xa0β the number of zones. The next line contains $$$n$$$ symbols $$$z_1z_2 ldots z_n$$$xa0β $$$z_i$$$ is the name of the zone of the $$$i$$$-th station. It is guaranteed that the conditions from the second paragraph are satisfied. The next line contains $$$k$$$ integers $$$pass_1$$$, $$$pass_2$$$, $$$ldots$$$, $$$pass_k$$$ ($$$1 leq pass_i leq 10^9$$$)xa0β initial costs of tickets. The next line contains $$$k$$$ integers $$$fine_1$$$, $$$fine_2$$$, $$$ldots$$$, $$$fine_k$$$ ($$$1 leq fine_i leq 10^9$$$)xa0β initial fines. The next line contains the single integer $$$T$$$ ($$$1 leq T leq 10^9$$$)xa0β the time gap between scans of control system. The next line contains the single integer $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$)xa0β the number of queries. Next $$$q$$$ lines contains queries as described in the statement. It is guaranteed that in the queries of the first and the second type $$$i$$$ is a correct name of the zone (one of the first $$$k$$$ capital latin letters) and $$$1 leq c leq 10^9$$$, and in the queries of the third type $$$1 leq u leq n$$$. Output For each query of the third type print the answer to it. Example Input 8 1 2 7 2 3 4 2 4 3 4 5 1 5 6 6 4 7 10 6 8 6 4 AABABBDB 11 12 10 42 16 15 15 30 4 6 3 2 1 A 10 3 3 2 A 3 3 7 3 6 Note Note, that the fine can be cheaper than the pass. The railway network from the example. Green color is used for stations and roads of zone A, blue color is used for zone B and red color is used for zone D. The integer near each road is time that trains spend to pass it. In the first query, the airport can be located near the station $$$2$$$ or near the station $$$4$$$. During the trip, tourist will always stay in the zone A. He already has the pass for this zone so the answer is $$$0$$$. After the second query, the cost of the pass in the zone A has become $$$10$$$. In the third query, the airport can be located only near the station $$$3$$$. Optimal solution will be to buy the pass for zone A. During the first $$$3$$$ seconds of trip tourist will be in the zone B. Then he will move to the zone A and will be scanned there on the $$$4$$$-th and the $$$8$$$-th second of his ride. Since he have a pass for this zone, he won't pay fines. After the forth query, the fine in the zone A has become $$$3$$$. In the fifth query, the airport can be located only near the station $$$7$$$ and $$$f(7) = 6$$$. In the sixth query, the airport can be located near the station $$$6$$$ or near the station $$$8$$$. Since $$$f(6)=9$$$ and $$$f(8)=6$$$ the answer is $$$6$$$. | 3,500 | false | false | false | false | false | false | false | false | false | true | 2,513 |
1141F1 | This problem is given in two editions, which differ exclusively in the constraints on the number $$$n$$$. You are given an array of integers $$$a[1], a[2], dots, a[n].$$$ A block is a sequence of contiguous (consecutive) elements $$$a[l], a[l+1], dots, a[r]$$$ ($$$1 le l le r le n$$$). Thus, a block is defined by a pair of indices $$$(l, r)$$$. Find a set of blocks $$$(l_1, r_1), (l_2, r_2), dots, (l_k, r_k)$$$ such that: They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks $$$(l_i, r_i)$$$ and $$$(l_j, r_j$$$) where $$$i eq j$$$ either $$$r_i < l_j$$$ or $$$r_j < l_i$$$. For each block the sum of its elements is the same. Formally, $$$$$$a[l_1]+a[l_1+1]+dots+a[r_1]=a[l_2]+a[l_2+1]+dots+a[r_2]=$$$$$$ $$$$$$dots =$$$$$$ $$$$$$a[l_k]+a[l_k+1]+dots+a[r_k].$$$$$$ The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks $$$(l_1', r_1'), (l_2', r_2'), dots, (l_{k'}', r_{k'}')$$$ satisfying the above two requirements with $$$k' > k$$$. The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer $$$n$$$ ($$$1 le n le 50$$$) β the length of the given array. The second line contains the sequence of elements $$$a[1], a[2], dots, a[n]$$$ ($$$-10^5 le a_i le 10^5$$$). Output In the first line print the integer $$$k$$$ ($$$1 le k le n$$$). The following $$$k$$$ lines should contain blocks, one per line. In each line print a pair of indices $$$l_i, r_i$$$ ($$$1 le l_i le r_i le n$$$) β the bounds of the $$$i$$$-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 | 1,900 | false | true | false | false | false | false | false | false | false | false | 5,016 |
1486A | You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing? Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks. Input First line contains a single integer $$$t$$$ $$$(1 leq t leq 10^4)$$$ β the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 leq n leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 leq h_i leq 10^9)$$$ β starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$. Output For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). Example Input 6 2 1 2 2 1 0 3 4 4 4 2 0 0 3 0 1 0 4 1000000000 1000000000 1000000000 1000000000 Output YES YES YES NO NO YES Note In the first test case there is no need to make any moves, the sequence of heights is already increasing. In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$. In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$. In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO. In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO. | 900 | false | true | true | false | false | false | false | false | false | false | 3,253 |
1280F | You are an intergalactic surgeon and you have an alien patient. For the purposes of this problem, we can and we will model this patient's body using a $$$2 imes (2k + 1)$$$ rectangular grid. The alien has $$$4k + 1$$$ distinct organs, numbered $$$1$$$ to $$$4k + 1$$$. In healthy such aliens, the organs are arranged in a particular way. For example, here is how the organs of a healthy such alien would be positioned, when viewed from the top, for $$$k = 4$$$: Here, the E represents empty space. In general, the first row contains organs $$$1$$$ to $$$2k + 1$$$ (in that order from left to right), and the second row contains organs $$$2k + 2$$$ to $$$4k + 1$$$ (in that order from left to right) and then empty space right after. Your patient's organs are complete, and inside their body, but they somehow got shuffled around! Your job, as an intergalactic surgeon, is to put everything back in its correct position. All organs of the alien must be in its body during the entire procedure. This means that at any point during the procedure, there is exactly one cell (in the grid) that is empty. In addition, you can only move organs around by doing one of the following things: You can switch the positions of the empty space E with any organ to its immediate left or to its immediate right (if they exist). In reality, you do this by sliding the organ in question to the empty space; You can switch the positions of the empty space E with any organ to its immediate top or its immediate bottom (if they exist) only if the empty space is on the leftmost column, rightmost column or in the centermost column. Again, you do this by sliding the organ in question to the empty space. Your job is to figure out a sequence of moves you must do during the surgical procedure in order to place back all $$$4k + 1$$$ internal organs of your patient in the correct cells. If it is impossible to do so, you must say so. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 4$$$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of three lines. The first line contains a single integer $$$k$$$ ($$$1 le k le 15$$$) which determines the size of the grid. Then two lines follow. Each of them contains $$$2k + 1$$$ space-separated integers or the letter E. They describe the first and second rows of organs, respectively. It is guaranteed that all $$$4k + 1$$$ organs are present and there is exactly one E. Output For each test case, first, print a single line containing either: SURGERY COMPLETE if it is possible to place back all internal organs in the correct locations; SURGERY FAILED if it is impossible. If it is impossible, then this is the only line of output for the test case. However, if it is possible, output a few more lines describing the sequence of moves to place the organs in the correct locations. The sequence of moves will be a (possibly empty) string of letters u, d, l or r, representing sliding the organ that's directly above, below, to the left or to the right of the empty space, respectively, into the empty space. Print the sequence of moves in the following line, as such a string. For convenience, you may use shortcuts to reduce the size of your output. You may use uppercase letters as shortcuts for sequences of moves. For example, you could choose T to represent the string lddrr. These shortcuts may also include other shortcuts on their own! For example, you could choose E to represent TruT, etc. You may use any number of uppercase letters (including none) as shortcuts. The only requirements are the following: The total length of all strings in your output for a single case is at most $$$10^4$$$; There must be no cycles involving the shortcuts that are reachable from the main sequence; The resulting sequence of moves is finite, after expanding all shortcuts. Note that the final sequence of moves (after expanding) may be much longer than $$$10^4$$$; the only requirement is that it's finite. As an example, if T = lddrr, E = TruT and R = rrr, then TurTlER expands to: TurTlER lddrrurTlER lddrrurlddrrlER lddrrurlddrrlTruTR lddrrurlddrrllddrrruTR lddrrurlddrrllddrrrulddrrR lddrrurlddrrllddrrrulddrrrrr To use shortcuts, print each one of them in a single line as the uppercase letter, then space, and then the string that this shortcut represents. They may be printed in any order. At the end of all of those, print a single line containing DONE. Note: You still need to print DONE even if you don't plan on using shortcuts. Your sequence does not need to be the shortest. Any valid sequence of moves (satisfying the requirements above) will be accepted. | 3,400 | true | false | false | false | false | true | false | false | false | false | 4,289 |
557D | After Vitaly was expelled from the university, he became interested in the graph theory. Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once. Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of _n_ vertices and _m_ edges, not necessarily connected, without parallel edges and loops. You need to find _t_ β the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find _w_ β the number of ways to add _t_ edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges. Two ways to add edges to the graph are considered equal if they have the same sets of added edges. Since Vitaly does not study at the university, he asked you to help him with this task. Input The first line of the input contains two integers _n_ and _m_ (xa0βxa0the number of vertices in the graph and the number of edges in the graph. Next _m_ lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers _a__i_, _b__i_ (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_)xa0βxa0the vertices that are connected by the _i_-th edge. All numbers in the lines are separated by a single space. It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected. Output Print in the first line of the output two space-separated integers _t_ and _w_xa0βxa0the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this. Note The simple cycle is a cycle that doesn't contain any vertex twice. | 2,000 | true | false | false | false | false | false | false | false | false | true | 7,605 |
1625E2 | This is the hard version of the problem. The only difference between the easy and the hard versions are removal queries, they are present only in the hard version. "Interplanetary Software, Inc." together with "Robots of Cydonia, Ltd." has developed and released robot cats. These electronic pets can meow, catch mice and entertain the owner in various ways. The developers from "Interplanetary Software, Inc." have recently decided to release a software update for these robots. After the update, the cats must solve the problems about bracket sequences. One of the problems is described below. First, we need to learn a bit of bracket sequence theory. Consider the strings that contain characters "(", ")" and ".". Call a string regular bracket sequence (RBS), if it can be transformed to an empty string by one or more operations of removing either single "." characters, or a continuous substring "()". For instance, the string "(()(.))" is an RBS, as it can be transformed to an empty string with the following sequence of removals: "(()(.))" $$$ ightarrow$$$ "(()())" $$$ ightarrow$$$ "(())" $$$ ightarrow$$$ "()" $$$ ightarrow$$$ "". We got an empty string, so the initial string was an RBS. At the same time, the string ")(" is not an RBS, as it is not possible to apply such removal operations to it. An RBS is simple if this RBS is not empty, doesn't start with ".", and doesn't end with ".". Denote the substring of the string $$$s$$$ as its sequential subsegment. In particular, $$$s[ldots r] = s_ls_{l+1}dots s_r$$$, where $$$s_i$$$ is the $$$i$$$-th character of the string $$$s$$$. Now, move on to the problem statement itself. You are given a string $$$s$$$, initially consisting of characters "(" and ")". You need to answer the following queries: 1. Given two indices, $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$). It's guaranteed that the $$$l$$$-th character is equal to "(", the $$$r$$$-th character is equal to ")", and the characters between them are equal to ".". Then the $$$l$$$-th and the $$$r$$$-th characters must be set to ".". 2. Given two indices, $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$), and it's guaranteed that the substring $$$s[ldots r]$$$ is a simple RBS. You need to find the number of substrings in $$$s[ldots r]$$$ such that they are simple RBS. In other words, find the number of index pairs $$$i$$$, $$$j$$$ such that $$$l le i < j le r$$$ and $$$s[idots j]$$$ is a simple RBS. You are an employee in "Interplanetary Software, Inc." and you were given the task to teach the cats to solve the problem above, after the update. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 3cdot10^5$$$, $$$1 le q le 3cdot10^5$$$), the length of the string, and the number of queries. The second line contains the string $$$s$$$, consisting of $$$n$$$ characters "(" and ")". Each of the following $$$q$$$ lines contains three integers $$$t$$$, $$$l$$$ and $$$r$$$ ($$$t in {1, 2}$$$, $$$1 le l < r le n$$$), the queries you need to answer. It is guaranteed that all the queries are valid and correspond to the problem statements. Output For each query, print a single integer in a separate line, the number of substrings that are simple RBS. The answers must be printed in the same order as the queries are specified in the input. Example Input 9 8 )(()())() 2 3 6 2 2 7 1 3 4 2 2 7 2 2 9 1 5 6 1 2 7 2 8 9 Note Consider the example test case. The answer to the first query is $$$3$$$, as there are three suitable substrings: $$$s[3dots6]$$$, $$$s[3dots4]$$$ and $$$s[5dots6]$$$. The answer to the second query is $$$4$$$. The substrings are $$$s[3dots6]$$$, $$$s[3dots4]$$$, $$$s[5dots6]$$$ and $$$s[2dots7]$$$. After the third query, the string becomes ")(..())()". The answer to the fourth query is $$$2$$$. The substrings are $$$s[5dots6]$$$ and $$$s[2dots7]$$$. Note that $$$s[3dots6]$$$ is not a simple RBS anymore, as it starts with ".". The answer to the fifth query is $$$4$$$. The substrings are $$$s[5dots6]$$$, $$$s[2dots7]$$$, $$$s[8dots9]$$$ and $$$s[2dots9]$$$. After the sixth query, the string becomes ")(....)()". After the seventh query, the string becomes ")......()". The answer to the eighth query is $$$1$$$. The substring is $$$s[8dots9]$$$. | 2,800 | false | false | false | false | true | false | false | true | false | true | 2,488 |
1983A | An array of integers $$$a_1,a_2,cdots,a_n$$$ is beautiful subject to an integer $$$k$$$ if it satisfies the following: The sum of $$$a_{j}$$$ over all $$$j$$$ such that $$$j$$$ is a multiple of $$$k$$$ and $$$1 le j le n $$$, itself, is a multiple of $$$k$$$. More formally, if $$$sum_{k j} a_{j}$$$ is divisible by $$$k$$$ for all $$$1 le j le n$$$ then the array $$$a$$$ is beautiful subject to $$$k$$$. Here, the notation $$${kj}$$$ means $$$k$$$ divides $$$j$$$, that is, $$$j$$$ is a multiple of $$$k$$$. Given $$$n$$$, find an array of positive nonzero integers, with each element less than or equal to $$$10^5$$$ that is beautiful subject to all $$$1 le k le n$$$. It can be shown that an answer always exists. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The first and only line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0β the size of the array. Output For each test case, print the required array as described in the problem statement. Example Output 4 22 18 10 6 15 32 125 54 23 18 27 36 5 66 7 Note In the second test case, when $$$n = 6$$$, for all integers $$$k$$$ such that $$$1 le k le 6$$$, let $$$S$$$ be the set of all indices of the array that are divisible by $$$k$$$. When $$$k = 1$$$, $$$S = {1, 2, 3,4,5,6}$$$ meaning $$$a_1+a_2+a_3+a_4+a_5+a_6=242$$$ must be divisible by $$$1$$$. When $$$k = 2$$$, $$$S = {2,4,6}$$$ meaning $$$a_2+a_4+a_6=92$$$ must be divisible by $$$2$$$. When $$$k = 3$$$, $$$S = {3,6}$$$ meaning $$$a_3+a_6=69$$$ must divisible by $$$3$$$. When $$$k = 4$$$, $$$S = {4}$$$ meaning $$$a_4=32$$$ must divisible by $$$4$$$. When $$$k = 5$$$, $$$S = {5}$$$ meaning $$$a_5=125$$$ must divisible by $$$5$$$. When $$$k = 6$$$, $$$S = {6}$$$ meaning $$$a_6=54$$$ must divisible by $$$6$$$. The array $$$a = [10, 6, 15, 32, 125, 54]$$$ satisfies all of the above conditions. Hence, $$$a$$$ is a valid array. | 800 | true | false | false | false | false | true | false | false | false | false | 390 |
286E | A ladies' shop has recently opened in the city of Ultima Thule. To get ready for the opening, the shop bought _n_ bags. Each bag is characterised by the total weight _a__i_ of the items you can put there. The weird thing is, you cannot use these bags to put a set of items with the total weight strictly less than _a__i_. However the weights of the items that will be sold in the shop haven't yet been defined. That's what you should determine right now. Your task is to find the set of the items' weights _p_1,u2009_p_2,u2009...,u2009_p__k_ (1u2009β€u2009_p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__k_), such that: 1. Any bag will be used. That is, for any _i_ (1u2009β€u2009_i_u2009β€u2009_n_) there will be such set of items that their total weight will equal _a__i_. We assume that there is the infinite number of items of any weight. You can put multiple items of the same weight in one bag. 2. For any set of items that have total weight less than or equal to _m_, there is a bag into which you can put this set. Similarly, a set of items can contain multiple items of the same weight. 3. Of all sets of the items' weights that satisfy points 1 and 2, find the set with the minimum number of weights. In other words, value _k_ should be as small as possible. Find and print the required set. Input The first line contains space-separated integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009106). The second line contains _n_ distinct space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a_1u2009<u2009_a_2u2009<u2009...u2009<u2009_a__n_u2009β€u2009_m_) β the bags' weight limits. Output In the first line print "NO" (without the quotes) if there isn't set _p__i_, that would meet the conditions. Otherwise, in the first line print "YES" (without the quotes), in the second line print an integer _k_ (showing how many numbers are in the suitable set with the minimum number of weights), in the third line print _k_ space-separated integers _p_1,u2009_p_2,u2009...,u2009_p__k_ (1u2009β€u2009_p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__k_). If there are multiple solutions, print any of them. | 2,800 | true | false | false | false | false | true | false | false | false | false | 8,687 |
699B | You are given a description of a depot. It is a rectangular checkered field of _n_u2009Γu2009_m_ size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*"). You have one bomb. If you lay the bomb at the cell (_x_,u2009_y_), then after triggering it will wipe out all walls in the row _x_ and all walls in the column _y_. You are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall. Input The first line contains two positive integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u20091000)xa0β the number of rows and columns in the depot field. The next _n_ lines contain _m_ symbols "." and "*" eachxa0β the description of the field. _j_-th symbol in _i_-th of them stands for cell (_i_,u2009_j_). If the symbol is equal to ".", then the corresponding cell is empty, otherwise it equals "*" and the corresponding cell is occupied by a wall. Output If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print "NO" in the first line (without quotes). Otherwise print "YES" (without quotes) in the first line and two integers in the second linexa0β the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them. Examples Input 6 5 ..*.. ..*.. **** ..*.. ..*.. ..*.. | 1,400 | false | false | true | false | false | false | false | false | false | false | 7,026 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.