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
1840G1
The only difference between easy and hard versions is the maximum number of queries. In this version, you are allowed to ask at most $$$2023$$$ queries. This is an interactive problem. You are playing a game. The circle is divided into $$$n$$$ sectors, sectors are numbered from $$$1$$$ to $$$n$$$ in some order. You are in the adjacent room and do not know either the number of sectors or their numbers. There is also an arrow that initially points to some sector. Initially, the host tells you the number of the sector to which the arrow points. After that, you can ask the host to move the arrow $$$k$$$ sectors counterclockwise or clockwise at most $$$2023$$$ times. And each time you are told the number of the sector to which the arrow points. Your task is to determine the integer $$$n$$$xa0β€” the number of sectors in at most $$$2023$$$ queries. It is guaranteed that $$$1 le n le 10^6$$$. Input The input consists of a single integer $$$x$$$ ($$$1 le x le n$$$)xa0β€” the number of the initial sector. Output After you determine the integer $$$n$$$xa0β€” the number of sectors, you should output "! n" ($$$1 le n le 10^6$$$). After that the program should immediately terminate. Note that, printing the answer does not count as a query. It is guaranteed that the integer $$$n$$$ and the numbers of the sectors are fixed initially and will not be changed by the jury program depending on the queries. Interaction After reading the description of the input, you may ask queries. Queries can be of two types: 1. "+ k" ($$$0 le k le 10^9$$$)xa0β€” ask to move the arrow $$$k$$$ sectors clockwise. 2. "- k" ($$$0 le k le 10^9$$$)xa0β€” ask to move the arrow $$$k$$$ sectors counterclockwise. After each query, you should read an integer $$$x$$$ ($$$1 le x le n$$$)xa0β€” the number of the current sector to which the arrow points. You are allowed to make at most $$$2023$$$ queries in total. If you make too many queries, you will get Wrong answer. After printing a query or the answer, do not forget to output a the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Example Input 1 5 6 7 2 10 9 8 4 3 1 Output + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ! 10 Note Hacks To hack, use the following test format. In the first line, output a single integer $$$n$$$ ($$$1 le n le 10^6$$$)xa0β€” the number of sectors. In the second line, output $$$n$$$ different integers $$$1 le a_1, a_2, dots, a_n le n$$$xa0β€” the numbers of the sectors in clockwise order, the arrow initially points to the sector with the number $$$a_1$$$.
2,200
true
false
false
false
false
true
false
false
false
false
1,247
1944A
There are $$$n$$$ islands, numbered $$$1, 2, ldots, n$$$. Initially, every pair of islands is connected by a bridge. Hence, there are a total of $$$frac{n (n - 1)}{2}$$$ bridges. Everule lives on island $$$1$$$ and enjoys visiting the other islands using bridges. Dominater has the power to destroy at most $$$k$$$ bridges to minimize the number of islands that Everule can reach using (possibly multiple) bridges. Find the minimum number of islands (including island $$$1$$$) that Everule can visit if Dominater destroys bridges optimally. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0β€” the number of test cases. The description of the test cases follows. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 100$$$, $$$0 le k le frac{n cdot (n - 1)}{2}$$$). Output For each test case, output the minimum number of islands that Everule can visit if Dominater destroys bridges optimally. Example Input 6 2 0 2 1 4 1 5 10 5 3 4 4 Note In the first test case, since no bridges can be destroyed, all the islands will be reachable. In the second test case, you can destroy the bridge between islands $$$1$$$ and $$$2$$$. Everule will not be able to visit island $$$2$$$ but can still visit island $$$1$$$. Therefore, the total number of islands that Everule can visit is $$$1$$$. In the third test case, Everule always has a way of reaching all islands despite what Dominater does. For example, if Dominater destroyed the bridge between islands $$$1$$$ and $$$2$$$, Everule can still visit island $$$2$$$ by traveling by $$$1 o 3 o 2$$$ as the bridges between $$$1$$$ and $$$3$$$, and between $$$3$$$ and $$$2$$$ are not destroyed. In the fourth test case, you can destroy all bridges since $$$k = frac{n cdot (n - 1)}{2}$$$. Everule will be only able to visit $$$1$$$ island (island $$$1$$$).
800
true
true
false
false
false
false
false
false
false
true
615
1661D
You are given two arrays: an array $$$a$$$ consisting of $$$n$$$ zeros and an array $$$b$$$ consisting of $$$n$$$ integers. You can apply the following operation to the array $$$a$$$ an arbitrary number of times: choose some subsegment of $$$a$$$ of length $$$k$$$ and add the arithmetic progression $$$1, 2, ldots, k$$$ to this subsegment β€” i.u2009e. add $$$1$$$ to the first element of the subsegment, $$$2$$$ to the second element, and so on. The chosen subsegment should be inside the borders of the array $$$a$$$ (i.e., if the left border of the chosen subsegment is $$$l$$$, then the condition $$$1 le l le l + k - 1 le n$$$ should be satisfied). Note that the progression added is always $$$1, 2, ldots, k$$$ but not the $$$k, k - 1, ldots, 1$$$ or anything else (i.e., the leftmost element of the subsegment always increases by $$$1$$$, the second element always increases by $$$2$$$ and so on). Your task is to find the minimum possible number of operations required to satisfy the condition $$$a_i ge b_i$$$ for each $$$i$$$ from $$$1$$$ to $$$n$$$. Note that the condition $$$a_i ge b_i$$$ should be satisfied for all elements at once. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 3 cdot 10^5$$$) β€” the number of elements in both arrays and the length of the subsegment, respectively. The second line of the input contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 10^{12}$$$), where $$$b_i$$$ is the $$$i$$$-th element of the array $$$b$$$. Output Print one integer β€” the minimum possible number of operations required to satisfy the condition $$$a_i ge b_i$$$ for each $$$i$$$ from $$$1$$$ to $$$n$$$. Examples Input 7 3 50 17 81 25 42 39 96 Note Consider the first example. In this test, we don't really have any choice, so we need to add at least five progressions to make the first element equals $$$5$$$. The array $$$a$$$ becomes $$$[5, 10, 15]$$$. Consider the second example. In this test, let's add one progression on the segment $$$[1; 3]$$$ and two progressions on the segment $$$[4; 6]$$$. Then, the array $$$a$$$ becomes $$$[1, 2, 3, 2, 4, 6]$$$.
1,900
false
true
false
false
true
false
false
false
false
false
2,316
1255A
Bob watches TV every day. He always sets the volume of his TV to $$$b$$$. However, today he is angry to find out someone has changed the volume to $$$a$$$. Of course, Bob has a remote control that can change the volume. There are six buttons ($$$-5, -2, -1, +1, +2, +5$$$) on the control, which in one press can either increase or decrease the current volume by $$$1$$$, $$$2$$$, or $$$5$$$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $$$0$$$. As Bob is so angry, he wants to change the volume to $$$b$$$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $$$a$$$ and $$$b$$$, finds the minimum number of presses to change the TV volume from $$$a$$$ to $$$b$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 le T le 1,000$$$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $$$a$$$ and $$$b$$$ ($$$0 le a, b le 10^{9}$$$)xa0β€” the current volume and Bob's desired volume, respectively. Output For each test case, output a single integerxa0β€” the minimum number of presses to change the TV volume from $$$a$$$ to $$$b$$$. If Bob does not need to change the volume (i.e. $$$a=b$$$), then print $$$0$$$. Note In the first example, Bob can press the $$$-2$$$ button twice to reach $$$0$$$. Note that Bob can not press $$$-5$$$ when the volume is $$$4$$$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $$$+5$$$ twice, then press $$$-1$$$ once. In the last example, Bob can press the $$$+5$$$ once, then press $$$+1$$$.
800
true
false
false
false
false
false
false
false
false
false
4,410
1417B
RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$. Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ β€” the number of pairs of integers $$$(i, j)$$$ such that $$$1 le i < j le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible. For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 le n le 10^5$$$, $$$0 le T le 10^9$$$) β€” the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 le a_i le 10^9$$$) β€” the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. Example Input 2 6 7 1 2 3 4 5 6 3 6 3 3 3
1,100
true
true
false
false
false
false
false
false
true
false
3,595
1178F1
This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of $$$m$$$ and the time limit. You need to solve both subtasks in order to hack this one. There are $$$n+1$$$ distinct colours in the universe, numbered $$$0$$$ through $$$n$$$. There is a strip of paper $$$m$$$ centimetres long initially painted with colour $$$0$$$. Alice took a brush and painted the strip using the following process. For each $$$i$$$ from $$$1$$$ to $$$n$$$, in this order, she picks two integers $$$0 leq a_i < b_i leq m$$$, such that the segment $$$[a_i, b_i]$$$ is currently painted with a single colour, and repaints it with colour $$$i$$$. Alice chose the segments in such a way that each centimetre is now painted in some colour other than $$$0$$$. Formally, the segment $$$[i-1, i]$$$ is painted with colour $$$c_i$$$ ($$$c_i eq 0$$$). Every colour other than $$$0$$$ is visible on the strip. Count the number of different pairs of sequences $$${a_i}_{i=1}^n$$$, $$${b_i}_{i=1}^n$$$ that result in this configuration. Since this number may be large, output it modulo $$$998244353$$$. Input The first line contains a two integers $$$n$$$, $$$m$$$ ($$$1 leq n leq 500$$$, $$$n = m$$$)xa0β€” the number of colours excluding the colour $$$0$$$ and the length of the paper, respectively. The second line contains $$$m$$$ space separated integers $$$c_1, c_2, ldots, c_m$$$ ($$$1 leq c_i leq n$$$)xa0β€” the colour visible on the segment $$$[i-1, i]$$$ after the process ends. It is guaranteed that for all $$$j$$$ between $$$1$$$ and $$$n$$$ there is an index $$$k$$$ such that $$$c_k = j$$$. Note that since in this subtask $$$n = m$$$, this means that $$$c$$$ is a permutation of integers $$$1$$$ through $$$n$$$. Output Output a single integerxa0β€” the number of ways Alice can perform the painting, modulo $$$998244353$$$. Note In the first example, there are $$$5$$$ ways, all depicted in the figure below. Here, $$$0$$$ is white, $$$1$$$ is red, $$$2$$$ is green and $$$3$$$ is blue. Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour $$$2$$$.
2,200
false
false
false
true
false
false
false
false
false
false
4,825
1725J
One day, Pak Chanek who is already bored of being alone at home decided to go traveling. While looking for an appropriate place, he found that Londonesia has an interesting structure. According to the information gathered by Pak Chanek, there are $$$N$$$ cities numbered from $$$1$$$ to $$$N$$$. The cities are connected to each other with $$$N-1$$$ two-directional roads, with the $$$i$$$-th road connecting cities $$$U_i$$$ and $$$V_i$$$, and taking a time of $$$W_i$$$ hours to be traversed. In other words, Londonesia's structure forms a tree. Pak Chanek wants to go on a journey in Londonesia starting and ending in any city (not necessarily the same city) such that each city is visited at least once with the least time possible. In order for the journey to end quicker, Pak Chanek also carries an instant teleportation device for moving from one city to any city that can only be used at most once. Help Pak Chanek for finding the minimum time needed. Notes: Pak Chanek only needs to visit each city at least once. Pak Chanek does not have to traverse each road. In the journey, a city or a road can be visited more than once. Input The first line contains a single integer $$$N$$$ ($$$1 le N le 10^5$$$) β€” the number of cities in Londonesia. The $$$i$$$-th of the next $$$N-1$$$ lines contains three integers $$$U_i$$$, $$$V_i$$$, and $$$W_i$$$ ($$$1 le U_i, V_i le N$$$, $$$1 le W_i le 10^9$$$) β€” a two-directional road that connects cities $$$U_i$$$ and $$$V_i$$$ that takes $$$W_i$$$ hours to be traversed. The roads in Londonesia form a tree. Output Output one integer, which represents the minimum time in hours that is needed to visit each city at least once. Examples Input 5 1 2 45 1 3 50 1 4 10 1 5 65 Note In the first example, the journey that has the minimum time is $$$2 β†’ 1 xrightarrow{ ext{teleport}} 4 β†’ 3$$$. In the second example, the journey that has the minimum time is $$$3 β†’ 1 β†’ 4 β†’ 1 β†’ 2 xrightarrow{ ext{teleport}} 5$$$.
2,500
false
false
false
true
false
false
false
false
false
false
1,937
602A
After seeing the "ALL YOUR BASE ARE BELONG TO US" meme for the first time, numbers _X_ and _Y_ realised that they have different bases, which complicated their relations. You're given a number _X_ represented in base _b__x_ and a number _Y_ represented in base _b__y_. Compare those two numbers. Input The first line of the input contains two space-separated integers _n_ and _b__x_ (1u2009≀u2009_n_u2009≀u200910, 2u2009≀u2009_b__x_u2009≀u200940), where _n_ is the number of digits in the _b__x_-based representation of _X_. The second line contains _n_ space-separated integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (0u2009≀u2009_x__i_u2009<u2009_b__x_) β€” the digits of _X_. They are given in the order from the most significant digit to the least significant one. The following two lines describe _Y_ in the same way: the third line contains two space-separated integers _m_ and _b__y_ (1u2009≀u2009_m_u2009≀u200910, 2u2009≀u2009_b__y_u2009≀u200940, _b__x_u2009β‰ u2009_b__y_), where _m_ is the number of digits in the _b__y_-based representation of _Y_, and the fourth line contains _m_ space-separated integers _y_1,u2009_y_2,u2009...,u2009_y__m_ (0u2009≀u2009_y__i_u2009<u2009_b__y_) β€” the digits of _Y_. There will be no leading zeroes. Both _X_ and _Y_ will be positive. All digits of both numbers are given in the standard decimal numeral system. Output Output a single character (quotes for clarity): '<' if _X_u2009<u2009_Y_ '>' if _X_u2009>u2009_Y_ '=' if _X_u2009=u2009_Y_ Examples Input 6 2 1 0 1 1 1 1 2 10 4 7 Input 7 16 15 15 4 0 0 7 10 7 9 4 8 0 3 1 5 0 Note In the first sample, _X_u2009=u20091011112u2009=u20094710u2009=u2009_Y_. In the second sample, _X_u2009=u20091023u2009=u2009215 and _Y_u2009=u2009245u2009=u20091123, thus _X_u2009<u2009_Y_. In the third sample, and _Y_u2009=u200948031509. We may notice that _X_ starts with much larger digits and _b__x_ is much larger than _b__y_, so _X_ is clearly larger than _Y_.
1,100
false
false
true
false
false
false
true
false
false
false
7,435
1726E
A permutation $$$p$$$ of length $$$n$$$ is called almost perfect if for all integer $$$1 leq i leq n$$$, it holds that $$$lvert p_i - p^{-1}_i vert le 1$$$, where $$$p^{-1}$$$ is the inverse permutation of $$$p$$$ (i.e. $$$p^{-1}_{k_1} = k_2$$$ if and only if $$$p_{k_2} = k_1$$$). Count the number of almost perfect permutations of length $$$n$$$ modulo $$$998244353$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) β€” the number of test cases. The description of each test case follows. The first and only line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 3 cdot 10^5$$$) β€” the length of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output a single integer β€” the number of almost perfect permutations of length $$$n$$$ modulo $$$998244353$$$. Note For $$$n = 2$$$, both permutations $$$[1, 2]$$$, and $$$[2, 1]$$$ are almost perfect. For $$$n = 3$$$, there are only $$$6$$$ permutations. Having a look at all of them gives us: $$$[1, 2, 3]$$$ is an almost perfect permutation. $$$[1, 3, 2]$$$ is an almost perfect permutation. $$$[2, 1, 3]$$$ is an almost perfect permutation. $$$[2, 3, 1]$$$ is NOT an almost perfect permutation ($$$lvert p_2 - p^{-1}_2 vert = lvert 3 - 1 vert = 2$$$). $$$[3, 1, 2]$$$ is NOT an almost perfect permutation ($$$lvert p_2 - p^{-1}_2 vert = lvert 1 - 3 vert = 2$$$). $$$[3, 2, 1]$$$ is an almost perfect permutation. So we get $$$4$$$ almost perfect permutations.
2,400
true
false
false
false
false
false
false
false
false
false
1,929
513B2
Problem - 513B2 - Codeforces =============== xa0 ]( "16140") as the following sum: Find the lexicographically _m_-th permutation of length _n_ in the set of permutations having the maximum possible value of _f_(_p_). Input The single line of input contains two integers _n_ and _m_ (1u2009≀u2009_m_u2009≀u2009_cnt__n_), where _cnt__n_ is the number of permutations of length _n_ with maximum possible value of _f_(_p_). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem B1 (3 points), the constraint 1u2009≀u2009_n_u2009≀u20098 will hold. In subproblem B2 (4 points), the constraint 1u2009≀u2009_n_u2009≀u200950 will hold. Output Output _n_ number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible _f_(_p_) which is equal to 4. Among them, (2,u20091) comes second in lexicographical order.
1,800
true
false
false
false
false
false
false
false
false
false
7,791
1720B
You are given an array $$$a$$$ that contains $$$n$$$ integers. You can choose any proper subsegment $$$a_l, a_{l + 1}, ldots, a_r$$$ of this array, meaning you can choose any two integers $$$1 le l le r le n$$$, where $$$r - l + 1 < n$$$. We define the beauty of a given subsegment as the value of the following expression: $$$$$$max(a_{1}, a_{2}, ldots, a_{l-1}, a_{r+1}, a_{r+2}, ldots, a_{n}) - min(a_{1}, a_{2}, ldots, a_{l-1}, a_{r+1}, a_{r+2}, ldots, a_{n}) + max(a_{l}, ldots, a_{r}) - min(a_{l}, ldots, a_{r}).$$$$$$ Please find the maximum beauty among all proper subsegments. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 1000$$$) β€” the number of test cases. Then follow the descriptions of each test case. The first line of each test case contains a single integer $$$n$$$ $$$(4 leq n leq 10^5)$$$ β€” the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_{i} leq 10^9$$$) β€” the elements of the given array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note In the first test case, the optimal segment is $$$l = 7$$$, $$$r = 8$$$. The beauty of this segment equals to $$$(6 - 1) + (5 - 1) = 9$$$. In the second test case, the optimal segment is $$$l = 2$$$, $$$r = 4$$$. The beauty of this segment equals $$$(100 - 2) + (200 - 1) = 297$$$.
800
true
true
false
false
true
false
true
false
true
false
1,964
1322D
A popular reality show is recruiting a new cast for the third season! $$$n$$$ candidates numbered from $$$1$$$ to $$$n$$$ have been interviewed. The candidate $$$i$$$ has aggressiveness level $$$l_i$$$, and recruiting this candidate will cost the show $$$s_i$$$ roubles. The show host reviewes applications of all candidates from $$$i=1$$$ to $$$i=n$$$ by increasing of their indices, and for each of them she decides whether to recruit this candidate or not. If aggressiveness level of the candidate $$$i$$$ is strictly higher than that of any already accepted candidates, then the candidate $$$i$$$ will definitely be rejected. Otherwise the host may accept or reject this candidate at her own discretion. The host wants to choose the cast so that to maximize the total profit. The show makes revenue as follows. For each aggressiveness level $$$v$$$ a corresponding profitability value $$$c_v$$$ is specified, which can be positive as well as negative. All recruited participants enter the stage one by one by increasing of their indices. When the participant $$$i$$$ enters the stage, events proceed as follows: The show makes $$$c_{l_i}$$$ roubles, where $$$l_i$$$ is initial aggressiveness level of the participant $$$i$$$. If there are two participants with the same aggressiveness level on stage, they immediately start a fight. The outcome of this is: the defeated participant is hospitalized and leaves the show. aggressiveness level of the victorious participant is increased by one, and the show makes $$$c_t$$$ roubles, where $$$t$$$ is the new aggressiveness level. The fights continue until all participants on stage have distinct aggressiveness levels. It is allowed to select an empty set of participants (to choose neither of the candidates). The host wants to recruit the cast so that the total profit is maximized. The profit is calculated as the total revenue from the events on stage, less the total expenses to recruit all accepted participants (that is, their total $$$s_i$$$). Help the host to make the show as profitable as possible. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2000$$$) β€” the number of candidates and an upper bound for initial aggressiveness levels. The second line contains $$$n$$$ integers $$$l_i$$$ ($$$1 le l_i le m$$$) β€” initial aggressiveness levels of all candidates. The third line contains $$$n$$$ integers $$$s_i$$$ ($$$0 le s_i le 5000$$$) β€” the costs (in roubles) to recruit each of the candidates. The fourth line contains $$$n + m$$$ integers $$$c_i$$$ ($$$c_i le 5000$$$) β€” profitability for each aggrressiveness level. It is guaranteed that aggressiveness level of any participant can never exceed $$$n + m$$$ under given conditions. Output Print a single integerxa0β€” the largest profit of the show. Examples Input 5 4 4 3 1 2 1 1 2 1 2 1 1 2 3 4 5 6 7 8 9 Input 2 2 1 2 0 0 2 1 -100 -100 Input 5 4 4 3 2 1 1 0 2 6 7 4 12 12 12 6 -3 -5 3 10 -4 Note In the first sample case it is optimal to recruit candidates $$$1, 2, 3, 5$$$. Then the show will pay $$$1 + 2 + 1 + 1 = 5$$$ roubles for recruitment. The events on stage will proceed as follows: a participant with aggressiveness level $$$4$$$ enters the stage, the show makes $$$4$$$ roubles; a participant with aggressiveness level $$$3$$$ enters the stage, the show makes $$$3$$$ roubles; a participant with aggressiveness level $$$1$$$ enters the stage, the show makes $$$1$$$ rouble; a participant with aggressiveness level $$$1$$$ enters the stage, the show makes $$$1$$$ roubles, a fight starts. One of the participants leaves, the other one increases his aggressiveness level to $$$2$$$. The show will make extra $$$2$$$ roubles for this. Total revenue of the show will be $$$4 + 3 + 1 + 1 + 2=11$$$ roubles, and the profit is $$$11 - 5 = 6$$$ roubles. In the second sample case it is impossible to recruit both candidates since the second one has higher aggressiveness, thus it is better to recruit the candidate $$$1$$$.
2,800
false
false
false
true
false
false
false
false
false
false
4,104
933E
East or west, home is best. That's why family reunion, the indispensable necessity of Lunar New Year celebration, is put in such a position. After the reunion dinner, Little Tommy plays a game with the family. Here is a concise introduction to this game: 1. There is a sequence of _n_ non-negative integers _p_1,u2009_p_2,u2009...,u2009_p__n_ in the beginning. It is ruled that each integer in this sequence should be non-negative at any time. 2. You can select two consecutive positive integers in this sequence, _p__i_ and _p__i_u2009+u20091 (1u2009≀u2009_i_u2009<u2009_n_), and then decrease them by their minimum (i.xa0e. _min_(_p__i_,u2009_p__i_u2009+u20091)), the cost of this operation is equal to _min_(_p__i_,u2009_p__i_u2009+u20091). We call such operation as a descension. 3. The game immediately ends when there are no two consecutive positive integers. Your task is to end the game so that the total cost of your operations is as small as possible. Obviously, every game ends after at most _n_u2009-u20091 descensions. Please share your solution of this game with the lowest cost. Input The first line contains one integer _n_ (1u2009≀u2009_n_u2009≀u20093Β·105). The second line contains _n_ space-separated integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (0u2009≀u2009_p__i_u2009≀u2009109,u2009_i_u2009=u20091,u20092,u2009...,u2009_n_). Output In the first line print one integer as the number of descensions _m_ (0u2009≀u2009_m_u2009≀u2009_n_u2009-u20091). In the next _m_ lines print the descensions chronologically. More precisely, in each line of the next _m_ lines print one integer _i_ (1u2009≀u2009_i_u2009<u2009_n_) representing a descension would operate on _p__i_ and _p__i_u2009+u20091 such that all the descensions could be utilized from top to bottom. If there are many possible solutions to reach the minimal cost, print any of them. Note In the first sample, one possible best solution is , of which the cost is 1u2009+u20091u2009=u20092. In the second sample, one possible best solution is , of which the cost is 1u2009+u20091u2009+u20091u2009=u20093.
3,200
false
false
false
true
false
true
false
false
false
false
5,967
1556E
William has two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ items. For some segments $$$l..r$$$ of these arrays William wants to know if it is possible to equalize the values of items in these segments using a balancing operation. Formally, the values are equalized if for each $$$i$$$ from $$$l$$$ to $$$r$$$ holds $$$a_i = b_i$$$. To perform a balancing operation an even number of indices must be selected, such that $$$l le pos_1 < pos_2 < dots < pos_k le r$$$. Next the items of array a at positions $$$pos_1, pos_3, pos_5, dots$$$ get incremented by one and the items of array b at positions $$$pos_2, pos_4, pos_6, dots$$$ get incremented by one. William wants to find out if it is possible to equalize the values of elements in two arrays for each segment using some number of balancing operations, and what is the minimal number of operations required for that. Note that for each segment the operations are performed independently. Input The first line contains a two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 10^5$$$, $$$1 le q le 10^5$$$), the size of arrays $$$a$$$ and $$$b$$$ and the number of segments. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ $$$(0 le a_i le 10^9)$$$. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ $$$(0 le b_i le 10^9)$$$. Each of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 le l_i < r_i le n)$$$, the edges of segments. Note For the first segment from $$$2$$$ to $$$6$$$ you can do one operation with $$$pos = [2, 3, 5, 6]$$$, after this operation the arrays will be: $$$a = [0, 2, 2, 9, 4, 2, 7, 5]$$$, $$$b = [2, 2, 2, 9, 4, 2, 5, 8]$$$. Arrays are equal on a segment from $$$2$$$ to $$$6$$$ after this operation. For the second segment from $$$1$$$ to $$$7$$$ you can do three following operations: 1. $$$pos = [1, 3, 5, 6]$$$ 2. $$$pos = [1, 7]$$$ 3. $$$pos = [2, 7]$$$ After these operations, the arrays will be: $$$a = [2, 2, 2, 9, 4, 2, 7, 5]$$$, $$$b = [2, 2, 2, 9, 4, 2, 7, 8]$$$. Arrays are equal on a segment from $$$1$$$ to $$$7$$$ after these operations. For the third segment from $$$2$$$ to $$$4$$$ you can do one operation with $$$pos = [2, 3]$$$, after the operation arrays will be: $$$a = [0, 2, 2, 9, 3, 2, 7, 5]$$$, $$$b = [2, 2, 2, 9, 4, 1, 5, 8]$$$. Arrays are equal on a segment from $$$2$$$ to $$$4$$$ after this operation. It is impossible to equalize the fourth and the fifth segment.
2,200
false
true
false
true
true
false
false
false
false
false
2,851
1010E
Natasha was already going to fly back to Earth when she remembered that she needs to go to the Martian store to buy Martian souvenirs for her friends. It is known, that the Martian year lasts $$$x_{max}$$$ months, month lasts $$$y_{max}$$$ days, day lasts $$$z_{max}$$$ seconds. Natasha also knows that this store works according to the following schedule: 2 months in a year were selected: $$$x_l$$$ and $$$x_r$$$ ($$$1le x_lle x_rle x_{max}$$$), 2 days in a month: $$$y_l$$$ and $$$y_r$$$ ($$$1le y_lle y_rle y_{max}$$$) and 2 seconds in a day: $$$z_l$$$ and $$$z_r$$$ ($$$1le z_lle z_rle z_{max}$$$). The store works at all such moments (month $$$x$$$, day $$$y$$$, second $$$z$$$), when simultaneously $$$x_lle xle x_r$$$, $$$y_lle yle y_r$$$ and $$$z_lle zle z_r$$$. Unfortunately, Natasha does not know the numbers $$$x_l,x_r,y_l,y_r,z_l,z_r$$$. One Martian told Natasha: "I went to this store $$$(n+m)$$$ times. $$$n$$$ times of them it was opened, and $$$m$$$ timesxa0β€” closed." He also described his every trip to the store: the month, day, second of the trip and whether the store was open or closed at that moment. Natasha can go to the store $$$k$$$ times. For each of them, determine whether the store at the time of the trip is open, closed, or this information is unknown. Input The first line contains $$$6$$$ integers $$$x_{max}$$$, $$$y_{max}$$$, $$$z_{max}$$$, $$$n$$$, $$$m$$$, $$$k$$$ ($$$1le x_{max},y_{max},z_{max}le 10^5$$$, $$$1le nle 10^5$$$, $$$0le mle 10^5$$$, $$$1le kle 10^5$$$)xa0β€” number of months in a year, days in a month, seconds in a day, times when the store (according to a Martian) was opened, when it was closed and Natasha's queries. The $$$i$$$-th of the next $$$n$$$ lines contains $$$3$$$ integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$1le x_ile x_{max}$$$, $$$1le y_ile y_{max}$$$, $$$1le z_ile z_{max}$$$)xa0β€” month, day and second of $$$i$$$-th time, when the store, according to the Martian, was opened. The $$$i$$$-th of the next $$$m$$$ lines contains $$$3$$$ integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$1le x_ile x_{max}$$$, $$$1le y_ile y_{max}$$$, $$$1le z_ile z_{max}$$$)xa0β€” month, day and second of $$$i$$$-th time, when the store, according to the Martian, was closed. The $$$i$$$-th of the next $$$k$$$ lines contains $$$3$$$ integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$1le x_ile x_{max}$$$, $$$1le y_ile y_{max}$$$, $$$1le z_ile z_{max}$$$)xa0β€” month, day and second of $$$i$$$-th Natasha's query. Output If the Martian was mistaken and his information about when the store is open and when it is closed is inconsistent, print a single line "INCORRECT" (without quotes). Otherwise, print the first line "CORRECT" (without quotes). Next output $$$k$$$ lines: in $$$i$$$-th of them, output an answer to $$$i$$$-th Natasha's query: "OPEN" (without quotes), if the store was opened at the moment of this query, "CLOSED" (without quotes), if it was closed, or "UNKNOWN" (without quotes), if this information can not be determined on the basis of available data. Examples Input 10 10 10 3 1 3 2 6 2 4 2 4 6 4 6 9 9 9 3 3 3 10 10 10 8 8 8 Output CORRECT OPEN CLOSED UNKNOWN Input 10 10 10 1 1 1 2 5 7 2 5 7 8 9 10 Note Consider the first test case. There are $$$10$$$ months in a year, $$$10$$$ days in a month, and $$$10$$$ seconds in a day. The store was opened in $$$3$$$ moments: month $$$2$$$, day $$$6$$$, second $$$2$$$; month $$$4$$$, day $$$2$$$, second $$$4$$$; month $$$6$$$, day $$$4$$$, second $$$6$$$. The store was closed at the time: month $$$9$$$, day $$$9$$$, second $$$9$$$. Queries: month $$$3$$$, day $$$3$$$, second $$$3$$$xa0β€” open ("OPEN") (since the store opens no later than month $$$2$$$, day $$$2$$$, second $$$2$$$ and closes no earlier than in month $$$6$$$, day $$$6$$$, second $$$6$$$); month $$$10$$$, day $$$10$$$, second $$$10$$$xa0β€” closed ("CLOSED") (since it is closed even in the month $$$9$$$, day $$$9$$$, second $$$9$$$); month $$$8$$$, day $$$8$$$, second $$$8$$$xa0β€” unknown ("UNKNOWN") (because the schedule in which the store is open at this moment exists, and the schedule in which the store is closed at this moment exists as well). In the second test case, the store was closed and opened at the same timexa0β€” contradiction ("INCORRECT").
2,700
false
false
false
false
true
false
false
false
false
false
5,631
1111C
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of $$$2$$$. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least $$$2$$$, divide the base into $$$2$$$ equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes $$$A$$$ amount of power, otherwise it takes his $$$B cdot n_a cdot l$$$ amount of power, where $$$n_a$$$ is the number of avengers and $$$l$$$ is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base. Input The first line contains four integers $$$n$$$, $$$k$$$, $$$A$$$ and $$$B$$$ ($$$1 leq n leq 30$$$, $$$1 leq k leq 10^5$$$, $$$1 leq A,B leq 10^4$$$), where $$$2^n$$$ is the length of the base, $$$k$$$ is the number of avengers and $$$A$$$ and $$$B$$$ are the constants explained in the question. The second line contains $$$k$$$ integers $$$a_{1}, a_{2}, a_{3}, ldots, a_{k}$$$ ($$$1 leq a_{i} leq 2^n$$$), where $$$a_{i}$$$ represents the position of avenger in the base. Output Output one integer β€” the minimum power needed to destroy the avengers base. Note Consider the first example. One option for Thanos is to burn the whole base $$$1-4$$$ with power $$$2 cdot 2 cdot 4 = 16$$$. Otherwise he can divide the base into two parts $$$1-2$$$ and $$$3-4$$$. For base $$$1-2$$$, he can either burn it with power $$$2 cdot 1 cdot 2 = 4$$$ or divide it into $$$2$$$ parts $$$1-1$$$ and $$$2-2$$$. For base $$$1-1$$$, he can burn it with power $$$2 cdot 1 cdot 1 = 2$$$. For $$$2-2$$$, he can destroy it with power $$$1$$$, as there are no avengers. So, the total power for destroying $$$1-2$$$ is $$$2 + 1 = 3$$$, which is less than $$$4$$$. Similarly, he needs $$$3$$$ power to destroy $$$3-4$$$. The total minimum power needed is $$$6$$$.
1,700
true
false
false
false
false
false
true
true
false
false
5,139
664A
Problem - 664A - 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 number theory *800 No tag edit access β†’ Contest materials of two positive integers _a_ and _b_ is equal to the biggest integer _d_ such that both integers _a_ and _b_ are divisible by _d_. There are many efficient algorithms to find greatest common divisor _GCD_(_a_,u2009_b_), for example, Euclid algorithm. Formally, find the biggest integer _d_, such that all integers _a_,u2009_a_u2009+u20091,u2009_a_u2009+u20092,u2009...,u2009_b_ are divisible by _d_. To make the problem even more complicated we allow _a_ and _b_ to be up to googol, 10100xa0β€” such number do not fit even in 64-bit integer type! Input The only line of the input contains two integers _a_ and _b_ (1u2009≀u2009_a_u2009≀u2009_b_u2009≀u200910100). Output Output one integerxa0β€” greatest common divisor of all integers from _a_ to _b_ inclusive. Examples Input 1 2 Output 1 Input 61803398874989484820458683436563811772030917980576 61803398874989484820458683436563811772030917980576 Output 61803398874989484820458683436563811772030917980576
800
true
false
false
false
false
false
false
false
false
false
7,162
1472C
Polycarp found under the Christmas tree an array $$$a$$$ of $$$n$$$ elements and instructions for playing with it: At first, choose index $$$i$$$ ($$$1 leq i leq n$$$)xa0β€” starting position in the array. Put the chip at the index $$$i$$$ (on the value $$$a_i$$$). While $$$i leq n$$$, add $$$a_i$$$ to your score and move the chip $$$a_i$$$ positions to the right (i.e. replace $$$i$$$ with $$$i + a_i$$$). If $$$i > n$$$, then Polycarp ends the game. For example, if $$$n = 5$$$ and $$$a = [7, 3, 1, 2, 3]$$$, then the following game options are possible: Polycarp chooses $$$i = 1$$$. Game process: $$$i = 1 overset{+7}{longrightarrow} 8$$$. The score of the game is: $$$a_1 = 7$$$. Polycarp chooses $$$i = 2$$$. Game process: $$$i = 2 overset{+3}{longrightarrow} 5 overset{+3}{longrightarrow} 8$$$. The score of the game is: $$$a_2 + a_5 = 6$$$. Polycarp chooses $$$i = 3$$$. Game process: $$$i = 3 overset{+1}{longrightarrow} 4 overset{+2}{longrightarrow} 6$$$. The score of the game is: $$$a_3 + a_4 = 3$$$. Polycarp chooses $$$i = 4$$$. Game process: $$$i = 4 overset{+2}{longrightarrow} 6$$$. The score of the game is: $$$a_4 = 2$$$. Polycarp chooses $$$i = 5$$$. Game process: $$$i = 5 overset{+3}{longrightarrow} 8$$$. The score of the game is: $$$a_5 = 3$$$. Help Polycarp to find out the maximum score he can get if he chooses the starting index in an optimal way. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β€” the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0β€” the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0β€” elements of the array $$$a$$$. 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 on a separate line one numberxa0β€” the maximum score that Polycarp can get by playing the game on the corresponding array according to the instruction from the statement. Note that Polycarp chooses any starting position from $$$1$$$ to $$$n$$$ in such a way as to maximize his result. Example Input 4 5 7 3 1 2 3 3 2 1 4 6 2 1000 2 3 995 1 5 1 1 1 1 1 Note The first test case is explained in the statement. In the second test case, the maximum score can be achieved by choosing $$$i = 1$$$. In the third test case, the maximum score can be achieved by choosing $$$i = 2$$$. In the fourth test case, the maximum score can be achieved by choosing $$$i = 1$$$.
1,100
false
false
false
true
false
false
false
false
false
true
3,322
1980G
Yasya was walking in the forest and accidentally found a tree with $$$n$$$ vertices. A tree is a connected undirected graph with no cycles. Next to the tree, the girl found an ancient manuscript with $$$m$$$ queries written on it. The queries can be of two types. The first type of query is described by the integer $$$y$$$. The weight of each edge in the tree is replaced by the and mentally draws a bidirectional edge of weight $$$x$$$ from $$$v$$$ to $$$u$$$ in the tree. Then Yasya finds a simple cycle in the resulting graph and calculates the . If an edge between $$$v$$$ and $$$u$$$ already existed, a simple cycle is the path $$$v o u o v$$$. Note that the second type of query is performed mentally, meaning the tree does not change in any way after it. Help Yasya answer all the queries. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le m le 2 cdot 10^5$$$) β€” the number of vertices in the tree and the number of queries. The next $$$n - 1$$$ lines of each test case contain three integers $$$v$$$, $$$u$$$, $$$w$$$ ($$$1 le v, u le n$$$, $$$1 le w le 10^9$$$) β€” the ends of some edge in the tree and its weight. It is guaranteed that the given set of edges forms a tree. The next $$$m$$$ lines of each test case describe the queries: ^ $$$y$$$ ($$$1 le y le 10^9$$$) β€” parameter of the first type query; ? $$$v$$$ $$$x$$$ ($$$1 le v le n$$$, $$$1 le x le 10^9$$$) β€” parameters of the second type query. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. The same is guaranteed for $$$m$$$. Output For each test case, output the answers to the queries of the second type. Examples Input 2 3 7 1 2 1 3 1 8 ^ 5 ? 2 9 ^ 1 ? 1 10 ^ 6 ? 3 1 ? 2 9 5 6 1 2 777 3 2 2812 4 1 16 5 3 1000000000 ^ 4 ? 3 123 ? 5 1000000000 ^ 1000000000 ? 1 908070 ? 2 1 Output 13 15 11 10 1000000127 2812 999756331 999999756 Input 3 8 4 8 6 3 6 3 4 2 5 4 7 6 2 7 1 10 4 1 4 5 1 2 ^ 4 ^ 7 ? 7 8 ? 4 10 5 6 3 1 4 2 3 9 4 3 6 5 2 10 ? 5 7 ^ 1 ^ 8 ? 4 10 ? 1 9 ? 3 6 4 2 2 1 4 4 3 5 2 3 4 ^ 13 ? 1 10 Output 14 13 13 8 11 11 10
2,300
false
true
false
false
true
false
false
false
false
true
403
727D
The organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXL you are given the number of t-shirts of this size. During the registration, the organizers asked each of the _n_ participants about the t-shirt size he wants. If a participant hesitated between two sizes, he could specify two neighboring sizesxa0β€” this means that any of these two sizes suits him. Write a program that will determine whether it is possible to present a t-shirt to each participant of the competition, or not. Of course, each participant should get a t-shirt of proper size: the size he wanted, if he specified one size; any of the two neibouring sizes, if he specified two sizes. If it is possible, the program should find any valid distribution of the t-shirts. Input The first line of the input contains six non-negative integersxa0β€” the number of t-shirts of each size. The numbers are given for the sizes S, M, L, XL, XXL, XXXL, respectively. The total number of t-shirts doesn't exceed 100u2009000. The second line contains positive integer _n_ (1u2009≀u2009_n_u2009≀u2009100u2009000)xa0β€” the number of participants. The following _n_ lines contain the sizes specified by the participants, one line per participant. The _i_-th line contains information provided by the _i_-th participant: single size or two sizes separated by comma (without any spaces). If there are two sizes, the sizes are written in increasing order. It is guaranteed that two sizes separated by comma are neighboring. Output If it is not possible to present a t-shirt to each participant, print Β«NOΒ» (without quotes). Otherwise, print _n_u2009+u20091 lines. In the first line print Β«YESΒ» (without quotes). In the following _n_ lines print the t-shirt sizes the orginizers should give to participants, one per line. The order of the participants should be the same as in the input. If there are multiple solutions, print any of them. Examples Input 0 1 0 1 1 0 3 XL S,M XL,XXL Input 1 1 2 0 1 1 5 S M S,M XXL,XXXL XL,XXL
1,800
false
true
false
false
false
true
false
false
false
false
6,897
1862G
Tema bought an old device with a small screen and a worn-out inscription "The Great Equalizer" on the side. The seller said that the device needs to be given an array $$$a$$$ of integers as input, after which "The Great Equalizer" will work as follows: 1. Sort the current array in non-decreasing order and remove duplicate elements leaving only one occurrence of each element. 2. If the current length of the array is equal to $$$1$$$, the device stops working and outputs the single number in the arrayxa0β€” output value of the device. 3. Add an arithmetic progression {$$$n, n - 1, n - 2, ldots, 1$$$} to the current array, where $$$n$$$ is the length of the current array. In other words, $$$n - i$$$ is added to the $$$i$$$-th element of the array, when indexed from zero. 4. Go to the first step. To test the operation of the device, Tema came up with a certain array of integers $$$a$$$, and then wanted to perform $$$q$$$ operations on the array $$$a$$$ of the following type: 1. Assign the value $$$x$$$ ($$$1 le x le 10^9$$$) to the element $$$a_i$$$ ($$$1 le i le n$$$). 2. Give the array $$$a$$$ as input to the device and find out the result of the device's operation, while the array $$$a$$$ remains unchanged during the operation of the device. Help Tema find out the output values of the device after each operation. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Then follows the description of each test case. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the size of the array $$$a$$$ that Tema initially came up with. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, a_3, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β€” the elements of the array $$$a$$$. The third line of a set contains a single integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0β€” the number of operations. Each of the next $$$q$$$ lines of a test case contains two integers $$$i$$$ ($$$1 le i le n$$$) and $$$x$$$ ($$$1 le x le 10^9$$$) - the descriptions of the operations. It is guaranteed that the sum of the values of $$$n$$$ and the sum of the values of $$$q$$$ for all test cases do not exceed $$$2 cdot 10^5$$$. Note Let's consider the first example of the input. Initially, the array of numbers given as input to the device will be $$$[6, 4, 8]$$$. It will change as follows: $$$$$$[6, 4, 8] ightarrow [4, 6, 8] ightarrow [7, 8, 9] ightarrow [10, 10, 10] ightarrow [10]$$$$$$ Then, the array of numbers given as input to the device will be $$$[6, 10, 8]$$$. It will change as follows: $$$$$$[6, 10, 8] ightarrow [6, 8, 10] ightarrow [9, 10, 11] ightarrow [12, 12, 12] ightarrow [12]$$$$$$ The last array of numbers given as input to the device will be $$$[6, 10, 1]$$$. It will change as follows: $$$$$$[6, 10, 1] ightarrow [1, 6, 10] ightarrow [4, 8, 11] ightarrow [7, 10, 12] ightarrow [10, 12, 13] ightarrow [13, 14, 14] ightarrow [13, 14] ightarrow [15, 15] ightarrow [15]$$$$$$
2,000
true
false
false
false
true
false
false
true
true
false
1,107
198C
Qwerty the Ranger arrived to the Diatar system with a very important task. He should deliver a special carcinogen for scientific research to planet Persephone. This is urgent, so Qwerty has to get to the planet as soon as possible. A lost day may fail negotiations as nobody is going to pay for an overdue carcinogen. You can consider Qwerty's ship, the planet Persephone and the star Diatar points on a plane. Diatar is located in the origin of coordinate axes β€” at point (0,u20090). Persephone goes round Diatar along a circular orbit with radius _R_ in the counter-clockwise direction at constant linear speed _v__p_ (thus, for instance, a full circle around the star takes of time). At the initial moment of time Persephone is located at point (_x__p_,u2009_y__p_). At the initial moment of time Qwerty's ship is at point (_x_,u2009_y_). Qwerty can move in any direction at speed of at most _v_ (_v_u2009>u2009_v__p_). The star Diatar is hot (as all stars), so Qwerty can't get too close to it. The ship's metal sheathing melts at distance _r_ (_r_u2009<u2009_R_) from the star. Find the minimum time Qwerty needs to get the carcinogen to planet Persephone. Input The first line contains space-separated integers _x__p_, _y__p_ and _v__p_ (u2009-u2009104u2009≀u2009_x__p_,u2009_y__p_u2009≀u2009104, 1u2009≀u2009_v__p_u2009<u2009104) β€” Persephone's initial position and the speed at which it goes round Diatar. The second line contains space-separated integers _x_, _y_, _v_ and _r_ (u2009-u2009104u2009≀u2009_x_,u2009_y_u2009≀u2009104, 1u2009<u2009_v_u2009≀u2009104, 1u2009≀u2009_r_u2009≀u2009104) β€” The intial position of Qwerty's ship, its maximum speed and the minimum safe distance to star Diatar. It is guaranteed that _r_2u2009<u2009_x_2u2009+u2009_y_2, _r_2u2009<u2009_x__p_2u2009+u2009_y__p_2 and _v__p_u2009<u2009_v_. Output Print a single real number β€” the minimum possible delivery time. The answer will be considered valid if its absolute or relative error does not exceed 10u2009-u20096.
2,400
false
false
false
false
false
false
false
true
false
false
9,049
1603C
For an array $$$b$$$ of $$$n$$$ integers, the extreme value of this array is the minimum number of times (possibly, zero) the following operation has to be performed to make $$$b$$$ non-decreasing: Select an index $$$i$$$ such that $$$1 le i le b$$$, where $$$b$$$ is the current length of $$$b$$$. Replace $$$b_i$$$ with two elements $$$x$$$ and $$$y$$$ such that $$$x$$$ and $$$y$$$ both are positive integers and $$$x + y = b_i$$$. This way, the array $$$b$$$ changes and the next operation is performed on this modified array. For example, if $$$b = [2, 4, 3]$$$ and index $$$2$$$ gets selected, then the possible arrays after this operation are $$$[2, underline{1}, underline{3}, 3]$$$, $$$[2, underline{2}, underline{2}, 3]$$$, or $$$[2, underline{3}, underline{1}, 3]$$$. And consequently, for this array, this single operation is enough to make it non-decreasing: $$$[2, 4, 3] ightarrow [2, underline{2}, underline{2}, 3]$$$. It's easy to see that every array of positive integers can be made non-decreasing this way. YouKn0wWho has an array $$$a$$$ of $$$n$$$ integers. Help him find the sum of extreme values of all nonempty subarrays of $$$a$$$ modulo $$$998,244,353$$$. If a subarray appears in $$$a$$$ multiple times, its extreme value should be counted the number of times it appears. An array $$$d$$$ is a subarray of an array $$$c$$$ if $$$d$$$ can be obtained from $$$c$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, print a single integer xa0β€” the sum of extreme values of all subarrays of $$$a$$$ modulo $$$998,244,353$$$. Note Let $$$f(l, r)$$$ denote the extreme value of $$$[a_l, a_{l+1}, ldots, a_r]$$$. In the first test case, $$$f(1, 3) = 3$$$, because YouKn0wWho can perform the following operations on the subarray $$$[5, 4, 3]$$$ (the newly inserted elements are underlined):$$$[5, 4, 3] ightarrow [underline{3}, underline{2}, 4, 3] ightarrow [3, 2, underline{2}, underline{2}, 3] ightarrow [underline{1}, underline{2}, 2, 2, 2, 3]$$$; $$$f(1, 2) = 1$$$, because $$$[5, 4] ightarrow [underline{2}, underline{3}, 4]$$$; $$$f(2, 3) = 1$$$, because $$$[4, 3] ightarrow [underline{1}, underline{3}, 3]$$$; $$$f(1, 1) = f(2, 2) = f(3, 3) = 0$$$, because they are already non-decreasing. So the total sum of extreme values of all subarrays of $$$a = 3 + 1 + 1 + 0 + 0 + 0 = 5$$$.
2,300
true
true
false
true
false
false
false
false
false
false
2,641
1637A
You have an array $$$a$$$ of length $$$n$$$. You can exactly once select an integer $$$len$$$ between $$$1$$$ and $$$n - 1$$$ inclusively, and then sort in non-decreasing order the prefix of the array of length $$$len$$$ and the suffix of the array of length $$$n - len$$$ independently. For example, if the array is $$$a = [3, 1, 4, 5, 2]$$$, and you choose $$$len = 2$$$, then after that the array will be equal to $$$[1, 3, 2, 4, 5]$$$. Could it be that after performing this operation, the array will not be sorted in non-decreasing order? 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 one integer $$$n$$$ ($$$2 leq n leq 10^4$$$)xa0β€” the length of the array. The second line of the test case contains a sequence of integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) β€” the array elements. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Output For each test case of input data, output "YES" (without quotes), if the array may be not sorted in non-decreasing order, output "NO" (without quotes) otherwise. You can output each letter in any case (uppercase or lowercase). Example Input 3 3 2 2 1 4 3 1 2 1 5 1 2 2 4 4 Note In the first test case, it's possible to select $$$len = 1$$$, then after operation, the array will not be sorted in non-decreasing order and will be equal to $$$[2, 1, 2]$$$. In the second test case, it's possible to select $$$len = 3$$$, then after operation, the array will not be sorted in non-decreasing order and will be equal to $$$[1, 2, 3, 1]$$$. In the third test case, the array will be sorted in non-decreasing order for every possible $$$len$$$.
800
false
false
false
false
false
false
true
false
true
false
2,434
1458E
After your debut mobile game "Nim" blew up, you decided to make a sequel called "Nim 2". This game will expand on the trusted Nim game formula, adding the much awaited second heap! In the game, there are two heaps, each containing a non-negative number of stones. Two players make moves in turn. On their turn, a player can take any positive number of stones from either one of the heaps. A player who is unable to move loses the game. To make the game easier to playtest, you've introduced developer shortcuts. There are $$$n$$$ shortcut positions $$$(x_1, y_1), ldots, (x_n, y_n)$$$. These change the game as follows: suppose that before a player's turn the first and second heap contain $$$x$$$ and $$$y$$$ stones respectively. If the pair $$$(x, y)$$$ is equal to one of the pairs $$$(x_i, y_i)$$$, then the player about to move loses instantly, otherwise they are able to make moves as normal. Note that in the above explanation the two heaps and all pairs are ordered, that is, $$$x$$$ must refer to the size of the first heap, and $$$y$$$ must refer to the size of the second heap. The game release was followed by too much celebration, and next thing you know is developer shortcuts made their way to the next official update of the game! Players now complain that the AI opponent has become unbeatable at certain stages of the game. You now have to write a program to figure out which of the given initial positions can be won by the starting player, assuming both players act optimally. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^5$$$)xa0β€” the number of shortcut positions, and the number of initial positions that need to be evaluated. The following $$$n$$$ lines describe shortcut positions. The $$$i$$$-th of these lines contains two integers $$$x_i, y_i$$$ ($$$0 leq x_i, y_i leq 10^9$$$). It is guaranteed that all shortcut positions are distinct. The following $$$m$$$ lines describe initial positions. The $$$i$$$-th of these lines contains two integers $$$a_i, b_i$$$ ($$$0 leq a_i, b_i leq 10^9$$$)xa0β€” the number of stones in the first and second heap respectively. It is guaranteed that all initial positions are distinct. However, initial positions are not necessarily distinct from shortcut positions. Output For each initial position, on a separate line print "WIN" if the starting player is able to win from this position, and "LOSE" otherwise.
3,100
false
false
false
false
true
false
false
false
false
false
3,390
1569D
There is a city that can be represented as a square grid with corner points in $$$(0, 0)$$$ and $$$(10^6, 10^6)$$$. The city has $$$n$$$ vertical and $$$m$$$ horizontal streets that goes across the whole city, i.xa0e. the $$$i$$$-th vertical streets goes from $$$(x_i, 0)$$$ to $$$(x_i, 10^6)$$$ and the $$$j$$$-th horizontal street goes from $$$(0, y_j)$$$ to $$$(10^6, y_j)$$$. All streets are bidirectional. Borders of the city are streets as well. There are $$$k$$$ persons staying on the streets: the $$$p$$$-th person at point $$$(x_p, y_p)$$$ (so either $$$x_p$$$ equal to some $$$x_i$$$ or $$$y_p$$$ equal to some $$$y_j$$$, or both). Let's say that a pair of persons form an inconvenient pair if the shortest path from one person to another going only by streets is strictly greater than the Manhattan distance between them. Calculate the number of inconvenient pairs of persons (pairs $$$(x, y)$$$ and $$$(y, x)$$$ are the same pair). Let's recall that Manhattan distance between points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$x_1 - x_2 + y_1 - y_2$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n, m le 2 cdot 10^5$$$; $$$2 le k le 3 cdot 10^5$$$)xa0β€” the number of vertical and horizontal streets and the number of persons. The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$0 = x_1 < x_2 < dots < x_{n - 1} < x_n = 10^6$$$)xa0β€” the $$$x$$$-coordinates of vertical streets. The third line contains $$$m$$$ integers $$$y_1, y_2, dots, y_m$$$ ($$$0 = y_1 < y_2 < dots < y_{m - 1} < y_m = 10^6$$$)xa0β€” the $$$y$$$-coordinates of horizontal streets. Next $$$k$$$ lines contains description of people. The $$$p$$$-th line contains two integers $$$x_p$$$ and $$$y_p$$$ ($$$0 le x_p, y_p le 10^6$$$; $$$x_p in {x_1, dots, x_n}$$$ or $$$y_p in {y_1, dots, y_m}$$$)xa0β€” the coordinates of the $$$p$$$-th person. All points are distinct. It guaranteed that sum of $$$n$$$ doesn't exceed $$$2 cdot 10^5$$$, sum of $$$m$$$ doesn't exceed $$$2 cdot 10^5$$$ and sum of $$$k$$$ doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print the number of inconvenient pairs. Example Input 2 2 2 4 0 1000000 0 1000000 1 0 1000000 1 999999 1000000 0 999999 5 4 9 0 1 2 6 1000000 0 4 8 1000000 4 4 2 5 2 2 6 3 1000000 1 3 8 5 8 8 8 6 8 Note The second test case is pictured below: For example, points $$$3$$$ and $$$4$$$ form an inconvenient pair, since the shortest path between them (shown red and equal to $$$7$$$) is greater than its Manhattan distance (equal to $$$5$$$). Points $$$3$$$ and $$$5$$$ also form an inconvenient pair: the shortest path equal to $$$1000001$$$ (shown green) is greater than the Manhattan distance equal to $$$999999$$$. But points $$$5$$$ and $$$9$$$ don't form an inconvenient pair, since the shortest path (shown purple) is equal to its Manhattan distance.
1,900
false
false
true
false
true
false
false
true
true
false
2,795
505C
The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from 0 to 30000 from the west to the east. These islands are known to contain many treasures. There are _n_ gems in the Shuseki Islands in total, and the _i_-th gem is located on island _p__i_. Mr. Kitayuta has just arrived at island 0. With his great jumping ability, he will repeatedly perform jumps between islands to the east according to the following process: First, he will jump from island 0 to island _d_. After that, he will continue jumping according to the following rule. Let _l_ be the length of the previous jump, that is, if his previous jump was from island _prev_ to island _cur_, let _l_u2009=u2009_cur_u2009-u2009_prev_. He will perform a jump of length _l_u2009-u20091, _l_ or _l_u2009+u20091 to the east. That is, he will jump to island (_cur_u2009+u2009_l_u2009-u20091), (_cur_u2009+u2009_l_) or (_cur_u2009+u2009_l_u2009+u20091) (if they exist). The length of a jump must be positive, that is, he cannot perform a jump of length 0 when _l_u2009=u20091. If there is no valid destination, he will stop jumping. Mr. Kitayuta will collect the gems on the islands visited during the process. Find the maximum number of gems that he can collect. Input The first line of the input contains two space-separated integers _n_ and _d_ (1u2009≀u2009_n_,u2009_d_u2009≀u200930000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively. The next _n_ lines describe the location of the gems. The _i_-th of them (1u2009≀u2009_i_u2009≀u2009_n_) contains a integer _p__i_ (_d_u2009≀u2009_p_1u2009≀u2009_p_2u2009≀u2009...u2009≀u2009_p__n_u2009≀u200930000), denoting the number of the island that contains the _i_-th gem. Output Print the maximum number of gems that Mr. Kitayuta can collect. Examples Input 13 7 8 8 9 16 17 17 18 21 23 24 24 26 30 Note In the first sample, the optimal route is 0 u2009β†’u2009 10 (+1 gem) u2009β†’u2009 19 u2009β†’u2009 27 (+2 gems) u2009β†’u2009...x01 In the second sample, the optimal route is 0 u2009β†’u2009 8 u2009β†’u2009 15 u2009β†’u2009 21u2009β†’u2009 28 (+1 gem) u2009β†’u2009 36 (+1 gem) u2009β†’u2009 45 (+1 gem) u2009β†’u2009 55 (+1 gem) u2009β†’u2009 66 (+1 gem) u2009β†’u2009 78 (+1 gem) u2009β†’u2009... In the third sample, the optimal route is 0 u2009β†’u2009 7 u2009β†’u2009 13 u2009β†’u2009 18 (+1 gem) u2009β†’u2009 24 (+2 gems) u2009β†’u2009 30 (+1 gem) u2009β†’u2009...
1,900
false
false
false
true
false
false
false
false
false
false
7,821
707B
Masha wants to open her own bakery and bake muffins in one of the _n_ cities numbered from 1 to _n_. There are _m_ bidirectional roads, each of whose connects some pair of cities. To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only _k_ storages, located in different cities numbered _a_1,u2009_a_2,u2009...,u2009_a__k_. Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another _n_u2009-u2009_k_ cities, and, of course, flour delivery should be paidxa0β€” for every kilometer of path between storage and bakery Masha should pay 1 ruble. Formally, Masha will pay _x_ roubles, if she will open the bakery in some city _b_ (_a__i_u2009β‰ u2009_b_ for every 1u2009≀u2009_i_u2009≀u2009_k_) and choose a storage in some city _s_ (_s_u2009=u2009_a__j_ for some 1u2009≀u2009_j_u2009≀u2009_k_) and _b_ and _s_ are connected by some path of roads of summary length _x_ (if there are more than one path, Masha is able to choose which of them should be used). Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of _k_ storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount. Input The first line of the input contains three integers _n_, _m_ and _k_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009105, 0u2009≀u2009_k_u2009≀u2009_n_)xa0β€” the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively. Then _m_ lines follow. Each of them contains three integers _u_, _v_ and _l_ (1u2009≀u2009_u_,u2009_v_u2009≀u2009_n_, 1u2009≀u2009_l_u2009≀u2009109, _u_u2009β‰ u2009_v_) meaning that there is a road between cities _u_ and _v_ of length of _l_ kilometers . If _k_u2009>u20090, then the last line of the input contains _k_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__k_ (1u2009≀u2009_a__i_u2009≀u2009_n_)xa0β€” the number of cities having flour storage located in. If _k_u2009=u20090 then this line is not presented in the input. Output Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line. If the bakery can not be opened (while satisfying conditions) in any of the _n_ cities, print u2009-u20091 in the only line.
1,300
false
false
false
false
false
false
false
false
false
true
6,993
1485A
Problem - 1485A - 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 greedy math number theory *1000 No tag edit access β†’ Contest materials ") ") $$$b=b+1$$$ (increase $$$b$$$ by $$$1$$$) Find the minimum number of operations required to make $$$a=0$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) β€” the number of test cases. The only line of the description of each test case contains two integers $$$a$$$, $$$b$$$ ($$$1 le a,b le 10^9$$$). Output For each test case, print a single integer: the minimum number of operations required to make $$$a=0$$$. Example Input 6 9 2 1337 1 1 1 50000000 4 991026972 997 1234 5678 Output 4 9 2 12 3 1 Note In the first test case, one of the optimal solutions is: 1. Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 4$$$ and $$$b = 2$$$. 2. Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 2$$$ and $$$b = 2$$$. 3. Increase $$$b$$$. After this operation $$$a = 2$$$ and $$$b = 3$$$. 4. Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 0$$$ and $$$b = 3$$$.
1,000
true
true
false
false
false
false
true
false
false
false
3,259
915A
Luba thinks about watering her garden. The garden can be represented as a segment of length _k_. Luba has got _n_ buckets, the _i_-th bucket allows her to water some continuous subsegment of garden of length exactly _a__i_ each hour. Luba can't water any parts of the garden that were already watered, also she can't water the ground outside the garden. Luba has to choose one of the buckets in order to water the garden as fast as possible (as mentioned above, each hour she will water some continuous subsegment of length _a__i_ if she chooses the _i_-th bucket). Help her to determine the minimum number of hours she has to spend watering the garden. It is guaranteed that Luba can always choose a bucket so it is possible water the garden. See the examples for better understanding. Input The first line of input contains two integer numbers _n_ and _k_ (1u2009≀u2009_n_,u2009_k_u2009≀u2009100) β€” the number of buckets and the length of the garden, respectively. The second line of input contains _n_ integer numbers _a__i_ (1u2009≀u2009_a__i_u2009≀u2009100) β€” the length of the segment that can be watered by the _i_-th bucket in one hour. It is guaranteed that there is at least one bucket such that it is possible to water the garden in integer number of hours using only this bucket. Output Print one integer number β€” the minimum number of hours required to water the garden. Note In the first test the best option is to choose the bucket that allows to water the segment of length 3. We can't choose the bucket that allows to water the segment of length 5 because then we can't water the whole garden. In the second test we can choose only the bucket that allows us to water the segment of length 1.
900
false
false
true
false
false
false
false
false
false
false
6,075
1881A
Given a string $$$x$$$ of length $$$n$$$ and a string $$$s$$$ of length $$$m$$$ ($$$n cdot m le 25$$$), consisting of lowercase Latin letters, you can apply any number of operations to the string $$$x$$$. In one operation, you append the current value of $$$x$$$ to the end of the string $$$x$$$. Note that the value of $$$x$$$ will change after this. For example, if $$$x =$$$"aba", then after applying operations, $$$x$$$ will change as follows: "aba" $$$ ightarrow$$$ "abaaba" $$$ ightarrow$$$ "abaabaabaaba". After what minimum number of operations $$$s$$$ will appear in $$$x$$$ as a substring? A substring of a string is defined as a contiguous segment of it. Input The first line of the input 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 two numbers $$$n$$$ and $$$m$$$ ($$$1 le n cdot m le 25$$$)xa0β€” the lengths of strings $$$x$$$ and $$$s$$$, respectively. The second line of each test case contains the string $$$x$$$ of length $$$n$$$. The third line of each test case contains the string $$$s$$$ of length $$$m$$$. Output For each test case, output a single numberxa0β€” the minimum number of operations after which $$$s$$$ will appear in $$$x$$$ as a substring. If this is not possible, output $$$-1$$$. Example Input 12 1 5 a aaaaa 5 5 eforc force 2 5 ab ababa 3 5 aba ababa 4 3 babb bbb 5 1 aaaaa a 4 2 aabb ba 2 8 bk kbkbkbkb 12 2 fjdgmujlcont tf 2 2 aa aa 3 5 abb babba 1 19 m mmmmmmmmmmmmmmmmmmm Output 3 1 2 -1 1 0 1 3 1 0 2 5 Note In the first test case of the example, after $$$2$$$ operations, the string will become "aaaa", and after $$$3$$$ operations, it will become "aaaaaaaa", so the answer is $$$3$$$. In the second test case of the example, after applying $$$1$$$ operation, the string will become "$$$ ext{e}color{red}{ ext{force}} ext{forc}$$$", where the substring is highlighted in red. In the fourth test case of the example, it can be shown that it is impossible to obtain the desired string as a substring.
800
false
false
false
false
false
false
true
false
false
false
1,004
776F
Sherlock met Moriarty for a final battle of wits. He gave him a regular _n_ sided convex polygon. In addition to it, he gave him certain diagonals to form regions on the polygon. It was guaranteed that the diagonals did not intersect in interior points. He took each of the region and calculated its importance value. Importance value for a region formed by vertices _a_1,u2009_a_2,u2009... ,u2009_a__x_ of the polygon will be given by 2_a_1u2009+u20092_a_2u2009+u2009...u2009+u20092_a__x_. Then, he sorted these regions on the basis of their importance value in ascending order. After that he assigned each region an index from 1 to _k_, where _k_ is the number of regions, and index of region is its position in the sorted array calculated above. He wants Moriarty to color the regions using not more than 20 colors, such that two regions have same color only if all the simple paths between these two regions have at least one region with color value less than the color value assigned to these regions. Simple path between two regions _f_ and _h_ is a sequence of regions _r_1,u2009_r_2,u2009... _r__t_ such that _r_1u2009=u2009_f_, _r__t_u2009=u2009_h_, for each 1u2009≀u2009_i_u2009<u2009_t_ regions _r__i_ and _r__i_u2009+u20091 share an edge, and _r__i_u2009=u2009_r__j_ if and only if _i_u2009=u2009_j_. Moriarty couldn't answer and asks Sherlock to solve it himself. Help Sherlock in doing so. Input First line contains two integers _n_ and _m_ (3u2009≀u2009_n_u2009≀u2009100000, 0u2009≀u2009_m_u2009≀u2009_n_u2009-u20093), the number of vertices in the polygon and the number of diagonals added. Each of the next _m_ lines contains two integers _a_ and _b_ (1u2009≀u2009_a_,u2009_b_u2009≀u2009_n_), describing a diagonal between vertices _a_ and _b_. It is guaranteed that the diagonals are correct, i.xa0e. _a_ and _b_ don't coincide and are not neighboring. It is guaranteed that the diagonals do not intersect. Output Let the number of regions be _k_. Output _k_ space-separated integers, each between 1 and 20, representing the colors of the regions in the order of increasing importance. If there are multiple answers, print any of them. It can be shown that at least one answer exists. Note In 2nd input, regions formed in order after sorting will be (1,u20092,u20093), (1,u20093,u20094), (1,u20094,u20095), (1,u20095,u20096), i.e, region (1,u20092,u20093) is first region followed by region (1,u20093,u20094) and so on. So, we can color regions 1 and 3 with same color, as region number 2 is on the path from 1 to 3 and it has color 1 which is less than color of 1 and 3, i.e., color number 2.
2,800
false
false
true
false
true
true
false
false
false
true
6,663
1923A
There is a ribbon divided into $$$n$$$ cells, numbered from $$$1$$$ to $$$n$$$ from left to right. Each cell either contains a chip or is free. You can perform the following operation any number of times (possibly zero): choose a chip and move it to the closest free cell to the left. You can choose any chip that you want, provided that there is at least one free cell to the left of it. When you move the chip, the cell where it was before the operation becomes free. Your goal is to move the chips in such a way that they form a single block, without any free cells between them. What is the minimum number of operations you have to perform? Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) β€” the number of test cases. Each test case consists of two lines: the first line contains one integer $$$n$$$ ($$$2 le n le 50$$$) β€” the number of cells; the second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 1$$$); $$$a_i = 0$$$ means that the $$$i$$$-th cell is free; $$$a_i = 1$$$ means that the $$$i$$$-th cell contains a chip. Additional constraint on the input: in each test case, at least one cell contains a chip. Output For each test case, print one integer β€” the minimum number of operations you have to perform so that all chips form a single block without any free cells between them. Example Input 5 8 0 1 1 1 0 1 1 0 6 0 1 0 0 0 0 6 1 1 1 1 1 1 5 1 0 1 0 1 9 0 1 1 0 0 0 1 1 0 Note In the first example, you can perform the operation on the chip in the $$$7$$$-th cell. The closest free cell to the left is the $$$5$$$-th cell, so it moves there. After that, all chips form a single block. In the second example, all chips are already in a single block. Same for the third example.
800
false
true
true
false
false
false
false
false
false
false
752
183D
You are going to work in Codeforces as an intern in a team of _n_ engineers, numbered 1 through _n_. You want to give each engineer a souvenir: a T-shirt from your country (T-shirts are highly desirable in Codeforces). Unfortunately you don't know the size of the T-shirt each engineer fits in. There are _m_ different sizes, numbered 1 through _m_, and each engineer will fit in a T-shirt of exactly one size. You don't know the engineers' exact sizes, so you asked your friend, Gerald. Unfortunately, he wasn't able to obtain the exact sizes either, but he managed to obtain for each engineer _i_ and for all sizes _j_, the probability that the size of the T-shirt that fits engineer _i_ is _j_. Since you're planning to give each engineer one T-shirt, you are going to bring with you exactly _n_ T-shirts. For those _n_ T-shirts, you can bring any combination of sizes (you can bring multiple T-shirts with the same size too!). You don't know the sizes of T-shirts for each engineer when deciding what sizes to bring, so you have to pick this combination based only on the probabilities given by your friend, Gerald. Your task is to maximize the expected number of engineers that receive a T-shirt of his size. This is defined more formally as follows. When you finally arrive at the office, you will ask each engineer his T-shirt size. Then, if you still have a T-shirt of that size, you will give him one of them. Otherwise, you don't give him a T-shirt. You will ask the engineers in order starting from engineer 1, then engineer 2, and so on until engineer _n_. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≀u2009_n_u2009≀u20093000,u20091u2009≀u2009_m_u2009≀u2009300), denoting the number of engineers and the number of T-shirt sizes, respectively. Then _n_ lines follow, each line contains _m_ space-separated integers. The _j_-th integer in the _i_-th line represents the probability that the _i_-th engineer fits in a T-shirt of size _j_. Each probability will be given as an integer between 0 and 1000, inclusive. The actual probability should be calculated as the given number divided by 1000. It is guaranteed that for any engineer, the sum of the probabilities for all _m_ T-shirts is equal to one. Output Print a single real number denoting the maximum possible expected number of engineers that will receive a T-shirt. For the answer the absolute or relative error of 10u2009-u20099 is acceptable. Examples Input 3 3 1000 0 0 1000 0 0 0 1000 0 Note For the first example, bring one T-shirt of each size. With 0.5 chance, either both engineers fit inside T-shirts of size 1 or both fit inside T-shirts of size 2. With the other 0.5 chance, one engineer fits inside a T-shirt of size 1 and the other inside a T-shirt of size 2. If the first is true, the number of engineers that receive a T-shirt is one. If the second is true, the number of such engineers is two. Hence, the expected number of engineers who receive a T-shirt is 1.5. This is maximum possible expected number of engineers for all sets of T-shirts. For the second example, bring two T-shirts of size 1 and one T-shirt of size 2. This way, each engineer will definitely receive a T-shirt of his size. For the third example, bring one T-shirt of size 4.
2,700
false
true
false
true
false
false
false
false
false
false
9,106
1714E
You are given an array of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ You can apply the following operation an arbitrary number of times: select an index $$$i$$$ ($$$1 le i le n$$$) and replace the value of the element $$$a_i$$$ with the value $$$a_i + (a_i bmod 10)$$$, where $$$a_i bmod 10$$$ is the remainder of the integer dividing $$$a_i$$$ by $$$10$$$. For a single index (value $$$i$$$), this operation can be applied multiple times. If the operation is applied repeatedly to the same index, then the current value of $$$a_i$$$ is taken into account each time. For example, if $$$a_i=47$$$ then after the first operation we get $$$a_i=47+7=54$$$, and after the second operation we get $$$a_i=54+4=58$$$. Check if it is possible to make all array elements equal by applying multiple (possibly zero) operations. For example, you have an array $$$[6, 11]$$$. Let's apply this operation to the first element of the array. Let's replace $$$a_1 = 6$$$ with $$$a_1 + (a_1 bmod 10) = 6 + (6 bmod 10) = 6 + 6 = 12$$$. We get the array $$$[12, 11]$$$. Then apply this operation to the second element of the array. Let's replace $$$a_2 = 11$$$ with $$$a_2 + (a_2 bmod 10) = 11 + (11 bmod 10) = 11 + 1 = 12$$$. We get the array $$$[12, 12]$$$. Thus, by applying $$$2$$$ operations, you can make all elements of an array equal. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. What follows is a description of each test case. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 le a_i le 10^9$$$)xa0β€” array elements. 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: YES if it is possible to make all array elements equal; NO otherwise. You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer) . Example Input 10 2 6 11 3 2 18 22 5 5 10 5 10 5 4 1 2 4 8 2 4 5 3 93 96 102 2 40 6 2 50 30 2 22 44 2 1 5 Output Yes No Yes Yes No Yes No No Yes No Note The first test case is clarified above. In the second test case, it is impossible to make all array elements equal. In the third test case, you need to apply this operation once to all elements equal to $$$5$$$. In the fourth test case, you need to apply this operation to all elements until they become equal to $$$8$$$. In the fifth test case, it is impossible to make all array elements equal. In the sixth test case, you need to apply this operation to all elements until they become equal to $$$102$$$.
1,400
true
false
false
false
false
false
true
false
false
false
1,996
1641E
You are given an array $$$a$$$ of length $$$n$$$. Also you are given $$$m$$$ distinct positions $$$p_1, p_2, ldots, p_m$$$ ($$$1 leq p_i leq n$$$). A non-empty subset of these positions $$$T$$$ is randomly selected with equal probability and the following value is calculated: $$$$$$sum_{i=1}^{n} (a_i cdot min_{j in T} lefti - j ight).$$$$$$ In other word, for each index of the array, $$$a_i$$$ and the distance to the closest chosen position are multiplied, and then these values are summed up. Find the expected value of this sum. This value must be found modulo $$$998,244,353$$$. More formally, let $$$M = 998,244,353$$$. It can be shown that the answer can be represented as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q eq 0$$$ (mod $$$M$$$). Output the integer equal to $$$p cdot q^{-1}$$$ (mod $$$M$$$). In other words, output such integer $$$x$$$ that $$$0 leq x < M$$$ and $$$x cdot q = p$$$ (mod $$$M$$$). Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq m leq n leq 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i < 998,244,353$$$). The third line contains $$$m$$$ distinct integers $$$p_1, p_2, ldots, p_m$$$ ($$$1 leq p_i le n$$$). For every $$$1 leq i < m$$$ it is guaranteed that $$$p_i < p_{i+1}$$$. Output Print a single integerxa0β€” the answer to the problem. Examples Input 6 6 4 2 4 2 4 2 1 2 3 4 5 6 Note In the first test: If only $$$1$$$ is choosen, than the value equals to $$$1 cdot 0 + 2 cdot 1 + 3 cdot 2 + 4 cdot 3 = 20$$$. If only $$$4$$$ is choosen, than the value equals to $$$1 cdot 3 + 2 cdot 2 + 3 cdot 1 + 4 cdot 0 = 10$$$. If both positions are chosen, than the value equals to $$$1 cdot 0 + 2 cdot 1 + 3 cdot 1 + 4 cdot 0 = 5$$$. The answer to the problem is $$$frac{20 + 10 + 5}{3} = frac{35}{3} = 665,496,247$$$ (modulo $$$998,244,353$$$).
3,300
true
false
false
false
false
false
false
false
false
false
2,405
903A
Problem - 903A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags greedy implementation *900 No tag edit access β†’ Contest materials β€” the number of testcases. The _i_-th of the following _n_ lines contains one integer _x__i_ (1u2009≀u2009_x__i_u2009≀u2009100) β€” the number of chicken chunks Ivan wants to eat. Output Print _n_ lines, in _i_-th line output YES if Ivan can buy exactly _x__i_ chunks. Otherwise, print NO. Example Input 2 6 5 Output YES NO Note In the first example Ivan can buy two small portions. In the second example Ivan cannot buy exactly 5 chunks, since one small portion is not enough, but two small portions or one large is too much.
900
false
true
true
false
false
false
false
false
false
false
6,134
1902D
There is an infinite $$$2$$$-dimensional grid. Initially, a robot stands in the point $$$(0, 0)$$$. The robot can execute four commands: Uxa0β€” move from point $$$(x, y)$$$ to $$$(x, y + 1)$$$; Dxa0β€” move from point $$$(x, y)$$$ to $$$(x, y - 1)$$$; Lxa0β€” move from point $$$(x, y)$$$ to $$$(x - 1, y)$$$; Rxa0β€” move from point $$$(x, y)$$$ to $$$(x + 1, y)$$$. You are given a sequence of commands $$$s$$$ of length $$$n$$$. Your task is to answer $$$q$$$ independent queries: given four integers $$$x$$$, $$$y$$$, $$$l$$$ and $$$r$$$; determine whether the robot visits the point $$$(x, y)$$$, while executing a sequence $$$s$$$, but the substring from $$$l$$$ to $$$r$$$ is reversed (i.u2009e. the robot performs commands in order $$$s_1 s_2 s_3 dots s_{l-1} s_r s_{r-1} s_{r-2} dots s_l s_{r+1} s_{r+2} dots s_n$$$). Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$)xa0β€” the length of the command sequence and the number of queries, respectively. The second line contains a string $$$s$$$ of length $$$n$$$, consisting of characters U, D, L and/or R. Then $$$q$$$ lines follow, the $$$i$$$-th of them contains four integers $$$x_i$$$, $$$y_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$-n le x_i, y_i le n$$$; $$$1 le l le r le n$$$) describing the $$$i$$$-th query. Output For each query, print YES if the robot visits the point $$$(x, y)$$$, while executing a sequence $$$s$$$, but the substring from $$$l$$$ to $$$r$$$ is reversed; otherwise print NO. Examples Input 8 3 RDLLUURU -1 2 1 7 0 0 3 4 0 1 7 8 Input 4 2 RLDU 0 0 2 2 -1 -1 2 3 Input 10 6 DLUDLRULLD -1 0 1 10 -1 -2 2 5 -4 -2 6 10 -1 0 3 9 0 1 4 7 -3 -1 5 8 Output YES YES YES NO YES YES Note In the first query of the first sample, the path of the robot looks as follows: In the second query of the first sample, the path of the robot looks as follows: In the third query of the first sample, the path of the robot looks as follows:
1,900
false
false
true
true
true
false
false
true
false
false
900
1168A
Toad Zitz has an array of integers, each integer is between $$$0$$$ and $$$m-1$$$ inclusive. The integers are $$$a_1, a_2, ldots, a_n$$$. In one operation Zitz can choose an integer $$$k$$$ and $$$k$$$ indices $$$i_1, i_2, ldots, i_k$$$ such that $$$1 leq i_1 < i_2 < ldots < i_k leq n$$$. He should then change $$$a_{i_j}$$$ to $$$((a_{i_j}+1) bmod m)$$$ for each chosen integer $$$i_j$$$. The integer $$$m$$$ is fixed for all operations and indices. Here $$$x bmod y$$$ denotes the remainder of the division of $$$x$$$ by $$$y$$$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 300,000$$$)xa0β€” the number of integers in the array and the parameter $$$m$$$. The next line contains $$$n$$$ space-separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i < m$$$)xa0β€” the given array. Output Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $$$0$$$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. Note In the first example, the array is already non-decreasing, so the answer is $$$0$$$. In the second example, you can choose $$$k=2$$$, $$$i_1 = 2$$$, $$$i_2 = 5$$$, the array becomes $$$[0,0,1,3,3]$$$. It is non-decreasing, so the answer is $$$1$$$.
1,700
false
true
false
false
false
false
false
true
false
false
4,876
292B
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of _n_ computers, some of them are connected by a cable. The computers are indexed by integers from 1 to _n_. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers _n_ and _m_ (4u2009≀u2009_n_u2009≀u2009105;xa03u2009≀u2009_m_u2009≀u2009105) β€” the number of nodes and edges in the graph, correspondingly. Next _m_ lines contain the description of the graph's edges. The _i_-th line contains a space-separated pair of integers _x__i_, _y__i_ (1u2009≀u2009_x__i_,u2009_y__i_u2009≀u2009_n_) β€” the numbers of nodes that are connected by the _i_-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes).
1,200
false
false
true
false
false
false
false
false
false
true
8,665
1334B
Many years ago Berland was a small country where only $$$n$$$ people lived. Each person had some savings: the $$$i$$$-th one had $$$a_i$$$ burles. The government considered a person as wealthy if he had at least $$$x$$$ burles. To increase the number of wealthy people Berland decided to carry out several reforms. Each reform looked like that: the government chooses some subset of people (maybe all of them); the government takes all savings from the chosen people and redistributes the savings among the chosen people equally. For example, consider the savings as list $$$[5, 1, 2, 1]$$$: if the government chose the $$$1$$$-st and the $$$3$$$-rd persons then it, at first, will take all $$$5 + 2 = 7$$$ burles and after that will return $$$3.5$$$ burles to the chosen people. As a result, the savings will become $$$[3.5, 1, 3.5, 1]$$$. A lot of data was lost from that time, so we don't know how many reforms were implemented and to whom. All we can do is ask you to calculate the maximum possible number of wealthy people after several (maybe zero) reforms. Input The first line contains single integer $$$T$$$ ($$$1 le T le 1000$$$) β€” the number of test cases. Next $$$2T$$$ lines contain the test cases β€” two lines per test case. The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 10^5$$$, $$$1 le x le 10^9$$$) β€” the number of people and the minimum amount of money to be considered as wealthy. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) β€” the initial savings of each person. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$10^5$$$. Output Print $$$T$$$ integers β€” one per test case. For each test case print the maximum possible number of wealthy people after several (maybe zero) reforms. Example Input 4 4 3 5 1 2 1 4 10 11 9 11 9 2 5 4 3 3 7 9 4 9 Note The first test case is described in the statement. In the second test case, the government, for example, could carry out two reforms: $$$[underline{11}, underline{9}, 11, 9] ightarrow [10, 10, underline{11}, underline{9}] ightarrow [10, 10, 10, 10]$$$. In the third test case, the government couldn't make even one person wealthy. In the fourth test case, the government could choose all people to carry out a reform: $$$[underline{9}, underline{4}, underline{9}] ightarrow [7frac{1}{3}, 7frac{1}{3}, 7frac{1}{3}]$$$.
1,100
false
true
false
false
false
false
false
false
true
false
4,036
117A
And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All _n_ participants who have made it to the finals found themselves in a huge _m_-floored 108-star hotel. Of course the first thought to come in a place like this is "How about checking out the elevator?". The hotel's elevator moves between floors according to one never changing scheme. Initially (at the moment of time 0) the elevator is located on the 1-st floor, then it moves to the 2-nd floor, then β€” to the 3-rd floor and so on until it reaches the _m_-th floor. After that the elevator moves to floor _m_u2009-u20091, then to floor _m_u2009-u20092, and so on until it reaches the first floor. This process is repeated infinitely. We know that the elevator has infinite capacity; we also know that on every floor people get on the elevator immediately. Moving between the floors takes a unit of time. For each of the _n_ participant you are given _s__i_, which represents the floor where the _i_-th participant starts, _f__i_, which represents the floor the _i_-th participant wants to reach, and _t__i_, which represents the time when the _i_-th participant starts on the floor _s__i_. For each participant print the minimum time of his/her arrival to the floor _f__i_. If the elevator stops on the floor _s__i_ at the time _t__i_, then the _i_-th participant can enter the elevator immediately. If the participant starts on the floor _s__i_ and that's the floor he wanted to reach initially (_s__i_u2009=u2009_f__i_), then the time of arrival to the floor _f__i_ for this participant is considered equal to _t__i_. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≀u2009_n_u2009≀u2009105,u20092u2009≀u2009_m_u2009≀u2009108). Next _n_ lines contain information about the participants in the form of three space-separated integers _s__i_ _f__i_ _t__i_ (1u2009≀u2009_s__i_,u2009_f__i_u2009≀u2009_m_,u20090u2009≀u2009_t__i_u2009≀u2009108), described in the problem statement. Note Let's consider the first sample. The first participant starts at floor _s_u2009=u20092 by the time equal to _t_u2009=u20093. To get to the floor _f_u2009=u20094, he has to wait until the time equals 7, that's the time when the elevator will go upwards for the second time. Then the first participant should get on the elevator and go two floors up. In this case the first participant gets to the floor _f_ at time equal to 9. The second participant starts at the time _t_u2009=u20090 on the floor _s_u2009=u20091, enters the elevator immediately, and arrives to the floor _f_u2009=u20092. The third participant doesn't wait for the elevator, because he needs to arrive to the same floor where he starts.
1,300
true
false
true
false
false
false
false
false
false
false
9,421
2036E
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 984 (Div. 3) Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags binary search constructive algorithms data structures greedy *1600 No tag edit access β†’ Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST E. Reverse the Rivers time limit per test2 seconds memory limit per test256 megabytes A conspiracy of ancient sages, who decided to redirect rivers for their own convenience, has put the world on the brink. But before implementing their grand plan, they decided to carefully think through their strategy β€” that's what sages do. There are $$$n$$$ countries, each with exactly $$$k$$$ regions. For the $$$j$$$-th region of the $$$i$$$-th country, they calculated the value $$$a_{i,j}$$$, which reflects the amount of water in it. The sages intend to create channels between the $$$j$$$-th region of the $$$i$$$-th country and the $$$j$$$-th region of the $$$(i + 1)$$$-th country for all $$$1 leq i leq (n - 1)$$$ and for all $$$1 leq j leq k$$$. Since all $$$n$$$ countries are on a large slope, water flows towards the country with the highest number. According to the sages' predictions, after the channel system is created, the new value of the $$$j$$$-th region of the $$$i$$$-th country will be $$$b_{i,j} = a_{1,j} a_{2,j} ... a_{i,j}$$$, where $$$$$$ denotes the bitwise "OR" operation. After the redistribution of water, the sages aim to choose the most suitable country for living, so they will send you $$$q$$$ queries for consideration. Each query will contain $$$m$$$ requirements. Each requirement contains three parameters: the region number $$$r$$$, the sign $$$o$$$ (either "$$$<$$$" or "$$$>$$$"), and the value $$$c$$$. If $$$o$$$ = "$$$<$$$", then in the $$$r$$$-th region of the country you choose, the new value must be strictly less than the limit $$$c$$$, and if $$$o$$$ = "$$$>$$$", it must be strictly greater. In other words, the chosen country $$$i$$$ must satisfy all $$$m$$$ requirements. If in the current requirement $$$o$$$ = "$$$<$$$", then it must hold that $$$b_{i,r} < c$$$, and if $$$o$$$ = "$$$>$$$", then $$$b_{i,r} > c$$$. In response to each query, you should output a single integer β€” the number of the suitable country. If there are multiple such countries, output the smallest one. If no such country exists, output $$$-1$$$. Input The first line contains three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 leq n, k, q leq 10^5$$$) β€” the number of countries, regions, and queries, respectively. Next, there are $$$n$$$ lines, where the $$$i$$$-th line contains $$$k$$$ integers $$$a_{i,1}, a_{i,2}, dots, a_{i,k}$$$ ($$$1 leq a_{i,j} leq 10^9$$$), where $$$a_{i,j}$$$ is the value of the $$$j$$$-th region of the $$$i$$$-th country. Then, $$$q$$$ queries are described. The first line of each query contains a single integer $$$m$$$ ($$$1 leq m leq 10^5$$$) β€” the number of requirements. Then follow $$$m$$$ lines, each containing an integer $$$r$$$, a character $$$o$$$, and an integer $$$c$$$ ($$$1 leq r leq k$$$, $$$0 leq c leq 2 cdot 10^9$$$), where $$$r$$$ and $$$c$$$ are the region number and the value, and $$$o$$$ is either "$$$<$$$" or "$$$>$$$" β€” the sign. It is guaranteed that $$$n cdot k$$$ does not exceed $$$10^5$$$ and that the sum of $$$m$$$ across all queries also does not exceed $$$10^5$$$. Output For each query, output a single integer on a new line β€” the smallest number of the suitable country, or $$$-1$$$ if no such country exists. Example input 3 4 4 1 3 5 9 4 6 5 3 2 1 2 7 3 1 > 4 2 < 8 1 < 6 2 1 < 8 2 > 8 1 3 > 5 2 4 > 8 1 < 8 output 2 -1 3 1 Note In the example, the initial values of the regions are as follows: $$$1$$$ $$$3$$$ $$$5$$$ $$$9$$$ $$$4$$$ $$$6$$$ $$$5$$$ $$$3$$$ $$$2$$$ $$$1$$$ $$$2$$$ $$$7$$$ After creating the channels, the new values will look like this: $$$1$$$ $$$3$$$ $$$5$$$ $$$9$$$ $$$1 4$$$ $$$3 6$$$ $$$5 5$$$ $$$9 3$$$ $$$1 4 2$$$ $$$3 6 1$$$ $$$5 5 2$$$ $$$9 3 7$$$ $$$downarrow$$$ $$$1$$$ $$$3$$$ $$$5$$$ $$$9$$$ $$$5$$$ $$$7$$$ $$$5$$$ $$$11$$$ $$$7$$$ $$$7$$$ $$$7$$$ $$$15$$$ In the first query, it is necessary to output the minimum country number (i.e., row) where, after the redistribution of water in the first region (i.e., column), the new value will be greater than four and less than six, and in the second region it will be less than eight. Only the country with number $$$2$$$ meets these requirements. In the second query, there are no countries that meet the specified requirements. In the third query, only the country with number $$$3$$$ is suitable. In the fourth query, all three countries meet the conditions, so the answer is the smallest number $$$1$$$. Codeforces (c)
1,600
false
true
false
false
true
true
false
true
false
false
48
1475C
At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls. Each class must present two couples to the ball. In Vasya's class, $$$a$$$ boys and $$$b$$$ girls wish to participate. But not all boys and not all girls are ready to dance in pairs. Formally, you know $$$k$$$ possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair. For example, if $$$a=3$$$, $$$b=4$$$, $$$k=4$$$ and the couples $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(3, 4)$$$ are ready to dance together (in each pair, the boy's number comes first, then the girl's number), then the following combinations of two pairs are possible (not all possible options are listed below): $$$(1, 3)$$$ and $$$(2, 2)$$$; $$$(3, 4)$$$ and $$$(1, 3)$$$; But the following combinations are not possible: $$$(1, 3)$$$ and $$$(1, 2)$$$xa0β€” the first boy enters two pairs; $$$(1, 2)$$$ and $$$(2, 2)$$$xa0β€” the second girl enters two pairs; Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains three integers $$$a$$$, $$$b$$$ and $$$k$$$ ($$$1 le a, b, k le 2 cdot 10^5$$$)xa0β€” the number of boys and girls in the class and the number of couples ready to dance together. The second line of each test case contains $$$k$$$ integers $$$a_1, a_2, ldots a_k$$$. ($$$1 le a_i le a$$$), where $$$a_i$$$ is the number of the boy in the pair with the number $$$i$$$. The third line of each test case contains $$$k$$$ integers $$$b_1, b_2, ldots b_k$$$. ($$$1 le b_i le b$$$), where $$$b_i$$$ is the number of the girl in the pair with the number $$$i$$$. It is guaranteed that the sums of $$$a$$$, $$$b$$$, and $$$k$$$ over all test cases do not exceed $$$2 cdot 10^5$$$. It is guaranteed that each pair is specified at most once in one test case. Output For each test case, on a separate line print one integerxa0β€” the number of ways to choose two pairs that match the condition above. Example Input 3 3 4 4 1 1 2 3 2 3 2 4 1 1 1 1 1 2 2 4 1 1 2 2 1 2 1 2 Note In the first test case, the following combinations of pairs fit: $$$(1, 2)$$$ and $$$(3, 4)$$$; $$$(1, 3)$$$ and $$$(2, 2)$$$; $$$(1, 3)$$$ and $$$(3, 4)$$$; $$$(2, 2)$$$ and $$$(3, 4)$$$. There is only one pair in the second test case. In the third test case, the following combinations of pairs fit: $$$(1, 1)$$$ and $$$(2, 2)$$$; $$$(1, 2)$$$ and $$$(2, 1)$$$.
1,400
true
false
false
false
false
false
false
false
false
true
3,302
1211I
Ivan on his birthday was presented with array of non-negative integers $$$a_1, a_2, ldots, a_n$$$. He immediately noted that all $$$a_i$$$ satisfy the condition $$$0 leq a_i leq 15$$$. Ivan likes graph theory very much, so he decided to transform his sequence to the graph. There will be $$$n$$$ vertices in his graph, and vertices $$$u$$$ and $$$v$$$ will present in the graph if and only if binary notations of integers $$$a_u$$$ and $$$a_v$$$ are differ in exactly one bit (in other words, $$$a_u oplus a_v = 2^k$$$ for some integer $$$k geq 0$$$. Where $$$oplus$$$ is }{2}$$$): number of vertices and edges in Ivan's graph. Next $$$m$$$ lines contain the description of edges: $$$i$$$-th line contain two integers $$$u_i, v_i$$$ ($$$1 leq u_i, v_i leq n; u_i eq v_i$$$), describing undirected edge connecting vertices $$$u_i$$$ and $$$v_i$$$ in the graph. It is guaranteed that there are no multiple edges in the graph. It is guaranteed that there exists some solution for the given graph. Output Output $$$n$$$ space-separated integers, $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 15$$$). Printed numbers should satisfy the constraints: edge between vertices $$$u$$$ and $$$v$$$ present in the graph if and only if $$$a_u oplus a_v = 2^k$$$ for some integer $$$k geq 0$$$. It is guaranteed that there exists some solution for the given graph. If there are multiple possible solutions, you can output any.
3,000
false
false
false
false
false
false
false
false
false
true
4,620
1492B
You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one. Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i.xa0e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k > 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $$$sumlimits_{i = 1}^{n}{n^{n - i} cdot p_i}$$$. Given the original deck, output the deck with maximum possible order you can make using the operation above. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β€” the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,dots, p_n$$$ ($$$1 le p_i le n$$$; $$$p_i eq p_j$$$ if $$$i eq j$$$)xa0β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. Example Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 Note In the first test case, one of the optimal strategies is the next one: 1. take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1, 2, 3]$$$, $$$p'$$$ becomes $$$[4]$$$; 2. take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1, 2]$$$, $$$p'$$$ becomes $$$[4, 3]$$$; 3. take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[4, 3, 2]$$$; 4. take $$$1$$$ card from the top of $$$p$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[4, 3, 2, 1]$$$. In result, $$$p'$$$ has order equal to $$$4^3 cdot 4 + 4^2 cdot 3 + 4^1 cdot 2 + 4^0 cdot 1$$$ $$$=$$$ $$$256 + 48 + 8 + 1 = 313$$$. In the second test case, one of the optimal strategies is: 1. take $$$4$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[1]$$$, $$$p'$$$ becomes $$$[5, 2, 4, 3]$$$; 2. take $$$1$$$ card from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[5, 2, 4, 3, 1]$$$; In result, $$$p'$$$ has order equal to $$$5^4 cdot 5 + 5^3 cdot 2 + 5^2 cdot 4 + 5^1 cdot 3 + 5^0 cdot 1$$$ $$$=$$$ $$$3125 + 250 + 100 + 15 + 1 = 3491$$$. In the third test case, one of the optimal strategies is: 1. take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2, 5, 3]$$$, $$$p'$$$ becomes $$$[6, 1]$$$; 2. take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes $$$[4, 2]$$$, $$$p'$$$ becomes $$$[6, 1, 5, 3]$$$; 3. take $$$2$$$ cards from the top of $$$p$$$ and move it to $$$p'$$$: $$$p$$$ becomes empty, $$$p'$$$ becomes $$$[6, 1, 5, 3, 4, 2]$$$. In result, $$$p'$$$ has order equal to $$$6^5 cdot 6 + 6^4 cdot 1 + 6^3 cdot 5 + 6^2 cdot 3 + 6^1 cdot 4 + 6^0 cdot 2$$$ $$$=$$$ $$$46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$$$.
1,100
true
true
false
false
true
false
false
false
false
false
3,212
923C
Alice has a very important message _M_ consisting of some non-negative integers that she wants to keep secret from Eve. Alice knows that the only theoretically secure cipher is one-time pad. Alice generates a random key _K_ of the length equal to the message's length. Alice computes the bitwise xor of each element of the message and the key (, where denotes the . She generated a key _K_u2009=u2009(16,u20097,u20096,u20093). The encrypted message is thus _A_u2009=u2009(16,u20098,u200915,u200917). Alice realised that she cannot store the key with the encrypted message. Alice sent her key _K_ to Bob and deleted her own copy. Alice is smart. Really, be like Alice. Bob realised that the encrypted message is only secure as long as the key is secret. Bob thus randomly permuted the key before storing it. Bob thinks that this way, even if Eve gets both the encrypted message and the key, she will not be able to read the message. Bob is not smart. Don't be like Bob. In the above example, Bob may have, for instance, selected a permutation (3,u20094,u20091,u20092) and stored the permuted key _P_u2009=u2009(6,u20093,u200916,u20097). One year has passed and Alice wants to decrypt her message. Only now Bob has realised that this is impossible. As he has permuted the key randomly, the message is lost forever. Did we mention that Bob isn't smart? Bob wants to salvage at least some information from the message. Since he is not so smart, he asks for your help. You know the encrypted message _A_ and the permuted key _P_. What is the lexicographically smallest message that could have resulted in the given encrypted text? More precisely, for given _A_ and _P_, find the lexicographically smallest message _O_, for which there exists a permutation Ο€ such that for every _i_. Note that the sequence _S_ is lexicographically smaller than the sequence _T_, if there is an index _i_ such that _S__i_u2009<u2009_T__i_ and for all _j_u2009<u2009_i_ the condition _S__j_u2009=u2009_T__j_ holds. Input The first line contains a single integer _N_ (1u2009≀u2009_N_u2009≀u2009300000), the length of the message. The second line contains _N_ integers _A_1,u2009_A_2,u2009...,u2009_A__N_ (0u2009≀u2009_A__i_u2009<u2009230) representing the encrypted message. The third line contains _N_ integers _P_1,u2009_P_2,u2009...,u2009_P__N_ (0u2009≀u2009_P__i_u2009<u2009230) representing the permuted encryption key. Note In the first case, the solution is (10,u20093,u200928), since , and . Other possible permutations of key yield messages (25,u20096,u200910), (25,u20093,u200915), (10,u200921,u200910), (15,u200921,u200915) and (15,u20096,u200928), which are all lexicographically larger than the solution.
1,800
false
true
false
false
true
false
false
false
false
false
6,021
53E
Life in Bertown has become hard. The city has too many roads and the government spends too much to maintain them. There are _n_ junctions and _m_ two way roads, at which one can get from each junction to any other one. The mayor wants to close some roads so that the number of roads left totaled to _n_u2009-u20091 roads and it were still possible to get from each junction to any other one. Besides, the mayor is concerned with the number of dead ends which are the junctions from which only one road goes. There shouldn't be too many or too few junctions. Having discussed the problem, the mayor and his assistants decided that after the roads are closed, the road map should contain exactly _k_ dead ends. Your task is to count the number of different ways of closing the roads at which the following conditions are met: There are exactly _n_u2009-u20091 roads left. It is possible to get from each junction to any other one. There are exactly _k_ dead ends on the resulting map. Two ways are considered different if there is a road that is closed in the first way, and is open in the second one. Input The first line contains three integers _n_, _m_ and _k_ (3u2009≀u2009_n_u2009≀u200910,u2009_n_u2009-u20091u2009≀u2009_m_u2009≀u2009_n_Β·(_n_u2009-u20091)u2009/u20092,u20092u2009≀u2009_k_u2009≀u2009_n_u2009-u20091) which represent the number of junctions, roads and dead ends correspondingly. Then follow _m_ lines each containing two different integers _v_1 and _v_2 (1u2009≀u2009_v_1,u2009_v_2u2009≀u2009_n_,u2009_v_1u2009β‰ u2009_v_2) which represent the number of junctions connected by another road. There can be no more than one road between every pair of junctions. The junctions are numbered with integers from 1 to _n_. It is guaranteed that it is possible to get from each junction to any other one along the original roads. Output Print a single number β€” the required number of ways. Examples Input 4 6 2 1 2 2 3 3 4 4 1 1 3 2 4 Input 4 6 3 1 2 2 3 3 4 4 1 1 3 2 4
2,500
false
false
false
true
false
false
false
false
false
false
9,706
2003C
Turtle gives you a string $$$s$$$, consisting of lowercase Latin letters. Turtle considers a pair of integers $$$(i, j)$$$ ($$$1 le i < j le n$$$) to be a pleasant pair if and only if there exists an integer $$$k$$$ such that $$$i le k < j$$$ and both of the following two conditions hold: $$$s_k e s_{k + 1}$$$; $$$s_k e s_i$$$ or $$$s_{k + 1} e s_j$$$. Besides, Turtle considers a pair of integers $$$(i, j)$$$ ($$$1 le i < j le n$$$) to be a good pair if and only if $$$s_i = s_j$$$ or $$$(i, j)$$$ is a pleasant pair. Turtle wants to reorder the string $$$s$$$ so that the number of good pairs is maximized. Please help him! Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) β€” the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters. 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 the string $$$s$$$ after reordering so that the number of good pairs is maximized. If there are multiple answers, print any of them. Example Input 5 3 abc 5 edddf 6 turtle 8 pppppppp 10 codeforces Output acb ddedf urtlet pppppppp codeforces Note In the first test case, $$$(1, 3)$$$ is a good pair in the reordered string. It can be seen that we can't reorder the string so that the number of good pairs is greater than $$$1$$$. bac and cab can also be the answer. In the second test case, $$$(1, 2)$$$, $$$(1, 4)$$$, $$$(1, 5)$$$, $$$(2, 4)$$$, $$$(2, 5)$$$, $$$(3, 5)$$$ are good pairs in the reordered string. efddd can also be the answer.
1,200
false
true
false
false
false
true
false
false
true
false
237
1500E
Vanya invented an interesting trick with a set of integers. Let an illusionist have a set of positive integers $$$S$$$. He names a positive integer $$$x$$$. Then an audience volunteer must choose some subset (possibly, empty) of $$$S$$$ without disclosing it to the illusionist. The volunteer tells the illusionist the size of the chosen subset. And here comes the trick: the illusionist guesses whether the sum of the subset elements does not exceed $$$x$$$. The sum of elements of an empty subset is considered to be $$$0$$$. Vanya wants to prepare the trick for a public performance. He prepared some set of distinct positive integers $$$S$$$. Vasya wants the trick to be successful. He calls a positive number $$$x$$$ unsuitable, if he can't be sure that the trick would be successful for every subset a viewer can choose. Vanya wants to count the number of unsuitable integers for the chosen set $$$S$$$. Vanya plans to try different sets $$$S$$$. He wants you to write a program that finds the number of unsuitable integers for the initial set $$$S$$$, and after each change to the set $$$S$$$. Vanya will make $$$q$$$ changes to the set, and each change is one of the following two types: add a new integer $$$a$$$ to the set $$$S$$$, or remove some integer $$$a$$$ from the set $$$S$$$. Input The first line contains two integers $$$n$$$, $$$q$$$ ($$$1 leq n, q leq 200,000$$$)xa0β€” the size of the initial set $$$S$$$ and the number of changes. The next line contains $$$n$$$ distinct integers $$$s_1, s_2, ldots, s_n$$$ ($$$1 leq s_i leq 10^{13}$$$)xa0β€” the initial elements of $$$S$$$. Each of the following $$$q$$$ lines contain two integers $$$t_i$$$, $$$a_i$$$ ($$$1 leq t_i leq 2$$$, $$$1 leq a_i leq 10^{13}$$$), describing a change: If $$$t_i = 1$$$, then an integer $$$a_i$$$ is added to the set $$$S$$$. It is guaranteed that this integer is not present in $$$S$$$ before this operation. If $$$t_i = 2$$$, then an integer $$$a_i$$$ is removed from the set $$$S$$$. In is guaranteed that this integer is present in $$$S$$$ before this operation. Output Print $$$q + 1$$$ lines. In the first line print the number of unsuitable integers for the initial set $$$S$$$. In the next $$$q$$$ lines print the number of unsuitable integers for $$$S$$$ after each change. Example Input 3 11 1 2 3 2 1 1 5 1 6 1 7 2 6 2 2 2 3 1 10 2 5 2 7 2 10 Output 4 1 6 12 19 13 8 2 10 3 0 0 Note In the first example the initial set is $$$S = {1, 2, 3}$$$. For this set the trick can be unsuccessful for $$$x in {1, 2, 3, 4}$$$. For example, if $$$x = 4$$$, the volunteer can choose the subset $$${1, 2}$$$ with sum $$$3 leq x$$$, and can choose the subset $$${2, 3}$$$ with sum $$$5 > x$$$. However, in both cases the illusionist only know the same size of the subset ($$$2$$$), so he can't be sure answering making a guess. Since there is only one subset of size $$$3$$$, and the sum of each subset of smaller size does not exceed $$$5$$$, all $$$x ge 5$$$ are suitable.
3,300
false
false
false
false
true
false
false
true
false
false
3,164
1838A
Two integers were written on a blackboard. After that, the following step was carried out $$$n-2$$$ times: Select any two integers on the board, and write the absolute value of their difference on the board. After this process was complete, the list of $$$n$$$ integers was shuffled. You are given the final list. Recover one of the initial two numbers. You do not need to recover the other one. You are guaranteed that the input can be generated using the above process. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 100$$$)xa0β€” the size of the final list. The next line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0β€” the shuffled list of numbers written on the blackboard. It is guaranteed that the input was generated using the process described above. Output For each test case, output a single integer $$$x$$$xa0β€” one of the two initial numbers on the blackboard. If there are multiple solutions, print any of them. Example Input 9 3 9 2 7 3 15 -4 11 4 -9 1 11 -10 5 3 0 0 0 3 7 8 16 8 0 8 16 8 4 0 0 0 0 10 27 1 24 28 2 -1 26 25 28 27 6 600000000 800000000 0 -200000000 1000000000 800000000 3 0 -1000000000 1000000000 Output 9 11 -9 3 8 0 -1 600000000 0 Note For the first test case, $$$a$$$ can be produced by starting with either $$$9$$$ and $$$2$$$, and then writing down $$$9-2=7$$$, or starting with $$$9$$$ and $$$7$$$ and writing down $$$9-7=2$$$. So $$$2$$$, $$$7$$$, and $$$9$$$ are all valid answers, because they all appear in at least one valid pair. For the second test case, we can show that the two initial numbers must have been $$$-4$$$ and $$$11$$$. For the fourth test case, the starting numbers could have been either $$$3$$$ and $$$3$$$, or $$$3$$$ and $$$0$$$, so $$$3$$$ and $$$0$$$ are both valid answers. For the fifth test case, we can show that the starting numbers were $$$8$$$ and $$$16$$$.
800
true
false
false
false
false
true
false
false
false
false
1,264
768C
Jon Snow now has to fight with White Walkers. He has _n_ rangers, each of which has his own strength. Also Jon Snow has his favourite number _x_. Each ranger can fight with a white walker only if the strength of the white walker equals his strength. He however thinks that his rangers are weak and need to improve. Jon now thinks that if he takes the bitwise XOR of strengths of some of rangers with his favourite number _x_, he might get soldiers of high strength. So, he decided to do the following operation _k_ times: 1. Arrange all the rangers in a straight line in the order of increasing strengths. 2. Take the bitwise XOR (is written as ) of the strength of each alternate ranger with _x_ and update it's strength. Suppose, Jon has 5 rangers with strengths [9,u20097,u200911,u200915,u20095] and he performs the operation 1 time with _x_u2009=u20092. He first arranges them in the order of their strengths, [5,u20097,u20099,u200911,u200915]. Then he does the following: 1. The strength of first ranger is updated to , i.e. 7. 2. The strength of second ranger remains the same, i.e. 7. 3. The strength of third ranger is updated to , i.e. 11. 4. The strength of fourth ranger remains the same, i.e. 11. 5. The strength of fifth ranger is updated to , i.e. 13. The new strengths of the 5 rangers are [7,u20097,u200911,u200911,u200913] Now, Jon wants to know the maximum and minimum strength of the rangers after performing the above operations _k_ times. He wants your help for this task. Can you help him? Input First line consists of three integers _n_, _k_, _x_ (1u2009≀u2009_n_u2009≀u2009105, 0u2009≀u2009_k_u2009≀u2009105, 0u2009≀u2009_x_u2009≀u2009103) β€” number of rangers Jon has, the number of times Jon will carry out the operation and Jon's favourite number respectively. Second line consists of _n_ integers representing the strengths of the rangers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≀u2009_a__i_u2009≀u2009103).
1,800
false
false
true
true
false
false
true
false
true
false
6,711
1901B
There is a ribbon divided into $$$n$$$ cells, numbered from $$$1$$$ to $$$n$$$ from left to right. Initially, an integer $$$0$$$ is written in each cell. Monocarp plays a game with a chip. The game consists of several turns. During the first turn, Monocarp places the chip in the $$$1$$$-st cell of the ribbon. During each turn except for the first turn, Monocarp does exactly one of the two following actions: move the chip to the next cell (i.u2009e. if the chip is in the cell $$$i$$$, it is moved to the cell $$$i+1$$$). This action is impossible if the chip is in the last cell; choose any cell $$$x$$$ and teleport the chip into that cell. It is possible to choose the cell where the chip is currently located. At the end of each turn, the integer written in the cell with the chip is increased by $$$1$$$. Monocarp's goal is to make some turns so that the $$$1$$$-st cell contains the integer $$$c_1$$$, the $$$2$$$-nd cell contains the integer $$$c_2$$$, ..., the $$$n$$$-th cell contains the integer $$$c_n$$$. He wants to teleport the chip as few times as possible. Help Monocarp calculate the minimum number of times he has to teleport the chip. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. Each test case consists of two lines: the first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$); the second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$0 le c_i le 10^9$$$; $$$c_1 ge 1$$$). It can be shown that under these constraints, it is always possible to make a finite amount of turns so that the integers in the cells match the sequence $$$c_1, c_2, dots, c_n$$$. Additional constraint on the input: the sum of values of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print one integer β€” the minimum number of times Monocarp has to teleport the chip. Example Input 4 4 1 2 2 1 5 1 0 1 0 1 5 5 4 3 2 1 1 12 Note In the first test case of the example, Monocarp can perform the turns as follows: place the chip in the $$$1$$$-st cell; the numbers in the cells are $$$[1, 0, 0, 0]$$$; move the chip to the next ($$$2$$$-nd) cell; the numbers in the cells are $$$[1, 1, 0, 0]$$$; move the chip to the next ($$$3$$$-rd) cell; the numbers in the cells are $$$[1, 1, 1, 0]$$$; teleport the chip to the $$$2$$$-nd cell; the numbers in the cells are $$$[1, 2, 1, 0]$$$; move the chip to the next ($$$3$$$-rd) cell; the numbers in the cells are $$$[1, 2, 2, 0]$$$; move the chip to the next ($$$4$$$-th) cell; the numbers in the cells are $$$[1, 2, 2, 1]$$$.
1,100
true
true
false
false
false
false
false
false
false
false
908
598E
You have a rectangular chocolate bar consisting of _n_u2009Γ—u2009_m_ single squares. You want to eat exactly _k_ squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length. For example, if you have a chocolate bar consisting of 2u2009Γ—u20093 unit squares then you can break it horizontally and get two 1u2009Γ—u20093 pieces (the cost of such breaking is 32u2009=u20099), or you can break it vertically in two ways and get two pieces: 2u2009Γ—u20091 and 2u2009Γ—u20092 (the cost of such breaking is 22u2009=u20094). For several given values _n_, _m_ and _k_ find the minimum total cost of breaking. You can eat exactly _k_ squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to _k_ squares. The remaining _n_Β·_m_u2009-u2009_k_ squares are not necessarily form a single rectangular piece. Input The first line of the input contains a single integer _t_ (1u2009≀u2009_t_u2009≀u200940910)xa0β€” the number of values _n_, _m_ and _k_ to process. Each of the next _t_ lines contains three integers _n_, _m_ and _k_ (1u2009≀u2009_n_,u2009_m_u2009≀u200930,u20091u2009≀u2009_k_u2009≀u2009_min_(_n_Β·_m_,u200950))xa0β€” the dimensions of the chocolate bar and the number of squares you want to eat respectively. Output For each _n_, _m_ and _k_ print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly _k_ squares. Examples Input 4 2 2 1 2 2 3 2 2 2 2 2 4 Note In the first query of the sample one needs to perform two breaks: to split 2u2009Γ—u20092 bar into two pieces of 2u2009Γ—u20091 (cost is 22u2009=u20094), to split the resulting 2u2009Γ—u20091 into two 1u2009Γ—u20091 pieces (cost is 12u2009=u20091). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
2,000
false
false
false
true
false
false
true
false
false
false
7,453
1750A
You are given a permutation $$$a_1, a_2, ldots, a_n$$$ of size $$$n$$$, where each integer from $$$1$$$ to $$$n$$$ appears exactly once. You can do the following operation any number of times (possibly, zero): Choose any three indices $$$i, j, k$$$ ($$$1 le i < j < k le n$$$). If $$$a_i > a_k$$$, replace $$$a_i$$$ with $$$a_i + a_j$$$. Otherwise, swap $$$a_j$$$ and $$$a_k$$$. Determine whether you can make the array $$$a$$$ sorted in non-descending order. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 5000$$$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 10$$$) β€” the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,dots,a_n$$$ ($$$1 le a_i le n$$$, $$$a_i eq a_j$$$ if $$$i eq j$$$) β€” the elements of the array $$$a$$$. Output For each test case, output "Yes" (without quotes) if the array can be sorted in non-descending order, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "YES", "yEs" and "yes" will be recognized as a positive response). Example Input 7 3 1 2 3 3 1 3 2 7 5 3 4 7 6 2 1 7 7 6 5 4 3 2 1 5 2 1 4 5 3 5 2 1 3 4 5 7 1 2 6 7 4 3 5 Output Yes Yes No No No No Yes Note In the first test case, $$$[1,2,3]$$$ is already sorted in non-descending order. In the second test case, we can choose $$$i = 1,j = 2,k = 3$$$. Since $$$a_1 le a_3$$$, swap $$$a_2$$$ and $$$a_3$$$, the array then becomes $$$[1,2,3]$$$, which is sorted in non-descending order. In the seventh test case, we can do the following operations successively: Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 le a_7$$$, swap $$$a_6$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,4,5,3]$$$. Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 > a_7$$$, replace $$$a_5$$$ with $$$a_5+a_6=9$$$, the array then becomes $$$[1,2,6,7,9,5,3]$$$. Choose $$$i = 2,j = 5,k = 7$$$. Since $$$a_2 le a_7$$$, swap $$$a_5$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,3,5,9]$$$. Choose $$$i = 2,j = 4,k = 6$$$. Since $$$a_2 le a_6$$$, swap $$$a_4$$$ and $$$a_6$$$, the array then becomes $$$[1,2,6,5,3,7,9]$$$. Choose $$$i = 1,j = 3,k = 5$$$. Since $$$a_1 le a_5$$$, swap $$$a_3$$$ and $$$a_5$$$, the array then becomes $$$[1,2,3,5,6,7,9]$$$, which is sorted in non-descending order. In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order.
800
true
false
true
false
false
true
false
false
false
false
1,785
384A
Problem - 384A - 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 one square horizontally or vertically. More precisely, if the Coder is located at position (_x_,u2009_y_), he can move to (or attack) positions (_x_u2009+u20091,u2009_y_), (_x_–1,u2009_y_), (_x_,u2009_y_u2009+u20091) and (_x_,u2009_y_–1). Iahub wants to know how many Coders can be placed on an _n_u2009Γ—u2009_n_ chessboard, so that no Coder attacks any other Coder. Input The first line contains an integer _n_ (1u2009≀u2009_n_u2009≀u20091000). Output On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next _n_ lines print _n_ characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. Examples Input 2 Output 2 C. .C
800
false
false
true
false
false
false
false
false
false
false
8,293
1482H
This year a Chunin Selection Exam is held again in Konoha, and taking part in it are $$$n$$$ ninjas named $$$s_1$$$, $$$s_2$$$, ..., $$$s_n$$$. All names are distinct. One of the exam stages consists of fights between the participants. This year the rules determining the ninjas for each fight are the following: ninjas $$$i$$$ and $$$j$$$ fight against each other if the following conditions are held: $$$i eq j$$$; $$$s_{j}$$$ is a substring of $$$s_{i}$$$; there is no $$$k$$$ except $$$i$$$ and $$$j$$$ that $$$s_{j}$$$ is a substring of $$$s_{k}$$$, and $$$s_{k}$$$ is a substring of $$$s_{i}$$$. A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Your task is to find out how many fights are going to take place this year. Input The first line consists of the only integer $$$n$$$ ($$$1 leq n leq 10^{6}$$$) standing for the number of examinees. The following $$$n$$$ lines contain their names. No two names coincide, all names are non-empty and consist of lowercase English letters. The total length of all names doesn't exceed $$$10^6$$$. Output Print the only integer standing for the number of fights. Examples Input 5 hidan dan hanabi bi nabi Input 4 abacaba abaca acaba aca Note In the first example hidan fights against dan, and hanabi fights against nabi, who also fights bi. Ninjas named hanabi and bi don't fight each other since there is the ninja called nabi who breaks the third condition for them. In the second example the fights are held between abacaba and acaba, abacaba and abaca, acaba and aca, abaca and aca.
3,400
false
false
false
false
true
false
false
false
false
false
3,260
294D
Shaass thinks a kitchen with all white floor tiles is so boring. His kitchen floor is made of _n_Β·_m_ square tiles forming a _n_u2009Γ—u2009_m_ rectangle. Therefore he's decided to color some of the tiles in black so that the floor looks like a checkerboard, which is no two side-adjacent tiles should have the same color. Shaass wants to use a painter robot to color the tiles. In the beginning the robot is standing in a border tile (_x__s_,u2009_y__s_) facing a diagonal direction (i.e. upper-left, upper-right, down-left or down-right). As the robot walks in the kitchen he paints every tile he passes even if it's painted before. Painting each tile consumes one unit of black paint. If at any moment the robot hits a wall of the kitchen he changes his direction according the reflection rules. Note that a tile gets painted when the robot enters the tile from another tile, in other words changing direction in the same tile doesn't lead to any painting. The first tile the robot is standing on, is also painted. The robot stops painting the first moment the floor is checkered. Given the dimensions of the kitchen and the position of the robot, find out the amount of paint the robot consumes before it stops painting the floor. Let's consider an examples depicted below. If the robot starts at tile number 1 (the tile (1,u20091)) of the left grid heading to down-right it'll pass tiles 1354236 and consumes 7 units of black paint on his way until he stops at tile number 6. But if it starts at tile number 1 in the right grid heading to down-right it will get stuck in a loop painting tiles 1, 2, and 3. Input The first line of the input contains two integers _n_ and _m_, (2u2009≀u2009_n_,u2009_m_u2009≀u2009105). The second line contains two integers _x__s_ and _y__s_ (1u2009≀u2009_x__s_u2009≀u2009_n_,u20091u2009≀u2009_y__s_u2009≀u2009_m_) and the direction robot is facing initially. Direction is one of the strings: "UL" (upper-left direction), "UR" (upper-right), "DL" (down-left) or "DR" (down-right). Note, that record (_x__s_,u2009_y__s_) denotes the tile that is located at the _x__s_-th row from the top and at the _y__s_-th column from the left of the kitchen. It's guaranteed that the starting position will be a border tile (a tile with less than four side-adjacent tiles). Output Print the amount of paint the robot consumes to obtain a checkered kitchen floor. Or print -1 if it never happens. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
2,500
false
false
true
false
false
false
true
false
false
false
8,653
540E
Problem - 540E - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags binary search data structures implementation sortings trees *2100 No tag edit access β†’ Contest materials is an operation of swapping the elements of the sequence on positions _a_ and _b_. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (_i_,u2009_j_), that _i_u2009<u2009_j_ and _p__i_u2009>u2009_p__j_. Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009105)xa0β€” the number of swap operations applied to the sequence. Each of the next _n_ lines contains two integers _a__i_ and _b__i_ (1u2009≀u2009_a__i_,u2009_b__i_u2009≀u2009109, _a__i_u2009β‰ u2009_b__i_)xa0β€” the arguments of the swap operation. Output Print a single integer β€” the number of inversions in the resulting sequence. Examples Input 2 4 2 1 4 Output 4 Input 3 1 6 3 4 2 5 Output 15 Note In the first sample the sequence is being modified as follows: . It has 4 inversions formed by index pairs (1,u20094), (2,u20093), (2,u20094) and (3,u20094).
2,100
false
false
true
false
true
false
false
true
true
false
7,676
2033B
During her journey with Kosuke, Sakurako and Kosuke found a valley that can be represented as a matrix of size $$$n imes n$$$, where at the intersection of the $$$i$$$-th row and the $$$j$$$-th column is a mountain with a height of $$$a_{i,j}$$$. If $$$a_{i,j} < 0$$$, then there is a lake there. Kosuke is very afraid of water, so Sakurako needs to help him: With her magic, she can select a square area of mountains and increase the height of each mountain on the main diagonal of that area by exactly one. More formally, she can choose a submatrix with the upper left corner located at $$$(i, j)$$$ and the lower right corner at $$$(p, q)$$$, such that $$$p-i=q-j$$$. She can then add one to each element at the intersection of the $$$(i + k)$$$-th row and the $$$(j + k)$$$-th column, for all $$$k$$$ such that $$$0 le k le p-i$$$. Determine the minimum number of times Sakurako must use her magic so that there are no lakes. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 200$$$)xa0β€” the number of test cases. Each test case is described as follows: The first line of each test case consists of a single number $$$n$$$ ($$$1 le n le 500$$$). Each of the following $$$n$$$ lines consists of $$$n$$$ integers separated by spaces, which correspond to the heights of the mountains in the valley $$$a$$$ ($$$-10^5 le a_{i,j} le 10^5$$$). It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$1000$$$. Output For each test case, output the minimum number of times Sakurako will have to use her magic so that all lakes disappear. Example Input 4 1 1 2 -1 2 3 0 3 1 2 3 -2 1 -1 0 0 -1 5 1 1 -1 -1 3 -3 1 4 4 -4 -1 -1 3 0 -5 4 5 3 -3 -1 3 1 -3 -1 5
900
false
true
false
false
false
true
true
false
false
false
67
1476E
You are given $$$n$$$ patterns $$$p_1, p_2, dots, p_n$$$ and $$$m$$$ strings $$$s_1, s_2, dots, s_m$$$. Each pattern $$$p_i$$$ consists of $$$k$$$ characters that are either lowercase Latin letters or wildcard characters (denoted by underscores). All patterns are pairwise distinct. Each string $$$s_j$$$ consists of $$$k$$$ lowercase Latin letters. A string $$$a$$$ matches a pattern $$$b$$$ if for each $$$i$$$ from $$$1$$$ to $$$k$$$ either $$$b_i$$$ is a wildcard character or $$$b_i=a_i$$$. You are asked to rearrange the patterns in such a way that the first pattern the $$$j$$$-th string matches is $$$p[mt_j]$$$. You are allowed to leave the order of the patterns unchanged. Can you perform such a rearrangement? If you can, then print any valid order. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 le n, m le 10^5$$$, $$$1 le k le 4$$$)xa0β€” the number of patterns, the number of strings and the length of each pattern and string. Each of the next $$$n$$$ lines contains a patternxa0β€” $$$k$$$ characters that are either lowercase Latin letters or underscores. All patterns are pairwise distinct. Each of the next $$$m$$$ lines contains a stringxa0β€” $$$k$$$ lowercase Latin letters, and an integer $$$mt$$$ ($$$1 le mt le n$$$)xa0β€” the index of the first pattern the corresponding string should match. Output Print "NO" if there is no way to rearrange the patterns in such a way that the first pattern that the $$$j$$$-th string matches is $$$p[mt_j]$$$. Otherwise, print "YES" in the first line. The second line should contain $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$xa0β€” the order of the patterns. If there are multiple answers, print any of them. Examples Input 5 3 4 _b_d __b_ aaaa ab__ _bcd abcd 4 abba 2 dbcd 5 Input 2 2 2 a_ _b ab 1 ab 2 Note The order of patterns after the rearrangement in the first example is the following: aaaa __b_ ab__ _bcd _b_d Thus, the first string matches patterns ab__, _bcd, _b_d in that order, the first of them is ab__, that is indeed $$$p[4]$$$. The second string matches __b_ and ab__, the first of them is __b_, that is $$$p[2]$$$. The last string matches _bcd and _b_d, the first of them is _bcd, that is $$$p[5]$$$. The answer to that test is not unique, other valid orders also exist. In the second example cba doesn't match __c, thus, no valid order exists. In the third example the order (a_, _b) makes both strings match pattern $$$1$$$ first and the order (_b, a_) makes both strings match pattern $$$2$$$ first. Thus, there is no order that produces the result $$$1$$$ and $$$2$$$.
2,300
false
false
false
false
true
false
false
false
true
true
3,293
1934E
Problem - 1934E - 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 constructive algorithms number theory *3000 No tag edit access β†’ Contest materials ") $$$, $$$a_j = operatorname{lcm}(x, z)$$$, and $$$a_k = operatorname{lcm}(x, y)$$$, where $$$operatorname{lcm}$$$ represents the least common multiple. Your task is to provide a possible sequence of operations, containing at most $$$lfloor frac{n}{6} floor + 5$$$ operations such that after executing these operations, if you create a set containing the greatest common divisors (GCDs) of all subsequences with a size greater than $$$1$$$, then all numbers from $$$1$$$ to $$$n$$$ should be present in this set.After all the operations $$$a_i le 10^{18}$$$ should hold for all $$$1 le i le n$$$. We can show that an answer always exists. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^2$$$)xa0β€” the number of test cases. The description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$3 leq n leq 3 cdot 10^{4}$$$)xa0β€” the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^{4}$$$. Output The first line should contain an integer $$$k$$$ ($$$0 leq k leq lfloor frac{n}{6} floor + 5$$$)xa0β€” where $$$k$$$ is the number of operations. The next $$$k$$$ lines should contain the description of each operation i.e. $$$3$$$ integers $$$i$$$, $$$j$$$ and $$$k$$$, where $$$1 leq i, j, k leq n$$$ and all must be distinct. Example Input 3 3 4 7 Output 1 1 2 3 1 1 3 4 3 3 5 7 5 6 7 2 3 4 Note In the third test case, $$$a =
3,000
false
false
false
false
false
true
true
false
false
false
675
993E
Problem - 993E - 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 chinese remainder theorem fft math *2300 No tag edit access β†’ Contest materials $$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(-10^9 le a_i le 10^9)$$$xa0β€” the given array. Output Print $$$n+1$$$ integers, where the $$$i$$$-th number is the answer for Nikita's question for $$$k=i-1$$$. Examples Input 5 3 1 2 3 4 5 Output 6 5 4 0 0 0 Input 2 6 -5 9 Output 1 2 0 Input 6 99 -1 -1 -1 -1 -1 -1 Output 0 6 5 4 3 2 1
2,300
true
false
false
false
false
false
false
false
false
false
5,730
703D
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers _a_1,u2009_a_2,u2009...,u2009_a__n_ of _n_ elements! Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process _m_ queries. Each query is processed in the following way: 1. Two integers _l_ and _r_ (1u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009_n_) are specifiedxa0β€” bounds of query segment. 2. Integers, presented in array segment [_l_,u2009u2009_r_] (in sequence of integers _a__l_,u2009_a__l_u2009+u20091,u2009...,u2009_a__r_) even number of times, are written down. 3. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are _x_1,u2009_x_2,u2009...,u2009_x__k_, then Mishka wants to know the value , where xa0β€” operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented. Input The first line of the input contains single integer _n_ (1u2009≀u2009_n_u2009≀u20091u2009000u2009000)xa0β€” the number of elements in the array. The second line of the input contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≀u2009_a__i_u2009≀u2009109)xa0β€” array elements. The third line of the input contains single integer _m_ (1u2009≀u2009_m_u2009≀u20091u2009000u2009000)xa0β€” the number of queries. Each of the next _m_ lines describes corresponding query by a pair of integers _l_ and _r_ (1u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009_n_)xa0β€” the bounds of query segment. Output Print _m_ non-negative integersxa0β€” the answers for the queries in the order they appear in the input. Examples Input 7 1 2 1 3 3 2 3 5 4 7 4 5 1 3 1 7 1 5 Note In the second sample: There is no integers in the segment of the first query, presented even number of times in the segmentxa0β€” the answer is 0. In the second query there is only integer 3 is presented even number of timesxa0β€” the answer is 3. In the third query only integer 1 is written downxa0β€” the answer is 1. In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is . In the fifth query 1 and 3 are written down. The answer is .
2,100
false
false
false
false
true
false
false
false
false
false
7,008
2002G
Problem - 2002G - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags bitmasks brute force hashing meet-in-the-middle *3400 No tag edit access β†’ Contest materials ") Editorial") $$$. There exists a directed edge from $$$(x,y)$$$ to $$$(x+1,y)$$$, with non-negative integer value $$$d_{x,y}$$$, for all $$$1le x < n, 1le y le n$$$, and there also exists a directed edge from $$$(x,y)$$$ to $$$(x,y+1)$$$, with non-negative integer value $$$r_{x,y}$$$, for all $$$1le x le n, 1le y < n$$$. Initially, you are at $$$(1,1)$$$, with an empty set $$$S$$$. You need to walk along the edges and eventually reach $$$(n,n)$$$. Whenever you pass an edge, its value will be inserted into $$$S$$$. Please maximize the MEX$$$^{ ext{βˆ—}}$$$ of $$$S$$$ when you reach $$$(n,n)$$$. $$$^{ ext{βˆ—}}$$$The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of $$$
3,400
false
false
false
false
false
false
true
false
false
false
241
1033B
Alice has a lovely piece of cloth. It has the shape of a square with a side of length $$$a$$$ centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length $$$b$$$ centimeters (where $$$b < a$$$). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below). Alice would like to know whether the area of her cloth expressed in square centimeters is xa0β€” the number of test cases. Each of the next $$$t$$$ lines describes the $$$i$$$-th test case. It contains two integers $$$a$$$ and $$$b~(1 leq b < a leq 10^{11})$$$xa0β€” the side length of Alice's square and the side length of the square that Bob wants. Output Print $$$t$$$ lines, where the $$$i$$$-th line is the answer to the $$$i$$$-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO". You can print each letter in an arbitrary case (upper or lower). Example Input 4 6 5 16 13 61690850361 24777622630 34 33 Note The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is $$$6^2 - 5^2 = 36 - 25 = 11$$$, which is prime, so the answer is "YES". In the second case, the area is $$$16^2 - 13^2 = 87$$$, which is divisible by $$$3$$$. In the third case, the area of the remaining piece is $$$61690850361^2 - 24777622630^2 = 3191830435068605713421$$$. This number is not prime because $$$3191830435068605713421 = 36913227731 cdot 86468472991 $$$. In the last case, the area is $$$34^2 - 33^2 = 67$$$.
1,100
true
false
false
false
false
false
false
false
false
false
5,534
538D
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous. Igor's chessboard is a square of size _n_u2009Γ—u2009_n_ cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves. Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to _n_. Let's assign to each square a pair of integers (_x_,u2009_y_)xa0β€” the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (_dx_,u2009_dy_); using this move, the piece moves from the field (_x_,u2009_y_) to the field (_x_u2009+u2009_dx_,u2009_y_u2009+u2009_dy_). You can perform the move if the cell (_x_u2009+u2009_dx_,u2009_y_u2009+u2009_dy_) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (_x_,u2009_y_) and (_x_u2009+u2009_dx_,u2009_y_u2009+u2009_dy_) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess). Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors. Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u200950). The next _n_ lines contain _n_ characters each describing the position offered by Igor. The _j_-th character of the _i_-th string can have the following values: o β€” in this case the field (_i_,u2009_j_) is occupied by a piece and the field may or may not be attacked by some other piece; x β€” in this case field (_i_,u2009_j_) is attacked by some piece; . β€” in this case field (_i_,u2009_j_) isn't attacked by any piece. It is guaranteed that there is at least one piece on the board. Output If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2_n_u2009-u20091)u2009Γ—u2009(2_n_u2009-u20091) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them. If a valid set of moves does not exist, print a single word 'NO'.
1,800
false
false
true
false
false
true
true
false
false
false
7,685
268E
Manao's friends often send him new songs. He never listens to them right away. Instead, he compiles them into a playlist. When he feels that his mind is open to new music, he opens the playlist and starts to listen to the songs. Of course, there are some songs that Manao doesn't particuarly enjoy. To get more pleasure from the received songs, he invented the following procedure of listening to the playlist: If after listening to some song Manao realizes that he liked it, then he remembers it and starts to listen to the next unlistened song. If after listening to some song Manao realizes that he did not like it, he listens to all the songs he liked up to this point and then begins to listen to the next unlistened song. For example, if Manao has four songs in the playlist, A, B, C, D (in the corresponding order) and he is going to like songs A and C in the end, then the order of listening is the following: 1. Manao listens to A, he likes it, he remembers it. 2. Manao listens to B, he does not like it, so he listens to A, again. 3. Manao listens to C, he likes the song and he remembers it, too. 4. Manao listens to D, but does not enjoy it and re-listens to songs A and C. That is, in the end Manao listens to song A three times, to song C twice and songs B and D once. Note that if Manao once liked a song, he will never dislike it on a subsequent listening. Manao has received _n_ songs: the _i_-th of them is _l__i_ seconds long and Manao may like it with a probability of _p__i_ percents. The songs could get on Manao's playlist in any order, so Manao wants to know the maximum expected value of the number of seconds after which the listening process will be over, for all possible permutations of the songs in the playlist. Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u200950000). The _i_-th of the following _n_ lines contains two integers, separated by a single space β€” _l__i_ and _p__i_ (15u2009≀u2009_l__i_u2009≀u20091000, 0u2009≀u2009_p__i_u2009≀u2009100) β€” the length of the _i_-th song in seconds and the probability that Manao will like the song, in percents. Note Consider the first test case. If Manao listens to the songs in the order in which they were originally compiled, the mathematical expectation will be equal to 467.5 seconds. The maximum expected value is obtained by putting the first song at the end of the playlist. Consider the second test case. The song which is 360 seconds long should be listened to first. The song 300 seconds long which Manao will dislike for sure should be put in the end.
2,100
true
false
false
false
false
false
false
false
true
false
8,759
188F
Problem - 188F - Codeforces =============== xa0 . Output Output the binary notation of _n_ (without any leading zeros). Examples Input 5 Output 101 Input 101 Output 1100101 Note In the first example 5u2009=u20091u2009*u200922u2009+u20090u2009*u200921u2009+u20091u2009*u200920.
1,400
false
false
true
false
false
false
false
false
false
false
9,087
1455E
You are given four different integer points $$$p_1$$$, $$$p_2$$$, $$$p_3$$$ and $$$p_4$$$ on $$$mathit{XY}$$$ grid. In one step you can choose one of the points $$$p_i$$$ and move it in one of four directions by one. In other words, if you have chosen point $$$p_i = (x, y)$$$ you can move it to $$$(x, y + 1)$$$, $$$(x, y - 1)$$$, $$$(x + 1, y)$$$ or $$$(x - 1, y)$$$. Your goal to move points in such a way that they will form a square with sides parallel to $$$mathit{OX}$$$ and $$$mathit{OY}$$$ axes (a square with side $$$0$$$ is allowed). What is the minimum number of steps you need to make such a square? 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 four lines. Each line contains two integers $$$x$$$ and $$$y$$$ ($$$0 le x, y le 10^9$$$)xa0β€” coordinates of one of the points $$$p_i = (x, y)$$$. All points are different in one test case. Output For each test case, print the single integerxa0β€” the minimum number of steps to make a square. Example Input 3 0 2 4 2 2 0 2 4 1 0 2 0 4 0 6 0 1 6 2 2 2 5 4 1 Note In the first test case, one of the optimal solutions is shown below: Each point was moved two times, so the answer $$$2 + 2 + 2 + 2 = 8$$$. In the second test case, one of the optimal solutions is shown below: The answer is $$$3 + 1 + 0 + 3 = 7$$$. In the third test case, one of the optimal solutions is shown below: The answer is $$$1 + 1 + 2 + 1 = 5$$$.
2,400
true
true
true
false
false
true
true
false
false
false
3,398
1336C
Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. xa0Kaavi has a string $$$T$$$ of length $$$m$$$ and all the strings with the prefix $$$T$$$ are magic spells. Kaavi also has a string $$$S$$$ of length $$$n$$$ and an empty string $$$A$$$. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: Delete the first character of $$$S$$$ and add it at the front of $$$A$$$. Delete the first character of $$$S$$$ and add it at the back of $$$A$$$. Kaavi can perform no more than $$$n$$$ operations. To finish the divination, she wants to know the number of different operation sequences to make $$$A$$$ a magic spell (i.e. with the prefix $$$T$$$). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo $$$998,244,353$$$. Two operation sequences are considered different if they are different in length or there exists an $$$i$$$ that their $$$i$$$-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string $$$S$$$ is a substring of $$$S$$$ that occurs at the beginning of $$$S$$$. Input The first line contains a string $$$S$$$ of length $$$n$$$ ($$$1 leq n leq 3000$$$). The second line contains a string $$$T$$$ of length $$$m$$$ ($$$1 leq m leq n$$$). Both strings contain only lowercase Latin letters. Note The first test: The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of $$$A$$$, although the results are the same, they are considered as different operations. So the answer is $$$6 imes2=12$$$.
2,200
false
false
false
true
false
false
false
false
false
false
4,021
331E2
Everybody knows that we have been living in the Matrix for a long time. And in the new seventh Matrix the world is ruled by beavers. So let's take beaver Neo. Neo has so-called "deja vu" outbursts when he gets visions of events in some places he's been at or is going to be at. Let's examine the phenomenon in more detail. We can say that Neo's city is represented by a directed graph, consisting of _n_ shops and _m_ streets that connect the shops. No two streets connect the same pair of shops (besides, there can't be one street from A to B and one street from B to A). No street connects a shop with itself. As Neo passes some streets, he gets visions. No matter how many times he passes street _k_, every time he will get the same visions in the same order. A vision is a sequence of shops. We know that Neo is going to get really shocked if he passes the way from some shop _a_ to some shop _b_, possible coinciding with _a_, such that the list of visited shops in the real life and in the visions coincide. Suggest beaver Neo such path of non-zero length. Or maybe you can even count the number of such paths modulo 1000000007 (109u2009+u20097)?.. Input The first line contains integers _n_ and _m_ β€” the number of shops and the number of streets, correspondingly, 1u2009≀u2009_n_u2009≀u200950, . Next _m_ lines contain the descriptions of the streets in the following format: _x__i_ _y__i_ _k__i_ _v_1 _v_2 ... _v__k_, where _x__i_ and _y__i_ (1u2009≀u2009_x__i_,u2009_y__i_u2009≀u2009_n_,u2009_x__i_u2009β‰ u2009_y__i_) are numbers of shops connected by a street, _k__i_ (0u2009≀u2009_k__i_u2009≀u2009_n_) is the number of visions on the way from _x__i_ to _y__i_; _v_1, _v_2, ..., _v__k_ (1u2009≀u2009_v__i_u2009≀u2009_n_) describe the visions: the numbers of the shops Neo saw. Note that the order of the visions matters. It is guaranteed that the total number of visions on all streets doesn't exceed 105. to get 50 points, you need to find any (not necessarily simple) path of length at most 2Β·_n_, that meets the attributes described above (subproblem E1); to get 50 more points, you need to count for each length from 1 to 2Β·_n_ the number of paths that have the attribute described above (subproblem E2). Output Subproblem E1. In the first line print an integer _k_ (1u2009≀u2009_k_u2009≀u20092Β·_n_) β€” the numbers of shops on Neo's path. In the next line print _k_ integers β€” the number of shops in the order Neo passes them. If the graph doesn't have such paths or the length of the shortest path includes more than 2Β·_n_ shops, print on a single line 0. Subproblem E2. Print 2Β·_n_ lines. The _i_-th line must contain a single integer β€” the number of required paths of length _i_ modulo 1000000007 (109u2009+u20097). Examples Input 6 6 1 2 2 1 2 2 3 1 3 3 4 2 4 5 4 5 0 5 3 1 3 6 1 1 6 Input 6 6 1 2 2 1 2 2 3 1 3 3 4 2 4 5 4 5 0 5 3 1 3 6 1 1 6 Note The input in both samples are the same. The first sample contains the answer to the first subproblem, the second sample contains the answer to the second subproblem.
3,100
false
false
false
true
false
true
false
false
false
false
8,511
1133A
Polycarp is going to participate in the contest. It starts at $$$h_1:m_1$$$ and ends at $$$h_2:m_2$$$. It is guaranteed that the contest lasts an even number of minutes (i.e. $$$m_1 % 2 = m_2 % 2$$$, where $$$x % y$$$ is $$$x$$$ modulo $$$y$$$). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes. Polycarp wants to know the time of the midpoint of the contest. For example, if the contest lasts from $$$10:00$$$ to $$$11:00$$$ then the answer is $$$10:30$$$, if the contest lasts from $$$11:10$$$ to $$$11:12$$$ then the answer is $$$11:11$$$. Input The first line of the input contains two integers $$$h_1$$$ and $$$m_1$$$ in the format hh:mm. The second line of the input contains two integers $$$h_2$$$ and $$$m_2$$$ in the same format (hh:mm). It is guaranteed that $$$0 le h_1, h_2 le 23$$$ and $$$0 le m_1, m_2 le 59$$$. It is guaranteed that the contest lasts an even number of minutes (i.e. $$$m_1 % 2 = m_2 % 2$$$, where $$$x % y$$$ is $$$x$$$ modulo $$$y$$$). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes. Output Print two integers $$$h_3$$$ and $$$m_3$$$ ($$$0 le h_3 le 23, 0 le m_3 le 59$$$) corresponding to the midpoint of the contest in the format hh:mm. Print each number as exactly two digits (prepend a number with leading zero if needed), separate them with ':'.
1,000
false
false
true
false
false
false
false
false
false
false
5,054
1873B
Problem - 1873B - 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 greedy math *800 No tag edit access β†’ Contest materials ") xa0β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 9$$$)xa0β€” the number of digits. The second line of each test case contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$0 leq a_i leq 9$$$)xa0β€” the digits in the array. Output For each test case, output a single integerxa0β€” the maximum product Slavic can make, by adding $$$1$$$ to exactly one of his digits. Example Input 4 4 2 2 1 2 3 0 1 2 5 4 3 2 3 4 9 9 9 9 9 9 9 9 9 9 Output 16 2 432 430467210
800
true
true
false
false
false
false
true
false
false
false
1,043
1730D
You have two strings $$$s_1$$$ and $$$s_2$$$ of length $$$n$$$, consisting of lowercase English letters. You can perform the following operation any (possibly zero) number of times: Choose a positive integer $$$1 leq k leq n$$$. Swap the prefix of the string $$$s_1$$$ and the suffix of the string $$$s_2$$$ of length $$$k$$$. Is it possible to make these two strings equal by doing described operations? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. Then the test cases follow. Each test case consists of three lines. The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) β€” the length of the strings $$$s_1$$$ and $$$s_2$$$. The second line contains the string $$$s_1$$$ of length $$$n$$$, consisting of lowercase English letters. The third line contains the string $$$s_2$$$ of length $$$n$$$, consisting of lowercase English letters. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print "YES" if it is possible to make the strings equal, and "NO" otherwise. Example Input 7 3 cbc aba 5 abcaa cbabb 5 abcaa cbabz 1 a a 1 a b 6 abadaa adaaba 8 abcabdaa adabcaba Output YES YES NO YES NO NO YES Note In the first test case: Initially $$$s_1 = mathtt{cbc}$$$, $$$s_2 = mathtt{aba}$$$. Operation with $$$k = 1$$$, after the operation $$$s_1 = mathtt{abc}$$$, $$$s_2 = mathtt{abc}$$$. In the second test case: Initially $$$s_1 = mathtt{abcaa}$$$, $$$s_2 = mathtt{cbabb}$$$. Operation with $$$k = 2$$$, after the operation $$$s_1 = mathtt{bbcaa}$$$, $$$s_2 = mathtt{cbaab}$$$. Operation with $$$k = 3$$$, after the operation $$$s_1 = mathtt{aabaa}$$$, $$$s_2 = mathtt{cbbbc}$$$. Operation with $$$k = 1$$$, after the operation $$$s_1 = mathtt{cabaa}$$$, $$$s_2 = mathtt{cbbba}$$$. Operation with $$$k = 2$$$, after the operation $$$s_1 = mathtt{babaa}$$$, $$$s_2 = mathtt{cbbca}$$$. Operation with $$$k = 1$$$, after the operation $$$s_1 = mathtt{aabaa}$$$, $$$s_2 = mathtt{cbbcb}$$$. Operation with $$$k = 2$$$, after the operation $$$s_1 = mathtt{cbbaa}$$$, $$$s_2 = mathtt{cbbaa}$$$. In the third test case, it's impossible to make strings equal.
2,200
false
false
false
false
false
true
false
false
false
false
1,908
1610D
Lee couldn't sleep lately, because he had nightmares. In one of his nightmares (which was about an unbalanced global round), he decided to fight back and propose a problem below (which you should solve) to balance the round, hopefully setting him free from the nightmares. A non-empty array $$$b_1, b_2, ldots, b_m$$$ is called good, if there exist $$$m$$$ integer sequences which satisfy the following properties: The $$$i$$$-th sequence consists of $$$b_i$$$ consecutive integers (for example if $$$b_i = 3$$$ then the $$$i$$$-th sequence can be $$$(-1, 0, 1)$$$ or $$$(-5, -4, -3)$$$ but not $$$(0, -1, 1)$$$ or $$$(1, 2, 3, 4)$$$). Assuming the sum of integers in the $$$i$$$-th sequence is $$$sum_i$$$, we want $$$sum_1 + sum_2 + ldots + sum_m$$$ to be equal to $$$0$$$. You are given an array $$$a_1, a_2, ldots, a_n$$$. It has $$$2^n - 1$$$ nonempty subsequences. Find how many of them are good. As this number can be very large, output it modulo $$$10^9 + 7$$$. An array $$$c$$$ is a subsequence of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0β€” the size of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β€” elements of the array. Output Print a single integerxa0β€” the number of nonempty good subsequences of $$$a$$$, modulo $$$10^9 + 7$$$. Examples Input 10 12391240 103904 1000000000 4142834 12039 142035823 1032840 49932183 230194823 984293123 Note For the first test, two examples of good subsequences are $$$[2, 7]$$$ and $$$[2, 2, 4, 7]$$$: For $$$b = [2, 7]$$$ we can use $$$(-3, -4)$$$ as the first sequence and $$$(-2, -1, ldots, 4)$$$ as the second. Note that subsequence $$$[2, 7]$$$ appears twice in $$$[2, 2, 4, 7]$$$, so we have to count it twice. Green circles denote $$$(-3, -4)$$$ and orange squares denote $$$(-2, -1, ldots, 4)$$$. For $$$b = [2, 2, 4, 7]$$$ the following sequences would satisfy the properties: $$$(-1, 0)$$$, $$$(-3, -2)$$$, $$$(0, 1, 2, 3)$$$ and $$$(-3, -2, ldots, 3)$$$
2,000
true
false
false
true
false
false
false
false
false
false
2,597
1881G
Anya received a string $$$s$$$ of length $$$n$$$ brought from Rome. The string $$$s$$$ consists of lowercase Latin letters and at first glance does not raise any suspicions. An instruction was attached to the string. Start of the instruction. A palindrome is a string that reads the same from left to right and right to left. For example, the strings "anna", "aboba", "level" are palindromes, while the strings "gorilla", "banan", "off" are not. A substring $$$[l ldots r]$$$ of string $$$s$$$ is a string $$$s_l s_{l+1} ldots s_{r-1} s_r$$$. For example, the substring $$$[4 ldots 6]$$$ of the string "generation" is the string "era". A string is called beautiful if it does not contain a substring of length at least two that is a palindrome. For example, the strings "fox", "abcdef", and "yioy" are beautiful, while the strings "xyxx", "yikjkitrb" are not. When an integer $$$x$$$ is added to the character $$$s_i$$$, it is replaced $$$x$$$ times with the next character in the alphabet, with "z" being replaced by "a". When an integer $$$x$$$ is added to the substring $$$[l, r]$$$ of string $$$s$$$, it becomes the string $$$s_1 s_2 ldots s_{l-1} (s_l + x) (s_{l+1} + x) ldots (s_{r-1} + x) (s_r + x) s_{r+1} ldots s_n$$$. For example, when the substring $$$[2, 4]$$$ of the string "abazaba" is added with the number $$$6$$$, the resulting string is "ahgfaba". End of the instruction. After reading the instruction, Anya resigned herself to the fact that she has to answer $$$m$$$ queries. The queries can be of two types: 1. Add the number $$$x$$$ to the substring $$$[l ldots r]$$$ of string $$$s$$$. 2. Determine whether the substring $$$[l ldots r]$$$ of string $$$s$$$ is beautiful. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) - the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) - the length of the string $$$s$$$ and the number of queries. The second line of each test case contains the string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters. The next $$$m$$$ lines contain the queries: $$$1$$$ $$$l$$$ $$$r$$$ $$$x$$$ ($$$1 le l le r le n$$$, $$$1 le x le 10^9$$$) - description of a query of the first type; $$$2$$$ $$$l$$$ $$$r$$$ ($$$1 le l le r le n$$$) - description of a query of the second type. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 cdot 10^5$$$. Output For each query of the second type, output "YES" if the substring $$$[l ldots r]$$$ of string $$$s$$$ is beautiful, otherwise output "NO". You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers).
2,000
false
false
false
false
true
false
false
true
false
false
998
1405A
A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Let $$$p$$$ be any permutation of length $$$n$$$. We define the fingerprint $$$F(p)$$$ of $$$p$$$ as the sorted array of sums of adjacent elements in $$$p$$$. More formally, $$$$$$F(p)=mathrm{sort}([p_1+p_2,p_2+p_3,ldots,p_{n-1}+p_n]).$$$$$$ For example, if $$$n=4$$$ and $$$p=[1,4,2,3],$$$ then the fingerprint is given by $$$F(p)=mathrm{sort}([1+4,4+2,2+3])=mathrm{sort}([5,6,5])=[5,5,6]$$$. You are given a permutation $$$p$$$ of length $$$n$$$. Your task is to find a different permutation $$$p'$$$ with the same fingerprint. Two permutations $$$p$$$ and $$$p'$$$ are considered different if there is some index $$$i$$$ such that $$$p_i e p'_i$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 668$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2le nle 100$$$) xa0β€” the length of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1,ldots,p_n$$$ ($$$1le p_ile n$$$). It is guaranteed that $$$p$$$ is a permutation. Output For each test case, output $$$n$$$ integers $$$p'_1,ldots, p'_n$$$ β€” a permutation such that $$$p' e p$$$ and $$$F(p')=F(p)$$$. We can prove that for every permutation satisfying the input constraints, a solution exists. If there are multiple solutions, you may output any. Example Input 3 2 1 2 6 2 1 6 5 4 3 5 2 4 3 1 5 Output 2 1 1 2 5 6 3 4 3 1 5 2 4 Note In the first test case, $$$F(p)=mathrm{sort}([1+2])=[3]$$$. And $$$F(p')=mathrm{sort}([2+1])=[3]$$$. In the second test case, $$$F(p)=mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$$$. And $$$F(p')=mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$$$. In the third test case, $$$F(p)=mathrm{sort}([2+4,4+3,3+1,1+5])=mathrm{sort}([6,7,4,6])=[4,6,6,7]$$$. And $$$F(p')=mathrm{sort}([3+1,1+5,5+2,2+4])=mathrm{sort}([4,6,7,6])=[4,6,6,7]$$$.
800
false
false
false
false
false
true
false
false
false
false
3,648
250C
A film festival is coming up in the city N. The festival will last for exactly _n_ days and each day will have a premiere of exactly one film. Each film has a genre β€” an integer from 1 to _k_. On the _i_-th day the festival will show a movie of genre _a__i_. We know that a movie of each of _k_ genres occurs in the festival programme at least once. In other words, each integer from 1 to _k_ occurs in the sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ at least once. Valentine is a movie critic. He wants to watch some movies of the festival and then describe his impressions on his site. As any creative person, Valentine is very susceptive. After he watched the movie of a certain genre, Valentine forms the mood he preserves until he watches the next movie. If the genre of the next movie is the same, it does not change Valentine's mood. If the genres are different, Valentine's mood changes according to the new genre and Valentine has a stress. Valentine can't watch all _n_ movies, so he decided to exclude from his to-watch list movies of one of the genres. In other words, Valentine is going to choose exactly one of the _k_ genres and will skip all the movies of this genre. He is sure to visit other movies. Valentine wants to choose such genre _x_ (1u2009≀u2009_x_u2009≀u2009_k_), that the total number of after-movie stresses (after all movies of genre _x_ are excluded) were minimum. Input The first line of the input contains two integers _n_ and _k_ (2u2009≀u2009_k_u2009≀u2009_n_u2009≀u2009105), where _n_ is the number of movies and _k_ is the number of genres. The second line of the input contains a sequence of _n_ positive integers _a_1, _a_2, ..., _a__n_ (1u2009≀u2009_a__i_u2009≀u2009_k_), where _a__i_ is the genre of the _i_-th movie. It is guaranteed that each number from 1 to _k_ occurs at least once in this sequence. Output Print a single number β€” the number of the genre (from 1 to _k_) of the excluded films. If there are multiple answers, print the genre with the minimum number. Examples Input 10 3 1 1 2 3 2 3 3 1 1 3 Note In the first sample if we exclude the movies of the 1st genre, the genres 2,u20093,u20092,u20093,u20093,u20093 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1,u20091,u20093,u20093,u20093,u20091,u20091,u20093 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1,u20091,u20092,u20092,u20091,u20091 remain, that is 2 stresses. In the second sample whatever genre Valentine excludes, he will have exactly 3 stresses.
1,600
false
true
false
false
false
false
false
false
false
false
8,834
994B
Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than $$$k$$$ other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins. Now each knight ponders: how many coins he can have if only he kills other knights? You should answer this question for each knight. Input The first line contains two integers $$$n$$$ and $$$k$$$ $$$(1 le n le 10^5, 0 le k le min(n-1,10))$$$ β€” the number of knights and the number $$$k$$$ from the statement. The second line contains $$$n$$$ integers $$$p_1, p_2 ,ldots,p_n$$$ $$$(1 le p_i le 10^9)$$$ β€” powers of the knights. All $$$p_i$$$ are distinct. The third line contains $$$n$$$ integers $$$c_1, c_2 ,ldots,c_n$$$ $$$(0 le c_i le 10^9)$$$ β€” the number of coins each knight has. Output Print $$$n$$$ integers β€” the maximum number of coins each knight can have it only he kills other knights. Examples Input 5 1 1 2 3 4 5 1 2 3 4 5 Note Consider the first example. The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has. The second knight can kill the first knight and add his coin to his own two. The third knight is the strongest, but he can't kill more than $$$k = 2$$$ other knights. It is optimal to kill the second and the fourth knights: $$$2+11+33 = 46$$$. The fourth knight should kill the first and the second knights: $$$33+1+2 = 36$$$. In the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own. In the third example there is only one knight, so he can't kill anyone.
1,400
false
true
true
false
false
false
false
false
true
false
5,727
534A
Problem - 534A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags constructive algorithms implementation math *1100 No tag edit access β†’ Contest materials ") always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure. Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side. Input A single line contains integer _n_ (1u2009≀u2009_n_u2009≀u20095000) β€” the number of students at an exam. Output In the first line print integer _k_ β€” the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other. In the second line print _k_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__k_ (1u2009≀u2009_a__i_u2009≀u2009_n_), where _a__i_ is the number of the student on the _i_-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: _a__i_u2009-u2009_a__i_u2009+u20091u2009β‰ u20091 for all _i_ from 1 to _k_u2009-u20091. If there are several possible answers, output any of them. Examples Input 6 Output 6 1 5 3 6 2 4 Input 3 Output 2 1 3
1,100
true
false
true
false
false
true
false
false
false
false
7,701
852C
Bill is a famous mathematician in BubbleLand. Thanks to his revolutionary math discoveries he was able to make enough money to build a beautiful house. Unfortunately, for not paying property tax on time, court decided to punish Bill by making him lose a part of his property. Bill’s property can be observed as a convex regular 2_n_-sided polygon _A_0 _A_1... _A_2_n_u2009-u20091 _A_2_n_,u2009 _A_2_n_u2009=u2009 _A_0, with sides of the exactly 1 meter in length. Court rules for removing part of his property are as follows: Split every edge _A__k_ _A__k_u2009+u20091,u2009 _k_u2009=u20090... 2_n_u2009-u20091 in _n_ equal parts of size 1u2009/u2009_n_ with points _P_0,u2009_P_1,u2009...,u2009_P__n_u2009-u20091 On every edge _A_2_k_ _A_2_k_u2009+u20091,u2009 _k_u2009=u20090... _n_u2009-u20091 court will choose one point _B_2_k_u2009=u2009 _P__i_ for some _i_u2009=u20090,u2009...,u2009 _n_u2009-u20091 such that On every edge _A_2_k_u2009+u20091_A_2_k_u2009+u20092,u2009 _k_u2009=u20090..._n_u2009-u20091 Bill will choose one point _B_2_k_u2009+u20091u2009=u2009 _P__i_ for some _i_u2009=u20090,u2009...,u2009 _n_u2009-u20091 such that Bill gets to keep property inside of 2_n_-sided polygon _B_0 _B_1... _B_2_n_u2009-u20091 Luckily, Bill found out which _B_2_k_ points the court chose. Even though he is a great mathematician, his house is very big and he has a hard time calculating. Therefore, he is asking you to help him choose points so he maximizes area of property he can keep. Input The first line contains one integer number _n_ (2u2009≀u2009_n_u2009≀u200950000), representing number of edges of 2_n_-sided polygon. The second line contains _n_ distinct integer numbers _B_2_k_ (0u2009≀u2009_B_2_k_u2009≀u2009_n_u2009-u20091,u2009 _k_u2009=u20090... _n_u2009-u20091) separated by a single space, representing points the court chose. If _B_2_k_u2009=u2009_i_, the court chose point _P__i_ on side _A_2_k_ _A_2_k_u2009+u20091. Output Output contains _n_ distinct integers separated by a single space representing points _B_1,u2009_B_3,u2009...,u2009_B_2_n_u2009-u20091 Bill should choose in order to maximize the property area. If there are multiple solutions that maximize the area, return any of them. Note To maximize area Bill should choose points: _B_1u2009=u2009_P_0, _B_3u2009=u2009_P_2, _B_5u2009=u2009_P_1
2,100
false
true
false
false
false
false
false
false
true
false
6,341
1523H
William really wants to get a pet. Since his childhood he dreamt about getting a pet grasshopper. William is being very responsible about choosing his pet, so he wants to set up a trial for the grasshopper! The trial takes place on an array $$$a$$$ of length $$$n$$$, which defines lengths of hops for each of $$$n$$$ cells. A grasshopper can hop around the sells according to the following rule: from a cell with index $$$i$$$ it can jump to any cell with indices from $$$i$$$ to $$$i+a_i$$$ inclusive. Let's call the $$$k$$$-grasshopper value of some array the smallest number of hops it would take a grasshopper to hop from the first cell to the last, but before starting you can select no more than $$$k$$$ cells and remove them from the array. When a cell is removed all other cells are renumbered but the values of $$$a_i$$$ for each cell remains the same. During this the first and the last cells may not be removed. It is required to process $$$q$$$ queries of the following format: you are given three numbers $$$l$$$, $$$r$$$, $$$k$$$. You are required to find the $$$k$$$-grasshopper value for an array, which is a subarray of the array $$$a$$$ with elements from $$$l$$$ to $$$r$$$ inclusive. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 20000$$$), the length of the array and the number of queries respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$) xa0– the elements of the array. The following $$$q$$$ lines contain queries: each line contains three integers $$$l$$$, $$$r$$$ and $$$k$$$ ($$$1 le l le r le n$$$, $$$0 le k le min(30, r-l)$$$), which are the edges of the subarray and the number of the grasshopper value respectively. Output For each query print a single number in a new line xa0β€” the response to a query. Example Input 9 5 1 1 2 1 3 1 2 1 1 1 1 0 2 5 1 5 9 1 2 8 2 1 9 4 Note For the second query the process occurs like this: For the third query the process occurs like this:
3,500
false
false
false
true
true
false
false
false
false
false
3,043
1257E
A team of three programmers is going to play a contest. The contest consists of $$$n$$$ problems, numbered from $$$1$$$ to $$$n$$$. Each problem is printed on a separate sheet of paper. The participants have decided to divide the problem statements into three parts: the first programmer took some prefix of the statements (some number of first paper sheets), the third contestant took some suffix of the statements (some number of last paper sheets), and the second contestant took all remaining problems. But something went wrong β€” the statements were printed in the wrong order, so the contestants have received the problems in some random order. The first contestant has received problems $$$a_{1, 1}, a_{1, 2}, dots, a_{1, k_1}$$$. The second one has received problems $$$a_{2, 1}, a_{2, 2}, dots, a_{2, k_2}$$$. The third one has received all remaining problems ($$$a_{3, 1}, a_{3, 2}, dots, a_{3, k_3}$$$). The contestants don't want to play the contest before they redistribute the statements. They want to redistribute them so that the first contestant receives some prefix of the problemset, the third contestant receives some suffix of the problemset, and the second contestant receives all the remaining problems. During one move, some contestant may give one of their problems to other contestant. What is the minimum number of moves required to redistribute the problems? It is possible that after redistribution some participant (or even two of them) will not have any problems. Input The first line contains three integers $$$k_1, k_2$$$ and $$$k_3$$$ ($$$1 le k_1, k_2, k_3 le 2 cdot 10^5, k_1 + k_2 + k_3 le 2 cdot 10^5$$$) β€” the number of problems initially taken by the first, the second and the third participant, respectively. The second line contains $$$k_1$$$ integers $$$a_{1, 1}, a_{1, 2}, dots, a_{1, k_1}$$$ β€” the problems initially taken by the first participant. The third line contains $$$k_2$$$ integers $$$a_{2, 1}, a_{2, 2}, dots, a_{2, k_2}$$$ β€” the problems initially taken by the second participant. The fourth line contains $$$k_3$$$ integers $$$a_{3, 1}, a_{3, 2}, dots, a_{3, k_3}$$$ β€” the problems initially taken by the third participant. It is guaranteed that no problem has been taken by two (or three) participants, and each integer $$$a_{i, j}$$$ meets the condition $$$1 le a_{i, j} le n$$$, where $$$n = k_1 + k_2 + k_3$$$. Output Print one integer β€” the minimum number of moves required to redistribute the problems so that the first participant gets the prefix of the problemset, the third participant gets the suffix of the problemset, and the second participant gets all of the remaining problems. Note In the first example the third contestant should give the problem $$$2$$$ to the first contestant, so the first contestant has $$$3$$$ first problems, the third contestant has $$$1$$$ last problem, and the second contestant has $$$1$$$ remaining problem. In the second example the distribution of problems is already valid: the first contestant has $$$3$$$ first problems, the third contestant has $$$1$$$ last problem, and the second contestant has $$$2$$$ remaining problems. The best course of action in the third example is to give all problems to the third contestant. The best course of action in the fourth example is to give all problems to the second contestant.
2,000
false
true
false
true
true
false
false
false
false
false
4,397
1225B1
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $$$k$$$ TV shows. You know the schedule for the next $$$n$$$ days: a sequence of integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le k$$$), where $$$a_i$$$ is the show, the episode of which will be shown in $$$i$$$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $$$d$$$ ($$$1 le d le n$$$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $$$d$$$ consecutive days in which all episodes belong to the purchased shows. Input The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$) β€” the number of test cases in the input. Then $$$t$$$ test case descriptions follow. The first line of each test case contains three integers $$$n, k$$$ and $$$d$$$ ($$$1 le n le 100$$$, $$$1 le k le 100$$$, $$$1 le d le n$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le k$$$), where $$$a_i$$$ is the show that is broadcasted on the $$$i$$$-th day. It is guaranteed that the sum of the values u200bu200bof $$$n$$$ for all test cases in the input does not exceed $$$100$$$. Output Print $$$t$$$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $$$d$$$ consecutive days. Please note that it is permissible that you will be able to watch more than $$$d$$$ days in a row. Example Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Note In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $$$1$$$ and on show $$$2$$$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $$$3,5,7,8,9$$$, and you will be able to watch shows for the last eight days.
1,000
false
false
true
false
false
false
false
false
false
false
4,551
1720A
Burenka came to kindergarden. This kindergarten is quite strange, so each kid there receives two fractions ($$$frac{a}{b}$$$ and $$$frac{c}{d}$$$) with integer numerators and denominators. Then children are commanded to play with their fractions. Burenka is a clever kid, so she noticed that when she claps once, she can multiply numerator or denominator of one of her two fractions by any integer of her choice (but she can't multiply denominators by $$$0$$$). Now she wants know the minimal number of claps to make her fractions equal (by value). Please help her and find the required number of claps! Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$) β€” the number of test cases. Then follow the descriptions of each test case. The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$0 leq a, c leq 10^9$$$, $$$1 leq b, d leq 10^9$$$) β€” numerators and denominators of the fractions given to Burenka initially. Output For each test case print a single integer β€” the minimal number of claps Burenka needs to make her fractions equal. Example Input 8 2 1 1 1 6 3 2 1 1 2 2 3 0 1 0 100 0 1 228 179 100 3 25 6 999999999 300000000 666666666 100000000 33 15 0 84 Note In the first case, Burenka can multiply $$$c$$$ by $$$2$$$, then the fractions will be equal. In the second case, fractions are already equal. In the third case, Burenka can multiply $$$a$$$ by $$$4$$$, then $$$b$$$ by $$$3$$$. Then the fractions will be equal ($$$frac{1 cdot 4}{2 cdot 3} = frac{2}{3}$$$).
900
true
false
false
false
false
false
false
false
false
false
1,965
1056B
Arkady and his friends love playing checkers on an $$$n imes n$$$ field. The rows and the columns of the field are enumerated from $$$1$$$ to $$$n$$$. The friends have recently won a championship, so Arkady wants to please them with some candies. Remembering an old parable (but not its moral), Arkady wants to give to his friends one set of candies per each cell of the field: the set of candies for cell $$$(i, j)$$$ will have exactly $$$(i^2 + j^2)$$$ candies of unique type. There are $$$m$$$ friends who deserve the present. How many of these $$$n imes n$$$ sets of candies can be split equally into $$$m$$$ parts without cutting a candy into pieces? Note that each set has to be split independently since the types of candies in different sets are different. Input The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^9$$$, $$$1 le m le 1000$$$)xa0β€” the size of the field and the number of parts to split the sets into. Output Print a single integerxa0β€” the number of sets that can be split equally. Examples Output 1000000000000000000 Note In the first example, only the set for cell $$$(3, 3)$$$ can be split equally ($$$3^2 + 3^2 = 18$$$, which is divisible by $$$m=3$$$). In the second example, the sets for the following cells can be divided equally: $$$(1, 2)$$$ and $$$(2, 1)$$$, since $$$1^2 + 2^2 = 5$$$, which is divisible by $$$5$$$; $$$(1, 3)$$$ and $$$(3, 1)$$$; $$$(2, 4)$$$ and $$$(4, 2)$$$; $$$(2, 6)$$$ and $$$(6, 2)$$$; $$$(3, 4)$$$ and $$$(4, 3)$$$; $$$(3, 6)$$$ and $$$(6, 3)$$$; $$$(5, 5)$$$. In the third example, sets in all cells can be divided equally, since $$$m = 1$$$.
1,600
true
false
false
false
false
false
false
false
false
false
5,431
298B
The polar bears are going fishing. They plan to sail from (_s__x_,u2009_s__y_) to (_e__x_,u2009_e__y_). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (_x_,u2009_y_). If the wind blows to the east, the boat will move to (_x_u2009+u20091,u2009_y_). If the wind blows to the south, the boat will move to (_x_,u2009_y_u2009-u20091). If the wind blows to the west, the boat will move to (_x_u2009-u20091,u2009_y_). If the wind blows to the north, the boat will move to (_x_,u2009_y_u2009+u20091). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (_x_,u2009_y_). Given the wind direction for _t_ seconds, what is the earliest time they sail to (_e__x_,u2009_e__y_)? Input The first line contains five integers _t_,u2009_s__x_,u2009_s__y_,u2009_e__x_,u2009_e__y_ (1u2009≀u2009_t_u2009≀u2009105,u2009u2009-u2009109u2009≀u2009_s__x_,u2009_s__y_,u2009_e__x_,u2009_e__y_u2009≀u2009109). The starting location and the ending location will be different. The second line contains _t_ characters, the _i_-th character is the wind blowing direction at the _i_-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north). Output If they can reach (_e__x_,u2009_e__y_) within _t_ seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes). Examples Input 10 5 3 3 6 NENSWESNEE Note In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. In the second sample, they cannot sail to the destination.
1,200
false
true
true
false
false
false
true
false
false
false
8,638
723B
Modern text editors usually show some information regarding the document being edited. For example, the number of words, the number of pages, or the number of characters. In this problem you should implement the similar functionality. You are given a string which only consists of: uppercase and lowercase English letters, underscore symbols (they are used as separators), parentheses (both opening and closing). It is guaranteed that each opening parenthesis has a succeeding closing parenthesis. Similarly, each closing parentheses has a preceding opening parentheses matching it. For each pair of matching parentheses there are no other parenthesis between them. In other words, each parenthesis in the string belongs to a matching "opening-closing" pair, and such pairs can't be nested. For example, the following string is valid: "_Hello_Vasya(and_Petya)__bye_(and_OK)". Word is a maximal sequence of consecutive letters, i.e. such sequence that the first character to the left and the first character to the right of it is an underscore, a parenthesis, or it just does not exist. For example, the string above consists of seven words: "Hello", "Vasya", "and", "Petya", "bye", "and" and "OK". Write a program that finds: the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses), the number of words inside the parentheses (print 0, if there is no word inside the parentheses). Input The first line of the input contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009255)xa0β€” the length of the given string. The second line contains the string consisting of only lowercase and uppercase English letters, parentheses and underscore symbols. Note In the first sample, the words "Hello", "Vasya" and "bye" are outside any of the parentheses, and the words "and", "Petya", "and" and "OK" are inside. Note, that the word "and" is given twice and you should count it twice in the answer.
1,100
false
false
true
false
false
false
false
false
false
false
6,919
359A
Simon has a rectangular table consisting of _n_ rows and _m_ columns. Simon numbered the rows of the table from top to bottom starting from one and the columns β€” from left to right starting from one. We'll represent the cell on the _x_-th row and the _y_-th column as a pair of numbers (_x_,u2009_y_). The table corners are cells: (1,u20091), (_n_,u20091), (1,u2009_m_), (_n_,u2009_m_). Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table. Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (_x_1,u2009_y_1), an arbitrary corner of the table (_x_2,u2009_y_2) and color all cells of the table (_p_,u2009_q_), which meet both inequations: _min_(_x_1,u2009_x_2)u2009≀u2009_p_u2009≀u2009_max_(_x_1,u2009_x_2), _min_(_y_1,u2009_y_2)u2009≀u2009_q_u2009≀u2009_max_(_y_1,u2009_y_2). Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times. Input The first line contains exactly two integers _n_, _m_ (3u2009≀u2009_n_,u2009_m_u2009≀u200950). Next _n_ lines contain the description of the table cells. Specifically, the _i_-th line contains _m_ space-separated integers _a__i_1,u2009_a__i_2,u2009...,u2009_a__im_. If _a__ij_ equals zero, then cell (_i_,u2009_j_) isn't good. Otherwise _a__ij_ equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner. Note In the first sample, the sequence of operations can be like this: For the first time you need to choose cell (2,u20092) and corner (1,u20091). For the second time you need to choose cell (2,u20092) and corner (3,u20093). For the third time you need to choose cell (2,u20092) and corner (3,u20091). For the fourth time you need to choose cell (2,u20092) and corner (1,u20093). In the second sample the sequence of operations can be like this: For the first time you need to choose cell (3,u20091) and corner (4,u20093). For the second time you need to choose cell (2,u20093) and corner (1,u20091).
1,000
false
true
true
false
false
true
false
false
false
false
8,399
1231E
The problem was inspired by Pied Piper story. After a challenge from Hooli's compression competitor Nucleus, Richard pulled an all-nighter to invent a new approach to compression: middle-out. You are given two strings $$$s$$$ and $$$t$$$ of the same length $$$n$$$. Their characters are numbered from $$$1$$$ to $$$n$$$ from left to right (i.e. from the beginning to the end). In a single move you can do the following sequence of actions: choose any valid index $$$i$$$ ($$$1 le i le n$$$), move the $$$i$$$-th character of $$$s$$$ from its position to the beginning of the string or move the $$$i$$$-th character of $$$s$$$ from its position to the end of the string. Note, that the moves don't change the length of the string $$$s$$$. You can apply a move only to the string $$$s$$$. For example, if $$$s=$$$"test" in one move you can obtain: if $$$i=1$$$ and you move to the beginning, then the result is "test" (the string doesn't change), if $$$i=2$$$ and you move to the beginning, then the result is "etst", if $$$i=3$$$ and you move to the beginning, then the result is "stet", if $$$i=4$$$ and you move to the beginning, then the result is "ttes", if $$$i=1$$$ and you move to the end, then the result is "estt", if $$$i=2$$$ and you move to the end, then the result is "tste", if $$$i=3$$$ and you move to the end, then the result is "tets", if $$$i=4$$$ and you move to the end, then the result is "test" (the string doesn't change). You want to make the string $$$s$$$ equal to the string $$$t$$$. What is the minimum number of moves you need? If it is impossible to transform $$$s$$$ to $$$t$$$, print -1. Input The first line contains integer $$$q$$$ ($$$1 le q le 100$$$) β€” the number of independent test cases in the input. Each test case is given in three lines. The first line of a test case contains $$$n$$$ ($$$1 le n le 100$$$) β€” the length of the strings $$$s$$$ and $$$t$$$. The second line contains $$$s$$$, the third line contains $$$t$$$. Both strings $$$s$$$ and $$$t$$$ have length $$$n$$$ and contain only lowercase Latin letters. There are no constraints on the sum of $$$n$$$ in the test (i.e. the input with $$$q=100$$$ and all $$$n=100$$$ is allowed). Output For every test print minimum possible number of moves, which are needed to transform $$$s$$$ into $$$t$$$, or -1, if it is impossible to do. Examples Input 3 9 iredppipe piedpiper 4 estt test 4 tste test Input 4 1 a z 5 adhas dasha 5 aashd dasha 5 aahsd dasha Note In the first example, the moves in one of the optimal answers are: for the first test case $$$s=$$$"iredppipe", $$$t=$$$"piedpiper": "iredppipe" $$$ ightarrow$$$ "iedppiper" $$$ ightarrow$$$ "piedpiper"; for the second test case $$$s=$$$"estt", $$$t=$$$"test": "estt" $$$ ightarrow$$$ "test"; for the third test case $$$s=$$$"tste", $$$t=$$$"test": "tste" $$$ ightarrow$$$ "etst" $$$ ightarrow$$$ "test".
2,200
false
true
false
false
false
true
false
false
false
false
4,526
1999E
Problem - 1999E - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags dp implementation math *1300 No tag edit access β†’ Contest materials ") Editorial") . What is the minimum number of operations Ivy needs to make all numbers on the board equal $$$0$$$? We have a proof that this is always possible. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β€” the number of test cases. The only line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 leq l < r leq 2 cdot 10^5$$$). Output For each test case, output a single integerxa0β€” the minimum number of operations needed to make all numbers on the board equal $$$0$$$. Example Input 4 1 3 2 4 199999 200000 19 84 Output 5 6 36 263 Note In the first test case, we can perform $$$5$$$ operations as follows: $$$$$$ 1,2,3 xrightarrow
1,300
true
false
true
true
false
false
false
false
false
false
267
676A
Nicholas has an array _a_ that contains _n_ distinct integers from 1 to _n_. In other words, Nicholas has a permutation of size _n_. Nicholas want the minimum element (integer 1) and the maximum element (integer _n_) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions. Input The first line of the input contains a single integer _n_ (2u2009≀u2009_n_u2009≀u2009100)xa0β€” the size of the permutation. The second line of the input contains _n_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≀u2009_a__i_u2009≀u2009_n_), where _a__i_ is equal to the element at the _i_-th position. Output Print a single integerxa0β€” the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap. Note In the first sample, one may obtain the optimal answer by swapping elements 1 and 2. In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2. In the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap 5 and 2.
800
false
false
true
false
false
true
false
false
false
false
7,126
706A
Vasiliy lives at point (_a_,u2009_b_) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested _n_ available Beru-taxi nearby. The _i_-th taxi is located at point (_x__i_,u2009_y__i_) and moves with a speed _v__i_. Consider that each of _n_ drivers will move directly to Vasiliy and with a maximum possible speed. Compute the minimum time when Vasiliy will get in any of Beru-taxi cars. Input The first line of the input contains two integers _a_ and _b_ (u2009-u2009100u2009≀u2009_a_,u2009_b_u2009≀u2009100)xa0β€” coordinates of Vasiliy's home. The second line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u20091000)xa0β€” the number of available Beru-taxi cars nearby. The _i_-th of the following _n_ lines contains three integers _x__i_, _y__i_ and _v__i_ (u2009-u2009100u2009≀u2009_x__i_,u2009_y__i_u2009≀u2009100, 1u2009≀u2009_v__i_u2009≀u2009100)xa0β€” the coordinates of the _i_-th car and its speed. It's allowed that several cars are located at the same point. Also, cars may be located at exactly the same point where Vasiliy lives. Output Print a single real valuexa0β€” the minimum time Vasiliy needs to get in any of the Beru-taxi cars. You 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 . Examples Output 1.00000000000000000000 Input 1 3 3 3 3 2 -2 3 6 -2 7 10 Output 0.50000000000000000000 Note In the first sample, first taxi will get to Vasiliy in time 2, and second will do this in time 1, therefore 1 is the answer. In the second sample, cars 2 and 3 will arrive simultaneously.
900
false
false
true
false
false
false
true
false
false
false
6,999
1733A
You are given an array $$$a$$$ with $$$n$$$ integers. You can perform the following operation at most $$$k$$$ times: Choose two indices $$$i$$$ and $$$j$$$, in which $$$i ,bmod, k = j ,bmod, k$$$ ($$$1 le i < j le n$$$). Swap $$$a_i$$$ and $$$a_j$$$. After performing all operations, you have to select $$$k$$$ consecutive elements, and the sum of the $$$k$$$ elements becomes your score. Find the maximum score you can get. Here $$$x bmod y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 600$$$)xa0β€” the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 100$$$)xa0β€” the length of the array and the number in the statement above. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$) xa0β€” the array itself. Output For each test case, print the maximum score you can get, one per line. Example Input 5 3 2 5 6 0 1 1 7 5 3 7 0 4 0 4 4 2 2 7 3 4 3 3 1000000000 1000000000 999999997 Output 11 7 15 10 2999999997 Note In the first test case, we can get a score of $$$11$$$ if we select $$$a_1, a_2$$$ without performing any operations. In the third test case, we can get a score of $$$15$$$ if we first swap $$$a_1$$$ with $$$a_4$$$ and then select $$$a_3, a_4, a_5$$$.
800
false
true
false
false
false
false
false
false
true
false
1,892
597C
Problem - 597C - Codeforces =============== xa0 β€” the length of sequence and the number of elements in increasing subsequences. Next _n_ lines contains one integer _a__i_ (1u2009≀u2009_a__i_u2009≀u2009_n_) each β€” elements of sequence. All values _a__i_ are different. Output Print one integer β€” the answer to the problem. Examples Input 5 2 1 2 3 5 4 Output 7
1,900
false
false
false
true
true
false
false
false
false
false
7,458
1684H
You are given a binary string $$$s$$$. You have to cut it into any number of non-intersecting substrings, so that the sum of binary integers denoted by these substrings is a power of 2. Each element of $$$s$$$ should be in exactly one substring. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). Description of the test cases follows. Each test case contains a binary string $$$s$$$ ($$$1 le s le 10^6$$$). It is guaranteed that the sum of $$$s$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case output the answer to the problem as follows: If the answer does not exist, output $$$-1$$$. If the answer exists, firstly output an integer $$$k$$$xa0β€” the number of substrings in the answer. After that output $$$k$$$ non-intersecting substrings, for $$$i$$$-th substring output two integers $$$l_i, r_i$$$ ($$$1 le l_i, r_i le s$$$)xa0β€” the description of $$$i$$$-th substring. If there are multiple valid solutions, you can output any of them. Example Input 4 00000 01101 0111011001011 000111100111110 Output -1 3 1 3 4 4 5 5 8 1 2 3 3 4 4 5 6 7 7 8 10 11 12 13 13 5 1 5 6 7 8 11 12 14 15 15 Note In the first test case it is impossible to cut the string into substrings, so that the sum is a power of 2. In the second test case such cut is valid: $$$011_2 = 3_{10}$$$, $$$0_2 = 0_{10}$$$, $$$1_2 = 1_{10}$$$. $$$3 + 0 + 1 = 4$$$, $$$4$$$ is a power of 2.
3,400
true
false
false
false
false
true
false
false
false
false
2,166
1007B
You are given a rectangular parallelepiped with sides of positive integer lengths $$$A$$$, $$$B$$$ and $$$C$$$. Find the number of different groups of three integers ($$$a$$$, $$$b$$$, $$$c$$$) such that $$$1leq aleq bleq c$$$ and parallelepiped $$$A imes B imes C$$$ can be paved with parallelepipeds $$$a imes b imes c$$$. Note, that all small parallelepipeds have to be rotated in the same direction. For example, parallelepiped $$$1 imes 5 imes 6$$$ can be divided into parallelepipeds $$$1 imes 3 imes 5$$$, but can not be divided into parallelepipeds $$$1 imes 2 imes 3$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$)xa0β€” the number of test cases. Each of the next $$$t$$$ lines contains three integers $$$A$$$, $$$B$$$ and $$$C$$$ ($$$1 leq A, B, C leq 10^5$$$)xa0β€” the sizes of the parallelepiped. Note In the first test case, rectangular parallelepiped $$$(1, 1, 1)$$$ can be only divided into rectangular parallelepiped with sizes $$$(1, 1, 1)$$$. In the second test case, rectangular parallelepiped $$$(1, 6, 1)$$$ can be divided into rectangular parallelepipeds with sizes $$$(1, 1, 1)$$$, $$$(1, 1, 2)$$$, $$$(1, 1, 3)$$$ and $$$(1, 1, 6)$$$. In the third test case, rectangular parallelepiped $$$(2, 2, 2)$$$ can be divided into rectangular parallelepipeds with sizes $$$(1, 1, 1)$$$, $$$(1, 1, 2)$$$, $$$(1, 2, 2)$$$ and $$$(2, 2, 2)$$$.
2,400
true
false
false
false
false
false
true
false
false
false
5,648
1486E
There are $$$n$$$ cities and $$$m$$$ bidirectional roads in the country. The roads in the country form an undirected weighted graph. The graph is not guaranteed to be connected. Each road has it's own parameter $$$w$$$. You can travel through the roads, but the government made a new law: you can only go through two roads at a time (go from city $$$a$$$ to city $$$b$$$ and then from city $$$b$$$ to city $$$c$$$) and you will have to pay $$$(w_{ab} + w_{bc})^2$$$ money to go through those roads. Find out whether it is possible to travel from city $$$1$$$ to every other city $$$t$$$ and what's the minimum amount of money you need to get from $$$1$$$ to $$$t$$$. Input First line contains two integers $$$n$$$, $$$m$$$ ($$$2 leq n leq 10^5$$$, $$$1 leq m leq min(frac{n cdot (n - 1)}{2}, 2 cdot 10^5)$$$). Next $$$m$$$ lines each contain three integers $$$v_i$$$, $$$u_i$$$, $$$w_i$$$ ($$$1 leq v_i, u_i leq n$$$, $$$1 leq w_i leq 50$$$, $$$u_i eq v_i$$$). It's guaranteed that there are no multiple edges, i.e. for any edge $$$(u_i, v_i)$$$ there are no other edges $$$(u_i, v_i)$$$ or $$$(v_i, u_i)$$$. Output For every city $$$t$$$ print one integer. If there is no correct path between $$$1$$$ and $$$t$$$ output $$$-1$$$. Otherwise print out the minimum amount of money needed to travel from $$$1$$$ to $$$t$$$. Examples Input 5 6 1 2 3 2 3 4 3 4 5 4 5 6 1 5 1 2 4 2 Note The graph in the first example looks like this. In the second example the path from $$$1$$$ to $$$3$$$ goes through $$$2$$$, so the resulting payment is $$$(1 + 2)^2 = 9$$$.
2,200
false
false
false
true
false
true
true
true
false
true
3,248
1061D
There are $$$n$$$ TV shows you want to watch. Suppose the whole time is split into equal parts called "minutes". The $$$i$$$-th of the shows is going from $$$l_i$$$-th to $$$r_i$$$-th minute, both ends inclusive. You need a TV to watch a TV show and you can't watch two TV shows which air at the same time on the same TV, so it is possible you will need multiple TVs in some minutes. For example, if segments $$$[l_i, r_i]$$$ and $$$[l_j, r_j]$$$ intersect, then shows $$$i$$$ and $$$j$$$ can't be watched simultaneously on one TV. Once you start watching a show on some TV it is not possible to "move" it to another TV (since it would be too distracting), or to watch another show on the same TV until this show ends. There is a TV Rental shop near you. It rents a TV for $$$x$$$ rupees, and charges $$$y$$$ ($$$y < x$$$) rupees for every extra minute you keep the TV. So in order to rent a TV for minutes $$$[a; b]$$$ you will need to pay $$$x + y cdot (b - a)$$$. You can assume, that taking and returning of the TV doesn't take any time and doesn't distract from watching other TV shows. Find the minimum possible cost to view all shows. Since this value could be too large, print it modulo $$$10^9 + 7$$$. Input The first line contains integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$1 le n le 10^5$$$, $$$1 le y < x le 10^9$$$)xa0β€” the number of TV shows, the cost to rent a TV for the first minute and the cost to rent a TV for every subsequent minute. Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le 10^9$$$) denoting the start and the end minute of the $$$i$$$-th TV show. Note In the first example, the optimal strategy would be to rent $$$3$$$ TVs to watch: Show $$$[1, 2]$$$ on the first TV, Show $$$[4, 10]$$$ on the second TV, Shows $$$[2, 4], [5, 9], [10, 11]$$$ on the third TV. This way the cost for the first TV is $$$4 + 3 cdot (2 - 1) = 7$$$, for the second is $$$4 + 3 cdot (10 - 4) = 22$$$ and for the third is $$$4 + 3 cdot (11 - 2) = 31$$$, which gives $$$60$$$ int total. In the second example, it is optimal watch each show on a new TV. In third example, it is optimal to watch both shows on a new TV. Note that the answer is to be printed modulo $$$10^9 + 7$$$.
2,000
false
true
true
false
true
false
false
false
true
false
5,405