NATIONAL COMPETITION PREPARATION · ELEMENTARY SCHOOL

Preparation for the National Competition in Informatics

This page is a guide for solving tasks that appear at the national (state) competitions in informatics for elementary schools in Serbia. Along with the tasks, there are simple preparatory examples that are building blocks of larger tasks. Every task has detailed hints and a C++ solution (and where a more efficient solution exists – several of them), which you can reveal after you try to solve the task yourself. There is a quiz at the end of the page.

If you have not prepared for the district competition yet, see the page Preparation for district competitions.

National competition 2021

You can also download the tasks with solutions and test cases from the website of the Society of Mathematicians of Serbia (in Serbian): dms.rs/informatika-osnovne-skole

TASK 01 2021

Rainforest

National competition 5th and 6th grade

Task source: dms.rs/informatika-osnovne-skole

Task statement

A miraculous table with 3 rows and 3 columns filled with digits was found in the rainforest. Next to the table, the text of a riddle was found. The riddle says: “Form the first three-digit number from the digits on the main diagonal of the table, starting from the top and going towards the bottom. Then form another three-digit number from the digits on the anti-diagonal, also from top to bottom. The solution of the riddle is the product of these two three-digit numbers.” Write a program that reads the digits written in the table and prints the product of the two three-digit numbers formed in the described way.

Clarification: The main diagonal consists of the first digit of the first row, the middle digit of the second row and the last digit of the third row. The anti-diagonal consists of the last digit of the first row, the middle digit of the second row and the first digit of the third row.

Input

Each of the three lines of standard input contains three digits written without spaces.

Output

The program must print one integer to standard output – the product of the two three-digit numbers formed as described above.

Example

Input

123
456
789

Output

56763
Explanation

The number formed from the main diagonal is 159, and the number formed from the anti-diagonal is 357. Their product is 56763.

Task analysis

One way to solve the task is to use matrices and nested loops. Since these topics often appear at the national competition, it is recommended to know them well. Before solving this task, try the two preparatory tasks below first.

Preparatory task 1: matrix diagonals

Read 9 numbers into a 3×3 matrix. Print the values on the main diagonal and on the anti-diagonal.

Example

Input

1 2 3
4 5 6
7 8 9

Output

Main diagonal: 1 5 9
Anti-diagonal: 3 5 7

Hints and solution

Preparatory task 2: build a number from known digits

Enter 4 integers that are the digits of a four-digit number. Build a new number whose digits are in reverse order.

Example

Input

4
5
6
7

Output

7654

Hints and solution

Hints and solution

TASK 02 2021

Sum of squares of odd digits

National competition 5th and 6th grade

Task source: dms.rs/informatika-osnovne-skole

Task statement

Lenka wants to calculate the sums of squares of the odd digits of several numbers. Help Lenka by writing a program that reads one natural number less than one billion from standard input and prints the sum of the squares of its odd digits to standard output (if there are no such digits, the sum is zero). The square of a digit equals the product of that digit with itself. For example, the square of the digit 3 is 9, because 3 · 3 = 9.

Input

One natural number less than one billion is read from standard input.

Output

Print the sum of the squares of the odd digits of that number to standard output.

Example 1

Input

1234560

Output

35
Explanation

The odd digits are 1, 3 and 5: 1 + 9 + 25 = 35.

Example 2

Input

11

Output

2

Example 3

Input

888

Output

0
Explanation

There are no odd digits.

Task analysis

To extract the digits of a number, we use the operators % and / (remainder of division by 10 and integer division by 10). You also need to know how to add numbers in a loop.

Hints and solution

TASK 03 2021

Magic sequence

National competition 5th and 6th grade

Task source: dms.rs/informatika-osnovne-skole

Task statement

Laza decided to create a magic sequence of numbers in the following way: he started the sequence from a given number, and each next term is obtained by adding the number of all its divisors to the previous number. For example, if a term of the sequence equals 6, the next term is 6 + 4, i.e. 10, because the number 6 has exactly 4 divisors (1, 2, 3 and 6).

