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
526G
Spiders are Om Nom's old enemies. They love eating candies as much as he does and that's why they keep trying to keep the monster away from his favorite candies. They came up with an evil plan to trap Om Nom. Let's consider a rope structure consisting of _n_ nodes and _n_u2009-u20091 ropes connecting the nodes. The structure is connected, thus, the ropes and the nodes form a tree. Each rope of the formed structure is associated with its length. A candy is tied to node _x_ of the structure. Om Nom really wants to eat this candy. The _y_ spiders are trying to stop him from doing it. They decided to entangle the candy and some part of the structure into a web, thus attaching the candy to as large as possible part of the rope structure. Each spider can use his web to cover all ropes on the path between two arbitrary nodes _a_ and _b_. Thus, _y_ spiders can cover the set of ropes which is a union of _y_ paths in the given tree. These _y_ paths can arbitrarily intersect each other. The spiders want the following conditions to be hold: the node containing the candy is adjacent to at least one rope covered with a web the ropes covered with the web form a connected structure (what's the idea of covering with a web the ropes that are not connected with the candy?) the total length of the ropes covered with web is as large as possible The spiders haven't yet decided to what node of the structure they will tie the candy and how many spiders will cover the structure with web, so they asked you to help them. Help them calculate the optimal plan for multiple values of _x_ and _y_. Input The first line contains numbers _n_ and _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009105) — the number of nodes in the structure and the number of questions that the spiders want to ask you. The next _n_u2009-u20091 lines determine the rope structure. The _i_-th line contains three integers _u__i_,u2009_v__i_,u2009_l__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_, 1u2009≤u2009_l__i_u2009≤u20091000), showing that there is a rope of length _l__i_ between nodes _u__i_ and _v__i_. Next _q_ lines describe the spiders' questions. As they want you to answer their question online, they encoded their messages in a special manner. Each of the next _q_ lines contains two numbers _x__i_,u2009_y__i_. In the first question of the spiders _x_u2009=u2009_x_1,u2009_y_u2009=u2009_y_1. To calculate values _x_ and _y_ in the spiders' _i_-th (2u2009≤u2009_i_u2009≤u2009_q_) question, you need to use the following formulas: where _Ans__i_u2009-u20091 is the total length of the ropes covered by a web in the answer for the (_i_u2009-u20091)-th question. The following inequality holds: 1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_. Output For each question of the spiders print on a separate line a single integer _Ans__i_ — the total length of the ropes covered with web in the optimal plan. Examples Input 6 3 1 2 2 2 3 2 3 4 2 4 6 1 3 5 10 3 1 2 5 1 1
3,300
false
true
false
false
false
false
false
false
false
false
7,725
598A
Problem - 598A - Codeforces =============== xa0 ]( "Announcement") — the number of values of _n_ to be processed. Each of next _t_ lines contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009109). Output Print the requested sum for each of _t_ integers _n_ given in the input. Examples Input 2 4 1000000000 Output -4 499999998352516354 Note The answer for the first sample is explained in the statement.
900
true
false
false
false
false
false
false
false
false
false
7,457
1899D
Yarik is a big fan of many kinds of music. But Yarik loves not only listening to music but also writing it. He likes electronic music most of all, so he has created his own system of music notes, which, in his opinion, is best for it. Since Yarik also likes informatics, in his system notes are denoted by integers of $$$2^k$$$, where $$$k ge 1$$$xa0— a positive integer. But, as you know, you can't use just notes to write music, so Yarik uses combinations of two notes. The combination of two notes $$$(a, b)$$$, where $$$a = 2^k$$$ and $$$b = 2^l$$$, he denotes by the integer $$$a^b$$$. For example, if $$$a = 8 = 2^3$$$, $$$b = 4 = 2^2$$$, then the combination $$$(a, b)$$$ is denoted by the integer $$$a^b = 8^4 = 4096$$$. Note that different combinations can have the same notation, e.g., the combination $$$(64, 2)$$$ is also denoted by the integer $$$4096 = 64^2$$$. Yarik has already chosen $$$n$$$ notes that he wants to use in his new melody. However, since their integers can be very large, he has written them down as an array $$$a$$$ of length $$$n$$$, then the note $$$i$$$ is $$$b_i = 2^{a_i}$$$. The integers in array $$$a$$$ can be repeated. The melody will consist of several combinations of two notes. Yarik was wondering how many pairs of notes $$$b_i, b_j$$$ $$$(i < j)$$$ exist such that the combination $$$(b_i, b_j)$$$ is equal to the combination $$$(b_j, b_i)$$$. In other words, he wants to count the number of pairs $$$(i, j)$$$ $$$(i < j)$$$ such that $$$b_i^{b_j} = b_j^{b_i}$$$. Help him find the number of such pairs. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the length of the arrays. The next line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$.
1,300
true
false
false
false
false
false
false
false
false
false
919
643B
Bearland has _n_ cities, numbered 1 through _n_. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities. Bear Limak was once in a city _a_ and he wanted to go to a city _b_. There was no direct connection so he decided to take a long walk, visiting each city exactly once. Formally: There is no road between _a_ and _b_. There exists a sequence (path) of _n_ distinct cities _v_1,u2009_v_2,u2009...,u2009_v__n_ that _v_1u2009=u2009_a_, _v__n_u2009=u2009_b_ and there is a road between _v__i_ and _v__i_u2009+u20091 for . On the other day, the similar thing happened. Limak wanted to travel between a city _c_ and a city _d_. There is no road between them but there exists a sequence of _n_ distinct cities _u_1,u2009_u_2,u2009...,u2009_u__n_ that _u_1u2009=u2009_c_, _u__n_u2009=u2009_d_ and there is a road between _u__i_ and _u__i_u2009+u20091 for . Also, Limak thinks that there are at most _k_ roads in Bearland. He wonders whether he remembers everything correctly. Given _n_, _k_ and four distinct cities _a_, _b_, _c_, _d_, can you find possible paths (_v_1,u2009...,u2009_v__n_) and (_u_1,u2009...,u2009_u__n_) to satisfy all the given conditions? Find any solution or print -1 if it's impossible. Input The first line of the input contains two integers _n_ and _k_ (4u2009≤u2009_n_u2009≤u20091000, _n_u2009-u20091u2009≤u2009_k_u2009≤u20092_n_u2009-u20092)xa0— the number of cities and the maximum allowed number of roads, respectively. The second line contains four distinct integers _a_, _b_, _c_ and _d_ (1u2009≤u2009_a_,u2009_b_,u2009_c_,u2009_d_u2009≤u2009_n_). Output Print -1 if it's impossible to satisfy all the given conditions. Otherwise, print two lines with paths descriptions. The first of these two lines should contain _n_ distinct integers _v_1,u2009_v_2,u2009...,u2009_v__n_ where _v_1u2009=u2009_a_ and _v__n_u2009=u2009_b_. The second line should contain _n_ distinct integers _u_1,u2009_u_2,u2009...,u2009_u__n_ where _u_1u2009=u2009_c_ and _u__n_u2009=u2009_d_. Two paths generate at most 2_n_u2009-u20092 roads: (_v_1,u2009_v_2),u2009(_v_2,u2009_v_3),u2009...,u2009(_v__n_u2009-u20091,u2009_v__n_),u2009(_u_1,u2009_u_2),u2009(_u_2,u2009_u_3),u2009...,u2009(_u__n_u2009-u20091,u2009_u__n_). Your answer will be considered wrong if contains more than _k_ distinct roads or any other condition breaks. Note that (_x_,u2009_y_) and (_y_,u2009_x_) are the same road. Examples Output 2 7 1 3 6 5 4 7 1 5 4 6 2 3 Note In the first sample test, there should be 7 cities and at most 11 roads. The provided sample solution generates 10 roads, as in the drawing. You can also see a simple path of length _n_ between 2 and 4, and a path between 7 and 3.
1,600
false
false
false
false
false
true
false
false
false
true
7,243
24C
Problem - 24C - Codeforces =============== xa0 . Here point _B_ is symmetric to _A_ according _M_, if _M_ is the center of the line segment _AB_. Given index _j_ find the point _M__j_. Input On the first line you will be given an integer _n_ (1u2009≤u2009_n_u2009≤u2009105), which will be odd, and _j_ (1u2009≤u2009_j_u2009≤u20091018), where _j_ is the index of the desired point. The next line contains two space separated integers, the coordinates of _M_0. After that _n_ lines follow, where the _i_-th line contain the space separated integer coordinates of the point _A__i_u2009-u20091. The absolute values of all input coordinates will not be greater then 1000. Output On a single line output the coordinates of _M__j_, space separated. Examples Input 3 4 0 0 1 1 2 3 -5 3 Output 14 0 Input 3 1 5 5 1000 1000 -1000 1000 3 100 Output 1995 1995
1,800
true
false
true
false
false
false
false
false
false
false
9,876
16A
Problem - 16A - 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 ") , _n_ — the amount of rows, _m_ — the amount of columns on the flag of Berland. Then there follows the description of the flag: each of the following _n_ lines contain _m_ characters. Each character is a digit between 0 and 9, and stands for the colour of the corresponding square. Output Output YES, if the flag meets the new ISO standard, and NO otherwise. Examples Input 3 3 000 111 222 Output YES Input 3 3 000 000 111 Output NO Input 3 3 000 111 002 Output NO
800
false
false
true
false
false
false
false
false
false
false
9,915
1519A
You have $$$r$$$ red and $$$b$$$ blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: has at least one red bean (or the number of red beans $$$r_i ge 1$$$); has at least one blue bean (or the number of blue beans $$$b_i ge 1$$$); the number of red and blue beans should differ in no more than $$$d$$$ (or $$$r_i - b_i le d$$$) Can you distribute all beans? Input The first line contains the single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first and only line of each test case contains three integers $$$r$$$, $$$b$$$, and $$$d$$$ ($$$1 le r, b le 10^9$$$; $$$0 le d le 10^9$$$)xa0— the number of red and blue beans and the maximum absolute difference in each packet. Output For each test case, if you can distribute all beans, print YES. Otherwise, print NO. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). Example Input 4 1 1 0 2 7 3 6 1 4 5 4 0 Note In the first test case, you can form one packet with $$$1$$$ red and $$$1$$$ blue bean. The absolute difference $$$1 - 1 = 0 le d$$$. In the second test case, you can form two packets: $$$1$$$ red and $$$4$$$ blue beans in the first packet and $$$1$$$ red and $$$3$$$ blue beans in the second one. In the third test case, since $$$b = 1$$$, you can form only one packet with $$$6$$$ red and $$$1$$$ blue beans. The absolute difference $$$6 - 1 = 5 > d$$$. In the fourth test case, since $$$d = 0$$$ so each packet should contain the same number of red and blue beans, but $$$r eq b$$$.
800
true
false
false
false
false
false
false
false
false
false
3,069
305C
Problem - 305C - 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 *1600 No tag edit access → Contest materials ") ") need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2_v_u2009-u20091 for some integer _v_ (_v_u2009≥u20090). Help Ivan, find the required quantity of numbers. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second input line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u20092·109). It is guaranteed that _a_1u2009≤u2009_a_2u2009≤u2009...u2009≤u2009_a__n_. Output Print a single integer — the answer to the problem. Examples Input 4 0 1 1 1 Output 0 Input 1 3 Output 3 Note In the first sample you do not need to add anything, the sum of numbers already equals 23u2009-u20091u2009=u20097. In the second sample you need to add numbers 20,u200921,u200922.
1,600
false
true
true
false
false
false
false
false
false
false
8,614
1334F
Let's denote the following function $$$f$$$. This function takes an array $$$a$$$ of length $$$n$$$ and returns an array. Initially the result is an empty array. For each integer $$$i$$$ from $$$1$$$ to $$$n$$$ we add element $$$a_i$$$ to the end of the resulting array if it is greater than all previous elements (more formally, if $$$a_i > maxlimits_{1 le j < i}a_j$$$). Some examples of the function $$$f$$$: 1. if $$$a = [3, 1, 2, 7, 7, 3, 6, 7, 8]$$$ then $$$f(a) = [3, 7, 8]$$$; 2. if $$$a = [1]$$$ then $$$f(a) = [1]$$$; 3. if $$$a = [4, 1, 1, 2, 3]$$$ then $$$f(a) = [4]$$$; 4. if $$$a = [1, 3, 1, 2, 6, 8, 7, 7, 4, 11, 10]$$$ then $$$f(a) = [1, 3, 6, 8, 11]$$$. You are given two arrays: array $$$a_1, a_2, dots , a_n$$$ and array $$$b_1, b_2, dots , b_m$$$. You can delete some elements of array $$$a$$$ (possibly zero). To delete the element $$$a_i$$$, you have to pay $$$p_i$$$ coins (the value of $$$p_i$$$ can be negative, then you get $$$p_i$$$ coins, if you delete this element). Calculate the minimum number of coins (possibly negative) you have to spend for fulfilling equality $$$f(a) = b$$$. Input The first line contains one integer $$$n$$$ $$$(1 le n le 5 cdot 10^5)$$$ — the length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ $$$(1 le a_i le n)$$$ — the array $$$a$$$. The third line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ $$$(p_i le 10^9)$$$ — the array $$$p$$$. The fourth line contains one integer $$$m$$$ $$$(1 le m le n)$$$ — the length of array $$$b$$$. The fifth line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ $$$(1 le b_i le n, b_{i-1} < b_i)$$$ — the array $$$b$$$. Output If the answer exists, in the first line print YES. In the second line, print the minimum number of coins you have to spend for fulfilling equality $$$f(a) = b$$$. Otherwise in only line print NO. Examples Input 11 4 1 3 3 7 8 7 9 10 7 11 3 5 0 -2 5 3 6 7 8 2 4 3 3 7 10 Input 6 2 1 5 3 6 5 3 -9 0 16 22 -14 4 2 3 5 6
2,500
false
true
false
true
true
false
false
true
false
false
4,032
1725M
Pak Chanek is playing one of his favourite board games. In the game, there is a directed graph with $$$N$$$ vertices and $$$M$$$ edges. In the graph, edge $$$i$$$ connects two different vertices $$$U_i$$$ and $$$V_i$$$ with a length of $$$W_i$$$. By using the $$$i$$$-th edge, something can move from $$$U_i$$$ to $$$V_i$$$, but not from $$$V_i$$$ to $$$U_i$$$. To play this game, initially Pak Chanek must place both of his hands onto two different vertices. In one move, he can move one of his hands to another vertex using an edge. To move a hand from vertex $$$U_i$$$ to vertex $$$V_i$$$, Pak Chanek needs a time of $$$W_i$$$ seconds. Note that Pak Chanek can only move one hand at a time. This game ends when both of Pak Chanek's hands are on the same vertex. Pak Chanek has several questions. For each $$$p$$$ satisfying $$$2 leq p leq N$$$, you need to find the minimum time in seconds needed for Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex $$$1$$$ and vertex $$$p$$$, or report if it is impossible. Input The first line contains two integers $$$N$$$ and $$$M$$$ ($$$2 leq N leq 10^5$$$, $$$0 leq M leq 2 cdot 10^5$$$) — the number of vertices and edges in the graph. The $$$i$$$-th of the next $$$M$$$ lines contains three integers $$$U_i$$$, $$$V_i$$$, and $$$W_i$$$ ($$$1 le U_i, V_i le N$$$, $$$U_i eq V_i$$$, $$$1 le W_i le 10^9$$$) — a directed edge that connects two different vertices $$$U_i$$$ and $$$V_i$$$ with a length of $$$W_i$$$. There is no pair of different edges $$$i$$$ and $$$j$$$ such that $$$U_i = U_j$$$ and $$$V_i = V_j$$$. Output Output a line containing $$$N-1$$$ integers. The $$$j$$$-th integer represents the minimum time in seconds needed by Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex $$$1$$$ and vertex $$$j+1$$$, or $$$-1$$$ if it is impossible. Example Input 5 7 1 2 2 2 4 1 4 1 4 2 5 3 5 4 1 5 2 4 2 1 1 Note If initially Pak Chanek's left hand is on vertex $$$1$$$ and his right hand is on vertex $$$5$$$, Pak Chanek can do the following moves: 1. Move his right hand to vertex $$$4$$$ in $$$1$$$ second. 2. Move his left hand to vertex $$$2$$$ in $$$2$$$ seconds. 3. Move his left hand to vertex $$$4$$$ in $$$1$$$ second. In total it needs $$$1+2+1=4$$$ seconds. It can be proven that there is no other way that is faster.
1,800
false
false
false
true
false
false
false
false
false
true
1,934
510C
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical! She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order. Lexicographical order is defined in following way. When we compare _s_ and _t_, first we find the leftmost position with differing characters: _s__i_u2009≠u2009_t__i_. If there is no such position (i. e. _s_ is a prefix of _t_ or vice versa) the shortest string is less. Otherwise, we compare characters _s__i_ and _t__i_ according to their order in alphabet. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100): number of names. Each of the following _n_ lines contain one string _name__i_ (1u2009≤u2009_name__i_u2009≤u2009100), the _i_-th name. Each name contains only lowercase Latin letters. All names are different. Output If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word "Impossible" (without quotes). Examples Input 3 rivest shamir adleman Output bcdefghijklmnopqrsatuvwxyz Input 10 tourist petr wjmzbmr yeputons vepifanov scottwu oooooooooooooooo subscriber rowdark tankengineer Input 10 petr egor endagorion feferivan ilovetanyaromanova kostka dmitriyh maratsnowbear bredorjaguarturnik cgyforever Output aghjlnopefikdmbcqrstuvwxyz Input 7 car care careful carefully becarefuldontforgetsomething otherwiseyouwillbehacked goodluck Output acbdefhijklmnogpqrstuvwxyz
1,600
false
false
false
false
false
false
false
false
true
true
7,798
340C
Problem - 340C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics implementation math *1600 No tag edit access → Contest materials . Next line contains _n_ distinct integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009107). Output Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. Examples Input 3 2 3 5 Output 22 3 Note Consider 6 possible routes:
1,600
true
false
true
false
false
false
false
false
false
false
8,473
1829B
Problem - 1829B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials ") Editorial") xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the length of the array. The second line of each test case contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$0 leq a_i leq 1$$$)xa0— the elements of the array. Output For each test case, output a single integerxa0— the length of the longest blank space. Example Input 5 5 1 0 0 1 0 4 0 1 1 1 1 0 3 1 1 1 9 1 0 0 0 1 0 0 0 1 Output 2 1 1 0 3
800
false
false
true
false
false
false
false
false
false
false
1,313
95A
Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name _w_ and the collection of forbidden substrings _s_1,u2009_s_2,u2009...,u2009_s__n_. All those strings consist of uppercase and lowercase Latin letters. String _w_ has the length of _w_, its characters are numbered from 1 to _w_. First Petya should find all the occurrences of forbidden substrings in the _w_ string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "aBC" and "ABc" are considered equal. After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position _i_ should be replaced by any other one if for position _i_ in string _w_ there exist pair of indices _l_,u2009_r_ (1u2009≤u2009_l_u2009≤u2009_i_u2009≤u2009_r_u2009≤u2009_w_) such that substring _w_[_l_xa0...xa0_r_] is contained in the collection _s_1,u2009_s_2,u2009...,u2009_s__n_, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring. Letter _letter_ (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the _letter_ occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically. Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them. Input The first line contains the only integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of forbidden substrings in the collection. Next _n_ lines contain these substrings. The next line contains string _w_. All those _n_u2009+u20091 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter _letter_. Output Output the only line — Petya's resulting string with the maximum number of letters _letter_. If there are several answers then output the one that comes first lexicographically. The lexicographical comparison is performed by the standard < operator in modern programming languages. The line _a_ is lexicographically smaller than the line _b_, if _a_ is a prefix of _b_, or there exists such an _i_ (1u2009≤u2009_i_u2009≤u2009_a_), that _a__i_u2009<u2009_b__i_, and for any _j_ (1u2009≤u2009_j_u2009<u2009_i_) _a__j_u2009=u2009_b__j_. _a_ stands for the length of string _a_.
1,600
false
false
true
false
false
false
false
false
false
false
9,508
1301F
Jaber is a superhero in a large country that can be described as a grid with $$$n$$$ rows and $$$m$$$ columns, where every cell in that grid contains a different city. Jaber gave every city in that country a specific color between $$$1$$$ and $$$k$$$. In one second he can go from the current city to any of the cities adjacent by the side or to any city with the same color as the current city color. Jaber has to do $$$q$$$ missions. In every mission he will be in the city at row $$$r_1$$$ and column $$$c_1$$$, and he should help someone in the city at row $$$r_2$$$ and column $$$c_2$$$. Jaber wants your help to tell him the minimum possible time to go from the starting city to the finishing city for every mission. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 leq n, m leq 1000$$$, $$$1 leq k leq min(40 , n cdot m)$$$)xa0— the number of rows, columns and colors. Each of the next $$$n$$$ lines contains $$$m$$$ integers. In the $$$i$$$-th line, the $$$j$$$-th integer is $$$a_{ij}$$$ ($$$1 leq a_{ij} leq k$$$), which is the color assigned to the city in the $$$i$$$-th row and $$$j$$$-th column. The next line contains one integer $$$q$$$ ($$$1 leq q leq 10^{5}$$$) xa0— the number of missions. For the next $$$q$$$ lines, every line contains four integers $$$r_1$$$, $$$c_1$$$, $$$r_2$$$, $$$c_2$$$ ($$$1 leq r_1 , r_2 leq n$$$, $$$1 leq c_1 , c_2 leq m$$$) xa0— the coordinates of the starting and the finishing cities of the corresponding mission. It is guaranteed that for every color between $$$1$$$ and $$$k$$$ there is at least one city of that color. Output For every mission print the minimum possible time to reach city at the cell $$$(r_2, c_2)$$$ starting from city at the cell $$$(r_1, c_1)$$$.
2,600
false
false
true
false
false
false
false
false
false
true
4,189
1971C
There is a clock labeled with the numbers $$$1$$$ through $$$12$$$ in clockwise order, as shown below. In this example, $$$(a,b,c,d)=(2,9,10,6)$$$, and the strings intersect. Alice and Bob have four distinct integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ not more than $$$12$$$. Alice ties a red string connecting $$$a$$$ and $$$b$$$, and Bob ties a blue string connecting $$$c$$$ and $$$d$$$. Do the strings intersect? (The strings are straight line segments.) Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5940$$$)xa0— the number of test cases. The only line of each test case contains four distinct integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$1 leq a, b, c, d leq 12$$$). Output For each test case, output "YES" (without quotes) if the strings intersect, 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 15 2 9 10 6 3 8 9 1 1 2 3 4 5 3 4 12 1 8 2 10 3 12 11 8 9 10 12 1 12 1 10 2 3 12 6 9 1 9 8 4 6 7 9 12 7 12 9 6 10 12 11 1 3 9 6 12 1 4 3 5 Output YES NO NO YES YES NO NO NO NO NO NO YES YES YES YES Note The first test case is pictured in the statement. In the second test case, the strings do not intersect, as shown below.
900
false
false
true
false
false
false
false
false
false
false
463
1900A
Filip has a row of cells, some of which are blocked, and some are empty. He wants all empty cells to have water in them. He has two actions at his disposal: $$$1$$$xa0— place water in an empty cell. $$$2$$$xa0— remove water from a cell and place it in any other empty cell. If at some moment cell $$$i$$$ ($$$2 le i le n-1$$$) is empty and both cells $$$i-1$$$ and $$$i+1$$$ contains water, then it becomes filled with water. Find the minimum number of times he needs to perform action $$$1$$$ in order to fill all empty cells with water. Note that you don't need to minimize the use of action $$$2$$$. Note that blocked cells neither contain water nor can Filip place water in them. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the number of cells. The next line contains a string $$$s$$$ of length $$$n$$$. The $$$i$$$-th character of $$$s$$$ is '.' if the cell $$$i$$$ is empty and '#' if cell $$$i$$$ is blocked. Output For each test case, output a single numberxa0— the minimal amount of actions $$$1$$$ needed to fill all empty cells with water. Example Input 5 3 ... 7 ##....# 7 ..#.#.. 4 #### 10 #...#..#.# Note Test Case 1 In the first test case, Filip can put water in cells $$$1$$$ and $$$3$$$. As cell $$$2$$$ is between $$$2$$$ cells with water, it gets filled with water too. Test Case 2 In the second case, he can put water sources in cells $$$3$$$ and $$$5$$$. That results in cell $$$4$$$ getting filled with water. Then he will remove water from cell $$$5$$$ and place it into cell $$$6$$$. As cell $$$5$$$'s neighbors, cell $$$4$$$ and cell $$$6$$$, have water in them, cell $$$5$$$ also gets filled with water. You can see the illustration of this case below. Operations in the second test case. White cells are empty, grey ones are blocked, and blue ones are water. Test Case 3 In the third case, he can put water in all the empty cells. That requires $$$5$$$ actions of type $$$1$$$. Test Case 4 In the fourth case, there are no empty cells. Therefore, he does not have to put any water in them. Test Case 5 In the fifth test case, there exists a sequence of actions that requires only $$$2$$$ type $$$1$$$ actions.
800
false
true
true
false
false
true
false
false
false
false
915
1121C
Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab. There are $$$n$$$ solutions, the $$$i$$$-th of them should be tested on $$$a_i$$$ tests, testing one solution on one test takes $$$1$$$ second. The solutions are judged in the order from $$$1$$$ to $$$n$$$. There are $$$k$$$ testing processes which test solutions simultaneously. Each of them can test at most one solution at a time. At any time moment $$$t$$$ when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id $$$i$$$, then it is being tested on the first test from time moment $$$t$$$ till time moment $$$t + 1$$$, then on the second test till time moment $$$t + 2$$$ and so on. This solution is fully tested at time moment $$$t + a_i$$$, and after that the testing process immediately starts testing another solution. Consider some time moment, let there be exactly $$$m$$$ fully tested solutions by this moment. There is a caption "System testing: $$$d$$$%" on the page with solutions, where $$$d$$$ is calculated as $$$$$$d = roundleft(100cdotfrac{m}{n} ight),$$$$$$ where $$$round(x) = lfloor{x + 0.5} floor$$$ is a function which maps every real to the nearest integer. Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test $$$q$$$, and the caption says "System testing: $$$q$$$%". Find the number of interesting solutions. Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter. Input The first line contains two positive integers $$$n$$$ and $$$k$$$ ($$$1 le n le 1000$$$, $$$1 le k le 100$$$) standing for the number of submissions and the number of testing processes respectively. The second line contains $$$n$$$ positive integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 150$$$), where $$$a_i$$$ is equal to the number of tests the $$$i$$$-th submission is to be run on. Note Consider the first example. At time moment $$$0$$$ both solutions start testing. At time moment $$$49$$$ the first solution is fully tested, so at time moment $$$49.5$$$ the second solution is being tested on the test $$$50$$$, and the caption says "System testing: $$$50$$$%" (because there is one fully tested solution out of two). So, the second solution is interesting. Consider the second example. At time moment $$$0$$$ the first and the second solutions start testing. At time moment $$$32$$$ the first solution is fully tested, the third solution starts testing, the caption says "System testing: $$$25$$$%". At time moment $$$32 + 24.5 = 56.5$$$ the third solutions is being tested on test $$$25$$$, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment $$$32 + 33 = 65$$$, the fourth solution is fully tested at time moment $$$65 + 1 = 66$$$. The captions becomes "System testing: $$$75$$$%", and at time moment $$$74.5$$$ the second solution is being tested on test $$$75$$$. So, this solution is also interesting. Overall, there are two interesting solutions.
1,600
false
false
true
false
false
false
false
false
false
false
5,078
1611C
Polycarp wrote on a whiteboard an array $$$p$$$ of length $$$n$$$, which is a permutation of numbers from $$$1$$$ to $$$n$$$. In other words, in $$$p$$$ each number from $$$1$$$ to $$$n$$$ occurs exactly once. He also prepared a resulting array $$$a$$$, which is initially empty (that is, it has a length of $$$0$$$). After that, he did exactly $$$n$$$ steps. Each step looked like this: Look at the leftmost and rightmost elements of $$$p$$$, and pick the smaller of the two. If you picked the leftmost element of $$$p$$$, append it to the left of $$$a$$$; otherwise, if you picked the rightmost element of $$$p$$$, append it to the right of $$$a$$$. The picked element is erased from $$$p$$$. Note that on the last step, $$$p$$$ has a length of $$$1$$$ and its minimum element is both leftmost and rightmost. In this case, Polycarp can choose what role the minimum element plays. In other words, this element can be added to $$$a$$$ both on the left and on the right (at the discretion of Polycarp). Let's look at an example. Let $$$n=4$$$, $$$p=[3, 1, 4, 2]$$$. Initially $$$a=[]$$$. Then: During the first step, the minimum is on the right (with a value of $$$2$$$), so after this step, $$$p=[3,1,4]$$$ and $$$a=[2]$$$ (he added the value $$$2$$$ to the right). During the second step, the minimum is on the left (with a value of $$$3$$$), so after this step, $$$p=[1,4]$$$ and $$$a=[3,2]$$$ (he added the value $$$3$$$ to the left). During the third step, the minimum is on the left (with a value of $$$1$$$), so after this step, $$$p=[4]$$$ and $$$a=[1,3,2]$$$ (he added the value $$$1$$$ to the left). During the fourth step, the minimum is both left and right (this value is $$$4$$$). Let's say Polycarp chose the right option. After this step, $$$p=[]$$$ and $$$a=[1,3,2,4]$$$ (he added the value $$$4$$$ to the right). Thus, a possible value of $$$a$$$ after $$$n$$$ steps could be $$$a=[1,3,2,4]$$$. You are given the final value of the resulting array $$$a$$$. Find any possible initial value for $$$p$$$ that can result the given $$$a$$$, or determine that there is no solution. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the test. Each test case consists of two lines. The first of them contains an integer $$$n$$$ ($$$1 le n le 2cdot10^5$$$) — 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$$$) — the elements of the array $$$a$$$. All elements of the $$$a$$$ array are distinct numbers. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2cdot10^5$$$. Output Print $$$t$$$ lines, each of the lines must contain the answer to the corresponding set of input data: numbers $$$p_1, p_2, dots, p_n$$$ xa0— any of the possible initial values of the array $$$p$$$, which will lead to the given array $$$a$$$. All elements of $$$p$$$ are distinct integers from $$$1$$$ to $$$n$$$. Thus, if there are several solutions, print any. If there is no solution, then print -1 on the line. Example Input 4 4 1 3 2 4 1 1 5 1 3 5 4 2 3 3 2 1 Note The first test case in the example is clarified in the main section of the problem statement. There may be other correct answers for this test set. In the second test case, $$$n=1$$$. Thus, there is only one permutation that can be the answer: $$$p=[1]$$$. Indeed, this is the answer to this test case. In the third test case of the example, no matter what permutation you take as $$$p$$$, after applying the $$$n$$$ steps, the result will differ from $$$a=[1, 3, 5, 4, 2]$$$.
1,000
false
false
false
false
false
true
false
false
false
false
2,589
1475A
Problem - 1475A - 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 *900 No tag edit access → Contest materials ") Editorial") that $$$n$$$ is divisible by $$$x$$$ and $$$x$$$ is odd). For example, if $$$n=6$$$, then there is $$$x=3$$$. If $$$n=4$$$, then such a number does not exist. 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. Each test case contains one integer $$$n$$$ ($$$2 le n le 10^{14}$$$). Please note, that the input for some test cases won't fit into $$$32$$$-bit integer type, so you should use at least $$$64$$$-bit integer type in your programming language. Output For each test case, output on a separate line: "YES" if $$$n$$$ has an odd divisor, greater than one; "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 6 2 3 4 5 998244353 1099511627776 Output NO YES NO YES YES NO
900
true
false
false
false
false
false
false
false
false
false
3,304
447B
Problem - 447B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation *1000 No tag edit access → Contest materials ") ") Editorial") he represents its value with a function _f_(_s_), where Now DZY has a string _s_. He wants to insert _k_ lowercase letters into this string in order to get the largest possible value of the resulting string. Can you help him calculate the largest possible value he could get? Input The first line contains a single string _s_xa0(1u2009≤u2009_s_u2009≤u2009103). The second line contains a single integer _k_xa0(0u2009≤u2009_k_u2009≤u2009103). The third line contains twenty-six integers from _w__a_ to _w__z_. Each such number is non-negative and doesn't exceed 1000. Output Print a single integer — the largest possible value of the resulting string DZY could get. Examples Input abc 3 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 41 Note In the test sample DZY can obtain "abcbbc", _value_u2009=u20091·1u2009+u20092·2u2009+u20093·2u2009+u20094·2u2009+u20095·2u2009+u20096·2u2009=u200941.
1,000
false
true
true
false
false
false
false
false
false
false
8,050
1148D
You are given $$$n$$$ pairs of integers $$$(a_1, b_1), (a_2, b_2), ldots, (a_n, b_n)$$$. All of the integers in the pairs are distinct and are in the range from $$$1$$$ to $$$2 cdot n$$$ inclusive. Let's call a sequence of integers $$$x_1, x_2, ldots, x_{2k}$$$ good if either $$$x_1 < x_2 > x_3 < ldots < x_{2k-2} > x_{2k-1} < x_{2k}$$$, or $$$x_1 > x_2 < x_3 > ldots > x_{2k-2} < x_{2k-1} > x_{2k}$$$. You need to choose a subset of distinct indices $$$i_1, i_2, ldots, i_t$$$ and their order in a way that if you write down all numbers from the pairs in a single sequence (the sequence would be $$$a_{i_1}, b_{i_1}, a_{i_2}, b_{i_2}, ldots, a_{i_t}, b_{i_t}$$$), this sequence is good. What is the largest subset of indices you can choose? You also need to construct the corresponding index sequence $$$i_1, i_2, ldots, i_t$$$. Input The first line contains single integer $$$n$$$ ($$$2 leq n leq 3 cdot 10^5$$$)xa0— the number of pairs. Each of the next $$$n$$$ lines contain two numbersxa0— $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le 2 cdot n$$$)xa0— the elements of the pairs. It is guaranteed that all integers in the pairs are distinct, that is, every integer from $$$1$$$ to $$$2 cdot n$$$ is mentioned exactly once. Output In the first line print a single integer $$$t$$$xa0— the number of pairs in the answer. Then print $$$t$$$ distinct integers $$$i_1, i_2, ldots, i_t$$$xa0— the indexes of pairs in the corresponding order.
1,800
false
true
false
false
false
false
false
false
true
false
4,974
1816B
Consider a $$$2 imes n$$$ grid, where $$$n$$$ is an even integer. You may place the integers $$$1, 2, ldots, 2n$$$ on the grid, using each integer exactly once. A path is a sequence of cells achieved by starting at $$$(1, 1)$$$, then repeatedly walking either downwards or to the right, and stopping when $$$(2, n)$$$ is reached. The path should not extend beyond the grid. The cost of a path is the alternating sum of the numbers written on the cells in a path. That is, let the numbers written on the cells be $$$a_1, a_2, ldots, a_k$$$ (in the order that it is visited), the cost of the path is $$$a_1 - a_2 + a_3 - a_4 + ldots = sum_{i=1}^k a_i cdot (-1)^{i+1}$$$. Construct a way to place the integers $$$1, 2, ldots, 2n$$$ on the grid, such that the minimum cost over all paths from $$$(1, 1)$$$ to $$$(2, n)$$$ is maximized. If there are multiple such grids that result in the maximum value, output any of them. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) — the number of test cases. The description of test cases follows. The first and the only line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 10^5$$$, $$$n$$$ is even) — the number of the columns in the grid. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output $$$2$$$ lines, each containing $$$n$$$ integers — the desired grid. If there are multiple solutions, output any of them. Note In the first test case, there are only two paths from cell $$$(1, 1)$$$ to cell $$$(2, 2)$$$. Their costs are $$$3-1+4=6$$$ and $$$3-2+4=5$$$. Then the minimum cost is $$$5$$$, which is the maximum possible value. In the second test case, there are four paths from cell $$$(1, 1)$$$ to cell $$$(2, 4)$$$. Their costs are $$$8-1+5-3+7=16$$$, $$$8-2+5-3+7=15$$$, $$$8-2+6-3+7=16$$$, and $$$8-2+6-4+7=15$$$. Then the minimum value is $$$15$$$, which is the maximum possible value.
1,000
false
true
false
false
false
true
false
false
false
false
1,374
596D
Wilbur the pig really wants to be a beaver, so he decided today to pretend he is a beaver and bite at trees to cut them down. There are _n_ trees located at various positions on a line. Tree _i_ is located at position _x__i_. All the given positions of the trees are distinct. The trees are equal, i.e. each tree has height _h_. Due to the wind, when a tree is cut down, it either falls left with probability _p_, or falls right with probability 1u2009-u2009_p_. If a tree hits another tree while falling, that tree will fall in the same direction as the tree that hit it. A tree can hit another tree only if the distance between them is strictly less than _h_. For example, imagine there are 4 trees located at positions 1, 3, 5 and 8, while _h_u2009=u20093 and the tree at position 1 falls right. It hits the tree at position 3 and it starts to fall too. In it's turn it hits the tree at position 5 and it also starts to fall. The distance between 8 and 5 is exactly 3, so the tree at position 8 will not fall. As long as there are still trees standing, Wilbur will select either the leftmost standing tree with probability 0.5 or the rightmost standing tree with probability 0.5. Selected tree is then cut down. If there is only one tree remaining, Wilbur always selects it. As the ground is covered with grass, Wilbur wants to know the expected total length of the ground covered with fallen trees after he cuts them all down because he is concerned about his grass-eating cow friends. Please help Wilbur. Input The first line of the input contains two integers, _n_ (1u2009≤u2009_n_u2009≤u20092000) and _h_ (1u2009≤u2009_h_u2009≤u2009108) and a real number _p_ (0u2009≤u2009_p_u2009≤u20091), given with no more than six decimal places. The second line of the input contains _n_ integers, _x_1,u2009_x_2,u2009...,u2009_x__n_ (u2009-u2009108u2009≤u2009_x__i_u2009≤u2009108) in no particular order. Output Print a single real numberxa0— the expected total length of the ground covered by trees when they have all fallen down. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Note Consider the first example, we have 2 trees with height 2. There are 3 scenarios: 1. Both trees falls left. This can either happen with the right tree falling left first, which has probability (also knocking down the left tree), or the left tree can fall left and then the right tree can fall left, which has probability. Total probability is . 2. Both trees fall right. This is analogous to (1), so the probability of this happening is . 3. The left tree fall left and the right tree falls right. This is the only remaining scenario so it must have probability. Cases 1 and 2 lead to a total of 3 units of ground covered, while case 3 leads to a total of 4 units of ground covered. Thus, the expected value is .
2,300
true
false
false
true
false
false
false
false
true
false
7,462
1401E
There is a square of size $$$10^6 imes 10^6$$$ on the coordinate plane with four points $$$(0, 0)$$$, $$$(0, 10^6)$$$, $$$(10^6, 0)$$$, and $$$(10^6, 10^6)$$$ as its vertices. You are going to draw segments on the plane. All segments are either horizontal or vertical and intersect with at least one side of the square. Now you are wondering how many pieces this square divides into after drawing all segments. Write a program calculating the number of pieces of the square. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$0 le n, m le 10^5$$$)xa0— the number of horizontal segments and the number of vertical segments. The next $$$n$$$ lines contain descriptions of the horizontal segments. The $$$i$$$-th line contains three integers $$$y_i$$$, $$$lx_i$$$ and $$$rx_i$$$ ($$$0 < y_i < 10^6$$$; $$$0 le lx_i < rx_i le 10^6$$$), which means the segment connects $$$(lx_i, y_i)$$$ and $$$(rx_i, y_i)$$$. The next $$$m$$$ lines contain descriptions of the vertical segments. The $$$i$$$-th line contains three integers $$$x_i$$$, $$$ly_i$$$ and $$$ry_i$$$ ($$$0 < x_i < 10^6$$$; $$$0 le ly_i < ry_i le 10^6$$$), which means the segment connects $$$(x_i, ly_i)$$$ and $$$(x_i, ry_i)$$$. It's guaranteed that there are no two segments on the same line, and each segment intersects with at least one of square's sides. Output Print the number of pieces the square is divided into after drawing all the segments. Example Input 3 3 2 3 1000000 4 0 4 3 0 1000000 4 0 1 2 0 5 3 1 1000000 Note The sample is like this:
2,400
false
false
true
false
true
false
false
false
true
false
3,661
90A
A group of university students wants to get to the top of a mountain to have a picnic there. For that they decided to use a cableway. A cableway is represented by some cablecars, hanged onto some cable stations by a cable. A cable is scrolled cyclically between the first and the last cable stations (the first of them is located at the bottom of the mountain and the last one is located at the top). As the cable moves, the cablecar attached to it move as well. The number of cablecars is divisible by three and they are painted three colors: red, green and blue, in such manner that after each red cablecar goes a green one, after each green cablecar goes a blue one and after each blue cablecar goes a red one. Each cablecar can transport no more than two people, the cablecars arrive with the periodicity of one minute (i. e. every minute) and it takes exactly 30 minutes for a cablecar to get to the top. All students are divided into three groups: _r_ of them like to ascend only in the red cablecars, _g_ of them prefer only the green ones and _b_ of them prefer only the blue ones. A student never gets on a cablecar painted a color that he doesn't like, The first cablecar to arrive (at the moment of time 0) is painted red. Determine the least time it will take all students to ascend to the mountain top. Input The first line contains three integers _r_, _g_ and _b_ (0u2009≤u2009_r_,u2009_g_,u2009_b_u2009≤u2009100). It is guaranteed that _r_u2009+u2009_g_u2009+u2009_b_u2009>u20090, it means that the group consists of at least one student. Output Print a single number — the minimal time the students need for the whole group to ascend to the top of the mountain. Note Let's analyze the first sample. At the moment of time 0 a red cablecar comes and one student from the _r_ group get on it and ascends to the top at the moment of time 30. At the moment of time 1 a green cablecar arrives and two students from the _g_ group get on it; they get to the top at the moment of time 31. At the moment of time 2 comes the blue cablecar and two students from the _b_ group get on it. They ascend to the top at the moment of time 32. At the moment of time 3 a red cablecar arrives but the only student who is left doesn't like red and the cablecar leaves empty. At the moment of time 4 a green cablecar arrives and one student from the _g_ group gets on it. He ascends to top at the moment of time 34. Thus, all the students are on the top, overall the ascension took exactly 34 minutes.
1,000
true
true
false
false
false
false
false
false
false
false
9,524
825A
Problem - 825A - Codeforces =============== xa0 . Digits are written one by one in order corresponding to number and separated by single '0' character. Though Polycarp learnt how to encode the numbers, he has no idea how to decode them back. Help him calculate the decoded number. Input The first line contains one integer number _n_ (1u2009≤u2009_n_u2009≤u200989) — length of the string _s_. The second line contains string _s_ — sequence of '0' and '1' characters, number in its encoded format. It is guaranteed that the number corresponding to the string is positive and doesn't exceed 109. The string always starts with '1'. Output Print the decoded number. Examples Input 3 111 Output 3 Input 9 110011101 Output 2031
1,100
false
false
true
false
false
false
false
false
false
false
6,462
797A
Problem - 797A - Codeforces =============== xa0 such that all these integers are strictly greater than 1, and their product is equal to _n_. Input The first line contains two integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u2009100000, 1u2009≤u2009_k_u2009≤u200920). Output If it's impossible to find the representation of _n_ as a product of _k_ numbers, print -1. Otherwise, print _k_ integers in any order. Their product must be equal to _n_. If there are multiple answers, print any of them. Examples Input 100000 2 Output 2 50000 Input 100000 20 Output -1 Input 1024 5 Output 2 64 2 2 2
1,100
true
false
true
false
false
false
false
false
false
false
6,587
1910E
Problem - 1910E - Codeforces =============== xa0 ]( --- Finished → Languages The following languages are only available languages for the problems from the contest Kotlin Heroes: Episode 9 (Unrated, T-Shirts + Prizes!): Kotlin 1.7.20 Kotlin 1.9.21 → 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 *special problem dp *2100 No tag edit access → Contest materials : swap $$$a_i$$$ and $$$b_i$$$. Let $$$f(c)$$$ be the maximum sum of a contiguous subarray of the array $$$c$$$ (including the empty subsegment, which sum is $$$0$$$). Your task is to calculate the maximum possible value of $$$f(a) + f(b)$$$, using the aforementioned operation any number of times. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$-10^9 le b_i le 10^9$$$). The sum of $$$n$$$ over all test case doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0— the maximum possible value of $$$f(a) + f(b)$$$, using the aforementioned operation any number of times. Example Input 3 3 2 -1 3 -4 0 1 6 4 2 -6 1 6 -4 -6 -2 -3 7 -3 2 2 -2 -5 0 -1 Output 6 21 0
2,100
false
false
false
true
false
false
false
false
false
false
843
45J
Problem - 45J - 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 *1800 No tag edit access → Contest materials ") — the number of rows and columns on Vasya's field Output If there's no solution, print -1. Otherwise, print _n_ lines containing _m_ numbers each — the trees' planting plan. In every square of the plan the height of a tree that should be planted on this square should be written. If there are several solutions to that problem, print any of them. Examples Input 2 3 Output 3 6 2 5 1 4 Input 2 1 Output -1
1,800
false
false
false
false
false
true
false
false
false
false
9,750
1669F
There are $$$n$$$ candies put from left to right on a table. The candies are numbered from left to right. The $$$i$$$-th candy has weight $$$w_i$$$. Alice and Bob eat candies. Alice can eat any number of candies from the left (she can't skip candies, she eats them in a row). Bob can eat any number of candies from the right (he can't skip candies, he eats them in a row). Of course, if Alice ate a candy, Bob can't eat it (and vice versa). They want to be fair. Their goal is to eat the same total weight of candies. What is the most number of candies they can eat in total? Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2cdot10^5$$$)xa0— the number of candies on the table. The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, dots, w_n$$$ ($$$1 leq w_i leq 10^4$$$)xa0— the weights of candies from left to right. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, print a single integerxa0— the maximum number of candies Alice and Bob can eat in total while satisfying the condition. Example Input 4 3 10 20 10 6 2 1 4 2 4 1 5 1 2 4 8 16 9 7 3 20 5 15 1 11 8 10 Note For the first test case, Alice will eat one candy from the left and Bob will eat one candy from the right. There is no better way for them to eat the same total amount of weight. The answer is $$$2$$$ because they eat two candies in total. For the second test case, Alice will eat the first three candies from the left (with total weight $$$7$$$) and Bob will eat the first three candies from the right (with total weight $$$7$$$). They cannot eat more candies since all the candies have been eaten, so the answer is $$$6$$$ (because they eat six candies in total). For the third test case, there is no way Alice and Bob will eat the same non-zero weight so the answer is $$$0$$$. For the fourth test case, Alice will eat candies with weights $$$[7, 3, 20]$$$ and Bob will eat candies with weights $$$[10, 8, 11, 1]$$$, they each eat $$$30$$$ weight. There is no better partition so the answer is $$$7$$$.
1,100
false
true
false
false
true
false
false
true
false
false
2,260
884C
The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are _n_ stations in the subway. It was built according to the Bertown Transport Law: 1. For each station _i_ there exists exactly one train that goes from this station. Its destination station is _p__i_, possibly _p__i_u2009=u2009_i_; 2. For each station _i_ there exists exactly one station _j_ such that _p__j_u2009=u2009_i_. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (_x_,u2009_y_) such that person can start at station _x_ and, after taking some subway trains (possibly zero), arrive at station _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of _p__i_ for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number _n_ (1u2009≤u2009_n_u2009≤u2009100000) — the number of stations. The second line contains _n_ integer numbers _p_1, _p_2, ..., _p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Note In the first example the mayor can change _p_2 to 3 and _p_3 to 1, so there will be 9 pairs: (1,u20091), (1,u20092), (1,u20093), (2,u20091), (2,u20092), (2,u20093), (3,u20091), (3,u20092), (3,u20093). In the second example the mayor can change _p_2 to 4 and _p_3 to 5.
1,500
true
true
false
false
false
false
false
false
false
false
6,212
103B
...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu... Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super weapon. Due to high seismic activity and poor weather conditions the satellites haven't yet been able to make clear shots of the monster. The analysis of the first shot resulted in an undirected graph with _n_ vertices and _m_ edges. Now the world's best minds are about to determine whether this graph can be regarded as Cthulhu or not. To add simplicity, let's suppose that Cthulhu looks from the space like some spherical body with tentacles attached to it. Formally, we shall regard as Cthulhu such an undirected graph that can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle. It is guaranteed that the graph contains no multiple edges and self-loops. Input The first line contains two integers — the number of vertices _n_ and the number of edges _m_ of the graph (1u2009≤u2009_n_u2009≤u2009100, 0u2009≤u2009_m_u2009≤u2009). Each of the following _m_ lines contains a pair of integers _x_ and _y_, that show that an edge exists between vertices _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_,u2009_x_u2009≠u2009_y_). For each pair of vertices there will be at most one edge between them, no edge connects a vertex to itself. Output Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is. Examples Input 6 6 6 3 6 4 5 1 2 5 1 4 5 4 Note Let us denote as a simple cycle a set of _v_ vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., _v_u2009-u20091 and _v_, _v_ and 1. A tree is a connected undirected graph consisting of _n_ vertices and _n_u2009-u20091 edges (_n_u2009>u20090). A rooted tree is a tree where one vertex is selected to be the root.
1,500
false
false
false
false
false
false
false
false
false
true
9,471
1168D
Toad Ilya has a rooted binary tree with vertex $$$1$$$ being the root. A tree is a connected graph without cycles. A tree is rooted if one vertex is selected and called the root. A vertex $$$u$$$ is a child of a vertex $$$v$$$ if $$$u$$$ and $$$v$$$ are connected by an edge and $$$v$$$ is closer to the root than $$$u$$$. A leaf is a non-root vertex that has no children. In the tree Ilya has each vertex has at most two children, and each edge has some character written on it. The character can be a lowercase English letter or the question mark '?'. Ilya will $$$q$$$ times update the tree a bit. Each update will replace exactly one character on some edge. After each update Ilya needs to find if the tree is anagrammable and if yes, find its anagramnity for each letter. Well, that's difficult to explain, but we'll try. To start with, a string $$$a$$$ is an anagram of a string $$$b$$$ if it is possible to rearrange letters in $$$a$$$ (without changing the letters itself) so that it becomes $$$b$$$. For example, the string "fortyfive" is an anagram of the string "overfifty", but the string "aabb" is not an anagram of the string "bbba". Consider a path from the root of the tree to a leaf. The characters on the edges on this path form a string, we say that this string is associated with this leaf. The tree is anagrammable if and only if it is possible to replace each question mark with a lowercase English letter so that for all pair of leaves the associated strings for these leaves are anagrams of each other. If the tree is anagrammable, then its anagramnity for the letter $$$c$$$ is the maximum possible number of letters $$$c$$$ in a string associated with some leaf in a valid replacement of all question marks. Please after each update find if the tree is anagrammable and if yes, find the $$$sum{f(c) cdot ind(c)}$$$ for all letters $$$c$$$, where $$$f(c)$$$ is the anagramnity for the letter $$$c$$$, and $$$ind(x)$$$ is the index of this letter in the alphabet ($$$ind($$$"a"$$$) = 1$$$, $$$ind($$$"b"$$$) = 2$$$, ..., $$$ind($$$"z"$$$) = 26$$$). Input The first line of input contains two integers $$$n$$$ and $$$q$$$ ($$$2 leq n leq 150,000$$$, $$$1 leq q leq 150,000$$$)xa0— the number of vertices in the tree and the number of queries. The next $$$n-1$$$ lines describe the initial tree. The $$$i$$$-th of them contains an integer $$$p_i$$$ and a character $$$c_i$$$ ($$$1 leq p_i leq i$$$, $$$c_i$$$ is a lowercase English letter or the question mark '?') describing an edge between vertices $$$p_i$$$ and $$$i+1$$$ with character $$$c_i$$$ written on it. The root of this tree is the vertex $$$1$$$, and each vertex has at most two children. The next $$$q$$$ lines describe the queries. The $$$i$$$-th of them contains two integers $$$v$$$ and $$$c$$$ ($$$2 leq v leq n$$$, $$$c$$$ is a lowercase English letter or the question mark '?'), meaning that updated character on the edge between $$$p_{v-1}$$$ to $$$v$$$ is $$$c$$$. The updated character can be the same as was written before. Output Output $$$q$$$ lines. In the $$$i$$$-th of them print "Fou" if the tree is not anagrammable after the first $$$i$$$ updates. Otherwise output "Shi" and the $$$sum{f(c) cdot ind(c)}$$$ for all letters $$$c$$$. Examples Input 3 4 1 ? 1 ? 2 ? 2 a 3 b 2 b Output Shi 351 Shi 1 Fou Shi 2 Input 5 2 1 ? 1 ? 2 ? 3 ? 4 a 5 b Note In the first example after the first query, for each character, you can set all edges equal to that character, and you will get $$$1$$$ such character on each path, so the answer is $$$1 cdot (1+2+ldots+26) = 351$$$. In the first example after the second query, you know that all paths should be an anagram of "a", so all paths should be "a", so the answer is $$$1 cdot 1 = 1$$$. In the first example after the third query, you have two paths with strings "a" and "b", but these strings are not anagrams, so the answer is "Fou". In the first example after the fourth query, you know that all paths should be "b", so the answer is $$$1 cdot 2 = 2$$$. In the second example after the first query, you know that $$$f($$$'a'$$$) = 2$$$ and $$$f(c) = 1$$$ for all other characters, so the answer is $$$1 cdot (2 + 3 + ldots + 26) + 2 = 352$$$. In the second example after the second query, you know that each path should contain one 'a' and one 'b', so the answer is $$$1 cdot 1 + 1 cdot 2 = 3$$$.
3,000
false
false
true
true
false
false
false
false
false
false
4,873
871E
Problem - 871E - 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 graphs greedy trees *2900 No tag edit access → Contest materials ") ") ) — the number of vertices in the tree and the number of vertices about which Petya remembers distance information. The following _k_ lines contain remembered information. The _i_-th line contains _n_ integers _d__i_,u20091,u2009_d__i_,u20092,u2009...,u2009_d__i_,u2009_n_ (0u2009≤u2009_d__i_,u2009_j_u2009≤u2009_n_u2009-u20091), where _d__i_,u2009_j_ — the distance to _j_-th vertex from the _i_-th vertex that Petya remembers. Output If there are no suitable trees, print -1. In the other case, print _n_u2009-u20091 lines: each line should contain two vertices connected by edge in the required tree. You can print edges and vertices in an edge in any order. The tree vertices are enumerated from 1 to _n_. If there are many solutions print any of them. Examples Input 5 2 0 1 2 3 2 2 1 0 1 2 Output 2 1 3 2 4 3 5 2 Input 3 1 1 2 1 Output -1 Note Picture for the first sample:
2,900
false
true
false
false
false
false
false
false
false
true
6,255
525E
Anya loves to fold and stick. Today she decided to do just that. Anya has _n_ cubes lying in a line and numbered from 1 to _n_ from left to right, with natural numbers written on them. She also has _k_ stickers with exclamation marks. We know that the number of stickers does not exceed the number of cubes. Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120. You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most _k_ exclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to _S_. Anya can stick at most one exclamation mark on each cube. Can you do it? Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks. Input The first line of the input contains three space-separated integers _n_, _k_ and _S_ (1u2009≤u2009_n_u2009≤u200925, 0u2009≤u2009_k_u2009≤u2009_n_, 1u2009≤u2009_S_u2009≤u20091016)xa0—xa0the number of cubes and the number of stickers that Anya has, and the sum that she needs to get. The second line contains _n_ positive integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0—xa0the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one. Multiple cubes can contain the same numbers. Output Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number _S_. Note In the first sample the only way is to choose both cubes and stick an exclamation mark on each of them. In the second sample the only way is to choose both cubes but don't stick an exclamation mark on any of them. In the third sample it is possible to choose any of the cubes in three ways, and also we may choose to stick or not to stick the exclamation mark on it. So, the total number of ways is six.
2,100
true
false
false
true
false
false
true
true
false
false
7,732
1461A
One fall day Joe got bored because he couldn't find himself something interesting to do. Marty suggested Joe to generate a string of length $$$n$$$ to entertain him somehow. It didn't seem particularly difficult, but Joe's generated string had to follow these rules: the string may only contain characters 'a', 'b', or 'c'; the maximum length of a substring of this string that is a palindrome does not exceed $$$k$$$. 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. For example, strings "a", "bc", "abc" are substrings of a string "abc", while strings "ac", "ba", "cba" are not. A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, strings "abccba", "abbba", "aba", "abacaba", "a", and "bacab" are palindromes, while strings "abcbba", "abb", and "ab" are not. Now Joe wants to find any correct string. Help him! It can be proven that the answer always exists under the given constraints. Input Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10$$$). The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 1,000$$$)xa0— the required string length and the maximum length of a palindrome substring, respectively. Output For each test case, print any string that satisfies the conditions from the problem statement. If there are multiple correct answers, you can print any one of them. It can be proven that the answer always exists under the given constraints. Note In the first test case of the example, the palindrome substring with the maximum length is "aa". Its length does not exceed $$$2$$$, so it fits. In the second test case all palindrome substrings have the length one.
800
false
true
false
false
false
true
false
false
false
false
3,386
919F
Imagine that Alice is playing a card game with her friend Bob. They both have exactly $$$8$$$ cards and there is an integer on each card, ranging from $$$0$$$ to $$$4$$$. In each round, Alice or Bob in turns choose two cards from different players, let them be $$$a$$$ and $$$b$$$, where $$$a$$$ is the number on the player's card, and $$$b$$$ is the number on the opponent's card. It is necessary that $$$a cdot b e 0$$$. Then they calculate $$$c = (a + b) bmod 5$$$ and replace the number $$$a$$$ with $$$c$$$. The player who ends up with numbers on all $$$8$$$ cards being $$$0$$$, wins. Now Alice wants to know who wins in some situations. She will give you her cards' numbers, Bob's cards' numbers and the person playing the first round. Your task is to determine who wins if both of them choose the best operation in their rounds. Input The first line contains one positive integer $$$T$$$ ($$$1 leq T leq 100,000$$$), denoting the number of situations you need to consider. The following lines describe those $$$T$$$ situations. For each situation: The first line contains a non-negative integer $$$f$$$ ($$$0 leq f leq 1$$$), where $$$f = 0$$$ means that Alice plays first and $$$f = 1$$$ means Bob plays first. The second line contains $$$8$$$ non-negative integers $$$a_1, a_2, ldots, a_8$$$ ($$$0 leq a_i leq 4$$$), describing Alice's cards. The third line contains $$$8$$$ non-negative integers $$$b_1, b_2, ldots, b_8$$$ ($$$0 leq b_i leq 4$$$), describing Bob's cards. We guarantee that if $$$f=0$$$, we have $$$sum_{i=1}^{8}a_i e 0$$$. Also when $$$f=1$$$, $$$sum_{i=1}^{8}b_i e 0$$$ holds. Output Output $$$T$$$ lines. For each situation, determine who wins. Output "Alice" (without quotes) if Alice wins. "Bob" (without quotes) if Bob wins. "Deal" (without quotes) if it gets into a deal, i.e. no one wins. Example Input 4 1 0 0 0 0 0 0 0 0 1 2 3 4 1 2 3 4 1 0 0 0 1 0 0 0 0 0 0 0 0 4 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 4 0 0 2 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Note In the first situation, Alice has all her numbers $$$0$$$. So she wins immediately. In the second situation, Bob picks the numbers $$$4$$$ and $$$1$$$. Because we have $$$(4 + 1) bmod 5 = 0$$$, Bob wins after this operation. In the third situation, Alice picks the numbers $$$1$$$ and $$$4$$$. She wins after this operation. In the fourth situation, we can prove that it falls into a loop.
2,600
false
false
false
false
false
false
false
false
false
true
6,051
1455A
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$. Let's define another function $$$g(x) = dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well). Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 le x le n$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 le n < 10^{100}$$$). This integer is given without leading zeroes. Output For each test case, print one integer — the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$. Example Input 5 4 37 998244353 1000000007 12345678901337426966631415 Note Explanations for the two first test cases of the example: 1. if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 le x le n$$$, $$$dfrac{x}{f(f(x))} = 1$$$; 2. if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 le x le n$$$, $$$dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
800
true
false
false
false
false
false
false
false
false
false
3,402
873C
Ivan is playing a strange game. He has a matrix _a_ with _n_ rows and _m_ columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: 1. Initially Ivan's score is 0; 2. In each column, Ivan will find the topmost 1 (that is, if the current column is _j_, then he will find minimum _i_ such that _a__i_,u2009_j_u2009=u20091). If there are no 1's in the column, this column is skipped; 3. Ivan will look at the next _min_(_k_,u2009_n_u2009-u2009_i_u2009+u20091) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score. Input The first line contains three integer numbers _n_, _m_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_m_u2009≤u2009100). Then _n_ lines follow, _i_-th of them contains _m_ integer numbers — the elements of _i_-th row of matrix _a_. Each number is either 0 or 1. Output Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score. Examples Input 4 3 2 0 1 0 1 0 1 0 1 0 1 1 1 Note In the first example Ivan will replace the element _a_1,u20092.
1,600
false
true
false
false
false
false
false
false
false
false
6,252
1817E
You're given a multiset of non-negative integers $$${a_1, a_2, dots, a_n}$$$. In one step you take two elements $$$x$$$ and $$$y$$$ of the multiset, remove them and insert their mean value $$$frac{x + y}{2}$$$ back into the multiset. You repeat the step described above until you are left with only two numbers $$$A$$$ and $$$B$$$. What is the maximum possible value of their absolute difference $$$A-B$$$? Since the answer is not an integer number, output it modulo $$$10^9+7$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^6$$$) — the size of the multiset. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$) — the elements of the multiset. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output a single integer, the answer to the problem modulo $$$10^9+7$$$. Formally, let $$$M = 10^9+7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output an integer $$$x$$$ such that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Example Input 5 2 7 3 4 1 2 10 11 3 1 2 3 6 64 32 64 16 64 0 4 1 1 1 1 Note In the first case, you can't do any operations, so the answer is $$$7-3=4$$$. In the second case, one of the optimal sequence of operations: 1. Substitute $$$1$$$ and $$$2$$$ with $$$1.5$$$; 2. Substitute $$$10$$$ and $$$11$$$ with $$$10.5$$$; 3. The difference between $$$1.5$$$ and $$$10.5$$$ is $$$9$$$. In the third case, the exact answer is $$$frac{3}{2}$$$, and $$$500,000,005 cdot 2 equiv 3 pmod{10^9+7}$$$.
3,400
false
true
false
false
false
false
true
false
false
false
1,369
1043E
Zibi is a competitive programming coach. There are $$$n$$$ competitors who want to be prepared well. The training contests are quite unusualxa0– there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems. Rules of scoring also aren't typical. The first problem is always an implementation problem: you have to implement some well-known algorithm very fast and the time of your typing is rated. The second one is an awful geometry task and you just have to get it accepted in reasonable time. Here the length and difficulty of your code are important. After that, Zibi will give some penalty points (possibly negative) for each solution and the final score of the team is the sum of them (the less the score is, the better). We know that the $$$i$$$-th competitor will always have score $$$x_i$$$ when he codes the first task and $$$y_i$$$ when he codes the second task. We can assume, that all competitors know each other's skills and during the contest distribute the problems in the way that minimizes their final score. Remember that each person codes exactly one problem in a contest. Zibi wants all competitors to write a contest with each other. However, there are $$$m$$$ pairs of people who really don't like to cooperate and they definitely won't write a contest together. Still, the coach is going to conduct trainings for all possible pairs of people, such that the people in pair don't hate each other. The coach is interested for each participant, what will be his or her sum of scores of all teams he trained in? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 300,000$$$, $$$0 le m le 300,000$$$)xa0— the number of participants and the number of pairs of people who will not write a contest together. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9 le x_i, y_i le 10^9$$$)xa0— the scores which will the $$$i$$$-th competitor get on the first problem and on the second problem. It is guaranteed that there are no two people having both $$$x_i$$$ and $$$y_i$$$ same. Each of the next $$$m$$$ lines contain two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$)xa0— indices of people who don't want to write a contest in one team. Each unordered pair of indices will appear at most once. Output Output $$$n$$$ integersxa0— the sum of scores for all participants in the same order as they appear in the input. Examples Input 3 3 1 2 2 3 1 3 1 2 2 3 1 3 Input 5 3 -1 3 2 4 1 1 3 5 2 2 1 4 2 3 3 5 Note In the first example, there will be only one team consisting of persons $$$1$$$ and $$$3$$$. The optimal strategy for them is to assign the first task to the $$$3$$$-rd person and the second task to the $$$1$$$-st person, this will lead to score equal to $$$1 + 2 = 3$$$. In the second example, nobody likes anyone, so there won't be any trainings. It seems that Zibi won't be titled coach in that case...
1,900
true
true
false
false
false
true
false
false
true
false
5,479
265A
Problem - 265A - 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 time limit per test 2 seconds memory limit per test 256 megabytes input stdin output stdout There is a sequence of colorful stones. The color of each stone is one of red, green, or blue. You are given a string _s_. The _i_-th (1-based) character of _s_ represents the color of the _i_-th stone. If the character is "R", "G", or "B", the color of the corresponding stone is red, green, or blue, respectively. Initially Squirrel Liss is standing on the first stone. You perform instructions one or more times. Each instruction is one of the three types: "RED", "GREEN", or "BLUE". After an instruction _c_, if Liss is standing on a stone whose colors is _c_, Liss will move one stone forward, else she will not move. You are given a string _t_. The number of instructions is equal to the length of _t_, and the _i_-th character of _t_ represents the _i_-th instruction. Calculate the final position of Liss (the number of the stone she is going to stand on in the end) after performing all the instructions, and print its 1-based position. It is guaranteed that Liss don't move out of the sequence. Input The input contains two lines. The first line contains the string _s_ (1u2009≤u2009_s_u2009≤u200950). The second line contains the string _t_ (1u2009≤u2009_t_u2009≤u200950). The characters of each string will be one of "R", "G", or "B". It is guaranteed that Liss don't move out of the sequence. Output Print the final 1-based position of Liss in a single line. Examples Input RGB RRR Output 2 Input RRRBGBRBBB BBBRR Output 3 Input BRRBGBRGRBGRGRRGGBGBGBRGBRGRGGGRBRRRBRBBBGRRRGGBBB BBRBGGRGRGBBBRBGRBRBBBBRBRRRBGBBGBBRRBBGGRBRRBRGRB Output 15
800
false
false
true
false
false
false
false
false
false
false
8,773
847M
Vasya came up with his own weather forecasting method. He knows the information about the average air temperature for each of the last _n_ days. Assume that the average air temperature for each day is integral. Vasya believes that if the average temperatures over the last _n_ days form an arithmetic progression, where the first term equals to the average temperature on the first day, the second term equals to the average temperature on the second day and so on, then the average temperature of the next (_n_u2009+u20091)-th day will be equal to the next term of the arithmetic progression. Otherwise, according to Vasya's method, the temperature of the (_n_u2009+u20091)-th day will be equal to the temperature of the _n_-th day. Your task is to help Vasya predict the average temperature for tomorrow, i. e. for the (_n_u2009+u20091)-th day. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009100) — the number of days for which the average air temperature is known. The second line contains a sequence of integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (u2009-u20091000u2009≤u2009_t__i_u2009≤u20091000)xa0— where _t__i_ is the average temperature in the _i_-th day. Output Print the average air temperature in the (_n_u2009+u20091)-th day, which Vasya predicts according to his method. Note that the absolute value of the predicted temperature can exceed 1000. Note In the first example the sequence of the average temperatures is an arithmetic progression where the first term is 10 and each following terms decreases by 5. So the predicted average temperature for the sixth day is u2009-u200910u2009-u20095u2009=u2009u2009-u200915. In the second example the sequence of the average temperatures is an arithmetic progression where the first term is 1 and each following terms equals to the previous one. So the predicted average temperature in the fifth day is 1. In the third example the average temperatures do not form an arithmetic progression, so the average temperature of the fourth day equals to the temperature of the third day and equals to u2009-u20095. In the fourth example the sequence of the average temperatures is an arithmetic progression where the first term is 900 and each the following terms increase by 100. So predicted average temperature in the third day is 1000u2009+u2009100u2009=u20091100.
1,000
true
false
true
false
false
false
false
false
false
false
6,359
45E
Vasya is a born Berland film director, he is currently working on a new blockbuster, "The Unexpected". Vasya knows from his own experience how important it is to choose the main characters' names and surnames wisely. He made up a list of _n_ names and _n_ surnames that he wants to use. Vasya haven't decided yet how to call characters, so he is free to match any name to any surname. Now he has to make the list of all the main characters in the following format: "_Name_1 _Surname_1, _Name_2 _Surname_2, ..., _Name__n_ _Surname__n_", i.e. all the name-surname pairs should be separated by exactly one comma and exactly one space, and the name should be separated from the surname by exactly one space. First of all Vasya wants to maximize the number of the pairs, in which the name and the surname start from one letter. If there are several such variants, Vasya wants to get the lexicographically minimal one. Help him. An answer will be verified a line in the format as is shown above, including the needed commas and spaces. It's the lexicographical minimality of such a line that needs to be ensured. The output line shouldn't end with a space or with a comma. Input The first input line contains number _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of names and surnames. Then follow _n_ lines — the list of names. Then follow _n_ lines — the list of surnames. No two from those 2_n_ strings match. Every name and surname is a non-empty string consisting of no more than 10 Latin letters. It is guaranteed that the first letter is uppercase and the rest are lowercase. Output The output data consist of a single line — the needed list. Note that one should follow closely the output data format! Examples Input 4 Ann Anna Sabrina John Petrov Ivanova Stoltz Abacaba Output Ann Abacaba, Anna Ivanova, John Petrov, Sabrina Stoltz Output Aa Ad, Ab Ae, Ac Bb, Ba Bc
2,000
false
true
false
false
false
true
false
false
false
false
9,755
1256E
There are $$$n$$$ students at your university. The programming skill of the $$$i$$$-th student is $$$a_i$$$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $$$2 cdot 10^5$$$ students ready for the finals! Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $$$k$$$ students with programming skills $$$a[i_1], a[i_2], dots, a[i_k]$$$, then the diversity of this team is $$$maxlimits_{j=1}^{k} a[i_j] - minlimits_{j=1}^{k} a[i_j]$$$). The total diversity is the sum of diversities of all teams formed. Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students. Input The first line of the input contains one integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$) — the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the programming skill of the $$$i$$$-th student. Output In the first line print two integers $$$res$$$ and $$$k$$$ — the minimum total diversity of the division of students and the number of teams in your division, correspondingly. In the second line print $$$n$$$ integers $$$t_1, t_2, dots, t_n$$$ ($$$1 le t_i le k$$$), where $$$t_i$$$ is the number of team to which the $$$i$$$-th student belong. If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students. Examples Input 10 1 2 5 129 185 581 1041 1909 1580 8150 Output 7486 3 3 3 3 2 2 2 2 1 1 1 Note In the first example, there is only one team with skills $$$[1, 1, 2, 3, 4]$$$ so the answer is $$$3$$$. It can be shown that you cannot achieve a better answer. In the second example, there are two teams with skills $$$[1, 2, 5]$$$ and $$$[12, 13, 15]$$$ so the answer is $$$4 + 3 = 7$$$. In the third example, there are three teams with skills $$$[1, 2, 5]$$$, $$$[129, 185, 581, 1041]$$$ and $$$[1580, 1909, 8150]$$$ so the answer is $$$4 + 912 + 6570 = 7486$$$.
2,000
false
true
false
true
false
false
false
false
true
false
4,403
1252G
Randall is a software engineer at a company with $$$N$$$ employees. Every year, the company re-evaluates its employees. At the end of every year, the company replaces its several worst-performing employees and replaces with the same number of new employees, so that the company keeps having $$$N$$$ employees. Each person has a constant performance and can be represented by an integer (higher integer means better performance), and no two people have the same performance. The performance of the initial employees are represented by an array of integers $$$A = [A_1, A_2, dots, A_N]$$$ where $$$A_i$$$ is the performance of the $$$i^{th}$$$ employee. Randall is employee $$$1$$$, so his performance is $$$A_1$$$. We will consider the first $$$M$$$ years. At the end of the $$$i^{th}$$$ year, the company replaces its $$$R_i$$$ worst-performing employees and replaces with $$$R_i$$$ new employees. The performance of these new employees are represented by an array of integers $$$B_i = [(B_i)_1, (B_i)_2, dots, (B_i)_{R_i}]$$$ where $$$(B_i)_j$$$ is the performance of the $$$j^{th}$$$ new employee. He will consider $$$Q$$$ scenarios. On the $$$i^{th}$$$ scenario, he will change the value of $$$(B_{X_i})_{Y_i}$$$ to $$$Z_i$$$. For each scenario, Randall is wondering whether he will still be in the company after $$$M$$$ years. Note that the changes in each scenario are kept for the subsequent scenarios. Input Input begins with a line containing three integers: $$$N$$$ $$$M$$$ $$$Q$$$ ($$$2 le N le 100,000$$$; $$$1 le M, Q le 100,000$$$) representing the number of employees, the number of years to be considered, and the number of scenarios, respectively. The next line contains $$$N$$$ integers: $$$A_i$$$ ($$$0 le A_i le 10^9$$$) representing the performance of the initial employees. The next $$$M$$$ lines each contains several integers: $$$R_i$$$ $$$(B_i)_1$$$, $$$(B_i)_2$$$, $$$cdots$$$, $$$(B_i)_{R_i}$$$ ($$$1 le R_i < N$$$; $$$0 le (B_i)_j le 10^9$$$) representing the number of employees replaced and the performance of the new employees, respectively. It is guaranteed that the sum of $$$R_i$$$ does not exceed $$$10^6$$$. The next $$$Q$$$ lines each contains three integers: $$$X_i$$$ $$$Y_i$$$ $$$Z_i$$$ ($$$1 le X_i le M$$$; $$$1 le Y_i le R_{(X_i)}$$$; $$$0 le Z_i le 10^9$$$) representing a scenario. It is guaranteed that all integers in all $$$A_i$$$, $$$(B_i)_j$$$, and $$$Z_i$$$ (combined together) are distinct. Output For each scenario in the same order as input, output in a line an integer $$$0$$$ if Randall will not be in the company after $$$M$$$ years, or $$$1$$$ if Randall will still be in the company after $$$M$$$ years. Example Input 5 3 3 50 40 30 20 10 4 1 2 3 100 1 4 2 6 7 1 3 300 2 1 400 2 1 5 Note Explanation for the sample input/output #1 Randall performance is represented by $$$50$$$. For the first scenario, the value of $$$(B_1)_3$$$ is updated to $$$300$$$, causes the following: Initially, the performance of the employees is $$$[50, 40, 30, 20, 10]$$$. At the end of the first year, $$$4$$$ worst-performing employees are replaced by employees with performance $$$[300, 100, 2, 1]$$$. Therefore, the performance of the employees is $$$[300, 100, 50, 2, 1]$$$. At the end of the second year, the performance of the employees is $$$[300, 100, 50, 4, 2]$$$. At the end of the third year, the performance of the employees is $$$[300, 100, 50, 7, 6]$$$. Therefore, Randall will still be in the company after $$$3$$$ years. For the second scenario, the value of $$$(B_2)_1$$$ is updated to $$$400$$$, causes the following: Initially, the performance of the employees is $$$[50, 40, 30, 20, 10]$$$. At the end of the first year, the performance of the employees is $$$[300, 100, 50, 2, 1]$$$. Recall that the change in the first scenario is kept for this scenario as well. At the end of the second year, the performance of the employees is $$$[400, 300, 100, 50, 2]$$$. At the end of the third year, the performance of the employees is $$$[400, 300, 100, 7, 6]$$$. Therefore, Randall will not be in the company after $$$3$$$ years.
2,100
false
false
false
false
true
false
false
false
false
false
4,428
628A
A tennis tournament with _n_ participants is running. The participants are playing by an olympic system, so the winners move on and the losers drop out. The tournament takes place in the following way (below, _m_ is the number of the participants of the current round): let _k_ be the maximal power of the number 2 such that _k_u2009≤u2009_m_, _k_ participants compete in the current round and a half of them passes to the next round, the other _m_u2009-u2009_k_ participants pass to the next round directly, when only one participant remains, the tournament finishes. Each match requires _b_ bottles of water for each participant and one bottle for the judge. Besides _p_ towels are given to each participant for the whole tournament. Find the number of bottles and towels needed for the tournament. Note that it's a tennis tournament so in each match two participants compete (one of them will win and the other will lose). Input The only line contains three integers _n_,u2009_b_,u2009_p_ (1u2009≤u2009_n_,u2009_b_,u2009_p_u2009≤u2009500) — the number of participants and the parameters described in the problem statement. Output Print two integers _x_ and _y_ — the number of bottles and towels need for the tournament. Note In the first example will be three rounds: 1. in the first round will be two matches and for each match 5 bottles of water are needed (two for each of the participants and one for the judge), 2. in the second round will be only one match, so we need another 5 bottles of water, 3. in the third round will also be only one match, so we need another 5 bottles of water. So in total we need 20 bottles of water. In the second example no participant will move on to some round directly.
1,000
true
false
true
false
false
false
false
false
false
false
7,315
1288C
Problem - 1288C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp *1600 No tag edit access → Contest materials $$$ such that: the length of both arrays is equal to $$$m$$$; each element of each array is an integer between $$$1$$$ and $$$n$$$ (inclusive); $$$a_i le b_i$$$ for any index $$$i$$$ from $$$1$$$ to $$$m$$$; array $$$a$$$ is sorted in non-descending order; array $$$b$$$ is sorted in non-ascending order. As the result can be very large, you should print it modulo $$$10^9+7$$$. Input The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 1000$$$, $$$1 le m le 10$$$). Output Print one integer – the number of arrays $$$a$$$ and $$$b$$$ satisfying the conditions described above modulo $$$10^9+7$$$. Examples Input 2 2 Output 5 Input 10 1 Output 55 Input 723 9 Output 157557417 Note In the first test there are $$$5$$$ suitable arrays: $$$a =
1,600
false
false
false
true
false
false
false
false
false
false
4,250
1716D
There is a chip on the coordinate line. Initially, the chip is located at the point $$$0$$$. You can perform any number of moves; each move increases the coordinate of the chip by some positive integer (which is called the length of the move). The length of the first move you make should be divisible by $$$k$$$, the length of the second movexa0— by $$$k+1$$$, the thirdxa0— by $$$k+2$$$, and so on. For example, if $$$k=2$$$, then the sequence of moves may look like this: $$$0 ightarrow 4 ightarrow 7 ightarrow 19 ightarrow 44$$$, because $$$4 - 0 = 4$$$ is divisible by $$$2 = k$$$, $$$7 - 4 = 3$$$ is divisible by $$$3 = k + 1$$$, $$$19 - 7 = 12$$$ is divisible by $$$4 = k + 2$$$, $$$44 - 19 = 25$$$ is divisible by $$$5 = k + 3$$$. You are given two positive integers $$$n$$$ and $$$k$$$. Your task is to count the number of ways to reach the point $$$x$$$, starting from $$$0$$$, for every $$$x in [1, n]$$$. The number of ways can be very large, so print it modulo $$$998244353$$$. Two ways are considered different if they differ as sets of visited positions. Note Let's look at the first example: Ways to reach the point $$$1$$$: $$$[0, 1]$$$; Ways to reach the point $$$2$$$: $$$[0, 2]$$$; Ways to reach the point $$$3$$$: $$$[0, 1, 3]$$$, $$$[0, 3]$$$; Ways to reach the point $$$4$$$: $$$[0, 2, 4]$$$, $$$[0, 4]$$$; Ways to reach the point $$$5$$$: $$$[0, 1, 5]$$$, $$$[0, 3, 5]$$$, $$$[0, 5]$$$; Ways to reach the point $$$6$$$: $$$[0, 1, 3, 6]$$$, $$$[0, 2, 6]$$$, $$$[0, 4, 6]$$$, $$$[0, 6]$$$; Ways to reach the point $$$7$$$: $$$[0, 2, 4, 7]$$$, $$$[0, 1, 7]$$$, $$$[0, 3, 7]$$$, $$$[0, 5, 7]$$$, $$$[0, 7]$$$; Ways to reach the point $$$8$$$: $$$[0, 3, 5, 8]$$$, $$$[0, 1, 5, 8]$$$, $$$[0, 2, 8]$$$, $$$[0, 4, 8]$$$, $$$[0, 6, 8]$$$, $$$[0, 8]$$$.
2,000
true
false
false
true
false
false
true
false
false
false
1,984
490D
Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is _a_1u2009×u2009_b_1 segments large and the second one is _a_2u2009×u2009_b_2 segments large. Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus's mind and Paraskevi's beauty are equally matched, so the two bars must have the same number of squares. To make the bars have the same number of squares, Polycarpus eats a little piece of chocolate each minute. Each minute he does the following: he either breaks one bar exactly in half (vertically or horizontally) and eats exactly a half of the bar, or he chips of exactly one third of a bar (vertically or horizontally) and eats exactly a third of the bar. In the first case he is left with a half, of the bar and in the second case he is left with two thirds of the bar. Both variants aren't always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16u2009×u200923, then Polycarpus can chip off a half, but not a third. If the bar is 20u2009×u200918, then Polycarpus can chip off both a half and a third. If the bar is 5u2009×u20097, then Polycarpus cannot chip off a half nor a third. What is the minimum number of minutes Polycarpus needs to make two bars consist of the same number of squares? Find not only the required minimum number of minutes, but also the possible sizes of the bars after the process. Input The first line of the input contains integers _a_1,u2009_b_1 (1u2009≤u2009_a_1,u2009_b_1u2009≤u2009109) — the initial sizes of the first chocolate bar. The second line of the input contains integers _a_2,u2009_b_2 (1u2009≤u2009_a_2,u2009_b_2u2009≤u2009109) — the initial sizes of the second bar. You can use the data of type int64 (in Pascal), long long (in С++), long (in Java) to process large integers (exceeding 231u2009-u20091). Output In the first line print _m_ — the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in _m_ minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line must correspond to the first bar and the third line must correspond to the second bar. If there are multiple solutions, print any of them. If there is no solution, print a single line with integer -1.
1,900
true
false
false
false
false
false
true
false
false
false
7,874
1234A
You are both a shop keeper and a shop assistant at a small nearby shop. You have $$$n$$$ goods, the $$$i$$$-th good costs $$$a_i$$$ coins. You got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all $$$n$$$ goods you have. However, you don't want to lose any money so you want to choose the price in such a way that the sum of new prices is not less than the sum of the initial prices. It means that if you sell all $$$n$$$ goods for the new price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices. On the other hand, you don't want to lose customers because of big prices so among all prices you can choose you need to choose the minimum one. So you need to find the minimum possible equal price of all $$$n$$$ goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 100$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 100)$$$ — the number of goods. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^7$$$), where $$$a_i$$$ is the price of the $$$i$$$-th good. Output For each query, print the answer for it — the minimum possible equal price of all $$$n$$$ goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices. Example Input 3 5 1 2 3 4 5 3 1 2 2 4 1 1 1 1
800
true
false
false
false
false
false
false
false
false
false
4,525
312B
Problem - 312B - 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 probabilities *1300 No tag edit access → Contest materials
1,300
true
false
false
false
false
false
false
false
false
false
8,596
52A
Problem - 52A - Codeforces =============== xa0 . The second line contains a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20093). Output Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal. Examples Input 9 1 3 2 2 2 1 1 2 3 Output 5 Note In the example all the numbers equal to 1 and 3 should be replaced by 2.
900
false
false
true
false
false
false
false
false
false
false
9,713
585B
The mobile application store has a new game called "Subway Roller". The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and _n_ columns. At the beginning of the game the hero is in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of two or more neighbouring cells in some row of the field. All trains are moving from right to left at a speed of two cells per second, and the hero runs from left to right at the speed of one cell per second. For simplicity, the game is implemented so that the hero and the trains move in turns. First, the hero moves one cell to the right, then one square up or down, or stays idle. Then all the trains move twice simultaneously one cell to the left. Thus, in one move, Philip definitely makes a move to the right and can move up or down. If at any point, Philip is in the same cell with a train, he loses. If the train reaches the left column, it continues to move as before, leaving the tunnel. Your task is to answer the question whether there is a sequence of movements of Philip, such that he would be able to get to the rightmost column. Input Each test contains from one to ten sets of the input data. The first line of the test contains a single integer _t_ (1u2009≤u2009_t_u2009≤u200910 for pretests and tests or _t_u2009=u20091 for hacks; see the Notes section for details) — the number of sets. Then follows the description of _t_ sets of the input data. The first line of the description of each set contains two integers _n_,u2009_k_ (2u2009≤u2009_n_u2009≤u2009100,u20091u2009≤u2009_k_u2009≤u200926) — the number of columns on the field and the number of trains. Each of the following three lines contains the sequence of _n_ character, representing the row of the field where the game is on. Philip's initial position is marked as 's', he is in the leftmost column. Each of the _k_ trains is marked by some sequence of identical uppercase letters of the English alphabet, located in one line. Distinct trains are represented by distinct letters. Character '.' represents an empty cell, that is, the cell that doesn't contain either Philip or the trains. Output For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise. Examples Input 2 16 4 ...AAAAA........ s.BBB......CCCCC ........DDDDD... 16 4 ...AAAAA........ s.BBB....CCCCC.. .......DDDDD.... Input 2 10 4 s.ZZ...... .....AAABB .YYYYYY... 10 4 s.ZZ...... ....AAAABB .YYYYYY... Note In the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip's path, so he can go straight to the end of the tunnel. Note that in this problem the challenges are restricted to tests that contain only one testset.
1,700
false
false
false
false
false
false
false
false
false
true
7,504
711D
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of _n_ towns numbered from 1 to _n_. There are _n_ directed roads in the Udayland. _i_-th of them goes from town _i_ to some other town _a__i_ (_a__i_u2009≠u2009_i_). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town _A_ to town _B_ before the flip, it will go from town _B_ to town _A_ after. ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns _A_1,u2009_A_2,u2009...,u2009_A__k_ (_k_u2009>u20091) such that for every 1u2009≤u2009_i_u2009<u2009_k_ there is a road from town _A__i_ to town _A__i_u2009+u20091 and another road from town _A__k_ to town _A_1. In other words, the roads are confusing if some of them form a directed cycle of some towns. Now ZS the Coder wonders how many sets of roads (there are 2_n_ variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing. Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities. Input The first line of the input contains single integer _n_ (2u2009≤u2009_n_u2009≤u20092·105)xa0— the number of towns in Udayland. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_i_), _a__i_ denotes a road going from town _i_ to town _a__i_. Output Print a single integerxa0— the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109u2009+u20097. Note Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are , , initially. Number the roads 1 to 3 in this order. The sets of roads that ZS the Coder can flip (to make them not confusing) are {1},u2009{2},u2009{3},u2009{1,u20092},u2009{1,u20093},u2009{2,u20093}. Note that the empty set is invalid because if no roads are flipped, then towns 1,u20092,u20093 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip. The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing.
1,900
true
false
false
false
false
false
false
false
false
true
6,973
625D
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder. Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13. Oksana Fillipovna picked some number _a_ without leading zeroes, and flipped it to get number _a__r_. Then she summed _a_ and _a__r_, and told Vitya the resulting value _n_. His goal is to find any valid _a_. As Oksana Fillipovna picked some small integers as _a_ and _a__r_, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given _n_ finds any _a_ without leading zeroes, such that _a_u2009+u2009_a__r_u2009=u2009_n_ or determine that such _a_ doesn't exist. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u200910100u2009000). Output If there is no such positive integer _a_ without leading zeroes that _a_u2009+u2009_a__r_u2009=u2009_n_ then print 0. Otherwise, print any valid _a_. If there are many possible answers, you are allowed to pick any. Note In the first sample 4u2009=u20092u2009+u20092, _a_u2009=u20092 is the only possibility. In the second sample 11u2009=u200910u2009+u20091, _a_u2009=u200910xa0— the only valid solution. Note, that _a_u2009=u200901 is incorrect, because _a_ can't have leading zeroes. It's easy to check that there is no suitable _a_ in the third sample. In the fourth sample 33u2009=u200930u2009+u20093u2009=u200912u2009+u200921, so there are three possibilities for _a_: _a_u2009=u200930, _a_u2009=u200912, _a_u2009=u200921. Any of these is considered to be correct answer.
2,400
true
false
true
false
false
true
false
false
false
false
7,330
1862C
Anya lives in the Flower City. By order of the city mayor, she has to build a fence for herself. The fence consists of $$$n$$$ planks, each with a height of $$$a_i$$$ meters. According to the order, the heights of the planks must not increase. In other words, it is true that $$$a_i ge a_j$$$ for all $$$i < j$$$. Anya became curious whether her fence is symmetrical with respect to the diagonal. In other words, will she get the same fence if she lays all the planks horizontally in the same order. For example, for $$$n = 5$$$, $$$a = [5, 4, 3, 2, 1]$$$, the fence is symmetrical. Because if all the planks are laid horizontally, the fence will be $$$[5, 4, 3, 2, 1]$$$, as shown in the diagram. On the left is the fence $$$[5, 4, 3, 2, 1]$$$, on the right is the same fence laid horizontally But for $$$n = 3$$$, $$$a = [4, 2, 1]$$$, the fence is not symmetrical. Because if all the planks are laid horizontally, the fence will be $$$[3, 2, 1, 1]$$$, as shown in the diagram. On the left is the fence $$$[4, 2, 1]$$$, on the right is the same fence laid horizontally Help Anya and determine whether her fence is symmetrical. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of a test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the fence. The second line of a test case contains $$$n$$$ integers $$$a_1 ge a_2 ge a_3 ge dots ge a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the heights of the planks. The sum of the values of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" if the fence is symmetrical, otherwise output "NO". You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes" and "YES" will be accepted as a positive answer. Example Input 7 5 5 4 3 2 1 3 3 1 1 3 4 2 1 1 2 5 5 3 3 1 1 5 5 5 5 3 3 2 6 1 Output YES YES NO NO YES YES NO Note In the first and second test cases of the example, the fence is symmetrical. In the third test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be $$$[3, 2, 1, 1]$$$. In the fourth test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be $$$[1, 1]$$$. In the fifth and sixth test cases of the example, the fence is symmetrical. In the seventh test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be $$$[2, 1, 1, 1, 1, 1]$$$.
1,100
false
false
true
false
true
false
false
true
true
false
1,111
1931G
You have a one-dimensional puzzle, all the elements of which need to be put in one row, connecting with each other. All the puzzle elements are completely white and distinguishable from each other only if they have different shapes. Each element has straight borders at the top and bottom, and on the left and right it has connections, each of which can be a protrusion or a recess. You cannot rotate the elements. You can see that there are exactly $$$4$$$ types of elements. Two elements can be connected if the right connection of the left element is opposite to the left connection of the right element. All possible types of elements. The puzzle contains $$$c_1, c_2, c_3, c_4$$$ elements of each type. The puzzle is considered complete if you have managed to combine all elements into one long chain. You want to know how many ways this can be done. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$) — the number of input test cases. The descriptions of the test cases follow. The description of each test case contains $$$4$$$ integers $$$c_i$$$ ($$$0 le c_i le 10^6$$$) — the number of elements of each type, respectively. It is guaranteed that the sum of $$$c_i$$$ for all test cases does not exceed $$$4 cdot 10^6$$$. Output For each test case, print one integer — the number of possible ways to solve the puzzle. Two methods are considered different if there is $$$i$$$, such that the types of elements at the $$$i$$$ position in these methods differ. Since the answer can be very large, output it modulo $$$998244353$$$. If it is impossible to solve the puzzle, print $$$0$$$. Example Input 11 1 1 1 1 1 2 5 10 4 6 100 200 900000 900000 900000 900000 0 0 0 0 0 0 566 239 1 0 0 0 100 0 100 0 0 0 0 4 5 5 0 2 5 4 0 5 Output 4 66 0 794100779 1 0 1 0 1 36 126
2,000
true
false
false
false
false
false
false
false
false
false
695
1174B
Problem - 1174B - 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 sortings *1200 No tag edit access → Contest materials $$$ such that $$$a_i+a_j$$$ is odd, then swap $$$a_i$$$ and $$$a_j$$$. What is lexicographically the smallest array you can obtain? An array $$$x$$$ is xa0— the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$ldots$$$, $$$a_{n}$$$ ($$$1 le a_i le 10^9$$$)xa0— the elements of the array $$$a$$$. Output The only line contains $$$n$$$ space-separated integers, the lexicographically smallest array you can obtain. Examples Input 3 4 1 7 Output 1 4 7 Input 2 1 1 Output 1 1 Note In the first example, we can swap $$$1$$$ and $$$4$$$ since $$$1+4=5$$$, which is odd.
1,200
false
false
false
false
false
false
false
false
true
false
4,850
1251A
Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains $$$26$$$ buttons (one for each letter of the Latin alphabet). Each button is either working fine or malfunctioning. To check which buttons need replacement, Polycarp pressed some buttons in sequence, and a string $$$s$$$ appeared on the screen. When Polycarp presses a button with character $$$c$$$, one of the following events happened: if the button was working correctly, a character $$$c$$$ appeared at the end of the string Polycarp was typing; if the button was malfunctioning, two characters $$$c$$$ appeared at the end of the string. For example, suppose the buttons corresponding to characters a and c are working correctly, and the button corresponding to b is malfunctioning. If Polycarp presses the buttons in the order a, b, a, c, a, b, a, then the string he is typing changes as follows: a $$$ ightarrow$$$ abb $$$ ightarrow$$$ abba $$$ ightarrow$$$ abbac $$$ ightarrow$$$ abbaca $$$ ightarrow$$$ abbacabb $$$ ightarrow$$$ abbacabba. You are given a string $$$s$$$ which appeared on the screen after Polycarp pressed some buttons. Help Polycarp to determine which buttons are working correctly for sure (that is, this string could not appear on the screen if any of these buttons was malfunctioning). You may assume that the buttons don't start malfunctioning when Polycarp types the string: each button either works correctly throughout the whole process, or malfunctions throughout the whole process. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases in the input. Then the test cases follow. Each test case is represented by one line containing a string $$$s$$$ consisting of no less than $$$1$$$ and no more than $$$500$$$ lowercase Latin letters. Output For each test case, print one line containing a string $$$res$$$. The string $$$res$$$ should contain all characters which correspond to buttons that work correctly in alphabetical order, without any separators or repetitions. If all buttons may malfunction, $$$res$$$ should be empty.
1,000
false
false
false
false
false
false
true
false
false
false
4,441
580E
One day Kefa the parrot was walking down the street as he was on the way home from the restaurant when he saw something glittering by the road. As he came nearer he understood that it was a watch. He decided to take it to the pawnbroker to earn some money. The pawnbroker said that each watch contains a serial number represented by a string of digits from 0 to 9, and the more quality checks this number passes, the higher is the value of the watch. The check is defined by three positive integers _l_, _r_ and _d_. The watches pass a check if a substring of the serial number from _l_ to _r_ has period _d_. Sometimes the pawnbroker gets distracted and Kefa changes in some substring of the serial number all digits to _c_ in order to increase profit from the watch. The seller has a lot of things to do to begin with and with Kefa messing about, he gave you a task: to write a program that determines the value of the watch. Let us remind you that number _x_ is called a period of string _s_ (1u2009≤u2009_x_u2009≤u2009_s_), if _s__i_u2009u2009=u2009u2009_s__i_u2009+u2009_x_ for all _i_ from 1 to _s_u2009u2009-u2009u2009_x_. Input The first line of the input contains three positive integers _n_, _m_ and _k_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_m_u2009+u2009_k_u2009≤u2009105) — the length of the serial number, the number of change made by Kefa and the number of quality checks. The second line contains a serial number consisting of _n_ digits. Then _m_u2009+u2009_k_ lines follow, containing either checks or changes. The changes are given as 1 _l_ _r_ _c_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_, 0u2009≤u2009_c_u2009≤u20099). That means that Kefa changed all the digits from the _l_-th to the _r_-th to be _c_. The checks are given as 2 _l_ _r_ _d_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_, 1u2009≤u2009_d_u2009≤u2009_r_u2009-u2009_l_u2009+u20091). Output For each check on a single line print "YES" if the watch passed it, otherwise print "NO". Examples Input 3 1 2 112 2 2 3 1 1 1 3 8 2 1 2 1 Input 6 2 3 334934 2 2 5 2 1 4 4 3 2 1 6 3 1 2 3 8 2 3 6 1 Note In the first sample test two checks will be made. In the first one substring "12" is checked on whether or not it has period 1, so the answer is "NO". In the second one substring "88", is checked on whether or not it has period 1, and it has this period, so the answer is "YES". In the second statement test three checks will be made. The first check processes substring "3493", which doesn't have period 2. Before the second check the string looks as "334334", so the answer to it is "YES". And finally, the third check processes substring "8334", which does not have period 1.
2,500
false
false
false
false
true
false
false
false
false
false
7,524
1854A1
The only difference between the two versions of this problem is the constraint on the maximum number of operations. You can make hacks only if all versions of the problem are solved. You are given an array $$$a_1, a_2,dots, a_n$$$ of integers (positive, negative or $$$0$$$). You can perform multiple operations on the array (possibly $$$0$$$ operations). In one operation, you choose $$$i, j$$$ ($$$1 leq i, j leq n$$$, they can be equal) and set $$$a_i := a_i + a_j$$$ (i.e., add $$$a_j$$$ to $$$a_i$$$). Make the array non-decreasing (i.e., $$$a_i leq a_{i+1}$$$ for $$$1 leq i leq n-1$$$) in at most $$$50$$$ operations. You do not need to minimize the number of operations. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$1 le n le 20$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-20 le a_i le 20$$$) — the array before performing the operations. Output For each test case, output your operations in the following format. The first line should contain an integer $$$k$$$ ($$$0 le k le 50$$$) — the number of operations. The next $$$k$$$ lines represent the $$$k$$$ operations in order. Each of these $$$k$$$ lines should contain two integers $$$i$$$ and $$$j$$$ ($$$1 leq i, j leq n$$$) — the corresponding operation consists in adding $$$a_j$$$ to $$$a_i$$$. After all the operations, the array $$$a_1, a_2,dots, a_n$$$ must be non-decreasing. Example Input 10 2 2 1 4 1 2 -10 3 5 2 1 1 1 1 8 0 0 0 0 0 0 0 0 5 1 2 -4 3 -10 10 11 12 13 14 15 -15 -16 -17 -18 -19 7 1 9 3 -4 -3 -2 -1 3 10 9 8 20 1 -14 2 -10 6 -5 10 -13 10 7 -14 19 -5 19 1 18 -16 -7 12 8 20 -15 -17 -13 8 14 -13 10 -4 11 -4 -16 -6 15 -4 -2 7 -9 5 -5 17 Output 1 2 1 3 4 4 4 4 3 4 4 2 1 3 1 4 1 5 1 0 7 3 4 3 4 5 4 5 4 5 4 5 4 5 4 15 6 1 6 1 6 1 7 2 7 2 7 2 8 3 8 3 8 3 9 4 9 4 9 4 10 5 10 5 10 5 8 3 4 3 4 2 4 2 4 2 4 2 4 1 4 1 4 3 2 1 3 1 3 1 31 14 1 18 7 13 11 15 11 6 4 5 17 19 6 19 12 10 5 11 12 1 17 15 19 16 10 14 2 16 11 20 7 7 6 9 5 3 6 6 14 17 18 18 14 12 3 17 16 8 18 13 16 9 8 14 8 16 2 11 8 12 7 31 5 12 19 13 9 1 5 17 18 19 6 16 15 8 6 9 15 14 7 10 19 7 17 20 14 4 15 20 4 3 1 8 16 12 16 15 5 6 12 10 11 15 20 3 20 19 13 14 11 14 18 10 7 3 12 17 4 7 13 2 11 13 Note In the first test case, by adding $$$a_1 = 2$$$ to $$$a_2$$$, we get the array $$$[2, 3]$$$ which is non-decreasing. In the second test case, the array changes as: $$$[1, 2, -10, 3]$$$ $$$[1, 2, -10, 6]$$$ $$$[1, 2, -10, 12]$$$ $$$[1, 2, 2, 12]$$$ In the third test case, the final array is $$$[2, 3, 3, 3, 3]$$$.
1,400
true
false
false
false
false
true
false
false
false
false
1,159
1896F
You are given a binary string $$$s$$$ of length $$$2n$$$ where each element is $$$mathtt{0}$$$ or $$$mathtt{1}$$$. You can do the following operation: 1. Choose a balanced bracket sequence$$$^dagger$$$ $$$b$$$ of length $$$2n$$$. 2. For every index $$$i$$$ from $$$1$$$ to $$$2n$$$ in order, where $$$b_i$$$ is an open bracket, let $$$p_i$$$ denote the minimum index such that $$$b[i,p_i]$$$ is a balanced bracket sequence. Then, we perform a range toggle operation$$$^ddagger$$$ from $$$i$$$ to $$$p_i$$$ on $$$s$$$. Note that since a balanced bracket sequence of length $$$2n$$$ will have $$$n$$$ open brackets, we will do $$$n$$$ range toggle operations on $$$s$$$. Your task is to find a sequence of no more than $$$10$$$ operations that changes all elements of $$$s$$$ to $$$mathtt{0}$$$, or determine that it is impossible to do so. Note that you do not have to minimize the number of operations. Under the given constraints, it can be proven that if it is possible to change all elements of $$$s$$$ to $$$mathtt{0}$$$, there exists a way that requires no more than $$$10$$$ operations. $$$^dagger$$$ A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters $$$+$$$ and $$$1$$$. For example, sequences "(())()", "()", and "(()(()))" are balanced, while ")(", "(()", and "(()))(" are not. $$$^ddagger$$$ If we perform a range toggle operation from $$$l$$$ to $$$r$$$ on a binary string $$$s$$$, then we toggle all values of $$$s_i$$$ such that $$$l leq i leq r$$$. If $$$s_i$$$ is toggled, we will set $$$s_i := mathtt{0}$$$ if $$$s_i = mathtt{1}$$$ or vice versa. For example, if $$$s=mathtt{1000101}$$$ and we perform a range toggle operation from $$$3$$$ to $$$5$$$, $$$s$$$ will be changed to $$$s=mathtt{1011001}$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2cdot 10^5$$$)xa0— where $$$2n$$$ is the length of string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$2n$$$ ($$$s_i = mathtt{0}$$$ or $$$s_i = mathtt{1}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output $$$-1$$$ in a single line if it is impossible to change all elements of $$$s$$$ to $$$mathtt{0}$$$. Otherwise, output a single integer $$$k$$$ ($$$0 le k le 10$$$) representing the number of operations needed to change all elements of $$$s$$$ to $$$mathtt{0}$$$. Then, on each of the next $$$k$$$ lines, output a balanced bracket sequence of length $$$2n$$$ representing the operations needed to change all elements of $$$s$$$ to $$$0$$$s. If there are multiple ways to change all elements of $$$s$$$ to $$$mathtt{0}$$$ that require not more than $$$10$$$ operations, you can output any of them. Example Input 4 1 01 2 0000 3 100111 4 01011100 Output -1 2 ()() ()() 1 (())() 2 (((()))) ()()(()) Note In the first test case, it can be proven that it is impossible to change all elements of $$$s$$$ to $$$mathtt{0}$$$. In the second test case, the first operation using the bracket sequence $$$b = mathtt{()()}$$$ will convert the binary string $$$s=mathtt{0000}$$$ to $$$s=mathtt{1111}$$$. Then, the second operation using the same bracket sequence $$$b = mathtt{()()}$$$ will convert the binary string $$$s=mathtt{1111}$$$ back to $$$s=mathtt{0000}$$$. Note that since all elements of $$$s$$$ is already $$$mathtt{0}$$$ initially, using $$$0$$$ operations is also a valid answer. In the third test case, a single operation using the bracket sequence $$$b = mathtt{(())()}$$$ will change all elements of $$$s$$$ to $$$mathtt{0}$$$. The operation will happen as follows. 1. $$$b_1$$$ is an open bracket and $$$p_1 = 4$$$ since $$$b[1,4]=mathtt{(())}$$$ is a balanced bracket sequence. Hence, we do a range toggle operation from $$$1$$$ to $$$4$$$ on the binary string $$$s = mathtt{100111}$$$ to obtain $$$s = mathtt{011011}$$$. 2. $$$b_2$$$ is an open bracket and $$$p_2 = 3$$$ since $$$b[2,3]=mathtt{()}$$$ is a balanced bracket sequence. Hence, we do a range toggle operation from $$$2$$$ to $$$3$$$ on the binary string $$$s = mathtt{011011}$$$ to obtain $$$s = mathtt{000011}$$$. 3. $$$b_3$$$ is not an open bracket, so no range toggle operation is done at this step. 4. $$$b_4$$$ is not an open bracket, so no range toggle operation is done at this step. 5. $$$b_5$$$ is an open bracket and $$$p_5 = 6$$$ since $$$b[5,6]=mathtt{()}$$$ is a balanced bracket sequence. Hence, we do a range toggle operation from $$$5$$$ to $$$6$$$ on the binary string $$$s = mathtt{000011}$$$ to obtain $$$s = mathtt{000000}$$$. 6. $$$b_6$$$ is not an open bracket, so no range toggle operation is done at this step. In the fourth test case, the first operation using the bracket sequence $$$b = mathtt{(((())))}$$$ will convert the binary string $$$s = mathtt{01011100}$$$ to $$$s = mathtt{11111001}$$$. Then, the second operation using the bracket sequence $$$b = mathtt{()()(())}$$$ will convert the binary string $$$s = mathtt{11111001}$$$ to $$$s=mathtt{00000000}$$$.
2,600
true
true
true
false
false
true
false
false
false
false
932
1886D
Monocarp has $$$n$$$ numbers $$$1, 2, dots, n$$$ and a set (initially empty). He adds his numbers to this set $$$n$$$ times in some order. During each step, he adds a new number (which has not been present in the set before). In other words, the sequence of added numbers is a permutation of length $$$n$$$. Every time Monocarp adds an element into the set except for the first time, he writes out a character: if the element Monocarp is trying to insert becomes the maximum element in the set, Monocarp writes out the character >; if the element Monocarp is trying to insert becomes the minimum element in the set, Monocarp writes out the character <; if none of the above, Monocarp writes out the character ?. You are given a string $$$s$$$ of $$$n-1$$$ characters, which represents the characters written out by Monocarp (in the order he wrote them out). You have to process $$$m$$$ queries to the string. Each query has the following format: $$$i$$$ $$$c$$$ — replace $$$s_i$$$ with the character $$$c$$$. Both before processing the queries and after each query, you have to calculate the number of different ways to order the integers $$$1, 2, 3, dots, n$$$ such that, if Monocarp inserts the integers into the set in that order, he gets the string $$$s$$$. Since the answers might be large, print them modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 3 cdot 10^5$$$; $$$1 le m le 3 cdot 10^5$$$). The second line contains the string $$$s$$$, consisting of exactly $$$n-1$$$ characters <, > and/or ?. Then $$$m$$$ lines follow. Each of them represents a query. Each line contains an integer $$$i$$$ and a character $$$c$$$ ($$$1 le i le n-1$$$; $$$c$$$ is either <, >, or ?). Output Both before processing the queries and after each query, print one integer — the number of ways to order the integers $$$1, 2, 3, dots, n$$$ such that, if Monocarp inserts the integers into the set in that order, he gets the string $$$s$$$. Since the answers might be large, print them modulo $$$998244353$$$. Examples Input 6 4 <?>?> 1 ? 4 < 5 < 1 > Note In the first example, there are three possible orderings before all queries: $$$3, 1, 2, 5, 4, 6$$$; $$$4, 1, 2, 5, 3, 6$$$; $$$4, 1, 3, 5, 2, 6$$$. After the last query, there is only one possible ordering: $$$3, 5, 4, 6, 2, 1$$$.
2,100
true
false
false
false
true
false
false
false
false
false
974
2008D
For a certain permutation $$$p$$$$$$^{ ext{∗}}$$$ Sakurako calls an integer $$$j$$$ reachable from an integer $$$i$$$ if it is possible to make $$$i$$$ equal to $$$j$$$ by assigning $$$i=p_i$$$ a certain number of times. If $$$p=[3,5,6,1,2,4]$$$, then, for example, $$$4$$$ is reachable from $$$1$$$, because: $$$i=1$$$ $$$ ightarrow$$$ $$$i=p_1=3$$$ $$$ ightarrow$$$ $$$i=p_3=6$$$ $$$ ightarrow$$$ $$$i=p_6=4$$$. Now $$$i=4$$$, so $$$4$$$ is reachable from $$$1$$$. Each number in the permutation is colored either black or white. Sakurako defines the function $$$F(i)$$$ as the number of black integers that are reachable from $$$i$$$. Sakurako is interested in $$$F(i)$$$ for each $$$1le ile n$$$, but calculating all values becomes very difficult, so she asks you, as her good friend, to compute this. $$$^{ ext{∗}}$$$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 (the number $$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$, but the array contains $$$4$$$). Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$) xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$) xa0— the number of elements in the array. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1le p_ile n$$$) xa0— the elements of the permutation. The third line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of '0' and '1'. If $$$s_i=0$$$, then the number $$$p_i$$$ is colored black; if $$$s_i=1$$$, then the number $$$p_i$$$ is colored white. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output $$$n$$$ integers $$$F(1), F(2), dots, F(n)$$$.
1,100
true
false
false
true
false
false
false
false
false
true
205
2026D
Problem - 2026D - 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 dp implementation math *1900 No tag edit access → Contest materials $$$ be the sum of elements from $$$a_l$$$ to $$$a_r$$$ (i.u2009e. $$$s(l,r) = sumlimits_{i=l}^{r} a_i$$$). Let's construct another sequence $$$b$$$ of size $$$frac{n(n+1)}{2}$$$ as follows: $$$b =
1,900
true
false
true
true
true
false
false
true
false
false
113
1704H2
This is the hard version of this problem. The difference between easy and hard versions is the constraint on $$$k$$$ and the time limit. Notice that you need to calculate the answer for all positive integers $$$n in [1,k]$$$ in this version. You can make hacks only if both versions of the problem are solved. Cirno is playing a war simulator game with $$$n$$$ towers (numbered from $$$1$$$ to $$$n$$$) and $$$n$$$ bots (numbered from $$$1$$$ to $$$n$$$). The $$$i$$$-th tower is initially occupied by the $$$i$$$-th bot for $$$1 le i le n$$$. Before the game, Cirno first chooses a permutation $$$p = [p_1, p_2, ldots, p_n]$$$ of length $$$n$$$ (A permutation of length $$$n$$$ is an array of length $$$n$$$ where each integer between $$$1$$$ and $$$n$$$ appears exactly once). After that, she can choose a sequence $$$a = [a_1, a_2, ldots, a_n]$$$ ($$$1 le a_i le n$$$ and $$$a_i e i$$$ for all $$$1 le i le n$$$). The game has $$$n$$$ rounds of attacks. In the $$$i$$$-th round, if the $$$p_i$$$-th bot is still in the game, it will begin its attack, and as the result the $$$a_{p_i}$$$-th tower becomes occupied by the $$$p_i$$$-th bot; the bot that previously occupied the $$$a_{p_i}$$$-th tower will no longer occupy it. If the $$$p_i$$$-th bot is not in the game, nothing will happen in this round. After each round, if a bot doesn't occupy any towers, it will be eliminated and leave the game. Please note that no tower can be occupied by more than one bot, but one bot can occupy more than one tower during the game. At the end of the game, Cirno will record the result as a sequence $$$b = [b_1, b_2, ldots, b_n]$$$, where $$$b_i$$$ is the number of the bot that occupies the $$$i$$$-th tower at the end of the game. However, as a mathematics master, she wants you to solve the following counting problem instead of playing games: Count the number of different pairs of sequences $$$a$$$, $$$b$$$ from all possible choices of sequence $$$a$$$ and permutation $$$p$$$. Calculate the answers for all $$$n$$$ such that $$$1 le n le k$$$. Since these numbers may be large, output them modulo $$$M$$$. Note For $$$n=1$$$, no valid sequence $$$a$$$ exists. We regard the answer as $$$0$$$. For $$$n=2$$$, there is only one possible array $$$a$$$: $$$[2, 1]$$$. For array $$$a$$$ is $$$[2, 1]$$$ and permutation $$$p$$$ is $$$[1, 2]$$$, the sequence $$$b$$$ will be $$$[1, 1]$$$ after all rounds have finished. The details for each rounds: In the first round, the first bot will begin its attack and successfully capture the tower $$$2$$$. After this round, the second bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the second bot is not in the game. For array $$$a$$$ is $$$[2, 1]$$$ and permutation $$$p$$$ is $$$[2, 1]$$$, the sequence $$$b$$$ will be $$$[2, 2]$$$ after all rounds have finished. The details for each rounds: In the first round, the second bot will begin its attack and successfully capture the tower $$$1$$$. After this round, the first bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the first bot is not in the game. So the number of different pairs of sequences $$$(a,b)$$$ is $$$2$$$ ($$$[2, 1]$$$, $$$[1, 1]$$$ and $$$[2, 1]$$$, $$$[2, 2]$$$) for $$$n=2$$$.
3,500
true
false
false
false
false
false
false
false
false
false
2,047
1130C
Alice lives on a flat planet that can be modeled as a square grid of size $$$n imes n$$$, with rows and columns enumerated from $$$1$$$ to $$$n$$$. We represent the cell at the intersection of row $$$r$$$ and column $$$c$$$ with ordered pair $$$(r, c)$$$. Each cell in the grid is either land or water. An example planet with $$$n = 5$$$. It also appears in the first sample test. Alice resides in land cell $$$(r_1, c_1)$$$. She wishes to travel to land cell $$$(r_2, c_2)$$$. At any moment, she may move to one of the cells adjacent to where she is—in one of the four directions (i.e., up, down, left, or right). Unfortunately, Alice cannot swim, and there is no viable transportation means other than by foot (i.e., she can walk only on land). As a result, Alice's trip may be impossible. To help Alice, you plan to create at most one tunnel between some two land cells. The tunnel will allow Alice to freely travel between the two endpoints. Indeed, creating a tunnel is a lot of effort: the cost of creating a tunnel between cells $$$(r_s, c_s)$$$ and $$$(r_t, c_t)$$$ is $$$(r_s-r_t)^2 + (c_s-c_t)^2$$$. For now, your task is to find the minimum possible cost of creating at most one tunnel so that Alice could travel from $$$(r_1, c_1)$$$ to $$$(r_2, c_2)$$$. If no tunnel needs to be created, the cost is $$$0$$$. Input The first line contains one integer $$$n$$$ ($$$1 leq n leq 50$$$) — the width of the square grid. The second line contains two space-separated integers $$$r_1$$$ and $$$c_1$$$ ($$$1 leq r_1, c_1 leq n$$$) — denoting the cell where Alice resides. The third line contains two space-separated integers $$$r_2$$$ and $$$c_2$$$ ($$$1 leq r_2, c_2 leq n$$$) — denoting the cell to which Alice wishes to travel. Each of the following $$$n$$$ lines contains a string of $$$n$$$ characters. The $$$j$$$-th character of the $$$i$$$-th such line ($$$1 leq i, j leq n$$$) is 0 if $$$(i, j)$$$ is land or 1 if $$$(i, j)$$$ is water. It is guaranteed that $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are land. Output Print an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from $$$(r_1, c_1)$$$ to $$$(r_2, c_2)$$$. Examples Input 5 1 1 5 5 00001 11111 00111 00110 00110 Input 3 1 3 3 1 010 101 010 Note In the first sample, a tunnel between cells $$$(1, 4)$$$ and $$$(4, 5)$$$ should be created. The cost of doing so is $$$(1-4)^2 + (4-5)^2 = 10$$$, which is optimal. This way, Alice could walk from $$$(1, 1)$$$ to $$$(1, 4)$$$, use the tunnel from $$$(1, 4)$$$ to $$$(4, 5)$$$, and lastly walk from $$$(4, 5)$$$ to $$$(5, 5)$$$. In the second sample, clearly a tunnel between cells $$$(1, 3)$$$ and $$$(3, 1)$$$ needs to be created. The cost of doing so is $$$(1-3)^2 + (3-1)^2 = 8$$$.
1,400
false
false
false
false
false
false
true
false
false
false
5,069
466D
Problem - 466D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp *2100 No tag edit access → Contest materials ") ") . At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments
2,100
false
false
false
true
false
false
false
false
false
false
7,974
526C
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs _W__r_ grams and each blue candy weighs _W__b_ grams. Eating a single red candy gives Om Nom _H__r_ joy units and eating a single blue candy gives Om Nom _H__b_ joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than _C_ grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. Input The single line contains five integers _C_,u2009_H__r_,u2009_H__b_,u2009_W__r_,u2009_W__b_ (1u2009≤u2009_C_,u2009_H__r_,u2009_H__b_,u2009_W__r_,u2009_W__b_u2009≤u2009109). Output Print a single integer — the maximum number of joy units that Om Nom can get. Note In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
2,000
true
true
false
false
false
false
true
false
false
false
7,729
202A
This problem's actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline. You are given string _s_ consisting of lowercase English letters only. Find its lexicographically largest palindromic subsequence. We'll call a non-empty string _s_[_p_1_p_2... _p__k_] = _s__p_1_s__p_2... _s__p__k_ (1 u2009≤u2009 _p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__k_ u2009≤u2009 _s_) a subsequence of string _s_ = _s_1_s_2... _s__s_, where _s_ is the length of string _s_. For example, strings "abcb", "b" and "abacaba" are subsequences of string "abacaba". String _x_ = _x_1_x_2... _x__x_ is lexicographically larger than string _y_ = _y_1_y_2... _y__y_ if either _x_ > _y_ and _x_1u2009=u2009_y_1, _x_2u2009=u2009_y_2, ...,u2009_x__y_u2009=u2009_y__y_, or there exists such number _r_ (_r_u2009<u2009_x_, _r_u2009<u2009_y_) that _x_1u2009=u2009_y_1, _x_2u2009=u2009_y_2, ..., _x__r_u2009=u2009_y__r_ and _x__r_u2009u2009+u2009u20091u2009>u2009_y__r_u2009u2009+u2009u20091. Characters in the strings are compared according to their ASCII codes. For example, string "ranger" is lexicographically larger than string "racecar" and string "poster" is lexicographically larger than string "post". String _s_ = _s_1_s_2... _s__s_ is a palindrome if it matches string _rev_(_s_) = _s__s__s__s_u2009-u20091... _s_1. In other words, a string is a palindrome if it reads the same way from left to right and from right to left. For example, palindromic strings are "racecar", "refer" and "z". Note Among all distinct subsequences of string "radar" the following ones are palindromes: "a", "d", "r", "aa", "rr", "ada", "rar", "rdr", "raar" and "radar". The lexicographically largest of them is "rr".
800
false
true
true
false
false
false
true
true
false
false
9,034
911A
Problem - 911A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *1100 No tag edit access → Contest materials minimums in it. It is guaranteed that in the array a minimum occurs at least two times. Input The first line contains positive integer _n_ (2u2009≤u2009_n_u2009≤u2009105) — size of the given array. The second line contains _n_ integers _a_0,u2009_a_1,u2009...,u2009_a__n_u2009-u20091 (1u2009≤u2009_a__i_u2009≤u2009109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times. Output Print the only number — distance between two nearest minimums in the array. Examples Input 2 3 3 Output 1 Input 3 5 6 5 Output 2 Input 9 2 1 3 5 4 1 2 3 1 Output 3
1,100
false
false
true
false
false
false
false
false
false
false
6,103
1540A
Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction. Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0— the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, ldots, d_n$$$ ($$$0 leq d_i leq 10^9$$$)xa0— the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. Example Input 3 3 0 2 3 2 0 1000000000 1 0 Note In the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$. In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$. In the third test case, you can't add any roads. The total cost is $$$0$$$.
1,400
false
true
false
false
false
true
false
false
true
true
2,942
614A
Problem - 614A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation *1500 No tag edit access → Contest materials ") Given integers _l_, _r_ and _k_, you need to print all powers of number _k_ within range from _l_ to _r_ inclusive. However, Rostislav doesn't want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him! Input The first line of the input contains three space-separated integers _l_, _r_ and _k_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u20091018, 2u2009≤u2009_k_u2009≤u2009109). Output Print all powers of number _k_, that lie within range from _l_ to _r_ in the increasing order. If there are no such numbers, print "-1" (without the quotes). Examples Input 1 10 2 Output 1 2 4 8 Input 2 4 5 Output -1 Note Note to the first sample: numbers 20u2009=u20091, 21u2009=u20092, 22u2009=u20094, 23u2009=u20098 lie within the specified range. The number 24u2009=u200916 is greater then 10, thus it shouldn't be printed.
1,500
false
false
true
false
false
false
true
false
false
false
7,382
1628D2
This is the hard version of the problem. The difference is the constraints on $$$n$$$, $$$m$$$ and $$$t$$$. You can make hacks only if all versions of the problem are solved. Alice and Bob are given the numbers $$$n$$$, $$$m$$$ and $$$k$$$, and play a game as follows: The game has a score that Alice tries to maximize, and Bob tries to minimize. The score is initially $$$0$$$. The game consists of $$$n$$$ turns. Each turn, Alice picks a real number from $$$0$$$ to $$$k$$$ (inclusive) which Bob either adds to or subtracts from the score of the game. But throughout the game, Bob has to choose to add at least $$$m$$$ out of the $$$n$$$ turns. Bob gets to know which number Alice picked before deciding whether to add or subtract the number from the score, and Alice gets to know whether Bob added or subtracted the number for the previous turn before picking the number for the current turn (except on the first turn since there was no previous turn). If Alice and Bob play optimally, what will the final score of the game be? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. The description of test cases follows. Each test case consists of a single line containing the three integers, $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 le m le n le 10^6, 0 le k < 10^9 + 7$$$) — the number of turns, how many of those turns Bob has to add, and the biggest number Alice can choose, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case output a single integer number — the score of the optimal game modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Example Input 7 3 3 2 2 1 10 6 3 10 6 4 10 100 1 1 4 4 0 69 4 20 Output 6 5 375000012 500000026 958557139 0 49735962 Note In the first test case, the entire game has $$$3$$$ turns, and since $$$m = 3$$$, Bob has to add in each of them. Therefore Alice should pick the biggest number she can, which is $$$k = 2$$$, every turn. In the third test case, Alice has a strategy to guarantee a score of $$$frac{75}{8} equiv 375000012 pmod{10^9 + 7}$$$. In the fourth test case, Alice has a strategy to guarantee a score of $$$frac{45}{2} equiv 500000026 pmod{10^9 + 7}$$$.
2,400
true
false
false
true
false
false
false
false
false
false
2,471
436C
During the loading of the game "Dungeons and Candies" you are required to get descriptions of _k_ levels from the server. Each description is a map of an _n_u2009×u2009_m_ checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as "." on the map, but if a cell has a candy, it is denoted as a letter of the English alphabet. A level may contain identical candies, in this case the letters in the corresponding cells of the map will be the same. When you transmit information via a network, you want to minimize traffic — the total size of the transferred data. The levels can be transmitted in any order. There are two ways to transmit the current level _A_: 1. You can transmit the whole level _A_. Then you need to transmit _n_·_m_ bytes via the network. 2. You can transmit the difference between level _A_ and some previously transmitted level _B_ (if it exists); this operation requires to transmit _d__A_,u2009_B_·_w_ bytes, where _d__A_,u2009_B_ is the number of cells of the field that are different for _A_ and _B_, and _w_ is a constant. Note, that you should compare only the corresponding cells of levels _A_ and _B_ to calculate _d__A_,u2009_B_. You cannot transform the maps of levels, i.e. rotate or shift them relatively to each other. Your task is to find a way to transfer all the _k_ levels and minimize the traffic. Input The first line contains four integers _n_,u2009_m_,u2009_k_,u2009_w_ (1u2009≤u2009_n_,u2009_m_u2009≤u200910;xa01u2009≤u2009_k_,u2009_w_u2009≤u20091000). Then follows the description of _k_ levels. Each level is described by _n_ lines, each line contains _m_ characters. Each character is either a letter of the English alphabet or a dot ("."). Please note that the case of the letters matters. Output In the first line print the required minimum number of transferred bytes. Then print _k_ pairs of integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2,u2009...,u2009_x__k_,u2009_y__k_, describing the way to transfer levels. Pair _x__i_, _y__i_ means that level _x__i_ needs to be transferred by way _y__i_. If _y__i_ equals 0, that means that the level must be transferred using the first way, otherwise _y__i_ must be equal to the number of a previously transferred level. It means that you will transfer the difference between levels _y__i_ and _x__i_ to transfer level _x__i_. Print the pairs in the order of transferring levels. The levels are numbered 1 through _k_ in the order they follow in the input. If there are multiple optimal solutions, you can print any of them. Examples Input 2 3 3 2 A.A ... A.a ..C X.Y ... Input 1 3 5 2 ABA BBB BBA BAB ABB
1,800
false
true
false
false
false
false
false
false
false
true
8,095
1572C
You are given a $$$1$$$ by $$$n$$$ pixel image. The $$$i$$$-th pixel of the image has color $$$a_i$$$. For each color, the number of pixels of that color is at most $$$20$$$. You can perform the following operation, which works like the bucket tool in paint programs, on this image: pick a color — an integer from $$$1$$$ to $$$n$$$; choose a pixel in the image; for all pixels connected to the selected pixel, change their colors to the selected color (two pixels of the same color are considered connected if all the pixels between them have the same color as those two pixels). Compute the minimum number of operations needed to make all the pixels in the image have the same color. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3cdot10^3$$$) — the number of pixels in the image. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$) — the colors of the pixels in the image. Note: for each color, the number of pixels of that color is at most $$$20$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3cdot10^3$$$. Output For each test case, print one integer: the minimum number of operations needed to make all the pixels in the image have the same color. Example Input 3 5 1 2 3 2 1 4 1 1 2 2 5 1 2 1 4 2 Note In the first example, the optimal solution is to apply the operation on the third pixel changing its color to $$$2$$$ and then to apply the operation on any pixel that has color $$$2$$$ changing its color and the color of all pixels connected to it to $$$1$$$. The sequence of operations is then: $$$[1, 2, 3, 2, 1] o [1, 2, 2, 2, 1] o [1, 1, 1, 1, 1]$$$. In the second example, we can either change the $$$1$$$s to $$$2$$$s in one operation or change the $$$2$$$s to $$$1$$$s also in one operation. In the third example, one possible way to make all the pixels have the same color is to apply the operation on the first, third and the fourth pixel each time changing its color to $$$2$$$.
2,700
false
true
false
true
false
false
false
false
false
false
2,780
592D
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ superhero. Byteforces is a country that consists of _n_ cities, connected by _n_u2009-u20091 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go from any city to any other city using only the given roads. There are _m_ cities being attacked by humans. So Ari... we meant Super M have to immediately go to each of the cities being attacked to scare those bad humans. Super M can pass from one city to another only using the given roads. Moreover, passing through one road takes her exactly one kron - the time unit used in Byteforces. However, Super M is not on Byteforces now - she is attending a training camp located in a nearby country Codeforces. Fortunately, there is a special device in Codeforces that allows her to instantly teleport from Codeforces to any city of Byteforces. The way back is too long, so for the purpose of this problem teleportation is used exactly once. You are to help Super M, by calculating the city in which she should teleport at the beginning in order to end her job in the minimum time (measured in krons). Also, provide her with this time so she can plan her way back to Codeforces. Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_m_u2009≤u2009_n_u2009≤u2009123456) - the number of cities in Byteforces, and the number of cities being attacked respectively. Then follow _n_u2009-u20091 lines, describing the road system. Each line contains two city numbers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_) - the ends of the road _i_. The last line contains _m_ distinct integers - numbers of cities being attacked. These numbers are given in no particular order. Output First print the number of the city Super M should teleport to. If there are many possible optimal answers, print the one with the lowest city number. Then print the minimum possible time needed to scare all humans in cities being attacked, measured in Krons. Note that the correct answer is always unique. Examples Input 7 2 1 2 1 3 1 4 3 5 3 6 3 7 2 7 Input 6 4 1 2 2 3 2 4 4 5 4 6 2 4 5 6 Note In the first sample, there are two possibilities to finish the Super M's job in 3 krons. They are: and . However, you should choose the first one as it starts in the city with the lower number.
2,200
false
false
false
true
false
false
false
false
false
true
7,479
1851E
Alchemist Nastya loves mixing potions. There are a total of $$$n$$$ types of potions, and one potion of type $$$i$$$ can be bought for $$$c_i$$$ coins. Any kind of potions can be obtained in no more than one way, by mixing from several others. The potions used in the mixing process will be consumed. Moreover, no potion can be obtained from itself through one or more mixing processes. As an experienced alchemist, Nastya has an unlimited supply of $$$k$$$ types of potions $$$p_1, p_2, dots, p_k$$$, but she doesn't know which one she wants to obtain next. To decide, she asks you to find, for each $$$1 le i le n$$$, the minimum number of coins she needs to spend to obtain a potion of type $$$i$$$ next. Input The first line of each test contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Each test case is described as follows: The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k < n le 2 cdot 10^5$$$)xa0— the total number of potion types and the number of potion types Nastya already has. The second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le 10^9$$$)xa0— the costs of buying the potions. The third line contains $$$k$$$ distinct integers $$$p_1, p_2, dots, p_k$$$ ($$$1 le p_i le n$$$)xa0— the indices of potions Nastya already has an unlimited supply of. This is followed by $$$n$$$ lines describing ways to obtain potions by mixing. Each line starts with the integer $$$m_i$$$ ($$$0 le m_i < n$$$)xa0— the number of potions required to mix a potion of the type $$$i$$$ ($$$1 le i le n$$$). Then line contains $$$m_i$$$ distinct integers $$$e_1, e_2, dots, e_{m_i}$$$ ($$$1 le e_j le n$$$, $$$e_j e i$$$)xa0— the indices of potions needed to mix a potion of the type $$$i$$$. If this list is empty, then a potion of the type $$$i$$$ can only be bought. It is guaranteed that no potion can be obtained from itself through one or more mixing processes. It is guaranteed that the sum of all $$$n$$$ values across all test cases does not exceed $$$2 cdot 10^5$$$. Similarly, it is guaranteed that the sum of all $$$m_i$$$ values across all test cases does not exceed $$$2 cdot 10^5$$$.
1,500
false
false
false
true
false
false
false
false
true
true
1,170
1046C
Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race. Given the current ranking in the competition and points distribution for the next race, your task is to calculate the best possible ranking for a given astronaut after the next race. It's guaranteed that given astronaut will have unique number of points before the race. Input The first line contains two integer numbers $$$N$$$ ($$$1 leq N leq 200000$$$) representing number of F1 astronauts, and current position of astronaut $$$D$$$ ($$$1 leq D leq N$$$) you want to calculate best ranking for (no other competitor will have the same number of points before the race). The second line contains $$$N$$$ integer numbers $$$S_k$$$ ($$$0 leq S_k leq 10^8$$$, $$$k=1...N$$$), separated by a single space, representing current ranking of astronauts. Points are sorted in non-increasing order. The third line contains $$$N$$$ integer numbers $$$P_k$$$ ($$$0 leq P_k leq 10^8$$$, $$$k=1...N$$$), separated by a single space, representing point awards for the next race. Points are sorted in non-increasing order, so winner of the race gets the maximum number of points. Output Output contains one integer number — the best possible ranking for astronaut after the race. If multiple astronauts have the same score after the race, they all share the best ranking. Example Input 4 3 50 30 20 10 15 10 7 3 Note If the third ranked astronaut wins the race, he will have 35 points. He cannot take the leading position, but he can overtake the second position if the second ranked astronaut finishes the race at the last position.
1,400
false
true
false
false
false
false
false
false
false
false
5,460
1157B
You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$. You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result. What is the maximum possible number you can obtain applying this operation no more than once? Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 le f(i) le 9$$$). Output Print the maximum number you can get after applying the operation described in the statement no more than once. Examples Input 4 1337 1 2 5 4 6 6 3 1 9 Input 5 11111 9 8 7 6 5 4 3 2 1 Input 2 33 1 1 1 1 1 1 1 1 1
1,300
false
true
false
false
false
false
false
false
false
false
4,922
1469B
Monocarp had a sequence $$$a$$$ consisting of $$$n + m$$$ integers $$$a_1, a_2, dots, a_{n + m}$$$. He painted the elements into two colors, red and blue; $$$n$$$ elements were painted red, all other $$$m$$$ elements were painted blue. After painting the elements, he has written two sequences $$$r_1, r_2, dots, r_n$$$ and $$$b_1, b_2, dots, b_m$$$. The sequence $$$r$$$ consisted of all red elements of $$$a$$$ in the order they appeared in $$$a$$$; similarly, the sequence $$$b$$$ consisted of all blue elements of $$$a$$$ in the order they appeared in $$$a$$$ as well. Unfortunately, the original sequence was lost, and Monocarp only has the sequences $$$r$$$ and $$$b$$$. He wants to restore the original sequence. In case there are multiple ways to restore it, he wants to choose a way to restore that maximizes the value of $$$$$$f(a) = max(0, a_1, (a_1 + a_2), (a_1 + a_2 + a_3), dots, (a_1 + a_2 + a_3 + dots + a_{n + m}))$$$$$$ Help Monocarp to calculate the maximum possible value of $$$f(a)$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Then the test cases follow. Each test case consists of four lines. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 100$$$). The second line contains $$$n$$$ integers $$$r_1, r_2, dots, r_n$$$ ($$$-100 le r_i le 100$$$). The third line contains one integer $$$m$$$ ($$$1 le m le 100$$$). The fourth line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$-100 le b_i le 100$$$). Output For each test case, print one integer — the maximum possible value of $$$f(a)$$$. Example Input 4 4 6 -5 7 -3 3 2 3 -4 2 1 1 4 10 -3 2 2 5 -1 -2 -3 -4 -5 5 -1 -2 -3 -4 -5 1 0 1 0 Note In the explanations for the sample test cases, red elements are marked as bold. In the first test case, one of the possible sequences $$$a$$$ is $$$[mathbf{6}, 2, mathbf{-5}, 3, mathbf{7}, mathbf{-3}, -4]$$$. In the second test case, one of the possible sequences $$$a$$$ is $$$[10, mathbf{1}, -3, mathbf{1}, 2, 2]$$$. In the third test case, one of the possible sequences $$$a$$$ is $$$[mathbf{-1}, -1, -2, -3, mathbf{-2}, -4, -5, mathbf{-3}, mathbf{-4}, mathbf{-5}]$$$. In the fourth test case, one of the possible sequences $$$a$$$ is $$$[0, mathbf{0}]$$$.
1,000
false
true
false
true
false
false
false
false
false
false
3,337
1142A
Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through $$$n cdot k$$$ cities. The cities are numerated from $$$1$$$ to $$$n cdot k$$$, the distance between the neighboring cities is exactly $$$1$$$ km. Sergey does not like beetles, he loves burgers. Fortunately for him, there are $$$n$$$ fast food restaurants on the circle, they are located in the $$$1$$$-st, the $$$(k + 1)$$$-st, the $$$(2k + 1)$$$-st, and so on, the $$$((n-1)k + 1)$$$-st cities, i.e. the distance between the neighboring cities with fast food restaurants is $$$k$$$ km. Sergey began his journey at some city $$$s$$$ and traveled along the circle, making stops at cities each $$$l$$$ km ($$$l > 0$$$), until he stopped in $$$s$$$ once again. Sergey then forgot numbers $$$s$$$ and $$$l$$$, but he remembers that the distance from the city $$$s$$$ to the nearest fast food restaurant was $$$a$$$ km, and the distance from the city he stopped at after traveling the first $$$l$$$ km from $$$s$$$ to the nearest fast food restaurant was $$$b$$$ km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions. Now Sergey is interested in two integers. The first integer $$$x$$$ is the minimum number of stops (excluding the first) Sergey could have done before returning to $$$s$$$. The second integer $$$y$$$ is the maximum number of stops (excluding the first) Sergey could have done before returning to $$$s$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 100,000$$$)xa0— the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively. The second line contains two integers $$$a$$$ and $$$b$$$ ($$$0 le a, b le frac{k}{2}$$$)xa0— the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively. Output Print the two integers $$$x$$$ and $$$y$$$. Note In the first example the restaurants are located in the cities $$$1$$$ and $$$4$$$, the initial city $$$s$$$ could be $$$2$$$, $$$3$$$, $$$5$$$, or $$$6$$$. The next city Sergey stopped at could also be at cities $$$2, 3, 5, 6$$$. Let's loop through all possible combinations of these cities. If both $$$s$$$ and the city of the first stop are at the city $$$2$$$ (for example, $$$l = 6$$$), then Sergey is at $$$s$$$ after the first stop already, so $$$x = 1$$$. In other pairs Sergey needs $$$1, 2, 3$$$, or $$$6$$$ stops to return to $$$s$$$, so $$$y = 6$$$. In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so $$$l$$$ is $$$2$$$, $$$4$$$, or $$$6$$$. Thus $$$x = 1$$$, $$$y = 3$$$. In the third example there is only one restaurant, so the possible locations of $$$s$$$ and the first stop are: $$$(6, 8)$$$ and $$$(6, 4)$$$. For the first option $$$l = 2$$$, for the second $$$l = 8$$$. In both cases Sergey needs $$$x=y=5$$$ stops to go to $$$s$$$.
1,700
true
false
false
false
false
false
true
false
false
false
5,013
1328F
You are given the array $$$a$$$ consisting of $$$n$$$ elements and the integer $$$k le n$$$. You want to obtain at least $$$k$$$ equal elements in the array $$$a$$$. In one move, you can make one of the following two operations: Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of $$$a$$$ is $$$mn$$$ then you choose such index $$$i$$$ that $$$a_i = mn$$$ and set $$$a_i := a_i + 1$$$); take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of $$$a$$$ is $$$mx$$$ then you choose such index $$$i$$$ that $$$a_i = mx$$$ and set $$$a_i := a_i - 1$$$). Your task is to calculate the minimum number of moves required to obtain at least $$$k$$$ equal elements in the array. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$) — the number of elements in $$$a$$$ and the required number of equal elements. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Output Print one integer — the minimum number of moves required to obtain at least $$$k$$$ equal elements in the array.
2,200
false
true
false
false
false
false
false
false
false
false
4,066
369B
Valera loves to participate in competitions. Especially in programming contests. Today he has participated in the contest with his team, consisting of _n_ students (including Valera). This contest was an individual competition, so each student in the team solved problems individually. After the contest was over, Valera was interested in results. He found out that: each student in the team scored at least _l_ points and at most _r_ points; in total, all members of the team scored exactly _s__all_ points; the total score of the _k_ members of the team who scored the most points is equal to exactly _s__k_; more formally, if _a_1,u2009_a_2,u2009...,u2009_a__n_ is the sequence of points earned by the team of students in the non-increasing order (_a_1u2009≥u2009_a_2u2009≥u2009...u2009≥u2009_a__n_), then _s__k_u2009=u2009_a_1u2009+u2009_a_2u2009+u2009...u2009+u2009_a__k_. However, Valera did not find out exactly how many points each of _n_ students scored. Valera asked you to recover any distribution of scores between the students of the team, such that all the conditions above are met. Input The first line of the input contains exactly six integers _n_,u2009_k_,u2009_l_,u2009_r_,u2009_s__all_,u2009_s__k_ (1u2009≤u2009_n_,u2009_k_,u2009_l_,u2009_r_u2009≤u20091000; _l_u2009≤u2009_r_; _k_u2009≤u2009_n_; 1u2009≤u2009_s__k_u2009≤u2009_s__all_u2009≤u2009106). It's guaranteed that the input is such that the answer exists. Output Print exactly _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ — the number of points each student scored. If there are multiple solutions, you can print any of them. You can print the distribution of points in any order.
1,400
true
false
true
false
false
true
false
false
false
false
8,357
1562A
Problem - 1562A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy math *800 No tag edit access → Contest materials ") ") $$$ of integers for which $$$rge a ge b ge l$$$. As a reminder, $$$a bmod b$$$ is a remainder we get when dividing $$$a$$$ by $$$b$$$. For example, $$$26 bmod 8 = 2$$$. Input Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ $$$(1le tle 10^4)$$$, denoting the number of test cases. Description of the test cases follows. The only line of each test case contains two integers $$$l$$$, $$$r$$$ ($$$1le l le r le 10^9$$$). Output For every test case, output the largest possible value of $$$a bmod b$$$ over all pairs $$$(a, b)$$$ of integers for which $$$rge a ge b ge l$$$. Example Input 4 1 1 999999999 1000000000 8 26 1 999999999 Output 0 1 12 499999999 Note In the first test case, the only allowed pair is $$$(a, b) = (1, 1)$$$, for which $$$a bmod b = 1 bmod 1 = 0$$$. In the second test case, the optimal choice is pair $$$(a, b) = (1000000000, 999999999)$$$, for which $$$a bmod b = 1$$$.
800
true
true
false
false
false
false
false
false
false
false
2,820
350B
Valera's finally decided to go on holiday! He packed up and headed for a ski resort. Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has _n_ objects (we will consider the objects indexed in some way by integers from 1 to _n_), each object is either a hotel or a mountain. Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object _v_, the resort has at most one object _u_, such that there is a ski track built from object _u_ to object _v_. We also know that no hotel has got a ski track leading from the hotel to some object. Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects _v_1,u2009_v_2,u2009...,u2009_v__k_ (_k_u2009≥u20091) and meet the following conditions: 1. Objects with numbers _v_1,u2009_v_2,u2009...,u2009_v__k_u2009-u20091 are mountains and the object with number _v__k_ is the hotel. 2. For any integer _i_ (1u2009≤u2009_i_u2009<u2009_k_), there is exactly one ski track leading from object _v__i_. This track goes to object _v__i_u2009+u20091. 3. The path contains as many objects as possible (_k_ is maximal). Help Valera. Find such path that meets all the criteria of our hero! Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of objects. The second line contains _n_ space-separated integers _type_1,u2009_type_2,u2009...,u2009_type__n_ — the types of the objects. If _type__i_ equals zero, then the _i_-th object is the mountain. If _type__i_ equals one, then the _i_-th object is the hotel. It is guaranteed that at least one object is a hotel. The third line of the input contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009_n_) — the description of the ski tracks. If number _a__i_ equals zero, then there is no such object _v_, that has a ski track built from _v_ to _i_. If number _a__i_ doesn't equal zero, that means that there is a track built from object _a__i_ to object _i_. Output In the first line print _k_ — the maximum possible path length for Valera. In the second line print _k_ integers _v_1,u2009_v_2,u2009...,u2009_v__k_ — the path. If there are multiple solutions, you can print any of them.
1,500
false
false
false
false
false
false
false
false
false
true
8,434
809A
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to _n_ computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis. Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to _n_. So the _i_-th hacked computer is located at the point _x__i_. Moreover the coordinates of all computers are distinct. Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task. Leha should calculate a sum of _F_(_a_) for all _a_, where _a_ is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote _A_ the set of all integers from 1 to _n_. Noora asks the hacker to find value of the expression . Here _F_(_a_) is calculated as the maximum among the distances between all pairs of computers from the set _a_. Formally, . Since the required sum can be quite large Noora asks to find it modulo 109u2009+u20097. Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date. Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u20093·105) denoting the number of hacked computers. The second line contains _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (1u2009≤u2009_x__i_u2009≤u2009109) denoting the coordinates of hacked computers. It is guaranteed that all _x__i_ are distinct.
1,500
true
false
true
false
false
false
false
false
true
false
6,528
698B
A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with _n_ vertices, numbered 1 through _n_. There are many ways to represent such a tree. One way is to create an array with _n_ integers _p_1,u2009_p_2,u2009...,u2009_p__n_, where _p__i_ denotes a parent of vertex _i_ (here, for convenience a root is considered its own parent). For this rooted tree the array _p_ is [2,u20093,u20093,u20092]. Given a sequence _p_1,u2009_p_2,u2009...,u2009_p__n_, one is able to restore a tree: 1. There must be exactly one index _r_ that _p__r_u2009=u2009_r_. A vertex _r_ is a root of the tree. 2. For all other _n_u2009-u20091 vertices _i_, there is an edge between vertex _i_ and vertex _p__i_. A sequence _p_1,u2009_p_2,u2009...,u2009_p__n_ is called valid if the described procedure generates some (any) rooted tree. For example, for _n_u2009=u20093 sequences (1,2,2), (2,3,1) and (2,1,3) are not valid. You are given a sequence _a_1,u2009_a_2,u2009...,u2009_a__n_, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them. Input The first line of the input contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of vertices in the tree. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_). Output In the first line print the minimum number of elements to change, in order to get a valid sequence. In the second line, print any valid sequence possible to get from (_a_1,u2009_a_2,u2009...,u2009_a__n_) in the minimum number of changes. If there are many such sequences, any of them will be accepted. Note In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (because _p_4u2009=u20094), which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right drawing below). On both drawings, roots are painted red. In the second sample, the given sequence is already valid.
1,700
false
false
false
false
false
true
false
false
false
true
7,032
929B
Statement is not available on English language В самолёте есть _n_ рядов мест. Если смотреть на ряды сверху, то в каждом ряду есть 3 места слева, затем проход между рядами, затем 4 центральных места, затем ещё один проход между рядами, а затем ещё 3 места справа. Известно, что некоторые места уже заняты пассажирами. Всего есть два вида пассажиров — статусные (те, которые часто летают) и обычные. Перед вами стоит задача рассадить ещё _k_ обычных пассажиров так, чтобы суммарное число соседей у статусных пассажиров было минимально возможным. Два пассажира считаются соседями, если они сидят в одном ряду и между ними нет других мест и прохода между рядами. Если пассажир является соседним пассажиром для двух статусных пассажиров, то его следует учитывать в сумме соседей дважды. Входные данные В первой строке следуют два целых числа _n_ и _k_ (1u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u200910·_n_) — количество рядов мест в самолёте и количество пассажиров, которых нужно рассадить. Далее следует описание рядов мест самолёта по одному ряду в строке. Если очередной символ равен '-', то это проход между рядами. Если очередной символ равен '.', то это свободное место. Если очередной символ равен 'S', то на текущем месте будет сидеть статусный пассажир. Если очередной символ равен 'P', то на текущем месте будет сидеть обычный пассажир. Гарантируется, что количество свободных мест не меньше _k_. Гарантируется, что все ряды удовлетворяют описанному в условии формату. Выходные данные В первую строку выведите минимальное суммарное число соседей у статусных пассажиров. Далее выведите план рассадки пассажиров, который минимизирует суммарное количество соседей у статусных пассажиров, в том же формате, что и во входных данных. Если в свободное место нужно посадить одного из _k_ пассажиров, выведите строчную букву 'x' вместо символа '.'. Примеры Входные данные 1 2 SP.-SS.S-S.S Выходные данные 5 SPx-SSxS-S.S Входные данные 4 9 PP.-PPPS-S.S PSP-PPSP-.S. .S.-S..P-SS. P.S-P.PP-PSP Выходные данные 15 PPx-PPPS-S.S PSP-PPSP-xSx xSx-SxxP-SSx P.S-PxPP-PSP Примечание В первом примере нужно посадить ещё двух обычных пассажиров. Для минимизации соседей у статусных пассажиров, нужно посадить первого из них на третье слева место, а второго на любое из оставшихся двух мест, так как независимо от выбора места он станет соседом двух статусных пассажиров. Изначально, у статусного пассажира, который сидит на самом левом месте уже есть сосед. Также на четвёртом и пятом местах слева сидят статусные пассажиры, являющиеся соседями друг для друга (что добавляет к сумме 2). Таким образом, после посадки ещё двух обычных пассажиров, итоговое суммарное количество соседей у статусных пассажиров станет равно пяти.
1,300
false
false
true
false
false
false
false
false
false
false
5,989
757C
It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has _n_ gyms. The _i_-th gym has _g__i_ Pokemon in it. There are _m_ distinct Pokemon types in the Himalayan region numbered from 1 to _m_. There is a special evolution camp set up in the fest which claims to evolve any Pokemon. The type of a Pokemon could change after evolving, subject to the constraint that if two Pokemon have the same type before evolving, they will have the same type after evolving. Also, if two Pokemon have different types before evolving, they will have different types after evolving. It is also possible that a Pokemon has the same type before and after evolving. Formally, an evolution plan is a permutation _f_ of {1,u20092,u2009...,u2009_m_}, such that _f_(_x_)u2009=u2009_y_ means that a Pokemon of type _x_ evolves into a Pokemon of type _y_. The gym leaders are intrigued by the special evolution camp and all of them plan to evolve their Pokemons. The protocol of the mountain states that in each gym, for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. They now want to find out how many distinct evolution plans exist which satisfy the protocol. Two evolution plans _f_1 and _f_2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon type in the two plans, i. e. there exists an _i_ such that _f_1(_i_)u2009≠u2009_f_2(_i_). Your task is to find how many distinct evolution plans are possible such that if all Pokemon in all the gyms are evolved, the number of Pokemon of each type in each of the gyms remains the same. As the answer can be large, output it modulo 109u2009+u20097. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_m_u2009≤u2009106)xa0— the number of gyms and the number of Pokemon types. The next _n_ lines contain the description of Pokemons in the gyms. The _i_-th of these lines begins with the integer _g__i_ (1u2009≤u2009_g__i_u2009≤u2009105)xa0— the number of Pokemon in the _i_-th gym. After that _g__i_ integers follow, denoting types of the Pokemons in the _i_-th gym. Each of these integers is between 1 and _m_. The total number of Pokemons (the sum of all _g__i_) does not exceed 5·105. Note In the first case, the only possible evolution plan is: In the second case, any permutation of (1,u2009u20092,u2009u20093) is valid. In the third case, there are two possible plans: In the fourth case, the only possible evolution plan is:
1,900
false
false
false
false
true
false
false
false
true
false
6,762
1144A
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent. Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed). You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No". Input The first line contains integer $$$n$$$ ($$$1 le n le 100$$$), denoting the number of strings to process. The following $$$n$$$ lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between $$$1$$$ and $$$100$$$, inclusive. Output Print $$$n$$$ lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable. Example Input 8 fced xyz r dabcef az aa bad babc Output Yes Yes Yes Yes No No No No
800
false
false
true
false
false
false
false
false
false
false
5,005
61E
Problem - 61E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures trees *1900 No tag edit access → Contest materials ") — the number of men in Roman army. Next line contains _n_ different positive integers _a__i_ (1u2009≤u2009_i_u2009≤u2009_n_,u20091u2009≤u2009_a__i_u2009≤u2009109) — powers of men in the Roman army. Output A single integer number, the weakness of the Roman army. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Examples Input 3 3 2 1 Output 1 Input 3 2 3 1 Output 0 Input 4 10 8 3 1 Output 4 Input 4 1 5 4 3 Output 1
1,900
false
false
false
false
true
false
false
false
false
false
9,666
1658D1
This is the easy version of the problem. The difference in the constraints between both versions is colored below in red. You can make hacks only if all versions of the problem are solved. Marin and Gojou are playing hide-and-seek with an array. Gojou initially performs the following steps: First, Gojou chooses $$$2$$$ integers $$$l$$$ and $$$r$$$ such that $$$l leq r$$$. Then, Gojou makes an array $$$a$$$ of length $$$r-l+1$$$ which is a permutation of the array $$$ — the number of test cases. In the first line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$ color{red}{boldsymbol{0} boldsymbol{=} boldsymbol{l}} le r < 2^{17}$$$). The second line contains $$$r - l + 1$$$ integers of $$$a_1,a_2,ldots,a_{r-l+1}$$$ ($$$0 le a_i < 2^{17}$$$). It is guaranteed that $$$a$$$ can be generated using the steps performed by Gojou. It is guaranteed that the sum of $$$r - l + 1$$$ over all test cases does not exceed $$$2^{17}$$$. Output For each test case print an integer $$$x$$$. If there are multiple answers, print any. Example Input 3 0 3 3 2 1 0 0 3 4 7 6 5 0 2 1 2 3 Note In the first test case, the original array is $$$[3, 2, 1, 0]$$$. In the second test case, the original array is $$$[0, 3, 2, 1]$$$. In the third test case, the original array is $$$[2, 1, 0]$$$.
1,600
true
false
false
false
false
false
false
false
false
false
2,336
1062A
JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ of integers, such that $$$1 le a_1 < a_2 < ldots < a_n le 10^3$$$, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range $$$[1, 10^3]$$$. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the number of elements in the array. The second line of the input contains $$$n$$$ integers $$$a_i$$$ ($$$1 le a_1<a_2<dots<a_n le 10^3$$$)xa0— the array written by Giraffe. Output Print a single integerxa0— the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print $$$0$$$. Note In the first example, JATC can erase the third and fourth elements, leaving the array $$$[1, 3, _, _, 6, 9]$$$. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become $$$[998, _, _]$$$. Because all the elements are less than or equal to $$$1000$$$, the array is still can be restored. Note, that he can't erase the first $$$2$$$ elements. In the third example, JATC can erase the first $$$4$$$ elements. Since all the elements are greater than or equal to $$$1$$$, Giraffe can still restore the array. Note, that he can't erase the last $$$4$$$ elements.
1,300
false
true
true
false
false
false
false
false
false
false
5,402
355B
Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has _n_ buses and _m_ trolleys, the buses are numbered by integers from 1 to _n_, the trolleys are numbered by integers from 1 to _m_. Public transport is not free. There are 4 types of tickets: 1. A ticket for one ride on some bus or trolley. It costs _c_1 burles; 2. A ticket for an unlimited number of rides on some bus or on some trolley. It costs _c_2 burles; 3. A ticket for an unlimited number of rides on all buses or all trolleys. It costs _c_3 burles; 4. A ticket for an unlimited number of rides on all buses and trolleys. It costs _c_4 burles. Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets. Input The first line contains four integers _c_1,u2009_c_2,u2009_c_3,u2009_c_4 (1u2009≤u2009_c_1,u2009_c_2,u2009_c_3,u2009_c_4u2009≤u20091000) — the costs of the tickets. The second line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000) — the number of buses and trolleys Vasya is going to use. The third line contains _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u20091000) — the number of times Vasya is going to use the bus number _i_. The fourth line contains _m_ integers _b__i_ (0u2009≤u2009_b__i_u2009≤u20091000) — the number of times Vasya is going to use the trolley number _i_. Output Print a single number — the minimum sum of burles Vasya will have to spend on the tickets. Examples Input 100 100 8 100 3 5 7 94 12 100 1 47 0 42 Note In the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1)u2009+u20093u2009+u20097u2009=u200912 burles. In the second sample the profitable strategy is to buy one ticket of the fourth type. In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys.
1,100
false
true
true
false
false
false
false
false
false
false
8,412
1201C
You are given an array $$$a$$$ of $$$n$$$ integers, where $$$n$$$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $$$a_i$$$) and increase it by $$$1$$$ (that is, replace it with $$$a_i + 1$$$). You want to make the median of the array the largest possible using at most $$$k$$$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $$$[1, 5, 2, 3, 5]$$$ is $$$3$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$n$$$ is odd, $$$1 le k le 10^9$$$)xa0— the number of elements in the array and the largest number of operations you can make. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). Output Print a single integerxa0— the maximum possible median after the operations. Note In the first example, you can increase the second element twice. Than array will be $$$[1, 5, 5]$$$ and it's median is $$$5$$$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $$$3$$$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $$$[5, 1, 2, 5, 3, 5, 5]$$$ and the median will be $$$5$$$.
1,400
true
true
false
false
false
false
false
true
true
false
4,693
1872C
You are given two integers $$$l le r$$$. You need to find positive integers $$$a$$$ and $$$b$$$ such that the following conditions are simultaneously satisfied: $$$l le a + b le r$$$ $$$gcd(a, b) eq 1$$$ or report that they do not exist. $$$gcd(a, b)$$$ denotes the = 3$$$, $$$gcd(8, 9) = 1$$$, $$$gcd(4, 2) = 2$$$. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of test cases. Then the descriptions of the test cases follow. The only line of the description of each test case contains $$$2$$$ integers $$$l, r$$$ ($$$1 le l le r le 10^7$$$). Output For each test case, output the integers $$$a, b$$$ that satisfy all the conditions on a separate line. If there is no answer, instead output a single number $$$-1$$$. If there are multiple answers, you can output any of them. Note In the first test case, $$$11 le 6 + 9 le 15$$$, $$$gcd(6, 9) = 3$$$, and all conditions are satisfied. Note that this is not the only possible answer, for example, $$${4, 10}, {5, 10}, {6, 6}$$$ are also valid answers for this test case. In the second test case, the only pairs $$${a, b}$$$ that satisfy the condition $$$1 le a + b le 3$$$ are $$${1, 1}, {1, 2}, {2, 1}$$$, but in each of these pairs $$$gcd(a, b)$$$ equals $$$1$$$, so there is no answer. In the third sample test, $$$gcd(14, 4) = 2$$$.
1,100
true
false
false
false
false
false
false
false
false
false
1,049
167B
One must train much to do well on wizardry contests. So, there are numerous wizardry schools and magic fees. One of such magic schools consists of _n_ tours. A winner of each tour gets a huge prize. The school is organised quite far away, so one will have to take all the prizes home in one go. And the bags that you've brought with you have space for no more than _k_ huge prizes. Besides the fact that you want to take all the prizes home, you also want to perform well. You will consider your performance good if you win at least _l_ tours. In fact, years of organizing contests proved to the organizers that transporting huge prizes is an issue for the participants. Alas, no one has ever invented a spell that would shrink the prizes... So, here's the solution: for some tours the winner gets a bag instead of a huge prize. Each bag is characterized by number _a__i_ — the number of huge prizes that will fit into it. You already know the subject of all tours, so you can estimate the probability _p__i_ of winning the _i_-th tour. You cannot skip the tour under any circumstances. Find the probability that you will perform well on the contest and will be able to take all won prizes home (that is, that you will be able to fit all the huge prizes that you won into the bags that you either won or brought from home). Input The first line contains three integers _n_, _l_, _k_ (1u2009≤u2009_n_u2009≤u2009200,u20090u2009≤u2009_l_,u2009_k_u2009≤u2009200) — the number of tours, the minimum number of tours to win, and the number of prizes that you can fit in the bags brought from home, correspondingly. The second line contains _n_ space-separated integers, _p__i_ (0u2009≤u2009_p__i_u2009≤u2009100) — the probability to win the _i_-th tour, in percents. The third line contains _n_ space-separated integers, _a__i_ (1u2009≤u2009_a__i_u2009≤u2009200) — the capacity of the bag that will be awarded to you for winning the _i_-th tour, or else -1, if the prize for the _i_-th tour is a huge prize and not a bag. Output Print a single real number — the answer to the problem. The answer will be accepted if the absolute or relative error does not exceed 10u2009-u20096. Examples Input 3 1 0 10 20 30 -1 -1 2 Note In the first sample we need either win no tour or win the third one. If we win nothing we wouldn't perform well. So, we must to win the third tour. Other conditions will be satisfied in this case. Probability of wining the third tour is 0.3. In the second sample we win the only tour with probability 1.0, and go back home with bag for it.
1,800
true
false
false
true
false
false
false
false
false
false
9,194