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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | none | [
"none"
] | null | null | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically *k*-th permutation of integers from 1 to *n*. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers. | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=109) β the number of elements in the permutation and the lexicographical number of the permutation. | If the *k*-th permutation of numbers from 1 to *n* does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes *i*, that *i* and *a**i* are both lucky numbers. | [
"7 4\n",
"4 7\n"
] | [
"1\n",
"1\n"
] | A permutation is an ordered set of *n* elements, where each integer from 1 to *n* occurs exactly once. The element of permutation in position with index *i* is denoted as *a*<sub class="lower-index">*i*</sub> (1ββ€β*i*ββ€β*n*). Permutation *a* is lexicographically smaller that permutation *b* if there is such a *i* (1ββ€β*i*ββ€β*n*), that *a*<sub class="lower-index">*i*</sub>β<β*b*<sub class="lower-index">*i*</sub>, and for any *j* (1ββ€β*j*β<β*i*) *a*<sub class="lower-index">*j*</sub>β=β*b*<sub class="lower-index">*j*</sub>. Let's make a list of all possible permutations of *n* elements and sort it in the order of lexicographical increasing. Then the lexicographically *k*-th permutation is the *k*-th element of this list of permutations.
In the first sample the permutation looks like that:
1 2 3 4 6 7 5
The only suitable position is 4.
In the second sample the permutation looks like that:
2 1 3 4
The only suitable position is 4. | [
{
"input": "7 4",
"output": "1"
},
{
"input": "4 7",
"output": "1"
},
{
"input": "7 1",
"output": "2"
},
{
"input": "7 5040",
"output": "1"
},
{
"input": "10 1023",
"output": "0"
},
{
"input": "7 7477",
"output": "-1"
},
{
"input": "10 10000",
"output": "1"
},
{
"input": "3 7",
"output": "-1"
},
{
"input": "27 1",
"output": "2"
},
{
"input": "40 8544",
"output": "2"
},
{
"input": "47 1",
"output": "4"
},
{
"input": "47 8547744",
"output": "3"
},
{
"input": "50 1000000000",
"output": "4"
},
{
"input": "64 87",
"output": "4"
},
{
"input": "98 854555",
"output": "6"
},
{
"input": "100 1",
"output": "6"
},
{
"input": "9985 5888454",
"output": "30"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "1 2",
"output": "-1"
},
{
"input": "2 1000000000",
"output": "-1"
},
{
"input": "10 1000000000",
"output": "-1"
},
{
"input": "20 1000000000",
"output": "2"
},
{
"input": "777777 1",
"output": "126"
},
{
"input": "777777 2",
"output": "125"
},
{
"input": "777474 10000",
"output": "120"
},
{
"input": "1000000000 1",
"output": "1022"
},
{
"input": "777777777 5",
"output": "1021"
},
{
"input": "777777777 1",
"output": "1022"
},
{
"input": "777477774 1",
"output": "989"
},
{
"input": "444747744 1000000000",
"output": "554"
},
{
"input": "475 88555458",
"output": "8"
},
{
"input": "12 855448",
"output": "1"
},
{
"input": "20 1000000000",
"output": "2"
},
{
"input": "47 99998544",
"output": "2"
},
{
"input": "49 1000000000",
"output": "2"
},
{
"input": "854459 95554455",
"output": "126"
},
{
"input": "77777779 1000000000",
"output": "508"
},
{
"input": "77 47",
"output": "5"
},
{
"input": "6999 85488877",
"output": "22"
},
{
"input": "7479 58884598",
"output": "24"
},
{
"input": "1000000000 1000000000",
"output": "1022"
},
{
"input": "7 1000",
"output": "0"
},
{
"input": "7 124",
"output": "1"
},
{
"input": "7 2048",
"output": "1"
},
{
"input": "7 3001",
"output": "1"
},
{
"input": "7 127",
"output": "1"
},
{
"input": "7 980",
"output": "1"
},
{
"input": "7 5000",
"output": "0"
},
{
"input": "7 4095",
"output": "2"
},
{
"input": "7 3856",
"output": "0"
},
{
"input": "7 5032",
"output": "0"
},
{
"input": "7 4999",
"output": "0"
},
{
"input": "2 3",
"output": "-1"
},
{
"input": "2 4",
"output": "-1"
},
{
"input": "7 985",
"output": "1"
},
{
"input": "4 25",
"output": "-1"
},
{
"input": "6 121",
"output": "1"
},
{
"input": "11 39916801",
"output": "-1"
},
{
"input": "29 1000000000",
"output": "2"
},
{
"input": "10 4589",
"output": "1"
},
{
"input": "10 100000",
"output": "1"
},
{
"input": "10 98564",
"output": "1"
},
{
"input": "10 1",
"output": "2"
},
{
"input": "10 100000009",
"output": "-1"
}
] | 60 | 0 | 0 | 5,743 |
|
416 | Art Union | [
"brute force",
"dp",
"implementation"
] | null | null | A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). The union consists of *n* painters who decided to organize their work as follows.
Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these *n* colors. Adding the *j*-th color to the *i*-th picture takes the *j*-th painter *t**ij* units of time.
Order is important everywhere, so the painters' work is ordered by the following rules:
- Each picture is first painted by the first painter, then by the second one, and so on. That is, after the *j*-th painter finishes working on the picture, it must go to the (*j*<=+<=1)-th painter (if *j*<=<<=*n*); - each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on; - each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest; - as soon as the *j*-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter.
Given that the painters start working at time 0, find for each picture the time when it is ready for sale. | The first line of the input contains integers *m*,<=*n* (1<=β€<=*m*<=β€<=50000,<=1<=β€<=*n*<=β€<=5), where *m* is the number of pictures and *n* is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains *n* integers *t**i*1,<=*t**i*2,<=...,<=*t**in* (1<=β€<=*t**ij*<=β€<=1000), where *t**ij* is the time the *j*-th painter needs to work on the *i*-th picture. | Print the sequence of *m* integers *r*1,<=*r*2,<=...,<=*r**m*, where *r**i* is the moment when the *n*-th painter stopped working on the *i*-th picture. | [
"5 1\n1\n2\n3\n4\n5\n",
"4 2\n2 5\n3 1\n5 3\n10 1\n"
] | [
"1 3 6 10 15 ",
"7 8 13 21 "
] | none | [
{
"input": "5 1\n1\n2\n3\n4\n5",
"output": "1 3 6 10 15 "
},
{
"input": "4 2\n2 5\n3 1\n5 3\n10 1",
"output": "7 8 13 21 "
},
{
"input": "1 1\n66",
"output": "66 "
},
{
"input": "2 2\n1 1\n1 1",
"output": "2 3 "
},
{
"input": "2 2\n10 1\n10 1",
"output": "11 21 "
},
{
"input": "1 5\n1 95 44 14 35",
"output": "189 "
},
{
"input": "7 1\n80\n92\n24\n88\n40\n45\n7",
"output": "80 172 196 284 324 369 376 "
},
{
"input": "1 2\n51 44",
"output": "95 "
},
{
"input": "2 1\n19\n4",
"output": "19 23 "
},
{
"input": "2 2\n1 10\n1 1",
"output": "11 12 "
},
{
"input": "3 3\n3 9 4\n5 10 8\n4 4 7",
"output": "16 30 37 "
},
{
"input": "10 3\n6 10 3\n2 7 9\n10 4 7\n6 3 4\n6 2 6\n8 4 4\n5 9 8\n6 9 7\n2 7 10\n2 6 2",
"output": "19 32 39 43 49 53 61 68 78 80 "
}
] | 732 | 9,625,600 | 0 | 5,746 |
|
660 | Seating On Bus | [
"implementation"
] | null | null | Consider 2*n* rows of the seats in a bus. *n* rows of the seats on the left and *n* rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4*n*.
Consider that *m* (*m*<=β€<=4*n*) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to *m* (in the order of their entering the bus). The pattern of the seat occupation is as below:
1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , *n*-th row left window seat, *n*-th row right window seat.
After occupying all the window seats (for *m*<=><=2*n*) the non-window seats are occupied:
1-st row left non-window seat, 1-st row right non-window seat, ... , *n*-th row left non-window seat, *n*-th row right non-window seat.
All the passengers go to a single final destination. In the final destination, the passengers get off in the given order.
1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , *n*-th row left non-window seat, *n*-th row left window seat, *n*-th row right non-window seat, *n*-th row right window seat.
You are given the values *n* and *m*. Output *m* numbers from 1 to *m*, the order in which the passengers will get off the bus. | The only line contains two integers, *n* and *m* (1<=β€<=*n*<=β€<=100,<=1<=β€<=*m*<=β€<=4*n*) β the number of pairs of rows and the number of passengers. | Print *m* distinct integers from 1 to *m* β the order in which the passengers will get off the bus. | [
"2 7\n",
"9 36\n"
] | [
"5 1 6 2 7 3 4\n",
"19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18\n"
] | none | [
{
"input": "2 7",
"output": "5 1 6 2 7 3 4"
},
{
"input": "9 36",
"output": "19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "1 4",
"output": "3 1 4 2"
},
{
"input": "10 1",
"output": "1"
},
{
"input": "10 10",
"output": "1 2 3 4 5 6 7 8 9 10"
},
{
"input": "10 40",
"output": "21 1 22 2 23 3 24 4 25 5 26 6 27 7 28 8 29 9 30 10 31 11 32 12 33 13 34 14 35 15 36 16 37 17 38 18 39 19 40 20"
},
{
"input": "10 39",
"output": "21 1 22 2 23 3 24 4 25 5 26 6 27 7 28 8 29 9 30 10 31 11 32 12 33 13 34 14 35 15 36 16 37 17 38 18 39 19 20"
},
{
"input": "77 1",
"output": "1"
},
{
"input": "77 13",
"output": "1 2 3 4 5 6 7 8 9 10 11 12 13"
},
{
"input": "77 53",
"output": "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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53"
},
{
"input": "77 280",
"output": "155 1 156 2 157 3 158 4 159 5 160 6 161 7 162 8 163 9 164 10 165 11 166 12 167 13 168 14 169 15 170 16 171 17 172 18 173 19 174 20 175 21 176 22 177 23 178 24 179 25 180 26 181 27 182 28 183 29 184 30 185 31 186 32 187 33 188 34 189 35 190 36 191 37 192 38 193 39 194 40 195 41 196 42 197 43 198 44 199 45 200 46 201 47 202 48 203 49 204 50 205 51 206 52 207 53 208 54 209 55 210 56 211 57 212 58 213 59 214 60 215 61 216 62 217 63 218 64 219 65 220 66 221 67 222 68 223 69 224 70 225 71 226 72 227 73 228 74 22..."
},
{
"input": "100 1",
"output": "1"
},
{
"input": "100 13",
"output": "1 2 3 4 5 6 7 8 9 10 11 12 13"
},
{
"input": "100 77",
"output": "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 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"
},
{
"input": "100 103",
"output": "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 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 101 102 103"
},
{
"input": "100 200",
"output": "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 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..."
},
{
"input": "100 199",
"output": "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 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..."
},
{
"input": "100 201",
"output": "201 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 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154..."
},
{
"input": "100 300",
"output": "201 1 202 2 203 3 204 4 205 5 206 6 207 7 208 8 209 9 210 10 211 11 212 12 213 13 214 14 215 15 216 16 217 17 218 18 219 19 220 20 221 21 222 22 223 23 224 24 225 25 226 26 227 27 228 28 229 29 230 30 231 31 232 32 233 33 234 34 235 35 236 36 237 37 238 38 239 39 240 40 241 41 242 42 243 43 244 44 245 45 246 46 247 47 248 48 249 49 250 50 251 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 27..."
},
{
"input": "100 399",
"output": "201 1 202 2 203 3 204 4 205 5 206 6 207 7 208 8 209 9 210 10 211 11 212 12 213 13 214 14 215 15 216 16 217 17 218 18 219 19 220 20 221 21 222 22 223 23 224 24 225 25 226 26 227 27 228 28 229 29 230 30 231 31 232 32 233 33 234 34 235 35 236 36 237 37 238 38 239 39 240 40 241 41 242 42 243 43 244 44 245 45 246 46 247 47 248 48 249 49 250 50 251 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 27..."
},
{
"input": "100 400",
"output": "201 1 202 2 203 3 204 4 205 5 206 6 207 7 208 8 209 9 210 10 211 11 212 12 213 13 214 14 215 15 216 16 217 17 218 18 219 19 220 20 221 21 222 22 223 23 224 24 225 25 226 26 227 27 228 28 229 29 230 30 231 31 232 32 233 33 234 34 235 35 236 36 237 37 238 38 239 39 240 40 241 41 242 42 243 43 244 44 245 45 246 46 247 47 248 48 249 49 250 50 251 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 27..."
},
{
"input": "3 9",
"output": "7 1 8 2 9 3 4 5 6"
}
] | 109 | 0 | 0 | 5,753 |
|
864 | Bus | [
"greedy",
"implementation",
"math"
] | null | null | A bus moves along the coordinate line *Ox* from the point *x*<==<=0 to the point *x*<==<=*a*. After starting from the point *x*<==<=0, it reaches the point *x*<==<=*a*, immediately turns back and then moves to the point *x*<==<=0. After returning to the point *x*<==<=0 it immediately goes back to the point *x*<==<=*a* and so on. Thus, the bus moves from *x*<==<=0 to *x*<==<=*a* and back. Moving from the point *x*<==<=0 to *x*<==<=*a* or from the point *x*<==<=*a* to *x*<==<=0 is called a bus journey. In total, the bus must make *k* journeys.
The petrol tank of the bus can hold *b* liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.
There is a gas station in point *x*<==<=*f*. This point is between points *x*<==<=0 and *x*<==<=*a*. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain *b* liters of gasoline.
What is the minimum number of times the bus needs to refuel at the point *x*<==<=*f* to make *k* journeys? The first journey starts in the point *x*<==<=0. | The first line contains four integers *a*, *b*, *f*, *k* (0<=<<=*f*<=<<=*a*<=β€<=106, 1<=β€<=*b*<=β€<=109, 1<=β€<=*k*<=β€<=104) β the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys. | Print the minimum number of times the bus needs to refuel to make *k* journeys. If it is impossible for the bus to make *k* journeys, print -1. | [
"6 9 2 4\n",
"6 10 2 4\n",
"6 5 4 3\n"
] | [
"4\n",
"2\n",
"-1\n"
] | In the first example the bus needs to refuel during each journey.
In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty.
In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling. | [
{
"input": "6 9 2 4",
"output": "4"
},
{
"input": "6 10 2 4",
"output": "2"
},
{
"input": "6 5 4 3",
"output": "-1"
},
{
"input": "2 2 1 1",
"output": "0"
},
{
"input": "10 4 6 10",
"output": "-1"
},
{
"input": "3 1 1 1",
"output": "-1"
},
{
"input": "2 1 1 1",
"output": "1"
},
{
"input": "1000000 51923215 2302 10000",
"output": "199"
},
{
"input": "10 11 3 2",
"output": "-1"
},
{
"input": "20 50 10 25",
"output": "11"
},
{
"input": "10 10 5 20",
"output": "20"
},
{
"input": "15 65 5 50",
"output": "12"
},
{
"input": "10 19 1 5",
"output": "3"
},
{
"input": "10 19 9 5",
"output": "3"
},
{
"input": "23 46 12 2",
"output": "0"
},
{
"input": "23 46 12 3",
"output": "1"
},
{
"input": "20 20 19 1",
"output": "0"
},
{
"input": "20 23 17 2",
"output": "1"
},
{
"input": "100 70 50 1",
"output": "1"
},
{
"input": "100 70 70 2",
"output": "2"
},
{
"input": "140 480 139 40",
"output": "18"
},
{
"input": "1000000 1000000000 1 1000",
"output": "0"
},
{
"input": "100000 1000000 50000 1000",
"output": "100"
},
{
"input": "1000000 1000000 500000 1000",
"output": "1000"
},
{
"input": "1000000 1000000 500000 10000",
"output": "10000"
},
{
"input": "1000000 2500000 500000 9999",
"output": "4998"
},
{
"input": "1000000 1500000 500000 9999",
"output": "9997"
},
{
"input": "1000000 1500000 500000 10000",
"output": "9998"
},
{
"input": "1000000 1 1 1",
"output": "-1"
},
{
"input": "2 1000000000 1 1",
"output": "0"
},
{
"input": "1000000 1000000000 1 1",
"output": "0"
},
{
"input": "1000000 1 999999 1",
"output": "-1"
},
{
"input": "1000000 1000000000 999999 1",
"output": "0"
},
{
"input": "2 1 1 10000",
"output": "-1"
},
{
"input": "1000000 1 1 10000",
"output": "-1"
},
{
"input": "1000000 1000000000 1 10000",
"output": "10"
},
{
"input": "1000000 1 999999 10000",
"output": "-1"
},
{
"input": "2 1000000000 1 10000",
"output": "0"
},
{
"input": "1000000 1000000000 999999 10000",
"output": "10"
},
{
"input": "10000 78393 3000 9999",
"output": "1428"
},
{
"input": "1000000 8839233 302200 9999",
"output": "1249"
},
{
"input": "900005 3333333 210000 9999",
"output": "3332"
},
{
"input": "6 7 4 2",
"output": "2"
},
{
"input": "3 1 2 1",
"output": "-1"
},
{
"input": "150 100 1 1",
"output": "-1"
},
{
"input": "10 5 6 1",
"output": "-1"
},
{
"input": "51 81 36 38",
"output": "36"
},
{
"input": "100 159 80 2",
"output": "1"
}
] | 109 | 0 | 0 | 5,776 |
|
262 | Roma and Changing Signs | [
"greedy"
] | null | null | Roma works in a company that sells TVs. Now he has to prepare a report for the last year.
Roma has got a list of the company's incomes. The list is a sequence that consists of *n* integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly *k* changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times.
The operation of changing a number's sign is the operation of multiplying this number by -1.
Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactly *k* changes. | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=105), showing, how many numbers are in the sequence and how many swaps are to be made.
The second line contains a non-decreasing sequence, consisting of *n* integers *a**i* (|*a**i*|<=β€<=104).
The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order. | In the single line print the answer to the problem β the maximum total income that we can obtain after exactly *k* changes. | [
"3 2\n-1 -1 1\n",
"3 1\n-1 -1 1\n"
] | [
"3\n",
"1\n"
] | In the first sample we can get sequence [1, 1, 1], thus the total income equals 3.
In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1. | [
{
"input": "3 2\n-1 -1 1",
"output": "3"
},
{
"input": "3 1\n-1 -1 1",
"output": "1"
},
{
"input": "17 27\n257 320 676 1136 2068 2505 2639 4225 4951 5786 7677 7697 7851 8337 8429 8469 9343",
"output": "81852"
},
{
"input": "69 28\n-9822 -9264 -9253 -9221 -9139 -9126 -9096 -8981 -8521 -8313 -8257 -8253 -7591 -7587 -7301 -7161 -7001 -6847 -6441 -6241 -5949 -5896 -5713 -5692 -5644 -5601 -5545 -5525 -5331 -5253 -5041 -5000 -4951 -4855 -4384 -4293 -4251 -4001 -3991 -3762 -3544 -3481 -3261 -2983 -2882 -2857 -2713 -2691 -2681 -2653 -2221 -2043 -2011 -1997 -1601 -1471 -1448 -1363 -1217 -1217 -1129 -961 -926 -801 -376 -327 -305 -174 -91",
"output": "102443"
},
{
"input": "12 28\n-6652 -6621 -6471 -5559 -5326 -4551 -4401 -4326 -3294 -1175 -1069 -43",
"output": "49488"
},
{
"input": "78 13\n-9961 -9922 -9817 -9813 -9521 -9368 -9361 -9207 -9153 -9124 -9008 -8981 -8951 -8911 -8551 -8479 -8245 -8216 -7988 -7841 -7748 -7741 -7734 -7101 -6846 -6804 -6651 -6526 -6519 -6463 -6297 -6148 -6090 -5845 -5209 -5201 -5161 -5061 -4537 -4529 -4433 -4370 -4266 -4189 -4125 -3945 -3843 -3777 -3751 -3476 -3461 -3279 -3205 -3001 -2889 -2761 -2661 -2521 -2481 -2305 -2278 -2269 -2225 -1648 -1524 -1476 -1353 -1097 -867 -785 -741 -711 -692 -440 -401 -225 -65 -41",
"output": "-147832"
},
{
"input": "4 1\n218 3441 4901 7601",
"output": "15725"
},
{
"input": "73 26\n-8497 -8363 -7603 -7388 -6830 -6827 -6685 -6389 -6237 -6099 -6013 -5565 -5465 -4965 -4947 -4201 -3851 -3793 -3421 -3410 -3201 -3169 -3156 -2976 -2701 -2623 -2321 -2169 -1469 -1221 -950 -926 -9 47 236 457 773 1321 1485 1545 1671 1736 2014 2137 2174 2301 2625 3181 3536 3851 4041 4685 4981 4987 5145 5163 5209 5249 6011 6337 6790 7254 7361 7407 7969 7982 8083 8251 8407 8735 9660 9855 9957",
"output": "315919"
},
{
"input": "53 5\n-9821 -9429 -9146 -8973 -8807 -8801 -8321 -7361 -7222 -7161 -6913 -5961 -4877 -4756 -4753 -4661 -3375 -3031 -2950 -2661 -2161 -2041 -1111 -1071 -905 -697 -397 323 772 1617 1752 2736 2737 3201 3465 4029 4121 4463 4561 4637 4814 6119 6610 6641 6961 7217 7523 8045 8610 8915 9004 9265 9576",
"output": "92703"
},
{
"input": "1 1\n0",
"output": "0"
},
{
"input": "1 1\n10000",
"output": "-10000"
},
{
"input": "1 2\n-1",
"output": "-1"
},
{
"input": "2 1\n0 1",
"output": "1"
},
{
"input": "2 2\n-1 0",
"output": "1"
},
{
"input": "5 6\n-3 -2 -1 5 6",
"output": "15"
},
{
"input": "3 3\n-50 -10 30",
"output": "70"
},
{
"input": "4 4\n-100 -90 -80 1",
"output": "269"
},
{
"input": "4 3\n-3 -2 1 2",
"output": "6"
},
{
"input": "6 4\n-6 -3 -2 1 2 3",
"output": "15"
},
{
"input": "5 6\n-10 -9 -8 1 2",
"output": "28"
},
{
"input": "1 2\n1",
"output": "1"
},
{
"input": "2 2\n-1 3",
"output": "2"
},
{
"input": "4 7\n-3 -2 1 6",
"output": "10"
},
{
"input": "4 3\n-7 -6 1 5",
"output": "17"
},
{
"input": "4 4\n-5 -1 1 2",
"output": "9"
}
] | 280 | 7,372,800 | 0 | 5,796 |
|
995 | Tesla | [
"constructive algorithms",
"implementation"
] | null | null | Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem.
Allen's future parking lot can be represented as a rectangle with $4$ rows and $n$ ($n \le 50$) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having $k$ ($k \le 2n$) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places.
However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space.
Allen knows he will be a very busy man, and will only have time to move cars at most $20000$ times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important. | The first line of the input contains two space-separated integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 2n$), representing the number of columns and the number of cars, respectively.
The next four lines will contain $n$ integers each between $0$ and $k$ inclusive, representing the initial state of the parking lot. The rows are numbered $1$ to $4$ from top to bottom and the columns are numbered $1$ to $n$ from left to right.
In the first and last line, an integer $1 \le x \le k$ represents a parking spot assigned to car $x$ (you can only move this car to this place), while the integer $0$ represents a empty space (you can't move any car to this place).
In the second and third line, an integer $1 \le x \le k$ represents initial position of car $x$, while the integer $0$ represents an empty space (you can move any car to this place).
Each $x$ between $1$ and $k$ appears exactly once in the second and third line, and exactly once in the first and fourth line. | If there is a sequence of moves that brings all of the cars to their parking spaces, with at most $20000$ car moves, then print $m$, the number of moves, on the first line. On the following $m$ lines, print the moves (one move per line) in the format $i$ $r$ $c$, which corresponds to Allen moving car $i$ to the neighboring space at row $r$ and column $c$.
If it is not possible for Allen to move all the cars to the correct spaces with at most $20000$ car moves, print a single line with the integer $-1$. | [
"4 5\n1 2 0 4\n1 2 0 4\n5 0 0 3\n0 5 0 3\n",
"1 2\n1\n2\n1\n2\n",
"1 2\n1\n1\n2\n2\n"
] | [
"6\n1 1 1\n2 1 2\n4 1 4\n3 4 4\n5 3 2\n5 4 2\n",
"-1\n",
"2\n1 1 1\n2 4 1\n"
] | In the first sample test case, all cars are in front of their spots except car $5$, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most $20000$ will be accepted.
In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible. | [
{
"input": "4 5\n1 2 0 4\n1 2 0 4\n5 0 0 3\n0 5 0 3",
"output": "6\n1 1 1\n2 1 2\n4 1 4\n3 4 4\n5 3 2\n5 4 2"
},
{
"input": "1 2\n1\n2\n1\n2",
"output": "-1"
},
{
"input": "1 2\n1\n1\n2\n2",
"output": "2\n1 1 1\n2 4 1"
},
{
"input": "2 2\n1 0\n0 2\n0 1\n0 2",
"output": "7\n2 2 1\n1 2 2\n2 3 1\n1 2 1\n2 3 2\n1 1 1\n2 4 2"
},
{
"input": "7 14\n2 11 1 14 9 8 5\n12 6 7 1 10 2 3\n14 13 9 8 5 4 11\n13 6 4 3 12 7 10",
"output": "-1"
},
{
"input": "10 20\n18 7 3 16 5 8 19 2 20 12\n15 16 7 11 14 3 12 4 8 10\n19 18 20 1 17 9 5 2 6 13\n11 15 13 17 6 9 14 1 10 4",
"output": "220\n9 4 6\n17 3 6\n1 3 5\n20 3 4\n18 3 3\n19 3 2\n15 3 1\n16 2 1\n7 2 2\n11 2 3\n14 2 4\n3 2 5\n12 2 6\n4 2 7\n8 2 8\n10 2 9\n13 2 10\n6 3 10\n2 3 9\n5 3 8\n17 3 7\n1 3 6\n20 3 5\n18 3 4\n19 3 3\n15 3 2\n16 3 1\n7 1 2\n11 2 2\n14 2 3\n3 2 4\n12 2 5\n4 2 6\n8 2 7\n10 2 8\n13 2 9\n6 2 10\n2 3 10\n5 3 9\n17 3 8\n1 3 7\n20 3 6\n18 3 5\n19 3 4\n15 4 2\n16 3 2\n11 2 1\n14 2 2\n3 2 3\n12 2 4\n4 2 5\n8 2 6\n10 2 7\n13 2 8\n6 2 9\n2 2 10\n5 3 10\n17 3 9\n1 3 8\n20 3 7\n18 3 6\n19 3 5\n16 3 3\n11 3 1\n14 2 1\n3 1 3..."
},
{
"input": "2 1\n0 0\n0 0\n0 1\n0 1",
"output": "1\n1 4 2"
},
{
"input": "2 3\n0 2\n0 1\n3 2\n3 1",
"output": "7\n1 2 1\n2 2 2\n3 4 1\n1 3 1\n2 1 2\n1 3 2\n1 4 2"
},
{
"input": "8 12\n9 7 10 5 0 0 8 0\n11 6 5 4 1 10 2 0\n0 8 0 7 0 3 9 12\n6 4 1 2 0 11 12 3",
"output": "105\n11 3 1\n6 2 1\n5 2 2\n4 2 3\n1 2 4\n10 2 5\n2 2 6\n12 2 8\n9 3 8\n3 3 7\n7 3 5\n8 3 3\n11 3 2\n6 3 1\n5 2 1\n4 2 2\n1 2 3\n10 2 4\n2 2 5\n12 2 7\n9 2 8\n3 3 8\n7 3 6\n8 3 4\n11 3 3\n6 4 1\n5 3 1\n4 2 1\n1 2 2\n10 2 3\n2 2 4\n12 2 6\n9 2 7\n3 4 8\n7 3 7\n8 3 5\n11 3 4\n5 3 2\n4 3 1\n1 2 1\n10 1 3\n2 2 3\n12 2 5\n9 2 6\n7 3 8\n8 3 6\n11 3 5\n5 3 3\n4 3 2\n1 3 1\n2 2 2\n12 2 4\n9 2 5\n7 2 8\n8 3 7\n11 3 6\n5 3 4\n4 4 2\n1 3 2\n2 2 1\n12 2 3\n9 2 4\n7 2 7\n8 3 8\n11 4 6\n5 3 5\n1 3 3\n2 3 1\n12 2 2\n9 2 3..."
},
{
"input": "1 1\n0\n1\n0\n1",
"output": "2\n1 3 1\n1 4 1"
},
{
"input": "2 4\n3 4\n2 1\n3 4\n2 1",
"output": "-1"
},
{
"input": "3 5\n2 1 5\n5 3 2\n4 0 1\n0 4 3",
"output": "18\n4 3 2\n5 3 1\n3 2 1\n2 2 2\n1 2 3\n4 4 2\n5 3 2\n3 3 1\n2 2 1\n1 2 2\n5 3 3\n3 3 2\n2 1 1\n1 1 2\n5 2 3\n3 3 3\n5 1 3\n3 4 3"
},
{
"input": "8 15\n15 13 0 14 2 7 4 9\n11 5 14 2 15 12 10 13\n1 9 7 4 3 8 0 6\n3 1 12 6 10 11 8 5",
"output": "136\n8 3 7\n3 3 6\n4 3 5\n7 3 4\n9 3 3\n1 3 2\n11 3 1\n5 2 1\n14 2 2\n2 2 3\n15 2 4\n12 2 5\n10 2 6\n13 2 7\n6 2 8\n8 4 7\n3 3 7\n4 3 6\n7 3 5\n9 3 4\n1 4 2\n11 3 2\n5 3 1\n14 2 1\n2 2 2\n15 2 3\n12 2 4\n10 2 5\n13 2 6\n6 2 7\n3 3 8\n4 3 7\n7 3 6\n9 3 5\n11 3 3\n5 3 2\n14 3 1\n2 2 1\n15 2 2\n12 2 3\n10 2 4\n13 2 5\n6 2 6\n3 2 8\n4 3 8\n7 3 7\n9 3 6\n11 3 4\n5 3 3\n14 3 2\n2 3 1\n15 2 1\n12 2 2\n10 2 3\n13 2 4\n6 2 5\n3 2 7\n4 2 8\n7 3 8\n9 3 7\n11 3 5\n5 3 4\n14 3 3\n2 3 2\n15 1 1\n12 2 1\n10 2 2\n13 2 3\n..."
},
{
"input": "8 14\n12 7 0 5 4 3 13 6\n6 9 7 0 4 12 2 14\n10 8 13 1 5 0 11 3\n2 0 8 10 9 14 1 11",
"output": "81\n4 1 5\n12 2 5\n2 2 6\n14 2 7\n3 2 8\n11 3 8\n5 3 6\n1 3 5\n13 3 4\n8 3 3\n10 3 2\n6 3 1\n9 2 1\n7 2 2\n12 2 4\n2 2 5\n14 2 6\n3 2 7\n11 4 8\n5 3 7\n1 3 6\n13 3 5\n8 4 3\n10 3 3\n6 3 2\n9 3 1\n7 1 2\n12 2 3\n2 2 4\n14 2 5\n3 2 6\n5 3 8\n1 3 7\n13 3 6\n10 3 4\n6 3 3\n9 3 2\n12 2 2\n2 2 3\n14 2 4\n3 1 6\n5 2 8\n1 4 7\n13 3 7\n10 4 4\n6 3 4\n9 3 3\n12 2 1\n2 2 2\n14 2 3\n5 2 7\n13 3 8\n6 3 5\n9 3 4\n12 1 1\n2 2 1\n14 2 2\n5 2 6\n13 2 8\n6 3 6\n9 3 5\n2 3 1\n14 2 1\n5 2 5\n13 2 7\n6 3 7\n9 4 5\n2 4 1\n14 3 ..."
},
{
"input": "10 1\n0 0 1 0 0 0 0 0 0 0\n0 0 1 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0",
"output": "1\n1 1 3"
},
{
"input": "10 10\n0 2 0 9 0 10 6 0 0 0\n0 9 2 0 0 0 4 0 6 0\n0 0 10 0 7 1 5 8 3 0\n1 5 3 4 7 0 8 0 0 0",
"output": "116\n9 2 1\n2 2 2\n4 2 6\n6 2 8\n3 3 10\n8 3 9\n5 3 8\n1 3 7\n7 4 5\n10 3 4\n9 3 1\n2 1 2\n4 2 5\n6 2 7\n3 2 10\n8 3 10\n5 3 9\n1 3 8\n10 3 5\n9 3 2\n4 2 4\n6 1 7\n3 2 9\n8 2 10\n5 3 10\n1 3 9\n10 3 6\n9 3 3\n4 2 3\n3 2 8\n8 2 9\n5 2 10\n1 3 10\n10 3 7\n9 3 4\n4 2 2\n3 2 7\n8 2 8\n5 2 9\n1 2 10\n10 3 8\n9 3 5\n4 2 1\n3 2 6\n8 2 7\n5 2 8\n1 2 9\n10 3 9\n9 3 6\n4 3 1\n3 2 5\n8 2 6\n5 2 7\n1 2 8\n10 3 10\n9 3 7\n4 3 2\n3 2 4\n8 2 5\n5 2 6\n1 2 7\n10 2 10\n9 3 8\n4 3 3\n3 2 3\n8 2 4\n5 2 5\n1 2 6\n10 2 9\n9 3 ..."
},
{
"input": "50 1\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\n0 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\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\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 1",
"output": "68\n1 2 17\n1 2 16\n1 2 15\n1 2 14\n1 2 13\n1 2 12\n1 2 11\n1 2 10\n1 2 9\n1 2 8\n1 2 7\n1 2 6\n1 2 5\n1 2 4\n1 2 3\n1 2 2\n1 2 1\n1 3 1\n1 3 2\n1 3 3\n1 3 4\n1 3 5\n1 3 6\n1 3 7\n1 3 8\n1 3 9\n1 3 10\n1 3 11\n1 3 12\n1 3 13\n1 3 14\n1 3 15\n1 3 16\n1 3 17\n1 3 18\n1 3 19\n1 3 20\n1 3 21\n1 3 22\n1 3 23\n1 3 24\n1 3 25\n1 3 26\n1 3 27\n1 3 28\n1 3 29\n1 3 30\n1 3 31\n1 3 32\n1 3 33\n1 3 34\n1 3 35\n1 3 36\n1 3 37\n1 3 38\n1 3 39\n1 3 40\n1 3 41\n1 3 42\n1 3 43\n1 3 44\n1 3 45\n1 3 46\n1 3 47\n1 3 48\n1 3 4..."
},
{
"input": "40 80\n38 45 18 59 53 44 49 27 46 63 42 61 26 39 29 7 52 79 11 73 24 69 55 43 20 32 37 25 57 19 1 54 4 22 36 16 71 15 65 12\n46 1 52 54 27 3 40 10 8 41 72 17 11 44 28 73 55 65 60 13 12 43 16 26 34 53 50 15 62 35 33 48 58 42 57 80 21 51 64 74\n22 29 4 18 69 36 31 68 77 61 37 6 70 59 78 19 25 71 79 56 30 38 66 2 32 7 47 75 67 39 9 76 49 23 63 24 5 45 20 14\n33 5 50 8 13 17 14 74 10 66 34 58 41 72 2 60 51 77 21 56 70 40 9 35 64 78 68 6 47 23 75 80 28 30 3 76 67 48 62 31",
"output": "3360\n56 4 20\n79 3 20\n71 3 19\n25 3 18\n19 3 17\n78 3 16\n59 3 15\n70 3 14\n6 3 13\n37 3 12\n61 3 11\n77 3 10\n68 3 9\n31 3 8\n36 3 7\n69 3 6\n18 3 5\n4 3 4\n29 3 3\n22 3 2\n46 3 1\n1 2 1\n52 2 2\n54 2 3\n27 2 4\n3 2 5\n40 2 6\n10 2 7\n8 2 8\n41 2 9\n72 2 10\n17 2 11\n11 2 12\n44 2 13\n28 2 14\n73 2 15\n55 2 16\n65 2 17\n60 2 18\n13 2 19\n12 2 20\n43 2 21\n16 2 22\n26 2 23\n34 2 24\n53 2 25\n50 2 26\n15 2 27\n62 2 28\n35 2 29\n33 2 30\n48 2 31\n58 2 32\n42 2 33\n57 2 34\n80 2 35\n21 2 36\n51 2 37\n64 2 3..."
},
{
"input": "40 77\n60 31 50 41 4 12 27 6 65 11 0 34 44 13 42 18 64 15 76 59 36 69 70 71 66 57 37 25 26 2 23 24 45 55 67 29 75 49 33 40\n11 14 65 44 74 51 55 16 19 29 75 41 27 35 69 10 70 2 73 58 45 61 0 7 30 6 23 25 66 63 28 62 24 77 20 43 0 18 50 52\n54 64 60 57 31 8 72 26 76 0 71 48 32 17 12 39 15 67 1 68 36 40 46 49 4 21 56 33 47 3 59 34 9 22 38 53 13 5 37 42\n51 52 30 9 20 62 14 74 38 21 48 0 16 28 43 10 47 72 56 5 17 58 61 53 77 63 0 7 39 54 22 19 3 1 68 46 73 32 8 35",
"output": "3200\n7 2 23\n30 2 24\n6 2 25\n23 2 26\n25 1 28\n66 2 28\n63 2 29\n28 2 30\n62 2 31\n24 2 32\n77 2 33\n20 2 34\n43 2 35\n18 2 37\n50 2 38\n52 2 39\n42 2 40\n37 3 40\n5 3 39\n13 3 38\n53 3 37\n38 3 36\n22 3 35\n9 3 34\n34 3 33\n59 3 32\n3 3 31\n47 3 30\n33 3 29\n56 3 28\n21 3 27\n4 3 26\n49 3 25\n46 3 24\n40 3 23\n36 3 22\n68 3 21\n1 3 20\n67 3 19\n15 3 18\n39 3 17\n12 3 16\n17 3 15\n32 3 14\n48 3 13\n71 3 12\n76 3 10\n26 3 9\n72 3 8\n8 3 7\n31 3 6\n57 3 5\n60 3 4\n64 3 3\n54 3 2\n11 3 1\n14 2 1\n65 2 2\n44..."
},
{
"input": "50 1\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 1 0 0 0 0 0 0 0 0\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\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 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\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",
"output": "34\n1 3 27\n1 3 28\n1 3 29\n1 3 30\n1 3 31\n1 3 32\n1 3 33\n1 3 34\n1 3 35\n1 3 36\n1 3 37\n1 3 38\n1 3 39\n1 3 40\n1 3 41\n1 3 42\n1 3 43\n1 3 44\n1 3 45\n1 3 46\n1 3 47\n1 3 48\n1 3 49\n1 3 50\n1 2 50\n1 2 49\n1 2 48\n1 2 47\n1 2 46\n1 2 45\n1 2 44\n1 2 43\n1 2 42\n1 1 42"
},
{
"input": "37 22\n0 18 0 0 0 16 0 0 0 0 1 21 0 0 0 4 0 15 0 8 0 0 0 0 0 0 0 9 14 0 0 0 22 0 0 3 0\n0 0 0 0 0 21 0 0 2 0 0 0 0 0 0 13 0 0 0 0 0 0 22 12 9 15 11 8 0 16 0 0 0 0 0 0 0\n0 3 1 0 0 0 0 14 0 20 0 7 0 0 0 4 0 6 0 0 5 0 18 0 17 10 0 0 0 0 19 0 0 0 0 0 0\n13 0 2 19 10 0 0 17 0 0 20 0 0 5 11 0 0 6 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0",
"output": "852\n21 2 5\n2 2 8\n13 2 15\n22 2 22\n12 2 23\n9 2 24\n15 2 25\n11 2 26\n8 2 27\n16 2 29\n19 3 32\n10 3 27\n17 3 26\n18 3 24\n5 3 22\n6 4 18\n4 3 17\n7 3 13\n20 3 11\n14 3 9\n1 3 4\n3 3 3\n21 2 4\n2 2 7\n13 2 14\n22 2 21\n12 2 22\n9 2 23\n15 2 24\n11 2 25\n8 2 26\n16 2 28\n19 3 33\n10 3 28\n17 3 27\n18 3 25\n5 3 23\n4 3 18\n7 3 14\n20 4 11\n14 3 10\n1 3 5\n3 3 4\n21 2 3\n2 2 6\n13 2 13\n22 2 20\n12 2 21\n9 2 22\n15 2 23\n11 2 24\n8 2 25\n16 2 27\n19 3 34\n10 3 29\n17 3 28\n18 3 26\n5 3 24\n4 3 19\n7 3 15\n..."
},
{
"input": "37 5\n0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 5 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0",
"output": "151\n3 2 12\n5 2 24\n2 3 26\n1 3 22\n4 3 9\n3 2 11\n5 2 23\n2 3 27\n1 3 23\n4 3 10\n3 2 10\n5 2 22\n2 3 28\n1 3 24\n4 3 11\n3 2 9\n5 2 21\n2 3 29\n1 3 25\n4 3 12\n3 2 8\n5 2 20\n2 3 30\n1 3 26\n4 3 13\n3 2 7\n5 2 19\n2 3 31\n1 3 27\n4 3 14\n3 2 6\n5 2 18\n2 3 32\n1 3 28\n4 3 15\n3 2 5\n5 2 17\n2 3 33\n1 3 29\n4 3 16\n3 2 4\n5 2 16\n2 3 34\n1 3 30\n4 3 17\n3 2 3\n5 2 15\n2 3 35\n1 3 31\n4 3 18\n3 2 2\n5 2 14\n2 3 36\n1 3 32\n4 3 19\n3 2 1\n5 2 13\n2 3 37\n1 3 33\n4 3 20\n3 3 1\n5 2 12\n2 2 37\n1 3 34\n4 3 2..."
},
{
"input": "48 17\n0 0 0 0 0 0 14 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 16 0 0 0 1 0 0 0 3 0 0 15 0 0 0 0 0 0 0 11\n0 0 0 0 0 0 0 0 0 0 17 0 0 0 6 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 13 0 3 10 11 0 0 0 0 0 0 0 0 0\n0 0 0 2 0 0 0 0 0 0 0 0 0 0 4 0 15 0 0 0 0 0 0 0 0 0 0 9 0 0 16 0 0 12 0 0 0 0 0 0 5 0 0 0 0 0 7 14\n0 0 5 13 0 0 0 10 0 0 0 0 17 0 0 0 0 0 0 12 0 0 0 7 0 0 0 0 0 0 9 0 0 0 0 0 6 0 0 0 0 0 4 0 0 0 0 0",
"output": "794\n17 2 10\n6 2 14\n8 2 18\n1 1 33\n13 2 34\n3 1 37\n10 2 37\n11 2 38\n14 2 48\n7 3 48\n5 3 42\n12 3 35\n16 3 32\n9 3 29\n15 3 18\n4 3 16\n2 3 5\n17 2 9\n6 2 13\n8 2 17\n13 2 33\n10 2 36\n11 2 37\n14 2 47\n7 2 48\n5 3 43\n12 3 36\n16 3 33\n9 3 30\n15 3 19\n4 3 17\n2 3 6\n17 2 8\n6 2 12\n8 2 16\n13 2 32\n10 2 35\n11 2 36\n14 2 46\n7 2 47\n5 3 44\n12 3 37\n16 3 34\n9 3 31\n15 3 20\n4 3 18\n2 3 7\n17 2 7\n6 2 11\n8 2 15\n13 2 31\n10 2 34\n11 2 35\n14 2 45\n7 2 46\n5 3 45\n12 3 38\n16 3 35\n9 4 31\n15 3 21\n..."
},
{
"input": "22 2\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0\n0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 0",
"output": "65\n2 2 13\n1 3 21\n2 2 12\n1 3 22\n2 2 11\n1 2 22\n2 2 10\n1 2 21\n2 2 9\n1 2 20\n2 2 8\n1 2 19\n2 2 7\n1 2 18\n2 2 6\n1 2 17\n2 2 5\n1 2 16\n2 2 4\n1 2 15\n2 2 3\n1 2 14\n2 2 2\n1 2 13\n2 2 1\n1 2 12\n2 3 1\n1 2 11\n2 3 2\n1 2 10\n2 3 3\n1 2 9\n2 3 4\n1 2 8\n2 3 5\n1 2 7\n2 3 6\n1 2 6\n2 3 7\n1 2 5\n2 3 8\n1 2 4\n2 3 9\n1 2 3\n2 3 10\n1 2 2\n2 3 11\n1 2 1\n2 3 12\n1 3 1\n2 3 13\n1 3 2\n2 3 14\n1 3 3\n2 3 15\n1 3 4\n2 3 16\n1 3 5\n2 3 17\n1 3 6\n2 3 18\n1 3 7\n2 4 18\n1 3 8\n1 4 8"
},
{
"input": "12 3\n0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0\n2 0 0 0 0 3 0 0 0 1 0 0\n0 0 0 0 0 0 0 1 3 0 2 0",
"output": "38\n1 3 11\n3 3 7\n2 3 2\n1 3 12\n3 3 8\n2 3 3\n1 2 12\n3 3 9\n2 3 4\n1 2 11\n3 4 9\n2 3 5\n1 2 10\n2 3 6\n1 2 9\n2 3 7\n1 2 8\n2 3 8\n1 2 7\n2 3 9\n1 2 6\n2 3 10\n1 2 5\n2 3 11\n1 2 4\n2 4 11\n1 2 3\n1 2 2\n1 2 1\n1 3 1\n1 3 2\n1 3 3\n1 3 4\n1 3 5\n1 3 6\n1 3 7\n1 3 8\n1 4 8"
},
{
"input": "10 20\n18 9 4 5 12 14 16 1 15 20\n11 13 16 6 18 5 20 17 4 3\n12 9 15 14 8 10 2 19 1 7\n6 11 13 2 7 19 10 3 8 17",
"output": "-1"
},
{
"input": "10 20\n1 12 11 7 4 2 13 10 20 9\n18 9 1 5 16 15 8 20 7 13\n2 10 4 12 14 19 3 11 17 6\n3 18 8 6 15 19 16 14 17 5",
"output": "200\n17 4 9\n11 3 9\n3 3 8\n19 4 6\n14 3 6\n12 3 5\n4 3 4\n10 3 3\n2 3 2\n18 3 1\n9 2 1\n1 2 2\n5 2 3\n16 2 4\n15 2 5\n8 2 6\n20 2 7\n7 2 8\n13 2 9\n6 2 10\n11 3 10\n3 3 9\n14 3 7\n12 3 6\n4 3 5\n10 3 4\n2 3 3\n18 3 2\n9 3 1\n1 2 1\n5 2 2\n16 2 3\n15 2 4\n8 2 5\n20 2 6\n7 2 7\n13 2 8\n6 2 9\n11 2 10\n3 3 10\n14 3 8\n12 3 7\n4 3 6\n10 3 5\n2 3 4\n18 4 2\n9 3 2\n1 1 1\n5 2 1\n16 2 2\n15 2 3\n8 2 4\n20 2 5\n7 2 6\n13 2 7\n6 2 8\n11 2 9\n3 2 10\n14 4 8\n12 3 8\n4 3 7\n10 3 6\n2 3 5\n9 3 3\n5 3 1\n16 2 1\n15 2 ..."
},
{
"input": "15 30\n20 24 17 13 26 8 5 6 27 14 18 22 25 2 15\n4 12 6 25 3 5 28 11 15 21 9 26 7 17 13\n19 20 24 16 2 23 8 29 22 30 1 27 10 14 18\n9 29 3 7 12 28 10 16 23 19 21 1 30 11 4",
"output": "-1"
},
{
"input": "40 20\n15 0 0 0 0 0 0 0 7 3 0 0 18 0 0 0 4 0 1 0 0 0 11 0 0 0 0 0 0 0 0 0 5 0 0 14 2 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 12 18 17 0 0 0 0 20 0 6 0 0 14 13 0 19 2 0 0 4 0 0 0 0\n15 0 0 0 0 0 9 0 7 0 0 16 8 5 0 0 0 0 0 0 0 0 0 0 0 10 0 0 11 0 0 0 0 0 0 3 0 0 0 0\n0 16 0 0 0 0 0 17 0 0 0 0 6 0 0 0 8 0 0 0 0 0 0 0 13 10 0 0 19 0 0 0 0 12 9 20 0 0 0 0",
"output": "895\n1 2 11\n12 2 16\n18 2 17\n17 2 18\n20 2 23\n6 2 25\n14 2 28\n13 2 29\n19 2 31\n2 2 32\n4 2 35\n3 3 37\n11 3 30\n10 4 26\n5 3 15\n8 3 14\n16 3 13\n7 3 10\n9 3 8\n15 3 2\n1 2 10\n12 2 15\n18 2 16\n17 2 17\n20 2 22\n6 2 24\n14 2 27\n13 2 28\n19 2 30\n2 2 31\n4 2 34\n3 3 38\n11 3 31\n5 3 16\n8 3 15\n16 3 14\n7 3 11\n9 3 9\n15 3 3\n1 2 9\n12 2 14\n18 2 15\n17 2 16\n20 2 21\n6 2 23\n14 2 26\n13 2 27\n19 2 29\n2 2 30\n4 2 33\n3 3 39\n11 3 32\n5 3 17\n8 3 16\n16 3 15\n7 3 12\n9 3 10\n15 3 4\n1 2 8\n12 2 13\n1..."
}
] | 109 | 307,200 | -1 | 5,810 |
|
863 | Almost Permutation | [
"flows"
] | null | null | Recently Ivan noticed an array *a* while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug.
Ivan clearly remembers that there were *n* elements in the array, and each element was not less than 1 and not greater than *n*. Also he remembers *q* facts about the array. There are two types of facts that Ivan remembers:
- 1 *l**i* *r**i* *v**i* β for each *x* such that *l**i*<=β€<=*x*<=β€<=*r**i* *a**x*<=β₯<=*v**i*; - 2 *l**i* *r**i* *v**i* β for each *x* such that *l**i*<=β€<=*x*<=β€<=*r**i* *a**x*<=β€<=*v**i*.
Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the *q* facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the *cost* of array as follows:
, where *cnt*(*i*) is the number of occurences of *i* in the array.
Help Ivan to determine minimum possible *cost* of the array that corresponds to the facts! | The first line contains two integer numbers *n* and *q* (1<=β€<=*n*<=β€<=50, 0<=β€<=*q*<=β€<=100).
Then *q* lines follow, each representing a fact about the array. *i*-th line contains the numbers *t**i*, *l**i*, *r**i* and *v**i* for *i*-th fact (1<=β€<=*t**i*<=β€<=2, 1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*v**i*<=β€<=*n*, *t**i* denotes the type of the fact). | If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible *cost* of the array. | [
"3 0\n",
"3 1\n1 1 3 2\n",
"3 2\n1 1 3 2\n2 1 3 2\n",
"3 2\n1 1 3 2\n2 1 3 1\n"
] | [
"3\n",
"5\n",
"9\n",
"-1\n"
] | none | [
{
"input": "3 0",
"output": "3"
},
{
"input": "3 1\n1 1 3 2",
"output": "5"
},
{
"input": "3 2\n1 1 3 2\n2 1 3 2",
"output": "9"
},
{
"input": "3 2\n1 1 3 2\n2 1 3 1",
"output": "-1"
},
{
"input": "50 0",
"output": "50"
},
{
"input": "50 1\n2 31 38 25",
"output": "50"
},
{
"input": "50 2\n2 38 41 49\n1 19 25 24",
"output": "50"
},
{
"input": "50 10\n2 4 24 29\n1 14 49 9\n2 21 29 12\n2 2 46 11\n2 4 11 38\n2 3 36 8\n1 24 47 28\n2 23 40 32\n1 16 50 38\n1 31 49 38",
"output": "-1"
},
{
"input": "50 20\n1 14 22 40\n1 23 41 3\n1 32 39 26\n1 8 47 25\n2 5 13 28\n2 2 17 32\n1 23 30 37\n1 33 45 49\n2 13 27 43\n1 30 32 2\n2 28 49 40\n2 33 35 32\n2 5 37 30\n1 45 45 32\n2 6 24 24\n2 28 44 16\n2 36 47 24\n1 5 11 9\n1 9 37 22\n1 28 40 24",
"output": "-1"
},
{
"input": "50 1\n1 12 38 31",
"output": "64"
},
{
"input": "50 2\n2 6 35 37\n1 19 46 44",
"output": "-1"
},
{
"input": "50 10\n1 17 44 44\n2 32 40 4\n2 1 45 31\n1 27 29 16\n1 8 9 28\n2 1 34 16\n2 16 25 2\n2 17 39 32\n1 16 35 34\n1 1 28 12",
"output": "-1"
},
{
"input": "50 20\n1 44 48 43\n1 15 24 9\n2 39 44 25\n1 36 48 35\n1 4 30 27\n1 31 44 15\n2 19 38 22\n2 18 43 24\n1 25 35 10\n2 38 43 5\n2 10 22 21\n2 5 19 30\n1 17 35 26\n1 17 31 10\n2 9 21 1\n2 29 34 10\n2 25 44 21\n2 13 33 13\n2 34 38 9\n2 23 43 4",
"output": "-1"
},
{
"input": "50 1\n2 12 34 9",
"output": "88"
},
{
"input": "50 2\n1 15 16 17\n2 12 35 41",
"output": "50"
},
{
"input": "50 10\n2 31 38 4\n2 33 43 1\n2 33 46 21\n2 37 48 17\n1 12 46 33\n2 25 44 43\n1 12 50 2\n1 15 35 18\n2 9 13 35\n1 2 25 28",
"output": "-1"
},
{
"input": "50 20\n1 7 49 43\n1 10 18 42\n2 10 37 24\n1 45 46 24\n2 5 36 33\n2 17 40 20\n1 22 30 7\n1 5 49 25\n2 18 49 21\n1 43 49 39\n2 9 25 23\n1 10 19 47\n2 36 48 10\n1 25 30 50\n1 15 49 13\n1 10 17 33\n2 8 33 7\n2 28 36 34\n2 40 40 16\n1 1 17 31",
"output": "-1"
},
{
"input": "1 0",
"output": "1"
},
{
"input": "1 1\n1 1 1 1",
"output": "1"
},
{
"input": "50 1\n2 1 2 1",
"output": "52"
},
{
"input": "50 2\n2 1 33 1\n2 14 50 1",
"output": "2500"
},
{
"input": "49 10\n2 17 19 14\n1 6 46 9\n2 19 32 38\n2 27 31 15\n2 38 39 17\n1 30 36 14\n2 35 41 8\n1 18 23 32\n2 8 35 13\n2 24 32 45",
"output": "-1"
},
{
"input": "49 7\n1 17 44 13\n1 14 22 36\n1 27 39 3\n2 20 36 16\n2 29 31 49\n1 32 40 10\n2 4 48 48",
"output": "-1"
},
{
"input": "50 8\n2 11 44 10\n2 2 13 2\n2 23 35 41\n1 16 28 17\n2 21 21 46\n1 22 39 43\n2 10 29 34\n1 17 27 22",
"output": "-1"
},
{
"input": "5 2\n1 1 2 4\n1 3 5 5",
"output": "13"
},
{
"input": "4 3\n2 1 2 2\n1 2 2 2\n2 3 4 1",
"output": "8"
},
{
"input": "5 2\n1 1 5 4\n2 3 5 4",
"output": "13"
},
{
"input": "42 16\n2 33 37 36\n1 14 18 1\n2 24 25 9\n2 4 34 29\n2 32 33 8\n2 27 38 23\n2 1 1 7\n2 15 42 35\n2 37 42 17\n2 8 13 4\n2 19 21 40\n2 37 38 6\n2 33 38 18\n2 12 40 26\n2 27 42 38\n2 40 40 30",
"output": "64"
},
{
"input": "7 3\n2 1 2 2\n1 3 7 2\n2 3 7 3",
"output": "17"
},
{
"input": "29 5\n2 4 9 27\n1 25 29 14\n1 9 10 18\n2 13 13 5\n2 1 19 23",
"output": "29"
},
{
"input": "3 6\n1 1 1 2\n2 1 1 2\n1 2 2 2\n2 2 2 2\n1 3 3 2\n2 3 3 3",
"output": "5"
},
{
"input": "7 14\n1 1 1 1\n2 1 1 6\n1 2 2 1\n2 2 2 5\n1 3 3 1\n2 3 3 6\n1 4 4 5\n2 4 4 7\n1 5 5 1\n2 5 5 2\n1 6 6 2\n2 6 6 2\n1 7 7 5\n2 7 7 5",
"output": "7"
},
{
"input": "8 16\n1 1 1 2\n2 1 1 3\n1 2 2 6\n2 2 2 8\n1 3 3 1\n2 3 3 2\n1 4 4 3\n2 4 4 3\n1 5 5 1\n2 5 5 2\n1 6 6 2\n2 6 6 5\n1 7 7 3\n2 7 7 3\n1 8 8 3\n2 8 8 3",
"output": "16"
}
] | 46 | 0 | 0 | 5,814 |
|
56 | Domino Principle | [
"binary search",
"data structures",
"sortings"
] | E. Domino Principle | 2 | 256 | Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put *n* dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The *i*-th domino has the coordinate *x**i* and the height *h**i*. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.
Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate *x* and height *h* leads to the fall of all dominoes on the segment [*x*<=+<=1,<=*x*<=+<=*h*<=-<=1]. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) which is the number of dominoes. Then follow *n* lines containing two integers *x**i* and *h**i* (<=-<=108<=β€<=*x**i*<=β€<=108,<=2<=β€<=*h**i*<=β€<=108) each, which are the coordinate and height of every domino. No two dominoes stand on one point. | Print *n* space-separated numbers *z**i* β the number of dominoes that will fall if Vasya pushes the *i*-th domino to the right (including the domino itself). | [
"4\n16 5\n20 5\n10 10\n18 2\n",
"4\n0 10\n1 5\n9 10\n15 10\n"
] | [
"3 1 4 1 ",
"4 1 2 1 "
] | none | [
{
"input": "4\n16 5\n20 5\n10 10\n18 2",
"output": "3 1 4 1 "
},
{
"input": "4\n0 10\n1 5\n9 10\n15 10",
"output": "4 1 2 1 "
},
{
"input": "3\n6 7\n2 9\n-6 10",
"output": "1 2 3 "
},
{
"input": "4\n-6 9\n9 5\n-7 4\n0 9",
"output": "2 1 3 1 "
},
{
"input": "5\n-47 19\n-43 14\n83 27\n-67 46\n-58 98",
"output": "2 1 1 4 3 "
},
{
"input": "6\n20 39\n38 13\n-25 48\n-22 38\n13 39\n90 54",
"output": "2 1 5 4 3 1 "
},
{
"input": "10\n34 9\n-41 26\n-62 32\n1 10\n77 14\n92 7\n-45 35\n75 19\n-23 2\n-46 8",
"output": "1 2 5 1 1 1 3 3 1 4 "
}
] | 2,000 | 21,606,400 | 0 | 5,817 |
143 | Help Kingdom of Far Far Away 2 | [
"implementation",
"strings"
] | null | null | For some time the program of rounding numbers that had been developed by the Codeforces participants during one of the previous rounds, helped the citizens of Far Far Away to convert numbers into a more easily readable format. However, as time went by, the economy of the Far Far Away developed and the scale of operations grew. So the King ordered to found the Bank of Far Far Away and very soon even the rounding didn't help to quickly determine even the order of the numbers involved in operations. Besides, rounding a number to an integer wasn't very convenient as a bank needed to operate with all numbers with accuracy of up to 0.01, and not up to an integer.
The King issued yet another order: to introduce financial format to represent numbers denoting amounts of money. The formal rules of storing a number in the financial format are as follows:
- A number contains the integer part and the fractional part. The two parts are separated with a character "." (decimal point). - To make digits in the integer part of a number easier to read, they are split into groups of three digits, starting from the least significant ones. The groups are separated with the character "," (comma). For example, if the integer part of a number equals 12345678, then it will be stored in the financial format as 12,345,678 - In the financial format a number's fractional part should contain exactly two digits. So, if the initial number (the number that is converted into the financial format) contains less than two digits in the fractional part (or contains no digits at all), it is complemented with zeros until its length equals 2. If the fractional part contains more than two digits, the extra digits are simply discarded (they are not rounded: see sample tests). - When a number is stored in the financial format, the minus sign is not written. Instead, if the initial number had the minus sign, the result is written in round brackets. - Please keep in mind that the bank of Far Far Away operates using an exotic foreign currency β snakes ($), that's why right before the number in the financial format we should put the sign "$". If the number should be written in the brackets, then the snake sign should also be inside the brackets.
For example, by the above given rules number 2012 will be stored in the financial format as "$2,012.00" and number -12345678.9 will be stored as "($12,345,678.90)".
The merchants of Far Far Away visited you again and expressed much hope that you supply them with the program that can convert arbitrary numbers to the financial format. Can you help them? | The input contains a number that needs to be converted into financial format. The number's notation length does not exceed 100 characters, including (possible) signs "-" (minus) and "." (decimal point). The number's notation is correct, that is:
- The number's notation only contains characters from the set {"0" β "9", "-", "."}. - The decimal point (if it is present) is unique and is preceded and followed by a non-zero quantity on decimal digits - A number cannot start with digit 0, except for a case when its whole integer part equals zero (in this case the integer parts is guaranteed to be a single zero: "0"). - The minus sign (if it is present) is unique and stands in the very beginning of the number's notation - If a number is identically equal to 0 (that is, if it is written as, for example, "0" or "0.000"), than it is not preceded by the minus sign. - The input data contains no spaces. - The number's notation contains at least one decimal digit. | Print the number given in the input in the financial format by the rules described in the problem statement. | [
"2012\n",
"0.000\n",
"-0.00987654321\n",
"-12345678.9\n"
] | [
"$2,012.00",
"$0.00",
"($0.00)",
"($12,345,678.90)"
] | Pay attention to the second and third sample tests. They show that the sign of a number in the financial format (and consequently, the presence or absence of brackets) is determined solely by the sign of the initial number. It does not depend on the sign of the number you got after translating the number to the financial format. | [
{
"input": "2012",
"output": "$2,012.00"
},
{
"input": "0.000",
"output": "$0.00"
},
{
"input": "-0.00987654321",
"output": "($0.00)"
},
{
"input": "-12345678.9",
"output": "($12,345,678.90)"
},
{
"input": "0.99999999999999999999",
"output": "$0.99"
},
{
"input": "-999999999.9999999999",
"output": "($999,999,999.99)"
},
{
"input": "4.30",
"output": "$4.30"
},
{
"input": "-3136",
"output": "($3,136.00)"
},
{
"input": "47.849",
"output": "$47.84"
},
{
"input": "0",
"output": "$0.00"
},
{
"input": "-1",
"output": "($1.00)"
},
{
"input": "5.3944",
"output": "$5.39"
},
{
"input": "-359789",
"output": "($359,789.00)"
},
{
"input": "-999999",
"output": "($999,999.00)"
},
{
"input": "50117.75",
"output": "$50,117.75"
},
{
"input": "-2717.859",
"output": "($2,717.85)"
},
{
"input": "446900763",
"output": "$446,900,763.00"
},
{
"input": "-92.04295",
"output": "($92.04)"
},
{
"input": "1000000000",
"output": "$1,000,000,000.00"
},
{
"input": "-4097961.5",
"output": "($4,097,961.50)"
},
{
"input": "-83348637.91",
"output": "($83,348,637.91)"
},
{
"input": "741968647.01",
"output": "$741,968,647.01"
},
{
"input": "8590210736.2",
"output": "$8,590,210,736.20"
},
{
"input": "-337322633.10",
"output": "($337,322,633.10)"
},
{
"input": "-9389724657.706",
"output": "($9,389,724,657.70)"
},
{
"input": "-337807291537795",
"output": "($337,807,291,537,795.00)"
},
{
"input": "-1000000000000000",
"output": "($1,000,000,000,000,000.00)"
},
{
"input": "1000000000000000000",
"output": "$1,000,000,000,000,000,000.00"
},
{
"input": "64852365412711705.4",
"output": "$64,852,365,412,711,705.40"
},
{
"input": "-14193044875680849641.0",
"output": "($14,193,044,875,680,849,641.00)"
},
{
"input": "-9087207850675188568.44",
"output": "($9,087,207,850,675,188,568.44)"
},
{
"input": "-999999999999999999999999",
"output": "($999,999,999,999,999,999,999,999.00)"
},
{
"input": "95464737206897655595566.87",
"output": "$95,464,737,206,897,655,595,566.87"
},
{
"input": "20486447414118.916680683147",
"output": "$20,486,447,414,118.91"
},
{
"input": "-195688513344900667321324887161",
"output": "($195,688,513,344,900,667,321,324,887,161.00)"
},
{
"input": "-467854663215578391335472070.522",
"output": "($467,854,663,215,578,391,335,472,070.52)"
},
{
"input": "-9946519009668593136622791780335166786329.966",
"output": "($9,946,519,009,668,593,136,622,791,780,335,166,786,329.96)"
},
{
"input": "-39243277445578948100023610303161362.21742597518",
"output": "($39,243,277,445,578,948,100,023,610,303,161,362.21)"
},
{
"input": "-999999999999999999999999999999999999999999999999",
"output": "($999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999.00)"
},
{
"input": "-1120451303595201012675538441508298946450567446.2",
"output": "($1,120,451,303,595,201,012,675,538,441,508,298,946,450,567,446.20)"
},
{
"input": "-667416497168265603150839581334265910632362977345",
"output": "($667,416,497,168,265,603,150,839,581,334,265,910,632,362,977,345.00)"
},
{
"input": "-5896634442314348289084387258044853039981310264175",
"output": "($5,896,634,442,314,348,289,084,387,258,044,853,039,981,310,264,175.00)"
},
{
"input": "645862132625704263852654466816044056725411814537812.8",
"output": "$645,862,132,625,704,263,852,654,466,816,044,056,725,411,814,537,812.80"
},
{
"input": "20302284249108248013254029284738266163210459601273.434",
"output": "$20,302,284,249,108,248,013,254,029,284,738,266,163,210,459,601,273.43"
},
{
"input": "-335585948391999514421347454725980775593710083728376.235",
"output": "($335,585,948,391,999,514,421,347,454,725,980,775,593,710,083,728,376.23)"
},
{
"input": "8069847002922332743537016743686274581681180388843128677728",
"output": "$8,069,847,002,922,332,743,537,016,743,686,274,581,681,180,388,843,128,677,728.00"
},
{
"input": "-1000000000000000000000000000000000000000000000000000000000",
"output": "($1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.00)"
},
{
"input": "-9426928046528138766008648709237083850143959438752.99576081",
"output": "($9,426,928,046,528,138,766,008,648,709,237,083,850,143,959,438,752.99)"
},
{
"input": "7847469828916401598273845389736502122924911071339770925.278",
"output": "$7,847,469,828,916,401,598,273,845,389,736,502,122,924,911,071,339,770,925.27"
},
{
"input": "6612569248276041501392573128342394934.339553169499895358359857",
"output": "$6,612,569,248,276,041,501,392,573,128,342,394,934.33"
},
{
"input": "-78441689173753107674674252785635804718172761356557153691194.62",
"output": "($78,441,689,173,753,107,674,674,252,785,635,804,718,172,761,356,557,153,691,194.62)"
},
{
"input": "-26420799441242046176813573049397911227605022448441841.79118151",
"output": "($26,420,799,441,242,046,176,813,573,049,397,911,227,605,022,448,441,841.79)"
},
{
"input": "1000000000000000000000000000000000000000000000000000000000000000",
"output": "$1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.00"
},
{
"input": "-440176280332493569864975483046616452663067706833582934195268991",
"output": "($440,176,280,332,493,569,864,975,483,046,616,452,663,067,706,833,582,934,195,268,991.00)"
},
{
"input": "45068840874548394281603568826222223550419177965629777875090709223",
"output": "$45,068,840,874,548,394,281,603,568,826,222,223,550,419,177,965,629,777,875,090,709,223.00"
},
{
"input": "694057847299426980275391007402296515925594191675094941155586653678",
"output": "$694,057,847,299,426,980,275,391,007,402,296,515,925,594,191,675,094,941,155,586,653,678.00"
},
{
"input": "-957970608566623530128907769981235852029999876705137521027635757.983",
"output": "($957,970,608,566,623,530,128,907,769,981,235,852,029,999,876,705,137,521,027,635,757.98)"
},
{
"input": "-999999999999999999999999999999999999999999999999999999999999999999999999",
"output": "($999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999.00)"
},
{
"input": "-31237099946005389291000524337411657445033712616943108265479899943319776753",
"output": "($31,237,099,946,005,389,291,000,524,337,411,657,445,033,712,616,943,108,265,479,899,943,319,776,753.00)"
},
{
"input": "129213728483376896322034359636257815625283844448760915618261775174758145181.4",
"output": "$129,213,728,483,376,896,322,034,359,636,257,815,625,283,844,448,760,915,618,261,775,174,758,145,181.40"
},
{
"input": "42436883801797921017002508329344377731225676938894736357215113693696441876.74",
"output": "$42,436,883,801,797,921,017,002,508,329,344,377,731,225,676,938,894,736,357,215,113,693,696,441,876.74"
},
{
"input": "-412877493852539226130846658848085431323015500045621801.186290244529330637919069841",
"output": "($412,877,493,852,539,226,130,846,658,848,085,431,323,015,500,045,621,801.18)"
},
{
"input": "-574893403412500337461904214575009975847859132644288548328404148513112616299380872537.0",
"output": "($574,893,403,412,500,337,461,904,214,575,009,975,847,859,132,644,288,548,328,404,148,513,112,616,299,380,872,537.00)"
},
{
"input": "5533548446182725508036320768515297517684533355269108005785922527441026147032711096226.86",
"output": "$5,533,548,446,182,725,508,036,320,768,515,297,517,684,533,355,269,108,005,785,922,527,441,026,147,032,711,096,226.86"
},
{
"input": "-388992510982960799226860251113727086.40151448032429506491841194161722800219231951466273",
"output": "($388,992,510,982,960,799,226,860,251,113,727,086.40)"
},
{
"input": "-1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "($1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.00)"
},
{
"input": "-5918197227517459215086434488069169077399840893456742554562785165395986123057440893145094.766",
"output": "($5,918,197,227,517,459,215,086,434,488,069,169,077,399,840,893,456,742,554,562,785,165,395,986,123,057,440,893,145,094.76)"
},
{
"input": "6478564388953796549388720554132845507729109849868298957775985580270942075809511904097608680.2",
"output": "$6,478,564,388,953,796,549,388,720,554,132,845,507,729,109,849,868,298,957,775,985,580,270,942,075,809,511,904,097,608,680.20"
},
{
"input": "-6608605342368730994322893748034318039589361759849416904183711274389684094202666590051634245034124",
"output": "($6,608,605,342,368,730,994,322,893,748,034,318,039,589,361,759,849,416,904,183,711,274,389,684,094,202,666,590,051,634,245,034,124.00)"
},
{
"input": "96923618713643049034901616201059739110612607940570171931128836281408507843006798661841666493086.61",
"output": "$96,923,618,713,643,049,034,901,616,201,059,739,110,612,607,940,570,171,931,128,836,281,408,507,843,006,798,661,841,666,493,086.61"
},
{
"input": "-517546026888198271507158769760866655703910236108772942356185789408213495267854245076096353651979.8",
"output": "($517,546,026,888,198,271,507,158,769,760,866,655,703,910,236,108,772,942,356,185,789,408,213,495,267,854,245,076,096,353,651,979.80)"
},
{
"input": "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
"output": "$9,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999.00"
},
{
"input": "-999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
"output": "($999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999.00)"
},
{
"input": "-815237564329654906966710129877160169011275185850610159260306644937525319275278007248384181194947.28",
"output": "($815,237,564,329,654,906,966,710,129,877,160,169,011,275,185,850,610,159,260,306,644,937,525,319,275,278,007,248,384,181,194,947.28)"
},
{
"input": "1609444903206838610558177906619581955157825950595724445549624361368550861446891019160980179056621441",
"output": "$1,609,444,903,206,838,610,558,177,906,619,581,955,157,825,950,595,724,445,549,624,361,368,550,861,446,891,019,160,980,179,056,621,441.00"
},
{
"input": "-35537407322675227867508928547215513270324784786663652634725025510744878530809034357724640012675.565",
"output": "($35,537,407,322,675,227,867,508,928,547,215,513,270,324,784,786,663,652,634,725,025,510,744,878,530,809,034,357,724,640,012,675.56)"
},
{
"input": "-1925998064032579186735317615389112142155311850475835576562145669565982488184005786899836428580775.0",
"output": "($1,925,998,064,032,579,186,735,317,615,389,112,142,155,311,850,475,835,576,562,145,669,565,982,488,184,005,786,899,836,428,580,775.00)"
},
{
"input": "-151277365498121078756232179307020255183838049147325207397719920725067524511168597227357027671262974",
"output": "($151,277,365,498,121,078,756,232,179,307,020,255,183,838,049,147,325,207,397,719,920,725,067,524,511,168,597,227,357,027,671,262,974.00)"
},
{
"input": "-94567610568172711079874848395505663034158058453541356405687412896214661991252184312404537628616.980",
"output": "($94,567,610,568,172,711,079,874,848,395,505,663,034,158,058,453,541,356,405,687,412,896,214,661,991,252,184,312,404,537,628,616.98)"
},
{
"input": "5552014028917125934664874618128879449020166415278427980290619767043458191075263555779358121.76899621",
"output": "$5,552,014,028,917,125,934,664,874,618,128,879,449,020,166,415,278,427,980,290,619,767,043,458,191,075,263,555,779,358,121.76"
},
{
"input": "2550200914539395142436748539585175024948346405871252468705518320188561734542212313710731590053887.14",
"output": "$2,550,200,914,539,395,142,436,748,539,585,175,024,948,346,405,871,252,468,705,518,320,188,561,734,542,212,313,710,731,590,053,887.14"
},
{
"input": "169111053680418810505586659748530205695340474893994150913915241455549545588046718243429009096899.721",
"output": "$169,111,053,680,418,810,505,586,659,748,530,205,695,340,474,893,994,150,913,915,241,455,549,545,588,046,718,243,429,009,096,899.72"
},
{
"input": "-8302081723264231257651127829066891591565707300162037272443063737275775635240827533455570038921755.8",
"output": "($8,302,081,723,264,231,257,651,127,829,066,891,591,565,707,300,162,037,272,443,063,737,275,775,635,240,827,533,455,570,038,921,755.80)"
},
{
"input": "-292248618257633380305171416004365379539463749949334547640267733391588708052597413502241817581110.84",
"output": "($292,248,618,257,633,380,305,171,416,004,365,379,539,463,749,949,334,547,640,267,733,391,588,708,052,597,413,502,241,817,581,110.84)"
},
{
"input": "8087188987747615879025660857396187057475326352182448073610839965896456538717186544887072170343027939",
"output": "$8,087,188,987,747,615,879,025,660,857,396,187,057,475,326,352,182,448,073,610,839,965,896,456,538,717,186,544,887,072,170,343,027,939.00"
},
{
"input": "762519263820550209316662292240308083373767394981759714.037848496865152996658249820591156785758954539",
"output": "$762,519,263,820,550,209,316,662,292,240,308,083,373,767,394,981,759,714.03"
},
{
"input": "-81065814290895584254457019744497055053248932892817738718849487679519028041818854925725440291395.398",
"output": "($81,065,814,290,895,584,254,457,019,744,497,055,053,248,932,892,817,738,718,849,487,679,519,028,041,818,854,925,725,440,291,395.39)"
},
{
"input": "-32941712101597478543219921523193493949615291911649974076128866311848385268672190709108207764990.550",
"output": "($32,941,712,101,597,478,543,219,921,523,193,493,949,615,291,911,649,974,076,128,866,311,848,385,268,672,190,709,108,207,764,990.55)"
},
{
"input": "2089113443991831781611590658416581830404242017.85102926202385542583311855337073083712400492547136479",
"output": "$2,089,113,443,991,831,781,611,590,658,416,581,830,404,242,017.85"
},
{
"input": "-93446155923266881322196606839694485100712773936897171033382798807975023881552872455711005123932.747",
"output": "($93,446,155,923,266,881,322,196,606,839,694,485,100,712,773,936,897,171,033,382,798,807,975,023,881,552,872,455,711,005,123,932.74)"
},
{
"input": "960516596871944593730108478032758053821336372808735358607440437077013969634756697387966042842288.508",
"output": "$960,516,596,871,944,593,730,108,478,032,758,053,821,336,372,808,735,358,607,440,437,077,013,969,634,756,697,387,966,042,842,288.50"
},
{
"input": "7542946645993289345871768107036410651745989844030221776852993379463784193885567707317993804499615689",
"output": "$7,542,946,645,993,289,345,871,768,107,036,410,651,745,989,844,030,221,776,852,993,379,463,784,193,885,567,707,317,993,804,499,615,689.00"
},
{
"input": "-62833497045916718064314002220718776776624697240820362462669558147156815011509869423334004968891.075",
"output": "($62,833,497,045,916,718,064,314,002,220,718,776,776,624,697,240,820,362,462,669,558,147,156,815,011,509,869,423,334,004,968,891.07)"
},
{
"input": "369983878656471317107141313973936685655559201630341263457253892446495.822347697919107135036916507458",
"output": "$369,983,878,656,471,317,107,141,313,973,936,685,655,559,201,630,341,263,457,253,892,446,495.82"
},
{
"input": "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "$1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.00"
},
{
"input": "-7200722479435658295856375503813639375609209638447823589904775057990210002452424572601761458228411.3",
"output": "($7,200,722,479,435,658,295,856,375,503,813,639,375,609,209,638,447,823,589,904,775,057,990,210,002,452,424,572,601,761,458,228,411.30)"
},
{
"input": "1.62929379626674077244098830537592273171157251593607257308766051098303017164327540412154291842807913",
"output": "$1.62"
},
{
"input": "9094697811219913240397316094992038813655777565859532452.35345453828434088557646454113264025096745262",
"output": "$9,094,697,811,219,913,240,397,316,094,992,038,813,655,777,565,859,532,452.35"
},
{
"input": "-241995182456075514870952227695034085165209475359259147742565065759917424411707290789641890279251.11",
"output": "($241,995,182,456,075,514,870,952,227,695,034,085,165,209,475,359,259,147,742,565,065,759,917,424,411,707,290,789,641,890,279,251.11)"
},
{
"input": "2567340036354357844391998756110821468858185018763415770617907336824217629234299240638243305079104961",
"output": "$2,567,340,036,354,357,844,391,998,756,110,821,468,858,185,018,763,415,770,617,907,336,824,217,629,234,299,240,638,243,305,079,104,961.00"
}
] | 92 | 512,000 | 0 | 5,824 |
|
172 | Phone Code | [
"*special",
"brute force",
"implementation"
] | null | null | Polycarpus has *n* friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they are strings *s*1,<=*s*2,<=...,<=*s**n*. All these strings consist only of digits and have the same length.
Once Polycarpus needed to figure out Tarasov city phone code. He assumed that the phone code of the city is the longest common prefix of all phone numbers of his friends. In other words, it is the longest string *c* which is a prefix (the beginning) of each *s**i* for all *i* (1<=β€<=*i*<=β€<=*n*). Help Polycarpus determine the length of the city phone code. | The first line of the input contains an integer *n* (2<=β€<=*n*<=β€<=3Β·104) β the number of Polycarpus's friends. The following *n* lines contain strings *s*1,<=*s*2,<=...,<=*s**n* β the phone numbers of Polycarpus's friends. It is guaranteed that all strings consist only of digits and have the same length from 1 to 20, inclusive. It is also guaranteed that all strings are different. | Print the number of digits in the city phone code. | [
"4\n00209\n00219\n00999\n00909\n",
"2\n1\n2\n",
"3\n77012345678999999999\n77012345678901234567\n77012345678998765432\n"
] | [
"2\n",
"0\n",
"12\n"
] | A prefix of string *t* is a string that is obtained by deleting zero or more digits from the end of string *t*. For example, string "00209" has 6 prefixes: "" (an empty prefix), "0", "00", "002", "0020", "00209".
In the first sample the city phone code is string "00".
In the second sample the city phone code is an empty string.
In the third sample the city phone code is string "770123456789". | [
{
"input": "4\n00209\n00219\n00999\n00909",
"output": "2"
},
{
"input": "2\n1\n2",
"output": "0"
},
{
"input": "3\n77012345678999999999\n77012345678901234567\n77012345678998765432",
"output": "12"
},
{
"input": "5\n4491183345\n4491184811\n4491162340\n4491233399\n4491449214",
"output": "4"
},
{
"input": "10\n15424\n10953\n19176\n15514\n16284\n18680\n19305\n13816\n16168\n15924",
"output": "1"
},
{
"input": "10\n4906361343\n8985777485\n1204265609\n7088384855\n4127287014\n7904807820\n3032139021\n5999959109\n6477458281\n3244359368",
"output": "0"
},
{
"input": "10\n3717208309\n3717208306\n3717208302\n3717208301\n3717208303\n3717208308\n3717208304\n3717208307\n3717208300\n3717208305",
"output": "9"
},
{
"input": "9\n2881\n2808\n2868\n2874\n2894\n2870\n2818\n2896\n2890",
"output": "2"
},
{
"input": "2\n4\n9",
"output": "0"
},
{
"input": "2\n29867863763143509570\n59261213969200291523",
"output": "0"
},
{
"input": "2\n84\n62",
"output": "0"
},
{
"input": "2\n75970434466248453472\n75970434466248453476",
"output": "19"
},
{
"input": "10\n17254072509168593435\n17254072509168593433\n17254072509168593430\n17254072509168593432\n17254072509168593439\n17254072509168593436\n17254072509168593438\n17254072509168593437\n17254072509168593431\n17254072509168593434",
"output": "19"
}
] | 248 | 2,969,600 | 3 | 5,833 |
|
888 | Almost Identity Permutations | [
"combinatorics",
"dp",
"math"
] | null | null | A permutation *p* of size *n* is an array such that every integer from 1 to *n* occurs exactly once in this array.
Let's call a permutation an almost identity permutation iff there exist at least *n*<=-<=*k* indices *i* (1<=β€<=*i*<=β€<=*n*) such that *p**i*<==<=*i*.
Your task is to count the number of almost identity permutations for given numbers *n* and *k*. | The first line contains two integers *n* and *k* (4<=β€<=*n*<=β€<=1000, 1<=β€<=*k*<=β€<=4). | Print the number of almost identity permutations for given *n* and *k*. | [
"4 1\n",
"4 2\n",
"5 3\n",
"5 4\n"
] | [
"1\n",
"7\n",
"31\n",
"76\n"
] | none | [
{
"input": "4 1",
"output": "1"
},
{
"input": "4 2",
"output": "7"
},
{
"input": "5 3",
"output": "31"
},
{
"input": "5 4",
"output": "76"
},
{
"input": "200 1",
"output": "1"
},
{
"input": "200 2",
"output": "19901"
},
{
"input": "200 3",
"output": "2646701"
},
{
"input": "200 4",
"output": "584811251"
},
{
"input": "400 1",
"output": "1"
},
{
"input": "400 2",
"output": "79801"
},
{
"input": "400 3",
"output": "21253401"
},
{
"input": "400 4",
"output": "9477912501"
},
{
"input": "600 1",
"output": "1"
},
{
"input": "600 2",
"output": "179701"
},
{
"input": "600 3",
"output": "71820101"
},
{
"input": "600 4",
"output": "48187303751"
},
{
"input": "800 1",
"output": "1"
},
{
"input": "800 2",
"output": "319601"
},
{
"input": "800 3",
"output": "170346801"
},
{
"input": "800 4",
"output": "152620985001"
},
{
"input": "1000 1",
"output": "1"
},
{
"input": "1000 2",
"output": "499501"
},
{
"input": "1000 3",
"output": "332833501"
},
{
"input": "1000 4",
"output": "373086956251"
},
{
"input": "4 4",
"output": "24"
}
] | 93 | 2,355,200 | 3 | 5,837 |
|
48 | Permutations | [
"greedy"
] | D. Permutations | 1 | 256 | A permutation is a sequence of integers from 1 to *n* of length *n* containing each number exactly once. For example, (1), (4,<=3,<=5,<=1,<=2), (3,<=2,<=1) are permutations, and (1,<=1), (4,<=3,<=1), (2,<=3,<=4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105). The next line contains the mixed array of *n* integers, divided with a single space. The numbers in the array are from 1 to 105. | If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain *n* numbers, corresponding to the elements of the given array. If the *i*-th element belongs to the first permutation, the *i*-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line <=-<=1. | [
"9\n1 2 3 1 2 1 4 2 5\n",
"4\n4 3 2 1\n",
"4\n1 2 2 3\n"
] | [
"3\n3 1 2 1 2 2 2 3 2\n",
"1\n1 1 1 1 ",
"-1\n"
] | In the first sample test the array is split into three permutations: (2,β1), (3,β2,β1,β4,β5), (1,β2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible. | [
{
"input": "9\n1 2 3 1 2 1 4 2 5",
"output": "3\n1 1 1 2 2 3 1 3 1 "
},
{
"input": "4\n4 3 2 1",
"output": "1\n1 1 1 1 "
},
{
"input": "4\n1 2 2 3",
"output": "-1"
},
{
"input": "1\n1",
"output": "1\n1 "
},
{
"input": "1\n2",
"output": "-1"
},
{
"input": "5\n1 1 1 1 1",
"output": "5\n1 2 3 4 5 "
},
{
"input": "3\n2 1 1",
"output": "2\n1 1 2 "
},
{
"input": "6\n3 3 2 2 1 1",
"output": "2\n1 2 1 2 1 2 "
},
{
"input": "2\n1000 1",
"output": "-1"
},
{
"input": "5\n2 2 1 1 3",
"output": "2\n1 2 1 2 1 "
},
{
"input": "10\n2 1 2 4 6 1 5 3 7 1",
"output": "3\n1 1 2 1 1 2 1 1 1 3 "
},
{
"input": "10\n4 1 2 1 3 3 1 2 2 1",
"output": "4\n1 1 1 2 1 2 3 2 3 4 "
},
{
"input": "10\n1 2 5 1 1 1 4 1 3 2",
"output": "5\n1 1 1 2 3 4 1 5 1 2 "
},
{
"input": "20\n2 7 3 8 4 6 3 7 6 4 13 5 1 12 1 10 2 11 5 9",
"output": "2\n1 1 1 1 1 1 2 2 2 2 1 1 1 1 2 1 2 1 2 1 "
},
{
"input": "20\n1 1 1 2 3 1 5 9 5 8 4 6 7 3 1 2 2 1 3 4",
"output": "6\n1 2 3 1 1 4 1 1 2 1 1 1 1 2 5 2 3 6 3 2 "
},
{
"input": "20\n2 10 3 3 2 1 14 13 2 15 1 4 5 12 7 11 9 1 6 8",
"output": "3\n1 1 1 2 2 1 1 1 3 1 2 1 1 1 1 1 1 3 1 1 "
},
{
"input": "20\n1 7 2 3 1 1 8 1 6 1 9 11 5 10 1 4 2 3 1 2",
"output": "7\n1 1 1 1 2 3 1 4 1 5 1 1 1 1 6 1 2 2 7 3 "
},
{
"input": "30\n6 1 2 3 6 4 1 8 1 2 2 5 5 1 1 3 9 1 5 8 1 2 7 7 4 3 1 3 4 2",
"output": "8\n1 1 1 1 2 1 2 1 3 2 3 1 2 4 5 2 1 6 3 2 7 4 1 2 2 3 8 4 3 5 "
},
{
"input": "30\n2 6 2 3 3 1 4 2 1 3 3 2 1 2 1 8 1 2 4 1 1 1 5 1 4 7 1 9 1 1",
"output": "12\n1 1 2 1 2 1 1 3 2 3 4 4 3 5 4 1 5 6 2 6 7 8 1 9 3 1 10 1 11 12 "
},
{
"input": "30\n1 3 2 5 9 4 16 14 2 2 4 11 7 17 1 15 13 3 6 12 6 19 8 1 20 5 18 4 10 3",
"output": "3\n1 1 1 1 1 1 1 1 2 3 2 1 1 1 2 1 1 2 1 1 2 1 1 3 1 2 1 3 1 3 "
},
{
"input": "10\n2 2 6 3 1 4 5 3 7 7",
"output": "-1"
},
{
"input": "20\n4 6 6 4 5 4 3 2 5 7 3 2 4 1 3 1 1 4 1 7",
"output": "-1"
},
{
"input": "30\n2 8 3 3 7 4 2 9 4 3 5 6 1 5 3 5 8 1 9 6 6 7 2 7 1 1 1 10 2 1",
"output": "-1"
},
{
"input": "30\n8 7 9 6 2 3 7 1 1 5 7 2 3 1 7 4 5 6 3 9 4 9 4 2 3 1 1 2 2 10",
"output": "-1"
},
{
"input": "50\n7 1 6 5 15 3 13 7 1 1 4 2 4 3 2 1 11 9 4 2 3 7 1 1 1 14 3 14 5 2 5 4 1 8 2 2 2 2 1 1 4 1 2 3 6 12 1 1 5 1",
"output": "-1"
},
{
"input": "50\n1 1 4 1 1 1 1 1 1 3 1 1 3 2 1 1 1 1 5 2 1 1 1 1 1 3 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "41\n1 2 1 3 4 5 6 7 8 1 9 10 2 1 11 12 13 14 1 2 15 16 17 18 19 3 20 21 22 23 24 25 3 26 27 4 28 29 30 31 32 33 34 35 36 37 38 39 40 41 "
},
{
"input": "100\n2 13 10 4 13 8 22 11 5 3 4 6 19 4 8 8 6 1 16 4 11 17 5 18 7 7 4 5 3 7 2 16 5 6 10 1 6 12 14 6 8 7 9 7 1 2 1 8 5 5 9 21 7 11 6 1 12 10 6 23 10 9 8 4 1 2 3 13 2 14 15 1 1 12 3 9 12 3 13 9 8 1 12 5 2 3 11 7 11 9 3 14 1 2 15 2 10 4 14 20",
"output": "10\n1 1 1 1 2 1 1 1 1 1 2 1 1 3 2 3 2 1 1 4 2 1 2 1 1 2 5 3 2 3 2 2 4 3 2 2 4 1 1 5 4 4 1 5 3 3 4 5 5 6 2 1 6 3 6 5 2 3 7 1 4 3 6 6 6 4 3 3 5 2 1 7 8 3 4 4 4 5 4 5 7 9 5 7 6 6 4 7 5 6 7 3 10 7 2 8 5 7 4 1 "
},
{
"input": "100\n9 6 3 28 10 2 2 11 2 1 25 3 13 5 14 13 4 14 2 16 12 27 8 1 7 9 8 19 33 23 4 1 15 6 7 12 2 8 30 4 1 31 6 1 15 5 18 3 2 24 7 3 1 20 10 8 26 22 3 3 9 6 1 10 1 5 1 3 7 6 11 10 1 16 19 5 9 4 4 4 2 18 12 21 11 5 2 32 17 29 2 4 8 1 7 5 3 2 17 1",
"output": "12\n1 1 1 1 1 1 2 1 3 1 1 2 1 1 1 2 1 2 4 1 1 1 1 2 1 2 2 1 1 1 2 3 1 2 2 2 5 3 1 3 4 1 3 5 2 2 1 3 6 1 3 4 6 1 2 4 1 1 5 6 3 4 7 3 8 3 9 7 4 5 2 4 10 2 2 4 4 4 5 6 7 2 3 1 3 5 8 1 1 1 9 7 5 11 5 6 8 10 2 12 "
},
{
"input": "100\n12 18 1 1 14 23 1 1 22 5 7 9 7 1 1 1 3 8 4 2 1 6 9 1 3 2 11 1 11 2 3 2 1 4 2 7 1 16 3 4 2 13 3 1 5 11 2 10 20 24 3 21 5 2 6 2 1 10 10 5 17 1 1 4 19 8 5 5 3 9 4 2 7 8 10 4 9 1 3 3 9 7 6 4 4 3 6 8 12 1 3 6 2 1 8 4 1 15 2 5",
"output": "20\n1 1 1 2 1 1 3 4 1 1 1 1 2 5 6 7 1 1 1 1 8 1 2 9 2 2 1 10 2 3 3 4 11 2 5 3 12 1 4 3 6 1 5 13 2 3 7 1 1 1 6 1 3 8 2 9 14 2 3 4 1 15 16 4 1 2 5 6 7 3 5 10 4 3 4 6 4 17 8 9 5 5 3 7 8 10 4 4 2 18 11 5 11 19 5 9 20 1 12 7 "
},
{
"input": "100\n10 1 13 1 5 2 5 5 9 10 3 8 4 1 3 5 6 4 1 4 9 8 1 1 1 1 8 2 3 1 1 2 5 1 1 12 6 4 5 3 1 3 18 10 1 2 3 2 6 2 3 15 1 3 5 3 9 7 1 6 11 7 7 8 6 17 11 7 6 1 4 4 1 1 3 1 2 6 7 14 4 4 5 1 11 1 4 2 8 4 2 7 16 12 1 1 2 2 1 2",
"output": "25\n1 1 1 2 1 1 2 3 1 2 1 1 1 3 2 4 1 2 4 3 2 2 5 6 7 8 3 2 3 9 10 3 5 11 12 1 2 4 6 4 13 5 1 3 14 4 6 5 3 6 7 1 15 8 7 9 3 1 16 4 1 2 3 4 5 1 2 4 6 17 5 6 18 19 10 20 7 7 5 1 7 8 8 21 3 22 9 8 5 10 9 6 1 2 23 24 10 11 25 12 "
}
] | 93 | 0 | 0 | 5,838 |
923 | Producing Snow | [
"binary search",
"data structures"
] | null | null | Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a giftΒ β a large snow maker. He plans to make some amount of snow every day. On day *i* he will make a pile of snow of volume *V**i* and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is *T**i*, each pile will reduce its volume by *T**i*. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day *i* already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day. | The first line contains a single integer *N* (1<=β€<=*N*<=β€<=105)Β β the number of days.
The second line contains *N* integers *V*1,<=*V*2,<=...,<=*V**N* (0<=β€<=*V**i*<=β€<=109), where *V**i* is the initial size of a snow pile made on the day *i*.
The third line contains *N* integers *T*1,<=*T*2,<=...,<=*T**N* (0<=β€<=*T**i*<=β€<=109), where *T**i* is the temperature on the day *i*. | Output a single line with *N* integers, where the *i*-th integer represents the total volume of snow melted on day *i*. | [
"3\n10 10 5\n5 7 2\n",
"5\n30 25 20 15 10\n9 10 12 4 13\n"
] | [
"5 12 4\n",
"9 20 35 11 25\n"
] | In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day. | [
{
"input": "3\n10 10 5\n5 7 2",
"output": "5 12 4"
},
{
"input": "5\n30 25 20 15 10\n9 10 12 4 13",
"output": "9 20 35 11 25"
},
{
"input": "4\n0 0 0 0\n1 2 3 4",
"output": "0 0 0 0"
},
{
"input": "10\n11 39 16 34 25 3 12 11 31 16\n10 0 4 9 8 9 7 8 9 2",
"output": "10 0 9 27 27 30 28 17 12 4"
},
{
"input": "10\n20 35 4 0 6 29 4 9 17 10\n0 9 4 7 5 1 4 3 9 4",
"output": "0 18 12 14 10 3 12 9 26 12"
},
{
"input": "1\n4\n5",
"output": "4"
},
{
"input": "1\n5\n4",
"output": "4"
},
{
"input": "1\n5\n5",
"output": "5"
},
{
"input": "2\n9 3\n8 2",
"output": "8 3"
},
{
"input": "2\n9 3\n4 4",
"output": "4 7"
},
{
"input": "2\n9 3\n10 2",
"output": "9 2"
},
{
"input": "13\n1 1 1 1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "1 1 1 1 1 1 1 1 1 1 1 1 1"
}
] | 686 | 37,068,800 | 3 | 5,839 |
|
1,003 | Intense Heat | [
"brute force",
"implementation",
"math"
] | null | null | The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are.
Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows:
Suppose we want to analyze the segment of $n$ consecutive days. We have measured the temperatures during these $n$ days; the temperature during $i$-th day equals $a_i$.
We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $x$ to day $y$, we calculate it as $\frac{\sum \limits_{i = x}^{y} a_i}{y - x + 1}$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $k$ consecutive days. For example, if analyzing the measures $[3, 4, 1, 2]$ and $k = 3$, we are interested in segments $[3, 4, 1]$, $[4, 1, 2]$ and $[3, 4, 1, 2]$ (we want to find the maximum value of average temperature over these segments).
You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task? | The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 5000$) β the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 5000$) β the temperature measures during given $n$ days. | Print one real number β the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $k$ consecutive days.
Your answer will be considered correct if the following condition holds: $|res - res_0| < 10^{-6}$, where $res$ is your answer, and $res_0$ is the answer given by the jury's solution. | [
"4 3\n3 4 1 2\n"
] | [
"2.666666666666667\n"
] | none | [
{
"input": "4 3\n3 4 1 2",
"output": "2.666666666666667"
},
{
"input": "5 1\n3 10 9 10 6",
"output": "10.000000000000000"
},
{
"input": "5 2\n7 3 3 1 8",
"output": "5.000000000000000"
},
{
"input": "5 3\n1 7 6 9 1",
"output": "7.333333333333333"
},
{
"input": "5 4\n5 1 10 6 1",
"output": "5.500000000000000"
},
{
"input": "5 5\n4 6 6 6 2",
"output": "4.800000000000000"
},
{
"input": "3 2\n2 1 2",
"output": "1.666666666666667"
},
{
"input": "1 1\n5000",
"output": "5000.000000000000000"
}
] | 4,000 | 0 | 0 | 5,868 |
|
850 | Five Dimensional Points | [
"brute force",
"geometry",
"math"
] | null | null | You are given set of *n* points in 5-dimensional space. The points are labeled from 1 to *n*. No two points coincide.
We will call point *a* bad if there are different points *b* and *c*, not equal to *a*, from the given set such that angle between vectors and is acute (i.e. strictly less than ). Otherwise, the point is called good.
The angle between vectors and in 5-dimensional space is defined as , where is the scalar product and is length of .
Given the list of points, print the indices of the good points in ascending order. | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=103)Β β the number of points.
The next *n* lines of input contain five integers *a**i*,<=*b**i*,<=*c**i*,<=*d**i*,<=*e**i* (|*a**i*|,<=|*b**i*|,<=|*c**i*|,<=|*d**i*|,<=|*e**i*|<=β€<=103) Β β the coordinates of the i-th point. All points are distinct. | First, print a single integer *k*Β β the number of good points.
Then, print *k* integers, each on their own lineΒ β the indices of the good points in ascending order. | [
"6\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 0 1\n",
"3\n0 0 1 2 0\n0 0 9 2 0\n0 0 5 9 0\n"
] | [
"1\n1\n",
"0\n"
] | In the first sample, the first point forms exactly a <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/362ac8c7a7496dffc06cd0c843287cd822be63c3.png" style="max-width: 100.0%;max-height: 100.0%;"/> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<img class="tex-graphics" src="https://espresso.codeforces.com/982219f7eb73ea120de10dd91baa59317fe7af64.png" style="max-width: 100.0%;max-height: 100.0%;"/>
We can see that all angles here are acute, so no points are good. | [
{
"input": "6\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 0 1",
"output": "1\n1"
},
{
"input": "3\n0 0 1 2 0\n0 0 9 2 0\n0 0 5 9 0",
"output": "0"
},
{
"input": "1\n0 0 0 0 0",
"output": "1\n1"
},
{
"input": "2\n0 1 2 3 4\n5 6 7 8 9",
"output": "2\n1\n2"
},
{
"input": "10\n0 -110 68 -51 -155\n-85 -110 68 -51 -155\n85 -70 51 68 -230\n0 -40 51 68 75\n0 5 -51 -68 -190\n85 0 0 0 0\n85 -115 -68 51 35\n85 -75 -187 34 -40\n-85 -110 -136 102 -155\n85 -110 -17 119 -155",
"output": "0"
},
{
"input": "6\n-305 -390 638 -623 343\n479 755 -343 144 89\n-268 843 -461 989 -301\n-986 -274 347 -847 -728\n278 718 -372 -674 270\n-477 562 -489 -858 611",
"output": "0"
},
{
"input": "10\n-705 38 170 -768 689\n-705 86 248 -768 709\n-705 86 170 -742 709\n-705 86 144 -768 709\n-705 86 170 -820 709\n-705 106 170 -768 661\n-822 86 170 -768 709\n-705 98 170 -768 714\n-705 86 170 -768 709\n-601 86 170 -768 709",
"output": "1\n9"
},
{
"input": "11\n358 -724 -232 53 -520\n486 -554 -328 53 -220\n358 -554 -232 -372 -520\n358 -554 -232 308 -520\n868 -554 448 53 -520\n478 -554 -322 53 -600\n358 296 -232 53 -520\n256 -554 -368 53 -520\n230 -554 -136 53 -820\n-182 -554 173 53 -160\n358 -554 -232 53 -520",
"output": "1\n11"
},
{
"input": "8\n-559 581 509 257 343\n-544 451 569 277 343\n-451 451 434 401 343\n-559 451 509 257 83\n-664 451 89 117 343\n-559 451 509 257 993\n-715 451 509 374 343\n-811 451 684 -79 343",
"output": "0"
},
{
"input": "11\n8 8 8 8 8\n2 2 2 2 2\n0 0 0 0 0\n6 6 6 6 6\n7 7 7 7 7\n10 10 10 10 10\n9 9 9 9 9\n3 3 3 3 3\n1 1 1 1 1\n5 5 5 5 5\n4 4 4 4 4",
"output": "0"
},
{
"input": "7\n49 457 -650 325 -325\n0 0 325 325 0\n253 204 -325 0 -325\n204 -253 325 325 325\n408 -506 -325 -325 325\n49 457 -650 325 -650\n0 0 0 650 -325",
"output": "0"
},
{
"input": "11\n1 0 0 0 0\n-1 0 0 0 0\n0 1 0 0 0\n0 -1 0 0 0\n0 0 1 0 0\n0 0 -1 0 0\n0 0 0 1 0\n0 0 0 -1 0\n0 0 0 0 1\n0 0 0 0 -1\n0 0 0 0 0",
"output": "1\n11"
},
{
"input": "4\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 1 1 0 0",
"output": "0"
}
] | 46 | 0 | 0 | 5,871 |
|
863 | Yet Another Array Queries Problem | [
"data structures",
"implementation"
] | null | null | You are given an array *a* of size *n*, and *q* queries to it. There are queries of two types:
- 1 *l**i* *r**i* β perform a cyclic shift of the segment [*l**i*,<=*r**i*] to the right. That is, for every *x* such that *l**i*<=β€<=*x*<=<<=*r**i* new value of *a**x*<=+<=1 becomes equal to old value of *a**x*, and new value of *a**l**i* becomes equal to old value of *a**r**i*; - 2 *l**i* *r**i* β reverse the segment [*l**i*,<=*r**i*].
There are *m* important indices in the array *b*1, *b*2, ..., *b**m*. For each *i* such that 1<=β€<=*i*<=β€<=*m* you have to output the number that will have index *b**i* in the array after all queries are performed. | The first line contains three integer numbers *n*, *q* and *m* (1<=β€<=*n*,<=*q*<=β€<=2Β·105, 1<=β€<=*m*<=β€<=100).
The second line contains *n* integer numbers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109).
Then *q* lines follow. *i*-th of them contains three integer numbers *t**i*, *l**i*, *r**i*, where *t**i* is the type of *i*-th query, and [*l**i*,<=*r**i*] is the segment where this query is performed (1<=β€<=*t**i*<=β€<=2, 1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*).
The last line contains *m* integer numbers *b*1, *b*2, ..., *b**m* (1<=β€<=*b**i*<=β€<=*n*) β important indices of the array. | Print *m* numbers, *i*-th of which is equal to the number at index *b**i* after all queries are done. | [
"6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3\n"
] | [
"3 3 1 5 2 \n"
] | none | [
{
"input": "6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3",
"output": "3 3 1 5 2 "
},
{
"input": "5 2 5\n64 3 4 665 2\n1 1 3\n2 1 5\n1 2 3 4 5",
"output": "2 665 3 64 4 "
},
{
"input": "1 1 1\n474812122\n2 1 1\n1",
"output": "474812122 "
}
] | 156 | 102,400 | 0 | 5,882 |
|
151 | Phone Numbers | [
"implementation",
"strings"
] | null | null | Winters are just damn freezing cold in Nvodsk! That's why a group of *n* friends prefers to take a taxi, order a pizza and call girls. The phone numbers in the city consist of three pairs of digits (for example, 12-34-56). Each friend has a phonebook of size *s**i* (that's the number of phone numbers). We know that taxi numbers consist of six identical digits (for example, 22-22-22), the numbers of pizza deliveries should necessarily be decreasing sequences of six different digits (for example, 98-73-21), all other numbers are the girls' numbers.
You are given your friends' phone books. Calculate which friend is best to go to when you are interested in each of those things (who has maximal number of phone numbers of each type).
If the phone book of one person contains some number two times, you should count it twice. That is, each number should be taken into consideration the number of times it occurs in the phone book. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=100) β the number of friends.
Then follow *n* data blocks that describe each friend's phone books. Each block is presented in the following form: first goes the line that contains integer *s**i* and string *name**i* (0<=β€<=*s**i*<=β€<=100) β the number of phone numbers in the phone book of the *i*-th friend and the name of the *i*-th friend. The name is a non-empty sequence of uppercase and lowercase Latin letters, containing no more than 20 characters. Next *s**i* lines contain numbers as "XX-XX-XX", where X is arbitrary digits from 0 to 9. | In the first line print the phrase "If you want to call a taxi, you should call: ". Then print names of all friends whose phone books contain maximal number of taxi phone numbers.
In the second line print the phrase "If you want to order a pizza, you should call: ". Then print names of all friends who have maximal number of pizza phone numbers.
In the third line print the phrase "If you want to go to a cafe with a wonderful girl, you should call: ". Then print names of all friends who have maximal number of girls' phone numbers.
Print the names in the order in which they are given in the input data. Separate two consecutive names with a comma and a space. Each line should end with exactly one point. For clarifications concerning the output form, see sample tests. It is necessary that you follow the output form strictly. Extra spaces are not allowed. | [
"4\n2 Fedorov\n22-22-22\n98-76-54\n3 Melnikov\n75-19-09\n23-45-67\n99-99-98\n7 Rogulenko\n22-22-22\n11-11-11\n33-33-33\n44-44-44\n55-55-55\n66-66-66\n95-43-21\n3 Kaluzhin\n11-11-11\n99-99-99\n98-65-32\n",
"3\n5 Gleb\n66-66-66\n55-55-55\n01-01-01\n65-43-21\n12-34-56\n3 Serega\n55-55-55\n87-65-43\n65-55-21\n5 Melnik\n12-42-12\n87-73-01\n36-04-12\n88-12-22\n82-11-43\n",
"3\n3 Kulczynski\n22-22-22\n65-43-21\n98-12-00\n4 Pachocki\n11-11-11\n11-11-11\n11-11-11\n98-76-54\n0 Smietanka\n"
] | [
"If you want to call a taxi, you should call: Rogulenko.\nIf you want to order a pizza, you should call: Fedorov, Rogulenko, Kaluzhin.\nIf you want to go to a cafe with a wonderful girl, you should call: Melnikov.\n",
"If you want to call a taxi, you should call: Gleb.\nIf you want to order a pizza, you should call: Gleb, Serega.\nIf you want to go to a cafe with a wonderful girl, you should call: Melnik.\n",
"If you want to call a taxi, you should call: Pachocki.\nIf you want to order a pizza, you should call: Kulczynski, Pachocki.\nIf you want to go to a cafe with a wonderful girl, you should call: Kulczynski.\n"
] | In the first sample you are given four friends. Fedorov's phone book contains one taxi number and one pizza delivery number, Melnikov's phone book only has 3 numbers of girls, Rogulenko's one has 6 taxi numbers and one pizza delivery number, Kaluzhin's one contains 2 taxi numbers and one pizza delivery number.
Thus, if you need to order a taxi, you should obviously call Rogulenko, if you need to order a pizza you should call anybody of the following: Rogulenko, Fedorov, Kaluzhin (each of them has one number). Melnikov has maximal number of phone numbers of girls. | [
{
"input": "4\n2 Fedorov\n22-22-22\n98-76-54\n3 Melnikov\n75-19-09\n23-45-67\n99-99-98\n7 Rogulenko\n22-22-22\n11-11-11\n33-33-33\n44-44-44\n55-55-55\n66-66-66\n95-43-21\n3 Kaluzhin\n11-11-11\n99-99-99\n98-65-32",
"output": "If you want to call a taxi, you should call: Rogulenko.\nIf you want to order a pizza, you should call: Fedorov, Rogulenko, Kaluzhin.\nIf you want to go to a cafe with a wonderful girl, you should call: Melnikov."
},
{
"input": "3\n5 Gleb\n66-66-66\n55-55-55\n01-01-01\n65-43-21\n12-34-56\n3 Serega\n55-55-55\n87-65-43\n65-55-21\n5 Melnik\n12-42-12\n87-73-01\n36-04-12\n88-12-22\n82-11-43",
"output": "If you want to call a taxi, you should call: Gleb.\nIf you want to order a pizza, you should call: Gleb, Serega.\nIf you want to go to a cafe with a wonderful girl, you should call: Melnik."
},
{
"input": "3\n3 Kulczynski\n22-22-22\n65-43-21\n98-12-00\n4 Pachocki\n11-11-11\n11-11-11\n11-11-11\n98-76-54\n0 Smietanka",
"output": "If you want to call a taxi, you should call: Pachocki.\nIf you want to order a pizza, you should call: Kulczynski, Pachocki.\nIf you want to go to a cafe with a wonderful girl, you should call: Kulczynski."
},
{
"input": "4\n0 Gleb\n0 Sergey\n0 Sasha\n0 HrenSGori",
"output": "If you want to call a taxi, you should call: Gleb, Sergey, Sasha, HrenSGori.\nIf you want to order a pizza, you should call: Gleb, Sergey, Sasha, HrenSGori.\nIf you want to go to a cafe with a wonderful girl, you should call: Gleb, Sergey, Sasha, HrenSGori."
},
{
"input": "5\n0 PmfItzXdroG\n0 HRykTUCkxgOaD\n0 fcHIUkrn\n2 eUvyUuXFvvuYobrFBxe\n98-76-32\n02-21-39\n8 VUMvHy\n97-65-41\n65-70-26\n54-49-11\n33-33-33\n76-54-31\n11-11-11\n82-95-22\n98-75-31",
"output": "If you want to call a taxi, you should call: VUMvHy.\nIf you want to order a pizza, you should call: VUMvHy.\nIf you want to go to a cafe with a wonderful girl, you should call: VUMvHy."
},
{
"input": "5\n2 ZaxsHjkGMPxZgwzpya\n94-20-75\n96-54-32\n2 gAiJXEYwXU\n11-11-11\n77-77-77\n1 j\n86-43-10\n1 dRJrc\n98-76-21\n2 UAiXZTnBKDoKb\n35-19-89\n98-65-40",
"output": "If you want to call a taxi, you should call: gAiJXEYwXU.\nIf you want to order a pizza, you should call: ZaxsHjkGMPxZgwzpya, j, dRJrc, UAiXZTnBKDoKb.\nIf you want to go to a cafe with a wonderful girl, you should call: ZaxsHjkGMPxZgwzpya, UAiXZTnBKDoKb."
},
{
"input": "5\n4 vKHeRjJubHZ\n11-11-11\n99-99-99\n00-00-00\n52-73-46\n6 hckQfheNMOgZVsa\n96-53-20\n50-69-33\n64-78-80\n77-77-77\n06-10-48\n33-39-96\n1 RykElQYdYbQfqlrk\n97-43-21\n4 GDptSUmbYqkjW\n87-42-10\n56-87-67\n86-54-20\n65-43-10\n6 jUEgOK\n87-65-40\n05-90-59\n06-32-30\n44-57-02\n48-78-94\n55-55-55",
"output": "If you want to call a taxi, you should call: vKHeRjJubHZ.\nIf you want to order a pizza, you should call: GDptSUmbYqkjW.\nIf you want to go to a cafe with a wonderful girl, you should call: hckQfheNMOgZVsa, jUEgOK."
},
{
"input": "10\n5 eeleGlOFWbcnIPPtnll\n55-55-55\n00-00-00\n98-65-32\n76-43-10\n98-76-54\n2 DMBiqRyQJkFvHPJNJp\n28-97-50\n87-64-10\n4 bAfmtnKHohIX\n61-58-93\n77-77-77\n53-17-51\n96-43-10\n3 cDX\n22-22-22\n77-77-77\n63-30-64\n1 HCeHJ\n44-44-44\n6 HgSpfAolwoaBQ\n96-93-53\n98-53-10\n33-33-33\n66-66-66\n87-54-32\n11-11-11\n0 hn\n7 qGRocddf\n74-34-87\n97-53-20\n76-32-10\n54-32-10\n98-74-21\n33-33-33\n00-00-00\n5 XrdtbTC\n99-99-99\n86-53-20\n96-34-97\n75-43-20\n85-32-10\n0 gDLEXYNyoDSgSLJSec",
"output": "If you want to call a taxi, you should call: HgSpfAolwoaBQ.\nIf you want to order a pizza, you should call: qGRocddf.\nIf you want to go to a cafe with a wonderful girl, you should call: bAfmtnKHohIX."
},
{
"input": "3\n5 hieu\n11-22-33\n22-33-55\n33-66-22\n99-00-22\n55-33-11\n4 duong\n11-11-11\n22-22-22\n33-33-33\n44-44-44\n3 quan\n98-76-54\n76-54-32\n65-43-21",
"output": "If you want to call a taxi, you should call: duong.\nIf you want to order a pizza, you should call: quan.\nIf you want to go to a cafe with a wonderful girl, you should call: hieu."
},
{
"input": "2\n3 ha\n11-11-11\n98-76-54\n12-34-56\n1 haha\n98-76-55",
"output": "If you want to call a taxi, you should call: ha.\nIf you want to order a pizza, you should call: ha.\nIf you want to go to a cafe with a wonderful girl, you should call: ha, haha."
},
{
"input": "2\n2 Alex\n12-12-12\n99-87-76\n2 Mula\n22-22-22\n99-87-76",
"output": "If you want to call a taxi, you should call: Mula.\nIf you want to order a pizza, you should call: Alex, Mula.\nIf you want to go to a cafe with a wonderful girl, you should call: Alex."
},
{
"input": "2\n2 Alex\n12-12-12\n99-98-76\n2 Mula\n22-22-22\n99-98-76",
"output": "If you want to call a taxi, you should call: Mula.\nIf you want to order a pizza, you should call: Alex, Mula.\nIf you want to go to a cafe with a wonderful girl, you should call: Alex."
},
{
"input": "3\n5 Gleb\n66-66-66\n55-55-55\n01-01-01\n65-43-21\n12-34-56\n8 Serega\n55-55-55\n87-65-43\n65-55-21\n11-22-33\n11-22-33\n11-22-33\n11-22-33\n11-22-33\n5 Melnik\n12-42-12\n87-73-01\n36-04-12\n88-12-22\n82-11-43",
"output": "If you want to call a taxi, you should call: Gleb.\nIf you want to order a pizza, you should call: Gleb, Serega.\nIf you want to go to a cafe with a wonderful girl, you should call: Serega."
},
{
"input": "2\n2 sur\n32-32-32\n43-43-43\n2 sun\n22-22-22\n23-41-31",
"output": "If you want to call a taxi, you should call: sun.\nIf you want to order a pizza, you should call: sur, sun.\nIf you want to go to a cafe with a wonderful girl, you should call: sur."
},
{
"input": "3\n1 Fedorov\n21-21-21\n1 Melnikov\n99-99-99\n1 Rogulenko\n95-43-21",
"output": "If you want to call a taxi, you should call: Melnikov.\nIf you want to order a pizza, you should call: Rogulenko.\nIf you want to go to a cafe with a wonderful girl, you should call: Fedorov."
},
{
"input": "3\n5 Gleb\n66-66-66\n55-55-55\n01-01-01\n65-43-21\n12-34-56\n8 Serega\n55-55-55\n87-65-43\n65-55-21\n56-34-12\n56-34-12\n56-34-12\n56-34-12\n56-34-12\n5 Melnik\n12-42-12\n87-73-01\n36-04-12\n88-12-22\n82-11-43",
"output": "If you want to call a taxi, you should call: Gleb.\nIf you want to order a pizza, you should call: Gleb, Serega.\nIf you want to go to a cafe with a wonderful girl, you should call: Serega."
}
] | 46 | 0 | 0 | 5,883 |
|
567 | Berland National Library | [
"implementation"
] | null | null | Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.
Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form "reader entered room", "reader left room". Every reader is assigned a registration number during the registration procedure at the library β it's a unique integer from 1 to 106. Thus, the system logs events of two forms:
- "+ *r**i*" β the reader with registration number *r**i* entered the room; - "- *r**i*" β the reader with registration number *r**i* left the room.
The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors.
Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence.
Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you. | The first line contains a positive integer *n* (1<=β€<=*n*<=β€<=100) β the number of records in the system log. Next follow *n* events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ *r**i*" or "- *r**i*", where *r**i* is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers).
It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors. | Print a single integer β the minimum possible capacity of the reading room. | [
"6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7\n",
"2\n- 1\n- 2\n",
"2\n+ 1\n- 1\n"
] | [
"3",
"2",
"1"
] | In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3. | [
{
"input": "6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7",
"output": "3"
},
{
"input": "2\n- 1\n- 2",
"output": "2"
},
{
"input": "2\n+ 1\n- 1",
"output": "1"
},
{
"input": "5\n+ 1\n- 1\n+ 2\n+ 3\n- 4",
"output": "3"
},
{
"input": "3\n- 1\n- 2\n- 3",
"output": "3"
},
{
"input": "4\n+ 1\n+ 2\n- 1\n+ 3",
"output": "2"
},
{
"input": "6\n+ 1\n+ 2\n- 1\n+ 3\n- 2\n+ 4",
"output": "2"
},
{
"input": "3\n+ 1\n+ 2\n- 3",
"output": "3"
},
{
"input": "3\n- 1\n+ 2\n- 2",
"output": "1"
},
{
"input": "4\n- 1\n- 2\n+ 3\n+ 4",
"output": "2"
},
{
"input": "1\n+ 1",
"output": "1"
},
{
"input": "1\n- 1",
"output": "1"
},
{
"input": "3\n- 1\n+ 1\n- 1",
"output": "1"
},
{
"input": "10\n+ 1\n+ 2\n+ 3\n+ 4\n+ 5\n+ 6\n+ 7\n+ 8\n+ 9\n+ 10",
"output": "10"
},
{
"input": "5\n+ 5\n+ 4\n- 4\n- 5\n+ 5",
"output": "2"
},
{
"input": "50\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100\n+ 100\n- 100",
"output": "1"
},
{
"input": "10\n- 8\n- 4\n+ 8\n+ 10\n+ 6\n- 8\n+ 9\n- 2\n- 7\n+ 4",
"output": "5"
},
{
"input": "20\n+ 3\n- 3\n- 2\n+ 2\n+ 3\n- 5\n- 1\n+ 1\n- 3\n+ 4\n- 1\n+ 1\n+ 3\n- 3\n+ 5\n- 2\n- 1\n+ 2\n+ 1\n- 5",
"output": "4"
},
{
"input": "50\n+ 4\n+ 5\n+ 3\n+ 2\n- 2\n- 3\n- 4\n+ 3\n+ 2\n- 3\n+ 4\n- 2\n- 4\n+ 2\n+ 3\n- 3\n- 5\n- 1\n+ 4\n+ 5\n- 5\n+ 3\n- 4\n- 3\n- 2\n+ 4\n+ 3\n+ 2\n- 2\n- 4\n+ 5\n+ 1\n+ 4\n+ 2\n- 2\n+ 2\n- 3\n- 5\n- 4\n- 1\n+ 5\n- 2\n- 5\n+ 5\n+ 3\n- 3\n+ 1\n+ 3\n+ 2\n- 1",
"output": "5"
},
{
"input": "10\n- 2\n+ 1\n- 1\n+ 2\n- 2\n+ 2\n+ 1\n- 1\n- 2\n+ 1",
"output": "2"
},
{
"input": "50\n+ 1\n+ 2\n+ 3\n+ 4\n+ 5\n+ 6\n+ 7\n+ 8\n+ 9\n+ 10\n+ 11\n+ 12\n+ 13\n+ 14\n+ 15\n+ 16\n+ 17\n+ 18\n+ 19\n+ 20\n+ 21\n+ 22\n+ 23\n+ 24\n+ 25\n+ 26\n+ 27\n+ 28\n+ 29\n+ 30\n+ 31\n+ 32\n+ 33\n+ 34\n+ 35\n+ 36\n+ 37\n+ 38\n+ 39\n+ 40\n+ 41\n+ 42\n+ 43\n+ 44\n+ 45\n+ 46\n+ 47\n+ 48\n+ 49\n+ 50",
"output": "50"
},
{
"input": "50\n- 1\n- 2\n- 3\n- 4\n- 5\n- 6\n- 7\n- 8\n- 9\n- 10\n- 11\n- 12\n- 13\n- 14\n- 15\n- 16\n- 17\n- 18\n- 19\n- 20\n- 21\n- 22\n- 23\n- 24\n- 25\n- 26\n- 27\n- 28\n- 29\n- 30\n- 31\n- 32\n- 33\n- 34\n- 35\n- 36\n- 37\n- 38\n- 39\n- 40\n- 41\n- 42\n- 43\n- 44\n- 45\n- 46\n- 47\n- 48\n- 49\n- 50",
"output": "50"
}
] | 78 | 0 | 0 | 5,903 |
|
552 | Vanya and Scales | [
"brute force",
"dp",
"greedy",
"math",
"meet-in-the-middle",
"number theory"
] | null | null | Vanya has a scales for weighing loads and weights of masses *w*0,<=*w*1,<=*w*2,<=...,<=*w*100 grams where *w* is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass *m* using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass *m* and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance. | The first line contains two integers *w*,<=*m* (2<=β€<=*w*<=β€<=109, 1<=β€<=*m*<=β€<=109) β the number defining the masses of the weights and the mass of the item. | Print word 'YES' if the item can be weighted and 'NO' if it cannot. | [
"3 7\n",
"100 99\n",
"100 50\n"
] | [
"YES\n",
"YES\n",
"NO\n"
] | Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7β+β3β=β9β+β1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input. | [
{
"input": "3 7",
"output": "YES"
},
{
"input": "100 99",
"output": "YES"
},
{
"input": "100 50",
"output": "NO"
},
{
"input": "1000000000 1",
"output": "YES"
},
{
"input": "100 10002",
"output": "NO"
},
{
"input": "4 7",
"output": "NO"
},
{
"input": "4 11",
"output": "YES"
},
{
"input": "5 781",
"output": "YES"
},
{
"input": "7 9",
"output": "NO"
},
{
"input": "5077 5988",
"output": "NO"
},
{
"input": "2 9596",
"output": "YES"
},
{
"input": "4 1069",
"output": "YES"
},
{
"input": "4 7134",
"output": "NO"
},
{
"input": "4 9083",
"output": "NO"
},
{
"input": "4 7927",
"output": "NO"
},
{
"input": "4 6772",
"output": "NO"
},
{
"input": "5 782",
"output": "NO"
},
{
"input": "4 1000000000",
"output": "NO"
},
{
"input": "4 357913941",
"output": "YES"
},
{
"input": "4 357918037",
"output": "NO"
},
{
"input": "5 12207031",
"output": "YES"
},
{
"input": "5 41503906",
"output": "YES"
},
{
"input": "5 90332031",
"output": "NO"
},
{
"input": "11 1786324",
"output": "YES"
},
{
"input": "10 999",
"output": "YES"
},
{
"input": "8 28087",
"output": "YES"
},
{
"input": "8 28598",
"output": "NO"
},
{
"input": "32 33586176",
"output": "YES"
},
{
"input": "87 56631258",
"output": "YES"
},
{
"input": "19 20",
"output": "YES"
},
{
"input": "58 11316496",
"output": "YES"
},
{
"input": "89 89",
"output": "YES"
},
{
"input": "21 85756882",
"output": "YES"
},
{
"input": "56 540897225",
"output": "YES"
},
{
"input": "91 8189",
"output": "YES"
},
{
"input": "27 14329927",
"output": "YES"
},
{
"input": "58 198535",
"output": "YES"
},
{
"input": "939 938",
"output": "YES"
},
{
"input": "27463 754243832",
"output": "YES"
},
{
"input": "21427 459137757",
"output": "YES"
},
{
"input": "26045 26045",
"output": "YES"
},
{
"input": "25336 25336",
"output": "YES"
},
{
"input": "24627 24626",
"output": "YES"
},
{
"input": "29245 855299270",
"output": "YES"
},
{
"input": "28536 814274759",
"output": "YES"
},
{
"input": "33154 33155",
"output": "YES"
},
{
"input": "27118 27119",
"output": "YES"
},
{
"input": "70 338171",
"output": "YES"
},
{
"input": "24 346226",
"output": "NO"
},
{
"input": "41 2966964",
"output": "NO"
},
{
"input": "31 29792",
"output": "YES"
},
{
"input": "48 2402",
"output": "NO"
},
{
"input": "65 4159",
"output": "YES"
},
{
"input": "20 67376840",
"output": "NO"
},
{
"input": "72 5111",
"output": "YES"
},
{
"input": "27 14349609",
"output": "YES"
},
{
"input": "44 89146",
"output": "NO"
},
{
"input": "22787 519292944",
"output": "NO"
},
{
"input": "24525 601475624",
"output": "YES"
},
{
"input": "3716 13816089",
"output": "NO"
},
{
"input": "4020 4020",
"output": "YES"
},
{
"input": "13766 13767",
"output": "YES"
},
{
"input": "23512 23511",
"output": "YES"
},
{
"input": "23816 567225671",
"output": "YES"
},
{
"input": "33562 33564",
"output": "NO"
},
{
"input": "33866 33866",
"output": "YES"
},
{
"input": "13057 13059",
"output": "NO"
},
{
"input": "441890232 441890232",
"output": "YES"
},
{
"input": "401739553 401739553",
"output": "YES"
},
{
"input": "285681920 285681919",
"output": "YES"
},
{
"input": "464591587 464591588",
"output": "YES"
},
{
"input": "703722884 703722884",
"output": "YES"
},
{
"input": "982276216 982276216",
"output": "YES"
},
{
"input": "867871061 867871062",
"output": "YES"
},
{
"input": "48433217 48433216",
"output": "YES"
},
{
"input": "8 324818663",
"output": "NO"
},
{
"input": "7 898367507",
"output": "NO"
},
{
"input": "6 471916351",
"output": "NO"
},
{
"input": "5 45465196",
"output": "NO"
},
{
"input": "9 768757144",
"output": "NO"
},
{
"input": "8 342305988",
"output": "NO"
},
{
"input": "6 114457122",
"output": "NO"
},
{
"input": "6 688005966",
"output": "NO"
},
{
"input": "4 556522107",
"output": "NO"
},
{
"input": "3 130070951",
"output": "YES"
},
{
"input": "6 558395604",
"output": "NO"
},
{
"input": "5 131944448",
"output": "NO"
},
{
"input": "2 1000000",
"output": "YES"
},
{
"input": "2 22222222",
"output": "YES"
},
{
"input": "3 100000000",
"output": "YES"
},
{
"input": "3 100000001",
"output": "YES"
},
{
"input": "3 100000002",
"output": "YES"
},
{
"input": "3 100000003",
"output": "YES"
},
{
"input": "3 100000004",
"output": "YES"
},
{
"input": "2 1",
"output": "YES"
},
{
"input": "2 1000000000",
"output": "YES"
},
{
"input": "3 1000000000",
"output": "YES"
},
{
"input": "99999 1000000000",
"output": "NO"
},
{
"input": "10 1000000000",
"output": "YES"
},
{
"input": "1000 1000000000",
"output": "YES"
},
{
"input": "10 999999999",
"output": "YES"
},
{
"input": "100 99999999",
"output": "YES"
},
{
"input": "1000 999999999",
"output": "YES"
},
{
"input": "1000 999999998",
"output": "NO"
},
{
"input": "2 536870912",
"output": "YES"
},
{
"input": "10 99",
"output": "YES"
},
{
"input": "10 8",
"output": "NO"
},
{
"input": "3 5",
"output": "YES"
},
{
"input": "3 26",
"output": "YES"
},
{
"input": "10 8888",
"output": "NO"
},
{
"input": "3 8",
"output": "YES"
},
{
"input": "3 984742145",
"output": "YES"
},
{
"input": "4 43",
"output": "YES"
},
{
"input": "1000000000 1000000000",
"output": "YES"
},
{
"input": "4194304 4194305",
"output": "YES"
},
{
"input": "10 899",
"output": "YES"
},
{
"input": "4 47",
"output": "YES"
},
{
"input": "4 822083581",
"output": "YES"
},
{
"input": "3 999987989",
"output": "YES"
},
{
"input": "4 31",
"output": "NO"
},
{
"input": "4 15",
"output": "YES"
},
{
"input": "100000000 100000001",
"output": "YES"
}
] | 108 | 0 | 0 | 5,924 |
|
0 | none | [
"none"
] | null | null | From beginning till end, this message has been waiting to be conveyed.
For a given unordered multiset of *n* lowercase English letters ("multi" means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation *n*<=-<=1 times:
- Remove any two elements *s* and *t* from the set, and add their concatenation *s*<=+<=*t* to the set.
The cost of such operation is defined to be , where *f*(*s*,<=*c*) denotes the number of times character *c* appears in string *s*.
Given a non-negative integer *k*, construct any valid non-empty set of no more than 100<=000 letters, such that the minimum accumulative cost of the whole process is exactly *k*. It can be shown that a solution always exists. | The first and only line of input contains a non-negative integer *k* (0<=β€<=*k*<=β€<=100<=000) β the required minimum cost. | Output a non-empty string of no more than 100<=000 lowercase English letters β any multiset satisfying the requirements, concatenated to be a string.
Note that the printed string doesn't need to be the final concatenated string. It only needs to represent an unordered multiset of letters. | [
"12\n",
"3\n"
] | [
"abababab\n",
"codeforces\n"
] | For the multiset {'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'}, one of the ways to complete the process is as follows:
- {"ab", "a", "b", "a", "b", "a", "b"}, with a cost of 0; - {"aba", "b", "a", "b", "a", "b"}, with a cost of 1; - {"abab", "a", "b", "a", "b"}, with a cost of 1; - {"abab", "ab", "a", "b"}, with a cost of 0; - {"abab", "aba", "b"}, with a cost of 1; - {"abab", "abab"}, with a cost of 1; - {"abababab"}, with a cost of 8.
The total cost is 12, and it can be proved to be the minimum cost of the process. | [
{
"input": "12",
"output": "abababab"
},
{
"input": "3",
"output": "codeforces"
},
{
"input": "0",
"output": "o"
},
{
"input": "2",
"output": "aabb"
},
{
"input": "5",
"output": "aaabbcc"
},
{
"input": "10",
"output": "aaaaa"
},
{
"input": "233",
"output": "ooououououououououooohhhhhhaaiiiiiibbjjjjjjcckkkkkkddlllllleemmmmmmffnnnnnnggzzzzzz"
},
{
"input": "418",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbcccc"
},
{
"input": "100000",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbcccccccccccc"
},
{
"input": "1",
"output": "aa"
},
{
"input": "4",
"output": "aaabb"
},
{
"input": "6",
"output": "aaaa"
},
{
"input": "7",
"output": "aaaabb"
},
{
"input": "8",
"output": "aaaabbcc"
},
{
"input": "9",
"output": "aaaabbb"
},
{
"input": "11",
"output": "aaaaabb"
},
{
"input": "1317",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbcccc"
},
{
"input": "1926",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbccc"
},
{
"input": "14514",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
},
{
"input": "25252",
"output": "niconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconiconicooooquququququququq"
},
{
"input": "99681",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "99998",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccc..."
},
{
"input": "82944",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb..."
},
{
"input": "831",
"output": "happybirthdayhappybirthdayhappybirthdayhappybirthdayhappybirthdayhappybirthdayhappybirthdayhappybirthdayhappybirthdayqqquqqquqqqu"
},
{
"input": "39393",
"output": "mikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumikumik..."
}
] | 46 | 0 | 0 | 5,935 |
|
677 | Vanya and Balloons | [
"binary search",
"brute force",
"dp",
"implementation"
] | null | null | Vanya plays a game of balloons on the field of size *n*<=Γ<=*n*, where each cell contains a balloon with one of the values 0, 1, 2 or 3. The goal is to destroy a cross, such that the product of all values of balloons in the cross is maximum possible. There are two types of crosses: normal and rotated. For example:
or
Formally, the cross is given by three integers *r*, *c* and *d*, such that *d*<=β€<=*r*,<=*c*<=β€<=*n*<=-<=*d*<=+<=1. The normal cross consists of balloons located in cells (*x*,<=*y*) (where *x* stay for the number of the row and *y* for the number of the column), such that |*x*<=-<=*r*|Β·|*y*<=-<=*c*|<==<=0 and |*x*<=-<=*r*|<=+<=|*y*<=-<=*c*|<=<<=*d*. Rotated cross consists of balloons located in cells (*x*,<=*y*), such that |*x*<=-<=*r*|<==<=|*y*<=-<=*c*| and |*x*<=-<=*r*|<=<<=*d*.
Vanya wants to know the maximum possible product of the values of balls forming one cross. As this value can be large, output it modulo 109<=+<=7. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=1000)Β β the number of rows and columns in the table with balloons.
The each of the following *n* lines contains *n* characters '0', '1', '2' or '3'Β β the description of the values in balloons. | Print the maximum possible product modulo 109<=+<=7. Note, that you are not asked to maximize the remainder modulo 109<=+<=7, but to find the maximum value and print it this modulo. | [
"4\n1233\n0213\n2020\n0303\n",
"5\n00300\n00300\n33333\n00300\n00300\n",
"5\n00003\n02030\n00300\n03020\n30000\n",
"5\n21312\n10003\n10002\n10003\n23231\n",
"5\n12131\n12111\n12112\n21311\n21212\n"
] | [
"108\n",
"19683\n",
"108\n",
"3\n",
"24\n"
] | In the first sample, the maximum product is achieved for a rotated cross with a center in the cell (3,β3) and radius 1: 2Β·2Β·3Β·3Β·3β=β108. | [] | 30 | 0 | 0 | 5,947 |
|
596 | Wilbur and Swimming Pool | [
"geometry",
"implementation"
] | null | null | After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the area of the rectangle must be positive. Wilbur had all four vertices of the planned pool written on a paper, until his friend came along and erased some of the vertices.
Now Wilbur is wondering, if the remaining *n* vertices of the initial rectangle give enough information to restore the area of the planned swimming pool. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=4)Β β the number of vertices that were not erased by Wilbur's friend.
Each of the following *n* lines contains two integers *x**i* and *y**i* (<=-<=1000<=β€<=*x**i*,<=*y**i*<=β€<=1000)Β βthe coordinates of the *i*-th vertex that remains. Vertices are given in an arbitrary order.
It's guaranteed that these points are distinct vertices of some rectangle, that has positive area and which sides are parallel to the coordinate axes. | Print the area of the initial rectangle if it could be uniquely determined by the points remaining. Otherwise, print <=-<=1. | [
"2\n0 0\n1 1\n",
"1\n1 1\n"
] | [
"1\n",
"-1\n"
] | In the first sample, two opposite corners of the initial rectangle are given, and that gives enough information to say that the rectangle is actually a unit square.
In the second sample there is only one vertex left and this is definitely not enough to uniquely define the area. | [
{
"input": "2\n0 0\n1 1",
"output": "1"
},
{
"input": "1\n1 1",
"output": "-1"
},
{
"input": "1\n-188 17",
"output": "-1"
},
{
"input": "1\n71 -740",
"output": "-1"
},
{
"input": "4\n-56 -858\n-56 -174\n778 -858\n778 -174",
"output": "570456"
},
{
"input": "2\n14 153\n566 -13",
"output": "91632"
},
{
"input": "2\n-559 894\n314 127",
"output": "669591"
},
{
"input": "1\n-227 -825",
"output": "-1"
},
{
"input": "2\n-187 583\n25 13",
"output": "120840"
},
{
"input": "2\n-337 451\n32 -395",
"output": "312174"
},
{
"input": "4\n-64 -509\n-64 960\n634 -509\n634 960",
"output": "1025362"
},
{
"input": "2\n-922 -505\n712 -683",
"output": "290852"
},
{
"input": "2\n-1000 -1000\n-1000 0",
"output": "-1"
},
{
"input": "2\n-1000 -1000\n0 -1000",
"output": "-1"
},
{
"input": "4\n-414 -891\n-414 896\n346 -891\n346 896",
"output": "1358120"
},
{
"input": "2\n56 31\n704 -121",
"output": "98496"
},
{
"input": "4\n-152 198\n-152 366\n458 198\n458 366",
"output": "102480"
},
{
"input": "3\n-890 778\n-418 296\n-890 296",
"output": "227504"
},
{
"input": "4\n852 -184\n852 724\n970 -184\n970 724",
"output": "107144"
},
{
"input": "1\n858 -279",
"output": "-1"
},
{
"input": "2\n-823 358\n446 358",
"output": "-1"
},
{
"input": "2\n-739 -724\n-739 443",
"output": "-1"
},
{
"input": "2\n686 664\n686 -590",
"output": "-1"
},
{
"input": "3\n-679 301\n240 -23\n-679 -23",
"output": "297756"
},
{
"input": "2\n-259 -978\n978 -978",
"output": "-1"
},
{
"input": "1\n627 -250",
"output": "-1"
},
{
"input": "3\n-281 598\n679 -990\n-281 -990",
"output": "1524480"
},
{
"input": "2\n-414 -431\n-377 -688",
"output": "9509"
},
{
"input": "3\n-406 566\n428 426\n-406 426",
"output": "116760"
},
{
"input": "3\n-686 695\n-547 308\n-686 308",
"output": "53793"
},
{
"input": "1\n-164 -730",
"output": "-1"
},
{
"input": "2\n980 -230\n980 592",
"output": "-1"
},
{
"input": "4\n-925 306\n-925 602\n398 306\n398 602",
"output": "391608"
},
{
"input": "3\n576 -659\n917 -739\n576 -739",
"output": "27280"
},
{
"input": "1\n720 -200",
"output": "-1"
},
{
"input": "4\n-796 -330\n-796 758\n171 -330\n171 758",
"output": "1052096"
},
{
"input": "2\n541 611\n-26 611",
"output": "-1"
},
{
"input": "3\n-487 838\n134 691\n-487 691",
"output": "91287"
},
{
"input": "2\n-862 -181\n-525 -181",
"output": "-1"
},
{
"input": "1\n-717 916",
"output": "-1"
},
{
"input": "1\n-841 -121",
"output": "-1"
},
{
"input": "4\n259 153\n259 999\n266 153\n266 999",
"output": "5922"
},
{
"input": "2\n295 710\n295 254",
"output": "-1"
},
{
"input": "4\n137 -184\n137 700\n712 -184\n712 700",
"output": "508300"
},
{
"input": "2\n157 994\n377 136",
"output": "188760"
},
{
"input": "1\n193 304",
"output": "-1"
},
{
"input": "4\n5 -952\n5 292\n553 -952\n553 292",
"output": "681712"
},
{
"input": "2\n-748 697\n671 575",
"output": "173118"
},
{
"input": "2\n-457 82\n260 -662",
"output": "533448"
},
{
"input": "2\n-761 907\n967 907",
"output": "-1"
},
{
"input": "3\n-639 51\n-321 -539\n-639 -539",
"output": "187620"
},
{
"input": "2\n-480 51\n89 -763",
"output": "463166"
},
{
"input": "4\n459 -440\n459 -94\n872 -440\n872 -94",
"output": "142898"
},
{
"input": "2\n380 -849\n68 -849",
"output": "-1"
},
{
"input": "2\n-257 715\n102 715",
"output": "-1"
},
{
"input": "2\n247 -457\n434 -921",
"output": "86768"
},
{
"input": "4\n-474 -894\n-474 -833\n-446 -894\n-446 -833",
"output": "1708"
},
{
"input": "3\n-318 831\n450 31\n-318 31",
"output": "614400"
},
{
"input": "3\n-282 584\n696 488\n-282 488",
"output": "93888"
},
{
"input": "3\n258 937\n395 856\n258 856",
"output": "11097"
},
{
"input": "1\n-271 -499",
"output": "-1"
},
{
"input": "2\n-612 208\n326 -559",
"output": "719446"
},
{
"input": "2\n115 730\n562 -546",
"output": "570372"
},
{
"input": "2\n-386 95\n-386 750",
"output": "-1"
},
{
"input": "3\n0 0\n0 1\n1 0",
"output": "1"
},
{
"input": "3\n0 4\n3 4\n3 1",
"output": "9"
},
{
"input": "3\n1 1\n1 2\n2 1",
"output": "1"
},
{
"input": "3\n1 4\n4 4\n4 1",
"output": "9"
},
{
"input": "3\n1 1\n2 1\n1 2",
"output": "1"
},
{
"input": "3\n0 0\n1 0\n1 1",
"output": "1"
},
{
"input": "3\n0 0\n0 5\n5 0",
"output": "25"
},
{
"input": "3\n0 0\n0 1\n1 1",
"output": "1"
},
{
"input": "4\n0 0\n1 0\n1 1\n0 1",
"output": "1"
},
{
"input": "3\n4 4\n1 4\n4 1",
"output": "9"
},
{
"input": "3\n0 0\n2 0\n2 1",
"output": "2"
},
{
"input": "3\n0 0\n2 0\n0 2",
"output": "4"
},
{
"input": "3\n0 0\n0 1\n5 0",
"output": "5"
},
{
"input": "3\n1 1\n1 3\n3 1",
"output": "4"
},
{
"input": "4\n0 0\n1 0\n0 1\n1 1",
"output": "1"
},
{
"input": "2\n1 0\n2 1",
"output": "1"
},
{
"input": "3\n0 0\n1 0\n0 1",
"output": "1"
},
{
"input": "3\n1 0\n0 0\n0 1",
"output": "1"
},
{
"input": "3\n0 0\n0 5\n5 5",
"output": "25"
},
{
"input": "3\n1 0\n5 0\n5 10",
"output": "40"
},
{
"input": "3\n0 0\n1 0\n1 2",
"output": "2"
},
{
"input": "4\n0 1\n0 0\n1 0\n1 1",
"output": "1"
},
{
"input": "3\n0 0\n2 0\n0 1",
"output": "2"
},
{
"input": "3\n-2 -1\n-1 -1\n-1 -2",
"output": "1"
},
{
"input": "2\n1 0\n0 1",
"output": "1"
},
{
"input": "4\n1 1\n3 3\n3 1\n1 3",
"output": "4"
},
{
"input": "3\n2 1\n1 2\n2 2",
"output": "1"
},
{
"input": "3\n0 0\n0 3\n3 0",
"output": "9"
},
{
"input": "2\n0 3\n3 3",
"output": "-1"
},
{
"input": "4\n2 0\n2 8\n5 8\n5 0",
"output": "24"
},
{
"input": "2\n0 999\n100 250",
"output": "74900"
},
{
"input": "3\n1 1\n1 5\n5 1",
"output": "16"
},
{
"input": "3\n0 1\n0 0\n1 1",
"output": "1"
},
{
"input": "3\n0 0\n10 0\n0 10",
"output": "100"
},
{
"input": "2\n0 0\n-1 -1",
"output": "1"
},
{
"input": "3\n1 5\n2 2\n2 5",
"output": "3"
},
{
"input": "3\n0 0\n0 1\n2 0",
"output": "2"
},
{
"input": "3\n0 1\n1 0\n0 0",
"output": "1"
},
{
"input": "3\n0 0\n0 -1\n1 -1",
"output": "1"
},
{
"input": "3\n0 1\n1 0\n1 1",
"output": "1"
},
{
"input": "3\n3 5\n3 2\n7 2",
"output": "12"
},
{
"input": "3\n1 2\n1 3\n2 2",
"output": "1"
},
{
"input": "3\n5 0\n0 0\n0 5",
"output": "25"
},
{
"input": "3\n1 0\n1 3\n5 0",
"output": "12"
},
{
"input": "3\n0 0\n0 2\n2 0",
"output": "4"
},
{
"input": "3\n1 1\n0 0\n1 0",
"output": "1"
},
{
"input": "3\n1 2\n1 3\n2 3",
"output": "1"
},
{
"input": "4\n0 0\n0 1\n1 1\n1 0",
"output": "1"
},
{
"input": "2\n-3 0\n3 3",
"output": "18"
},
{
"input": "3\n1 1\n0 1\n1 0",
"output": "1"
},
{
"input": "3\n0 0\n5 0\n5 5",
"output": "25"
},
{
"input": "3\n79 79\n79 158\n158 79",
"output": "6241"
},
{
"input": "3\n1 0\n1 -1\n0 0",
"output": "1"
},
{
"input": "3\n1 1\n1 2\n2 2",
"output": "1"
},
{
"input": "3\n0 1\n0 0\n1 0",
"output": "1"
},
{
"input": "3\n2 1\n2 4\n6 1",
"output": "12"
},
{
"input": "3\n5 0\n0 0\n5 5",
"output": "25"
}
] | 62 | 0 | 3 | 5,976 |
|
386 | Fly, freebies, fly! | [
"binary search",
"brute force",
"implementation"
] | null | null | Everyone loves a freebie. Especially students.
It is well-known that if in the night before exam a student opens window, opens the student's record-book and shouts loudly three times "Fly, freebie, fly!" β then flown freebie helps him to pass the upcoming exam.
In the night before the exam on mathematical analysis *n* students living in dormitory shouted treasured words. The *i*-th student made a sacrament at the time *t**i*, where *t**i* is the number of seconds elapsed since the beginning of the night.
It is known that the freebie is a capricious and willful lady. That night the freebie was near dormitory only for *T* seconds. Therefore, if for two students their sacrament times differ for more than *T*, then the freebie didn't visit at least one of them.
Since all students are optimists, they really want to know what is the maximal number of students visited by the freebie can be. | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=100), where *n* β the number of students shouted "Fly, freebie, fly!" The second line contains *n* positive integers *t**i* (1<=β€<=*t**i*<=β€<=1000).
The last line contains integer *T* (1<=β€<=*T*<=β€<=1000) β the time interval during which the freebie was near the dormitory. | Print a single integer β the largest number of people who will pass exam tomorrow because of the freebie visit. | [
"6\n4 1 7 8 3 8\n1\n"
] | [
"3\n"
] | none | [
{
"input": "6\n4 1 7 8 3 8\n1",
"output": "3"
},
{
"input": "4\n4 2 1 5\n2",
"output": "2"
},
{
"input": "10\n4 7 1 3 8 5 2 1 8 4\n3",
"output": "6"
},
{
"input": "8\n39 49 37 28 40 17 50 2\n10",
"output": "3"
},
{
"input": "2\n1 1\n1",
"output": "2"
},
{
"input": "2\n1 1\n2",
"output": "2"
},
{
"input": "2\n1 1\n1000",
"output": "2"
},
{
"input": "2\n1 2\n2",
"output": "2"
},
{
"input": "2\n450 826\n1000",
"output": "2"
},
{
"input": "3\n3 1 1\n1",
"output": "2"
},
{
"input": "3\n3 1 2\n2",
"output": "3"
},
{
"input": "3\n3 4 3\n1",
"output": "3"
},
{
"input": "3\n3 4 3\n1",
"output": "3"
},
{
"input": "100\n63 69 36 40 74 31 86 42 81 95 60 55 98 98 2 16 84 37 61 47 81 91 85 62 85 32 79 74 65 48 39 60 97 90 59 76 98 73 58 5 16 54 59 42 9 27 95 24 9 6 42 49 64 61 22 27 43 60 39 87 99 57 5 62 48 67 81 36 27 87 41 88 5 33 43 81 82 65 46 52 43 68 85 75 81 99 30 56 67 55 92 4 3 3 66 32 30 45 22 88\n5",
"output": "11"
},
{
"input": "100\n97 29 39 42 68 100 44 54 6 70 17 100 52 85 67 1 43 49 1 47 98 35 5 38 37 73 84 20 13 15 78 65 29 92 20 40 38 11 12 100 24 94 29 92 83 47 25 63 23 85 85 93 61 60 35 40 96 50 19 15 28 19 98 59 42 14 54 65 2 53 38 9 15 69 43 63 63 8 55 12 81 57 69 21 57 11 99 45 23 31 59 2 16 61 43 36 12 39 42 13\n50",
"output": "62"
},
{
"input": "100\n31 1 56 82 96 98 25 41 74 73 8 66 95 50 89 77 98 12 69 45 6 10 48 59 1 77 15 77 9 52 66 8 6 71 39 3 58 73 66 45 8 22 67 83 58 6 96 79 46 43 44 90 13 67 56 32 83 96 93 22 49 10 100 79 99 41 13 71 42 96 89 10 84 95 89 7 18 49 16 54 61 35 25 71 26 68 22 40 68 19 30 51 18 20 12 61 11 23 86 72\n1",
"output": "6"
},
{
"input": "100\n30 74 20 6 3 63 48 45 36 26 33 24 60 71 45 5 19 37 74 100 98 82 67 76 37 46 68 48 56 29 33 19 15 84 76 92 50 53 42 19 5 91 23 38 93 50 39 45 89 17 57 14 86 81 31 6 16 5 80 6 86 49 18 75 30 30 85 94 38 33 50 76 72 32 73 96 28 3 18 20 96 84 89 48 71 64 6 59 87 31 94 24 9 64 15 86 66 11 32 40\n90",
"output": "94"
},
{
"input": "100\n398 82 739 637 913 962 680 125 963 931 311 680 20 530 795 126 881 666 226 323 594 416 176 6 820 317 866 723 831 432 139 706 608 218 963 550 592 544 874 927 763 468 121 424 91 956 42 442 883 66 299 654 964 730 160 615 515 255 709 278 224 223 304 292 41 450 445 556 477 327 647 518 90 470 894 837 655 495 612 113 746 610 751 486 116 933 314 348 736 58 219 429 976 773 678 642 696 522 161 422\n1",
"output": "3"
},
{
"input": "100\n760 621 622 793 66 684 411 813 474 404 304 934 319 411 99 965 722 156 681 400 481 462 571 726 696 244 124 350 403 566 564 641 381 494 703 3 348 213 343 390 27 660 46 591 990 931 477 823 890 21 936 267 282 753 599 269 387 443 622 673 473 745 646 224 911 7 155 880 332 932 51 994 144 666 789 691 323 738 192 372 191 246 903 666 929 252 132 614 11 938 298 286 309 596 210 18 143 760 759 584\n10",
"output": "6"
},
{
"input": "100\n923 357 749 109 685 126 961 437 859 91 985 488 644 777 950 144 479 667 1 535 475 38 843 606 672 333 798 42 595 854 410 914 934 586 329 595 861 321 603 924 434 636 475 395 619 449 336 790 279 931 605 898 276 47 537 935 508 576 168 465 115 884 960 593 883 581 468 426 848 289 525 309 589 106 924 238 829 975 897 373 650 41 952 621 817 46 366 488 924 561 960 449 311 32 517 737 20 765 799 3\n100",
"output": "18"
},
{
"input": "100\n98 63 672 100 254 218 623 415 426 986 920 915 736 795 407 541 382 213 935 743 961 59 660 512 134 935 248 378 739 356 543 714 28 667 602 596 759 791 103 564 225 520 159 542 966 332 983 655 517 273 95 242 593 940 286 236 41 318 941 727 384 225 319 627 982 359 232 769 854 172 643 598 215 231 305 30 347 469 929 919 90 294 739 641 368 270 932 452 234 741 309 234 357 392 707 873 808 398 417 483\n1000",
"output": "100"
},
{
"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 1\n1",
"output": "100"
},
{
"input": "100\n2 1 1 1 2 2 2 2 2 2 1 1 1 1 2 2 1 1 1 2 2 1 1 1 1 2 1 2 1 2 1 2 1 2 2 2 1 1 2 1 2 2 1 1 2 2 2 2 2 1 1 2 1 1 1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 1 1 2 2 1 2 2 1 1 1 2 2 2 1 1 2 2 1 2 2 2 1 2 2 1 2 2\n1",
"output": "100"
},
{
"input": "100\n3 3 1 2 3 3 1 3 3 2 2 2 2 1 2 3 2 1 2 2 2 2 3 2 1 3 3 3 2 1 3 1 2 1 1 2 2 3 2 2 3 1 1 3 1 2 1 3 3 1 1 3 1 3 2 3 3 2 2 2 2 1 1 1 2 1 1 2 1 1 1 1 1 3 2 2 1 3 1 1 3 1 2 2 1 3 1 1 1 1 2 2 2 3 2 2 3 1 1 3\n1",
"output": "72"
},
{
"input": "100\n2 1 3 4 1 1 4 1 3 2 1 4 4 4 4 4 3 2 1 1 2 2 1 3 3 1 1 1 2 3 4 3 1 1 1 4 2 2 2 2 4 1 2 4 2 2 4 3 3 4 1 2 4 1 3 4 1 2 1 2 1 3 3 2 1 1 4 2 1 3 3 2 3 4 1 2 2 4 2 1 4 3 4 3 1 4 3 1 2 3 3 3 2 4 1 1 4 1 2 3\n1",
"output": "55"
},
{
"input": "100\n5 1 3 1 2 3 2 5 5 2 5 1 1 4 1 1 3 5 3 3 3 3 4 4 3 5 4 1 1 3 1 4 2 5 2 5 4 2 3 5 1 3 5 5 5 2 2 5 1 4 1 5 1 5 1 3 3 2 2 4 3 2 1 4 2 5 4 1 2 1 4 3 3 5 4 3 5 5 1 2 4 1 4 2 1 1 2 5 3 3 4 1 3 3 3 5 4 1 1 1\n1",
"output": "41"
},
{
"input": "100\n1 7 8 10 9 4 2 1 6 5 10 6 3 1 10 1 8 4 3 1 7 4 3 7 4 9 1 3 3 5 10 3 7 10 10 10 3 6 2 8 1 3 3 6 2 8 3 7 8 3 4 1 6 4 4 2 10 6 2 10 10 1 7 8 8 1 9 8 7 8 5 2 5 9 2 5 7 10 3 9 8 3 9 4 3 8 6 8 2 8 9 6 7 10 7 9 6 4 4 8\n1",
"output": "24"
},
{
"input": "1\n1\n1",
"output": "1"
},
{
"input": "1\n1\n1000",
"output": "1"
},
{
"input": "1\n849\n1",
"output": "1"
}
] | 31 | 0 | 0 | 5,988 |
|
257 | Playing Cubes | [
"games",
"greedy",
"implementation"
] | null | null | Petya and Vasya decided to play a little. They found *n* red cubes and *m* blue cubes. The game goes like that: the players take turns to choose a cube of some color (red or blue) and put it in a line from left to right (overall the line will have *n*<=+<=*m* cubes). Petya moves first. Petya's task is to get as many pairs of neighbouring cubes of the same color as possible. Vasya's task is to get as many pairs of neighbouring cubes of different colors as possible.
The number of Petya's points in the game is the number of pairs of neighboring cubes of the same color in the line, the number of Vasya's points in the game is the number of neighbouring cubes of the different color in the line. Your task is to calculate the score at the end of the game (Petya's and Vasya's points, correspondingly), if both boys are playing optimally well. To "play optimally well" first of all means to maximize the number of one's points, and second β to minimize the number of the opponent's points. | The only line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of red and blue cubes, correspondingly. | On a single line print two space-separated integers β the number of Petya's and Vasya's points correspondingly provided that both players play optimally well. | [
"3 1\n",
"2 4\n"
] | [
"2 1\n",
"3 2\n"
] | In the first test sample the optimal strategy for Petya is to put the blue cube in the line. After that there will be only red cubes left, so by the end of the game the line of cubes from left to right will look as [blue, red, red, red]. So, Petya gets 2 points and Vasya gets 1 point.
If Petya would choose the red cube during his first move, then, provided that both boys play optimally well, Petya would get 1 point and Vasya would get 2 points. | [
{
"input": "3 1",
"output": "2 1"
},
{
"input": "2 4",
"output": "3 2"
},
{
"input": "1 1",
"output": "0 1"
},
{
"input": "2 1",
"output": "1 1"
},
{
"input": "4 4",
"output": "3 4"
},
{
"input": "10 7",
"output": "9 7"
},
{
"input": "5 13",
"output": "12 5"
},
{
"input": "7 11",
"output": "10 7"
},
{
"input": "1 2",
"output": "1 1"
},
{
"input": "10 10",
"output": "9 10"
},
{
"input": "50 30",
"output": "49 30"
},
{
"input": "80 120",
"output": "119 80"
},
{
"input": "304 122",
"output": "303 122"
},
{
"input": "500 800",
"output": "799 500"
},
{
"input": "900 1000",
"output": "999 900"
},
{
"input": "1 1000",
"output": "999 1"
},
{
"input": "997 9",
"output": "996 9"
},
{
"input": "341 678",
"output": "677 341"
},
{
"input": "784 913",
"output": "912 784"
},
{
"input": "57 888",
"output": "887 57"
},
{
"input": "100000 100000",
"output": "99999 100000"
},
{
"input": "10000 100000",
"output": "99999 10000"
},
{
"input": "9999 99999",
"output": "99998 9999"
},
{
"input": "12 100000",
"output": "99999 12"
},
{
"input": "9999 31411",
"output": "31410 9999"
},
{
"input": "12930 98391",
"output": "98390 12930"
},
{
"input": "98813 893",
"output": "98812 893"
},
{
"input": "99801 38179",
"output": "99800 38179"
},
{
"input": "831 69318",
"output": "69317 831"
},
{
"input": "99999 99997",
"output": "99998 99997"
},
{
"input": "74 99",
"output": "98 74"
},
{
"input": "159 259",
"output": "258 159"
},
{
"input": "245 317",
"output": "316 245"
},
{
"input": "947 883",
"output": "946 883"
},
{
"input": "7131 3165",
"output": "7130 3165"
},
{
"input": "11536 12192",
"output": "12191 11536"
},
{
"input": "25938 40897",
"output": "40896 25938"
},
{
"input": "81314 31958",
"output": "81313 31958"
},
{
"input": "294 83621",
"output": "83620 294"
},
{
"input": "64896 18105",
"output": "64895 18105"
}
] | 156 | 0 | -1 | 5,994 |
|
60 | Where Are My Flakes? | [
"implementation",
"two pointers"
] | A. Where Are My Flakes? | 2 | 256 | One morning the Cereal Guy found out that all his cereal flakes were gone. He found a note instead of them. It turned out that his smart roommate hid the flakes in one of *n* boxes. The boxes stand in one row, they are numbered from 1 to *n* from the left to the right. The roommate left hints like "Hidden to the left of the *i*-th box" ("To the left of *i*"), "Hidden to the right of the *i*-th box" ("To the right of *i*"). Such hints mean that there are no flakes in the *i*-th box as well. The Cereal Guy wants to know the minimal number of boxes he necessarily needs to check to find the flakes considering all the hints. Or he wants to find out that the hints are contradictory and the roommate lied to him, that is, no box has the flakes. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=1000,<=0<=β€<=*m*<=β€<=1000) which represent the number of boxes and the number of hints correspondingly. Next *m* lines contain hints like "To the left of *i*" and "To the right of *i*", where *i* is integer (1<=β€<=*i*<=β€<=*n*). The hints may coincide. | The answer should contain exactly one integer β the number of boxes that should necessarily be checked or "-1" if the hints are contradictory. | [
"2 1\nTo the left of 2\n",
"3 2\nTo the right of 1\nTo the right of 2\n",
"3 1\nTo the left of 3\n",
"3 2\nTo the left of 2\nTo the right of 1\n"
] | [
"1\n",
"1\n",
"2\n",
"-1\n"
] | none | [
{
"input": "2 1\nTo the left of 2",
"output": "1"
},
{
"input": "3 2\nTo the right of 1\nTo the right of 2",
"output": "1"
},
{
"input": "3 1\nTo the left of 3",
"output": "2"
},
{
"input": "3 2\nTo the left of 2\nTo the right of 1",
"output": "-1"
},
{
"input": "3 0",
"output": "3"
},
{
"input": "2 2\nTo the left of 2\nTo the left of 2",
"output": "1"
},
{
"input": "5 0",
"output": "5"
},
{
"input": "6 2\nTo the right of 2\nTo the left of 4",
"output": "1"
},
{
"input": "8 6\nTo the right of 3\nTo the left of 4\nTo the left of 4\nTo the right of 1\nTo the right of 5\nTo the left of 6",
"output": "-1"
},
{
"input": "459 16\nTo the right of 9\nTo the right of 148\nTo the left of 406\nTo the left of 335\nTo the right of 240\nTo the left of 409\nTo the left of 365\nTo the left of 426\nTo the left of 416\nTo the right of 247\nTo the left of 284\nTo the right of 187\nTo the right of 134\nTo the right of 128\nTo the right of 98\nTo the left of 412",
"output": "36"
},
{
"input": "1000 1\nTo the right of 117",
"output": "883"
},
{
"input": "1000 0",
"output": "1000"
},
{
"input": "1 0",
"output": "1"
},
{
"input": "2 0",
"output": "2"
},
{
"input": "1 1\nTo the right of 1",
"output": "-1"
},
{
"input": "1 1\nTo the left of 1",
"output": "-1"
},
{
"input": "2 1\nTo the right of 2",
"output": "-1"
}
] | 374 | 1,433,600 | 3.90383 | 5,995 |
221 | Little Elephant and Numbers | [
"implementation"
] | null | null | The Little Elephant loves numbers.
He has a positive integer *x*. The Little Elephant wants to find the number of positive integers *d*, such that *d* is the divisor of *x*, and *x* and *d* have at least one common (the same) digit in their decimal representations.
Help the Little Elephant to find the described number. | A single line contains a single integer *x* (1<=β€<=*x*<=β€<=109). | In a single line print an integer β the answer to the problem. | [
"1\n",
"10\n"
] | [
"1\n",
"2\n"
] | none | [
{
"input": "1",
"output": "1"
},
{
"input": "10",
"output": "2"
},
{
"input": "47",
"output": "1"
},
{
"input": "100",
"output": "5"
},
{
"input": "128",
"output": "6"
},
{
"input": "2",
"output": "1"
},
{
"input": "17",
"output": "2"
},
{
"input": "1000000",
"output": "41"
},
{
"input": "1000000000",
"output": "91"
},
{
"input": "4584725",
"output": "5"
},
{
"input": "999999999",
"output": "6"
},
{
"input": "9",
"output": "1"
},
{
"input": "3",
"output": "1"
},
{
"input": "4",
"output": "1"
},
{
"input": "20",
"output": "3"
},
{
"input": "24",
"output": "4"
},
{
"input": "48",
"output": "4"
},
{
"input": "2458450",
"output": "11"
},
{
"input": "97648850",
"output": "44"
},
{
"input": "96488450",
"output": "21"
},
{
"input": "879541",
"output": "7"
},
{
"input": "111111111",
"output": "5"
},
{
"input": "222222222",
"output": "6"
},
{
"input": "777777777",
"output": "9"
},
{
"input": "211768200",
"output": "244"
},
{
"input": "536870912",
"output": "29"
},
{
"input": "654885000",
"output": "698"
},
{
"input": "223092870",
"output": "479"
},
{
"input": "901800900",
"output": "639"
},
{
"input": "101871000",
"output": "460"
},
{
"input": "49",
"output": "1"
},
{
"input": "999999993",
"output": "5"
},
{
"input": "999999666",
"output": "8"
},
{
"input": "999999997",
"output": "6"
},
{
"input": "960690025",
"output": "8"
},
{
"input": "16",
"output": "2"
},
{
"input": "999000011",
"output": "2"
},
{
"input": "999999937",
"output": "1"
},
{
"input": "999999998",
"output": "6"
}
] | 124 | 0 | 0 | 6,003 |
|
177 | Rectangular Game | [
"number theory"
] | null | null | The Smart Beaver from ABBYY decided to have a day off. But doing nothing the whole day turned out to be too boring, and he decided to play a game with pebbles. Initially, the Beaver has *n* pebbles. He arranges them in *a* equal rows, each row has *b* pebbles (*a*<=><=1). Note that the Beaver must use all the pebbles he has, i. e. *n*<==<=*a*Β·*b*.
Once the Smart Beaver has arranged the pebbles, he takes back any of the resulting rows (that is, *b* pebbles) and discards all other pebbles. Then he arranges all his pebbles again (possibly choosing other values of *a* and *b*) and takes back one row, and so on. The game continues until at some point the Beaver ends up with exactly one pebble.
The game process can be represented as a finite sequence of integers *c*1,<=...,<=*c**k*, where:
- *c*1<==<=*n* - *c**i*<=+<=1 is the number of pebbles that the Beaver ends up with after the *i*-th move, that is, the number of pebbles in a row after some arrangement of *c**i* pebbles (1<=β€<=*i*<=<<=*k*). Note that *c**i*<=><=*c**i*<=+<=1. - *c**k*<==<=1
The result of the game is the sum of numbers *c**i*. You are given *n*. Find the maximum possible result of the game. | The single line of the input contains a single integer *n* β the initial number of pebbles the Smart Beaver has.
The input limitations for getting 30 points are:
- 2<=β€<=*n*<=β€<=50
The input limitations for getting 100 points are:
- 2<=β€<=*n*<=β€<=109 | Print a single number β the maximum possible result of the game. | [
"10\n",
"8\n"
] | [
"16\n",
"15\n"
] | Consider the first example (*c*<sub class="lower-index">1</sub>β=β10). The possible options for the game development are:
- Arrange the pebbles in 10 rows, one pebble per row. Then *c*<sub class="lower-index">2</sub>β=β1, and the game ends after the first move with the result of 11. - Arrange the pebbles in 5 rows, two pebbles per row. Then *c*<sub class="lower-index">2</sub>β=β2, and the game continues. During the second move we have two pebbles which can be arranged in a unique way (remember that you are not allowed to put all the pebbles in the same row!) β 2 rows, one pebble per row. *c*<sub class="lower-index">3</sub>β=β1, and the game ends with the result of 13. - Finally, arrange the pebbles in two rows, five pebbles per row. The same logic leads us to *c*<sub class="lower-index">2</sub>β=β5,β*c*<sub class="lower-index">3</sub>β=β1, and the game ends with the result of 16 β the maximum possible result. | [
{
"input": "10",
"output": "16"
},
{
"input": "8",
"output": "15"
},
{
"input": "4",
"output": "7"
},
{
"input": "36",
"output": "67"
},
{
"input": "32",
"output": "63"
},
{
"input": "46",
"output": "70"
},
{
"input": "6",
"output": "10"
},
{
"input": "13",
"output": "14"
},
{
"input": "9",
"output": "13"
},
{
"input": "18",
"output": "31"
},
{
"input": "20",
"output": "36"
},
{
"input": "37",
"output": "38"
},
{
"input": "42",
"output": "71"
},
{
"input": "50",
"output": "81"
},
{
"input": "48",
"output": "94"
},
{
"input": "33",
"output": "45"
},
{
"input": "49",
"output": "57"
},
{
"input": "34",
"output": "52"
},
{
"input": "35",
"output": "43"
},
{
"input": "37",
"output": "38"
},
{
"input": "39",
"output": "53"
},
{
"input": "42",
"output": "71"
},
{
"input": "14",
"output": "22"
},
{
"input": "2",
"output": "3"
},
{
"input": "3",
"output": "4"
},
{
"input": "5",
"output": "6"
}
] | 186 | 20,172,800 | 3 | 6,005 |
|
79 | Colorful Field | [
"implementation",
"sortings"
] | B. Colorful Field | 2 | 256 | Fox Ciel saw a large field while she was on a bus. The field was a *n*<=Γ<=*m* rectangle divided into 1<=Γ<=1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes.
After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following procedure:
- Assume that the rows are numbered 1 to *n* from top to bottom and the columns are numbered 1 to *m* from left to right, and a cell in row *i* and column *j* is represented as (*i*,<=*j*). - First, each field is either cultivated or waste. Crop plants will be planted in the cultivated cells in the order of (1,<=1)<=β<=...<=β<=(1,<=*m*)<=β<=(2,<=1)<=β<=...<=β<=(2,<=*m*)<=β<=...<=β<=(*n*,<=1)<=β<=...<=β<=(*n*,<=*m*). Waste cells will be ignored. - Crop plants (either carrots or kiwis or grapes) will be planted in each cell one after another cyclically. Carrots will be planted in the first cell, then kiwis in the second one, grapes in the third one, carrots in the forth one, kiwis in the fifth one, and so on.
The following figure will show you the example of this procedure. Here, a white square represents a cultivated cell, and a black square represents a waste cell.
Now she is wondering how to determine the crop plants in some certain cells. | In the first line there are four positive integers *n*,<=*m*,<=*k*,<=*t* (1<=β€<=*n*<=β€<=4Β·104,<=1<=β€<=*m*<=β€<=4Β·104,<=1<=β€<=*k*<=β€<=103,<=1<=β€<=*t*<=β€<=103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in a certain cell.
Following each *k* lines contains two integers *a*,<=*b* (1<=β€<=*a*<=β€<=*n*,<=1<=β€<=*b*<=β€<=*m*), which denotes a cell (*a*,<=*b*) is waste. It is guaranteed that the same cell will not appear twice in this section.
Following each *t* lines contains two integers *i*,<=*j* (1<=β€<=*i*<=β€<=*n*,<=1<=β€<=*j*<=β€<=*m*), which is a query that asks you the kind of crop plants of a cell (*i*,<=*j*). | For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes. | [
"4 5 5 6\n4 3\n1 3\n3 3\n2 5\n3 2\n1 3\n1 4\n2 3\n2 4\n1 1\n1 1\n"
] | [
"Waste\nGrapes\nCarrots\nKiwis\nCarrots\nCarrots\n"
] | The sample corresponds to the figure in the statement. | [
{
"input": "4 5 5 6\n4 3\n1 3\n3 3\n2 5\n3 2\n1 3\n1 4\n2 3\n2 4\n1 1\n1 1",
"output": "Waste\nGrapes\nCarrots\nKiwis\nCarrots\nCarrots"
},
{
"input": "2 3 2 2\n1 1\n2 2\n2 1\n2 2",
"output": "Grapes\nWaste"
},
{
"input": "31 31 31 4\n4 9\n16 27\n11 29\n8 28\n11 2\n10 7\n22 6\n1 25\n14 8\n9 7\n9 1\n2 3\n5 2\n21 16\n20 19\n23 14\n27 6\n25 21\n14 1\n18 14\n7 2\n19 12\n30 27\n4 27\n24 12\n25 20\n26 22\n21 17\n11 6\n5 28\n28 24\n17 30\n2 5\n30 10\n4 21",
"output": "Kiwis\nCarrots\nGrapes\nGrapes"
},
{
"input": "39898 39898 3 1\n4567 8901\n12345 23456\n24680 35679\n29292 12121",
"output": "Grapes"
},
{
"input": "1 1 1 1\n1 1\n1 1",
"output": "Waste"
},
{
"input": "1 2 1 2\n1 1\n1 2\n1 1",
"output": "Carrots\nWaste"
},
{
"input": "1 6 3 12\n1 2\n1 4\n1 6\n1 1\n1 2\n1 6\n1 2\n1 5\n1 3\n1 4\n1 5\n1 5\n1 4\n1 6\n1 3",
"output": "Carrots\nWaste\nWaste\nWaste\nGrapes\nKiwis\nWaste\nGrapes\nGrapes\nWaste\nWaste\nKiwis"
},
{
"input": "1 10 1 3\n1 5\n1 5\n1 5\n1 5",
"output": "Waste\nWaste\nWaste"
},
{
"input": "5 5 1 3\n2 2\n2 2\n2 2\n2 2",
"output": "Waste\nWaste\nWaste"
},
{
"input": "3 3 3 5\n1 1\n2 2\n3 3\n1 1\n2 2\n2 2\n2 2\n3 3",
"output": "Waste\nWaste\nWaste\nWaste\nWaste"
},
{
"input": "1 10 2 4\n1 9\n1 5\n1 5\n1 6\n1 9\n1 10",
"output": "Waste\nKiwis\nWaste\nKiwis"
},
{
"input": "5 2 2 1\n3 2\n4 2\n5 2",
"output": "Kiwis"
}
] | 186 | 0 | 0 | 6,007 |
0 | none | [
"none"
] | null | null | It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has *n* cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to *n* from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an *n*<=-<=1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than *m* times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only *k* minutes left before morning, and on each of these *k* minutes he can perform no more than *m* operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning. | The first line contains integers *n*, *m* and *k* (1<=β€<=*n*<=β€<=104, 1<=β€<=*m*,<=*k*<=β€<=109). The next line contains *n* numbers. The *i*-th number is equal to the amount of diamonds in the *i*-th cell β it is an integer from 0 to 105. | Print a single number β the maximum number of diamonds Joe can steal. | [
"2 3 1\n2 3\n",
"3 2 2\n4 1 3\n"
] | [
"0",
"2"
] | In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket. | [
{
"input": "2 3 1\n2 3",
"output": "0"
},
{
"input": "3 2 2\n4 1 3",
"output": "2"
},
{
"input": "5 10 10\n7 0 7 0 7",
"output": "7"
},
{
"input": "6 10 4\n1 2 3 4 5 6",
"output": "0"
},
{
"input": "7 5 2\n1 2 3 4 5 6 7",
"output": "1"
},
{
"input": "16 100 100\n30 89 12 84 62 24 10 59 98 21 13 69 65 12 54 32",
"output": "0"
},
{
"input": "99 999 999\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 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 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 9 9 9 9 9 9 9 9 9 9 9",
"output": "9"
},
{
"input": "1 1 1\n0",
"output": "0"
},
{
"input": "1 64 25\n100000",
"output": "1600"
},
{
"input": "1 1000000000 1\n100",
"output": "100"
},
{
"input": "1 1 1000000000\n100",
"output": "100"
},
{
"input": "1 1000000000 1000000000\n100",
"output": "100"
},
{
"input": "5 2 9494412\n5484 254 1838 18184 9421",
"output": "0"
},
{
"input": "5 10 7\n98765 78654 25669 45126 98745",
"output": "21"
},
{
"input": "13 94348844 381845400\n515 688 5464 155 441 9217 114 21254 55 9449 1800 834 384",
"output": "55"
},
{
"input": "17 100 100\n47 75 22 18 42 53 95 98 94 50 63 55 46 80 9 20 99",
"output": "9"
},
{
"input": "47 20 1000000\n81982 19631 19739 13994 50426 14232 79125 95908 20227 79428 84065 86233 30742 82664 54626 10849 11879 67198 15667 75866 47242 90766 23115 20130 37293 8312 57308 52366 49768 28256 56085 39722 40397 14166 16743 28814 40538 50753 60900 99449 94318 54247 10563 5260 76407 42235 417",
"output": "0"
},
{
"input": "58 5858758 7544547\n6977 5621 6200 6790 7495 5511 6214 6771 6526 6557 5936 7020 6925 5462 7519 6166 5974 6839 6505 7113 5674 6729 6832 6735 5363 5817 6242 7465 7252 6427 7262 5885 6327 7046 6922 5607 7238 5471 7145 5822 5465 6369 6115 5694 6561 7330 7089 7397 7409 7093 7537 7279 7613 6764 7349 7095 6967 5984",
"output": "0"
},
{
"input": "79 5464 64574\n3800 2020 2259 503 4922 975 5869 6140 3808 2635 3420 992 4683 3748 5732 4787 6564 3302 6153 4955 2958 6107 2875 3449 1755 5029 5072 5622 2139 1892 4640 1199 3918 1061 4074 5098 4939 5496 2019 356 5849 4796 4446 4633 1386 1129 3351 639 2040 3769 4106 4048 3959 931 3457 1938 4587 6438 2938 132 2434 3727 3926 2135 1665 2871 2798 6359 989 6220 97 2116 2048 251 4264 3841 4428 5286 1914",
"output": "97"
},
{
"input": "95 97575868 5\n4612 1644 3613 5413 5649 2419 5416 3926 4610 4419 2796 5062 2112 1071 3790 4220 3955 2142 4638 2832 2702 2115 2045 4085 3599 2452 5495 4767 1368 2344 4625 4132 5755 5815 2581 6259 1330 4938 815 5430 1628 3108 4342 3692 2928 1941 3714 4498 4471 4842 1822 867 3395 2587 3372 6394 6423 3728 3720 6525 4296 2091 4400 994 1321 3454 5285 2989 1755 504 5019 2629 3834 3191 6254 844 5338 615 5608 4898 2497 4482 850 5308 2763 1943 6515 5459 5556 829 4646 5258 2019 5582 1226",
"output": "815"
},
{
"input": "77 678686 878687\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",
"output": "1"
},
{
"input": "2 7597 8545\n74807 22362",
"output": "0"
},
{
"input": "3 75579860 8570575\n10433 30371 14228",
"output": "10433"
}
] | 122 | 0 | 0 | 6,008 |
|
816 | Karen and Morning | [
"brute force",
"implementation"
] | null | null | Karen is getting ready for a new school day!
It is currently hh:mm, given in a 24-hour format. As you know, Karen loves palindromes, and she believes that it is good luck to wake up when the time is a palindrome.
What is the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome?
Remember that a palindrome is a string that reads the same forwards and backwards. For instance, 05:39 is not a palindrome, because 05:39 backwards is 93:50. On the other hand, 05:50 is a palindrome, because 05:50 backwards is 05:50. | The first and only line of input contains a single string in the format hh:mm (00<=β€<= hh <=β€<=23, 00<=β€<= mm <=β€<=59). | Output a single integer on a line by itself, the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome. | [
"05:39\n",
"13:31\n",
"23:59\n"
] | [
"11\n",
"0\n",
"1\n"
] | In the first test case, the minimum number of minutes Karen should sleep for is 11. She can wake up at 05:50, when the time is a palindrome.
In the second test case, Karen can wake up immediately, as the current time, 13:31, is already a palindrome.
In the third test case, the minimum number of minutes Karen should sleep for is 1 minute. She can wake up at 00:00, when the time is a palindrome. | [
{
"input": "05:39",
"output": "11"
},
{
"input": "13:31",
"output": "0"
},
{
"input": "23:59",
"output": "1"
},
{
"input": "13:32",
"output": "69"
},
{
"input": "14:40",
"output": "1"
},
{
"input": "14:00",
"output": "41"
},
{
"input": "05:50",
"output": "0"
},
{
"input": "12:22",
"output": "69"
},
{
"input": "12:34",
"output": "57"
},
{
"input": "05:30",
"output": "20"
},
{
"input": "14:14",
"output": "27"
},
{
"input": "01:10",
"output": "0"
},
{
"input": "02:20",
"output": "0"
},
{
"input": "03:30",
"output": "0"
},
{
"input": "04:40",
"output": "0"
},
{
"input": "10:01",
"output": "0"
},
{
"input": "11:11",
"output": "0"
},
{
"input": "12:21",
"output": "0"
},
{
"input": "14:41",
"output": "0"
},
{
"input": "15:51",
"output": "0"
},
{
"input": "20:02",
"output": "0"
},
{
"input": "21:12",
"output": "0"
},
{
"input": "22:22",
"output": "0"
},
{
"input": "23:32",
"output": "0"
},
{
"input": "01:11",
"output": "69"
},
{
"input": "02:21",
"output": "69"
},
{
"input": "03:31",
"output": "69"
},
{
"input": "04:41",
"output": "69"
},
{
"input": "05:51",
"output": "250"
},
{
"input": "10:02",
"output": "69"
},
{
"input": "11:12",
"output": "69"
},
{
"input": "14:42",
"output": "69"
},
{
"input": "15:52",
"output": "250"
},
{
"input": "20:03",
"output": "69"
},
{
"input": "21:13",
"output": "69"
},
{
"input": "22:23",
"output": "69"
},
{
"input": "23:33",
"output": "27"
},
{
"input": "00:00",
"output": "0"
},
{
"input": "00:01",
"output": "69"
},
{
"input": "22:21",
"output": "1"
},
{
"input": "20:01",
"output": "1"
},
{
"input": "11:10",
"output": "1"
},
{
"input": "06:59",
"output": "182"
},
{
"input": "02:00",
"output": "20"
},
{
"input": "02:19",
"output": "1"
},
{
"input": "17:31",
"output": "151"
},
{
"input": "19:00",
"output": "62"
},
{
"input": "13:37",
"output": "64"
},
{
"input": "07:59",
"output": "122"
},
{
"input": "04:20",
"output": "20"
},
{
"input": "07:10",
"output": "171"
},
{
"input": "06:00",
"output": "241"
},
{
"input": "06:01",
"output": "240"
},
{
"input": "08:15",
"output": "106"
},
{
"input": "06:59",
"output": "182"
},
{
"input": "01:00",
"output": "10"
},
{
"input": "07:00",
"output": "181"
},
{
"input": "06:10",
"output": "231"
},
{
"input": "18:52",
"output": "70"
},
{
"input": "09:59",
"output": "2"
},
{
"input": "19:00",
"output": "62"
},
{
"input": "15:52",
"output": "250"
},
{
"input": "06:50",
"output": "191"
},
{
"input": "00:00",
"output": "0"
},
{
"input": "19:20",
"output": "42"
},
{
"input": "05:51",
"output": "250"
},
{
"input": "06:16",
"output": "225"
},
{
"input": "10:10",
"output": "61"
},
{
"input": "17:11",
"output": "171"
},
{
"input": "18:00",
"output": "122"
},
{
"input": "00:01",
"output": "69"
},
{
"input": "05:04",
"output": "46"
},
{
"input": "16:00",
"output": "242"
},
{
"input": "23:31",
"output": "1"
},
{
"input": "17:25",
"output": "157"
},
{
"input": "23:32",
"output": "0"
},
{
"input": "23:58",
"output": "2"
},
{
"input": "02:21",
"output": "69"
},
{
"input": "01:11",
"output": "69"
},
{
"input": "23:46",
"output": "14"
},
{
"input": "00:09",
"output": "61"
},
{
"input": "09:20",
"output": "41"
},
{
"input": "05:59",
"output": "242"
},
{
"input": "18:59",
"output": "63"
},
{
"input": "02:02",
"output": "18"
},
{
"input": "00:30",
"output": "40"
},
{
"input": "05:54",
"output": "247"
},
{
"input": "19:59",
"output": "3"
},
{
"input": "16:59",
"output": "183"
},
{
"input": "17:51",
"output": "131"
},
{
"input": "09:30",
"output": "31"
},
{
"input": "10:01",
"output": "0"
},
{
"input": "16:55",
"output": "187"
},
{
"input": "20:02",
"output": "0"
},
{
"input": "16:12",
"output": "230"
},
{
"input": "20:00",
"output": "2"
},
{
"input": "01:01",
"output": "9"
},
{
"input": "23:01",
"output": "31"
},
{
"input": "06:05",
"output": "236"
},
{
"input": "19:19",
"output": "43"
},
{
"input": "17:00",
"output": "182"
},
{
"input": "07:50",
"output": "131"
},
{
"input": "21:20",
"output": "62"
},
{
"input": "23:23",
"output": "9"
},
{
"input": "19:30",
"output": "32"
},
{
"input": "00:59",
"output": "11"
},
{
"input": "22:59",
"output": "33"
},
{
"input": "18:18",
"output": "104"
},
{
"input": "17:46",
"output": "136"
},
{
"input": "07:30",
"output": "151"
},
{
"input": "17:16",
"output": "166"
},
{
"input": "06:06",
"output": "235"
},
{
"input": "23:30",
"output": "2"
},
{
"input": "05:57",
"output": "244"
},
{
"input": "19:46",
"output": "16"
},
{
"input": "11:10",
"output": "1"
},
{
"input": "17:07",
"output": "175"
},
{
"input": "18:53",
"output": "69"
},
{
"input": "07:06",
"output": "175"
},
{
"input": "17:50",
"output": "132"
},
{
"input": "09:15",
"output": "46"
},
{
"input": "09:55",
"output": "6"
},
{
"input": "20:05",
"output": "67"
},
{
"input": "22:55",
"output": "37"
},
{
"input": "10:00",
"output": "1"
},
{
"input": "07:55",
"output": "126"
},
{
"input": "07:40",
"output": "141"
},
{
"input": "19:08",
"output": "54"
},
{
"input": "23:24",
"output": "8"
},
{
"input": "16:15",
"output": "227"
},
{
"input": "07:20",
"output": "161"
},
{
"input": "23:10",
"output": "22"
},
{
"input": "06:51",
"output": "190"
},
{
"input": "16:30",
"output": "212"
},
{
"input": "17:54",
"output": "128"
},
{
"input": "07:07",
"output": "174"
},
{
"input": "08:01",
"output": "120"
},
{
"input": "09:58",
"output": "3"
},
{
"input": "18:03",
"output": "119"
},
{
"input": "07:59",
"output": "122"
},
{
"input": "05:55",
"output": "246"
},
{
"input": "07:39",
"output": "142"
},
{
"input": "05:52",
"output": "249"
},
{
"input": "12:02",
"output": "19"
},
{
"input": "08:59",
"output": "62"
},
{
"input": "09:21",
"output": "40"
},
{
"input": "16:04",
"output": "238"
},
{
"input": "04:00",
"output": "40"
},
{
"input": "18:21",
"output": "101"
},
{
"input": "09:13",
"output": "48"
},
{
"input": "17:20",
"output": "162"
},
{
"input": "06:15",
"output": "226"
},
{
"input": "19:21",
"output": "41"
},
{
"input": "16:02",
"output": "240"
},
{
"input": "15:59",
"output": "243"
},
{
"input": "06:07",
"output": "234"
},
{
"input": "08:08",
"output": "113"
},
{
"input": "09:09",
"output": "52"
},
{
"input": "02:19",
"output": "1"
},
{
"input": "21:59",
"output": "23"
}
] | 61 | 6,963,200 | 0 | 6,024 |
|
879 | Table Tennis | [
"data structures",
"implementation"
] | null | null | *n* people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins *k* games in a row. This player becomes the winner.
For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner. | The first line contains two integers: *n* and *k* (2<=β€<=*n*<=β€<=500, 2<=β€<=*k*<=β€<=1012)Β β the number of people and the number of wins.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all *a**i* are distinct. | Output a single integer β power of the winner. | [
"2 2\n1 2\n",
"4 2\n3 1 2 4\n",
"6 2\n6 5 3 1 2 4\n",
"2 10000000000\n2 1\n"
] | [
"2 ",
"3 ",
"6 ",
"2\n"
] | Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | [
{
"input": "2 2\n1 2",
"output": "2 "
},
{
"input": "4 2\n3 1 2 4",
"output": "3 "
},
{
"input": "6 2\n6 5 3 1 2 4",
"output": "6 "
},
{
"input": "2 10000000000\n2 1",
"output": "2"
},
{
"input": "4 4\n1 3 4 2",
"output": "4 "
},
{
"input": "2 2147483648\n2 1",
"output": "2"
},
{
"input": "3 2\n1 3 2",
"output": "3 "
},
{
"input": "3 3\n1 2 3",
"output": "3 "
},
{
"input": "5 2\n2 1 3 4 5",
"output": "5 "
},
{
"input": "10 2\n7 10 5 8 9 3 4 6 1 2",
"output": "10 "
},
{
"input": "100 2\n62 70 29 14 12 87 94 78 39 92 84 91 61 49 60 33 69 37 19 82 42 8 45 97 81 43 54 67 1 22 77 58 65 17 18 28 25 57 16 90 40 13 4 21 68 35 15 76 73 93 56 95 79 47 74 75 30 71 66 99 41 24 88 83 5 6 31 96 38 80 27 46 51 53 2 86 32 9 20 100 26 36 63 7 52 55 23 3 50 59 48 89 85 44 34 64 10 72 11 98",
"output": "70 "
},
{
"input": "4 10\n2 1 3 4",
"output": "4"
},
{
"input": "10 2\n1 2 3 4 5 6 7 8 9 10",
"output": "10 "
},
{
"input": "10 2\n10 9 8 7 6 5 4 3 2 1",
"output": "10 "
},
{
"input": "4 1000000000000\n3 4 1 2",
"output": "4"
},
{
"input": "100 10\n19 55 91 50 31 23 60 84 38 1 22 51 27 76 28 98 11 44 61 63 15 93 52 3 66 16 53 36 18 62 35 85 78 37 73 64 87 74 46 26 82 69 49 33 83 89 56 67 71 25 39 94 96 17 21 6 47 68 34 42 57 81 13 10 54 2 48 80 20 77 4 5 59 30 90 95 45 75 8 88 24 41 40 14 97 32 7 9 65 70 100 99 72 58 92 29 79 12 86 43",
"output": "91 "
},
{
"input": "100 50\n2 4 82 12 47 63 52 91 87 45 53 1 17 25 64 50 9 13 22 54 21 30 43 24 38 33 68 11 41 78 99 23 28 18 58 67 79 10 71 56 49 61 26 29 59 20 90 74 5 75 89 8 39 95 72 42 66 98 44 32 88 35 92 3 97 55 65 51 77 27 81 76 84 69 73 85 19 46 62 100 60 37 7 36 57 6 14 83 40 48 16 70 96 15 31 93 80 86 94 34",
"output": "100 "
},
{
"input": "2 1000000000000\n1 2",
"output": "2"
},
{
"input": "5 2\n1 4 3 5 2",
"output": "4 "
},
{
"input": "5 2\n1 3 2 4 5",
"output": "3 "
},
{
"input": "4 1000000000000\n3 1 2 4",
"output": "4"
},
{
"input": "4 2\n1 3 2 4",
"output": "3 "
},
{
"input": "10 3\n8 1 9 2 3 10 4 5 6 7",
"output": "9 "
},
{
"input": "5 2\n2 1 4 3 5",
"output": "4 "
},
{
"input": "3 4294967297\n2 1 3",
"output": "3"
},
{
"input": "4 4294967297\n3 2 1 4",
"output": "4"
},
{
"input": "5 4294967298\n3 2 1 4 5",
"output": "5"
},
{
"input": "10 4\n5 4 7 1 2 9 3 6 8 10",
"output": "9 "
},
{
"input": "11 21474836489\n10 1 2 3 4 5 6 7 8 9 11",
"output": "11"
}
] | 61 | 0 | 0 | 6,045 |
|
761 | Dasha and Password | [
"brute force",
"dp",
"implementation"
] | null | null | After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length *n* which satisfies the following requirements:
- There is at least one digit in the string, - There is at least one lowercase (small) letter of the Latin alphabet in the string, - There is at least one of three listed symbols in the string: '#', '*', '&'.
Considering that these are programming classes it is not easy to write the password.
For each character of the password we have a fixed string of length *m*, on each of these *n* strings there is a pointer on some character. The *i*-th character displayed on the screen is the pointed character in the *i*-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one).
During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index *m*, and when we move it to the right from the position *m* it moves to the position 1.
You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password. | The first line contains two integers *n*, *m* (3<=β€<=*n*<=β€<=50,<=1<=β€<=*m*<=β€<=50) β the length of the password and the length of strings which are assigned to password symbols.
Each of the next *n* lines contains the string which is assigned to the *i*-th symbol of the password string. Its length is *m*, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.
You have such input data that you can always get a valid password. | Print one integer β the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password. | [
"3 4\n1**2\na3*0\nc4**\n",
"5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&\n"
] | [
"1\n",
"3\n"
] | In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.
In the second test one of possible algorithms will be:
- to move the pointer of the second symbol once to the right. - to move the pointer of the third symbol twice to the right. | [
{
"input": "3 4\n1**2\na3*0\nc4**",
"output": "1"
},
{
"input": "5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&",
"output": "3"
},
{
"input": "5 2\n&l\n*0\n*9\n*#\n#o",
"output": "2"
},
{
"input": "25 16\nvza**ooxkmd#*ywa\ndip#*#&ef&z&&&pv\nwggob&&72#*&&nku\nrsb##*&jm&#u**te\nzif#lu#t&2w#jbqb\nwfo&#&***0xp#&hp\njbw##h*###nkmkdn\nqrn*&y#3cnf&d*rc\nend*zg&0f*&g*&ak\niayh&r#8om#o**yq\nwym&e&*v0j&#zono\ntzu*vj&i18iew&ht\nhpfnceb193&#&acf\ngesvq&l&*&m*l*ru\nfot#u&pq&0y&s*pg\nqdfgs&hk*wob&&bw\nbqd&&&lnv&&ax&ql\nell#&t&k*p#n*rlg\nclfou#ap#*vxulmt\nfhp*gax&s1&pinql\nyihmh*yy&2&#&prc\nrmv**#h*bxyf&&eq\nziu##ku#f#uh*fek\nhmg&&cvx0p*#odgw\nquu&csv*aph#dkiq",
"output": "10"
},
{
"input": "3 5\n*****\n1***a\n**a**",
"output": "2"
},
{
"input": "5 2\n&e\n#j\n&&\n*2\n94",
"output": "1"
},
{
"input": "5 2\ns*\nsq\n*v\nes\n*5",
"output": "1"
},
{
"input": "10 2\n0n\n5h\n7&\n1b\n5&\n4*\n9k\n0*\n7m\n62",
"output": "2"
},
{
"input": "10 2\n89\n7&\ns8\now\n2#\n5&\nu&\n89\n8#\n3u",
"output": "1"
},
{
"input": "10 2\n#y\njc\n#6\n#0\nt7\ns7\nd#\nn2\n#7\n&3",
"output": "1"
},
{
"input": "15 12\n502j2*su#*j4\n48vtw8#r5\n43wl0085#&64\n99pedbk#*ol2\n08w#h#&y1346\n259*874&b*76\n40l#5hc*qta4\n280#h#r*3k98\n20t8o&l1##55\n8048l#6&o*37\n01a3z0179#30\n65p28q#0*3j3\n51tx885#**56\n105&&f64n639\n40v3&l61yr65",
"output": "5"
},
{
"input": "15 12\ndcmzv&*zzflc\neftqm&**njyp\ntwlsi*jvuman\ngcxdlb#xwbul\nnpgvufdyqoaz\nxvvpk##&bpso\njlwcfb&kqlbu\nnpxxr#1augfd\nngnaph#erxpl\nlsfaoc*ulsbi\npffbe&6lrybj\nsuvpz#q&aahf\nizhoba**jjmc\nmkdtg#6*xtnp\nqqfpjo1gddqo",
"output": "11"
},
{
"input": "15 12\n#&*&s#&&9&&&\n*&##*4&le&*#\n#*##24qh3*#&\n&***2j&a2###\n#*&#n68*z###\n##**1#&w#**&\n*&*#*0#&#***\n#*#*2723&*##\n&#&&mg3iu##*\n*&&#zl4k#&*&\n##&*5g#01&&*\n*##&wg1#6&*#\n#&**pvr6*&&#\n&&#*mzd#5&*#\n###*e2684#**",
"output": "8"
},
{
"input": "20 13\n885**jh##mj0t\nky3h&h&clr#27\nq6n&v127i64xo\n3lz4du4zi5&z9\n0r7056qp8r*5a\nc8v94v#402l7n\nu968vxt9&2fkn\n2jl4m**o6412n\nh10v&vl*#4&h4\nj4864*##48*9d\n402i&3#x&o786\nzn8#w&*p#8&6l\n2e7&68p#&kc47\njf4e7fv&o0*3z\n0z67ocr7#5*79\nr8az68#&u&5a9\n65a#&9#*8o178\nqjevs&&muj893\n4c83i63j##m37\ng1g85c##f7y3f",
"output": "3"
},
{
"input": "20 13\nvpym*054*4hoi\nldg&1u*yu4inw\nvs#b7*s27iqgo\nfp&*s2g#1i&#k\nyp&v474*58*#w\nzwfxx***4hqdg\nqqv*3163r2*&l\naxdc4l7&5l#fj\nqq&h#1z*&5#*a\nyml&&&9#a2*pr\nmpn&&78rbthpb\nac#d50**b7t#o\ndk&z7q&z&&#&j\ngyh#&f#0q5#&x\ncxw*#hgm#9nqn\nqm#&*c*k&2&bz\nxc#&86o#d9g#w\nzjm&12&9x3#hp\nzy&s##47u1jyf\nub*&9ao5qy#ip",
"output": "6"
},
{
"input": "20 13\n*8002g&87&8&6\n&4n*51i4&0\n40*#iq3pnc&87\n#*&0*s458&475\n802*8&1z*g533\n7171&a&2&2*8*\n*&##&&&&&t**&\n3#&*7#80*m18#\n#4#cqt9*7\n6*#56*#*&762&\n9406&ge0&7&07\n9**&6lv*v*2&&\n9##&c&i&z13#*\n68#*4g*9&f4&1\n37##80#&f2*&2\n81##*xo#q#5&0\n5247#hqy&d9&2\n#*13*5477*9#*\n2*&#q*0*fb9#*\n&2&4v*2##&&32",
"output": "4"
},
{
"input": "25 16\n5v7dnmg1##qqa75*\n0187oa*&c&&ew9h*\nr70*&##*q#4i6*&#\n7*wk*&4v06col***\n28*0h94x**&21*f5\neh5vbt#8&8#8#3r&\np*01u&&90&08p*#*\nb9#e7&r8lc56b*##\nyb4&x#&4956iw&8*\n39&5#4d5#&3r8t5x\n7x13**kk#0n**&80\n4oux8yhz*pg84nnr\nb2yfb&b70xa&k56e\nqt5&q4&6#&*z5#3&\n5*#*086*51l&&44#\n84k5**0lij37j#&v\ns&j0m4j&2v3fv9h&\np&hu68704*&cufs#\n34ra*i1993*i*&55\nr#w#4#1#30*cudj*\n0m3p&e3t##y97&90\nk6my174e##5z1##4\n2*&v#0u&49f#*47#\nv5276hv1xn*wz8if\nk24*#&hu7e*##n8&",
"output": "1"
},
{
"input": "25 16\n&*#&#**sw&**&#&#\n&*#*d#j*3b&q***#\n###&yq*v3q*&##**\n#**&#jpt#*#*#\n***#y*cd&l*oe*##\n&&&***#k*e&*p&#*\n&###*&fkn*pni#**\n**&#ybz*&u*##&&#\n**##p&renhvlq#&#\n*#*&q&*#1&p#&&#&\n**&##&##2*ved&&*\n##*&tug&x*fx&*&*\n###**nt*u&&ux*&&\n&#&#**##1xca*#&&\n*#*&jw#rc#vow&&&\n&*#&exgq&&m*&#*&\n&&##l&&mbizc&**&\n##*&&#m*0&o#*##*\n&#*&fcqsy#&&#*#&\n**#**#*cdm#yf*&*\n*&##s#*v#*g#*&**\n*&##&*#mu##eh&#*\n####v#&i5*bnb&&&\n**##*hj&9#ro#&**\n#&&**&s*9x#f&&*#",
"output": "12"
},
{
"input": "50 1\n#\n4\n7\n#\n&\n*\n3\n&\nc\n*\n7\n*\n#\nw\n1\n&\n8\n7\n&\n&\ny\ng\n#\n5\n*\n4\nx\ny\np\n6\nf\ne\np\n&\n#\n#\ns\nt\na\nm\n&\n1\nv\n#\n&\n1\nq\n0\ny\n3",
"output": "0"
},
{
"input": "3 1\nr\n&\n6",
"output": "0"
},
{
"input": "3 1\n1\nz\n#",
"output": "0"
},
{
"input": "3 1\n6\n*\nt",
"output": "0"
},
{
"input": "3 1\ni\n3\n&",
"output": "0"
},
{
"input": "3 1\nj\n#\n0",
"output": "0"
},
{
"input": "3 1\n&\n7\no",
"output": "0"
},
{
"input": "3 1\n&\nr\n3",
"output": "0"
},
{
"input": "3 8\n*1a*****\n*******a\n*******1",
"output": "2"
},
{
"input": "3 15\naaaaaaa1aaaaaaa\naaaaaaa*aaaaaaa\naaaaaaa*aaaaaaa",
"output": "14"
}
] | 685 | 30,003,200 | 3 | 6,049 |
|
57 | Square Earth? | [
"dfs and similar",
"greedy",
"implementation"
] | A. Square Earth? | 2 | 256 | Meg the Rabbit decided to do something nice, specifically β to determine the shortest distance between two points on the surface of our planet. But Meg... what can you say, she wants everything simple. So, she already regards our planet as a two-dimensional circle. No, wait, it's even worse β as a square of side *n*. Thus, the task has been reduced to finding the shortest path between two dots on a square (the path should go through the square sides). To simplify the task let us consider the vertices of the square to lie at points whose coordinates are: (0,<=0), (*n*,<=0), (0,<=*n*) and (*n*,<=*n*). | The single line contains 5 space-separated integers: *n*,<=*x*1,<=*y*1,<=*x*2,<=*y*2 (1<=β€<=*n*<=β€<=1000,<=0<=β€<=*x*1,<=*y*1,<=*x*2,<=*y*2<=β€<=*n*) which correspondingly represent a side of the square, the coordinates of the first point and the coordinates of the second point. It is guaranteed that the points lie on the sides of the square. | You must print on a single line the shortest distance between the points. | [
"2 0 0 1 0\n",
"2 0 1 2 1\n",
"100 0 0 100 100\n"
] | [
"1\n",
"4\n",
"200\n"
] | none | [
{
"input": "2 0 0 1 0",
"output": "1"
},
{
"input": "2 0 1 2 1",
"output": "4"
},
{
"input": "100 0 0 100 100",
"output": "200"
},
{
"input": "4 0 3 1 4",
"output": "2"
},
{
"input": "10 8 10 10 0",
"output": "12"
},
{
"input": "26 21 0 26 14",
"output": "19"
},
{
"input": "15 0 1 11 0",
"output": "12"
},
{
"input": "26 26 7 26 12",
"output": "5"
},
{
"input": "6 6 0 2 6",
"output": "10"
},
{
"input": "5 1 5 2 5",
"output": "1"
},
{
"input": "99 12 0 35 99",
"output": "146"
},
{
"input": "44 44 31 28 0",
"output": "47"
},
{
"input": "42 42 36 5 0",
"output": "73"
},
{
"input": "87 87 66 0 5",
"output": "158"
},
{
"input": "85 0 32 0 31",
"output": "1"
},
{
"input": "30 20 30 3 0",
"output": "53"
},
{
"input": "5 4 0 5 1",
"output": "2"
},
{
"input": "40 24 40 4 0",
"output": "68"
},
{
"input": "11 0 2 11 4",
"output": "17"
},
{
"input": "82 0 11 35 0",
"output": "46"
},
{
"input": "32 19 32 0 1",
"output": "50"
},
{
"input": "54 12 0 0 44",
"output": "56"
},
{
"input": "75 42 75 28 0",
"output": "145"
},
{
"input": "48 31 48 0 4",
"output": "75"
},
{
"input": "69 4 69 69 59",
"output": "75"
},
{
"input": "561 0 295 233 0",
"output": "528"
},
{
"input": "341 158 0 0 190",
"output": "348"
},
{
"input": "887 887 461 39 887",
"output": "1274"
},
{
"input": "700 0 288 700 368",
"output": "1356"
},
{
"input": "512 70 512 512 99",
"output": "855"
},
{
"input": "826 188 826 592 0",
"output": "1606"
},
{
"input": "953 0 773 0 903",
"output": "130"
},
{
"input": "80 80 4 0 54",
"output": "138"
},
{
"input": "208 73 0 208 123",
"output": "258"
},
{
"input": "983 0 894 199 0",
"output": "1093"
},
{
"input": "686 615 686 470 686",
"output": "145"
},
{
"input": "869 869 833 0 578",
"output": "1196"
},
{
"input": "169 0 94 0 132",
"output": "38"
},
{
"input": "68 42 68 68 28",
"output": "66"
},
{
"input": "967 967 607 279 0",
"output": "1295"
},
{
"input": "489 489 139 455 489",
"output": "384"
},
{
"input": "964 205 964 604 964",
"output": "399"
},
{
"input": "86 0 34 86 21",
"output": "141"
},
{
"input": "209 166 209 131 0",
"output": "330"
},
{
"input": "684 684 113 314 684",
"output": "941"
},
{
"input": "16 0 6 0 8",
"output": "2"
},
{
"input": "862 154 862 297 862",
"output": "143"
},
{
"input": "418 222 0 254 418",
"output": "778"
},
{
"input": "571 504 571 143 571",
"output": "361"
},
{
"input": "371 371 210 81 371",
"output": "451"
},
{
"input": "1000 0 0 1000 1000",
"output": "2000"
},
{
"input": "1000 564 0 436 1000",
"output": "2000"
},
{
"input": "1000 0 573 12 1000",
"output": "439"
},
{
"input": "1000 984 0 1000 999",
"output": "1015"
},
{
"input": "100 10 0 10 0",
"output": "0"
}
] | 218 | 3,379,200 | -1 | 6,062 |
427 | Checkposts | [
"dfs and similar",
"graphs",
"two pointers"
] | null | null | Your city has *n* junctions. There are *m* one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.
To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction *i* can protect junction *j* if either *i*<==<=*j* or the police patrol car can go to *j* from *i* and then come back to *i*.
Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.
You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other. | In the first line, you will be given an integer *n*, number of junctions (1<=β€<=*n*<=β€<=105). In the next line, *n* space-separated integers will be given. The *i**th* integer is the cost of building checkpost at the *i**th* junction (costs will be non-negative and will not exceed 109).
The next line will contain an integer *m*Β (0<=β€<=*m*<=β€<=3Β·105). And each of the next *m* lines contains two integers *u**i* and *v**i*Β (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*;Β *u*<=β <=*v*). A pair *u**i*,<=*v**i* means, that there is a one-way road which goes from *u**i* to *v**i*. There will not be more than one road between two nodes in the same direction. | Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109<=+<=7). | [
"3\n1 2 3\n3\n1 2\n2 3\n3 2\n",
"5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1\n",
"10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9\n",
"2\n7 91\n2\n1 2\n2 1\n"
] | [
"3 1\n",
"8 2\n",
"15 6\n",
"7 1\n"
] | none | [
{
"input": "3\n1 2 3\n3\n1 2\n2 3\n3 2",
"output": "3 1"
},
{
"input": "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1",
"output": "8 2"
},
{
"input": "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9",
"output": "15 6"
},
{
"input": "2\n7 91\n2\n1 2\n2 1",
"output": "7 1"
},
{
"input": "10\n1 1 1 1 1 1 1 2 2 2\n15\n2 3\n2 4\n4 1\n4 7\n4 10\n4 5\n4 8\n2 6\n3 2\n1 2\n7 2\n10 2\n5 2\n8 2\n6 2",
"output": "3 7"
},
{
"input": "100\n174 174 49 152 10 185 101 157 28 177 105 83 155 178 119 123 197 197 56 174 39 53 85 94 72 166 71 43 177 123 186 11 64 82 145 81 11 29 100 10 106 45 138 79 131 90 80 104 149 116 129 30 96 192 11 182 92 157 95 136 152 38 68 34 42 95 164 134 117 75 186 185 28 146 86 24 147 12 166 169 116 44 46 99 55 179 85 187 150 161 49 46 146 79 100 130 59 135 81 176\n0",
"output": "10585 1"
},
{
"input": "1\n1000000000\n0",
"output": "1000000000 1"
},
{
"input": "1\n0\n0",
"output": "0 1"
}
] | 810 | 22,630,400 | -1 | 6,098 |
|
261 | Maxim and Restaurant | [
"dp",
"math",
"probabilities"
] | null | null | Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is *p* meters.
Maxim has got a dinner party tonight, *n* guests will come to him. Let's index the guests of Maxim's restaurant from 1 to *n*. Maxim knows the sizes of all guests that are going to come to him. The *i*-th guest's size (*a**i*) represents the number of meters the guest is going to take up if he sits at the restaurant table.
Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than *p*. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table.
Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible *n*! orders of guests in the queue. Help Maxim, calculate this number. | The first line contains integer *n* (1<=β€<=*n*<=β€<=50) β the number of guests in the restaurant. The next line contains integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=50) β the guests' sizes in meters. The third line contains integer *p* (1<=β€<=*p*<=β€<=50) β the table's length in meters.
The numbers in the lines are separated by single spaces. | In a single line print a real number β the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10<=-<=4. | [
"3\n1 2 3\n3\n"
] | [
"1.3333333333\n"
] | In the first sample the people will come in the following orders:
- (1,β2,β3) β there will be two people in the restaurant; - (1,β3,β2) β there will be one person in the restaurant; - (2,β1,β3) β there will be two people in the restaurant; - (2,β3,β1) β there will be one person in the restaurant; - (3,β1,β2) β there will be one person in the restaurant; - (3,β2,β1) β there will be one person in the restaurant.
In total we get (2β+β1β+β2β+β1β+β1β+β1)β/β6 = 8β/β6 = 1.(3). | [
{
"input": "3\n1 2 3\n3",
"output": "1.3333333333"
},
{
"input": "9\n2 2 2 2 2 2 2 1 2\n9",
"output": "4.5555555556"
},
{
"input": "7\n2 1 1 2 1 1 2\n2",
"output": "1.2857142857"
},
{
"input": "8\n3 1 5 6 1 5 4 4\n7",
"output": "1.6250000000"
},
{
"input": "2\n1 3\n3",
"output": "1.0000000000"
},
{
"input": "2\n1 2\n2",
"output": "1.0000000000"
},
{
"input": "5\n1 2 3 1 2\n3",
"output": "1.5000000000"
},
{
"input": "9\n1 2 2 7 4 4 1 4 7\n7",
"output": "1.7341269841"
},
{
"input": "6\n1 1 1 1 1 1\n1",
"output": "1.0000000000"
},
{
"input": "10\n35 5 7 41 17 27 32 9 45 40\n30",
"output": "0.6500000000"
},
{
"input": "27\n38 39 19 33 30 42 34 16 40 9 5 31 28 7 24 37 22 46 25 23 21 30 28 24 48 13 37\n24",
"output": "0.4333903134"
},
{
"input": "41\n37 6 18 6 25 32 3 1 1 42 25 17 31 8 42 8 38 8 38 4 34 46 10 10 9 22 39 23 47 7 31 14 19 1 42 13 6 11 10 25 38\n12",
"output": "0.5001534565"
},
{
"input": "49\n46 42 3 1 42 37 25 21 47 22 49 50 19 35 32 35 4 50 19 39 1 39 28 18 29 44 49 34 8 22 11 18 14 15 10 17 36 2 1 50 20 7 49 4 25 9 45 10 40\n34",
"output": "0.9154259295"
},
{
"input": "3\n36 44 44\n46",
"output": "1.0000000000"
},
{
"input": "24\n15 4 49 1 9 19 31 47 49 32 40 49 10 8 23 23 39 43 39 30 41 8 9 42\n38",
"output": "0.8604837192"
},
{
"input": "16\n3 5 3 1 4 2 3 2 1 4 5 3 5 2 2 4\n39",
"output": "12.3395604396"
},
{
"input": "23\n2 2 1 1 2 2 1 1 1 2 2 2 1 2 2 2 2 1 2 1 2 1 1\n2",
"output": "1.1778656126"
},
{
"input": "18\n2 1 2 2 1 2 1 1 2 2 1 2 2 1 2 1 1 2\n8",
"output": "4.9849398967"
},
{
"input": "40\n5 8 2 14 14 19 14 2 12 10 17 15 2 9 11 24 7 19 14 21 8 3 24 18 20 10 14 1 9 9 18 18 13 2 23 7 23 2 17 13\n24",
"output": "1.6715713966"
},
{
"input": "23\n16 21 14 27 15 30 13 10 4 15 25 21 6 10 17 4 5 3 9 9 8 6 19\n30",
"output": "1.9401705846"
},
{
"input": "42\n3 2 3 1 1 3 1 3 2 3 3 2 1 3 2 3 3 2 3 3 1 3 3 2 3 2 3 1 2 2 2 3 3 1 2 1 1 3 1 3 3 2\n3",
"output": "1.2020905923"
},
{
"input": "23\n2 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 2 1 1 1\n37",
"output": "23.0000000000"
},
{
"input": "3\n2 2 1\n22",
"output": "3.0000000000"
},
{
"input": "19\n8 11 13 11 7 11 3 11 6 7 3 14 4 10 10 8 2 1 8\n15",
"output": "1.6128310974"
},
{
"input": "28\n3 5 4 24 21 3 13 24 22 13 12 21 1 15 11 3 17 6 2 12 22 12 23 4 21 16 25 14\n25",
"output": "1.6461894466"
},
{
"input": "14\n8 13 28 2 17 31 2 11 39 12 24 3 8 10\n41",
"output": "2.4931734932"
},
{
"input": "8\n9 14 13 2 1 11 4 19\n25",
"output": "2.3500000000"
},
{
"input": "35\n5 1 2 3 1 4 1 2 2 2 3 2 3 3 2 5 2 2 3 3 3 3 2 1 2 4 5 5 1 5 3 2 1 4 3\n6",
"output": "1.9851721334"
},
{
"input": "35\n2 2 1 2 2 2 2 2 2 1 2 1 2 1 1 2 2 2 2 2 2 1 1 1 2 2 1 1 2 2 2 1 2 1 1\n35",
"output": "21.2873098934"
},
{
"input": "44\n24 19 6 4 23 10 11 16 21 15 18 17 13 9 25 3 1 11 24 26 12 12 21 17 19 2 6 24 21 18 7 2 12 2 4 25 17 26 22 10 22 11 13 27\n27",
"output": "1.5513891043"
},
{
"input": "36\n5 2 4 5 1 4 3 3 5 2 2 3 3 2 5 1 4 5 2 3 1 4 4 3 5 2 3 5 1 4 3 5 1 2 4 1\n10",
"output": "2.9649127243"
},
{
"input": "38\n2 4 2 4 1 2 5 1 5 3 5 4 2 5 4 3 1 1 1 5 4 3 4 3 5 4 2 5 4 1 1 3 2 4 5 3 5 1\n48",
"output": "15.0079078318"
},
{
"input": "40\n1 26 39 14 16 17 19 28 38 18 23 41 19 22 4 24 18 36 15 21 31 29 34 13 19 19 38 45 4 10 2 14 3 24 21 27 4 30 9 17\n45",
"output": "1.8507376624"
},
{
"input": "41\n31 21 49 18 37 34 36 27 36 39 4 30 25 49 24 10 8 17 45 6 19 27 12 26 6 2 50 47 35 16 15 43 26 14 43 47 49 23 27 7 24\n50",
"output": "1.5535424434"
},
{
"input": "30\n2 3 1 4 1 2 2 2 5 5 2 3 2 4 3 1 1 2 1 2 1 2 3 2 1 1 3 5 4 4\n5",
"output": "1.8614767098"
},
{
"input": "50\n2 1 2 1 2 1 1 1 2 2 2 1 1 1 1 1 1 2 2 1 2 1 2 2 1 2 2 1 1 2 1 1 1 2 2 2 1 2 1 2 2 2 2 2 1 1 2 2 1 2\n3",
"output": "1.8379591837"
},
{
"input": "50\n15 28 34 29 17 21 20 34 37 17 10 20 37 10 18 25 31 25 16 1 37 27 39 3 5 18 2 32 10 35 20 17 29 20 3 29 3 25 9 32 37 5 25 23 25 33 35 8 31 29\n39",
"output": "1.4997987526"
},
{
"input": "50\n1 5 2 4 3 4 1 4 1 2 5 1 4 5 4 2 1 2 5 3 4 5 5 2 1 2 2 2 2 2 3 2 5 1 2 2 3 2 5 5 1 3 4 5 2 1 3 4 2 2\n29",
"output": "9.8873093486"
},
{
"input": "50\n3 2 3 2 1 5 5 5 2 1 4 2 3 5 1 4 4 2 3 2 5 5 4 3 5 1 3 5 5 4 4 4 2 5 4 2 2 3 4 4 3 2 3 3 1 3 4 3 3 4\n19",
"output": "5.5762635183"
},
{
"input": "50\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\n25",
"output": "25.0000000000"
},
{
"input": "10\n42 18 35 1 20 25 29 9 50 36\n50",
"output": "1.5269841270"
},
{
"input": "7\n42 35 1 20 29 50 36\n50",
"output": "1.3142857143"
},
{
"input": "50\n1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7\n50",
"output": "12.0011471293"
},
{
"input": "1\n1\n1",
"output": "1.0000000000"
},
{
"input": "1\n2\n1",
"output": "0.0000000000"
},
{
"input": "5\n2 3 2 3 6\n30",
"output": "5.0000000000"
},
{
"input": "3\n1 2 3\n7",
"output": "3.0000000000"
},
{
"input": "3\n1 1 1\n50",
"output": "3.0000000000"
},
{
"input": "4\n1 2 3 4\n11",
"output": "4.0000000000"
},
{
"input": "50\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\n50",
"output": "50.0000000000"
},
{
"input": "50\n1 2 3 4 4 4 4 4 4 4 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\n50",
"output": "2.3167627104"
},
{
"input": "20\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n20",
"output": "20.0000000000"
},
{
"input": "2\n1 2\n3",
"output": "2.0000000000"
},
{
"input": "3\n1 2 3\n10",
"output": "3.0000000000"
},
{
"input": "5\n1 2 3 4 5\n20",
"output": "5.0000000000"
}
] | 248 | 716,800 | -1 | 6,117 |
|
981 | Bookshelves | [
"bitmasks",
"dp",
"greedy"
] | null | null | Mr Keks is a typical white-collar in Byteland.
He has a bookshelf in his office with some books on it, each book has an integer positive price.
Mr Keks defines the value of a shelf as the sum of books prices on it.
Miraculously, Mr Keks was promoted and now he is moving into a new office.
He learned that in the new office he will have not a single bookshelf, but exactly $k$ bookshelves. He decided that the beauty of the $k$ shelves is the [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND) of the values of all the shelves.
He also decided that he won't spend time on reordering the books, so he will place several first books on the first shelf, several next books on the next shelf and so on. Of course, he will place at least one book on each shelf. This way he will put all his books on $k$ shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty. | The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 50$)Β β the number of books and the number of shelves in the new office.
The second line contains $n$ integers $a_1, a_2, \ldots a_n$, ($0 < a_i < 2^{50}$)Β β the prices of the books in the order they stand on the old shelf. | Print the maximum possible beauty of $k$ shelves in the new office. | [
"10 4\n9 14 28 1 7 13 15 29 2 31\n",
"7 3\n3 14 15 92 65 35 89\n"
] | [
"24\n",
"64\n"
] | In the first example you can split the books as follows:
$$(9 + 14 + 28 + 1 + 7) \& (13 + 15) \& (29 + 2) \& (31) = 24.$$
In the second example you can split the books as follows:
$$(3 + 14 + 15 + 92) \& (65) \& (35 + 89) = 64.$$ | [
{
"input": "10 4\n9 14 28 1 7 13 15 29 2 31",
"output": "24"
},
{
"input": "7 3\n3 14 15 92 65 35 89",
"output": "64"
},
{
"input": "40 5\n6 18 24 5 14 16 31 9 15 5 25 2 18 12 19 27 10 23 23 18 22 14 1 14 6 14 17 28 11 21 8 23 10 30 21 5 17 11 26 16",
"output": "80"
},
{
"input": "20 15\n927353279298143 655102800384382 40376603048780 1008958973042960 1123049780860278 853122601026128 154596679092462 200013924385343 591199113039915 140875624438732 924096460433635 609326666846280 639191601375336 868486002971126 338452290857190 947205016908287 1091731324024232 315465850740682 804685495436596 1102057294815123",
"output": "16777216"
},
{
"input": "10 4\n318346909478488 165408439052762 201407789817026 481299976321209 960738945073700 249445428976993 1096588610084096 605348669136305 817617728356976 256799633127974",
"output": "563104572248080"
},
{
"input": "50 15\n10 30 15 11 9 5 7 16 4 3 29 28 14 20 9 10 21 16 1 7 30 19 16 25 12 29 12 5 3 23 27 16 11 25 21 18 22 12 23 16 24 27 19 7 11 12 30 1 24 11",
"output": "32"
},
{
"input": "20 8\n29 25 19 16 26 18 25 9 5 2 17 16 19 16 3 16 4 17 7 20",
"output": "16"
},
{
"input": "30 4\n893642632982367 772277951746295 510759449350295 826812150840579 1060893142351815 525992742241552 154832018679993 645715002371268 27232975419720 475004229372388 200177708355593 810661468479466 49618609534806 301608930846726 97042172725806 441637617418914 594900693592862 626510702147446 653604971179679 51832188158797 771139366286148 604472775724297 543106972286801 854107321252442 958411818162963 419377189839607 595529582510881 663396887427244 543023709380824 1078875721144944",
"output": "3940649673949202"
},
{
"input": "40 5\n22 16 24 23 3 18 20 12 25 26 29 4 10 31 31 30 2 7 11 16 24 8 2 24 19 17 5 17 20 7 15 26 1 16 26 21 27 4 19 25",
"output": "106"
},
{
"input": "20 9\n162679258761381 497029570950369 441562370676165 658883349839306 803660720071652 397337645985983 1098171846564844 578539182000384 612361670323974 773464823371908 133451479483291 1053028311550002 812258927376098 1012522114357354 692621870999109 73882870347249 783703450776904 897456103589081 217535031946368 318888756116976",
"output": "571746046443520"
},
{
"input": "50 12\n22 12 31 3 3 12 19 19 21 15 24 25 31 18 9 3 8 5 3 24 6 26 30 25 14 25 9 25 3 29 9 6 11 3 12 12 15 6 1 28 28 28 26 9 15 12 17 2 18 18",
"output": "36"
},
{
"input": "16 7\n1103813397013349 727264530801741 378049079598082 630901233315595 518569339136212 532452143552615 428093804691193 371529237344587 940308912730366 704551472087683 631663816743474 29306660032804 583388823624504 1109142272484691 257363549141980 1089402363164001",
"output": "572020924350465"
},
{
"input": "30 2\n26 8 26 25 27 18 6 13 10 22 25 19 7 14 20 4 10 22 9 10 30 30 11 7 27 8 23 13 17 21",
"output": "125"
},
{
"input": "10 3\n30 15 26 16 21 13 25 5 27 11",
"output": "33"
},
{
"input": "10 6\n977831009728918 953593140925615 784863715891337 774777129376154 384491823368699 788094311512692 223966626677969 1066171741163060 119044778274639 876388748099519",
"output": "562949953683460"
},
{
"input": "20 9\n2 3 15 8 17 7 23 12 30 23 4 23 16 21 11 6 7 16 3 3",
"output": "16"
},
{
"input": "20 8\n483137921056907 768218083275243 207834138817392 354531452905166 963403044757413 833232873786483 582872528360258 514660235281883 1011861985723061 459485641216220 942598735621775 873490204734628 649359794076787 543622431653711 10411517577635 91729283882125 291509560140622 287260200844128 879410314598283 206118644453476",
"output": "562951027164160"
},
{
"input": "30 9\n21 6 19 18 27 3 14 18 1 5 7 9 11 13 9 26 8 12 14 31 21 6 9 3 3 10 2 24 22 24",
"output": "36"
},
{
"input": "30 22\n73306162994949 868677001959133 1106639997936963 879122920975808 161227935543926 760034092541884 1120594333824270 1054807665792407 476276883119188 271363692993319 440885048025850 7074457876332 636209225974410 221086579024023 1009685779703594 647498280909980 816934931718733 452863796888983 1061332951888385 942660914590384 365185997676754 112920162715865 178172288962138 810584030647354 923637556325003 1102808429103893 1118229467600088 1074261154123678 839042706865637 352292698714231",
"output": "2097152"
},
{
"input": "23 10\n935459645688325 899894863244863 776172843769766 986122540485649 248016433194357 475375806620409 20513751182830 421086691387167 247543829168446 155673663632778 415875856906302 128486751653325 849753391760463 1083573552851049 244133940871958 934119204927643 1085966359617308 577156229051957 1081049229814148 935408354841926 1093470105244215 288440615891778 467185550898222",
"output": "562950087639040"
},
{
"input": "40 5\n2 20 10 21 4 30 27 12 17 20 5 21 11 12 28 12 24 20 20 31 19 2 1 27 31 1 14 8 1 21 10 2 29 22 29 9 12 29 21 7",
"output": "80"
},
{
"input": "10 4\n22 21 18 7 16 14 12 11 18 5",
"output": "16"
},
{
"input": "50 24\n19 14 27 12 29 18 26 24 16 23 23 22 3 28 18 11 27 14 24 19 25 23 14 7 25 31 20 7 23 10 21 9 30 3 25 17 11 16 27 28 3 25 8 19 17 27 21 19 26 31",
"output": "16"
},
{
"input": "10 4\n23 12 4 5 7 10 22 18 1 6",
"output": "4"
},
{
"input": "4 2\n158042378809438 929800196167200 663932725437382 1056705514263205",
"output": "594152991508074"
},
{
"input": "20 8\n8 29 23 29 3 24 21 4 2 8 12 17 22 8 14 15 21 18 2 22",
"output": "16"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "50 50\n1 3 7 15 31 63 127 255 511 1023 2047 4095 8191 16383 32767 65535 131071 262143 524287 1048575 2097151 4194303 8388607 16777215 33554431 67108863 134217727 268435455 536870911 1073741823 2147483647 4294967295 8589934591 17179869183 34359738367 68719476735 137438953471 274877906943 549755813887 1099511627775 2199023255551 4398046511103 8796093022207 17592186044415 35184372088831 70368744177663 140737488355327 281474976710655 562949953421311 1125899906842623",
"output": "1"
},
{
"input": "1 1\n847523811295364",
"output": "847523811295364"
},
{
"input": "16 4\n874317845770206 387621540079007 155991277344382 691438508026243 930771471405935 572771476513435 743983828305226 4504492981600 163201683368400 839227763533133 66330553316499 1019586586453035 361344913793137 983404846165187 1054785536664003 328380935299964",
"output": "1214153029059594"
},
{
"input": "8 3\n1091780344011621 940196424994342 1122602056403610 446348279968546 844618893669060 630726534256806 583591612685392 410152590972252",
"output": "633396032195072"
},
{
"input": "4 2\n162835105872970 116592879151848 139965176195127 395498533500931",
"output": "392680337115137"
},
{
"input": "2 1\n209353646212060 198207100745412",
"output": "407560746957472"
},
{
"input": "16 14\n104025519164115 388661629481441 867118235830760 833392737978583 446248951095413 1075527144295046 1065273356416483 60098918081731 29503418096534 995667681082455 1087347063175248 131807733936246 235096689659338 202086293947852 889771265698655 384926479287439",
"output": "0"
},
{
"input": "8 5\n173728498114277 92445238671418 136142198208213 974561146026363 1099401507969061 180377338638143 458779251007513 55384960555288",
"output": "140737555464192"
},
{
"input": "4 3\n266761283825161 255669386891250 772637301427796 659604966525743",
"output": "19928690198528"
},
{
"input": "8 1\n562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312",
"output": "4503599627370496"
},
{
"input": "16 1\n562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312 562949953421312",
"output": "9007199254740992"
},
{
"input": "17 1\n1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 1125899906842623 16",
"output": "18014398509481984"
}
] | 655 | 50,995,200 | 3 | 6,122 |
|
40 | Find Color | [
"constructive algorithms",
"geometry",
"implementation",
"math"
] | A. Find Color | 2 | 256 | Not so long ago as a result of combat operations the main Berland place of interest β the magic clock β was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin corresponds to the clock center. The clock was painted two colors as is shown in the picture:
The picture shows only the central part of the clock. This coloring naturally extends to infinity.
The balls can be taken to be points on the plane. Your task is to find the color of the area, damaged by the given ball.
All the points located on the border of one of the areas have to be considered painted black. | The first and single line contains two integers *x* and *y* β the coordinates of the hole made in the clock by the ball. Each of the numbers *x* and *y* has an absolute value that does not exceed 1000. | Find the required color.
All the points between which and the origin of coordinates the distance is integral-value are painted black. | [
"-2 1\n",
"2 1\n",
"4 3\n"
] | [
"white\n",
"black\n",
"black\n"
] | none | [
{
"input": "-2 1",
"output": "white"
},
{
"input": "2 1",
"output": "black"
},
{
"input": "4 3",
"output": "black"
},
{
"input": "3 3",
"output": "black"
},
{
"input": "4 4",
"output": "white"
},
{
"input": "-4 4",
"output": "black"
},
{
"input": "4 -4",
"output": "black"
},
{
"input": "-4 -4",
"output": "white"
},
{
"input": "0 0",
"output": "black"
},
{
"input": "0 1",
"output": "black"
},
{
"input": "0 2",
"output": "black"
},
{
"input": "0 1000",
"output": "black"
},
{
"input": "1000 0",
"output": "black"
},
{
"input": "-1000 0",
"output": "black"
},
{
"input": "0 -1000",
"output": "black"
},
{
"input": "1000 -1000",
"output": "white"
},
{
"input": "12 5",
"output": "black"
},
{
"input": "12 -5",
"output": "black"
},
{
"input": "-12 -35",
"output": "black"
},
{
"input": "20 -21",
"output": "black"
},
{
"input": "-677 492",
"output": "white"
},
{
"input": "-673 -270",
"output": "white"
},
{
"input": "-668 970",
"output": "black"
},
{
"input": "-220 208",
"output": "white"
},
{
"input": "-215 -996",
"output": "black"
},
{
"input": "-211 243",
"output": "black"
},
{
"input": "-206 -518",
"output": "white"
},
{
"input": "-201 278",
"output": "black"
},
{
"input": "-196 -484",
"output": "black"
},
{
"input": "902 479",
"output": "white"
},
{
"input": "-441 572",
"output": "white"
},
{
"input": "217 221",
"output": "white"
},
{
"input": "875 -129",
"output": "white"
},
{
"input": "-469 -36",
"output": "black"
},
{
"input": "189 -387",
"output": "white"
},
{
"input": "847 -294",
"output": "white"
},
{
"input": "-496 -644",
"output": "black"
},
{
"input": "-281 -552",
"output": "white"
},
{
"input": "377 -902",
"output": "black"
},
{
"input": "165 -738",
"output": "white"
},
{
"input": "61 -175",
"output": "black"
},
{
"input": "-42 389",
"output": "black"
},
{
"input": "-589 952",
"output": "black"
},
{
"input": "-693 -929",
"output": "white"
},
{
"input": "-796 -365",
"output": "white"
},
{
"input": "658 198",
"output": "white"
},
{
"input": "555 319",
"output": "black"
},
{
"input": "8 882",
"output": "black"
},
{
"input": "-96 -556",
"output": "black"
},
{
"input": "-129 489",
"output": "black"
},
{
"input": "207 -224",
"output": "black"
},
{
"input": "64 0",
"output": "black"
},
{
"input": "17 144",
"output": "black"
},
{
"input": "60 -448",
"output": "black"
},
{
"input": "-399 -40",
"output": "black"
},
{
"input": "128 -504",
"output": "black"
},
{
"input": "0 72",
"output": "black"
},
{
"input": "168 -26",
"output": "black"
},
{
"input": "72 -154",
"output": "black"
},
{
"input": "117 -44",
"output": "black"
},
{
"input": "-72 -646",
"output": "black"
},
{
"input": "253 -204",
"output": "black"
},
{
"input": "-40 198",
"output": "black"
},
{
"input": "-216 -90",
"output": "black"
},
{
"input": "15 -8",
"output": "black"
},
{
"input": "-180 -432",
"output": "black"
},
{
"input": "280 342",
"output": "black"
},
{
"input": "132 224",
"output": "black"
},
{
"input": "-192 -256",
"output": "black"
},
{
"input": "351 -280",
"output": "black"
}
] | 154 | 6,758,400 | 3.948911 | 6,126 |
126 | Pills | [
"brute force",
"flows"
] | null | null | Doctor prescribed medicine to his patient. The medicine is represented by pills. Each pill consists of a shell and healing powder. The shell consists of two halves; each half has one of four colors β blue, red, white or yellow.
The doctor wants to put 28 pills in a rectangular box 7<=Γ<=8 in size. Besides, each pill occupies exactly two neighboring cells and any cell contains exactly one half of a pill. Thus, the result is a four colored picture 7<=Γ<=8 in size.
The doctor thinks that a patient will recover sooner if the picture made by the pills will be special. Unfortunately, putting the pills in the box so as to get the required picture is not a very easy task. That's why doctor asks you to help.
Doctor has some amount of pills of each of 10 painting types. They all contain the same medicine, that's why it doesn't matter which 28 of them will be stored inside the box.
Place the pills in the box so that the required picture was formed. If it is impossible to place the pills in the required manner, then place them so that the number of matching colors in all 56 cells in the final arrangement and the doctor's picture were maximum. | First 7 lines contain the doctor's picture. Each line contains 8 characters, each character can be "B", "R", "W" and "Y" that stands for blue, red, white and yellow colors correspondingly.
Next four lines contain 10 numbers that stand for, correspondingly, the number of pills painted:
"BY" "BW" "BR" "BB"
"RY" "RW" "RR"
"WY" "WW"
"YY"
Those numbers lie within range from 0 to 28 inclusively. It is guaranteed that the total number of pills in no less than 28. | Print on the first line the maximal number cells for which the colors match.
Then print 13 lines each containing 15 characters β the pills' position in the optimal arrangement. The intersections of odd lines and odd columns should contain characters "B", "R", "W" and "Y". All other positions should contain characters ".", "-" and "|". Use "-" and "|" to show which halves belong to one pill. See the samples for more clarification.
If there are several possible solutions, print any of them. | [
"WWWBBWWW\nWWWBBWWW\nYYWBBWWW\nYYWBBWRR\nYYWBBWRR\nYYWBBWRR\nYYWBBWRR\n0 0 0 8\n0 1 5\n1 10\n5\n",
"WWWWWWWW\nWBYWRBBY\nBRYRBWYY\nWWBRYWBB\nBWWRWBYW\nRBWRBWYY\nWWWWWWWW\n0 0 0 1\n0 0 1\n0 1\n25\n"
] | [
"53\nW.W.W.B.B.W.W.W\n|.|.|.|.|.|.|.|\nW.W.W.B.B.W.W.W\n...............\nY.Y.W.B.B.W.W-W\n|.|.|.|.|.|....\nY.Y.W.B.B.W.R.R\n............|.|\nY.Y.W.B.B.R.R.R\n|.|.|.|.|.|....\nY.Y.W.B.B.W.R.R\n............|.|\nY-Y.B-B.B-B.R.R\n",
"15\nW.Y.Y-Y.Y-Y.Y-Y\n|.|............\nW.Y.Y.Y.Y.B-B.Y\n....|.|.|.....|\nY-Y.Y.Y.Y.Y-Y.Y\n...............\nY.Y.Y.R.Y.Y.Y-Y\n|.|.|.|.|.|....\nY.Y.Y.R.Y.Y.Y.Y\n............|.|\nY-Y.Y.Y-Y.Y.Y.Y\n....|.....|....\nY-Y.Y.Y-Y.Y.Y-Y\n"
] | none | [] | 46 | 0 | 0 | 6,142 |
|
229 | Planets | [
"binary search",
"data structures",
"graphs",
"shortest paths"
] | null | null | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.
Overall the galaxy has *n* planets, indexed with numbers from 1 to *n*. Jack is on the planet with index 1, and Apophis will land on the planet with index *n*. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.
It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time *t* another traveller arrives to the planet, Jack can only pass through the stargate at time *t*<=+<=1, unless there are more travellers arriving at time *t*<=+<=1 to the same planet.
Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index *n*. | The first line contains two space-separated integers: *n* (2<=β€<=*n*<=β€<=105), the number of planets in the galaxy, and *m* (0<=β€<=*m*<=β€<=105) β the number of pairs of planets between which Jack can travel using stargates. Then *m* lines follow, containing three integers each: the *i*-th line contains numbers of planets *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*), which are connected through stargates, and the integer transfer time (in seconds) *c**i* (1<=β€<=*c**i*<=β€<=104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection.
Then *n* lines follow: the *i*-th line contains an integer *k**i* (0<=β€<=*k**i*<=β€<=105) that denotes the number of moments of time when other travellers arrive to the planet with index *i*. Then *k**i* distinct space-separated integers *t**ij* (0<=β€<=*t**ij*<=<<=109) follow, sorted in ascending order. An integer *t**ij* means that at time *t**ij* (in seconds) another traveller arrives to the planet *i*. It is guaranteed that the sum of all *k**i* does not exceed 105. | Print a single number β the least amount of time Jack needs to get from planet 1 to planet *n*. If Jack can't get to planet *n* in any amount of time, print number -1. | [
"4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0\n",
"3 1\n1 2 3\n0\n1 3\n0\n"
] | [
"7\n",
"-1\n"
] | In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then β to planet 4, then he spends a total of only 2β+β5β=β7 seconds.
In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | [
{
"input": "4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0",
"output": "7"
},
{
"input": "3 1\n1 2 3\n0\n1 3\n0",
"output": "-1"
},
{
"input": "2 1\n1 2 3\n0\n1 3",
"output": "3"
},
{
"input": "2 1\n1 2 3\n1 0\n0",
"output": "4"
},
{
"input": "3 3\n1 2 5\n2 3 6\n1 3 7\n0\n0\n0",
"output": "7"
},
{
"input": "3 3\n1 2 3\n2 3 2\n1 3 7\n0\n0\n0",
"output": "5"
},
{
"input": "2 0\n0\n0",
"output": "-1"
},
{
"input": "3 1\n1 2 3\n1 1\n1 5\n0",
"output": "-1"
},
{
"input": "2 1\n1 2 3\n0\n2 2 4",
"output": "3"
},
{
"input": "2 1\n1 2 1\n0\n0",
"output": "1"
},
{
"input": "2 1\n2 1 10000\n0\n0",
"output": "10000"
},
{
"input": "2 1\n1 2 3\n0\n3 3 4 5",
"output": "3"
},
{
"input": "3 0\n0\n0\n0",
"output": "-1"
},
{
"input": "3 2\n1 2 5\n2 3 7\n2 0 1\n3 4 5 6\n3 11 12 13",
"output": "14"
},
{
"input": "2 1\n1 2 3\n3 0 1 2\n3 5 6 7",
"output": "6"
},
{
"input": "3 3\n1 2 3\n2 3 2\n1 3 7\n0\n4 3 4 5 6\n0",
"output": "7"
},
{
"input": "6 7\n1 2 1\n1 3 8\n2 4 2\n4 3 3\n3 5 4\n4 6 100\n5 6 5\n0\n0\n1 7\n2 3 4\n0\n0",
"output": "17"
},
{
"input": "3 3\n1 2 3\n2 3 2\n1 3 6\n0\n1 3\n0",
"output": "6"
},
{
"input": "7 7\n1 2 1\n2 4 2\n2 3 2\n3 6 2\n6 5 2\n4 5 3\n5 7 7\n0\n0\n0\n3 3 4 5\n0\n0\n0",
"output": "14"
},
{
"input": "7 6\n1 2 1\n1 3 1\n1 4 1\n1 5 1\n1 6 1\n1 7 1\n1 0\n0\n0\n0\n0\n0\n0",
"output": "2"
},
{
"input": "8 10\n1 2 3\n2 8 3\n1 4 1\n4 3 6\n3 7 7\n4 5 5\n5 7 2\n7 8 1\n1 6 8\n6 8 7\n0\n4 1 2 3 4\n0\n0\n0\n0\n0\n0",
"output": "8"
},
{
"input": "7 6\n1 2 1\n1 3 2\n2 4 3\n2 5 4\n3 5 6\n3 6 7\n0\n3 1 2 3\n2 2 3\n0\n2 7 8\n0\n0",
"output": "-1"
}
] | 92 | 0 | 0 | 6,151 |
|
722 | Generating Sets | [
"binary search",
"data structures",
"dfs and similar",
"greedy",
"strings",
"trees"
] | null | null | You are given a set *Y* of *n* distinct positive integers *y*1,<=*y*2,<=...,<=*y**n*.
Set *X* of *n* distinct positive integers *x*1,<=*x*2,<=...,<=*x**n* is said to generate set *Y* if one can transform *X* to *Y* by applying some number of the following two operation to integers in *X*:
1. Take any integer *x**i* and multiply it by two, i.e. replace *x**i* with 2Β·*x**i*. 1. Take any integer *x**i*, multiply it by two and add one, i.e. replace *x**i* with 2Β·*x**i*<=+<=1.
Note that integers in *X* are not required to be distinct after each operation.
Two sets of distinct integers *X* and *Y* are equal if they are equal as sets. In other words, if we write elements of the sets in the array in the increasing order, these arrays would be equal.
Note, that any set of integers (or its permutation) generates itself.
You are given a set *Y* and have to find a set *X* that generates *Y* and the maximum element of *X* is mininum possible. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=50<=000)Β β the number of elements in *Y*.
The second line contains *n* integers *y*1,<=...,<=*y**n* (1<=β€<=*y**i*<=β€<=109), that are guaranteed to be distinct. | Print *n* integersΒ β set of distinct integers that generate *Y* and the maximum element of which is minimum possible. If there are several such sets, print any of them. | [
"5\n1 2 3 4 5\n",
"6\n15 14 3 13 1 12\n",
"6\n9 7 13 17 5 11\n"
] | [
"4 5 2 3 1 \n",
"12 13 14 7 3 1 \n",
"4 5 2 6 3 1 \n"
] | none | [
{
"input": "5\n1 2 3 4 5",
"output": "4 5 2 3 1 "
},
{
"input": "6\n15 14 3 13 1 12",
"output": "12 13 14 7 3 1 "
},
{
"input": "6\n9 7 13 17 5 11",
"output": "4 5 2 6 3 1 "
},
{
"input": "10\n18 14 19 17 11 7 20 10 4 12",
"output": "8 9 4 10 5 2 6 7 3 1 "
},
{
"input": "100\n713 716 230 416 3 2 597 216 779 839 13 156 723 793 168 368 232 316 98 257 170 27 746 9 616 147 792 890 796 362 852 117 993 556 885 73 131 475 121 753 508 158 473 931 527 282 541 325 606 321 159 17 682 290 586 685 529 11 645 224 821 53 152 966 269 754 672 523 386 347 719 525 92 315 832 393 893 83 956 725 258 851 112 38 601 782 324 210 642 818 56 485 679 10 922 469 36 990 14 742",
"output": "128 129 130 131 65 32 132 134 135 139 141 17 145 146 147 73 36 149 150 151 152 154 38 156 157 158 159 79 9 160 161 80 162 81 83 168 84 85 42 86 21 10 89 44 90 45 22 92 93 46 94 47 23 11 5 2 96 97 48 98 99 49 24 102 51 12 104 105 52 106 53 26 108 110 111 55 27 13 6 112 56 115 57 28 116 117 58 118 119 59 29 14 120 121 60 123 124 127 3 1 "
},
{
"input": "100\n41 173 40 30 165 155 92 180 193 24 187 189 65 4 200 80 152 174 20 81 170 72 104 8 13 7 117 176 191 34 90 46 17 188 63 134 76 60 116 42 183 45 1 103 15 119 142 70 148 136 73 68 86 94 32 190 112 166 141 78 6 102 66 97 93 106 47 22 132 129 139 177 62 105 100 77 88 54 3 167 120 145 197 195 64 11 38 2 28 140 87 109 185 23 31 153 39 18 57 122",
"output": "129 64 65 32 132 66 134 136 68 139 34 140 141 70 142 17 8 145 72 73 148 18 152 153 76 155 77 38 78 39 4 80 81 40 165 166 167 41 20 170 42 173 86 174 87 176 177 88 180 90 183 45 22 185 92 187 93 46 188 189 94 95 47 23 11 5 2 96 97 48 98 24 100 50 102 103 104 105 106 109 54 13 6 112 57 28 116 117 119 120 60 122 30 62 63 31 15 7 3 1 "
},
{
"input": "10\n10 1 6 7 9 8 4 3 5 2",
"output": "8 9 4 10 5 2 6 7 3 1 "
},
{
"input": "100\n70 54 10 72 81 84 56 15 27 19 43 100 49 44 52 33 63 40 95 17 58 2 51 39 22 18 82 1 16 99 32 29 24 94 9 98 5 37 47 14 42 73 41 31 79 64 12 6 53 26 68 67 89 13 90 4 21 93 46 74 75 88 66 57 23 7 25 48 92 62 30 8 50 61 38 87 71 34 97 28 80 11 60 91 3 35 86 96 36 20 59 65 83 45 76 77 78 69 85 55",
"output": "64 65 32 66 67 33 16 68 69 34 70 71 35 17 8 72 73 36 74 75 37 18 76 77 38 78 79 39 19 9 4 80 81 40 82 83 41 20 84 85 42 86 87 43 21 10 88 89 44 90 91 45 22 92 93 46 94 95 47 23 11 5 2 96 97 48 98 99 49 24 100 50 51 25 12 52 53 26 54 55 27 13 6 56 57 28 58 59 29 14 60 61 30 62 63 31 15 7 3 1 "
},
{
"input": "1\n32",
"output": "1 "
},
{
"input": "30\n1000000000 500000000 250000000 125000000 62500000 31250000 15625000 7812500 3906250 1953125 976562 488281 244140 122070 61035 30517 15258 7629 3814 1907 953 476 238 119 59 29 14 7 3 1",
"output": "1000000000 500000000 250000000 125000000 62500000 31250000 15625000 7812500 3906250 1953125 976562 488281 244140 122070 61035 30517 15258 7629 3814 1907 953 476 238 119 59 29 14 7 3 1 "
}
] | 46 | 0 | 0 | 6,178 |
|
770 | Online Courses In BSU | [
"*special",
"dfs and similar",
"graphs",
"implementation"
] | null | null | Now you can take online courses in the Berland State University! Polycarp needs to pass *k* main online courses of his specialty to get a diploma. In total *n* courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once. | The first line contains *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=105) β the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains *k* distinct integers from 1 to *n* β numbers of main online-courses of Polycarp's specialty.
Then *n* lines follow, each of them describes the next course: the *i*-th of them corresponds to the course *i*. Each line starts from the integer *t**i* (0<=β€<=*t**i*<=β€<=*n*<=-<=1) β the number of courses on which the *i*-th depends. Then there follows the sequence of *t**i* distinct integers from 1 to *n* β numbers of courses in random order, on which the *i*-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values *t**i* doesn't exceed 105. | Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer *m* β the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print *m* distinct integers β numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them. | [
"6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5\n",
"9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2\n",
"3 3\n1 2 3\n1 2\n1 3\n1 1\n"
] | [
"5\n1 2 3 4 5 \n",
"6\n1 2 9 4 5 3 \n",
"-1\n"
] | In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course. | [
{
"input": "6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5",
"output": "5\n1 2 3 4 5 "
},
{
"input": "9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2",
"output": "6\n1 2 9 4 5 3 "
},
{
"input": "3 3\n1 2 3\n1 2\n1 3\n1 1",
"output": "-1"
},
{
"input": "5 3\n2 1 4\n0\n0\n1 5\n0\n0",
"output": "3\n1 2 4 "
},
{
"input": "5 2\n4 1\n0\n1 4\n1 5\n0\n2 1 2",
"output": "2\n1 4 "
},
{
"input": "5 2\n4 5\n2 3 4\n1 4\n1 4\n0\n0",
"output": "2\n4 5 "
},
{
"input": "6 6\n5 4 3 2 6 1\n1 4\n0\n2 2 6\n2 3 6\n3 3 4 6\n0",
"output": "6\n2 6 3 4 1 5 "
},
{
"input": "6 6\n4 1 6 3 2 5\n2 3 5\n4 1 3 4 5\n1 5\n2 3 5\n0\n2 1 5",
"output": "6\n5 3 1 4 2 6 "
},
{
"input": "6 5\n2 4 1 3 5\n0\n0\n0\n1 1\n0\n1 3",
"output": "5\n1 2 3 4 5 "
},
{
"input": "7 6\n4 3 2 1 6 5\n0\n2 4 5\n1 6\n1 7\n1 6\n0\n1 4",
"output": "-1"
},
{
"input": "7 2\n1 5\n5 2 3 4 5 6\n2 1 7\n0\n3 1 2 7\n0\n2 5 7\n0",
"output": "-1"
},
{
"input": "7 6\n2 5 3 1 7 6\n1 7\n2 3 7\n0\n0\n0\n1 3\n1 2",
"output": "-1"
},
{
"input": "3 3\n1 3 2\n0\n1 3\n1 1",
"output": "3\n1 3 2 "
},
{
"input": "10 1\n1\n1 5\n1 3\n0\n1 10\n0\n1 8\n1 1\n2 7 4\n2 6 2\n0",
"output": "2\n5 1 "
},
{
"input": "1 1\n1\n0",
"output": "1\n1 "
},
{
"input": "2 2\n1 2\n0\n0",
"output": "2\n1 2 "
},
{
"input": "2 2\n2 1\n0\n0",
"output": "2\n1 2 "
},
{
"input": "2 1\n1\n1 2\n0",
"output": "2\n2 1 "
},
{
"input": "2 1\n1\n0\n0",
"output": "1\n1 "
},
{
"input": "2 1\n2\n0\n1 1",
"output": "2\n1 2 "
},
{
"input": "2 1\n2\n0\n0",
"output": "1\n2 "
},
{
"input": "3 1\n1\n2 2 3\n0\n1 2",
"output": "3\n2 3 1 "
},
{
"input": "3 3\n2 1 3\n0\n2 1 3\n1 2",
"output": "-1"
},
{
"input": "10 3\n8 4 1\n1 3\n0\n0\n0\n1 1\n2 10 9\n1 4\n3 5 1 2\n2 2 7\n2 8 4",
"output": "6\n3 1 2 4 5 8 "
},
{
"input": "6 6\n1 2 3 4 5 6\n2 2 6\n1 3\n2 4 5\n0\n1 4\n1 2",
"output": "6\n4 5 3 2 6 1 "
},
{
"input": "3 2\n1 3\n0\n0\n1 1",
"output": "2\n1 3 "
},
{
"input": "3 1\n1\n2 2 3\n0\n0",
"output": "3\n2 3 1 "
},
{
"input": "3 3\n3 1 2\n0\n0\n0",
"output": "3\n1 2 3 "
},
{
"input": "3 3\n1 2 3\n0\n0\n0",
"output": "3\n1 2 3 "
},
{
"input": "3 2\n2 1\n0\n0\n0",
"output": "2\n1 2 "
},
{
"input": "3 3\n3 2 1\n0\n0\n0",
"output": "3\n1 2 3 "
},
{
"input": "3 3\n3 2 1\n0\n0\n0",
"output": "3\n1 2 3 "
},
{
"input": "3 3\n3 1 2\n0\n0\n0",
"output": "3\n1 2 3 "
},
{
"input": "3 2\n3 2\n0\n1 3\n1 1",
"output": "3\n1 3 2 "
},
{
"input": "3 3\n2 1 3\n0\n1 1\n0",
"output": "3\n1 2 3 "
},
{
"input": "3 2\n3 1\n1 3\n0\n0",
"output": "2\n3 1 "
},
{
"input": "3 1\n3\n0\n0\n1 2",
"output": "2\n2 3 "
},
{
"input": "3 1\n1\n0\n1 1\n0",
"output": "1\n1 "
},
{
"input": "3 2\n3 2\n0\n1 1\n1 2",
"output": "3\n1 2 3 "
},
{
"input": "3 3\n1 2 3\n0\n1 1\n2 1 2",
"output": "3\n1 2 3 "
},
{
"input": "4 2\n2 3\n2 3 4\n1 1\n0\n0",
"output": "4\n3 4 1 2 "
},
{
"input": "4 4\n3 2 1 4\n2 2 3\n1 1\n1 2\n1 3",
"output": "-1"
},
{
"input": "4 2\n4 3\n0\n0\n0\n0",
"output": "2\n3 4 "
},
{
"input": "4 1\n1\n2 2 3\n0\n2 2 4\n0",
"output": "4\n2 4 3 1 "
},
{
"input": "4 1\n2\n0\n0\n2 1 4\n2 1 2",
"output": "1\n2 "
},
{
"input": "4 4\n3 1 4 2\n1 2\n1 3\n1 2\n0",
"output": "-1"
},
{
"input": "4 4\n1 3 2 4\n1 3\n1 3\n0\n1 2",
"output": "4\n3 1 2 4 "
},
{
"input": "4 1\n4\n2 2 4\n0\n1 2\n0",
"output": "1\n4 "
},
{
"input": "4 2\n3 1\n0\n0\n0\n0",
"output": "2\n1 3 "
},
{
"input": "4 4\n3 1 4 2\n1 4\n0\n0\n0",
"output": "4\n4 1 2 3 "
},
{
"input": "4 1\n1\n1 4\n2 1 3\n1 4\n1 3",
"output": "-1"
},
{
"input": "4 2\n3 2\n0\n1 4\n1 1\n0",
"output": "4\n1 4 2 3 "
},
{
"input": "4 4\n2 3 1 4\n0\n2 1 3\n2 1 4\n0",
"output": "4\n1 4 3 2 "
},
{
"input": "4 4\n4 1 2 3\n2 2 4\n0\n0\n0",
"output": "4\n2 4 1 3 "
},
{
"input": "4 1\n1\n0\n1 1\n0\n0",
"output": "1\n1 "
},
{
"input": "5 1\n5\n0\n1 1\n2 2 5\n0\n0",
"output": "1\n5 "
},
{
"input": "5 5\n1 2 4 3 5\n0\n0\n2 1 2\n1 5\n0",
"output": "5\n1 2 3 5 4 "
},
{
"input": "5 5\n2 1 5 4 3\n1 4\n0\n0\n0\n1 2",
"output": "5\n4 1 2 3 5 "
},
{
"input": "5 2\n2 4\n1 2\n0\n1 2\n1 2\n0",
"output": "2\n2 4 "
},
{
"input": "5 2\n2 1\n1 3\n1 3\n1 1\n3 1 2 3\n1 3",
"output": "-1"
},
{
"input": "5 4\n5 2 1 3\n2 3 5\n1 3\n0\n0\n2 2 4",
"output": "5\n3 2 4 5 1 "
},
{
"input": "5 4\n5 1 4 2\n0\n0\n1 5\n1 1\n0",
"output": "4\n1 2 4 5 "
},
{
"input": "5 2\n1 3\n0\n2 4 5\n0\n1 2\n2 1 2",
"output": "2\n1 3 "
},
{
"input": "5 1\n5\n1 4\n2 1 4\n2 4 5\n2 2 5\n1 1",
"output": "-1"
},
{
"input": "5 4\n3 2 1 4\n1 2\n0\n0\n0\n0",
"output": "4\n2 1 3 4 "
},
{
"input": "5 1\n2\n3 2 3 4\n0\n2 2 4\n0\n4 1 2 3 4",
"output": "1\n2 "
},
{
"input": "5 3\n5 2 4\n1 4\n0\n0\n0\n0",
"output": "3\n2 4 5 "
},
{
"input": "5 1\n3\n2 4 5\n0\n0\n0\n1 3",
"output": "1\n3 "
},
{
"input": "5 3\n2 5 1\n1 2\n0\n0\n1 5\n0",
"output": "3\n2 1 5 "
},
{
"input": "5 3\n4 2 3\n0\n0\n1 2\n0\n1 4",
"output": "3\n2 3 4 "
},
{
"input": "6 4\n2 1 4 3\n3 3 4 5\n1 4\n0\n1 3\n4 2 3 4 6\n1 3",
"output": "6\n3 4 2 6 5 1 "
},
{
"input": "6 2\n3 6\n2 2 3\n0\n1 1\n1 6\n0\n0",
"output": "-1"
},
{
"input": "6 1\n2\n0\n0\n1 6\n0\n1 2\n0",
"output": "1\n2 "
},
{
"input": "6 3\n6 5 1\n0\n1 1\n0\n1 3\n0\n1 5",
"output": "3\n1 5 6 "
},
{
"input": "6 6\n1 3 6 5 4 2\n0\n0\n0\n0\n0\n0",
"output": "6\n1 2 3 4 5 6 "
},
{
"input": "6 5\n3 4 1 6 5\n2 2 6\n2 4 5\n1 1\n0\n1 4\n0",
"output": "6\n4 5 2 6 1 3 "
},
{
"input": "6 2\n5 2\n1 4\n0\n1 2\n0\n0\n1 5",
"output": "2\n2 5 "
},
{
"input": "6 6\n4 5 1 6 3 2\n0\n1 6\n1 1\n2 1 3\n1 1\n2 1 3",
"output": "6\n1 3 6 2 4 5 "
},
{
"input": "6 6\n3 2 4 1 5 6\n1 6\n1 1\n0\n1 5\n0\n0",
"output": "6\n6 1 2 3 5 4 "
},
{
"input": "6 1\n3\n2 4 6\n2 4 6\n2 1 2\n1 2\n1 2\n1 5",
"output": "-1"
},
{
"input": "6 6\n5 1 2 3 6 4\n0\n0\n0\n0\n1 4\n1 1",
"output": "6\n1 2 3 4 5 6 "
},
{
"input": "6 5\n3 6 2 4 1\n1 4\n1 3\n0\n0\n0\n2 1 5",
"output": "6\n4 1 3 2 5 6 "
},
{
"input": "6 4\n4 3 6 5\n0\n0\n3 1 4 5\n1 6\n1 6\n0",
"output": "5\n1 6 4 5 3 "
},
{
"input": "6 1\n1\n0\n0\n1 5\n0\n0\n1 5",
"output": "1\n1 "
},
{
"input": "6 6\n4 2 5 6 1 3\n1 3\n0\n2 5 6\n2 2 6\n1 2\n1 4",
"output": "-1"
},
{
"input": "7 7\n1 7 6 2 5 4 3\n0\n2 5 6\n1 5\n1 2\n0\n1 1\n1 1",
"output": "7\n1 5 6 2 3 4 7 "
},
{
"input": "7 6\n6 3 5 1 4 7\n0\n0\n0\n0\n1 1\n1 2\n1 1",
"output": "7\n1 2 3 4 5 6 7 "
},
{
"input": "7 2\n2 3\n0\n0\n0\n0\n0\n1 4\n0",
"output": "2\n2 3 "
},
{
"input": "7 4\n7 5 4 2\n0\n2 6 7\n0\n1 3\n2 2 6\n0\n2 3 4",
"output": "6\n6 3 4 7 2 5 "
},
{
"input": "7 6\n5 4 2 1 6 7\n2 2 7\n1 5\n0\n0\n1 3\n1 2\n0",
"output": "7\n3 5 2 7 1 4 6 "
},
{
"input": "7 4\n2 1 6 7\n0\n2 3 6\n1 6\n0\n2 1 3\n1 7\n0",
"output": "5\n1 7 6 3 2 "
},
{
"input": "7 2\n5 1\n4 2 5 6 7\n1 5\n5 1 2 5 6 7\n1 2\n0\n0\n4 2 4 5 6",
"output": "6\n5 2 6 4 7 1 "
},
{
"input": "7 1\n5\n2 2 5\n0\n2 5 7\n0\n1 6\n0\n0",
"output": "2\n6 5 "
},
{
"input": "7 6\n5 7 2 4 3 6\n2 5 7\n0\n3 2 5 7\n2 2 6\n0\n0\n2 2 5",
"output": "6\n2 5 7 3 6 4 "
},
{
"input": "7 4\n6 4 7 3\n0\n0\n2 2 5\n1 6\n2 1 7\n2 1 2\n0",
"output": "7\n1 2 7 5 3 6 4 "
},
{
"input": "7 5\n1 5 4 7 2\n1 4\n4 1 4 6 7\n2 1 4\n1 6\n3 3 4 7\n0\n0",
"output": "7\n6 4 1 7 2 3 5 "
},
{
"input": "2 1\n1\n0\n1 1",
"output": "1\n1 "
},
{
"input": "2 1\n1\n1 2\n1 1",
"output": "-1"
},
{
"input": "2 1\n2\n1 2\n0",
"output": "1\n2 "
},
{
"input": "2 1\n2\n1 2\n1 1",
"output": "-1"
},
{
"input": "2 2\n1 2\n1 2\n0",
"output": "2\n2 1 "
},
{
"input": "2 2\n2 1\n0\n1 1",
"output": "2\n1 2 "
},
{
"input": "2 2\n2 1\n1 2\n1 1",
"output": "-1"
},
{
"input": "7 1\n4\n0\n6 1 3 4 5 6 7\n4 1 4 6 7\n2 1 7\n4 1 3 6 7\n2 3 4\n0",
"output": "3\n1 7 4 "
},
{
"input": "7 2\n1 2\n0\n0\n3 2 4 6\n1 3\n1 6\n1 5\n0",
"output": "2\n1 2 "
},
{
"input": "7 4\n1 7 6 2\n1 7\n0\n0\n0\n1 1\n0\n0",
"output": "4\n7 1 2 6 "
},
{
"input": "7 6\n3 7 4 1 6 2\n2 4 6\n0\n0\n3 2 3 5\n1 3\n1 2\n3 1 5 6",
"output": "7\n2 3 5 4 6 1 7 "
},
{
"input": "8 5\n7 1 2 8 3\n0\n0\n0\n0\n0\n0\n0\n0",
"output": "5\n1 2 3 7 8 "
},
{
"input": "8 3\n4 8 7\n0\n1 3\n0\n1 2\n0\n0\n1 1\n0",
"output": "6\n1 3 2 4 7 8 "
},
{
"input": "8 2\n2 6\n0\n0\n0\n2 5 7\n0\n2 1 2\n0\n3 1 2 3",
"output": "3\n1 2 6 "
},
{
"input": "8 6\n8 3 6 4 7 5\n0\n1 4\n1 4\n1 8\n1 7\n1 4\n0\n0",
"output": "6\n8 4 3 7 5 6 "
},
{
"input": "8 7\n2 5 3 6 4 8 1\n3 3 5 6\n1 3\n2 4 5\n4 1 2 5 6\n2 1 2\n2 2 8\n1 2\n2 6 7",
"output": "-1"
},
{
"input": "8 5\n2 5 8 3 1\n3 2 5 6\n1 5\n1 4\n5 1 5 6 7 8\n0\n2 2 8\n4 1 3 5 6\n1 2",
"output": "-1"
},
{
"input": "8 5\n6 4 7 5 1\n1 7\n1 6\n1 1\n0\n0\n0\n1 5\n1 7",
"output": "5\n5 7 1 4 6 "
},
{
"input": "8 3\n3 1 8\n0\n3 4 6 7\n2 6 7\n2 3 6\n2 4 6\n1 1\n1 1\n1 3",
"output": "5\n1 6 7 3 8 "
},
{
"input": "8 8\n6 3 1 2 4 8 5 7\n0\n0\n0\n2 5 7\n0\n1 5\n0\n1 1",
"output": "8\n1 2 3 5 7 4 6 8 "
},
{
"input": "8 5\n2 1 5 7 6\n1 8\n3 3 4 6\n0\n0\n1 6\n0\n0\n0",
"output": "8\n8 1 3 4 6 2 5 7 "
},
{
"input": "8 8\n3 1 2 7 8 4 5 6\n2 4 8\n2 3 8\n1 6\n0\n2 4 6\n0\n5 2 3 4 5 8\n2 3 4",
"output": "8\n4 6 3 8 1 2 5 7 "
},
{
"input": "8 3\n4 3 1\n0\n0\n0\n0\n0\n0\n0\n0",
"output": "3\n1 3 4 "
},
{
"input": "8 1\n3\n0\n3 1 3 6\n0\n0\n1 1\n0\n1 6\n1 7",
"output": "1\n3 "
},
{
"input": "8 8\n5 8 7 2 1 3 4 6\n1 3\n3 1 3 4\n0\n0\n1 1\n1 5\n0\n2 4 6",
"output": "8\n3 1 4 2 5 6 7 8 "
},
{
"input": "8 7\n6 3 7 8 1 5 4\n0\n2 1 5\n0\n2 7 8\n1 4\n0\n0\n0",
"output": "7\n1 3 7 8 4 5 6 "
},
{
"input": "9 9\n6 3 1 4 2 9 5 7 8\n0\n0\n0\n0\n0\n0\n0\n0\n0",
"output": "9\n1 2 3 4 5 6 7 8 9 "
},
{
"input": "9 3\n5 7 3\n3 3 4 5\n4 4 6 7 9\n2 1 2\n2 3 5\n1 3\n4 4 5 7 8\n3 1 4 5\n3 1 3 4\n7 1 2 4 5 6 7 8",
"output": "-1"
},
{
"input": "9 6\n1 6 7 4 5 3\n2 2 6\n3 5 6 8\n5 2 4 5 6 9\n3 5 6 8\n0\n0\n5 2 3 5 6 9\n4 1 3 5 6\n5 1 2 4 6 8",
"output": "-1"
},
{
"input": "9 8\n4 2 9 1 8 3 7 6\n0\n2 1 8\n0\n0\n1 1\n2 1 8\n2 6 8\n3 4 5 9\n5 1 2 5 7 8",
"output": "-1"
},
{
"input": "9 2\n6 9\n2 3 9\n0\n1 8\n1 6\n3 3 6 7\n1 2\n1 9\n0\n0",
"output": "3\n2 6 9 "
},
{
"input": "9 6\n5 4 3 2 6 7\n3 4 5 9\n1 6\n4 1 5 8 9\n3 3 5 6\n0\n0\n2 3 8\n1 3\n0",
"output": "-1"
},
{
"input": "9 8\n2 8 4 7 3 6 9 5\n0\n1 4\n0\n0\n0\n1 8\n0\n3 2 3 7\n0",
"output": "8\n4 2 3 5 7 8 6 9 "
},
{
"input": "9 6\n6 7 1 5 9 2\n0\n0\n0\n0\n1 4\n0\n0\n2 1 3\n1 6",
"output": "7\n1 2 4 5 6 7 9 "
},
{
"input": "9 4\n5 1 2 3\n1 7\n0\n1 8\n0\n0\n3 1 5 8\n1 6\n2 5 7\n2 1 4",
"output": "-1"
},
{
"input": "9 8\n4 8 6 9 5 7 2 3\n0\n1 4\n0\n3 2 6 8\n1 6\n1 7\n0\n0\n2 3 6",
"output": "-1"
},
{
"input": "9 3\n8 5 3\n3 3 6 9\n1 5\n1 5\n1 8\n1 2\n1 3\n1 9\n1 5\n0",
"output": "-1"
},
{
"input": "9 6\n7 3 1 6 4 2\n1 3\n0\n1 7\n1 8\n1 4\n1 7\n1 8\n0\n2 1 7",
"output": "7\n8 7 3 1 2 4 6 "
},
{
"input": "9 2\n7 4\n1 2\n0\n1 7\n0\n1 1\n0\n0\n2 2 6\n1 5",
"output": "2\n4 7 "
},
{
"input": "9 5\n3 8 2 5 1\n1 5\n3 1 6 7\n3 4 6 8\n3 2 6 9\n2 7 9\n2 5 7\n1 2\n2 4 5\n2 1 6",
"output": "-1"
},
{
"input": "9 4\n6 9 7 8\n3 5 8 9\n1 3\n1 4\n0\n2 4 9\n2 4 9\n5 2 3 4 8 9\n0\n1 7",
"output": "-1"
},
{
"input": "10 1\n7\n2 4 10\n1 8\n2 4 8\n0\n1 3\n1 2\n2 3 5\n1 7\n0\n1 1",
"output": "-1"
},
{
"input": "10 2\n9 4\n0\n0\n0\n0\n1 7\n0\n0\n1 9\n0\n0",
"output": "2\n4 9 "
},
{
"input": "10 3\n7 5 3\n3 3 4 5\n1 10\n1 7\n3 2 6 7\n1 7\n0\n0\n3 1 4 6\n3 2 3 5\n1 6",
"output": "3\n7 3 5 "
},
{
"input": "10 1\n1\n1 5\n1 1\n3 4 6 10\n1 1\n0\n4 1 2 5 9\n4 1 6 9 10\n6 1 2 3 6 9 10\n2 2 5\n4 1 2 5 9",
"output": "2\n5 1 "
},
{
"input": "10 1\n4\n0\n0\n0\n0\n1 10\n0\n0\n0\n0\n0",
"output": "1\n4 "
},
{
"input": "10 10\n6 2 4 5 8 1 9 3 10 7\n4 2 7 8 9\n2 7 9\n5 1 6 8 9 10\n2 7 9\n6 1 4 6 7 8 9\n1 8\n0\n2 4 9\n0\n4 2 4 7 9",
"output": "10\n7 9 2 4 8 1 6 10 3 5 "
},
{
"input": "10 5\n2 1 10 4 9\n2 3 6\n5 1 6 7 8 10\n3 4 6 7\n2 1 6\n2 6 7\n1 3\n1 4\n3 5 6 10\n4 1 2 8 10\n4 1 5 6 7",
"output": "-1"
},
{
"input": "10 5\n4 8 3 1 6\n0\n1 10\n0\n0\n1 3\n2 3 5\n1 3\n1 10\n2 1 6\n0",
"output": "7\n1 3 4 5 6 10 8 "
},
{
"input": "10 8\n1 5 4 10 6 2 3 9\n7 3 4 5 6 7 8 10\n1 5\n4 2 5 7 10\n3 2 5 6\n0\n3 2 5 7\n1 2\n8 1 2 3 5 6 7 9 10\n4 2 4 6 7\n3 4 6 7",
"output": "-1"
},
{
"input": "10 5\n6 9 8 5 2\n2 7 9\n4 4 5 6 7\n2 6 7\n2 5 8\n2 6 9\n1 9\n2 2 6\n3 1 2 7\n3 3 5 6\n6 1 2 5 6 8 9",
"output": "-1"
},
{
"input": "10 7\n7 10 5 1 9 4 3\n4 2 4 9 10\n5 1 4 6 8 9\n7 2 4 5 6 7 8 10\n3 3 5 10\n2 7 10\n3 4 5 9\n6 1 2 3 4 6 8\n4 1 3 4 10\n1 5\n1 1",
"output": "-1"
},
{
"input": "10 9\n5 1 3 6 10 8 2 9 7\n0\n0\n2 1 6\n1 3\n1 4\n2 5 7\n1 6\n0\n1 8\n0",
"output": "-1"
},
{
"input": "10 4\n2 5 10 9\n2 2 4\n5 3 4 6 7 10\n2 7 10\n4 1 3 8 10\n2 6 10\n2 7 10\n1 1\n3 6 7 10\n1 7\n3 1 7 8",
"output": "-1"
},
{
"input": "10 8\n6 8 2 1 7 10 3 4\n0\n2 1 4\n2 6 7\n0\n3 1 8 9\n3 1 8 9\n0\n0\n1 6\n1 8",
"output": "-1"
},
{
"input": "10 3\n1 6 3\n1 4\n1 4\n0\n0\n2 3 10\n1 2\n0\n1 4\n0\n1 2",
"output": "5\n4 1 2 3 6 "
},
{
"input": "11 2\n10 7\n5 2 3 6 10 11\n0\n1 8\n5 1 3 6 9 10\n4 1 2 3 6\n1 5\n5 2 6 9 10 11\n5 2 3 4 7 11\n3 3 6 8\n6 2 4 5 6 8 9\n3 2 3 5",
"output": "-1"
},
{
"input": "11 11\n3 2 1 7 8 4 10 11 9 6 5\n3 2 7 11\n0\n0\n1 11\n1 1\n1 8\n2 4 5\n0\n1 4\n0\n0",
"output": "-1"
},
{
"input": "11 7\n11 2 1 7 9 8 6\n0\n7 3 4 5 6 8 10 11\n3 1 5 8\n1 11\n3 1 7 8\n7 1 3 4 5 7 8 10\n3 4 6 8\n1 5\n2 8 10\n4 1 4 5 7\n5 1 4 6 8 10",
"output": "-1"
},
{
"input": "11 6\n7 1 10 3 2 11\n0\n1 11\n0\n0\n1 9\n1 5\n0\n0\n0\n0\n0",
"output": "6\n1 11 2 3 7 10 "
},
{
"input": "11 7\n6 9 7 3 4 10 11\n4 3 6 8 11\n3 3 5 9\n2 6 7\n1 6\n1 4\n0\n0\n2 7 9\n0\n2 4 11\n3 6 7 9",
"output": "7\n6 7 3 4 9 11 10 "
},
{
"input": "11 5\n10 11 8 2 7\n1 9\n1 3\n0\n1 6\n1 1\n0\n0\n1 2\n2 4 8\n0\n0",
"output": "6\n3 2 7 8 10 11 "
},
{
"input": "11 6\n6 3 11 1 9 4\n6 2 3 6 7 8 9\n4 5 6 8 10\n4 1 2 6 8\n7 1 3 5 6 7 9 11\n4 3 6 7 8\n1 8\n2 3 9\n0\n0\n5 1 5 7 8 9\n5 1 2 3 7 8",
"output": "-1"
},
{
"input": "11 6\n4 2 9 7 3 1\n1 11\n0\n1 10\n1 11\n3 7 8 10\n1 11\n1 11\n1 11\n0\n1 2\n1 2",
"output": "8\n2 11 1 10 3 4 7 9 "
},
{
"input": "11 5\n3 2 5 7 6\n4 3 5 7 9\n2 7 9\n3 7 9 11\n5 5 6 7 9 10\n3 7 9 11\n6 2 3 5 7 10 11\n0\n2 7 10\n0\n2 2 11\n2 7 9",
"output": "8\n7 9 2 11 3 5 10 6 "
},
{
"input": "11 11\n11 6 4 7 8 5 1 3 2 9 10\n5 3 4 7 9 11\n0\n1 2\n1 3\n2 3 4\n6 1 3 4 8 10 11\n1 3\n2 2 4\n3 2 4 11\n5 4 5 7 9 11\n4 2 3 4 7",
"output": "11\n2 3 4 7 11 9 1 5 8 10 6 "
},
{
"input": "11 6\n7 1 6 4 3 8\n0\n0\n1 2\n1 1\n0\n0\n1 8\n0\n0\n1 1\n0",
"output": "7\n1 2 3 4 6 8 7 "
},
{
"input": "11 3\n9 11 5\n0\n0\n0\n0\n1 8\n0\n2 1 11\n0\n1 2\n0\n0",
"output": "5\n2 8 5 9 11 "
},
{
"input": "11 11\n5 4 2 1 6 10 3 7 11 8 9\n0\n1 3\n0\n0\n0\n2 9 11\n1 9\n0\n0\n0\n0",
"output": "11\n1 3 2 4 5 9 11 6 7 8 10 "
},
{
"input": "11 10\n9 6 10 3 2 8 4 7 11 5\n1 2\n0\n5 1 8 9 10 11\n4 1 7 8 11\n3 2 7 11\n3 1 7 10\n0\n2 6 11\n6 1 2 6 7 10 11\n2 1 11\n2 1 7",
"output": "11\n2 1 7 11 10 6 8 9 3 4 5 "
},
{
"input": "11 10\n5 8 7 6 1 4 9 3 2 11\n3 3 8 10\n2 4 8\n1 5\n2 1 11\n1 4\n3 4 8 9\n2 3 11\n1 5\n3 1 5 8\n2 3 5\n0",
"output": "-1"
},
{
"input": "12 9\n9 2 5 7 6 1 10 12 11\n0\n3 6 7 12\n1 4\n1 7\n1 3\n1 1\n0\n0\n2 1 4\n1 3\n0\n2 2 10",
"output": "-1"
},
{
"input": "12 10\n2 6 1 5 7 9 10 8 12 3\n1 10\n1 9\n1 11\n0\n1 10\n0\n1 3\n1 7\n1 6\n1 11\n0\n0",
"output": "11\n11 10 1 6 9 2 3 5 7 8 12 "
},
{
"input": "12 10\n9 11 3 6 4 12 2 7 10 8\n1 7\n3 7 8 9\n3 1 8 11\n4 1 7 9 10\n1 4\n1 12\n1 2\n1 2\n0\n2 1 9\n1 7\n1 7",
"output": "-1"
},
{
"input": "12 3\n8 10 11\n4 2 5 6 7\n5 4 7 8 10 11\n6 2 4 5 6 8 10\n2 6 8\n0\n3 5 7 8\n0\n2 3 7\n8 2 4 5 6 8 10 11 12\n2 4 7\n6 2 3 5 6 7 12\n5 1 3 6 7 8",
"output": "-1"
},
{
"input": "12 1\n8\n2 2 4\n1 9\n1 10\n1 12\n4 6 10 11 12\n0\n0\n1 9\n0\n1 8\n0\n0",
"output": "2\n9 8 "
},
{
"input": "12 10\n4 10 9 6 7 2 1 11 3 8\n1 4\n0\n7 2 4 5 6 7 8 11\n3 1 10 11\n3 4 8 12\n6 4 7 8 10 11 12\n2 2 11\n1 11\n6 3 4 8 10 11 12\n1 12\n1 1\n0",
"output": "-1"
},
{
"input": "12 3\n4 7 8\n2 11 12\n0\n0\n2 3 9\n3 7 11 12\n5 1 3 7 8 10\n1 3\n0\n2 2 8\n1 11\n0\n2 8 11",
"output": "6\n2 3 8 9 4 7 "
},
{
"input": "12 9\n2 10 6 3 4 12 7 1 5\n0\n0\n0\n1 8\n0\n1 8\n0\n1 3\n0\n0\n0\n1 8",
"output": "10\n1 2 3 8 4 5 6 7 10 12 "
},
{
"input": "12 1\n10\n0\n1 12\n2 2 9\n0\n2 1 2\n3 1 7 8\n3 8 9 10\n0\n0\n3 5 11 12\n0\n0",
"output": "6\n1 12 2 5 11 10 "
},
{
"input": "12 4\n5 1 7 3\n0\n3 4 5 12\n0\n1 10\n1 12\n1 9\n3 3 4 9\n1 1\n1 11\n1 5\n2 1 4\n0",
"output": "9\n1 3 12 5 10 4 11 9 7 "
},
{
"input": "12 2\n11 4\n0\n0\n0\n1 5\n0\n0\n0\n0\n1 2\n0\n0\n0",
"output": "3\n5 4 11 "
},
{
"input": "12 2\n6 8\n6 2 4 5 7 9 11\n4 8 9 11 12\n0\n2 8 9\n2 8 12\n4 2 3 5 9\n2 9 12\n0\n0\n4 3 4 7 9\n2 7 8\n0",
"output": "9\n8 9 12 7 11 2 3 5 6 "
},
{
"input": "12 10\n8 7 9 5 10 6 4 12 3 11\n1 5\n1 10\n1 1\n1 5\n1 7\n1 11\n1 10\n2 1 3\n0\n1 1\n1 8\n0",
"output": "-1"
},
{
"input": "12 1\n4\n2 4 11\n1 8\n2 2 5\n0\n0\n1 3\n0\n0\n1 2\n1 9\n2 2 6\n0",
"output": "1\n4 "
},
{
"input": "12 2\n10 5\n0\n0\n3 1 5 11\n1 3\n0\n1 1\n2 5 9\n2 5 7\n1 8\n2 6 9\n0\n1 1",
"output": "-1"
}
] | 46 | 307,200 | 0 | 6,185 |
|
744 | Hongcow Masters the Cyclic Shift | [
"strings",
"two pointers"
] | null | null | Hongcow's teacher heard that Hongcow had learned about the cyclic shift, and decided to set the following problem for him.
You are given a list of *n* strings *s*1,<=*s*2,<=...,<=*s**n* contained in the list *A*.
A list *X* of strings is called stable if the following condition holds.
First, a message is defined as a concatenation of some elements of the list *X*. You can use an arbitrary element as many times as you want, and you may concatenate these elements in any arbitrary order. Let *S**X* denote the set of of all messages you can construct from the list. Of course, this set has infinite size if your list is nonempty.
Call a single message good if the following conditions hold:
- Suppose the message is the concatenation of *k* strings *w*1,<=*w*2,<=...,<=*w**k*, where each *w**i* is an element of *X*. - Consider the |*w*1|<=+<=|*w*2|<=+<=...<=+<=|*w**k*| cyclic shifts of the string. Let *m* be the number of these cyclic shifts of the string that are elements of *S**X*. - A message is good if and only if *m* is exactly equal to *k*.
The list *X* is called stable if and only if every element of *S**X* is good.
Let *f*(*L*) be 1 if *L* is a stable list, and 0 otherwise.
Find the sum of *f*(*L*) where *L* is a nonempty contiguous sublist of *A* (there are contiguous sublists in total). | The first line of input will contain a single integer *n* (1<=β€<=*n*<=β€<=30), denoting the number of strings in the list.
The next *n* lines will each contain a string *s**i* (). | Print a single integer, the number of nonempty contiguous sublists that are stable. | [
"4\na\nab\nb\nbba\n",
"5\nhh\nee\nll\nll\noo\n",
"6\naab\nab\nbba\nb\nab\nc\n"
] | [
"7\n",
"0\n",
"13\n"
] | For the first sample, there are 10 sublists to consider. Sublists ["a", "ab", "b"], ["ab", "b", "bba"], and ["a", "ab", "b", "bba"] are not stable. The other seven sublists are stable.
For example, *X* = ["a", "ab", "b"] is not stable, since the message "ab" + "ab" = "abab" has four cyclic shifts ["abab", "baba", "abab", "baba"], which are all elements of *S*<sub class="lower-index">*X*</sub>. | [] | 46 | 4,608,000 | -1 | 6,221 |
|
338 | GCD Table | [
"chinese remainder theorem",
"math",
"number theory"
] | null | null | Consider a table *G* of size *n*<=Γ<=*m* such that *G*(*i*,<=*j*)<==<=*GCD*(*i*,<=*j*) for all 1<=β€<=*i*<=β€<=*n*,<=1<=β€<=*j*<=β€<=*m*. *GCD*(*a*,<=*b*) is the greatest common divisor of numbers *a* and *b*.
You have a sequence of positive integer numbers *a*1,<=*a*2,<=...,<=*a**k*. We say that this sequence occurs in table *G* if it coincides with consecutive elements in some row, starting from some position. More formally, such numbers 1<=β€<=*i*<=β€<=*n* and 1<=β€<=*j*<=β€<=*m*<=-<=*k*<=+<=1 should exist that *G*(*i*,<=*j*<=+<=*l*<=-<=1)<==<=*a**l* for all 1<=β€<=*l*<=β€<=*k*.
Determine if the sequence *a* occurs in table *G*. | The first line contains three space-separated integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=1012; 1<=β€<=*k*<=β€<=10000). The second line contains *k* space-separated integers *a*1,<=*a*2,<=...,<=*a**k* (1<=β€<=*a**i*<=β€<=1012). | Print a single word "YES", if the given sequence occurs in table *G*, otherwise print "NO". | [
"100 100 5\n5 2 1 2 1\n",
"100 8 5\n5 2 1 2 1\n",
"100 100 7\n1 2 3 4 5 6 7\n"
] | [
"YES\n",
"NO\n",
"NO\n"
] | Sample 1. The tenth row of table *G* starts from sequence {1, 2, 1, 2, 5, 2, 1, 2, 1, 10}. As you can see, elements from fifth to ninth coincide with sequence *a*.
Sample 2. This time the width of table *G* equals 8. Sequence *a* doesn't occur there. | [
{
"input": "100 100 5\n5 2 1 2 1",
"output": "YES"
},
{
"input": "100 8 5\n5 2 1 2 1",
"output": "NO"
},
{
"input": "100 100 7\n1 2 3 4 5 6 7",
"output": "NO"
},
{
"input": "5 5 5\n1 1 1 1 1",
"output": "YES"
},
{
"input": "11 10 1\n11",
"output": "NO"
},
{
"input": "108 942 35\n1 1 3 1 1 3 1 1 3 1 1 3 1 1 3 1 1 3 1 1 3 1 1 3 31 1 3 1 1 3 1 1 3 1 1",
"output": "YES"
},
{
"input": "1000000000000 1000000000000 116\n1587924000 7 2 3 4 5 6 1 56 9 10 1 12 13 2 105 16 1 18 1 620 3 14 1 24 25 26 27 4 203 30 1 32 3 2 5 252 1 2 39 40 1 6 7 4 45 2 1 48 1 350 93 52 1 54 5 8 21 58 1 60 1 2 9 224 65 6 1 4 3 10 7 72 1 2 75 4 1 546 1 80 81 62 1 12 35 2 87 8 1 90 13 28 3 2 5 96 1 2 63 100 1 6 1 104 15 14 1 108 1 10 3 16 217 6 5",
"output": "YES"
},
{
"input": "1000000000000 1000000000000 10\n99991 99992 99993 99994 99995 99996 99997 99998 99999 31000000000",
"output": "NO"
},
{
"input": "100 100 10\n3 5 1 1 1 1 1 1 1 9",
"output": "NO"
},
{
"input": "54275126675 128566125 50\n1 1 3 1 1 3 7 1 9 1 11 3 13 7 3 1 1 27 1 1 21 11 1 3 1 13 9 7 1 3 1 1 33 1 7 9 37 1 39 1 1 21 1 11 27 1 1 3 7 1",
"output": "YES"
},
{
"input": "100000 49999 2\n50000 1",
"output": "NO"
},
{
"input": "1000000000000 1000000000000 59\n1 1 3 1 5 3 1 1 3 5 1 3 1 1 15 1 1 3 1 5 3 1 1 3 5 1 3 1 1 15 1 1 3 1 5 3 1 1 3 5 1 3 1 1 15 1 1 3 1 5 3 1 1 3 5 1 3 1 1",
"output": "YES"
},
{
"input": "1000000000000 1000000000000 6\n8 21 2 1 12 1",
"output": "YES"
},
{
"input": "1000000000000 1000000000000 6\n1 6 5 2 3 2",
"output": "YES"
},
{
"input": "1000000000000 1000000000000 100\n2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 74 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 111 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1",
"output": "YES"
},
{
"input": "1000000000000 1000000000000 100\n2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 74 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 12 1 2 3 2 1 18 1 2 111 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1",
"output": "NO"
},
{
"input": "1000000000000 1000000000000 100\n2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 74 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 111 2 1 6 1 2 9 2 1 6 1 2 3 2 1 9 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1",
"output": "NO"
},
{
"input": "1000000000000 1000000000000 40\n2 1 8 1 10 1 4 1 2 25 16 1 2 1 20 1 2 1 8 5 2 1 4 1 10 1 128 1 2 5 4 1 2 1 1000 1 2 1 4 5",
"output": "YES"
},
{
"input": "1000000000000 1000000000000 40\n2 1 8 1 10 1 4 1 2 5 16 1 2 1 20 1 2 1 8 5 2 1 4 1 10 1 64 1 2 5 4 1 2 1 500 1 2 1 4 5",
"output": "NO"
},
{
"input": "1000000000000 1000000000000 2\n1 1000000000000",
"output": "YES"
},
{
"input": "1000000000000 1000000000000 4\n1 2 1 100000000000",
"output": "YES"
},
{
"input": "991234567890 927215128595 5\n6 11 8 3 2000000014",
"output": "YES"
},
{
"input": "991234567890 182000001269 5\n6 11 8 3 2000000014",
"output": "NO"
},
{
"input": "999999999999 999999999999 2\n20145182300 20145182301",
"output": "NO"
}
] | 186 | 921,600 | 3 | 6,250 |
|
476 | Dreamoon and Sets | [
"constructive algorithms",
"greedy",
"math"
] | null | null | Dreamoon likes to play with sets, integers and . is defined as the largest positive integer that divides both *a* and *b*.
Let *S* be a set of exactly four distinct integers greater than 0. Define *S* to be of rank *k* if and only if for all pairs of distinct elements *s**i*, *s**j* from *S*, .
Given *k* and *n*, Dreamoon wants to make up *n* sets of rank *k* using integers from 1 to *m* such that no integer is used in two different sets (of course you can leave some integers without use). Calculate the minimum *m* that makes it possible and print one possible solution. | The single line of the input contains two space separated integers *n*, *k* (1<=β€<=*n*<=β€<=10<=000,<=1<=β€<=*k*<=β€<=100). | On the first line print a single integer β the minimal possible *m*.
On each of the next *n* lines print four space separated integers representing the *i*-th set.
Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal *m*, print any one of them. | [
"1 1\n",
"2 2\n"
] | [
"5\n1 2 3 5\n",
"22\n2 4 6 22\n14 18 10 16\n"
] | For the first example it's easy to see that set {1,β2,β3,β4} isn't a valid set of rank 1 since <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e2af04e5e60e1fe79a4d74bf22dfa575f0b0f7bb.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "1 1",
"output": "5\n1 3 4 5"
},
{
"input": "2 2",
"output": "22\n2 6 8 10\n14 18 20 22"
},
{
"input": "7 7",
"output": "287\n7 21 28 35\n49 63 70 77\n91 105 112 119\n133 147 154 161\n175 189 196 203\n217 231 238 245\n259 273 280 287"
},
{
"input": "13 7",
"output": "539\n7 21 28 35\n49 63 70 77\n91 105 112 119\n133 147 154 161\n175 189 196 203\n217 231 238 245\n259 273 280 287\n301 315 322 329\n343 357 364 371\n385 399 406 413\n427 441 448 455\n469 483 490 497\n511 525 532 539"
},
{
"input": "15 27",
"output": "2403\n27 81 108 135\n189 243 270 297\n351 405 432 459\n513 567 594 621\n675 729 756 783\n837 891 918 945\n999 1053 1080 1107\n1161 1215 1242 1269\n1323 1377 1404 1431\n1485 1539 1566 1593\n1647 1701 1728 1755\n1809 1863 1890 1917\n1971 2025 2052 2079\n2133 2187 2214 2241\n2295 2349 2376 2403"
},
{
"input": "19 21",
"output": "2373\n21 63 84 105\n147 189 210 231\n273 315 336 357\n399 441 462 483\n525 567 588 609\n651 693 714 735\n777 819 840 861\n903 945 966 987\n1029 1071 1092 1113\n1155 1197 1218 1239\n1281 1323 1344 1365\n1407 1449 1470 1491\n1533 1575 1596 1617\n1659 1701 1722 1743\n1785 1827 1848 1869\n1911 1953 1974 1995\n2037 2079 2100 2121\n2163 2205 2226 2247\n2289 2331 2352 2373"
},
{
"input": "113 97",
"output": "65669\n97 291 388 485\n679 873 970 1067\n1261 1455 1552 1649\n1843 2037 2134 2231\n2425 2619 2716 2813\n3007 3201 3298 3395\n3589 3783 3880 3977\n4171 4365 4462 4559\n4753 4947 5044 5141\n5335 5529 5626 5723\n5917 6111 6208 6305\n6499 6693 6790 6887\n7081 7275 7372 7469\n7663 7857 7954 8051\n8245 8439 8536 8633\n8827 9021 9118 9215\n9409 9603 9700 9797\n9991 10185 10282 10379\n10573 10767 10864 10961\n11155 11349 11446 11543\n11737 11931 12028 12125\n12319 12513 12610 12707\n12901 13095 13192 13289\n13483 ..."
},
{
"input": "10000 100",
"output": "5999900\n100 300 400 500\n700 900 1000 1100\n1300 1500 1600 1700\n1900 2100 2200 2300\n2500 2700 2800 2900\n3100 3300 3400 3500\n3700 3900 4000 4100\n4300 4500 4600 4700\n4900 5100 5200 5300\n5500 5700 5800 5900\n6100 6300 6400 6500\n6700 6900 7000 7100\n7300 7500 7600 7700\n7900 8100 8200 8300\n8500 8700 8800 8900\n9100 9300 9400 9500\n9700 9900 10000 10100\n10300 10500 10600 10700\n10900 11100 11200 11300\n11500 11700 11800 11900\n12100 12300 12400 12500\n12700 12900 13000 13100\n13300 13500 13600 13700\n..."
},
{
"input": "10000 1",
"output": "59999\n1 3 4 5\n7 9 10 11\n13 15 16 17\n19 21 22 23\n25 27 28 29\n31 33 34 35\n37 39 40 41\n43 45 46 47\n49 51 52 53\n55 57 58 59\n61 63 64 65\n67 69 70 71\n73 75 76 77\n79 81 82 83\n85 87 88 89\n91 93 94 95\n97 99 100 101\n103 105 106 107\n109 111 112 113\n115 117 118 119\n121 123 124 125\n127 129 130 131\n133 135 136 137\n139 141 142 143\n145 147 148 149\n151 153 154 155\n157 159 160 161\n163 165 166 167\n169 171 172 173\n175 177 178 179\n181 183 184 185\n187 189 190 191\n193 195 196 197\n199 201 202 203..."
},
{
"input": "1 100",
"output": "500\n100 300 400 500"
},
{
"input": "9252 39",
"output": "2164929\n39 117 156 195\n273 351 390 429\n507 585 624 663\n741 819 858 897\n975 1053 1092 1131\n1209 1287 1326 1365\n1443 1521 1560 1599\n1677 1755 1794 1833\n1911 1989 2028 2067\n2145 2223 2262 2301\n2379 2457 2496 2535\n2613 2691 2730 2769\n2847 2925 2964 3003\n3081 3159 3198 3237\n3315 3393 3432 3471\n3549 3627 3666 3705\n3783 3861 3900 3939\n4017 4095 4134 4173\n4251 4329 4368 4407\n4485 4563 4602 4641\n4719 4797 4836 4875\n4953 5031 5070 5109\n5187 5265 5304 5343\n5421 5499 5538 5577\n5655 5733 5772 5..."
},
{
"input": "8096 59",
"output": "2865925\n59 177 236 295\n413 531 590 649\n767 885 944 1003\n1121 1239 1298 1357\n1475 1593 1652 1711\n1829 1947 2006 2065\n2183 2301 2360 2419\n2537 2655 2714 2773\n2891 3009 3068 3127\n3245 3363 3422 3481\n3599 3717 3776 3835\n3953 4071 4130 4189\n4307 4425 4484 4543\n4661 4779 4838 4897\n5015 5133 5192 5251\n5369 5487 5546 5605\n5723 5841 5900 5959\n6077 6195 6254 6313\n6431 6549 6608 6667\n6785 6903 6962 7021\n7139 7257 7316 7375\n7493 7611 7670 7729\n7847 7965 8024 8083\n8201 8319 8378 8437\n8555 8673 ..."
},
{
"input": "4237 87",
"output": "2211627\n87 261 348 435\n609 783 870 957\n1131 1305 1392 1479\n1653 1827 1914 2001\n2175 2349 2436 2523\n2697 2871 2958 3045\n3219 3393 3480 3567\n3741 3915 4002 4089\n4263 4437 4524 4611\n4785 4959 5046 5133\n5307 5481 5568 5655\n5829 6003 6090 6177\n6351 6525 6612 6699\n6873 7047 7134 7221\n7395 7569 7656 7743\n7917 8091 8178 8265\n8439 8613 8700 8787\n8961 9135 9222 9309\n9483 9657 9744 9831\n10005 10179 10266 10353\n10527 10701 10788 10875\n11049 11223 11310 11397\n11571 11745 11832 11919\n12093 12267 ..."
},
{
"input": "3081 11",
"output": "203335\n11 33 44 55\n77 99 110 121\n143 165 176 187\n209 231 242 253\n275 297 308 319\n341 363 374 385\n407 429 440 451\n473 495 506 517\n539 561 572 583\n605 627 638 649\n671 693 704 715\n737 759 770 781\n803 825 836 847\n869 891 902 913\n935 957 968 979\n1001 1023 1034 1045\n1067 1089 1100 1111\n1133 1155 1166 1177\n1199 1221 1232 1243\n1265 1287 1298 1309\n1331 1353 1364 1375\n1397 1419 1430 1441\n1463 1485 1496 1507\n1529 1551 1562 1573\n1595 1617 1628 1639\n1661 1683 1694 1705\n1727 1749 1760 1771\n17..."
},
{
"input": "9221 39",
"output": "2157675\n39 117 156 195\n273 351 390 429\n507 585 624 663\n741 819 858 897\n975 1053 1092 1131\n1209 1287 1326 1365\n1443 1521 1560 1599\n1677 1755 1794 1833\n1911 1989 2028 2067\n2145 2223 2262 2301\n2379 2457 2496 2535\n2613 2691 2730 2769\n2847 2925 2964 3003\n3081 3159 3198 3237\n3315 3393 3432 3471\n3549 3627 3666 3705\n3783 3861 3900 3939\n4017 4095 4134 4173\n4251 4329 4368 4407\n4485 4563 4602 4641\n4719 4797 4836 4875\n4953 5031 5070 5109\n5187 5265 5304 5343\n5421 5499 5538 5577\n5655 5733 5772 5..."
},
{
"input": "770 59",
"output": "272521\n59 177 236 295\n413 531 590 649\n767 885 944 1003\n1121 1239 1298 1357\n1475 1593 1652 1711\n1829 1947 2006 2065\n2183 2301 2360 2419\n2537 2655 2714 2773\n2891 3009 3068 3127\n3245 3363 3422 3481\n3599 3717 3776 3835\n3953 4071 4130 4189\n4307 4425 4484 4543\n4661 4779 4838 4897\n5015 5133 5192 5251\n5369 5487 5546 5605\n5723 5841 5900 5959\n6077 6195 6254 6313\n6431 6549 6608 6667\n6785 6903 6962 7021\n7139 7257 7316 7375\n7493 7611 7670 7729\n7847 7965 8024 8083\n8201 8319 8378 8437\n8555 8673 8..."
},
{
"input": "5422 87",
"output": "2830197\n87 261 348 435\n609 783 870 957\n1131 1305 1392 1479\n1653 1827 1914 2001\n2175 2349 2436 2523\n2697 2871 2958 3045\n3219 3393 3480 3567\n3741 3915 4002 4089\n4263 4437 4524 4611\n4785 4959 5046 5133\n5307 5481 5568 5655\n5829 6003 6090 6177\n6351 6525 6612 6699\n6873 7047 7134 7221\n7395 7569 7656 7743\n7917 8091 8178 8265\n8439 8613 8700 8787\n8961 9135 9222 9309\n9483 9657 9744 9831\n10005 10179 10266 10353\n10527 10701 10788 10875\n11049 11223 11310 11397\n11571 11745 11832 11919\n12093 12267 ..."
},
{
"input": "1563 15",
"output": "140655\n15 45 60 75\n105 135 150 165\n195 225 240 255\n285 315 330 345\n375 405 420 435\n465 495 510 525\n555 585 600 615\n645 675 690 705\n735 765 780 795\n825 855 870 885\n915 945 960 975\n1005 1035 1050 1065\n1095 1125 1140 1155\n1185 1215 1230 1245\n1275 1305 1320 1335\n1365 1395 1410 1425\n1455 1485 1500 1515\n1545 1575 1590 1605\n1635 1665 1680 1695\n1725 1755 1770 1785\n1815 1845 1860 1875\n1905 1935 1950 1965\n1995 2025 2040 2055\n2085 2115 2130 2145\n2175 2205 2220 2235\n2265 2295 2310 2325\n2355 ..."
},
{
"input": "407 39",
"output": "95199\n39 117 156 195\n273 351 390 429\n507 585 624 663\n741 819 858 897\n975 1053 1092 1131\n1209 1287 1326 1365\n1443 1521 1560 1599\n1677 1755 1794 1833\n1911 1989 2028 2067\n2145 2223 2262 2301\n2379 2457 2496 2535\n2613 2691 2730 2769\n2847 2925 2964 3003\n3081 3159 3198 3237\n3315 3393 3432 3471\n3549 3627 3666 3705\n3783 3861 3900 3939\n4017 4095 4134 4173\n4251 4329 4368 4407\n4485 4563 4602 4641\n4719 4797 4836 4875\n4953 5031 5070 5109\n5187 5265 5304 5343\n5421 5499 5538 5577\n5655 5733 5772 581..."
},
{
"input": "6518 18",
"output": "703926\n18 54 72 90\n126 162 180 198\n234 270 288 306\n342 378 396 414\n450 486 504 522\n558 594 612 630\n666 702 720 738\n774 810 828 846\n882 918 936 954\n990 1026 1044 1062\n1098 1134 1152 1170\n1206 1242 1260 1278\n1314 1350 1368 1386\n1422 1458 1476 1494\n1530 1566 1584 1602\n1638 1674 1692 1710\n1746 1782 1800 1818\n1854 1890 1908 1926\n1962 1998 2016 2034\n2070 2106 2124 2142\n2178 2214 2232 2250\n2286 2322 2340 2358\n2394 2430 2448 2466\n2502 2538 2556 2574\n2610 2646 2664 2682\n2718 2754 2772 2790..."
},
{
"input": "1171 46",
"output": "323150\n46 138 184 230\n322 414 460 506\n598 690 736 782\n874 966 1012 1058\n1150 1242 1288 1334\n1426 1518 1564 1610\n1702 1794 1840 1886\n1978 2070 2116 2162\n2254 2346 2392 2438\n2530 2622 2668 2714\n2806 2898 2944 2990\n3082 3174 3220 3266\n3358 3450 3496 3542\n3634 3726 3772 3818\n3910 4002 4048 4094\n4186 4278 4324 4370\n4462 4554 4600 4646\n4738 4830 4876 4922\n5014 5106 5152 5198\n5290 5382 5428 5474\n5566 5658 5704 5750\n5842 5934 5980 6026\n6118 6210 6256 6302\n6394 6486 6532 6578\n6670 6762 6808..."
},
{
"input": "7311 70",
"output": "3070550\n70 210 280 350\n490 630 700 770\n910 1050 1120 1190\n1330 1470 1540 1610\n1750 1890 1960 2030\n2170 2310 2380 2450\n2590 2730 2800 2870\n3010 3150 3220 3290\n3430 3570 3640 3710\n3850 3990 4060 4130\n4270 4410 4480 4550\n4690 4830 4900 4970\n5110 5250 5320 5390\n5530 5670 5740 5810\n5950 6090 6160 6230\n6370 6510 6580 6650\n6790 6930 7000 7070\n7210 7350 7420 7490\n7630 7770 7840 7910\n8050 8190 8260 8330\n8470 8610 8680 8750\n8890 9030 9100 9170\n9310 9450 9520 9590\n9730 9870 9940 10010\n10150 1..."
},
{
"input": "6155 94",
"output": "3471326\n94 282 376 470\n658 846 940 1034\n1222 1410 1504 1598\n1786 1974 2068 2162\n2350 2538 2632 2726\n2914 3102 3196 3290\n3478 3666 3760 3854\n4042 4230 4324 4418\n4606 4794 4888 4982\n5170 5358 5452 5546\n5734 5922 6016 6110\n6298 6486 6580 6674\n6862 7050 7144 7238\n7426 7614 7708 7802\n7990 8178 8272 8366\n8554 8742 8836 8930\n9118 9306 9400 9494\n9682 9870 9964 10058\n10246 10434 10528 10622\n10810 10998 11092 11186\n11374 11562 11656 11750\n11938 12126 12220 12314\n12502 12690 12784 12878\n13066 ..."
},
{
"input": "7704 18",
"output": "832014\n18 54 72 90\n126 162 180 198\n234 270 288 306\n342 378 396 414\n450 486 504 522\n558 594 612 630\n666 702 720 738\n774 810 828 846\n882 918 936 954\n990 1026 1044 1062\n1098 1134 1152 1170\n1206 1242 1260 1278\n1314 1350 1368 1386\n1422 1458 1476 1494\n1530 1566 1584 1602\n1638 1674 1692 1710\n1746 1782 1800 1818\n1854 1890 1908 1926\n1962 1998 2016 2034\n2070 2106 2124 2142\n2178 2214 2232 2250\n2286 2322 2340 2358\n2394 2430 2448 2466\n2502 2538 2556 2574\n2610 2646 2664 2682\n2718 2754 2772 2790..."
},
{
"input": "3844 46",
"output": "1060898\n46 138 184 230\n322 414 460 506\n598 690 736 782\n874 966 1012 1058\n1150 1242 1288 1334\n1426 1518 1564 1610\n1702 1794 1840 1886\n1978 2070 2116 2162\n2254 2346 2392 2438\n2530 2622 2668 2714\n2806 2898 2944 2990\n3082 3174 3220 3266\n3358 3450 3496 3542\n3634 3726 3772 3818\n3910 4002 4048 4094\n4186 4278 4324 4370\n4462 4554 4600 4646\n4738 4830 4876 4922\n5014 5106 5152 5198\n5290 5382 5428 5474\n5566 5658 5704 5750\n5842 5934 5980 6026\n6118 6210 6256 6302\n6394 6486 6532 6578\n6670 6762 680..."
},
{
"input": "1 10",
"output": "50\n10 30 40 50"
}
] | 31 | 0 | -1 | 6,270 |
|
859 | Declined Finalists | [
"greedy",
"implementation"
] | null | null | This year, as in previous years, MemSQL is inviting the top 25 competitors from the Start[c]up qualification round to compete onsite for the final round. Not everyone who is eligible to compete onsite can afford to travel to the office, though. Initially the top 25 contestants are invited to come onsite. Each eligible contestant must either accept or decline the invitation. Whenever a contestant declines, the highest ranked contestant not yet invited is invited to take the place of the one that declined. This continues until 25 contestants have accepted invitations.
After the qualifying round completes, you know *K* of the onsite finalists, as well as their qualifying ranks (which start at 1, there are no ties). Determine the minimum possible number of contestants that declined the invitation to compete onsite in the final round. | The first line of input contains *K* (1<=β€<=*K*<=β€<=25), the number of onsite finalists you know. The second line of input contains *r*1,<=*r*2,<=...,<=*r**K* (1<=β€<=*r**i*<=β€<=106), the qualifying ranks of the finalists you know. All these ranks are distinct. | Print the minimum possible number of contestants that declined the invitation to compete onsite. | [
"25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28\n",
"5\n16 23 8 15 4\n",
"3\n14 15 92\n"
] | [
"3\n",
"0\n",
"67\n"
] | In the first example, you know all 25 onsite finalists. The contestants who ranked 1-st, 13-th, and 27-th must have declined, so the answer is 3. | [
{
"input": "25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28",
"output": "3"
},
{
"input": "5\n16 23 8 15 4",
"output": "0"
},
{
"input": "3\n14 15 92",
"output": "67"
},
{
"input": "1\n1000000",
"output": "999975"
},
{
"input": "25\n1000000 999999 999998 999997 999996 999995 999994 999993 999992 999991 999990 999989 999988 999987 999986 999985 999984 999983 999982 999981 999980 999979 999978 999977 999976",
"output": "999975"
},
{
"input": "25\n13 15 24 2 21 18 9 4 16 6 10 25 20 11 23 17 8 3 1 12 5 19 22 14 7",
"output": "0"
},
{
"input": "10\n17 11 7 13 18 12 14 5 16 2",
"output": "0"
},
{
"input": "22\n22 14 23 20 11 21 4 12 3 8 7 9 19 10 13 17 15 1 5 18 16 2",
"output": "0"
},
{
"input": "21\n6 21 24 3 10 23 14 2 26 12 8 1 15 13 9 5 19 20 4 16 22",
"output": "1"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "2\n100 60",
"output": "75"
},
{
"input": "4\n999 581 787 236",
"output": "974"
},
{
"input": "6\n198 397 732 1234 309 827",
"output": "1209"
},
{
"input": "11\n6494 3961 1858 4351 8056 780 7720 6211 1961 8192 3621",
"output": "8167"
},
{
"input": "14\n18809 9534 11652 6493 8929 9370 4125 23888 16403 3559 23649 19243 14289 17852",
"output": "23863"
},
{
"input": "18\n24939 35558 47058 70307 26221 12866 3453 40422 47557 36322 40698 64060 10825 77777 48645 26124 4859 64222",
"output": "77752"
},
{
"input": "24\n633483 654321 122445 481150 347578 37803 525083 151084 211073 358699 339420 452023 219553 119727 74852 66750 371279 405099 618894 649977 235337 607819 81649 649804",
"output": "654296"
},
{
"input": "25\n58115 794098 753382 484882 238434 674285 690118 858677 196185 173301 349729 918792 600745 636016 122678 366783 137179 377098 917081 369620 449039 379412 503678 1000000 292099",
"output": "999975"
},
{
"input": "2\n26 27",
"output": "2"
},
{
"input": "3\n40 30 35",
"output": "15"
},
{
"input": "2\n46 45",
"output": "21"
},
{
"input": "3\n1 25 90",
"output": "65"
},
{
"input": "5\n14 15 16 30 92",
"output": "67"
},
{
"input": "2\n1000 1001",
"output": "976"
},
{
"input": "25\n3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28 2",
"output": "3"
}
] | 46 | 0 | 3 | 6,278 |
|
3 | Least Cost Bracket Sequence | [
"greedy"
] | D. Least Cost Bracket Sequence | 1 | 64 | This is yet another problem on regular bracket sequences.
A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequence that consists of characters "(", ")" and "?". You have to replace each character "?" with a bracket so, that you get a regular bracket sequence.
For each character "?" the cost of its replacement with "(" and ")" is given. Among all the possible variants your should choose the cheapest. | The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5Β·104. Then there follow *m* lines, where *m* is the number of characters "?" in the pattern. Each line contains two integer numbers *a**i* and *b**i* (1<=β€<=*a**i*,<=<=*b**i*<=β€<=106), where *a**i* is the cost of replacing the *i*-th character "?" with an opening bracket, and *b**i* β with a closing one. | Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.
Print -1, if there is no answer. If the answer is not unique, print any of them. | [
"(??)\n1 2\n2 8\n"
] | [
"4\n()()\n"
] | none | [
{
"input": "(??)\n1 2\n2 8",
"output": "4\n()()"
},
{
"input": "??\n1 1\n1 1",
"output": "2\n()"
},
{
"input": "(???\n1 1\n1 1\n1 1",
"output": "3\n(())"
},
{
"input": "(??)\n2 1\n1 1",
"output": "2\n()()"
},
{
"input": "(???)?\n3 3\n3 1\n3 3\n2 3",
"output": "10\n(()())"
},
{
"input": "((????\n3 2\n3 2\n1 1\n2 3",
"output": "8\n(())()"
},
{
"input": "???())\n2 4\n3 3\n4 1",
"output": "6\n(()())"
},
{
"input": "((????\n3 5\n4 1\n2 2\n1 5",
"output": "11\n((()))"
},
{
"input": "?(?)(???\n2 3\n2 2\n3 2\n3 1\n3 1",
"output": "8\n((()()))"
},
{
"input": "(??????)\n1 1\n3 3\n3 3\n3 2\n1 3\n3 3",
"output": "13\n((())())"
},
{
"input": "?????)??\n2 3\n2 1\n1 3\n5 1\n3 3\n1 3\n3 2",
"output": "11\n()()()()"
},
{
"input": "?)???(??\n1 4\n3 4\n2 4\n2 5\n3 3\n3 1",
"output": "14\n()()(())"
},
{
"input": "???(??))\n2 1\n2 1\n2 1\n1 2\n2 1",
"output": "7\n(()(()))"
},
{
"input": "??(()??)\n3 2\n3 3\n1 3\n2 2",
"output": "9\n()(()())"
},
{
"input": "????(???\n2 2\n1 3\n1 3\n3 3\n4 1\n4 4\n2 4",
"output": "16\n((()()))"
},
{
"input": "?(??????\n1 5\n2 4\n4 4\n4 3\n4 5\n5 4\n2 3",
"output": "21\n((())())"
},
{
"input": "???????)\n6 3\n5 3\n4 1\n1 4\n4 1\n2 6\n4 3",
"output": "19\n(()()())"
},
{
"input": "??????)?\n2 2\n4 2\n3 5\n3 2\n7 4\n6 2\n1 6",
"output": "24\n(((())))"
},
{
"input": "?((?)?)?\n1 2\n4 2\n1 3\n1 2",
"output": "6\n((())())"
},
{
"input": "??(????)\n3 2\n1 4\n4 4\n2 3\n2 3\n2 4",
"output": "16\n((()))()"
},
{
"input": "???(?)??(??)?)(?(?????????(?()????)(????(?)????)???)??))(?(?????????))???(??)?????))???????(????????\n9 10\n6 3\n8 2\n9 10\n9 3\n6 2\n8 5\n6 7\n2 6\n7 8\n6 10\n1 7\n1 7\n10 7\n10 7\n8 4\n5 9\n9 3\n3 10\n1 10\n8 2\n8 8\n4 8\n6 6\n4 10\n4 5\n5 2\n5 6\n7 7\n7 3\n10 1\n1 4\n5 10\n3 2\n2 8\n8 9\n6 5\n8 6\n3 4\n8 6\n8 5\n7 7\n10 9\n5 5\n2 1\n2 7\n2 3\n5 10\n9 7\n1 9\n10 9\n4 5\n8 2\n2 5\n6 7\n3 6\n4 2\n2 5\n3 9\n4 4\n6 3\n4 9\n3 1\n5 7\n8 7\n6 9\n5 3\n6 4\n8 3\n5 8\n8 4\n7 6\n1 4",
"output": "309\n(()(()))()()()(((((()))()(((())((()((()((()))(())(()))))((())))))((()))()(())((()())())()()(()))()))"
},
{
"input": "(?(((???))(??)?)?))))(?)????(()()???(?)????(??(??????)()(????(?)))))??(???(??)?(??)????????(????(?()\n39 78\n1 83\n2 35\n28 89\n53 53\n96 67\n16 46\n43 28\n25 73\n8 97\n57 41\n15 25\n47 49\n23 18\n97 77\n38 33\n68 80\n38 98\n62 8\n61 79\n84 50\n71 48\n12 16\n97 95\n16 70\n72 58\n55 85\n88 42\n49 56\n39 63\n51 100\n41 15\n97 17\n71 63\n21 44\n1 41\n22 14\n42 65\n88 33\n57 95\n57 28\n59 8\n56 42\n18 99\n43 6\n75 93\n34 23\n62 57\n62 71\n67 92\n91 60\n49 58\n97 14\n75 68\n20 9\n55 98\n12 3",
"output": "2140\n(((((((())(())())))))(()()(((()())))(()()()()(((()()()()((())())))))((()()(()))()())())(()(())))()()"
},
{
"input": "(())()",
"output": "0\n(())()"
},
{
"input": "?(?(??\n1 1\n2 2\n1 1\n1 1",
"output": "5\n(()())"
},
{
"input": "(????(\n1 1\n2 1\n2 1\n3 3",
"output": "-1"
},
{
"input": "(?(???\n2 3\n1 1\n3 3\n1 4",
"output": "10\n((()))"
},
{
"input": "))))))",
"output": "-1"
},
{
"input": ")?)??)\n4 4\n3 5\n3 6",
"output": "-1"
},
{
"input": "((((((",
"output": "-1"
},
{
"input": "((((((",
"output": "-1"
},
{
"input": "()()()",
"output": "0\n()()()"
},
{
"input": "????((\n7 6\n1 10\n9 8\n4 4",
"output": "-1"
},
{
"input": "))))))",
"output": "-1"
},
{
"input": "))))))",
"output": "-1"
},
{
"input": "((((((",
"output": "-1"
},
{
"input": "((()))",
"output": "0\n((()))"
},
{
"input": "?))?))\n9 13\n8 11",
"output": "-1"
},
{
"input": "))))))",
"output": "-1"
},
{
"input": "?(?)?)\n6 14\n8 6\n4 3",
"output": "16\n(())()"
},
{
"input": "?(?(((\n8 7\n17 15",
"output": "-1"
},
{
"input": "))))))",
"output": "-1"
}
] | 404 | 2,048,000 | 3.782741 | 6,284 |
9 | Hexadecimal's Numbers | [
"brute force",
"implementation",
"math"
] | C. Hexadecimal's Numbers | 1 | 64 | One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of *n* different natural numbers from 1 to *n* to obtain total control over her energy.
But his plan failed. The reason for this was very simple: Hexadecimal didn't perceive any information, apart from numbers written in binary format. This means that if a number in a decimal representation contained characters apart from 0 and 1, it was not stored in the memory. Now Megabyte wants to know, how many numbers were loaded successfully. | Input data contains the only number *n* (1<=β€<=*n*<=β€<=109). | Output the only number β answer to the problem. | [
"10\n"
] | [
"2"
] | For *n* = 10 the answer includes numbers 1 and 10. | [
{
"input": "10",
"output": "2"
},
{
"input": "20",
"output": "3"
},
{
"input": "72",
"output": "3"
},
{
"input": "99",
"output": "3"
},
{
"input": "100",
"output": "4"
},
{
"input": "101",
"output": "5"
},
{
"input": "102",
"output": "5"
},
{
"input": "111",
"output": "7"
},
{
"input": "112",
"output": "7"
},
{
"input": "745",
"output": "7"
},
{
"input": "23536",
"output": "31"
},
{
"input": "1",
"output": "1"
},
{
"input": "1010011",
"output": "83"
},
{
"input": "312410141",
"output": "511"
},
{
"input": "1000000000",
"output": "512"
},
{
"input": "999999999",
"output": "511"
},
{
"input": "111111111",
"output": "511"
},
{
"input": "101010101",
"output": "341"
},
{
"input": "121212121",
"output": "511"
},
{
"input": "106341103",
"output": "383"
},
{
"input": "901556123",
"output": "511"
},
{
"input": "832513432",
"output": "511"
},
{
"input": "3",
"output": "1"
},
{
"input": "732875234",
"output": "511"
},
{
"input": "7",
"output": "1"
},
{
"input": "9",
"output": "1"
},
{
"input": "2",
"output": "1"
},
{
"input": "11",
"output": "3"
},
{
"input": "12",
"output": "3"
},
{
"input": "13",
"output": "3"
},
{
"input": "101020101",
"output": "351"
},
{
"input": "111100100",
"output": "484"
},
{
"input": "110110101",
"output": "437"
},
{
"input": "100111001",
"output": "313"
},
{
"input": "100100",
"output": "36"
},
{
"input": "110100102",
"output": "421"
}
] | 1,090 | 67,072,000 | 0 | 6,304 |
0 | none | [
"none"
] | null | null | There are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that they all have integer $y$-coordinates, and their $x$-coordinate is equal to $-100$, while the second group is positioned in such a way that they all have integer $y$-coordinates, and their $x$-coordinate is equal to $100$.
Each spaceship in both groups will simultaneously shoot two laser shots (infinite ray that destroys any spaceship it touches), one towards each of the small spaceships, all at the same time. The small spaceships will be able to avoid all the laser shots, and now want to position themselves at some locations with $x=0$ (with not necessarily integer $y$-coordinates), such that the rays shot at them would destroy as many of the enemy spaceships as possible. Find the largest numbers of spaceships that can be destroyed this way, assuming that the enemy spaceships can't avoid laser shots. | The first line contains two integers $n$ and $m$ ($1 \le n, m \le 60$), the number of enemy spaceships with $x = -100$ and the number of enemy spaceships with $x = 100$, respectively.
The second line contains $n$ integers $y_{1,1}, y_{1,2}, \ldots, y_{1,n}$ ($|y_{1,i}| \le 10\,000$) β the $y$-coordinates of the spaceships in the first group.
The third line contains $m$ integers $y_{2,1}, y_{2,2}, \ldots, y_{2,m}$ ($|y_{2,i}| \le 10\,000$) β the $y$-coordinates of the spaceships in the second group.
The $y$ coordinates are not guaranteed to be unique, even within a group. | Print a single integer β the largest number of enemy spaceships that can be destroyed. | [
"3 9\n1 2 3\n1 2 3 7 8 9 11 12 13\n",
"5 5\n1 2 3 4 5\n1 2 3 4 5\n"
] | [
"9\n",
"10\n"
] | In the first example the first spaceship can be positioned at $(0, 2)$, and the second β at $(0, 7)$. This way all the enemy spaceships in the first group and $6$ out of $9$ spaceships in the second group will be destroyed.
In the second example the first spaceship can be positioned at $(0, 3)$, and the second can be positioned anywhere, it will be sufficient to destroy all the enemy spaceships. | [
{
"input": "3 9\n1 2 3\n1 2 3 7 8 9 11 12 13",
"output": "9"
},
{
"input": "5 5\n1 2 3 4 5\n1 2 3 4 5",
"output": "10"
},
{
"input": "50 50\n744 333 562 657 680 467 357 376 759 311 371 327 369 172 286 577 446 922 16 69 350 92 627 852 878 733 148 857 663 969 131 250 563 665 67 169 178 625 975 457 414 434 146 602 235 86 240 756 161 675\n222 371 393 634 76 268 348 294 227 429 835 534 756 67 174 704 685 462 829 561 249 148 868 512 118 232 33 450 445 420 397 129 122 74 426 441 989 892 662 727 492 702 352 818 399 968 894 297 342 405",
"output": "29"
},
{
"input": "60 60\n842 229 415 973 606 880 422 808 121 317 41 358 725 32 395 286 819 550 410 516 81 599 623 275 568 102 778 234 385 445 194 89 105 643 220 165 872 858 420 653 843 465 696 723 594 8 127 273 289 345 260 553 231 940 912 687 205 272 14 706\n855 361 529 341 602 225 922 807 775 149 212 789 547 766 813 624 236 583 207 586 516 21 621 839 259 774 419 286 537 284 685 944 223 189 358 232 495 688 877 920 400 105 968 919 543 700 538 466 739 33 729 292 891 797 707 174 799 427 321 953",
"output": "40"
},
{
"input": "1 5\n1\n1 2 3 4 5",
"output": "3"
},
{
"input": "5 1\n1 2 3 4 5\n1",
"output": "3"
},
{
"input": "2 2\n-10000 10000\n-10000 10000",
"output": "4"
},
{
"input": "8 57\n-107 1000 -238 -917 -918 668 -769 360\n124 250 601 242 189 155 688 -886 -504 39 -924 -266 -122 109 232 216 567 576 269 -349 257 589 -462 939 977 0 -808 118 -423 -856 769 954 889 21 996 -714 198 -854 981 -99 554 302 -27 454 -557 -585 465 -513 -113 714 -82 -906 522 75 -866 -942 -293",
"output": "8"
},
{
"input": "43 48\n-10 -4 -4 3 -4 3 -1 9 10 4 -2 -8 -9 -6 4 0 4 3 -1 -3 -1 7 10 -2 6 6 -4 -7 7 10 -5 -2 9 -4 -3 -1 -3 -9 0 -5 -6 -7 2\n-8 10 8 4 -3 7 2 -6 10 -1 4 -8 1 3 -8 5 2 4 8 7 -4 -7 8 -8 2 4 -2 4 2 1 -4 9 -3 -9 -1 6 -9 1 -6 -4 6 -2 3 5 5 6 -3 -3",
"output": "91"
},
{
"input": "8 9\n782 -300 482 -158 -755 809 -125 27\n0 251 593 796 371 839 -892 -954 236",
"output": "4"
},
{
"input": "54 41\n-5 9 -4 -7 8 -2 -5 -3 -10 -10 -9 2 9 1 -8 -5 -5 -3 1 -7 -2 -8 -5 -1 2 6 -2 -10 -7 5 2 -4 -9 -2 4 -6 5 5 -3 7 -5 2 7 0 -3 8 -10 5 6 -4 -7 3 -9 6\n-5 -5 10 3 2 5 -3 4 -5 -6 2 9 -7 3 0 -3 -10 -6 -5 -5 9 0 1 -6 1 0 -9 8 -10 -3 -2 -10 4 -1 -3 -10 -6 -7 -6 -3 2",
"output": "95"
},
{
"input": "46 52\n-31 11 38 -71 38 39 57 -31 -2 85 25 -85 17 -8 93 -1 75 -89 22 -61 -66 63 -91 80 -66 19 57 86 42 36 16 -65 -76 53 -21 85 -66 -96 85 45 35 29 54 18 -94 78\n-14 65 94 33 42 23 94 98 -44 -68 5 -27 -5 50 30 -56 49 -31 -61 34 9 -63 -92 48 17 99 -98 54 -13 34 46 13 -38 81 6 -58 68 -97 21 97 84 -10 5 11 99 -65 36 99 23 -20 -81 50",
"output": "53"
},
{
"input": "51 49\n-6 6 -4 -9 10 -5 1 -7 10 -7 -9 7 -6 5 -7 -5 5 6 -1 9 -10 6 -9 -7 1 7 6 -2 -6 0 -9 5 3 -9 0 8 -8 -5 -6 3 0 2 -1 -8 -3 -4 -8 0 1 -7 10\n-9 -4 10 -1 4 7 -2 5 -4 -8 0 -2 -10 10 9 9 10 -6 -8 -3 -6 -7 2 1 -4 -4 5 -5 5 2 8 -3 -7 5 10 7 2 -2 6 7 6 -3 -4 -8 -7 -3 5 -7 4",
"output": "100"
},
{
"input": "49 45\n293 126 883 638 33 -235 -591 -317 -532 -850 367 249 -470 373 -438 866 271 357 423 -972 -358 -418 531 -255 524 831 -200 -677 -424 -486 513 84 -598 86 525 -612 749 -525 -904 -773 599 170 -385 -44 40 979 -963 320 -875\n-197 47 -399 -7 605 -94 371 -752 370 459 297 775 -144 91 895 871 774 997 71 -23 301 138 241 891 -806 -990 111 -120 -233 552 557 633 -221 -804 713 -384 404 13 345 4 -759 -826 148 889 -270",
"output": "20"
},
{
"input": "59 50\n-85 -30 33 10 94 91 -53 58 -21 68 5 76 -61 -35 9 -19 -32 8 57 -75 -49 57 92 8 92 -39 98 -81 -55 -79 -9 36 19 57 -32 11 -68 60 -20 25 -65 1 -25 -59 -65 -30 93 -60 59 10 -92 -76 -83 71 -89 33 1 60 -65\n39 -57 -21 -13 9 34 -93 -11 56 0 -40 -85 18 -96 66 -29 -64 52 -61 -20 67 54 -20 83 -8 -20 75 37 75 -81 37 -67 -89 -91 -30 86 93 58 33 62 -68 -48 87 -7 72 -62 59 81 -6 30",
"output": "68"
},
{
"input": "57 57\n77 62 -5 -19 75 31 -71 29 -73 68 -4 42 -73 72 29 20 50 45 -4 28 73 -1 -25 69 -55 27 5 88 81 52 84 45 -11 -93 -4 23 -33 11 65 47 45 -83 -89 -11 -100 -26 89 41 35 -91 11 4 -23 57 38 17 -67\n68 75 5 10 -98 -17 73 68 -56 -82 69 55 62 -73 -75 -6 46 87 14 -81 -50 -69 -73 42 0 14 -82 -19 -5 40 -60 12 52 -46 97 70 45 -93 29 36 -41 61 -75 -84 -50 20 85 -33 10 80 33 50 44 -67 91 63 6",
"output": "64"
},
{
"input": "52 16\n-4770 -9663 -5578 4931 6841 2993 -9006 -1526 -7843 -6401 -3082 -1988 -790 -2443 135 3540 6817 1432 -5237 -588 2459 4466 -4806 -3125 -8135 2879 -7059 8579 5834 9838 4467 -8424 -115 -6929 3050 -9010 9686 -9669 -3200 8478 -605 4845 1800 3070 2025 3063 -3787 -2948 3255 1614 7372 1484\n8068 -5083 -2302 8047 8609 -1144 -2610 -7251 820 -9517 -7419 -1291 1444 4232 -5153 5539",
"output": "8"
},
{
"input": "8 7\n1787 -3614 8770 -5002 -7234 -8845 -585 -908\n1132 -7180 -5499 3850 352 2707 -8875",
"output": "4"
},
{
"input": "50 46\n17 29 -14 -16 -17 -54 74 -70 -43 5 80 15 82 -10 -21 -98 -98 -52 50 90 -2 97 -93 8 83 89 -31 44 -96 32 100 -4 77 36 71 28 -79 72 -18 89 -80 -3 -73 66 12 70 -78 -59 55 -44\n-10 -58 -14 -60 -6 -100 -41 -52 -67 -75 -33 -80 -98 -51 -76 92 -43 -4 -70 83 -70 28 -95 8 83 0 -54 -78 75 61 21 38 -53 -61 -95 4 -42 -43 14 60 -15 45 -73 -23 76 -73",
"output": "56"
},
{
"input": "6 8\n9115 641 -7434 1037 -612 -6061\n-8444 4031 7752 -7787 -1387 -9687 -1176 8891",
"output": "4"
},
{
"input": "60 13\n999 863 66 -380 488 494 -351 -911 -690 -341 -729 -215 -427 -286 -189 657 44 -577 655 646 731 -673 -49 -836 -768 -84 -833 -539 345 -244 562 -748 260 -765 569 -264 43 -853 -568 134 -574 -874 -64 -946 941 408 393 -741 155 -492 -994 -2 107 508 -560 15 -278 264 -875 -817\n-138 422 -958 95 245 820 -805 -27 376 121 -508 -951 977",
"output": "12"
},
{
"input": "50 58\n-7 7 10 1 4 1 10 -10 -8 2 1 5 -9 10 2 -3 -6 -7 -8 2 7 0 8 -2 -7 9 -4 8 -6 10 -9 -9 2 -8 8 0 -2 8 -10 -10 -10 2 8 -3 5 1 0 4 -9 -2\n6 6 -9 10 -2 -2 7 -5 9 -5 -7 -8 -8 5 -9 -3 -3 7 9 0 9 -1 1 5 1 0 -8 -9 -4 4 -4 5 -2 2 -7 -6 10 -1 -8 -3 6 -1 -10 -5 -10 3 9 7 5 -3 8 -7 6 9 1 10 -9 3",
"output": "108"
},
{
"input": "17 49\n17 55 -3 72 43 -91 1 -51 -5 -58 -30 -3 71 -39 44 9 7\n-38 -9 -74 -77 -14 14 78 13 -96 85 54 -83 -90 18 22 4 -61 23 -13 -38 -87 -79 -25 31 -64 47 -92 91 55 -8 -38 -34 -46 6 31 15 -72 80 -46 58 -1 90 -47 -28 53 31 -61 89 61",
"output": "27"
},
{
"input": "22 54\n484 -77 -421 -590 633 -472 -983 -396 756 -21 -320 -96 -590 -677 758 -556 -672 -798 430 -449 -213 -944\n309 -468 -484 973 -992 -385 -210 205 -318 350 468 196 802 461 286 -431 -81 984 286 -462 47 -647 -760 629 314 -388 986 507 898 287 -434 -390 95 -163 584 -67 655 -19 -756 50 215 833 -753 485 -127 62 -897 -898 1 -924 -224 30 -373 975",
"output": "17"
},
{
"input": "33 30\n-55 26 -48 -87 -87 -73 13 87 -79 -88 91 38 80 86 55 -66 72 -72 -77 -41 95 11 13 -99 -23 -66 -20 35 90 -40 59 -2 43\n-56 -23 16 51 78 -58 -61 -18 -7 -57 -8 86 -44 -47 -70 -31 -34 -80 -85 -21 53 93 -93 88 -54 -83 97 57 47 80",
"output": "28"
},
{
"input": "10 8\n8780 -6753 -8212 -1027 1193 -6328 -4260 -8031 4114 -135\n-6545 1378 6091 -4158 3612 1509 -8731 1391",
"output": "4"
},
{
"input": "10 5\n-7722 3155 -4851 -5222 -2712 4693 -3099 222 -4282 -4848\n3839 3098 -8804 4627 -7437",
"output": "4"
},
{
"input": "4 10\n1796 5110 -8430 -617\n9903 -5666 -2809 -4878 -284 -1123 5202 -3694 -789 5483",
"output": "4"
},
{
"input": "46 60\n-119 682 371 355 -473 978 -474 311 379 -311 601 -287 683 625 982 -772 -706 -995 451 -877 452 -823 -51 826 -771 -419 -215 -502 -110 -454 844 -433 942 250 155 -787 628 282 -818 -784 282 -888 200 628 -320 62\n-389 -518 341 98 -138 -816 -628 81 567 112 -220 -122 -307 -891 -85 253 -352 -244 194 779 -884 866 -23 298 -191 -497 106 -553 -612 -48 -279 847 -721 195 -397 -455 486 -572 -489 -183 -582 354 -542 -371 -330 -105 -110 -536 -559 -487 -297 -533 813 281 847 -786 8 -179 394 -734",
"output": "19"
},
{
"input": "39 31\n268 -441 -422 252 377 420 749 748 660 893 -309 722 -612 -667 363 79 650 884 -672 -880 518 -936 806 376 359 -965 -964 138 851 717 -131 316 603 -375 114 421 976 688 -527\n-989 -76 -404 971 -572 771 149 674 -471 218 -317 -225 994 10 509 719 915 -811 -57 -995 865 -486 7 -766 143 -53 699 -466 -165 -486 602",
"output": "15"
},
{
"input": "5 3\n-8452 -1472 4013 -5048 -6706\n-8387 -7493 -7090",
"output": "4"
},
{
"input": "58 58\n-2 79 3 14 40 -23 87 -86 80 -23 77 12 55 -81 59 -84 -66 89 92 -85 14 -44 -28 -75 77 -36 97 69 21 -31 -26 -13 9 83 -70 38 58 79 -34 68 -52 -50 -68 41 86 -9 -87 64 90 -88 -55 -32 35 100 76 -85 63 -29\n68 3 -18 -13 -98 -52 -90 -21 43 -63 -97 49 40 65 -96 83 15 2 76 54 50 49 4 -71 -62 53 26 -90 -38 -24 71 -69 -58 -86 66 5 31 -23 -76 -34 -79 72 7 45 -86 -97 -43 85 -51 -76 26 98 58 -28 58 44 82 -70",
"output": "79"
},
{
"input": "9 10\n-393 439 961 649 441 -536 -453 989 733\n-952 -776 674 696 -452 -700 58 -430 540 271",
"output": "8"
},
{
"input": "8 6\n-90 817 655 798 -547 -390 -828 -50\n-626 -365 426 139 513 -607",
"output": "6"
},
{
"input": "54 11\n-10 5 -4 -7 -2 10 -10 -4 6 4 9 -7 -10 8 8 6 0 -6 8 4 -6 -1 6 4 -6 1 -2 8 -5 -2 -9 -8 9 6 1 2 10 3 1 3 -3 -10 8 -2 3 9 8 3 -9 -5 -6 -2 -5 -6\n10 1 0 -9 -5 -6 8 0 -3 5 -5",
"output": "55"
},
{
"input": "6 7\n3403 -4195 5813 -1096 -9300 -959\n-4820 9153 2254 6322 -5071 6383 -687",
"output": "4"
},
{
"input": "41 56\n6 2 0 -3 3 6 0 10 -7 -5 -5 7 -5 -9 -3 -5 -2 9 5 -1 1 8 -2 1 -10 10 -4 -9 10 -8 8 7 7 7 4 4 -2 2 4 -6 -7\n9 6 -5 6 -7 2 -6 -3 -6 -1 10 -5 -5 3 10 10 4 3 0 2 8 4 -3 3 9 4 -6 0 2 6 6 -2 0 -3 -5 3 4 -2 -3 10 -10 1 3 -3 -7 2 -2 2 0 4 -6 8 -4 -1 1 -6",
"output": "97"
},
{
"input": "45 57\n-5 -3 -10 2 -3 1 10 -3 -3 -7 -9 6 6 1 8 2 -4 3 -6 9 8 10 -1 8 -2 -8 -9 -7 -8 4 -1 -10 0 -4 8 -7 3 -1 0 3 -8 -10 -6 -8 -5\n1 3 -1 7 1 10 3 -2 8 6 0 2 -3 -3 10 -10 -6 -7 10 5 9 10 3 -2 4 10 -10 0 -2 4 -6 -1 -1 -5 7 -3 -2 -7 7 -2 2 2 1 -10 -7 -8 -3 4 0 8 -5 -7 -7 9 -3 8 -5",
"output": "102"
},
{
"input": "51 39\n-10 6 -8 2 6 6 0 2 4 -3 8 10 7 1 9 -8 4 -2 3 5 8 -2 1 3 1 3 -5 0 2 2 7 -3 -10 4 9 -3 -7 5 5 10 -5 -5 9 -3 9 -1 -4 9 -7 -8 5\n-5 10 -2 -8 -10 5 -7 1 7 6 -3 -5 0 -4 0 -9 2 -9 -10 2 -6 10 0 4 -4 -8 -3 1 10 7 5 7 0 -7 1 0 9 0 -5",
"output": "90"
},
{
"input": "4 10\n3 -7 -4 -8\n7 3 -1 -8 2 -1 -5 8 -8 9",
"output": "12"
},
{
"input": "44 41\n-6 0 -2 5 5 -9 -4 -5 -2 -6 -7 -10 5 2 -6 -3 1 4 8 2 -7 6 5 0 10 -2 -9 3 -6 -3 7 5 -3 7 -10 -1 6 0 10 -6 -5 -6 -6 6\n6 -3 1 -1 8 9 6 7 -6 -4 -2 -4 -3 3 2 -1 3 1 10 -2 2 -10 -9 -3 8 -3 -1 -4 0 0 -4 7 -10 6 10 -8 5 6 2 -9 -4",
"output": "85"
},
{
"input": "52 43\n-514 -667 -511 516 -332 73 -233 -594 125 -847 -584 432 631 -673 -380 835 69 523 -568 -110 -752 -731 864 250 550 -249 525 357 8 43 -395 -328 61 -84 -151 165 -896 955 -660 -195 375 806 -160 870 143 -725 -814 494 -953 -463 704 -415\n608 -584 673 -920 -227 -442 242 815 533 -184 -502 -594 -381 -960 786 -627 -531 -579 583 -252 -445 728 902 934 -311 971 119 -391 710 -794 738 -82 774 580 -142 208 704 -745 -509 979 -236 -276 -800",
"output": "18"
},
{
"input": "51 48\n642 261 822 -266 700 18 -62 -915 39 997 564 -130 605 -141 58 426 -514 425 -310 -56 -524 860 -793 57 511 -563 -529 -140 -679 -489 -841 -326 -108 -785 599 3 -90 -52 769 -513 -328 -709 -887 736 729 -148 232 680 -589 77 531\n689 765 386 700 612 -936 -258 966 873 130 230 -78 -835 739 -755 127 -963 -282 -728 -833 345 -817 -61 680 944 -475 -46 -915 777 -789 -742 -755 325 -474 -220 544 19 828 -483 -388 -330 -150 -912 -219 185 -541 237 724",
"output": "20"
},
{
"input": "35 32\n8 3 9 -6 -6 -3 -6 2 2 3 0 -4 8 9 -10 -7 7 -6 -4 1 -9 3 -2 3 -4 -8 -8 5 -10 2 6 4 -7 -6 -1\n7 -2 1 -9 1 8 4 -4 -4 -7 5 4 0 3 5 8 9 -7 -1 -8 -1 7 2 5 6 -2 -8 -2 9 -6 8 -6",
"output": "67"
},
{
"input": "54 55\n-95 -3 -18 81 -85 -78 -76 95 -4 -91 88 98 35 88 -30 -82 -1 23 -98 82 -83 100 -47 -7 93 -87 -57 -5 -57 -46 30 -16 -27 -46 78 -58 4 87 86 -58 22 19 -40 8 -6 92 -65 10 -51 -34 -70 -69 -70 -51\n-42 75 48 -79 58 23 -8 47 48 33 -2 97 -30 -8 -87 56 22 -91 25 27 -91 -75 -10 45 -27 54 -94 60 -49 22 18 2 35 -81 8 -61 91 12 78 6 -83 76 -81 -27 -65 56 -99 -69 3 91 81 -34 9 -29 61",
"output": "63"
},
{
"input": "18 4\n81 -30 22 81 -9 -66 -39 -11 16 9 91 74 -36 40 -26 -11 -13 -22\n21 67 96 96",
"output": "8"
},
{
"input": "5 5\n9 9 -7 -4 6\n2 3 -3 -8 -1",
"output": "7"
},
{
"input": "44 49\n28 76 41 66 49 31 3 4 41 89 44 41 33 73 5 -85 57 -55 86 43 25 0 -26 -36 81 -80 -71 77 96 85 -8 -96 -91 28 3 -98 -82 -87 -50 70 -39 -99 -70 66\n-12 28 11 -25 -34 70 -4 69 9 -31 -23 -8 19 -54 -5 -24 -7 -45 -70 -71 -64 77 39 60 -63 10 -7 -92 22 4 45 75 100 49 95 -66 -96 -85 -35 92 -9 -37 -38 62 -62 24 -35 40 3",
"output": "52"
},
{
"input": "57 41\n-10 9 2 7 -1 1 -10 9 7 -9 -4 8 4 -8 -6 8 1 5 -7 9 8 -4 1 -2 -7 6 -9 -10 3 0 8 7 1 -4 -6 9 -10 8 3 -9 4 -9 6 -7 10 -4 3 -4 10 -1 -7 -7 -10 -10 0 3 -10\n-2 1 -5 6 0 -2 -4 8 2 -9 -6 7 2 6 -9 -1 -1 3 -4 -8 -4 -10 7 -1 -6 -5 -7 5 10 -5 -4 4 -7 6 -2 -9 -10 3 -7 -5 -9",
"output": "98"
},
{
"input": "9 5\n7 -5 -2 -3 -10 -3 5 4 10\n-1 6 -4 3 2",
"output": "11"
},
{
"input": "31 55\n68 -31 19 47 95 -44 67 45 32 -17 31 -14 52 -19 -75 97 88 9 -11 77 -23 74 -29 31 -42 -15 -77 30 -17 75 -9\n-63 94 98 45 -57 -41 4 34 38 67 68 69 -36 47 91 -55 58 73 77 -71 4 -89 -6 49 71 70 -64 -24 -87 -3 -96 62 -31 -56 -2 88 -80 95 -97 -91 25 -2 1 -80 -45 -96 -62 12 12 -61 -13 23 -32 6 29",
"output": "43"
},
{
"input": "50 59\n-6 -5 8 -6 7 9 2 -7 0 -9 -7 1 -5 10 -6 -2 -10 6 -6 -2 -7 -10 1 4 -4 9 2 -8 -3 -1 5 -4 2 8 -10 7 -10 4 8 7 -4 9 1 5 -10 -7 -2 3 -9 5\n-10 -1 3 9 0 8 5 10 -6 -2 -2 4 4 -1 -3 -9 4 -6 9 -5 -5 -4 -7 -2 9 6 -3 -6 -1 -9 1 9 2 -9 2 -5 3 7 -10 7 -3 -1 -10 -3 2 6 -2 -5 10 5 8 7 4 6 -7 -5 2 -2 -10",
"output": "109"
},
{
"input": "53 51\n678 -657 703 569 -524 -801 -221 -600 -95 11 -660 866 506 683 649 -842 604 -33 -929 541 379 939 -512 -347 763 697 653 844 927 488 -233 -313 357 -717 119 885 -864 738 -20 -350 -724 906 -41 324 -713 424 -432 154 173 406 29 -420 62\n-834 648 564 735 206 490 297 -968 -482 -914 -149 -312 506 56 -773 527 816 137 879 552 -224 811 -786 739 -828 -203 -873 148 -290 395 832 -845 302 -324 32 299 746 638 -684 216 392 -137 496 57 -187 477 -16 395 -325 -186 -801",
"output": "24"
},
{
"input": "51 7\n323 236 120 48 521 587 327 613 -470 -474 522 -705 320 -51 1 288 -430 -954 732 -805 -562 300 -710 190 515 280 -101 -927 77 282 198 -51 -350 -990 -435 -765 178 -934 -955 704 -565 -640 853 -27 950 170 -712 -780 620 -572 -409\n244 671 425 977 773 -294 268",
"output": "9"
},
{
"input": "9 9\n-9 -1 2 8 10 2 9 7 3\n5 8 -5 4 -4 -8 2 8 -8",
"output": "16"
},
{
"input": "50 60\n445 303 -861 -583 436 -125 312 -700 -829 -865 -276 -25 -725 -286 528 -221 757 720 -572 514 -514 359 294 -992 -838 103 611 776 830 143 -247 182 -241 -627 299 -824 635 -571 -660 924 511 -876 160 569 -570 827 75 558 708 46\n899 974 750 -138 -439 -904 -113 -761 -150 -92 -279 489 323 -649 -759 667 -600 -76 -991 140 701 -654 -276 -563 108 -301 161 -989 -852 -97 316 31 -724 848 979 -501 -883 569 925 -532 86 456 302 -985 826 79 -911 660 752 941 -464 -157 -110 433 829 -872 172 496 528 -576",
"output": "24"
},
{
"input": "7 3\n90 67 1 68 40 -100 -26\n-96 70 -74",
"output": "6"
},
{
"input": "7 4\n-8 -2 -5 -3 -8 -9 8\n-8 4 -1 10",
"output": "8"
},
{
"input": "52 48\n-552 43 -670 -163 -765 -603 -768 673 -248 337 -89 941 -676 -406 280 409 -630 -577 324 115 927 477 -242 108 -337 591 -158 -524 928 859 825 935 818 638 -51 988 -568 871 -842 889 -737 -272 234 -643 766 -422 473 -570 -1000 -735 -279 845\n963 -436 738 113 273 -374 -568 64 276 -698 -135 -748 909 -250 -740 -344 -436 414 719 119 973 -881 -576 868 -45 909 630 286 -845 458 684 -64 579 965 598 205 -318 -974 228 -596 596 -946 -198 923 571 -907 -911 -341",
"output": "21"
},
{
"input": "40 54\n-26 -98 27 -64 0 33 62 -12 -8 -10 -62 28 28 75 -5 -89 -75 -100 -63 9 -97 -20 -81 62 56 -39 87 -33 13 61 19 -97 -23 95 18 -4 -48 55 -40 -81\n-69 -71 1 -46 -58 0 -100 -82 31 43 -59 -43 77 -8 -61 -2 -36 -55 -35 -44 -59 -13 -16 14 60 -95 -61 25 76 -1 -3 9 -92 48 -92 -54 2 -7 73 -16 42 -40 36 -58 97 81 13 -64 -12 -28 65 85 -61 8",
"output": "54"
},
{
"input": "22 34\n-73 45 -81 -67 9 61 -97 0 -64 86 -9 47 -28 100 79 -53 25 -59 -80 -86 -47 24\n57 69 8 -34 -37 12 -32 -81 27 -65 87 -64 62 -44 97 34 44 -93 44 25 -72 93 14 95 -60 -65 96 -95 23 -69 28 -20 -95 74",
"output": "27"
},
{
"input": "46 48\n-710 947 515 217 26 -548 -416 494 431 -872 -616 848 -950 -138 -104 560 241 -462 -265 90 66 -331 934 -788 -815 -558 86 39 784 86 -856 879 -733 -653 104 -673 -588 -568 78 368 -226 850 195 982 140 370\n470 -337 283 460 -710 -434 739 459 -567 173 217 511 -830 644 -734 764 -211 -106 423 -356 -126 677 -454 42 680 557 -636 9 -552 877 -246 -352 -44 442 -505 -811 -100 -768 -130 588 -428 755 -904 -138 14 -253 -40 265",
"output": "20"
},
{
"input": "58 40\n120 571 -472 -980 993 -885 -546 -220 617 -697 -182 -42 128 -29 567 -615 -260 -876 -507 -642 -715 -283 495 -584 97 4 376 -131 186 -301 729 -545 8 -610 -643 233 -123 -769 -173 119 993 825 614 -503 891 426 -850 -992 -406 784 634 -294 997 127 697 -509 934 316\n630 -601 937 -544 -50 -176 -885 530 -828 556 -784 460 -422 647 347 -862 131 -76 490 -576 5 -761 922 -426 -7 -401 989 -554 688 -531 -303 -415 -507 -752 -581 -589 513 -151 -279 317",
"output": "18"
},
{
"input": "8 6\n23 -18 -94 -80 74 89 -48 61\n-84 45 -12 -9 -74 63",
"output": "7"
},
{
"input": "57 55\n-34 744 877 -4 -902 398 -404 225 -560 -600 634 180 -198 703 910 681 -864 388 394 -317 839 -95 -706 -474 -340 262 228 -196 -35 221 -689 -960 -430 946 -231 -146 741 -754 330 217 33 276 381 -734 780 -522 528 -425 -202 -19 -302 -743 53 -247 69 -314 -845\n-158 24 461 -274 -30 -58 223 -806 747 568 -59 126 56 661 8 419 -422 320 340 480 -185 905 -459 -530 -50 -484 303 366 889 404 915 -361 -985 -642 -690 -577 -80 -250 173 -740 385 908 -4 593 -559 731 326 -209 -697 490 265 -548 716 320 23",
"output": "24"
},
{
"input": "59 56\n-8 -8 -4 5 -4 4 4 -5 -10 -2 0 6 -9 -2 10 1 -8 -2 9 5 2 -1 -5 7 -7 7 2 -7 -5 2 -2 -8 4 10 5 -9 5 9 6 10 7 6 -6 -4 -8 5 9 6 1 -7 5 -9 8 10 -7 1 3 -6 8\n6 -2 -7 4 -2 -5 -9 -5 0 8 5 5 -4 -6 -5 -10 4 3 -4 -4 -8 2 -6 -10 -10 4 -3 8 -1 8 -8 -5 -2 3 7 3 -8 10 6 0 -8 0 9 -3 9 0 9 0 -3 4 -3 10 10 5 -6 4",
"output": "115"
},
{
"input": "53 30\n-4206 -1169 3492 6759 5051 -3338 4024 8267 -4651 -7685 -3346 -4958 2648 9321 6062 -3566 8118 9067 -1331 5064 148 6375 6193 -2024 -9376 -663 3837 3989 6583 6971 -146 2515 -4222 8159 -94 -4937 -8364 -6025 3566 556 -5229 3138 -9504 1383 1171 -3918 -1587 -6532 -2299 -6648 -5861 4864 9220\n-2359 7436 1682 1775 3850 2691 -4326 6670 3245 -3821 5932 -1159 6162 -2818 -5255 -7439 -6688 1778 -5132 8085 -3576 9153 -5260 -1438 9941 -4729 532 -5206 2133 -2252",
"output": "10"
},
{
"input": "4 9\n-2 3 -5 -10\n7 -7 5 5 2 4 9 -4 5",
"output": "11"
},
{
"input": "44 50\n23 -401 692 -570 264 -885 417 -355 560 -254 -468 -849 900 997 559 12 853 424 -579 485 711 67 638 771 -750 -583 294 -410 -225 -117 -262 148 385 627 610 983 -345 -236 -62 635 -421 363 88 682\n-204 -429 -74 855 533 -817 -613 205 972 941 -566 -813 79 -660 -604 661 273 -70 -70 921 -240 148 314 328 -155 -56 -793 259 -630 92 -975 -361 671 963 430 315 -94 957 465 548 -796 626 -58 -595 315 -455 -918 398 279 99",
"output": "22"
},
{
"input": "53 30\n5 10 -1 -9 7 -7 1 6 0 7 2 -2 -2 1 -9 -9 2 -7 9 10 -9 1 -1 -9 -9 -5 -8 -3 2 4 -3 -6 6 4 -2 -3 -3 -9 2 -4 9 5 6 -5 -5 6 -2 -1 10 7 4 -4 -2\n-1 10 3 -1 7 10 -2 -1 -2 0 3 -10 -6 1 -9 2 -10 9 6 -7 -9 3 -7 1 0 9 -8 2 9 7",
"output": "82"
},
{
"input": "9 9\n1 10 0 -2 9 -7 1 -4 3\n-7 -1 6 -4 8 2 6 6 -3",
"output": "15"
},
{
"input": "9 9\n5181 -7243 3653 3587 -5051 -4899 -4110 7981 -6429\n-7365 -2247 7942 9486 -7160 -1020 -8934 7733 -3010",
"output": "4"
},
{
"input": "55 43\n9 1 0 -7 4 3 4 4 -8 3 0 -7 0 -9 3 -6 0 4 7 1 -1 -10 -7 -6 -8 -8 2 -5 5 -4 -9 -7 5 -3 -7 -10 -4 -2 -7 -3 2 4 9 8 -8 9 -10 0 0 3 -6 -5 -2 9 -6\n-4 -6 9 -4 -2 5 9 6 -8 -2 -3 -7 -8 8 -8 5 1 7 9 7 -5 10 -10 -8 -3 10 0 8 8 4 8 3 10 -8 -4 -6 1 9 0 -3 -4 8 -10",
"output": "98"
},
{
"input": "53 12\n63 88 91 -69 -15 20 98 40 -70 -49 -51 -74 -34 -52 1 21 83 -14 57 40 -57 33 94 2 -74 22 86 79 9 -18 67 -31 72 31 -64 -83 83 29 50 -29 -27 97 -40 -8 -57 69 -93 18 42 68 -71 -86 22\n51 19 33 12 98 91 -83 65 -6 16 81 86",
"output": "27"
},
{
"input": "1 1\n0\n0",
"output": "2"
},
{
"input": "3 3\n1 1 1\n1 2 2",
"output": "6"
},
{
"input": "1 1\n1\n1",
"output": "2"
},
{
"input": "1 1\n0\n1",
"output": "2"
},
{
"input": "3 3\n0 0 0\n0 0 0",
"output": "6"
},
{
"input": "5 5\n5 5 5 5 5\n5 5 5 5 5",
"output": "10"
},
{
"input": "60 60\n0 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59\n0 60 120 180 240 300 360 420 480 540 600 660 720 780 840 900 960 1020 1080 1140 1200 1260 1320 1380 1440 1500 1560 1620 1680 1740 1800 1860 1920 1980 2040 2100 2160 2220 2280 2340 2400 2460 2520 2580 2640 2700 2760 2820 2880 2940 3000 3060 3120 3180 3240 3300 3360 3420 3480 3540",
"output": "4"
},
{
"input": "2 2\n0 2\n0 1",
"output": "4"
},
{
"input": "1 1\n5\n5",
"output": "2"
},
{
"input": "10 10\n1 1 1 1 1 1 1 1 1 1\n-30 -30 -30 -30 40 40 40 40 40 40",
"output": "20"
}
] | 30 | 0 | 0 | 6,314 |
|
802 | Heidi and Library (easy) | [
"greedy"
] | null | null | Your search for Heidi is over β you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are *n* different books, numbered 1 through *n*.
We will look at the library's operation during *n* consecutive days. Heidi knows in advance that on the *i*-th day (1<=β€<=*i*<=β€<=*n*) precisely one person will come to the library, request to borrow the book *a**i*, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book *a**i* available in the library on the *i*-th day. During the night before the *i*-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is *k* β this means that at any time, there can be at most *k* books at the library. If buying a new book would cause Heidi to have more than *k* books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given *k* and the sequence of requests for books *a*1,<=*a*2,<=...,<=*a**n*. What is the minimum cost (in CHF) of buying new books to satisfy all the requests? | The first line of input will contain two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=80). The second line will contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*) β the sequence of book requests. | On a single line print the minimum cost of buying books at the store so as to satisfy all requests. | [
"4 80\n1 2 2 1\n",
"4 1\n1 2 2 1\n",
"4 2\n1 2 3 1\n"
] | [
"2\n",
"3\n",
"3\n"
] | In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day. | [
{
"input": "4 80\n1 2 2 1",
"output": "2"
},
{
"input": "4 1\n1 2 2 1",
"output": "3"
},
{
"input": "4 2\n1 2 3 1",
"output": "3"
},
{
"input": "11 1\n1 2 3 5 1 10 10 1 1 3 5",
"output": "9"
},
{
"input": "5 2\n1 2 3 1 2",
"output": "4"
},
{
"input": "4 2\n1 2 3 2",
"output": "3"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "80 4\n9 9 2 6 3 10 2 5 4 9 6 7 5 5 3 8 5 3 2 10 7 8 5 3 4 9 4 3 9 5 2 10 8 4 7 3 8 3 5 2 3 7 8 4 2 4 4 7 2 2 5 7 5 8 10 10 5 1 1 3 5 2 10 8 7 9 7 4 8 3 2 8 7 9 10 9 7 1 5 5",
"output": "34"
},
{
"input": "80 4\n10 19 20 18 16 7 13 18 15 5 7 13 16 8 14 8 3 15 19 19 7 13 17 9 18 16 4 14 10 18 1 3 5 3 20 18 9 4 17 19 13 20 16 12 15 5 5 18 17 16 4 5 20 10 18 4 7 19 10 15 8 15 17 3 10 16 19 2 6 6 3 12 10 7 15 3 17 15 6 8",
"output": "49"
},
{
"input": "80 4\n28 34 9 3 29 12 19 17 22 10 21 2 26 18 14 7 7 10 37 39 10 1 9 37 33 4 25 21 23 2 4 2 35 1 11 19 33 31 18 10 23 1 26 20 17 31 18 27 31 22 33 7 2 5 30 24 18 32 1 14 2 33 7 26 2 10 1 10 5 19 37 33 33 34 28 20 1 22 11 14",
"output": "58"
},
{
"input": "80 4\n71 49 41 21 72 71 37 14 51 59 73 11 70 15 36 46 32 57 58 15 72 67 16 75 70 11 67 3 40 36 2 9 63 68 32 22 63 52 67 55 35 19 72 59 22 19 44 55 59 74 4 34 53 3 22 57 32 27 78 12 71 4 26 15 43 21 79 10 67 39 34 74 38 26 31 78 2 78 69 42",
"output": "62"
},
{
"input": "80 8\n16 13 11 16 3 4 1 4 4 16 6 6 1 12 19 18 12 15 2 10 2 18 18 13 3 17 16 15 7 6 19 8 2 14 17 13 1 14 4 2 3 16 2 15 13 15 9 10 7 14 7 2 1 18 19 15 7 3 19 8 9 4 12 4 3 4 9 10 6 5 4 4 9 4 20 8 17 7 1 14",
"output": "32"
},
{
"input": "80 8\n5 17 39 25 40 34 11 23 7 16 20 35 31 14 18 17 32 10 40 9 17 23 5 33 2 9 21 22 8 11 22 7 28 36 3 10 12 21 20 29 25 5 12 30 8 21 18 19 1 29 9 4 19 5 15 36 38 37 10 27 15 13 6 22 31 5 40 30 21 39 23 21 39 32 37 28 29 11 34 16",
"output": "51"
},
{
"input": "80 8\n8 72 32 27 27 20 69 28 77 25 8 4 75 11 41 71 57 17 45 65 79 8 61 15 24 80 39 36 34 13 76 37 16 71 64 77 11 58 30 26 61 23 18 30 68 65 12 47 69 65 3 55 71 3 32 4 20 39 47 25 75 49 34 60 48 56 77 70 59 59 75 6 5 23 55 30 62 66 4 4",
"output": "57"
},
{
"input": "80 12\n9 5 8 1 12 2 6 19 8 20 6 12 9 6 16 1 2 5 11 6 8 4 13 7 2 17 18 12 15 17 13 2 9 8 1 17 10 2 9 12 18 3 5 11 10 16 7 16 8 11 3 18 13 19 8 13 13 2 20 13 11 14 20 3 2 1 17 18 17 8 4 3 12 3 19 18 4 16 6 6",
"output": "25"
},
{
"input": "80 12\n27 12 25 30 13 27 12 17 35 25 1 28 35 16 23 20 38 1 37 2 35 29 16 26 37 4 23 39 24 2 16 21 39 21 23 38 33 9 38 22 40 36 23 39 1 2 4 14 22 26 32 4 31 38 4 5 4 15 35 12 5 32 37 38 11 14 16 26 36 38 2 40 10 15 33 38 36 20 35 12",
"output": "37"
},
{
"input": "80 12\n30 19 34 24 56 38 31 63 57 50 53 69 79 5 6 74 47 47 73 17 18 70 72 49 35 20 65 21 18 4 54 12 67 8 28 25 64 6 31 36 35 54 61 7 45 54 55 49 50 6 3 7 10 29 76 62 50 50 32 66 25 19 17 3 67 17 37 67 58 18 54 25 8 78 35 16 61 19 45 40",
"output": "55"
},
{
"input": "80 16\n4 27 31 28 8 17 28 31 20 7 39 5 40 13 28 6 23 1 16 4 34 2 13 6 6 9 18 1 25 19 33 26 33 16 24 5 13 23 25 9 10 16 25 34 39 8 4 6 33 25 7 40 32 23 13 17 32 20 28 25 33 20 29 2 40 34 23 6 28 2 12 12 9 36 18 39 32 8 11 15",
"output": "36"
},
{
"input": "80 16\n31 26 40 46 75 35 63 29 2 49 51 14 4 65 10 4 8 72 44 67 57 60 69 21 52 40 37 54 27 12 31 24 21 59 61 80 11 76 58 7 77 10 55 9 11 36 7 41 61 13 2 28 28 77 22 57 54 62 65 80 78 32 72 64 41 69 36 46 50 5 48 53 6 76 76 65 57 7 29 67",
"output": "53"
},
{
"input": "80 40\n34 71 32 39 65 8 13 4 7 4 18 66 20 12 57 74 58 50 30 27 31 48 1 6 63 63 7 32 56 48 42 35 45 55 52 76 52 26 40 15 8 38 73 47 55 75 17 22 36 59 28 19 6 79 58 7 40 66 48 39 71 67 55 61 71 24 60 39 63 6 47 70 8 10 68 49 49 67 53 15",
"output": "54"
},
{
"input": "80 60\n70 59 1 24 50 77 11 71 36 26 75 56 30 31 76 51 73 29 49 2 80 55 32 52 51 56 2 28 37 11 37 1 63 33 74 60 59 63 55 28 73 45 5 69 77 8 35 76 47 48 49 77 25 75 38 70 77 80 27 9 13 45 14 30 33 34 32 78 52 4 7 57 55 43 13 1 45 36 79 45",
"output": "50"
},
{
"input": "80 80\n73 32 65 26 35 2 10 73 80 64 67 14 40 34 15 59 8 71 5 9 49 63 32 51 23 1 13 9 34 6 80 31 32 60 31 27 65 20 21 74 26 51 17 11 19 37 4 50 73 22 71 55 76 7 51 21 50 63 6 75 66 55 37 62 10 45 52 21 42 18 80 12 7 28 54 50 9 5 25 42",
"output": "53"
},
{
"input": "80 4\n1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5",
"output": "23"
},
{
"input": "80 8\n1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8",
"output": "17"
},
{
"input": "80 16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12",
"output": "20"
},
{
"input": "80 32\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 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 1 2 3 4 5 6 7 8 9 10 11 12 13 14",
"output": "34"
},
{
"input": "80 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 71 72 73 74 75 76 77 78 79 80",
"output": "80"
}
] | 62 | 0 | 0 | 6,322 |
|
66 | Petya and Post | [
"data structures",
"dp"
] | E. Petya and Post | 2 | 256 | Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then he must drive in the given car exactly one round along the circular road and return to the starting post office (the uncle can drive along the circle in any direction, counterclockwise or clockwise). Besides, since the car belongs to the city post, it should also be fuelled with gasoline only at the Post Office stations.
The total number of stations equals to *n*. One can fuel the car at the *i*-th station with no more than *a**i* liters of gasoline. Besides, one can fuel the car no more than once at each station. Also, the distance between the 1-st and the 2-nd station is *b*1 kilometers, the distance between the 2-nd and the 3-rd one is *b*2 kilometers, ..., between the (*n*<=-<=1)-th and the *n*-th ones the distance is *b**n*<=-<=1 kilometers and between the *n*-th and the 1-st one the distance is *b**n* kilometers. Petya's uncle's high-tech car uses only one liter of gasoline per kilometer. It is known that the stations are located so that the sum of all *a**i* is equal to the sum of all *b**i*. The *i*-th gas station and *i*-th post office are very close, so the distance between them is 0 kilometers.
Thus, it becomes clear that if we start from some post offices, then it is not always possible to drive one round along a circular road. The uncle faces the following problem: to what stations can he go in the morning to be able to ride exactly one circle along the circular road and visit all the post offices that are on it?
Petya, who used to attend programming classes, has volunteered to help his uncle, but his knowledge turned out to be not enough, so he asks you to help him write the program that will solve the posed problem. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* integers *a**i* β amount of gasoline on the *i*-th station. The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n*. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the *n*-th and the 1-st ones, respectively. The sum of all *b**i* equals to the sum of all *a**i* and is no more than 109. Each of the numbers *a**i*, *b**i* is no less than 1 and no more than 109. | Print on the first line the number *k* β the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line *k* numbers in the ascending order β the numbers of offices, from which the car can start. | [
"4\n1 7 2 3\n8 1 1 3\n",
"8\n1 2 1 2 1 2 1 2\n2 1 2 1 2 1 2 1\n"
] | [
"2\n2 4\n",
"8\n1 2 3 4 5 6 7 8\n"
] | none | [
{
"input": "4\n1 7 2 3\n8 1 1 3",
"output": "2\n2 4"
},
{
"input": "8\n1 2 1 2 1 2 1 2\n2 1 2 1 2 1 2 1",
"output": "8\n1 2 3 4 5 6 7 8"
},
{
"input": "20\n31 16 20 30 19 35 8 11 20 45 10 26 21 39 29 52 8 10 37 49\n16 33 41 32 43 24 35 48 19 37 28 26 7 10 23 48 18 2 1 25",
"output": "4\n1 2 12 13"
},
{
"input": "20\n10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10\n10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10",
"output": "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20"
},
{
"input": "1\n1\n1",
"output": "1\n1"
},
{
"input": "1\n1000000000\n1000000000",
"output": "1\n1"
},
{
"input": "3\n3 3 3\n3 2 4",
"output": "3\n1 2 3"
},
{
"input": "10\n1 5 4 3 2 1 5 8 2 3\n1 1 1 1 5 5 5 5 5 5",
"output": "3\n1 2 5"
},
{
"input": "10\n44 22 14 9 93 81 52 64 3 99\n43 23 13 10 92 82 51 65 2 100",
"output": "6\n1 3 5 7 9 10"
}
] | 92 | 0 | 0 | 6,330 |
327 | Hungry Sequence | [
"math"
] | null | null | Iahub and Iahubina went to a date at a luxury restaurant. Everything went fine until paying for the food. Instead of money, the waiter wants Iahub to write a Hungry sequence consisting of *n* integers.
A sequence *a*1, *a*2, ..., *a**n*, consisting of *n* integers, is Hungry if and only if:
- Its elements are in increasing order. That is an inequality *a**i*<=<<=*a**j* holds for any two indices *i*,<=*j* (*i*<=<<=*j*). - For any two indices *i* and *j* (*i*<=<<=*j*), *a**j* must not be divisible by *a**i*.
Iahub is in trouble, so he asks you for help. Find a Hungry sequence with *n* elements. | The input contains a single integer: *n* (1<=β€<=*n*<=β€<=105). | Output a line that contains *n* space-separated integers *a*1 *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=107), representing a possible Hungry sequence. Note, that each *a**i* must not be greater than 10000000 (107) and less than 1.
If there are multiple solutions you can output any one. | [
"3\n",
"5\n"
] | [
"2 9 15\n",
"11 14 20 27 31\n"
] | none | [
{
"input": "3",
"output": "2 9 15"
},
{
"input": "5",
"output": "11 14 20 27 31"
},
{
"input": "1",
"output": "3"
},
{
"input": "1000",
"output": "3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3..."
},
{
"input": "100000",
"output": "300000 300001 300002 300003 300004 300005 300006 300007 300008 300009 300010 300011 300012 300013 300014 300015 300016 300017 300018 300019 300020 300021 300022 300023 300024 300025 300026 300027 300028 300029 300030 300031 300032 300033 300034 300035 300036 300037 300038 300039 300040 300041 300042 300043 300044 300045 300046 300047 300048 300049 300050 300051 300052 300053 300054 300055 300056 300057 300058 300059 300060 300061 300062 300063 300064 300065 300066 300067 300068 300069 300070 300071 300072 ..."
},
{
"input": "46550",
"output": "139650 139651 139652 139653 139654 139655 139656 139657 139658 139659 139660 139661 139662 139663 139664 139665 139666 139667 139668 139669 139670 139671 139672 139673 139674 139675 139676 139677 139678 139679 139680 139681 139682 139683 139684 139685 139686 139687 139688 139689 139690 139691 139692 139693 139694 139695 139696 139697 139698 139699 139700 139701 139702 139703 139704 139705 139706 139707 139708 139709 139710 139711 139712 139713 139714 139715 139716 139717 139718 139719 139720 139721 139722 ..."
},
{
"input": "61324",
"output": "183972 183973 183974 183975 183976 183977 183978 183979 183980 183981 183982 183983 183984 183985 183986 183987 183988 183989 183990 183991 183992 183993 183994 183995 183996 183997 183998 183999 184000 184001 184002 184003 184004 184005 184006 184007 184008 184009 184010 184011 184012 184013 184014 184015 184016 184017 184018 184019 184020 184021 184022 184023 184024 184025 184026 184027 184028 184029 184030 184031 184032 184033 184034 184035 184036 184037 184038 184039 184040 184041 184042 184043 184044 ..."
},
{
"input": "13176",
"output": "39528 39529 39530 39531 39532 39533 39534 39535 39536 39537 39538 39539 39540 39541 39542 39543 39544 39545 39546 39547 39548 39549 39550 39551 39552 39553 39554 39555 39556 39557 39558 39559 39560 39561 39562 39563 39564 39565 39566 39567 39568 39569 39570 39571 39572 39573 39574 39575 39576 39577 39578 39579 39580 39581 39582 39583 39584 39585 39586 39587 39588 39589 39590 39591 39592 39593 39594 39595 39596 39597 39598 39599 39600 39601 39602 39603 39604 39605 39606 39607 39608 39609 39610 39611 39612 3..."
},
{
"input": "73274",
"output": "219822 219823 219824 219825 219826 219827 219828 219829 219830 219831 219832 219833 219834 219835 219836 219837 219838 219839 219840 219841 219842 219843 219844 219845 219846 219847 219848 219849 219850 219851 219852 219853 219854 219855 219856 219857 219858 219859 219860 219861 219862 219863 219864 219865 219866 219867 219868 219869 219870 219871 219872 219873 219874 219875 219876 219877 219878 219879 219880 219881 219882 219883 219884 219885 219886 219887 219888 219889 219890 219891 219892 219893 219894 ..."
},
{
"input": "86947",
"output": "260841 260842 260843 260844 260845 260846 260847 260848 260849 260850 260851 260852 260853 260854 260855 260856 260857 260858 260859 260860 260861 260862 260863 260864 260865 260866 260867 260868 260869 260870 260871 260872 260873 260874 260875 260876 260877 260878 260879 260880 260881 260882 260883 260884 260885 260886 260887 260888 260889 260890 260891 260892 260893 260894 260895 260896 260897 260898 260899 260900 260901 260902 260903 260904 260905 260906 260907 260908 260909 260910 260911 260912 260913 ..."
},
{
"input": "26342",
"output": "79026 79027 79028 79029 79030 79031 79032 79033 79034 79035 79036 79037 79038 79039 79040 79041 79042 79043 79044 79045 79046 79047 79048 79049 79050 79051 79052 79053 79054 79055 79056 79057 79058 79059 79060 79061 79062 79063 79064 79065 79066 79067 79068 79069 79070 79071 79072 79073 79074 79075 79076 79077 79078 79079 79080 79081 79082 79083 79084 79085 79086 79087 79088 79089 79090 79091 79092 79093 79094 79095 79096 79097 79098 79099 79100 79101 79102 79103 79104 79105 79106 79107 79108 79109 79110 7..."
},
{
"input": "22345",
"output": "67035 67036 67037 67038 67039 67040 67041 67042 67043 67044 67045 67046 67047 67048 67049 67050 67051 67052 67053 67054 67055 67056 67057 67058 67059 67060 67061 67062 67063 67064 67065 67066 67067 67068 67069 67070 67071 67072 67073 67074 67075 67076 67077 67078 67079 67080 67081 67082 67083 67084 67085 67086 67087 67088 67089 67090 67091 67092 67093 67094 67095 67096 67097 67098 67099 67100 67101 67102 67103 67104 67105 67106 67107 67108 67109 67110 67111 67112 67113 67114 67115 67116 67117 67118 67119 6..."
},
{
"input": "19639",
"output": "58917 58918 58919 58920 58921 58922 58923 58924 58925 58926 58927 58928 58929 58930 58931 58932 58933 58934 58935 58936 58937 58938 58939 58940 58941 58942 58943 58944 58945 58946 58947 58948 58949 58950 58951 58952 58953 58954 58955 58956 58957 58958 58959 58960 58961 58962 58963 58964 58965 58966 58967 58968 58969 58970 58971 58972 58973 58974 58975 58976 58977 58978 58979 58980 58981 58982 58983 58984 58985 58986 58987 58988 58989 58990 58991 58992 58993 58994 58995 58996 58997 58998 58999 59000 59001 5..."
},
{
"input": "12337",
"output": "37011 37012 37013 37014 37015 37016 37017 37018 37019 37020 37021 37022 37023 37024 37025 37026 37027 37028 37029 37030 37031 37032 37033 37034 37035 37036 37037 37038 37039 37040 37041 37042 37043 37044 37045 37046 37047 37048 37049 37050 37051 37052 37053 37054 37055 37056 37057 37058 37059 37060 37061 37062 37063 37064 37065 37066 37067 37068 37069 37070 37071 37072 37073 37074 37075 37076 37077 37078 37079 37080 37081 37082 37083 37084 37085 37086 37087 37088 37089 37090 37091 37092 37093 37094 37095 3..."
},
{
"input": "67989",
"output": "203967 203968 203969 203970 203971 203972 203973 203974 203975 203976 203977 203978 203979 203980 203981 203982 203983 203984 203985 203986 203987 203988 203989 203990 203991 203992 203993 203994 203995 203996 203997 203998 203999 204000 204001 204002 204003 204004 204005 204006 204007 204008 204009 204010 204011 204012 204013 204014 204015 204016 204017 204018 204019 204020 204021 204022 204023 204024 204025 204026 204027 204028 204029 204030 204031 204032 204033 204034 204035 204036 204037 204038 204039 ..."
},
{
"input": "57610",
"output": "172830 172831 172832 172833 172834 172835 172836 172837 172838 172839 172840 172841 172842 172843 172844 172845 172846 172847 172848 172849 172850 172851 172852 172853 172854 172855 172856 172857 172858 172859 172860 172861 172862 172863 172864 172865 172866 172867 172868 172869 172870 172871 172872 172873 172874 172875 172876 172877 172878 172879 172880 172881 172882 172883 172884 172885 172886 172887 172888 172889 172890 172891 172892 172893 172894 172895 172896 172897 172898 172899 172900 172901 172902 ..."
},
{
"input": "63287",
"output": "189861 189862 189863 189864 189865 189866 189867 189868 189869 189870 189871 189872 189873 189874 189875 189876 189877 189878 189879 189880 189881 189882 189883 189884 189885 189886 189887 189888 189889 189890 189891 189892 189893 189894 189895 189896 189897 189898 189899 189900 189901 189902 189903 189904 189905 189906 189907 189908 189909 189910 189911 189912 189913 189914 189915 189916 189917 189918 189919 189920 189921 189922 189923 189924 189925 189926 189927 189928 189929 189930 189931 189932 189933 ..."
},
{
"input": "952",
"output": "2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2..."
},
{
"input": "77840",
"output": "233520 233521 233522 233523 233524 233525 233526 233527 233528 233529 233530 233531 233532 233533 233534 233535 233536 233537 233538 233539 233540 233541 233542 233543 233544 233545 233546 233547 233548 233549 233550 233551 233552 233553 233554 233555 233556 233557 233558 233559 233560 233561 233562 233563 233564 233565 233566 233567 233568 233569 233570 233571 233572 233573 233574 233575 233576 233577 233578 233579 233580 233581 233582 233583 233584 233585 233586 233587 233588 233589 233590 233591 233592 ..."
},
{
"input": "42157",
"output": "126471 126472 126473 126474 126475 126476 126477 126478 126479 126480 126481 126482 126483 126484 126485 126486 126487 126488 126489 126490 126491 126492 126493 126494 126495 126496 126497 126498 126499 126500 126501 126502 126503 126504 126505 126506 126507 126508 126509 126510 126511 126512 126513 126514 126515 126516 126517 126518 126519 126520 126521 126522 126523 126524 126525 126526 126527 126528 126529 126530 126531 126532 126533 126534 126535 126536 126537 126538 126539 126540 126541 126542 126543 ..."
},
{
"input": "46375",
"output": "139125 139126 139127 139128 139129 139130 139131 139132 139133 139134 139135 139136 139137 139138 139139 139140 139141 139142 139143 139144 139145 139146 139147 139148 139149 139150 139151 139152 139153 139154 139155 139156 139157 139158 139159 139160 139161 139162 139163 139164 139165 139166 139167 139168 139169 139170 139171 139172 139173 139174 139175 139176 139177 139178 139179 139180 139181 139182 139183 139184 139185 139186 139187 139188 139189 139190 139191 139192 139193 139194 139195 139196 139197 ..."
},
{
"input": "55142",
"output": "165426 165427 165428 165429 165430 165431 165432 165433 165434 165435 165436 165437 165438 165439 165440 165441 165442 165443 165444 165445 165446 165447 165448 165449 165450 165451 165452 165453 165454 165455 165456 165457 165458 165459 165460 165461 165462 165463 165464 165465 165466 165467 165468 165469 165470 165471 165472 165473 165474 165475 165476 165477 165478 165479 165480 165481 165482 165483 165484 165485 165486 165487 165488 165489 165490 165491 165492 165493 165494 165495 165496 165497 165498 ..."
},
{
"input": "60299",
"output": "180897 180898 180899 180900 180901 180902 180903 180904 180905 180906 180907 180908 180909 180910 180911 180912 180913 180914 180915 180916 180917 180918 180919 180920 180921 180922 180923 180924 180925 180926 180927 180928 180929 180930 180931 180932 180933 180934 180935 180936 180937 180938 180939 180940 180941 180942 180943 180944 180945 180946 180947 180948 180949 180950 180951 180952 180953 180954 180955 180956 180957 180958 180959 180960 180961 180962 180963 180964 180965 180966 180967 180968 180969 ..."
},
{
"input": "28862",
"output": "86586 86587 86588 86589 86590 86591 86592 86593 86594 86595 86596 86597 86598 86599 86600 86601 86602 86603 86604 86605 86606 86607 86608 86609 86610 86611 86612 86613 86614 86615 86616 86617 86618 86619 86620 86621 86622 86623 86624 86625 86626 86627 86628 86629 86630 86631 86632 86633 86634 86635 86636 86637 86638 86639 86640 86641 86642 86643 86644 86645 86646 86647 86648 86649 86650 86651 86652 86653 86654 86655 86656 86657 86658 86659 86660 86661 86662 86663 86664 86665 86666 86667 86668 86669 86670 8..."
},
{
"input": "48251",
"output": "144753 144754 144755 144756 144757 144758 144759 144760 144761 144762 144763 144764 144765 144766 144767 144768 144769 144770 144771 144772 144773 144774 144775 144776 144777 144778 144779 144780 144781 144782 144783 144784 144785 144786 144787 144788 144789 144790 144791 144792 144793 144794 144795 144796 144797 144798 144799 144800 144801 144802 144803 144804 144805 144806 144807 144808 144809 144810 144811 144812 144813 144814 144815 144816 144817 144818 144819 144820 144821 144822 144823 144824 144825 ..."
},
{
"input": "2",
"output": "6 7"
},
{
"input": "4",
"output": "12 13 14 15"
}
] | 92 | 3,379,200 | -1 | 6,333 |
|
932 | Permutation Cycle | [
"brute force",
"constructive algorithms"
] | null | null | For a permutation *P*[1... *N*] of integers from 1 to *N*, function *f* is defined as follows:
Let *g*(*i*) be the minimum positive integer *j* such that *f*(*i*,<=*j*)<==<=*i*. We can show such *j* always exists.
For given *N*,<=*A*,<=*B*, find a permutation *P* of integers from 1 to *N* such that for 1<=β€<=*i*<=β€<=*N*, *g*(*i*) equals either *A* or *B*. | The only line contains three integers *N*,<=*A*,<=*B* (1<=β€<=*N*<=β€<=106,<=1<=β€<=*A*,<=*B*<=β€<=*N*). | If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to *N*. | [
"9 2 5\n",
"3 2 1\n"
] | [
"6 5 8 3 4 1 9 2 7",
"1 2 3 "
] | In the first example, *g*(1)β=β*g*(6)β=β*g*(7)β=β*g*(9)β=β2 and *g*(2)β=β*g*(3)β=β*g*(4)β=β*g*(5)β=β*g*(8)β=β5
In the second example, *g*(1)β=β*g*(2)β=β*g*(3)β=β1 | [
{
"input": "9 2 5",
"output": "2 1 4 3 6 7 8 9 5 "
},
{
"input": "3 2 1",
"output": "1 2 3 "
},
{
"input": "7 4 4",
"output": "-1"
},
{
"input": "1000000 999998 3",
"output": "-1"
},
{
"input": "1 1 1",
"output": "1 "
},
{
"input": "993012 997 1001",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "1000000 2017 881",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "390612 20831 55790",
"output": "-1"
},
{
"input": "689292 69319 96267",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "99929 99929 2",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "807990 72713 11616",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "514004 50866 26101",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "631610 7702 63553",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "391861 47354 60383",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "822954 53638 55936",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "794948 794948 85946",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "786009 37429 59524",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "402440 201220 220895",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "701502 342867 350751",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "865746 865746 634846",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "562825 562825 145593",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "960677 797144 960677",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "228456 38076 136364",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "465111 297688 155037",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "1000000 3 999997",
"output": "2 3 1 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "474441 99291 77277",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "542226 90371 64993",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "911106 51038 78188",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "800577 56373 62017",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "667141 63085 50338",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "321361 79845 81826",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "439365 78717 87873",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "436061 59464 79277",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "482184 56941 83597",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "253274 82704 85285",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "679275 59632 75475",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "279013 56717 52145",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "91401 88756 91401",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "414372 59196 93713",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "482120 96424 93248",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "505383 77277 99291",
"output": "-1"
},
{
"input": "276681 90371 92227",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "201292 78188 62600",
"output": "-1"
},
{
"input": "223899 74633 69608",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "726152 70146 71567",
"output": "-1"
},
{
"input": "432613 95501 84278",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "383151 97630 81017",
"output": "-1"
},
{
"input": "663351 51961 83597",
"output": "-1"
},
{
"input": "255855 82704 85285",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "210582 59632 75475",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "422699 52145 56717",
"output": "-1"
},
{
"input": "734965 91401 69490",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "732687 59196 63663",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "432316 96424 86324",
"output": "-1"
},
{
"input": "674504 89149 64156",
"output": "-1"
},
{
"input": "449238 72357 77951",
"output": "-1"
},
{
"input": "500754 60855 65493",
"output": "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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "510382 53668 84117",
"output": "-1"
},
{
"input": "536156 82311 68196",
"output": "-1"
},
{
"input": "620908 51298 77886",
"output": "-1"
},
{
"input": "9 7 9",
"output": "2 3 4 5 6 7 8 9 1 "
},
{
"input": "10 7 5",
"output": "2 3 4 5 1 7 8 9 10 6 "
},
{
"input": "4 3 2",
"output": "2 1 4 3 "
},
{
"input": "5 4 5",
"output": "2 3 4 5 1 "
},
{
"input": "5 3 4",
"output": "-1"
},
{
"input": "1000000 3 3",
"output": "-1"
},
{
"input": "999999 2 4",
"output": "-1"
},
{
"input": "1000000 1 500001",
"output": "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 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..."
},
{
"input": "999999 2 2",
"output": "-1"
}
] | 2,000 | 30,105,600 | 0 | 6,340 |
|
0 | none | [
"none"
] | null | null | Vasya should paint a fence in front of his own cottage. The fence is a sequence of *n* wooden boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number the board fence using numbers 1,<=2,<=...,<=*n* from left to right. The height of the *i*-th board is *h**i* centimeters.
Vasya has a 1 centimeter wide brush and the paint of two colors, red and green. Of course, the amount of the paint is limited. Vasya counted the area he can paint each of the colors. It turned out that he can not paint over *a* square centimeters of the fence red, and he can not paint over *b* square centimeters green. Each board of the fence should be painted exactly one of the two colors. Perhaps Vasya won't need one of the colors.
In addition, Vasya wants his fence to look smart. To do this, he should paint the fence so as to minimize the value that Vasya called the fence unattractiveness value. Vasya believes that two consecutive fence boards, painted different colors, look unattractive. The unattractiveness value of a fence is the total length of contact between the neighboring boards of various colors. To make the fence look nice, you need to minimize the value as low as possible. Your task is to find what is the minimum unattractiveness Vasya can get, if he paints his fence completely.
The picture shows the fence, where the heights of boards (from left to right) are 2,3,2,4,3,1. The first and the fifth boards are painted red, the others are painted green. The first and the second boards have contact length 2, the fourth and fifth boards have contact length 3, the fifth and the sixth have contact length 1. Therefore, the unattractiveness of the given painted fence is 2+3+1=6. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=200) β the number of boards in Vasya's fence.
The second line contains two integers *a* and *b* (0<=β€<=*a*,<=*b*<=β€<=4Β·104) β the area that can be painted red and the area that can be painted green, correspondingly.
The third line contains a sequence of *n* integers *h*1,<=*h*2,<=...,<=*h**n* (1<=β€<=*h**i*<=β€<=200) β the heights of the fence boards.
All numbers in the lines are separated by single spaces. | Print a single number β the minimum unattractiveness value Vasya can get if he paints his fence completely. If it is impossible to do, print <=-<=1. | [
"4\n5 7\n3 3 4 1\n",
"3\n2 3\n1 3 1\n",
"3\n3 3\n2 2 2\n"
] | [
"3\n",
"2\n",
"-1\n"
] | none | [] | 92 | 0 | 0 | 6,343 |
|
213 | Numbers | [
"combinatorics",
"dp"
] | null | null | Furik loves writing all sorts of problems, especially such that he can't solve himself. You've got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it.
There is integer *n* and array *a*, consisting of ten integers, indexed by numbers from 0 to 9. Your task is to count the number of positive integers with the following properties:
- the number's length does not exceed *n*; - the number doesn't have leading zeroes; - digit *i* (0<=β€<=*i*<=β€<=9) occurs in the number at least *a*[*i*] times. | The first line contains integer *n* (1<=β€<=*n*<=β€<=100). The next line contains 10 integers *a*[0], *a*[1], ..., *a*[9] (0<=β€<=*a*[*i*]<=β€<=100) β elements of array *a*. The numbers are separated by spaces. | On a single line print the remainder of dividing the answer to the problem by 1000000007 (109<=+<=7). | [
"1\n0 0 0 0 0 0 0 0 0 1\n",
"2\n1 1 0 0 0 0 0 0 0 0\n",
"3\n1 1 0 0 0 0 0 0 0 0\n"
] | [
"1\n",
"1\n",
"36\n"
] | In the first sample number 9 meets the requirements.
In the second sample number 10 meets the requirements.
In the third sample numbers 10, 110, 210, 120, 103 meet the requirements. There are other suitable numbers, 36 in total. | [
{
"input": "1\n0 0 0 0 0 0 0 0 0 1",
"output": "1"
},
{
"input": "2\n1 1 0 0 0 0 0 0 0 0",
"output": "1"
},
{
"input": "3\n1 1 0 0 0 0 0 0 0 0",
"output": "36"
},
{
"input": "4\n0 1 0 1 2 0 0 0 0 0",
"output": "12"
},
{
"input": "5\n2 1 2 0 0 0 0 0 0 0",
"output": "18"
},
{
"input": "6\n1 1 0 1 2 1 0 0 0 0",
"output": "300"
},
{
"input": "7\n0 0 2 2 1 0 1 0 0 0",
"output": "9660"
},
{
"input": "8\n1 0 1 0 1 1 2 2 0 0",
"output": "8820"
},
{
"input": "9\n1 1 1 2 1 0 0 2 0 1",
"output": "80640"
},
{
"input": "10\n2 0 0 1 0 2 0 1 1 0",
"output": "46501116"
},
{
"input": "100\n10 11 14 16 12 17 10 10 0 0",
"output": "5806772"
},
{
"input": "100\n0 0 0 0 0 0 0 0 0 0",
"output": "226732709"
},
{
"input": "100\n12 7 8 5 17 1 19 5 7 9",
"output": "72317872"
},
{
"input": "100\n15 16 10 9 11 7 18 14 0 0",
"output": "657295203"
},
{
"input": "100\n1 12 0 7 16 19 15 2 17 11",
"output": "94324764"
},
{
"input": "100\n19 9 15 16 9 10 15 7 0 0",
"output": "965593411"
},
{
"input": "100\n12 11 2 10 18 15 10 2 8 9",
"output": "2861328"
},
{
"input": "100\n5 3 15 14 9 4 11 2 0 6",
"output": "20742041"
},
{
"input": "100\n12 2 12 4 12 7 18 18 13 2",
"output": "213099632"
},
{
"input": "100\n7 12 8 18 13 1 1 19 13 8",
"output": "570613710"
},
{
"input": "100\n13 3 4 7 2 15 6 12 7 6",
"output": "765010290"
},
{
"input": "100\n0 45 0 0 0 0 0 55 0 0",
"output": "742404204"
},
{
"input": "100\n9 0 3 23 0 0 0 0 0 65",
"output": "417270431"
},
{
"input": "100\n0 0 19 0 0 0 27 0 0 54",
"output": "697702662"
},
{
"input": "100\n0 0 68 0 18 14 0 0 0 0",
"output": "31893604"
},
{
"input": "100\n0 34 12 0 16 0 0 0 38 0",
"output": "425145859"
},
{
"input": "100\n0 45 29 0 0 4 0 0 0 22",
"output": "53914825"
},
{
"input": "100\n32 0 0 0 0 0 0 67 1 0",
"output": "916165184"
},
{
"input": "100\n58 0 0 0 0 0 40 2 0 0",
"output": "61389954"
},
{
"input": "100\n0 27 0 0 0 0 0 0 73 0",
"output": "739250810"
},
{
"input": "100\n0 0 40 0 0 0 0 0 60 0",
"output": "213157642"
},
{
"input": "100\n0 24 0 0 0 29 25 22 0 0",
"output": "830465544"
},
{
"input": "100\n4 0 1 15 20 0 0 34 0 26",
"output": "873619937"
},
{
"input": "100\n30 0 8 19 0 1 11 0 0 31",
"output": "428927538"
},
{
"input": "100\n31 0 27 15 7 9 5 0 0 6",
"output": "338317227"
},
{
"input": "100\n1 14 5 6 7 27 13 0 27 0",
"output": "636666417"
},
{
"input": "100\n5 18 12 0 0 2 15 0 8 40",
"output": "280146328"
},
{
"input": "100\n13 34 0 0 0 0 19 27 1 6",
"output": "989464034"
},
{
"input": "100\n24 0 0 0 0 36 16 24 0 0",
"output": "386276754"
},
{
"input": "100\n0 27 0 0 0 0 22 21 30 0",
"output": "362638820"
},
{
"input": "100\n0 2 23 27 0 23 0 0 24 1",
"output": "974134889"
},
{
"input": "100\n6 6 7 5 9 8 9 6 7 9",
"output": "896625890"
},
{
"input": "100\n17 18 19 13 26 22 26 17 19 26",
"output": "0"
},
{
"input": "100\n3 24 1 12 29 27 27 25 5 20",
"output": "0"
},
{
"input": "100\n23 18 6 14 10 7 8 5 1 24",
"output": "0"
},
{
"input": "100\n23 10 21 11 6 7 10 19 11 4",
"output": "0"
},
{
"input": "100\n5 18 12 5 28 2 15 20 12 40",
"output": "0"
},
{
"input": "100\n13 34 34 12 11 29 26 27 1 6",
"output": "0"
},
{
"input": "100\n24 9 23 26 28 36 16 24 39 36",
"output": "0"
},
{
"input": "100\n16 27 26 10 17 39 22 21 30 25",
"output": "0"
},
{
"input": "100\n18 2 23 27 9 23 27 13 24 39",
"output": "0"
},
{
"input": "100\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "55\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "82\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "80\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "74\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "70\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "96\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "14\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "46\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "57\n100 100 100 100 100 100 100 100 100 100",
"output": "0"
},
{
"input": "100\n100 0 0 0 0 0 0 0 0 0",
"output": "0"
},
{
"input": "100\n0 100 0 0 0 0 0 0 0 0",
"output": "1"
},
{
"input": "100\n0 0 100 0 0 0 0 0 0 0",
"output": "1"
},
{
"input": "100\n0 0 0 0 0 100 0 0 0 0",
"output": "1"
},
{
"input": "100\n0 0 0 0 0 0 100 0 0 0",
"output": "1"
},
{
"input": "100\n0 0 0 0 0 0 0 100 0 0",
"output": "1"
},
{
"input": "100\n0 0 0 0 0 0 0 0 100 0",
"output": "1"
},
{
"input": "100\n0 0 0 0 0 0 0 0 0 100",
"output": "1"
},
{
"input": "100\n50 0 0 0 0 50 0 0 0 0",
"output": "769496025"
},
{
"input": "100\n2 2 2 3 2 3 2 3 1 2",
"output": "962893731"
},
{
"input": "100\n2 2 2 3 2 3 2 3 1 2",
"output": "962893731"
},
{
"input": "100\n2 1 1 1 3 0 3 1 1 1",
"output": "824639681"
},
{
"input": "100\n3 3 2 1 2 1 3 3 0 1",
"output": "824583946"
},
{
"input": "100\n0 2 0 1 3 3 3 0 3 3",
"output": "714175595"
},
{
"input": "100\n3 1 3 3 1 2 3 2 0 2",
"output": "230289012"
},
{
"input": "100\n2 2 0 2 3 3 2 0 1 1",
"output": "40065169"
},
{
"input": "100\n1 0 3 2 1 0 2 0 0 1",
"output": "366089372"
},
{
"input": "100\n2 0 2 0 2 1 3 3 2 1",
"output": "40065169"
},
{
"input": "100\n1 2 1 3 2 0 0 3 2 2",
"output": "886460596"
},
{
"input": "100\n2 0 0 1 0 3 1 2 1 1",
"output": "93799192"
},
{
"input": "6\n1 1 1 1 1 1 0 0 0 0",
"output": "600"
},
{
"input": "4\n0 0 1 0 1 0 0 0 1 1",
"output": "24"
},
{
"input": "6\n0 1 0 1 1 0 0 0 1 0",
"output": "23160"
},
{
"input": "4\n1 1 1 0 1 0 0 0 0 0",
"output": "18"
},
{
"input": "5\n1 1 1 0 1 1 0 0 0 0",
"output": "96"
},
{
"input": "6\n2 2 2 0 0 0 0 0 0 0",
"output": "60"
},
{
"input": "4\n1 1 2 0 0 0 0 0 0 0",
"output": "9"
},
{
"input": "77\n2 2 3 2 3 2 3 1 2 2",
"output": "296754123"
},
{
"input": "69\n1 1 3 0 3 1 1 1 3 3",
"output": "441116461"
},
{
"input": "76\n1 2 1 3 3 0 1 0 2 0",
"output": "434673284"
}
] | 92 | 0 | 0 | 6,350 |
|
75 | Facetook Priority Wall | [
"expression parsing",
"implementation",
"strings"
] | B. Facetook Priority Wall | 2 | 256 | Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described).
This priority factor will be affected by three types of actions:
- 1. "*X* posted on *Y*'s wall" (15 points), - 2. "*X* commented on *Y*'s post" (10 points), - 3. "*X* likes *Y*'s post" (5 points).
*X* and *Y* will be two distinct names. And each action will increase the priority factor between *X* and *Y* (and vice versa) by the above value of points (the priority factor between *X* and *Y* is the same as the priority factor between *Y* and *X*).
You will be given *n* actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. | The first line contains your name. The second line contains an integer *n*, which is the number of actions (1<=β€<=*n*<=β€<=100). Then *n* lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. | Print *m* lines, where *m* is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order.
Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor.
The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line *a* is lexicographically smaller than the line *b*, if either *a* is the prefix of *b*, or if exists such an *i* (1<=β€<=*i*<=β€<=*min*(|*a*|,<=|*b*|)), that *a**i*<=<<=*b**i*, and for any *j* (1<=β€<=*j*<=<<=*i*) *a**j*<==<=*b**j*, where |*a*| and |*b*| stand for the lengths of strings *a* and *b* correspondently. | [
"ahmed\n3\nahmed posted on fatma's wall\nfatma commented on ahmed's post\nmona likes ahmed's post\n",
"aba\n1\nlikes likes posted's post\n"
] | [
"fatma\nmona\n",
"likes\nposted\n"
] | none | [
{
"input": "ahmed\n3\nahmed posted on fatma's wall\nfatma commented on ahmed's post\nmona likes ahmed's post",
"output": "fatma\nmona"
},
{
"input": "aba\n1\nlikes likes posted's post",
"output": "likes\nposted"
},
{
"input": "nu\n5\ng commented on pwyndmh's post\nqv posted on g's wall\ng likes nu's post\ng posted on nu's wall\nqv commented on pwyndmh's post",
"output": "g\npwyndmh\nqv"
},
{
"input": "szfwtzfp\n5\nzqx posted on szfwtzfp's wall\nr commented on scguem's post\nr posted on civ's wall\nr likes scguem's post\nr likes scguem's post",
"output": "zqx\nciv\nr\nscguem"
},
{
"input": "oaquudhavr\n3\ni posted on cwfwujpc's wall\ni likes oaquudhavr's post\noaquudhavr commented on cwfwujpc's post",
"output": "cwfwujpc\ni"
},
{
"input": "eo\n4\neo commented on xkgjgwxtrx's post\neo posted on iqquh's wall\nn commented on xkgjgwxtrx's post\niqquh commented on n's post",
"output": "iqquh\nxkgjgwxtrx\nn"
},
{
"input": "plwun\n3\neusjuq commented on plwun's post\nagktgdar likes eusjuq's post\nagppcoil likes agktgdar's post",
"output": "eusjuq\nagktgdar\nagppcoil"
},
{
"input": "fgzrn\n3\nzhl likes fgzrn's post\nxryet likes fgzrn's post\nzhl commented on fgzrn's post",
"output": "zhl\nxryet"
},
{
"input": "qatugmdjwg\n3\nb posted on cf's wall\nyjxkat posted on b's wall\nko commented on qatugmdjwg's post",
"output": "ko\nb\ncf\nyjxkat"
},
{
"input": "dagwdwxsuf\n5\nesrvncb commented on dagwdwxsuf's post\nzcepigpbz posted on dagwdwxsuf's wall\nesrvncb commented on zcepigpbz's post\nesrvncb commented on dagwdwxsuf's post\ndagwdwxsuf commented on esrvncb's post",
"output": "esrvncb\nzcepigpbz"
},
{
"input": "a\n1\nb likes c's post",
"output": "b\nc"
},
{
"input": "a\n1\nc likes b's post",
"output": "b\nc"
},
{
"input": "wuaiz\n10\nmnbggnud posted on xttaqvel's wall\ns posted on xopffmspf's wall\nkysxb likes qnrtpzkh's post\ngptks likes quebtsup's post\nkgmd commented on kmtnhsiue's post\newqjtxtiyn commented on a's post\nol posted on iglplaj's wall\nif posted on yuo's wall\nfs posted on dwjtuhgrq's wall\nygmdprun likes tzfneuly's post",
"output": "a\ndwjtuhgrq\newqjtxtiyn\nfs\ngptks\nif\niglplaj\nkgmd\nkmtnhsiue\nkysxb\nmnbggnud\nol\nqnrtpzkh\nquebtsup\ns\ntzfneuly\nxopffmspf\nxttaqvel\nygmdprun\nyuo"
},
{
"input": "fzhzg\n11\nv likes xyf's post\nktqtpzhlh commented on ffsxarrn's post\nktqtpzhlh commented on lbt's post\njcdwpcycj commented on qbuigcgflm's post\nl likes pmg's post\nracszbmsk posted on ojr's wall\nojr commented on n's post\nnzqx commented on lkj's post\nv posted on lzoca's wall\nnwqnoham commented on gyivezpu's post\nfzhzg likes uqvzgzrpac's post",
"output": "uqvzgzrpac\nffsxarrn\ngyivezpu\njcdwpcycj\nktqtpzhlh\nl\nlbt\nlkj\nlzoca\nn\nnwqnoham\nnzqx\nojr\npmg\nqbuigcgflm\nracszbmsk\nv\nxyf"
},
{
"input": "qdrnpb\n12\nymklhj commented on dkcbo's post\nhcucrenckl posted on mut's wall\nnvkyta commented on eo's post\npvgow likes mut's post\nob likes wlwcxtf's post\npvgow commented on advpu's post\nkfflyfbr commented on igozjnrxw's post\nsq commented on qdrnpb's post\nmrvn posted on lahduc's wall\ngsnlicy likes u's post\ndltqujf commented on qgzk's post\nr posted on bey's wall",
"output": "sq\nadvpu\nbey\ndkcbo\ndltqujf\neo\ngsnlicy\nhcucrenckl\nigozjnrxw\nkfflyfbr\nlahduc\nmrvn\nmut\nnvkyta\nob\npvgow\nqgzk\nr\nu\nwlwcxtf\nymklhj"
},
{
"input": "biycvwb\n13\nhp likes cigobksf's post\nmcoqt commented on gaswzwat's post\nnz posted on xyvetbokl's wall\nqbnwy commented on ylkfbwjy's post\nqdwktrro likes rxgujnzecs's post\nbbsw commented on hwtatkfnps's post\ngspx posted on ugjxfnahuc's wall\nxlmut likes plle's post\numbwlleag commented on xfwlhen's post\nrlwxqksbwi commented on rypqtrgf's post\nbj posted on vovq's wall\nozpdpb commented on zti's post\nhqj posted on rxgujnzecs's wall",
"output": "bbsw\nbj\ncigobksf\ngaswzwat\ngspx\nhp\nhqj\nhwtatkfnps\nmcoqt\nnz\nozpdpb\nplle\nqbnwy\nqdwktrro\nrlwxqksbwi\nrxgujnzecs\nrypqtrgf\nugjxfnahuc\numbwlleag\nvovq\nxfwlhen\nxlmut\nxyvetbokl\nylkfbwjy\nzti"
},
{
"input": "kmircqsffq\n14\nfrnf likes xgmmp's post\nfnfdpupayp commented on syz's post\nxefshpn commented on xgmmp's post\nm posted on gdwydzktok's wall\neskm likes pqmbnuc's post\npnqiapduhz likes zzqvjdz's post\nx likes nouuurc's post\nvnyxhoukuo posted on uhblapjab's wall\nblpjpxn likes zvwbger's post\nj posted on vuknetvl's wall\nscsw commented on xaggwxlxe's post\npqmbnuc commented on ojwaibie's post\niaazdlqdew commented on kmircqsffq's post\nqznqshxdi commented on umdqztoqun's post",
"output": "iaazdlqdew\nblpjpxn\neskm\nfnfdpupayp\nfrnf\ngdwydzktok\nj\nm\nnouuurc\nojwaibie\npnqiapduhz\npqmbnuc\nqznqshxdi\nscsw\nsyz\nuhblapjab\numdqztoqun\nvnyxhoukuo\nvuknetvl\nx\nxaggwxlxe\nxefshpn\nxgmmp\nzvwbger\nzzqvjdz"
},
{
"input": "posted\n3\nposted posted on fatma's wall\nfatma commented on posted's post\nmona likes posted's post",
"output": "fatma\nmona"
},
{
"input": "posted\n3\nposted posted on wall's wall\nwall commented on posted's post\nmona likes posted's post",
"output": "wall\nmona"
},
{
"input": "posted\n3\nposted posted on wall's wall\nwall commented on posted's post\npost likes posted's post",
"output": "wall\npost"
},
{
"input": "wall\n5\nwall posted on posted's wall\nwall posted on on's wall\nwall posted on commented's wall\nwall posted on likes's wall\nwall posted on post's wall",
"output": "commented\nlikes\non\npost\nposted"
},
{
"input": "commented\n5\non commented on commented's post\npos commented on commented's post\nlikes commented on commented's post\nposted commented on commented's post\nwall commented on commented's post",
"output": "likes\non\npos\nposted\nwall"
},
{
"input": "likes\n3\nlikes posted on post's wall\nlikes commented on on's post\nlikes likes commented's post",
"output": "post\non\ncommented"
},
{
"input": "on\n4\non posted on posted's wall\non commented on commented's post\non posted on wall's wall\non commented on post's post",
"output": "posted\nwall\ncommented\npost"
},
{
"input": "wall\n9\nwall posted on posted's wall\non commented on wall's post\nwall likes post's post\nposted posted on wall's wall\nwall commented on post's post\nlikes likes wall's post\nwall posted on on's wall\npost commented on wall's post\nwall likes likes's post",
"output": "posted\non\npost\nlikes"
},
{
"input": "post\n9\npost posted on wall's wall\non commented on post's post\npost likes likes's post\ncommented posted on post's wall\npost commented on likes's post\nlikes likes post's post\npost posted on posted's wall\non commented on post's post\npost likes commented's post",
"output": "commented\nlikes\non\nposted\nwall"
},
{
"input": "ahmed\n9\npost posted on ahmeds's wall\nahmeds commented on post's post\npost likes ahmeds's post\nahmeds posted on post's wall\npost commented on ahmeds's post\nahmeds likes post's post\npost posted on ahmeds's wall\nahmeds commented on post's post\npost likes ahmeds's post",
"output": "ahmeds\npost"
}
] | 62 | 102,400 | 0 | 6,353 |
182 | Common Divisors | [
"brute force",
"hashing",
"implementation",
"math",
"strings"
] | null | null | Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String *a* is the divisor of string *b* if and only if there exists a positive integer *x* such that if we write out string *a* consecutively *x* times, we get string *b*. For example, string "abab" has two divisors β "ab" and "abab".
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him. | The first input line contains a non-empty string *s*1.
The second input line contains a non-empty string *s*2.
Lengths of strings *s*1 and *s*2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters. | Print the number of common divisors of strings *s*1 and *s*2. | [
"abcdabcd\nabcdabcdabcdabcd\n",
"aaa\naa\n"
] | [
"2\n",
"1\n"
] | In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | [
{
"input": "abcdabcd\nabcdabcdabcdabcd",
"output": "2"
},
{
"input": "aaa\naa",
"output": "1"
},
{
"input": "aaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaa",
"output": "3"
},
{
"input": "aaaaaaaaaaaaaa\naaaaaaaaaaaaaa",
"output": "4"
},
{
"input": "a\nb",
"output": "0"
},
{
"input": "a\na",
"output": "1"
},
{
"input": "ab\nac",
"output": "0"
},
{
"input": "asdkjjaskldjklasjdhasjdasdas\nasdjahsgdjslkdaygsudhasdkasnjdbayusvduasdklmaklsd",
"output": "0"
},
{
"input": "aaa\naaaaab",
"output": "0"
},
{
"input": "ab\naa",
"output": "0"
},
{
"input": "aa\naac",
"output": "0"
},
{
"input": "aba\nabaaba",
"output": "1"
},
{
"input": "aa\nbb",
"output": "0"
},
{
"input": "abababab\ncdcdcdcd",
"output": "0"
},
{
"input": "ab\nab",
"output": "1"
},
{
"input": "abcabcabc\nertert",
"output": "0"
},
{
"input": "aaaa\nbbbb",
"output": "0"
},
{
"input": "abc\ncde",
"output": "0"
},
{
"input": "abc\nabcabcab",
"output": "0"
},
{
"input": "aba\naaa",
"output": "0"
},
{
"input": "abcabc\nabdabdabd",
"output": "0"
},
{
"input": "aaaaaa\naaaaaaaaa",
"output": "2"
},
{
"input": "aaa\nbbb",
"output": "0"
}
] | 62 | 204,800 | 0 | 6,360 |
|
514 | R2D2 and Droid Army | [
"binary search",
"data structures",
"two pointers"
] | null | null | An army of *n* droids is lined up in one row. Each droid is described by *m* integers *a*1,<=*a*2,<=...,<=*a**m*, where *a**i* is the number of details of the *i*-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has *m* weapons, the *i*-th weapon can affect all the droids in the army by destroying one detail of the *i*-th type (if the droid doesn't have details of this type, nothing happens to it).
A droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most *k* shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length? | The first line contains three integers *n*,<=*m*,<=*k* (1<=β€<=*n*<=β€<=105, 1<=β€<=*m*<=β€<=5, 0<=β€<=*k*<=β€<=109) β the number of droids, the number of detail types and the number of available shots, respectively.
Next *n* lines follow describing the droids. Each line contains *m* integers *a*1,<=*a*2,<=...,<=*a**m* (0<=β€<=*a**i*<=β€<=108), where *a**i* is the number of details of the *i*-th type for the respective robot. | Print *m* space-separated integers, where the *i*-th number is the number of shots from the weapon of the *i*-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length.
If there are multiple optimal solutions, print any of them.
It is not necessary to make exactly *k* shots, the number of shots can be less. | [
"5 2 4\n4 0\n1 2\n2 1\n0 2\n1 3\n",
"3 2 4\n1 2\n1 3\n2 2\n"
] | [
"2 2\n",
"1 3\n"
] | In the first test the second, third and fourth droids will be destroyed.
In the second test the first and second droids will be destroyed. | [
{
"input": "5 2 4\n4 0\n1 2\n2 1\n0 2\n1 3",
"output": "2 2"
},
{
"input": "3 2 4\n1 2\n1 3\n2 2",
"output": "1 3"
},
{
"input": "1 1 0\n0",
"output": "0"
},
{
"input": "1 1 0\n1",
"output": "0"
},
{
"input": "1 1 1\n0",
"output": "0"
},
{
"input": "4 5 33\n2 10 2 3 2\n10 6 4 5 0\n3 1 7 3 2\n4 4 2 1 5",
"output": "10 6 7 5 5"
},
{
"input": "4 5 40\n0 10 9 0 4\n10 5 5 7 4\n9 9 5 5 2\n6 7 9 4 3",
"output": "10 10 9 7 4"
},
{
"input": "31 2 1913\n845 576\n862 325\n914 283\n431 837\n193 171\n30 248\n290 488\n810 552\n463 74\n765 469\n785 119\n107 267\n528 761\n583 395\n359 45\n840 559\n147 510\n882 830\n267 390\n639 47\n849 312\n518 6\n643 828\n195 886\n377 948\n333 841\n484 99\n486 999\n134 342\n736 490\n624 677",
"output": "914 999"
},
{
"input": "49 2 1971\n794 866\n401 575\n341 83\n103 208\n352 134\n260 878\n497 931\n630 570\n885 464\n23 663\n60 775\n416 870\n955 405\n392 961\n530 258\n73 404\n736 923\n44 436\n594 314\n904 138\n980 163\n76 720\n879 809\n81 838\n263 599\n218 139\n659 493\n848 754\n656 302\n490 7\n204 530\n184 758\n114 849\n80 649\n653 439\n961 350\n104 387\n482 441\n628 972\n451 503\n367 926\n50 332\n855 991\n528 261\n131 447\n551 841\n963 962\n253 979\n700 218",
"output": "980 991"
},
{
"input": "1 5 264566327\n35969364 23665385 95351498 57005692 52574389",
"output": "0 0 0 0 0"
},
{
"input": "1 5 240247522\n48772229 15259125 4834706 88633344 82748118",
"output": "48772229 15259125 4834706 88633344 82748118"
},
{
"input": "1 5 211500111\n18123011 48739518 50372830 20177276 74087475",
"output": "18123011 48739518 50372830 20177276 74087475"
}
] | 46 | 0 | 0 | 6,386 |
|
353 | Queue | [
"constructive algorithms",
"dp"
] | null | null | There are *n* schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes.
Each second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginning of the line). In other words, if at some time the *i*-th position has a boy and the (*i*<=+<=1)-th position has a girl, then in a second, the *i*-th position will have a girl and the (*i*<=+<=1)-th one will have a boy.
Let's take an example of a line of four people: a boy, a boy, a girl, a girl (from the beginning to the end of the line). Next second the line will look like that: a boy, a girl, a boy, a girl. Next second it will be a girl, a boy, a girl, a boy. Next second it will be a girl, a girl, a boy, a boy. The line won't change any more.
Your task is: given the arrangement of the children in the line to determine the time needed to move all girls in front of boys (in the example above it takes 3 seconds). Baking buns takes a lot of time, so no one leaves the line until the line stops changing. | The first line contains a sequence of letters without spaces *s*1*s*2... *s**n* (1<=β€<=*n*<=β€<=106), consisting of capital English letters M and F. If letter *s**i* equals M, that means that initially, the line had a boy on the *i*-th position. If letter *s**i* equals F, then initially the line had a girl on the *i*-th position. | Print a single integer β the number of seconds needed to move all the girls in the line in front of the boys. If the line has only boys or only girls, print 0. | [
"MFM\n",
"MMFF\n",
"FFMMM\n"
] | [
"1\n",
"3\n",
"0\n"
] | In the first test case the sequence of changes looks as follows: MFM βββ FMM.
The second test sample corresponds to the sample from the statement. The sequence of changes is: MMFF βββ MFMF βββ FMFM βββ FFMM. | [
{
"input": "MFM",
"output": "1"
},
{
"input": "MMFF",
"output": "3"
},
{
"input": "FFMMM",
"output": "0"
},
{
"input": "MMFMMFFFFM",
"output": "7"
},
{
"input": "MFFFMMFMFMFMFFFMMMFFMMMMMMFMMFFMMMFMMFMFFFMMFMMMFFMMFFFFFMFMFFFMMMFFFMFMFMFMFFFMMMMFMMFMMFFMMMMMMFFM",
"output": "54"
},
{
"input": "MFFMFMFFMM",
"output": "5"
},
{
"input": "MFFFFFMFFM",
"output": "7"
},
{
"input": "MMMFMFFFFF",
"output": "8"
},
{
"input": "MMMMMFFMFMFMFMMFMMFFMMFMFFFFFFFMFFFMMMMMMFFMMMFMFMMFMFFMMFMMMFFFFFMMMMMFMMMMFMMMFFMFFMFFFMFFMFFMMFFM",
"output": "58"
},
{
"input": "MMMMFMMMMMFFMMFMFMMMFMMFMFMMFFFMMFMMMFFFMMMFMFFMFMMFFMFMFMFFMMMFMMFMFMFFFMFMFFFMFFMMMMFFFFFFFMMMFMFM",
"output": "59"
},
{
"input": "MMMMFFFMMFMFMFMFFMMFFMFMFFFFFFFFFFFMMFFFFMFFFFFMFFMFFMMMFFMFFFFFFMFMMMMFMFFMFMFMMFFMFMFMFFFMMFMFFFFF",
"output": "65"
},
{
"input": "MFMMFMF",
"output": "4"
},
{
"input": "MFMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMFMMMMMMMMMMMMFMMMMMMMMMMMMMMMMMMMMMMMM",
"output": "50"
},
{
"input": "FFFMFMMMMMMFMMMMMFFMFMMFMMFMMFFMMMMMMFFMFMMFFFFMFMMFFFMMFFMFMFMFFMMFMMMFMMFFM",
"output": "45"
},
{
"input": "F",
"output": "0"
},
{
"input": "M",
"output": "0"
},
{
"input": "FF",
"output": "0"
},
{
"input": "FM",
"output": "0"
},
{
"input": "MF",
"output": "1"
},
{
"input": "MM",
"output": "0"
}
] | 0 | 0 | -1 | 6,398 |
|
986 | Perfect Encoding | [
"fft",
"math"
] | null | null | You are working as an analyst in a company working on a new system for big data storage. This system will store $n$ different objects. Each object should have a unique ID.
To create the system, you choose the parameters of the systemΒ β integers $m \ge 1$ and $b_{1}, b_{2}, \ldots, b_{m}$. With these parameters an ID of some object in the system is an array of integers $[a_{1}, a_{2}, \ldots, a_{m}]$ where $1 \le a_{i} \le b_{i}$ holds for every $1 \le i \le m$.
Developers say that production costs are proportional to $\sum_{i=1}^{m} b_{i}$. You are asked to choose parameters $m$ and $b_{i}$ so that the system will be able to assign unique IDs to $n$ different objects and production costs are minimized. Note that you don't have to use all available IDs. | In the only line of input there is one positive integer $n$. The length of the decimal representation of $n$ is no greater than $1.5 \cdot 10^{6}$. The integer does not contain leading zeros. | Print one numberΒ β minimal value of $\sum_{i=1}^{m} b_{i}$. | [
"36\n",
"37\n",
"12345678901234567890123456789\n"
] | [
"10\n",
"11\n",
"177\n"
] | none | [
{
"input": "36",
"output": "10"
},
{
"input": "37",
"output": "11"
},
{
"input": "12345678901234567890123456789",
"output": "177"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "2"
},
{
"input": "3",
"output": "3"
},
{
"input": "4",
"output": "4"
},
{
"input": "7421902501252475186372406731932548506197390793597574544727433297197476846519276598727359617092494798",
"output": "629"
},
{
"input": "71057885893313745806894531138592341136175030511382512555364579061229040750815096670263802546201989828165866147027119861863385397179695224216202346062872417111920113483747119385957051753101263769591892062039112567316036455789217245754461225443096439906225767290690128677713047690686004149082311677134836383178262318973298581951974863511315252485252083010690948164456205330279738760034861583874764199950445592461479109814313530332776429627014232776723160331462731018692207739471347664936326394313671025",
"output": "3144"
},
{
"input": "515377520732011331036461129765621272702107522001",
"output": "300"
},
{
"input": "515377520732011331036461129765621272702107522002",
"output": "301"
},
{
"input": "515377520732011331036461129765621272702107522000",
"output": "300"
},
{
"input": "2644141638961613273780910519504288731930844065504296335329840736453657194693409799081556627701216123927819555393745164711901909164201237823730685450515907348055240450396641607756029548457929682548780800235177236082257895631246188876123132346108173348981012356250960688811094108794077791634930736509832272441660537127557164580456832796615775793837112808169797875218746484343692719877391033530037881176218120852179342877728205628700771297494331664021228732264346205537805710440002",
"output": "3002"
}
] | 249 | 5,939,200 | 3 | 6,400 |
|
343 | Rational Resistance | [
"math",
"number theory"
] | null | null | Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.
However, all Mike has is lots of identical resistors with unit resistance *R*0<==<=1. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements:
1. one resistor; 1. an element and one resistor plugged in sequence; 1. an element and one resistor plugged in parallel.
With the consecutive connection the resistance of the new element equals *R*<==<=*R**e*<=+<=*R*0. With the parallel connection the resistance of the new element equals . In this case *R**e* equals the resistance of the element being connected.
Mike needs to assemble an element with a resistance equal to the fraction . Determine the smallest possible number of resistors he needs to make such an element. | The single input line contains two space-separated integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=1018). It is guaranteed that the fraction is irreducible. It is guaranteed that a solution always exists. | Print a single number β the answer to the problem.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specifier. | [
"1 1\n",
"3 2\n",
"199 200\n"
] | [
"1\n",
"3\n",
"200\n"
] | In the first sample, one resistor is enough.
In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5305da389756aab6423d918a08ced468f05604df.png" style="max-width: 100.0%;max-height: 100.0%;"/>. We cannot make this element using two resistors. | [
{
"input": "1 1",
"output": "1"
},
{
"input": "3 2",
"output": "3"
},
{
"input": "199 200",
"output": "200"
},
{
"input": "1 1000000000000000000",
"output": "1000000000000000000"
},
{
"input": "3 1",
"output": "3"
},
{
"input": "21 8",
"output": "7"
},
{
"input": "18 55",
"output": "21"
},
{
"input": "1 2",
"output": "2"
},
{
"input": "2 1",
"output": "2"
},
{
"input": "1 3",
"output": "3"
},
{
"input": "2 3",
"output": "3"
},
{
"input": "1 4",
"output": "4"
},
{
"input": "5 2",
"output": "4"
},
{
"input": "2 5",
"output": "4"
},
{
"input": "4 5",
"output": "5"
},
{
"input": "3 5",
"output": "4"
},
{
"input": "13 4",
"output": "7"
},
{
"input": "21 17",
"output": "9"
},
{
"input": "5 8",
"output": "5"
},
{
"input": "13 21",
"output": "7"
},
{
"input": "74 99",
"output": "28"
},
{
"input": "2377 1055",
"output": "33"
},
{
"input": "645597 134285",
"output": "87"
},
{
"input": "29906716 35911991",
"output": "92"
},
{
"input": "3052460231 856218974",
"output": "82"
},
{
"input": "288565475053 662099878640",
"output": "88"
},
{
"input": "11504415412768 12754036168327",
"output": "163"
},
{
"input": "9958408561221547 4644682781404278",
"output": "196"
},
{
"input": "60236007668635342 110624799949034113",
"output": "179"
},
{
"input": "4 43470202936783249",
"output": "10867550734195816"
},
{
"input": "16 310139055712567491",
"output": "19383690982035476"
},
{
"input": "15 110897893734203629",
"output": "7393192915613582"
},
{
"input": "439910263967866789 38",
"output": "11576585893891241"
},
{
"input": "36 316049483082136289",
"output": "8779152307837131"
},
{
"input": "752278442523506295 52",
"output": "14466893125452056"
},
{
"input": "4052739537881 6557470319842",
"output": "62"
},
{
"input": "44945570212853 72723460248141",
"output": "67"
},
{
"input": "498454011879264 806515533049393",
"output": "72"
},
{
"input": "8944394323791464 5527939700884757",
"output": "77"
},
{
"input": "679891637638612258 420196140727489673",
"output": "86"
},
{
"input": "1 923438",
"output": "923438"
},
{
"input": "3945894354376 1",
"output": "3945894354376"
},
{
"input": "999999999999999999 5",
"output": "200000000000000004"
},
{
"input": "999999999999999999 1000000000000000000",
"output": "1000000000000000000"
},
{
"input": "999999999999999991 1000000000000000000",
"output": "111111111111111120"
},
{
"input": "999999999999999993 999999999999999991",
"output": "499999999999999998"
},
{
"input": "3 1000000000000000000",
"output": "333333333333333336"
},
{
"input": "1000000000000000000 3",
"output": "333333333333333336"
},
{
"input": "10000000000 1000000001",
"output": "100000019"
},
{
"input": "2 999999999999999999",
"output": "500000000000000001"
},
{
"input": "999999999999999999 2",
"output": "500000000000000001"
},
{
"input": "2 1000000001",
"output": "500000002"
},
{
"input": "123 1000000000000000000",
"output": "8130081300813023"
}
] | 122 | 0 | 0 | 6,403 |
|
45 | Codecraft III | [
"implementation"
] | A. Codecraft III | 2 | 256 | Today Vasya visited a widely known site and learned that the continuation of his favourite game Codecraft II will appear after exactly *k* months. He looked at the calendar and learned that at the moment is the month number *s*. Vasya immediately got interested in what month Codecraft III will appear. Help him understand that.
All the twelve months in Vasya's calendar are named using their usual English names: January, February, March, April, May, June, July, August, September, October, November, December. | The first input line contains the name of the current month. It is guaranteed that it is a proper English name of one of twelve months. The first letter is uppercase, the rest are lowercase. The second line contains integer *k* (0<=β€<=*k*<=β€<=100) β the number of months left till the appearance of Codecraft III. | Print starting from an uppercase letter the name of the month in which the continuation of Codeforces II will appear. The printed name must be contained in the list January, February, March, April, May, June, July, August, September, October, November, December. | [
"November\n3\n",
"May\n24\n"
] | [
"February\n",
"May\n"
] | none | [
{
"input": "November\n3",
"output": "February"
},
{
"input": "May\n24",
"output": "May"
},
{
"input": "April\n0",
"output": "April"
},
{
"input": "September\n0",
"output": "September"
},
{
"input": "August\n0",
"output": "August"
},
{
"input": "June\n1",
"output": "July"
},
{
"input": "July\n2",
"output": "September"
},
{
"input": "September\n3",
"output": "December"
},
{
"input": "July\n4",
"output": "November"
},
{
"input": "August\n24",
"output": "August"
},
{
"input": "May\n48",
"output": "May"
},
{
"input": "November\n47",
"output": "October"
},
{
"input": "December\n49",
"output": "January"
},
{
"input": "June\n99",
"output": "September"
},
{
"input": "March\n100",
"output": "July"
},
{
"input": "December\n1",
"output": "January"
},
{
"input": "January\n11",
"output": "December"
},
{
"input": "December\n0",
"output": "December"
},
{
"input": "January\n0",
"output": "January"
},
{
"input": "July\n77",
"output": "December"
},
{
"input": "February\n11",
"output": "January"
},
{
"input": "February\n22",
"output": "December"
},
{
"input": "July\n33",
"output": "April"
},
{
"input": "May\n44",
"output": "January"
},
{
"input": "June\n97",
"output": "July"
}
] | 186 | 0 | 0 | 6,424 |
57 | Martian Architecture | [
"implementation"
] | B. Martian Architecture | 2 | 256 | Chris the Rabbit found the traces of an ancient Martian civilization. The brave astronomer managed to see through a small telescope an architecture masterpiece β "A Road to the Sun". The building stands on cubical stones of the same size. The foundation divides the entire "road" into cells, into which the cubical stones are fit tightly. Thus, to any cell of the foundation a coordinate can be assigned. To become the leader of the tribe, a Martian should build a Road to the Sun, that is to build from those cubical stones on a given foundation a stairway. The stairway should be described by the number of stones in the initial coordinate and the coordinates of the stairway's beginning and end. Each following cell in the coordinate's increasing order should contain one cubical stone more than the previous one. At that if the cell has already got stones, they do not count in this building process, the stairways were simply built on them. In other words, let us assume that a stairway is built with the initial coordinate of *l*, the final coordinate of *r* and the number of stones in the initial coordinate *x*. That means that *x* stones will be added in the cell *l*, *x*<=+<=1 stones will be added in the cell *l*<=+<=1, ..., *x*<=+<=*r*<=-<=*l* stones will be added in the cell *r*.
Chris managed to find an ancient manuscript, containing the descriptions of all the stairways. Now he wants to compare the data to be sure that he has really found "A Road to the Sun". For that he chose some road cells and counted the total number of cubical stones that has been accumulated throughout the Martian history and then asked you to count using the manuscript to what the sum should ideally total. | The first line contains three space-separated integers: *n*,<=*m*,<=*k* (1<=β€<=*n*,<=*m*<=β€<=105,<=1<=β€<=*k*<=β€<=*min*(*n*,<=100)) which is the number of cells, the number of "Roads to the Sun" and the number of cells in the query correspondingly. Each of the following *m* roads contain three space-separated integers: *a**i*,<=*b**i*,<=*c**i* (1<=β€<=*a**i*<=β€<=*b**i*<=β€<=*n*,<=1<=β€<=*c**i*<=β€<=1000) which are the stairway's description, its beginning, end and the initial cell's height. Then follow a line, containing *k* different space-separated integers *b**i*. All these numbers ranging from 1 to *n* are cells, the number of stones in which interests Chris. | You have to print a single number on a single line which is the sum of stones in all the cells Chris is interested in.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). | [
"5 2 1\n1 5 1\n2 4 1\n3\n",
"3 2 1\n1 3 1\n1 3 1\n2\n",
"3 2 1\n1 3 1\n1 3 1\n3\n"
] | [
"5\n",
"4\n",
"6\n"
] | none | [
{
"input": "5 2 1\n1 5 1\n2 4 1\n3",
"output": "5"
},
{
"input": "3 2 1\n1 3 1\n1 3 1\n2",
"output": "4"
},
{
"input": "3 2 1\n1 3 1\n1 3 1\n3",
"output": "6"
},
{
"input": "10 10 3\n6 10 74\n7 9 35\n3 6 63\n2 4 80\n2 10 78\n10 10 13\n4 10 16\n1 2 13\n3 7 17\n4 6 67\n9 8 10",
"output": "635"
},
{
"input": "10 10 6\n5 9 618\n4 8 717\n4 7 815\n7 10 22\n4 6 186\n8 10 205\n2 6 424\n5 10 267\n2 3 512\n1 6 967\n5 8 9 1 3 2",
"output": "11745"
},
{
"input": "10 10 1\n4 9 6\n4 5 5\n1 5 6\n4 5 6\n8 10 5\n7 10 1\n2 3 6\n4 8 4\n5 8 7\n7 8 6\n1",
"output": "6"
},
{
"input": "100 10 19\n75 86 9\n3 16 49\n12 94 58\n67 78 98\n32 54 30\n34 36 46\n39 89 4\n7 75 38\n6 85 73\n11 66 79\n91 69 65 43 82 19 96 50 97 20 58 49 8 83 85 73 70 56 57",
"output": "6666"
},
{
"input": "272 10 10\n16 29 2\n43 269 2\n146 265 2\n198 267 8\n38 140 3\n172 187 1\n2 172 4\n142 230 1\n12 199 5\n15 27 8\n70 3 190 83 81 17 16 148 15 107",
"output": "1994"
},
{
"input": "986 10 20\n249 846 37\n110 335 5\n170 568 34\n399 668 30\n118 832 42\n294 336 62\n335 713 60\n245 671 18\n263 512 61\n94 417 75\n63 641 553 61 818 339 254 784 673 572 932 657 432 257 335 971 223 548 758 79",
"output": "21557"
}
] | 2,000 | 8,806,400 | 0 | 6,466 |
958 | Hyperspace Jump (easy) | [
"expression parsing",
"math"
] | null | null | The Rebel fleet is on the run. It consists of *m* ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form .
To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope! | The first line of the input contains a single integer *m* (1<=β€<=*m*<=β€<=200<=000) β the number of ships. The next *m* lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer *a* of up to two decimal digits, a plus sign +, a positive integer *b* of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer *c* of up to two decimal digits. | Print a single line consisting of *m* space-separated integers. The *i*-th integer should be equal to the number of ships whose coordinate is equal to that of the *i*-th ship (including the *i*-th ship itself). | [
"4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7\n"
] | [
"1 2 2 1 "
] | In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | [
{
"input": "4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7",
"output": "1 2 2 1 "
},
{
"input": "10\n(44+98)/19\n(36+58)/47\n(62+74)/68\n(69+95)/82\n(26+32)/29\n(32+46)/39\n(32+24)/28\n(47+61)/54\n(39+13)/26\n(98+98)/98",
"output": "1 9 9 9 9 9 9 9 9 9 "
},
{
"input": "30\n(89+76)/87\n(81+78)/18\n(60+97)/32\n(41+14)/48\n(55+65)/27\n(29+15)/95\n(64+13)/96\n(78+30)/75\n(43+6)/60\n(69+34)/48\n(62+2)/97\n(85+42)/3\n(4+97)/42\n(1+18)/39\n(46+55)/76\n(22+59)/24\n(62+81)/98\n(64+8)/51\n(9+59)/48\n(47+2)/80\n(33+74)/76\n(61+83)/44\n(86+4)/51\n(65+41)/49\n(53+36)/45\n(6+19)/15\n(51+21)/68\n(98+36)/86\n(92+65)/86\n(27+58)/78",
"output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "
}
] | 608 | 19,763,200 | 3 | 6,477 |
|
94 | Friends | [
"graphs",
"implementation",
"math"
] | B. Friends | 1 | 256 | One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something.
The following statement caught his attention: "Among any six people there are either three pairwise acquainted people or three pairwise unacquainted people"
Igor just couldn't get why the required minimum is 6 people. "Well, that's the same for five people, too!" β he kept on repeating in his mind. β "Let's take, say, Max, Ilya, Vova β here, they all know each other! And now let's add Dima and Oleg to Vova β none of them is acquainted with each other! Now, that math is just rubbish!"
Igor K. took 5 friends of his and wrote down who of them is friends with whom. Now he wants to check whether it is true for the five people that among them there are either three pairwise acquainted or three pairwise not acquainted people. | The first line contains an integer *m* (0<=β€<=*m*<=β€<=10), which is the number of relations of acquaintances among the five friends of Igor's.
Each of the following *m* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=5;*a**i*<=β <=*b**i*), where (*a**i*,<=*b**i*) is a pair of acquainted people. It is guaranteed that each pair of the acquaintances is described exactly once. The acquaintance relation is symmetrical, i.e. if *x* is acquainted with *y*, then *y* is also acquainted with *x*. | Print "FAIL", if among those five people there are no either three pairwise acquainted or three pairwise unacquainted people. Otherwise print "WIN". | [
"4\n1 3\n2 3\n1 4\n5 3\n",
"5\n1 2\n2 3\n3 4\n4 5\n5 1\n"
] | [
"WIN\n",
"FAIL\n"
] | none | [
{
"input": "4\n1 3\n2 3\n1 4\n5 3",
"output": "WIN"
},
{
"input": "5\n1 2\n2 3\n3 4\n4 5\n5 1",
"output": "FAIL"
},
{
"input": "1\n4 3",
"output": "WIN"
},
{
"input": "6\n1 3\n2 3\n1 2\n5 3\n4 2\n4 5",
"output": "WIN"
},
{
"input": "2\n1 3\n2 5",
"output": "WIN"
},
{
"input": "3\n5 3\n4 3\n4 5",
"output": "WIN"
},
{
"input": "5\n1 3\n3 2\n2 4\n5 4\n1 5",
"output": "FAIL"
},
{
"input": "7\n1 3\n5 1\n1 4\n2 1\n5 3\n4 5\n2 5",
"output": "WIN"
},
{
"input": "5\n5 1\n4 1\n2 3\n4 5\n3 1",
"output": "WIN"
},
{
"input": "0",
"output": "WIN"
},
{
"input": "10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5",
"output": "WIN"
},
{
"input": "4\n1 2\n2 3\n3 4\n4 1",
"output": "WIN"
},
{
"input": "1\n2 1",
"output": "WIN"
},
{
"input": "1\n2 5",
"output": "WIN"
},
{
"input": "2\n2 1\n1 5",
"output": "WIN"
},
{
"input": "2\n4 2\n1 5",
"output": "WIN"
},
{
"input": "2\n3 4\n5 2",
"output": "WIN"
},
{
"input": "2\n1 5\n4 3",
"output": "WIN"
},
{
"input": "3\n4 1\n4 5\n2 1",
"output": "WIN"
},
{
"input": "3\n5 1\n5 3\n2 5",
"output": "WIN"
},
{
"input": "3\n1 2\n4 2\n1 3",
"output": "WIN"
},
{
"input": "3\n3 2\n1 5\n5 3",
"output": "WIN"
},
{
"input": "3\n1 2\n2 4\n3 2",
"output": "WIN"
},
{
"input": "3\n2 1\n1 3\n5 4",
"output": "WIN"
},
{
"input": "4\n4 2\n2 5\n1 4\n4 5",
"output": "WIN"
},
{
"input": "4\n5 2\n2 4\n5 3\n1 5",
"output": "WIN"
},
{
"input": "4\n2 5\n1 3\n4 3\n4 2",
"output": "WIN"
},
{
"input": "4\n1 4\n3 1\n2 3\n1 2",
"output": "WIN"
},
{
"input": "4\n5 4\n2 3\n1 5\n5 2",
"output": "WIN"
},
{
"input": "4\n2 5\n5 4\n1 4\n5 3",
"output": "WIN"
},
{
"input": "4\n2 1\n2 4\n5 1\n4 1",
"output": "WIN"
},
{
"input": "4\n1 2\n1 5\n4 5\n2 3",
"output": "WIN"
},
{
"input": "5\n4 1\n2 4\n3 2\n5 3\n1 5",
"output": "FAIL"
},
{
"input": "5\n1 3\n4 1\n5 2\n2 4\n3 5",
"output": "FAIL"
},
{
"input": "5\n3 5\n4 2\n1 3\n2 1\n5 4",
"output": "FAIL"
},
{
"input": "5\n5 2\n1 3\n4 5\n2 1\n3 4",
"output": "FAIL"
},
{
"input": "5\n2 3\n3 5\n1 2\n4 1\n5 4",
"output": "FAIL"
},
{
"input": "5\n1 2\n4 5\n5 3\n3 1\n2 4",
"output": "FAIL"
},
{
"input": "5\n5 3\n3 2\n2 4\n1 5\n4 1",
"output": "FAIL"
},
{
"input": "5\n3 2\n4 1\n2 5\n1 3\n5 4",
"output": "FAIL"
},
{
"input": "5\n3 5\n1 4\n5 1\n2 3\n4 2",
"output": "FAIL"
},
{
"input": "5\n4 2\n5 3\n2 1\n3 4\n1 5",
"output": "FAIL"
},
{
"input": "5\n3 1\n5 1\n4 5\n2 4\n5 3",
"output": "WIN"
},
{
"input": "5\n5 4\n5 3\n3 1\n1 4\n2 3",
"output": "WIN"
},
{
"input": "5\n4 1\n3 5\n3 4\n5 4\n5 2",
"output": "WIN"
},
{
"input": "5\n4 1\n5 2\n3 1\n4 2\n5 1",
"output": "WIN"
},
{
"input": "5\n2 3\n1 5\n5 3\n2 4\n1 4",
"output": "FAIL"
},
{
"input": "5\n5 4\n5 3\n2 3\n5 2\n5 1",
"output": "WIN"
},
{
"input": "5\n2 4\n3 4\n1 4\n2 1\n3 2",
"output": "WIN"
},
{
"input": "5\n2 3\n3 4\n1 3\n4 1\n5 2",
"output": "WIN"
},
{
"input": "5\n1 2\n2 5\n4 2\n4 3\n3 1",
"output": "WIN"
},
{
"input": "5\n2 1\n2 5\n4 5\n2 3\n3 5",
"output": "WIN"
},
{
"input": "5\n4 1\n5 1\n5 4\n4 3\n5 2",
"output": "WIN"
},
{
"input": "5\n1 3\n2 4\n1 5\n5 2\n4 1",
"output": "WIN"
},
{
"input": "5\n1 5\n3 5\n2 3\n4 1\n3 1",
"output": "WIN"
},
{
"input": "5\n5 2\n3 2\n2 1\n4 3\n4 2",
"output": "WIN"
},
{
"input": "5\n1 3\n4 5\n3 4\n3 5\n5 1",
"output": "WIN"
},
{
"input": "5\n4 5\n2 5\n5 3\n4 2\n4 1",
"output": "WIN"
},
{
"input": "5\n2 5\n1 5\n1 3\n3 5\n1 2",
"output": "WIN"
},
{
"input": "5\n2 4\n1 2\n5 2\n5 3\n4 5",
"output": "WIN"
},
{
"input": "5\n2 1\n4 5\n5 3\n1 5\n1 4",
"output": "WIN"
},
{
"input": "5\n1 3\n2 5\n4 2\n3 4\n4 1",
"output": "WIN"
},
{
"input": "6\n3 2\n2 4\n3 1\n3 5\n5 2\n1 2",
"output": "WIN"
},
{
"input": "6\n2 1\n5 1\n5 4\n3 5\n3 4\n4 1",
"output": "WIN"
},
{
"input": "6\n3 1\n1 4\n5 4\n2 1\n4 2\n1 5",
"output": "WIN"
},
{
"input": "6\n5 1\n5 4\n3 4\n1 3\n1 4\n4 2",
"output": "WIN"
},
{
"input": "6\n1 3\n5 4\n4 2\n2 1\n4 1\n2 3",
"output": "WIN"
},
{
"input": "6\n4 3\n5 3\n4 1\n1 3\n1 2\n2 4",
"output": "WIN"
},
{
"input": "6\n4 1\n3 5\n4 5\n3 1\n4 3\n5 2",
"output": "WIN"
},
{
"input": "6\n2 1\n1 4\n4 5\n5 2\n1 3\n3 2",
"output": "WIN"
},
{
"input": "7\n5 1\n3 5\n2 5\n4 5\n2 3\n3 1\n4 3",
"output": "WIN"
},
{
"input": "7\n5 3\n5 1\n4 2\n4 5\n3 2\n3 4\n1 3",
"output": "WIN"
},
{
"input": "7\n3 5\n1 4\n5 2\n1 5\n1 3\n4 2\n4 3",
"output": "WIN"
},
{
"input": "7\n5 1\n5 4\n2 4\n2 3\n3 5\n2 5\n4 1",
"output": "WIN"
},
{
"input": "7\n1 3\n2 5\n4 3\n2 1\n2 3\n4 5\n2 4",
"output": "WIN"
},
{
"input": "7\n3 1\n4 5\n3 5\n5 1\n2 4\n1 2\n1 4",
"output": "WIN"
},
{
"input": "8\n1 5\n3 1\n2 5\n4 2\n2 1\n4 5\n4 3\n4 1",
"output": "WIN"
},
{
"input": "8\n4 2\n3 1\n4 3\n2 5\n3 2\n4 5\n1 2\n3 5",
"output": "WIN"
},
{
"input": "8\n2 4\n3 2\n2 5\n3 4\n3 1\n5 1\n4 5\n5 3",
"output": "WIN"
},
{
"input": "8\n2 3\n1 5\n1 3\n4 5\n2 4\n1 4\n3 5\n3 4",
"output": "WIN"
},
{
"input": "9\n3 5\n3 2\n1 5\n4 3\n5 4\n1 4\n1 3\n4 2\n5 2",
"output": "WIN"
},
{
"input": "9\n3 5\n2 5\n5 1\n4 5\n1 3\n3 2\n1 4\n4 3\n4 2",
"output": "WIN"
},
{
"input": "3\n3 4\n4 5\n5 3",
"output": "WIN"
},
{
"input": "3\n1 2\n1 3\n4 5",
"output": "WIN"
},
{
"input": "3\n2 3\n3 5\n2 5",
"output": "WIN"
}
] | 61 | 0 | 0 | 6,507 |
735 | Urbanization | [
"greedy",
"number theory",
"sortings"
] | null | null | Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are *n* people who plan to move to the cities. The wealth of the *i* of them is equal to *a**i*. Authorities plan to build two cities, first for *n*1 people and second for *n*2 people. Of course, each of *n* candidates can settle in only one of the cities. Thus, first some subset of candidates of size *n*1 settle in the first city and then some subset of size *n*2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home.
To make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth *a**i* among all its residents divided by the number of them (*n*1 or *n*2 depending on the city). The division should be done in real numbers without any rounding.
Please, help authorities find the optimal way to pick residents for two cities. | The first line of the input contains three integers *n*, *n*1 and *n*2 (1<=β€<=*n*,<=*n*1,<=*n*2<=β€<=100<=000, *n*1<=+<=*n*2<=β€<=*n*)Β β the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=100<=000), the *i*-th of them is equal to the wealth of the *i*-th candidate. | Print one real valueΒ β the maximum possible sum of arithmetic means of wealth of cities' residents. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if . | [
"2 1 1\n1 5\n",
"4 2 1\n1 4 2 3\n"
] | [
"6.00000000\n",
"6.50000000\n"
] | In the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second.
In the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (*a*<sub class="lower-index">3</sub>β+β*a*<sub class="lower-index">4</sub>)β/β2β+β*a*<sub class="lower-index">2</sub>β=β(3β+β2)β/β2β+β4β=β6.5 | [
{
"input": "2 1 1\n1 5",
"output": "6.00000000"
},
{
"input": "4 2 1\n1 4 2 3",
"output": "6.50000000"
},
{
"input": "3 1 2\n1 2 3",
"output": "4.50000000"
},
{
"input": "10 4 6\n3 5 7 9 12 25 67 69 83 96",
"output": "88.91666667"
},
{
"input": "19 7 12\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 100000 100000",
"output": "47052.10714286"
},
{
"input": "100 9 6\n109 711 40 95 935 48 228 253 308 726 816 534 252 8 966 363 162 508 84 83 807 506 748 178 45 30 106 108 764 698 825 198 336 353 158 790 64 262 403 334 577 571 742 541 946 602 279 621 910 776 421 886 29 133 114 394 762 965 339 263 750 530 49 80 124 31 322 292 27 590 960 278 111 932 849 491 561 744 469 511 106 271 156 160 836 363 149 473 457 543 976 809 490 29 85 626 265 88 995 946",
"output": "1849.66666667"
},
{
"input": "69 6 63\n53475 22876 79144 6335 33763 79104 65441 45527 65847 94406 74670 43529 75330 19403 67629 56187 57949 23071 64910 54409 55348 18056 855 24961 50565 6622 26467 33989 22660 79469 41246 13965 79706 14422 16075 93378 81313 48173 13470 97348 2346 27452 59427 29925 29847 73823 32021 10988 24609 98855 90919 45939 17203 8439 43007 40138 55693 30314 71734 33458 66850 4011 20089 20546 92090 50842 78859 62756 40177",
"output": "135712.88888889"
},
{
"input": "69 6 9\n2612 17461 69001 33130 10662 85485 88195 45974 16712 81365 67119 87797 15559 20197 74716 92979 97268 49466 68603 48351 99905 35606 54242 98603 68232 54398 82637 49647 38979 46171 54680 23334 15892 92186 69670 29711 67999 2220 32317 717 70667 68262 86760 55720 97158 61122 7251 138 21022 27197 12691 59331 13576 66999 38332 13574 83484 66646 17704 33065 98583 80259 64631 16745 69431 40747 82089 82788 32739",
"output": "183129.44444444"
}
] | 202 | 30,720,000 | 3 | 6,519 |
|
797 | Broken BST | [
"data structures",
"dfs and similar"
] | null | null | Let *T* be arbitrary binary tree β tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent β it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree *T*:
1. Set pointer to the root of a tree. 1. Return success if the value in the current vertex is equal to the number you are looking for 1. Go to the left child of the vertex if the value in the current vertex is greater than the number you are looking for 1. Go to the right child of the vertex if the value in the current vertex is less than the number you are looking for 1. Return fail if you try to go to the vertex that doesn't exist
Here is the pseudo-code of the described algorithm:
The described algorithm works correctly if the tree is binary search tree (i.e. for each node the values of left subtree are less than the value in the node, the values of right subtree are greater than the value in the node). But it can return invalid result if tree is not a binary search tree.
Since the given tree is not necessarily a binary search tree, not all numbers can be found this way. Your task is to calculate, how many times the search will fail being running on every value from the tree.
If the tree has multiple vertices with the same values on them then you should run algorithm on every one of them separately. | First line contains integer number *n* (1<=β€<=*n*<=β€<=105) β number of vertices in the tree.
Each of the next *n* lines contains 3 numbers *v*, *l*, *r* (0<=β€<=*v*<=β€<=109) β value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't exist then number <=-<=1 is set instead. Note that different vertices of the tree may contain the same values. | Print number of times when search algorithm will fail. | [
"3\n15 -1 -1\n10 1 3\n5 -1 -1\n",
"8\n6 2 3\n3 4 5\n12 6 7\n1 -1 8\n4 -1 -1\n5 -1 -1\n14 -1 -1\n2 -1 -1\n"
] | [
"2\n",
"1\n"
] | In the example the root of the tree in vertex 2. Search of numbers 5 and 15 will return fail because on the first step algorithm will choose the subtree which doesn't contain numbers you are looking for. | [
{
"input": "3\n15 -1 -1\n10 1 3\n5 -1 -1",
"output": "2"
},
{
"input": "8\n6 2 3\n3 4 5\n12 6 7\n1 -1 8\n4 -1 -1\n5 -1 -1\n14 -1 -1\n2 -1 -1",
"output": "1"
},
{
"input": "1\n493041212 -1 -1",
"output": "0"
},
{
"input": "10\n921294733 5 9\n341281094 -1 -1\n35060484 10 -1\n363363160 -1 -1\n771156014 6 8\n140806462 -1 -1\n118732846 4 2\n603229555 -1 -1\n359289513 3 7\n423237010 -1 -1",
"output": "7"
},
{
"input": "10\n911605217 -1 -1\n801852416 -1 -1\n140035920 -1 9\n981454947 10 2\n404988051 6 3\n307545107 8 7\n278188888 4 1\n523010786 -1 -1\n441817740 -1 -1\n789680429 -1 -1",
"output": "7"
},
{
"input": "10\n921072710 6 8\n727122964 -1 -1\n248695736 2 -1\n947477665 -1 -1\n41229309 -1 -1\n422047611 3 9\n424558429 -1 4\n665046372 -1 5\n74510531 -1 -1\n630373520 7 1",
"output": "7"
},
{
"input": "1\n815121916 -1 -1",
"output": "0"
},
{
"input": "1\n901418150 -1 -1",
"output": "0"
},
{
"input": "3\n2 -1 -1\n1 1 3\n2 -1 -1",
"output": "0"
},
{
"input": "4\n20 2 3\n16 4 -1\n20 -1 -1\n20 -1 -1",
"output": "0"
},
{
"input": "3\n2 2 3\n1 -1 -1\n1 -1 -1",
"output": "0"
},
{
"input": "4\n7122 2 3\n87 4 -1\n7122 -1 -1\n7122 -1 -1",
"output": "0"
},
{
"input": "4\n712222 2 3\n98887 4 -1\n712222 -1 -1\n712222 -1 -1",
"output": "0"
},
{
"input": "3\n6 2 3\n5 -1 -1\n5 -1 -1",
"output": "0"
},
{
"input": "4\n1 -1 2\n0 3 -1\n100 -1 4\n1 -1 -1",
"output": "2"
},
{
"input": "4\n98 2 3\n95 4 -1\n98 -1 -1\n98 -1 -1",
"output": "0"
},
{
"input": "3\n15 2 3\n1 -1 -1\n1 -1 -1",
"output": "0"
},
{
"input": "4\n6 2 -1\n6 3 4\n6 -1 -1\n7 -1 -1",
"output": "1"
},
{
"input": "3\n2 2 3\n3 -1 -1\n3 -1 -1",
"output": "0"
},
{
"input": "4\n1 -1 2\n0 3 -1\n1 -1 4\n0 -1 -1",
"output": "2"
},
{
"input": "4\n1 2 3\n2 -1 -1\n3 4 -1\n2 -1 -1",
"output": "0"
},
{
"input": "1\n0 -1 -1",
"output": "0"
},
{
"input": "3\n5 2 -1\n6 -1 3\n5 -1 -1",
"output": "1"
},
{
"input": "10\n2 -1 -1\n1 -1 8\n2 4 9\n5 -1 2\n5 7 6\n1 -1 1\n4 -1 -1\n0 -1 -1\n8 5 10\n5 -1 -1",
"output": "3"
},
{
"input": "8\n6 -1 -1\n0 5 -1\n4 6 7\n3 -1 -1\n4 3 1\n1 -1 -1\n2 8 4\n2 -1 -1",
"output": "7"
},
{
"input": "4\n5 3 -1\n1 4 -1\n3 -1 2\n1 -1 -1",
"output": "2"
},
{
"input": "3\n10 2 3\n5 -1 -1\n5 -1 -1",
"output": "0"
}
] | 888 | 31,232,000 | -1 | 6,520 |
|
886 | Maximum Element | [
"combinatorics",
"dp",
"math"
] | null | null | One day Petya was solving a very interesting problem. But although he used many optimization techniques, his solution still got Time limit exceeded verdict. Petya conducted a thorough analysis of his program and found out that his function for finding maximum element in an array of *n* positive integers was too slow. Desperate, Petya decided to use a somewhat unexpected optimization using parameter *k*, so now his function contains the following code:
That way the function iteratively checks array elements, storing the intermediate maximum, and if after *k* consecutive iterations that maximum has not changed, it is returned as the answer.
Now Petya is interested in fault rate of his function. He asked you to find the number of permutations of integers from 1 to *n* such that the return value of his function on those permutations is not equal to *n*. Since this number could be very big, output the answer modulo 109<=+<=7. | The only line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=106), separated by a spaceΒ β the length of the permutations and the parameter *k*. | Output the answer to the problem modulo 109<=+<=7. | [
"5 2\n",
"5 3\n",
"6 3\n"
] | [
"22\n",
"6\n",
"84\n"
] | Permutations from second example:
[4,β1,β2,β3,β5], [4,β1,β3,β2,β5], [4,β2,β1,β3,β5], [4,β2,β3,β1,β5], [4,β3,β1,β2,β5], [4,β3,β2,β1,β5]. | [
{
"input": "5 2",
"output": "22"
},
{
"input": "5 3",
"output": "6"
},
{
"input": "6 3",
"output": "84"
},
{
"input": "5 10",
"output": "0"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "2 1",
"output": "0"
},
{
"input": "2 2",
"output": "0"
},
{
"input": "3 1",
"output": "1"
},
{
"input": "3 2",
"output": "0"
},
{
"input": "3 3",
"output": "0"
},
{
"input": "4 1",
"output": "8"
},
{
"input": "4 2",
"output": "2"
},
{
"input": "4 3",
"output": "0"
},
{
"input": "4 4",
"output": "0"
},
{
"input": "5 1",
"output": "55"
},
{
"input": "5 4",
"output": "0"
},
{
"input": "5 5",
"output": "0"
},
{
"input": "1000000 500000",
"output": "900097839"
},
{
"input": "1000000 1000000",
"output": "0"
},
{
"input": "1000000 1",
"output": "131797017"
},
{
"input": "1 1000000",
"output": "0"
},
{
"input": "959139 199252",
"output": "770937198"
},
{
"input": "9859 748096",
"output": "0"
},
{
"input": "125987 264237",
"output": "0"
},
{
"input": "209411 813081",
"output": "0"
},
{
"input": "325539 329221",
"output": "0"
},
{
"input": "376259 910770",
"output": "0"
},
{
"input": "492387 235422",
"output": "249147139"
},
{
"input": "608515 751563",
"output": "0"
},
{
"input": "691939 300407",
"output": "700547157"
},
{
"input": "30518 196518",
"output": "0"
},
{
"input": "146646 521171",
"output": "0"
},
{
"input": "230070 37311",
"output": "993306535"
},
{
"input": "313494 586155",
"output": "0"
},
{
"input": "396918 167704",
"output": "943821934"
},
{
"input": "513046 683844",
"output": "0"
},
{
"input": "629174 232688",
"output": "831745227"
},
{
"input": "679894 524637",
"output": "655418678"
},
{
"input": "796022 73481",
"output": "883548575"
},
{
"input": "879446 655030",
"output": "563982505"
},
{
"input": "405440 588704",
"output": "0"
}
] | 108 | 15,052,800 | 0 | 6,522 |
|
903 | Almost Difference | [
"data structures",
"math"
] | null | null | Let's denote a function
You are given an array *a* consisting of *n* integers. You have to calculate the sum of *d*(*a**i*,<=*a**j*) over all pairs (*i*,<=*j*) such that 1<=β€<=*i*<=β€<=*j*<=β€<=*n*. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=200000) β the number of elements in *a*.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109) β elements of the array. | Print one integer β the sum of *d*(*a**i*,<=*a**j*) over all pairs (*i*,<=*j*) such that 1<=β€<=*i*<=β€<=*j*<=β€<=*n*. | [
"5\n1 2 3 1 3\n",
"4\n6 6 5 5\n",
"4\n6 6 4 4\n"
] | [
"4\n",
"0\n",
"-8\n"
] | In the first example:
1. *d*(*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">2</sub>)β=β0; 1. *d*(*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">3</sub>)β=β2; 1. *d*(*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">4</sub>)β=β0; 1. *d*(*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">5</sub>)β=β2; 1. *d*(*a*<sub class="lower-index">2</sub>,β*a*<sub class="lower-index">3</sub>)β=β0; 1. *d*(*a*<sub class="lower-index">2</sub>,β*a*<sub class="lower-index">4</sub>)β=β0; 1. *d*(*a*<sub class="lower-index">2</sub>,β*a*<sub class="lower-index">5</sub>)β=β0; 1. *d*(*a*<sub class="lower-index">3</sub>,β*a*<sub class="lower-index">4</sub>)β=ββ-β2; 1. *d*(*a*<sub class="lower-index">3</sub>,β*a*<sub class="lower-index">5</sub>)β=β0; 1. *d*(*a*<sub class="lower-index">4</sub>,β*a*<sub class="lower-index">5</sub>)β=β2. | [
{
"input": "5\n1 2 3 1 3",
"output": "4"
},
{
"input": "4\n6 6 5 5",
"output": "0"
},
{
"input": "4\n6 6 4 4",
"output": "-8"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "1\n1000000000",
"output": "0"
},
{
"input": "2\n1 1000000000",
"output": "999999999"
},
{
"input": "5\n1 999999996 999999998 999999994 1000000000",
"output": "3999999992"
},
{
"input": "100\n7 4 5 5 10 10 5 8 5 7 4 5 4 6 8 8 2 6 3 3 10 7 10 8 6 2 7 3 9 7 7 2 4 5 2 4 9 5 10 1 10 5 10 4 1 3 4 2 6 9 9 9 10 6 2 5 6 1 8 10 4 10 3 4 10 5 5 4 10 4 5 3 7 10 2 7 3 6 9 6 1 6 5 5 4 6 6 4 4 1 5 1 6 6 6 8 8 6 2 6",
"output": "-1774"
},
{
"input": "100\n591 417 888 251 792 847 685 3 182 461 102 348 555 956 771 901 712 878 580 631 342 333 285 899 525 725 537 718 929 653 84 788 104 355 624 803 253 853 201 995 536 184 65 205 540 652 549 777 248 405 677 950 431 580 600 846 328 429 134 983 526 103 500 963 400 23 276 704 570 757 410 658 507 620 984 244 486 454 802 411 985 303 635 283 96 597 855 775 139 839 839 61 219 986 776 72 729 69 20 917",
"output": "-91018"
},
{
"input": "100\n7 8 5 9 5 6 6 9 7 6 8 7 5 10 7 2 6 1 8 10 7 9 9 8 9 6 8 5 10 6 3 7 5 8 9 7 6 1 9 9 6 9 9 2 10 4 4 6 7 9 7 7 9 10 6 10 8 6 4 7 5 5 8 10 10 7 6 9 8 1 5 1 6 6 2 9 8 4 6 6 9 10 6 1 9 9 9 6 1 8 9 2 8 7 1 10 8 2 4 7",
"output": "-1713"
},
{
"input": "100\n82 81 14 33 78 80 15 60 89 82 79 13 15 17 25 13 21 20 63 26 62 63 79 36 18 21 88 92 27 18 59 64 18 96 28 4 76 43 26 25 89 88 96 33 27 97 52 37 92 80 23 18 78 14 88 5 3 14 85 72 84 75 41 3 51 92 91 79 18 78 19 79 8 35 85 86 78 17 51 36 100 32 49 95 2 100 67 72 55 53 42 3 21 100 12 51 50 79 47 2",
"output": "6076"
},
{
"input": "5\n3 1 1 1 3",
"output": "0"
},
{
"input": "1\n22955",
"output": "0"
},
{
"input": "1\n32955",
"output": "0"
}
] | 296 | 35,328,000 | 3 | 6,534 |
|
627 | XOR Equation | [
"dp",
"math"
] | null | null | Two positive integers *a* and *b* have a sum of *s* and a bitwise XOR of *x*. How many possible values are there for the ordered pair (*a*,<=*b*)? | The first line of the input contains two integers *s* and *x* (2<=β€<=*s*<=β€<=1012, 0<=β€<=*x*<=β€<=1012), the sum and bitwise xor of the pair of positive integers, respectively. | Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0. | [
"9 5\n",
"3 3\n",
"5 2\n"
] | [
"4\n",
"2\n",
"0\n"
] | In the first sample, we have the following solutions: (2,β7), (3,β6), (6,β3), (7,β2).
In the second sample, the only solutions are (1,β2) and (2,β1). | [
{
"input": "9 5",
"output": "4"
},
{
"input": "3 3",
"output": "2"
},
{
"input": "5 2",
"output": "0"
},
{
"input": "6 0",
"output": "1"
},
{
"input": "549755813887 549755813887",
"output": "549755813886"
},
{
"input": "2 0",
"output": "1"
},
{
"input": "2 2",
"output": "0"
},
{
"input": "433864631347 597596794426",
"output": "0"
},
{
"input": "80 12",
"output": "4"
},
{
"input": "549755813888 549755813886",
"output": "274877906944"
},
{
"input": "643057379466 24429729346",
"output": "2048"
},
{
"input": "735465350041 356516240229",
"output": "32768"
},
{
"input": "608032203317 318063018433",
"output": "4096"
},
{
"input": "185407964720 148793115916",
"output": "16384"
},
{
"input": "322414792152 285840263184",
"output": "4096"
},
{
"input": "547616456703 547599679487",
"output": "68719476736"
},
{
"input": "274861129991 274861129463",
"output": "34359738368"
},
{
"input": "549688705887 549688703839",
"output": "34359738368"
},
{
"input": "412182675455 412182609919",
"output": "68719476736"
},
{
"input": "552972910589 546530328573",
"output": "17179869184"
},
{
"input": "274869346299 274869346299",
"output": "8589934590"
},
{
"input": "341374319077 341374319077",
"output": "134217726"
},
{
"input": "232040172650 232040172650",
"output": "65534"
},
{
"input": "322373798090 322373798090",
"output": "1048574"
},
{
"input": "18436 18436",
"output": "6"
},
{
"input": "137707749376 137707749376",
"output": "30"
},
{
"input": "9126813696 9126813696",
"output": "6"
},
{
"input": "419432708 419432708",
"output": "62"
},
{
"input": "1839714 248080",
"output": "128"
},
{
"input": "497110 38",
"output": "8"
},
{
"input": "1420572 139928",
"output": "64"
},
{
"input": "583545 583545",
"output": "4094"
},
{
"input": "33411 33411",
"output": "30"
},
{
"input": "66068 66068",
"output": "14"
},
{
"input": "320 320",
"output": "2"
},
{
"input": "1530587 566563",
"output": "256"
},
{
"input": "1988518 108632",
"output": "128"
},
{
"input": "915425594051 155160267299",
"output": "0"
},
{
"input": "176901202458 21535662096",
"output": "0"
},
{
"input": "865893190664 224852444148",
"output": "32768"
},
{
"input": "297044970199 121204864",
"output": "0"
},
{
"input": "241173201018 236676464482",
"output": "0"
},
{
"input": "1582116 139808",
"output": "0"
},
{
"input": "1707011 656387",
"output": "0"
},
{
"input": "169616 132704",
"output": "32"
},
{
"input": "2160101 553812",
"output": "0"
},
{
"input": "1322568 271816",
"output": "0"
},
{
"input": "228503520839 471917524248",
"output": "0"
},
{
"input": "32576550340 504864993495",
"output": "0"
},
{
"input": "910648542843 537125462055",
"output": "0"
},
{
"input": "751720572344 569387893618",
"output": "0"
},
{
"input": "629791564846 602334362179",
"output": "0"
},
{
"input": "1000000000000 1000000000000",
"output": "8190"
},
{
"input": "1000000000000 999999999999",
"output": "0"
},
{
"input": "1000000000000 4",
"output": "0"
},
{
"input": "1000000000000 4096",
"output": "2"
},
{
"input": "3 1",
"output": "0"
},
{
"input": "2097152 0",
"output": "1"
},
{
"input": "40 390",
"output": "0"
},
{
"input": "22212 39957",
"output": "0"
},
{
"input": "128 36",
"output": "0"
},
{
"input": "14 4",
"output": "0"
},
{
"input": "6 2",
"output": "0"
},
{
"input": "43 18467",
"output": "0"
},
{
"input": "7 1",
"output": "0"
},
{
"input": "7 5",
"output": "0"
},
{
"input": "251059 79687",
"output": "0"
},
{
"input": "17 7",
"output": "0"
},
{
"input": "4 6",
"output": "0"
},
{
"input": "2 4",
"output": "0"
},
{
"input": "3 7",
"output": "0"
}
] | 46 | 0 | 0 | 6,538 |
|
706 | Beru-taxi | [
"brute force",
"geometry",
"implementation"
] | null | null | Vasiliy lives at point (*a*,<=*b*) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested *n* available Beru-taxi nearby. The *i*-th taxi is located at point (*x**i*,<=*y**i*) and moves with a speed *v**i*.
Consider that each of *n* drivers will move directly to Vasiliy and with a maximum possible speed. Compute the minimum time when Vasiliy will get in any of Beru-taxi cars. | The first line of the input contains two integers *a* and *b* (<=-<=100<=β€<=*a*,<=*b*<=β€<=100)Β β coordinates of Vasiliy's home.
The second line contains a single integer *n* (1<=β€<=*n*<=β€<=1000)Β β the number of available Beru-taxi cars nearby.
The *i*-th of the following *n* lines contains three integers *x**i*, *y**i* and *v**i* (<=-<=100<=β€<=*x**i*,<=*y**i*<=β€<=100, 1<=β€<=*v**i*<=β€<=100)Β β the coordinates of the *i*-th car and its speed.
It's allowed that several cars are located at the same point. Also, cars may be located at exactly the same point where Vasiliy lives. | Print a single real valueΒ β the minimum time Vasiliy needs to get in any of the Beru-taxi cars. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if . | [
"0 0\n2\n2 0 1\n0 2 2\n",
"1 3\n3\n3 3 2\n-2 3 6\n-2 7 10\n"
] | [
"1.00000000000000000000",
"0.50000000000000000000"
] | In the first sample, first taxi will get to Vasiliy in time 2, and second will do this in time 1, therefore 1 is the answer.
In the second sample, cars 2 and 3 will arrive simultaneously. | [
{
"input": "0 0\n2\n2 0 1\n0 2 2",
"output": "1.00000000000000000000"
},
{
"input": "1 3\n3\n3 3 2\n-2 3 6\n-2 7 10",
"output": "0.50000000000000000000"
},
{
"input": "2 2\n10\n8 10 1\n14 18 5\n2 2 1\n4 2 2\n5 2 1\n0 2 1\n2 10 4\n10 2 4\n14 18 20\n14 18 10",
"output": "0.00000000000000000000"
},
{
"input": "-100 100\n3\n100 100 1\n-100 0 5\n-100 -100 20",
"output": "10.00000000000000000000"
},
{
"input": "5 5\n4\n20 5 1\n20 5 3\n20 5 5\n20 5 15",
"output": "1.00000000000000000000"
},
{
"input": "0 0\n6\n12 0 1\n0 12 12\n12 0 6\n12 0 3\n0 12 4\n12 0 2",
"output": "1.00000000000000000000"
},
{
"input": "0 0\n1\n3 4 5",
"output": "1.00000000000000000000"
},
{
"input": "1 0\n3\n1 1 1\n2 0 1\n3 0 2",
"output": "1.00000000000000000000"
},
{
"input": "95 69\n2\n100 -47 34\n43 80 72",
"output": "0.73820457032879509778"
},
{
"input": "-21 -48\n5\n69 4 95\n86 -44 90\n-51 -23 85\n64 -8 21\n-47 41 82",
"output": "0.45942645152392084672"
},
{
"input": "2 2\n2\n1 3 99\n3 3 100",
"output": "0.01414213562373095049"
},
{
"input": "0 0\n2\n0 1 100\n0 0 1",
"output": "0.00000000000000000000"
},
{
"input": "-24 -35\n19\n7 25 34\n-7 12 17\n-40 2 54\n-60 54 38\n68 -49 8\n-43 -25 25\n-84 -44 21\n4 71 43\n96 -60 66\n-77 62 92\n23 -6 79\n44 67 10\n-21 -26 55\n-82 24 10\n92 55 23\n-82 -40 33\n78 -91 3\n-48 -17 26\n-74 87 18",
"output": "0.17248787237282069083"
},
{
"input": "-56 45\n23\n-79 -82 42\n43 -54 73\n-91 65 54\n-79 -25 36\n40 -22 95\n57 67 31\n-12 -32 37\n-25 95 95\n39 6 24\n96 73 1\n45 -20 35\n-59 50 36\n-49 -18 72\n-74 0 12\n-22 -1 50\n-79 68 13\n-7 -63 27\n-35 3 29\n-95 -54 12\n71 92 76\n25 -90 19\n-95 -66 23\n99 -96 76",
"output": "0.16197088596792501308"
},
{
"input": "-88 -12\n29\n60 -57 48\n52 100 14\n-86 -78 95\n59 -67 2\n-62 59 14\n-71 74 68\n5 -63 21\n-72 14 78\n84 30 35\n-41 -78 15\n-38 34 82\n38 40 57\n99 24 97\n-87 -43 7\n74 -84 14\n-92 4 61\n39 27 22\n86 -88 79\n-39 -83 37\n-93 56 25\n-35 -38 34\n-4 9 90\n-82 -69 54\n-85 31 28\n18 54 71\n89 -3 34\n-78 -81 20\n91 34 43\n34 -30 18",
"output": "0.27036758200771544589"
},
{
"input": "-85 71\n31\n-64 -97 57\n7 41 20\n29 41 85\n27 -81 9\n-63 100 59\n-54 72 66\n-13 -33 36\n89 66 64\n77 -46 54\n86 -58 75\n71 -32 56\n78 -91 74\n-37 69 39\n67 -3 76\n-39 -62 56\n49 16 50\n6 -25 23\n-8 96 34\n14 -81 58\n34 -61 53\n0 77 37\n-27 -27 61\n-37 63 54\n86 12 10\n94 -41 53\n-81 24 49\n-32 81 62\n42 -4 77\n24 70 69\n-51 -19 20\n18 -17 61",
"output": "0.46994128543244917054"
},
{
"input": "-16 -86\n37\n-25 28 67\n-9 -81 61\n9 99 25\n65 77 71\n-91 -19 73\n19 54 8\n-96 36 19\n-58 -15 48\n48 -21 77\n24 -8 1\n88 22 7\n50 100 95\n-65 -90 64\n29 -46 75\n-69 -20 16\n36 28 98\n76 65 13\n-12 81 76\n-6 90 87\n47 5 6\n-35 -72 56\n39 -54 41\n82 -10 28\n-72 47 32\n-48 -60 5\n13 0 66\n-61 -49 61\n21 -90 16\n-65 -85 84\n76 31 45\n-75 84 12\n8 -66 27\n10 -17 16\n45 -26 78\n-78 -24 37\n18 26 22\n99 24 66",
"output": "0.14102172568922338971"
},
{
"input": "-27 -63\n39\n-88 87 70\n86 -89 2\n-57 19 40\n77 -62 67\n9 -34 11\n1 48 16\n-7 17 16\n53 -17 2\n96 96 15\n-31 -16 37\n1 73 89\n-94 -13 3\n17 74 44\n8 -10 4\n30 79 94\n-2 -52 78\n-76 70 40\n-5 -84 25\n-4 -54 69\n-41 -6 27\n38 -13 31\n35 55 59\n-28 24 25\n-74 -67 12\n-79 1 55\n-23 -67 36\n-53 34 67\n22 99 67\n-2 65 32\n10 13 82\n37 -24 27\n-96 -69 11\n14 82 96\n-52 70 26\n1 93 77\n-20 80 44\n-80 8 29\n77 -100 95\n83 -15 89",
"output": "0.15713484026367722764"
},
{
"input": "-24 -5\n41\n-11 46 71\n42 -47 16\n-17 -39 26\n45 -1 74\n-92 -93 57\n18 -55 14\n-24 23 32\n13 -91 88\n90 45 27\n21 -98 1\n9 7 59\n-54 83 29\n83 -82 85\n62 31 72\n19 0 47\n64 60 79\n68 -83 41\n25 25 80\n-52 -51 86\n-14 -24 54\n-29 1 30\n-88 44 37\n-83 55 29\n72 -61 94\n-3 81 33\n-93 -16 51\n-8 -5 9\n49 61 5\n88 40 82\n7 -63 1\n-6 -99 82\n20 81 99\n57 90 46\n27 30 77\n-78 -13 79\n-32 -85 4\n82 55 93\n11 -3 45\n39 -66 43\n-37 44 63\n75 -94 2",
"output": "0.26034165586355514647"
},
{
"input": "66 -82\n43\n27 -21 70\n-64 46 58\n-7 -20 41\n-42 60 57\n-93 -7 95\n26 -61 26\n-10 -72 25\n-78 -18 55\n-62 -64 69\n-33 95 50\n24 59 45\n4 72 37\n66 57 61\n16 -60 5\n17 -78 36\n-75 56 59\n-60 98 77\n-94 37 28\n76 6 63\n99 -35 75\n69 -40 1\n9 -48 56\n-94 -11 19\n69 1 63\n43 42 80\n-28 -83 21\n100 -23 6\n50 49 49\n-26 -12 22\n-34 45 46\n-96 -82 100\n-60 78 88\n35 88 26\n31 25 25\n-98 -97 12\n-70 -31 30\n-71 65 56\n32 26 58\n-80 82 30\n64 34 74\n5 15 2\n-72 -9 46\n68 43 84",
"output": "0.76570954748593223420"
},
{
"input": "0 0\n2\n100 100 1\n100 99 1",
"output": "140.71602609511114245766"
},
{
"input": "1 2\n1\n4 6 3",
"output": "1.66666666666666666663"
},
{
"input": "100 100\n1\n-100 -100 1",
"output": "282.84271247461900974929"
},
{
"input": "4 7\n1\n11 34 17",
"output": "1.64074419776251211988"
},
{
"input": "91 91\n3\n3 3 2\n-2 3 6\n-2 7 10",
"output": "12.53195914452325367139"
},
{
"input": "100 100\n3\n3 3 2\n-2 3 6\n-2 7 10",
"output": "13.80326048439280884911"
},
{
"input": "0 0\n1\n1 5 5",
"output": "1.01980390271855696595"
},
{
"input": "3 6\n2\n18 92 45\n32 94 86",
"output": "1.07738691952544205876"
},
{
"input": "-100 -100\n1\n100 100 1",
"output": "282.84271247461900974929"
},
{
"input": "0 0\n1\n0 31 13",
"output": "2.38461538461538461548"
},
{
"input": "0 0\n1\n1 2 2",
"output": "1.11803398874989484821"
},
{
"input": "0 0\n1\n10 0 6",
"output": "1.66666666666666666663"
},
{
"input": "100 100\n1\n-100 -100 100",
"output": "2.82842712474619009753"
},
{
"input": "-100 -100\n1\n100 100 2",
"output": "141.42135623730950487464"
}
] | 77 | 1,638,400 | 3 | 6,540 |
|
922 | Magic Forest | [
"brute force"
] | null | null | Imp is in a magic forest, where xorangles grow (wut?)
A xorangle of order *n* is such a non-degenerate triangle, that lengths of its sides are integers not exceeding *n*, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of order *n* to get out of the forest.
Formally, for a given integer *n* you have to find the number of such triples (*a*,<=*b*,<=*c*), that:
- 1<=β€<=*a*<=β€<=*b*<=β€<=*c*<=β€<=*n*; - , where denotes the [bitwise xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers *x* and *y*. - (*a*,<=*b*,<=*c*) form a non-degenerate (with strictly positive area) triangle. | The only line contains a single integer *n* (1<=β€<=*n*<=β€<=2500). | Print the number of xorangles of order *n*. | [
"6\n",
"10\n"
] | [
"1\n",
"2\n"
] | The only xorangle in the first sample is (3,β5,β6). | [
{
"input": "6",
"output": "1"
},
{
"input": "10",
"output": "2"
},
{
"input": "3",
"output": "0"
},
{
"input": "4",
"output": "0"
},
{
"input": "5",
"output": "0"
},
{
"input": "2500",
"output": "700393"
},
{
"input": "952",
"output": "118547"
},
{
"input": "88",
"output": "536"
},
{
"input": "1216",
"output": "160822"
},
{
"input": "2140",
"output": "614785"
},
{
"input": "564",
"output": "35087"
},
{
"input": "1488",
"output": "239580"
},
{
"input": "116",
"output": "1332"
},
{
"input": "1040",
"output": "145820"
},
{
"input": "1965",
"output": "545494"
},
{
"input": "593",
"output": "36605"
},
{
"input": "779",
"output": "63500"
},
{
"input": "1703",
"output": "352045"
},
{
"input": "331",
"output": "9877"
},
{
"input": "1051",
"output": "145985"
},
{
"input": "2179",
"output": "618074"
},
{
"input": "603",
"output": "37312"
},
{
"input": "1731",
"output": "369691"
},
{
"input": "2451",
"output": "681980"
},
{
"input": "1079",
"output": "146833"
},
{
"input": "2207",
"output": "621708"
},
{
"input": "2394",
"output": "663240"
},
{
"input": "818",
"output": "73972"
},
{
"input": "1946",
"output": "529383"
},
{
"input": "166",
"output": "2200"
},
{
"input": "1294",
"output": "175915"
},
{
"input": "2218",
"output": "623386"
},
{
"input": "846",
"output": "82106"
},
{
"input": "1566",
"output": "273341"
},
{
"input": "194",
"output": "3240"
},
{
"input": "1322",
"output": "183405"
},
{
"input": "1508",
"output": "247634"
},
{
"input": "2433",
"output": "675245"
},
{
"input": "857",
"output": "85529"
},
{
"input": "1781",
"output": "402718"
},
{
"input": "1",
"output": "0"
},
{
"input": "2",
"output": "0"
},
{
"input": "2444",
"output": "679373"
},
{
"input": "2498",
"output": "699536"
}
] | 93 | 1,945,600 | 3 | 6,550 |
|
483 | Friends and Presents | [
"binary search",
"math"
] | null | null | You have two friends. You want to present each of them several positive integers. You want to present *cnt*1 numbers to the first friend and *cnt*2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number *x*. The second one does not like the numbers that are divisible without remainder by prime number *y*. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number *v*, that you can form presents using numbers from a set 1,<=2,<=...,<=*v*. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself. | The only line contains four positive integers *cnt*1, *cnt*2, *x*, *y* (1<=β€<=*cnt*1,<=*cnt*2<=<<=109; *cnt*1<=+<=*cnt*2<=β€<=109; 2<=β€<=*x*<=<<=*y*<=β€<=3Β·104)Β β the numbers that are described in the statement. It is guaranteed that numbers *x*, *y* are prime. | Print a single integer β the answer to the problem. | [
"3 1 2 3\n",
"1 3 2 3\n"
] | [
"5\n",
"4\n"
] | In the first sample you give the set of numbers {1,β3,β5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1,β3,β5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1,β2,β4} to the second friend. Thus, the answer to the problem is 4. | [
{
"input": "3 1 2 3",
"output": "5"
},
{
"input": "1 3 2 3",
"output": "4"
},
{
"input": "916200 69682 2 3",
"output": "1832399"
},
{
"input": "808351 17767 433 509",
"output": "826121"
},
{
"input": "8851 901 20897 26183",
"output": "9752"
},
{
"input": "5099 2895 16273 29473",
"output": "7994"
},
{
"input": "5099 2895 16273 29473",
"output": "7994"
},
{
"input": "4969 694 293 2347",
"output": "5663"
},
{
"input": "683651932 161878530 2 5",
"output": "1367303863"
},
{
"input": "325832598 637961741 2 3",
"output": "1156553206"
},
{
"input": "999999999 1 2 3",
"output": "1999999997"
},
{
"input": "11006 976 6287 9007",
"output": "11982"
},
{
"input": "150064728 173287472 439 503",
"output": "323353664"
},
{
"input": "819712074 101394406 6173 7307",
"output": "921106500"
},
{
"input": "67462086 313228052 15131 29027",
"output": "380690138"
},
{
"input": "500000000 500000000 29983 29989",
"output": "1000000001"
},
{
"input": "500000000 500000000 2 3",
"output": "1199999999"
},
{
"input": "500000000 500000000 29959 29983",
"output": "1000000001"
},
{
"input": "999999999 1 29983 29989",
"output": "1000033352"
},
{
"input": "1 999999999 29983 29989",
"output": "1000033345"
},
{
"input": "1 999999999 2 3",
"output": "1499999998"
},
{
"input": "999999998 1 2 3",
"output": "1999999995"
},
{
"input": "999999998 2 2 3",
"output": "1999999995"
},
{
"input": "9999999 10000 29983 29989",
"output": "10009999"
},
{
"input": "1000 9999999 29983 29989",
"output": "10000999"
},
{
"input": "110 40 1567 7681",
"output": "150"
},
{
"input": "197 2 6361 18223",
"output": "199"
},
{
"input": "39 154 1033 18947",
"output": "193"
},
{
"input": "126 51 26249 29443",
"output": "177"
},
{
"input": "14 179 19699 29303",
"output": "193"
},
{
"input": "14797 3593 13147 13933",
"output": "18390"
},
{
"input": "782 5750 7079 23957",
"output": "6532"
},
{
"input": "10876 8828 12487 16607",
"output": "19704"
},
{
"input": "16860 2201 6427 23327",
"output": "19061"
},
{
"input": "4901 563 1997 15053",
"output": "5464"
},
{
"input": "1277613 596606 10427 29387",
"output": "1874219"
},
{
"input": "1513389 40825 5641 10009",
"output": "1554214"
},
{
"input": "1749165 72848 9743 20023",
"output": "1822013"
},
{
"input": "954386 580262 4993 15629",
"output": "1534648"
},
{
"input": "159606 875491 43 19121",
"output": "1035098"
},
{
"input": "139694297 21147406 8819 28499",
"output": "160841703"
},
{
"input": "26941620 135675892 4093 28979",
"output": "162617513"
},
{
"input": "9156260 174697920 8171 29863",
"output": "183854180"
},
{
"input": "191370899 1962652 3517 24023",
"output": "193333553"
},
{
"input": "78618222 88031575 28289 29023",
"output": "166649797"
},
{
"input": "1 1 2 3",
"output": "2"
},
{
"input": "1 2 2 3",
"output": "3"
},
{
"input": "1 2 29983 29989",
"output": "3"
},
{
"input": "1 1 29983 29989",
"output": "2"
},
{
"input": "2 1 2 3",
"output": "3"
}
] | 62 | 0 | 3 | 6,566 |
|
244 | Undoubtedly Lucky Numbers | [
"bitmasks",
"brute force",
"dfs and similar"
] | null | null | Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits *x* and *y*. For example, if *x*<==<=4, and *y*<==<=7, then numbers 47, 744, 4 are lucky.
Let's call a positive integer *a* undoubtedly lucky, if there are such digits *x* and *y* (0<=β€<=*x*,<=*y*<=β€<=9), that the decimal representation of number *a* (without leading zeroes) contains only digits *x* and *y*.
Polycarpus has integer *n*. He wants to know how many positive integers that do not exceed *n*, are undoubtedly lucky. Help him, count this number. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=109) β Polycarpus's number. | Print a single integer that says, how many positive integers that do not exceed *n* are undoubtedly lucky. | [
"10\n",
"123\n"
] | [
"10\n",
"113\n"
] | In the first test sample all numbers that do not exceed 10 are undoubtedly lucky.
In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. | [
{
"input": "10",
"output": "10"
},
{
"input": "123",
"output": "113"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "2"
},
{
"input": "1000",
"output": "352"
},
{
"input": "1000000000",
"output": "40744"
},
{
"input": "999999999",
"output": "40743"
},
{
"input": "999999998",
"output": "40742"
},
{
"input": "999999997",
"output": "40741"
},
{
"input": "909090901",
"output": "38532"
},
{
"input": "142498040",
"output": "21671"
},
{
"input": "603356456",
"output": "31623"
},
{
"input": "64214872",
"output": "15759"
},
{
"input": "820040584",
"output": "36407"
},
{
"input": "442198",
"output": "3071"
},
{
"input": "784262",
"output": "4079"
},
{
"input": "642678",
"output": "3615"
},
{
"input": "468390",
"output": "3223"
},
{
"input": "326806",
"output": "2759"
},
{
"input": "940",
"output": "331"
},
{
"input": "356",
"output": "175"
},
{
"input": "68",
"output": "68"
},
{
"input": "132",
"output": "114"
},
{
"input": "72",
"output": "72"
},
{
"input": "89",
"output": "89"
},
{
"input": "1",
"output": "1"
},
{
"input": "3",
"output": "3"
},
{
"input": "4",
"output": "4"
},
{
"input": "5",
"output": "5"
},
{
"input": "6",
"output": "6"
},
{
"input": "7",
"output": "7"
},
{
"input": "8",
"output": "8"
},
{
"input": "9",
"output": "9"
},
{
"input": "101",
"output": "101"
},
{
"input": "102",
"output": "101"
}
] | 2,000 | 0 | 0 | 6,596 |
|
49 | Sum | [
"math"
] | B. Sum | 2 | 256 | Vasya studies positional numeral systems. Unfortunately, he often forgets to write the base of notation in which the expression is written. Once he saw a note in his notebook saying *a*<=+<=*b*<==<=?, and that the base of the positional notation wasnβt written anywhere. Now Vasya has to choose a base *p* and regard the expression as written in the base *p* positional notation. Vasya understood that he can get different results with different bases, and some bases are even invalid. For example, expression 78<=+<=87 in the base 16 positional notation is equal to *FF*16, in the base 15 positional notation it is equal to 11015, in the base 10 one β to 16510, in the base 9 one β to 1769, and in the base 8 or lesser-based positional notations the expression is invalid as all the numbers should be strictly less than the positional notation base. Vasya got interested in what is the length of the longest possible expression value. Help him to find this length.
The length of a number should be understood as the number of numeric characters in it. For example, the length of the longest answer for 78<=+<=87<==<=? is 3. It is calculated like that in the base 15 (11015), base 10 (16510), base 9 (1769) positional notations, for example, and in some other ones. | The first letter contains two space-separated numbers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=1000) which represent the given summands. | Print a single number β the length of the longest answer. | [
"78 87\n",
"1 1\n"
] | [
"3\n",
"2\n"
] | none | [
{
"input": "78 87",
"output": "3"
},
{
"input": "1 1",
"output": "2"
},
{
"input": "9 7",
"output": "2"
},
{
"input": "11 11",
"output": "3"
},
{
"input": "43 21",
"output": "3"
},
{
"input": "84 89",
"output": "3"
},
{
"input": "12 34",
"output": "3"
},
{
"input": "99 11",
"output": "3"
},
{
"input": "11 99",
"output": "3"
},
{
"input": "99 99",
"output": "3"
},
{
"input": "1 2",
"output": "2"
},
{
"input": "1 3",
"output": "2"
},
{
"input": "2 1",
"output": "2"
},
{
"input": "2 2",
"output": "2"
},
{
"input": "2 3",
"output": "2"
},
{
"input": "3 1",
"output": "2"
},
{
"input": "3 2",
"output": "2"
},
{
"input": "3 3",
"output": "2"
},
{
"input": "1 466",
"output": "3"
},
{
"input": "1 1000",
"output": "4"
},
{
"input": "1 999",
"output": "4"
},
{
"input": "149 1",
"output": "3"
},
{
"input": "999 1",
"output": "4"
},
{
"input": "1000 1",
"output": "4"
},
{
"input": "998 998",
"output": "4"
},
{
"input": "998 999",
"output": "4"
},
{
"input": "998 1000",
"output": "4"
},
{
"input": "999 998",
"output": "4"
},
{
"input": "999 999",
"output": "4"
},
{
"input": "999 1000",
"output": "4"
},
{
"input": "1000 998",
"output": "4"
},
{
"input": "1000 999",
"output": "4"
},
{
"input": "1000 1000",
"output": "5"
},
{
"input": "1000 539",
"output": "4"
},
{
"input": "999 619",
"output": "4"
},
{
"input": "2 511",
"output": "3"
},
{
"input": "877 2",
"output": "3"
},
{
"input": "379 999",
"output": "4"
},
{
"input": "247 1000",
"output": "4"
},
{
"input": "555 555",
"output": "4"
},
{
"input": "208 997",
"output": "4"
},
{
"input": "633 581",
"output": "4"
},
{
"input": "411 517",
"output": "4"
},
{
"input": "836 101",
"output": "4"
},
{
"input": "262 685",
"output": "4"
},
{
"input": "39 269",
"output": "3"
},
{
"input": "464 205",
"output": "4"
},
{
"input": "890 789",
"output": "4"
},
{
"input": "667 373",
"output": "4"
},
{
"input": "840 975",
"output": "4"
},
{
"input": "810 413",
"output": "4"
},
{
"input": "133 851",
"output": "4"
},
{
"input": "104 938",
"output": "4"
},
{
"input": "427 376",
"output": "4"
},
{
"input": "398 815",
"output": "4"
},
{
"input": "721 253",
"output": "4"
},
{
"input": "692 339",
"output": "4"
},
{
"input": "15 778",
"output": "3"
},
{
"input": "986 216",
"output": "4"
},
{
"input": "450 277",
"output": "3"
},
{
"input": "333 1",
"output": "4"
},
{
"input": "499 499",
"output": "3"
},
{
"input": "79 1",
"output": "2"
},
{
"input": "87 1",
"output": "2"
},
{
"input": "47 8",
"output": "2"
},
{
"input": "87 8",
"output": "3"
},
{
"input": "1 11",
"output": "3"
}
] | 248 | 0 | 0 | 6,610 |
168 | Wizards and Demonstration | [
"implementation",
"math"
] | null | null | Some country is populated by wizards. They want to organize a demonstration.
There are *n* people living in the city, *x* of them are the wizards who will surely go to the demonstration. Other city people (*n*<=-<=*x* people) do not support the wizards and aren't going to go to the demonstration. We know that the city administration will react only to the demonstration involving at least *y* percent of the city people. Having considered the matter, the wizards decided to create clone puppets which can substitute the city people on the demonstration.
So all in all, the demonstration will involve only the wizards and their puppets. The city administration cannot tell the difference between a puppet and a person, so, as they calculate the percentage, the administration will consider the city to be consisting of only *n* people and not containing any clone puppets.
Help the wizards and find the minimum number of clones to create to that the demonstration had no less than *y* percent of the city people. | The first line contains three space-separated integers, *n*, *x*, *y* (1<=β€<=*n*,<=*x*,<=*y*<=β€<=104,<=*x*<=β€<=*n*) β the number of citizens in the city, the number of wizards and the percentage the administration needs, correspondingly.
Please note that *y* can exceed 100 percent, that is, the administration wants to see on a demonstration more people that actually live in the city (<=><=*n*). | Print a single integer β the answer to the problem, the minimum number of clones to create, so that the demonstration involved no less than *y* percent of *n* (the real total city population). | [
"10 1 14\n",
"20 10 50\n",
"1000 352 146\n"
] | [
"1\n",
"0\n",
"1108\n"
] | In the first sample it is necessary that at least 14% of 10 people came to the demonstration. As the number of people should be integer, then at least two people should come. There is only one wizard living in the city and he is going to come. That isn't enough, so he needs to create one clone.
In the second sample 10 people should come to the demonstration. The city has 10 wizards. They will all come to the demonstration, so nobody has to create any clones. | [
{
"input": "10 1 14",
"output": "1"
},
{
"input": "20 10 50",
"output": "0"
},
{
"input": "1000 352 146",
"output": "1108"
},
{
"input": "68 65 20",
"output": "0"
},
{
"input": "78 28 27",
"output": "0"
},
{
"input": "78 73 58",
"output": "0"
},
{
"input": "70 38 66",
"output": "9"
},
{
"input": "54 4 38",
"output": "17"
},
{
"input": "3 1 69",
"output": "2"
},
{
"input": "11 9 60",
"output": "0"
},
{
"input": "71 49 65",
"output": "0"
},
{
"input": "78 55 96",
"output": "20"
},
{
"input": "2765 768 9020",
"output": "248635"
},
{
"input": "3478 1728 9727",
"output": "336578"
},
{
"input": "9678 6173 5658",
"output": "541409"
},
{
"input": "1138 570 6666",
"output": "75290"
},
{
"input": "7754 204 9038",
"output": "700603"
},
{
"input": "8403 7401 4769",
"output": "393339"
},
{
"input": "4909 2111 8860",
"output": "432827"
},
{
"input": "6571 6449 8965",
"output": "582642"
},
{
"input": "9178 2255 7996",
"output": "731618"
},
{
"input": "6151 6148 3746",
"output": "224269"
},
{
"input": "8890 5449 8734",
"output": "771004"
},
{
"input": "7879 2590 2818",
"output": "219441"
},
{
"input": "8484 6400 547",
"output": "40008"
},
{
"input": "7835 6710 1639",
"output": "121706"
},
{
"input": "2379 1436 9663",
"output": "228447"
},
{
"input": "6487 5670 8",
"output": "0"
},
{
"input": "4470 2543 6",
"output": "0"
},
{
"input": "7261 5328 10",
"output": "0"
},
{
"input": "3871 3795 7",
"output": "0"
},
{
"input": "9620 6557 6",
"output": "0"
},
{
"input": "4890 1112 5",
"output": "0"
},
{
"input": "1 1 1",
"output": "0"
},
{
"input": "1 1 10000",
"output": "99"
},
{
"input": "10000 1 1",
"output": "99"
},
{
"input": "10000 1 10000",
"output": "999999"
},
{
"input": "10000 10000 1",
"output": "0"
},
{
"input": "10000 10000 10000",
"output": "990000"
},
{
"input": "67 1 3",
"output": "2"
},
{
"input": "7878 4534 9159",
"output": "717013"
},
{
"input": "1 1 1",
"output": "0"
},
{
"input": "3271 5 50",
"output": "1631"
}
] | 154 | 0 | 3 | 6,611 |
|
526 | Om Nom and Dark Park | [
"dfs and similar",
"greedy",
"implementation"
] | null | null | Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him.
The park consists of 2*n*<=+<=1<=-<=1 squares connected by roads so that the scheme of the park is a full binary tree of depth *n*. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2*n*,<=2*n*<=+<=1,<=...,<=2*n*<=+<=1<=-<=1 and these exits lead straight to the Om Nom friends' houses. From each square *i* (2<=β€<=*i*<=<<=2*n*<=+<=1) there is a road to the square . Thus, it is possible to go from the park entrance to each of the exits by walking along exactly *n* roads.
Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe.
He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads. | The first line contains integer *n* (1<=β€<=*n*<=β€<=10) β the number of roads on the path from the entrance to any exit.
The next line contains 2*n*<=+<=1<=-<=2 numbers *a*2,<=*a*3,<=... *a*2*n*<=+<=1<=-<=1 β the initial numbers of street lights on each road of the park. Here *a**i* is the number of street lights on the road between squares *i* and . All numbers *a**i* are positive integers, not exceeding 100. | Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe. | [
"2\n1 2 3 4 5 6\n"
] | [
"5\n"
] | Picture for the sample test. Green color denotes the additional street lights. | [
{
"input": "2\n1 2 3 4 5 6",
"output": "5"
},
{
"input": "2\n1 2 3 3 2 2",
"output": "0"
},
{
"input": "1\n39 52",
"output": "13"
},
{
"input": "2\n59 96 34 48 8 72",
"output": "139"
},
{
"input": "3\n87 37 91 29 58 45 51 74 70 71 47 38 91 89",
"output": "210"
},
{
"input": "5\n39 21 95 89 73 90 9 55 85 32 30 21 68 59 82 91 20 64 52 70 6 88 53 47 30 47 34 14 11 22 42 15 28 54 37 48 29 3 14 13 18 77 90 58 54 38 94 49 45 66 13 74 11 14 64 72 95 54 73 79 41 35",
"output": "974"
},
{
"input": "1\n49 36",
"output": "13"
},
{
"input": "1\n77 88",
"output": "11"
},
{
"input": "1\n1 33",
"output": "32"
},
{
"input": "2\n72 22 81 23 14 75",
"output": "175"
},
{
"input": "2\n100 70 27 1 68 52",
"output": "53"
},
{
"input": "2\n24 19 89 82 22 21",
"output": "80"
},
{
"input": "3\n86 12 92 91 3 68 57 56 76 27 33 62 71 84",
"output": "286"
},
{
"input": "3\n14 56 53 61 57 45 40 44 31 9 73 2 61 26",
"output": "236"
},
{
"input": "3\n35 96 7 43 10 14 16 36 95 92 16 50 59 55",
"output": "173"
},
{
"input": "4\n1 97 18 48 96 65 24 91 17 45 36 27 74 93 78 86 39 55 53 21 26 68 31 33 79 63 80 92 1 26",
"output": "511"
},
{
"input": "4\n25 42 71 29 50 30 99 79 77 24 76 66 68 23 97 99 65 17 75 62 66 46 48 4 40 71 98 57 21 92",
"output": "603"
},
{
"input": "4\n49 86 17 7 3 6 86 71 36 10 27 10 58 64 12 16 88 67 93 3 15 20 58 87 97 91 11 6 34 62",
"output": "470"
},
{
"input": "5\n16 87 36 16 81 53 87 35 63 56 47 91 81 95 80 96 91 7 58 99 25 28 47 60 7 69 49 14 51 52 29 30 83 23 21 52 100 26 91 14 23 94 72 70 40 12 50 32 54 52 18 74 5 15 62 3 48 41 24 25 56 43",
"output": "1060"
},
{
"input": "5\n40 27 82 94 38 22 66 23 18 34 87 31 71 28 95 5 14 61 76 52 66 6 60 40 68 77 70 63 64 18 47 13 82 55 34 64 30 1 29 24 24 9 65 17 29 96 61 76 72 23 32 26 90 39 54 41 35 66 71 29 75 48",
"output": "1063"
},
{
"input": "5\n64 72 35 68 92 95 45 15 77 16 26 74 61 65 18 22 32 19 98 97 14 84 70 23 29 1 87 28 88 89 73 79 69 88 43 60 64 64 66 39 17 27 46 71 18 83 73 20 90 77 49 70 84 63 50 72 26 87 26 37 78 65",
"output": "987"
},
{
"input": "6\n35 61 54 77 70 50 53 70 4 66 58 47 76 100 78 5 43 50 55 93 13 93 59 92 30 74 22 23 98 70 19 56 90 92 19 7 28 53 45 77 42 91 71 56 19 83 100 53 13 93 37 13 70 60 16 13 76 3 12 22 17 26 50 6 63 7 25 41 92 29 36 80 11 4 10 14 77 75 53 82 46 24 56 46 82 36 80 75 8 45 24 22 90 34 45 76 18 38 86 43 7 49 80 56 90 53 12 51 98 47 44 58 32 4 2 6 3 60 38 72 74 46 30 86 1 98",
"output": "2499"
},
{
"input": "6\n63 13 100 54 31 15 29 58 59 44 2 99 70 33 97 14 70 12 73 42 65 71 68 67 87 83 43 84 18 41 37 22 81 24 27 11 57 28 83 92 39 1 56 15 16 67 16 97 31 52 50 65 63 89 8 52 55 20 71 27 28 35 86 92 94 60 10 65 83 63 89 71 34 20 78 40 34 62 2 86 100 81 87 69 25 4 52 17 57 71 62 38 1 3 54 71 34 85 20 60 80 23 82 47 4 19 7 18 14 18 28 27 4 55 26 71 45 9 2 40 67 28 32 19 81 92",
"output": "2465"
},
{
"input": "6\n87 62 58 32 81 92 12 50 23 27 38 39 64 74 16 35 84 59 91 87 14 48 90 47 44 95 64 45 31 11 67 5 80 60 36 15 91 3 21 2 40 24 37 69 5 50 23 37 49 19 68 21 49 9 100 94 45 41 22 31 31 48 25 70 25 25 95 88 82 1 37 53 49 31 57 74 94 45 55 93 43 37 13 85 59 72 15 68 3 90 96 55 100 64 63 69 43 33 66 84 57 97 87 34 23 89 97 77 39 89 8 92 68 13 50 36 95 61 71 96 73 13 30 49 57 89",
"output": "2513"
}
] | 31 | 0 | 0 | 6,627 |
|
14 | Four Segments | [
"brute force",
"constructive algorithms",
"geometry",
"implementation",
"math"
] | C. Four Segments | 2 | 64 | Several months later Alex finally got his brother Bob's creation by post. And now, in his turn, Alex wants to boast about something to his brother. He thought for a while, and came to the conclusion that he has no ready creations, and decided to write a program for rectangles detection. According to his plan, the program detects if the four given segments form a rectangle of a positive area and with sides parallel to coordinate axes. As Alex does badly at school and can't write this program by himself, he asks you to help him. | The input data contain four lines. Each of these lines contains four integers *x*1, *y*1, *x*2, *y*2 (<=-<=109<=β€<=*x*1,<=*y*1,<=*x*2,<=*y*2<=β€<=109) β coordinates of segment's beginning and end positions. The given segments can degenerate into points. | Output the word Β«YESΒ», if the given four segments form the required rectangle, otherwise output Β«NOΒ». | [
"1 1 6 1\n1 0 6 0\n6 0 6 1\n1 1 1 0\n",
"0 0 0 3\n2 0 0 0\n2 2 2 0\n0 2 2 2\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "1 1 6 1\n1 0 6 0\n6 0 6 1\n1 1 1 0",
"output": "YES"
},
{
"input": "0 0 0 3\n2 0 0 0\n2 2 2 0\n0 2 2 2",
"output": "NO"
},
{
"input": "0 0 0 2\n2 0 0 0\n2 2 2 0\n0 2 2 2",
"output": "YES"
},
{
"input": "0 0 10 0\n0 0 10 0\n0 0 0 5\n0 0 0 -5",
"output": "NO"
},
{
"input": "0 0 4 0\n4 0 3 0\n3 0 2 0\n2 0 0 0",
"output": "NO"
},
{
"input": "0 0 3 0\n0 0 0 3\n0 3 3 3\n3 3 3 0",
"output": "YES"
},
{
"input": "0 0 1 0\n1 0 1 1\n0 1 1 1\n0 0 0 1",
"output": "YES"
},
{
"input": "0 0 1 0\n1 0 1 1\n1 1 1 0\n1 0 0 0",
"output": "NO"
},
{
"input": "0 0 1 1\n1 1 2 0\n2 0 1 -1\n1 -1 0 0",
"output": "NO"
},
{
"input": "0 0 0 10\n0 10 0 9\n0 9 0 8\n0 8 0 0",
"output": "NO"
},
{
"input": "0 0 4 0\n4 0 4 0\n4 0 0 0\n0 0 0 0",
"output": "NO"
},
{
"input": "0 0 0 2\n0 2 2 2\n0 0 2 2\n2 2 2 0",
"output": "NO"
},
{
"input": "0 0 0 2\n2 0 2 2\n0 2 0 0\n2 2 2 0",
"output": "NO"
},
{
"input": "13 13 13 13\n13 13 13 13\n13 13 13 13\n13 13 13 13",
"output": "NO"
},
{
"input": "0 0 2 0\n0 1 0 3\n0 4 3 4\n3 0 3 3",
"output": "NO"
},
{
"input": "1 1 1 1\n0 1 -1 1\n-1 1 1 1\n-1 1 1 1",
"output": "NO"
},
{
"input": "1 -1 1 -1\n1 -1 1 -1\n1 -1 1 -1\n1 -1 1 -1",
"output": "NO"
},
{
"input": "-1 0 -1 0\n-1 0 -1 0\n-1 0 -1 0\n-1 0 -1 0",
"output": "NO"
},
{
"input": "-1 0 -1 0\n-1 0 -1 0\n-1 0 -1 0\n-1 0 -1 0",
"output": "NO"
},
{
"input": "0 -1 0 1\n0 0 0 1\n0 -1 0 -1\n0 -1 0 -1",
"output": "NO"
},
{
"input": "0 0 -1 0\n-1 0 0 0\n-1 0 -1 0\n-1 0 -1 0",
"output": "NO"
},
{
"input": "0 0 0 0\n0 0 0 1\n0 0 0 1\n0 0 0 0",
"output": "NO"
},
{
"input": "-1 1 -1 1\n-1 1 -1 1\n-1 1 -1 1\n-1 1 -1 1",
"output": "NO"
},
{
"input": "-1 1 -1 1\n0 1 1 1\n1 -1 -1 1\n-1 1 1 1",
"output": "NO"
},
{
"input": "-1 -1 -1 -1\n-1 0 -1 0\n-1 0 -1 0\n-1 -1 -1 -1",
"output": "NO"
},
{
"input": "1 2 1 2\n-2 2 1 2\n1 -2 -2 2\n-2 -2 1 -2",
"output": "NO"
},
{
"input": "-2 1 -2 -1\n-2 -2 -2 -2\n-2 -1 -2 -2\n-2 1 -2 -1",
"output": "NO"
},
{
"input": "1 2 1 2\n1 -1 1 -1\n1 -1 1 -1\n1 -1 1 -1",
"output": "NO"
},
{
"input": "-2 0 -2 -1\n-2 2 -2 0\n-2 2 -2 2\n-2 0 -2 -1",
"output": "NO"
},
{
"input": "-1 1 -2 1\n0 -1 -1 1\n-2 1 -1 -1\n0 1 0 -1",
"output": "NO"
},
{
"input": "2 -1 -2 -1\n2 -1 2 -1\n2 -1 -2 -1\n2 -1 2 -1",
"output": "NO"
},
{
"input": "0 2 0 2\n0 2 0 1\n0 1 0 1\n0 2 0 1",
"output": "NO"
},
{
"input": "1 0 1 0\n1 0 1 0\n1 0 0 0\n1 0 1 0",
"output": "NO"
},
{
"input": "-1 1 2 1\n0 1 0 1\n0 1 2 1\n2 1 -1 1",
"output": "NO"
},
{
"input": "0 0 2 0\n0 0 2 0\n0 -2 0 0\n0 -2 0 0",
"output": "NO"
},
{
"input": "0 -3 0 -1\n1 -1 1 -1\n0 -1 1 -2\n0 -2 -2 -3",
"output": "NO"
},
{
"input": "-3 -2 -2 -2\n3 -2 3 -2\n-3 -2 -2 -2\n3 -2 3 -2",
"output": "NO"
},
{
"input": "1 2 -2 2\n-2 2 3 2\n1 2 -2 2\n-2 2 3 2",
"output": "NO"
},
{
"input": "0 -2 1 3\n1 3 1 3\n1 3 1 3\n1 3 1 -2",
"output": "NO"
},
{
"input": "0 -3 -2 -3\n0 1 0 -3\n0 1 0 -3\n0 1 0 -3",
"output": "NO"
},
{
"input": "1 -3 1 -3\n1 -3 1 -3\n1 -3 1 -3\n1 -3 1 -3",
"output": "NO"
},
{
"input": "-3 2 -2 1\n0 2 0 -3\n0 -3 -2 1\n0 1 -3 -3",
"output": "NO"
},
{
"input": "-3 3 2 3\n2 3 2 3\n-3 3 -3 3\n-3 3 2 3",
"output": "NO"
},
{
"input": "2 -2 2 -2\n2 -2 2 -2\n2 -2 2 -2\n2 -2 2 -2",
"output": "NO"
},
{
"input": "2 -1 0 -2\n-3 -2 -3 3\n2 -2 2 -2\n0 3 -3 -2",
"output": "NO"
},
{
"input": "1 -3 -1 1\n0 -2 1 -3\n1 1 0 1\n1 -3 0 1",
"output": "NO"
},
{
"input": "-2 4 -2 4\n-2 4 -2 -2\n-2 4 -2 -2\n-2 4 -2 -2",
"output": "NO"
},
{
"input": "3 1 3 1\n-3 1 3 1\n3 3 -3 1\n-3 1 3 1",
"output": "NO"
},
{
"input": "0 1 4 1\n0 1 4 1\n4 1 0 1\n0 -2 4 1",
"output": "NO"
},
{
"input": "0 -2 0 -1\n0 -1 0 -2\n0 -2 0 -2\n0 -2 0 -2",
"output": "NO"
},
{
"input": "-1 1 -1 1\n-1 1 -1 1\n-1 1 -1 3\n-3 1 -3 1",
"output": "NO"
},
{
"input": "578327678 518066351 578327678 498442467\n583129774 498442467 578327678 518066351\n583129774 518066351 578327678 518066351\n583129774 498442467 578327678 518066351",
"output": "NO"
},
{
"input": "-973576966 32484917 -973576966 32484917\n-973576966 32484917 347173379 32484917\n-973576966 32484917 347173379 32484917\n-973576966 32484917 347173379 32484917",
"output": "NO"
},
{
"input": "572793036 194804279 572793036 -866298887\n572793036 461349977 -860420833 194804279\n572793036 461349977 572793036 -866298887\n-860420833 461349977 572793036 -866298887",
"output": "NO"
},
{
"input": "949753871 -467933239 -72251156 462207752\n949753871 462207752 425479768 -467933239\n425479768 462207752 425479768 -467933239\n949753871 -467933239 949753871 462207752",
"output": "NO"
},
{
"input": "1 -1 1 -1\n-1 -1 -1 -1\n1 0 -1 -1\n1 -1 -1 -1",
"output": "NO"
},
{
"input": "1 -1 1 -1\n1 0 1 0\n1 0 1 -1\n1 0 1 -1",
"output": "NO"
},
{
"input": "0 0 0 1\n0 1 0 1\n0 1 0 0\n0 1 0 1",
"output": "NO"
},
{
"input": "1 -1 1 0\n1 0 1 0\n0 0 0 -1\n1 -1 1 0",
"output": "NO"
},
{
"input": "0 0 2 2\n0 0 2 0\n2 2 2 2\n0 2 0 2",
"output": "NO"
},
{
"input": "-2 -1 -1 -1\n-2 -1 -1 -1\n-2 -1 -2 2\n-2 2 -1 2",
"output": "NO"
},
{
"input": "2 1 -1 0\n-1 0 2 1\n2 1 2 1\n-1 0 2 1",
"output": "NO"
},
{
"input": "1 -1 2 -1\n1 -2 2 -2\n1 -2 2 -2\n1 -2 2 -2",
"output": "NO"
},
{
"input": "-1 -2 -1 2\n-1 -2 -1 -2\n-1 2 -1 2\n-1 -2 -1 -2",
"output": "NO"
},
{
"input": "2 0 2 -1\n2 -1 -1 0\n2 -1 -1 0\n2 -1 -1 0",
"output": "NO"
},
{
"input": "2 -3 1 3\n1 -3 1 3\n2 3 2 -3\n2 -3 2 -3",
"output": "NO"
},
{
"input": "130120899 550158649 130120899 831843953\n130120899 550158649 130120899 831843953\n130120899 550158649 434006978 831843953\n434006978 550158649 434006978 550158649",
"output": "NO"
},
{
"input": "-214484034 559719641 -214484034 559719641\n-214484034 559719641 -214484034 559719641\n-214484034 2764087 -214484034 559719641\n-214484034 2764087 734280017 2764087",
"output": "NO"
},
{
"input": "-966947426 664261857 -994206270 664261857\n-966947426 664261857 -994206270 664261857\n-966947426 789165019 -966947426 789165019\n-966947426 664261857 -966947426 789165019",
"output": "NO"
},
{
"input": "264411509 -329579381 264411509 -329579381\n-726758913 -329579381 264411509 357369289\n-726758913 -329579381 264411509 -329579381\n264411509 -329579381 264411509 -329579381",
"output": "NO"
},
{
"input": "-193720583 -547078093 -570748852 58725936\n-570748852 -547078093 -570748852 58725936\n-193720583 58725936 -570748852 -547078093\n-570748852 -547078093 -193720583 58725936",
"output": "NO"
},
{
"input": "-534094150 -333730697 120658438 -333730697\n-534094150 -333730697 120658438 869464313\n-534094150 -333730697 -534094150 -333730697\n-534094150 869464313 -534094150 -333730697",
"output": "NO"
},
{
"input": "-328545071 835751660 -345950135 835751660\n-345950135 243569491 -328545071 835751660\n-328545071 835751660 -345950135 243569491\n-328545071 243569491 -328545071 243569491",
"output": "NO"
},
{
"input": "-985236057 -809433993 -985236057 -225363622\n-484344733 -225363622 -484344733 -225363622\n-985236057 -225363622 -985236057 -809433993\n-484344733 -225363622 -484344733 -809433993",
"output": "NO"
},
{
"input": "774287068 919049158 774287068 919049158\n250033372 653817677 250033372 653817677\n250033372 919049158 774287068 653817677\n250033372 653817677 250033372 653817677",
"output": "NO"
},
{
"input": "15319063 -661389770 632904085 -661389770\n15319063 834266473 632904085 834266473\n15319063 834266473 15319063 -661389770\n632904085 -661389770 632904085 834266473",
"output": "YES"
},
{
"input": "157550209 -594704878 157550209 524666828\n671878188 -594704878 157550209 -594704878\n671878188 -594704878 671878188 524666828\n671878188 524666828 157550209 524666828",
"output": "YES"
},
{
"input": "-887135208 728202342 127569272 728202342\n127569272 728202342 127569272 -932260532\n-887135208 -932260532 -887135208 728202342\n127569272 -932260532 -887135208 -932260532",
"output": "YES"
},
{
"input": "-777411660 -392696120 -777411660 878250237\n461320023 878250237 -777411660 878250237\n461320023 878250237 461320023 -392696120\n461320023 -392696120 -777411660 -392696120",
"output": "YES"
},
{
"input": "-892785315 567101756 -892785315 212349618\n-403060667 212349618 -403060667 567101756\n-403060667 567101756 -892785315 567101756\n-892785315 212349618 -403060667 212349618",
"output": "YES"
},
{
"input": "-360046034 -871603961 -37695563 -871603961\n-37695563 664955871 -37695563 -871603961\n-360046034 664955871 -360046034 -871603961\n-360046034 664955871 -37695563 664955871",
"output": "YES"
},
{
"input": "375089524 -852468724 -952575952 -852468724\n-952575952 -852468724 -952575952 -883150295\n-952575952 -883150295 375089524 -883150295\n375089524 -852468724 375089524 -883150295",
"output": "YES"
},
{
"input": "851113265 -893293930 851113265 -444742025\n-864765585 -893293930 -864765585 -444742025\n-864765585 -893293930 851113265 -893293930\n-864765585 -444742025 851113265 -444742025",
"output": "YES"
},
{
"input": "-309306779 559081237 255096743 559081237\n-309306779 -359724178 255096743 -359724178\n255096743 -359724178 255096743 559081237\n-309306779 559081237 -309306779 -359724178",
"output": "YES"
},
{
"input": "542957347 -480242202 566995046 -480242202\n542957347 -480242202 542957347 -298569507\n566995046 -298569507 542957347 -298569507\n566995046 -298569507 566995046 -480242202",
"output": "YES"
},
{
"input": "724715871 -943657730 964573294 -943657730\n724715871 -943657730 724715871 -216459206\n964573294 -216459206 964573294 -943657730\n724715871 -216459206 964573294 -216459206",
"output": "YES"
},
{
"input": "-394306310 -279360055 -394306310 771835446\n-394306310 -279360055 -358661829 -279360055\n-358661829 771835446 -358661829 -279360055\n-358661829 771835446 -394306310 771835446",
"output": "YES"
},
{
"input": "-204472047 -894485730 -204472047 640004355\n960702643 -894485730 960702643 640004355\n960702643 -894485730 -204472047 -894485730\n-204472047 640004355 960702643 640004355",
"output": "YES"
},
{
"input": "747591 5158024 -837871358 5158024\n-837871358 -874026904 747591 -874026904\n747591 -874026904 747591 5158024\n-837871358 -874026904 -837871358 5158024",
"output": "YES"
},
{
"input": "-442585231 90863587 800882871 90863587\n800882871 288218107 800882871 90863587\n800882871 288218107 -442585231 288218107\n-442585231 90863587 -442585231 288218107",
"output": "YES"
},
{
"input": "-969490772 476931470 -969490772 929999730\n-426216863 929999730 -969490772 929999730\n-426216863 929999730 -426216863 476931470\n-969490772 476931470 -426216863 476931470",
"output": "YES"
},
{
"input": "-683046010 -125472203 -683046010 418507423\n817863270 418507423 817863270 -125472203\n817863270 418507423 -683046010 418507423\n817863270 -125472203 -683046010 -125472203",
"output": "YES"
},
{
"input": "231996287 974811621 -923122611 974811621\n-923122611 646880519 -923122611 974811621\n231996287 646880519 231996287 974811621\n-923122611 646880519 231996287 646880519",
"output": "YES"
},
{
"input": "-83429272 -350159212 -990378619 -350159212\n-990378619 -350159212 -990378619 247777831\n-83429272 -350159212 -83429272 247777831\n-990378619 247777831 -83429272 247777831",
"output": "YES"
},
{
"input": "-679599706 974881765 -679599706 -84192294\n-554774137 -84192294 -554774137 974881765\n-554774137 974881765 -679599706 974881765\n-554774137 -84192294 -679599706 -84192294",
"output": "YES"
},
{
"input": "-155221108 -190475340 -155221108 -819044368\n-155221108 -190475340 -155875856 -190475340\n-155875856 -190475340 -155875856 -819044368\n-155875856 -819044368 -155221108 -819044368",
"output": "YES"
},
{
"input": "377126871 -877660066 -633390329 -877660066\n377126871 -175686511 377126871 -877660066\n-633390329 -877660066 -633390329 -175686511\n-633390329 -175686511 377126871 -175686511",
"output": "YES"
},
{
"input": "919022298 897009314 77151365 897009314\n77151365 579795002 919022298 579795002\n77151365 579795002 77151365 897009314\n919022298 579795002 919022298 897009314",
"output": "YES"
},
{
"input": "146411776 -188986353 146411776 -808042296\n-381166510 -808042296 -381166510 -188986353\n146411776 -188986353 -381166510 -188986353\n146411776 -808042296 -381166510 -808042296",
"output": "YES"
},
{
"input": "438703475 871560515 571565350 871560515\n571565350 -204157747 438703475 -204157747\n438703475 -204157747 438703475 871560515\n571565350 -204157747 571565350 871560515",
"output": "YES"
},
{
"input": "0 0 0 0\n5 5 5 5\n5 0 5 5\n0 5 0 0",
"output": "NO"
},
{
"input": "0 1 1 2\n2 1 1 2\n1 0 0 1\n2 1 1 0",
"output": "NO"
},
{
"input": "-3 0 -3 3\n0 0 0 3\n3 3 -3 3\n3 0 -3 0",
"output": "NO"
},
{
"input": "0 0 0 0\n1 1 1 1\n0 1 0 1\n1 0 1 0",
"output": "NO"
},
{
"input": "0 0 0 0\n0 0 0 1\n0 0 1 0\n1 1 1 1",
"output": "NO"
},
{
"input": "0 0 1 0\n1 1 0 1\n0 0 1 0\n1 1 0 1",
"output": "NO"
},
{
"input": "0 0 0 1\n0 1 1 1\n1 1 1 0\n1 0 0 1",
"output": "NO"
},
{
"input": "0 0 1 1\n0 1 1 0\n1 1 0 0\n1 0 0 1",
"output": "NO"
},
{
"input": "0 0 0 0\n1 1 1 1\n0 1 1 0\n1 0 0 1",
"output": "NO"
},
{
"input": "0 0 1 0\n0 1 1 1\n0 0 1 0\n0 1 1 1",
"output": "NO"
}
] | 62 | 0 | 0 | 6,628 |
868 | Huge Strings | [
"bitmasks",
"brute force",
"dp",
"implementation",
"strings"
] | null | null | You are given *n* strings *s*1,<=*s*2,<=...,<=*s**n* consisting of characters 0 and 1. *m* operations are performed, on each of them you concatenate two existing strings into a new one. On the *i*-th operation the concatenation *s**a**i**s**b**i* is saved into a new string *s**n*<=+<=*i* (the operations are numbered starting from 1). After each operation you need to find the maximum positive integer *k* such that all possible strings consisting of 0 and 1 of length *k* (there are 2*k* such strings) are substrings of the new string. If there is no such *k*, print 0. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=100)Β β the number of strings. The next *n* lines contain strings *s*1,<=*s*2,<=...,<=*s**n* (1<=β€<=|*s**i*|<=β€<=100), one per line. The total length of strings is not greater than 100.
The next line contains single integer *m* (1<=β€<=*m*<=β€<=100)Β β the number of operations. *m* lines follow, each of them contains two integers *a**i* abd *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*<=+<=*i*<=-<=1)Β β the number of strings that are concatenated to form *s**n*<=+<=*i*. | Print *m* lines, each should contain one integerΒ β the answer to the question after the corresponding operation. | [
"5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4\n"
] | [
"1\n2\n0\n"
] | On the first operation, a new string "0110" is created. For *k*β=β1 the two possible binary strings of length *k* are "0" and "1", they are substrings of the new string. For *k*β=β2 and greater there exist strings of length *k* that do not appear in this string (for *k*β=β2 such string is "00"). So the answer is 1.
On the second operation the string "01100" is created. Now all strings of length *k*β=β2 are present.
On the third operation the string "1111111111" is created. There is no zero, so the answer is 0. | [
{
"input": "5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4",
"output": "1\n2\n0"
},
{
"input": "5\n01\n1\n0011\n0\n01\n6\n5 5\n3 2\n4 2\n6 7\n5 1\n9 7",
"output": "1\n1\n1\n2\n1\n2"
},
{
"input": "5\n111101000111100011100110000100\n000111001\n01101000\n0000110100100010011001000000010100100111110110\n0110001\n10\n5 5\n2 2\n5 6\n1 1\n1 7\n10 6\n6 2\n11 1\n3 6\n8 2",
"output": "2\n2\n2\n3\n3\n4\n3\n4\n2\n3"
},
{
"input": "1\n1\n1\n1 1",
"output": "0"
},
{
"input": "5\n110101010101010110000011011\n111111\n1000100011100111100101101010011111100000001001\n00\n1111101100001110000\n10\n4 3\n6 6\n7 5\n8 8\n8 7\n10 8\n11 9\n10 12\n13 13\n12 13",
"output": "4\n4\n4\n4\n4\n4\n4\n4\n4\n4"
},
{
"input": "5\n100010010\n0\n1001100110010111\n0001000011000111000011011000110000010010010001110001000011011\n0100000100100\n10\n5 5\n6 6\n6 7\n7 8\n8 9\n10 8\n11 9\n10 9\n12 13\n12 13",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1"
},
{
"input": "5\n0\n1\n11\n110000010001100101001\n1101011011111\n10\n5 3\n6 4\n7 6\n8 7\n9 8\n10 9\n11 10\n12 11\n13 12\n14 13",
"output": "1\n4\n5\n5\n5\n5\n5\n5\n5\n5"
},
{
"input": "10\n0\n1\n1111100000\n0\n1\n0000\n11000\n1010001110010010110\n01101001111\n010101110110111111\n20\n10 3\n11 4\n12 5\n13 6\n14 7\n15 8\n16 9\n17 16\n18 17\n19 18\n20 19\n21 20\n22 21\n23 22\n24 23\n25 24\n26 25\n27 26\n28 27\n29 28",
"output": "2\n2\n3\n3\n3\n4\n5\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6"
},
{
"input": "10\n0\n1\n1111\n110000000\n100000\n1\n1\n000010100001110001\n00100010110001101000111100100110010101001011\n100110110011101\n50\n10 3\n11 4\n12 5\n13 6\n14 7\n15 8\n16 9\n17 1\n18 1\n19 2\n20 2\n21 2\n22 2\n23 2\n24 1\n25 2\n26 1\n27 2\n28 1\n29 2\n30 2\n31 1\n32 2\n33 1\n34 2\n35 2\n36 2\n37 2\n38 1\n39 2\n40 2\n41 1\n42 2\n43 2\n44 2\n45 1\n46 2\n47 2\n48 2\n49 2\n50 2\n51 2\n52 2\n53 52\n54 53\n55 54\n56 55\n57 56\n58 57\n59 58",
"output": "2\n2\n3\n3\n3\n4\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6\n6"
},
{
"input": "2\n001010011100101110111\n001100110011001100110011001100110011001100110011001100111001\n14\n1 2\n3 3\n4 4\n5 5\n6 6\n7 7\n8 8\n9 9\n10 10\n11 11\n12 12\n13 13\n14 14\n15 15",
"output": "2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2"
},
{
"input": "2\n1\n0\n40\n1 2\n3 3\n4 4\n5 5\n6 6\n7 7\n8 8\n9 9\n10 10\n11 11\n12 12\n13 13\n14 14\n15 15\n16 16\n17 17\n18 18\n19 19\n20 20\n21 21\n22 22\n23 23\n24 24\n25 25\n26 26\n27 27\n28 28\n29 29\n30 30\n31 31\n32 32\n33 33\n34 34\n35 35\n36 36\n37 37\n38 38\n39 39\n40 40\n41 41",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1"
},
{
"input": "2\n011\n100\n63\n1 1\n3 3\n4 4\n5 5\n6 6\n7 7\n8 8\n9 9\n10 10\n11 11\n12 12\n13 13\n14 14\n15 15\n16 16\n17 17\n18 18\n19 19\n20 20\n21 21\n22 22\n23 23\n24 24\n25 25\n26 26\n27 27\n28 28\n29 29\n30 30\n31 31\n32 32\n2 2\n34 34\n35 35\n36 36\n37 37\n38 38\n39 39\n40 40\n41 41\n42 42\n43 43\n44 44\n45 45\n46 46\n47 47\n48 48\n49 49\n50 50\n51 51\n52 52\n53 53\n54 54\n55 55\n56 56\n57 57\n58 58\n59 59\n60 60\n61 61\n62 62\n63 63\n33 64",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n2"
},
{
"input": "1\n0000000000000000000000000000000000000000000000000000000000000000\n25\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n7 7\n8 8\n9 9\n10 10\n11 11\n12 12\n13 13\n14 14\n15 15\n16 16\n17 17\n18 18\n19 19\n20 20\n21 21\n22 22\n23 23\n24 24\n25 25",
"output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0"
}
] | 810 | 268,390,400 | 0 | 6,630 |
|
999 | Cards and Joy | [
"dp"
] | null | null | There are $n$ players sitting at the card table. Each player has a favorite number. The favorite number of the $j$-th player is $f_j$.
There are $k \cdot n$ cards on the table. Each card contains a single integer: the $i$-th card contains number $c_i$. Also, you are given a sequence $h_1, h_2, \dots, h_k$. Its meaning will be explained below.
The players have to distribute all the cards in such a way that each of them will hold exactly $k$ cards. After all the cards are distributed, each player counts the number of cards he has that contains his favorite number. The joy level of a player equals $h_t$ if the player holds $t$ cards containing his favorite number. If a player gets no cards with his favorite number (i.e., $t=0$), his joy level is $0$.
Print the maximum possible total joy levels of the players after the cards are distributed. Note that the sequence $h_1, \dots, h_k$ is the same for all the players. | The first line of input contains two integers $n$ and $k$ ($1 \le n \le 500, 1 \le k \le 10$) β the number of players and the number of cards each player will get.
The second line contains $k \cdot n$ integers $c_1, c_2, \dots, c_{k \cdot n}$ ($1 \le c_i \le 10^5$) β the numbers written on the cards.
The third line contains $n$ integers $f_1, f_2, \dots, f_n$ ($1 \le f_j \le 10^5$) β the favorite numbers of the players.
The fourth line contains $k$ integers $h_1, h_2, \dots, h_k$ ($1 \le h_t \le 10^5$), where $h_t$ is the joy level of a player if he gets exactly $t$ cards with his favorite number written on them. It is guaranteed that the condition $h_{t - 1} < h_t$ holds for each $t \in [2..k]$. | Print one integer β the maximum possible total joy levels of the players among all possible card distributions. | [
"4 3\n1 3 2 8 5 5 8 2 2 8 5 2\n1 2 2 5\n2 6 7\n",
"3 3\n9 9 9 9 9 9 9 9 9\n1 2 3\n1 2 3\n"
] | [
"21\n",
"0\n"
] | In the first example, one possible optimal card distribution is the following:
- Player $1$ gets cards with numbers $[1, 3, 8]$; - Player $2$ gets cards with numbers $[2, 2, 8]$; - Player $3$ gets cards with numbers $[2, 2, 8]$; - Player $4$ gets cards with numbers $[5, 5, 5]$.
Thus, the answer is $2 + 6 + 6 + 7 = 21$.
In the second example, no player can get a card with his favorite number. Thus, the answer is $0$. | [
{
"input": "4 3\n1 3 2 8 5 5 8 2 2 8 5 2\n1 2 2 5\n2 6 7",
"output": "21"
},
{
"input": "3 3\n9 9 9 9 9 9 9 9 9\n1 2 3\n1 2 3",
"output": "0"
},
{
"input": "1 1\n1\n2\n1",
"output": "0"
},
{
"input": "1 1\n1\n1\n1",
"output": "1"
},
{
"input": "1 1\n1\n1\n100000",
"output": "100000"
},
{
"input": "50 1\n52 96 99 37 143 148 10 140 131 29 82 134 56 73 121 57 98 101 134 4 103 10 86 70 4 98 102 35 149 47 136 87 4 127 142 105 78 10 123 75 67 149 81 78 34 79 62 12 43 115\n31 132 59 75 4 135 138 33 33 60 135 5 30 127 61 74 102 131 11 16 74 4 101 74 70 45 29 12 137 59 24 52 25 122 64 147 92 77 23 6 19 76 26 55 126 130 4 148 86 3\n94393",
"output": "1321502"
},
{
"input": "50 1\n995 1815 941 1716 725 1098 747 627 1728 1007 34 1001 679 1742 22 1495 1299 1696 507 631 1971 775 1052 1665 1035 203 1564 1329 1592 1295 983 177 734 1442 172 943 33 486 1078 946 947 592 1524 563 396 1541 1670 326 543 79\n176 214 1601 1758 1468 972 628 1524 1506 425 746 309 387 1761 1002 625 496 1638 1855 1115 47 1813 1258 289 891 518 1247 1782 788 1449 1174 183 899 1728 366 1270 1641 327 1839 1093 223 1005 1214 1866 1432 1715 25 1240 1234 324\n52314",
"output": "104628"
}
] | 2,000 | 63,385,600 | 0 | 6,635 |
|
374 | Inna and Nine | [
"combinatorics",
"greedy"
] | null | null | Inna loves digit 9 very much. That's why she asked Dima to write a small number consisting of nines. But Dima must have misunderstood her and he wrote a very large number *a*, consisting of digits from 1 to 9.
Inna wants to slightly alter the number Dima wrote so that in the end the number contained as many digits nine as possible. In one move, Inna can choose two adjacent digits in a number which sum equals 9 and replace them by a single digit 9.
For instance, Inna can alter number 14545181 like this: 14545181<=β<=1945181<=β<=194519<=β<=19919. Also, she can use this method to transform number 14545181 into number 19991. Inna will not transform it into 149591 as she can get numbers 19919 and 19991 which contain more digits nine.
Dima is a programmer so he wants to find out how many distinct numbers containing as many digits nine as possible Inna can get from the written number. Help him with this challenging task. | The first line of the input contains integer *a* (1<=β€<=*a*<=β€<=10100000). Number *a* doesn't have any zeroes. | In a single line print a single integer β the answer to the problem. It is guaranteed that the answer to the problem doesn't exceed 263<=-<=1.
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. | [
"369727\n",
"123456789987654321\n",
"1\n"
] | [
"2\n",
"1\n",
"1\n"
] | Notes to the samples
In the first sample Inna can get the following numbers: 369727βββ99727βββ9997, 369727βββ99727βββ9979.
In the second sample, Inna can act like this: 123456789987654321βββ12396789987654321βββ1239678998769321. | [
{
"input": "369727",
"output": "2"
},
{
"input": "123456789987654321",
"output": "1"
},
{
"input": "1",
"output": "1"
},
{
"input": "3636363636363454545454543636363636454545452727272727218181818181999111777",
"output": "1512"
},
{
"input": "1188",
"output": "1"
},
{
"input": "121212912121291299129191219",
"output": "1"
},
{
"input": "181818918181891918918181918189181818181891818191818191819189",
"output": "54"
},
{
"input": "12191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219121912191219",
"output": "1"
},
{
"input": "444444444444445444444444454444444444444444444444445544444444444444444444444444444444444444444554444444444444444444444444444444444444445454444444444444444444444444444444444444454444444444444444444444444444444444445444444444444444444444444444444444444444444445444444444444444444444444444444444456666666666666666666663555555555555555888888888882333333312567312389542179415242164512341234213443123412341293412341234123412746129342154796124123459123491238471234213451692341278451234125934",
"output": "96"
},
{
"input": "123456789",
"output": "1"
},
{
"input": "3639272918194549",
"output": "16"
},
{
"input": "1121314151617181921222324252627282931323334353637383941424344454647484951525354556575859616263646566768697172737475767787981828384858687888991929394959696979899",
"output": "256"
},
{
"input": "14545181",
"output": "2"
},
{
"input": "272727272",
"output": "5"
},
{
"input": "1212121217272727121",
"output": "4"
},
{
"input": "1212172727",
"output": "3"
},
{
"input": "181817272727",
"output": "12"
}
] | 655 | 11,264,000 | 3 | 6,637 |
|
877 | Slava and tanks | [
"constructive algorithms"
] | null | null | Slava plays his favorite game "Peace Lightning". Now he is flying a bomber on a very specific map.
Formally, map is a checkered field of size 1<=Γ<=*n*, the cells of which are numbered from 1 to *n*, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he flies very high, but he can drop a bomb in any cell. All tanks in this cell will be damaged.
If a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell *n* can only move to the cell *n*<=-<=1, a tank in the cell 1 can only move to the cell 2). If a tank takes damage for the second time, it's counted as destroyed and never moves again. The tanks move only when they are damaged for the first time, they do not move by themselves.
Help Slava to destroy all tanks using as few bombs as possible. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=100<=000) β the size of the map. | In the first line print *m* β the minimum number of bombs Slava needs to destroy all tanks.
In the second line print *m* integers *k*1,<=*k*2,<=...,<=*k**m*. The number *k**i* means that the *i*-th bomb should be dropped at the cell *k**i*.
If there are multiple answers, you can print any of them. | [
"2\n",
"3\n"
] | [
"3\n2 1 2 ",
"4\n2 1 3 2 "
] | none | [
{
"input": "2",
"output": "3\n2 1 2 "
},
{
"input": "3",
"output": "4\n2 1 3 2 "
},
{
"input": "4",
"output": "6\n2 4 1 3 2 4 "
},
{
"input": "6",
"output": "9\n2 4 6 1 3 5 2 4 6 "
},
{
"input": "10",
"output": "15\n2 4 6 8 10 1 3 5 7 9 2 4 6 8 10 "
},
{
"input": "15",
"output": "22\n2 4 6 8 10 12 14 1 3 5 7 9 11 13 15 2 4 6 8 10 12 14 "
},
{
"input": "100",
"output": "150\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 "
},
{
"input": "100000",
"output": "150000\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 ..."
},
{
"input": "55333",
"output": "82999\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "42273",
"output": "63409\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "29857",
"output": "44785\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "27687",
"output": "41530\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "25517",
"output": "38275\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "23347",
"output": "35020\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "10931",
"output": "16396\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "98514",
"output": "147771\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 ..."
},
{
"input": "6591",
"output": "9886\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 28..."
},
{
"input": "94174",
"output": "141261\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 ..."
},
{
"input": "92004",
"output": "138006\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 ..."
},
{
"input": "59221",
"output": "88831\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "46806",
"output": "70209\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "54882",
"output": "82323\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "42466",
"output": "63699\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "40296",
"output": "60444\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "27880",
"output": "41820\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "25710",
"output": "38565\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "23540",
"output": "35310\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "11124",
"output": "16686\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "8954",
"output": "13431\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 2..."
},
{
"input": "85387",
"output": "128080\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 ..."
},
{
"input": "72971",
"output": "109456\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 ..."
},
{
"input": "81047",
"output": "121570\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 232 234 236 238 240 242 244 246 248 250 252 254 256 258 260 262 264 266 268 270 272 274 276 278 ..."
},
{
"input": "5",
"output": "7\n2 4 1 3 5 2 4 "
}
] | 62 | 0 | 0 | 6,638 |
|
23 | Tree | [
"dp"
] | E. Tree | 2 | 256 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. | The first input line contains integer number *n* (1<=β€<=*n*<=β€<=700) β amount of vertices in the tree. The following *n*<=-<=1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, *a**i*, *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*). It's guaranteed that the graph described in the input is a tree. | Output the only number β the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | [
"5\n1 2\n2 3\n3 4\n4 5\n",
"8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8\n",
"3\n1 2\n1 3\n"
] | [
"6",
"18",
"3"
] | none | [
{
"input": "5\n1 2\n2 3\n3 4\n4 5",
"output": "6"
},
{
"input": "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8",
"output": "18"
},
{
"input": "3\n1 2\n1 3",
"output": "3"
},
{
"input": "5\n3 2\n1 5\n4 5\n5 3",
"output": "6"
},
{
"input": "5\n2 1\n3 4\n3 5\n5 2",
"output": "6"
},
{
"input": "5\n1 4\n4 5\n4 3\n4 2",
"output": "5"
},
{
"input": "5\n2 3\n3 4\n3 5\n3 1",
"output": "5"
},
{
"input": "10\n4 8\n10 2\n6 3\n10 9\n2 3\n4 1\n7 10\n2 1\n5 1",
"output": "36"
},
{
"input": "15\n4 6\n15 1\n3 8\n15 2\n13 11\n9 10\n14 4\n9 12\n11 6\n8 10\n4 5\n2 11\n7 8\n8 4",
"output": "243"
},
{
"input": "50\n45 39\n18 12\n13 6\n48 45\n46 36\n46 8\n40 36\n29 28\n2 18\n43 26\n25 14\n43 31\n13 30\n12 35\n38 42\n20 5\n7 35\n10 50\n27 13\n1 41\n24 21\n25 5\n7 31\n15 45\n32 25\n43 23\n4 50\n46 11\n40 14\n37 21\n50 2\n41 42\n43 30\n14 22\n36 48\n8 24\n49 20\n19 26\n32 10\n35 29\n1 6\n34 33\n29 3\n6 9\n34 39\n5 47\n16 9\n31 44\n33 17",
"output": "68024448"
},
{
"input": "10\n8 10\n5 7\n1 6\n4 9\n3 8\n8 9\n2 3\n5 8\n8 1",
"output": "32"
},
{
"input": "5\n1 5\n4 3\n2 4\n4 1",
"output": "6"
},
{
"input": "1",
"output": "1"
},
{
"input": "2\n2 1",
"output": "2"
},
{
"input": "3\n1 2\n2 3",
"output": "3"
}
] | 0 | 0 | -1 | 6,651 |
746 | Green and Black Tea | [
"constructive algorithms",
"greedy",
"math"
] | null | null | Innokentiy likes tea very much and today he wants to drink exactly *n* cups of tea. He would be happy to drink more but he had exactly *n* tea bags, *a* of them are green and *b* are black.
Innokentiy doesn't like to drink the same tea (green or black) more than *k* times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink *n* cups of tea, without drinking the same tea more than *k* times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once. | The first line contains four integers *n*, *k*, *a* and *b* (1<=β€<=*k*<=β€<=*n*<=β€<=105, 0<=β€<=*a*,<=*b*<=β€<=*n*)Β β the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that *a*<=+<=*b*<==<=*n*. | If it is impossible to drink *n* cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length *n*, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.
If there are multiple answers, print any of them. | [
"5 1 3 2\n",
"7 2 2 5\n",
"4 3 4 0\n"
] | [
"GBGBG\n",
"BBGBGBB",
"NO\n"
] | none | [
{
"input": "5 1 3 2",
"output": "GBGBG"
},
{
"input": "7 2 2 5",
"output": "BBGBBGB"
},
{
"input": "4 3 4 0",
"output": "NO"
},
{
"input": "2 2 0 2",
"output": "BB"
},
{
"input": "3 2 0 3",
"output": "NO"
},
{
"input": "1 1 0 1",
"output": "B"
},
{
"input": "1 1 1 0",
"output": "G"
},
{
"input": "11 2 3 8",
"output": "BBGBBGBBGBB"
},
{
"input": "100000 39 24855 75145",
"output": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..."
},
{
"input": "2 2 2 0",
"output": "GG"
},
{
"input": "2 2 1 1",
"output": "GB"
},
{
"input": "3 2 2 1",
"output": "GGB"
},
{
"input": "3 2 1 2",
"output": "BBG"
},
{
"input": "5 1 4 1",
"output": "NO"
},
{
"input": "10 1 7 3",
"output": "NO"
},
{
"input": "20 1 5 15",
"output": "NO"
},
{
"input": "1000 123 447 553",
"output": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGB..."
},
{
"input": "3000 70 2946 54",
"output": "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGG..."
},
{
"input": "10000 590 4020 5980",
"output": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..."
},
{
"input": "10001 1841 1052 8949",
"output": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..."
},
{
"input": "50000 104 31045 18955",
"output": "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGBGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG..."
},
{
"input": "59999 16660 46835 13164",
"output": "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG..."
},
{
"input": "70000 3017 31589 38411",
"output": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..."
},
{
"input": "99999 15805 82842 17157",
"output": "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG..."
},
{
"input": "100000 6397 59122 40878",
"output": "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG..."
},
{
"input": "100000 856 69042 30958",
"output": "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG..."
},
{
"input": "6 1 3 3",
"output": "GBGBGB"
},
{
"input": "9 2 3 6",
"output": "BBGBBGBBG"
},
{
"input": "9 1 6 3",
"output": "NO"
},
{
"input": "10 1 4 6",
"output": "NO"
},
{
"input": "10 1 3 7",
"output": "NO"
},
{
"input": "10 1 2 8",
"output": "NO"
},
{
"input": "10 1 5 5",
"output": "GBGBGBGBGB"
},
{
"input": "11 1 2 9",
"output": "NO"
},
{
"input": "11 2 4 7",
"output": "BBGBBGBBGBG"
},
{
"input": "11 2 5 6",
"output": "BBGBGBGBGBG"
},
{
"input": "11 2 6 5",
"output": "GGBGBGBGBGB"
},
{
"input": "11 1 7 4",
"output": "NO"
},
{
"input": "11 2 8 3",
"output": "GGBGGBGGBGG"
},
{
"input": "11 1 9 2",
"output": "NO"
},
{
"input": "99999 10760 33333 66666",
"output": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..."
},
{
"input": "99999 3434 66666 33333",
"output": "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG..."
},
{
"input": "99999 7343 33332 66667",
"output": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..."
},
{
"input": "99999 177 33334 66665",
"output": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..."
},
{
"input": "99999 3580 66665 33334",
"output": "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG..."
},
{
"input": "99999 1681 66667 33332",
"output": "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG..."
},
{
"input": "7 3 2 5",
"output": "BBBGBBG"
},
{
"input": "9 2 7 2",
"output": "NO"
},
{
"input": "10 2 8 2",
"output": "NO"
},
{
"input": "100000 50000 50000 50000",
"output": "GBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBGBG..."
}
] | 92 | 2,764,800 | 0 | 6,657 |
|
228 | Zigzag | [
"data structures"
] | null | null | The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function.
The Zigag's sequence with the zigzag factor z is an infinite sequence *S**i**z* (*i*<=β₯<=1;Β *z*<=β₯<=2), that is determined as follows:
- *S**i**z*<==<=2, when ; - , when ; - , when .
Operation means taking the remainder from dividing number *x* by number *y*. For example, the beginning of sequence *S**i*3 (zigzag factor 3) looks as follows: 1, 2, 3, 2, 1, 2, 3, 2, 1.
Let's assume that we are given an array *a*, consisting of *n* integers. Let's define element number *i* (1<=β€<=*i*<=β€<=*n*) of the array as *a**i*. The Zigzag function is function , where *l*,<=*r*,<=*z* satisfy the inequalities 1<=β€<=*l*<=β€<=*r*<=β€<=*n*, *z*<=β₯<=2.
To become better acquainted with the Zigzag sequence and the Zigzag function, the wizard offers you to implement the following operations on the given array *a*.
1. The assignment operation. The operation parameters are (*p*,<=*v*). The operation denotes assigning value *v* to the *p*-th array element. After the operation is applied, the value of the array element *a**p* equals *v*. 1. The Zigzag operation. The operation parameters are (*l*,<=*r*,<=*z*). The operation denotes calculating the Zigzag function *Z*(*l*,<=*r*,<=*z*).
Explore the magical powers of zigzags, implement the described operations. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β The number of elements in array *a*. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β the elements of the array.
The third line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of operations. Next *m* lines contain the operations' descriptions. An operation's description starts with integer *t**i* (1<=β€<=*t**i*<=β€<=2) β the operation type.
- If *t**i*<==<=1 (assignment operation), then on the line follow two space-separated integers: *p**i*,<=*v**i* (1<=β€<=*p**i*<=β€<=*n*;Β 1<=β€<=*v**i*<=β€<=109) β the parameters of the assigning operation. - If *t**i*<==<=2 (Zigzag operation), then on the line follow three space-separated integers: *l**i*,<=*r**i*,<=*z**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*;Β 2<=β€<=*z**i*<=β€<=6) β the parameters of the Zigzag operation.
You should execute the operations in the order, in which they are given in the input. | For each Zigzag operation print the calculated value of the Zigzag function on a single line. Print the values for Zigzag functions in the order, in which they are given in the input.
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. | [
"5\n2 3 1 5 5\n4\n2 2 3 2\n2 1 5 3\n1 3 5\n2 1 5 3\n"
] | [
"5\n26\n38\n"
] | Explanation of the sample test:
- Result of the first operation is *Z*(2,β3,β2)β=β3Β·1β+β1Β·2β=β5. - Result of the second operation is *Z*(1,β5,β3)β=β2Β·1β+β3Β·2β+β1Β·3β+β5Β·2β+β5Β·1β=β26. - After the third operation array *a* is equal to 2,β3,β5,β5,β5. - Result of the forth operation is *Z*(1,β5,β3)β=β2Β·1β+β3Β·2β+β5Β·3β+β5Β·2β+β5Β·1β=β38. | [] | 92 | 0 | 0 | 6,660 |
|
761 | Dasha and Puzzle | [
"constructive algorithms",
"dfs and similar",
"graphs",
"greedy",
"trees"
] | null | null | Dasha decided to have a rest after solving the problem. She had been ready to start her favourite activity β origami, but remembered the puzzle that she could not solve.
The tree is a non-oriented connected graph without cycles. In particular, there always are *n*<=-<=1 edges in a tree with *n* vertices.
The puzzle is to position the vertices at the points of the Cartesian plane with integral coordinates, so that the segments between the vertices connected by edges are parallel to the coordinate axes. Also, the intersection of segments is allowed only at their ends. Distinct vertices should be placed at different points.
Help Dasha to find any suitable way to position the tree vertices on the plane.
It is guaranteed that if it is possible to position the tree vertices on the plane without violating the condition which is given above, then you can do it by using points with integral coordinates which don't exceed 1018 in absolute value. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=30) β the number of vertices in the tree.
Each of next *n*<=-<=1 lines contains two integers *u**i*, *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*) that mean that the *i*-th edge of the tree connects vertices *u**i* and *v**i*.
It is guaranteed that the described graph is a tree. | If the puzzle doesn't have a solution then in the only line print "NO".
Otherwise, the first line should contain "YES". The next *n* lines should contain the pair of integers *x**i*, *y**i* (|*x**i*|,<=|*y**i*|<=β€<=1018) β the coordinates of the point which corresponds to the *i*-th vertex of the tree.
If there are several solutions, print any of them. | [
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7",
"6\n1 2\n2 3\n2 4\n2 5\n2 6\n",
"4\n1 2\n2 3\n3 4"
] | [
"YES\n0 0\n1 0\n0 1\n2 0\n1 -1\n-1 1\n0 2",
"NO\n",
"YES\n3 3\n4 3\n5 3\n6 3"
] | In the first sample one of the possible positions of tree is: <img class="tex-graphics" src="https://espresso.codeforces.com/360c9c903e5177970a5a3b2711f6718c2163ffd8.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7",
"output": "YES\n0 0\n0 1073741824\n1073741824 0\n0 1610612736\n536870912 1073741824\n1073741824 536870912\n1610612736 0"
},
{
"input": "6\n1 2\n2 3\n2 4\n2 5\n2 6",
"output": "NO"
},
{
"input": "4\n1 2\n2 3\n3 4",
"output": "YES\n0 0\n0 1073741824\n0 1610612736\n0 1879048192"
},
{
"input": "10\n2 9\n9 3\n3 10\n9 4\n9 6\n9 5\n2 7\n2 1\n2 8",
"output": "NO"
},
{
"input": "10\n2 5\n5 6\n6 1\n6 7\n6 8\n5 10\n5 3\n2 9\n2 4",
"output": "YES\n0 0\n0 1879048192\n-268435456 1610612736\n134217728 1879048192\n0 1610612736\n0 1073741824\n536870912 1073741824\n-536870912 1073741824\n0 2013265920\n268435456 1610612736"
},
{
"input": "8\n7 8\n8 2\n2 4\n8 6\n8 5\n7 3\n7 1",
"output": "YES\n0 0\n0 1879048192\n536870912 1073741824\n0 2013265920\n-268435456 1610612736\n268435456 1610612736\n0 1073741824\n0 1610612736"
},
{
"input": "10\n10 9\n9 3\n3 4\n4 5\n5 1\n1 8\n8 2\n2 6\n6 7",
"output": "YES\n0 0\n1073741824 536870912\n0 1879048192\n0 1610612736\n0 1073741824\n1073741824 805306368\n1073741824 939524096\n1073741824 0\n0 2013265920\n0 2080374784"
},
{
"input": "12\n1 5\n5 7\n7 4\n4 6\n4 2\n7 11\n7 9\n5 10\n5 3\n1 8\n1 12",
"output": "YES\n0 0\n134217728 1879048192\n-536870912 1073741824\n0 1879048192\n0 1073741824\n0 2013265920\n0 1610612736\n1073741824 0\n-268435456 1610612736\n536870912 1073741824\n268435456 1610612736\n0 -1073741824"
},
{
"input": "15\n2 5\n5 10\n10 14\n14 3\n3 7\n7 11\n3 6\n3 15\n14 1\n14 8\n14 12\n10 13\n5 4\n2 9",
"output": "NO"
},
{
"input": "20\n12 7\n7 17\n17 19\n19 15\n15 4\n4 5\n5 18\n18 16\n16 13\n13 2\n2 3\n16 8\n18 9\n18 11\n5 10\n5 14\n4 6\n17 1\n17 20",
"output": "YES\n0 0\n536870912 1606418432\n536870912 1608515584\n536870912 1476395008\n536870912 1543503872\n603979776 1476395008\n0 1610612736\n545259520 1593835520\n553648128 1577058304\n570425344 1543503872\n520093696 1577058304\n0 1879048192\n536870912 1602224128\n503316480 1543503872\n536870912 1342177280\n536870912 1593835520\n0 1073741824\n536870912 1577058304\n536870912 1073741824\n-536870912 1073741824"
},
{
"input": "21\n12 20\n20 6\n6 9\n9 11\n11 5\n5 7\n7 17\n17 16\n16 19\n19 8\n16 21\n17 13\n7 4\n5 18\n11 3\n11 1\n6 14\n6 2\n20 15\n20 10",
"output": "YES\n0 0\n-134217728 1879048192\n-536870912 1073741824\n671088640 1342177280\n536870912 1073741824\n0 1879048192\n536870912 1342177280\n536870912 1593835520\n0 1610612736\n-67108864 2013265920\n0 1073741824\n0 2080374784\n603979776 1476395008\n134217728 1879048192\n67108864 2013265920\n536870912 1543503872\n536870912 1476395008\n805306368 1073741824\n536870912 1577058304\n0 2013265920\n570425344 1543503872"
},
{
"input": "30\n21 11\n11 22\n22 24\n24 2\n2 8\n8 10\n10 28\n28 26\n26 29\n29 15\n29 16\n26 4\n26 3\n28 23\n28 18\n10 19\n10 14\n8 5\n8 1\n2 9\n2 17\n24 20\n24 13\n22 27\n22 6\n11 30\n11 7\n21 12\n21 25",
"output": "YES\n0 0\n0 1610612736\n469762048 1476395008\n603979776 1476395008\n-536870912 1073741824\n-67108864 2013265920\n-33554432 2080374784\n0 1073741824\n268435456 1610612736\n536870912 1073741824\n0 2080374784\n0 2130706432\n-134217728 1879048192\n536870912 805306368\n536870912 1577058304\n570425344 1543503872\n-268435456 1610612736\n402653184 1342177280\n805306368 1073741824\n134217728 1879048192\n0 2113929216\n0 2013265920\n671088640 1342177280\n0 1879048192\n16777216 2113929216\n536870912 1476395008\n671088..."
},
{
"input": "20\n6 20\n20 10\n10 5\n5 2\n2 7\n7 14\n14 4\n4 3\n14 15\n14 19\n7 18\n7 8\n2 13\n5 9\n5 1\n10 12\n20 11\n20 17\n6 16",
"output": "YES\n0 0\n536870912 1073741824\n536870912 1577058304\n536870912 1543503872\n0 1073741824\n0 2013265920\n536870912 1342177280\n402653184 1342177280\n-536870912 1073741824\n0 1610612736\n134217728 1879048192\n268435456 1610612736\n805306368 1073741824\n536870912 1476395008\n603979776 1476395008\n0 2080374784\n-134217728 1879048192\n671088640 1342177280\n469762048 1476395008\n0 1879048192"
},
{
"input": "15\n8 14\n14 3\n3 1\n1 13\n13 5\n5 15\n15 2\n15 4\n5 10\n13 6\n1 12\n3 11\n14 7\n8 9",
"output": "YES\n0 0\n1073741824 939524096\n0 1073741824\n1207959552 805306368\n1073741824 536870912\n1610612736 0\n268435456 1610612736\n0 1879048192\n0 2013265920\n1342177280 536870912\n536870912 1073741824\n0 -1073741824\n1073741824 0\n0 1610612736\n1073741824 805306368"
},
{
"input": "30\n29 21\n21 16\n16 4\n4 27\n27 13\n13 30\n30 15\n15 14\n14 25\n25 26\n26 1\n1 19\n19 3\n3 2\n2 20\n20 18\n18 8\n8 10\n10 28\n28 17\n17 7\n7 11\n11 6\n6 5\n5 12\n12 23\n23 24\n24 9\n9 22",
"output": "YES\n0 0\n1073741824 805306368\n1073741824 536870912\n0 2139095040\n1073741824 1073479680\n1073741824 1073217536\n1073741824 1071644672\n1073741824 1040187392\n1073741824 1073725440\n1073741824 1056964608\n1073741824 1072693248\n1073741824 1073610752\n0 2113929216\n0 1879048192\n0 2013265920\n0 2143289344\n1073741824 1069547520\n1073741824 1006632960\n1073741824 0\n1073741824 939524096\n0 2145386496\n1073741824 1073733632\n1073741824 1073676288\n1073741824 1073709056\n0 1610612736\n0 1073741824\n0 21307064..."
},
{
"input": "30\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n4 8\n4 9\n5 10\n5 11\n6 12\n6 13\n7 14\n7 15\n8 16\n8 17\n9 18\n9 19\n10 20\n10 21\n11 22\n11 23\n12 24\n12 25\n13 26\n13 27\n14 28\n14 29\n15 30",
"output": "YES\n0 0\n0 1073741824\n1073741824 0\n0 1610612736\n536870912 1073741824\n1073741824 536870912\n1610612736 0\n0 1879048192\n268435456 1610612736\n536870912 1342177280\n805306368 1073741824\n1073741824 805306368\n1342177280 536870912\n1610612736 268435456\n1879048192 0\n0 2013265920\n134217728 1879048192\n268435456 1744830464\n402653184 1610612736\n536870912 1476395008\n671088640 1342177280\n805306368 1207959552\n939524096 1073741824\n1073741824 939524096\n1207959552 805306368\n1342177280 671088640\n1476395..."
},
{
"input": "13\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\n3 10\n4 11\n4 12\n4 13",
"output": "YES\n0 0\n0 1073741824\n1073741824 0\n0 -1073741824\n0 1610612736\n536870912 1073741824\n-536870912 1073741824\n1073741824 536870912\n1610612736 0\n1073741824 -536870912\n536870912 -1073741824\n0 -1610612736\n-536870912 -1073741824"
},
{
"input": "20\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n3 9\n3 10\n3 11\n4 12\n4 13\n4 14\n5 15\n5 16\n5 17\n18 17\n19 17\n20 17",
"output": "YES\n0 0\n0 1073741824\n1073741824 0\n0 -1073741824\n-1073741824 0\n0 1610612736\n536870912 1073741824\n-536870912 1073741824\n1073741824 536870912\n1610612736 0\n1073741824 -536870912\n536870912 -1073741824\n0 -1610612736\n-536870912 -1073741824\n-1073741824 536870912\n-1073741824 -536870912\n-1610612736 0\n-1610612736 268435456\n-1610612736 -268435456\n-1879048192 0"
},
{
"input": "1",
"output": "YES\n0 0"
},
{
"input": "20\n6 1\n7 1\n8 1\n6 5\n5 4\n4 3\n3 2\n7 9\n9 10\n10 11\n11 12\n12 13\n14 8\n15 14\n15 16\n17 16\n17 18\n18 19\n19 20",
"output": "YES\n0 0\n0 2080374784\n0 2013265920\n0 1879048192\n0 1610612736\n0 1073741824\n1073741824 0\n0 -1073741824\n1073741824 536870912\n1073741824 805306368\n1073741824 939524096\n1073741824 1006632960\n1073741824 1040187392\n536870912 -1073741824\n536870912 -805306368\n536870912 -671088640\n536870912 -603979776\n536870912 -570425344\n536870912 -553648128\n536870912 -545259520"
},
{
"input": "25\n1 2\n1 3\n1 4\n2 5\n5 6\n6 7\n7 8\n2 9\n9 10\n11 2\n12 11\n13 12\n3 14\n14 15\n14 16\n16 17\n14 18\n18 19\n20 4\n20 21\n20 22\n22 23\n20 24\n24 25",
"output": "YES\n0 0\n0 1073741824\n1073741824 0\n0 -1073741824\n0 1610612736\n0 1879048192\n0 2013265920\n0 2080374784\n536870912 1073741824\n536870912 1342177280\n-536870912 1073741824\n-536870912 1342177280\n-536870912 1476395008\n1073741824 536870912\n1073741824 805306368\n1342177280 536870912\n1342177280 671088640\n805306368 536870912\n805306368 671088640\n536870912 -1073741824\n536870912 -805306368\n805306368 -1073741824\n805306368 -939524096\n536870912 -1342177280\n671088640 -1342177280"
},
{
"input": "30\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",
"output": "NO"
},
{
"input": "30\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n2 10\n10 11\n10 12\n10 13\n2 14\n14 15\n14 16\n14 17\n4 18\n18 19\n18 20\n18 21\n4 22\n22 23\n22 24\n22 25\n6 26\n26 27\n6 28\n28 29\n28 30",
"output": "YES\n0 0\n0 1073741824\n0 1610612736\n0 1879048192\n0 2013265920\n0 2080374784\n0 2113929216\n0 2130706432\n0 2139095040\n536870912 1073741824\n536870912 1342177280\n805306368 1073741824\n536870912 805306368\n-536870912 1073741824\n-536870912 1342177280\n-536870912 805306368\n-805306368 1073741824\n134217728 1879048192\n134217728 1946157056\n201326592 1879048192\n134217728 1811939328\n-134217728 1879048192\n-134217728 1946157056\n-134217728 1811939328\n-201326592 1879048192\n33554432 2080374784\n33554432 2..."
},
{
"input": "28\n24 3\n3 8\n8 21\n21 23\n23 6\n6 16\n16 22\n22 11\n11 12\n11 20\n11 7\n22 15\n22 13\n16 14\n16 27\n6 26\n6 19\n23 17\n23 1\n21 2\n21 18\n8 28\n8 25\n3 5\n3 10\n24 9\n24 4",
"output": "YES\n0 0\n268435456 1610612736\n0 2013265920\n33554432 2080374784\n67108864 2013265920\n536870912 1073741824\n503316480 1543503872\n0 1879048192\n0 2113929216\n-67108864 2013265920\n536870912 1543503872\n536870912 1577058304\n469762048 1476395008\n671088640 1342177280\n603979776 1476395008\n536870912 1342177280\n-536870912 1073741824\n-268435456 1610612736\n536870912 805306368\n570425344 1543503872\n0 1610612736\n536870912 1476395008\n0 1073741824\n0 2080374784\n-134217728 1879048192\n805306368 1073741824\n..."
},
{
"input": "21\n17 7\n7 14\n14 6\n6 2\n2 20\n20 11\n11 4\n11 18\n20 16\n20 13\n2 1\n2 15\n6 19\n6 5\n14 21\n14 10\n7 3\n7 12\n17 9\n17 8",
"output": "YES\n0 0\n0 1073741824\n67108864 2013265920\n536870912 1476395008\n-268435456 1610612736\n0 1610612736\n0 2013265920\n33554432 2080374784\n0 2113929216\n-134217728 1879048192\n536870912 1342177280\n-67108864 2013265920\n536870912 805306368\n0 1879048192\n-536870912 1073741824\n805306368 1073741824\n0 2080374784\n671088640 1342177280\n268435456 1610612736\n536870912 1073741824\n134217728 1879048192"
},
{
"input": "17\n9 17\n17 4\n4 1\n1 3\n3 14\n14 7\n7 16\n16 10\n16 15\n7 8\n14 13\n3 11\n1 6\n4 5\n17 2\n9 12",
"output": "YES\n0 0\n268435456 1610612736\n1073741824 0\n0 1073741824\n536870912 1073741824\n0 -1073741824\n1073741824 805306368\n1207959552 805306368\n0 1879048192\n1073741824 1006632960\n1610612736 0\n0 2013265920\n1342177280 536870912\n1073741824 536870912\n1140850688 939524096\n1073741824 939524096\n0 1610612736"
},
{
"input": "19\n3 12\n12 11\n11 17\n17 2\n2 19\n19 16\n19 4\n19 1\n2 8\n2 5\n17 14\n17 10\n11 13\n11 9\n12 18\n12 6\n3 15\n3 7",
"output": "YES\n0 0\n0 1610612736\n0 2113929216\n-536870912 1073741824\n-268435456 1610612736\n-33554432 2080374784\n16777216 2113929216\n268435456 1610612736\n-67108864 2013265920\n-134217728 1879048192\n0 2013265920\n0 2080374784\n67108864 2013265920\n134217728 1879048192\n0 2130706432\n536870912 1073741824\n0 1879048192\n33554432 2080374784\n0 1073741824"
},
{
"input": "18\n17 13\n13 11\n11 9\n9 15\n15 3\n3 16\n3 14\n15 10\n15 5\n9 1\n9 7\n11 4\n11 2\n13 6\n13 12\n17 18\n17 8",
"output": "YES\n0 0\n-268435456 1610612736\n536870912 1342177280\n268435456 1610612736\n536870912 805306368\n134217728 1879048192\n-536870912 1073741824\n67108864 2013265920\n0 1073741824\n805306368 1073741824\n0 1610612736\n-134217728 1879048192\n0 1879048192\n671088640 1342177280\n536870912 1073741824\n536870912 1476395008\n0 2013265920\n0 2080374784"
},
{
"input": "30\n29 3\n3 13\n13 7\n7 5\n5 6\n6 10\n10 8\n8 26\n26 17\n26 15\n8 25\n8 12\n8 11\n10 27\n10 14\n6 21\n5 2\n5 1\n5 19\n5 30\n7 4\n13 18\n3 9\n3 28\n3 24\n3 20\n29 16\n29 23\n29 22",
"output": "NO"
},
{
"input": "30\n20 15\n15 2\n2 1\n1 29\n29 18\n18 8\n8 12\n12 22\n22 30\n30 16\n16 28\n28 9\n9 11\n11 21\n9 6\n16 27\n16 26\n30 4\n22 5\n18 17\n29 13\n29 23\n1 19\n1 24\n2 7\n15 10\n15 3\n20 14\n20 25",
"output": "YES\n0 0\n0 1073741824\n-268435456 1610612736\n1090519040 1040187392\n1107296256 1006632960\n1075838976 1069547520\n536870912 1073741824\n1073741824 805306368\n1073741824 1069547520\n268435456 1610612736\n1073741824 1071644672\n1073741824 939524096\n1610612736 0\n0 2013265920\n0 1610612736\n1073741824 1056964608\n1342177280 536870912\n1073741824 536870912\n0 -1073741824\n0 1879048192\n1073741824 1072693248\n1073741824 1006632960\n1073741824 -536870912\n-1073741824 0\n134217728 1879048192\n1065353216 105696..."
},
{
"input": "30\n28 30\n30 20\n20 29\n29 15\n15 2\n2 27\n27 6\n6 4\n6 11\n6 7\n6 10\n6 24\n27 14\n27 5\n27 22\n2 16\n15 13\n15 9\n15 25\n29 1\n29 3\n29 12\n29 26\n30 8\n30 18\n30 23\n30 17\n30 19\n28 21",
"output": "NO"
},
{
"input": "2\n2 1",
"output": "YES\n0 0\n0 1073741824"
},
{
"input": "3\n1 2\n2 3",
"output": "YES\n0 0\n0 1073741824\n0 1610612736"
},
{
"input": "4\n2 1\n1 3\n3 4",
"output": "YES\n0 0\n0 1073741824\n1073741824 0\n1073741824 536870912"
},
{
"input": "5\n2 4\n4 1\n1 3\n3 5",
"output": "YES\n0 0\n0 1610612736\n1073741824 0\n0 1073741824\n1073741824 536870912"
},
{
"input": "6\n3 4\n4 1\n1 5\n5 2\n2 6",
"output": "YES\n0 0\n1073741824 536870912\n0 1610612736\n0 1073741824\n1073741824 0\n1073741824 805306368"
},
{
"input": "7\n5 4\n4 7\n7 1\n1 2\n2 3\n3 6",
"output": "YES\n0 0\n1073741824 0\n1073741824 536870912\n0 1610612736\n0 1879048192\n1073741824 805306368\n0 1073741824"
},
{
"input": "8\n5 6\n6 8\n8 2\n2 7\n7 1\n1 4\n4 3",
"output": "YES\n0 0\n0 1610612736\n1073741824 536870912\n1073741824 0\n0 2080374784\n0 2013265920\n0 1073741824\n0 1879048192"
},
{
"input": "9\n7 1\n1 4\n4 5\n5 6\n6 2\n2 8\n8 3\n3 9",
"output": "YES\n0 0\n1073741824 939524096\n1073741824 1040187392\n1073741824 0\n1073741824 536870912\n1073741824 805306368\n0 1073741824\n1073741824 1006632960\n1073741824 1056964608"
},
{
"input": "3\n2 1\n3 1",
"output": "YES\n0 0\n0 1073741824\n1073741824 0"
},
{
"input": "4\n2 1\n1 3\n1 4",
"output": "YES\n0 0\n0 1073741824\n1073741824 0\n0 -1073741824"
},
{
"input": "5\n5 1\n1 4\n1 3\n2 1",
"output": "YES\n0 0\n-1073741824 0\n0 -1073741824\n1073741824 0\n0 1073741824"
},
{
"input": "30\n1 15\n15 30\n30 14\n14 16\n16 19\n19 12\n19 22\n19 2\n16 9\n16 21\n16 23\n16 24\n14 7\n14 29\n14 17\n14 18\n30 13\n30 27\n30 4\n30 8\n15 10\n15 11\n15 5\n15 3\n15 25\n1 6\n1 26\n1 28\n1 20",
"output": "NO"
},
{
"input": "30\n2 29\n29 26\n26 13\n13 30\n30 24\n24 20\n20 3\n3 19\n19 8\n8 27\n27 14\n14 12\n12 23\n14 25\n27 4\n8 7\n8 21\n19 6\n19 16\n3 1\n20 18\n24 9\n30 10\n30 22\n13 17\n26 15\n29 5\n29 11\n2 28",
"output": "YES\n0 0\n0 2139095040\n0 1073741824\n603979776 1476395008\n8388608 2130706432\n805306368 1073741824\n671088640 1342177280\n536870912 1342177280\n134217728 1879048192\n67108864 2013265920\n-8388608 2130706432\n536870912 1577058304\n0 2080374784\n536870912 1543503872\n16777216 2113929216\n536870912 805306368\n33554432 2080374784\n268435456 1610612736\n536870912 1073741824\n0 1610612736\n402653184 1342177280\n-67108864 2013265920\n536870912 1593835520\n0 1879048192\n570425344 1543503872\n0 2113929216\n536870..."
},
{
"input": "30\n29 18\n18 8\n8 27\n27 26\n26 17\n17 11\n11 23\n23 16\n16 6\n23 19\n23 22\n23 2\n23 28\n23 1\n11 14\n11 13\n11 5\n11 9\n11 30\n17 15\n26 3\n26 7\n26 25\n27 24\n27 4\n8 21\n18 20\n18 12\n29 10",
"output": "NO"
},
{
"input": "30\n10 15\n15 17\n17 14\n14 7\n7 3\n3 27\n3 25\n3 21\n3 5\n3 9\n7 11\n7 18\n7 26\n7 16\n7 4\n7 8\n7 23\n7 2\n7 29\n17 12\n17 30\n17 13\n17 24\n17 20\n17 28\n17 22\n17 1\n15 6\n10 19",
"output": "NO"
},
{
"input": "30\n8 23\n23 13\n13 29\n29 14\n13 18\n13 5\n13 24\n13 21\n13 4\n13 1\n13 9\n13 16\n13 19\n23 12\n23 17\n23 11\n23 27\n23 22\n23 28\n23 20\n8 3\n8 10\n8 26\n8 15\n8 25\n8 6\n8 30\n8 7\n8 2",
"output": "NO"
},
{
"input": "17\n2 13\n13 7\n7 6\n6 12\n6 9\n6 14\n6 1\n6 4\n7 8\n7 11\n13 17\n13 10\n2 3\n2 5\n2 16\n2 15",
"output": "NO"
},
{
"input": "20\n17 18\n18 13\n13 6\n6 3\n6 2\n6 14\n13 20\n13 15\n18 11\n18 7\n18 19\n18 9\n17 5\n17 4\n17 12\n17 10\n17 16\n17 1\n17 8",
"output": "NO"
},
{
"input": "6\n2 1\n1 4\n4 6\n4 3\n1 5",
"output": "YES\n0 0\n0 1073741824\n1610612736 0\n1073741824 0\n0 -1073741824\n1073741824 536870912"
},
{
"input": "10\n9 1\n9 10\n9 5\n9 8\n9 2\n9 7\n9 3\n9 6\n9 4",
"output": "NO"
},
{
"input": "15\n5 2\n2 7\n7 3\n3 9\n9 15\n9 12\n3 10\n3 11\n7 1\n7 6\n2 13\n2 8\n5 14\n5 4",
"output": "YES\n0 0\n0 1610612736\n536870912 1073741824\n134217728 1879048192\n0 1879048192\n-536870912 1073741824\n0 1073741824\n-268435456 1610612736\n536870912 1342177280\n805306368 1073741824\n536870912 805306368\n671088640 1342177280\n268435456 1610612736\n0 2013265920\n536870912 1476395008"
},
{
"input": "30\n7 20\n20 25\n25 4\n4 17\n17 28\n4 23\n4 3\n4 10\n25 18\n25 13\n25 9\n25 14\n25 29\n25 27\n25 21\n25 6\n20 5\n20 15\n20 16\n20 24\n20 2\n7 26\n7 12\n7 8\n7 1\n7 30\n7 19\n7 11\n7 22",
"output": "NO"
},
{
"input": "30\n6 29\n29 27\n27 4\n4 2\n2 10\n10 19\n10 8\n10 25\n2 16\n2 15\n2 28\n4 1\n4 30\n27 18\n27 12\n27 20\n27 7\n27 3\n29 26\n29 23\n29 17\n29 22\n29 14\n29 24\n6 5\n6 9\n6 13\n6 21\n6 11",
"output": "NO"
},
{
"input": "19\n13 3\n3 10\n10 19\n19 14\n19 16\n19 17\n19 2\n19 11\n10 7\n10 6\n10 18\n10 12\n10 15\n10 8\n3 9\n3 5\n13 1\n13 4",
"output": "NO"
},
{
"input": "18\n12 16\n16 5\n5 10\n10 7\n10 11\n5 18\n5 8\n5 13\n5 6\n5 3\n5 1\n16 2\n16 9\n12 17\n12 15\n12 4\n12 14",
"output": "NO"
},
{
"input": "15\n5 13\n13 15\n15 10\n10 9\n10 6\n10 8\n15 7\n13 4\n13 11\n13 2\n5 14\n5 1\n5 3\n5 12",
"output": "NO"
},
{
"input": "4\n3 2\n3 4\n3 1",
"output": "YES\n0 0\n0 1610612736\n0 1073741824\n536870912 1073741824"
},
{
"input": "30\n18 9\n9 26\n26 25\n25 14\n14 20\n20 27\n27 30\n30 16\n16 1\n1 29\n1 21\n16 19\n16 23\n30 8\n30 4\n27 12\n27 10\n20 7\n20 6\n14 15\n14 11\n25 13\n25 28\n26 24\n26 5\n9 3\n9 2\n18 17\n18 22",
"output": "YES\n0 0\n-4194304 2139095040\n4194304 2139095040\n-268435456 1610612736\n-8388608 2130706432\n-67108864 2013265920\n67108864 2013265920\n268435456 1610612736\n0 2139095040\n-134217728 1879048192\n-33554432 2080374784\n134217728 1879048192\n16777216 2113929216\n0 2080374784\n33554432 2080374784\n0 1073741824\n0 2145386496\n0 2143289344\n536870912 1073741824\n0 2013265920\n0 -1073741824\n2097152 2143289344\n-536870912 1073741824\n8388608 2130706432\n0 2113929216\n0 2130706432\n0 1879048192\n-16777216 211392..."
},
{
"input": "30\n15 22\n22 18\n18 25\n25 27\n27 10\n10 7\n7 3\n3 28\n28 29\n29 5\n5 4\n4 24\n24 2\n2 26\n26 6\n2 14\n24 16\n4 9\n5 11\n29 8\n28 12\n3 17\n7 19\n10 30\n27 20\n25 1\n18 13\n22 23\n15 21",
"output": "YES\n0 0\n536870912 1609564160\n536870912 1543503872\n536870912 1606418432\n536870912 1602224128\n536870912 1610350592\n536870912 1476395008\n545259520 1593835520\n538968064 1606418432\n536870912 1342177280\n541065216 1602224128\n553648128 1577058304\n268435456 1610612736\n537395200 1609564160\n0 2013265920\n537919488 1608515584\n570425344 1543503872\n0 1610612736\n603979776 1476395008\n805306368 1073741824\n0 2080374784\n0 1879048192\n134217728 1879048192\n536870912 1608515584\n0 1073741824\n536870912 161..."
}
] | 155 | 409,600 | 0 | 6,664 |
|
79 | Bus Game | [
"greedy"
] | A. Bus Game | 2 | 256 | After Fox Ciel won an onsite round of a programming contest, she took a bus to return to her castle. The fee of the bus was 220 yen. She met Rabbit Hanako in the bus. They decided to play the following game because they got bored in the bus.
- Initially, there is a pile that contains *x* 100-yen coins and *y* 10-yen coins. - They take turns alternatively. Ciel takes the first turn. - In each turn, they must take exactly 220 yen from the pile. In Ciel's turn, if there are multiple ways to take 220 yen, she will choose the way that contains the maximal number of 100-yen coins. In Hanako's turn, if there are multiple ways to take 220 yen, she will choose the way that contains the maximal number of 10-yen coins. - If Ciel or Hanako can't take exactly 220 yen from the pile, she loses.
Determine the winner of the game. | The first line contains two integers *x* (0<=β€<=*x*<=β€<=106) and *y* (0<=β€<=*y*<=β€<=106), separated by a single space. | If Ciel wins, print "Ciel". Otherwise, print "Hanako". | [
"2 2\n",
"3 22\n"
] | [
"Ciel\n",
"Hanako\n"
] | In the first turn (Ciel's turn), she will choose 2 100-yen coins and 2 10-yen coins. In the second turn (Hanako's turn), she will choose 1 100-yen coin and 12 10-yen coins. In the third turn (Ciel's turn), she can't pay exactly 220 yen, so Ciel will lose. | [
{
"input": "2 2",
"output": "Ciel"
},
{
"input": "3 22",
"output": "Hanako"
},
{
"input": "0 22",
"output": "Ciel"
},
{
"input": "1000 1000",
"output": "Ciel"
},
{
"input": "0 0",
"output": "Hanako"
},
{
"input": "0 21",
"output": "Hanako"
},
{
"input": "1 11",
"output": "Hanako"
},
{
"input": "1 12",
"output": "Ciel"
},
{
"input": "2 1",
"output": "Hanako"
},
{
"input": "2 23",
"output": "Ciel"
},
{
"input": "2 24",
"output": "Hanako"
},
{
"input": "3 1",
"output": "Hanako"
},
{
"input": "3 2",
"output": "Ciel"
},
{
"input": "3 13",
"output": "Ciel"
},
{
"input": "3 14",
"output": "Hanako"
},
{
"input": "4 1",
"output": "Hanako"
},
{
"input": "4 2",
"output": "Ciel"
},
{
"input": "4 25",
"output": "Hanako"
},
{
"input": "4 26",
"output": "Ciel"
},
{
"input": "5 1",
"output": "Hanako"
},
{
"input": "5 2",
"output": "Ciel"
},
{
"input": "5 15",
"output": "Hanako"
},
{
"input": "5 16",
"output": "Ciel"
},
{
"input": "5 23",
"output": "Ciel"
},
{
"input": "5 24",
"output": "Hanako"
},
{
"input": "6 1",
"output": "Hanako"
},
{
"input": "6 2",
"output": "Ciel"
},
{
"input": "6 13",
"output": "Ciel"
},
{
"input": "6 14",
"output": "Hanako"
},
{
"input": "6 23",
"output": "Ciel"
},
{
"input": "6 24",
"output": "Hanako"
},
{
"input": "7 1",
"output": "Hanako"
},
{
"input": "7 2",
"output": "Ciel"
},
{
"input": "7 13",
"output": "Ciel"
},
{
"input": "7 14",
"output": "Hanako"
},
{
"input": "7 25",
"output": "Hanako"
},
{
"input": "7 26",
"output": "Ciel"
},
{
"input": "8 1",
"output": "Hanako"
},
{
"input": "8 2",
"output": "Ciel"
},
{
"input": "8 15",
"output": "Hanako"
},
{
"input": "8 16",
"output": "Ciel"
},
{
"input": "8 25",
"output": "Hanako"
},
{
"input": "8 26",
"output": "Ciel"
},
{
"input": "9 1",
"output": "Hanako"
},
{
"input": "9 2",
"output": "Ciel"
},
{
"input": "9 15",
"output": "Hanako"
},
{
"input": "9 16",
"output": "Ciel"
},
{
"input": "9 23",
"output": "Ciel"
},
{
"input": "9 24",
"output": "Hanako"
},
{
"input": "10 12",
"output": "Ciel"
},
{
"input": "10 13",
"output": "Ciel"
},
{
"input": "10 22",
"output": "Ciel"
},
{
"input": "10 23",
"output": "Ciel"
},
{
"input": "11 12",
"output": "Ciel"
},
{
"input": "11 13",
"output": "Ciel"
},
{
"input": "11 24",
"output": "Hanako"
},
{
"input": "11 25",
"output": "Hanako"
},
{
"input": "12 14",
"output": "Hanako"
},
{
"input": "12 15",
"output": "Hanako"
},
{
"input": "12 24",
"output": "Hanako"
},
{
"input": "12 25",
"output": "Hanako"
},
{
"input": "0 1000000",
"output": "Hanako"
},
{
"input": "1000000 0",
"output": "Hanako"
},
{
"input": "1000000 1000000",
"output": "Ciel"
},
{
"input": "178087 42116",
"output": "Ciel"
},
{
"input": "378897 104123",
"output": "Ciel"
},
{
"input": "61207 166129",
"output": "Hanako"
},
{
"input": "743519 228136",
"output": "Ciel"
},
{
"input": "425829 771644",
"output": "Ciel"
},
{
"input": "626640 833651",
"output": "Ciel"
},
{
"input": "308950 895657",
"output": "Hanako"
},
{
"input": "991262 957664",
"output": "Ciel"
},
{
"input": "192071 19670",
"output": "Hanako"
},
{
"input": "874382 81677",
"output": "Hanako"
},
{
"input": "202081 745873",
"output": "Hanako"
},
{
"input": "233663 723781",
"output": "Hanako"
},
{
"input": "783744 701689",
"output": "Hanako"
},
{
"input": "333825 679597",
"output": "Hanako"
},
{
"input": "365407 657504",
"output": "Hanako"
},
{
"input": "915488 635412",
"output": "Hanako"
},
{
"input": "947070 613320",
"output": "Hanako"
},
{
"input": "497151 591228",
"output": "Hanako"
},
{
"input": "528732 87635",
"output": "Ciel"
},
{
"input": "78813 65543",
"output": "Hanako"
},
{
"input": "6 4",
"output": "Hanako"
},
{
"input": "3 5",
"output": "Ciel"
},
{
"input": "4 24",
"output": "Hanako"
},
{
"input": "2 14",
"output": "Ciel"
},
{
"input": "2 26",
"output": "Hanako"
},
{
"input": "5 26",
"output": "Ciel"
},
{
"input": "5 5",
"output": "Hanako"
},
{
"input": "359 479",
"output": "Hanako"
},
{
"input": "1 20",
"output": "Ciel"
},
{
"input": "11 0",
"output": "Hanako"
},
{
"input": "3 0",
"output": "Hanako"
},
{
"input": "0 30",
"output": "Ciel"
},
{
"input": "0 35",
"output": "Ciel"
},
{
"input": "100 99",
"output": "Ciel"
},
{
"input": "3 4",
"output": "Ciel"
}
] | 280 | 2,867,200 | 3.924659 | 6,668 |
981 | Businessmen Problems | [
"sortings"
] | null | null | Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered $n$ distinct chemical elements with indices $a_1, a_2, \ldots, a_n$, and will get an income of $x_i$ Berland rubles if the $i$-th element from this list is in the set of this company.
The TopChemist company discovered $m$ distinct chemical elements with indices $b_1, b_2, \ldots, b_m$, and it will get an income of $y_j$ Berland rubles for including the $j$-th element from this list to its set.
In other words, the first company can present any subset of elements from $\{a_1, a_2, \ldots, a_n\}$ (possibly empty subset), the second company can present any subset of elements from $\{b_1, b_2, \ldots, b_m\}$ (possibly empty subset). There shouldn't be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible. | The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) Β β the number of elements discovered by ChemForces.
The $i$-th of the next $n$ lines contains two integers $a_i$ and $x_i$ ($1 \leq a_i \leq 10^9$, $1 \leq x_i \leq 10^9$) Β β the index of the $i$-th element and the income of its usage on the exhibition. It is guaranteed that all $a_i$ are distinct.
The next line contains a single integer $m$ ($1 \leq m \leq 10^5$) Β β the number of chemicals invented by TopChemist.
The $j$-th of the next $m$ lines contains two integers $b_j$ and $y_j$, ($1 \leq b_j \leq 10^9$, $1 \leq y_j \leq 10^9$) Β β the index of the $j$-th element and the income of its usage on the exhibition. It is guaranteed that all $b_j$ are distinct. | Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. | [
"3\n1 2\n7 2\n3 10\n4\n1 4\n2 4\n3 4\n4 4\n",
"1\n1000000000 239\n3\n14 15\n92 65\n35 89\n"
] | [
"24\n",
"408\n"
] | In the first example ChemForces can choose the set ($3, 7$), while TopChemist can choose ($1, 2, 4$). This way the total income is $(10 + 2) + (4 + 4 + 4) = 24$.
In the second example ChemForces can choose the only element $10^9$, while TopChemist can choose ($14, 92, 35$). This way the total income is $(239) + (15 + 65 + 89) = 408$. | [
{
"input": "3\n1 2\n7 2\n3 10\n4\n1 4\n2 4\n3 4\n4 4",
"output": "24"
},
{
"input": "1\n1000000000 239\n3\n14 15\n92 65\n35 89",
"output": "408"
},
{
"input": "10\n598654597 488228616\n544064902 21923894\n329635457 980089248\n988262691 654502493\n967529230 543358150\n835120075 128123793\n809901567 613170206\n152157661 479980560\n859252956 318029856\n614959273 735298093\n10\n849172317 361325206\n341655282 740807372\n453949939 809030434\n813199219 765838311\n725032511 427644854\n631233366 115983627\n825067417 721190169\n463026366 953335259\n881501578 987288937\n500099903 427659916",
"output": "11272808994"
},
{
"input": "10\n598654597 488228616\n544064902 21923894\n329635457 980089248\n988262691 654502493\n967529230 543358150\n835120075 128123793\n809901567 613170206\n152157661 479980560\n859252956 318029856\n614959273 735298093\n10\n544064902 740807372\n859252956 765838311\n835120075 115983627\n825067417 463026366\n809901567 500099903\n614959273 322932832\n33955432 657532637\n816892960 927933923\n916770606 754371712\n967529230 566149252",
"output": "8955052582"
},
{
"input": "1\n259021863 682413763\n1\n554146726 693647287",
"output": "1376061050"
},
{
"input": "1\n259021863 682413763\n1\n554146726 425883728",
"output": "1108297491"
},
{
"input": "1\n1000000000 1\n1\n1000000000 1000000000",
"output": "1000000000"
},
{
"input": "2\n1 4\n2 4\n2\n1 4\n2 4",
"output": "8"
},
{
"input": "1\n14 15\n3\n35 89\n92 65\n1000000000 239",
"output": "408"
}
] | 1,263 | 22,016,000 | 3 | 6,675 |
|
931 | Laboratory Work | [
"implementation",
"math"
] | null | null | Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some value *n* times, and then compute the average value to lower the error.
Kirill has already made his measurements, and has got the following integer values: *x*1, *x*2, ..., *x**n*. It is important that the values are close to each other, namely, the difference between the maximum value and the minimum value is at most 2.
Anya does not want to make the measurements, however, she can't just copy the values from Kirill's work, because the error of each measurement is a random value, and this coincidence will be noted by the teacher. Anya wants to write such integer values *y*1, *y*2, ..., *y**n* in her work, that the following conditions are met:
- the average value of *x*1,<=*x*2,<=...,<=*x**n* is equal to the average value of *y*1,<=*y*2,<=...,<=*y**n*;- all Anya's measurements are in the same bounds as all Kirill's measurements, that is, the maximum value among Anya's values is not greater than the maximum value among Kirill's values, and the minimum value among Anya's values is not less than the minimum value among Kirill's values;- the number of equal measurements in Anya's work and Kirill's work is as small as possible among options with the previous conditions met. Formally, the teacher goes through all Anya's values one by one, if there is equal value in Kirill's work and it is not strike off yet, he strikes off this Anya's value and one of equal values in Kirill's work. The number of equal measurements is then the total number of strike off values in Anya's work.
Help Anya to write such a set of measurements that the conditions above are met. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000) β the numeber of measurements made by Kirill.
The second line contains a sequence of integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=100<=000<=β€<=*x**i*<=β€<=100<=000) β the measurements made by Kirill. It is guaranteed that the difference between the maximum and minimum values among values *x*1,<=*x*2,<=...,<=*x**n* does not exceed 2. | In the first line print the minimum possible number of equal measurements.
In the second line print *n* integers *y*1,<=*y*2,<=...,<=*y**n* β the values Anya should write. You can print the integers in arbitrary order. Keep in mind that the minimum value among Anya's values should be not less that the minimum among Kirill's values, and the maximum among Anya's values should be not greater than the maximum among Kirill's values.
If there are multiple answers, print any of them. | [
"6\n-1 1 1 0 0 -1\n",
"3\n100 100 101\n",
"7\n-10 -9 -10 -8 -10 -9 -9\n"
] | [
"2\n0 0 0 0 0 0 \n",
"3\n101 100 100 \n",
"5\n-10 -10 -9 -9 -9 -9 -9 \n"
] | In the first example Anya can write zeros as here measurements results. The average value is then equal to the average value of Kirill's values, and there are only two equal measurements.
In the second example Anya should write two values 100 and one value 101 (in any order), because it is the only possibility to make the average be the equal to the average of Kirill's values. Thus, all three measurements are equal.
In the third example the number of equal measurements is 5. | [
{
"input": "6\n-1 1 1 0 0 -1",
"output": "2\n0 0 0 0 0 0 "
},
{
"input": "3\n100 100 101",
"output": "3\n101 100 100 "
},
{
"input": "7\n-10 -9 -10 -8 -10 -9 -9",
"output": "5\n-10 -10 -9 -9 -9 -9 -9 "
},
{
"input": "60\n-8536 -8536 -8536 -8535 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8535 -8536 -8535 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8535 -8536 -8536 -8535 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8535 -8536 -8536 -8536 -8535 -8535 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8535",
"output": "60\n-8535 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8535 -8535 -8536 -8536 -8536 -8535 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8535 -8536 -8536 -8535 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8535 -8536 -8535 -8536 -8536 -8536 -8536 -8536 -8536 -8536 -8535 -8536 -8536 -8536 "
},
{
"input": "9\n-71360 -71359 -71360 -71360 -71359 -71359 -71359 -71359 -71359",
"output": "9\n-71359 -71359 -71359 -71359 -71359 -71360 -71360 -71359 -71360 "
},
{
"input": "10\n100 100 100 100 100 100 100 100 100 100",
"output": "10\n100 100 100 100 100 100 100 100 100 100 "
},
{
"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": "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 "
},
{
"input": "5\n-399 -399 -400 -399 -400",
"output": "5\n-400 -399 -400 -399 -399 "
},
{
"input": "10\n1001 1000 1000 1001 1000 1000 1001 1001 1000 1001",
"output": "10\n1001 1000 1001 1001 1000 1000 1001 1000 1000 1001 "
},
{
"input": "20\n-100000 -99999 -100000 -99999 -99999 -100000 -99999 -100000 -99999 -100000 -99999 -99999 -99999 -100000 -100000 -99999 -100000 -100000 -100000 -99999",
"output": "20\n-99999 -100000 -100000 -100000 -99999 -100000 -100000 -99999 -99999 -99999 -100000 -99999 -100000 -99999 -100000 -99999 -99999 -100000 -99999 -100000 "
},
{
"input": "50\n99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 100000 99999 99999 99999 99999 99999 100000 99999 99999 99999 100000 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 100000 99999 99999 99999 100000 99999 99999 99999",
"output": "50\n99999 99999 99999 100000 99999 99999 99999 100000 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 100000 99999 99999 99999 100000 99999 99999 99999 99999 99999 100000 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 "
},
{
"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 2 1 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": "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 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "
},
{
"input": "1\n-100000",
"output": "1\n-100000 "
},
{
"input": "1\n-1",
"output": "1\n-1 "
},
{
"input": "1\n0",
"output": "1\n0 "
},
{
"input": "1\n1",
"output": "1\n1 "
},
{
"input": "1\n100000",
"output": "1\n100000 "
},
{
"input": "5\n2 2 1 1 2",
"output": "5\n2 1 1 2 2 "
},
{
"input": "10\n0 -1 0 1 1 1 1 -1 0 0",
"output": "6\n0 0 0 0 0 0 0 0 1 1 "
},
{
"input": "20\n-4344 -4342 -4344 -4342 -4343 -4343 -4344 -4344 -4342 -4343 -4344 -4343 -4344 -4344 -4344 -4342 -4344 -4343 -4342 -4344",
"output": "10\n-4344 -4344 -4344 -4344 -4344 -4343 -4343 -4343 -4343 -4343 -4343 -4343 -4343 -4343 -4343 -4343 -4343 -4343 -4343 -4343 "
},
{
"input": "40\n113 113 112 112 112 112 112 112 112 112 112 113 113 112 113 112 113 112 112 112 111 112 112 113 112 112 112 112 112 112 112 112 113 112 113 112 112 113 112 113",
"output": "12\n111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 "
},
{
"input": "5\n-94523 -94523 -94523 -94524 -94524",
"output": "5\n-94524 -94524 -94523 -94523 -94523 "
},
{
"input": "10\n-35822 -35823 -35823 -35823 -35821 -35823 -35823 -35821 -35822 -35821",
"output": "4\n-35823 -35823 -35822 -35822 -35822 -35822 -35822 -35822 -35822 -35822 "
},
{
"input": "11\n-50353 -50353 -50353 -50353 -50353 -50352 -50353 -50353 -50353 -50353 -50352",
"output": "11\n-50352 -50353 -50353 -50353 -50353 -50352 -50353 -50353 -50353 -50353 -50353 "
},
{
"input": "20\n46795 46795 46795 46795 46795 46795 46795 46793 46794 46795 46794 46795 46795 46795 46795 46795 46795 46795 46795 46795",
"output": "18\n46794 46794 46794 46794 46795 46795 46795 46795 46795 46795 46795 46795 46795 46795 46795 46795 46795 46795 46795 46795 "
},
{
"input": "40\n72263 72261 72262 72263 72263 72263 72263 72263 72263 72262 72263 72263 72263 72263 72263 72262 72263 72262 72263 72262 72262 72263 72263 72262 72263 72263 72262 72262 72263 72262 72263 72263 72263 72263 72263 72263 72263 72263 72263 72262",
"output": "30\n72261 72261 72261 72261 72261 72261 72262 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 72263 "
},
{
"input": "50\n-46992 -46992 -46992 -46991 -46992 -46991 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46991 -46991 -46991 -46992 -46990 -46991 -46991 -46991 -46991 -46992 -46992 -46991 -46992 -46992 -46992 -46990 -46992 -46991 -46991 -46992 -46992 -46992 -46991 -46991 -46991 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992",
"output": "36\n-46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46992 -46991 -46990 -46990 -46990 -46990 -46990 -46990 -46990 -46990 -46990 "
},
{
"input": "60\n-86077 -86075 -86076 -86076 -86077 -86077 -86075 -86075 -86075 -86077 -86075 -86076 -86075 -86075 -86075 -86076 -86075 -86076 -86075 -86075 -86076 -86076 -86076 -86075 -86075 -86075 -86075 -86077 -86075 -86076 -86075 -86075 -86075 -86076 -86075 -86076 -86077 -86075 -86075 -86075 -86076 -86075 -86076 -86075 -86076 -86076 -86075 -86076 -86076 -86075 -86075 -86075 -86077 -86076 -86075 -86075 -86075 -86075 -86075 -86075",
"output": "42\n-86077 -86077 -86077 -86077 -86077 -86077 -86077 -86077 -86077 -86077 -86077 -86077 -86077 -86077 -86077 -86077 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 -86075 "
},
{
"input": "70\n-87 -86 -88 -86 -87 -86 -88 -88 -87 -86 -86 -88 -86 -86 -88 -87 -87 -87 -86 -87 -87 -87 -88 -88 -88 -87 -88 -87 -88 -87 -88 -86 -86 -86 -88 -86 -87 -87 -86 -86 -88 -86 -88 -87 -88 -87 -87 -86 -88 -87 -86 -88 -87 -86 -87 -87 -86 -88 -87 -86 -87 -88 -87 -88 -86 -87 -88 -88 -87 -87",
"output": "28\n-88 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 -87 "
},
{
"input": "2\n0 2",
"output": "0\n1 1 "
},
{
"input": "4\n1 1 3 3",
"output": "0\n2 2 2 2 "
},
{
"input": "6\n1 1 1 3 3 3",
"output": "0\n2 2 2 2 2 2 "
},
{
"input": "2\n1 3",
"output": "0\n2 2 "
},
{
"input": "7\n0 1 1 1 1 1 2",
"output": "3\n0 0 0 1 2 2 2 "
},
{
"input": "6\n1 1 1 -1 -1 -1",
"output": "0\n0 0 0 0 0 0 "
},
{
"input": "3\n1 1 3",
"output": "1\n1 2 2 "
},
{
"input": "2\n2 0",
"output": "0\n1 1 "
},
{
"input": "10\n1 3 3 3 3 3 3 3 3 3",
"output": "8\n2 2 3 3 3 3 3 3 3 3 "
},
{
"input": "7\n1 3 3 3 3 3 3",
"output": "5\n2 2 3 3 3 3 3 "
},
{
"input": "7\n1 2 2 2 2 2 3",
"output": "3\n1 1 1 2 3 3 3 "
},
{
"input": "5\n-8 -8 -8 -10 -10",
"output": "1\n-9 -9 -9 -9 -8 "
},
{
"input": "3\n1 2 3",
"output": "1\n2 2 2 "
},
{
"input": "4\n2 2 4 4",
"output": "0\n3 3 3 3 "
},
{
"input": "4\n1 1 -1 -1",
"output": "0\n0 0 0 0 "
}
] | 295 | 12,697,600 | 0 | 6,681 |
|
83 | Magical Array | [
"math"
] | A. Magical Array | 2 | 256 | Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements. | The first line of the input data contains an integer *n* (1<=β€<=*n*<=β€<=105). The second line contains an array of original integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109). | Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). | [
"4\n2 1 1 4\n",
"5\n-2 -2 -2 0 1\n"
] | [
"5\n",
"8\n"
] | Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3]. | [
{
"input": "4\n2 1 1 4",
"output": "5"
},
{
"input": "5\n-2 -2 -2 0 1",
"output": "8"
},
{
"input": "1\n10",
"output": "1"
},
{
"input": "2\n5 6",
"output": "2"
},
{
"input": "5\n5 5 4 5 5",
"output": "7"
},
{
"input": "8\n1 2 0 0 0 0 3 3",
"output": "15"
},
{
"input": "12\n-4 3 3 2 3 3 3 -4 2 -4 -4 -4",
"output": "19"
},
{
"input": "10\n7 1 0 10 0 -5 -3 -2 0 0",
"output": "11"
},
{
"input": "20\n6 0 0 -3 1 -3 0 -8 1 3 5 2 -1 -5 -1 9 0 6 -2 4",
"output": "21"
},
{
"input": "100\n0 -18 -9 -15 3 16 -28 0 -28 0 28 -20 -9 9 -11 0 18 -15 -18 -26 0 -27 -25 -22 6 -5 8 14 -17 24 20 3 -6 24 -27 1 -23 0 4 12 -20 0 -10 30 22 -6 13 16 0 15 17 -8 -2 0 -5 13 11 23 -17 -29 10 15 -28 0 -23 4 20 17 -7 -5 -16 -17 16 2 20 19 -8 0 8 -5 12 0 0 -14 -15 -28 -10 20 0 8 -1 10 14 9 0 4 -16 15 13 -10",
"output": "101"
},
{
"input": "50\n2 0 2 0 0 0 0 -1 -2 -2 -2 1 1 2 2 0 2 0 2 -3 0 0 0 0 3 1 -2 0 -1 0 -2 3 -1 2 0 2 0 0 0 0 2 0 1 0 0 3 0 0 -2 0",
"output": "75"
},
{
"input": "2\n-510468670 0",
"output": "2"
},
{
"input": "150\n0 -2 1 -2 0 0 0 0 -2 0 -2 -1 0 0 2 0 1 -2 1 -1 0 0 0 2 -2 2 -1 0 0 0 -2 0 2 0 1 0 -2 0 -2 -1 -1 -2 -2 2 0 0 1 -2 -2 -1 -2 0 2 1 1 -1 1 0 -2 2 0 0 0 1 -1 0 -2 -1 0 -2 2 1 1 0 0 2 0 0 2 -1 0 0 2 0 2 0 -2 -1 1 -2 1 0 0 -2 -1 -1 0 0 2 -1 -1 -1 -1 -2 0 0 2 -1 -1 1 0 0 1 -1 0 0 -1 2 2 0 0 0 -1 -2 0 1 0 -1 0 -1 -1 0 2 0 1 2 0 0 2 0 0 1 2 0 2 -2 2 0 2 2",
"output": "196"
}
] | 92 | 0 | 0 | 6,683 |
0 | none | [
"none"
] | null | null | Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible.
Limak has a string *s* that consists of uppercase English letters. In one move he can swap two adjacent letters of the string. For example, he can transform a string "ABBC" into "BABC" or "ABCB" in one move.
Limak wants to obtain a string without a substring "VK" (i.e. there should be no letter 'V' immediately followed by letter 'K'). It can be easily proved that it's possible for any initial string *s*.
What is the minimum possible number of moves Limak can do? | The first line of the input contains an integer *n* (1<=β€<=*n*<=β€<=75)Β β the length of the string.
The second line contains a string *s*, consisting of uppercase English letters. The length of the string is equal to *n*. | Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring "VK". | [
"4\nVKVK\n",
"5\nBVVKV\n",
"7\nVVKEVKK\n",
"20\nVKVKVVVKVOVKVQKKKVVK\n",
"5\nLIMAK\n"
] | [
"3\n",
"2\n",
"3\n",
"8\n",
"0\n"
] | In the first sample, the initial string is "VKVK". The minimum possible number of moves is 3. One optimal sequence of moves is:
1. Swap two last letters. The string becomes "VKKV".1. Swap first two letters. The string becomes "KVKV".1. Swap the second and the third letter. The string becomes "KKVV". Indeed, this string doesn't have a substring "VK".
In the second sample, there are two optimal sequences of moves. One is "BVVKV"βββββ"VBVKV"βββββ"VVBKV". The other is "BVVKV"βββββ"BVKVV"βββββ"BKVVV".
In the fifth sample, no swaps are necessary. | [] | 46 | 0 | 0 | 6,691 |
|
159 | Matchmaker | [
"*special",
"greedy",
"sortings"
] | null | null | Polycarpus has *n* markers and *m* marker caps. Each marker is described by two numbers: *x**i* is the color and *y**i* is the diameter. Correspondingly, each cap is described by two numbers: *a**j* is the color and *b**j* is the diameter. Cap (*a**j*,<=*b**j*) can close marker (*x**i*,<=*y**i*) only if their diameters match, that is, *b**j*<==<=*y**i*. Besides, a marker is considered to be beautifully closed, if the cap color and the marker color match, that is, *a**j*<==<=*x**i*.
Find the way to close the maximum number of markers. If there are several such ways, then choose the one that has the maximum number of beautifully closed markers. | The first input line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of markers and the number of caps, correspondingly.
Next *n* lines describe the markers. The *i*-th line contains two space-separated integers *x**i*, *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=1000) β the *i*-th marker's color and diameter, correspondingly.
Next *m* lines describe the caps. The *j*-th line contains two space-separated integers *a**j*, *b**j* (1<=β€<=*a**j*,<=*b**j*<=β€<=1000) β the color and diameter of the *j*-th cap, correspondingly. | Print two space-separated integers *u*,<=*v*, where *u* is the number of closed markers and *v* is the number of beautifully closed markers in the sought optimal way. Remember that you have to find the way to close the maximum number of markers, and if there are several such ways, you should choose the one where the number of beautifully closed markers is maximum. | [
"3 4\n1 2\n3 4\n2 4\n5 4\n2 4\n1 1\n1 2\n",
"2 2\n1 2\n2 1\n3 4\n5 1\n"
] | [
"3 2\n",
"1 0\n"
] | In the first test sample the first marker should be closed by the fourth cap, the second marker should be closed by the first cap and the third marker should be closed by the second cap. Thus, three markers will be closed, and two of them will be beautifully closed β the first and the third markers. | [
{
"input": "3 4\n1 2\n3 4\n2 4\n5 4\n2 4\n1 1\n1 2",
"output": "3 2"
},
{
"input": "2 2\n1 2\n2 1\n3 4\n5 1",
"output": "1 0"
},
{
"input": "6 7\n2 1\n2 2\n2 1\n1 1\n2 1\n1 2\n2 2\n2 2\n2 2\n1 2\n2 2\n1 1\n1 2",
"output": "3 3"
},
{
"input": "6 7\n2 1\n1 1\n2 2\n1 2\n1 1\n1 2\n2 1\n1 1\n1 1\n1 1\n1 2\n2 2\n1 1",
"output": "5 5"
},
{
"input": "6 7\n2 1\n2 2\n1 1\n1 2\n2 2\n1 2\n1 2\n1 1\n1 2\n2 2\n2 2\n2 2\n1 1",
"output": "6 5"
},
{
"input": "6 7\n1 1\n1 1\n1 2\n1 2\n2 2\n1 2\n2 1\n2 1\n2 1\n2 1\n1 1\n1 1\n1 2",
"output": "3 3"
},
{
"input": "6 2\n1 1\n2 1\n1 1\n1 1\n1 2\n1 1\n2 2\n1 2",
"output": "1 1"
},
{
"input": "3 3\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1",
"output": "3 3"
},
{
"input": "1 1\n1 1\n1 1",
"output": "1 1"
},
{
"input": "1 1\n1 1\n2 2",
"output": "0 0"
},
{
"input": "1 1\n1 1\n1 2",
"output": "0 0"
},
{
"input": "1 1\n1 2\n2 2",
"output": "1 0"
},
{
"input": "1 5\n1 1\n2 2\n1 1\n2 1\n1 2\n1 1",
"output": "1 1"
}
] | 3,000 | 10,444,800 | 0 | 6,696 |
|
699 | One Bomb | [
"implementation"
] | null | null | You are given a description of a depot. It is a rectangular checkered field of *n*<=Γ<=*m* size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*").
You have one bomb. If you lay the bomb at the cell (*x*,<=*y*), then after triggering it will wipe out all walls in the row *x* and all walls in the column *y*.
You are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall. | The first line contains two positive integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000)Β β the number of rows and columns in the depot field.
The next *n* lines contain *m* symbols "." and "*" eachΒ β the description of the field. *j*-th symbol in *i*-th of them stands for cell (*i*,<=*j*). If the symbol is equal to ".", then the corresponding cell is empty, otherwise it equals "*" and the corresponding cell is occupied by a wall. | If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print "NO" in the first line (without quotes).
Otherwise print "YES" (without quotes) in the first line and two integers in the second lineΒ β the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them. | [
"3 4\n.*..\n....\n.*..\n",
"3 3\n..*\n.*.\n*..\n",
"6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*..\n"
] | [
"YES\n1 2\n",
"NO\n",
"YES\n3 3\n"
] | none | [
{
"input": "3 4\n.*..\n....\n.*..",
"output": "YES\n1 2"
},
{
"input": "3 3\n..*\n.*.\n*..",
"output": "NO"
},
{
"input": "6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*..",
"output": "YES\n3 3"
},
{
"input": "1 10\n**********",
"output": "YES\n1 1"
},
{
"input": "10 1\n*\n*\n*\n*\n*\n*\n*\n*\n*\n*",
"output": "YES\n1 1"
},
{
"input": "10 10\n.........*\n.........*\n........**\n.........*\n.........*\n.........*\n.........*\n.........*\n.........*\n.........*",
"output": "YES\n3 10"
},
{
"input": "10 10\n.........*\n.........*\n.........*\n.........*\n.........*\n.........*\n.........*\n.........*\n.........*\n.........*",
"output": "YES\n1 10"
},
{
"input": "2 2\n.*\n*.",
"output": "YES\n2 2"
},
{
"input": "4 4\n....\n...*\n....\n*..*",
"output": "YES\n4 4"
},
{
"input": "4 4\n*...\n*...\n....\n****",
"output": "YES\n4 1"
},
{
"input": "1 1\n*",
"output": "YES\n1 1"
},
{
"input": "1 1\n.",
"output": "YES\n1 1"
},
{
"input": "1 2\n.*",
"output": "YES\n1 2"
},
{
"input": "2 1\n.\n*",
"output": "YES\n1 1"
},
{
"input": "2 2\n**\n**",
"output": "NO"
},
{
"input": "3 1\n*\n*\n*",
"output": "YES\n1 1"
},
{
"input": "3 2\n*.\n.*\n.*",
"output": "YES\n1 2"
},
{
"input": "3 3\n***\n***\n***",
"output": "NO"
},
{
"input": "2 2\n..\n.*",
"output": "YES\n1 2"
},
{
"input": "6 5\n..*..\n..*..\n**.**\n..*..\n..*..\n..*..",
"output": "YES\n3 3"
},
{
"input": "3 3\n.*.\n*.*\n.*.",
"output": "YES\n2 2"
},
{
"input": "4 4\n*...\n....\n....\n...*",
"output": "YES\n4 1"
},
{
"input": "2 4\n...*\n...*",
"output": "YES\n1 4"
},
{
"input": "2 2\n..\n..",
"output": "YES\n1 1"
},
{
"input": "3 3\n..*\n.*.\n..*",
"output": "YES\n2 3"
},
{
"input": "2 2\n*.\n.*",
"output": "YES\n2 1"
},
{
"input": "3 2\n.*\n*.\n.*",
"output": "YES\n2 2"
},
{
"input": "3 3\n***\n.*.\n.*.",
"output": "YES\n1 2"
},
{
"input": "4 4\n*.*.\n..*.\n.***\n..*.",
"output": "NO"
},
{
"input": "2 3\n..*\n**.",
"output": "YES\n2 3"
},
{
"input": "3 2\n*.\n.*\n*.",
"output": "YES\n2 1"
},
{
"input": "4 4\n..*.\n**.*\n..*.\n..*.",
"output": "YES\n2 3"
},
{
"input": "3 3\n*..\n*..\n***",
"output": "YES\n3 1"
},
{
"input": "3 3\n...\n*.*\n.*.",
"output": "YES\n2 2"
},
{
"input": "3 2\n..\n..\n**",
"output": "YES\n3 1"
},
{
"input": "3 4\n...*\n...*\n...*",
"output": "YES\n1 4"
},
{
"input": "5 5\n..*..\n..*..\n**.**\n..*..\n..*..",
"output": "YES\n3 3"
},
{
"input": "6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*.*",
"output": "NO"
},
{
"input": "3 3\n...\n.*.\n..*",
"output": "YES\n3 2"
},
{
"input": "3 5\n....*\n....*\n....*",
"output": "YES\n1 5"
},
{
"input": "3 3\n...\n...\n.*.",
"output": "YES\n1 2"
},
{
"input": "3 3\n*..\n...\n..*",
"output": "YES\n3 1"
},
{
"input": "2 3\n..*\n..*",
"output": "YES\n1 3"
},
{
"input": "2 2\n**\n.*",
"output": "YES\n1 2"
},
{
"input": "3 3\n..*\n*..\n*..",
"output": "YES\n1 1"
},
{
"input": "5 4\n.*..\n*.**\n.*..\n.*..\n.*..",
"output": "YES\n2 2"
},
{
"input": "6 5\n*.*..\n..*..\n*****\n..*..\n..*..\n..*..",
"output": "NO"
},
{
"input": "4 4\n.*..\n*.**\n....\n.*..",
"output": "YES\n2 2"
},
{
"input": "3 5\n....*\n....*\n*****",
"output": "YES\n3 5"
},
{
"input": "3 3\n..*\n*..\n..*",
"output": "YES\n2 3"
},
{
"input": "6 6\n..*...\n......\n......\n......\n......\n*....*",
"output": "YES\n6 3"
},
{
"input": "4 4\n.*..\n*...\n.*..\n.*..",
"output": "YES\n2 2"
},
{
"input": "3 3\n...\n..*\n.*.",
"output": "YES\n3 3"
},
{
"input": "3 2\n.*\n*.\n*.",
"output": "YES\n1 1"
},
{
"input": "4 2\n**\n.*\n.*\n.*",
"output": "YES\n1 2"
},
{
"input": "5 5\n*...*\n.....\n.....\n.....\n..*..",
"output": "YES\n1 3"
},
{
"input": "3 3\n**.\n...\n..*",
"output": "YES\n1 3"
},
{
"input": "3 3\n*.*\n*..\n*.*",
"output": "NO"
},
{
"input": "5 4\n....\n....\n*..*\n....\n.*..",
"output": "YES\n3 2"
},
{
"input": "5 5\n...*.\n...*.\n...*.\n...*.\n***.*",
"output": "YES\n5 4"
},
{
"input": "5 5\n*****\n*****\n*****\n*****\n*****",
"output": "NO"
},
{
"input": "3 3\n.*.\n..*\n.*.",
"output": "YES\n2 2"
},
{
"input": "3 3\n*.*\n...\n*.*",
"output": "NO"
},
{
"input": "2 3\n.*.\n*.*",
"output": "YES\n2 2"
},
{
"input": "3 10\n.......*..\n........*.\n.........*",
"output": "NO"
},
{
"input": "3 3\n.*.\n.*.\n.**",
"output": "YES\n3 2"
},
{
"input": "4 4\n*...\n....\n....\n..**",
"output": "YES\n4 1"
},
{
"input": "4 4\n****\n****\n****\n****",
"output": "NO"
},
{
"input": "3 2\n.*\n.*\n*.",
"output": "YES\n3 2"
},
{
"input": "3 3\n..*\n..*\n**.",
"output": "YES\n3 3"
},
{
"input": "6 3\n...\n...\n...\n...\n**.\n.*.",
"output": "YES\n5 2"
},
{
"input": "3 4\n****\n..*.\n..*.",
"output": "YES\n1 3"
},
{
"input": "5 5\n*..*.\n.....\n.....\n.....\n...*.",
"output": "YES\n1 4"
},
{
"input": "6 5\n..*..\n..*..\n.*...\n..*..\n..*..\n..*..",
"output": "YES\n3 3"
}
] | 46 | 0 | 0 | 6,701 |
|
519 | A and B and Lecture Rooms | [
"binary search",
"data structures",
"dfs and similar",
"dp",
"trees"
] | null | null | A and B are preparing themselves for programming contests.
The University where A and B study is a set of rooms connected by corridors. Overall, the University has *n* rooms connected by *n*<=-<=1 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to *n*.
Every day Π and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them.
As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following *m* days. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of rooms in the University.
The next *n*<=-<=1 lines describe the corridors. The *i*-th of these lines (1<=β€<=*i*<=β€<=*n*<=-<=1) contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*), showing that the *i*-th corridor connects rooms *a**i* and *b**i*.
The next line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of queries.
Next *m* lines describe the queries. The *j*-th of these lines (1<=β€<=*j*<=β€<=*m*) contains two integers *x**j* and *y**j* (1<=β€<=*x**j*,<=*y**j*<=β€<=*n*) that means that on the *j*-th day A will write the contest in the room *x**j*, B will write in the room *y**j*. | In the *i*-th (1<=β€<=*i*<=β€<=*m*) line print the number of rooms that are equidistant from the rooms where A and B write contest on the *i*-th day. | [
"4\n1 2\n1 3\n2 4\n1\n2 3\n",
"4\n1 2\n2 3\n2 4\n2\n1 2\n1 3\n"
] | [
"1\n",
"0\n2\n"
] | in the first sample there is only one room at the same distance from rooms number 2 and 3 β room number 1. | [
{
"input": "4\n1 2\n1 3\n2 4\n1\n2 3",
"output": "1"
},
{
"input": "4\n1 2\n2 3\n2 4\n2\n1 2\n1 3",
"output": "0\n2"
},
{
"input": "15\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n5 8\n6 9\n9 14\n14 15\n7 10\n4 13\n3 11\n3 12\n6\n10 15\n13 12\n2 15\n8 4\n15 12\n6 13",
"output": "1\n10\n1\n7\n0\n4"
},
{
"input": "5\n1 2\n1 3\n2 5\n2 4\n1\n5 4",
"output": "3"
},
{
"input": "8\n1 2\n2 3\n2 4\n2 5\n5 6\n6 7\n6 8\n6\n3 5\n3 6\n3 8\n5 8\n7 3\n5 7",
"output": "3\n0\n1\n2\n1\n2"
},
{
"input": "8\n1 2\n2 3\n2 4\n2 5\n5 6\n6 7\n6 8\n7\n3 5\n3 6\n3 8\n5 8\n7 3\n5 7\n5 5",
"output": "3\n0\n1\n2\n1\n2\n8"
}
] | 2,000 | 14,336,000 | 0 | 6,735 |
|
272 | Dima and Two Sequences | [
"combinatorics",
"math",
"sortings"
] | null | null | Little Dima has two sequences of points with integer coordinates: sequence (*a*1,<=1),<=(*a*2,<=2),<=...,<=(*a**n*,<=*n*) and sequence (*b*1,<=1),<=(*b*2,<=2),<=...,<=(*b**n*,<=*n*).
Now Dima wants to count the number of distinct sequences of points of length 2Β·*n* that can be assembled from these sequences, such that the *x*-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence.
Dima considers two assembled sequences (*p*1,<=*q*1),<=(*p*2,<=*q*2),<=...,<=(*p*2Β·*n*,<=*q*2Β·*n*) and (*x*1,<=*y*1),<=(*x*2,<=*y*2),<=...,<=(*x*2Β·*n*,<=*y*2Β·*n*) distinct, if there is such *i* (1<=β€<=*i*<=β€<=2Β·*n*), that (*p**i*,<=*q**i*)<=β <=(*x**i*,<=*y**i*).
As the answer can be rather large, print the remainder from dividing the answer by number *m*. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109). The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=109). The numbers in the lines are separated by spaces.
The last line contains integer *m* (2<=β€<=*m*<=β€<=109<=+<=7). | In the single line print the remainder after dividing the answer to the problem by number *m*. | [
"1\n1\n2\n7\n",
"2\n1 2\n2 3\n11\n"
] | [
"1\n",
"2\n"
] | In the first sample you can get only one sequence: (1,β1),β(2,β1).
In the second sample you can get such sequences : (1,β1),β(2,β2),β(2,β1),β(3,β2); (1,β1),β(2,β1),β(2,β2),β(3,β2). Thus, the answer is 2. | [
{
"input": "1\n1\n2\n7",
"output": "1"
},
{
"input": "2\n1 2\n2 3\n11",
"output": "2"
},
{
"input": "100\n1 8 10 6 5 3 2 3 4 2 3 7 1 1 5 1 4 1 8 1 5 5 6 5 3 7 4 5 5 3 8 7 8 6 8 9 10 7 8 5 8 9 1 3 7 2 6 1 7 7 2 8 1 5 4 2 10 4 9 8 1 10 1 5 9 8 1 9 5 1 5 7 1 6 7 8 8 2 2 3 3 7 2 10 6 3 6 3 5 3 10 4 4 6 9 9 3 2 6 6\n4 3 8 4 4 2 4 6 6 3 3 5 8 4 1 6 2 7 6 1 6 10 7 9 2 9 2 9 10 1 1 1 1 7 4 5 3 6 8 6 10 4 3 4 8 6 5 3 1 2 2 4 1 9 1 3 1 9 6 8 9 4 8 8 4 2 1 4 6 2 6 3 4 7 7 7 8 10 7 8 8 6 4 10 10 7 4 5 5 8 3 8 2 8 6 4 5 2 10 2\n29056621",
"output": "5236748"
},
{
"input": "100\n6 1 10 4 8 7 7 3 2 4 6 3 2 5 3 7 1 6 9 8 3 10 1 6 8 1 4 2 5 6 3 5 4 6 3 10 2 8 10 4 2 6 4 5 3 1 8 6 9 8 5 2 7 1 10 5 10 2 9 1 6 4 9 5 2 4 6 7 10 10 10 6 6 9 2 3 3 1 2 4 1 6 9 8 4 10 10 9 9 2 5 7 10 1 9 7 6 6 4 5\n4 9 2 5 5 4 6 9 1 2 6 3 8 9 4 4 4 3 1 3 6 2 9 1 10 6 5 1 9 10 6 2 10 9 8 7 8 2 1 5 8 4 3 2 10 9 5 7 1 8 4 4 4 2 1 3 4 5 3 6 10 3 8 9 5 6 3 9 3 6 5 1 9 1 4 3 8 4 4 8 10 6 4 9 8 4 2 3 1 9 9 1 4 1 8 4 7 9 10 9\n66921358",
"output": "12938646"
},
{
"input": "100\n2 2 10 3 5 6 4 7 9 8 2 7 5 5 1 7 5 9 2 2 10 3 6 10 9 9 10 7 3 9 7 8 8 3 9 3 9 3 3 6 3 7 9 9 7 10 9 1 1 3 6 2 9 5 9 9 6 2 6 5 6 8 2 10 1 1 6 8 8 4 5 2 6 8 8 5 9 2 3 3 7 7 10 5 4 2 10 6 7 6 5 4 10 6 10 3 9 9 1 5\n3 5 6 4 2 3 2 9 3 8 3 1 10 7 4 3 6 9 3 5 9 5 3 10 4 7 9 7 4 3 3 6 9 8 1 1 10 9 1 6 8 8 8 2 1 6 10 1 8 6 3 5 7 7 10 4 6 6 9 1 5 3 5 10 4 4 1 7 9 7 5 10 6 5 4 1 9 6 4 5 7 3 1 10 2 10 6 6 1 10 7 5 1 4 2 9 2 7 3 10\n727992321",
"output": "340960284"
},
{
"input": "100\n2 5 5 6 5 2 8 10 6 1 5 3 10 3 8 6 4 5 7 9 7 1 3 3 5 2 3 7 9 3 7 2 7 6 7 10 5 9 2 4 8 2 3 8 6 6 8 4 1 2 10 5 2 8 4 3 1 3 8 3 2 4 4 6 8 1 9 8 9 9 1 7 1 9 2 4 6 2 1 9 2 7 9 6 6 7 1 9 3 1 6 10 3 9 10 5 3 3 9 8\n6 5 3 1 3 3 8 6 5 4 2 3 9 3 9 9 10 5 10 6 7 8 8 7 8 4 2 4 4 9 1 3 1 5 8 4 8 9 7 9 7 8 4 9 9 9 4 2 9 1 3 10 6 4 5 3 2 8 1 5 1 8 10 10 3 3 7 1 2 4 4 3 3 5 9 8 9 8 5 9 4 8 10 6 7 4 1 9 4 7 1 8 3 3 5 9 8 6 5 4\n608692736",
"output": "550164992"
},
{
"input": "2\n1 2\n1 2\n4",
"output": "1"
},
{
"input": "4\n1 2 3 4\n4 3 2 1\n1009",
"output": "16"
},
{
"input": "5\n1 2 3 3 5\n1 2 3 5 3\n12",
"output": "0"
},
{
"input": "1\n1000000000\n1000000000\n2",
"output": "1"
},
{
"input": "2\n1 2\n2 2\n4",
"output": "3"
}
] | 778 | 47,718,400 | 3 | 6,749 |
|
229 | Shifts | [
"brute force",
"two pointers"
] | null | null | You are given a table consisting of *n* rows and *m* columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.
To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100".
Determine the minimum number of moves needed to make some table column consist only of numbers 1. | The first line contains two space-separated integers: *n* (1<=β€<=*n*<=β€<=100)Β β the number of rows in the table and *m* (1<=β€<=*m*<=β€<=104)Β β the number of columns in the table. Then *n* lines follow, each of them contains *m* characters "0" or "1": the *j*-th character of the *i*-th line describes the contents of the cell in the *i*-th row and in the *j*-th column of the table.
It is guaranteed that the description of the table contains no other characters besides "0" and "1". | Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. | [
"3 6\n101010\n000100\n100000\n",
"2 3\n111\n000\n"
] | [
"3\n",
"-1\n"
] | In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s.
In the second sample one can't shift the rows to get a column containing only 1s. | [
{
"input": "3 6\n101010\n000100\n100000",
"output": "3"
},
{
"input": "2 3\n111\n000",
"output": "-1"
},
{
"input": "1 1\n1",
"output": "0"
},
{
"input": "1 1\n0",
"output": "-1"
},
{
"input": "3 1\n1\n1\n0",
"output": "-1"
},
{
"input": "6 2\n10\n11\n01\n01\n10\n11",
"output": "2"
},
{
"input": "3 3\n001\n010\n100",
"output": "2"
},
{
"input": "4 4\n0001\n0100\n0010\n1000",
"output": "4"
},
{
"input": "5 5\n10000\n01000\n00100\n00010\n00001",
"output": "6"
},
{
"input": "5 5\n10001\n00100\n01000\n01001\n11111",
"output": "2"
},
{
"input": "5 5\n11111\n11111\n11111\n11111\n00000",
"output": "-1"
},
{
"input": "5 10\n0001000100\n1000001000\n0001000001\n0100001010\n0110100000",
"output": "5"
},
{
"input": "6 6\n111000\n011100\n001110\n000111\n100011\n110001",
"output": "4"
},
{
"input": "2 9\n101010101\n010101010",
"output": "1"
},
{
"input": "4 6\n000001\n100000\n100000\n100000",
"output": "1"
},
{
"input": "3 6\n000010\n010000\n000100",
"output": "3"
},
{
"input": "4 10\n0000101010\n1010101010\n0101010101\n0000010100",
"output": "2"
},
{
"input": "10 10\n0000000000\n0000000010\n0010000000\n0111000010\n1000000000\n0000000100\n0000000100\n0000100100\n0010000000\n0000100000",
"output": "-1"
},
{
"input": "10 10\n0000000000\n0000001000\n0000000100\n0101000100\n0000000000\n0000000000\n1000110000\n1011010010\n0000100000\n0000001001",
"output": "-1"
},
{
"input": "10 10\n0001001101\n0010001010\n1100000000\n0110110110\n1011011010\n1001001001\n0100010001\n0110000100\n0000100000\n1000010000",
"output": "8"
},
{
"input": "10 10\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111",
"output": "0"
},
{
"input": "2 5\n10000\n00001",
"output": "1"
},
{
"input": "3 7\n1000000\n0000010\n1000000",
"output": "2"
},
{
"input": "4 5\n10010\n11001\n00010\n11000",
"output": "2"
},
{
"input": "2 10\n0000000001\n1000000000",
"output": "1"
},
{
"input": "5 5\n10000\n10000\n00001\n10000\n10000",
"output": "1"
},
{
"input": "3 4\n0001\n0001\n1000",
"output": "1"
},
{
"input": "3 6\n101010\n000010\n100000",
"output": "2"
},
{
"input": "4 7\n0100000\n0100000\n0000001\n0000001",
"output": "4"
},
{
"input": "5 1\n0\n0\n0\n0\n0",
"output": "-1"
},
{
"input": "3 5\n00001\n10000\n00001",
"output": "1"
},
{
"input": "3 1\n0\n0\n0",
"output": "-1"
}
] | 218 | 0 | 0 | 6,754 |
|
573 | Bear and Drawing | [
"constructive algorithms",
"dfs and similar",
"trees"
] | null | null | Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but why would bears do it? Limak lives in the forest and he decides to draw a tree.
Recall that tree is a connected graph consisting of *n* vertices and *n*<=-<=1 edges.
Limak chose a tree with *n* vertices. He has infinite strip of paper with two parallel rows of dots. Little bear wants to assign vertices of a tree to some *n* distinct dots on a paper so that edges would intersect only at their endpoints β drawn tree must be planar. Below you can see one of correct drawings for the first sample test.
Is it possible for Limak to draw chosen tree? | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105).
Next *n*<=-<=1 lines contain description of a tree. *i*-th of them contains two space-separated integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*) denoting an edge between vertices *a**i* and *b**i*. It's guaranteed that given description forms a tree. | Print "Yes" (without the quotes) if Limak can draw chosen tree. Otherwise, print "No" (without the quotes). | [
"8\n1 2\n1 3\n1 6\n6 4\n6 7\n6 5\n7 8\n",
"13\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\n3 10\n4 11\n4 12\n4 13\n"
] | [
"Yes\n",
"No\n"
] | none | [] | 30 | 0 | 0 | 6,755 |
|
616 | Sum of Remainders | [
"implementation",
"math",
"number theory"
] | null | null | Calculate the value of the sum: *n* mod 1 + *n* mod 2 + *n* mod 3 + ... + *n* mod *m*. As the result can be very large, you should print the value modulo 109<=+<=7 (the remainder when divided by 109<=+<=7).
The modulo operator *a* mod *b* stands for the remainder after dividing *a* by *b*. For example 10 mod 3 = 1. | The only line contains two integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=1013) β the parameters of the sum. | Print integer *s* β the value of the required sum modulo 109<=+<=7. | [
"3 4\n",
"4 4\n",
"1 1\n"
] | [
"4\n",
"1\n",
"0\n"
] | none | [
{
"input": "3 4",
"output": "4"
},
{
"input": "4 4",
"output": "1"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "20000000 10000000",
"output": "176305083"
},
{
"input": "10000000000000 10000000000000",
"output": "869957328"
},
{
"input": "1 10000000000000",
"output": "999930006"
},
{
"input": "10000000000000 1",
"output": "0"
},
{
"input": "100 100",
"output": "1701"
},
{
"input": "1000 1000",
"output": "176919"
},
{
"input": "500 1000",
"output": "294117"
},
{
"input": "1010 435",
"output": "48431"
},
{
"input": "997167959139 7344481199252",
"output": "695194729"
},
{
"input": "7244641009859 6300054748096",
"output": "955368330"
},
{
"input": "3483524125987 5259923264237",
"output": "953462184"
},
{
"input": "9726702209411 4215496813081",
"output": "517714807"
},
{
"input": "5965585325539 3175365329221",
"output": "286054680"
},
{
"input": "2213058376259 2126643910770",
"output": "971336268"
},
{
"input": "8451941492387 3119072235422",
"output": "394884104"
},
{
"input": "4690824608515 2078940751563",
"output": "858876367"
},
{
"input": "1666199186071 28985049",
"output": "729884985"
},
{
"input": "9999999999989 9999998979371",
"output": "915094997"
},
{
"input": "9999999999999 9999999999957",
"output": "344038473"
},
{
"input": "56598 56",
"output": "755"
}
] | 2,000 | 0 | 0 | 6,759 |
|
797 | Minimal string | [
"data structures",
"greedy",
"strings"
] | null | null | Petya recieved a gift of a string *s* with length up to 105 characters for his birthday. He took two more empty strings *t* and *u* and decided to play a game. This game has two possible moves:
- Extract the first character of *s* and append *t* with this character. - Extract the last character of *t* and append *u* with this character.
Petya wants to get strings *s* and *t* empty and string *u* lexicographically minimal.
You should write a program that will help Petya win the game. | First line contains non-empty string *s* (1<=β€<=|*s*|<=β€<=105), consisting of lowercase English letters. | Print resulting string *u*. | [
"cab\n",
"acdb\n"
] | [
"abc\n",
"abdc\n"
] | none | [
{
"input": "cab",
"output": "abc"
},
{
"input": "acdb",
"output": "abdc"
},
{
"input": "a",
"output": "a"
},
{
"input": "ab",
"output": "ab"
},
{
"input": "ba",
"output": "ab"
},
{
"input": "dijee",
"output": "deeji"
},
{
"input": "bhrmc",
"output": "bcmrh"
},
{
"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "bababaaababaabbbbbabbbbbbaaabbabaaaaabbbbbaaaabbbbabaabaabababbbabbabbabaaababbabbababaaaaabaaaabbba",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
},
{
"input": "bccbbcccbccbacacbaccaababcbaababaaaaabcaaabcaacbabcaababaabaccacacccbacbcacbbbaacaaccccabbbbacbcbbba",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbcbcbbbbcccccbbbccbcbccccccbbbcbbccbcbbbbcbbccbccbccbcccbbccb"
},
{
"input": "eejahjfbbcdhbieiigaihidhageiechaadieecaaehcehjbddgcjgagdfgffdaaihbecebdjhjagghecdhbhdfbedhfhfafbjajg",
"output": "aaaaaaaaaaaaagjjbffhfhdebfdhbhdcehggjhjdbecebhidffgfdggjcgddbjhecheceeidhceieghdihigiieibhdcbbfjhjee"
},
{
"input": "bnrdfnybkzepmluyrhofwnwvfmkdwolvyzrqhuhztvlwjldqmoyxzytpfmrgouymeupxrvpbesyxixnrfbxnqcwgmgjstknqtwrr",
"output": "bbbbcggjknqrrwttsmwqnxfrnxixysepvrxpuemyuogrmfptyzxyomqdljwlvtzhuhqrzyvlowdkmfvwnwfohryulmpezkynfdrn"
},
{
"input": "bcaeaae",
"output": "aaaecbe"
},
{
"input": "edcadcbcdd",
"output": "abccdcddde"
},
{
"input": "a",
"output": "a"
},
{
"input": "a",
"output": "a"
},
{
"input": "a",
"output": "a"
},
{
"input": "b",
"output": "b"
},
{
"input": "b",
"output": "b"
},
{
"input": "a",
"output": "a"
},
{
"input": "c",
"output": "c"
},
{
"input": "a",
"output": "a"
},
{
"input": "b",
"output": "b"
},
{
"input": "c",
"output": "c"
},
{
"input": "b",
"output": "b"
},
{
"input": "a",
"output": "a"
},
{
"input": "e",
"output": "e"
},
{
"input": "b",
"output": "b"
},
{
"input": "b",
"output": "b"
},
{
"input": "aa",
"output": "aa"
},
{
"input": "aa",
"output": "aa"
},
{
"input": "aa",
"output": "aa"
},
{
"input": "aa",
"output": "aa"
},
{
"input": "bb",
"output": "bb"
},
{
"input": "bb",
"output": "bb"
},
{
"input": "ba",
"output": "ab"
},
{
"input": "ca",
"output": "ac"
},
{
"input": "ab",
"output": "ab"
},
{
"input": "cb",
"output": "bc"
},
{
"input": "bb",
"output": "bb"
},
{
"input": "aa",
"output": "aa"
},
{
"input": "da",
"output": "ad"
},
{
"input": "ab",
"output": "ab"
},
{
"input": "cd",
"output": "cd"
},
{
"input": "aaa",
"output": "aaa"
},
{
"input": "aaa",
"output": "aaa"
},
{
"input": "aaa",
"output": "aaa"
},
{
"input": "aab",
"output": "aab"
},
{
"input": "aaa",
"output": "aaa"
},
{
"input": "baa",
"output": "aab"
},
{
"input": "bab",
"output": "abb"
},
{
"input": "baa",
"output": "aab"
},
{
"input": "ccc",
"output": "ccc"
},
{
"input": "ddd",
"output": "ddd"
},
{
"input": "ccd",
"output": "ccd"
},
{
"input": "bca",
"output": "acb"
},
{
"input": "cde",
"output": "cde"
},
{
"input": "ece",
"output": "cee"
},
{
"input": "bdd",
"output": "bdd"
},
{
"input": "aaaa",
"output": "aaaa"
},
{
"input": "aaaa",
"output": "aaaa"
},
{
"input": "aaaa",
"output": "aaaa"
},
{
"input": "abaa",
"output": "aaab"
},
{
"input": "abab",
"output": "aabb"
},
{
"input": "bbbb",
"output": "bbbb"
},
{
"input": "bbba",
"output": "abbb"
},
{
"input": "caba",
"output": "aabc"
},
{
"input": "ccbb",
"output": "bbcc"
},
{
"input": "abac",
"output": "aabc"
},
{
"input": "daba",
"output": "aabd"
},
{
"input": "cdbb",
"output": "bbdc"
},
{
"input": "bddd",
"output": "bddd"
},
{
"input": "dacb",
"output": "abcd"
},
{
"input": "abcc",
"output": "abcc"
},
{
"input": "aaaaa",
"output": "aaaaa"
},
{
"input": "aaaaa",
"output": "aaaaa"
},
{
"input": "aaaaa",
"output": "aaaaa"
},
{
"input": "baaab",
"output": "aaabb"
},
{
"input": "aabbb",
"output": "aabbb"
},
{
"input": "aabaa",
"output": "aaaab"
},
{
"input": "abcba",
"output": "aabcb"
},
{
"input": "bacbc",
"output": "abbcc"
},
{
"input": "bacba",
"output": "aabcb"
},
{
"input": "bdbda",
"output": "adbdb"
},
{
"input": "accbb",
"output": "abbcc"
},
{
"input": "dbccc",
"output": "bcccd"
},
{
"input": "decca",
"output": "acced"
},
{
"input": "dbbdd",
"output": "bbddd"
},
{
"input": "accec",
"output": "accce"
},
{
"input": "aaaaaa",
"output": "aaaaaa"
},
{
"input": "aaaaaa",
"output": "aaaaaa"
},
{
"input": "aaaaaa",
"output": "aaaaaa"
},
{
"input": "bbbbab",
"output": "abbbbb"
},
{
"input": "bbbbab",
"output": "abbbbb"
},
{
"input": "aaaaba",
"output": "aaaaab"
},
{
"input": "cbbbcc",
"output": "bbbccc"
},
{
"input": "aaacac",
"output": "aaaacc"
},
{
"input": "bacbbc",
"output": "abbbcc"
},
{
"input": "cacacc",
"output": "aacccc"
},
{
"input": "badbdc",
"output": "abbcdd"
},
{
"input": "ddadad",
"output": "aadddd"
},
{
"input": "ccdece",
"output": "cccede"
},
{
"input": "eecade",
"output": "acdeee"
},
{
"input": "eabdcb",
"output": "abbcde"
},
{
"input": "aaaaaaa",
"output": "aaaaaaa"
},
{
"input": "aaaaaaa",
"output": "aaaaaaa"
},
{
"input": "aaaaaaa",
"output": "aaaaaaa"
},
{
"input": "aaabbaa",
"output": "aaaaabb"
},
{
"input": "baaabab",
"output": "aaaabbb"
},
{
"input": "bbababa",
"output": "aaabbbb"
},
{
"input": "bcccacc",
"output": "acccbcc"
},
{
"input": "cbbcccc",
"output": "bbccccc"
},
{
"input": "abacaaa",
"output": "aaaaacb"
},
{
"input": "ccdbdac",
"output": "acdbdcc"
},
{
"input": "bbacaba",
"output": "aaabcbb"
},
{
"input": "abbaccc",
"output": "aabbccc"
},
{
"input": "bdcbcab",
"output": "abcbcdb"
},
{
"input": "dabcbce",
"output": "abbccde"
},
{
"input": "abaaabe",
"output": "aaaabbe"
},
{
"input": "aaaaaaaa",
"output": "aaaaaaaa"
},
{
"input": "aaaaaaaa",
"output": "aaaaaaaa"
},
{
"input": "aaaaaaaa",
"output": "aaaaaaaa"
},
{
"input": "ababbbba",
"output": "aaabbbbb"
},
{
"input": "aaaaaaba",
"output": "aaaaaaab"
},
{
"input": "babbbaab",
"output": "aaabbbbb"
},
{
"input": "bcaccaab",
"output": "aaabcccb"
},
{
"input": "bbccaabc",
"output": "aabccbbc"
},
{
"input": "cacaaaac",
"output": "aaaaaccc"
},
{
"input": "daacbddc",
"output": "aabccddd"
},
{
"input": "cdbdcdaa",
"output": "aadcdbdc"
},
{
"input": "bccbdacd",
"output": "acdbccbd"
},
{
"input": "abbeaade",
"output": "aaadebbe"
},
{
"input": "ccabecba",
"output": "aabcebcc"
},
{
"input": "ececaead",
"output": "aadecece"
},
{
"input": "aaaaaaaaa",
"output": "aaaaaaaaa"
},
{
"input": "aaaaaaaaa",
"output": "aaaaaaaaa"
},
{
"input": "aaaaaaaaa",
"output": "aaaaaaaaa"
},
{
"input": "aabaaabbb",
"output": "aaaaabbbb"
},
{
"input": "abbbbbaab",
"output": "aaabbbbbb"
},
{
"input": "bbbaababb",
"output": "aaabbbbbb"
},
{
"input": "babcaaccb",
"output": "aaabcccbb"
},
{
"input": "ccbcabaac",
"output": "aaabcbccc"
},
{
"input": "caaaccccb",
"output": "aaabccccc"
},
{
"input": "abbcdbddb",
"output": "abbbbdddc"
},
{
"input": "dbcaacbbb",
"output": "aabbbccbd"
},
{
"input": "cadcbddac",
"output": "aacddbcdc"
},
{
"input": "ecebadadb",
"output": "aabddbece"
},
{
"input": "bdbeeccdd",
"output": "bbccddeed"
},
{
"input": "daaedecda",
"output": "aaadceded"
},
{
"input": "aaaaaaaaaa",
"output": "aaaaaaaaaa"
},
{
"input": "aaaaaaaaaa",
"output": "aaaaaaaaaa"
},
{
"input": "aaaaaaaaaa",
"output": "aaaaaaaaaa"
},
{
"input": "abaaaaabbb",
"output": "aaaaaabbbb"
},
{
"input": "bbaaaabaaa",
"output": "aaaaaaabbb"
},
{
"input": "bbabbaaaaa",
"output": "aaaaaabbbb"
},
{
"input": "cbaabcaacc",
"output": "aaaacbbccc"
},
{
"input": "aaaaccccab",
"output": "aaaaabcccc"
},
{
"input": "bccaccaacc",
"output": "aaaccccbcc"
},
{
"input": "dbdccdcacd",
"output": "accdccdbdd"
},
{
"input": "caaddaaccb",
"output": "aaaabccddc"
},
{
"input": "adbbabcbdc",
"output": "aabbbbccdd"
},
{
"input": "cdeabdbbad",
"output": "aabbdbdedc"
},
{
"input": "eeddcbeeec",
"output": "bcceeeddee"
},
{
"input": "bbcebddeba",
"output": "abeddbecbb"
}
] | 108 | 307,200 | 0 | 6,760 |
|
397 | On Segment's Own Points | [
"implementation"
] | null | null | Our old friend Alexey has finally entered the University of City N β the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm.
The dorm has exactly one straight dryer β a 100 centimeter long rope to hang clothes on. The dryer has got a coordinate system installed: the leftmost end of the dryer has coordinate 0, and the opposite end has coordinate 100. Overall, the university has *n* students. Dean's office allows *i*-th student to use the segment (*l**i*,<=*r**i*) of the dryer. However, the dean's office actions are contradictory and now one part of the dryer can belong to multiple students!
Alexey don't like when someone touch his clothes. That's why he want make it impossible to someone clothes touch his ones. So Alexey wonders: what is the total length of the parts of the dryer that he may use in a such way that clothes of the others (*n*<=-<=1) students aren't drying there. Help him! Note that Alexey, as the most respected student, has number 1. | The first line contains a positive integer *n* (1<=β€<=*n*<=β€<=100). The (*i*<=+<=1)-th line contains integers *l**i* and *r**i* (0<=β€<=*l**i*<=<<=*r**i*<=β€<=100) βΒ the endpoints of the corresponding segment for the *i*-th student. | On a single line print a single number *k*, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments. | [
"3\n0 5\n2 8\n1 6\n",
"3\n0 10\n1 5\n7 15\n"
] | [
"1\n",
"3\n"
] | Note that it's not important are clothes drying on the touching segments (e.g. (0,β1) and (1,β2)) considered to be touching or not because you need to find the length of segments.
In the first test sample Alexey may use the only segment (0,β1). In such case his clothes will not touch clothes on the segments (1,β6) and (2,β8). The length of segment (0,β1) is 1.
In the second test sample Alexey may dry his clothes on segments (0,β1) and (5,β7). Overall length of these segments is 3. | [
{
"input": "3\n0 5\n2 8\n1 6",
"output": "1"
},
{
"input": "3\n0 10\n1 5\n7 15",
"output": "3"
},
{
"input": "1\n0 100",
"output": "100"
},
{
"input": "2\n1 9\n1 9",
"output": "0"
},
{
"input": "2\n1 9\n5 10",
"output": "4"
},
{
"input": "2\n1 9\n3 5",
"output": "6"
},
{
"input": "2\n3 5\n1 9",
"output": "0"
},
{
"input": "10\n43 80\n39 75\n26 71\n4 17\n11 57\n31 42\n1 62\n9 19\n27 76\n34 53",
"output": "4"
},
{
"input": "50\n33 35\n98 99\n1 2\n4 6\n17 18\n63 66\n29 30\n35 37\n44 45\n73 75\n4 5\n39 40\n92 93\n96 97\n23 27\n49 50\n2 3\n60 61\n43 44\n69 70\n7 8\n45 46\n21 22\n85 86\n48 49\n41 43\n70 71\n10 11\n27 28\n71 72\n6 7\n15 16\n46 47\n89 91\n54 55\n19 21\n86 87\n37 38\n77 82\n84 85\n54 55\n93 94\n45 46\n37 38\n75 76\n22 23\n50 52\n38 39\n1 2\n66 67",
"output": "2"
},
{
"input": "2\n1 5\n7 9",
"output": "4"
},
{
"input": "2\n1 5\n3 5",
"output": "2"
},
{
"input": "2\n1 5\n1 2",
"output": "3"
},
{
"input": "5\n5 10\n5 10\n5 10\n5 10\n5 10",
"output": "0"
},
{
"input": "6\n1 99\n33 94\n68 69\n3 35\n93 94\n5 98",
"output": "3"
},
{
"input": "11\n2 98\n63 97\n4 33\n12 34\n34 65\n23 31\n43 54\n82 99\n15 84\n23 52\n4 50",
"output": "2"
},
{
"input": "10\n95 96\n19 20\n72 73\n1 2\n25 26\n48 49\n90 91\n22 23\n16 17\n16 17",
"output": "1"
},
{
"input": "11\n1 100\n63 97\n4 33\n12 34\n34 65\n23 31\n43 54\n82 99\n15 84\n23 52\n4 50",
"output": "4"
},
{
"input": "21\n0 100\n81 90\n11 68\n18 23\n75 78\n45 86\n37 58\n15 21\n40 98\n53 100\n10 70\n14 75\n1 92\n23 81\n13 66\n93 100\n6 34\n22 87\n27 84\n15 63\n54 91",
"output": "1"
},
{
"input": "10\n60 66\n5 14\n1 3\n55 56\n70 87\n34 35\n16 21\n23 24\n30 31\n25 27",
"output": "6"
},
{
"input": "40\n29 31\n22 23\n59 60\n70 71\n42 43\n13 15\n11 12\n64 65\n1 2\n62 63\n54 56\n8 9\n2 3\n53 54\n27 28\n48 49\n72 73\n17 18\n46 47\n18 19\n43 44\n39 40\n83 84\n63 64\n52 53\n33 34\n3 4\n24 25\n74 75\n0 1\n61 62\n68 69\n80 81\n5 6\n36 37\n81 82\n50 51\n66 67\n69 70\n20 21",
"output": "2"
},
{
"input": "15\n22 31\n0 4\n31 40\n77 80\n81 83\n11 13\n59 61\n53 59\n51 53\n87 88\n14 22\n43 45\n8 10\n45 47\n68 71",
"output": "9"
},
{
"input": "31\n0 100\n2 97\n8 94\n9 94\n14 94\n15 93\n15 90\n17 88\n19 88\n19 87\n20 86\n25 86\n30 85\n32 85\n35 82\n35 81\n36 80\n37 78\n38 74\n38 74\n39 71\n40 69\n40 68\n41 65\n43 62\n44 62\n45 61\n45 59\n46 57\n49 54\n50 52",
"output": "5"
},
{
"input": "21\n0 97\n46 59\n64 95\n3 16\n86 95\n55 71\n51 77\n26 28\n47 88\n30 40\n26 34\n2 12\n9 10\n4 19\n35 36\n41 92\n1 16\n41 78\n56 81\n23 35\n40 68",
"output": "7"
},
{
"input": "27\n0 97\n7 9\n6 9\n12 33\n12 26\n15 27\n10 46\n33 50\n31 47\n15 38\n12 44\n21 35\n24 37\n51 52\n65 67\n58 63\n53 60\n63 68\n57 63\n60 68\n55 58\n74 80\n70 75\n89 90\n81 85\n93 99\n93 98",
"output": "19"
},
{
"input": "20\n23 24\n22 23\n84 86\n6 10\n40 45\n11 13\n24 27\n81 82\n53 58\n87 90\n14 15\n49 50\n70 75\n75 78\n98 100\n66 68\n18 21\n1 2\n92 93\n34 37",
"output": "1"
},
{
"input": "11\n2 100\n34 65\n4 50\n63 97\n82 99\n43 54\n23 52\n4 33\n15 84\n23 31\n12 34",
"output": "3"
},
{
"input": "60\n73 75\n6 7\n69 70\n15 16\n54 55\n66 67\n7 8\n39 40\n38 39\n37 38\n1 2\n46 47\n7 8\n21 22\n23 27\n15 16\n45 46\n37 38\n60 61\n4 6\n63 66\n10 11\n33 35\n43 44\n2 3\n4 6\n10 11\n93 94\n45 46\n7 8\n44 45\n41 43\n35 37\n17 18\n48 49\n89 91\n27 28\n46 47\n71 72\n1 2\n75 76\n49 50\n84 85\n17 18\n98 99\n54 55\n46 47\n19 21\n77 82\n29 30\n4 5\n70 71\n85 86\n96 97\n86 87\n92 93\n22 23\n50 52\n44 45\n63 66",
"output": "2"
},
{
"input": "40\n47 48\n42 44\n92 94\n15 17\n20 22\n11 13\n37 39\n6 8\n39 40\n35 37\n21 22\n41 42\n77 78\n76 78\n69 71\n17 19\n18 19\n17 18\n84 85\n9 10\n11 12\n51 52\n99 100\n7 8\n97 99\n22 23\n60 62\n7 8\n67 69\n20 22\n13 14\n89 91\n15 17\n12 13\n56 57\n37 39\n29 30\n24 26\n37 38\n25 27",
"output": "1"
},
{
"input": "10\n28 36\n18 26\n28 35\n95 100\n68 72\n41 42\n76 84\n99 100\n6 8\n58 60",
"output": "1"
},
{
"input": "20\n69 72\n88 92\n77 80\n64 69\n66 67\n79 81\n91 96\n78 83\n81 86\n11 12\n48 53\n22 23\n81 84\n89 92\n56 60\n1 4\n1 5\n60 62\n20 23\n63 66",
"output": "3"
},
{
"input": "71\n1 99\n11 69\n86 92\n7 49\n31 70\n42 53\n48 81\n86 96\n36 91\n19 38\n39 91\n41 64\n87 93\n83 97\n40 41\n3 32\n15 18\n58 65\n22 32\n1 71\n58 86\n64 77\n15 69\n4 34\n42 89\n9 66\n15 18\n58 65\n59 96\n39 89\n19 38\n6 63\n26 73\n29 47\n55 88\n5 78\n41 74\n48 81\n20 71\n59 96\n42 49\n4 69\n41 74\n87 93\n0 65\n2 34\n15 18\n10 56\n55 88\n33 56\n42 89\n86 92\n42 81\n65 82\n5 78\n13 52\n32 85\n7 65\n59 96\n4 65\n46 69\n10 56\n42 89\n4 69\n0 65\n32 35\n5 78\n32 75\n42 53\n55 59\n64 77",
"output": "2"
},
{
"input": "1\n1 2",
"output": "1"
}
] | 93 | 6,144,000 | 0 | 6,764 |
|
484 | Bits | [
"bitmasks",
"constructive algorithms"
] | null | null | Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negative integer *x*.
You are given multiple queries consisting of pairs of integers *l* and *r*. For each query, find the *x*, such that *l*<=β€<=*x*<=β€<=*r*, and is maximum possible. If there are multiple such numbers find the smallest of them. | The first line contains integer *n*Β β the number of queries (1<=β€<=*n*<=β€<=10000).
Each of the following *n* lines contain two integers *l**i*,<=*r**i*Β β the arguments for the corresponding query (0<=β€<=*l**i*<=β€<=*r**i*<=β€<=1018). | For each query print the answer in a separate line. | [
"3\n1 2\n2 4\n1 10\n"
] | [
"1\n3\n7\n"
] | The binary representations of numbers from 1 to 10 are listed below:
1<sub class="lower-index">10</sub>β=β1<sub class="lower-index">2</sub>
2<sub class="lower-index">10</sub>β=β10<sub class="lower-index">2</sub>
3<sub class="lower-index">10</sub>β=β11<sub class="lower-index">2</sub>
4<sub class="lower-index">10</sub>β=β100<sub class="lower-index">2</sub>
5<sub class="lower-index">10</sub>β=β101<sub class="lower-index">2</sub>
6<sub class="lower-index">10</sub>β=β110<sub class="lower-index">2</sub>
7<sub class="lower-index">10</sub>β=β111<sub class="lower-index">2</sub>
8<sub class="lower-index">10</sub>β=β1000<sub class="lower-index">2</sub>
9<sub class="lower-index">10</sub>β=β1001<sub class="lower-index">2</sub>
10<sub class="lower-index">10</sub>β=β1010<sub class="lower-index">2</sub> | [
{
"input": "3\n1 2\n2 4\n1 10",
"output": "1\n3\n7"
},
{
"input": "55\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n2 2\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n3 3\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n4 4\n4 5\n4 6\n4 7\n4 8\n4 9\n4 10\n5 5\n5 6\n5 7\n5 8\n5 9\n5 10\n6 6\n6 7\n6 8\n6 9\n6 10\n7 7\n7 8\n7 9\n7 10\n8 8\n8 9\n8 10\n9 9\n9 10\n10 10",
"output": "1\n1\n3\n3\n3\n3\n7\n7\n7\n7\n2\n3\n3\n3\n3\n7\n7\n7\n7\n3\n3\n3\n3\n7\n7\n7\n7\n4\n5\n5\n7\n7\n7\n7\n5\n5\n7\n7\n7\n7\n6\n7\n7\n7\n7\n7\n7\n7\n7\n8\n9\n9\n9\n9\n10"
},
{
"input": "18\n1 10\n1 100\n1 1000\n1 10000\n1 100000\n1 1000000\n1 10000000\n1 100000000\n1 1000000000\n1 10000000000\n1 100000000000\n1 1000000000000\n1 10000000000000\n1 100000000000000\n1 1000000000000000\n1 10000000000000000\n1 100000000000000000\n1 1000000000000000000",
"output": "7\n63\n511\n8191\n65535\n524287\n8388607\n67108863\n536870911\n8589934591\n68719476735\n549755813887\n8796093022207\n70368744177663\n562949953421311\n9007199254740991\n72057594037927935\n576460752303423487"
},
{
"input": "3\n0 0\n1 3\n2 4",
"output": "0\n3\n3"
},
{
"input": "17\n0 0\n0 8\n1 8\n36 39\n3 4\n3 7\n2 17\n8 12\n9 12\n10 12\n10 15\n6 14\n8 15\n9 15\n15 15\n100000000000000000 1000000000000000000\n99999999999999999 1000000000000000000",
"output": "0\n7\n7\n39\n3\n7\n15\n11\n11\n11\n15\n7\n15\n15\n15\n576460752303423487\n576460752303423487"
}
] | 109 | 0 | 0 | 6,774 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.