A COLLECTION OF TASKS · PRIMARY SCHOOL

District computer science competitions – tasks

This page brings together tasks from various district competitions for primary schools. Each task has a separate statement, input, output and examples, together with detailed instructions and a C++ solution that you can show once you have tried to solve it yourself. There is a quiz at the end of the page.

Before solving these tasks you can look at the preparation for the district competitions. You can also first do the tasks from the qualifications for the district competitions.

You can test tasks 13–30 on the site takprog.petlja.org (you need to log in). The tests there check the output exactly as in the original tasks, so for these tasks the output words are left in Serbian, with their meaning explained.

District computer science competition – 2016

The tasks are taken from the site of the Mathematical Society of Serbia, computer science competitions: dms.rs/informatika-osnovne-skole

TASK 01 2016.

Product

District competition grade not stated

Source of the task: dms.rs/informatika-osnovne-skole

Task statement

Write a program PRODUCT that finds the largest product that can be obtained if a multiplication sign is put between two digits of a positive number N.

Input

Standard input contains the number N (100 ≤ N ≤ 999).

Output

On the only line of standard output print the largest product.

Example 1

Input

795

Output

665
Explanation

The splits of the number 795 are 7 · 95 = 665 and 79 · 5 = 395; the larger is 665.

Example 2

Input

100

Output

0
Explanation

The splits are 1 · 00 = 0 and 10 · 0 = 0.

Example 3

Input

105

Output

50
Explanation

The splits are 1 · 05 = 5 and 10 · 5 = 50.

Instructions and solution

TASK 02 2016.

The contest

District competition grade not stated

Source of the task: dms.rs/informatika-osnovne-skole

Task statement

Three programmers – A, B and C (in that order) entered a contest for choosing the winning video game. The jury gives each game two marks – a mark for design (the first mark) and a mark for functionality (the second mark). Write a program CONTEST that finds which programmer won first place.

The winner is chosen like this: for each game both marks are added up, and the programmer whose game has the largest sum wins. If several programmers have the same largest sum, the one who got the higher mark for design wins. If there is still a tie, the one who presented the game to the jury first wins (the order of presentation is A, B, C).

Input

Standard input contains, on one line, three pairs of natural numbers (none greater than 1000) separated by spaces. The first pair are the marks of the first programmer, the second pair the marks of the second, and the third pair the marks of the third programmer.

Output

Print one of the letters A, B or C, depending on which programmer won.

Example 1

Input

5 6 7 4 5 3

Output

B
Explanation

The sums are 11, 11 and 8. Programmers A and B have the same sum, but B has a higher mark for design (7 against 5).

Example 2

Input

14 18 17 7 18 7

Output

A
Explanation

The sums are 14 + 18 = 32, 17 + 7 = 24 and 18 + 7 = 25, so the winner is A. (In the original collection this input is given with the output B, which is an obvious mistake: by the rules A wins.)

Instructions and solution

TASK 03 2016.

Beauty

District competition grade not stated

Source of the task: dms.rs/informatika-osnovne-skole

Task statement

We say that a number is beautiful if all its digits are different from each other. Write a program BEAUTY that finds how many beautiful numbers there are in the given segment [m, n].

Input

The first line of standard input contains two whole numbers m and n separated by a space.

Constraints: 0 ≤ m ≤ n ≤ 10 000 000.

Output

Print to standard output one whole number – the number of beautiful numbers in the segment.

Example

Input

90 91

Output

2
Explanation

Both numbers, 90 and 91, have different digits.

Instructions and solution

District computer science competition – 2019

The tasks are taken from the site of the Mathematical Society of Serbia, computer science competitions: dms.rs/informatika-osnovne-skole

TASK 04 2019.

The meeting

District competition 7th grade

Source of the task: dms.rs/informatika-osnovne-skole

Task statement

Five programmers spend one day at the company. For each of them we know the hour and minute of arrival and the hour and minute of departure. Write a program that finds whether they can hold a joint meeting (at which all of them must be present) and, if they can, the longest possible length of such a meeting (in hours and minutes).

Input

Each of the five lines of standard input contains four whole numbers separated by spaces: the hour and minute of arrival and the hour and minute of departure of one programmer.

Output

Print to standard output the length of the meeting, with the number of hours and the number of minutes separated by one space, or the word no if the meeting cannot be held.

Example 1

Input