Write a program that reads from standard input the first term of the sequence (0 < a1 ≤ 10000) in the first line, and a natural number N (0 < N ≤ 100) in the second line, and prints the N-th term of Laza's sequence to standard output.

Input

The first line contains the first term of the sequence a1 (0 < a1 ≤ 10000), and the second line contains a natural number N (0 < N ≤ 100).

Output

Print the N-th term of Laza's sequence.

Example 1

Input

2
4

Output

9
Explanation

a1 = 2, a2 = 4, a3 = 7, a4 = 9.

Example 2

Input

3882
93

Output

4878

Task analysis

The task is solved by moving the computation of the number of divisors of a given number into a separate function. You need to know loops and, optionally, functions.

Hints and solution

TASK 04 2021

Position of the K-th occurrence of a number

National competition 5th and 6th grade

Task source: dms.rs/informatika-osnovne-skole

Task statement

Let us look at the following rows:

1
121
12321
1234321
123454321
...

The row with number M (M = 1, 2, 3, ...) is formed by writing the natural numbers from 1 to M, and then writing the numbers in reverse order from M − 1 down to 1. Now we form an infinite sequence by taking the first of the rows above, appending the second row, then the third row, and so on. The beginning of the resulting infinite sequence looks like this:

1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 ...

The terms of this sequence are numbered starting from one. Write a program that finds at which position the given number n appears for the k-th time.

Input

The only line of standard input contains two integers n and k, separated by a single space (0 < n ≤ 1 000 000, 0 < k ≤ 1 000 000).

Output

The program must print one integer to standard output – the position of the k-th occurrence of the number n in the sequence.

Example 1

Input

1 1

Output

1

Example 2

Input

2 2

Output

6
Explanation

The sequence is 1 1 2 1 1 2 3 2 1 .... The number 2 appears for the first time at position 3, and for the second time at position 6.

Example 3

Input

3 3

Output

14

Task analysis

Let us find the rule. The row with number M has 2M − 1 terms, so after the first M rows the sequence has M² terms in total (1 + 3 + 5 + ... + (2M − 1) = M²).

Row MRow contentRow lengthPosition of the last term in the sequence
1111
21 2 134
31 2 3 2 159
41 2 3 4 3 2 1716
51 2 3 4 5 4 3 2 1925
61 2 3 4 5 6 5 4 3 2 11136

The number n does not appear in rows smaller than n. In row n it appears exactly once (in the middle), and in every following row M > n it appears exactly twice: at the n-th place of the row and at place 2M − n.

Hints and solutions

TASK 05 2021

Odd number of divisors

National competition 7th and 8th grade

Task source: dms.rs/informatika-osnovne-skole

Task statement

Write a program that finds how many integers between two given values a and b (including a and b) have an odd number of divisors.

Input

The only line of standard input contains two integers a and b, separated by a single space (0 < a ≤ b < 1015).

Output

The program must print one integer to standard output – the number of integers between a and b (including a and b) that have an odd number of divisors.

Example

Input

3 12

Output

2
Explanation

The divisors of the numbers from 3 to 12 are: 3: 1, 3; 4: 1, 2, 4; 5: 1, 5; 6: 1, 2, 3, 6; 7: 1, 7; 8: 1, 2, 4, 8; 9: 1, 3, 9; 10: 1, 2, 5, 10; 11: 1, 11; 12: 1, 2, 3, 4, 6, 12. We see that two numbers in total (4 and 9) have an odd number of divisors.

Hints and solutions

Additional analysis (in Serbian)
TASK 06 2021

The Mallow-Coloured Garden

National competition 7th and 8th grade

Task source: dms.rs/informatika-osnovne-skole

Task statement

Do you remember “The Mallow-Coloured Garden” (Bašta sljezove boje), the wonderful book by Branko Ćopić, and the adventures of little Baja, the saddler Petrak and Baja's grandfather? Little Baja has several cards, and on each card an integer between 1 and 10 is written. Baja and old Petrak set off on a trip to the Moon. In the meantime grandfather Rade, who does not see very well, arranged the cards in a row.

