contestId
int64
0
1.01k
name
stringlengths
2
58
tags
sequencelengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
sequencelengths
0
7
demo-output
sequencelengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
863
Turn Off The TV
[ "data structures", "sortings" ]
null
null
Luba needs your help again! Luba has *n* TV sets. She knows that *i*-th TV set will be working from moment of time *l**i* till moment *r**i*, inclusive. Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set. Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
The first line contains one integer number *n* (1<=≤<=*n*<=≤<=2·105) — the number of TV sets. Then *n* lines follow, each of them containing two integer numbers *l**i*,<=*r**i* (0<=≤<=*l**i*<=≤<=*r**i*<=≤<=109) denoting the working time of *i*-th TV set.
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to *n*). If there are multiple answers, print any of them.
[ "3\n1 3\n4 6\n1 7\n", "2\n0 10\n0 10\n", "3\n1 2\n3 4\n6 8\n", "3\n1 2\n2 3\n3 4\n" ]
[ "1\n", "1\n", "-1\n", "2\n" ]
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one). Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].
[ { "input": "3\n1 3\n4 6\n1 7", "output": "1" }, { "input": "2\n0 10\n0 10", "output": "1" }, { "input": "3\n1 2\n3 4\n6 8", "output": "-1" }, { "input": "3\n1 2\n2 3\n3 4", "output": "2" }, { "input": "3\n0 500000000\n500000001 1000000000\n0 1000000000", "output": "1" }, { "input": "3\n1 5\n2 4\n6 10", "output": "2" }, { "input": "10\n4 4\n5 9\n5 7\n2 8\n6 10\n4 10\n1 3\n8 9\n0 0\n5 7", "output": "1" }, { "input": "2\n1 3\n2 4", "output": "-1" }, { "input": "1\n8 9", "output": "-1" }, { "input": "8\n13 17\n83 89\n31 33\n7 13\n52 52\n88 89\n29 30\n16 22", "output": "6" }, { "input": "4\n63 63\n12 34\n17 29\n58 91", "output": "1" }, { "input": "3\n1 10\n5 15\n10 20", "output": "2" }, { "input": "2\n1 3\n1 6", "output": "1" }, { "input": "2\n1 2\n1 3", "output": "1" }, { "input": "3\n5 6\n1 3\n1 4", "output": "2" }, { "input": "3\n1 4\n2 100\n4 5", "output": "3" }, { "input": "4\n1 1\n3 3\n4 7\n4 5", "output": "4" }, { "input": "3\n2 3\n3 4\n1 2", "output": "1" }, { "input": "1\n0 0", "output": "-1" }, { "input": "6\n99 100\n65 65\n34 34\n16 18\n65 67\n88 88", "output": "2" }, { "input": "2\n50 67\n54 64", "output": "2" }, { "input": "3\n1 3\n2 100\n3 5", "output": "3" }, { "input": "3\n57 90\n35 45\n18 52", "output": "2" }, { "input": "4\n14 15\n46 73\n15 40\n28 53", "output": "-1" }, { "input": "3\n37 38\n51 54\n28 28", "output": "-1" }, { "input": "2\n64 66\n47 61", "output": "-1" }, { "input": "4\n50 68\n63 67\n67 69\n11 12", "output": "2" }, { "input": "4\n42 62\n93 103\n34 62\n5 12", "output": "1" }, { "input": "6\n42 60\n78 107\n6 38\n58 81\n70 105\n70 105", "output": "5" }, { "input": "5\n71 71\n21 22\n58 58\n57 57\n16 16", "output": "-1" }, { "input": "7\n28 42\n70 75\n83 92\n19 22\n26 32\n85 99\n30 39", "output": "7" }, { "input": "3\n8 28\n80 110\n39 81", "output": "-1" }, { "input": "7\n90 115\n87 113\n2 26\n39 40\n91 112\n42 53\n65 79", "output": "5" }, { "input": "7\n12 13\n26 28\n9 11\n15 15\n8 10\n22 24\n5 7", "output": "-1" }, { "input": "7\n3 5\n26 31\n11 15\n2 4\n16 18\n4 4\n7 12", "output": "6" }, { "input": "3\n1 5\n1 2\n4 5", "output": "2" }, { "input": "3\n999999995 999999997\n999999998 1000000000\n999999996 999999999", "output": "3" }, { "input": "4\n1 2\n4 6\n4 10\n200 300", "output": "2" } ]
2,000
59,801,600
0
7,710
0
none
[ "none" ]
null
null
You've got array *A*, consisting of *n* integers and a positive integer *k*. Array *A* is indexed by integers from 1 to *n*. You need to permute the array elements so that value
The first line contains two integers *n*,<=*k* (2<=≤<=*n*<=≤<=3·105, 1<=≤<=*k*<=≤<=*min*(5000,<=*n*<=-<=1)). The second line contains *n* integers *A*[1],<=*A*[2],<=...,<=*A*[*n*] (<=-<=109<=≤<=*A*[*i*]<=≤<=109), separate by spaces — elements of the array *A*.
Print the minimum possible value of the sum described in the statement.
[ "3 2\n1 2 4\n", "5 2\n3 -5 3 -5 3\n", "6 3\n4 3 4 3 2 5\n" ]
[ "1\n", "0\n", "3\n" ]
In the first test one of the optimal permutations is 1 4 2. In the second test the initial order is optimal. In the third test one of the optimal permutations is 2 3 4 4 3 5.
[]
46
0
0
7,723
653
Bear and Forgotten Tree 2
[ "dfs and similar", "dsu", "graphs", "trees" ]
null
null
A tree is a connected undirected graph consisting of *n* vertices and *n*<=<=-<=<=1 edges. Vertices are numbered 1 through *n*. Limak is a little polar bear. He once had a tree with *n* vertices but he lost it. He still remembers something about the lost tree though. You are given *m* pairs of vertices (*a*1,<=*b*1),<=(*a*2,<=*b*2),<=...,<=(*a**m*,<=*b**m*). Limak remembers that for each *i* there was no edge between *a**i* and *b**i*. He also remembers that vertex 1 was incident to exactly *k* edges (its degree was equal to *k*). Is it possible that Limak remembers everything correctly? Check whether there exists a tree satisfying the given conditions.
The first line of the input contains three integers *n*, *m* and *k* () — the number of vertices in Limak's tree, the number of forbidden pairs of vertices, and the degree of vertex 1, respectively. The *i*-th of next *m* lines contains two distinct integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*,<=*a**i*<=≠<=*b**i*) — the *i*-th pair that is forbidden. It's guaranteed that each pair of vertices will appear at most once in the input.
Print "possible" (without quotes) if there exists at least one tree satisfying the given conditions. Otherwise, print "impossible" (without quotes).
[ "5 4 2\n1 2\n2 3\n4 2\n4 1\n", "6 5 3\n1 2\n1 3\n1 4\n1 5\n1 6\n" ]
[ "possible\n", "impossible\n" ]
In the first sample, there are *n* = 5 vertices. The degree of vertex 1 should be *k* = 2. All conditions are satisfied for a tree with edges 1 - 5, 5 - 2, 1 - 3 and 3 - 4. In the second sample, Limak remembers that none of the following edges existed: 1 - 2, 1 - 3, 1 - 4, 1 - 5 and 1 - 6. Hence, vertex 1 couldn't be connected to any other vertex and it implies that there is no suitable tree.
[ { "input": "5 4 2\n1 2\n2 3\n4 2\n4 1", "output": "possible" }, { "input": "6 5 3\n1 2\n1 3\n1 4\n1 5\n1 6", "output": "impossible" }, { "input": "4 3 2\n2 3\n2 4\n3 4", "output": "impossible" }, { "input": "4 2 2\n1 2\n1 3", "output": "impossible" }, { "input": "7 11 1\n1 2\n1 3\n1 4\n1 5\n1 7\n6 2\n6 3\n6 4\n6 5\n6 7\n2 3", "output": "impossible" }, { "input": "3 1 2\n1 3", "output": "impossible" }, { "input": "4 2 2\n2 1\n1 4", "output": "impossible" }, { "input": "4 2 3\n2 1\n2 4", "output": "impossible" }, { "input": "4 5 1\n2 3\n2 4\n4 3\n3 1\n1 2", "output": "impossible" }, { "input": "5 2 4\n4 3\n1 3", "output": "impossible" }, { "input": "5 3 1\n2 1\n4 3\n3 1", "output": "possible" }, { "input": "5 3 2\n1 2\n5 1\n1 4", "output": "impossible" }, { "input": "5 3 3\n5 4\n2 4\n2 1", "output": "possible" }, { "input": "5 4 2\n2 1\n4 1\n4 2\n3 5", "output": "possible" }, { "input": "5 4 3\n5 1\n5 3\n3 1\n4 2", "output": "impossible" }, { "input": "5 4 4\n4 5\n4 2\n1 5\n5 3", "output": "impossible" }, { "input": "5 5 2\n2 1\n3 1\n5 3\n3 2\n3 4", "output": "impossible" }, { "input": "5 6 1\n3 1\n4 5\n3 5\n4 3\n1 2\n2 4", "output": "impossible" }, { "input": "5 6 2\n3 5\n2 1\n2 5\n1 5\n1 3\n2 4", "output": "impossible" }, { "input": "5 6 3\n3 1\n4 3\n2 5\n4 5\n2 4\n3 2", "output": "possible" }, { "input": "5 8 1\n1 4\n4 3\n1 5\n3 5\n2 4\n1 2\n5 2\n4 5", "output": "impossible" }, { "input": "2 0 1", "output": "possible" }, { "input": "2 1 1\n2 1", "output": "impossible" }, { "input": "300000 0 1", "output": "possible" }, { "input": "300000 0 299999", "output": "possible" } ]
2,000
11,776,000
0
7,739
1,000
Codehorses T-shirts
[ "greedy", "implementation" ]
null
null
Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners. The valid sizes of T-shirts are either "M" or from $0$ to $3$ "X" followed by "S" or "L". For example, sizes "M", "XXS", "L", "XXXL" are valid and "XM", "Z", "XXXXL" are not. There are $n$ winners to the cup for both the previous year and the current year. Ksenia has a list with the T-shirt sizes printed for the last year cup and is yet to send the new list to the printing office. Organizers want to distribute the prizes as soon as possible, so now Ksenia is required not to write the whole list from the scratch but just make some changes to the list of the previous year. In one second she can choose arbitrary position in any word and replace its character with some uppercase Latin letter. Ksenia can't remove or add letters in any of the words. What is the minimal number of seconds Ksenia is required to spend to change the last year list to the current one? The lists are unordered. That means, two lists are considered equal if and only if the number of occurrences of any string is the same in both lists.
The first line contains one integer $n$ ($1 \le n \le 100$) — the number of T-shirts. The $i$-th of the next $n$ lines contains $a_i$ — the size of the $i$-th T-shirt of the list for the previous year. The $i$-th of the next $n$ lines contains $b_i$ — the size of the $i$-th T-shirt of the list for the current year. It is guaranteed that all the sizes in the input are valid. It is also guaranteed that Ksenia can produce list $b$ from the list $a$.
Print the minimal number of seconds Ksenia is required to spend to change the last year list to the current one. If the lists are already equal, print 0.
[ "3\nXS\nXS\nM\nXL\nS\nXS\n", "2\nXXXL\nXXL\nXXL\nXXXS\n", "2\nM\nXS\nXS\nM\n" ]
[ "2\n", "1\n", "0\n" ]
In the first example Ksenia can replace "M" with "S" and "S" in one of the occurrences of "XS" with "L". In the second example Ksenia should replace "L" in "XXXL" with "S". In the third example lists are equal.
[ { "input": "3\nXS\nXS\nM\nXL\nS\nXS", "output": "2" }, { "input": "2\nXXXL\nXXL\nXXL\nXXXS", "output": "1" }, { "input": "2\nM\nXS\nXS\nM", "output": "0" }, { "input": "1\nXXXL\nXXXL", "output": "0" }, { "input": "1\nM\nM", "output": "0" }, { "input": "4\nS\nL\nXXS\nXXS\nM\nL\nXXS\nXXL", "output": "2" }, { "input": "5\nXS\nXXXS\nL\nXS\nM\nXL\nS\nXS\nM\nXXXL", "output": "3" }, { "input": "1\nS\nM", "output": "1" }, { "input": "26\nS\nL\nM\nL\nS\nXXXS\nXXXS\nM\nXXXS\nXXXS\nM\nXXS\nM\nXXXL\nM\nXL\nXXXL\nXXXS\nXXL\nL\nM\nM\nL\nXL\nXS\nXXL\nXS\nXXXS\nXL\nXXXL\nXXXL\nL\nL\nXXXS\nL\nL\nXXL\nXXXS\nL\nM\nM\nXXL\nS\nL\nXXL\nXXXS\nL\nS\nL\nL\nXL\nXXXL", "output": "7" }, { "input": "43\nL\nXXXL\nM\nXS\nL\nXXL\nS\nL\nM\nXXXL\nXXS\nS\nXS\nS\nXS\nXL\nXXL\nXXL\nXS\nXXS\nXXL\nL\nXXXL\nL\nL\nXXL\nXXL\nM\nXXXS\nM\nXXL\nXXXS\nXXXL\nXXS\nL\nL\nXS\nL\nXS\nXS\nL\nXXL\nM\nXXXL\nXXL\nXXL\nXL\nL\nXS\nM\nXL\nXXXS\nXXXS\nS\nXXL\nXXXL\nXXL\nXS\nXXS\nL\nS\nXXXS\nXL\nL\nXL\nXXXS\nM\nL\nXXL\nL\nXL\nM\nM\nL\nXXS\nS\nXXS\nXS\nXXL\nL\nM\nS\nL\nXXS\nXXS\nM", "output": "10" }, { "input": "5\nXXXL\nXXL\nXL\nL\nM\nXXXS\nXXS\nXS\nS\nM", "output": "4" }, { "input": "1\nL\nM", "output": "1" }, { "input": "2\nL\nS\nS\nM", "output": "1" }, { "input": "13\nS\nM\nL\nS\nL\nL\nXL\nS\nXL\nXXL\nXL\nXXL\nM\nM\nXS\nM\nXXL\nL\nM\nXXS\nXS\nM\nS\nS\nXS\nL", "output": "6" }, { "input": "14\nXXL\nS\nS\nXXXL\nXL\nXXXS\nXS\nS\nS\nXL\nM\nXXXS\nM\nS\nL\nXXS\nXXXS\nS\nL\nXL\nL\nXXXL\nXS\nXL\nS\nM\nS\nXXXS", "output": "4" }, { "input": "2\nL\nM\nS\nL", "output": "1" }, { "input": "9\nM\nXXL\nXL\nL\nS\nXS\nXXL\nXL\nXXS\nS\nXXL\nXS\nXS\nS\nXXL\nXL\nXXS\nM", "output": "2" }, { "input": "6\nM\nXXS\nXXL\nXXL\nL\nL\nXXS\nXXL\nS\nXXS\nL\nL", "output": "2" }, { "input": "1\nL\nS", "output": "1" }, { "input": "11\nM\nS\nS\nXXXS\nS\nXXXS\nM\nM\nL\nXXXL\nXL\nXXXL\nM\nM\nL\nXXXS\nXXXS\nL\nM\nL\nM\nXS", "output": "4" }, { "input": "8\nXL\nXS\nS\nXXXL\nXXXL\nXL\nXXXL\nS\nXS\nXXXS\nXL\nL\nXXXS\nM\nXS\nXXXL", "output": "5" }, { "input": "2\nL\nL\nM\nM", "output": "2" }, { "input": "6\nL\nS\nXS\nM\nXL\nXXS\nXS\nM\nL\nXL\nXXL\nL", "output": "2" }, { "input": "2\nL\nS\nM\nL", "output": "1" }, { "input": "6\nXXL\nXXS\nXXXS\nL\nXXXS\nXXXL\nXXL\nL\nXXS\nXXXS\nXXXL\nXXXL", "output": "1" }, { "input": "5\nXXS\nXXS\nXXL\nXXXS\nL\nXXS\nXXXL\nS\nXXS\nXXS", "output": "3" }, { "input": "8\nXXL\nS\nXXS\nXXXS\nXXXL\nXL\nXXL\nS\nXXXS\nM\nXXS\nXXL\nXXS\nXXXL\nS\nXS", "output": "3" }, { "input": "3\nL\nL\nM\nL\nM\nS", "output": "1" }, { "input": "5\nXXXL\nXXXS\nXXXL\nXL\nL\nXXXS\nS\nXS\nXXXL\nXXXS", "output": "3" }, { "input": "10\nXXXS\nXXXL\nXXL\nM\nXXXS\nXS\nL\nXL\nXXS\nM\nM\nM\nXL\nXXL\nXXXS\nXL\nXXXS\nXXXS\nL\nXXS", "output": "2" }, { "input": "2\nM\nL\nM\nS", "output": "1" }, { "input": "1\nXXXS\nXXXL", "output": "1" }, { "input": "1\nXS\nXL", "output": "1" }, { "input": "5\nL\nS\nL\nXL\nXL\nXL\nXS\nM\nL\nM", "output": "3" } ]
92
0
3
7,759
18
Triangle
[ "brute force", "geometry" ]
A. Triangle
2
64
At a geometry lesson Bob learnt that a triangle is called right-angled if it is nondegenerate and one of its angles is right. Bob decided to draw such a triangle immediately: on a sheet of paper he drew three points with integer coordinates, and joined them with segments of straight lines, then he showed the triangle to Peter. Peter said that Bob's triangle is not right-angled, but is almost right-angled: the triangle itself is not right-angled, but it is possible to move one of the points exactly by distance 1 so, that all the coordinates remain integer, and the triangle become right-angled. Bob asks you to help him and find out if Peter tricks him. By the given coordinates of the triangle you should find out if it is right-angled, almost right-angled, or neither of these.
The first input line contains 6 space-separated integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=*x*3,<=*y*3 — coordinates of the triangle's vertices. All the coordinates are integer and don't exceed 100 in absolute value. It's guaranteed that the triangle is nondegenerate, i.e. its total area is not zero.
If the given triangle is right-angled, output RIGHT, if it is almost right-angled, output ALMOST, and if it is neither of these, output NEITHER.
[ "0 0 2 0 0 1\n", "2 3 4 5 6 6\n", "-1 0 2 0 0 1\n" ]
[ "RIGHT\n", "NEITHER\n", "ALMOST\n" ]
none
[ { "input": "0 0 2 0 0 1", "output": "RIGHT" }, { "input": "2 3 4 5 6 6", "output": "NEITHER" }, { "input": "-1 0 2 0 0 1", "output": "ALMOST" }, { "input": "27 74 85 23 100 99", "output": "NEITHER" }, { "input": "-97 -19 17 62 30 -76", "output": "NEITHER" }, { "input": "28 -15 86 32 98 -41", "output": "NEITHER" }, { "input": "-66 24 8 -29 17 62", "output": "NEITHER" }, { "input": "-83 40 -80 52 -71 43", "output": "NEITHER" }, { "input": "-88 67 -62 37 -49 75", "output": "NEITHER" }, { "input": "58 45 6 22 13 79", "output": "NEITHER" }, { "input": "75 86 -82 89 -37 -35", "output": "NEITHER" }, { "input": "34 74 -2 -95 63 -33", "output": "NEITHER" }, { "input": "-7 63 78 74 -39 -30", "output": "NEITHER" }, { "input": "-49 -99 7 92 61 -28", "output": "NEITHER" }, { "input": "-90 90 87 -92 -40 -26", "output": "NEITHER" }, { "input": "-100 -100 100 -100 0 73", "output": "NEITHER" }, { "input": "39 22 94 25 69 -23", "output": "NEITHER" }, { "input": "100 100 -100 100 1 -73", "output": "NEITHER" }, { "input": "0 0 0 1 1 0", "output": "RIGHT" }, { "input": "-100 -100 100 100 -100 100", "output": "RIGHT" }, { "input": "29 83 35 35 74 65", "output": "NEITHER" }, { "input": "28 -15 86 32 -19 43", "output": "RIGHT" }, { "input": "-28 12 -97 67 -83 -57", "output": "RIGHT" }, { "input": "-83 40 -80 52 -79 39", "output": "RIGHT" }, { "input": "30 8 49 13 25 27", "output": "RIGHT" }, { "input": "23 6 63 -40 69 46", "output": "RIGHT" }, { "input": "49 -7 19 -76 26 3", "output": "RIGHT" }, { "input": "0 0 1 0 2 1", "output": "ALMOST" }, { "input": "0 0 1 0 3 1", "output": "ALMOST" }, { "input": "0 0 1 0 2 2", "output": "ALMOST" }, { "input": "0 0 1 0 4 1", "output": "NEITHER" }, { "input": "0 0 1 0 100 1", "output": "NEITHER" }, { "input": "60 4 90 -53 32 -12", "output": "ALMOST" }, { "input": "52 -34 -37 -63 23 54", "output": "ALMOST" }, { "input": "39 22 95 25 42 -33", "output": "ALMOST" }, { "input": "-10 -11 62 6 -12 -3", "output": "ALMOST" }, { "input": "22 -15 -24 77 -69 -60", "output": "ALMOST" }, { "input": "99 85 90 87 64 -20", "output": "ALMOST" }, { "input": "-50 -37 -93 -6 -80 -80", "output": "ALMOST" }, { "input": "4 -13 4 -49 -24 -13", "output": "RIGHT" }, { "input": "0 -3 -3 -10 4 -7", "output": "NEITHER" }, { "input": "-45 -87 -34 -79 -60 -62", "output": "NEITHER" }, { "input": "-67 49 89 -76 -37 87", "output": "NEITHER" }, { "input": "22 32 -33 -30 -18 68", "output": "NEITHER" }, { "input": "36 1 -17 -54 -19 55", "output": "ALMOST" }, { "input": "55 44 15 14 23 83", "output": "NEITHER" }, { "input": "-19 0 -89 -54 25 -57", "output": "NEITHER" }, { "input": "69 -45 1 11 56 -63", "output": "NEITHER" }, { "input": "72 68 56 72 33 -88", "output": "RIGHT" }, { "input": "59 86 74 -49 77 88", "output": "RIGHT" }, { "input": "-50 0 0 50 0 -50", "output": "RIGHT" }, { "input": "-50 0 0 50 0 -51", "output": "ALMOST" }, { "input": "-50 0 0 50 0 -49", "output": "ALMOST" }, { "input": "-50 0 0 50 1 -50", "output": "ALMOST" }, { "input": "-50 0 0 50 -1 -50", "output": "ALMOST" }, { "input": "-50 0 0 49 0 -50", "output": "ALMOST" }, { "input": "-50 0 0 51 0 -50", "output": "ALMOST" }, { "input": "-50 0 1 50 0 -50", "output": "ALMOST" }, { "input": "-50 0 -1 50 0 -50", "output": "ALMOST" }, { "input": "-50 1 0 50 0 -50", "output": "ALMOST" }, { "input": "-50 -1 0 50 0 -50", "output": "ALMOST" }, { "input": "-51 0 0 50 0 -50", "output": "ALMOST" }, { "input": "-49 0 0 50 0 -50", "output": "ALMOST" } ]
216
0
0
7,766
921
Labyrinth-5
[]
null
null
See the problem statement here: [http://codeforces.com/contest/921/problem/01](//codeforces.com/contest/921/problem/01).
none
none
[]
[]
none
[]
30
5,632,000
2
7,768
526
Om Nom and Candies
[ "brute force", "greedy", "math" ]
null
null
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs *W**r* grams and each blue candy weighs *W**b* grams. Eating a single red candy gives Om Nom *H**r* joy units and eating a single blue candy gives Om Nom *H**b* joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than *C* grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
The single line contains five integers *C*,<=*H**r*,<=*H**b*,<=*W**r*,<=*W**b* (1<=≤<=*C*,<=*H**r*,<=*H**b*,<=*W**r*,<=*W**b*<=≤<=109).
Print a single integer — the maximum number of joy units that Om Nom can get.
[ "10 3 5 2 3\n" ]
[ "16\n" ]
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
[ { "input": "10 3 5 2 3", "output": "16" }, { "input": "5 3 1 6 7", "output": "0" }, { "input": "982068341 55 57 106 109", "output": "513558662" }, { "input": "930064129 32726326 25428197 83013449 64501049", "output": "363523396" }, { "input": "927155987 21197 15994 54746 41309", "output": "358983713" }, { "input": "902303498 609628987 152407246 8 2", "output": "68758795931537065" }, { "input": "942733698 9180 9072 1020 1008", "output": "8484603228" }, { "input": "951102310 39876134 24967176 70096104 43888451", "output": "539219654" }, { "input": "910943911 107 105 60 59", "output": "1624516635" }, { "input": "910943911 38162 31949 67084 56162", "output": "518210503" }, { "input": "910943911 9063 9045 1007 1005", "output": "8198495199" }, { "input": "903796108 270891702 270891702 1 1", "output": "244830865957095816" }, { "input": "936111602 154673223 309346447 1 2", "output": "144791399037089047" }, { "input": "947370735 115930744 347792233 1 3", "output": "109829394468167085" }, { "input": "958629867 96557265 386229061 1 4", "output": "92562678344491221" }, { "input": "969889000 84931386 424656931 1 5", "output": "82374017230131800" }, { "input": "925819493 47350513 28377591 83230978 49881078", "output": "520855643" }, { "input": "934395168 119 105 67 59", "output": "1662906651" }, { "input": "934395168 29208 38362 51342 67432", "output": "531576348" }, { "input": "934395168 9171 9045 1019 1005", "output": "8409556512" }, { "input": "946401698 967136832 483568416 2 1", "output": "457649970001570368" }, { "input": "962693577 967217455 967217455 2 2", "output": "465567015261784540" }, { "input": "989976325 646076560 969114840 2 3", "output": "319800249268721000" }, { "input": "901235456 485501645 971003291 2 4", "output": "218775648435471424" }, { "input": "912494588 389153108 972882772 2 5", "output": "177550052841687584" }, { "input": "995503930 29205027 18903616 51333090 33226507", "output": "565303099" }, { "input": "983935533 115 108 65 61", "output": "1742049794" }, { "input": "983935533 33986 27367 59737 48104", "output": "559787479" }, { "input": "983935533 7105 7056 1015 1008", "output": "6887548731" }, { "input": "994040035 740285170 246761723 3 1", "output": "245291032098926983" }, { "input": "905299166 740361314 493574209 3 2", "output": "223416160034288041" }, { "input": "911525551 740437472 740437472 3 3", "output": "224975891301803200" }, { "input": "922784684 566833132 755777509 3 4", "output": "174354977531116762" }, { "input": "955100178 462665160 771108601 3 5", "output": "147297192414486195" }, { "input": "949164751 36679609 23634069 64467968 41539167", "output": "537909080" }, { "input": "928443151 60 63 106 112", "output": "525533853" }, { "input": "928443151 25031 33442 43995 58778", "output": "528241752" }, { "input": "928443151 1006 1012 1006 1012", "output": "928443150" }, { "input": "936645623 540336743 135084185 4 1", "output": "126526011319256470" }, { "input": "947904756 540408420 270204210 4 2", "output": "128063927875111380" }, { "input": "959163888 540480074 405360055 4 3", "output": "129602242291091928" }, { "input": "970423020 540551739 540551739 4 4", "output": "131140962756657945" }, { "input": "976649406 455467553 569334442 4 5", "output": "111208028918928288" }, { "input": "923881933 18531902 53987967 32570076 94884602", "output": "524563246" }, { "input": "977983517 57 63 101 112", "output": "551931291" }, { "input": "977983517 29808 22786 52389 40047", "output": "556454318" }, { "input": "977983517 9009 9108 1001 1012", "output": "8801851608" }, { "input": "984283960 367291526 73458305 5 1", "output": "72303831537144592" }, { "input": "990510345 367358723 146943489 5 2", "output": "72774523091497887" }, { "input": "901769477 367425909 220455545 5 3", "output": "66266693959035917" }, { "input": "907995862 367493085 293994468 5 4", "output": "66736440098722854" }, { "input": "924287742 367560271 367560271 5 5", "output": "67946290439275508" }, { "input": "1000000000 1000 999 100 1000000000", "output": "10000000000" }, { "input": "999999999 10 499999995 2 99999999", "output": "4999999995" }, { "input": "999999999 1 1000000000 2 1000000000", "output": "499999999" }, { "input": "999999997 2 999999997 2 999999997", "output": "999999997" }, { "input": "1000000000 1 1 11 11", "output": "90909090" }, { "input": "999999999 999999998 5 999999999 5", "output": "999999998" }, { "input": "100000001 3 100000000 3 100000001", "output": "100000000" }, { "input": "999999999 2 3 1 2", "output": "1999999998" }, { "input": "1000000000 2 1 3 4", "output": "666666666" }, { "input": "999999999 10000 494999 2 99", "output": "4999999994999" }, { "input": "1000000000 1 1 1 1", "output": "1000000000" }, { "input": "998999 1000 999 1000 999", "output": "998999" }, { "input": "3 100 101 2 3", "output": "101" }, { "input": "345415838 13999 13997 13999 13997", "output": "345415838" }, { "input": "5000005 3 2 5 1", "output": "10000010" }, { "input": "1000000000 1 1 1 1000000000", "output": "1000000000" }, { "input": "999999999 3 2 10 3", "output": "666666666" }, { "input": "1000000000 1000 1000 1 1", "output": "1000000000000" }, { "input": "200000001 100000002 1 100000001 1", "output": "200000002" }, { "input": "100000000 1000000000 1 100000001 1", "output": "100000000" }, { "input": "1000000000 99 100 1 2", "output": "99000000000" }, { "input": "1000000000 5 5 1 1", "output": "5000000000" }, { "input": "1000000000 1 1000000000 1 1000000000", "output": "1000000000" } ]
312
2,457,600
3
7,773
48
Land Lot
[ "brute force", "implementation" ]
B. Land Lot
2
256
Vasya has a beautiful garden where wonderful fruit trees grow and yield fantastic harvest every year. But lately thieves started to sneak into the garden at nights and steal the fruit too often. Vasya can’t spend the nights in the garden and guard the fruit because there’s no house in the garden! Vasya had been saving in for some time and finally he decided to build the house. The rest is simple: he should choose in which part of the garden to build the house. In the evening he sat at his table and drew the garden’s plan. On the plan the garden is represented as a rectangular checkered field *n*<=×<=*m* in size divided into squares whose side length is 1. In some squares Vasya marked the trees growing there (one shouldn’t plant the trees too close to each other that’s why one square contains no more than one tree). Vasya wants to find a rectangular land lot *a*<=×<=*b* squares in size to build a house on, at that the land lot border should go along the lines of the grid that separates the squares. All the trees that grow on the building lot will have to be chopped off. Vasya loves his garden very much, so help him choose the building land lot location so that the number of chopped trees would be as little as possible.
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=50) which represent the garden location. The next *n* lines contain *m* numbers 0 or 1, which describe the garden on the scheme. The zero means that a tree doesn’t grow on this square and the 1 means that there is a growing tree. The last line contains two integers *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=50). Note that Vasya can choose for building an *a*<=×<=*b* rectangle as well a *b*<=×<=*a* one, i.e. the side of the lot with the length of *a* can be located as parallel to the garden side with the length of *n*, as well as parallel to the garden side with the length of *m*.
Print the minimum number of trees that needs to be chopped off to select a land lot *a*<=×<=*b* in size to build a house on. It is guaranteed that at least one lot location can always be found, i. e. either *a*<=≤<=*n* and *b*<=≤<=*m*, or *a*<=≤<=*m* и *b*<=≤<=*n*.
[ "2 2\n1 0\n1 1\n1 1\n", "4 5\n0 0 1 0 1\n0 1 1 1 0\n1 0 1 0 1\n1 1 1 1 1\n2 3\n" ]
[ "0\n", "2\n" ]
In the second example the upper left square is (1,1) and the lower right is (3,2).
[ { "input": "2 2\n1 0\n1 1\n1 1", "output": "0" }, { "input": "4 5\n0 0 1 0 1\n0 1 1 1 0\n1 0 1 0 1\n1 1 1 1 1\n2 3", "output": "2" }, { "input": "3 3\n0 0 0\n0 0 0\n0 0 0\n1 2", "output": "0" }, { "input": "3 3\n1 1 1\n1 1 1\n1 1 1\n2 1", "output": "2" }, { "input": "3 2\n1 1\n1 1\n1 0\n2 1", "output": "1" }, { "input": "2 3\n1 0 1\n0 1 0\n3 2", "output": "3" }, { "input": "1 1\n0\n1 1", "output": "0" }, { "input": "1 1\n1\n1 1", "output": "1" }, { "input": "3 4\n1 0 1 0\n0 1 0 1\n1 0 1 0\n2 2", "output": "2" }, { "input": "3 4\n1 1 1 1\n1 0 0 1\n1 1 1 1\n3 1", "output": "1" }, { "input": "10 10\n1 1 1 0 0 0 0 1 1 0\n1 1 1 0 1 1 0 1 1 1\n1 0 1 1 0 1 1 1 1 0\n0 1 1 1 1 1 1 1 1 1\n1 1 1 1 0 1 1 1 1 1\n1 1 1 1 0 0 1 1 1 1\n1 1 1 1 0 1 1 1 0 1\n0 1 1 1 1 1 1 0 1 0\n1 1 1 1 1 0 0 1 0 1\n1 1 0 1 0 1 1 1 1 0\n5 4", "output": "12" }, { "input": "10 10\n0 1 1 1 1 1 1 0 1 1\n0 1 1 1 1 1 0 0 1 1\n1 1 0 0 1 1 0 0 0 0\n0 0 0 0 1 0 1 1 1 0\n1 0 1 0 1 0 1 1 1 1\n1 0 0 1 1 1 1 1 0 1\n0 0 0 1 1 0 1 1 1 0\n1 0 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n0 0 0 1 1 0 0 1 1 1\n1 10", "output": "4" }, { "input": "10 10\n1 0 1 1 1 1 0 0 1 1\n1 1 1 1 1 1 1 1 0 1\n1 0 0 1 1 1 1 1 1 1\n1 0 1 1 1 1 0 1 1 1\n0 0 1 0 1 1 1 1 1 1\n1 1 1 0 0 1 1 1 1 1\n0 1 1 0 1 1 0 1 1 0\n1 0 1 1 1 0 1 1 1 1\n1 0 1 1 1 0 1 1 0 1\n1 1 0 1 1 1 0 0 1 0\n10 1", "output": "4" }, { "input": "10 7\n0 1 1 0 0 1 1\n1 1 0 0 0 0 1\n0 1 0 0 0 1 0\n0 1 0 1 1 1 1\n1 1 0 1 0 0 1\n0 1 0 0 0 0 0\n0 1 0 0 1 0 1\n0 1 0 1 1 0 0\n1 1 0 1 1 1 0\n1 1 0 0 0 1 0\n1 8", "output": "0" }, { "input": "10 8\n1 1 0 1 1 1 0 0\n0 1 0 1 1 1 1 1\n1 1 0 0 1 0 0 1\n0 1 1 1 1 0 1 0\n0 1 1 0 1 1 0 1\n0 1 1 0 0 1 0 1\n1 0 0 0 1 1 0 1\n0 1 1 0 1 1 1 1\n0 1 1 1 0 1 0 1\n1 1 0 1 1 0 1 1\n4 9", "output": "20" }, { "input": "10 10\n1 0 1 1 1 1 1 1 1 1\n1 1 1 0 1 1 0 1 1 1\n1 1 1 0 1 1 1 1 0 1\n1 1 0 1 1 1 0 0 0 1\n0 1 0 1 1 1 0 1 1 1\n1 0 1 0 1 0 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 0 1 1\n1 1 1 1 0 1 1 1 1 1\n0 1 1 1 1 0 1 1 0 1\n10 10", "output": "80" }, { "input": "10 10\n0 1 1 0 0 0 1 0 0 0\n0 0 1 1 1 1 0 1 0 0\n1 1 0 1 1 0 0 1 0 0\n1 0 0 0 0 0 0 0 1 0\n0 0 0 1 0 0 0 1 0 0\n0 1 0 0 1 0 0 0 1 0\n0 1 0 1 1 1 1 0 0 0\n1 0 0 1 0 1 0 0 0 0\n0 0 0 0 1 0 0 0 0 0\n1 1 0 0 0 0 0 0 1 0\n3 7", "output": "4" }, { "input": "10 10\n1 1 1 0 1 1 1 1 0 0\n1 1 1 1 1 0 0 0 0 1\n0 1 1 0 0 1 1 1 0 0\n1 1 1 1 0 1 1 1 1 1\n1 0 0 1 0 1 1 1 1 1\n1 1 1 1 1 1 0 1 0 1\n1 1 1 1 1 1 1 1 0 0\n0 1 0 0 1 1 1 1 1 1\n0 1 1 1 0 1 0 1 0 0\n1 1 0 1 0 1 1 1 1 0\n6 7", "output": "27" }, { "input": "10 8\n0 1 1 1 1 1 1 0\n0 0 1 1 1 1 1 1\n0 1 0 1 1 1 1 0\n0 0 1 0 1 0 1 1\n0 1 1 1 1 1 1 1\n0 1 0 1 0 0 1 1\n0 0 0 0 0 0 0 1\n1 1 1 1 1 0 1 1\n1 1 1 0 1 1 1 0\n1 1 0 0 1 1 0 1\n8 10", "output": "51" }, { "input": "10 1\n0\n1\n1\n1\n1\n1\n1\n0\n1\n1\n1 5", "output": "4" } ]
310
0
3.9225
7,785
940
Points on the line
[ "brute force", "greedy", "sortings" ]
null
null
We've got no test cases. A big olympiad is coming up. But the problemsetters' number one priority should be adding another problem to the round. The diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1,<=3,<=2,<=1} is 2. Diameter of multiset consisting of one point is 0. You are given *n* points on the line. What is the minimum number of points you have to remove, so that the diameter of the multiset of the remaining points will not exceed *d*?
The first line contains two integers *n* and *d* (1<=≤<=*n*<=≤<=100,<=0<=≤<=*d*<=≤<=100) — the amount of points and the maximum allowed diameter respectively. The second line contains *n* space separated integers (1<=≤<=*x**i*<=≤<=100) — the coordinates of the points.
Output a single integer — the minimum number of points you have to remove.
[ "3 1\n2 1 4\n", "3 0\n7 7 7\n", "6 3\n1 3 4 6 9 10\n" ]
[ "1\n", "0\n", "3\n" ]
In the first test case the optimal strategy is to remove the point with coordinate 4. The remaining points will have coordinates 1 and 2, so the diameter will be equal to 2 - 1 = 1. In the second test case the diameter is equal to 0, so its is unnecessary to remove any points. In the third test case the optimal strategy is to remove points with coordinates 1, 9 and 10. The remaining points will have coordinates 3, 4 and 6, so the diameter will be equal to 6 - 3 = 3.
[ { "input": "3 1\n2 1 4", "output": "1" }, { "input": "3 0\n7 7 7", "output": "0" }, { "input": "6 3\n1 3 4 6 9 10", "output": "3" }, { "input": "11 5\n10 11 12 13 14 15 16 17 18 19 20", "output": "5" }, { "input": "1 100\n1", "output": "0" }, { "input": "100 10\n22 75 26 45 72 81 47 29 97 2 75 25 82 84 17 56 32 2 28 37 57 39 18 11 79 6 40 68 68 16 40 63 93 49 91 10 55 68 31 80 57 18 34 28 76 55 21 80 22 45 11 67 67 74 91 4 35 34 65 80 21 95 1 52 25 31 2 53 96 22 89 99 7 66 32 2 68 33 75 92 84 10 94 28 54 12 9 80 43 21 51 92 20 97 7 25 67 17 38 100", "output": "84" }, { "input": "100 70\n22 75 26 45 72 81 47 29 97 2 75 25 82 84 17 56 32 2 28 37 57 39 18 11 79 6 40 68 68 16 40 63 93 49 91 10 55 68 31 80 57 18 34 28 76 55 21 80 22 45 11 67 67 74 91 4 35 34 65 80 21 95 1 52 25 31 2 53 96 22 89 99 7 66 32 2 68 33 75 92 84 10 94 28 54 12 9 80 43 21 51 92 20 97 7 25 67 17 38 100", "output": "27" }, { "input": "1 10\n25", "output": "0" }, { "input": "70 80\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", "output": "0" }, { "input": "3 1\n25 26 27", "output": "1" }, { "input": "100 5\n51 56 52 60 52 53 52 60 56 54 55 50 53 51 57 53 52 54 54 52 51 55 50 56 60 51 58 50 60 59 50 54 60 55 55 57 54 59 59 55 55 52 56 57 59 54 53 57 52 50 50 55 59 54 54 56 51 58 52 51 56 56 58 56 54 54 57 52 51 58 56 57 54 59 58 53 50 52 50 60 57 51 54 59 54 54 52 55 53 55 51 53 52 54 51 56 55 53 58 56", "output": "34" }, { "input": "100 11\n44 89 57 64 94 96 73 96 55 52 91 73 73 93 51 62 63 85 43 75 60 78 98 55 80 84 65 75 61 88 62 71 53 57 94 85 60 96 66 96 61 72 97 64 51 44 63 82 67 86 60 57 74 85 57 79 61 94 86 78 84 56 60 75 91 91 92 62 89 85 79 57 76 97 65 56 46 78 51 69 50 52 85 80 76 71 81 51 90 71 77 60 63 62 84 59 79 84 69 81", "output": "70" }, { "input": "100 0\n22 75 26 45 72 81 47 29 97 2 75 25 82 84 17 56 32 2 28 37 57 39 18 11 79 6 40 68 68 16 40 63 93 49 91 10 55 68 31 80 57 18 34 28 76 55 21 80 22 45 11 67 67 74 91 4 35 34 65 80 21 95 1 52 25 31 2 53 96 22 89 99 7 66 32 2 68 33 75 92 84 10 94 28 54 12 9 80 43 21 51 92 20 97 7 25 67 17 38 100", "output": "96" }, { "input": "100 100\n22 75 26 45 72 81 47 29 97 2 75 25 82 84 17 56 32 2 28 37 57 39 18 11 79 6 40 68 68 16 40 63 93 49 91 10 55 68 31 80 57 18 34 28 76 55 21 80 22 45 11 67 67 74 91 4 35 34 65 80 21 95 1 52 25 31 2 53 96 22 89 99 7 66 32 2 68 33 75 92 84 10 94 28 54 12 9 80 43 21 51 92 20 97 7 25 67 17 38 100", "output": "0" }, { "input": "76 32\n50 53 69 58 55 39 40 42 40 55 58 73 55 72 75 44 45 55 46 60 60 42 41 64 77 39 68 51 61 49 38 41 56 57 64 43 78 36 39 63 40 66 52 76 39 68 39 73 40 68 54 60 35 67 69 52 58 52 38 63 69 38 69 60 73 64 65 41 59 55 37 57 40 34 35 35", "output": "13" }, { "input": "100 1\n22 75 26 45 72 81 47 29 97 2 75 25 82 84 17 56 32 2 28 37 57 39 18 11 79 6 40 68 68 16 40 63 93 49 91 10 55 68 31 80 57 18 34 28 76 55 21 80 22 45 11 67 67 74 91 4 35 34 65 80 21 95 1 52 25 31 2 53 96 22 89 99 7 66 32 2 68 33 75 92 84 10 94 28 54 12 9 80 43 21 51 92 20 97 7 25 67 17 38 100", "output": "93" }, { "input": "100 5\n22 75 26 45 72 81 47 29 97 2 75 25 82 84 17 56 32 2 28 37 57 39 18 11 79 6 40 68 68 16 40 63 93 49 91 10 55 68 31 80 57 18 34 28 76 55 21 80 22 45 11 67 67 74 91 4 35 34 65 80 21 95 1 52 25 31 2 53 96 22 89 99 7 66 32 2 68 33 75 92 84 10 94 28 54 12 9 80 43 21 51 92 20 97 7 25 67 17 38 100", "output": "89" }, { "input": "98 64\n2 29 36 55 58 15 25 33 7 16 61 1 4 24 63 26 36 16 16 3 57 39 56 7 11 24 20 12 22 10 56 5 11 39 61 52 27 54 21 6 61 36 40 52 54 5 15 52 58 23 45 39 65 16 27 40 13 64 47 24 51 29 9 18 49 49 8 47 2 64 7 63 49 10 20 26 34 3 45 66 8 46 16 32 16 38 3 6 15 17 35 48 36 5 57 29 61 15", "output": "1" }, { "input": "100 56\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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100", "output": "43" }, { "input": "100 0\n14 13 14 13 14 13 13 13 13 14 13 13 14 14 13 14 14 14 14 13 13 13 14 13 13 14 14 14 14 14 14 13 13 13 13 14 13 14 13 14 13 14 14 14 14 13 13 14 14 13 13 13 13 14 13 14 13 14 13 14 13 13 13 14 13 13 14 13 14 14 13 13 13 14 14 14 14 13 13 14 14 14 14 14 14 14 13 14 13 13 13 14 14 13 13 13 13 13 14 14", "output": "50" }, { "input": "100 0\n14 17 18 22 19 18 19 21 19 19 22 22 19 21 24 23 24 19 25 24 24 21 20 13 26 18 17 15 25 13 17 20 20 21 13 22 27 15 18 27 19 15 16 25 18 17 18 22 19 17 18 24 14 16 18 16 22 16 17 27 18 17 18 24 22 13 14 20 23 19 16 21 19 13 14 14 25 15 27 24 26 22 16 20 16 14 21 27 15 23 23 24 27 14 24 17 19 24 15 27", "output": "89" }, { "input": "100 100\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "0" }, { "input": "1 100\n22", "output": "0" }, { "input": "1 0\n22", "output": "0" }, { "input": "1 99\n99", "output": "0" }, { "input": "1 5\n6", "output": "0" }, { "input": "3 1\n10 20 30", "output": "2" }, { "input": "3 0\n1 2 3", "output": "2" }, { "input": "3 2\n1 50 99", "output": "2" }, { "input": "7 4\n1 3 4 9 10 11 12", "output": "3" }, { "input": "2 5\n67 23", "output": "1" }, { "input": "4 2\n1 4 7 9", "output": "2" }, { "input": "2 0\n1 2", "output": "1" }, { "input": "8 1\n3 3 3 5 5 5 5 5", "output": "3" }, { "input": "5 1\n3 5 5 5 6", "output": "1" } ]
77
0
3
7,788
288
Polo the Penguin and XOR operation
[ "implementation", "math" ]
null
null
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to *n*, inclusive. For permutation *p*<==<=*p*0,<=*p*1,<=...,<=*p**n*, Polo has defined its beauty — number . Expression means applying the operation of bitwise excluding "OR" to numbers *x* and *y*. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal — as "xor". Help him find among all permutations of integers from 0 to *n* the permutation with the maximum beauty.
The single line contains a positive integer *n* (1<=≤<=*n*<=≤<=106).
In the first line print integer *m* the maximum possible beauty. In the second line print any permutation of integers from 0 to *n* with the beauty equal to *m*. If there are several suitable permutations, you are allowed to print any of them.
[ "4\n" ]
[ "20\n0 2 1 4 3\n" ]
none
[ { "input": "4", "output": "20\n0 2 1 4 3" }, { "input": "7", "output": "56\n7 6 5 4 3 2 1 0" }, { "input": "1", "output": "2\n1 0" }, { "input": "2", "output": "6\n0 2 1" }, { "input": "3", "output": "12\n3 2 1 0" }, { "input": "8", "output": "72\n0 6 5 4 3 2 1 8 7" }, { "input": "10", "output": "110\n0 2 1 4 3 10 9 8 7 6 5" }, { "input": "47", "output": "2256\n15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16" }, { "input": "74", "output": "5550\n0 2 1 4 3 10 9 8 7 6 5 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53" }, { "input": "99", "output": "9900\n3 2 1 0 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28" }, { "input": "128", "output": "16512\n0 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 128 127" }, { "input": "257", "output": "66306\n1 0 253 252 251 250 249 248 247 246 245 244 243 242 241 240 239 238 237 236 235 234 233 232 231 230 229 228 227 226 225 224 223 222 221 220 219 218 217 216 215 214 213 212 211 210 209 208 207 206 205 204 203 202 201 200 199 198 197 196 195 194 193 192 191 190 189 188 187 186 185 184 183 182 181 180 179 178 177 176 175 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 159 158 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 ..." }, { "input": "1000000", "output": "1000001000000\n0 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 64 63 446 445 444 443 442 441 440 439 438 437 436 435 434 433 432 431 430 429 428 427 426 425 424 423 422 421 420 419 418 417 416 415 414 413 412 411 410 409 408 407 406 405 404 403 402 401 400 399 398 397 396 395 394 393 392 391 390 389 388 387 386 385 384 383 382 381 380 379 378 377 376 375 374 373 372 371 370 369..." }, { "input": "77845", "output": "6059921870\n1 0 5 4 3 2 9 8 7 6 21 20 19 18 17 16 15 14 13 12 11 10 4073 4072 4071 4070 4069 4068 4067 4066 4065 4064 4063 4062 4061 4060 4059 4058 4057 4056 4055 4054 4053 4052 4051 4050 4049 4048 4047 4046 4045 4044 4043 4042 4041 4040 4039 4038 4037 4036 4035 4034 4033 4032 4031 4030 4029 4028 4027 4026 4025 4024 4023 4022 4021 4020 4019 4018 4017 4016 4015 4014 4013 4012 4011 4010 4009 4008 4007 4006 4005 4004 4003 4002 4001 4000 3999 3998 3997 3996 3995 3994 3993 3992 3991 3990 3989 3988 3987 3986 398..." }, { "input": "100000", "output": "10000100000\n0 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 32 31 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 160 159 158 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105..." }, { "input": "100001", "output": "10000300002\n1 0 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 33 32 31 30 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 161 160 159 158 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 10..." }, { "input": "999999", "output": "999999000000\n63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 447 446 445 444 443 442 441 440 439 438 437 436 435 434 433 432 431 430 429 428 427 426 425 424 423 422 421 420 419 418 417 416 415 414 413 412 411 410 409 408 407 406 405 404 403 402 401 400 399 398 397 396 395 394 393 392 391 390 389 388 387 386 385 384 383 382 381 380 379 378 377 376 375 374 373 372 371 370 369..." }, { "input": "777777", "output": "604937839506\n1 0 13 12 11 10 9 8 7 6 5 4 3 2 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 461 460 459 458 457 456 455 454 453 452 451 450 449 448 447 446 445 444 443 442 441 440 439 438 437 436 435 434 433 432 431 430 429 428 427 426 425 424 423 422 421 420 419 418 417 416 415 414 413 412 411 410 409 408 407 406 405 404 403 402 401 400 399 398 397 396 395 394 393 392 391 390 389 388 387 386 385 384 383 382 381 380 379 378 377 376 375 374 373 3..." }, { "input": "687500", "output": "472656937500\n0 2 1 12 11 10 9 8 7 6 5 4 3 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 396 395 394 393 392 391 390 389 388 387 386 385 384 383 382 381 380 379 378 377 376 375 374 373 372 371 370 369 368 367 366 365 364 363 362 361 360..." }, { "input": "17", "output": "306\n1 0 13 12 11 10 9 8 7 6 5 4 3 2 17 16 15 14" }, { "input": "18", "output": "342\n0 2 1 12 11 10 9 8 7 6 5 4 3 18 17 16 15 14 13" }, { "input": "19", "output": "380\n3 2 1 0 11 10 9 8 7 6 5 4 19 18 17 16 15 14 13 12" }, { "input": "20", "output": "420\n0 2 1 4 3 10 9 8 7 6 5 20 19 18 17 16 15 14 13 12 11" }, { "input": "4587", "output": "21045156\n3 2 1 0 11 10 9 8 7 6 5 4 19 18 17 16 15 14 13 12 491 490 489 488 487 486 485 484 483 482 481 480 479 478 477 476 475 474 473 472 471 470 469 468 467 466 465 464 463 462 461 460 459 458 457 456 455 454 453 452 451 450 449 448 447 446 445 444 443 442 441 440 439 438 437 436 435 434 433 432 431 430 429 428 427 426 425 424 423 422 421 420 419 418 417 416 415 414 413 412 411 410 409 408 407 406 405 404 403 402 401 400 399 398 397 396 395 394 393 392 391 390 389 388 387 386 385 384 383 382 381 380 379..." }, { "input": "15475", "output": "239491100\n3 2 1 0 11 10 9 8 7 6 5 4 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 907 906 905 904 903 902 901 900 899 898 897 896 895 894 893 892 891 890 889 888 887 886 885 884 883 882 881 880 879 878 877 876 875 874 873 872 87..." }, { "input": "68450", "output": "4685470950\n0 2 1 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 ..." }, { "input": "6100", "output": "37216100\n0 2 1 4 3 10 9 8 7 6 5 20 19 18 17 16 15 14 13 12 11 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 2004 2003 2002 2001 2000 1999 1998 1997 1996 1995 1994 1993 1992 1991 1990 1989 1988 1987 1986 1985 1984 1983 1982 1981 1980 1979 1978 1977 1976 1975 1974 1973 1972 1971 1970 1969 1968 1967 1966 1965 1964 1963 1962 1961 1960 1959 1958 1957 1956 1955 1954 1953 1952 1951 1950 1949 1948 1947 1946 1945 1944 1943 1942 1941 1940 1939 1938 1937 1936 1935 1934 1933 1932 1931 1930 1929 19..." }, { "input": "1047", "output": "1097256\n7 6 5 4 3 2 1 0 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 999 998 997 996 995 994 993 992 991 990 989 988 987 986 985 984 983 982 981 980 979 978 977 976 975 974 973 972 971 970 969 968 967 966 965 964 963 962 961 960 959 958 957 956 955 954 953 952 951 950 949 948 947 946 945 944 943 942 941 940 939 938 937 936 935 934 933 932 931 930 929 928 927 926 925 924 923 922 921 920 919 918 917 916 915 914 913 912 911 910 909 908 907 906 905 904 903 902 901 900 899 898 897 896 895 894 893 892 891 890 ..." }, { "input": "670041", "output": "448955611722\n1 0 5 4 3 2 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 37 36 35 34 33 32 31 30 29 28 27 26 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 165 164 163 162 161 160 159 158 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 1..." }, { "input": "875495", "output": "766492370520\n7 6 5 4 3 2 1 0 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 999 998 997 996 995 994 993 992 991 990 989 988 987 986 985 984 983 982 981 980 979 978 977 976 975 974 973 972 971 970 969 968 967 966 965 964 963 962 961 960 959 958 957 956 955 954 953 952 951 950 949 948 947 946 945 944 943 942 941 940 939 938 937 936 935 934 933 932 931 930 929 928 927 926 925 924 923 922 921 920 919 918 917 916 915 914 913 912 911 910 909 908 907 906 905 904 903 902 901 900 899 898 897 896 895 894 893 892 891..." }, { "input": "687548", "output": "472722939852\n0 2 1 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 66 65 64 63 62 61 444 443 442 441 440 439 438 437 436 435 434 433 432 431 430 429 428 427 426 425 424 423 422 421 420 419 418 417 416 415 414 413 412 411 410 409 408 407 406 405 404 403 402 401 400 399 398 397 396 395 394 393 392 391 390 389 388 387 386 385 384 383 382 381 380 379 378 377 376 375 374 373 372 371 370 369 36..." }, { "input": "154781", "output": "23957312742\n1 0 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 10..." }, { "input": "684501", "output": "468542303502\n1 0 5 4 3 2 9 8 7 6 21 20 19 18 17 16 15 14 13 12 11 10 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 469 468 467 466 465 464 463 462 461 460 459 458 457 456 455 454 453 452 451 450 449 448 447 446 445 444 443 442 441 440 439 438 437 436 435 434 433 432 431 430 429 428 427 426 425 424 423 422 421 420 419 418 417 416 415 414 413 412 411 410 409 408 407 406 405 404 403 402 401 400 399 398 397 396 395 394 393 392 391 390 389 388 387 386 385 384 383 382 381 380 379 378 377 376 375 3..." }, { "input": "754810", "output": "569738890910\n0 2 1 4 3 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 900 899 898 897 896 895 894 893 892 891 890 889 888 887 886 885 884 883 882 881 880 879 878 877 876 875 874 873 872..." }, { "input": "987548", "output": "975252039852\n0 2 1 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 412 411 410 409 408 407 406 405 404 403 402 401 400 399 398 397 396 395 394 393 392 391 390 389 388 387 386 385 384 383 382 381 380 379 378 377 376 375 374 373 372 371 370 369 368 367 366 365 364 363 362 361 36..." }, { "input": "348754", "output": "121629701270\n0 2 1 12 11 10 9 8 7 6 5 4 3 18 17 16 15 14 13 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 428 427 426 425 424 423 422 421 420 419 418 417 416 415 414 413 412 411 410 409 408 407 406 405 404 403 402 401 400 399 398 397 396 395 394 393 392 391 390 389 388 387 386 385 384 383 382 381 380 379 378 377 376 375 374 373 372 371 370 369 368 367 366 365 36..." }, { "input": "20", "output": "420\n0 2 1 4 3 10 9 8 7 6 5 20 19 18 17 16 15 14 13 12 11" }, { "input": "11", "output": "132\n3 2 1 0 11 10 9 8 7 6 5 4" }, { "input": "12", "output": "156\n0 2 1 12 11 10 9 8 7 6 5 4 3" }, { "input": "13", "output": "182\n1 0 13 12 11 10 9 8 7 6 5 4 3 2" }, { "input": "14", "output": "210\n0 14 13 12 11 10 9 8 7 6 5 4 3 2 1" }, { "input": "15", "output": "240\n15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0" } ]
2,000
0
0
7,815
505
Mr. Kitayuta's Colorful Graph
[ "dfs and similar", "dp", "dsu", "graphs" ]
null
null
Mr. Kitayuta has just bought an undirected graph consisting of *n* vertices and *m* edges. The vertices of the graph are numbered from 1 to *n*. Each edge, namely edge *i*, has a color *c**i*, connecting vertex *a**i* and *b**i*. Mr. Kitayuta wants you to process the following *q* queries. In the *i*-th query, he gives you two integers — *u**i* and *v**i*. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex *u**i* and vertex *v**i* directly or indirectly.
The first line of the input contains space-separated two integers — *n* and *m* (2<=≤<=*n*<=≤<=100,<=1<=≤<=*m*<=≤<=100), denoting the number of the vertices and the number of the edges, respectively. The next *m* lines contain space-separated three integers — *a**i*, *b**i* (1<=≤<=*a**i*<=&lt;<=*b**i*<=≤<=*n*) and *c**i* (1<=≤<=*c**i*<=≤<=*m*). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if *i*<=≠<=*j*, (*a**i*,<=*b**i*,<=*c**i*)<=≠<=(*a**j*,<=*b**j*,<=*c**j*). The next line contains a integer — *q* (1<=≤<=*q*<=≤<=100), denoting the number of the queries. Then follows *q* lines, containing space-separated two integers — *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*). It is guaranteed that *u**i*<=≠<=*v**i*.
For each query, print the answer in a separate line.
[ "4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4\n", "5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4\n" ]
[ "2\n1\n0\n", "1\n1\n1\n1\n2\n" ]
Let's consider the first sample. - Vertex 1 and vertex 2 are connected by color 1 and 2. - Vertex 3 and vertex 4 are connected by color 3. - Vertex 1 and vertex 4 are not connected by any single color.
[ { "input": "4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4", "output": "2\n1\n0" }, { "input": "5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4", "output": "1\n1\n1\n1\n2" }, { "input": "2 1\n1 2 1\n1\n1 2", "output": "1" }, { "input": "2 3\n1 2 3\n1 2 2\n1 2 1\n1\n1 2", "output": "3" }, { "input": "2 5\n1 2 1\n1 2 2\n1 2 3\n1 2 4\n1 2 5\n1\n1 2", "output": "5" } ]
170
2,355,200
3
7,829
3
Lorry
[ "greedy", "sortings" ]
B. Lorry
2
64
A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres). Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.
The first line contains a pair of integer numbers *n* and *v* (1<=≤<=*n*<=≤<=105; 1<=≤<=*v*<=≤<=109), where *n* is the number of waterborne vehicles in the boat depot, and *v* is the truck body volume of the lorry in cubic metres. The following *n* lines contain the information about the waterborne vehicles, that is a pair of numbers *t**i*,<=*p**i* (1<=≤<=*t**i*<=≤<=2; 1<=≤<=*p**i*<=≤<=104), where *t**i* is the vehicle type (1 – a kayak, 2 – a catamaran), and *p**i* is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.
In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.
[ "3 2\n1 2\n2 7\n1 3\n" ]
[ "7\n2\n" ]
none
[ { "input": "3 2\n1 2\n2 7\n1 3", "output": "7\n2" }, { "input": "5 3\n1 9\n2 9\n1 9\n2 10\n1 6", "output": "24\n3 1 5" }, { "input": "10 10\n1 14\n2 15\n2 11\n2 12\n2 9\n1 14\n2 15\n1 9\n2 11\n2 6", "output": "81\n6 1 7 2 4 9" }, { "input": "20 19\n2 47\n1 37\n1 48\n2 42\n2 48\n1 38\n2 47\n1 48\n2 47\n1 41\n2 46\n1 28\n1 49\n1 45\n2 34\n1 43\n2 29\n1 46\n2 45\n2 18", "output": "630\n13 8 3 18 14 16 10 6 2 5 9 7 1 11" }, { "input": "50 27\n2 93\n1 98\n2 62\n1 56\n1 86\n1 42\n2 67\n2 97\n2 59\n1 73\n1 83\n2 96\n1 20\n1 66\n1 84\n1 83\n1 91\n2 97\n1 81\n2 88\n2 63\n1 99\n2 57\n1 39\n1 74\n2 88\n1 30\n2 68\n1 100\n2 57\n1 87\n1 93\n1 83\n1 100\n1 91\n1 14\n1 38\n2 98\n2 85\n2 61\n1 44\n2 93\n2 66\n2 55\n2 74\n1 67\n2 67\n1 85\n2 59\n1 83", "output": "2055\n34 29 22 2 32 35 17 31 5 48 15 50 33 16 11 19 25 10 46 14 4 38 18 8" }, { "input": "1 1\n1 600", "output": "600\n1" }, { "input": "10 14\n2 230\n2 516\n2 527\n2 172\n2 854\n2 61\n1 52\n2 154\n2 832\n2 774", "output": "3905\n5 9 10 3 2 1 4" }, { "input": "8 8\n1 1\n1 1\n1 1\n1 1\n2 100\n2 100\n2 100\n2 100", "output": "400\n8 7 6 5" }, { "input": "8 4\n1 100\n1 100\n1 100\n1 100\n2 1\n2 1\n2 1\n2 1", "output": "400\n4 3 2 1" } ]
60
0
0
7,883
0
none
[ "none" ]
null
null
Вася купил стол, у которого *n* ножек. Каждая ножка состоит из двух частей, которые соединяются друг с другом. Каждая часть может быть произвольной положительной длины, но гарантируется, что из всех 2*n* частей возможно составить *n* ножек одинаковой длины. При составлении ножки любые две части могут быть соединены друг с другом. Изначально все ножки стола разобраны, а вам заданы длины 2*n* частей в произвольном порядке. Помогите Васе собрать все ножки стола так, чтобы все они были одинаковой длины, разбив заданные 2*n* части на пары правильным образом. Каждая ножка обязательно должна быть составлена ровно из двух частей, не разрешается использовать как ножку только одну часть.
В первой строке задано число *n* (1<=≤<=*n*<=≤<=1000) — количество ножек у стола, купленного Васей. Во второй строке следует последовательность из 2*n* целых положительных чисел *a*1,<=*a*2,<=...,<=*a*2*n* (1<=≤<=*a**i*<=≤<=100<=000) — длины частей ножек стола в произвольном порядке.
Выведите *n* строк по два целых числа в каждой — длины частей ножек, которые надо соединить друг с другом. Гарантируется, что всегда возможно собрать *n* ножек одинаковой длины. Если ответов несколько, разрешается вывести любой из них.
[ "3\n1 3 2 4 5 3\n", "3\n1 1 1 2 2 2\n" ]
[ "1 5\n2 4\n3 3\n", "1 2\n2 1\n1 2\n" ]
none
[ { "input": "3\n1 3 2 4 5 3", "output": "1 5\n2 4\n3 3" }, { "input": "3\n1 1 1 2 2 2", "output": "1 2\n1 2\n1 2" }, { "input": "1\n3 7", "output": "3 7" }, { "input": "10\n9 13 18 7 18 13 2 2 5 16 3 17 5 4 18 2 15 11 7 15", "output": "2 18\n2 18\n2 18\n3 17\n4 16\n5 15\n5 15\n7 13\n7 13\n9 11" }, { "input": "10\n759 82 475 841 46 461 288 525 918 241 789 847 58 954 712 159 942 211 153 539", "output": "46 954\n58 942\n82 918\n153 847\n159 841\n211 789\n241 759\n288 712\n461 539\n475 525" }, { "input": "100\n8 7 7 5 2 7 7 5 1 8 6 3 6 7 2 4 4 2 6 8 5 6 5 2 6 1 3 9 5 8 7 6 5 4 8 6 5 5 3 2 6 5 4 9 7 1 5 7 9 5 7 4 1 6 5 8 2 6 6 1 4 2 3 2 3 9 3 8 7 1 2 4 5 7 3 5 5 6 3 8 3 6 1 5 5 3 3 3 8 8 1 4 3 6 7 1 1 2 4 4 7 3 7 7 8 9 5 8 6 6 4 7 4 9 3 4 7 5 2 8 4 1 9 7 9 7 9 6 7 7 9 6 1 1 1 9 9 4 4 1 5 6 6 3 9 3 3 7 4 2 4 9 6 3 7 5 5 2 9 7 5 4 8 3 1 8 6 3 5 9 9 3 6 8 1 3 7 7 4 4 4 3 8 1 9 3 3 3 3 7 2 4 7 7 1 2 9 3 2 2", "output": "1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5" }, { "input": "10\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1" }, { "input": "10\n9 13 18 7 18 13 2 2 5 16 3 17 5 4 18 2 15 11 7 15", "output": "2 18\n2 18\n2 18\n3 17\n4 16\n5 15\n5 15\n7 13\n7 13\n9 11" }, { "input": "10\n759 82 475 841 46 461 288 525 918 241 789 847 58 954 712 159 942 211 153 539", "output": "46 954\n58 942\n82 918\n153 847\n159 841\n211 789\n241 759\n288 712\n461 539\n475 525" }, { "input": "100\n8 7 7 5 2 7 7 5 1 8 6 3 6 7 2 4 4 2 6 8 5 6 5 2 6 1 3 9 5 8 7 6 5 4 8 6 5 5 3 2 6 5 4 9 7 1 5 7 9 5 7 4 1 6 5 8 2 6 6 1 4 2 3 2 3 9 3 8 7 1 2 4 5 7 3 5 5 6 3 8 3 6 1 5 5 3 3 3 8 8 1 4 3 6 7 1 1 2 4 4 7 3 7 7 8 9 5 8 6 6 4 7 4 9 3 4 7 5 2 8 4 1 9 7 9 7 9 6 7 7 9 6 1 1 1 9 9 4 4 1 5 6 6 3 9 3 3 7 4 2 4 9 6 3 7 5 5 2 9 7 5 4 8 3 1 8 6 3 5 9 9 3 6 8 1 3 7 7 4 4 4 3 8 1 9 3 3 3 3 7 2 4 7 7 1 2 9 3 2 2", "output": "1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n1 9\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n2 8\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n3 7\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n4 6\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5\n5 5" }, { "input": "1\n7 7", "output": "7 7" } ]
608
5,529,600
3
7,886
582
Once Again...
[ "constructive algorithms", "dp", "matrices" ]
null
null
You are given an array of positive integers *a*1,<=*a*2,<=...,<=*a**n*<=×<=*T* of length *n*<=×<=*T*. We know that for any *i*<=&gt;<=*n* it is true that *a**i*<==<=*a**i*<=-<=*n*. Find the length of the longest non-decreasing sequence of the given array.
The first line contains two space-separated integers: *n*, *T* (1<=≤<=*n*<=≤<=100, 1<=≤<=*T*<=≤<=107). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=300).
Print a single number — the length of a sought sequence.
[ "4 3\n3 1 4 2\n" ]
[ "5\n" ]
The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.
[ { "input": "4 3\n3 1 4 2", "output": "5" }, { "input": "1 1000\n42", "output": "1000" }, { "input": "31 3767\n16 192 152 78 224 202 186 52 118 19 13 38 199 196 35 295 100 64 205 37 166 124 169 214 66 243 134 192 253 270 92", "output": "7546" }, { "input": "15 12226\n18 125 213 221 124 147 154 182 134 184 51 49 267 88 251", "output": "12234" }, { "input": "81 10683\n3 52 265 294 213 242 185 151 27 165 128 237 124 14 43 147 104 162 124 103 233 156 288 57 289 195 129 77 97 138 153 289 203 126 34 5 97 35 224 120 200 203 222 94 171 294 293 108 145 193 227 206 34 295 1 233 258 7 246 34 60 232 58 169 77 150 272 279 171 228 168 84 114 229 149 97 66 246 212 236 151", "output": "32070" }, { "input": "29 7954\n1 257 8 47 4 26 49 228 120 53 138 101 101 35 293 232 299 195 219 45 195 174 96 157 168 138 288 114 291", "output": "15919" }, { "input": "39 1057\n1 120 247 206 260 117 152 24 162 266 202 152 278 199 63 188 271 62 62 177 213 77 229 197 263 178 211 102 255 257 163 134 14 66 11 113 216 288 225", "output": "2128" }, { "input": "2 766\n147 282", "output": "767" }, { "input": "2 13101\n180 199", "output": "13102" }, { "input": "29 1918\n8 81 38 146 195 199 31 153 267 139 48 202 38 259 139 71 253 3 289 44 210 81 78 259 236 189 219 102 133", "output": "3845" }, { "input": "46 13793\n1 239 20 83 33 183 122 208 46 141 11 264 196 266 104 130 116 117 31 213 235 207 219 206 206 46 89 112 260 191 245 234 87 255 186 4 251 177 130 59 81 54 227 116 105 284", "output": "27600" }, { "input": "2 8698\n71 225", "output": "8699" }, { "input": "68 2450\n107 297 185 215 224 128 8 65 101 202 19 145 255 233 138 223 144 132 32 122 153 85 31 160 219 125 167 220 138 255 219 119 165 249 47 124 20 37 160 24 156 154 163 226 270 88 74 192 204 300 194 184 235 93 267 160 12 216 91 191 267 241 152 9 111 76 201 295", "output": "7366" }, { "input": "100 10000000\n98 99 96 97 94 95 92 93 90 91 88 89 86 87 84 85 82 83 80 81 78 79 76 77 74 75 72 73 70 71 68 69 66 67 64 65 62 63 60 61 58 59 56 57 54 55 52 53 50 51 48 49 46 47 44 45 42 43 40 41 38 39 36 37 34 35 32 33 30 31 28 29 26 27 24 25 22 23 20 21 18 19 16 17 14 15 12 13 10 11 8 9 6 7 4 5 2 3 1 100", "output": "10000050" }, { "input": "99 10000000\n97 98 95 96 93 94 91 92 89 90 87 88 85 86 83 84 81 82 79 80 77 78 75 76 73 74 71 72 69 70 67 68 65 66 63 64 61 62 59 60 57 58 55 56 53 54 51 52 49 50 47 48 45 46 43 44 41 42 39 40 37 38 35 36 33 34 31 32 29 30 27 28 25 26 23 24 21 22 19 20 17 18 15 16 13 14 11 12 9 10 7 8 5 6 3 4 1 2 99", "output": "10000050" }, { "input": "99 10000000\n96 97 98 93 94 95 90 91 92 87 88 89 84 85 86 81 82 83 78 79 80 75 76 77 72 73 74 69 70 71 66 67 68 63 64 65 60 61 62 57 58 59 54 55 56 51 52 53 48 49 50 45 46 47 42 43 44 39 40 41 36 37 38 33 34 35 30 31 32 27 28 29 24 25 26 21 22 23 18 19 20 15 16 17 12 13 14 9 10 11 6 7 8 3 4 5 2 1 99", "output": "10000065" }, { "input": "100 10000000\n97 98 99 94 95 96 91 92 93 88 89 90 85 86 87 82 83 84 79 80 81 76 77 78 73 74 75 70 71 72 67 68 69 64 65 66 61 62 63 58 59 60 55 56 57 52 53 54 49 50 51 46 47 48 43 44 45 40 41 42 37 38 39 34 35 36 31 32 33 28 29 30 25 26 27 22 23 24 19 20 21 16 17 18 13 14 15 10 11 12 7 8 9 4 5 6 1 2 3 100", "output": "10000067" }, { "input": "98 10000000\n95 96 97 92 93 94 89 90 91 86 87 88 83 84 85 80 81 82 77 78 79 74 75 76 71 72 73 68 69 70 65 66 67 62 63 64 59 60 61 56 57 58 53 54 55 50 51 52 47 48 49 44 45 46 41 42 43 38 39 40 35 36 37 32 33 34 29 30 31 26 27 28 23 24 25 20 21 22 17 18 19 14 15 16 11 12 13 8 9 10 5 6 7 2 3 4 97 98", "output": "20000034" }, { "input": "95 10000000\n92 93 94 89 90 91 86 87 88 83 84 85 80 81 82 77 78 79 74 75 76 71 72 73 68 69 70 65 66 67 62 63 64 59 60 61 56 57 58 53 54 55 50 51 52 47 48 49 44 45 46 41 42 43 38 39 40 35 36 37 32 33 34 29 30 31 26 27 28 23 24 25 20 21 22 17 18 19 14 15 16 11 12 13 8 9 10 5 6 7 2 3 4 94 95", "output": "20000033" }, { "input": "98 10000000\n195 196 197 192 193 194 189 190 191 186 187 188 183 184 185 180 181 182 177 178 179 174 175 176 171 172 173 168 169 170 165 166 167 162 163 164 159 160 161 156 157 158 153 154 155 150 151 152 147 148 149 144 145 146 141 142 143 138 139 140 135 136 137 132 133 134 129 130 131 126 127 128 123 124 125 120 121 122 117 118 119 114 115 116 111 112 113 108 109 110 105 106 107 102 103 104 1 2", "output": "10000065" }, { "input": "95 10000000\n192 193 194 189 190 191 186 187 188 183 184 185 180 181 182 177 178 179 174 175 176 171 172 173 168 169 170 165 166 167 162 163 164 159 160 161 156 157 158 153 154 155 150 151 152 147 148 149 144 145 146 141 142 143 138 139 140 135 136 137 132 133 134 129 130 131 126 127 128 123 124 125 120 121 122 117 118 119 114 115 116 111 112 113 108 109 110 105 106 107 102 103 104 1 2", "output": "10000063" }, { "input": "98 10000000\n1 2 195 196 197 192 193 194 189 190 191 186 187 188 183 184 185 180 181 182 177 178 179 174 175 176 171 172 173 168 169 170 165 166 167 162 163 164 159 160 161 156 157 158 153 154 155 150 151 152 147 148 149 144 145 146 141 142 143 138 139 140 135 136 137 132 133 134 129 130 131 126 127 128 123 124 125 120 121 122 117 118 119 114 115 116 111 112 113 108 109 110 105 106 107 102 103 104", "output": "10000066" }, { "input": "98 10000000\n1 2 5 4 3 8 7 6 11 10 9 14 13 12 17 16 15 20 19 18 23 22 21 26 25 24 29 28 27 32 31 30 35 34 33 38 37 36 41 40 39 44 43 42 47 46 45 50 49 48 53 52 51 56 55 54 59 58 57 62 61 60 65 64 63 68 67 66 71 70 69 74 73 72 77 76 75 80 79 78 83 82 81 86 85 84 89 88 87 92 91 90 95 94 93 98 97 96", "output": "10000033" }, { "input": "98 10000000\n1 1 5 4 3 8 7 6 11 10 9 14 13 12 17 16 15 20 19 18 23 22 21 26 25 24 29 28 27 32 31 30 35 34 33 38 37 36 41 40 39 44 43 42 47 46 45 50 49 48 53 52 51 56 55 54 59 58 57 62 61 60 65 64 63 68 67 66 71 70 69 74 73 72 77 76 75 80 79 78 83 82 81 86 85 84 89 88 87 92 91 90 95 94 93 98 97 96", "output": "20000032" }, { "input": "98 10000000\n1 2 95 96 97 92 93 94 89 90 91 86 87 88 83 84 85 80 81 82 77 78 79 74 75 76 71 72 73 68 69 70 65 66 67 62 63 64 59 60 61 56 57 58 53 54 55 50 51 52 47 48 49 44 45 46 41 42 43 38 39 40 35 36 37 32 33 34 29 30 31 26 27 28 23 24 25 20 21 22 17 18 19 14 15 16 11 12 13 8 9 10 5 6 7 2 3 4", "output": "20000034" }, { "input": "99 10000000\n1 2 3 95 96 97 92 93 94 89 90 91 86 87 88 83 84 85 80 81 82 77 78 79 74 75 76 71 72 73 68 69 70 65 66 67 62 63 64 59 60 61 56 57 58 53 54 55 50 51 52 47 48 49 44 45 46 41 42 43 38 39 40 35 36 37 32 33 34 29 30 31 26 27 28 23 24 25 20 21 22 17 18 19 14 15 16 11 12 13 8 9 10 5 6 7 2 3 4", "output": "20000034" }, { "input": "100 10000000\n1 2 2 1 2 2 1 1 2 2 1 2 1 1 1 1 1 2 2 2 1 2 1 2 1 2 1 2 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 2 2 2 2 2 2 1 1 2 2 1 1 2 1 1 2 1 2 1 1 2 1 2 2 2 1 1 2 2 1 2 1 1 2 2 1 1 1 2 1 2 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1", "output": "560000000" }, { "input": "100 10000000\n2 4 2 5 2 1 1 3 2 4 3 5 3 4 2 4 2 4 1 2 3 3 1 1 3 3 1 3 5 1 2 1 5 2 3 4 5 2 1 2 1 3 4 4 4 3 5 5 3 1 5 2 1 4 4 3 2 3 2 3 2 4 2 1 3 3 3 2 3 5 1 5 4 3 1 4 5 3 2 4 5 4 1 3 4 1 1 3 4 2 2 5 4 2 2 3 3 2 3 1", "output": "260000004" }, { "input": "100 10000000\n31 150 132 17 273 18 292 260 226 217 165 68 36 176 89 75 227 246 137 151 87 215 267 242 21 156 27 27 202 73 218 290 57 2 85 159 96 39 191 268 67 64 55 266 29 209 215 85 149 267 161 153 118 293 104 197 91 252 275 56 288 76 82 239 215 105 283 88 76 294 138 166 9 273 14 119 67 101 250 13 63 215 80 5 221 234 258 195 129 67 152 56 277 129 111 98 213 22 209 299", "output": "40000023" }, { "input": "100 10000000\n285 219 288 277 266 249 297 286 290 266 210 201 275 280 200 272 297 253 246 292 272 285 226 250 297 270 214 251 263 285 237 292 245 225 247 221 263 250 253 280 235 288 278 297 283 294 208 279 227 290 246 208 274 238 282 240 214 277 239 282 255 278 214 292 277 267 290 257 239 234 252 246 217 274 254 249 229 275 210 297 254 215 222 228 262 287 290 292 277 227 292 282 248 278 207 249 236 240 252 216", "output": "50000016" }, { "input": "100 10000000\n300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300", "output": "1000000000" }, { "input": "99 10000000\n300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300", "output": "990000000" }, { "input": "99 10000000\n299 299 300 300 299 299 300 299 299 299 299 299 299 299 299 300 300 300 299 300 300 300 299 299 299 299 299 299 300 299 299 300 299 299 300 300 300 299 300 300 299 299 300 299 300 300 299 300 299 300 299 300 300 299 299 299 299 299 299 300 299 299 300 300 300 299 300 299 300 300 299 299 299 299 299 299 299 299 300 299 300 300 299 300 300 299 299 300 300 299 300 300 299 300 299 299 300 299 299", "output": "580000001" }, { "input": "1 1\n5", "output": "1" }, { "input": "1 10000000\n1", "output": "10000000" }, { "input": "2 1\n1 2", "output": "2" }, { "input": "2 2\n1 2", "output": "3" }, { "input": "2 1000\n1 2", "output": "1001" }, { "input": "100 100\n99 100 97 98 95 96 93 94 91 92 89 90 87 88 85 86 83 84 81 82 79 80 77 78 75 76 73 74 71 72 69 70 67 68 65 66 63 64 61 62 59 60 57 58 55 56 53 54 51 52 49 50 47 48 45 46 43 44 41 42 39 40 37 38 35 36 33 34 31 32 29 30 27 28 25 26 23 24 21 22 19 20 17 18 15 16 13 14 11 12 9 10 7 8 5 6 3 4 1 2", "output": "150" }, { "input": "100 82\n151 81 114 37 17 178 92 164 215 108 286 89 108 87 77 166 110 215 212 300 125 92 247 221 78 120 163 113 249 141 36 241 179 116 187 287 69 103 76 80 160 200 249 170 159 72 8 138 171 45 97 271 114 176 54 181 4 259 246 39 29 292 203 49 122 253 99 259 252 74 231 92 43 142 23 144 109 282 47 207 140 212 9 3 255 137 285 146 22 84 52 98 41 21 177 63 217 62 291 64", "output": "274" }, { "input": "99 105\n16 118 246 3 44 149 156 290 44 267 221 123 57 175 233 24 23 120 298 228 119 62 23 183 169 294 195 115 131 157 223 298 77 106 283 117 255 41 17 298 22 176 164 187 214 101 10 181 117 70 271 291 59 156 44 204 140 205 253 176 270 43 188 287 40 250 271 100 244 297 133 228 98 218 290 69 171 66 195 283 63 154 191 66 238 104 32 122 79 190 55 110 276 2 188 26 44 276 230", "output": "435" }, { "input": "99 84\n62 4 145 285 106 132 30 96 211 28 144 190 95 184 227 177 128 60 143 19 19 81 38 83 108 172 241 228 48 39 171 282 233 294 74 271 178 87 24 180 212 190 223 153 230 198 261 232 150 18 190 91 265 61 280 13 207 70 182 117 270 77 242 163 138 212 165 273 247 23 52 88 243 85 293 12 135 284 162 91 174 109 42 19 218 289 9 59 9 117 61 122 78 287 144 176 281 123 243", "output": "280" }, { "input": "99 116\n102 257 115 247 279 111 118 255 198 168 183 184 32 3 36 204 178 186 88 67 205 91 21 40 116 93 2 148 226 65 37 69 69 7 82 205 152 25 34 272 26 283 78 142 17 110 101 250 120 128 145 276 182 57 19 104 228 221 94 220 279 216 220 294 3 289 185 272 73 180 246 107 246 260 219 268 218 41 166 50 230 143 166 158 194 153 256 209 28 255 77 33 143 296 38 81 133 57 263", "output": "268" }, { "input": "99 125\n85 108 102 3 173 193 27 38 288 272 14 270 98 42 34 206 275 54 20 164 207 255 3 196 183 3 61 37 98 223 208 231 144 76 114 19 138 156 157 198 124 39 120 283 34 139 240 240 247 132 211 81 225 12 101 108 63 20 30 158 266 201 101 101 113 157 132 108 41 215 54 27 154 102 175 276 103 35 52 130 10 266 229 202 85 210 116 149 214 14 121 263 217 152 240 275 113 253 53", "output": "404" }, { "input": "99 9\n218 254 64 32 130 52 242 40 29 188 196 300 258 165 110 151 265 142 295 166 141 260 158 218 184 251 180 16 177 125 192 279 201 189 170 37 7 150 117 79 97 13 69 156 254 287 17 214 95 300 150 197 133 161 46 26 82 119 174 6 252 42 264 136 273 127 42 274 113 278 165 173 231 209 159 56 248 39 46 41 222 278 114 84 150 13 63 106 179 279 44 15 13 74 50 168 38 181 127", "output": "51" }, { "input": "100 200\n99 100 97 98 95 96 93 94 91 92 89 90 87 88 85 86 83 84 81 82 79 80 77 78 75 76 73 74 71 72 69 70 67 68 65 66 63 64 61 62 59 60 57 58 55 56 53 54 51 52 49 50 47 48 45 46 43 44 41 42 39 40 37 38 35 36 33 34 31 32 29 30 27 28 25 26 23 24 21 22 19 20 17 18 15 16 13 14 11 12 9 10 7 8 5 6 3 4 1 2", "output": "250" }, { "input": "100 201\n99 100 97 98 95 96 93 94 91 92 89 90 87 88 85 86 83 84 81 82 79 80 77 78 75 76 73 74 71 72 69 70 67 68 65 66 63 64 61 62 59 60 57 58 55 56 53 54 51 52 49 50 47 48 45 46 43 44 41 42 39 40 37 38 35 36 33 34 31 32 29 30 27 28 25 26 23 24 21 22 19 20 17 18 15 16 13 14 11 12 9 10 7 8 5 6 3 4 1 2", "output": "251" }, { "input": "100 199\n99 100 97 98 95 96 93 94 91 92 89 90 87 88 85 86 83 84 81 82 79 80 77 78 75 76 73 74 71 72 69 70 67 68 65 66 63 64 61 62 59 60 57 58 55 56 53 54 51 52 49 50 47 48 45 46 43 44 41 42 39 40 37 38 35 36 33 34 31 32 29 30 27 28 25 26 23 24 21 22 19 20 17 18 15 16 13 14 11 12 9 10 7 8 5 6 3 4 1 2", "output": "249" } ]
0
0
-1
7,894
696
Puzzles
[ "dfs and similar", "math", "probabilities", "trees" ]
null
null
Barney lives in country USC (United States of Charzeh). USC has *n* cities numbered from 1 through *n* and *n*<=-<=1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city he wants by following roads. Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses a random DFS to search in the cities. A pseudo code of this algorithm is as follows: As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)). Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city *i*, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help.
The first line of input contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of cities in USC. The second line contains *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* (1<=≤<=*p**i*<=&lt;<=*i*), where *p**i* is the number of the parent city of city number *i* in the tree, meaning there is a road between cities numbered *p**i* and *i* in USC.
In the first and only line of output print *n* numbers, where *i*-th number is the expected value of starting_time[i]. Your answer for each city will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
[ "7\n1 2 1 1 4 4\n", "12\n1 1 2 2 4 4 3 3 1 10 8\n" ]
[ "1.0 4.0 5.0 3.5 4.5 5.0 5.0 \n", "1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 \n" ]
none
[ { "input": "7\n1 2 1 1 4 4", "output": "1.0 4.0 5.0 3.5 4.5 5.0 5.0 " }, { "input": "12\n1 1 2 2 4 4 3 3 1 10 8", "output": "1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 " }, { "input": "3\n1 2", "output": "1.0 2.0 3.0 " }, { "input": "8\n1 1 2 2 3 6 1", "output": "1.0 4.0 4.0 5.5 5.5 5.0 6.0 5.0 " }, { "input": "85\n1 1 2 2 4 6 1 3 6 3 3 11 9 14 12 5 8 11 16 19 12 17 2 19 1 24 6 2 6 6 24 3 20 1 1 1 17 8 4 25 31 32 39 12 35 23 31 26 46 9 37 7 5 23 41 41 39 9 11 54 36 54 28 15 25 58 56 18 23 70 68 18 3 48 57 70 15 65 22 35 25 13 49 34", "output": "1.0 28.5 27.0 38.0 38.5 39.5 44.5 40.0 40.5 45.0 37.0 40.5 44.0 42.5 43.5 43.0 41.0 43.0 39.5 44.0 45.0 44.0 42.5 42.5 41.0 42.5 44.5 44.5 44.0 45.0 43.5 44.0 44.0 45.0 42.0 43.0 43.0 45.0 42.5 44.5 43.0 45.5 45.0 44.5 44.5 43.5 45.5 45.0 43.5 44.5 44.5 44.0 45.5 43.5 45.5 45.0 45.5 44.0 44.5 44.5 45.0 44.0 45.0 45.5 45.0 45.5 45.0 46.0 44.5 44.5 46.0 47.0 44.5 44.0 46.0 46.5 46.0 45.5 46.0 45.0 44.0 45.5 45.0 44.5 46.0 " }, { "input": "1", "output": "1.0 " }, { "input": "2\n1", "output": "1.0 2.0 " }, { "input": "10\n1 2 2 2 5 4 6 5 6", "output": "1.0 2.0 6.5 6.0 4.5 6.0 7.0 7.5 7.0 7.5 " } ]
1,000
5,939,200
0
7,896
702
Cellular Network
[ "binary search", "implementation", "two pointers" ]
null
null
You are given *n* points on the straight line — the positions (*x*-coordinates) of the cities and *m* points on the same line — the positions (*x*-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more than *r* from this tower. Your task is to find minimal *r* that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than *r*. If *r*<==<=0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than *r* from this tower.
The first line contains two positive integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105) — the number of cities and the number of cellular towers. The second line contains a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates *a**i* are given in non-decreasing order. The third line contains a sequence of *m* integers *b*1,<=*b*2,<=...,<=*b**m* (<=-<=109<=≤<=*b**j*<=≤<=109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates *b**j* are given in non-decreasing order.
Print minimal *r* so that each city will be covered by cellular network.
[ "3 2\n-2 2 4\n-3 0\n", "5 3\n1 5 10 14 17\n4 11 15\n" ]
[ "4\n", "3\n" ]
none
[ { "input": "3 2\n-2 2 4\n-3 0", "output": "4" }, { "input": "5 3\n1 5 10 14 17\n4 11 15", "output": "3" }, { "input": "1 1\n-1000000000\n1000000000", "output": "2000000000" }, { "input": "1 1\n1000000000\n-1000000000", "output": "2000000000" }, { "input": "10 10\n1 1 2 2 2 4 4 6 7 9\n0 1 3 3 3 6 7 8 9 10", "output": "1" }, { "input": "10 10\n2 52 280 401 416 499 721 791 841 943\n246 348 447 486 507 566 568 633 953 986", "output": "244" }, { "input": "7 7\n1 2 3 3 4 5 6\n1 1 2 3 4 5 6", "output": "0" }, { "input": "1 3\n-3\n-1 -1 8", "output": "2" }, { "input": "1 2\n8\n-7 5", "output": "3" }, { "input": "2 1\n4 8\n-1", "output": "9" }, { "input": "1 2\n6\n-8 -8", "output": "14" }, { "input": "1 4\n4\n-8 0 1 7", "output": "3" }, { "input": "1 2\n2\n4 7", "output": "2" }, { "input": "2 2\n-5 2\n-7 4", "output": "2" }, { "input": "1 21\n3\n3 10 23 32 34 40 42 49 49 50 50 58 70 71 71 74 76 79 79 80 83", "output": "0" }, { "input": "1 3\n-4\n-8 -1 1", "output": "3" }, { "input": "4 1\n-6 -3 -1 2\n-7", "output": "9" }, { "input": "2 3\n-2 7\n-7 -2 5", "output": "2" }, { "input": "1 1\n-1\n0", "output": "1" }, { "input": "1 3\n0\n-4 0 5", "output": "0" } ]
92
2,048,000
-1
7,916
154
Hometask
[ "greedy" ]
null
null
Sergey attends lessons of the *N*-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the *N*-ish language. Sentences of the *N*-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of *N*-ish. The spelling rules state that *N*-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair. Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".
The first line contains a non-empty string *s*, consisting of lowercase Latin letters — that's the initial sentence in *N*-ish, written by Sergey. The length of string *s* doesn't exceed 105. The next line contains integer *k* (0<=≤<=*k*<=≤<=13) — the number of forbidden pairs of letters. Next *k* lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.
Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
[ "ababa\n1\nab\n", "codeforces\n2\ndo\ncs\n" ]
[ "2\n", "1\n" ]
In the first sample you should remove two letters b. In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
[ { "input": "ababa\n1\nab", "output": "2" }, { "input": "codeforces\n2\ndo\ncs", "output": "1" }, { "input": "nllnrlrnll\n1\nrl", "output": "1" }, { "input": "aludfbjtwnkgnfl\n1\noy", "output": "0" }, { "input": "pgpgppgggpbbnnn\n2\npg\nnb", "output": "7" }, { "input": "eepeeeeppppppeepeppe\n1\npe", "output": "10" }, { "input": "vefneyamdzoytemupniw\n13\nve\nfg\noi\nan\nck\nwx\npq\nml\nbt\nud\nrz\nsj\nhy", "output": "1" }, { "input": "drvwfaacccwnncfwuptsorrrvvvrgdzytrwweeexzyyyxuwuuk\n13\nld\nac\nnp\nrv\nmo\njh\ngb\nuw\nfq\nst\nkx\nzy\nei", "output": "11" }, { "input": "pninnihzipirpbdggrdglzdpbldtzihgbzdnrgznbpdanhnlag\n4\nli\nqh\nad\nbp", "output": "4" }, { "input": "mbmxuuuuxuuuuhhooooxxxuxxxuxuuxuuuxxjvjvjjjjvvvjjjjjvvjvjjjvvvjjvjjvvvjjjvjvvjvjjjjjmmbmbbbbbmbbbbmm\n5\nmb\nho\nxu\njv\nyp", "output": "37" }, { "input": "z\n0", "output": "0" }, { "input": "t\n13\nzx\nig\nyq\nbd\nph\nar\nne\nwo\ntk\njl\ncv\nfs\nmu", "output": "0" }, { "input": "rbnxovfcwkdjctdjfskaozjzthlcntuaoiavnbsfpuzxyvhfbxetvryvwrqetokdshadxpxijtpkrqvghsrejgnqntwiypiglzmp\n13\njr\nnf\nyk\ntq\nwe\nil\ngu\npb\naz\nxm\nhc\nvd\nso", "output": "0" }, { "input": "yynynnyyyiynyniiiinyynniiyiyyyniyniyynyyyynyynnniiiniyyniiyyiynyiynnnnyiiyiyniyyininiyiiiyynnnyiyinnnnyiinnnnnyninyinyynynyiynyyyiyinyynnyyinynyinininyniniynniiyyiiyy\n1\nni", "output": "28" }, { "input": "eowsgsewestwsootoetteoeeeetwssesstswegswetwetggewsteeteoggetssetseegwteswtteowsgowwoowetwgogewssogwgtttgwwoeotgoswwwwswsgeeoowwwwetwgeswsgwtsgewswtstwgggtegsssggsstwsoggeoseotgwswwggtggweewwgwegggoteweessotsewttwseosgegswwsoewgwstetwteegseowgwoteegwttwwowtwtosweeggweeeeoeesgseetgwgtswteotgwewetssggteewteeetetweeotwttwoeswggoosogoeg\n3\nst\neo\ngw", "output": "49" }, { "input": "ttspivgoupgupvgpusvppogsseueusuglpiugssviuutlvgvugtpovuvspvpipggooplgvgugpogvesppulovugsvuougveiivgvslllviogpvtolepglguuellotpvgevspvoiiggiiplppgosuguvtstttpielteuglopgeoeeuivtptggltoltpgliviotsospvitupiopisoetupvvspooilsopslpoloptigstspoiuutiuvvuvlpplvioitsuvlpvtleveslslovolloogsgpvpepgueesslgtssttuupieppsstoeopeeppvopglpvtvuugpsuoplttsgstsiplpiugtoolisgpeeiuvutstpulvtllipsvsslvoepooslvteoevipvpsuoiippeuegoovoovstlvgsstitsivlvseguoolpslueuepgeooltviovpveeivolssloieuugovguoleulvssopetlpivpgoigtv\n1\ngs", "output": "8" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n1\nab", "output": "75" } ]
216
2,662,400
3
7,949
896
Ithea Plays With Chtholly
[ "binary search", "constructive algorithms", "games", "greedy", "interactive" ]
null
null
This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. Initially, Ithea puts *n* clear sheets of paper in a line. They are numbered from 1 to *n* from left to right. This game will go on for *m* rounds. In each round, Ithea will give Chtholly an integer between 1 and *c*, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the *n* numbers are in non-decreasing order looking from left to right from sheet 1 to sheet *n*, and if after *m* rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number.
The first line contains 3 integers *n*,<=*m* and *c* (, means rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process.
none
[ "2 4 4\n2\n1\n3\n" ]
[ "1\n2\n2\n" ]
In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: - The first line contains 3 integers *n*, *m* and *c*; - The following *m* lines each contains an integer between 1 and *c*, indicating the number given to Chtholly in each round.
[ { "input": "2 4 4\n2\n1\n3\n4", "output": "3" }, { "input": "2 2 2\n1\n2", "output": "2" }, { "input": "3 6 3\n1\n2\n1\n3\n1\n3", "output": "3" }, { "input": "4 8 4\n4\n4\n4\n4\n4\n4\n4\n4", "output": "4" }, { "input": "10 120 15\n6\n11\n9\n11\n3\n12\n11\n12\n2\n8\n3\n11\n13\n5\n12\n11\n9\n3\n10\n9\n9\n13\n13\n5\n6\n11\n3\n15\n8\n8\n10\n13\n7\n6\n4\n14\n9\n10\n5\n13\n4\n1\n8\n6\n13\n1\n3\n4\n9\n12\n2\n7\n3\n3\n7\n2\n2\n9\n2\n4\n2\n2\n11\n12\n15\n13\n6\n2\n11\n1\n8\n3\n13\n6\n15\n6\n4\n3\n4\n15\n15\n9\n5\n8\n3\n6\n14\n14\n5\n9\n4\n4\n14\n1\n12\n4\n12\n9\n11\n7\n4\n2\n5\n4\n4\n13\n13\n4\n5\n8\n3\n4\n2\n15\n3\n10\n9\n8\n12\n8", "output": "20" }, { "input": "2 2 2\n2\n1", "output": "2" }, { "input": "2 2 1\n1\n1", "output": "2" }, { "input": "2 2 2\n2\n2", "output": "2" }, { "input": "3 3 2\n2\n2\n1", "output": "3" }, { "input": "3 3 2\n1\n2\n1", "output": "3" }, { "input": "3 3 2\n2\n1\n2", "output": "3" }, { "input": "3 3 2\n2\n1\n1", "output": "3" }, { "input": "3 3 1\n1\n1\n1", "output": "3" }, { "input": "3 6 3\n2\n2\n3\n3\n1\n1", "output": "5" }, { "input": "3 6 3\n2\n2\n1\n1\n3\n3", "output": "3" }, { "input": "3 6 3\n3\n3\n2\n2\n1\n1", "output": "3" }, { "input": "4 4 2\n1\n2\n2\n1", "output": "4" }, { "input": "4 4 2\n1\n2\n1\n2", "output": "4" }, { "input": "4 4 2\n2\n2\n2\n1", "output": "4" }, { "input": "4 8 3\n2\n1\n2\n1\n2\n1\n1\n1", "output": "4" }, { "input": "4 8 3\n3\n2\n3\n2\n3\n2\n3\n1", "output": "6" }, { "input": "4 8 3\n2\n3\n2\n3\n3\n3\n3\n1", "output": "6" }, { "input": "4 8 4\n2\n3\n2\n3\n2\n3\n2\n3", "output": "4" }, { "input": "4 8 4\n3\n4\n3\n3\n4\n4\n1\n2", "output": "7" }, { "input": "10 100 20\n4\n1\n15\n2\n11\n1\n18\n9\n17\n5\n17\n12\n20\n6\n14\n19\n20\n3\n6\n14\n12\n17\n17\n10\n11\n8\n6\n6\n19\n16\n20\n6\n14\n5\n6\n19\n16\n11\n12\n1\n18\n10\n20\n8\n6\n12\n18\n16\n9\n10\n13\n17\n19\n7\n15\n7\n11\n1\n9\n10\n12\n5\n4\n16\n5\n7\n9\n15\n14\n20\n6\n3\n12\n10\n3\n6\n1\n16\n15\n18\n6\n19\n4\n17\n15\n15\n2\n6\n18\n12\n20\n10\n5\n13\n9\n11\n20\n20\n4\n16", "output": "20" }, { "input": "10 100 20\n11\n11\n11\n11\n11\n11\n11\n11\n11\n10\n10\n10\n10\n10\n10\n10\n10\n10\n9\n9\n9\n9\n9\n9\n9\n9\n9\n8\n8\n8\n8\n8\n8\n8\n8\n8\n7\n7\n7\n7\n7\n7\n7\n7\n7\n6\n6\n6\n6\n6\n6\n6\n6\n6\n5\n5\n5\n5\n5\n5\n5\n5\n5\n4\n4\n4\n4\n4\n4\n4\n4\n4\n3\n3\n3\n3\n3\n3\n3\n3\n3\n2\n2\n2\n2\n2\n2\n2\n2\n2\n1\n1\n1\n1\n1\n1\n1\n1\n1\n12", "output": "10" } ]
109
4,608,000
0
7,960
779
Weird Rounding
[ "brute force", "greedy" ]
null
null
Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10*k*. In the given number of *n* Polycarp wants to remove the least number of digits to get a number that is divisible by 10*k*. For example, if *k*<==<=3, in the number 30020 it is enough to delete a single digit (2). In this case, the result is 3000 that is divisible by 103<==<=1000. Write a program that prints the minimum number of digits to be deleted from the given integer number *n*, so that the result is divisible by 10*k*. The result should not start with the unnecessary leading zero (i.e., zero can start only the number 0, which is required to be written as exactly one digit). It is guaranteed that the answer exists.
The only line of the input contains two integer numbers *n* and *k* (0<=≤<=*n*<=≤<=2<=000<=000<=000, 1<=≤<=*k*<=≤<=9). It is guaranteed that the answer exists. All numbers in the input are written in traditional notation of integers, that is, without any extra leading zeros.
Print *w* — the required minimal number of digits to erase. After removing the appropriate *w* digits from the number *n*, the result should have a value that is divisible by 10*k*. The result can start with digit 0 in the single case (the result is zero and written by exactly the only digit 0).
[ "30020 3\n", "100 9\n", "10203049 2\n" ]
[ "1\n", "2\n", "3\n" ]
In the example 2 you can remove two digits: 1 and any 0. The result is number 0 which is divisible by any number.
[ { "input": "30020 3", "output": "1" }, { "input": "100 9", "output": "2" }, { "input": "10203049 2", "output": "3" }, { "input": "0 1", "output": "0" }, { "input": "0 9", "output": "0" }, { "input": "100 2", "output": "0" }, { "input": "102030404 2", "output": "2" }, { "input": "1000999999 3", "output": "6" }, { "input": "12000000 4", "output": "0" }, { "input": "1090090090 5", "output": "2" }, { "input": "10 1", "output": "0" }, { "input": "10 2", "output": "1" }, { "input": "10 9", "output": "1" }, { "input": "100 1", "output": "0" }, { "input": "100 3", "output": "2" }, { "input": "101010110 3", "output": "3" }, { "input": "101010110 1", "output": "0" }, { "input": "101010110 2", "output": "2" }, { "input": "101010110 4", "output": "4" }, { "input": "101010110 5", "output": "8" }, { "input": "101010110 9", "output": "8" }, { "input": "1234567890 1", "output": "0" }, { "input": "1234567890 2", "output": "9" }, { "input": "1234567890 9", "output": "9" }, { "input": "2000000000 1", "output": "0" }, { "input": "2000000000 2", "output": "0" }, { "input": "2000000000 3", "output": "0" }, { "input": "2000000000 9", "output": "0" }, { "input": "1010101010 1", "output": "0" }, { "input": "1010101010 2", "output": "1" }, { "input": "1010101010 3", "output": "2" }, { "input": "1010101010 4", "output": "3" }, { "input": "1010101010 5", "output": "4" }, { "input": "1010101010 6", "output": "9" }, { "input": "1010101010 7", "output": "9" }, { "input": "1010101010 8", "output": "9" }, { "input": "1010101010 9", "output": "9" }, { "input": "10001000 1", "output": "0" }, { "input": "10001000 2", "output": "0" }, { "input": "10001000 3", "output": "0" }, { "input": "10001000 4", "output": "1" }, { "input": "10001000 5", "output": "1" }, { "input": "10001000 6", "output": "1" }, { "input": "10001000 7", "output": "7" }, { "input": "10001000 8", "output": "7" }, { "input": "10001000 9", "output": "7" }, { "input": "1000000001 1", "output": "1" }, { "input": "1000000001 2", "output": "1" }, { "input": "1000000001 3", "output": "1" }, { "input": "1000000001 6", "output": "1" }, { "input": "1000000001 7", "output": "1" }, { "input": "1000000001 8", "output": "1" }, { "input": "1000000001 9", "output": "9" }, { "input": "1000 1", "output": "0" }, { "input": "100001100 3", "output": "2" }, { "input": "7057 6", "output": "3" }, { "input": "30000000 5", "output": "0" }, { "input": "470 1", "output": "0" }, { "input": "500500000 4", "output": "0" }, { "input": "2103 8", "output": "3" }, { "input": "600000000 2", "output": "0" }, { "input": "708404442 1", "output": "4" }, { "input": "5000140 6", "output": "6" }, { "input": "1100047 3", "output": "2" }, { "input": "309500 5", "output": "5" }, { "input": "70053160 4", "output": "7" }, { "input": "44000 1", "output": "0" }, { "input": "400370000 3", "output": "0" }, { "input": "5800 6", "output": "3" }, { "input": "20700050 1", "output": "0" }, { "input": "650 1", "output": "0" }, { "input": "320005070 6", "output": "8" }, { "input": "370000 4", "output": "0" }, { "input": "1011 2", "output": "3" }, { "input": "1000111 5", "output": "6" }, { "input": "1001111 5", "output": "6" }, { "input": "99990 3", "output": "4" }, { "input": "10100200 6", "output": "7" }, { "input": "200 3", "output": "2" }, { "input": "103055 3", "output": "5" }, { "input": "1030555 3", "output": "6" }, { "input": "100111 4", "output": "5" }, { "input": "101 2", "output": "2" }, { "input": "1001 3", "output": "3" }, { "input": "100000 6", "output": "5" }, { "input": "1100000 6", "output": "6" }, { "input": "123450 2", "output": "5" }, { "input": "1003 3", "output": "3" }, { "input": "1111100 4", "output": "6" }, { "input": "532415007 8", "output": "8" }, { "input": "801 2", "output": "2" }, { "input": "1230 2", "output": "3" }, { "input": "9900 3", "output": "3" }, { "input": "14540444 2", "output": "7" }, { "input": "11111100 4", "output": "7" }, { "input": "11001 3", "output": "4" }, { "input": "1011110 3", "output": "6" }, { "input": "15450112 2", "output": "7" }, { "input": "2220 3", "output": "3" }, { "input": "90099 3", "output": "4" }, { "input": "10005 4", "output": "4" }, { "input": "1010 3", "output": "3" }, { "input": "444444400 3", "output": "8" }, { "input": "10020 4", "output": "4" }, { "input": "10303 3", "output": "4" }, { "input": "123000 4", "output": "5" }, { "input": "12300 3", "output": "4" }, { "input": "101 1", "output": "1" }, { "input": "500001 8", "output": "5" }, { "input": "121002 3", "output": "5" }, { "input": "10011 3", "output": "4" }, { "input": "505050 4", "output": "5" }, { "input": "1421011 2", "output": "6" }, { "input": "1202022 3", "output": "6" }, { "input": "1000023 7", "output": "6" }, { "input": "110 2", "output": "2" }, { "input": "111000 4", "output": "5" }, { "input": "10340 3", "output": "4" }, { "input": "101 9", "output": "2" }, { "input": "2001 3", "output": "3" }, { "input": "122320 2", "output": "5" }, { "input": "22200 3", "output": "4" }, { "input": "11110 2", "output": "4" }, { "input": "11010 3", "output": "4" }, { "input": "1000002333 6", "output": "9" }, { "input": "101010 4", "output": "5" }, { "input": "210 9", "output": "2" }, { "input": "500555 3", "output": "5" }, { "input": "1110111 3", "output": "6" }, { "input": "1100000000 9", "output": "9" }, { "input": "11000 4", "output": "4" }, { "input": "100 4", "output": "2" }, { "input": "234560 3", "output": "5" }, { "input": "10230 3", "output": "4" }, { "input": "10030234 5", "output": "7" }, { "input": "1200 3", "output": "3" }, { "input": "123400 3", "output": "5" }, { "input": "1034543 4", "output": "6" }, { "input": "10100 4", "output": "4" }, { "input": "10 5", "output": "1" }, { "input": "4501022 3", "output": "6" }, { "input": "12340 2", "output": "4" }, { "input": "30020 4", "output": "4" }, { "input": "1111100 6", "output": "6" }, { "input": "10101 5", "output": "4" }, { "input": "32132100 3", "output": "7" }, { "input": "1000023 6", "output": "6" }, { "input": "12300 4", "output": "4" }, { "input": "78400 3", "output": "4" }, { "input": "10203049 5", "output": "7" }, { "input": "404044 3", "output": "5" }, { "input": "1024 2", "output": "3" }, { "input": "505 2", "output": "2" }, { "input": "20 2", "output": "1" }, { "input": "1111100 3", "output": "6" }, { "input": "1000 9", "output": "3" }, { "input": "3333300 3", "output": "6" }, { "input": "1100 3", "output": "3" }, { "input": "963000 4", "output": "5" }, { "input": "100457 5", "output": "5" }, { "input": "10049 3", "output": "4" } ]
62
4,608,000
3
7,964
45
Planting Trees
[ "constructive algorithms" ]
J. Planting Trees
2
256
Vasya is a Greencode wildlife preservation society proponent. One day he found an empty field nobody owned, divided it into *n*<=×<=*m* squares and decided to plant a forest there. Vasya will plant *nm* trees of all different heights from 1 to *nm*. For his forest to look more natural he wants any two trees growing in the side neighbouring squares to have the absolute value of difference in heights to be strictly more than 1. Help Vasya: make the plan of the forest planting for which this condition is fulfilled.
The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100) — the number of rows and columns on Vasya's field
If there's no solution, print -1. Otherwise, print *n* lines containing *m* numbers each — the trees' planting plan. In every square of the plan the height of a tree that should be planted on this square should be written. If there are several solutions to that problem, print any of them.
[ "2 3\n", "2 1\n" ]
[ "3 6 2\n5 1 4\n", "-1\n" ]
none
[ { "input": "2 3", "output": "4 1 5 \n2 6 3 " }, { "input": "2 1", "output": "-1" }, { "input": "1 1", "output": "1 " }, { "input": "1 2", "output": "-1" }, { "input": "1 3", "output": "-1" }, { "input": "1 4", "output": "3 1 4 2 " }, { "input": "1 5", "output": "1 4 2 5 3 " }, { "input": "1 6", "output": "1 4 2 5 3 6 " }, { "input": "1 98", "output": "1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 10 59 11 60 12 61 13 62 14 63 15 64 16 65 17 66 18 67 19 68 20 69 21 70 22 71 23 72 24 73 25 74 26 75 27 76 28 77 29 78 30 79 31 80 32 81 33 82 34 83 35 84 36 85 37 86 38 87 39 88 40 89 41 90 42 91 43 92 44 93 45 94 46 95 47 96 48 97 49 98 " }, { "input": "1 99", "output": "1 51 2 52 3 53 4 54 5 55 6 56 7 57 8 58 9 59 10 60 11 61 12 62 13 63 14 64 15 65 16 66 17 67 18 68 19 69 20 70 21 71 22 72 23 73 24 74 25 75 26 76 27 77 28 78 29 79 30 80 31 81 32 82 33 83 34 84 35 85 36 86 37 87 38 88 39 89 40 90 41 91 42 92 43 93 44 94 45 95 46 96 47 97 48 98 49 99 50 " }, { "input": "1 100", "output": "1 51 2 52 3 53 4 54 5 55 6 56 7 57 8 58 9 59 10 60 11 61 12 62 13 63 14 64 15 65 16 66 17 67 18 68 19 69 20 70 21 71 22 72 23 73 24 74 25 75 26 76 27 77 28 78 29 79 30 80 31 81 32 82 33 83 34 84 35 85 36 86 37 87 38 88 39 89 40 90 41 91 42 92 43 93 44 94 45 95 46 96 47 97 48 98 49 99 50 100 " }, { "input": "1 1", "output": "1 " }, { "input": "2 1", "output": "-1" }, { "input": "3 1", "output": "-1" }, { "input": "4 1", "output": "3 \n1 \n4 \n2 " }, { "input": "5 1", "output": "1 \n4 \n2 \n5 \n3 " }, { "input": "6 1", "output": "1 \n4 \n2 \n5 \n3 \n6 " }, { "input": "98 1", "output": "1 \n50 \n2 \n51 \n3 \n52 \n4 \n53 \n5 \n54 \n6 \n55 \n7 \n56 \n8 \n57 \n9 \n58 \n10 \n59 \n11 \n60 \n12 \n61 \n13 \n62 \n14 \n63 \n15 \n64 \n16 \n65 \n17 \n66 \n18 \n67 \n19 \n68 \n20 \n69 \n21 \n70 \n22 \n71 \n23 \n72 \n24 \n73 \n25 \n74 \n26 \n75 \n27 \n76 \n28 \n77 \n29 \n78 \n30 \n79 \n31 \n80 \n32 \n81 \n33 \n82 \n34 \n83 \n35 \n84 \n36 \n85 \n37 \n86 \n38 \n87 \n39 \n88 \n40 \n89 \n41 \n90 \n42 \n91 \n43 \n92 \n44 \n93 \n45 \n94 \n46 \n95 \n47 \n96 \n48 \n97 \n49 \n98 " }, { "input": "99 1", "output": "1 \n51 \n2 \n52 \n3 \n53 \n4 \n54 \n5 \n55 \n6 \n56 \n7 \n57 \n8 \n58 \n9 \n59 \n10 \n60 \n11 \n61 \n12 \n62 \n13 \n63 \n14 \n64 \n15 \n65 \n16 \n66 \n17 \n67 \n18 \n68 \n19 \n69 \n20 \n70 \n21 \n71 \n22 \n72 \n23 \n73 \n24 \n74 \n25 \n75 \n26 \n76 \n27 \n77 \n28 \n78 \n29 \n79 \n30 \n80 \n31 \n81 \n32 \n82 \n33 \n83 \n34 \n84 \n35 \n85 \n36 \n86 \n37 \n87 \n38 \n88 \n39 \n89 \n40 \n90 \n41 \n91 \n42 \n92 \n43 \n93 \n44 \n94 \n45 \n95 \n46 \n96 \n47 \n97 \n48 \n98 \n49 \n99 \n50 " }, { "input": "100 1", "output": "1 \n51 \n2 \n52 \n3 \n53 \n4 \n54 \n5 \n55 \n6 \n56 \n7 \n57 \n8 \n58 \n9 \n59 \n10 \n60 \n11 \n61 \n12 \n62 \n13 \n63 \n14 \n64 \n15 \n65 \n16 \n66 \n17 \n67 \n18 \n68 \n19 \n69 \n20 \n70 \n21 \n71 \n22 \n72 \n23 \n73 \n24 \n74 \n25 \n75 \n26 \n76 \n27 \n77 \n28 \n78 \n29 \n79 \n30 \n80 \n31 \n81 \n32 \n82 \n33 \n83 \n34 \n84 \n35 \n85 \n36 \n86 \n37 \n87 \n38 \n88 \n39 \n89 \n40 \n90 \n41 \n91 \n42 \n92 \n43 \n93 \n44 \n94 \n45 \n95 \n46 \n96 \n47 \n97 \n48 \n98 \n49 \n99 \n50 \n100 " }, { "input": "2 2", "output": "-1" }, { "input": "2 4", "output": "1 5 2 6 \n7 3 8 4 " }, { "input": "2 5", "output": "1 6 2 7 3 \n8 4 9 5 10 " }, { "input": "2 6", "output": "1 7 2 8 3 9 \n10 4 11 5 12 6 " }, { "input": "2 99", "output": "1 100 2 101 3 102 4 103 5 104 6 105 7 106 8 107 9 108 10 109 11 110 12 111 13 112 14 113 15 114 16 115 17 116 18 117 19 118 20 119 21 120 22 121 23 122 24 123 25 124 26 125 27 126 28 127 29 128 30 129 31 130 32 131 33 132 34 133 35 134 36 135 37 136 38 137 39 138 40 139 41 140 42 141 43 142 44 143 45 144 46 145 47 146 48 147 49 148 50 \n149 51 150 52 151 53 152 54 153 55 154 56 155 57 156 58 157 59 158 60 159 61 160 62 161 63 162 64 163 65 164 66 165 67 166 68 167 69 168 70 169 71 170 72 171 73 172 74 173 ..." }, { "input": "2 100", "output": "1 101 2 102 3 103 4 104 5 105 6 106 7 107 8 108 9 109 10 110 11 111 12 112 13 113 14 114 15 115 16 116 17 117 18 118 19 119 20 120 21 121 22 122 23 123 24 124 25 125 26 126 27 127 28 128 29 129 30 130 31 131 32 132 33 133 34 134 35 135 36 136 37 137 38 138 39 139 40 140 41 141 42 142 43 143 44 144 45 145 46 146 47 147 48 148 49 149 50 150 \n151 51 152 52 153 53 154 54 155 55 156 56 157 57 158 58 159 59 160 60 161 61 162 62 163 63 164 64 165 65 166 66 167 67 168 68 169 69 170 70 171 71 172 72 173 73 174 74 ..." }, { "input": "3 2", "output": "1 4 \n5 2 \n3 6 " }, { "input": "3 3", "output": "1 6 2 \n7 3 8 \n4 9 5 " }, { "input": "3 4", "output": "1 7 2 8 \n9 3 10 4 \n5 11 6 12 " }, { "input": "3 5", "output": "1 9 2 10 3 \n11 4 12 5 13 \n6 14 7 15 8 " }, { "input": "3 99", "output": "1 150 2 151 3 152 4 153 5 154 6 155 7 156 8 157 9 158 10 159 11 160 12 161 13 162 14 163 15 164 16 165 17 166 18 167 19 168 20 169 21 170 22 171 23 172 24 173 25 174 26 175 27 176 28 177 29 178 30 179 31 180 32 181 33 182 34 183 35 184 36 185 37 186 38 187 39 188 40 189 41 190 42 191 43 192 44 193 45 194 46 195 47 196 48 197 49 198 50 \n199 51 200 52 201 53 202 54 203 55 204 56 205 57 206 58 207 59 208 60 209 61 210 62 211 63 212 64 213 65 214 66 215 67 216 68 217 69 218 70 219 71 220 72 221 73 222 74 223 ..." }, { "input": "3 100", "output": "1 151 2 152 3 153 4 154 5 155 6 156 7 157 8 158 9 159 10 160 11 161 12 162 13 163 14 164 15 165 16 166 17 167 18 168 19 169 20 170 21 171 22 172 23 173 24 174 25 175 26 176 27 177 28 178 29 179 30 180 31 181 32 182 33 183 34 184 35 185 36 186 37 187 38 188 39 189 40 190 41 191 42 192 43 193 44 194 45 195 46 196 47 197 48 198 49 199 50 200 \n201 51 202 52 203 53 204 54 205 55 206 56 207 57 208 58 209 59 210 60 211 61 212 62 213 63 214 64 215 65 216 66 217 67 218 68 219 69 220 70 221 71 222 72 223 73 224 74 ..." }, { "input": "4 2", "output": "1 5 \n6 2 \n3 7 \n8 4 " }, { "input": "4 3", "output": "1 7 2 \n8 3 9 \n4 10 5 \n11 6 12 " }, { "input": "4 4", "output": "1 9 2 10 \n11 3 12 4 \n5 13 6 14 \n15 7 16 8 " }, { "input": "4 5", "output": "1 11 2 12 3 \n13 4 14 5 15 \n6 16 7 17 8 \n18 9 19 10 20 " }, { "input": "4 99", "output": "1 199 2 200 3 201 4 202 5 203 6 204 7 205 8 206 9 207 10 208 11 209 12 210 13 211 14 212 15 213 16 214 17 215 18 216 19 217 20 218 21 219 22 220 23 221 24 222 25 223 26 224 27 225 28 226 29 227 30 228 31 229 32 230 33 231 34 232 35 233 36 234 37 235 38 236 39 237 40 238 41 239 42 240 43 241 44 242 45 243 46 244 47 245 48 246 49 247 50 \n248 51 249 52 250 53 251 54 252 55 253 56 254 57 255 58 256 59 257 60 258 61 259 62 260 63 261 64 262 65 263 66 264 67 265 68 266 69 267 70 268 71 269 72 270 73 271 74 272 ..." }, { "input": "4 100", "output": "1 201 2 202 3 203 4 204 5 205 6 206 7 207 8 208 9 209 10 210 11 211 12 212 13 213 14 214 15 215 16 216 17 217 18 218 19 219 20 220 21 221 22 222 23 223 24 224 25 225 26 226 27 227 28 228 29 229 30 230 31 231 32 232 33 233 34 234 35 235 36 236 37 237 38 238 39 239 40 240 41 241 42 242 43 243 44 244 45 245 46 246 47 247 48 248 49 249 50 250 \n251 51 252 52 253 53 254 54 255 55 256 56 257 57 258 58 259 59 260 60 261 61 262 62 263 63 264 64 265 65 266 66 267 67 268 68 269 69 270 70 271 71 272 72 273 73 274 74 ..." }, { "input": "5 2", "output": "1 6 \n7 2 \n3 8 \n9 4 \n5 10 " }, { "input": "5 3", "output": "1 9 2 \n10 3 11 \n4 12 5 \n13 6 14 \n7 15 8 " }, { "input": "5 4", "output": "1 11 2 12 \n13 3 14 4 \n5 15 6 16 \n17 7 18 8 \n9 19 10 20 " }, { "input": "5 5", "output": "1 14 2 15 3 \n16 4 17 5 18 \n6 19 7 20 8 \n21 9 22 10 23 \n11 24 12 25 13 " }, { "input": "5 99", "output": "1 249 2 250 3 251 4 252 5 253 6 254 7 255 8 256 9 257 10 258 11 259 12 260 13 261 14 262 15 263 16 264 17 265 18 266 19 267 20 268 21 269 22 270 23 271 24 272 25 273 26 274 27 275 28 276 29 277 30 278 31 279 32 280 33 281 34 282 35 283 36 284 37 285 38 286 39 287 40 288 41 289 42 290 43 291 44 292 45 293 46 294 47 295 48 296 49 297 50 \n298 51 299 52 300 53 301 54 302 55 303 56 304 57 305 58 306 59 307 60 308 61 309 62 310 63 311 64 312 65 313 66 314 67 315 68 316 69 317 70 318 71 319 72 320 73 321 74 322 ..." }, { "input": "5 100", "output": "1 251 2 252 3 253 4 254 5 255 6 256 7 257 8 258 9 259 10 260 11 261 12 262 13 263 14 264 15 265 16 266 17 267 18 268 19 269 20 270 21 271 22 272 23 273 24 274 25 275 26 276 27 277 28 278 29 279 30 280 31 281 32 282 33 283 34 284 35 285 36 286 37 287 38 288 39 289 40 290 41 291 42 292 43 293 44 294 45 295 46 296 47 297 48 298 49 299 50 300 \n301 51 302 52 303 53 304 54 305 55 306 56 307 57 308 58 309 59 310 60 311 61 312 62 313 63 314 64 315 65 316 66 317 67 318 68 319 69 320 70 321 71 322 72 323 73 324 74 ..." }, { "input": "98 2", "output": "1 99 \n100 2 \n3 101 \n102 4 \n5 103 \n104 6 \n7 105 \n106 8 \n9 107 \n108 10 \n11 109 \n110 12 \n13 111 \n112 14 \n15 113 \n114 16 \n17 115 \n116 18 \n19 117 \n118 20 \n21 119 \n120 22 \n23 121 \n122 24 \n25 123 \n124 26 \n27 125 \n126 28 \n29 127 \n128 30 \n31 129 \n130 32 \n33 131 \n132 34 \n35 133 \n134 36 \n37 135 \n136 38 \n39 137 \n138 40 \n41 139 \n140 42 \n43 141 \n142 44 \n45 143 \n144 46 \n47 145 \n146 48 \n49 147 \n148 50 \n51 149 \n150 52 \n53 151 \n152 54 \n55 153 \n154 56 \n57 155 \n156 58 \n..." }, { "input": "98 3", "output": "1 148 2 \n149 3 150 \n4 151 5 \n152 6 153 \n7 154 8 \n155 9 156 \n10 157 11 \n158 12 159 \n13 160 14 \n161 15 162 \n16 163 17 \n164 18 165 \n19 166 20 \n167 21 168 \n22 169 23 \n170 24 171 \n25 172 26 \n173 27 174 \n28 175 29 \n176 30 177 \n31 178 32 \n179 33 180 \n34 181 35 \n182 36 183 \n37 184 38 \n185 39 186 \n40 187 41 \n188 42 189 \n43 190 44 \n191 45 192 \n46 193 47 \n194 48 195 \n49 196 50 \n197 51 198 \n52 199 53 \n200 54 201 \n55 202 56 \n203 57 204 \n58 205 59 \n206 60 207 \n61 208 62 \n209 63 2..." }, { "input": "98 4", "output": "1 197 2 198 \n199 3 200 4 \n5 201 6 202 \n203 7 204 8 \n9 205 10 206 \n207 11 208 12 \n13 209 14 210 \n211 15 212 16 \n17 213 18 214 \n215 19 216 20 \n21 217 22 218 \n219 23 220 24 \n25 221 26 222 \n223 27 224 28 \n29 225 30 226 \n227 31 228 32 \n33 229 34 230 \n231 35 232 36 \n37 233 38 234 \n235 39 236 40 \n41 237 42 238 \n239 43 240 44 \n45 241 46 242 \n243 47 244 48 \n49 245 50 246 \n247 51 248 52 \n53 249 54 250 \n251 55 252 56 \n57 253 58 254 \n255 59 256 60 \n61 257 62 258 \n259 63 260 64 \n65 261 6..." }, { "input": "98 5", "output": "1 246 2 247 3 \n248 4 249 5 250 \n6 251 7 252 8 \n253 9 254 10 255 \n11 256 12 257 13 \n258 14 259 15 260 \n16 261 17 262 18 \n263 19 264 20 265 \n21 266 22 267 23 \n268 24 269 25 270 \n26 271 27 272 28 \n273 29 274 30 275 \n31 276 32 277 33 \n278 34 279 35 280 \n36 281 37 282 38 \n283 39 284 40 285 \n41 286 42 287 43 \n288 44 289 45 290 \n46 291 47 292 48 \n293 49 294 50 295 \n51 296 52 297 53 \n298 54 299 55 300 \n56 301 57 302 58 \n303 59 304 60 305 \n61 306 62 307 63 \n308 64 309 65 310 \n66 311 67 312..." }, { "input": "98 99", "output": "1 4852 2 4853 3 4854 4 4855 5 4856 6 4857 7 4858 8 4859 9 4860 10 4861 11 4862 12 4863 13 4864 14 4865 15 4866 16 4867 17 4868 18 4869 19 4870 20 4871 21 4872 22 4873 23 4874 24 4875 25 4876 26 4877 27 4878 28 4879 29 4880 30 4881 31 4882 32 4883 33 4884 34 4885 35 4886 36 4887 37 4888 38 4889 39 4890 40 4891 41 4892 42 4893 43 4894 44 4895 45 4896 46 4897 47 4898 48 4899 49 4900 50 \n4901 51 4902 52 4903 53 4904 54 4905 55 4906 56 4907 57 4908 58 4909 59 4910 60 4911 61 4912 62 4913 63 4914 64 4915 65 491..." }, { "input": "98 100", "output": "1 4901 2 4902 3 4903 4 4904 5 4905 6 4906 7 4907 8 4908 9 4909 10 4910 11 4911 12 4912 13 4913 14 4914 15 4915 16 4916 17 4917 18 4918 19 4919 20 4920 21 4921 22 4922 23 4923 24 4924 25 4925 26 4926 27 4927 28 4928 29 4929 30 4930 31 4931 32 4932 33 4933 34 4934 35 4935 36 4936 37 4937 38 4938 39 4939 40 4940 41 4941 42 4942 43 4943 44 4944 45 4945 46 4946 47 4947 48 4948 49 4949 50 4950 \n4951 51 4952 52 4953 53 4954 54 4955 55 4956 56 4957 57 4958 58 4959 59 4960 60 4961 61 4962 62 4963 63 4964 64 4965 6..." }, { "input": "99 2", "output": "1 100 \n101 2 \n3 102 \n103 4 \n5 104 \n105 6 \n7 106 \n107 8 \n9 108 \n109 10 \n11 110 \n111 12 \n13 112 \n113 14 \n15 114 \n115 16 \n17 116 \n117 18 \n19 118 \n119 20 \n21 120 \n121 22 \n23 122 \n123 24 \n25 124 \n125 26 \n27 126 \n127 28 \n29 128 \n129 30 \n31 130 \n131 32 \n33 132 \n133 34 \n35 134 \n135 36 \n37 136 \n137 38 \n39 138 \n139 40 \n41 140 \n141 42 \n43 142 \n143 44 \n45 144 \n145 46 \n47 146 \n147 48 \n49 148 \n149 50 \n51 150 \n151 52 \n53 152 \n153 54 \n55 154 \n155 56 \n57 156 \n157 58 ..." }, { "input": "99 3", "output": "1 150 2 \n151 3 152 \n4 153 5 \n154 6 155 \n7 156 8 \n157 9 158 \n10 159 11 \n160 12 161 \n13 162 14 \n163 15 164 \n16 165 17 \n166 18 167 \n19 168 20 \n169 21 170 \n22 171 23 \n172 24 173 \n25 174 26 \n175 27 176 \n28 177 29 \n178 30 179 \n31 180 32 \n181 33 182 \n34 183 35 \n184 36 185 \n37 186 38 \n187 39 188 \n40 189 41 \n190 42 191 \n43 192 44 \n193 45 194 \n46 195 47 \n196 48 197 \n49 198 50 \n199 51 200 \n52 201 53 \n202 54 203 \n55 204 56 \n205 57 206 \n58 207 59 \n208 60 209 \n61 210 62 \n211 63 2..." }, { "input": "99 4", "output": "1 199 2 200 \n201 3 202 4 \n5 203 6 204 \n205 7 206 8 \n9 207 10 208 \n209 11 210 12 \n13 211 14 212 \n213 15 214 16 \n17 215 18 216 \n217 19 218 20 \n21 219 22 220 \n221 23 222 24 \n25 223 26 224 \n225 27 226 28 \n29 227 30 228 \n229 31 230 32 \n33 231 34 232 \n233 35 234 36 \n37 235 38 236 \n237 39 238 40 \n41 239 42 240 \n241 43 242 44 \n45 243 46 244 \n245 47 246 48 \n49 247 50 248 \n249 51 250 52 \n53 251 54 252 \n253 55 254 56 \n57 255 58 256 \n257 59 258 60 \n61 259 62 260 \n261 63 262 64 \n65 263 6..." }, { "input": "99 5", "output": "1 249 2 250 3 \n251 4 252 5 253 \n6 254 7 255 8 \n256 9 257 10 258 \n11 259 12 260 13 \n261 14 262 15 263 \n16 264 17 265 18 \n266 19 267 20 268 \n21 269 22 270 23 \n271 24 272 25 273 \n26 274 27 275 28 \n276 29 277 30 278 \n31 279 32 280 33 \n281 34 282 35 283 \n36 284 37 285 38 \n286 39 287 40 288 \n41 289 42 290 43 \n291 44 292 45 293 \n46 294 47 295 48 \n296 49 297 50 298 \n51 299 52 300 53 \n301 54 302 55 303 \n56 304 57 305 58 \n306 59 307 60 308 \n61 309 62 310 63 \n311 64 312 65 313 \n66 314 67 315..." }, { "input": "99 99", "output": "1 4902 2 4903 3 4904 4 4905 5 4906 6 4907 7 4908 8 4909 9 4910 10 4911 11 4912 12 4913 13 4914 14 4915 15 4916 16 4917 17 4918 18 4919 19 4920 20 4921 21 4922 22 4923 23 4924 24 4925 25 4926 26 4927 27 4928 28 4929 29 4930 30 4931 31 4932 32 4933 33 4934 34 4935 35 4936 36 4937 37 4938 38 4939 39 4940 40 4941 41 4942 42 4943 43 4944 44 4945 45 4946 46 4947 47 4948 48 4949 49 4950 50 \n4951 51 4952 52 4953 53 4954 54 4955 55 4956 56 4957 57 4958 58 4959 59 4960 60 4961 61 4962 62 4963 63 4964 64 4965 65 496..." }, { "input": "99 100", "output": "1 4951 2 4952 3 4953 4 4954 5 4955 6 4956 7 4957 8 4958 9 4959 10 4960 11 4961 12 4962 13 4963 14 4964 15 4965 16 4966 17 4967 18 4968 19 4969 20 4970 21 4971 22 4972 23 4973 24 4974 25 4975 26 4976 27 4977 28 4978 29 4979 30 4980 31 4981 32 4982 33 4983 34 4984 35 4985 36 4986 37 4987 38 4988 39 4989 40 4990 41 4991 42 4992 43 4993 44 4994 45 4995 46 4996 47 4997 48 4998 49 4999 50 5000 \n5001 51 5002 52 5003 53 5004 54 5005 55 5006 56 5007 57 5008 58 5009 59 5010 60 5011 61 5012 62 5013 63 5014 64 5015 6..." }, { "input": "100 2", "output": "1 101 \n102 2 \n3 103 \n104 4 \n5 105 \n106 6 \n7 107 \n108 8 \n9 109 \n110 10 \n11 111 \n112 12 \n13 113 \n114 14 \n15 115 \n116 16 \n17 117 \n118 18 \n19 119 \n120 20 \n21 121 \n122 22 \n23 123 \n124 24 \n25 125 \n126 26 \n27 127 \n128 28 \n29 129 \n130 30 \n31 131 \n132 32 \n33 133 \n134 34 \n35 135 \n136 36 \n37 137 \n138 38 \n39 139 \n140 40 \n41 141 \n142 42 \n43 143 \n144 44 \n45 145 \n146 46 \n47 147 \n148 48 \n49 149 \n150 50 \n51 151 \n152 52 \n53 153 \n154 54 \n55 155 \n156 56 \n57 157 \n158 58 ..." }, { "input": "100 3", "output": "1 151 2 \n152 3 153 \n4 154 5 \n155 6 156 \n7 157 8 \n158 9 159 \n10 160 11 \n161 12 162 \n13 163 14 \n164 15 165 \n16 166 17 \n167 18 168 \n19 169 20 \n170 21 171 \n22 172 23 \n173 24 174 \n25 175 26 \n176 27 177 \n28 178 29 \n179 30 180 \n31 181 32 \n182 33 183 \n34 184 35 \n185 36 186 \n37 187 38 \n188 39 189 \n40 190 41 \n191 42 192 \n43 193 44 \n194 45 195 \n46 196 47 \n197 48 198 \n49 199 50 \n200 51 201 \n52 202 53 \n203 54 204 \n55 205 56 \n206 57 207 \n58 208 59 \n209 60 210 \n61 211 62 \n212 63 2..." }, { "input": "100 4", "output": "1 201 2 202 \n203 3 204 4 \n5 205 6 206 \n207 7 208 8 \n9 209 10 210 \n211 11 212 12 \n13 213 14 214 \n215 15 216 16 \n17 217 18 218 \n219 19 220 20 \n21 221 22 222 \n223 23 224 24 \n25 225 26 226 \n227 27 228 28 \n29 229 30 230 \n231 31 232 32 \n33 233 34 234 \n235 35 236 36 \n37 237 38 238 \n239 39 240 40 \n41 241 42 242 \n243 43 244 44 \n45 245 46 246 \n247 47 248 48 \n49 249 50 250 \n251 51 252 52 \n53 253 54 254 \n255 55 256 56 \n57 257 58 258 \n259 59 260 60 \n61 261 62 262 \n263 63 264 64 \n65 265 6..." }, { "input": "100 5", "output": "1 251 2 252 3 \n253 4 254 5 255 \n6 256 7 257 8 \n258 9 259 10 260 \n11 261 12 262 13 \n263 14 264 15 265 \n16 266 17 267 18 \n268 19 269 20 270 \n21 271 22 272 23 \n273 24 274 25 275 \n26 276 27 277 28 \n278 29 279 30 280 \n31 281 32 282 33 \n283 34 284 35 285 \n36 286 37 287 38 \n288 39 289 40 290 \n41 291 42 292 43 \n293 44 294 45 295 \n46 296 47 297 48 \n298 49 299 50 300 \n51 301 52 302 53 \n303 54 304 55 305 \n56 306 57 307 58 \n308 59 309 60 310 \n61 311 62 312 63 \n313 64 314 65 315 \n66 316 67 317..." }, { "input": "100 99", "output": "1 4951 2 4952 3 4953 4 4954 5 4955 6 4956 7 4957 8 4958 9 4959 10 4960 11 4961 12 4962 13 4963 14 4964 15 4965 16 4966 17 4967 18 4968 19 4969 20 4970 21 4971 22 4972 23 4973 24 4974 25 4975 26 4976 27 4977 28 4978 29 4979 30 4980 31 4981 32 4982 33 4983 34 4984 35 4985 36 4986 37 4987 38 4988 39 4989 40 4990 41 4991 42 4992 43 4993 44 4994 45 4995 46 4996 47 4997 48 4998 49 4999 50 \n5000 51 5001 52 5002 53 5003 54 5004 55 5005 56 5006 57 5007 58 5008 59 5009 60 5010 61 5011 62 5012 63 5013 64 5014 65 501..." }, { "input": "100 100", "output": "1 5001 2 5002 3 5003 4 5004 5 5005 6 5006 7 5007 8 5008 9 5009 10 5010 11 5011 12 5012 13 5013 14 5014 15 5015 16 5016 17 5017 18 5018 19 5019 20 5020 21 5021 22 5022 23 5023 24 5024 25 5025 26 5026 27 5027 28 5028 29 5029 30 5030 31 5031 32 5032 33 5033 34 5034 35 5035 36 5036 37 5037 38 5038 39 5039 40 5040 41 5041 42 5042 43 5043 44 5044 45 5045 46 5046 47 5047 48 5048 49 5049 50 5050 \n5051 51 5052 52 5053 53 5054 54 5055 55 5056 56 5057 57 5058 58 5059 59 5060 60 5061 61 5062 62 5063 63 5064 64 5065 6..." }, { "input": "8 97", "output": "1 389 2 390 3 391 4 392 5 393 6 394 7 395 8 396 9 397 10 398 11 399 12 400 13 401 14 402 15 403 16 404 17 405 18 406 19 407 20 408 21 409 22 410 23 411 24 412 25 413 26 414 27 415 28 416 29 417 30 418 31 419 32 420 33 421 34 422 35 423 36 424 37 425 38 426 39 427 40 428 41 429 42 430 43 431 44 432 45 433 46 434 47 435 48 436 49 \n437 50 438 51 439 52 440 53 441 54 442 55 443 56 444 57 445 58 446 59 447 60 448 61 449 62 450 63 451 64 452 65 453 66 454 67 455 68 456 69 457 70 458 71 459 72 460 73 461 74 462 ..." }, { "input": "33 81", "output": "1 1338 2 1339 3 1340 4 1341 5 1342 6 1343 7 1344 8 1345 9 1346 10 1347 11 1348 12 1349 13 1350 14 1351 15 1352 16 1353 17 1354 18 1355 19 1356 20 1357 21 1358 22 1359 23 1360 24 1361 25 1362 26 1363 27 1364 28 1365 29 1366 30 1367 31 1368 32 1369 33 1370 34 1371 35 1372 36 1373 37 1374 38 1375 39 1376 40 1377 41 \n1378 42 1379 43 1380 44 1381 45 1382 46 1383 47 1384 48 1385 49 1386 50 1387 51 1388 52 1389 53 1390 54 1391 55 1392 56 1393 57 1394 58 1395 59 1396 60 1397 61 1398 62 1399 63 1400 64 1401 65 140..." }, { "input": "11 17", "output": "1 95 2 96 3 97 4 98 5 99 6 100 7 101 8 102 9 \n103 10 104 11 105 12 106 13 107 14 108 15 109 16 110 17 111 \n18 112 19 113 20 114 21 115 22 116 23 117 24 118 25 119 26 \n120 27 121 28 122 29 123 30 124 31 125 32 126 33 127 34 128 \n35 129 36 130 37 131 38 132 39 133 40 134 41 135 42 136 43 \n137 44 138 45 139 46 140 47 141 48 142 49 143 50 144 51 145 \n52 146 53 147 54 148 55 149 56 150 57 151 58 152 59 153 60 \n154 61 155 62 156 63 157 64 158 65 159 66 160 67 161 68 162 \n69 163 70 164 71 165 72 166 73 16..." }, { "input": "36 1", "output": "1 \n19 \n2 \n20 \n3 \n21 \n4 \n22 \n5 \n23 \n6 \n24 \n7 \n25 \n8 \n26 \n9 \n27 \n10 \n28 \n11 \n29 \n12 \n30 \n13 \n31 \n14 \n32 \n15 \n33 \n16 \n34 \n17 \n35 \n18 \n36 " }, { "input": "62 85", "output": "1 2636 2 2637 3 2638 4 2639 5 2640 6 2641 7 2642 8 2643 9 2644 10 2645 11 2646 12 2647 13 2648 14 2649 15 2650 16 2651 17 2652 18 2653 19 2654 20 2655 21 2656 22 2657 23 2658 24 2659 25 2660 26 2661 27 2662 28 2663 29 2664 30 2665 31 2666 32 2667 33 2668 34 2669 35 2670 36 2671 37 2672 38 2673 39 2674 40 2675 41 2676 42 2677 43 \n2678 44 2679 45 2680 46 2681 47 2682 48 2683 49 2684 50 2685 51 2686 52 2687 53 2688 54 2689 55 2690 56 2691 57 2692 58 2693 59 2694 60 2695 61 2696 62 2697 63 2698 64 2699 65 270..." }, { "input": "39 69", "output": "1 1347 2 1348 3 1349 4 1350 5 1351 6 1352 7 1353 8 1354 9 1355 10 1356 11 1357 12 1358 13 1359 14 1360 15 1361 16 1362 17 1363 18 1364 19 1365 20 1366 21 1367 22 1368 23 1369 24 1370 25 1371 26 1372 27 1373 28 1374 29 1375 30 1376 31 1377 32 1378 33 1379 34 1380 35 \n1381 36 1382 37 1383 38 1384 39 1385 40 1386 41 1387 42 1388 43 1389 44 1390 45 1391 46 1392 47 1393 48 1394 49 1395 50 1396 51 1397 52 1398 53 1399 54 1400 55 1401 56 1402 57 1403 58 1404 59 1405 60 1406 61 1407 62 1408 63 1409 64 1410 65 141..." }, { "input": "64 5", "output": "1 161 2 162 3 \n163 4 164 5 165 \n6 166 7 167 8 \n168 9 169 10 170 \n11 171 12 172 13 \n173 14 174 15 175 \n16 176 17 177 18 \n178 19 179 20 180 \n21 181 22 182 23 \n183 24 184 25 185 \n26 186 27 187 28 \n188 29 189 30 190 \n31 191 32 192 33 \n193 34 194 35 195 \n36 196 37 197 38 \n198 39 199 40 200 \n41 201 42 202 43 \n203 44 204 45 205 \n46 206 47 207 48 \n208 49 209 50 210 \n51 211 52 212 53 \n213 54 214 55 215 \n56 216 57 217 58 \n218 59 219 60 220 \n61 221 62 222 63 \n223 64 224 65 225 \n66 226 67 227..." }, { "input": "90 89", "output": "1 4006 2 4007 3 4008 4 4009 5 4010 6 4011 7 4012 8 4013 9 4014 10 4015 11 4016 12 4017 13 4018 14 4019 15 4020 16 4021 17 4022 18 4023 19 4024 20 4025 21 4026 22 4027 23 4028 24 4029 25 4030 26 4031 27 4032 28 4033 29 4034 30 4035 31 4036 32 4037 33 4038 34 4039 35 4040 36 4041 37 4042 38 4043 39 4044 40 4045 41 4046 42 4047 43 4048 44 4049 45 \n4050 46 4051 47 4052 48 4053 49 4054 50 4055 51 4056 52 4057 53 4058 54 4059 55 4060 56 4061 57 4062 58 4063 59 4064 60 4065 61 4066 62 4067 63 4068 64 4069 65 407..." }, { "input": "67 73", "output": "1 2447 2 2448 3 2449 4 2450 5 2451 6 2452 7 2453 8 2454 9 2455 10 2456 11 2457 12 2458 13 2459 14 2460 15 2461 16 2462 17 2463 18 2464 19 2465 20 2466 21 2467 22 2468 23 2469 24 2470 25 2471 26 2472 27 2473 28 2474 29 2475 30 2476 31 2477 32 2478 33 2479 34 2480 35 2481 36 2482 37 \n2483 38 2484 39 2485 40 2486 41 2487 42 2488 43 2489 44 2490 45 2491 46 2492 47 2493 48 2494 49 2495 50 2496 51 2497 52 2498 53 2499 54 2500 55 2501 56 2502 57 2503 58 2504 59 2505 60 2506 61 2507 62 2508 63 2509 64 2510 65 251..." }, { "input": "40 75", "output": "1 1501 2 1502 3 1503 4 1504 5 1505 6 1506 7 1507 8 1508 9 1509 10 1510 11 1511 12 1512 13 1513 14 1514 15 1515 16 1516 17 1517 18 1518 19 1519 20 1520 21 1521 22 1522 23 1523 24 1524 25 1525 26 1526 27 1527 28 1528 29 1529 30 1530 31 1531 32 1532 33 1533 34 1534 35 1535 36 1536 37 1537 38 \n1538 39 1539 40 1540 41 1541 42 1542 43 1543 44 1544 45 1545 46 1546 47 1547 48 1548 49 1549 50 1550 51 1551 52 1552 53 1553 54 1554 55 1555 56 1556 57 1557 58 1558 59 1559 60 1560 61 1561 62 1562 63 1563 64 1564 65 156..." }, { "input": "10 13", "output": "1 66 2 67 3 68 4 69 5 70 6 71 7 \n72 8 73 9 74 10 75 11 76 12 77 13 78 \n14 79 15 80 16 81 17 82 18 83 19 84 20 \n85 21 86 22 87 23 88 24 89 25 90 26 91 \n27 92 28 93 29 94 30 95 31 96 32 97 33 \n98 34 99 35 100 36 101 37 102 38 103 39 104 \n40 105 41 106 42 107 43 108 44 109 45 110 46 \n111 47 112 48 113 49 114 50 115 51 116 52 117 \n53 118 54 119 55 120 56 121 57 122 58 123 59 \n124 60 125 61 126 62 127 63 128 64 129 65 130 " }, { "input": "33 51", "output": "1 843 2 844 3 845 4 846 5 847 6 848 7 849 8 850 9 851 10 852 11 853 12 854 13 855 14 856 15 857 16 858 17 859 18 860 19 861 20 862 21 863 22 864 23 865 24 866 25 867 26 \n868 27 869 28 870 29 871 30 872 31 873 32 874 33 875 34 876 35 877 36 878 37 879 38 880 39 881 40 882 41 883 42 884 43 885 44 886 45 887 46 888 47 889 48 890 49 891 50 892 51 893 \n52 894 53 895 54 896 55 897 56 898 57 899 58 900 59 901 60 902 61 903 62 904 63 905 64 906 65 907 66 908 67 909 68 910 69 911 70 912 71 913 72 914 73 915 74 91..." }, { "input": "4 38", "output": "1 77 2 78 3 79 4 80 5 81 6 82 7 83 8 84 9 85 10 86 11 87 12 88 13 89 14 90 15 91 16 92 17 93 18 94 19 95 \n96 20 97 21 98 22 99 23 100 24 101 25 102 26 103 27 104 28 105 29 106 30 107 31 108 32 109 33 110 34 111 35 112 36 113 37 114 38 \n39 115 40 116 41 117 42 118 43 119 44 120 45 121 46 122 47 123 48 124 49 125 50 126 51 127 52 128 53 129 54 130 55 131 56 132 57 133 \n134 58 135 59 136 60 137 61 138 62 139 63 140 64 141 65 142 66 143 67 144 68 145 69 146 70 147 71 148 72 149 73 150 74 151 75 152 76 " }, { "input": "27 76", "output": "1 1027 2 1028 3 1029 4 1030 5 1031 6 1032 7 1033 8 1034 9 1035 10 1036 11 1037 12 1038 13 1039 14 1040 15 1041 16 1042 17 1043 18 1044 19 1045 20 1046 21 1047 22 1048 23 1049 24 1050 25 1051 26 1052 27 1053 28 1054 29 1055 30 1056 31 1057 32 1058 33 1059 34 1060 35 1061 36 1062 37 1063 38 1064 \n1065 39 1066 40 1067 41 1068 42 1069 43 1070 44 1071 45 1072 46 1073 47 1074 48 1075 49 1076 50 1077 51 1078 52 1079 53 1080 54 1081 55 1082 56 1083 57 1084 58 1085 59 1086 60 1087 61 1088 62 1089 63 1090 64 1091 6..." }, { "input": "98 15", "output": "1 736 2 737 3 738 4 739 5 740 6 741 7 742 8 \n743 9 744 10 745 11 746 12 747 13 748 14 749 15 750 \n16 751 17 752 18 753 19 754 20 755 21 756 22 757 23 \n758 24 759 25 760 26 761 27 762 28 763 29 764 30 765 \n31 766 32 767 33 768 34 769 35 770 36 771 37 772 38 \n773 39 774 40 775 41 776 42 777 43 778 44 779 45 780 \n46 781 47 782 48 783 49 784 50 785 51 786 52 787 53 \n788 54 789 55 790 56 791 57 792 58 793 59 794 60 795 \n61 796 62 797 63 798 64 799 65 800 66 801 67 802 68 \n803 69 804 70 805 71 806 72 80..." }, { "input": "21 53", "output": "1 558 2 559 3 560 4 561 5 562 6 563 7 564 8 565 9 566 10 567 11 568 12 569 13 570 14 571 15 572 16 573 17 574 18 575 19 576 20 577 21 578 22 579 23 580 24 581 25 582 26 583 27 \n584 28 585 29 586 30 587 31 588 32 589 33 590 34 591 35 592 36 593 37 594 38 595 39 596 40 597 41 598 42 599 43 600 44 601 45 602 46 603 47 604 48 605 49 606 50 607 51 608 52 609 53 610 \n54 611 55 612 56 613 57 614 58 615 59 616 60 617 61 618 62 619 63 620 64 621 65 622 66 623 67 624 68 625 69 626 70 627 71 628 72 629 73 630 74 63..." }, { "input": "92 39", "output": "1 1795 2 1796 3 1797 4 1798 5 1799 6 1800 7 1801 8 1802 9 1803 10 1804 11 1805 12 1806 13 1807 14 1808 15 1809 16 1810 17 1811 18 1812 19 1813 20 \n1814 21 1815 22 1816 23 1817 24 1818 25 1819 26 1820 27 1821 28 1822 29 1823 30 1824 31 1825 32 1826 33 1827 34 1828 35 1829 36 1830 37 1831 38 1832 39 1833 \n40 1834 41 1835 42 1836 43 1837 44 1838 45 1839 46 1840 47 1841 48 1842 49 1843 50 1844 51 1845 52 1846 53 1847 54 1848 55 1849 56 1850 57 1851 58 1852 59 \n1853 60 1854 61 1855 62 1856 63 1857 64 1858 65..." }, { "input": "15 78", "output": "1 586 2 587 3 588 4 589 5 590 6 591 7 592 8 593 9 594 10 595 11 596 12 597 13 598 14 599 15 600 16 601 17 602 18 603 19 604 20 605 21 606 22 607 23 608 24 609 25 610 26 611 27 612 28 613 29 614 30 615 31 616 32 617 33 618 34 619 35 620 36 621 37 622 38 623 39 624 \n625 40 626 41 627 42 628 43 629 44 630 45 631 46 632 47 633 48 634 49 635 50 636 51 637 52 638 53 639 54 640 55 641 56 642 57 643 58 644 59 645 60 646 61 647 62 648 63 649 64 650 65 651 66 652 67 653 68 654 69 655 70 656 71 657 72 658 73 659 74 ..." } ]
124
7,065,600
-1
7,966
746
New Roads
[ "constructive algorithms", "graphs", "trees" ]
null
null
There are *n* cities in Berland, each of them has a unique id — an integer from 1 to *n*, the capital is the one with id 1. Now there is a serious problem in Berland with roads — there are no roads. That is why there was a decision to build *n*<=-<=1 roads so that there will be exactly one simple path between each pair of cities. In the construction plan *t* integers *a*1,<=*a*2,<=...,<=*a**t* were stated, where *t* equals to the distance from the capital to the most distant city, concerning new roads. *a**i* equals the number of cities which should be at the distance *i* from the capital. The distance between two cities is the number of roads one has to pass on the way from one city to another. Also, it was decided that among all the cities except the capital there should be exactly *k* cities with exactly one road going from each of them. Such cities are dead-ends and can't be economically attractive. In calculation of these cities the capital is not taken into consideration regardless of the number of roads from it. Your task is to offer a plan of road's construction which satisfies all the described conditions or to inform that it is impossible.
The first line contains three positive numbers *n*, *t* and *k* (2<=≤<=*n*<=≤<=2·105, 1<=≤<=*t*,<=*k*<=&lt;<=*n*) — the distance to the most distant city from the capital and the number of cities which should be dead-ends (the capital in this number is not taken into consideration). The second line contains a sequence of *t* integers *a*1,<=*a*2,<=...,<=*a**t* (1<=≤<=*a**i*<=&lt;<=*n*), the *i*-th number is the number of cities which should be at the distance *i* from the capital. It is guaranteed that the sum of all the values *a**i* equals *n*<=-<=1.
If it is impossible to built roads which satisfy all conditions, print -1. Otherwise, in the first line print one integer *n* — the number of cities in Berland. In the each of the next *n*<=-<=1 line print two integers — the ids of cities that are connected by a road. Each road should be printed exactly once. You can print the roads and the cities connected by a road in any order. If there are multiple answers, print any of them. Remember that the capital has id 1.
[ "7 3 3\n2 3 1\n", "14 5 6\n4 4 2 2 1\n", "3 1 1\n2\n" ]
[ "7\n1 3\n2 1\n2 6\n2 4\n7 4\n3 5\n", "14\n3 1\n1 4\n11 6\n1 2\n10 13\n6 10\n10 12\n14 12\n8 4\n5 1\n3 7\n2 6\n5 9\n", "-1\n" ]
none
[ { "input": "7 3 3\n2 3 1", "output": "7\n1 2\n2 6\n5 3\n2 4\n1 3\n7 4" }, { "input": "14 5 6\n4 4 2 2 1", "output": "14\n12 14\n7 3\n6 10\n5 1\n13 10\n1 3\n8 4\n9 5\n4 1\n6 2\n12 10\n6 11\n2 1" }, { "input": "3 1 1\n2", "output": "-1" }, { "input": "6 3 3\n1 2 2", "output": "6\n3 5\n6 3\n2 1\n4 2\n2 3" }, { "input": "11 6 4\n1 2 2 1 3 1", "output": "11\n3 5\n11 8\n7 9\n10 7\n7 8\n2 3\n2 4\n6 4\n1 2\n5 7" }, { "input": "21 10 9\n2 1 3 1 1 1 3 2 3 3", "output": "21\n5 4\n14 18\n7 4\n12 10\n17 15\n2 1\n10 11\n20 16\n9 10\n14 16\n4 2\n1 3\n14 11\n8 9\n13 10\n16 19\n5 8\n16 21\n6 4\n15 12" }, { "input": "51 16 31\n1 3 3 3 4 4 3 5 4 1 3 3 3 2 3 5", "output": "51\n49 44\n28 32\n31 23\n36 33\n16 20\n9 12\n4 2\n42 45\n18 12\n44 51\n33 38\n11 8\n34 32\n23 20\n8 5\n39 36\n39 42\n48 44\n16 21\n12 16\n23 30\n12 19\n9 15\n20 24\n37 33\n13 9\n20 25\n23 29\n6 3\n17 12\n42 44\n50 44\n7 4\n36 40\n28 23\n35 32\n33 32\n9 6\n43 39\n36 41\n1 2\n47 44\n20 27\n22 16\n46 42\n14 9\n7 10\n5 2\n3 2\n20 26" }, { "input": "1001 179 490\n8 8 6 7 1 6 10 3 7 8 8 7 2 4 9 8 4 5 8 3 5 7 9 10 9 5 2 6 5 3 3 2 9 6 4 2 10 2 3 5 7 4 5 8 3 9 5 9 2 5 4 9 3 8 2 9 6 7 4 4 10 9 8 5 2 6 9 10 9 3 1 9 9 3 9 4 10 1 7 4 4 9 2 5 6 4 9 3 4 10 1 3 10 3 3 10 9 2 5 6 6 1 1 9 4 9 9 7 5 5 10 3 8 7 10 3 2 10 6 1 4 4 5 7 4 7 1 1 8 5 8 6 2 9 3 3 5 8 7 1 8 3 6 2 8 10 4 5 1 6 3 4 6 9 8 10 1 3 9 9 4 6 6 7 10 2 4 8 2 1 5 4 5 3 7 2 5 9 3", "output": "1001\n695 689\n737 746\n383 376\n188 185\n312 318\n901 891\n33 39\n413 404\n793 794\n865 854\n831 839\n811 814\n340 351\n944 952\n985 991\n569 579\n776 770\n845 840\n20 26\n1 2\n56 48\n778 789\n471 475\n811 815\n415 419\n767 758\n146 137\n510 498\n621 612\n383 389\n649 663\n360 369\n83 87\n756 750\n531 534\n164 169\n489 494\n258 255\n24 31\n513 516\n188 193\n794 802\n740 729\n440 449\n890 887\n738 729\n444 434\n813 811\n649 646\n240 244\n704 714\n905 891\n167 158\n192 188\n305 302\n958 962\n169 175\n705 70..." }, { "input": "200000 5 190092\n47191 35051 33744 40989 43024", "output": "200000\n115988 194495\n115988 186970\n136478 82244\n150960 82244\n185127 115988\n191591 115988\n187442 115988\n115988 181548\n115988 167357\n90874 47193\n115988 182235\n166965 115988\n115988 195352\n115988 177339\n188896 115988\n186993 115988\n115988 192573\n172293 115988\n115988 199003\n196650 115988\n166407 115988\n115988 186132\n115988 192130\n115988 192121\n115988 175735\n178910 115988\n121195 82244\n115988 193692\n188417 115988\n127665 82244\n199363 115988\n155907 82244\n147227 82244\n115988 186681\n1..." }, { "input": "2000 3 1337\n970 632 397", "output": "2000\n1 695\n1786 972\n1327 357\n1 188\n1253 283\n972 1891\n1787 972\n1 413\n156 1126\n865 1\n1705 972\n1 814\n1 348\n992 1624\n1068 98\n1 575\n972 1957\n1 845\n149 1119\n972 1993\n1 54\n1 789\n1 475\n1 815\n1 419\n1841 972\n1 147\n507 1\n1 621\n427 1397\n1 663\n1118 148\n1 87\n756 1\n556 1526\n972 1970\n972 1837\n1872 972\n1 31\n1 517\n192 1\n391 1361\n242 1212\n1 449\n890 1\n1379 409\n983 1615\n813 1\n1 649\n1128 158\n1945 972\n978 1610\n167 1\n319 1289\n1592 622\n1986 972\n371 1341\n1 705\n1524 554\n175..." }, { "input": "2001 34 1714\n28 91 43 47 52 42 69 95 11 70 59 68 88 92 71 19 7 70 100 66 1 38 36 88 58 52 87 85 40 75 26 42 85 99", "output": "2001\n294 336\n954 1024\n345 303\n1265 1376\n1463 1596\n1776 1892\n1025 954\n487 469\n1470 1411\n1411 1528\n2 61\n954 1090\n1194 1190\n228 280\n1872 1776\n1265 1403\n1485 1411\n1644 1550\n941 857\n869 765\n568 480\n1353 1433\n1476 1411\n609 759\n469 501\n944 857\n699 609\n1635 1688\n948 928\n609 755\n1229 1191\n1463 1579\n1955 1818\n765 912\n907 765\n1024 1158\n263 350\n550 665\n954 1083\n1969 1818\n876 765\n104 2\n1003 947\n1919 1818\n609 744\n1776 1893\n1408 1265\n663 550\n1534 1411\n154 63\n28 1\n1411 1..." }, { "input": "150001 7 147760\n8286 37995 4975 28947 23162 15808 30827", "output": "150001\n139099 103367\n136999 103367\n125242 103367\n122472 103367\n35871 2\n80205 106310\n103367 134790\n128032 103367\n145793 103367\n46283 56175\n116621 80205\n131527 103367\n103367 146171\n103367 132459\n103367 133969\n103367 129263\n112482 80205\n85396 51258\n129908 103367\n108776 80205\n103367 142201\n127810 103367\n109636 80205\n103367 140692\n1 3018\n140898 103367\n103367 147446\n46283 75221\n103367 127230\n103367 142840\n2 41625\n103367 133122\n131090 103367\n101465 51258\n103367 123412\n138388 10..." }, { "input": "200000 5 195020\n47330 743 46163 36629 69134", "output": "200000\n94238 194495\n94238 186970\n136478 94238\n150960 94238\n185127 94238\n191591 94238\n187442 94238\n94238 181548\n94238 167357\n91616 47332\n94238 182235\n166965 94238\n94238 195352\n94238 177339\n188896 94238\n186993 94238\n94238 192573\n172293 94238\n94238 199003\n196650 94238\n166407 94238\n94238 186132\n94238 192130\n94238 192121\n94238 175735\n178910 94238\n124686 48075\n94238 193692\n188417 94238\n94528 48365\n199363 94238\n155907 94238\n147227 94238\n94238 186681\n189754 94238\n94238 195609\n1..." }, { "input": "200000 18 199982\n19878 5843 2935 16419 14574 12751 15665 18170 12578 14385 6515 9607 4121 224 14079 4972 10940 16343", "output": "200000\n174928 167746\n167746 176523\n172718 191873\n153667 171107\n172718 193613\n172718 197400\n172718 192738\n198484 172718\n110297 88067\n172718 186509\n139715 153381\n153667 167876\n167746 180125\n172718 185267\n153443 155797\n172718 190718\n116189 88067\n163463 153443\n172718 196975\n166400 153443\n150876 139715\n149767 139715\n172718 191355\n172718 187814\n156266 153443\n106237 131171\n178082 167746\n172718 194718\n172718 199797\n172718 193391\n172718 199525\n133200 143479\n184398 172718\n178155 167..." }, { "input": "200000 4 90823\n63010 23084 77424 36481", "output": "-1" }, { "input": "200000 1 199999\n199999", "output": "200000\n1 120418\n162163 1\n156675 1\n187544 1\n1 187800\n1 192966\n184902 1\n191949 1\n1 126665\n191367 1\n1 183579\n1 181788\n1 129868\n197312 1\n1 197439\n1 179130\n1 164809\n1 114623\n188700 1\n104067 1\n1 177790\n181957 1\n1 136583\n169479 1\n1 197419\n191015 1\n8501 1\n196002 1\n1 161615\n1 199967\n1 191729\n184391 1\n1 187114\n194633 1\n173285 1\n1 189731\n1 172409\n174313 1\n126009 1\n177518 1\n189355 1\n1 168589\n184969 1\n1 179245\n1 158495\n171452 1\n130676 1\n1 198971\n180303 1\n190327 1\n15838..." }, { "input": "6390 29 1740\n407 261 213 375 118 127 276 322 353 166 120 308 178 261 270 275 157 26 36 159 20 272 152 316 213 237 167 372 232", "output": "-1" }, { "input": "2 1 1\n1", "output": "2\n2 1" }, { "input": "4 2 3\n1 2", "output": "-1" }, { "input": "17 5 11\n5 3 2 5 1", "output": "17\n3 8\n17 12\n14 10\n6 1\n10 7\n13 10\n4 1\n15 10\n1 3\n7 2\n1 2\n2 9\n10 12\n1 5\n16 10\n7 11" }, { "input": "16 4 10\n2 3 5 5", "output": "16\n8 5\n7 12\n7 4\n2 4\n2 1\n9 4\n4 11\n4 10\n5 3\n13 7\n7 15\n2 6\n1 3\n16 7\n14 7" }, { "input": "2 1 1\n1", "output": "2\n2 1" }, { "input": "3 2 2\n1 1", "output": "-1" }, { "input": "3 2 2\n1 1", "output": "-1" }, { "input": "4 2 3\n2 1", "output": "-1" }, { "input": "4 1 1\n3", "output": "-1" }, { "input": "4 3 1\n1 1 1", "output": "4\n2 1\n2 3\n3 4" }, { "input": "5 4 3\n1 1 1 1", "output": "-1" }, { "input": "5 4 3\n1 1 1 1", "output": "-1" }, { "input": "5 4 4\n1 1 1 1", "output": "-1" }, { "input": "5 4 4\n1 1 1 1", "output": "-1" }, { "input": "6 4 5\n1 1 1 2", "output": "-1" } ]
46
2,764,800
0
7,972
66
Petya and Java
[ "implementation", "strings" ]
A. Petya and Java
2
256
Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger. But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer *n*?" Petya knows only 5 integer types: 1) byte occupies 1 byte and allows you to store numbers from <=-<=128 to 127 2) short occupies 2 bytes and allows you to store numbers from <=-<=32768 to 32767 3) int occupies 4 bytes and allows you to store numbers from <=-<=2147483648 to 2147483647 4) long occupies 8 bytes and allows you to store numbers from <=-<=9223372036854775808 to 9223372036854775807 5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower. For all the types given above the boundary values are included in the value range. From this list, Petya wants to choose the smallest type that can store a positive integer *n*. Since BigInteger works much slower, Peter regards it last. Help him.
The first line contains a positive number *n*. It consists of no more than 100 digits and doesn't contain any leading zeros. The number *n* can't be represented as an empty string. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number *n*, in accordance with the data given above.
[ "127\n", "130\n", "123456789101112131415161718192021222324\n" ]
[ "byte\n", "short\n", "BigInteger\n" ]
none
[ { "input": "127", "output": "byte" }, { "input": "130", "output": "short" }, { "input": "123456789101112131415161718192021222324", "output": "BigInteger" }, { "input": "6", "output": "byte" }, { "input": "16", "output": "byte" }, { "input": "126", "output": "byte" }, { "input": "128", "output": "short" }, { "input": "32766", "output": "short" }, { "input": "111111", "output": "int" }, { "input": "22222", "output": "short" }, { "input": "32767", "output": "short" }, { "input": "32768", "output": "int" }, { "input": "32769", "output": "int" }, { "input": "2147483645", "output": "int" }, { "input": "2147483646", "output": "int" }, { "input": "2147483647", "output": "int" }, { "input": "2147483648", "output": "long" }, { "input": "2147483649", "output": "long" }, { "input": "9223372036854775805", "output": "long" }, { "input": "9223372036854775806", "output": "long" }, { "input": "9223372036854775807", "output": "long" }, { "input": "9223372036854775808", "output": "BigInteger" }, { "input": "9223372036854775809", "output": "BigInteger" }, { "input": "1111111111111111111111111111111111111111111111", "output": "BigInteger" }, { "input": "232", "output": "short" }, { "input": "241796563564014133460267652699", "output": "BigInteger" }, { "input": "29360359146807441660707083821018832188095237636414144034857851003419752010124705615779249", "output": "BigInteger" }, { "input": "337300529263821789926982715723773719445001702036602052198530564", "output": "BigInteger" }, { "input": "381127467969689863953686682245136076127159921", "output": "BigInteger" }, { "input": "2158324958633591462", "output": "long" }, { "input": "268659422768117401499491767189496733446324586965055954729177892248858259490346", "output": "BigInteger" }, { "input": "3023764505449745844381036446038799100004717936344985", "output": "BigInteger" }, { "input": "13408349824892484976400774", "output": "BigInteger" }, { "input": "18880842614378213198381172973704766723997934818440985546083314104481253291692101136681", "output": "BigInteger" }, { "input": "1180990956946757129733650596194933741", "output": "BigInteger" }, { "input": "73795216631038776655609800540262114612084443385902708034055020082090470662930545328551", "output": "BigInteger" }, { "input": "1658370691480968202384509492140362150472696196949", "output": "BigInteger" }, { "input": "59662093286671707493190399502717308574459619342109544431740791973099298641871347858082458491958703", "output": "BigInteger" }, { "input": "205505005582428018613354752739589866670902346355933720701937", "output": "BigInteger" }, { "input": "53348890623013817139699", "output": "BigInteger" }, { "input": "262373979958859125198440634134122707574734706745701184688685117904709744", "output": "BigInteger" }, { "input": "69113784278456828987289369893745977", "output": "BigInteger" }, { "input": "2210209454022702335652564247406666491086662454147967686455330365147159266087", "output": "BigInteger" }, { "input": "630105816139991597267787581532092408135", "output": "BigInteger" }, { "input": "800461429306907809762708270", "output": "BigInteger" }, { "input": "7685166910821197056344900917707673568669808490600751439157", "output": "BigInteger" }, { "input": "713549841568602590705962611607726022334779480510421458817648621376683672722573289661127894", "output": "BigInteger" }, { "input": "680504312323996476676434432", "output": "BigInteger" }, { "input": "3376595620091080825479292544658464163405755746884100218035", "output": "BigInteger" }, { "input": "303681723783491968617491075591006152690484825330764215796396316561122383310011589365655481", "output": "BigInteger" }, { "input": "4868659422768117401499491767189496733446324586965055954729177892248858259490346614099717639491763430", "output": "BigInteger" }, { "input": "3502376450544974584438103644603879910000471793634498544789130945841846713263971487355748226237288709", "output": "BigInteger" }, { "input": "2334083498248924849764007740114454487565621932425948046430072197452845278935316358800789014185793377", "output": "BigInteger" }, { "input": "1988808426143782131983811729737047667239979348184409855460833141044812532916921011366813880911319644", "output": "BigInteger" }, { "input": "1018099095694675712973365059619493374113337270925179793757322992466016001294122941535439492265169131", "output": "BigInteger" }, { "input": "8437952166310387766556098005402621146120844433859027080340550200820904706629305453285512716464931911", "output": "BigInteger" }, { "input": "6965837069148096820238450949214036215047269619694967357734070611376013382163559966747678150791825071", "output": "BigInteger" }, { "input": "4596620932866717074931903995027173085744596193421095444317407919730992986418713478580824584919587030", "output": "BigInteger" }, { "input": "1905505005582428018613354752739589866670902346355933720701937408006000562951996789032987808118459990", "output": "BigInteger" }, { "input": "8433488906230138171396997888322916936677429522910871017295155818340819168386140293774243244435122950", "output": "BigInteger" }, { "input": "6862373979958859125198440634134122707574734706745701184688685117904709744830303784215298687654884810", "output": "BigInteger" }, { "input": "4491137842784568289872893698937459777201151060689848471272003426250808340375567208957554901863756992", "output": "BigInteger" }, { "input": "9721020945402270233565256424740666649108666245414796768645533036514715926608741510409618545180420952", "output": "BigInteger" }, { "input": "7330105816139991597267787581532092408135003429259616955239761315950805521264994021242873979309182812", "output": "BigInteger" }, { "input": "2000461429306907809762708270752707617318091579531521957022940951538737203583768926365382290530636885", "output": "BigInteger" }, { "input": "9868516691082119705634490091770767356866980849060075143915700796802700437762260163478754592094654326", "output": "BigInteger" }, { "input": "8713549841568602590705962611607726022334779480510421458817648621376683672722573289661127894678771177", "output": "BigInteger" }, { "input": "4580504312323996476676434432646986768872786931159974634901608445720467716981185426771899006352697916", "output": "BigInteger" }, { "input": "2537659562009108082547929254465846416340575574688410021803548570097340949141688442074263189944614467", "output": "BigInteger" }, { "input": "1403681723783491968617491075591006152690484825330764215796396316561122383310011589365655481428540208", "output": "BigInteger" }, { "input": "26", "output": "byte" }, { "input": "302376450544", "output": "long" }, { "input": "13", "output": "byte" }, { "input": "188808426143", "output": "long" }, { "input": "118099095694675", "output": "long" }, { "input": "73795216631038", "output": "long" }, { "input": "1658370691480", "output": "long" }, { "input": "596620932866", "output": "long" }, { "input": "2055050055", "output": "int" }, { "input": "533488906", "output": "int" }, { "input": "26237397", "output": "int" }, { "input": "6911378", "output": "int" }, { "input": "221020945402270233", "output": "long" }, { "input": "63010581613999159", "output": "long" }, { "input": "80046142930", "output": "long" }, { "input": "7685166910821197", "output": "long" }, { "input": "71", "output": "byte" }, { "input": "6805043123239964766", "output": "long" }, { "input": "3376", "output": "short" }, { "input": "3036817237", "output": "long" } ]
62
0
0
7,980
250
Building Bridge
[ "geometry", "ternary search", "two pointers" ]
null
null
Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines *x*<==<=*a* and *x*<==<=*b* (0<=&lt;<=*a*<=&lt;<=*b*). The west village lies in a steppe at point *O*<==<=(0,<=0). There are *n* pathways leading from the village to the river, they end at points *A**i*<==<=(*a*,<=*y**i*). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are *m* twisted paths leading from this village to the river and ending at points *B**i*<==<=(*b*,<=*y*'*i*). The lengths of all these paths are known, the length of the path that leads from the eastern village to point *B**i*, equals *l**i*. The villagers want to choose exactly one point on the left bank of river *A**i*, exactly one point on the right bank *B**j* and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |*OA**i*|<=+<=|*A**i**B**j*|<=+<=*l**j*, where |*XY*| is the Euclidean distance between points *X* and *Y*) were minimum. The Euclidean distance between points (*x*1,<=*y*1) and (*x*2,<=*y*2) equals . Help them and find the required pair of points.
The first line contains integers *n*, *m*, *a*, *b* (1<=≤<=*n*,<=*m*<=≤<=105, 0<=&lt;<=*a*<=&lt;<=*b*<=&lt;<=106). The second line contains *n* integers in the ascending order: the *i*-th integer determines the coordinate of point *A**i* and equals *y**i* (|*y**i*|<=≤<=106). The third line contains *m* integers in the ascending order: the *i*-th integer determines the coordinate of point *B**i* and equals *y*'*i* (|*y*'*i*|<=≤<=106). The fourth line contains *m* more integers: the *i*-th of them determines the length of the path that connects the eastern village and point *B**i*, and equals *l**i* (1<=≤<=*l**i*<=≤<=106). It is guaranteed, that there is such a point *C* with abscissa at least *b*, that |*B**i**C*|<=≤<=*l**i* for all *i* (1<=≤<=*i*<=≤<=*m*). It is guaranteed that no two points *A**i* coincide. It is guaranteed that no two points *B**i* coincide.
Print two integers — the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to *n*, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to *m* in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10<=-<=6 in absolute or relative value.
[ "3 2 3 5\n-2 -1 4\n-1 2\n7 3\n" ]
[ "2 2" ]
none
[ { "input": "3 2 3 5\n-2 -1 4\n-1 2\n7 3", "output": "2 2" }, { "input": "1 1 10 20\n5\n-5\n1", "output": "1 1" }, { "input": "2 2 1 2\n-1 10\n8 9\n3 7", "output": "1 1" }, { "input": "10 20 50 60\n-96 -75 32 37 42 43 44 57 61 65\n-95 -90 -86 -79 -65 -62 -47 -11 -8 -6 1 8 23 25 32 51 73 88 94 100\n138 75 132 116 49 43 96 166 96 161 146 112 195 192 201 186 251 254 220 227", "output": "2 6" } ]
1,000
11,571,200
0
7,982
243
The Brand New Function
[ "bitmasks" ]
null
null
Polycarpus has a sequence, consisting of *n* non-negative integers: *a*1,<=*a*2,<=...,<=*a**n*. Let's define function *f*(*l*,<=*r*) (*l*,<=*r* are integer, 1<=≤<=*l*<=≤<=*r*<=≤<=*n*) for sequence *a* as an operation of bitwise OR of all the sequence elements with indexes from *l* to *r*. Formally: *f*(*l*,<=*r*)<==<=*a**l* | *a**l*<=+<=1 | ...  | *a**r*. Polycarpus took a piece of paper and wrote out the values of function *f*(*l*,<=*r*) for all *l*,<=*r* (*l*,<=*r* are integer, 1<=≤<=*l*<=≤<=*r*<=≤<=*n*). Now he wants to know, how many distinct values he's got in the end. Help Polycarpus, count the number of distinct values of function *f*(*l*,<=*r*) for the given sequence *a*. Expression *x* | *y* means applying the operation of bitwise OR to numbers *x* and *y*. This operation exists in all modern programming languages, for example, in language C++ and Java it is marked as "|", in Pascal — as "or".
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements of sequence *a*. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=106) — the elements of sequence *a*.
Print a single integer — the number of distinct values of function *f*(*l*,<=*r*) for the given sequence *a*. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "3\n1 2 0\n", "10\n1 2 3 4 5 6 1 2 9 10\n" ]
[ "4", "11" ]
In the first test case Polycarpus will have 6 numbers written on the paper: *f*(1, 1) = 1, *f*(1, 2) = 3, *f*(1, 3) = 3, *f*(2, 2) = 2, *f*(2, 3) = 2, *f*(3, 3) = 0. There are exactly 4 distinct numbers among them: 0, 1, 2, 3.
[ { "input": "3\n1 2 0", "output": "4" }, { "input": "10\n1 2 3 4 5 6 1 2 9 10", "output": "11" }, { "input": "1\n123", "output": "1" }, { "input": "10\n6 8 4 5 1 9 10 2 3 7", "output": "15" }, { "input": "7\n1 2 4 8 16 32 64", "output": "28" }, { "input": "10\n375813 659427 484038 348181 432640 368050 271089 721588 345312 630771", "output": "29" }, { "input": "5\n0 1 2 0 4", "output": "7" }, { "input": "1\n0", "output": "1" }, { "input": "1\n1000000", "output": "1" } ]
2,000
10,547,200
0
8,011
555
Case of Matryoshkas
[ "implementation" ]
null
null
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of *n* matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to *n*. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1<=→<=2<=→<=4<=→<=5. In one second, you can perform one of the two following operations: - Having a matryoshka *a* that isn't nested in any other matryoshka and a matryoshka *b*, such that *b* doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put *a* in *b*; - Having a matryoshka *a* directly contained in matryoshka *b*, such that *b* is not nested in any other matryoshka, you may get *a* out of *b*. According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1<=→<=2<=→<=...<=→<=*n*). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.
The first line contains integers *n* (1<=≤<=*n*<=≤<=105) and *k* (1<=≤<=*k*<=≤<=105) — the number of matryoshkas and matryoshka chains in the initial configuration. The next *k* lines contain the descriptions of the chains: the *i*-th line first contains number *m**i* (1<=≤<=*m**i*<=≤<=*n*), and then *m**i* numbers *a**i*1,<=*a**i*2,<=...,<=*a**im**i* — the numbers of matryoshkas in the chain (matryoshka *a**i*1 is nested into matryoshka *a**i*2, that is nested into matryoshka *a**i*3, and so on till the matryoshka *a**im**i* that isn't nested into any other matryoshka). It is guaranteed that *m*1<=+<=*m*2<=+<=...<=+<=*m**k*<==<=*n*, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.
In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.
[ "3 2\n2 1 2\n1 3\n", "7 3\n3 1 3 7\n2 2 5\n2 4 6\n" ]
[ "1\n", "10\n" ]
In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3. In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.
[ { "input": "3 2\n2 1 2\n1 3", "output": "1" }, { "input": "7 3\n3 1 3 7\n2 2 5\n2 4 6", "output": "10" }, { "input": "1 1\n1 1", "output": "0" }, { "input": "3 2\n1 2\n2 1 3", "output": "3" }, { "input": "5 3\n1 4\n3 1 2 3\n1 5", "output": "2" }, { "input": "8 5\n2 1 2\n2 3 4\n1 5\n2 6 7\n1 8", "output": "8" }, { "input": "10 10\n1 5\n1 4\n1 10\n1 3\n1 7\n1 1\n1 8\n1 6\n1 9\n1 2", "output": "9" }, { "input": "20 6\n3 8 9 13\n3 4 14 20\n2 15 17\n3 2 5 11\n5 7 10 12 18 19\n4 1 3 6 16", "output": "33" }, { "input": "50 10\n6 17 21 31 42 45 49\n6 11 12 15 22 26 38\n3 9 29 36\n3 10 23 43\n5 14 19 28 46 48\n2 30 39\n6 13 20 24 33 37 47\n8 1 2 3 4 5 6 7 8\n7 16 18 25 27 34 40 44\n4 32 35 41 50", "output": "75" }, { "input": "13 8\n1 5\n2 8 10\n1 13\n4 1 2 3 11\n1 7\n2 6 12\n1 4\n1 9", "output": "13" }, { "input": "21 13\n1 18\n2 8 13\n1 21\n1 17\n2 7 9\n1 20\n1 19\n1 4\n1 16\n2 5 6\n3 12 14 15\n3 1 2 3\n2 10 11", "output": "24" }, { "input": "50 50\n1 2\n1 5\n1 28\n1 46\n1 42\n1 24\n1 3\n1 37\n1 33\n1 50\n1 23\n1 40\n1 43\n1 26\n1 49\n1 34\n1 8\n1 45\n1 15\n1 1\n1 22\n1 18\n1 27\n1 25\n1 13\n1 39\n1 38\n1 10\n1 44\n1 6\n1 17\n1 47\n1 7\n1 35\n1 20\n1 36\n1 31\n1 21\n1 32\n1 29\n1 4\n1 12\n1 19\n1 16\n1 11\n1 41\n1 9\n1 14\n1 30\n1 48", "output": "49" }, { "input": "100 3\n45 1 2 3 4 5 6 7 8 9 19 21 24 27 28 30 34 35 37 39 40 41 42 43 46 47 48 51 52 55 58 59 61 63 64 66 69 71 76 80 85 86 88 89 94 99\n26 10 11 15 18 23 29 31 33 36 38 44 49 54 56 60 62 65 75 78 82 83 84 95 96 97 98\n29 12 13 14 16 17 20 22 25 26 32 45 50 53 57 67 68 70 72 73 74 77 79 81 87 90 91 92 93 100", "output": "180" }, { "input": "100 19\n6 62 72 83 91 94 97\n3 61 84 99\n1 63\n5 46 53 56 69 78\n5 41 43 49 74 89\n5 55 57 79 85 87\n3 47 59 98\n3 64 76 82\n3 48 66 75\n2 60 88\n2 67 77\n4 40 51 73 95\n41 1 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 44 71 81\n4 58 65 90 93\n1 100\n5 39 45 52 80 86\n2 50 68\n1 92\n4 42 54 70 96", "output": "106" } ]
358
8,294,400
3
8,030
923
Primal Sport
[ "math", "number theory" ]
null
null
Alice and Bob begin their day with a quick game. They first choose a starting number *X*0<=≥<=3 and try to reach one million by the process described below. Alice goes first and then they take alternating turns. In the *i*-th turn, the player whose turn it is selects a prime number smaller than the current number, and announces the smallest multiple of this prime number that is not smaller than the current number. Formally, he or she selects a prime *p*<=&lt;<=*X**i*<=-<=1 and then finds the minimum *X**i*<=≥<=*X**i*<=-<=1 such that *p* divides *X**i*. Note that if the selected prime *p* already divides *X**i*<=-<=1, then the number does not change. Eve has witnessed the state of the game after two turns. Given *X*2, help her determine what is the smallest possible starting number *X*0. Note that the players don't necessarily play optimally. You should consider all possible game evolutions.
The input contains a single integer *X*2 (4<=≤<=*X*2<=≤<=106). It is guaranteed that the integer *X*2 is composite, that is, is not prime.
Output a single integer — the minimum possible *X*0.
[ "14\n", "20\n", "8192\n" ]
[ "6\n", "15\n", "8191\n" ]
In the first test, the smallest possible starting number is *X*<sub class="lower-index">0</sub> = 6. One possible course of the game is as follows: - Alice picks prime 5 and announces *X*<sub class="lower-index">1</sub> = 10 - Bob picks prime 7 and announces *X*<sub class="lower-index">2</sub> = 14. In the second case, let *X*<sub class="lower-index">0</sub> = 15. - Alice picks prime 2 and announces *X*<sub class="lower-index">1</sub> = 16 - Bob picks prime 5 and announces *X*<sub class="lower-index">2</sub> = 20.
[ { "input": "14", "output": "6" }, { "input": "20", "output": "15" }, { "input": "8192", "output": "8191" }, { "input": "1000000", "output": "998677" }, { "input": "959806", "output": "239958" }, { "input": "1452", "output": "1206" }, { "input": "4", "output": "3" }, { "input": "6", "output": "3" }, { "input": "8", "output": "7" }, { "input": "9", "output": "7" }, { "input": "10", "output": "4" }, { "input": "12", "output": "6" }, { "input": "15", "output": "8" }, { "input": "16", "output": "11" }, { "input": "110880", "output": "55440" }, { "input": "166320", "output": "110879" }, { "input": "221760", "output": "110880" }, { "input": "277200", "output": "138600" }, { "input": "332640", "output": "166320" }, { "input": "498960", "output": "332639" }, { "input": "554400", "output": "415798" }, { "input": "665280", "output": "498958" }, { "input": "720720", "output": "540538" }, { "input": "510510", "output": "255248" }, { "input": "570570", "output": "285282" }, { "input": "690690", "output": "460455" }, { "input": "959818", "output": "239958" }, { "input": "959878", "output": "239978" }, { "input": "959902", "output": "239978" }, { "input": "974847", "output": "324954" }, { "input": "974859", "output": "324978" }, { "input": "974931", "output": "324980" }, { "input": "885481", "output": "442272" }, { "input": "896809", "output": "447944" }, { "input": "908209", "output": "453632" }, { "input": "935089", "output": "467064" }, { "input": "720721", "output": "355298" }, { "input": "690691", "output": "342864" }, { "input": "959903", "output": "479702" }, { "input": "974932", "output": "470060" }, { "input": "935090", "output": "463950" }, { "input": "524288", "output": "524287" }, { "input": "524289", "output": "174768" }, { "input": "524286", "output": "262110" }, { "input": "531441", "output": "526737" }, { "input": "531442", "output": "262490" }, { "input": "531440", "output": "265704" }, { "input": "81", "output": "76" }, { "input": "999958", "output": "250008" }, { "input": "2048", "output": "1959" } ]
982
9,113,600
3
8,051
0
none
[ "none" ]
null
null
Arkady decides to observe a river for *n* consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the *i*-th day this value is equal to *m**i*. Define *d**i* as the number of marks strictly under the water level on the *i*-th day. You are to find out the minimum possible sum of *d**i* over all days. There are no marks on the channel before the first day.
The first line contains a single positive integer *n* (1<=≤<=*n*<=≤<=105) — the number of days. The second line contains *n* space-separated integers *m*1,<=*m*2,<=...,<=*m**n* (0<=≤<=*m**i*<=&lt;<=*i*) — the number of marks strictly above the water on each day.
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
[ "6\n0 1 0 3 0 2\n", "5\n0 1 2 1 2\n", "5\n0 1 1 2 2\n" ]
[ "6\n", "1\n", "0\n" ]
In the first example, the following figure shows an optimal case. Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case.
[ { "input": "6\n0 1 0 3 0 2", "output": "6" }, { "input": "5\n0 1 2 1 2", "output": "1" }, { "input": "5\n0 1 1 2 2", "output": "0" }, { "input": "1\n0", "output": "0" }, { "input": "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 23 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4", "output": "761" }, { "input": "2\n0 1", "output": "0" }, { "input": "2\n0 0", "output": "0" }, { "input": "3\n0 1 0", "output": "1" }, { "input": "3\n0 0 1", "output": "0" }, { "input": "3\n0 1 1", "output": "0" }, { "input": "3\n0 1 2", "output": "0" }, { "input": "3\n0 0 0", "output": "0" }, { "input": "4\n0 0 1 2", "output": "0" }, { "input": "4\n0 1 0 3", "output": "2" }, { "input": "4\n0 1 1 0", "output": "1" }, { "input": "4\n0 0 1 1", "output": "0" }, { "input": "5\n0 1 0 3 1", "output": "4" }, { "input": "6\n0 0 0 2 0 1", "output": "4" }, { "input": "7\n0 1 1 3 0 0 6", "output": "10" }, { "input": "8\n0 0 2 0 3 0 3 2", "output": "7" }, { "input": "9\n0 1 0 1 1 4 0 4 8", "output": "17" }, { "input": "10\n0 1 2 0 4 5 3 6 0 5", "output": "12" }, { "input": "10\n0 0 2 2 3 2 3 3 1 3", "output": "4" } ]
233
14,848,000
3
8,061
990
Post Lamps
[ "brute force", "greedy" ]
null
null
Adilbek's house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has $n$ positions to install lamps, they correspond to the integer numbers from $0$ to $n - 1$ on the OX axis. However, some positions are blocked and no post lamp can be placed there. There are post lamps of different types which differ only by their power. When placed in position $x$, post lamp of power $l$ illuminates the segment $[x; x + l]$. The power of each post lamp is always a positive integer number. The post lamp shop provides an infinite amount of lamps of each type from power $1$ to power $k$. Though each customer is only allowed to order post lamps of exactly one type. Post lamps of power $l$ cost $a_l$ each. What is the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street? If some lamps illuminate any other segment of the street, Adilbek does not care, so, for example, he may place a lamp of power $3$ in position $n - 1$ (even though its illumination zone doesn't completely belong to segment $[0; n]$).
The first line contains three integer numbers $n$, $m$ and $k$ ($1 \le k \le n \le 10^6$, $0 \le m \le n$) — the length of the segment of the street Adilbek wants to illuminate, the number of the blocked positions and the maximum power of the post lamp available. The second line contains $m$ integer numbers $s_1, s_2, \dots, s_m$ ($0 \le s_1 &lt; s_2 &lt; \dots s_m &lt; n$) — the blocked positions. The third line contains $k$ integer numbers $a_1, a_2, \dots, a_k$ ($1 \le a_i \le 10^6$) — the costs of the post lamps.
Print the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street. If illumintaing the entire segment $[0; n]$ is impossible, print -1.
[ "6 2 3\n1 3\n1 2 3\n", "4 3 4\n1 2 3\n1 10 100 1000\n", "5 1 5\n0\n3 3 3 3 3\n", "7 4 3\n2 4 5 6\n3 14 15\n" ]
[ "6\n", "1000\n", "-1\n", "-1\n" ]
none
[ { "input": "6 2 3\n1 3\n1 2 3", "output": "6" }, { "input": "4 3 4\n1 2 3\n1 10 100 1000", "output": "1000" }, { "input": "5 1 5\n0\n3 3 3 3 3", "output": "-1" }, { "input": "7 4 3\n2 4 5 6\n3 14 15", "output": "-1" }, { "input": "1 0 1\n\n1000000", "output": "1000000" }, { "input": "1 1 1\n0\n1000", "output": "-1" }, { "input": "3 2 3\n1 2\n1 1 1000000", "output": "1000000" }, { "input": "3 0 3\n\n333 500 1001", "output": "999" }, { "input": "3 0 3\n\n334 500 1001", "output": "1000" }, { "input": "6 2 3\n2 3\n1 1 3", "output": "9" }, { "input": "9 4 3\n3 4 7 8\n1 1 1", "output": "4" }, { "input": "11 4 6\n3 4 5 6\n1000000 1000000 1000000 1000000 1000000 1", "output": "3" }, { "input": "1000000 0 1\n\n999999", "output": "999999000000" }, { "input": "1000000 0 1\n\n1000000", "output": "1000000000000" }, { "input": "2 1 2\n1\n1 2", "output": "2" }, { "input": "2 1 1\n1\n1", "output": "-1" }, { "input": "4 1 3\n3\n3 2 9", "output": "4" }, { "input": "3 1 2\n1\n8 61", "output": "122" }, { "input": "20 2 10\n9 16\n109 58 165 715 341 620 574 732 653 675", "output": "638" }, { "input": "4 0 4\n\n1 4 4 3", "output": "3" }, { "input": "20 16 16\n1 2 3 4 5 6 8 9 10 11 13 14 15 16 18 19\n2 1 1 1 1 1 3 3 2 2 1 3 3 3 3 2", "output": "3" }, { "input": "10 3 2\n2 3 8\n2 4", "output": "-1" }, { "input": "4 1 3\n3\n838 185 210", "output": "370" }, { "input": "3 1 2\n2\n1 1", "output": "2" }, { "input": "3 1 1\n2\n1", "output": "-1" } ]
1,606
88,064,000
0
8,095
711
Chris and Magic Square
[ "constructive algorithms", "implementation" ]
null
null
ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a *n*<=×<=*n* magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell. Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal — and the secondary diagonal — ) are equal. Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=500) — the number of rows and columns of the magic grid. *n* lines follow, each of them contains *n* integers. The *j*-th number in the *i*-th of them denotes *a**i*,<=*j* (1<=≤<=*a**i*,<=*j*<=≤<=109 or *a**i*,<=*j*<==<=0), the number in the *i*-th row and *j*-th column of the magic grid. If the corresponding cell is empty, *a**i*,<=*j* will be equal to 0. Otherwise, *a**i*,<=*j* is positive. It is guaranteed that there is exactly one pair of integers *i*,<=*j* (1<=≤<=*i*,<=*j*<=≤<=*n*) such that *a**i*,<=*j*<==<=0.
Output a single integer, the positive integer *x* (1<=≤<=*x*<=≤<=1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer *x* does not exist, output <=-<=1 instead. If there are multiple solutions, you may print any of them.
[ "3\n4 0 2\n3 5 7\n8 1 6\n", "4\n1 1 1 1\n1 1 0 1\n1 1 1 1\n1 1 1 1\n", "4\n1 1 1 1\n1 1 0 1\n1 1 2 1\n1 1 1 1\n" ]
[ "9\n", "1\n", "-1\n" ]
In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed, The sum of numbers in each row is: 4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15. The sum of numbers in each column is: 4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15. The sum of numbers in the two diagonals is: 4 + 5 + 6 = 2 + 5 + 8 = 15. In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.
[ { "input": "3\n4 0 2\n3 5 7\n8 1 6", "output": "9" }, { "input": "4\n1 1 1 1\n1 1 0 1\n1 1 1 1\n1 1 1 1", "output": "1" }, { "input": "4\n1 1 1 1\n1 1 0 1\n1 1 2 1\n1 1 1 1", "output": "-1" }, { "input": "1\n0", "output": "1" }, { "input": "10\n92 67 99 74 1 51 8 58 15 40\n17 42 24 49 0 26 83 33 90 65\n98 73 80 55 7 57 14 64 16 41\n23 48 5 30 82 32 89 39 91 66\n4 54 81 56 88 63 20 70 22 47\n79 29 6 31 13 38 95 45 97 72\n85 60 87 62 19 69 21 71 3 28\n10 35 12 37 94 44 96 46 78 53\n86 61 93 68 25 75 2 52 9 34\n11 36 18 43 100 50 77 27 84 59", "output": "76" }, { "input": "4\n1000000000 1000000000 1000000000 1000000000\n1000000000 1000000000 1000000000 1000000000\n1000000000 1000000000 0 1000000000\n1000000000 1000000000 1000000000 1000000000", "output": "1000000000" }, { "input": "3\n3 8 1\n2 4 6\n7 0 5", "output": "-1" }, { "input": "3\n1 2 2\n2 2 1\n0 1 2", "output": "-1" }, { "input": "3\n1 6 10\n5 6 16\n0 5 1", "output": "-1" }, { "input": "3\n2 2 1\n1 2 2\n0 1 2", "output": "-1" }, { "input": "3\n1 2 2\n2 2 1\n2 1 0", "output": "-1" }, { "input": "3\n2016 2016 2016\n2016 0 2016\n2016 2016 2016", "output": "2016" }, { "input": "10\n92 67 99 74 1 51 8 58 15 40\n17 42 24 49 76 26 83 33 90 65\n98 73 80 55 7 57 14 64 16 41\n23 48 5 30 82 32 89 39 91 66\n4 54 81 56 88 63 20 70 22 47\n79 29 6 31 13 38 95 45 97 72\n85 60 87 62 19 69 21 71 3 28\n10 35 12 37 94 44 96 46 78 53\n86 61 93 68 25 75 2 52 0 34\n11 36 18 43 100 50 77 27 84 59", "output": "9" }, { "input": "10\n92 67 99 74 1 51 8 58 15 40\n17 42 24 49 76 26 83 33 90 65\n98 73 80 55 7 57 14 64 16 41\n23 48 5 30 82 32 89 39 91 66\n4 54 81 56 0 63 20 70 22 47\n79 29 6 31 13 38 95 45 97 72\n85 60 87 62 19 69 21 71 3 28\n10 35 12 37 94 44 96 46 78 53\n86 61 93 68 25 75 2 52 9 34\n11 36 18 43 100 50 77 27 84 59", "output": "88" }, { "input": "3\n2 2 1\n1 2 2\n2 1 0", "output": "-1" }, { "input": "10\n92 67 99 74 1 51 8 58 15 0\n17 42 24 49 76 26 83 33 90 65\n98 73 80 55 7 57 14 64 16 41\n23 48 5 30 82 32 89 39 91 66\n4 54 81 56 88 63 20 70 22 47\n79 29 6 31 13 38 95 45 97 72\n85 60 87 62 19 69 21 71 3 28\n10 35 12 37 94 44 96 46 78 53\n86 61 93 68 25 75 2 52 9 34\n11 36 18 43 100 50 77 27 84 59", "output": "40" }, { "input": "4\n2 2 2 2\n2 0 2 2\n3 2 2 1\n2 2 2 2", "output": "-1" }, { "input": "3\n1 15 5\n11 7 3\n9 0 13", "output": "-1" }, { "input": "3\n61 0 41\n11 31 51\n21 71 1", "output": "-1" }, { "input": "3\n3 0 3\n2 3 2\n2 3 2", "output": "-1" }, { "input": "3\n0 2 2\n3 1 1\n1 2 2", "output": "-1" }, { "input": "3\n1 0 1\n1 1 2\n1 1 1", "output": "-1" }, { "input": "3\n1 0 1\n2 1 2\n2 1 2", "output": "-1" }, { "input": "3\n1 0 1\n4 1 4\n1 1 1", "output": "-1" }, { "input": "3\n1 1 1\n1 1 0\n1 2 1", "output": "-1" }, { "input": "3\n2 0 1\n1 2 1\n1 1 2", "output": "-1" }, { "input": "3\n1 2 2\n3 1 1\n0 2 2", "output": "-1" }, { "input": "4\n0 1 1 1\n1 1 1 1\n1 1 1 2\n1 1 2 1", "output": "-1" }, { "input": "4\n1 1 0 1\n1 1 1 1\n1 1 1 1\n1 2 1 1", "output": "-1" }, { "input": "5\n1 1 1000000000 1000000000 1000000000\n1 1000000000 1 1000000000 1000000000\n0 1 1 1 1\n1 1000000000 1000000000 1000000000 1\n1 1000000000 1000000000 1 1000000000", "output": "2999999998" }, { "input": "3\n5 5 5\n6 5 0\n5 5 5", "output": "-1" }, { "input": "3\n1 0 1\n50 1 500\n2 1 2", "output": "-1" }, { "input": "9\n1 1000000000 1 1000000000 1 1000000000 1 1000000000 1\n1000000000 1 1000000000 1 1 1 1000000000 1 1000000000\n1 1000000000 1 1000000000 1 1000000000 1 1000000000 1\n1000000000 1 1000000000 1 1 1 1000000000 1 1000000000\n1 1 1 1 0 1 1 1 1\n1000000000 1 1000000000 1 1 1 1000000000 1 1000000000\n1 1000000000 1 1000000000 1 1000000000 1 1000000000 1\n1000000000 1 1000000000 1 1 1 1000000000 1 1000000000\n1 1000000000 1 1000000000 1 1000000000 1 1000000000 1", "output": "3999999997" }, { "input": "3\n7 22 1\n4 10 16\n19 0 13", "output": "-1" }, { "input": "5\n1 1 1 1 1\n1 1 1 1 0\n1 2 1 1 1\n1 1 1 1 1\n1 1 1 1 1", "output": "-1" }, { "input": "4\n3 6 0 2\n5 5 7 1\n1 7 4 6\n2 9 1 6", "output": "-1" }, { "input": "5\n1 2 1 1 1\n1 1 2 1 1\n2 1 1 0 1\n1 1 1 1 2\n1 1 1 2 1", "output": "-1" }, { "input": "11\n5 5 5 5 5 5 5 5 5 5 5\n5 5 5 5 5 5 5 5 5 5 5\n5 5 5 5 5 5 5 5 5 5 5\n5 5 5 5 5 5 5 5 5 5 5\n5 5 5 5 5 5 5 5 5 5 5\n5 5 5 5 5 5 5 5 5 5 5\n5 5 5 5 5 5 5 5 5 5 5\n5 5 5 5 5 5 5 5 5 5 5\n5 5 5 5 13 1 1 5 5 5 5\n5 5 5 5 5 9 1 5 5 5 5\n5 5 5 5 0 5 13 5 5 5 5", "output": "-1" }, { "input": "2\n5 5\n5 0", "output": "5" }, { "input": "5\n10 10 1 10 10\n1 1 0 1 1\n10 10 1 10 10\n10 10 1 10 10\n10 10 1 10 10", "output": "-1" }, { "input": "5\n1 1 1 2 1\n1 1 1 1 1\n1 1 0 1 1\n1 1 1 1 1\n1 1 1 1 1", "output": "-1" }, { "input": "3\n1000000000 1000000000 1000000000\n1000000000 1000000000 1000000000\n1000000000 0 1000000000", "output": "1000000000" }, { "input": "3\n3 3 3\n0 2 5\n1 1 1", "output": "-1" }, { "input": "4\n2 2 3 1\n1 0 3 3\n4 3 4 1\n1 2 3 1", "output": "-1" }, { "input": "3\n1 1 2\n2 1 0\n1 2 1", "output": "-1" }, { "input": "2\n1 2\n1 0", "output": "-1" }, { "input": "2\n0 535\n535 535", "output": "535" }, { "input": "6\n0 1 1 1 1 1\n1 1 1000000000 1000000000 1000000000 1000000000\n1 1000000000 1 1000000000 1000000000 1000000000\n1 1000000000 1000000000 1 1000000000 1000000000\n1 1000000000 1000000000 1000000000 1 1000000000\n1 1000000000 1000000000 1000000000 1000000000 1", "output": "3999999997" }, { "input": "4\n2 6 0 3\n5 5 7 1\n5 1 3 9\n6 6 1 5", "output": "-1" }, { "input": "5\n2 1 2 1 2\n2 2 2 2 2\n2 2 0 2 2\n2 2 2 2 2\n2 2 2 2 2", "output": "-1" }, { "input": "3\n1 2 3\n1 0 3\n1 2 3", "output": "-1" }, { "input": "3\n0 1 2\n1 2 1\n2 1 1", "output": "-1" }, { "input": "4\n2 3 2 3\n3 2 3 0\n2 4 2 2\n3 1 3 3", "output": "-1" }, { "input": "3\n1 1 1\n1 0 1\n1 2 1", "output": "-1" }, { "input": "3\n1 1 1\n1 4 1\n1 1 0", "output": "-1" }, { "input": "5\n1 1 2 1 1\n1 1 1 1 1\n1 1 1 0 1\n1 1 1 1 1\n1 1 1 1 1", "output": "-1" }, { "input": "3\n0 1 1\n1 1 1\n1 1 2", "output": "-1" }, { "input": "3\n1 2 1\n1 0 1\n1 2 1", "output": "-1" }, { "input": "3\n6 7 2\n1 0 9\n8 3 4", "output": "5" }, { "input": "3\n1 1 1\n1 1 1\n1 0 1", "output": "1" }, { "input": "3\n3 6 0\n3 3 5\n5 2 4", "output": "-1" }, { "input": "5\n1 2 2 2 1\n1 1 1 1 0\n2 2 1 2 1\n2 1 2 1 1\n1 2 2 2 1", "output": "-1" }, { "input": "4\n1 1 1 1\n1 1 1 0\n1 1 2 1\n1 1 1 1", "output": "-1" }, { "input": "3\n13 0 19\n16 10 4\n1 22 7", "output": "-1" }, { "input": "4\n1 2 2 1\n2 1 0 2\n2 1 1 2\n1 2 2 1", "output": "-1" } ]
0
0
-1
8,108
518
Anya and Smartphone
[ "constructive algorithms", "data structures", "implementation" ]
null
null
Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly *n* applications, each application has its own icon. The icons are located on different screens, one screen contains *k* icons. The icons from the first to the *k*-th one are located on the first screen, from the (*k*<=+<=1)-th to the 2*k*-th ones are on the second screen and so on (the last screen may be partially empty). Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen *t*, Anya needs to make the following gestures: first she scrolls to the required screen number *t*, by making *t*<=-<=1 gestures (if the icon is on the screen *t*), and then make another gesture — press the icon of the required application exactly once to launch it. After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1. All applications are numbered from 1 to *n*. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change. Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order? Note that one application may be launched multiple times.
The first line of the input contains three numbers *n*,<=*m*,<=*k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=105) — the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen. The next line contains *n* integers, permutation *a*1,<=*a*2,<=...,<=*a**n* — the initial order of icons from left to right in the menu (from the first to the last one), *a**i* —  is the id of the application, whose icon goes *i*-th in the menu. Each integer from 1 to *n* occurs exactly once among *a**i*. The third line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m*(1<=≤<=*b**i*<=≤<=*n*) — the ids of the launched applications in the planned order. One application may be launched multiple times.
Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order.
[ "8 3 3\n1 2 3 4 5 6 7 8\n7 8 1\n", "5 4 2\n3 1 5 2 4\n4 4 4 4\n" ]
[ "7\n", "8\n" ]
In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8. After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch it Anya makes 3 gestures. After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures. After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture. In total, Anya makes 7 gestures.
[ { "input": "8 3 3\n1 2 3 4 5 6 7 8\n7 8 1", "output": "7" }, { "input": "5 4 2\n3 1 5 2 4\n4 4 4 4", "output": "8" }, { "input": "10 10 3\n1 2 3 4 5 6 7 8 9 10\n2 3 4 5 6 7 8 9 10 1", "output": "25" }, { "input": "10 12 3\n6 1 2 9 3 10 8 5 7 4\n3 9 9 4 8 2 3 8 10 8 3 4", "output": "29" }, { "input": "16 12 8\n6 11 12 5 1 10 16 3 13 15 9 4 8 2 14 7\n6 13 8 12 8 15 12 16 8 11 3 16", "output": "18" }, { "input": "4 3 5\n3 1 4 2\n4 2 4", "output": "3" }, { "input": "4 2 15\n2 4 3 1\n1 2", "output": "2" }, { "input": "71 96 75634\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\n27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26 27 26", "output": "96" }, { "input": "71 81 2149\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 29 28 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\n29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29 28 29", "output": "81" } ]
327
15,769,600
3
8,109
883
Automatic Door
[ "implementation" ]
null
null
There is an automatic door at the entrance of a factory. The door works in the following way: - when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, - when one or several people come to the door and it is open, all people immediately come inside, - opened door immediately closes in *d* seconds after its opening, - if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close. For example, if *d*<==<=3 and four people are coming at four different moments of time *t*1<==<=4, *t*2<==<=7, *t*3<==<=9 and *t*4<==<=13 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12. It is known that *n* employees will enter at moments *a*,<=2·*a*,<=3·*a*,<=...,<=*n*·*a* (the value *a* is positive integer). Also *m* clients will enter at moments *t*1,<=*t*2,<=...,<=*t**m*. Write program to find the number of times the automatic door will open. Assume that the door is initially closed.
The first line contains four integers *n*, *m*, *a* and *d* (1<=≤<=*n*,<=*a*<=≤<=109, 1<=≤<=*m*<=≤<=105, 1<=≤<=*d*<=≤<=1018) — the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes. The second line contains integer sequence *t*1,<=*t*2,<=...,<=*t**m* (1<=≤<=*t**i*<=≤<=1018) — moments of time when clients will come. The values *t**i* are given in non-decreasing order.
Print the number of times the door will open.
[ "1 1 3 4\n7\n", "4 3 4 2\n7 9 11\n" ]
[ "1\n", "4\n" ]
In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time.
[ { "input": "1 1 3 4\n7", "output": "1" }, { "input": "4 3 4 2\n7 9 11", "output": "4" }, { "input": "10 10 51 69\n154 170 170 183 251 337 412 426 445 452", "output": "6" }, { "input": "70 10 26 17\n361 371 579 585 629 872 944 1017 1048 1541", "output": "70" }, { "input": "100 20 49 52\n224 380 690 1585 1830 1973 2490 2592 3240 3341 3406 3429 3549 3560 3895 3944 4344 4390 4649 4800", "output": "55" }, { "input": "100 30 36 47\n44 155 275 390 464 532 1186 1205 1345 1349 1432 1469 1482 1775 1832 1856 1869 2049 2079 2095 2374 2427 2577 2655 2792 2976 3020 3317 3482 3582", "output": "51" }, { "input": "97 60 1 1\n5 6 6 7 9 10 10 11 11 11 12 13 13 13 13 14 14 15 16 18 20 23 23 24 25 26 29 31 32 35 38 41 43 43 46 47 48 48 49 52 53 54 55 56 58 59 68 70 72 74 78 81 81 82 91 92 96 96 97 98", "output": "49" }, { "input": "1000000000 1 157 468\n57575875712", "output": "333333334" }, { "input": "1000000000 1 1000000000 1000000000000000000\n1000000000000000000", "output": "1" } ]
46
0
0
8,131
0
none
[ "none" ]
null
null
PMP is getting a warrior. He is practicing a lot, but the results are not acceptable yet. This time instead of programming contests, he decided to compete in a car racing to increase the spirit of victory. He decides to choose a competition that also exhibits algorithmic features. AlgoRace is a special league of car racing where different teams compete in a country of *n* cities. Cities are numbered 1 through *n*. Every two distinct cities in the country are connected with one bidirectional road. Each competing team should introduce one driver and a set of cars. The competition is held in *r* rounds. In *i*-th round, drivers will start at city *s**i* and finish at city *t**i*. Drivers are allowed to change their cars at most *k**i* times. Changing cars can take place in any city in no time. One car can be used multiple times in one round, but total number of changes should not exceed *k**i*. Drivers can freely choose their path to destination. PMP has prepared *m* type of purpose-built cars. Beside for PMP’s driving skills, depending on properties of the car and the road, a car traverses each road in each direction in different times. PMP Warriors wants to devise best strategies of choosing car and roads in each round to maximize the chance of winning the cup. For each round they want to find the minimum time required to finish it.
The first line contains three space-separated integers *n*,<=*m*,<=*r* (2<=≤<=*n*<=≤<=60,<=1<=≤<=*m*<=≤<=60,<=1<=≤<=*r*<=≤<=105) — the number of cities, the number of different types of cars and the number of rounds in the competition, correspondingly. Next *m* sets of *n*<=×<=*n* matrices of integers between 0 to 106 (inclusive) will follow — describing the time one car requires to traverse different roads. The *k*-th integer in *j*-th line of the *i*-th set is the time that *i*-th car requires to traverse the road from *j*-th city to *k*-th city. These matrices are not necessarily symmetric, but their diagonal is always zero. Next *r* lines contain description of the rounds. The *i*-th of these lines contains space-separated integers *s**i*,<=*t**i*,<=*k**i* (1<=≤<=*s**i*,<=*t**i*<=≤<=*n*,<=*s**i*<=≠<=*t**i*,<=0<=≤<=*k**i*<=≤<=1000) — the number of starting city, finishing city and the number of possible car changes in *i*-th round, correspondingly.
For each round you should print the minimum required time to complete the round in a single line.
[ "4 2 3\n0 1 5 6\n2 0 3 6\n1 3 0 1\n6 6 7 0\n0 3 5 6\n2 0 1 6\n1 3 0 2\n6 6 7 0\n1 4 2\n1 4 1\n1 4 3\n", "4 2 3\n0 7 3 3\n8 0 10 5\n1 1 0 4\n8 9 2 0\n0 3 3 9\n7 0 4 9\n3 8 0 4\n4 8 9 0\n2 3 3\n2 1 3\n1 2 2\n" ]
[ "3\n4\n3\n", "4\n5\n3\n" ]
In the first sample, in all rounds PMP goes from city #1 to city #2, then city #3 and finally city #4. But the sequences of types of the cars he uses are (1, 2, 1) in the first round and (1, 2, 2) in the second round. In the third round, although he can change his car three times, he uses the same strategy as the first round which only needs two car changes.
[]
62
0
0
8,132
771
Bear and Rectangle Strips
[ "dp", "greedy" ]
null
null
Limak has a grid that consists of 2 rows and *n* columns. The *j*-th cell in the *i*-th row contains an integer *t**i*,<=*j* which can be positive, negative or zero. A non-empty rectangle of cells is called nice if and only if the sum of numbers in its cells is equal to 0. Limak wants to choose some nice rectangles and give them to his friends, as gifts. No two chosen rectangles should share a cell. What is the maximum possible number of nice rectangles Limak can choose?
The first line of the input contains an integer *n* (1<=≤<=*n*<=≤<=300<=000) — the number of columns in the grid. The next two lines contain numbers in the grid. The *i*-th of those two lines contains *n* integers *t**i*,<=1,<=*t**i*,<=2,<=...,<=*t**i*,<=*n* (<=-<=109<=≤<=*t**i*,<=*j*<=≤<=109).
Print one integer, denoting the maximum possible number of cell-disjoint nice rectangles.
[ "6\n70 70 70 70 70 -15\n90 -60 -30 30 -30 15\n", "4\n0 -1 0 0\n0 0 1 0\n", "3\n1000000000 999999999 -1000000000\n999999999 -1000000000 -999999998\n" ]
[ "3\n", "6\n", "1\n" ]
In the first sample, there are four nice rectangles: Limak can't choose all of them because they are not disjoint. He should take three nice rectangles: those denoted as blue frames on the drawings. In the second sample, it's optimal to choose six nice rectangles, each consisting of one cell with a number 0. In the third sample, the only nice rectangle is the whole grid — the sum of all numbers is 0. Clearly, Limak can choose at most one nice rectangle, so the answer is 1.
[]
0
0
-1
8,136
978
Bus Video System
[ "combinatorics", "math" ]
null
null
The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops. If $x$ is the number of passengers in a bus just before the current bus stop and $y$ is the number of passengers in the bus just after current bus stop, the system records the number $y-x$. So the system records show how number of passengers changed. The test run was made for single bus and $n$ bus stops. Thus, the system recorded the sequence of integers $a_1, a_2, \dots, a_n$ (exactly one number for each bus stop), where $a_i$ is the record for the bus stop $i$. The bus stops are numbered from $1$ to $n$ in chronological order. Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$ (that is, at any time in the bus there should be from $0$ to $w$ passengers inclusive).
The first line contains two integers $n$ and $w$ $(1 \le n \le 1\,000, 1 \le w \le 10^{9})$ — the number of bus stops and the capacity of the bus. The second line contains a sequence $a_1, a_2, \dots, a_n$ $(-10^{6} \le a_i \le 10^{6})$, where $a_i$ equals to the number, which has been recorded by the video system after the $i$-th bus stop.
Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.
[ "3 5\n2 1 -3\n", "2 4\n-1 1\n", "4 10\n2 4 1 2\n" ]
[ "3\n", "4\n", "2\n" ]
In the first example initially in the bus could be $0$, $1$ or $2$ passengers. In the second example initially in the bus could be $1$, $2$, $3$ or $4$ passengers. In the third example initially in the bus could be $0$ or $1$ passenger.
[ { "input": "3 5\n2 1 -3", "output": "3" }, { "input": "2 4\n-1 1", "output": "4" }, { "input": "4 10\n2 4 1 2", "output": "2" }, { "input": "2 10\n-1 2", "output": "9" }, { "input": "3 4\n-3 -4 4", "output": "0" }, { "input": "10 1\n-1 -1 3 -4 2 3 0 -3 3 2", "output": "0" }, { "input": "5 21\n-3 2 -4 -1 -5", "output": "11" }, { "input": "5 9\n-2 -1 2 -1 -2", "output": "6" }, { "input": "8 7\n-5 0 -3 1 -1 5 0 -6", "output": "0" }, { "input": "3 4\n-2 -1 0", "output": "2" }, { "input": "1 1000000000\n0", "output": "1000000001" }, { "input": "2 1000000000\n-1000000 -1000000", "output": "998000001" }, { "input": "2 1000000000\n1000000 -1000000", "output": "999000001" }, { "input": "2 1000000000\n-1000000 1000000", "output": "999000001" }, { "input": "2 1000000000\n1000000 1000000", "output": "998000001" }, { "input": "102 1\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 1 1", "output": "0" }, { "input": "1 1234564\n1", "output": "1234564" }, { "input": "3 4\n1 2 3", "output": "0" } ]
46
0
3
8,137
76
Plus and xor
[ "dp", "greedy", "math" ]
D. Plus and xor
0
256
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different. For example, if *X*<==<=10910<==<=11011012, *Y*<==<=4110<==<=1010012, then: Write a program, which takes two non-negative integers *A* and *B* as an input and finds two non-negative integers *X* and *Y*, which satisfy the following conditions: - *A*<==<=*X*<=+<=*Y* - *B* <==<= *X* xor *Y*, where xor is bitwise exclusive or. - *X* is the smallest number among all numbers for which the first two conditions are true.
The first line contains integer number *A* and the second line contains integer number *B* (0<=≤<=*A*,<=*B*<=≤<=264<=-<=1).
The only output line should contain two integer non-negative numbers *X* and *Y*. Print the only number -1 if there is no answer.
[ "142\n76\n" ]
[ "33 109\n" ]
none
[ { "input": "142\n76", "output": "33 109" }, { "input": "638\n126", "output": "256 382" }, { "input": "1639\n1176", "output": "-1" }, { "input": "12608\n0", "output": "6304 6304" }, { "input": "104066\n104066", "output": "0 104066" }, { "input": "1024996\n990106", "output": "17445 1007551" }, { "input": "1215996781\n108302929", "output": "553846926 662149855" }, { "input": "1870807699\n259801747", "output": "805502976 1065304723" }, { "input": "339671107814\n208405902980", "output": "65632602417 274038505397" }, { "input": "1367480970723947\n584615739735395", "output": "391432615494276 976048355229671" }, { "input": "9992164445234764941\n8162963574901971597", "output": "914600435166396672 9077564010068368269" } ]
92
0
3
8,141
220
Little Elephant and Shifts
[ "data structures" ]
null
null
The Little Elephant has two permutations *a* and *b* of length *n*, consisting of numbers from 1 to *n*, inclusive. Let's denote the *i*-th (1<=≤<=*i*<=≤<=*n*) element of the permutation *a* as *a**i*, the *j*-th (1<=≤<=*j*<=≤<=*n*) element of the permutation *b* — as *b**j*. The distance between permutations *a* and *b* is the minimum absolute value of the difference between the positions of the occurrences of some number in *a* and in *b*. More formally, it's such minimum |*i*<=-<=*j*|, that *a**i*<==<=*b**j*. A cyclic shift number *i* (1<=≤<=*i*<=≤<=*n*) of permutation *b* consisting from *n* elements is a permutation *b**i**b**i*<=+<=1... *b**n**b*1*b*2... *b**i*<=-<=1. Overall a permutation has *n* cyclic shifts. The Little Elephant wonders, for all cyclic shifts of permutation *b*, what is the distance between the cyclic shift and permutation *a*?
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the size of the permutations. The second line contains permutation *a* as *n* distinct numbers from 1 to *n*, inclusive. The numbers are separated with single spaces. The third line contains permutation *b* in the same format.
In *n* lines print *n* integers — the answers for cyclic shifts. Print the answers to the shifts in the order of the shifts' numeration in permutation *b*, that is, first for the 1-st cyclic shift, then for the 2-nd, and so on.
[ "2\n1 2\n2 1\n", "4\n2 1 3 4\n3 4 2 1\n" ]
[ "1\n0\n", "2\n1\n0\n1\n" ]
none
[]
92
0
0
8,162
547
Mike and Feet
[ "binary search", "data structures", "dp", "dsu" ]
null
null
Mike is the president of country What-The-Fatherland. There are *n* bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to *n* from left to right. *i*-th bear is exactly *a**i* feet high. A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group. Mike is a curious to know for each *x* such that 1<=≤<=*x*<=≤<=*n* the maximum strength among all groups of size *x*.
The first line of input contains integer *n* (1<=≤<=*n*<=≤<=2<=×<=105), the number of bears. The second line contains *n* integers separated by space, *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109), heights of bears.
Print *n* integers in one line. For each *x* from 1 to *n*, print the maximum strength among all groups of size *x*.
[ "10\n1 2 3 4 5 4 3 2 1 6\n" ]
[ "6 4 4 3 3 2 2 1 1 1 \n" ]
none
[ { "input": "10\n1 2 3 4 5 4 3 2 1 6", "output": "6 4 4 3 3 2 2 1 1 1 " }, { "input": "3\n524125987 923264237 374288891", "output": "923264237 524125987 374288891 " }, { "input": "5\n585325539 365329221 412106895 291882089 564718673", "output": "585325539 365329221 365329221 291882089 291882089 " }, { "input": "20\n452405440 586588704 509061481 552472140 16115810 148658854 66743034 628305150 677780684 519361360 208050516 401554301 954478790 346543678 387546138 832279893 641889899 80960260 717802881 588066499", "output": "954478790 641889899 519361360 452405440 346543678 346543678 208050516 208050516 208050516 208050516 80960260 80960260 80960260 66743034 66743034 16115810 16115810 16115810 16115810 16115810 " }, { "input": "1\n1376", "output": "1376 " }, { "input": "2\n10 10", "output": "10 10 " }, { "input": "2\n10 9", "output": "10 9 " }, { "input": "3\n1 2 3", "output": "3 2 1 " }, { "input": "3\n1 3 2", "output": "3 2 1 " }, { "input": "10\n802030518 598196518 640274071 983359971 71550121 96204862 799843967 446173607 796619138 402690754", "output": "983359971 640274071 598196518 598196518 96204862 71550121 71550121 71550121 71550121 71550121 " }, { "input": "19\n519879446 764655030 680293934 914539062 744988123 317088317 653721289 239862203 605157354 943428394 261437390 821695238 312192823 432992892 547139308 408916833 829654733 223751525 672158759", "output": "943428394 744988123 680293934 680293934 519879446 317088317 317088317 261437390 261437390 239862203 239862203 239862203 239862203 239862203 239862203 239862203 239862203 223751525 223751525 " } ]
0
0
-1
8,181
631
Report
[ "data structures", "sortings" ]
null
null
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are *n* commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of *m* managers. Each of them may reorder the elements in some order. Namely, the *i*-th manager either sorts first *r**i* numbers in non-descending or non-ascending order and then passes the report to the manager *i*<=+<=1, or directly to Blake (if this manager has number *i*<==<=*m*). Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence *a**i* of length *n* and the description of each manager, that is value *r**i* and his favourite order. You are asked to speed up the process and determine how the final report will look like.
The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=200<=000) — the number of commodities in the report and the number of managers, respectively. The second line contains *n* integers *a**i* (|*a**i*|<=≤<=109) — the initial report before it gets to the first manager. Then follow *m* lines with the descriptions of the operations managers are going to perform. The *i*-th of these lines contains two integers *t**i* and *r**i* (, 1<=≤<=*r**i*<=≤<=*n*), meaning that the *i*-th manager sorts the first *r**i* numbers either in the non-descending (if *t**i*<==<=1) or non-ascending (if *t**i*<==<=2) order.
Print *n* integers — the final report, which will be passed to Blake by manager number *m*.
[ "3 1\n1 2 3\n2 2\n", "4 2\n1 2 4 3\n2 3\n1 2\n" ]
[ "2 1 3 ", "2 4 1 3 " ]
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form. In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake.
[ { "input": "3 1\n1 2 3\n2 2", "output": "2 1 3 " }, { "input": "4 2\n1 2 4 3\n2 3\n1 2", "output": "2 4 1 3 " }, { "input": "4 1\n4 3 2 1\n1 4", "output": "1 2 3 4 " }, { "input": "5 1\n1 2 3 4 5\n2 5", "output": "5 4 3 2 1 " }, { "input": "6 2\n3 1 2 6 4 5\n1 6\n2 3", "output": "3 2 1 4 5 6 " }, { "input": "10 3\n6 4 0 2 -3 7 8 -9 1 5\n1 8\n1 4\n2 2", "output": "-3 -9 0 2 4 6 7 8 1 5 " }, { "input": "100 30\n65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 74 57 115 16 55 88 79 97 21 80 41 56 49 103 61 66 1 36 44 43 82 37 38 106 27 114 51 112 55 87 41 69 31 86 58 27 46 99 18 105 91 38 5 9 2 109 39 2 27 47\n2 38\n2 32\n1 46\n1 37\n1 20\n1 5\n2 42\n2 18\n1 35\n1 36\n2 44\n1 36\n1 20\n2 21\n2 14\n1 13\n2 2\n1 15\n2 50\n2 35\n1 42\n1 21\n1 50\n2 50\n1 1\n2 48\n2 24\n1 34\n1 23\n2 9", "output": "65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 74 57 115 16 55 88 79 97 21 80 41 56 49 103 61 66 1 36 44 43 82 37 38 106 27 114 51 112 55 87 41 69 31 86 58 27 46 99 18 105 91 38 5 9 2 109 39 2 27 47 " }, { "input": "1 1\n1234\n1 1", "output": "1234 " }, { "input": "1 1\n1000000000\n2 1", "output": "1000000000 " }, { "input": "1 2\n-1000000000\n1 1\n2 1", "output": "-1000000000 " }, { "input": "30 13\n15 44 5 56 84 15 24 72 97 3 61 97 36 33 98 49 1 40 76 94 7 46 85 53 79 68 78 54 80 33\n2 18\n1 9\n1 6\n2 30\n2 15\n2 4\n2 17\n2 16\n2 20\n2 16\n2 7\n2 12\n1 20", "output": "40 44 46 49 53 54 56 61 68 72 76 78 79 80 84 85 94 97 97 98 36 33 33 24 15 15 7 5 3 1 " } ]
2,000
14,643,200
0
8,201
821
Okabe and Boxes
[ "data structures", "greedy", "trees" ]
null
null
Okabe and Super Hacker Daru are stacking and removing boxes. There are *n* boxes numbered from 1 to *n*. Initially there are no boxes on the stack. Okabe, being a control freak, gives Daru 2*n* commands: *n* of which are to add a box to the top of the stack, and *n* of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to *n*. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack. That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it. Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed.
The first line of input contains the integer *n* (1<=≤<=*n*<=≤<=3·105) — the number of boxes. Each of the next 2*n* lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer *x* (1<=≤<=*x*<=≤<=*n*) follows, indicating that Daru should add the box with number *x* to the top of the stack. It is guaranteed that exactly *n* lines contain "add" operations, all the boxes added are distinct, and *n* lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed.
Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands.
[ "3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove\n", "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove\n" ]
[ "1\n", "2\n" ]
In the first sample, Daru should reorder the boxes after adding box 3 to the stack. In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack.
[ { "input": "3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove", "output": "1" }, { "input": "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove", "output": "2" }, { "input": "4\nadd 1\nadd 3\nremove\nadd 4\nadd 2\nremove\nremove\nremove", "output": "2" }, { "input": "2\nadd 1\nremove\nadd 2\nremove", "output": "0" }, { "input": "1\nadd 1\nremove", "output": "0" }, { "input": "15\nadd 12\nadd 7\nadd 10\nadd 11\nadd 5\nadd 2\nadd 1\nadd 6\nadd 8\nremove\nremove\nadd 15\nadd 4\nadd 13\nadd 9\nadd 3\nadd 14\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove", "output": "2" }, { "input": "14\nadd 7\nadd 2\nadd 13\nadd 5\nadd 12\nadd 6\nadd 4\nadd 1\nadd 14\nremove\nadd 10\nremove\nadd 9\nadd 8\nadd 11\nadd 3\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove", "output": "3" }, { "input": "11\nadd 10\nadd 9\nadd 11\nadd 1\nadd 5\nadd 6\nremove\nadd 3\nadd 8\nadd 2\nadd 4\nremove\nremove\nremove\nremove\nremove\nadd 7\nremove\nremove\nremove\nremove\nremove", "output": "2" }, { "input": "3\nadd 3\nadd 2\nadd 1\nremove\nremove\nremove", "output": "0" }, { "input": "4\nadd 1\nadd 3\nadd 4\nremove\nadd 2\nremove\nremove\nremove", "output": "1" }, { "input": "6\nadd 3\nadd 4\nadd 5\nadd 1\nadd 6\nremove\nadd 2\nremove\nremove\nremove\nremove\nremove", "output": "1" }, { "input": "16\nadd 1\nadd 2\nadd 3\nadd 4\nadd 5\nadd 6\nadd 7\nadd 8\nadd 9\nadd 10\nadd 11\nadd 12\nadd 13\nadd 14\nadd 15\nadd 16\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove", "output": "1" }, { "input": "2\nadd 2\nadd 1\nremove\nremove", "output": "0" }, { "input": "17\nadd 1\nadd 2\nadd 3\nadd 4\nadd 5\nadd 6\nadd 7\nadd 8\nadd 9\nadd 10\nadd 11\nadd 12\nadd 13\nadd 14\nadd 15\nadd 16\nadd 17\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove", "output": "1" }, { "input": "18\nadd 1\nadd 2\nadd 3\nadd 4\nadd 5\nadd 6\nadd 7\nadd 8\nadd 9\nadd 10\nadd 11\nadd 12\nadd 13\nadd 14\nadd 15\nadd 16\nadd 17\nadd 18\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove", "output": "1" }, { "input": "4\nadd 1\nadd 2\nremove\nremove\nadd 4\nadd 3\nremove\nremove", "output": "1" }, { "input": "19\nadd 1\nadd 2\nadd 3\nadd 4\nadd 5\nadd 6\nadd 7\nadd 8\nadd 9\nadd 10\nadd 11\nadd 12\nadd 13\nadd 14\nadd 15\nadd 16\nadd 17\nadd 18\nadd 19\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove\nremove", "output": "1" }, { "input": "5\nadd 4\nadd 3\nadd 1\nremove\nadd 2\nremove\nremove\nadd 5\nremove\nremove", "output": "1" }, { "input": "7\nadd 4\nadd 6\nadd 1\nadd 5\nadd 7\nremove\nadd 2\nremove\nadd 3\nremove\nremove\nremove\nremove\nremove", "output": "1" }, { "input": "8\nadd 1\nadd 2\nadd 3\nadd 7\nadd 8\nremove\nremove\nremove\nadd 6\nadd 5\nadd 4\nremove\nremove\nremove\nremove\nremove", "output": "1" }, { "input": "4\nadd 1\nadd 4\nremove\nadd 3\nadd 2\nremove\nremove\nremove", "output": "1" }, { "input": "7\nadd 1\nadd 2\nadd 3\nadd 5\nadd 7\nremove\nremove\nremove\nadd 4\nremove\nremove\nadd 6\nremove\nremove", "output": "1" }, { "input": "4\nadd 4\nadd 1\nadd 2\nremove\nremove\nadd 3\nremove\nremove", "output": "1" }, { "input": "5\nadd 1\nadd 3\nadd 4\nadd 5\nremove\nadd 2\nremove\nremove\nremove\nremove", "output": "1" }, { "input": "5\nadd 2\nadd 1\nremove\nremove\nadd 5\nadd 3\nremove\nadd 4\nremove\nremove", "output": "0" }, { "input": "9\nadd 3\nadd 2\nadd 1\nadd 4\nadd 6\nadd 9\nremove\nremove\nremove\nremove\nadd 5\nremove\nremove\nadd 8\nadd 7\nremove\nremove\nremove", "output": "1" }, { "input": "10\nadd 9\nadd 10\nadd 4\nadd 3\nadd 2\nadd 1\nremove\nremove\nremove\nremove\nadd 8\nadd 7\nadd 5\nadd 6\nremove\nremove\nremove\nremove\nremove\nremove", "output": "1" } ]
93
23,142,400
-1
8,204
10
Greedy Change
[ "constructive algorithms" ]
E. Greedy Change
2
256
Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of *n* coins of different face values, and the coins of each value are not limited in number. The task is to collect the sum *x* with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding *x*. Obviously, if among the coins' face values exists the face value 1, any sum *x* can be collected with the help of greedy algorithm. However, greedy algorithm does not always give the optimal representation of the sum, i.e. the representation with the minimum amount of coins. For example, if there are face values {1,<=3,<=4} and it is asked to collect the sum 6, greedy algorithm will represent the sum as 4<=+<=1<=+<=1, while the optimal representation is 3<=+<=3, containing one coin less. By the given set of face values find out if there exist such a sum *x* that greedy algorithm will collect in a non-optimal way. If such a sum exists, find out the smallest of these sums.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=400) — the amount of the coins' face values. The second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=109), describing the face values. It is guaranteed that *a*1<=&gt;<=*a*2<=&gt;<=...<=&gt;<=*a**n* and *a**n*<==<=1.
If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way.
[ "5\n25 10 5 2 1\n", "3\n4 3 1\n" ]
[ "-1\n", "6\n" ]
none
[ { "input": "5\n25 10 5 2 1", "output": "-1" }, { "input": "3\n4 3 1", "output": "6" }, { "input": "5\n9 8 5 2 1", "output": "13" }, { "input": "5\n18 17 10 2 1", "output": "27" }, { "input": "4\n73 70 33 1", "output": "99" }, { "input": "4\n25 10 5 1", "output": "-1" }, { "input": "3\n4 3 1", "output": "6" }, { "input": "4\n25 20 10 1", "output": "30" }, { "input": "3\n25 15 1", "output": "30" }, { "input": "50\n500000 96 94 92 90 88 86 84 82 80 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 3 1", "output": "98" }, { "input": "50\n500000 96 94 92 90 88 86 84 82 80 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 1", "output": "-1" }, { "input": "50\n500000 499999 499998 499997 499996 499995 499994 499993 499992 499991 499990 499989 499988 499987 499986 499985 499984 499983 499982 499981 499980 499979 499978 499977 499976 499975 499974 499973 499972 499971 499970 499969 499968 499967 499966 499965 499964 499963 499962 499961 499960 499959 499958 499957 499956 499955 499954 499953 499952 1", "output": "999904" }, { "input": "3\n500000 499999 1", "output": "999998" }, { "input": "50\n500000 499999 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1", "output": "999998" }, { "input": "11\n447804 447682 436259 404021 392659 376034 367731 268597 145236 138718 1", "output": "277436" }, { "input": "37\n497929 464223 451341 425516 401751 360871 345120 339165 332320 327088 325949 321681 321255 312179 306305 300100 268659 268282 236636 232536 230145 202281 183443 181845 174423 159166 158458 155492 138575 113413 98040 91707 63679 51416 21296 11433 1", "output": "22866" }, { "input": "20\n489868 466294 428151 412378 394446 317619 316891 307256 199979 190697 181240 161325 143287 115819 111476 89766 71400 63806 32885 1", "output": "65770" }, { "input": "7\n447790 366103 338088 127192 119283 73058 1", "output": "146116" }, { "input": "26\n488655 449635 420758 337786 333696 329417 326150 285413 281835 273319 226900 208862 195375 175739 163162 160822 146976 104568 97418 96208 88790 78402 48286 26152 24564 1", "output": "49128" }, { "input": "5\n454748 375083 231979 228729 1", "output": "457458" }, { "input": "47\n496705 492806 462703 446368 424326 398277 392315 383243 372226 371522 361579 360696 356273 339981 330738 287896 287634 281675 277054 253588 215824 204345 201450 194746 163926 159313 157418 155438 145068 142673 132488 129873 126535 126163 122414 119202 96854 91808 88824 78898 77961 66091 51953 50293 41578 23871 1", "output": "47742" }, { "input": "9\n487264 453898 452366 383095 172725 168148 164570 141228 1", "output": "282456" }, { "input": "30\n461488 412667 406467 389755 375075 351026 332191 320180 312165 280759 266670 259978 258741 251297 248771 235766 218200 209793 142034 131703 115953 115369 92627 78342 71508 70411 61656 51268 39439 1", "output": "78878" }, { "input": "14\n472313 469103 339876 336194 308551 248071 166133 156622 154291 133164 110132 71138 33236 1", "output": "99708" }, { "input": "43\n494419 475439 473426 456392 445433 431242 426289 425690 418018 402924 379683 376621 334000 322846 320891 317240 311817 308876 278091 271657 269026 262973 224579 192149 177832 165986 128118 119033 112104 105502 76211 74773 71557 67947 67559 67425 62142 47834 47585 19596 11198 7035 1", "output": "14070" }, { "input": "25\n486057 441139 430698 427152 408599 383365 343126 339252 243930 223716 219312 216608 170945 163699 154598 141066 128583 79423 78606 58072 30640 28228 24571 5383 1", "output": "26915" }, { "input": "25\n486881 460940 449767 431421 407350 404925 399937 398840 387683 386968 290650 286122 275574 264283 257659 254750 132977 88279 82487 48945 46514 45560 30078 19083 1", "output": "38166" }, { "input": "3\n456782 213875 1", "output": "641625" }, { "input": "32\n492066 469227 464311 435058 417006 414732 397127 394962 386377 364630 347968 343897 341581 339433 338590 302427 298316 293383 273532 229938 213982 173494 171191 170922 146178 141986 139758 120345 118826 91184 46938 1", "output": "93876" }, { "input": "43\n494369 493360 454400 448348 441640 436359 402863 401152 386813 370360 365576 345832 319343 316740 312530 292656 268899 264495 243804 239368 236670 229069 216624 211903 209871 199189 185267 180886 180668 159763 157998 153674 153270 142608 132757 132541 119705 68207 59506 58596 56040 14699 1", "output": "58796" }, { "input": "43\n499757 498394 494467 494430 490217 487135 467623 461915 425822 400145 392402 368528 361824 357415 355141 352566 347715 326964 321584 317670 306465 280958 218579 216402 213660 180022 118457 115776 88678 82331 69984 69423 60451 56563 56365 48016 31055 24772 15544 2919 2200 1227 1", "output": "2454" }, { "input": "27\n477764 440484 431041 427346 368028 323248 314692 310003 299283 277684 269855 267120 229578 224810 220515 210521 161374 158029 150799 141291 115593 59379 37803 34726 27618 24403 1", "output": "48806" }, { "input": "39\n497634 495009 494063 483944 451886 448180 446192 441429 434545 429614 417363 402833 384941 384693 383154 331915 326597 321084 293206 274672 239694 239524 236198 233609 229670 226033 222079 157049 146525 141417 131035 118766 70980 58945 51894 50469 1773 558 1", "output": "2232" }, { "input": "15\n471739 409412 379958 365326 363517 219800 219742 152834 143060 109805 86434 39410 8208 4578 1", "output": "9156" }, { "input": "28\n499767 465863 409631 394241 389304 383062 342044 267362 233500 208747 205255 202242 199753 187685 185714 183202 163533 148220 142514 140009 139233 137046 75954 67079 66246 46908 16602 1", "output": "49806" }, { "input": "44\n497740 484010 477990 474388 466289 465183 446018 441372 423091 415352 385791 365228 356372 335550 327462 311065 304033 294885 291767 264525 260472 251770 250269 234813 214163 186129 166948 131304 120039 114941 106418 95802 92888 81526 81226 81172 75533 69794 69540 51954 49533 39272 12299 1", "output": "49196" }, { "input": "21\n472112 431946 411829 406527 399130 395891 385543 377038 361918 360308 356334 312243 305948 206826 199258 182494 179322 103717 31666 5333 1", "output": "31998" }, { "input": "9\n440526 404455 396537 310357 288186 187476 66947 17125 1", "output": "68500" }, { "input": "28\n492480 477288 470289 392974 378641 376009 365748 364172 341864 307796 301010 257710 257594 216542 194868 164331 142397 139139 109890 105906 105464 93772 87446 85023 66294 51969 26330 1", "output": "52660" }, { "input": "8\n406324 317344 298165 217984 201340 124738 102678 1", "output": "205356" }, { "input": "19\n471558 461066 456587 453273 388550 344142 314691 298434 237269 173595 167045 143089 78600 75441 62529 44939 26814 1094 1", "output": "27350" }, { "input": "3\n389909 142619 1", "output": "427857" }, { "input": "31\n495696 494916 482481 477452 476590 455869 439117 434349 430442 422009 419764 414718 406279 400915 400223 392067 374574 360035 358987 342956 307082 298876 267886 249356 190282 186130 86642 76932 50898 41267 1", "output": "82534" }, { "input": "43\n499775 490519 483154 474647 472568 471619 440605 437066 434554 433454 412132 403425 394878 377320 363904 363097 330413 325438 316926 316009 313018 312685 293695 286675 277379 269071 260734 260348 240829 238798 191166 154910 120927 119970 116321 104280 104077 96025 83649 67903 52781 14197 1", "output": "56788" }, { "input": "49\n487033 478497 477190 468339 464679 442615 442353 417495 395024 388721 371348 369146 368473 362006 355135 337332 335814 330942 327739 324659 316101 284491 277738 276615 259056 254219 253581 245423 238528 236553 230196 229992 216788 200669 194784 190311 164328 157601 152545 105292 94967 76049 55151 43335 39024 38606 3720 447 1", "output": "4023" }, { "input": "21\n495512 445997 403739 389462 371069 349426 316341 261014 246618 222432 199502 185241 172680 155152 90507 87176 64608 58781 55482 51081 1", "output": "102162" }, { "input": "21\n477846 443845 425918 402914 362857 346087 339332 322165 312882 299423 275613 221233 173300 159327 145354 141628 133996 93551 85703 809 1", "output": "93793" }, { "input": "3\n429655 401440 1", "output": "802880" }, { "input": "28\n490849 431182 419223 344530 312448 307141 301286 295369 281234 272874 270974 266173 257650 252736 222659 201481 193625 187072 145349 130491 111128 95714 92096 58715 37147 6341 5498 1", "output": "10996" }, { "input": "22\n430292 392392 391275 385209 370127 359090 311623 300514 265716 213205 200436 196664 191059 150927 146478 111868 101347 88871 73268 56725 30639 1", "output": "61278" }, { "input": "9\n359113 291909 263064 208071 185843 149260 94352 58856 1", "output": "117712" }, { "input": "28\n434419 433070 431479 424448 423449 392416 368998 367310 329030 316399 311541 302510 283863 262469 257928 248272 242310 217371 183364 172064 164154 131734 131169 117466 23544 19990 11006 1", "output": "22012" }, { "input": "1\n1", "output": "-1" }, { "input": "2\n227967 1", "output": "-1" }, { "input": "2\n353767 1", "output": "-1" }, { "input": "13\n496784 464754 425906 370916 351740 336779 292952 238796 178464 166413 75629 11855 1", "output": "82985" }, { "input": "22\n484731 436693 432081 387148 385052 369760 340058 311053 274965 263426 257736 253057 204507 198863 173100 153737 136236 133973 117279 49285 10635 1", "output": "53175" }, { "input": "20\n483959 458820 443030 396109 340406 334711 283762 278455 253801 253009 210156 208557 206641 169337 150807 121158 41861 41781 30976 1", "output": "61952" }, { "input": "38\n499229 495127 492174 485565 485544 447205 436284 425604 391744 391263 389916 386798 385484 363315 348314 330911 324192 314185 307277 297202 296116 263928 260467 253314 243583 211620 189479 182591 156707 152281 137039 120083 114556 109738 86227 33547 4957 1", "output": "34699" }, { "input": "25\n494273 487040 483980 449842 405763 383373 378433 347085 338845 284162 276741 270769 243629 213677 132684 129380 124239 100462 92951 87003 75776 56281 33220 13169 1", "output": "39507" }, { "input": "24\n498804 485678 468139 437676 385667 362095 356653 355933 320469 292428 277311 272265 249544 210894 207237 199958 197976 109903 75290 52108 38180 37537 20930 1", "output": "41860" }, { "input": "2\n467971 1", "output": "-1" }, { "input": "8\n456034 327797 326500 321462 312039 303728 110658 1", "output": "331974" }, { "input": "21\n469177 434800 431701 392733 387609 373571 336673 317296 308699 275508 274622 250969 230783 207596 204963 165701 132461 119669 58221 44668 1", "output": "89336" }, { "input": "50\n500000 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1", "output": "-1" }, { "input": "19\n262144 131072 65536 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1", "output": "-1" }, { "input": "50\n500000 96 94 92 90 88 86 84 80 79 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 1", "output": "83" }, { "input": "3\n3 2 1", "output": "-1" }, { "input": "2\n500000 1", "output": "-1" }, { "input": "3\n250 100 1", "output": "300" }, { "input": "4\n5 4 3 1", "output": "7" }, { "input": "3\n110 50 1", "output": "150" }, { "input": "3\n500000 499999 1", "output": "999998" }, { "input": "8\n50000 25020 25010 40 30 20 10 1", "output": "-1" }, { "input": "2\n2 1", "output": "-1" }, { "input": "5\n10 7 5 2 1", "output": "14" }, { "input": "12\n234 144 89 55 34 21 13 8 5 3 2 1", "output": "246" }, { "input": "13\n313 217 201 127 63 31 15 12 7 5 3 2 1", "output": "19" }, { "input": "13\n313 217 201 127 63 31 15 12 7 5 3 2 1", "output": "19" }, { "input": "30\n205 204 203 202 201 105 104 103 102 101 56 55 54 53 52 51 50 13 12 11 10 9 8 7 6 5 4 3 2 1", "output": "100" }, { "input": "42\n899 504 405 404 403 402 401 305 304 303 302 301 205 204 203 202 201 105 104 103 102 101 56 55 54 53 52 51 50 13 12 11 10 9 8 7 6 5 4 3 2 1", "output": "100" }, { "input": "44\n800000 80000 899 504 405 404 403 402 401 305 304 303 302 301 205 204 203 202 201 105 104 103 102 101 56 55 54 53 52 51 50 13 12 11 10 9 8 7 6 5 4 3 2 1", "output": "100" }, { "input": "44\n800000 80000 899 504 405 404 403 402 401 305 304 303 302 301 205 204 203 202 201 105 104 103 102 101 56 55 54 53 52 51 50 13 12 11 10 9 8 7 6 5 4 3 2 1", "output": "100" }, { "input": "58\n3000000 2511334 2511333 2102003 1901011 1700000 1200000 800000 750000 508111 202000 101000 80000 35000 20000 9000 5000 2000 899 504 405 404 403 402 401 305 304 303 302 301 205 204 203 202 201 105 104 103 102 101 56 55 54 53 52 51 50 13 12 10 9 8 7 5 4 3 2 1", "output": "19" }, { "input": "91\n4000000 3000000 2900000 2511334 2511333 2102003 1901011 1700000 1200000 999999 800000 750000 600000 508111 202000 101000 35000 20000 9000 5000 2000 899 630 629 628 627 626 625 624 623 622 621 620 619 618 617 616 615 614 613 612 611 610 609 608 607 606 605 604 603 602 601 504 405 404 403 402 401 305 304 303 302 301 205 204 203 202 201 105 104 103 102 101 56 55 54 53 52 51 50 13 12 10 9 8 7 5 4 3 2 1", "output": "19" }, { "input": "7\n21 15 10 5 3 2 1", "output": "25" } ]
62
0
0
8,208
837
Two Seals
[ "brute force", "implementation" ]
null
null
One very important person has a piece of paper in the form of a rectangle *a*<=×<=*b*. Also, he has *n* seals. Each seal leaves an impression on the paper in the form of a rectangle of the size *x**i*<=×<=*y**i*. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees). A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals?
The first line contains three integer numbers *n*, *a* and *b* (1<=≤<=*n*,<=*a*,<=*b*<=≤<=100). Each of the next *n* lines contain two numbers *x**i*, *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=100).
Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0.
[ "2 2 2\n1 2\n2 1\n", "4 10 9\n2 3\n1 1\n5 10\n9 11\n", "3 10 10\n6 6\n7 7\n20 5\n" ]
[ "4\n", "56\n", "0\n" ]
In the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper. In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area. In the third example there is no such pair of seals that they both can fit on a piece of paper.
[ { "input": "2 2 2\n1 2\n2 1", "output": "4" }, { "input": "4 10 9\n2 3\n1 1\n5 10\n9 11", "output": "56" }, { "input": "3 10 10\n6 6\n7 7\n20 5", "output": "0" }, { "input": "2 1 1\n1 1\n1 1", "output": "0" }, { "input": "2 1 2\n1 1\n1 1", "output": "2" }, { "input": "2 100 100\n100 100\n1 1", "output": "0" }, { "input": "2 100 100\n50 100\n100 50", "output": "10000" }, { "input": "2 100 100\n100 100\n87 72", "output": "0" }, { "input": "5 100 100\n100 100\n100 100\n100 100\n100 100\n100 100", "output": "0" }, { "input": "15 50 50\n9 36\n28 14\n77 74\n35 2\n20 32\n83 85\n47 3\n41 50\n21 7\n38 46\n17 6\n79 90\n91 83\n9 33\n24 11", "output": "2374" }, { "input": "15 100 100\n100 100\n100 100\n100 100\n42 58\n80 22\n100 100\n100 100\n100 100\n100 100\n100 100\n48 42\n100 100\n100 100\n100 100\n100 100", "output": "4452" }, { "input": "30 100 100\n60 34\n29 82\n89 77\n39 1\n100 100\n82 12\n57 87\n93 43\n78 50\n38 55\n37 9\n67 5\n100 100\n100 100\n82 47\n3 71\n100 100\n19 26\n25 94\n89 5\n100 100\n32 1\n100 100\n34 3\n40 99\n100 100\n36 12\n100 100\n100 100\n100 100", "output": "8958" }, { "input": "3 100 1\n1 50\n1 60\n1 30", "output": "90" }, { "input": "3 1 60\n1 40\n2 2\n20 1", "output": "60" }, { "input": "4 1 100\n1 25\n25 1\n1 25\n2 100", "output": "50" }, { "input": "1 100 50\n4 20", "output": "0" }, { "input": "2 2 4\n3 1\n2 2", "output": "0" }, { "input": "2 2 4\n2 3\n2 1", "output": "8" }, { "input": "2 4 2\n1 2\n2 3", "output": "8" }, { "input": "2 1 4\n1 2\n1 2", "output": "4" }, { "input": "2 4 5\n2 4\n4 3", "output": "20" }, { "input": "2 1 4\n1 1\n3 3", "output": "0" }, { "input": "6 9 5\n4 5\n6 2\n1 4\n5 6\n3 7\n6 5", "output": "34" }, { "input": "6 8 5\n4 1\n3 3\n5 3\n6 7\n2 2\n5 4", "output": "35" }, { "input": "6 7 5\n6 4\n5 7\n4 7\n5 4\n1 1\n3 6", "output": "29" }, { "input": "6 9 7\n1 2\n1 5\n4 3\n4 7\n3 5\n6 7", "output": "57" }, { "input": "6 5 9\n2 3\n7 4\n1 5\n1 7\n2 5\n7 1", "output": "38" }, { "input": "2 4 2\n2 2\n1 3", "output": "0" }, { "input": "2 3 2\n3 2\n1 1", "output": "0" }, { "input": "6 7 5\n6 6\n4 7\n6 1\n4 1\n4 6\n1 5", "output": "34" }, { "input": "2 2 3\n1 2\n2 3", "output": "0" }, { "input": "2 2 2\n2 1\n1 1", "output": "3" }, { "input": "5 9 7\n6 7\n4 5\n2 7\n4 2\n5 8", "output": "56" }, { "input": "2 11 51\n1 10\n11 50", "output": "560" }, { "input": "5 9 7\n3 8\n7 6\n4 1\n5 8\n7 8", "output": "60" }, { "input": "2 4 6\n4 4\n4 2", "output": "24" }, { "input": "5 9 7\n1 6\n7 9\n1 5\n1 5\n7 3", "output": "27" }, { "input": "5 9 7\n5 2\n6 9\n1 4\n7 7\n6 4", "output": "59" }, { "input": "5 9 7\n6 7\n4 1\n1 2\n4 7\n5 6", "output": "58" }, { "input": "5 9 7\n2 8\n3 8\n2 8\n4 4\n2 2", "output": "40" }, { "input": "2 2 3\n1 4\n2 1", "output": "0" }, { "input": "5 9 7\n4 7\n3 9\n5 4\n3 4\n3 8", "output": "55" }, { "input": "5 9 7\n7 4\n6 9\n4 3\n7 5\n2 3", "output": "63" }, { "input": "2 2 3\n1 2\n2 2", "output": "6" }, { "input": "2 4 3\n2 1\n1 2", "output": "4" }, { "input": "2 4 6\n4 2\n4 4", "output": "24" }, { "input": "2 1 4\n3 2\n3 3", "output": "0" } ]
233
9,216,000
3
8,210
387
George and Number
[ "greedy", "implementation" ]
null
null
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 *b*1,<=*b*2,<=...,<=*b*|*b*| (record |*b*| denotes the current length of the array). Then one change is a sequence of actions: - Choose two distinct indexes *i* and *j* (1<=≤<=*i*,<=*j*<=≤<=|*b*|; *i*<=≠<=*j*), such that *b**i*<=≥<=*b**j*. - Get number *v*<==<=*concat*(*b**i*,<=*b**j*), where *concat*(*x*,<=*y*) is a number obtained by adding number *y* to the end of the decimal record of number *x*. For example, *concat*(500,<=10)<==<=50010, *concat*(2,<=2)<==<=22. - Add number *v* to the end of the array. The length of the array will increase by one. - Remove from the array numbers with indexes *i* and *j*. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array. George played for a long time with his array *b* and received from array *b* an array consisting of exactly one number *p*. Now George wants to know: what is the maximum number of elements array *b* could contain originally? Help him find this number. Note that originally the array could contain only positive integers.
The first line of the input contains a single integer *p* (1<=≤<=*p*<=&lt;<=10100000). It is guaranteed that number *p* doesn't contain any leading zeroes.
Print an integer — the maximum number of elements array *b* could contain originally.
[ "9555\n", "10000000005\n", "800101\n", "45\n", "1000000000000001223300003342220044555\n", "19992000\n", "310200\n" ]
[ "4", "2", "3", "1", "17", "1", "2" ]
Let's consider the test examples: - Originally array *b* can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}. - Originally array *b* could be equal to {1000000000, 5}. Please note that the array *b* cannot contain zeros. - Originally array *b* could be equal to {800, 10, 1}. - Originally array *b* could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation. Note that the numbers can be very large.
[ { "input": "9555", "output": "4" }, { "input": "10000000005", "output": "2" }, { "input": "800101", "output": "3" }, { "input": "45", "output": "1" }, { "input": "1000000000000001223300003342220044555", "output": "17" }, { "input": "19992000", "output": "1" }, { "input": "310200", "output": "2" }, { "input": "63100605000394089000505000600600062000170273350000", "output": "21" }, { "input": "20900000000090009000070069000026000000000000020008", "output": "10" }, { "input": "60000000000000000000000000000000000000000000000000", "output": "1" }, { "input": "7", "output": "1" }, { "input": "6944262915652659458125599978116114458736683259866511789545994938161622536342972115877623999684282136", "output": "99" }, { "input": "45000", "output": "1" }, { "input": "10", "output": "1" }, { "input": "1", "output": "1" }, { "input": "456", "output": "2" }, { "input": "542", "output": "3" }, { "input": "54000", "output": "1" }, { "input": "999", "output": "3" }, { "input": "11", "output": "2" }, { "input": "11001000", "output": "2" }, { "input": "10001000", "output": "2" }, { "input": "112000", "output": "1" }, { "input": "1111200", "output": "5" }, { "input": "10000000000000000000000000000000000000400500000000000000000000000000000000030020010300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "2" }, { "input": "110003000054", "output": "3" } ]
795
10,649,600
-1
8,226
0
none
[ "none" ]
null
null
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 2*i* and another road between *i* and 2*i*<=+<=1 for every positive integer *i*. You can clearly see that there exists a unique shortest path between any two intersections. Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will *q* consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers *v*, *u* and *w*. As the result of this action, the passing fee of all roads on the shortest path from *u* to *v* increases by *w* dollars. 2. Barney starts moving from some intersection *v* and goes to intersection *u* where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
The first line of input contains a single integer *q* (1<=≤<=*q*<=≤<=1<=000). The next *q* lines contain the information about the events in chronological order. Each event is described in form 1 *v* *u* *w* if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from *u* to *v* by *w* dollars, or in form 2 *v* *u* if it's an event when Barnie goes to cuddle from the intersection *v* to the intersection *u*. 1<=≤<=*v*,<=*u*<=≤<=1018,<=*v*<=≠<=*u*,<=1<=≤<=*w*<=≤<=109 states for every description line.
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
[ "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" ]
[ "94\n0\n32\n" ]
In the example testcase: Here are the intersections used: 1. Intersections on the path are 3, 1, 2 and 4. 1. Intersections on the path are 4, 2 and 1. 1. Intersections on the path are only 3 and 6. 1. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 1. Intersections on the path are 6, 3 and 1. 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 1. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
[ { "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", "output": "94\n0\n32" }, { "input": "1\n2 666077344481199252 881371880336470888", "output": "0" }, { "input": "10\n1 1 63669439577744021 396980128\n1 2582240553355225 63669439577744021 997926286\n1 2582240553355225 1 619026011\n1 1 4 231881718\n2 63669439577744021 3886074192977\n2 4 63669439577744021\n2 124354374175272 10328962213420903\n1 10328962213420903 3886074192977 188186816\n1 124354374175272 31088593543820 705639304\n2 2582240553355225 254677758310976084", "output": "19528689796\n80417520800\n140119493557\n179078288337" }, { "input": "10\n1 1 399719082491 159376944\n1 186 1 699740230\n2 410731850987390 1\n1 410731850987390 399719082491 699271234\n1 1 186 255736462\n1 1 186 544477714\n1 399719082491 410731850987390 366708275\n2 1 186\n2 410731850987390 1\n2 399719082491 186", "output": "6013820218\n11615319450\n55320479319\n37986050043" }, { "input": "10\n2 37526406560905229 37526426361107171\n2 37526424114740747 18763396439955441\n2 300485276957081578 301492476099962199\n1 75035386466351570 441803674395985082 642312512\n2 300197522144700185 220954108245114486\n1 150105696341181576 559187296 100113944\n1 300197522135707767 150242638470761995 170574370\n2 150105691058036871 220954108245108400\n2 37560659619635168 150070774425697078\n2 18780329809814344 300222324900057526", "output": "0\n0\n0\n13488562752\n14270974176\n13899046930\n5418394872" }, { "input": "1\n2 1 343417335313797025", "output": "0" }, { "input": "2\n1 562949953421312 562949953421311 1\n2 562949953421312 562949953421311", "output": "97" }, { "input": "2\n1 100 50 1\n2 4294967396 1", "output": "0" }, { "input": "2\n1 4294967298 4294967299 10\n2 2 3", "output": "0" }, { "input": "2\n1 500000000000 250000000000 1\n2 1783793664 891896832", "output": "0" }, { "input": "2\n1 100000000000000 200000000000000 1\n2 276447232 552894464", "output": "0" }, { "input": "2\n1 2147540141 4295080282 1\n2 1 112986", "output": "0" }, { "input": "2\n1 239841676148963 1 20\n2 2112405731 1", "output": "20" } ]
93
3,993,600
3
8,227
56
Spoilt Permutation
[ "implementation" ]
B. Spoilt Permutation
2
256
Vasya collects coins: he has exactly one coin for every year from 1 to *n*. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from *l* to *r* inclusively and put them in the reverse order. That is, he took a certain segment [*l*,<=*r*] and reversed it. At that the segment's endpoints did not coincide. For example, if *n*<==<=8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2,<=6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... *n* using exactly one segment reversal. If it is possible, find the segment itself.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=1000) which is the number of coins in Vasya's collection. The second line contains space-separated *n* integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to *n*, and every number is used exactly 1 time.
If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers *l* *r* (1<=≤<=*l*<=&lt;<=*r*<=≤<=*n*) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... *n* the given one.
[ "8\n1 6 5 4 3 2 7 8\n", "4\n2 3 4 1\n", "4\n1 2 3 4\n" ]
[ "2 6\n", "0 0\n", "0 0\n" ]
none
[ { "input": "8\n1 6 5 4 3 2 7 8", "output": "2 6" }, { "input": "4\n2 3 4 1", "output": "0 0" }, { "input": "4\n1 2 3 4", "output": "0 0" }, { "input": "8\n1 3 2 4 6 5 7 8", "output": "0 0" }, { "input": "8\n1 3 4 2 6 5 7 8", "output": "0 0" }, { "input": "1\n1", "output": "0 0" }, { "input": "2\n1 2", "output": "0 0" }, { "input": "2\n2 1", "output": "1 2" }, { "input": "149\n9 120 122 97 93 70 85 56 102 16 103 112 88 84 118 135 113 62 65 19 89 15 108 73 82 21 147 27 115 130 136 6 1 90 29 94 149 17 53 132 99 123 64 95 71 67 141 126 59 8 10 114 121 134 107 87 128 79 66 55 72 39 31 111 60 137 2 4 23 129 133 47 12 54 100 77 98 30 86 125 11 5 45 148 57 49 91 28 74 18 140 3 144 78 142 101 110 131 127 20 63 139 96 32 80 50 52 69 75 76 119 26 33 109 48 116 117 35 44 83 124 68 7 14 51 40 41 104 22 105 42 38 46 37 61 146 13 106 43 36 25 143 92 138 24 81 145 34 58", "output": "0 0" }, { "input": "35\n7 33 34 15 16 24 5 27 1 19 17 22 29 3 4 23 31 11 21 35 32 2 12 20 8 9 6 28 18 26 30 14 13 10 25", "output": "0 0" }, { "input": "114\n26 20 11 61 28 89 49 42 103 74 99 71 19 67 111 85 92 13 31 18 47 91 23 95 40 29 79 2 109 70 33 82 90 5 21 77 45 41 15 86 35 46 58 87 83 62 43 9 66 3 106 14 73 107 17 22 110 104 4 100 32 52 54 55 112 96 97 44 98 75 94 80 72 69 59 57 60 108 65 30 64 78 16 10 53 84 27 6 76 7 93 114 37 105 8 113 68 1 102 24 63 39 34 51 101 25 12 48 81 36 88 56 38 50", "output": "0 0" }, { "input": "133\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 81 82 83 84 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 128 129 130 131 132 133", "output": "85 127" }, { "input": "4\n1 2 4 3", "output": "3 4" }, { "input": "4\n1 4 3 2", "output": "2 4" } ]
186
307,200
0
8,247
146
Lucky Mask
[ "brute force", "implementation" ]
null
null
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya calls a mask of a positive integer *n* the number that is obtained after successive writing of all lucky digits of number *n* from the left to the right. For example, the mask of number 72174994 is number 7744, the mask of 7 is 7, the mask of 9999047 is 47. Obviously, mask of any number is always a lucky number. Petya has two numbers — an arbitrary integer *a* and a lucky number *b*. Help him find the minimum number *c* (*c*<=&gt;<=*a*) such that the mask of number *c* equals *b*.
The only line contains two integers *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=105). It is guaranteed that number *b* is lucky.
In the only line print a single number — the number *c* that is sought by Petya.
[ "1 7\n", "100 47\n" ]
[ "7\n", "147\n" ]
none
[ { "input": "1 7", "output": "7" }, { "input": "100 47", "output": "147" }, { "input": "458 47", "output": "467" }, { "input": "7 7", "output": "17" }, { "input": "547 47", "output": "647" }, { "input": "77 77", "output": "177" }, { "input": "44 4", "output": "45" }, { "input": "740 4", "output": "804" }, { "input": "100000 77777", "output": "177777" }, { "input": "77777 77777", "output": "177777" }, { "input": "47 74", "output": "74" }, { "input": "74 77", "output": "77" }, { "input": "77 74", "output": "174" }, { "input": "98545 7474", "output": "107474" }, { "input": "99997 4", "output": "100004" }, { "input": "100000 7", "output": "100007" }, { "input": "99997 47", "output": "100047" }, { "input": "47774 774", "output": "50774" }, { "input": "47744 7", "output": "50007" }, { "input": "45896 4", "output": "45898" }, { "input": "45679 77777", "output": "77777" }, { "input": "99979 77", "output": "100077" }, { "input": "10 77777", "output": "77777" }, { "input": "1 47774", "output": "47774" }, { "input": "47774 47774", "output": "147774" }, { "input": "47580 47774", "output": "47774" }, { "input": "55557 74", "output": "55574" }, { "input": "59765 4774", "output": "64774" }, { "input": "76492 447", "output": "80447" }, { "input": "69700 77477", "output": "77477" }, { "input": "31975 74", "output": "32074" }, { "input": "369 47", "output": "407" }, { "input": "39999 4", "output": "40000" }, { "input": "39999 4774", "output": "40774" }, { "input": "474 74", "output": "574" }, { "input": "40007 74444", "output": "74444" }, { "input": "40007 74", "output": "50074" }, { "input": "1 4", "output": "4" }, { "input": "4 4", "output": "14" }, { "input": "700 74", "output": "704" }, { "input": "476 47", "output": "478" }, { "input": "99999 77", "output": "100077" }, { "input": "46 7", "output": "57" } ]
278
3,174,400
3
8,249
255
Mr. Bender and Square
[ "binary search", "implementation", "math" ]
null
null
Mr. Bender has a digital table of size *n*<=×<=*n*, each cell can be switched on or off. He wants the field to have at least *c* switched on squares. When this condition is fulfilled, Mr Bender will be happy. We'll consider the table rows numbered from top to bottom from 1 to *n*, and the columns — numbered from left to right from 1 to *n*. Initially there is exactly one switched on cell with coordinates (*x*,<=*y*) (*x* is the row number, *y* is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on. For a cell with coordinates (*x*,<=*y*) the side-adjacent cells are cells with coordinates (*x*<=-<=1,<=*y*), (*x*<=+<=1,<=*y*), (*x*,<=*y*<=-<=1), (*x*,<=*y*<=+<=1). In how many seconds will Mr. Bender get happy?
The first line contains four space-separated integers *n*,<=*x*,<=*y*,<=*c* (1<=≤<=*n*,<=*c*<=≤<=109; 1<=≤<=*x*,<=*y*<=≤<=*n*; *c*<=≤<=*n*2).
In a single line print a single integer — the answer to the problem.
[ "6 4 3 1\n", "9 3 8 10\n" ]
[ "0\n", "2\n" ]
Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. <img class="tex-graphics" src="https://espresso.codeforces.com/51bd695513bdc59c6ded01f0d34daa5361285209.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "6 4 3 1", "output": "0" }, { "input": "9 3 8 10", "output": "2" }, { "input": "9 4 3 10", "output": "2" }, { "input": "9 8 2 10", "output": "2" }, { "input": "1 1 1 1", "output": "0" }, { "input": "10 7 2 7", "output": "2" }, { "input": "8 2 6 10", "output": "2" }, { "input": "8 1 2 10", "output": "3" }, { "input": "6 1 4 10", "output": "3" }, { "input": "1000000 951981 612086 60277", "output": "174" }, { "input": "1000000 587964 232616 62357", "output": "177" }, { "input": "1000000 948438 69861 89178", "output": "211" }, { "input": "1000000000 504951981 646612086 602763371", "output": "17360" }, { "input": "1000000000 81587964 595232616 623563697", "output": "17657" }, { "input": "1000000000 55 60 715189365", "output": "37707" }, { "input": "1000000000 85 61 857945620", "output": "41279" }, { "input": "1000000000 55 85 423654797", "output": "28970" }, { "input": "1000000000 63 65 384381709", "output": "27600" }, { "input": "1000000000 44 30 891773002", "output": "42159" }, { "input": "1000000000 6 97 272656295", "output": "23250" }, { "input": "1000000000 999999946 999999941 715189365", "output": "37707" }, { "input": "1000000000 999999916 999999940 857945620", "output": "41279" }, { "input": "1000000000 999999946 999999916 423654797", "output": "28970" }, { "input": "1000000000 999999938 999999936 384381709", "output": "27600" }, { "input": "1000000000 55 999999941 715189365", "output": "37707" }, { "input": "1000000000 85 999999940 857945620", "output": "41279" }, { "input": "1000000000 55 999999916 423654797", "output": "28970" }, { "input": "1000000000 63 999999936 384381709", "output": "27600" }, { "input": "1000000000 44 999999971 891773002", "output": "42159" }, { "input": "1000000000 6 999999904 272656295", "output": "23250" }, { "input": "1000000000 999999946 60 715189365", "output": "37707" }, { "input": "1000000000 999999916 61 857945620", "output": "41279" }, { "input": "1000000000 999999946 85 423654797", "output": "28970" }, { "input": "1000000000 999999938 65 384381709", "output": "27600" }, { "input": "1000000000 999999957 30 891773002", "output": "42159" }, { "input": "548813503 532288332 26800940 350552333", "output": "13239" }, { "input": "847251738 695702891 698306947 648440371", "output": "18006" }, { "input": "891773002 152235342 682786380 386554406", "output": "13902" }, { "input": "812168727 57791401 772019566 644719499", "output": "17954" }, { "input": "71036059 25478942 38920202 19135721", "output": "3093" }, { "input": "549 198 8 262611", "output": "635" }, { "input": "848 409 661 620581", "output": "771" }, { "input": "892 364 824 53858", "output": "183" }, { "input": "813 154 643 141422", "output": "299" }, { "input": "72 40 68 849", "output": "25" }, { "input": "958 768 649 298927", "output": "431" }, { "input": "800 305 317 414868", "output": "489" }, { "input": "721 112 687 232556", "output": "556" }, { "input": "522 228 495 74535", "output": "249" }, { "input": "737 231 246 79279", "output": "199" }, { "input": "6 4 3 36", "output": "6" }, { "input": "9 3 8 55", "output": "7" }, { "input": "9 4 3 73", "output": "8" }, { "input": "9 8 2 50", "output": "7" }, { "input": "1 1 1 1", "output": "0" }, { "input": "10 7 2 7", "output": "2" }, { "input": "8 2 6 20", "output": "3" }, { "input": "8 1 2 64", "output": "13" }, { "input": "6 1 4 15", "output": "3" }, { "input": "8 8 3 1", "output": "0" } ]
30
0
0
8,273
51
Geometrical problem
[ "implementation" ]
D. Geometrical problem
1
256
Polycarp loves geometric progressions — he collects them. However, as such progressions occur very rarely, he also loves the sequences of numbers where it is enough to delete a single element to get a geometric progression. In this task we shall define geometric progressions as finite sequences of numbers *a*1,<=*a*2,<=...,<=*a**k*, where *a**i*<==<=*c*·*b**i*<=-<=1 for some real numbers *c* and *b*. For example, the sequences [2, -4, 8], [0, 0, 0, 0], [199] are geometric progressions and [0, 1, 2, 3] is not. Recently Polycarp has found a sequence and he can't classify it. Help him to do it. Determine whether it is a geometric progression. If it is not, check if it can become a geometric progression if an element is deleted from it.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements in the given sequence. The second line contains the given sequence. The numbers are space-separated. All the elements of the given sequence are integers and their absolute value does not exceed 104.
Print 0, if the given sequence is a geometric progression. Otherwise, check if it is possible to make the sequence a geometric progression by deleting a single element. If it is possible, print 1. If it is impossible, print 2.
[ "4\n3 6 12 24\n", "4\n-8 -16 24 -32\n", "4\n0 1 2 3\n" ]
[ "0\n", "1\n", "2\n" ]
none
[ { "input": "4\n3 6 12 24", "output": "0" }, { "input": "4\n-8 -16 24 -32", "output": "1" }, { "input": "4\n0 1 2 3", "output": "2" }, { "input": "5\n1 1 1 1 2", "output": "1" }, { "input": "4\n1 -1 1 -1", "output": "0" }, { "input": "8\n1 2 4 8 16 32 -64 64", "output": "1" }, { "input": "1\n1", "output": "0" }, { "input": "1\n2", "output": "0" }, { "input": "1\n-1", "output": "0" }, { "input": "1\n0", "output": "0" }, { "input": "2\n0 0", "output": "0" }, { "input": "2\n1 0", "output": "0" }, { "input": "2\n0 1", "output": "1" }, { "input": "2\n1 1", "output": "0" }, { "input": "2\n-1 1", "output": "0" }, { "input": "2\n1 -1", "output": "0" }, { "input": "3\n1 2 3", "output": "1" }, { "input": "3\n1 2 2", "output": "1" }, { "input": "3\n0 0 1", "output": "1" }, { "input": "3\n0 1 0", "output": "1" }, { "input": "3\n1 0 0", "output": "0" }, { "input": "3\n1 0 1", "output": "1" }, { "input": "3\n1 0 -1", "output": "1" }, { "input": "3\n-1 0 -1", "output": "1" }, { "input": "4\n1 0 0 0", "output": "0" }, { "input": "4\n0 0 0 -1", "output": "1" }, { "input": "4\n1 1 0 1", "output": "1" }, { "input": "4\n1 1 -1 1", "output": "1" }, { "input": "4\n1 -1 -1 1", "output": "1" }, { "input": "4\n-1 -1 -1 1", "output": "1" }, { "input": "2\n7 91", "output": "0" }, { "input": "2\n9 -27", "output": "0" }, { "input": "2\n17 527", "output": "0" }, { "input": "2\n-17 -527", "output": "0" }, { "input": "2\n17 -527", "output": "0" }, { "input": "6\n7 21 63 189 567 1701", "output": "0" }, { "input": "9\n1 2 4 8 16 32 64 128 256", "output": "0" }, { "input": "14\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192", "output": "0" }, { "input": "14\n-1 -2 -4 -8 -16 -32 -64 -128 -256 -512 -1024 -2048 -4096 -8192", "output": "0" }, { "input": "14\n-1 2 -4 8 -16 32 -64 128 -256 512 -1024 2048 -4096 8192", "output": "0" }, { "input": "14\n1 -2 4 -8 16 -32 64 -128 256 -512 1024 -2048 4096 -8192", "output": "0" }, { "input": "5\n1 10 100 1000 10000", "output": "0" }, { "input": "5\n-1 -10 -100 -1000 -10000", "output": "0" }, { "input": "5\n-1 10 -100 1000 -10000", "output": "0" }, { "input": "5\n1 -10 100 -1000 10000", "output": "0" }, { "input": "4\n7 -77 847 -9317", "output": "0" }, { "input": "4\n7 77 847 9317", "output": "0" }, { "input": "4\n-7 -77 -847 -9317", "output": "0" }, { "input": "4\n-7 77 -847 9317", "output": "0" }, { "input": "2\n7 0", "output": "0" }, { "input": "2\n9 -27", "output": "0" }, { "input": "2\n17 0", "output": "0" }, { "input": "2\n0 -17", "output": "1" }, { "input": "2\n-17 17", "output": "0" }, { "input": "6\n7 21 63 189 567 -1701", "output": "1" }, { "input": "9\n1 2 4 8 16 32 64 128 0", "output": "1" }, { "input": "14\n1 2 4 8 16 32 64 128 256 256 512 1024 2048 4096", "output": "1" }, { "input": "14\n-1 -2 -2 -4 -8 -16 -32 -64 -128 -256 -512 -1024 -2048 -4096", "output": "1" }, { "input": "14\n-1 2 -4 8 -16 32 -64 128 -256 512 -1024 -2048 2048 -4096", "output": "1" }, { "input": "14\n1 1 -2 4 -8 16 -32 64 -128 256 -512 1024 -2048 4096", "output": "1" }, { "input": "5\n1 10 100 1000 -10000", "output": "1" }, { "input": "5\n1 -1 -10 -100 -1000", "output": "1" }, { "input": "5\n-1 10 -100 -1000 1000", "output": "1" }, { "input": "5\n1 -10 100 -1000 -10000", "output": "1" }, { "input": "4\n0 7 -77 847", "output": "1" }, { "input": "4\n7 77 -847 847", "output": "1" }, { "input": "4\n-7 -77 847 -847", "output": "1" }, { "input": "7\n1 0 0 0 0 0 1", "output": "1" }, { "input": "5\n1 0 1 0 1", "output": "2" }, { "input": "3\n1 0 -1", "output": "1" }, { "input": "9\n-1 0 0 0 0 0 0 -1 0", "output": "1" }, { "input": "4\n1 0 -1 0", "output": "1" }, { "input": "10\n0 0 0 0 1 0 0 0 0 1", "output": "2" }, { "input": "5\n5 0 5 5 5", "output": "1" }, { "input": "4\n10000 -10000 -10000 10000", "output": "1" }, { "input": "10\n-10000 -10000 -10000 -10000 10000 -10000 -10000 -10000 -10000 10000", "output": "2" }, { "input": "10\n1 2 3 1 4 2 2 4 1 7", "output": "2" }, { "input": "3\n288 48 8", "output": "0" }, { "input": "8\n729 243 81 27 9 9 3 1", "output": "1" }, { "input": "10\n512 0 256 128 64 32 16 8 4 2", "output": "1" }, { "input": "5\n0 8 4 2 1", "output": "1" }, { "input": "4\n3 2 0 1", "output": "2" } ]
154
0
0
8,294
490
Hacking Cypher
[ "brute force", "math", "number theory", "strings" ]
null
null
Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won. Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long integer which may consist of even a million digits! Polycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisible by *a* as a separate number, and the second (right) part is divisible by *b* as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values *a* and *b*. Help Polycarpus and find any suitable method to cut the public key.
The first line of the input contains the public key of the messenger — an integer without leading zeroes, its length is in range from 1 to 106 digits. The second line contains a pair of space-separated positive integers *a*, *b* (1<=≤<=*a*,<=*b*<=≤<=108).
In the first line print "YES" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines — the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by *a*, and the right part must be divisible by *b*. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them. If there is no answer, print in a single line "NO" (without the quotes).
[ "116401024\n97 1024\n", "284254589153928171911281811000\n1009 1000\n", "120\n12 1\n" ]
[ "YES\n11640\n1024\n", "YES\n2842545891539\n28171911281811000\n", "NO\n" ]
none
[ { "input": "116401024\n97 1024", "output": "YES\n11640\n1024" }, { "input": "284254589153928171911281811000\n1009 1000", "output": "YES\n2842545891539\n28171911281811000" }, { "input": "120\n12 1", "output": "NO" }, { "input": "604\n6 4", "output": "YES\n60\n4" }, { "input": "2108\n7 8", "output": "YES\n210\n8" }, { "input": "7208\n10 1", "output": "YES\n720\n8" }, { "input": "97502821\n25 91", "output": "YES\n9750\n2821" }, { "input": "803405634\n309 313", "output": "YES\n80340\n5634" }, { "input": "15203400\n38 129", "output": "NO" }, { "input": "8552104774\n973 76", "output": "NO" }, { "input": "2368009434\n320 106", "output": "YES\n236800\n9434" }, { "input": "425392502895812\n4363 2452", "output": "YES\n42539250\n2895812" }, { "input": "142222201649130\n4854 7853", "output": "YES\n14222220\n1649130" }, { "input": "137871307228140\n9375 9092", "output": "NO" }, { "input": "8784054131798916\n9 61794291", "output": "YES\n87840\n54131798916" }, { "input": "24450015102786098\n75 55729838", "output": "YES\n244500\n15102786098" }, { "input": "100890056766780885\n177 88010513", "output": "YES\n1008900\n56766780885" }, { "input": "2460708054301924950\n9428 85246350", "output": "YES\n24607080\n54301924950" }, { "input": "39915186055525904358\n90102 63169402", "output": "YES\n399151860\n55525904358" }, { "input": "199510140021146591389\n458644 28692797", "output": "YES\n1995101400\n21146591389" }, { "input": "4802711808015050898224\n8381696 51544172", "output": "YES\n48027118080\n15050898224" }, { "input": "6450225349035040017740\n8872387 56607460", "output": "YES\n64502253490\n35040017740" }, { "input": "4530228043401488\n71454701 8", "output": "YES\n453022804340\n1488" }, { "input": "18769213650033200\n56876405 100", "output": "YES\n187692136500\n33200" }, { "input": "389744672208415\n17019418 765", "output": "YES\n38974467220\n8415" }, { "input": "1256363256202133560\n26228878 7460", "output": "YES\n125636325620\n2133560" }, { "input": "10213094404080691512\n64639838 83359", "output": "YES\n102130944040\n80691512" }, { "input": "14525757302059286788\n44151238 152801", "output": "YES\n145257573020\n59286788" }, { "input": "443852406270256089240\n54194433 423288", "output": "YES\n443852406270\n256089240" }, { "input": "6450225349035040017740\n8872387 56607460", "output": "YES\n64502253490\n35040017740" }, { "input": "16375289070073689\n33903290 216", "output": "NO" }, { "input": "3415280033041307294\n15179 79809921", "output": "NO" }, { "input": "4261508098904115227\n52546339 6430", "output": "NO" }, { "input": "15016\n15 16", "output": "YES\n150\n16" }, { "input": "120007\n120 7", "output": "YES\n12000\n7" }, { "input": "23\n2 3", "output": "YES\n2\n3" } ]
1,000
10,956,800
0
8,302
919
Congruence Equation
[ "chinese remainder theorem", "math", "number theory" ]
null
null
Given an integer $x$. Your task is to find out how many positive integers $n$ ($1 \leq n \leq x$) satisfy $$n \cdot a^n \equiv b \quad (\textrm{mod}\;p),$$ where $a, b, p$ are all known constants.
The only line contains four integers $a,b,p,x$ ($2 \leq p \leq 10^6+3$, $1 \leq a,b &lt; p$, $1 \leq x \leq 10^{12}$). It is guaranteed that $p$ is a prime.
Print a single integer: the number of possible answers $n$.
[ "2 3 5 8\n", "4 6 7 13\n", "233 233 10007 1\n" ]
[ "2\n", "1\n", "1\n" ]
In the first sample, we can see that $n=2$ and $n=8$ are possible answers.
[ { "input": "2 3 5 8", "output": "2" }, { "input": "4 6 7 13", "output": "1" }, { "input": "233 233 10007 1", "output": "1" }, { "input": "338792 190248 339821 152634074578", "output": "449263" }, { "input": "629260 663548 739463 321804928248", "output": "434818" }, { "input": "656229 20757 818339 523535590429", "output": "639482" }, { "input": "1000002 1000002 1000003 1000000000000", "output": "999998" }, { "input": "345 2746 1000003 5000000", "output": "4" }, { "input": "802942 824238 836833 605503824329", "output": "723664" }, { "input": "1 1 2 880336470888", "output": "440168235444" }, { "input": "2 2 3 291982585081", "output": "97327528361" }, { "input": "699601 39672 1000003 391631540387", "output": "391905" }, { "input": "9 1 11 792412106895", "output": "72037464262" }, { "input": "85 535 541 680776274925", "output": "1258366493" }, { "input": "3153 4504 7919 903755230811", "output": "114124839" }, { "input": "10021 18448 20719 509684975746", "output": "24599907" }, { "input": "66634 64950 66889 215112576953", "output": "3215965" }, { "input": "585128 179390 836839 556227387547", "output": "664796" }, { "input": "299973 381004 1000003 140225320941", "output": "140481" }, { "input": "941641 359143 1000003 851964325687", "output": "851984" }, { "input": "500719 741769 1000003 596263138944", "output": "596056" }, { "input": "142385 83099 1000003 308002143690", "output": "307937" }, { "input": "891986 300056 999983 445202944465", "output": "445451" }, { "input": "620328 378284 999983 189501757723", "output": "189574" }, { "input": "524578 993938 999979 535629124351", "output": "535377" }, { "input": "419620 683571 999979 243073161801", "output": "243611" }, { "input": "339138 549930 999883 962863668031", "output": "962803" }, { "input": "981603 635385 999233 143056117417", "output": "143126" }, { "input": "416133 340425 998561 195227456237", "output": "195090" }, { "input": "603835 578057 996323 932597132292", "output": "936103" }, { "input": "997998 999323 1000003 999968459613", "output": "999964" }, { "input": "997642 996418 999983 999997055535", "output": "1000007" }, { "input": "812415 818711 820231 999990437063", "output": "1219017" }, { "input": "994574 993183 1000003 999974679059", "output": "999965" }, { "input": "999183 998981 999979 999970875649", "output": "999996" }, { "input": "1 1 2 1", "output": "1" }, { "input": "699601 39672 1000003 1", "output": "0" }, { "input": "4 1 5 15", "output": "2" }, { "input": "912896 91931 999983 236754", "output": "1" }, { "input": "154814 35966 269041 1234567", "output": "4" }, { "input": "1 2 5 470854713201", "output": "94170942640" }, { "input": "3 27 29 968042258975", "output": "33380767549" }, { "input": "473 392 541 108827666667", "output": "201160200" }, { "input": "8 27 29 193012366642", "output": "6655598851" }, { "input": "1302 504 1987 842777827450", "output": "424145863" }, { "input": "693528 398514 1000003 1000000000000", "output": "999995" }, { "input": "533806 514846 1000003 999999999999", "output": "999997" }, { "input": "812509 699256 1000003 999999999999", "output": "999997" }, { "input": "28361 465012 1000003 1000000000000", "output": "999996" }, { "input": "28361 465012 1000003 12693229", "output": "1" }, { "input": "28361 465012 1000003 13271836", "output": "2" }, { "input": "28361 465012 1000003 13271835", "output": "1" }, { "input": "28361 465012 1000003 13421000", "output": "4" }, { "input": "28361 465012 1000003 19609900", "output": "9" }, { "input": "28361 465012 1000003 12693228", "output": "0" }, { "input": "1 1 2 1000000000000", "output": "500000000000" }, { "input": "1 1000002 1000003 1000000000000", "output": "999997" }, { "input": "1 44444 1000003 999999999998", "output": "999997" }, { "input": "2 1000002 1000003 1000000000000", "output": "1000001" }, { "input": "2 23333 1000003 1000000000000", "output": "999999" } ]
1,356
69,632,000
0
8,319
675
Money Transfers
[ "constructive algorithms", "data structures", "greedy", "sortings" ]
null
null
There are *n* banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank *n* are neighbours if *n*<=&gt;<=1. No bank is a neighbour of itself. Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank. There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation. Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of banks. The second line contains *n* integers *a**i* (<=-<=109<=≤<=*a**i*<=≤<=109), the *i*-th of them is equal to the initial balance of the account in the *i*-th bank. It's guaranteed that the sum of all *a**i* is equal to 0.
Print the minimum number of operations required to change balance in each bank to zero.
[ "3\n5 0 -5\n", "4\n-1 0 1 0\n", "4\n1 2 3 -6\n" ]
[ "1\n", "2\n", "3\n" ]
In the first sample, Vasya may transfer 5 from the first bank to the third. In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first. In the third sample, the following sequence provides the optimal answer: 1. transfer 1 from the first bank to the second bank; 1. transfer 3 from the second bank to the third; 1. transfer 6 from the third bank to the fourth.
[ { "input": "3\n5 0 -5", "output": "1" }, { "input": "4\n-1 0 1 0", "output": "2" }, { "input": "4\n1 2 3 -6", "output": "3" }, { "input": "1\n0", "output": "0" }, { "input": "50\n108431864 128274949 -554057370 -384620666 -202862975 -803855410 -482167063 -55139054 -215901009 0 0 0 0 0 94325701 730397219 358214459 -673647271 -131397668 -377892440 0 0 0 0 0 -487994257 -360271553 639988328 489338210 -281060728 250208758 0 993242346 -213071841 -59752620 -864351041 -114363541 506279952 999648597 -173503559 -144629749 -559693009 0 -46793577 511999017 -343503822 -741715911 647437511 821346413 993112810", "output": "36" }, { "input": "6\n1 -1 1 -1 1 -1", "output": "3" } ]
218
16,179,200
3
8,325
28
Bender Problem
[ "implementation" ]
A. Bender Problem
2
256
Robot Bender decided to make Fray a birthday present. He drove *n* nails and numbered them from 1 to *n* in some order. Bender decided to make a picture using metal rods. The picture is a closed polyline, which vertices should be nails (in the given order). The segments of the polyline should be parallel to the coordinate axes. Polyline is allowed to have self-intersections. Bender can take a rod and fold it exactly once in any place to form an angle of 90 degrees. Then he can attach the place of the fold to some unoccupied nail and attach two ends of this rod to adjacent nails. A nail is considered unoccupied if there is no rod attached to it (neither by it's end nor the by the fold place). No rod could be used twice. It is not required to use all the rods. Help Bender to solve this difficult task.
The first line contains two positive integers *n* and *m* (4<=≤<=*n*<=≤<=500,<=2<=≤<=*m*<=≤<=500, *n* is even) — the amount of nails and the amount of rods. *i*-th of the following *n* lines contains a pair of integers, denoting the coordinates of the *i*-th nail. Nails should be connected in the same order as they are given in the input. The last line contains *m* integers — the lenghts of the rods. All coordinates do not exceed 104 by absolute value. Lengths of the rods are between 1 and 200<=000. No rod can be used twice. It is guaranteed that all segments of the given polyline are parallel to coordinate axes. No three consecutive nails lie on the same line.
If it is impossible to solve Bender's problem, output NO. Otherwise, output YES in the first line, and in the second line output *n* numbers — *i*-th of them should be the number of rod, which fold place is attached to the *i*-th nail, or -1, if there is no such rod. If there are multiple solutions, print any of them.
[ "4 2\n0 0\n0 2\n2 2\n2 0\n4 4\n", "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3\n", "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3\n" ]
[ "YES\n1 -1 2 -1 \n", "YES\n1 -1 2 -1 3 -1 \n", "NO\n" ]
none
[ { "input": "4 2\n0 0\n0 2\n2 2\n2 0\n4 4", "output": "YES\n1 -1 2 -1 " }, { "input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3", "output": "YES\n1 -1 2 -1 3 -1 " }, { "input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3", "output": "NO" }, { "input": "4 4\n0 0\n0 1\n1 1\n1 0\n1 1 1 1", "output": "NO" }, { "input": "6 2\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2", "output": "NO" }, { "input": "6 3\n0 0\n2 0\n2 2\n1 2\n1 1\n0 1\n4 2 2", "output": "YES\n-1 1 -1 2 -1 3 " }, { "input": "4 4\n-8423 7689\n6902 7689\n6902 2402\n-8423 2402\n20612 20612 91529 35617", "output": "YES\n1 -1 2 -1 " }, { "input": "4 4\n1679 -198\n9204 -198\n9204 -5824\n1679 -5824\n18297 92466 187436 175992", "output": "NO" }, { "input": "4 2\n0 0\n0 2\n2 2\n2 0\n200000 200000", "output": "NO" } ]
124
307,200
0
8,369
163
Lemmings
[ "binary search" ]
null
null
As you know, lemmings like jumping. For the next spectacular group jump *n* lemmings gathered near a high rock with *k* comfortable ledges on it. The first ledge is situated at the height of *h* meters, the second one is at the height of 2*h* meters, and so on (the *i*-th ledge is at the height of *i*·*h* meters). The lemmings are going to jump at sunset, and there's not much time left. Each lemming is characterized by its climbing speed of *v**i* meters per minute and its weight *m**i*. This means that the *i*-th lemming can climb to the *j*-th ledge in minutes. To make the jump beautiful, heavier lemmings should jump from higher ledges: if a lemming of weight *m**i* jumps from ledge *i*, and a lemming of weight *m**j* jumps from ledge *j* (for *i*<=&lt;<=*j*), then the inequation *m**i*<=≤<=*m**j* should be fulfilled. Since there are *n* lemmings and only *k* ledges (*k*<=≤<=*n*), the *k* lemmings that will take part in the jump need to be chosen. The chosen lemmings should be distributed on the ledges from 1 to *k*, one lemming per ledge. The lemmings are to be arranged in the order of non-decreasing weight with the increasing height of the ledge. In addition, each lemming should have enough time to get to his ledge, that is, the time of his climb should not exceed *t* minutes. The lemmings climb to their ledges all at the same time and they do not interfere with each other. Find the way to arrange the lemmings' jump so that time *t* is minimized.
The first line contains space-separated integers *n*, *k* and *h* (1<=≤<=*k*<=≤<=*n*<=≤<=105, 1<=≤<=*h*<=≤<=104) — the total number of lemmings, the number of ledges and the distance between adjacent ledges. The second line contains *n* space-separated integers *m*1,<=*m*2,<=...,<=*m**n* (1<=≤<=*m**i*<=≤<=109), where *m**i* is the weight of *i*-th lemming. The third line contains *n* space-separated integers *v*1,<=*v*2,<=...,<=*v**n* (1<=≤<=*v**i*<=≤<=109), where *v**i* is the speed of *i*-th lemming.
Print *k* different numbers from 1 to *n* — the numbers of the lemmings who go to ledges at heights *h*,<=2*h*,<=...,<=*kh*, correspondingly, if the jump is organized in an optimal way. If there are multiple ways to select the lemmings, pick any of them.
[ "5 3 2\n1 2 3 2 1\n1 2 1 2 10\n", "5 3 10\n3 4 3 2 1\n5 4 3 2 1\n" ]
[ "5 2 4\n", "4 3 1\n" ]
Let's consider the first sample case. The fifth lemming (speed 10) gets to the ledge at height 2 in <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/215e7035e1b836a262740867b9bbd824fd3c66fe.png" style="max-width: 100.0%;max-height: 100.0%;"/> minutes; the second lemming (speed 2) gets to the ledge at height 4 in 2 minutes; the fourth lemming (speed 2) gets to the ledge at height 6 in 3 minutes. All lemmings manage to occupy their positions in 3 minutes.
[]
920
53,145,600
0
8,379
409
The Great Game
[ "*special" ]
null
null
Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion.
The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team.
Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie.
[ "[]()[]8&lt;\n8&lt;[]()8&lt;\n", "8&lt;8&lt;()\n[]8&lt;[]\n" ]
[ "TEAM 2 WINS\n", "TIE\n" ]
none
[ { "input": "[]()[]8<\n8<[]()8<", "output": "TEAM 2 WINS" }, { "input": "8<8<()\n[]8<[]", "output": "TIE" }, { "input": "()\n[]", "output": "TEAM 2 WINS" }, { "input": "()\n8<", "output": "TEAM 1 WINS" }, { "input": "8<\n[]", "output": "TEAM 1 WINS" }, { "input": "[]8<()()()()8<8<8<[]\n()()[][][]8<[]()8<8<", "output": "TEAM 2 WINS" }, { "input": "()[]()()()\n[]()[][]8<", "output": "TEAM 2 WINS" }, { "input": "()\n8<", "output": "TEAM 1 WINS" }, { "input": "()[][]()()[][]()8<8<\n8<[]()()()8<[][]()()", "output": "TEAM 2 WINS" }, { "input": "()[][]8<\n8<()8<()", "output": "TIE" }, { "input": "8<()8<8<8<8<()8<\n[]()()8<()[][][]", "output": "TIE" }, { "input": "[][]8<8<8<8<\n8<[][]()8<()", "output": "TEAM 2 WINS" }, { "input": "[]\n()", "output": "TEAM 1 WINS" }, { "input": "8<8<8<[]\n[][][][]", "output": "TEAM 1 WINS" }, { "input": "[][]8<[][]8<[]()()()\n()()[][]8<[]()8<[][]", "output": "TEAM 1 WINS" }, { "input": "[]8<8<[]\n[]8<()[]", "output": "TEAM 2 WINS" }, { "input": "[]\n[]", "output": "TIE" }, { "input": "[]8<[]()()()[]\n8<[]8<()8<()8<", "output": "TEAM 2 WINS" }, { "input": "[]()()()8<[]8<[]\n[][]8<[]()[][][]", "output": "TEAM 2 WINS" }, { "input": "8<()8<[]\n()[][]()", "output": "TIE" }, { "input": "()[]()()8<[]8<[]\n()()()8<8<()8<[]", "output": "TEAM 1 WINS" }, { "input": "8<()()()8<8<\n[]8<()()[][]", "output": "TEAM 1 WINS" }, { "input": "()[]()()\n()()[]()", "output": "TIE" }, { "input": "[]8<[]8<[]()\n8<[]8<8<[]8<", "output": "TIE" }, { "input": "8<()()[]()[]\n8<8<8<8<[][]", "output": "TIE" }, { "input": "[][]()[]\n[]8<8<[]", "output": "TIE" }, { "input": "[]()\n()()", "output": "TEAM 1 WINS" }, { "input": "()()()()8<()()()8<\n()[][][]8<()[][][]", "output": "TEAM 2 WINS" }, { "input": "[]8<\n8<()", "output": "TEAM 2 WINS" }, { "input": "8<8<8<\n[]()8<", "output": "TIE" }, { "input": "[]\n[]", "output": "TIE" }, { "input": "()\n()", "output": "TIE" }, { "input": "8<\n8<", "output": "TIE" }, { "input": "()\n[]", "output": "TEAM 2 WINS" }, { "input": "8<\n[]", "output": "TEAM 1 WINS" } ]
140
0
0
8,388
376
I.O.U.
[ "implementation" ]
null
null
Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles. This task is a generalisation of a described example. Imagine that your group of friends has *n* people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=100; 0<=≤<=*m*<=≤<=104). The next *m* lines contain the debts. The *i*-th line contains three integers *a**i*,<=*b**i*,<=*c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*; 1<=≤<=*c**i*<=≤<=100), which mean that person *a**i* owes person *b**i* *c**i* rubles. Assume that the people are numbered by integers from 1 to *n*. It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (*x*,<=*y*) and pair of people (*y*,<=*x*).
Print a single integer — the minimum sum of debts in the optimal rearrangement.
[ "5 3\n1 2 10\n2 3 1\n2 4 1\n", "3 0\n", "4 3\n1 2 1\n2 3 1\n3 1 1\n" ]
[ "10\n", "0\n", "0\n" ]
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10. In the second sample, there are no debts. In the third sample, you can annul all the debts.
[ { "input": "5 3\n1 2 10\n2 3 1\n2 4 1", "output": "10" }, { "input": "3 0", "output": "0" }, { "input": "4 3\n1 2 1\n2 3 1\n3 1 1", "output": "0" }, { "input": "20 28\n1 5 6\n1 12 7\n1 13 4\n1 15 7\n1 20 3\n2 4 1\n2 15 6\n3 5 3\n3 8 10\n3 13 8\n3 20 6\n4 6 10\n4 12 8\n4 19 5\n5 17 8\n6 9 9\n6 16 2\n6 19 9\n7 14 6\n8 9 3\n8 16 10\n9 11 7\n9 17 8\n11 13 8\n11 17 17\n11 19 1\n15 20 2\n17 20 1", "output": "124" }, { "input": "20 36\n1 2 13\n1 3 1\n1 6 4\n1 12 8\n1 13 9\n1 15 3\n1 18 4\n2 10 2\n2 15 2\n2 18 6\n3 7 8\n3 16 19\n4 7 1\n4 18 4\n5 9 2\n5 15 9\n5 17 4\n5 18 5\n6 11 7\n6 13 1\n6 14 9\n7 10 4\n7 12 10\n7 15 9\n7 17 8\n8 14 4\n10 13 8\n10 19 9\n11 12 5\n12 17 6\n13 15 8\n13 19 4\n14 15 9\n14 16 8\n17 19 8\n17 20 7", "output": "147" }, { "input": "20 40\n1 13 4\n2 3 3\n2 4 5\n2 7 7\n2 17 10\n3 5 3\n3 6 9\n3 10 4\n3 12 2\n3 13 2\n3 14 3\n4 5 4\n4 8 7\n4 13 9\n5 6 14\n5 14 5\n7 11 5\n7 12 13\n7 15 7\n8 14 5\n8 16 7\n8 18 17\n9 11 8\n9 19 19\n10 12 4\n10 16 3\n10 18 10\n10 20 9\n11 13 9\n11 20 2\n12 13 8\n12 18 2\n12 20 3\n13 17 1\n13 20 4\n14 16 8\n16 19 3\n18 19 3\n18 20 7\n19 20 10", "output": "165" }, { "input": "50 10\n1 5 1\n2 34 2\n3 8 10\n5 28 4\n7 28 6\n13 49 9\n15 42 7\n16 26 7\n18 47 5\n20 41 10", "output": "60" }, { "input": "50 46\n1 6 10\n1 18 1\n1 24 10\n1 33 2\n1 40 8\n3 16 7\n4 26 8\n4 32 2\n4 34 6\n5 29 8\n6 44 3\n8 20 5\n8 42 13\n10 13 5\n10 25 7\n10 27 9\n10 29 10\n11 23 4\n12 28 7\n12 30 10\n12 40 10\n13 18 2\n13 33 2\n14 15 7\n14 43 10\n14 47 3\n16 27 10\n17 21 6\n17 30 9\n19 40 4\n22 24 8\n22 25 7\n22 38 18\n25 38 1\n27 31 7\n27 40 8\n30 36 8\n31 34 1\n32 49 6\n33 35 4\n33 50 7\n38 47 1\n42 47 2\n42 50 5\n43 44 9\n47 50 5", "output": "228" }, { "input": "100 48\n1 56 6\n2 42 3\n3 52 1\n9 50 8\n10 96 8\n11 39 2\n12 51 6\n12 68 7\n13 40 5\n14 18 10\n14 70 6\n15 37 4\n15 38 8\n15 82 6\n15 85 5\n16 48 4\n16 50 9\n16 71 9\n17 18 3\n17 100 10\n20 73 3\n22 32 9\n22 89 9\n23 53 3\n24 53 1\n27 78 10\n30 50 5\n33 94 8\n34 87 9\n35 73 3\n36 51 8\n37 88 10\n37 97 2\n40 47 8\n40 90 6\n44 53 3\n44 65 3\n47 48 8\n48 72 10\n49 98 2\n53 68 10\n53 71 9\n57 62 2\n63 76 10\n66 90 9\n71 76 8\n72 80 5\n75 77 7", "output": "253" }, { "input": "4 3\n1 4 1\n2 3 1\n4 2 2", "output": "2" } ]
62
0
3
8,403
32
Hide-and-Seek
[ "geometry", "implementation" ]
E. Hide-and-Seek
2
256
Victor and Peter are playing hide-and-seek. Peter has hidden, and Victor is to find him. In the room where they are playing, there is only one non-transparent wall and one double-sided mirror. Victor and Peter are points with coordinates (*x**v*,<=*y**v*) and (*x**p*,<=*y**p*) respectively. The wall is a segment joining points with coordinates (*x**w*,<=1,<=*y**w*,<=1) and (*x**w*,<=2,<=*y**w*,<=2), the mirror — a segment joining points (*x**m*,<=1,<=*y**m*,<=1) and (*x**m*,<=2,<=*y**m*,<=2). If an obstacle has a common point with a line of vision, it's considered, that the boys can't see each other with this line of vision. If the mirror has a common point with the line of vision, it's considered, that the boys can see each other in the mirror, i.e. reflection takes place. The reflection process is governed by laws of physics — the angle of incidence is equal to the angle of reflection. The incident ray is in the same half-plane as the reflected ray, relative to the mirror. I.e. to see each other Victor and Peter should be to the same side of the line, containing the mirror (see example 1). If the line of vision is parallel to the mirror, reflection doesn't take place, and the mirror isn't regarded as an obstacle (see example 4). Victor got interested if he can see Peter, while standing at the same spot. Help him solve this problem.
The first line contains two numbers *x**v* and *y**v* — coordinates of Victor. The second line contains two numbers *x**p* and *y**p* — coordinates of Peter. The third line contains 4 numbers *x**w*,<=1, *y**w*,<=1, *x**w*,<=2, *y**w*,<=2 — coordinates of the wall. The forth line contains 4 numbers *x**m*,<=1, *y**m*,<=1, *x**m*,<=2, *y**m*,<=2 — coordinates of the mirror. All the coordinates are integer numbers, and don't exceed 104 in absolute value. It's guaranteed, that the segments don't have common points, Victor and Peter are not on any of the segments, coordinates of Victor and Peter aren't the same, the segments don't degenerate into points.
Output YES, if Victor can see Peter without leaving the initial spot. Otherwise output NO.
[ "-1 3\n1 3\n0 2 0 4\n0 0 0 1\n", "0 0\n1 1\n0 1 1 0\n-100 -100 -101 -101\n", "0 0\n1 1\n0 1 1 0\n-1 1 1 3\n", "0 0\n10 0\n100 100 101 101\n1 0 3 0\n" ]
[ "NO\n", "NO\n", "YES\n", "YES\n" ]
none
[ { "input": "-1 3\n1 3\n0 2 0 4\n0 0 0 1", "output": "NO" }, { "input": "0 0\n1 1\n0 1 1 0\n-100 -100 -101 -101", "output": "NO" }, { "input": "0 0\n1 1\n0 1 1 0\n-1 1 1 3", "output": "YES" }, { "input": "0 0\n10 0\n100 100 101 101\n1 0 3 0", "output": "YES" }, { "input": "0 0\n1 1\n100 100 101 101\n-100 -100 -101 -101", "output": "YES" }, { "input": "-1 1\n1 1\n0 3 0 4\n0 0 0 2", "output": "NO" }, { "input": "0 0\n2 0\n1 1 1 -1\n0 2 1 2", "output": "YES" }, { "input": "0 0\n2 0\n1 1 1 -1\n-10 2 -12 2", "output": "NO" }, { "input": "0 0\n2 0\n0 2 2 2\n1 1 1 -1", "output": "NO" }, { "input": "0 0\n2 0\n0 2 2 2\n1 0 1 -1", "output": "NO" }, { "input": "0 0\n2 0\n0 2 2 2\n1 1 1 0", "output": "NO" }, { "input": "0 0\n3 0\n0 2 2 2\n1 0 2 0", "output": "YES" }, { "input": "0 0\n3 0\n1 0 2 0\n0 2 2 2", "output": "YES" }, { "input": "0 0\n3 0\n1 0 2 0\n-10 2 -12 2", "output": "NO" }, { "input": "0 0\n5 0\n1 0 2 0\n3 0 4 0", "output": "NO" }, { "input": "0 0\n10 0\n0 1 10 1\n1 0 9 0", "output": "YES" }, { "input": "0 0\n2 0\n1 1 1 -1\n0 2 2 2", "output": "YES" }, { "input": "0 0\n2 0\n1 1 1 -1\n1 2 2 2", "output": "YES" }, { "input": "-1 2\n1 2\n0 2 0 4\n0 0 0 1", "output": "NO" }, { "input": "-1 4\n1 4\n0 2 0 4\n0 0 0 1", "output": "NO" }, { "input": "-1 5\n1 5\n0 2 0 4\n0 0 0 1", "output": "YES" }, { "input": "-1 2\n1 2\n0 3 0 4\n0 0 0 2", "output": "NO" }, { "input": "5023 -2243\n5648 1799\n1758 9228 -5815 3403\n-5967 -5718 -9900 -7956", "output": "YES" }, { "input": "5498 9900\n7010 -7756\n-7598 8619 -7166 -3069\n-7486 -727 -9707 7115", "output": "YES" }, { "input": "5974 2044\n8371 6409\n-675 4290 -8517 -9542\n-9005 4263 -9763 -8777", "output": "YES" }, { "input": "2729 -5812\n9733 574\n9967 3682 -9868 3984\n9477 9254 2347 3997", "output": "YES" }, { "input": "3205 -9947\n7374 -5260\n-3110 3073 5060 -2488\n7958 -5755 -7406 -816", "output": "NO" }, { "input": "3680 2196\n8736 8904\n7532 2465 3709 -8961\n6439 -765 3053 3026", "output": "NO" }, { "input": "4156 -5659\n-9903 3069\n-5545 1857 2358 4566\n4920 4225 5236 7868", "output": "YES" }, { "input": "4632 -9795\n7738 -6486\n1377 1248 1007 -1906\n3402 9216 250 6991", "output": "YES" }, { "input": "5108 2348\n9100 7679\n-7980 640 -4064 -8379\n1883 -5793 9041 8957", "output": "NO" }, { "input": "509 -7077\n1396 9890\n1192 2964 -1072 -8932\n-3260 -6877 -1781 1603", "output": "NO" }, { "input": "9060 4628\n5720 -2370\n-1791 9325 1552 -3684\n-161 -4597 2287 -3890", "output": "YES" }, { "input": "-2389 53\n-9955 5368\n-4775 -593 7898 -2157\n6659 1403 9634 -3275", "output": "YES" }, { "input": "6161 -4520\n-5630 -3171\n-7760 9489 -9477 -630\n9758 7404 -4730 -8056", "output": "NO" }, { "input": "-5289 7185\n-5026 4568\n9256 -429 -3131 4617\n-7143 -6595 -5505 -370", "output": "YES" }, { "input": "3261 2610\n-701 -7693\n6272 9652 -506 6144\n-322 -4315 -1436 -2142", "output": "YES" }, { "input": "-8188 -5684\n3623 46\n3288 -265 5839 7672\n2776 1685 2632 -7636", "output": "NO" }, { "input": "362 9742\n7948 7786\n303 6095 -7816 9199\n5875 7686 6702 6871", "output": "YES" }, { "input": "-7366 1446\n-7727 -754\n-2680 -3822 -5191 -5553\n-7304 -6313 -9229 1377", "output": "YES" }, { "input": "1184 -3127\n-7123 6985\n-1943 6259 1154 -4026\n-4205 -4033 -5160 -4115", "output": "NO" }, { "input": "-4496 6192\n-8443 7736\n2227 9746 -7197 6992\n-4002 -6638 7315 -9557", "output": "NO" }, { "input": "-3802 -741\n-9954 6119\n-1364 -1523 -8659 2791\n6451 -4606 -7143 2288", "output": "NO" }, { "input": "-3107 -7674\n4815 780\n-4957 3486 1980 -2625\n-432 -4075 -9497 -171", "output": "NO" }, { "input": "1307 5392\n3305 -837\n-8550 -7782 5287 1042\n-2922 -287 8944 3865", "output": "NO" }, { "input": "2001 2179\n-1925 -2455\n7858 948 71 2256\n4850 9947 1159 2295", "output": "NO" }, { "input": "2696 -4753\n-3435 -7793\n4265 5958 -3218 -8692\n4458 4449 4175 1539", "output": "NO" }, { "input": "7111 8313\n-4946 -9411\n672 -5310 -7733 -1889\n378 8589 -915 7675", "output": "NO" }, { "input": "7806 5101\n9823 8971\n-6640 -300 9044 7390\n-2297 -3829 7806 2982", "output": "NO" }, { "input": "-7779 -1832\n8313 7354\n9767 8430 -8438 -3487\n3855 4077 1085 -5181", "output": "NO" }, { "input": "-7086 -8767\n6803 2015\n6175 -6560 3372 573\n256 3769 393 9678", "output": "NO" }, { "input": "6777 -4260\n1717 5583\n3262 248 5800 1524\n9267 -7380 8541 5269", "output": "YES" }, { "input": "3336 -9832\n-9350 -9113\n-939 3907 -3643 -9931\n8186 6365 6202 6802", "output": "NO" }, { "input": "-6319 2768\n-431 5862\n-5417 8036 -5331 3150\n2385 -1888 3450 7359", "output": "YES" }, { "input": "-7267 -975\n-7760 5217\n-9341 -5054 -7370 3072\n-4926 1220 7603 -1721", "output": "NO" }, { "input": "9293 -6547\n4895 -9479\n2738 -1395 8489 -8324\n3540 -4860 -8555 1675", "output": "NO" }, { "input": "5851 7882\n-6171 -4174\n-1463 5985 4956 -8663\n6370 -9777 -864 9154", "output": "NO" }, { "input": "-1311 2310\n2764 1131\n-1964 -5465 -199 2630\n-5664 9644 9110 2262", "output": "YES" }, { "input": "-4752 -3262\n-4582 6436\n-8997 -6490 -4630 1426\n-9865 -6697 -2371 -5622", "output": "YES" }, { "input": "-8193 -8833\n4353 -8260\n2214 683 -8074 -8978\n5548 -850 -1486 -8372", "output": "NO" }, { "input": "4646 5596\n-2993 766\n2443 -3831 3479 5022\n-1987 4342 9668 7192", "output": "YES" }, { "input": "1772 9009\n-8123 3429\n-1873 9437 -2887 -997\n8018 -9250 -5090 -9980", "output": "YES" }, { "input": "97 -43\n7213 -4204\n5460 -8068 4212 -2411\n9853 -8477 7807 6052", "output": "YES" }, { "input": "-823 -3132\n-1924 -8397\n3277 5496 -1772 -6699\n-5322 -4634 922 182", "output": "YES" }, { "input": "7880 -7342\n1175 7552\n-6177 6690 6086 -2763\n6149 -2325 8602 -8496", "output": "YES" }, { "input": "-3417 8449\n7996 3499\n3163 7949 5262 4575\n1340 -17 -3934 8703", "output": "YES" }, { "input": "5286 518\n-8906 -4274\n-2162 1352 -751 -3408\n-3469 2291 -9521 -462", "output": "YES" }, { "input": "-1541 -7140\n-3434 7016\n-831 6470 -4500 2354\n-5503 -553 -5998 6361", "output": "YES" }, { "input": "-3234 2278\n-1683 1276\n-8143 -2879 -903 2275\n9053 -2468 7486 6408", "output": "YES" }, { "input": "-8607 4167\n4114 -152\n3668 -1725 -7827 4541\n-5338 9216 880 9081", "output": "YES" }, { "input": "-1971 -1636\n8799 -5185\n2406 2302 1967 -7166\n-4122 5320 7348 337", "output": "NO" }, { "input": "-1541 -7140\n-3434 7016\n-831 6470 -7233 -5530\n-5503 -553 -5998 6361", "output": "NO" }, { "input": "-9867 3736\n2889 5086\n2601 -5285 -8930 4748\n-3752 511 3554 1104", "output": "NO" }, { "input": "3874 -278\n-8905 -3524\n-1544 -3249 4063 -111\n-59 1361 7173 -847", "output": "NO" }, { "input": "5567 -9695\n5625 2216\n1965 -7804 8129 -7554\n1665 -446 5340 7652", "output": "NO" }, { "input": "-8240 -4453\n8478 -878\n7033 7520 -1002 -3146\n-9913 8035 -9876 -8757", "output": "NO" }, { "input": "866 4303\n-2945 -7242\n-8638 4653 -1155 -7439\n-950 -5491 2786 3812", "output": "NO" }, { "input": "1462 5132\n-8664 2428\n175 -8258 -9863 8483\n-5685 3527 1556 5387", "output": "NO" }, { "input": "866 4303\n-2945 -7242\n-8638 4653 -1155 -7439\n-950 -5491 2786 3812", "output": "NO" }, { "input": "-8836 -5283\n-5804 9454\n-4442 76 -9550 -986\n-5178 -982 -7763 -7108", "output": "NO" } ]
186
0
3.9535
8,408
0
none
[ "none" ]
null
null
We'll call a set of positive integers *a* beautiful if the following condition fulfills: for any prime *p*, if , then . In other words, if one number from the set is divisible by prime *p*, then at least half of numbers from the set is divisible by *p*. Your task is to find any beautiful set, where the number of elements is equal to *k* and each element doesn't exceed 2*k*2.
The first line contains integer *k* (10<=≤<=*k*<=≤<=5000) that shows how many numbers the required beautiful set should have.
In the first line print *k* space-separated integers that are a beautiful set. If there are multiple such sets, you are allowed to print any of them.
[ "10\n" ]
[ "16 18 24 27 36 48 54 72 108 144 \n" ]
none
[]
46
0
0
8,441
975
Ghosts
[ "geometry", "math" ]
null
null
Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way. There are $n$ ghosts in the universe, they move in the $OXY$ plane, each one of them has its own velocity that does not change in time: $\overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j}$ where $V_{x}$ is its speed on the $x$-axis and $V_{y}$ is on the $y$-axis. A ghost $i$ has experience value $EX_i$, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time. As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind $GX = \sum_{i=1}^{n} EX_i$ will never increase. Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time $T$, and magically all the ghosts were aligned on a line of the form $y = a \cdot x + b$. You have to compute what will be the experience index of the ghost kind $GX$ in the indefinite future, this is your task for today. Note that when Tameem took the picture, $GX$ may already be greater than $0$, because many ghosts may have scared one another at any moment between $[-\infty, T]$.
The first line contains three integers $n$, $a$ and $b$ ($1 \leq n \leq 200000$, $1 \leq |a| \leq 10^9$, $0 \le |b| \le 10^9$) — the number of ghosts in the universe and the parameters of the straight line. Each of the next $n$ lines contains three integers $x_i$, $V_{xi}$, $V_{yi}$ ($-10^9 \leq x_i \leq 10^9$, $-10^9 \leq V_{x i}, V_{y i} \leq 10^9$), where $x_i$ is the current $x$-coordinate of the $i$-th ghost (and $y_i = a \cdot x_i + b$). It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all $(i,j)$ $x_i \neq x_j$ for $i \ne j$.
Output one line: experience index of the ghost kind $GX$ in the indefinite future.
[ "4 1 1\n1 -1 -1\n2 1 1\n3 1 1\n4 -1 -1\n", "3 1 0\n-1 1 0\n0 0 -1\n1 -1 -2\n", "3 1 0\n0 0 0\n1 0 0\n2 0 0\n" ]
[ "8\n", "6\n", "0\n" ]
There are four collisions $(1,2,T-0.5)$, $(1,3,T-1)$, $(2,4,T+1)$, $(3,4,T+0.5)$, where $(u,v,t)$ means a collision happened between ghosts $u$ and $v$ at moment $t$. At each collision, each ghost gained one experience point, this means that $GX = 4 \cdot 2 = 8$. In the second test, all points will collide when $t = T + 1$. The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.
[ { "input": "4 1 1\n1 -1 -1\n2 1 1\n3 1 1\n4 -1 -1", "output": "8" }, { "input": "3 1 0\n-1 1 0\n0 0 -1\n1 -1 -2", "output": "6" }, { "input": "3 1 0\n0 0 0\n1 0 0\n2 0 0", "output": "0" }, { "input": "10 7 -626288749\n795312099 49439844 266151109\n-842143911 23740808 624973405\n-513221420 -44452680 -391096559\n-350963348 -5068756 -160670209\n690883790 11897718 3356227\n-509035268 -45646185 -210137445\n-121282138 -32581578 230716703\n491731655 9500548 -13423963\n-665038289 48170248 446577586\n495114076 -38468595 -159894315", "output": "20" }, { "input": "10 65536 0\n1 0 0\n2 0 0\n3 65536 0\n4 -65536 0\n5 -65536 0\n6 65536 0\n7 -65536 0\n8 65536 0\n9 -65536 0\n10 -65536 0", "output": "0" }, { "input": "20 1 123123\n100 0 -100\n10100 0 -100\n20100 0 -100\n30100 0 -100\n40100 0 -100\n50100 0 -100\n60100 0 -100\n70100 0 -100\n80100 0 -100\n90100 0 -100\n0 100 0\n-10000 100 0\n-20000 100 0\n-30000 100 0\n-40000 100 0\n-50000 100 0\n-60000 100 0\n-70000 100 0\n-80000 100 0\n-90000 100 0", "output": "200" }, { "input": "2 4 0\n0 -536870912 0\n1 536870911 -4", "output": "0" } ]
904
43,008,000
3
8,456
769
News About Credit
[ "*special", "greedy", "two pointers" ]
null
null
Polycarp studies at the university in the group which consists of *n* students (including himself). All they are registrated in the social net "TheContacnt!". Not all students are equally sociable. About each student you know the value *a**i* — the maximum number of messages which the *i*-th student is agree to send per day. The student can't send messages to himself. In early morning Polycarp knew important news that the programming credit will be tomorrow. For this reason it is necessary to urgently inform all groupmates about this news using private messages. Your task is to make a plan of using private messages, so that: - the student *i* sends no more than *a**i* messages (for all *i* from 1 to *n*); - all students knew the news about the credit (initially only Polycarp knew it); - the student can inform the other student only if he knows it himself. Let's consider that all students are numerated by distinct numbers from 1 to *n*, and Polycarp always has the number 1. In that task you shouldn't minimize the number of messages, the moment of time, when all knew about credit or some other parameters. Find any way how to use private messages which satisfies requirements above.
The first line contains the positive integer *n* (2<=≤<=*n*<=≤<=100) — the number of students. The second line contains the sequence *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=100), where *a**i* equals to the maximum number of messages which can the *i*-th student agree to send. Consider that Polycarp always has the number 1.
Print -1 to the first line if it is impossible to inform all students about credit. Otherwise, in the first line print the integer *k* — the number of messages which will be sent. In each of the next *k* lines print two distinct integers *f* and *t*, meaning that the student number *f* sent the message with news to the student number *t*. All messages should be printed in chronological order. It means that the student, who is sending the message, must already know this news. It is assumed that students can receive repeated messages with news of the credit. If there are several answers, it is acceptable to print any of them.
[ "4\n1 2 1 0\n", "6\n2 0 1 3 2 0\n", "3\n0 2 2\n" ]
[ "3\n1 2\n2 4\n2 3\n", "6\n1 3\n3 4\n1 2\n4 5\n5 6\n4 6\n", "-1\n" ]
In the first test Polycarp (the student number 1) can send the message to the student number 2, who after that can send the message to students number 3 and 4. Thus, all students knew about the credit.
[ { "input": "4\n1 2 1 0", "output": "3\n1 2\n2 3\n2 4" }, { "input": "6\n2 0 1 3 2 0", "output": "5\n1 4\n1 5\n4 3\n4 2\n4 6" }, { "input": "3\n0 2 2", "output": "-1" }, { "input": "2\n0 0", "output": "-1" }, { "input": "2\n1 0", "output": "1\n1 2" }, { "input": "2\n0 1", "output": "-1" }, { "input": "2\n1 1", "output": "1\n1 2" }, { "input": "3\n1 1 0", "output": "2\n1 2\n2 3" }, { "input": "3\n0 1 1", "output": "-1" }, { "input": "3\n1 0 0", "output": "-1" }, { "input": "3\n2 0 0", "output": "2\n1 2\n1 3" }, { "input": "3\n1 0 1", "output": "2\n1 3\n3 2" }, { "input": "3\n1 1 1", "output": "2\n1 2\n2 3" }, { "input": "40\n3 3 2 1 0 0 0 4 5 4 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 2 3 2 0 1 0 0 2 0 3 0 1 0", "output": "-1" }, { "input": "100\n1 0 0 2 0 2 0 0 2 0 0 2 0 0 2 2 2 1 1 2 1 2 2 2 1 2 0 1 0 1 0 2 2 2 0 1 2 0 0 2 0 2 0 1 1 0 1 0 2 0 0 2 1 2 1 2 2 2 2 1 0 2 0 0 1 0 2 0 0 2 0 1 0 2 1 1 2 2 2 2 0 0 2 0 2 1 0 0 0 1 0 2 2 2 0 1 0 1 1 0", "output": "99\n1 4\n4 6\n4 9\n6 12\n6 15\n9 16\n9 17\n12 20\n12 22\n15 23\n15 24\n16 26\n16 32\n17 33\n17 34\n20 37\n20 40\n22 42\n22 49\n23 52\n23 54\n24 56\n24 57\n26 58\n26 59\n32 62\n32 67\n33 70\n33 74\n34 77\n34 78\n37 79\n37 80\n40 83\n40 85\n42 92\n42 93\n49 94\n49 18\n52 19\n52 21\n54 25\n54 28\n56 30\n56 36\n57 44\n57 45\n58 47\n58 53\n59 55\n59 60\n62 65\n62 72\n67 75\n67 76\n70 86\n70 90\n74 96\n74 98\n77 99\n77 2\n78 3\n78 5\n79 7\n79 8\n80 10\n80 11\n83 13\n83 14\n85 27\n85 29\n92 31\n92 35\n93 38\n93 3..." }, { "input": "4\n2 0 0 0", "output": "-1" }, { "input": "4\n2 0 0 1", "output": "3\n1 4\n1 2\n4 3" }, { "input": "4\n2 0 1 0", "output": "3\n1 3\n1 2\n3 4" }, { "input": "4\n2 1 0 0", "output": "3\n1 2\n1 3\n2 4" }, { "input": "100\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "-1" }, { "input": "100\n99 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "99\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n..." }, { "input": "100\n98 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "-1" }, { "input": "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 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 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", "output": "99\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 77\n..." }, { "input": "100\n1 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 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", "output": "99\n1 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 7..." }, { "input": "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 0", "output": "99\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 51\n51 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n..." }, { "input": "100\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "99\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n..." }, { "input": "100\n1 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "99\n1 2\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31\n2 32\n2 33\n2 34\n2 35\n2 36\n2 37\n2 38\n2 39\n2 40\n2 41\n2 42\n2 43\n2 44\n2 45\n2 46\n2 47\n2 48\n2 49\n2 50\n2 51\n2 52\n2 53\n2 54\n2 55\n2 56\n2 57\n2 58\n2 59\n2 60\n2 61\n2 62\n2 63\n2 64\n2 65\n2 66\n2 67\n2 68\n2 69\n2 70\n2 71\n2 72\n2 73\n2 74\n2 75\n2 76\n2 77\n2 78\n2 79\n2 80\n2 81\n2 82\n2 83\n2 84\n2 85\n2 86\n2 87\n..." }, { "input": "100\n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 98 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "99\n1 55\n55 2\n55 3\n55 4\n55 5\n55 6\n55 7\n55 8\n55 9\n55 10\n55 11\n55 12\n55 13\n55 14\n55 15\n55 16\n55 17\n55 18\n55 19\n55 20\n55 21\n55 22\n55 23\n55 24\n55 25\n55 26\n55 27\n55 28\n55 29\n55 30\n55 31\n55 32\n55 33\n55 34\n55 35\n55 36\n55 37\n55 38\n55 39\n55 40\n55 41\n55 42\n55 43\n55 44\n55 45\n55 46\n55 47\n55 48\n55 49\n55 50\n55 51\n55 52\n55 53\n55 54\n55 56\n55 57\n55 58\n55 59\n55 60\n55 61\n55 62\n55 63\n55 64\n55 65\n55 66\n55 67\n55 68\n55 69\n55 70\n55 71\n55 72\n55 73\n55 74\n55 75..." }, { "input": "2\n0 1", "output": "-1" }, { "input": "5\n0 0 1 1 2", "output": "-1" }, { "input": "7\n2 0 0 0 1 0 3", "output": "6\n1 7\n1 5\n7 2\n7 3\n7 4\n5 6" }, { "input": "10\n3 0 0 0 0 2 0 1 0 3", "output": "9\n1 10\n1 6\n1 8\n10 2\n10 3\n10 4\n6 5\n6 7\n8 9" }, { "input": "20\n0 2 0 0 2 0 0 2 2 0 0 2 0 2 1 0 1 3 1 1", "output": "-1" }, { "input": "30\n2 0 2 2 0 2 2 0 0 0 3 0 1 1 2 0 0 2 2 0 1 0 3 0 1 0 2 0 0 1", "output": "29\n1 11\n1 23\n11 3\n11 4\n11 6\n23 7\n23 15\n23 18\n3 19\n3 27\n4 13\n4 14\n6 21\n6 25\n7 30\n7 2\n15 5\n15 8\n18 9\n18 10\n19 12\n19 16\n27 17\n27 20\n13 22\n14 24\n21 26\n25 28\n30 29" }, { "input": "31\n2 0 0 4 0 0 0 0 0 0 0 0 0 3 2 0 0 0 0 0 3 0 4 3 0 2 0 0 0 3 4", "output": "30\n1 4\n1 23\n4 31\n4 14\n4 21\n4 24\n23 30\n23 15\n23 26\n23 2\n31 3\n31 5\n31 6\n31 7\n14 8\n14 9\n14 10\n21 11\n21 12\n21 13\n24 16\n24 17\n24 18\n30 19\n30 20\n30 22\n15 25\n15 27\n26 28\n26 29" }, { "input": "39\n2 0 3 0 0 2 0 0 2 1 1 0 0 3 3 0 2 0 2 3 0 0 3 0 3 2 0 0 3 0 0 0 3 0 0 0 0 0 0", "output": "38\n1 3\n1 14\n3 15\n3 20\n3 23\n14 25\n14 29\n14 33\n15 6\n15 9\n15 17\n20 19\n20 26\n20 10\n23 11\n23 2\n23 4\n25 5\n25 7\n25 8\n29 12\n29 13\n29 16\n33 18\n33 21\n33 22\n6 24\n6 27\n9 28\n9 30\n17 31\n17 32\n19 34\n19 35\n26 36\n26 37\n10 38\n11 39" }, { "input": "58\n4 2 1 3 5 3 0 0 1 0 3 0 2 1 0 0 0 4 0 0 0 0 0 1 2 3 4 0 1 1 0 0 1 0 0 0 2 0 0 0 0 2 2 0 2 0 0 4 0 2 0 0 0 0 0 1 0 0", "output": "57\n1 5\n1 18\n1 27\n1 48\n5 4\n5 6\n5 11\n5 26\n5 2\n18 13\n18 25\n18 37\n18 42\n27 43\n27 45\n27 50\n27 3\n48 9\n48 14\n48 24\n48 29\n4 30\n4 33\n4 56\n6 7\n6 8\n6 10\n11 12\n11 15\n11 16\n26 17\n26 19\n26 20\n2 21\n2 22\n13 23\n13 28\n25 31\n25 32\n37 34\n37 35\n42 36\n42 38\n43 39\n43 40\n45 41\n45 44\n50 46\n50 47\n3 49\n9 51\n14 52\n24 53\n29 54\n30 55\n33 57\n56 58" }, { "input": "65\n3 0 0 0 0 3 0 0 0 0 0 4 2 0 0 0 0 0 0 0 0 8 0 0 0 0 0 6 7 0 3 0 0 0 0 4 0 3 0 0 0 0 1 0 0 5 0 0 0 0 3 0 0 4 0 0 0 0 0 1 0 0 0 0 7", "output": "64\n1 22\n1 29\n1 65\n22 28\n22 46\n22 12\n22 36\n22 54\n22 6\n22 31\n22 38\n29 51\n29 13\n29 43\n29 60\n29 2\n29 3\n29 4\n65 5\n65 7\n65 8\n65 9\n65 10\n65 11\n65 14\n28 15\n28 16\n28 17\n28 18\n28 19\n28 20\n46 21\n46 23\n46 24\n46 25\n46 26\n12 27\n12 30\n12 32\n12 33\n36 34\n36 35\n36 37\n36 39\n54 40\n54 41\n54 42\n54 44\n6 45\n6 47\n6 48\n31 49\n31 50\n31 52\n38 53\n38 55\n38 56\n51 57\n51 58\n51 59\n13 61\n13 62\n43 63\n60 64" }, { "input": "77\n7 0 0 0 0 0 0 0 0 8 0 0 0 0 3 0 0 0 0 9 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 2 6 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 7 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 8", "output": "76\n1 20\n1 63\n1 10\n1 37\n1 77\n1 24\n1 60\n20 44\n20 15\n20 43\n20 55\n20 2\n20 3\n20 4\n20 5\n20 6\n63 7\n63 8\n63 9\n63 11\n63 12\n63 13\n63 14\n63 16\n63 17\n10 18\n10 19\n10 21\n10 22\n10 23\n10 25\n10 26\n10 27\n37 28\n37 29\n37 30\n37 31\n37 32\n37 33\n37 34\n37 35\n77 36\n77 38\n77 39\n77 40\n77 41\n77 42\n77 45\n77 46\n24 47\n24 48\n24 49\n24 50\n24 51\n24 52\n24 53\n60 54\n60 56\n60 57\n60 58\n60 59\n60 61\n60 62\n44 64\n44 65\n44 66\n44 67\n44 68\n44 69\n15 70\n15 71\n15 72\n43 73\n43 74\n55 7..." }, { "input": "80\n2 3 0 2 2 1 3 3 3 0 0 0 1 0 1 0 3 1 0 2 0 2 3 0 2 3 0 3 0 0 0 3 0 0 0 2 3 0 0 2 0 0 0 0 0 3 2 0 0 3 0 3 0 3 0 3 1 2 0 0 0 0 0 0 0 1 0 3 0 0 0 1 0 2 0 2 0 0 0 0", "output": "79\n1 2\n1 7\n2 8\n2 9\n2 17\n7 23\n7 26\n7 28\n8 32\n8 37\n8 46\n9 50\n9 52\n9 54\n17 56\n17 68\n17 4\n23 5\n23 20\n23 22\n26 25\n26 36\n26 40\n28 47\n28 58\n28 74\n32 76\n32 6\n32 13\n37 15\n37 18\n37 57\n46 66\n46 72\n46 3\n50 10\n50 11\n50 12\n52 14\n52 16\n52 19\n54 21\n54 24\n54 27\n56 29\n56 30\n56 31\n68 33\n68 34\n68 35\n4 38\n4 39\n5 41\n5 42\n20 43\n20 44\n22 45\n22 48\n25 49\n25 51\n36 53\n36 55\n40 59\n40 60\n47 61\n47 62\n58 63\n58 64\n74 65\n74 67\n76 69\n76 70\n6 71\n13 73\n15 75\n18 77\n57..." }, { "input": "90\n2 0 0 0 0 1 2 0 1 1 0 1 0 4 0 1 1 0 1 0 1 0 1 1 2 0 0 1 2 3 0 1 1 0 0 1 1 0 0 2 0 2 2 1 0 1 0 0 2 0 1 4 2 0 1 2 2 0 1 0 0 5 0 0 3 0 1 2 0 0 0 0 2 3 0 0 3 3 0 3 3 0 0 0 1 0 1 2 2 2", "output": "89\n1 62\n1 14\n62 52\n62 30\n62 65\n62 74\n62 77\n14 78\n14 80\n14 81\n14 7\n52 25\n52 29\n52 40\n52 42\n30 43\n30 49\n30 53\n65 56\n65 57\n65 68\n74 73\n74 88\n74 89\n77 90\n77 6\n77 9\n78 10\n78 12\n78 16\n80 17\n80 19\n80 21\n81 23\n81 24\n81 28\n7 32\n7 33\n25 36\n25 37\n29 44\n29 46\n40 51\n40 55\n42 59\n42 67\n43 85\n43 87\n49 2\n49 3\n53 4\n53 5\n56 8\n56 11\n57 13\n57 15\n68 18\n68 20\n73 22\n73 26\n88 27\n88 31\n89 34\n89 35\n90 38\n90 39\n6 41\n9 45\n10 47\n12 48\n16 50\n17 54\n19 58\n21 60\n23 ..." }, { "input": "99\n1 2 1 0 2 1 2 0 1 2 1 1 2 1 0 2 0 0 1 2 0 1 1 1 0 0 1 1 2 3 1 0 0 0 1 1 0 0 1 2 1 0 2 0 0 2 0 1 0 2 1 1 0 0 3 1 0 2 2 2 2 1 0 2 0 1 1 0 3 2 0 0 1 1 2 0 0 2 0 0 1 2 3 0 3 0 0 3 0 3 0 1 2 1 0 1 0 1 1", "output": "98\n1 30\n30 55\n30 69\n30 83\n55 85\n55 88\n55 90\n69 2\n69 5\n69 7\n83 10\n83 13\n83 16\n85 20\n85 29\n85 40\n88 43\n88 46\n88 50\n90 58\n90 59\n90 60\n2 61\n2 64\n5 70\n5 75\n7 78\n7 82\n10 93\n10 3\n13 6\n13 9\n16 11\n16 12\n20 14\n20 19\n29 22\n29 23\n40 24\n40 27\n43 28\n43 31\n46 35\n46 36\n50 39\n50 41\n58 48\n58 51\n59 52\n59 56\n60 62\n60 66\n61 67\n61 73\n64 74\n64 81\n70 92\n70 94\n75 96\n75 98\n78 99\n78 4\n82 8\n82 15\n93 17\n93 18\n3 21\n6 25\n9 26\n11 32\n12 33\n14 34\n19 37\n22 38\n23 42\n..." }, { "input": "100\n18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0 13 0 0 0 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0", "output": "99\n1 26\n1 99\n1 38\n1 62\n1 32\n1 71\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n26 14\n26 15\n26 16\n26 17\n26 18\n26 19\n26 20\n26 21\n26 22\n26 23\n26 24\n26 25\n26 27\n26 28\n26 29\n26 30\n99 31\n99 33\n99 34\n99 35\n99 36\n99 37\n99 39\n99 40\n99 41\n99 42\n99 43\n99 44\n99 45\n99 46\n99 47\n99 48\n38 49\n38 50\n38 51\n38 52\n38 53\n38 54\n38 55\n38 56\n38 57\n38 58\n38 59\n38 60\n38 61\n38 63\n38 64\n62 65\n62 66\n62 67\n62 68\n62 69\n62 70\n62 72\n62 73\n62 74\n62 75\n62 76\n6..." }, { "input": "100\n3 2 1 0 1 0 0 0 2 3 2 1 0 0 0 3 1 1 3 0 1 1 1 3 0 2 2 2 0 1 1 1 0 0 1 0 2 1 1 2 1 0 0 0 1 1 0 3 0 0 0 4 1 2 0 0 0 1 0 3 2 0 0 2 0 3 0 1 4 2 1 0 0 1 1 0 1 0 0 4 0 1 0 1 0 1 1 0 0 0 0 1 2 0 0 0 3 3 0 2", "output": "99\n1 52\n1 69\n1 80\n52 10\n52 16\n52 19\n52 24\n69 48\n69 60\n69 66\n69 97\n80 98\n80 2\n80 9\n80 11\n10 26\n10 27\n10 28\n16 37\n16 40\n16 54\n19 61\n19 64\n19 70\n24 93\n24 100\n24 3\n48 5\n48 12\n48 17\n60 18\n60 21\n60 22\n66 23\n66 30\n66 31\n97 32\n97 35\n97 38\n98 39\n98 41\n98 45\n2 46\n2 53\n9 58\n9 68\n11 71\n11 74\n26 75\n26 77\n27 82\n27 84\n28 86\n28 87\n37 92\n37 4\n40 6\n40 7\n54 8\n54 13\n61 14\n61 15\n64 20\n64 25\n70 29\n70 33\n93 34\n93 36\n100 42\n100 43\n3 44\n5 47\n12 49\n17 50\n18 ..." }, { "input": "100\n66 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 33 0 0 0", "output": "99\n1 97\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n97 67\n97 68\n97 69\n97 70\n97 71\n97 72\n97 73\n97 74\n97 75\n97 76\n97 77\n97 78\n97 79\n97 80\n97 81\n97 82\n97 83\n..." }, { "input": "20\n0 0 3 0 0 0 3 4 2 0 2 0 0 0 0 1 0 1 0 1", "output": "-1" }, { "input": "60\n3 0 0 1 0 0 0 0 3 1 3 4 0 0 0 3 0 0 0 2 0 3 4 1 3 3 0 2 0 4 1 5 3 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 1 0 1 0 3 0 0", "output": "-1" }, { "input": "80\n4 0 0 0 0 0 0 3 0 3 0 0 0 4 3 0 1 0 2 0 0 0 5 0 5 0 0 0 0 4 0 3 0 0 0 1 0 0 2 0 5 2 0 0 4 4 0 3 0 0 0 0 0 0 0 2 5 0 2 0 0 0 0 0 0 0 0 0 0 3 0 0 3 5 0 0 0 0 0 0", "output": "-1" }, { "input": "100\n2 0 0 2 0 0 0 0 0 2 0 0 0 5 0 0 0 0 0 0 0 1 0 7 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 7 4 0 0 0 0 5 0 0 0 0 0 0 7 4 0 0 0 0 0 0 7 7 0 0 0 0 0 2 0 0 0 0 0 0 0 0 4 7 7 0 0 0", "output": "-1" }, { "input": "100\n1 0 0 0 1 2 3 2 1 0 1 2 3 1 3 1 0 0 1 1 0 0 2 1 2 1 3 3 1 0 0 1 0 2 2 0 3 0 1 1 1 2 0 2 0 1 0 2 0 1 2 2 0 0 0 0 1 0 0 0 1 1 4 0 2 0 1 0 2 0 2 2 2 1 1 0 0 2 0 3 1 0 0 1 1 0 1 0 2 3 2 0 1 2 0 0 0 0 0 1", "output": "99\n1 63\n63 7\n63 13\n63 15\n63 27\n7 28\n7 37\n7 80\n13 90\n13 6\n13 8\n15 12\n15 23\n15 25\n27 34\n27 35\n27 42\n28 44\n28 48\n28 51\n37 52\n37 65\n37 69\n80 71\n80 72\n80 73\n90 78\n90 89\n90 91\n6 94\n6 5\n8 9\n8 11\n12 14\n12 16\n23 19\n23 20\n25 24\n25 26\n34 29\n34 32\n35 39\n35 40\n42 41\n42 46\n44 50\n44 57\n48 61\n48 62\n51 67\n51 74\n52 75\n52 81\n65 84\n65 85\n69 87\n69 93\n71 100\n71 2\n72 3\n72 4\n73 10\n73 17\n78 18\n78 21\n89 22\n89 30\n91 31\n91 33\n94 36\n94 38\n5 43\n9 45\n11 47\n14 49\n..." }, { "input": "100\n5 0 0 1 1 0 0 0 1 0 0 0 0 0 5 0 2 1 0 1 0 6 0 0 0 3 0 0 0 0 0 0 0 0 1 0 3 0 0 0 0 0 0 4 0 0 3 1 1 0 0 4 0 0 0 0 0 0 3 2 3 3 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 2 1 0 5 1 0 8 0 1 0 0 10 4 0 0 0 6 4 0 0 0 0 1", "output": "99\n1 89\n1 84\n1 22\n1 94\n1 15\n89 81\n89 44\n89 52\n89 90\n89 95\n89 26\n89 37\n89 47\n89 59\n89 61\n84 62\n84 17\n84 60\n84 75\n84 78\n84 4\n84 5\n84 9\n22 18\n22 20\n22 35\n22 48\n22 49\n22 79\n94 82\n94 86\n94 100\n94 2\n94 3\n94 6\n15 7\n15 8\n15 10\n15 11\n15 12\n81 13\n81 14\n81 16\n81 19\n81 21\n44 23\n44 24\n44 25\n44 27\n52 28\n52 29\n52 30\n52 31\n90 32\n90 33\n90 34\n90 36\n95 38\n95 39\n95 40\n95 41\n26 42\n26 43\n26 45\n37 46\n37 50\n37 51\n47 53\n47 54\n47 55\n59 56\n59 57\n59 58\n61 63\n6..." }, { "input": "100\n47 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 4 0 1 0 2 0 0 1 0 0 0 0 0 1 18 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0", "output": "99\n1 47\n1 12\n1 66\n1 33\n1 93\n1 37\n1 81\n1 25\n1 35\n1 40\n1 46\n1 61\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 34\n1 36\n1 38\n1 39\n1 41\n1 42\n47 43\n47 44\n47 45\n47 48\n47 49\n47 50\n47 51\n47 52\n47 53\n47 54\n47 55\n47 56\n47 57\n47 58\n47 59\n47 60\n47 62\n47 63\n12 64\n12 65\n12 67\n12 68\n12 69\n12 70\n12 71\n12 72\n12 73\n66 74\n66 75\n66 76\n66 77\n66 78\n66 79\n66..." }, { "input": "100\n1 0 2 1 1 1 0 0 0 3 2 1 1 1 0 1 0 1 1 1 0 1 1 1 0 1 1 1 1 2 1 0 2 1 1 1 0 0 1 2 1 3 1 1 0 0 2 1 0 1 1 1 2 1 2 0 3 1 2 0 1 1 2 2 1 1 1 1 1 2 0 0 2 1 1 0 1 2 1 1 1 1 1 0 1 1 1 0 3 0 0 2 2 0 0 0 2 1 2 0", "output": "99\n1 10\n10 42\n10 57\n10 89\n42 3\n42 11\n42 30\n57 33\n57 40\n57 47\n89 53\n89 55\n89 59\n3 63\n3 64\n11 70\n11 73\n30 78\n30 92\n33 93\n33 97\n40 99\n40 4\n47 5\n47 6\n53 12\n53 13\n55 14\n55 16\n59 18\n59 19\n63 20\n63 22\n64 23\n64 24\n70 26\n70 27\n73 28\n73 29\n78 31\n78 34\n92 35\n92 36\n93 39\n93 41\n97 43\n97 44\n99 48\n99 50\n4 51\n5 52\n6 54\n12 58\n13 61\n14 62\n16 65\n18 66\n19 67\n20 68\n22 69\n23 74\n24 75\n26 77\n27 79\n28 80\n29 81\n31 82\n34 83\n35 85\n36 86\n39 87\n41 98\n43 2\n44 7\n4..." }, { "input": "100\n83 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0", "output": "99\n1 99\n1 83\n1 22\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n99 84\n99 85\n99 ..." }, { "input": "100\n1 1 0 1 0 1 1 1 2 1 0 1 1 0 1 2 1 1 1 1 2 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 2 2 2 2 1 1 0 1 2 1 1 0 1 1 0 1 0 1 1 0 0 1 1 1 1 1 1 2 0 2 1 2 1 1 0 0 1 1 1 1 0 2 4 2 1 1 1 0 1 2 1 1 1 0 2 1 2", "output": "99\n1 86\n86 9\n86 16\n86 21\n86 44\n9 45\n9 46\n16 47\n16 52\n21 71\n21 73\n44 75\n44 85\n45 87\n45 93\n46 98\n46 100\n47 2\n47 4\n52 6\n52 7\n71 8\n71 10\n73 12\n73 13\n75 15\n75 17\n85 18\n85 19\n87 20\n87 22\n93 23\n93 25\n98 27\n98 28\n100 29\n100 30\n2 31\n4 32\n6 33\n7 34\n8 35\n10 36\n12 37\n13 38\n15 39\n17 41\n18 42\n19 48\n20 49\n22 51\n23 53\n25 54\n27 56\n28 57\n29 59\n30 61\n31 62\n32 65\n33 66\n34 67\n35 68\n36 69\n37 70\n38 74\n39 76\n41 77\n42 80\n48 81\n49 82\n51 83\n53 88\n54 89\n56 90\n..." }, { "input": "100\n99 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "99\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n1 53\n1 54\n1 55\n1 56\n1 57\n1 58\n1 59\n1 60\n1 61\n1 62\n1 63\n1 64\n1 65\n1 66\n1 67\n1 68\n1 69\n1 70\n1 71\n1 72\n1 73\n1 74\n1 75\n1 76\n1 77\n1 78\n1 79\n1 80\n1 81\n1 82\n1 83\n1 84\n1 85\n1 86\n1 87\n..." }, { "input": "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 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "99\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50\n50 52\n52 53\n53 54\n54 55\n55 56\n56 57\n57 58\n58 59\n59 60\n60 61\n61 62\n62 63\n63 64\n64 65\n65 66\n66 67\n67 68\n68 69\n69 70\n70 71\n71 72\n72 73\n73 74\n74 75\n75 76\n76 77\n..." }, { "input": "2\n0 100", "output": "-1" }, { "input": "2\n100 0", "output": "1\n1 2" }, { "input": "2\n100 100", "output": "1\n1 2" } ]
62
4,915,200
3
8,460
27
Number With The Given Amount Of Divisors
[ "brute force", "dp", "number theory" ]
E. Number With The Given Amount Of Divisors
2
256
Given the number *n*, find the smallest positive integer which has exactly *n* divisors. It is guaranteed that for the given *n* the answer will not exceed 1018.
The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=1000).
Output the smallest positive integer with exactly *n* divisors.
[ "4\n", "6\n" ]
[ "6\n", "12\n" ]
none
[ { "input": "1", "output": "1" }, { "input": "7", "output": "64" }, { "input": "8", "output": "24" }, { "input": "9", "output": "36" }, { "input": "10", "output": "48" }, { "input": "15", "output": "144" }, { "input": "20", "output": "240" }, { "input": "47", "output": "70368744177664" }, { "input": "59", "output": "288230376151711744" }, { "input": "100", "output": "45360" }, { "input": "159", "output": "40532396646334464" }, { "input": "265", "output": "364791569817010176" }, { "input": "312", "output": "14192640" }, { "input": "473", "output": "259700248434180096" }, { "input": "637", "output": "46656000000" }, { "input": "500", "output": "62370000" }, { "input": "720", "output": "61261200" }, { "input": "902", "output": "324625310542725120" }, { "input": "940", "output": "199495389743677440" }, { "input": "1000", "output": "810810000" }, { "input": "999", "output": "757632231014400" }, { "input": "118", "output": "864691128455135232" } ]
92
0
3.977
8,477
774
Big Number and Remainder
[ "*special", "math", "number theory" ]
null
null
Stepan has a very big positive integer. Let's consider all cyclic shifts of Stepan's integer (if we look at his integer like at a string) which are also integers (i.e. they do not have leading zeros). Let's call such shifts as good shifts. For example, for the integer 10203 the good shifts are the integer itself 10203 and integers 20310 and 31020. Stepan wants to know the minimum remainder of the division by the given number *m* among all good shifts. Your task is to determine the minimum remainder of the division by *m*.
The first line contains the integer which Stepan has. The length of Stepan's integer is between 2 and 200<=000 digits, inclusive. It is guaranteed that Stepan's integer does not contain leading zeros. The second line contains the integer *m* (2<=≤<=*m*<=≤<=108) — the number by which Stepan divides good shifts of his integer.
Print the minimum remainder which Stepan can get if he divides all good shifts of his integer by the given number *m*.
[ "521\n3\n", "1001\n5\n", "5678901234567890123456789\n10000\n" ]
[ "2\n", "0\n", "123\n" ]
In the first example all good shifts of the integer 521 (good shifts are equal to 521, 215 and 152) has same remainder 2 when dividing by 3. In the second example there are only two good shifts: the Stepan's integer itself and the shift by one position to the right. The integer itself is 1001 and the remainder after dividing it by 5 equals 1. The shift by one position to the right equals to 1100 and the remainder after dividing it by 5 equals 0, which is the minimum possible remainder.
[ { "input": "521\n3", "output": "2" }, { "input": "1001\n5", "output": "0" }, { "input": "5678901234567890123456789\n10000", "output": "123" }, { "input": "552352155\n13", "output": "2" }, { "input": "11533077525260\n193983", "output": "22331" }, { "input": "15\n19", "output": "13" }, { "input": "2342341\n2342340", "output": "1" }, { "input": "12345\n12344", "output": "1" }, { "input": "23457\n23456", "output": "1" }, { "input": "79\n60847671", "output": "79" }, { "input": "456\n79575973", "output": "456" }, { "input": "1908\n86567928", "output": "1908" }, { "input": "30866\n55357692", "output": "30866" }, { "input": "980552\n32506042", "output": "298055" }, { "input": "4865180\n11799246", "output": "1804865" }, { "input": "63014535\n5261418", "output": "948834" }, { "input": "260597722\n10577910", "output": "38629" }, { "input": "8460711003592660299377170851036141857177105821455679225940496548902563144980707248030407967017317182\n13315371", "output": "16290" } ]
3,000
5,529,600
0
8,484
0
none
[ "none" ]
null
null
One day Natalia was walking in the woods when she met a little mushroom gnome. The gnome told her the following story: Everybody knows that the mushroom gnomes' power lies in the magic mushrooms that grow in the native woods of the gnomes. There are *n* trees and *m* magic mushrooms in the woods: the *i*-th tree grows at a point on a straight line with coordinates *a**i* and has the height of *h**i*, the *j*-th mushroom grows at the point with coordinates *b**j* and has magical powers *z**j*. But one day wild mushroommunchers, the sworn enemies of mushroom gnomes unleashed a terrible storm on their home forest. As a result, some of the trees began to fall and crush the magic mushrooms. The supreme oracle of mushroom gnomes calculated in advance the probability for each tree that it will fall to the left, to the right or will stand on. If the tree with the coordinate *x* and height *h* falls to the left, then all the mushrooms that belong to the right-open interval [*x*<=-<=*h*,<=*x*), are destroyed. If a tree falls to the right, then the mushrooms that belong to the left-open interval (*x*,<=*x*<=+<=*h*] are destroyed. Only those mushrooms that are not hit by a single tree survive. Knowing that all the trees fall independently of each other (i.e., all the events are mutually independent, and besides, the trees do not interfere with other trees falling in an arbitrary direction), the supreme oracle was also able to quickly calculate what would be the expectation of the total power of the mushrooms which survived after the storm. His calculations ultimately saved the mushroom gnomes from imminent death. Natalia, as a good Olympiad programmer, got interested in this story, and she decided to come up with a way to quickly calculate the expectation of the sum of the surviving mushrooms' power.
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=105, 1<=≤<=*m*<=≤<=104) — the number of trees and mushrooms, respectively. Each of the next *n* lines contain four integers — *a**i*, *h**i*, *l**i*, *r**i* (|*a**i*|<=≤<=109, 1<=≤<=*h**i*<=≤<=109, 0<=≤<=*l**i*,<=*r**i*,<=*l**i*<=+<=*r**i*<=≤<=100) which represent the coordinate of the *i*-th tree, its height, the percentage of the probabilities that the tree falls to the left and to the right, respectively (the remaining percentage is the probability that the tree will stand on). Each of next *m* lines contain two integers *b**j*, *z**j* (|*b**j*|<=≤<=109, 1<=≤<=*z**j*<=≤<=103) which represent the coordinate and the magical power of the *j*-th mushroom, respectively. An arbitrary number of trees and mushrooms can grow in one point.
Print a real number — the expectation of the total magical power of the surviving mushrooms. The result is accepted with relative or absolute accuracy 10<=-<=4.
[ "1 1\n2 2 50 50\n1 1\n", "2 1\n2 2 50 50\n4 2 50 50\n3 1\n" ]
[ "0.5000000000\n", "0.2500000000\n" ]
It is believed that the mushroom with the coordinate *x* belongs to the right-open interval [*l*, *r*) if and only if *l* ≤ *x* &lt; *r*. Similarly, the mushroom with the coordinate *x* belongs to the left-open interval (*l*, *r*] if and only if *l* &lt; *x* ≤ *r*. In the first test the mushroom survives with the probability of 50%, depending on where the single tree falls. In the second test the mushroom survives only if neither of the two trees falls on it. It occurs with the probability of 50%  ×  50% = 25%. Pretest №12 is the large test with 10<sup class="upper-index">5</sup> trees and one mushroom.
[]
31
0
0
8,498
727
Bill Total Value
[ "expression parsing", "implementation", "strings" ]
null
null
Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format "*name*1*price*1*name*2*price*2...*name**n**price**n*", where *name**i* (name of the *i*-th purchase) is a non-empty string of length not more than 10, consisting of lowercase English letters, and *price**i* (the price of the *i*-th purchase) is a non-empty string, consisting of digits and dots (decimal points). It is possible that purchases with equal names have different prices. The price of each purchase is written in the following format. If the price is an integer number of dollars then cents are not written. Otherwise, after the number of dollars a dot (decimal point) is written followed by cents in a two-digit format (if number of cents is between 1 and 9 inclusively, there is a leading zero). Also, every three digits (from less significant to the most) in dollars are separated by dot (decimal point). No extra leading zeroes are allowed. The price always starts with a digit and ends with a digit. For example: - "234", "1.544", "149.431.10", "0.99" and "123.05" are valid prices, - ".333", "3.33.11", "12.00", ".33", "0.1234" and "1.2" are not valid. Write a program that will find the total price of all purchases in the given bill.
The only line of the input contains a non-empty string *s* with length not greater than 1000 — the content of the bill. It is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 106 dollars.
Print the total price exactly in the same format as prices given in the input.
[ "chipsy48.32televizor12.390\n", "a1b2c3.38\n", "aa0.01t0.03\n" ]
[ "12.438.32\n", "6.38\n", "0.04\n" ]
none
[ { "input": "chipsy48.32televizor12.390", "output": "12.438.32" }, { "input": "a1b2c3.38", "output": "6.38" }, { "input": "aa0.01t0.03", "output": "0.04" }, { "input": "test0.50test0.50", "output": "1" }, { "input": "a500b500", "output": "1.000" }, { "input": "tcjbjlbtjf329.910", "output": "329.910" }, { "input": "iwpcfsmzen297.618.42ff585.209.84", "output": "882.828.26" }, { "input": "dpinb27.277fwxpdbfg709.917vocemjru16.491ade860.722tvb870.469.51wrpgy565.046gddrwv202.271.28", "output": "3.252.193.79" }, { "input": "vayscqiwpc686.919.75bwyudkz759.174kgqq444.563.54feupje806.486.78vojngmlc385.668.02jrkzbsa819.334b32.509wmjg980.332yh894.786hw356.243oiuueu662.016ychbsklfln21.860.87p836.999.94huhiiqlqoc596.917.99", "output": "8.283.810.89" }, { "input": "amhppqxei543.370.32o544.196nocwgxticn776.562nm212.195dcftrrg635.773n646.814.94vrfmjjsgoi405.114k821.983.12rb749.955.62jifmdlgs615.101hg42.083.41gdqififg908.729qrrgopyn684.451avcjul727.150s864.068bcd196.732.37jd349.984.25ghn379.763.11dw881.650.19eysthrm790.534.68gilg546.048qs648.876pdudevipn986.325jcwqq376.669.92qp169.861qyjguum254.785.35kcxgl820.940adtenavaj279.104naaxcl531.444.02jh478.042.53", "output": "16.868.306.83" }, { "input": "aasf0.01egfr0.50edfasdf0.99rwer999.999.99", "output": "1.000.001.49" }, { "input": "a1.01", "output": "1.01" }, { "input": "a0.11", "output": "0.11" }, { "input": "r0.30q0.10", "output": "0.40" }, { "input": "asd0.03sgbgfh0.27", "output": "0.30" }, { "input": "sadfa4.44f0.56", "output": "5" }, { "input": "tr999.999.99r0.01", "output": "1.000.000" }, { "input": "f999.999.99fsdf0.01wef1.10dfs2.90", "output": "1.000.004" }, { "input": "a0.01", "output": "0.01" }, { "input": "q999.10", "output": "999.10" }, { "input": "a0.40", "output": "0.40" }, { "input": "t999.000.01", "output": "999.000.01" }, { "input": "kapusta123.456", "output": "123.456" } ]
93
6,963,200
0
8,508
803
Coprime Subsequences
[ "bitmasks", "combinatorics", "number theory" ]
null
null
Let's call a non-empty sequence of positive integers *a*1,<=*a*2... *a**k* coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array *a* consisting of *n* positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 109<=+<=7. Note that two subsequences are considered different if chosen indices are different. For example, in the array [1,<=1] there are 3 different subsequences: [1], [1] and [1,<=1].
The first line contains one integer number *n* (1<=≤<=*n*<=≤<=100000). The second line contains *n* integer numbers *a*1,<=*a*2... *a**n* (1<=≤<=*a**i*<=≤<=100000).
Print the number of coprime subsequences of *a* modulo 109<=+<=7.
[ "3\n1 2 3\n", "4\n1 1 1 1\n", "7\n1 3 5 15 3 105 35\n" ]
[ "5\n", "15\n", "100\n" ]
In the first example coprime subsequences are: 1. 1 1. 1, 2 1. 1, 3 1. 1, 2, 3 1. 2, 3 In the second example all subsequences are coprime.
[ { "input": "3\n1 2 3", "output": "5" }, { "input": "4\n1 1 1 1", "output": "15" }, { "input": "7\n1 3 5 15 3 105 35", "output": "100" }, { "input": "1\n1", "output": "1" }, { "input": "1\n100000", "output": "0" }, { "input": "5\n10 8 6 4 6", "output": "0" }, { "input": "5\n5 1 3 5 4", "output": "26" }, { "input": "5\n5 1 6 6 6", "output": "23" }, { "input": "10\n9 6 8 5 5 2 8 9 2 2", "output": "951" }, { "input": "10\n2 2 16 16 14 1 9 12 15 13", "output": "953" }, { "input": "50\n17 81 20 84 6 86 11 33 19 46 70 79 23 64 40 99 78 70 3 10 32 42 18 73 35 36 69 90 81 81 8 25 87 23 76 100 53 11 36 19 87 89 53 65 97 67 3 65 88 87", "output": "896338157" }, { "input": "50\n166 126 98 42 179 166 99 192 1 185 114 173 152 187 57 21 132 88 152 55 110 51 1 30 147 153 34 115 59 3 78 16 19 136 188 134 28 48 54 120 97 74 108 54 181 79 143 187 51 4", "output": "763698643" }, { "input": "100\n154 163 53 13 186 87 143 114 17 111 143 108 102 111 158 171 69 74 67 18 87 43 80 104 63 109 19 113 86 52 119 91 15 154 9 153 140 91 19 19 191 193 76 84 50 128 173 27 120 83 6 59 65 5 135 59 162 121 15 110 146 107 137 99 55 189 2 118 55 27 4 198 23 79 167 125 72 30 74 163 44 184 166 43 198 116 68 5 47 138 121 146 98 103 89 75 137 36 146 195", "output": "363088732" }, { "input": "100\n881 479 355 759 257 497 690 598 275 446 439 787 257 326 584 713 322 5 253 781 434 307 164 154 241 381 38 942 680 906 240 11 431 478 628 959 346 74 493 964 455 746 950 41 585 549 892 687 264 41 487 676 63 453 861 980 477 901 80 907 285 506 619 748 773 743 56 925 651 685 845 313 419 504 770 324 2 559 405 851 919 128 318 698 820 409 547 43 777 496 925 918 162 725 481 83 220 203 609 617", "output": "934190491" } ]
982
26,521,600
3
8,516
545
Toy Cars
[ "implementation" ]
null
null
Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph. There are *n* toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an *n*<=×<=*n* matrix *А*: there is a number on the intersection of the *і*-th row and *j*-th column that describes the result of the collision of the *і*-th and the *j*-th car: - <=-<=1: if this pair of cars never collided. <=-<=1 occurs only on the main diagonal of the matrix. - 0: if no car turned over during the collision. - 1: if only the *i*-th car turned over during the collision. - 2: if only the *j*-th car turned over during the collision. - 3: if both cars turned over during the collision. Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task?
The first line contains integer *n* (1<=≤<=*n*<=≤<=100) — the number of cars. Each of the next *n* lines contains *n* space-separated integers that determine matrix *A*. It is guaranteed that on the main diagonal there are <=-<=1, and <=-<=1 doesn't appear anywhere else in the matrix. It is guaranteed that the input is correct, that is, if *A**ij*<==<=1, then *A**ji*<==<=2, if *A**ij*<==<=3, then *A**ji*<==<=3, and if *A**ij*<==<=0, then *A**ji*<==<=0.
Print the number of good cars and in the next line print their space-separated indices in the increasing order.
[ "3\n-1 0 0\n0 -1 1\n0 2 -1\n", "4\n-1 3 3 3\n3 -1 3 3\n3 3 -1 3\n3 3 3 -1\n" ]
[ "2\n1 3 ", "0\n" ]
none
[ { "input": "3\n-1 0 0\n0 -1 1\n0 2 -1", "output": "2\n1 3 " }, { "input": "4\n-1 3 3 3\n3 -1 3 3\n3 3 -1 3\n3 3 3 -1", "output": "0" }, { "input": "1\n-1", "output": "1\n1 " }, { "input": "2\n-1 0\n0 -1", "output": "2\n1 2 " }, { "input": "2\n-1 1\n2 -1", "output": "1\n2 " }, { "input": "2\n-1 2\n1 -1", "output": "1\n1 " }, { "input": "2\n-1 3\n3 -1", "output": "0" } ]
46
0
3
8,541
618
Constellation
[ "geometry", "implementation" ]
null
null
Cat Noku has obtained a map of the night sky. On this map, he found a constellation with *n* stars numbered from 1 to *n*. For each *i*, the *i*-th star is located at coordinates (*x**i*,<=*y**i*). No two stars are located at the same position. In the evening Noku is going to take a look at the night sky. He would like to find three distinct stars and form a triangle. The triangle must have positive area. In addition, all other stars must lie strictly outside of this triangle. He is having trouble finding the answer and would like your help. Your job is to find the indices of three stars that would form a triangle that satisfies all the conditions. It is guaranteed that there is no line such that all stars lie on that line. It can be proven that if the previous condition is satisfied, there exists a solution to this problem.
The first line of the input contains a single integer *n* (3<=≤<=*n*<=≤<=100<=000). Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=109<=≤<=*x**i*,<=*y**i*<=≤<=109). It is guaranteed that no two stars lie at the same point, and there does not exist a line such that all stars lie on that line.
Print three distinct integers on a single line — the indices of the three points that form a triangle that satisfies the conditions stated in the problem. If there are multiple possible answers, you may print any of them.
[ "3\n0 1\n1 0\n1 1\n", "5\n0 0\n0 2\n2 0\n2 2\n1 1\n" ]
[ "1 2 3\n", "1 3 5\n" ]
In the first sample, we can print the three indices in any order. In the second sample, we have the following picture. Note that the triangle formed by starts 1, 4 and 3 doesn't satisfy the conditions stated in the problem, as point 5 is not strictly outside of this triangle (it lies on it's border).
[ { "input": "3\n0 1\n1 0\n1 1", "output": "1 2 3" }, { "input": "5\n0 0\n0 2\n2 0\n2 2\n1 1", "output": "1 3 5" }, { "input": "3\n819934317 939682125\n487662889 8614219\n-557136619 382982369", "output": "1 3 2" }, { "input": "10\n25280705 121178189\n219147240 -570920213\n-829849659 923854124\n18428128 -781819137\n-876779400 528386329\n-780997681 387686853\n-101900553 749998368\n58277314 355353788\n732128908 336416193\n840698381 600685123", "output": "1 3 2" }, { "input": "10\n404775998 670757742\n30131431 723806809\n25599613 633170449\n13303280 387243789\n-33017802 -539177851\n1425218 149682549\n-47620079 -831223391\n-25996011 -398742031\n38471092 890600029\n-3745401 46270169", "output": "1 2 3" }, { "input": "10\n13303280 387243789\n30131431 723806809\n404775998 670757742\n-25996011 -398742031\n25599613 633170449\n38471092 890600029\n-33017802 -539177851\n-47620079 -831223391\n1425218 149682549\n-3745401 46270169", "output": "1 3 5" }, { "input": "10\n999999999 1\n999999998 1\n999999997 1\n1000000000 1\n999999996 1\n999999995 1\n999999994 1\n999999992 1\n999999993 1\n0 0", "output": "1 2 10" }, { "input": "4\n0 1\n0 2\n0 3\n7 7", "output": "1 4 2" }, { "input": "3\n0 0\n999999999 1\n999999998 1", "output": "1 2 3" }, { "input": "10\n0 999999999\n0 1000000000\n-1 1000000000\n1 1000000000\n-2 1000000000\n2 1000000000\n-3 1000000000\n3 1000000000\n-4 1000000000\n4 1000000000", "output": "1 2 3" }, { "input": "12\n1000000000 0\n1000000000 1\n1000000000 2\n1000000000 3\n1000000000 4\n1000000000 5\n1000000000 6\n1000000000 7\n1000000000 8\n1000000000 9\n1000000000 10\n999999999 5", "output": "1 2 12" }, { "input": "12\n1000000000 0\n1000000000 1\n1000000000 2\n1000000000 3\n1000000000 4\n1000000000 5\n1000000000 6\n1000000000 7\n1000000000 8\n1000000000 9\n1000000000 10\n999999999 -1", "output": "1 2 12" }, { "input": "12\n1000000000 0\n1000000000 1\n1000000000 2\n1000000000 3\n1000000000 4\n1000000000 5\n1000000000 6\n1000000000 7\n1000000000 8\n1000000000 9\n1000000000 10\n999999999 10", "output": "1 2 12" }, { "input": "12\n1000000000 0\n1000000000 1\n1000000000 2\n1000000000 3\n1000000000 4\n1000000000 5\n1000000000 6\n1000000000 7\n1000000000 8\n1000000000 9\n1000000000 10\n999999999 1", "output": "1 2 12" }, { "input": "11\n-1000000000 1\n-1000000000 2\n-1000000000 3\n-1000000000 4\n-1000000000 5\n-1000000000 6\n-1000000000 7\n-1000000000 8\n-1000000000 9\n-1000000000 10\n-999999999 5", "output": "1 11 2" }, { "input": "11\n-1000000000 1\n-1000000000 2\n-1000000000 3\n-1000000000 4\n-1000000000 5\n-1000000000 6\n-1000000000 7\n-1000000000 8\n-1000000000 9\n-1000000000 10\n-999999999 7", "output": "1 11 2" }, { "input": "11\n-1000000000 1\n-1000000000 2\n-1000000000 3\n-1000000000 4\n-1000000000 5\n-1000000000 6\n-1000000000 7\n-1000000000 8\n-1000000000 9\n-1000000000 10\n-999999999 8", "output": "1 11 2" }, { "input": "11\n-1000000000 1\n-1000000000 2\n-1000000000 3\n-1000000000 4\n-1000000000 5\n-1000000000 6\n-1000000000 7\n-1000000000 8\n-1000000000 9\n-1000000000 10\n-999999999 10", "output": "1 11 2" }, { "input": "11\n-1000000000 -1\n-1000000000 -2\n-1000000000 -3\n-1000000000 -4\n-1000000000 -5\n-1000000000 -6\n-1000000000 -7\n-1000000000 -8\n-1000000000 -9\n-1000000000 -10\n-999999999 -5", "output": "1 2 11" }, { "input": "11\n-1000000000 -1\n-1000000000 -2\n-1000000000 -3\n-1000000000 -4\n-1000000000 -5\n-1000000000 -6\n-1000000000 -7\n-1000000000 -8\n-1000000000 -9\n-1000000000 -10\n-999999999 -1", "output": "1 2 11" }, { "input": "11\n-1000000000 -1\n-1000000000 -2\n-1000000000 -3\n-1000000000 -4\n-1000000000 -5\n-1000000000 -6\n-1000000000 -7\n-1000000000 -8\n-1000000000 -9\n-1000000000 -10\n-999999999 -2", "output": "1 2 11" }, { "input": "11\n-1000000000 -1\n-1000000000 -2\n-1000000000 -3\n-1000000000 -4\n-1000000000 -5\n-1000000000 -6\n-1000000000 -7\n-1000000000 -8\n-1000000000 -9\n-1000000000 -10\n-999999999 -4", "output": "1 2 11" }, { "input": "11\n-1000000000 -1\n-1000000000 -2\n-1000000000 -3\n-1000000000 -4\n-1000000000 -5\n-1000000000 -6\n-1000000000 -7\n-1000000000 -8\n-1000000000 -9\n-1000000000 -10\n-999999999 -8", "output": "1 2 11" }, { "input": "10\n2 1000000000\n8 1000000000\n9 1000000000\n3 1000000000\n4 1000000000\n5 1000000000\n6 1000000000\n1 1000000000\n7 1000000000\n0 0", "output": "1 10 4" }, { "input": "10\n1000000000 1\n999999999 1\n999999998 1\n999999997 1\n999999996 1\n999999995 1\n999999994 1\n999999993 1\n999999992 1\n0 0", "output": "1 2 10" }, { "input": "10\n999999999 1\n999999998 1\n999999997 1\n999999996 1\n999999995 1\n999999994 1\n999999993 1\n1000000000 1\n999999992 1\n0 0", "output": "1 2 10" }, { "input": "4\n0 0\n1 0\n2 0\n1 100", "output": "1 2 4" }, { "input": "4\n0 0\n3 0\n2 0\n1 1", "output": "3 2 4" }, { "input": "4\n0 0\n1 1\n2 2\n3 4", "output": "1 2 4" }, { "input": "4\n0 0\n0 1\n0 2\n1 1", "output": "1 4 2" }, { "input": "4\n0 0\n2 0\n1 0\n1 1", "output": "3 2 4" }, { "input": "4\n0 0\n1 1\n2 2\n5 -1", "output": "1 4 2" }, { "input": "5\n0 1\n0 2\n0 3\n0 4\n10 10", "output": "1 5 2" }, { "input": "4\n0 1\n0 2\n0 3\n1 1", "output": "1 4 2" }, { "input": "4\n0 0\n1 0\n2 0\n2 1", "output": "1 2 4" }, { "input": "4\n0 0\n-1 -1\n1 1\n100 0", "output": "1 2 4" }, { "input": "4\n0 0\n2 0\n1 1\n1 0", "output": "4 2 3" }, { "input": "4\n0 0\n1 0\n2 0\n3 1", "output": "1 2 4" }, { "input": "3\n0 0\n12345691 12336918\n19349510 19335760", "output": "1 3 2" }, { "input": "21\n0 19\n0 0\n0 8\n0 2\n0 18\n0 17\n0 1\n0 5\n0 16\n0 11\n0 10\n0 13\n0 12\n0 14\n0 6\n0 7\n0 3\n0 15\n0 4\n0 9\n1 1", "output": "7 2 21" }, { "input": "10\n0 0\n1 -100\n1 100\n1 50\n1 0\n1 -50\n1 10\n1 -10\n1 5\n1 -5", "output": "1 2 6" }, { "input": "3\n1 2\n2 1\n2 3", "output": "1 2 3" }, { "input": "3\n-1000000000 -1000000000\n1000000000 -1000000000\n-1000000000 1000000000", "output": "1 2 3" }, { "input": "10\n0 0\n1 0\n2 0\n3 0\n4 0\n5 0\n6 0\n7 0\n8 1\n9 0", "output": "1 2 9" }, { "input": "4\n1 1\n2 2\n3 3\n10 11", "output": "1 2 4" }, { "input": "4\n0 0\n0 2\n0 1\n3 3", "output": "1 4 3" }, { "input": "4\n0 0\n2 2\n1 1\n2 0", "output": "1 4 3" }, { "input": "4\n0 1\n0 0\n0 5\n1 1", "output": "1 2 4" }, { "input": "4\n1 0\n2 0\n3 0\n-7 -7", "output": "1 4 2" }, { "input": "4\n0 0\n0 2\n0 1\n10 10", "output": "1 4 3" }, { "input": "4\n-50000000 204926\n0 0\n8192 50000000\n16384 100000000", "output": "1 2 3" }, { "input": "4\n65537 536870912\n0 536805376\n1 536870912\n-8191 0", "output": "1 3 2" }, { "input": "4\n0 0\n131072 0\n131072 131072\n200000 0", "output": "1 2 3" }, { "input": "3\n-536870912 10\n536870912 11\n-536870912 6", "output": "1 3 2" }, { "input": "4\n3 7\n2 4\n1 2\n0 0", "output": "1 3 2" }, { "input": "4\n0 0\n0 1\n0 2\n3 3", "output": "1 4 2" } ]
717
307,200
0
8,544
260
Balls and Boxes
[ "constructive algorithms", "greedy", "implementation" ]
null
null
Little Vasya had *n* boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to *n* from left to right. Once Vasya chose one of the boxes, let's assume that its number is *i*, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers *i*<=+<=1, *i*<=+<=2, *i*<=+<=3 and so on. If Vasya puts a ball into the box number *n*, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number *i*. If *i*<==<=*n*, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on. For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if *i*<==<=3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4,<=1,<=2,<=3,<=4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one. At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number *x* — the number of the box, where he put the last of the taken out balls. He asks you to help to find the initial arrangement of the balls in the boxes.
The first line of the input contains two integers *n* and *x* (2<=≤<=*n*<=≤<=105, 1<=≤<=*x*<=≤<=*n*), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n*, where integer *a**i* (0<=≤<=*a**i*<=≤<=109, *a**x*<=≠<=0) represents the number of balls in the box with index *i* after Vasya completes all the actions. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print *n* integers, where the *i*-th one represents the number of balls in the box number *i* before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
[ "4 4\n4 3 1 6\n", "5 2\n3 2 0 2 7\n", "3 3\n2 3 1\n" ]
[ "3 2 5 4 ", "2 1 4 1 6 ", "1 2 3 " ]
none
[ { "input": "4 4\n4 3 1 6", "output": "3 2 5 4 " }, { "input": "5 2\n3 2 0 2 7", "output": "2 1 4 1 6 " }, { "input": "3 3\n2 3 1", "output": "1 2 3 " }, { "input": "10 3\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000", "output": "0 0 10000000000 0 0 0 0 0 0 0 " }, { "input": "5 4\n0 554459682 978416312 784688178 954779973", "output": "3 554459681 978416311 784688177 954779973 " }, { "input": "5 2\n1 554459683 978416312 784688178 954779974", "output": "6 554459681 978416311 784688177 954779973 " }, { "input": "10 8\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 " }, { "input": "10 5\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163", "output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 " }, { "input": "15 12\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495", "output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 " }, { "input": "15 8\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496", "output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 " }, { "input": "48 34\n227460647 746912226 53993109 682685525 621533698 666833117 492590398 167395931 678377836 66509684 638633255 713194369 386921920 34175132 704550051 220688091 499436760 495071385 102952101 137372655 0 720974146 209203457 946682102 237312198 943872065 957150897 357568282 367006748 0 730509325 68523190 726721460 85037180 620964625 219537305 396613042 39840356 91947418 566463810 791505982 87036026 446282153 912204581 895882687 284561729 35994526 423462628", "output": "227460647 746912226 53993109 682685525 621533698 666833117 492590398 167395931 678377836 66509684 638633255 713194369 386921920 34175132 704550051 220688091 499436760 495071385 102952101 137372655 0 720974146 209203457 946682102 237312198 943872065 957150897 357568282 367006748 4 730509324 68523189 726721459 85037179 620964625 219537305 396613042 39840356 91947418 566463810 791505982 87036026 446282153 912204581 895882687 284561729 35994526 423462628 " }, { "input": "48 19\n227460648 746912227 53993110 682685526 621533699 666833118 492590399 167395932 678377837 66509685 638633256 713194370 386921921 34175133 704550052 220688092 499436761 495071386 102952102 137372655 0 720974146 209203457 946682102 237312198 943872065 957150897 357568282 367006748 0 730509325 68523190 726721460 85037180 620964626 219537306 396613043 39840357 91947419 566463811 791505983 87036027 446282154 912204582 895882688 284561730 35994527 423462629", "output": "227460647 746912226 53993109 682685525 621533698 666833117 492590398 167395931 678377836 66509684 638633255 713194369 386921920 34175132 704550051 220688091 499436760 495071385 102952101 137372655 0 720974146 209203457 946682102 237312198 943872065 957150897 357568282 367006748 37 730509324 68523189 726721459 85037179 620964625 219537305 396613042 39840356 91947418 566463810 791505982 87036026 446282153 912204581 895882687 284561729 35994526 423462628 " }, { "input": "10 5\n3 3 3 3 4 3 3 3 3 3", "output": "0 0 0 31 0 0 0 0 0 0 " }, { "input": "5 4\n3 1 3 1 3", "output": "2 0 2 5 2 " } ]
93
0
0
8,565
961
Pair Of Lines
[ "geometry" ]
null
null
You are given *n* points on Cartesian plane. Every point is a lattice point (i.<=e. both of its coordinates are integers), and all points are distinct. You may draw two straight lines (not necessarily distinct). Is it possible to do this in such a way that every point lies on at least one of these lines?
The first line contains one integer *n* (1<=≤<=*n*<=≤<=105) — the number of points you are given. Then *n* lines follow, each line containing two integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=≤<=109)— coordinates of *i*-th point. All *n* points are distinct.
If it is possible to draw two straight lines in such a way that each of given points belongs to at least one of these lines, print YES. Otherwise, print NO.
[ "5\n0 0\n0 1\n1 1\n1 -1\n2 2\n", "5\n0 0\n1 0\n2 1\n1 1\n2 3\n" ]
[ "YES\n", "NO\n" ]
In the first example it is possible to draw two lines, the one containing the points 1, 3 and 5, and another one containing two remaining points.
[ { "input": "5\n0 0\n0 1\n1 1\n1 -1\n2 2", "output": "YES" }, { "input": "5\n0 0\n1 0\n2 1\n1 1\n2 3", "output": "NO" }, { "input": "1\n-1000000000 1000000000", "output": "YES" }, { "input": "5\n2 -1\n-4 1\n0 -9\n5 -9\n9 -10", "output": "NO" }, { "input": "5\n6 1\n10 5\n10 -2\n-2 -10\n-4 -9", "output": "YES" }, { "input": "5\n-10 3\n4 -5\n-9 5\n-5 -3\n-4 -6", "output": "NO" }, { "input": "5\n2 9\n-1 -4\n-3 -8\n-4 8\n7 2", "output": "NO" }, { "input": "10\n315 202\n315 203\n315 204\n-138 -298\n-136 -295\n-134 -292\n-132 -289\n-130 -286\n-128 -283\n-126 -280", "output": "YES" }, { "input": "10\n416 -473\n-162 491\n-164 488\n-170 479\n-166 485\n-172 476\n416 -475\n416 -474\n-168 482\n-160 494", "output": "YES" }, { "input": "6\n0 0\n1 1\n0 1\n1 0\n0 2\n2 0", "output": "NO" }, { "input": "5\n3 3\n6 3\n0 0\n10 0\n-10 0", "output": "YES" }, { "input": "1\n0 0", "output": "YES" }, { "input": "10\n0 0\n1 0\n0 1\n0 2\n2 0\n3 0\n0 3\n0 4\n4 0\n0 -10000000", "output": "YES" }, { "input": "6\n0 0\n0 1\n0 2\n1 1\n1 2\n2 1", "output": "NO" }, { "input": "6\n0 -1\n1 -1\n3 3\n2 0\n-2 -2\n1 -2", "output": "NO" }, { "input": "5\n1000000000 1000000000\n999999999 999999999\n999999999 999999998\n-1000000000 1000000000\n-1000000000 999999999", "output": "NO" }, { "input": "5\n0 0\n1 0\n0 1\n1 1\n-1 1", "output": "YES" }, { "input": "6\n0 0\n0 1\n0 -1\n1 1\n1 -1\n2 -1", "output": "NO" }, { "input": "4\n0 0\n0 1\n1 0\n1 1", "output": "YES" }, { "input": "6\n0 0\n1 0\n2 1\n1 1\n0 1\n6 0", "output": "YES" }, { "input": "10\n536870912 536870912\n268435456 368435456\n268435456 168435456\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9", "output": "NO" }, { "input": "5\n0 0\n0 1\n100 100\n100 99\n100 98", "output": "YES" }, { "input": "8\n0 0\n1 0\n2 1\n1 1\n0 1\n6 0\n5 0\n7 0", "output": "YES" }, { "input": "5\n0 0\n2 0\n1 1\n0 2\n5 1", "output": "YES" }, { "input": "7\n0 0\n4 0\n1 1\n2 2\n3 1\n5 1\n6 2", "output": "NO" }, { "input": "6\n1 1\n2 2\n3 2\n4 1\n5 2\n6 1", "output": "YES" }, { "input": "8\n0 0\n1 0\n2 0\n3 0\n0 1\n1 1\n2 1\n3 1", "output": "YES" }, { "input": "12\n0 0\n1 1\n2 2\n3 3\n10 11\n20 11\n30 11\n40 11\n-1 1\n-2 2\n-3 3\n-4 4", "output": "NO" }, { "input": "6\n0 0\n165580141 267914296\n331160282 535828592\n267914296 433494437\n535828592 866988874\n433494437 701408733", "output": "NO" }, { "input": "5\n-1000000000 -1000000000\n-588442013 -868997024\n-182303377 -739719081\n-999999999 -999999999\n229254610 -608716105", "output": "NO" }, { "input": "5\n-1000000000 -1000000000\n229254610 -608716105\n-588442013 -868997024\n-182303377 -739719081\n-176884026 -737994048", "output": "YES" }, { "input": "6\n0 0\n0 1\n0 2\n5 0\n5 1\n5 -1", "output": "YES" }, { "input": "5\n-1 1\n1 0\n1 1\n1 -1\n-1 -1", "output": "YES" }, { "input": "5\n-1000000000 -1000000000\n229254610 -608716105\n-588442013 -868997024\n-182303377 -739719081\n-999999999 -999999999", "output": "NO" }, { "input": "6\n1 1\n0 0\n-1 -1\n1 0\n0 -1\n-1 -10", "output": "NO" }, { "input": "5\n8 8\n3303829 10\n10 1308\n4 2\n6 3", "output": "NO" }, { "input": "5\n0 0\n0 1\n0 2\n0 3\n1 0", "output": "YES" }, { "input": "5\n0 0\n165580142 267914296\n331160283 535828592\n267914296 433494437\n535828592 866988874", "output": "YES" }, { "input": "59\n1 0\n0 2\n0 3\n0 4\n0 5\n6 0\n7 0\n8 0\n9 0\n10 0\n0 11\n12 0\n13 0\n14 0\n15 0\n0 16\n0 17\n18 0\n19 0\n20 0\n21 0\n0 22\n23 0\n24 0\n0 25\n26 0\n27 0\n0 28\n0 29\n30 0\n31 0\n0 32\n33 0\n34 0\n0 35\n0 36\n37 0\n0 38\n39 0\n40 0\n0 41\n42 0\n0 43\n0 44\n0 45\n0 46\n47 0\n0 48\n0 49\n50 0\n0 51\n0 52\n53 0\n0 54\n55 0\n0 56\n57 0\n0 58\n59 0", "output": "YES" }, { "input": "5\n10000000 40000100\n3 112\n2 400000100\n1 104\n1000000 701789036", "output": "YES" }, { "input": "5\n514 2131\n312 52362\n1 1\n2 2\n3 3", "output": "YES" }, { "input": "9\n-65536 65536\n0 65536\n65536 65536\n-65536 0\n0 0\n65536 0\n-65536 -65536\n0 -65536\n65536 -65536", "output": "NO" }, { "input": "5\n0 -7\n0 10000\n1 1000000000\n100 0\n200 0", "output": "NO" }, { "input": "7\n0 0\n2 2\n2 -2\n-2 2\n-2 -2\n0 1\n0 3", "output": "NO" }, { "input": "5\n3 0\n4 1\n0 0\n1 1\n2 2", "output": "YES" }, { "input": "5\n-65536 -65536\n65536 0\n131072 0\n0 65536\n0 131072", "output": "NO" }, { "input": "4\n0 0\n1 0\n0 1\n1 1", "output": "YES" }, { "input": "6\n0 0\n2 0\n0 2\n0 -2\n-2 1\n-4 2", "output": "NO" }, { "input": "5\n-1000000000 -1000000000\n134903170 -298591267\n-566505563 -732085704\n-298591267 -566505563\n-133011126 -464171408", "output": "YES" }, { "input": "5\n-1000000000 -1000000000\n134903170 -298591267\n-566505563 -732085704\n-298591267 -566505563\n-999999999 -999999999", "output": "NO" }, { "input": "5\n1 1\n-1 0\n0 1\n-1 1\n0 0", "output": "YES" }, { "input": "5\n0 0\n-1 -1\n0 -1\n-1 1\n-1 0", "output": "YES" }, { "input": "5\n0 0\n-1 1\n-1 0\n0 -1\n-1 -1", "output": "YES" }, { "input": "6\n0 0\n-1 1\n-1 0\n1 1\n-1 -1\n0 -1", "output": "NO" }, { "input": "5\n-1 2\n-1 1\n2 1\n-2 2\n1 1", "output": "YES" }, { "input": "6\n-1 -1\n-1 -2\n-1 -3\n1000000000 1\n-1000000000 0\n999999999 1", "output": "NO" }, { "input": "6\n-1 -1\n-1 -2\n-1 -3\n0 0\n65536 65536\n65536 131072", "output": "NO" }, { "input": "6\n-1 -1\n-1 -2\n-1 -3\n1000000000 1\n999999999 1\n-1000000000 0", "output": "NO" }, { "input": "3\n-1 1\n-1 -1\n0 0", "output": "YES" }, { "input": "7\n1 -1\n3 -3\n1 2\n0 -2\n1 -3\n0 1\n0 2", "output": "NO" }, { "input": "4\n0 0\n-1 1\n-1 -1\n1 0", "output": "YES" }, { "input": "6\n0 0\n0 1\n-1 1\n0 -1\n1 0\n-1 -1", "output": "NO" }, { "input": "5\n1 1\n0 0\n-1 0\n0 1\n1 0", "output": "YES" }, { "input": "11\n-2 -2\n2 3\n3 -2\n1 -2\n2 -2\n2 0\n2 2\n-3 -2\n-1 -2\n2 -3\n2 1", "output": "YES" }, { "input": "5\n0 0\n-1 0\n-1 1\n1 0\n1 -1", "output": "YES" }, { "input": "5\n1 -1\n0 0\n0 1\n-1 1\n1 1", "output": "YES" }, { "input": "5\n0 0\n1 1\n0 -2\n1 -1\n1 2", "output": "YES" }, { "input": "5\n-999999998 -999999998\n229254612 -608716103\n-588442011 -868997022\n-182303375 -739719079\n-176884024 -737994046", "output": "YES" } ]
326
34,816,000
3
8,575
106
Space Rescuers
[ "geometry", "ternary search" ]
E. Space Rescuers
2
256
The Galaxy contains *n* planets, there are many different living creatures inhabiting each planet. And each creature can get into troubles! Space rescuers know it perfectly well and they are always ready to help anyone who really needs help. All you need to do is call for them. Now the space rescuers plan to build the largest in the history of the Galaxy rescue station; however, the rescue station's location is yet to be determined. As some cases are real emergencies, the rescuers want to find such a point in the Galaxy from which it would be possible to get to the remotest planet in the minimum possible time. In other words, the rescuers need such point in the space that the distance between it and the planet remotest from it was minimal (if we compare this point with all other possible points in the space). Unfortunately, the rescuers can't sole this problem. As the planets are quite remote from each other, they can be considered as points in Euclidean three-dimensional space. The distance between points (*x**i*,<=*y**i*,<=*z**i*) and (*x**j*,<=*y**j*,<=*z**j*) can be calculated by the formula . The rescue station can be positioned in any point in the space. It can also coincide with some planet. Galaxy is in danger! Save the space rescuers and find the required point for them.
The first line of the input file contains integer *n* — the number of planets (1<=≤<=*N*<=≤<=100). Each of the following *n* lines contains information about the planets. The *i*-th line contains three integers *x**i*,<=*y**i*,<=*z**i* — the coordinates of the *i*-th planet (<=-<=104<=≤<=*x**i*,<=*y**i*,<=*z**i*<=≤<=104, 1<=≤<=*i*<=≤<=*n*). No two planets coincide.
Print on the first line of the output file three space-separated real numbers *x*0,<=*y*0,<=*z*0 — the coordinates for the future base. If there are several solutions, you are allowed to print any of them. The answer will be accepted if the distance from this point to the remotest planet will differ from the juries' variant in no more than 10<=-<=6 in absolute or relative value.
[ "5\n5 0 0\n-5 0 0\n0 3 4\n4 -3 0\n2 2 -2\n" ]
[ "0.000 0.000 0.000\n" ]
none
[ { "input": "5\n5 0 0\n-5 0 0\n0 3 4\n4 -3 0\n2 2 -2", "output": "-0.0000000017 -0.0000000319 0.0000000473" }, { "input": "4\n-2 -9 1\n10 4 0\n-1 1 0\n3 -10 -4", "output": "4.0000068501 -2.5000015036 0.5000626514" }, { "input": "5\n6 0 -4\n8 1 5\n-8 5 -6\n-2 -4 -3\n8 -2 1", "output": "0.0000449540 2.9999160856 -0.5000959014" }, { "input": "6\n-1 1 -5\n-1 -1 -9\n5 -2 -6\n4 8 2\n-4 -5 8\n-3 2 10", "output": "-1.6984521916 0.4352048078 0.5419726934" }, { "input": "7\n-1 4 -9\n6 -5 8\n1 -6 -10\n-9 5 7\n6 4 -6\n-1 -6 2\n-8 -6 4", "output": "-1.9794286570 -1.4394949979 0.2964799042" }, { "input": "8\n0 10 8\n-6 8 -5\n-6 3 2\n9 -7 1\n-5 3 -1\n-1 9 8\n-7 10 7\n-5 9 8", "output": "0.7416514696 1.6715998752 2.8248709447" }, { "input": "9\n-1 -7 4\n8 -1 -1\n5 5 4\n-4 6 6\n-10 -10 3\n-3 -8 7\n5 -8 -1\n0 4 3\n7 2 -4", "output": "-1.5154900846 -3.2958137613 0.6695576316" }, { "input": "10\n1 8 7\n0 10 3\n-9 -4 7\n9 0 1\n10 -8 7\n3 10 10\n7 0 -10\n5 -7 4\n-5 -3 8\n7 -7 8", "output": "1.8296283657 0.3157346029 1.7080583631" }, { "input": "1\n1 2 3", "output": "1.0000000050 1.9999999972 2.9999999886" }, { "input": "1\n10000 10000 10000", "output": "9999.9999999642 9999.9999999642 9999.9999999642" }, { "input": "2\n0 0 0\n10 10 10", "output": "5.0000195287 5.0000055246 4.9999749492" }, { "input": "3\n-2 5 7\n5 9 1\n-4 6 -2", "output": "-0.0866477656 6.7666194226 1.9933238886" }, { "input": "3\n-5 -5 -5\n1 1 1\n5 5 5", "output": "-0.0001166594 0.0000667381 0.0000499248" }, { "input": "4\n-1 -1 -1\n1 -1 1\n-2 1 -3\n-3 2 1", "output": "-1.0411732147 0.4451023784 -0.3715688976" }, { "input": "4\n0 1 2\n0 -3 4\n0 -2 -1\n0 2 -1", "output": "0.0000000017 -0.5000034057 1.4999965947" }, { "input": "4\n-1 -2 -3\n0 0 0\n3 6 9\n10 20 30", "output": "4.4997882318 8.9999640024 13.5000945803" }, { "input": "4\n10000 10000 10000\n-10000 -10000 10000\n-10000 10000 -10000\n10000 -10000 -10000", "output": "-0.0000000017 0.0000000017 0.0000000017" }, { "input": "8\n10000 10000 10000\n10000 10000 -10000\n10000 -10000 10000\n10000 -10000 -10000\n-10000 10000 10000\n-10000 10000 -10000\n-10000 -10000 10000\n-10000 -10000 -10000", "output": "0.0000000017 0.0000000017 0.0000000017" }, { "input": "20\n-60 50 -44\n-19 -98 -35\n-56 14 -81\n39 -71 58\n-48 -79 -62\n-56 14 -26\n-10 -100 84\n-13 -68 -96\n-50 39 -36\n-78 48 -73\n-52 62 72\n21 65 59\n26 27 82\n41 -85 1\n14 -61 -1\n-21 90 -47\n100 88 -97\n75 -41 38\n7 60 2\n-71 -57 -90", "output": "37.6686812764 -7.9351947681 -12.9655341227" }, { "input": "20\n-9983 -1468 -7570\n-5049 -6137 -3828\n-7892 7849 -5065\n4129 8135 8447\n-2950 -7915 -8406\n5062 7012 -1371\n7408 8618 348\n5524 -4358 5181\n1426 3793 -891\n-4306 654 4011\n4668 350 4864\n-8488 -2135 62\n4016 5236 8201\n3586 -7394 4239\n-1477 -8231 -4024\n1570 323 7190\n-3508 499 -1993\n-9038 1250 9631\n193 5533 5534\n-853 -4492 3715", "output": "-909.8740860117 269.7743663381 498.1414036099" }, { "input": "66\n2 2 3\n2 2 6\n2 2 9\n2 2 12\n2 4 3\n2 4 6\n2 4 9\n2 4 12\n2 6 3\n2 6 6\n2 6 9\n2 6 12\n2 8 3\n2 8 6\n2 8 9\n2 8 12\n4 2 3\n4 2 6\n4 2 9\n4 2 12\n4 4 3\n4 4 6\n4 4 9\n4 4 12\n4 6 3\n4 6 6\n4 6 9\n4 6 12\n4 8 3\n4 8 6\n4 8 9\n4 8 12\n6 2 3\n6 2 6\n6 2 9\n6 2 12\n6 4 3\n6 4 6\n6 4 9\n6 4 12\n6 6 3\n6 6 6\n6 6 9\n6 6 12\n6 8 3\n6 8 6\n6 8 9\n6 8 12\n8 2 3\n8 2 6\n8 2 9\n8 2 12\n8 4 3\n8 4 6\n8 4 9\n8 4 12\n8 6 3\n8 6 6\n8 6 9\n8 6 12\n8 8 3\n8 8 6\n8 8 9\n8 8 12\n10000 9658 3621\n-9986 -9887 -2873", "output": "6.9989324584 -114.4983343790 373.9982724515" } ]
2,000
10,342,400
0
8,580
365
The Fibonacci Segment
[ "implementation" ]
null
null
You have array *a*1,<=*a*2,<=...,<=*a**n*. Segment [*l*,<=*r*] (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) is good if *a**i*<==<=*a**i*<=-<=1<=+<=*a**i*<=-<=2, for all *i* (*l*<=+<=2<=≤<=*i*<=≤<=*r*). Let's define *len*([*l*,<=*r*])<==<=*r*<=-<=*l*<=+<=1, *len*([*l*,<=*r*]) is the length of the segment [*l*,<=*r*]. Segment [*l*1,<=*r*1], is longer than segment [*l*2,<=*r*2], if *len*([*l*1,<=*r*1])<=&gt;<=*len*([*l*2,<=*r*2]). Your task is to find a good segment of the maximum length in array *a*. Note that a segment of length 1 or 2 is always good.
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements in the array. The second line contains integers: *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=109).
Print the length of the longest good segment in array *a*.
[ "10\n1 2 3 5 8 13 21 34 55 89\n", "5\n1 1 1 1 1\n" ]
[ "10\n", "2\n" ]
none
[ { "input": "10\n1 2 3 5 8 13 21 34 55 89", "output": "10" }, { "input": "5\n1 1 1 1 1", "output": "2" }, { "input": "1\n1000", "output": "1" }, { "input": "51\n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "50" }, { "input": "1\n0", "output": "1" }, { "input": "2\n0 0", "output": "2" }, { "input": "3\n0 0 0", "output": "3" }, { "input": "4\n0 0 0 0", "output": "4" }, { "input": "5\n0 0 0 0 0", "output": "5" }, { "input": "6\n10 20 30 10 40 50", "output": "4" }, { "input": "5\n8 9 17 26 43", "output": "5" }, { "input": "12\n1 2 3 5 8 13 0 1 1 2 3 5", "output": "6" }, { "input": "13\n1 2 3 5 8 13 7 0 1 1 2 3 5", "output": "6" }, { "input": "2\n1 3", "output": "2" }, { "input": "2\n7 1", "output": "2" } ]
93
1,331,200
0
8,590
245
Internet Address
[ "implementation", "strings" ]
null
null
Vasya is an active Internet user. One day he came across an Internet resource he liked, so he wrote its address in the notebook. We know that the address of the written resource has format: where: - &lt;protocol&gt; can equal either "http" (without the quotes) or "ftp" (without the quotes), - &lt;domain&gt; is a non-empty string, consisting of lowercase English letters, - the /&lt;context&gt; part may not be present. If it is present, then &lt;context&gt; is a non-empty string, consisting of lowercase English letters. If string &lt;context&gt; isn't present in the address, then the additional character "/" isn't written. Thus, the address has either two characters "/" (the ones that go before the domain), or three (an extra one in front of the context). When the boy came home, he found out that the address he wrote in his notebook had no punctuation marks. Vasya must have been in a lot of hurry and didn't write characters ":", "/", ".". Help Vasya to restore the possible address of the recorded Internet resource.
The first line contains a non-empty string that Vasya wrote out in his notebook. This line consists of lowercase English letters only. It is guaranteed that the given string contains at most 50 letters. It is guaranteed that the given string can be obtained from some correct Internet resource address, described above.
Print a single line — the address of the Internet resource that Vasya liked. If there are several addresses that meet the problem limitations, you are allowed to print any of them.
[ "httpsunrux\n", "ftphttprururu\n" ]
[ "http://sun.ru/x\n", "ftp://http.ru/ruru\n" ]
In the second sample there are two more possible answers: "ftp://httpruru.ru" and "ftp://httpru.ru/ru".
[ { "input": "httpsunrux", "output": "http://sun.ru/x" }, { "input": "ftphttprururu", "output": "ftp://http.ru/ruru" }, { "input": "httpuururrururruruurururrrrrurrurrurruruuruuu", "output": "http://uu.ru/rrururruruurururrrrrurrurrurruruuruuu" }, { "input": "httpabuaruauabbaruru", "output": "http://abua.ru/auabbaruru" }, { "input": "httpuurrruurruuruuruuurrrurururuurruuuuuuruurr", "output": "http://uurr.ru/urruuruuruuurrrurururuurruuuuuuruurr" }, { "input": "httpruhhphhhpuhruruhhpruhhphruhhru", "output": "http://ruhhphhhpuh.ru/ruhhpruhhphruhhru" }, { "input": "httpftprftprutprururftruruftptp", "output": "http://ftprftp.ru/tprururftruruftptp" }, { "input": "httpfttpftpfttftpftpftppfrurururu", "output": "http://fttpftpfttftpftpftppf.ru/rururu" }, { "input": "httpruhttttpruhttprupruhttpruhtturuhttphtruuru", "output": "http://ruhttttp.ru/httprupruhttpruhtturuhttphtruuru" }, { "input": "httpsjkazaaghasjkasjkabruru", "output": "http://sjkazaaghasjkasjkab.ru/ru" }, { "input": "httpftphttptphttphrururuhpftphtpftphtpftphtptpft", "output": "http://ftphttptphttph.ru/ruruhpftphtpftphtpftphtptpft" }, { "input": "httpppppru", "output": "http://pppp.ru" }, { "input": "ftprrurururrurururuurrururruuru", "output": "ftp://r.ru/rururrurururuurrururruuru" }, { "input": "ftpabaruru", "output": "ftp://aba.ru/ru" }, { "input": "ftpruurruurururururuuruuur", "output": "ftp://ruur.ru/urururururuuruuur" }, { "input": "ftphhphruhhpruhhpuhhpuruhhphruhhruhhpuhhru", "output": "ftp://hhph.ru/hhpruhhpuhhpuruhhphruhhruhhpuhhru" }, { "input": "ftparua", "output": "ftp://a.ru/a" }, { "input": "httpzru", "output": "http://z.ru" }, { "input": "httprrur", "output": "http://r.ru/r" }, { "input": "ftprru", "output": "ftp://r.ru" } ]
124
307,200
3
8,627
718
Efim and Strange Grade
[ "dp", "implementation", "math" ]
null
null
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 the decimal point (also, he can ask to round to the nearest integer). There are *t* seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than *t* seconds. Note, that he can choose to not use all *t* seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the *n*-th digit one has to take a look at the digit *n*<=+<=1. If it is less than 5 than the *n*-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the *n*<=+<=1 digit is greater or equal to 5, the digit at the position *n* is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3.
The first line of the input contains two integers *n* and *t* (1<=≤<=*n*<=≤<=200<=000, 1<=≤<=*t*<=≤<=109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0.
Print the maximum grade that Efim can get in *t* seconds. Do not print trailing zeroes.
[ "6 1\n10.245\n", "6 2\n10.245\n", "3 100\n9.2\n" ]
[ "10.25\n", "10.3\n", "9.2\n" ]
In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
[ { "input": "6 1\n10.245", "output": "10.25" }, { "input": "6 2\n10.245", "output": "10.3" }, { "input": "3 100\n9.2", "output": "9.2" }, { "input": "12 5\n872.04488525", "output": "872.1" }, { "input": "35 8\n984227318.2031144444444444494637612", "output": "984227318.2031144445" }, { "input": "320 142\n2704701300865535.432223312233434114130011113220102420131323010344144201124303144444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444447444444444444444444444444444444615444444482101673308979557675074444444444444446867245414595534444693160202254444449544495367", "output": "2704701300865535.4322233122334341141300111132201024201313230103441442011243032" }, { "input": "5 10\n1.555", "output": "2" }, { "input": "6 1\n0.9454", "output": "1" }, { "input": "7 1000000000\n239.923", "output": "240" }, { "input": "7 235562\n999.999", "output": "1000" }, { "input": "9 2\n23999.448", "output": "23999.5" }, { "input": "9 3\n23999.448", "output": "24000" }, { "input": "13 1\n761.044449428", "output": "761.04445" }, { "input": "3 1\n0.1", "output": "0.1" }, { "input": "3 1\n9.9", "output": "10" }, { "input": "3 1\n0.9", "output": "1" }, { "input": "31 15\n2707786.24030444444444444724166", "output": "2707786.24031" }, { "input": "4 100\n99.9", "output": "100" }, { "input": "3 10\n9.9", "output": "10" }, { "input": "22 100\n11111111111111111111.5", "output": "11111111111111111112" }, { "input": "3 1\n9.5", "output": "10" }, { "input": "8 100\n9.444445", "output": "10" }, { "input": "6 2\n999.45", "output": "1000" }, { "input": "3 100\n9.9", "output": "10" }, { "input": "18 100\n9.4444444444454444", "output": "10" }, { "input": "16 999\n9595959.95959595", "output": "9595960" }, { "input": "4 100\n99.5", "output": "100" }, { "input": "5 1\n999.9", "output": "1000" }, { "input": "4 1\n5.59", "output": "6" }, { "input": "4 1\n99.5", "output": "100" }, { "input": "4 1\n99.9", "output": "100" }, { "input": "18 6\n102345678999.44449", "output": "102345679000" }, { "input": "3 3\n9.9", "output": "10" }, { "input": "5 1\n99.99", "output": "100" }, { "input": "7 1\n99999.9", "output": "100000" }, { "input": "3 121\n9.9", "output": "10" }, { "input": "8 6\n9.444445", "output": "10" }, { "input": "3 100\n8.9", "output": "9" }, { "input": "10 1\n999.999999", "output": "1000" }, { "input": "5 100\n6.666", "output": "7" }, { "input": "4 100\n9.99", "output": "10" }, { "input": "6 1\n9.9999", "output": "10" }, { "input": "4 10\n99.9", "output": "100" }, { "input": "5 1\n9.999", "output": "10" }, { "input": "3 1231\n9.9", "output": "10" }, { "input": "5 2\n999.9", "output": "1000" }, { "input": "5 100\n144.5", "output": "145" }, { "input": "5 100\n99.45", "output": "100" }, { "input": "10 1\n0.50444445", "output": "1" }, { "input": "7 1\n1.51111", "output": "2" }, { "input": "5 1\n199.9", "output": "200" }, { "input": "3 100\n9.5", "output": "10" }, { "input": "7 1000\n409.659", "output": "410" }, { "input": "4 10\n99.5", "output": "100" }, { "input": "4 10\n10.9", "output": "11" }, { "input": "4 1\n19.5", "output": "20" } ]
46
0
-1
8,629
962
Students in Railway Carriage
[ "constructive algorithms", "greedy", "implementation" ]
null
null
There are $n$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of $a$ student-programmers and $b$ student-athletes. Determine the largest number of students from all $a+b$ students, which you can put in the railway carriage so that: - no student-programmer is sitting next to the student-programmer; - and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).
The first line contain three integers $n$, $a$ and $b$ ($1 \le n \le 2\cdot10^{5}$, $0 \le a, b \le 2\cdot10^{5}$, $a + b &gt; 0$) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length $n$, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.
Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.
[ "5 1 1\n*...*\n", "6 2 3\n*...*.\n", "11 3 10\n.*....**.*.\n", "3 2 3\n***\n" ]
[ "2\n", "4\n", "7\n", "0\n" ]
In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete.
[ { "input": "5 1 1\n*...*", "output": "2" }, { "input": "6 2 3\n*...*.", "output": "4" }, { "input": "11 3 10\n.*....**.*.", "output": "7" }, { "input": "3 2 3\n***", "output": "0" }, { "input": "9 5 3\n*...*...*", "output": "6" }, { "input": "9 2 4\n*...*...*", "output": "6" }, { "input": "9 2 200000\n*...*...*", "output": "6" }, { "input": "1 0 1\n.", "output": "1" }, { "input": "14 3 7\n.*.......*..*.", "output": "10" }, { "input": "6 1 3\n*....*", "output": "3" }, { "input": "5 1 2\n...*.", "output": "3" }, { "input": "2 2 0\n..", "output": "1" }, { "input": "2 0 2\n..", "output": "1" }, { "input": "5 3 3\n...**", "output": "3" }, { "input": "3 0 1\n.*.", "output": "1" }, { "input": "13 3 3\n*...*...*...*", "output": "6" }, { "input": "7 0 4\n...*..*", "output": "3" }, { "input": "20 5 5\n.*.*.............*..", "output": "10" }, { "input": "64 59 2\n.*.***......****.*..**..**..****.*.*.*.**...**..***.***.*..*..*.", "output": "23" }, { "input": "2 1 1\n..", "output": "2" }, { "input": "10 7 0\n.*...*..*.", "output": "5" }, { "input": "4 1 1\n..*.", "output": "2" }, { "input": "10 4 3\n.*..*...*.", "output": "7" }, { "input": "5 0 1\n*.*.*", "output": "1" }, { "input": "6 1 1\n*...*.", "output": "2" }, { "input": "1 1 1\n.", "output": "1" }, { "input": "11 6 2\n.*...*...*.", "output": "8" }, { "input": "11 7 1\n.*...*...*.", "output": "7" } ]
265
8,396,800
0
8,633
0
none
[ "none" ]
null
null
Fox Ciel is participating in a party in Prime Kingdom. There are *n* foxes there (include Fox Ciel). The i-th fox is *a**i* years old. They will have dinner around some round tables. You want to distribute foxes such that: 1. Each fox is sitting at some table. 1. Each table has at least 3 foxes sitting around it. 1. The sum of ages of any two adjacent foxes around each table should be a prime number. If *k* foxes *f*1, *f*2, ..., *f**k* are sitting around table in clockwise order, then for 1<=≤<=*i*<=≤<=*k*<=-<=1: *f**i* and *f**i*<=+<=1 are adjacent, and *f*1 and *f**k* are also adjacent. If it is possible to distribute the foxes in the desired manner, find out a way to do that.
The first line contains single integer *n* (3<=≤<=*n*<=≤<=200): the number of foxes in this party. The second line contains *n* integers *a**i* (2<=≤<=*a**i*<=≤<=104).
If it is impossible to do this, output "Impossible". Otherwise, in the first line output an integer *m* (): the number of tables. Then output *m* lines, each line should start with an integer *k* -=– the number of foxes around that table, and then *k* numbers — indices of fox sitting around that table in clockwise order. If there are several possible arrangements, output any of them.
[ "4\n3 4 8 9\n", "5\n2 2 2 2 2\n", "12\n2 3 4 5 6 7 8 9 10 11 12 13\n", "24\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25\n" ]
[ "1\n4 1 2 4 3\n", "Impossible\n", "1\n12 1 2 3 6 5 12 9 8 7 10 11 4\n", "3\n6 1 2 3 6 5 4\n10 7 8 9 12 15 14 13 16 11 10\n8 17 18 23 22 19 20 21 24\n" ]
In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes. In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number.
[ { "input": "4\n3 4 8 9", "output": "1\n4 1 2 4 3" }, { "input": "5\n2 2 2 2 2", "output": "Impossible" }, { "input": "12\n2 3 4 5 6 7 8 9 10 11 12 13", "output": "1\n12 1 2 3 6 5 12 9 8 7 10 11 4" }, { "input": "24\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25", "output": "3\n6 1 2 3 6 5 4\n10 7 8 9 12 15 14 13 16 11 10\n8 17 18 23 22 19 20 21 24" }, { "input": "4\n2 2 9973 9967", "output": "Impossible" }, { "input": "30\n2 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", "output": "3\n16 1 2 3 26 19 22 23 28 29 30 27 24 21 20 25 4\n6 5 6 9 8 7 10\n8 11 16 13 14 15 12 17 18" }, { "input": "20\n76 38 74 176 106 134 12 88 66 178 63 105 199 99 29 67 135 29 101 47", "output": "Impossible" }, { "input": "20\n12 4 12 12 2 10 4 12 18 14 21 21 15 7 17 11 5 11 3 13", "output": "3\n6 1 14 3 16 4 15\n10 2 13 10 17 8 18 9 20 7 19\n4 5 11 6 12" }, { "input": "152\n29 23 17 25 13 29 29 29 25 23 25 29 19 25 13 25 13 23 21 27 15 29 29 25 27 17 17 19 25 19 13 19 15 13 19 13 17 17 19 17 17 13 25 21 17 13 21 17 25 21 19 23 17 17 29 15 15 17 25 13 25 13 21 13 19 19 13 13 21 25 23 19 19 21 29 29 26 30 22 20 22 28 24 28 18 16 22 18 16 20 12 26 16 20 12 24 20 28 16 16 16 16 12 20 22 12 20 12 22 18 22 12 22 22 24 22 30 28 20 24 30 14 18 12 16 14 18 18 16 22 16 20 20 20 28 30 20 24 12 24 24 28 22 30 24 18 12 20 22 24 12 12", "output": "17\n30 1 126 45 122 41 120 42 121 46 123 51 124 48 77 58 134 63 137 69 143 66 142 65 141 64 140 62 138 60 136\n40 2 80 3 92 74 149 73 81 4 82 5 84 9 86 11 87 13 91 14 89 15 88 18 90 10 85 12 83 8 78 7 150 6 147 72 79 70 146 71 148\n12 16 93 17 98 21 99 24 103 23 96 22 95\n4 19 94 20 97\n4 25 100 31 101\n4 26 104 27 107\n4 28 105 29 106\n18 30 108 37 112 38 117 40 115 39 118 43 114 35 113 33 111 32 109\n4 34 102 36 110\n4 44 116 47 119\n4 49 125 50 129\n4 52 127 55 128\n4 53 132 54 133\n4 56 130 57 131\n4 5..." }, { "input": "92\n5 5 3 5 3 3 5 3 5 3 5 5 5 3 3 5 3 5 3 5 3 5 3 5 3 3 3 5 3 5 5 5 5 5 5 3 5 3 3 5 3 5 5 3 3 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4", "output": "Impossible" }, { "input": "15\n3 3 3 3 3 3 3 4 2 4 2 2 2 4 2", "output": "Impossible" }, { "input": "88\n29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 28 28 30 30 28 28 30 28 28 28 30 30 30 30 28 30 30 28 28 28 30 28 30 30 30 30 28 30 30 30 28 30 28 28 28 30 30 30 30 28 30 28 30 28", "output": "Impossible" }, { "input": "52\n11 33 37 51 27 59 57 55 73 67 13 47 45 39 27 21 23 61 37 35 39 63 69 53 61 55 44 34 64 30 54 48 32 66 32 62 50 44 38 24 22 30 14 54 12 28 40 40 50 54 64 56", "output": "4\n10 1 33 4 52 2 28 22 51 21 49\n10 3 29 10 30 6 32 8 31 9 34\n28 5 35 7 37 14 38 15 43 17 27 24 50 26 46 18 45 20 44 12 40 11 42 19 41 16 39 13 36\n4 23 47 25 48" }, { "input": "102\n87 73 87 81 71 83 71 91 75 87 87 79 77 85 83 71 91 83 85 81 79 81 81 91 91 87 79 81 91 81 77 87 71 87 91 89 89 77 87 91 87 75 83 87 75 73 83 81 79 77 91 76 76 88 82 88 78 86 72 84 86 72 74 74 88 84 86 80 84 90 80 88 84 82 80 84 74 72 86 86 76 82 80 86 74 84 88 74 82 90 72 86 72 80 80 82 86 88 82 78 72 88", "output": "8\n4 1 94 44 95\n72 2 60 49 102 51 101 50 63 9 64 13 68 11 58 20 55 4 53 10 52 3 97 5 57 7 100 46 90 43 88 37 86 36 85 31 79 28 74 25 78 27 87 29 82 30 89 35 91 38 93 40 96 48 99 45 98 42 56 21 54 24 72 19 65 17 70 47 73 15 77 18 76\n4 6 66 12 69\n4 8 59 14 62\n4 16 71 26 75\n4 22 61 23 67\n6 32 80 33 83 34 81\n4 39 84 41 92" }, { "input": "10\n119 289 109 185 251 184 224 588 360 518", "output": "Impossible" }, { "input": "76\n7 7 9 9 9 11 9 11 7 7 9 7 9 9 9 7 11 11 7 11 7 11 7 7 9 11 7 7 7 7 11 7 9 11 11 9 9 11 8 10 8 8 8 10 10 10 10 8 8 8 8 10 10 10 8 8 8 10 8 8 8 8 8 8 10 8 8 10 10 10 10 10 8 10 10 10", "output": "9\n4 1 40 2 76\n4 3 41 4 42\n44 5 43 6 48 7 49 8 50 11 51 13 55 14 56 15 57 17 59 18 60 20 61 22 62 26 63 25 65 24 58 23 54 21 53 19 52 16 47 12 46 10 45 9 44\n4 27 68 28 69\n4 29 70 30 71\n4 31 64 34 66\n4 32 72 33 74\n4 35 67 38 73\n4 36 39 37 75" }, { "input": "12\n1751 1909 1655 1583 1867 1841 1740 1584 1518 1806 1664 1518", "output": "Impossible" }, { "input": "146\n3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 4 2 4 2 4 2 4 2 2 2 4 2 4 2 4 4 2 4 4 2 2 4 2 2 2 4 4 2 2 2 2 2 2 2 4 4 4 4 4 2 2 4 2 2 2 2 4 4 2 4 4 2 2 2 2 2 2 4 4 4 4 4 4 2 2 2 2 2 2 4 4 4", "output": "36\n6 1 74 73 145 72 146\n4 2 75 3 76\n4 4 77 5 78\n4 6 79 7 80\n4 8 81 9 82\n4 10 83 11 84\n4 12 85 13 86\n4 14 87 15 88\n4 16 89 17 90\n4 18 91 19 92\n4 20 93 21 94\n4 22 95 23 96\n4 24 97 25 98\n4 26 99 27 100\n4 28 101 29 102\n4 30 103 31 104\n4 32 105 33 106\n4 34 107 35 108\n4 36 109 37 110\n4 38 111 39 112\n4 40 113 41 114\n4 42 115 43 116\n4 44 117 45 118\n4 46 119 47 120\n4 48 121 49 122\n4 50 123 51 124\n4 52 125 53 126\n4 54 127 55 128\n4 56 129 57 130\n4 58 131 59 132\n4 60 133 61 134\n4 62 135..." }, { "input": "78\n159 575 713 275 463 365 461 537 301 439 669 165 555 267 571 383 495 375 321 605 367 481 619 675 115 193 447 303 263 421 189 491 591 673 635 309 301 391 379 736 652 704 634 258 708 206 476 408 702 630 650 236 546 328 348 86 96 628 668 426 640 170 434 486 168 640 260 426 186 272 650 616 252 372 442 178 266 464", "output": "1\n78 1 42 2 69 30 66 27 63 24 58 17 59 16 53 15 55 23 61 14 52 19 62 11 54 13 47 12 77 32 74 4 49 7 50 10 41 37 75 39 76 38 73 35 70 33 40 34 72 28 71 31 78 36 67 29 44 3 65 25 68 26 60 22 64 21 57 20 56 18 43 5 45 9 48 6 46 8 51" }, { "input": "10\n5 5 7 7 5 6 6 6 6 6", "output": "2\n6 1 6 5 9 4 10\n4 2 7 3 8" }, { "input": "148\n73 53 49 49 65 69 61 67 57 55 53 57 57 59 69 59 71 55 71 49 51 67 57 73 71 55 59 59 61 55 73 69 63 55 59 51 69 73 67 55 61 53 49 69 53 63 71 71 65 63 61 63 65 69 61 63 63 71 71 65 57 63 61 69 49 53 59 51 73 61 55 73 63 65 70 68 68 66 64 56 68 50 68 56 68 70 68 54 70 60 62 68 64 56 52 66 66 64 72 58 70 58 52 50 56 50 56 50 50 72 70 64 50 62 58 70 72 62 62 72 64 52 50 54 56 54 72 64 62 62 72 70 66 70 62 64 50 72 62 58 58 58 56 72 58 52 60 72", "output": "13\n76 1 133 63 134 70 75 7 78 5 139 60 138 67 83 64 81 54 135 53 131 49 130 44 129 37 119 36 118 32 114 21 95 23 101 22 99 27 104 28 106 33 108 42 109 46 112 38 115 34 117 35 110 30 103 29 111 41 116 51 122 55 132 61 125 59 76 62 137 73 77 57 123 56 128 3 136\n4 2 80 9 82\n26 4 79 8 90 11 84 12 86 13 89 15 91 6 85 14 87 16 88 18 146 71 148 74 144 10 140\n4 17 92 19 94\n4 20 93 24 98\n4 25 96 47 97\n4 26 100 31 102\n4 39 120 40 127\n4 43 124 45 126\n4 48 105 58 107\n4 50 113 52 121\n6 65 141 68 143 66 147\n..." }, { "input": "80\n5599 5365 6251 3777 6887 5077 4987 6925 3663 5457 5063 4077 3531 6359 4293 6305 4585 3641 6737 6403 6863 4839 3765 3767 5807 6657 7275 5625 3635 3939 7035 6945 7167 5023 5949 4295 4899 4595 5725 3863 3750 4020 5096 5232 6566 6194 5524 3702 6876 4464 3720 5782 5160 3712 7028 6204 5378 5896 5494 7084 5290 6784 6408 5410 4260 5082 4210 5336 4110 5064 3664 4964 5202 5410 5634 3990 5034 6774 4956 4806", "output": "5\n40 1 41 21 77 6 49 7 42 3 44 5 50 2 79 38 78 36 75 34 76 24 73 29 46 31 57 27 58 26 62 28 72 32 68 33 43 18 80 39 70\n28 4 45 40 56 19 69 20 65 25 66 8 48 16 63 17 59 22 60 12 54 23 52 10 74 30 64 13 61\n4 9 47 15 55\n4 11 51 14 53\n4 35 67 37 71" }, { "input": "16\n5 7 7 7 11 11 9 5 4 6 6 10 6 4 10 6", "output": "4\n4 1 10 5 11\n4 2 9 3 12\n4 4 14 7 15\n4 6 13 8 16" }, { "input": "74\n3 3 5 3 5 5 3 5 3 3 5 5 3 5 3 3 3 3 3 3 3 5 5 3 5 3 5 3 3 5 5 5 5 3 3 5 3 4 6 6 6 6 4 4 4 6 6 6 6 4 6 4 4 6 6 4 6 4 4 6 6 4 4 4 6 4 4 4 4 6 4 4 4 4", "output": "18\n6 1 38 37 73 35 74\n4 2 43 4 44\n4 3 39 5 40\n4 6 41 8 42\n4 7 45 9 50\n4 10 52 13 53\n4 11 46 12 47\n4 14 48 22 49\n4 15 56 16 58\n4 17 59 18 62\n4 19 63 20 64\n4 21 66 24 67\n4 23 51 25 54\n4 26 68 28 69\n4 27 55 30 57\n4 29 71 34 72\n4 31 60 32 61\n4 33 65 36 70" }, { "input": "70\n763 657 799 713 667 531 829 675 799 721 741 549 793 553 723 579 853 713 835 833 581 801 683 551 617 733 611 699 607 565 579 693 897 543 607 848 774 602 544 846 710 722 568 740 548 702 908 572 572 806 834 794 648 770 908 778 748 692 704 624 580 746 780 666 678 822 834 640 548 788", "output": "4\n6 1 57 26 67 30 64\n6 2 48 6 49 16 52\n52 3 53 20 63 5 60 19 56 13 51 25 50 8 47 18 54 33 59 31 70 32 62 34 69 23 36 21 42 22 58 11 45 12 44 4 41 15 43 10 46 7 39 14 40 17 37 35 61 29 66 9 68\n6 24 55 28 38 27 65" }, { "input": "98\n5 5 3 3 3 3 3 5 3 5 3 5 3 3 5 5 5 5 3 5 5 3 3 5 3 3 5 3 3 3 5 5 3 5 3 3 3 5 5 5 3 5 5 5 3 5 5 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4", "output": "Impossible" }, { "input": "30\n25 43 41 17 15 29 29 39 17 19 23 9 39 19 25 26 32 38 12 42 44 44 12 22 26 20 34 12 30 16", "output": "1\n30 1 20 6 19 4 16 9 25 5 18 3 17 13 26 12 24 10 23 7 21 8 22 11 29 2 30 15 28 14 27" }, { "input": "90\n11 9 11 9 9 11 9 9 11 9 11 9 11 11 9 11 11 11 11 9 9 11 11 11 9 9 9 11 11 9 11 11 9 11 9 9 11 11 11 11 9 11 11 11 11 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10", "output": "Impossible" }, { "input": "6\n681 673 659 656 650 644", "output": "Impossible" }, { "input": "38\n5 7 7 5 7 7 7 5 7 5 7 5 7 5 7 7 5 7 7 4 6 4 8 4 4 8 4 8 4 6 6 8 6 8 6 4 8 6", "output": "1\n38 1 23 4 26 8 28 10 32 12 34 14 37 17 35 16 33 15 31 13 30 11 29 9 27 7 25 6 24 5 22 3 21 2 20 19 36 18 38" }, { "input": "81\n7627 7425 8929 7617 5649 7853 4747 6267 4997 6447 5411 7707 5169 5789 8011 9129 8045 7463 6139 8263 7547 7453 7993 8343 5611 7039 9001 5569 9189 7957 5537 8757 8795 4963 9149 5845 9203 5459 8501 7273 9152 7472 8050 8568 6730 8638 4938 9000 9230 5464 5950 6090 7394 5916 4890 6246 4816 4920 8638 4706 6308 6816 7570 8940 5060 7368 5252 6526 9072 5168 7420 5336 4734 8076 7048 8504 5696 9266 8966 7416 5162", "output": "Impossible" }, { "input": "98\n575 581 569 571 571 583 573 581 569 589 579 575 575 577 585 569 569 571 581 577 583 573 575 589 585 569 579 585 585 579 579 577 575 575 577 585 583 569 571 589 571 583 569 587 575 585 585 583 581 572 568 568 576 580 582 570 576 580 582 588 572 584 576 580 576 582 568 574 588 580 572 586 568 574 578 568 568 584 576 588 588 574 578 586 588 570 568 568 568 580 586 576 574 586 582 584 570 572", "output": "11\n32 1 60 42 51 21 89 37 88 36 87 29 77 28 76 25 52 48 67 6 73 15 72 20 79 14 53 34 85 45 92 44 63\n10 2 56 8 50 11 61 19 86 49 97\n14 3 59 26 62 17 55 16 96 43 95 9 78 38 66\n10 4 64 18 58 41 70 39 54 5 90\n4 7 75 22 83\n4 10 82 40 93\n4 12 65 13 69\n4 23 80 33 81\n8 24 68 31 98 30 71 27 74\n4 32 57 35 84\n4 46 91 47 94" }, { "input": "124\n135 161 147 135 137 153 145 159 147 129 131 157 163 161 127 129 141 133 133 151 147 169 159 137 137 153 165 137 139 151 149 161 157 149 147 139 145 129 159 155 133 129 139 151 155 145 135 155 135 137 157 141 169 151 163 151 159 129 171 169 129 159 154 142 158 152 172 142 172 164 142 158 156 128 144 128 140 160 154 144 126 140 166 134 146 148 130 166 160 168 172 138 148 126 138 144 156 130 172 130 164 136 130 132 142 126 138 164 158 154 166 160 164 168 128 160 162 168 158 172 150 130 132 172", "output": "9\n88 1 68 56 64 54 117 60 118 53 114 46 111 3 70 9 122 55 121 51 116 59 112 44 104 50 106 45 65 40 95 37 99 39 93 27 91 23 86 22 92 29 98 30 94 33 97 2 123 5 75 13 63 12 81 24 82 25 84 28 85 32 73 14 66 11 107 43 110 42 108 38 101 35 100 36 103 41 102 47 105 52 67 7 69 8 72 4 71\n4 6 74 10 76\n6 15 79 16 77 17 83\n4 18 78 19 80\n6 20 87 26 89 21 88\n4 31 90 34 96\n4 48 109 49 119\n4 57 120 62 124\n4 58 113 61 115" }, { "input": "60\n9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 8 10 10 10 10 8 10 10 8 10 8 8 10 8 8 10 10 10 8 8 8 8 10 8 10 8 8 8 8 10", "output": "15\n4 1 31 2 32\n4 3 33 4 34\n4 5 35 6 36\n4 7 37 8 38\n4 9 39 10 40\n4 11 41 12 42\n4 13 43 14 44\n4 15 45 16 46\n4 17 47 18 48\n4 19 49 20 50\n4 21 51 22 52\n4 23 53 24 54\n4 25 55 26 56\n4 27 57 28 58\n4 29 59 30 60" }, { "input": "62\n37 45 41 45 49 37 47 41 39 43 43 39 45 41 43 47 37 41 47 37 47 49 43 39 37 45 45 47 37 47 43 34 42 36 48 36 44 48 44 46 48 44 44 48 36 42 40 38 36 48 48 38 46 48 34 34 46 42 34 36 34 36", "output": "Impossible" }, { "input": "128\n3 3 5 3 5 3 5 3 5 5 3 5 3 5 3 5 3 5 5 5 5 5 5 5 5 3 3 3 5 3 5 3 3 3 3 5 3 5 5 3 3 3 3 5 5 5 5 3 5 3 3 5 5 3 5 3 3 5 3 3 5 3 3 3 6 6 6 4 4 4 4 4 6 6 6 6 6 6 4 6 6 4 6 6 4 4 4 6 4 6 6 4 6 4 4 6 4 4 6 4 6 4 6 6 6 6 6 6 4 6 4 6 6 4 4 6 4 6 6 4 6 4 6 4 6 6 4 6", "output": "Impossible" }, { "input": "60\n633 713 645 745 641 685 731 645 655 633 703 715 633 739 657 755 657 671 567 699 743 737 667 701 649 721 671 699 697 675 570 570 570 648 684 732 598 558 674 766 720 692 702 756 756 646 568 630 668 742 604 628 628 764 636 600 678 734 638 758", "output": "3\n48 1 49 13 47 14 52 20 51 6 55 27 60 30 42 19 46 17 50 15 40 12 36 24 38 9 34 5 41 11 37 10 39 3 54 8 59 21 48 22 45 18 44 16 57 25 53 28 58\n4 2 32 23 33\n8 4 35 29 56 26 31 7 43" }, { "input": "178\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6", "output": "44\n6 1 90 89 177 88 178\n4 2 91 3 92\n4 4 93 5 94\n4 6 95 7 96\n4 8 97 9 98\n4 10 99 11 100\n4 12 101 13 102\n4 14 103 15 104\n4 16 105 17 106\n4 18 107 19 108\n4 20 109 21 110\n4 22 111 23 112\n4 24 113 25 114\n4 26 115 27 116\n4 28 117 29 118\n4 30 119 31 120\n4 32 121 33 122\n4 34 123 35 124\n4 36 125 37 126\n4 38 127 39 128\n4 40 129 41 130\n4 42 131 43 132\n4 44 133 45 134\n4 46 135 47 136\n4 48 137 49 138\n4 50 139 51 140\n4 52 141 53 142\n4 54 143 55 144\n4 56 145 57 146\n4 58 147 59 148\n4 60 149 ..." } ]
46
0
0
8,634
958
Maximum Control (easy)
[ "implementation" ]
null
null
The Resistance is trying to take control over all planets in a particular solar system. This solar system is shaped like a tree. More precisely, some planets are connected by bidirectional hyperspace tunnels in such a way that there is a path between every pair of the planets, but removing any tunnel would disconnect some of them. The Resistance already has measures in place that will, when the time is right, enable them to control every planet that is not remote. A planet is considered to be remote if it is connected to the rest of the planets only via a single hyperspace tunnel. How much work is there left to be done: that is, how many remote planets are there?
The first line of the input contains an integer *N* (2<=≤<=*N*<=≤<=1000) – the number of planets in the galaxy. The next *N*<=-<=1 lines describe the hyperspace tunnels between the planets. Each of the *N*<=-<=1 lines contains two space-separated integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*N*) indicating that there is a bidirectional hyperspace tunnel between the planets *u* and *v*. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets.
A single integer denoting the number of remote planets.
[ "5\n4 1\n4 2\n1 3\n1 5\n", "4\n1 2\n4 3\n1 4\n" ]
[ "3\n", "2\n" ]
In the first example, only planets 2, 3 and 5 are connected by a single tunnel. In the second example, the remote planets are 2 and 3. Note that this problem has only two versions – easy and medium.
[ { "input": "5\n4 1\n4 2\n1 3\n1 5", "output": "3" }, { "input": "4\n1 2\n4 3\n1 4", "output": "2" }, { "input": "10\n4 3\n2 6\n10 1\n5 7\n5 8\n10 6\n5 9\n9 3\n2 9", "output": "4" } ]
0
0
-1
8,646
376
Lever
[ "implementation", "math" ]
null
null
You have a description of a lever as string *s*. We'll represent the string length as record |*s*|, then the lever looks as a horizontal bar with weights of length |*s*|<=-<=1 with exactly one pivot. We will assume that the bar is a segment on the *Ox* axis between points 0 and |*s*|<=-<=1. The decoding of the lever description is given below. - If the *i*-th character of the string equals "^", that means that at coordinate *i* there is the pivot under the bar. - If the *i*-th character of the string equals "=", that means that at coordinate *i* there is nothing lying on the bar. - If the *i*-th character of the string equals digit *c* (1-9), that means that at coordinate *i* there is a weight of mass *c* on the bar. Your task is, given the lever description, print if it will be in balance or not. Assume that the bar doesn't weight anything. Assume that the bar initially is in balance then all weights are simultaneously put on it. After that the bar either tilts to the left, or tilts to the right, or is in balance.
The first line contains the lever description as a non-empty string *s* (3<=≤<=|*s*|<=≤<=106), consisting of digits (1-9) and characters "^" and "=". It is guaranteed that the line contains exactly one character "^". It is guaranteed that the pivot of the lever isn't located in any end of the lever bar. To solve the problem you may need 64-bit integer numbers. Please, do not forget to use them in your programs.
Print "left" if the given lever tilts to the left, "right" if it tilts to the right and "balance", if it is in balance.
[ "=^==\n", "9===^==1\n", "2==^7==\n", "41^52==\n" ]
[ "balance\n", "left\n", "right\n", "balance\n" ]
As you solve the problem, you may find the following link useful to better understand how a lever functions: http://en.wikipedia.org/wiki/Lever. The pictures to the examples:
[ { "input": "=^==", "output": "balance" }, { "input": "9===^==1", "output": "left" }, { "input": "2==^7==", "output": "right" }, { "input": "41^52==", "output": "balance" }, { "input": "=^2=4=1===1=", "output": "right" }, { "input": "9=6===5==3=9=1=1^7=1==", "output": "left" }, { "input": "85=61=36=^93===4==44==35==94===39===15===", "output": "right" }, { "input": "==88=^95==83=45===8====73===7==7====1=29====29=8=85=", "output": "right" }, { "input": "==41^52==", "output": "balance" }, { "input": "2===================^2", "output": "left" }, { "input": "9^=============1", "output": "right" }, { "input": "4=========^=55", "output": "left" }, { "input": "123^321", "output": "balance" }, { "input": "7^1=2", "output": "balance" }, { "input": "589==^==958", "output": "right" } ]
249
18,124,800
3
8,649
283
Cows and Sequence
[ "constructive algorithms", "data structures", "implementation" ]
null
null
Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform *n* operations. Each operation is one of the following: 1. Add the integer *x**i* to the first *a**i* elements of the sequence. 1. Append an integer *k**i* to the end of the sequence. (And hence the size of the sequence increases by 1) 1. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=2·105) — the number of operations. The next *n* lines describe the operations. Each line will start with an integer *t**i* (1<=≤<=*t**i*<=≤<=3), denoting the type of the operation (see above). If *t**i*<==<=1, it will be followed by two integers *a**i*,<=*x**i* (|*x**i*|<=≤<=103; 1<=≤<=*a**i*). If *t**i*<==<=2, it will be followed by a single integer *k**i* (|*k**i*|<=≤<=103). If *t**i*<==<=3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.
Output *n* lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=6.
[ "5\n2 1\n3\n2 3\n2 1\n3\n", "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3\n" ]
[ "0.500000\n0.000000\n1.500000\n1.333333\n1.500000\n", "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000\n" ]
In the second sample, the sequence becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fb5aaaa5dc516fe540cef52fd153768bfdb941c8.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "5\n2 1\n3\n2 3\n2 1\n3", "output": "0.500000\n0.000000\n1.500000\n1.333333\n1.500000" }, { "input": "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3", "output": "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000" }, { "input": "1\n1 1 1", "output": "1.000000" }, { "input": "1\n2 1", "output": "0.500000" }, { "input": "2\n2 1\n1 2 1", "output": "0.500000\n1.500000" }, { "input": "5\n2 1\n1 2 1\n2 1\n2 1\n1 2 1", "output": "0.500000\n1.500000\n1.333333\n1.250000\n1.750000" }, { "input": "5\n1 1 7\n1 1 7\n1 1 7\n2 5\n1 2 2", "output": "7.000000\n14.000000\n21.000000\n13.000000\n15.000000" }, { "input": "5\n1 1 -48\n1 1 19\n1 1 -35\n2 -67\n1 2 -13", "output": "-48.000000\n-29.000000\n-64.000000\n-65.500000\n-78.500000" }, { "input": "1\n1 1 0", "output": "0.000000" }, { "input": "1\n2 0", "output": "0.000000" }, { "input": "5\n2 -980\n1 2 -156\n2 641\n2 -253\n2 -514", "output": "-490.000000\n-646.000000\n-217.000000\n-226.000000\n-283.600000" } ]
1,500
10,547,200
0
8,660
730
Ber Patio
[]
null
null
Polycarp is a regular customer at the restaurant "Ber Patio". He likes having lunches there. "Ber Patio" has special discount program for regular customers. A customer can collect bonuses and partially cover expenses in the restaurant. Let's assume a customer currently has *b* bonuses and she has to pay *r* burles for a lunch. In this case the customer can use bonuses (1 bonus = 1 burle) to reduce the payment. She can cover at most half of the payment using bonuses. However, 1 bonus will be added to the customer's bonus balance per each 10 burles she paid. Formally: 1. a customer can choose any number *x* of bonuses to use ()), 1. the customer's bonus balance is reduced by *x*, 1. the customer pays *r*<=-<=*x* burles, 1. the customer's bonus balance is increased by ⌊(*r*<=-<=*x*)<=/<=10⌋ (i.e. integer division rounded down is used). Initially, there are *b* bonuses on Polycarp's account. Polycarp is going to have a lunch in "Ber Patio" for the next *n* days. He estimated the values *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is the number of burles in a receipt for the *i*-th day. The sum over all receipts doesn't exceed 105 burles. Write a program to find the minimum number of burles Polycarp has to spend and an optimal strategy to use bonuses.
The first line contains two integer numbers *n* and *b* (1<=≤<=*n*<=≤<=5000, 0<=≤<=*b*<=≤<=105) — number of days and initial number of bonuses Polycarp has. The second line contains the integer sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=1000), where *a**i* is the amount of burles in the *i*-th day's receipt. It is guaranteed that the sum of all receipts does not exceed 105 burles.
On the first line, print the expected minimal number of burles to pay for all *n* receipts. On the second line, print the sequence of integer numbers *b*1,<=*b*2,<=...,<=*b**n*, where *b**i* is the number of bonuses to use on the *i*-th day. If there are multiple solutions, print any of them.
[ "3 21\n12 75 52\n", "3 39\n58 64 33\n" ]
[ "110\n2 5 22 \n", "107\n28 4 16 \n" ]
none
[]
31
0
0
8,662
587
Duff in Mafia
[ "2-sat", "binary search" ]
null
null
Duff is one if the heads of Mafia in her country, Andarz Gu. Andarz Gu has *n* cities (numbered from 1 to *n*) connected by *m* bidirectional roads (numbered by 1 to *m*). Each road has a destructing time, and a color. *i*-th road connects cities *v**i* and *u**i* and its color is *c**i* and its destructing time is *t**i*. Mafia wants to destruct a matching in Andarz Gu. A matching is a subset of roads such that no two roads in this subset has common endpoint. They can destruct these roads in parallel, i. e. the total destruction time is a maximum over destruction times of all selected roads. They want two conditions to be satisfied: 1. The remaining roads form a proper coloring. 1. Destructing time of this matching is minimized. The remaining roads after destructing this matching form a proper coloring if and only if no two roads of the same color have same endpoint, or, in the other words, edges of each color should form a matching. There is no programmer in Mafia. That's why Duff asked for your help. Please help her and determine which matching to destruct in order to satisfied those conditions (or state that this is not possible).
The first line of input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=5<=×<=104 and 1<=≤<=*m*<=≤<=5<=×<=104), number of cities and number of roads in the country. The next *m* lines contain the the roads. *i*<=-<=*th* of them contains four integers *v**i*,<=*u**i*,<=*c**i* and *t**i* (1<=≤<=*v**i*,<=*u**i*<=≤<=*n*, *v**i*<=≠<=*u**i* and 1<=≤<=*c**i*,<=*t**i*<=≤<=109 for each 1<=≤<=*i*<=≤<=*m*).
In the first line of input, print "Yes" (without quotes) if satisfying the first condition is possible and "No" (without quotes) otherwise. If it is possible, then you have to print two integers *t* and *k* in the second line, the minimum destructing time and the number of roads in the matching (). In the third line print *k* distinct integers separated by spaces, indices of the roads in the matching in any order. Roads are numbered starting from one in order of their appearance in the input. If there's more than one solution, print any of them.
[ "5 7\n2 1 3 7\n3 1 1 6\n5 4 1 8\n4 5 1 1\n3 2 2 3\n4 5 2 5\n2 3 2 4\n", "3 5\n3 2 1 3\n1 3 1 1\n3 2 1 4\n1 3 2 2\n1 3 2 10\n" ]
[ "Yes\n3 2\n4 5\n", "No\n" ]
Graph of Andarz Gu in the first sample case is as follows: A solution would be to destruct the roads with crosses. Graph of Andarz Gu in the second sample case is as follows:
[]
93
2,252,800
-1
8,669
900
Remove Extra One
[ "brute force", "data structures", "math" ]
null
null
You are given a permutation *p* of length *n*. Remove one element from permutation to make the number of records the maximum possible. We remind that in a sequence of numbers *a*1,<=*a*2,<=...,<=*a**k* the element *a**i* is a record if for every integer *j* (1<=≤<=*j*<=&lt;<=*i*) the following holds: *a**j*<=&lt;<=*a**i*.
The first line contains the only integer *n* (1<=≤<=*n*<=≤<=105) — the length of the permutation. The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*) — the permutation. All the integers are distinct.
Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one.
[ "1\n1\n", "5\n5 1 2 3 4\n" ]
[ "1\n", "5\n" ]
In the first example the only element can be removed.
[ { "input": "1\n1", "output": "1" }, { "input": "5\n5 1 2 3 4", "output": "5" }, { "input": "5\n4 3 5 1 2", "output": "1" }, { "input": "9\n9 5 8 6 3 2 4 1 7", "output": "9" }, { "input": "3\n3 2 1", "output": "1" }, { "input": "7\n1 6 7 4 2 5 3", "output": "2" }, { "input": "48\n38 6 31 19 45 28 27 43 11 35 36 20 9 16 42 48 14 22 39 18 12 10 34 25 13 26 40 29 17 8 33 46 24 30 37 44 1 15 2 21 3 5 4 47 32 23 41 7", "output": "38" }, { "input": "26\n23 14 15 19 9 22 20 12 5 4 21 1 16 8 6 11 3 17 2 10 24 26 13 18 25 7", "output": "23" }, { "input": "46\n32 25 11 1 3 10 8 12 18 42 28 16 35 30 41 38 43 4 13 23 6 17 36 34 39 22 26 14 45 20 33 44 21 7 15 5 40 46 2 29 37 9 31 19 27 24", "output": "42" }, { "input": "24\n20 3 22 10 2 14 7 18 6 23 17 12 5 11 15 13 19 24 16 1 21 4 8 9", "output": "1" }, { "input": "57\n40 11 43 39 13 29 18 57 54 48 17 4 22 5 38 15 36 53 33 3 51 41 30 9 26 10 55 27 35 56 23 20 1 8 12 46 21 28 6 19 34 2 45 31 49 42 50 16 44 7 25 52 14 32 47 37 24", "output": "57" }, { "input": "85\n82 72 24 38 81 18 49 62 37 28 41 57 10 55 83 67 56 2 73 44 26 85 78 14 27 40 51 61 54 29 16 25 5 31 71 42 21 30 3 74 6 63 76 33 39 68 66 23 53 20 22 43 45 52 80 60 1 59 50 58 12 77 65 36 15 19 46 17 79 9 47 8 70 75 34 7 69 32 4 84 64 35 11 13 48", "output": "82" }, { "input": "5\n2 3 4 1 5", "output": "1" }, { "input": "87\n66 53 79 35 24 61 22 70 29 43 6 21 75 4 85 2 37 18 65 49 40 82 58 73 33 87 71 19 34 83 84 25 56 48 9 63 38 20 67 32 74 42 51 39 11 1 78 86 44 64 81 17 62 72 47 54 52 23 7 5 41 46 3 28 77 57 13 15 59 68 14 36 50 27 80 31 26 10 55 60 69 76 16 12 8 45 30", "output": "79" }, { "input": "92\n42 64 33 89 57 9 24 44 87 67 92 84 39 88 26 27 85 62 22 83 23 71 14 13 73 79 15 49 2 12 76 53 81 40 31 3 72 58 1 61 7 82 20 54 46 77 11 16 28 48 6 45 36 43 60 38 18 4 32 74 10 91 19 86 75 51 50 52 78 25 65 8 55 30 90 69 59 63 56 80 29 68 70 17 35 41 37 47 66 34 5 21", "output": "1" }, { "input": "5\n1 2 3 4 5", "output": "1" } ]
186
2,560,000
-1
8,679
0
none
[ "none" ]
null
null
You are given a string *s*, consisting of small Latin letters. Let's denote the length of the string as |*s*|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string *s* so that for any prime number *p*<=≤<=|*s*| and for any integer *i* ranging from 1 to |*s*|<=/<=*p* (inclusive) the following condition was fulfilled *s**p*<==<=*s**p*<=×<=*i*. If the answer is positive, find one way to rearrange the characters.
The only line contains the initial string *s*, consisting of small Latin letters (1<=≤<=|*s*|<=≤<=1000).
If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO".
[ "abc\n", "abcd\n", "xxxyxxx\n" ]
[ "YES\nabc\n", "NO\n", "YES\nxxxxxxy\n" ]
In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at *p* = 2 (*s*<sub class="lower-index">2</sub> = *s*<sub class="lower-index">4</sub>). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.
[ { "input": "abc", "output": "YES\nabc" }, { "input": "abcd", "output": "NO" }, { "input": "xxxyxxx", "output": "YES\nxxxxxxy" }, { "input": "xxxjddyxduquybxdxx", "output": "NO" }, { "input": "jjjjjjjjjjzjjjjjjjjjjjjjjjj", "output": "YES\njjjjjjjjjjjjjjjjjjjjjjzjjjj" }, { "input": "mggnngggnjgnggggngnggpnggumggpg", "output": "NO" }, { "input": "hfihihhfh", "output": "NO" }, { "input": "ppppppopppionpppppppppwppppppppppppppppppppppb", "output": "YES\nbpppppppppppppppppppppppppppipnpppppopppopwppp" }, { "input": "jntnnnnnnngnnnnntnnennannnnnnnnnnnnnnnnjnnnnngnnnnannnnnnnnnq", "output": "NO" }, { "input": "dattqddidyddddmriftkdddddddddqvedwddfdrdiaddiokdddodyqqddddtdddvdddaodd", "output": "NO" }, { "input": "qqqqqqqqqqqqqqqqqqqqzqqqzqqqqqqqqqqqqqzqqqqqqqqcqqzqqqqqzqqqqqqqqqqqq", "output": "YES\ncqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzqqqqqzqqqqqzqzqqqqqzqq" }, { "input": "danmwkmkcekwkkekwugcqwfrkrkqowkakqinzkzksisekhmqtykkuhugknygvkkrhdhnuuf", "output": "NO" }, { "input": "jjjjjjjjjjjjjjjjjjjnjjjjjjjujjjjjjjjjjjjjjjjjjjjmpjjjjjjjjjjpjmjmpjjjjpjujjjjjjjjpjjjjjjujjjjjjnjjjjjjj", "output": "YES\nmjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjmjjjjjmjnjjjjjnjjjpjpjjjjjpjjjpjjjjjpjjjjjjjujjjuju" }, { "input": "qqqqqqqqqqsqqqqqqqqqqqqgpqqqqqqqbqqqqqqqqqqqqqqqqqqqqpqqqbqqqqqqqqqqqqqqqqqwqqqqsqqzqzqoqqqqqqqqqqwqqpqqqpq", "output": "NO" }, { "input": "uupwbpqnpwpcpppewppdsppiplpppwidplpnqblppppppwsxpplxpxlppikpewxppnlbkpppqecbpeppqupsepppqpneqnpexpbqcpqpccnple", "output": "NO" }, { "input": "nnnnnnnnnnnnjonnnnnnnnnnnnnnnnnnnnnnnndnnnnnnnnnnnnnnjnnnnnnnnnnnnnnnnndfnnnnnnnnnnnnnnnnnnnnn", "output": "YES\ndnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnndnnnnnfnjnnnnnjnnnnnnnnnnnnnnnnnnnnnonnnnn" }, { "input": "ssjjvshvssjdjnsdhvsjneehnnsshlldhjshdvsssshvsndsvsshlssvssvveslhllssshesssjsvvlsesssnselhjlljldlvnnshvsedllsennnnjssnsnsdsv", "output": "NO" }, { "input": "cccccccccccxccgccccccccccgccciccccicccgccccccccccccccccxcccccccccccycccccccccctcccccccccccccccccccctccccaicccccgccccccccccccccccccccccaicccccccc", "output": "YES\nacccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccacccccgcccgcccccgcccccccgccciciccciciccctccccccccccccctcccxcccccxcyccccc" }, { "input": "httttfttttttttttthttttttttttthtttttutttttttttttsjttttttttutttttajdttttttytttttuttttttattttttttttttttttttttttdttttttttttttttttttttttttttattttttsttttttfttttttt", "output": "NO" }, { "input": "wwwiuwujwijwwwwwwwwwwdwwwwhwwwwwiwwwwwwwwwuwwwhwwwjwwwwwwhwwwwwwwwniwwiwwwwwwwwwwwwwjwwwwwwwhwwwuhwwojwwwwjwwuwwwwwwwwwwiwwwwnwdwjwwwwwwwnwwwwh", "output": "NO" }, { "input": "kkkkkkokkkkkkokkkkkokkkkkkkxkkkkkkkkxkkkkkkkkkkkkkkkkkkkkkjkkkkekokkkkkkkkkkkkxkkkkokkkkkkkkkkkkkkkkkokkkkkkkkkbkkkkkkekkkxkkkkkkkkkkkkkkjkkkkkkkkkkkkkxkkkkkkkkkkkkkkkkkkkkkkkekkkkk", "output": "YES\nbkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkekkkekekkkjkjkkkkkkkkkkkkkkkkkkkkkokkkkkokokkkkkkkkkokokkkkkokkkkkxkkkxkkkkkxkkkkkxkx" }, { "input": "ouscouyvvoooocvokcuoovoooyorjyocosackfocooaoosaucooaojhacoooooorsruocorokufcvyjokoooproaroocjooyrkoorojsocfooskxujoxxpokoopyookooysofooovuoroxraofokupcpjrcokoxucskovoaokoocyoyoovfrovouoocvsc", "output": "NO" }, { "input": "ssssssssssssssssupsssssssqsssssssssssssssssssssssssssssssssssssassssssssssssssssssssssacssshsssssssscssssssssssspsssssslsgssssssslssssssdsasssessssssssssscssssbsszssssssssssssssssssossstsssssssssssssshs", "output": "YES\nasssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssasssasbssscssssssssssssscssscsssssdsesssssssssgshssssshssssslssslsssssossssspspsssssssssqstsssuszsss" }, { "input": "uuuuuuuuuuuuouuuuuuuuuuuuuuuuuuuuuuuucuuuuuuuuuuuuuuuuyuuuuuwuuuduuuuuuuuuutuuuuuuuuuuuuuuuuuuuuuuuuwuuuuquuuuuuuuuuuuuuuuuuuuuuuujumuuuuuutuuuuuuuhuuuuuuuauecuduuuuuuuuuuuuuuuuujusuuiuuuuuuuuuumuuyujuu", "output": "NO" }, { "input": "hlchxdgrkdxglihydlyqdgxggvdhdgrgigkugrqhhgpgxhyhgkcgyqildrpcvdlpggikrxggculgyudrrgvkycvgklldxkgggxlxvqgrgygpigcudhidgcigyqqhyxkgdxixglpgdxuxhlyxkidglriglkvgyckqrqvguclryvpyvdggluggcgkillcghdxkrhdglgcxgxicrkgkpugvkhliqyii", "output": "NO" }, { "input": "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjhhhhhhhhuhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjhhuhhhhhhhhhhhhhhhjhhhhhhhhhhhhhhhhjhhhhhhhuhhuhhhhhhhhhhhhhhhhjhhhhhhhuhhhhuhhhhhhhhhhhhhjhhhhhhhh", "output": "YES\nhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjhhhhhjhhhhhjhjhhhhhhhhhjhjhhhjhuhhhhhhhhhhhuhhhhhhhhhhhuhhhuhuhhhuhh" }, { "input": "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", "output": "YES\nhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" }, { "input": "uuuruuuuuuuuuuuuuuuuuuuuuuuiuuuuuuuuuuuuigzuuuuuuuuuuuguuuuuuouuuuuuuuuuuuuuuuuuuuuguuuuuuuzuueuuuuuuuuuuuuuufuuaouuuxuuuuutuuuuuuuuuuuuuuuuuuuuuuuuuuuulluuuutuuduuuuuuuujuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuupuruuouuuuuuuuuuuuuuuuuuuuqauu", "output": "YES\nauuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuauuuduuuuueufuuuuuuuuuguguuuuuguuuuuiuuuiuuuuujuuuuululuuuuuuuuuououuuoupuuuuuuuuuuuquuuuuuuuuuuruuurutuuutuuuuuxuzuuuuuuuuuz" }, { "input": "kkkmkkkkkkkymkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkdekkkkkkkkkkhrkkkkkkkkkkkkkkkkkkkkkxkkkkkokkkkikkkkkkkkkkkkkkkkkkkkkkkkkkkkkxkkfkkkkkkkkkkkkmkkkktykkkkkkkkkkkrkkkkkkrbekkkkkkkkkkkkkkkkkkfkskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkykkkkkkkkkkkkkkzykkkkkkkkkkkmbkkkkkkkkk", "output": "NO" }, { "input": "rrrvjurvrrsukrsrvxskrrrrbrapyrmrmvprmrseuakrmrnvokshwoawhnrbrrvrynwrynrxurrmprmmsryvkvronrmrprsrsrrrrrvwrarfrrrusrsvemwrevrarbwrmrrkhrryrhxornrorrrrrraphrvvhrrkrrfrprvorkkrsurpwvrkexpavbpnksafrrebxxyhsyrsywrrrrraryprunaxkbvrauokrrwrkpwwpwrrhakrxjrrxerefrrnxkwabrpbrrwmr", "output": "NO" }, { "input": "mmmmqmmmmmmmmmmmmmmlmmmmmmmmammmmmmmmmmmmmmmmmmmmmmmmmmmmmmmtmmmmmmmmmmimmmmmmmmmmmmmmmmmmammmmimmmmmmmmhmimmmomlmmmmmammmmmmmmmmmmmmmmmmimmmmlmmmmmmmimmmhmmmmmmmmmmmmmmmmmmmlmmmmmmmmmmmmmmhmmmmmmmmmmmmmmmtmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmotmmmmlmmmmmmmmmqmmmmmmmmmmmmmmmmmm", "output": "YES\nammmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmamammmmmmmmmhmhmmmmmhmmmmmimmmimmmmmimmmmmimimmmmmmmmmlmlmmmlmlmmmmmmmmmmmlmmmmmmmmmmmmmmmmmmmmmmmmmmmomommmmmmmmmqmmmmmqmmmmmtmmmmmtmtm" }, { "input": "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmymmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm", "output": "YES\nmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmymmm" }, { "input": "zzzzzzzzzzzzzzrzzzzbmzzzezzzzzzgzzzzzzzzzzhzzzzgzzzxzzzznzzzzzyzzzzzzzzzzzxzzzzzzzzzzzzzzzizzzzzzzzzzzzzzzzzzzzzzuzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzmzzzzzzzzzzzzzzzzzzzzzhzzzzzyzzzzzzzzzmzzfzbzzuzzzzzzzzzzzzzzzzzzzzzzzozzzzzzzyzzzzzzzzbzhzzzzzzzzzrzzzzzzyzzzzzzzzzzzzzzzzzzzzajzzzzz", "output": "YES\nazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzbzbzzzzzbzzzzzezzzfzzzzzgzzzzzgzhzzzzzzzzzhzhzzzizjzzzzzzzzzzzmzzzzzzzzzzzmzzzmznzzzozzzzzrzrzzzzzzzzzuzzzzzuzzzzzxzzzzzxzyzzzzzyzzzyzyzz" }, { "input": "bbbbbbbbbcbbbbbbqbbbbbkbbbbbbbbbbbbbvbsbbbbbebbbbbbbbbbbbbbbbbbbbbbbcbbbkbvbbbbbbubbbbbbbfbbbbbbbbbbbbbbbbbbcbbbbbbbbbbbbbbbekkbbbbbbbbbbbbbbbbfbqbbbbbbbbbfbbebbbbbbbbbbbbbbbbbbbbbbbbbeebbbbbbbbbbbbbbbeubbbbbbbbbbbqbbbbbbbbbbbbbbbbbbbbbbbbbbfbbbbbbbbbbbbbbbbkbebbbbbbbbbbbbuvbbbbbfbbbbbbbbbbbbbbbbbbbbbbbbbbbbcbbb", "output": "NO" }, { "input": "wtwwttwttwwwwwwwwtwtwwwwwtwwtwwtwwwwwtwtwtwtwwtwwwtwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwtwwwwwwwwwwwtwwwwwtwttwwwwtwwwwwwwwwwwwwwwwwtwwwwtwwwwwwwwwwwwwttwtwwwwwtwwtwwwwwwwtwwttttwwwwwwwwwwwwwwwwwwwtwwwttwtwwtwwtwwtwwwwtwwtwtwwtwwwwwwwwwwtwwww", "output": "NO" }, { "input": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "output": "YES\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }, { "input": "koadaaaaarnaavmmavimdolavaaovkaaabalaavabaolarkewawaavaaaaiaaalaaeaaenaobaamaaikaiaabaavaeenlaaaaaadaoaaaaaaaiekaebavvwaaabiobaadavaiaaviaaaadaaaamarakmaaakaeavwlaaeanaaanaaaaaakllovwnenvlakaiomaaaaaaamaainebaeakaaaaaalraiaallaannammaaamaaaaeeaaiawaabvaaealaermaobaavreaaaaakavrvdaawaraedaaaaawiaeoaaaadaaaaavvadaaaoaaeewokbaaaeaaamaabdaaama", "output": "NO" }, { "input": "oooooooocooocooooooooooooooooooooooooofoooooooooooooovoooozoooooooooqoooooooooooooooooooooooooooooooooxooovoooooooocoooooxooobooczoooooooooqooooooooooooooooocooooeooooooxoooooooooooooooooozooooooooooooooooooxoopooooocooobooooooooooooooooloooooooooooooooooooobooooelooooooooooooooooooooooooxooiooooooooooopooboooooooooooooooooooooolooooooooooooooooooooo", "output": "YES\nbooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooobobooooooooobocooococooooooooooocooooooooooocooocoeoooeooooofoiooooooooolooooolooooolooooopopoooooqoooqovooooooooovoooooooooooooxoooxoxoooxoooooooooooooxooooozooooooooozozooo" }, { "input": "bbbbbbzbbbbbbbbbbgbbbbbbbbbbbpbbbbbbbbbbbbbbbbbbsbbbbbbbbbbbbbbbbbbbbbbbbmbbbbbbbobbbbbbbwubbbrbbbbbbbbbbbbbbbubbbbbbbqobbbbdbbbbbbbbbbrbbbbbbbbbbbbbbbbbbbbbbbbbbbxbbbbbbbbbbbbbbbbbbdbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbobbbbbbbbbibbbbbbbbbbgbbbbebbybbbbbbbbbbbbbbbabbbbbbbbbybiybbbbdbbbgbebbbbbkbbbbbbnbbbbbbbbbbbbbbbbbbbbbbbsbbbbbbbbbbbbbbbtbbbbbbbbbpbbb", "output": "NO" }, { "input": "llvlluelllllllllzllnndluelllhdunlnlllllhnbhldllelllllhllleulunvlldlllllbepellpzulbulllelllvblllulllllllllezpllhldellzldlllllllelllhlllehllvevlnullllvllllpudldlllvdbllllhnvlllllvllvllvlllplelzplpzllllulzlbllzldevlullelllhhzdeulllllllllnlzvllllllbllvllllllllluellnlllulnllllbdlevlhlvllllpenblvzlzlllblvhllllhllllpeblevelpllllnpbhlhllbllllndllblldvn", "output": "NO" }, { "input": "zzzzzzzzzzzzzzzzzzzzzzzzzzzozzzzzzzzzzzzzzzzzzzzzzzzozzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzozzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", "output": "YES\nozzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzozozzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" }, { "input": "lsllllmllllslllxlvlxluyylllslllmlllvllxllslllvlslyulllullllllmlxulllyxlllmlllllvullllllslllsmlllxmllvvlslllllllllllulllvlllllllmllllsylylvluylllmullslllllvylmmlluyllvlylllllllllllvllllsmlxlslmlllxxslllmyluvylylllllxmxlulsslslmllulxllmllxuusllllumlllllmsssmxullllmsllllmlxxllmllllylvllmlluymxlllllslyxllvlllllllllllllllsllullmlllllslllsllullslllllllllllvyllllvlllylllsllvlvsmlvllllllulullllllslxlml", "output": "NO" }, { "input": "cccjcchccccccjcccccccjlccccccccccccchccccwccctccccccccccccccccczccccccccccccccccccccccccccccccccccccccnwcccccccccccccccccccoccwcccccccccccwccccccccccccccncdccccbcctcccccccccccccccccccccccxccccccvcccnccccbccccccccccccccccccccccclccccccccczccvccccccvscccccccccxccccocccccccscccccccccccccxvccccccccccccccccccccccccccccccccccccccccccccccxccccccccccccwccccccchccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", "output": "YES\nbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccbcccccccccccdccchchccchcccccjcjcccccccccjccccclccccclcccccncncccccncccococccccccccscccccccccccccsccctctcccvcccccccccccccvcccccvcccccccccvcwcccwcccccwcccccccwcccccwcccccxcccxcccccxcccccccxccczccccccczccccc" }, { "input": "uuuuuuuuuuuuuuuuuuuuuuuuuuquuuuulquuuuuuuouuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuouuuuxouuuuuxuuuuuuuuuuuuuuuuuuuuuuuxuuuuuuruuuuuuxuuuuuuuuuuuuuuuuuuuuuuuzuuuuuuuuuuuuuuupuuuuuuuuuuuuuuuuuuluuuuuuuuuuuuuuuuuuuuuuuuuuuuuurluuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuxuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuruuuuuuouuuquuuluuuuuuuuupuuouuuuuuuuuuzuuuuuuuokuuluuuupuuukuuuuuruuuuuuupuuuuuuuuuuuuzuouuuuuuuuuuuuuuuz", "output": "NO" }, { "input": "vkxvxkxvxxxkkvkxvkkxvvvvvvvvkkvkkxvvkkvkkvkkvxxvxvvvvkvkvvxvvxvxvvvkvxkkxkxxkxxkxvxvvxvkvvxvkvkvkxkxvxkvvkvxkxkvkxvxvxxxxxvxxvxxxkkvvvvkkvvvxxkxkvvkxvvxkkkvkvvkvvvvxxxkxxxvvvkxkxvkvxxkxkxkxkvvxxkkkkvxxkvkkxvxvxxvxxxxvvkvxkxkxvkkvxxvxkvvkxkkvxvkkkkkkxkxkvxkkkxxxxxxxvxkxkkvxkkxvkvkkvkkkxvkvkvkvvxkkkxxvxxxxxvkvvxkvvvxvxvkkvvvxkvkxkvxxkvvkxvvxkxxkxkkkvxkxxvkkkxxkxxxvxkxkkkvkkxxxvvkkvvkvkkkxkvxkxxxvkvxvxxkvkxkvkxvxvvxvxxkkkxxkkkkvvvv", "output": "NO" }, { "input": "gggggggggggqggggggggnqggggggepgggeggggpggqeggggegggggggggpggggggggggggggggggggggggggggggggggggggggggggggggggggggggggghggqghgggggggggggggggggggggggqgqggggghgggnnggggggggggggggggqgggggpgggggggggggnggggpggggggggggggggggggggggggggggggggqgggqggggggggggeegggggggggggggggggggggngggggghqggggggggggggggggggggggggggggggepggggggggghgggggggggggggggggggggggggghggggggghgggnhgggggggggpggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", "output": "YES\negggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggegggegegggegggggegeggggggggggggggghggggghggggghghggggghggghghggggggggghgggggggggggggngggngngggngggggggggggggngggggngggggggggpgpgggpgggggpgggggggpgggggpgggggpgggqgggggqgggggggqgggqgggggggqgggggggggqgqgggggggggqgqgggggqggg" }, { "input": "pynyyyyypyyyfyyyyyyyvoyyyyvyyyldyyyyyhyyvyyyyyyyyyyyyoyyppyyyyyyywyyysyyyyayyyayyyyyyyoyyyydyyyyyavyyygiyyhfyyyyyyyyyyzyyyysyyyyicyyyyyyycjyyhykyiyykkyylyyyyiyyyyyyyyifyyyyyyyzyyyyyiyyryykyyyhzyydryyyysyyyyyyyyyyyyyaybyyyoyyyykyyyyyyyyyyncyyyyvryyjyyyyqyyfbyyysyyqypyydyyyyyyyyyyyqyywjyyyyyayyyyyyyyyjyyyyyyyyyyyyaygyyypyyyyyayyyyyyyoyyyyiyyyyyiyyyyyjoyykyyyyyyyyyyyyyyyyyyyyyohyyyyvyykyybiyyyyyyyyyyyyyyyyyyyyyyyfyyyyyyyyyyyyyyyyvyyyyyyy", "output": "NO" }, { "input": "bbbbbbbbbbbbzqbbbbibbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbibbbbbbbbbbbjbbbbbbbbbbbbbbgbbbbbbbbbbzebbbbbvbbbbbbbbbbbbbbbbbbbbbbbbtbbbtbvbebbbbzvbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbibbbbbbqbqjbbpbbzbbbbbbbbbbbbbbbbbbbbbbbbbbbebbbbzbbcbbbbbbbqbbbvbbbbbbbbbbbbbbbbbbbbpbbbbbbbbbbbgbbbbzbbbbbbbbbbbbjbbbbbbbbbbbbbbbbbbbbbcbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcbbbbebqbbbbjbbbbbbbbbbbbbbbbbbbbbbbbtbbbbbbbbbbbbbbbbbbbbbbbbbbbbqbbbbbbbbbbbbbbbibbbbbbbbbbbbbbbbbzb", "output": "YES\ncbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcbbbcbbbbbebebbbbbbbbbebbbbbebbbbbgbbbbbgbibbbbbibbbibibbbbbbbbbjbbbbbbbbbbbbbjbbbjbjbbbpbbbbbbbbbbbbbpbbbbbqbbbbbbbbbqbqbbbqbbbbbqbbbbbbbqbbbbbtbbbbbtbbbtbbbbbvbbbbbbbvbbbvbbbbbbbvbbbbbbbbbzbzbbbbbbbbbzbzbbbbbzbbbzbbbbbzbbbbb" }, { "input": "tsssshssusssssssssssusssspssssssssssssssssssssssssssssssvsssssssssssssssossssvssssssssssysssssscsssssssussfsssssssszssssssssfssssssssssssssssssssssswssssssssdsssssssssssussssssessmssssssssssjsssssvsssssssssssssssssssssssssssssssssssssssssssssssstssssssssrsssssshssssssssssssssssssssssssssssusssssssssssssssssssssssssssssssessscssrssssssessssssslwwsssssssssssusssscsssssssssssssswssssssssssssssessssstssssssstsshsssssssssssssssssssssisssssssssssssssssssssssosssssswsss", "output": "NO" }, { "input": "fkfffffffvfkfoffffqoqqffaftftvfuffjffffffffoflftvfffgffffffzfrvfocfmfkmjfftfmfwafzfffajsfffalguvjdfftfndfcnfjfgfffoofoflnffffffofdiczffajffdifrffffffrffffnifofffifdffvgffvoffwbfkfqfsfwfmwcfrkzuzokufmffffffcfffqfzfoffonfnfffkfffkwqifffsfifffzfofofffrfffflftkkfvfffbffuwffjtffmfffaffdlznffjllfqgfffffffffdmlfwlabfblcfdfwffffbfvmfrifwvvffffzffbfafnsffantfrkffzffffffifffrwacfrffffsdfffkffffooaffqckqbnbqqofvafffrtgffwsmfngffbzfffgffofnflfffvrffffmjjkfvfcfccfqfjfnf", "output": "NO" }, { "input": "xxxxxxxxxxfxxxxxxxxxxxxxxxxxxxxxxxxxxxrxtxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxexxxxmxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoxxxxxxxxxxexxxxxxxxixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxtxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxuxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoxxxxxxxxxxxxxxxxxxxxxxxxxxaxxxbxxxxxxxxxxxxxxxxxxxxxxxxxxxbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsxxxxxzxxxxxxxxxxxxxxxxxxxxxxxxxhrxxnxxxxxxxxxxxxxxxxtxxxxxxxxxx", "output": "YES\naxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbxxxxxbxxxxxexxxxxexfxxxxxhxxxixmxxxxxxxxxnxxxxxxxxxxxxxoxxxoxoxxxpxxxxxxxxxxxxxrxxxxxrxxxxxxxxxsxtxxxtxxxxxtxxxxxxxuxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxzxxxx" }, { "input": "vtvzvlvlzvrtevdvvvsvjyvwrwwjhovvtotvkvvvvvovvewlvrvvfsvvvvnvfcsvvvtcrmckvvvztvvlvvdyfvcylvhvvcvvvvrvhwvryvhmvvcnsozusvltocvvywvvsvvvdocoftvkvvzvvtnvcmevyvhvhvlovvvvlvvvvtdjcvvvyvvurvuovusvvwvrvtvvfvmvvvmnycnvmvovuzvzvvvjvvvvnvvdrvvcrrvfvzujvyvtmrucvvvcjuvrvvtukvvvlvrkhvvsvjvftvzvmvvunsweuzcvvvfrvrwhyvdjerltlvcvvzednvyvjkvktvvesovvvmkvvnvzvjvcvfwvzhfkvvvuvvduovvmevkvhvuvmnvnvvvvvuvvlfjtjmdvceslvmevvvjvvkvvkjnvfyseevmvlvzvvvvvfdvleuvvlvvyvmrmvvfsevovhzdrru", "output": "NO" }, { "input": "mmmmmmmmmmmmmmmmmmmmmmmmmsmmmmmmmmmmmmummmmmummmmmmmmmmmmmmmsmmmmmmmmmmmmmmsmmmmmmmmmmmmmmmmmmmmmsmmmmmmmmmmmmmmmmrummmmmmmmmmmmmmmmmmmmmmmmmmmmmsmsmmmmmmmrmmmmmmmmmmmmmmmmmmrmmmmmmcmmsmmmmmmmmrmmmmmmmmmmmmmmmmmmmmmmummmmmmcmmmmmmmmmmmmmmmmmmmcmmmmmmmrmmmmmmmmmmcmmmmmrurmmmmmmmmmmmmmummmmmmmmmmmummmmmmsmmmmrmmmmmmmmmmmmmmcmmmmmmmmmmmmmmmmummsmmmummmmmmmmmrmmmmmmmmmmmmummummrmmmmmmmmmmmmmmmmmmmrmrmmmmmmmmmmmmmmummmmmmmmmmmmmsmmmmmmmummmmmmmmmmmmmmmcmmmmmmmmmmmmmmmmmmmmmm", "output": "YES\ncmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmcmcmmmmmmmmmcmmmmmcmmmmmcmmmmmrmrmmmmmrmmmrmrmmmmmmmmmrmmmmmmmmmmmmmrmmmrmrmmmrmmmmmmmmmmmmmrmmmmmrmmmmmmmmmsmsmmmsmmmmmsmmmmmmmsmmmmmsmmmmmsmmmsmmmmmsmmmmmmmsmmmummmmmmmummmmmmmmmumummmmmmmmmumummmmmummmummmmmummmmmmmummmumummmummmmmmm" }, { "input": "z", "output": "YES\nz" } ]
216
20,275,200
0
8,690
1,004
Sonya and Matrix
[ "brute force", "constructive algorithms", "implementation" ]
null
null
Since Sonya has just learned the basics of matrices, she decided to play with them a little bit. Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so. The Manhattan distance between two cells ($x_1$, $y_1$) and ($x_2$, $y_2$) is defined as $|x_1 - x_2| + |y_1 - y_2|$. For example, the Manhattan distance between the cells $(5, 2)$ and $(7, 1)$ equals to $|5-7|+|2-1|=3$. Note that rhombic matrices are uniquely defined by $n$, $m$, and the coordinates of the cell containing the zero. She drew a $n\times m$ rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of $n\cdot m$ numbers). Note that Sonya will not give you $n$ and $m$, so only the sequence of numbers in this matrix will be at your disposal. Write a program that finds such an $n\times m$ rhombic matrix whose elements are the same as the elements in the sequence in some order.
The first line contains a single integer $t$ ($1\leq t\leq 10^6$) — the number of cells in the matrix. The second line contains $t$ integers $a_1, a_2, \ldots, a_t$ ($0\leq a_i&lt; t$) — the values in the cells in arbitrary order.
In the first line, print two positive integers $n$ and $m$ ($n \times m = t$) — the size of the matrix. In the second line, print two integers $x$ and $y$ ($1\leq x\leq n$, $1\leq y\leq m$) — the row number and the column number where the cell with $0$ is located. If there are multiple possible answers, print any of them. If there is no solution, print the single integer $-1$.
[ "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4\n", "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1\n", "6\n2 1 0 2 1 2\n" ]
[ "4 5\n2 2\n", "3 6\n2 3\n", "-1\n" ]
You can see the solution to the first example in the legend. You also can choose the cell $(2, 2)$ for the cell where $0$ is located. You also can choose a $5\times 4$ matrix with zero at $(4, 2)$. In the second example, there is a $3\times 6$ matrix, where the zero is located at $(2, 3)$ there. In the third example, a solution does not exist.
[ { "input": "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4", "output": "4 5\n2 2" }, { "input": "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1", "output": "3 6\n2 3" }, { "input": "6\n2 1 0 2 1 2", "output": "-1" }, { "input": "1\n0", "output": "1 1\n1 1" }, { "input": "7\n0 1 2 3 4 2 6", "output": "-1" }, { "input": "6\n0 0 0 0 0 0", "output": "-1" }, { "input": "4\n0 0 0 0", "output": "-1" } ]
155
8,396,800
-1
8,720
128
Games with Rectangle
[ "combinatorics", "dp" ]
null
null
In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted *n*<=×<=*m* rectangle (only the border, no filling). Anna and Maria move in turns and Anna starts. During each move one should paint inside the last-painted rectangle a new lesser rectangle (along the grid lines). The new rectangle should have no common points with the previous one. Note that when we paint a rectangle, we always paint only the border, the rectangles aren't filled. Nobody wins the game — Anna and Maria simply play until they have done *k* moves in total. Count the number of different ways to play this game.
The first and only line contains three integers: *n*,<=*m*,<=*k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=1000).
Print the single number — the number of the ways to play the game. As this number can be very big, print the value modulo 1000000007 (109<=+<=7).
[ "3 3 1\n", "4 4 1\n", "6 7 2\n" ]
[ "1\n", "9\n", "75\n" ]
Two ways to play the game are considered different if the final pictures are different. In other words, if one way contains a rectangle that is not contained in the other way. In the first sample Anna, who performs her first and only move, has only one possible action plan — insert a 1 × 1 square inside the given 3 × 3 square. In the second sample Anna has as much as 9 variants: 4 ways to paint a 1 × 1 square, 2 ways to insert a 1 × 2 rectangle vertically, 2 more ways to insert it horizontally and one more way is to insert a 2 × 2 square.
[ { "input": "3 3 1", "output": "1" }, { "input": "4 4 1", "output": "9" }, { "input": "6 7 2", "output": "75" }, { "input": "5 5 3", "output": "0" }, { "input": "2 2 1", "output": "0" }, { "input": "999 999 499", "output": "1" }, { "input": "456 876 1000", "output": "0" }, { "input": "3 5 1", "output": "6" }, { "input": "5 7 2", "output": "15" }, { "input": "10 13 3", "output": "77616" }, { "input": "1000 1000 499", "output": "998001" }, { "input": "1000 1000 500", "output": "0" }, { "input": "3 1000 1", "output": "498501" }, { "input": "1000 3 1", "output": "498501" }, { "input": "998 1000 499", "output": "0" }, { "input": "1000 1000 250", "output": "263321201" }, { "input": "999 996 247", "output": "729817056" }, { "input": "86 564 16", "output": "966200617" }, { "input": "711 390 95", "output": "187455436" }, { "input": "963 415 36", "output": "336772492" }, { "input": "356 628 17", "output": "665796305" }, { "input": "214 538 33", "output": "661877504" }, { "input": "840 474 207", "output": "895622621" }, { "input": "589 898 280", "output": "752764170" }, { "input": "227 405 404", "output": "0" }, { "input": "351 286 60", "output": "414370922" }, { "input": "531 131 43", "output": "102593830" }, { "input": "980 811 236", "output": "542553202" }, { "input": "638 119 38", "output": "73514263" }, { "input": "897 301 47", "output": "886904759" }, { "input": "569 191 164", "output": "0" }, { "input": "409 92 105", "output": "0" }, { "input": "307 190 52", "output": "186536168" }, { "input": "354 923 125", "output": "708700715" }, { "input": "705 155 490", "output": "0" }, { "input": "188 413 35", "output": "103598368" }, { "input": "954 950 732", "output": "0" }, { "input": "580 1000 203", "output": "693824000" }, { "input": "104 935 326", "output": "0" }, { "input": "611 229 104", "output": "737450171" }, { "input": "277 939 15", "output": "934000455" }, { "input": "338 949 121", "output": "67858020" }, { "input": "734 917 148", "output": "80695422" }, { "input": "505 380 86", "output": "926905224" }, { "input": "340 124 41", "output": "801948369" }, { "input": "565 606 234", "output": "509636173" }, { "input": "956 926 201", "output": "186215807" }, { "input": "1000 1000 20", "output": "155086097" }, { "input": "1000 1000 1000", "output": "0" }, { "input": "1000 1000 100", "output": "58573582" } ]
482
114,176,000
3
8,750
696
Legen...
[ "data structures", "dp", "matrices", "strings" ]
null
null
Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible. Initially, happiness level of Nora is 0. Nora loves some pickup lines like "I'm falling for you" and stuff. Totally, she knows *n* pickup lines, each consisting only of lowercase English letters, also some of them may be equal (in writing, but different in pronouncing or meaning though). Every time Nora sees *i*-th pickup line as a consecutive subsequence of Barney's text message her happiness level increases by *a**i*. These substrings may overlap, for example, Nora will see the pickup line aa twice and the pickup line ab once in text message aaab. Due to texting app limits, Barney's text may have up to *l* characters. Barney asked you to help him make Nora as much happy as possible, it's gonna be legen...
The first line of input contains two integers *n* and *l* (1<=≤<=*n*<=≤<=200,<=1<=≤<=*l*<=≤<=1014) — the number of pickup lines and the maximum length of Barney's text. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=100), meaning that Nora's happiness level increases by *a**i* after every time seeing *i*-th pickup line. The next *n* lines contain the pickup lines. *i*-th of them contains a single string *s**i* consisting of only English lowercase letter. Summary length of all pickup lines does not exceed 200. All strings are not empty.
Print the only integer — the maximum possible value of Nora's happiness level after reading Barney's text.
[ "3 6\n3 2 1\nheart\nearth\nart\n", "3 6\n3 2 8\nheart\nearth\nart\n" ]
[ "6\n", "16\n" ]
An optimal answer for the first sample case is hearth containing each pickup line exactly once. An optimal answer for the second sample case is artart.
[]
30
0
0
8,805
475
Bayan Bus
[ "implementation" ]
null
null
The final round of Bayan Programming Contest will be held in Tehran, and the participants will be carried around with a yellow bus. The bus has 34 passenger seats: 4 seats in the last row and 3 seats in remaining rows. The event coordinator has a list of *k* participants who should be picked up at the airport. When a participant gets on the bus, he will sit in the last row with an empty seat. If there is more than one empty seat in that row, he will take the leftmost one. In order to keep track of the people who are on the bus, the event coordinator needs a figure showing which seats are going to be taken by *k* participants. Your task is to draw the figure representing occupied seats.
The only line of input contains integer *k*, (0<=≤<=*k*<=≤<=34), denoting the number of participants.
Print the figure of a bus with *k* passengers as described in sample tests. Character '#' denotes an empty seat, while 'O' denotes a taken seat. 'D' is the bus driver and other characters in the output are for the purpose of beautifying the figure. Strictly follow the sample test cases output format. Print exactly six lines. Do not output extra space or other characters.
[ "9\n", "20\n" ]
[ "+------------------------+\n|O.O.O.#.#.#.#.#.#.#.#.|D|)\n|O.O.O.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+\n", "+------------------------+\n|O.O.O.O.O.O.O.#.#.#.#.|D|)\n|O.O.O.O.O.O.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.#.#.#.#.#.|.|)\n+------------------------+\n" ]
none
[ { "input": "9", "output": "+------------------------+\n|O.O.O.#.#.#.#.#.#.#.#.|D|)\n|O.O.O.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "20", "output": "+------------------------+\n|O.O.O.O.O.O.O.#.#.#.#.|D|)\n|O.O.O.O.O.O.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "30", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.O.O.#.|D|)\n|O.O.O.O.O.O.O.O.O.O.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.O.#.#.|.|)\n+------------------------+" }, { "input": "5", "output": "+------------------------+\n|O.O.#.#.#.#.#.#.#.#.#.|D|)\n|O.#.#.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.#.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "0", "output": "+------------------------+\n|#.#.#.#.#.#.#.#.#.#.#.|D|)\n|#.#.#.#.#.#.#.#.#.#.#.|.|\n|#.......................|\n|#.#.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "1", "output": "+------------------------+\n|O.#.#.#.#.#.#.#.#.#.#.|D|)\n|#.#.#.#.#.#.#.#.#.#.#.|.|\n|#.......................|\n|#.#.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "2", "output": "+------------------------+\n|O.#.#.#.#.#.#.#.#.#.#.|D|)\n|O.#.#.#.#.#.#.#.#.#.#.|.|\n|#.......................|\n|#.#.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "3", "output": "+------------------------+\n|O.#.#.#.#.#.#.#.#.#.#.|D|)\n|O.#.#.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|#.#.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "4", "output": "+------------------------+\n|O.#.#.#.#.#.#.#.#.#.#.|D|)\n|O.#.#.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.#.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "6", "output": "+------------------------+\n|O.O.#.#.#.#.#.#.#.#.#.|D|)\n|O.O.#.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.#.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "7", "output": "+------------------------+\n|O.O.#.#.#.#.#.#.#.#.#.|D|)\n|O.O.#.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "8", "output": "+------------------------+\n|O.O.O.#.#.#.#.#.#.#.#.|D|)\n|O.O.#.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "10", "output": "+------------------------+\n|O.O.O.#.#.#.#.#.#.#.#.|D|)\n|O.O.O.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "11", "output": "+------------------------+\n|O.O.O.O.#.#.#.#.#.#.#.|D|)\n|O.O.O.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "12", "output": "+------------------------+\n|O.O.O.O.#.#.#.#.#.#.#.|D|)\n|O.O.O.O.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.#.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "13", "output": "+------------------------+\n|O.O.O.O.#.#.#.#.#.#.#.|D|)\n|O.O.O.O.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "14", "output": "+------------------------+\n|O.O.O.O.O.#.#.#.#.#.#.|D|)\n|O.O.O.O.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "15", "output": "+------------------------+\n|O.O.O.O.O.#.#.#.#.#.#.|D|)\n|O.O.O.O.O.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.#.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "16", "output": "+------------------------+\n|O.O.O.O.O.#.#.#.#.#.#.|D|)\n|O.O.O.O.O.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "17", "output": "+------------------------+\n|O.O.O.O.O.O.#.#.#.#.#.|D|)\n|O.O.O.O.O.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "18", "output": "+------------------------+\n|O.O.O.O.O.O.#.#.#.#.#.|D|)\n|O.O.O.O.O.O.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.#.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "19", "output": "+------------------------+\n|O.O.O.O.O.O.#.#.#.#.#.|D|)\n|O.O.O.O.O.O.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "21", "output": "+------------------------+\n|O.O.O.O.O.O.O.#.#.#.#.|D|)\n|O.O.O.O.O.O.O.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.#.#.#.#.#.|.|)\n+------------------------+" }, { "input": "22", "output": "+------------------------+\n|O.O.O.O.O.O.O.#.#.#.#.|D|)\n|O.O.O.O.O.O.O.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.#.#.#.#.|.|)\n+------------------------+" }, { "input": "23", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.#.#.#.|D|)\n|O.O.O.O.O.O.O.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.#.#.#.#.|.|)\n+------------------------+" }, { "input": "24", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.#.#.#.|D|)\n|O.O.O.O.O.O.O.O.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.#.#.#.#.|.|)\n+------------------------+" }, { "input": "25", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.#.#.#.|D|)\n|O.O.O.O.O.O.O.O.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.#.#.#.|.|)\n+------------------------+" }, { "input": "26", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.O.#.#.|D|)\n|O.O.O.O.O.O.O.O.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.#.#.#.|.|)\n+------------------------+" }, { "input": "27", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.O.#.#.|D|)\n|O.O.O.O.O.O.O.O.O.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.#.#.#.|.|)\n+------------------------+" }, { "input": "28", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.O.#.#.|D|)\n|O.O.O.O.O.O.O.O.O.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.O.#.#.|.|)\n+------------------------+" }, { "input": "29", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.O.O.#.|D|)\n|O.O.O.O.O.O.O.O.O.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.O.#.#.|.|)\n+------------------------+" }, { "input": "31", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.O.O.#.|D|)\n|O.O.O.O.O.O.O.O.O.O.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.O.O.#.|.|)\n+------------------------+" }, { "input": "32", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.O.O.O.|D|)\n|O.O.O.O.O.O.O.O.O.O.#.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.O.O.#.|.|)\n+------------------------+" }, { "input": "33", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.O.O.O.|D|)\n|O.O.O.O.O.O.O.O.O.O.O.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.O.O.#.|.|)\n+------------------------+" }, { "input": "34", "output": "+------------------------+\n|O.O.O.O.O.O.O.O.O.O.O.|D|)\n|O.O.O.O.O.O.O.O.O.O.O.|.|\n|O.......................|\n|O.O.O.O.O.O.O.O.O.O.O.|.|)\n+------------------------+" } ]
62
0
3
8,816
135
Cycle
[ "brute force", "dfs and similar", "implementation" ]
null
null
Little Petya very much likes rectangular tables that consist of characters "0" and "1". Recently he has received one such table as a gift from his mother. The table contained *n* rows and *m* columns. The rows are numbered from top to bottom from 1 to *n*, the columns are numbered from the left to the right from 1 to *m*. Petya immediately decided to find the longest cool cycle whatever it takes. A cycle is a sequence of pairwise distinct cells where each two consecutive cells have a common side; besides, the first cell has a common side with the last cell. A cycle is called cool if it fulfills all the following conditions simultaneously: - The cycle entirely consists of the cells that contain "1". - Each cell that belongs to the cycle, has a common side with exactly two other cells that belong to the cycle. - Each cell of the table that contains "1" either belongs to the cycle or is positioned outside of it (see definition below). To define the notion of "outside" formally, let's draw a cycle on a plane. Let each cell of the cycle (*i*,<=*j*) (*i* is the row number, *j* is the column number) correspond to the point (*i*,<=*j*) on the coordinate plane. Let a straight line segment join each pair of points that correspond to the cells belonging to the cycle and sharing a side. Thus, we will get a closed polyline that has no self-intersections and self-touches. The polyline divides the plane into two connected parts: the part of an infinite area and the part of a finite area. It is considered that cell (*r*,<=*c*) lies outside of the cycle if it does not belong to the cycle and the corresponding point on the plane with coordinates (*r*,<=*c*) lies in the part with the infinite area. Help Petya to find the length of the longest cool cycle in the table. The cycle length is defined as the number of cells that belong to the cycle.
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — the number of rows and columns in the table, respectively. Each of the following *n* lines contains *m* characters. Each character can be either "0" or "1".
Print a single number — the length of the longest cool cycle in the table. If such cycles do not exist, print 0.
[ "3 3\n111\n101\n111\n", "5 5\n01010\n10101\n01010\n10101\n01010\n", "7 7\n1111111\n1000101\n1000101\n1000101\n1000111\n1000001\n1111111\n", "5 5\n11111\n10001\n10101\n10001\n11111\n" ]
[ "8\n", "0\n", "24\n", "0\n" ]
In the first example there's only one cycle and it is cool. In the second sample there's no cycle at all. In the third sample there are two cool cycles: their lengths are 12 and 24. In the fourth sample there also is only one cycle but it isn't cool as there's a cell containing "1" inside this cycle.
[]
60
0
0
8,828
645
Enduring Exodus
[ "binary search", "two pointers" ]
null
null
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. Farmer John wants to book a set of *k*<=+<=1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms *i* and *j* is defined as |*j*<=-<=*i*|. Help Farmer John protect his cows by calculating this minimum possible distance.
The first line of the input contains two integers *n* and *k* (1<=≤<=*k*<=&lt;<=*n*<=≤<=100<=000) — the number of rooms in the hotel and the number of cows travelling with Farmer John. The second line contains a string of length *n* describing the rooms. The *i*-th character of the string will be '0' if the *i*-th room is free, and '1' if the *i*-th room is occupied. It is guaranteed that at least *k*<=+<=1 characters of this string are '0', so there exists at least one possible choice of *k*<=+<=1 rooms for Farmer John and his cows to stay in.
Print the minimum possible distance between Farmer John's room and his farthest cow.
[ "7 2\n0100100\n", "5 1\n01010\n", "3 2\n000\n" ]
[ "2\n", "2\n", "1\n" ]
In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms. In the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2. In the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1.
[ { "input": "7 2\n0100100", "output": "2" }, { "input": "5 1\n01010", "output": "2" }, { "input": "3 2\n000", "output": "1" }, { "input": "10 1\n1101111101", "output": "6" }, { "input": "2 1\n00", "output": "1" }, { "input": "3 1\n010", "output": "2" }, { "input": "8 7\n00000000", "output": "4" }, { "input": "7 6\n0000000", "output": "3" }, { "input": "112 12\n0110101000000010101110010111100101011010011110100111111100011101011111000111101101110100111011110001100110110010", "output": "10" }, { "input": "9 8\n000000000", "output": "4" }, { "input": "9 3\n010001000", "output": "2" }, { "input": "5 3\n00000", "output": "2" }, { "input": "8 7\n00000000", "output": "4" }, { "input": "6 1\n000011", "output": "1" }, { "input": "100 40\n0010010100000100011100010100110001101100110000110010000000001010000111100000100100100101010010001100", "output": "30" }, { "input": "93 79\n000000000000000000011000000000000000000000000000000000000000000000010000000000100000100000000", "output": "42" }, { "input": "31 11\n0000001011011100010000000110001", "output": "7" }, { "input": "47 46\n00000000000000000000000000000000000000000000000", "output": "23" }, { "input": "100 96\n0000000000000010000010000000000000000000000000000000000000000000000000000010000000000000000000000000", "output": "50" }, { "input": "491 89\n01111101111111100000111010110001010001110111000010101111101000100010010111011101110110111101101010111000111000011100011010010010111111000011011010100110001000011100111000001011100010001111101111101000111001100110010100101000001110010100100100100101001100010101001000010000111110011000000100000100101000100101000001001101011011100000110101111110101001001000100110010000010110101011000101011001001011001000110000011111001110101011000000110101000000100110001101111000101001001001100001001110101", "output": "73" }, { "input": "308 17\n01000000100000000000000001000001000010000000000000000001001110000001010001000110000000000000100101000000010000001000000000001100000110000000000000000001000000000000000100000001000010001000000001000000000000000100010000000000000000000000000000000000001000000000001001101100000000000010000000000000000000000000", "output": "9" }, { "input": "8 4\n00111000", "output": "5" }, { "input": "18 2\n010111110111011110", "output": "5" }, { "input": "29 3\n01110011111111111111110110110", "output": "17" } ]
390
307,200
3
8,831
32
Constellation
[ "implementation" ]
D. Constellation
2
256
A star map in Berland is a checked field *n*<=×<=*m* squares. In each square there is or there is not a star. The favourite constellation of all Berland's astronomers is the constellation of the Cross. This constellation can be formed by any 5 stars so, that for some integer *x* (radius of the constellation) the following is true: - the 2nd is on the same vertical line as the 1st, but *x* squares up - the 3rd is on the same vertical line as the 1st, but *x* squares down - the 4th is on the same horizontal line as the 1st, but *x* squares left - the 5th is on the same horizontal line as the 1st, but *x* squares right Such constellations can be very numerous, that's why they are numbered with integers from 1 on the following principle: when two constellations are compared, the one with a smaller radius gets a smaller index; if their radii are equal — the one, whose central star if higher than the central star of the other one; if their central stars are at the same level — the one, whose central star is to the left of the central star of the other one. Your task is to find the constellation with index *k* by the given Berland's star map.
The first line contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=300,<=1<=≤<=*k*<=≤<=3·107) — height and width of the map and index of the required constellation respectively. The upper-left corner has coordinates (1,<=1), and the lower-right — (*n*,<=*m*). Then there follow *n* lines, *m* characters each — description of the map. *j*-th character in *i*-th line is «*», if there is a star in the corresponding square, and «.» if this square is empty.
If the number of the constellations is less than *k*, output -1. Otherwise output 5 lines, two integers each — coordinates of the required constellation. Output the stars in the following order: central, upper, lower, left, right.
[ "5 6 1\n....*.\n...***\n....*.\n..*...\n.***..\n", "5 6 2\n....*.\n...***\n....*.\n..*...\n.***..\n", "7 7 2\n...*...\n.......\n...*...\n*.***.*\n...*...\n.......\n...*...\n" ]
[ "2 5\n1 5\n3 5\n2 4\n2 6\n", "-1\n", "4 4\n1 4\n7 4\n4 1\n4 7\n" ]
none
[ { "input": "5 6 1\n....*.\n...***\n....*.\n..*...\n.***..", "output": "2 5\n1 5\n3 5\n2 4\n2 6" }, { "input": "5 6 2\n....*.\n...***\n....*.\n..*...\n.***..", "output": "-1" }, { "input": "5 5 1\n.....\n.....\n.*..*\n*.*..\n....*", "output": "-1" }, { "input": "5 5 3\n*.***\n.****\n..***\n*.***\n.**.*", "output": "3 4\n2 4\n4 4\n3 3\n3 5" }, { "input": "10 10 6\n.*..**.**.\n**********\n****.*****\n**.***....\n***.******\n****.**..*\n**.**.****\n****.**..*\n..**.**.*.\n.*.*..***.", "output": "5 2\n4 2\n6 2\n5 1\n5 3" }, { "input": "10 10 59\n**.*******\n********.*\n***.******\n**.*******\n*******.**\n**********\n**.**.****\n.*********\n*****.****\n*********.", "output": "6 5\n3 5\n9 5\n6 2\n6 8" }, { "input": "10 10 40\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********", "output": "6 9\n5 9\n7 9\n6 8\n6 10" } ]
2,000
95,027,200
0
8,935
0
none
[ "none" ]
null
null
There are *n* points on a straight line, and the *i*-th point among them is located at *x**i*. All these coordinates are distinct. Determine the number *m* — the smallest number of points you should add on the line to make the distances between all neighboring points equal.
The first line contains a single integer *n* (3<=≤<=*n*<=≤<=100<=000) — the number of points. The second line contains a sequence of integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=109<=≤<=*x**i*<=≤<=109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
Print a single integer *m* — the smallest number of points you should add on the line to make the distances between all neighboring points equal.
[ "3\n-5 10 5\n", "6\n100 200 400 300 600 500\n", "4\n10 9 0 -1\n" ]
[ "1\n", "0\n", "8\n" ]
In the first example you can add one point with coordinate 0. In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
[ { "input": "3\n-5 10 5", "output": "1" }, { "input": "6\n100 200 400 300 600 500", "output": "0" }, { "input": "4\n10 9 0 -1", "output": "8" }, { "input": "3\n1 4 7", "output": "0" }, { "input": "3\n1 4 6", "output": "3" }, { "input": "3\n1 2 6", "output": "3" }, { "input": "3\n1 3 6", "output": "3" }, { "input": "4\n1 2 3 4", "output": "0" }, { "input": "3\n-1000000000 -999999999 1000000000", "output": "1999999998" }, { "input": "3\n-1000000000 999999999 1000000000", "output": "1999999998" }, { "input": "3\n-1000000000 -999999998 1000000000", "output": "999999998" }, { "input": "3\n-1000000000 999999998 1000000000", "output": "999999998" }, { "input": "3\n422800963 4663162 694989823", "output": "230108885" }, { "input": "5\n-268968800 -435386086 -484420288 579138544 945328473", "output": "204249819" }, { "input": "10\n711183437 845779129 -106125616 -481773790 66231250 -183390793 -711197523 -196001897 -440633306 -873649505", "output": "156311685" }, { "input": "3\n300000002 -799999998 -599999998", "output": "9" }, { "input": "5\n-166282087 234698547 -853072571 644571043 444292437", "output": "3533" }, { "input": "7\n996073710 -246878649 34663194 35526441 634003254 -704646201 -905166147", "output": "15411" }, { "input": "3\n-1000000000 1 1000000000", "output": "1999999998" }, { "input": "5\n-1000000000 1000000000 1 2 -30000", "output": "1999999996" }, { "input": "3\n422800963 0 1000000000", "output": "999999998" }, { "input": "3\n1000000000 500000001 -500000001", "output": "1499999999" } ]
62
7,065,600
0
8,938
86
Powerful array
[ "data structures", "implementation", "math", "two pointers" ]
D. Powerful array
5
256
An array of positive integers *a*1,<=*a*2,<=...,<=*a**n* is given. Let us consider its arbitrary subarray *a**l*,<=*a**l*<=+<=1...,<=*a**r*, where 1<=≤<=*l*<=≤<=*r*<=≤<=*n*. For every positive integer *s* denote by *K**s* the number of occurrences of *s* into the subarray. We call the power of the subarray the sum of products *K**s*·*K**s*·*s* for every positive integer *s*. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite. You should calculate the power of *t* given subarrays.
First line contains two integers *n* and *t* (1<=≤<=*n*,<=*t*<=≤<=200000) — the array length and the number of queries correspondingly. Second line contains *n* positive integers *a**i* (1<=≤<=*a**i*<=≤<=106) — the elements of the array. Next *t* lines contain two positive integers *l*, *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) each — the indices of the left and the right ends of the corresponding subarray.
Output *t* lines, the *i*-th line of the output should contain single positive integer — the power of the *i*-th query subarray. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).
[ "3 2\n1 2 1\n1 2\n1 3\n", "8 3\n1 1 2 2 1 3 1 1\n2 7\n1 6\n2 7\n" ]
[ "3\n6\n", "20\n20\n20\n" ]
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):
[ { "input": "3 2\n1 2 1\n1 2\n1 3", "output": "3\n6" }, { "input": "8 3\n1 1 2 2 1 3 1 1\n2 7\n1 6\n2 7", "output": "20\n20\n20" }, { "input": "20 8\n1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2\n4 15\n1 2\n2 20\n7 7\n13 18\n7 7\n3 19\n3 8", "output": "108\n3\n281\n1\n27\n1\n209\n27" }, { "input": "10 5\n10 9 8 7 6 5 4 3 2 1\n4 8\n1 10\n3 9\n2 2\n5 10", "output": "25\n55\n35\n9\n21" }, { "input": "8 10\n100 100 100 100 100 100 100 100\n1 8\n2 8\n3 8\n4 8\n5 8\n6 8\n7 8\n8 8\n1 1\n1 5", "output": "6400\n4900\n3600\n2500\n1600\n900\n400\n100\n100\n2500" }, { "input": "1 2\n1\n1 1\n1 1", "output": "1\n1" }, { "input": "1 1\n1000000\n1 1", "output": "1000000" }, { "input": "5 15\n103 45 103 67 45\n1 1\n1 2\n1 3\n1 4\n1 5\n2 2\n2 3\n2 4\n2 5\n3 3\n3 4\n3 5\n4 4\n4 5\n5 5", "output": "103\n148\n457\n524\n659\n45\n148\n215\n350\n103\n170\n215\n67\n112\n45" }, { "input": "8 10\n5 7 3 1 4 2 1 1\n5 7\n3 8\n2 7\n4 8\n4 5\n2 8\n4 6\n1 2\n4 7\n3 5", "output": "7\n18\n20\n15\n5\n25\n7\n12\n10\n8" }, { "input": "9 10\n5 3 10 2 4 8 7 5 1\n6 9\n1 8\n3 3\n2 7\n3 5\n4 6\n9 9\n8 8\n1 9\n5 6", "output": "21\n54\n10\n34\n16\n14\n1\n5\n55\n12" }, { "input": "9 10\n10 2 6 2 4 6 6 4 1\n4 8\n8 9\n2 3\n8 9\n9 9\n4 5\n1 7\n6 6\n6 8\n1 1", "output": "42\n5\n8\n5\n1\n6\n76\n6\n28\n10" }, { "input": "10 10\n5 10 9 2 9 3 5 5 6 5\n5 10\n2 2\n3 10\n4 8\n1 3\n1 5\n5 5\n10 10\n8 9\n6 10", "output": "63\n10\n92\n34\n24\n53\n9\n5\n11\n54" }, { "input": "9 10\n6 2 3 1 4 8 5 9 8\n3 9\n6 6\n9 9\n9 9\n9 9\n2 3\n3 9\n3 8\n6 9\n5 6", "output": "54\n8\n8\n8\n8\n5\n54\n30\n46\n12" } ]
186
0
-1
8,995
915
Almost Acyclic Graph
[ "dfs and similar", "graphs" ]
null
null
You are given a [directed graph](https://en.wikipedia.org/wiki/Directed_graph) consisting of *n* vertices and *m* edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it. Can you make this graph [acyclic](https://en.wikipedia.org/wiki/Directed_acyclic_graph) by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex).
The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=500, 1<=≤<=*m*<=≤<=*min*(*n*(*n*<=-<=1),<=100000)) — the number of vertices and the number of edges, respectively. Then *m* lines follow. Each line contains two integers *u* and *v* denoting a directed edge going from vertex *u* to vertex *v* (1<=≤<=*u*,<=*v*<=≤<=*n*, *u*<=≠<=*v*). Each ordered pair (*u*,<=*v*) is listed at most once (there is at most one directed edge from *u* to *v*).
If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.
[ "3 4\n1 2\n2 3\n3 2\n3 1\n", "5 6\n1 2\n2 3\n3 2\n3 1\n2 1\n4 5\n" ]
[ "YES\n", "NO\n" ]
In the first example you can remove edge <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and the graph becomes acyclic. In the second example you have to remove at least two edges (for example, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/420322fe5fba4eb3e3eba6886a2edb31f15762ce.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>) in order to make the graph acyclic.
[ { "input": "3 4\n1 2\n2 3\n3 2\n3 1", "output": "YES" }, { "input": "5 6\n1 2\n2 3\n3 2\n3 1\n2 1\n4 5", "output": "NO" }, { "input": "2 2\n1 2\n2 1", "output": "YES" }, { "input": "7 7\n1 3\n3 6\n3 7\n5 3\n6 2\n6 7\n7 2", "output": "YES" }, { "input": "500 50\n396 340\n47 341\n422 140\n492 209\n263 248\n461 300\n124 495\n33 6\n93 384\n389 182\n130 297\n217 329\n131 136\n355 94\n388 275\n115 368\n279 462\n126 285\n185 287\n223 221\n207 167\n203 127\n39 245\n394 444\n166 99\n399 328\n3 276\n142 325\n284 153\n65 3\n102 5\n459 168\n156 17\n99 162\n293 194\n493 198\n171 356\n269 155\n479 37\n269 336\n28 183\n363 43\n398 45\n142 68\n437 301\n150 353\n1 211\n326 340\n459 14\n90 441", "output": "YES" }, { "input": "4 5\n1 3\n3 2\n2 1\n3 4\n4 1", "output": "YES" }, { "input": "5 6\n1 3\n2 1\n3 5\n4 3\n5 4\n3 2", "output": "NO" }, { "input": "3 4\n1 2\n2 1\n1 3\n3 1", "output": "NO" }, { "input": "5 7\n1 2\n2 3\n3 1\n3 4\n4 1\n4 5\n5 1", "output": "YES" }, { "input": "4 6\n1 2\n2 3\n3 1\n3 2\n3 4\n4 2", "output": "YES" }, { "input": "4 5\n1 2\n2 3\n3 4\n4 1\n3 1", "output": "YES" }, { "input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 3", "output": "YES" }, { "input": "4 6\n2 3\n3 2\n3 4\n4 3\n4 2\n2 4", "output": "NO" }, { "input": "4 5\n1 2\n2 3\n2 4\n3 1\n4 1", "output": "YES" }, { "input": "4 5\n1 2\n2 1\n3 4\n4 3\n1 3", "output": "NO" }, { "input": "7 6\n2 3\n3 4\n4 2\n5 6\n6 7\n7 5", "output": "NO" }, { "input": "5 6\n1 2\n2 3\n3 4\n4 5\n5 1\n4 2", "output": "YES" }, { "input": "4 4\n1 2\n2 1\n3 4\n4 3", "output": "NO" }, { "input": "7 9\n1 2\n2 3\n1 3\n3 4\n3 5\n5 6\n6 1\n6 7\n7 1", "output": "YES" }, { "input": "8 7\n1 2\n2 3\n3 4\n4 1\n4 5\n5 6\n6 3", "output": "YES" }, { "input": "4 6\n1 2\n2 4\n2 3\n3 1\n4 3\n3 2", "output": "NO" }, { "input": "5 6\n1 2\n2 3\n3 4\n4 5\n4 1\n5 2", "output": "YES" }, { "input": "4 5\n2 4\n1 2\n2 1\n3 4\n4 3", "output": "NO" }, { "input": "6 8\n1 2\n2 3\n3 1\n2 4\n4 5\n5 1\n2 6\n6 1", "output": "YES" }, { "input": "6 8\n1 2\n2 3\n3 4\n4 1\n3 5\n5 6\n6 2\n1 3", "output": "NO" }, { "input": "6 7\n1 2\n2 5\n5 6\n6 1\n5 4\n4 3\n3 2", "output": "YES" }, { "input": "10 22\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5\n6 7\n6 8\n6 9\n6 10\n7 8\n7 9\n7 10\n8 9\n8 10\n9 10\n5 6\n10 1", "output": "YES" }, { "input": "4 6\n1 2\n2 3\n3 4\n4 1\n2 4\n3 1", "output": "YES" }, { "input": "5 7\n1 2\n2 3\n3 4\n4 5\n5 1\n1 3\n3 5", "output": "YES" }, { "input": "5 6\n1 2\n2 3\n3 4\n4 5\n4 2\n5 2", "output": "YES" }, { "input": "4 5\n2 3\n3 4\n4 2\n2 4\n3 2", "output": "NO" }, { "input": "7 8\n1 2\n2 3\n3 4\n4 5\n5 2\n3 6\n6 7\n7 2", "output": "YES" }, { "input": "4 5\n1 2\n2 3\n3 4\n4 1\n1 3", "output": "YES" }, { "input": "4 6\n1 2\n2 3\n3 4\n4 1\n2 4\n4 2", "output": "NO" }, { "input": "8 9\n2 6\n5 6\n5 2\n3 5\n4 5\n6 4\n1 2\n2 8\n2 3", "output": "NO" }, { "input": "8 10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 1\n5 4\n4 8\n8 5", "output": "NO" }, { "input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "NO" }, { "input": "5 8\n1 4\n1 5\n4 2\n4 3\n5 2\n5 3\n2 1\n3 1", "output": "NO" }, { "input": "5 6\n1 2\n2 3\n3 4\n4 5\n5 1\n1 4", "output": "YES" }, { "input": "4 5\n1 2\n2 4\n2 3\n3 1\n4 3", "output": "YES" }, { "input": "5 8\n4 3\n3 1\n4 1\n5 1\n5 2\n1 4\n1 3\n5 3", "output": "NO" }, { "input": "6 12\n2 1\n2 3\n2 4\n3 4\n4 1\n1 3\n1 5\n5 4\n6 5\n6 4\n6 1\n1 4", "output": "YES" }, { "input": "6 8\n1 2\n2 3\n3 4\n1 5\n5 6\n6 4\n4 1\n4 2", "output": "NO" }, { "input": "8 11\n5 1\n1 2\n1 6\n6 2\n2 3\n2 7\n7 3\n3 4\n3 8\n8 4\n4 1", "output": "YES" }, { "input": "4 6\n1 2\n2 3\n3 4\n4 1\n1 3\n3 1", "output": "NO" }, { "input": "5 8\n1 2\n1 3\n1 4\n2 3\n3 4\n3 5\n5 2\n5 1", "output": "YES" }, { "input": "4 5\n2 1\n1 3\n3 2\n3 4\n4 1", "output": "YES" }, { "input": "3 4\n3 2\n1 2\n2 3\n1 3", "output": "YES" }, { "input": "11 13\n1 2\n2 3\n3 4\n4 1\n1 5\n5 6\n6 7\n7 4\n3 8\n8 9\n9 10\n10 11\n11 2", "output": "NO" }, { "input": "5 8\n1 2\n2 3\n3 4\n4 5\n5 1\n4 1\n3 5\n1 3", "output": "NO" }, { "input": "8 10\n3 2\n1 5\n8 1\n1 2\n6 8\n3 8\n5 3\n2 4\n4 1\n4 3", "output": "NO" }, { "input": "10 14\n3 10\n10 9\n9 2\n8 3\n4 3\n4 2\n1 8\n7 1\n6 5\n2 7\n6 4\n5 8\n10 1\n8 10", "output": "YES" }, { "input": "5 6\n4 2\n3 5\n2 3\n5 4\n4 5\n3 4", "output": "NO" }, { "input": "3 3\n2 3\n2 1\n3 2", "output": "YES" }, { "input": "9 9\n1 2\n2 3\n3 4\n4 5\n5 1\n6 7\n7 8\n8 9\n9 6", "output": "NO" }, { "input": "10 15\n3 9\n2 3\n4 10\n6 4\n3 10\n6 10\n8 6\n6 2\n6 7\n9 4\n6 3\n10 7\n1 3\n8 1\n7 3", "output": "YES" }, { "input": "10 18\n10 3\n2 7\n2 5\n1 10\n4 3\n1 4\n6 10\n9 2\n5 10\n5 9\n1 9\n1 5\n2 3\n2 4\n10 4\n6 5\n8 5\n9 6", "output": "YES" }, { "input": "10 13\n3 5\n1 6\n9 6\n5 4\n4 7\n10 9\n8 7\n5 6\n2 10\n9 3\n2 4\n6 3\n3 10", "output": "NO" }, { "input": "10 16\n3 6\n5 6\n5 4\n3 2\n2 10\n1 7\n7 4\n6 2\n7 3\n4 6\n9 2\n9 7\n5 2\n10 9\n9 4\n7 8", "output": "YES" }, { "input": "10 10\n10 1\n6 9\n5 3\n9 4\n3 8\n2 1\n5 9\n8 10\n6 5\n10 5", "output": "YES" }, { "input": "5 9\n1 3\n1 4\n1 5\n2 1\n2 3\n2 4\n3 2\n5 2\n5 4", "output": "NO" }, { "input": "10 18\n4 10\n7 2\n2 1\n7 5\n5 6\n6 8\n3 9\n3 10\n6 9\n8 7\n4 3\n2 10\n9 5\n7 3\n6 4\n7 10\n10 5\n3 2", "output": "YES" }, { "input": "10 19\n5 9\n2 10\n3 7\n4 8\n4 2\n9 10\n3 6\n8 5\n6 10\n3 5\n4 1\n7 10\n8 9\n8 2\n7 9\n8 7\n9 1\n4 9\n8 10", "output": "YES" }, { "input": "5 5\n1 2\n2 1\n3 4\n3 5\n4 5", "output": "YES" }, { "input": "10 17\n5 6\n4 9\n7 1\n6 10\n3 10\n4 10\n9 3\n8 1\n2 4\n1 9\n3 7\n4 7\n6 2\n5 4\n3 8\n10 9\n7 10", "output": "YES" }, { "input": "10 13\n7 2\n7 10\n10 5\n2 9\n10 4\n8 3\n4 5\n1 8\n7 8\n5 7\n2 10\n9 6\n5 9", "output": "YES" }, { "input": "6 7\n1 2\n3 4\n4 5\n4 6\n5 6\n6 4\n6 3", "output": "NO" }, { "input": "6 8\n1 2\n2 3\n3 4\n4 5\n5 6\n6 1\n1 3\n4 6", "output": "YES" }, { "input": "10 9\n7 2\n10 5\n9 1\n1 5\n4 6\n1 10\n6 2\n10 9\n5 9", "output": "YES" }, { "input": "10 14\n8 2\n10 6\n6 1\n8 10\n6 2\n1 10\n4 7\n1 7\n9 1\n3 6\n1 4\n7 6\n10 4\n8 4", "output": "YES" }, { "input": "10 19\n10 3\n9 2\n7 4\n6 3\n1 6\n6 5\n2 8\n6 9\n1 5\n9 8\n10 9\n1 8\n3 2\n5 2\n7 10\n8 7\n3 4\n2 4\n4 1", "output": "NO" }, { "input": "10 14\n10 1\n8 9\n7 2\n8 2\n7 3\n7 10\n2 10\n6 3\n4 1\n6 5\n7 8\n10 6\n1 2\n8 10", "output": "YES" }, { "input": "10 19\n10 9\n1 2\n3 6\n9 6\n2 6\n3 7\n2 10\n3 8\n2 9\n2 8\n4 7\n2 7\n6 7\n10 5\n8 1\n6 10\n8 5\n8 6\n3 2", "output": "NO" }, { "input": "10 18\n8 2\n9 2\n7 4\n2 6\n7 1\n5 3\n9 4\n3 9\n3 8\n10 2\n10 1\n9 1\n6 7\n10 6\n5 6\n9 6\n7 5\n7 9", "output": "YES" }, { "input": "8 13\n3 5\n6 2\n5 3\n8 3\n5 7\n6 4\n5 1\n7 6\n3 1\n7 2\n4 8\n4 1\n3 6", "output": "NO" }, { "input": "7 7\n5 1\n3 7\n4 3\n1 5\n7 5\n3 6\n1 6", "output": "YES" }, { "input": "3 4\n3 1\n3 2\n1 3\n1 2", "output": "YES" }, { "input": "5 10\n1 3\n3 1\n2 3\n1 4\n2 4\n2 1\n5 3\n5 1\n4 1\n3 5", "output": "NO" }, { "input": "5 6\n2 1\n3 2\n1 2\n2 3\n1 5\n3 1", "output": "NO" }, { "input": "6 7\n6 2\n5 4\n2 1\n5 2\n6 5\n1 5\n5 6", "output": "NO" }, { "input": "9 12\n1 2\n2 3\n2 4\n4 5\n3 5\n5 6\n6 7\n6 8\n7 9\n8 9\n9 1\n3 6", "output": "YES" }, { "input": "4 6\n1 2\n1 3\n3 4\n4 2\n4 1\n2 3", "output": "YES" }, { "input": "5 7\n1 2\n2 3\n3 1\n2 4\n4 1\n3 5\n5 2", "output": "NO" }, { "input": "7 10\n1 5\n6 2\n2 7\n6 3\n5 7\n1 2\n3 5\n4 3\n5 2\n7 5", "output": "YES" }, { "input": "8 11\n8 4\n3 6\n1 2\n8 1\n7 2\n4 3\n7 4\n3 1\n2 6\n4 5\n2 3", "output": "YES" }, { "input": "7 16\n6 4\n5 1\n6 1\n3 7\n3 1\n5 4\n6 3\n2 7\n6 2\n1 4\n5 2\n4 7\n1 7\n6 5\n7 5\n2 4", "output": "YES" }, { "input": "7 16\n1 7\n4 7\n2 3\n5 1\n6 1\n5 4\n3 1\n4 6\n2 1\n6 7\n4 1\n2 7\n3 4\n3 7\n7 2\n6 2", "output": "NO" }, { "input": "4 7\n1 2\n3 4\n3 2\n1 4\n4 1\n4 2\n1 3", "output": "YES" }, { "input": "500 13\n1 2\n2 3\n3 4\n4 1\n1 5\n5 6\n6 7\n7 4\n3 8\n8 9\n9 10\n10 11\n11 2", "output": "NO" } ]
1,000
21,299,200
0
9,002
691
Exponential notation
[ "implementation", "strings" ]
null
null
You are given a positive decimal number *x*. Your task is to convert it to the "simple exponential notation". Let *x*<==<=*a*·10*b*, where 1<=≤<=*a*<=&lt;<=10, then in general case the "simple exponential notation" looks like "aEb". If *b* equals to zero, the part "Eb" should be skipped. If *a* is an integer, it should be written without decimal point. Also there should not be extra zeroes in *a* and *b*.
The only line contains the positive decimal number *x*. The length of the line will not exceed 106. Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other.
Print the only line — the "simple exponential notation" of the given number *x*.
[ "16\n", "01.23400\n", ".100\n", "100.\n" ]
[ "1.6E1\n", "1.234\n", "1E-1\n", "1E2\n" ]
none
[ { "input": "16", "output": "1.6E1" }, { "input": "01.23400", "output": "1.234" }, { "input": ".100", "output": "1E-1" }, { "input": "100.", "output": "1E2" }, { "input": "9000", "output": "9E3" }, { "input": "0.0012", "output": "1.2E-3" }, { "input": "0001100", "output": "1.1E3" }, { "input": "1", "output": "1" }, { "input": "1.0000", "output": "1" }, { "input": "2206815224318443962208128404511577750057653265995300414539703580103256087275661997018352502651118684", "output": "2.206815224318443962208128404511577750057653265995300414539703580103256087275661997018352502651118684E99" }, { "input": ".642190250125247518637240673193254850619739079359757454472743329719747684651927659872735961709249479", "output": "6.42190250125247518637240673193254850619739079359757454472743329719747684651927659872735961709249479E-1" }, { "input": "143529100720960530144687499862369157252883621496987867683546098241081752607457981824764693332677189.", "output": "1.43529100720960530144687499862369157252883621496987867683546098241081752607457981824764693332677189E98" }, { "input": "5649388306043547446322173224045662327678394712363.27277681139968970424738731716530805786323956813790", "output": "5.6493883060435474463221732240456623276783947123632727768113996897042473873171653080578632395681379E48" }, { "input": "0.1", "output": "1E-1" }, { "input": ".1", "output": "1E-1" }, { "input": "1.", "output": "1" }, { "input": "0.111", "output": "1.11E-1" }, { "input": ".111", "output": "1.11E-1" }, { "input": "1.1", "output": "1.1" }, { "input": "01.1", "output": "1.1" }, { "input": "1.10", "output": "1.1" }, { "input": "01.10", "output": "1.1" }, { "input": "10.0", "output": "1E1" }, { "input": "16.00", "output": "1.6E1" }, { "input": "0016.", "output": "1.6E1" }, { "input": ".000016", "output": "1.6E-5" }, { "input": "16000.000", "output": "1.6E4" }, { "input": "016.00", "output": "1.6E1" }, { "input": "0016.00", "output": "1.6E1" }, { "input": "0.16", "output": "1.6E-1" }, { "input": "00.16", "output": "1.6E-1" }, { "input": "00.160", "output": "1.6E-1" } ]
312
10,752,000
3
9,005
799
T-shirt buying
[ "data structures", "implementation" ]
null
null
A new pack of *n* t-shirts came to a shop. Each of the t-shirts is characterized by three integers *p**i*, *a**i* and *b**i*, where *p**i* is the price of the *i*-th t-shirt, *a**i* is front color of the *i*-th t-shirt and *b**i* is back color of the *i*-th t-shirt. All values *p**i* are distinct, and values *a**i* and *b**i* are integers from 1 to 3. *m* buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the *j*-th buyer we know his favorite color *c**j*. A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served. You are to compute the prices each buyer will pay for t-shirts.
The first line contains single integer *n* (1<=≤<=*n*<=≤<=200<=000) — the number of t-shirts. The following line contains sequence of integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=1<=000<=000<=000), where *p**i* equals to the price of the *i*-th t-shirt. The following line contains sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=3), where *a**i* equals to the front color of the *i*-th t-shirt. The following line contains sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≤<=*b**i*<=≤<=3), where *b**i* equals to the back color of the *i*-th t-shirt. The next line contains single integer *m* (1<=≤<=*m*<=≤<=200<=000) — the number of buyers. The following line contains sequence *c*1,<=*c*2,<=...,<=*c**m* (1<=≤<=*c**j*<=≤<=3), where *c**j* equals to the favorite color of the *j*-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Print to the first line *m* integers — the *j*-th integer should be equal to the price of the t-shirt which the *j*-th buyer will buy. If the *j*-th buyer won't buy anything, print -1.
[ "5\n300 200 400 500 911\n1 2 1 2 3\n2 1 3 2 1\n6\n2 3 1 2 1 1\n", "2\n1000000000 1\n1 1\n1 2\n2\n2 1\n" ]
[ "200 400 300 500 911 -1 \n", "1 1000000000 \n" ]
none
[ { "input": "5\n300 200 400 500 911\n1 2 1 2 3\n2 1 3 2 1\n6\n2 3 1 2 1 1", "output": "200 400 300 500 911 -1 " }, { "input": "2\n1000000000 1\n1 1\n1 2\n2\n2 1", "output": "1 1000000000 " }, { "input": "10\n251034796 163562337 995167403 531046374 341924810 828969071 971837553 183763940 857690534 687685084\n3 2 1 3 2 3 1 3 2 1\n2 3 3 1 2 3 2 3 3 2\n10\n1 3 2 3 2 3 3 1 2 3", "output": "531046374 163562337 251034796 183763940 341924810 828969071 857690534 687685084 971837553 995167403 " }, { "input": "20\n414468312 20329584 106106409 584924603 666547477 670032002 726095027 276840253 368277336 940941705 531635095 213813062 440421387 959075599 240727854 495316522 838268432 786936631 586382273 806443734\n3 1 2 3 3 2 2 1 3 2 3 2 3 3 3 2 1 3 1 2\n3 1 2 2 2 2 3 1 2 3 2 1 1 2 3 1 2 3 3 2\n40\n1 1 2 1 3 2 3 1 3 3 1 2 3 1 1 1 2 3 3 1 3 1 3 1 2 2 3 3 1 2 1 2 3 2 2 1 2 1 2 2", "output": "20329584 213813062 106106409 276840253 240727854 368277336 414468312 440421387 531635095 584924603 495316522 666547477 586382273 838268432 -1 -1 670032002 726095027 786936631 -1 940941705 -1 959075599 -1 806443734 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 " }, { "input": "1\n529469903\n1\n3\n1\n3", "output": "529469903 " } ]
3,000
24,576,000
0
9,041
837
Vasya's Function
[ "binary search", "implementation", "math" ]
null
null
Vasya is studying number theory. He has denoted a function *f*(*a*,<=*b*) such that: - *f*(*a*,<=0)<==<=0; - *f*(*a*,<=*b*)<==<=1<=+<=*f*(*a*,<=*b*<=-<=*gcd*(*a*,<=*b*)), where *gcd*(*a*,<=*b*) is the greatest common divisor of *a* and *b*. Vasya has two numbers *x* and *y*, and he wants to calculate *f*(*x*,<=*y*). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly.
The first line contains two integer numbers *x* and *y* (1<=≤<=*x*,<=*y*<=≤<=1012).
Print *f*(*x*,<=*y*).
[ "3 5\n", "6 3\n" ]
[ "3\n", "1\n" ]
none
[ { "input": "3 5", "output": "3" }, { "input": "6 3", "output": "1" }, { "input": "1000000009 1000000008", "output": "1000000008" }, { "input": "1000000007 1000000006", "output": "1000000006" }, { "input": "2000000018 2000000017", "output": "1000000009" }, { "input": "1000000000000 1", "output": "1" }, { "input": "1000000000000 1000000000000", "output": "1" }, { "input": "1 1000000000000", "output": "1000000000000" }, { "input": "100000000000 100000000000", "output": "1" }, { "input": "1 100000000000", "output": "100000000000" }, { "input": "100000000000 1", "output": "1" }, { "input": "1000000009 1000000000000", "output": "999992008" }, { "input": "1000000000000 1000000007", "output": "4" }, { "input": "124556361363 136616361", "output": "1617" }, { "input": "153136316 5153643", "output": "1288412" }, { "input": "15316888 315347573", "output": "59298" }, { "input": "153907320131 11351356", "output": "16996" }, { "input": "3 135415909531", "output": "45138636511" }, { "input": "1 157831805135", "output": "157831805135" }, { "input": "1000000009 1000000010", "output": "2" }, { "input": "767389814 1136900240", "output": "14254" }, { "input": "999966000289 999966000288", "output": "1999964" }, { "input": "150917076326 287596534405", "output": "14306025" }, { "input": "49544527863 318162327511", "output": "6965053451" }, { "input": "999999999989 999999999988", "output": "999999999988" }, { "input": "339860248091 167735311934", "output": "1843245188" }, { "input": "414654652183 366894205623", "output": "366894205623" }, { "input": "450002679907 706296532001", "output": "55285" }, { "input": "243220976099 419527537895", "output": "580057" }, { "input": "3 100000007", "output": "33333337" }, { "input": "999962000357 100000000000", "output": "200044" }, { "input": "1000000007 1000000000000", "output": "999994006" }, { "input": "963761198400 999999999997", "output": "20" }, { "input": "3999999979 3999999978", "output": "3999999978" }, { "input": "154210543621 542105421054", "output": "96099620" }, { "input": "191480607107 629918602611", "output": "55476781293" }, { "input": "516832075292 844855235404", "output": "103412121" }, { "input": "598718273423 543198266606", "output": "1769375540" }, { "input": "963761198400 787405476727", "output": "45" }, { "input": "283286197375 459489599842", "output": "1409627228" }, { "input": "963761198400 33129788784", "output": "30" }, { "input": "104338884626 894039957000", "output": "40428" }, { "input": "963761198400 394879907912", "output": "21" }, { "input": "324161862590 324161862595", "output": "2" }, { "input": "450002679907 2", "output": "2" }, { "input": "999999999958 999999999957", "output": "499999999979" } ]
77
5,734,400
0
9,043
0
none
[ "none" ]
null
null
Alyona has a tree with *n* vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex *i* she wrote *a**i*. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges). Let's define *dist*(*v*,<=*u*) as the sum of the integers written on the edges of the simple path from *v* to *u*. The vertex *v* controls the vertex *u* (*v*<=≠<=*u*) if and only if *u* is in the subtree of *v* and *dist*(*v*,<=*u*)<=≤<=*a**u*. Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex *v* what is the number of vertices *u* such that *v* controls *u*.
The first line contains single integer *n* (1<=≤<=*n*<=≤<=2·105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the integers written in the vertices. The next (*n*<=-<=1) lines contain two integers each. The *i*-th of these lines contains integers *p**i* and *w**i* (1<=≤<=*p**i*<=≤<=*n*, 1<=≤<=*w**i*<=≤<=109) — the parent of the (*i*<=+<=1)-th vertex in the tree and the number written on the edge between *p**i* and (*i*<=+<=1). It is guaranteed that the given graph is a tree.
Print *n* integers — the *i*-th of these numbers should be equal to the number of vertices that the *i*-th vertex controls.
[ "5\n2 5 1 4 6\n1 7\n1 1\n3 5\n3 6\n", "5\n9 7 8 6 5\n1 1\n2 1\n3 1\n4 1\n" ]
[ "1 0 1 0 0\n", "4 3 2 1 0\n" ]
In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1 controls the vertex 5).
[ { "input": "5\n2 5 1 4 6\n1 7\n1 1\n3 5\n3 6", "output": "1 0 1 0 0" }, { "input": "5\n9 7 8 6 5\n1 1\n2 1\n3 1\n4 1", "output": "4 3 2 1 0" }, { "input": "1\n1", "output": "0" }, { "input": "2\n1 1\n1 1", "output": "1 0" }, { "input": "10\n40 77 65 14 86 16 2 51 62 79\n1 75\n10 86\n3 52\n6 51\n10 8\n3 61\n3 53\n5 98\n2 7", "output": "1 3 0 0 0 1 0 0 0 2" }, { "input": "10\n52 1 84 16 59 26 56 74 52 97\n5 7\n7 13\n3 98\n7 22\n7 19\n9 54\n4 45\n10 95\n1 94", "output": "1 0 0 1 0 0 3 0 2 0" }, { "input": "10\n68 29 12 14 27 47 95 100 45 14\n10 42\n9 52\n3 44\n2 81\n5 34\n3 46\n6 40\n8 89\n1 85", "output": "0 0 1 0 2 1 0 0 0 0" }, { "input": "10\n84 65 39 20 8 52 49 18 35 32\n3 70\n9 79\n1 99\n3 49\n4 41\n3 43\n3 35\n4 83\n2 72", "output": "0 0 1 1 0 0 0 0 0 0" }, { "input": "10\n96 92 63 25 80 74 95 41 28 54\n6 98\n1 11\n5 45\n3 12\n7 63\n4 39\n7 31\n8 81\n2 59", "output": "2 0 1 1 1 0 2 0 0 0" }, { "input": "10\n4 24 86 31 49 87 42 75 18 71\n4 37\n5 46\n9 88\n1 75\n10 74\n5 32\n4 22\n7 79\n8 50", "output": "0 0 0 1 2 0 0 1 0 1" }, { "input": "10\n19 48 18 37 34 1 96 98 3 85\n7 65\n2 77\n6 34\n3 39\n1 85\n6 24\n2 9\n3 73\n2 41", "output": "0 2 0 0 0 3 1 0 0 0" }, { "input": "10\n31 83 37 43 2 14 39 24 93 7\n6 1\n9 17\n8 84\n3 6\n4 100\n5 21\n1 9\n6 67\n2 29", "output": "1 0 1 0 1 2 0 0 1 0" }, { "input": "10\n47 7 65 49 75 36 93 47 86 24\n3 28\n4 40\n1 35\n3 65\n3 11\n2 17\n5 96\n2 60\n8 24", "output": "1 2 3 2 0 0 0 1 0 0" }, { "input": "10\n1 65 76 59 21 58 97 37 30 84\n6 4\n7 28\n9 19\n2 65\n1 53\n5 10\n5 42\n10 72\n2 89", "output": "2 1 0 0 2 2 1 0 1 0" }, { "input": "5\n1 1 1 1 1\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000", "output": "0 0 0 0 0" }, { "input": "6\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000\n5 1000000000", "output": "1 1 1 1 1 0" }, { "input": "10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000\n5 1000000000\n6 1000000000\n7 1000000000\n8 1000000000\n9 1000000000", "output": "1 1 1 1 1 1 1 1 1 0" }, { "input": "10\n1 1 1 1 1 1 1 1 1 1\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000\n5 1000000000\n6 1000000000\n7 1000000000\n8 1000000000\n9 1000000000", "output": "0 0 0 0 0 0 0 0 0 0" }, { "input": "10\n500000000 500000000 500000000 500000000 500000000 500000000 500000000 500000000 500000000 500000000\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000\n5 1000000000\n6 1000000000\n7 1000000000\n8 1000000000\n9 1000000000", "output": "0 0 0 0 0 0 0 0 0 0" }, { "input": "5\n1000000000 1000000000 1000000000 1000000000 1000000000\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000", "output": "1 1 1 1 0" } ]
46
0
-1
9,048
316
Special Task
[ "math" ]
null
null
Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there. The content special agent has got an important task: to get the latest research by British scientists on the English Language. These developments are encoded and stored in a large safe. The Beaver's teeth are strong enough, so the authorities assured that upon arriving at the place the beaver won't have any problems with opening the safe. And he finishes his aspen sprig and leaves for this important task. Of course, the Beaver arrived at the location without any problems, but alas. He can't open the safe with his strong and big teeth. At this point, the Smart Beaver get a call from the headquarters and learns that opening the safe with the teeth is not necessary, as a reliable source has sent the following information: the safe code consists of digits and has no leading zeroes. There also is a special hint, which can be used to open the safe. The hint is string *s* with the following structure: - if *s**i* = "?", then the digit that goes *i*-th in the safe code can be anything (between 0 to 9, inclusively); - if *s**i* is a digit (between 0 to 9, inclusively), then it means that there is digit *s**i* on position *i* in code; - if the string contains letters from "A" to "J", then all positions with the same letters must contain the same digits and the positions with distinct letters must contain distinct digits. - The length of the safe code coincides with the length of the hint. For example, hint "?JGJ9" has such matching safe code variants: "51919", "55959", "12329", "93539" and so on, and has wrong variants such as: "56669", "00111", "03539" and "13666". After receiving such information, the authorities change the plan and ask the special agents to work quietly and gently and not to try to open the safe by mechanical means, and try to find the password using the given hint. At a special agent school the Smart Beaver was the fastest in his platoon finding codes for such safes, but now he is not in that shape: the years take their toll ... Help him to determine the number of possible variants of the code to the safe, matching the given hint. After receiving this information, and knowing his own speed of entering codes, the Smart Beaver will be able to determine whether he will have time for tonight's show "Beavers are on the trail" on his favorite TV channel, or he should work for a sleepless night...
The first line contains string *s* — the hint to the safe code. String *s* consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string *s* doesn't equal to character 0. The input limits for scoring 30 points are (subproblem A1): - 1<=≤<=|*s*|<=≤<=5. The input limits for scoring 100 points are (subproblems A1+A2): - 1<=≤<=|*s*|<=≤<=105. Here |*s*| means the length of string *s*.
Print the number of codes that match the given hint.
[ "AJ\n", "1?AA\n" ]
[ "81\n", "100\n" ]
none
[ { "input": "AJ", "output": "81" }, { "input": "1?AA", "output": "100" }, { "input": "?", "output": "9" }, { "input": "7", "output": "1" }, { "input": "A", "output": "9" }, { "input": "BBB?", "output": "90" }, { "input": "BC??", "output": "8100" }, { "input": "CC", "output": "9" }, { "input": "CB?", "output": "810" }, { "input": "B??C?", "output": "81000" }, { "input": "BB?C?", "output": "8100" }, { "input": "?BCB?", "output": "8100" }, { "input": "?C", "output": "90" }, { "input": "??C?C", "output": "9000" }, { "input": "???2", "output": "900" }, { "input": "9???", "output": "1000" }, { "input": "GJH2?", "output": "6480" }, { "input": "7I9G4", "output": "90" }, { "input": "JG50?", "output": "810" }, { "input": "CDEFG", "output": "27216" }, { "input": "1023456789??????????????????????????????????????ABCDIFGHIJ", "output": "362880000000000000000000000000000000000000000" } ]
372
819,200
-1
9,049
923
Perfect Security
[ "data structures", "greedy", "strings", "trees" ]
null
null
Alice has a very important message *M* consisting of some non-negative integers that she wants to keep secret from Eve. Alice knows that the only theoretically secure cipher is one-time pad. Alice generates a random key *K* of the length equal to the message's length. Alice computes the bitwise xor of each element of the message and the key (, where denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)) and stores this encrypted message *A*. Alice is smart. Be like Alice. For example, Alice may have wanted to store a message *M*<==<=(0,<=15,<=9,<=18). She generated a key *K*<==<=(16,<=7,<=6,<=3). The encrypted message is thus *A*<==<=(16,<=8,<=15,<=17). Alice realised that she cannot store the key with the encrypted message. Alice sent her key *K* to Bob and deleted her own copy. Alice is smart. Really, be like Alice. Bob realised that the encrypted message is only secure as long as the key is secret. Bob thus randomly permuted the key before storing it. Bob thinks that this way, even if Eve gets both the encrypted message and the key, she will not be able to read the message. Bob is not smart. Don't be like Bob. In the above example, Bob may have, for instance, selected a permutation (3,<=4,<=1,<=2) and stored the permuted key *P*<==<=(6,<=3,<=16,<=7). One year has passed and Alice wants to decrypt her message. Only now Bob has realised that this is impossible. As he has permuted the key randomly, the message is lost forever. Did we mention that Bob isn't smart? Bob wants to salvage at least some information from the message. Since he is not so smart, he asks for your help. You know the encrypted message *A* and the permuted key *P*. What is the lexicographically smallest message that could have resulted in the given encrypted text? More precisely, for given *A* and *P*, find the lexicographically smallest message *O*, for which there exists a permutation π such that for every *i*. Note that the sequence *S* is lexicographically smaller than the sequence *T*, if there is an index *i* such that *S**i*<=&lt;<=*T**i* and for all *j*<=&lt;<=*i* the condition *S**j*<==<=*T**j* holds.
The first line contains a single integer *N* (1<=≤<=*N*<=≤<=300000), the length of the message. The second line contains *N* integers *A*1,<=*A*2,<=...,<=*A**N* (0<=≤<=*A**i*<=&lt;<=230) representing the encrypted message. The third line contains *N* integers *P*1,<=*P*2,<=...,<=*P**N* (0<=≤<=*P**i*<=&lt;<=230) representing the permuted encryption key.
Output a single line with *N* integers, the lexicographically smallest possible message *O*. Note that all its elements should be non-negative.
[ "3\n8 4 13\n17 2 7\n", "5\n12 7 87 22 11\n18 39 9 12 16\n", "10\n331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951\n226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667\n" ]
[ "10 3 28\n", "0 14 69 6 44\n", "128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284\n" ]
In the first case, the solution is (10, 3, 28), since <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a896b30a69636d1bfbfa981eae10650f5fee843c.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e383e4333ea37c4652ce2ac1ccfc2cfcf96e0896.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c24ed3c6f88805eb3710487b3fe07ff64034151a.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Other possible permutations of key yield messages (25, 6, 10), (25, 3, 15), (10, 21, 10), (15, 21, 15) and (15, 6, 28), which are all lexicographically larger than the solution.
[ { "input": "3\n8 4 13\n17 2 7", "output": "10 3 28" }, { "input": "5\n12 7 87 22 11\n18 39 9 12 16", "output": "0 14 69 6 44" }, { "input": "10\n331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951\n226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667", "output": "128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284" }, { "input": "5\n134 246 57 176 239\n14 83 97 175 187", "output": "41 77 55 209 188" }, { "input": "10\n241 187 20 18 151 144 238 193 86 63\n18 69 86 91 111 118 124 172 227 253", "output": "12 23 6 68 116 203 129 132 32 67" }, { "input": "4\n0 0 0 0\n0 0 0 0", "output": "0 0 0 0" }, { "input": "4\n5 5 3 3\n5 3 3 7", "output": "0 2 0 0" } ]
3,500
161,996,800
0
9,058
383
Vowels
[ "combinatorics", "divide and conquer", "dp" ]
null
null
Iahubina is tired of so many complicated languages, so she decided to invent a new, simple language. She already made a dictionary consisting of *n* 3-words. A 3-word is a sequence of exactly 3 lowercase letters of the first 24 letters of the English alphabet (*a* to *x*). She decided that some of the letters are vowels, and all the others are consonants. The whole language is based on a simple rule: any word that contains at least one vowel is correct. Iahubina forgot which letters are the vowels, and wants to find some possible correct sets of vowels. She asks Iahub questions. In each question, she will give Iahub a set of letters considered vowels (in this question). For each question she wants to know how many words of the dictionary are correct, considering the given set of vowels. Iahubina wants to know the *xor* of the squared answers to all the possible questions. There are 224 different questions, they are all subsets of the set of the first 24 letters of the English alphabet. Help Iahub find that number.
The first line contains one integer, *n* (1<=≤<=*n*<=≤<=104). Each of the next *n* lines contains a 3-word consisting of 3 lowercase letters. There will be no two identical 3-words.
Print one number, the *xor* of the squared answers to the queries.
[ "5\nabc\naaa\nada\nbcd\ndef\n" ]
[ "0\n" ]
none
[ { "input": "5\nabc\naaa\nada\nbcd\ndef", "output": "0" }, { "input": "100\namd\namj\natr\nbcp\nbjm\ncna\ncpj\ncse\ndij\ndjp\ndlv\nebk\nedf\nelw\nfbr\nfcl\nfhs\nflo\nfmj\ngcg\ngen\nghg\ngvb\ngxx\nhbe\nhbf\nhgu\nhlv\nhqa\nibg\nifp\nima\nitt\nivl\nixu\njle\njli\nket\nkit\nkws\nlep\nles\nleu\nmbp\nmci\nmdv\nmhf\nmih\nmll\nmop\nndp\nnfs\nngl\nnng\noic\nomo\nooj\noti\npax\npfo\npjd\npup\nqer\nrad\nrdg\nrfq\nrvt\nrwa\nrxj\nshc\nsjv\nswx\ntcu\ntlm\ntmb\ntml\ntmw\ntvr\ntvx\nuid\nuir\nukf\nulg\nvce\nves\nvfb\nvok\nvut\nvvi\nvwb\nwab\nwba\nwdf\nweq\nwog\nwsl\nxbk\nxiq\nxop\nxpp", "output": "13888" }, { "input": "100\naip\nbfl\nbld\nblh\nbpk\nbqd\nbtk\ncfu\nciv\nckf\ncog\ncro\nctt\ncve\ncvn\ndlj\neer\negw\negx\nffi\nfld\nggk\ngis\ngkv\ngnq\ngvj\nhdo\nhgf\nhgu\nhjt\nhla\nhni\nhnk\nifa\niir\niml\njfa\njgl\nkbf\nliv\nlqo\nmlw\nmot\nmpx\nnas\nnlo\nobt\nodo\nodx\nolr\nolw\nonc\npac\npdp\nphn\npku\npng\npsd\nptl\npuq\npvk\npvx\nqjj\nqju\nqpf\nqqv\nqsf\nrac\nrgj\nrrg\nsbm\nsdf\nsif\nsil\nsnv\nspt\nsxt\ntou\nttj\nufi\nuht\nujm\null\nupm\nuqf\nvof\nvpq\nwae\nwck\nwed\nwhd\nwjn\nwpp\nwvd\nxbx\nxdv\nxeh\nxmq\nxsm\nxsp", "output": "8624" }, { "input": "10\nhjk\nkkw\nmsw\nnht\noqu\npcx\npet\nshd\nutb\nwbw", "output": "0" }, { "input": "20\netf\nffq\ngqe\nhpj\nido\niep\nkbv\nlgs\nlkl\nlvg\nmhs\nocr\nonc\nonv\npmv\nqhk\nrck\nrgj\nsib\nuox", "output": "0" }, { "input": "30\nagf\naov\ncac\ncdq\nclc\ncue\ndmh\ndrr\ndxv\nfrv\njmg\nkih\nkii\nkqm\nkwc\nnri\nohw\nrfk\nrrd\nrrk\ntmp\ntsc\nuhg\nuhx\nujw\nvms\nvrg\nwer\nxml\nxuv", "output": "0" }, { "input": "40\nbhw\nblh\ncal\nccg\ncdd\ncsm\ndir\ndux\nefp\nfnw\ngcr\nhuc\niaf\nipv\niva\niwl\njeb\njwk\nlot\nmcf\nmnk\nnak\nopl\norb\noxj\nqws\nrbl\nsmo\nsuw\nsws\ntgt\numg\nvhn\nvud\nwml\nwqg\nxbv\nxgj\nxlm\nxxv", "output": "944" }, { "input": "50\nagj\nbnk\nbtg\ncqt\ncxs\ndjv\neft\neqt\nfbf\nfbp\nfko\nfrg\ngdb\ngdw\ngie\ngvv\nhdw\nijo\nixc\njif\njph\nkad\nkje\nlel\nles\nlhw\nlkw\nmht\nnii\nnsb\nnuo\nnwp\nolm\nomb\noti\notm\nove\npnl\npqf\npwc\nrfq\nrkl\nsrm\nthb\ntje\ntpw\nugo\nwhk\nwwq\nxpx", "output": "1184" }, { "input": "50\naah\naoh\naqc\nauo\ncnk\ndfa\ndok\nfvd\nhxk\nibb\nicl\nigj\nird\njjv\njmv\nkbo\nkgj\nkji\nkxp\nlnf\nlqe\nndq\nnoi\nohh\noro\npdg\npio\npjq\npkw\npsg\npvt\nqdi\nqmo\nrba\nrkh\nrpk\nrrm\nrxs\nssu\ntcn\ntea\ntjb\ntkr\nuuh\nvmn\nvqd\nwaj\nwnl\nwqp\nxtw", "output": "2736" }, { "input": "50\nabh\navn\nbrx\ndcp\ndqe\nedr\neub\nfmg\ngda\ngmm\ngpn\nhbd\nhnw\nhta\nhuk\nhun\nieo\nifc\niwn\nixm\njpc\njsr\nkrj\nksc\nlie\nljj\nllb\nlqp\nmap\nmkx\nnob\nogl\nokh\noxq\npqu\npxk\nqfv\nqkt\nrjw\nseu\ntpe\nupe\nvlk\nwbw\nwce\nxae\nxqk\nxsv\nxve\nxvk", "output": "224" }, { "input": "50\nbpx\ncpq\ncqo\ndct\ndhh\ndid\ndlr\ndpl\neie\nesj\nfnc\nfse\nfxp\ngat\nghq\ngmg\nhan\nhdq\nhqn\nhse\nhwt\nibk\njbg\njda\nkgi\nkrr\nkrt\nkvo\nlwe\nmuh\nmve\nnfp\noac\nodw\nofq\npdr\nqlr\nrjm\nsdl\nsfj\nshs\ntae\ntdt\nual\nukf\nuup\nvkw\nvnj\nwbh\nxsp", "output": "3200" }, { "input": "50\nbfu\nbqa\ncew\nclt\ncnx\ncor\ncvq\nddq\ndgm\ndme\nehr\neua\newd\nfhq\nhep\nill\njmp\njnc\njng\njts\njtt\njww\nkei\nkjr\nkmk\nkoq\nkxi\nmgu\nnbb\nnqa\nnrp\nntq\nnwg\nost\notf\noxc\npia\nqgo\nqli\nqqa\nrrx\nrug\nsaj\nsjc\ntqm\nvoh\nvoo\nvwd\nwke\nwqg", "output": "2432" }, { "input": "100\nacs\nako\naqn\navw\naxm\nbea\nbmw\nbro\nbrw\nbvn\nciv\ncpn\ndas\ndex\ndjo\ndwq\neat\nedq\negu\neqw\nfkt\nflt\nfqv\nfrf\nfwg\ngab\nhcs\nhfw\nhoq\nhwu\nicq\niji\nins\nirs\nivn\njga\njng\nkcq\nkfe\nkox\nkps\nkts\nlmt\nlok\nlvm\nlwt\nmfd\nmlc\nmnm\nmwu\nnad\nnai\nnot\nogr\nope\noqm\nosd\novq\nprj\nqad\nqoh\nqqk\nrnq\nrqx\nrsh\nrug\nrxg\nsar\nsbn\nsbu\nsbw\nseg\nskp\nsqm\nssx\ntoo\nttm\nuch\nuek\nuhm\nuhn\nusv\nvaw\nvcw\nvkm\nvsj\nvwi\nwbm\nwcg\nwqr\nwri\nwsw\nxbs\nxcn\nxhw\nxip\nxoq\nxue\nxuk\nxvg", "output": "7488" }, { "input": "100\naie\naoq\nban\nbdw\ncdk\ncgw\ncls\ncoq\ncsp\ncwi\ndmg\negd\negi\nejd\nfbs\nfiv\nfjv\nfrp\nfto\ngcf\ngfb\ngkg\ngvg\nhfe\nhfr\nhgi\nhgx\nhpe\nhwt\nhxn\nibd\nifb\nihu\nipf\niwe\njds\njfe\njkb\njkx\njvq\nkdr\nkjh\nkll\nkog\nltk\nmik\nmsb\nnci\nndl\nnfo\nnfp\nnio\nnkr\nnmi\nnpk\noch\nogx\noka\nolf\nopm\norv\nphm\npmd\npuo\npxq\nqae\nqik\nqlp\nqna\nqst\nqth\nqxm\nrak\nrpj\nrqd\nsbq\nsfv\nstw\ntaj\nteh\ntlw\ntmj\ntmm\ntqv\nujn\nuko\nunb\nuvm\nvdb\nvjd\nvtp\nvvt\nwme\nwnq\nwqs\nwwj\nxan\nxdn\nxjg\nxkd", "output": "8960" }, { "input": "100\nahd\nahw\narc\naro\natd\naui\nbas\nbeg\nblc\nbmu\nboo\nbpt\nbqa\ncds\nchn\ncni\ncsh\nddt\ndjb\ndkh\neal\near\necr\neea\nefr\nekf\nekq\netb\neui\nfau\nfcr\nfdc\nfhp\nfpc\nfwv\ngaf\ngoo\ngut\nhek\nheu\nhfq\nhjk\nhjx\nhmk\nhqp\nhsa\niax\nijm\njlf\njlw\njok\njqi\njss\njte\nknb\nkrt\nlbi\nlej\nlqu\nlva\nlxf\nmll\nndb\nndf\nngc\nolh\nope\npds\npli\npuk\nqec\nqgi\nqkr\nqqu\nrks\nrsj\nscb\nsig\nsnj\ntdc\ntpa\ntro\nttc\ntwn\nuef\nuhh\nujb\nujn\nuka\nulk\nuss\nuwa\nuwu\nvmr\nvmt\nvoq\nwug\nwvh\nxef\nxrk", "output": "6624" }, { "input": "100\nagg\nals\naxf\nbdd\nbex\nbsx\nchb\nclr\ncmm\ndaf\ndbf\nddw\ndng\nduw\nebp\nech\neex\neff\nefg\neqt\nerp\nexg\nfbd\nffg\nfif\nfta\nghv\ngqn\ngrf\nhcc\nhdc\nhos\nhqh\nims\nipf\niro\nixu\njhx\njil\njqn\njuh\nkeb\nknl\nkol\nksj\nksl\nkxn\nlbn\nlci\nlfr\nliw\nlpc\nmdq\nmhx\nmts\nmwl\nnde\nnik\nnlo\nnnk\nnpc\nntt\nohr\nona\npap\npfb\npgm\npgo\npql\npsd\npvd\nqax\nqcj\nqfj\nqiv\nqke\nqks\nrhu\nrrg\nseo\nskr\ntjp\ntlt\ntof\ntop\ntpn\ntxe\nvfl\nvpn\nvrh\nwbd\nwet\nwgo\nwlm\nwox\nwwi\nxas\nxmg\nxng\nxqj", "output": "13408" }, { "input": "100\navm\nbir\nbmx\nbve\nbvx\ncbr\nccq\nckn\ncmd\ncuu\ncxh\nddw\ndfb\ndgt\ndmo\ndqd\neon\nerm\nerp\neux\newl\nfau\nfek\nfss\nftg\nfvb\ngfu\ngkw\nguj\ngwe\nhjf\nhrq\nibk\njjs\njmp\njqs\nkbu\nklu\nkqw\nkqx\nlaa\nlbe\nmek\nmga\nmio\nmle\nmls\nmma\nmoj\nmpb\nmxu\nnfs\nnht\noap\nods\noee\nokc\noqr\npdh\npdt\nphq\nphw\npwa\nqgt\nqji\nqnj\nqqt\nqvu\nqwb\nqwc\nrdv\nrfq\nrnx\nrse\nruq\nrvs\nsoo\nsxe\nthh\ntop\ntrg\ntud\ntur\nugu\nupj\nupt\nvak\nver\nvhu\nvul\nwes\nwkm\nwqc\nwuf\nxbk\nxdf\nxlh\nxnv\nxqo\nxvu", "output": "10864" } ]
30
0
0
9,117
342
Xenia and Spies
[ "brute force", "greedy", "implementation" ]
null
null
Xenia the vigorous detective faced *n* (*n*<=≥<=2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to *n* from left to right. Spy *s* has an important note. He has to pass the note to spy *f*. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is *x*, he can pass the note to another spy, either *x*<=-<=1 or *x*<=+<=1 (if *x*<==<=1 or *x*<==<=*n*, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During *m* steps Xenia watches some spies attentively. Specifically, during step *t**i* (steps are numbered from 1) Xenia watches spies numbers *l**i*,<=*l**i*<=+<=1,<=*l**i*<=+<=2,<=...,<=*r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got *s* and *f*. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy *s* to spy *f* as quickly as possible (in the minimum number of steps).
The first line contains four integers *n*, *m*, *s* and *f* (1<=≤<=*n*,<=*m*<=≤<=105; 1<=≤<=*s*,<=*f*<=≤<=*n*; *s*<=≠<=*f*; *n*<=≥<=2). Each of the following *m* lines contains three integers *t**i*,<=*l**i*,<=*r**i* (1<=≤<=*t**i*<=≤<=109,<=1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). It is guaranteed that *t*1<=&lt;<=*t*2<=&lt;<=*t*3<=&lt;<=...<=&lt;<=*t**m*.
Print *k* characters in a line: the *i*-th character in the line must represent the spies' actions on step *i*. If on step *i* the spy with the note must pass the note to the spy with a lesser number, the *i*-th character should equal "L". If on step *i* the spy with the note must pass it to the spy with a larger number, the *i*-th character must equal "R". If the spy must keep the note at the *i*-th step, the *i*-th character must equal "X". As a result of applying the printed sequence of actions spy *s* must pass the note to spy *f*. The number of printed characters *k* must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
[ "3 5 1 3\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n10 1 3\n" ]
[ "XXRR\n" ]
none
[ { "input": "3 5 1 3\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n10 1 3", "output": "XXRR" }, { "input": "2 3 2 1\n1 1 2\n2 1 2\n4 1 2", "output": "XXL" }, { "input": "5 11 1 5\n1 1 5\n2 2 2\n3 1 1\n4 3 3\n5 3 3\n6 1 1\n7 4 4\n8 4 5\n10 1 3\n11 5 5\n13 1 5", "output": "XXXRXRXXRR" }, { "input": "4 6 4 2\n2 2 2\n3 3 3\n4 1 1\n10 1 4\n11 2 3\n12 2 4", "output": "LXXL" }, { "input": "7 5 7 6\n1 4 5\n2 7 7\n3 6 6\n4 3 4\n5 1 3", "output": "L" }, { "input": "4 4 3 4\n1 2 4\n2 1 2\n3 3 4\n4 2 3", "output": "XR" }, { "input": "10 10 1 10\n1 1 10\n2 1 1\n3 7 10\n4 6 7\n5 9 9\n6 4 9\n7 2 5\n8 3 10\n9 2 10\n10 7 9", "output": "XXRRRXXXXRRRRRR" }, { "input": "20 20 17 20\n1 16 20\n2 12 13\n3 14 16\n4 13 15\n5 3 15\n6 2 11\n7 18 18\n8 5 15\n9 6 12\n10 19 19\n11 9 11\n12 14 17\n13 19 19\n14 12 20\n15 1 1\n16 11 17\n17 13 14\n18 5 17\n19 2 10\n20 19 20", "output": "XRRR" }, { "input": "100000 1 11500 70856\n1 9881 75626", "output": "XRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR..." }, { "input": "100000 2 37212 89918\n1 24285 99164\n2 67042 82268", "output": "XRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR..." }, { "input": "100 5 99 1\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n10 1 3", "output": "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL" }, { "input": "5 1 1 5\n1 1 1", "output": "XRRRR" }, { "input": "3 5 1 3\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n1000000000 1 3", "output": "XXRR" }, { "input": "2 2 1 2\n1 1 2\n1000000000 1 2", "output": "XR" }, { "input": "10 1 1 10\n1 5 6", "output": "RRRRRRRRR" } ]
62
0
0
9,124
958
Death Stars (medium)
[ "hashing", "strings" ]
null
null
The stardate is 1983, and Princess Heidi is getting better at detecting the Death Stars. This time, two Rebel spies have yet again given Heidi two maps with the possible locations of the Death Star. Since she got rid of all double agents last time, she knows that both maps are correct, and indeed show the map of the solar system that contains the Death Star. However, this time the Empire has hidden the Death Star very well, and Heidi needs to find a place that appears on both maps in order to detect the Death Star. The first map is an *N*<=×<=*M* grid, each cell of which shows some type of cosmic object that is present in the corresponding quadrant of space. The second map is an *M*<=×<=*N* grid. Heidi needs to align those two maps in such a way that they overlap over some *M*<=×<=*M* section in which all cosmic objects are identical. Help Heidi by identifying where such an *M*<=×<=*M* section lies within both maps.
The first line of the input contains two space-separated integers *N* and *M* (1<=≤<=*N*<=≤<=2000, 1<=≤<=*M*<=≤<=200, *M*<=≤<=*N*). The next *N* lines each contain *M* lower-case Latin characters (a-z), denoting the first map. Different characters correspond to different cosmic object types. The next *M* lines each contain *N* characters, describing the second map in the same format.
The only line of the output should contain two space-separated integers *i* and *j*, denoting that the section of size *M*<=×<=*M* in the first map that starts at the *i*-th row is equal to the section of the second map that starts at the *j*-th column. Rows and columns are numbered starting from 1. If there are several possible ways to align the maps, Heidi will be satisfied with any of those. It is guaranteed that a solution exists.
[ "10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nnoise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo\n" ]
[ "4 6\n" ]
The 5-by-5 grid for the first test case looks like this:
[ { "input": "10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nnoise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo", "output": "4 6" }, { "input": "1 1\ng\ng", "output": "1 1" } ]
1,715
154,214,400
3
9,141
0
none
[ "none" ]
null
null
Santa Claus likes palindromes very much. There was his birthday recently. *k* of his friends came to him to congratulate him, and each of them presented to him a string *s**i* having the same length *n*. We denote the beauty of the *i*-th string by *a**i*. It can happen that *a**i* is negative — that means that Santa doesn't find this string beautiful at all. Santa Claus is crazy about palindromes. He is thinking about the following question: what is the maximum possible total beauty of a palindrome which can be obtained by concatenating some (possibly all) of the strings he has? Each present can be used at most once. Note that all strings have the same length *n*. Recall that a palindrome is a string that doesn't change after one reverses it. Since the empty string is a palindrome too, the answer can't be negative. Even if all *a**i*'s are negative, Santa can obtain the empty string.
The first line contains two positive integers *k* and *n* divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1<=≤<=*k*,<=*n*<=≤<=100<=000; *n*·*k* <=≤<=100<=000). *k* lines follow. The *i*-th of them contains the string *s**i* and its beauty *a**i* (<=-<=10<=000<=≤<=*a**i*<=≤<=10<=000). The string consists of *n* lowercase English letters, and its beauty is integer. Some of strings may coincide. Also, equal strings can have different beauties.
In the only line print the required maximum possible beauty.
[ "7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4\n", "3 1\na 1\na 2\na 3\n", "2 5\nabcde 10000\nabcde 10000\n" ]
[ "12\n", "6\n", "0\n" ]
In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).
[ { "input": "7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4", "output": "12" }, { "input": "3 1\na 1\na 2\na 3", "output": "6" }, { "input": "2 5\nabcde 10000\nabcde 10000", "output": "0" }, { "input": "10 10\nnjxbzflaka -1\nfelbvvtkja 6\ngxiuztqkcw 5\naomvscmtti 6\njsqmkoyuca -2\nwckqtzuixg 5\najktvvblef -5\nittmcsvmoa -1\nakalfzbxjn 10\nacuyokmqsj 8", "output": "31" }, { "input": "10 20\njvyxocgomfmrtllgmagp 13\ngvtjnyaofrswcnnifzfq 17\nqisxpseggpjfoijmqnel -5\nlenqmjiofjpggespxsiq 14\nqfzfinncwsrfoaynjtvg 14\ncaayidazlylxyisihdhx 14\npgamglltrmfmogcoxyvj 11\nxhdhisiyxlylzadiyaac 2\ntbirihfpjgbbtclpxwhv 19\nvhwxplctbbgjpfhiribt 10", "output": "109" }, { "input": "1 1\ne -1", "output": "0" }, { "input": "2 1\nt 1\nt 2", "output": "3" }, { "input": "1 2\nyy 1", "output": "1" }, { "input": "2 2\nsn 1\nns 2", "output": "3" }, { "input": "3 3\nada -1\nxuv -1\nvux 3", "output": "2" }, { "input": "4 3\ndbd 24\naba 90\ncbc 54\naba 46", "output": "190" }, { "input": "2 3\naaa 5\naaa -2", "output": "5" }, { "input": "4 3\naba 4\naba 3\naba 3\naba -2", "output": "10" }, { "input": "4 3\naba 4\naba 2\naba 2\naba -1", "output": "8" }, { "input": "3 2\naa 5\naa -2\nbb 1", "output": "5" }, { "input": "2 2\naa 500\naa -50", "output": "500" }, { "input": "2 1\na 5\na -1", "output": "5" }, { "input": "2 3\naba 10\naba -3", "output": "10" }, { "input": "2 3\naba 10\naba -9", "output": "10" } ]
46
0
-1
9,146