A COLLECTION OF TASKS · PRIMARY SCHOOL · 8TH GRADE

Qualifications for the district computer science competitions – 8th grade

This collection contains tasks from the qualifications for the district computer science competitions intended for 8th grade pupils (and older competitors), as well as tasks for which the grade is not stated. The tasks follow the order of the original collection, with separate statements, input, output, examples and extra explanations.

Tasks for the 5th, 6th and 7th grade can be found on the separate page with qualifications.

TASK 01 2022/2023.

The cook

Qualifications, round 1 8th grade

Source of the task: The original collection of tasks on Petlja

What you need to know first: Data in C++ Operators in C++

Task statement

For one portion the cook needs m grams of meat for the main course, and for the salad either k grams of cabbage or c grams of beetroot.

There are M grams of meat, K grams of cabbage and C grams of beetroot available (and there is much more of all the other ingredients).

Write a program that reads the amounts needed for one portion m, k, c, and then the available amounts M, K, C, and prints for how many whole portions (main course and salad) the cook has enough ingredients.

Input

Standard input contains, in order, the numbers m, k, c, M, K, C, each on a separate line. All the numbers are whole, positive and less than 50000.

Output

Print to standard output only one non-negative whole number: the number of whole portions that can be prepared.

Example 1

Input

200
250
150
2200
1400
890

Output

10

Example 2

Input

150
200
100
6100
5100
1000

Output

35
Note

In the original text, the labels for the available amounts were not fully given in the sentence. Here they are written as M, K and C, following the order of the numbers in the input.

Open the solution
TASK 02 2022/2023.

Towers of blocks

Qualifications, round 1 8th grade

Source of the task: The original collection of tasks on Petlja

What you need to know first: Data in C++ Operators in C++ Branching in a program

Task statement

Two friends are playing with blocks. From the previous game they have two towers left, of heights A and B. Now they have two ideas for a new building.

For the first idea they need towers of heights A1 and B1, and for the second towers of heights A2 and B2.

To change the height of a tower by 1, one friend needs Tx time and the other Ty time. What is the least time in which the friends can rebuild (make taller or shorter) the existing towers into towers that fit one of the ideas, if each of them rebuilds one tower?

The time is measured from the moment the friends start rebuilding at the same time until the moment both of them have finished.

Input

Each of the four lines of standard input contains two natural numbers separated by a space:

  • the first line contains A and B;
  • the second line contains A1 and B1;
  • the third line contains A2 and B2;
  • the fourth line contains Tx and Ty.

The values A, B, A1, B1, A2 and B2 are in the interval [1, 1000000], and Tx and Ty in the interval [1, 100].

Output

Print to standard output one natural number — the required least time.

Example 1

Input

10 20
18 28
21 31
1 10

Output

20

Example 2

Input

7 4
9 13
14 12
3 2

Output

15
TASK 03 2022/2023.

The date with the largest takings

Qualifications, round 1 8th grade

Source of the task: The original collection of tasks on Petlja

Task statement

We know all the fiscal receipts issued by one bookshop. From each receipt we can read the date when it was issued and the amount that was charged.

Write a program that finds the largest total amount the bookshop charged in one day, i.e. the maximum daily takings, as well as the date when that amount was the largest.

If there are several such dates, print all of them, in chronological order from the oldest to the newest.

Input

The number of receipts issued n (1 ≤ n ≤ 105) is read from standard input, and then the data for each receipt in the form:

dd-mm-yyyy amount

The amount is a real number rounded to two decimal places.

Output

Print to standard output the maximum amount, as a real number rounded to two decimal places, and then on the following lines the dates when that maximum amount was charged.

If there are several dates, they should be printed in order from the oldest to the newest.

Example

Input

5
03-07-2021 340.00
05-04-2021 285.50
03-07-2021 100.50
04-07-2021 270.00
05-04-2021 155.00

Output

440.50
05-04-2021
03-07-2021
Explanation of the example

For the date 03-07-2021 the total takings are 340.00 + 100.50 = 440.50.

For the date 05-04-2021 the total takings are 285.50 + 155.00 = 440.50.

These are the largest daily takings. Since both dates have the same amount, both are printed, chronologically: first 05-04-2021, and then 03-07-2021.

Idea for the solution

We need to add up the amounts of all the receipts that have the same date. Then, among the total daily amounts, we find the largest one and pick out all the dates that have exactly that amount.

For grouping we can use a map (map), in which the date is the key and the value is the sum of the amounts for that day.

We must take special care with the chronological order: text dates of the form dd-mm-yyyy cannot be put in the right chronological order with ordinary lexicographic sorting.