When Baja returned from the unsuccessful trip to the Moon, he started dealing the cards. In each deal he wanted to arrange the received cards from the smallest to the largest (if some cards repeat, they are placed next to each other). Help Baja by writing a program that calculates how many times Baja made a mistake and did not arrange the cards as he wanted.

Input

The first line of standard input contains an integer N (1 < N < 100) – the number of deals Baja played, then the number k (2 < k < 10) – the number of cards in each deal, and then for each deal k cards listed in the order in which Baja arranged them. Each number is given in a separate line.

Output

In a single line of standard output the program should print the number of deals in which Baja did not arrange the cards correctly.

Example

Input

4
3
1
2
3
4
3
4
1
5
5
8
8
8

Output

1
Explanation

Data for 4 deals (3 cards each) was read. The first, third and fourth deals are arranged correctly. The second deal, with cards 4, 3, 4, is not.

Hints and solutions

Additional analysis (in Serbian)
TASK 07 2021

Dino the Dinosaur

National competition 7th and 8th grade

Task source: dms.rs/informatika-osnovne-skole

Prerequisites: Loops in C/C++ Binary search

Task statement

Dino the Dinosaur is enjoying a tournament organized by a well-known internet browser. You remember the dinosaur game, i.e. the game of collecting cacti. Every participant may collect consecutive cacti arranged along a straight line. The state committee has to determine the threshold for a contestant to advance from the first level of the game to the next one, looking only at the number of collected cacti. A large number of contestants take part in this game.

Dino keeps the score table, and contestants keep asking him how many contestants would advance if the lower threshold were this many points (all participants whose number of points is greater than or equal to the threshold advance). Dino decided to write a program that answers these questions.

Input

The number of contestants n (1 ≤ n ≤ 50000) is read from standard input, followed by the contestants' points (natural numbers), given in sorted order from the largest to the smallest and separated by spaces. Then the number m (1 ≤ m ≤ 50000) is read, the number of questions Dino has to answer, followed by m numbers separated by spaces; for each of them we need to answer how many contestants would advance if that number were taken as the lower threshold.

Output

Print the required numbers of advancing contestants to standard output, each threshold in a separate line.

Example

Input

5
79 63 63 46 13
4
85 40 60 5

Output

0
4
3
5
Explanation

If the threshold is 85 points, nobody advances. If the threshold is 40 points, the contestants with 79, 63, 63 and 46 points advance. If the threshold is 60 points, the contestants with 79, 63 and 63 points advance. If the threshold is 5 points, all contestants advance.

Hints and solutions

National competition 2019

TASK 08 2019

Tall students

National competition 6th grade

Task source: dms.rs/informatika-osnovne-skole

Task statement

The heights of all students of a school are known. Write a program that finds the tallest boy and the tallest girl in the school, determines which of them is taller and prints the difference of their heights.

Input

The number of students (5 ≤ n ≤ 100) is entered from standard input, and then each of the next n lines contains data for one student. For each student, the height (an integer between 120 and 200) and the gender mark (m for male and z for female – from the Serbian word ženski) are entered, separated by a single space. Assume that there is at least one boy and at least one girl.

Output

If the tallest girl is taller than the tallest boy, print z, a space and then how much taller she is. If the tallest boy is taller than the tallest girl, print m, a space and then how much taller he is. If they are of the same height, print only =.

Example

Input

5
152 z
174 m
165 z
172 m
170 z

Output

m 4
Explanation

The tallest boy is 174 tall and the tallest girl is 170, so the boy is taller by 4.

Hints and solution

TASK 09 2019

Book

National competition 6th grade

Task source: dms.rs/informatika-osnovne-skole

Prerequisites: Loops in C/C++

Task statement

