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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
645E | After getting kicked out of her reporting job for not knowing the alphabet, Bessie has decided to attend school at the Fillet and Eggs Eater Academy. She has been making good progress with her studies and now knows the first _k_ English letters. Each morning, Bessie travels to school along a sidewalk consisting of _m_u2009+u2009_n_ tiles. In order to help Bessie review, Mr. Moozing has labeled each of the first _m_ sidewalk tiles with one of the first _k_ lowercase English letters, spelling out a string _t_. Mr. Moozing, impressed by Bessie's extensive knowledge of farm animals, plans to let her finish labeling the last _n_ tiles of the sidewalk by herself. Consider the resulting string _s_ (_s_u2009=u2009_m_u2009+u2009_n_) consisting of letters labeled on tiles in order from home to school. For any sequence of indices _p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__q_ we can define subsequence of the string _s_ as string _s__p_1_s__p_2... _s__p__q_. Two subsequences are considered to be distinct if they differ as strings. Bessie wants to label the remaining part of the sidewalk such that the number of distinct subsequences of tiles is maximum possible. However, since Bessie hasn't even finished learning the alphabet, she needs your help! Note that empty subsequence also counts. Input The first line of the input contains two integers _n_ and _k_ (0u2009≤u2009_n_u2009≤u20091u2009000u2009000, 1u2009≤u2009_k_u2009≤u200926). The second line contains a string _t_ (_t_u2009=u2009_m_,u20091u2009≤u2009_m_u2009≤u20091u2009000u2009000) consisting of only first _k_ lowercase English letters. Output Determine the maximum number of distinct subsequences Bessie can form after labeling the last _n_ sidewalk tiles each with one of the first _k_ lowercase English letters. Since this number can be rather large, you should print it modulo 109u2009+u20097. Please note, that you are not asked to maximize the remainder modulo 109u2009+u20097! The goal is to maximize the initial value and then print the remainder. Note In the first sample, the optimal labeling gives 8 different subsequences: "" (the empty string), "a", "c", "b", "ac", "ab", "cb", and "acb". In the second sample, the entire sidewalk is already labeled. The are 10 possible different subsequences: "" (the empty string), "a", "b", "aa", "ab", "ba", "aaa", "aab", "aba", and "aaba". Note that some strings, including "aa", can be obtained with multiple sequences of tiles, but are only counted once. | 2,200 | false | true | false | true | false | false | false | false | false | false | 7,230 |
78E | They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon. The station is represented by a square _n_u2009×u2009_n_ divided into 1u2009×u20091 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge. In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist. The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks. There are exactly _t_ minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected. Find the maximum number of scientists who will be able to escape. Input The first line contains two integers _n_ and _t_ (2u2009≤u2009_n_u2009≤u200910, 1u2009≤u2009_t_u2009≤u200960). Each of the next _n_ lines contains _n_ characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next _n_ more lines contains _n_ characters. These lines describe the rescue capsules' locations. In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory. | 2,300 | false | false | false | false | false | false | false | false | false | true | 9,571 |
515E | Drazil is a monkey. He lives in a circular park. There are _n_ trees around the park. The distance between the _i_-th tree and (_i_u2009+u20091)-st trees is _d__i_, the distance between the _n_-th tree and the first tree is _d__n_. The height of the _i_-th tree is _h__i_. Drazil starts each day with the morning run. The morning run consists of the following steps: Drazil chooses two different trees He starts with climbing up the first tree Then he climbs down the first tree, runs around the park (in one of two possible directions) to the second tree, and climbs on it Then he finally climbs down the second tree. But there are always children playing around some consecutive trees. Drazil can't stand children, so he can't choose the trees close to children. He even can't stay close to those trees. If the two trees Drazil chooses are _x_-th and _y_-th, we can estimate the energy the morning run takes to him as 2(_h__x_u2009+u2009_h__y_)u2009+u2009_dist_(_x_,u2009_y_). Since there are children on exactly one of two arcs connecting _x_ and _y_, the distance _dist_(_x_,u2009_y_) between trees _x_ and _y_ is uniquely defined. Now, you know that on the _i_-th day children play between _a__i_-th tree and _b__i_-th tree. More formally, if _a__i_u2009≤u2009_b__i_, children play around the trees with indices from range [_a__i_,u2009_b__i_], otherwise they play around the trees with indices from . Please help Drazil to determine which two trees he should choose in order to consume the most energy (since he wants to become fit and cool-looking monkey) and report the resulting amount of energy for each day. Input The first line contains two integer _n_ and _m_ (3u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_m_u2009≤u2009105), denoting number of trees and number of days, respectively. The second line contains _n_ integers _d_1,u2009_d_2,u2009...,u2009_d__n_ (1u2009≤u2009_d__i_u2009≤u2009109), the distances between consecutive trees. The third line contains _n_ integers _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009≤u2009_h__i_u2009≤u2009109), the heights of trees. Each of following _m_ lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_) describing each new day. There are always at least two different trees Drazil can choose that are not affected by children. Output For each day print the answer in a separate line. Examples Input 5 3 2 2 2 2 2 3 5 2 1 4 1 3 2 2 4 5 Input 3 3 5 1 4 5 1 4 3 3 2 2 1 1 | 2,300 | false | false | false | false | true | false | false | false | false | false | 7,771 |
555D | Andrewid the Android is a galaxy-famous detective. Now he is busy with a top secret case, the details of which are not subject to disclosure. However, he needs help conducting one of the investigative experiment. There are _n_ pegs put on a plane, they are numbered from 1 to _n_, the coordinates of the _i_-th of them are (_x__i_,u20090). Then, we tie to the bottom of one of the pegs a weight on a tight rope of length _l_ (thus, its coordinates will be equal to (_x__i_,u2009u2009-u2009_l_), where _i_ is the number of the used peg). Then the weight is pushed to the right, so that it starts to rotate counterclockwise. At the same time, if the weight during rotation touches some of the other pegs, it then begins to rotate around that peg. Suppose that each peg itself is very thin and does not affect the rope length while weight is rotating around it. More formally, if at some moment the segment of the rope contains one or more pegs in addition to the peg around which the weight is rotating, the weight will then rotate around the farthermost one of them on a shorter segment of a rope. In particular, if the segment of the rope touches some peg by its endpoint, it is considered that the weight starts to rotate around that peg on a segment of the rope of length 0. At some moment the weight will begin to rotate around some peg, without affecting the rest of the pegs. Andrewid interested in determining the number of this peg. Andrewid prepared _m_ queries containing initial conditions for pushing the weight, help him to determine for each of them, around what peg the weight will eventually rotate. Input The first line contains integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105) — the number of pegs and queries. The next line contains _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (u2009-u2009109u2009≤u2009_x__i_u2009≤u2009109) — the coordinates of the pegs. It is guaranteed that the coordinates of all the pegs are distinct integers. Next _m_ lines contain the descriptions of the queries of pushing the weight, each consists of two integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009_n_) and _l__i_ (1u2009≤u2009_l__i_u2009≤u2009109) — the number of the starting peg and the length of the rope. Note Picture to the first sample test: Picture to the second sample test: Note that in the last query weight starts to rotate around the peg 1 attached to a rope segment of length 0. | 2,500 | true | false | true | false | false | false | false | true | false | false | 7,612 |
1615B | You are given an array consisting of all integers from $$$xa0— the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq 2 cdot 10^5$$$)xa0— the description of the array. Note In the first test case, the array is $$$[1, 2]$$$. Currently, the bitwise AND is $$$0$$$, as $$$1 & 2 = 0$$$. However, after deleting $$$1$$$ (or $$$2$$$), the array becomes $$$[2]$$$ (or $$$[1]$$$), and the bitwise AND becomes $$$2$$$ (or $$$1$$$). This can be proven to be the optimal, so the answer is $$$1$$$. In the second test case, the array is $$$[2, 3, 4, 5, 6, 7, 8]$$$. Currently, the bitwise AND is $$$0$$$. However, after deleting $$$4$$$, $$$5$$$, and $$$8$$$, the array becomes $$$[2, 3, 6, 7]$$$, and the bitwise AND becomes $$$2$$$. This can be proven to be the optimal, so the answer is $$$3$$$. Note that there may be other ways to delete $$$3$$$ elements. | 1,300 | true | true | false | false | false | false | false | false | false | false | 2,563 |
1102E | Problem - 1102E - 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 sortings *1700 No tag edit access → Contest materials ") ; for every index $$$i in | 1,700 | false | false | false | false | false | false | false | false | true | false | 5,189 |
388C | Fox Ciel is playing a card game with her friend Fox Jiro. There are _n_ piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty. Suppose Ciel and Jiro play optimally, what is the score of the game? Input The first line contain an integer _n_ (1u2009≤u2009_n_u2009≤u2009100). Each of the next _n_ lines contains a description of the pile: the first integer in the line is _s__i_ (1u2009≤u2009_s__i_u2009≤u2009100) — the number of cards in the _i_-th pile; then follow _s__i_ positive integers _c_1, _c_2, ..., _c__k_, ..., _c__s__i_ (1u2009≤u2009_c__k_u2009≤u20091000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. Output Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. Examples Input 1 9 2 8 6 5 9 4 7 1 3 Input 3 3 1 3 2 3 5 4 6 2 8 7 Input 3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 1000 1000 1000 1000 1000 Note In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | 2,000 | false | true | false | false | false | false | false | false | true | false | 8,275 |
626C | Problem - 626C - Codeforces =============== xa0 ]( "23515") tower by stacking pieces lengthwise on top of each other. _n_ of the students use pieces made of two blocks and _m_ of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. Input The first line of the input contains two space-separated integers _n_ and _m_ (0u2009≤u2009_n_,u2009_m_u2009≤u20091u2009000u2009000, _n_u2009+u2009_m_u2009>u20090)xa0— the number of students using two-block pieces and the number of students using three-block pieces, respectively. Output Print a single integer, denoting the minimum possible height of the tallest tower. Examples Input 1 3 Output 9 Input 3 2 Output 8 Input 5 0 Output 10 Note In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks. In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks. | 1,600 | true | true | false | false | false | false | true | false | false | false | 7,326 |
1255B | Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it. For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i e j$$$) that the person $$$j$$$ can open the fridge $$$i$$$. For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person). Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost. Input Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 le T le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 le n le 1000$$$, $$$1 le m le n$$$)xa0— the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^4$$$)xa0— weights of all fridges. | 1,100 | false | false | true | false | false | false | false | false | false | true | 4,409 |
630N | Problem - 630N - Codeforces =============== xa0 ]( "Experimental Educational Round: VolBIT Formulas Blitz") — the coefficients of _ax_2u2009+u2009_bx_u2009+u2009_c_u2009=u20090 equation. Output In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10u2009-u20096. Examples Input 1 30 200 Output -10.000000000000000 -20.000000000000000 | 1,300 | true | false | false | false | false | false | false | false | false | false | 7,291 |
536D | Tavas lives in Kansas. Kansas has _n_ cities numbered from 1 to _n_ connected with _m_ bidirectional roads. We can travel from any city to any other city via these roads. Kansas is as strange as Tavas. So there may be a road between a city and itself or more than one road between two cities. Tavas invented a game and called it "Dashti". He wants to play Dashti with his girlfriends, Nafas. In this game, they assign an arbitrary integer value to each city of Kansas. The value of _i_-th city equals to _p__i_. During the game, Tavas is in city _s_ and Nafas is in city _t_. They play in turn and Tavas goes first. A player in his/her turn, must choose a non-negative integer _x_ and his/her score increases by the sum of values of all cities with (shortest) distance no more than _x_ from his/her city. Each city may be used once, or in the other words, after first time a player gets score from a city, city score becomes zero. There is an additional rule: the player must choose _x_ such that he/she gets the point of at least one city that was not used before. Note that city may initially have value 0, such city isn't considered as been used at the beginning of the game, i. e. each player may use it to fullfill this rule. The game ends when nobody can make a move. A player's score is the sum of the points he/she earned during the game. The winner is the player with greater score, or there is a draw if players score the same value. Both players start game with zero points. If Tavas wins, he'll break his girlfriend's heart, and if Nafas wins, Tavas will cry. But if their scores are equal, they'll be happy and Tavas will give Nafas flowers. They're not too emotional after all, so they'll play optimally. Your task is to tell Tavas what's going to happen after the game ends. Input The first line of input contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u20092000, _n_u2009-u20091u2009≤u2009_m_u2009≤u2009105). The second line of input contains two integers _s_ and _t_ (1u2009≤u2009_s_,u2009_t_u2009≤u2009_n_, _s_u2009≠u2009_t_). The next line contains _n_ integers _p_1,u2009_p_2,u2009...,u2009_p__n_ separated by spaces (_p__i_u2009≤u2009109). The next _m_ lines contain the roads. Each line contains three integers _v_,u2009_u_,u2009_w_ and it means that there's an road with length _w_ between cities _v_ and _u_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_ and 0u2009≤u2009_w_u2009≤u2009109). The road may lead from the city to itself, there may be several roads between each pair of cities. | 2,900 | false | false | false | true | false | false | false | false | false | false | 7,690 |
1515A | Phoenix has collected $$$n$$$ pieces of gold, and he wants to weigh them together so he can feel rich. The $$$i$$$-th piece of gold has weight $$$w_i$$$. All weights are distinct. He will put his $$$n$$$ pieces of gold on a weight scale, one piece at a time. The scale has an unusual defect: if the total weight on it is exactly $$$x$$$, it will explode. Can he put all $$$n$$$ gold pieces onto the scale in some order, without the scale exploding during the process? If so, help him find some possible order. Formally, rearrange the array $$$w$$$ so that for each $$$i$$$ $$$(1 le i le n)$$$, $$$sumlimits_{j = 1}^{i}w_j e x$$$. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 100$$$; $$$1 le x le 10^4$$$)xa0— the number of gold pieces that Phoenix has and the weight to avoid, respectively. The second line of each test case contains $$$n$$$ space-separated integers $$$(1 le w_i le 100)$$$xa0— the weights of the gold pieces. It is guaranteed that the weights are pairwise distinct. Output For each test case, if Phoenix cannot place all $$$n$$$ pieces without the scale exploding, print NO. Otherwise, print YES followed by the rearranged array $$$w$$$. If there are multiple solutions, print any. Example Input 3 3 2 3 2 1 5 3 1 2 3 4 8 1 5 5 Output YES 3 2 1 YES 8 1 2 3 4 NO Note In the first test case, Phoenix puts the gold piece with weight $$$3$$$ on the scale first, then the piece with weight $$$2$$$, and finally the piece with weight $$$1$$$. The total weight on the scale is $$$3$$$, then $$$5$$$, then $$$6$$$. The scale does not explode because the total weight on the scale is never $$$2$$$. In the second test case, the total weight on the scale is $$$8$$$, $$$9$$$, $$$11$$$, $$$14$$$, then $$$18$$$. It is never $$$3$$$. In the third test case, Phoenix must put the gold piece with weight $$$5$$$ on the scale, and the scale will always explode. | 800 | true | true | false | false | false | true | false | false | false | false | 3,091 |
1974A | Little Rosie has a phone with a desktop (or launcher, as it is also called). The desktop can consist of several screens. Each screen is represented as a grid of size $$$5 imes 3$$$, i.e., five rows and three columns. There are $$$x$$$ applications with an icon size of $$$1 imes 1$$$ cells; such an icon occupies only one cell of the screen. There are also $$$y$$$ applications with an icon size of $$$2 imes 2$$$ cells; such an icon occupies a square of $$$4$$$ cells on the screen. Each cell of each screen can be occupied by no more than one icon. Rosie wants to place the application icons on the minimum number of screens. Help her find the minimum number of screens needed. Input The first line of the input contains $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first and only line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$0 leq x, y leq 99$$$)xa0— the number of applications with a $$$1 imes 1$$$ icon and the number of applications with a $$$2 imes 2$$$ icon, respectively. Output For each test case, output the minimal number of required screens on a separate line. Example Input 11 1 1 7 2 12 4 0 3 1 0 8 1 0 0 2 0 15 0 8 2 0 9 Output 1 1 2 2 1 1 0 1 1 2 5 Note The solution for the first test case can look as follows: Blue squares represent empty spaces for icons, green squares represent $$$1 imes 1$$$ icons, red squares represent $$$2 imes 2$$$ icons The solution for the third test case can look as follows: | 800 | true | true | false | false | false | false | false | false | false | false | 449 |
1617C | Paprika loves permutations. She has an array $$$a_1, a_2, dots, a_n$$$. She wants to make the array a permutation of integers $$$1$$$ to $$$n$$$. In order to achieve this goal, she can perform operations on the array. In each operation she can choose two integers $$$i$$$ ($$$1 le i le n$$$) and $$$x$$$ ($$$x > 0$$$), then perform $$$a_i := a_i bmod x$$$ (that is, replace $$$a_i$$$ by the remainder of $$$a_i$$$ divided by $$$x$$$). In different operations, the chosen $$$i$$$ and $$$x$$$ can be different. Determine the minimum number of operations needed to make the array a permutation of integers $$$1$$$ to $$$n$$$. If it is impossible, output $$$-1$$$. A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$. ($$$1 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the minimum number of operations needed to make the array a permutation of integers $$$1$$$ to $$$n$$$, or $$$-1$$$ if it is impossible. Note For the first test, the only possible sequence of operations which minimizes the number of operations is: Choose $$$i=2$$$, $$$x=5$$$. Perform $$$a_2 := a_2 bmod 5 = 2$$$. For the second test, it is impossible to obtain a permutation of integers from $$$1$$$ to $$$n$$$. | 1,300 | true | true | false | false | false | false | false | true | true | false | 2,546 |
1657C | You are given a bracket sequence consisting of $$$n$$$ characters '(' and/or )'. You perform several operations with it. During one operation, you choose the shortest prefix of this string (some amount of first characters of the string) that is good and remove it from the string. The prefix is considered good if one of the following two conditions is satisfied: this prefix is a regular bracket sequence; this prefix is a palindrome of length at least two. A bracket sequence is called regular if it is possible to obtain a correct arithmetic expression by inserting characters '+' and '1' into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not. The bracket sequence is called palindrome if it reads the same back and forth. For example, the bracket sequences )), (( and )(() are palindromes, while bracket sequences (), )( and ))( are not palindromes. You stop performing the operations when it's not possible to find a good prefix. Your task is to find the number of operations you will perform on the given string and the number of remaining characters in the string. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$) — the length of the bracket sequence. The second line of the test case contains $$$n$$$ characters '(' and/or ')' — the bracket sequence itself. It is guaranteed that the sum of $$$n$$$ over all test cases do not exceed $$$5 cdot 10^5$$$ ($$$sum n le 5 cdot 10^5$$$). Output For each test case, print two integers $$$c$$$ and $$$r$$$ — the number of operations you will perform on the given bracket sequence and the number of characters that remain in the string after performing all operations. | 1,200 | false | true | true | false | false | false | false | false | false | false | 2,343 |
1744E1 | Problem - 1744E1 - 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 math number theory *1500 No tag edit access → Contest materials ") Editorial") time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output This is an easy version of the problem. The only difference between an easy and a hard version is the constraints on $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$. You are given $$$4$$$ positive integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ with $$$a < c$$$ and $$$b < d$$$. Find any pair of numbers $$$x$$$ and $$$y$$$ that satisfies the following conditions: $$$a < x leq c$$$, $$$b < y leq d$$$, $$$x cdot y$$$ is divisible by $$$a cdot b$$$. Note that required $$$x$$$ and $$$y$$$ may not exist. Input The first line of the input contains a single integer $$$t$$$ $$$(1 leq t leq 10$$$), the number of test cases. The descriptions of the test cases follow. The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$1 leq a < c leq 10^5$$$, $$$1 leq b < d leq 10^5$$$). Output For each test case print a pair of numbers $$$a < x leq c$$$ and $$$b < y leq d$$$ such that $$$x cdot y$$$ is divisible by $$$a cdot b$$$. If there are multiple answers, print any of them. If there is no such pair of numbers, then print -1 -1. Example Input 5 1 1 2 2 3 4 5 7 8 9 15 18 12 21 14 24 36 60 48 66 Output 2 2 4 6 12 12 -1 -1 -1 -1 | 1,500 | true | false | false | false | false | false | true | false | false | false | 1,813 |
1421D | Lindsey Buckingham told Stevie Nicks $$$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $$$(0, 0)$$$ to $$$(1, 1)$$$ will take the exact same cost as going from $$$(-2, -1)$$$ to $$$(-1, 0)$$$. The costs are given in the input in the order $$$c_1$$$, $$$c_2$$$, $$$c_3$$$, $$$c_4$$$, $$$c_5$$$, $$$c_6$$$ as in the picture below. Print the smallest cost of a path from the origin which has coordinates $$$(0, 0)$$$ to the given cell. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^{4}$$$). Description of the test cases follows. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$-10^{9} le x, y le 10^{9}$$$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $$$c_1$$$, $$$c_2$$$, $$$c_3$$$, $$$c_4$$$, $$$c_5$$$, $$$c_6$$$ ($$$1 le c_1, c_2, c_3, c_4, c_5, c_6 le 10^{9}$$$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). Output For each testcase output the smallest cost of a path from the origin to the given cell. Example Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 Note The picture below shows the solution for the first sample. The cost $$$18$$$ is reached by taking $$$c_3$$$ 3 times and $$$c_2$$$ once, amounting to $$$5+5+5+3=18$$$. | 1,900 | true | true | true | false | false | true | true | false | false | false | 3,571 |
121A | Problem - 121A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *1100 No tag edit access → Contest materials be the minimum lucky number which is larger than or equals _x_. Petya is interested what is the value of the expression _next_(_l_)u2009+u2009_next_(_l_u2009+u20091)u2009+u2009...u2009+u2009_next_(_r_u2009-u20091)u2009+u2009_next_(_r_). Help him solve this problem. Input The single line contains two integers _l_ and _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009109) — the left and right interval limits. Output In the single line print the only number — the sum _next_(_l_)u2009+u2009_next_(_l_u2009+u20091)u2009+u2009...u2009+u2009_next_(_r_u2009-u20091)u2009+u2009_next_(_r_). 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. Examples Input 2 7 Output 33 Input 7 7 Output 7 Note In the first sample: _next_(2)u2009+u2009_next_(3)u2009+u2009_next_(4)u2009+u2009_next_(5)u2009+u2009_next_(6)u2009+u2009_next_(7)u2009=u20094u2009+u20094u2009+u20094u2009+u20097u2009+u20097u2009+u20097u2009=u200933 In the second sample: _next_(7)u2009=u20097 | 1,100 | false | false | true | false | false | false | false | false | false | false | 9,396 |
1062E | The company $$$X$$$ has $$$n$$$ employees numbered from $$$1$$$ through $$$n$$$. Each employee $$$u$$$ has a direct boss $$$p_u$$$ ($$$1 le p_u le n$$$), except for the employee $$$1$$$ who has no boss. It is guaranteed, that values $$$p_i$$$ form a tree. Employee $$$u$$$ is said to be in charge of employee $$$v$$$ if $$$u$$$ is the direct boss of $$$v$$$ or there is an employee $$$w$$$ such that $$$w$$$ is in charge of $$$v$$$ and $$$u$$$ is the direct boss of $$$w$$$. Also, any employee is considered to be in charge of himself. In addition, for each employee $$$u$$$ we define it's level $$$lv(u)$$$ as follow: $$$lv(1)=0$$$ $$$lv(u)=lv(p_u)+1$$$ for $$$u eq 1$$$ In the near future, there are $$$q$$$ possible plans for the company to operate. The $$$i$$$-th plan consists of two integers $$$l_i$$$ and $$$r_i$$$, meaning that all the employees in the range $$$[l_i, r_i]$$$, and only they, are involved in this plan. To operate the plan smoothly, there must be a project manager who is an employee in charge of all the involved employees. To be precise, if an employee $$$u$$$ is chosen as the project manager for the $$$i$$$-th plan then for every employee $$$v in [l_i, r_i]$$$, $$$u$$$ must be in charge of $$$v$$$. Note, that $$$u$$$ is not necessary in the range $$$[l_i, r_i]$$$. Also, $$$u$$$ is always chosen in such a way that $$$lv(u)$$$ is as large as possible (the higher the level is, the lower the salary that the company has to pay the employee). Before any plan is operated, the company has JATC take a look at their plans. After a glance, he tells the company that for every plan, it's possible to reduce the number of the involved employees exactly by one without affecting the plan. Being greedy, the company asks JATC which employee they should kick out of the plan so that the level of the project manager required is as large as possible. JATC has already figured out the answer and challenges you to do the same. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 100,000$$$, $$$1 le q le 100,000$$$)xa0— the number of employees and the number of plans, respectively. The second line contains $$$n-1$$$ integers $$$p_2, p_3, dots, p_n$$$ ($$$1 le p_i le n$$$) meaning $$$p_i$$$ is the direct boss of employee $$$i$$$. It is guaranteed, that values $$$p_i$$$ form a directed tree with the root of $$$1$$$. Each of the following $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i<r_i le n$$$)xa0— the range of the employees, involved in the corresponding plan. Output Print $$$q$$$ lines, each containing two integersxa0— the number of the employee which should be kicked from the corresponding plan and the maximum possible level of the project manager in that case. If there are more than one way to choose that employee, print any of them. Example Input 11 5 1 1 3 3 3 4 2 7 7 6 4 6 4 8 1 11 9 11 8 11 Note In the example: In the first query, we can choose whether $$$4$$$ or $$$5$$$ or $$$6$$$ and the project manager will be $$$3$$$. In the second query, if we choose any employee other than the employee $$$8$$$, the project manager will be $$$1$$$. If we choose $$$8$$$, the project manager will be $$$3$$$. Since $$$lv(3)=1 > lv(1)=0$$$, choosing $$$8$$$ is the best strategy. In the third query, no matter how we choose the employee, the project manager will always be $$$1$$$. In the fourth query, if we choose $$$9$$$ or $$$10$$$ then the project manager will be $$$3$$$. If we choose $$$11$$$ then the project manager will be $$$7$$$. Since $$$lv(7)=3>lv(3)=1$$$, we choose $$$11$$$ as the answer. | 2,300 | false | true | false | false | true | false | false | true | false | false | 5,398 |
2027C | You're given an array $$$a$$$ initially containing $$$n$$$ integers. In one operation, you must do the following: Choose a position $$$i$$$ such that $$$1 < i le a$$$ and $$$a_i = a + 1 - i$$$, where $$$a$$$ is the current size of the array. Append $$$i - 1$$$ zeros onto the end of $$$a$$$. After performing this operation as many times as you want, what is the maximum possible length of the array $$$a$$$? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. The first line of each test case contains $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^{12}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Note In the first test case, we can first choose $$$i = 4$$$, since $$$a_4 = 5 + 1 - 4 = 2$$$. After this, the array becomes $$$[2, 4, 6, 2, 5, 0, 0, 0]$$$. We can then choose $$$i = 3$$$ since $$$a_3 = 8 + 1 - 3 = 6$$$. After this, the array becomes $$$[2, 4, 6, 2, 5, 0, 0, 0, 0, 0]$$$, which has a length of $$$10$$$. It can be shown that no sequence of operations will make the final array longer. In the second test case, we can choose $$$i=2$$$, then $$$i=3$$$, then $$$i=4$$$. The final array will be $$$[5, 4, 4, 5, 1, 0, 0, 0, 0, 0, 0]$$$, with a length of $$$11$$$. | 1,500 | false | true | false | true | true | false | true | false | false | true | 108 |
848A | From beginning till end, this message has been waiting to be conveyed. For a given unordered multiset of _n_ lowercase English letters ("multi" means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation _n_u2009-u20091 times: Remove any two elements _s_ and _t_ from the set, and add their concatenation _s_u2009+u2009_t_ to the set. The cost of such operation is defined to be , where _f_(_s_,u2009_c_) denotes the number of times character _c_ appears in string _s_. Given a non-negative integer _k_, construct any valid non-empty set of no more than 100u2009000 letters, such that the minimum accumulative cost of the whole process is exactly _k_. It can be shown that a solution always exists. Input The first and only line of input contains a non-negative integer _k_ (0u2009≤u2009_k_u2009≤u2009100u2009000) — the required minimum cost. Output Output a non-empty string of no more than 100u2009000 lowercase English letters — any multiset satisfying the requirements, concatenated to be a string. Note that the printed string doesn't need to be the final concatenated string. It only needs to represent an unordered multiset of letters. Note For the multiset {'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'}, one of the ways to complete the process is as follows: {"ab", "a", "b", "a", "b", "a", "b"}, with a cost of 0; {"aba", "b", "a", "b", "a", "b"}, with a cost of 1; {"abab", "a", "b", "a", "b"}, with a cost of 1; {"abab", "ab", "a", "b"}, with a cost of 0; {"abab", "aba", "b"}, with a cost of 1; {"abab", "abab"}, with a cost of 1; {"abababab"}, with a cost of 8. The total cost is 12, and it can be proved to be the minimum cost of the process. | 1,600 | false | false | false | false | false | true | false | false | false | false | 6,358 |
1838B | You are given a permutation $$$p$$$ of size $$$n$$$. You want to minimize the number of subarrays of $$$p$$$ that are permutations. In order to do so, you must perform the following operation exactly once: Select integers $$$i$$$, $$$j$$$, where $$$1 le i, j le n$$$, then Swap $$$p_i$$$ and $$$p_j$$$. For example, if $$$p = [5, 1, 4, 2, 3]$$$ and we choose $$$i = 2$$$, $$$j = 3$$$, the resulting array will be $$$[5, 4, 1, 2, 3]$$$. If instead we choose $$$i = j = 5$$$, the resulting array will be $$$[5, 1, 4, 2, 3]$$$. Which choice of $$$i$$$ and $$$j$$$ will minimize the number of subarrays that are permutations? A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). An array $$$a$$$ is a subarray of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 2cdot 10^5$$$)xa0— the size of the permutation. The next line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots p_n$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ are distinct)xa0— the elements of the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output two integers $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$) xa0— the indices to swap in $$$p$$$. If there are multiple solutions, print any of them. Note For the first test case, there are four possible arrays after the swap: If we swap $$$p_1$$$ and $$$p_2$$$, we get the array $$$[2, 1, 3]$$$, which has 3 subarrays that are permutations ($$$[1]$$$, $$$[2, 1]$$$, $$$[2, 1, 3]$$$). If we swap $$$p_1$$$ and $$$p_3$$$, we get the array $$$[3, 2, 1]$$$, which has 3 subarrays that are permutations ($$$[1]$$$, $$$[2, 1]$$$, $$$[3, 2, 1]$$$). If we swap $$$p_2$$$ and $$$p_3$$$, we get the array $$$[1, 3, 2]$$$, which has 2 subarrays that are permutations ($$$[1]$$$, $$$[1, 3, 2]$$$). If we swap any element with itself, we get the array $$$[1, 2, 3]$$$, which has 3 subarrays that are permutations ($$$[1]$$$, $$$[1, 2]$$$, $$$[1, 2, 3]$$$). So the best swap to make is positions $$$2$$$ and $$$3$$$. For the third sample case, after we swap elements at positions $$$2$$$ and $$$5$$$, the resulting array is $$$[1, 4, 2, 5, 3]$$$. The only subarrays that are permutations are $$$[1]$$$ and $$$[1, 4, 2, 5, 3]$$$. We can show that this is minimal. | 1,100 | true | false | false | false | false | true | false | false | false | false | 1,263 |
121E | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has an array consisting of _n_ numbers. He wants to perform _m_ operations of two types: add _l_ _r_ _d_ — add an integer _d_ to all elements whose indexes belong to the interval from _l_ to _r_, inclusive (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_,u20091u2009≤u2009_d_u2009≤u2009104); count _l_ _r_ — find and print on the screen how many lucky numbers there are among elements with indexes that belong to the interval from _l_ to _r_ inclusive (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_). Each lucky number should be counted as many times as it appears in the interval. Petya has a list of all operations. The operations are such that after all additions the array won't have numbers that would exceed 104. Help Petya write a program that would perform these operations. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of numbers in the array and the number of operations correspondingly. The second line contains _n_ positive integers, none of which exceeds 104 — those are the array numbers. Next _m_ lines contain operations, one per line. They correspond to the description given in the statement. It is guaranteed that after all operations are fulfilled each number in the array will not exceed 104. Output For each operation of the second type print the single number on the single line — the number of lucky numbers in the corresponding interval. Examples Input 3 6 2 3 4 count 1 3 count 1 2 add 1 3 2 count 1 3 add 2 3 3 count 1 3 Input 4 5 4 4 4 4 count 1 4 add 1 4 3 count 1 4 add 2 3 40 count 1 4 Note In the first sample after the first addition the array will look in the following manner: 4 5 6 After the second addition: 4 8 9 The second sample after the first addition: 7 7 7 7 After the second addition: 7 47 47 7 | 2,400 | false | false | false | false | true | false | false | false | false | false | 9,392 |
1459B | Problem - 1459B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp math *1300 No tag edit access → Contest materials ") xa0— the number of steps the robot makes. Output Print a single integerxa0— the number of different possible locations after exactly $$$n$$$ steps. Examples Input 1 Output 4 Input 2 Output 4 Input 3 Output 12 Note In the first sample case, the robot will end up 1 meter north, south, west, or east depending on its initial direction. In the second sample case, the robot will always end up $$$sqrt{2}$$$ meters north-west, north-east, south-west, or south-east. | 1,300 | true | false | false | true | false | false | false | false | false | false | 3,387 |
42C | Problem - 42C - Codeforces =============== xa0 if the safe is secure, that is it's impossible to crack it. Otherwise, output should contain the sequence of operations (one operations per line) leading to unlocking the safe. You don't have to minimize the number of operations, but it should not exceed 1000. To make things clear, assume numbers stand on positions 1 through 4. Each operation is encoded by two symbols. If the following operation is dividing then first symbol is '/'; otherwise it's '+' (addition). The second symbol is the position of the first number in pair in consecutive order. (see samples for clarification). If there are several solutions, output any of them. Examples Input 1 1 1 1 Output Input 1 2 4 2 Output /2 /3 Input 3 3 1 1 Output +1 /1 /1 Input 2 1 2 4 Output /3 /4 | 2,200 | false | false | false | false | false | true | true | false | false | false | 9,777 |
1082F | Polycarp's phone book contains $$$n$$$ phone numbers, each of them is described by $$$s_i$$$ — the number itself and $$$m_i$$$ — the number of times Polycarp dials it in daily. Polycarp has just bought a brand new phone with an amazing speed dial feature! More precisely, $$$k$$$ buttons on it can have a number assigned to it (not necessary from the phone book). To enter some number Polycarp can press one of these $$$k$$$ buttons and then finish the number using usual digit buttons (entering a number with only digit buttons is also possible). Speed dial button can only be used when no digits are entered. No button can have its number reassigned. What is the minimal total number of digit number presses Polycarp can achieve after he assigns numbers to speed dial buttons and enters each of the numbers from his phone book the given number of times in an optimal way? Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 500$$$, $$$1 le k le 10$$$) — the amount of numbers in Polycarp's phone book and the number of speed dial buttons his new phone has. The $$$i$$$-th of the next $$$n$$$ lines contain a string $$$s_i$$$ and an integer $$$m_i$$$ $$$(1 le m_i le 500)$$$, where $$$s_i$$$ is a non-empty string of digits from $$$0$$$ to $$$9$$$ inclusive (the $$$i$$$-th number), and $$$m_i$$$ is the amount of times it will be dialed, respectively. It is guaranteed that the total length of all phone numbers will not exceed $$$500$$$. Output Print a single integer — the minimal total number of digit number presses Polycarp can achieve after he assigns numbers to speed dial buttons and enters each of the numbers from his phone book the given number of times in an optimal way. Examples Input 3 1 0001 5 001 4 01 1 Input 3 1 0001 5 001 6 01 1 Note The only speed dial button in the first example should have "0001" on it. The total number of digit button presses will be $$$0 cdot 5$$$ for the first number + $$$3 cdot 4$$$ for the second + $$$2 cdot 1$$$ for the third. $$$14$$$ in total. The only speed dial button in the second example should have "00" on it. The total number of digit button presses will be $$$2 cdot 5$$$ for the first number + $$$1 cdot 6$$$ for the second + $$$2 cdot 1$$$ for the third. $$$18$$$ in total. | 2,800 | false | false | false | true | false | false | false | false | false | false | 5,310 |
508D | While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of _n_u2009+u20092 characters. She has written all the possible _n_ three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the password out. Each three-letter substring was written the number of times it occurred in the password. Thus, Tanya ended up with _n_ pieces of paper. Then Tanya realized that dad will be upset to learn about her game and decided to restore the password or at least any string corresponding to the final set of three-letter strings. You have to help her in this difficult task. We know that dad's password consisted of lowercase and uppercase letters of the Latin alphabet and digits. Uppercase and lowercase letters of the Latin alphabet are considered distinct. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20092·105), the number of three-letter substrings Tanya got. Next _n_ lines contain three letters each, forming the substring of dad's password. Each character in the input is a lowercase or uppercase Latin letter or a digit. Output If Tanya made a mistake somewhere during the game and the strings that correspond to the given set of substrings don't exist, print "NO". If it is possible to restore the string that corresponds to given set of substrings, print "YES", and then print any suitable password option. Examples Input 7 aaa aaa aaa aaa aaa aaa aaa | 2,500 | false | false | false | false | false | false | false | false | false | true | 7,808 |
382B | Problem - 382B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search math *2000 No tag edit access → Contest materials Editorial") and Alexander took integer _с_. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: _c_u2009=u2009_c_u2009-u20091. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if _b_u2009≥u2009_x_, perform the assignment _b_u2009=u2009_b_u2009-u2009_x_, if _b_u2009<u2009_x_, then perform two consecutive assignments _a_u2009=u2009_a_u2009-u20091;xa0_b_u2009=u2009_w_u2009-u2009(_x_u2009-u2009_b_). You've got numbers _a_,u2009_b_,u2009_w_,u2009_x_,u2009_c_. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if _c_u2009≤u2009_a_. Input The first line contains integers _a_,u2009_b_,u2009_w_,u2009_x_,u2009_c_ (1u2009≤u2009_a_u2009≤u20092·109,u20091u2009≤u2009_w_u2009≤u20091000,u20090u2009≤u2009_b_u2009<u2009_w_,u20090u2009<u2009_x_u2009<u2009_w_,u20091u2009≤u2009_c_u2009≤u20092·109). Output Print a single integer — the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem's limits. Examples Input 4 2 3 1 6 Output 2 Input 4 2 3 1 7 Output 4 Input 1 2 3 2 6 Output 13 Input 1 1 2 1 1 Output 0 | 2,000 | true | false | false | false | false | false | false | true | false | false | 8,302 |
25A | Problem - 25A - 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 *1300 No tag edit access → Contest materials ") — amount of numbers in the task. The second line contains _n_ space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness. Output Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order. Examples Input 5 2 4 7 8 10 Output 3 Input 4 1 2 1 1 Output 2 | 1,300 | false | false | false | false | false | false | true | false | false | false | 9,873 |
2018F3 | This is the hard version of the problem. In the three versions, the constraints on $$$n$$$ and the time limit are different. You can make hacks only if all the versions of the problem are solved. This is the statement of Problem D1B: There are $$$n$$$ cities in a row, numbered $$$1, 2, ldots, n$$$ left to right. At time $$$1$$$, you conquer exactly one city, called the starting city. At time $$$2, 3, ldots, n$$$, you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each $$$i$$$, you conquer city $$$i$$$ at a time no later than $$$a_i$$$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win? For each $$$0 leq k leq n$$$, count the number of arrays of positive integers $$$a_1, a_2, ldots, a_n$$$ such that $$$1 leq a_i leq n$$$ for each $$$1 leq i leq n$$$; the answer to Problem D1B is $$$k$$$. The answer can be very large, so you have to calculate it modulo a given prime $$$p$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 3000$$$). The description of the test cases follows. The only line of each test case contains two integers $$$n$$$, $$$p$$$ ($$$1 le n le 3000$$$, $$$10^8 leq p leq 10^9$$$, $$$p$$$ is prime)xa0— the number of cities and the modulo. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. Output For each test case, output $$$n+1$$$ integers: the $$$i$$$-th integer should be the number of arrays that satisfy the conditions for $$$k = i-1$$$. Example Input 11 1 998244353 2 998244353 3 998244353 4 998244353 5 998244353 6 998244353 7 998244353 8 998244353 9 998244353 10 102275857 10 999662017 Output 0 1 1 2 1 14 7 4 2 183 34 19 16 4 2624 209 112 120 48 12 42605 1546 793 992 468 216 36 785910 13327 6556 9190 4672 2880 864 144 16382863 130922 61939 94992 50100 36960 14256 4608 576 382823936 1441729 657784 1086596 583344 488700 216000 96480 23040 2880 20300780 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 944100756 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 Note In the first test case, arrays with $$$1$$$ good starting city: $$$[1]$$$. In the second test case, arrays with $$$0$$$ good starting cities: $$$[1, 1]$$$; arrays with $$$1$$$ good starting city: $$$[1, 2]$$$, $$$[2, 1]$$$; arrays with $$$2$$$ good starting cities: $$$[2, 2]$$$. In the third test case, arrays with $$$0$$$ good starting cities: $$$[1, 1, 1]$$$, $$$[1, 1, 2]$$$, $$$[1, 1, 3]$$$, $$$[1, 2, 1]$$$, $$$[1, 2, 2]$$$, $$$[1, 3, 1]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 1]$$$, $$$[2, 1, 2]$$$, $$$[2, 2, 1]$$$, $$$[2, 2, 2]$$$, $$$[2, 3, 1]$$$, $$$[2, 3, 2]$$$, $$$[3, 1, 1]$$$; arrays with $$$1$$$ good starting city: $$$[1, 2, 3]$$$, $$$[1, 3, 3]$$$, $$$[2, 1, 3]$$$, $$$[3, 1, 2]$$$, $$$[3, 1, 3]$$$, $$$[3, 2, 1]$$$, $$$[3, 3, 1]$$$; arrays with $$$2$$$ good starting cities: $$$[2, 2, 3]$$$, $$$[2, 3, 3]$$$, $$$[3, 2, 2]$$$, $$$[3, 3, 2]$$$; arrays with $$$3$$$ good starting cities: $$$[3, 2, 3]$$$, $$$[3, 3, 3]$$$. | 3,100 | true | true | false | true | false | false | false | false | false | false | 155 |
1436E | Problem - 1436E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search data structures two pointers *2400 No tag edit access → Contest materials based on Final Andersen Programming Contest 2020") . Editorial") elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself. Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays! Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of the array. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the elements of the array. Output Print a single integerxa0— the MEX of MEXes of all subarrays. Examples Input 3 1 3 2 Output 3 Input 5 1 4 3 1 2 Output 6 | 2,400 | false | false | false | false | true | false | false | true | false | false | 3,487 |
749A | Problem - 749A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation math number theory *800 No tag edit access → Contest materials . Output The first line of the output contains a single integer _k_xa0— maximum possible number of primes in representation. The second line should contain _k_ primes with their sum equal to _n_. You can print them in any order. If there are several optimal solution, print any of them. Examples Input 5 Output 2 2 3 Input 6 Output 3 2 2 2 | 800 | true | true | true | false | false | false | false | false | false | false | 6,798 |
429E | Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw _n_ distinct segments [_l__i_,u2009_r__i_] on the _OX_ axis. He can draw each segment with either red or blue. The drawing is good if and only if the following requirement is met: for each point _x_ of the _OX_ axis consider all the segments that contains point _x_; suppose, that _r__x_ red segments and _b__x_ blue segments contain point _x_; for each point _x_ inequality _r__x_u2009-u2009_b__x_u2009≤u20091 must be satisfied. A segment [_l_,u2009_r_] contains a point _x_ if and only if _l_u2009≤u2009_x_u2009≤u2009_r_. Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. Input The first line of input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of segments. The _i_-th of the next _n_ lines contains two integers _l__i_ and _r__i_ (0u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009109) — the borders of the _i_-th segment. It's guaranteed that all the segments are distinct. Output If there is no good drawing for a given test, output a single integer -1. Otherwise output _n_ integers; each integer must be 0 or 1. The _i_-th number denotes the color of the _i_-th segment (0 is red and 1 is blue). If there are multiple good drawings you can output any of them. Examples Input 6 1 5 1 3 3 5 2 10 11 11 12 12 | 3,000 | false | false | false | false | false | false | false | false | false | true | 8,122 |
1883E | You are given an array of integers $$$a_1, a_2, ldots, a_n$$$. You need to make it non-decreasing with the minimum number of operations. In one operation, you do the following: Choose an index $$$1 leq i leq n$$$, Set $$$a_i = a_i cdot 2$$$. An array $$$b_1, b_2, ldots, b_n$$$ is non-decreasing if $$$b_i leq b_{i+1}$$$ for all $$$1 leq i < n$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. This is followed by their description. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 10^5$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the minimum number of operations needed to make the array non-decreasing. Example Input 9 1 1 2 2 1 3 3 2 1 4 7 1 5 3 5 11 2 15 7 10 6 1 8 2 16 8 16 2 624323799 708290323 12 2 1 1 3 3 11 12 22 45 777 777 1500 12 12 11 10 9 8 7 6 5 4 3 2 1 Output 0 1 3 6 10 3 0 2 66 Note No operations are needed in the first test case. In the second test case, we need to choose $$$i = 2$$$, after which the array will be $$$[2, 2]$$$. In the third test case, we can apply the following operations: Choose $$$i = 3$$$, after which the array will be $$$[3, 2, 2]$$$, Choose $$$i = 3$$$, after which the array will be $$$[3, 2, 4]$$$, Choose $$$i = 2$$$, after which the array will be $$$[3, 4, 4]$$$. | 1,700 | false | true | false | false | false | false | false | false | false | false | 987 |
1814C | Suppose you have $$$n$$$ boxes. The $$$i$$$-th box contains infinitely many balls of color $$$i$$$. Sometimes you need to get a ball with some specific color; but you're too lazy to do it yourself. You have bought two robots to retrieve the balls for you. Now you have to program them. In order to program the robots, you have to construct two lists $$$[a_1, a_2, dots, a_k]$$$ and $$$[b_1, b_2, dots, b_{n-k}]$$$, where the list $$$a$$$ represents the boxes assigned to the first robot, and the list $$$b$$$ represents the boxes assigned to the second robot. Every integer from $$$1$$$ to $$$n$$$ must be present in exactly one of these lists. When you request a ball with color $$$x$$$, the robots work as follows. Each robot looks through the boxes that were assigned to that robot, in the order they appear in the list. The first robot spends $$$s_1$$$ seconds analyzing the contents of a box; the second robot spends $$$s_2$$$. As soon as one of the robots finds the box with balls of color $$$x$$$ (and analyzes its contents), the search ends. The search time is the number of seconds from the beginning of the search until one of the robots finishes analyzing the contents of the $$$x$$$-th box. If a robot analyzes the contents of all boxes assigned to it, it stops searching. For example, suppose $$$s_1 = 2$$$, $$$s_2 = 3$$$, $$$a = [4, 1, 5, 3, 7]$$$, $$$b = [2, 6]$$$. If you request a ball with color $$$3$$$, the following happens: initially, the first robot starts analyzing the box $$$4$$$, and the second robot starts analyzing the box $$$2$$$; at the end of the $$$2$$$-nd second, the first robot finishes analyzing the box $$$4$$$. It is not the box you need, so the robot continues with the box $$$1$$$; at the end of the $$$3$$$-rd second, the second robot finishes analyzing the box $$$2$$$. It is not the box you need, so the robot continues with the box $$$6$$$; at the end of the $$$4$$$-th second, the first robot finishes analyzing the box $$$1$$$. It is not the box you need, so the robot continues with the box $$$5$$$; at the end of the $$$6$$$-th second, the first robot finishes analyzing the box $$$5$$$. It is not the box you need, so the robot continues with the box $$$3$$$. At the same time, the second robot finishes analyzing the box $$$6$$$. It is not the box you need, and the second robot has analyzed all the boxes in its list, so that robot stops searching; at the end of the $$$8$$$-th second, the first robot finishes analyzing the box $$$3$$$. It is the box you need, so the search ends; so, the search time is $$$8$$$ seconds. You know that you are going to request a ball of color $$$1$$$ $$$r_1$$$ times, a ball of color $$$2$$$ $$$r_2$$$ times, and so on. You want to construct the lists $$$a$$$ and $$$b$$$ for the robots in such a way that the total search time over all requests is the minimum possible. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of two lines: the first line contains three integers $$$n$$$, $$$s_1$$$, $$$s_2$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le s_1, s_2 le 10$$$); the second line contains $$$n$$$ integers $$$r_1, r_2, dots, r_n$$$ ($$$1 le r_i le 10^6$$$). 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 two lines. The first line should contain the list $$$a$$$, the second line — the list $$$b$$$. Each list has to be printed as follows: first, print the number of elements in it, and then the elements themselves. If there are multiple answers, you may print any of them. | 1,500 | false | true | false | false | false | true | false | false | true | false | 1,385 |
1182D | You have given tree consist of $$$n$$$ vertices. Select a vertex as root vertex that satisfies the condition below. For all vertices $$$v_{1}$$$ and $$$v_{2}$$$, if $$$distance$$$($$$root$$$, $$$v_{1}$$$) $$$= distance$$$($$$root$$$, $$$v_{2})$$$ then $$$degree$$$($$$v_{1}$$$) $$$= degree$$$($$$v_{2}$$$), where $$$degree$$$ means the number of vertices connected to that vertex, and $$$distance$$$ means the number of edges between two vertices. Determine and find if there is such root vertex in the tree. If there are multiple answers, find any of them. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^{5}$$$)xa0— the number of vertices. Each of the next $$$n-1$$$ lines contains two integers $$$v_{i}$$$ and $$$u_{i}$$$ ($$$1 le v_{i} lt u_{i} le n$$$)xa0— it means there is an edge exist between $$$v_{i}$$$ and $$$u_{i}$$$. It is guaranteed that the graph forms tree. Output If there is such root vertex exists, print any of them. Otherwise, print $$$-1$$$. Examples Input 7 1 2 2 3 3 4 4 5 3 6 6 7 Input 6 1 3 2 3 3 4 4 5 4 6 Note This is the picture for the first example. $$$1$$$, $$$5$$$, $$$7$$$ also can be a valid answer. This is the picture for the second example. You can see that it's impossible to find such root vertex. | 2,400 | false | false | true | true | false | true | false | false | false | false | 4,805 |
1486C2 | The only difference between the easy and the hard version is the limit to the number of queries. This is an interactive problem. There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 20 queries. A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array. Input The first line contains a single integer $$$n$$$ $$$(2 leq n leq 10^5)$$$ — the number of elements in the array. Interaction You can ask queries by printing "? $$$l$$$ $$$r$$$" $$$(1 leq l < r leq n)$$$. The answer is the index of the second maximum of all elements $$$a_l, a_{l + 1}, ..., a_r$$$. Array $$$a$$$ is fixed beforehand and can't be changed in time of interaction. You can output the answer by printing "! $$$p$$$", where $$$p$$$ is the index of the maximum element in the array. You can ask no more than 20 queries. Printing the answer doesn't count as a query. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages Hacks To make a hack, use the following test format. In the first line output a single integer $$$n$$$ $$$(2 leq n leq 10^5)$$$. In the second line output a permutation of $$$n$$$ integers $$$1$$$ to $$$n$$$. The position of $$$n$$$ in the permutation is the position of the maximum Note In the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$. Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction. | 1,900 | false | false | false | false | false | false | false | true | false | false | 3,250 |
1166D | Given a positive integer $$$m$$$, we say that a sequence $$$x_1, x_2, dots, x_n$$$ of positive integers is $$$m$$$-cute if for every index $$$i$$$ such that $$$2 le i le n$$$ it holds that $$$x_i = x_{i - 1} + x_{i - 2} + dots + x_1 + r_i$$$ for some positive integer $$$r_i$$$ satisfying $$$1 le r_i le m$$$. You will be given $$$q$$$ queries consisting of three positive integers $$$a$$$, $$$b$$$ and $$$m$$$. For each query you must determine whether or not there exists an $$$m$$$-cute sequence whose first term is $$$a$$$ and whose last term is $$$b$$$. If such a sequence exists, you must additionally find an example of it. Input The first line contains an integer number $$$q$$$ ($$$1 le q le 10^3$$$)xa0— the number of queries. Each of the following $$$q$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$m$$$ ($$$1 le a, b, m le 10^{14}$$$, $$$a leq b$$$), describing a single query. Output For each query, if no $$$m$$$-cute sequence whose first term is $$$a$$$ and whose last term is $$$b$$$ exists, print $$$-1$$$. Otherwise print an integer $$$k$$$ ($$$1 le k leq 50$$$), followed by $$$k$$$ integers $$$x_1, x_2, dots, x_k$$$ ($$$1 le x_i le 10^{14}$$$). These integers must satisfy $$$x_1 = a$$$, $$$x_k = b$$$, and that the sequence $$$x_1, x_2, dots, x_k$$$ is $$$m$$$-cute. It can be shown that under the problem constraints, for each query either no $$$m$$$-cute sequence exists, or there exists one with at most $$$50$$$ terms. If there are multiple possible sequences, you may print any of them. Note Consider the sample. In the first query, the sequence $$$5, 6, 13, 26$$$ is valid since $$$6 = 5 + bf{color{blue} 1}$$$, $$$13 = 6 + 5 + {bfcolor{blue} 2}$$$ and $$$26 = 13 + 6 + 5 + {bfcolor{blue} 2}$$$ have the bold values all between $$$1$$$ and $$$2$$$, so the sequence is $$$2$$$-cute. Other valid sequences, such as $$$5, 7, 13, 26$$$ are also accepted. In the second query, the only possible $$$1$$$-cute sequence starting at $$$3$$$ is $$$3, 4, 8, 16, dots$$$, which does not contain $$$9$$$. | 2,200 | true | true | false | false | false | false | true | true | false | false | 4,886 |
701C | Sergei B., the young coach of Pokemons, has found the big house which consists of _n_ flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number _n_ is only connected with the flat number _n_u2009-u20091. There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once. Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit. Input The first line contains the integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000) — the number of flats in the house. The second line contains the row _s_ with the length _n_, it consists of uppercase and lowercase letters of English alphabet, the _i_-th letter equals the type of Pokemon, which is in the flat number _i_. Output Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house. Note In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2. In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6. In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6. | 1,500 | false | false | false | false | false | false | false | true | false | false | 7,018 |
96B | Problem - 96B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search bitmasks brute force *1300 No tag edit access → Contest materials Question A - D") time limit per test 2 seconds memory limit per test 256 megabytes input stdin output stdout Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not. One day Petya came across a positive integer _n_. Help him to find the least super lucky number which is not less than _n_. Input The only line contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u2009109). This number doesn't have leading zeroes. Output Output the least super lucky number that is more than or equal to _n_. 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. Examples Input 4500 Output 4747 Input 47 Output 47 | 1,300 | false | false | false | false | false | false | true | true | false | false | 9,502 |
219D | The country Treeland consists of _n_ cities, some pairs of them are connected with unidirectional roads. Overall there are _n_u2009-u20091 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one. The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city _a_ is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city _a_ to any other city. For that some roads may have to be inversed. Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country. Input The first input line contains integer _n_ (2u2009≤u2009_n_u2009≤u20092·105) — the number of cities in Treeland. Next _n_u2009-u20091 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers _s__i_,u2009_t__i_ (1u2009≤u2009_s__i_,u2009_t__i_u2009≤u2009_n_;xa0_s__i_u2009≠u2009_t__i_) — the numbers of cities, connected by that road. The _i_-th road is oriented from city _s__i_ to city _t__i_. You can consider cities in Treeland indexed from 1 to _n_. Output In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order. | 1,700 | false | false | false | true | false | false | false | false | false | true | 8,961 |
847G | Problem - 847G - 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 *900 No tag edit access → Contest materials ") ") ") — the number of groups. Each of the following _n_ lines contains a sequence consisting of 7 zeroes and ones — the schedule of classes on Monday for a group. If the symbol in a position equals to 1 then the group has class in the corresponding time slot. In the other case, the group has no class in the corresponding time slot. Output Print minimum number of rooms needed to hold all groups classes on Monday. Examples Input 2 0101010 1010101 Output 1 Input 3 0101011 0011001 0110111 Output 3 Note In the first example one room is enough. It will be occupied in each of the seven time slot by the first group or by the second group. In the second example three rooms is enough, because in the seventh time slot all three groups have classes. | 900 | false | false | true | false | false | false | false | false | false | false | 6,365 |
1132A | A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all. You have a number of strings; each string is a bracket sequence of length $$$2$$$. So, overall you have $$$cnt_1$$$ strings "((", $$$cnt_2$$$ strings "()", $$$cnt_3$$$ strings ")(" and $$$cnt_4$$$ strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length $$$2(cnt_1 + cnt_2 + cnt_3 + cnt_4)$$$. You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either. Input The input consists of four lines, $$$i$$$-th of them contains one integer $$$cnt_i$$$ ($$$0 le cnt_i le 10^9$$$). Output Print one integer: $$$1$$$ if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, $$$0$$$ otherwise. Note In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence. In the second example it is possible to construct a string "", which is a regular bracket sequence. | 1,100 | false | true | true | false | false | false | false | false | false | false | 5,061 |
1277B | There are $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$. For the one move you can choose any even value $$$c$$$ and divide by two all elements that equal $$$c$$$. For example, if $$$a=[6,8,12,6,3,12]$$$ and you choose $$$c=6$$$, and $$$a$$$ is transformed into $$$a=[3,8,12,3,3,12]$$$ after the move. You need to find the minimal number of moves for transforming $$$a$$$ to an array of only odd integers (each element shouldn't be divisible by $$$2$$$). Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains $$$n$$$ ($$$1 le n le 2cdot10^5$$$) — the number of integers in the sequence $$$a$$$. The second line contains positive integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). The sum of $$$n$$$ for all test cases in the input doesn't exceed $$$2cdot10^5$$$. Output For $$$t$$$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $$$2$$$). Example Input 4 6 40 6 40 3 20 1 1 1024 4 2 4 8 16 3 3 1 7 Note In the first test case of the example, the optimal sequence of moves can be as follows: before making moves $$$a=[40, 6, 40, 3, 20, 1]$$$; choose $$$c=6$$$; now $$$a=[40, 3, 40, 3, 20, 1]$$$; choose $$$c=40$$$; now $$$a=[20, 3, 20, 3, 20, 1]$$$; choose $$$c=20$$$; now $$$a=[10, 3, 10, 3, 10, 1]$$$; choose $$$c=10$$$; now $$$a=[5, 3, 5, 3, 5, 1]$$$ — all numbers are odd. Thus, all numbers became odd after $$$4$$$ moves. In $$$3$$$ or fewer moves, you cannot make them all odd. | 1,200 | false | true | false | false | false | false | false | false | false | false | 4,308 |
669A | Problem - 669A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *800 No tag edit access → Contest materials ") ") xa0— number of stones Artem received on his birthday. Output Print the maximum possible number of times Artem can give presents to Masha. Examples Input 1 Output 1 Input 2 Output 1 Input 3 Output 2 Input 4 Output 3 Note In the first sample, Artem can only give 1 stone to Masha. In the second sample, Atrem can give Masha 1 or 2 stones, though he can't give her 1 stone two times. In the third sample, Atrem can first give Masha 2 stones, a then 1 more stone. In the fourth sample, Atrem can first give Masha 1 stone, then 2 stones, and finally 1 stone again. | 800 | true | false | false | false | false | false | false | false | false | false | 7,148 |
1536F | Omkar and Akmar are playing a game on a circular board with $$$n$$$ ($$$2 leq n leq 10^6$$$) cells. The cells are numbered from $$$1$$$ to $$$n$$$ so that for each $$$i$$$ ($$$1 leq i leq n-1$$$) cell $$$i$$$ is adjacent to cell $$$i+1$$$ and cell $$$1$$$ is adjacent to cell $$$n$$$. Initially, each cell is empty. Omkar and Akmar take turns placing either an A or a B on the board, with Akmar going first. The letter must be placed on an empty cell. In addition, the letter cannot be placed adjacent to a cell containing the same letter. A player loses when it is their turn and there are no more valid moves. Output the number of possible distinct games where both players play optimally modulo $$$10^9+7$$$. Note that we only consider games where some player has lost and there are no more valid moves. Two games are considered distinct if the number of turns is different or for some turn, the letter or cell number that the letter is placed on were different. A move is considered optimal if the move maximizes the player's chance of winning, assuming the other player plays optimally as well. More formally, if the player who has to move has a winning strategy, they have to make a move after which they will still have a winning strategy. If they do not, they can make any move. Note For the first sample case, the first player has $$$4$$$ possible moves. No matter what the first player plays, the second player only has $$$1$$$ possible move, so there are $$$4$$$ possible games. | 2,600 | true | false | false | false | false | true | false | false | false | false | 2,963 |
1553I | Problem - 1553I - 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 divide and conquer dp fft math *3400 No tag edit access → Contest materials ") Editorial") xa0— the length of a stair array $$$a$$$. The second line of input contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$). Output Print the number of permutations which have stair array equal to $$$a$$$. Since the number can be big, compute it modulo $$$998,244,353$$$. Examples Input 6 3 3 3 1 1 1 Output 6 Input 7 4 4 4 4 3 3 3 Output 6 Input 1 1 Output 1 Input 8 2 2 2 2 2 2 1 1 Output 370 Input 4 3 2 3 1 Output 0 | 3,400 | true | false | false | true | false | false | false | false | false | false | 2,867 |
1798B | Lottery "Three Sevens" was held for $$$m$$$ days. On day $$$i$$$, $$$n_i$$$ people with the numbers $$$a_{i, 1}, ldots, a_{i, n_i}$$$ participated in the lottery. It is known that in each of the $$$m$$$ days, only one winner was selected from the lottery participants. The lottery winner on day $$$i$$$ was not allowed to participate in the lottery in the days from $$$i+1$$$ to $$$m$$$. Unfortunately, the information about the lottery winners has been lost. You need to find any possible list of lottery winners on days from $$$1$$$ to $$$m$$$ or determine that no solution exists. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 50,000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$m$$$ ($$$1 le m le 50,000$$$)xa0— the number of days in which the lottery was held. Next, for each $$$i$$$ from $$$1$$$ to $$$m$$$, follows a two-line block of data. The first line of each block contains a single integer $$$n_i$$$ ($$$1 le n_i le 50,000$$$)xa0— the number of lottery participants on day $$$i$$$. The second line of the block contains integers $$$a_{i, 1}, ldots, a_{i, n_i}$$$ ($$$1 le a_{i, j} le 50,000$$$)xa0— lottery participants on day $$$i$$$. It is guaranteed that all the numbers $$$a_{i, 1}, ldots, a_{i, n_i}$$$ are pairwise distinct. It is guaranteed that the sum of $$$n_i$$$ over all blocks of all test cases does not exceed $$$50,000$$$. Output For each test case, if there is no solution, print a single integer $$$-1$$$. Otherwise, print $$$m$$$ integers $$$p_1, p_2, ldots, p_m$$$ ($$$1 le p_i le 50,000$$$)xa0— lottery winners on days from $$$1$$$ to $$$m$$$. If there are multiple solutions, print any of them. Example Input 3 3 4 1 2 4 8 3 2 9 1 2 1 4 2 2 1 2 2 2 1 4 4 1 2 3 4 1 1 1 4 1 3 Note In the first test case, one of the answers is $$$[8, 2, 1]$$$ since the participant with the number $$$8$$$ participated on day $$$1$$$, but did not participate on days $$$2$$$ and $$$3$$$; the participant with the number $$$2$$$ participated on day $$$2$$$, but did not participate on day $$$3$$$; and the participant with the number $$$1$$$ participated on day $$$3$$$. Note that this is not the only possible answer, for example, $$$[8, 9, 4]$$$ is also a correct answer. In the second test case, both lottery participants participated on both days, so any possible lottery winner on the day $$$1$$$ must have participated on the day $$$2$$$, which is not allowed. Thus, there is no correct answer. In the third test case, only one participant participated on days $$$2$$$, $$$3$$$, $$$4$$$, and on day $$$1$$$ there is only one participant who did not participate in the lottery on days $$$2, 3, 4$$$xa0— participant $$$2$$$, which means $$$[2, 1, 4, 3]$$$ is the only correct answer to this test case. | 1,000 | false | true | true | false | true | false | true | false | false | false | 1,489 |
1594C | Theofanis has a string $$$s_1 s_2 dots s_n$$$ and a character $$$c$$$. He wants to make all characters of the string equal to $$$c$$$ using the minimum number of operations. In one operation he can choose a number $$$x$$$ ($$$1 le x le n$$$) and for every position $$$i$$$, where $$$i$$$ is not divisible by $$$x$$$, replace $$$s_i$$$ with $$$c$$$. Find the minimum number of operations required to make all the characters equal to $$$c$$$ and the $$$x$$$-s that he should use in his operations. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains the integer $$$n$$$ ($$$3 le n le 3 cdot 10^5$$$) and a lowercase Latin letter $$$c$$$xa0— the length of the string $$$s$$$ and the character the resulting string should consist of. The second line of each test case contains a string $$$s$$$ of lowercase Latin lettersxa0— the initial string. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, firstly print one integer $$$m$$$xa0— the minimum number of operations required to make all the characters equal to $$$c$$$. Next, print $$$m$$$ integers $$$x_1, x_2, dots, x_m$$$ ($$$1 le x_j le n$$$)xa0— the $$$x$$$-s that should be used in the order they are given. It can be proved that under given constraints, an answer always exists. If there are multiple answers, print any. Example Input 3 4 a aaaa 4 a baaa 4 b bzyx Note Let's describe what happens in the third test case: 1. $$$x_1 = 2$$$: we choose all positions that are not divisible by $$$2$$$ and replace them, i.xa0e. bzyx $$$ ightarrow$$$ bzbx; 2. $$$x_2 = 3$$$: we choose all positions that are not divisible by $$$3$$$ and replace them, i.xa0e. bzbx $$$ ightarrow$$$ bbbb. | 1,200 | true | true | false | false | false | false | true | false | false | false | 2,676 |
1420D | Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are $$$n$$$ lamps with Spirit Tree's light. Sein knows the time of turning on and off for the $$$i$$$-th lampxa0— $$$l_i$$$ and $$$r_i$$$ respectively. To open the door you have to choose $$$k$$$ lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the $$$k$$$ lamps to pick, Ori is interested: how many ways there are to pick such $$$k$$$ lamps that the door will open? It may happen that Sein may be wrong and there are no such $$$k$$$ lamps. The answer might be large, so print it modulo $$$998,244,353$$$. Input First line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 3 cdot 10^5$$$, $$$1 le k le n$$$)xa0— total number of lamps and the number of lamps that must be turned on simultaneously. Next $$$n$$$ lines contain two integers $$$l_i$$$ ans $$$r_i$$$ ($$$1 le l_i le r_i le 10^9$$$)xa0— period of time when $$$i$$$-th lamp is turned on. Note In first test case there are nine sets of $$$k$$$ lamps: $$$(1, 2, 3)$$$, $$$(1, 2, 4)$$$, $$$(1, 2, 5)$$$, $$$(1, 2, 6)$$$, $$$(1, 3, 6)$$$, $$$(1, 4, 6)$$$, $$$(2, 3, 6)$$$, $$$(2, 4, 6)$$$, $$$(2, 6, 7)$$$. In second test case $$$k=1$$$, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time $$$3$$$, so the answer is 1. In fifth test case there are seven sets of $$$k$$$ lamps: $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$, $$$(3, 5)$$$, $$$(4, 5)$$$. | 1,800 | false | false | false | false | true | false | false | false | true | false | 3,576 |
793C | Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them. The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (_x_1,u2009_y_1) and (_x_2,u2009_y_2). Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the _i_-th mouse is equal to (_v__i__x_,u2009_v__i__y_), that means that the _x_ coordinate of the mouse increases by _v__i__x_ units per second, while the _y_ coordinates increases by _v__i__y_ units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap. Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of computer mice on the desk. The second line contains four integers _x_1, _y_1, _x_2 and _y_2 (0u2009≤u2009_x_1u2009≤u2009_x_2u2009≤u2009100u2009000), (0u2009≤u2009_y_1u2009≤u2009_y_2u2009≤u2009100u2009000)xa0— the coordinates of the opposite corners of the mousetrap. The next _n_ lines contain the information about mice. The _i_-th of these lines contains four integers _r__i__x_, _r__i__y_, _v__i__x_ and _v__i__y_, (0u2009≤u2009_r__i__x_,u2009_r__i__y_u2009≤u2009100u2009000, u2009-u2009100u2009000u2009≤u2009_v__i__x_,u2009_v__i__y_u2009≤u2009100u2009000), where (_r__i__x_,u2009_r__i__y_) is the initial position of the mouse, and (_v__i__x_,u2009_v__i__y_) is its speed. Output In the only line print minimum possible non-negative number _t_ such that if Igor closes the mousetrap at _t_ seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such _t_, print -1. Your answer is considered correct if its absolute or relative error doesn't exceed 10u2009-u20096. Formally, let your answer be _a_, and the jury's answer be _b_. Your answer is considered correct if . Examples Input 4 7 7 9 8 3 5 7 5 7 5 2 4 3 3 7 8 6 6 3 2 Output 0.57142857142857139685 Input 4 7 7 9 8 0 3 -5 4 5 0 5 4 9 9 -1 -6 10 5 -7 -10 Note Here is a picture of the first sample Points A, B, C, D - start mice positions, segments are their paths. Then, at first time when all mice will be in rectangle it will be looks like this: Here is a picture of the second sample Points A, D, B will never enter rectangle. | 2,300 | true | false | true | false | false | false | false | false | true | false | 6,605 |
2009D | Satyam is given $$$n$$$ distinct points on the 2D coordinate plane. It is guaranteed that $$$0 leq y_i leq 1$$$ for all given points $$$(x_i, y_i)$$$. How many different nondegenerate right triangles$$$^{ ext{∗}}$$$ can be formed from choosing three different points as its vertices? Two triangles $$$a$$$ and $$$b$$$ are different if there is a point $$$v$$$ such that $$$v$$$ is a vertex of $$$a$$$ but not a vertex of $$$b$$$. $$$^{ ext{∗}}$$$A nondegenerate right triangle has positive area and an interior $$$90^{circ}$$$ angle. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$3 leq n leq 2 cdot 10^5$$$) — the number of points. The following $$$n$$$ lines contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 leq x_i leq n$$$, $$$0 leq y_i leq 1$$$) — the $$$i$$$'th point that Satyam can choose from. It is guaranteed that all $$$(x_i, y_i)$$$ are pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output Output an integer for each test case, the number of distinct nondegenerate right triangles that can be formed from choosing three points. Example Input 3 5 1 0 1 1 3 0 5 0 2 1 3 0 0 1 0 3 0 9 1 0 2 0 3 0 4 0 5 0 2 1 7 1 8 1 9 1 Note The four triangles in question for the first test case: | 1,400 | true | false | false | false | false | false | false | false | false | false | 197 |
1244B | Nikolay lives in a two-storied house. There are $$$n$$$ rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between $$$1$$$ and $$$n$$$). If Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers $$$i$$$ and $$$i+1$$$ on each floor are neighbouring, for all $$$1 leq i leq n - 1$$$. There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room $$$x$$$ on the first floor and the room $$$x$$$ on the second floor, then Nikolay can use it to move from one room to another. The picture illustrates a house with $$$n = 4$$$. There is a staircase between the room $$$2$$$ on the first floor and the room $$$2$$$ on the second floor, and another staircase between the room $$$4$$$ on the first floor and the room $$$4$$$ on the second floor. The arrows denote possible directions in which Nikolay can move. The picture corresponds to the string "0101" in the input. Nikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won't enter a room where he has already been). Calculate the maximum number of rooms Nikolay can visit during his tour, if: he can start in any room on any floor of his choice, and he won't visit the same room twice. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of two lines. The first line contains one integer $$$n$$$ $$$(1 le n le 1,000)$$$ — the number of rooms on each floor. The second line contains one string consisting of $$$n$$$ characters, each character is either a '0' or a '1'. If the $$$i$$$-th character is a '1', then there is a staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor. If the $$$i$$$-th character is a '0', then there is no staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor. In hacks it is allowed to use only one test case in the input, so $$$t = 1$$$ should be satisfied. Output For each test case print one integer — the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice. Example Input 4 5 00100 8 00000000 5 11111 3 110 Note In the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then — to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then — to the fifth room on the second floor. So, Nikolay visits $$$6$$$ rooms. There are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room). In the third test case it is possible to visit all rooms: first floor, first room $$$ ightarrow$$$ second floor, first room $$$ ightarrow$$$ second floor, second room $$$ ightarrow$$$ first floor, second room $$$ ightarrow$$$ first floor, third room $$$ ightarrow$$$ second floor, third room $$$ ightarrow$$$ second floor, fourth room $$$ ightarrow$$$ first floor, fourth room $$$ ightarrow$$$ first floor, fifth room $$$ ightarrow$$$ second floor, fifth room. In the fourth test case it is also possible to visit all rooms: second floor, third room $$$ ightarrow$$$ second floor, second room $$$ ightarrow$$$ second floor, first room $$$ ightarrow$$$ first floor, first room $$$ ightarrow$$$ first floor, second room $$$ ightarrow$$$ first floor, third room. | 1,000 | false | false | true | false | false | false | true | false | false | false | 4,480 |
1984B | A digit is large if it is between $$$5$$$ and $$$9$$$, inclusive. A positive integer is large if all of its digits are large. You are given an integer $$$x$$$. Can it be the sum of two large positive integers with the same number of digits? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The only line of each test case contains a single integer $$$x$$$ ($$$10 leq x leq 10^{18}$$$). Output For each test case, output $$$ exttt{YES}$$$ if $$$x$$$ satisfies the condition, and $$$ exttt{NO}$$$ otherwise. You can output $$$ exttt{YES}$$$ and $$$ exttt{NO}$$$ in any case (for example, strings $$$ exttt{yES}$$$, $$$ exttt{yes}$$$, and $$$ exttt{Yes}$$$ will be recognized as a positive response). Example Input 11 1337 200 1393938 1434 98765432123456789 11111111111111111 420 1984 10 69 119 Output YES NO YES YES NO YES NO YES YES NO NO Note In the first test case, we can have $$$658 + 679 = 1337$$$. In the second test case, it can be shown that no numbers of equal length and only consisting of large digits can add to $$$200$$$. In the third test case, we can have $$$696,969 + 696,969 = 1,393,938$$$. In the fourth test case, we can have $$$777 + 657 = 1434$$$. | 1,100 | true | false | true | false | false | false | false | false | false | false | 382 |
337D | Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains _n_ settlements numbered from 1 to _n_. Moving through the swamp is very difficult, so people tramped exactly _n_u2009-u20091 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths. The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range _d_. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance _d_ or less from the settlement where the Book resides. Manao has heard of _m_ settlements affected by the Book of Evil. Their numbers are _p_1,u2009_p_2,u2009...,u2009_p__m_. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task. Input The first line contains three space-separated integers _n_, _m_ and _d_ (1u2009≤u2009_m_u2009≤u2009_n_u2009≤u2009100000;xa00u2009≤u2009_d_u2009≤u2009_n_u2009-u20091). The second line contains _m_ distinct space-separated integers _p_1,u2009_p_2,u2009...,u2009_p__m_ (1u2009≤u2009_p__i_u2009≤u2009_n_). Then _n_u2009-u20091 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers _a__i_ and _b__i_ representing the ends of this path. Output Print a single number — the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0. Examples Input 6 2 3 1 2 1 5 2 3 3 4 4 5 5 6 Note Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5. | 2,000 | false | false | false | true | false | false | false | false | false | false | 8,484 |
1204E | Natasha's favourite numbers are $$$n$$$ and $$$1$$$, and Sasha's favourite numbers are $$$m$$$ and $$$-1$$$. One day Natasha and Sasha met and wrote down every possible array of length $$$n+m$$$ such that some $$$n$$$ of its elements are equal to $$$1$$$ and another $$$m$$$ elements are equal to $$$-1$$$. For each such array they counted its maximal prefix sum, probably an empty one which is equal to $$$0$$$ (in another words, if every nonempty prefix sum is less to zero, then it is considered equal to zero). Formally, denote as $$$f(a)$$$ the maximal prefix sum of an array $$$a_{1, ldots ,l}$$$ of length $$$l geq 0$$$. Then: $$$$$$f(a) = max (0, smash{displaystylemax_{1 leq i leq l}} sum_{j=1}^{i} a_j )$$$$$$ Now they want to count the sum of maximal prefix sums for each such an array and they are asking you to help. As this sum can be very large, output it modulo $$$998: 244: 853$$$. Note In the first example the only possible array is [-1,-1], its maximal prefix sum is equal to $$$0$$$. In the second example the only possible array is [1,1], its maximal prefix sum is equal to $$$2$$$. There are $$$6$$$ possible arrays in the third example: [1,1,-1,-1], f([1,1,-1,-1]) = 2 [1,-1,1,-1], f([1,-1,1,-1]) = 1 [1,-1,-1,1], f([1,-1,-1,1]) = 1 [-1,1,1,-1], f([-1,1,1,-1]) = 1 [-1,1,-1,1], f([-1,1,-1,1]) = 0 [-1,-1,1,1], f([-1,-1,1,1]) = 0 So the answer for the third example is $$$2+1+1+1+0+0 = 5$$$. | 2,300 | true | false | false | true | false | false | false | false | false | false | 4,670 |
1047A | Problem - 1047A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *800 No tag edit access → Contest materials — the integer Little C has. Output Print $$$3$$$ positive integers $$$a,b,c$$$ in a single line, such that $$$a+b+c=n$$$ and none of them is a multiple of $$$3$$$. It can be proved that there is at least one solution. If there are multiple solutions, print any of them. Examples Input 3 Output 1 1 1 Input 233 Output 77 77 79 | 800 | true | false | false | false | false | false | false | false | false | false | 5,457 |
762B | Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, somexa0— only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for _m_ mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. Input The first line contains three integers _a_, _b_ and _c_ (0u2009≤u2009_a_,u2009_b_,u2009_c_u2009≤u2009105) xa0— the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer _m_ (0u2009≤u2009_m_u2009≤u20093·105) xa0— the number of mouses in the price list. The next _m_ lines each describe another mouse. The _i_-th line contains first integer _val__i_ (1u2009≤u2009_val__i_u2009≤u2009109) xa0— the cost of the _i_-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. Output Output two integers separated by spacexa0— the number of equipped computers and the total cost of the mouses you will buy. Example Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Note In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports. | 1,400 | false | true | true | false | false | false | false | false | true | false | 6,742 |
1614E | Divan's new cottage is finally complete! However, after a thorough inspection, it turned out that the workers had installed the insulation incorrectly, and now the temperature in the house directly depends on the temperature outside. More precisely, if the temperature in the house is $$$P$$$ in the morning, and the street temperature is $$$T$$$, then by the next morning the temperature in the house changes according to the following rule: $$$P_{new} = P + 1$$$, if $$$P < T$$$, $$$P_{new} = P - 1$$$, if $$$P > T$$$, $$$P_{new} = P$$$, if $$$P = T$$$. Here $$$P_{new}$$$ is the temperature in the house next morning. Divan is a very busy businessman, so sometimes he is not at home for long periods and does not know what the temperature is there now, so he hired you to find it. You will work for $$$n$$$ days. In the beginning of the $$$i$$$-th day, the temperature outside $$$T_i$$$ is first given to you. After that, on the $$$i$$$-th day, you will receive $$$k_i$$$ queries. Each query asks the following: "if the temperature in the house was $$$x_i$$$ at the morning of the first day, what would be the temperature in the house next morning (after day $$$i$$$)?" Please answer all the businessman's queries. Input The first line of the input contains the number $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$) — the number of days. The following is a description of $$$n$$$ days in the following format. The first line of the description contains an integer $$$T_i$$$ ($$$0 leq T_i leq 10^9$$$) — the temperature on that day. The second line contains a non-negative integer $$$k_i$$$ ($$$0 le k_i le 2 cdot 10^5$$$) — the number of queries that day. The third line contains $$$k$$$ integers $$$x'_i$$$ ($$$0 leq x'_{i} leq 10^9$$$) — the encrypted version of Divan's queries. Let $$$lastans = 0$$$ initially. Divan's actual queries are given by $$$x_i = (x'_i + lastans) bmod (10^9 + 1)$$$, where $$$a bmod b$$$ is the reminder when $$$a$$$ is divided by $$$b$$$. After answering the query, set $$$lastans$$$ to the answer. It is guaranteed that the total number of queries (the sum of all $$$k_i$$$) does not exceed $$$2 cdot 10^5$$$. Output For each query, output a single integer — the temperature in the house after day $$$i$$$. Examples Input 3 50 3 1 2 3 50 3 4 5 6 0 3 7 8 9 Output 2 5 9 15 22 30 38 47 53 Input 4 728 3 859 1045 182 104 1 689 346 6 634 356 912 214 1 1 755 3 241 765 473 Output 858 1902 2083 2770 3401 3754 4663 4874 4872 4870 5107 5868 6337 Input 2 500000000 3 1000000000 999999999 888888888 250000000 5 777777777 666666666 555555555 444444444 333333333 Output 999999999 999999996 888888882 666666656 333333321 888888874 333333317 666666648 Note Let's look at the first four queries from the example input. The temperature is $$$50$$$ on the first day, $$$50$$$ on the second day, and $$$0$$$ on the third day. Note that $$$lastans = 0$$$ initially. The initial temperature of the first query of the first day is $$$(1 , + , lastans) bmod (10^9 + 1) = 1$$$. After the first day, the temperature rises by $$$1$$$, because $$$1 < 50$$$. So the answer to the query is $$$2$$$. Then, we set $$$lastans = 2$$$. The initial temperature of the second query of the first day is $$$(2 , + , lastans) bmod (10^9 + 1) = 4$$$. After the first day, the temperature rises by $$$1$$$, because $$$4 < 50$$$. So the answer to the query is $$$5$$$. Then, we set $$$lastans = 5$$$. The initial temperature of the third query of the first day is $$$(3 , + , lastans) bmod (10^9 + 1) = 8$$$. After the first day, the temperature rises by $$$1$$$. So the answer to the query is $$$9$$$. Then, we set $$$lastans = 9$$$. The initial temperature of the first query of the second day is $$$(4 , + , lastans) bmod (10^9 + 1) = 13$$$. After the first day, the temperature rises by $$$1$$$. After the second day, the temperature rises by $$$1$$$. So the answer to the query is $$$15$$$. Then, we set $$$lastans = 15$$$. | 2,600 | false | false | false | false | true | false | false | true | false | false | 2,565 |
1285D | Problem - 1285D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks brute force dfs and similar divide and conquer dp greedy strings trees *1900 No tag edit access → Contest materials ") Editorial") $$$ is minimum possible, where $$$oplus$$$ denotes the $$$. Input The first line contains integer $$$n$$$ ($$$1le n le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 2^{30}-1$$$). Output Print one integer — the minimum possible value of $$$underset{1 leq i leq n}{max} (a_i oplus X)$$$. Examples Input 3 1 2 3 Output 2 Input 2 1 5 Output 4 Note In the first sample, we can choose $$$X = 3$$$. In the second sample, we can choose $$$X = 5$$$. | 1,900 | false | true | false | true | false | false | true | false | false | false | 4,264 |
1969B | Let's define a cyclic shift of some string $$$s$$$ as a transformation from $$$s_1 s_2 dots s_{n-1} s_{n}$$$ into $$$s_{n} s_1 s_2 dots s_{n-1}$$$. In other words, you take one last character $$$s_n$$$ and place it before the first character while moving all other characters to the right. You are given a binary string $$$s$$$ (a string consisting of only 0-s and/or 1-s). In one operation, you can choose any substring $$$s_l s_{l+1} dots s_r$$$ ($$$1 le l < r le s$$$) and cyclically shift it. The cost of such operation is equal to $$$r - l + 1$$$ (or the length of the chosen substring). You can perform the given operation any number of times. What is the minimum total cost to make $$$s$$$ sorted in non-descending order? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first and only line of each test case contains a binary string $$$s$$$ ($$$2 le s le 2 cdot 10^5$$$; $$$s_i in$$$ {0, 1})xa0— the string you need to sort. Additional constraint on the input: the sum of lengths of strings over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print the single integerxa0— the minimum total cost to make string sorted using operation above any number of times. Example Input 5 10 0000 11000 101011 01101001 Note In the first test case, you can choose the whole string and perform a cyclic shift: 10 $$$ ightarrow$$$ 01. The length of the substring is $$$2$$$, so the cost is $$$2$$$. In the second test case, the string is already sorted, so you don't need to perform any operations. In the third test case, one of the optimal strategies is the next: 1. choose substring $$$[1, 3]$$$: 11000 $$$ ightarrow$$$ 01100; 2. choose substring $$$[2, 4]$$$: 01100 $$$ ightarrow$$$ 00110; 3. choose substring $$$[3, 5]$$$: 00110 $$$ ightarrow$$$ 00011. The total cost is $$$3 + 3 + 3 = 9$$$. | 1,000 | false | true | false | false | false | true | false | false | false | false | 491 |
1873D | You are given a strip of paper $$$s$$$ that is $$$n$$$ cells long. Each cell is either black or white. In an operation you can take any $$$k$$$ consecutive cells and make them all white. Find the minimum number of operations needed to remove all black cells. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq k leq n leq 2 cdot 10^5$$$)xa0— the length of the paper and the integer used in the operation. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of characters $$$ exttt{B}$$$ (representing a black cell) or $$$ exttt{W}$$$ (representing a white cell). The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the minimum number of operations needed to remove all black cells. Example Input 8 6 3 WBWWWB 7 3 WWBWBWW 5 4 BWBWB 5 5 BBBBB 8 2 BWBWBBBB 10 2 WBBWBBWBBW 4 1 BBBB 3 2 WWW Note In the first test case you can perform the following operations: $$$$$$color{red}{ exttt{WBW}} exttt{WWB} o exttt{WWW}color{red}{ exttt{WWB}} o exttt{WWWWWW}$$$$$$ In the second test case you can perform the following operations: $$$$$$ exttt{WW}color{red}{ exttt{BWB}} exttt{WW} o exttt{WWWWWWW}$$$$$$ In the third test case you can perform the following operations: $$$$$$ exttt{B}color{red}{ exttt{WBWB}} o color{red}{ exttt{BWWW}} exttt{W} o exttt{WWWWW}$$$$$$ | 800 | false | true | true | false | false | false | false | false | false | false | 1,041 |
643G | One social network developer recently suggested a new algorithm of choosing ads for users. There are _n_ slots which advertisers can buy. It is possible to buy a segment of consecutive slots at once. The more slots you own, the bigger are the chances your ad will be shown to users. Every time it is needed to choose ads to show, some segment of slots is picked by a secret algorithm. Then some advertisers are chosen. The only restriction is that it should be guaranteed for advertisers which own at least _p_% of slots composing this segment that their ad will be shown. From the other side, users don't like ads. So it was decided to show no more than ads at once. You are asked to develop a system to sell segments of slots and choose ads in accordance with the rules described above. Input The first line of the input contains three integers _n_, _m_ and _p_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009150u2009000,u200920u2009≤u2009_p_u2009≤u2009100)xa0— the number of slots, the number of queries to your system and threshold for which display of the ad is guaranteed. Next line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009150u2009000), where the _i_-th number means id of advertiser who currently owns the _i_-th slot. Next _m_ lines contain queries descriptions. Each description is of one of the following forms: 1 l r id (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_,u20091u2009≤u2009_id_u2009≤u2009150 000)xa0— advertiser _id_ bought all slots in a range from _l_ to _r_ inclusive; 2 l r (1u2009≤u2009_l_u2009≤u2009_r_)xa0— you need to choose advertisers for segment [_l_,u2009_r_]. Output For each query of the second type answer should be printed in a separate line. First integer of the answer should be the number of advertisements that will be shown . Next _cnt_ integers should be advertisers' ids. It is allowed to print one advertiser more than once, but each advertiser that owns at least slots of the segment from _l_ to _r_ should be in your answer. Example Input 5 9 33 1 2 1 3 3 2 1 5 2 1 5 2 1 3 2 3 3 1 2 4 5 2 1 5 2 3 5 1 4 5 1 2 1 5 Output 3 1 2 3 2 1 3 2 2 1 3 1 1000 1000 1 5 2 5 3 2 1 5 Note Samples demonstrate that you actually have quite a lot of freedom in choosing advertisers. | 3,200 | false | false | false | false | true | false | false | false | false | false | 7,238 |
672A | Problem - 672A - 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 xa0— the position of the digit you need to print. Output Print the _n_-th digit of the line. Examples Input 3 Output 3 Input 11 Output 0 Note In the first sample the digit at position 3 is '3', as both integers 1 and 2 consist on one digit. In the second sample, the digit at position 11 is '0', it belongs to the integer 10. | 800 | false | false | true | false | false | false | false | false | false | false | 7,135 |
1561C | In a certain video game, the player controls a hero characterized by a single integer value: power. The hero will have to beat monsters that are also characterized by a single integer value: armor. On the current level, the hero is facing $$$n$$$ caves. To pass the level, the hero must enter all the caves in some order, each cave exactly once, and exit every cave safe and sound. When the hero enters cave $$$i$$$, he will have to fight $$$k_i$$$ monsters in a row: first a monster with armor $$$a_{i, 1}$$$, then a monster with armor $$$a_{i, 2}$$$ and so on, finally, a monster with armor $$$a_{i, k_i}$$$. The hero can beat a monster if and only if the hero's power is strictly greater than the monster's armor. If the hero can't beat the monster he's fighting, the game ends and the player loses. Note that once the hero enters a cave, he can't exit it before he fights all the monsters in it, strictly in the given order. Each time the hero beats a monster, the hero's power increases by $$$1$$$. Find the smallest possible power the hero must start the level with to be able to enter all the caves in some order and beat all the monsters. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of caves. The $$$i$$$-th of the next $$$n$$$ lines contains an integer $$$k_i$$$ ($$$1 le k_i le 10^5$$$)xa0— the number of monsters in the $$$i$$$-th cave, followed by $$$k_i$$$ integers $$$a_{i, 1}, a_{i, 2}, ldots, a_{i, k_i}$$$ ($$$1 le a_{i, j} le 10^9$$$)xa0— armor levels of the monsters in cave $$$i$$$ in order the hero has to fight them. It is guaranteed that the sum of $$$k_i$$$ over all test cases does not exceed $$$10^5$$$. Note In the first test case, the hero has to beat a single monster with armor $$$42$$$, it's enough to have power $$$43$$$ to achieve that. In the second test case, the hero can pass the level with initial power $$$13$$$ as follows: enter cave $$$2$$$: beat a monster with armor $$$12$$$, power increases to $$$14$$$; beat a monster with armor $$$11$$$, power increases to $$$15$$$; enter cave $$$1$$$: beat a monster with armor $$$10$$$, power increases to $$$16$$$; beat a monster with armor $$$15$$$, power increases to $$$17$$$; beat a monster with armor $$$8$$$, power increases to $$$18$$$. | 1,300 | false | true | false | false | false | false | false | true | true | false | 2,822 |
1311B | You are given an array $$$a$$$ of length $$$n$$$. You are also given a set of distinct positions $$$p_1, p_2, dots, p_m$$$, where $$$1 le p_i < n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions. Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 le a_2 le dots le a_n$$$) using only allowed swaps. For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order. You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le m < n le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, dots, p_m$$$ ($$$1 le p_i < n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement. Output For each test case, print the answer — "YES" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 le a_2 le dots le a_n$$$) using only allowed swaps. Otherwise, print "NO". | 1,200 | false | false | false | false | false | false | false | false | true | false | 4,142 |
585F | Vasily has recently learned about the amazing properties of number π. In one of the articles it has been hypothesized that, whatever the sequence of numbers we have, in some position, this sequence is found among the digits of number π. Thus, if you take, for example, the epic novel "War and Peace" of famous Russian author Leo Tolstoy, and encode it with numbers, then we will find the novel among the characters of number π. Vasily was absolutely delighted with this, because it means that all the books, songs and programs have already been written and encoded in the digits of π. Vasily is, of course, a bit wary that this is only a hypothesis and it hasn't been proved, so he decided to check it out. To do this, Vasily downloaded from the Internet the archive with the sequence of digits of number π, starting with a certain position, and began to check the different strings of digits on the presence in the downloaded archive. Vasily quickly found short strings of digits, but each time he took a longer string, it turned out that it is not in the archive. Vasily came up with a definition that a string of length _d_ is a half-occurrence if it contains a substring of length of at least , which occurs in the archive. To complete the investigation, Vasily took 2 large numbers _x_,u2009_y_ (_x_u2009≤u2009_y_) with the same number of digits and now he wants to find the number of numbers in the interval from _x_ to _y_, which are half-occurrences in the archive. Help Vasily calculate this value modulo 109u2009+u20097. Input The first line contains string _s_ consisting of decimal digits (1u2009≤u2009_s_u2009≤u20091000) that Vasily will use to search substrings in. According to hypothesis, this sequence of digis indeed occurs in the decimal representation of π, although we can't guarantee that. The second and third lines contain two positive integers _x_,u2009_y_ of the same length _d_ (_x_u2009≤u2009_y_, 2u2009≤u2009_d_u2009≤u200950). Numbers _x_,u2009_y_ do not contain leading zeroes. | 3,200 | false | false | true | true | false | false | false | false | false | false | 7,500 |
2005B1 | This is the easy version of the problem. The only differences between the two versions are the constraints on $$$m$$$ and $$$q$$$. In this version, $$$m=2$$$ and $$$q=1$$$. You can make hacks only if both versions of the problem are solved. Narek and Tsovak were busy preparing this round, so they have not managed to do their homework and decided to steal David's homework. Their strict teacher noticed that David has no homework and now wants to punish him. She hires other teachers to help her catch David. And now $$$m$$$ teachers together are chasing him. Luckily, the classroom is big, so David has many places to hide. The classroom can be represented as a one-dimensional line with cells from $$$1$$$ to $$$n$$$, inclusive. At the start, all $$$m$$$ teachers and David are in distinct cells. Then they make moves. During each move David goes to an adjacent cell or stays at the current one. Then, each of the $$$m$$$ teachers simultaneously goes to an adjacent cell or stays at the current one. This continues until David is caught. David is caught if any of the teachers (possibly more than one) is located in the same cell as David. Everyone sees others' moves, so they all act optimally. Your task is to find how many moves it will take for the teachers to catch David if they all act optimally. Acting optimally means the student makes his moves in a way that maximizes the number of moves the teachers need to catch him; and the teachers coordinate with each other to make their moves in a way that minimizes the number of moves they need to catch the student. Also, as Narek and Tsovak think this task is easy, they decided to give you $$$q$$$ queries on David's position. Note: this is the easy version, and you are given only one query. Input In the first line of the input, you are given a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0— the number of test cases. The description of each test case follows. In the first line of each test case, you are given three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$3 le n le 10^9$$$, $$$m=2$$$, $$$q=1$$$)xa0— the number of cells on the line, the number of teachers, and the number of queries. In the second line of each test case, you are given $$$m$$$ distinct integers $$$b_1, b_2, ldots, b_m$$$ ($$$1 le b_i le n$$$)xa0— the cell numbers of the teachers. In the third line of each test case, you are given $$$q$$$ integers $$$a_1, a_2, ldots, a_q$$$ ($$$1 le a_i le n$$$)xa0— David's cell number for every query. It is guaranteed that for any $$$i$$$, $$$j$$$ such that $$$1 le i le m$$$ and $$$1 le j le q$$$, $$$b_i eq a_j$$$. Output For each test case, output $$$q$$$ lines, the $$$i$$$-th of them containing the answer of the $$$i$$$-th query. Example Input 3 10 2 1 1 4 2 8 2 1 3 6 1 8 2 1 3 6 8 Note In the first example, the student can just stay at cell $$$2$$$. The teacher, initially located in cell $$$1$$$, can reach cell $$$2$$$ in one move. Therefore, the answer is $$$1$$$. In the second example, the student should just stay at cell $$$1$$$. The teacher, initially located in cell $$$3$$$, can reach cell $$$1$$$ in two moves. Therefore, the answer is $$$2$$$. | 1,000 | true | true | false | false | false | false | false | false | true | false | 223 |
1702D | Let $$$s$$$ be a string of lowercase Latin letters. Its price is the sum of the indices of letters (an integer between 1 and 26) that are included in it. For example, the price of the string abca is $$$1+2+3+1=7$$$. The string $$$w$$$ and the integer $$$p$$$ are given. Remove the minimal number of letters from $$$w$$$ so that its price becomes less than or equal to $$$p$$$ and print the resulting string. Note that the resulting string may be empty. You can delete arbitrary letters, they do not have to go in a row. If the price of a given string $$$w$$$ is less than or equal to $$$p$$$, then nothing needs to be deleted and $$$w$$$ must be output. Note that when you delete a letter from $$$w$$$, the order of the remaining letters is preserved. For example, if you delete the letter e from the string test, you get tst. Input The first line of input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the test. The following are descriptions of $$$t$$$ test cases. Each case consists of two lines. The first of them is the string $$$w$$$, it is non-empty and consists of lowercase Latin letters. Its length does not exceed $$$2cdot10^5$$$. The second line contains an integer $$$p$$$ ($$$1 le p le 5,200,000$$$). It is guaranteed that the sum of string lengths $$$w$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output Output exactly $$$t$$$ rows, the $$$i$$$-th of them should contain the answer to the $$$i$$$-th set of input data. Print the longest string that is obtained from $$$w$$$ by deleting letters such that its price is less or equal to $$$p$$$. If there are several answers, then output any of them. Note that the empty string xa0— is one of the possible answers. In this case, just output an empty string. Example Input 5 abca 2 abca 6 codeforces 1 codeforces 10 codeforces 100 Output aa abc cdc codeforces | 1,000 | false | true | false | false | false | false | false | false | false | false | 2,067 |
1887F | You had a sequence $$$a_1, a_2, ldots, a_n$$$ consisting of integers from $$$1$$$ to $$$n$$$, not necessarily distinct. For some unknown reason, you decided to calculate the following characteristic of the sequence: Let $$$r_i$$$ ($$$1 le i le n$$$) be the smallest $$$j ge i$$$ such that on the subsegment $$$a_i, a_{i+1}, ldots, a_j$$$ all distinct numbers from the sequence $$$a$$$ appear. More formally, for any $$$k in [1, n]$$$, there exists $$$l in [i, j]$$$ such that $$$a_k = a_l$$$. If such $$$j$$$ does not exist, $$$r_i$$$ is considered to be equal to $$$n+1$$$. The characteristic of the sequence $$$a$$$ is defined as the sequence $$$r_1, r_2, ldots, r_n$$$. Unfortunately, the sequence $$$a$$$ got lost, but you still have its characteristic $$$r$$$. You want to reconstruct any sequence $$$a$$$ that matches the characteristic, or determine that there is an error in the characteristic and such a sequence does not exist. Input Each test consist of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the lost sequence $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$r_1, r_2, ldots, r_n$$$ ($$$i le r_i le n+1$$$)xa0— the characteristic of the lost sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the following: If there is no sequence $$$a$$$ with the characteristic $$$r$$$, print "No". Otherwise, print "Yes" on the first line, and on the second line, print any sequence of integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$) that matches the characteristic $$$r$$$. You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 5 3 2 3 4 5 2 3 5 4 6 1 1 3 1 3 4 8 3 6 6 6 8 9 9 9 Output Yes 1 2 1 No Yes 1 No Yes 1 3 5 3 5 1 1 3 Note In the first test case, the sequence $$$a = [1, 2, 1]$$$ is suitable. The integers $$$1$$$ and $$$2$$$ appear on the subsegments $$$[1, 2]$$$ and $$$[2, 3]$$$. In the second test case, it can be proved that there is no suitable sequence $$$a$$$. | 3,400 | false | false | false | false | false | true | false | false | false | false | 967 |
94B | One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something. The following statement caught his attention: "Among any six people there are either three pairwise acquainted people or three pairwise unacquainted people" Igor just couldn't get why the required minimum is 6 people. "Well, that's the same for five people, too!" — he kept on repeating in his mind. — "Let's take, say, Max, Ilya, Vova — here, they all know each other! And now let's add Dima and Oleg to Vova — none of them is acquainted with each other! Now, that math is just rubbish!" Igor K. took 5 friends of his and wrote down who of them is friends with whom. Now he wants to check whether it is true for the five people that among them there are either three pairwise acquainted or three pairwise not acquainted people. Input The first line contains an integer _m_ (0u2009≤u2009_m_u2009≤u200910), which is the number of relations of acquaintances among the five friends of Igor's. Each of the following _m_ lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u20095;_a__i_u2009≠u2009_b__i_), where (_a__i_,u2009_b__i_) is a pair of acquainted people. It is guaranteed that each pair of the acquaintances is described exactly once. The acquaintance relation is symmetrical, i.e. if _x_ is acquainted with _y_, then _y_ is also acquainted with _x_. Output Print "FAIL", if among those five people there are no either three pairwise acquainted or three pairwise unacquainted people. Otherwise print "WIN". | 1,300 | true | false | true | false | false | false | false | false | false | true | 9,509 |
1300B | Reminder: the . The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized. What is the minimum possible absolute difference you can achieve? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of students halved. The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, dots, a_{2 n}$$$ ($$$1 le a_i le 10^9$$$)xa0— skill levels of students. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note In the first test, there is only one way to partition studentsxa0— one in each class. The absolute difference of the skill levels will be $$$1 - 1 = 0$$$. In the second test, one of the possible partitions is to make the first class of students with skill levels $$$[6, 4, 2]$$$, so that the skill level of the first class will be $$$4$$$, and second with $$$[5, 1, 3]$$$, so that the skill level of the second class will be $$$3$$$. Absolute difference will be $$$4 - 3 = 1$$$. Note that you can't assign like $$$[2, 3]$$$, $$$[6, 5, 4, 1]$$$ or $$$[]$$$, $$$[6, 5, 4, 1, 2, 3]$$$ because classes have even number of students. $$$[2]$$$, $$$[1, 3, 4]$$$ is also not possible because students with skills $$$5$$$ and $$$6$$$ aren't assigned to a class. In the third test you can assign the students in the following way: $$$[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$$$ or $$$[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$$$. Both divisions give minimal possible absolute difference. | 1,000 | false | true | true | false | false | false | false | false | true | false | 4,195 |
793B | Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank. Bankopolis looks like a grid of _n_ rows and _m_ columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000)xa0— the number of rows and the number of columns in the grid. Each of the next _n_ lines contains _m_ characters denoting the corresponding row of the grid. The following characters can occur: "." — an empty cell; "*" — a cell with road works; "S" — the cell where Igor's home is located; "T" — the cell where Igor's office is located. It is guaranteed that "S" and "T" appear exactly once each. Output In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise. Examples Input 5 5 ..S.. ****. T.... ****. ..... Input 5 5 S.... ****. ..... .*** ..T.. Note The first sample is shown on the following picture: In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture: | 1,600 | false | false | true | false | false | false | false | false | false | true | 6,606 |
1735E | There are $$$n$$$ houses in your city arranged on an axis at points $$$h_1, h_2, ldots, h_n$$$. You want to build a new house for yourself and consider two options where to place it: points $$$p_1$$$ and $$$p_2$$$. As you like visiting friends, you have calculated in advance the distances from both options to all existing houses. More formally, you have calculated two arrays $$$d_1$$$, $$$d_2$$$: $$$d_{i, j} = leftp_i - h_j ight$$$, where $$$x$$$ defines the absolute value of $$$x$$$. After a long time of inactivity you have forgotten the locations of the houses $$$h$$$ and the options $$$p_1$$$, $$$p_2$$$. But your diary still keeps two arrays — $$$d_1$$$, $$$d_2$$$, whose authenticity you doubt. Also, the values inside each array could be shuffled, so values at the same positions of $$$d_1$$$ and $$$d_2$$$ may correspond to different houses. Pay attention, that values from one array could not get to another, in other words, all values in the array $$$d_1$$$ correspond the distances from $$$p_1$$$ to the houses, and in the array $$$d_2$$$ xa0— from $$$p_2$$$ to the houses. Also pay attention, that the locations of the houses $$$h_i$$$ and the considered options $$$p_j$$$ could match. For example, the next locations are correct: $$$h = {1, 0, 3, 3}$$$, $$$p = {1, 1}$$$, that could correspond to already shuffled $$$d_1 = {0, 2, 1, 2}$$$, $$$d_2 = {2, 2, 1, 0}$$$. Check whether there are locations of houses $$$h$$$ and considered points $$$p_1$$$, $$$p_2$$$, for which the founded arrays of distances would be correct. If it is possible, find appropriate locations of houses and considered options. 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 one integer $$$n$$$ ($$$1 le n le 10^3$$$) — the length of arrays $$$d_1$$$, $$$d_2$$$. The next two lines contain $$$n$$$ integers each: arrays $$$d_1$$$ and $$$d_2$$$ ($$$0 le d_{i, j} le 10^9$$$) respectively. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^3$$$. Output For each test case, output a single line "NO" if there is no answer. Otherwise output three lines. The first line must contain "YES". In the second line, print $$$n$$$ integers $$$h_1, h_2, ldots, h_n$$$. In the third line print two integers $$$p_1$$$, $$$p_2$$$. It must be satisfied that $$$0 le h_i, p_1, p_2 le 2 cdot 10^9$$$. We can show that if there is an answer, then there is one satisfying these constraints. If there are several answers, output any of them. Note In the image below you can see the sample solutions. Planned houses are shown in bright colours: pink and purple. Existing houses are dim. In test case $$$1$$$, the first planned house is located at point $$$0$$$, the second at point $$$10$$$. The existing house is located at point $$$5$$$ and is at a distance of $$$5$$$ from both planned houses. It can be shown that there is no answer for test case $$$2$$$. In test case $$$3$$$, the planned houses are located at points $$$33$$$ and $$$69$$$. Note that in test case $$$4$$$, both plans are located at point $$$1$$$, where one of the existing houses is located at the same time. It is a correct placement. | 2,400 | false | true | false | false | true | true | false | false | false | false | 1,876 |
1004A | Sonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants. The country where Sonya lives is an endless line. There is a city in each integer coordinate on this line. She has $$$n$$$ hotels, where the $$$i$$$-th hotel is located in the city with coordinate $$$x_i$$$. Sonya is a smart girl, so she does not open two or more hotels in the same city. Sonya understands that her business needs to be expanded by opening new hotels, so she decides to build one more. She wants to make the minimum distance from this hotel to all others to be equal to $$$d$$$. The girl understands that there are many possible locations to construct such a hotel. Thus she wants to know the number of possible coordinates of the cities where she can build a new hotel. Because Sonya is lounging in a jacuzzi in one of her hotels, she is asking you to find the number of cities where she can build a new hotel so that the minimum distance from the original $$$n$$$ hotels to the new one is equal to $$$d$$$. Input The first line contains two integers $$$n$$$ and $$$d$$$ ($$$1leq nleq 100$$$, $$$1leq dleq 10^9$$$)xa0— the number of Sonya's hotels and the needed minimum distance from a new hotel to all others. The second line contains $$$n$$$ different integers in strictly increasing order $$$x_1, x_2, ldots, x_n$$$ ($$$-10^9leq x_ileq 10^9$$$)xa0— coordinates of Sonya's hotels. Output Print the number of cities where Sonya can build a new hotel so that the minimum distance from this hotel to all others is equal to $$$d$$$. Note In the first example, there are $$$6$$$ possible cities where Sonya can build a hotel. These cities have coordinates $$$-6$$$, $$$5$$$, $$$6$$$, $$$12$$$, $$$13$$$, and $$$19$$$. In the second example, there are $$$5$$$ possible cities where Sonya can build a hotel. These cities have coordinates $$$2$$$, $$$6$$$, $$$13$$$, $$$16$$$, and $$$21$$$. | 900 | false | false | true | false | false | false | false | false | false | false | 5,668 |
1633F | You are given a tree consisting of $$$n$$$ vertices (numbered from $$$1$$$ to $$$n$$$) and $$$n-1$$$ edges (numbered from $$$1$$$ to $$$n-1$$$). Initially, all vertices except vertex $$$1$$$ are inactive. You have to process queries of three types: $$$1$$$ $$$v$$$ — activate the vertex $$$v$$$. It is guaranteed that the vertex $$$v$$$ is inactive before this query, and one of its neighbors is active. After activating the vertex, you have to choose a subset of edges of the tree such that each active vertex is incident to exactly one chosen edge, and each inactive vertex is not incident to any of the chosen edges — in other words, this subset should represent a perfect matching on the active part of the tree. If any such subset of edges exists, print the sum of indices of edges in it; otherwise, print $$$0$$$. $$$2$$$ — queries of this type will be asked only right after a query of type $$$1$$$, and there will be at most $$$10$$$ such queries. If your answer to the previous query was $$$0$$$, simply print $$$0$$$; otherwise, print the subset of edges for the previous query as follows: first, print the number of edges in the subset, then print the indices of the chosen edges in ascending order. The sum of indices should be equal to your answer to the previous query. $$$3$$$ — terminate the program. 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. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program. Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the number of vertices of the tree. Then $$$n-1$$$ lines follow. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i e v_i$$$) — the endpoints of the $$$i$$$-th edge. These edges form a tree. Then the queries follow in the format described in the statement, one line per query. There will be at least $$$2$$$ and at most $$$n+10$$$ queries. The last query (and only the last one) will be of type $$$3$$$. Note that you can read the $$$i$$$-th query only if you have already given the answer for the query $$$i-1$$$ (except for $$$i = 1$$$). If your answer for one of the queries is incorrect and the judging program recognizes it, instead of the next query, you may receive the integer $$$0$$$ on a separate line. After receiving it, your program should terminate gracefully, and you will receive "Wrong Answer" verdict. If your program doesn't terminate, your solution may receive some other verdict, like "Time Limit Exceeded", "Idleness Limit Exceeded", etc. Note that the fact that your solution doesn't receive the integer $$$0$$$, it does not mean that all your answers are correct, some of them will be checked only after your program is terminated. | 2,800 | false | false | false | false | true | false | false | false | false | false | 2,447 |
1833E | # Round Dance Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes n people came to the festival and decided to dance a few round dances. There are at least 2 people in the round dance and each person has exactly two neighbors. If there are 2 people in the round dance then they have the same neighbor on each side. You decided to find out exactly how many dances there were. But each participant of the holiday remembered exactly one neighbor. Your task is to determine what the minimum and maximum number of round dances could be. For example, if there were 6 people at the holiday, and the numbers of the neighbors they remembered are equal [2 , 1, 4, 3, 6, 5] , then the minimum number of round dances is 1: • 1 − 2 − 3 − 4 − 5 − 6 − 1 and the maximum is 3: • 1 − 2 − 1 • 3 − 4 − 3 • 5 − 6 − 5 # Input The first line contains a positive number t (1 ≤ t ≤ 10 4) x16 the number of test cases. The following is a description of the test cases. The first line of the description of each test case contains a positive number n (2 ≤ n ≤ 2 · 10 5) x16 the number of people at the holiday. The second line of the description of each test case contains n integers ai (1 ≤ ai ≤ n, a i 6 = i) x16 the number of the neighbor that the ith person remembered. It is guaranteed that the test cases are correct and corresponds to at least one division of people into round dances. It is guaranteed that the sum of n for all test cases does not exceed 2 · 10 5. # Output For each test case, output two integers x16 the minimum and maximum number of round dances that could be. Page 1 of 2 Example standard input standard output 10 62 1 4 3 6 5 62 3 1 5 6 4 92 3 2 5 6 5 8 9 8 22 1 44 3 2 1 52 3 4 5 1 65 3 4 1 1 2 53 5 4 1 2 66 3 2 5 4 3 65 1 4 3 4 2 1 3 2 2 1 3 1 1 1 2 1 1 1 1 2 2 1 2 1 1 Page 2 of 2 | 1,600 | false | false | false | false | false | false | false | false | false | true | 1,287 |
330A | Problem - 330A - 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 , denoting the number of rows and the number of columns of the cake. The next _r_ lines each contains _c_ characters — the _j_-th character of the _i_-th line denotes the content of the cell at row _i_ and column _j_, and is either one of these: '.' character denotes a cake cell with no evil strawberry; 'S' character denotes a cake cell with an evil strawberry. Output Output the maximum number of cake cells that the cakeminator can eat. Examples Input 3 4 S... .... ..S. Output 8 Note For the first example, one possible way to eat the maximum number of cake cells is as follows (perform 3 eats). | 800 | false | false | true | false | false | false | true | false | false | false | 8,524 |
896B | This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. Initially, Ithea puts _n_ clear sheets of paper in a line. They are numbered from 1 to _n_ from left to right. This game will go on for _m_ rounds. In each round, Ithea will give Chtholly an integer between 1 and _c_, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the _n_ numbers are in non-decreasing order looking from left to right from sheet 1 to sheet _n_, and if after _m_ rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. Input The first line contains 3 integers _n_,u2009_m_ and _c_ (, means rounded up)xa0— the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. Interaction In each round, your program needs to read one line containing a single integer _p__i_ (1u2009≤u2009_p__i_u2009≤u2009_c_), indicating the number given to Chtholly. Your program should then output a line containing an integer between 1 and _n_, indicating the number of sheet to write down this number in. After outputting each line, don't forget to flush the output. For example: fflush(stdout) in C/C++; System.out.flush() in Java; sys.stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game. Note In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: The first line contains 3 integers _n_,u2009_m_ and _c_; The following _m_ lines each contains an integer between 1 and _c_, indicating the number given to Chtholly in each round. | 2,000 | false | true | false | false | false | true | false | true | false | false | 6,164 |
7E | Most C/C++ programmers know about excellent opportunities that preprocessor #define directives give; but many know as well about the problems that can arise because of their careless use. In this problem we consider the following model of #define constructions (also called macros). Each macro has its name and value. The generic syntax for declaring a macro is the following: #define macro_name macro_value After the macro has been declared, "macro_name" is replaced with "macro_value" each time it is met in the program (only the whole tokens can be replaced; i.e. "macro_name" is replaced only when it is surrounded by spaces or other non-alphabetic symbol). A "macro_value" within our model can only be an arithmetic expression consisting of variables, four arithmetic operations, brackets, and also the names of previously declared macros (in this case replacement is performed sequentially). The process of replacing macros with their values is called substitution. One of the main problems arising while using macros — the situation when as a result of substitution we get an arithmetic expression with the changed order of calculation because of different priorities of the operations. Let's consider the following example. Say, we declared such a #define construction: #define sum x + y and further in the program the expression "2 sum" is calculated. After macro substitution is performed we get "2 x + y", instead of intuitively expected "2 (x + y)". Let's call the situation "suspicious", if after the macro substitution the order of calculation changes, falling outside the bounds of some macro. Thus, your task is to find out by the given set of #define definitions and the given expression if this expression is suspicious or not. Let's speak more formally. We should perform an ordinary macros substitution in the given expression. Moreover, we should perform a "safe" macros substitution in the expression, putting in brackets each macro value; after this, guided by arithmetic rules of brackets expansion, we can omit some of the brackets. If there exist a way to get an expression, absolutely coinciding with the expression that is the result of an ordinary substitution (character-by-character, but ignoring spaces), then this expression and the macros system are called correct, otherwise — suspicious. Note that we consider the "/" operation as the usual mathematical division, not the integer division like in C/C++. That's why, for example, in the expression "a*(b/c)" we can omit brackets to get the expression "a*b/c". Input The first line contains the only number _n_ (0u2009≤u2009_n_u2009≤u2009100) — the amount of #define constructions in the given program. Then there follow _n_ lines, each of them contains just one #define construction. Each construction has the following syntax: #define name expression where name — the macro name, expression — the expression with which the given macro will be replaced. An expression is a non-empty string, containing digits,names of variables, names of previously declared macros, round brackets and operational signs +-*/. It is guaranteed that the expression (before and after macros substitution) is a correct arithmetic expression, having no unary operations. The expression contains only non-negative integers, not exceeding 109. All the names (#define constructions' names and names of their arguments) are strings of case-sensitive Latin characters. It is guaranteed that the name of any variable is different from any #define construction. Then, the last line contains an expression that you are to check. This expression is non-empty and satisfies the same limitations as the expressions in #define constructions. The input lines may contain any number of spaces anywhere, providing these spaces do not break the word "define" or the names of constructions and variables. In particular, there can be any number of spaces before and after the "#" symbol. The length of any line from the input file does not exceed 100 characters. | 2,600 | false | false | true | true | false | false | false | false | false | false | 9,956 |
1822C | Tema loves cinnabon rolls — buns with cinnabon and chocolate in the shape of a "snail". Cinnabon rolls come in different sizes and are square when viewed from above. The most delicious part of a roll is the chocolate, which is poured in a thin layer over the cinnabon roll in the form of a spiral and around the bun, as in the following picture: Cinnabon rolls of sizes 4, 5, 6 For a cinnabon roll of size $$$n$$$, the length of the outer side of the square is $$$n$$$, and the length of the shortest vertical chocolate segment in the central part is one. Formally, the bun consists of two dough spirals separated by chocolate. A cinnabon roll of size $$$n + 1$$$ is obtained from a cinnabon roll of size $$$n$$$ by wrapping each of the dough spirals around the cinnabon roll for another layer. It is important that a cinnabon roll of size $$$n$$$ is defined in a unique way. Tema is interested in how much chocolate is in his cinnabon roll of size $$$n$$$. Since Tema has long stopped buying small cinnabon rolls, it is guaranteed that $$$n ge 4$$$. Answer this non-obvious question by calculating the total length of the chocolate layer. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0— the number of test cases. The following $$$t$$$ lines describe the test cases. Each test case is described by a single integer $$$n$$$ ($$$4 le n le 10^9$$$)xa0— the size of the cinnabon roll. Output Output $$$t$$$ integers. The $$$i$$$-th of them should be equal to the total length of the chocolate layer in the $$$i$$$-th test case. Example Output 26 37 50 32105178545472401 | 800 | true | false | false | false | false | false | false | false | false | false | 1,349 |
1825B | LuoTianyi gave an array $$$b$$$ of $$$n cdot m$$$ integers. She asks you to construct a table $$$a$$$ of size $$$n imes m$$$, filled with these $$$n cdot m$$$ numbers, and each element of the array must be used exactly once. Also she asked you to maximize the following value: $$$sumlimits_{i=1}^{n}sumlimits_{j=1}^{m}left(maxlimits_{1 le x le i, 1 le y le j}a_{x,y}-minlimits_{1 le x le i, 1 le y le j}a_{x,y} ight)$$$ This means that we consider $$$n cdot m$$$ subtables with the upper left corner in $$$(1,1)$$$ and the bottom right corner in $$$(i, j)$$$ ($$$1 le i le n$$$, $$$1 le j le m$$$), for each such subtable calculate the difference of the maximum and minimum elements in it, then sum up all these differences. You should maximize the resulting sum. Help her find the maximal possible value, you don't need to reconstruct the table itself. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 200$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 100$$$) — the number of rows and columns of the table. The second line of each test case contains $$$n cdot m$$$ integers $$$b_1, b_2, ldots, b_{ncdot m}$$$ ($$$-10^5 le b_{i} le 10^5$$$) — the numbers you can put in the table. Note, that integers in the array $$$b$$$ can be negative. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases doesn't exceed $$$2cdot 10^5$$$. Output For each test case, output a single integer — the maximal value, that can be obtained. Example Input 5 2 2 1 3 1 4 2 2 -1 -1 -1 -1 2 3 7 8 9 -3 10 8 3 2 4 8 -3 0 -7 1 4 3 -32030 59554 16854 -85927 68060 -64460 -79547 90932 85063 82703 -12001 38762 Note In the first test case, the table is follows: In the subtable with the bottom right corner in $$$(1, 1)$$$, the difference of the maximal and minimal elements is $$$4 - 4 = 0$$$. In the subtable with the bottom right corner in $$$(1, 2)$$$, the difference of the maximal and minimal elements is $$$4 - 1 = 3$$$. In the subtable with the bottom right corner in $$$(2, 1)$$$, the difference of the maximal and minimal elements is $$$4 - 1 = 3$$$. In the subtable with the bottom right corner in $$$(2, 2)$$$, the difference of the maximal and minimal elements is $$$4 - 1 = 3$$$. Then the maximum possible value is $$$0+3+3+3=9$$$. In the second test case, all elements are equal, so all differences are $$$0$$$, and the answer is $$$0$$$. | 1,000 | true | true | false | false | false | false | false | false | false | false | 1,330 |
1462B | Polycarp has a string $$$s[1 dots n]$$$ of length $$$n$$$ consisting of decimal digits. Polycarp performs the following operation with the string $$$s$$$ no more than once (i.e. he can perform operation $$$0$$$ or $$$1$$$ time): Polycarp selects two numbers $$$i$$$ and $$$j$$$ ($$$1 leq i leq j leq n$$$) and removes characters from the $$$s$$$ string at the positions $$$i, i+1, i+2, ldots, j$$$ (i.e. removes substring $$$s[i dots j]$$$). More formally, Polycarp turns the string $$$s$$$ into the string $$$s_1 s_2 ldots s_{i-1} s_{j+1} s_{j+2} ldots s_{n}$$$. For example, the string $$$s = $$$"20192020" Polycarp can turn into strings: "2020" (in this case $$$(i, j)=(3, 6)$$$ or $$$(i, j)=(1, 4)$$$); "2019220" (in this case $$$(i, j)=(6, 6)$$$); "020" (in this case $$$(i, j)=(1, 5)$$$); other operations are also possible, only a few of them are listed above. Polycarp likes the string "2020" very much, so he is wondering if it is possible to turn the string $$$s$$$ into a string "2020" in no more than one operation? Note that you can perform zero operations. Input The first line contains a positive integer $$$t$$$ ($$$1 leq t leq 1000 $$$)xa0— number of test cases in the test. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$4 leq n leq 200$$$)xa0— length of the string $$$s$$$. The next line contains a string $$$s$$$ of length $$$n$$$ consisting of decimal digits. It is allowed that the string $$$s$$$ starts with digit 0. Output For each test case, output on a separate line: "YES" if Polycarp can turn the string $$$s$$$ into a string "2020" in no more than one operation (i.e. he can perform $$$0$$$ or $$$1$$$ operation); "NO" otherwise. You may print every letter of "YES" and "NO" in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 6 8 20192020 8 22019020 4 2020 5 20002 6 729040 6 200200 Output YES YES YES NO NO NO Note In the first test case, Polycarp could choose $$$i=3$$$ and $$$j=6$$$. In the second test case, Polycarp could choose $$$i=2$$$ and $$$j=5$$$. In the third test case, Polycarp did not perform any operations with the string. | 800 | false | false | true | true | false | false | false | false | false | false | 3,379 |
1095A | Problem - 1095A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials ") Editorial") , Polycarp uses the following algorithm: he writes down $$$s_1$$$ ones, he writes down $$$s_2$$$ twice, he writes down $$$s_3$$$ three times, ... he writes down $$$s_m$$$ $$$m$$$ times. For example, if $$$s$$$="bab" the process is: "b" $$$ o$$$ "baa" $$$ o$$$ "baabbb". So the encrypted $$$s$$$="bab" is "baabbb". Given string $$$t$$$ — the result of encryption of some string $$$s$$$. Your task is to decrypt it, i.u2009e. find the string $$$s$$$. Input The first line contains integer $$$n$$$ ($$$1 le n le 55$$$) — the length of the encrypted string. The second line of the input contains $$$t$$$ — the result of encryption of some string $$$s$$$. It contains only lowercase Latin letters. The length of $$$t$$$ is exactly $$$n$$$. It is guaranteed that the answer to the test exists. Output Print such string $$$s$$$ that after encryption it equals $$$t$$$. Examples Input 6 baabbb Output bab Input 10 ooopppssss Output oops Input 1 z Output z | 800 | false | false | true | false | false | false | false | false | false | false | 5,237 |
705B | Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the second, second is connected with third and so on, while the last one is connected with the first one again. Cycle may consist of a single isolated vertex. Initially there are _k_ cycles, _i_-th of them consisting of exactly _v__i_ vertices. Players play alternatively. Peter goes first. On each turn a player must choose a cycle with at least 2 vertices (for example, _x_ vertices) among all available cycles and replace it by two cycles with _p_ and _x_u2009-u2009_p_ vertices where 1u2009≤u2009_p_u2009<u2009_x_ is chosen by the player. The player who cannot make a move loses the game (and his life!). Peter wants to test some configurations of initial cycle sets before he actually plays with Dr. Octopus. Initially he has an empty set. In the _i_-th test he adds a cycle with _a__i_ vertices to the set (this is actually a multiset because it can contain two or more identical cycles). After each test, Peter wants to know that if the players begin the game with the current set of cycles, who wins? Peter is pretty good at math, but now he asks you to help. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of tests Peter is about to make. The second line contains _n_ space separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109), _i_-th of them stands for the number of vertices in the cycle added before the _i_-th test. Output Print the result of all tests in order they are performed. Print 1 if the player who moves first wins or 2 otherwise. Note In the first sample test: In Peter's first test, there's only one cycle with 1 vertex. First player cannot make a move and loses. In his second test, there's one cycle with 1 vertex and one with 2. No one can make a move on the cycle with 1 vertex. First player can replace the second cycle with two cycles of 1 vertex and second player can't make any move and loses. In his third test, cycles have 1, 2 and 3 vertices. Like last test, no one can make a move on the first cycle. First player can replace the third cycle with one cycle with size 1 and one with size 2. Now cycles have 1, 1, 2, 2 vertices. Second player's only move is to replace a cycle of size 2 with 2 cycles of size 1. And cycles are 1, 1, 1, 1, 2. First player replaces the last cycle with 2 cycles with size 1 and wins. In the second sample test: Having cycles of size 1 is like not having them (because no one can make a move on them). In Peter's third test: There a cycle of size 5 (others don't matter). First player has two options: replace it with cycles of sizes 1 and 4 or 2 and 3. If he replaces it with cycles of sizes 1 and 4: Only second cycle matters. Second player will replace it with 2 cycles of sizes 2. First player's only option to replace one of them with two cycles of size 1. Second player does the same thing with the other cycle. First player can't make any move and loses. If he replaces it with cycles of sizes 2 and 3: Second player will replace the cycle of size 3 with two of sizes 1 and 2. Now only cycles with more than one vertex are two cycles of size 2. As shown in previous case, with 2 cycles of size 2 second player wins. So, either way first player loses. | 1,100 | true | false | false | false | false | false | false | false | false | false | 7,000 |
1153F | Getting closer and closer to a mathematician, Serval becomes a university student on math major in Japari University. On the Calculus class, his teacher taught him how to calculate the expected length of a random subsegment of a given segment. Then he left a bonus problem as homework, with the award of a garage kit from IOI. The bonus is to extend this problem to the general case as follows. You are given a segment with length $$$l$$$. We randomly choose $$$n$$$ segments by choosing two points (maybe with non-integer coordinates) from the given segment equiprobably and the interval between the two points forms a segment. You are given the number of random segments $$$n$$$, and another integer $$$k$$$. The $$$2n$$$ endpoints of the chosen segments split the segment into $$$(2n+1)$$$ intervals. Your task is to calculate the expected total length of those intervals that are covered by at least $$$k$$$ segments of the $$$n$$$ random segments. You should find the answer modulo $$$998244353$$$. Input First line contains three space-separated positive integers $$$n$$$, $$$k$$$ and $$$l$$$ ($$$1leq k leq n leq 2000$$$, $$$1leq lleq 10^9$$$). Output Output one integerxa0— the expected total length of all the intervals covered by at least $$$k$$$ segments of the $$$n$$$ random segments modulo $$$998244353$$$. Formally, let $$$M = 998244353$$$. 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, the expected total length is $$$int_0^1 int_0^1 x-y ,mathrm{d}x,mathrm{d}y = {1over 3}$$$, and $$$3^{-1}$$$ modulo $$$998244353$$$ is $$$332748118$$$. | 2,600 | true | false | false | true | false | false | false | false | false | false | 4,944 |
1264E | A football league has recently begun in Beautiful land. There are $$$n$$$ teams participating in the league. Let's enumerate them with integers from $$$1$$$ to $$$n$$$. There will be played exactly $$$frac{n(n-1)}{2}$$$ matches: each team will play against all other teams exactly once. In each match, there is always a winner and loser and there is no draw. After all matches are played, the organizers will count the number of beautiful triples. Let's call a triple of three teams $$$(A, B, C)$$$ beautiful if a team $$$A$$$ win against a team $$$B$$$, a team $$$B$$$ win against a team $$$C$$$ and a team $$$C$$$ win against a team $$$A$$$. We look only to a triples of different teams and the order of teams in the triple is important. The beauty of the league is the number of beautiful triples. At the moment, $$$m$$$ matches were played and their results are known. What is the maximum beauty of the league that can be, after playing all remaining matches? Also find a possible results for all remaining $$$frac{n(n-1)}{2} - m$$$ matches, so that the league has this maximum beauty. Input The first line contains two integers $$$n, m$$$ ($$$3 leq n leq 50, 0 leq m leq frac{n(n-1)}{2}$$$)xa0— the number of teams in the football league and the number of matches that were played. Each of $$$m$$$ following lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 leq u, v leq n$$$, $$$u eq v$$$) denoting that the $$$u$$$-th team won against the $$$v$$$-th team. It is guaranteed that each unordered pair of teams appears at most once. Output Print $$$n$$$ lines, each line having a string of exactly $$$n$$$ characters. Each character must be either $$$0$$$ or $$$1$$$. Let $$$a_{ij}$$$ be the $$$j$$$-th number in the $$$i$$$-th line. For all $$$1 leq i leq n$$$ it should be true, that $$$a_{ii} = 0$$$. For all pairs of teams $$$i eq j$$$ the number $$$a_{ij}$$$ indicates the result of the match between the $$$i$$$-th team and the $$$j$$$-th team: If $$$a_{ij}$$$ is $$$1$$$, the $$$i$$$-th team wins against the $$$j$$$-th team; Otherwise the $$$j$$$-th team wins against the $$$i$$$-th team; Also, it should be true, that $$$a_{ij} + a_{ji} = 1$$$. Also note that the results of the $$$m$$$ matches that were already played cannot be changed in your league. The beauty of the league in the output should be maximum possible. If there are multiple possible answers with maximum beauty, you can print any of them. Examples Output 0110 0001 0100 1010 Note The beauty of league in the first test case is equal to $$$3$$$ because there exists three beautiful triples: $$$(1, 2, 3)$$$, $$$(2, 3, 1)$$$, $$$(3, 1, 2)$$$. The beauty of league in the second test is equal to $$$6$$$ because there exists six beautiful triples: $$$(1, 2, 4)$$$, $$$(2, 4, 1)$$$, $$$(4, 1, 2)$$$, $$$(2, 4, 3)$$$, $$$(4, 3, 2)$$$, $$$(3, 2, 4)$$$. | 2,700 | false | false | false | false | false | true | false | false | false | false | 4,376 |
1817B | You are given a simple undirected graph with $$$n$$$ nodes and $$$m$$$ edges. Note that the graph is not necessarily connected. The nodes are labeled from $$$1$$$ to $$$n$$$. We define a graph to be a Fish Graph if it contains a simple cycle with a special node $$$u$$$ belonging to the cycle. Apart from the edges in the cycle, the graph should have exactly $$$2$$$ extra edges. Both edges should connect to node $$$u$$$, but they should not be connected to any other node of the cycle. Determine if the graph contains a subgraph that is a Fish Graph, and if so, find any such subgraph. In this problem, we define a subgraph as a graph obtained by taking any subset of the edges of the original graph. Visualization of example 1. The red edges form one possible subgraph that is a Fish Graph. Input The first line of input contains the integer $$$t$$$ ($$$1 leq t leq 1000$$$), the number of test cases. The description of test cases follows. The first line of each test case contains two integers, $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2,000$$$)xa0— the number of nodes and the number of edges. Each of the next $$$m$$$ lines contains the description of an edge. Each line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$)xa0— an edge connects node $$$u_i$$$ to node $$$v_i$$$. It is guaranteed that no two edges connect the same unordered pair of nodes. Furthermore, it is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases both do not exceed $$$2,000$$$. Output For each testcase, output "YES" if the graph contains a subgraph that is a Fish Graph, otherwise print "NO". If the answer is "YES", on the following lines output a description of the subgraph. The first line of the description contains one integer $$$k$$$ — the number of edges of the subgraph. On the next $$$k$$$ lines, output the edges of the chosen subgraph. Each of the $$$k$$$ lines should contains two integers $$$u$$$ and $$$v$$$ ($$$1le u, vle n$$$, $$$u eq v$$$) — the edge between $$$u$$$ and $$$v$$$ belongs to the subgraph. The order in which $$$u$$$ and $$$v$$$ are printed does not matter, as long as the two nodes are connected by an edge in the original graph. The order in which you print the edges does not matter, as long as the resulting subgraph is a fish graph. If there are multiple solutions, print any. Example Input 3 7 8 1 2 2 3 3 4 4 1 4 5 4 6 4 2 6 7 7 7 6 7 1 2 2 3 3 4 4 1 1 3 3 5 4 4 1 3 3 4 4 1 1 2 Output YES 6 5 4 6 4 4 3 1 4 2 1 3 2 YES 5 5 3 2 3 3 1 4 3 1 4 NO Note In the first example, a possible valid subgraph contains the cycle $$$1 ightarrow 2 ightarrow 3 ightarrow 4 ightarrow 1$$$. The special node of this cycle is node $$$4$$$. The two extra edges $$$4 - 5$$$ and $$$4 - 6$$$ are both connected to $$$4$$$, completing the Fish Graph. In the second example, a possible valid subgraph contains the cycle $$$1 ightarrow 3 ightarrow 4 ightarrow 1$$$. The special node of this cycle is node $$$3$$$. The two extra edges $$$3 - 2$$$ and $$$3 - 5$$$ are both connected to $$$3$$$, completing the Fish Graph. In the last example, it can be proven that there is no valid subgraph. | 1,900 | false | false | false | false | false | true | true | false | false | true | 1,372 |
812C | On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains _n_ different items numbered from 1 to _n_. The _i_-th item has base cost _a__i_ Egyptian pounds. If Sagheer buys _k_ items with indices _x_1,u2009_x_2,u2009...,u2009_x__k_, then the cost of item _x__j_ is _a__x__j_u2009+u2009_x__j_·_k_ for 1u2009≤u2009_j_u2009≤u2009_k_. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor _k_. Sagheer wants to buy as many souvenirs as possible without paying more than _S_ Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task? Input The first line contains two integers _n_ and _S_ (1u2009≤u2009_n_u2009≤u2009105 and 1u2009≤u2009_S_u2009≤u2009109)xa0— the number of souvenirs in the market and Sagheer's budget. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105)xa0— the base costs of the souvenirs. Output On a single line, print two integers _k_, _T_xa0— the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these _k_ souvenirs. Note In the first example, he cannot take the three items because they will cost him [5,u20099,u200914] with total cost 28. If he decides to take only two items, then the costs will be [4,u20097,u200911]. So he can afford the first and second items. In the second example, he can buy all items as they will cost him [5,u200910,u200917,u200922]. In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it. | 1,500 | false | false | false | false | false | false | false | true | true | false | 6,514 |
980D | Problem - 980D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp math number theory *2100 No tag edit access → Contest materials ") . Input The first line of input contains a single integer $$$n$$$ ($$$1 leq n leq 5000$$$), the size of the array. The second line contains $$$n$$$ integers $$$a_1$$$,$$$a_2$$$,$$$dots$$$,$$$a_n$$$ ($$$-10^8 leq a_i leq 10^8$$$), the values of the array. Output Output $$$n$$$ space-separated integers, the $$$k$$$-th integer should be the number of contiguous subarrays of $$$A$$$ that have an answer to the problem equal to $$$k$$$. Examples Input 2 5 5 Output 3 0 Input 5 5 -4 2 1 8 Output 5 5 3 2 0 Input 1 0 Output 1 | 2,100 | true | false | false | true | false | false | false | false | false | false | 5,803 |
1418G | Problem - 1418G - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures divide and conquer hashing two pointers *2500 No tag edit access → Contest materials ") . A subarray is considered good if every integer that occurs in this subarray occurs there exactly thrice. For example, the array $$$ | 2,500 | false | false | false | false | true | false | false | false | false | false | 3,588 |
756D | Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters "a", ..., "z". The testtube is divided into _n_ consecutive regions. Each region is occupied by a single colony of a certain bacteria type at any given moment. Hence, the population of the testtube at any moment can be described by a string of _n_ Latin characters. Sometimes a colony can decide to conquer another colony in one of the adjacent regions. When that happens, the attacked colony is immediately eliminated and replaced by a colony of the same type as the attacking colony, while the attacking colony keeps its type. Note that a colony can only attack its neighbours within the boundaries of the testtube. At any moment, at most one attack can take place. For example, consider a testtube with population "babb". There are six options for an attack that may happen next: the first colony attacks the second colony (1u2009→u20092), the resulting population is "bbbb"; 2u2009→u20091, the result is "aabb"; 2u2009→u20093, the result is "baab"; 3u2009→u20092, the result is "bbbb" (note that the result is the same as the first option); 3u2009→u20094 or 4u2009→u20093, the population does not change. The pattern of attacks is rather unpredictable. Julia is now wondering how many different configurations of bacteria in the testtube she can obtain after a sequence of attacks takes place (it is possible that no attacks will happen at all). Since this number can be large, find it modulo 109u2009+u20097. Input The first line contains an integer _n_xa0— the number of regions in the testtube (1u2009≤u2009_n_u2009≤u20095u2009000). The second line contains _n_ small Latin letters that describe the initial population of the testtube. Output Print one numberxa0— the answer to the problem modulo 109u2009+u20097. Note In the first sample the population can never change since all bacteria are of the same type. In the second sample three configurations are possible: "ab" (no attacks), "aa" (the first colony conquers the second colony), and "bb" (the second colony conquers the first colony). To get the answer for the third sample, note that more than one attack can happen. | 2,400 | false | false | false | true | false | false | true | false | false | false | 6,767 |
1433E | Problem - 1433E - 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 math *1300 No tag edit access → Contest materials ") Editorial") met on a plaza and made two round dances, each round dance consists of exactly $$$frac{n}{2}$$$ people. Your task is to find the number of ways $$$n$$$ people can make two round dances if each round dance consists of exactly $$$frac{n}{2}$$$ people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of $$$1$$$ or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances $$$ | 1,300 | true | false | false | false | false | false | false | false | false | false | 3,495 |
995C | For a vector $$$vec{v} = (x, y)$$$, define $$$v = sqrt{x^2 + y^2}$$$. Allen had a bit too much to drink at the bar, which is at the origin. There are $$$n$$$ vectors $$$vec{v_1}, vec{v_2}, cdots, vec{v_n}$$$. Allen will make $$$n$$$ moves. As Allen's sense of direction is impaired, during the $$$i$$$-th move he will either move in the direction $$$vec{v_i}$$$ or $$$-vec{v_i}$$$. In other words, if his position is currently $$$p = (x, y)$$$, he will either move to $$$p + vec{v_i}$$$ or $$$p - vec{v_i}$$$. Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position $$$p$$$ satisfies $$$p le 1.5 cdot 10^6$$$ so that he can stay safe. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of moves. Each of the following lines contains two space-separated integers $$$x_i$$$ and $$$y_i$$$, meaning that $$$vec{v_i} = (x_i, y_i)$$$. We have that $$$v_i le 10^6$$$ for all $$$i$$$. Output Output a single line containing $$$n$$$ integers $$$c_1, c_2, cdots, c_n$$$, each of which is either $$$1$$$ or $$$-1$$$. Your solution is correct if the value of $$$p = sum_{i = 1}^n c_i vec{v_i}$$$, satisfies $$$p le 1.5 cdot 10^6$$$. It can be shown that a solution always exists under the given constraints. Examples Input 3 999999 0 0 999999 999999 0 Input 8 -67761 603277 640586 -396671 46147 -122580 569609 -2112 400 914208 131792 309779 -850150 -486293 5272 721899 | 2,300 | true | true | false | false | true | false | true | false | true | false | 5,724 |
1361C | Johnny's younger sister Megan had a birthday recently. Her brother has bought her a box signed as "Your beautiful necklacexa0— do it yourself!". It contains many necklace parts and some magic glue. The necklace part is a chain connecting two pearls. Color of each pearl can be defined by a non-negative integer. The magic glue allows Megan to merge two pearls (possibly from the same necklace part) into one. The beauty of a connection of pearls in colors $$$u$$$ and $$$v$$$ is defined as follows: let $$$2^k$$$ be the greatest power of two dividing $$$u oplus v$$$xa0— . The beauty of such a necklace is the minimum beauty of a single connection in it. The girl wants to use all available necklace parts to build exactly one necklace consisting of all of them with the largest possible beauty. Help her! Input The first line contains $$$n$$$ $$$(1 leq n leq 5 cdot 10^5)$$$xa0— the number of necklace parts in the box. Each of the next $$$n$$$ lines contains two integers $$$a$$$ and $$$b$$$ $$$(0 leq a, b < 2^{20})$$$, which denote colors of pearls presented in the necklace parts. Pearls in the $$$i$$$-th line have indices $$$2i - 1$$$ and $$$2i$$$ respectively. Output The first line should contain a single integer $$$b$$$ denoting the maximum possible beauty of a necklace built from all given parts. The following line should contain $$$2n$$$ distinct integers $$$p_i$$$ $$$(1 leq p_i leq 2n)$$$xa0— the indices of initial pearls in the order in which they appear on a cycle. Indices of pearls belonging to the same necklace part have to appear at neighboring positions in this permutation (so $$$1,4,3,2$$$ is not a valid output, whereas $$$2,1,4,3$$$ and $$$4,3,1,2$$$ are). If there are many possible answers, you can print any. Examples Input 5 13 11 11 1 3 5 17 1 9 27 Output 3 8 7 9 10 5 6 1 2 3 4 Input 5 13 11 11 1 3 5 17 1 7 29 Output 2 8 7 10 9 5 6 4 3 2 1 Note In the first example the following pairs of pearls are combined: $$$(7, 9)$$$, $$$(10, 5)$$$, $$$(6, 1)$$$, $$$(2, 3)$$$ and $$$(4, 8)$$$. The beauties of connections equal correspondingly: $$$3$$$, $$$3$$$, $$$3$$$, $$$20$$$, $$$20$$$. The following drawing shows this construction. | 2,500 | false | false | false | false | false | true | false | true | false | true | 3,875 |
1428B | In the snake exhibition, there are $$$n$$$ rooms (numbered $$$0$$$ to $$$n - 1$$$) arranged in a circle, with a snake in each room. The rooms are connected by $$$n$$$ conveyor belts, and the $$$i$$$-th conveyor belt connects the rooms $$$i$$$ and $$$(i+1) bmod n$$$. In the other words, rooms $$$0$$$ and $$$1$$$, $$$1$$$ and $$$2$$$, $$$ldots$$$, $$$n-2$$$ and $$$n-1$$$, $$$n-1$$$ and $$$0$$$ are connected with conveyor belts. The $$$i$$$-th conveyor belt is in one of three states: If it is clockwise, snakes can only go from room $$$i$$$ to $$$(i+1) bmod n$$$. If it is anticlockwise, snakes can only go from room $$$(i+1) bmod n$$$ to $$$i$$$. If it is off, snakes can travel in either direction. Above is an example with $$$4$$$ rooms, where belts $$$0$$$ and $$$3$$$ are off, $$$1$$$ is clockwise, and $$$2$$$ is anticlockwise. Each snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there? Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$): the number of test cases. The description of the test cases follows. The first line of each test case description contains a single integer $$$n$$$ ($$$2 le n le 300,000$$$): the number of rooms. The next line of each test case description contains a string $$$s$$$ of length $$$n$$$, consisting of only '<', '>' and '-'. If $$$s_{i} = $$$ '>', the $$$i$$$-th conveyor belt goes clockwise. If $$$s_{i} = $$$ '<', the $$$i$$$-th conveyor belt goes anticlockwise. If $$$s_{i} = $$$ '-', the $$$i$$$-th conveyor belt is off. It is guaranteed that the sum of $$$n$$$ among all test cases does not exceed $$$300,000$$$. Output For each test case, output the number of returnable rooms. Example Input 4 4 -><- 5 >>>>> 3 <-- 2 <> Note In the first test case, all rooms are returnable except room $$$2$$$. The snake in the room $$$2$$$ is trapped and cannot exit. This test case corresponds to the picture from the problem statement. In the second test case, all rooms are returnable by traveling on the series of clockwise belts. | 1,200 | false | false | true | false | false | false | false | false | false | true | 3,524 |
1250I | The director of the famous dance show plans a tour. It is already decided that the tour will consist of up to $$$m$$$ concerts. There are $$$n$$$ dancers in the troupe. Each dancer is characterized by her awkwardness: the awkwardness of the $$$i$$$-th dancer is equal to $$$a_i$$$. The director likes diversity. For this reason, each concert will be performed by a different set of dancers. A dancer may perform in multiple concerts. For example, it is possible that a set of dancers performs in one concert and a subset of this set of dancers performs in another concert. The only constraint is that the same set of dancers cannot perform twice. The director prefers the set with larger number of dancers over the set with smaller number of dancers. If two sets consist of the same number of dancers, then the director prefers the one which has smaller sum of awkwardness of dancers. If two sets of dancers are equal in size and total awkwardness, then the director does not have a preference which one is better. A marketing study shows that viewers are not ready to come to a concert if the total awkwardness of all the dancers performing in the concert is greater than $$$k$$$. The director wants to find the best plan for $$$m$$$ concerts. He thinks to write down all possible sets of dancers; then get rid of the sets with total awkwardness greater than $$$k$$$. The remaining sets of dancers will be sorted according to his preference. The most preferred set of dancers will give the first concert, the second preferred set — the second concert and so on until the $$$m$$$-th concert. If it turns out that the total number of valid sets is less than $$$m$$$, then the total number of concerts will be equal to the number of valid sets. It turns out that the director delegated finding the plan to you! Please, notice that there might be several acceptable plans due to the fact that the director does not have a preference over sets of dancers with the same size and total awkwardness. In this case any of these plans is good enough. For each concert find the number of dancers and the total awkwardness of the set performing. Also, for the last concert find its set of dancers. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases in the input. Then the test cases follow. Each test case begins with a line containing three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 le n le 10^6$$$, $$$1 le k le 10^{18}$$$, $$$1 le m le 10^6$$$) — the total number of dancers, the maximum acceptable awkwardness of a set of dancers and the maximum number of concerts, respectively. The following line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^{12}$$$), where $$$a_i$$$ is the awkwardness of the $$$i$$$-th dancer. The sum of the values of $$$n$$$ over all test cases in the input does not exceed $$$10^6$$$. Similarly, the sum of the values of $$$m$$$ over all test cases in the input does not exceed $$$10^6$$$. Output Print the answers to all test cases in the input. If the troupe cannot give concerts at all, then simply print one line "0". In this case, you should not print anything else. If the troupe gives a positive number of concerts $$$r$$$ ($$$r$$$ is equal to the minimum of $$$m$$$ and the total number of valid sets), then first print the value of $$$r$$$, then $$$r$$$ lines: the $$$j$$$-th line should contain two integers $$$s_j$$$ and $$$t_j$$$ — the number of dancers in the $$$j$$$-th concert and the total awkwardness of the dancers performing in the $$$j$$$-th concert. Complete the output to a test case with a line that describes the last set: print exactly $$$s_r$$$ distinct integers from $$$1$$$ to $$$n$$$ — the numbers of the dancers who will perform at the $$$r$$$-th (last) concert, in any order. If there are several answers, print any of them. Example Input 3 7 13 10 3 1 5 1 8 2 13 2 10 1 12 12 3 32 100000 2 1 5 Output 10 5 12 4 7 4 9 4 10 4 11 4 11 4 12 4 13 3 4 3 5 2 4 1 0 7 3 8 2 3 2 6 2 7 1 1 1 2 1 5 3 | 3,100 | false | true | false | false | false | false | true | true | false | false | 4,447 |
1993F1 | This is the easy version of the problem. The only difference is that in this version $$$k le n$$$. You can make hacks only if both versions of the problem are solved. Given a $$$w imes h$$$ rectangle on the $$$Oxy$$$ plane, with points $$$(0, 0)$$$ at the bottom-left and $$$(w, h)$$$ at the top-right of the rectangle. You also have a robot initially at point $$$(0, 0)$$$ and a script $$$s$$$ of $$$n$$$ characters. Each character is either L, R, U, or D, which tells the robot to move left, right, up, or down respectively. The robot can only move inside the rectangle; otherwise, it will change the script $$$s$$$ as follows: If it tries to move outside a vertical border, it changes all L characters to R's (and vice versa, all R's to L's). If it tries to move outside a horizontal border, it changes all U characters to D's (and vice versa, all D's to U's). Then, it will execute the changed script starting from the character which it couldn't execute. An example of the robot's movement process, $$$s = exttt{"ULULURD"}$$$ The script $$$s$$$ will be executed for $$$k$$$ times continuously. All changes to the string $$$s$$$ will be retained even when it is repeated. During this process, how many times will the robot move to the point $$$(0, 0)$$$ in total? Note that the initial position does NOT count. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains four integers $$$n$$$, $$$k$$$, $$$w$$$, and $$$h$$$ ($$$1 le n, w, h le 10^6$$$; $$$1 le k le n$$$). The second line contains a single string $$$s$$$ of size $$$n$$$ ($$$s_i in { exttt{L}, exttt{R}, exttt{U}, exttt{D}}$$$)xa0— the script to be executed. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, print a single integerxa0— the number of times the robot reaches $$$(0, 0)$$$ when executing script $$$s$$$ for $$$k$$$ times continuously. Example Input 5 2 2 2 2 UR 4 2 1 1 LLDD 6 3 3 1 RLRRRL 5 5 3 3 RUURD 7 5 3 4 RRDLUUU Note In the first test case, the robot only moves up and right. In the end, it occupies the position $$$(2, 2)$$$ but never visits $$$(0, 0)$$$. So the answer is $$$0$$$. In the second test case, each time executing the script the robot visits the origin twice. And since $$$k=2$$$, it visits the origin $$$2 cdot 2 = 4$$$ times overall. In the third test case, the visualization is shown as below: | 2,400 | true | false | false | false | false | true | true | false | false | false | 307 |
760B | _n_ hobbits are planning to spend the night at Frodo's house. Frodo has _n_ beds standing in a row and _m_ pillows (_n_u2009≤u2009_m_). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have. Frodo will sleep on the _k_-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt? Input The only line contain three integers _n_, _m_ and _k_ (1u2009≤u2009_n_u2009≤u2009_m_u2009≤u2009109, 1u2009≤u2009_k_u2009≤u2009_n_)xa0— the number of hobbits, the number of pillows and the number of Frodo's bed. Output Print single integerxa0— the maximum number of pillows Frodo can have so that no one is hurt. Note In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds. In the second example Frodo can take at most four pillows, giving three pillows to each of the others. In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed. | 1,500 | false | true | false | false | false | false | false | true | false | false | 6,750 |
900C | Problem - 900C - 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 data structures math *1700 No tag edit access → Contest materials . Announcement.") . Editorial.") the following holds: _a__j_u2009<u2009_a__i_. Input The first line contains the only integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the length of the permutation. The second line contains _n_ integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_)xa0— the permutation. All the integers are distinct. Output Print the only integerxa0— the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. Examples Input 1 1 Output 1 Input 5 5 1 2 3 4 Output 5 Note In the first example the only element can be removed. | 1,700 | true | false | false | false | true | false | true | false | false | false | 6,144 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.