8 15 16 30
9 40 17 15
9 20 14 30
8 45 16 45
9 15 15 20

Output

4 50
Explanation

The last arrival is at 9:40, and the first departure at 14:30. The meeting lasts from 9:40 to 14:30, so 4 hours and 50 minutes.

Example 2

Input

7 30 12 30
7 48 13 29
12 28 19 12
12 33 17 37
11 36 18 51

Output

no
Explanation

The last arrival is at 12:33, and the first departure at 12:30, so they are never all at the company at the same time.

Instructions and solution

TASK 05 2019.

Ships

District competition grade not stated

Source of the task: dms.rs/informatika-osnovne-skole

What you need to know first: Data sizes Logical data types continue Nested loops Arrays

Task statement

A transport company runs ship lines between its home port and several (at most 5) other port cities. Exactly one ship runs on each line: it leaves the home port, takes passengers to its destination, and then brings other passengers back to the home port.

All the ships leave the home port at the same time, and for each of them we know how many days it needs to travel and come back to the home port (assume that each trip and return is completed within one calendar year). Write a program that finds after how many days all the ships will meet again in the home port. Note that, although a solution always exists, it can be a very large number.

Input

The number of lines (at most 5) is entered from standard input, and then, for each line, the number of days the ship needs to travel and come back.

Output

Print to standard output the required number of days.

Example

Input

3
15
24
60

Output

120
Explanation

The ships are together in the port after LCM(15, 24, 60) = 120 days.

Instructions and solution

TASK 06 2019.

Decimals

District competition grade not stated

Source of the task: dms.rs/informatika-osnovne-skole

What you need to know first: Operators Loops

Task statement

Write a program that calculates and prints the quotient of the natural numbers m and n (less than a million) with the given number of decimal places k (1 ≤ k ≤ 1000). Do not round the result – just cut off the decimals after the last required one. Show the result with a decimal comma (not a decimal point), as is usual in Serbia.

Input

The numbers k, m and n are read from standard input in that order (each on a separate line).

Output

Print the quotient m / n with exactly k decimal places, with a decimal comma.

Example 1

Input

20
1
7

Output

0,14285714285714285714

Example 2

Input

7
1
2

Output

0,5000000

Instructions and solution

TASK 07 2019.

Hidden codes

District competition grade not stated

Source of the task: dms.rs/informatika-osnovne-skole

Task statement

The police, trying to catch a hacker, managed to get hold of some messages the hackers exchange. Each message is one number. Encryption experts concluded that the number is a sequence of codes for accessing some computers. Each code contains only the digits from 0 to 5, the end of a code is marked by the digit 6 or by the end of the number, and the digits 7, 8 and 9 are used for masking and should be ignored. This way hackers can send several codes in one number, but the number 0 cannot be a code.

Write a program that, for the number K entered, prints the codes hidden in it (in any order) and how many codes there are in that number.

Input

The number K is read from standard input.

Output

On the first line print the codes (the words The codes are:, followed by the codes separated by spaces), and on the second line the number of codes sent.

Example

Input

23675

Output

The codes are: 5 23
2 codes were sent
Explanation

The digits 2 and 3 form one code, the digit 6 ends it, the digit 7 is ignored, and the digit 5 is the second code.

Instructions and solution

Other district competitions – combined

Tasks from the district competitions in 2009 and 2018 (the link to the site dms.rs is given at the top of the page).

TASK 08 2009.

GroupOS

District competition 5th and 6th grade

Source of the task: dms.rs/informatika-osnovne-skole

What you need to know first: Operators Selection statements

Task statement

At the big P.O.S.O.T. computer games tournament there are team competitions in which two teams play against each other. In the first round, three teams labelled with the numbers 1, 2 and 3 played in group O.S. The first game was played by teams 1 and 2, and their points were recorded (for team 1 as R11, and for team 2 as R12). In the second game teams 1 and 3 played (points R21 and R23), and then teams 2 and 3 (points R32 and R33).

A game is won by the team with more points. If the teams have the same number of points in a game, neither team gets a win. After all three games the winner of the round is decided: it is the team with the largest number of wins. If two teams have the same number of wins, which is larger than the number of wins of the third team, or if all three teams are level, extra games are played, marked DP (from the Serbian dodatne partije, extra games).