The array a contains the lengths of the n chapters of a book. Element a0 is the number of pages of the first chapter, element a1 of the second, and so on. The book has to be split into two parts so that the total numbers of pages of the two parts differ as little as possible. The split is made after one of the chapters. If there are two possible splits with the same smallest difference, the split is made after the earlier of the two chapters (so that the first part is shorter). Write a program that determines after which chapter the split should be made.

Input

The first line of standard input contains a natural number n (1 < n ≤ 50000). Each of the next n lines contains one natural number (between 1 and 1000) – the number of pages of each chapter.

Output

Print in one line of standard output the number of the chapter (chapters are numbered from zero) after which the book should be split.

Example

Input

5
100
120
50
150
70

Output

1
Explanation

When the split is made after chapter number 1 (the second chapter), the first part of the book contains 220 pages and the second 270 pages, which gives the smallest possible difference of 50 pages. A split after chapter number 2 (the third chapter) would also give a difference of 50 (270 and 220), but according to the task the first part has to be the shorter one, so the answer is 1.

Task analysis

Let left[i] be the number of pages in chapters 0 to i, and right[i] the number of pages in all chapters after chapter i. For the example from the task we get:

i01234
chapter a[i]1001205015070
left[i]100220270420490
right[i]390270220700
right[i] − left[i]29050−50−350−490

We are looking for the split with the smallest absolute value in the last row. The values decrease until they cross from positive to negative, and after that they grow in absolute value, so the minimum is always around that crossing. When two values are equal in absolute value (columns 1 and 2), the earlier one is taken.

Hints and solution

TASK 10 2019

Increasing digits

National competition 6th grade

Task source: dms.rs/informatika-osnovne-skole

Task statement

Write a program that, among the entered numbers, finds those whose digits are strictly increasing, looking from the most significant digit (the first digit from the left).

Input

The number N (1 ≤ N ≤ 5000) is read from standard input, followed by N natural numbers greater than or equal to 10 and less than or equal to 109, each in a separate line.

Output

Print the numbers with increasing digits to standard output, each in a separate line.

Example

Input

3
123
222
321

Output

123

Hints and solutions

TASK 11 2019

Height difference

National competition 6th grade

Task source: dms.rs/informatika-osnovne-skole

Prerequisites: Loops in C/C++ Binary search

Task statement

A school is choosing actors for a school play in which the characters have a big difference in height. Write a program that determines in how many ways we can choose two actors from a class so that their height difference equals the given number r.

Input

First a positive natural number r (1 ≤ r ≤ 1000) is entered from standard input, in the next line the number of students n (1 ≤ n ≤ 50000), and after that, in the next n lines, the height of each student (a number between 1 and 100000).

Note. In 20 of the 25 test cases all children have different heights.

Output

Print the number of pairs that can be formed to standard output.

Example 1

Input

10
5
150
160
165
170
175

Output

3
Explanation

Pairs can be formed from the first and second child (150, 160), the second and fourth child (160, 170) and the third and fifth child (165, 175).

Example 2

Input

23
5
157
180
157
162
134

Output

4
Explanation

Pairs can be formed from the first and second child (157, 180), the fifth and first (134, 157), the second and third (157, 180) and the fifth and third child (134, 157).

Task analysis

If the heights are read into an array, it is a good idea to sort it first. For every pair of heights we need to check whether their difference equals r. This can be done with two nested loops or more efficiently: for a fixed first height, the second one is found with binary search, or the heights are counted in advance.

Hints and solutions

National competition 2018

TASK 12 2018

Shopping

National competition grade not specified

Task source: dms.rs/informatika-osnovne-skole

Prerequisites: Loops in C/C++

Task statement

The prices of all items sold in a shop are known. A customer has a certain amount of money and wants to buy items that are as expensive as possible. He takes the items one by one starting from the most expensive, while he has money. If he does not have enough money for the most expensive one, he takes the most expensive one he can afford.

Note. This strategy does not guarantee that the items the customer buys will have the greatest possible total value (for example, if he has 5 dinars and the items cost 4, 3 and 2 dinars, he will buy only the item for 4 dinars, although he could buy the items for 3 and 2 dinars).