Extra instructions

Open the solution
TASK 04 2022/2023.

Sums after splitting

Qualifications, round 1 Arrays and parity

Source of the task: The original collection of tasks on Petlja

Task statement

An array of whole numbers of length n is given. The array has to be cut into two parts.

After cutting, we count how many pairs of numbers there are such that one number is from the left part, the other from the right part, and their sum is an even number.

Find the maximum number of such pairs that can be obtained by cutting the array.

Input

The whole number n (2 ≤ n ≤ 50000) is entered from standard input. Then, on the next line, the elements of the array a[i] (0 ≤ a[i] ≤ 100) are entered, separated by spaces.

Output

Print to standard output one whole number, the required maximum value.

Example 1

Input

8
1 7 3 4 6 5 8 0

Output

7
Explanation

The largest number of pairs is obtained by splitting the array into the parts:

Left part:  1 7 3 4 6
Right part: 5 8 0

The pairs whose sum is even are:

(1,5), (7,5), (3,5),
(4,8), (4,0), (6,8), (6,0)

There are 7 pairs altogether.

Example 2

Input

7
1 9 3 11 12 4 8

Output

4
Explanation

One optimal split is:

Left part:  1 9
Right part: 3 11 12 4 8

All the numbers in the left part are odd. In the right part there are two odd numbers, 3 and 11, so each of them can form an even sum with both numbers from the left part.

We get 2 × 2 = 4 pairs.

Idea for the solution

The sum of two numbers is even if both are even or both are odd. That is why we do not need to check every possible pair of numbers. It is enough to count the even and odd elements to the left and to the right of the cut.

For a particular split, the number of suitable pairs is:

even_left * even_right
+ odd_left * odd_right

The cut moves through the array, and the counters of even and odd elements are updated as each element moves from the right side to the left side.

Extra instructions

Open the solution
TASK 05 2022/2023.

The mixer

Qualifications, round 3 Permutations

Source of the task: The original collection of tasks on Petlja

Task statement

We have a mixer that can shuffle the order of the elements of an array. The mixer always shuffles the elements in the same way.

We are given the array obtained by shuffling the array 0 1 2 ... n-1. Write a program that finds the array obtained by shuffling the array 0 1 2 ... n-1 a total of m times.

Input

The natural numbers n (1 ≤ n ≤ 50000) and m (0 ≤ m ≤ 1018) are entered from standard input, separated by a space.

The next line contains n different numbers between 0 and n−1, separated by spaces, which are the elements of the array 0 1 2 ... n-1 after one shuffle.

Output

Print to standard output n different numbers between 0 and n−1, separated by spaces, which are the elements of the starting array after m shuffles.

Example 1

Input

4 2
0 2 3 1

Output

0 3 1 2
Explanation

The starting array is:

0 1 2 3

The first shuffle gives:

0 2 3 1

The second shuffle gives:

0 3 1 2

Example 2

Input

5 3
1 2 0 4 3

Output

0 1 2 4 3
Explanation

The starting array is:

0 1 2 3 4

After the first shuffle:

1 2 0 4 3

After the second shuffle:

2 0 1 3 4

After the third shuffle:

0 1 2 4 3

Example 3

Input

4 3
1 0 3 2

Output

1 0 3 2
Explanation

The starting array is:

0 1 2 3

The first shuffle gives:

1 0 3 2

The second shuffle gives the starting array:

0 1 2 3

The third shuffle gives:

1 0 3 2
Idea for the solution

If we repeated the shuffle one by one, the algorithm would be too slow for a very large m. We need to use the fact that the mixer always applies the same permutation.

One possibility is to split the permutation into cycles. In each cycle the elements return to their starting positions after a certain number of shuffles.

For a cycle of length k it is enough to calculate m % k. This avoids repeating all m shuffles and gives an efficient solution even when m is very large.

Extra instructions and video

Open the solution

Video explanation

Video in Serbian with English subtitles – turn on subtitles (CC) in the YouTube player.

TASK 06 2022/2023.

A correct expression

Qualifications, round 2 8th grade

Source of the task: The original collection of tasks on Petlja

What you need to know first: Data in C++ Operators in C++ Branching in a program Loops Strings Arrays

Task statement

In the expression a_b_c=d two arithmetic operations have to be put in place of the underscores so that the expression is correct.

The operations that can be used are addition (+), subtraction (-), multiplication (*) and integer division (/). The standard order of operations applies.

Input

Each of the four lines of standard input contains one whole number: a, b, c and d, in that order. All the numbers are in the range [1, 1000].

Output