Write a program GROUPOS that reads the values R11, R12, R21, R23, R32 and R33 in order, and then finds and prints which team is the winner, or prints that extra games are played (DP).

Input

The numbers R11, R12, R21, R23, R32 and R33 are entered on one line, in that order.

Output

Print the label of the winning team (1, 2 or 3) or DP.

Example 1

Input

56 254 104 198 156 148

Output

2
Explanation

R11 = 56, R12 = 254, R21 = 104, R23 = 198, R32 = 156, R33 = 148. Team 2 won twice, team 3 once, team 1 never.

Example 2

Input

156 154 104 104 256 137

Output

DP
Explanation

Team 1 won the first game, the second game was a draw, and team 2 won the third. Teams 1 and 2 have one win each, so DP is played.

Instructions and solution

TASK 09 2009.

Sensors

District competition 6th, 7th and 8th grade

Source of the task: dms.rs/informatika-osnovne-skole

What you need to know first: Operators Loops Selection statements

Task statement

In one of the games Žarko plays at the P.O.S.O.T. tournament, the character he controls has to get through a room with sensors. The sensors are placed on the floor of the room in N parallel lines across the width of the room. The first line, which is closest to Žarko's character, has only one sensor, exactly in the middle of the room. The second line has three sensors, with the middle one exactly behind the sensor in the first line. The third line has 5 sensors, with the middle three exactly behind the sensors in the second line, and the other two one on each side.

Each next line has two more sensors, one added on the left and one on the right compared with the previous line. The distance between two neighbouring sensors in each line is the same and is R centimetres. Sensors placed this way form a triangle. The room is large enough for all the planned sensors.

Žarko's character in the game must go straight through the middle of the room to the other side, and it will activate all the sensors it passes over. If Žarko knows that his character is S centimetres wide, he needs to work out how many sensors his character will activate. Write a program SENSORS that finds the number of activated sensors for the values N, R and S entered.

District 2009: the task “Sensors”
Figure 1: District 2009: the task “Sensors”

Input

The number of lines N, the distance between sensors R and the width of the character S are entered on one line (R and S are real numbers).

Output

Print the number of activated sensors.

Example

Input

5 1.1 5.4

Output

19
Explanation

The lines have 1, 3, 5, 7 and 9 sensors, but a character 5.4 cm wide covers only the sensors at a distance of at most 2.7 cm from the middle, which are the two on each side (at 1.1 and 2.2 cm). So: 1 + 3 + 5 + 5 + 5 = 19.

Instructions and solution

TASK 10 2018.

Degrees

District competition 5th grade

Source of the task: dms.rs/informatika-osnovne-skole

What you need to know first: Operators Loops Selection statements

Task statement

The sum of the interior angles of every triangle is 180 degrees. A triangle is acute if all three of its angles are acute (less than 90 degrees), right-angled if one of its angles is a right angle (equal to 90 degrees), and obtuse if one of its angles is obtuse (greater than 90 degrees). Write a program that reads the sizes of two angles of a triangle in degrees (whole numbers, each on a separate line), decides whether the triangle is acute, right-angled or obtuse, and prints the corresponding text.

Input

The whole numbers – the sizes of two angles of the triangle in degrees – are entered on two lines.

Output

Print one of the words acute, right or obtuse (as in the examples).

Example 1

Input

30
60

Output

right
Explanation

The third angle is 180 − 30 − 60 = 90.

Example 2

Input

18
18

Output

obtuse
Explanation

The third angle is 180 − 18 − 18 = 144.

Example 3

Input

75
60

Output

acute
Explanation

The third angle is 45, so all three angles are acute.

Instructions and solution

TASK 11 2018.

Noughts and crosses

District competition 5th grade

Source of the task: dms.rs/informatika-osnovne-skole

What you need to know first: Operators Selection statements

Task statement

Mirko is a young programmer who is trying to program a game of noughts and crosses (tic-tac-toe). He is close to finishing, but he needs a little help. He came up with the idea that the user chooses with the mouse the square into which their symbol will be written. The playing field consists of 9 squares (arranged in three rows and three columns) and each square is 100 by 100 pixels (the field is 300 by 300 pixels).

We know the position of the pixel that was clicked with the mouse. We need to find the number of the square that contains that pixel. The position of the pixel is given by the numbers (coordinates) of that pixel horizontally and vertically, counting from the bottom left corner of the field (the pixels are numbered from 1 to 300). The squares are numbered from 1 to 9, row by row, starting from the bottom left corner of the field upwards, as shown in the picture.