Write a program that determines how much money the customer has left after shopping in the described way.

Input

The first line of standard input contains the amount of money (an integer) the customer has, the second line the number of items N (a natural number less than 50000), and the next line contains the prices of the items, separated by spaces.

Output

Print the remaining amount of money after shopping in the described way to standard output.

Example 1

Input

1250
5
1010 357 725 1125 115

Output

10
Explanation

The customer buys the item for 1125 (125 is left), then he cannot buy 1010, 725 and 357, and he buys the item for 115 (10 is left).

Example 2

Input

10000
6
3010 3005 5725 1265 2075 385

Output

0

Task analysis

The algorithm of the task is shown in the figure. The array of prices is sorted in decreasing order. At the start, the remainder equals the money the customer has. Then the array is traversed in order: if the price of an item is less than or equal to the current remainder, the item is “bought”, i.e. the price is subtracted from the remainder. Otherwise the item is skipped.

Preparation for the national competition 2018: task “Shopping” – algorithm
Figure 1: Preparation for the national competition 2018: task “Shopping” – algorithm (labels in Serbian)

Hints and solution

TASK 13 2018

Binary sequences

National competition grade not specified

Task source: dms.rs/informatika-osnovne-skole

Prerequisites: Recursive algorithms

Task statement

Write a program that prints all binary sequences (sequences containing only zeros and ones) of length n that do not contain two consecutive ones. Print the sequences in lexicographic order (so that the binary numbers formed from these sequences are in increasing order).

Input

The first line of standard input contains a natural number n (n ≤ 10).

Output

Print the required binary sequences to standard output in lexicographic order, each sequence in a separate line.

Example

Input

3

Output

000
001
010
100
101

Task analysis

The task can be solved with a recursive function that forms the binary sequences. The function has one parameter – the index i of the position we are currently filling. If the initial value is i = 0, the next call has i = 1, and so on.

In each call we try to write a zero and to write a one at position i. A zero can always be written. A one may be written only if the previous element is not a one (according to the task there must not be two consecutive ones). In both cases we call the function for the next position i + 1. When i = n is reached, the sequence is complete and it is printed. Since a zero is always written first and only then a one, the sequences are produced in lexicographic order.

Preparation for the national competition 2018: task “Binary sequences” – algorithm
Figure 2: Preparation for the national competition 2018: task “Binary sequences” – tree of recursive calls

Hints and solution

TASK 14 2018

Stocks

National competition grade not specified

Task source: dms.rs/informatika-osnovne-skole

Prerequisites: Loops in C/C++

Task statement

The value of a company's stocks changed from day to day over a long period of time. Write a program that determines how many sequences of consecutive days there are in which the total sum of changes of the stock value equals the given number z.

Input

The first line of standard input contains the number z (an integer between −10000 and 10000), the next line the number of days n during which the changes were tracked (3 ≤ n ≤ 50000), and the next line the changes for each day compared to the previous day (integers between −100 and 100), separated by spaces.

Output

Print the number of periods in which the total change equals z (i.e. the number of non-empty segments of the array of changes whose sum equals z) to standard output.

Example

Input

11
10
1 2 3 5 1 -1 1 5 3 2

Output

7
Explanation

The following segments have the sum 11: (1 2 3 5), (1 2 3 5 1 −1), (2 3 5 1), (2 3 5 1 −1 1), (5 1 −1 1 5), (1 −1 1 5 3 2), (1 5 3 2).

Task analysis

The simplest approach is to check every segment (two loops), but that is O(n²), which for n = 50000 means about 1.25 billion additions. It is solved more efficiently with prefix sums.

Hints and solutions

National competition 2016

TASK 15 2016

Divisible

National competition 7th and 8th grade (2nd category)

Task source: dms.rs/informatika-osnovne-skole

Prerequisites: Operators in C++ Loops in C/C++

Task statement

