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
1373F
# Network Coverage Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes The government of Berland decided to improve network coverage in his country. Berland has a unique structure: the capital in the center and n cities in a circle around the capital. The capital already has a good network coverage (so the government ignores it), but the i-th city contains ai households that require a connection. The government designed a plan to build n network stations between all pairs of neighboring cities which will maintain connections only for these cities. In other words, the i-th network station will provide service only for the i-th and the (i + 1) -th city (the n-th station is connected to the n-th and the 1-st city). All network stations have capacities: the i-th station can provide the connection to at most bi households. Now the government asks you to check can the designed stations meet the needs of all cities or not x16 that is, is it possible to assign each household a network station so that each network station i provides the connection to at most bi households. # Input The first line contains a single integer t (1 ≤ t ≤ 10 4) x16 the number of test cases. The first line of each test case contains the single integer n (2 ≤ n ≤ 10 6) x16 the number of cities and stations. The second line of each test case contains n integers ( 1 ≤ ai ≤ 10 9) x16 the number of households in the i-th city. The third line of each test case contains n integers ( 1 ≤ bi ≤ 10 9) x16 the capacities of the designed stations. It’s guaranteed that the sum of n over test cases doesn’t exceed 10 6. # Output For each test case, print YES , if the designed stations can meet the needs of all cities, or NO otherwise (case insensitive). # Example standard input standard output 532 3 4 3 3 3 33 3 3 2 3 4 42 3 4 5 3 7 2 2 44 5 2 3 2 3 2 7 21 1 10 10 YES YES NO YES YES Page 1 of 2 Note In the first test case: • the first network station can provide 2 connections to the first city and 1 connection to the second city; • the second station can provide 2 connections to the second city and 1 connection to the third city; • the third station can provide 3 connections to the third city. In the second test case: • the 1-st station can provide 2 connections to the 1-st city; • the 2-nd station can provide 3 connections to the 2-nd city; • the 3-rd station can provide 3 connections to the 3-rd city and 1 connection to the 1-st station. In the third test case, the fourth city needs 5 connections, but the third and the fourth station has 4 connections in total. Page 2 of 2
2,400
false
true
false
false
true
true
false
true
false
false
3,796
228A
Problem - 228A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials ") Tutorial") — the colors of horseshoes Valera has. Consider all possible colors indexed with integers. Output Print a single integer — the minimum number of horseshoes Valera needs to buy. Examples Input 1 7 3 3 Output 1 Input 7 7 7 7 Output 3
800
false
false
true
false
false
false
false
false
false
false
8,928
1208F
You are given an array $$$a$$$ of $$$n$$$ integers. You need to find the maximum value of $$$a_{i} ( a_{j} & a_{k} )$$$ over all triplets $$$(i,j,k)$$$ such that $$$i < j < k$$$. Here $$$&$$$ denotes the , the size of the array $$$a$$$. Next line contains $$$n$$$ space separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 le a_{i} le 2 cdot 10^{6}$$$), representing the elements of the array $$$a$$$. Output Output a single integer, the maximum value of the expression given in the statement. Note In the first example, the only possible triplet is $$$(1, 2, 3)$$$. Hence, the answer is $$$2 (4 & 6) = 6$$$. In the second example, there are $$$4$$$ possible triplets: 1. $$$(1, 2, 3)$$$, value of which is $$$2(8&4) = 2$$$. 2. $$$(1, 2, 4)$$$, value of which is $$$2(8&7) = 2$$$. 3. $$$(1, 3, 4)$$$, value of which is $$$2(4&7) = 6$$$. 4. $$$(2, 3, 4)$$$, value of which is $$$8(4&7) = 12$$$. The maximum value hence is $$$12$$$.
2,600
false
true
false
true
false
false
false
false
false
false
4,649
3C
Problem - 3C - Codeforces =============== xa0 . The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced. You are given a 3u2009×u20093 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below: illegal — if the given board layout can't appear during a valid game; the first player won — if in the given board layout the first player has just won; the second player won — if in the given board layout the second player has just won; draw — if the given board layout has just let to a draw. Input The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero). Output Print one of the six verdicts: first, second, illegal, the first player won, the second player won or draw. Examples Input X0X .0. .X. Output second
1,800
false
false
true
false
false
false
true
false
false
false
9,976
1657E
Problem - 1657E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp graph matchings math *2200 No tag edit access → Contest materials . Output Print one integerxa0— the number of complete beautiful graphs having exactly $$$n$$$ vertices and the weights of edges from $$$1$$$ to $$$k$$$. Since the answer might be large, print it modulo $$$998244353$$$. Examples Input 3 2 Output 5 Input 4 4 Output 571 Input 6 9 Output 310640163 Input 42 13 Output 136246935
2,200
true
false
false
true
false
false
false
false
false
false
2,341
1162A
You are planning to build housing on a street. There are $$$n$$$ spots available on the street on which you can build a house. The spots are labeled from $$$1$$$ to $$$n$$$ from left to right. In each spot, you can build a house with an integer height between $$$0$$$ and $$$h$$$. In each spot, if a house has height $$$a$$$, you will gain $$$a^2$$$ dollars from it. The city has $$$m$$$ zoning restrictions. The $$$i$$$-th restriction says that the tallest house from spots $$$l_i$$$ to $$$r_i$$$ (inclusive) must be at most $$$x_i$$$. You would like to build houses to maximize your profit. Determine the maximum profit possible. Input The first line contains three integers $$$n$$$, $$$h$$$, and $$$m$$$ ($$$1 leq n,h,m leq 50$$$)xa0— the number of spots, the maximum height, and the number of restrictions. Each of the next $$$m$$$ lines contains three integers $$$l_i$$$, $$$r_i$$$, and $$$x_i$$$ ($$$1 leq l_i leq r_i leq n$$$, $$$0 leq x_i leq h$$$)xa0— left and right limits (inclusive) of the $$$i$$$-th restriction and the maximum possible height in that range. Output Print a single integer, the maximum profit you can make. Examples Input 3 3 3 1 1 1 2 2 3 3 3 2 Note In the first example, there are $$$3$$$ houses, the maximum height of a house is $$$3$$$, and there are $$$3$$$ restrictions. The first restriction says the tallest house between $$$1$$$ and $$$1$$$ must be at most $$$1$$$. The second restriction says the tallest house between $$$2$$$ and $$$2$$$ must be at most $$$3$$$. The third restriction says the tallest house between $$$3$$$ and $$$3$$$ must be at most $$$2$$$. In this case, it is optimal to build houses with heights $$$[1, 3, 2]$$$. This fits within all the restrictions. The total profit in this case is $$$1^2 + 3^2 + 2^2 = 14$$$. In the second example, there are $$$4$$$ houses, the maximum height of a house is $$$10$$$, and there are $$$2$$$ restrictions. The first restriction says the tallest house from $$$2$$$ to $$$3$$$ must be at most $$$8$$$. The second restriction says the tallest house from $$$3$$$ to $$$4$$$ must be at most $$$7$$$. In this case, it's optimal to build houses with heights $$$[10, 8, 7, 7]$$$. We get a profit of $$$10^2+8^2+7^2+7^2 = 262$$$. Note that there are two restrictions on house $$$3$$$ and both of them must be satisfied. Also, note that even though there isn't any explicit restrictions on house $$$1$$$, we must still limit its height to be at most $$$10$$$ ($$$h=10$$$).
800
false
false
true
false
false
false
false
false
false
false
4,906
816A
Karen is getting ready for a new school day! It is currently hh:mm, given in a 24-hour format. As you know, Karen loves palindromes, and she believes that it is good luck to wake up when the time is a palindrome. What is the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome? Remember that a palindrome is a string that reads the same forwards and backwards. For instance, 05:39 is not a palindrome, because 05:39 backwards is 93:50. On the other hand, 05:50 is a palindrome, because 05:50 backwards is 05:50. Input The first and only line of input contains a single string in the format hh:mm (00u2009≤u2009 hh u2009≤u200923, 00u2009≤u2009 mm u2009≤u200959). Output Output a single integer on a line by itself, the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome. Note In the first test case, the minimum number of minutes Karen should sleep for is 11. She can wake up at 05:50, when the time is a palindrome. In the second test case, Karen can wake up immediately, as the current time, 13:31, is already a palindrome. In the third test case, the minimum number of minutes Karen should sleep for is 1 minute. She can wake up at 00:00, when the time is a palindrome.
1,000
false
false
true
false
false
false
true
false
false
false
6,495
1348A
Phoenix has $$$n$$$ coins with weights $$$2^1, 2^2, dots, 2^n$$$. He knows that $$$n$$$ is even. He wants to split the coins into two piles such that each pile has exactly $$$frac{n}{2}$$$ coins and the difference of weights between the two piles is minimized. Formally, let $$$a$$$ denote the sum of weights in the first pile, and $$$b$$$ denote the sum of weights in the second pile. Help Phoenix minimize $$$a-b$$$, the absolute value of $$$a-b$$$. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 le n le 30$$$; $$$n$$$ is even)xa0— the number of coins that Phoenix has. Output For each test case, output one integerxa0— the minimum possible difference of weights between the two piles. Note In the first test case, Phoenix has two coins with weights $$$2$$$ and $$$4$$$. No matter how he divides the coins, the difference will be $$$4-2=2$$$. In the second test case, Phoenix has four coins of weight $$$2$$$, $$$4$$$, $$$8$$$, and $$$16$$$. It is optimal for Phoenix to place coins with weights $$$2$$$ and $$$16$$$ in one pile, and coins with weights $$$4$$$ and $$$8$$$ in another pile. The difference is $$$(2+16)-(4+8)=6$$$.
800
true
true
false
false
false
false
false
false
false
false
3,970
1872F
You are the owner of a menagerie consisting of $$$n$$$ animals numbered from $$$1$$$ to $$$n$$$. However, maintaining the menagerie is quite expensive, so you have decided to sell it! It is known that each animal is afraid of exactly one other animal. More precisely, animal $$$i$$$ is afraid of animal $$$a_i$$$ ($$$a_i eq i$$$). Also, the cost of each animal is known, for animal $$$i$$$ it is equal to $$$c_i$$$. You will sell all your animals in some fixed order. Formally, you will need to choose some permutation$$$^dagger$$$ $$$p_1, p_2, ldots, p_n$$$, and sell animal $$$p_1$$$ first, then animal $$$p_2$$$, and so on, selling animal $$$p_n$$$ last. When you sell animal $$$i$$$, there are two possible outcomes: If animal $$$a_i$$$ was sold before animal $$$i$$$, you receive $$$c_i$$$ money for selling animal $$$i$$$. If animal $$$a_i$$$ was not sold before animal $$$i$$$, you receive $$$2 cdot c_i$$$ money for selling animal $$$i$$$. (Surprisingly, animals that are currently afraid are more valuable). Your task is to choose the order of selling the animals in order to maximize the total profit. For example, if $$$a = [3, 4, 4, 1, 3]$$$, $$$c = [3, 4, 5, 6, 7]$$$, and the permutation you choose is $$$[4, 2, 5, 1, 3]$$$, then: The first animal to be sold is animal $$$4$$$. Animal $$$a_4 = 1$$$ was not sold before, so you receive $$$2 cdot c_4 = 12$$$ money for selling it. The second animal to be sold is animal $$$2$$$. Animal $$$a_2 = 4$$$ was sold before, so you receive $$$c_2 = 4$$$ money for selling it. The third animal to be sold is animal $$$5$$$. Animal $$$a_5 = 3$$$ was not sold before, so you receive $$$2 cdot c_5 = 14$$$ money for selling it. The fourth animal to be sold is animal $$$1$$$. Animal $$$a_1 = 3$$$ was not sold before, so you receive $$$2 cdot c_1 = 6$$$ money for selling it. The fifth animal to be sold is animal $$$3$$$. Animal $$$a_3 = 4$$$ was sold before, so you receive $$$c_3 = 5$$$ money for selling it. Your total profit, with this choice of permutation, is $$$12 + 4 + 14 + 6 + 5 = 41$$$. Note that $$$41$$$ is not the maximum possible profit in this example. $$$^dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in any 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 $$$4$$$ is present in the array). Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then follow the descriptions of the test cases. The first line of each test case description contains an integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the number of animals. The second line of the test case description contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$, $$$a_i eq i$$$)xa0— $$$a_i$$$ means the index of the animal that animal $$$i$$$ is afraid of. The third line of the test case description contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le 10^9$$$)xa0— the costs of the animals. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output Output $$$t$$$ lines, each containing the answer to the corresponding test case. The answer should be $$$n$$$ integersxa0— the permutation $$$p_1, p_2, ldots, p_n$$$, indicating in which order to sell the animals in order to maximize the profit. If there are multiple possible answers, you can output any of them.
1,800
true
false
true
false
false
false
false
false
false
true
1,046
431C
Problem - 431C - 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 trees *1600 No tag edit access → Contest materials ") ") , then their weights will equal 1,u20092,u20093,u2009...,u2009_k_. The picture below shows a part of a 3-tree. As soon as Dima, a good friend of Lesha, found out about the tree, he immediately wondered: "How many paths of total weight _n_ (the sum of all weights of the edges in the path) are there, starting from the root of a _k_-tree and also containing at least one edge of weight at least _d_?".Help Dima find an answer to his question. As the number of ways can be rather large, print it modulo 1000000007 (109u2009+u20097). Input A single line contains three space-separated integers: _n_, _k_ and _d_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009100; 1u2009≤u2009_d_u2009≤u2009_k_). Output Print a single integer — the answer to the problem modulo 1000000007 (109u2009+u20097). Examples Input 3 3 2 Output 3 Input 3 3 3 Output 1 Input 4 3 2 Output 6 Input 4 5 2 Output 7
1,600
false
false
true
true
false
false
false
false
false
false
8,117
1919A
Alice and Bob are bored, so they decide to play a game with their wallets. Alice has $$$a$$$ coins in her wallet, while Bob has $$$b$$$ coins in his wallet. Both players take turns playing, with Alice making the first move. In each turn, the player will perform the following steps in order: 1. Choose to exchange wallets with their opponent, or to keep their current wallets. 2. Remove $$$1$$$ coin from the player's current wallet. The current wallet cannot have $$$0$$$ coins before performing this step. The player who cannot make a valid move on their turn loses. If both Alice and Bob play optimally, determine who will win the game. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The description of the test cases follows. The first and only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^9$$$)xa0— the number of coins in Alice's and Bob's wallets, respectively. Output For each test case, output "Alice" if Alice will win the game, and "Bob" if Bob will win the game. Example Input 10 1 1 1 4 5 3 4 5 11 9 83 91 1032 9307 839204 7281 1000000000 1000000000 53110 2024 Output Bob Alice Bob Alice Bob Bob Alice Alice Bob Bob Note In the first test case, an example of the game is shown below: Alice chooses to not swap wallets with Bob in step 1 of her move. Now, $$$a=0$$$ and $$$b=1$$$. Since Alice's wallet is empty, Bob must choose to not swap their wallets in step 1 of his move. Now, $$$a=0$$$ and $$$b=0$$$. Since both Alice's and Bob's wallets are empty, Alice is unable to make a move. Hence, Bob wins. In the second test case, an example of the game is shown below: Alice chooses to swap wallets with Bob in step 1 of her move. Now, $$$a=3$$$ and $$$b=1$$$. Bob chooses to swap wallets with Alice in step 1 of his move. Now, $$$a=1$$$ and $$$b=2$$$. Alice chooses to not swap wallets with Bob in step 1 of her move. Now, $$$a=0$$$ and $$$b=2$$$. Since Alice's wallet is empty, Bob can only choose to not swap wallets with Alice in step 1 of his move. Now, $$$a=0$$$ and $$$b=1$$$. Since Alice's wallet is empty, Alice can only choose to swap wallets with Bob in step 1 of her move. Now, $$$a=0$$$ and $$$b=0$$$. Since both Alice's wallet and Bob's wallet are empty, Bob is unable to make a move. Hence, Alice wins.
800
true
false
false
false
false
false
false
false
false
false
781
118E
Bertown has _n_ junctions and _m_ bidirectional roads. We know that one can get from any junction to any other one by the existing roads. As there were more and more cars in the city, traffic jams started to pose real problems. To deal with them the government decided to make the traffic one-directional on all the roads, thus easing down the traffic. Your task is to determine whether there is a way to make the traffic one-directional so that there still is the possibility to get from any junction to any other one. If the answer is positive, you should also find one of the possible ways to orient the roads. Input The first line contains two space-separated integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009105,u2009_n_u2009-u20091u2009≤u2009_m_u2009≤u20093·105) which represent the number of junctions and the roads in the town correspondingly. Then follow _m_ lines, each containing two numbers which describe the roads in the city. Each road is determined by two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_) — the numbers of junctions it connects. It is guaranteed that one can get from any junction to any other one along the existing bidirectional roads. Each road connects different junctions, there is no more than one road between each pair of junctions. Output If there's no solution, print the single number 0. Otherwise, print _m_ lines each containing two integers _p__i_ and _q__i_ — each road's orientation. That is the traffic flow will move along a one-directional road from junction _p__i_ to junction _q__i_. You can print the roads in any order. If there are several solutions to that problem, print any of them. Examples Input 6 8 1 2 2 3 1 3 4 5 4 6 5 6 2 4 3 5 Output 1 2 2 3 3 1 4 5 5 6 6 4 4 2 3 5 Input 6 7 1 2 2 3 1 3 4 5 4 6 5 6 2 4
2,000
false
false
false
false
false
false
false
false
false
true
9,412
1305E
Kuroni is the coordinator of the next Mathforces round written by the "Proof by AC" team. All the preparation has been done, and he is discussing with the team about the score distribution for the round. The round consists of $$$n$$$ problems, numbered from $$$1$$$ to $$$n$$$. The problems are ordered in increasing order of difficulty, no two problems have the same difficulty. A score distribution for the round can be denoted by an array $$$a_1, a_2, dots, a_n$$$, where $$$a_i$$$ is the score of $$$i$$$-th problem. Kuroni thinks that the score distribution should satisfy the following requirements: The score of each problem should be a positive integer not exceeding $$$10^9$$$. A harder problem should grant a strictly higher score than an easier problem. In other words, $$$1 leq a_1 < a_2 < dots < a_n leq 10^9$$$. The balance of the score distribution, defined as the number of triples $$$(i, j, k)$$$ such that $$$1 leq i < j < k leq n$$$ and $$$a_i + a_j = a_k$$$, should be exactly $$$m$$$. Help the team find a score distribution that satisfies Kuroni's requirement. In case such a score distribution does not exist, output $$$-1$$$. Input The first and single line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 5000$$$, $$$0 leq m leq 10^9$$$)xa0— the number of problems and the required balance. Output If there is no solution, print a single integer $$$-1$$$. Otherwise, print a line containing $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$, representing a score distribution that satisfies all the requirements. If there are multiple answers, print any of them. Examples Output 10 11 12 13 14 15 16 17 Note In the first example, there are $$$3$$$ triples $$$(i, j, k)$$$ that contribute to the balance of the score distribution. $$$(1, 2, 3)$$$ $$$(1, 3, 4)$$$ $$$(2, 4, 5)$$$
2,200
true
true
true
false
false
true
false
false
false
false
4,160
1535E
You are given a rooted tree. Each vertex contains $$$a_i$$$ tons of gold, which costs $$$c_i$$$ per one ton. Initially, the tree consists only a root numbered $$$0$$$ with $$$a_0$$$ tons of gold and price $$$c_0$$$ per ton. There are $$$q$$$ queries. Each query has one of two types: 1. Add vertex $$$i$$$ (where $$$i$$$ is an index of query) as a son to some vertex $$$p_i$$$; vertex $$$i$$$ will have $$$a_i$$$ tons of gold with $$$c_i$$$ per ton. It's guaranteed that $$$c_i > c_{p_i}$$$. 2. For a given vertex $$$v_i$$$ consider the simple path from $$$v_i$$$ to the root. We need to purchase $$$w_i$$$ tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can. If we buy $$$x$$$ tons of gold in some vertex $$$v$$$ the remaining amount of gold in it decreases by $$$x$$$ (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well. Input The first line contains three integers $$$q$$$, $$$a_0$$$ and $$$c_0$$$ ($$$1 le q le 3 cdot 10^5$$$; $$$1 le a_0, c_0 < 10^6$$$)xa0— the number of queries, the amount of gold in the root and its price. Next $$$q$$$ lines contain descriptions of queries; The $$$i$$$-th query has one of two types: "$$$1$$$ $$$p_i$$$ $$$a_i$$$ $$$c_i$$$" ($$$0 le p_i < i$$$; $$$1 le a_i, c_i < 10^6$$$): add vertex $$$i$$$ as a son to vertex $$$p_i$$$. The vertex $$$i$$$ will have $$$a_i$$$ tons of gold with price $$$c_i$$$ per one ton. It's guaranteed that $$$p_i$$$ exists and $$$c_i > c_{p_i}$$$. "$$$2$$$ $$$v_i$$$ $$$w_i$$$" ($$$0 le v_i < i$$$; $$$1 le w_i < 10^6$$$): buy $$$w_i$$$ tons of gold from vertices on path from $$$v_i$$$ to $$$0$$$ spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex $$$v_i$$$ exist. It's guaranteed that there is at least one query of the second type. Output For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend. Example Input 5 5 2 2 0 2 1 0 3 4 2 2 4 1 0 1 3 2 4 2 Note Explanation of the sample: At the first query, the tree consist of root, so we purchase $$$2$$$ tons of gold and pay $$$2 cdot 2 = 4$$$. $$$3$$$ tons remain in the root. At the second query, we add vertex $$$2$$$ as a son of vertex $$$0$$$. Vertex $$$2$$$ now has $$$3$$$ tons of gold with price $$$4$$$ per one ton. At the third query, a path from $$$2$$$ to $$$0$$$ consists of only vertices $$$0$$$ and $$$2$$$ and since $$$c_0 < c_2$$$ we buy $$$3$$$ remaining tons of gold in vertex $$$0$$$ and $$$1$$$ ton in vertex $$$2$$$. So we bought $$$3 + 1 = 4$$$ tons and paid $$$3 cdot 2 + 1 cdot 4 = 10$$$. Now, in vertex $$$0$$$ no gold left and $$$2$$$ tons of gold remain in vertex $$$2$$$. At the fourth query, we add vertex $$$4$$$ as a son of vertex $$$0$$$. Vertex $$$4$$$ now has $$$1$$$ ton of gold with price $$$3$$$. At the fifth query, a path from $$$4$$$ to $$$0$$$ consists of only vertices $$$0$$$ and $$$4$$$. But since no gold left in vertex $$$0$$$ and only $$$1$$$ ton is in vertex $$$4$$$, we buy $$$1$$$ ton of gold in vertex $$$4$$$ and spend $$$1 cdot 3 = 3$$$. Now, in vertex $$$4$$$ no gold left.
2,200
false
true
false
true
true
false
false
true
false
false
2,970
1582B
Luntik came out for a morning stroll and found an array $$$a$$$ of length $$$n$$$. He calculated the sum $$$s$$$ of the elements of the array ($$$s= sum_{i=1}^{n} a_i$$$). Luntik calls a subsequence of the array $$$a$$$ nearly full if the sum of the numbers in that subsequence is equal to $$$s-1$$$. Luntik really wants to know the number of nearly full subsequences of the array $$$a$$$. But he needs to come home so he asks you to solve that problem! A sequence $$$x$$$ is a subsequence of a sequence $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) elements. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The next $$$2 cdot t$$$ lines contain descriptions of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 60$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$) — the elements of the array $$$a$$$. Output For each test case print the number of nearly full subsequences of the array. Example Input 5 5 1 2 3 4 5 2 1000 1000 2 1 0 5 3 0 2 1 1 5 2 1 0 3 0 Note In the first test case, $$$s=1+2+3+4+5=15$$$, only $$$(2,3,4,5)$$$ is a nearly full subsequence among all subsequences, the sum in it is equal to $$$2+3+4+5=14=15-1$$$. In the second test case, there are no nearly full subsequences. In the third test case, $$$s=1+0=1$$$, the nearly full subsequences are $$$(0)$$$ and $$$()$$$ (the sum of an empty subsequence is $$$0$$$).
900
true
false
false
false
false
false
false
false
false
false
2,724
1277A
Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp! Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years? According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$. Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10). Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 le n le 10^9$$$) — how many years Polycarp has turned. Output Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive. Example Input 6 18 1 9 100500 33 1000000000 Note In the first test case of the example beautiful years are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ and $$$11$$$.
1,000
false
false
true
false
false
false
false
false
false
false
4,309
2038C
You are given a list of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$. You need to pick $$$8$$$ elements from the list and use them as coordinates of four points. These four points should be corners of a rectangle which has its sides parallel to the coordinate axes. Your task is to pick coordinates in such a way that the resulting rectangle has the maximum possible area. The rectangle can be degenerate, i.u2009e. its area can be $$$0$$$. Each integer can be used as many times as it occurs in the list (or less). Input The first line contains one integer $$$t$$$ ($$$1 le t le 25,000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$8 le n le 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print the answer as follows: if it is impossible to construct a rectangle which meets the constraints from the statement, print a single line containing the word NO (case-insensitive); otherwise, in the first line, print YES (case-insensitive). In the second line, print $$$8$$$ integers $$$x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4$$$ — the coordinates of the corners of the rectangle. You can print the corners in any order. Example Input 3 16 -5 1 1 2 2 3 3 4 4 5 5 6 6 7 7 10 8 0 0 -1 2 2 1 1 3 8 0 0 0 0 0 5 0 5 Output YES 1 2 1 7 6 2 6 7 NO YES 0 0 0 5 0 0 0 5
1,400
false
true
false
false
true
false
false
false
true
false
36
174B
Eudokimus, a system administrator is in trouble again. As a result of an error in some script, a list of names of very important files has been damaged. Since they were files in the BerFS file system, it is known that each file name has a form "name.ext", where: name is a string consisting of lowercase Latin letters, its length is from 1 to 8 characters; ext is a string consisting of lowercase Latin letters, its length is from 1 to 3 characters. For example, "read.me", "example.txt" and "b.cpp" are valid file names and "version.info", "ntldr" and "contestdata.zip" are not. Damage to the list meant that all the file names were recorded one after another, without any separators. So now Eudokimus has a single string. Eudokimus needs to set everything right as soon as possible. He should divide the resulting string into parts so that each part would be a valid file name in BerFS. Since Eudokimus has already proved that he is not good at programming, help him. The resulting file list can contain the same file names. Input The input data consists of a single string _s_, its length is from 1 to 4·105 characters. The string can contain only lowercase Latin letters ('a' - 'z') and periods ('.'). Output In the first line print "YES" (without the quotes), if it is possible to divide _s_ into parts as required. In this case, the following lines should contain the parts of the required partition, one per line in the order in which they appear in _s_. The required partition can contain the same file names. If there are multiple solutions, print any of them. If the solution does not exist, then print in a single line "NO" (without the quotes). Examples Input read.meexample.txtb.cpp Output YES read.m eexample.t xtb.cpp Input version.infontldrcontestdata.zip
1,400
false
true
true
true
false
false
false
false
false
false
9,167
316B2
In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another. (Cultural note: standing in huge and disorganized queues for hours is a native tradition in Russia, dating back to the Soviet period. Queues can resemble crowds rather than lines. Not to get lost in such a queue, a person should follow a strict survival technique: you approach the queue and ask who the last person is, somebody answers and you join the crowd. Now you're the last person in the queue till somebody else shows up. You keep an eye on the one who was last before you as he is your only chance to get to your destination) I'm sure many people have had the problem when a stranger asks who the last person in the queue is and even dares to hint that he will be the last in the queue and then bolts away to some unknown destination. These are the representatives of the modern world, in which the ratio of lack of time is so great that they do not even watch foreign top-rated TV series. Such people often create problems in queues, because the newcomer does not see the last person in the queue and takes a place after the "virtual" link in this chain, wondering where this legendary figure has left. The Smart Beaver has been ill and he's made an appointment with a therapist. The doctor told the Beaver the sad news in a nutshell: it is necessary to do an electrocardiogram. The next day the Smart Beaver got up early, put on the famous TV series on download (three hours till the download's complete), clenched his teeth and bravely went to join a queue to the electrocardiogram room, which is notorious for the biggest queues at the clinic. Having stood for about three hours in the queue, the Smart Beaver realized that many beavers had not seen who was supposed to stand in the queue before them and there was a huge mess. He came up to each beaver in the ECG room queue and asked who should be in front of him in the queue. If the beaver did not know his correct position in the queue, then it might be his turn to go get an ECG, or maybe he should wait for a long, long time... As you've guessed, the Smart Beaver was in a hurry home, so he gave you all the necessary information for you to help him to determine what his number in the queue can be. Input The first line contains two integers _n_ (1u2009≤u2009_n_u2009≤u2009103) and _x_ (1u2009≤u2009_x_u2009≤u2009_n_) — the number of beavers that stand in the queue and the Smart Beaver's number, correspondingly. All willing to get to the doctor are numbered from 1 to _n_. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009_n_) — the number of the beaver followed by the _i_-th beaver. If _a__i_u2009=u20090, then the _i_-th beaver doesn't know who is should be in front of him. It is guaranteed that values _a__i_ are correct. That is there is no cycles in the dependencies. And any beaver is followed by at most one beaver in the queue. The input limits for scoring 30 points are (subproblem B1): It is guaranteed that the number of zero elements _a__i_ doesn't exceed 20. The input limits for scoring 100 points are (subproblems B1+B2): The number of zero elements _a__i_ is arbitrary.
1,600
false
false
false
true
false
false
false
false
false
false
8,580
1267H
Very soon, the new cell phone services provider "BerLine" will begin its work in Berland! The start of customer service is planned along the main street of the capital. There are $$$n$$$xa0base stations that are already installed. They are located one after another along the main street in the order from thexa0$$$1$$$-st to thexa0$$$n$$$-th from left to right. Currently, all these base stations are turned off. They will be turned on one by one, one base station per day, according to some permutation $$$p = [p_1, p_2, dots, p_n]$$$ ($$$ 1 le p_i le n$$$), where $$$p_i$$$ is the index of a base station that will be turned on on the $$$i$$$-th day. Thus, it will take $$$n$$$ days to turn on all base stations. Each base station is characterized by its operating frequency $$$f_i$$$xa0— an integer between $$$1$$$ and $$$24$$$, inclusive. There is an important requirement for operating frequencies of base stations. Consider an arbitrary moment in time. For any phone owner, if we consider all base stations turned on in the access area of their phone, then in this set of base stations there should be at least one whose operating frequency is unique among the frequencies of these stations. Since the power of the phone and the position are not known in advance, this means that for any nonempty subsegment of turned on base stations, at least one of them has to have the operating frequency that is unique among the stations of this subsegment. For example, let's take a look at a case of $$$n = 7$$$, all $$$n$$$ stations are turned on, and their frequencies are equal to $$$f = [1, 2, 1, 3, 1, 2, 1]$$$. Consider any subsegment of the base stationsxa0— there is a base station with a unique frequency within this subsegment. However, if $$$f = [1, 2, 1, 2, 3, 2, 1]$$$, then there is no unique frequency on the segment $$$[1, 2, 1, 2]$$$ from the index $$$1$$$ to the index $$$4$$$, inclusive. Your task is to assign a frequency from $$$1$$$ to $$$24$$$ to each of $$$n$$$ base stations in such a way that the frequency requirement is met at every moment. Remember that the base stations are turned on in the order of the given permutation $$$p$$$. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 50$$$)xa0— the number of test cases in the input. Then $$$t$$$ test case descriptions follow. The first line of a test case contains an integer $$$n$$$ ($$$ 1 le n le 8,500$$$)xa0— the number of "BerLine" base stations. The following line contains $$$n$$$ distinct integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$)xa0— the order in which the base stations are turned on, i.xa0e. on the $$$i$$$-th day the base station with the index $$$p_i$$$ is turned on. It is guaranteed that a correct answer exists for all test cases in the input. Output Print exactly $$$t$$$ lines, where the $$$j$$$-th line contains the answer for the $$$j$$$-th test case in the input. Print the required frequencies $$$f_1, f_2, dots, f_n$$$ ($$$1 le f_i le 24$$$). If there are several possible answers, print any of them. Note In the first test case $$$n = 3$$$ and $$$p = [1, 3, 2]$$$. The base stations can be assigned frequencies $$$[1, 3, 2]$$$. Day 1: only the base station $$$1$$$ is turned on, its frequency is $$$1$$$. Day 2: the base stations $$$1$$$ and $$$3$$$ are turned on, their frequencies are $$$[1, 2]$$$. Day 3: all base stations are turned on, their frequencies are $$$[1, 3, 2]$$$ (in the direction along the street). On each day, each nonempty subsegment of turned on base stations has a base station with a unique frequency among this subsegment. It can be shown that three distinct frequencies are necessary in this test case.
3,200
false
false
false
false
false
true
false
false
false
false
4,356
259A
The Little Elephant loves chess very much. One day the Little Elephant and his friend decided to play chess. They've got the chess pieces but the board is a problem. They've got an 8u2009×u20098 checkered board, each square is painted either black or white. The Little Elephant and his friend know that a proper chessboard doesn't have any side-adjacent cells with the same color and the upper left cell is white. To play chess, they want to make the board they have a proper chessboard. For that the friends can choose any row of the board and cyclically shift the cells of the chosen row, that is, put the last (rightmost) square on the first place in the row and shift the others one position to the right. You can run the described operation multiple times (or not run it at all). For example, if the first line of the board looks like that "BBBBBBWW" (the white cells of the line are marked with character "W", the black cells are marked with character "B"), then after one cyclic shift it will look like that "WBBBBBBW". Help the Little Elephant and his friend to find out whether they can use any number of the described operations to turn the board they have into a proper chessboard. Input The input consists of exactly eight lines. Each line contains exactly eight characters "W" or "B" without any spaces: the _j_-th character in the _i_-th line stands for the color of the _j_-th cell of the _i_-th row of the elephants' board. Character "W" stands for the white color, character "B" stands for the black color. Consider the rows of the board numbered from 1 to 8 from top to bottom, and the columns — from 1 to 8 from left to right. The given board can initially be a proper chessboard. Output In a single line print "YES" (without the quotes), if we can make the board a proper chessboard and "NO" (without the quotes) otherwise. Examples Input WBWBWBWB BWBWBWBW BWBWBWBW BWBWBWBW WBWBWBWB WBWBWBWB BWBWBWBW WBWBWBWB Input WBWBWBWB WBWBWBWB BBWBWWWB BWBWBWBW BWBWBWBW BWBWBWWW BWBWBWBW BWBWBWBW Note In the first sample you should shift the following lines one position to the right: the 3-rd, the 6-th, the 7-th and the 8-th. In the second sample there is no way you can achieve the goal.
1,000
false
false
false
false
false
false
true
false
false
false
8,797
1747D
You are given an array $$$a$$$ of $$$n$$$ integers $$$a_1, a_2, a_3, ldots, a_n$$$. You have to answer $$$q$$$ independent queries, each consisting of two integers $$$l$$$ and $$$r$$$. Consider the subarray $$$a$$$ xa0— the length of the array $$$a$$$ and the number of queries. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(0 le a_i lt 2^{30})$$$ xa0— the elements of the array $$$a$$$. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 le l_i le r_i le n)$$$ xa0— the description of the $$$i$$$-th query. Note In the first query, $$$l = 3, r = 4$$$, subarray = $$$[3, 3]$$$. We can apply operation only to the subarrays of length $$$1$$$, which won't change the array; hence it is impossible to make all elements equal to $$$0$$$. In the second query, $$$l = 4, r = 6$$$, subarray = $$$[3, 1, 2]$$$. We can choose the whole subarray $$$(L = 4, R = 6)$$$ and replace all elements by their XOR $$$(3 oplus 1 oplus 2) = 0$$$, making the subarray $$$[0, 0, 0]$$$. In the fifth query, $$$l = 1, r = 6$$$, subarray = $$$[3, 0, 3, 3, 1, 2]$$$. We can make the operations as follows: 1. Choose $$$L = 4, R = 6$$$, making the subarray $$$[3, 0, 3, 0, 0, 0]$$$. 2. Choose $$$L = 1, R = 5$$$, making the subarray $$$[0, 0, 0, 0, 0, 0]$$$.
1,900
false
false
false
false
true
true
false
true
false
false
1,799
1537D
Alice and Bob are playing a game. They start with a positive integer $$$n$$$ and take alternating turns doing operations on it. Each turn a player can subtract from $$$n$$$ one of its divisors that isn't $$$1$$$ or $$$n$$$. The player who cannot make a move on his/her turn loses. Alice always moves first. Note that they subtract a divisor of the current number in each turn. You are asked to find out who will win the game if both players play optimally. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^9$$$) — the initial number. Output For each test case output "Alice" if Alice will win the game or "Bob" if Bob will win, if both players play optimally. Example Output Bob Alice Alice Bob Note In the first test case, the game ends immediately because Alice cannot make a move. In the second test case, Alice can subtract $$$2$$$ making $$$n = 2$$$, then Bob cannot make a move so Alice wins. In the third test case, Alice can subtract $$$3$$$ so that $$$n = 9$$$. Bob's only move is to subtract $$$3$$$ and make $$$n = 6$$$. Now, Alice can subtract $$$3$$$ again and $$$n = 3$$$. Then Bob cannot make a move, so Alice wins.
1,700
true
false
false
false
false
false
false
false
false
false
2,959
1654G
There are $$$n$$$ locations on a snowy mountain range (numbered from $$$1$$$ to $$$n$$$), connected by $$$n-1$$$ trails in the shape of a tree. Each trail has length $$$1$$$. Some of the locations are base lodges. The height $$$h_i$$$ of each location is equal to the distance to the nearest base lodge (a base lodge has height $$$0$$$). There is a skier at each location, each skier has initial kinetic energy $$$0$$$. Each skier wants to ski along as many trails as possible. Suppose that the skier is skiing along a trail from location $$$i$$$ to $$$j$$$. Skiers are not allowed to ski uphill (i.e., if $$$h_i < h_j$$$). It costs one unit of kinetic energy to ski along flat ground (i.e., if $$$h_i = h_j$$$), and a skier gains one unit of kinetic energy by skiing downhill (i.e., if $$$h_i > h_j$$$). For each location, compute the length of the longest sequence of trails that the skier starting at that location can ski along without their kinetic energy ever becoming negative. Skiers are allowed to visit the same location or trail multiple times. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$l_1, l_2, ldots, l_n$$$ ($$$0 le l_i le 1$$$). If $$$l_i = 1$$$, location $$$i$$$ is a base lodge; if $$$l_i = 0$$$, location $$$i$$$ is not a base lodge. It is guaranteed that there is at least $$$1$$$ base lodge. Each of the next $$$n-1$$$ lines contains two integers $$$u, v$$$ ($$$1 leq u, v leq n$$$, $$$u eq v$$$), meaning that there is a trail that connects the locations $$$u$$$ and $$$v$$$. It is guaranteed that the given trails form a tree. Note In the first test, $$$h = [0, 0, 1, 1, 2, 3]$$$. The skier starting from $$$6$$$ can ski along at most $$$5$$$ trails, in the path $$$6 ightarrow 5 ightarrow 4 ightarrow 3 ightarrow 4 ightarrow 2$$$ (notice that a skier can ski multiple times along the same trail and can visit more than once the same location): at the location $$$6$$$, the kinetic energy is $$$0$$$; at the location $$$5$$$, the kinetic energy increases by $$$1$$$ (because $$$h_5 < h_6$$$), so it becomes $$$1$$$; at the location $$$4$$$, the kinetic energy increases by $$$1$$$ (because $$$h_4 < h_5$$$), so it becomes $$$2$$$; at the location $$$3$$$, the kinetic energy decreases by $$$1$$$ (because $$$h_3 = h_4$$$), so it becomes $$$1$$$; at the location $$$4$$$, the kinetic energy decreases by $$$1$$$ (because $$$h_4 = h_3$$$), so it becomes $$$0$$$; at the location $$$2$$$, the kinetic energy increases by $$$1$$$ (because $$$h_2 < h_4$$$), so it becomes $$$1$$$. There isn't any sequence of trails of length greater than $$$5$$$ such that the kinetic energy is always non-negative. Moreover, the optimal path for the skier starting from $$$1$$$ is $$$1$$$ (no trails); the optimal path for the skier starting from $$$2$$$ is $$$2$$$ (no trails); the optimal path for the skier starting from $$$3$$$ is $$$3 ightarrow 1$$$; the optimal path for the skier starting from $$$4$$$ is $$$4 ightarrow 2$$$; the optimal path for the skier starting from $$$5$$$ is $$$5 ightarrow 4 ightarrow 3 ightarrow 1$$$. In the second test, $$$h = [3, 2, 2, 1, 1, 1, 0, 0, 0]$$$. The skier starting from $$$1$$$ can ski along at most $$$5$$$ trails, in the path $$$1 ightarrow 3 ightarrow 2 ightarrow 5 ightarrow 4 ightarrow 7$$$. at the location $$$1$$$, the kinetic energy is $$$0$$$; at the location $$$3$$$, the kinetic energy increases by $$$1$$$ (because $$$h_3 < h_1$$$), so it becomes $$$1$$$; at the location $$$2$$$, the kinetic energy decreases by $$$1$$$ (because $$$h_2 = h_3$$$), so it becomes $$$0$$$; at the location $$$5$$$, the kinetic energy increases by $$$1$$$ (because $$$h_5 < h_2$$$), so it becomes $$$1$$$; at the location $$$4$$$, the kinetic energy decreases by $$$1$$$ (because $$$h_4 = h_5$$$), so it becomes $$$0$$$; at the location $$$7$$$, the kinetic energy increases by $$$1$$$ (because $$$h_7 < h_4$$$), so it becomes $$$1$$$. There isn't any sequence of trails of length greater than $$$5$$$ such that the kinetic energy is always non-negative. In the third test, for the skier starting from vertex $$$1$$$, the optimal path is $$$1 ightarrow 2 ightarrow 5 ightarrow 4 ightarrow 3 ightarrow 6 ightarrow 11 ightarrow 10 ightarrow 11$$$. Here are pictures of the first, second, and third test, with the base lodges shown in red:
2,900
false
true
false
false
true
false
false
false
false
true
2,356
1336F
In the wilds far beyond lies the Land of Sacredness, which can be viewed as a tree xa0— connected undirected graph consisting of $$$n$$$ nodes and $$$n-1$$$ edges. The nodes are numbered from $$$1$$$ to $$$n$$$. There are $$$m$$$ travelers attracted by its prosperity and beauty. Thereupon, they set off their journey on this land. The $$$i$$$-th traveler will travel along the shortest path from $$$s_i$$$ to $$$t_i$$$. In doing so, they will go through all edges in the shortest path from $$$s_i$$$ to $$$t_i$$$, which is unique in the tree. During their journey, the travelers will acquaint themselves with the others. Some may even become friends. To be specific, the $$$i$$$-th traveler and the $$$j$$$-th traveler will become friends if and only if there are at least $$$k$$$ edges that both the $$$i$$$-th traveler and the $$$j$$$-th traveler will go through. Your task is to find out the number of pairs of travelers $$$(i, j)$$$ satisfying the following conditions: $$$1 leq i < j leq m$$$. the $$$i$$$-th traveler and the $$$j$$$-th traveler will become friends. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n, m le 1.5 cdot 10^5$$$, $$$1le kle n$$$). Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$), denoting there is an edge between $$$u$$$ and $$$v$$$. The $$$i$$$-th line of the next $$$m$$$ lines contains two integers $$$s_i$$$ and $$$t_i$$$ ($$$1le s_i,t_ile n$$$, $$$s_i eq t_i$$$), denoting the starting point and the destination of $$$i$$$-th traveler. It is guaranteed that the given edges form a tree. Output The only line contains a single integer xa0— the number of pairs of travelers satisfying the given conditions. Examples Input 8 4 1 1 7 1 2 2 5 4 6 6 3 6 2 6 8 7 8 3 8 2 6 4 1 Input 10 4 2 3 10 9 3 4 9 4 6 8 2 1 7 2 1 4 5 6 7 7 1 8 7 9 2 10 3 Input 13 8 3 7 6 9 11 5 6 11 3 9 7 2 12 4 3 1 2 5 8 6 13 5 10 3 1 10 4 10 11 8 11 4 9 2 5 3 5 7 3 8 10 Note In the first example there are $$$4$$$ pairs satisfying the given requirements: $$$(1,2)$$$, $$$(1,3)$$$, $$$(1,4)$$$, $$$(3,4)$$$. The $$$1$$$-st traveler and the $$$2$$$-nd traveler both go through the edge $$$6-8$$$. The $$$1$$$-st traveler and the $$$3$$$-rd traveler both go through the edge $$$2-6$$$. The $$$1$$$-st traveler and the $$$4$$$-th traveler both go through the edge $$$1-2$$$ and $$$2-6$$$. The $$$3$$$-rd traveler and the $$$4$$$-th traveler both go through the edge $$$2-6$$$.
3,500
false
false
false
false
true
false
false
false
false
true
4,017
1313C1
This is an easier version of the problem. In this version $$$n le 1000$$$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $$$n$$$ plots along the highway and is preparing to build $$$n$$$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $$$1$$$ to $$$n$$$. Then if the skyscraper on the $$$i$$$-th plot has $$$a_i$$$ floors, it must hold that $$$a_i$$$ is at most $$$m_i$$$ ($$$1 le a_i le m_i$$$). Also there mustn't be integers $$$j$$$ and $$$k$$$ such that $$$j < i < k$$$ and $$$a_j > a_i < a_k$$$. Plots $$$j$$$ and $$$k$$$ are not required to be adjacent to $$$i$$$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 1000$$$)xa0— the number of plots. The second line contains the integers $$$m_1, m_2, ldots, m_n$$$ ($$$1 leq m_i leq 10^9$$$)xa0— the limit on the number of floors for every possible number of floors for a skyscraper on each plot. Output Print $$$n$$$ integers $$$a_i$$$xa0— the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. Note In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $$$[10, 6, 6]$$$ is optimal. Note that the answer of $$$[6, 6, 8]$$$ also satisfies all restrictions, but is not optimal.
1,500
false
true
false
true
true
false
true
false
false
false
4,128
401E
2,500
true
false
false
false
false
false
false
false
false
false
8,218
264A
Problem - 264A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms data structures implementation two pointers *1200 No tag edit access → Contest materials . Each character in _s_ will be either "l" or "r". Output Output _n_ lines — on the _i_-th line you should print the _i_-th stone's number from the left. Examples Input llrlr Output 3 5 4 2 1 Input rrlll Output 1 2 5 4 3 Input lrlrr Output 2 4 5 3 1 Note In the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1.
1,200
false
false
true
false
true
true
false
false
false
false
8,778
946A
Problem - 946A - 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 *800 No tag edit access → Contest materials . What is the maximum possible value of _B_u2009-u2009_C_? Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of elements in _a_. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (u2009-u2009100u2009≤u2009_a__i_u2009≤u2009100) — the elements of sequence _a_. Output Print the maximum possible value of _B_u2009-u2009_C_, where _B_ is the sum of elements of sequence _b_, and _C_ is the sum of elements of sequence _c_. Examples Input 3 1 -2 0 Output 3 Input 6 16 23 16 15 42 8 Output 120 Note In the first example we may choose _b_u2009=u2009{1,u20090}, _c_u2009=u2009{u2009-u20092}. Then _B_u2009=u20091, _C_u2009=u2009u2009-u20092, _B_u2009-u2009_C_u2009=u20093. In the second example we choose _b_u2009=u2009{16,u200923,u200916,u200915,u200942,u20098}, _c_u2009=u2009{} (an empty sequence). Then _B_u2009=u2009120, _C_u2009=u20090, _B_u2009-u2009_C_u2009=u2009120.
800
false
true
false
false
false
false
false
false
false
false
5,932
208B
A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: A deck of _n_ cards is carefully shuffled, then all _n_ cards are put on the table in a line from left to right; Before each move the table has several piles of cards lying in a line (initially there are _n_ piles, each pile has one card). Let's number the piles from left to right, from 1 to _x_. During one move, a player can take the whole pile with the maximum number _x_ (that is the rightmost of remaining) and put it on the top of pile _x_u2009-u20091 (if it exists) or on the top of pile _x_u2009-u20093 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile _x_ goes on top of pile _y_, then the top card of pile _x_ becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. Input The first input line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u200952) — the number of cards in Vasya's deck. The next line contains _n_ space-separated strings _c_1,u2009_c_2,u2009...,u2009_c__n_, where string _c__i_ describes the _i_-th card on the table. Each string _c__i_ consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C". It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat.
1,900
false
false
false
true
false
false
false
false
false
false
9,000
1338D
You have a tree of $$$n$$$ vertices. You are going to convert this tree into $$$n$$$ rubber bands on infinitely large plane. Conversion rule follows: For every pair of vertices $$$a$$$ and $$$b$$$, rubber bands $$$a$$$ and $$$b$$$ should intersect if and only if there is an edge exists between $$$a$$$ and $$$b$$$ in the tree. Shape of rubber bands must be a simple loop. In other words, rubber band is a loop which doesn't self-intersect. Now let's define following things: Rubber band $$$a$$$ includes rubber band $$$b$$$, if and only if rubber band $$$b$$$ is in rubber band $$$a$$$'s area, and they don't intersect each other. Sequence of rubber bands $$$a_{1}, a_{2}, ldots, a_{k}$$$ ($$$k ge 2$$$) are nested, if and only if for all $$$i$$$ ($$$2 le i le k$$$), $$$a_{i-1}$$$ includes $$$a_{i}$$$. This is an example of conversion. Note that rubber bands $$$5$$$ and $$$6$$$ are nested. It can be proved that is it possible to make a conversion and sequence of nested rubber bands under given constraints. What is the maximum length of sequence of nested rubber bands can be obtained from given tree? Find and print it. Input The first line contains integer $$$n$$$ ($$$3 le n le 10^{5}$$$)xa0— the number of vertices in tree. The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 le a_{i} lt b_{i} le n$$$)xa0— it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. It is guaranteed that given graph forms tree of $$$n$$$ vertices. Examples Input 6 1 3 2 3 3 4 4 5 4 6 Note In the first sample, you can obtain a nested sequence of $$$4$$$ rubber bands($$$1$$$, $$$2$$$, $$$5$$$, and $$$6$$$) by the conversion shown below. Of course, there are other conversions exist to make a nested sequence of $$$4$$$ rubber bands. However, you cannot make sequence of $$$5$$$ or more nested rubber bands with given tree. You can see one of the possible conversions for the second sample below.
2,700
true
false
false
true
false
true
false
false
false
false
4,011
643F
There are _n_ bears in the inn and _p_ places to sleep. Bears will party together for some number of nights (and days). Bears love drinking juice. They don't like wine but they can't distinguish it from juice by taste or smell. A bear doesn't sleep unless he drinks wine. A bear must go to sleep a few hours after drinking a wine. He will wake up many days after the party is over. Radewoosh is the owner of the inn. He wants to put some number of barrels in front of bears. One barrel will contain wine and all other ones will contain juice. Radewoosh will challenge bears to find a barrel with wine. Each night, the following happens in this exact order: 1. Each bear must choose a (maybe empty) set of barrels. The same barrel may be chosen by many bears. 2. Each bear drinks a glass from each barrel he chose. 3. All bears who drink wine go to sleep (exactly those bears who chose a barrel with wine). They will wake up many days after the party is over. If there are not enough places to sleep then bears lose immediately. At the end, if it's sure where wine is and there is at least one awake bear then bears win (unless they have lost before because of the number of places to sleep). Radewoosh wants to allow bears to win. He considers _q_ scenarios. In the _i_-th scenario the party will last for _i_ nights. Then, let _R__i_ denote the maximum number of barrels for which bears surely win if they behave optimally. Let's define . Your task is to find , where denotes the exclusive or (also denoted as XOR). Note that the same barrel may be chosen by many bears and all of them will go to sleep at once. Input The only line of the input contains three integers _n_, _p_ and _q_ (1u2009≤u2009_n_u2009≤u2009109, 1u2009≤u2009_p_u2009≤u2009130, 1u2009≤u2009_q_u2009≤u20092u2009000u2009000)xa0— the number of bears, the number of places to sleep and the number of scenarios, respectively. Output Print one integer, equal to . Note In the first sample, there are 5 bears and only 1 place to sleep. We have _R_1u2009=u20096,u2009_R_2u2009=u200911,u2009_R_3u2009=u200916 so the answer is . Let's analyze the optimal strategy for scenario with 2 days. There are _R_2u2009=u200911 barrels and 10 of them contain juice. In the first night, the _i_-th bear chooses a barrel _i_ only. If one of the first 5 barrels contains wine then one bear goes to sleep. Then, bears win because they know where wine is and there is at least one awake bear. But let's say none of the first 5 barrels contains wine. In the second night, the _i_-th bear chooses a barrel 5u2009+u2009_i_. If one of barrels 6u2009–u200910 contains wine then one bear goes to sleep. And again, bears win in such a situation. If nobody went to sleep then wine is in a barrel 11. In the second sample, there is only one bear. He should choose an empty set of barrels in each night. Otherwise, he would maybe get wine and bears would lose (because there must be at least one awake bear). So, for any number of days we have _R__i_u2009=u20091. The answer is .
2,900
true
false
false
true
false
false
false
false
false
false
7,239
1450H1
The only difference between the two versions of the problem is that there are no updates in the easy version. There are $$$n$$$ spools of thread placed on the rim of a circular table. The spools come in two types of thread: the first thread is black and the second thread is white. For any two spools of the same color, you can attach them with a thread of that color in a straight line segment. Define a matching as a way to attach spools together so that each spool is attached to exactly one other spool. Coloring is an assignment of colors (white and black) to the spools. A coloring is called valid if it has at least one matching. That is if the number of black spools and the number of white spools are both even. Given a matching, we can find the number of times some white thread intersects some black thread. We compute the number of pairs of differently colored threads that intersect instead of the number of intersection points, so one intersection point may be counted multiple times if different pairs of threads intersect at the same point. If $$$c$$$ is a valid coloring, let $$$f(c)$$$ denote the minimum number of such intersections out of all possible matchings. The circle above is described by the coloring bwbbbwww. After matching the spools as shown, there is one intersection between differently colored threads. It can be proven that it is the minimum possible, so $$$f( ext{bwbbbwww}) = 1$$$. You are given a string $$$s$$$ representing an unfinished coloring, with black, white, and uncolored spools. A coloring $$$c$$$ is called $$$s$$$-reachable if you can achieve it by assigning colors to the uncolored spools of $$$s$$$ without changing the others. A coloring $$$c$$$ is chosen uniformly at random among all valid, $$$s$$$-reachable colorings. Compute the $$$. You should find it by modulo $$$998244353$$$. We can show that the answer can be written in the form $$$frac{p}{q}$$$ where $$$p$$$ and $$$q$$$ are relatively prime integers and $$$q otequiv 0pmod{998244353}$$$. The answer by modulo $$$998244353$$$ is equal to $$$(pcdot q^{-1})$$$ modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$2le nle 2cdot 10^5$$$, $$$n$$$ is even, $$$m=0$$$) — the number of spools and updates, respectively. There are no updates in the easy version, so it is always $$$0$$$. The second line contains a string $$$s$$$ of length $$$n$$$ — the unfinished coloring of the spools. The $$$i$$$-th character will be 'w', 'b', or '?', describing if the $$$i$$$-th spool is white, black, or uncolored, respectively. It is guaranteed there exists at least one uncolored spool. Note The first test corresponds closely to the image. Coloring '?' as 'w' does not create a valid coloring because the number of black spools is odd. Then the only reachable valid coloring is 'bwbbbwww' and $$$f( ext{bwbbbwww}) = 1$$$, so the expected value is $$$1$$$. It can be shown that the expected value for the second test is $$$frac{9}{16}$$$.
2,900
true
false
false
false
false
false
false
false
false
false
3,430
724B
You are given a table consisting of _n_ rows and _m_ columns. Numbers in each row form a permutation of integers from 1 to _m_. You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to _n_u2009+u20091 actions in total. Operations can be performed in any order. You have to check whether it's possible to obtain the identity permutation 1,u20092,u2009...,u2009_m_ in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200920)xa0— the number of rows and the number of columns in the given table. Each of next _n_ lines contains _m_ integersxa0— elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to _m_. Output If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). Examples Input 4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 Input 3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5 Note In the first sample, one can act in the following way: 1. Swap second and third columns. Now the table is 1xa02xa03xa04 1xa04xa03xa02 2. In the second row, swap the second and the fourth elements. Now the table is 1xa02xa03xa04 1xa02xa03xa04
1,500
true
true
true
false
false
false
true
false
false
false
6,913
424C
Problem - 424C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *1600 No tag edit access → Contest materials Editorial") operation to integers _x_ and _y_. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by "^", in Pascal — by "xor". People in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence _p_, calculate the value of _Q_. Input The first line of the input contains the only integer _n_ (1u2009≤u2009_n_u2009≤u2009106). The next line contains _n_ integers: _p_1,u2009_p_2,u2009...,u2009_p__n_ (0u2009≤u2009_p__i_u2009≤u20092·109). Output The only line of output should contain a single integer — the value of _Q_. Examples Input 3 1 2 3 Output 3
1,600
true
false
false
false
false
false
false
false
false
false
8,141
1304D
Gildong recently learned how to find the in $$$O(nlog{n})$$$ time for a sequence of length $$$n$$$. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$, inclusive, to test his code with your output. The quiz is as follows. Gildong provides a string of length $$$n-1$$$, consisting of characters '<' and '>' only. The $$$i$$$-th (1-indexed) character is the comparison result between the $$$i$$$-th element and the $$$i+1$$$-st element of the sequence. If the $$$i$$$-th character of the string is '<', then the $$$i$$$-th element of the sequence is less than the $$$i+1$$$-st element. If the $$$i$$$-th character of the string is '>', then the $$$i$$$-th element of the sequence is greater than the $$$i+1$$$-st element. He wants you to find two possible sequences (not necessarily distinct) consisting of $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible. Input Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is $$$n-1$$$. It is guaranteed that the sum of all $$$n$$$ in all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print two lines with $$$n$$$ integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between $$$1$$$ and $$$n$$$, inclusive, and should satisfy the comparison results. It can be shown that at least one answer always exists. Example Input 3 3 << 7 >><>>< 5 >>>< Output 1 2 3 1 2 3 5 4 3 7 2 1 6 4 3 1 7 5 2 6 4 3 2 1 5 5 4 2 1 3 Note In the first case, $$$1$$$ $$$2$$$ $$$3$$$ is the only possible answer. In the second case, the shortest length of the LIS is $$$2$$$, and the longest length of the LIS is $$$3$$$. In the example of the maximum LIS sequence, $$$4$$$ '$$$3$$$' $$$1$$$ $$$7$$$ '$$$5$$$' $$$2$$$ '$$$6$$$' can be one of the possible LIS.
1,800
false
true
false
false
false
true
false
false
false
true
4,168
1638B
You are given an array $$$a_1, a_2, dots, a_n$$$. You can perform operations on the array. In each operation you can choose an integer $$$i$$$ ($$$1 le i < n$$$), and swap elements $$$a_i$$$ and $$$a_{i+1}$$$ of the array, if $$$a_i + a_{i+1}$$$ is odd. Determine whether it can be sorted in non-decreasing order using this operation any number of times. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print "Yes" or "No" depending on whether you can or can not sort the given array. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer). Example Input 4 4 1 6 31 14 2 4 2 5 2 9 6 7 10 3 6 6 6 Note In the first test case, we can simply swap $$$31$$$ and $$$14$$$ ($$$31 + 14 = 45$$$ which is odd) and obtain the non-decreasing array $$$[1,6,14,31]$$$. In the second test case, the only way we could sort the array is by swapping $$$4$$$ and $$$2$$$, but this is impossible, since their sum $$$4 + 2 = 6$$$ is even. In the third test case, there is no way to make the array non-decreasing. In the fourth test case, the array is already non-decreasing.
1,100
true
false
false
false
true
false
false
false
true
false
2,425
916A
Jamie loves sleeping. One day, he decides that he needs to wake up at exactly _hh_:u2009_mm_. However, he hates waking up, so he wants to make waking up less painful by setting the alarm at a lucky time. He will then press the snooze button every _x_ minutes until _hh_:u2009_mm_ is reached, and only then he will wake up. He wants to know what is the smallest number of times he needs to press the snooze button. A time is considered lucky if it contains a digit '7'. For example, 13:u200907 and 17:u200927 are lucky, while 00:u200948 and 21:u200934 are not lucky. Note that it is not necessary that the time set for the alarm and the wake-up time are on the same day. It is guaranteed that there is a lucky time Jamie can set so that he can wake at _hh_:u2009_mm_. Formally, find the smallest possible non-negative integer _y_ such that the time representation of the time _x_·_y_ minutes before _hh_:u2009_mm_ contains the digit '7'. Jamie uses 24-hours clock, so after 23:u200959 comes 00:u200900. Input The first line contains a single integer _x_ (1u2009≤u2009_x_u2009≤u200960). The second line contains two two-digit integers, _hh_ and _mm_ (00u2009≤u2009_hh_u2009≤u200923,u200900u2009≤u2009_mm_u2009≤u200959). Output Print the minimum number of times he needs to press the button. Note In the first sample, Jamie needs to wake up at 11:23. So, he can set his alarm at 11:17. He would press the snooze button when the alarm rings at 11:17 and at 11:20. In the second sample, Jamie can set his alarm at exactly at 01:07 which is lucky.
900
true
false
true
false
false
false
true
false
false
false
6,068
1797E
Li Hua wants to solve a problem about $$$varphi$$$xa0— Euler's totient function. Please recall that $$$varphi(x)=sumlimits_{i=1}^xxa0— the number of elements in the array and the number of operations to process, respectively. The second line contains $$$n$$$ integers $$$a_{1},a_{2},cdots ,a_{n}$$$ ($$$1le a_{i}le 5cdot 10^{6}$$$)xa0— the elements of the array. Next $$$m$$$ lines, each line contains three integers $$$t_{i},l_{i},r_{i}$$$ ($$$t_iin{1,2},1le l_ile r_ile n$$$)xa0— the $$$i$$$-th operation. Note Denote $$$varphi^k(x)=begin{cases}x,&k=0varphi(varphi^{k-1}(x)),&k > 0end{cases}$$$. At first, $$$a=[8,1,6,3,7]$$$. To make sure $$$a_1=a_2=a_3=a_4=a_5$$$, we can change $$$a$$$ to $$$a'=[varphi^3(8),varphi^0(1),varphi^2(6),varphi^2(3),varphi^3(7)]=[1,1,1,1,1]$$$, using $$$3+0+2+2+3=10$$$ changes. To make sure $$$a_3=a_4$$$, we can change $$$a$$$ to $$$a'=[varphi^0(8),varphi^0(1),varphi^1(6),varphi^1(3),varphi^0(7)]=[8,1,2,2,7]$$$, using $$$0+0+1+1+0=2$$$ changes. After "1 $$$1$$$ $$$3$$$", $$$a$$$ is changed to $$$a=[varphi^1(8),varphi^1(1),varphi^1(6),varphi^0(3),varphi^0(7)]=[4,1,2,3,7]$$$. To make sure $$$a_3=a_4$$$, we can change $$$a$$$ to $$$a'=[varphi^0(4),varphi^0(1),varphi^0(2),varphi^1(3),varphi^0(7)]=[4,1,2,2,7]$$$, using $$$0+0+0+1+0=1$$$ change.
2,300
true
false
false
false
true
false
true
false
false
false
1,492
1916C
Masha and Olya have an important team olympiad coming up soon. In honor of this, Masha, for warm-up, suggested playing a game with Olya: There is an array $$$a$$$ of size $$$n$$$. Masha goes first, and the players take turns. Each move is described by the following sequence of actions: $$$bullet$$$ If the size of the array is $$$1$$$, the game ends. $$$bullet$$$ The player who is currently playing chooses two different indices $$$i$$$, $$$j$$$ ($$$1 le i, j le a$$$), and performs the following operationxa0— removes $$$a_i$$$ and $$$a_j$$$ from the array and adds to the array a number equal to $$$lfloor frac{a_i + a_j}{2} floor cdot 2$$$. In other words, first divides the sum of the numbers $$$a_i$$$, $$$a_j$$$ by $$$2$$$ rounding down, and then multiplies the result by $$$2$$$. Masha aims to maximize the final number, while Olya aims to minimize it. Masha and Olya decided to play on each non-empty prefix of the initial array $$$a$$$, and asked for your help. For each $$$k = 1, 2, ldots, n$$$, answer the following question. Let only the first $$$k$$$ elements of the array $$$a$$$ be present in the game, with indices $$$1, 2, ldots, k$$$ respectively. What number will remain at the end with optimal play by both players? Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the size of the array. The second line contains $$$n$$$ integers $$$a_1,a_2, ldots,a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the array on which Masha and Olya play. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output $$$n$$$ integers. The $$$k$$$-th of these numbers should be equal to the number that will remain at the end with optimal play by both players, on the array consisting of the first $$$k$$$ elements of the array $$$a$$$. Note In the third test case, for a prefix of length $$$1$$$, the answer is $$$3$$$. For a prefix of length $$$2$$$, Masha has only one move, so the answer is $$$12$$$. For a prefix of length $$$3$$$, Masha has three possible moves: she chooses $$$3$$$ and $$$10$$$, then the final number is $$$22$$$, $$$3$$$ and $$$11$$$, then the final number is $$$24$$$, $$$10$$$ and $$$11$$$, then the final number is $$$22$$$, so Masha will choose $$$3$$$ and $$$11$$$ and get $$$24$$$.
1,200
true
true
true
false
false
true
false
false
false
false
801
1216E1
The only difference between the easy and the hard versions is the maximum value of $$$k$$$. You are given an infinite sequence of form "112123123412345$$$dots$$$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $$$1$$$ to $$$1$$$, the second one — from $$$1$$$ to $$$2$$$, the third one — from $$$1$$$ to $$$3$$$, $$$dots$$$, the $$$i$$$-th block consists of all numbers from $$$1$$$ to $$$i$$$. So the first $$$56$$$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $$$1$$$-st element of the sequence is $$$1$$$, the $$$3$$$-rd element of the sequence is $$$2$$$, the $$$20$$$-th element of the sequence is $$$5$$$, the $$$38$$$-th element is $$$2$$$, the $$$56$$$-th element of the sequence is $$$0$$$. Your task is to answer $$$q$$$ independent queries. In the $$$i$$$-th query you are given one integer $$$k_i$$$. Calculate the digit at the position $$$k_i$$$ of the sequence. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 500$$$) — the number of queries. The $$$i$$$-th of the following $$$q$$$ lines contains one integer $$$k_i$$$ $$$(1 le k_i le 10^9)$$$ — the description of the corresponding query. Output Print $$$q$$$ lines. In the $$$i$$$-th line print one digit $$$x_i$$$ $$$(0 le x_i le 9)$$$ — the answer to the query $$$i$$$, i.e. $$$x_i$$$ should be equal to the element at the position $$$k_i$$$ of the sequence. Examples Input 4 2132 506 999999999 1000000000 Note Answers on queries from the first example are described in the problem statement.
1,900
true
false
false
false
false
false
true
true
false
false
4,593
30C
One warm and sunny day king Copa decided to visit the shooting gallery, located at the Central Park, and try to win the main prize — big pink plush panda. The king is not good at shooting, so he invited you to help him. The shooting gallery is an infinite vertical plane with Cartesian coordinate system on it. The targets are points on this plane. Each target is described by it's coordinates _x__i_, and _y__i_, by the time of it's appearance _t__i_ and by the number _p__i_, which gives the probability that Copa hits this target if he aims at it. A target appears and disappears instantly, so Copa can hit the target only if at the moment _t__i_ his gun sight aimed at (_x__i_,u2009_y__i_). Speed of movement of the gun sight on the plane is equal to 1. Copa knows all the information about the targets beforehand (remember, he is a king!). He wants to play in the optimal way, which maximizes the expected value of the amount of hit targets. He can aim at any target at the moment 0. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — amount of targets in the shooting gallery. Then _n_ lines follow, each describing one target. Each description consists of four numbers _x__i_, _y__i_, _t__i_, _p__i_ (where _x__i_, _y__i_, _t__i_ — integers, u2009-u20091000u2009≤u2009_x__i_,u2009_y__i_u2009≤u20091000,u20090u2009≤u2009_t__i_u2009≤u2009109, real number _p__i_ is given with no more than 6 digits after the decimal point, 0u2009≤u2009_p__i_u2009≤u20091). No two targets may be at the same point. Output Output the maximum expected value of the amount of targets that was shot by the king. Your answer will be accepted if it differs from the correct answer by not more than 10u2009-u20096.
1,800
false
false
false
true
false
false
false
false
false
false
9,846
811A
Problem - 811A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation *800 No tag edit access → Contest materials — number of Vladik and Valera candies respectively. Output Pring a single line "Vladik’’ in case, if Vladik first who can’t give right amount of candy, or "Valera’’ otherwise. Examples Input 1 1 Output Valera Input 7 6 Output Vladik Note Illustration for first test case: Illustration for second test case:
800
false
false
true
false
false
false
true
false
false
false
6,521
558E
Problem - 558E - 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 sortings strings *2300 No tag edit access → Contest materials ") Editorial") , the length of the string and the number of queries respectively. Next line contains a string _S_ itself. It contains only lowercase English letters. Next _q_ lines will contain three integers each _i_,u2009_j_,u2009_k_ (1u2009≤u2009_i_u2009≤u2009_j_u2009≤u2009_n_, ). Output Output one line, the string _S_ after applying the queries. Examples Input 10 5 abacdabcda 7 10 0 5 8 1 1 4 0 3 6 0 7 10 1 Output cbcaaaabdd Input 10 1 agjucbvdfk 1 10 1 Output abcdfgjkuv Note First sample test explanation:
2,300
false
false
false
false
true
false
false
false
true
false
7,599
1320C
Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind. Roma has a choice to buy exactly one of $$$n$$$ different weapons and exactly one of $$$m$$$ different armor sets. Weapon $$$i$$$ has attack modifier $$$a_i$$$ and is worth $$$ca_i$$$ coins, and armor set $$$j$$$ has defense modifier $$$b_j$$$ and is worth $$$cb_j$$$ coins. After choosing his equipment Roma can proceed to defeat some monsters. There are $$$p$$$ monsters he can try to defeat. Monster $$$k$$$ has defense $$$x_k$$$, attack $$$y_k$$$ and possesses $$$z_k$$$ coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster $$$k$$$ can be defeated with a weapon $$$i$$$ and an armor set $$$j$$$ if $$$a_i > x_k$$$ and $$$b_j > y_k$$$. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one. Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins. Help Roma find the maximum profit of the grind. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$p$$$ ($$$1 leq n, m, p leq 2 cdot 10^5$$$)xa0— the number of available weapons, armor sets and monsters respectively. The following $$$n$$$ lines describe available weapons. The $$$i$$$-th of these lines contains two integers $$$a_i$$$ and $$$ca_i$$$ ($$$1 leq a_i leq 10^6$$$, $$$1 leq ca_i leq 10^9$$$)xa0— the attack modifier and the cost of the weapon $$$i$$$. The following $$$m$$$ lines describe available armor sets. The $$$j$$$-th of these lines contains two integers $$$b_j$$$ and $$$cb_j$$$ ($$$1 leq b_j leq 10^6$$$, $$$1 leq cb_j leq 10^9$$$)xa0— the defense modifier and the cost of the armor set $$$j$$$. The following $$$p$$$ lines describe monsters. The $$$k$$$-th of these lines contains three integers $$$x_k, y_k, z_k$$$ ($$$1 leq x_k, y_k leq 10^6$$$, $$$1 leq z_k leq 10^3$$$)xa0— defense, attack and the number of coins of the monster $$$k$$$. Output Print a single integerxa0— the maximum profit of the grind. Example Input 2 3 3 2 3 4 7 2 4 3 2 5 11 1 2 4 2 1 6 3 4 6
2,000
false
false
false
false
true
false
true
false
true
false
4,113
420C
Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the _n_ coders on the meeting said: 'I know for sure that either _x_ or _y_ did it!' The head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders' opinions. That's why the head wants to make such a choice that at least _p_ of _n_ coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects? Note that even if some coder was chosen as a suspect, he can agree with the head's choice if he named the other chosen coder at the meeting. Input The first line contains integers _n_ and _p_ (3u2009≤u2009_n_u2009≤u20093·105;xa00u2009≤u2009_p_u2009≤u2009_n_) — the number of coders in the F company and the minimum number of agreed people. Each of the next _n_ lines contains two integers _x__i_, _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_) — the numbers of coders named by the _i_-th coder. It is guaranteed that _x__i_u2009≠u2009_i_,u2009xa0_y__i_u2009≠u2009_i_,u2009xa0_x__i_u2009≠u2009_y__i_. Output Print a single integer –– the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets (1,u20092) и (2,u20091) are considered identical. Examples Input 8 6 5 6 5 7 5 8 6 2 2 1 7 3 1 3 1 4
1,900
false
false
true
false
true
false
false
false
false
true
8,148
630J
Problem - 630J - Codeforces =============== xa0 ]( "Experimental Educational Round: VolBIT Formulas Blitz") — the prediction on the number of people who will buy the game. Output Output one integer showing how many numbers from 1 to _n_ are divisible by all numbers from 2 to 10. Examples Input 3000 Output 1
1,100
true
false
false
false
false
false
false
false
false
false
7,295
986C
Problem - 986C - 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 dfs and similar dsu graphs *2500 No tag edit access → Contest materials ") . In the second line there are $$$m$$$ integers $$$a_1, a_2, ldots, a_m$$$ ($$$0 le a_{i} < 2^{n}$$$)xa0— the elements of the set. All $$$a_{i}$$$ are distinct. Output Print the number of connected components. Examples Input 2 3 1 2 3 Output 2 Input 5 5 5 19 10 20 12 Output 2 Note Graph from first sample: Graph from second sample:
2,500
false
false
false
false
false
false
false
false
false
true
5,770
911G
Problem - 911G - 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 *2500 No tag edit access → Contest materials — the size of array _a_. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009100) — the elements of array _a_. The third line contains one integer _q_ (1u2009≤u2009_q_u2009≤u2009200000) — the number of queries you have to process. Then _q_ lines follow. _i_-th line contains four integers _l_, _r_, _x_ and _y_ denoting _i_-th query (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_, 1u2009≤u2009_x_,u2009_y_u2009≤u2009100). Output Print _n_ integers — elements of array _a_ after all changes are made. Example Input 5 1 2 3 4 5 3 3 5 3 5 1 5 5 1 1 5 1 5 Output 5 2 5 4 5
2,500
false
false
false
false
true
false
false
false
false
false
6,097
1926D
Vladislav has $$$n$$$ non-negative integers, and he wants to divide all of them into several groups so that in any group, any pair of numbers does not have matching bit values among bits from $$$1$$$-st to $$$31$$$-st bit (i.e., considering the $$$31$$$ least significant bits of the binary representation). For an integer $$$k$$$, let $$$k_2(i)$$$ denote the $$$i$$$-th bit in its binary representation (from right to left, indexing from 1). For example, if $$$k=43$$$, since $$$43=101011_2$$$, then $$$43_2(1)=1$$$, $$$43_2(2)=1$$$, $$$43_2(3)=0$$$, $$$43_2(4)=1$$$, $$$43_2(5)=0$$$, $$$43_2(6)=1$$$, $$$43_2(7)=0$$$, $$$43_2(8)=0, dots, 43_2(31)=0$$$. Formally, for any two numbers $$$x$$$ and $$$y$$$ in the same group, the condition $$$x_2(i) eq y_2(i)$$$ must hold for all $$$1 leq i < 32$$$. What is the minimum number of groups Vlad needs to achieve his goal? Each number must fall into exactly one group. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the total number of integers. The second line of each test case contains $$$n$$$ given integers $$$a_1, ldots, a_n$$$ ($$$0 leq a_j < 2^{31}$$$). The sum of $$$n$$$ over all test cases in a test does not exceed $$$2 cdot 10^5$$$. Note In the first test case, any two numbers have the same last $$$31$$$ bits, so we need to place each number in its own group. In the second test case, $$$a_1=0000000000000000000000000000000_2$$$, $$$a_2=1111111111111111111111111111111_2$$$ so they can be placed in the same group because $$$a_1(i) e a_2(i)$$$ for each $$$i$$$ between $$$1$$$ and $$$31$$$, inclusive.
1,300
false
true
false
false
false
false
false
false
false
false
734
859B
Problem - 859B - Codeforces =============== xa0 ]( "Announcing MemSQL Start , the number of city blocks that must be enclosed by the route. Output Print the minimum perimeter that can be achieved. Examples Input 4 Output 8 Input 11 Output 14 Input 22 Output 20 Note Here are some possible shapes for the examples:
1,000
true
false
false
false
false
false
true
false
false
false
6,307
906C
Arseny likes to organize parties and invite people to it. However, not only friends come to his parties, but friends of his friends, friends of friends of his friends and so on. That's why some of Arseny's guests can be unknown to him. He decided to fix this issue using the following procedure. At each step he selects one of his guests _A_, who pairwise introduces all of his friends to each other. After this action any two friends of _A_ become friends. This process is run until all pairs of guests are friends. Arseny doesn't want to spend much time doing it, so he wants to finish this process using the minimum number of steps. Help Arseny to do it. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u200922; )xa0— the number of guests at the party (including Arseny) and the number of pairs of people which are friends. Each of the next _m_ lines contains two integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_; _u_u2009≠u2009_v_), which means that people with numbers _u_ and _v_ are friends initially. It's guaranteed that each pair of friends is described not more than once and the graph of friendship is connected. Output In the first line print the minimum number of steps required to make all pairs of guests friends. In the second line print the ids of guests, who are selected at each step. If there are multiple solutions, you can output any of them. Examples Input 5 6 1 2 1 3 2 3 2 5 3 4 4 5 Note In the first test case there is no guest who is friend of all other guests, so at least two steps are required to perform the task. After second guest pairwise introduces all his friends, only pairs of guests (4,u20091) and (4,u20092) are not friends. Guest 3 or 5 can introduce them. In the second test case guest number 1 is a friend of all guests, so he can pairwise introduce all guests in one step.
2,400
false
false
false
true
false
false
true
false
false
true
6,125
1322E
Berlandxa0— is a huge country with diverse geography. One of the most famous natural attractions of Berland is the "Median mountain range". This mountain range is $$$n$$$ mountain peaks, located on one straight line and numbered in order of $$$1$$$ to $$$n$$$. The height of the $$$i$$$-th mountain top is $$$a_i$$$. "Median mountain range" is famous for the so called alignment of mountain peaks happening to it every day. At the moment of alignment simultaneously for each mountain from $$$2$$$ to $$$n - 1$$$ its height becomes equal to the median height among it and two neighboring mountains. Formally, if before the alignment the heights were equal $$$b_i$$$, then after the alignment new heights $$$a_i$$$ are as follows: $$$a_1 = b_1$$$, $$$a_n = b_n$$$ and for all $$$i$$$ from $$$2$$$ to $$$n - 1$$$ $$$a_i = exttt{median}(b_{i-1}, b_i, b_{i+1})$$$. The median of three integers is the second largest number among them. For example, $$$ exttt{median}(5,1,2) = 2$$$, and $$$ exttt{median}(4,2,4) = 4$$$. Recently, Berland scientists have proved that whatever are the current heights of the mountains, the alignment process will stabilize sooner or later, i.e. at some point the altitude of the mountains won't changing after the alignment any more. The government of Berland wants to understand how soon it will happen, i.e. to find the value of $$$c$$$xa0— how many alignments will occur, which will change the height of at least one mountain. Also, the government of Berland needs to determine the heights of the mountains after $$$c$$$ alignments, that is, find out what heights of the mountains stay forever. Help scientists solve this important problem! Input The first line contains integers $$$n$$$ ($$$1 le n le 500,000$$$)xa0— the number of mountains. The second line contains integers $$$a_1, a_2, a_3, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— current heights of the mountains. Output In the first line print $$$c$$$xa0— the number of alignments, which change the height of at least one mountain. In the second line print $$$n$$$ integersxa0— the final heights of the mountains after $$$c$$$ alignments. Note In the first example, the heights of the mountains at index $$$1$$$ and $$$5$$$ never change. Since the median of $$$1$$$, $$$2$$$, $$$1$$$ is $$$1$$$, the second and the fourth mountains will have height 1 after the first alignment, and since the median of $$$2$$$, $$$1$$$, $$$2$$$ is $$$2$$$, the third mountain will have height 2 after the first alignment. This way, after one alignment the heights are $$$1$$$, $$$1$$$, $$$2$$$, $$$1$$$, $$$1$$$. After the second alignment the heights change into $$$1$$$, $$$1$$$, $$$1$$$, $$$1$$$, $$$1$$$ and never change from now on, so there are only $$$2$$$ alignments changing the mountain heights. In the third examples the alignment doesn't change any mountain height, so the number of alignments changing any height is $$$0$$$.
3,300
false
false
false
false
true
false
false
false
false
false
4,103
1728G
Consider a segment $$$[0, d]$$$ of the coordinate line. There are $$$n$$$ lanterns and $$$m$$$ points of interest in this segment. For each lantern, you can choose its power — an integer between $$$0$$$ and $$$d$$$ (inclusive). A lantern with coordinate $$$x$$$ illuminates the point of interest with coordinate $$$y$$$ if $$$x - y$$$ is less than or equal to the power of the lantern. A way to choose the power values for all lanterns is considered valid if every point of interest is illuminated by at least one lantern. You have to process $$$q$$$ queries. Each query is represented by one integer $$$f_i$$$. To answer the $$$i$$$-th query, you have to: add a lantern on coordinate $$$f_i$$$; calculate the number of valid ways to assign power values to all lanterns, and print it modulo $$$998244353$$$; remove the lantern you just added. Input The first line contains three integers $$$d$$$, $$$n$$$ and $$$m$$$ ($$$4 le d le 3 cdot 10^5$$$; $$$1 le n le 2 cdot 10^5$$$; $$$1 le m le 16$$$) — the size of the segment, the number of lanterns and the number of points of interest, respectively. The second line contains $$$n$$$ integers $$$l_1, l_2, dots, l_n$$$ ($$$1 le l_i le d - 1$$$), where $$$l_i$$$ is the coordinate of the $$$i$$$-th lantern. The third line contains $$$m$$$ integers $$$p_1, p_2, dots, p_m$$$ ($$$1 le p_i le d - 1$$$), where $$$p_i$$$ is the coordinate of the $$$i$$$-th point of interest. The fourth line contains one integer $$$q$$$ ($$$1 le q le 5 cdot 10^5$$$) — the number of queries. The fifth line contains $$$q$$$ integers $$$f_1, f_2, dots, f_q$$$ ($$$1 le f_i le d - 1$$$), where $$$f_i$$$ is the integer representing the $$$i$$$-th query. Additional constraint on the input: during the processing of each query, no coordinate contains more than one object (i.u2009e. there cannot be two or more lanterns with the same coordinate, two or more points of interest with the same coordinate, or a lantern and a point of interest with the same coordinate).
2,700
true
false
false
true
false
false
true
true
false
false
1,919
574A
Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are _n_ candidates, including Limak. We know how many citizens are going to vote for each candidate. Now _i_-th candidate would get _a__i_ votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate. Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn't have many candies and wonders - how many citizens does he have to bribe? Input The first line contains single integer _n_ (2u2009≤u2009_n_u2009≤u2009100) - number of candidates. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091000) - number of votes for each candidate. Limak is candidate number 1. Note that after bribing number of votes for some candidate might be zero or might be greater than 1000. Output Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate. Note In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9,u20091,u20097,u20092,u20098 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9,u20090,u20098,u20092,u20098. In the second sample Limak will steal 2 votes from each candidate. Situation will be 7,u20096,u20096,u20096. In the third sample Limak is a winner without bribing any citizen.
1,200
false
true
true
false
false
false
false
false
false
false
7,554
34D
There are _n_ cities in Berland. Each city has its index — an integer number from 1 to _n_. The capital has index _r_1. All the roads in Berland are two-way. The road system is such that there is exactly one path from the capital to each city, i.e. the road map looks like a tree. In Berland's chronicles the road map is kept in the following way: for each city _i_, different from the capital, there is kept number _p__i_ — index of the last city on the way from the capital to _i_. Once the king of Berland Berl XXXIV decided to move the capital from city _r_1 to city _r_2. Naturally, after this the old representation of the road map in Berland's chronicles became incorrect. Please, help the king find out a new representation of the road map in the way described above. Input The first line contains three space-separated integers _n_, _r_1, _r_2 (2u2009≤u2009_n_u2009≤u20095·104,u20091u2009≤u2009_r_1u2009≠u2009_r_2u2009≤u2009_n_) — amount of cities in Berland, index of the old capital and index of the new one, correspondingly. The following line contains _n_u2009-u20091 space-separated integers — the old representation of the road map. For each city, apart from _r_1, there is given integer _p__i_ — index of the last city on the way from the capital to city _i_. All the cities are described in order of increasing indexes. Output Output _n_u2009-u20091 numbers — new representation of the road map in the same format.
1,600
false
false
false
false
false
false
false
false
false
true
9,825
177A1
Problem - 177A1 - Codeforces =============== xa0 separated by single spaces — the elements of the given matrix. The input limitations for getting 30 points are: 1u2009≤u2009_n_u2009≤u20095 The input limitations for getting 100 points are: 1u2009≤u2009_n_u2009≤u2009101 Output Print a single integer — the sum of good matrix elements. Examples Input 3 1 2 3 4 5 6 7 8 9 Output 45 Input 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 17 Note In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
800
false
false
true
false
false
false
false
false
false
false
9,154
1141A
Problem - 1141A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation math *1000 No tag edit access → Contest materials ") . Input The only line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le m le 5cdot10^8$$$). Output Print the number of moves to transform $$$n$$$ to $$$m$$$, or -1 if there is no solution. Examples Input 120 51840 Output 7 Input 42 42 Output 0 Input 48 72 Output -1 Note In the first example, the possible sequence of moves is: $$$120 ightarrow 240 ightarrow 720 ightarrow 1440 ightarrow 4320 ightarrow 12960 ightarrow 25920 ightarrow 51840.$$$ The are $$$7$$$ steps in total. In the second example, no moves are needed. Thus, the answer is $$$0$$$. In the third example, it is impossible to transform $$$48$$$ to $$$72$$$.
1,000
true
false
true
false
false
false
false
false
false
false
5,021
817B
Problem - 817B - Codeforces =============== xa0 (_i_u2009<u2009_j_u2009<u2009_k_), such that _a__i_·_a__j_·_a__k_ is minimum possible, are there in the array? Help him with it! Input The first line of input contains a positive integer number _n_xa0(3u2009≤u2009_n_u2009≤u2009105) — the number of elements in array _a_. The second line contains _n_ positive integer numbers _a__i_xa0(1u2009≤u2009_a__i_u2009≤u2009109) — the elements of a given array. Output Print one number — the quantity of triples (_i_,u2009xa0_j_,u2009xa0_k_) such that _i_,u2009xa0_j_ and _k_ are pairwise distinct and _a__i_·_a__j_·_a__k_ is minimum possible. Examples Input 4 1 1 1 1 Output 4 Input 5 1 3 2 3 4 Output 2 Input 6 1 3 3 1 3 2 Output 1 Note In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4. In the second example a triple of numbers (1,u20092,u20093) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2. In the third example a triple of numbers (1,u20091,u20092) is chosen, and there's only one way to choose indices.
1,500
true
false
true
false
false
false
false
false
true
false
6,492
1676H2
The only difference between the two versions is that in this version $$$n leq 2 cdot 10^5$$$ and the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. A terminal is a row of $$$n$$$ equal segments numbered $$$1$$$ to $$$n$$$ in order. There are two terminals, one above the other. You are given an array $$$a$$$ of length $$$n$$$. For all $$$i = 1, 2, dots, n$$$, there should be a straight wire from some point on segment $$$i$$$ of the top terminal to some point on segment $$$a_i$$$ of the bottom terminal. You can't select the endpoints of a segment. For example, the following pictures show two possible wirings if $$$n=7$$$ and $$$a=[4,1,4,6,7,7,5]$$$. A crossing occurs when two wires share a point in common. In the picture above, crossings are circled in red. What is the maximum number of crossings there can be if you place the wires optimally? Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq n$$$)xa0— the elements of the array. The sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the maximum number of crossings there can be if you place the wires optimally. Example Input 4 7 4 1 4 6 7 7 5 2 2 1 1 1 3 2 2 2 Note The first test case is shown in the second picture in the statement. In the second test case, the only wiring possible has the two wires cross, so the answer is $$$1$$$. In the third test case, the only wiring possible has one wire, so the answer is $$$0$$$.
1,500
false
false
false
false
true
false
false
false
true
false
2,207
741B
Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight _w__i_ and some beauty _b__i_. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses _x_ and _y_ are in the same friendship group if and only if there is a sequence of Hoses _a_1,u2009_a_2,u2009...,u2009_a__k_ such that _a__i_ and _a__i_u2009+u20091 are friends for each 1u2009≤u2009_i_u2009<u2009_k_, and _a_1u2009=u2009_x_ and _a__k_u2009=u2009_y_. Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most _w_ weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than _w_ and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed _w_. Input The first line contains integers _n_, _m_ and _w_ (1u2009u2009≤u2009u2009_n_u2009u2009≤u2009u20091000, , 1u2009≤u2009_w_u2009≤u20091000)xa0— the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains _n_ integers _w_1,u2009_w_2,u2009...,u2009_w__n_ (1u2009≤u2009_w__i_u2009≤u20091000)xa0— the weights of the Hoses. The third line contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b__i_u2009≤u2009106)xa0— the beauties of the Hoses. The next _m_ lines contain pairs of friends, the _i_-th of them contains two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_, _x__i_u2009≠u2009_y__i_), meaning that Hoses _x__i_ and _y__i_ are friends. Note that friendship is bidirectional. All pairs (_x__i_,u2009_y__i_) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed _w_. Examples Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Note In the first sample there are two friendship groups: Hoses {1,u20092} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1,u20092,u20093} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12u2009>u200911, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
1,600
false
false
false
true
false
false
false
false
false
false
6,835
1753B
You are given an integer $$$x$$$ and an array of integers $$$a_1, a_2, ldots, a_n$$$. You have to determine if the number $$$a_1! + a_2! + ldots + a_n!$$$ is divisible by $$$x!$$$. Here $$$k!$$$ is a factorial of $$$k$$$xa0— the product of all positive integers less than or equal to $$$k$$$. For example, $$$3! = 1 cdot 2 cdot 3 = 6$$$, and $$$5! = 1 cdot 2 cdot 3 cdot 4 cdot 5 = 120$$$. Input The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 500,000$$$, $$$1 le x le 500,000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le x$$$)xa0— elements of given array. Output In the only line print "Yes" (without quotes) if $$$a_1! + a_2! + ldots + a_n!$$$ is divisible by $$$x!$$$, and "No" (without quotes) otherwise. Examples Input 10 5 4 3 2 1 4 3 2 4 3 4 Input 2 500000 499999 499999 Note In the first example $$$3! + 2! + 2! + 2! + 3! + 3! = 6 + 2 + 2 + 2 + 6 + 6 = 24$$$. Number $$$24$$$ is divisible by $$$4! = 24$$$. In the second example $$$3! + 2! + 2! + 2! + 2! + 2! + 1! + 1! = 18$$$, is divisible by $$$3! = 6$$$. In the third example $$$7! + 7! + 7! + 7! + 7! + 7! + 7! = 7 cdot 7!$$$. It is easy to prove that this number is not divisible by $$$8!$$$.
1,600
true
false
false
false
false
false
false
false
false
false
1,775
1676H1
The only difference between the two versions is that in this version $$$n leq 1000$$$ and the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. A terminal is a row of $$$n$$$ equal segments numbered $$$1$$$ to $$$n$$$ in order. There are two terminals, one above the other. You are given an array $$$a$$$ of length $$$n$$$. For all $$$i = 1, 2, dots, n$$$, there should be a straight wire from some point on segment $$$i$$$ of the top terminal to some point on segment $$$a_i$$$ of the bottom terminal. You can't select the endpoints of a segment. For example, the following pictures show two possible wirings if $$$n=7$$$ and $$$a=[4,1,4,6,7,7,5]$$$. A crossing occurs when two wires share a point in common. In the picture above, crossings are circled in red. What is the maximum number of crossings there can be if you place the wires optimally? Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 1000$$$)xa0— the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq n$$$)xa0— the elements of the array. The sum of $$$n$$$ across all test cases does not exceed $$$1000$$$. Output For each test case, output a single integerxa0— the maximum number of crossings there can be if you place the wires optimally. Example Input 4 7 4 1 4 6 7 7 5 2 2 1 1 1 3 2 2 2 Note The first test case is shown in the second picture in the statement. In the second test case, the only wiring possible has the two wires cross, so the answer is $$$1$$$. In the third test case, the only wiring possible has one wire, so the answer is $$$0$$$.
1,400
false
false
false
false
false
false
true
false
false
false
2,208
1228A
Problem - 1228A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation *800 No tag edit access → Contest materials ") . Output If an answer exists, print any of them. Otherwise, print $$$-1$$$. Examples Input 121 130 Output 123 Input 98766 100000 Output -1 Note In the first example, $$$123$$$ is one of the possible answers. However, $$$121$$$ can't be the answer, because there are multiple $$$1$$$s on different digits. In the second example, there is no valid answer.
800
false
false
true
false
false
false
true
false
false
false
4,535
888C
Problem - 888C - Codeforces =============== xa0 . Output Print one number — the minimum value of _k_ such that there exists at least one _k_-dominant character. Examples Input abacaba Output 2 Input zzzzz Output 1 Input abcde Output 3
1,400
false
false
true
false
false
false
false
true
false
false
6,194
1774A
Problem - 1774A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms math *800 No tag edit access → Contest materials ") Editorial") xa0– the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 100$$$) — the length of $$$a$$$. The second line of each test case contains a string $$$a$$$ of length $$$n$$$, consisting of only $$$0$$$ and $$$1$$$. Output For each test case, output a string of length $$$n - 1$$$ consisting of $$$-$$$ and $$$+$$$ on a separate line. If there is more than one assignment of signs that produces the smallest possible absolute value, any of them is accepted. Example Input 3 2 11 5 01101 5 10001 Output - +-++ +++- Note In the first test case, we can get the expression $$$1 - 1 = 0$$$, with absolute value $$$0$$$. In the second test case, we can get the expression $$$0 + 1 - 1 + 0 + 1 = 1$$$, with absolute value $$$1$$$. In the third test case, we can get the expression $$$1 + 0 + 0 + 0 - 1 = 0$$$, with absolute value $$$0$$$.
800
true
false
false
false
false
true
false
false
false
false
1,644
766B
Problem - 766B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms geometry greedy math number theory sortings *1000 No tag edit access → Contest materials ") xa0— the number of line segments Mahmoud has. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the lengths of line segments Mahmoud has. Output In the only line print "YES" if he can choose exactly three line segments and form a non-degenerate triangle with them, and "NO" otherwise. Examples Input 5 1 5 3 2 4 Output YES Input 3 4 1 2 Output NO Note For the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle.
1,000
true
true
false
false
false
true
false
false
true
false
6,722
1427B
You like playing chess tournaments online. In your last tournament you played $$$n$$$ games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get $$$0$$$ points. When you win you get $$$1$$$ or $$$2$$$ points: if you have won also the previous game you get $$$2$$$ points, otherwise you get $$$1$$$ point. If you win the very first game of the tournament you get $$$1$$$ point (since there is not a "previous game"). The outcomes of the $$$n$$$ games are represented by a string $$$s$$$ of length $$$n$$$: the $$$i$$$-th character of $$$s$$$ is W if you have won the $$$i$$$-th game, while it is L if you have lost the $$$i$$$-th game. After the tournament, you notice a bug on the website that allows you to change the outcome of at most $$$k$$$ of your games (meaning that at most $$$k$$$ times you can change some symbol L to W, or W to L). Since your only goal is to improve your chess rating, you decide to cheat and use the bug. Compute the maximum score you can get by cheating in the optimal way. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1le t le 20,000$$$) — the number of test cases. The description of the test cases follows. The first line of each testcase contains two integers $$$n, k$$$ ($$$1le nle 100,000$$$, $$$0le kle n$$$) – the number of games played and the number of outcomes that you can change. The second line contains a string $$$s$$$ of length $$$n$$$ containing only the characters W and L. If you have won the $$$i$$$-th game then $$$s_i=,$$$W, if you have lost the $$$i$$$-th game then $$$s_i=,$$$L. It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$200,000$$$. Output For each testcase, print a single integer – the maximum score you can get by cheating in the optimal way. Example Input 8 5 2 WLWLL 6 5 LLLWWL 7 1 LWLWLWL 15 5 WWWLLLWWWLLLWWW 40 7 LLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL 1 0 L 1 1 L 6 1 WLLWLW Note Explanation of the first testcase. Before changing any outcome, the score is $$$2$$$. Indeed, you won the first game, so you got $$$1$$$ point, and you won also the third, so you got another $$$1$$$ point (and not $$$2$$$ because you lost the second game). An optimal way to cheat is to change the outcomes of the second and fourth game. Doing so, you end up winning the first four games (the string of the outcomes becomes WWWWL). Hence, the new score is $$$7=1+2+2+2$$$: $$$1$$$ point for the first game and $$$2$$$ points for the second, third and fourth game. Explanation of the second testcase. Before changing any outcome, the score is $$$3$$$. Indeed, you won the fourth game, so you got $$$1$$$ point, and you won also the fifth game, so you got $$$2$$$ more points (since you won also the previous game). An optimal way to cheat is to change the outcomes of the first, second, third and sixth game. Doing so, you end up winning all games (the string of the outcomes becomes WWWWWW). Hence, the new score is $$$11 = 1+2+2+2+2+2$$$: $$$1$$$ point for the first game and $$$2$$$ points for all the other games.
1,400
false
true
true
false
false
false
false
false
true
false
3,532
830A
There are _n_ people and _k_ keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to determine the minimum time needed for all _n_ people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it. Input The first line contains three integers _n_, _k_ and _p_ (1u2009≤u2009_n_u2009≤u20091u2009000, _n_u2009≤u2009_k_u2009≤u20092u2009000, 1u2009≤u2009_p_u2009≤u2009109) — the number of people, the number of keys and the office location. The second line contains _n_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — positions in which people are located initially. The positions are given in arbitrary order. The third line contains _k_ distinct integers _b_1,u2009_b_2,u2009...,u2009_b__k_ (1u2009≤u2009_b__j_u2009≤u2009109) — positions of the keys. The positions are given in arbitrary order. Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point. Output Print the minimum time (in seconds) needed for all _n_ to reach the office with keys. Examples Input 2 4 50 20 100 60 10 40 80 Note In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
1,800
false
true
false
true
false
false
true
true
true
false
6,447
1985D
Given a $$$n$$$ by $$$m$$$ grid consisting of '.' and '#' characters, there exists a whole manhattan circle on the grid. The top left corner of the grid has coordinates $$$(1,1)$$$, and the bottom right corner has coordinates $$$(n, m)$$$. Point ($$$a, b$$$) belongs to the manhattan circle centered at ($$$h, k$$$) if $$$h - a + k - b < r$$$, where $$$r$$$ is a positive constant. On the grid, the set of points that are part of the manhattan circle is marked as '#'. Find the coordinates of the center of the circle. Input The first line contains $$$t$$$ ($$$1 leq t leq 1000$$$) xa0— the number of test cases. The first line of each test case contains $$$n$$$ and $$$m$$$ ($$$1 leq n cdot m leq 2 cdot 10^5$$$)xa0— the height and width of the grid, respectively. The next $$$n$$$ lines contains $$$m$$$ characters '.' or '#'. If the character is '#', then the point is part of the manhattan circle. It is guaranteed the sum of $$$n cdot m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$, and there is a whole manhattan circle on the grid. Output For each test case, output the two integers, the coordinates of the center of the circle. Example Input 6 5 5 ..... ..... ..#.. ..... ..... 5 5 ..#.. .###. ##### .###. ..#.. 5 6 ...... ...... .#.... ###... .#.... 1 1 # 5 6 ...#.. ..###. .##### ..###. ...#.. 2 10 .......... ...#...... Output 3 3 3 3 4 2 1 1 3 4 2 4
900
true
false
true
false
false
false
false
false
false
false
371
1015D
There are $$$n$$$ houses in a row. They are numbered from $$$1$$$ to $$$n$$$ in order from left to right. Initially you are in the house $$$1$$$. You have to perform $$$k$$$ moves to other house. In one move you go from your current house to some other house. You can't stay where you are (i.e., in each move the new house differs from the current house). If you go from the house $$$x$$$ to the house $$$y$$$, the total distance you walked increases by $$$x-y$$$ units of distance, where $$$a$$$ is the absolute value of $$$a$$$. It is possible to visit the same house multiple times (but you can't visit the same house in sequence). Your goal is to walk exactly $$$s$$$ units of distance in total. If it is impossible, print "NO". Otherwise print "YES" and any of the ways to do that. Remember that you should do exactly $$$k$$$ moves. Input The first line of the input contains three integers $$$n$$$, $$$k$$$, $$$s$$$ ($$$2 le n le 10^9$$$, $$$1 le k le 2 cdot 10^5$$$, $$$1 le s le 10^{18}$$$) — the number of houses, the number of moves and the total distance you want to walk. Output If you cannot perform $$$k$$$ moves with total walking distance equal to $$$s$$$, print "NO". Otherwise print "YES" on the first line and then print exactly $$$k$$$ integers $$$h_i$$$ ($$$1 le h_i le n$$$) on the second line, where $$$h_i$$$ is the house you visit on the $$$i$$$-th move. For each $$$j$$$ from $$$1$$$ to $$$k-1$$$ the following condition should be satisfied: $$$h_j e h_{j + 1}$$$. Also $$$h_1 e 1$$$ should be satisfied. Examples Output YES 10 1 10 1 2 1 2 1 6 Output YES 10 1 10 1 10 1 10 1 10
1,600
false
true
false
false
false
true
false
false
false
false
5,616
1788B
The sum of digits of a non-negative integer $$$a$$$ is the result of summing up its digits together when written in the decimal system. For example, the sum of digits of $$$123$$$ is $$$6$$$ and the sum of digits of $$$10$$$ is $$$1$$$. In a formal way, the sum of digits of $$$displaystyle a=sum_{i=0}^{infty} a_i cdot 10^i$$$, where $$$0 leq a_i leq 9$$$, is defined as $$$displaystylesum_{i=0}^{infty}{a_i}$$$. Given an integer $$$n$$$, find two non-negative integers $$$x$$$ and $$$y$$$ which satisfy the following conditions. $$$x+y=n$$$, and the sum of digits of $$$x$$$ and the sum of digits of $$$y$$$ differ by at most $$$1$$$. It can be shown that such $$$x$$$ and $$$y$$$ always exist. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10,000$$$). Each test case consists of a single integer $$$n$$$ ($$$1 leq n leq 10^9$$$) Output For each test case, print two integers $$$x$$$ and $$$y$$$. If there are multiple answers, print any. Example Output 1 0 67 94 60 7 1138 68 14 5 Note In the second test case, the sum of digits of $$$67$$$ and the sum of digits of $$$94$$$ are both $$$13$$$. In the third test case, the sum of digits of $$$60$$$ is $$$6$$$, and the sum of digits of $$$7$$$ is $$$7$$$.
1,100
true
true
true
false
false
true
false
false
false
false
1,553
757E
Problem - 757E - 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 number theory *2500 No tag edit access → Contest materials Announcement") Editorial") , which denotes the number of ways of factoring _n_ into two factors _p_ and _q_ such that _gcd_(_p_,u2009_q_)u2009=u20091. In other words, _f_0(_n_) is the number of ordered pairs of positive integers (_p_,u2009_q_) such that _p_·_q_u2009=u2009_n_ and _gcd_(_p_,u2009_q_)u2009=u20091. But Bash felt that it was too easy to calculate this function. So he defined a series of functions, where _f__r_u2009+u20091 is defined as: Where (_u_,u2009_v_) is any ordered pair of positive integers, they need not to be co-prime. Now Bash wants to know the value of _f__r_(_n_) for different _r_ and _n_. Since the value could be huge, he would like to know the value modulo 109u2009+u20097. Help him! Input The first line contains an integer _q_ (1u2009≤u2009_q_u2009≤u2009106)xa0— the number of values Bash wants to know. Each of the next _q_ lines contain two integers _r_ and _n_ (0u2009≤u2009_r_u2009≤u2009106, 1u2009≤u2009_n_u2009≤u2009106), which denote Bash wants to know the value _f__r_(_n_). Output Print _q_ integers. For each pair of _r_ and _n_ given, print _f__r_(_n_) modulo 109u2009+u20097 on a separate line. Example Input 5 0 30 1 25 3 65 2 5 4 48 Output 8 5 25 4 630
2,500
false
false
false
true
false
false
true
false
false
false
6,760
128E
Anna's got a birthday today. She invited many guests and cooked a huge (nearly infinite) birthday cake decorated by _n_ banana circles of different sizes. Maria's birthday is about to start in 7 minutes too, and while Anna is older, she decided to play the boss a little. She told Maria to cut the cake by _k_ straight-line cuts (the cutting lines can intersect) to divide banana circles into banana pieces. Anna has many guests and she wants everyone to get at least one banana piece. That's why she told Maria to make the total number of banana pieces maximum. It's not a problem if some banana pieces end up on the same cake piece — the key is to make the maximum number of banana pieces. Determine what result Maria will achieve. Input The first line contains two integers _n_ and _k_ — the number of banana circles and the number of cuts Maria should perform (1u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_k_u2009≤u2009105). Next _n_ lines contain the positions and sizes of the banana circles (all banana circles are round). On the cake the Cartesian coordinate system is defined. Each line contains three integers _x_, _y_ and _r_ — the coordinates of the center of the corresponding banana piece and its radius (u2009-u20091000u2009≤u2009_x_,u2009_y_u2009≤u20091000, 1u2009≤u2009_r_u2009≤u20091000). It is guaranteed that the banana circles do not intersect, do not touch each other and do not overlap with each other. Pretest 10 is big test with _n_u2009=u2009_k_u2009=u20091000. Output Print the only integer — the largest number of banana pieces that Maria can get after she performs the _k_ straight-line cuts. Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
2,900
true
false
false
false
false
false
false
false
false
false
9,366
1451E2
The only difference between the easy and hard versions is the constraints on the number of queries. This is an interactive problem. Ridbit has a hidden array $$$a$$$ of $$$n$$$ integers which he wants Ashish to guess. Note that $$$n$$$ is a power of two. Ashish is allowed to ask three different types of queries. They are of the form AND $$$i$$$ $$$j$$$: ask for the $$$ OR $$$i$$$ $$$j$$$: ask for the $$$ XOR $$$i$$$ $$$j$$$: ask for the $$$ Can you help Ashish guess the elements of the array? In this version, each element takes a value in the range $$$[0, n-1]$$$ (inclusive) and Ashish can ask no more than $$$n+1$$$ queries. Interaction To ask a query print a single line containing one of the following (without quotes) "AND i j" "OR i j" "XOR i j" where $$$i$$$ and $$$j$$$ $$$(1 leq i, j le n$$$, $$$i eq j)$$$ denote the indices being queried. For each query, you will receive an integer $$$x$$$ whose value depends on the type of query. If the indices queried are invalid or you exceed the number of queries however, you will get $$$x = -1$$$. In this case, you should terminate the program immediately. When you have guessed the elements of the array, print a single line "! " (without quotes), followed by $$$n$$$ space-separated integers xa0— the elements of the array. Guessing the array does not count towards the number of queries asked. The interactor is not adaptive. The array $$$a$$$ does not change with queries. After printing a query do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hacks To hack the solution, use the following test format: On the first line print a single integer $$$n$$$ $$$(4 le n le 2^{16})$$$xa0— the length of the array. It must be a power of 2. The next line should contain $$$n$$$ space-separated integers in the range $$$[0, n-1]$$$xa0— the array $$$a$$$. Note The array $$$a$$$ in the example is $$$[0, 0, 2, 3]$$$.
2,300
true
false
false
false
false
true
false
false
false
false
3,423
520C
Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Let's assume that strings _s_ and _t_ have the same length _n_, then the function _h_(_s_,u2009_t_) is defined as the number of positions in which the respective symbols of _s_ and _t_ are the same. Function _h_(_s_,u2009_t_) can be used to define the function of Vasya distance ρ(_s_,u2009_t_): where is obtained from string _s_, by applying left circular shift _i_ times. For example, ρ("_AGC_",u2009"_CGT_")u2009=u2009 _h_("_AGC_",u2009"_CGT_")u2009+u2009_h_("_AGC_",u2009"_GTC_")u2009+u2009_h_("_AGC_",u2009"_TCG_")u2009+u2009 _h_("_GCA_",u2009"_CGT_")u2009+u2009_h_("_GCA_",u2009"_GTC_")u2009+u2009_h_("_GCA_",u2009"_TCG_")u2009+u2009 _h_("_CAG_",u2009"_CGT_")u2009+u2009_h_("_CAG_",u2009"_GTC_")u2009+u2009_h_("_CAG_",u2009"_TCG_")u2009=u2009 1u2009+u20091u2009+u20090u2009+u20090u2009+u20091u2009+u20091u2009+u20091u2009+u20090u2009+u20091u2009=u20096 Vasya found a string _s_ of length _n_ on the Internet. Now he wants to count how many strings _t_ there are such that the Vasya distance from the string _s_ attains maximum possible value. Formally speaking, _t_ must satisfy the equation: . Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109u2009+u20097. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second line of the input contains a single string of length _n_, consisting of characters "ACGT". Output Print a single numberxa0— the answer modulo 109u2009+u20097. Note Please note that if for two distinct strings _t_1 and _t_2 values ρ(_s_,u2009_t_1) и ρ(_s_,u2009_t_2) are maximum among all possible _t_, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one. In the first sample, there is ρ("_C_",u2009"_C_")u2009=u20091, for the remaining strings _t_ of length 1 the value of ρ(_s_,u2009_t_) is 0. In the second sample, ρ("_AG_",u2009"_AG_")u2009=u2009ρ("_AG_",u2009"_GA_")u2009=u2009ρ("_AG_",u2009"_AA_")u2009=u2009ρ("_AG_",u2009"_GG_")u2009=u20094. In the third sample, ρ("_TTT_",u2009"_TTT_")u2009=u200927
1,500
true
false
false
false
false
false
false
false
false
false
7,755
1025G
There are $$$n$$$ startups. Startups can be active or acquired. If a startup is acquired, then that means it has exactly one active startup that it is following. An active startup can have arbitrarily many acquired startups that are following it. An active startup cannot follow any other startup. The following steps happen until there is exactly one active startup. The following sequence of steps takes exactly 1 day. 1. Two distinct active startups $$$A$$$, $$$B$$$, are chosen uniformly at random. 2. A fair coin is flipped, and with equal probability, $$$A$$$ acquires $$$B$$$ or $$$B$$$ acquires $$$A$$$ (i.e. if $$$A$$$ acquires $$$B$$$, then that means $$$B$$$'s state changes from active to acquired, and its starts following $$$A$$$). 3. When a startup changes from active to acquired, all of its previously acquired startups become active. For example, the following scenario can happen: Let's say $$$A$$$, $$$B$$$ are active startups. $$$C$$$, $$$D$$$, $$$E$$$ are acquired startups under $$$A$$$, and $$$F$$$, $$$G$$$ are acquired startups under $$$B$$$: Active startups are shown in red. If $$$A$$$ acquires $$$B$$$, then the state will be $$$A$$$, $$$F$$$, $$$G$$$ are active startups. $$$C$$$, $$$D$$$, $$$E$$$, $$$B$$$ are acquired startups under $$$A$$$. $$$F$$$ and $$$G$$$ have no acquired startups: If instead, $$$B$$$ acquires $$$A$$$, then the state will be $$$B$$$, $$$C$$$, $$$D$$$, $$$E$$$ are active startups. $$$F$$$, $$$G$$$, $$$A$$$ are acquired startups under $$$B$$$. $$$C$$$, $$$D$$$, $$$E$$$ have no acquired startups: You are given the initial state of the startups. For each startup, you are told if it is either acquired or active. If it is acquired, you are also given the index of the active startup that it is following. You're now wondering, what is the expected number of days needed for this process to finish with exactly one active startup at the end. It can be shown the expected number of days can be written as a rational number $$$P/Q$$$, where $$$P$$$ and $$$Q$$$ are co-prime integers, and $$$Q ot= 0 pmod{10^9+7}$$$. Return the value of $$$P cdot Q^{-1}$$$ modulo $$$10^9+7$$$. Input The first line contains a single integer $$$n$$$ ($$$2 leq n leq 500$$$), the number of startups. The next line will contain $$$n$$$ space-separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$a_i = -1$$$ or $$$1 leq a_i leq n$$$). If $$$a_i = -1$$$, then that means startup $$$i$$$ is active. Otherwise, if $$$1 leq a_i leq n$$$, then startup $$$i$$$ is acquired, and it is currently following startup $$$a_i$$$. It is guaranteed if $$$a_i ot= -1$$$, then $$$a_{a_i} =-1$$$ (that is, all startups that are being followed are active). Note In the first sample, there are three active startups labeled $$$1$$$, $$$2$$$ and $$$3$$$, and zero acquired startups. Here's an example of how one scenario can happen 1. Startup $$$1$$$ acquires startup $$$2$$$ (This state can be represented by the array $$$[-1, 1, -1]$$$) 2. Startup $$$3$$$ acquires startup $$$1$$$ (This state can be represented by the array $$$[3, -1, -1]$$$) 3. Startup $$$2$$$ acquires startup $$$3$$$ (This state can be represented by the array $$$[-1, -1, 2]$$$). 4. Startup $$$2$$$ acquires startup $$$1$$$ (This state can be represented by the array $$$[2, -1, 2]$$$). At this point, there is only one active startup, and this sequence of steps took $$$4$$$ days. It can be shown the expected number of days is $$$3$$$. For the second sample, there is only one active startup, so we need zero days. For the last sample, remember to take the answer modulo $$$10^9+7$$$.
3,200
true
false
false
false
false
true
false
false
false
false
5,577
600C
Problem - 600C - Codeforces =============== xa0 ]( "21794") smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically. Input The only line contains string _s_ (1u2009≤u2009_s_u2009≤u20092·105) consisting of only lowercase Latin letters. Output Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes. Examples Input aabc Output abba Input aabcd Output abcba
1,800
false
true
false
false
false
true
false
false
false
false
7,444
1738H
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Global Round 22 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 strings *3300 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST H. Palindrome Addicts time limit per test2 seconds memory limit per test1024 megabytes Your task is to maintain a queue consisting of lowercase English letters as follows: "push $$$c$$$": insert a letter $$$c$$$ at the back of the queue; "pop": delete a letter from the front of the queue. Initially, the queue is empty. After each operation, you are asked to count the number of distinct palindromic substrings in the string that are obtained by concatenating the letters from the front to the back of the queue. Especially, the number of distinct palindromic substrings of the empty string is $$$0$$$. A string $$$s[1..n]$$$ of length $$$n$$$ is palindromic if $$$s[i] = s[n-i+1]$$$ for every $$$1 leq i leq n$$$. The string $$$s[l..r]$$$ is a substring of string $$$s[1..n]$$$ for every $$$1 leq l leq r leq n$$$. Two strings $$$s[1..n]$$$ and $$$t[1..m]$$$ are distinct, if at least one of the following holds. $$$n eq m$$$; $$$s[i] eq t[i]$$$ for some $$$1 leq i leq min{n,m}$$$. Input The first line is an integer $$$q$$$ ($$$1 leq q leq 10^6$$$), indicating the number of operations. Then $$$q$$$ lines follow. Each of the following lines contains one of the operations as follows. "push $$$c$$$": insert a letter $$$c$$$ at the back of the queue, where $$$c$$$ is a lowercase English letter; "pop": delete a letter from the front of the queue. It is guaranteed that no "pop" operation will be performed when the queue is empty. Output After each operation, print the number of distinct palindromic substrings in the string presented in the queue. Example input 12 push a pop push a push a push b push b push a push a pop pop pop push b output 1 0 1 2 3 4 5 6 5 4 3 4 Note Let $$$s_k$$$ be the string presented in the queue after the $$$k$$$-th operation, and let $$$c_k$$$ be the number of distinct palindromic substrings of $$$s_k$$$. The following table shows the details of the example. $$$k$$$ $$$s_k$$$ $$$c_k$$$ $$$1$$$ $$$a$$$ $$$1$$$ $$$2$$$ $$$ extsf{empty}$$$ $$$0$$$ $$$3$$$ $$$a$$$ $$$1$$$ $$$4$$$ $$$aa$$$ $$$2$$$ $$$5$$$ $$$aab$$$ $$$3$$$ $$$6$$$ $$$aabb$$$ $$$4$$$ $$$7$$$ $$$aabba$$$ $$$5$$$ $$$8$$$ $$$aabbaa$$$ $$$6$$$ $$$9$$$ $$$abbaa$$$ $$$5$$$ $$$10$$$ $$$bbaa$$$ $$$4$$$ $$$11$$$ $$$baa$$$ $$$3$$$ $$$12$$$ $$$baab$$$ $$$4$$$ It is worth pointing out that After the $$$2$$$-nd operation, the string is empty and thus has no substrings. So the answer is $$$0$$$; After the $$$8$$$-th operation, the string is "$$$aabbaa$$$". The $$$6$$$ distinct palindromic substrings are "$$$a$$$", "$$$aa$$$", "$$$aabbaa$$$", "$$$abba$$$", "$$$b$$$", and "$$$bb$$$". Codeforces (c)
3,300
false
false
false
false
true
false
false
false
false
false
1,854
678F
Lena is a programmer. She got a task to solve at work. There is an empty set of pairs of integers and _n_ queries to process. Each query is one of three types: 1. Add a pair (_a_,u2009_b_) to the set. 2. Remove a pair added in the query number _i_. All queries are numbered with integers from 1 to _n_. 3. For a given integer _q_ find the maximal value _x_·_q_u2009+u2009_y_ over all pairs (_x_,u2009_y_) from the set. Help Lena to process the queries. Input The first line of input contains integer _n_ (1u2009≤u2009_n_u2009≤u20093·105) — the number of queries. Each of the next _n_ lines starts with integer _t_ (1u2009≤u2009_t_u2009≤u20093) — the type of the query. A pair of integers _a_ and _b_ (u2009-u2009109u2009≤u2009_a_,u2009_b_u2009≤u2009109) follows in the query of the first type. An integer _i_ (1u2009≤u2009_i_u2009≤u2009_n_) follows in the query of the second type. It is guaranteed that _i_ is less than the number of the query, the query number _i_ has the first type and the pair from the _i_-th query is not already removed. An integer _q_ (u2009-u2009109u2009≤u2009_q_u2009≤u2009109) follows in the query of the third type. Output For the queries of the third type print on a separate line the desired maximal value of _x_·_q_u2009+u2009_y_. If there are no pairs in the set print "EMPTY SET". Example Input 7 3 1 1 2 3 3 1 1 -1 100 3 1 2 4 3 1
2,500
false
false
false
false
true
false
false
false
false
false
7,111
762F
You are given two trees (connected undirected acyclic graphs) _S_ and _T_. Count the number of subtrees (connected subgraphs) of _S_ that are isomorphic to tree _T_. Since this number can get quite large, output it modulo 109u2009+u20097. Two subtrees of tree _S_ are considered different, if there exists a vertex in _S_ that belongs to exactly one of them. Tree _G_ is called isomorphic to tree _H_ if there exists a bijection _f_ from the set of vertices of _G_ to the set of vertices of _H_ that has the following property: if there is an edge between vertices _A_ and _B_ in tree _G_, then there must be an edge between vertices _f_(_A_) and _f_(_B_) in tree _H_. And vice versaxa0— if there is an edge between vertices _A_ and _B_ in tree _H_, there must be an edge between _f_u2009-u20091(_A_) and _f_u2009-u20091(_B_) in tree _G_. Input The first line contains a single integer _S_ (1u2009≤u2009_S_u2009≤u20091000) — the number of vertices of tree _S_. Next _S_u2009-u20091 lines contain two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_S_) and describe edges of tree _S_. The next line contains a single integer _T_ (1u2009≤u2009_T_u2009≤u200912) — the number of vertices of tree _T_. Next _T_u2009-u20091 lines contain two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_T_) and describe edges of tree _T_. Output On the first line output a single integer — the answer to the given task modulo 109u2009+u20097. Examples Input 5 1 2 2 3 3 4 4 5 3 1 2 2 3 Input 7 1 2 1 3 1 4 1 5 1 6 1 7 4 4 1 4 2 4 3 Input 5 1 2 2 3 3 4 4 5 4 4 1 4 2 4 3
2,800
false
false
false
false
false
false
false
false
false
true
6,738
1735F
There are two currencies: pebbles and beads. Initially you have $$$a$$$ pebbles, $$$b$$$ beads. There are $$$n$$$ days, each day you can exchange one currency for another at some exchange rate. On day $$$i$$$, you can exchange $$$-p_i leq x leq p_i$$$ pebbles for $$$-q_i leq y leq q_i$$$ beads or vice versa. It's allowed not to perform an exchange at all. Meanwhile, if you perform an exchange, the proportion $$$x cdot q_i = -y cdot p_i$$$ must be fulfilled. Fractional exchanges are allowed. You can perform no more than one such exchange in one day. The numbers of pebbles and beads you have must always remain non-negative. Please solve the following $$$n$$$ problems independently: for each day $$$i$$$, output the maximum number of pebbles that you can have at the end of day $$$i$$$ if you perform exchanges optimally. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^3$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 le n le 300,000$$$, $$$0 le a, b le 10^9$$$) — the number of days and the initial number of pebbles and beads respectively. The second line of each test case contains $$$n$$$ integers: $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le 10^9$$$). The third line of each test case contains $$$n$$$ integers: $$$q_1, q_2, ldots, q_n$$$ ($$$1 le q_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$300,000$$$. Output Output $$$n$$$ numbers — the maximum number of pebbles at the end of each day. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$frac{lefta - b ight}{max(1, leftb ight)} le 10^{-6}$$$. Example Input 3 2 6 0 2 3 4 2 3 0 6 4 2 10 2 3 10 1 10 10 33 1000 Output 6 8 4 6 9.000000 10.33 Note In the image below you can see the solutions for the first two test cases. In each line there is an optimal sequence of actions for each day. In the first test case, the optimal strategy for the first day is to do no action at all, as we can only decrease the number of pebbles. The optimal strategy for the second day is at first to exchange $$$1$$$ pebble for $$$2$$$ beads, which is a correct exchange, since $$$1 cdot 4 = 2 cdot 2$$$, and then to exchange $$$2$$$ beads for $$$3$$$ pebbles.
2,900
false
false
false
false
true
false
false
false
false
false
1,875
1835E
During the latest mission of the starship U.S.S. Coder, Captain Jan Bitovsky was accidentally teleported to the surface of an unknown planet. Trying to find his way back, Jan found an artifact from planet Earth's ancient civilization — a mobile device capable of interstellar calls created by Byterola. Unfortunately, there was another problem. Even though Jan, as a representative of humans, knew perfectly the old notation of the cell phone numbers, the symbols on the device's keyboard were completely worn down and invisible to the human eye. The old keyboards had exactly $$$m + 1$$$ buttons, one for each digit from the base $$$m$$$ numerical system, and one single backspace button allowing one to erase the last written digit (if nothing was written on the screen, then this button does nothing, but it's still counted as pressed). Jan would like to communicate with his crew. He needs to type a certain number (also from the base $$$m$$$ numerical system, that is, digits from $$$0$$$ to $$$m - 1$$$). He wants to know the expected number of button presses necessary to contact the U.S.S. Coder. Jan always chooses the most optimal buttons based on his current knowledge. Buttons are indistinguishable until pressed. Help him! Input In the first line of input, there are two integer numbers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^6$$$, $$$2 le m le 10^3$$$) — the length of the number to U.S.S. Coder and the base of the numerical system. In the next and the last input line, there are $$$n$$$ integers between $$$0$$$ and $$$m - 1$$$: the number to type in the base $$$m$$$ numerical system. Output Output the expected number of button presses modulo $$$1,000,000,007$$$. Formally, let $$$M = 1,000,000,007$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Note In the first example, two digits ($$$0$$$ and $$$1$$$) and a backspace button are available on the keyboard. Jan has no way of knowing which one is which, so he presses a random one. With probability $$$frac{1}{3}$$$, he presses $$$0$$$ and manages to type the crew's number. With probability $$$frac{1}{3}$$$, he presses backspace, and nothing happens. Then with probability $$$frac{1}{2}$$$ he manages to press $$$0$$$ (finishing the process). Otherwise, with probability $$$frac{1}{2}$$$, he types $$$1$$$, which he then needs to remove with backspace and hit the last button, which has to be $$$0$$$. In this case, he needs $$$4$$$ button presses. At last, he might press the $$$1$$$ button first, also with probability $$$frac{1}{3}$$$. Then, if he presses the backspace with a chance of $$$50%$$$, he is all set and only needs to press the last button (3 presses in total). In the worst case, he would press the $$$0$$$ button first and need to remove both with backspace, then finally type the number $$$0$$$ (5 presses in total). We get the expected value of $$$frac{16}{6}$$$. The modular inverse of $$$6$$$ modulo $$$1;000;000;007$$$ is $$$166666668$$$, so $$$16 cdot 166666668 = 666666674 mod ; 1;000;000;007$$$
3,500
false
false
false
true
false
false
false
false
false
false
1,274
1716E
You are given an array of length $$$2^n$$$. The elements of the array are numbered from $$$1$$$ to $$$2^n$$$. You have to process $$$q$$$ queries to this array. In the $$$i$$$-th query, you will be given an integer $$$k$$$ ($$$0 le k le n-1$$$). To process the query, you should do the following: for every $$$i in [1, 2^n-2^k]$$$ in ascending order, do the following: if the $$$i$$$-th element was already swapped with some other element during this query, skip it; otherwise, swap $$$a_i$$$ and $$$a_{i+2^k}$$$; after that, print the maximum sum over all contiguous subsegments of the array (including the empty subsegment). For example, if the array $$$a$$$ is $$$[-3, 5, -3, 2, 8, -20, 6, -1]$$$, and $$$k = 1$$$, the query is processed as follows: the $$$1$$$-st element wasn't swapped yet, so we swap it with the $$$3$$$-rd element; the $$$2$$$-nd element wasn't swapped yet, so we swap it with the $$$4$$$-th element; the $$$3$$$-rd element was swapped already; the $$$4$$$-th element was swapped already; the $$$5$$$-th element wasn't swapped yet, so we swap it with the $$$7$$$-th element; the $$$6$$$-th element wasn't swapped yet, so we swap it with the $$$8$$$-th element. So, the array becomes $$$[-3, 2, -3, 5, 6, -1, 8, -20]$$$. The subsegment with the maximum sum is $$$[5, 6, -1, 8]$$$, and the answer to the query is $$$18$$$. Note that the queries actually change the array, i.u2009e. after a query is performed, the array does not return to its original state, and the next query will be applied to the modified array. Input The first line contains one integer $$$n$$$ ($$$1 le n le 18$$$). The second line contains $$$2^n$$$ integers $$$a_1, a_2, dots, a_{2^n}$$$ ($$$-10^9 le a_i le 10^9$$$). The third line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$). Then $$$q$$$ lines follow, the $$$i$$$-th of them contains one integer $$$k$$$ ($$$0 le k le n-1$$$) describing the $$$i$$$-th query. Output For each query, print one integerxa0— the maximum sum over all contiguous subsegments of the array (including the empty subsegment) after processing the query. Example Input 3 -3 5 -3 2 8 -20 6 -1 3 1 0 1 Note Transformation of the array in the example: $$$[-3, 5, -3, 2, 8, -20, 6, -1] ightarrow [-3, 2, -3, 5, 6, -1, 8, -20] ightarrow [2, -3, 5, -3, -1, 6, -20, 8] ightarrow [5, -3, 2, -3, -20, 8, -1, 6]$$$.
2,500
false
false
false
true
true
false
false
false
false
false
1,983
1559C
The city where Mocha lives in is called Zhijiang. There are $$$n+1$$$ villages and $$$2n-1$$$ directed roads in this city. There are two kinds of roads: $$$n-1$$$ roads are from village $$$i$$$ to village $$$i+1$$$, for all $$$1leq i leq n-1$$$. $$$n$$$ roads can be described by a sequence $$$a_1,ldots,a_n$$$. If $$$a_i=0$$$, the $$$i$$$-th of these roads goes from village $$$i$$$ to village $$$n+1$$$, otherwise it goes from village $$$n+1$$$ to village $$$i$$$, for all $$$1leq ileq n$$$. Mocha plans to go hiking with Taki this weekend. To avoid the trip being boring, they plan to go through every village exactly once. They can start and finish at any villages. Can you help them to draw up a plan? Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 20$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^4$$$) — indicates that the number of villages is $$$n+1$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 1$$$). If $$$a_i=0$$$, it means that there is a road from village $$$i$$$ to village $$$n+1$$$. If $$$a_i=1$$$, it means that there is a road from village $$$n+1$$$ to village $$$i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Output For each test case, print a line with $$$n+1$$$ integers, where the $$$i$$$-th number is the $$$i$$$-th village they will go through. If the answer doesn't exist, print $$$-1$$$. If there are multiple correct answers, you can print any one of them. Note In the first test case, the city looks like the following graph: So all possible answers are $$$(1 o 4 o 2 o 3)$$$, $$$(1 o 2 o 3 o 4)$$$. In the second test case, the city looks like the following graph: So all possible answers are $$$(4 o 1 o 2 o 3)$$$, $$$(1 o 2 o 3 o 4)$$$, $$$(3 o 4 o 1 o 2)$$$, $$$(2 o 3 o 4 o 1)$$$.
1,200
false
false
false
false
false
true
false
false
false
true
2,834
1462F
Polycarp found $$$n$$$ segments on the street. A segment with the index $$$i$$$ is described by two integers $$$l_i$$$ and $$$r_i$$$xa0— coordinates of the beginning and end of the segment, respectively. Polycarp realized that he didn't need all the segments, so he wanted to delete some of them. Polycarp believes that a set of $$$k$$$ segments is good if there is a segment $$$[l_i, r_i]$$$ ($$$1 leq i leq k$$$) from the set, such that it intersects every segment from the set (the intersection must be a point or segment). For example, a set of $$$3$$$ segments $$$[[1, 4], [2, 3], [3, 6]]$$$ is good, since the segment $$$[2, 3]$$$ intersects each segment from the set. Set of $$$4$$$ segments $$$[[1, 2], [2, 3], [3, 5], [4, 5]]$$$ is not good. Polycarp wonders, what is the minimum number of segments he has to delete so that the remaining segments form a good set? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2 cdot 10^5$$$)xa0— number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the number of segments. This is followed by $$$n$$$ lines describing the segments. Each segment is described by two integers $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq 10^9$$$)xa0— coordinates of the beginning and end of the segment, respectively. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$.
1,800
false
true
false
false
true
false
false
true
false
false
3,374
1692H
Marian is at a casino. The game at the casino works like this. Before each round, the player selects a number between $$$1$$$ and $$$10^9$$$. After that, a dice with $$$10^9$$$ faces is rolled so that a random number between $$$1$$$ and $$$10^9$$$ appears. If the player guesses the number correctly their total money is doubled, else their total money is halved. Marian predicted the future and knows all the numbers $$$x_1, x_2, dots, x_n$$$ that the dice will show in the next $$$n$$$ rounds. He will pick three integers $$$a$$$, $$$l$$$ and $$$r$$$ ($$$l leq r$$$). He will play $$$r-l+1$$$ rounds (rounds between $$$l$$$ and $$$r$$$ inclusive). In each of these rounds, he will guess the same number $$$a$$$. At the start (before the round $$$l$$$) he has $$$1$$$ dollar. Marian asks you to determine the integers $$$a$$$, $$$l$$$ and $$$r$$$ ($$$1 leq a leq 10^9$$$, $$$1 leq l leq r leq n$$$) such that he makes the most money at the end. Note that during halving and multiplying there is no rounding and there are no precision errors. So, for example during a game, Marian could have money equal to $$$dfrac{1}{1024}$$$, $$$dfrac{1}{128}$$$, $$$dfrac{1}{2}$$$, $$$1$$$, $$$2$$$, $$$4$$$, etc. (any value of $$$2^t$$$, where $$$t$$$ is an integer of any sign). Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2cdot 10^5$$$)xa0— the number of rounds. The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 leq x_i leq 10^9$$$), where $$$x_i$$$ is the number that will fall on the dice in the $$$i$$$-th round. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, output three integers $$$a$$$, $$$l$$$, and $$$r$$$ such that Marian makes the most amount of money gambling with his strategy. If there are multiple answers, you may output any of them. Note For the first test case, the best choice is $$$a=4$$$, $$$l=1$$$, $$$r=5$$$, and the game would go as follows. Marian starts with one dollar. After the first round, he ends up with $$$2$$$ dollars because the numbers coincide with the chosen one. After the second round, he ends up with $$$4$$$ dollars because the numbers coincide again. After the third round, he ends up with $$$2$$$ dollars because he guesses $$$4$$$ even though $$$3$$$ is the correct choice. After the fourth round, he ends up with $$$4$$$ dollars again. In the final round, he ends up $$$8$$$ dollars because he again guessed correctly. There are many possible answers for the second test case, but it can be proven that Marian will not end up with more than $$$2$$$ dollars, so any choice with $$$l = r$$$ with the appropriate $$$a$$$ is acceptable.
1,700
true
true
false
true
true
false
false
false
false
false
2,123
1695D1
The only difference between this problem and D2 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 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2000$$$) 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 $$$2000$$$. 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,200
false
true
false
true
false
true
true
false
false
false
2,111
607A
There are _n_ beacons located at distinct positions on a number line. The _i_-th beacon has position _a__i_ and power level _b__i_. When the _i_-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance _b__i_ inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated. Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed. Input The first line of input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000) — the initial number of beacons. The _i_-th of next _n_ lines contains two integers _a__i_ and _b__i_ (0u2009≤u2009_a__i_u2009≤u20091u2009000u2009000, 1u2009≤u2009_b__i_u2009≤u20091u2009000u2009000)xa0— the position and power level of the _i_-th beacon respectively. No two beacons will have the same position, so _a__i_u2009≠u2009_a__j_ if _i_u2009≠u2009_j_. Output Print a single integerxa0— the minimum number of beacons that could be destroyed if exactly one beacon is added. Examples Input 7 1 1 2 1 3 1 4 1 5 1 6 1 7 1 Note For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2. For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
1,600
false
false
false
true
false
false
false
true
false
false
7,419
1305H
As a professional private tutor, Kuroni has to gather statistics of an exam. Kuroni has appointed you to complete this important task. You must not disappoint him. The exam consists of $$$n$$$ questions, and $$$m$$$ students have taken the exam. Each question was worth $$$1$$$ point. Question $$$i$$$ was solved by at least $$$l_i$$$ and at most $$$r_i$$$ students. Additionally, you know that the total score of all students is $$$t$$$. Furthermore, you took a glance at the final ranklist of the quiz. The students were ranked from $$$1$$$ to $$$m$$$, where rank $$$1$$$ has the highest score and rank $$$m$$$ has the lowest score. Ties were broken arbitrarily. You know that the student at rank $$$p_i$$$ had a score of $$$s_i$$$ for $$$1 le i le q$$$. You wonder if there could have been a huge tie for first place. Help Kuroni determine the maximum number of students who could have gotten as many points as the student with rank $$$1$$$, and the maximum possible score for rank $$$1$$$ achieving this maximum number of students. Input The first line of input contains two integers ($$$1 le n, m le 10^{5}$$$), denoting the number of questions of the exam and the number of students respectively. The next $$$n$$$ lines contain two integers each, with the $$$i$$$-th line containing $$$l_{i}$$$ and $$$r_{i}$$$ ($$$0 le l_{i} le r_{i} le m$$$). The next line contains a single integer $$$q$$$ ($$$0 le q le m$$$). The next $$$q$$$ lines contain two integers each, denoting $$$p_{i}$$$ and $$$s_{i}$$$ ($$$1 le p_{i} le m$$$, $$$0 le s_{i} le n$$$). It is guaranteed that all $$$p_{i}$$$ are distinct and if $$$p_{i} le p_{j}$$$, then $$$s_{i} ge s_{j}$$$. The last line contains a single integer $$$t$$$ ($$$0 le t le nm$$$), denoting the total score of all students. Output Output two integers: the maximum number of students who could have gotten as many points as the student with rank $$$1$$$, and the maximum possible score for rank $$$1$$$ achieving this maximum number of students. If there is no valid arrangement that fits the given data, output $$$-1$$$ $$$-1$$$. Examples Input 5 4 2 4 2 3 1 1 0 1 0 0 1 4 1 7 Input 5 6 0 6 0 6 2 5 6 6 4 6 1 3 3 30 Note For the first sample, here is one possible arrangement that fits the data: Students $$$1$$$ and $$$2$$$ both solved problems $$$1$$$ and $$$2$$$. Student $$$3$$$ solved problems $$$2$$$ and $$$3$$$. Student $$$4$$$ solved problem $$$4$$$. The total score of all students is $$$T = 7$$$. Note that the scores of the students are $$$2$$$, $$$2$$$, $$$2$$$ and $$$1$$$ respectively, which satisfies the condition that the student at rank $$$4$$$ gets exactly $$$1$$$ point. Finally, $$$3$$$ students tied for first with a maximum score of $$$2$$$, and it can be proven that we cannot do better with any other arrangement.
3,500
false
true
false
false
false
false
false
true
false
false
4,157
1447A
There are $$$n$$$ bags with candies, initially the $$$i$$$-th bag contains $$$i$$$ candies. You want all the bags to contain an equal amount of candies in the end. To achieve this, you will: Choose $$$m$$$ such that $$$1 le m le 1000$$$ Perform $$$m$$$ operations. In the $$$j$$$-th operation, you will pick one bag and add $$$j$$$ candies to all bags apart from the chosen one. Your goal is to find a valid sequence of operations after which all the bags will contain an equal amount of candies. It can be proved that for the given constraints such a sequence always exists. You don't have to minimize $$$m$$$. If there are several valid sequences, you can output any. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first and only line of each test case contains one integer $$$n$$$ ($$$2 le nle 100$$$). Output For each testcase, print two lines with your answer. In the first line print $$$m$$$ ($$$1le m le 1000$$$)xa0— the number of operations you want to take. In the second line print $$$m$$$ positive integers $$$a_1, a_2, dots, a_m$$$ ($$$1 le a_i le n$$$), where $$$a_j$$$ is the number of bag you chose on the $$$j$$$-th operation. Note In the first case, adding $$$1$$$ candy to all bags except of the second one leads to the arrangement with $$$[2, 2]$$$ candies. In the second case, firstly you use first three operations to add $$$1+2+3=6$$$ candies in total to each bag except of the third one, which gives you $$$[7, 8, 3]$$$. Later, you add $$$4$$$ candies to second and third bag, so you have $$$[7, 12, 7]$$$, and $$$5$$$ candies to first and third bag xa0— and the result is $$$[12, 12, 12]$$$.
800
true
false
false
false
false
true
false
false
false
false
3,440
1187A
Your favorite shop sells $$$n$$$ Kinder Surprise chocolate eggs. You know that exactly $$$s$$$ stickers and exactly $$$t$$$ toys are placed in $$$n$$$ eggs in total. Each Kinder Surprise can be one of three types: it can contain a single sticker and no toy; it can contain a single toy and no sticker; it can contain both a single sticker and a single toy. But you don't know which type a particular Kinder Surprise has. All eggs look identical and indistinguishable from each other. What is the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you'll obtain at least one sticker and at least one toy? Note that you do not open the eggs in the purchasing process, that is, you just buy some number of eggs. It's guaranteed that the answer always exists. Input The first line contains the single integer $$$T$$$ ($$$1 le T le 100$$$) — the number of queries. Next $$$T$$$ lines contain three integers $$$n$$$, $$$s$$$ and $$$t$$$ each ($$$1 le n le 10^9$$$, $$$1 le s, t le n$$$, $$$s + t ge n$$$) — the number of eggs, stickers and toys. All queries are independent. Output Print $$$T$$$ integers (one number per query) — the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you'll obtain at least one sticker and one toy Example Input 3 10 5 7 10 10 10 2 1 1 Note In the first query, we have to take at least $$$6$$$ eggs because there are $$$5$$$ eggs with only toy inside and, in the worst case, we'll buy all of them. In the second query, all eggs have both a sticker and a toy inside, that's why it's enough to buy only one egg. In the third query, we have to buy both eggs: one with a sticker and one with a toy.
900
true
false
false
false
false
false
false
false
false
false
4,766
1114C
The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number $$$9200$$$ has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is. However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers. Given two integers $$$n$$$ and $$$b$$$ (in decimal notation), your task is to calculate the number of trailing zero digits in the $$$b$$$-ary (in the base/radix of $$$b$$$) representation of $$$n,!$$$ (. Input The only line of the input contains two integers $$$n$$$ and $$$b$$$ ($$$1 le n le 10^{18}$$$, $$$2 le b le 10^{12}$$$). Output Print an only integerxa0— the number of trailing zero digits in the $$$b$$$-ary representation of $$$n!$$$ Note In the first example, $$$6!_{(10)} = 720_{(10)} = 880_{(9)}$$$. In the third and fourth example, $$$5!_{(10)} = 120_{(10)} = 1111000_{(2)}$$$. The representation of the number $$$x$$$ in the $$$b$$$-ary base is $$$d_1, d_2, ldots, d_k$$$ if $$$x = d_1 b^{k - 1} + d_2 b^{k - 2} + ldots + d_k b^0$$$, where $$$d_i$$$ are integers and $$$0 le d_i le b - 1$$$. For example, the number $$$720$$$ from the first example is represented as $$$880_{(9)}$$$ since $$$720 = 8 cdot 9^2 + 8 cdot 9 + 0 cdot 1$$$. You can read more about bases [here](
1,700
true
false
true
false
false
false
true
false
false
false
5,132
1681A
Alice and Bob play a game. Alice has $$$n$$$ cards, the $$$i$$$-th of them has the integer $$$a_i$$$ written on it. Bob has $$$m$$$ cards, the $$$j$$$-th of them has the integer $$$b_j$$$ written on it. On the first turn of the game, the first player chooses one of his/her cards and puts it on the table (plays it). On the second turn, the second player chooses one of his/her cards such that the integer on it is greater than the integer on the card played on the first turn, and plays it. On the third turn, the first player chooses one of his/her cards such that the integer on it is greater than the integer on the card played on the second turn, and plays it, and so on — the players take turns, and each player has to choose one of his/her cards with greater integer than the card played by the other player on the last turn. If some player cannot make a turn, he/she loses. For example, if Alice has $$$4$$$ cards with numbers $$$[10, 5, 3, 8]$$$, and Bob has $$$3$$$ cards with numbers $$$[6, 11, 6]$$$, the game may go as follows: Alice can choose any of her cards. She chooses the card with integer $$$5$$$ and plays it. Bob can choose any of his cards with number greater than $$$5$$$. He chooses a card with integer $$$6$$$ and plays it. Alice can choose any of her cards with number greater than $$$6$$$. She chooses the card with integer $$$10$$$ and plays it. Bob can choose any of his cards with number greater than $$$10$$$. He chooses a card with integer $$$11$$$ and plays it. Alice can choose any of her cards with number greater than $$$11$$$, but she has no such cards, so she loses. Both Alice and Bob play optimally (if a player is able to win the game no matter how the other player plays, the former player will definitely win the game). You have to answer two questions: who wins if Alice is the first player? who wins if Bob is the first player? Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Each test case consists of four lines. The first line of a test case contains one integer $$$n$$$ ($$$1 le n le 50$$$) — the number of cards Alice has. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 50$$$) — the numbers written on the cards that Alice has. The third line contains one integer $$$m$$$ ($$$1 le m le 50$$$) — the number of Bob's cards. The fourth line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 le b_i le 50$$$) — the numbers on Bob's cards. Output For each test case, print two lines. The first line should be Alice if Alice wins when she is the first player; otherwise, the first line should be Bob. The second line should contain the name of the winner if Bob is the first player, in the same format. Example Input 4 1 6 2 6 8 4 1 3 3 7 2 4 2 1 50 2 25 50 10 1 2 3 4 5 6 7 8 9 10 2 5 15 Output Bob Bob Alice Alice Alice Bob Bob Bob Note Let's consider the first test case of the example. Alice has one card with integer $$$6$$$, Bob has two cards with numbers $$$[6, 8]$$$. If Alice is the first player, she has to play the card with number $$$6$$$. Bob then has to play the card with number $$$8$$$. Alice has no cards left, so she loses. If Bob is the first player, then no matter which of his cards he chooses on the first turn, Alice won't be able to play her card on the second turn, so she will lose.
800
false
true
false
false
false
false
false
false
false
false
2,185
1979E
The Manhattan distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is defined as: $$$$$$x_1 - x_2 + y_1 - y_2.$$$$$$ We call a Manhattan triangle three points on the plane, the Manhattan distances between each pair of which are equal. You are given a set of pairwise distinct points and an even integer $$$d$$$. Your task is to find any Manhattan triangle, composed of three distinct points from the given set, where the Manhattan distance between any pair of vertices is equal to $$$d$$$. Input Each test consists of multiple test cases. The first line contains one 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 $$$d$$$ ($$$3 le n le 2 cdot 10^5$$$, $$$2 le d le 4 cdot 10^5$$$, $$$d$$$ is even)xa0— the number of points and the required Manhattan distance between the vertices of the triangle. The $$$(i + 1)$$$-th line of each test case contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^5 le x_i, y_i le 10^5$$$)xa0— the coordinates of the $$$i$$$-th point. It is guaranteed that all points are pairwise distinct. 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 three distinct integers $$$i$$$, $$$j$$$, and $$$k$$$ ($$$1 le i,j,k le n$$$)xa0— the indices of the points forming the Manhattan triangle. If there is no solution, output "$$$0 0 0$$$" (without quotes). If there are multiple solutions, output any of them. Example Input 6 6 4 3 1 0 0 0 -2 5 -3 3 -5 2 -2 5 4 0 0 0 -2 5 -3 3 -5 2 -2 6 6 3 1 0 0 0 -2 5 -3 3 -5 2 -2 4 4 3 0 0 3 -3 0 0 -3 10 8 2 1 -5 -1 -4 -1 -5 -3 0 1 -2 5 -4 4 -4 2 0 0 -4 1 4 400000 100000 100000 -100000 100000 100000 -100000 -100000 -100000 Output 2 6 1 4 3 5 3 5 1 0 0 0 6 1 3 0 0 0 Note In the first test case: Points $$$A$$$, $$$B$$$, and $$$F$$$ form a Manhattan triangle, the Manhattan distance between each pair of vertices is $$$4$$$. Points $$$D$$$, $$$E$$$, and $$$F$$$ can also be the answer. In the third test case: Points $$$A$$$, $$$C$$$, and $$$E$$$ form a Manhattan triangle, the Manhattan distance between each pair of vertices is $$$6$$$. In the fourth test case, there are no two points with a Manhattan distance of $$$4$$$, and therefore there is no suitable Manhattan triangle.
2,400
false
false
true
false
true
true
false
true
false
false
412
1538A
Polycarp is playing a new computer game. This game has $$$n$$$ stones in a row. The stone on the position $$$i$$$ has integer power $$$a_i$$$. The powers of all stones are distinct. Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more. Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal. For example, if $$$n = 5$$$ and $$$a = [1, 5, 4, 3, 2]$$$, then Polycarp could make the following moves: Destroy the leftmost stone. After this move $$$a = [5, 4, 3, 2]$$$; Destroy the rightmost stone. After this move $$$a = [5, 4, 3]$$$; Destroy the leftmost stone. After this move $$$a = [4, 3]$$$. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: Destroy the leftmost stone. After this move $$$a = [5, 4, 3, 2]$$$; Destroy the leftmost stone. After this move $$$a = [4, 3, 2]$$$. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Input The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 le n le 100$$$)xa0— the number of stones. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the power of the stones. Output For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power. Example Input 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4
800
false
true
false
true
false
false
true
false
false
false
2,955
849B
Problem - 849B - 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 geometry *1600 No tag edit access → Contest materials . Determine whether it's possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set. Input The first line of input contains a positive integer _n_ (3u2009≤u2009_n_u2009≤u20091u2009000) — the number of points. The second line contains _n_ space-separated integers _y_1,u2009_y_2,u2009...,u2009_y__n_ (u2009-u2009109u2009≤u2009_y__i_u2009≤u2009109) — the vertical coordinates of each point. Output Output "Yes" (without quotes) if it's possible to fulfill the requirements, and "No" otherwise. You can print each letter in any case (upper or lower). Examples Input 5 7 5 8 6 9 Output Yes Input 5 -1 -2 0 0 -5 Output No Input 5 5 4 3 2 1 Output No Input 5 1000000000 0 0 0 0 Output Yes Note In the first example, there are five points: (1,u20097), (2,u20095), (3,u20098), (4,u20096) and (5,u20099). It's possible to draw a line that passes through points 1,u20093,u20095, and another one that passes through points 2,u20094 and is parallel to the first one. In the second example, while it's possible to draw two lines that cover all points, they cannot be made parallel. In the third example, it's impossible to satisfy both requirements at the same time.
1,600
false
false
false
false
false
false
true
false
false
false
6,352
5C
Problem - 5C - Codeforces =============== xa0 )()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. Input The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106. Output Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". Examples Input )((())))(()()) Output 6 2 Input ))( Output 0 1
1,900
false
true
false
true
true
true
false
false
true
false
9,968
1266D
There are $$$n$$$ people in this world, conveniently numbered $$$1$$$ through $$$n$$$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $$$d(a,b)$$$ denote the debt of $$$a$$$ towards $$$b$$$, or $$$0$$$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: 1. Let $$$d(a,b) > 0$$$ and $$$d(c,d) > 0$$$ such that $$$a eq c$$$ or $$$b eq d$$$. We can decrease the $$$d(a,b)$$$ and $$$d(c,d)$$$ byxa0$$$z$$$ and increase $$$d(c,b)$$$ and $$$d(a,d)$$$ byxa0$$$z$$$, where $$$0 < z leq min(d(a,b),d(c,d))$$$. 2. Let $$$d(a,a) > 0$$$. We can set $$$d(a,a)$$$ to $$$0$$$. The total debt is defined as the sum of all debts: $$$$$$Sigma_d = sum_{a,b} d(a,b)$$$$$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. Input The first line contains two space separated integers $$$n$$$xa0($$$1 leq n leq 10^5$$$) and $$$m$$$xa0($$$0 leq m leq 3cdot 10^5$$$), representing the number of people and the number of debts, respectively. $$$m$$$ lines follow, each of which contains three space separated integers $$$u_i$$$, $$$v_i$$$xa0($$$1 leq u_i, v_i leq n, u_i eq v_i$$$), $$$d_i$$$xa0($$$1 leq d_i leq 10^9$$$), meaning that the person $$$u_i$$$ borrowed $$$d_i$$$ burles from person $$$v_i$$$. Output On the first line print an integer $$$m'$$$xa0($$$0 leq m' leq 3cdot 10^5$$$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $$$m'$$$ lines, $$$i$$$-th of which contains three space separated integers $$$u_i, v_i, d_i$$$, meaning that the person $$$u_i$$$ owes the person $$$v_i$$$ exactly $$$d_i$$$ burles. The output must satisfy $$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$ and $$$0 < d_i leq 10^{18}$$$. For each pair $$$i eq j$$$, it should hold that $$$u_i eq u_j$$$ or $$$v_i eq v_j$$$. In other words, each pair of people can be included at most once in the output. Examples Input 3 3 1 2 10 2 3 15 3 1 10 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Note In the first example the optimal sequence of operations can be the following: 1. Perform an operation of the first type with $$$a = 1$$$, $$$b = 2$$$, $$$c = 2$$$, $$$d = 3$$$ and $$$z = 5$$$. The resulting debts are: $$$d(1, 2) = 5$$$, $$$d(2, 2) = 5$$$, $$$d(1, 3) = 5$$$, all other debts are $$$0$$$; 2. Perform an operation of the second type with $$$a = 2$$$. The resulting debts are: $$$d(1, 2) = 5$$$, $$$d(1, 3) = 5$$$, all other debts are $$$0$$$. In the second example the optimal sequence of operations can be the following: 1. Perform an operation of the first type with $$$a = 1$$$, $$$b = 2$$$, $$$c = 3$$$, $$$d = 1$$$ and $$$z = 10$$$. The resulting debts are: $$$d(3, 2) = 10$$$, $$$d(2, 3) = 15$$$, $$$d(1, 1) = 10$$$, all other debts are $$$0$$$; 2. Perform an operation of the first type with $$$a = 2$$$, $$$b = 3$$$, $$$c = 3$$$, $$$d = 2$$$ and $$$z = 10$$$. The resulting debts are: $$$d(2, 2) = 10$$$, $$$d(3, 3) = 10$$$, $$$d(2, 3) = 5$$$, $$$d(1, 1) = 10$$$, all other debts are $$$0$$$; 3. Perform an operation of the second type with $$$a = 2$$$. The resulting debts are: $$$d(3, 3) = 10$$$, $$$d(2, 3) = 5$$$, $$$d(1, 1) = 10$$$, all other debts are $$$0$$$; 4. Perform an operation of the second type with $$$a = 3$$$. The resulting debts are: $$$d(2, 3) = 5$$$, $$$d(1, 1) = 10$$$, all other debts are $$$0$$$; 5. Perform an operation of the second type with $$$a = 1$$$. The resulting debts are: $$$d(2, 3) = 5$$$, all other debts are $$$0$$$.
2,000
true
true
true
false
true
true
false
false
false
true
4,368
554B
Ohana Matsumae is trying to clean a room, which is divided up into an _n_ by _n_ grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column. Return the maximum number of rows that she can make completely clean. Input The first line of input will be a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100). The next _n_ lines will describe the state of the room. The _i_-th line will contain a binary string with _n_ characters denoting the state of the _i_-th row of the room. The _j_-th character on this line is '1' if the _j_-th square in the _i_-th row is clean, and '0' if it is dirty. Output The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean. Note In the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean. In the second sample, everything is already clean, so Ohana doesn't need to do anything.
1,200
false
true
false
false
false
false
true
false
false
false
7,616