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
1903C
Theofanis easily gets obsessed with problems before going to sleep and often has nightmares about them. To deal with his obsession he visited his doctor, Dr. Emix. In his latest nightmare, he has an array $$$a$$$ of size $$$n$$$ and wants to divide it into non-empty subarrays$$$^{dagger}$$$ such that every element is in exactly one of the subarrays. For example, the array $$$[1,-3,7,-6,2,5]$$$ can be divided to $$$[1] [-3,7] [-6,2] [5]$$$. The Cypriot value of such division is equal to $$$Sigma_{i=1}^{k} i cdot mathrm{sum}_i$$$ where $$$k$$$ is the number of subarrays that we divided the array into and $$$mathrm{sum}_i$$$ is the sum of the $$$i$$$-th subarray. The Cypriot value of this division of the array $$$[1] [-3,7] [-6,2] [5] = 1 cdot 1 + 2 cdot (-3 + 7) + 3 cdot (-6 + 2) + 4 cdot 5 = 17$$$. Theofanis is wondering what is the maximum Cypriot value of any division of the array. $$$^{dagger}$$$ An array $$$b$$$ is a subarray of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^{5}$$$)xa0— the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^8 le a_i le 10^{8}$$$)xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^{5}$$$. Note In the first test case, to get the maximum Cypriot value we divide the array into $$$[1][-3][7][-6][2][5]$$$ which gives us: $$$Sigma_{i=1}^{k} i cdot mathrm{sum}_i = 1 cdot 1 + 2 cdot (-3) + 3 cdot 7 + 4 cdot (-6) + 5 cdot 2 + 6 cdot 5 = 32$$$ Similarly, in the second test case we divide the array into $$$[2][9,-5,-3]$$$ which gives us $$$Sigma_{i=1}^{k} i cdot mathrm{sum}_i = 1 cdot 2 + 2 cdot (9 + (-5) + (-3)) = 4$$$.
1,400
false
true
false
false
false
true
false
false
false
false
895
1208E
You are given $$$n$$$ arrays that can have different sizes. You also have a table with $$$w$$$ columns and $$$n$$$ rows. The $$$i$$$-th array is placed horizontally in the $$$i$$$-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table. You need to find the maximum sum of the integers in the $$$j$$$-th column for each $$$j$$$ from $$$1$$$ to $$$w$$$ independently. Optimal placements for columns $$$1$$$, $$$2$$$ and $$$3$$$ are shown on the pictures from left to right. Note that you can exclude any array out of a column provided it remains in the window. In this case its value is considered to be zero. Input The first line contains two integers $$$n$$$ ($$$1 le n le 10^{6}$$$) and $$$w$$$ ($$$1 le w le 10^{6}$$$)xa0— the number of arrays and the width of the table. Each of the next $$$n$$$ lines consists of an integer $$$l_{i}$$$ ($$$1 le l_{i} le w$$$), the length of the $$$i$$$-th array, followed by $$$l_{i}$$$ integers $$$a_{i1}, a_{i2}, ldots, a_{il_i}$$$ ($$$-10^{9} le a_{ij} le 10^{9}$$$)xa0— the elements of the array. The total length of the arrays does no exceed $$$10^{6}$$$. Output Print $$$w$$$ integers, the $$$i$$$-th of them should be the maximum sum for column $$$i$$$. Examples Input 3 3 3 2 4 8 2 2 5 2 6 3 Note Illustration for the first example is in the statement.
2,200
false
false
true
false
true
false
false
false
false
false
4,650
476D
Dreamoon likes to play with sets, integers and . is defined as the largest positive integer that divides both _a_ and _b_. Let _S_ be a set of exactly four distinct integers greater than 0. Define _S_ to be of rank _k_ if and only if for all pairs of distinct elements _s__i_, _s__j_ from _S_, . Given _k_ and _n_, Dreamoon wants to make up _n_ sets of rank _k_ using integers from 1 to _m_ such that no integer is used in two different sets (of course you can leave some integers without use). Calculate the minimum _m_ that makes it possible and print one possible solution. Input The single line of the input contains two space separated integers _n_, _k_ (1u2009≤u2009_n_u2009≤u200910u2009000,u20091u2009≤u2009_k_u2009≤u2009100). Output On the first line print a single integer — the minimal possible _m_. On each of the next _n_ lines print four space separated integers representing the _i_-th set. Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal _m_, print any one of them. Examples Output 22 2 4 6 22 14 18 10 16 Note For the first example it's easy to see that set {1,u20092,u20093,u20094} isn't a valid set of rank 1 since .
1,900
true
true
false
false
false
true
false
false
false
false
7,925
1411B
Problem - 1411B - Codeforces =============== xa0 and Technocup 2021 — Elimination Round 3") Editorial") , but $$$282$$$ is not, because it isn't divisible by $$$8$$$. Given a positive integer $$$n$$$. Find the minimum integer $$$x$$$, such that $$$n leq x$$$ and $$$x$$$ is fair. Input The first line contains number of test cases $$$t$$$ ($$$1 leq t leq 10^3$$$). Each of the next $$$t$$$ lines contains an integer $$$n$$$ ($$$1 leq n leq 10^{18}$$$). Output For each of $$$t$$$ test cases print a single integerxa0— the least fair number, which is not less than $$$n$$$. Example Input 4 1 282 1234567890 1000000000000000000 Output 1 288 1234568040 1000000000000000000 Note Explanations for some test cases: In the first test case number $$$1$$$ is fair itself. In the second test case number $$$288$$$ is fair (it's divisible by both $$$2$$$ and $$$8$$$). None of the numbers from $$$
1,000
false
false
false
false
false
false
true
false
false
false
3,620
1799D2
This is a hard 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 10^5$$$). The first line of each test case contains $$$n$$$ and $$$k$$$ ($$$1 le n, k le 3 cdot 10^5$$$). 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 $$$3 cdot 10^5$$$. 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.
2,100
false
false
false
true
true
false
false
false
false
false
1,480
1375C
# Element Extermination Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes You are given an array a of length n, which initially is a permutation of numbers from 1 to n. In one operation, you can choose an index i (1 ≤ i < n ) such that ai < a i+1 , and remove either ai or ai+1 from the array (after the removal, the remaining parts are concatenated). For example, if you have the array [1 , 3, 2] , you can choose i = 1 (since a1 = 1 < a 2 = 3 ), then either remove a1 which gives the new array [3 , 2] , or remove a2 which gives the new array [1 , 2] .Is it possible to make the length of this array equal to 1 with these operations? # Input The first line contains a single integer t (1 ≤ t ≤ 2 · 10 4) x16 the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (2 ≤ n ≤ 3 · 10 5) x16 the length of the array. The second line of each test case contains n integers a1, a2, ..., an (1 ≤ ai ≤ n, ai are pairwise distinct ) x16 elements of the array. It is guaranteed that the sum of n over all test cases doesn’t exceed 3 · 10 5. # Output For each test case, output on a single line the word “ YES ” if it is possible to reduce the array to a single element using the aforementioned operation, or “ NO ” if it is impossible to do so. # Example standard input standard output 431 2 3 43 1 2 4 32 3 1 62 4 6 1 3 5 YES YES NO YES # Note For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation): [1, 2, 3] → [1, 2] → [1][3, 1, 2, 4] → [3, 1, 4] → [3, 4] → [4][2, 4, 6, 1, 3, 5] → [4, 6, 1, 3, 5] → [4, 1, 3, 5] → [4, 1, 5] → [4, 5] → [4] Page 1 of 1
1,400
false
true
false
false
true
true
false
false
false
false
3,785
440A
Problem - 440A - Codeforces =============== xa0 xa0— the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to _n_. The second line contains _n_u2009-u20091 integer _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_)xa0— the numbers of episodes that Polycarpus has watched. All values of _a__i_ are distinct. Output Print the number of the episode that Polycarpus hasn't watched. Examples Input 10 3 8 10 1 7 9 6 5 2 Output 4
800
false
false
true
false
false
false
false
false
false
false
8,079
1891C
A boy called Smilo is playing a new game! In the game, there are $$$n$$$ hordes of monsters, and the $$$i$$$-th horde contains $$$a_i$$$ monsters. The goal of the game is to destroy all the monsters. To do this, you have two types of attacks and a combo counter $$$x$$$, initially set to $$$0$$$: The first type: you choose a number $$$i$$$ from $$$1$$$ to $$$n$$$, such that there is at least one monster left in the horde with the number $$$i$$$. Then, you kill one monster from horde number $$$i$$$, and the combo counter $$$x$$$ increases by $$$1$$$. The second type: you choose a number $$$i$$$ from $$$1$$$ to $$$n$$$, such that there are at least $$$x$$$ monsters left in the horde with number $$$i$$$. Then, you use an ultimate attack and kill $$$x$$$ monsters from the horde with number $$$i$$$. After that, $$$x$$$ is reset to zero. Your task is to destroy all of the monsters, meaning that there should be no monsters left in any of the hordes. Smilo wants to win as quickly as possible, so he wants to the minimum number of attacks required to win the game. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each input data set contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$) — the number of hordes of monsters. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) — the number of monsters in each horde. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 cdot {10^5}$$$. Output For each test case, out put the minimum number of attacks required to kill all monsters. Example Input 4 4 1 3 1 1 4 1 2 1 1 6 3 2 1 5 2 4 2 1 6 Note In the first test case, we can use an attack of the first type on the $$$1$$$-st, $$$3$$$-rd and $$$4$$$-th hordes, and then use an attack of the second type to finish off the $$$2$$$-nd horde. We need $$$4$$$ attacks in total. In the second test case, we can use an attack of the first type on the $$$1$$$-st and $$$3$$$-rd hordes, then use an attack of the second type to finish off the $$$2$$$-nd horde, and then use an attack of the first type on the $$$4$$$-th horde. We need $$$4$$$ attacks in total. In the fourth test case, we can use an attack of the first type once on the $$$1$$$-st horde, twice on the $$$2$$$-nd horde, and then use an attack of the second type on the $$$2$$$-nd horde, and finally use an attack of the first type to finish off the $$$2$$$-nd horde. We need $$$5$$$ attacks in total.
1,500
false
true
false
false
false
true
false
true
true
false
955
954G
Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into _n_ sections. At this moment there are exactly _a__i_ archers located at the _i_-th section of this wall. You know that archer who stands at section _i_ can shoot orcs that attack section located at distance not exceeding _r_, that is all such sections _j_ that _i_u2009-u2009_j_u2009≤u2009_r_. In particular, _r_u2009=u20090 means that archers are only capable of shooting at orcs who attack section _i_. Denote as defense level of section _i_ the total number of archers who can shoot at the orcs attacking this section. Reliability of the defense plan is the minimum value of defense level of individual wall section. There is a little time left till the attack so you can't redistribute archers that are already located at the wall. However, there is a reserve of _k_ archers that you can distribute among wall sections in arbitrary way. You would like to achieve maximum possible reliability of the defence plan. Input The first line of the input contains three integers _n_, _r_ and _k_ (1u2009≤u2009_n_u2009≤u2009500u2009000, 0u2009≤u2009_r_u2009≤u2009_n_, 0u2009≤u2009_k_u2009≤u20091018)xa0— the number of sections of the wall, the maximum distance to other section archers can still shoot and the number of archers yet to be distributed along the wall. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109)xa0— the current number of archers at each section. Output Print one integerxa0— the maximum possible value of defense plan reliability, i.e. the maximum possible value of minimum defense level if we distribute _k_ additional archers optimally.
2,000
false
true
false
false
true
false
false
true
false
false
5,903
884B
A one-dimensional Japanese crossword can be represented as a binary string of length _x_. An encoding of this crossword is an array _a_ of size _n_, where _n_ is the number of segments formed completely of 1's, and _a__i_ is the length of _i_-th segment. No two segments touch or intersect. For example: If _x_u2009=u20096 and the crossword is 111011, then its encoding is an array {3,u20092}; If _x_u2009=u20098 and the crossword is 01101010, then its encoding is an array {2,u20091,u20091}; If _x_u2009=u20095 and the crossword is 11111, then its encoding is an array {5}; If _x_u2009=u20095 and the crossword is 00000, then its encoding is an empty array. Mishka wants to create a new one-dimensional Japanese crossword. He has already picked the length and the encoding for this crossword. And now he needs to check if there is exactly one crossword such that its length and encoding are equal to the length and encoding he picked. Help him to check it! Input The first line contains two integer numbers _n_ and _x_ (1u2009≤u2009_n_u2009≤u2009100000, 1u2009≤u2009_x_u2009≤u2009109) — the number of elements in the encoding and the length of the crossword Mishka picked. The second line contains _n_ integer numbers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u200910000) — the encoding. Output Print YES if there exists exaclty one crossword with chosen length and encoding. Otherwise, print NO.
1,100
false
false
true
false
false
false
false
false
false
false
6,213
887F
During the final part of fashion show all models come to the stage and stay in one row and fashion designer stays to right to model on the right. During the rehearsal, Izabella noticed, that row isn't nice, but she can't figure out how to fix it. Like many other creative people, Izabella has a specific sense of beauty. Evaluating beauty of row of models Izabella looks at heights of models. She thinks that row is nice if for each model distance to nearest model with less height (model or fashion designer) to the right of her doesn't exceed _k_ (distance between adjacent people equals 1, the distance between people with exactly one man between them equals 2, etc). She wants to make row nice, but fashion designer has his own sense of beauty, so she can at most one time select two models from the row and swap their positions if the left model from this pair is higher than the right model from this pair. Fashion designer (man to the right of rightmost model) has less height than all models and can't be selected for exchange. You should tell if it's possible to make at most one exchange in such a way that row becomes nice for Izabella. Input In first line there are two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u20095·105, 1u2009≤u2009_k_u2009≤u2009_n_)xa0— number of models and required distance. Second line contains _n_ space-separated integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— height of each model. Pay attention that height of fashion designer is not given and can be less than 1. Output Print «YES» (without quotes) if it's possible to make row nice using at most one exchange, and «NO» (without quotes) otherwise.
2,500
false
true
false
false
false
false
false
false
true
false
6,197
1090L
, # Problem A. Berland University Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 512 megabytes There are t students studying in the best university of Berland. They only study programming in Berland, so there is only one subject. Each student must attend the lectures. The entire course consists of n lectures. It is known that a student who visits at least k of them will pass the course. There are only two auditoriums in the university, one has space for a people and the other one — for b people. To make it comfortable, the administration decided that in odd weeks the lectures will be in the first auditorium, and in the even weeks — in the second auditorium. So the first lecture will be in the 1-st auditorium, the second lecture in the 2-nd one, the third in the 1-st one again, and so on. The sizes of the auditoriums are small so it might not be possible for all students to attend at least k lectures. They ask you to count the maximum number of students that can pass the course. # Input The first line contains five integers: • t — the number of students; • n — the number of lectures; • a — the size of the first auditorium; • b — the size of the second auditorium; • k — the minimal number of lectures to pass the course. The limits are: 1 ≤ t, n, a, b, k ≤ 10 9. # Output Print a single integer — the maximal number of students that can attend at least k lectures and thus pass the course. # Examples standard input standard output 10 3 4 4 3 410 3 4 4 5 0100000 100000 100000 100000 1 100000 5 4 5 3 3 5100 9 6 3 6 7 # Note In the fourth sample, 5 students can pass the course. Here’s one possible strategy: 1. Students 1, 2, 3, 4, 5 visit the first lecture. 2. Students 1, 3 visit the second lecture. 3. Students 1, 2, 3, 4, 5 visit the third lecture. 4. Students 2, 4, 5 visit the fourth lecture. This way each of these 5 students can attend at least 3 lectures. Page 1 of 1
2,000
true
true
false
false
false
false
false
false
false
false
5,261
629D
As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake. Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has _n_ simple cakes and he is going to make a special cake placing some cylinders on each other. However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number _i_ can be placed only on the table or on some cake number _j_ where _j_u2009<u2009_i_. Moreover, in order to impress friends Babaei will put the cake _i_ on top of the cake _j_ only if the volume of the cake _i_ is strictly greater than the volume of the cake _j_. Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of simple cakes Babaei has. Each of the following _n_ lines contains two integers _r__i_ and _h__i_ (1u2009≤u2009_r__i_,u2009_h__i_u2009≤u200910u2009000), giving the radius and height of the _i_-th cake. Output Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Note In first sample, the optimal way is to choose the cake number 1. In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4.
2,000
false
false
false
true
true
false
false
false
false
false
7,306
292E
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly. More formally, you've got two arrays of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ and _b_1,u2009_b_2,u2009...,u2009_b__n_ of length _n_. Also, you've got _m_ queries of two types: 1. Copy the subsegment of array _a_ of length _k_, starting from position _x_, into array _b_, starting from position _y_, that is, execute _b__y_u2009+u2009_q_u2009=u2009_a__x_u2009+u2009_q_ for all integer _q_ (0u2009≤u2009_q_u2009<u2009_k_). The given operation is correct — both subsegments do not touch unexistent elements. 2. Determine the value in position _x_ of array _b_, that is, find value _b__x_. For each query of the second type print the result — the value of the corresponding element of array _b_. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (_a__i_u2009≤u2009109). The third line contains an array of integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (_b__i_u2009≤u2009109). Next _m_ lines contain the descriptions of the queries. The _i_-th line first contains integer _t__i_ — the type of the _i_-th query (1u2009≤u2009_t__i_u2009≤u20092). If _t__i_u2009=u20091, then the _i_-th query means the copying operation. If _t__i_u2009=u20092, then the _i_-th query means taking the value in array _b_. If _t__i_u2009=u20091, then the query type is followed by three integers _x__i_,u2009_y__i_,u2009_k__i_ (1u2009≤u2009_x__i_,u2009_y__i_,u2009_k__i_u2009≤u2009_n_) — the parameters of the copying query. If _t__i_u2009=u20092, then the query type is followed by integer _x__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_) — the position in array _b_. All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays _a_ and _b_.
1,900
false
false
false
false
true
false
false
false
false
false
8,662
724E
There are _n_ cities located along the one-way road. Cities are numbered from 1 to _n_ in the direction of the road. The _i_-th city had produced _p__i_ units of goods. No more than _s__i_ units of goods can be sold in the _i_-th city. For each pair of cities _i_ and _j_ such that 1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_n_ you can no more than once transport no more than _c_ units of goods from the city _i_ to the city _j_. Note that goods can only be transported from a city with a lesser index to the city with a larger index. You can transport goods between cities in any order. Determine the maximum number of produced goods that can be sold in total in all the cities after a sequence of transportations. Input The first line of the input contains two integers _n_ andxa0_c_ (1u2009≤u2009_n_u2009≤u200910u2009000, 0u2009≤u2009_c_u2009≤u2009109)xa0— the number of cities and the maximum amount of goods for a single transportation. The second line contains _n_ integers _p__i_ (0u2009≤u2009_p__i_u2009≤u2009109)xa0— the number of units of goods that were produced in each city. The third line of input contains _n_ integers _s__i_ (0u2009≤u2009_s__i_u2009≤u2009109)xa0— the number of units of goods that can be sold in each city. Output Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations. Examples Input 5 1 7 4 2 1 0 1 2 3 4 5 Input 4 3 13 10 7 4 4 7 10 13
2,900
false
true
false
true
false
false
false
false
false
false
6,910
1718B
In all schools in Buryatia, in the $$$1$$$ class, everyone is told the theory of Fibonacci strings. "A block is a subsegment of a string where all the letters are the same and are bounded on the left and right by the ends of the string or by letters other than the letters in the block. A string is called a Fibonacci string if, when it is divided into blocks, their lengths in the order they appear in the string form the Fibonacci sequence ($$$f_0 = f_1 = 1$$$, $$$f_i = f_{i-2} + f_{i-1}$$$), starting from the zeroth member of this sequence. A string is called semi-Fibonacci if it possible to reorder its letters to get a Fibonacci string." Burenka decided to enter the Buryat State University, but at the entrance exam she was given a difficult task. She was given a string consisting of the letters of the Buryat alphabet (which contains exactly $$$k$$$ letters), and was asked if the given string is semi-Fibonacci. The string can be very long, so instead of the string, she was given the number of appearances of each letter ($$$c_i$$$ for the $$$i$$$-th letter) in that string. Unfortunately, Burenka no longer remembers the theory of Fibonacci strings, so without your help she will not pass the exam. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The following is a description of the input data sets. The first line of each test case contains one integer $$$k$$$ ($$$1 leq k leq 100$$$) — the number of letters in the alphabet. The second line of each test case contains $$$k$$$ integers $$$c_1, c_2, ldots, c_k$$$ ($$$1 leq c_i leq 10^9$$$) — the number of occurrences of each letter in the string. Output For each test case print the string "YES" if the corresponding string is semi-Fibonacci, and "NO" if it is not. You can print "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes" will be recognized as a positive answer). Example Input 6 1 1 2 1 1 2 1 2 3 3 1 3 2 7 5 6 26 8 3 4 13 34 Output YES YES NO YES NO YES Note In the first test case, a one-character string is semi-Fibonacci, being itself a Fibonacci string. In the second test case, a string of two different characters is Fibonacci. In the third test case, the string "abb" (let the first of the alphabet letter be a, the second letter b) is not a semi-Fibonacci string, since no permutation of its letters ("abb", "bab", and "bba") is a Fibonacci string. In the fourth test case, two permutations of the letters of the string "abaccac" (the first letter is a, the second letter is b, the third letter is c) are Fibonacci strings — "abaaccc" and "cbccaaa".
2,000
true
true
true
false
false
false
false
false
false
false
1,973
1015F
You are given a bracket sequence $$$s$$$ (not necessarily a regular one). A bracket sequence is a string containing only characters '(' and ')'. A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not. Your problem is to calculate the number of regular bracket sequences of length $$$2n$$$ containing the given bracket sequence $$$s$$$ as a substring (consecutive sequence of characters) modulo $$$10^9+7$$$ ($$$1000000007$$$). Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 100$$$) — the half-length of the resulting regular bracket sequences (the resulting sequences must have length equal to $$$2n$$$). The second line of the input contains one string $$$s$$$ ($$$1 le s le 200$$$) — the string $$$s$$$ that should be a substring in each of the resulting regular bracket sequences ($$$s$$$ is the length of $$$s$$$). Output Print only one integer — the number of regular bracket sequences containing the given bracket sequence $$$s$$$ as a substring. Since this number can be huge, print it modulo $$$10^9+7$$$ ($$$1000000007$$$). Note All regular bracket sequences satisfying the conditions above for the first example: "(((()))())"; "((()()))()"; "((()))()()"; "(()(()))()"; "()((()))()". All regular bracket sequences satisfying the conditions above for the second example: "((()))"; "(()())"; "(())()"; "()(())". And there is no regular bracket sequences of length $$$4$$$ containing "(((" as a substring in the third example.
2,300
false
false
false
true
false
false
false
false
false
false
5,613
2041A
Problem - 2041A - 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 sortings *1300 No tag edit access → Contest materials ") ") and outputs the restaurant you should visit on Friday. Input A single line of input containing four integers $$$a, b, c, d$$$, each between 1 and 5 (inclusive), representing the restaurant numbers you visited from Monday to Thursday, in order. $$$1le a, b, c, dle 5$$$ All four numbers will be different. Output Output the restaurant number you should visit on Friday. Examples Input 1 3 2 5 Output 4 Input 2 5 4 3 Output 1
1,300
false
false
true
false
false
false
false
false
true
false
13
1408C
There is a road with length $$$l$$$ meters. The start of the road has coordinate $$$0$$$, the end of the road has coordinate $$$l$$$. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of $$$1$$$ meter per second. There are $$$n$$$ flags at different coordinates $$$a_1, a_2, ldots, a_n$$$. Each time when any of two cars drives through a flag, the speed of that car increases by $$$1$$$ meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$): the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$l$$$ ($$$1 leq n leq 10^5$$$, $$$1 leq l leq 10^9$$$): the number of flags and the length of the road. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ in the increasing order ($$$1 leq a_1 < a_2 < ldots < a_n < l$$$). It is guaranteed that the sum of $$$n$$$ among all test cases does not exceed $$$10^5$$$. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed $$$10^{-6}$$$. More formally, if your answer is $$$a$$$ and jury's answer is $$$b$$$, your answer will be considered correct if $$$frac{a-b}{max{(1, b)}} leq 10^{-6}$$$. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate $$$5$$$. The first car will be in the coordinate $$$1$$$ in $$$1$$$ second and after that its speed will increase by $$$1$$$ and will be equal to $$$2$$$ meters per second. After $$$2$$$ more seconds it will be in the coordinate $$$5$$$. So, it will be in the coordinate $$$5$$$ in $$$3$$$ seconds. The second car will be in the coordinate $$$9$$$ in $$$1$$$ second and after that its speed will increase by $$$1$$$ and will be equal to $$$2$$$ meters per second. After $$$2$$$ more seconds it will be in the coordinate $$$5$$$. So, it will be in the coordinate $$$5$$$ in $$$3$$$ seconds. In the second test case after $$$1$$$ second the first car will be in the coordinate $$$1$$$ and will have the speed equal to $$$2$$$ meters per second, the second car will be in the coordinate $$$9$$$ and will have the speed equal to $$$1$$$ meter per second. So, they will meet after $$$frac{9-1}{2+1} = frac{8}{3}$$$ seconds. So, the answer is equal to $$$1 + frac{8}{3} = frac{11}{3}$$$.
1,500
true
false
true
true
false
false
false
true
false
false
3,634
957A
Overlooking the captivating blend of myriads of vernal hues, Arkady the painter lays out a long, long canvas. Arkady has a sufficiently large amount of paint of three colours: cyan, magenta, and yellow. On the one-dimensional canvas split into _n_ consecutive segments, each segment needs to be painted in one of the colours. Arkady has already painted some (possibly none or all) segments and passes the paintbrush to you. You are to determine whether there are at least two ways of colouring all the unpainted segments so that no two adjacent segments are of the same colour. Two ways are considered different if and only if a segment is painted in different colours in them. Input The first line contains a single positive integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the length of the canvas. The second line contains a string _s_ of _n_ characters, the _i_-th of which is either 'C' (denoting a segment painted in cyan), 'M' (denoting one painted in magenta), 'Y' (one painted in yellow), or '?' (an unpainted one). Output If there are at least two different ways of painting, output "Yes"; otherwise output "No" (both without quotes). You can print each character in any case (upper or lower). Note For the first example, there are exactly two different ways of colouring: CYCMY and CYMCY. For the second example, there are also exactly two different ways of colouring: CMCMY and CYCMY. For the third example, there are four ways of colouring: MCYCM, MCYCY, YCYCM, and YCYCY. For the fourth example, no matter how the unpainted segments are coloured, the existing magenta segments will prevent the painting from satisfying the requirements. The similar is true for the fifth example.
1,300
false
false
true
false
false
false
false
false
false
false
5,894
416C
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are _n_ booking requests received by now. Each request is characterized by two numbers: _c__i_ and _p__i_ — the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all _c__i_ people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are _k_ tables in the restaurant. For each table, we know _r__i_ — the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of requests from visitors. Then _n_ lines follow. Each line contains two integers: _c__i_,u2009_p__i_ (1u2009≤u2009_c__i_,u2009_p__i_u2009≤u20091000) — the size of the group of visitors who will come by the _i_-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer _k_ (1u2009≤u2009_k_u2009≤u20091000) — the number of tables in the restaurant. The last line contains _k_ space-separated integers: _r_1,u2009_r_2,u2009...,u2009_r__k_ (1u2009≤u2009_r__i_u2009≤u20091000) — the maximum number of people that can sit at each table. Output In the first line print two integers: _m_,u2009_s_ — the number of accepted requests and the total money you get from these requests, correspondingly. Then print _m_ lines — each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. Examples Input 3 10 50 2 100 5 30 3 4 6 9
1,600
false
true
true
true
false
false
false
true
false
false
8,160
1787I
Define the beauty value of a sequence $$$b_1,b_2,ldots,b_c$$$ as the maximum value of $$$sumlimits_{i=1}^{q}b_i + sumlimits_{i=s}^{t}b_i$$$, where $$$q$$$, $$$s$$$, $$$t$$$ are all integers and $$$s > q$$$ or $$$tleq q$$$. Note that $$$b_i = 0$$$ when $$$i<1$$$ or $$$i>c$$$, $$$sumlimits_{i=s}^{t}b_i = 0$$$ when $$$s>t$$$. For example, when $$$b = [-1,-2,-3]$$$, we may have $$$q = 0$$$, $$$s = 3$$$, $$$t = 2$$$ so the beauty value is $$$0 + 0 = 0$$$. And when $$$b = [-1,2,-3]$$$, we have $$$q = s = t = 2$$$ so the beauty value is $$$1 + 2 = 3$$$. You are given a sequence $$$a$$$ of length $$$n$$$, determine the sum of the beauty value of all non-empty subsegments $$$a_l,a_{l+1},ldots,a_r$$$ ($$$1leq lleq rleq n$$$) of the sequence $$$a$$$. Print the answer modulo $$$998,244,353$$$. Input Each test contains multiple test cases. The first line contains an integer $$$T$$$ ($$$1 le T le 10^4$$$) — the number of test cases. The first line contains an integer $$$n$$$ ($$$1le nle 10^6$$$) — the length of $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$-10^6 leq a_i leq 10^6$$$) — the given sequence. It's guaranteed that the sum of $$$n$$$ does not exceed $$$10^6$$$. Output For each test case, print a line containing a single integer — the answer modulo $$$998,244,353$$$. Example Input 4 7 80 59 100 -52 -86 -62 75 8 -48 -14 -26 43 -41 34 13 55 1 74 20 56 -60 62 13 88 -48 64 36 -10 19 94 25 -69 88 87 79 -70 74 -26 59 Output 5924 2548 148 98887 Note In the second test case, for the subsequence $$$[-26,43,-41,34,13]$$$, when $$$q=5$$$, $$$s=2$$$, $$$t=5$$$, $$$sumlimits_{i=1}^{q}b_i + sumlimits_{i=s}^{t}b_i = 23 + 49 = 72$$$. In the third test case, there is only one non-empty consecutive subsequence $$$[74]$$$. When $$$q=1$$$, $$$s=1$$$, $$$t=1$$$, $$$sumlimits_{i=1}^{q}b_i + sumlimits_{i=s}^{t}b_i = 148$$$.
3,400
false
false
false
false
true
false
false
false
false
false
1,555
1906A
You are playing a word puzzle. The puzzle starts with a $$$3$$$ by $$$3$$$ grid, where each cell contains either the letter A, B, or C. The goal of this puzzle is to find the lexicographically smallest possible word of length $$$3$$$. The word can be formed by choosing three different cells where the cell containing the first letter is adjacent to the cell containing the second letter, and the cell containing the second letter is adjacent to the cell containing the third letter. Two cells are adjacent to each other if they share a border or a corner, as shown in the following illustration. Formally, if $$$(r, c)$$$ denotes the cell in the $$$r$$$-th row and $$$c$$$-th column, then cell $$$(r, c)$$$ is adjacent to cell $$$(r, c + 1)$$$, $$$(r - 1, c + 1)$$$, $$$(r - 1, c)$$$, $$$(r - 1, c - 1)$$$, $$$(r, c - 1)$$$, $$$(r + 1, c - 1)$$$, $$$(r + 1, c)$$$, and $$$(r + 1, c + 1)$$$. Determine the lexicographically smallest possible word of length $$$3$$$ that you can find within the grid. A string $$$s$$$ of length $$$n$$$ is lexicographically smaller than string $$$t$$$ of the same length if there exists an integer $$$1 leq i leq n$$$ such that $$$s_j = t_j$$$ for all $$$1 leq j < i$$$, and $$$s_i < t_i$$$ in alphabetical order. The following illustration shows some examples on some grids and their the lexicographically smallest possible word of length $$$3$$$ that you can find within the grids. Input Input consists of three lines, each containing three letters, representing the puzzle grid. Each letter in the grid can only be either A, B, or C.
1,000
false
false
false
false
false
false
true
false
false
false
877
238C
You must have heard of the two brothers dreaming of ruling the world. With all their previous plans failed, this time they decided to cooperate with each other in order to rule the world. As you know there are _n_ countries in the world. These countries are connected by _n_u2009-u20091 directed roads. If you don't consider direction of the roads there is a unique path between every pair of countries in the world, passing through each road at most once. Each of the brothers wants to establish his reign in some country, then it's possible for him to control the countries that can be reached from his country using directed roads. The brothers can rule the world if there exists at most two countries for brothers to choose (and establish their reign in these countries) so that any other country is under control of at least one of them. In order to make this possible they want to change the direction of minimum number of roads. Your task is to calculate this minimum number of roads. Input The first line of input contains an integer _n_ (1u2009≤u2009_n_u2009≤u20093000). Each of the next _n_u2009-u20091 lines contains two space-separated integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_;xa0_a__i_u2009≠u2009_b__i_) saying there is a road from country _a__i_ to country _b__i_. Consider that countries are numbered from 1 to _n_. It's guaranteed that if you don't consider direction of the roads there is a unique path between every pair of countries in the world, passing through each road at most once. Output In the only line of output print the minimum number of roads that their direction should be changed so that the brothers will be able to rule the world.
2,100
false
true
false
true
false
false
false
false
false
false
8,882
388A
Fox Ciel has _n_ boxes in her room. They have the same size and weight, but they might have different strength. The _i_-th box can hold at most _x__i_ boxes on its top (we'll call _x__i_ the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than _x__i_ boxes on the top of _i_-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100). The next line contains _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (0u2009≤u2009_x__i_u2009≤u2009100). Output Output a single integer — the minimal possible number of piles. Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom).
1,400
false
true
false
false
false
false
false
false
true
false
8,277
316F3
Smart Beaver became interested in drawing. He draws suns. However, at some point, Smart Beaver realized that simply drawing suns is boring. So he decided to design a program that will process his drawings. You are given a picture drawn by the beaver. It will have two colors: one for the background and one for the suns in the image. Your task will be to count the number of suns in the image and for each of them to count the number of rays. Sun is arbitrarily rotated ellipse with rays. Ray is a segment which connects point on boundary of the ellipse with some point outside ellipse. An image where all suns are circles. An image where all suns are ellipses, their axes are parallel to the coordinate axes. An image where all suns are rotated ellipses. It is guaranteed that: No two suns have common points. The rays’ width is 3 pixels. The lengths of the ellipsis suns’ axes will lie between 40 and 200 pixels. No two rays intersect. The lengths of all rays will lie between 10 and 30 pixels. Input The first line contains two integers _h_ and _w_ — the height and width of the image (1u2009≤u2009_h_,u2009_w_u2009≤u20091600). Next _h_ lines will contain _w_ space-separated integers each. They describe Smart Beaver’s picture. Each number equals either a 0 (the image background), or a 1 (the sun color). The input limits for scoring 30 points are (subproblem F1): All suns on the image are circles. The input limits for scoring 70 points are (subproblems F1+F2): All suns on the image are ellipses with axes parallel to the coordinate axes. The input limits for scoring 100 points are (subproblems F1+F2+F3): All suns on the image are ellipses, they can be arbitrarily rotated. Output The first line must contain a single number _k_ — the number of suns on the beaver’s image. The second line must contain exactly _k_ space-separated integers, corresponding to the number of rays on each sun. The numbers of the second line must be sorted in the increasing order. Examples Note For each complexity level you are suggested a sample in the initial data. You can download the samples at
2,200
false
false
true
false
false
true
false
false
false
false
8,569
1267C
, # Cactus Revenge Input file: standard input Output file: standard output Time limit: 3 seconds Memory limit: 512 megabytes NE(E)RC featured a number of problems in previous years about cactuses — connected undirected graphs in which every edge belongs to at most one simple cycle. Intuitively, a cactus is a generalization of a tree where some cycles are allowed. The traditional cactus that was initially used in NEERC 2005 problem is given on the second picture in the Examples section. You are given n integers d1, d 2, . . . , d n. Construct any cactus with n vertices such that vertex i has degree di (i. e. exactly di incident edges), or determine that no such cactus exists. Parallel edges and loops are not allowed. # Input The first line contains a single integer n (2 ≤ n ≤ 2 000 ) — the number of vertices in the cactus. The second line contains n integers d1, d 2, . . . , d n (1 ≤ di ≤ n − 1) — the desired vertex degrees. # Output If it’s impossible to construct a cactus satisfying the conditions, output a single integer −1.Otherwise, by tradition, output the constructed cactus as a set of edge-distinct paths. In the first line output an integer m — the number of such paths. Each of the following m lines should contain a path in the graph. A path should start with an integer ki (ki ≥ 2) followed by ki integers from 1 to n. These ki integers should represent consecutive vertices of this path. Adjacent vertices in the path should be distinct. The path can visit the same vertex multiple times, but every edge of the cactus should be traversed exactly once in the whole output. # Examples standard input standard output Illustration 52 2 3 2 1 42 1 2 2 2 3 2 3 1 3 3 4 5 123 4 5 43 3 2 2 -1 61 2 1 1 2 1 -1 15 1 4 3 2 2 2 2 2 4 4 2 2 2 2 2 39 1 2 3 4 5 6 7 8 3 7 2 9 10 11 12 13 10 5 2 14 9 15 10 12345678910 11 12 13 14 15 Page 1 of 2 , # Note Both in the second and the third example, there exist graphs that satisfy the given conditions but none of them are cactuses. Page 2 of 2
3,500
false
false
false
false
false
true
false
false
false
false
4,361
1676A
A ticket is a string consisting of six digits. A ticket is considered lucky if the sum of the first three digits is equal to the sum of the last three digits. Given a ticket, output if it is lucky or not. Note that a ticket can have leading zeroes. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of testcases. The description of each test consists of one line containing one string consisting of six digits. Output Output $$$t$$$ lines, each of which contains the answer to the corresponding test case. Output "YES" if the given ticket is lucky, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 5 213132 973894 045207 000000 055776 Note In the first test case, the sum of the first three digits is $$$2 + 1 + 3 = 6$$$ and the sum of the last three digits is $$$1 + 3 + 2 = 6$$$, they are equal so the answer is "YES". In the second test case, the sum of the first three digits is $$$9 + 7 + 3 = 19$$$ and the sum of the last three digits is $$$8 + 9 + 4 = 21$$$, they are not equal so the answer is "NO". In the third test case, the sum of the first three digits is $$$0 + 4 + 5 = 9$$$ and the sum of the last three digits is $$$2 + 0 + 7 = 9$$$, they are equal so the answer is "YES".
800
false
false
true
false
false
false
false
false
false
false
2,215
188G
Problem - 188G - Codeforces =============== xa0 — the size of the array. The following _n_ lines contain the elements of the array, one per line. Each element of the array is an integer between 1 and 100, inclusive. The array might contain duplicate elements. Output Output space-separated elements of the sorted array. Examples Input 5 7 1 9 7 3 Output 1 3 7 7 9 Input 10 100 1 100 1 100 1 100 1 100 1 Output 1 1 1 1 1 100 100 100 100 100
1,600
false
false
true
false
false
false
false
false
false
false
9,086
1689A
Kuznecov likes art, poetry, and music. And strings consisting of lowercase English letters. Recently, Kuznecov has found two strings, $$$a$$$ and $$$b$$$, of lengths $$$n$$$ and $$$m$$$ respectively. They consist of lowercase English letters and no character is contained in both strings. Let another string $$$c$$$ be initially empty. Kuznecov can do the following two types of operations: Choose any character from the string $$$a$$$, remove it from $$$a$$$, and add it to the end of $$$c$$$. Choose any character from the string $$$b$$$, remove it from $$$b$$$, and add it to the end of $$$c$$$. But, he can not do more than $$$k$$$ operations of the same type in a row. He must perform operations until either $$$a$$$ or $$$b$$$ becomes empty. What is the lexicographically smallest possible value of $$$c$$$ after he finishes? A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x eq y$$$; in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$. Input There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. This is followed by the test cases description. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1leq n,m,k leq 100$$$)xa0— parameters from the statement. The second line of each test case contains the string $$$a$$$ of length $$$n$$$. The third line of each test case contains the string $$$b$$$ of length $$$m$$$. The strings contain only lowercase English letters. It is guaranteed that no symbol appears in $$$a$$$ and $$$b$$$ simultaneously. Output In each test case, output a single string $$$c$$$xa0— the answer to the problem. Example Input 3 6 4 2 aaaaaa bbbb 5 9 3 caaca bedededeb 7 7 1 noskill wxhtzdy Output aabaabaa aaabbcc dihktlwlxnyoz Note In the first test case, it is optimal to take two 'a's from the string $$$a$$$ and add them to the string $$$c$$$. Then it is forbidden to take more characters from $$$a$$$, hence one character 'b' from the string $$$b$$$ has to be taken. Following that logic, we end up with $$$c$$$ being 'aabaabaa' when string $$$a$$$ is emptied. In the second test case it is optimal to take as many 'a's from string $$$a$$$ as possible, then take as many 'b's as possible from string $$$b$$$. In the end, we take two 'c's from the string $$$a$$$ emptying it.
800
false
true
true
false
false
false
true
false
true
false
2,148
1650G
Given an undirected connected graph with $$$n$$$ vertices and $$$m$$$ edges. The graph contains no loops (edges from a vertex to itself) and multiple edges (i.e. no more than one edge between each pair of vertices). The vertices of the graph are numbered from $$$1$$$ to $$$n$$$. Find the number of paths from a vertex $$$s$$$ to $$$t$$$ whose length differs from the shortest path from $$$s$$$ to $$$t$$$ by no more than $$$1$$$. It is necessary to consider all suitable paths, even if they pass through the same vertex or edge more than once (i.e. they are not simple). Graph consisting of $$$6$$$ of vertices and $$$8$$$ of edges For example, let $$$n = 6$$$, $$$m = 8$$$, $$$s = 6$$$ and $$$t = 1$$$, and let the graph look like the figure above. Then the length of the shortest path from $$$s$$$ to $$$t$$$ is $$$1$$$. Consider all paths whose length is at most $$$1 + 1 = 2$$$. $$$6 ightarrow 1$$$. The length of the path is $$$1$$$. $$$6 ightarrow 4 ightarrow 1$$$. Path length is $$$2$$$. $$$6 ightarrow 2 ightarrow 1$$$. Path length is $$$2$$$. $$$6 ightarrow 5 ightarrow 1$$$. Path length is $$$2$$$. There is a total of $$$4$$$ of matching paths. Input The first line of test contains the number $$$t$$$ ($$$1 le t le 10^4$$$)xa0—the number of test cases in the test. Before each test case, there is a blank line. The first line of test case contains two numbers $$$n, m$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le m le 2 cdot 10^5$$$)xa0—the number of vertices and edges in the graph. The second line contains two numbers $$$s$$$ and $$$t$$$ ($$$1 le s, t le n$$$, $$$s eq t$$$)xa0—the numbers of the start and end vertices of the path. The following $$$m$$$ lines contain descriptions of edges: the $$$i$$$th line contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i,v_i le n$$$)xa0— the numbers of vertices that connect the $$$i$$$th edge. It is guaranteed that the graph is connected and does not contain loops and multiple edges. It is guaranteed that the sum of values $$$n$$$ on all test cases of input data does not exceed $$$2 cdot 10^5$$$. Similarly, it is guaranteed that the sum of values $$$m$$$ on all test cases of input data does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single numberxa0— the number of paths from $$$s$$$ to $$$t$$$ such that their length differs from the length of the shortest path by no more than $$$1$$$. Since this number may be too large, output it modulo $$$10^9 + 7$$$. Example Input 4 4 4 1 4 1 2 3 4 2 3 2 4 6 8 6 1 1 4 1 6 1 5 1 2 5 6 4 6 6 3 2 6 5 6 1 3 3 5 5 4 3 1 4 2 2 1 1 4 8 18 5 1 2 1 3 1 4 2 5 2 6 5 7 3 8 4 6 4 8 7 1 4 4 7 1 6 6 7 3 8 8 5 4 5 4 3 8 2
2,100
false
false
false
true
true
false
false
false
false
true
2,369
467B
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3». The game has (_m_u2009+u20091) players and _n_ types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (_m_u2009+u20091). Types of soldiers are numbered from 0 to _n_u2009-u20091. Each player has an army. Army of the _i_-th player can be described by non-negative integer _x__i_. Consider binary representation of _x__i_: if the _j_-th bit of number _x__i_ equal to one, then the army of the _i_-th player has soldiers of the _j_-th type. Fedor is the (_m_u2009+u20091)-th player of the game. He assume that two players can become friends if their armies differ in at most _k_ types of soldiers (in other words, binary representations of the corresponding numbers differ in at most _k_ bits). Help Fedor and count how many players can become his friends. Input The first line contains three integers _n_, _m_, _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u200920;xa01u2009≤u2009_m_u2009≤u20091000). The _i_-th of the next (_m_u2009+u20091) lines contains a single integer _x__i_ (1u2009≤u2009_x__i_u2009≤u20092_n_u2009-u20091), that describes the _i_-th player's army. We remind you that Fedor is the (_m_u2009+u20091)-th player. Output Print a single integer — the number of Fedor's potential friends.
1,100
false
false
true
false
false
true
true
false
false
false
7,971
1873H
Marcel and Valeriu are in the mad city, which is represented by $$$n$$$ buildings with $$$n$$$ two-way roads between them. Marcel and Valeriu start at buildings $$$a$$$ and $$$b$$$ respectively. Marcel wants to catch Valeriu, in other words, be in the same building as him or meet on the same road. During each move, they choose to go to an adjacent building of their current one or stay in the same building. Because Valeriu knows Marcel so well, Valeriu can predict where Marcel will go in the next move. Valeriu can use this information to make his move. They start and end the move at the same time. It is guaranteed that any pair of buildings is connected by some path and there is at most one road between any pair of buildings. Assuming both players play optimally, answer if Valeriu has a strategy to indefinitely escape Marcel. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains three space-separated integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$3 leq n leq 2 cdot 10^5$$$; $$$1 leq a, b leq n$$$)xa0— the number of buildings (which equals the number of roads) and the starting buildings of Marcel and Valeriu. The following $$$n$$$ lines each contain two integers $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i eq v_i$$$)xa0— there is a road between buildings $$$u_i$$$ and $$$v_i$$$. There is at most one road between any unordered pair of buildings. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. The roads are given that it is possible to get from any building to any other building going along the roads. Output For each test case output "YES" if Valeriu can escape Marcel forever and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 6 3 2 1 2 1 3 2 1 3 4 1 4 1 4 1 2 1 3 2 3 4 1 2 1 2 2 3 2 4 3 4 7 1 1 4 1 2 1 5 3 4 6 4 2 7 5 3 4 8 5 3 8 3 5 1 2 6 6 8 1 2 4 8 5 7 6 7 10 6 1 1 2 4 3 5 8 7 8 10 4 1 9 2 4 8 1 6 2 3 1 Output YES NO YES NO NO YES Note In the first test case the graph looks as follows: Marcel starts at building $$$2$$$, while Valeriu starts at building $$$1$$$. Valeriu knows which way Marcel will move around the triangle, and he can simply always move in the same way to avoid Marcel forever. In the second test case the graph looks as follows: Marcel starts at building $$$1$$$, while Valeriu starts at building $$$4$$$. Marcel can go to building $$$4$$$ on his first move and win, since Valeriu must either go to building $$$1$$$ (then he meets Marcel on the road from $$$1$$$ to $$$4$$$) or stay at building $$$4$$$ (then he meets Marcel at building $$$4$$$). So there is no strategy for Valeriu to win.
1,700
false
false
false
false
false
false
false
false
false
true
1,037
407D
Problem - 407D - 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 hashing *2700 No tag edit access → Contest materials such elements _a__ij_ of the given matrix that _i_1u2009≤u2009_i_u2009≤u2009_i_2 AND _j_1u2009≤u2009_j_u2009≤u2009_j_2. We'll call the area of the submatrix number (_i_2u2009-u2009_i_1u2009+u20091)·(_j_2u2009-u2009_j_1u2009+u20091). We'll call a submatrix inhomogeneous, if all its elements are distinct. Find the largest (in area) inhomogenous submatrix of the given matrix. Input The first line contains two integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009400)xa0— the number of rows and columns of the matrix, correspondingly. Each of the next _n_ lines contains _m_ integers _a__ij_ (1u2009≤u2009_a__ij_u2009≤u2009160000)xa0— the elements of the matrix. Output Print a single integer — the area of the optimal inhomogenous submatrix. Examples Input 3 3 1 3 1 4 5 6 2 6 1 Output 6 Input 3 4 5 2 3 1 3 3 5 3 4 4 4 5 Output 4 Input 2 6 1 2 3 4 5 6 8 6 7 8 9 1 Output 8
2,700
false
false
false
true
false
false
false
false
false
false
8,195
321E
Fox Ciel is in the Amusement Park. And now she is in a queue in front of the Ferris wheel. There are _n_ people (or foxes more precisely) in the queue: we use first people to refer one at the head of the queue, and _n_-th people to refer the last one in the queue. There will be _k_ gondolas, and the way we allocate gondolas looks like this: When the first gondolas come, the _q_1 people in head of the queue go into the gondolas. Then when the second gondolas come, the _q_2 people in head of the remain queue go into the gondolas.... The remain _q__k_ people go into the last (_k_-th) gondolas. Note that _q_1, _q_2, ..., _q__k_ must be positive. You can get from the statement that and _q__i_u2009>u20090. You know, people don't want to stay with strangers in the gondolas, so your task is to find an optimal allocation way (that is find an optimal sequence _q_) to make people happy. For every pair of people _i_ and _j_, there exists a value _u__ij_ denotes a level of unfamiliar. You can assume _u__ij_u2009=u2009_u__ji_ for all _i_,u2009_j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_) and _u__ii_u2009=u20090 for all _i_ (1u2009≤u2009_i_u2009≤u2009_n_). Then an unfamiliar value of a gondolas is the sum of the levels of unfamiliar between any pair of people that is into the gondolas. A total unfamiliar value is the sum of unfamiliar values for all gondolas. Help Fox Ciel to find the minimal possible total unfamiliar value for some optimal allocation. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u20094000 and 1u2009≤u2009_k_u2009≤u2009_min_(_n_,u2009800)) — the number of people in the queue and the number of gondolas. Each of the following _n_ lines contains _n_ integers — matrix _u_, (0u2009≤u2009_u__ij_u2009≤u20099, _u__ij_u2009=u2009_u__ji_ and _u__ii_u2009=u20090). Please, use fast input methods (for example, please use BufferedReader instead of Scanner for Java). Output Print an integer — the minimal possible total unfamiliar value. Examples Input 5 2 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 Input 8 3 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 Note In the first example, we can allocate people like this: {1, 2} goes into a gondolas, {3, 4, 5} goes into another gondolas. In the second example, an optimal solution is : {1, 2, 3} {4, 5, 6} {7, 8}.
2,600
false
false
false
true
true
false
false
false
false
false
8,547
1175G
Problem - 1175G - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures divide and conquer dp geometry two pointers *3000 No tag edit access → Contest materials . The weight of a subsegment $$$a_l, a_{l+1}, dots, a_r$$$ is equal to $$$(r - l + 1) cdot maxlimits_{l le i le r}(a_i)$$$. The weight of a partition is a total weight of all its segments. Find the partition of minimal weight. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^4$$$, $$$1 le k le min(100, n)$$$) — the length of the array $$$a$$$ and the number of subsegments in the partition. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2 cdot 10^4$$$) — the array $$$a$$$. Output Print single integer — the minimal weight among all possible partitions. Examples Input 4 2 6 1 7 4 Output 25 Input 4 3 6 1 7 4 Output 21 Input 5 4 5 1 5 1 5 Output 21 Note The optimal partition in the first example is next: $$$6$$$ $$$1$$$ $$$7$$$ $$$bigg$$$ $$$4$$$. The optimal partition in the second example is next: $$$6$$$ $$$bigg$$$ $$$1$$$ $$$bigg$$$ $$$7$$$ $$$4$$$. One of the optimal partitions in the third example is next: $$$5$$$ $$$bigg$$$ $$$1$$$ $$$5$$$ $$$bigg$$$ $$$1$$$ $$$bigg$$$ $$$5$$$.
3,000
false
false
false
true
true
false
false
false
false
false
4,839
1569A
You are given a string $$$s$$$, consisting of $$$n$$$ letters, each letter is either 'a' or 'b'. The letters in the string are numbered from $$$1$$$ to $$$n$$$. $$$s[l; r]$$$ is a continuous substring of letters from index $$$l$$$ to $$$r$$$ of the string inclusive. A string is called balanced if the number of letters 'a' in it is equal to the number of letters 'b'. For example, strings "baba" and "aabbab" are balanced and strings "aaab" and "b" are not. Find any non-empty balanced substring $$$s[l; r]$$$ of string $$$s$$$. Print its $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$). If there is no such substring, then print $$$-1$$$ $$$-1$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 le n le 50$$$)xa0— the length of the string. The second line of the testcase contains a string $$$s$$$, consisting of $$$n$$$ letters, each letter is either 'a' or 'b'. Output For each testcase print two integers. If there exists a non-empty balanced substring $$$s[l; r]$$$, then print $$$l$$$ $$$r$$$ ($$$1 le l le r le n$$$). Otherwise, print $$$-1$$$ $$$-1$$$. Example Input 4 1 a 6 abbaba 6 abbaba 9 babbabbaa Note In the first testcase there are no non-empty balanced subtrings. In the second and third testcases there are multiple balanced substrings, including the entire string "abbaba" and substring "baba".
800
false
false
true
false
false
false
false
false
false
false
2,798
788A
Problem - 788A - 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 two pointers *1600 No tag edit access → Contest materials Editorial") xa0— the size of the array _a_. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (-109u2009≤u2009_a__i_u2009≤u2009109)xa0— the array elements. Output Print the only integerxa0— the maximum value of _f_. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of _f_ is reached on intervals
1,600
false
false
false
true
false
false
false
false
false
false
6,621
526B
Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him. The park consists of 2_n_u2009+u20091u2009-u20091 squares connected by roads so that the scheme of the park is a full binary tree of depth _n_. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2_n_,u20092_n_u2009+u20091,u2009...,u20092_n_u2009+u20091u2009-u20091 and these exits lead straight to the Om Nom friends' houses. From each square _i_ (2u2009≤u2009_i_u2009<u20092_n_u2009+u20091) there is a road to the square . Thus, it is possible to go from the park entrance to each of the exits by walking along exactly _n_ roads. To light the path roads in the evening, the park keeper installed street lights along each road. The road that leads from square _i_ to square has _a__i_ lights. Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe. He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u200910) — the number of roads on the path from the entrance to any exit. The next line contains 2_n_u2009+u20091u2009-u20092 numbers _a_2,u2009_a_3,u2009... _a_2_n_u2009+u20091u2009-u20091 — the initial numbers of street lights on each road of the park. Here _a__i_ is the number of street lights on the road between squares _i_ and . All numbers _a__i_ are positive integers, not exceeding 100.
1,400
false
true
true
false
false
false
false
false
false
false
7,730
1921A
Problem - 1921A - 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 math *800 No tag edit access → Contest materials ") ") area is located on the coordinate plane, with sides parallel to the coordinate axes. You are given the coordinates of its corners, in random order. Your task is to find the area of the square. Input Each test consists of several testcases. The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of testcases. The following is a description of the testcases. Each testcase contains four lines, each line contains two integers $$$x_i, y_i$$$ ($$$-1000le x_i, y_ile 1000$$$), coordinates of the corners of the square. It is guaranteed that there is a square with sides parallel to the coordinate axes, with positive (strictly greater than $$$0$$$) area, with corners in given points. Output For each test case, print a single integer, the area of the square. Example Input 3 1 2 4 5 1 5 4 2 -1 1 1 -1 1 1 -1 -1 45 11 45 39 17 11 17 39 Output 9 4 784
800
true
true
false
false
false
false
false
false
false
false
765
1974C
Polycarp was given an array $$$a$$$ of $$$n$$$ integers. He really likes triples of numbers, so for each $$$j$$$ ($$$1 le j le n - 2$$$) he wrote down a triple of elements $$$[a_j, a_{j + 1}, a_{j + 2}]$$$. Polycarp considers a pair of triples $$$b$$$ and $$$c$$$ beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied: $$$b_1 e c_1$$$ and $$$b_2 = c_2$$$ and $$$b_3 = c_3$$$; $$$b_1 = c_1$$$ and $$$b_2 e c_2$$$ and $$$b_3 = c_3$$$; $$$b_1 = c_1$$$ and $$$b_2 = c_2$$$ and $$$b_3 e c_3$$$. Find the number of beautiful pairs of triples among the written triples $$$[a_j, a_{j + 1}, a_{j + 2}]$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0— the elements of the array. It is guaranteed that the sum of the values of $$$n$$$ for all test cases in the test does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the number of beautiful pairs of triples among the pairs of the form $$$[a_j, a_{j + 1}, a_{j + 2}]$$$. Note that the answer may not fit into 32-bit data types. Example Input 8 5 3 2 2 2 3 5 1 2 1 2 1 8 1 2 3 2 2 3 4 2 4 2 1 1 1 8 2 1 1 2 1 1 1 1 7 2 1 1 1 1 1 1 6 2 1 1 1 1 1 5 2 1 1 1 1 Note In the first example, $$$a = [3, 2, 2, 2, 3]$$$, Polycarp will write the following triples: 1. $$$[3, 2, 2]$$$; 2. $$$[2, 2, 2]$$$; 3. $$$[2, 2, 3]$$$. The beautiful pairs are triple $$$1$$$ with triple $$$2$$$ and triple $$$2$$$ with triple $$$3$$$. In the third example, $$$a = [1, 2, 3, 2, 2, 3, 4, 2]$$$, Polycarp will write the following triples: 1. $$$[1, 2, 3]$$$; 2. $$$[2, 3, 2]$$$; 3. $$$[3, 2, 2]$$$; 4. $$$[2, 2, 3]$$$; 5. $$$[2, 3, 4]$$$; 6. $$$[3, 4, 2]$$$; The beautiful pairs are triple $$$1$$$ with triple $$$4$$$, triple $$$2$$$ with triple $$$5$$$, and triple $$$3$$$ with triple $$$6$$$.
1,400
false
false
false
false
true
false
false
false
false
false
447
1196D1
The only difference between easy and hard versions is the size of the input. You are given a string $$$s$$$ consisting of $$$n$$$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $$$k$$$. Your task is to change the minimum number of characters in the initial string $$$s$$$ so that after the changes there will be a string of length $$$k$$$ that is a substring of $$$s$$$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $$$a$$$ is a substring of string $$$b$$$ if there exists a positive integer $$$i$$$ such that $$$a_1 = b_i$$$, $$$a_2 = b_{i + 1}$$$, $$$a_3 = b_{i + 2}$$$, ..., $$$a_{a} = b_{i + a - 1}$$$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 2000$$$)xa0— the number of queries. Then $$$q$$$ queries follow. The first line of the query contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2000$$$)xa0— the length of the string $$$s$$$ and the length of the substring. The second line of the query contains a string $$$s$$$ consisting of $$$n$$$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2000$$$ ($$$sum n le 2000$$$). Output For each query print one integerxa0— the minimum number of characters you need to change in the initial string $$$s$$$ so that after changing there will be a substring of length $$$k$$$ in $$$s$$$ that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
1,500
false
false
true
false
false
false
false
false
false
false
4,719
793E
Earlier, when there was no Internet, each bank had a lot of offices all around Bankopolis, and it caused a lot of problems. Namely, each day the bank had to collect cash from all the offices. Once Oleg the bank client heard a dialogue of two cash collectors. Each day they traveled through all the departments and offices of the bank following the same route every day. The collectors started from the central department and moved between some departments or between some department and some office using special roads. Finally, they returned to the central department. The total number of departments and offices was _n_, the total number of roads was _n_u2009-u20091. In other words, the special roads system was a rooted tree in which the root was the central department, the leaves were offices, the internal vertices were departments. The collectors always followed the same route in which the number of roads was minimum possible, that is 2_n_u2009-u20092. One of the collectors said that the number of offices they visited between their visits to offices _a_ and then _b_ (in the given order) is equal to the number of offices they visited between their visits to offices _b_ and then _a_ (in this order). The other collector said that the number of offices they visited between their visits to offices _c_ and then _d_ (in this order) is equal to the number of offices they visited between their visits to offices _d_ and then _c_ (in this order). The interesting part in this talk was that the shortest path (using special roads only) between any pair of offices among _a_, _b_, _c_ and _d_ passed through the central department. Given the special roads map and the indexes of offices _a_, _b_, _c_ and _d_, determine if the situation described by the collectors was possible, or not. Input The first line contains single integer _n_ (5u2009≤u2009_n_u2009≤u20095000)xa0— the total number of offices and departments. The departments and offices are numbered from 1 to _n_, the central office has index 1. The second line contains four integers _a_, _b_, _c_ and _d_ (2u2009≤u2009_a_,u2009_b_,u2009_c_,u2009_d_u2009≤u2009_n_)xa0— the indexes of the departments mentioned in collector's dialogue. It is guaranteed that these indexes are offices (i.e. leaves of the tree), not departments. It is guaranteed that the shortest path between any pair of these offices passes through the central department. On the third line _n_u2009-u20091 integers follow: _p_2,u2009_p_3,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009<u2009_i_), where _p__i_ denotes that there is a special road between the _i_-th office or department and the _p__i_-th department. Please note the joint enumeration of departments and offices. It is guaranteed that the given graph is a tree. The offices are the leaves, the departments are the internal vertices. Output If the situation described by the cash collectors was possible, print "YES". Otherwise, print "NO". Examples Input 10 3 8 9 10 1 2 2 2 2 2 1 1 1 Input 13 13 12 9 7 1 1 1 1 5 5 2 2 2 3 3 4 Note In the first example the following collector's route was possible: . We can note that between their visits to offices _a_ and _b_ the collectors visited the same number of offices as between visits to offices _b_ and _a_; the same holds for _c_ and _d_ (the collectors' route is infinite as they follow it each day). In the second example there is no route such that between their visits to offices _c_ and _d_ the collectors visited the same number of offices as between visits to offices _d_ and _c_. Thus, there situation is impossible. In the third example one of the following routes is: .
2,900
false
false
false
true
false
true
false
false
false
false
6,603
2020D
One fine evening, Alice sat down to play the classic game "Connect the Dots", but with a twist. To play the game, Alice draws a straight line and marks $$$n$$$ points on it, indexed from $$$1$$$ to $$$n$$$. Initially, there are no arcs between the points, so they are all disjoint. After that, Alice performs $$$m$$$ operations of the following type: She picks three integers $$$a_i$$$, $$$d_i$$$ ($$$1 le d_i le 10$$$), and $$$k_i$$$. She selects points $$$a_i, a_i+d_i, a_i+2d_i, a_i+3d_i, ldots, a_i+k_icdot d_i$$$ and connects each pair of these points with arcs. After performing all $$$m$$$ operations, she wants to know the number of connected components$$$^dagger$$$ these points form. Please help her find this number. $$$^dagger$$$ Two points are said to be in one connected component if there is a path between them via several (possibly zero) arcs and other points. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le m le 2 cdot 10^5$$$). The $$$i$$$-th of the following $$$m$$$ lines contains three integers $$$a_i$$$, $$$d_i$$$, and $$$k_i$$$ ($$$1 le a_i le a_i + k_icdot d_i le n$$$, $$$1 le d_i le 10$$$, $$$0 le k_i le n$$$). It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 cdot 10^5$$$. Output For each test case, output the number of connected components. Example Input 3 10 2 1 2 4 2 2 4 100 1 19 2 4 100 3 1 2 5 7 2 6 17 2 31 Note In the first test case, there are $$$n = 10$$$ points. The first operation joins the points $$$1$$$, $$$3$$$, $$$5$$$, $$$7$$$, and $$$9$$$. The second operation joins the points $$$2$$$, $$$4$$$, $$$6$$$, $$$8$$$, and $$$10$$$. There are thus two connected components: $$${1, 3, 5, 7, 9}$$$ and $$${2, 4, 6, 8, 10}$$$. In the second test case, there are $$$n = 100$$$ points. The only operation joins the points $$$19$$$, $$$21$$$, $$$23$$$, $$$25$$$, and $$$27$$$. Now all of them form a single connected component of size $$$5$$$. The other $$$95$$$ points form single-point connected components. Thus, the answer is $$$1 + 95 = 96$$$. In the third test case, there are $$$n = 100$$$ points. After the operations, all odd points from $$$1$$$ to $$$79$$$ will be in one connected component of size $$$40$$$. The other $$$60$$$ points form single-point connected components. Thus, the answer is $$$1 + 60 = 61$$$.
1,800
true
false
false
true
false
false
true
false
false
true
149
702C
You are given _n_ points on the straight line — the positions (_x_-coordinates) of the cities and _m_ points on the same line — the positions (_x_-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more than _r_ from this tower. Your task is to find minimal _r_ that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than _r_. If _r_u2009=u20090 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than _r_ from this tower. Input The first line contains two positive integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of cities and the number of cellular towers. The second line contains a sequence of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates _a__i_ are given in non-decreasing order. The third line contains a sequence of _m_ integers _b_1,u2009_b_2,u2009...,u2009_b__m_ (u2009-u2009109u2009≤u2009_b__j_u2009≤u2009109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates _b__j_ are given in non-decreasing order. Output Print minimal _r_ so that each city will be covered by cellular network. Examples Input 5 3 1 5 10 14 17 4 11 15
1,500
false
false
true
false
false
false
false
true
false
false
7,015
1690D
You have a stripe of checkered paper of length $$$n$$$. Each cell is either white or black. What is the minimum number of cells that must be recolored from white to black in order to have a segment of $$$k$$$ consecutive black cells on the stripe? If the input data is such that a segment of $$$k$$$ consecutive black cells already exists, then print 0. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Next, descriptions of $$$t$$$ test cases follow. The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2cdot10^5$$$). The second line consists of the letters 'W' (white) and 'B' (black). The line length is $$$n$$$. It is guaranteed that the sum of values $$$n$$$ does not exceed $$$2cdot10^5$$$. Output For each of $$$t$$$ test cases print an integerxa0— the minimum number of cells that need to be repainted from white to black in order to have a segment of $$$k$$$ consecutive black cells. Example Input 4 5 3 BBWBW 5 5 BBWBW 5 1 BBWBW 1 1 W Note In the first test case, $$$s$$$="BBWBW" and $$$k=3$$$. It is enough to recolor $$$s_3$$$ and get $$$s$$$="BBBBW". This string contains a segment of length $$$k=3$$$ consisting of the letters 'B'. In the second test case of the example $$$s$$$="BBWBW" and $$$k=5$$$. It is enough to recolor $$$s_3$$$ and $$$s_5$$$ and get $$$s$$$="BBBBB". This string contains a segment of length $$$k=5$$$ consisting of the letters 'B'. In the third test case of the example $$$s$$$="BBWBW" and $$$k=1$$$. The string $$$s$$$ already contains a segment of length $$$k=1$$$ consisting of the letters 'B'.
1,000
false
false
true
false
false
false
false
false
false
false
2,140
1982F
You have an array $$$a$$$ of $$$n$$$ elements. There are also $$$q$$$ modifications of the array. Before the first modification and after each modification, you would like to know the following: What is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array $$$a$$$ to be completely sorted in non-decreasing order? More formally, you want to select a subarray of the array $$$(l, r)$$$ with the minimum value of $$$r - l + 1$$$. After that, you will sort the elements $$$a_{l}, a_{l + 1}, ldots, a_{r}$$$ and want the condition $$$a_{i} le a_{i + 1}$$$ to hold for all $$$1 le i < n$$$. If the array is already sorted in non-decreasing order, then $$$l$$$ and $$$r$$$ should be considered as equal to $$$-1$$$. Note that finding such $$$(l, r)$$$ does not change the array in any way. The modifications themselves take the form: assign $$$a_{pos} = x$$$ for given $$$pos$$$ and $$$x$$$. Input Each test consists of several test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10$$$)xa0— the number of test cases. Then follows the description of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^{5}$$$). The second line of each test case contains $$$n$$$ integers $$$a_{i}$$$ ($$$0 le a_{i} le 10^{9}$$$)xa0— the initial elements of the array $$$a$$$. The third line of each test case contains a number $$$q$$$ ($$$0 le q le 5 cdot 10^{5}$$$)xa0— the number of modifications to the array. The following $$$q$$$ lines of each test case contain two integers $$$pos_{i}$$$ ($$$1 le pos_{i} le n$$$) and $$$val_{i}$$$ ($$$0 le val_{i} le 10^{9}$$$)xa0— this means that for the $$$i$$$-th modification, $$$a_{pos_{i}}$$$ is assigned the value $$$val_{i}$$$. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ for all test cases does not exceed $$$5 cdot 10^{5}$$$. Output For each test case, output $$$q + 1$$$ lines. Each line should contain $$$2$$$ integers $$$l, r$$$xa0— the boundaries of the minimum subarray, such that sorting it will make the array $$$a$$$ completely sorted. If $$$a$$$ is already sorted, then output $$$l = -1$$$, $$$r = -1$$$. Example Input 2 5 2 2 3 4 5 3 2 1 4 1 1 1 5 1 2 3 4 5 9 1 4 2 3 5 2 3 1 1 1 5 1 4 1 3 1 2 1 Output -1 -1 1 2 1 4 3 4 -1 -1 1 3 1 3 1 5 1 5 2 5 2 5 2 5 2 5 -1 -1 Note Let's consider the first test case: Initially, the array is sorted in non-decreasing order: $$$[2, 2, 3, 4, 5]$$$ After the first query, the array looks like this: $$$[color{red}{2}, color{red}{1}, 3, 4, 5]$$$. After the second query, the array looks like this: $$$[color{red}{2}, color{red}{1}, color{red}{3}, color{red}{1}, 5]$$$. After the third query, the array looks like this: $$$[1, 1, color{red}{3}, color{red}{1}, 5]$$$. The red segments indicate the subarrays that need to be sorted in order for the entire array to be sorted in non-decreasing order.
2,600
false
false
false
false
true
false
false
true
true
false
391
1841D
Two segments $$$[l_1, r_1]$$$ and $$$[l_2, r_2]$$$ intersect if there exists at least one $$$x$$$ such that $$$l_1 le x le r_1$$$ and $$$l_2 le x le r_2$$$. An array of segments $$$[[l_1, r_1], [l_2, r_2], dots, [l_k, r_k]]$$$ is called beautiful if $$$k$$$ is even, and is possible to split the elements of this array into $$$frac{k}{2}$$$ pairs in such a way that: every element of the array belongs to exactly one of the pairs; segments in each pair intersect with each other; segments in different pairs do not intersect. For example, the array $$$[[2, 4], [9, 12], [2, 4], [7, 7], [10, 13], [6, 8]]$$$ is beautiful, since it is possible to form $$$3$$$ pairs as follows: the first element of the array (segment $$$[2, 4]$$$) and the third element of the array (segment $$$[2, 4]$$$); the second element of the array (segment $$$[9, 12]$$$) and the fifth element of the array (segment $$$[10, 13]$$$); the fourth element of the array (segment $$$[7, 7]$$$) and the sixth element of the array (segment $$$[6, 8]$$$). As you can see, the segments in each pair intersect, and no segments from different pairs intersect. You are given an array of $$$n$$$ segments $$$[[l_1, r_1], [l_2, r_2], dots, [l_n, r_n]]$$$. You have to remove the minimum possible number of elements from this array so that the resulting array is beautiful. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$2 le n le 2000$$$) — the number of segments in the array. Then, $$$n$$$ lines follow, the $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 le l_i le r_i le 10^9$$$) denoting the $$$i$$$-th segment. Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Note In the first test case of the example, it is enough to delete the $$$5$$$-th element of the array of segments. Then you get the array $$$[[2, 4], [9, 12], [2, 4], [7, 7], [10, 13], [6, 8]]$$$, which is beautiful. In the second test case of the example, you can delete the $$$1$$$-st, $$$3$$$-rd and $$$4$$$-th element of the array. Then you get the array $$$[[2, 8], [5, 6]]$$$, which is beautiful. In the third test case of the example, you have to delete the whole array.
2,000
false
true
false
false
true
false
false
false
true
false
1,242
1391B
Consider a conveyor belt represented using a grid consisting of $$$n$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row from the top and the $$$j$$$-th column from the left is labelled $$$(i,j)$$$. Every cell, except $$$(n,m)$$$, has a direction R (Right) or D (Down) assigned to it. If the cell $$$(i,j)$$$ is assigned direction R, any luggage kept on that will move to the cell $$$(i,j+1)$$$. Similarly, if the cell $$$(i,j)$$$ is assigned direction D, any luggage kept on that will move to the cell $$$(i+1,j)$$$. If at any moment, the luggage moves out of the grid, it is considered to be lost. There is a counter at the cell $$$(n,m)$$$ from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell $$$(i,j)$$$, any luggage placed in this cell should eventually end up in the cell $$$(n,m)$$$. This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change. Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 le n le 100$$$, $$$1 le m le 100$$$) xa0— the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th line, $$$a_{i,j}$$$ is the initial direction of the cell $$$(i, j)$$$. Please note that $$$a_{n,m}=$$$ C. Note In the first case, just changing the direction of $$$(2,3)$$$ to D is enough. You can verify that the resulting belt is functional. For example, if we place any luggage at $$$(2,2)$$$, it first moves to $$$(3,2)$$$ and then to $$$(3,3)$$$. In the second case, we have no option but to change the first $$$3$$$ cells from D to R making the grid equal to RRRC.
800
false
true
true
false
false
false
true
false
false
false
3,720
1687A
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$$ through $$$n$$$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $$$a_1,a_2,ldots,a_n$$$. Marisa can start out at any point in the forest on minute $$$0$$$. Each minute, the followings happen in order: She moves from point $$$x$$$ to $$$y$$$ ($$$x-yle 1$$$, possibly $$$y=x$$$). She collects all mushrooms on point $$$y$$$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $$$0$$$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $$$k$$$ minutes. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 2 cdot 10 ^ 5$$$, $$$1le k le 10^9$$$) — the number of positions with mushrooms and the time Marisa has, 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$$$) — the initial number of mushrooms on point $$$1,2,ldots,n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10 ^ 5$$$. Output For each test case, print the maximum number of mushrooms Marisa can pick after $$$k$$$ minutes. Example Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 Note Test case 1: Marisa can start at $$$x=2$$$. In the first minute, she moves to $$$x=1$$$ and collect $$$5$$$ mushrooms. The number of mushrooms will be $$$[1,7,2,3,4]$$$. In the second minute, she moves to $$$x=2$$$ and collects $$$7$$$ mushrooms. The numbers of mushrooms will be $$$[2,1,3,4,5]$$$. After $$$2$$$ minutes, Marisa collects $$$12$$$ mushrooms. It can be shown that it is impossible to collect more than $$$12$$$ mushrooms. Test case 2: This is one of her possible moving path: $$$2 ightarrow 3 ightarrow 2 ightarrow 1 ightarrow 2 ightarrow 3 ightarrow 4 ightarrow 5$$$ It can be shown that it is impossible to collect more than $$$37$$$ mushrooms.
1,600
false
true
false
false
false
false
true
false
false
false
2,157
1338B
You have unweighted tree of $$$n$$$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $$$v_{1}$$$ and $$$v_{2}$$$ of this tree, }$$$). It's guaranteed that such assignment always exists under given constraints. Now let's define $$$f$$$ as the number of distinct weights in assignment. In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $$$0$$$. $$$f$$$ value is $$$2$$$ here, because there are $$$2$$$ distinct edge weights($$$4$$$ and $$$5$$$).In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $$$1$$$ and vertex $$$6$$$ ($$$3, 4, 5, 4$$$) is not $$$0$$$. What are the minimum and the maximum possible values of $$$f$$$ for the given tree? Find and print both. Input The first line contains integer $$$n$$$ ($$$3 le n le 10^{5}$$$)xa0— the number of vertices in given tree. The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 le a_{i} lt b_{i} le n$$$)xa0— it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. It is guaranteed that given graph forms tree of $$$n$$$ vertices. Output Print two integersxa0— the minimum and maximum possible value of $$$f$$$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. Examples Input 6 1 3 2 3 3 4 4 5 5 6 Input 6 1 3 2 3 3 4 4 5 4 6 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Note In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. In the second example, possible assignments for each minimum and maximum are described in picture below. The $$$f$$$ value of valid assignment of this tree is always $$$3$$$. In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.
1,800
true
true
false
false
false
true
false
false
false
false
4,013
1571D
Kotlin Heroes competition is nearing completion. This time $$$n$$$ programmers took part in the competition. Now organizers are thinking how to entertain spectators as well. One of the possibilities is holding sweepstakes. So for now they decided to conduct a survey among spectators. In total, organizers asked $$$m$$$ viewers two questions: 1. Who will take the first place? 2. Who will take the last place? After receiving answers, organizers ranked all spectators based on the number of programmers they guessed right. Suppose, there are $$$c_2$$$ viewers who guessed right both first and last place, $$$c_1$$$ viewers who guessed either first or last place right and $$$c_0$$$ viewers who didn't guess at all. All $$$c_2$$$ viewers will get rank $$$1$$$, all viewers with one right answer will get rank $$$c_2 + 1$$$ and all remaining viewersxa0— rank $$$c_2 + c_1 + 1$$$. You were one of the interviewed spectators. Also, as one of the organizers, you have access to survey results, but not to competition results. Calculate, what is the worst rank you can possibly get according to organizers' ranking system? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 1000$$$; $$$1 le m le 2 cdot 10^5$$$)xa0— the number of programmers participating in the competition and the number of surveyed spectators. Next $$$m$$$ lines contain answers of spectators. The $$$i$$$-th line contains two integers $$$f_i$$$ and $$$l_i$$$ ($$$1 le f_i, l_i le n$$$; $$$f_i e l_i$$$)xa0— the indices of programmers who will take the first and last places in opinion of the $$$i$$$-th viewer. For simplicity, you are the first among spectators, so your answers are $$$f_1$$$ and $$$l_1$$$. Output Print the single integerxa0— the worst rank among spectators you can possibly get according to organizers' ranking system (bigger rankxa0— worse, of course). Examples Input 3 6 3 1 3 2 2 1 3 2 3 2 3 1 Note In the first example, if the second programmer takes first place, while the first programmer takes last place, you'll have $$$0$$$ right answers while the other two spectatorsxa0— $$$2$$$ right answers. That's why your rank (in the worst case) will be $$$c_2 + c_1 + 1$$$ $$$=$$$ $$$2 + 0 + 1 = 3$$$. In the second example, for example, if the third programmer takes the first place and the second programmer takes the last place, then you'll have $$$1$$$ right answer. The spectators $$$2$$$, $$$4$$$ and $$$5$$$ will have $$$2$$$ right answers, spectator $$$6$$$xa0— $$$1$$$ right answer and spectator $$$3$$$xa0— $$$0$$$ right answers. As a result, your rank will be equal to $$$c_2 + 1$$$ $$$=$$$ $$$3 + 1 = 4$$$. (Note that spectator $$$6$$$ will have the same rank $$$4$$$).
1,800
true
false
true
false
false
true
true
false
false
false
2,789
1178B
Problem - 1178B - Codeforces =============== xa0 characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo". The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w": "vvvv" "vvvv" "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows: "vvvovvv" "vvvovvv" "vvvovvv" "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive. For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov". Input The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$. Output Output a single integer, the wow factor of $$$s$$$. Examples Input vvvovvv Output 4 Input vvovooovovvovoovoovvvvovovvvov Output 100 Note The first example is explained in the legend.
1,300
false
false
false
true
false
false
false
false
false
false
4,829
1185C2
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $$$n$$$ students. Students will take the exam one-by-one in order from $$$1$$$-th to $$$n$$$-th. Rules of the exam are following: The $$$i$$$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $$$t_i$$$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $$$M$$$ minutes ($$$max t_i le M$$$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $$$i$$$, you should count the minimum possible number of students who need to fail the exam so the $$$i$$$-th student has enough time to pass the exam. For each student $$$i$$$, find the answer independently. That is, if when finding the answer for the student $$$i_1$$$ some student $$$j$$$ should leave, then while finding the answer for $$$i_2$$$ ($$$i_2>i_1$$$) the student $$$j$$$ student does not have to go home. Input The first line of the input contains two integers $$$n$$$ and $$$M$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le M le 2 cdot 10^7$$$)xa0— the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $$$n$$$ integers $$$t_i$$$ ($$$1 le t_i le 100$$$)xa0— time in minutes that $$$i$$$-th student spends to answer to a ticket. It's guaranteed that all values of $$$t_i$$$ are not greater than $$$M$$$. Output Print $$$n$$$ numbers: the $$$i$$$-th number must be equal to the minimum number of students who have to leave the exam in order to $$$i$$$-th student has enough time to pass the exam. Examples Input 5 100 80 40 40 40 60 Note The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $$$M=15$$$ (the sum is $$$1+2+3+4+5=15$$$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $$$0$$$. In order for the $$$6$$$-th student to pass the exam, it is necessary that at least $$$2$$$ students must fail it before (for example, the $$$3$$$-rd and $$$4$$$-th, then the $$$6$$$-th will finish its exam in $$$1+2+5+6=14$$$ minutes, which does not exceed $$$M$$$). In order for the $$$7$$$-th student to pass the exam, it is necessary that at least $$$3$$$ students must fail it before (for example, the $$$2$$$-nd, $$$5$$$-th and $$$6$$$-th, then the $$$7$$$-th will finish its exam in $$$1+3+4+7=15$$$ minutes, which does not exceed $$$M$$$).
1,700
true
true
false
false
true
false
true
false
false
false
4,777
286A
Problem - 286A - 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 math *1400 No tag edit access → Contest materials . A lucky permutation is such permutation _p_, that any integer _i_ (1u2009≤u2009_i_u2009≤u2009_n_) meets this condition _p__p__i_u2009=u2009_n_u2009-u2009_i_u2009+u20091. You have integer _n_. Find some lucky permutation _p_ of size _n_. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the required permutation size. Output Print "-1" (without the quotes) if the lucky permutation _p_ of size _n_ doesn't exist. Otherwise, print _n_ distinct integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_) after a space — the required permutation. If there are multiple answers, you can print any of them. Examples Input 1 Output 1 Input 2 Output -1 Input 4 Output 2 4 1 3 Input 5 Output 2 5 3 1 4
1,400
true
false
false
false
false
true
false
false
false
false
8,691
345D
A chain letter is a kind of a message which urges the recipient to forward it to as many contacts as possible, usually with some kind of mystic explanation. Of course, this is only a superstition, and you don't believe in it, but all your friends do. You know that today there will be one of these letters going around, and you want to know how many times you'll receive it — of course, not that you'll be sending it yourself! You are given an array of strings _f_ with _n_ elements which describes the contacts between you and _n_u2009-u20091 of your friends: _j_-th character of _i_-th string (_f_[_i_][_j_]) is "1" if people _i_ and _j_ will send messages to each other, and "0" otherwise. Person 1 starts sending the letter to all his contacts; every person who receives the letter for the first time sends it to all his contacts. You are person _n_, and you don't forward the letter when you receive it. Calculate the number of copies of this letter you'll receive. Input The first line of the input contains an integer _n_ (2u2009≤u2009_n_u2009≤u200950) — the number of people involved. Next _n_ following lines contain elements of _f_, strings of length _n_. Each character in _f_ is either "0" or "1". It's guaranteed that two following equations hold: _f_[_i_][_j_] = _f_[_j_][_i_], _f_[_i_][_i_] = 0, for all _i_,u2009_j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_). Output Output a single integer — the number of copies of the letter you will receive eventually. Note In the first case, everybody sends letters to everyone, so you get copies from all three of your friends. In the second case, you don't know any of these people, so they don't bother you with their superstitious stuff. In the third case, two of your friends send you copies of the letter but the third friend doesn't know them so he is unaffected.
2,200
false
false
false
false
false
false
false
false
false
true
8,453
534F
In this task you have to write a program dealing with nonograms on fields no larger than 5u2009×u200920. Simplified nonogram is a task where you have to build such field (each cell is either white or black) that satisfies the given information about rows and columns. For each row and each column the number of contiguous black segments is specified. For example if size of the field is _n_u2009=u20093,u2009_m_u2009=u20095, аnd numbers of contiguous black segments in rows are: [2,u20093,u20092] and in columns are: [1,u20090,u20091,u20092,u20091] then the solution may look like: It is guaranteed that on each test in the testset there exists at least one solution. Input In the first line there follow two integers _n_, _m_ (1u2009≤u2009_n_u2009≤u20095,u20091u2009≤u2009_m_u2009≤u200920) — number of rows and number of columns respectively. Second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ where _a__i_ is the number of contiguous black segments in _i_-th row of the field. Similarly, third line contains _m_ integers _b_1,u2009_b_2,u2009...,u2009_b__m_ where _b__i_ is the number of contiguous black segments in the _i_-th column of the field. It is guaranteed that there exists at least one solution. Output Output any possible solution. Output should consist of _n_ lines each containing _m_ characters. Denote white cell as "." and black cell as "*".
2,400
false
false
false
true
false
false
false
false
false
false
7,696
1201D
You are on the island which can be represented as a $$$n imes m$$$ table. The rows are numbered from $$$1$$$ to $$$n$$$ and the columns are numbered from $$$1$$$ to $$$m$$$. There are $$$k$$$ treasures on the island, the $$$i$$$-th of them is located at the position $$$(r_i, c_i)$$$. Initially you stand at the lower left corner of the island, at the position $$$(1, 1)$$$. If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from $$$(r, c)$$$ to $$$(r+1, c)$$$), left (from $$$(r, c)$$$ to $$$(r, c-1)$$$), or right (from position $$$(r, c)$$$ to $$$(r, c+1)$$$). Because of the traps, you can't move down. However, moving up is also risky. You can move up only if you are in a safe column. There are $$$q$$$ safe columns: $$$b_1, b_2, ldots, b_q$$$. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures. Input The first line contains integers $$$n$$$, $$$m$$$, $$$k$$$ and $$$q$$$ ($$$2 le n, , m, , k, , q le 2 cdot 10^5$$$, $$$q le m$$$)xa0— the number of rows, the number of columns, the number of treasures in the island and the number of safe columns. Each of the next $$$k$$$ lines contains two integers $$$r_i, c_i$$$, ($$$1 le r_i le n$$$, $$$1 le c_i le m$$$)xa0— the coordinates of the cell with a treasure. All treasures are located in distinct cells. The last line contains $$$q$$$ distinct integers $$$b_1, b_2, ldots, b_q$$$ ($$$1 le b_i le m$$$) — the indices of safe columns. Output Print the minimum number of moves required to collect all the treasures. Examples Input 3 3 3 2 1 1 2 1 3 1 2 3 Input 3 5 3 2 1 2 2 3 3 1 1 5 Input 3 6 3 2 1 6 2 2 3 4 1 6 Note In the first example you should use the second column to go up, collecting in each row treasures from the first column. In the second example, it is optimal to use the first column to go up. In the third example, it is optimal to collect the treasure at cell $$$(1;6)$$$, go up to row $$$2$$$ at column $$$6$$$, then collect the treasure at cell $$$(2;2)$$$, go up to the top row at column $$$1$$$ and collect the last treasure at cell $$$(3;4)$$$. That's a total of $$$15$$$ moves.
2,100
false
true
true
true
false
false
false
true
false
false
4,692
1613D
Let's call a sequence of integers $$$x_1, x_2, dots, x_k$$$ MEX-correct if for all $$$i$$$ ($$$1 le i le k$$$) $$$x_i - operatorname{MEX}(x_1, x_2, dots, x_i) le 1$$$ holds. Where $$$operatorname{MEX}(x_1, dots, x_k)$$$ is the minimum non-negative integer that doesn't belong to the set $$$x_1, dots, x_k$$$. For example, $$$operatorname{MEX}(1, 0, 1, 3) = 2$$$ and $$$operatorname{MEX}(2, 1, 5) = 0$$$. You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers. Calculate the number of non-empty MEX-correct subsequences of a given array. The number of subsequences can be very large, so print it modulo $$$998244353$$$. Note: a subsequence of an array $$$a$$$ is a sequence $$$[a_{i_1}, a_{i_2}, dots, a_{i_m}]$$$ meeting the constraints $$$1 le i_1 < i_2 < dots < i_m le n$$$. If two different ways to choose the sequence of indices $$$[i_1, i_2, dots, i_m]$$$ yield the same subsequence, the resulting subsequence should be counted twice (i.u2009e. two subsequences are different if their sequences of indices $$$[i_1, i_2, dots, i_m]$$$ are not the same). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$5 cdot 10^5$$$. Output For each test case, print a single integerxa0— the number of non-empty MEX-correct subsequences of a given array, taken modulo $$$998244353$$$. Note In the first example, the valid subsequences are $$$[0]$$$, $$$[1]$$$, $$$[0,1]$$$ and $$$[0,2]$$$. In the second example, the valid subsequences are $$$[0]$$$ and $$$[1]$$$. In the third example, any non-empty subsequence is valid.
1,900
true
false
false
true
false
false
false
false
false
false
2,573
1823C
A prime number is an integer greater than $$$1$$$, which has exactly two divisors. For example, $$$7$$$ is a prime, since it has two divisors $$${1, 7}$$$. A composite number is an integer greater than $$$1$$$, which has more than two different divisors. Note that the integer $$$1$$$ is neither prime nor composite. Let's look at some composite number $$$v$$$. It has several divisors: some divisors are prime, others are composite themselves. If the number of prime divisors of $$$v$$$ is less or equal to the number of composite divisors, let's name $$$v$$$ as strongly composite. For example, number $$$12$$$ has $$$6$$$ divisors: $$${1, 2, 3, 4, 6, 12}$$$, two divisors $$$2$$$ and $$$3$$$ are prime, while three divisors $$$4$$$, $$$6$$$ and $$$12$$$ are composite. So, $$$12$$$ is strongly composite. Other examples of strongly composite numbers are $$$4$$$, $$$8$$$, $$$9$$$, $$$16$$$ and so on. On the other side, divisors of $$$15$$$ are $$${1, 3, 5, 15}$$$: $$$3$$$ and $$$5$$$ are prime, $$$15$$$ is composite. So, $$$15$$$ is not a strongly composite. Other examples are: $$$2$$$, $$$3$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$10$$$ and so on. You are given $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$a_i > 1$$$). You have to build an array $$$b_1, b_2, dots, b_k$$$ such that following conditions are satisfied: Product of all elements of array $$$a$$$ is equal to product of all elements of array $$$b$$$: $$$a_1 cdot a_2 cdot ldots cdot a_n = b_1 cdot b_2 cdot ldots cdot b_k$$$; All elements of array $$$b$$$ are integers greater than $$$1$$$ and strongly composite; The size $$$k$$$ of array $$$b$$$ is the maximum possible. Find the size $$$k$$$ of array $$$b$$$, or report, that there is no array $$$b$$$ satisfying the conditions. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 1000$$$)xa0— the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integer $$$a_1, a_2, dots a_n$$$ ($$$2 le a_i le 10^7$$$)xa0— the array $$$a$$$ itself. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. Note In the first test case, we can get array $$$b = [18]$$$: $$$a_1 cdot a_2 = 18 = b_1$$$; $$$18$$$ is strongly composite number. In the second test case, we can get array $$$b = [60]$$$: $$$a_1 cdot a_2 cdot a_3 = 60 = b_1$$$; $$$60$$$ is strongly composite number. In the third test case, there is no array $$$b$$$ satisfying the conditions. In the fourth test case, we can get array $$$b = [4, 105]$$$: $$$a_1 cdot a_2 cdot a_3 = 420 = b_1 cdot b_2$$$; $$$4$$$ and $$$105$$$ are strongly composite numbers.
1,300
true
true
false
false
false
false
false
false
false
false
1,341
1736A
You are given two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ elements, each element is either $$$0$$$ or $$$1$$$. You can make operations of $$$2$$$ kinds. Pick an index $$$i$$$ and change $$$a_i$$$ to $$$1-a_i$$$. Rearrange the array $$$a$$$ however you want. Find the minimum number of operations required to make $$$a$$$ equal to $$$b$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 400$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$) — the length of the arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,ldots,a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), representing the array $$$a$$$. The third line of each test case contains $$$n$$$ space-separated integers $$$b_1,b_2,ldots,b_n$$$ ($$$b_i$$$ is $$$0$$$ or $$$1$$$), representing the array $$$b$$$. Output For each test case, print the minimum number of operations required to make $$$a$$$ equal to $$$b$$$. Example Input 5 3 1 0 1 0 0 1 4 1 1 0 0 0 1 1 1 2 1 1 1 1 4 1 0 0 1 0 1 1 0 1 0 1 Note In the first case, we need only one operation: change $$$a_1$$$ to $$$1-a_i$$$. Now $$$a = [0, 0]$$$ which is equal to $$$b$$$. In the second case, the optimal way is to rearrange $$$a$$$ to get the array $$$[0, 1, 11$$$. Now $$$a = [0, 0, 1]$$$ which is equal to $$$b$$$. In the second case, one of optimal ways would be to first change $$$a_3$$$ to $$$1 - a_3$$$, then rearrange $$$a$$$. In the third case, no operation is needed. In the fourth case, the optimal way is to rearrange $$$a$$$ to get the array $$$[0, 1, 1, 0]$$$.
800
false
true
false
false
false
false
true
false
true
false
1,874
1388C
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index. There are $$$n$$$ cities and $$$n−1$$$ undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from $$$1$$$ to $$$n$$$ and the city $$$1$$$ is a capital. In other words, the country has a tree structure. There are $$$m$$$ citizens living in the country. A $$$p_i$$$ people live in the $$$i$$$-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths. Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it. Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the $$$i$$$-th city calculates a happiness index $$$h_i$$$ as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city. Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes. Uncle Bogdan successfully solved the problem. Can you do the same? More formally, You need to check: "Is it possible that, after all people return home, for each city $$$i$$$ the happiness index will be equal exactly to $$$h_i$$$". Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5$$$; $$$0 le m le 10^9$$$)xa0— the number of cities and citizens. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_{n}$$$ ($$$0 le p_i le m$$$; $$$p_1 + p_2 + ldots + p_{n} = m$$$), where $$$p_i$$$ is the number of people living in the $$$i$$$-th city. The third line contains $$$n$$$ integers $$$h_1, h_2, ldots, h_{n}$$$ ($$$-10^9 le h_i le 10^9$$$), where $$$h_i$$$ is the calculated happiness index of the $$$i$$$-th city. Next $$$n − 1$$$ lines contain description of the roads, one per line. Each line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$; $$$x_i eq y_i$$$), where $$$x_i$$$ and $$$y_i$$$ are cities connected by the $$$i$$$-th road. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print YES, if the collected data is correct, or NOxa0— otherwise. You can print characters in YES or NO in any case. Examples Input 2 7 4 1 0 1 1 0 1 0 4 0 0 -1 0 -1 0 1 2 1 3 1 4 3 5 3 6 3 7 5 11 1 2 5 2 1 -11 -2 -6 -2 -1 1 2 1 3 1 4 3 5 Input 2 4 4 1 1 1 1 4 1 -3 -1 1 2 1 3 1 4 3 13 3 3 7 13 1 4 1 2 1 3 Note Let's look at the first test case of the first sample: At first, all citizens are in the capital. Let's describe one of possible scenarios: a person from city $$$1$$$: he lives in the capital and is in good mood; a person from city $$$4$$$: he visited cities $$$1$$$ and $$$4$$$, his mood was ruined between cities $$$1$$$ and $$$4$$$; a person from city $$$3$$$: he visited cities $$$1$$$ and $$$3$$$ in good mood; a person from city $$$6$$$: he visited cities $$$1$$$, $$$3$$$ and $$$6$$$, his mood was ruined between cities $$$1$$$ and $$$3$$$; In total, $$$h_1 = 4 - 0 = 4$$$, $$$h_2 = 0$$$, $$$h_3 = 1 - 1 = 0$$$, $$$h_4 = 0 - 1 = -1$$$, $$$h_5 = 0$$$, $$$h_6 = 0 - 1 = -1$$$, $$$h_7 = 0$$$. The second case of the first test: All people have already started in bad mood in the capitalxa0— this is the only possible scenario. The first case of the second test: The second case of the second test: It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
1,800
true
true
false
false
false
false
false
false
false
false
3,731
1996E
Problem - 1996E - 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 data structures implementation math *1600 No tag edit access → Contest materials Editorial") $$$ $$$(1 leq l leq r leq n)$$$, count the number of pairs $$$(x, y)$$$ $$$(l leq x leq y leq r)$$$ such that the amount of $$$mathtt{0}$$$ equals the amount of $$$mathtt{1}$$$ in the substring $$$s_xs_{x+1}...s_y$$$. Output the sum of counts over all possible $$$(l, r)$$$ modulo $$$10^9+7$$$. Input The first line contains $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. Each test case contains a binary string $$$s$$$ ($$$1 leq s leq 2 cdot 10^5$$$). It is guaranteed $$$s$$$ only contains characters $$$mathtt{0}$$$ and $$$mathtt{1}$$$. It is guaranteed the sum of $$$s$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output an integer, the answer modulo $$$10^9+7$$$. Example Input 4 0000 01010101 1100111001 11000000111 Output 0 130 147 70
1,600
true
false
true
false
true
false
false
false
false
false
286
1105A
Salem gave you $$$n$$$ sticks with integer positive lengths $$$a_1, a_2, ldots, a_n$$$. For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from $$$a$$$ to $$$b$$$ is $$$a - b$$$, where $$$x$$$ means the absolute value of $$$x$$$. A stick length $$$a_i$$$ is called almost good for some integer $$$t$$$ if $$$a_i - t le 1$$$. Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer $$$t$$$ and the total cost of changing is minimum possible. The value of $$$t$$$ is not fixed in advance and you can choose it as any positive integer. As an answer, print the value of $$$t$$$ and the minimum cost. If there are multiple optimal choices for $$$t$$$, print any of them. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 1000$$$)xa0— the number of sticks. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le 100$$$)xa0— the lengths of the sticks. Output Print the value of $$$t$$$ and the minimum possible cost. If there are multiple optimal choices for $$$t$$$, print any of them. Note In the first example, we can change $$$1$$$ into $$$2$$$ and $$$10$$$ into $$$4$$$ with cost $$$1 - 2 + 10 - 4 = 1 + 6 = 7$$$ and the resulting lengths $$$[2, 4, 4]$$$ are almost good for $$$t = 3$$$. In the second example, the sticks lengths are already almost good for $$$t = 2$$$, so we don't have to do anything.
1,100
false
false
true
false
false
false
true
false
false
false
5,180
1095C
Problem - 1095C - 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 greedy *1400 No tag edit access → Contest materials ") Editorial") . Output If it is impossible to represent $$$n$$$ as the sum of $$$k$$$ powers of two, print NO. Otherwise, print YES, and then print $$$k$$$ positive integers $$$b_1, b_2, dots, b_k$$$ such that each of $$$b_i$$$ is a power of two, and $$$sum limits_{i = 1}^{k} b_i = n$$$. If there are multiple answers, you may print any of them. Examples Input 9 4 Output YES 1 2 2 4 Input 8 1 Output YES 8 Input 5 1 Output NO Input 3 7 Output NO
1,400
false
true
false
false
false
false
false
false
false
false
5,235
2000F
You have $$$n$$$ rectangles, the $$$i$$$-th of which has a width of $$$a_i$$$ and a height of $$$b_i$$$. You can perform the following operation an unlimited number of times: choose a rectangle and a cell in it, and then color it. Each time you completely color any row or column, you earn $$$1$$$ point. Your task is to score at least $$$k$$$ points with as few operations as possible. Suppose you have a rectangle with a width of $$$6$$$ and a height of $$$3$$$. You can score $$$4$$$ points by coloring all the cells in any $$$4$$$ columns, thus performing $$$12$$$ operations. Input The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The following are the descriptions of the test cases. The first line of each test case description contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 1000, 1 le k le 100$$$) — the number of rectangles in the case and the required number of points. The next $$$n$$$ lines contain the descriptions of the rectangles. The $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le 100$$$) — the width and height of the $$$i$$$-th rectangle. It is guaranteed that the sum of the values of $$$n$$$ across all test cases does not exceed $$$1000$$$. Output For each test case, output a single integer — the minimum number of operations required to score at least $$$k$$$ points. If it is impossible to score at least $$$k$$$ points, output -1. Example Input 7 1 4 6 3 1 5 4 4 5 10 1 1 1 1 1 1 1 1 1 1 2 100 1 2 5 6 3 11 2 2 3 3 4 4 3 25 9 2 4 3 8 10 4 18 5 4 8 5 8 3 6 2 Output 12 14 5 -1 17 80 35
1,900
true
true
true
true
false
false
false
false
false
false
258
34C
«Bersoft» company is working on a new version of its most popular text editor — Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates them with a comma, without spaces). Your task is to write a part of the program, responsible for «standardization» of this sequence. Your program gets the sequence, keyed by the user, as input. The program should output this sequence in format _l_1-_r_1,_l_2-_r_2,...,_l__k_-_r__k_, where _r__i_u2009+u20091u2009<u2009_l__i_u2009+u20091 for all _i_ from 1 to _k_u2009-u20091, and _l__i_u2009≤u2009_r__i_. The new sequence should contain all the page numbers, keyed by the user, and nothing else. If some page number appears in the input sequence several times, its appearances, starting from the second one, should be ignored. If for some element _i_ from the new sequence _l__i_u2009=u2009_r__i_, this element should be output as _l__i_, and not as «_l__i_u2009-u2009_l__i_». For example, sequence 1,2,3,1,1,2,6,6,2 should be output as 1-3,6. Input The only line contains the sequence, keyed by the user. The sequence contains at least one and at most 100 positive integer numbers. It's guaranteed, that this sequence consists of positive integer numbers, not exceeding 1000, separated with a comma, doesn't contain any other characters, apart from digits and commas, can't end with a comma, and the numbers don't contain leading zeroes. Also it doesn't start with a comma or contain more than one comma in a row.
1,300
false
false
true
false
false
false
false
false
true
false
9,826
1156D
Problem - 1156D - 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 dfs and similar divide and conquer dp dsu trees *2200 No tag edit access → Contest materials consisting of $$$n$$$ vertices and $$$n - 1$$$ edges. A number is written on each edge, each number is either $$$0$$$ (let's call such edges $$$0$$$-edges) or $$$1$$$ (those are $$$1$$$-edges). Let's call an ordered pair of vertices $$$(x, y)$$$ ($$$x e y$$$) valid if, while traversing the simple path from $$$x$$$ to $$$y$$$, we never go through a $$$0$$$-edge after going through a $$$1$$$-edge. Your task is to calculate the number of valid pairs in the tree. Input The first line contains one integer $$$n$$$ ($$$2 le n le 200000$$$) — the number of vertices in the tree. Then $$$n - 1$$$ lines follow, each denoting an edge of the tree. Each edge is represented by three integers $$$x_i$$$, $$$y_i$$$ and $$$c_i$$$ ($$$1 le x_i, y_i le n$$$, $$$0 le c_i le 1$$$, $$$x_i e y_i$$$) — the vertices connected by this edge and the number written on it, respectively. It is guaranteed that the given edges form a tree. Output Print one integer — the number of valid pairs of vertices. Example Input 7 2 1 1 3 2 0 4 2 1 5 2 0 6 7 1 7 2 1 Output 34 Note The picture corresponding to the first example:
2,200
false
false
false
true
false
false
false
false
false
false
4,927
1690E
A batch of $$$n$$$ goods ($$$n$$$xa0— an even number) is brought to the store, $$$i$$$-th of which has weight $$$a_i$$$. Before selling the goods, they must be packed into packages. After packing, the following will be done: There will be $$$frac{n}{2}$$$ packages, each package contains exactly two goods; The weight of the package that contains goods with indices $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$) is $$$a_i + a_j$$$. With this, the cost of a package of weight $$$x$$$ is always $$$left lfloorfrac{x}{k} ight floor$$$ burles (rounded down), where $$$k$$$xa0— a fixed and given value. Pack the goods to the packages so that the revenue from their sale is maximized. In other words, make such $$$frac{n}{2}$$$ pairs of given goods that the sum of the values $$$left lfloorfrac{x_i}{k} ight floor$$$, where $$$x_i$$$ is the weight of the package number $$$i$$$ ($$$1 le i le frac{n}{2}$$$), is maximal. For example, let $$$n = 6, k = 3$$$, weights of goods $$$a = [3, 2, 7, 1, 4, 8]$$$. Let's pack them into the following packages. In the first package we will put the third and sixth goods. Its weight will be $$$a_3 + a_6 = 7 + 8 = 15$$$. The cost of the package will be $$$left lfloorfrac{15}{3} ight floor = 5$$$ burles. In the second package put the first and fifth goods, the weight is $$$a_1 + a_5 = 3 + 4 = 7$$$. The cost of the package is $$$left lfloorfrac{7}{3} ight floor = 2$$$ burles. In the third package put the second and fourth goods, the weight is $$$a_2 + a_4 = 2 + 1 = 3$$$. The cost of the package is $$$left lfloorfrac{3}{3} ight floor = 1$$$ burle. With this packing, the total cost of all packs would be $$$5 + 2 + 1 = 8$$$ burles. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0—the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ ($$$2 le n le 2cdot10^5$$$) and $$$k$$$ ($$$1 le k le 1000$$$). The number $$$n$$$xa0— is even. The second line of each test case contains exactly $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all the test cases does not exceed $$$2cdot10^5$$$. Note The first test case is analyzed in the statement. In the second test case, you can get a total value equal to $$$4$$$ if you put the first and second goods in the first package and the third and fourth goods in the second package. In the third test case, the cost of each item is $$$0$$$, so the total cost will also be $$$0$$$.
1,500
true
true
false
false
false
false
false
true
false
false
2,139
1044F
Let $$$T$$$ be a tree on $$$n$$$ vertices. Consider a graph $$$G_0$$$, initially equal to $$$T$$$. You are given a sequence of $$$q$$$ updates, where the $$$i$$$-th update is given as a pair of two distinct integers $$$u_i$$$ and $$$v_i$$$. For every $$$i$$$ from $$$1$$$ to $$$q$$$, we define the graph $$$G_i$$$ as follows: If $$$G_{i-1}$$$ contains an edge $$${u_i, v_i}$$$, then remove this edge to form $$$G_i$$$. Otherwise, add this edge to $$$G_{i-1}$$$ to form $$$G_i$$$. Formally, $$$G_i := G_{i-1} riangle {{u_i, v_i}}$$$ where $$$ riangle$$$ denotes the set form a spanning tree of the graph $$$H$$$. This spanning tree is not generally fixed for a particular graphxa0— it depends on the starting vertex, and on the order in which the neighbors of each vertex are traversed. We call vertex $$$w$$$ good if one can order the neighbors of each vertex in such a way that the depth-first search started from $$$w$$$ produces $$$T$$$ as the spanning tree. For every $$$i$$$ from $$$1$$$ to $$$q$$$, find and report the number of good vertices. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$3 le n le 2cdot 10^5$$$, $$$1 le q le 2 cdot 10^5$$$)xa0— the number of nodes and the number of updates, respectively. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$)xa0— vertices connected by an edge in $$$T$$$. It is guaranteed that this graph is a tree. Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$)xa0— the endpoints of the edge that is added or removed. It is guaranteed that this edge does not belong to $$$T$$$. Output For each update, print one integer $$$k$$$xa0— the number of good vertices $$$w$$$ after the corresponding update. Examples Input 6 6 1 2 2 3 1 4 4 5 1 6 2 5 3 4 5 2 6 4 3 4 6 5 Note The first sample is depicted in the following figure. After the first update, $$$G$$$ contains all three possible edges. The result of a DFS is as follows: Let the starting vertex be $$$1$$$. We have two choices of ordering the neighbors of $$$1$$$, either $$$[2, 3]$$$ or $$$[3, 2]$$$. If we choose the former, then we reach vertex $$$2$$$. Regardless of the ordering of its neighbors, the next visited vertex will be $$$3$$$. Thus, the spanning tree generated by this DFS will contain edges $$${1, 2}$$$ and $$${2, 3}$$$, which does not equal to $$$T$$$. If we choose the latter, we obtain a spanning tree with edges $$${1, 3}$$$ and $$${2, 3}$$$.Hence, there is no way of ordering the neighbors of vertices such that the DFS produces $$$T$$$, and subsequently $$$1$$$ is not a good vertex. Let the starting vertex be $$$2$$$. We have two choices of traversing its neighbors. If we visit $$$3$$$ first, then the spanning tree will consist of edges $$${2,3}$$$ and $$${1,3}$$$, which is not equal to $$$T$$$. If we, however, visit $$$1$$$ first, then we can only continue to $$$3$$$ from here, and the spanning tree will consist of edges $$${1, 2}$$$ and $$${1,3}$$$, which equals to $$$T$$$. Hence, $$$2$$$ is a good vertex. The case when we start in the vertex $$$3$$$ is symmetrical to starting in $$$2$$$, and hence $$$3$$$ is a good vertex. Therefore, the answer is $$$2$$$. After the second update, the edge between $$$2$$$ and $$$3$$$ is removed, and $$$G = T$$$. It follows that the spanning tree generated by DFS will be always equal to $$$T$$$ independent of the choice of the starting vertex. Thus, the answer is $$$3$$$. In the second sample, the set of good vertices after the corresponding query is: $$${2, 3, 5}$$$ $$${3, 5}$$$ $$${3, 4, 5}$$$ $$${4, 5}$$$ $$${4, 5, 6}$$$ $$${5, 6}$$$
2,700
false
false
false
false
true
false
false
false
false
false
5,471
303B
You are given a rectangle grid. That grid's size is _n_u2009×u2009_m_. Let's denote the coordinate system on the grid. So, each point on the grid will have coordinates — a pair of integers (_x_,u2009_y_) (0u2009≤u2009_x_u2009≤u2009_n_,u20090u2009≤u2009_y_u2009≤u2009_m_). Your task is to find a maximum sub-rectangle on the grid (_x_1,u2009_y_1,u2009_x_2,u2009_y_2) so that it contains the given point (_x_,u2009_y_), and its length-width ratio is exactly (_a_,u2009_b_). In other words the following conditions must hold: 0u2009≤u2009_x_1u2009≤u2009_x_u2009≤u2009_x_2u2009≤u2009_n_, 0u2009≤u2009_y_1u2009≤u2009_y_u2009≤u2009_y_2u2009≤u2009_m_, . The sides of this sub-rectangle should be parallel to the axes. And values _x_1,u2009_y_1,u2009_x_2,u2009_y_2 should be integers. If there are multiple solutions, find the rectangle which is closest to (_x_,u2009_y_). Here "closest" means the Euclid distance between (_x_,u2009_y_) and the center of the rectangle is as small as possible. If there are still multiple solutions, find the lexicographically minimum one. Here "lexicographically minimum" means that we should consider the sub-rectangle as sequence of integers (_x_1,u2009_y_1,u2009_x_2,u2009_y_2), so we can choose the lexicographically minimum one. Input The first line contains six integers _n_,u2009_m_,u2009_x_,u2009_y_,u2009_a_,u2009_b_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009109,u20090u2009≤u2009_x_u2009≤u2009_n_,u20090u2009≤u2009_y_u2009≤u2009_m_,u20091u2009≤u2009_a_u2009≤u2009_n_,u20091u2009≤u2009_b_u2009≤u2009_m_). Output Print four integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2, which represent the founded sub-rectangle whose left-bottom point is (_x_1,u2009_y_1) and right-up point is (_x_2,u2009_y_2).
1,700
true
false
true
false
false
false
false
false
false
false
8,622
1904F
Lunchbox has a tree of size $$$n$$$ rooted at node $$$1$$$. Each node is then assigned a value. Lunchbox considers the tree to be beautiful if each value is distinct and ranges from $$$1$$$ to $$$n$$$. In addition, a beautiful tree must also satisfy $$$m$$$ requirements of $$$2$$$ types: "1 a b c"xa0— The node with the smallest value on the path between nodes $$$a$$$ and $$$b$$$ must be located at $$$c$$$. "2 a b c"xa0— The node with the largest value on the path between nodes $$$a$$$ and $$$b$$$ must be located at $$$c$$$. Now, you must assign values to each node such that the resulting tree is beautiful. If it is impossible to do so, output $$$-1$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 2 cdot 10^5$$$). The next $$$n - 1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n, u e v$$$)xa0— denoting an edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree. The next $$$m$$$ lines each contain four integers $$$t$$$, $$$a$$$, $$$b$$$, and $$$c$$$ ($$$t in {1,2}$$$, $$$1 le a, b, c le n$$$). It is guaranteed that node $$$c$$$ is on the path between nodes $$$a$$$ and $$$b$$$. Output If it is impossible to assign values such that the tree is beautiful, output $$$-1$$$. Otherwise, output $$$n$$$ integers, the $$$i$$$-th of which denotes the value of node $$$i$$$. Examples Input 7 5 1 2 1 3 1 4 3 5 4 6 3 7 1 6 5 1 2 6 7 3 1 2 7 1 1 7 5 7 2 4 2 2 Input 2 2 1 2 1 1 2 1 1 1 2 2
2,800
false
false
true
false
true
false
false
false
false
true
884
66A
Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer _n_?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from u2009-u2009128 to 127 2) short occupies 2 bytes and allows you to store numbers from u2009-u200932768 to 32767 3) int occupies 4 bytes and allows you to store numbers from u2009-u20092147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from u2009-u20099223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer _n_. Since BigInteger works much slower, Peter regards it last. Help him. Input The first line contains a positive number _n_. It consists of no more than 100 digits and doesn't contain any leading zeros. The number _n_ can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Output Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number _n_, in accordance with the data given above.
1,300
false
false
true
false
false
false
false
false
false
false
9,641
808C
Polycarp invited all his friends to the tea party to celebrate the holiday. He has _n_ cups, one for each of his _n_ friends, with volumes _a_1,u2009_a_2,u2009...,u2009_a__n_. His teapot stores _w_ milliliters of tea (_w_u2009≤u2009_a_1u2009+u2009_a_2u2009+u2009...u2009+u2009_a__n_). Polycarp wants to pour tea in cups in such a way that: Every cup will contain tea for at least half of its volume Every cup will contain integer number of milliliters of tea All the tea from the teapot will be poured into cups All friends will be satisfied. Friend with cup _i_ won't be satisfied, if there exists such cup _j_ that cup _i_ contains less tea than cup _j_ but _a__i_u2009>u2009_a__j_. For each cup output how many milliliters of tea should be poured in it. If it's impossible to pour all the tea and satisfy all conditions then output -1. Input The first line contains two integer numbers _n_ and _w_ (1u2009≤u2009_n_u2009≤u2009100, ). The second line contains _n_ numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009100). Output Output how many milliliters of tea every cup should contain. If there are multiple answers, print any of them. If it's impossible to pour all the tea and satisfy all conditions then output -1. Note In the third example you should pour to the first cup at least 5 milliliters, to the second one at least 4, to the third one at least 5. It sums up to 14, which is greater than 10 milliliters available.
1,400
false
true
false
false
false
true
false
false
true
false
6,533
1328A
Problem - 1328A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *800 No tag edit access → Contest materials ") . Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^9$$$). Output For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. Example Input 5 10 4 13 9 100 13 123 456 92 46 Output 2 5 4 333 0
800
true
false
false
false
false
false
false
false
false
false
4,071
877C
Slava plays his favorite game "Peace Lightning". Now he is flying a bomber on a very specific map. Formally, map is a checkered field of size 1u2009×u2009_n_, the cells of which are numbered from 1 to _n_, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he flies very high, but he can drop a bomb in any cell. All tanks in this cell will be damaged. If a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell _n_ can only move to the cell _n_u2009-u20091, a tank in the cell 1 can only move to the cell 2). If a tank takes damage for the second time, it's counted as destroyed and never moves again. The tanks move only when they are damaged for the first time, they do not move by themselves. Help Slava to destroy all tanks using as few bombs as possible. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009100u2009000) — the size of the map. Output In the first line print _m_ — the minimum number of bombs Slava needs to destroy all tanks. In the second line print _m_ integers _k_1,u2009_k_2,u2009...,u2009_k__m_. The number _k__i_ means that the _i_-th bomb should be dropped at the cell _k__i_. If there are multiple answers, you can print any of them.
1,600
false
false
false
false
false
true
false
false
false
false
6,238
379D
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas). Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, _s_1 anf _s_2, consisting of uppercase English letters. Then the boy makes string _s__k_, using a recurrent equation _s__n_u2009=u2009_s__n_u2009-u20092u2009+u2009_s__n_u2009-u20091, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string _s__k_ on a piece of paper, puts it in the envelope and sends in to Santa. Vasya is absolutely sure that Santa will bring him the best present if the resulting string _s__k_ has exactly _x_ occurrences of substring AC (the short-cut reminds him оf accepted problems). Besides, Vasya decided that string _s_1 should have length _n_, and string _s_2 should have length _m_. Vasya hasn't decided anything else. At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, _s_1 and _s_2 in the required manner. Help Vasya. Input The first line contains four integers _k_,u2009_x_,u2009_n_,u2009_m_ (3u2009≤u2009_k_u2009≤u200950;xa00u2009≤u2009_x_u2009≤u2009109;xa01u2009≤u2009_n_,u2009_m_u2009≤u2009100). Output In the first line print string _s_1, consisting of _n_ uppercase English letters. In the second line print string _s_2, consisting of _m_ uppercase English letters. If there are multiple valid strings, print any of them. If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
2,000
false
false
false
true
false
false
true
false
false
false
8,314
10D
This problem differs from one which was on the online contest. The sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ is called increasing, if _a__i_u2009<u2009_a__i_u2009+u20091 for _i_u2009<u2009_n_. The sequence _s_1,u2009_s_2,u2009...,u2009_s__k_ is called the subsequence of the sequence _a_1,u2009_a_2,u2009...,u2009_a__n_, if there exist such a set of indexes 1u2009≤u2009_i_1u2009<u2009_i_2u2009<u2009...u2009<u2009_i__k_u2009≤u2009_n_ that _a__i__j_u2009=u2009_s__j_. In other words, the sequence _s_ can be derived from the sequence _a_ by crossing out some elements. You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009500) — the length of the first sequence. The second line contains _n_ space-separated integers from the range [0,u2009109] — elements of the first sequence. The third line contains an integer _m_ (1u2009≤u2009_m_u2009≤u2009500) — the length of the second sequence. The fourth line contains _m_ space-separated integers from the range [0,u2009109] — elements of the second sequence. Output In the first line output _k_ — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any. Examples Input 7 2 3 1 6 5 4 6 4 1 3 5 6
2,800
false
false
false
true
false
false
false
false
false
false
9,942
245G
Polycarpus works as a programmer in a start-up social network. His boss gave his a task to develop a mechanism for determining suggested friends. Polycarpus thought much about the task and came to the folowing conclusion. Let's say that all friendship relationships in a social network are given as _m_ username pairs _a__i_,u2009_b__i_ (_a__i_u2009≠u2009_b__i_). Each pair _a__i_,u2009_b__i_ means that users _a__i_ and _b__i_ are friends. Friendship is symmetric, that is, if _a__i_ is friends with _b__i_, then _b__i_ is also friends with _a__i_. User _y_ is a suggested friend for user _x_, if the following conditions are met: 1. _x_u2009≠u2009_y_; 2. _x_ and _y_ aren't friends; 3. among all network users who meet the first two conditions, user _y_ has most of all common friends with user _x_. User _z_ is a common friend of user _x_ and user _y_ (_z_u2009≠u2009_x_,u2009_z_u2009≠u2009_y_), if _x_ and _z_ are friends, and _y_ and _z_ are also friends. Your task is to help Polycarpus to implement a mechanism for determining suggested friends. Input The first line contains a single integer _m_ (1u2009≤u2009_m_u2009≤u20095000) — the number of pairs of friends in the social network. Next _m_ lines contain pairs of names of the users who are friends with each other. The _i_-th line contains two space-separated names _a__i_ and _b__i_ (_a__i_u2009≠u2009_b__i_). The users' names are non-empty and consist of at most 20 uppercase and lowercase English letters. It is guaranteed that each pair of friends occurs only once in the input. For example, the input can't contain _x_, _y_ and _y_, _x_ at the same time. It is guaranteed that distinct users have distinct names. It is guaranteed that each social network user has at least one friend. The last thing guarantees that each username occurs at least once in the input. Output In the first line print a single integer _n_ — the number of network users. In next _n_ lines print the number of suggested friends for each user. In the _i_-th line print the name of the user _c__i_ and the number of his suggested friends _d__i_ after a space. You can print information about the users in any order. Examples Input 5 Mike Gerald Kate Mike Kate Tank Gerald Tank Gerald David Output 5 Mike 1 Gerald 1 Kate 1 Tank 1 David 2 Input 4 valera vanya valera edik pasha valera igor valera Output 5 valera 0 vanya 3 edik 3 pasha 3 igor 3 Note In the first test case consider user David. Users Mike and Tank have one common friend (Gerald) with David. User Kate has no common friends with David. That's why David's suggested friends are users Mike and Tank.
2,200
false
false
false
false
false
false
true
false
false
true
8,850
1730E
You are given an array $$$a_1, a_2, ldots, a_n$$$ of positive integers. Find the number of pairs of indices $$$(l, r)$$$, where $$$1 le l le r le n$$$, that pass the check. The check is performed in the following manner: 1. The minimum and maximum numbers are found among $$$a_l, a_{l+1}, ldots, a_r$$$. 2. The check is passed if the maximum number is divisible by the minimum number. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10$$$) — the number of test cases. Then the test cases follow. Each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$) — the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, print a single integer — the number of pairs of indices that pass the check. Example Input 6 1 1 2 2 4 2 2 3 4 2 4 7 14 7 16 5 18 7 7 12 14 6 16 14 2 6 16 2 Note Below $$$x mid y$$$ denotes that $$$y$$$ is divisible by $$$x$$$. In the first test case, there is one pair $$$(1, 1)$$$, the maximum for this pair is $$$1$$$, the minimum is also $$$1$$$, $$$1 mid 1$$$, so the check is passed, and the answer is $$$1$$$. In the second test case, there are $$$3$$$ segments: $$$(1, 1)$$$: the maximum is $$$2$$$, the minimum is $$$2$$$, $$$2 mid 2$$$, so the check is passed. $$$(1, 2)$$$: the maximum is $$$4$$$, the minimum is $$$2$$$, $$$2 mid 4$$$, so the check is passed. $$$(2, 2)$$$: the maximum is $$$4$$$, the minimum is $$$4$$$, $$$4 mid 4$$$, so the check is passed. In the third test case, there are $$$3$$$ segments: $$$(1, 1)$$$: the maximum is $$$2$$$, the minimum is $$$2$$$, $$$2 mid 2$$$, so the check is passed. $$$(1, 2)$$$: the maximum is $$$3$$$, the minimum is $$$2$$$, $$$3$$$ isn't divisible by $$$2$$$, so the check is failed. $$$(2, 2)$$$: the maximum is $$$3$$$, the minimum is $$$3$$$, $$$3 mid 3$$$, so the check is passed.
2,700
false
false
false
false
true
false
false
false
false
false
1,907
152A
Vasya, or Mr. Vasily Petrov is a dean of a department in a local university. After the winter exams he got his hands on a group's gradebook. Overall the group has _n_ students. They received marks for _m_ subjects. Each student got a mark from 1 to 9 (inclusive) for each subject. Let's consider a student the best at some subject, if there is no student who got a higher mark for this subject. Let's consider a student successful, if there exists a subject he is the best at. Your task is to find the number of successful students in the group. Input The first input line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100) — the number of students and the number of subjects, correspondingly. Next _n_ lines each containing _m_ characters describe the gradebook. Each character in the gradebook is a number from 1 to 9. Note that the marks in a rows are not sepatated by spaces. Output Print the single number — the number of successful students in the given group. Note In the first sample test the student number 1 is the best at subjects 1 and 3, student 2 is the best at subjects 1 and 2, but student 3 isn't the best at any subject. In the second sample test each student is the best at at least one subject.
900
false
false
true
false
false
false
false
false
false
false
9,269
1203F1
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $$$r$$$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $$$i$$$-th project, Polycarp needs to have at least $$$a_i$$$ units of rating; after he completes this project, his rating will change by $$$b_i$$$ (his rating will increase or decrease by $$$b_i$$$) ($$$b_i$$$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Is it possible to complete all the projects? Formally, write a program to check if such an order of the projects exists, that Polycarp has enough rating before starting each project, and he has non-negative rating after completing each project. In other words, you have to check that there exists such an order of projects in which Polycarp will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Input The first line of the input contains two integers $$$n$$$ and $$$r$$$ ($$$1 le n le 100, 1 le r le 30000$$$) — the number of projects and the initial rating of Polycarp, respectively. The next $$$n$$$ lines contain projects, one per line. The $$$i$$$-th project is represented as a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i le 30000$$$, $$$-300 le b_i le 300$$$) — the rating required to complete the $$$i$$$-th project and the rating change after the project completion. Output Print "YES" or "NO". Examples Input 4 4 5 2 5 -3 2 1 4 -2 Input 3 10 10 0 10 -10 30 0 Note In the first example, the possible order is: $$$1, 2, 3$$$. In the second example, the possible order is: $$$2, 3, 1$$$. In the third example, the possible order is: $$$3, 1, 4, 2$$$.
2,100
false
true
false
false
false
false
false
false
false
false
4,677
1608D
You are given $$$n$$$ dominoes. Each domino has a left and a right cell. Each cell can be colored either black or white. Some cells are already colored, while some aren't yet. The coloring is said to be valid if and only if it is possible to rearrange the dominoes in some order such that for each $$$1 le i le n$$$ the color of the right cell of the $$$i$$$-th domino is different from the color of the left cell of the $$$((i bmod n)+1)$$$-st domino. Note that you can't rotate the dominoes, so the left cell always remains the left cell, and the right cell always remains the right cell. Count the number of valid ways to color the yet uncolored cells of dominoes. Two ways are considered different if there is a cell that is colored white in one way and black in the other. In particular, colorings BW WB and WB BW different (and both invalid). As this number can be very big, output it modulo $$$998,244,353$$$. Input The first line of the input contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the number of dominoes. The next $$$n$$$ lines describe dominoes. Each line contains two characters which represent the left and the right cell. Character B means that the corresponding cell is black, character W means that the corresponding cell is white, and ? means that the cell is yet to be colored. Output Print a single integer — the answer to the problem. Note In the first test case, there is only one domino, and we need the color of its right cell to be different from the color of its left cell. There is only one way to achieve this. In the second test case, there are only $$$2$$$ such colorings: BB WW and WB WB.
2,400
true
false
false
false
false
false
false
false
false
true
2,612
1950A
Problem - 1950A - 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 *800 No tag edit access → Contest materials ") Editorial") xa0— the number of test cases. The only line of each test case contains three digits $$$a$$$, $$$b$$$, $$$c$$$ ($$$0 leq a$$$, $$$b$$$, $$$c leq 9$$$). Output For each test case, output "STAIR" if the digits form a stair, "PEAK" if the digits form a peak, and "NONE" otherwise (output the strings without quotes). Example Input 7 1 2 3 3 2 1 1 5 3 3 4 1 0 0 0 4 1 7 4 5 7 Output STAIR NONE PEAK PEAK NONE NONE STAIR
800
false
false
true
false
false
false
false
false
false
false
581
1446D1
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array $$$[a_1, a_2, dots, a_n]$$$. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs $$$f$$$ times in this subarray, then at least $$$2$$$ different values should occur exactly $$$f$$$ times. An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 200,000$$$)xa0— the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le min(n, 100)$$$)xa0— elements of the array. Output You should output exactly one integer xa0— the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output $$$0$$$. Examples Input 10 1 1 1 5 4 1 3 1 2 2 Note In the first sample, the subarray $$$[1, 1, 2, 2, 3, 3]$$$ is good, but $$$[1, 1, 2, 2, 3, 3, 3]$$$ isn't: in the latter there are $$$3$$$ occurrences of number $$$3$$$, and no other element appears $$$3$$$ times.
2,600
false
true
false
false
true
false
false
false
false
false
3,444
1916D
The mathematicians of the 31st lyceum were given the following task: You are given an odd number $$$n$$$, and you need to find $$$n$$$ different numbers that are squares of integers. But it's not that simple. Each number should have a length of $$$n$$$ (and should not have leading zeros), and the multiset of digits of all the numbers should be the same. For example, for $$$mathtt{234}$$$ and $$$mathtt{432}$$$, and $$$mathtt{11223}$$$ and $$$mathtt{32211}$$$, the multisets of digits are the same, but for $$$mathtt{123}$$$ and $$$mathtt{112233}$$$, they are not. The mathematicians couldn't solve this problem. Can you? Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The following $$$t$$$ lines contain one odd integer $$$n$$$ ($$$1 leq n leq 99$$$)xa0— the number of numbers to be found and their length. It is guaranteed that the solution exists within the given constraints. It is guaranteed that the sum of $$$n^2$$$ does not exceed $$$10^5$$$. The numbers can be output in any order. Output For each test case, you need to output $$$n$$$ numbers of length $$$n$$$xa0— the answer to the problem. If there are several answers, print any of them. Example Output 1 169 196 961 16384 31684 36481 38416 43681 Note Below are the squares of the numbers that are the answers for the second test case: $$$mathtt{169}$$$ = $$$mathtt{13}^2$$$ $$$mathtt{196}$$$ = $$$mathtt{14}^2$$$ $$$mathtt{961}$$$ = $$$mathtt{31}^2$$$ Below are the squares of the numbers that are the answers for the third test case: $$$mathtt{16384}$$$ = $$$mathtt{128}^2$$$ $$$mathtt{31684}$$$ = $$$mathtt{178}^2$$$ $$$mathtt{36481}$$$ = $$$mathtt{191}^2$$$ $$$mathtt{38416}$$$ = $$$mathtt{196}^2$$$ $$$mathtt{43681}$$$ = $$$mathtt{209}^2$$$
1,700
true
false
false
false
false
true
true
false
false
false
800
1991D
You are given an undirected graph with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. There is an edge between vertices $$$u$$$ and $$$v$$$ if and only if $$$u oplus v$$$ is a . The description of test cases follows. The only line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of vertices in the graph. 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 two lines. The first line should contain a single integer $$$k$$$ ($$$1 le k le n$$$)xa0— the minimum number of colors required. The second line should contain $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$1 le c_i le k$$$)xa0— the color of each vertex. If there are multiple solutions, output any of them. Example Output 1 1 2 1 2 2 1 2 2 3 1 2 2 3 3 1 2 2 3 3 4 1 2 2 3 3 4 Note In the first test case, the minimum number of colors is $$$1$$$, because there is only one vertex. In the second test case, the minimum number of colors is $$$2$$$, because there is an edge connecting $$$1$$$ and $$$2$$$ ($$$1 oplus 2 = 3$$$, which is a prime number). In the third test case, the minimum number of colors is still $$$2$$$, because $$$2$$$ and $$$3$$$ can be colored the same since there is no edge between $$$2$$$ and $$$3$$$ ($$$2 oplus 3 = 1$$$, which is not a prime number). In the fourth test case, it can be shown that the minimum number of colors is $$$3$$$. In the fifth test case, it can be shown that the minimum number of colors is $$$3$$$. In the sixth test case, it can be shown that the minimum number of colors is $$$4$$$.
1,900
true
true
false
false
false
true
false
false
false
true
325
1385F
You are given a tree (connected graph without cycles) consisting of $$$n$$$ vertices. The tree is unrooted — it is just a connected undirected graph without cycles. In one move, you can choose exactly $$$k$$$ leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves $$$u_1, u_2, dots, u_k$$$ that there are edges $$$(u_1, v)$$$, $$$(u_2, v)$$$, $$$dots$$$, $$$(u_k, v)$$$ and remove these leaves and these edges. Your task is to find the maximum number of moves you can perform if you remove leaves optimally. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le k < n$$$) — the number of vertices in the tree and the number of leaves you remove in one move, respectively. The next $$$n-1$$$ lines describe edges. The $$$i$$$-th edge is represented as two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$), where $$$x_i$$$ and $$$y_i$$$ are vertices the $$$i$$$-th edge connects. It is guaranteed that the given set of edges forms a tree. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer — the maximum number of moves you can perform if you remove leaves optimally. Example Input 4 8 3 1 2 1 5 7 6 6 8 3 1 6 4 6 1 10 3 1 2 1 10 2 3 1 5 1 6 2 4 7 10 10 9 8 10 7 2 3 1 4 5 3 6 7 4 1 2 1 4 5 1 1 2 2 3 4 3 5 3 Note The picture corresponding to the first test case of the example: There you can remove vertices $$$2$$$, $$$5$$$ and $$$3$$$ during the first move and vertices $$$1$$$, $$$7$$$ and $$$4$$$ during the second move. The picture corresponding to the second test case of the example: There you can remove vertices $$$7$$$, $$$8$$$ and $$$9$$$ during the first move, then vertices $$$5$$$, $$$6$$$ and $$$10$$$ during the second move and vertices $$$1$$$, $$$3$$$ and $$$4$$$ during the third move. The picture corresponding to the third test case of the example: There you can remove vertices $$$5$$$ and $$$7$$$ during the first move, then vertices $$$2$$$ and $$$4$$$ during the second move and vertices $$$1$$$ and $$$6$$$ during the third move.
2,300
false
true
true
false
true
false
false
false
false
false
3,742
544B
A map of some object is a rectangular field consisting of _n_ rows and _n_ columns. Each cell is initially occupied by the sea but you can cover some some cells of the map with sand so that exactly _k_ islands appear on the map. We will call a set of sand cells to be island if it is possible to get from each of them to each of them by moving only through sand cells and by moving from a cell only to a side-adjacent cell. The cells are called to be side-adjacent if they share a vertical or horizontal side. It is easy to see that islands do not share cells (otherwise they together form a bigger island). Find a way to cover some cells with sand so that exactly _k_ islands appear on the _n_u2009×u2009_n_ map, or determine that no such way exists. Input The single line contains two positive integers _n_, _k_ (1u2009≤u2009_n_u2009≤u2009100, 0u2009≤u2009_k_u2009≤u2009_n_2) — the size of the map and the number of islands you should form. Output If the answer doesn't exist, print "NO" (without the quotes) in a single line. Otherwise, print "YES" in the first line. In the next _n_ lines print the description of the map. Each of the lines of the description must consist only of characters 'S' and 'L', where 'S' is a cell that is occupied by the sea and 'L' is the cell covered with sand. The length of each line of the description must equal _n_. If there are multiple answers, you may print any of them. You should not maximize the sizes of islands. Examples Output YES SSSSS LLLLL SSSSS LLLLL SSSSS
1,400
false
false
true
false
false
true
false
false
false
false
7,663
1846B
Problem - 1846B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation strings *800 No tag edit access → Contest materials ") — the number of test cases. Each test case consists of three lines, each of which consists of three characters. The symbol can be one of four: "X" means a cross, "O" means a nought, "+" means a plus, "." means an empty cell. Output For each test case, print the string "X" if the crosses won, "O" if the noughts won, "+" if the pluses won, "DRAW" if there was a draw. Example Input 5 +X+ OXO OX. O+. +OX X+O .XO OX. +++ O.+ X.O +.. .++ X.O +.. Output X O + DRAW DRAW
800
false
false
true
false
false
false
true
false
false
false
1,207
920B
Recently _n_ students from city S moved to city P to attend a programming camp. They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea. _i_-th student comes to the end of the queue at the beginning of _l__i_-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of _r__i_-th second student _i_ still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it). Input The first line contains one integer _t_ — the number of test cases to solve (1u2009≤u2009_t_u2009≤u20091000). Then _t_ test cases follow. The first line of each test case contains one integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of students. Then _n_ lines follow. Each line contains two integer _l__i_, _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u20095000) — the second _i_-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every condition _l__i_u2009-u20091u2009≤u2009_l__i_ holds. The sum of _n_ over all test cases doesn't exceed 1000. Note that in hacks you have to set _t_u2009=u20091. Output For each test case print _n_ integers. _i_-th of them must be equal to the second when _i_-th student gets his tea, or 0 if he leaves without tea. Note The example contains 2 tests: 1. During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. 2. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea.
1,200
false
false
true
false
false
false
false
false
false
false
6,049
1965E
There are $$$n cdot m$$$ unit cubes currently in positions $$$(1, 1, 1)$$$ through $$$(n, m, 1)$$$. Each of these cubes is one of $$$k$$$ colors. You want to add additional cubes at any integer coordinates such that the subset of cubes of each color is connected, where two cubes are considered connected if they share a face. In other words, for every pair of cubes of the same color $$$c$$$, it should be possible to travel from one to the other, moving only through cubes of color $$$c$$$ that share a face. The existing cubes are currently in the corner of a room. There are colorless cubes completely filling the planes $$$x = 0$$$, $$$y = 0$$$, and $$$z = 0$$$, preventing you from placing additional cubes there or at any negative coordinates. Find a solution that uses at most $$$4 cdot 10^5$$$ additional cubes (not including the cubes that are currently present), or determine that there is no solution. It can be shown that under the given constraints, if there is a solution, there is one using at most $$$4 cdot 10^5$$$ additional cubes. Input The first line of the input contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$2 le n, m, k le 50$$$)xa0— the number of rows and columns of cubes, and the number of colors, respectively. The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ integers. The $$$j$$$-th of these is $$$a_{ij}$$$ ($$$1 le a_{ij} le k$$$)xa0— the color of the cube at position $$$(i, j, 1)$$$. For every color from $$$1$$$ to $$$k$$$, it is guaranteed that there is at least one cube in the input of that color. Output If there is no solution, print a single integer $$$-1$$$. Otherwise, the first line of output should contain a single integer $$$p$$$ ($$$0 le p le 4 cdot 10^5$$$)xa0— the number of additional cubes you will add. The next $$$p$$$ lines should contain four integers $$$x$$$, $$$y$$$, $$$z$$$ and $$$c$$$ ($$$1 le x, y, z le 10^6$$$, $$$1 le c le k$$$)xa0— indicating that you are adding a cube with color $$$c$$$ at position $$$(x, y, z)$$$. No two cubes in the output should have the same coordinates, and no cube in the output should have the same coordinates as any cube in the input. If there are multiple solutions, print any. Examples Input 3 4 3 3 2 3 1 1 1 1 1 1 3 3 2 Output 13 1 1 2 3 1 3 2 3 2 1 2 3 2 2 2 3 2 3 2 3 3 3 2 3 1 2 2 2 1 2 3 2 1 3 3 2 1 4 3 2 2 4 3 2 3 4 3 2 3 4 2 2 Output 9 1 3 1 1 2 3 1 1 3 1 1 1 3 2 1 1 3 3 1 1 1 1 2 2 1 2 2 2 2 1 2 2 2 2 2 2 Note The image in the statement corresponds to the first example case, with $$$ ext{red} = 1$$$, $$$ ext{blue} = 2$$$, $$$ ext{green} = 3$$$.
3,100
false
false
false
false
false
true
false
false
false
false
512
1255C
Bob is an avid fan of the video game "League of Leesins", and today he celebrates as the League of Leesins World Championship comes to an end! The tournament consisted of $$$n$$$ ($$$n ge 5$$$) teams around the world. Before the tournament starts, Bob has made a prediction of the rankings of each team, from $$$1$$$-st to $$$n$$$-th. After the final, he compared the prediction with the actual result and found out that the $$$i$$$-th team according to his prediction ended up at the $$$p_i$$$-th position ($$$1 le p_i le n$$$, all $$$p_i$$$ are unique). In other words, $$$p$$$ is a permutation of $$$1, 2, dots, n$$$. As Bob's favorite League player is the famous "3ga", he decided to write down every $$$3$$$ consecutive elements of the permutation $$$p$$$. Formally, Bob created an array $$$q$$$ of $$$n-2$$$ triples, where $$$q_i = (p_i, p_{i+1}, p_{i+2})$$$ for each $$$1 le i le n-2$$$. Bob was very proud of his array, so he showed it to his friend Alice. After learning of Bob's array, Alice declared that she could retrieve the permutation $$$p$$$ even if Bob rearranges the elements of $$$q$$$ and the elements within each triple. Of course, Bob did not believe in such magic, so he did just the same as above to see Alice's respond. For example, if $$$n = 5$$$ and $$$p = [1, 4, 2, 3, 5]$$$, then the original array $$$q$$$ will be $$$[(1, 4, 2), (4, 2, 3), (2, 3, 5)]$$$. Bob can then rearrange the numbers within each triple and the positions of the triples to get $$$[(4, 3, 2), (2, 3, 5), (4, 1, 2)]$$$. Note that $$$[(1, 4, 2), (4, 2, 2), (3, 3, 5)]$$$ is not a valid rearrangement of $$$q$$$, as Bob is not allowed to swap numbers belong to different triples. As Alice's friend, you know for sure that Alice was just trying to show off, so you decided to save her some face by giving her any permutation $$$p$$$ that is consistent with the array $$$q$$$ she was given. Input The first line contains a single integer $$$n$$$ ($$$5 le n le 10^5$$$)xa0— the size of permutation $$$p$$$. The $$$i$$$-th of the next $$$n-2$$$ lines contains $$$3$$$ integers $$$q_{i, 1}$$$, $$$q_{i, 2}$$$, $$$q_{i, 3}$$$ ($$$1 le q_{i, j} le n$$$)xa0— the elements of the $$$i$$$-th triple of the rearranged (shuffled) array $$$q_i$$$, in random order. Remember, that the numbers within each triple can be rearranged and also the positions of the triples can be rearranged. It is guaranteed that there is at least one permutation $$$p$$$ that is consistent with the input.
1,600
false
false
true
false
false
true
false
false
false
false
4,408
461E
Appleman and Toastman like games. Today they play a game with strings with the following rules. Firstly Toastman tells Appleman two strings _s_ and _t_ both consisting only of letters 'A', 'B', 'C', 'D'. Then Appleman must build string _s_ as quickly as possible. Initially he has empty string, and in one second he can append to end of the current string any contiguous substring of _t_. Now, Toastman and Appleman are beginning to play the game. Toastman has already told string _t_ to Appleman, but he hasn't come up with string _s_ yet. Toastman only thinks, that he should choose string _s_ consisting of _n_ characters. Of course, he wants to find the worst string for Appleman (such string, that Appleman will spend as much time as possible during the game). Tell Toastman, how much time will Appleman spend during the game if Toastman finds the worst string for him. You can assume that Appleman plays optimally, therefore he builds any string _s_ in minimal possible time. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20091018). The second line contains string _t_ (1u2009≤u2009_t_u2009≤u2009105). String _t_ consists of only letters 'A', 'B', 'C', 'D'. Each letter appears at least once in string _t_. Output Print a single integer — the largest possible time Appleman needs. Examples Input 5 AAABACADBABBBCBDCACBCCCDDDBDCDD Note In the first example, Toastman can choose _s_ equal to "AAAAA". In the second example, Toastman can choose _s_ equal to "DADDA".
3,000
false
false
false
false
false
false
false
true
false
false
7,992
1541A
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering. For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 100$$$)xa0— the number of cats. It can be proven that under the constraints of the problem, an answer always exist. Output Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integersxa0— a permutation with the minimum total distance. If there are multiple answers, print any. Note For the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$. The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
800
false
true
true
false
false
true
false
false
false
false
2,936
383D
Iahub accidentally discovered a secret lab. He found there _n_ devices ordered in a line, numbered from 1 to _n_ from left to right. Each device _i_ (1u2009≤u2009_i_u2009≤u2009_n_) can create either _a__i_ units of matter or _a__i_ units of antimatter. Iahub wants to choose some contiguous subarray of devices in the lab, specify the production mode for each of them (produce matter or antimatter) and finally take a photo of it. However he will be successful only if the amounts of matter and antimatter produced in the selected subarray will be the same (otherwise there would be overflowing matter or antimatter in the photo). You are requested to compute the number of different ways Iahub can successful take a photo. A photo is different than another if it represents another subarray, or if at least one device of the subarray is set to produce matter in one of the photos and antimatter in the other one. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20091000). The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u20091000). The sum _a_1u2009+u2009_a_2u2009+u2009...u2009+u2009_a__n_ will be less than or equal to 10000. Note The possible photos are [1+, 2-], [1-, 2+], [2+, 3-], [2-, 3+], [3+, 4-], [3-, 4+], [1+, 2+, 3-, 4-], [1+, 2-, 3+, 4-], [1+, 2-, 3-, 4+], [1-, 2+, 3+, 4-], [1-, 2+, 3-, 4+] and [1-, 2-, 3+, 4+], where "_i_+" means that the _i_-th element produces matter, and "_i_-" means that the _i_-th element produces antimatter.
2,300
false
false
false
true
false
false
false
false
false
false
8,295
1882E1
This is the easy version of the problem. The difference between the two versions is that you do not have to minimize the number of operations in this version. You can make hacks only if both versions of the problem are solved. You have two permutations$$$^{dagger}$$$ $$$p_{1}, p_{2}, ldots, p_{n}$$$ (of integers $$$1$$$ to $$$n$$$) and $$$q_{1}, q_{2}, ldots, q_{m}$$$ (of integers $$$1$$$ to $$$m$$$). Initially $$$p_{i}=a_{i}$$$ for $$$i=1, 2, ldots, n$$$, and $$$q_{j} = b_{j}$$$ for $$$j = 1, 2, ldots, m$$$. You can apply the following operation on the permutations several (possibly, zero) times. In one operation, $$$p$$$ and $$$q$$$ will change according to the following three steps: You choose integers $$$i$$$, $$$j$$$ which satisfy $$$1 le i le n$$$ and $$$1 le j le m$$$. Permutation $$$p$$$ is partitioned into three parts using $$$p_i$$$ as a pivot: the left part is formed by elements $$$p_1, p_2, ldots, p_{i-1}$$$ (this part may be empty), the middle part is the single element $$$p_i$$$, and the right part is $$$p_{i+1}, p_{i+2}, ldots, p_n$$$ (this part may be empty). To proceed, swap the left and the right parts of this partition. Formally, after this step, $$$p$$$ will become $$$p_{i+1}, p_{i+2}, ldots, p_{n}, p_{i}, p_{1}, p_{2}, ldots, p_{i-1}$$$. The elements of the newly formed $$$p$$$ will be reindexed starting from $$$1$$$. Perform the same transformation on $$$q$$$ with index $$$j$$$. Formally, after this step, $$$q$$$ will become $$$q_{j+1}, q_{j+2}, ldots, q_{m}, q_{j}, q_{1}, q_{2}, ldots, q_{j-1}$$$. The elements of the newly formed $$$q$$$ will be reindexed starting from $$$1$$$. Your goal is to simultaneously make $$$p_{i}=i$$$ for $$$i=1, 2, ldots, n$$$, and $$$q_{j} = j$$$ for $$$j = 1, 2, ldots, m$$$. Find any valid way to achieve the goal using at most $$$10,000$$$ operations, or say that none exists. Please note that you do not have to minimize the number of operations. It can be proved that if it is possible to achieve the goal, then there exists a way to do so using at most $$$10,000$$$ operations. $$$^{dagger}$$$ A permutation of length $$$k$$$ is an array consisting of $$$k$$$ distinct integers from $$$1$$$ to $$$k$$$ 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 ($$$k=3$$$ but there is $$$4$$$ in the array). Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2500$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$1 le b_i le m$$$). It is guaranteed that $$$a$$$ and $$$b$$$ are permutations. Output If there is no solution, print a single integer $$$-1$$$. Otherwise, print an integer $$$k$$$ ($$$0 le k le 10,000$$$)xa0— the number of operations to perform, followed by $$$k$$$ lines, each containing two integers $$$i$$$ and $$$j$$$ ($$$1 le i le n$$$, $$$1 le j le m$$$)xa0— the integers chosen for the operation. If there are multiple solutions, print any of them. Please note that you do not have to minimize the number of operations.
2,400
false
true
false
false
false
true
true
false
false
false
993
41C
Sometimes one has to spell email addresses over the phone. Then one usually pronounces a dot as dot, an at sign as at. As a result, we get something like vasyaatgmaildotcom. Your task is to transform it into a proper email address (vasya@gmail.com). It is known that a proper email address contains only such symbols as . @ and lower-case Latin letters, doesn't start with and doesn't end with a dot. Also, a proper email address doesn't start with and doesn't end with an at sign. Moreover, an email address contains exactly one such symbol as @, yet may contain any number (possible, zero) of dots. You have to carry out a series of replacements so that the length of the result was as short as possible and it was a proper email address. If the lengths are equal, you should print the lexicographically minimal result. Overall, two variants of replacement are possible: dot can be replaced by a dot, at can be replaced by an at. Input The first line contains the email address description. It is guaranteed that that is a proper email address with all the dots replaced by dot an the at signs replaced by at. The line is not empty and its length does not exceed 100 symbols. Output Print the shortest email address, from which the given line could be made by the described above replacements. If there are several solutions to that problem, print the lexicographically minimal one (the lexicographical comparison of the lines are implemented with an operator < in modern programming languages). In the ASCII table the symbols go in this order: . @ ab...z
1,300
false
false
true
false
false
false
false
false
false
false
9,782
197B
Problem - 197B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *1400 No tag edit access → Contest materials u2009=u2009_a_0·_x__n_u2009+u2009_a_1·_x__n_u2009-u20091u2009+u2009...u2009+u2009_a__n_u2009-u20091·_x_u2009+u2009_a__n_ and _Q_(_x_)u2009=u2009_b_0·_x__m_u2009+u2009_b_1·_x__m_u2009-u20091u2009+u2009...u2009+u2009_b__m_u2009-u20091·_x_u2009+u2009_b__m_. Calculate limit . Input The first line contains two space-separated integers _n_ and _m_ (0u2009≤u2009_n_,u2009_m_u2009≤u2009100) — degrees of polynomials _P_(_x_) and _Q_(_x_) correspondingly. The second line contains _n_u2009+u20091 space-separated integers — the factors of polynomial _P_(_x_): _a_0, _a_1, ..., _a__n_u2009-u20091, _a__n_ (u2009-u2009100u2009≤u2009_a__i_u2009≤u2009100,u2009_a_0u2009≠u20090). The third line contains _m_u2009+u20091 space-separated integers — the factors of polynomial _Q_(_x_): _b_0, _b_1, ..., _b__m_u2009-u20091, _b__m_ (u2009-u2009100u2009≤u2009_b__i_u2009≤u2009100,u2009_b_0u2009≠u20090). Output If the limit equals u2009+u2009∞, print "Infinity" (without quotes). If the limit equals u2009-u2009∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where _p_ is the — numerator, _q_ (_q_u2009>u20090) is the denominator of the fraction. Examples Input 2 1 1 1 1 2 5 Output Infinity Input 1 0 -1 3 2 Output -Infinity Input 0 1 1 1 0 Output 0/1 Input 2 2 2 1 6 4 5 -7 Output 1/2 Input 1 1 9 0 -5 2 Output -9/5 Note Let's consider all samples: 1. 2. 3. 4. 5. You can learn more about the definition and properties of limits if you follow the link:
1,400
true
false
false
false
false
false
false
false
false
false
9,052
1371E2
# Asterism (Hard Version) Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes This is the hard version of the problem. The difference between versions is the constraints on n and ai. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has ai candies. Yuzu is going to determine a permutation P . 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 (because n = 3 but there is the number 4 in the array). After that, she will do n duels with the enemies with the following rules: • If Yuzu has equal or more number of candies than enemy Pi, she wins the duel and gets 1 candy .Otherwise, she loses the duel and gets nothing. • The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels . How many valid permutations P exist? This problem was easy and wasn’t interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let’s define f (x) as the number of valid permutations for the integer x.You are given n, a and a prime number p ≤ n. Let’s call a positive integer x good , if the value f (x) is not divisible by p. Find all good integers x.Your task is to solve this problem made by Akari. # Input The first line contains two integers n, p (2 ≤ p ≤ n ≤ 10 5). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p). The second line contains n integers a1, a 2, . . . , a n (1 ≤ ai ≤ 10 9). # Output In the first line, print the number of good integers x.In the second line, output all good integers x in the ascending order .It is guaranteed that the number of good integers x does not exceed 10 5. Page 1 of 2 Examples standard input standard output 3 2 3 4 5 134 3 2 3 5 6 23 4 4 3 9 1 1 1 03 2 1000000000 1 999999999 1999999998 # Note In the first test, p = 2 . • If x ≤ 2, there are no valid permutations for Yuzu. So f (x) = 0 for all x ≤ 2. The number 0 is divisible by 2, so all integers x ≤ 2 are not good. • If x = 3 , {1, 2, 3} is the only valid permutation for Yuzu. So f (3) = 1 , so the number 3 is good. • If x = 4 , {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1} are all valid permutations for Yuzu. So f (4) = 4 , so the number 4 is not good. • If x ≥ 5, all 6 permutations are valid for Yuzu. So f (x) = 6 for all x ≥ 5, so all integers x ≥ 5 are not good. So, the only good number is 3.In the third test, for all positive integers x the value f (x) is divisible by p = 3 . Page 2 of 2
2,300
true
false
false
true
false
false
false
true
true
false
3,809