In every task the time limit is 1 second and the memory limit is 64 MB.

Two integers a, b (0 < a < b < 1015) and an integer c (1 < c < 1015) are given. Write a program (console application) DELJIVI that prints how many integers between a and b (including a and b) are divisible by the given number c.

Input

The first line of standard input contains a natural number a, the second line a natural number b, and the third line a natural number c.

Output

Print the required number in the only line of standard output.

Example 1

Input

8
40
5

Output

7
Explanation

The numbers divisible by 5 are 10, 15, 20, 25, 30, 35 and 40.

Example 2

Input

8
40
8

Output

5

Example 3

Input

8
1234567890
8

Output

154320986

Task analysis

The simplest solution is to check, in a loop, the divisibility of every number from a to b by c. That is not efficient enough (because of the time limit it does not pass all test cases). It is somewhat better to continue in steps of c once the first number divisible by c is found. The most efficient way is to compute the count with a formula.

Hints and solutions

National competition 2024

The tasks are taken from Petlja, from the website takprog.petlja.org. To check your solutions, log in to Petlja and click the link for the corresponding grade.

TASK 16 2024

Heavy bags

National competition grade not specified

Task source: takprog.petlja.org – elementary school tasks

Prerequisites: Recursive algorithms

Task statement

Two little travellers have four heavy bags in total. Each traveller can carry at most two bags, one in each hand. All bags are shared and any traveller can carry any bag. If the bags are too heavy, our travellers want to use as little help from others as possible. The weight of each bag is known, as well as the greatest load each traveller can carry. Determine the smallest total weight of the bags they cannot carry.

Input

The first line of standard input contains four natural numbers separated by single spaces – the weights of the bags. The second line contains two natural numbers separated by a single space – the weights the little travellers can carry. All numbers are less than 1000.

Output

Print one non-negative integer to standard output – the smallest total weight of the bags for which the travellers need help.

Example 1

Input

1 2 3 4
5 6

Output

0
Explanation

One traveller can carry the bags weighing 1 and 4, and the other the bags weighing 2 and 3, so they need no help.

Example 2

Input

2 3 4 5
5 6

Output

3
Explanation

One traveller can carry the bag weighing 5, and the other the bags weighing 2 and 4, so they need help with the bag weighing 3.

Example 3

Input

12 20 15 17
17 14

Output

35

Example 4

Input

99 56 34 22
3 4

Output

211
Explanation

No bag is light enough for the travellers, so they need help with all bags: 99 + 56 + 34 + 22 = 211.

Example 5

Input

7 5 4 2
7 9

Output

2
Explanation

The first traveller carries the bag weighing 7, and the second the bags weighing 5 and 4 (9 in total). The bag weighing 2 is left.

Hints and solutions

Additional analysis (in Serbian)
TASK 17 2024

Quantum puzzle

National competition 7th grade

Task source: takprog.petlja.org – elementary school tasks

Task statement

Đole is a quantum physicist who designed an experiment as part of his doctoral studies. Đole is trying to bring a certain particle into a stable position at room temperature. To check whether he succeeded, he measures the speed of the particle over time. For the particle to be considered stable, its speed must oscillate during some k consecutive moments, i.e. during those k moments the speed must alternately increase and decrease. Đole is a brilliant physicist, but not such a good programmer, so he asked you to determine, based on the sequence of measurements, whether the experiment succeeded.

Input

The first line of standard input contains two natural numbers n and k (2 ≤ k ≤ n ≤ 106). The second line contains n positive real numbers separated by spaces. These numbers are the speeds of the particle in each of the n moments of the experiment.

Output

In the first line of standard output print yes or no, depending on whether the experiment succeeded. In the second line print the length of the longest run of moments in which the speed of the particle oscillated.

Note: the original task (in Serbian) uses the words da / ne; here they are translated.

Example 1

Input

12 7
2.7 3.7 6.9 7.8 4.3 17.11 2.6 5.8 5.9 4.1 7.9 6.2

