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
1859F
There are $$$n$$$ cities in Byteland, some of which are connected by roads, which can be traversed in any direction. The $$$i$$$-th road has its own hardness parameter $$$w_i$$$. Time spent on traversing a road with its hardness equal to $$$w_i$$$ is $$$lceilfrac{w_i}{c} ceil$$$, where $$$c$$$ is the current driving skill. The travel network of Byteland is a tree. In other words, between any pair of cities, there is exactly one path that passes through each city at most once. In some cities you can visit driving courses. A single course takes $$$T$$$ time to complete, and after completing the course the driver's skill $$$c$$$ is increased by $$$2$$$ times. Notice that the time $$$T$$$ required to complete a course is the same in all cities, and courses can be completed in the same city more than once. You need to answer the $$$q$$$ queries: what is the minimum time it takes to get from the city $$$a$$$ to city $$$b$$$ if you start the travelling with driving skill $$$c = 1$$$? Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 le n le 10^5, 1 le T le 10^6$$$) - the number of cities and time required to complete a single driving course. The following $$$n - 1$$$ lines each contain three integers $$$u_i$$$, $$$v_i$$$ and $$$w_i$$$ ($$$1 le u_i, v_i le n, 1 le w_i le 10^6, u_i eq v_i$$$), which mean that there exists a road connecting towns $$$u_i$$$ and $$$v_i$$$ with hardness equal to $$$w_i$$$ . The next line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of symbols $$$0$$$ and $$$1$$$. If $$$s_i = 1$$$ ($$$1 le i le n$$$), then you can visit driving courses in the $$$i$$$-th city. If $$$s_i = 0$$$ ($$$1 le i le n$$$), then you cannot visit driving courses in the $$$i$$$-th city. The next line contains a single integer $$$q$$$ ($$$1 le q le 10^5$$$) β€” the number of queries you are required to answer. The next $$$q$$$ lines contain two integers $$$a_j$$$, $$$b_j$$$ ($$$1 le a_j, b_j le n, 1 le j le q$$$) β€” the cities you are required to process in the $$$j$$$-th query. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$. Output For each query, print one integer in a separate line β€” the minimum time it takes to get in the corresponding query. Example Input 2 2 3 1 2 1 11 1 1 2 5 3 1 4 5 1 3 8 2 3 8 4 5 10 11001 5 1 5 2 5 5 1 3 4 4 2 Note In the only query of the first test case, it is optimal to ignore the driving courses. Then the minimum time required is equal to the distance between vertexes $$$1$$$ and $$$2$$$, which is $$$1$$$. In the first query of the second test case, we can spend $$$3$$$ time in city number $$$1$$$ visiting the driving courses, then go to vertex $$$5$$$. Then the minimum time required is $$$3 + lceilfrac{5}{2} ceil + lceilfrac{10}{2} ceil = 11$$$.
3,200
false
false
false
false
true
false
false
false
false
true
1,126
1651C
There is a classroom with two rows of computers. There are $$$n$$$ computers in each row and each computer has its own grade. Computers in the first row has grades $$$a_1, a_2, dots, a_n$$$ and in the second rowxa0β€” $$$b_1, b_2, dots, b_n$$$. Initially, all pairs of neighboring computers in each row are connected by wire (pairs $$$(i, i + 1)$$$ for all $$$1 le i < n$$$), so two rows form two independent computer networks. Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $$$i$$$-th computer from the first row and the $$$j$$$-th computer from the second row costs $$$a_i - b_j$$$. You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts. That is the minimum total cost to make a fault-tolerant network? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$)xa0β€” the number of computers in each row. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β€” the grades of computers in the first row. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0β€” the grades of computers in the second row. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0β€” the minimum total cost to make a fault-tolerant network. Example Input 2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000 Note In the first test case, it's optimal to connect four pairs of computers: 1. computer $$$1$$$ from the first row with computer $$$2$$$ from the second row: cost $$$1 - 4 = 3$$$; 2. computer $$$3$$$ from the first row with computer $$$2$$$ from the second row: cost $$$1 - 4 = 3$$$; 3. computer $$$2$$$ from the first row with computer $$$1$$$ from the second row: cost $$$10 - 20 = 10$$$; 4. computer $$$2$$$ from the first row with computer $$$3$$$ from the second row: cost $$$10 - 25 = 15$$$; In total, $$$3 + 3 + 10 + 15 = 31$$$. In the second test case, it's optimal to connect $$$1$$$ from the first row with $$$1$$$ from the second row, and $$$4$$$ from the first row with $$$4$$$ from the second row.
1,500
false
false
true
false
true
false
true
false
false
false
2,366
568E
Note that the memory limit in this problem is less than usual. Let's consider an array consisting of positive integers, some positions of which contain gaps. We have a collection of numbers that can be used to fill the gaps. Each number from the given collection can be used at most once. Your task is to determine such way of filling gaps that the longest increasing subsequence in the formed array has a maximum size. Input The first line contains a single integer _n_xa0β€” the length of the array (1u2009≀u2009_n_u2009≀u2009105). The second line contains _n_ space-separated integersxa0β€” the elements of the sequence. A gap is marked as "-1". The elements that are not gaps are positive integers not exceeding 109. It is guaranteed that the sequence contains 0u2009≀u2009_k_u2009≀u20091000 gaps. The third line contains a single positive integer _m_xa0β€” the number of elements to fill the gaps (_k_u2009≀u2009_m_u2009≀u2009105). The fourth line contains _m_ positive integersxa0β€” the numbers to fill gaps. Each number is a positive integer not exceeding 109. Some numbers may be equal. Output Print _n_ space-separated numbers in a single linexa0β€” the resulting sequence. If there are multiple possible answers, print any of them. Note In the first sample there are no gaps, so the correct answer is the initial sequence. In the second sample there is only one way to get an increasing subsequence of length 3. In the third sample answer "4 2" would also be correct. Note that only strictly increasing subsequences are considered. In the fifth sample the answer "1 1 1 2" is not considered correct, as number 1 can be used in replacing only two times.
3,000
false
false
false
true
true
false
false
false
false
false
7,574
1165F1
The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β€” and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $$$n$$$ types of microtransactions in the game. Each microtransaction costs $$$2$$$ burles usually and $$$1$$$ burle if it is on sale. Ivan has to order exactly $$$k_i$$$ microtransactions of the $$$i$$$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $$$1$$$ burle and otherwise he can buy it for $$$2$$$ burles. There are also $$$m$$$ special offers in the game shop. The $$$j$$$-th offer $$$(d_j, t_j)$$$ means that microtransactions of the $$$t_j$$$-th type are on sale during the $$$d_j$$$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 1000$$$) β€” the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $$$n$$$ integers $$$k_1, k_2, dots, k_n$$$ ($$$0 le k_i le 1000$$$), where $$$k_i$$$ is the number of copies of microtransaction of the $$$i$$$-th type Ivan has to order. It is guaranteed that sum of all $$$k_i$$$ is not less than $$$1$$$ and not greater than $$$1000$$$. The next $$$m$$$ lines contain special offers. The $$$j$$$-th of these lines contains the $$$j$$$-th special offer. It is given as a pair of integers $$$(d_j, t_j)$$$ ($$$1 le d_j le 1000, 1 le t_j le n$$$) and means that microtransactions of the $$$t_j$$$-th type are on sale during the $$$d_j$$$-th day. Output Print one integer β€” the minimum day when Ivan can order all microtransactions he wants and actually start playing. Examples Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Input 5 3 4 2 1 3 2 3 5 4 2 2 5
2,000
false
true
false
false
false
false
false
true
false
false
4,891
1996D
Problem - 1996D - 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 brute force combinatorics math number theory *1500 No tag edit access β†’ Contest materials Editorial") of positive integers such that $$$ab + ac + bc le n$$$ and $$$a + b + c le x$$$. Note that order matters (e.g. ($$$1, 1, 2$$$) and ($$$1, 2, 1$$$) are treated as different) and $$$a$$$, $$$b$$$, $$$c$$$ must be strictly greater than $$$0$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) xa0β€” the number of test cases. Each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 leq n,x leq 10^6$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ and that the sum of $$$x$$$ over all test cases does not exceed $$$10^6$$$. Output Output a single integer β€” the number of triplets ($$$a,b,c$$$) of positive integers such that $$$ab + ac + bc le n$$$ and $$$a + b + c le x$$$. Example Input 4 7 4 10 5 7 1000 900000 400000 Output 4 10 7 1768016938 Note In the first test case, the triplets are ($$$1, 1, 1$$$), ($$$1, 1, 2$$$), ($$$1, 2, 1$$$), and ($$$2, 1, 1$$$). In the second test case, the triplets are ($$$1, 1, 1$$$), ($$$1, 1, 2$$$), ($$$1, 1, 3$$$), ($$$1, 2, 1$$$), ($$$1, 2, 2$$$), ($$$1, 3, 1$$$), ($$$2, 1, 1$$$), ($$$2, 1, 2$$$), ($$$2, 2, 1$$$), and ($$$3, 1, 1$$$).
1,500
true
false
false
false
false
false
true
true
false
false
287
1688C
As a human, she can erase history of its entirety. As a Bai Ze (Hakutaku), she can create history out of nothingness. β€”Perfect Memento in Strict Sense Keine has the ability to manipulate history. The history of Gensokyo is a string $$$s$$$ of length $$$1$$$ initially. To fix the chaos caused by Yukari, she needs to do the following operations $$$n$$$ times, for the $$$i$$$-th time: She chooses a non-empty substring $$$t_{2i-1}$$$ of $$$s$$$. She replaces $$$t_{2i-1}$$$ with a non-empty string, $$$t_{2i}$$$. Note that the lengths of strings $$$t_{2i-1}$$$ and $$$t_{2i}$$$ can be different. Note that if $$$t_{2i-1}$$$ occurs more than once in $$$s$$$, exactly one of them will be replaced. For example, let $$$s=$$$"marisa", $$$t_{2i-1}=$$$"a", and $$$t_{2i}=$$$"z". After the operation, $$$s$$$ becomes "mzrisa" or "marisz". After $$$n$$$ operations, Keine got the final string and an operation sequence $$$t$$$ of length $$$2n$$$. Just as Keine thinks she has finished, Yukari appears again and shuffles the order of $$$t$$$. Worse still, Keine forgets the initial history. Help Keine find the initial history of Gensokyo! Recall that a substring is a sequence of consecutive characters of the string. For example, for string "abc" its substrings are: "ab", "c", "bc" and some others. But the following strings are not its substring: "ac", "cba", "acb". Hacks You cannot make hacks in this problem. Input Each test contains multiple test cases. The first line contains a single integer $$$T$$$ ($$$1 leq T leq 10^3$$$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n < 10 ^ 5$$$) β€” the number of operations. The next $$$2n$$$ lines contains one non-empty string $$$t_{i}$$$ β€” the $$$i$$$-th string of the shuffled sequence $$$t$$$. The next line contains one non-empty string $$$s$$$ β€” the final string. It is guaranteed that the total length of given strings (including $$$t_i$$$ and $$$s$$$) over all test cases does not exceed $$$2 cdot 10 ^ 5$$$. All given strings consist of lowercase English letters only. It is guaranteed that the initial string exists. It can be shown that the initial string is unique. Note Test case 1: Initially $$$s$$$ is "a". In the first operation, Keine chooses "a", and replaces it with "ab". $$$s$$$ becomes "ab". In the second operation, Keine chooses "b", and replaces it with "cd". $$$s$$$ becomes "acd". So the final string is "acd", and $$$t=[$$$"a", "ab", "b", "cd"$$$]$$$ before being shuffled. Test case 2: Initially $$$s$$$ is "z". In the first operation, Keine chooses "z", and replaces it with "aa". $$$s$$$ becomes "aa". In the second operation, Keine chooses "a", and replaces it with "ran". $$$s$$$ becomes "aran". In the third operation, Keine chooses "a", and replaces it with "yakumo". $$$s$$$ becomes "yakumoran". So the final string is "yakumoran", and $$$t=[$$$"z", "aa", "a", "ran", "a", "yakumo"$$$]$$$ before being shuffled.
1,700
false
true
false
false
false
true
false
false
false
false
2,149
997B
Problem - 997B - 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 combinatorics dp greedy math *2000 No tag edit access β†’ Contest materials xa0β€” the number of roman digits to use. Output Output a single integerxa0β€” the number of distinct integers which can be represented using $$$n$$$ roman digits exactly. Examples Input 1 Output 4 Input 2 Output 10 Input 10 Output 244 Note In the first sample there are exactly $$$4$$$ integers which can be representedxa0β€” I, V, X and L. In the second sample it is possible to represent integers $$$2$$$ (II), $$$6$$$ (VI), $$$10$$$ (VV), $$$11$$$ (XI), $$$15$$$ (XV), $$$20$$$ (XX), $$$51$$$ (IL), $$$55$$$ (VL), $$$60$$$ (XL) and $$$100$$$ (LL).
2,000
true
true
false
true
false
false
true
false
false
false
5,717
228E
Berland has _n_ cities, some of them are connected by bidirectional roads. For each road we know whether it is asphalted or not. The King of Berland Valera II wants to asphalt all roads of Berland, for that he gathered a group of workers. Every day Valera chooses exactly one city and orders the crew to asphalt all roads that come from the city. The valiant crew fulfilled the King's order in a day, then workers went home. Unfortunately, not everything is as great as Valera II would like. The main part of the group were gastarbeiters β€” illegal immigrants who are enthusiastic but not exactly good at understanding orders in Berlandian. Therefore, having received orders to asphalt the roads coming from some of the city, the group asphalted all non-asphalted roads coming from the city, and vice versa, took the asphalt from the roads that had it. Upon learning of this progress, Valera II was very upset, but since it was too late to change anything, he asked you to make a program that determines whether you can in some way asphalt Berlandian roads in at most _n_ days. Help the king. Input The first line contains two space-separated integers _n_,u2009_m_ β€” the number of cities and roads in Berland, correspondingly. Next _m_ lines contain the descriptions of roads in Berland: the _i_-th line contains three space-separated integers _a__i_,u2009_b__i_,u2009_c__i_ (1u2009≀u2009_a__i_,u2009_b__i_u2009≀u2009_n_;xa0_a__i_u2009β‰ u2009_b__i_;xa00u2009≀u2009_c__i_u2009≀u20091). The first two integers (_a__i_,u2009_b__i_) are indexes of the cities that are connected by the _i_-th road, the third integer (_c__i_) equals 1, if the road was initially asphalted, and 0 otherwise. Consider the cities in Berland indexed from 1 to _n_, and the roads indexed from 1 to _m_. It is guaranteed that between two Berlandian cities there is not more than one road. Output In the first line print a single integer _x_ (0u2009≀u2009_x_u2009≀u2009_n_) β€” the number of days needed to asphalt all roads. In the second line print _x_ space-separated integers β€” the indexes of the cities to send the workers to. Print the cities in the order, in which Valera send the workers to asphalt roads. If there are multiple solutions, print any of them. If there's no way to asphalt all roads, print "Impossible" (without the quotes). Examples Input 4 4 1 2 1 2 4 0 4 3 1 3 2 0
1,900
false
false
false
false
false
false
false
false
false
true
8,924
1220C
Mike and Ann are sitting in the classroom. The lesson is boring, so they decided to play an interesting game. Fortunately, all they need to play this game is a string $$$s$$$ and a number $$$k$$$ ($$$0 le k < s$$$). At the beginning of the game, players are given a substring of $$$s$$$ with left border $$$l$$$ and right border $$$r$$$, both equal to $$$k$$$ (i.e. initially $$$l=r=k$$$). Then players start to make moves one by one, according to the following rules: A player chooses $$$l^{prime}$$$ and $$$r^{prime}$$$ so that $$$l^{prime} le l$$$, $$$r^{prime} ge r$$$ and $$$s[l^{prime}, r^{prime}]$$$ is lexicographically less than $$$s[l, r]$$$. Then the player changes $$$l$$$ and $$$r$$$ in this way: $$$l := l^{prime}$$$, $$$r := r^{prime}$$$. Ann moves first. The player, that can't make a move loses. Recall that a substring $$$s[l, r]$$$ ($$$l le r$$$) of a string $$$s$$$ is a continuous segment of letters from s that starts at position $$$l$$$ and ends at position $$$r$$$. For example, "ehn" is a substring ($$$s[3, 5]$$$) of "aaaehnsvz" and "ahz" is not. Mike and Ann were playing so enthusiastically that they did not notice the teacher approached them. Surprisingly, the teacher didn't scold them, instead of that he said, that he can figure out the winner of the game before it starts, even if he knows only $$$s$$$ and $$$k$$$. Unfortunately, Mike and Ann are not so keen in the game theory, so they ask you to write a program, that takes $$$s$$$ and determines the winner for all possible $$$k$$$.
1,300
false
true
false
false
false
false
false
false
false
false
4,571
2027D1
This is the easy version of this problem. The only difference is that you only need to output the minimum total cost of operations in this version. You must solve both versions to be able to hack. You're given an array $$$a$$$ of length $$$n$$$, and an array $$$b$$$ of length $$$m$$$ ($$$b_i > b_{i+1}$$$ for all $$$1 le i < m$$$). Initially, the value of $$$k$$$ is $$$1$$$. Your aim is to make the array $$$a$$$ empty by performing one of these two operations repeatedly: Type $$$1$$$xa0β€” If the value of $$$k$$$ is less than $$$m$$$ and the array $$$a$$$ is not empty, you can increase the value of $$$k$$$ by $$$1$$$. This does not incur any cost. Type $$$2$$$xa0β€” You remove a non-empty prefix of array $$$a$$$, such that its sum does not exceed $$$b_k$$$. This incurs a cost of $$$m - k$$$. You need to minimize the total cost of the operations to make array $$$a$$$ empty. If it's impossible to do this through any sequence of operations, output $$$-1$$$. Otherwise, output the minimum total cost of the operations. 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 two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 3 cdot 10^5$$$, $$$boldsymbol{1 le n cdot m le 3 cdot 10^5}$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). The third line of each test case contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$1 le b_i le 10^9$$$). It is also guaranteed that $$$b_i > b_{i+1}$$$ for all $$$1 le i < m$$$. It is guaranteed that the sum of $$$boldsymbol{n cdot m}$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, if it's possible to make $$$a$$$ empty, then output the minimum total cost of the operations. If there is no possible sequence of operations which makes $$$a$$$ empty, then output a single integer $$$-1$$$. Example Input 5 4 2 9 3 4 3 11 7 1 2 20 19 18 10 2 2 5 2 1 10 3 2 9 9 6 17 9 10 11 2 2 2 2 2 2 2 2 2 2 20 18 16 14 12 10 8 6 4 2 1 1 6 10 32 16 8 4 2 1 Note In the first test case, one optimal sequence of operations which yields a total cost of $$$1$$$ is as follows: Perform an operation of type $$$2$$$. Choose the prefix to be $$$[9]$$$. This incurs a cost of $$$1$$$. Perform an operation of type $$$1$$$. The value of $$$k$$$ is now $$$2$$$. This incurs no cost. Perform an operation of type $$$2$$$. Choose the prefix to be $$$[3, 4]$$$. This incurs a cost of $$$0$$$. Perform an operation of type $$$2$$$. Choose the prefix to be $$$[3]$$$. This incurs a cost of $$$0$$$. The array $$$a$$$ is now empty, and the total cost of all operations is $$$1$$$. In the second test case, it's impossible to remove any prefix of the array since $$$a_1 > b_1$$$, so array $$$a$$$ cannot be made empty by any sequence of operations.
1,700
false
true
true
true
false
false
false
true
false
true
107
1852F
The red pandas are in town to meet their relatives, the blue pandas! The town is modeled by a number line. The pandas have already planned their meetup, but the schedule keeps changing. You are given $$$q$$$ updates of the form x t c. If $$$c < 0$$$, it means $$$c$$$ more red pandas enter the number line at position $$$x$$$ and time $$$t$$$. Then, each unit of time, they can each independently move one unit in either direction across the number line, or not move at all. If $$$c > 0$$$, it means that $$$c$$$ more blue pandas check position $$$x$$$ for red pandas at time $$$t$$$. If a blue panda does not meet a red panda at that specific location and time, they dejectedly leave the number line right away. If there is a red panda at a position at the same time a blue panda checks it, they form a friendship and leave the number line. Each red panda can form a friendship with at most one blue panda and vice versa. The updates will be given in order of non-decreasing $$$x$$$ values. After each update, please print the maximum number of friendships if the red pandas move in an optimal order based on all the updates given in the input above (and including) this update. The order in which a red panda moves can change between updates. Input The first line contains $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$) xa0– the number of updates. The $$$i$$$-th line of the next $$$q$$$ lines consists of $$$3$$$ integers $$$x_i$$$, $$$t_i$$$ and $$$c_i$$$ ($$$0 leq x_i leq 10^9$$$, $$$0 leq t_i leq 10^9$$$, $$$0 < c_i leq 1000$$$)xa0– the description of the $$$i$$$-th update. It is guaranteed that the $$$x_i$$$ will be given in non-decreasing order. Output After each update, print the maximum number of friendships that can be formed. Examples Input 5 0 6 3 4 2 -5 7 4 -6 10 5 100 10 8 7 Input 5 0 6 3 4 2 -5 7 4 -6 10 5 100 11 8 7 Input 7 0 8 6 2 7 -2 3 1 -6 5 3 -8 7 3 -3 8 0 -2 8 2 1 Input 4 0 0 -3 0 0 2 0 0 4 0 0 -10 Note In the first example, the number of friendships after each update can be optimized as follows: 1. $$$3$$$ blue pandas now check for red pandas at position $$$0$$$ at time $$$6$$$. There are no red pandas anywhere, so there are no friendships. 2. $$$5$$$ red pandas now appear at position $$$4$$$ and time $$$2$$$. $$$4$$$ of the red pandas can travel to position $$$0$$$ at time $$$6$$$, where $$$3$$$ of them can make friendships with the $$$3$$$ existing blue pandas. 3. $$$6$$$ red pandas now appear at position $$$7$$$ and time $$$4$$$. No new blue pandas are added, so the maximum number of friendships is still $$$3$$$. 4. $$$100$$$ blue pandas now appear at position $$$10$$$ and time $$$5$$$. No red pandas can reach them at a time of $$$5$$$, so no new friendships are created. 5. $$$7$$$ blue pandas now appear at position $$$10$$$ and time $$$8$$$. $$$6$$$ of the red pandas at position $$$7$$$ and time $$$4$$$, along with $$$1$$$ red panda at position $$$4$$$ and time $$$2$$$, can reach $$$7$$$ of the blue pandas at position $$$10$$$ at time $$$8$$$, adding $$$7$$$ new friendships. This brings the total to $$$10$$$ friendships.
3,500
false
false
false
true
true
false
false
false
false
false
1,162
1118C
Let's call some square matrix with integer values in its cells palindromic if it doesn't change after the order of rows is reversed and it doesn't change after the order of columns is reversed. For example, the following matrices are palindromic: The following matrices are not palindromic because they change after the order of rows is reversed: The following matrices are not palindromic because they change after the order of columns is reversed: You are given $$$n^2$$$ integers. Put them into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic. If there are multiple answers, print any. If there is no solution, print "NO". Input The first line contains one integer $$$n$$$ ($$$1 le n le 20$$$). The second line contains $$$n^2$$$ integers $$$a_1, a_2, dots, a_{n^2}$$$ ($$$1 le a_i le 1000$$$) β€” the numbers to put into a matrix of $$$n$$$ rows and $$$n$$$ columns. Output If it is possible to put all of the $$$n^2$$$ numbers into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic, then print "YES". Then print $$$n$$$ lines with $$$n$$$ space-separated numbers β€” the resulting matrix. If it's impossible to construct any matrix, then print "NO". You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable. Examples Input 4 1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1 Output YES 1 2 2 1 8 2 2 8 8 2 2 8 1 2 2 1 Output YES 1 3 1 3 1 3 1 3 1 Input 4 1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1 Note Note that there exist multiple answers for the first two examples.
1,700
false
false
true
false
false
true
false
false
false
false
5,100
283E
Farmer John is hosting a tennis tournament with his _n_ cows. Each cow has a skill level _s__i_, and no two cows having the same skill level. Every cow plays every other cow exactly once in the tournament, and each cow beats every cow with skill level lower than its own. However, Farmer John thinks the tournament will be demoralizing for the weakest cows who lose most or all of their matches, so he wants to flip some of the results. In particular, at _k_ different instances, he will take two integers _a__i_,u2009_b__i_ (_a__i_u2009<u2009_b__i_) and flip all the results between cows with skill level between _a__i_ and _b__i_ inclusive. That is, for any pair _x_,u2009_y_ he will change the result of the match on the final scoreboard (so if _x_ won the match, the scoreboard will now display that _y_ won the match, and vice versa). It is possible that Farmer John will change the result of a match multiple times. It is not guaranteed that _a__i_ and _b__i_ are equal to some cow's skill level. Farmer John wants to determine how balanced he made the tournament results look. In particular, he wants to count the number of triples of cows (_p_,u2009_q_,u2009_r_) for which the final leaderboard shows that cow _p_ beats cow _q_, cow _q_ beats cow _r_, and cow _r_ beats cow _p_. Help him determine this number. Note that two triples are considered different if they do not contain the same set of cows (i.e. if there is a cow in one triple that is not in the other). Input On the first line are two space-separated integers, _n_ and _k_ (3u2009≀u2009_n_u2009≀u2009105;xa00u2009≀u2009_k_u2009≀u2009105). On the next line are _n_ space-separated distinct integers, _s_1,u2009_s_2,u2009...,u2009_s__n_ (1u2009≀u2009_s__i_u2009≀u2009109), denoting the skill levels of the cows. On the next _k_ lines are two space separated integers, _a__i_ and _b__i_ (1u2009≀u2009_a__i_u2009<u2009_b__i_u2009≀u2009109) representing the changes Farmer John made to the scoreboard in the order he makes it. Output A single integer, containing the number of triples of cows (_p_,u2009_q_,u2009_r_) for which the final leaderboard shows that cow _p_ beats cow _q_, cow _q_ beats cow _r_, and cow _r_ beats cow _p_. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 5 3 5 9 4 1 7 1 7 2 8 3 9 Note In the first sample, cow 3 > cow 1, cow 3 > cow 2, and cow 2 > cow 1. However, the results between cows 1 and 2 and cows 2 and 3 are flipped, so now FJ's results show that cow 1 > cow 2, cow 2 > cow 3, and cow 3 > cow 1, so cows 1, 2, and 3 form a balanced triple.
2,800
true
false
false
false
true
false
false
false
false
false
8,699
1012F
Gleb is a famous competitive programming teacher from Innopolis. He is planning a trip to _N_ programming camps in the nearest future. Each camp will be held in a different country. For each of them, Gleb needs to apply for a visa. For each of these trips Gleb knows three integers: the number of the first day of the trip _s__i_, the length of the trip in days _len__i_, and the number of days _t__i_ this country's consulate will take to process a visa application and stick a visa in a passport. Gleb has _P_ (1u2009≀u2009_P_u2009≀u20092) valid passports and is able to decide which visa he wants to put in which passport. For each trip, Gleb will have a flight to that country early in the morning of the day _s__i_ and will return back late in the evening of the day _s__i_u2009+u2009_len__i_u2009-u20091. To apply for a visa on the day _d_, Gleb needs to be in Innopolis in the middle of this day. So he can't apply for a visa while he is on a trip, including the first and the last days. If a trip starts the next day after the end of the other one, Gleb can't apply for a visa between them as well. The earliest Gleb can apply for a visa is day 1. After applying for a visa of country _i_ on day _d_, Gleb will get his passport back in the middle of the day _d_u2009+u2009_t__i_. Consulates use delivery services, so Gleb can get his passport back even if he is not in Innopolis on this day. Gleb can apply for another visa on the same day he received his passport back, if he is in Innopolis this day. Gleb will not be able to start his trip on day _s__i_ if he doesn't has a passport with a visa for the corresponding country in the morning of day _s__i_. In particular, the passport should not be in another country's consulate for visa processing. Help Gleb to decide which visas he needs to receive in which passport, and when he should apply for each visa. Input In the first line of the input there are two integers _N_ (1u2009≀u2009_N_u2009≀u200922) and _P_ (1u2009≀u2009_P_u2009≀u20092)β€”the number of trips and the number of passports Gleb has, respectively. The next _N_ lines describe Gleb's trips. Each line contains three positive integers _s__i_, _len__i_, _t__i_ (1u2009≀u2009_s__i_,u2009_len__i_,u2009_t__i_u2009≀u2009109)β€”the first day of the trip, the length of the trip and number of days the consulate of this country needs to process a visa application. It is guaranteed that no two trips intersect. Output If it is impossible to get all visas on time, just print "NO" (quotes for clarity). Otherwise, print "YES" and _N_ lines describing trips. For each trip, first print number of the passport Gleb should put this country's visa in, and then print number of the day he should apply for it. Print trips in the same order as they appear in the input. Days are numbered from 1, starting with tomorrowβ€”the first day you can apply for a visa. Passports are numbered from 1 to _P_. If there are several possible answers, print any of them. Examples Input 3 1 13 2 2 7 3 1 19 3 4 Input 7 2 15 1 1 14 1 1 18 1 1 21 1 1 9 4 6 22 2 5 5 4 3 Output YES 2 13 1 1 1 16 1 19 1 2 2 16 2 1 Input 3 1 7 3 1 13 2 3 19 3 4 Note Examples with answer "YES" are depicted below. Each cell of the stripe represents a single day. Rectangles represent trips, each trip starts in the morning and ends in the evening. Rectangles with angled corners represent visa applications. Each application starts in the middle of a day and ends _t__i_ days after. The trip and the visa application for the same country have the same color. In examples with two passports, visa applications and trips depicted above the time stripe are made using the first passport, visa applications and trips depicted below the time stripe are made using the second passport. Example 1: Example 2: Example 3:
3,400
false
false
true
true
false
false
false
false
false
false
5,622
1180A
Problem - 1180A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags dp implementation math *800 No tag edit access β†’ Contest materials . A $$$n$$$-th order rhombus for all $$$n geq 2$$$ one obtains from a $$$n-1$$$-th order rhombus adding all cells which have a common side with it to it (look at the picture to understand it better). Alex asks you to compute the number of cells in a $$$n$$$-th order rhombus. Input The first and only input line contains integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0β€” order of a rhombus whose numbers of cells should be computed. Output Print exactly one integerxa0β€” the number of cells in a $$$n$$$-th order rhombus. Examples Input 1 Output 1 Input 2 Output 5 Input 3 Output 13 Note Images of rhombus corresponding to the examples are given in the statement.
800
true
false
true
true
false
false
false
false
false
false
4,816
1514B
Problem - 1514B - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags bitmasks combinatorics math *1200 No tag edit access β†’ Contest materials ") ; the xa0β€” the number of test cases you need to solve. Each test case consists of a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^{5}$$$, $$$1 le k le 20$$$). Output For each test case, print the number of arrays satisfying the conditions. Since the answer can be very large, print its remainder when divided by $$$10^9+7$$$. Example Input 2 2 2 100000 20 Output 4 226732710 Note In the first example, the $$$4$$$ arrays are: $$$
1,200
true
false
false
false
false
false
false
false
false
false
3,095
940C
And where the are the phone numbers? You are given a string _s_ consisting of lowercase English letters and an integer _k_. Find the lexicographically smallest string _t_ of length _k_, such that its set of letters is a subset of the set of letters of _s_ and _s_ is lexicographically smaller than _t_. It's guaranteed that the answer exists. Note that the set of letters is a set, not a multiset. For example, the set of letters of abadaba is {_a_,u2009_b_,u2009_d_}. String _p_ is lexicographically smaller than string _q_, if _p_ is a prefix of _q_, is not equal to _q_ or there exists _i_, such that _p__i_u2009<u2009_q__i_ and for all _j_u2009<u2009_i_ it is satisfied that _p__j_u2009=u2009_q__j_. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abec, afa is not lexicographically smaller than ab and a is not lexicographically smaller than a. Input The first line of input contains two space separated integers _n_ and _k_ (1u2009≀u2009_n_,u2009_k_u2009≀u2009100u2009000)xa0β€” the length of _s_ and the required length of _t_. The second line of input contains the string _s_ consisting of _n_ lowercase English letters. Output Output the string _t_ conforming to the requirements above. It's guaranteed that the answer exists. Note In the first example the list of strings _t_ of length 3, such that the set of letters of _t_ is a subset of letters of _s_ is as follows: aaa, aab, aac, aba, abb, abc, aca, acb, .... Among them, those are lexicographically greater than abc: aca, acb, .... Out of those the lexicographically smallest is aca.
1,500
false
false
true
false
false
true
false
false
false
false
5,936
515C
Problem - 515C - 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 sortings *1400 No tag edit access β†’ Contest materials β€” the number of digits in _a_. The second line contains _n_ digits of _a_. There is at least one digit in _a_ that is larger than 1. Number _a_ may possibly contain leading zeroes. Output Output a maximum possible integer satisfying the conditions above. There should be no zeroes and ones in this number decimal representation. Examples Input 4 1234 Output 33222 Input 3 555 Output 555 Note In the first case,
1,400
true
true
false
false
false
false
false
false
true
false
7,773
1252E
Andi is a mathematician, a computer scientist, and a songwriter. After spending so much time writing songs, he finally writes a catchy melody that he thought as his best creation. However, the singer who will sing the song/melody has a unique vocal range, thus, an adjustment may be needed. A melody is defined as a sequence of $$$N$$$ notes which are represented by integers. Let $$$A$$$ be the original melody written by Andi. Andi needs to adjust $$$A$$$ into a new melody $$$B$$$ such that for every $$$i$$$ where $$$1 le i < N$$$: If $$$A_i < A_{i+1}$$$, then $$$B_i < B_{i+1}$$$. If $$$A_i = A_{i+1}$$$, then $$$B_i = B_{i+1}$$$. If $$$A_i > A_{i+1}$$$, then $$$B_i > B_{i+1}$$$. $$$B_i - B_{i+1} le K$$$, i.e. the difference between two successive notes is no larger than $$$K$$$. Moreover, the singer also requires that all notes are within her vocal range, i.e. $$$L le B_i le R$$$ for all $$$1 le i le N$$$. Help Andi to determine whether such $$$B$$$ exists, and find the lexicographically smallest $$$B$$$ if it exists. A melody $$$X$$$ is lexicographically smaller than melody $$$Y$$$ if and only if there exists $$$j$$$ ($$$1 le j le N$$$) such that $$$X_i = Y_i$$$ for all $$$i < j$$$ and $$$X_{j} < Y_{j}$$$. For example, consider a melody $$$A = {1,3,5,6,7,8,9,10,3,7,8,9,10,11,12,12}$$$ as shown in the following figure. The diagonal arrow up in the figure implies that $$$A_i < A_{i+1}$$$, the straight right arrow implies that $$$A_i = A_{i+1}$$$, and the diagonal arrow down implies that $$$A_i > A_{i+1}$$$. Supposed we want to make a new melody with $$$L = 1$$$, $$$R = 8$$$, and $$$K = 6$$$. The new melody $$$B = {1,2,3,4,5,6,7,8,2,3,4,5,6,7,8,8}$$$ as shown in the figure satisfies all the requirements, and it is the lexicographically smallest possible.
2,200
false
true
false
false
false
false
false
false
false
false
4,430
892B
Problem - 892B - 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 two pointers *1200 No tag edit access β†’ Contest materials β€” the number of guilty people. Second line contains _n_ space-separated integers _L_1,u2009_L_2,u2009...,u2009_L__n_ (0u2009≀u2009_L__i_u2009≀u2009109), where _L__i_ is the length of the _i_-th person's claw. Output Print one integer β€” the total number of alive people after the bell rings. Examples Input 4 0 1 0 10 Output 1 Input 2 0 0 Output 2 Input 10 1 1 3 0 0 0 2 1 0 3 Output 3 Note In first sample the last person kills everyone in front of him.
1,200
false
true
true
false
false
false
false
false
false
false
6,182
733B
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be _n_ columns participating in the parade, the _i_-th column consists of _l__i_ soldiers, who start to march from left leg, and _r__i_ soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if _L_ is the total number of soldiers on the parade who start to march from the left leg, and _R_ is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal _L_u2009-u2009_R_. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index _i_ and swap values _l__i_ and _r__i_. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer _n_ (1u2009≀u2009_n_u2009≀u2009105)xa0β€” the number of columns. The next _n_ lines contain the pairs of integers _l__i_ and _r__i_ (1u2009≀u2009_l__i_,u2009_r__i_u2009≀u2009500)xa0β€” the number of soldiers in the _i_-th column which start to march from the left or the right leg respectively. Output Print single integer _k_xa0β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to _n_ in the order they are given in the input data. If there are several answers, print any of them. Examples Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5u2009+u20098u2009+u200910u2009=u200923, and from the right legxa0β€” 6u2009+u20099u2009+u20093u2009=u200918. In this case the beauty of the parade will equal 23u2009-u200918u2009=u20095. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5u2009+u20098u2009+u20093u2009=u200916, and who march from the right legxa0β€” 6u2009+u20099u2009+u200910u2009=u200925. In this case the beauty equals 16u2009-u200925u2009=u20099. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
1,100
true
false
false
false
false
false
false
false
false
false
6,863
629C
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length _n_ more than any other strings! The sequence of round brackets is called valid if and only if: 1. the total number of opening brackets is equal to the total number of closing brackets; 2. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets. Gabi bought a string _s_ of length _m_ (_m_u2009≀u2009_n_) and want to complete it to obtain a valid sequence of brackets of length _n_. He is going to pick some strings _p_ and _q_ consisting of round brackets and merge them in a string _p_u2009+u2009_s_u2009+u2009_q_, that is add the string _p_ at the beginning of the string _s_ and string _q_ at the end of the string _s_. Now he wonders, how many pairs of strings _p_ and _q_ exists, such that the string _p_u2009+u2009_s_u2009+u2009_q_ is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109u2009+u20097. Input First line contains _n_ and _m_ (1u2009≀u2009_m_u2009≀u2009_n_u2009≀u2009100u2009000,u2009_n_u2009-u2009_m_u2009≀u20092000)xa0β€” the desired length of the string and the length of the string bought by Gabi, respectively. The second line contains string _s_ of length _m_ consisting of characters '(' and ')' only. Output Print the number of pairs of string _p_ and _q_ such that _p_u2009+u2009_s_u2009+u2009_q_ is a valid sequence of round brackets modulo 109u2009+u20097. Note In the first sample there are four different valid pairs: 1. _p_u2009=u2009"(", _q_u2009=u2009"))" 2. _p_u2009=u2009"()", _q_u2009=u2009")" 3. _p_u2009=u2009"", _q_u2009=u2009"())" 4. _p_u2009=u2009"", _q_u2009=u2009")()" In the second sample the only way to obtain a desired string is choose empty _p_ and _q_. In the third sample there is no way to get a valid sequence of brackets.
2,000
false
false
false
true
false
false
false
false
false
false
7,307
576E
Note the unusual memory limit for this problem. You are given an undirected graph consisting of _n_ vertices and _m_ edges. The vertices are numbered with integers from 1 to _n_, the edges are numbered with integers from 1 to _m_. Each edge can be unpainted or be painted in one of the _k_ colors, which are numbered with integers from 1 to _k_. Initially, none of the edges is painted in any of the colors. You get queries of the form "Repaint edge _e__i_ to color _c__i_". At any time the graph formed by the edges of the same color must be bipartite. If after the repaint this condition is violated, then the query is considered to be invalid and edge _e__i_ keeps its color. Otherwise, edge _e__i_ is repainted in color _c__i_, and the query is considered to valid. Recall that the graph is called bipartite if the set of its vertices can be divided into two parts so that no edge connected vertices of the same parts. For example, suppose you are given a triangle graph, that is a graph with three vertices and edges (1,u20092), (2,u20093) and (3,u20091). Suppose that the first two edges are painted color 1, and the third one is painted color 2. Then the query of "repaint the third edge in color 1" will be incorrect because after its execution the graph formed by the edges of color 1 will not be bipartite. On the other hand, it is possible to repaint the second edge in color 2. You receive _q_ queries. For each query, you should either apply it, and report that the query is valid, or report that the query is invalid. Input The first line contains integers _n_, _m_, _k_, _q_ (2u2009≀u2009_n_u2009≀u20095Β·105, 1u2009≀u2009_m_,u2009_q_u2009≀u20095Β·105, 1u2009≀u2009_k_u2009≀u200950) β€” the number of vertices, the number of edges, the number of colors and the number of queries. Then follow _m_ edges of the graph in the form _a__i_, _b__i_ (1u2009≀u2009_a__i_,u2009_b__i_u2009≀u2009_n_). Then follow _q_ queries of the form _e__i_, _c__i_ (1u2009≀u2009_e__i_u2009≀u2009_m_, 1u2009≀u2009_c__i_u2009≀u2009_k_). It is guaranteed that the graph doesn't contain multiple edges and loops. Output For each query print "YES" (without the quotes), if it is valid, or "NO" (without the quotes), if this query destroys the bipartivity of the graph formed by the edges of some color.
3,300
false
false
false
false
true
false
false
true
false
false
7,539
1172D
Nauuo is a girl who loves playing games related to portals. One day she was playing a game as follows. In an $$$n imes n$$$ grid, the rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, the columns are numbered from $$$1$$$ to $$$n$$$ from left to right. We denote a cell on the intersection of the $$$r$$$-th row and $$$c$$$-th column as $$$(r,c)$$$. A portal is a pair of doors. You can travel from one of them to another without changing your direction. More formally, if you walk into a cell with a door, you will teleport to the cell with the other door of the same portal and then walk into the next cell facing the original direction. There can not be more than one doors in a single cell. The "next cell" is the nearest cell in the direction you are facing. For example, if you are facing bottom, the next cell of $$$(2,5)$$$ is $$$(3,5)$$$. If you walk into a cell without a door, you must walk into the next cell after that without changing the direction. If the next cell does not exist, you must exit the grid. You have to set some (possibly zero) portals in the grid, so that if you walk into $$$(i,1)$$$ facing right, you will eventually exit the grid from $$$(r_i,n)$$$, if you walk into $$$(1, i)$$$ facing bottom, you will exit the grid from $$$(n,c_i)$$$. It is guaranteed that both $$$r_{1..n}$$$ and $$$c_{1..n}$$$ are permutations of $$$n$$$ elements. A permutation of $$$n$$$ elements is a sequence of numbers $$$p_1,p_2,ldots,p_n$$$ in which every integer from $$$1$$$ to $$$n$$$ appears exactly once. She got confused while playing the game, can you help her to find a solution? Input The first line contains a single integer $$$n$$$ ($$$1le nle 1000$$$) β€” the side length of the grid. The second line contains $$$n$$$ integers $$$r_1,r_2,ldots,r_n$$$ ($$$1le r_ile n$$$) β€” if you walk into $$$(i,1)$$$ facing right, you should exit the grid from $$$(r_i,n)$$$. It is guaranteed that $$$r_{1..n}$$$ is a permutation of $$$n$$$ elements. The third line contains $$$n$$$ integers $$$c_1,c_2,ldots,c_n$$$ ($$$1le c_ile n$$$) β€” if you walk into $$$(1,i)$$$ facing bottom, you should exit the grid from $$$(n,c_i)$$$. It is guaranteed that $$$c_{1..n}$$$ is a permutation of $$$n$$$ elements. Output If it is impossible to satisfy the rule, print the only number $$$-1$$$. Otherwise the first line should contain a single integer $$$m$$$ ($$$0le mlefrac{n^2}2$$$) β€” the number of portals you set. In the following $$$m$$$ lines, each line should contain four integers $$$x_1,y_1,x_2,y_2$$$, represents that you set a portal consisting of two doors in $$$(x_1,y_1)$$$ and $$$(x_2,y_2)$$$. If there are multiple answers, print any. You do not have to minimize $$$m$$$. Note Example 1 The cells with the same letter are a portal. You can set portals in this way: It satisfies the rule, because: Example 2 You can set portals in this way:
2,900
false
false
false
false
false
true
false
false
false
false
4,856
438D
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array _a_[1],u2009_a_[2],u2009...,u2009_a_[_n_]. Then he should perform a sequence of _m_ operations. An operation can be one of the following: 1. Print operation _l_,u2009_r_. Picks should write down the value of . 2. Modulo operation _l_,u2009_r_,u2009_x_. Picks should perform assignment _a_[_i_]u2009=u2009_a_[_i_]xa0_mod_xa0_x_ for each _i_ (_l_u2009≀u2009_i_u2009≀u2009_r_). 3. Set operation _k_,u2009_x_. Picks should set the value of _a_[_k_] to _x_ (in other words perform an assignment _a_[_k_]u2009=u2009_x_). Can you help Picks to perform the whole sequence of operations? Input The first line of input contains two integer: _n_,u2009_m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009105). The second line contains _n_ integers, separated by space: _a_[1],u2009_a_[2],u2009...,u2009_a_[_n_]xa0(1u2009≀u2009_a_[_i_]u2009≀u2009109) β€” initial value of array elements. Each of the next _m_ lines begins with a number _type_ . If _type_u2009=u20091, there will be two integers more in the line: _l_,u2009_r_xa0(1u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009_n_), which correspond the operation 1. If _type_u2009=u20092, there will be three integers more in the line: _l_,u2009_r_,u2009_x_xa0(1u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009_n_;xa01u2009≀u2009_x_u2009≀u2009109), which correspond the operation 2. If _type_u2009=u20093, there will be two integers more in the line: _k_,u2009_x_xa0(1u2009≀u2009_k_u2009≀u2009_n_;xa01u2009≀u2009_x_u2009≀u2009109), which correspond the operation 3. Output For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer. Examples Input 5 5 1 2 3 4 5 2 3 5 4 3 3 5 1 2 5 2 1 3 3 1 1 3 Input 10 10 6 9 6 7 6 1 10 10 9 5 1 3 9 2 7 10 9 2 5 10 8 1 4 7 3 3 7 2 7 9 9 1 2 4 1 6 6 1 5 9 3 1 10 Note Consider the first testcase: At first, _a_u2009=u2009{1,u20092,u20093,u20094,u20095}. After operation 1, _a_u2009=u2009{1,u20092,u20093,u20090,u20091}. After operation 2, _a_u2009=u2009{1,u20092,u20095,u20090,u20091}. At operation 3, 2u2009+u20095u2009+u20090u2009+u20091u2009=u20098. After operation 4, _a_u2009=u2009{1,u20092,u20092,u20090,u20091}. At operation 5, 1u2009+u20092u2009+u20092u2009=u20095.
2,300
true
false
false
false
true
false
false
false
false
false
8,086
1045D
In the intergalactic empire Bubbledom there are $$$N$$$ planets, of which some pairs are directly connected by two-way wormholes. There are $$$N-1$$$ wormholes. The wormholes are of extreme religious importance in Bubbledom, a set of planets in Bubbledom consider themselves one intergalactic kingdom if and only if any two planets in the set can reach each other by traversing the wormholes. You are given that Bubbledom is one kingdom. In other words, the network of planets and wormholes is a tree. However, Bubbledom is facing a powerful enemy also possessing teleportation technology. The enemy attacks every night, and the government of Bubbledom retakes all the planets during the day. In a single attack, the enemy attacks every planet of Bubbledom at once, but some planets are more resilient than others. Planets are number $$$0,1,…,N-1$$$ and the planet $$$i$$$ will fall with probability $$$p_i$$$. Before every night (including the very first one), the government reinforces or weakens the defenses of a single planet. The government of Bubbledom is interested in the following question: what is the expected number of intergalactic kingdoms Bubbledom will be split into, after a single enemy attack (before they get a chance to rebuild)? In other words, you need to print the expected number of connected components after every attack. Input The first line contains one integer number $$$N$$$ ($$$1 le N le 10^5$$$) denoting the number of planets in Bubbledom (numbered from $$$0$$$ to $$$N-1$$$). The next line contains $$$N$$$ different real numbers in the interval $$$[0,1]$$$, specified with 2 digits after the decimal point, denoting the probabilities that the corresponding planet will fall. The next $$$N-1$$$ lines contain all the wormholes in Bubbledom, where a wormhole is specified by the two planets it connects. The next line contains a positive integer $$$Q$$$ ($$$1 le Q le 10^5$$$), denoting the number of enemy attacks. The next $$$Q$$$ lines each contain a non-negative integer and a real number from interval $$$[0,1]$$$, denoting the planet the government of Bubbledom decided to reinforce or weaken, along with the new probability that the planet will fall. Output Output contains $$$Q$$$ numbers, each of which represents the expected number of kingdoms that are left after each enemy attack. Your answers will be considered correct if their absolute or relative error does not exceed $$$10^{-4}$$$. Example Input 5 0.50 0.29 0.49 0.95 0.83 2 3 0 3 3 4 2 1 3 4 0.66 1 0.69 0 0.36 Output 1.68040 1.48440 1.61740
2,200
true
false
false
false
false
false
false
false
false
false
5,467
1935D
The Master's Assistance Center has announced an entrance exam, which consists of the following. The candidate is given a set $$$s$$$ of size $$$n$$$ and some strange integer $$$c$$$. For this set, it is needed to calculate the number of pairs of integers $$$(x, y)$$$ such that $$$0 leq x leq y leq c$$$, $$$x + y$$$ is not contained in the set $$$s$$$, and also $$$y - x$$$ is not contained in the set $$$s$$$. Your friend wants to enter the Center. Help him pass the exam! Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2 cdot 10^4$$$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$c$$$ ($$$1 leq n leq 3 cdot 10^5$$$, $$$1 leq c leq 10^9$$$) β€” the size of the set and the strange integer. The second line of each test case contains $$$n$$$ integers $$$s_1, s_2, ldots, s_{n}$$$ ($$$0 leq s_1 < s_2 < ldots < s_{n} leq c$$$) β€” the elements of the set $$$s$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output a single integer β€” the number of suitable pairs of integers. Example Input 8 3 3 1 2 3 1 179 57 4 6 0 3 5 6 1 1 1 5 10 0 2 4 8 10 5 10 1 3 5 7 9 4 10 2 4 6 7 3 1000000000 228 1337 998244353 Output 3 16139 10 2 33 36 35 499999998999122959 Note In the first test case, the following pairs are suitable: $$$(0, 0)$$$, $$$(2, 2)$$$, $$$(3, 3)$$$. In the third test case, the following pairs are suitable: $$$(0, 1)$$$, $$$(0, 2)$$$, $$$(0, 4)$$$, $$$(1, 3)$$$, $$$(2, 6)$$$, $$$(3, 4)$$$, $$$(3, 5)$$$, $$$(4, 5)$$$, $$$(4, 6)$$$, $$$(5, 6)$$$.
1,800
true
false
true
false
false
false
false
true
false
false
671
1176E
You are given an undirected unweighted connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. It is guaranteed that there are no self-loops or multiple edges in the given graph. Your task is to choose at most $$$lfloorfrac{n}{2} floor$$$ vertices in this graph so each unchosen vertex is adjacent (in other words, connected by an edge) to at least one of chosen vertices. It is guaranteed that the answer exists. If there are multiple answers, you can print any. You will be given multiple independent queries to answer. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$) β€” the number of queries. Then $$$t$$$ queries follow. The first line of each query contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$n - 1 le m le min(2 cdot 10^5, frac{n(n-1)}{2})$$$) β€” the number of vertices and the number of edges, respectively. The following $$$m$$$ lines denote edges: edge $$$i$$$ is represented by a pair of integers $$$v_i$$$, $$$u_i$$$ ($$$1 le v_i, u_i le n$$$, $$$u_i e v_i$$$), which are the indices of vertices connected by the edge. There are no self-loops or multiple edges in the given graph, i.u2009e. for each pair ($$$v_i, u_i$$$) there are no other pairs ($$$v_i, u_i$$$) or ($$$u_i, v_i$$$) in the list of edges, and for each pair ($$$v_i, u_i$$$) the condition $$$v_i e u_i$$$ is satisfied. It is guaranteed that the given graph is connected. It is guaranteed that $$$sum m le 2 cdot 10^5$$$ over all queries. Output For each query print two lines. In the first line print $$$k$$$ ($$$1 le lfloorfrac{n}{2} floor$$$) β€” the number of chosen vertices. In the second line print $$$k$$$ distinct integers $$$c_1, c_2, dots, c_k$$$ in any order, where $$$c_i$$$ is the index of the $$$i$$$-th chosen vertex. It is guaranteed that the answer exists. If there are multiple answers, you can print any. Note In the first query any vertex or any pair of vertices will suffice. Note that you don't have to minimize the number of chosen vertices. In the second query two vertices can be enough (vertices $$$2$$$ and $$$4$$$) but three is also ok.
1,700
false
false
false
false
false
false
false
false
false
true
4,834
1089G
Berland State University invites people from all over the world as guest students. You can come to the capital of Berland and study with the best teachers in the country. Berland State University works every day of the week, but classes for guest students are held on the following schedule. You know the sequence of seven integers $$$a_1, a_2, dots, a_7$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$): $$$a_1=1$$$ if and only if there are classes for guest students on Sundays; $$$a_2=1$$$ if and only if there are classes for guest students on Mondays; ... $$$a_7=1$$$ if and only if there are classes for guest students on Saturdays. The classes for guest students are held in at least one day of a week. You want to visit the capital of Berland and spend the minimum number of days in it to study $$$k$$$ days as a guest student in Berland State University. Write a program to find the length of the shortest continuous period of days to stay in the capital to study exactly $$$k$$$ days as a guest student. Input The first line of the input contains integer $$$t$$$ ($$$1 le t le 10,000$$$) β€” the number of test cases to process. For each test case independently solve the problem and print the answer. Each test case consists of two lines. The first of them contains integer $$$k$$$ ($$$1 le k le 10^8$$$) β€” the required number of days to study as a guest student. The second line contains exactly seven integers $$$a_1, a_2, dots, a_7$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$) where $$$a_i=1$$$ if and only if classes for guest students are held on the $$$i$$$-th day of a week. Output Print $$$t$$$ lines, the $$$i$$$-th line should contain the answer for the $$$i$$$-th test case β€” the length of the shortest continuous period of days you need to stay to study exactly $$$k$$$ days as a guest student. Example Input 3 2 0 1 0 0 0 0 0 100000000 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 Note In the first test case you must arrive to the capital of Berland on Monday, have classes on this day, spend a week until next Monday and have classes on the next Monday. In total you need to spend $$$8$$$ days in the capital of Berland.
1,500
true
false
false
false
false
false
false
false
false
false
5,279
815E
It's been long after the events of the previous problems, and Karen has now moved on from student life and is looking to relocate to a new neighborhood. The neighborhood consists of _n_ houses in a straight line, labelled 1 to _n_ from left to right, all an equal distance apart. Everyone in this neighborhood loves peace and quiet. Because of this, whenever a new person moves into the neighborhood, he or she always chooses the house whose minimum distance to any occupied house is maximized. If there are multiple houses with the maximum possible minimum distance, he or she chooses the leftmost one. Note that the first person to arrive always moves into house 1. Karen is the _k_-th person to enter this neighborhood. If everyone, including herself, follows this rule, which house will she move into? Input The first and only line of input contains two integers, _n_ and _k_ (1u2009≀u2009_k_u2009≀u2009_n_u2009≀u20091018), describing the number of houses in the neighborhood, and that Karen was the _k_-th person to move in, respectively. Output Output a single integer on a line by itself, the label of the house Karen will move into. Note In the first test case, there are 6 houses in the neighborhood, and Karen is the fourth person to move in: 1. The first person moves into house 1. 2. The second person moves into house 6. 3. The third person moves into house 3. 4. The fourth person moves into house 2. In the second test case, there are 39 houses in the neighborhood, and Karen is the third person to move in: 1. The first person moves into house 1. 2. The second person moves into house 39. 3. The third person moves into house 20.
2,900
false
false
true
false
false
true
false
true
false
false
6,496
114B
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers _n_ (1u2009≀u2009_n_u2009≀u200916) β€” the number of volunteers, and _m_ () β€” the number of pairs that do not get on. Next _n_ lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next _m_ lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All _n_ names are distinct. Output The first output line should contain the single number _k_ β€” the number of people in the sought team. Next _k_ lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya
1,500
false
false
false
false
false
false
true
false
false
true
9,429
1394D
Since Boboniu finished building his Jianghu, he has been doing Kungfu on these mountains every day. Boboniu designs a map for his $$$n$$$ mountains. He uses $$$n-1$$$ roads to connect all $$$n$$$ mountains. Every pair of mountains is connected via roads. For the $$$i$$$-th mountain, Boboniu estimated the tiredness of doing Kungfu on the top of it as $$$t_i$$$. He also estimated the height of each mountain as $$$h_i$$$. A path is a sequence of mountains $$$M$$$ such that for each $$$i$$$ ($$$1 le i < M$$$), there exists a road between $$$M_i$$$ and $$$M_{i+1}$$$. Boboniu would regard the path as a challenge if for each $$$i$$$ ($$$1le i<M$$$), $$$h_{M_i}le h_{M_{i+1}}$$$. Boboniu wants to divide all $$$n-1$$$ roads into several challenges. Note that each road must appear in exactly one challenge, but a mountain may appear in several challenges. Boboniu wants to minimize the total tiredness to do all the challenges. The tiredness of a challenge $$$M$$$ is the sum of tiredness of all mountains in it, i.e. $$$sum_{i=1}^{M}t_{M_i}$$$. He asked you to find the minimum total tiredness. As a reward for your work, you'll become a guardian in his Jianghu. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$), denoting the number of the mountains. The second line contains $$$n$$$ integers $$$t_1, t_2, ldots, t_n$$$ ($$$1 le t_i le 10^6$$$), denoting the tiredness for Boboniu to do Kungfu on each mountain. The third line contains $$$n$$$ integers $$$h_1, h_2, ldots, h_n$$$ ($$$1 le h_i le 10^6$$$), denoting the height of each mountain. Each of the following $$$n - 1$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i, v_i le n, u_i eq v_i$$$), denoting the ends of the road. It's guaranteed that all mountains are connected via roads. Output Print one integer: the smallest sum of tiredness of all challenges. Examples Input 5 40 10 30 50 20 2 3 2 3 1 1 2 1 3 2 4 2 5 Input 5 1000000 1 1 1 1 1000000 1 1 1 1 1 2 1 3 1 4 1 5 Input 10 510916 760492 684704 32545 484888 933975 116895 77095 127679 989957 402815 705067 705067 705067 623759 103335 749243 138306 138306 844737 1 2 3 2 4 3 1 5 6 4 6 7 8 7 8 9 9 10 Note For the first example: In the picture, the lighter a point is, the higher the mountain it represents. One of the best divisions is: Challenge $$$1$$$: $$$3 o 1 o 2$$$ Challenge $$$2$$$: $$$5 o 2 o 4$$$ The total tiredness of Boboniu is $$$(30 + 40 + 10) + (20 + 10 + 50) = 160$$$. It can be shown that this is the minimum total tiredness.
2,800
false
true
false
true
false
false
false
false
true
false
3,698
853D
Michael has just bought a new electric car for moving across city. Michael does not like to overwork, so each day he drives to only one of two his jobs. Michael's day starts from charging his electric car for getting to the work and back. He spends 1000 burles on charge if he goes to the first job, and 2000 burles if he goes to the second job. On a charging station he uses there is a loyalty program that involves bonus cards. Bonus card may have some non-negative amount of bonus burles. Each time customer is going to buy something for the price of _x_ burles, he is allowed to pay an amount of _y_ (0u2009≀u2009_y_u2009≀u2009_x_) burles that does not exceed the bonus card balance with bonus burles. In this case he pays _x_u2009-u2009_y_ burles with cash, and the balance on the bonus card is decreased by _y_ bonus burles. If customer pays whole price with cash (i.e., _y_u2009=u20090) then 10% of price is returned back to the bonus card. This means that bonus card balance increases by bonus burles. Initially the bonus card balance is equal to 0 bonus burles. Michael has planned next _n_ days and he knows how much does the charge cost on each of those days. Help Michael determine the minimum amount of burles in cash he has to spend with optimal use of bonus card. Assume that Michael is able to cover any part of the price with cash in any day. It is not necessary to spend all bonus burles at the end of the given period. Input The first line of input contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009300u2009000), the number of days Michael has planned. Next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (_a__i_u2009=u20091000 or _a__i_u2009=u20092000) with _a__i_ denoting the charging cost at the day _i_. Output Output the minimum amount of burles Michael has to spend. Examples Input 6 2000 2000 2000 2000 2000 1000 Note In the first sample case the most optimal way for Michael is to pay for the first two days spending 3000 burles and get 300 bonus burles as return. After that he is able to pay only 700 burles for the third days, covering the rest of the price with bonus burles. In the second sample case the most optimal way for Michael is to pay the whole price for the first five days, getting 1000 bonus burles as return and being able to use them on the last day without paying anything in cash.
2,400
false
true
false
true
false
false
false
true
false
false
6,331
291A
Polycarpus is the director of a large corporation. There are _n_ secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number. One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment. Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so. Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u2009103) β€” the number of secretaries in Polycarpus's corporation. The next line contains _n_ space-separated integers: _id_1,u2009_id_2,u2009...,u2009_id__n_ (0u2009≀u2009_id__i_u2009≀u2009109). Number _id__i_ equals the number of the call session of the _i_-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to _n_ in some way. Output Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place. Note In the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5. In the second test sample the described situation is impossible as conferences aren't allowed.
800
false
false
true
false
false
false
false
false
true
false
8,671
1739C
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$. Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on. On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn. Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i.u2009e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris. For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
1,500
false
false
false
true
false
true
false
false
false
false
1,851
1917A
You are given an array of integers $$$a_1, a_2, dots, a_n$$$. You can perform the following operation any number of times (possibly zero): Choose any element $$$a_i$$$ from the array and change its value to any integer between $$$0$$$ and $$$a_i$$$ (inclusive). More formally, if $$$a_i < 0$$$, replace $$$a_i$$$ with any integer in $$$[a_i, 0]$$$, otherwise replace $$$a_i$$$ with any integer in $$$[0, a_i]$$$. Let $$$r$$$ be the minimum possible product of all the $$$a_i$$$ after performing the operation any number of times. Find the minimum number of operations required to make the product equal to $$$r$$$. Also, print one such shortest sequence of operations. If there are multiple answers, you can print any of them. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 500$$$) - the number of test cases. This is followed by their description. The first line of each test case contains the a single integer $$$n$$$ ($$$1 leq n leq 100$$$) β€” the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$). Output For each test case: The first line must contain the minimum number of operations $$$k$$$ ($$$0 leq k leq n$$$). The $$$j$$$-th of the next $$$k$$$ lines must contain two integers $$$i$$$ and $$$x$$$, which represent the $$$j$$$-th operation. That operation consists in replacing $$$a_i$$$ with $$$x$$$. Example Input 4 1 155 4 2 8 -1 3 4 -1 0 -2 -5 4 -15 -75 -25 -30 Note In the first test case, we can change the value of the first integer into $$$0$$$ and the product will become $$$0$$$, which is the minimum possible. In the second test case, initially, the product of integers is equal to $$$2 cdot 8 cdot (-1) cdot 3 = -48$$$ which is the minimum possible, so we should do nothing in this case.
800
true
false
false
false
false
true
false
false
false
false
794
371C
Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has _n__b_ pieces of bread, _n__s_ pieces of sausage and _n__c_ pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are _p__b_ rubles for a piece of bread, _p__s_ for a piece of sausage and _p__c_ for a piece of cheese. Polycarpus has _r_ rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers _n__b_, _n__s_, _n__c_ (1u2009≀u2009_n__b_,u2009_n__s_,u2009_n__c_u2009≀u2009100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers _p__b_, _p__s_, _p__c_ (1u2009≀u2009_p__b_,u2009_p__s_,u2009_p__c_u2009≀u2009100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer _r_ (1u2009≀u2009_r_u2009≀u20091012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BSC 1 1 1 1 1 3 1000000000000
1,600
false
false
false
false
false
false
true
true
false
false
8,346
1182A
Problem - 1182A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags dp math *1000 No tag edit access β†’ Contest materials xa0β€” the length. Output Print the number of ways to fill. Examples Input 4 Output 4 Input 1 Output 0 Note In the first example, there are $$$4$$$ possible cases of filling. In the second example, you cannot fill the shapes in $$$3 imes 1$$$ tiles.
1,000
true
false
false
true
false
false
false
false
false
false
4,808
1080F
It is a very important day for Katya. She has a test in a programming class. As always, she was given an interesting problem that she solved very fast. Can you solve that problem? You are given $$$n$$$ ordered segments sets. Each segment can be represented as a pair of two integers $$$[l, r]$$$ where $$$lleq r$$$. Each set can contain an arbitrary number of segments (even $$$0$$$). It is possible that some segments are equal. You are also given $$$m$$$ queries, each of them can be represented as four numbers: $$$a, b, x, y$$$. For each segment, find out whether it is true that each set $$$p$$$ ($$$aleq pleq b$$$) contains at least one segment $$$[l, r]$$$ that lies entirely on the segment $$$[x, y]$$$, that is $$$xleq lleq rleq y$$$. Find out the answer to each query. Note that you need to solve this problem online. That is, you will get a new query only after you print the answer for the previous query. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ $$$(1leq n,mleq 10^5, 1leq kleq 3cdot10^5)$$$xa0β€” the number of sets, queries, and segments respectively. Each of the next $$$k$$$ lines contains three integers $$$l$$$, $$$r$$$, and $$$p$$$ $$$(1leq lleq rleq 10^9, 1leq pleq n)$$$xa0β€” the limits of the segment and the index of a set, to which this segment belongs. Each of the next $$$m$$$ lines contains four integers $$$a, b, x, y$$$ $$$(1leq aleq bleq n, 1leq xleq yleq 10^9)$$$xa0β€” the description of the query. Output For each query, print "yes" or "no" in a new line. Interaction After printing a query, do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Example Input 5 5 9 3 6 3 1 3 1 2 4 2 1 2 3 4 6 5 2 5 3 7 9 4 2 3 1 4 10 4 1 2 2 3 1 2 2 4 1 3 1 5 2 3 3 6 2 4 2 9 Note For the first query, the answer is negative since the second set does not contain a segment that lies on the segment $$$[2, 3]$$$. In the second query, the first set contains $$$[2, 3]$$$, and the second set contains $$$[2, 4]$$$. In the third query, the first set contains $$$[2, 3]$$$, the second set contains $$$[2, 4]$$$, and the third set contains $$$[2, 5]$$$. In the fourth query, the second set does not contain a segment that lies on the segment $$$[3, 6]$$$. In the fifth query, the second set contains $$$[2, 4]$$$, the third set contains $$$[2, 5]$$$, and the fourth contains $$$[7, 9]$$$.
2,400
false
false
false
false
true
false
false
false
true
false
5,324
760A
Problem - 760A - Codeforces =============== xa0 (8VC Venture Cup 2017 - Final Round Div. 2 Edition)]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags implementation math *800 No tag edit access β†’ Contest materials , rows correspond to weekdays, and cells contain dates. For example, a calendar for January 2017 should look like on the picture: Petr wants to know how many columns his table should have given the month and the weekday of the first date of that month? Assume that the year is non-leap. Input The only line contain two integers _m_ and _d_ (1u2009≀u2009_m_u2009≀u200912, 1u2009≀u2009_d_u2009≀u20097)xa0β€” the number of month (January is the first month, December is the twelfth) and the weekday of the first date of this month (1 is Monday, 7 is Sunday). Output Print single integer: the number of columns the table should have. Examples Input 1 7 Output 6 Input 1 1 Output 5 Input 11 6 Output 5 Note The first example corresponds to the January 2017 shown on the picture in the statements. In the second example 1-st January is Monday, so the whole month fits into 5 columns. In the third example 1-st November is Saturday and 5 columns is enough.
800
true
false
true
false
false
false
false
false
false
false
6,751
1513A
A sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once. Given two integers $$$n$$$ and $$$k$$$, construct a permutation $$$a$$$ of numbers from $$$1$$$ to $$$n$$$ which has exactly $$$k$$$ peaks. An index $$$i$$$ of an array $$$a$$$ of size $$$n$$$ is said to be a peak if $$$1 < i < n$$$ and $$$a_i gt a_{i-1}$$$ and $$$a_i gt a_{i+1}$$$. If such permutation is not possible, then print $$$-1$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0β€” the number of test cases. Then $$$t$$$ lines follow, each containing two space-separated integers $$$n$$$ ($$$1 leq n leq 100$$$) and $$$k$$$ ($$$0 leq k leq n$$$)xa0β€” the length of an array and the required number of peaks. Output Output $$$t$$$ lines. For each test case, if there is no permutation with given length and number of peaks, then print $$$-1$$$. Otherwise print a line containing $$$n$$$ space-separated integers which forms a permutation of numbers from $$$1$$$ to $$$n$$$ and contains exactly $$$k$$$ peaks. If there are multiple answers, print any. Example Input 5 1 0 5 2 6 6 2 1 6 1 Output 1 2 4 1 5 3 -1 -1 1 3 6 5 4 2 Note In the second test case of the example, we have array $$$a = [2,4,1,5,3]$$$. Here, indices $$$i=2$$$ and $$$i=4$$$ are the peaks of the array. This is because $$$(a_{2} gt a_{1} $$$, $$$a_{2} gt a_{3})$$$ and $$$(a_{4} gt a_{3}$$$, $$$a_{4} gt a_{5})$$$.
800
false
false
true
false
false
true
false
false
false
false
3,102
552E
Problem - 552E - 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 dp expression parsing greedy implementation strings *2100 No tag edit access β†’ Contest materials ") Editorial ") , its odd positions only contain digits from 1 to 9, and even positions only contain signs u2009+u2009 and u2009*u2009. The number of signs u2009*u2009 doesn't exceed 15. Output In the first line print the maximum possible value of an expression. Examples Input 3+5*7+8*4 Output 303 Input 2+3*5 Output 25 Input 3*4*5 Output 60 Note Note to the first sample test. 3u2009+u20095u2009*u2009(7u2009+u20098)u2009*u20094u2009=u2009303. Note to the second sample test. (2u2009+u20093)u2009*u20095u2009=u200925. Note to the third sample test. (3u2009*u20094)u2009*u20095u2009=u200960 (also many other variants are valid, for instance, (3)u2009*u20094u2009*u20095u2009=u200960).
2,100
false
true
true
true
false
false
true
false
false
false
7,623
1560C
Polycarp has found a table having an infinite number of rows and columns. The rows are numbered from $$$1$$$, starting from the topmost one. The columns are numbered from $$$1$$$, starting from the leftmost one. Initially, the table hasn't been filled and Polycarp wants to fix it. He writes integers from $$$1$$$ and so on to the table as follows. The figure shows the placement of the numbers from $$$1$$$ to $$$10$$$. The following actions are denoted by the arrows. The leftmost topmost cell of the table is filled with the number $$$1$$$. Then he writes in the table all positive integers beginning from $$$2$$$ sequentially using the following algorithm. First, Polycarp selects the leftmost non-filled cell in the first row and fills it. Then, while the left neighbor of the last filled cell is filled, he goes down and fills the next cell. So he goes down until the last filled cell has a non-filled neighbor to the left (look at the vertical arrow going down in the figure above). After that, he fills the cells from the right to the left until he stops at the first column (look at the horizontal row in the figure above). Then Polycarp selects the leftmost non-filled cell in the first row, goes down, and so on. A friend of Polycarp has a favorite number $$$k$$$. He wants to know which cell will contain the number. Help him to find the indices of the row and the column, such that the intersection of the row and the column is the cell containing the number $$$k$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$k$$$ ($$$1 le k le 10^9$$$) which location must be found. Output For each test case, output in a separate line two integers $$$r$$$ and $$$c$$$ ($$$r, c ge 1$$$) separated by spaces β€” the indices of the row and the column containing the cell filled by the number $$$k$$$, respectively.
800
true
false
true
false
false
false
false
false
false
false
2,828
938G
You are given an undirected connected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). There are three types of queries you have to process: 1 _x_ _y_ _d_ β€” add an edge connecting vertex _x_ to vertex _y_ with weight _d_. It is guaranteed that there is no edge connecting _x_ to _y_ before this query; 2 _x_ _y_ β€” remove an edge connecting vertex _x_ to vertex _y_. It is guaranteed that there was such edge in the graph, and the graph stays connected after this query; 3 _x_ _y_ β€” calculate the length of the shortest path (possibly non-simple) from vertex _x_ to vertex _y_. Print the answers for all queries of type 3. Input The first line contains two numbers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009200000) β€” the number of vertices and the number of edges in the graph, respectively. Then _m_ lines follow denoting the edges of the graph. Each line contains three integers _x_, _y_ and _d_ (1u2009≀u2009_x_u2009<u2009_y_u2009≀u2009_n_, 0u2009≀u2009_d_u2009≀u2009230u2009-u20091). Each pair (_x_,u2009_y_) is listed at most once. The initial graph is connected. Then one line follows, containing an integer _q_ (1u2009≀u2009_q_u2009≀u2009200000) β€” the number of queries you have to process. Then _q_ lines follow, denoting queries in the following form: 1 _x_ _y_ _d_ (1u2009≀u2009_x_u2009<u2009_y_u2009≀u2009_n_, 0u2009≀u2009_d_u2009≀u2009230u2009-u20091) β€” add an edge connecting vertex _x_ to vertex _y_ with weight _d_. It is guaranteed that there is no edge connecting _x_ to _y_ before this query; 2 _x_ _y_ (1u2009≀u2009_x_u2009<u2009_y_u2009≀u2009_n_) β€” remove an edge connecting vertex _x_ to vertex _y_. It is guaranteed that there was such edge in the graph, and the graph stays connected after this query; 3 _x_ _y_ (1u2009≀u2009_x_u2009<u2009_y_u2009≀u2009_n_) β€” calculate the length of the shortest path (possibly non-simple) from vertex _x_ to vertex _y_. It is guaranteed that at least one query has type 3. Output Print the answers for all queries of type 3 in the order they appear in input. Example Input 5 5 1 2 3 2 3 4 3 4 5 4 5 6 1 5 1 5 3 1 5 1 1 3 1 3 1 5 2 1 5 3 1 5
2,900
false
false
false
false
true
false
false
false
false
true
5,945
1028G
This problem is interactive. You should guess hidden number $$$x$$$ which is between $$$1$$$ and $$$M = 10004205361450474$$$, inclusive. You could use up to $$$5$$$ queries. In each query, you can output an increasing sequence of $$$k leq x$$$ integers, each between $$$1$$$ and $$$M$$$, inclusive, and you will obtain one of the following as an answer: either the hidden number belongs to your query sequence, in this case you immediately win; or you will be given where the hidden number is located with respect to your query sequence, that is, either it is less than all numbers from the sequence, greater than all numbers from the sequence, or you will be given such an $$$i$$$ that the hidden number $$$x$$$ is between the $$$i$$$-th and the $$$(i+1)$$$-st numbers of your sequence. See the interaction section for clarity. Be aware that the interactor is adaptive, i.e. the hidden number can depend on queries the solution makes. However, it is guaranteed that for any solution the interactor works non-distinguishable from the situation when the hidden number is fixed beforehand. Hacks are allowed only with fixed hidden number. A hack is represented by a single integer between $$$1$$$ and $$$M$$$. In all pretests the hidden number is fixed as well. Interaction You can make up to $$$5$$$ queries. To make a query print a number $$$k$$$ ($$$1 le k le 10^4$$$) and then an increasing sequence $$$t_0 < t_1 < ldots < t_{k-1}$$$ of $$$k$$$ numbers, each between $$$1$$$ and $$$M$$$, inclusive. If $$$k > x$$$, you lose. You get one integer as a response. If it is $$$-2$$$, it means you made an invalid query or you lost. Exit immediately after receiving $$$-2$$$ and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. If it is $$$-1$$$, you guessed the number and should terminate too. Otherwise you get a number $$$i$$$ between $$$0$$$ and $$$k$$$, inclusive, denoting where the hidden number is, with respect to the printed numbers. If $$$i = 0$$$, then $$$x < t_0$$$. If $$$i = k$$$, then $$$t_{k-1} < x$$$. Otherwise $$$t_{i-1} < x < t_i$$$. After printing a query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Example Output 2 2 32 20 30 3 5 7 9 Note In the first example the number $$$5$$$ is hidden.
3,000
false
false
false
true
false
false
false
false
false
false
5,563
1264D1
This is the easy version of this problem. The only difference is the limit of $$$n$$$ - the length of the input string. In this version, $$$1 leq n leq 2000$$$. The hard version of this challenge is not offered in the round for the second division. Let's define a correct bracket sequence and its depth as follow: An empty string is a correct bracket sequence with depth $$$0$$$. If "s" is a correct bracket sequence with depth $$$d$$$ then "(s)" is a correct bracket sequence with depth $$$d + 1$$$. If "s" and "t" are both correct bracket sequences then their concatenation "st" is a correct bracket sequence with depth equal to the maximum depth of $$$s$$$ and $$$t$$$. For a (not necessarily correct) bracket sequence $$$s$$$, we define its depth as the maximum depth of any correct bracket sequence induced by removing some characters from $$$s$$$ (possibly zero). For example: the bracket sequence $$$s = $$$"())(())" has depth $$$2$$$, because by removing the third character we obtain a correct bracket sequence "()(())" with depth $$$2$$$. Given a string $$$a$$$ consists of only characters '(', ')' and '?'. Consider all (not necessarily correct) bracket sequences obtained by replacing all characters '?' in $$$a$$$ by either '(' or ')'. Calculate the sum of all the depths of all these bracket sequences. As this number can be large, find it modulo $$$998244353$$$. Hacks in this problem in the first division can be done only if easy and hard versions of this problem was solved. Input The only line contains a non-empty string consist of only '(', ')' and '?'. The length of the string is at most $$$2000$$$. Output Print the answer modulo $$$998244353$$$ in a single line. Note In the first test case, we can obtain $$$4$$$ bracket sequences by replacing all characters '?' with either '(' or ')': "((". Its depth is $$$0$$$; "))". Its depth is $$$0$$$; ")(". Its depth is $$$0$$$; "()". Its depth is $$$1$$$. So, the answer is $$$1 = 0 + 0 + 0 + 1$$$. In the second test case, we can obtain $$$4$$$ bracket sequences by replacing all characters '?' with either '(' or ')': "(((())". Its depth is $$$2$$$; "()()))". Its depth is $$$2$$$; "((()))". Its depth is $$$3$$$; "()(())". Its depth is $$$2$$$. So, the answer is $$$9 = 2 + 2 + 3 + 2$$$.
2,600
false
false
false
true
false
false
false
false
false
false
4,378
1473D
You are given a program that consists of $$$n$$$ instructions. Initially a single variable $$$x$$$ is assigned to $$$0$$$. Afterwards, the instructions are of two types: increase $$$x$$$ by $$$1$$$; decrease $$$x$$$ by $$$1$$$. You are given $$$m$$$ queries of the following format: query $$$l$$$ $$$r$$$xa0β€” how many distinct values is $$$x$$$ assigned to if all the instructions between the $$$l$$$-th one and the $$$r$$$-th one inclusive are ignored and the rest are executed without changing the order? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of testcases. Then the description of $$$t$$$ testcases follows. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$)xa0β€” the number of instructions in the program and the number of queries. The second line of each testcase contains a programxa0β€” a string of $$$n$$$ characters: each character is either '+' or '-'xa0β€” increment and decrement instruction, respectively. Each of the next $$$m$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$)xa0β€” the description of the query. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase print $$$m$$$ integersxa0β€” for each query $$$l$$$, $$$r$$$ print the number of distinct values variable $$$x$$$ is assigned to if all the instructions between the $$$l$$$-th one and the $$$r$$$-th one inclusive are ignored and the rest are executed without changing the order. Example Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 Note The instructions that remain for each query of the first testcase are: 1. empty programxa0β€” $$$x$$$ was only equal to $$$0$$$; 2. "-"xa0β€” $$$x$$$ had values $$$0$$$ and $$$-1$$$; 3. "---+"xa0β€” $$$x$$$ had values $$$0$$$, $$$-1$$$, $$$-2$$$, $$$-3$$$, $$$-2$$$xa0β€” there are $$$4$$$ distinct values among them; 4. "+--+--+"xa0β€” the distinct values are $$$1$$$, $$$0$$$, $$$-1$$$, $$$-2$$$.
1,700
false
false
true
true
true
false
false
false
false
false
3,314
1430E
Problem - 1430E - 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 greedy strings *1900 No tag edit access β†’ Contest materials β€” the length of $$$s$$$. The second line contains $$$s$$$ β€” a string consisting of $$$n$$$ lowercase Latin letters. Output Print one integer β€” the minimum number of swaps of neighboring elements you have to perform to reverse the string. Examples Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 Note In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
1,900
false
true
false
false
true
false
false
false
false
false
3,512
1669E
Given $$$n$$$ strings, each of length $$$2$$$, consisting of lowercase Latin alphabet letters from 'a' to 'k', output the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and the $$$i$$$-th string and the $$$j$$$-th string differ in exactly one position. In other words, count the number of pairs $$$(i, j)$$$ ($$$i < j$$$) such that the $$$i$$$-th string and the $$$j$$$-th string have exactly one position $$$p$$$ ($$$1 leq p leq 2$$$) such that $$${s_{i}}_{p} eq {s_{j}}_{p}$$$. The answer may not fit into 32-bit integer type, so you should use 64-bit integers like long long in C++ to avoid integer overflow. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) β€” the number of strings. Then follows $$$n$$$ lines, the $$$i$$$-th of which containing a single string $$$s_i$$$ of length $$$2$$$, consisting of lowercase Latin letters from 'a' to 'k'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print a single integer β€” the number of pairs $$$(i, j)$$$ ($$$i < j$$$) such that the $$$i$$$-th string and the $$$j$$$-th string have exactly one position $$$p$$$ ($$$1 leq p leq 2$$$) such that $$${s_{i}}_{p} eq {s_{j}}_{p}$$$. Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). Example Input 4 6 ab cb db aa cc ef 7 aa bb cc ac ca bb aa 4 kk kk ab ab 5 jf jf jk jk jk Note For the first test case the pairs that differ in exactly one position are: ("ab", "cb"), ("ab", "db"), ("ab", "aa"), ("cb", "db") and ("cb", "cc"). For the second test case the pairs that differ in exactly one position are: ("aa", "ac"), ("aa", "ca"), ("cc", "ac"), ("cc", "ca"), ("ac", "aa") and ("ca", "aa"). For the third test case, the are no pairs satisfying the conditions.
1,200
true
false
false
false
true
false
false
false
false
false
2,261
1807B
Mihai and Bianca are playing with bags of candies. They have a row $$$a$$$ of $$$n$$$ bags of candies. The $$$i$$$-th bag has $$$a_i$$$ candies. The bags are given to the players in the order from the first bag to the $$$n$$$-th bag. If a bag has an even number of candies, Mihai grabs the bag. Otherwise, Bianca grabs the bag. Once a bag is grabbed, the number of candies in it gets added to the total number of candies of the player that took it. Mihai wants to show off, so he wants to reorder the array so that at any moment (except at the start when they both have no candies), Mihai will have strictly more candies than Bianca. Help Mihai find out if such a reordering exists. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)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 number of bags in the array. The second line of each test case contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$1 leq a_i leq 100$$$)xa0β€” the number of candies in each bag. Output For each test case, output "YES" (without quotes) if such a reordering exists, and "NO" (without quotes) otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 3 4 1 2 3 4 4 1 1 1 2 3 1 4 3 Note In the first test case, Mihai can reorder the array as follows: $$$[4, 1, 2, 3]$$$. Then the process proceeds as follows: the first bag has $$$4$$$ candies, which is even, so Mihai takes itxa0β€” Mihai has $$$4$$$ candies, and Bianca has $$$0$$$. the second bag has $$$1$$$ candies, which is odd, so Bianca takes itxa0β€” Mihai has $$$4$$$ candies, and Bianca has $$$1$$$. the third bag has $$$2$$$ candies, which is even, so Mihai takes itxa0β€” Mihai has $$$6$$$ candies, and Bianca has $$$1$$$. the fourth bag has $$$3$$$ candies, which is odd, so Bianca takes itxa0β€” Mihai has $$$6$$$ candies, and Bianca has $$$4$$$. Since Mihai always has more candies than Bianca, this reordering works.
800
false
true
false
false
false
false
false
false
false
false
1,434
37A
Problem - 37A - Codeforces =============== xa0 ") β€” the number of bars at Vasya’s disposal. The second line contains _N_ space-separated integers _l__i_ β€” the lengths of the bars. All the lengths are natural numbers not exceeding 1000. Output In one line output two numbers β€” the height of the largest tower and their total number. Remember that Vasya should use all the bars. Examples Input 3 1 2 3 Output 1 3 Input 4 6 5 6 7 Output 2 3
1,000
false
false
false
false
false
false
false
false
true
false
9,813
1440B
A median of an array of integers of length $$$n$$$ is the number standing on the $$$lceil {frac{n}{2}} ceil$$$ (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting with $$$1$$$. For example, a median of the array $$$[2, 6, 4, 1, 3, 5]$$$ is equal to $$$3$$$. There exist some other definitions of the median, but in this problem, we will use the described one. Given two integers $$$n$$$ and $$$k$$$ and non-decreasing array of $$$nk$$$ integers. Divide all numbers into $$$k$$$ arrays of size $$$n$$$, such that each number belongs to exactly one array. You want the sum of medians of all $$$k$$$ arrays to be the maximum possible. Find this maximum possible sum. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0β€” the number of test cases. The next $$$2t$$$ lines contain descriptions of test cases. The first line of the description of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 leq n, k leq 1000$$$). The second line of the description of each test case contains $$$nk$$$ integers $$$a_1, a_2, ldots, a_{nk}$$$ ($$$0 leq a_i leq 10^9$$$)xa0β€” given array. It is guaranteed that the array is non-decreasing: $$$a_1 leq a_2 leq ldots leq a_{nk}$$$. It is guaranteed that the sum of $$$nk$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Note The examples of possible divisions into arrays for all test cases of the first test: Test case $$$1$$$: $$$[0, 24], [34, 58], [62, 64], [69, 78]$$$. The medians are $$$0, 34, 62, 69$$$. Their sum is $$$165$$$. Test case $$$2$$$: $$$[27, 61], [81, 91]$$$. The medians are $$$27, 81$$$. Their sum is $$$108$$$. Test case $$$3$$$: $$$[2, 91, 92, 95], [4, 36, 53, 82], [16, 18, 21, 27]$$$. The medians are $$$91, 36, 18$$$. Their sum is $$$145$$$. Test case $$$4$$$: $$$[3, 33, 35], [11, 94, 99], [12, 38, 67], [22, 69, 71]$$$. The medians are $$$33, 94, 38, 69$$$. Their sum is $$$234$$$. Test case $$$5$$$: $$$[11, 41]$$$. The median is $$$11$$$. The sum of the only median is $$$11$$$. Test case $$$6$$$: $$$[1, 1, 1], [1, 1, 1], [1, 1, 1]$$$. The medians are $$$1, 1, 1$$$. Their sum is $$$3$$$.
900
true
true
false
false
false
false
false
false
false
false
3,465
418E
In order to ensure confidentiality, the access to the "Russian Code Cup" problems is password protected during the problem development process. To select a password, the jury can generate a special table that contains _n_ columns and the infinite number of rows. To construct a table, the first row is fixed, and all the others are obtained by the following rule: In the row _i_ at position _p_ there is a number equal to the number of times _a_[_i_u2009-u20091][_p_] occurs on the prefix _a_[_i_u2009-u20091][1... _p_]. To ensure the required level of confidentiality, the jury must be able to perform the following operations: Replace number _a_[1][_p_] by _v_ and rebuild the table. Find the number _a_[_x_][_y_], which will be the new password. Doing all these steps manually is very tedious, so the jury asks you to help him. Write a program that responds to the request of the jury. Input The first line contains an integer _n_ (1u2009≀u2009_n_u2009≀u2009100000) β€” the number of columns. The second line contains the description of the first row of the table, that is, _n_ integers, which are not less than 1 and do not exceed 109. The third line of the input contains an integer _m_ (1u2009≀u2009_m_u2009≀u2009100000) β€” the number of requests. Next, each row contains a description of the request, which consists of three integers: If the first number is equal to 1, then the remaining two numbers are _v_, _p_ (1u2009≀u2009_v_u2009≀u2009109; 1u2009≀u2009_p_u2009≀u2009_n_). So, you should put value _v_ in the position _p_ in the first row. If the first number is equal to 2, then the remaining two numbers are _x_, _y_ (1u2009≀u2009_x_u2009≀u2009105; 1u2009≀u2009_y_u2009≀u2009_n_) β€” the row and column of the table cell from which you want to get value. Output Print an answer for each request of the second type in the order you receive them. Examples Input 6 1 2 2 2 3 1 3 2 2 3 1 3 3 2 3 4
3,100
false
false
false
false
true
false
false
false
false
false
8,151
601D
You are given a tree _T_ with _n_ vertices (numbered 1 through _n_) and a letter in each vertex. The tree is rooted at vertex 1. Let's look at the subtree _T__v_ of some vertex _v_. It is possible to read a string along each simple path starting at _v_ and ending at some vertex in _T__v_ (possibly _v_ itself). Let's denote the number of distinct strings which can be read this way as . Also, there's a number _c__v_ assigned to each vertex _v_. We are interested in vertices with the maximum value of . You should compute two statistics: the maximum value of and the number of vertices _v_ with the maximum . Input The first line of the input contains one integer _n_ (1u2009≀u2009_n_u2009≀u2009300u2009000)xa0β€” the number of vertices of the tree. The second line contains _n_ space-separated integers _c__i_ (0u2009≀u2009_c__i_u2009≀u2009109). The third line contains a string _s_ consisting of _n_ lowercase English lettersxa0β€” the _i_-th character of this string is the letter in vertex _i_. The following _n_u2009-u20091 lines describe the tree _T_. Each of them contains two space-separated integers _u_ and _v_ (1u2009≀u2009_u_,u2009_v_u2009≀u2009_n_) indicating an edge between vertices _u_ and _v_. It's guaranteed that the input will describe a tree. Output Print two lines. On the first line, print over all 1u2009≀u2009_i_u2009≀u2009_n_. On the second line, print the number of vertices _v_ for which . Examples Input 10 1 2 7 20 20 30 40 50 50 50 cacabbcddd 1 2 6 8 7 2 6 2 5 4 5 9 3 10 2 5 2 3 Input 6 0 2 4 1 1 1 raaaba 1 2 2 3 2 4 2 5 3 6 Note In the first sample, the tree looks like this: The sets of strings that can be read from individual vertices are: Finally, the values of are: In the second sample, the values of are (5,u20094,u20092,u20091,u20091,u20091). The distinct strings read in _T_2 are ; note that can be read down to vertices 3 or 4.
2,400
false
false
false
false
true
false
false
false
false
false
7,437
986F
Surely you have seen insane videos by South Korean rapper PSY, such as "Gangnam Style", "Gentleman" and "Daddy". You might also hear that PSY has been recording video "Oppa Funcan Style" two years ago (unfortunately we couldn't find it on the internet). We will remind you what this hit looked like (you can find original description $$$. The moving rule $$$f$$$ is selected in advance and is not changed throughout the clip. The duration of the clip was $$$k$$$ seconds and the rule $$$f$$$ was chosen in such a way that after $$$k$$$ seconds all dancers were in their initial positions (i.e. the $$$i$$$-th dancer stood on the platform with the number $$$i$$$). That allowed to loop the clip and collect even more likes. PSY knows that enhanced versions of old artworks become more and more popular every day. So he decided to release a remastered-version of his video. In his case "enhanced version" means even more insanity, so the number of platforms can be up to $$$10^{18}$$$! But the video director said that if some dancer stays on the same platform all the time, then the viewer will get bored and will turn off the video immediately. Therefore, for all $$$x$$$ from $$$1$$$ to $$$n$$$ $$$f(x) eq x$$$ must hold. Big part of classic video's success was in that looping, so in the remastered version all dancers should return to their initial positions in the end of the clip as well. PSY hasn't decided on the exact number of platforms and video duration yet, so he asks you to check if there is a good rule $$$f$$$ for different options. Input In the first line of input there is one integer $$$t$$$ ($$$1 le t le 10^{4}$$$)xa0β€” the number of options for $$$n$$$ and $$$k$$$ to check. In the next $$$t$$$ lines the options are given: each option is described with two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^{18}$$$, $$$1 le k le 10^{15}$$$)xa0β€” the number of dancers and the duration in seconds. It is guaranteed that the number of different values of $$$k$$$ in one test is not greater than $$$50$$$. Output Print $$$t$$$ lines. If the $$$i$$$-th option of the video is feasible, print "YES" (without quotes) in $$$i$$$-th line, otherwise print "NO" (without quotes).
3,300
true
false
false
false
false
false
false
false
false
true
5,767
1876B
Chaneka has an array $$$[a_1,a_2,ldots,a_n]$$$. Initially, all elements are white. Chaneka will choose one or more different indices and colour the elements at those chosen indices black. Then, she will choose all white elements whose indices are multiples of the index of at least one black element and colour those elements green. After that, her score is the maximum value of $$$a_i$$$ out of all black and green elements. There are $$$2^n-1$$$ ways for Chaneka to choose the black indices. Find the sum of scores for all possible ways Chaneka can choose the black indices. Since the answer can be very big, print the answer modulo $$$998,244,353$$$. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) β€” the size of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,a_3,ldots,a_n$$$ ($$$0leq a_ileq10^5$$$). Output An integer representing the sum of scores for all possible ways Chaneka can choose the black indices, modulo $$$998,244,353$$$. Examples Input 15 90000 9000 99000 900 90900 9900 99900 90 90090 9090 99090 990 90990 9990 99990 Note In the first example, below are the $$$15$$$ possible ways to choose the black indices: Index $$$1$$$ is black. Indices $$$2$$$, $$$3$$$, and $$$4$$$ are green. Maximum value among them is $$$19$$$. Index $$$2$$$ is black. Index $$$4$$$ is green. Maximum value among them is $$$14$$$. Index $$$3$$$ is black. Maximum value among them is $$$19$$$. Index $$$4$$$ is black. Maximum value among them is $$$9$$$. Indices $$$1$$$ and $$$2$$$ are black. Indices $$$3$$$ and $$$4$$$ are green. Maximum value among them is $$$19$$$. Indices $$$1$$$ and $$$3$$$ are black. Indices $$$2$$$ and $$$4$$$ are green. Maximum value among them is $$$19$$$. Indices $$$1$$$ and $$$4$$$ are black. Indices $$$2$$$ and $$$3$$$ are green. Maximum value among them is $$$19$$$. Indices $$$2$$$ and $$$3$$$ are black. Index $$$4$$$ is green. Maximum value among them is $$$19$$$. Indices $$$2$$$ and $$$4$$$ are black. Maximum value among them is $$$14$$$. Indices $$$3$$$ and $$$4$$$ are black. Maximum value among them is $$$19$$$. Indices $$$1$$$, $$$2$$$, and $$$3$$$ are black. Index $$$4$$$ is green. Maximum value among them is $$$19$$$. Indices $$$1$$$, $$$2$$$, and $$$4$$$ are black. Index $$$3$$$ is green. Maximum value among them is $$$19$$$. Indices $$$1$$$, $$$3$$$, and $$$4$$$ are black. Index $$$2$$$ is green. Maximum value among them is $$$19$$$. Indices $$$2$$$, $$$3$$$, and $$$4$$$ are black. Maximum value among them is $$$19$$$. Indices $$$1$$$, $$$2$$$, $$$3$$$, and $$$4$$$ are black. Maximum value among them is $$$19$$$. The total sum is $$$19+14+19+9+19+19+19+19+14+19+19+19+19+19+19 = 265$$$.
1,500
false
false
false
false
false
false
false
false
true
false
1,025
632E
Problem - 632E - Codeforces =============== xa0 . Find all the possible total costs of products the thief can nick into his knapsack. Input The first line contains two integers _n_ and _k_ (1u2009≀u2009_n_,u2009_k_u2009≀u20091000) β€” the number of kinds of products and the number of products the thief will take. The second line contains _n_ integers _a__i_ (1u2009≀u2009_a__i_u2009≀u20091000) β€” the costs of products for kinds from 1 to _n_. Output Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order. Examples Input 3 2 1 2 3 Output 2 3 4 5 6 Input 5 5 1 1 1 1 1 Output 5 Input 3 3 3 5 11 Output 9 11 13 15 17 19 21 25 27 33
2,400
true
false
false
true
false
false
false
false
false
false
7,277
1491B
There is a graph of $$$n$$$ rows and $$$10^6 + 2$$$ columns, where rows are numbered from $$$1$$$ to $$$n$$$ and columns from $$$0$$$ to $$$10^6 + 1$$$: Let's denote the node in the row $$$i$$$ and column $$$j$$$ by $$$(i, j)$$$. Initially for each $$$i$$$ the $$$i$$$-th row has exactly one obstacle β€” at node $$$(i, a_i)$$$. You want to move some obstacles so that you can reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs $$$u$$$ or $$$v$$$ coins, as below: If there is an obstacle in the node $$$(i, j)$$$, you can use $$$u$$$ coins to move it to $$$(i-1, j)$$$ or $$$(i+1, j)$$$, if such node exists and if there is no obstacle in that node currently. If there is an obstacle in the node $$$(i, j)$$$, you can use $$$v$$$ coins to move it to $$$(i, j-1)$$$ or $$$(i, j+1)$$$, if such node exists and if there is no obstacle in that node currently. Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from $$$(1,1)$$$ to $$$(0,1)$$$. Refer to the picture above for a better understanding. Now you need to calculate the minimal number of coins you need to spend to be able to reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph without passing through obstacles. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$u$$$ and $$$v$$$ ($$$2 le n le 100$$$, $$$1 le u, v le 10^9$$$)xa0β€” the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0β€” where $$$a_i$$$ represents that the obstacle in the $$$i$$$-th row is in node $$$(i, a_i)$$$. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^4$$$. Output For each test case, output a single integerxa0β€” the minimal number of coins you need to spend to be able to reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph without passing through obstacles. It can be shown that under the constraints of the problem there is always a way to make such a trip possible. Example Input 3 2 3 4 2 2 2 3 4 3 2 2 4 3 3 2 Note In the first sample, two obstacles are at $$$(1, 2)$$$ and $$$(2,2)$$$. You can move the obstacle on $$$(2, 2)$$$ to $$$(2, 3)$$$, then to $$$(1, 3)$$$. The total cost is $$$u+v = 7$$$ coins. In the second sample, two obstacles are at $$$(1, 3)$$$ and $$$(2,2)$$$. You can move the obstacle on $$$(1, 3)$$$ to $$$(2, 3)$$$. The cost is $$$u = 3$$$ coins.
1,200
true
false
false
false
false
false
true
false
false
false
3,221
1428H
This is an interactive problem. To prevent the mischievous rabbits from freely roaming around the zoo, Zookeeper has set up a special lock for the rabbit enclosure. This lock is called the Rotary Laser Lock. The lock consists of $$$n$$$ concentric rings numbered from $$$0$$$ to $$$n-1$$$. The innermost ring is ring $$$0$$$ and the outermost ring is ring $$$n-1$$$. All rings are split equally into $$$nm$$$ sections each. Each of those rings contains a single metal arc that covers exactly $$$m$$$ contiguous sections. At the center of the ring is a core and surrounding the entire lock are $$$nm$$$ receivers aligned to the $$$nm$$$ sections. The core has $$$nm$$$ lasers that shine outward from the center, one for each section. The lasers can be blocked by any of the arcs. A display on the outside of the lock shows how many lasers hit the outer receivers. In the example above, there are $$$n=3$$$ rings, each covering $$$m=4$$$ sections. The arcs are colored in green (ring $$$0$$$), purple (ring $$$1$$$), and blue (ring $$$2$$$) while the lasers beams are shown in red. There are $$$nm=12$$$ sections and $$$3$$$ of the lasers are not blocked by any arc, thus the display will show $$$3$$$ in this case. Wabbit is trying to open the lock to free the rabbits, but the lock is completely opaque, and he cannot see where any of the arcs are. Given the relative positions of the arcs, Wabbit can open the lock on his own. To be precise, Wabbit needs $$$n-1$$$ integers $$$p_1,p_2,ldots,p_{n-1}$$$ satisfying $$$0 leq p_i < nm$$$ such that for each $$$i$$$ $$$(1 leq i < n)$$$, Wabbit can rotate ring $$$0$$$ clockwise exactly $$$p_i$$$ times such that the sections that ring $$$0$$$ covers perfectly aligns with the sections that ring $$$i$$$ covers. In the example above, the relative positions are $$$p_1 = 1$$$ and $$$p_2 = 7$$$. To operate the lock, he can pick any of the $$$n$$$ rings and rotate them by $$$1$$$ section either clockwise or anti-clockwise. You will see the number on the display after every rotation. Because his paws are small, Wabbit has asked you to help him to find the relative positions of the arcs after all of your rotations are completed. You may perform up to $$$15000$$$ rotations before Wabbit gets impatient. Input The first line consists of 2 integers $$$n$$$ and $$$m$$$ $$$(2 leq n leq 100, 2 leq m leq 20)$$$, indicating the number of rings and the number of sections each ring covers. Interaction To perform a rotation, print on a single line "? x d" where $$$x$$$ $$$(0 leq x < n)$$$ is the ring that you wish to rotate and $$$d$$$ $$$(d in {-1,1})$$$ is the direction that you would like to rotate in. $$$d=1$$$ indicates a clockwise rotation by $$$1$$$ section while $$$d=-1$$$ indicates an anticlockwise rotation by $$$1$$$ section. For each query, you will receive a single integer $$$a$$$: the number of lasers that are not blocked by any of the arcs after the rotation has been performed. Once you have figured out the relative positions of the arcs, print ! followed by $$$n-1$$$ integers $$$p_1, p_2, ldots, p_{n-1}$$$. Do note that the positions of the rings are predetermined for each test case and won't change during the interaction process. After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded verdict. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacks: To hack, use the following format of test: The first line should contain two integers $$$n$$$ and $$$m$$$. The next line of should contain $$$n-1$$$ integers $$$p_1,p_2,ldots,p_{n-1}$$$: relative positions of rings $$$1,2,ldots,n-1$$$. Example Output ? 0 1 ? 2 -1 ? 1 1 ! 1 5 Note For the first test, the configuration is the same as shown on the picture from the statement. After the first rotation (which is rotating ring $$$0$$$ clockwise by $$$1$$$ section), we obtain the following configuration: After the second rotation (which is rotating ring $$$2$$$ counter-clockwise by $$$1$$$ section), we obtain the following configuration: After the third rotation (which is rotating ring $$$1$$$ clockwise by $$$1$$$ section), we obtain the following configuration: If we rotate ring $$$0$$$ clockwise once, we can see that the sections ring $$$0$$$ covers will be the same as the sections that ring $$$1$$$ covers, hence $$$p_1=1$$$. If we rotate ring $$$0$$$ clockwise five times, the sections ring $$$0$$$ covers will be the same as the sections that ring $$$2$$$ covers, hence $$$p_2=5$$$. Note that if we will make a different set of rotations, we can end up with different values of $$$p_1$$$ and $$$p_2$$$ at the end.
3,500
false
false
false
false
false
false
false
true
false
false
3,517
1779D
Boris thinks that chess is a tedious game. So he left his tournament early and went to a barber shop as his hair was a bit messy. His current hair can be described by an array $$$a_1,a_2,ldots, a_n$$$, where $$$a_i$$$ is the height of the hair standing at position $$$i$$$. His desired haircut can be described by an array $$$b_1,b_2,ldots, b_n$$$ in a similar fashion. The barber has $$$m$$$ razors. Each has its own size and can be used at most once. In one operation, he chooses a razor and cuts a segment of Boris's hair. More formally, an operation is: Choose any razor which hasn't been used before, let its size be $$$x$$$; Choose a segment $$$[l,r]$$$ ($$$1leq l leq r leq n$$$); Set $$$a_i := min (a_i,x)$$$ for each $$$lleq i leq r$$$; Notice that some razors might have equal sizesxa0β€” the barber can choose some size $$$x$$$ only as many times as the number of razors with size $$$x$$$. He may perform as many operations as he wants, as long as any razor is used at most once and $$$a_i = b_i$$$ for each $$$1 leq i leq n$$$ at the end. He does not have to use all razors. Can you determine whether the barber can give Boris his desired haircut? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 20,000$$$). The description of the test cases follows. The first line of each test case contains a positive integer $$$n$$$ ($$$3leq nleq 2cdot 10^5$$$)xa0β€” the length of arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) xa0β€” Boris's current hair. The third line of each test case contains $$$n$$$ positive integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_i leq 10^9$$$) xa0β€” Boris's desired hair. The fourth line of each test case contains a positive integer $$$m$$$ ($$$1 leq m leq 2cdot 10^5$$$) xa0β€” the number of razors. The fifth line of each test case contains $$$m$$$ positive integers $$$x_1,x_2, ldots, x_m$$$ ($$$1 leq x_i leq 10^9$$$) xa0β€” the sizes of the razors. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2cdot 10^5$$$. Output For each test case, print "YES" if the barber can cut Boris's hair as desired. Otherwise, print "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 7 3 3 3 3 2 1 2 2 1 2 6 3 4 4 6 3 4 3 1 2 3 2 3 3 3 2 3 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 10 3 1 1 1 1 1 2 12 4 2 4 3 1 5 6 3 5 6 2 1 13 7 9 4 5 3 3 3 6 8 10 3 2 5 5 3 1 5 3 2 2 5 8 5 1 1 5 8 1 5 3 5 4 2 3 1 13 7 9 4 5 3 3 3 6 8 10 3 2 5 5 3 1 5 3 2 2 5 8 5 1 1 5 7 1 5 3 4 2 3 1 3 19747843 2736467 938578397 2039844 2039844 2039844 1 2039844 Output YES NO YES NO YES NO YES Note In the first test case, Boris's hair is initially $$$[3,3,3]$$$. Let us describe a sequence of $$$2$$$ operations the barber can perform: The barber uses the razor with size $$$1$$$ on the segment $$$[2,2]$$$; hence Boris's hair becomes $$$[3,1,3]$$$. The barber uses the razor with size $$$2$$$ on the segment $$$[1,3]$$$; hence Boris's hair becomes $$$[2,1,2]$$$ which is the desired haircut. In the third test case, no operation has to be done since Boris's hair is already as desired. In the fourth test case, no cuts will be able to increase the third element in $$$[1,1,1]$$$ in a way that the array becomes $$$[1,1,2]$$$.
1,700
false
true
false
true
true
true
false
false
true
false
1,599
634A
A remote island chain contains _n_ islands, labeled 1 through _n_. Bidirectional bridges connect the islands to form a simple cyclexa0β€” a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands _n_ and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal. The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal. Determine if it is possible for the islanders to arrange the statues in the desired order. Input The first line contains a single integer _n_ (2u2009≀u2009_n_u2009≀u2009200u2009000)xa0β€” the total number of islands. The second line contains _n_ space-separated integers _a__i_ (0u2009≀u2009_a__i_u2009≀u2009_n_u2009-u20091)xa0β€” the statue currently placed on the _i_-th island. If _a__i_u2009=u20090, then the island has no statue. It is guaranteed that the _a__i_ are distinct. The third line contains _n_ space-separated integers _b__i_ (0u2009≀u2009_b__i_u2009≀u2009_n_u2009-u20091) β€” the desired statues of the _i_th island. Once again, _b__i_u2009=u20090 indicates the island desires no statue. It is guaranteed that the _b__i_ are distinct. Output Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise. Note In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3. In the second sample, the islanders can simply move statue 1 from island 1 to island 2. In the third sample, no sequence of movements results in the desired position.
1,300
false
false
true
false
false
true
false
false
false
false
7,267
1730C
You have a string $$$s$$$ consisting of digits from $$$0$$$ to $$$9$$$ inclusive. You can perform the following operation any (possibly zero) number of times: You can choose a position $$$i$$$ and delete a digit $$$d$$$ on the $$$i$$$-th position. Then insert the digit $$$min{(d + 1, 9)}$$$ on any position (at the beginning, at the end or in between any two adjacent digits). What is the lexicographically smallest string you can get by performing these operations? A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ of the same length if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a smaller digit than the corresponding digit in $$$b$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. Then the test cases follow. Each test case consists of a single line that contains one string $$$s$$$ ($$$1 le s le 2 cdot 10^5$$$) β€” the string consisting of digits. Please note that $$$s$$$ is just a string consisting of digits, so leading zeros are allowed. It is guaranteed that the sum of lengths of $$$s$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output Print a single string β€” the minimum string that is possible to obtain. Example Input 4 04829 9 01 314752277691991 Output 02599 9 01 111334567888999 Note In the first test case: Delete $$$8$$$ and insert $$$9$$$ at the end of the notation. The resulting notation is $$$04299$$$. Delete $$$4$$$ and insert $$$5$$$ in the $$$3$$$-rd position of the notation. The resulting notation is $$$02599$$$. Nothing needs to be done in the second and third test cases.
1,200
true
true
false
false
true
false
false
false
true
false
1,909
622E
Problem - 622E - Codeforces =============== xa0 ]( "Educational Codeforces Round 7") β€” the number of vertices in the tree. Each of the next _n_u2009-u20091 lines contains two integers _x__i_,u2009_y__i_ (1u2009≀u2009_x__i_,u2009_y__i_u2009≀u2009_n_) β€” the ends of the _i_-th edge. It is guaranteed that you are given the correct undirected tree. Output Print the only integer _t_ β€” the minimal time required for all ants to be in the root of the tree. Examples Input 12 1 2 1 3 1 4 2 5 2 6 3 7 3 8 3 9 8 10 8 11 8 12 Output 6 Input 2 2 1 Output 1
2,200
false
true
false
false
false
false
false
false
true
false
7,342
1196D2
The only difference between easy and hard versions is the size of the input. You are given a string $$$s$$$ consisting of $$$n$$$ characters, each character is 'R', 'G' or 'B'. You are also given an integer $$$k$$$. Your task is to change the minimum number of characters in the initial string $$$s$$$ so that after the changes there will be a string of length $$$k$$$ that is a substring of $$$s$$$, and is also a substring of the infinite string "RGBRGBRGB ...". A string $$$a$$$ is a substring of string $$$b$$$ if there exists a positive integer $$$i$$$ such that $$$a_1 = b_i$$$, $$$a_2 = b_{i + 1}$$$, $$$a_3 = b_{i + 2}$$$, ..., $$$a_{a} = b_{i + a - 1}$$$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0β€” the number of queries. Then $$$q$$$ queries follow. The first line of the query contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$)xa0β€” the length of the string $$$s$$$ and the length of the substring. The second line of the query contains a string $$$s$$$ consisting of $$$n$$$ characters 'R', 'G' and 'B'. It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each query print one integerxa0β€” the minimum number of characters you need to change in the initial string $$$s$$$ so that after changing there will be a substring of length $$$k$$$ in $$$s$$$ that is also a substring of the infinite string "RGBRGBRGB ...". Example Input 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR Note In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB". In the second example, the substring is "BRG".
1,600
false
false
true
true
true
false
false
false
false
false
4,718
1332G
Given a sequence of integers $$$a$$$ of length $$$n$$$, a tuple $$$(i,j,k)$$$ is called monotone triples if $$$1 le i<j<kle n$$$; $$$a_i le a_j le a_k$$$ or $$$a_i ge a_j ge a_k$$$ is satisfied. For example, $$$a=[5,3,4,5]$$$, then $$$(2,3,4)$$$ is monotone triples for sequence $$$a$$$ while $$$(1,3,4)$$$ is not. Bob is given a sequence of integers $$$a$$$ of length $$$n$$$ in a math exam. The exams itself contains questions of form $$$L, R$$$, for each of them he is asked to find any subsequence $$$b$$$ with size greater than $$$2$$$ (i.e. $$$b ge 3$$$) of sequence $$$a_L, a_{L+1},ldots, a_{R}$$$. Recall that an sequence $$$b$$$ is a subsequence of sequence $$$a$$$ if $$$b$$$ can be obtained by deletion of several (possibly zero, or all) elements. However, he hates monotone stuff, and he wants to find a subsequence free from monotone triples. Besides, he wants to find one subsequence with the largest length among all subsequences free from monotone triples for every query. Please help Bob find out subsequences meeting the above constraints. Input The first line contains two integers $$$n$$$, $$$q$$$ ($$$3 le n le 2 cdot 10^5$$$, $$$1 le q le 2 cdot 10^5$$$) β€” the length of sequence $$$a$$$ and the number of queries. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots, a_n$$$ ($$$1 le a_i le 10^{9}$$$), representing the sequence $$$a$$$. Then each of the following $$$q$$$ lines contains two integers $$$L$$$, $$$R$$$ ($$$1 le L,R le n$$$, $$$R-Lge 2$$$). Output For each query, output $$$0$$$ if there is no subsequence $$$b$$$ satisfying the constraints mentioned in the legend. You can print the empty line after but that's not mandatory. Otherwise, output one integer $$$k$$$ ($$$k > 2$$$) denoting the length of sequence $$$b$$$, then output $$$k$$$ integers $$$i_1, i_2, ldots, i_k$$$ ($$$L le i_1 < i_2<ldots<i_kle R$$$) satisfying that $$$b_j = a_{i_j}$$$ for $$$1 le j le k$$$. If there are multiple answers with the maximum length, print any of them. Example Input 6 2 3 1 4 1 5 9 1 3 4 6 Note For the first query, the given sequence itself is monotone triples free. For the second query, it can be shown that there is no subsequence $$$b$$$ with length greater than $$$2$$$ such that $$$b$$$ is monotone triples free.
3,100
false
false
false
false
true
false
false
false
false
false
4,044
1847D
Josuke is tired of his peaceful life in Morioh. Following in his nephew Jotaro's footsteps, he decides to study hard and become a professor of computer science. While looking up competitive programming problems online, he comes across the following one: Let $$$s$$$ be a binary string of length $$$n$$$. An operation on $$$s$$$ is defined as choosing two distinct integers $$$i$$$ and $$$j$$$ ($$$1 leq i < j leq n$$$), and swapping the characters $$$s_i, s_j$$$. Consider the $$$m$$$ strings $$$t_1, t_2, ldots, t_m$$$, where $$$t_i$$$ is the substring $$$^dagger$$$ of $$$s$$$ from $$$l_i$$$ to $$$r_i$$$. Define $$$t(s) = t_1+t_2+ldots+t_m$$$ as the concatenation of the strings $$$t_i$$$ in that order. There are $$$q$$$ updates to the string. In the $$$i$$$-th update $$$s_{x_i}$$$ gets flipped. That is if $$$s_{x_i}=1$$$, then $$$s_{x_i}$$$ becomes $$$0$$$ and vice versa. After each update, find the minimum number of operations one must perform on $$$s$$$ to make $$$t(s)$$$ lexicographically as large$$$^ddagger$$$ as possible. Note that no operation is actually performed. We are only interested in the number of operations. Help Josuke in his dream by solving the problem for him. β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” $$$dagger$$$ A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. $$$ddagger$$$ A string $$$a$$$ is lexicographically larger than a string $$$b$$$ of the same length if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a $$$1$$$, and the string $$$b$$$ has a $$$0$$$. Input The first line contains three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$1 leq n,m,q leq 2 cdot 10^5$$$). The next line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of digits $$$0$$$ and $$$1$$$. The $$$i$$$-th line of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i leq r_i leq n$$$). The $$$i$$$-th line of the next $$$q$$$ lines contains a single integer $$$x_i$$$ ($$$1 leq x_i leq n$$$). Note In the first test case, Originally, $$$t(s) = s(1,2) + s(1,2) = 0101$$$. After the $$$1$$$-st query, $$$s$$$ becomes $$$11$$$ and consequently $$$t$$$ becomes $$$1111$$$. You don't need to perform any operation as $$$t(s)$$$ is already the lexicographically largest string possible. After the $$$2$$$-nd query, $$$s$$$ becomes $$$01$$$ and consequently $$$t$$$ becomes $$$0101$$$. You need to perform $$$1$$$ operation by swapping $$$s_1$$$ and $$$s_2$$$. Consequently, $$$t(s)$$$ becomes $$$1010$$$ which is the lexicographically largest string you can achieve.
1,900
false
true
true
false
true
false
false
false
false
false
1,197
725C
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid: ABCDEFGHIJKLM NOPQRSTUVWXYZ We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself. A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself). You’re given a string _s_ which consists of 27 upper-case English letters. Each English letter occurs at least once in _s_. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string _s_. If there’s no solution, print "Impossible" (without the quotes). Input The only line of the input contains the string _s_, consisting of 27 upper-case English letters. Each English letter occurs at least once in _s_. Output Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible". Examples Input ABCDEFGHIJKLMNOPQRSGTUVWXYZ Output YXWVUTGHIJKLM ZABCDEFSRQPON Input BUVTYZFQSNRIWOXXGJLKACPEMDH
1,600
false
false
true
false
false
true
true
false
false
false
6,905
97A
Little Gennady was presented with a set of domino for his birthday. The set consists of 28 different dominoes of size 2u2009Γ—u20091. Both halves of each domino contain one digit from 0 to 6. 0-0 0-1 0-2 0-3 0-4 0-5 0-6 1-1 1-2 1-3 1-4 1-5 1-6 2-2 2-3 2-4 2-5 2-6 3-3 3-4 3-5 3-6 4-4 4-5 4-6 5-5 5-6 6-6 The figure that consists of 28 dominoes is called magic, if it can be fully covered with 14 non-intersecting squares of size 2u2009Γ—u20092 so that each square contained four equal numbers. Every time Gennady assembles a magic figure, some magic properties of the set appear β€” he wins the next contest. Gennady noticed that he can't assemble a figure that has already been assembled, otherwise someone else wins the contest. Gennady chose a checked field of size _n_u2009Γ—u2009_m_ and put there rectangular chips of sizes 1u2009Γ—u20092 and 2u2009Γ—u20091. Each chip fully occupies exactly two neighboring squares of the field. Those chips do not overlap but they can touch each other. Overall the field has exactly 28 chips, equal to the number of dominoes in the set. Now Gennady wants to replace each chip with a domino so that a magic figure appeared as a result. Different chips should be replaced by different dominoes. Determine in what number of contests Gennady can win over at the given position of the chips. You are also required to find one of the possible ways of replacing chips with dominoes to win the next Codeforces round. Input The first line contains two positive integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u200930). Each of the following _n_ lines contains _m_ characters, which is the position of chips on the field. The dots stand for empty spaces, Latin letters from "a" to "z" and "A", "B" stand for the positions of the chips. There are exactly 28 chips on the field. The squares covered by the same chip are marked by the same letter, different chips are marked by different letters. It is guaranteed that the field's description is correct. It is also guaranteed that at least one solution exists. Output Print on the first line the number of ways to replace chips with dominoes to get a magic figure. That is the total number of contests that can be won using this arrangement of the chips. Next _n_ lines containing _m_ characters each, should contain a field from dots and numbers from 0 to 6 β€” any of the possible solutions. All dominoes should be different. Examples Input 8 8 .aabbcc. .defghi. kdefghij klmnopqj .lmnopq. .rstuvw. xrstuvwy xzzAABBy Output 10080 .001122. .001122. 33440055 33440055 .225566. .225566. 66113344 66113344
2,400
false
false
true
false
false
false
true
false
false
false
9,501
814B
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized. On that night, Sengoku constructed a permutation _p_1,u2009_p_2,u2009...,u2009_p__n_ of integers from 1 to _n_ inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with _n_ meteorids, colours of which being integer sequences _a_1,u2009_a_2,u2009...,u2009_a__n_ and _b_1,u2009_b_2,u2009...,u2009_b__n_ respectively. Meteoroids' colours were also between 1 and _n_ inclusive, and the two sequences were not identical, that is, at least one _i_ (1u2009≀u2009_i_u2009≀u2009_n_) exists, such that _a__i_u2009β‰ u2009_b__i_ holds. Well, she almost had it all β€” each of the sequences _a_ and _b_ matched exactly _n_u2009-u20091 elements in Sengoku's permutation. In other words, there is exactly one _i_ (1u2009≀u2009_i_u2009≀u2009_n_) such that _a__i_u2009β‰ u2009_p__i_, and exactly one _j_ (1u2009≀u2009_j_u2009≀u2009_n_) such that _b__j_u2009β‰ u2009_p__j_. For now, Sengoku is able to recover the actual colour sequences _a_ and _b_ through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night. Input The first line of input contains a positive integer _n_ (2u2009≀u2009_n_u2009≀u20091u2009000) β€” the length of Sengoku's permutation, being the length of both meteor outbursts at the same time. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≀u2009_a__i_u2009≀u2009_n_) β€” the sequence of colours in the first meteor outburst. The third line contains _n_ space-separated integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≀u2009_b__i_u2009≀u2009_n_) β€” the sequence of colours in the second meteor outburst. At least one _i_ (1u2009≀u2009_i_u2009≀u2009_n_) exists, such that _a__i_u2009β‰ u2009_b__i_ holds. Output Output _n_ space-separated integers _p_1,u2009_p_2,u2009...,u2009_p__n_, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them. Input guarantees that such permutation exists. Note In the first sample, both 1,u20092,u20095,u20094,u20093 and 1,u20092,u20093,u20094,u20095 are acceptable outputs. In the second sample, 5,u20094,u20092,u20093,u20091 is the only permutation to satisfy the constraints.
1,300
false
false
false
false
false
true
false
false
false
false
6,504
1098C
Misha walked through the snowy forest and he was so fascinated by the trees to decide to draw his own tree! Misha would like to construct a rooted tree with $$$n$$$ vertices, indexed from 1 to $$$n$$$, where the root has index 1. Every other vertex has a parent $$$p_i$$$, and $$$i$$$ is called a child of vertex $$$p_i$$$. Vertex $$$u$$$ belongs to the subtree of vertex $$$v$$$ iff $$$v$$$ is reachable from $$$u$$$ while iterating over the parents ($$$u$$$, $$$p_{u}$$$, $$$p_{p_{u}}$$$, ...). Clearly, $$$v$$$ belongs to its own subtree, and the number of vertices in the subtree is called the size of the subtree. Misha is only interested in trees where every vertex belongs to the subtree of vertex $$$1$$$. Below there is a tree with $$$6$$$ vertices. The subtree of vertex $$$2$$$ contains vertices $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$. Hence the size of its subtree is $$$4$$$. The branching coefficient of the tree is defined as the maximum number of children in any vertex. For example, for the tree above the branching coefficient equals $$$2$$$. Your task is to construct a tree with $$$n$$$ vertices such that the sum of the subtree sizes for all vertices equals $$$s$$$, and the branching coefficient is minimum possible. Input The only input line contains two integers $$$n$$$ and $$$s$$$xa0β€” the number of vertices in the tree and the desired sum of the subtree sizes ($$$2 le n le 10^5$$$; $$$1 le s le 10^{10}$$$). Output If the required tree does not exist, output Β«NoΒ». Otherwise output Β«YesΒ» on the first line, and in the next one output integers $$$p_2$$$, $$$p_3$$$, ..., $$$p_n$$$, where $$$p_i$$$ denotes the parent of vertex $$$i$$$. Note Below one can find one of the possible solutions for the first sample case. The sum of subtree sizes equals $$$3 + 1 + 1 = 5$$$, and the branching coefficient equals $$$2$$$. Below one can find one of the possible solutions for the third sample case. The sum of subtree sizes equals $$$6 + 3 + 2 + 1 + 2 + 1 = 15$$$, and the branching coefficient equals $$$2$$$.
2,400
false
true
false
false
false
true
false
true
false
true
5,214
1082E
Problem - 1082E - 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 dp greedy *2000 No tag edit access β†’ Contest materials and integer value $$$k$$$ (positive, negative or even zero) and change $$$a_l, a_{l + 1}, dots, a_r$$$ by $$$k$$$ each (i.e. $$$a_i := a_i + k$$$ for each $$$l le i le r$$$). What is the maximum possible number of elements with value $$$c$$$ that can be obtained after one such operation? Input The first line contains two integers $$$n$$$ and $$$c$$$ ($$$1 le n le 5 cdot 10^5$$$, $$$1 le c le 5 cdot 10^5$$$) β€” the length of array and the value $$$c$$$ to obtain. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 5 cdot 10^5$$$) β€” array $$$a$$$. Output Print one integer β€” the maximum possible number of elements with value $$$c$$$ which can be obtained after performing operation described above. Examples Input 6 9 9 9 9 9 9 9 Output 6 Input 3 2 6 2 6 Output 2 Note In the first example we can choose any segment and $$$k = 0$$$. The array will stay same. In the second example we can choose segment $$$
2,000
false
true
false
true
false
false
false
true
false
false
5,311
1152F2
This problem is same as the previous one, but has larger constraints. Aki is playing a new video game. In the video game, he will control Neko, the giant cat, to fly between planets in the Catniverse. There are $$$n$$$ planets in the Catniverse, numbered from $$$1$$$ to $$$n$$$. At the beginning of the game, Aki chooses the planet where Neko is initially located. Then Aki performs $$$k - 1$$$ moves, where in each move Neko is moved from the current planet $$$x$$$ to some other planet $$$y$$$ such that: Planet $$$y$$$ is not visited yet. $$$1 leq y leq x + m$$$ (where $$$m$$$ is a fixed constant given in the input) This way, Neko will visit exactly $$$k$$$ different planets. Two ways of visiting planets are called different if there is some index $$$i$$$ such that, the $$$i$$$-th planet visited in the first way is different from the $$$i$$$-th planet visited in the second way. What is the total number of ways to visit $$$k$$$ planets this way? Since the answer can be quite large, print it modulo $$$10^9 + 7$$$. Input The only line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 le n le 10^9$$$, $$$1 le k le min(n, 12)$$$, $$$1 le m le 4$$$)xa0β€” the number of planets in the Catniverse, the number of planets Neko needs to visit and the said constant $$$m$$$. Output Print exactly one integerxa0β€” the number of different ways Neko can visit exactly $$$k$$$ planets. Since the answer can be quite large, print it modulo $$$10^9 + 7$$$. Note In the first example, there are $$$4$$$ ways Neko can visit all the planets: $$$1 ightarrow 2 ightarrow 3$$$ $$$2 ightarrow 3 ightarrow 1$$$ $$$3 ightarrow 1 ightarrow 2$$$ $$$3 ightarrow 2 ightarrow 1$$$ In the second example, there are $$$9$$$ ways Neko can visit exactly $$$2$$$ planets: $$$1 ightarrow 2$$$ $$$2 ightarrow 1$$$ $$$2 ightarrow 3$$$ $$$3 ightarrow 1$$$ $$$3 ightarrow 2$$$ $$$3 ightarrow 4$$$ $$$4 ightarrow 1$$$ $$$4 ightarrow 2$$$ $$$4 ightarrow 3$$$ In the third example, with $$$m = 4$$$, Neko can visit all the planets in any order, so there are $$$5! = 120$$$ ways Neko can visit all the planets. In the fourth example, Neko only visit exactly $$$1$$$ planet (which is also the planet he initially located), and there are $$$100$$$ ways to choose the starting planet for Neko.
3,000
false
false
false
true
false
false
false
false
false
false
4,950
827B
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of _n_ nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly _k_ of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability. Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes. Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible. Input The first line contains two integers _n_ and _k_ (3u2009≀u2009_n_u2009≀u20092Β·105, 2u2009≀u2009_k_u2009≀u2009_n_u2009-u20091)xa0β€” the total number of nodes and the number of exit-nodes. Note that it is always possible to build at least one network with _n_ nodes and _k_ exit-nodes within the given constraints. Output In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next _n_u2009-u20091 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to _n_. Exit-nodes can have any ids. If there are multiple answers, print any of them. Note In the first example the only network is shown on the left picture. In the second example one of optimal networks is shown on the right picture. Exit-nodes are highlighted.
1,800
false
true
true
false
false
true
false
false
false
false
6,454
700C
Again, there are hard times in Berland! Many towns have such tensions that even civil war is possible. There are _n_ towns in Reberland, some pairs of which connected by two-way roads. It is not guaranteed that it is possible to reach one town from any other town using these roads. Towns _s_ and _t_ announce the final break of any relationship and intend to rule out the possibility of moving between them by the roads. Now possibly it is needed to close several roads so that moving from _s_ to _t_ using roads becomes impossible. Each town agrees to spend money on closing no more than one road, therefore, the total number of closed roads will be no more than two. Help them find set of no more than two roads such that there will be no way between _s_ and _t_ after closing these roads. For each road the budget required for its closure was estimated. Among all sets find such that the total budget for the closure of a set of roads is minimum. Input The first line of the input contains two integers _n_ and _m_ (2u2009≀u2009_n_u2009≀u20091000, 0u2009≀u2009_m_u2009≀u200930u2009000)xa0β€” the number of towns in Berland and the number of roads. The second line contains integers _s_ and _t_ (1u2009≀u2009_s_,u2009_t_u2009≀u2009_n_, _s_u2009β‰ u2009_t_)xa0β€” indices of towns which break up the relationships. Then follow _m_ lines, each of them contains three integers _x__i_, _y__i_ and _w__i_ (1u2009≀u2009_x__i_,u2009_y__i_u2009≀u2009_n_, 1u2009≀u2009_w__i_u2009≀u2009109)xa0β€” indices of towns connected by the _i_-th road, and the budget on its closure. All roads are bidirectional. It is allowed that the pair of towns is connected by more than one road. Roads that connect the city to itself are allowed. Output In the first line print the minimum budget required to break up the relations between _s_ and _t_, if it is allowed to close no more than two roads. In the second line print the value _c_ (0u2009≀u2009_c_u2009≀u20092)xa0β€” the number of roads to be closed in the found solution. In the third line print in any order _c_ diverse integers from 1 to _m_xa0β€” indices of closed roads. Consider that the roads are numbered from 1 to _m_ in the order they appear in the input. If it is impossible to make towns _s_ and _t_ disconnected by removing no more than 2 roads, the output should contain a single line -1. If there are several possible answers, you may print any of them. Examples Input 6 7 1 6 2 1 6 2 3 5 3 4 9 4 6 4 4 6 5 4 5 1 3 1 3 Input 6 7 1 6 2 3 1 1 2 2 1 3 3 4 5 4 3 6 5 4 6 6 1 5 7 Input 5 4 1 5 2 1 3 3 2 1 3 4 4 4 5 2 Input 2 3 1 2 1 2 734458840 1 2 817380027 1 2 304764803
2,600
false
false
false
false
false
false
false
false
false
true
7,023
1722A
Timur likes his name. As a spelling of his name, he allows any permutation of the letters of the name. For example, the following strings are valid spellings of his name: Timur, miurT, Trumi, mriTu. Note that the correct spelling must have uppercased T and lowercased other letters. Today he wrote string $$$s$$$ of length $$$n$$$ consisting only of uppercase or lowercase Latin letters. He asks you to check if $$$s$$$ is the correct spelling of his name. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0β€” the number of test cases. The first line of each test case contains an integer $$$n$$$ $$$(1 leq n leq 10)$$$xa0β€” the length of string $$$s$$$. The second line of each test case contains a string $$$s$$$ consisting of only uppercase or lowercase Latin characters. Output For each test case, output "YES" (without quotes) if $$$s$$$ satisfies the condition, and "NO" (without quotes) otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 10 5 Timur 5 miurT 5 Trumi 5 mriTu 5 timur 4 Timr 6 Timuur 10 codeforces 10 TimurTimur 5 TIMUR Output YES YES YES YES NO NO NO NO NO NO
800
false
false
true
false
false
false
false
false
false
false
1,953
1976B
You are given two integer arrays: array $$$a$$$ of length $$$n$$$ and array $$$b$$$ of length $$$n+1$$$. You can perform the following operations any number of times in any order: choose any element of the array $$$a$$$ and increase it by $$$1$$$; choose any element of the array $$$a$$$ and decrease it by $$$1$$$; choose any element of the array $$$a$$$, copy it and append the copy to the end of the array $$$a$$$. Your task is to calculate the minimum number of aforementioned operations (possibly zero) required to transform the array $$$a$$$ into the array $$$b$$$. It can be shown that under the constraints of the problem, it is always possible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Each test case consists of three lines: the first line 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$$$ ($$$1 le a_i le 10^9$$$); the third line contains $$$n + 1$$$ integers $$$b_1, b_2, dots, b_{n + 1}$$$ ($$$1 le b_i le 10^9$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0β€” the minimum number of operations (possibly zero) required to transform the array $$$a$$$ into the array $$$b$$$. Example Input 3 1 2 1 3 2 3 3 3 3 3 4 4 2 1 2 2 1 5 2 3 Note In the first example, you can transform $$$a$$$ into $$$b$$$ as follows: $$$[2] ightarrow [2, 2] ightarrow [1, 2] ightarrow [1, 3]$$$.
1,100
false
true
true
false
false
false
false
false
false
false
432
1499D
Problem - 1499D - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags dp math number theory *2100 No tag edit access β†’ Contest materials integers $$$c$$$, $$$d$$$ and $$$x$$$. You have to find the number of pairs of positive integers $$$(a, b)$$$ such that equality $$$c cdot lcm(a, b) - d cdot gcd(a, b) = x$$$ holds. Where $$$lcm(a, b)$$$ is the least common multiple of $$$a$$$ and $$$b$$$ and $$$gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. Each test case consists of one line containing three integer $$$c$$$, $$$d$$$ and $$$x$$$ ($$$1 le c, d, x le 10^7$$$). Output For each test case, print one integer β€” the number of pairs ($$$a, b$$$) such that the above equality holds. Example Input 4 1 1 3 4 2 6 3 3 7 2 7 25 Output 4 3 0 8 Note In the first example, the correct pairs are: ($$$1, 4$$$), ($$$4,1$$$), ($$$3, 6$$$), ($$$6, 3$$$). In the second example, the correct pairs are: ($$$1, 2$$$), ($$$2, 1$$$), ($$$3, 3$$$).
2,100
true
false
false
true
false
false
false
false
false
false
3,172
841B
Problem - 841B - 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 games math *1100 No tag edit access β†’ Contest materials β€” length of the array. Next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≀u2009_a__i_u2009≀u2009109). Output Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes). Examples Input 4 1 3 2 3 Output First Input 2 2 2 Output Second Note In first sample first player remove whole array in one move and win. In second sample first player can't make a move and lose.
1,100
true
false
false
false
false
false
false
false
false
false
6,397
1612D
You are given a pair of integers $$$(a, b)$$$ and an integer $$$x$$$. You can change the pair in two different ways: set (assign) $$$a := a - b$$$; set (assign) $$$b := a - b$$$, where $$$a - b$$$ is the absolute difference between $$$a$$$ and $$$b$$$. The pair $$$(a, b)$$$ is called $$$x$$$-magic if $$$x$$$ is obtainable either as $$$a$$$ or as $$$b$$$ using only the given operations (i.e. the pair $$$(a, b)$$$ is $$$x$$$-magic if $$$a = x$$$ or $$$b = x$$$ after some number of operations applied). You can apply the operations any number of times (even zero). Your task is to find out if the pair $$$(a, b)$$$ is $$$x$$$-magic or not. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. The next $$$t$$$ lines describe test cases. The only line of the test case contains three integers $$$a$$$, $$$b$$$ and $$$x$$$ ($$$1 le a, b, x le 10^{18}$$$). Output For the $$$i$$$-th test case, print YES if the corresponding pair $$$(a, b)$$$ is $$$x$$$-magic and NO otherwise. Example Input 8 6 9 3 15 38 7 18 8 8 30 30 30 40 50 90 24 28 20 365 216 52 537037812705867558 338887693834423551 3199921013340 Output YES YES YES YES NO YES YES YES
1,600
true
false
false
false
false
false
false
false
false
false
2,580
1594D
Theofanis started playing the new online game called "Among them". However, he always plays with Cypriot players, and they all have the same name: "Andreas" (the most common name in Cyprus). In each game, Theofanis plays with $$$n$$$ other players. Since they all have the same name, they are numbered from $$$1$$$ to $$$n$$$. The players write $$$m$$$ comments in the chat. A comment has the structure of "$$$i$$$ $$$j$$$ $$$c$$$" where $$$i$$$ and $$$j$$$ are two distinct integers and $$$c$$$ is a string ($$$1 le i, j le n$$$; $$$i eq j$$$; $$$c$$$ is either imposter or crewmate). The comment means that player $$$i$$$ said that player $$$j$$$ has the role $$$c$$$. An imposter always lies, and a crewmate always tells the truth. Help Theofanis find the maximum possible number of imposters among all the other Cypriot players, or determine that the comments contradict each other (see the notes for further explanation). Note that each player has exactly one role: either imposter or crewmate. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Description of each test case follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$0 le m le 5 cdot 10^5$$$)xa0β€” the number of players except Theofanis and the number of comments. Each of the next $$$m$$$ lines contains a comment made by the players of the structure "$$$i$$$ $$$j$$$ $$$c$$$" where $$$i$$$ and $$$j$$$ are two distinct integers and $$$c$$$ is a string ($$$1 le i, j le n$$$; $$$i eq j$$$; $$$c$$$ is either imposter or crewmate). There can be multiple comments for the same pair of $$$(i, j)$$$. It is guaranteed that the sum of all $$$n$$$ does not exceed $$$2 cdot 10^5$$$ and the sum of all $$$m$$$ does not exceed $$$5 cdot 10^5$$$. Output For each test case, print one integerxa0β€” the maximum possible number of imposters. If the comments contradict each other, print $$$-1$$$. Example Input 5 3 2 1 2 imposter 2 3 crewmate 5 4 1 3 crewmate 2 5 crewmate 2 4 imposter 3 4 imposter 2 2 1 2 imposter 2 1 crewmate 3 5 1 2 imposter 1 2 imposter 3 2 crewmate 3 2 crewmate 1 3 imposter 5 0 Note In the first test case, imposters can be Andreas $$$2$$$ and $$$3$$$. In the second test case, imposters can be Andreas $$$1$$$, $$$2$$$, $$$3$$$ and $$$5$$$. In the third test case, comments contradict each other. This is because player $$$1$$$ says that player $$$2$$$ is an imposter, and player $$$2$$$ says that player $$$1$$$ is a crewmate. If player $$$1$$$ is a crewmate, then he must be telling the truth, so player $$$2$$$ must be an imposter. But if player $$$2$$$ is an imposter then he must be lying, so player $$$1$$$ can't be a crewmate. Contradiction.
1,700
false
false
false
true
false
true
false
false
false
true
2,675
1946F
Input Each test consists of several sets of input data. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of sets of input data. The description of the sets of input data follows. The first line of each set of input data contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 10^6$$$)xa0β€” the length of the permutation and the number of Nechipor's questions. The second line of each set of input data contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β€” the permutation $$$a$$$ itself. In each of the next $$$q$$$ lines of each set of input data, there are two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$)xa0β€” the next question of Nechipor. It is guaranteed that the sum of the values of $$$n$$$ and the sum of the values of $$$q$$$ over all test cases does not exceed $$$10^6$$$. Note All suitable arrays in the first set of input data: ($$$1$$$), ($$$2$$$), ($$$3$$$), ($$$4$$$), ($$$5$$$), ($$$6$$$), ($$$7$$$), ($$$8$$$), ($$$1, 3$$$), ($$$1, 6$$$), ($$$1, 7$$$), ($$$1, 6, 7$$$), ($$$2, 3$$$), ($$$2, 4$$$), ($$$2, 5$$$), ($$$2, 6$$$), ($$$2, 7$$$), ($$$2, 8$$$), ($$$2, 6, 7$$$), ($$$6, 7$$$). All suitable arrays in the fourth set of input data: ($$$1$$$), ($$$2$$$), ($$$3$$$), ($$$4$$$), ($$$5$$$), ($$$6$$$), ($$$7$$$), ($$$8$$$), ($$$1, 2$$$), ($$$1, 3$$$), ($$$1, 4$$$), ($$$1, 5$$$), ($$$1, 6$$$), ($$$1, 7$$$), ($$$1, 8$$$), ($$$1, 2, 4$$$), ($$$1, 2, 6$$$), ($$$1, 2, 8$$$), ($$$1, 2, 4, 8$$$), ($$$1, 3, 6$$$), ($$$1, 4, 8$$$), ($$$2, 4$$$), ($$$2, 6$$$), ($$$2, 8$$$), ($$$2, 4, 8$$$), ($$$3, 6$$$), ($$$4, 8$$$).
2,500
false
false
false
true
true
false
false
false
false
false
600
1593D1
This problem is a simplified version of D2, but it has significant differences, so read the whole statement. Polycarp has an array of $$$n$$$ ($$$n$$$ is even) integers $$$a_1, a_2, dots, a_n$$$. Polycarp conceived of a positive integer $$$k$$$. After that, Polycarp began performing the following operations on the array: take an index $$$i$$$ ($$$1 le i le n$$$) and reduce the number $$$a_i$$$ by $$$k$$$. After Polycarp performed some (possibly zero) number of such operations, it turned out that all numbers in the array became the same. Find the maximum $$$k$$$ at which such a situation is possible, or print $$$-1$$$ if such a number can be arbitrarily large. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first line contains an even integer $$$n$$$ ($$$4 le n le 40$$$) ($$$n$$$ is even). The second line contains $$$n$$$ integers $$$a_1, a_2, dots a_n$$$ ($$$-10^6 le a_i le 10^6$$$). It is guaranteed that the sum of all $$$n$$$ specified in the given test cases does not exceed $$$100$$$. Output For each test case output on a separate line an integer $$$k$$$ ($$$k ge 1$$$) β€” the maximum possible number that Polycarp used in operations on the array, or $$$-1$$$, if such a number can be arbitrarily large. Example Input 3 6 1 5 3 1 1 5 8 -1 0 1 -1 0 1 -1 0 4 100 -1000 -1000 -1000
1,100
true
false
false
false
false
false
false
false
false
false
2,683
916B
Jamie is preparing a Codeforces round. He has got an idea for a problem, but does not know how to solve it. Help him write a solution to the following problem: Find _k_ integers such that the sum of two to the power of each number equals to the number _n_ and the largest integer in the answer is as small as possible. As there may be multiple answers, you are asked to output the lexicographically largest one. To be more clear, consider all integer sequence with length _k_ (_a_1,u2009_a_2,u2009...,u2009_a__k_) with . Give a value to each sequence. Among all sequence(s) that have the minimum _y_ value, output the one that is the lexicographically largest. For definitions of powers and lexicographical order see notes. Output Output "No" (without quotes) in a single line if there does not exist such sequence. Otherwise, output "Yes" (without quotes) in the first line, and _k_ numbers separated by space in the second linexa0β€” the required sequence. It is guaranteed that the integers in the answer sequence fit the range [u2009-u20091018,u20091018]. Note Sample 1: 23u2009+u200923u2009+u200922u2009+u200921u2009+u200920u2009=u20098u2009+u20098u2009+u20094u2009+u20092u2009+u20091u2009=u200923 Answers like (3,u20093,u20092,u20090,u20091) or (0,u20091,u20092,u20093,u20093) are not lexicographically largest. Answers like (4,u20091,u20091,u20091,u20090) do not have the minimum _y_ value. Sample 2: It can be shown there does not exist a sequence with length 2. Sample 3: Powers of 2: If _x_u2009>u20090, then 2_x_u2009=u20092Β·2Β·2Β·...Β·2 (_x_ times). If _x_u2009=u20090, then 2_x_u2009=u20091. If _x_u2009<u20090, then . Lexicographical order: Given two different sequences of the same length, (_a_1,u2009_a_2,u2009... ,u2009_a__k_) and (_b_1,u2009_b_2,u2009... ,u2009_b__k_), the first one is smaller than the second one for the lexicographical order, if and only if _a__i_u2009<u2009_b__i_, for the first _i_ where _a__i_ and _b__i_ differ.
2,000
true
true
false
false
false
false
false
false
false
false
6,067
1157D
Polycarp has to solve exactly $$$n$$$ problems to improve his programming skill before an important programming competition. But this competition will be held very soon, most precisely, it will start in $$$k$$$ days. It means that Polycarp has exactly $$$k$$$ days for training! Polycarp doesn't want to procrastinate, so he wants to solve at least one problem during each of $$$k$$$ days. He also doesn't want to overwork, so if he solves $$$x$$$ problems during some day, he should solve no more than $$$2x$$$ problems during the next day. And, at last, he wants to improve his skill, so if he solves $$$x$$$ problems during some day, he should solve at least $$$x+1$$$ problem during the next day. More formally: let $$$[a_1, a_2, dots, a_k]$$$ be the array of numbers of problems solved by Polycarp. The $$$i$$$-th element of this array is the number of problems Polycarp solves during the $$$i$$$-th day of his training. Then the following conditions must be satisfied: sum of all $$$a_i$$$ for $$$i$$$ from $$$1$$$ to $$$k$$$ should be $$$n$$$; $$$a_i$$$ should be greater than zero for each $$$i$$$ from $$$1$$$ to $$$k$$$; the condition $$$a_i < a_{i + 1} le 2 a_i$$$ should be satisfied for each $$$i$$$ from $$$1$$$ to $$$k-1$$$. Your problem is to find any array $$$a$$$ of length $$$k$$$ satisfying the conditions above or say that it is impossible to do it. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^9, 1 le k le 10^5$$$) β€” the number of problems Polycarp wants to solve and the number of days Polycarp wants to train. Output If it is impossible to find any array $$$a$$$ of length $$$k$$$ satisfying Polycarp's rules of training, print "NO" in the first line. Otherwise print "YES" in the first line, then print $$$k$$$ integers $$$a_1, a_2, dots, a_k$$$ in the second line, where $$$a_i$$$ should be the number of problems Polycarp should solve during the $$$i$$$-th day. If there are multiple answers, you can print any.
1,900
true
true
false
false
false
true
false
false
false
false
4,919
1444A
Oleg's favorite subjects are History and Math, and his favorite branch of mathematics is division. To improve his division skills, Oleg came up with $$$t$$$ pairs of integers $$$p_i$$$ and $$$q_i$$$ and for each pair decided to find the greatest integer $$$x_i$$$, such that: $$$p_i$$$ is divisible by $$$x_i$$$; $$$x_i$$$ is not divisible by $$$q_i$$$. Oleg is really good at division and managed to find all the answers quickly, how about you? Input The first line contains an integer $$$t$$$ ($$$1 le t le 50$$$)xa0β€” the number of pairs. Each of the following $$$t$$$ lines contains two integers $$$p_i$$$ and $$$q_i$$$ ($$$1 le p_i le 10^{18}$$$; $$$2 le q_i le 10^{9}$$$)xa0β€” the $$$i$$$-th pair of integers. Output Print $$$t$$$ integers: the $$$i$$$-th integer is the largest $$$x_i$$$ such that $$$p_i$$$ is divisible by $$$x_i$$$, but $$$x_i$$$ is not divisible by $$$q_i$$$. One can show that there is always at least one value of $$$x_i$$$ satisfying the divisibility conditions for the given constraints. Note For the first pair, where $$$p_1 = 10$$$ and $$$q_1 = 4$$$, the answer is $$$x_1 = 10$$$, since it is the greatest divisor of $$$10$$$ and $$$10$$$ is not divisible by $$$4$$$. For the second pair, where $$$p_2 = 12$$$ and $$$q_2 = 6$$$, note that $$$12$$$ is not a valid $$$x_2$$$, since $$$12$$$ is divisible by $$$q_2 = 6$$$; $$$6$$$ is not valid $$$x_2$$$ as well: $$$6$$$ is also divisible by $$$q_2 = 6$$$. The next available divisor of $$$p_2 = 12$$$ is $$$4$$$, which is the answer, since $$$4$$$ is not divisible by $$$6$$$.
1,500
true
false
false
false
false
false
true
false
false
false
3,454
1830D
You are given a tree with $$$n$$$ nodes. For each node, you either color it in $$$0$$$ or $$$1$$$. The value of a path $$$(u,v)$$$ is equal to the MEX$$$^dagger$$$ of the colors of the nodes from the shortest path between $$$u$$$ and $$$v$$$. The value of a coloring is equal to the sum of values of all paths $$$(u,v)$$$ such that $$$1 leq u leq v leq n$$$. What is the maximum possible value of any coloring of the tree? $$$^{dagger}$$$ The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of $$$[2,2,1]$$$ is $$$0$$$, because $$$0$$$ does not belong to the array. The MEX of $$$[3,1,0,1]$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the array, but $$$2$$$ does not. The MEX of $$$[0,3,1,2]$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$, and $$$3$$$ belong to the array, but $$$4$$$ does not. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the number of nodes in the tree. The following $$$n-1$$$ lines of each test case contains $$$2$$$ integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq n, a_i eq b_i$$$)xa0β€” indicating an edge between vertices $$$a_i$$$ and $$$b_i$$$. It is guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print the maximum possible value of any coloring of the tree. Example Input 4 3 1 2 2 3 4 1 2 1 3 1 4 10 1 2 1 3 3 4 3 5 1 6 5 7 2 8 6 9 6 10 1 Note In the first sample, we will color vertex $$$2$$$ in $$$1$$$ and vertices $$$1,3$$$ in $$$0$$$. After this, we consider all paths: $$$(1,1)$$$ with value $$$1$$$ $$$(1,2)$$$ with value $$$2$$$ $$$(1,3)$$$ with value $$$2$$$ $$$(2,2)$$$ with value $$$0$$$ $$$(2,3)$$$ with value $$$2$$$ $$$(3,3)$$$ with value $$$1$$$ We notice the sum of values is $$$8$$$ which is the maximum possible.
2,800
false
false
false
true
false
false
true
false
false
false
1,303
2021A
Pak Chanek has an array $$$a$$$ of $$$n$$$ positive integers. Since he is currently learning how to calculate the floored average of two numbers, he wants to practice it on his array $$$a$$$. While the array $$$a$$$ has at least two elements, Pak Chanek will perform the following three-step operation: 1. Pick two different indices $$$i$$$ and $$$j$$$ ($$$1 leq i, j leq a$$$; $$$i eq j$$$), note that $$$a$$$ denotes the current size of the array $$$a$$$. 2. Append $$$lfloor frac{a_i+a_j}{2} floor$$$$$$^{ ext{βˆ—}}$$$ to the end of the array. 3. Remove elements $$$a_i$$$ and $$$a_j$$$ from the array and concatenate the remaining parts of the array. For example, suppose that $$$a=[5,4,3,2,1,1]$$$. If we choose $$$i=1$$$ and $$$j=5$$$, the resulting array will be $$$a=[4,3,2,1,3]$$$. If we choose $$$i=4$$$ and $$$j=3$$$, the resulting array will be $$$a=[5,4,1,1,2]$$$. After all operations, the array will consist of a single element $$$x$$$. Find the maximum possible value of $$$x$$$ if Pak Chanek performs the operations optimally. $$$^{ ext{βˆ—}}$$$$$$lfloor x floor$$$ denotes the floor function of $$$x$$$, which is the greatest integer that is less than or equal to $$$x$$$. For example, $$$lfloor 6 floor = 6$$$, $$$lfloor 2.5 floor=2$$$, $$$lfloor -3.6 floor=-4$$$ and $$$lfloor pi floor=3$$$ Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 5000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 50$$$)xa0β€” the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β€” the elements of the array $$$a$$$. Do note that the sum of $$$n$$$ over all test cases is not bounded. Note In the first test case, the array is initially $$$a=[1,7,8,4,5]$$$. Pak Chanek will perform the following operations: 1. Pick $$$i=1$$$ and $$$j=2$$$, then $$$a=[8,4,5,4]$$$. 2. Pick $$$i=3$$$ and $$$j=2$$$, then $$$a=[8,4,4]$$$. 3. Pick $$$i=2$$$ and $$$j=3$$$, then $$$a=[8,4]$$$. 4. Pick $$$i=1$$$ and $$$j=2$$$, then $$$a=[6]$$$. After all the operations, the array consists of a single element $$$x=6$$$. It can be proven that there is no series of operations that results in $$$x$$$ greater than $$$6$$$ in the end.
800
true
true
false
false
true
false
false
false
true
false
146
1080B
Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them. Recently, she was presented with an array $$$a$$$ of the size of $$$10^9$$$ elements that is filled as follows: $$$a_1 = -1$$$ $$$a_2 = 2$$$ $$$a_3 = -3$$$ $$$a_4 = 4$$$ $$$a_5 = -5$$$ And so on ... That is, the value of the $$$i$$$-th element of the array $$$a$$$ is calculated using the formula $$$a_i = i cdot (-1)^i$$$. She immediately came up with $$$q$$$ queries on this array. Each query is described with two numbers: $$$l$$$ and $$$r$$$. The answer to a query is the sum of all the elements of the array at positions from $$$l$$$ to $$$r$$$ inclusive. Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to youxa0β€” the best programmer. Help her find the answers! Input The first line contains a single integer $$$q$$$ ($$$1 le q le 10^3$$$)xa0β€” the number of the queries. Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le 10^9$$$)xa0β€” the descriptions of the queries. Output Print $$$q$$$ lines, each containing one numberxa0β€” the answer to the query. Example Input 5 1 3 2 5 5 5 4 4 2 3 Note In the first query, you need to find the sum of the elements of the array from position $$$1$$$ to position $$$3$$$. The sum is equal to $$$a_1 + a_2 + a_3 = -1 + 2 -3 = -2$$$. In the second query, you need to find the sum of the elements of the array from position $$$2$$$ to position $$$5$$$. The sum is equal to $$$a_2 + a_3 + a_4 + a_5 = 2 -3 + 4 - 5 = -2$$$. In the third query, you need to find the sum of the elements of the array from position $$$5$$$ to position $$$5$$$. The sum is equal to $$$a_5 = -5$$$. In the fourth query, you need to find the sum of the elements of the array from position $$$4$$$ to position $$$4$$$. The sum is equal to $$$a_4 = 4$$$. In the fifth query, you need to find the sum of the elements of the array from position $$$2$$$ to position $$$3$$$. The sum is equal to $$$a_2 + a_3 = 2 - 3 = -1$$$.
900
true
false
false
false
false
false
false
false
false
false
5,328
1781D
You are given a set $$$a_1, a_2, ldots, a_n$$$ of distinct positive integers. We define the squareness of an integer $$$x$$$ as the number of perfect squares among the numbers $$$a_1 + x, a_2 + x, ldots, a_n + x$$$. Find the maximum squareness among all integers $$$x$$$ between $$$0$$$ and $$$10^{18}$$$, inclusive. Perfect squares are integers of the form $$$t^2$$$, where $$$t$$$ is a non-negative integer. The smallest perfect squares are $$$0, 1, 4, 9, 16, ldots$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 50$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$xa0($$$1 le n le 50$$$)xa0β€” the size of the set. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ in increasing orderxa0($$$1 le a_1 < a_2 < ldots < a_n le 10^9$$$)xa0β€” the set itself. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$50$$$. Output For each test case, print a single integerxa0β€” the largest possible number of perfect squares among $$$a_1 + x, a_2 + x, ldots, a_n + x$$$, for some $$$0 le x le 10^{18}$$$. Example Input 4 5 1 2 3 4 5 5 1 6 13 22 97 1 100 5 2 5 10 17 26 Note In the first test case, for $$$x = 0$$$ the set contains two perfect squares: $$$1$$$ and $$$4$$$. It is impossible to obtain more than two perfect squares. In the second test case, for $$$x = 3$$$ the set looks like $$$4, 9, 16, 25, 100$$$, that is, all its elements are perfect squares.
1,800
true
false
false
false
false
false
true
false
false
false
1,585
341E
Iahub is playing an uncommon game. Initially, he has _n_ boxes, numbered 1, 2, 3, ..., _n_. Each box has some number of candies in it, described by a sequence _a_1, _a_2, ..., _a__n_. The number _a__k_ represents the number of candies in box _k_. The goal of the game is to move all candies into exactly two boxes. The rest of _n_u2009-u20092 boxes must contain zero candies. Iahub is allowed to do several (possible zero) moves. At each move he chooses two different boxes _i_ and _j_, such that _a__i_u2009≀u2009_a__j_. Then, Iahub moves from box _j_ to box _i_ exactly _a__i_ candies. Obviously, when two boxes have equal number of candies, box number _j_ becomes empty. Your task is to give him a set of moves such as Iahub to archive the goal of the game. If Iahub can't win the game for the given configuration of boxes, output -1. Please note that in case there exist a solution, you don't need to print the solution using minimal number of moves. Input The first line of the input contains integer _n_ (3u2009≀u2009_n_u2009≀u20091000). The next line contains _n_ non-negative integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ β€” sequence elements. It is guaranteed that sum of all numbers in sequence _a_ is up to 106. Output In case there exists no solution, output -1. Otherwise, in the first line output integer _c_ (0u2009≀u2009_c_u2009≀u2009106), representing number of moves in your solution. Each of the next _c_ lines should contain two integers _i_ and _j_ (1u2009≀u2009_i_,u2009_j_u2009≀u2009_n_,u2009_i_u2009β‰ u2009_j_): integers _i_, _j_ in the _k_th line mean that at the _k_-th move you will move candies from the _j_-th box to the _i_-th one. Note For the first sample, after the first move the boxes will contain 3, 12 and 3 candies. After the second move, the boxes will contain 6, 12 and 0 candies. Now all candies are in exactly 2 boxes. For the second sample, you can observe that the given configuration is not valid, as all candies are in a single box and they should be in two boxes. Also, any move won't change the configuration, so there exists no solution. For the third sample, all candies are already in 2 boxes. Hence, no move is needed.
3,000
false
true
false
false
false
true
false
false
false
false
8,469
86A
Problem - 86A - Codeforces =============== xa0 ") which is obtained from _n_ by replacing every digit _a_ in the decimal notation of _n_ with the digit (9u2009u2009-u2009u2009_a_). We say that ψ(_n_) is the reflection of _n_. For example, reflection of 192 equals 807. Note that leading zeros (if any) should be omitted. So reflection of 9 equals 0, reflection of 91 equals 8. Let us call the weight of the number the product of the number and its reflection. Thus, the weight of the number 10 is equal to 10·89u2009=u2009890. Your task is to find the maximum weight of the numbers in the given range
1,600
true
false
false
false
false
false
false
false
false
false
9,541
1768C
You are given an array $$$a$$$ of $$$n$$$ integers. Find two permutations$$$^dagger$$$ $$$p$$$ and $$$q$$$ of length $$$n$$$ such that $$$max(p_i,q_i)=a_i$$$ for all $$$1 leq i leq n$$$ or report that such $$$p$$$ and $$$q$$$ do not exist. $$$^dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 leq a_i leq n$$$)xa0β€” the array $$$a$$$. It is guaranteed that the total sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, if there do not exist $$$p$$$ and $$$q$$$ that satisfy the conditions, output "NO" (without quotes). Otherwise, output "YES" (without quotes) and then output $$$2$$$ lines. The first line should contain $$$n$$$ integers $$$p_1,p_2,ldots,p_n$$$ and the second line should contain $$$n$$$ integers $$$q_1,q_2,ldots,q_n$$$. If there are multiple solutions, you may output any of them. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). Example Output YES 1 1 YES 1 3 4 2 5 5 2 3 1 4 NO Note In the first test case, $$$p=q=[1]$$$. It is correct since $$$a_1 = max(p_1,q_1) = 1$$$. In the second test case, $$$p=[1,3,4,2,5]$$$ and $$$q=[5,2,3,1,4]$$$. It is correct since: $$$a_1 = max(p_1, q_1) = max(1, 5) = 5$$$, $$$a_2 = max(p_2, q_2) = max(3, 2) = 3$$$, $$$a_3 = max(p_3, q_3) = max(4, 3) = 4$$$, $$$a_4 = max(p_4, q_4) = max(2, 1) = 2$$$, $$$a_5 = max(p_5, q_5) = max(5, 4) = 5$$$. In the third test case, one can show that no such $$$p$$$ and $$$q$$$ exist.
1,300
false
true
true
false
false
true
false
false
true
false
1,689
1033A
Alice and Bob are playing chess on a huge chessboard with dimensions $$$n imes n$$$. Alice has a single piece leftxa0β€” a queen, located at $$$(a_x, a_y)$$$, while Bob has only the king standing at $$$(b_x, b_y)$$$. Alice thinks that as her queen is dominating the chessboard, victory is hers. But Bob has made a devious plan to seize the victory for himselfxa0β€” he needs to march his king to $$$(c_x, c_y)$$$ in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns. Bob will win if he can move his king from $$$(b_x, b_y)$$$ to $$$(c_x, c_y)$$$ without ever getting in check. Remember that a king can move to any of the $$$8$$$ adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen. Find whether Bob can win or not. Input The first line contains a single integer $$$n$$$ ($$$3 leq n leq 1000$$$)xa0β€” the dimensions of the chessboard. The second line contains two integers $$$a_x$$$ and $$$a_y$$$ ($$$1 leq a_x, a_y leq n$$$)xa0β€” the coordinates of Alice's queen. The third line contains two integers $$$b_x$$$ and $$$b_y$$$ ($$$1 leq b_x, b_y leq n$$$)xa0β€” the coordinates of Bob's king. The fourth line contains two integers $$$c_x$$$ and $$$c_y$$$ ($$$1 leq c_x, c_y leq n$$$)xa0β€” the coordinates of the location that Bob wants to get to. It is guaranteed that Bob's king is currently not in check and the target location is not in check either. Furthermore, the king is not located on the same square as the queen (i.e. $$$a_x eq b_x$$$ or $$$a_y eq b_y$$$), and the target does coincide neither with the queen's position (i.e. $$$c_x eq a_x$$$ or $$$c_y eq a_y$$$) nor with the king's position (i.e. $$$c_x eq b_x$$$ or $$$c_y eq b_y$$$). Output Print "YES" (without quotes) if Bob can get from $$$(b_x, b_y)$$$ to $$$(c_x, c_y)$$$ without ever getting in check, otherwise print "NO". You can print each letter in any case (upper or lower). Note In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue. In the first case, the king can move, for instance, via the squares $$$(2, 3)$$$ and $$$(3, 2)$$$. Note that the direct route through $$$(2, 2)$$$ goes through check. In the second case, the queen watches the fourth rank, and the king has no means of crossing it. In the third case, the queen watches the third file.
1,000
false
false
true
false
false
false
false
false
false
true
5,535
1092D2
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches. The current state of the wall can be respresented by a sequence $$$a$$$ of $$$n$$$ integers, with $$$a_i$$$ being the height of the $$$i$$$-th part of the wall. Vova can only use $$$2 imes 1$$$ bricks to put in the wall (he has infinite supply of them, however). Vova can put bricks only horizontally on the neighbouring parts of the wall of equal height. It means that if for some $$$i$$$ the current height of part $$$i$$$ is the same as for part $$$i + 1$$$, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part $$$1$$$ of the wall or to the right of part $$$n$$$ of it). Note that Vova can't put bricks vertically. Vova is a perfectionist, so he considers the wall completed when: all parts of the wall has the same height; the wall has no empty spaces inside it. Can Vova complete the wall using any amount of bricks (possibly zero)? Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β€” the number of parts in the wall. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) β€” the initial heights of the parts of the wall. Output Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero). Print "NO" otherwise. Note In the first example Vova can put a brick on parts 2 and 3 to make the wall $$$[2, 2, 2, 2, 5]$$$ and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it $$$[5, 5, 5, 5, 5]$$$. In the second example Vova can put no bricks in the wall. In the third example the wall is already complete.
2,200
false
false
true
false
true
false
false
false
false
false
5,247
1562B
During the hypnosis session, Nicholas suddenly remembered a positive integer $$$n$$$, which doesn't contain zeros in decimal notation. Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one? For some numbers doing so is impossible: for example, for number $$$53$$$ it's impossible to delete some of its digits to obtain a not prime integer. However, for all $$$n$$$ in the test cases of this problem, it's guaranteed that it's possible to delete some of their digits to obtain a not prime number. Note that you cannot remove all the digits from the number. A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. $$$1$$$ is neither a prime nor a composite number. Input Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 le t le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$k$$$ ($$$1 le k le 50$$$)xa0β€” the number of digits in the number. The second line of each test case contains a positive integer $$$n$$$, which doesn't contain zeros in decimal notation ($$$10^{k-1} le n < 10^{k}$$$). It is guaranteed that it is always possible to remove less than $$$k$$$ digits to make the number not prime. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^4$$$. Output For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions. If there are multiple solutions, print any. Example Input 7 3 237 5 44444 3 221 2 35 3 773 1 4 30 626221626221626221626221626221 Output 2 27 1 4 1 1 2 35 2 77 1 4 1 6 Note In the first test case, you can't delete $$$2$$$ digits from the number $$$237$$$, as all the numbers $$$2$$$, $$$3$$$, and $$$7$$$ are prime. However, you can delete $$$1$$$ digit, obtaining a number $$$27 = 3^3$$$. In the second test case, you can delete all digits except one, as $$$4 = 2^2$$$ is a composite number.
1,000
true
false
true
false
false
true
true
false
false
false
2,819
1695D2
The only difference between this problem and D1 is the bound on the size of the tree. You are given an unrooted tree with $$$n$$$ vertices. There is some hidden vertex $$$x$$$ in that tree that you are trying to find. To do this, you may ask $$$k$$$ queries $$$v_1, v_2, ldots, v_k$$$ where the $$$v_i$$$ are vertices in the tree. After you are finished asking all of the queries, you are given $$$k$$$ numbers $$$d_1, d_2, ldots, d_k$$$, where $$$d_i$$$ is the number of edges on the shortest path between $$$v_i$$$ and $$$x$$$. Note that you know which distance corresponds to which query. What is the minimum $$$k$$$ such that there exists some queries $$$v_1, v_2, ldots, v_k$$$ that let you always uniquely identify $$$x$$$ (no matter what $$$x$$$ is). Note that you don't actually need to output these queries. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2cdot10^5$$$) xa0β€” the number of vertices in the tree. Each of the next $$$n-1$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$), meaning there is an edges between vertices $$$x$$$ and $$$y$$$ in the tree. It is guaranteed that the given edges form a tree. 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 nonnegative integer, the minimum number of queries you need, on its own line. Example Input 3 1 2 1 2 10 2 4 2 1 5 7 3 10 8 6 6 1 1 3 4 7 9 6 Note In the first test case, there is only one vertex, so you don't need any queries. In the second test case, you can ask a single query about the node $$$1$$$. Then, if $$$x = 1$$$, you will get $$$0$$$, otherwise you will get $$$1$$$.
2,300
false
true
false
true
false
true
false
false
false
false
2,110
1836A
John is a lead programmer on a destroyer belonging to the space navy of the Confederacy of Independent Operating Systems. One of his tasks is checking if the electronic brains of robots were damaged during battles. A standard test is to order the robots to form one or several lines, in each line the robots should stand one after another. After that, each robot reports the number of robots standing in front of it in its line. An example of robots' arrangement (the front of the lines is on the left). The robots report the numbers above. The $$$i$$$-th robot reported number $$$l_i$$$. Unfortunately, John does not know which line each robot stands in, and can't check the reported numbers. Please determine if it is possible to form the lines in such a way that all reported numbers are correct, or not. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$), denoting the number of test cases. The first line in each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0β€” the number of robots. The second line in each test case contains $$$n$$$ integers $$$l_1, l_2, ldots, l_n$$$ ($$$0 leq l_i < 100$$$), $$$l_i$$$ is equal to the number of robots in front of the $$$i$$$-th robot in its line. The sum of $$$n$$$ over all test cases won't exceed $$$200$$$. Output For each test case, output "YES", if there exists a robot arrangement consistent with robots' reports. Otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 5 6 0 1 2 0 1 0 9 0 0 0 0 1 1 1 2 2 3 0 0 2 1 99 5 0 1 2 3 4 Note Example arrangement consistent with robot statements from the first example test case: Example arrangement consistent with robot statements from the second example is shown in the statement. In the third test case, the third robot claims that there are two machines in front of it. In such a case, the robot directly in front of it would have one machine in front. No robot claims that, so there is no valid arrangement.
800
false
false
true
false
false
false
false
false
true
false
1,272
1211D
Problem - 1211D - Codeforces =============== xa0 ]( "Kotlin Heroes: Episode 2") . The second line contains the sequence of player's ratings β€” integers $$$r_1, r_2, dots, r_n$$$ ($$$1 le r_i le 10^6$$$). Output Print only one integer β€” the maximum number of teams that can be composed from $$$n$$$ given players. Examples Input 12 1 2 2 1 1 2 2 2 2 2 3 3 4 6 6 Output 3 Input 14 1 1 3 3 3 1 1 9 9 2 3 6 6 3 18 3 18 Output 6 Input 1 2 3 10 1000000 Output 0
2,000
true
true
false
false
false
false
false
true
false
false
4,625
1408I
Problem - 1408I - Codeforces =============== xa0 ]( "Grakn Forces 2020 $$$, $$$1 leq k leq 16$$$, $$$1 leq c leq 16$$$). The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$k leq a_i leq 2^c-1$$$). Output Print $$$2^c$$$ integers: the probability that the bitwise XOR is equal to $$$x$$$ in the end for $$$x$$$ in $$${0, 1, ldots, 2^c-1}$$$ modulo $$$998,244,353$$$. Example Input 4 1 3 1 2 3 4 Output 0 0 0 748683265 0 499122177 0 748683265
3,200
true
false
false
true
false
false
false
false
false
false
3,628
1848B
In the summer, Vika likes to visit her country house. There is everything for relaxation: comfortable swings, bicycles, and a river. There is a wooden bridge over the river, consisting of $$$n$$$ planks. It is quite old and unattractive, so Vika decided to paint it. And in the shed, they just found cans of paint of $$$k$$$ colors. After painting each plank in one of $$$k$$$ colors, Vika was about to go swinging to take a break from work. However, she realized that the house was on the other side of the river, and the paint had not yet completely dried, so she could not walk on the bridge yet. In order not to spoil the appearance of the bridge, Vika decided that she would still walk on it, but only stepping on planks of the same color. Otherwise, a small layer of paint on her sole will spoil the plank of another color. Vika also has a little paint left, but it will only be enough to repaint one plank of the bridge. Now Vika is standing on the ground in front of the first plank. To walk across the bridge, she will choose some planks of the same color (after repainting), which have numbers $$$1 le i_1 < i_2 < ldots < i_m le n$$$ (planks are numbered from $$$1$$$ from left to right). Then Vika will have to cross $$$i_1 - 1, i_2 - i_1 - 1, i_3 - i_2 - 1, ldots, i_m - i_{m-1} - 1, n - i_m$$$ planks as a result of each of $$$m + 1$$$ steps. Since Vika is afraid of falling, she does not want to take too long steps. Help her and tell her the minimum possible maximum number of planks she will have to cross in one step, if she can repaint one (or zero) plank a different color while crossing the bridge. Input Each test consists of multiple test cases. The first line contains a single 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 each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$)xa0β€” the number of planks in the bridge and the number of different colors of paint. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, c_3, dots, c_n$$$ ($$$1 le c_i le k$$$)xa0β€” the colors in which Vika painted the planks of the bridge. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0β€” the minimum possible maximum number of planks that Vika will have to step over in one step. Example Input 5 5 2 1 1 2 1 1 7 3 1 2 3 3 3 2 1 6 6 1 2 3 4 5 6 8 4 1 2 3 4 2 3 1 4 3 1 1 1 1 Note In the first test case, Vika can repaint the plank in the middle in color $$$1$$$ and walk across the bridge without stepping over any planks. In the second test case, Vika can repaint the plank in the middle in color $$$2$$$ and walk across the bridge, stepping over only one plank each time. In the third test case, Vika can repaint the penultimate plank in color $$$2$$$ and walk across the bridge, stepping only on planks with numbers $$$2$$$ and $$$5$$$. Then Vika will have to step over $$$1$$$, $$$2$$$ and $$$1$$$ plank each time she steps, so the answer is $$$2$$$. In the fourth test case, Vika can simply walk across the bridge without repainting it, stepping over two planks each time, walking on planks of color $$$3$$$. In the fifth test case, Vika can simply walk across the bridge without repainting it, without stepping over any planks.
1,200
true
true
true
false
true
false
false
true
true
false
1,193