Print to standard output all possible solutions, in any order, each on a separate line. One solution consists of two signs separated by a space, which are the operations to be put in, in that order.

If a correct expression cannot be made, print nemoguce (Serbian for “impossible”; this is the original word checked by the tests on petlja.org).

Example 1

Input

2
3
4
2

Output

* -
+ /
- /
Explanation

The solution * - is correct because:

2 * 3 - 4 = 2

The solution + / is also correct:

2 + 3 / 4 = 2

Here 3 / 4 is integer division, so its result is 0. Division has a higher priority than addition.

In the same way:

2 - 3 / 4 = 2

Example 2

Input

8
3
2
12

Output

* /

Example 3

Input

2
2
9
3

Output

nemoguce

Example 4

Input

2
2
7
4

Output

nemoguce
Idea for the solution

For each of the two empty places there are four possibilities: +, -, * and /. So there are only 4 × 4 = 16 combinations altogether.

Instead of trying to derive the operations mathematically, we can try all 16 pairs and check whether the expression gives the value d.

It is important to evaluate the expression with the standard order of operations: multiplication and division take precedence over addition and subtraction. The operator / is integer division.

Extra instructions

Open the solution
TASK 07 Arrays · Loops

Valleys

Arrays Consecutive elements Conditions
What you need to know first: Arrays Loops Branching in a program

Task statement

We are given a sequence of the altitudes of some terrain. A valley is a part of the terrain made of one or more consecutive equal heights, such that the height just before that part and the height just after it are both strictly greater than the height of the bottom of the valley.

Write a program that finds how many valleys there are in the given terrain.

Important: Several consecutive equal heights that form the bottom of one valley count as one valley. The bottom must have neighbours on both sides, and both neighbouring heights must be strictly greater.

Input

The number of parts of the terrain \(n\) is entered from standard input, where \(1 \leq n \leq 150\). On the next line \(n\) whole numbers separated by spaces are entered; they are the altitudes of the terrain. All the heights are in the interval [0, 100].

Output

Print one whole number, the number of valleys.

Example 1

Input

8
5 3 3 7 4 4 4 8

Output

2

Explanation

There are two valleys in the sequence. The first is made by the heights 5 3 3 7, and the second by the heights 7 4 4 4 8. Their bottoms are at heights 3 and 4.

Example 2

Input

5
4 3 1 2 4

Output

1

Explanation

The only valley is 3 1 2, whose bottom is at height 1.

Example 3

Input

11
2 2 3 4 4 2 2 2 5 4 5

Output

2

Explanation

The valleys are 5 2 2 2 5 and 5 4 5. In both cases the bottom is lower than the neighbouring heights.

Understanding and solving the task

We go through the sequence from left to right and look at groups of consecutive equal heights. Each group is one possible valley bottom. To decide whether a group really is a valley, we check the height just to the left and just to the right of it.

If the group has neighbours on both sides and both neighbouring heights are strictly greater than the height of the group, we increase the number of valleys by one. Then we continue from the first element after that group.

Key idea: First find the beginning and the end of the block of equal heights. Then check whether the heights right next to the block are greater than the height of the bottom. This way one block of equal heights is counted only once.

An example of recognising a valley

In the sequence 5 3 3 3 7, all three elements of height 3 form the bottom of one valley. The height to the left of the block is 5 and to the right 7, so both neighbours are higher than the bottom.

5 > 3
7 > 3
TASK 08 2022/2023.

Notes

Qualifications, round 1 8th grade

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

What you need to know first: Data in C++ Operators in C++ Branching Loops Strings Arrays

Task statement

In music, notes are arranged in octaves. One octave has seven natural notes: C, D, E, F, G, A and H, as well as five sharp notes that lie between some of the natural notes.

We agree to write all the notes in between as sharps, with the sign #. The order of the notes is:

C, C#, D, D#, E, F, F#, G, G#, A, A#, H

After the note H comes C again. (H is the name used in Serbian and German for the note called B in English; the task and its tests use H.) Transposing a melody means moving every note a certain number of places forwards or backwards in this cyclic order.

Write a program that transposes the melody entered by the given number of notes.

Input

The first line contains a string of at most 1000 characters that represents the melody. The notes are written one after another, without spaces. After that a whole number between -11 and 11 is entered; it is the number of places to transpose by. A positive number means moving forwards, and a negative one backwards.

Output

Print the transposed melody, without spaces between the notes.

Example 1

Input

GDGHDCAF#D
5

Output

CGCEGFDHG

Explanation

Moving the note G by five places gives C, because after H we go back to C. In the same way D becomes G, and F# becomes H.

Example 2

Input

C#F#AG
-3

Output

A#D#F#E

