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'... | 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... | [
"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ββ€... | [] | 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... | 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 foll... | 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 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... | 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 fir... | 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... | [
"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 co... | 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... | 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 '<' o... | [
"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,<=... | 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... | 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], i... | [
{
"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 tw... | 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* t... | 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 ... | 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 roo... | 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 se... | 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 i... | [
{
"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 cons... | 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* ... | 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://e... | [
{
"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).... | 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 o... | 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 va... | 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 t... | 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 s... | 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... | [
{
"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 b... | 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... | 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 bi... | 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 b... | 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 left... | 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 t... | 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 ... | [] | 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 *... | 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* bour... | 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 h... | 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 ... | 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*<... | 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="... | [
{
"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 visi... | 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 apartment... | [
{
"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 ... | 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... | 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 domino... | 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 ... | 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 ... | 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... | 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 thir... | [
"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)$ re... | [
{
"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 o... | 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*,<=*... | 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-... | [
{
"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 polynomi... | 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... | 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="upp... | [
{
"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 *... | 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 ... | [
"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... | 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... | 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 t... | 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 β ... | 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 b... | 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* ... | 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 ... | 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
Orig... | [
{
"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 ... | 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*... | 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. I... | [
{
"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 ... | 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, ... | 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 ... | [] | 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 ... | 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 coordinat... | 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 botto... | 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 p... | 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 de... | 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 a... | [
"..-**-..\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... | 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 ... | 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 su... | 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... | 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 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% prob... | [
{
"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*... | 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 ... | [
"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.wikiped... | [
{
"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 cer... | 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... | 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 ... | [
"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 choo... | 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. Wal... | 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 ... | 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="lowe... | [
{
"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 e... | 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* (... | 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 y... | [
{
"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 collec... | 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* a... | 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 s... | [
{
"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, ... | 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. Firs... | 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 t... | 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 ... | 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 repres... | 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 ... | 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**... | 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 strin... | 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 d... | 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 pi... | 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 comfo... | 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 ar... | 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 permutatio... | 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 ele... | 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 ... | 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 cub... | 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.
... | 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... | [
{
"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... | 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... | 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... | 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 ... | 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 at... | 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 simultaneous... | 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... | 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" ... | [
"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... | 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 tha... | 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 ... | 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<=... | 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 ... | 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*. ... | 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 lead... | 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... | 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... | [
{
"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... | 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... | 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 ... | [
{
"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 th... | 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 no... | 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 ... | [
"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 vec... | 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,... | [
"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: ... | [
{
"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 ... | 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 reveale... | 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 hidde... | [
{
"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 ... | 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 t... | [
"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 t... | 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 ... | [
{
"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 ele... | 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 ... | [
{
"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 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... | 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... | 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 surround... | 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 gua... | 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... | 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 number... | 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 ... | 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 pri... | [
"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 to... | 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*... | [
{
"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 bitw... | 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 ... | 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 guarant... | [
"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 prob... | 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 o... | 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... | 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 ... | 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 ... | 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 ... | 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 ... | 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... | 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 ... | [
{
"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... | 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 numbe... | 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 obj... | [
"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*... | 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 d... | 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 coin... | [
"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 ... | 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 li... | 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 partici... | [
{
"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 me... | 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.