source
int64
2
2
difficulty
int64
7
25
name
stringlengths
9
60
description
stringlengths
164
7.12k
public_tests
dict
private_tests
dict
cf_rating
int64
0
3.5k
cf_points
float64
0
4k
2
9
1413_C. Perform Easily
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has 6 strings and an infinite number of frets numbered from 1. Fretting the fret number j on the i-th string produces the note a_{i} + j. Tayuya wants to play a melody of n notes. Each note can be play...
{ "input": [ "1 4 100 10 30 5\n6\n101 104 105 110 130 200\n", "1 1 2 2 3 3\n7\n13 4 11 12 11 13 12\n" ], "output": [ "0", "7" ] }
{ "input": [ "5 4 7 6 4 1\n10\n19 16 18 12 16 15 16 20 16 14\n", "11 16 12 20 12 13\n10\n21 21 21 21 21 21 21 21 21 21\n", "1 1 1 96 99 100\n3\n101 146 175\n", "158260522 877914575 602436426 24979445 861648772 623690081\n1\n896194147\n", "58260522 77914575 2436426 24979445 61648772 23690081\n10\n5...
1,900
1,500
2
9
1455_C. Ping-pong
Alice and Bob play ping-pong with simplified rules. During the game, the player serving the ball commences a play. The server strikes the ball then the receiver makes a return by hitting the ball back. Thereafter, the server and receiver must alternately make a return until one of them doesn't make a return. The one ...
{ "input": [ "3\n1 1\n2 1\n1 7\n" ], "output": [ "\n0 1\n1 1\n0 7\n" ] }
{ "input": [ "3\n1 1\n2 1\n96342 7\n", "1\n99899 99899\n", "1\n99899 100000\n", "1\n1000000 1\n", "1\n99999 100000\n", "12\n1 1\n2 1\n1 7\n1 1\n2 1\n1 7\n1 1\n2 1\n1 7\n1 1\n2 1\n1 7\n", "1\n1000000 1000000\n", "2\n1000000 1\n1 1000000\n", "1\n8627 2007\n", "2\n1000000 1000000\...
1,100
0
2
8
152_B. Steps
One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangu...
{ "input": [ "10 10\n1 2\n1\n-1 0\n", "4 5\n1 1\n3\n1 1\n1 1\n0 -2\n" ], "output": [ "0\n", "4\n" ] }
{ "input": [ "10 20\n10 3\n10\n-2 -6\n-1 0\n-8 0\n0 5\n-1 3\n16 -16\n-1 9\n0 -18\n9 -1\n-9 5\n", "1 1\n1 1\n1\n1 1\n", "20 10\n14 4\n10\n6 0\n-7 -7\n12 -2\n-4 9\n20 3\n-1 -16\n0 2\n-1 1\n20 0\n-1 1\n", "3 3\n2 2\n1\n2 0\n", "1000 2000\n226 566\n20\n0 -100\n-100 100\n100 0\n42 0\n-100 -79\n-66 -16\...
1,300
1,000
2
10
160_D. Edges in MST
You are given a connected weighted undirected graph without any loops and multiple edges. Let us remind you that a graph's spanning tree is defined as an acyclic connected subgraph of the given graph that includes all of the graph's vertexes. The weight of a tree is defined as the sum of weights of the edges that the...
{ "input": [ "3 3\n1 2 1\n2 3 1\n1 3 2\n", "4 5\n1 2 101\n1 3 100\n2 3 2\n2 4 2\n3 4 1\n", "3 3\n1 2 1\n2 3 1\n1 3 1\n" ], "output": [ "any\nany\nnone\n", "none\nany\nat least one\nat least one\nany\n", "at least one\nat least one\nat least one\n" ] }
{ "input": [ "5 8\n2 5 3\n4 1 3\n3 4 7\n3 1 9\n1 2 6\n5 3 7\n2 4 7\n4 5 9\n", "10 15\n6 5 805980\n1 6 805980\n7 8 805980\n4 9 805980\n4 1 805980\n3 6 805980\n6 9 805980\n8 10 805980\n3 1 805980\n1 8 805980\n8 4 805980\n2 8 805980\n2 10 805980\n2 7 805980\n2 9 805980\n", "2 1\n1 2 1\n", "25 25\n17 13 5...
2,300
2,000
2
7
180_A. Defragmentation
In this problem you have to implement an algorithm to defragment your hard disk. The hard disk consists of a sequence of clusters, numbered by integers from 1 to n. The disk has m recorded files, the i-th file occupies clusters with numbers ai, 1, ai, 2, ..., ai, ni. These clusters are not necessarily located consecuti...
{ "input": [ "7 2\n2 1 3\n3 2 4 5\n", "7 2\n2 1 2\n3 3 4 5\n" ], "output": [ "3\n2 6\n3 2\n6 3\n", "0\n" ] }
{ "input": [ "7 3\n1 7\n2 6 5\n3 4 3 2\n", "100 30\n8 62 50 93 34 82 24 87 65\n2 64 86\n3 91 42 88\n2 76 7\n2 35 63\n3 78 84 15\n4 90 46 73 9\n4 74 13 95 58\n3 71 59 55\n2 4 31\n1 19\n1 10\n2 45 23\n2 27 69\n4 12 66 44 22\n1 36\n2 67 2\n2 77 54\n4 96 47 25 52\n5 18 29 43 70 56\n2 14 41\n2 100 6\n3 30 94 75\n2...
1,800
0
2
8
203_B. Game on Paper
One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of n × n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the p...
{ "input": [ "4 11\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n3 3\n4 1\n", "4 12\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n4 2\n4 1\n3 1\n" ], "output": [ "10", "-1" ] }
{ "input": [ "1 1\n1 1\n", "1000 1\n542 374\n", "4 12\n2 2\n1 1\n3 3\n3 4\n1 2\n1 3\n1 4\n2 1\n3 2\n2 3\n3 1\n4 1\n", "2 1\n1 1\n", "1000 10\n1000 1000\n1000 999\n1000 998\n999 1000\n999 999\n999 998\n998 1000\n998 999\n998 998\n1 1\n", "3 1\n1 3\n", "10 50\n9 7\n4 8\n8 9\n1 6\n6 3\n3 1\n5...
1,300
1,000
2
7
228_A. Is your horseshoe on the other hoof?
Valera the Horse is going to the party with friends. He has been following the fashion trends for a while, and he knows that it is very popular to wear all horseshoes of different color. Valera has got four horseshoes left from the last year, but maybe some of them have the same color. In this case he needs to go to th...
{ "input": [ "7 7 7 7\n", "1 7 3 3\n" ], "output": [ "3\n", "1\n" ] }
{ "input": [ "2 2 2 1\n", "3491663 217797045 522540872 715355328\n", "28442865 741657755 978106882 978106882\n", "240458500 511952208 240458500 511952208\n", "133315691 265159773 734556507 265159773\n", "255635360 732742923 798648949 883146723\n", "81170865 673572653 756938629 995577259\n"...
800
500
2
10
251_D. Two Sets
Little Petya likes numbers a lot. Recently his mother has presented him a collection of n non-negative integers. There's only one thing Petya likes more than numbers: playing with little Masha. He immediately decided to give a part of his new collection to her. To make the game even more interesting, Petya decided to g...
{ "input": [ "3\n1000000000000 1000000000000 1000000000000\n", "8\n1 1 2 2 3 3 4 4\n", "6\n1 2 3 4 5 6\n" ], "output": [ " 2 2 ...
{ "input": [ "10\n1 2 1 0 0 1 100 1 3 1\n", "2\n1024 2048\n", "5\n0 0 1 3 128\n", "2\n123456789123456789 123456789123456789\n", "17\n1 2 3 4 5 6 7 8 9 10 11 12 13 13 13 17 16\n", "30\n285 214 1 421 145 3 52 54 62 24 2 245 2 28 8 2 2 20 2 0 3333 171 172 173 174 175 176 666 219 30\n", "1\n12...
2,700
2,000
2
8
29_B. Traffic Lights
A car moves from point A to point B at speed v meters per second. The action takes place on the X-axis. At the distance d meters from A there are traffic lights. Starting from time 0, for the first g seconds the green light is on, then for the following r seconds the red light is on, then again the green light is on fo...
{ "input": [ "5 4 3 1 1\n", "2 1 3 4 5\n" ], "output": [ "2.33333333333\n", "0.666666666667\n" ] }
{ "input": [ "1000 999 1000 1000 1\n", "1000 999 1 1000 1000\n", "1000 999 1000 1 1000\n", "2 1 1 1 1000\n", "1000 1 1 1000 1\n", "29 12 569 939 259\n", "1000 999 1 1 1000\n", "1000 1 1000 1 1000\n", "1000 1 1000 1000 1\n", "2 1 1000 1 1\n", "1000 999 1 1000 1\n", "2 1 ...
1,500
1,000
2
9
347_C. Alice and Bob
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two di...
{ "input": [ "3\n5 6 7\n", "2\n5 3\n", "2\n2 3\n" ], "output": [ "Bob\n", "Alice\n", "Alice\n" ] }
{ "input": [ "2\n1 2\n", "10\n1 999999999 999999998 999999997 999999996 999999995 999999994 999999993 999999992 999999991\n", "2\n6 2\n", "4\n2 3 15 30\n", "2\n4 6\n", "10\n72 96 24 66 6 18 12 30 60 48\n", "2\n1 1000000000\n", "2\n10 4\n", "10\n98 63 42 56 14 77 70 35 84 21\n", ...
1,600
500
2
10
370_D. Broken Monitor
Innocentius has a problem — his computer monitor has broken. Now some of the pixels are "dead", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus. Innocentius is touch-typing a program that pain...
{ "input": [ "4 8\n..w..w..\n........\n........\n..w..w..\n", "5 6\n......\n.w....\n......\n..w...\n......\n", "2 4\n....\n.w..\n", "2 6\nw..w.w\n...w..\n" ], "output": [ "..w++w..\n..+..+..\n..+..+..\n..w++w..\n", "......\n+w+...\n+.+...\n++w...\n......\n", "....\n.w..\n", "-1\n" ...
{ "input": [ "1 3\n.w.\n", "8 10\n..........\n..........\n.....w....\n.w........\n..........\n....w.....\n..........\n..........\n", "1 2\nww\n", "5 7\n.......\n.wwww..\n.......\n.......\n.......\n", "4 6\n....w.\n......\n.w....\n......\n", "4 4\nw...\n..w.\n....\n....\n", "5 4\n....\n.w.....
2,100
3,000
2
7
392_A. Blocked Points
Imagine you have an infinite 2D plane with Cartesian coordinate system. Some of the integral points are blocked, and others are not. Two integral points A and B on the plane are 4-connected if and only if: * the Euclidean distance between A and B is one unit and neither A nor B is blocked; * or there is some inte...
{ "input": [ "2\n", "1\n", "3\n" ], "output": [ "8\n", "4\n", "16\n" ] }
{ "input": [ "34714265\n", "31975828\n", "16\n", "46340\n", "12\n", "39099999\n", "10\n", "46341\n", "15012490\n", "9\n", "15\n", "11\n", "6\n", "39999996\n", "39999997\n", "3107977\n", "17590047\n", "24562258\n", "12823666\n", "2346673\n...
0
500
2
8
415_B. Mashmokh and Tokens
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of ...
{ "input": [ "1 1 1\n1\n", "3 1 2\n1 2 3\n", "5 1 4\n12 6 11 9 1\n" ], "output": [ "0\n", "1 0 1\n", "0 2 3 1 1\n" ] }
{ "input": [ "1 1 1000000000\n1000000000\n", "1 1 1000000000\n999999999\n", "10 1 100000000\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\n" ], "output": [ "0\n", "999999999\n", "99999999 99999999 99999999 99999999 99999999 99999999 99...
1,500
1,000
2
7
464_A. No to Palindromes!
Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more. Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable st...
{ "input": [ "3 4\ncba\n", "4 4\nabcd\n", "3 3\ncba\n" ], "output": [ "cbd\n", "abda\n", "NO\n" ] }
{ "input": [ "6 3\nacbacb\n", "30 7\ncedcfedcfgcfgcbadcadgfaegfacgf\n", "1 26\no\n", "1 2\na\n", "3 26\nyzx\n", "333 5\nedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedcedc...
1,700
500
2
10
510_D. Fox And Jumping
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After app...
{ "input": [ "3\n100 99 9900\n1 1 1\n", "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n", "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10\n", "5\n10 20 30 40 50\n1 1 1 1 1\n" ], "output": [ "2\n", "7237\n", "6\n", "-1\n" ] }
{ "input": [ "1\n1\n1\n", "39\n692835 4849845 22610 1995 19019 114 6270 15 85085 27170 1365 1155 7410 238 3135 546 373065 715 110 969 15 10374 2730 19019 85 65 5187 26 3233230 1122 399 1122 53295 910 110 12597 16302 125970 67830\n4197 6490 2652 99457 65400 96257 33631 23456 14319 22288 16179 74656 89713 31503...
1,900
2,000
2
7
560_A. Currency System in Geraldion
A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of ea...
{ "input": [ "5\n1 2 3 4 5\n" ], "output": [ "-1\n" ] }
{ "input": [ "10\n635370 154890 909382 220996 276501 716105 538714 140162 171960 271264\n", "1\n1\n", "10\n371054 506438 397130 1 766759 208409 769264 549213 641270 771837\n", "1\n1000000\n", "1\n2\n", "2\n3 2\n", "50\n110876 835020 859879 999908 712969 788264 287153 921820 330355 499311 2...
1,000
500
2
9
586_C. Gennady the Dentist
Gennady is one of the best child dentists in Berland. Today n children got an appointment with him, they lined up in front of his office. All children love to cry loudly at the reception at the dentist. We enumerate the children with integers from 1 to n in the order they go in the line. Every child is associated with...
{ "input": [ "5\n4 2 2\n4 1 2\n5 2 4\n3 3 5\n5 1 2\n", "5\n4 5 1\n5 3 9\n4 1 2\n2 1 8\n4 1 9\n" ], "output": [ "2\n1 3", "4\n1 2 4 5" ] }
{ "input": [ "10\n10 3 3\n8 6 17\n9 5 26\n10 7 17\n3 10 29\n3 1 27\n3 3 7\n8 10 28\n1 3 23\n3 4 6\n", "1\n1 1 1\n", "10\n9 8 8\n2 9 8\n10 7 16\n7 2 9\n3 5 23\n9 9 25\n3 2 35\n3 5 36\n5 3 40\n4 4 42\n", "10\n10 7 10\n3 6 11\n8 4 10\n10 1 11\n7 3 13\n7 2 13\n7 6 14\n3 4 17\n9 4 20\n5 2 24\n", "3\n5 ...
1,800
500
2
10
608_D. Zuma
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible. In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is ...
{ "input": [ "3\n1 2 3\n", "7\n1 4 4 2 3 2 1\n", "3\n1 2 1\n" ], "output": [ "3", "2", "1" ] }
{ "input": [ "50\n22 19 14 22 20 11 16 28 23 15 3 23 6 16 30 15 15 10 24 28 19 19 22 30 28 1 27 12 12 14 17 30 17 26 21 26 27 1 11 23 9 30 18 19 17 29 11 20 29 24\n", "50\n5 7 5 10 7 9 1 9 10 2 8 3 5 7 3 10 2 3 7 6 2 7 1 2 2 2 4 7 3 5 8 3 4 4 1 6 7 10 5 4 8 1 9 5 5 3 4 4 8 3\n", "50\n30 17 31 15 10 3 39 3...
1,900
1,250
2
8
656_B. Scrambled
Btoh yuo adn yuor roomatme lhoate wianshg disehs, btu stlil sdmoeboy msut peorrfm tihs cohre dialy. Oen dya yuo decdie to idourtcne smoe syestm. Yuor rmmotaoe sstgegus teh fooniwllg dael. Yuo argee on tow arayrs of ientgres M adn R, nmebur upmicnog dyas (induiclng teh cunrret oen) wtih sicsescuve irnegets (teh ceurrnt ...
{ "input": [ "2\n2 3\n1 0\n", "1\n2\n0\n" ], "output": [ "0.6666666666666666\n", "0.5\n" ] }
{ "input": [ "1\n15\n1\n", "1\n7\n5\n", "1\n6\n3\n", "2\n10 14\n2 5\n", "16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n", "1\n16\n15\n", "3\n6 14 7\n4 2 0\n", "2\n13 3\n6 0\n", "12\n8 5 5 12 12 14 14 16 5 11 9 3\n1 4 0 11 10 0 2 3 1 8 8 2\n", ...
1,700
0
2
8
67_B. Restoration of the Permutation
Let A = {a1, a2, ..., an} be any permutation of the first n natural numbers {1, 2, ..., n}. You are given a positive integer k and another sequence B = {b1, b2, ..., bn}, where bi is the number of elements aj in A to the left of the element at = i such that aj ≥ (i + k). For example, if n = 5, a possible A is {5, 1, 4...
{ "input": [ "5 2\n1 2 1 0 0\n", "4 2\n1 0 0 0\n" ], "output": [ "4 1 5 2 3\n", "2 3 1 4\n" ] }
{ "input": [ "13 2\n1 2 3 4 5 4 3 2 1 0 0 0 0\n", "20 4\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0\n", "20 2\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0\n", "10 10\n0 0 0 0 0 0 0 0 0 0\n", "10 3\n4 2 4 2 1 0 1 0 0 0\n", "20 1\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0\n", "20 1\n1 1 1 1 1 1 1 1 1...
1,800
1,000
2
8
702_B. Powers of Two
You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2 (i. e. some integer x exists so that ai + aj = 2x). Input The first line contains the single positive integer n (1 ≤ n ≤ 105) — the number of integers. The second line contains n positive integers ...
{ "input": [ "3\n1 1 1\n", "4\n7 3 2 1\n" ], "output": [ "3\n", "2\n" ] }
{ "input": [ "2\n1 1\n", "10\n2827343 1373647 96204862 723505 796619138 71550121 799843967 5561265 402690754 446173607\n", "1\n2\n", "10\n6 6 7 3 9 14 15 7 2 2\n", "100\n3 6 12 1 16 4 9 5 4 4 5 8 12 4 6 14 5 1 2 2 2 1 7 1 9 10 6 13 7 8 3 11 8 11 7 5 15 6 14 10 4 2 10 9 1 8 14 9 5 11 3 4 1 12 6 8 1...
1,500
0
2
9
724_C. Ray Tracing
There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes. At...
{ "input": [ "3 3 4\n1 1\n1 2\n2 1\n2 2\n", "3 4 6\n1 1\n2 1\n1 2\n2 2\n1 3\n2 3\n", "7 4 5\n1 3\n2 2\n5 1\n5 3\n4 3\n" ], "output": [ "1\n-1\n-1\n2\n", "1\n-1\n-1\n2\n5\n-1\n", "13\n2\n9\n5\n-1\n" ] }
{ "input": [ "10 10 10\n3 8\n1 7\n2 3\n4 2\n4 8\n3 3\n2 8\n5 5\n6 3\n3 1\n" ], "output": [ "-1\n-1\n-1\n-1\n-1\n3\n-1\n5\n-1\n-1\n" ] }
1,800
1,500
2
9
746_C. Tram
The tram in Berland goes along a straight line from the point 0 to the point s and back, passing 1 meter per t1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points x = 0 and x = s. Igor is at the point x1. He should reach the point...
{ "input": [ "4 2 4\n3 4\n1 1\n", "5 4 0\n1 2\n3 1\n" ], "output": [ "8\n", "7\n" ] }
{ "input": [ "50 10 30\n1 50\n10 1\n", "1000 913 474\n34 162\n566 -1\n", "1000 394 798\n155 673\n954 -1\n", "10 4 8\n1 5\n4 -1\n", "40 31 14\n628 1000\n36 1\n", "5 4 1\n1 100\n4 -1\n", "4 2 4\n3 4\n2 1\n", "20 5 19\n163 174\n4 1\n", "4 1 2\n1 10\n3 1\n", "6 4 2\n1 2\n3 1\n", ...
1,600
1,500
2
9
769_C. Cycle In Maze
The Robot is in a rectangular maze of size n × m. Each cell of the maze is either empty or occupied by an obstacle. The Robot can move between neighboring cells on the side left (the symbol "L"), right (the symbol "R"), up (the symbol "U") or down (the symbol "D"). The Robot can move to the cell only if it is empty. In...
{ "input": [ "3 3 4\n***\n*X*\n***\n", "5 6 14\n..***.\n*...X.\n..*...\n..*.**\n....*.\n", "2 3 2\n.**\nX..\n" ], "output": [ "IMPOSSIBLE\n", "DLDDLLLRRRUURU\n", "RL\n" ] }
{ "input": [ "1 10 1\n........X.\n", "2 1 2\nX\n.\n", "20 10 116\n..........\n....*.....\n.......*..\n*.........\n*....*....\n*........*\n..........\n*.........\n.......*..\n...*..*...\n..........\n...*......\n..*.......\n.....**..*\n........*.\n........*.\n...*......\n.........*\n.....*.X..\n*......*.*\n...
1,700
1,500
2
9
793_C. Mice problem
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them. The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located i...
{ "input": [ "4\n7 7 9 8\n0 3 -5 4\n5 0 5 4\n9 9 -1 -6\n10 5 -7 -10\n", "4\n7 7 9 8\n3 5 7 5\n7 5 2 4\n3 3 7 8\n6 6 3 2\n" ], "output": [ "-1", "0.5714286714" ] }
{ "input": [ "1\n0 0 10 10\n5 5 5 5\n", "1\n0 0 5 5\n2 5 0 0\n", "4\n0 49998 2 50002\n1 50000 0 0\n1 50000 0 0\n1 0 0 1\n1 100000 0 -1\n", "1\n0 0 100000 100000\n0 0 -1 -1\n", "4\n17501 63318 51967 74514\n1305 84026 79493 -78504\n41159 81000 -44104 -42722\n31063 65435 25578 33487\n18330 79949 8346...
2,300
1,500
2
7
85_A. Domino
We all know the problem about the number of ways one can tile a 2 × n field by 1 × 2 dominoes. You probably remember that it goes down to Fibonacci numbers. We will talk about some other problem below, there you also are going to deal with tiling a rectangular field with dominoes. You are given a 4 × n rectangular fie...
{ "input": [ "4\n" ], "output": [ "abbz\naccz\nddee\neeff\n" ] }
{ "input": [ "25\n", "27\n", "1\n", "97\n", "98\n", "94\n", "21\n", "97\n", "19\n", "3\n", "28\n", "2\n", "8\n", "12\n", "15\n", "91\n", "4\n", "9\n", "23\n", "29\n", "22\n", "96\n", "30\n", "7\n", "100\n", "16\n",...
1,300
500
2
7
886_A. ACM ICPC
In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team s...
{ "input": [ "1 1 1 1 1 99\n", "1 3 2 1 2 1\n" ], "output": [ "NO\n", "YES\n" ] }
{ "input": [ "1000 1000 1000 1000 1000 1000\n", "180 179 188 50 75 214\n", "633 609 369 704 573 416\n", "2 2 2 2 2 1\n", "101 200 400 300 10 9\n", "1 6 6 1 20 2\n", "72 8 186 92 267 69\n", "101 200 300 400 10 9\n", "353 313 327 470 597 31\n", "101 400 200 9 300 10\n", "36 9...
1,000
500
2
7
909_A. Generate Login
The preferred way to generate user login in Polygon is to concatenate a prefix of the user's first name and a prefix of their last name, in that order. Each prefix must be non-empty, and any of the prefixes can be the full name. Typically there are multiple possible logins for each person. You are given the first and ...
{ "input": [ "tom riddle\n", "harry potter\n" ], "output": [ "tomr\n", "hap\n" ] }
{ "input": [ "jjxwj kxccwx\n", "dtbqya fyyymv\n", "apple pie\n", "ca cf\n", "amolfed pun\n", "ab b\n", "bgopsdfji uaps\n", "aa ab\n", "aaaaaaaaaa aaaaaaaaaa\n", "obljndajv q\n", "mybiqxmnqq l\n", "a aaa\n", "aaaaaaa a\n", "xohesmku ef\n", "fapkdme rtzxovx\n"...
1,000
500
2
9
930_C. Teodor is not a liar!
Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge [1;m] segment. One day Teodor noticed that picture he just drawn has one interesting feature: there doesn't exist an integer point, that belongs each of segments in the picture. Having discovered this fact, Teodor ...
{ "input": [ "2 4\n1 2\n3 4\n", "4 6\n1 3\n2 3\n4 6\n5 6\n" ], "output": [ "4", "5" ] }
{ "input": [ "11 3\n1 1\n1 1\n1 1\n1 1\n2 2\n2 2\n2 2\n3 3\n3 3\n3 3\n3 3\n", "43 1319\n750 1030\n857 946\n941 1203\n407 1034\n947 1290\n546 585\n630 1201\n72 342\n693 1315\n34 719\n176 1097\n36 931\n198 973\n5 1025\n892 1054\n461 1287\n195 1273\n832 1039\n308 955\n642 866\n770 838\n440 777\n289 948\n98 814\n...
1,900
1,500
2
10
958_D1. Hyperspace Jump (easy)
The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independent...
{ "input": [ "4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7\n" ], "output": [ "1 2 2 1 \n" ] }
{ "input": [ "30\n(89+76)/87\n(81+78)/18\n(60+97)/32\n(41+14)/48\n(55+65)/27\n(29+15)/95\n(64+13)/96\n(78+30)/75\n(43+6)/60\n(69+34)/48\n(62+2)/97\n(85+42)/3\n(4+97)/42\n(1+18)/39\n(46+55)/76\n(22+59)/24\n(62+81)/98\n(64+8)/51\n(9+59)/48\n(47+2)/80\n(33+74)/76\n(61+83)/44\n(86+4)/51\n(65+41)/49\n(53+36)/45\n(6+19...
1,400
0
2
9
984_C. Finite or not?
You are given several queries. Each query consists of three integers p, q and b. You need to answer whether the result of p/q in notation with base b is a finite fraction. A fraction in notation with base b is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction ...
{ "input": [ "2\n6 12 10\n4 3 10\n", "4\n1 1 2\n9 36 2\n4 12 3\n3 5 4\n" ], "output": [ "Finite\nInfinite\n", "Finite\nFinite\nFinite\nInfinite\n" ] }
{ "input": [ "10\n10 8 5\n0 6 9\n0 7 6\n5 7 3\n7 6 8\n0 4 8\n2 6 3\n10 2 9\n6 7 9\n9 1 4\n", "1\n1 5244319080000 30030\n", "10\n10 5 3\n1 7 10\n7 5 7\n4 4 9\n6 5 2\n6 7 5\n9 9 7\n7 5 5\n6 6 4\n10 8 2\n", "1\n1 864691128455135232 2\n", "10\n5 8 2\n0 5 8\n5 9 7\n0 7 2\n6 7 2\n10 3 7\n8 1 10\n9 1 8\n...
1,700
500
2
10
1023_D. Array Restoration
Initially there was an array a consisting of n integers. Positions in it are numbered from 1 to n. Exactly q queries were performed on the array. During the i-th query some segment (l_i, r_i) (1 ≤ l_i ≤ r_i ≤ n) was selected and values of elements on positions from l_i to r_i inclusive got changed to i. The order of t...
{ "input": [ "4 3\n1 0 2 3\n", "3 10\n10 10 10\n", "3 5\n0 0 0\n", "5 6\n6 5 6 2 2\n" ], "output": [ "YES\n1 1 2 3\n", "YES\n10 10 10\n", "YES\n5 5 5\n", "NO\n" ] }
{ "input": [ "50 2\n0 1 0 1 0 0 1 0 1 1 0 1 1 1 2 2 0 2 0 2 0 2 0 0 2 2 2 0 0 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 1 1 0 1 0 0\n", "1 1\n1\n", "5 5\n4 3 5 4 1\n", "6 4\n4 4 3 3 4 4\n", "1 2\n1\n", "50 500\n494 500 0 0 0 0 500 500 0 0 500 0 500 0 500 0 500 0 500 0 500 0 500 500 500 500 0 0 500 0 500 0...
1,700
2,000
2
8
108_B. Datatypes
Tattah's youngest brother, Tuftuf, is new to programming. Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava. Today, Tuftuf was introduced to Gava's unsigned integer datatypes. G...
{ "input": [ "4\n4 2 1 3\n", "3\n64 16 32\n" ], "output": [ "YES\n", "NO\n" ] }
{ "input": [ "9\n20 44 92 8 20 380 8 188 764\n", "7\n1 2 3 4 8 16 32\n", "2\n1000000000 999999999\n", "56\n43 641 626 984 107 521 266 835 707 220 402 406 558 199 988 685 843 808 182 73 553 17 765 979 116 178 489 271 532 889 26 263 654 680 240 392 980 267 264 46 888 444 874 519 735 301 743 526 376 793 ...
1,400
1,000
2
7
1109_A. Sasha and a Bit of Relax
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful. Therefore, Sasha decided ...
{ "input": [ "3\n42 4 2\n", "6\n3 2 2 3 7 6\n", "5\n1 2 3 4 5\n" ], "output": [ "0", "3", "1" ] }
{ "input": [ "2\n60202 951227\n" ], "output": [ "0" ] }
1,600
500
2
10
1138_D. Camp Schedule
The new camp by widely-known over the country Spring Programming Camp is going to start soon. Hence, all the team of friendly curators and teachers started composing the camp's schedule. After some continuous discussion, they came up with a schedule s, which can be represented as a binary string, in which the i-th symb...
{ "input": [ "10\n11100\n", "10010110\n100011\n", "101101\n110\n" ], "output": [ "10\n", "10001101", "110110\n" ] }
{ "input": [ "0101011\n0101\n", "00010000111010011101110110010110100010001101001110\n1011111000111010111001111\n", "10100110\n10\n", "10000111\n10101\n", "11111111111111111111111111111111111111111111111111\n11111111111111111111111111111111111111111111111110\n", "1111111111111111111111111111111...
1,600
750
2
8
1197_B. Pillars
There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius a_i. You can move these disks from one pillar to another. You can take a disk from pillar i and place it on top of pillar j if all these conditions are met: ...
{ "input": [ "3\n3 1 2\n", "4\n1 3 4 2\n" ], "output": [ "NO", "YES" ] }
{ "input": [ "5\n3 1 5 4 2\n", "13\n1 2 3 4 5 6 7 8 9 10 11 12 13\n", "100\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80...
1,000
0
2
9
1214_C. Bad Sequence
Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself. To make everything right, Petya is going to move at most one bracket from i...
{ "input": [ "2\n)(\n", "10\n)))))(((((\n", "3\n(()\n", "2\n()\n" ], "output": [ "Yes\n", "No\n", "No\n", "Yes\n" ] }
{ "input": [ "3\n)((\n", "100\n))))))))))))))))))))))))))))))))))))))))))))))))))((((((((((((((((((((((((((((((((((((((((((((((((((\n", "500\n()))))()()(()((((())))((((()()((()))))))((()())))()(()(()))()()()))())))()))))))(()()(()((()))()(((()(()(()())(((())()((()((()((())(()()()(()(()())(()(((((((((()))(...
1,200
1,500
2
11
1237_E. Balanced Binary Search Trees
Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number o...
{ "input": [ "4\n", "3\n" ], "output": [ "1", "0" ] }
{ "input": [ "1000000\n", "988727\n", "22\n", "581472\n", "796867\n", "43690\n", "84\n", "13156\n", "1\n", "7\n", "19\n", "26\n", "43689\n", "621012\n", "699050\n", "17\n", "11\n", "8\n", "181407\n", "530259\n", "259060\n", "75009...
2,400
2,500
2
11
1255_E1. Send Boxes to Alice (Easy Version)
This is the easier version of the problem. In this version, 1 ≤ n ≤ 10^5 and 0 ≤ a_i ≤ 1. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes o...
{ "input": [ "1\n1\n", "3\n1 0 1\n" ], "output": [ "-1\n", "2\n" ] }
{ "input": [ "10\n3 3 3 5 6 9 3 1 7 3\n", "16\n47 14 19 48 2 41 38 25 5 24 38 22 1 30 38 41\n", "100\n1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", ...
1,800
500
2
10
1279_D. Santa's Bot
Santa Claus has received letters from n different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the i-th kid asked Santa to give them one of k_i different items as a present. Some items could have been asked by multiple kids. Santa is really busy, so he wants the ...
{ "input": [ "5\n2 1 2\n2 3 1\n3 2 4 3\n2 1 4\n3 4 3 2\n", "2\n2 2 1\n1 1\n" ], "output": [ "798595483\n", "124780545\n" ] }
{ "input": [ "10\n5 48258 84644 992412 548310 132019\n5 132019 556600 548310 84644 992412\n6 132019 771663 523582 548310 463969 556600\n7 556600 132019 992412 523582 548310 70239 84644\n4 556600 548310 523582 463969\n7 548310 84644 771663 556600 132019 463969 48258\n5 556600 771663 463969 523582 84644\n6 48258 54...
1,700
0
2
8
129_B. Students and Shoelaces
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the f...
{ "input": [ "6 5\n1 4\n2 4\n3 4\n5 4\n6 4\n", "6 3\n1 2\n2 3\n3 4\n", "3 3\n1 2\n2 3\n3 1\n" ], "output": [ "1\n", "2\n", "0\n" ] }
{ "input": [ "9 33\n5 7\n5 9\n9 6\n9 1\n7 4\n3 5\n7 8\n8 6\n3 6\n8 2\n3 8\n1 6\n1 8\n1 4\n4 2\n1 2\n2 5\n3 4\n8 5\n2 6\n3 1\n1 5\n1 7\n3 2\n5 4\n9 4\n3 9\n7 3\n6 4\n9 8\n7 9\n8 4\n6 5\n", "51 23\n46 47\n31 27\n1 20\n49 16\n2 10\n29 47\n13 27\n34 26\n31 2\n28 20\n17 40\n39 4\n29 26\n28 44\n3 39\n50 12\n19 1\n3...
1,200
1,000
2
10
1341_D. Nastya and Scoreboard
Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally... Denis asks her to be together, but ... Nastya doesn't give any answer. The poor boy was very upset because of that. He w...
{ "input": [ "1 7\n0000000\n", "3 5\n0100001\n1001001\n1010011\n", "2 5\n0010010\n0010010\n" ], "output": [ "8\n", "-1\n", "97\n" ] }
{ "input": [ "10 10\n1101001\n0110000\n0111010\n0010000\n1010000\n0111000\n1011011\n1010010\n1101011\n1111110\n", "10 10\n1100011\n1010011\n0000111\n1110110\n0101011\n0111111\n1001111\n1000000\n1111011\n0111000\n", "10 0\n0111010\n1101111\n1111011\n0111010\n1101011\n1101011\n1110111\n1010010\n1111111\n001...
1,700
750
2
7
1405_A. Permutation Forgery
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array). Let p be any permutation of length ...
{ "input": [ "3\n2\n1 2\n6\n2 1 6 5 4 3\n5\n2 4 3 1 5\n" ], "output": [ "2 1\n3 4 5 6 1 2\n5 1 3 4 2\n" ] }
{ "input": [ "3\n2\n1 2\n6\n2 1 6 5 4 3\n5\n2 4 3 1 5\n" ], "output": [ "2 1\n3 4 5 6 1 2\n5 1 3 4 2\n" ] }
800
500
2
8
144_B. Meeting
The Super Duper Secret Meeting of the Super Duper Secret Military Squad takes place in a Super Duper Secret Place. The place is an infinite plane with introduced Cartesian coordinate system. The meeting table is represented as a rectangle whose sides are parallel to the coordinate axes and whose vertexes are located at...
{ "input": [ "5 2 6 3\n2\n6 2 2\n6 5 3\n", "2 5 4 2\n3\n3 1 2\n5 3 1\n1 3 2\n" ], "output": [ "0", "4" ] }
{ "input": [ "-210 783 -260 833\n10\n406 551 1000\n372 -373 999\n-12 -532 999\n371 -30 999\n258 480 558\n648 -957 1000\n-716 654 473\n156 813 366\n-870 425 707\n-288 -426 1000\n", "0 0 1 1\n1\n-1 -1000 1000\n", "-705 595 -702 600\n1\n-589 365 261\n", "1 1 1000 1000\n1\n50 50 1\n", "-343 -444 -419 ...
1,300
1,000
2
8
1473_B. String LCM
Let's define a multiplication operation between a string a and a positive integer x: a ⋅ x is the string that is a result of writing x copies of a one after another. For example, "abc" ⋅~2~= "abcabc", "a" ⋅~5~= "aaaaa". A string a is divisible by another string b if there exists an integer x such that b ⋅ x = a. For e...
{ "input": [ "3\nbaba\nba\naa\naaa\naba\nab\n" ], "output": [ "\nbaba\naaaaaa\n-1\n" ] }
{ "input": [ "13\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\n", "1\nabababababbb\nab\n" ], "output": [ "a\na\na\na\na\na\na\na\na\na\na\na\na\n", "-1\n" ] }
1,000
0
2
8
1521_B. Nastia and a Good Array
Nastia has received an array of n positive integers as a gift. She calls such an array a good that for all i (2 ≤ i ≤ n) takes place gcd(a_{i - 1}, a_{i}) = 1, where gcd(u, v) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers u and v. You can perform the op...
{ "input": [ "2\n5\n9 6 3 11 15\n3\n7 5 13\n" ], "output": [ "\n2\n1 5 11 9\n2 5 7 6\n0" ] }
{ "input": [ "2\n5\n9 6 3 11 15\n3\n7 5 13\n" ], "output": [ "4\n3 1 3 5\n3 2 3 4\n3 4 3 4\n3 5 3 5\n2\n2 1 5 6\n2 3 5 6\n" ] }
1,300
1,000
2
8
197_B. Limit
You are given two polynomials: * P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and * Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit <image>. Input The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The seco...
{ "input": [ "2 1\n1 1 1\n2 5\n", "1 0\n-1 3\n2\n", "1 1\n9 0\n-5 2\n", "2 2\n2 1 6\n4 5 -7\n", "0 1\n1\n1 0\n" ], "output": [ "Infinity\n", "-Infinity\n", "-9/5\n", "1/2\n", "0/1\n" ] }
{ "input": [ "1 1\n-2 1\n4 1\n", "0 0\n36\n-8\n", "2 2\n-4 2 1\n-5 8 -19\n", "84 54\n82 -54 28 68 74 -61 54 98 59 67 -65 -1 16 65 -78 -16 61 -79 2 14 44 96 -62 77 51 87 37 66 65 28 88 -99 -21 -83 24 80 39 64 -65 45 86 -53 -49 94 -75 -31 -42 -1 -35 -18 74 30 31 -40 30 -6 47 58 -71 -21 20 13 75 -79 15 -...
1,400
500
2
8
26_B. Regular Bracket Sequence
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. One day Johnny got bracket sequence. He decided to remove some...
{ "input": [ "(()))(\n", "((()())\n" ], "output": [ "4\n", "6\n" ] }
{ "input": [ "))()()((()()))())()(((((((())((((((((())()()((())(\n", ")))((((())(()((()((((()()())((\n", "()()(()(((\n", "(\n", "()))(()((((()(())\n", "())))((()())())))))())\n", "))())))))))())))))()()))()()))))())))))()))))))))))))(()))())(()))))(()))))())))((((()()))))()))()))\n", "...
1,400
1,000
2
8
340_B. Maximal Area Quadrilateral
Iahub has drawn a set of n points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners). Please note that a quadrilateral doesn't have to be convex. A special quadrilateral is one...
{ "input": [ "5\n0 0\n0 4\n4 0\n4 4\n2 3\n" ], "output": [ "16.000000\n" ] }
{ "input": [ "7\n-2 -1\n4 3\n2 2\n-4 0\n-2 4\n0 0\n1 -3\n", "10\n-6 -4\n-7 5\n-7 -7\n5 -7\n4 -9\n-6 7\n2 9\n-4 -6\n2 10\n-10 -4\n", "4\n0 0\n0 5\n5 0\n1 1\n", "6\n-4 -3\n-1 3\n0 0\n2 2\n2 1\n-3 1\n", "4\n-3 3\n0 3\n-2 -1\n2 2\n", "4\n-874 606\n-996 -207\n897 847\n775 191\n", "5\n-4 -3\n-3 ...
2,100
3,000
2
10
363_D. Renting Bikes
A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own pe...
{ "input": [ "2 2 10\n5 5\n7 6\n", "4 5 2\n8 1 1 2\n6 3 7 5 2\n" ], "output": [ "2 3\n", "3 8\n" ] }
{ "input": [ "3 3 3\n1 1 2\n3 5 6\n", "20 10 31\n17 27 2 6 11 12 5 3 12 4 2 10 4 8 2 10 7 9 12 1\n24 11 18 10 30 16 20 18 24 24\n", "6 6 2\n6 1 5 3 10 1\n11 4 7 8 11 7\n", "4 8 10\n2 1 2 2\n10 12 10 8 7 9 10 9\n", "40 40 61\n28 59 8 27 45 67 33 32 61 3 42 2 3 37 8 8 10 61 1 5 65 28 34 27 8 35 45 4...
1,800
2,000
2
9
387_C. George and Number
George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence...
{ "input": [ "9555\n", "19992000\n", "10000000005\n", "45\n", "800101\n", "310200\n", "1000000000000001223300003342220044555\n" ], "output": [ "4", "1", "2", "1", "3", "2", "17" ] }
{ "input": [ "20900000000090009000070069000026000000000000020008\n", "542\n", "10000000000000000000000000000000000000400500000000000000000000000000000000030020010300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
1,700
1,500
2
7
408_A. Line to Cashier
Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products. There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki p...
{ "input": [ "1\n1\n1\n", "4\n1 4 3 2\n100\n1 2 2 3\n1 9 1\n7 8\n" ], "output": [ "20\n", "100\n" ] }
{ "input": [ "1\n1\n100\n", "5\n10 10 10 10 10\n6 7 8 6 8 5 9 8 10 5\n9 6 9 8 7 8 8 10 8 5\n8 7 7 8 7 5 6 8 9 5\n6 5 10 5 5 10 7 8 5 5\n10 9 8 7 6 9 7 9 6 5\n", "10\n9 10 10 10 9 5 9 7 8 7\n11 6 10 4 4 15 7 15 5\n3 9 11 12 11 1 13 13 1 5\n6 15 9 12 3 2 8 12 11 10\n7 1 1 6 10 2 6 1 14 2\n8 14 2 3 6 1 14 1 ...
900
500
2
8
435_B. Pasha Maximizes
Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer. Help Pasha count the maximum number he can get if he has the time to make at most k sw...
{ "input": [ "1990 1\n", "9090000078001234 6\n", "300 0\n", "1034 2\n" ], "output": [ "9190\n", "9907000008001234\n", "300\n", "3104\n" ] }
{ "input": [ "9022 2\n", "1234567891234567 99\n", "191919191919119911 100\n", "787464780004 2\n", "1234 5\n", "901000000954321789 28\n", "12 100\n", "219810011901120912 100\n", "901000000954321789 40\n", "619911311932347059 3\n", "39940894417248510 10\n", "92153348 6\n"...
1,400
1,000
2
8
47_B. Coins
One day Vasya came across three Berland coins. They didn't have any numbers that's why Vasya didn't understand how their denominations differ. He supposed that if one coin is heavier than the other one, then it should be worth more. Vasya weighed all the three pairs of coins on pan balance scales and told you the resul...
{ "input": [ "A&gt;B\nC&lt;B\nA&gt;C\n", "A&lt;B\nB&gt;C\nC&gt;A\n" ], "output": [ "Impossible\n", "Impossible\n" ] }
{ "input": [ "C>A\nC<B\nB>A\n", "C<B\nB<A\nC>A\n", "C<B\nB>A\nA<C\n", "A>C\nC>B\nB<A\n", "C<B\nC<A\nB<A\n", "A>B\nC>B\nA<C\n", "A>C\nC<B\nB>A\n", "B>A\nC<A\nC>B\n", "B<A\nC>B\nC>A\n", "A>B\nC>A\nB<C\n", "B>A\nC<B\nC>A\n", "C>A\nA<B\nC>B\n", "B>C\nA<B\nA<C\n", "B...
1,200
1,000
2
8
504_B. Misha and Permutations Summation
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order. For example, Perm(0) = (0, 1, ..., n...
{ "input": [ "2\n0 1\n0 1\n", "2\n0 1\n1 0\n", "3\n1 2 0\n2 1 0\n" ], "output": [ "0 1 \n", "1 0 \n", "1 0 2 \n" ] }
{ "input": [ "10\n3 5 7 0 2 8 9 6 1 4\n4 3 8 7 9 6 0 5 2 1\n", "8\n2 3 0 5 4 7 6 1\n6 3 2 5 0 4 7 1\n", "8\n5 2 4 6 1 0 3 7\n7 4 3 0 2 6 1 5\n", "10\n7 4 6 1 0 9 2 8 5 3\n4 7 0 5 2 8 9 6 1 3\n", "9\n8 5 0 1 6 7 4 2 3\n6 5 0 8 7 1 4 3 2\n", "75\n71 69 34 23 13 68 19 45 40 6 74 11 53 24 27 7 50 ...
2,000
3,000
2
9
553_C. Love Triangles
There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has n characters. The characters are labeled from 1 to n. Every pair of two characters can either mutually love each other or mutually hate each other (the...
{ "input": [ "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 1\n", "3 0\n", "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 0\n" ], "output": [ "0\n", " 4\n", " 1\n" ] }
{ "input": [ "4 4\n1 2 0\n2 3 0\n2 4 0\n3 4 0\n", "6 6\n1 2 0\n2 3 1\n3 4 0\n4 5 1\n5 6 0\n6 1 1\n", "100000 0\n", "4 3\n2 3 0\n3 4 0\n2 4 0\n", "100 3\n1 2 0\n2 3 0\n3 1 0\n", "28567 13\n28079 24675 1\n18409 26720 1\n980 10815 1\n20794 16571 1\n7376 19861 1\n11146 706 1\n4255 16391 1\n27376 1...
2,200
1,000
2
7
601_A. The Two Routes
In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one r...
{ "input": [ "4 2\n1 3\n3 4\n", "5 5\n4 2\n3 5\n4 5\n5 1\n1 2\n", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n" ], "output": [ "2", "3", "-1" ] }
{ "input": [ "100 1\n100 1\n", "4 1\n1 4\n", "3 1\n1 2\n", "4 5\n1 3\n2 1\n3 4\n4 2\n2 3\n", "400 1\n1 400\n", "381 0\n", "3 0\n", "3 2\n2 3\n3 1\n", "5 4\n1 2\n3 2\n3 4\n5 4\n", "5 5\n2 5\n1 2\n1 4\n1 3\n3 2\n", "2 0\n", "3 1\n1 3\n", "20 1\n20 1\n", "3 3\n1 2\...
1,600
500
2
10
623_D. Birthday
A MIPT student named Misha has a birthday today, and he decided to celebrate it in his country house in suburban Moscow. n friends came by, and after a typical party they decided to play blind man's buff. The birthday boy gets blindfolded and the other players scatter around the house. The game is played in several ro...
{ "input": [ "4\n50 20 20 10\n", "2\n50 50\n" ], "output": [ "39.284626344368\n", "5.000000000000\n" ] }
{ "input": [ "6\n14 14 18 21 20 13\n", "12\n6 10 11 9 6 9 9 12 8 8 5 7\n", "100\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "4\n25 25 25 25\n...
2,700
2,000
2
9
645_C. Enduring Exodus
In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his k cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of n rooms located in a row, some of which are occupied. ...
{ "input": [ "3 2\n000\n", "5 1\n01010\n", "7 2\n0100100\n" ], "output": [ "1\n", "2\n", "2\n" ] }
{ "input": [ "5 3\n00000\n", "9 3\n010001000\n", "7 6\n0000000\n", "9 8\n000000000\n", "491 89\n0111110111111110000011101011000101000111011100001010111110100010001001011101110111011011110110101011100011100001110001101001001011111100001101101010011000100001110011100000101110001000111110111110100011...
1,600
1,500
2
7
672_A. Summer Camp
Every year, hundreds of people come to summer camps, they learn new algorithms and solve hard problems. This is your first year at summer camp, and you are asked to solve the following problem. All integers starting with 1 are written in one line. The prefix of these line is "123456789101112131415...". Your task is to...
{ "input": [ "11\n", "3\n" ], "output": [ "0\n", "3\n" ] }
{ "input": [ "942\n", "952\n", "191\n", "289\n", "179\n", "453\n", "945\n", "157\n", "879\n", "781\n", "500\n", "12\n", "270\n", "491\n", "171\n", "999\n", "108\n", "121\n", "613\n", "643\n", "423\n", "8\n", "29\n", "100\n...
800
500
2
9
697_C. Lorenzo Von Matterhorn
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two...
{ "input": [ "7\n1 3 4 30\n1 4 1 2\n1 3 6 8\n2 4 3\n1 6 1 40\n2 3 7\n2 2 4\n" ], "output": [ "94\n0\n32\n" ] }
{ "input": [ "1\n2 1 343417335313797025\n", "2\n1 100 50 1\n2 4294967396 1\n", "2\n1 562949953421312 562949953421311 1\n2 562949953421312 562949953421311\n", "1\n2 666077344481199252 881371880336470888\n", "2\n1 239841676148963 1 20\n2 2112405731 1\n", "2\n1 4294967298 4294967299 10\n2 2 3\n",...
1,500
500
2
7
718_A. Efim and Strange Grade
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after th...
{ "input": [ "6 2\n10.245\n", "3 100\n9.2\n", "6 1\n10.245\n" ], "output": [ "10.3\n", "9.2\n", "10.25\n" ] }
{ "input": [ "7 1000\n409.659\n", "4 10\n99.9\n", "5 100\n6.666\n", "4 1\n5.59\n", "8 6\n9.444445\n", "9 2\n23999.448\n", "6 1\n0.9454\n", "31 15\n2707786.24030444444444444724166\n", "16 999\n9595959.95959595\n", "7 235562\n999.999\n", "13 1\n761.044449428\n", "5 1\n99....
1,700
500
2
7
73_A. The Elder Trolls IV: Oblivon
Vasya plays The Elder Trolls IV: Oblivon. Oh, those creators of computer games! What they do not come up with! Absolutely unique monsters have been added to the The Elder Trolls IV: Oblivon. One of these monsters is Unkillable Slug. Why it is "Unkillable"? Firstly, because it can be killed with cutting weapon only, so ...
{ "input": [ "2 2 2 1\n", "2 2 2 3\n" ], "output": [ "2\n", "8\n" ] }
{ "input": [ "1000000 1000000 1000000 1000000000\n", "1000 988 1000000 3000\n", "418223 118667 573175 776998\n", "2 2 2 0\n", "2 1000 1000000 1000000000\n", "500000 1000000 750000 100000\n", "797745 854005 98703 735186\n", "100500 5000 500 100000000\n", "781081 414037 495753 892089...
1,600
500
2
7
786_A. Berzerk
Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer. In this game there are n objects numbered from 1 to n arranged in a circle (in clockwise order). Object number 1 is a black hole and the oth...
{ "input": [ "5\n2 3 2\n3 1 2 3\n", "8\n4 6 2 3 4\n2 3 6\n" ], "output": [ "Lose Win Win Loop \nLoop Win Win Win \n", "Win Win Win Win Win Win Win \nLose Win Lose Lose Win Lose Lose \n" ] }
{ "input": [ "1000\n14 77 649 670 988 469 453 445 885 101 58 728 474 488 230\n8 83 453 371 86 834 277 847 958\n", "4096\n6 3736 3640 553 2608 1219 1640\n4 112 2233 3551 2248\n", "7000\n1 6694\n1 2973\n", "17\n1 10\n1 12\n", "100\n66 70 54 10 72 81 84 56 15 27 19 43 55 49 44 52 33 63 40 95 17 58 2 ...
2,000
750
2
7
832_A. Sasha and Sticks
It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends. Today he invented one simple game to play with Lena, with whom he shares a desk. The rules are simple. Sasha draws n sticks in a row. After that the players tak...
{ "input": [ "1 1\n", "10 4\n" ], "output": [ "YES\n", "NO\n" ] }
{ "input": [ "871412474 749817171\n", "257439908778973480 64157133126869976\n", "999999999 1247\n", "6 6\n", "828159210 131819483\n", "851941088 712987048\n", "825175814723458 324\n", "545668929424440387 508692735816921376\n", "2 1\n", "547321411485639939 36665750286082900\n", ...
800
500
2
9
877_C. Slava and tanks
Slava plays his favorite game "Peace Lightning". Now he is flying a bomber on a very specific map. Formally, map is a checkered field of size 1 × n, the cells of which are numbered from 1 to n, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he flies ...
{ "input": [ "3\n", "2\n" ], "output": [ "4\n2 1 3 2\n", "3\n2 1 2\n" ] }
{ "input": [ "11124\n", "10931\n", "4\n", "6591\n", "6\n", "15\n", "10\n", "8954\n", "100\n", "5\n", "23347\n", "23540\n" ], "output": [ "16686\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 8...
1,600
1,500
2
10
900_D. Unusual Sequences
Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers such that gcd(a1, a2, ..., an) = x and <image>. As this number could be large, print the answer modulo 109 + 7. gcd here means the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). Input Th...
{ "input": [ "3 9\n", "5 8\n" ], "output": [ "3\n", "0" ] }
{ "input": [ "2 12\n", "1 223092870\n", "415879151 194713963\n", "859550004 563726557\n", "417485019 230941257\n", "34601 35742833\n", "1000000000 1\n", "1 1\n", "1 9\n", "741547455 471761895\n", "495219 444706662\n", "1 282521795\n", "524 991033864\n", "1 8\n",...
2,000
2,000
2
8
923_B. Producing Snow
Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden. Each day, every pile w...
{ "input": [ "5\n30 25 20 15 10\n9 10 12 4 13\n", "3\n10 10 5\n5 7 2\n" ], "output": [ "9 20 35 11 25 ", "5 12 4 " ] }
{ "input": [ "2\n9 3\n10 2\n", "1\n5\n4\n", "10\n20 35 4 0 6 29 4 9 17 10\n0 9 4 7 5 1 4 3 9 4\n", "1\n4\n5\n", "4\n0 0 0 0\n1 2 3 4\n", "13\n1 1 1 1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1 1 1 1\n", "2\n9 3\n8 2\n", "10\n11 39 16 34 25 3 12 11 31 16\n10 0 4 9 8 9 7 8 9 2\n", "1\n5\...
1,600
1,000
2
10
950_D. A Leapfrog in the Array
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that i...
{ "input": [ "4 3\n2\n3\n4\n", "13 4\n10\n5\n4\n8\n" ], "output": [ "3\n2\n4\n", "13\n3\n8\n9\n" ] }
{ "input": [ "12 12\n9\n11\n5\n3\n7\n2\n8\n6\n4\n10\n12\n1\n", "3 3\n3\n2\n1\n", "2 2\n1\n2\n", "1 1\n1\n" ], "output": [ "5\n6\n3\n2\n4\n7\n12\n8\n10\n9\n11\n1\n", "2\n3\n1\n", "1\n2\n", "1\n" ] }
1,700
1,000
2
13
978_G. Petya's Exams
Petya studies at university. The current academic year finishes with n special days. Petya needs to pass m exams in those special days. The special days in this problem are numbered from 1 to n. There are three values about each exam: * s_i — the day, when questions for the i-th exam will be published, * d_i — t...
{ "input": [ "3 2\n1 3 1\n1 2 1\n", "10 3\n4 7 2\n1 10 3\n8 9 1\n", "5 2\n1 3 1\n1 5 1\n" ], "output": [ "-1\n", "2 2 2 1 1 0 4 3 4 4\n", "1 2 3 0 3\n" ] }
{ "input": [ "100 37\n49 51 2\n79 81 2\n46 48 2\n71 73 2\n31 33 2\n42 44 1\n17 19 2\n64 66 2\n24 26 1\n8 10 2\n38 40 1\n1 3 2\n75 77 2\n52 54 2\n11 13 2\n87 89 1\n98 100 2\n60 62 1\n56 58 2\n39 41 1\n92 94 1\n13 15 1\n67 69 2\n4 6 2\n19 21 1\n91 93 1\n86 88 1\n43 45 1\n25 27 1\n94 96 1\n81 83 1\n35 37 1\n34 36 1\...
1,700
0
2
7
999_A. Mishka and Contest
Mishka started participating in a programming contest. There are n problems in the contest. Mishka's problem-solving skill is equal to k. Mishka arranges all problems from the contest into a list. Because of his weird principles, Mishka only solves problems from one of the ends of the list. Every time, he chooses whic...
{ "input": [ "5 2\n3 1 2 1 3\n", "8 4\n4 2 3 1 5 1 6 4\n", "5 100\n12 34 55 43 21\n" ], "output": [ "0\n", "5\n", "5\n" ] }
{ "input": [ "100 3\n86 53 82 40 2 20 59 2 46 63 75 49 24 81 70 22 9 9 93 72 47 23 29 77 78 51 17 59 19 71 35 3 20 60 70 9 11 96 71 94 91 19 88 93 50 49 72 19 53 30 38 67 62 71 81 86 5 26 5 32 63 98 1 97 22 32 87 65 96 55 43 85 56 37 56 67 12 100 98 58 77 54 18 20 33 53 21 66 24 64 42 71 59 32 51 69 49 79 10 1\n"...
800
0
2
8
1017_B. The Bits
Rudolf is on his way to the castle. Before getting into the castle, the security staff asked him a question: Given two binary numbers a and b of length n. How many different ways of swapping two digits in a (only in a, not b) so that bitwise OR of these two numbers will be changed? In other words, let c be the bitwise...
{ "input": [ "6\n011000\n010011\n", "5\n01011\n11001\n" ], "output": [ "6\n", "4\n" ] }
{ "input": [ "2\n00\n11\n", "2\n00\n00\n", "30\n011110110100010000011001000100\n110111101001011001100001101101\n", "10\n0110101101\n1010000101\n" ], "output": [ "0\n", "0\n", "146\n", "21\n" ] }
1,200
1,000
2
9
1085_C. Connect Three
The Squareland national forest is divided into equal 1 × 1 square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates (x, y) of its south-west corner. Three friends, Alice, Bob, and Charlie are going to buy three distinct plots of land A, B, C i...
{ "input": [ "0 0\n1 1\n2 2\n", "0 0\n2 0\n1 1\n" ], "output": [ "5\n0 0\n1 0\n1 1\n1 2\n2 2\n", "4\n0 0\n1 0\n1 1\n2 0\n" ] }
{ "input": [ "0 1\n1 1\n1 0\n", "0 1\n1 2\n2 1\n", "2 2\n0 0\n1 1\n", "358 161\n868 457\n606 776\n", "0 2\n1 1\n2 0\n", "482 954\n158 954\n998 498\n", "865 651\n504 482\n865 450\n", "1 2\n0 1\n2 0\n", "670 672\n593 626\n593 792\n", "529 294\n529 251\n472 434\n", "488 980\n5...
1,600
1,250
2
7
1105_A. Salem and Sticks
Salem gave you n sticks with integer positive lengths a_1, a_2, …, a_n. For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from a to b is |a - b|, where |x| means the absolute value of x. A stick length a_i is...
{ "input": [ "3\n10 1 4\n", "5\n1 1 2 2 3\n" ], "output": [ "3 7\n", "2 0\n" ] }
{ "input": [ "3\n1 4 4\n", "2\n2 4\n", "4\n1 2 70 71\n", "4\n1 1 9 9\n", "2\n1 100\n", "4\n1 1 5 5\n", "4\n100 54 93 96\n", "10\n1 1 1 1 1 1 1 1 1 9\n", "6\n1 4 10 18 20 25\n", "5\n4 4 4 4 6\n", "5\n1 1 1 100 100\n", "4\n1 2 4 5\n", "4\n1 2 9 10\n", "3\n1 100 10...
1,100
500
2
10
1132_D. Stressful Training
Berland SU holds yet another training contest for its students today. n students came, each of them brought his laptop. However, it turned out that everyone has forgot their chargers! Let students be numbered from 1 to n. Laptop of the i-th student has charge a_i at the beginning of the contest and it uses b_i of char...
{ "input": [ "2 4\n3 2\n4 2\n", "1 6\n4\n2\n", "1 5\n4\n2\n", "2 2\n2 10\n3 15\n" ], "output": [ "5", "2", "1", "-1" ] }
{ "input": [ "7 7\n64 16 50 18 66 25 60\n8 4 10 2 3 5 2\n", "1 1\n1\n2\n", "2 5\n6 3\n4 2\n", "3 200000\n6 4 8\n3 2 2\n", "3 200000\n8 5 9\n1 8 1\n", "7 200000\n63 48 70 14 63 69 40\n9 4 10 7 9 3 8\n", "3 100\n2 7 3\n10 3 5\n", "4 4\n3 4 4 3\n2 2 1 1\n", "7 200000\n66 57 15 65 32 4...
2,300
0
2
11
1174_E. Ehab and the Expected GCD Problem
Let's define a function f(p) on a permutation p as follows. Let g_i be the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of elements p_1, p_2, ..., p_i (in other words, it is the GCD of the prefix of length i). Then f(p) is the number of distinct elements among g_1, g_2, ..., g_...
{ "input": [ "2\n", "3\n", "6\n" ], "output": [ "1\n", "4\n", "120\n" ] }
{ "input": [ "955654\n", "460829\n", "798412\n", "999999\n", "8969\n", "425221\n", "957\n", "28211\n", "629462\n", "5\n", "786432\n", "10\n", "786431\n", "847\n", "217292\n", "73\n", "175863\n", "40\n", "42\n", "89\n", "21504\n", ...
2,500
2,500
2
9
1210_C. Kamil and Making a Stream
Kamil likes streaming the competitive programming videos. His MeTube channel has recently reached 100 million subscribers. In order to celebrate this, he posted a video with an interesting problem he couldn't solve yet. Can you help him? You're given a tree — a connected undirected graph consisting of n vertices conne...
{ "input": [ "7\n0 2 3 0 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n", "5\n4 5 6 0 8\n1 2\n1 3\n1 4\n4 5\n" ], "output": [ "30\n", "42\n" ] }
{ "input": [ "2\n123456789234 987654321432\n1 2\n", "2\n0 0\n2 1\n", "4\n6 10 15 0\n1 4\n2 4\n3 4\n", "2\n987987987987 987987987987\n2 1\n", "8\n1000000000000 0 0 1000000000000 0 0 999999999999 1000000000000\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n", "32\n402528994560 0 0 0 0 0 0 932646223872 8931...
2,000
1,000
2
7
1231_A. Dawid and Bags of Candies
Dawid has four bags of candies. The i-th of them contains a_i candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total? Note, that you can't keep bags for yourself or thro...
{ "input": [ "7 3 2 5\n", "1 7 11 5\n" ], "output": [ "NO\n", "YES\n" ] }
{ "input": [ "26 52 7 19\n", "1 2 3 4\n", "1 1 2 3\n", "7 3 6 3\n", "5 10 1 6\n", "14 9 10 6\n", "1 2 3 3\n", "1 2 2 7\n", "3 1 1 1\n", "2 3 3 4\n", "1 1 14 12\n", "48 14 3 31\n", "70 100 10 86\n", "1 1 4 2\n", "2 4 6 6\n", "1 1 4 5\n", "100 100 100 ...
800
500
2
9
1272_C. Yet Another Broken Keyboard
Recently, Norge found a string s = s_1 s_2 … s_n consisting of n lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string s. Yes, all (n (n + 1))/(2) of them! A substring of s is a non-empty string x = s[a … b] = s_{a} s_{a + 1} … s_{b} (1 ≤ a ≤ b ≤ n). For e...
{ "input": [ "7 2\nabacaba\na b\n", "10 3\nsadfaasdda\nf a d\n", "7 1\naaaaaaa\nb\n" ], "output": [ "12\n", "21\n", "0\n" ] }
{ "input": [ "200 13\nqownuutwuwqnrxxtnlvnqtroztwpnvunynwrzzpsotnrqwxqstxnnzosszovtznquvxwvunpvxqzvyrwxwpxvxnnzzuzarepcqxzrseqqorwpuntzvwqnwuvvuygnpgrrznvootrtcvtxnoowywptwzvwrqwpxusuxqznvoqpnxsrquuzorkxvuwvpxyntrqywqvotuf\na b c d e f g h i j k l m\n", "200 13\nuzqrruuwunntqnotxvtyzoqooznonqyvrpnzppvtowswpyv...
1,200
0
2
10
1295_D. Same GCDs
You are given two integers a and m. Calculate the number of integers x such that 0 ≤ x < m and \gcd(a, m) = \gcd(a + x, m). Note: \gcd(a, b) is the greatest common divisor of a and b. Input The first line contains the single integer T (1 ≤ T ≤ 50) — the number of test cases. Next T lines contain test cases — one pe...
{ "input": [ "3\n4 9\n5 10\n42 9999999967\n" ], "output": [ "6\n1\n9999999966\n" ] }
{ "input": [ "10\n164 252\n94 253\n171 254\n196 255\n35 256\n174 257\n251 258\n239 259\n9 260\n98 261\n", "10\n119 152\n144 153\n41 154\n69 155\n57 156\n91 157\n21 158\n54 159\n105 160\n79 161\n", "10\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n1 8\n7 9\n8 10\n1 11\n", "10\n37 102\n78 103\n31 104\n7 105\n80 106\n5...
1,800
0
2
10
1316_D. Nash Matrix
Nash designed an interesting yet simple board game where a player is simply required to follow instructions written on the cell where the player currently stands. This board game is played on the n× n board. Rows and columns of this board are numbered from 1 to n. The cell on the intersection of the r-th row and c-th...
{ "input": [ "3\n-1 -1 -1 -1 -1 -1\n-1 -1 2 2 -1 -1\n-1 -1 -1 -1 -1 -1\n", "2\n1 1 1 1\n2 2 2 2\n" ], "output": [ "VALID\nDRD\nDXD\nURU\n", "VALID\nXL\nRX\n" ] }
{ "input": [ "10\n-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n-1 -1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 -1 -1\n-1 -1 -1 -1 3 3 3 4 3 5 3 6 3 7 3 8 -1 -1 -1 -1\n-1 -1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 -1 -1 -1 -1\n-1 -1 5 2 5 3 5 3 5 5 5 6 5 7 5 9 5 9 -1 -1\n-1 -1 6 2 6 3 6 5 6 5 6 6 6 7 6 8 6 9 -1 -1\n-1 -...
2,000
1,750
2
11
137_E. Last Chance
Having read half of the book called "Storm and Calm" on the IT lesson, Innocentius was absolutely determined to finish the book on the maths lessons. All was fine until the math teacher Ms. Watkins saw Innocentius reading fiction books instead of solving equations of the fifth degree. As during the last maths class Inn...
{ "input": [ "OEIS\n", "EA\n", "Abo\n", "AaaBRAaaCAaaDAaaBRAaa\n", "auBAAbeelii\n" ], "output": [ "3 1\n", "No solution\n", "3 1\n", "18 4\n", "9 3\n" ] }
{ "input": [ "eEijaiUeefuYpqEUUAmoUAEpiuaDaOOORuaOuaolEOXeAooEinIOwoUUIwukOAbiAOueceUEIOuyzOuDAoiEUImweEhAIIouEfAeepaiAEexiaEiuSiUueaEeEaieeBEiMoEOROZIUIAuoEUHeIEOhUhIeEOOiIehIuaEoELauUeEUIuEiAauUOOeuiXaERAEoOqiaGu\n", "aaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaab\n", "IgwLknyWcuHzTWGUsaXm...
2,000
2,500
2
7
1444_A. Division
Oleg's favorite subjects are History and Math, and his favorite branch of mathematics is division. To improve his division skills, Oleg came up with t pairs of integers p_i and q_i and for each pair decided to find the greatest integer x_i, such that: * p_i is divisible by x_i; * x_i is not divisible by q_i. ...
{ "input": [ "3\n10 4\n12 6\n179 822\n" ], "output": [ "10\n4\n179\n" ] }
{ "input": [ "1\n42034266112 80174\n", "10\n246857872446986130 713202678\n857754240051582063 933416507\n873935277189052612 530795521\n557307185726829409 746530097\n173788420792057536 769449696\n101626841876448103 132345797\n598448092106640578 746411314\n733629261048200000 361714100\n981271355542147402 38\n559...
1,500
500
2
10
1494_D. Dogeforces
The Dogeforces company has k employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employ...
{ "input": [ "3\n2 5 7\n5 1 7\n7 7 4\n" ], "output": [ "\n5\n2 1 4 7 5 \n4\n1 5\n2 5\n5 4\n3 4\n" ] }
{ "input": [ "4\n97 99 100 99\n99 98 100 99\n100 100 99 100\n99 99 100 98\n", "6\n17 20 20 20 19 20\n20 17 18 18 20 18\n20 18 14 16 20 16\n20 18 16 14 20 16\n19 20 20 20 17 20\n20 18 16 16 20 15\n", "9\n98 100 100 100 100 100 100 99 100\n100 96 100 100 99 100 100 100 97\n100 100 98 99 100 100 100 100 100\...
2,300
0
2
9
1517_C. Fillomino 2
Fillomino is a classic logic puzzle. (You do not need to know Fillomino in order to solve this problem.) In one classroom in Yunqi town, some volunteers are playing a board game variant of it: Consider an n by n chessboard. Its rows are numbered from 1 to n from the top to the bottom. Its columns are numbered from 1 t...
{ "input": [ "3\n2 3 1\n", "5\n1 2 3 4 5\n" ], "output": [ "\n2\n2 3\n3 3 1\n", "\n1\n2 2\n3 3 3\n4 4 4 4\n5 5 5 5 5\n" ] }
{ "input": [ "2\n1 2\n", "104\n17 61 71 74 98 16 35 27 9 96 84 38 42 104 103 15 24 40 20 29 86 54 77 68 14 92 65 53 75 100 94 63 101 72 78 31 102 18 26 56 13 91 62 58 60 37 49 6 5 99 7 57 44 52 41 43 12 90 48 4 39 46 28 64 51 1 45 85 47 59 69 73 88 81 19 82 33 10 50 55 83 70 22 89 25 76 36 95 80 66 97 2 34 87...
1,400
1,500
2
8
1545_B. AquaMoon and Chess
Cirno gave AquaMoon a chessboard of size 1 × n. Its cells are numbered with integers from 1 to n from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied. In each operation, AquaMoon can choose a cell i with a pawn, and do either of the following (if po...
{ "input": [ "6\n4\n0110\n6\n011011\n5\n01010\n20\n10001111110110111000\n20\n00110110100110111101\n20\n11101111011000100010\n" ], "output": [ "3\n6\n1\n1287\n1287\n715\n" ] }
{ "input": [ "2\n3\n011\n2\n11\n" ], "output": [ "2\n1\n" ] }
1,900
1,000
2
7
172_A. Phone Code
Polycarpus has n friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they are strings s1, s2, ..., sn. All these strings consist only of digits and have the same length. Once Polycarpus needed to figure out Tarasov city phone code. He assumed that the phone code of the city is the longest comm...
{ "input": [ "4\n00209\n00219\n00999\n00909\n", "2\n1\n2\n", "3\n77012345678999999999\n77012345678901234567\n77012345678998765432\n" ], "output": [ "2\n", "0\n", "12\n" ] }
{ "input": [ "2\n4\n9\n", "10\n4906361343\n8985777485\n1204265609\n7088384855\n4127287014\n7904807820\n3032139021\n5999959109\n6477458281\n3244359368\n", "10\n15424\n10953\n19176\n15514\n16284\n18680\n19305\n13816\n16168\n15924\n", "5\n4491183345\n4491184811\n4491162340\n4491233399\n4491449214\n", ...
800
1,000
2
8
192_B. Walking in the Rain
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from ...
{ "input": [ "5\n10 2 8 3 5\n", "4\n10 3 5 10\n" ], "output": [ "5\n", "5\n" ] }
{ "input": [ "50\n14 4 20 37 50 46 19 20 25 47 10 6 34 12 41 47 9 22 28 41 34 47 40 12 42 9 4 15 15 27 8 38 9 4 17 8 13 47 7 9 38 30 48 50 7 41 34 23 11 16\n", "1\n1\n", "10\n5 17 8 1 10 20 9 18 12 20\n", "25\n220 93 216 467 134 408 132 220 292 11 363 404 282 253 141 313 310 356 214 256 380 81 42 128 ...
1,100
1,000
2
8
216_B. Forming Teams
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B ...
{ "input": [ "6 2\n1 4\n3 4\n", "5 4\n1 2\n2 4\n5 3\n1 4\n", "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n" ], "output": [ "0", "1", "2" ] }
{ "input": [ "4 3\n1 3\n3 2\n2 4\n", "6 5\n1 2\n2 3\n3 4\n4 5\n5 1\n", "20 11\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 1\n", "20 12\n16 20\n8 3\n20 5\n5 10\n17 7\n13 2\n18 9\n17 18\n1 6\n14 4\n11 12\n10 16\n", "6 5\n2 1\n3 4\n2 3\n4 5\n5 6\n", "16 16\n1 2\n2 3\n1 3\n4 5\n5 6\n4...
1,700
1,500
2
8
336_B. Vasily the Bear and Fly
One beautiful day Vasily the bear painted 2m circles of the same radius R on a coordinate plane. Circles with numbers from 1 to m had centers at points (2R - R, 0), (4R - R, 0), ..., (2Rm - R, 0), respectively. Circles with numbers from m + 1 to 2m had centers at points (2R - R, 2R), (4R - R, 2R), ..., (2Rm - R, 2R), r...
{ "input": [ "1 1\n", "2 2\n" ], "output": [ "2.00000000", "5.41421356" ] }
{ "input": [ "92399 1\n", "99999 1\n", "100000 3\n", "1 10\n", "2 4\n", "3333 3\n", "99999 7\n", "6 1\n", "67676 7\n", "3 1\n", "7656 2\n", "43 4\n", "8 8\n", "3134 9\n", "100000 10\n", "4444 4\n", "66666 6\n", "66666 9\n", "2 8\n", "4234...
1,900
1,000
2
9
359_C. Prime Number
Simon has a prime number x and an array of non-negative integers a1, a2, ..., an. Simon loves fractions very much. Today he wrote out number <image> on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: <image>, where number t equals xa1 + a2 + ... + an. Now ...
{ "input": [ "3 3\n1 2 3\n", "4 5\n0 0 0 0\n", "2 2\n2 2\n", "2 2\n29 29\n" ], "output": [ "27\n", "1\n", "8\n", "73741817\n" ] }
{ "input": [ "26 7\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2\n", "3 3\n1 1 1\n", "3 2\n0 1 1\n", "1 127\n1000000000\n", "26 2\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2\n", "1 800000011\n800000011\n", "1 2\n1000000000\n", "1 800000011\n999999999\n" ], "output":...
1,900
1,500
2
8
382_B. Number Busters
Arthur and Alexander are number busters. Today they've got a competition. Arthur took a group of four integers a, b, w, x (0 ≤ b < w, 0 < x < w) and Alexander took integer с. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number...
{ "input": [ "4 2 3 1 7\n", "1 1 2 1 1\n", "4 2 3 1 6\n", "1 2 3 2 6\n" ], "output": [ "4\n", "0\n", "2\n", "13\n" ] }
{ "input": [ "1 999 1000 2 2000000000\n", "10 3 6 5 30\n", "10 1 6 4 20\n", "1999999999 47 1000 527 2000000000\n", "1 504 998 900 2000000000\n", "1 851 999 721 2000000000\n", "1 250 997 55 2000000000\n", "200000000 794 1000 117 2000000000\n", "10 32 312 72 1000\n", "2000000000 ...
2,000
2,500
2
7
403_A. Searching for Graph
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill: * the graph contains exactly 2n + p edges; * the graph doesn't contain self-loops and multiple edges; * for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges. A sub...
{ "input": [ "1\n6 0\n" ], "output": [ "1 2\n1 3\n1 4\n1 5\n1 6\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6\n" ] }
{ "input": [ "5\n15 0\n14 0\n13 0\n12 0\n11 0\n", "5\n24 10\n23 50\n24 228\n24 200\n23 150\n", "5\n6 0\n5 0\n7 0\n8 0\n9 0\n", "5\n6 1\n5 0\n7 1\n8 1\n9 1\n", "5\n19 1\n18 1\n17 1\n16 1\n15 1\n", "5\n10 0\n20 0\n24 0\n19 0\n17 0\n", "5\n15 1\n14 1\n13 1\n12 1\n11 1\n", "5\n21 1\n19 1\n...
1,500
1,500
2
8
430_B. Balls Game
Iahub is training for the IOI. What is a better way to train than playing a Zuma-like game? There are n balls put in a row. Each ball is colored in one of k colors. Initially the row doesn't contain three or more contiguous balls with the same color. Iahub has a single ball of color x. He can insert his ball at any p...
{ "input": [ "1 1 1\n1\n", "6 2 2\n1 1 2 2 1 1\n" ], "output": [ "0", "6" ] }
{ "input": [ "100 2 2\n2 1 1 2 2 1 1 2 1 2 1 1 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 1 2 1 2 1 1 2 2 1 1 2 1 1 2 1 2 2 1 1 2 1 2 1 1 2 2 1 1 2 1 2 1 2 1 2 2 1 1 2 2 1 1 2 2 1 2 1 2 1 1 2 1 1 2 2 1 2 1 2 2 1 2 2 1 1 2 1 2 2 1 2 2\n", "75 5 5\n1 1 5 5 3 5 2 3 3 2 2 1 1 5 4 4 3 4 5 4 3 3 1 2 2 1 2 1 2 5 5 2 1 3 2 2 3 ...
1,400
1,000
2
9
499_C. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the po...
{ "input": [ "1 1\n-1 -1\n2\n0 1 0\n1 0 0\n", "1 1\n-1 -1\n3\n1 0 0\n0 1 0\n1 1 -3\n" ], "output": [ "2", "2" ] }
{ "input": [ "841746 527518\n595261 331297\n10\n-946901 129987 670374\n-140388 -684770 309555\n-302589 415564 -387435\n-565799 -72069 -395358\n-523453 -511446 854898\n-846967 -749453 -341866\n-622388 434663 264157\n-638453 625357 344195\n-255265 -676356 -772398\n-824723 -319141 33585\n", "0 1\n2 2\n1\n1 1 2\n...
1,700
500
2
10
522_D. Closest Equals
You are given sequence a1, a2, ..., an and m queries lj, rj (1 ≤ lj ≤ rj ≤ n). For each query you need to print the minimum distance between such pair of elements ax and ay (x ≠ y), that: * both indexes of the elements lie within range [lj, rj], that is, lj ≤ x, y ≤ rj; * the values of the elements are equal, tha...
{ "input": [ "5 3\n1 1 2 3 2\n1 5\n2 4\n3 5\n", "6 5\n1 2 1 3 2 3\n4 6\n1 3\n2 5\n2 4\n1 6\n" ], "output": [ "1\n-1\n2\n", "2\n2\n3\n-1\n2\n" ] }
{ "input": [ "10 6\n2 2 1 5 6 4 9 8 5 4\n1 2\n1 10\n2 10\n2 9\n5 5\n2 8\n", "1 1\n1\n1 1\n", "2 1\n1 1\n1 1\n", "1 3\n1\n1 1\n1 1\n1 1\n", "2 1\n1 1\n1 2\n", "2 4\n1 2\n1 1\n1 2\n2 2\n1 2\n", "2 5\n1 1\n1 1\n1 2\n2 2\n1 2\n1 1\n" ], "output": [ "1\n1\n4\n5\n-1\n-1\n", "-1\n", ...
2,000
2,000
2
14
575_H. Bots
Sasha and Ira are two best friends. But they aren’t just friends, they are software engineers and experts in artificial intelligence. They are developing an algorithm for two bots playing a two-player game. The game is cooperative and turn based. In each turn, one of the players makes a move (it doesn’t matter which pl...
{ "input": [ "2\n" ], "output": [ "19\n" ] }
{ "input": [ "3\n", "1\n", "9999\n", "5\n", "999999\n", "524287\n", "178481\n", "9\n", "524288\n", "10\n", "131071\n", "4\n", "99999\n", "1000000\n", "99\n", "8\n", "6\n", "7\n", "999\n" ], "output": [ "69\n", "5\n", "70359326...
1,800
0
2
9
598_C. Nearest vectors
You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them. Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 ...
{ "input": [ "6\n-1 0\n0 -1\n1 0\n1 1\n-4 -5\n-4 -6\n", "4\n-1 0\n0 -1\n1 0\n1 1\n" ], "output": [ "5 6\n", "3 4\n" ] }
{ "input": [ "10\n8 6\n-7 -3\n9 8\n7 10\n-3 -8\n3 7\n6 -8\n-9 8\n9 2\n6 7\n", "5\n-6 0\n6 1\n6 -1\n0 6\n0 -6\n", "3\n3 1\n3 -1\n0 3\n", "4\n-6427 6285\n-5386 5267\n3898 -7239\n3905 -7252\n", "4\n6427 -6285\n5386 -5267\n3898 7239\n3905 7252\n", "3\n1 0\n-1 1\n-1 -1\n", "4\n9800 9981\n61 989...
2,300
0
2
7
61_A. Ultra-Fast Mathematician
Shapur was an extremely gifted student. He was great at everything including Combinatorics, Algebra, Number Theory, Geometry, Calculus, etc. He was not only smart but extraordinarily fast! He could manage to sum 1018 numbers in a single second. One day in 230 AD Shapur was trying to find out if any one can possibly do...
{ "input": [ "1010100\n0100101\n", "000\n111\n", "1110\n1010\n", "01110\n01100\n" ], "output": [ "1110001\n", "111\n", "0100\n", "00010\n" ] }
{ "input": [ "0\n0\n", "101000001101111101101111111000001110110010101101010\n010011100111100001100000010001100101000000111011011\n", "0010001011001010001100000010010011110110011000100000000100110000101111001110\n1100110100111000110100001110111001011101001100001010100001010011100110110001\n", "01100110...
800
500
2
9
667_C. Reberland Linguistics
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know l...
{ "input": [ "abacabaca\n", "abaca\n" ], "output": [ "3\naca\nba\nca\n", "0\n" ] }
{ "input": [ "aaaaaxyxxxx\n", "aaaaaxxxxxx\n", "gzqgchv\n", "aaaaaxyxy\n", "glaoyryxrgsysy\n", "xxxxxababc\n", "aaaaaxx\n", "lcrjhbybgamwetyrppxmvvxiyufdkcotwhmptefkqxjhrknjdponulsynpkgszhbkeinpnjdonjfwzbsaweqwlsvuijauwezfydktfljxgclpxpknhygdqyiapvzudyyqomgnsrdhhxhsrdfrwnxdolkmwmw\n", ...
1,800
500