Understanding and solving the task

Each note needs to be given an index in the array of 12 notes. When we find the index of a note, we add the number of places to transpose by and get the index of the new note.

Since the notes repeat cyclically, the index must stay in the range from 0 to 11. We achieve this with the remainder of division by 12. We must take special care with negative shifts, because in C++ the remainder of a negative number can be negative.

Key idea: The notes are stored in an array in the order they appear. We first recognise each note in the input melody, and then move its index cyclically. A note with the sign # is one note, so its characters are processed together.

An example of shifting

If the index of the note G is 7 and the shift is 5, the new index is:

(7 + 5) % 12 = 0

Index 0 corresponds to the note C, so G transposed five places forwards becomes C.

TASK 09 2022/2023.

An error in the text

Qualifications, round 2 7th and 8th grade

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

What you need to know first: Data in C++ Operators in C++ Branching Loops Strings Arrays

Task statement

A message of four capital letters was sent three times. Each time, because of interference during transmission, exactly one character of the message was received as some other capital letter.

Write a program that reconstructs the original message from the three messages received. The reconstructed message has four characters. At the positions where the original letter can be determined with certainty that letter is printed, and at the other positions an asterisk (*).

Input

Each of the three lines of standard input contains four capital letters of the English alphabet.

Output

Print on one line the four characters of the reconstructed message. If the original character at some position cannot be determined uniquely, print an asterisk at that position.

Example 1

Input

AXCD
ABYD
ABCZ

Output

ABCD

Example 2

Input

AXCD
AYCD
AZCD

Output

A*CD

Understanding and solving the task

The original message has only four letters, so we can try all possible messages made of capital letters of the English alphabet. There are \(26^4 = 456976\) such messages.

We accept a candidate only if it differs from each of the three received messages at exactly one position. After we have found all the candidates that meet this condition, we compare their letters position by position.

Key idea: If the letter at some position is the same in all the accepted candidates, we can safely print that letter. If the candidates differ at that position, we print an asterisk.

An example of the reasoning

In example 2, all three messages start with the letter A and end with CD. The second letter is different: X, Y and Z. So the first, third and fourth positions are known, while an asterisk is printed at the second.

TASK 10 2022/23 · 8th grade

Number of three-digit even numbers

Arrays Digits Counting

Note: A task from round 2 of the qualifications, season 2022/23, for the 8th grade. Taken from takprog.petlja.org.

What you need to know first: Data in C++, Operators in C++, Branching, Loops and Arrays.

Optional: Dynamic array – vector.

Write a program that, for a given sequence of digits, prints the number of different three-digit even numbers that can be made from the given digits.

When making each number, every element of the input sequence can be used at most once. Numbers with the same digits in the same positions are counted only once, even if they can be obtained by choosing different elements of the sequence.

Input

The first line contains the number of digits N, where 3 ≤ N ≤ 50000. The second line contains N digits separated by spaces. Each input value is a digit from 0 to 9.

Output

Print one whole number — the number of different three-digit even numbers that can be made.

Example 1
Input
4
2 4 2 2
Output
4

The possible numbers are 222, 224, 242 and 422.

Example 2
Input
3
0 1 0
Output
1

The only possible three-digit even number is 100. A number that starts with zero is not considered a three-digit number.

Example 3
Input
3
4 1 2
Output
4

The possible numbers are 124, 142, 214 and 412.

Idea for the solution: Count how many times each digit from 0 to 9 appears. Then try all the combinations of the hundreds, tens and units digits. The hundreds digit must not be zero, and the units digit must be even. For each combination check whether the input sequence has enough occurrences of each digit.
TASK 11 2022/23 · 8th grade

The snail

Matrices Spiral movement Strings

Note: A task from round 2 of the qualifications, season 2022/23, for the 8th grade. Taken from takprog.petlja.org.

What you need to know first: Data in C++, Operators in C++, Branching, Loops, Strings and Arrays.

Optional: Dynamic array – vector.

Nemanja is six years old and has started learning to read and write. His uncle gave him a workbook for learning letters with an exercise called “The snail”. In the exercise you have to fill in an n × m table with the letters of a given sentence.

Filling starts in the top left corner. The letters are written to the right as far as the edge of the table, then down, then to the left, then up. When you reach the border of the filled part, you continue in the same circular way through the inside of the table until all the cells are filled.

Only letters are written into the table; spaces, punctuation marks and other characters are ignored. The uncle filled in the table, and Nemanja wants to know whether the sentence used to fill it contains the name of his favourite superhero.

Write a program that reconstructs the string obtained by reading the table along the same spiral path, and then searches it for the name of the superhero.