789456123
Figure 2: Numbering of the squares in the task Noughts and crosses

Input

Two whole numbers (from 1 to 300) are entered on one line: the horizontal coordinate of the pixel and the vertical coordinate.

Output

Print the number of the square (from 1 to 9) that contains that pixel.

Example 1

Input

1 1

Output

1

Example 2

Input

120 280

Output

8

Example 3

Input

100 201

Output

7

Example 4

Input

101 200

Output

5

Instructions and solution

TASK 12 2009.

Žarko

District competition 5th and 6th grade

Source of the task: dms.rs/informatika-osnovne-skole

What you need to know first: Data Operators

Task statement

At the big P.O.S.O.T. computer games tournament Žarko played four games. After the games he decided that it was easier for him to follow the change in score between two games than the exact number of points. So he remembered that:

  • in the second game he had exactly 1042 points more than in the first,
  • in the third game he had as many as in the first and second together,
  • in the fourth game he had half as many as in the third.

After the end he knew the exact number of points he had scored in the fourth game. Write a program ZARKO that, for the number of points C scored in the fourth game, calculates how many points Žarko scored in the first game.

Input

The number C – the number of points in the fourth game – is read from standard input.

Output

Print the number of points in the first game.

Example

Input

2845

Output

2324
Explanation

First game: 2324, second: 3366, third: 5690, fourth: 2845.

Instructions and solution

District competitions for the 7th and 8th grade

The tasks are taken from the site takprog.petlja.org/osnovnaskola/posts/30. To test the examples, log in to that site.

Year 2023/24

TASK 13 2023/24.

A strange journey

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

Travelling from Agrad to Begrad, Maja passes four signposts (not necessarily in the order in which they are given in the input). The signpost with number i is described by two numbers: ai and bi – this means that the signpost is ai kilometres from Agrad and bi kilometres from Begrad.

However, one signpost is wrong. Find which one.

Input

Each of the four lines of standard input contains two numbers ai and bi. Maja does not like long journeys, so all the numbers in the input are less than 109.

Output

Print to standard output exactly one number – the number of the signpost that is wrong.

Example

Input

2 9
8 3
9 4
5 6

Output

3
Explanation

The signposts are numbered 1, 2, 3 and 4 in order. The sums are 11, 11, 13 and 11, so the third signpost is wrong.

Instructions and solution

TASK 14 2023/24.

Product of prime digits

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

A natural number n (n ≤ 109) is given. Find the product of the prime digits in that number.

Input

Exactly one natural number n is entered from standard input.

Output

Print to standard output one natural number – the product of the prime digits in that number. If the number has no prime digits, print -1.

Note. We say that a natural number is prime if it has exactly two different divisors in the set of natural numbers. The numbers 0 and 1 are not prime.

Example 1

Input

52483579

Output

1050
Explanation

The product of the prime digits is 5 · 2 · 3 · 5 · 7 = 1050.

Example 2

Input

186

Output

-1
Explanation

The digits 1, 8 and 6 are not prime.

Example 3

Input

1026

Output

2

Instructions and solution

TASK 15 2023/24.

Sentences in brackets

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

We are given a string that contains single words and sentences separated by spaces, where the sentences are enclosed in square brackets. Write a program that finds all the words and sentences written.

Input

A string of at most 10 000 characters, made of lowercase letters of the English alphabet, spaces and square brackets, is read from standard input. The string is written correctly and contains words and sentences. The string does not contain several consecutive spaces.

Output

Print to standard output each word or sentence on a separate line.

Example 1

Input

[hello everyone] good morning [how are you] bye

Output

hello everyone
good
morning
how are you
bye

Example 2

Input

hello everyone

Output

hello
everyone

Example 3

Input

Today is [a competition] [in programming]

Output

Today
is
a competition
in programming

Instructions and solution

TASK 16 2023/24.

Competitors

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

We say that a pair of competitors is good if the difference between the points of the two competitors is at most k. Find the number of good pairs in the competition.

Input

The first line of standard input contains the number of competitors n (n ≤ 105). The second line contains n natural numbers less than 105 – the points of the competitors. The third line contains the largest allowed difference in points k (1 ≤ k ≤ 105).

