contestId int64 0 1.01k | name stringlengths 2 58 | tags listlengths 0 11 | title stringclasses 523
values | time-limit stringclasses 8
values | memory-limit stringclasses 8
values | problem-description stringlengths 0 7.15k | input-specification stringlengths 0 2.05k | output-specification stringlengths 0 1.5k | demo-input listlengths 0 7 | demo-output listlengths 0 7 | note stringlengths 0 5.24k | test_cases listlengths 0 402 | timeConsumedMillis int64 0 8k | memoryConsumedBytes int64 0 537M | score float64 -1 3.99 | __index_level_0__ int64 0 621k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
441 | Valera and Swaps | [
"constructive algorithms",
"dsu",
"graphs",
"implementation",
"math",
"string suffix structures"
] | null | null | A permutation *p* of length *n* is a sequence of distinct integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*). A permutation is an identity permutation, if for any *i* the following equation holds *p**i*<==<=*i*.
A swap (*i*,<=*j*) is the operation that swaps elements *p**i* and *p**j* in the permutation. Let's assume that *f*(*p*) is the minimum number of swaps that you need to make the permutation *p* an identity permutation.
Valera wonders, how he can transform permutation *p* into any permutation *q*, such that *f*(*q*)<==<=*m*, using the minimum number of swaps. Help him do that. | The first line contains integer *n* (1<=β€<=*n*<=β€<=3000) β the length of permutation *p*. The second line contains *n* distinct integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*) β Valera's initial permutation. The last line contains integer *m* (0<=β€<=*m*<=<<=*n*). | In the first line, print integer *k* β the minimum number of swaps.
In the second line, print 2*k* integers *x*1,<=*x*2,<=...,<=*x*2*k* β the description of the swap sequence. The printed numbers show that you need to consecutively make swaps (*x*1,<=*x*2), (*x*3,<=*x*4), ..., (*x*2*k*<=-<=1,<=*x*2*k*).
If there are multiple sequence swaps of the minimum length, print the lexicographically minimum one. | [
"5\n1 2 3 4 5\n2\n",
"5\n2 1 4 5 3\n2\n"
] | [
"2\n1 2 1 3 ",
"1\n1 2 "
] | Sequence *x*<sub class="lower-index">1</sub>,β*x*<sub class="lower-index">2</sub>,β...,β*x*<sub class="lower-index">*s*</sub> is lexicographically smaller than sequence *y*<sub class="lower-index">1</sub>,β*y*<sub class="lower-index">2</sub>,β...,β*y*<sub class="lower-index">*s*</sub>, if there is such integer *r* (1ββ€β*r*ββ€β*s*), that *x*<sub class="lower-index">1</sub>β=β*y*<sub class="lower-index">1</sub>,β*x*<sub class="lower-index">2</sub>β=β*y*<sub class="lower-index">2</sub>,β...,β*x*<sub class="lower-index">*r*β-β1</sub>β=β*y*<sub class="lower-index">*r*β-β1</sub> and *x*<sub class="lower-index">*r*</sub>β<β*y*<sub class="lower-index">*r*</sub>. | [] | 46 | 0 | 0 | 6,800 | |
786 | Legacy | [
"data structures",
"graphs",
"shortest paths"
] | null | null | Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So Rick wants to give his legacy to Morty before bad guys catch them.
There are *n* planets in their universe numbered from 1 to *n*. Rick is in planet number *s* (the earth) and he doesn't know where Morty is. As we all know, Rick owns a portal gun. With this gun he can open one-way portal from a planet he is in to any other planet (including that planet). But there are limits on this gun because he's still using its free trial.
By default he can not open any portal by this gun. There are *q* plans in the website that sells these guns. Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.
Plans on the website have three types:
1. With a plan of this type you can open a portal from planet *v* to planet *u*. 1. With a plan of this type you can open a portal from planet *v* to any planet with index in range [*l*,<=*r*]. 1. With a plan of this type you can open a portal from any planet with index in range [*l*,<=*r*] to planet *v*.
Rick doesn't known where Morty is, but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately. So for each planet (including earth itself) he wants to know the minimum amount of money he needs to get from earth to that planet. | The first line of input contains three integers *n*, *q* and *s* (1<=β€<=*n*,<=*q*<=β€<=105, 1<=β€<=*s*<=β€<=*n*) β number of planets, number of plans and index of earth respectively.
The next *q* lines contain the plans. Each line starts with a number *t*, type of that plan (1<=β€<=*t*<=β€<=3). If *t*<==<=1 then it is followed by three integers *v*, *u* and *w* where *w* is the cost of that plan (1<=β€<=*v*,<=*u*<=β€<=*n*, 1<=β€<=*w*<=β€<=109). Otherwise it is followed by four integers *v*, *l*, *r* and *w* where *w* is the cost of that plan (1<=β€<=*v*<=β€<=*n*, 1<=β€<=*l*<=β€<=*r*<=β€<=*n*, 1<=β€<=*w*<=β€<=109). | In the first and only line of output print *n* integers separated by spaces. *i*-th of them should be minimum money to get from earth to *i*-th planet, or <=-<=1 if it's impossible to get to that planet. | [
"3 5 1\n2 3 2 3 17\n2 3 2 2 16\n2 2 2 3 3\n3 3 1 1 12\n1 3 3 17\n",
"4 3 1\n3 4 1 3 12\n2 2 3 4 10\n1 2 4 16\n"
] | [
"0 28 12 \n",
"0 -1 -1 12 \n"
] | In the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2. | [
{
"input": "3 5 1\n2 3 2 3 17\n2 3 2 2 16\n2 2 2 3 3\n3 3 1 1 12\n1 3 3 17",
"output": "0 28 12 "
},
{
"input": "4 3 1\n3 4 1 3 12\n2 2 3 4 10\n1 2 4 16",
"output": "0 -1 -1 12 "
},
{
"input": "6 1 5\n1 3 6 80612370",
"output": "-1 -1 -1 -1 0 -1 "
},
{
"input": "10 8 7\n1 10 ... | 46 | 4,608,000 | -1 | 6,804 | |
774 | Maximum Number | [
"*special",
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below.
So, for example, to show the digit 3 on the display, 5 sections must be highlighted; and for the digit 6, 6 sections must be highlighted.
The battery of the newest device allows to highlight at most *n* sections on the display.
Stepan wants to know the maximum possible integer number which can be shown on the display of his newest device. Your task is to determine this number. Note that this number must not contain leading zeros. Assume that the size of the display is enough to show any integer. | The first line contains the integer *n* (2<=β€<=*n*<=β€<=100<=000) β the maximum number of sections which can be highlighted on the display. | Print the maximum integer which can be shown on the display of Stepan's newest device. | [
"2\n",
"3\n"
] | [
"1\n",
"7\n"
] | none | [
{
"input": "2",
"output": "1"
},
{
"input": "3",
"output": "7"
},
{
"input": "4",
"output": "11"
},
{
"input": "5",
"output": "71"
},
{
"input": "6",
"output": "111"
},
{
"input": "85651",
"output": "711111111111111111111111111111111111111111111111... | 202 | 6,451,200 | 3 | 6,819 | |
557 | Ilya and Diplomas | [
"greedy",
"implementation",
"math"
] | null | null | Soon a school Olympiad in Informatics will be held in Berland, *n* schoolchildren will participate there.
At a meeting of the jury of the Olympiad it was decided that each of the *n* participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one diploma.
They also decided that there must be given at least *min*1 and at most *max*1 diplomas of the first degree, at least *min*2 and at most *max*2 diplomas of the second degree, and at least *min*3 and at most *max*3 diplomas of the third degree.
After some discussion it was decided to choose from all the options of distributing diplomas satisfying these limitations the one that maximizes the number of participants who receive diplomas of the first degree. Of all these options they select the one which maximizes the number of the participants who receive diplomas of the second degree. If there are multiple of these options, they select the option that maximizes the number of diplomas of the third degree.
Choosing the best option of distributing certificates was entrusted to Ilya, one of the best programmers of Berland. However, he found more important things to do, so it is your task now to choose the best option of distributing of diplomas, based on the described limitations.
It is guaranteed that the described limitations are such that there is a way to choose such an option of distributing diplomas that all *n* participants of the Olympiad will receive a diploma of some degree. | The first line of the input contains a single integer *n* (3<=β€<=*n*<=β€<=3Β·106)Β βΒ the number of schoolchildren who will participate in the Olympiad.
The next line of the input contains two integers *min*1 and *max*1 (1<=β€<=*min*1<=β€<=*max*1<=β€<=106)Β βΒ the minimum and maximum limits on the number of diplomas of the first degree that can be distributed.
The third line of the input contains two integers *min*2 and *max*2 (1<=β€<=*min*2<=β€<=*max*2<=β€<=106)Β βΒ the minimum and maximum limits on the number of diplomas of the second degree that can be distributed.
The next line of the input contains two integers *min*3 and *max*3 (1<=β€<=*min*3<=β€<=*max*3<=β€<=106)Β βΒ the minimum and maximum limits on the number of diplomas of the third degree that can be distributed.
It is guaranteed that *min*1<=+<=*min*2<=+<=*min*3<=β€<=*n*<=β€<=*max*1<=+<=*max*2<=+<=*max*3. | In the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas.
The optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first degree. Of all the suitable options, the best one is the one which maximizes the number of participants who receive diplomas of the second degree. If there are several of these options, the best one is the one that maximizes the number of diplomas of the third degree. | [
"6\n1 5\n2 6\n3 7\n",
"10\n1 2\n1 3\n1 5\n",
"6\n1 3\n2 2\n2 2\n"
] | [
"1 2 3 \n",
"2 3 5 \n",
"2 2 2 \n"
] | none | [
{
"input": "6\n1 5\n2 6\n3 7",
"output": "1 2 3 "
},
{
"input": "10\n1 2\n1 3\n1 5",
"output": "2 3 5 "
},
{
"input": "6\n1 3\n2 2\n2 2",
"output": "2 2 2 "
},
{
"input": "55\n1 1000000\n40 50\n10 200",
"output": "5 40 10 "
},
{
"input": "3\n1 1\n1 1\n1 1",
"o... | 31 | 0 | 0 | 6,823 | |
53 | Autocomplete | [
"implementation"
] | A. Autocomplete | 2 | 256 | Autocomplete is a program function that enables inputting the text (in editors, command line shells, browsers etc.) completing the text by its inputted part. Vasya is busy working on a new browser called 'BERowser'. He happens to be working on the autocomplete function in the address line at this very moment. A list consisting of *n* last visited by the user pages and the inputted part *s* are known. Your task is to complete *s* to make it an address of one of the pages from the list. You have to find the lexicographically smallest address having a prefix *s*. | The first line contains the *s* line which is the inputted part. The second line contains an integer *n* (1<=β€<=*n*<=β€<=100) which is the number of visited pages. Then follow *n* lines which are the visited pages, one on each line. All the lines have lengths of from 1 to 100 symbols inclusively and consist of lowercase Latin letters only. | If *s* is not the beginning of any of *n* addresses of the visited pages, print *s*. Otherwise, print the lexicographically minimal address of one of the visited pages starting from *s*.
The lexicographical order is the order of words in a dictionary. The lexicographical comparison of lines is realized by the '<' operator in the modern programming languages. | [
"next\n2\nnextpermutation\nnextelement\n",
"find\n4\nfind\nfindfirstof\nfindit\nfand\n",
"find\n4\nfondfind\nfondfirstof\nfondit\nfand\n"
] | [
"nextelement\n",
"find\n",
"find\n"
] | none | [
{
"input": "next\n2\nnextpermutation\nnextelement",
"output": "nextelement"
},
{
"input": "find\n4\nfind\nfindfirstof\nfindit\nfand",
"output": "find"
},
{
"input": "find\n4\nfondfind\nfondfirstof\nfondit\nfand",
"output": "find"
},
{
"input": "kudljmxcse\n4\nkudljmxcse\nszje... | 62 | 0 | 0 | 6,824 |
911 | Inversion Counting | [
"brute force",
"math"
] | null | null | A permutation of size *n* is an array of size *n* such that each integer from 1 to *n* occurs exactly once in this array. An inversion in a permutation *p* is a pair of indices (*i*,<=*j*) such that *i*<=><=*j* and *a**i*<=<<=*a**j*. For example, a permutation [4,<=1,<=3,<=2] contains 4 inversions: (2,<=1), (3,<=1), (4,<=1), (4,<=3).
You are given a permutation *a* of size *n* and *m* queries to it. Each query is represented by two indices *l* and *r* denoting that you have to reverse the segment [*l*,<=*r*] of the permutation. For example, if *a*<==<=[1,<=2,<=3,<=4] and a query *l*<==<=2, *r*<==<=4 is applied, then the resulting permutation is [1,<=4,<=3,<=2].
After each query you have to determine whether the number of inversions is odd or even. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=1500) β the size of the permutation.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=*n*) β the elements of the permutation. These integers are pairwise distinct.
The third line contains one integer *m* (1<=β€<=*m*<=β€<=2Β·105) β the number of queries to process.
Then *m* lines follow, *i*-th line containing two integers *l**i*, *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*) denoting that *i*-th query is to reverse a segment [*l**i*,<=*r**i*] of the permutation. All queries are performed one after another. | Print *m* lines. *i*-th of them must be equal to odd if the number of inversions in the permutation after *i*-th query is odd, and even otherwise. | [
"3\n1 2 3\n2\n1 2\n2 3\n",
"4\n1 2 4 3\n4\n1 1\n1 4\n1 4\n2 3\n"
] | [
"odd\neven\n",
"odd\nodd\nodd\neven\n"
] | The first example:
1. after the first query *a*β=β[2,β1,β3], inversion: (2,β1); 1. after the second query *a*β=β[2,β3,β1], inversions: (3,β1), (3,β2).
The second example:
1. *a*β=β[1,β2,β4,β3], inversion: (4,β3); 1. *a*β=β[3,β4,β2,β1], inversions: (3,β1), (4,β1), (3,β2), (4,β2), (4,β3); 1. *a*β=β[1,β2,β4,β3], inversion: (4,β3); 1. *a*β=β[1,β4,β2,β3], inversions: (3,β2), (4,β2). | [
{
"input": "3\n1 2 3\n2\n1 2\n2 3",
"output": "odd\neven"
},
{
"input": "4\n1 2 4 3\n4\n1 1\n1 4\n1 4\n2 3",
"output": "odd\nodd\nodd\neven"
},
{
"input": "7\n2 6 1 7 4 5 3\n5\n4 5\n7 7\n5 6\n4 5\n4 5",
"output": "odd\nodd\neven\nodd\neven"
},
{
"input": "3\n2 1 3\n3\n2 3\n1 ... | 1,700 | 21,504,000 | 3 | 6,825 | |
24 | Ring road | [
"graphs"
] | A. Ring road | 2 | 256 | Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all *n* cities of Berland were connected by *n* two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all *n* roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other? | The first line contains integer *n* (3<=β€<=*n*<=β€<=100) β amount of cities (and roads) in Berland. Next *n* lines contain description of roads. Each road is described by three integers *a**i*, *b**i*, *c**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*,<=1<=β€<=*c**i*<=β€<=100) β road is directed from city *a**i* to city *b**i*, redirecting the traffic costs *c**i*. | Output single integer β the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other. | [
"3\n1 3 1\n1 2 1\n3 2 1\n",
"3\n1 3 1\n1 2 5\n3 2 1\n",
"6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42\n",
"4\n1 2 9\n2 3 8\n3 4 7\n4 1 5\n"
] | [
"1\n",
"2\n",
"39\n",
"0\n"
] | none | [
{
"input": "3\n1 3 1\n1 2 1\n3 2 1",
"output": "1"
},
{
"input": "3\n1 3 1\n1 2 5\n3 2 1",
"output": "2"
},
{
"input": "6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42",
"output": "39"
},
{
"input": "4\n1 2 9\n2 3 8\n3 4 7\n4 1 5",
"output": "0"
},
{
"input": "5\n... | 248 | 0 | 3.938 | 6,829 |
328 | IQ Test | [
"implementation"
] | null | null | Petya is preparing for IQ test and he has noticed that there many problems like: you are given a sequence, find the next number. Now Petya can solve only problems with arithmetic or geometric progressions.
Arithmetic progression is a sequence *a*1, *a*1<=+<=*d*, *a*1<=+<=2*d*, ..., *a*1<=+<=(*n*<=-<=1)*d*, where *a*1 and *d* are any numbers.
Geometric progression is a sequence *b*1, *b*2<==<=*b*1*q*, ..., *b**n*<==<=*b**n*<=-<=1*q*, where *b*1<=β <=0, *q*<=β <=0, *q*<=β <=1.
Help Petya and write a program to determine if the given sequence is arithmetic or geometric. Also it should found the next number. If the sequence is neither arithmetic nor geometric, print 42 (he thinks it is impossible to find better answer). You should also print 42 if the next element of progression is not integer. So answer is always integer. | The first line contains exactly four integer numbers between 1 and 1000, inclusively. | Print the required number. If the given sequence is arithmetic progression, print the next progression element. Similarly, if the given sequence is geometric progression, print the next progression element.
Print 42 if the given sequence is not an arithmetic or geometric progression. | [
"836 624 412 200\n",
"1 334 667 1000\n"
] | [
"-12\n",
"1333\n"
] | This problem contains very weak pretests! | [
{
"input": "836 624 412 200",
"output": "-12"
},
{
"input": "1 334 667 1000",
"output": "1333"
},
{
"input": "501 451 400 350",
"output": "42"
},
{
"input": "836 624 412 200",
"output": "-12"
},
{
"input": "1 334 667 1000",
"output": "1333"
},
{
"input... | 0 | 0 | -1 | 6,834 | |
978 | Letters | [
"binary search",
"implementation",
"two pointers"
] | null | null | There are $n$ dormitories in Berland State University, they are numbered with integers from $1$ to $n$. Each dormitory consists of rooms, there are $a_i$ rooms in $i$-th dormitory. The rooms in $i$-th dormitory are numbered from $1$ to $a_i$.
A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all $n$ dormitories is written on an envelope. In this case, assume that all the rooms are numbered from $1$ to $a_1 + a_2 + \dots + a_n$ and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.
For example, in case $n=2$, $a_1=3$ and $a_2=5$ an envelope can have any integer from $1$ to $8$ written on it. If the number $7$ is written on an envelope, it means that the letter should be delivered to the room number $4$ of the second dormitory.
For each of $m$ letters by the room number among all $n$ dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered. | The first line contains two integers $n$ and $m$ $(1 \le n, m \le 2 \cdot 10^{5})$ β the number of dormitories and the number of letters.
The second line contains a sequence $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^{10})$, where $a_i$ equals to the number of rooms in the $i$-th dormitory. The third line contains a sequence $b_1, b_2, \dots, b_m$ $(1 \le b_j \le a_1 + a_2 + \dots + a_n)$, where $b_j$ equals to the room number (among all rooms of all dormitories) for the $j$-th letter. All $b_j$ are given in increasing order. | Print $m$ lines. For each letter print two integers $f$ and $k$ β the dormitory number $f$ $(1 \le f \le n)$ and the room number $k$ in this dormitory $(1 \le k \le a_f)$ to deliver the letter. | [
"3 6\n10 15 12\n1 9 12 23 26 37\n",
"2 3\n5 10000000000\n5 6 9999999999\n"
] | [
"1 1\n1 9\n2 2\n2 13\n3 1\n3 12\n",
"1 5\n2 1\n2 9999999994\n"
] | In the first example letters should be delivered in the following order:
- the first letter in room $1$ of the first dormitory - the second letter in room $9$ of the first dormitory - the third letter in room $2$ of the second dormitory - the fourth letter in room $13$ of the second dormitory - the fifth letter in room $1$ of the third dormitory - the sixth letter in room $12$ of the third dormitory | [
{
"input": "3 6\n10 15 12\n1 9 12 23 26 37",
"output": "1 1\n1 9\n2 2\n2 13\n3 1\n3 12"
},
{
"input": "2 3\n5 10000000000\n5 6 9999999999",
"output": "1 5\n2 1\n2 9999999994"
},
{
"input": "1 1\n1\n1",
"output": "1 1"
},
{
"input": "5 15\n10 20 30 20 10\n1 6 10 11 15 30 31 54... | 1,840 | 23,756,800 | 3 | 6,836 | |
318 | Strings of Power | [
"implementation",
"strings",
"two pointers"
] | null | null | Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string. | Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. | Print exactly one number β the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"heavymetalisheavymetal\n",
"heavymetalismetal\n",
"trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou\n"
] | [
"3",
"2",
"3"
] | In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | [
{
"input": "heavymetalisheavymetal",
"output": "3"
},
{
"input": "heavymetalismetal",
"output": "2"
},
{
"input": "trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou",
"output": "3"
},
{
"input": "fpgzbvhheavymheheavyzmheavyavyebknkhheavyhsbqmm... | 966 | 3,072,000 | 3 | 6,839 | |
0 | none | [
"none"
] | null | null | This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to *x*.
More formally, there is a singly liked list built on an array of *n* elements. Element with index *i* contains two integers: *value**i* is the integer value in this element, and *next**i* that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if *next**i*<=β <=<=-<=1, then *value**next**i*<=><=*value**i*.
You are given the number of elements in the list *n*, the index of the first element *start*, and the integer *x*.
You can make up to 2000 queries of the following two types:
- ? i (1<=β€<=*i*<=β€<=*n*)Β β ask the values *value**i* and *next**i*, - ! ansΒ β give the answer for the problem: the minimum integer, greater than or equal to *x*, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem. | The first line contains three integers *n*, *start*, *x* (1<=β€<=*n*<=β€<=50000, 1<=β€<=*start*<=β€<=*n*, 0<=β€<=*x*<=β€<=109)Β β the number of elements in the list, the index of the first element and the integer *x*. | To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to *x*, or -1, if there is no such integer. | [
"5 3 80\n97 -1\n58 5\n16 2\n81 1\n79 4\n"
] | [
"? 1\n? 2\n? 3\n? 4\n? 5\n! 81"
] | You can read more about singly linked list by the following link: [https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list](https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list)
The illustration for the first sample case. Start and finish elements are marked dark. <img class="tex-graphics" src="https://espresso.codeforces.com/5202ec3b5e896b7db692ff7b80457c26cf6adb32.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "5 3 80\n97 -1\n58 5\n16 2\n81 1\n79 4",
"output": "81\n1003"
},
{
"input": "5 1 6\n1 2\n2 3\n3 4\n4 5\n5 -1",
"output": "-1\n1002"
},
{
"input": "1 1 0\n0 -1",
"output": "0\n2"
},
{
"input": "1 1 2\n0 -1",
"output": "-1\n1002"
},
{
"input": "1 1 100000... | 78 | 921,600 | 0 | 6,871 | |
887 | Ratings and Reality Shows | [
"data structures",
"two pointers"
] | null | null | There are two main kinds of events in the life of top-model: fashion shows and photo shoots. Participating in any of these events affects the rating of appropriate top-model. After each photo shoot model's rating increases by *a* and after each fashion show decreases by *b* (designers do too many experiments nowadays). Moreover, sometimes top-models participates in talk shows. After participating in talk show model becomes more popular and increasing of her rating after photo shoots become *c* and decreasing of her rating after fashion show becomes *d*.
Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will never become negative. Help her to find a suitable moment for participating in the talk show.
Let's assume that model's career begins in moment 0. At that moment Izabella's rating was equal to *start*. If talk show happens in moment *t* if will affect all events in model's life in interval of time [*t*..*t*<=+<=*len*) (including *t* and not including *t*<=+<=*len*), where *len* is duration of influence.
Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will not become become negative before talk show or during period of influence of talk show. Help her to find a suitable moment for participating in the talk show. | In first line there are 7 positive integers *n*, *a*, *b*, *c*, *d*, *start*, *len* (1<=β€<=*n*<=β€<=3Β·105, 0<=β€<=*start*<=β€<=109, 1<=β€<=*a*,<=*b*,<=*c*,<=*d*,<=*len*<=β€<=109), where *n* is a number of fashion shows and photo shoots, *a*, *b*, *c* and *d* are rating changes described above, *start* is an initial rating of model and *len* is a duration of influence of talk show.
In next *n* lines descriptions of events are given. Each of those lines contains two integers *t**i* and *q**i* (1<=β€<=*t**i*<=β€<=109, 0<=β€<=*q*<=β€<=1)Β β moment, in which event happens and type of this event. Type 0 corresponds to the fashion show and type 1Β β to photo shoot.
Events are given in order of increasing *t**i*, all *t**i* are different. | Print one non-negative integer *t*Β β the moment of time in which talk show should happen to make Izabella's rating non-negative before talk show and during period of influence of talk show. If there are multiple answers print smallest of them. If there are no such moments, print <=-<=1. | [
"5 1 1 1 4 0 5\n1 1\n2 1\n3 1\n4 0\n5 0\n",
"1 1 2 1 2 1 2\n1 0\n"
] | [
"6",
"-1"
] | none | [
{
"input": "5 1 1 1 4 0 5\n1 1\n2 1\n3 1\n4 0\n5 0",
"output": "6"
},
{
"input": "1 1 2 1 2 1 2\n1 0",
"output": "-1"
},
{
"input": "10 1 1 1 2 0 10\n1 1\n2 1\n3 0\n4 0\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1",
"output": "5"
}
] | 61 | 0 | 0 | 6,877 | |
687 | The Values You Can Make | [
"dp"
] | null | null | Pari wants to buy an expensive chocolate from Arya. She has *n* coins, the value of the *i*-th coin is *c**i*. The price of the chocolate is *k*, so Pari will take a subset of her coins with sum equal to *k* and give it to Arya.
Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values *x*, such that Arya will be able to make *x* using some subset of coins with the sum *k*.
Formally, Pari wants to know the values *x* such that there exists a subset of coins with the sum *k* such that some subset of this subset has the sum *x*, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum *x* using these coins. | The first line contains two integers *n* and *k* (1<=<=β€<=<=*n*,<=*k*<=<=β€<=<=500)Β β the number of coins and the price of the chocolate, respectively.
Next line will contain *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=500)Β β the values of Pari's coins.
It's guaranteed that one can make value *k* using these coins. | First line of the output must contain a single integer *q*β the number of suitable values *x*. Then print *q* integers in ascending orderΒ β the values that Arya can make for some subset of coins of Pari that pays for the chocolate. | [
"6 18\n5 6 1 10 12 2\n",
"3 50\n25 25 50\n"
] | [
"16\n0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18 \n",
"3\n0 25 50 \n"
] | none | [
{
"input": "6 18\n5 6 1 10 12 2",
"output": "16\n0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18 "
},
{
"input": "3 50\n25 25 50",
"output": "3\n0 25 50 "
},
{
"input": "1 79\n79",
"output": "2\n0 79 "
},
{
"input": "1 114\n114",
"output": "2\n0 114 "
},
{
"input": "5 1\n... | 2,000 | 182,681,600 | 0 | 6,892 | |
911 | Three Garlands | [
"brute force",
"constructive algorithms"
] | null | null | Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.
When a garland is switched on, it periodically changes its state β sometimes it is lit, sometimes not. Formally, if *i*-th garland is switched on during *x*-th second, then it is lit only during seconds *x*, *x*<=+<=*k**i*, *x*<=+<=2*k**i*, *x*<=+<=3*k**i* and so on.
Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers *x*1, *x*2 and *x*3 (not necessarily distinct) so that he will switch on the first garland during *x*1-th second, the second one β during *x*2-th second, and the third one β during *x*3-th second, respectively, and during each second starting from *max*(*x*1,<=*x*2,<=*x*3) at least one garland will be lit.
Help Mishka by telling him if it is possible to do this! | The first line contains three integers *k*1, *k*2 and *k*3 (1<=β€<=*k**i*<=β€<=1500) β time intervals of the garlands. | If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES.
Otherwise, print NO. | [
"2 2 3\n",
"4 2 3\n"
] | [
"YES\n",
"NO\n"
] | In the first example Mishka can choose *x*<sub class="lower-index">1</sub>β=β1, *x*<sub class="lower-index">2</sub>β=β2, *x*<sub class="lower-index">3</sub>β=β1. The first garland will be lit during seconds 1,β3,β5,β7,β..., the second β 2,β4,β6,β8,β..., which already cover all the seconds after the 2-nd one. It doesn't even matter what *x*<sub class="lower-index">3</sub> is chosen. Our choice will lead third to be lit during seconds 1,β4,β7,β10,β..., though.
In the second example there is no way to choose such moments of time, there always be some seconds when no garland is lit. | [
{
"input": "2 2 3",
"output": "YES"
},
{
"input": "4 2 3",
"output": "NO"
},
{
"input": "1499 1498 1500",
"output": "NO"
},
{
"input": "1500 1500 1500",
"output": "NO"
},
{
"input": "100 4 1",
"output": "YES"
},
{
"input": "4 2 4",
"output": "YES"
... | 109 | 20,377,600 | 0 | 6,894 | |
38 | Chess | [
"brute force",
"implementation",
"math"
] | B. Chess | 2 | 256 | Two chess pieces, a rook and a knight, stand on a standard chessboard 8<=Γ<=8 in size. The positions in which they are situated are known. It is guaranteed that none of them beats the other one.
Your task is to find the number of ways to place another knight on the board so that none of the three pieces on the board beat another one. A new piece can only be placed on an empty square. | The first input line contains the description of the rook's position on the board. This description is a line which is 2 in length. Its first symbol is a lower-case Latin letter from a to h, and its second symbol is a number from 1 to 8. The second line contains the description of the knight's position in a similar way. It is guaranteed that their positions do not coincide. | Print a single number which is the required number of ways. | [
"a1\nb2\n",
"a8\nd4\n"
] | [
"44\n",
"38\n"
] | none | [
{
"input": "a1\nb2",
"output": "44"
},
{
"input": "a8\nd4",
"output": "38"
},
{
"input": "a8\nf1",
"output": "42"
},
{
"input": "f8\nh3",
"output": "42"
},
{
"input": "g8\nb7",
"output": "42"
},
{
"input": "h1\ng5",
"output": "42"
},
{
"inp... | 342 | 20,684,800 | 3.875972 | 6,896 |
424 | Biathlon Track | [
"binary search",
"brute force",
"constructive algorithms",
"data structures",
"dp"
] | null | null | Recently an official statement of the world Olympic Committee said that the Olympic Winter Games 2030 will be held in Tomsk. The city officials decided to prepare for the Olympics thoroughly and to build all the necessary Olympic facilities as early as possible. First, a biathlon track will be built.
To construct a biathlon track a plot of land was allocated, which is a rectangle divided into *n*<=Γ<=*m* identical squares. Each of the squares has two coordinates: the number of the row (from 1 to *n*), where it is located, the number of the column (from 1 to *m*), where it is located. Also each of the squares is characterized by its height. During the sports the biathletes will have to move from one square to another. If a biathlete moves from a higher square to a lower one, he makes a descent. If a biathlete moves from a lower square to a higher one, he makes an ascent. If a biathlete moves between two squares with the same height, then he moves on flat ground.
The biathlon track should be a border of some rectangular area of the allocated land on which biathletes will move in the clockwise direction. It is known that on one move on flat ground an average biathlete spends *t**p* seconds, an ascent takes *t**u* seconds, a descent takes *t**d* seconds. The Tomsk Administration wants to choose the route so that the average biathlete passes it in as close to *t* seconds as possible. In other words, the difference between time *t**s* of passing the selected track and *t* should be minimum.
For a better understanding you can look at the first sample of the input data. In this sample *n*<==<=6,<=*m*<==<=7, and the administration wants the track covering time to be as close to *t*<==<=48 seconds as possible, also, *t**p*<==<=3, *t**u*<==<=6 and *t**d*<==<=2. If we consider the rectangle shown on the image by arrows, the average biathlete can move along the boundary in a clockwise direction in exactly 48 seconds. The upper left corner of this track is located in the square with the row number 4, column number 3 and the lower right corner is at square with row number 6, column number 7.
Among other things the administration wants all sides of the rectangle which boundaries will be the biathlon track to consist of no less than three squares and to be completely contained within the selected land.
You are given the description of the given plot of land and all necessary time values. You are to write the program to find the most suitable rectangle for a biathlon track. If there are several such rectangles, you are allowed to print any of them. | The first line of the input contains three integers *n*, *m* and *t* (3<=β€<=*n*,<=*m*<=β€<=300, 1<=β€<=*t*<=β€<=109) β the sizes of the land plot and the desired distance covering time.
The second line also contains three integers *t**p*, *t**u* and *t**d* (1<=β€<=*t**p*,<=*t**u*,<=*t**d*<=β€<=100) β the time the average biathlete needs to cover a flat piece of the track, an ascent and a descent respectively.
Then *n* lines follow, each line contains *m* integers that set the heights of each square of the given plot of land. Each of the height values is a positive integer, not exceeding 106. | In a single line of the output print four positive integers β the number of the row and the number of the column of the upper left corner and the number of the row and the number of the column of the lower right corner of the rectangle that is chosen for the track. | [
"6 7 48\n3 6 2\n5 4 8 3 3 7 9\n4 1 6 8 7 1 1\n1 6 4 6 4 8 6\n7 2 6 1 6 9 4\n1 9 8 6 3 9 2\n4 5 6 8 4 3 7"
] | [
"4 3 6 7\n"
] | none | [] | 62 | 0 | 0 | 6,899 | |
0 | none | [
"none"
] | null | null | The mobile application store has a new game called "Subway Roller".
The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and *n* columns. At the beginning of the game the hero is in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of two or more neighbouring cells in some row of the field.
All trains are moving from right to left at a speed of two cells per second, and the hero runs from left to right at the speed of one cell per second. For simplicity, the game is implemented so that the hero and the trains move in turns. First, the hero moves one cell to the right, then one square up or down, or stays idle. Then all the trains move twice simultaneously one cell to the left. Thus, in one move, Philip definitely makes a move to the right and can move up or down. If at any point, Philip is in the same cell with a train, he loses. If the train reaches the left column, it continues to move as before, leaving the tunnel.
Your task is to answer the question whether there is a sequence of movements of Philip, such that he would be able to get to the rightmost column. | Each test contains from one to ten sets of the input data. The first line of the test contains a single integer *t* (1<=β€<=*t*<=β€<=10 for pretests and tests or *t*<==<=1 for hacks; see the Notes section for details) β the number of sets.
Then follows the description of *t* sets of the input data.
The first line of the description of each set contains two integers *n*,<=*k* (2<=β€<=*n*<=β€<=100,<=1<=β€<=*k*<=β€<=26) β the number of columns on the field and the number of trains. Each of the following three lines contains the sequence of *n* character, representing the row of the field where the game is on. Philip's initial position is marked as 's', he is in the leftmost column. Each of the *k* trains is marked by some sequence of identical uppercase letters of the English alphabet, located in one line. Distinct trains are represented by distinct letters. Character '.' represents an empty cell, that is, the cell that doesn't contain either Philip or the trains. | For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise. | [
"2\n16 4\n...AAAAA........\ns.BBB......CCCCC\n........DDDDD...\n16 4\n...AAAAA........\ns.BBB....CCCCC..\n.......DDDDD....\n",
"2\n10 4\ns.ZZ......\n.....AAABB\n.YYYYYY...\n10 4\ns.ZZ......\n....AAAABB\n.YYYYYY...\n"
] | [
"YES\nNO\n",
"YES\nNO\n"
] | In the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip's path, so he can go straight to the end of the tunnel.
Note that in this problem the challenges are restricted to tests that contain only one testset. | [] | 46 | 0 | 0 | 6,901 | |
0 | none | [
"none"
] | null | null | Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis.
Petya decided to compress tables. He is given a table *a* consisting of *n* rows and *m* columns that is filled with positive integers. He wants to build the table *a*' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row *i* of the initial table *a**i*,<=*j*<=<<=*a**i*,<=*k*, then in the resulting table *a*'*i*,<=*j*<=<<=*a*'*i*,<=*k*, and if *a**i*,<=*j*<==<=*a**i*,<=*k* then *a*'*i*,<=*j*<==<=*a*'*i*,<=*k*. Similarly, if in some column *j* of the initial table *a**i*,<=*j*<=<<=*a**p*,<=*j* then in compressed table *a*'*i*,<=*j*<=<<=*a*'*p*,<=*j* and if *a**i*,<=*j*<==<=*a**p*,<=*j* then *a*'*i*,<=*j*<==<=*a*'*p*,<=*j*.
Because large values require more space to store them, the maximum value in *a*' should be as small as possible.
Petya is good in theory, however, he needs your help to implement the algorithm. | The first line of the input contains two integers *n* and *m* (, the number of rows and the number of columns of the table respectively.
Each of the following *n* rows contain *m* integers *a**i*,<=*j* (1<=β€<=*a**i*,<=*j*<=β€<=109) that are the values in the table. | Output the compressed table in form of *n* lines each containing *m* integers.
If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them. | [
"2 2\n1 2\n3 4\n",
"4 3\n20 10 30\n50 40 30\n50 60 70\n90 80 70\n"
] | [
"1 2\n2 3\n",
"2 1 3\n5 4 3\n5 6 7\n9 8 7\n"
] | In the first sample test, despite the fact *a*<sub class="lower-index">1,β2</sub>ββ β*a*<sub class="lower-index">21</sub>, they are not located in the same row or column so they may become equal after the compression. | [] | 46 | 0 | 0 | 6,903 | |
893 | Credit Card | [
"data structures",
"dp",
"greedy",
"implementation"
] | null | null | Recenlty Luba got a credit card and started to use it. Let's consider *n* consecutive days Luba uses the card.
She starts with 0 money on her account.
In the evening of *i*-th day a transaction *a**i* occurs. If *a**i*<=><=0, then *a**i* bourles are deposited to Luba's account. If *a**i*<=<<=0, then *a**i* bourles are withdrawn. And if *a**i*<==<=0, then the amount of money on Luba's account is checked.
In the morning of any of *n* days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed *d*.
It can happen that the amount of money goes greater than *d* by some transaction in the evening. In this case answer will be Β«-1Β».
Luba must not exceed this limit, and also she wants that every day her account is checked (the days when *a**i*<==<=0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! | The first line contains two integers *n*, *d* (1<=β€<=*n*<=β€<=105, 1<=β€<=*d*<=β€<=109) βthe number of days and the money limitation.
The second line contains *n* integer numbers *a*1,<=*a*2,<=... *a**n* (<=-<=104<=β€<=*a**i*<=β€<=104), where *a**i* represents the transaction in *i*-th day. | Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. | [
"5 10\n-1 5 0 -5 3\n",
"3 4\n-10 0 20\n",
"5 10\n-5 0 10 -11 0\n"
] | [
"0\n",
"-1\n",
"2\n"
] | none | [
{
"input": "5 10\n-1 5 0 -5 3",
"output": "0"
},
{
"input": "3 4\n-10 0 20",
"output": "-1"
},
{
"input": "5 10\n-5 0 10 -11 0",
"output": "2"
},
{
"input": "5 13756\n-2 -9 -10 0 10",
"output": "1"
},
{
"input": "20 23036\n-1 1 -1 -1 -1 -1 1 -1 -1 0 0 1 1 0 0 1 0 ... | 77 | 0 | 0 | 6,908 | |
142 | Help General | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Once upon a time in the Kingdom of Far Far Away lived Sir Lancelot, the chief Royal General. He was very proud of his men and he liked to invite the King to come and watch drill exercises which demonstrated the fighting techniques and tactics of the squad he was in charge of. But time went by and one day Sir Lancelot had a major argument with the Fairy Godmother (there were rumors that the argument occurred after the general spoke badly of the Godmother's flying techniques. That seemed to hurt the Fairy Godmother very deeply).
As the result of the argument, the Godmother put a rather strange curse upon the general. It sounded all complicated and quite harmless: "If the squared distance between some two soldiers equals to 5, then those soldiers will conflict with each other!"
The drill exercises are held on a rectangular *n*<=Γ<=*m* field, split into *nm* square 1<=Γ<=1 segments for each soldier. Thus, the square of the distance between the soldiers that stand on squares (*x*1,<=*y*1) and (*x*2,<=*y*2) equals exactly (*x*1<=-<=*x*2)2<=+<=(*y*1<=-<=*y*2)2. Now not all *nm* squad soldiers can participate in the drill exercises as it was before the Fairy Godmother's curse. Unless, of course, the general wants the soldiers to fight with each other or even worse... For example, if he puts a soldier in the square (2,<=2), then he cannot put soldiers in the squares (1,<=4), (3,<=4), (4,<=1) and (4,<=3) β each of them will conflict with the soldier in the square (2,<=2).
Your task is to help the general. You are given the size of the drill exercise field. You are asked to calculate the maximum number of soldiers that can be simultaneously positioned on this field, so that no two soldiers fall under the Fairy Godmother's curse. | The single line contains space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000) that represent the size of the drill exercise field. | Print the desired maximum number of warriors. | [
"2 4\n",
"3 4\n"
] | [
"4",
"6"
] | In the first sample test Sir Lancelot can place his 4 soldiers on the 2βΓβ4 court as follows (the soldiers' locations are marked with gray circles on the scheme):
In the second sample test he can place 6 soldiers on the 3βΓβ4 site in the following manner: | [
{
"input": "2 4",
"output": "4"
},
{
"input": "3 4",
"output": "6"
},
{
"input": "4 4",
"output": "8"
},
{
"input": "4 3",
"output": "6"
},
{
"input": "4 2",
"output": "4"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "3 5",
"output"... | 248 | 2,252,800 | -1 | 6,920 | |
592 | Super M | [
"dfs and similar",
"dp",
"graphs",
"trees"
] | null | null | Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforcesβ superhero. Byteforces is a country that consists of *n* cities, connected by *n*<=-<=1 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go from any city to any other city using only the given roads. There are *m* cities being attacked by humans. So Ari... we meant Super M have to immediately go to each of the cities being attacked to scare those bad humans. Super M can pass from one city to another only using the given roads. Moreover, passing through one road takes her exactly one kron - the time unit used in Byteforces.
However, Super M is not on Byteforces now - she is attending a training camp located in a nearby country Codeforces. Fortunately, there is a special device in Codeforces that allows her to instantly teleport from Codeforces to any city of Byteforces. The way back is too long, so for the purpose of this problem teleportation is used exactly once.
You are to help Super M, by calculating the city in which she should teleport at the beginning in order to end her job in the minimum time (measured in krons). Also, provide her with this time so she can plan her way back to Codeforces. | The first line of the input contains two integers *n* and *m* (1<=β€<=*m*<=β€<=*n*<=β€<=123456) - the number of cities in Byteforces, and the number of cities being attacked respectively.
Then follow *n*<=-<=1 lines, describing the road system. Each line contains two city numbers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*) - the ends of the road *i*.
The last line contains *m* distinct integers - numbers of cities being attacked. These numbers are given in no particular order. | First print the number of the city Super M should teleport to. If there are many possible optimal answers, print the one with the lowest city number.
Then print the minimum possible time needed to scare all humans in cities being attacked, measured in Krons.
Note that the correct answer is always unique. | [
"7 2\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n2 7\n",
"6 4\n1 2\n2 3\n2 4\n4 5\n4 6\n2 4 5 6\n"
] | [
"2\n3\n",
"2\n4\n"
] | In the first sample, there are two possibilities to finish the Super M's job in 3 krons. They are:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/93d3c0306b529e9c2324f68158ca2156587473a2.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/df80aa84591eaa7b9f52c88cc43b5f7da5bfead3.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
However, you should choose the first one as it starts in the city with the lower number. | [
{
"input": "7 2\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n2 7",
"output": "2\n3"
},
{
"input": "6 4\n1 2\n2 3\n2 4\n4 5\n4 6\n2 4 5 6",
"output": "2\n4"
},
{
"input": "2 1\n2 1\n1",
"output": "1\n0"
},
{
"input": "1 1\n1",
"output": "1\n0"
},
{
"input": "10 2\n6 9\n6 2\n1 6\... | 794 | 21,606,400 | 0 | 6,927 | |
854 | Maxim Buys an Apartment | [
"constructive algorithms",
"math"
] | null | null | Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has *n* apartments that are numbered from 1 to *n* and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale.
Maxim often visits his neighbors, so apartment is good for him if it is available for sale and there is at least one already inhabited apartment adjacent to it. Maxim knows that there are exactly *k* already inhabited apartments, but he doesn't know their indices yet.
Find out what could be the minimum possible and the maximum possible number of apartments that are good for Maxim. | The only line of the input contains two integers: *n* and *k* (1<=β€<=*n*<=β€<=109, 0<=β€<=*k*<=β€<=*n*). | Print the minimum possible and the maximum possible number of apartments good for Maxim. | [
"6 3\n"
] | [
"1 3\n"
] | In the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 1, 2 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 1, 3 and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good. | [
{
"input": "6 3",
"output": "1 3"
},
{
"input": "10 1",
"output": "1 2"
},
{
"input": "10 9",
"output": "1 1"
},
{
"input": "8 0",
"output": "0 0"
},
{
"input": "8 8",
"output": "0 0"
},
{
"input": "966871928 890926970",
"output": "1 75944958"
},... | 77 | 2,048,000 | -1 | 6,930 | |
0 | none | [
"none"
] | null | null | Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an *h*<=Γ<=*w* field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?
The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process. | The first line of the input contains three integers: *h*,<=*w*,<=*n* β the sides of the board and the number of black cells (1<=β€<=*h*,<=*w*<=β€<=105,<=1<=β€<=*n*<=β€<=2000).
Next *n* lines contain the description of black cells. The *i*-th of these lines contains numbers *r**i*,<=*c**i* (1<=β€<=*r**i*<=β€<=*h*,<=1<=β€<=*c**i*<=β€<=*w*) β the number of the row and column of the *i*-th cell.
It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct. | Print a single line β the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo 109<=+<=7. | [
"3 4 2\n2 2\n2 3\n",
"100 100 3\n15 16\n16 15\n99 88\n"
] | [
"2\n",
"545732279\n"
] | none | [
{
"input": "3 4 2\n2 2\n2 3",
"output": "2"
},
{
"input": "100 100 3\n15 16\n16 15\n99 88",
"output": "545732279"
},
{
"input": "1000 1000 4\n50 50\n51 50\n50 51\n51 51",
"output": "899660737"
},
{
"input": "100000 100000 4\n50001 50001\n50000 50000\n50000 50001\n50001 50000"... | 61 | 409,600 | 0 | 6,937 | |
405 | Domino Effect | [] | null | null | Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a "domino show".
Chris arranges *n* dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.
After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process.
Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process! | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=3000), the number of the dominoes in the line. The next line contains a character string *s* of length *n*. The *i*-th character of the string *s**i* is equal to
- "L", if the *i*-th domino has been pushed to the left; - "R", if the *i*-th domino has been pushed to the right; - ".", if the *i*-th domino has not been pushed.
It is guaranteed that if *s**i*<==<=*s**j*<==<="L" and *i*<=<<=*j*, then there exists such *k* that *i*<=<<=*k*<=<<=*j* and *s**k*<==<="R"; if *s**i*<==<=*s**j*<==<="R" and *i*<=<<=*j*, then there exists such *k* that *i*<=<<=*k*<=<<=*j* and *s**k*<==<="L". | Output a single integer, the number of the dominoes that remain vertical at the end of the process. | [
"14\n.L.R...LR..L..\n",
"5\nR....\n",
"1\n.\n"
] | [
"4\n",
"0\n",
"1\n"
] | The first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.
In the second example case, all pieces fall down since the first piece topples all the other pieces.
In the last example case, a single piece has not been pushed in either direction. | [
{
"input": "14\n.L.R...LR..L..",
"output": "4"
},
{
"input": "1\n.",
"output": "1"
},
{
"input": "1\nL",
"output": "0"
},
{
"input": "1\nR",
"output": "0"
},
{
"input": "2\nL.",
"output": "1"
},
{
"input": "2\nRL",
"output": "0"
},
{
"input... | 62 | 1,638,400 | 3 | 6,948 | |
925 | Resource Distribution | [
"binary search",
"implementation",
"sortings"
] | null | null | One department of some software company has $n$ servers of different specifications. Servers are indexed with consecutive integers from $1$ to $n$. Suppose that the specifications of the $j$-th server may be expressed with a single integer number $c_j$ of artificial resource units.
In order for production to work, it is needed to deploy two services $S_1$ and $S_2$ to process incoming requests using the servers of the department. Processing of incoming requests of service $S_i$ takes $x_i$ resource units.
The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service $S_i$ is deployed using $k_i$ servers, then the load is divided equally between these servers and each server requires only $x_i / k_i$ (that may be a fractional number) resource units.
Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides.
Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services. | The first line contains three integers $n$, $x_1$, $x_2$ ($2 \leq n \leq 300\,000$, $1 \leq x_1, x_2 \leq 10^9$)Β β the number of servers that the department may use, and resource units requirements for each of the services.
The second line contains $n$ space-separated integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^9$)Β β the number of resource units provided by each of the servers. | If it is impossible to deploy both services using the given servers, print the only word "No" (without the quotes).
Otherwise print the word "Yes" (without the quotes).
In the second line print two integers $k_1$ and $k_2$ ($1 \leq k_1, k_2 \leq n$)Β β the number of servers used for each of the services.
In the third line print $k_1$ integers, the indices of the servers that will be used for the first service.
In the fourth line print $k_2$ integers, the indices of the servers that will be used for the second service.
No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them. | [
"6 8 16\n3 5 2 9 8 7\n",
"4 20 32\n21 11 11 12\n",
"4 11 32\n5 5 16 16\n",
"5 12 20\n7 8 4 11 9\n"
] | [
"Yes\n3 2\n1 2 6\n5 4",
"Yes\n1 3\n1\n2 3 4\n",
"No\n",
"No\n"
] | In the first sample test each of the servers 1, 2 and 6 will will provide $8 / 3 = 2.(6)$ resource units and each of the servers 5, 4 will provide $16 / 2 = 8$ resource units.
In the second sample test the first server will provide $20$ resource units and each of the remaining servers will provide $32 / 3 = 10.(6)$ resource units. | [
{
"input": "6 8 16\n3 5 2 9 8 7",
"output": "Yes\n4 2\n3 1 2 6\n5 4"
},
{
"input": "4 20 32\n21 11 11 12",
"output": "Yes\n1 3\n1\n2 3 4"
},
{
"input": "4 11 32\n5 5 16 16",
"output": "No"
},
{
"input": "5 12 20\n7 8 4 11 9",
"output": "No"
},
{
"input": "2 1 1\n1... | 61 | 7,065,600 | 0 | 6,955 | |
858 | Which floor? | [
"brute force",
"implementation"
] | null | null | In a building where Polycarp lives there are equal number of flats on each floor. Unfortunately, Polycarp don't remember how many flats are on each floor, but he remembers that the flats are numbered from 1 from lower to upper floors. That is, the first several flats are on the first floor, the next several flats are on the second and so on. Polycarp don't remember the total number of flats in the building, so you can consider the building to be infinitely high (i.e. there are infinitely many floors). Note that the floors are numbered from 1.
Polycarp remembers on which floors several flats are located. It is guaranteed that this information is not self-contradictory. It means that there exists a building with equal number of flats on each floor so that the flats from Polycarp's memory have the floors Polycarp remembers.
Given this information, is it possible to restore the exact floor for flat *n*? | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=100, 0<=β€<=*m*<=β€<=100), where *n* is the number of the flat you need to restore floor for, and *m* is the number of flats in Polycarp's memory.
*m* lines follow, describing the Polycarp's memory: each of these lines contains a pair of integers *k**i*,<=*f**i* (1<=β€<=*k**i*<=β€<=100, 1<=β€<=*f**i*<=β€<=100), which means that the flat *k**i* is on the *f**i*-th floor. All values *k**i* are distinct.
It is guaranteed that the given information is not self-contradictory. | Print the number of the floor in which the *n*-th flat is located, if it is possible to determine it in a unique way. Print -1 if it is not possible to uniquely restore this floor. | [
"10 3\n6 2\n2 1\n7 3\n",
"8 4\n3 1\n6 2\n5 2\n2 1\n"
] | [
"4\n",
"-1\n"
] | In the first example the 6-th flat is on the 2-nd floor, while the 7-th flat is on the 3-rd, so, the 6-th flat is the last on its floor and there are 3 flats on each floor. Thus, the 10-th flat is on the 4-th floor.
In the second example there can be 3 or 4 flats on each floor, so we can't restore the floor for the 8-th flat. | [
{
"input": "10 3\n6 2\n2 1\n7 3",
"output": "4"
},
{
"input": "8 4\n3 1\n6 2\n5 2\n2 1",
"output": "-1"
},
{
"input": "8 3\n7 2\n6 2\n1 1",
"output": "2"
},
{
"input": "4 2\n8 3\n3 1",
"output": "2"
},
{
"input": "11 4\n16 4\n11 3\n10 3\n15 4",
"output": "3"
... | 46 | 0 | 0 | 6,959 | |
0 | none | [
"none"
] | null | null | Limak is a little polar bear. He doesn't have many toys and thus he often plays with polynomials.
He considers a polynomial valid if its degree is *n* and its coefficients are integers not exceeding *k* by the absolute value. More formally:
Let *a*0,<=*a*1,<=...,<=*a**n* denote the coefficients, so . Then, a polynomial *P*(*x*) is valid if all the following conditions are satisfied:
- *a**i* is integer for every *i*; - |*a**i*|<=β€<=*k* for every *i*; - *a**n*<=β <=0.
Limak has recently got a valid polynomial *P* with coefficients *a*0,<=*a*1,<=*a*2,<=...,<=*a**n*. He noticed that *P*(2)<=β <=0 and he wants to change it. He is going to change one coefficient to get a valid polynomial *Q* of degree *n* that *Q*(2)<==<=0. Count the number of ways to do so. You should count two ways as a distinct if coefficients of target polynoms differ. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=200<=000,<=1<=β€<=*k*<=β€<=109)Β β the degree of the polynomial and the limit for absolute values of coefficients.
The second line contains *n*<=+<=1 integers *a*0,<=*a*1,<=...,<=*a**n* (|*a**i*|<=β€<=*k*,<=*a**n*<=β <=0)Β β describing a valid polynomial . It's guaranteed that *P*(2)<=β <=0. | Print the number of ways to change one coefficient to get a valid polynomial *Q* that *Q*(2)<==<=0. | [
"3 1000000000\n10 -9 -3 5\n",
"3 12\n10 -9 -3 5\n",
"2 20\n14 -7 19\n"
] | [
"3\n",
"2\n",
"0\n"
] | In the first sample, we are given a polynomial *P*(*x*)β=β10β-β9*x*β-β3*x*<sup class="upper-index">2</sup>β+β5*x*<sup class="upper-index">3</sup>.
Limak can change one coefficient in three ways:
1. He can set *a*<sub class="lower-index">0</sub>β=ββ-β10. Then he would get *Q*(*x*)β=ββ-β10β-β9*x*β-β3*x*<sup class="upper-index">2</sup>β+β5*x*<sup class="upper-index">3</sup> and indeed *Q*(2)β=ββ-β10β-β18β-β12β+β40β=β0. 1. Or he can set *a*<sub class="lower-index">2</sub>β=ββ-β8. Then *Q*(*x*)β=β10β-β9*x*β-β8*x*<sup class="upper-index">2</sup>β+β5*x*<sup class="upper-index">3</sup> and indeed *Q*(2)β=β10β-β18β-β32β+β40β=β0. 1. Or he can set *a*<sub class="lower-index">1</sub>β=ββ-β19. Then *Q*(*x*)β=β10β-β19*x*β-β3*x*<sup class="upper-index">2</sup>β+β5*x*<sup class="upper-index">3</sup> and indeed *Q*(2)β=β10β-β38β-β12β+β40β=β0.
In the second sample, we are given the same polynomial. This time though, *k* is equal to 12 instead of 10<sup class="upper-index">9</sup>. Two first of ways listed above are still valid but in the third way we would get |*a*<sub class="lower-index">1</sub>|β>β*k* what is not allowed. Thus, the answer is 2 this time. | [
{
"input": "3 1000000000\n10 -9 -3 5",
"output": "3"
},
{
"input": "3 12\n10 -9 -3 5",
"output": "2"
},
{
"input": "2 20\n14 -7 19",
"output": "0"
},
{
"input": "5 5\n0 -4 -2 -2 0 5",
"output": "1"
},
{
"input": "6 10\n-2 -1 7 -3 2 7 -6",
"output": "2"
},
... | 61 | 5,222,400 | 0 | 6,969 | |
66 | Petya and His Friends | [
"constructive algorithms",
"math",
"number theory"
] | D. Petya and His Friends | 2 | 256 | Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to *n*.
Let us remind you the definition of the greatest common divisor: *GCD*(*a*1,<=...,<=*a**k*)<==<=*d*, where *d* represents such a maximal positive number that each *a**i* (1<=β€<=*i*<=β€<=*k*) is evenly divisible by *d*. At that, we assume that all *a**i*'s are greater than zero.
Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives *a*1 sweets, the 2-nd one gives *a*2 sweets, ..., the *n*-th one gives *a**n* sweets. At the same time, for any *i* and *j* (1<=β€<=*i*,<=*j*<=β€<=*n*) they want the *GCD*(*a**i*,<=*a**j*) not to be equal to 1. However, they also want the following condition to be satisfied: *GCD*(*a*1,<=*a*2,<=...,<=*a**n*)<==<=1. One more: all the *a**i* should be distinct.
Help the friends to choose the suitable numbers *a*1,<=...,<=*a**n*. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=50). | If there is no answer, print "-1" without quotes. Otherwise print a set of *n* distinct positive numbers *a*1,<=*a*2,<=...,<=*a**n*. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them.
Do not forget, please, that all of the following conditions must be true:
- For every *i* and *j* (1<=β€<=*i*,<=*j*<=β€<=*n*): *GCD*(*a**i*,<=*a**j*)<=β <=1- *GCD*(*a*1,<=*a*2,<=...,<=*a**n*)<==<=1- For every *i* and *j* (1<=β€<=*i*,<=*j*<=β€<=*n*,<=*i*<=β <=*j*): *a**i*<=β <=*a**j*
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | [
"3\n",
"4\n"
] | [
"99\n55\n11115\n",
"385\n360\n792\n8360\n"
] | none | [
{
"input": "3",
"output": "15\n10\n6"
},
{
"input": "4",
"output": "105\n70\n42\n30"
},
{
"input": "5",
"output": "1155\n770\n462\n330\n210"
},
{
"input": "6",
"output": "15015\n10010\n6006\n4290\n2730\n2310"
},
{
"input": "7",
"output": "255255\n170170\n10210... | 154 | 2,355,200 | 3.957113 | 6,980 |
11 | How Many Squares? | [
"implementation"
] | C. How Many Squares? | 2 | 64 | You are given a 0-1 rectangular matrix. What is the number of squares in it? A square is a solid square frame (border) with linewidth equal to 1. A square should be at least 2<=Γ<=2. We are only interested in two types of squares:
1. squares with each side parallel to a side of the matrix; 1. squares with each side parallel to a diagonal of the matrix.
Regardless of type, a square must contain at least one 1 and can't touch (by side or corner) any foreign 1. Of course, the lengths of the sides of each square should be equal.
How many squares are in the given matrix? | The first line contains integer *t* (1<=β€<=*t*<=β€<=10000), where *t* is the number of test cases in the input. Then test cases follow. Each case starts with a line containing integers *n* and *m* (2<=β€<=*n*,<=*m*<=β€<=250), where *n* is the number of rows and *m* is the number of columns. The following *n* lines contain *m* characters each (0 or 1).
The total number of characters in all test cases doesn't exceed 106 for any input file. | You should output exactly *t* lines, with the answer to the *i*-th test case on the *i*-th line. | [
"2\n8 8\n00010001\n00101000\n01000100\n10000010\n01000100\n00101000\n11010011\n11000011\n10 10\n1111111000\n1000001000\n1011001000\n1011001010\n1000001101\n1001001010\n1010101000\n1001001000\n1000001000\n1111111000\n",
"1\n12 11\n11111111111\n10000000001\n10111111101\n10100000101\n10101100101\n10101100101\n101000... | [
"1\n2\n",
"3\n"
] | none | [
{
"input": "2\n8 8\n00010001\n00101000\n01000100\n10000010\n01000100\n00101000\n11010011\n11000011\n10 10\n1111111000\n1000001000\n1011001000\n1011001010\n1000001101\n1001001010\n1010101000\n1001001000\n1000001000\n1111111000",
"output": "1\n2"
},
{
"input": "1\n12 11\n11111111111\n10000000001\n1011... | 2,000 | 6,963,200 | 0 | 6,982 |
493 | Vasya and Basketball | [
"binary search",
"brute force",
"data structures",
"implementation",
"sortings",
"two pointers"
] | null | null | Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of *d* meters, and a throw is worth 3 points if the distance is larger than *d* meters, where *d* is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of *d*. Help him to do that. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the number of throws of the first team. Then follow *n* integer numbers β the distances of throws *a**i* (1<=β€<=*a**i*<=β€<=2Β·109).
Then follows number *m* (1<=β€<=*m*<=β€<=2Β·105) β the number of the throws of the second team. Then follow *m* integer numbers β the distances of throws of *b**i* (1<=β€<=*b**i*<=β€<=2Β·109). | Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction *a*<=-<=*b* is maximum. If there are several such scores, find the one in which number *a* is maximum. | [
"3\n1 2 3\n2\n5 6\n",
"5\n6 7 8 9 10\n5\n1 2 3 4 5\n"
] | [
"9:6\n",
"15:10\n"
] | none | [
{
"input": "3\n1 2 3\n2\n5 6",
"output": "9:6"
},
{
"input": "5\n6 7 8 9 10\n5\n1 2 3 4 5",
"output": "15:10"
},
{
"input": "5\n1 2 3 4 5\n5\n6 7 8 9 10",
"output": "15:15"
},
{
"input": "3\n1 2 3\n3\n6 4 5",
"output": "9:9"
},
{
"input": "10\n1 2 3 4 5 6 7 8 9 10... | 405 | 99,635,200 | 0 | 6,991 | |
713 | Sonya and Queries | [
"data structures",
"implementation"
] | null | null | Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her *t* queries, each of one of the following type:
1. <=+<= *a**i*Β β add non-negative integer *a**i* to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 1. <=-<= *a**i*Β β delete a single occurrence of non-negative integer *a**i* from the multiset. It's guaranteed, that there is at least one *a**i* in the multiset. 1. ? *s*Β β count the number of integers in the multiset (with repetitions) that match some pattern *s* consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer *x* matches the pattern *s*, if the parity of the *i*-th from the right digit in decimal notation matches the *i*-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left.
For example, if the pattern is *s*<==<=010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. | The first line of the input contains an integer *t* (1<=β€<=*t*<=β€<=100<=000)Β β the number of operation Sonya has to perform.
Next *t* lines provide the descriptions of the queries in order they appear in the input file. The *i*-th row starts with a character *c**i*Β β the type of the corresponding operation. If *c**i* is equal to '+' or '-' then it's followed by a space and an integer *a**i* (0<=β€<=*a**i*<=<<=1018) given without leading zeroes (unless it's 0). If *c**i* equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18.
It's guaranteed that there will be at least one query of type '?'.
It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. | For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. | [
"12\n+ 1\n+ 241\n? 1\n+ 361\n- 241\n? 0101\n+ 101\n? 101\n- 101\n? 101\n+ 4000\n? 0\n",
"4\n+ 200\n+ 200\n- 200\n? 0\n"
] | [
"2\n1\n2\n1\n1\n",
"1\n"
] | Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input.
1. 1 and 241. 1. 361. 1. 101 and 361. 1. 361. 1. 4000. | [
{
"input": "12\n+ 1\n+ 241\n? 1\n+ 361\n- 241\n? 0101\n+ 101\n? 101\n- 101\n? 101\n+ 4000\n? 0",
"output": "2\n1\n2\n1\n1"
},
{
"input": "4\n+ 200\n+ 200\n- 200\n? 0",
"output": "1"
},
{
"input": "20\n+ 61\n+ 99\n+ 51\n+ 70\n+ 7\n+ 34\n+ 71\n+ 86\n+ 68\n+ 39\n+ 78\n+ 81\n+ 89\n? 10\n? 00... | 77 | 4,198,400 | -1 | 6,995 | |
630 | Parking Lot | [
"combinatorics",
"math"
] | null | null | To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes.
The parking lot before the office consists of one line of (2*n*<=-<=2) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever.
Looking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly *n* successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way. | The only line of the input contains one integer *n* (3<=β€<=*n*<=β€<=30) β the amount of successive cars of the same make. | Output one integer β the number of ways to fill the parking lot by cars of four makes using the described way. | [
"3\n"
] | [
"24"
] | Let's denote car makes in the following way: A β Aston Martin, B β Bentley, M β Mercedes-Maybach, Z β Zaporozhets. For *n*β=β3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM
Originally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City. | [
{
"input": "3",
"output": "24"
},
{
"input": "4",
"output": "132"
},
{
"input": "5",
"output": "672"
},
{
"input": "6",
"output": "3264"
},
{
"input": "7",
"output": "15360"
},
{
"input": "12",
"output": "27525120"
},
{
"input": "15",
"... | 15 | 409,600 | 0 | 7,003 | |
366 | Dima and To-do List | [
"brute force",
"implementation"
] | null | null | You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.
Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete *k*<=-<=1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.
Overall, Dima has *n* tasks to do, each task has a unique number from 1 to *n*. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.
Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible. | The first line of the input contains two integers *n*,<=*k*Β (1<=β€<=*k*<=β€<=*n*<=β€<=105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n*Β (1<=β€<=*a**i*<=β€<=103), where *a**i* is the power Inna tells Dima off with if she is present in the room while he is doing the *i*-th task.
It is guaranteed that *n* is divisible by *k*. | In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do. | [
"6 2\n3 2 1 6 5 4\n",
"10 5\n1 3 5 7 9 9 4 1 8 5\n"
] | [
"1\n",
"3\n"
] | Explanation of the first example.
If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as *k* = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.
Explanation of the second example.
In the second example *k* = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3. | [
{
"input": "6 2\n3 2 1 6 5 4",
"output": "1"
},
{
"input": "10 5\n1 3 5 7 9 9 4 1 8 5",
"output": "3"
},
{
"input": "20 4\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "1"
},
{
"input": "10 10\n8 4 5 7 6 9 2 2 3 5",
"output": "7"
},
{
"input": "50 10\n1 2 3... | 171 | 29,388,800 | 3 | 7,018 | |
280 | k-Maximum Subsequence Sum | [
"data structures",
"flows",
"graphs",
"implementation"
] | null | null | Consider integer sequence *a*1,<=*a*2,<=...,<=*a**n*. You should run queries of two types:
- The query format is "0 *i* *val*". In reply to this query you should make the following assignment: *a**i*<==<=*val*. - The query format is "1 *l* *r* *k*". In reply to this query you should print the maximum sum of at most *k* non-intersecting subsegments of sequence *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r*. Formally, you should choose at most *k* pairs of integers (*x*1,<=*y*1),<=(*x*2,<=*y*2),<=...,<=(*x**t*,<=*y**t*) (*l*<=β€<=*x*1<=β€<=*y*1<=<<=*x*2<=β€<=*y*2<=<<=...<=<<=*x**t*<=β€<=*y**t*<=β€<=*r*;Β *t*<=β€<=*k*) such that the sum *a**x*1<=+<=*a**x*1<=+<=1<=+<=...<=+<=*a**y*1<=+<=*a**x*2<=+<=*a**x*2<=+<=1<=+<=...<=+<=*a**y*2<=+<=...<=+<=*a**x**t*<=+<=*a**x**t*<=+<=1<=+<=...<=+<=*a**y**t* is as large as possible. Note that you should choose at most *k* subsegments. Particularly, you can choose 0 subsegments. In this case the described sum considered equal to zero. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105), showing how many numbers the sequence has. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=β€<=500).
The third line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of queries. The next *m* lines contain the queries in the format, given in the statement.
All changing queries fit into limits: 1<=β€<=*i*<=β€<=*n*, |*val*|<=β€<=500.
All queries to count the maximum sum of at most *k* non-intersecting subsegments fit into limits: 1<=β€<=*l*<=β€<=*r*<=β€<=*n*, 1<=β€<=*k*<=β€<=20. It is guaranteed that the number of the queries to count the maximum sum of at most *k* non-intersecting subsegments doesn't exceed 10000. | For each query to count the maximum sum of at most *k* non-intersecting subsegments print the reply β the maximum sum. Print the answers to the queries in the order, in which the queries follow in the input. | [
"9\n9 -8 9 -1 -1 -1 9 -8 9\n3\n1 1 9 1\n1 1 9 2\n1 4 6 3\n",
"15\n-4 8 -3 -10 10 4 -7 -7 0 -6 3 8 -10 7 2\n15\n1 3 9 2\n1 6 12 1\n0 6 5\n0 10 -7\n1 4 9 1\n1 7 9 1\n0 10 -3\n1 4 10 2\n1 3 13 2\n1 4 11 2\n0 15 -9\n0 13 -9\n0 11 -10\n1 5 14 2\n1 6 12 1\n"
] | [
"17\n25\n0\n",
"14\n11\n15\n0\n15\n26\n18\n23\n8\n"
] | In the first query of the first example you can select a single pair (1,β9). So the described sum will be 17.
Look at the second query of the first example. How to choose two subsegments? (1, 3) and (7, 9)? Definitely not, the sum we could get from (1, 3) and (7, 9) is 20, against the optimal configuration (1, 7) and (9, 9) with 25.
The answer to the third query is 0, we prefer select nothing if all of the numbers in the given interval are negative. | [] | 46 | 0 | 0 | 7,038 | |
9 | Running Student | [
"brute force",
"geometry",
"implementation"
] | B. Running Student | 1 | 64 | And again a misfortune fell on Poor Student. He is being late for an exam.
Having rushed to a bus stop that is in point (0,<=0), he got on a minibus and they drove along a straight line, parallel to axis *OX*, in the direction of increasing *x*.
Poor Student knows the following:
- during one run the minibus makes *n* stops, the *i*-th stop is in point (*x**i*,<=0) - coordinates of all the stops are different - the minibus drives at a constant speed, equal to *v**b* - it can be assumed the passengers get on and off the minibus at a bus stop momentarily - Student can get off the minibus only at a bus stop - Student will have to get off the minibus at a terminal stop, if he does not get off earlier - the University, where the exam will be held, is in point (*x**u*,<=*y**u*) - Student can run from a bus stop to the University at a constant speed *v**s* as long as needed - a distance between two points can be calculated according to the following formula: - Student is already on the minibus, so, he cannot get off at the first bus stop
Poor Student wants to get to the University as soon as possible. Help him to choose the bus stop, where he should get off. If such bus stops are multiple, choose the bus stop closest to the University. | The first line contains three integer numbers: 2<=β€<=*n*<=β€<=100, 1<=β€<=*v**b*,<=*v**s*<=β€<=1000. The second line contains *n* non-negative integers in ascending order: coordinates *x**i* of the bus stop with index *i*. It is guaranteed that *x*1 equals to zero, and *x**n*<=β€<=105. The third line contains the coordinates of the University, integers *x**u* and *y**u*, not exceeding 105 in absolute value. | In the only line output the answer to the problem β index of the optimum bus stop. | [
"4 5 2\n0 2 4 6\n4 1\n",
"2 1 1\n0 100000\n100000 100000\n"
] | [
"3",
"2"
] | As you know, students are a special sort of people, and minibuses usually do not hurry. That's why you should not be surprised, if Student's speed is higher than the speed of the minibus. | [
{
"input": "4 5 2\n0 2 4 6\n4 1",
"output": "3"
},
{
"input": "2 1 1\n0 100000\n100000 100000",
"output": "2"
},
{
"input": "6 5 1\n0 1 2 3 4 5\n0 0",
"output": "2"
},
{
"input": "4 100 10\n0 118 121 178\n220 220",
"output": "4"
},
{
"input": "4 3 3\n0 6 8 10\n7 -... | 124 | 0 | 0 | 7,061 |
155 | Combination | [
"greedy",
"sortings"
] | null | null | Ilya plays a card game by the following rules.
A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number *a**i*, and the bottom contains number *b**i*, then when the player is playing the card, he gets *a**i* points and also gets the opportunity to play additional *b**i* cards. After the playing the card is discarded.
More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number *b**i*, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.
Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards? | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=1000) β the number of cards Ilya has.
Each of the next *n* lines contains two non-negative space-separated integers β *a**i* and *b**i* (0<=β€<=*a**i*,<=*b**i*<=β€<=104) β the numbers, written at the top and the bottom of the *i*-th card correspondingly. | Print the single number β the maximum number of points you can score in one round by the described rules. | [
"2\n1 0\n2 0\n",
"3\n1 0\n2 0\n0 2\n"
] | [
"2\n",
"3\n"
] | In the first sample none of two cards brings extra moves, so you should play the one that will bring more points.
In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards. | [
{
"input": "2\n1 0\n2 0",
"output": "2"
},
{
"input": "3\n1 0\n2 0\n0 2",
"output": "3"
},
{
"input": "5\n0 0\n2 0\n2 0\n3 0\n5 1",
"output": "8"
},
{
"input": "7\n9 1\n8 1\n9 0\n9 1\n5 1\n1 1\n0 1",
"output": "41"
},
{
"input": "7\n5 0\n4 0\n3 0\n5 2\n3 0\n4 2\n0... | 154 | 6,451,200 | 0 | 7,082 | |
926 | Choose Place | [] | null | null | A classroom in a school has six rows with 3 desks in each row. Two people can use the same desk: one sitting on the left and one sitting on the right.
Some places are already occupied, and some places are vacant. Petya has just entered the class and wants to occupy the most convenient place. The conveniences of the places are shown on the picture:
Here, the desks in the top row are the closest to the blackboard, while the desks in the bottom row are the furthest from the blackboard.
You are given a plan of the class, where '*' denotes an occupied place, '.' denotes a vacant place, and the aisles are denoted by '-'.
Find any of the most convenient vacant places for Petya. | The input consists of 6 lines. Each line describes one row of desks, starting from the closest to the blackboard. Each line is given in the following format: two characters, each is '*' or '.' β the description of the left desk in the current row; a character '-' β the aisle; two characters, each is '*' or '.' β the description of the center desk in the current row; a character '-' β the aisle; two characters, each is '*' or '.' β the description of the right desk in the current row. So, the length of each of the six lines is 8.
It is guaranteed that there is at least one vacant place in the classroom. | Print the plan of the classroom after Petya takes one of the most convenient for him places. Mark this place with the letter 'P'. There should be exactly one letter 'P' in the plan. Petya can only take a vacant place. In all other places the output should coincide with the input.
If there are multiple answers, print any. | [
"..-**-..\n..-**-..\n..-..-..\n..-..-..\n..-..-..\n..-..-..\n",
"**-**-**\n**-**-**\n..-**-.*\n**-**-**\n..-..-..\n..-**-..\n",
"**-**-*.\n*.-*.-**\n**-**-**\n**-**-**\n..-..-..\n..-**-..\n"
] | [
"..-**-..\n..-**-..\n..-..-..\n..-P.-..\n..-..-..\n..-..-..\n",
"**-**-**\n**-**-**\n..-**-.*\n**-**-**\n..-P.-..\n..-**-..\n",
"**-**-*.\n*.-*P-**\n**-**-**\n**-**-**\n..-..-..\n..-**-..\n"
] | In the first example the maximum convenience is 3.
In the second example the maximum convenience is 2.
In the third example the maximum convenience is 4. | [
{
"input": "..-**-..\n..-**-..\n..-..-..\n..-..-..\n..-..-..\n..-..-..",
"output": "..-**-..\n..-**-..\n..-..-..\n..-P.-..\n..-..-..\n..-..-.."
},
{
"input": "**-**-**\n**-**-**\n..-**-.*\n**-**-**\n..-..-..\n..-**-..",
"output": "**-**-**\n**-**-**\n..-**-.*\n**-**-**\n..-P.-..\n..-**-.."
},
... | 46 | 0 | 0 | 7,086 | |
342 | Xenia and Tree | [
"data structures",
"divide and conquer",
"trees"
] | null | null | Xenia the programmer has a tree consisting of *n* nodes. We will consider the tree nodes indexed from 1 to *n*. We will also consider the first node to be initially painted red, and the other nodes β to be painted blue.
The distance between two tree nodes *v* and *u* is the number of edges in the shortest path between *v* and *u*.
Xenia needs to learn how to quickly execute queries of two types:
1. paint a specified blue node in red; 1. calculate which red node is the closest to the given one and print the shortest distance to the closest red node.
Your task is to write a program which will execute the described queries. | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=105,<=1<=β€<=*m*<=β€<=105) β the number of nodes in the tree and the number of queries. Next *n*<=-<=1 lines contain the tree edges, the *i*-th line contains a pair of integers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*) β an edge of the tree.
Next *m* lines contain queries. Each query is specified as a pair of integers *t**i*,<=*v**i* (1<=β€<=*t**i*<=β€<=2,<=1<=β€<=*v**i*<=β€<=*n*). If *t**i*<==<=1, then as a reply to the query we need to paint a blue node *v**i* in red. If *t**i*<==<=2, then we should reply to the query by printing the shortest distance from some red node to node *v**i*.
It is guaranteed that the given graph is a tree and that all queries are correct. | For each second type query print the reply in a single line. | [
"5 4\n1 2\n2 3\n2 4\n4 5\n2 1\n2 5\n1 2\n2 5\n"
] | [
"0\n3\n2\n"
] | none | [
{
"input": "5 4\n1 2\n2 3\n2 4\n4 5\n2 1\n2 5\n1 2\n2 5",
"output": "0\n3\n2"
}
] | 77 | 3,891,200 | -1 | 7,094 | |
988 | Substrings Sort | [
"sortings",
"strings"
] | null | null | You are given $n$ strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings.
String $a$ is a substring of string $b$ if it is possible to choose several consecutive letters in $b$ in such a way that they form $a$. For example, string "for" is contained as a substring in strings "codeforces", "for" and "therefore", but is not contained as a substring in strings "four", "fofo" and "rof". | The first line contains an integer $n$ ($1 \le n \le 100$) β the number of strings.
The next $n$ lines contain the given strings. The number of letters in each string is from $1$ to $100$, inclusive. Each string consists of lowercase English letters.
Some strings might be equal. | If it is impossible to reorder $n$ given strings in required order, print "NO" (without quotes).
Otherwise print "YES" (without quotes) and $n$ given strings in required order. | [
"5\na\naba\nabacaba\nba\naba\n",
"5\na\nabacaba\nba\naba\nabab\n",
"3\nqwerty\nqwerty\nqwerty\n"
] | [
"YES\na\nba\naba\naba\nabacaba\n",
"NO\n",
"YES\nqwerty\nqwerty\nqwerty\n"
] | In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba". | [
{
"input": "5\na\naba\nabacaba\nba\naba",
"output": "YES\na\nba\naba\naba\nabacaba"
},
{
"input": "5\na\nabacaba\nba\naba\nabab",
"output": "NO"
},
{
"input": "3\nqwerty\nqwerty\nqwerty",
"output": "YES\nqwerty\nqwerty\nqwerty"
},
{
"input": "1\nwronganswer",
"output": "Y... | 46 | 0 | 3 | 7,097 | |
474 | Flowers | [
"dp"
] | null | null | We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.
But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size *k*.
Now Marmot wonders in how many ways he can eat between *a* and *b* flowers. As the number of ways could be very large, print it modulo 1000000007 (109<=+<=7). | Input contains several test cases.
The first line contains two integers *t* and *k* (1<=β€<=*t*,<=*k*<=β€<=105), where *t* represents the number of test cases.
The next *t* lines contain two integers *a**i* and *b**i* (1<=β€<=*a**i*<=β€<=*b**i*<=β€<=105), describing the *i*-th test. | Print *t* lines to the standard output. The *i*-th line should contain the number of ways in which Marmot can eat between *a**i* and *b**i* flowers at dinner modulo 1000000007 (109<=+<=7). | [
"3 2\n1 3\n2 3\n4 4\n"
] | [
"6\n5\n5\n"
] | - For *K* = 2 and length 1 Marmot can eat (*R*). - For *K* = 2 and length 2 Marmot can eat (*RR*) and (*WW*). - For *K* = 2 and length 3 Marmot can eat (*RRR*), (*RWW*) and (*WWR*). - For *K* = 2 and length 4 Marmot can eat, for example, (*WWWW*) or (*RWWR*), but for example he can't eat (*WWWR*). | [
{
"input": "3 2\n1 3\n2 3\n4 4",
"output": "6\n5\n5"
},
{
"input": "1 1\n1 3",
"output": "14"
},
{
"input": "1 2\n64329 79425",
"output": "0"
}
] | 1,076 | 9,420,800 | 3 | 7,099 | |
992 | Nastya and a Wardrobe | [
"math"
] | null | null | Nastya received a gift on New YearΒ β a magic wardrobe. It is magic because in the end of each month the number of dresses in it doubles (i.e. the number of dresses becomes twice as large as it is in the beginning of the month).
Unfortunately, right after the doubling the wardrobe eats one of the dresses (if any) with the 50% probability. It happens every month except the last one in the year.
Nastya owns *x* dresses now, so she became interested in the [expected number](https://en.wikipedia.org/wiki/Expected_value) of dresses she will have in one year. Nastya lives in Byteland, so the year lasts for *k*<=+<=1 months.
Nastya is really busy, so she wants you to solve this problem. You are the programmer, after all. Also, you should find the answer modulo 109<=+<=7, because it is easy to see that it is always integer. | The only line contains two integers *x* and *k* (0<=β€<=*x*,<=*k*<=β€<=1018), where *x* is the initial number of dresses and *k*<=+<=1 is the number of months in a year in Byteland. | In the only line print a single integerΒ β the expected number of dresses Nastya will own one year later modulo 109<=+<=7. | [
"2 0\n",
"2 1\n",
"3 2\n"
] | [
"4\n",
"7\n",
"21\n"
] | In the first example a year consists on only one month, so the wardrobe does not eat dresses at all.
In the second example after the first month there are 3 dresses with 50% probability and 4 dresses with 50% probability. Thus, in the end of the year there are 6 dresses with 50% probability and 8 dresses with 50% probability. This way the answer for this test is (6β+β8)β/β2β=β7. | [
{
"input": "2 0",
"output": "4"
},
{
"input": "2 1",
"output": "7"
},
{
"input": "3 2",
"output": "21"
},
{
"input": "1 411",
"output": "485514976"
},
{
"input": "1 692",
"output": "860080936"
},
{
"input": "16 8",
"output": "7937"
},
{
"in... | 93 | 0 | 0 | 7,104 | |
0 | none | [
"none"
] | null | null | This is an interactive problem.
Jury has hidden a permutation *p* of integers from 0 to *n*<=-<=1. You know only the length *n*. Remind that in permutation all integers are distinct.
Let *b* be the inverse permutation for *p*, i.e. *p**b**i*<==<=*i* for all *i*. The only thing you can do is to ask xor of elements *p**i* and *b**j*, printing two indices *i* and *j* (not necessarily distinct). As a result of the query with indices *i* and *j* you'll get the value , where denotes the xor operation. You can find the description of xor operation in notes.
Note that some permutations can remain indistinguishable from the hidden one, even if you make all possible *n*2 queries. You have to compute the number of permutations indistinguishable from the hidden one, and print one of such permutations, making no more than 2*n* queries.
The hidden permutation does not depend on your queries. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=5000) β the length of the hidden permutation. You should read this integer first. | When your program is ready to print the answer, print three lines.
In the first line print "!".
In the second line print single integer *answers*_*cnt*Β β the number of permutations indistinguishable from the hidden one, including the hidden one.
In the third line print *n* integers *p*0,<=*p*1,<=...,<=*p**n*<=-<=1 (0<=β€<=*p**i*<=<<=*n*, all *p**i* should be distinct)Β β one of the permutations indistinguishable from the hidden one.
Your program should terminate after printing the answer. | [
"3\n0\n0\n3\n2\n3\n2",
"4\n2\n3\n2\n0\n2\n3\n2\n0"
] | [
"? 0 0\n? 1 1\n? 1 2\n? 0 2\n? 2 1\n? 2 0\n!\n1\n0 1 2",
"? 0 1\n? 1 2\n? 2 3\n? 3 3\n? 3 2\n? 2 1\n? 1 0\n? 0 0\n!\n2\n3 1 2 0"
] | xor operation, or bitwise exclusive OR, is an operation performed over two integers, in which the *i*-th digit in binary representation of the result is equal to 1 if and only if exactly one of the two integers has the *i*-th digit in binary representation equal to 1. For more information, see [here](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
In the first example *p*β=β[0,β1,β2], thus *b*β=β[0,β1,β2], the values <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5dae53b8ed0e62c9af6c21130fc022b384e359ff.png" style="max-width: 100.0%;max-height: 100.0%;"/> are correct for the given *i*,β*j*. There are no other permutations that give the same answers for the given queries.
The answers for the queries are:
- <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b36cf4ccf629eaa3a225cda06a5697eb030eb4d7.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/88e848d2e69ecfb3b8c8d63fdc8949c7cc1e9f28.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/233e40a031e99efe33bdf68f4a383163a23e1e7b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3ec81fca9911a6bde9c2c7cae6c189514105085f.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5d5595e56564765e3db6b865de2219cdadadeb.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3355af6093e240986c338bf13071b479e2604ca9.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second example *p*β=β[3,β1,β2,β0], and *b*β=β[3,β1,β2,β0], the values <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5dae53b8ed0e62c9af6c21130fc022b384e359ff.png" style="max-width: 100.0%;max-height: 100.0%;"/> match for all pairs *i*,β*j*. But there is one more suitable permutation *p*β=β[0,β2,β1,β3], *b*β=β[0,β2,β1,β3] that matches all *n*<sup class="upper-index">2</sup> possible queries as well. All other permutations do not match even the shown queries. | [
{
"input": "3\n0 1 2",
"output": "1\n0 1 2 "
},
{
"input": "4\n3 1 2 0",
"output": "2\n0 2 1 3 "
},
{
"input": "4\n3 2 1 0",
"output": "4\n0 1 2 3 "
},
{
"input": "8\n2 3 0 1 4 5 6 7",
"output": "4\n0 1 2 3 6 7 4 5 "
},
{
"input": "1\n0",
"output": "1\n0 "
}... | 2,000 | 5,529,600 | 0 | 7,111 | |
702 | Powers of Two | [
"brute force",
"data structures",
"implementation",
"math"
] | null | null | You are given *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Find the number of pairs of indexes *i*,<=*j* (*i*<=<<=*j*) that *a**i*<=+<=*a**j* is a power of 2 (i. e. some integer *x* exists so that *a**i*<=+<=*a**j*<==<=2*x*). | The first line contains the single positive integer *n* (1<=β€<=*n*<=β€<=105) β the number of integers.
The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109). | Print the number of pairs of indexes *i*,<=*j* (*i*<=<<=*j*) that *a**i*<=+<=*a**j* is a power of 2. | [
"4\n7 3 2 1\n",
"3\n1 1 1\n"
] | [
"2\n",
"3\n"
] | In the first example the following pairs of indexes include in answer: (1,β4) and (2,β4).
In the second example all pairs of indexes (*i*,β*j*) (where *i*β<β*j*) include in answer. | [
{
"input": "4\n7 3 2 1",
"output": "2"
},
{
"input": "3\n1 1 1",
"output": "3"
},
{
"input": "1\n1000000000",
"output": "0"
},
{
"input": "10\n2827343 1373647 96204862 723505 796619138 71550121 799843967 5561265 402690754 446173607",
"output": "2"
},
{
"input": "1... | 3,000 | 6,246,400 | 0 | 7,124 | |
523 | Mean Requests | [
"*special",
"implementation"
] | null | null | In this problem you will have to deal with a real algorithm that is used in the VK social network.
As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of *T* seconds (for example, *T*<==<=60Β *seconds*<==<=1Β *min* and *T*<==<=86400Β *seconds*<==<=1Β *day*). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.
However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.
Let's consider the following formal model. We have a service that works for *n* seconds. We know the number of queries to this resource *a**t* at each moment of time *t* (1<=β€<=*t*<=β€<=*n*). Let's formulate the following algorithm of calculating the mean with exponential decay. Let *c* be some real number, strictly larger than one.
Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant *c* correctly will make the value of mean not very different from the real mean value *a**x* at *t*<=-<=*T*<=+<=1<=β€<=*x*<=β€<=*t*.
The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.
However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data.
You are given *n* values *a**t*, integer *T* and real number *c*. Also, you are given *m* moments *p**j* (1<=β€<=*j*<=β€<=*m*), where we are interested in the mean value of the number of queries for the last *T* seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula . The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula , where *approx* is the approximate value, obtained by the second algorithm, and *real* is the exact value obtained by the first algorithm. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2Β·105), integer *T* (1<=β€<=*T*<=β€<=*n*) and real number *c* (1<=<<=*c*<=β€<=100) β the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient *c* of the work of approximate algorithm. Number *c* is given with exactly six digits after the decimal point.
The next line contains *n* integers *a**t* (1<=β€<=*a**t*<=β€<=106) β the number of queries to the service at each moment of time.
The next line contains integer *m* (1<=β€<=*m*<=β€<=*n*) β the number of moments of time when we are interested in the mean number of queries for the last *T* seconds.
The next line contains *m* integers *p**j* (*T*<=β€<=*p**j*<=β€<=*n*), representing another moment of time for which we need statistics. Moments *p**j* are strictly increasing. | Print *m* lines. The *j*-th line must contain three numbers *real*, *approx* and *error*, where:
- is the real mean number of queries for the last *T* seconds; - *approx* is calculated by the given algorithm and equals *mean* at the moment of time *t*<==<=*p**j* (that is, after implementing the *p**j*-th iteration of the cycle); - is the relative error of the approximate algorithm.
The numbers you printed will be compared to the correct numbers with the relative or absolute error 10<=-<=4. It is recommended to print the numbers with at least five digits after the decimal point. | [
"1 1 2.000000\n1\n1\n1\n",
"11 4 1.250000\n9 11 7 5 15 6 6 6 6 6 6\n8\n4 5 6 7 8 9 10 11\n",
"13 4 1.250000\n3 3 3 3 3 20 3 3 3 3 3 3 3\n10\n4 5 6 7 8 9 10 11 12 13\n"
] | [
"1.000000 0.500000 0.500000\n",
"8.000000 4.449600 0.443800\n9.500000 6.559680 0.309507\n8.250000 6.447744 0.218455\n8.000000 6.358195 0.205226\n8.250000 6.286556 0.237993\n6.000000 6.229245 0.038207\n6.000000 6.183396 0.030566\n6.000000 6.146717 0.024453\n",
"3.000000 1.771200 0.409600\n3.000000 2.016960 0.327... | none | [
{
"input": "1 1 2.000000\n1\n1\n1",
"output": "1.000000 0.500000 0.500000"
},
{
"input": "11 4 1.250000\n9 11 7 5 15 6 6 6 6 6 6\n8\n4 5 6 7 8 9 10 11",
"output": "8.000000 4.449600 0.443800\n9.500000 6.559680 0.309507\n8.250000 6.447744 0.218455\n8.000000 6.358195 0.205226\n8.250000 6.286556 0.... | 4,000 | 17,817,600 | 0 | 7,134 | |
442 | Andrey and Problem | [
"greedy",
"math",
"probabilities"
] | null | null | Andrey needs one more problem to conduct a programming contest. He has *n* friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends β the probability that this friend will come up with a problem if Andrey asks him.
Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of Andrey's friends. The second line contains *n* real numbers *p**i* (0.0<=β€<=*p**i*<=β€<=1.0) β the probability that the *i*-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point. | Print a single real number β the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10<=-<=9. | [
"4\n0.1 0.2 0.3 0.8\n",
"2\n0.1 0.2\n"
] | [
"0.800000000000\n",
"0.260000000000\n"
] | In the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one.
In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1Β·0.8β+β0.9Β·0.2β=β0.26. | [
{
"input": "4\n0.1 0.2 0.3 0.8",
"output": "0.800000000000"
},
{
"input": "2\n0.1 0.2",
"output": "0.260000000000"
},
{
"input": "1\n0.217266",
"output": "0.217266000000"
},
{
"input": "2\n0.608183 0.375030",
"output": "0.608183000000"
},
{
"input": "3\n0.388818 0... | 108 | 20,172,800 | 0 | 7,149 | |
689 | Mike and Shortcuts | [
"dfs and similar",
"graphs",
"greedy",
"shortest paths"
] | null | null | Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.
City consists of *n* intersections numbered from 1 to *n*. Mike starts walking from his house located at the intersection number 1 and goes along some sequence of intersections. Walking from intersection number *i* to intersection *j* requires |*i*<=-<=*j*| units of energy. The total energy spent by Mike to visit a sequence of intersections *p*1<==<=1,<=*p*2,<=...,<=*p**k* is equal to units of energy.
Of course, walking would be boring if there were no shortcuts. A shortcut is a special path that allows Mike walking from one intersection to another requiring only 1 unit of energy. There are exactly *n* shortcuts in Mike's city, the *i**th* of them allows walking from intersection *i* to intersection *a**i* (*i*<=β€<=*a**i*<=β€<=*a**i*<=+<=1) (but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence *p*1<==<=1,<=*p*2,<=...,<=*p**k* then for each 1<=β€<=*i*<=<<=*k* satisfying *p**i*<=+<=1<==<=*a**p**i* and *a**p**i*<=β <=*p**i* Mike will spend only 1 unit of energy instead of |*p**i*<=-<=*p**i*<=+<=1| walking from the intersection *p**i* to intersection *p**i*<=+<=1. For example, if Mike chooses a sequence *p*1<==<=1,<=*p*2<==<=*a**p*1,<=*p*3<==<=*a**p*2,<=...,<=*p**k*<==<=*a**p**k*<=-<=1, he spends exactly *k*<=-<=1 units of total energy walking around them.
Before going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each 1<=β€<=*i*<=β€<=*n* Mike is interested in finding minimum possible total energy of some sequence *p*1<==<=1,<=*p*2,<=...,<=*p**k*<==<=*i*. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of Mike's city intersection.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (*i*<=β€<=*a**i*<=β€<=*n* , , describing shortcuts of Mike's city, allowing to walk from intersection *i* to intersection *a**i* using only 1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from *a**i* to *i*). | In the only line print *n* integers *m*1,<=*m*2,<=...,<=*m**n*, where *m**i* denotes the least amount of total energy required to walk from intersection 1 to intersection *i*. | [
"3\n2 2 3\n",
"5\n1 2 3 4 5\n",
"7\n4 4 4 4 7 7 7\n"
] | [
"0 1 2 \n",
"0 1 2 3 4 \n",
"0 1 2 1 2 3 3 \n"
] | In the first sample case desired sequences are:
1:β1; *m*<sub class="lower-index">1</sub>β=β0;
2:β1,β2; *m*<sub class="lower-index">2</sub>β=β1;
3:β1,β3; *m*<sub class="lower-index">3</sub>β=β|3β-β1|β=β2.
In the second sample case the sequence for any intersection 1β<β*i* is always 1,β*i* and *m*<sub class="lower-index">*i*</sub>β=β|1β-β*i*|.
In the third sample caseΒ β consider the following intersection sequences:
1:β1; *m*<sub class="lower-index">1</sub>β=β0;
2:β1,β2; *m*<sub class="lower-index">2</sub>β=β|2β-β1|β=β1;
3:β1,β4,β3; *m*<sub class="lower-index">3</sub>β=β1β+β|4β-β3|β=β2;
4:β1,β4; *m*<sub class="lower-index">4</sub>β=β1;
5:β1,β4,β5; *m*<sub class="lower-index">5</sub>β=β1β+β|4β-β5|β=β2;
6:β1,β4,β6; *m*<sub class="lower-index">6</sub>β=β1β+β|4β-β6|β=β3;
7:β1,β4,β5,β7; *m*<sub class="lower-index">7</sub>β=β1β+β|4β-β5|β+β1β=β3. | [
{
"input": "3\n2 2 3",
"output": "0 1 2 "
},
{
"input": "5\n1 2 3 4 5",
"output": "0 1 2 3 4 "
},
{
"input": "7\n4 4 4 4 7 7 7",
"output": "0 1 2 1 2 3 3 "
},
{
"input": "98\n17 17 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 87 87... | 46 | 0 | 0 | 7,154 | |
628 | Bear and Fair Set | [
"flows",
"graphs"
] | null | null | Limak is a grizzly bear. He is big and dreadful. You were chilling in the forest when you suddenly met him. It's very unfortunate for you. He will eat all your cookies unless you can demonstrate your mathematical skills. To test you, Limak is going to give you a puzzle to solve.
It's a well-known fact that Limak, as every bear, owns a set of numbers. You know some information about the set:
- The elements of the set are distinct positive integers. - The number of elements in the set is *n*. The number *n* is divisible by 5. - All elements are between 1 and *b*, inclusive: bears don't know numbers greater than *b*. - For each *r* in {0,<=1,<=2,<=3,<=4}, the set contains exactly elements that give remainder *r* when divided by 5. (That is, there are elements divisible by 5, elements of the form 5*k*<=+<=1, elements of the form 5*k*<=+<=2, and so on.)
Limak smiles mysteriously and gives you *q* hints about his set. The *i*-th hint is the following sentence: "If you only look at elements that are between 1 and *upTo**i*, inclusive, you will find exactly *quantity**i* such elements in my set."
In a moment Limak will tell you the actual puzzle, but something doesn't seem right... That smile was very strange. You start to think about a possible reason. Maybe Limak cheated you? Or is he a fair grizzly bear?
Given *n*, *b*, *q* and hints, check whether Limak can be fair, i.e. there exists at least one set satisfying the given conditions. If it's possible then print ''fair". Otherwise, print ''unfair". | The first line contains three integers *n*, *b* and *q* (5<=β€<=*n*<=β€<=*b*<=β€<=104, 1<=β€<=*q*<=β€<=104, *n* divisible by 5) β the size of the set, the upper limit for numbers in the set and the number of hints.
The next *q* lines describe the hints. The *i*-th of them contains two integers *upTo**i* and *quantity**i* (1<=β€<=*upTo**i*<=β€<=*b*, 0<=β€<=*quantity**i*<=β€<=*n*). | Print ''fair" if there exists at least one set that has all the required properties and matches all the given hints. Otherwise, print ''unfair". | [
"10 20 1\n10 10\n",
"10 20 3\n15 10\n5 0\n10 5\n",
"10 20 2\n15 3\n20 10\n"
] | [
"fair\n",
"fair\n",
"unfair\n"
] | In the first example there is only one set satisfying all conditions: {1,β2,β3,β4,β5,β6,β7,β8,β9,β10}.
In the second example also there is only one set satisfying all conditions: {6,β7,β8,β9,β10,β11,β12,β13,β14,β15}.
Easy to see that there is no set satisfying all conditions from the third example. So Limak lied to you :-( | [
{
"input": "10 20 1\n10 10",
"output": "fair"
},
{
"input": "10 20 3\n15 10\n5 0\n10 5",
"output": "fair"
},
{
"input": "10 20 2\n15 3\n20 10",
"output": "unfair"
},
{
"input": "15 27 2\n6 4\n23 5",
"output": "unfair"
},
{
"input": "50 7654 4\n1273 11\n6327 38\n12... | 46 | 0 | 0 | 7,168 | |
441 | Valera and Fruits | [
"greedy",
"implementation"
] | null | null | Valera loves his garden, where *n* fruit trees grow.
This year he will enjoy a great harvest! On the *i*-th tree *b**i* fruit grow, they will ripen on a day number *a**i*. Unfortunately, the fruit on the tree get withered, so they can only be collected on day *a**i* and day *a**i*<=+<=1 (all fruits that are not collected in these two days, become unfit to eat).
Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than *v* fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? | The first line contains two space-separated integers *n* and *v* (1<=β€<=*n*,<=*v*<=β€<=3000) β the number of fruit trees in the garden and the number of fruits that Valera can collect in a day.
Next *n* lines contain the description of trees in the garden. The *i*-th line contains two space-separated integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=3000) β the day the fruits ripen on the *i*-th tree and the number of fruits on the *i*-th tree. | Print a single integer β the maximum number of fruit that Valera can collect. | [
"2 3\n1 5\n2 3\n",
"5 10\n3 20\n2 20\n1 20\n4 20\n5 20\n"
] | [
"8\n",
"60\n"
] | In the first sample, in order to obtain the optimal answer, you should act as follows.
- On the first day collect 3 fruits from the 1-st tree. - On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. - On the third day collect the remaining fruits from the 2-nd tree.
In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither. | [
{
"input": "2 3\n1 5\n2 3",
"output": "8"
},
{
"input": "5 10\n3 20\n2 20\n1 20\n4 20\n5 20",
"output": "60"
},
{
"input": "10 3000\n1 2522\n4 445\n8 1629\n5 772\n9 2497\n6 81\n3 426\n7 1447\n2 575\n10 202",
"output": "10596"
},
{
"input": "5 3000\n5 772\n1 2522\n2 575\n4 445... | 77 | 204,800 | 0 | 7,176 | |
245 | Mishap in Club | [
"greedy",
"implementation"
] | null | null | Polycarpus just has been out of luck lately! As soon as he found a job in the "Binary Cat" cafe, the club got burgled. All ice-cream was stolen.
On the burglary night Polycarpus kept a careful record of all club visitors. Each time a visitor entered the club, Polycarpus put down character "+" in his notes. Similarly, each time a visitor left the club, Polycarpus put character "-" in his notes. We know that all cases of going in and out happened consecutively, that is, no two events happened at the same time. Polycarpus doesn't remember whether there was somebody in the club at the moment when his shift begun and at the moment when it ended.
Right now the police wonders what minimum number of distinct people Polycarpus could have seen. Assume that he sees anybody coming in or out of the club. Each person could have come in or out an arbitrary number of times. | The only line of the input contains a sequence of characters "+" and "-", the characters are written one after another without any separators. The characters are written in the order, in which the corresponding events occurred. The given sequence has length from 1 to 300 characters, inclusive. | Print the sought minimum number of people | [
"+-+-+\n",
"---"
] | [
"1\n",
"3"
] | none | [
{
"input": "+-+-+",
"output": "1"
},
{
"input": "---",
"output": "3"
},
{
"input": "-",
"output": "1"
},
{
"input": "--",
"output": "2"
},
{
"input": "---",
"output": "3"
},
{
"input": "----",
"output": "4"
},
{
"input": "---+",
"output... | 248 | 0 | 0 | 7,182 | |
288 | Polo the Penguin and Houses | [
"combinatorics"
] | null | null | Little penguin Polo loves his home village. The village has *n* houses, indexed by integers from 1 to *n*. Each house has a plaque containing an integer, the *i*-th house has a plaque containing integer *p**i* (1<=β€<=*p**i*<=β€<=*n*).
Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a house number *x*. Then he goes to the house whose number is written on the plaque of house *x* (that is, to house *p**x*), then he goes to the house whose number is written on the plaque of house *p**x* (that is, to house *p**p**x*), and so on.
We know that:
1. When the penguin starts walking from any house indexed from 1 to *k*, inclusive, he can walk to house number 1. 1. When the penguin starts walking from any house indexed from *k*<=+<=1 to *n*, inclusive, he definitely cannot walk to house number 1. 1. When the penguin starts walking from house number 1, he can get back to house number 1 after some non-zero number of walks from a house to a house.
You need to find the number of ways you may write the numbers on the houses' plaques so as to fulfill the three above described conditions. Print the remainder after dividing this number by 1000000007 (109<=+<=7). | The single line contains two space-separated integers *n* and *k* (1<=β€<=*n*<=β€<=1000,<=1<=β€<=*k*<=β€<=*min*(8,<=*n*)) β the number of the houses and the number *k* from the statement. | In a single line print a single integer β the answer to the problem modulo 1000000007 (109<=+<=7). | [
"5 2\n",
"7 4\n"
] | [
"54\n",
"1728\n"
] | none | [
{
"input": "5 2",
"output": "54"
},
{
"input": "7 4",
"output": "1728"
},
{
"input": "8 5",
"output": "16875"
},
{
"input": "8 1",
"output": "823543"
},
{
"input": "10 7",
"output": "3176523"
},
{
"input": "12 8",
"output": "536870912"
},
{
... | 217 | 22,323,200 | -1 | 7,203 | |
429 | Tricky Function | [
"data structures",
"divide and conquer",
"geometry"
] | null | null | Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a friend of Iahub, managed to get hold of the problem before the contest. Because he wants to make sure Iahub will be the one qualified, he tells Iahub the following task.
You're given an (1-based) array *a* with *n* elements. Let's define function *f*(*i*,<=*j*) (1<=β€<=*i*,<=*j*<=β€<=*n*) as (*i*<=-<=*j*)2<=+<=*g*(*i*,<=*j*)2. Function g is calculated by the following pseudo-code:
Find a value *min**i*<=β <=*j*Β Β *f*(*i*,<=*j*).
Probably by now Iahub already figured out the solution to this problem. Can you? | The first line of input contains a single integer *n* (2<=β€<=*n*<=β€<=100000). Next line contains *n* integers *a*[1], *a*[2], ..., *a*[*n*] (<=-<=104<=β€<=*a*[*i*]<=β€<=104). | Output a single integer β the value of *min**i*<=β <=*j*Β Β *f*(*i*,<=*j*). | [
"4\n1 0 0 -1\n",
"2\n1 -1\n"
] | [
"1\n",
"2\n"
] | none | [
{
"input": "4\n1 0 0 -1",
"output": "1"
},
{
"input": "2\n1 -1",
"output": "2"
},
{
"input": "100\n-57 -64 83 76 80 27 60 76 -80 -56 52 72 -17 92 -96 87 41 -88 94 89 12 42 36 34 -100 -43 -42 62 3 87 -69 -6 -27 -59 -7 5 -90 -23 63 -87 -60 -92 -40 54 -16 -47 67 -64 10 33 -19 53 -7 -62 16 -... | 2,000 | 17,817,600 | 0 | 7,205 | |
46 | Parking Lot | [
"data structures",
"implementation"
] | D. Parking Lot | 2 | 256 | Nowadays it is becoming increasingly difficult to park a car in cities successfully. Let's imagine a segment of a street as long as *L* meters along which a parking lot is located. Drivers should park their cars strictly parallel to the pavement on the right side of the street (remember that in the country the authors of the tasks come from the driving is right side!). Every driver when parking wants to leave for themselves some extra space to move their car freely, that's why a driver is looking for a place where the distance between his car and the one behind his will be no less than *b* meters and the distance between his car and the one in front of his will be no less than *f* meters (if there's no car behind then the car can be parked at the parking lot segment edge; the same is true for the case when there're no cars parked in front of the car). Let's introduce an axis of coordinates along the pavement. Let the parking lot begin at point 0 and end at point *L*. The drivers drive in the direction of the coordinates' increasing and look for the earliest place (with the smallest possible coordinate) where they can park the car. In case there's no such place, the driver drives on searching for his perfect peaceful haven. Sometimes some cars leave the street and free some space for parking. Considering that there never are two moving cars on a street at a time write a program that can use the data on the drivers, entering the street hoping to park there and the drivers leaving it, to model the process and determine a parking lot space for each car. | The first line contains three integers *L*, *b* ΠΈ *f* (10<=β€<=*L*<=β€<=100000,<=1<=β€<=*b*,<=*f*<=β€<=100). The second line contains an integer *n* (1<=β€<=*n*<=β€<=100) that indicates the number of requests the program has got. Every request is described on a single line and is given by two numbers. The first number represents the request type. If the request type is equal to 1, then in that case the second number indicates the length of a car (in meters) that enters the street looking for a place to park. And if the request type is equal to 2, then the second number identifies the number of such a request (starting with 1) that the car whose arrival to the parking lot was described by a request with this number, leaves the parking lot. It is guaranteed that that car was parked at the moment the request of the 2 type was made. The lengths of cars are integers from 1 to 1000. | For every request of the 1 type print number -1 on the single line if the corresponding car couldn't find place to park along the street. Otherwise, print a single number equal to the distance between the back of the car in its parked position and the beginning of the parking lot zone. | [
"30 1 2\n6\n1 5\n1 4\n1 5\n2 2\n1 5\n1 4\n",
"30 1 1\n6\n1 5\n1 4\n1 5\n2 2\n1 5\n1 4\n",
"10 1 1\n1\n1 12\n"
] | [
"0\n6\n11\n17\n23\n",
"0\n6\n11\n17\n6\n",
"-1\n"
] | none | [
{
"input": "30 1 2\n6\n1 5\n1 4\n1 5\n2 2\n1 5\n1 4",
"output": "0\n6\n11\n17\n23"
},
{
"input": "30 1 1\n6\n1 5\n1 4\n1 5\n2 2\n1 5\n1 4",
"output": "0\n6\n11\n17\n6"
},
{
"input": "10 1 1\n1\n1 12",
"output": "-1"
},
{
"input": "10 1 1\n1\n1 9",
"output": "0"
},
{
... | 248 | 2,252,800 | -1 | 7,207 |
633 | A Trivial Problem | [
"brute force",
"constructive algorithms",
"math",
"number theory"
] | null | null | Mr. Santa asks all the great programmers of the world to solve a trivial problem. He gives them an integer *m* and asks for the number of positive integers *n*, such that the factorial of *n* ends with exactly *m* zeroes. Are you among those great programmers who can solve this problem? | The only line of input contains an integer *m* (1<=β€<=*m*<=β€<=100<=000)Β β the required number of trailing zeroes in factorial. | First print *k*Β β the number of values of *n* such that the factorial of *n* ends with *m* zeroes. Then print these *k* integers in increasing order. | [
"1\n",
"5\n"
] | [
"5\n5 6 7 8 9 ",
"0"
] | The factorial of *n* is equal to the product of all integers from 1 to *n* inclusive, that is *n*!β=β1Β·2Β·3Β·...Β·*n*.
In the first sample, 5!β=β120, 6!β=β720, 7!β=β5040, 8!β=β40320 and 9!β=β362880. | [
{
"input": "1",
"output": "5\n5 6 7 8 9 "
},
{
"input": "5",
"output": "0"
},
{
"input": "2",
"output": "5\n10 11 12 13 14 "
},
{
"input": "3",
"output": "5\n15 16 17 18 19 "
},
{
"input": "7",
"output": "5\n30 31 32 33 34 "
},
{
"input": "12",
"ou... | 171 | 6,553,600 | 3 | 7,218 | |
74 | Chessboard Billiard | [
"dfs and similar",
"dsu",
"graphs",
"number theory"
] | C. Chessboard Billiard | 2 | 256 | Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
It is considered that one billiard ball *a* beats another billiard ball *b* if *a* can reach a point where *b* is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard *n*<=Γ<=*m* in size. | The first line contains two integers *n* and *m* (2<=β€<=*n*,<=*m*<=β€<=106). | Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator). | [
"3 4\n",
"3 3\n"
] | [
"2",
"3"
] | none | [
{
"input": "3 4",
"output": "2"
},
{
"input": "3 3",
"output": "3"
},
{
"input": "2 2",
"output": "2"
},
{
"input": "4 3",
"output": "2"
},
{
"input": "2 3",
"output": "2"
},
{
"input": "4 4",
"output": "4"
},
{
"input": "4 6",
"output"... | 218 | 614,400 | -1 | 7,236 |
0 | none | [
"none"
] | null | null | Ivan had string *s* consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string *s*. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string *s*. Namely, he remembers, that string *t**i* occurs in string *s* at least *k**i* times or more, he also remembers exactly *k**i* positions where the string *t**i* occurs in string *s*: these positions are *x**i*,<=1,<=*x**i*,<=2,<=...,<=*x**i*,<=*k**i*. He remembers *n* such strings *t**i*.
You are to reconstruct lexicographically minimal string *s* such that it fits all the information Ivan remembers. Strings *t**i* and string *s* consist of small English letters only. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105) β the number of strings Ivan remembers.
The next *n* lines contain information about the strings. The *i*-th of these lines contains non-empty string *t**i*, then positive integer *k**i*, which equal to the number of times the string *t**i* occurs in string *s*, and then *k**i* distinct positive integers *x**i*,<=1,<=*x**i*,<=2,<=...,<=*x**i*,<=*k**i* in increasing order β positions, in which occurrences of the string *t**i* in the string *s* start. It is guaranteed that the sum of lengths of strings *t**i* doesn't exceed 106, 1<=β€<=*x**i*,<=*j*<=β€<=106, 1<=β€<=*k**i*<=β€<=106, and the sum of all *k**i* doesn't exceed 106. The strings *t**i* can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists. | Print lexicographically minimal string that fits all the information Ivan remembers. | [
"3\na 4 1 3 5 7\nab 2 1 5\nca 1 4\n",
"1\na 1 3\n",
"3\nab 1 1\naba 1 3\nab 2 3 5\n"
] | [
"abacaba\n",
"aaa\n",
"ababab\n"
] | none | [
{
"input": "3\na 4 1 3 5 7\nab 2 1 5\nca 1 4",
"output": "abacaba"
},
{
"input": "1\na 1 3",
"output": "aaa"
},
{
"input": "3\nab 1 1\naba 1 3\nab 2 3 5",
"output": "ababab"
},
{
"input": "6\nba 2 16 18\na 1 12\nb 3 4 13 20\nbb 2 6 8\nababbbbbaab 1 3\nabababbbbb 1 1",
"ou... | 2,000 | 143,974,400 | 0 | 7,239 | |
125 | Simple XML | [
"implementation"
] | null | null | Let's define a string <x> as an opening tag, where *x* is any small letter of the Latin alphabet. Each opening tag matches a closing tag of the type </x>, where *x* is the same letter.
Tegs can be nested into each other: in this case one opening and closing tag pair is located inside another pair.
Let's define the notion of a XML-text:
- an empty string is a XML-text - if *s* is a XML-text, then *s*'=<a>+*s*+</a> also is a XML-text, where *a* is any small Latin letter - if *s*1, *s*2 are XML-texts, then *s*1+*s*2 also is a XML-text
You are given a XML-text (it is guaranteed that the text is valid), your task is to print in the following form:
- each tag (opening and closing) is located on a single line - print before the tag 2<=*<=*h* spaces, where *h* is the level of the tag's nestedness. | The input data consists on the only non-empty string β the XML-text, its length does not exceed 1000 characters. It is guaranteed that the text is valid. The text contains no spaces. | Print the given XML-text according to the above-given rules. | [
"<a><b><c></c></b></a>\n",
"<a><b></b><d><c></c></d></a>\n"
] | [
"<a>\n <b>\n <c>\n </c>\n </b>\n</a>\n",
"<a>\n <b>\n </b>\n <d>\n <c>\n </c>\n </d>\n</a>\n"
] | none | [
{
"input": "<a><b><c></c></b></a>",
"output": "<a>\n <b>\n <c>\n </c>\n </b>\n</a>"
},
{
"input": "<a><b></b><d><c></c></d></a>",
"output": "<a>\n <b>\n </b>\n <d>\n <c>\n </c>\n </d>\n</a>"
},
{
"input": "<z></z>",
"output": "<z>\n</z>"
},
{
"input": "<u><d... | 216 | 307,200 | 0 | 7,262 | |
305 | Ivan and Powers of Two | [
"greedy",
"implementation"
] | null | null | Ivan has got an array of *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n*. Ivan knows that the array is sorted in the non-decreasing order.
Ivan wrote out integers 2*a*1,<=2*a*2,<=...,<=2*a**n* on a piece of paper. Now he wonders, what minimum number of integers of form 2*b* (*b*<=β₯<=0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2*v*<=-<=1 for some integer *v* (*v*<=β₯<=0).
Help Ivan, find the required quantity of numbers. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second input line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=2Β·109). It is guaranteed that *a*1<=β€<=*a*2<=β€<=...<=β€<=*a**n*. | Print a single integer β the answer to the problem. | [
"4\n0 1 1 1\n",
"1\n3\n"
] | [
"0\n",
"3\n"
] | In the first sample you do not need to add anything, the sum of numbers already equals 2<sup class="upper-index">3</sup>β-β1β=β7.
In the second sample you need to add numbers 2<sup class="upper-index">0</sup>,β2<sup class="upper-index">1</sup>,β2<sup class="upper-index">2</sup>. | [
{
"input": "4\n0 1 1 1",
"output": "0"
},
{
"input": "1\n3",
"output": "3"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "1\n2000000000",
"output": "2000000000"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "26\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... | 500 | 6,656,000 | 0 | 7,277 | |
331 | Escaping on Beaveractor | [
"dfs and similar",
"implementation"
] | null | null | Don't put up with what you're sick of! The Smart Beaver decided to escape from the campus of Beaver Science Academy (BSA). BSA is a *b*<=Γ<=*b* square on a plane. Each point *x*,<=*y* (0<=β€<=*x*,<=*y*<=β€<=*b*) belongs to BSA. To make the path quick and funny, the Beaver constructed a Beaveractor, an effective and comfortable types of transport.
The campus obeys traffic rules: there are *n* arrows, parallel to the coordinate axes. The arrows do not intersect and do not touch each other. When the Beaveractor reaches some arrow, it turns in the arrow's direction and moves on until it either reaches the next arrow or gets outside the campus. The Beaveractor covers exactly one unit of space per one unit of time. You can assume that there are no obstacles to the Beaveractor.
The BSA scientists want to transport the brand new Beaveractor to the "Academic Tractor" research institute and send the Smart Beaver to do his postgraduate studies and sharpen pencils. They have *q* plans, representing the Beaveractor's initial position (*x**i*,<=*y**i*), the initial motion vector *w**i* and the time *t**i* that have passed after the escape started.
Your task is for each of the *q* plans to determine the Smart Beaver's position after the given time. | The first line contains two integers: the number of traffic rules *n* and the size of the campus *b*, 0<=β€<=*n*, 1<=β€<=*b*. Next *n* lines contain the rules. Each line of the rules contains four space-separated integers *x*0, *y*0, *x*1, *y*1 β the beginning and the end of the arrow. It is guaranteed that all arrows are parallel to the coordinate axes and have no common points. All arrows are located inside the campus, that is, 0<=β€<=*x*0,<=*y*0,<=*x*1,<=*y*1<=β€<=*b* holds.
Next line contains integer *q* β the number of plans the scientists have, 1<=β€<=*q*<=β€<=105. The *i*-th plan is represented by two integers, *x**i*, *y**i* are the Beaveractor's coordinates at the initial time, 0<=β€<=*x**i*,<=*y**i*<=β€<=*b*, character *w**i*, that takes value U, D, L, R and sets the initial direction up, down, to the left or to the right correspondingly (the Y axis is directed upwards), and *t**i* β the time passed after the escape started, 0<=β€<=*t**i*<=β€<=1015.
- to get 30 points you need to solve the problem with constraints *n*,<=*b*<=β€<=30 (subproblem D1); - to get 60 points you need to solve the problem with constraints *n*,<=*b*<=β€<=1000 (subproblems D1+D2); - to get 100 points you need to solve the problem with constraints *n*,<=*b*<=β€<=105 (subproblems D1+D2+D3). | Print *q* lines. Each line should contain two integers β the Beaveractor's coordinates at the final moment of time for each plan. If the Smart Beaver manages to leave the campus in time *t**i*, print the coordinates of the last point in the campus he visited. | [
"3 3\n0 0 0 1\n0 2 2 2\n3 3 2 3\n12\n0 0 L 0\n0 0 L 1\n0 0 L 2\n0 0 L 3\n0 0 L 4\n0 0 L 5\n0 0 L 6\n2 0 U 2\n2 0 U 3\n3 0 U 5\n1 3 D 2\n1 3 R 2\n"
] | [
"0 0\n0 1\n0 2\n1 2\n2 2\n3 2\n3 2\n2 2\n3 2\n1 3\n2 2\n1 3\n"
] | none | [] | 3,462 | 62,771,200 | 0 | 7,278 | |
463 | Gargari and Permutations | [
"dfs and similar",
"dp",
"graphs",
"implementation"
] | null | null | Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found *k* permutations. Each of them consists of numbers 1,<=2,<=...,<=*n* in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?
You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=1000;Β 2<=β€<=*k*<=β€<=5). Each of the next *k* lines contains integers 1,<=2,<=...,<=*n* in some order β description of the current permutation. | Print the length of the longest common subsequence. | [
"4 3\n1 4 2 3\n4 1 2 3\n1 2 4 3\n"
] | [
"3\n"
] | The answer for the first test sample is subsequence [1, 2, 3]. | [
{
"input": "4 3\n1 4 2 3\n4 1 2 3\n1 2 4 3",
"output": "3"
},
{
"input": "6 3\n2 5 1 4 6 3\n5 1 4 3 2 6\n5 4 2 6 3 1",
"output": "3"
},
{
"input": "41 4\n24 15 17 35 13 41 4 14 23 5 8 16 21 18 30 36 6 22 11 29 26 1 40 31 7 3 32 10 28 38 12 20 39 37 34 19 33 27 2 25 9\n22 13 25 24 38 35 2... | 93 | 4,403,200 | 3 | 7,280 | |
551 | GukiZ and GukiZiana | [
"binary search",
"data structures",
"implementation"
] | null | null | Professor GukiZ was playing with arrays again and accidentally discovered new function, which he called *GukiZiana*. For given array *a*, indexed with integers from 1 to *n*, and number *y*, *GukiZiana*(*a*,<=*y*) represents maximum value of *j*<=-<=*i*, such that *a**j*<==<=*a**i*<==<=*y*. If there is no *y* as an element in *a*, then *GukiZiana*(*a*,<=*y*) is equal to <=-<=1. GukiZ also prepared a problem for you. This time, you have two types of queries:
1. First type has form 1 *l* *r* *x* and asks you to increase values of all *a**i* such that *l*<=β€<=*i*<=β€<=*r* by the non-negative integer *x*. 1. Second type has form 2 *y* and asks you to find value of *GukiZiana*(*a*,<=*y*).
For each query of type 2, print the answer and make GukiZ happy! | The first line contains two integers *n*, *q* (1<=β€<=*n*<=β€<=5<=*<=105,<=1<=β€<=*q*<=β€<=5<=*<=104), size of array *a*, and the number of queries.
The second line contains *n* integers *a*1,<=*a*2,<=... *a**n* (1<=β€<=*a**i*<=β€<=109), forming an array *a*.
Each of next *q* lines contain either four or two numbers, as described in statement:
If line starts with 1, then the query looks like 1 *l* *r* *x* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*, 0<=β€<=*x*<=β€<=109), first type query.
If line starts with 2, then th query looks like 2 *y* (1<=β€<=*y*<=β€<=109), second type query. | For each query of type 2, print the value of *GukiZiana*(*a*,<=*y*), for *y* value for that query. | [
"4 3\n1 2 3 4\n1 1 2 1\n1 1 1 1\n2 3\n",
"2 3\n1 2\n1 2 2 1\n2 3\n2 4\n"
] | [
"2\n",
"0\n-1\n"
] | none | [
{
"input": "4 3\n1 2 3 4\n1 1 2 1\n1 1 1 1\n2 3",
"output": "2"
},
{
"input": "2 3\n1 2\n1 2 2 1\n2 3\n2 4",
"output": "0\n-1"
},
{
"input": "8 5\n1 1 1 2 1 3 1 1\n2 1\n1 1 8 1\n2 2\n1 2 5 2\n2 4",
"output": "7\n7\n4"
},
{
"input": "8 8\n1 9 1 9 2 3 4 5\n1 3 7 1\n2 6\n2 8\n2 ... | 140 | 0 | 0 | 7,287 | |
774 | Lie or Truth | [
"*special",
"constructive algorithms",
"implementation",
"sortings"
] | null | null | Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to *a*1,<=*a*2,<=...,<=*a**n*.
While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to *b*1,<=*b*2,<=...,<=*b**n*.
Stepan said that he swapped only cubes which where on the positions between *l* and *r*, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions *l* and *r*, inclusive, in some way).
Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother. | The first line contains three integers *n*, *l*, *r* (1<=β€<=*n*<=β€<=105, 1<=β€<=*l*<=β€<=*r*<=β€<=*n*) β the number of Vasya's cubes and the positions told by Stepan.
The second line contains the sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*) β the sequence of integers written on cubes in the Vasya's order.
The third line contains the sequence *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=*n*) β the sequence of integers written on cubes after Stepan rearranged their order.
It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes. | Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes). | [
"5 2 4\n3 4 2 3 1\n3 2 3 4 1\n",
"3 1 2\n1 2 3\n3 1 2\n",
"4 2 4\n1 1 1 1\n1 1 1 1\n"
] | [
"TRUTH\n",
"LIE\n",
"TRUTH\n"
] | In the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).
In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.
In the third example for any values *l* and *r* there is a situation when Stepan said the truth. | [
{
"input": "5 2 4\n3 4 2 3 1\n3 2 3 4 1",
"output": "TRUTH"
},
{
"input": "3 1 2\n1 2 3\n3 1 2",
"output": "LIE"
},
{
"input": "4 2 4\n1 1 1 1\n1 1 1 1",
"output": "TRUTH"
},
{
"input": "5 1 3\n2 2 2 1 2\n2 2 2 1 2",
"output": "TRUTH"
},
{
"input": "7 1 4\n2 5 5 5... | 140 | 19,046,400 | 3 | 7,288 | |
580 | Kefa and Company | [
"binary search",
"sortings",
"two pointers"
] | null | null | Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.
Kefa has *n* friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least *d* units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company! | The first line of the input contains two space-separated integers, *n* and *d* (1<=β€<=*n*<=β€<=105, ) β the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.
Next *n* lines contain the descriptions of Kefa's friends, the (*i*<=+<=1)-th line contains the description of the *i*-th friend of type *m**i*, *s**i* (0<=β€<=*m**i*,<=*s**i*<=β€<=109) β the amount of money and the friendship factor, respectively. | Print the maximum total friendship factir that can be reached. | [
"4 5\n75 5\n0 100\n150 20\n75 1\n",
"5 100\n0 7\n11 32\n99 10\n46 8\n87 54\n"
] | [
"100\n",
"111\n"
] | In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.
In the second sample test we can take all the friends. | [
{
"input": "4 5\n75 5\n0 100\n150 20\n75 1",
"output": "100"
},
{
"input": "5 100\n0 7\n11 32\n99 10\n46 8\n87 54",
"output": "111"
},
{
"input": "1 1000000000\n15 12",
"output": "12"
},
{
"input": "5 1\n5 9\n2 10\n8 5\n18 12\n1 1",
"output": "12"
},
{
"input": "3... | 0 | 0 | 0 | 7,294 | |
3 | Tic-tac-toe | [
"brute force",
"games",
"implementation"
] | C. Tic-tac-toe | 1 | 64 | Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3<=Γ<=3 grid (one player always draws crosses, the other β noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced.
You are given a 3<=Γ<=3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below:
- illegal β if the given board layout can't appear during a valid game; - the first player won β if in the given board layout the first player has just won; - the second player won β if in the given board layout the second player has just won; - draw β if the given board layout has just let to a draw. | The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero). | Print one of the six verdicts: first, second, illegal, the first player won, the second player won or draw. | [
"X0X\n.0.\n.X.\n"
] | [
"second\n"
] | none | [
{
"input": "X0X\n.0.\n.X.",
"output": "second"
},
{
"input": "0.X\nXX.\n000",
"output": "illegal"
},
{
"input": "XXX\n.0.\n000",
"output": "illegal"
},
{
"input": "XXX\n...\n000",
"output": "illegal"
},
{
"input": "X.X\nX..\n00.",
"output": "second"
},
{
... | 218 | 0 | 0 | 7,297 |
512 | Fox And Travelling | [
"dp",
"trees"
] | null | null | Fox Ciel is going to travel to New Foxland during this summer.
New Foxland has *n* attractions that are linked by *m* undirected roads. Two attractions are called adjacent if they are linked by a road. Fox Ciel has *k* days to visit this city and each day she will visit exactly one attraction.
There is one important rule in New Foxland: you can't visit an attraction if it has more than one adjacent attraction that you haven't visited yet.
At the beginning Fox Ciel haven't visited any attraction. During her travelling she may move aribtrarly between attraction. After visiting attraction *a*, she may travel to any attraction *b* satisfying conditions above that hasn't been visited yet, even if it is not reachable from *a* by using the roads (Ciel uses boat for travelling between attractions, so it is possible).
She wants to know how many different travelling plans she can make. Calculate this number modulo 109<=+<=9 for every *k* from 0 to *n* since she hasn't decided for how many days she is visiting New Foxland. | First line contains two integers: *n*, *m* (1<=β€<=*n*<=β€<=100, ), the number of attractions and number of undirected roads.
Then next *m* lines each contain two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n* and *a**i*<=β <=*b**i*), describing a road. There is no more than one road connecting each pair of attractions. | Output *n*<=+<=1 integer: the number of possible travelling plans modulo 109<=+<=9 for all *k* from 0 to *n*. | [
"3 2\n1 2\n2 3\n",
"4 4\n1 2\n2 3\n3 4\n4 1\n",
"12 11\n2 3\n4 7\n4 5\n5 6\n4 6\n6 12\n5 12\n5 8\n8 9\n10 8\n11 9\n",
"13 0\n"
] | [
"1\n2\n4\n4\n",
"1\n0\n0\n0\n0\n",
"1\n6\n31\n135\n483\n1380\n3060\n5040\n5040\n0\n0\n0\n0\n",
"1\n13\n156\n1716\n17160\n154440\n1235520\n8648640\n51891840\n259459200\n37836791\n113510373\n227020746\n227020746\n"
] | In the first sample test for *k*β=β3 there are 4 travelling plans: {1,β2,β3},β{1,β3,β2},β{3,β1,β2},β{3,β2,β1}.
In the second sample test Ciel can't visit any attraction in the first day, so for *k*β>β0 the answer is 0.
In the third sample test Foxlands look like this: | [
{
"input": "3 2\n1 2\n2 3",
"output": "1\n2\n4\n4"
},
{
"input": "4 4\n1 2\n2 3\n3 4\n4 1",
"output": "1\n0\n0\n0\n0"
},
{
"input": "12 11\n2 3\n4 7\n4 5\n5 6\n4 6\n6 12\n5 12\n5 8\n8 9\n10 8\n11 9",
"output": "1\n6\n31\n135\n483\n1380\n3060\n5040\n5040\n0\n0\n0\n0"
},
{
"inp... | 15 | 0 | 0 | 7,300 | |
248 | Chilly Willy | [
"math",
"number theory"
] | null | null | Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up with a few games that were connected with them.
Chilly Willy wants to find the minimum number of length *n*, such that it is simultaneously divisible by all numbers Willy already knows (2, 3, 5 and 7). Help him with that.
A number's length is the number of digits in its decimal representation without leading zeros. | A single input line contains a single integer *n* (1<=β€<=*n*<=β€<=105). | Print a single integer β the answer to the problem without leading zeroes, or "-1" (without the quotes), if the number that meet the problem condition does not exist. | [
"1\n",
"5\n"
] | [
"-1\n",
"10080"
] | none | [
{
"input": "1",
"output": "-1"
},
{
"input": "5",
"output": "10080"
},
{
"input": "6",
"output": "100170"
},
{
"input": "4",
"output": "1050"
},
{
"input": "15",
"output": "100000000000110"
},
{
"input": "16",
"output": "1000000000000050"
},
{
... | 122 | 0 | 0 | 7,312 | |
656 | Without Text | [
"*special"
] | null | null | You can preview the image in better quality by the link: [http://assets.codeforces.com/files/656/without-text.png](//assets.codeforces.com/files/656/without-text.png) | The only line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be an alphanumeric character or a full stop ".". | Output the required answer. | [
"Codeforces\n",
"APRIL.1st\n"
] | [
"-87\n",
"17\n"
] | none | [
{
"input": "Codeforces",
"output": "-87"
},
{
"input": "APRIL.1st",
"output": "17"
},
{
"input": ".0.1.2.",
"output": "0"
},
{
"input": "CODEcode",
"output": "0"
},
{
"input": "A",
"output": "1"
},
{
"input": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ... | 156 | 0 | 3 | 7,321 | |
468 | Two Sets | [
"2-sat",
"dfs and similar",
"dsu",
"graph matchings",
"greedy"
] | null | null | Little X has *n* distinct integers: *p*1,<=*p*2,<=...,<=*p**n*. He wants to divide all of them into two sets *A* and *B*. The following two conditions must be satisfied:
- If number *x* belongs to set *A*, then number *a*<=-<=*x* must also belong to set *A*. - If number *x* belongs to set *B*, then number *b*<=-<=*x* must also belong to set *B*.
Help Little X divide the numbers into two sets or determine that it's impossible. | The first line contains three space-separated integers *n*,<=*a*,<=*b* (1<=β€<=*n*<=β€<=105;Β 1<=β€<=*a*,<=*b*<=β€<=109). The next line contains *n* space-separated distinct integers *p*1,<=*p*2,<=...,<=*p**n*Β (1<=β€<=*p**i*<=β€<=109). | If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print *n* integers: *b*1,<=*b*2,<=...,<=*b**n* (*b**i* equals either 0, or 1), describing the division. If *b**i* equals to 0, then *p**i* belongs to set *A*, otherwise it belongs to set *B*.
If it's impossible, print "NO" (without the quotes). | [
"4 5 9\n2 3 4 5\n",
"3 3 4\n1 2 4\n"
] | [
"YES\n0 0 1 1\n",
"NO\n"
] | It's OK if all the numbers are in the same set, and the other one is empty. | [
{
"input": "4 5 9\n2 3 4 5",
"output": "YES\n0 0 1 1"
},
{
"input": "3 3 4\n1 2 4",
"output": "NO"
},
{
"input": "100 8883 915\n1599 4666 663 3646 754 2113 2200 3884 4082 1640 3795 2564 2711 2766 1122 4525 1779 2678 2816 2182 1028 2337 4918 1273 4141 217 2682 1756 309 4744 915 1351 3302 ... | 140 | 409,600 | 0 | 7,325 | |
25 | Test | [
"hashing",
"strings"
] | E. Test | 2 | 256 | Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings β input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring *s*1, the second enters an infinite loop if the input data contains the substring *s*2, and the third requires too much memory if the input data contains the substring *s*3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions? | There are exactly 3 lines in the input data. The *i*-th line contains string *s**i*. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105. | Output one number β what is minimal length of the string, containing *s*1, *s*2 and *s*3 as substrings. | [
"ab\nbc\ncd\n",
"abacaba\nabaaba\nx\n"
] | [
"4\n",
"11\n"
] | none | [
{
"input": "ab\nbc\ncd",
"output": "4"
},
{
"input": "abacaba\nabaaba\nx",
"output": "11"
},
{
"input": "syvncqmfhautvxudqdhggz\nhrpxzeghsocjpicuixskfuzupytsgjsdiyb\nybcmnmnbpndbxlxbzhbfnqvwcffvrdhtickyqhupmcehls",
"output": "100"
},
{
"input": "jwdezvgfm\nmdoqvylpuvyk\nqylld... | 30 | 0 | 0 | 7,326 |
745 | Hongcow Solves A Puzzle | [
"implementation"
] | null | null | Hongcow likes solving puzzles.
One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an *n* by *m* grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.
The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.
You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | The first line of input will contain two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=500), the dimensions of the puzzle piece.
The next *n* lines will describe the jigsaw piece. Each line will have length *m* and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space.
It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | [
"2 3\nXXX\nXXX\n",
"2 2\n.X\nXX\n",
"5 5\n.....\n..X..\n.....\n.....\n.....\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | For the first sample, one example of a rectangle we can form is as follows
For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.
In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: | [
{
"input": "2 3\nXXX\nXXX",
"output": "YES"
},
{
"input": "2 2\n.X\nXX",
"output": "NO"
},
{
"input": "1 500\n.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX... | 420 | 614,400 | 3 | 7,330 | |
584 | Dima and Lisa | [
"brute force",
"math",
"number theory"
] | null | null | Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes.
More formally, you are given an odd numer *n*. Find a set of numbers *p**i* (1<=β€<=*i*<=β€<=*k*), such that
1. 1<=β€<=*k*<=β€<=31. *p**i* is a prime1.
The numbers *p**i* do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists. | The single line contains an odd number *n* (3<=β€<=*n*<=<<=109). | In the first line print *k* (1<=β€<=*k*<=β€<=3), showing how many numbers are in the representation you found.
In the second line print numbers *p**i* in any order. If there are multiple possible solutions, you can print any of them. | [
"27\n"
] | [
"3\n5 11 11\n"
] | A prime is an integer strictly larger than one that is divisible only by one and by itself. | [
{
"input": "27",
"output": "3\n2 2 23"
},
{
"input": "3",
"output": "1\n3"
},
{
"input": "25",
"output": "2\n2 23"
},
{
"input": "9",
"output": "2\n2 7"
},
{
"input": "91",
"output": "2\n2 89"
},
{
"input": "57",
"output": "3\n2 2 53"
},
{
... | 124 | 4,096,000 | 3 | 7,332 | |
678 | The Same Calendar | [
"implementation"
] | null | null | The girl Taylor has a beautiful calendar for the year *y*. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
The calendar is so beautiful that she wants to know what is the next year after *y* when the calendar will be exactly the same. Help Taylor to find that year.
Note that leap years has 366 days. The year is leap if it is divisible by 400 or it is divisible by 4, but not by 100 ([https://en.wikipedia.org/wiki/Leap_year](https://en.wikipedia.org/wiki/Leap_year)). | The only line contains integer *y* (1000<=β€<=*y*<=<<=100'000) β the year of the calendar. | Print the only integer *y*' β the next year after *y* when the calendar will be the same. Note that you should find the first year after *y* with the same calendar. | [
"2016\n",
"2000\n",
"50501\n"
] | [
"2044\n",
"2028\n",
"50507\n"
] | Today is Monday, the 13th of June, 2016. | [
{
"input": "2016",
"output": "2044"
},
{
"input": "2000",
"output": "2028"
},
{
"input": "50501",
"output": "50507"
},
{
"input": "1000",
"output": "1006"
},
{
"input": "1900",
"output": "1906"
},
{
"input": "1899",
"output": "1905"
},
{
"i... | 109 | 0 | 0 | 7,340 | |
169 | Replacing Digits | [
"greedy"
] | null | null | You are given an integer *a* that consists of *n* digits. You are also given a sequence of digits *s* of length *m*. The digit in position *j* (1<=β€<=*j*<=β€<=*m*) of sequence *s* means that you can choose an arbitrary position *i* (1<=β€<=*i*<=β€<=*n*) in *a* and replace the digit in the chosen position *i* with *s**j*. Each element in the sequence *s* can participate in no more than one replacing operation.
Your task is to perform such sequence of replacements, that the given number *a* gets maximum value. You are allowed to use not all elements from *s*. | The first line contains positive integer *a*. Its length *n* is positive and doesn't exceed 105. The second line contains sequence of digits *s*. Its length *m* is positive and doesn't exceed 105. The digits in the sequence *s* are written consecutively without any separators.
The given number *a* doesn't contain leading zeroes. | Print the maximum value that can be obtained from *a* after a series of replacements. You are allowed to use not all elements from *s*. The printed number shouldn't contain any leading zeroes. | [
"1024\n010\n",
"987\n1234567\n"
] | [
"1124\n",
"987\n"
] | none | [
{
"input": "1024\n010",
"output": "1124"
},
{
"input": "987\n1234567",
"output": "987"
},
{
"input": "10\n1",
"output": "11"
},
{
"input": "11\n1",
"output": "11"
},
{
"input": "12\n2",
"output": "22"
},
{
"input": "1\n0",
"output": "1"
},
{
... | 216 | 3,481,600 | -1 | 7,353 | |
985 | Chess Placing | [
"implementation"
] | null | null | You are given a chessboard of size 1<=Γ<=*n*. It is guaranteed that *n* is even. The chessboard is painted like this: "BWBW...BW".
Some cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to .
In one step you can move one of the pieces one cell to the left or to the right. You cannot move pieces beyond the borders of the board. You also cannot move pieces to the cells that are already occupied.
Your task is to place all the pieces in the cells of the same color using the minimum number of moves (all the pieces must occupy only the black cells or only the white cells after all the moves are made). | The first line of the input contains one integer *n* (2<=β€<=*n*<=β€<=100, *n* is even) β the size of the chessboard.
The second line of the input contains integer numbers (1<=β€<=*p**i*<=β€<=*n*) β initial positions of the pieces. It is guaranteed that all the positions are distinct. | Print one integer β the minimum number of moves you have to make to place all the pieces in the cells of the same color. | [
"6\n1 2 6\n",
"10\n1 2 3 4 5\n"
] | [
"2\n",
"10\n"
] | In the first example the only possible strategy is to move the piece at the position 6 to the position 5 and move the piece at the position 2 to the position 3. Notice that if you decide to place the pieces in the white cells the minimum number of moves will be 3.
In the second example the possible strategy is to move <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e1e06f6a15cce30628c7a2360c4ffa57a8ba0ebd.png" style="max-width: 100.0%;max-height: 100.0%;"/> in 4 moves, then <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c84dfbe0c6a917b45fc3f69467c256c4ac460eeb.png" style="max-width: 100.0%;max-height: 100.0%;"/> in 3 moves, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/598731d81393332209d914cb0bbe97d8566c887d.png" style="max-width: 100.0%;max-height: 100.0%;"/> in 2 moves and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/> in 1 move. | [
{
"input": "6\n1 2 6",
"output": "2"
},
{
"input": "10\n1 2 3 4 5",
"output": "10"
},
{
"input": "2\n2",
"output": "0"
},
{
"input": "100\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 ... | 109 | 0 | 0 | 7,379 | |
706 | Vasiliy's Multiset | [
"binary search",
"bitmasks",
"data structures",
"trees"
] | null | null | Author has gone out of the stories about Vasiliy, so here is just a formal task description.
You are given *q* queries and a multiset *A*, initially containing only integer 0. There are three types of queries:
1. "+ x"Β β add integer *x* to multiset *A*.1. "- x"Β β erase one occurrence of integer *x* from multiset *A*. It's guaranteed that at least one *x* is present in the multiset *A* before this query.1. "? x"Β β you are given integer *x* and need to compute the value , i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer *x* and some integer *y* from the multiset *A*.
Multiset is a set, where equal elements are allowed. | The first line of the input contains a single integer *q* (1<=β€<=*q*<=β€<=200<=000)Β β the number of queries Vasiliy has to perform.
Each of the following *q* lines of the input contains one of three characters '+', '-' or '?' and an integer *x**i* (1<=β€<=*x**i*<=β€<=109). It's guaranteed that there is at least one query of the third type.
Note, that the integer 0 will always be present in the set *A*. | For each query of the type '?' print one integerΒ β the maximum value of bitwise exclusive OR (XOR) of integer *x**i* and some integer from the multiset *A*. | [
"10\n+ 8\n+ 9\n+ 11\n+ 6\n+ 1\n? 3\n- 8\n? 3\n? 8\n? 11\n"
] | [
"11\n10\n14\n13\n"
] | After first five operations multiset *A* contains integers 0, 8, 9, 11, 6 and 1.
The answer for the sixth query is integer <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9e2f3d4f1a7c134a5695ba1d548df2b4f9292206.png" style="max-width: 100.0%;max-height: 100.0%;"/>Β β maximum among integers <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6cb1b5a0f9bbad177b1de700e918187c8fb3e972.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7644536d2010111824755b5dd2b6043340b2bf4d.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9a437d1611f6c2ae5cd9a275a6ab2df61296988e.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4e3eeab99fa495ecdcd103c68de47dd72943016f.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/97b73b0f3e59862ed9f80fa83527d97e0ed0a084.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "10\n+ 8\n+ 9\n+ 11\n+ 6\n+ 1\n? 3\n- 8\n? 3\n? 8\n? 11",
"output": "11\n10\n14\n13"
},
{
"input": "12\n+ 4\n+ 4\n+ 5\n? 3\n- 4\n? 3\n- 4\n? 3\n? 3\n- 5\n+ 10\n? 1",
"output": "7\n7\n6\n6\n11"
},
{
"input": "10\n? 1\n+ 1\n+ 8\n- 1\n+ 2\n+ 7\n+ 4\n+ 7\n+ 3\n? 7",
"output": ... | 46 | 0 | 0 | 7,386 | |
426 | Sereja and Mugs | [
"implementation"
] | null | null | Sereja showed an interesting game to his friends. The game goes like that. Initially, there is a table with an empty cup and *n* water mugs on it. Then all players take turns to move. During a move, a player takes a non-empty mug of water and pours all water from it into the cup. If the cup overfills, then we assume that this player lost.
As soon as Sereja's friends heard of the game, they wanted to play it. Sereja, on the other hand, wanted to find out whether his friends can play the game in such a way that there are no losers. You are given the volumes of all mugs and the cup. Also, you know that Sereja has (*n*<=-<=1) friends. Determine if Sereja's friends can play the game so that nobody loses. | The first line contains integers *n* and *s* (2<=β€<=*n*<=β€<=100;Β 1<=β€<=*s*<=β€<=1000) β the number of mugs and the volume of the cup. The next line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=10). Number *a**i* means the volume of the *i*-th mug. | In a single line, print "YES" (without the quotes) if his friends can play in the described manner, and "NO" (without the quotes) otherwise. | [
"3 4\n1 1 1\n",
"3 4\n3 1 3\n",
"3 4\n4 4 4\n"
] | [
"YES\n",
"YES\n",
"NO\n"
] | none | [
{
"input": "3 4\n1 1 1",
"output": "YES"
},
{
"input": "3 4\n3 1 3",
"output": "YES"
},
{
"input": "3 4\n4 4 4",
"output": "NO"
},
{
"input": "2 1\n1 10",
"output": "YES"
},
{
"input": "3 12\n5 6 6",
"output": "YES"
},
{
"input": "4 10\n6 3 8 7",
"... | 77 | 6,758,400 | 3 | 7,388 | |
550 | Divisibility by Eight | [
"brute force",
"dp",
"math"
] | null | null | You are given a non-negative integer *n*, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it. | The single line of the input contains a non-negative integer *n*. The representation of number *n* doesn't contain any leading zeroes and its length doesn't exceed 100 digits. | Print "NO" (without quotes), if there is no such way to remove some digits from number *n*.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number *n* in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them. | [
"3454\n",
"10\n",
"111111\n"
] | [
"YES\n344\n",
"YES\n0\n",
"NO\n"
] | none | [
{
"input": "3454",
"output": "YES\n344"
},
{
"input": "10",
"output": "YES\n0"
},
{
"input": "111111",
"output": "NO"
},
{
"input": "8996988892",
"output": "YES\n8"
},
{
"input": "5555555555",
"output": "NO"
},
{
"input": "1",
"output": "NO"
},
... | 46 | 0 | 0 | 7,401 | |
610 | Harmony Analysis | [
"constructive algorithms"
] | null | null | The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or <=-<=1 and any two vectors are orthogonal. Just as a reminder, two vectors in *n*-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is:
Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2*k* vectors in 2*k*-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it? | The only line of the input contains a single integer *k* (0<=β€<=*k*<=β€<=9). | Print 2*k* lines consisting of 2*k* characters each. The *j*-th character of the *i*-th line must be equal to '<=*<=' if the *j*-th coordinate of the *i*-th vector is equal to <=-<=1, and must be equal to '<=+<=' if it's equal to <=+<=1. It's guaranteed that the answer always exists.
If there are many correct answers, print any. | [
"2\n"
] | [
"++**\n+*+*\n++++\n+**+"
] | Consider all scalar products in example:
- Vectors 1 and 2: (β+β1)Β·(β+β1)β+β(β+β1)Β·(β-β1)β+β(β-β1)Β·(β+β1)β+β(β-β1)Β·(β-β1)β=β0 - Vectors 1 and 3: (β+β1)Β·(β+β1)β+β(β+β1)Β·(β+β1)β+β(β-β1)Β·(β+β1)β+β(β-β1)Β·(β+β1)β=β0 - Vectors 1 and 4: (β+β1)Β·(β+β1)β+β(β+β1)Β·(β-β1)β+β(β-β1)Β·(β-β1)β+β(β-β1)Β·(β+β1)β=β0 - Vectors 2 and 3: (β+β1)Β·(β+β1)β+β(β-β1)Β·(β+β1)β+β(β+β1)Β·(β+β1)β+β(β-β1)Β·(β+β1)β=β0 - Vectors 2 and 4: (β+β1)Β·(β+β1)β+β(β-β1)Β·(β-β1)β+β(β+β1)Β·(β-β1)β+β(β-β1)Β·(β+β1)β=β0 - Vectors 3 and 4: (β+β1)Β·(β+β1)β+β(β+β1)Β·(β-β1)β+β(β+β1)Β·(β-β1)β+β(β+β1)Β·(β+β1)β=β0 | [
{
"input": "2",
"output": "++++\n+*+*\n++**\n+**+"
},
{
"input": "1",
"output": "++\n+*"
},
{
"input": "3",
"output": "++++++++\n+*+*+*+*\n++**++**\n+**++**+\n++++****\n+*+**+*+\n++****++\n+**+*++*"
},
{
"input": "0",
"output": "+"
},
{
"input": "4",
"output":... | 30 | 0 | 0 | 7,404 | |
883 | Field of Wonders | [
"implementation",
"strings"
] | null | null | Polycarpus takes part in the "Field of Wonders" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden.
The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is "abacaba" and the player tells the letter "a", the host will reveal letters at all positions, occupied by "a": 1, 3, 5 and 7 (positions are numbered from left to right starting from 1).
Polycarpus knows *m* words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these *m* words.
At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed.
It is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed.
Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=50) β the length of the hidden word.
The following line describes already revealed letters. It contains the string of length *n*, which consists of lowercase Latin letters and symbols "*". If there is a letter at some position, then this letter was already revealed. If the position contains symbol "*", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed.
The third line contains an integer *m* (1<=β€<=*m*<=β€<=1000) β the number of words of length *n*, which Polycarpus knows. The following *m* lines contain the words themselves β *n*-letter strings of lowercase Latin letters. All words are distinct.
It is guaranteed that the hidden word appears as one of the given *m* words. Before the current move Polycarp has told exactly the letters which are currently revealed. | Output the single integer β the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero. | [
"4\na**d\n2\nabcd\nacbd\n",
"5\nlo*er\n2\nlover\nloser\n",
"3\na*a\n2\naaa\naba\n"
] | [
"2\n",
"0\n",
"1\n"
] | In the first example Polycarpus can tell letters "b" and "c", which assuredly will be revealed.
The second example contains no letters which can be told as it is not clear, which of the letters "v" or "s" is located at the third position of the hidden word.
In the third example Polycarpus exactly knows that the hidden word is "aba", because in case it was "aaa", then the second letter "a" would have already been revealed in one of previous turns. | [
{
"input": "4\na**d\n2\nabcd\nacbd",
"output": "2"
},
{
"input": "5\nlo*er\n2\nlover\nloser",
"output": "0"
},
{
"input": "3\na*a\n2\naaa\naba",
"output": "1"
},
{
"input": "1\n*\n1\na",
"output": "1"
},
{
"input": "1\n*\n1\nz",
"output": "1"
},
{
"inp... | 15 | 0 | 0 | 7,406 | |
234 | Practice | [
"constructive algorithms",
"divide and conquer",
"implementation"
] | null | null | Little time is left before Berland annual football championship. Therefore the coach of team "Losewille Rangers" decided to resume the practice, that were indefinitely interrupted for uncertain reasons. Overall there are *n* players in "Losewille Rangers". Each player on the team has a number β a unique integer from 1 to *n*. To prepare for the championship, the coach Mr. Floppe decided to spend some number of practices.
Mr. Floppe spent some long nights of his holiday planning how to conduct the practices. He came to a very complex practice system. Each practice consists of one game, all *n* players of the team take part in the game. The players are sorted into two teams in some way. In this case, the teams may have different numbers of players, but each team must have at least one player.
The coach wants to be sure that after the series of the practice sessions each pair of players had at least one practice, when they played in different teams. As the players' energy is limited, the coach wants to achieve the goal in the least number of practices.
Help him to schedule the practices. | A single input line contains integer *n* (2<=β€<=*n*<=β€<=1000). | In the first line print *m* β the minimum number of practices the coach will have to schedule. Then print the descriptions of the practices in *m* lines.
In the *i*-th of those lines print *f**i* β the number of players in the first team during the *i*-th practice (1<=β€<=*f**i*<=<<=*n*), and *f**i* numbers from 1 to *n* β the numbers of players in the first team. The rest of the players will play in the second team during this practice. Separate numbers on a line with spaces. Print the numbers of the players in any order. If there are multiple optimal solutions, print any of them. | [
"2\n",
"3\n"
] | [
"1\n1 1\n",
"2\n2 1 2\n1 1\n"
] | none | [
{
"input": "2",
"output": "1\n1 1"
},
{
"input": "3",
"output": "2\n2 1 2\n1 1"
},
{
"input": "4",
"output": "2\n2 1 2\n2 1 3"
},
{
"input": "5",
"output": "3\n3 1 2 3\n3 1 2 4\n1 1"
},
{
"input": "6",
"output": "3\n3 1 2 3\n4 1 2 4 5\n2 1 4"
},
{
"inp... | 248 | 3,276,800 | 3 | 7,447 | |
0 | none | [
"none"
] | null | null | The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness".
The test consists of *n* questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question *n*. Question *i* contains *a**i* answer variants, exactly one of them is correct.
A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the *n* questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.
Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? | The first line contains a positive integer *n* (1<=β€<=*n*<=β€<=100). It is the number of questions in the test. The second line contains space-separated *n* positive integers *a**i* (1<=β€<=*a**i*<=β€<=109), the number of answer variants to question *i*. | Print a single number β the minimal number of clicks needed to pass the test it the worst-case scenario.
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. | [
"2\n1 1\n",
"2\n2 2\n",
"1\n10\n"
] | [
"2",
"5",
"10"
] | Note to the second sample. In the worst-case scenario you will need five clicks:
- the first click selects the first variant to the first question, this answer turns out to be wrong. - the second click selects the second variant to the first question, it proves correct and we move on to the second question; - the third click selects the first variant to the second question, it is wrong and we go back to question 1; - the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; - the fifth click selects the second variant to the second question, it proves correct, the test is finished. | [
{
"input": "2\n1 1",
"output": "2"
},
{
"input": "2\n2 2",
"output": "5"
},
{
"input": "1\n10",
"output": "10"
},
{
"input": "3\n2 4 1",
"output": "10"
},
{
"input": "4\n5 5 3 1",
"output": "22"
},
{
"input": "2\n1000000000 1000000000",
"output": "... | 124 | 4,710,400 | 3 | 7,450 | |
895 | Square Subsets | [
"bitmasks",
"combinatorics",
"dp",
"math"
] | null | null | Petya was late for the lesson too. The teacher gave him an additional task. For some array *a* Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109<=+<=7. | First line contains one integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of elements in the array.
Second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=70)Β β the elements of the array. | Print one integerΒ β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109<=+<=7. | [
"4\n1 1 1 1\n",
"4\n2 2 2 2\n",
"5\n1 2 4 5 8\n"
] | [
"15\n",
"7\n",
"7\n"
] | In first sample product of elements chosen by any way is 1 and 1β=β1<sup class="upper-index">2</sup>. So the answer is 2<sup class="upper-index">4</sup>β-β1β=β15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6β+β1β=β7. | [
{
"input": "4\n1 1 1 1",
"output": "15"
},
{
"input": "4\n2 2 2 2",
"output": "7"
},
{
"input": "5\n1 2 4 5 8",
"output": "7"
},
{
"input": "1\n64",
"output": "1"
},
{
"input": "5\n2 2 2 2 2",
"output": "15"
},
{
"input": "6\n1 2 3 4 5 6",
"output"... | 4,000 | 41,472,000 | 0 | 7,451 | |
424 | Magic Formulas | [
"math"
] | null | null | People in the Tomskaya region like magic formulas very much. You can see some of them below.
Imagine you are given a sequence of positive integer numbers *p*1, *p*2, ..., *p**n*. Lets write down some magic formulas:
Here, "mod" means the operation of taking the residue after dividing.
The expression means applying the bitwise *xor* (excluding "OR") operation to integers *x* and *y*. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by "^", in Pascal β by "xor".
People in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence *p*, calculate the value of *Q*. | The first line of the input contains the only integer *n* (1<=β€<=*n*<=β€<=106). The next line contains *n* integers: *p*1,<=*p*2,<=...,<=*p**n* (0<=β€<=*p**i*<=β€<=2Β·109). | The only line of output should contain a single integer β the value of *Q*. | [
"3\n1 2 3\n"
] | [
"3\n"
] | none | [
{
"input": "3\n1 2 3",
"output": "3"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "2\n65535 0",
"output": "65534"
},
{
"input": "10\n1356106972 165139648 978829595 410669403 873711167 287346624 117863440 228957745 835903650 1575323015",
"output": "948506286"
},
{... | 46 | 0 | 0 | 7,459 | |
163 | e-Government | [
"data structures",
"dfs and similar",
"dp",
"strings",
"trees"
] | null | null | The best programmers of Embezzland compete to develop a part of the project called "e-Government" β the system of automated statistic collecting and press analysis.
We know that any of the *k* citizens can become a member of the Embezzland government. The citizens' surnames are *a*1,<=*a*2,<=...,<=*a**k*. All surnames are different. Initially all *k* citizens from this list are members of the government. The system should support the following options:
- Include citizen *a**i* to the government. - Exclude citizen *a**i* from the government. - Given a newspaper article text, calculate how politicized it is. To do this, for every active government member the system counts the number of times his surname occurs in the text as a substring. All occurrences are taken into consideration, including the intersecting ones. The degree of politicization of a text is defined as the sum of these values for all active government members.
Implement this system. | The first line contains space-separated integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=105) β the number of queries to the system and the number of potential government members.
Next *k* lines contain the surnames *a*1,<=*a*2,<=...,<=*a**k*, one per line. All surnames are pairwise different.
Next *n* lines contain queries to the system, one per line. Each query consists of a character that determines an operation and the operation argument, written consecutively without a space.
Operation "include in the government" corresponds to the character "+", operation "exclude" corresponds to "-". An argument of those operations is an integer between 1 and *k* β the index of the citizen involved in the operation. Any citizen can be included and excluded from the government an arbitrary number of times in any order. Including in the government a citizen who is already there or excluding the citizen who isn't there changes nothing.
The operation "calculate politicization" corresponds to character "?". Its argument is a text.
All strings β surnames and texts β are non-empty sequences of lowercase Latin letters. The total length of all surnames doesn't exceed 106, the total length of all texts doesn't exceed 106. | For any "calculate politicization" operation print on a separate line the degree of the politicization of the given text. Print nothing for other operations. | [
"7 3\na\naa\nab\n?aaab\n-2\n?aaab\n-3\n?aaab\n+2\n?aabbaa\n"
] | [
"6\n4\n3\n6\n"
] | none | [] | 1,000 | 11,673,600 | 0 | 7,461 | |
190 | Surrounded | [
"geometry"
] | null | null | So, the Berland is at war with its eternal enemy Flatland again, and Vasya, an accountant, was assigned to fulfil his duty to the nation.
Right now the situation in Berland is dismal β their both cities are surrounded! The armies of flatlanders stand on the borders of circles, the circles' centers are in the surrounded cities. At any moment all points of the flatland ring can begin to move quickly in the direction of the city β that's the strategy the flatlanders usually follow when they besiege cities.
The berlanders are sure that they can repel the enemy's attack if they learn the exact time the attack starts. For that they need to construct a radar that would register any movement at the distance of at most *r* from it. Thus, we can install a radar at such point, that at least one point of the enemy ring will be in its detecting range (that is, at a distance of at most *r*). Then the radar can immediately inform about the enemy's attack.
Due to the newest technologies, we can place a radar at any point without any problems. But the problem is that the berlanders have the time to make only one radar. Besides, the larger the detection radius (*r*) is, the more the radar costs.
That's why Vasya's task (that is, your task) is to find the minimum possible detection radius for the radar. In other words, your task is to find the minimum radius *r* (*r*<=β₯<=0) such, that a radar with radius *r* can be installed at some point and it can register the start of the movements of both flatland rings from that point.
In this problem you can consider the cities as material points, the attacking enemy rings - as circles with centers in the cities, the radar's detection range β as a disk (including the border) with the center at the point where the radar is placed. | The input files consist of two lines. Each line represents the city and the flatland ring that surrounds it as three space-separated integers *x**i*, *y**i*, *r**i* (|*x**i*|,<=|*y**i*|<=β€<=104;Β 1<=β€<=*r**i*<=β€<=104) β the city's coordinates and the distance from the city to the flatlanders, correspondingly.
It is guaranteed that the cities are located at different points. | Print a single real number β the minimum detection radius of the described radar. The answer is considered correct if the absolute or relative error does not exceed 10<=-<=6. | [
"0 0 1\n6 0 3\n",
"-10 10 3\n10 -10 3\n"
] | [
"1.000000000000000",
"11.142135623730951"
] | The figure below shows the answer to the first sample. In this sample the best decision is to put the radar at point with coordinates (2,β0).
The figure below shows the answer for the second sample. In this sample the best decision is to put the radar at point with coordinates (0,β0). | [
{
"input": "0 0 1\n6 0 3",
"output": "1.000000000000000"
},
{
"input": "-10 10 3\n10 -10 3",
"output": "11.142135623730951"
},
{
"input": "2 1 3\n8 9 5",
"output": "1.000000000000000"
},
{
"input": "0 0 1\n-10 -10 9",
"output": "2.071067811865475"
},
{
"input": "1... | 109 | 6,963,200 | 3 | 7,502 | |
165 | Compatible Numbers | [
"bitmasks",
"brute force",
"dfs and similar",
"dp"
] | null | null | Two integers *x* and *y* are compatible, if the result of their bitwise "AND" equals zero, that is, *a* & *b*<==<=0. For example, numbers 90 (10110102) and 36 (1001002) are compatible, as 10110102 & 1001002<==<=02, and numbers 3 (112) and 6 (1102) are not compatible, as 112 & 1102<==<=102.
You are given an array of integers *a*1,<=*a*2,<=...,<=*a**n*. Your task is to find the following for each array element: is this element compatible with some other element from the given array? If the answer to this question is positive, then you also should find any suitable element. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=106) β the number of elements in the given array. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=4Β·106) β the elements of the given array. The numbers in the array can coincide. | Print *n* integers *ans**i*. If *a**i* isn't compatible with any other element of the given array *a*1,<=*a*2,<=...,<=*a**n*, then *ans**i* should be equal to -1. Otherwise *ans**i* is any such number, that *a**i* & *ans**i*<==<=0, and also *ans**i* occurs in the array *a*1,<=*a*2,<=...,<=*a**n*. | [
"2\n90 36\n",
"4\n3 6 3 6\n",
"5\n10 6 9 8 2\n"
] | [
"36 90",
"-1 -1 -1 -1",
"-1 8 2 2 8"
] | none | [
{
"input": "2\n90 36",
"output": "36 90"
},
{
"input": "4\n3 6 3 6",
"output": "-1 -1 -1 -1"
},
{
"input": "5\n10 6 9 8 2",
"output": "-1 8 2 2 8"
},
{
"input": "10\n4 9 8 3 2 6 8 2 9 7",
"output": "8 4 4 8 8 8 4 8 4 8"
},
{
"input": "10\n3 5 18 12 4 20 11 19 15 6... | 1,776 | 268,390,400 | 0 | 7,505 | |
482 | Diverse Permutation | [
"constructive algorithms",
"greedy"
] | null | null | Permutation *p* is an ordered set of integers *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*, consisting of *n* distinct positive integers not larger than *n*. We'll denote as *n* the length of permutation *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*.
Your task is to find such permutation *p* of length *n*, that the group of numbers |*p*1<=-<=*p*2|,<=|*p*2<=-<=*p*3|,<=...,<=|*p**n*<=-<=1<=-<=*p**n*| has exactly *k* distinct elements. | The single line of the input contains two space-separated positive integers *n*, *k* (1<=β€<=*k*<=<<=*n*<=β€<=105). | Print *n* integers forming the permutation. If there are multiple answers, print any of them. | [
"3 2\n",
"3 1\n",
"5 2\n"
] | [
"1 3 2\n",
"1 2 3\n",
"1 3 2 4 5\n"
] | By |*x*| we denote the absolute value of number *x*. | [
{
"input": "3 2",
"output": "1 3 2"
},
{
"input": "3 1",
"output": "1 2 3"
},
{
"input": "5 2",
"output": "1 3 2 4 5"
},
{
"input": "5 4",
"output": "1 5 2 4 3"
},
{
"input": "10 4",
"output": "1 10 2 9 8 7 6 5 4 3"
},
{
"input": "10 3",
"output": ... | 514 | 614,400 | 3 | 7,509 | |
135 | Rectangle and Square | [
"brute force",
"geometry",
"math"
] | null | null | Little Petya very much likes rectangles and especially squares. Recently he has received 8 points on the plane as a gift from his mother. The points are pairwise distinct. Petya decided to split them into two sets each containing 4 points so that the points from the first set lay at the vertexes of some square and the points from the second set lay at the vertexes of a rectangle. Each point of initial 8 should belong to exactly one set. It is acceptable for a rectangle from the second set was also a square. If there are several partitions, Petya will be satisfied by any of them. Help him find such partition. Note that the rectangle and the square from the partition should have non-zero areas. The sides of the figures do not have to be parallel to the coordinate axes, though it might be the case. | You are given 8 pairs of integers, a pair per line β the coordinates of the points Petya has. The absolute value of all coordinates does not exceed 104. It is guaranteed that no two points coincide. | Print in the first output line "YES" (without the quotes), if the desired partition exists. In the second line output 4 space-separated numbers β point indexes from the input, which lie at the vertexes of the square. The points are numbered starting from 1. The numbers can be printed in any order. In the third line print the indexes of points lying at the vertexes of a rectangle in the similar format. All printed numbers should be pairwise distinct.
If the required partition does not exist, the first line should contain the word "NO" (without the quotes), after which no output is needed. | [
"0 0\n10 11\n10 0\n0 11\n1 1\n2 2\n2 1\n1 2\n",
"0 0\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n7 7\n",
"0 0\n4 4\n4 0\n0 4\n1 2\n2 3\n3 2\n2 1\n"
] | [
"YES\n5 6 7 8\n1 2 3 4\n",
"NO\n",
"YES\n1 2 3 4\n5 6 7 8\n"
] | Pay attention to the third example: the figures do not necessarily have to be parallel to the coordinate axes. | [
{
"input": "0 0\n10 11\n10 0\n0 11\n1 1\n2 2\n2 1\n1 2",
"output": "YES\n5 6 7 8\n1 2 3 4"
},
{
"input": "0 0\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n7 7",
"output": "NO"
},
{
"input": "0 0\n4 4\n4 0\n0 4\n1 2\n2 3\n3 2\n2 1",
"output": "YES\n1 2 3 4\n5 6 7 8"
},
{
"input": "-160 336\... | 186 | 204,800 | -1 | 7,523 | |
464 | No to Palindromes! | [
"greedy",
"strings"
] | null | null | Paul hates palindromes. He assumes that string *s* is tolerable if each its character is one of the first *p* letters of the English alphabet and *s* doesn't contain any palindrome contiguous substring of length 2 or more.
Paul has found a tolerable string *s* of length *n*. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | The first line contains two space-separated integers: *n* and *p* (1<=β€<=*n*<=β€<=1000; 1<=β€<=*p*<=β€<=26). The second line contains string *s*, consisting of *n* small English letters. It is guaranteed that the string is tolerable (according to the above definition). | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | [
"3 3\ncba\n",
"3 4\ncba\n",
"4 4\nabcd\n"
] | [
"NO\n",
"cbd\n",
"abda\n"
] | String *s* is lexicographically larger (or simply larger) than string *t* with the same length, if there is number *i*, such that *s*<sub class="lower-index">1</sub>β=β*t*<sub class="lower-index">1</sub>, ..., *s*<sub class="lower-index">*i*</sub>β=β*t*<sub class="lower-index">*i*</sub>, *s*<sub class="lower-index">*i*β+β1</sub>β>β*t*<sub class="lower-index">*i*β+β1</sub>.
The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.
A palindrome is a string that reads the same forward or reversed. | [
{
"input": "3 3\ncba",
"output": "NO"
},
{
"input": "3 4\ncba",
"output": "cbd"
},
{
"input": "4 4\nabcd",
"output": "abda"
},
{
"input": "2 2\nab",
"output": "ba"
},
{
"input": "2 2\nba",
"output": "NO"
},
{
"input": "1 2\na",
"output": "b"
},
... | 78 | 0 | 0 | 7,553 | |
276 | Little Girl and Maximum XOR | [
"bitmasks",
"dp",
"greedy",
"implementation",
"math"
] | null | null | A little girl loves problems on bitwise operations very much. Here's one of them.
You are given two integers *l* and *r*. Let's consider the values of for all pairs of integers *a* and *b* (*l*<=β€<=*a*<=β€<=*b*<=β€<=*r*). Your task is to find the maximum value among all considered ones.
Expression means applying bitwise excluding or operation to integers *x* and *y*. The given operation exists in all modern programming languages, for example, in languages *C*++ and *Java* it is represented as "^", in *Pascal* β as "xor". | The single line contains space-separated integers *l* and *r* (1<=β€<=*l*<=β€<=*r*<=β€<=1018).
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | In a single line print a single integer β the maximum value of for all pairs of integers *a*, *b* (*l*<=β€<=*a*<=β€<=*b*<=β€<=*r*). | [
"1 2\n",
"8 16\n",
"1 1\n"
] | [
"3\n",
"31\n",
"0\n"
] | none | [
{
"input": "1 2",
"output": "3"
},
{
"input": "8 16",
"output": "31"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "506 677",
"output": "1023"
},
{
"input": "33 910",
"output": "1023"
},
{
"input": "36 94",
"output": "127"
},
{
"input": ... | 30 | 0 | 0 | 7,558 | |
765 | Artsem and Saunders | [
"constructive algorithms",
"dsu",
"math"
] | null | null | Artsem has a friend Saunders from University of Chicago. Saunders presented him with the following problem.
Let [*n*] denote the set {1,<=...,<=*n*}. We will also write *f*:<=[*x*]<=β<=[*y*] when a function *f* is defined in integer points 1, ..., *x*, and all its values are integers from 1 to *y*.
Now then, you are given a function *f*:<=[*n*]<=β<=[*n*]. Your task is to find a positive integer *m*, and two functions *g*:<=[*n*]<=β<=[*m*], *h*:<=[*m*]<=β<=[*n*], such that *g*(*h*(*x*))<==<=*x* for all , and *h*(*g*(*x*))<==<=*f*(*x*) for all , or determine that finding these is impossible. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105).
The second line contains *n* space-separated integersΒ β values *f*(1),<=...,<=*f*(*n*) (1<=β€<=*f*(*i*)<=β€<=*n*). | If there is no answer, print one integer -1.
Otherwise, on the first line print the number *m* (1<=β€<=*m*<=β€<=106). On the second line print *n* numbers *g*(1),<=...,<=*g*(*n*). On the third line print *m* numbers *h*(1),<=...,<=*h*(*m*).
If there are several correct answers, you may output any of them. It is guaranteed that if a valid answer exists, then there is an answer satisfying the above restrictions. | [
"3\n1 2 3\n",
"3\n2 2 2\n",
"2\n2 1\n"
] | [
"3\n1 2 3\n1 2 3\n",
"1\n1 1 1\n2\n",
"-1\n"
] | none | [
{
"input": "3\n1 2 3",
"output": "3\n1 2 3\n1 2 3"
},
{
"input": "3\n2 2 2",
"output": "1\n1 1 1\n2"
},
{
"input": "2\n2 1",
"output": "-1"
},
{
"input": "1\n1",
"output": "1\n1\n1"
},
{
"input": "2\n2 1",
"output": "-1"
},
{
"input": "2\n2 2",
"ou... | 2,000 | 11,980,800 | 0 | 7,572 | |
74 | Room Leader | [
"implementation"
] | A. Room Leader | 2 | 256 | Let us remind you part of the rules of Codeforces. The given rules slightly simplified, use the problem statement as a formal document.
In the beginning of the round the contestants are divided into rooms. Each room contains exactly *n* participants. During the contest the participants are suggested to solve five problems, *A*, *B*, *C*, *D* and *E*. For each of these problem, depending on when the given problem was solved and whether it was solved at all, the participants receive some points. Besides, a contestant can perform hacks on other contestants. For each successful hack a contestant earns 100 points, for each unsuccessful hack a contestant loses 50 points. The number of points for every contestant is represented by the sum of points he has received from all his problems, including hacks.
You are suggested to determine the leader for some room; the leader is a participant who has maximum points. | The first line contains an integer *n*, which is the number of contestants in the room (1<=β€<=*n*<=β€<=50). The next *n* lines contain the participants of a given room. The *i*-th line has the format of "*handle**i* *plus**i* *minus**i* *a**i* *b**i* *c**i* *d**i* *e**i*" β it is the handle of a contestant, the number of successful hacks, the number of unsuccessful hacks and the number of points he has received from problems *A*, *B*, *C*, *D*, *E* correspondingly. The handle of each participant consists of Latin letters, digits and underscores and has the length from 1 to 20 characters. There are the following limitations imposed upon the numbers:
- 0<=β€<=*plus**i*,<=*minus**i*<=β€<=50; - 150<=β€<=*a**i*<=β€<=500 or *a**i*<==<=0, if problem *A* is not solved; - 300<=β€<=*b**i*<=β€<=1000 or *b**i*<==<=0, if problem *B* is not solved; - 450<=β€<=*c**i*<=β€<=1500 or *c**i*<==<=0, if problem *C* is not solved; - 600<=β€<=*d**i*<=β€<=2000 or *d**i*<==<=0, if problem *D* is not solved; - 750<=β€<=*e**i*<=β€<=2500 or *e**i*<==<=0, if problem *E* is not solved.
All the numbers are integer. All the participants have different handles. It is guaranteed that there is exactly one leader in the room (i.e. there are no two participants with the maximal number of points). | Print on the single line the handle of the room leader. | [
"5\nPetr 3 1 490 920 1000 1200 0\ntourist 2 0 490 950 1100 1400 0\nEgor 7 0 480 900 950 0 1000\nc00lH4x0R 0 10 150 0 0 0 0\nsome_participant 2 1 450 720 900 0 0\n"
] | [
"tourist"
] | The number of points that each participant from the example earns, are as follows:
- Petr β 3860 - tourist β 4140 - Egor β 4030 - c00lH4x0R β β-β350 - some_participant β 2220
Thus, the leader of the room is tourist. | [
{
"input": "5\nPetr 3 1 490 920 1000 1200 0\ntourist 2 0 490 950 1100 1400 0\nEgor 7 0 480 900 950 0 1000\nc00lH4x0R 0 10 150 0 0 0 0\nsome_participant 2 1 450 720 900 0 0",
"output": "tourist"
},
{
"input": "1\nA 0 0 200 0 0 0 0",
"output": "A"
},
{
"input": "2\n12345678901234567890 1 0... | 124 | 0 | 0 | 7,595 |
121 | Lucky Transformation | [
"strings"
] | null | null | 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 a number consisting of *n* digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it *d*. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation *k* times: find the minimum *x* (1<=β€<=*x*<=<<=*n*) such that *d**x*<==<=4 and *d**x*<=+<=1<==<=7, if *x* is odd, then to assign *d**x*<==<=*d**x*<=+<=1<==<=4, otherwise to assign *d**x*<==<=*d**x*<=+<=1<==<=7. Note that if no *x* was found, then the operation counts as completed and the array doesn't change at all.
You are given the initial number as an array of digits and the number *k*. Help Petya find the result of completing *k* operations. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=105,<=0<=β€<=*k*<=β€<=109) β the number of digits in the number and the number of completed operations. The second line contains *n* digits without spaces representing the array of digits *d*, starting with *d*1. It is guaranteed that the first digit of the number does not equal zero. | In the single line print the result without spaces β the number after the *k* operations are fulfilled. | [
"7 4\n4727447\n",
"4 2\n4478\n"
] | [
"4427477\n",
"4478\n"
] | In the first sample the number changes in the following sequence: 4727447βββ4427447βββ4427477βββ4427447βββ4427477.
In the second sample: 4478βββ4778βββ4478. | [
{
"input": "7 4\n4727447",
"output": "4427477"
},
{
"input": "4 2\n4478",
"output": "4478"
},
{
"input": "7 7\n4211147",
"output": "4211177"
},
{
"input": "7 6\n4747477",
"output": "4444477"
},
{
"input": "10 2\n9474444474",
"output": "9774444774"
},
{
... | 62 | 0 | 0 | 7,599 | |
45 | Director | [
"constructive algorithms",
"greedy"
] | E. Director | 2 | 256 | Vasya is a born Berland film director, he is currently working on a new blockbuster, "The Unexpected". Vasya knows from his own experience how important it is to choose the main characters' names and surnames wisely. He made up a list of *n* names and *n* surnames that he wants to use. Vasya haven't decided yet how to call characters, so he is free to match any name to any surname. Now he has to make the list of all the main characters in the following format: "*Name*1 *Surname*1, *Name*2 *Surname*2, ..., *Name**n* *Surname**n*", i.e. all the name-surname pairs should be separated by exactly one comma and exactly one space, and the name should be separated from the surname by exactly one space. First of all Vasya wants to maximize the number of the pairs, in which the name and the surname start from one letter. If there are several such variants, Vasya wants to get the lexicographically minimal one. Help him.
An answer will be verified a line in the format as is shown above, including the needed commas and spaces. It's the lexicographical minimality of such a line that needs to be ensured. The output line shouldn't end with a space or with a comma. | The first input line contains number *n* (1<=β€<=*n*<=β€<=100) β the number of names and surnames. Then follow *n* lines β the list of names. Then follow *n* lines β the list of surnames. No two from those 2*n* strings match. Every name and surname is a non-empty string consisting of no more than 10 Latin letters. It is guaranteed that the first letter is uppercase and the rest are lowercase. | The output data consist of a single line β the needed list. Note that one should follow closely the output data format! | [
"4\nAnn\nAnna\nSabrina\nJohn\nPetrov\nIvanova\nStoltz\nAbacaba\n",
"4\nAa\nAb\nAc\nBa\nAd\nAe\nBb\nBc\n"
] | [
"Ann Abacaba, Anna Ivanova, John Petrov, Sabrina Stoltz",
"Aa Ad, Ab Ae, Ac Bb, Ba Bc"
] | none | [
{
"input": "4\nAnn\nAnna\nSabrina\nJohn\nPetrov\nIvanova\nStoltz\nAbacaba",
"output": "Ann Abacaba, Anna Ivanova, John Petrov, Sabrina Stoltz"
},
{
"input": "4\nAa\nAb\nAc\nBa\nAd\nAe\nBb\nBc",
"output": "Aa Ad, Ab Ae, Ac Bb, Ba Bc"
},
{
"input": "5\nDa\nEcccdbbdc\nD\nEabbd\nFaafbfdffa\n... | 62 | 0 | 0 | 7,616 |
38 | Blinds | [
"brute force"
] | C. Blinds | 2 | 256 | The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are *n* blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)
Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)
After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.
Thus, if the blinds consist of *k* pieces each *d* in length, then they are of form of a rectangle of *k*<=Γ<=*d* bourlemeters.
Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than *l* bourlemeter. The window is of form of a rectangle with side lengths as positive integers. | The first output line contains two space-separated integers *n* and *l* (1<=β€<=*n*,<=*l*<=β€<=100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated *n* integers *a**i*. They are the lengths of initial stripes in bourlemeters (1<=β€<=*a**i*<=β€<=100). | Print the single number β the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. | [
"4 2\n1 2 3 4\n",
"5 3\n5 5 7 3 1\n",
"2 3\n1 2\n"
] | [
"8\n",
"15\n",
"0\n"
] | In the first sample test the required window is 2βΓβ4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves. | [
{
"input": "4 2\n1 2 3 4",
"output": "8"
},
{
"input": "5 3\n5 5 7 3 1",
"output": "15"
},
{
"input": "2 3\n1 2",
"output": "0"
},
{
"input": "2 2\n3 3",
"output": "6"
},
{
"input": "5 2\n2 4 1 1 3",
"output": "8"
},
{
"input": "7 4\n3 2 1 1 1 3 2",
... | 92 | 0 | 0 | 7,652 |
42 | Baldman and the military | [
"dfs and similar",
"graphs",
"trees"
] | E. Baldman and the military | 4 | 256 | Baldman is a warp master. He possesses a unique ability β creating wormholes! Given two positions in space, Baldman can make a wormhole which makes it possible to move between them in both directions. Unfortunately, such operation isn't free for Baldman: each created wormhole makes him lose plenty of hair from his head.
Because of such extraordinary abilities, Baldman has caught the military's attention. He has been charged with a special task. But first things first.
The military base consists of several underground objects, some of which are connected with bidirectional tunnels. There necessarily exists a path through the tunnel system between each pair of objects. Additionally, exactly two objects are connected with surface. For the purposes of security, a patrol inspects the tunnel system every day: he enters one of the objects which are connected with surface, walks the base passing each tunnel at least once and leaves through one of the objects connected with surface. He can enter and leave either through the same object, or through different objects. The military management noticed that the patrol visits some of the tunnels multiple times and decided to optimize the process. Now they are faced with a problem: a system of wormholes needs to be made to allow of a patrolling which passes each tunnel exactly once. At the same time a patrol is allowed to pass each wormhole any number of times.
This is where Baldman comes to operation: he is the one to plan and build the system of the wormholes. Unfortunately for him, because of strict confidentiality the military can't tell him the arrangement of tunnels. Instead, they insist that his system of portals solves the problem for any arrangement of tunnels which satisfies the given condition. Nevertheless, Baldman has some information: he knows which pairs of objects he can potentially connect and how much it would cost him (in hair). Moreover, tomorrow he will be told which objects (exactly two) are connected with surface. Of course, our hero decided not to waste any time and calculate the minimal cost of getting the job done for some pairs of objects (which he finds likely to be the ones connected with surface). Help Baldman! | First line of the input contains a single natural number *n* (2<=β€<=*n*<=β€<=100000) β the number of objects on the military base. The second line β one number *m* (1<=β€<=*m*<=β€<=200000) β the number of the wormholes Baldman can make. The following *m* lines describe the wormholes: each line contains three integer numbers *a*,<=*b*,<=*c* (1<=β€<=*a*,<=*b*<=β€<=*n*,<=1<=β€<=*c*<=β€<=100000) β the numbers of objects which can be connected and the number of hair Baldman has to spend to make this wormhole.
The next line contains one natural number *q* (1<=β€<=*q*<=β€<=100000) β the number of queries. Finally, the last *q* lines contain a description of one query each β a pair of numbers of different objects *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*). There could be more than one wormhole between a pair of objects. | Your program should output *q* lines, one for each query. The *i*-th line should contain a single integer number β the answer for *i*-th query: the minimum cost (in hair) of a system of wormholes allowing the optimal patrol for any system of tunnels (satisfying the given conditions) if *a**i* and *b**i* are the two objects connected with surface, or "-1" if such system of wormholes cannot be made. | [
"2\n1\n1 2 3\n1\n1 2\n",
"3\n1\n1 2 3\n2\n1 2\n1 3\n"
] | [
"0\n",
"-1\n3\n"
] | none | [
{
"input": "2\n1\n1 2 3\n1\n1 2",
"output": "0"
},
{
"input": "3\n1\n1 2 3\n2\n1 2\n1 3",
"output": "-1\n3"
},
{
"input": "4\n4\n1 2 1\n1 3 2\n3 4 3\n1 3 4\n6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4",
"output": "5\n4\n3\n4\n3\n3"
},
{
"input": "4\n5\n1 2 10\n2 3 3\n3 4 4\n1 4 5\n2 4 6\... | 60 | 0 | 0 | 7,660 |
429 | Points and Segments | [
"graphs"
] | null | null | 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*,<=*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*<=-<=*b**x*|<=β€<=1 must be satisfied.
A segment [*l*,<=*r*] contains a point *x* if and only if *l*<=β€<=*x*<=β€<=*r*.
Iahub gives you the starting and ending points of all the segments. You have to find any good drawing for him. | The first line of input contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of segments. The *i*-th of the next *n* lines contains two integers *l**i* and *r**i* (0<=β€<=*l**i*<=β€<=*r**i*<=β€<=109) β the borders of the *i*-th segment.
It's guaranteed that all the segments are distinct. | 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. | [
"2\n0 2\n2 3\n",
"6\n1 5\n1 3\n3 5\n2 10\n11 11\n12 12\n"
] | [
"0 1\n",
"0 1 0 1 0 0\n"
] | none | [] | 108 | 921,600 | -1 | 7,661 | |
58 | Coins | [
"greedy"
] | B. Coins | 2 | 256 | In Berland a money reform is being prepared. New coins are being introduced. After long economic calculations was decided that the most expensive coin should possess the denomination of exactly *n* Berland dollars. Also the following restriction has been introduced for comfort: the denomination of each coin should be divisible by the denomination of any cheaper coin. It is known that among all the possible variants the variant with the largest number of new coins will be chosen. Find this variant. Print in the order of decreasing of the coins' denominations. | The first and only line contains an integer *n* (1<=β€<=*n*<=β€<=106) which represents the denomination of the most expensive coin. | Print the denominations of all the coins in the order of decreasing. The number of coins must be the largest possible (with the given denomination *n* of the most expensive coin). Also, the denomination of every coin must be divisible by the denomination of any cheaper coin. Naturally, the denominations of all the coins should be different. If there are several solutins to that problem, print any of them. | [
"10\n",
"4\n",
"3\n"
] | [
"10 5 1\n",
"4 2 1\n",
"3 1\n"
] | none | [
{
"input": "10",
"output": "10 5 1"
},
{
"input": "4",
"output": "4 2 1"
},
{
"input": "3",
"output": "3 1"
},
{
"input": "2",
"output": "2 1"
},
{
"input": "5",
"output": "5 1"
},
{
"input": "6",
"output": "6 3 1"
},
{
"input": "7",
"o... | 530 | 0 | 3.8675 | 7,680 |
552 | Vanya and Brackets | [
"brute force",
"dp",
"expression parsing",
"greedy",
"implementation",
"strings"
] | null | null | Vanya is doing his maths homework. He has an expression of form , where *x*1,<=*x*2,<=...,<=*x**n* are digits from 1 to 9, and sign represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression. | The first line contains expression *s* (1<=β€<=|*s*|<=β€<=5001, |*s*| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs <=+<= and <=*<=.
The number of signs <=*<= doesn't exceed 15. | In the first line print the maximum possible value of an expression. | [
"3+5*7+8*4\n",
"2+3*5\n",
"3*4*5\n"
] | [
"303\n",
"25\n",
"60\n"
] | Note to the first sample test. 3β+β5β*β(7β+β8)β*β4β=β303.
Note to the second sample test. (2β+β3)β*β5β=β25.
Note to the third sample test. (3β*β4)β*β5β=β60 (also many other variants are valid, for instance, (3)β*β4β*β5β=β60). | [
{
"input": "3+5*7+8*4",
"output": "303"
},
{
"input": "2+3*5",
"output": "25"
},
{
"input": "3*4*5",
"output": "60"
},
{
"input": "5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5",
"output": "152587890625"
},
{
"input": "2*2+2*2",
"output": "16"
},
{
"input": "1+1+1+1... | 77 | 0 | 0 | 7,688 | |
939 | Convenient For Everybody | [
"binary search",
"two pointers"
] | null | null | In distant future on Earth day lasts for *n* hours and that's why there are *n* timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to *n* are used, i.e. there is no time "0 hours", instead of it "*n* hours" is used. When local time in the 1-st timezone is 1 hour, local time in the *i*-th timezone is *i* hours.
Some online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are *a**i* people from *i*-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than *s* hours 00 minutes local time and ends not later than *f* hours 00 minutes local time. Values *s* and *f* are equal for all time zones. If the contest starts at *f* hours 00 minutes local time, the person won't participate in it.
Help platform select such an hour, that the number of people who will participate in the contest is maximum. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=100<=000)Β β the number of hours in day.
The second line contains *n* space-separated integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=10<=000), where *a**i* is the number of people in the *i*-th timezone who want to participate in the contest.
The third line contains two space-separated integers *s* and *f* (1<=β€<=*s*<=<<=*f*<=β€<=*n*). | Output a single integerΒ β the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them. | [
"3\n1 2 3\n1 3\n",
"5\n1 2 3 4 1\n1 3\n"
] | [
"3\n",
"4\n"
] | In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate.
In second example only people from the third and the fourth timezones will participate. | [
{
"input": "3\n1 2 3\n1 3",
"output": "3"
},
{
"input": "5\n1 2 3 4 1\n1 3",
"output": "4"
},
{
"input": "2\n5072 8422\n1 2",
"output": "2"
},
{
"input": "10\n7171 2280 6982 9126 9490 2598 569 6744 5754 1855\n7 9",
"output": "4"
},
{
"input": "10\n5827 8450 8288 5... | 46 | 0 | 0 | 7,693 | |
0 | none | [
"none"
] | null | null | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya loves long lucky numbers very much. He is interested in the minimum lucky number *d* that meets some condition. Let *cnt*(*x*) be the number of occurrences of number *x* in number *d* as a substring. For example, if *d*<==<=747747, then *cnt*(4)<==<=2, *cnt*(7)<==<=4, *cnt*(47)<==<=2, *cnt*(74)<==<=2. Petya wants the following condition to fulfil simultaneously: *cnt*(4)<==<=*a*1, *cnt*(7)<==<=*a*2, *cnt*(47)<==<=*a*3, *cnt*(74)<==<=*a*4. Petya is not interested in the occurrences of other numbers. Help him cope with this task. | The single line contains four integers *a*1, *a*2, *a*3 and *a*4 (1<=β€<=*a*1,<=*a*2,<=*a*3,<=*a*4<=β€<=106). | On the single line print without leading zeroes the answer to the problem β the minimum lucky number *d* such, that *cnt*(4)<==<=*a*1, *cnt*(7)<==<=*a*2, *cnt*(47)<==<=*a*3, *cnt*(74)<==<=*a*4. If such number does not exist, print the single number "-1" (without the quotes). | [
"2 2 1 1\n",
"4 7 3 1\n"
] | [
"4774\n",
"-1\n"
] | none | [
{
"input": "2 2 1 1",
"output": "4774"
},
{
"input": "4 7 3 1",
"output": "-1"
},
{
"input": "4 7 4 7",
"output": "-1"
},
{
"input": "1 1 1 1",
"output": "-1"
},
{
"input": "2 2 1 2",
"output": "7474"
},
{
"input": "2 1 2 1",
"output": "-1"
},
... | 154 | 7,987,200 | 3 | 7,701 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.