Table with letters filled in spiral order
Input

The first line contains two whole numbers n and m, where 2 ≤ n, m ≤ 50. They are the number of rows and the number of columns of the table.

Each of the next n lines contains m lowercase letters of the English alphabet, which represent the filled table. The last line contains the name of the superhero we are looking for — a string of at most 30 characters, made of lowercase letters of the English alphabet.

Output

Print the position in the reconstructed sentence where the name of the superhero starts. Positions are counted from 1. If the name does not appear, print -1. If it appears more than once, print the position of its first occurrence.

Example 1
Input
4 5
mnogo
jdrmv
aaneo
psmil
spajdrmen
Output
11

Reading the table in spiral order gives the string mnogovolimspajdrmena (Serbian for “I really love Spider-Man”). The name spajdrmen starts at position 11.

Example 2
Input
5 5
dalis
rnuui
ccudt
oivoi
adelg
crnaudovica
Output
-1

The reconstructed string does not contain crnaudovica (Serbian for “black widow”). The required string does not appear in the text.

Idea for the solution: Keep the borders of the unread part of the table: the top row, the bottom row, the left column and the right column. In each iteration read the top row from left to right, the right column from top to bottom, the bottom row from right to left and the left column from bottom to top. Check that the borders have not crossed, so that no cell is read twice. Finally use find to find the name of the superhero.
TASK 12 2022/23 · 8th grade

The smallest gain

Arrays Sorting Efficiency

Note: A task from round 2 of the qualifications, season 2022/23, for the 8th grade. Taken from takprog.petlja.org.

What you need to know first: Data in C++, Operators in C++, Branching, Loops, Arrays and Sorting.

Imagine a huge matrix filled with zeros. With each move (aᵢ, bᵢ, cᵢ), all the elements in the first aᵢ rows and the first bᵢ columns of the matrix are increased by cᵢ.

We need to find the smallest positive value in the matrix after all n moves.

Input

The first line of standard input contains the number of moves n (1 ≤ n ≤ 50000). Each of the next n lines contains three natural numbers aᵢ, bᵢ and cᵢ, separated by spaces. The values aᵢ and bᵢ are not greater than one billion, and the sum of all cᵢ does not exceed two billion.

Assume that the matrix has more than a billion rows and more than a billion columns.

Output

Print one whole number — the smallest positive value in the matrix.

Example 1
Input
4
2 3 1
4 5 2
1 2 1
4 5 1
Output
3

After all four moves, the matrix starts like this:

5 5 4 3 3 0 0 ...
4 4 4 3 3 0 0 ...
3 3 3 3 3 0 0 ...
3 3 3 3 3 0 0 ...
0 0 0 0 0 0 0 ...
...

The smallest positive value in the matrix is 3.

Example 2
Input
2
3 7 3
4 5 2
Output
2

After both moves, the matrix starts like this:

5 5 5 5 5 3 3 0 0 ...
5 5 5 5 5 3 3 0 0 ...
5 5 5 5 5 3 3 0 0 ...
2 2 2 2 2 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 ...
...

The smallest positive value in the matrix is 2.

Idea for the solution: The move (aᵢ, bᵢ, cᵢ) affects the cells whose row number is at most aᵢ and whose column number is at most bᵢ. That is why the value in the cell (aᵢ, bᵢ) is the sum of all cⱼ for which aⱼ ≥ aᵢ and bⱼ ≥ bᵢ. It is enough to calculate such a sum for every move and take the smallest. By sorting by aᵢ and using a Fenwick tree for the sums by bᵢ, all the necessary sums can be calculated in O(n log n).
TASK 13 2022/23 · 8th grade

Reversing

Strings Big numbers Palindromes

Note: A task from round 2 of the qualifications, season 2022/23, for the 8th grade. Taken from takprog.petlja.org.

What you need to know first: Data in C++, Operators in C++, Branching, Loops, Strings and Arrays.

Optional: Dynamic array – vector.

We are given a number N and the number of its reversals r. We need to print whether the number obtained after r consecutive reversals of N is equal to the starting number N.

Reversing a number gives the number written with the same digits, but in reverse order. For example, reversing 310 gives 13, and reversing 2227 gives 7222.

Input

The first line contains the number N, and the second the number r. Both numbers are non-negative whole numbers, and 0 ≤ N < 10⁵⁰⁰⁰⁰ and 0 ≤ r < 1 000 000.

In tests worth 60% of the points, 0 ≤ N < 200 000 and 0 ≤ r < 15.

Output

Print DA (yes) if the result after r reversals is equal to the starting number, and otherwise print NE (no); these are the original Serbian words checked by the tests on petlja.org.