In 50% of the tests n ≤ 103. In the other tests the number of competitors is large, so an efficient solution is expected for 100% of the points.

Output

Print to standard output exactly one whole number – the number of good pairs.

Example 1

Input

6
1 12 2 9 5 8
4

Output

8
Explanation

The good pairs are the competitors with the points (1,2), (1,5), (12,9), (12,8), (2,5), (9,5), (9,8) and (5,8).

Example 2

Input

10
15 3 24 17 9 16 3 22 19 8
5

Output

14

Instructions and solution

TASK 17 2023/24.

The triangle with the largest perimeter

District competition 8th grade

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

If we know the lengths of the sides of several triangles, find the value of the largest perimeter of those triangles.

Input

The number n (1 ≤ n ≤ 100) is entered from standard input, and then on the next n lines three natural numbers each, which are the lengths of the sides of a triangle (all the triples of numbers satisfy the triangle inequalities).

Output

Print to standard output the largest perimeter of a triangle.

Example

Input

3
3 4 5
10 3 11
5 7 8

Output

24
Explanation

The perimeters are 12, 24 and 20; the largest is 24.

Instructions and solution

TASK 18 2023/24.

The best players of a tournament

District competition 8th grade

Source of the task: takprog.petlja.org — the original collection of tasks

What you need to know first: Operators, loops, arrays
Optional: Vectors Maps in C++

Task statement

Ivan wrote down the results of the 4 quarter-final matches, 2 semi-final matches and the final match of the knockout stage of a tournament (7 matches in total). The results given in the input are not sorted (for example, the last line of the input does not necessarily mean that this match was played last). Find the winner and the runner-up of the tournament.

Input

Seven lines of standard input contain 2 names each, separated by one space. The first name on a line is the winner, and the second the loser of that match. All the names consist of at least one and at most 10 letters of the English alphabet.

Output

On the first line of standard output print the winner of the tournament. On the second line print the runner-up of the tournament.

Example

Input

Bojan Aleksa
Vanja Goran
Emil Zivadin
Emil Vanja
Vanja Bojan
Emil Dusan
Dusan Djordje

Output

Emil
Vanja
Explanation

In the quarter-finals: Emil beat Živadin, Dušan beat Đorđe, Bojan beat Aleksa, Vanja beat Goran. In the semi-finals Emil beat Dušan, and Vanja beat Bojan. In the final Emil beat Vanja.

Instructions and solution

TASK 19 2023/24.

Scissors

District competition grade not stated

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

In a children's game every child stands next to their own tree, except one, whom we call the tailor. The tailor has no tree to stand next to, but goes from child to child asking: “Who has the scissors?”. The child asked answers with the name of one of the other children. While the tailor is moving towards the child named, all the other children try to swap places, and the tailor tries to take a tree by reaching it before the other child, or at least at the same time. If the tailor succeeds, the child left without a tree takes over the tailor's role.

In this task we assume that all the children move at the same speed and that the tailor immediately notices every swap that starts and heads for the nearer of the two temporarily free trees. The first child is trying to work out with how many other children it could swap places without the tailor being a threat. Can you help?

The task Scissors – district computer science competition, 8th grade, 2023/24
Figure 3: The task Scissors – district computer science competition, 8th grade, 2023/24

Input

The first line of standard input contains two whole numbers separated by a space – the coordinates of the tailor. The second line contains one whole number n, the number of trees, i.e. the number of children who are not the tailor (n ≤ 50). Each of the next n lines contains two whole numbers separated by a space – the coordinates of one child (the first child given is the one making the estimate). All the coordinates are in the interval [0, 30000].

Output

Print to standard output one whole number – the number of children with whom the first child can safely swap places.

Example 1

Input

19 7
5
11 11
3 17
8 4
14 3
18 15

Output

1
Explanation

The first child is at (11, 11). Only the swap with the child at (8, 4) is safe: the children run about 7.6, while the tailor is about 8.9 away from the nearer tree.

Example 2

Input

100 100
4
50 50
150 50
50 150
150 150

Output

0
Explanation

The tailor is in the middle, so for every swap he is closer to one of the free trees than the distance the children need to run.

Instructions and solution

TASK 20 2023/24.

Ropes

District competition grade not stated

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