Output

no
6
Explanation

There is no run of 7 consecutive elements in which the speed oscillates. The longest such run has 6 elements: 6.9, 7.8, 4.3, 17.11, 2.6, 5.8.

Example 2

Input

10 4
6.8 5.3 4.1 9.17 3.7 2.1 1.0 9.9 17.64 2.1

Output

yes
4
Explanation

The run of consecutive moments 5.3, 4.1, 9.17, 3.7 satisfies the condition.

Example 3

Input

7 4
2.2 7.5 3.5 3.5 5.7 9.9 10.0

Output

no
3
Explanation

There is no run of 4 consecutive elements in which the speed oscillates. The run 2.2, 7.5, 3.5, 3.5 does not satisfy the condition because the last two elements are equal.

Hints and solution

TASK 18 2024

Sneaker shop

National competition 7th grade

Task source: takprog.petlja.org – elementary school tasks

Prerequisites: Vectors Maps in C++ Binary search

Task statement

A popular sneaker shop, Patikarnica, decided to offer a new service to its customers. A touch screen will be placed at the entrance, suggesting to customers the most expensive sneaker models that fit the budget they planned for new sneakers. Patikarnica bought all the equipment needed for the new service, but asked us to write a program that, based on the sneakers available in the shop and the money a customer is willing to spend, shows the list of the most expensive models the customer can afford.

Patikarnica has n different sneaker models in stock, each described by its name and price. During the day, k customers enter the shop. At the entrance, each customer enters the amount of money x they want to spend on new sneakers, and the system shows them the list of the most expensive sneakers they can afford.

Input

The first line contains the number n (1 ≤ n ≤ 105) of different sneaker models offered by Patikarnica, then each of the next n lines contains the name of a model (a string of at most 200 characters) and its price (a natural number less than 50000). After that the number k (1 ≤ k ≤ 106) of customers who entered the shop is read, and then, in the next k lines, the budgets xi (1 ≤ i ≤ k) of individual customers (natural numbers less than 50000).

Output

For each of the k customers and their budgets xi, print to standard output a lexicographically sorted list of the most expensive sneakers they can afford (name and price, in the same line). If there are no sneakers the customer can afford, print the string none.

Note: the original task (in Serbian) prints the word nema; here it is translated.

Example

Input

10
NikeAirMaxIVO 16799
AsicsMetaspeedSky+ 29999
NikeVaporFly3 32899
AsicsSkzElite 17199
NikeAirMax90 20699
NikeJordanMaxAura5 17199
NikeStreakFly 13999
Adidas4DFWD3 25999
AdidasUltraBoostLight 16799
AdidasXPLRBoost 17199
2
18000
12000

Output

AdidasXPLRBoost 17199
AsicsSkzElite 17199
NikeJordanMaxAura5 17199
none
Explanation

The first customer entered a budget of 18000 dinars, and the most expensive sneakers they can afford are AdidasXPLRBoost, AsicsSkzElite and NikeJordanMaxAura5 at the price of 17199 dinars. The second customer entered a budget of 12000 dinars and there are no sneakers they can afford, so the word none is printed.

Hints and solution

TASK 19 2024

Letter rotator

National competition 7th grade

Task source: takprog.petlja.org – elementary school tasks

Prerequisites: Functions in C/C++

Task statement

When a key on the keyboard is pressed, the first letter of every word in the text is moved to the last place of that word. How many times does the key have to be pressed for the sentence to return to its initial state?

Input

One line of text is read from standard input; it consists of lowercase letters of the English alphabet and spaces and has at most 106 characters. All test cases are such that the sentence returns to its initial state after at most 1018 key presses.

In 50% of the test cases the words are not periodic. In 60% of the test cases all words are shorter than 100 characters.

Output

Print to standard output the smallest number of key presses (greater than 0) needed for the sentence to return to its initial state.

Example 1

Input

mala sirena zivi na dnu mora

Output

12
Explanation