Example 1
Input
1239999999999999999999321
787657
Output
DA
Example 2
Input
1000000020000000300000004
458698
Output
DA
Example 3
Input
10000000200000003000000040
2
Output
NE
Idea for the solution: We do not really have to do up to a million reversals. If r = 0, the number stays unchanged. If N = 0, it also stays the same for any number of reversals. If N ends with a zero and r is positive, the first reversal removes those trailing zeros, so the number can no longer be equal to the starting one. In the other cases reversing behaves like an ordinary change of the order of the characters: an even number of reversals gives back the starting number, and an odd number gives it back only if N is a palindrome.
TASK 14 Grade and season not stated

Who plays first

Arrays Sorting Conditions

What you need to know first: Data in C++, Operators in C++, Branching and Arrays.

Ana, Boban and Vlada want to play a board game. To decide the order of play, each of them throws a die. The one who got the largest number plays first, the one who got the next largest number plays second, and the one who got the smallest number plays third.

Write a program that prints the order of play based on the results of the dice throws.

Input

Three different numbers a, b and v are entered, separated by spaces. Each number is between 1 and 6. They are the results of the throws of Ana, Boban and Vlada, in that order.

Output

Print the letters A, B and V in the order in which the players play. The letters are printed without spaces.

Example 1
Input
2 6 3
Output
BVA

Boban got 6, Vlada 3 and Ana 2, so they play in the order B, V, A.

Example 2
Input
5 4 3
Output
ABV
Example 3
Input
2 1 4
Output
VAB
Idea for the solution: Store each person's throw together with their letter. Then sort the players by result from the largest to the smallest and print their letters in that order.
TASK 15

Overlapping digits

Note: A programming task. The grade is not stated in the statement provided.

What you need to know first: Data in C++, Operators in C++.

Mare and Paja are playing a video game and have reached a level where they have to solve a code. They realised that the code is a five-digit number that was written on a wall in the previous level of the game.

Neither of them can remember the whole number, but they remember parts of it. Mare remembered the first three digits, and Paja the last three.

Help them get to the next level by finding the required five-digit number. Assume that both of them remembered the digits correctly and that the input data are correct.

Input

The first line contains the three-digit number Mare remembered. The second line contains the three-digit number Paja remembered.

Output

Print the required five-digit number.

Example 1

Input
145
563
Output
14563

Explanation: Mare remembered the first three digits, so the number starts with the digits 145. Paja remembered the last three digits, so the number ends with the digits 563. The required number is 14563.

Example 2

Input
788
851
Output
78851
TASK 16

The home team's largest lead

Note: A programming task. The grade is not stated in the statement provided.

What you need to know first: Data in C++, Operators in C++, Branching in a program in C/C++, Loops.

We are given the score of a basketball game after each quarter. Find the largest possible lead of the home team during the game.

The scores are given after the end of each quarter. During a quarter, the home team may score its points before the away team scores its points in that quarter.

Input

Each of the four lines of standard input contains two non-negative whole numbers separated by a space.

Each line is the score after one quarter, in order. The first number is the number of points of the home team, and the second the number of points of the away team. No number is greater than 200.

Output

Print one whole number — the largest possible lead of the home team. If the home team could not have been in the lead at any moment, print 0.

Example 1

Input
21 18
39 40
63 54
78 77
Output
24

Reasoning: At one moment the score could have been 78:54, which is a home team lead of 24 points.

Example 2

Input
0 1
1 3
2 5
4 9
Output
0

Reasoning: The home team was never in the lead. The best score for it was at the start of the game, when the score was 0:0.

TASK 17

Changing school

Note: A programming task. The grade is not stated in the statement provided.

What you need to know first: Data in C++, Operators in C++, Branching in a program in C/C++.

At the end of the first year of secondary school, Ivana decided to move from the medical school to a grammar school. At the grammar school they decided to calculate her average mark only from the subjects that exist in both schools.

We know that \(k\) of Ivana's subjects from the medical school will not count towards her average at the grammar school. Find Ivana's new average if we know the total number of subjects she took at the medical school (\(n\)), the marks in the subjects that will not count, and her average so far \(p\).

Assume that Ivana passed the year, i.e. that she had no final mark of 1 (a fail).

The average of the marks \(a_1,a_2,\ldots,a_n\) is calculated as:

\((a_1+a_2+\cdots+a_n)/n\)

Input

The first line of standard input contains a natural number \(n\), and the second the number \(k\), where \(1 \leq k < n \leq 50\).

The next \(k\) lines contain Ivana's final marks in the subjects that will not count towards the new average. Each mark is a natural number from 2 to 5.