Mika gave Pera N (N ≤ 2·105) pieces of string of whole-number lengths (all the strings are shorter than 1018). Pera throws away exactly one string from the set he was given. After Pera throws away that string, he wants to cut the remaining strings into pieces of the same length (all the pieces must be of equal length, regardless of which string they came from). Pera wants to throw away the string after which he could cut the remaining N − 1 strings into pieces as long as possible. What is that length?

Remember that your solution must be efficient enough – it must run in less than 1 second.

Input

The first line of standard input contains a natural number N – the number of strings (N ≤ 2·105). The second line contains exactly N natural numbers – the lengths of the strings (all the strings are shorter than 1018).

Extra constraints. The tests are divided into three disjoint groups:

  • 40 points: N ≤ 103 and 1 ≤ Ai ≤ 104;
  • 20 points: N ≤ 4·104 and 1 ≤ Ai ≤ 104;
  • 40 points: N ≤ 2·105 and 1 ≤ Ai ≤ 1018.

Output

Print to standard output exactly one natural number – the maximum length into which we can cut the strings so that all the pieces left are of the same length.

Example 1

Input

3
8 39 12

Output

4
Explanation

Pera can throw away the string of length 39. Then he can cut the remaining two strings into pieces of length 4. If he threw away the string of length 8, the maximum length would be 3, and if he threw away the string of length 12, it would be 1. So the maximum length is 4.

Example 2

Input

4
10 13 12 8

Output

2

Instructions and solution

Year 2022/23

TASK 21 2022/23.

Millionaire

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

What you need to know first: Operators, loops, arrays
Optional: Vectors

Task statement

In the quiz “Who Wants to Be a Millionaire?” contestants answer questions that have 4 possible answers (a, b, c, d). The contestants have three lifelines:

  • Ask the audience – 100 viewers vote for one of the 4 answers, and the contestant can see how many people voted for each answer.
  • Fifty-fifty – the computer randomly removes two wrong answers and leaves the player one correct and one wrong answer.
  • Phone a friend – the contestant phones a friend, who says what they think the correct answer is.

Darko has reached the second-to-last question and still has all three lifelines. Since he does not know the answer, he decided on the following tactic: first he will ask the audience, and if one answer has more votes than all the others together, he will choose it. If no answer meets the condition, he will use fifty-fifty. When two answers are left, Darko will choose the one that has at least 20 more audience votes than the other. If still no answer meets the condition, Darko will phone a friend and answer as the friend tells him. Find Darko's final answer to the second-to-last question.

Input

The first line of standard input contains 4 whole numbers separated by spaces, which are the numbers of viewers who voted for the answers a, b, c and d, in that order. All these numbers are between 0 and 100 and their sum is 100.

If needed (i.e. if Darko did not give an answer based on the first lifeline), the second line contains two different letters (from the set a, b, c, d) separated by a space, which are the answers the computer left for Darko.

If needed (i.e. if Darko did not give an answer based on the second lifeline either), the last line contains one letter – the answer the friend suggests. That letter is certainly one of those the computer left as a possible answer.

Output

On the only line of standard output print one letter (a, b, c or d), the final answer Darko gave.

Example 1

Input

12 17 11 60

Output

d
Explanation

More viewers voted for answer d than for all the others together, so Darko immediately decides on that answer and does not use the other lifelines.

Example 2

Input

12 17 34 37
b c
c

Output

c
Explanation

No answer has the necessary majority, so Darko asks for fifty-fifty. The answers b and c are left, whose difference in audience votes (17 and 34) is less than 20, so Darko phones a friend and answers c.

Instructions and solution

Solution – analysis
TASK 22 2022/23.

Two pictures on a sheet of paper

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

What you need to know first: Operators, loops, arrays
Optional: Vectors

Task statement

Write a program that checks how many valid arrangements there are of two pictures of known sizes on a sheet of paper of known size, such that the edges of the pictures are parallel to the edges of the paper, the pictures are shown whole and they do not overlap. The pictures may not be rotated, but the paper may (it can be placed either in the horizontal “landscape” orientation or in the vertical “portrait” orientation). The pictures may be placed either one below the other or one next to the other, on paper turned either horizontally or vertically (so there are at most 4 possible arrangements, and some of them may be invalid because the pictures are too large).

Input

The sizes (width and height) of the paper, the sizes of the first picture and the sizes of the second picture are entered from standard input.

Output

Print to standard output a number from 0 to 4, the number of possible ways to arrange the pictures on the paper.

Example 1

Input

