question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the database.It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database.In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. | Input: ['3 4', '1 10 10 100', '1 1 1 100', '10 100 1 100', ''] Output:['Yes', '1 10 1 100', ''] | [
0,
2
] |
You are given three integers a, b, k.Find two binary integers x and y (x >= y) such that both x and y consist of a zeroes and b ones; x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. | Input: ['4 2 3', ''] Output:['Yes', '101000', '100001', ''] | [
2,
3
] |
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: s of length n and t of length m.A sequence p_1, p_2, ..., p_m, where 1 <=q p_1 < p_2 < ... < p_m <=q n, is called beautiful, if s_{p_i} = t_i for all i from 1 to m. The width of a sequence is defined as \max\limits_{1 <= i < m} <=ft(p_{i + 1} - p_i\right).Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings s and t there is at least one beautiful sequence. | Input: ['5 3', 'abbbc', 'abc', ''] Output:['3', ''] | [
2,
4
] |
You have a deck of n cards, and you'd like to reorder it to a new one.Each card has a value between 1 and n equal to p_i. All p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p_1 stands for the bottom card, p_n is the top card. In each step you pick some integer k > 0, take the top k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as \sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}.Given the original deck, output the deck with maximum possible order you can make using the operation above. | Input: ['4', '4', '1 2 3 4', '5', '1 5 2 4 3', '6', '4 2 5 3 6 1', '1', '1', ''] Output:['4 3 2 1', '5 2 4 3 1', '6 1 5 3 4 2', '1', ''] | [
2,
3
] |
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, ... minutes after the start time, the second one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, ... minutes.You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool. | Input: ['4', '9 5 4 8', '2 6 10 9', '10 2 5 10', '10 9 9 9', ''] Output:['1', '4', '0', '8', ''] | [
3
] |
After realizing that Zookeeper is just a duck, the animals have overthrown Zookeeper. They now have to decide a new ruler among themselves through a fighting tournament of the following format:Initially, animal 0 is king, while everyone else queues up with animal 1 at the front of the queue and animal n-1 at the back. The animal at the front of the queue will challenge the king to a fight, and the animal with greater strength will win the fight. The winner will become king, while the loser joins the back of the queue.An animal who wins 3 times consecutively will be crowned ruler for the whole zoo. The strength of each animal depends on how many consecutive fights he won. Animal i has strength A_i with 0 consecutive win, B_i with 1 consecutive win, and C_i with 2 consecutive wins. Initially, everyone has 0 consecutive win.For all animals, A_i > B_i and C_i > B_i. Also, the values of A_i, B_i, C_i are distinct (all 3n values are pairwise different). In other words, an animal who is not a king has strength A_i. A king usually has a strength of B_i or C_i. The exception is on the first turn, the first king (animal 0) has strength A_i.Who is the new ruler, and after how many fights? Or will it end up that animals fight forever with no one ending up as ruler? | Input: ['4', '5 1 2', '10 8 11', '9 0 3', '7 4 6', ''] Output:['-1 -1'] | [
0
] |
There are n coins labeled from 1 to n. Initially, coin c_i is on position i and is facing upwards ((c_1, c_2, ..., c_n) is a permutation of numbers from 1 to n). You can do some operations on these coins. In one operation, you can do the following:Choose 2 distinct indices i and j.Then, swap the coins on positions i and j.Then, flip both coins on positions i and j. (If they are initially faced up, they will be faced down after the operation and vice versa)Construct a sequence of at most n+1 operations such that after performing all these operations the coin i will be on position i at the end, facing up.Note that you do not need to minimize the number of operations. | Input: ['3', '2 1 3', ''] Output:['3', '1 3', '3 2', '3 1', ''] | [
3
] |
This is an interactive problem.Kochiya Sanae is playing with magnets. Realizing that some of those magnets are demagnetized, she is curious to find them out.There are n magnets, which can be of the following 3 types: N S - β these magnets are demagnetized. Note that you don't know the types of these magnets beforehand.You have a machine which can measure the force between the magnets, and you can use it at most n+\lfloor \log_2n\rfloor times.You can put some magnets to the left part of the machine and some to the right part of the machine, and launch the machine. Obviously, you can put one magnet to at most one side (you don't have to put all magnets). You can put the same magnet in different queries.Then the machine will tell the force these magnets produce. Formally, let n_1,s_1 be the number of N and S magnets correspondently on the left and n_2,s_2 β on the right. Then the force between them would be n_1n_2+s_1s_2-n_1s_2-n_2s_1. Please note that the force is a signed value.However, when the absolute value of the force is strictly larger than n, the machine will crash into pieces.You need to find all magnets of type - (all demagnetized ones), without breaking the machine.Note that the interactor is not adaptive. The types of the magnets are fixed before the start of the interaction and do not change with queries.It is guaranteed that there are at least 2 magnets whose type is not -, and at least 1 magnet of type -. | Input: ['1', '4', '', '', '', '0', '', '', '', '1', '', '', '', '0', '', '', '', '0', '', ''] Output:['', '', '? 1 1', '3', '4', '', '? 1 2', '1', '2 3', '', '? 1 1', '1', '4', '', '? 1 1', '1', '3', '', '! 2 3 4', ''] | [
4
] |
Let F_k denote the k-th term of Fibonacci sequence, defined as below: F_0 = F_1 = 1 for any integer n >=q 0, F_{n+2} = F_{n+1} + F_nYou are given a tree with n vertices. Recall that a tree is a connected undirected graph without cycles.We call a tree a Fib-tree, if its number of vertices equals F_k for some k, and at least one of the following conditions holds: The tree consists of only 1 vertex; You can divide it into two Fib-trees by removing some edge of the tree. Determine whether the given tree is a Fib-tree or not. | Input: ['3', '1 2', '2 3', ''] Output:['YES', ''] | [
0
] |
There is a new attraction in Singapore Zoo: The Infinite Zoo.The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,.... There is a directed edge from vertex u to vertex u+v if and only if u\&v=v, where \& denotes the bitwise AND operation. There are no other edges in the graph.Zookeeper has q queries. In the i-th query she will ask you if she can travel from vertex u_i to vertex v_i by going through directed edges. | Input: ['5', '1 4', '3 6', '1 6', '6 2', '5 5', ''] Output:['YES', 'YES', 'NO', 'NO', 'YES', ''] | [
2,
3
] |
There is a trampoline park with n trampolines in a line. The i-th of which has strength S_i.Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice. If at the moment Pekora jumps on trampoline i, the trampoline will launch her to position i + S_i, and S_i will become equal to \max(S_i-1,1). In other words, S_i will decrease by 1, except of the case S_i=1, when S_i will remain equal to 1. If there is no trampoline in position i + S_i, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position i + S_i by the same rule as above.Pekora can't stop jumping during the pass until she lands at the position larger than n (in which there is no trampoline). Poor Pekora!Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all S_i to 1. What is the minimum number of passes she needs to reduce all S_i to 1? | Input: ['3', '7', '1 4 2 2 2 2 2', '2', '2 3', '5', '1 1 1 1 1', ''] Output:['4', '3', '0', ''] | [
0,
2
] |
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1: Let's denote the node in the row i and column j by (i, j).Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below: If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently. If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently. Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1). Refer to the picture above for a better understanding. Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles. | Input: ['3', '2 3 4', '2 2', '2 3 4', '3 2', '2 4 3', '3 2', ''] Output:['7', '3', '3', ''] | [
0,
3
] |
You are given an array a consisting of n integers. Initially all elements of a are either 0 or 1. You need to process q queries of two kinds: 1 x : Assign to a_x the value 1 - a_x. 2 k : Print the k-th largest value of the array. As a reminder, k-th largest value of the array b is defined as following: Sort the array in the non-increasing order, return k-th element from it. For example, the second largest element in array [0, 1, 0, 1] is 1, as after sorting in non-increasing order it becomes [1, 1, 0, 0], and the second element in this array is equal to 1. | Input: ['5 5', '1 1 0 1 0', '2 3', '1 2', '2 3', '2 1', '2 5', ''] Output:['1', '0', '1', '0', ''] | [
0,
2
] |
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with n integers written on it.Polycarp wrote the numbers from the disk into the a array. It turned out that the drive works according to the following algorithm: the drive takes one positive number x as input and puts a pointer to the first element of the a array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the a array, the last element is again followed by the first one; as soon as the sum is at least x, the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you m questions. To answer the i-th of them, you need to find how many seconds the drive will work if you give it x_i as input. Please note that in some cases the drive can work infinitely.For example, if n=3, m=3, a=[1, -3, 4] and x=[1, 5, 2], then the answers to the questions are as follows: the answer to the first query is 0 because the drive initially points to the first item and the initial sum is 1. the answer to the second query is 6, the drive will spin the disk completely twice and the amount becomes 1+(-3)+4+1+(-3)+4+1=5. the answer to the third query is 2, the amount is 1+(-3)+4=2. | Input: ['3', '3 3', '1 -3 4', '1 5 2', '2 2', '-2 0', '1 2', '2 2', '0 1', '1 2', ''] Output:['0 6 2 ', '-1 -1 ', '1 3 ', ''] | [
3,
4
] |
Polycarp was gifted an array a of length n. Polycarp considers an array beautiful if there exists a number C, such that each number in the array occurs either zero or C times. Polycarp wants to remove some elements from the array a to make it beautiful.For example, if n=6 and a = [1, 3, 2, 1, 4, 2], then the following options are possible to make the array a array beautiful: Polycarp removes elements at positions 2 and 5, array a becomes equal to [1, 2, 1, 2]; Polycarp removes elements at positions 1 and 6, array a becomes equal to [3, 2, 1, 4]; Polycarp removes elements at positions 1, 2 and 6, array a becomes equal to [2, 1, 4]; Help Polycarp determine the minimum number of elements to remove from the array a to make it beautiful. | Input: ['3', '6', '1 3 2 1 4 2', '4', '100 100 4 100', '8', '1 2 3 3 3 2 6 6', ''] Output:['2', '1', '2', ''] | [
2,
3,
4
] |
A championship is held in Berland, in which n players participate. The player with the number i has a_i (a_i >= 1) tokens.The championship consists of n-1 games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship.All random decisions that are made during the championship are made equally probable and independently.For example, if n=4, a = [1, 2, 4, 3], then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now a = [0, 2, 4, 4]; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now a = [0, 2, 8, 0]; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now a = [0, 0, 10, 0]; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. | Input: ['2', '4', '1 2 4 3', '5', '1 1 1 1 1', ''] Output:['3', '2 3 4 ', '5', '1 2 3 4 5 ', ''] | [
2,
4
] |
You are given a positive integer x. Check whether the number x is representable as the sum of the cubes of two positive integers.Formally, you need to check if there are two integers a and b (1 <= a, b) such that a^3+b^3=x.For example, if x = 35, then the numbers a=2 and b=3 are suitable (2^3+3^3=8+27=35). If x=4, then no pair of numbers a and b is suitable. | Input: ['7', '1', '2', '4', '34', '35', '16', '703657519796', ''] Output:['NO', 'YES', 'NO', 'NO', 'YES', 'YES', 'YES', ''] | [
0,
0,
3,
4
] |
You are given a number n (divisible by 3) and an array a[1 ... n]. In one move, you can increase any of the array elements by one. Formally, you choose the index i (1 <= i <= n) and replace a_i with a_i + 1. You can choose the same index i multiple times for different moves.Let's denote by c_0, c_1 and c_2 the number of numbers from the array a that have remainders 0, 1 and 2 when divided by the number 3, respectively. Let's say that the array a has balanced remainders if c_0, c_1 and c_2 are equal.For example, if n = 6 and a = [0, 2, 5, 5, 4, 8], then the following sequence of moves is possible: initially c_0 = 1, c_1 = 1 and c_2 = 4, these values are not equal to each other. Let's increase a_3, now the array a = [0, 2, 6, 5, 4, 8]; c_0 = 2, c_1 = 1 and c_2 = 3, these values are not equal. Let's increase a_6, now the array a = [0, 2, 6, 5, 4, 9]; c_0 = 3, c_1 = 1 and c_2 = 2, these values are not equal. Let's increase a_1, now the array a = [1, 2, 6, 5, 4, 9]; c_0 = 2, c_1 = 2 and c_2 = 2, these values are equal to each other, which means that the array a has balanced remainders. Find the minimum number of moves needed to make the array a have balanced remainders. | Input: ['4', '6', '0 2 5 5 4 8', '6', '2 0 2 1 0 0', '9', '7 1 3 4 2 10 3 9 6', '6', '0 1 2 3 4 5', ''] Output:['3', '1', '3', '0', ''] | [
0,
3
] |
Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 <= i <= n-1), this condition must be satisfied: \frac{\max(a[i], a[i+1])}{\min(a[i], a[i+1])} <= 2For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense.You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added.For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{\textbf{3}},\underline{\textbf{5}},10,\underline{\textbf{6}},\underline{\textbf{4}},\underline{\textbf{2}},1] (there are other ways to build such a). | Input: ['6', '4', '4 2 10 1', '2', '1 3', '2', '6 1', '3', '1 4 2', '5', '1 2 3 4 3', '12', '4 31 25 50 30 20 34 46 42 16 15 16', ''] Output:['5', '1', '2', '1', '0', '3', ''] | [
2,
3
] |
Your friend is running a flower shop. In order to be prepared for the next holidays (when, as usual, sales skyrocket) she asked you to write her a special program that will help to analyze the stocks she has.There are n different types of flowers she can order and each flower of the type i costs w_i. The last holidays were a great success, she sold all flowers she had, so right now all her stocks are empty.From this point, she starts routine operations of ordering and selling flowers, while trying to analyze what she has at hand. All of this can be represented as m queries of three types: "1 i c" β she bought c flowers of type i; "2 i c" β she disposed of c flowers of type i; "3 l r k" β how many variants of bouquets she can make using only flowers of types l, l + 1, ..., r with the total cost no more than k. For simplicity, you can think that a bouquet is a multiset of flowers, and two bouquets are different if they are different as multisets. The cost of a bouquet is the sum of all flowers it has. Help your friend and write the program that can process all these queries. | Input: ['5 12', '1 2 3 2 1', '1 1 5', '1 2 3', '1 3 1', '3 1 5 10', '3 4 5 100', '1 4 4', '1 5 1', '3 2 5 7', '3 1 1 3', '3 1 5 100', '2 1 5', '3 1 5 100', ''] Output:['40', '0', '28', '3', '479', '79', ''] | [
3
] |
You are given n integers, each integer is from 1 to n, all of them are pairwise distinct. You have to paint them red and blue (each integer should have exactly one color).The cost of painting is the number of pairs (x, y) such that y \bmod x = 0, y is red and x is blue.For each k \in [1, n], calculate the maximum cost of painting if exactly k integers should have a red color. | Input: ['6', ''] Output:['3 5 6 6 5 0', ''] | [
2
] |
Recently, cryptocurrencies have become increasingly popular. Ann decided that it was about time she started mining the Dogecoin cryptocurrency. Ann's computer is not very powerful, so Ann mines exactly 1 Dogecoin per day. On the Internet, there are forecasts of the cost of 1 dogecoin for the next n days. Every day, Ann can sell any amount of dogecoin that she currently has. Note that if Ann mined Dogecoin on the i-th day, then she can sell it on the same day.Ann has not yet decided when to start mining. She has prepared q possible plans and for each of them wants to know the maximum profit that she can get. Each of the plans is described by two numbers l and r β the first and last days of mining. Note that Ann should not have any Dogecoin left after the r-th day. | Input: ['5', '4 1 2 3 2', '4', '1 5', '2 4', '3 5', '5 5', ''] Output:['15 9 8 2', ''] | [
4
] |
Polycarp has decided to do a problemsolving marathon. He wants to solve s problems in n days. Let a_i be the number of problems he solves during the i-th day. He wants to find a distribution of problems into days such that: a_i is an integer value for all i from 1 to n; a_i >= 1 for all i from 1 to n; a_{i + 1} >= a_i for all i from 1 to n-1; a_{i + 1} <= 2 \cdot a_i for all i from 1 to n-1; a_n is maximized. Note that a_1 can be arbitrarily large.What is the largest value of a_n Polycarp can obtain? | Input: ['3', '1 15', '3 9', '2 6', ''] Output:['15', '4', '4', ''] | [
2,
4
] |
There is a street that can be represented as an array of length n.There are two policemen patrolling a street: the first one is standing at the point x of the street and the second one is standing at the point y of the street.During one minute, both policemen can decide what to do (independently): move left (if the current position is greater than 1), move right (if the current position is less than n), or do nothing.The street is considered clear if each point of the street is visited by at least one policeman.Your task is to find the minimum number of minutes the policemen need to visit each point of the street (again, each point should be visited by at least one of them).You have to answer t independent test cases. | Input: ['6', '4 1 2', '7 7 1', '10 2 6', '8 5 2', '2 1 2', '20 4 14', ''] Output:['2', '3', '5', '4', '0', '12', ''] | [
0,
3,
4
] |
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); bracket sequences ")(", "(" and ")" are not. You are given a string s, which is an RBS. You can apply any number of operations to this string. Each operation can have one of the following types: choose some non-empty prefix of s and remove it from s, so s is still an RBS. For example, we can apply this operation as follows: "(())()(())()()" \to "()()" (the first 10 characters are removed); choose some contiguous non-empty substring of s and remove it from s, so s is still an RBS. For example, we can apply this operation as follows: "(())()(())()()" \to "(())()()()" (the characters from the 7-th to the 10-th are removed). The operation 2 can be applied at most k times. Calculate the maximum number of operations you can apply until s becomes empty. | Input: ['3', '12 2', '(()())((()))', '6 3', '()()()', '8 1', '(((())))', ''] Output:['4', '3', '2', ''] | [
2
] |
You are given two positive (greater than zero) integers x and y. There is a variable k initially set to 0.You can perform the following two types of operations: add 1 to k (i.βe. assign k := k + 1); add x \cdot 10^{p} to k for some non-negative p (i.βe. assign k := k + x \cdot 10^{p} for some p >= 0). Find the minimum number of operations described above to set the value of k to y. | Input: ['3', '2 7', '3 42', '25 1337', ''] Output:['4', '5', '20', ''] | [
3
] |
You have c_1 letters 'a', c_2 letters 'b', ..., c_{26} letters 'z'. You want to build a beautiful string of length n from them (obviously, you cannot use the i-th letter more than c_i times). Each c_i is greater than \frac{n}{3}.A string is called beautiful if there are no palindromic contiguous substrings of odd length greater than 1 in it. For example, the string "abacaba" is not beautiful, it has several palindromic substrings of odd length greater than 1 (for example, "aca"). Another example: the string "abcaa" is beautiful.Calculate the number of different strings you can build, and print the answer modulo 998244353. | Input: ['4', '2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2', ''] Output:['422500', ''] | [
3
] |
You are given a positive (greater than zero) integer n.You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. Among all possible representations, you have to find the one that uses the minimum number of ones in total. | Input: ['24', ''] Output:['6', ''] | [
2
] |
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.There are n_1 different types of first courses Ivan can buy (the i-th of them costs a_i coins), n_2 different types of second courses (the i-th of them costs b_i coins), n_3 different types of drinks (the i-th of them costs c_i coins) and n_4 different types of desserts (the i-th of them costs d_i coins).Some dishes don't go well with each other. There are m_1 pairs of first courses and second courses that don't go well with each other, m_2 pairs of second courses and drinks, and m_3 pairs of drinks and desserts that don't go well with each other.Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option! | Input: ['4 3 2 1', '1 2 3 4', '5 6 7', '8 9', '10', '2', '1 2', '1 1', '2', '3 1', '3 2', '1', '1 1', ''] Output:['26', ''] | [
0,
2
] |
A Pythagorean triple is a triple of integer numbers (a, b, c) such that it is possible to form a right triangle with the lengths of the first cathetus, the second cathetus and the hypotenuse equal to a, b and c, respectively. An example of the Pythagorean triple is (3, 4, 5).Vasya studies the properties of right triangles, and he uses a formula that determines if some triple of integers is Pythagorean. Unfortunately, he has forgotten the exact formula; he remembers only that the formula was some equation with squares. So, he came up with the following formula: c = a^2 - b.Obviously, this is not the right formula to check if a triple of numbers is Pythagorean. But, to Vasya's surprise, it actually worked on the triple (3, 4, 5): 5 = 3^2 - 4, so, according to Vasya's formula, it is a Pythagorean triple.When Vasya found the right formula (and understood that his formula is wrong), he wondered: how many are there triples of integers (a, b, c) with 1 <= a <= b <= c <= n such that they are Pythagorean both according to his formula and the real definition? He asked you to count these triples. | Input: ['3', '3', '6', '9', ''] Output:['0', '1', '1', ''] | [
0,
3,
4
] |
A big football championship will occur soon! n teams will compete in it, and each pair of teams will play exactly one game against each other.There are two possible outcomes of a game: the game may result in a tie, then both teams get 1 point; one team might win in a game, then the winning team gets 3 points and the losing team gets 0 points. The score of a team is the number of points it gained during all games that it played.You are interested in a hypothetical situation when all teams get the same score at the end of the championship. A simple example of that situation is when all games result in ties, but you want to minimize the number of ties as well.Your task is to describe a situation (choose the result of each game) so that all teams get the same score, and the number of ties is the minimum possible. | Input: ['2', '2', '3', ''] Output:['0 ', '1 -1 1 ', ''] | [
0,
2,
3
] |
Suppose you are living with two cats: A and B. There are n napping spots where both cats usually sleep.Your cats like to sleep and also like all these spots, so they change napping spot each hour cyclically: Cat A changes its napping place in order: n, n - 1, n - 2, ..., 3, 2, 1, n, n - 1, ... In other words, at the first hour it's on the spot n and then goes in decreasing order cyclically; Cat B changes its napping place in order: 1, 2, 3, ..., n - 1, n, 1, 2, ... In other words, at the first hour it's on the spot 1 and then goes in increasing order cyclically. The cat B is much younger, so they have a strict hierarchy: A and B don't lie together. In other words, if both cats'd like to go in spot x then the A takes this place and B moves to the next place in its order (if x < n then to x + 1, but if x = n then to 1). Cat B follows his order, so it won't return to the skipped spot x after A frees it, but will move to the spot x + 2 and so on.Calculate, where cat B will be at hour k? | Input: ['7', '2 1', '2 2', '3 1', '3 2', '3 3', '5 5', '69 1337', ''] Output:['1', '2', '1', '3', '2', '2', '65', ''] | [
3
] |
There are n cities and m bidirectional roads in the country. The roads in the country form an undirected weighted graph. The graph is not guaranteed to be connected. Each road has it's own parameter w. You can travel through the roads, but the government made a new law: you can only go through two roads at a time (go from city a to city b and then from city b to city c) and you will have to pay (w_{ab} + w_{bc})^2 money to go through those roads. Find out whether it is possible to travel from city 1 to every other city t and what's the minimum amount of money you need to get from 1 to t. | Input: ['5 6', '1 2 3', '2 3 4', '3 4 5', '4 5 6', '1 5 1', '2 4 2', ''] Output:['0 98 49 25 114 '] | [
0,
4
] |
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.A median in an array of length n is an element which occupies position number \lfloor \frac{n + 1}{2} \rfloor after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},...,a_r for some 1 <=q l <=q r <=q n, its length is r - l + 1. | Input: ['5 3', '1 2 3 2 1', ''] Output:['2'] | [
4
] |
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array a of n different numbers. In one query you can ask the position of the second maximum element in a subsegment a[l..r]. Find the position of the maximum element in the array in no more than 20 queries.A subsegment a[l..r] is all the elements a_l, a_{l + 1}, ..., a_r. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array. | Input: ['5', '', '3', '', '4', '', ''] Output:['? 1 5', '', '? 4 5', '', '! 1'] | [
4
] |
The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array a of n different numbers. In one query you can ask the position of the second maximum element in a subsegment a[l..r]. Find the position of the maximum element in the array in no more than 40 queries.A subsegment a[l..r] is all the elements a_l, a_{l + 1}, ..., a_r. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array. | Input: ['5', '', '3', '', '4', '', ''] Output:['? 1 5', '', '? 4 5', '', '! 1'] | [
4
] |
You and your friends live in n houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points (x_1, y_1) and (x_2, y_2) is |x_1 - x_2| + |y_1 - y_2|, where |x| is the absolute value of x. | Input: ['6', '3', '0 0', '2 0', '1 2', '4', '1 0', '0 2', '2 3', '3 1', '4', '0 0', '0 1', '1 0', '1 1', '2', '0 0', '1 1', '2', '0 0', '2 0', '2', '0 0', '0 0', ''] Output:['1', '4', '4', '4', '3', '1', ''] | [
4
] |
You have n stacks of blocks. The i-th stack contains h_i blocks and it's height is the number of blocks in it. In one move you can take a block from the i-th stack (if there is at least one block) and put it to the i + 1-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains n: stacks don't disappear when they have 0 blocks. | Input: ['6', '2', '1 2', '2', '1 0', '3', '4 4 4', '2', '0 0', '3', '0 1 0', '4', '1000000000 1000000000 1000000000 1000000000', ''] Output:['YES', 'YES', 'YES', 'NO', 'NO', 'YES', ''] | [
2
] |
You are given n - 1 integers a_2, ..., a_n and a tree with n vertices rooted at vertex 1. The leaves are all at the same distance d from the root. Recall that a tree is a connected undirected graph without cycles. The distance between two vertices is the number of edges on the simple path between them. All non-root vertices with degree 1 are leaves. If vertices s and f are connected by an edge and the distance of f from the root is greater than the distance of s from the root, then f is called a child of s.Initially, there are a red coin and a blue coin on the vertex 1. Let r be the vertex where the red coin is and let b be the vertex where the blue coin is. You should make d moves. A move consists of three steps: Move the red coin to any child of r. Move the blue coin to any vertex b' such that dist(1, b') = dist(1, b) + 1. Here dist(x, y) indicates the length of the simple path between x and y. Note that b and b' are not necessarily connected by an edge. You can optionally swap the two coins (or skip this step). Note that r and b can be equal at any time, and there is no number written on the root.After each move, you gain |a_r - a_b| points. What's the maximum number of points you can gain after d moves? | Input: ['4', '14', '1 1 1 2 3 4 4 5 5 6 7 8 8', '2 3 7 7 6 9 5 9 7 3 6 6 5', '6', '1 2 2 3 4', '32 78 69 5 41', '15', '1 15 1 10 4 9 11 2 4 1 8 6 10 11', '62 13 12 43 39 65 42 86 25 38 19 19 43 62', '15', '11 2 7 6 9 8 10 1 1 1 5 3 15 2', '50 19 30 35 9 45 13 24 8 44 16 26 10 40', ''] Output:['14', '45', '163', '123', ''] | [
2
] |
You are given a matrix a consisting of positive integers. It has n rows and m columns.Construct a matrix b consisting of positive integers. It should have the same size as a, and the following conditions should be met: 1 <= b_{i,j} <= 10^6; b_{i,j} is a multiple of a_{i,j}; the absolute value of the difference between numbers in any adjacent pair of cells (two cells that share the same side) in b is equal to k^4 for some integer k >= 1 (k is not necessarily the same for all pairs, it is own for each pair). We can show that the answer always exists. | Input: ['2 2', '1 2', '2 3', ''] Output:['1 2', '2 3', ''] | [
3
] |
A pair of positive integers (a,b) is called special if \lfloor \frac{a}{b} \rfloor = a \bmod b. Here, \lfloor \frac{a}{b} \rfloor is the result of the integer division between a and b, while a \bmod b is its remainder.You are given two integers x and y. Find the number of special pairs (a,b) such that 1<=q a <=q x and 1 <=q b <=q y. | Input: ['9', '3 4', '2 100', '4 3', '50 3', '12 4', '69 420', '12345 6789', '123456 789', '12345678 9', ''] Output:['1', '0', '2', '3', '5', '141', '53384', '160909', '36', ''] | [
0,
3,
4
] |
Given a positive integer k, two arrays are called k-similar if: they are strictly increasing; they have the same length; all their elements are positive integers between 1 and k (inclusive); they differ in exactly one position. You are given an integer k, a strictly increasing array a and q queries. For each query, you are given two integers l_i <=q r_i. Your task is to find how many arrays b exist, such that b is k-similar to array [a_{l_i},a_{l_i+1}...,a_{r_i}]. | Input: ['4 2 5', '1 2 4 5', '2 3', '3 4', ''] Output:['4', '3', ''] | [
3
] |
You have two positive integers a and b.You can perform two kinds of operations: a = \lfloor \frac{a}{b} \rfloor (replace a with the integer part of the division between a and b) b=b+1 (increase b by 1) Find the minimum number of operations required to make a=0. | Input: ['6', '9 2', '1337 1', '1 1', '50000000 4', '991026972 997', '1234 5678', ''] Output:['4', '9', '2', '12', '3', '1', ''] | [
0,
2,
3
] |
Gustaw is the chief bank manager in a huge bank. He has unlimited access to the database system of the bank, in a few clicks he can move any amount of money from the bank's reserves to his own private account. However, the bank uses some fancy AI fraud detection system that makes stealing more difficult.Gustaw knows that the anti-fraud system just detects any operation that exceeds some fixed limit M euros and these operations are checked manually by a number of clerks. Thus, any fraud operation exceeding this limit is detected, while any smaller operation gets unnoticed.Gustaw doesn't know the limit M and wants to find it out. In one operation, he can choose some integer X and try to move X euros from the bank's reserves to his own account. Then, the following happens. If X <= M, the operation is unnoticed and Gustaw's account balance raises by X euros. Otherwise, if X > M, the fraud is detected and cancelled. Moreover, Gustaw has to pay X euros from his own account as a fine. If he has less than X euros on the account, he is fired and taken to the police. Initially Gustaw has 1 euro on his account. Help him find the exact value of M in no more than 105 operations without getting him fired. | Input: ['1', '', 'Lucky!', '', 'Lucky!', '', 'Lucky!', '', 'Lucky!', '', 'Lucky!', '', 'Fraudster!', ''] Output:['? 1', '', '? 2', '', '? 3', '', '? 4', '', '? 5', '', '? 6', '', '! 5', ''] | [
4
] |
Aleksey has n friends. He is also on a vacation right now, so he has m days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these m days.On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than <=ft\lceil\dfrac{m}{2}\right\rceil times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone.Help him to choose teammates so that nobody is chosen strictly more than <=ft\lceil\dfrac{m}{2}\right\rceil times. | Input: ['2', '4 6', '1 1', '2 1 2', '3 1 2 3', '4 1 2 3 4', '2 2 3', '1 3', '2 2', '1 1', '1 1', ''] Output:['YES', '1 2 1 1 2 3 ', 'NO', ''] | [
0,
2
] |
For the first place at the competition, Alex won many arrays of integers and was assured that these arrays are very expensive. After the award ceremony Alex decided to sell them. There is a rule in arrays pawnshop: you can sell array only if it can be compressed to a generator.This generator takes four non-negative numbers n, m, c, s. n and m must be positive, s non-negative and for c it must be true that 0 <=q c < m. The array a of length n is created according to the following rules: a_1 = s \bmod m, here x \bmod y denotes remainder of the division of x by y; a_i = (a_{i-1} + c) \bmod m for all i such that 1 < i <= n. For example, if n = 5, m = 7, c = 4, and s = 10, then a = [3, 0, 4, 1, 5].Price of such an array is the value of m in this generator.Alex has a question: how much money he can get for each of the arrays. Please, help him to understand for every array whether there exist four numbers n, m, c, s that generate this array. If yes, then maximize m. | Input: ['6', '6', '1 9 17 6 14 3', '3', '4 2 2', '3', '7 3 4', '3', '2 2 4', '5', '0 1000000000 0 1000000000 0', '2', '1 1', ''] Output:['19 8', '-1', '-1', '-1', '2000000000 1000000000', '0', ''] | [
3
] |
Michael is accused of violating the social distancing rules and creating a risk of spreading coronavirus. He is now sent to prison. Luckily, Michael knows exactly what the prison looks like from the inside, especially since it's very simple.The prison can be represented as a rectangle a* b which is divided into ab cells, each representing a prison cell, common sides being the walls between cells, and sides on the perimeter being the walls leading to freedom. Before sentencing, Michael can ask his friends among the prison employees to make (very well hidden) holes in some of the walls (including walls between cells and the outermost walls). Michael wants to be able to get out of the prison after this, no matter which cell he is placed in. However, he also wants to break as few walls as possible.Your task is to find out the smallest number of walls to be broken so that there is a path to the outside from every cell after this. | Input: ['2', '2 2', '1 3', ''] Output:['4', '3', ''] | [
3
] |
Kilani and Abd are neighbors for 3000 years, but then the day came and Kilani decided to move to another house. As a farewell gift, Kilani is going to challenge Abd with a problem written by their other neighbor with the same name Abd. The problem is:You are given a connected tree rooted at node 1.You should assign a character a or b to every node in the tree so that the total number of a's is equal to x and the total number of b's is equal to n - x.Let's define a string for each node v of the tree as follows: if v is root then the string is just one character assigned to v: otherwise, let's take a string defined for the v's parent p_v and add to the end of it a character assigned to v. You should assign every node a character in a way that minimizes the number of distinct strings among the strings of all nodes. | Input: ['9 3', '1 2 2 4 4 4 3 1', ''] Output:['4', 'aabbbbbba', ''] | [
2
] |
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are n books standing in a row on the shelf, the i-th book has color a_i.You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other.In one operation you can take one book from any position on the shelf and move it to the right end of the shelf.What is the minimum number of operations you need to make the shelf beautiful? | Input: ['5', '1 2 2 1 3', ''] Output:['2', ''] | [
2
] |
Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem. You are given a complete directed graph with n vertices without self-loops. In other words, you have n vertices and each pair of vertices u and v (u \neq v) has both directed edges (u, v) and (v, u).Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges (u, v) and (v, u) may have different labels).You are also given an integer m > 0. You should find a path of length m such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.You can visit the same vertex and the same directed edge any number of times. | Input: ['5', '3 1', '*ba', 'b*b', 'ab*', '3 3', '*ba', 'b*b', 'ab*', '3 4', '*ba', 'b*b', 'ab*', '4 6', '*aaa', 'b*ba', 'ab*a', 'bba*', '2 6', '*a', 'b*', ''] Output:['YES', '1 2', 'YES', '2 1 3 2', 'YES', '1 3 1 3 1', 'YES', '1 2 1 3 4 1 4', 'NO', ''] | [
0,
2
] |
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence β so plain and boring, that you'd like to repaint it. You have a fence consisting of n planks, where the i-th plank has the color a_i. You want to repaint the fence in such a way that the i-th plank has the color b_i.You've invited m painters for this purpose. The j-th painter will arrive at the moment j and will recolor exactly one plank to color c_j. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring b you want? If it's possible, print for each painter which plank he must paint. | Input: ['6', '1 1', '1', '1', '1', '5 2', '1 2 2 1 1', '1 2 2 1 1', '1 2', '3 3', '2 2 2', '2 2 2', '2 3 2', '10 5', '7 3 2 1 7 9 4 2 7 9', '9 9 2 1 4 9 4 2 3 9', '9 9 7 4 3', '5 2', '1 2 2 1 1', '1 2 2 1 1', '3 3', '6 4', '3 4 2 4 1 2', '2 3 1 3 1 1', '2 2 3 4', ''] Output:['YES', '1', 'YES', '2 2', 'YES', '1 1 1', 'YES', '2 1 9 5 9', 'NO', 'NO', ''] | [
0,
2
] |
After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array h_1, h_2, ..., h_n, where h_i is the height of the i-th mountain, and k β the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is h_i): if h_i >= h_{i + 1}, the boulder will roll to the next mountain; if h_i < h_{i + 1}, the boulder will stop rolling and increase the mountain height by 1 (h_i = h_i + 1); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the k-th boulder or determine that it will fall into the waste collection system. | Input: ['4', '4 3', '4 1 2 3', '2 7', '1 8', '4 5', '4 1 2 3', '3 1', '5 3 1', ''] Output:['2', '1', '-1', '-1', ''] | [
0,
2
] |
You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y).The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: if s_i = \text{U}, you move to (x, y + 1); if s_i = \text{D}, you move to (x, y - 1); if s_i = \text{R}, you move to (x + 1, y); if s_i = \text{L}, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions.Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? | Input: ['6', '10 5', 'RRRRRRRRRRUUUUU', '1 1', 'UDDDRLLL', '-3 -5', 'LDLDLDDDR', '1 2', 'LLLLUU', '3 -2', 'RDULRLLDR', '-1 6', 'RUDURUUUUR', ''] Output:['YES', 'YES', 'YES', 'NO', 'YES', 'NO', ''] | [
2
] |
The great hero guards the country where Homer lives. The hero has attack power A and initial health value B. There are n monsters in front of the hero. The i-th monster has attack power a_i and initial health value b_i. The hero or a monster is said to be living, if his or its health value is positive (greater than or equal to 1); and he or it is said to be dead, if his or its health value is non-positive (less than or equal to 0).In order to protect people in the country, the hero will fight with monsters until either the hero is dead or all the monsters are dead. In each fight, the hero can select an arbitrary living monster and fight with it. Suppose the i-th monster is selected, and the health values of the hero and the i-th monster are x and y before the fight, respectively. After the fight, the health values of the hero and the i-th monster become x-a_i and y-A, respectively. Note that the hero can fight the same monster more than once.For the safety of the people in the country, please tell them whether the great hero can kill all the monsters (even if the great hero himself is dead after killing the last monster). | Input: ['5', '3 17 1', '2', '16', '10 999 3', '10 20 30', '100 50 30', '1000 1000 4', '200 300 400 500', '1000 1000 1000 1000', '999 999 1', '1000', '1000', '999 999 1', '1000000', '999', ''] Output:['YES', 'YES', 'YES', 'NO', 'YES', ''] | [
2
] |
Homer has two friends Alice and Bob. Both of them are string fans. One day, Alice and Bob decide to play a game on a string s = s_1 s_2 ... s_n of length n consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move.In a move, a player must choose an index i (1 <=q i <=q n) that has not been chosen before, and change s_i to any other lowercase English letter c that c \neq s_i.When all indices have been chosen, the game ends. The goal of Alice is to make the final string lexicographically as small as possible, while the goal of Bob is to make the final string lexicographically as large as possible. Both of them are game experts, so they always play games optimally. Homer is not a game expert, so he wonders what the final string will be.A string a is lexicographically smaller than a string b if and only if one of the following holds: a is a prefix of b, but a!=b; in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. | Input: ['3', 'a', 'bbbb', 'az', ''] Output:['b', 'azaz', 'by', ''] | [
2
] |
In Homer's school, there are n students who love clubs. Initially, there are m clubs, and each of the n students is in exactly one club. In other words, there are a_i students in the i-th club for 1 <=q i <=q m and a_1+a_2+...+a_m = n.The n students are so unfriendly that every day one of them (chosen uniformly at random from all of the n students) gets angry. The student who gets angry will do one of the following things. With probability \frac 1 2, he leaves his current club, then creates a new club himself and joins it. There is only one student (himself) in the new club he creates. With probability \frac 1 2, he does not create new clubs. In this case, he changes his club to a new one (possibly the same club he is in currently) with probability proportional to the number of students in it. Formally, suppose there are k clubs and there are b_i students in the i-th club for 1 <=q i <=q k (before the student gets angry). He leaves his current club, and then joins the i-th club with probability \frac {b_i} {n}. We note that when a club becomes empty, students will never join it because any student who gets angry will join an empty club with probability 0 according to the above statement.Homer wonders the expected number of days until every student is in the same club for the first time.We can prove that the answer can be represented as a rational number \frac p q with \gcd(p, q) = 1. Therefore, you are asked to find the value of pq^{-1} \bmod 998\,244\,353. It can be shown that q \bmod 998\,244\,353 \neq 0 under the given constraints of the problem. | Input: ['2', '1 1', ''] Output:['4', ''] | [
3
] |
In Homer's country, there are n cities numbered 1 to n and they form a tree. That is, there are (n-1) undirected roads between these n cities and every two cities can reach each other through these roads. Homer's country is an industrial country, and each of the n cities in it contains some mineral resource. The mineral resource of city i is labeled a_i. Homer is given the plans of the country in the following q years. The plan of the i-th year is described by four parameters u_i, v_i, l_i and r_i, and he is asked to find any mineral resource c_i such that the following two conditions hold: mineral resource c_i appears an odd number of times between city u_i and city v_i; and l_i <=q c_i <=q r_i. As the best friend of Homer, he asks you for help. For every plan, find any such mineral resource c_i, or tell him that there doesn't exist one. | Input: ['6 8', '3 2 1 3 1 3', '1 2', '1 3', '2 4', '2 5', '4 6', '3 5 1 1', '3 5 1 3', '3 5 1 3', '1 1 2 2', '1 1 3 3', '1 4 1 5', '1 6 1 3', '1 6 1 3', ''] Output:['-1', '2', '3', '-1', '3', '2', '2', '3'] | [
0,
4
] |
The only difference between the two versions is that this version asks the minimal possible answer.Homer likes arrays a lot. Today he is painting an array a_1, a_2, ..., a_n with two kinds of colors, white and black. A painting assignment for a_1, a_2, ..., a_n is described by an array b_1, b_2, ..., b_n that b_i indicates the color of a_i (0 for white and 1 for black).According to a painting assignment b_1, b_2, ..., b_n, the array a is split into two new arrays a^{(0)} and a^{(1)}, where a^{(0)} is the sub-sequence of all white elements in a and a^{(1)} is the sub-sequence of all black elements in a. For example, if a = [1,2,3,4,5,6] and b = [0,1,0,1,0,0], then a^{(0)} = [1,3,5,6] and a^{(1)} = [2,4].The number of segments in an array c_1, c_2, ..., c_k, denoted \mathit{seg}(c), is the number of elements if we merge all adjacent elements with the same value in c. For example, the number of segments in [1,1,2,2,3,3,3,2] is 4, because the array will become [1,2,3,2] after merging adjacent elements with the same value. Especially, the number of segments in an empty array is 0.Homer wants to find a painting assignment b, according to which the number of segments in both a^{(0)} and a^{(1)}, i.e. \mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)}), is as small as possible. Find this number. | Input: ['6', '1 2 3 1 2 2', ''] Output:['4', ''] | [
2
] |
The only difference between the two versions is that this version asks the maximal possible answer.Homer likes arrays a lot. Today he is painting an array a_1, a_2, ..., a_n with two kinds of colors, white and black. A painting assignment for a_1, a_2, ..., a_n is described by an array b_1, b_2, ..., b_n that b_i indicates the color of a_i (0 for white and 1 for black).According to a painting assignment b_1, b_2, ..., b_n, the array a is split into two new arrays a^{(0)} and a^{(1)}, where a^{(0)} is the sub-sequence of all white elements in a and a^{(1)} is the sub-sequence of all black elements in a. For example, if a = [1,2,3,4,5,6] and b = [0,1,0,1,0,0], then a^{(0)} = [1,3,5,6] and a^{(1)} = [2,4].The number of segments in an array c_1, c_2, ..., c_k, denoted \mathit{seg}(c), is the number of elements if we merge all adjacent elements with the same value in c. For example, the number of segments in [1,1,2,2,3,3,3,2] is 4, because the array will become [1,2,3,2] after merging adjacent elements with the same value. Especially, the number of segments in an empty array is 0.Homer wants to find a painting assignment b, according to which the number of segments in both a^{(0)} and a^{(1)}, i.e. \mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)}), is as large as possible. Find this number. | Input: ['7', '1 1 2 2 3 3 3', ''] Output:['6', ''] | [
2
] |
This is an interactive problem.Homer likes arrays a lot and he wants to play a game with you. Homer has hidden from you a permutation a_1, a_2, ..., a_n of integers 1 to n. You are asked to find any index k (1 <=q k <=q n) which is a local minimum. For an array a_1, a_2, ..., a_n, an index i (1 <=q i <=q n) is said to be a local minimum if a_i < \min\{a_{i-1},a_{i+1}\}, where a_0 = a_{n+1} = +\infty. An array is said to be a permutation of integers 1 to n, if it contains all integers from 1 to n exactly once.Initially, you are only given the value of n without any other information about this permutation.At each interactive step, you are allowed to choose any i (1 <=q i <=q n) and make a query with it. As a response, you will be given the value of a_i. You are asked to find any index k which is a local minimum after at most 100 queries. | Input: ['5', '3', '2', '1', '4', '5', ''] Output:['? 1', '? 2', '? 3', '? 4', '? 5', '! 3', ''] | [
4,
4
] |
Long time ago there was a symmetric array a_1,a_2,...,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,...,a_{2n} is called symmetric if for each integer 1 <= i <= 2n, there exists an integer 1 <= j <= 2n such that a_i = -a_j.For each integer 1 <= i <= 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = \sum_{j = 1}^{2n} {|a_i - a_j|}.Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d. | Input: ['6', '2', '8 12 8 12', '2', '7 7 9 11', '2', '7 11 7 11', '1', '1 1', '4', '40 56 48 40 80 56 80 48', '6', '240 154 210 162 174 154 186 240 174 186 162 210', ''] Output:['YES', 'NO', 'NO', 'NO', 'NO', 'YES', ''] | [
3
] |
Nezzar's favorite digit among 1,...,9 is d. He calls a positive integer lucky if d occurs at least once in its decimal representation. Given q integers a_1,a_2,...,a_q, for each 1 <= i <= q Nezzar would like to know if a_i can be equal to a sum of several (one or more) lucky numbers. | Input: ['2', '3 7', '24 25 27', '10 7', '51 52 53 54 55 56 57 58 59 60', ''] Output:['YES', 'NO', 'YES', 'YES', 'YES', 'NO', 'YES', 'YES', 'YES', 'YES', 'YES', 'YES', 'NO', ''] | [
0,
2,
3
] |
Nezzar has n balls, numbered with integers 1, 2, ..., n. Numbers a_1, a_2, ..., a_n are written on them, respectively. Numbers on those balls form a non-decreasing sequence, which means that a_i <=q a_{i+1} for all 1 <=q i < n.Nezzar wants to color the balls using the minimum number of colors, such that the following holds. For any color, numbers on balls will form a strictly increasing sequence if he keeps balls with this chosen color and discards all other balls. Note that a sequence with the length at most 1 is considered as a strictly increasing sequence.Please help Nezzar determine the minimum number of colors. | Input: ['5', '6', '1 1 1 2 3 4', '5', '1 1 2 2 3', '4', '2 2 2 2', '3', '1 2 3', '1', '1', ''] Output:['3', '2', '4', '1', '1', ''] | [
0,
2
] |
Nezzar buys his favorite snack β n chocolate bars with lengths l_1,l_2,...,l_n. However, chocolate bars might be too long to store them properly! In order to solve this problem, Nezzar designs an interesting process to divide them into small pieces. Firstly, Nezzar puts all his chocolate bars into a black box. Then, he will perform the following operation repeatedly until the maximum length over all chocolate bars does not exceed k. Nezzar picks a chocolate bar from the box with probability proportional to its length x. After step 1, Nezzar uniformly picks a real number r \in (0,x) and divides the chosen chocolate bar into two chocolate bars with lengths r and x-r. Lastly, he puts those two new chocolate bars into the black box. Nezzar now wonders, what is the expected number of operations he will perform to divide his chocolate bars into small pieces.It can be shown that the answer can be represented as \frac{P}{Q}, where P and Q are coprime integers and Q \not \equiv 0 (\bmod 998\,244\,353). Print the value of P\cdot Q^{-1} \mod 998\,244\,353. | Input: ['1 1', '2', ''] Output:['4', ''] | [
3
] |
In the famous Oh-Suit-United tournament, two teams are playing against each other for the grand prize of precious pepper points.The first team consists of n players, and the second team consists of m players. Each player has a potential: the potential of the i-th player in the first team is a_i, and the potential of the i-th player in the second team is b_i.In the tournament, all players will be on the stage in some order. There will be a scoring device, initially assigned to an integer k, which will be used to value the performance of all players.The scores for all players will be assigned in the order they appear on the stage. Let the potential of the current player be x, and the potential of the previous player be y (y equals x for the first player). Then, x-y is added to the value in the scoring device, Afterwards, if the value in the scoring device becomes negative, the value will be reset to 0. Lastly, the player's score is assigned to the current value on the scoring device. The score of a team is the sum of the scores of all its members.As an insane fan of the first team, Nezzar desperately wants the biggest win for the first team. He now wonders what is the maximum difference between scores of the first team and the second team.Formally, let the score of the first team be score_f and the score of the second team be score_s. Nezzar wants to find the maximum value of score_f - score_s over all possible orders of players on the stage.However, situation often changes and there are q events that will happen. There are three types of events: 1 pos x β change a_{pos} to x; 2 pos x β change b_{pos} to x; 3 x β tournament is held with k = x and Nezzar wants you to compute the maximum value of score_f - score_s. Can you help Nezzar to answer the queries of the third type? | Input: ['3 4 3', '1 2 7', '3 4 5 6', '3 5', '1 1 10', '3 5', ''] Output:['-4', '9', ''] | [
2
] |
Nezzar loves the game osu!.osu! is played on beatmaps, which can be seen as an array consisting of distinct points on a plane. A beatmap is called nice if for any three consecutive points A,B,C listed in order, the angle between these three points, centered at B, is strictly less than 90 degrees. Points A,B,C on the left have angle less than 90 degrees, so they can be three consecutive points of a nice beatmap; Points A',B',C' on the right have angle greater or equal to 90 degrees, so they cannot be three consecutive points of a nice beatmap. Now Nezzar has a beatmap of n distinct points A_1,A_2,...,A_n. Nezzar would like to reorder these n points so that the resulting beatmap is nice.Formally, you are required to find a permutation p_1,p_2,...,p_n of integers from 1 to n, such that beatmap A_{p_1},A_{p_2},...,A_{p_n} is nice. If it is impossible, you should determine it. | Input: ['5', '0 0', '5 0', '4 2', '2 1', '3 0', ''] Output:['1 2 5 3 4', ''] | [
2,
3
] |
Nezzar has a binary string s of length n that he wants to share with his best friend, Nanako. Nanako will spend q days inspecting the binary string. At the same time, Nezzar wants to change the string s into string f during these q days, because it looks better.It is known that Nanako loves consistency so much. On the i-th day, Nanako will inspect a segment of string s from position l_i to position r_i inclusive. If the segment contains both characters '0' and '1', Nanako becomes unhappy and throws away the string.After this inspection, at the i-th night, Nezzar can secretly change strictly less than half of the characters in the segment from l_i to r_i inclusive, otherwise the change will be too obvious.Now Nezzar wonders, if it is possible to avoid Nanako being unhappy and at the same time have the string become equal to the string f at the end of these q days and nights. | Input: ['4', '5 2', '00000', '00111', '1 5', '1 3', '2 1', '00', '01', '1 2', '10 6', '1111111111', '0110001110', '1 10', '5 9', '7 10', '1 7', '3 5', '6 10', '5 2', '10000', '11000', '2 5', '1 3', ''] Output:['YES', 'NO', 'YES', 'NO', ''] | [
2
] |
n distinct integers x_1,x_2,...,x_n are written on the board. Nezzar can perform the following operation multiple times. Select two integers x,y (not necessarily distinct) on the board, and write down 2x-y. Note that you don't remove selected numbers. Now, Nezzar wonders if it is possible to have his favorite number k on the board after applying above operation multiple times. | Input: ['6', '2 1', '1 2', '3 0', '2 3 7', '2 -1', '31415926 27182818', '2 1000000000000000000', '1 1000000000000000000', '2 -1000000000000000000', '-1000000000000000000 123', '6 80', '-5 -20 13 -14 -2 -11', ''] Output:['YES', 'YES', 'NO', 'YES', 'YES', 'NO', ''] | [
3
] |
There are n lanterns in a row. The lantern i is placed in position i and has power equal to p_i.Each lantern can be directed to illuminate either some lanterns to the left or some lanterns to the right. If the i-th lantern is turned to the left, it illuminates all such lanterns j that j \in [i - p_i, i - 1]. Similarly, if it is turned to the right, it illuminates all such lanterns j that j \in [i + 1, i + p_i].Your goal is to choose a direction for each lantern so each lantern is illuminated by at least one other lantern, or report that it is impossible. | Input: ['4', '8', '0 0 3 1 1 1 1 2', '2', '1 1', '2', '2 2', '2', '0 1', ''] Output:['YES', 'RRLLLLRL', 'YES', 'RL', 'YES', 'RL', 'NO', ''] | [
4
] |
You have n chains, the i-th chain consists of c_i vertices. Vertices in each chain are numbered independently from 1 to c_i along the chain. In other words, the i-th chain is the undirected graph with c_i vertices and (c_i - 1) edges connecting the j-th and the (j + 1)-th vertices for each 1 <= j < c_i.Now you decided to unite chains in one graph in the following way: the first chain is skipped; the 1-st vertex of the i-th chain is connected by an edge with the a_i-th vertex of the (i - 1)-th chain; the last (c_i-th) vertex of the i-th chain is connected by an edge with the b_i-th vertex of the (i - 1)-th chain. Picture of the first test case. Dotted lines are the edges added during uniting process Calculate the length of the longest simple cycle in the resulting graph.A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once. | Input: ['3', '4', '3 4 3 3', '-1 1 2 2', '-1 2 2 3', '2', '5 6', '-1 5', '-1 1', '3', '3 5 2', '-1 1 1', '-1 3 5', ''] Output:['7', '11', '8', ''] | [
2
] |
You have a statistic of price changes for one product represented as an array of n positive integers p_0, p_1, ..., p_{n - 1}, where p_0 is the initial price of the product and p_i is how the price was increased during the i-th month.Using these price changes you are asked to calculate the inflation coefficients for each month as the ratio of current price increase p_i to the price at the start of this month (p_0 + p_1 + ... + p_{i - 1}).Your boss said you clearly that the inflation coefficients must not exceed k %, so you decided to increase some values p_i in such a way, that all p_i remain integers and the inflation coefficients for each month don't exceed k %.You know, that the bigger changes β the more obvious cheating. That's why you need to minimize the total sum of changes.What's the minimum total sum of changes you need to make all inflation coefficients not more than k %? | Input: ['2', '4 1', '20100 1 202 202', '3 100', '1 1 1', ''] Output:['99', '0', ''] | [
0,
2,
3,
4
] |
You are given two integers n and k.You should create an array of n positive integers a_1, a_2, ..., a_n such that the sum (a_1 + a_2 + ... + a_n) is divisible by k and maximum element in a is minimum possible.What is the minimum possible maximum element in a? | Input: ['4', '1 5', '4 3', '8 8', '8 17', ''] Output:['5', '2', '1', '3', ''] | [
2,
3,
4
] |
Polycarp found on the street an array a of n elements.Polycarp invented his criterion for the beauty of an array. He calls an array a beautiful if at least one of the following conditions must be met for each different pair of indices i!=j: a_i is divisible by a_j; or a_j is divisible by a_i. For example, if: n=5 and a=[7, 9, 3, 14, 63], then the a array is not beautiful (for i=4 and j=2, none of the conditions above is met); n=3 and a=[2, 14, 42], then the a array is beautiful; n=4 and a=[45, 9, 3, 18], then the a array is not beautiful (for i=1 and j=4 none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array a so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array a beautiful. | Input: ['4', '5', '7 9 3 14 63', '3', '2 14 42', '4', '45 9 3 18', '3', '2 2 8', ''] Output:['2', '0', '1', '0', ''] | [
3
] |
You are given two binary square matrices a and b of size n * n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): vertical xor. You choose the number j (1 <= j <= n) and for all i (1 <= i <= n) do the following: a_{i, j} := a_{i, j} \oplus 1 (\oplus β is the operation xor (exclusive or)). horizontal xor. You choose the number i (1 <= i <= n) and for all j (1 <= j <= n) do the following: a_{i, j} := a_{i, j} \oplus 1. Note that the elements of the a matrix change after each operation.For example, if n=3 and the matrix a is: \begin{pmatrix} 1 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 1 & 0 \end{pmatrix} Then the following sequence of operations shows an example of transformations: vertical xor, j=1. a= \begin{pmatrix} 0 & 1 & 0 \\ 1 & 0 & 1 \\ 0 & 1 & 0 \end{pmatrix} horizontal xor, i=2. a= \begin{pmatrix} 0 & 1 & 0 \\ 0 & 1 & 0 \\ 0 & 1 & 0 \end{pmatrix} vertical xor, j=2. a= \begin{pmatrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{pmatrix} Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. | Input: ['3', '3', '110', '001', '110', '', '000', '000', '000', '3', '101', '010', '101', '', '010', '101', '010', '2', '01', '11', '', '10', '10', ''] Output:['YES', 'YES', 'NO', ''] | [
0
] |
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers: conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6. conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6. Since the answer can be quite large, output it modulo 10^9+7. | Input: ['3', '4 3', '1 3 1 2', '4 2', '1 1 1 1', '2 1', '1 2', ''] Output:['2', '6', '1', ''] | [
3
] |
Polycarp often uses his smartphone. He has already installed n applications on it. Application with number i takes up a_i units of memory.Polycarp wants to free at least m units of memory (by removing some applications).Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β he assigned an integer b_i to each application: b_i = 1 β regular application; b_i = 2 β important application. According to this rating system, his phone has b_1 + b_2 + ... + b_n convenience points.Polycarp believes that if he removes applications with numbers i_1, i_2, ..., i_k, then he will free a_{i_1} + a_{i_2} + ... + a_{i_k} units of memory and lose b_{i_1} + b_{i_2} + ... + b_{i_k} convenience points.For example, if n=5, m=7, a=[5, 3, 2, 1, 4], b=[2, 1, 1, 2, 1], then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers 1, 4 and 5. In this case, it will free a_1+a_4+a_5=10 units of memory and lose b_1+b_4+b_5=5 convenience points; applications with numbers 1 and 3. In this case, it will free a_1+a_3=7 units of memory and lose b_1+b_3=3 convenience points. applications with numbers 2 and 5. In this case, it will free a_2+a_5=7 memory units and lose b_2+b_5=2 convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. | Input: ['5', '5 7', '5 3 2 1 4', '2 1 1 2 1', '1 3', '2', '1', '5 10', '2 3 2 3 2', '1 2 1 2 1', '4 10', '5 1 3 4', '1 2 1 2', '4 5', '3 2 1 2', '2 1 2 1', ''] Output:['2', '-1', '6', '4', '3', ''] | [
4
] |
At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls.Each class must present two couples to the ball. In Vasya's class, a boys and b girls wish to participate. But not all boys and not all girls are ready to dance in pairs.Formally, you know k possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair.For example, if a=3, b=4, k=4 and the couples (1, 2), (1, 3), (2, 2), (3, 4) are ready to dance together (in each pair, the boy's number comes first, then the girl's number), then the following combinations of two pairs are possible (not all possible options are listed below): (1, 3) and (2, 2); (3, 4) and (1, 3); But the following combinations are not possible: (1, 3) and (1, 2) β the first boy enters two pairs; (1, 2) and (2, 2) β the second girl enters two pairs; Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs. | Input: ['3', '3 4 4', '1 1 2 3', '2 3 2 4', '1 1 1', '1', '1', '2 2 4', '1 1 2 2', '1 2 1 2', ''] Output:['4', '0', '2', ''] | [
3
] |
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.For example, if: n=4041, then the number n can be represented as the sum 2020 + 2021; n=4042, then the number n can be represented as the sum 2021 + 2021; n=8081, then the number n can be represented as the sum 2020 + 2020 + 2020 + 2021; n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021. Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021. | Input: ['5', '1', '4041', '4042', '8081', '8079', ''] Output:['NO', 'YES', 'YES', 'YES', 'NO', ''] | [
0,
3
] |
You are given an integer n. Check if n has an odd divisor, greater than one (does there exist such a number x (x > 1) that n is divisible by x and x is odd).For example, if n=6, then there is x=3. If n=4, then such a number does not exist. | Input: ['6', '2', '3', '4', '5', '998244353', '1099511627776', ''] Output:['NO', 'YES', 'NO', 'YES', 'YES', 'NO', ''] | [
3
] |
Igor had a sequence d_1, d_2, ..., d_n of integers. When Igor entered the classroom there was an integer x written on the blackboard.Igor generated sequence p using the following algorithm: initially, p = [x]; for each 1 <=q i <=q n he did the following operation |d_i| times: if d_i >=q 0, then he looked at the last element of p (let it be y) and appended y + 1 to the end of p; if d_i < 0, then he looked at the last element of p (let it be y) and appended y - 1 to the end of p. For example, if x = 3, and d = [1, -1, 2], p will be equal [3, 4, 3, 4, 5].Igor decided to calculate the length of the longest increasing subsequence of p and the number of them.A sequence a is a subsequence of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements.A sequence a is an increasing sequence if each element of a (except the first one) is strictly greater than the previous element.For p = [3, 4, 3, 4, 5], the length of longest increasing subsequence is 3 and there are 3 of them: [\underline{3}, \underline{4}, 3, 4, \underline{5}], [\underline{3}, 4, 3, \underline{4}, \underline{5}], [3, 4, \underline{3}, \underline{4}, \underline{5}]. | Input: ['3', '3', '1 -1 2', ''] Output:['3 3', ''] | [
3
] |
Lunar rover finally reached planet X. After landing, he met an obstacle, that contains permutation p of length n. Scientists found out, that to overcome an obstacle, the robot should make p an identity permutation (make p_i = i for all i).Unfortunately, scientists can't control the robot. Thus the only way to make p an identity permutation is applying the following operation to p multiple times: Select two indices i and j (i \neq j), such that p_j = i and swap the values of p_i and p_j. It takes robot (j - i)^2 seconds to do this operation. Positions i and j are selected by the robot (scientists can't control it). He will apply this operation while p isn't an identity permutation. We can show that the robot will make no more than n operations regardless of the choice of i and j on each operation.Scientists asked you to find out the maximum possible time it will take the robot to finish making p an identity permutation (i. e. worst-case scenario), so they can decide whether they should construct a new lunar rover or just rest and wait. They won't believe you without proof, so you should build an example of p and robot's operations that maximizes the answer.For a better understanding of the statement, read the sample description. | Input: ['3', '2', '3', '3', ''] Output:['1', '2 1', '1', '2 1', '5', '2 3 1', '2', '1 3', '3 2', '5', '2 3 1', '2', '1 3', '2 3'] | [
2
] |
During cleaning the coast, Alice found n piles of stones. The i-th pile has a_i stones.Piles i and i + 1 are neighbouring for all 1 <=q i <=q n - 1. If pile i becomes empty, piles i - 1 and i + 1 doesn't become neighbouring.Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation: Select two neighboring piles and, if both of them are not empty, remove one stone from each of them. Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability: Before the start of cleaning, you can select two neighboring piles and swap them. Determine, if it is possible to remove all stones using the superability not more than once. | Input: ['5', '3', '1 2 1', '3', '1 1 2', '5', '2 2 2 1 3', '5', '2100 1900 1600 3000 1600', '2', '2443 2445', ''] Output:['YES', 'YES', 'YES', 'YES', 'NO', ''] | [
2,
3
] |
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a.It could have been an easy task, but it turned out that you should follow some rules: In the beginning, you select any positive integer x. Then you do the following operation n times: select two elements of array with sum equals x; remove them from a and replace x with maximum of that two numbers. For example, if initially a = [3, 5, 1, 2], you can select x = 6. Then you can select the second and the third elements of a with sum 5 + 1 = 6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation.Note, that you choose x before the start and can't change it as you want between the operations.Determine how should you behave to throw out all elements of a. | Input: ['4', '2', '3 5 1 2', '3', '1 1 8 8 64 64', '2', '1 1 2 4', '5', '1 2 3 4 5 6 7 14 3 11', ''] Output:['YES', '6', '1 5', '2 3', 'NO', 'NO', 'YES', '21', '14 7', '3 11', '5 6', '2 4', '3 1'] | [
0,
2
] |
Positive integer x is called divisor of positive integer y, if y is divisible by x without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8.We gave you an integer d and asked you to find the smallest positive integer a, such that a has at least 4 divisors; difference between any two divisors of a is at least d. | Input: ['2', '1', '2', ''] Output:['6', '15', ''] | [
2,
3,
4
] |
In the 2022 year, Mike found two binary integers a and b of length n (both of them are written only by digits 0 and 1) that can have leading zeroes. In order not to forget them, he wanted to construct integer d in the following way: he creates an integer c as a result of bitwise summing of a and b without transferring carry, so c may have one or more 2-s. For example, the result of bitwise summing of 0110 and 1101 is 1211 or the sum of 011000 and 011000 is 022000; after that Mike replaces equal consecutive digits in c by one digit, thus getting d. In the cases above after this operation, 1211 becomes 121 and 022000 becomes 020 (so, d won't have equal consecutive digits). Unfortunately, Mike lost integer a before he could calculate d himself. Now, to cheer him up, you want to find any binary integer a of length n such that d will be maximum possible as integer.Maximum possible as integer means that 102 > 21, 012 < 101, 021 = 21 and so on. | Input: ['5', '1', '0', '3', '011', '3', '110', '6', '111000', '6', '001011', ''] Output:['1', '110', '100', '101101', '101110', ''] | [
2
] |
Consider a road consisting of several rows. Each row is divided into several rectangular tiles, and all tiles in the same row are equal. The first row contains exactly one rectangular tile. Look at the picture below which shows how the tiles are arranged.The road is constructed as follows: the first row consists of 1 tile; then a_1 rows follow; each of these rows contains 1 tile greater than the previous row; then b_1 rows follow; each of these rows contains 1 tile less than the previous row; then a_2 rows follow; each of these rows contains 1 tile greater than the previous row; then b_2 rows follow; each of these rows contains 1 tile less than the previous row; ... then a_n rows follow; each of these rows contains 1 tile greater than the previous row; then b_n rows follow; each of these rows contains 1 tile less than the previous row. An example of the road with n = 2, a_1 = 4, b_1 = 2, a_2 = 2, b_2 = 3. Rows are arranged from left to right. You start from the only tile in the first row and want to reach the last row (any tile of it). From your current tile, you can move to any tile in the next row which touches your current tile.Calculate the number of different paths from the first row to the last row. Since it can be large, print it modulo 998244353. | Input: ['2', '4 2', '2 3', ''] Output:['850', ''] | [
3
] |
Note that the memory limit is unusual.You are given an integer n and two sequences a_1, a_2, ..., a_n and b_1, b_2, ..., b_n.Let's call a set of integers S such that S \subseteq \{1, 2, 3, ..., n\} strange, if, for every element i of S, the following condition is met: for every j \in [1, i - 1], if a_j divides a_i, then j is also included in S. An empty set is always strange.The cost of the set S is \sum\limits_{i \in S} b_i. You have to calculate the maximum possible cost of a strange set. | Input: ['9', '4 7 3 4 5 6 7 8 13', '-2 3 -19 5 -6 7 -8 9 1', ''] Output:['16', ''] | [
3
] |
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k <= n < 2k).Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i!=t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t). | Input: ['4', '1 1', '2 2', '3 2', '4 3', ''] Output:['1 ', '1 2 ', '2 1 ', '1 3 2 ', ''] | [
3
] |
Let's define a multiplication operation between a string a and a positive integer x: a \cdot x is the string that is a result of writing x copies of a one after another. For example, "abc" \cdot~2~= "abcabc", "a" \cdot~5~= "aaaaa".A string a is divisible by another string b if there exists an integer x such that b \cdot x = a. For example, "abababab" is divisible by "ab", but is not divisible by "ababab" or "aa".LCM of two strings s and t (defined as LCM(s, t)) is the shortest non-empty string that is divisible by both s and t.You are given two strings s and t. Find LCM(s, t) or report that it does not exist. It can be shown that if LCM(s, t) exists, it is unique. | Input: ['3', 'baba', 'ba', 'aa', 'aaa', 'aba', 'ab', ''] Output:['baba', 'aaaaaa', '-1', ''] | [
0,
3
] |
You have an array a_1, a_2, ..., a_n. All a_i are positive integers.In one step you can choose three distinct indices i, j, and k (i \neq j; i \neq k; j \neq k) and assign the sum of a_j and a_k to a_i, i. e. make a_i = a_j + a_k.Can you make all a_i lower or equal to d using the operation above any number of times (possibly, zero)? | Input: ['3', '5 3', '2 3 2 5 4', '3 4', '2 4 4', '5 4', '2 1 5 3 6', ''] Output:['NO', 'YES', 'YES', ''] | [
2,
3
] |
Every year Santa Claus gives gifts to all children. However, each country has its own traditions, and this process takes place in different ways. For example, in Berland you need to solve the New Year's puzzle.Polycarp got the following problem: given a grid strip of size 2 * n, some cells of it are blocked. You need to check if it is possible to tile all free cells using the 2 * 1 and 1 * 2 tiles (dominoes).For example, if n = 5 and the strip looks like this (black cells are blocked): Then it can be tiled, for example, using two vertical and two horizontal tiles, as in the picture below (different tiles are marked by different colors). And if n = 3 and the strip looks like this: It is impossible to tile free cells.Polycarp easily solved this task and received his New Year's gift. Can you solve it? | Input: ['3', '', '5 2', '2 2', '1 4', '', '3 2', '2 1', '2 3', '', '6 4', '2 1', '2 3', '2 4', '2 6', ''] Output:['YES', 'NO', 'NO', ''] | [
0,
2
] |
Polycarp has invited n friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.Each friend is characterized by two values h_i (their height) and w_i (their width). On the photo the i-th friend will occupy a rectangle h_i * w_i (if they are standing) or w_i * h_i (if they are lying on the side).The j-th friend can be placed in front of the i-th friend on the photo if his rectangle is lower and narrower than the rectangle of the i-th friend. Formally, at least one of the following conditions must be fulfilled: h_j < h_i and w_j < w_i (both friends are standing or both are lying); w_j < h_i and h_j < w_i (one of the friends is standing and the other is lying). For example, if n = 3, h=[3,5,3] and w=[4,4,3], then: the first friend can be placed in front of the second: w_1 < h_2 and h_1 < w_2 (one of the them is standing and the other one is lying); the third friend can be placed in front of the second: h_3 < h_2 and w_3 < w_2 (both friends are standing or both are lying). In other cases, the person in the foreground will overlap the person in the background.Help Polycarp for each i find any j, such that the j-th friend can be located in front of the i-th friend (i.e. at least one of the conditions above is fulfilled).Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend i any other friend j who can be located in front of him. Think about it as you need to solve n separate independent subproblems. | Input: ['4', '3', '3 4', '5 4', '3 3', '3', '1 3', '2 2', '3 1', '4', '2 2', '3 1', '6 3', '5 4', '4', '2 2', '2 3', '1 1', '4 4', ''] Output:['-1 3 -1 ', '-1 -1 -1 ', '-1 -1 2 2 ', '3 3 -1 3 ', ''] | [
4
] |
During their New Year holidays, Alice and Bob play the following game using an array a of n integers: Players take turns, Alice moves first. Each turn a player chooses any element and removes it from the array. If Alice chooses even value, then she adds it to her score. If the chosen value is odd, Alice's score does not change. Similarly, if Bob chooses odd value, then he adds it to his score. If the chosen value is even, then Bob's score does not change. If there are no numbers left in the array, then the game ends. The player with the highest score wins. If the scores of the players are equal, then a draw is declared.For example, if n = 4 and a = [5, 2, 7, 3], then the game could go as follows (there are other options): On the first move, Alice chooses 2 and get two points. Her score is now 2. The array a is now [5, 7, 3]. On the second move, Bob chooses 5 and get five points. His score is now 5. The array a is now [7, 3]. On the third move, Alice chooses 7 and get no points. Her score is now 2. The array a is now [3]. On the last move, Bob chooses 3 and get three points. His score is now 8. The array a is empty now. Since Bob has more points at the end of the game, he is the winner. You want to find out who will win if both players play optimally. Note that there may be duplicate numbers in the array. | Input: ['4', '4', '5 2 7 3', '3', '3 2 1', '4', '2 2 2 2', '2', '7 8', ''] Output:['Bob', 'Tie', 'Alice', 'Alice', ''] | [
2
] |
Alice and Bob received n candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice's candies is equal to the total weight of Bob's candies.Check if they can do that.Note that candies are not allowed to be cut in half. | Input: ['5', '2', '1 1', '2', '1 2', '4', '1 2 1 2', '3', '2 2 2', '3', '2 1 2', ''] Output:['YES', 'NO', 'YES', 'NO', 'NO', ''] | [
2,
3
] |
For the New Year, Polycarp decided to send postcards to all his n friends. He wants to make postcards with his own hands. For this purpose, he has a sheet of paper of size w * h, which can be cut into pieces.Polycarp can cut any sheet of paper w * h that he has in only two cases: If w is even, then he can cut the sheet in half and get two sheets of size \frac{w}{2} * h; If h is even, then he can cut the sheet in half and get two sheets of size w * \frac{h}{2}; If w and h are even at the same time, then Polycarp can cut the sheet according to any of the rules above.After cutting a sheet of paper, the total number of sheets of paper is increased by 1.Help Polycarp to find out if he can cut his sheet of size w * h at into n or more pieces, using only the rules described above. | Input: ['5', '2 2 3', '3 3 2', '5 10 2', '11 13 1', '1 4 4', ''] Output:['YES', 'NO', 'YES', 'YES', 'YES', ''] | [
2,
3
] |
You have given an array a of length n and an integer x to a brand new robot. What the robot does is the following: it iterates over the elements of the array, let the current element be q. If q is divisible by x, the robot adds x copies of the integer \frac{q}{x} to the end of the array, and moves on to the next element. Note that the newly added elements could be processed by the robot later. Otherwise, if q is not divisible by x, the robot shuts down.Please determine the sum of all values of the array at the end of the process. | Input: ['2', '1 2', '12', '4 2', '4 6 8 2', ''] Output:['36', '44', ''] | [
0,
2,
3
] |
You are given an array a of length n, and an integer x. You can perform the following operation as many times as you would like (possibly zero): replace two adjacent elements of the array by their sum. For example, if the initial array was [3, 6, 9], in a single operation one can replace the last two elements by their sum, yielding an array [3, 15], or replace the first two elements to get an array [9, 9]. Note that the size of the array decreases after each operation.The beauty of an array b=[b_1, ..., b_k] is defined as \sum_{i=1}^k <=ft\lceil \frac{b_i}{x} \right\rceil, which means that we divide each element by x, round it up to the nearest integer, and sum up the resulting values. For example, if x = 3, and the array is [4, 11, 6], the beauty of the array is equal to <=ft\lceil \frac{4}{3} \right\rceil + <=ft\lceil \frac{11}{3} \right\rceil + <=ft\lceil \frac{6}{3} \right\rceil = 2 + 4 + 2 = 8.Please determine the minimum and the maximum beauty you can get by performing some operations on the original array. | Input: ['2', '3 3', '3 6 9', '3 3', '6 4 11', ''] Output:['6 6', '7 8', ''] | [
2,
3
] |
Alice had a permutation p_1, p_2, ..., p_n. Unfortunately, the permutation looked very boring, so she decided to change it and choose some non-overlapping subranges of this permutation and reverse them. The cost of reversing a single subrange [l, r] (elements from position l to position r, inclusive) is equal to r - l, and the cost of the operation is the sum of costs of reversing individual subranges. Alice had an integer c in mind, so she only considered operations that cost no more than c.Then she got really bored, and decided to write down all the permutations that she could possibly obtain by performing exactly one operation on the initial permutation. Of course, Alice is very smart, so she wrote down each obtainable permutation exactly once (no matter in how many ways it can be obtained), and of course the list was sorted lexicographically.Now Bob would like to ask Alice some questions about her list. Each question is in the following form: what is the i-th number in the j-th permutation that Alice wrote down? Since Alice is too bored to answer these questions, she asked you to help her out. | Input: ['2', '3 1 9', '1 2 3', '1 1', '2 1', '3 1', '1 2', '2 2', '3 2', '1 3', '2 3', '3 3', '6 4 4', '6 5 4 3 1 2', '1 1', '3 14', '1 59', '2 6', ''] Output:['1', '2', '3', '1', '3', '2', '2', '1', '3', '1', '4', '-1', '5', ''] | [
4
] |
Students of Winter Informatics School are going to live in a set of houses connected by underground passages. Teachers are also going to live in some of these houses, but they can not be accommodated randomly. For safety reasons, the following must hold: All passages between two houses will be closed, if there are no teachers in both of them. All other passages will stay open. It should be possible to travel between any two houses using the underground passages that are open. Teachers should not live in houses, directly connected by a passage. Please help the organizers to choose the houses where teachers will live to satisfy the safety requirements or determine that it is impossible. | Input: ['2', '3 2', '3 2', '2 1', '4 2', '1 4', '2 3', ''] Output:['YES', '2', '1 3 ', 'NO', ''] | [
2
] |
This is an interactive problem.n people sitting in a circle are trying to shuffle a deck of cards. The players are numbered from 1 to n, so that players i and i+1 are neighbours (as well as players 1 and n). Each of them has exactly k cards, where k is even. The left neighbour of a player i is player i - 1, and their right neighbour is player i + 1 (except for players 1 and n, who are respective neighbours of each other).Each turn the following happens: if a player has x cards, they give \lfloor x / 2 \rfloor to their neighbour on the left and \lceil x / 2 \rceil cards to their neighbour on the right. This happens for all players simultaneously.However, one player p is the impostor and they just give all their cards to their neighbour on the right. You know the number of players n and the number of cards k each player has initially, but p is unknown to you. Your task is to determine the value of p, by asking questions like "how many cards does player q have?" for an index q of your choice. After each question all players will make exactly one move and give their cards to their neighbours. You need to find the impostor by asking no more than 1000 questions. | Input: ['4 2', '', '2', '', '1', '', '2', '', '3', '', '2', ''] Output:['? 1', '', '? 1', '', '? 2', '', '? 3', '', '? 4', '', '! 2', ''] | [
0,
4
] |