Pressing the key gives the following sentences: mala sirena zivi na dnu mora, alam irenas iviz an nud oram, lama renasi vizi na udn ramo, amal enasir iziv an dnu amor, mala nasire zivi na nud mora, ..., and after 12 presses the initial sentence is obtained. (The sentence is in Serbian: “the little mermaid lives at the bottom of the sea”.)

Example 2

Input

sestaci sedmaci i osmaci vole informatiku

Output

924

Example 3

Input

dada tata i mama

Output

2

Hints and solutions

Additional analysis (in Serbian)

National competition 2023

The tasks are taken from Petlja, from the website takprog.petlja.org. To check your solutions, log in to Petlja and click the link for the corresponding grade.

TASK 20 2023

Even-odd sequences

National competition 7th grade

Task source: takprog.petlja.org – elementary school tasks

Task statement

A sequence is even-odd (even-odd-even-odd...) if the even positions of the sequence (0, 2, 4, ...) contain only even numbers, and the odd positions (1, 3, 5, ...) contain only odd numbers. Write a program that checks for several sequences whether they are even-odd sequences.

Input

The numbers n (5 ≤ n ≤ 100) and k (5 ≤ k ≤ 100) are read from standard input, and then, from the next n lines, n sequences of k elements each, with values between 0 and 1000.

Output

For each sequence, print yes to standard output if it satisfies the condition, and no otherwise.

Note: the original task (in Serbian) uses the words da / ne; here they are translated.

Example

Input

3 5
1 2 3 4 5
2 3 4 5 6
4 7 0 9 2

Output

no
yes
yes
Explanation

In the first sequence, position 0, which is even, contains the number 1, which is odd. In the second sequence, the even positions contain 2, 4 and 6, which are even, and the odd positions contain 3 and 5, which are odd. In the third sequence, the even positions contain 4, 0 and 2, which are even, and the odd positions contain 7 and 9, which are odd.

Hints and solutions

Additional analysis (in Serbian)
TASK 21 2023

Translation

National competition 7th grade

Task source: takprog.petlja.org – elementary school tasks

Prerequisites: Maps in C++

Task statement

Write a program that implements a Serbian–English dictionary. First a list of words and their translations is read, and then the user asks the system to translate some words (either from Serbian to English or from English to Serbian).

Input

The number n (1 ≤ n ≤ 50000) of word pairs in English and Serbian is entered from standard input (in each line first the English word and then the Serbian word, as in the example). All Serbian words are different from each other and all English words are different from each other. After that m (1 ≤ m ≤ 50000) queries are entered. Each query contains the target language (sr or en) and then a word in the other language that has to be translated.

Output

Print all translations to standard output. If no translation was entered for a word, print ?.

Example

Input

3
cat macka
dog pas
mouse mis
4
en pas
sr mouse
sr lion
en mis

Output

dog
mis
?
mouse
Explanation

The query en pas asks for the English translation of the Serbian word pas: dog. The query sr mouse asks for the Serbian translation of the English word mouse: mis. There is no translation for the word lion.

Hints and solution

Quiz: test your knowledge

Quiz – preparation for the national competition

1. Which elements of the matrix a[3][3] form the anti-diagonal?

2. In the task “Rainforest”, what is the easiest way to read digits written without spaces (e.g. 123)?

3. Which numbers have an odd number of divisors?

4. Why do we use long long in the task “Position of the K-th occurrence of a number”?

5. What is necessary for binary search to work correctly?

6. How many integers divisible by c are there in the interval [a, b]?

7. In the task “Stocks” a segment has the sum z. What do we look for in the prefix sums P?

8. In the task “Letter rotator”, how is the final answer for the whole sentence obtained?

9. In the task “Binary sequences” a one may be written at position i only if…

10. Why does the strategy from the task “Shopping” (always take the most expensive item you can) not always give the best result?

11. In the task “Dino the Dinosaur”, how can the questions be answered faster when the points are sorted?

12. In the task “Translation”, what is a good choice for storing word → translation pairs?