800 600
200 400
400 200

Output

4
Explanation

All 4 arrangements of the pictures are valid.

Example 2

Input

800 600
400 500
300 650

Output

0
Explanation

All 4 arrangements of the pictures are invalid.

Example 3

Input

800 600
400 500
300 250

Output

2
Explanation

2 arrangements are valid and 2 are invalid.

Instructions and solution

Solution – analysis
TASK 23 2022/23.

Product without the current number

District competition grade not stated

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

For each position in an array of whole numbers, find the product of all the elements of the array except the element at that position.

Input

The number n (1 ≤ n ≤ 105) is read from standard input, and then the elements of the array (numbers from −10 to 10); it is guaranteed that each of the required products is a number between −109 and 109.

Output

Print to standard output the required products separated by spaces.

Example 1

Input

5
1 2 3 4 5

Output

120 60 40 30 24

Example 2

Input

5
1 2 0 4 5

Output

0 0 40 0 0

Instructions and solution

TASK 24 2022/23.

K nearest

District competition grade not stated

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

We are given an array of n whole numbers and one whole number x. Write a program that finds the k numbers from the array nearest to the number x (if two numbers are at the same distance from x, the smaller one is chosen). The distance is the absolute value of the difference of the numbers.

Input

The numbers n (1 ≤ n ≤ 105), k (1 ≤ k ≤ n) and x (1 ≤ x ≤ 109) are entered on the first line of standard input, separated by spaces. Then the next line contains n whole numbers ai (1 ≤ ai ≤ 109), the elements of the array, also separated by spaces.

Output

Print to standard output the required k numbers separated by spaces, in non-decreasing order.

Example 1

Input

7 4 4
7 4 2 3 1 5 6

Output

2 3 4 5
Explanation

Of all the numbers entered, the 4 numbers nearest to the number 4 are 2, 3, 4 and 5. The other numbers have a larger difference from the number 4.

Example 2

Input

5 3 2
3 1 3 1 3

Output

1 1 3

Example 3

Input

5 3 20
5 3 2 4 1

Output

3 4 5

Instructions and solution

TASK 25 2022/23.

The delivery robot

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

What you need to know first: Operators, loops, arrays
Optional: Vectors

Task statement

A company that sells online uses robots to deliver the goods bought. The customers that the robot Mile will serve tomorrow all live on the same side of the same street. For each address Mile knows its coordinate, i.e. the distance from the beginning of the street to that building. Mile is programmed to visit the addresses in the order in which they are given to him.

Write a program that, for the given number of deliveries and the given coordinates of the buildings (the distances of the buildings from the beginning of the street), calculates and prints the total distance the robot Mile will travel from the first to the last delivery.

Input

The first line of standard input contains a natural number n, not greater than 30. Each of the next n lines contains one unsigned whole number – the distance from the beginning of the street to the next building (i.e. the coordinate of the building) to which an order should be delivered. These numbers are not greater than 10000.

Output

Print to standard output only one whole number – the total length of the route the robot Mile travels.

Example 1

Input

5
2
7
7
1
3

Output

13
Explanation

The positions of the buildings in the order of delivery are 2, 7, 7, 1, 3. Mile first goes from position 2 to position 7, then from 7 to 7 (a delivery in the same building), then from 7 to 1 and finally from 1 to 3. The distances travelled are 5, 0, 6, 2, and their sum is 13.

Example 2

Input

1
7

Output

0
Explanation

The first and the last delivery are the same delivery, so Mile does not move at all and the distance travelled is 0.

Instructions and solution

TASK 26 2022/23.

Minimax

District competition grade not stated

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

A teacher gave Perica homework in which, from 4 given decimal digits, he has to find the difference between the largest and the smallest four-digit number that can be made from those digits. Write a program that, from the 4 decimal digits entered, will help Perica check whether he did his homework correctly.

Input

Each of the 4 lines of standard input contains one decimal digit, at least one of which is not zero.

Output

Print to standard output only one natural number, the difference between the largest and the smallest four-digit number that can be made from the given digits.

Example 1

Input

3
7
5
9

Output

6174
Explanation

The largest four-digit number that can be made is 9753, and the smallest 3579. The difference is 9753 − 3579 = 6174.

Example 2

Input

2
0
5
6

Output

4464
Explanation