The last line contains the number \(p\), Ivana's average so far at the medical school, rounded to two decimal places.

Output

Print Ivana's new average, rounded to two decimal places.

Example 1

Input
3
1
4
4.33
Output
4.50

Example 2

Input
12
3
4
5
4
4.58
Output
4.67
TASK 18

An app PIN from the phone PIN

Note: A task from the qualifications for the 2024/25 school year. The statement provided gives the 8th grade.

What you need to know first: Data in C++, Operators in C++, Operators — examples.

A student needs to choose a four-digit PIN for a new app, but is not sure they will remember it. So they came up with the idea of calculating the new PIN from the PIN of their phone.

They will add up their phone PIN, the number obtained by swapping the first and last digits of that PIN, and the number obtained by swapping the two middle digits.

The new PIN is made of the last four digits of the sum obtained this way.

For example, if the phone PIN is 1234, the sum \(1234 + 4231 + 1324 = 6789\) is calculated, so the new PIN is 6789.

Input

A four-digit phone PIN is entered from standard input. The PIN may contain leading zeros.

Output

Print the four-digit app PIN, including leading zeros if there are any.

Example 1

Input
1234
Output
6789

Example 2

Input
9999
Output
9997

Explanation: The sum is \(9999 + 9999 + 9999 = 29997\), so the last four digits give 9997.

TASK 19

Horoscope

Note: A task from round 2 of the qualifications, season 2019/20. The grade is not confirmed in the statement provided.

Source: dms.rs/informatika-osnovne-skole

What you need to know first: Data in C++, Branching in a program in C/C++, Loops, Strings.

The unique master citizen number (JMBG) in Serbia is a sequence of 13 digits. The first two digits are the day, and the next two the month of birth. For example, if a JMBG starts with 0904, the person was born on 9 April, and if it starts with 3112, the person was born on 31 December.

Perica wants to count how many people on a list belong to each sign of the zodiac. For each sign the start and end dates are known:

  • ARIES — from 21 March to 20 April
  • TAURUS — from 21 April to 21 May
  • GEMINI — from 22 May to 21 June
  • CANCER — from 22 June to 22 July
  • LEO — from 23 July to 22 August
  • VIRGO — from 23 August to 22 September
  • LIBRA — from 23 September to 23 October
  • SCORPIO — from 24 October to 22 November
  • SAGITTARIUS — from 23 November to 21 December
  • CAPRICORN — from 22 December to 20 January
  • AQUARIUS — from 21 January to 19 February
  • PISCES — from 20 February to 20 March

Input

The first line contains the number \(N\) (\(1 \leq N \leq 200\)), the number of JMBGs on the list.

Each of the next \(N\) lines contains one JMBG of 13 digits, without spaces.

Output

Print 12 whole numbers, each on a separate line. The first number is the number of people born under Aries, the second under Taurus and so on, up to the twelfth number, which is the number of people born under Pisces.

Example 1

Input
5
1504279718463
0412622712918
1903326718649
0710262713307
0104646719372
Output
2
0
0
0
0
0
1
0
1
0
0
1
TASK 20

Artimogrifs

Note: A task from round 2 of the qualifications. The grade and season are not confirmed in the statement provided.

What you need to know first: Data in C++, Strings, Arrays.

To solve one well-known kind of arithmetic puzzle (a cryptarithm), the same letters have to be replaced by the same digits and different letters by different digits, so that we get a correct equation.

For example, for the puzzle SNEG + KRUG = SPORT one of the solutions is 1937 + 8627 = 10564. In this task we do not check whether the equation is correct, only whether the letters were replaced by digits properly.

For example, the offered “solution” 4832 + 5962 = 10794 is not valid, even though the equation is correct. The letter S was replaced by the digit 4 in one place and by the digit 1 in another. Also, the digit 4 corresponds to two different letters.

Write a program that checks whether the letters were replaced by digits properly.

Input

The first line contains a string that is an arithmetic puzzle. It may contain capital letters of the English alphabet, spaces, signs of mathematical operations and the equals sign. The total length of the string is not greater than 100, and the number of different letters is not greater than 10.

The second line contains a string that is the offered solution of the puzzle from the first line. The second string differs from the first only in that every letter is replaced by some digit.

Output

Print da (yes) if the replacement is correct, regardless of whether the equation is correct. Otherwise print ne (no). These are the original Serbian words checked by the tests.

Example 1

Input
SNEG + KRUG = SPORT
2341 + 8751 = 20679
Output
da

Example 2

Input
SNEG + KRUG = SPORT
4832 + 5962 = 10794
Output
ne
TASK 21