The largest number is 6520, and the smallest 2056. The difference is 6520 − 2056 = 4464. The task asks for a four-digit number, so the number must not start with zero – that is why the smallest number is not 0256.

Example 3

Input

0
0
5
0

Output

0
Explanation

The largest and the smallest four-digit number are the same: 5000. The difference is 0.

Instructions and solution

Year 2021/22 – tasks for the 7th grade

TASK 27 2021/22.

Noughts and crosses (winner)

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

Two players are playing noughts and crosses. The game is played on a 3×3 board on which the players take turns writing a cross or a nought. The winner is the player who manages to write 3 of their signs in one row, one column or one diagonal. For the given final position of the game, decide whether the cross player won, the nought player won, or the game ended in a draw.

Input

Each of the three lines of standard input contains three characters x, o or *. The first line of the input describes how the first row of the board is filled, and so on. The mark x means a cross, o a nought, and * that the cell is empty at the end of the game.

Output

If the cross won, print the character x; if the nought won, print o; and if it is a draw, print nereseno (Serbian for “draw”; this is the original word checked by the tests on petlja.org).

Example 1

Input

x*x
ox*
xoo

Output

x
Explanation

The cross has three signs on the anti-diagonal.

Example 2

Input

xxo
oox
xox

Output

nereseno

Instructions and solution

TASK 28 2021/22.

Digits and letters

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

Write a program that, for the string entered, finds the absolute value of the difference between the number of digits and the number of letters in that string. Lowercase and capital letters of the English alphabet count as letters.

Input

The only line of standard input contains a string of at most 20 characters. Besides lowercase and capital letters of the English alphabet and digits, the string may contain spaces and other ASCII characters (letters of other alphabets will not appear).

Output

Print the required value to standard output.

Example

Input

Programming 1

Output

10
Explanation

The string has 11 letters and 1 digit, so the difference is 10.

Instructions and solution

TASK 29 2021/22.

Accounts

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

We need to simulate a banking system for k different users. Each user has an account with a starting balance of 0. Two kinds of operations need to be supported:

  • upit x – finds how many users have exactly x dinars in their account (upit is Serbian for “query”; this is the original keyword used in the tests on petlja.org);
  • name x – adds x dinars to the account of the person called name (x can also be negative).

Write a program that supports carrying out n such operations.

Input

The numbers n and k are entered from standard input. After that, one operation is entered on each of n lines.

Output

For each query (an operation of the first kind) print the answer, each on a separate line.

Example

Input

6 4
marko 2
milan 5
dragana 4
upit 0
milan -1
upit 4

Output

1
2
Explanation

After the first three operations Marko has 2, Milan 5 and Dragana 4 dinars, and the fourth user 0 – exactly one account has 0 dinars. When Milan loses 1 dinar, both Milan and Dragana have 4 dinars.

Instructions and solution

TASK 30 2021/22.

Moles and flags

District competition 7th grade

Source of the task: takprog.petlja.org — the original collection of tasks

Task statement

Flags are stuck into the ground along a meadow (for each flag a whole-number x-coordinate is known). This annoyed the moles, who took action, and each of them dug a hole somewhere along the meadow (for each hole a whole-number x-coordinate is known). Around each hole the ground becomes unstable, and all the flags at a distance of at most d from some hole collapse (including those at distance d). Write a program that finds how many flags are left after all the holes have been dug.

Input

The first line of standard input contains the number of flags m (1 ≤ m ≤ 105), and the second the whole-number coordinates of all the flags (whole numbers between 0 and 109) separated by single spaces.

The third line contains the number of holes n (1 ≤ n ≤ 105), and the fourth the whole-number coordinates of all the holes (whole numbers between 0 and 109), also separated by single spaces. Finally, the fifth line contains the number d (1 ≤ d ≤ 109).

Extra constraints. In tests worth 49% of the points, also 1 ≤ m ≤ 100 and 1 ≤ n ≤ 100; in tests worth 51% of the points there are no extra constraints.

Output

Print to standard output the number of flags left.

Example

Input

3
4 5 8
4
10 9 1 8
2

Output

2
Explanation

The flag at 8 collapses because of the hole at 8. The flags at 4 and 5 are at a distance of 3 > 2 from the nearest hole, so they are left.

Instructions and solution

Quiz: check what you have learned

Ten short questions about the ideas from the tasks on this page. Choose an answer – you will immediately see whether it is correct and why.