Two shelves

Competition: round 2 of the qualifications

Grade: grade not confirmed

Task statement

A shop sells two kinds of shelves whose lengths and prices are known. We want to put shelves along one wall so as to fill as much of its length as possible. If the same length of the wall can be filled in several ways, we choose the combination of shelves with the lowest total price.

Write a program that finds the greatest possible length of the wall that can be filled with shelves and the lowest price at which this can be done.

Input

The first line of standard input contains a whole number z (1 ≤ z ≤ 10⁶), the length of the wall. The next two lines contain the data for the kinds of shelves: length and price. The length of each shelf is a natural number between 1 and 10⁶, and the price is a natural number between 1 and 1000.

Output

Print the greatest possible length of the wall that can be filled with shelves and the lowest total price for that length, separated by one space.

Example

Input

24
3 10
5 8

Output

24 54

One way to fill all 24 units of the wall is to use three shelves of length 3 and three shelves of length 5. Their total price is 3 · 10 + 3 · 8 = 54.

Idea of the solution

For each length of the wall from 0 to z we calculate the lowest price at which that length can be filled completely. A length can be reached by adding a shelf of the first or the second kind to an already filled part of the wall. When we have calculated all the possibilities, we look for the greatest reachable length that is not greater than z.

TASK 22

The largest number from the given digits

Competition: round 3 of the qualifications

Grade: grade not confirmed

Task statement

For a given natural number we need to print the largest number that can be written using the same digits, where each digit is used the same number of times as in the starting number.

Input

The first line of standard input contains the number N. The number can have at most 30 digits.

Output

Print the largest number that can be made from the digits of N.

Example 1

Input

90123

Output

93210

Example 2

Input

777

Output

777

Idea of the solution

For the number to be as large as possible, its digits should be arranged from the largest to the smallest. We read the number as a string because it can have 30 digits, which is more than fits in the standard integer types.

TASK 23

Risk

Competition: round 3 of the qualifications

Grade: grade not confirmed

Task statement

During a game of Risk, the attacker and the defender fight battles by throwing dice. Each player has three dice, but depending on the current state of the game they can throw one, two or three dice.

After the throw, each player's dice are arranged from the largest to the smallest. Then the strongest dice are compared, then the second strongest, and then the weakest dice, if both players have them. If one player threw fewer dice, the other player's weaker dice that are not compared are ignored.

If the attacker has a strictly larger number on a die than the defender, the defender loses one point. Otherwise, the attacker loses one point. So in the case of equal values the attacker loses the point.

Write a program that finds how many points the attacker loses and how many the defender loses.

Input

The first line contains the number of dice thrown by the attacker (1, 2 or 3). The second line contains the values of the attacker's dice, from 1 to 6. The third line contains the number of dice thrown by the defender (1, 2 or 3), and the fourth line the values of the defender's dice.

Output

Print the number of points the attacker loses and the number of points the defender loses, separated by one space.

Example

Input

3
5 6 3
3
5 3 4

Output

1 2

The attacker's dice are 6, 5 and 3, and the defender's 5, 4 and 3. The attacker wins the first comparison, because 6 is greater than 5. In the second comparison 5 is greater than 4, so the defender loses another point. In the third comparison the values are equal, so the attacker loses one point.

Idea of the solution

We sort both arrays of dice in descending order. We repeat the comparison as many times as the number of dice of the player who threw fewer. For each comparison we increase the number of points lost by the corresponding player.

TASK 24

ATP winner

Competition: round 3 of the qualifications

Grade: grade not confirmed

Task statement

During the year many tennis tournaments are held at which players win points. The points won at different tournaments are added up, and at the end of the year the players are ranked by their total number of points.

Write a program that, from the results of all the tournaments, finds the player with the most points and prints his surname and total number of points. Assume that the winner has strictly more points than all the other players.

Input

The first line of standard input contains the number n. Each of the next n lines contains a player's surname and the number of points won at one tournament. The same player can appear more than once.

Output

Print the winner's surname and his total number of points, separated by one space.

Example 1

Input

9
Djokovic 1000
Nadal 800
Federer 600
Nadal 1000
Federer 800
Djokovic 600
Djokovic 1000
Cicipas 800
Nadal 600

Output

Djokovic 2600

Example 2

Input

6
Zverev 1000
Cicipas 800
Medvedev 700
Thiem 1000
Cicipas 800
Medvedev 600

Output

Cicipas 1600

Idea of the solution

For each player we keep the total sum of points. When we read the result of a tournament, we add the points won to that player's sum. After all the lines have been processed, we find the player with the largest total.